fix: force even dimensions before H.264 encode (odd sizes broke ffmpeg CRF)#114
Merged
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the odd-dimension bug flagged by the Cursor bot review on #113.
_h264_roundtrip_ffmpegencodes withlibx264 -pix_fmt yuv420p, which rejects odd width/height. Any frame with an odd dimension made the ffmpeg subprocess exit non-zero, so the helper returnedNoneand the robustness pass silently fell back to cv2 / per-frame JPEG — even with ffmpeg on PATH — whileparams["method"]could still imply a CRF roundtrip that never actually ran. The resolution ladder'smax(2, round(...))didn't guarantee evenness either.Fix
Trim a trailing row/column to even dimensions once before encoding, covering both the ffmpeg
-crfand 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_sizeafterward, so the effect on the benchmark is nil. Guarded so a degenerate <2px axis is left untouched for the fallback to handle.Verification
End-to-end with real numpy/opencv/Pillow/ffmpeg — all now engage
method == "ffmpeg_crf":ffmpeg_crf✓scale_factor=0.5ffmpeg_crf✓ffmpeg_crfffmpeg_crf✓🤖 Generated with Claude Code
Note
Low Risk
Localized preprocessing in the video robustness path with at most one pixel trimmed per axis; no auth, data, or API surface changes.
Overview
Video robustness now forces even width and height on frames immediately before the H.264 roundtrip in
apply_video_robustness_augmentations, solibx264/yuv420p(ffmpeg CRF and cv2avc1) no longer fail on odd sizes.When either dimension is odd, the code drops at most one trailing row or column (only if both sides stay ≥2), then re-reads
T, H, W, Cbefore choosing the encode path. That stops silent fallback to cv2 or per-frame JPEG when ffmpeg is available, and keeps reportedparams["method"]aligned with the encoder that actually ran. Frames are still resized to the model target afterward, so benchmark impact is negligible.Reviewed by Cursor Bugbot for commit 098bd67. Bugbot is set up for automated code reviews on this repo. Configure here.