AV1: treat full-refresh Delayed Random Access Points as keyframes (matroskadec, av1_parser) #25607

Closed
superuser404 wants to merge 3 commits from superuser404/FFmpeg:av1-delayed-rap into master
Contributor

A Temporal Unit that carries a Sequence Header and opens with a hidden Key Frame (show_frame = 0) refreshing all eight reference slots can be decoded from directly: nothing before it is reachable afterwards. FFmpeg currently treats it as an inter frame in two places, and an encode built on such forward key frames becomes unseekable in Matroska.

What goes wrong today

  1. av1_parser sets key_frame only for a shown Key Frame. Matroska video other than HEVC is opened with AVSTREAM_PARSE_HEADERS, so the parser's verdict replaces the block's keyframe flag, and the flag is lost even when the muxer set it.
  2. Muxers leave the SimpleBlock keyframe flag off for a hidden Key Frame. mkvmerge v103 does (checked by remuxing), and lavf's own muxer takes the flag from (1). The file then has no keyframe at all, no Cues, and matroskadec never adds an index entry, so avformat_seek_file() fails, including the seek back to the start after probing.

The AV1 Matroska mapping already allows such a block to be a keyframe: it requires a Sequence Header and a first frame of type KEY_FRAME, and says nothing about show_frame.

The patches

  1. avformat/av1: ff_av1_is_random_access_point() reads the first frame header up to refresh_frame_flags. It accepts a shown Key Frame, or a hidden one with refresh_frame_flags == 0xFF, and only when the TU carries a Sequence Header.
  2. avformat/matroskadec: an unlaced, unencoded AV1 SimpleBlock without the flag gets it when (1) says so. Blocks the muxer flagged, and BlockGroups, are left as they are.
  3. avcodec/av1_parser: report the same full-refresh hidden Key Frame as key_frame = 1.

A hidden Key Frame with a partial refresh is not affected. libaom's --enable-fwd-kf writes those (refresh_frame_flags = 0x02 in the stream below), and the frames after it still reference the previous shown Key Frame. Treating that as a random access point would move seeks onto a place where dav1d reports errors until the Key Frame Dependent Recovery Point, so the check refuses it.

Where this came from

