Skip to content

Reject video frames whose size or pixel format differs from the encoder - #109

Merged
rubas-agent[bot] merged 1 commit into
mainfrom
encoder-frame-size
Dec 2, 2026
Merged

rubas-agent[bot] merged 1 commit into
mainfrom
encoder-frame-size

Conversation

@rubas-agent

@rubas-agent rubas-agent Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Before

A source build against FFmpeg 9.0.2, a 320x240 clip, and
video_filter: "scale=w='if(lt(n,5),iw,iw/2)':h='if(lt(n,5),ih,ih/2)':eval=frame":

mpeg4: ok
[libx264] Input picture width (320) is greater than stride (192)
libx264: encode_error Send frame to a codec context failed. (-542398533)
libvpx-vp9: ok
ffv1: ok

Problem

A :video_filter can shrink the frames mid-stream. exmpeg opens the encoder once at the size of the first frames, and FFmpeg 9.0.2 and older hand it the smaller frame anyway. mpeg4, libvpx-vp9, and ffv1 return ok, and only x264 notices that the frame's stride is too small for its width.

FFmpeg 9.0.3 rejects such a frame, so the precompiled NIFs of 0.8.0 return :encode_error. A source build links the host FFmpeg, and Arch still ships 9.0.2.

After

mpeg4: unsupported video frames change size or pixel format mid-stream %{"encoder" => "320x240", "frame" => "160x120"}
libx264: unsupported video frames change size or pixel format mid-stream %{"encoder" => "320x240", "frame" => "160x120"}
libvpx-vp9: unsupported video frames change size or pixel format mid-stream %{"encoder" => "320x240", "frame" => "160x120"}
ffv1: unsupported video frames change size or pixel format mid-stream %{"encoder" => "320x240", "frame" => "160x120"}

The README now says to use FFmpeg 9.0.3 or newer for untrusted input in a source build.

Cause, design, tests

transcode.rs:drain_filter_and_encode sent every buffersink frame to the encoder. The encoder takes its size and pixel format from the buffersink once, after the graph config, so nothing compared later frames with it. The gap exists since the first release, which added :video_filter.

The check compares each frame's width, height, and pixel format with the encoder right before send_frame. It returns unsupported, because the output stream has one size, and the same error fits a source whose resolution changes under a filter that keeps the size. A build-time floor of FFmpeg 9.0.3 would also close this, but it would break every source build on Arch and the dev setup in AGENTS.md until Arch ships 9.0.3. The other encoder paths don't need the check: extract_frame allocates its own frame at the encoder's size, and the audio paths keep FFmpeg's own frame_size check.

The new integration test transcode returns an error when a :video_filter changes the frame size mid-stream failed with {:ok, _} against FFmpeg 9.0.2 before the fix and passes after. The full suite passes with 110 tests, and so do clippy, rustfmt, and credo.

I also diffed libavfilter of 9.0.2 and 9.0.3 and found no new filter and no new option that opens a file, so filter_io.rs stays as it is. The diff is 51 lines.

A filter such as scale with eval=frame can shrink frames mid-stream.
FFmpeg 9.0.2 and older pass such a frame to encoders that read it at
the size they opened with. FFmpeg 9.0.3 rejects it, but a source build
links the host FFmpeg, so exmpeg checks each frame itself and returns
:unsupported.
@rubas-agent
rubas-agent Bot merged commit 37c6768 into main Dec 2, 2026
1 check passed
@rubas-agent
rubas-agent Bot deleted the encoder-frame-size branch December 2, 2026 09:41
This was referenced Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant