diff --git a/crates/engine-hw/src/lib.rs b/crates/engine-hw/src/lib.rs index c4e4b4b..70ad2ba 100644 --- a/crates/engine-hw/src/lib.rs +++ b/crates/engine-hw/src/lib.rs @@ -59,7 +59,8 @@ type Handle = as StatelessVideoDecoder>::Handle; /// still loads and still decodes H.264 (it simply refuses the newer codecs' /// files at `Demuxer`). /// -/// 4:2:0 only, 8- or 10-bit: HEVC Main and Main 10, VP9 profile 0, AV1 Main. +/// 4:2:0 only, 8- or 10-bit: HEVC Main and Main 10, VP9 profile 0, AV1 Main at +/// either depth. /// A 10-bit stream decodes into a P010 pool and is read back down to the 8-bit /// I420 every frame here is ([`Session::emit`]). /// @@ -67,8 +68,8 @@ type Handle = as StatelessVideoDecoder>::Handle; /// interface all the way to the renderer. That costs the low two bits of a Main /// 10 source; the upgrade path is a 16-bit `VhFrame`, which is a change to every /// stage between here and the texture upload, not to this file. -/// VP9 profile 2 and 10-bit AV1 are still refused by `Demuxer`: neither codec -/// states its depth where this reads it. +/// VP9 profile 2 is still refused by `Demuxer`: a VP9 stream does not state its +/// depth where that reads it (an `av1C` and an `hvcC` both do). enum Decoder { H264(Dec), Hevc(Dec), diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index f3f08ed..324c0b0 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -103,9 +103,10 @@ impl Demuxer { } /// Bits per luma sample the stream is coded at: 8 for everything but an - /// HEVC Main 10 track, which decodes into a P010 surface rather than an - /// NV12 one. Not part of [`VideoMeta`] because nothing above the plugin has - /// a use for it -- what comes out of the read-back is 8-bit either way. + /// HEVC Main 10 or a 10-bit AV1 track, which decode into a P010 surface + /// rather than an NV12 one. Not part of [`VideoMeta`] because nothing above + /// the plugin has a use for it -- what comes out of the read-back is 8-bit + /// either way. pub fn bit_depth(&self) -> u8 { match self { Self::Mp4(d) => d.bit_depth, @@ -234,7 +235,9 @@ impl Mp4Demuxer { let (_, entry) = sample_entry(path, track_id)?; let av1c = child(entry.get(78..).unwrap_or_default(), b"av1C") .ok_or("no av1C box in the AV1 sample entry")?; - parameter_sets = parse_av1c(av1c)?; + let (sets, depth) = parse_av1c(av1c)?; + parameter_sets = sets; + bit_depth = depth; } // No parameter sets: a VP9 sample is self-contained. Codec::Vp9 => {} @@ -733,7 +736,10 @@ fn mkv_track_entry( // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4 // sample entry carries, so it parses with the very same reader. let (codec, nal_length, config, bit_depth) = match (kind, codec.as_str()) { - (1, "V_AV1") => (Codec::Av1, 4, parse_av1c(&config)?, 8), + (1, "V_AV1") => { + let (sets, bit_depth) = parse_av1c(&config)?; + (Codec::Av1, 4, sets, bit_depth) + } (1, "V_MPEGH/ISO/HEVC") => { let (nal_length, sets, bit_depth) = parse_hvcc(&config)?; (Codec::Hevc, nal_length, sets, bit_depth) @@ -808,25 +814,33 @@ fn parse_avcc(rec: &[u8]) -> crate::Result<(usize, Vec)> { /// §2.3.3): four fixed bytes, then the sequence header OBU in the same /// low-overhead format the blocks are in, so it needs no reframing. /// +/// Also the bits per luma sample, out of `high_bitdepth`/`twelve_bit` in byte 2: +/// 10-bit decodes through the P010 pool HEVC Main 10 already goes through +/// ([`crate::hw`]), 12-bit is refused by name because that pool is the only +/// deeper one there is. +/// /// An empty record is not an error -- an encoder that repeats its sequence -/// header in-band needs none -- but a 10- or 12-bit one is refused by name, for -/// the same reason HEVC Main 10 is: the plugin's NV12 pool and its `vaGetImage` -/// read-back are 8-bit, and the upgrade path is a P010 pool. -fn parse_av1c(rec: &[u8]) -> crate::Result> { +/// header in-band needs none -- and is taken as 8-bit, which is what the depth +/// defaults to everywhere else here. +fn parse_av1c(rec: &[u8]) -> crate::Result<(Vec, u8)> { if rec.is_empty() { - return Ok(Vec::new()); + return Ok((Vec::new(), 8)); } let &flags = rec .get(2) .ok_or("av1C record shorter than its fixed header")?; - if flags & 0x40 != 0 { - return Err(format!( - "{}-bit AV1 is not supported yet — this file is not 8-bit", - if flags & 0x20 != 0 { 12 } else { 10 } - ) - .into()); - } - Ok(rec.get(4..).unwrap_or_default().to_vec()) + // `twelve_bit` only says anything when `high_bitdepth` is set (AV1 §5.5.1), + // so it is read where the record means it and nowhere else. + let bit_depth = match (flags & 0x40 != 0, flags & 0x20 != 0) { + (true, true) => { + return Err( + "12-bit AV1 is not supported — 8- and 10-bit are what the decoder carries".into(), + ); + } + (true, false) => 10, + _ => 8, + }; + Ok((rec.get(4..).unwrap_or_default().to_vec(), bit_depth)) } /// Every block of track `number`, in storage order, with the presentation span @@ -1618,8 +1632,9 @@ mod tests { } /// The `av1C` record: four fixed bytes and then the sequence header OBU, - /// which is handed to the decoder as it stands. 10-bit is refused by name - /// exactly as HEVC Main 10 is, and for the same 8-bit read-back reason. + /// which is handed to the decoder as it stands, and the depth beside it -- + /// 10-bit reads as 10 (the P010 pool, exactly as HEVC Main 10), 12-bit is + /// refused by name. #[test] fn reads_the_sequence_header_out_of_an_av1c_record() { // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a @@ -1628,14 +1643,16 @@ mod tests { rec.extend_from_slice(&[ 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00, ]); - let config = parse_av1c(&rec).unwrap(); + let (config, depth) = parse_av1c(&rec).unwrap(); assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs"); assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header"); + assert_eq!(depth, 8); let mut ten = rec.clone(); ten[2] |= 0x40; // high_bitdepth - let refused = parse_av1c(&ten).unwrap_err().to_string(); - assert!(refused.contains("10-bit"), "{refused}"); + let (ten_config, ten_depth) = parse_av1c(&ten).unwrap(); + assert_eq!(ten_depth, 10, "what picks the P010 surface pool"); + assert_eq!(ten_config, config, "the OBUs are read the same either way"); let mut twelve = ten.clone(); twelve[2] |= 0x20; // twelve_bit assert!( @@ -1644,10 +1661,15 @@ mod tests { .to_string() .contains("12-bit") ); + // `twelve_bit` without `high_bitdepth` says nothing, and an 8-bit + // record reads as one whatever that bit holds. + let mut stray = rec.clone(); + stray[2] |= 0x20; + assert_eq!(parse_av1c(&stray).unwrap().1, 8); // An encoder that repeats its sequence header in-band writes no record // at all, which is not an error; a truncated one is. - assert!(parse_av1c(&[]).unwrap().is_empty()); + assert!(parse_av1c(&[]).unwrap().0.is_empty()); assert!(parse_av1c(&[0x81, 0x05]).is_err()); } diff --git a/crates/engine/tests/av1.rs b/crates/engine/tests/av1.rs index 4eb6ed4..bc8ea5c 100644 --- a/crates/engine/tests/av1.rs +++ b/crates/engine/tests/av1.rs @@ -57,6 +57,23 @@ fn the_demuxer_reports_an_av1_track_in_matroska() { assert_eq!(vp9.codec, Codec::Vp9); } +/// The depth, which is what picks the surface pool the plugin decodes into: an +/// `av1C` with `high_bitdepth` set reads as 10 and takes the P010 pool HEVC Main +/// 10 already goes through, and the 8-bit fixture beside it still reads as 8. +/// It used to be an outright refusal at open ("10-bit AV1 is not supported"). +#[test] +fn a_ten_bit_av1_track_is_read_as_ten_bit() { + let (meta, ten) = Demuxer::open(&asset("test_av1_10.mkv")).expect("open test_av1_10.mkv"); + assert_eq!(meta.codec, Codec::Av1); + assert_eq!((meta.width, meta.height), (1280, 720)); + assert_eq!(meta.frame_count, FRAMES); + assert_eq!(ten.bit_depth(), 10, "what picks the P010 surface pool"); + + // The invariant: the 8-bit file is untouched by any of it. + let (_, eight) = Demuxer::open(&asset("test_av1.mkv")).expect("open test_av1.mkv"); + assert_eq!(eight.bit_depth(), 8); +} + /// The delivered audio scope, as an assert: a Matroska file's AAC track is read /// like any other file's (`tests/hevc_mkv.rs` is where that lives), and its /// *picture* being AV1 changes nothing about it. @@ -181,6 +198,37 @@ fn the_plugin_decodes_every_av1_frame() { ); } +/// ...and the 10-bit file through the very call the window's open door makes: a +/// user opens it and the timeline shows a picture, read back off the P010 pool +/// to the same 8-bit BGRA every other frame arrives in. The whole point of the +/// slice, and the half a container test cannot say anything about. +#[test] +#[ignore = "needs libengine_hw.so and a VA-API driver with 10-bit AV1 decode"] +fn opening_a_ten_bit_av1_file_shows_frames() { + let mut session = + PlaybackSession::open(asset("test_av1_10.mkv")).expect("open the 10-bit file"); + assert_eq!(session.meta().codec, Codec::Av1); + assert_eq!(session.meta().frame_count, FRAMES); + + // The decoder is a thread behind the door, so a frame is waited for exactly + // as the window's own pump waits for one. + let deadline = Instant::now() + Duration::from_secs(60); + let frame = loop { + if let Some(frame) = session.try_frame() { + break frame; + } + assert!(Instant::now() < deadline, "no frame in 60 s"); + std::thread::sleep(Duration::from_millis(10)); + }; + assert_eq!((frame.width, frame.height), (1280, 720)); + // A picture, not a flat surface: a P010 surface read back through the NV12 + // path would come out as noise or as nothing at all. + assert!( + frame.bgra.chunks_exact(4).any(|px| px != &frame.bgra[..4]), + "the first frame is a single colour -- no picture was decoded" + ); +} + /// A seek into the second GOP hands back that very frame, not the keyframe it /// had to restart from: the demuxer's sync index and the plugin's skip count /// have to agree, and Matroska is where both are this crate's own doing. diff --git a/scripts/gen_fixtures.sh b/scripts/gen_fixtures.sh index e849eba..a2466d6 100755 --- a/scripts/gen_fixtures.sh +++ b/scripts/gen_fixtures.sh @@ -103,16 +103,26 @@ ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ # at all, so AV1 is read out of an mkv (`demux::MkvDemuxer`). Same A/V shape as # the others so the tests measure the codec and nothing else -- but `-g 30` is # not a preference: it puts a second keyframe at frame 30, which is what the -# sync index and the seek test are checked against. 8-bit Main profile, because -# the plugin's NV12 read-back cannot carry 10-bit. Its AAC track is deliberate -# too: it is the AAC a Matroska file's sound is read out of through symphonia, -# beside the AC-3 fixtures below that no symphonia version decodes. +# sync index and the seek test are checked against. 8-bit Main profile here; the +# 10-bit twin is below. Its AAC track is deliberate too: it is the AAC a Matroska +# file's sound is read out of through symphonia, beside the AC-3 fixtures further +# down that no symphonia version decodes. ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ -f lavfi -i "sine=frequency=440:duration=2" \ -f lavfi -i "sine=frequency=880:duration=2" \ -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \ -map 0:v -map "[a]" -c:v libsvtav1 -preset 8 -g 30 -pix_fmt yuv420p \ -c:a aac -b:a 128k assets/test_av1.mkv +# ...and the same file 10-bit, which is what an `av1C` with `high_bitdepth` set +# reads as and what the P010 surface pool is picked by -- the AV1 seat of the +# pair test_hevc.mkv/test_hevc10.mkv already make for HEVC. libaom rather than +# SVT-AV1 because it is the encoder that writes a 10-bit `av1C` here. +ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ + -f lavfi -i "sine=frequency=440:duration=2" \ + -f lavfi -i "sine=frequency=880:duration=2" \ + -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \ + -map 0:v -map "[a]" -c:v libaom-av1 -cpu-used 8 -row-mt 1 -g 30 -pix_fmt yuv420p10le \ + -c:a aac -b:a 128k assets/test_av1_10.mkv # H.264 in Matroska: the same stream the mp4 fixtures carry, in the container # that used to refuse it. Baseline profile so `rusty_h264` reads it with nothing # installed, `-g 30` for the second keyframe the sync index is checked against,