Merge remote-tracking branch 'origin/main' into worktree-agent-ab6959260e5ee211c
Browse files at this commit4 changed files +112 -31
- crates/engine-hw/src/lib.rs +4 -3
- crates/engine/src/demux.rs +46 -24
- crates/engine/tests/av1.rs +48 -0
- scripts/gen_fixtures.sh +14 -4
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-bit | 64 | /// 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 Main | 68 | /// 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 every | 69 | /// 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 codec | 71 | +/// 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 an | 105 | /// 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 | 106 | + /// 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 has | 107 | + /// 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 => {} |
| @@ -733,7 +736,10 @@ fn mkv_track_entry( | |||
| 733 | // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4 | 736 | // `av1C` for AV1 and an `hvcC` for HEVC, byte for byte the one an mp4 |
| 734 | // sample entry carries, so it parses with the very same reader. | 737 | // sample entry carries, so it parses with the very same reader. |
| 735 | let (codec, nal_length, config, bit_depth) = match (kind, codec.as_str()) { | 738 | let (codec, nal_length, config, bit_depth) = match (kind, codec.as_str()) { |
| 736 | - (1, "V_AV1") => (Codec::Av1, 4, parse_av1c(&config)?, 8), | 739 | + (1, "V_AV1") => { |
| 740 | + let (sets, bit_depth) = parse_av1c(&config)?; | ||
| 741 | + (Codec::Av1, 4, sets, bit_depth) | ||
| 742 | + } | ||
| 737 | (1, "V_MPEGH/ISO/HEVC") => { | 743 | (1, "V_MPEGH/ISO/HEVC") => { |
| 738 | let (nal_length, sets, bit_depth) = parse_hvcc(&config)?; | 744 | let (nal_length, sets, bit_depth) = parse_hvcc(&config)?; |
| 739 | (Codec::Hevc, nal_length, sets, bit_depth) | 745 | (Codec::Hevc, nal_length, sets, bit_depth) |
| @@ -808,25 +814,33 @@ fn parse_avcc(rec: &[u8]) -> crate::Result<(usize, Vec<u8>)> { | |||
| 808 | /// §2.3.3): four fixed bytes, then the sequence header OBU in the same | 814 | /// §2.3.3): four fixed bytes, then the sequence header OBU in the same |
| 809 | /// low-overhead format the blocks are in, so it needs no reframing. | 815 | /// low-overhead format the blocks are in, so it needs no reframing. |
| 810 | /// | 816 | /// |
| 817 | +/// Also the bits per luma sample, out of `high_bitdepth`/`twelve_bit` in byte 2: | ||
| 818 | +/// 10-bit decodes through the P010 pool HEVC Main 10 already goes through | ||
| 819 | +/// ([`crate::hw`]), 12-bit is refused by name because that pool is the only | ||
| 820 | +/// deeper one there is. | ||
| 821 | +/// | ||
| 811 | /// An empty record is not an error -- an encoder that repeats its sequence | 822 | /// An empty record is not an error -- an encoder that repeats its sequence |
| 812 | -/// header in-band needs none -- but a 10- or 12-bit one is refused by name, for | 823 | +/// header in-band needs none -- and is taken as 8-bit, which is what the depth |
| 813 | -/// the same reason HEVC Main 10 is: the plugin's NV12 pool and its `vaGetImage` | 824 | +/// defaults to everywhere else here. |
| 814 | -/// read-back are 8-bit, and the upgrade path is a P010 pool. | 825 | +fn parse_av1c(rec: &[u8]) -> crate::Result<(Vec<u8>, u8)> { |
| 815 | -fn parse_av1c(rec: &[u8]) -> crate::Result<Vec<u8>> { | ||
| 816 | if rec.is_empty() { | 826 | if rec.is_empty() { |
| 817 | - return Ok(Vec::new()); | 827 | + return Ok((Vec::new(), 8)); |
| 818 | } | 828 | } |
| 819 | let &flags = rec | 829 | let &flags = rec |
| 820 | .get(2) | 830 | .get(2) |
| 821 | .ok_or("av1C record shorter than its fixed header")?; | 831 | .ok_or("av1C record shorter than its fixed header")?; |
| 822 | - if flags & 0x40 != 0 { | 832 | + // `twelve_bit` only says anything when `high_bitdepth` is set (AV1 §5.5.1), |
| 823 | - return Err(format!( | 833 | + // so it is read where the record means it and nowhere else. |
| 824 | - "{}-bit AV1 is not supported yet — this file is not 8-bit", | 834 | + let bit_depth = match (flags & 0x40 != 0, flags & 0x20 != 0) { |
| 825 | - if flags & 0x20 != 0 { 12 } else { 10 } | 835 | + (true, true) => { |
| 826 | - ) | 836 | + return Err( |
| 827 | - .into()); | 837 | + "12-bit AV1 is not supported — 8- and 10-bit are what the decoder carries".into(), |
| 828 | - } | 838 | + ); |
| 829 | - Ok(rec.get(4..).unwrap_or_default().to_vec()) | 839 | + } |
| 840 | + (true, false) => 10, | ||
| 841 | + _ => 8, | ||
| 842 | + }; | ||
| 843 | + Ok((rec.get(4..).unwrap_or_default().to_vec(), bit_depth)) | ||
| 830 | } | 844 | } |
| 831 | 845 | ||
| 832 | /// Every block of track `number`, in storage order, with the presentation span | 846 | /// Every block of track `number`, in storage order, with the presentation span |
| @@ -1618,8 +1632,9 @@ mod tests { | |||
| 1618 | } | 1632 | } |
| 1619 | 1633 | ||
| 1620 | /// The `av1C` record: four fixed bytes and then the sequence header OBU, | 1634 | /// The `av1C` record: four fixed bytes and then the sequence header OBU, |
| 1621 | - /// which is handed to the decoder as it stands. 10-bit is refused by name | 1635 | + /// which is handed to the decoder as it stands, and the depth beside it -- |
| 1622 | - /// exactly as HEVC Main 10 is, and for the same 8-bit read-back reason. | 1636 | + /// 10-bit reads as 10 (the P010 pool, exactly as HEVC Main 10), 12-bit is |
| 1637 | + /// refused by name. | ||
| 1623 | #[test] | 1638 | #[test] |
| 1624 | fn reads_the_sequence_header_out_of_an_av1c_record() { | 1639 | fn reads_the_sequence_header_out_of_an_av1c_record() { |
| 1625 | // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a | 1640 | // marker/version, profile 0 + level 5, 8-bit 4:2:0, no delay; then a |
| @@ -1628,14 +1643,16 @@ mod tests { | |||
| 1628 | rec.extend_from_slice(&[ | 1643 | rec.extend_from_slice(&[ |
| 1629 | 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00, | 1644 | 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x2D, 0x4C, 0xFF, 0xB3, 0xC0, 0x2F, 0x80, 0x00, |
| 1630 | ]); | 1645 | ]); |
| 1631 | - let config = parse_av1c(&rec).unwrap(); | 1646 | + let (config, depth) = parse_av1c(&rec).unwrap(); |
| 1632 | assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs"); | 1647 | assert_eq!(config, rec[4..], "the fixed header is not part of the OBUs"); |
| 1633 | assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header"); | 1648 | assert_eq!((config[0] >> 3) & 0xF, 1, "OBU type 1 is a sequence header"); |
| 1649 | + assert_eq!(depth, 8); | ||
| 1634 | 1650 | ||
| 1635 | let mut ten = rec.clone(); | 1651 | let mut ten = rec.clone(); |
| 1636 | ten[2] |= 0x40; // high_bitdepth | 1652 | ten[2] |= 0x40; // high_bitdepth |
| 1637 | - let refused = parse_av1c(&ten).unwrap_err().to_string(); | 1653 | + let (ten_config, ten_depth) = parse_av1c(&ten).unwrap(); |
| 1638 | - assert!(refused.contains("10-bit"), "{refused}"); | 1654 | + assert_eq!(ten_depth, 10, "what picks the P010 surface pool"); |
| 1655 | + assert_eq!(ten_config, config, "the OBUs are read the same either way"); | ||
| 1639 | let mut twelve = ten.clone(); | 1656 | let mut twelve = ten.clone(); |
| 1640 | twelve[2] |= 0x20; // twelve_bit | 1657 | twelve[2] |= 0x20; // twelve_bit |
| 1641 | assert!( | 1658 | assert!( |
| @@ -1644,10 +1661,15 @@ mod tests { | |||
| 1644 | .to_string() | 1661 | .to_string() |
| 1645 | .contains("12-bit") | 1662 | .contains("12-bit") |
| 1646 | ); | 1663 | ); |
| 1664 | + // `twelve_bit` without `high_bitdepth` says nothing, and an 8-bit | ||
| 1665 | + // record reads as one whatever that bit holds. | ||
| 1666 | + let mut stray = rec.clone(); | ||
| 1667 | + stray[2] |= 0x20; | ||
| 1668 | + assert_eq!(parse_av1c(&stray).unwrap().1, 8); | ||
| 1647 | 1669 | ||
| 1648 | // An encoder that repeats its sequence header in-band writes no record | 1670 | // An encoder that repeats its sequence header in-band writes no record |
| 1649 | // at all, which is not an error; a truncated one is. | 1671 | // at all, which is not an error; a truncated one is. |
| 1650 | - assert!(parse_av1c(&[]).unwrap().is_empty()); | 1672 | + assert!(parse_av1c(&[]).unwrap().0.is_empty()); |
| 1651 | assert!(parse_av1c(&[0x81, 0x05]).is_err()); | 1673 | assert!(parse_av1c(&[0x81, 0x05]).is_err()); |
| 1652 | } | 1674 | } |
| 1653 | 1675 | ||
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 read | 77 | /// 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 its | 78 | /// 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 it | 232 | /// 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 count | 233 | /// 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+14 -4
| @@ -103,16 +103,26 @@ 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 as | 103 | # 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` is | 104 | # 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 the | 105 | # 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 | 106 | +# 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 deliberate | 107 | +# 10-bit twin is below. Its AAC track is deliberate too: it is the AAC a Matroska |
| 108 | -# too: it is the AAC a Matroska file's sound is read out of through symphonia, | 108 | +# file's sound is read out of through symphonia, beside the AC-3 fixtures further |
| 109 | -# beside the AC-3 fixtures below that no symphonia version decodes. | 109 | +# down that no symphonia version decodes. |
| 110 | ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ | 110 | ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ |
| 111 | -f lavfi -i "sine=frequency=440:duration=2" \ | 111 | -f lavfi -i "sine=frequency=440:duration=2" \ |
| 112 | -f lavfi -i "sine=frequency=880:duration=2" \ | 112 | -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]" \ | 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]" \ |
| 114 | -map 0:v -map "[a]" -c:v libsvtav1 -preset 8 -g 30 -pix_fmt yuv420p \ | 114 | -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.mkv | 115 | -c:a aac -b:a 128k assets/test_av1.mkv |
| 116 | +# ...and the same file 10-bit, which is what an `av1C` with `high_bitdepth` set | ||
| 117 | +# reads as and what the P010 surface pool is picked by -- the AV1 seat of the | ||
| 118 | +# pair test_hevc.mkv/test_hevc10.mkv already make for HEVC. libaom rather than | ||
| 119 | +# SVT-AV1 because it is the encoder that writes a 10-bit `av1C` here. | ||
| 120 | +ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ | ||
| 121 | + -f lavfi -i "sine=frequency=440:duration=2" \ | ||
| 122 | + -f lavfi -i "sine=frequency=880:duration=2" \ | ||
| 123 | + -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \ | ||
| 124 | + -map 0:v -map "[a]" -c:v libaom-av1 -cpu-used 8 -row-mt 1 -g 30 -pix_fmt yuv420p10le \ | ||
| 125 | + -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 container | 126 | # 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 nothing | 127 | # 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, | 128 | # installed, `-g 30` for the second keyframe the sync index is checked against, |