forge
Explore
Log in

Don't you dare go Hollow

10-bit AV1 opens instead of being refused

Browse files at this commit

25aef8c13e69879e65545b621f72fa2a7c3f6359Ahmet Kaan GümüşParent: ae661e4
  • feat
    • 10-bit AV1 opens instead of being refused
Show full message
✨ feat(av1): 10-bit AV1 opens instead of being refused

The `av1C` record's `high_bitdepth` was an outright refusal ("10-bit AV1 is
not supported yet"), on a comment that had gone stale: the vendored decoder
takes its RT format from the stream and maps Depth10 to YUV420_10, and the
plugin's read-back pool is P010 whenever the demuxer says >8 -- which is how
HEVC Main 10 has been playing all along.

So `parse_av1c` reads the depth instead of refusing it, in both containers
(the mp4 `av1C` box and the Matroska `CodecPrivate`), and 12-bit stays
refused by name in the words HEVC's own depth check uses. Nothing else needs
the plugin any differently: AV1 has no software decoder at any depth, so a
machine without `libengine_hw.so` still refuses it as `needs_plugin` always
has.

Fixture: assets/test_av1_10.mkv (libaom, yuv420p10le), the AV1 seat of the
pair test_hevc.mkv/test_hevc10.mkv already make for HEVC.
crates/engine-hw/src/lib.rsmodified+4 -3
@@ -59,7 +59,8 @@ type Handle = <Dec<H264> as StatelessVideoDecoder>::Handle;
59 /// still loads and still decodes H.264 (it simply refuses the newer codecs'59 /// still loads and still decodes H.264 (it simply refuses the newer codecs'
60 /// files at `Demuxer`).60 /// files at `Demuxer`).
61 ///61 ///
62-/// 4:2:0 only, 8- or 10-bit: HEVC Main and Main 10, VP9 profile 0, AV1 Main.62+/// 4:2:0 only, 8- or 10-bit: HEVC Main and Main 10, VP9 profile 0, AV1 Main at
63+/// either depth.
63 /// A 10-bit stream decodes into a P010 pool and is read back down to the 8-bit64 /// A 10-bit stream decodes into a P010 pool and is read back down to the 8-bit
64 /// I420 every frame here is ([`Session::emit`]).65 /// I420 every frame here is ([`Session::emit`]).
65 ///66 ///
@@ -67,8 +68,8 @@ type Handle = <Dec<H264> as StatelessVideoDecoder>::Handle;
67 /// interface all the way to the renderer. That costs the low two bits of a Main68 /// interface all the way to the renderer. That costs the low two bits of a Main
68 /// 10 source; the upgrade path is a 16-bit `VhFrame`, which is a change to every69 /// 10 source; the upgrade path is a 16-bit `VhFrame`, which is a change to every
69 /// stage between here and the texture upload, not to this file.70 /// stage between here and the texture upload, not to this file.
70-/// VP9 profile 2 and 10-bit AV1 are still refused by `Demuxer`: neither codec71+/// VP9 profile 2 is still refused by `Demuxer`: a VP9 stream does not state its
71-/// states its depth where this reads it.72+/// depth where that reads it (an `av1C` and an `hvcC` both do).
72 enum Decoder {73 enum Decoder {
73 H264(Dec<H264>),74 H264(Dec<H264>),
74 Hevc(Dec<H265>),75 Hevc(Dec<H265>),
crates/engine/src/demux.rsmodified+46 -24
@@ -103,9 +103,10 @@ impl Demuxer {
103 }103 }
104 104
105 /// Bits per luma sample the stream is coded at: 8 for everything but an105 /// Bits per luma sample the stream is coded at: 8 for everything but an
106- /// HEVC Main 10 track, which decodes into a P010 surface rather than an106+ /// HEVC Main 10 or a 10-bit AV1 track, which decode into a P010 surface
107- /// NV12 one. Not part of [`VideoMeta`] because nothing above the plugin has107+ /// rather than an NV12 one. Not part of [`VideoMeta`] because nothing above
108- /// a use for it -- what comes out of the read-back is 8-bit either way.108+ /// the plugin has a use for it -- what comes out of the read-back is 8-bit
109+ /// either way.
109 pub fn bit_depth(&self) -> u8 {110 pub fn bit_depth(&self) -> u8 {
110 match self {111 match self {
111 Self::Mp4(d) => d.bit_depth,112 Self::Mp4(d) => d.bit_depth,
@@ -234,7 +235,9 @@ impl Mp4Demuxer {
234 let (_, entry) = sample_entry(path, track_id)?;235 let (_, entry) = sample_entry(path, track_id)?;
235 let av1c = child(entry.get(78..).unwrap_or_default(), b"av1C")236 let av1c = child(entry.get(78..).unwrap_or_default(), b"av1C")
236 .ok_or("no av1C box in the AV1 sample entry")?;237 .ok_or("no av1C box in the AV1 sample entry")?;
237- parameter_sets = parse_av1c(av1c)?;238+ let (sets, depth) = parse_av1c(av1c)?;
239+ parameter_sets = sets;
240+ bit_depth = depth;
238 }241 }
239 // No parameter sets: a VP9 sample is self-contained.242 // No parameter sets: a VP9 sample is self-contained.
240 Codec::Vp9 => {}243 Codec::Vp9 => {}
@@ -639,7 +642,10 @@ fn mkv_track_entry(
639 // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4642 // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4
640 // sample entry carries, so it parses with the very same reader.643 // sample entry carries, so it parses with the very same reader.
641 let (codec, nal_length, config, bit_depth) = match (kind, codec.as_str()) {644 let (codec, nal_length, config, bit_depth) = match (kind, codec.as_str()) {
642- (1, "V_AV1") => (Codec::Av1, 4, parse_av1c(&config)?, 8),645+ (1, "V_AV1") => {
646+ let (sets, bit_depth) = parse_av1c(&config)?;
647+ (Codec::Av1, 4, sets, bit_depth)
648+ }
643 (1, "V_MPEGH/ISO/HEVC") => {649 (1, "V_MPEGH/ISO/HEVC") => {
644 let (nal_length, sets, bit_depth) = parse_hvcc(&config)?;650 let (nal_length, sets, bit_depth) = parse_hvcc(&config)?;
645 (Codec::Hevc, nal_length, sets, bit_depth)651 (Codec::Hevc, nal_length, sets, bit_depth)
@@ -714,25 +720,33 @@ fn parse_avcc(rec: &[u8]) -> crate::Result<(usize, Vec<u8>)> {
714 /// §2.3.3): four fixed bytes, then the sequence header OBU in the same720 /// §2.3.3): four fixed bytes, then the sequence header OBU in the same
715 /// low-overhead format the blocks are in, so it needs no reframing.721 /// low-overhead format the blocks are in, so it needs no reframing.
716 ///722 ///
723+/// Also the bits per luma sample, out of `high_bitdepth`/`twelve_bit` in byte 2:
724+/// 10-bit decodes through the P010 pool HEVC Main 10 already goes through
725+/// ([`crate::hw`]), 12-bit is refused by name because that pool is the only
726+/// deeper one there is.
727+///
717 /// An empty record is not an error -- an encoder that repeats its sequence728 /// An empty record is not an error -- an encoder that repeats its sequence
718-/// header in-band needs none -- but a 10- or 12-bit one is refused by name, for729+/// header in-band needs none -- and is taken as 8-bit, which is what the depth
719-/// the same reason HEVC Main 10 is: the plugin's NV12 pool and its `vaGetImage`730+/// defaults to everywhere else here.
720-/// read-back are 8-bit, and the upgrade path is a P010 pool.731+fn parse_av1c(rec: &[u8]) -> crate::Result<(Vec<u8>, u8)> {
721-fn parse_av1c(rec: &[u8]) -> crate::Result<Vec<u8>> {
722 if rec.is_empty() {732 if rec.is_empty() {
723- return Ok(Vec::new());733+ return Ok((Vec::new(), 8));
724 }734 }
725 let &flags = rec735 let &flags = rec
726 .get(2)736 .get(2)
727 .ok_or("av1C record shorter than its fixed header")?;737 .ok_or("av1C record shorter than its fixed header")?;
728- if flags & 0x40 != 0 {738+ // `twelve_bit` only says anything when `high_bitdepth` is set (AV1 §5.5.1),
729- return Err(format!(739+ // so it is read where the record means it and nowhere else.
730- "{}-bit AV1 is not supported yet — this file is not 8-bit",740+ let bit_depth = match (flags & 0x40 != 0, flags & 0x20 != 0) {
731- if flags & 0x20 != 0 { 12 } else { 10 }741+ (true, true) => {
732- )742+ return Err(
733- .into());743+ "12-bit AV1 is not supported — 8- and 10-bit are what the decoder carries".into(),
734- }744+ );
735- Ok(rec.get(4..).unwrap_or_default().to_vec())745+ }
746+ (true, false) => 10,
747+ _ => 8,
748+ };
749+ Ok((rec.get(4..).unwrap_or_default().to_vec(), bit_depth))
736 }750 }
737 751
738 /// Every block of track `number`, in storage order, with the presentation span752 /// Every block of track `number`, in storage order, with the presentation span
@@ -1307,8 +1321,9 @@ mod tests {
1307 }1321 }
1308 1322
1309 /// The `av1C` record: four fixed bytes and then the sequence header OBU,1323 /// The `av1C` record: four fixed bytes and then the sequence header OBU,
1310- /// which is handed to the decoder as it stands. 10-bit is refused by name1324+ /// which is handed to the decoder as it stands, and the depth beside it --
1311- /// exactly as HEVC Main 10 is, and for the same 8-bit read-back reason.1325+ /// 10-bit reads as 10 (the P010 pool, exactly as HEVC Main 10), 12-bit is
1326+ /// refused by name.
1312 #[test]1327 #[test]
1313 fn reads_the_sequence_header_out_of_an_av1c_record() {1328 fn reads_the_sequence_header_out_of_an_av1c_record() {
1314 // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a1329 // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a
@@ -1317,14 +1332,16 @@ mod tests {
1317 rec.extend_from_slice(&[1332 rec.extend_from_slice(&[
1318 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00,1333 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00,
1319 ]);1334 ]);
1320- let config = parse_av1c(&rec).unwrap();1335+ let (config, depth) = parse_av1c(&rec).unwrap();
1321 assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs");1336 assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs");
1322 assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header");1337 assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header");
1338+ assert_eq!(depth, 8);
1323 1339
1324 let mut ten = rec.clone();1340 let mut ten = rec.clone();
1325 ten[2] |= 0x40; // high_bitdepth1341 ten[2] |= 0x40; // high_bitdepth
1326- let refused = parse_av1c(&ten).unwrap_err().to_string();1342+ let (ten_config, ten_depth) = parse_av1c(&ten).unwrap();
1327- assert!(refused.contains("10-bit"), "{refused}");1343+ assert_eq!(ten_depth, 10, "what picks the P010 surface pool");
1344+ assert_eq!(ten_config, config, "the OBUs are read the same either way");
1328 let mut twelve = ten.clone();1345 let mut twelve = ten.clone();
1329 twelve[2] |= 0x20; // twelve_bit1346 twelve[2] |= 0x20; // twelve_bit
1330 assert!(1347 assert!(
@@ -1333,10 +1350,15 @@ mod tests {
1333 .to_string()1350 .to_string()
1334 .contains("12-bit")1351 .contains("12-bit")
1335 );1352 );
1353+ // `twelve_bit` without `high_bitdepth` says nothing, and an 8-bit
1354+ // record reads as one whatever that bit holds.
1355+ let mut stray = rec.clone();
1356+ stray[2] |= 0x20;
1357+ assert_eq!(parse_av1c(&stray).unwrap().1, 8);
1336 1358
1337 // An encoder that repeats its sequence header in-band writes no record1359 // An encoder that repeats its sequence header in-band writes no record
1338 // at all, which is not an error; a truncated one is.1360 // at all, which is not an error; a truncated one is.
1339- assert!(parse_av1c(&[]).unwrap().is_empty());1361+ assert!(parse_av1c(&[]).unwrap().0.is_empty());
1340 assert!(parse_av1c(&[0x81, 0x05]).is_err());1362 assert!(parse_av1c(&[0x81, 0x05]).is_err());
1341 }1363 }
1342 1364
crates/engine/tests/av1.rsmodified+48 -0
@@ -57,6 +57,23 @@ fn the_demuxer_reports_an_av1_track_in_matroska() {
57 assert_eq!(vp9.codec, Codec::Vp9);57 assert_eq!(vp9.codec, Codec::Vp9);
58 }58 }
59 59
60+/// The depth, which is what picks the surface pool the plugin decodes into: an
61+/// `av1C` with `high_bitdepth` set reads as 10 and takes the P010 pool HEVC Main
62+/// 10 already goes through, and the 8-bit fixture beside it still reads as 8.
63+/// It used to be an outright refusal at open ("10-bit AV1 is not supported").
64+#[test]
65+fn a_ten_bit_av1_track_is_read_as_ten_bit() {
66+ let (meta, ten) = Demuxer::open(&asset("test_av1_10.mkv")).expect("open test_av1_10.mkv");
67+ assert_eq!(meta.codec, Codec::Av1);
68+ assert_eq!((meta.width, meta.height), (1280, 720));
69+ assert_eq!(meta.frame_count, FRAMES);
70+ assert_eq!(ten.bit_depth(), 10, "what picks the P010 surface pool");
71+
72+ // The invariant: the 8-bit file is untouched by any of it.
73+ let (_, eight) = Demuxer::open(&asset("test_av1.mkv")).expect("open test_av1.mkv");
74+ assert_eq!(eight.bit_depth(), 8);
75+}
76+
60 /// The delivered audio scope, as an assert: a Matroska file's AAC track is read77 /// The delivered audio scope, as an assert: a Matroska file's AAC track is read
61 /// like any other file's (`tests/hevc_mkv.rs` is where that lives), and its78 /// like any other file's (`tests/hevc_mkv.rs` is where that lives), and its
62 /// *picture* being AV1 changes nothing about it.79 /// *picture* being AV1 changes nothing about it.
@@ -181,6 +198,37 @@ fn the_plugin_decodes_every_av1_frame() {
181 );198 );
182 }199 }
183 200
201+/// ...and the 10-bit file through the very call the window's open door makes: a
202+/// user opens it and the timeline shows a picture, read back off the P010 pool
203+/// to the same 8-bit BGRA every other frame arrives in. The whole point of the
204+/// slice, and the half a container test cannot say anything about.
205+#[test]
206+#[ignore = "needs libengine_hw.so and a VA-API driver with 10-bit AV1 decode"]
207+fn opening_a_ten_bit_av1_file_shows_frames() {
208+ let mut session =
209+ PlaybackSession::open(asset("test_av1_10.mkv")).expect("open the 10-bit file");
210+ assert_eq!(session.meta().codec, Codec::Av1);
211+ assert_eq!(session.meta().frame_count, FRAMES);
212+
213+ // The decoder is a thread behind the door, so a frame is waited for exactly
214+ // as the window's own pump waits for one.
215+ let deadline = Instant::now() + Duration::from_secs(60);
216+ let frame = loop {
217+ if let Some(frame) = session.try_frame() {
218+ break frame;
219+ }
220+ assert!(Instant::now() < deadline, "no frame in 60 s");
221+ std::thread::sleep(Duration::from_millis(10));
222+ };
223+ assert_eq!((frame.width, frame.height), (1280, 720));
224+ // A picture, not a flat surface: a P010 surface read back through the NV12
225+ // path would come out as noise or as nothing at all.
226+ assert!(
227+ frame.bgra.chunks_exact(4).any(|px| px != &frame.bgra[..4]),
228+ "the first frame is a single colour -- no picture was decoded"
229+ );
230+}
231+
184 /// A seek into the second GOP hands back that very frame, not the keyframe it232 /// A seek into the second GOP hands back that very frame, not the keyframe it
185 /// had to restart from: the demuxer's sync index and the plugin's skip count233 /// had to restart from: the demuxer's sync index and the plugin's skip count
186 /// have to agree, and Matroska is where both are this crate's own doing.234 /// have to agree, and Matroska is where both are this crate's own doing.
scripts/gen_fixtures.shmodified+13 -4
@@ -103,16 +103,25 @@ ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \
103 # at all, so AV1 is read out of an mkv (`demux::MkvDemuxer`). Same A/V shape as103 # at all, so AV1 is read out of an mkv (`demux::MkvDemuxer`). Same A/V shape as
104 # the others so the tests measure the codec and nothing else -- but `-g 30` is104 # the others so the tests measure the codec and nothing else -- but `-g 30` is
105 # not a preference: it puts a second keyframe at frame 30, which is what the105 # not a preference: it puts a second keyframe at frame 30, which is what the
106-# sync index and the seek test are checked against. 8-bit Main profile, because106+# sync index and the seek test are checked against. 8-bit Main profile here; the
107-# the plugin's NV12 read-back cannot carry 10-bit. Its AAC track is deliberate107+# 10-bit twin is below. Its AAC track is deliberate too: Matroska audio is not
108-# too: Matroska audio is not wired to the decoder yet, and the notice that says108+# wired to the decoder yet, and the notice that says so needs a track to name.
109-# so needs a track to name.
110 ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \109 ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \
111 -f lavfi -i "sine=frequency=440:duration=2" \110 -f lavfi -i "sine=frequency=440:duration=2" \
112 -f lavfi -i "sine=frequency=880:duration=2" \111 -f lavfi -i "sine=frequency=880:duration=2" \
113 -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \112 -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \
114 -map 0:v -map "[a]" -c:v libsvtav1 -preset 8 -g 30 -pix_fmt yuv420p \113 -map 0:v -map "[a]" -c:v libsvtav1 -preset 8 -g 30 -pix_fmt yuv420p \
115 -c:a aac -b:a 128k assets/test_av1.mkv114 -c:a aac -b:a 128k assets/test_av1.mkv
115+# ...and the same file 10-bit, which is what an `av1C` with `high_bitdepth` set
116+# reads as and what the P010 surface pool is picked by -- the AV1 seat of the
117+# pair test_hevc.mkv/test_hevc10.mkv already make for HEVC. libaom rather than
118+# SVT-AV1 because it is the encoder that writes a 10-bit `av1C` here.
119+ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \
120+ -f lavfi -i "sine=frequency=440:duration=2" \
121+ -f lavfi -i "sine=frequency=880:duration=2" \
122+ -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \
123+ -map 0:v -map "[a]" -c:v libaom-av1 -cpu-used 8 -row-mt 1 -g 30 -pix_fmt yuv420p10le \
124+ -c:a aac -b:a 128k assets/test_av1_10.mkv
116 # H.264 in Matroska: the same stream the mp4 fixtures carry, in the container125 # H.264 in Matroska: the same stream the mp4 fixtures carry, in the container
117 # that used to refuse it. Baseline profile so `rusty_h264` reads it with nothing126 # that used to refuse it. Baseline profile so `rusty_h264` reads it with nothing
118 # installed, `-g 30` for the second keyframe the sync index is checked against,127 # installed, `-g 30` for the second keyframe the sync index is checked against,