Skip to content

Count an MP4 edit list's discarded pre-roll out of the frame count (task #54) - #213

Open
StuartCameronCode wants to merge 4 commits into
mainfrom
stuart/54-mp4-with-an-edit-list-the-app-s-frame-co
Open

StuartCameronCode wants to merge 4 commits into
mainfrom
stuart/54-mp4-with-an-edit-list-the-app-s-frame-co

Conversation

@StuartCameronCode

Copy link
Copy Markdown
Owner

The fault

For an MP4/MOV, the source model's frame count used nb_frames. That number includes the pre-roll packets an edit list keeps for decoding but marks as discarded. small_clip.mp4 stores 3154 frames but decodes 3151. So the app's timeline offered 3 frames that don't exist, and an untrimmed encode ended on 3 copies of the last picture.

The fix

  • source_model::frame_count, rule 2a: when the demuxer marks video packets as discarded (D in packet=flags), the count is the last kept picture's grid slot + 1.
    • Discarded packets are found by the head probe, which now reads flags, or by a tail probe over the last 3 s. The tail probe runs alongside the other probes, for any MP4/MOV that stores a count.
    • Counting the packets that weren't discarded would also be wrong. The new fixture edit_list_bframes.mp4 is a stream-copy cut that stores 146 frames and keeps 125, but has gaps in its timestamps that the grid fills, so it decodes to 128.
  • Jobs sent without a count (CLI, tests) now take the model's count over their trim (apply_source_model). -count_frames is kept only for a job with no model, or for a raw MPEG-PS/TS whose timestamps reset and that wasn't continuity-checked (counts_by_decoding). Audio Restoration's drift measurement on a trimmed job also uses the model's count.
  • DVB-T clip to AVI: a job without a count now encodes 128 frames: the 126 decoded pictures plus 2 repeated slots. That matches what an app job has always sent.
  • App: no change. It already sends the model's count, so its timeline loses frames 3151–3153.

Cost

On a 2-hour MP4, the tail probe takes 0.05 s and the whole --probe-source 0.13 s. -count_frames takes 20.7 s on the same file. All four counts agree. Every other committed fixture keeps its count.

Known limit

An edit list whose discarded packets are only in the middle of the file (a multi-entry edit list) still counts nb_frames; nothing cheap detects that case.

Tests

  • Unit: rule 2a and its fallbacks, parsing the tail probe's output (CRLF too), the MP4/MOV format check, discards_from_probe, apply_source_model for every trim shape, and counts_by_decoding.
  • Heavy:
    • integration_source_grid_test.dart adds small_clip.mp4 and edit_list_bframes.mp4, plus a group checking that a job without a count encodes every slot.
    • integration_video_start_offset_test.dart: the DVB-T AVI test now expects 128, and the small_clip AVI test checks the output ends on the source's last pictures.
    • The v210 test now compares against the model's count.
  • Results:
    • cargo test: all pass except the subtitle test, which needs the Whisper add-on (not installed).
    • Flutter push gate: 1639 passed, 7 skipped.
    • Heavy files: one case, late_video.mkv to AVI with Convert, failed once and then passed 3 times out of 3. That is the known intermittent fault, task Version 0.9.11 Not Deinterlacing #58.
  • Not done: the app's UI was not clicked through by hand.

Report: docs/reports/task-54-edit-list-count. Task #59 was filed for an issue with trim end points found along the way.

🤖 Generated with Claude Code

StuartCameronCode and others added 4 commits November 17, 2026 10:31
…del's frame count (task #54)

nb_frames of an MP4/MOV is the sample table's count, and an edit list's
pre-roll (kept for decoding, marked discard, never output) is in it:
small_clip.mp4 stores 3154 and decodes 3151, so the app's timeline ran 3
frames long and an untrimmed encode ended on 3 copies of the last picture.

When the head probe (now with flags) or an MP4/MOV's tail probe sees a
discarded packet, the count is the last kept picture's grid slot + 1.
Not 'packets not discarded': a stream-copy cut leaves timestamp holes the
grid fills (edit_list_bframes.mp4: 146 stored, 125 kept, 128 slots).

A job without a count takes the model's again (apply_source_model);
-count_frames stays only for a job with no model or a raw reset TS/PS
the app did not prepare (counts_by_decoding). Audio Restoration's drift
reads the whole source's length from the model too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e the model's count (task #54)

The source grid suite checks small_clip.mp4 and edit_list_bframes.mp4 at
their last frames, and jobs sent without a count encode every slot. DVB-T
to AVI without a count now stores the grid's 128 slots (126 pictures, two
repeated at field-coding gaps), as an app job always did; small_clip copied
into AVI ends on the source's last pictures; v210 compares with the model's
79, not -count_frames' 75.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ll out (task #54)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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