AV1: treat full-refresh Delayed Random Access Points as keyframes (matroskadec, av1_parser) #25607
No reviewers
Labels
No labels
avcodec
avdevice
avfilter
avformat
avutil
backport/release/4.3
backport/release/4.4
backport/release/5.0
backport/release/5.1
backport/release/6.0
backport/release/6.1
backport/release/7.0
backport/release/7.1
backport/release/8.0
backport/release/8.1
backport/release/9.0
backport/release/9.1
CLI
doc
Forgejo Fairy
swresample
swscale
vulkan
API
API major
bug
enhancement
fix/bug
fix/regression
GSoC
GSoC-Qualification
help wanted
important
LGTM
meta
needs backport
needs docs
needs fate test
needs info
needs sample
needs testing
new
question
regression
repro
flaky
repro
no
repro
no(env)
repro
yes
resolution
duplicate
resolution
external
resolution
fixed
resolution
invalid
resolution
wontfix
security
small
No milestone
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
FFmpeg/FFmpeg!25607
Loading…
Reference in a new issue
No description provided.
Delete branch "superuser404/FFmpeg:av1-delayed-rap"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
av1_parsersetskey_frameonly for a shown Key Frame. Matroska video other than HEVC is opened withAVSTREAM_PARSE_HEADERS, so the parser's verdict replaces the block's keyframe flag, and the flag is lost even when the muxer set it.matroskadecnever adds an index entry, soavformat_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 aboutshow_frame.The patches
avformat/av1:ff_av1_is_random_access_point()reads the first frame header up torefresh_frame_flags. It accepts a shown Key Frame, or a hidden one withrefresh_frame_flags == 0xFF, and only when the TU carries a Sequence Header.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.avcodec/av1_parser: report the same full-refresh hidden Key Frame askey_frame = 1.A hidden Key Frame with a partial refresh is not affected. libaom's
--enable-fwd-kfwrites those (refresh_frame_flags = 0x02in 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
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 eightrefresh_frame_flagsbits, 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)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
av1fsample 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.@superuser404 wrote in #25607 (comment):
That sounds like an invalid bitstream...
...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.
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.
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.
Pull request closed