Skip to content

cute_h264.h: place MP4 pictures by ranking order counts, not halving them - #603

Merged
RandyGaul merged 2 commits into
RandyGaul:masterfrom
pusewicz:fix-mp4-poc-step
Sep 11, 2026
Merged

RandyGaul merged 2 commits into
RandyGaul:masterfrom
pusewicz:fix-mp4-poc-step

Conversation

@pusewicz

@pusewicz pusewicz commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #602.

The muxer took a picture's order count divided by two as its display slot. The specification numbers fields, so most encoders step the count by two per frame, but VideoToolbox steps it by one and reorders, so every two frames shared a slot and the clip played at twice speed. Order counts only have to sort pictures, so the muxer now ranks them within each keyframe group and gives every picture its own slot. A step of one, a step of two, and gaps all come out right, and the software encoder's output is unchanged.

Verified on an M3 Max with a 30-frame VideoToolbox clip: before, ffprobe showed 15 distinct presentation times with the last at 0.467 s of a 1.0 s file; after, 30 distinct times ending at 0.967 s.

CI notes: the muxer-only test feeds hand-built streams to ch_mp4_wrap, which shared builds do not export, so it runs in static builds only. The public-API test's hardware-size case is skipped on Windows, where Media Foundation writes its own container.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NEv4Z3RZ5msceW87XGkqoB

…them

The muxer took a picture's order count divided by two as its display slot. The specification numbers fields, so most encoders step the count by two per frame, but VideoToolbox steps it by one and reorders, so every two frames shared a slot and clips played at twice speed. Order counts only have to sort pictures, so rank them within each keyframe group and give every picture its own slot; a step of one, a step of two, and gaps all come out right, and the software encoder's output is unchanged.

Fixes RandyGaul#602

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NEv4Z3RZ5msceW87XGkqoB
Copilot AI lite review requested due to automatic review settings September 2, 2026 21:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The newly added qsort comparator in test_video.cpp uses subtraction (x - y), which can overflow and violate comparator ordering requirements.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes MP4 muxer presentation timing when H.264 picture order counts (POC) don’t step by 2 per frame (e.g., VideoToolbox stepping by 1 with reordering), by ranking pictures within each IDR-delimited group instead of deriving display slots from POC/2.

Changes:

  • Update MP4 muxer sample placement to treat POC as an ordering key and convert to contiguous display positions via per-GOP ranking.
  • Add MP4 timeline tests that parse stts/ctts/elst to validate contiguous presentation slots (including step=1, step=2, gaps, and wraparound cases).
  • Update macOS VideoToolbox path commentary to reflect verified behavior and the muxer fix.
File summaries
File Description
test/test_video.cpp Adds timeline-parsing tests to validate MP4 presentation order/lead and introduces a fill helper for variable sizes.
src/cute_video.cpp Updates documentation comment for the VideoToolbox backend and references the muxer’s ranking approach and new test.
libraries/cute/cute_h264.h Implements per-keyframe-group ranking of POC values into contiguous display slots; improves error propagation in ch_mp4_wrap.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/test_video.cpp Outdated
The qsort comparator in the timeline check returned x - y, which can overflow when the values are far apart. The times it sorts here are small, but the muxer's own comparator already uses the sign-of-difference form, so use it here too.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NEv4Z3RZ5msceW87XGkqoB
@RandyGaul
RandyGaul merged commit d8a8385 into RandyGaul:master Sep 11, 2026
14 checks passed
@pusewicz
pusewicz deleted the fix-mp4-poc-step branch September 16, 2026 08:49
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.

Video encoder: mp4 clips from VideoToolbox play at 2x speed (muxer assumes POC steps by 2)

3 participants