Repository navigation
Cap raw-frame reads at the video frame-pixel budget - #807
cursor[bot] wants to merge 2 commits into
Conversation
Camera-motion decoding, RGB sampling, and the camera_stability check sized each raw-frame read from the dimensions ffprobe reported, so a header that declared an enormous frame made the measurement process allocate it. Refuse probed dimensions over the 33554432-pixel budget that probe_video and video import already enforce, refuse any raw-frame read shape over it inside the reader, check scaled RGB samples before decoding, and pass -max_pixels to every raw decode so a stream that changes between probe and decode cannot push FFmpeg past the bound. The window-measurement luma reader applies the caller's frame limit to the Y4M header it reads.
[Medium impact] The reviewed changes appear safe to merge; no blocking issue was found.
|
kstonekuan
left a comment
There was a problem hiding this comment.
Could you include a regression test in this PR that checks an over-budget frame is rejected before decoding, and use one source for the default pixel budget shared with VideoLimits?
|
@cursor Could you update this PR to share the default pixel budget with VideoLimits and add a small test of rejection before decode using an ordinary fixture and a low test limit? |
|
I've updated PR #807. Shared budget: the default lives in a new module, One trade-off you should know about: the measurement package isn't allowed to import Test:
Other results:
|
Move the 33554432-pixel default into the dependency-free hflow._frame_budget module, used by VideoLimits, the raw-frame readers, and the FFmpeg decoder cap. luma_frames and rgb_frames take a maximum_frame_pixels limit, and a regression test with an ordinary 160x120 fixture and a lower limit checks that the readers refuse before FFmpeg starts a decode.
Dismissed because a newer commit was pushed; Greptile will re-review the current head.
kstonekuan
left a comment
There was a problem hiding this comment.
The shared limit and refusal tests are in place. The full local suite passed on Python 3.11 and 3.14.
|
Landed on main in c3ee742. |


Summary
Camera-motion streaming and measurement, raw RGB sampling (used by
mediapipe_hands), and the opt-incamera_stabilitycheck now refuse a video whose frames exceed 33,554,432 pixels. That is the defaultVideoLimits.maximum_frame_pixelsthatprobe_videoand video import already enforce. The refusal is aRawFrameErrorthat names the budget, raised before any raw decode starts. In-budget videos decode exactly as before.Why
_probe_frame_shapereturned whatever width and height ffprobe reported._raw_frame_streamthen multiplied them into a per-frameread()size. A container header declaring an enormous frame made the measurement process allocate a buffer of that size for every frame, and ffmpeg allocate the decoded frame too.The fix binds the budget to the values actually used:
_probe_frame_shaperefuses probed dimensions over the budget, before any ffmpeg decode is spawned._raw_frame_streamchecks the frame shape it is given (pixels and at most 3 bytes per pixel) before it starts ffmpeg. The read size can never exceed the budget, whatever produced the shape.rgb_frameschecks the scaled sample shape before the digest trace. An upscalinglong_edge_pixelscan't make ffmpeg build oversized frames.rawvideoreader,framemd5trace, window-measurement decode) passes-max_pixelsto ffmpeg's decoder. The probe and the decode open the file separately, so a file swapped in between, or a stream that changes resolution mid-stream, is refused by the decoder itself. ffmpeg checks the stride-aligned width (up to 64 padding columns) against-max_pixels, so the decoder cap is twice the budget: a cap at exactly the budget rejected legal frames such as 1000×33554.window_measurements(_Y4mLumaFrames) applies the caller'slimits.maximum_frame_pixelsto the Y4M header it reads from ffmpeg. Previously only the earlier, separateprobe_videocall checked it.One source for the default budget
The default lives in a new dependency-free module,
hflow._frame_budget:DEFAULT_MAXIMUM_FRAME_PIXELS: the single source for the default budget.ffmpeg_decoder_maximum_pixels(): derives ffmpeg's-max_pixelsvalue from a budget (twice the budget, capped at the option's maximum).VideoLimits, the raw-frame readers and window measurements all use it.luma_framesandrgb_framestake amaximum_frame_pixelskeyword that defaults to it.hflow._video_measurementsis kept extractable bytests/test_module_boundaries.py. That test now allowshflow._frame_budgetbesidehflow._field_guards, and a new boundary rule keeps_frame_budgetitself free of HFlow imports. I chose this over havinghflow.mediaimport from the measurement package because that package's__init__loads numpy and the camera-motion code. Plainimport hflow.mediastill doesn't load numpy.No transform output changes, so
TRANSFORM_BEHAVIOR_VERSIONis not bumped.Other ffmpeg decodes I reviewed and left unchanged:
Episode.framesandEpisode.frames_at_indices(src/hflow/episode.py). They have ffmpeg write JPEGs and aframecrctrace to disk, and never read a raw frame into the Python process.Validation
tests/test_ffmpeg.py::test_raw_frames_refuse_frames_over_the_pixel_limit_before_decoding.luma_framesandrgb_frames. It uses the ordinary 160×120black_tail_videofixture, a limit of160 * 120 - 1, the real ffprobe, and a stand-in ffmpeg script that leaves a marker file if started.RawFrameErrornaming the limit, and no marker.decode failed,ffmpeg failed to trace decoded frames).long_edge_pixels=20000on a 160×120 source): refused before the digest trace.-max_pixels, and the Python peak is 0.1 MB.MeasurementFailure('MediaToolError'), peak 1.2 MB.uv run pytest -q -n 4ontest_camera_shake_summary,test_checks,test_episode,test_ffmpeg,test_mediapipe_hands,test_module_boundaries,test_motion,test_motion_stream,test_testing,test_video_measurement_settings,test_window_measurements,test_video_importandtest_identity_stability: 520 passed, 6 skipped.uv run pytest -q -n 4: 2603 passed, 8 skipped, 37 failed.tests/test_packaging.pyandtests/test_packaging_manifest_parsing.py. The native-overlay compile fails because this VM lacks Python 3.12 headers, and they fail identically on unchangedmain.test_verification.pytest that also fails intermittently onmain.uv run ruff check --fix,uv run ruff format,uv run ty check: all pass.Checklist
uv run ruff check --fix,uv run ruff format, anduv run ty check.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Note
Medium Risk
Touches all raw-frame and window-decode paths; misconfigured limits could reject valid large frames or leave edge cases, but behavior aligns with existing
probe_videolimits.Reviewed by Cursor Bugbot for commit 1ba7769. Bugbot is set up for automated code reviews on this repo. Configure here.