From 098bd679d781dbcf550cd1c2d1f62dbe1da39b93 Mon Sep 17 00:00:00 2001 From: kenobijon Date: Fri, 19 Jun 2026 14:41:57 -0500 Subject: [PATCH] fix: force even dimensions before H.264 encode in video robustness aug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit libx264 with yuv420p rejects odd width/height, so _h264_roundtrip_ffmpeg exited non-zero on any frame with an odd dimension and the pass silently fell back to cv2/per-frame JPEG — while params["method"] could still imply a CRF roundtrip that never happened. The resolution ladder's max(2, round(...)) did not guarantee evenness either. Trim a trailing row/column to even dimensions once before encoding, covering both the ffmpeg -crf and cv2 avc1 paths (both are H.264/4:2:0). At most one pixel per axis is dropped, and the frame is resized to target afterward, so the effect on the benchmark is nil. Guarded so a degenerate <2px axis is left untouched for the fallback to handle. Verified: 97x131 input (both dims odd), odd input with scale_factor=0.5, and an even-dim regression case all now engage method="ffmpeg_crf". Reported by Cursor bot review on #113. Co-Authored-By: Claude Opus 4.8 --- src/gasbench/processing/transforms.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/gasbench/processing/transforms.py b/src/gasbench/processing/transforms.py index bb5d6a5..f9824ce 100644 --- a/src/gasbench/processing/transforms.py +++ b/src/gasbench/processing/transforms.py @@ -290,6 +290,16 @@ def apply_video_robustness_augmentations( T, H, W, C = video_array.shape + # H.264 with yuv420p (both the ffmpeg -crf and cv2 avc1 encoders) requires + # even width and height; libx264 rejects odd dimensions outright. Trim a + # trailing row/column when needed so encoding succeeds instead of silently + # erroring into the fallback path (which would also mis-set method). At most + # one pixel per axis is dropped, and the frame is resized to target anyway. + even_h, even_w = H - (H % 2), W - (W % 2) + if (even_h, even_w) != (H, W) and even_h >= 2 and even_w >= 2: + video_array = video_array[:, :even_h, :even_w, :] + T, H, W, C = video_array.shape + method = "ffmpeg_crf" compressed = _h264_roundtrip_ffmpeg(video_array, crf, fps) if compressed is None: