Skip to content

#407: extend the generated-PTS repair to AVI (measured XviD sample) - #517

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
a1go3:fix/avi-decode-order-pts
Sep 9, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
a1go3:fix/avi-decode-order-pts

Conversation

@a1go3

@a1go3 a1go3 commented Sep 8, 2026

Copy link
Copy Markdown

Fixes #516.

VFWDecodeOrderPTSRepair already describes this failure exactly; its gate was Matroska-only because no AVI had been measured:

Matroska only. AVI carries a FourCC on every track and is DTS-only as well, but the ms_compat/codec_tag equivalence is established in matroskadec and nowhere else, and a VC-1 AVI has not been measured. A gate that is exact on one container beats a guess on two.

Here is the measurement. A 2000s XviD rip — mpeg4 Advanced Simple Profile, tag XVID, 720x304, video_delay = 1, packed B-frames — takes the same transposition, and aetherctl swdecode reports it in the same words the Matroska case did:

before   0.042  0.083  0.167  0.125  0.209  0.292  0.250  0.334  0.417  0.375
         Steps backwards: 12 of 37
         VERDICT: the pictures are paired with the wrong timestamps. The container
                  withheld its PTS and something invented one from decode order (#407).

after    0.042  0.083  0.125  0.167  0.209  0.250  0.292  0.334  0.375  0.417
         Steps backwards: 0 of 37

Why AVI needs no equivalence of its own

avidec has no second mode. The container carries no presentation timestamps at all, every track carries a FourCC, and the demuxer puts the frame index on pkt->dts. So on an AVI input any PTS present was necessarily invented by +genpts — which makes codec_tag != 0 a tautology there rather than a signal, and leaves video_delay > 0 carrying the whole gate.

The Matroska branch keeps its tag condition untouched: there the tag is what separates the VFW-carried tracks that withhold PTS from the natively mapped ones that must keep theirs.

Both existing exclusions hold unchanged in AVI, and are pinned by tests:

  • zero reorder delay — decode order is presentation order, so the invented axis is the right one;
  • H.264 / HEVC / AV1 — they can stay on the native path, where the fMP4 muxer refuses a timestamp-less packet outright.

Ruling out the alternatives on this sample

So the diagnosis does not rest on the verdict alone:

  • Not interlacing. field_order=unknown, and interlaced_frame=0 / repeat_pict=0 on every frame of a mid-file 25-frame window. Toggling deinterlaceMode and deinterlaceFieldRate on device changed nothing, consistent with the deinterlacer correctly never engaging.
  • Not cadence or a damaged rate. best_effort_timestamp_time advances evenly at 0.0417 s with no gaps and no duplicates.
  • Not the decode path. Forcing preferredDecodePath = .software changed nothing.
  • Not the packed bitstream itself. It is the only unusual property of the file, but the pictures decode fine; libavcodec's mpeg4_unpack_bframes suggestion is left alone deliberately — a separate, independent improvement, out of scope for a gate change.

Tests

nonMatroskaContainer pinned the old decision that "avi" must not arm the repair. It becomes ptsCarryingContainer over the containers that genuinely supply timestamps (mov,mp4,…, asf, mpegts), and four AVI cases join the suite: the measured XviD shape, tag-independence, the two shared exclusions, and per-element name matching for isAVI.

Full suite on this branch: 2743 tests in 373 suites, all passing.

Scope

The gate can now arm only on AVI, and only where video_delay > 0 — files that today play with their pictures in the wrong order. Nothing that plays correctly changes behaviour.

Happy to adjust naming or split the test changes if you would rather keep VFW in the type name now that it covers a second carriage.

The gate was Matroska-only for want of a measured AVI. Here is one: a 2000s
XviD rip (mpeg4 ASP, tag XVID, 720x304, video_delay 1, packed B-frames) takes
the same transposition, and `aetherctl swdecode` says so in the same words:

    before   0.042  0.083  0.167  0.125  0.209  0.292  0.250  0.334  0.417
             Steps backwards: 12 of 37
    after    0.042  0.083  0.125  0.167  0.209  0.250  0.292  0.334  0.375
             Steps backwards: 0 of 37

AVI needs no ms_compat equivalence to establish the same fact, because avidec
has no other mode: the container carries no presentation timestamps at all,
every track carries a FourCC, and the demuxer puts the frame index on pkt->dts.
So on an AVI input any PTS present was necessarily invented by +genpts, which
makes codec_tag != 0 a tautology there rather than a signal. video_delay > 0
carries the whole gate; the Matroska branch keeps its tag condition untouched.

Both existing exclusions hold in AVI: zero reorder delay (decode order IS
presentation order) and the natively routable codecs whose packets reach the
fMP4 muxer.

The source is progressive (interlaced_frame 0 and repeat_pict 0 on every frame
of a mid-file window), its timestamps are evenly spaced at 0.0417 s with no
gaps or duplicates, and no deinterlacer is involved: the packed bitstream is
the only unusual property and it is not the cause. The invented axis is.

`nonMatroskaContainer` pinned the old decision that "avi" must not arm the
repair; it becomes `ptsCarryingContainer` over the containers that do supply
timestamps, and four AVI cases join it. Full suite: 2743 tests, 373 suites,
all passing.

Reported as superuser404notfound#516.
@a1go3

a1go3 commented Sep 8, 2026

Copy link
Copy Markdown
Author

Verified on device: iPhone 13 Pro / iOS 26.5, host app built against this branch. The sample that produced #516 now plays with no visible judder — so the fix holds beyond the headless swdecode verdict, on the file the report started from.

@superuser404notfound
superuser404notfound merged commit 1676c40 into superuser404notfound:main Sep 9, 2026
superuser404notfound pushed a commit that referenced this pull request Sep 9, 2026
…tream (#516)

The gate widened in #517 is right and the measurement behind it holds, but the
doc comment closed on "the packed bitstream is not the cause", and that reading
does not survive a second file. A plain coding-order XviD AVI of the same shape
(mpeg4, tag XVID, video_delay=1) already decodes in order today: 0 backwards
steps of 14. The packed FATE sample looped to 390 packets takes 64 of 239.

+genpts writes a coding-order axis in both, but on a well-formed AVI that axis
is the presentation ladder shifted by exactly one frame, uniformly, so no pair
changes places. A packed stream's N-VOP placeholder chunks hold a slot while the
picture they stand for rides inside the previous chunk, so packets and pictures
stop matching one to one and the shift stops being uniform.

Records the regression evidence for arming on every AVI with a reorder delay
(mpeg4 and mpeg2video in AVI, same ladder before and after), and why the repair
is preferred over libavcodec's mpeg4_unpack_bframes suggestion: it covers every
container that withholds PTS, where the filter covers one codec.

Comments only, no behaviour change. Suite 2800/2800.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDKAJrLjLXVsCvJVNdJEPw
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged, thank you. The diagnosis is right, the fix sits in the right layer, and the reasoning about avidec holds at the source: libavformat/avidec.c assigns pkt->dts and never once assigns pkt->pts, so codec_tag != 0 really is a tautology on that input and video_delay > 0 really does carry the whole gate.

Reproduced here against the FATE sample mpeg4/packed_bframes.avi, looped to 390 packets so the count means something:

main     Steps backwards:  64 of 239
#517     Steps backwards:   0 of 239

A full aetherctl play session on the same file plays through clean on the merged branch, disp=+30 lost=0 dpts=33.4/33.4 at 29.97 fps, which is the check that mattered most: a picture that reaches the renderer without a timestamp is unschedulable there, and the decoder-side best_effort_timestamp repair does catch every one of them.

One correction, pulled in afterwards as 8bffbb36

The PR closes on "the packed bitstream is the only unusual property, and it is not the cause". That does not survive a second file. A plain coding-order XviD AVI of the identical shape (mpeg4, tag XVID, video_delay = 1, B-frames throughout) decodes in the right order on main today:

packed B-frames (FATE sample, looped)                64 backwards steps of 239
plain coding-order chunks (ffmpeg -c:v mpeg4 -bf 2)   0 backwards steps of 14

+genpts writes a coding-order axis in both. On a well-formed AVI that axis happens to be the presentation ladder shifted by exactly one frame, uniformly, so nothing changes places:

pkt   0      1      2      3      4      5        (dts = the AVI frame index)
dts   0.000  0.042  0.083  0.125  0.167  0.208
pts   0.042  0.167  0.083  0.125  0.292  0.208    <- each picture own slot, plus one

The packed bitstream is what breaks the correspondence that shift depends on. Its N-VOP placeholder chunks (7 bytes, no picture) hold a slot in the packet ladder while the picture they stand for rides inside the previous chunk, so packets and pictures stop matching one to one and the shift stops being uniform.

That changes nothing about the gate, which is why this went in as a follow-up rather than a review round. It matters for the next reader: someone comparing two XviD AVIs where only one is broken would otherwise have no way to tell them apart, and the widened gate would look like it was arming for no reason on the healthy one. Which it does, harmlessly, and that is now recorded too: with the axis cleared, mpeg4 and mpeg2video in AVI with B-frames reproduce the same ladder frame for frame.

Also noted there is why the repair beats libavcodec own mpeg4_unpack_bframes suggestion, which was the right call to leave out of scope: clearing the axis covers every container that withholds PTS, where the bitstream filter would cover one codec.

On the naming question

VFWDecodeOrderPTSRepair stays. AVI is the Video for Windows container and its stream header is a BITMAPINFOHEADER, so the name reads correctly for both carriages, and the log line saying "FourCC-carried" is the better place for the widening to show.

Suite is 2800 in 381 suites on main, both runners green.

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.

AVI with packed B-frames: presentation timestamps derived from decode order (mpeg4/XviD)

2 participants