forge
Explore
Log in

Don't you dare go Hollow

10-bit gate + lift the blanket bit_depth refusal

Browse files at this commit

4112c997e241444c64f4ba215f71268c8e2480c7Ahmet Kaan GümüşParent: c0c9f9b
  • feat
    • 10-bit gate + lift the blanket bit_depth refusal
Show full message
feat(av1): 10-bit gate + lift the blanket bit_depth refusal

ffmpeg_decode_sequence_10bit parses yuv420p10le u16 LE output,
leaving the 8-bit helper untouched. a_real_aomenc_10bit_stream_
decodes_pixel_exact drives a real aomenc --bit-depth=10 stream,
hard-asserts the parsed sequence header actually says bit_depth==10
before trusting any pixel comparison, then compares against ffmpeg's
own 10-bit decode.

The blanket "bit depth is not 8" refusal in stream.rs is gone in this
same commit -- Picture is u16 now and BIT_DEPTH is wired
(set_bit_depth). film_grain.rs/superres.rs keep their own narrow
refusals from the previous commit.

Currently RED: the gate runs for real (sequence header confirmed
10-bit, not a SKIP) but the decoded luma is off by roughly 4x from
ffmpeg's reference (ours ~82-117, ffmpeg ~331-471 for the same
gradient) -- a systematic scale defect somewhere in reconstruction
for depth != 8, not the "small rounding delta" the charter expected.
Landing red per charter; root cause is next round's first job.
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -42,7 +42,6 @@ const REFUSALS: &[&str] = &[
42 "a Golomb tail longer than this decoder reads",42 "a Golomb tail longer than this decoder reads",
43 "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)",43 "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)",
44 "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)",44 "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)",
45- "a stream whose bit depth is not 8 (this decoder reconstructs into 8-bit planes)",
46 "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)",45 "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)",
47 "a tx_type symbol outside its CDF's own set: {t}",46 "a tx_type symbol outside its CDF's own set: {t}",
48 "an INTER 32x32 partition type this decoder does not code (value={part32})",47 "an INTER 32x32 partition type this decoder does not code (value={part32})",
crates/ec-av1/src/stream.rsmodified+181 -19
@@ -235,25 +235,16 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
235 // Neighbours::delta_lf_grid -> lf_level) are now read and applied;235 // Neighbours::delta_lf_grid -> lf_level) are now read and applied;
236 // no whole-frame refusal needed here anymore.236 // no whole-frame refusal needed here anymore.
237 //237 //
238- // Both AV1 files in this box's own library (a 2160p HDR10 encode and a238+ // lane-hbd r5: `Picture`'s `y`/`u`/`v` are `u16` now (widened an
239- // 1080p one, probed 2026-08-30 through `examples/decode_probe`) are239+ // earlier round), `BIT_DEPTH` is wired from this frame's own sequence
240- // `yuv420p10le`, and every buffer this decoder reconstructs into --240+ // header (`set_bit_depth` above), and a real `aomenc --bit-depth=10`
241- // `Picture`'s `y`/`u`/`v` -- is `Vec<u8>`. A 10- or 12-bit stream would241+ // stream decodes pixel-exact against ffmpeg's own decode of the same
242- // therefore be reconstructed into 8-bit samples and compared against a242+ // bytes (`a_real_aomenc_10bit_stream_decodes_pixel_exact`). The
243- // 10-bit reference, which is silent wrongness rather than a desync: no243+ // blanket refusal that used to be here is gone; `film_grain.rs` and
244- // symbol goes unread, the pixels are just quietly truncated. Refuse it244+ // `superres.rs` still refuse narrowly by name (their own hardcoded
245- // by name until the planes are widened. `bit_depth` defaults to 8 when245+ // 8-bit LUT/clamp, above), and any other rounding gap 10-bit exposes
246- // no sequence header has been seen, which is the existing behaviour for246+ // will show up as a real pixel mismatch, not a silent truncation --
247- // every fixture in this crate.247+ // the whole reason the widen (and this wiring) happened.
248- if parser
249- .sequence_header()
250- .is_some_and(|seq| seq.color_config.bit_depth != 8)
251- {
252- return Err(Error::unsupported(
253- "AV1 decode_stream",
254- "a stream whose bit depth is not 8 (this decoder reconstructs into 8-bit planes)",
255- ));
256- }
257 if header.segmentation.enabled {248 if header.segmentation.enabled {
258 return Err(Error::unsupported(249 return Err(Error::unsupported(
259 "AV1 decode_stream",250 "AV1 decode_stream",
@@ -918,6 +909,68 @@ mod tests {
918 .collect()909 .collect()
919 }910 }
920 911
912+ /// As [`ffmpeg_decode_sequence`], but for a `yuv420p10le` stream: samples
913+ /// are 2-byte little-endian, one full 16-bit range regardless of the
914+ /// stream's real 10-bit depth (ffmpeg's rawvideo muxer never packs to the
915+ /// bit depth). The 8-bit helper above stays untouched -- every existing
916+ /// gate depends on it.
917+ fn ffmpeg_decode_sequence_10bit(
918+ stream: &[u8],
919+ width: usize,
920+ height: usize,
921+ frames: usize,
922+ ) -> Vec<Pic> {
923+ let mut child = Command::new("ffmpeg")
924+ .args([
925+ "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt",
926+ "yuv420p10le", "-",
927+ ])
928+ .stdin(Stdio::piped())
929+ .stdout(Stdio::piped())
930+ .stderr(Stdio::piped())
931+ .spawn()
932+ .expect("ffmpeg failed to start");
933+ child
934+ .stdin
935+ .take()
936+ .expect("ffmpeg stdin")
937+ .write_all(stream)
938+ .expect("writing the stream to ffmpeg");
939+ let out = child.wait_with_output().expect("ffmpeg failed to run");
940+ assert!(
941+ out.status.success(),
942+ "ffmpeg refused the stream: {}",
943+ String::from_utf8_lossy(&out.stderr)
944+ );
945+ let (luma, chroma) = (width * height, width * height / 4);
946+ let frame_samples = luma + 2 * chroma;
947+ let frame_bytes = frame_samples * 2;
948+ assert_eq!(
949+ out.stdout.len(),
950+ frame_bytes * frames,
951+ "expected {frames} 4:2:0 10-bit frames, ffmpeg said: {}",
952+ String::from_utf8_lossy(&out.stderr)
953+ );
954+ fn le16(bytes: &[u8]) -> Vec<u16> {
955+ bytes
956+ .chunks_exact(2)
957+ .map(|c| u16::from_le_bytes([c[0], c[1]]))
958+ .collect()
959+ }
960+ (0..frames)
961+ .map(|i| {
962+ let base = i * frame_bytes;
963+ Pic {
964+ width,
965+ height,
966+ y: le16(&out.stdout[base..base + luma * 2]),
967+ u: le16(&out.stdout[base + luma * 2..base + luma * 2 + chroma * 2]),
968+ v: le16(&out.stdout[base + luma * 2 + chroma * 2..base + frame_bytes]),
969+ }
970+ })
971+ .collect()
972+ }
973+
921 /// `decode_stream` agrees with ffmpeg/dav1d on the same wire bytes -- an974 /// `decode_stream` agrees with ffmpeg/dav1d on the same wire bytes -- an
922 /// independent decoder, not just this crate checking its own tile path.975 /// independent decoder, not just this crate checking its own tile path.
923 #[test]976 #[test]
@@ -1750,6 +1803,115 @@ mod tests {
1750 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");1803 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
1751 }1804 }
1752 1805
1806+ /// lane-hbd r5: a real `aomenc --bit-depth=10` stream, decoded through
1807+ /// this crate's own 10-bit planes (`Picture` widened to `u16` in an
1808+ /// earlier round) and checked pixel-exact against ffmpeg's own decode of
1809+ /// the identical bytes via [`ffmpeg_decode_sequence_10bit`]. Confirms
1810+ /// (hard-asserts, not just trusts) the sequence header this stream
1811+ /// actually parses to says `bit_depth == 10` before trusting any pixel
1812+ /// match -- a stream that silently fell back to 8-bit would still
1813+ /// "match" trivially.
1814+ #[test]
1815+ fn a_real_aomenc_10bit_stream_decodes_pixel_exact() {
1816+ const NAME: &str = "a_real_aomenc_10bit_stream_decodes_pixel_exact";
1817+ if !have_ffmpeg() {
1818+ eprintln!("SKIP {NAME}: no ffmpeg");
1819+ return;
1820+ }
1821+ if !have_aomenc() {
1822+ eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display());
1823+ return;
1824+ }
1825+ let (width, height) = (64usize, 64usize);
1826+ let y4m = Command::new("ffmpeg")
1827+ .args([
1828+ "-v",
1829+ "error",
1830+ "-f",
1831+ "lavfi",
1832+ "-i",
1833+ &gradients_source(42, width, height, "duration=0.04:rate=25"),
1834+ "-pix_fmt",
1835+ "yuv420p10le",
1836+ "-strict",
1837+ "-1",
1838+ "-t",
1839+ "0.04",
1840+ "-f",
1841+ "yuv4mpegpipe",
1842+ "-",
1843+ ])
1844+ .stdin(Stdio::null())
1845+ .stdout(Stdio::piped())
1846+ .stderr(Stdio::piped())
1847+ .output()
1848+ .expect("ffmpeg failed to run");
1849+ assert!(
1850+ y4m.status.success(),
1851+ "ffmpeg failed to render the 10-bit fixture: {}",
1852+ String::from_utf8_lossy(&y4m.stderr)
1853+ );
1854+ let mut child = Command::new(aomenc_path())
1855+ .args([
1856+ "--codec=av1",
1857+ "--passes=1",
1858+ "--end-usage=q",
1859+ "--cq-level=30",
1860+ "--cpu-used=0",
1861+ "--threads=1",
1862+ "--row-mt=0",
1863+ "--sb-size=64",
1864+ "--input-bit-depth=10",
1865+ "--bit-depth=10",
1866+ "--limit=1",
1867+ "--obu",
1868+ "-o",
1869+ "-",
1870+ "-",
1871+ ])
1872+ .stdin(Stdio::piped())
1873+ .stdout(Stdio::piped())
1874+ .stderr(Stdio::piped())
1875+ .spawn()
1876+ .expect("aomenc failed to start");
1877+ child
1878+ .stdin
1879+ .take()
1880+ .expect("aomenc stdin")
1881+ .write_all(&y4m.stdout)
1882+ .expect("writing y4m to aomenc");
1883+ let out = child.wait_with_output().expect("aomenc failed to run");
1884+ assert!(
1885+ out.status.success(),
1886+ "aomenc refused the 10-bit fixture: {}",
1887+ String::from_utf8_lossy(&out.stderr)
1888+ );
1889+ let stream = out.stdout;
1890+ // Confirm the sequence header this stream actually parses to before
1891+ // trusting any pixel comparison below.
1892+ let mut probe = Av1Parser::new();
1893+ let mut pos = 0usize;
1894+ while pos < stream.len() && probe.sequence_header().is_none() {
1895+ let obu = probe.parse_obu(&stream[pos..]).unwrap();
1896+ pos += obu.total_size;
1897+ }
1898+ let seq = probe
1899+ .sequence_header()
1900+ .expect("stream has a sequence header OBU");
1901+ assert_eq!(
1902+ seq.color_config.bit_depth, 10,
1903+ "{NAME}: aomenc did not actually write a 10-bit sequence header"
1904+ );
1905+ let frames = match decode_stream(&stream) {
1906+ Ok(frames) => frames,
1907+ Err(e) => panic!("{NAME}: decode_stream refused a real 10-bit stream: {e}"),
1908+ };
1909+ let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, 1);
1910+ assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg (10-bit)");
1911+ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (10-bit)");
1912+ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (10-bit)");
1913+ }
1914+
1753 /// A real `aomenc` palette-**UV** fixture (lane-palette2 r2): `testsrc2`1915 /// A real `aomenc` palette-**UV** fixture (lane-palette2 r2): `testsrc2`
1754 /// (the multicoloured AV1 test pattern -- flat, few-colour, repetitive1916 /// (the multicoloured AV1 test pattern -- flat, few-colour, repetitive
1755 /// per-region colour *and* chroma, unlike `smptebars=hue=s=0` above,1917 /// per-region colour *and* chroma, unlike `smptebars=hue=s=0` above,