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;
5959 /// still loads and still decodes H.264 (it simply refuses the newer codecs'
6060 /// files at `Demuxer`).
6161 ///
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.
6364 /// A 10-bit stream decodes into a P010 pool and is read back down to the 8-bit
6465 /// I420 every frame here is ([`Session::emit`]).
6566 ///
@@ -67,8 +68,8 @@ type Handle = <Dec<H264> as StatelessVideoDecoder>::Handle;
6768 /// interface all the way to the renderer. That costs the low two bits of a Main
6869 /// 10 source; the upgrade path is a 16-bit `VhFrame`, which is a change to every
6970 /// 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 codec
71-/// states its depth where this reads it.
71+/// VP9 profile 2 is still refused by `Demuxer`: a VP9 stream does not state its
72+/// depth where that reads it (an `av1C` and an `hvcC` both do).
7273 enum Decoder {
7374 H264(Dec<H264>),
7475 Hevc(Dec<H265>),
crates/engine/src/demux.rsmodified+46 -24
@@ -103,9 +103,10 @@ impl Demuxer {
103103 }
104104
105105 /// 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 an
107- /// NV12 one. Not part of [`VideoMeta`] because nothing above the plugin has
108- /// a use for it -- what comes out of the read-back is 8-bit either way.
106+ /// HEVC Main 10 or a 10-bit AV1 track, which decode into a P010 surface
107+ /// rather than an NV12 one. Not part of [`VideoMeta`] because nothing above
108+ /// the plugin has a use for it -- what comes out of the read-back is 8-bit
109+ /// either way.
109110 pub fn bit_depth(&self) -> u8 {
110111 match self {
111112 Self::Mp4(d) => d.bit_depth,
@@ -234,7 +235,9 @@ impl Mp4Demuxer {
234235 let (_, entry) = sample_entry(path, track_id)?;
235236 let av1c = child(entry.get(78..).unwrap_or_default(), b"av1C")
236237 .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;
238241 }
239242 // No parameter sets: a VP9 sample is self-contained.
240243 Codec::Vp9 => {}
@@ -639,7 +642,10 @@ fn mkv_track_entry(
639642 // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4
640643 // sample entry carries, so it parses with the very same reader.
641644 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+ }
643649 (1, "V_MPEGH/ISO/HEVC") => {
644650 let (nal_length, sets, bit_depth) = parse_hvcc(&config)?;
645651 (Codec::Hevc, nal_length, sets, bit_depth)
@@ -714,25 +720,33 @@ fn parse_avcc(rec: &[u8]) -> crate::Result<(usize, Vec<u8>)> {
714720 /// §2.3.3): four fixed bytes, then the sequence header OBU in the same
715721 /// low-overhead format the blocks are in, so it needs no reframing.
716722 ///
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+///
717728 /// 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, for
719-/// the same reason HEVC Main 10 is: the plugin's NV12 pool and its `vaGetImage`
720-/// read-back are 8-bit, and the upgrade path is a P010 pool.
721-fn parse_av1c(rec: &[u8]) -> crate::Result<Vec<u8>> {
729+/// header in-band needs none -- and is taken as 8-bit, which is what the depth
730+/// defaults to everywhere else here.
731+fn parse_av1c(rec: &[u8]) -> crate::Result<(Vec<u8>, u8)> {
722732 if rec.is_empty() {
723- return Ok(Vec::new());
733+ return Ok((Vec::new(), 8));
724734 }
725735 let &flags = rec
726736 .get(2)
727737 .ok_or("av1C record shorter than its fixed header")?;
728- if flags & 0x40 != 0 {
729- return Err(format!(
730- "{}-bit AV1 is not supported yet — this file is not 8-bit",
731- if flags & 0x20 != 0 { 12 } else { 10 }
732- )
733- .into());
734- }
735- Ok(rec.get(4..).unwrap_or_default().to_vec())
738+ // `twelve_bit` only says anything when `high_bitdepth` is set (AV1 §5.5.1),
739+ // so it is read where the record means it and nowhere else.
740+ let bit_depth = match (flags & 0x40 != 0, flags & 0x20 != 0) {
741+ (true, true) => {
742+ return Err(
743+ "12-bit AV1 is not supported — 8- and 10-bit are what the decoder carries".into(),
744+ );
745+ }
746+ (true, false) => 10,
747+ _ => 8,
748+ };
749+ Ok((rec.get(4..).unwrap_or_default().to_vec(), bit_depth))
736750 }
737751
738752 /// Every block of track `number`, in storage order, with the presentation span
@@ -1307,8 +1321,9 @@ mod tests {
13071321 }
13081322
13091323 /// 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 name
1311- /// exactly as HEVC Main 10 is, and for the same 8-bit read-back reason.
1324+ /// which is handed to the decoder as it stands, and the depth beside it --
1325+ /// 10-bit reads as 10 (the P010 pool, exactly as HEVC Main 10), 12-bit is
1326+ /// refused by name.
13121327 #[test]
13131328 fn reads_the_sequence_header_out_of_an_av1c_record() {
13141329 // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a
@@ -1317,14 +1332,16 @@ mod tests {
13171332 rec.extend_from_slice(&[
13181333 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00,
13191334 ]);
1320- let config = parse_av1c(&rec).unwrap();
1335+ let (config, depth) = parse_av1c(&rec).unwrap();
13211336 assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs");
13221337 assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header");
1338+ assert_eq!(depth, 8);
13231339
13241340 let mut ten = rec.clone();
13251341 ten[2] |= 0x40; // high_bitdepth
1326- let refused = parse_av1c(&ten).unwrap_err().to_string();
1327- assert!(refused.contains("10-bit"), "{refused}");
1342+ let (ten_config, ten_depth) = parse_av1c(&ten).unwrap();
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");
13281345 let mut twelve = ten.clone();
13291346 twelve[2] |= 0x20; // twelve_bit
13301347 assert!(
@@ -1333,10 +1350,15 @@ mod tests {
13331350 .to_string()
13341351 .contains("12-bit")
13351352 );
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);
13361358
13371359 // An encoder that repeats its sequence header in-band writes no record
13381360 // 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());
13401362 assert!(parse_av1c(&[0x81, 0x05]).is_err());
13411363 }
13421364
crates/engine/tests/av1.rsmodified+48 -0
@@ -57,6 +57,23 @@ fn the_demuxer_reports_an_av1_track_in_matroska() {
5757 assert_eq!(vp9.codec, Codec::Vp9);
5858 }
5959
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+
6077 /// The delivered audio scope, as an assert: a Matroska file's AAC track is read
6178 /// like any other file's (`tests/hevc_mkv.rs` is where that lives), and its
6279 /// *picture* being AV1 changes nothing about it.
@@ -181,6 +198,37 @@ fn the_plugin_decodes_every_av1_frame() {
181198 );
182199 }
183200
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+
184232 /// A seek into the second GOP hands back that very frame, not the keyframe it
185233 /// had to restart from: the demuxer's sync index and the plugin's skip count
186234 /// 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 \
103103 # at all, so AV1 is read out of an mkv (`demux::MkvDemuxer`). Same A/V shape as
104104 # the others so the tests measure the codec and nothing else -- but `-g 30` is
105105 # 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, because
107-# the plugin's NV12 read-back cannot carry 10-bit. Its AAC track is deliberate
108-# too: Matroska audio is not wired to the decoder yet, and the notice that says
109-# so needs a track to name.
106+# sync index and the seek test are checked against. 8-bit Main profile here; the
107+# 10-bit twin is below. Its AAC track is deliberate too: Matroska audio is not
108+# wired to the decoder yet, and the notice that says so needs a track to name.
110109 ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \
111110 -f lavfi -i "sine=frequency=440:duration=2" \
112111 -f lavfi -i "sine=frequency=880:duration=2" \
113112 -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \
114113 -map 0:v -map "[a]" -c:v libsvtav1 -preset 8 -g 30 -pix_fmt yuv420p \
115114 -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
116125 # H.264 in Matroska: the same stream the mp4 fixtures carry, in the container
117126 # that used to refuse it. Baseline profile so `rusty_h264` reads it with nothing
118127 # installed, `-g 30` for the second keyframe the sync index is checked against,