A 1080p BD encode in Matroska, muxed by mkvmerge v95.0 (encoder not identifiable from the file). Its first Temporal Unit is a Sequence Header, a hidden Key Frame with refresh_frame_flags = 0xFF, and a shown inter frame predicted from it. No packet is flagged as a keyframe, and a player that waits for one never starts (report: https://github.com/superuser404notfound/AetherEngine/issues/931). I can't attach that file. The numbers below use a fixture made from its opening second by stream copy and repeated 30 times (720 packets, Opus audio added), muxed once with lavf and once with mkvmerge v103. Those fixtures stay local, they hold the original content.

Reproducing with a stream anyone can make

ffmpeg -f lavfi -i testsrc2=size=320x180:rate=24:duration=10 -pix_fmt yuv420p src.y4m
aomenc --cpu-used=6 --end-usage=q --cq-level=50 --kf-max-dist=64 --enable-fwd-kf=1 \
       --fwd-kf-dist=32 --lag-in-frames=48 --auto-alt-ref=1 --ivf -o fwd.ivf src.y4m
mkvmerge -o fwd.mkv fwd.ivf

That stream has four shown Key Frames and four partial-refresh hidden ones. Probe and seeks are identical with and without this series (4 keyframes, the same landings), which is the point: those are not entry points. To get a full-refresh one, take the second TU of fwd.ivf (a Sequence Header plus a hidden Key Frame) and set the eight refresh_frame_flags bits, which sit at bit 41 of its Frame OBU for this stream. Muxed with mkvmerge into a one-block file, stock FFmpeg reports no keyframe; with this series it reports one. The untouched TU stays unflagged with or without it.

Measured (avformat_seek_file() and the first video packet after it)

file master patch 2 only full series
lavf mux, 30 hidden full-refresh KFs: keyframe packets 0 0 30
lavf mux: seek 0 / 17.5 s ret -1, EOF / ret -1, EOF 0.000 / 17.000, key=0 0.000 / 17.000, key=1
mkvmerge v103 mux: seek 0 / 17.5 s 0.000 / 17.000, key=0 0.000 / 17.000, key=1
libaom fwd-kf (partial refresh): keyframe packets 4 4
libaom fwd-kf: seek 17.5 s 8.000, key=1 8.000, key=1

FATE

625 tests green (every test whose name matches av1, matroska, mkv, webm, dav1d, cbs, mov, mp4, isom or seek, plus their dependencies). None of the existing samples has a hidden Key Frame, so nothing changed. I can add a FATE test for (2) and (3) with the one-block fixture above (about 9 KB, our own encode) if it goes into the suite. I'd mail it to samples-request first.

One consequence to decide on

With (3), remuxing such a file to MP4 lists these TUs as sync samples. AV1-ISOBMFF reserves sync samples for TUs whose first frame is a shown Key Frame and wants Delayed Random Access Points signaled with the av1f sample group. With a full refresh, decoding from one is correct, but if movenc should stay strict, it can clear the flag for an AV1 TU whose first Key Frame is hidden. I can add that as a fourth commit if that's preferred.

A Temporal Unit that carries a Sequence Header and opens with a hidden Key Frame (`show_frame = 0`) refreshing all eight reference slots can be decoded from directly: nothing before it is reachable afterwards. FFmpeg currently treats it as an inter frame in two places, and an encode built on such forward key frames becomes unseekable in Matroska. **What goes wrong today** 1. `av1_parser` sets `key_frame` only for a shown Key Frame. Matroska video other than HEVC is opened with `AVSTREAM_PARSE_HEADERS`, so the parser's verdict replaces the block's keyframe flag, and the flag is lost even when the muxer set it. 2. Muxers leave the SimpleBlock keyframe flag off for a hidden Key Frame. mkvmerge v103 does (checked by remuxing), and lavf's own muxer takes the flag from (1). The file then has no keyframe at all, no Cues, and `matroskadec` never adds an index entry, so `avformat_seek_file()` fails, including the seek back to the start after probing. The AV1 Matroska mapping already allows such a block to be a keyframe: it requires a Sequence Header and a first frame of type `KEY_FRAME`, and says nothing about `show_frame`. **The patches** 1. `avformat/av1`: `ff_av1_is_random_access_point()` reads the first frame header up to `refresh_frame_flags`. It accepts a shown Key Frame, or a hidden one with `refresh_frame_flags == 0xFF`, and only when the TU carries a Sequence Header. 2. `avformat/matroskadec`: an unlaced, unencoded AV1 SimpleBlock without the flag gets it when (1) says so. Blocks the muxer flagged, and BlockGroups, are left as they are. 3. `avcodec/av1_parser`: report the same full-refresh hidden Key Frame as `key_frame = 1`. A hidden Key Frame with a partial refresh is not affected. libaom's `--enable-fwd-kf` writes those (`refresh_frame_flags = 0x02` in the stream below), and the frames after it still reference the previous shown Key Frame. Treating that as a random access point would move seeks onto a place where dav1d reports errors until the Key Frame Dependent Recovery Point, so the check refuses it. **Where this came from** A 1080p BD encode in Matroska, muxed by mkvmerge v95.0 (encoder not identifiable from the file). Its first Temporal Unit is a Sequence Header, a hidden Key Frame with `refresh_frame_flags = 0xFF`, and a shown inter frame predicted from it. No packet is flagged as a keyframe, and a player that waits for one never starts (report: https://github.com/superuser404notfound/AetherEngine/issues/931). I can't attach that file. The numbers below use a fixture made from its opening second by stream copy and repeated 30 times (720 packets, Opus audio added), muxed once with lavf and once with mkvmerge v103. Those fixtures stay local, they hold the original content. **Reproducing with a stream anyone can make** ```sh ffmpeg -f lavfi -i testsrc2=size=320x180:rate=24:duration=10 -pix_fmt yuv420p src.y4m aomenc --cpu-used=6 --end-usage=q --cq-level=50 --kf-max-dist=64 --enable-fwd-kf=1 \ --fwd-kf-dist=32 --lag-in-frames=48 --auto-alt-ref=1 --ivf -o fwd.ivf src.y4m mkvmerge -o fwd.mkv fwd.ivf ``` That stream has four shown Key Frames and four partial-refresh hidden ones. Probe and seeks are identical with and without this series (4 keyframes, the same landings), which is the point: those are not entry points. To get a full-refresh one, take the second TU of `fwd.ivf` (a Sequence Header plus a hidden Key Frame) and set the eight `refresh_frame_flags` bits, which sit at bit 41 of its Frame OBU for this stream. Muxed with mkvmerge into a one-block file, stock FFmpeg reports no keyframe; with this series it reports one. The untouched TU stays unflagged with or without it. **Measured** (`avformat_seek_file()` and the first video packet after it) | file | master | patch 2 only | full series | |---|---|---|---| | lavf mux, 30 hidden full-refresh KFs: keyframe packets | 0 | 0 | 30 | | lavf mux: seek 0 / 17.5 s | ret -1, EOF / ret -1, EOF | 0.000 / 17.000, key=0 | 0.000 / 17.000, key=1 | | mkvmerge v103 mux: seek 0 / 17.5 s | 0.000 / 17.000, key=0 | | 0.000 / 17.000, key=1 | | libaom fwd-kf (partial refresh): keyframe packets | 4 | | 4 | | libaom fwd-kf: seek 17.5 s | 8.000, key=1 | | 8.000, key=1 | **FATE** 625 tests green (every test whose name matches av1, matroska, mkv, webm, dav1d, cbs, mov, mp4, isom or seek, plus their dependencies). None of the existing samples has a hidden Key Frame, so nothing changed. I can add a FATE test for (2) and (3) with the one-block fixture above (about 9 KB, our own encode) if it goes into the suite. I'd mail it to samples-request first. **One consequence to decide on** With (3), remuxing such a file to MP4 lists these TUs as sync samples. AV1-ISOBMFF reserves sync samples for TUs whose first frame is a shown Key Frame and wants Delayed Random Access Points signaled with the `av1f` sample group. With a full refresh, decoding from one is correct, but if movenc should stay strict, it can clear the flag for an AV1 TU whose first Key Frame is hidden. I can add that as a fourth commit if that's preferred.
ff_av1_is_random_access_point() tells whether a Temporal Unit carries a
Sequence Header OBU and opens with a Key Frame that refreshes every
reference slot. That covers a shown Key Frame and also a hidden one
(show_frame equal to 0), the Delayed Random Access Point that forward key
frame encodes are built on, but only when nothing decoded before it can be
referenced after it. A hidden Key Frame that refreshes fewer slots, as
libaom's forward key frames do, is not a place to start decoding and is
rejected.

The frame header is read up to refresh_frame_flags, so the sequence header
parser now also keeps the fields uncompressed_header() needs to get there.

Signed-off-by: Vincent Herbst <[email protected]>
The AV1 Matroska mapping lets a SimpleBlock be a keyframe when it carries
a Sequence Header OBU and its first frame is a Key Frame; it does not ask
for that frame to be shown. Muxers leave the flag off for a hidden one
(mkvmerge v103 does, and so does lavf, which takes the flag from the
parser), so an encode built on forward key frames ends up with no keyframe
in the file at all, no Cues and nothing to build an index from: every
seek, including the one back to the start after probing, fails.

Infer the flag for an unlaced, unencoded AV1 SimpleBlock whose Temporal
Unit is a random access point by ff_av1_is_random_access_point(), which
only accepts a hidden Key Frame that refreshes every reference slot.
Blocks the muxer did flag, and BlockGroups, are left as they are.

Signed-off-by: Vincent Herbst <[email protected]>
avcodec/av1_parser: report full-refresh Delayed Random Access Points as key frames
All checks were successful
Backport / PR (pull_request_target) Has been skipped
Lint / Pre-Commit (pull_request) Successful in 33s
Test / Fate (linux-loongarch64, static, 64 bit) (pull_request) Successful in 6m33s
Test / Fate (Full, Linux, aarch64) (pull_request) Successful in 10m24s
Test / Fate (linux-aarch64, static, 64 bit) (pull_request) Successful in 12m22s
Test / Fate (linux-amd64, static, 32 bit) (pull_request) Successful in 16m37s
Test / Fate (linux-amd64, shared, 64 bit) (pull_request) Successful in 17m7s
Test / Fate (Qemu, Linux, mips64) (pull_request) Successful in 21m2s
Test / Fate (Qemu, Linux, ppc64) (pull_request) Successful in 24m13s
Test / Fate (Full, Wine, win64) (pull_request) Successful in 23m37s
Test / Fate (Qemu, Linux, riscv64) (pull_request) Successful in 28m3s
Autolabel / Labeler (pull_request_target) Successful in 9s
7ced654262
The parser sets key_frame only for a shown Key Frame. A Temporal Unit that
carries a Sequence Header and opens with a hidden Key Frame refreshing all
eight reference slots can be decoded from just as well, since nothing
before it is reachable afterwards, but is reported as an inter frame.

For streams that need parsing this verdict replaces the container's own
flag, so in Matroska such a Temporal Unit loses its keyframe flag even when
the muxer set it, and an encode built on forward key frames reaches the
caller without a single keyframe. Report it as a key frame. A hidden Key
Frame with a partial refresh, as libaom writes for forward key frames,
still is not one.

Signed-off-by: Vincent Herbst <[email protected]>
Owner

@superuser404 wrote in #25607 (comment):

A 1080p BD encode in Matroska, muxed by mkvmerge v95.0 (encoder not identifiable from the file). Its first Temporal Unit is a Sequence Header, a hidden Key Frame with refresh_frame_flags = 0xFF, and a shown inter frame predicted from it. No packet is flagged as a keyframe, and a player that waits for one never starts

That sounds like an invalid bitstream...

That stream has four shown Key Frames and four partial-refresh hidden ones. Probe and seeks are identical with and without this series (4 keyframes, the same landings), which is the point: those are not entry points. To get a full-refresh one, take the second TU of fwd.ivf (a Sequence Header plus a hidden Key Frame) and set the eight refresh_frame_flags bits

...and the fact you need to do this to get a sample to reproduce the "issue" more or less confirms it. If it was a compliant bitstream, aomenc should be able to produce it.

With (3), remuxing such a file to MP4 lists these TUs as sync samples. AV1-ISOBMFF reserves sync samples for TUs whose first frame is a shown Key Frame and wants Delayed Random Access Points signaled with the av1f sample group.

Correct, such packets are not meant to be flagged as a key frame because muxers will assume it's a RAP, and having to add considerations to them so they figure out they are not real key frames is not acceptable. If a packet is marked as key, a muxer should not have to question it.

So no, this change is not ok, but it could be partly reused to get the mov muxer to write av1f sample groups.

@superuser404 wrote in https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25607#issue-6240: > A 1080p BD encode in Matroska, muxed by mkvmerge v95.0 (encoder not identifiable from the file). Its first Temporal Unit is a Sequence Header, a hidden Key Frame with `refresh_frame_flags = 0xFF`, and a shown inter frame predicted from it. No packet is flagged as a keyframe, and a player that waits for one never starts That sounds like an invalid bitstream... > That stream has four shown Key Frames and four partial-refresh hidden ones. Probe and seeks are identical with and without this series (4 keyframes, the same landings), which is the point: those are not entry points. To get a full-refresh one, take the second TU of fwd.ivf (a Sequence Header plus a hidden Key Frame) and set the eight refresh_frame_flags bits ...and the fact you need to do this to get a sample to reproduce the "issue" more or less confirms it. If it was a compliant bitstream, aomenc should be able to produce it. > With (3), remuxing such a file to MP4 lists these TUs as sync samples. AV1-ISOBMFF reserves sync samples for TUs whose first frame is a shown Key Frame and wants Delayed Random Access Points signaled with the av1f sample group. Correct, such packets are not meant to be flagged as a key frame because muxers will assume it's a RAP, and having to add considerations to them so they figure out they are not real key frames is not acceptable. If a packet is marked as key, a muxer should not have to question it. So no, this change is not ok, but it could be partly reused to get the mov muxer to write av1f sample groups.
James Almer closed this pull request 2026-10-30 13:10:34 +00:00
Author
Contributor

Thanks for the review. Understood: a key flag has to mean a real random access point for every consumer, and pushing that check into the muxers is the wrong place, so closing this is fine.

The av1f sample group for movenc is a fair idea, but I have no real-world use for it right now, so I won't send it without a sample that needs it. If such a file turns up, I'll open a separate PR for that.

Thanks for the review. Understood: a key flag has to mean a real random access point for every consumer, and pushing that check into the muxers is the wrong place, so closing this is fine. The av1f sample group for movenc is a fair idea, but I have no real-world use for it right now, so I won't send it without a sample that needs it. If such a file turns up, I'll open a separate PR for that.
All checks were successful
Backport / PR (pull_request_target) Has been skipped
Lint / Pre-Commit (pull_request) Successful in 33s
Required
Details
Test / Fate (linux-loongarch64, static, 64 bit) (pull_request) Successful in 6m33s
Required
Details
Test / Fate (Full, Linux, aarch64) (pull_request) Successful in 10m24s
Required
Details
Test / Fate (linux-aarch64, static, 64 bit) (pull_request) Successful in 12m22s
Required
Details
Test / Fate (linux-amd64, static, 32 bit) (pull_request) Successful in 16m37s
Required
Details
Test / Fate (linux-amd64, shared, 64 bit) (pull_request) Successful in 17m7s
Required
Details
Test / Fate (Qemu, Linux, mips64) (pull_request) Successful in 21m2s
Required
Details
Test / Fate (Qemu, Linux, ppc64) (pull_request) Successful in 24m13s
Required
Details
Test / Fate (Full, Wine, win64) (pull_request) Successful in 23m37s
Required
Details
Test / Fate (Qemu, Linux, riscv64) (pull_request) Successful in 28m3s
Required
Details
Autolabel / Labeler (pull_request_target) Successful in 9s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
FFmpeg/FFmpeg!25607
No description provided.