From abd0a086a1a6e695232562ce2428ecf768d7feb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 23:57:20 +0300 Subject: [PATCH] fix(av1): eob_pt class-1 CDF row missing from 9 of 16 TxbSet arms -- every inter chroma block under a 1D luma tx_type read the 2D row lane-eobc1 r1. libaom's av1_default_eob_multi{16,32,64,128,256,512,1024}_cdfs are [q][PLANE_TYPES][2]; index 1 is eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1 (entropymode.h:81-82, decodetxb.c). Main carried the class-1 row for 6 of the 11 eob_pt tables and left `eob_pt_class1: None` in 9 TxbSet arms -- including Luma4Inter, Luma8Inter and ChromaRect16x8, all of which can code a 1D type. Chroma codes no tx_type symbol of its own: it inherits the colocated luma type verbatim (av1_get_tx_type, blockd.h:1291), so an 8x8 inter leaf with H_/V_ luma puts its 4x4 chroma on the class-1 row -- right eob value, wrong interval, desync one symbol later, surfacing as bogus partition refusals. Ported the remaining 5 tables (128_chroma, 512_luma/chroma, 1024_luma/chroma) from lane-gmaffine's f5e1851, machine-extracted afresh: scripts/extract-eob-class1.py parses token_cdfs.h, emits the [q][plane][1] rows and `--check`s them against cdf.rs -- all 44 class-1 consts now in the crate diff bit-exact, 0 mismatched. All 16 arms are Some; 0 `None` left. The 512/1024 rows are libaom's untouched uniform initialiser, because av1_get_ext_tx_set_type returns DCT_IDTX/DCTONLY once tx_size_sqr_up >= TX_32X32 and both members are TX_CLASS_2D -- wired anyway for domain completeness (class table-domain-narrowed), and cdf.rs's new eob_pt_class1_rows_cover_every_size test pins exactly that split: the 8 trained rows differ from their 2D sibling, the four 32-point ones are uniform. GATE (new, 8-bit + 10-bit): a_real_aomenc_{,10bit_}inter_sequence_with_a_ class1_chroma_eob_decodes_pixel_exact -- real aomenc, full tx set (--reduced-tx-type-set=0 --enable-flip-idtx=1), --min/max-partition-size=8/16, global/warp/obmc off, 4 frames of a sinusoidal-stripe luma + moving chroma ramp; hard-asserts decode::chroma_eob_class1_hits() moved on the counted attempt and compares every shown frame against ffmpeg. Both green. Ablation (chroma 4x4/8x8 arms set back to None): all 12 attempts fail in both bit depths -- 10 with bogus partition refusals, 1 with no firing. --- crates/ec-av1/src/cdf.rs | 114 ++++++++++++++++++++ crates/ec-av1/src/cdf_state.rs | 73 +++++++++++-- crates/ec-av1/src/decode.rs | 26 +++++ crates/ec-av1/src/stream.rs | 192 +++++++++++++++++++++++++++++++++ scripts/extract-eob-class1.py | 69 ++++++++++++ 5 files changed, 465 insertions(+), 9 deletions(-) create mode 100644 scripts/extract-eob-class1.py diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 7b2a2f58..fec08a57 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -3600,6 +3600,53 @@ mod tests { } } + /// Every `eob_pt` table libaom keeps as `[q][plane][2]` has its class-1 + /// (`tx_class != TX_CLASS_2D`) row here, and it is the *other* row. + /// + /// libaom only ever trained the class-1 row where a 1D tx type can occur: + /// `av1_get_ext_tx_set_type` returns `DCT_IDTX`/`DCTONLY` once + /// `tx_size_sqr_up >= TX_32X32`, so the 512- and 1024-point rows are the + /// untouched uniform initialiser. Pinned so a future table edit cannot + /// quietly narrow the domain back to 2D-only (lane-eobc1 r1; values + /// diffed against `token_cdfs.h` by `scripts/extract-eob-class1.py`). + #[test] + fn eob_pt_class1_rows_cover_every_size() { + fn uniform(row: &[u16]) -> bool { + let n = row.len() - 1; + row[..n].iter().enumerate().all(|(i, &v)| { + let n = n as u32; + v == (((i as u32 + 1) * 32768 + n / 2) / n) as u16 + }) + } + let trained: [(&[u16], &[u16]); 8] = [ + (&EOB_PT_16_LUMA, &EOB_PT_16_LUMA_CLASS1), + (&EOB_PT_16_CHROMA, &EOB_PT_16_CHROMA_CLASS1), + (&EOB_PT_64_LUMA, &EOB_PT_64_LUMA_CLASS1), + (&EOB_PT_64_CHROMA, &EOB_PT_64_CHROMA_CLASS1), + (&EOB_PT_128_CHROMA, &EOB_PT_128_CHROMA_CLASS1), + (&EOB_PT_256_LUMA, &EOB_PT_256_LUMA_CLASS1), + (&EOB_PT_256_CHROMA, &EOB_PT_256_CHROMA_CLASS1), + (&EOB_PT_512_LUMA, &EOB_PT_512_LUMA_CLASS1), + ]; + for (two_d, class1) in trained { + assert_row_shape(class1); + assert_eq!(two_d.len(), class1.len()); + } + for (two_d, class1) in &trained[..7] { + assert_ne!(two_d, class1, "class-1 row is a copy of the 2D row"); + assert!(!uniform(class1), "class-1 row was never trained"); + } + for row in [ + &EOB_PT_512_LUMA_CLASS1[..], + &EOB_PT_512_CHROMA_CLASS1[..], + &EOB_PT_1024_LUMA_CLASS1[..], + &EOB_PT_1024_CHROMA_CLASS1[..], + ] { + assert_row_shape(row); + assert!(uniform(row), "libaom trained a 32-point class-1 row"); + } + } + #[test] fn inter_frame_tables_are_well_formed_cdfs() { for row in INTRA_INTER { @@ -5126,3 +5173,70 @@ pub const EOB_PT_16_CHROMA_CLASS1_Q0: [u16; 6] = [1904, 3354, 7763, 14647, 32768 pub const EOB_PT_16_CHROMA_CLASS1_Q1: [u16; 6] = [2497, 4096, 8866, 16993, 32768, 0]; pub const EOB_PT_16_CHROMA_CLASS1: [u16; 6] = [3192, 5032, 10297, 19755, 32768, 0]; pub const EOB_PT_16_CHROMA_CLASS1_Q3: [u16; 6] = [7297, 10767, 19273, 28194, 32768, 0]; + +/// The class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling of [`EOB_PT_128_CHROMA`] +/// (lane-eobc1 r1, machine-extracted from libaom `token_cdfs.h`). +pub const EOB_PT_128_CHROMA_CLASS1_Q0: [u16; 9] = + [2054, 3472, 5869, 14232, 18242, 20590, 26752, 32768, 0]; +pub const EOB_PT_128_CHROMA_CLASS1_Q1: [u16; 9] = + [2310, 4160, 7471, 14997, 17931, 20768, 30240, 32768, 0]; +pub const EOB_PT_128_CHROMA_CLASS1: [u16; 9] = + [6275, 9889, 14769, 23164, 27988, 30493, 32272, 32768, 0]; +pub const EOB_PT_128_CHROMA_CLASS1_Q3: [u16; 9] = + [9165, 13282, 21150, 30286, 31894, 32571, 32712, 32768, 0]; +/// The class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling of [`EOB_PT_512_LUMA`] +/// (lane-eobc1 r1, machine-extracted from libaom `token_cdfs.h`). +pub const EOB_PT_512_LUMA_CLASS1_Q0: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_LUMA_CLASS1_Q1: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_LUMA_CLASS1: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_LUMA_CLASS1_Q3: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +/// The class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling of [`EOB_PT_512_CHROMA`] +/// (lane-eobc1 r1, machine-extracted from libaom `token_cdfs.h`). +pub const EOB_PT_512_CHROMA_CLASS1_Q0: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_CHROMA_CLASS1_Q1: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_CHROMA_CLASS1: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +pub const EOB_PT_512_CHROMA_CLASS1_Q3: [u16; 11] = [ + 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, +]; +/// The class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling of [`EOB_PT_1024_LUMA`] +/// (lane-eobc1 r1, machine-extracted from libaom `token_cdfs.h`). +pub const EOB_PT_1024_LUMA_CLASS1_Q0: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_LUMA_CLASS1_Q1: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_LUMA_CLASS1: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_LUMA_CLASS1_Q3: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +/// The class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling of [`EOB_PT_1024_CHROMA`] +/// (lane-eobc1 r1, machine-extracted from libaom `token_cdfs.h`). +pub const EOB_PT_1024_CHROMA_CLASS1_Q0: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_CHROMA_CLASS1_Q1: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_CHROMA_CLASS1: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; +pub const EOB_PT_1024_CHROMA_CLASS1_Q3: [u16; 12] = [ + 2979, 5958, 8937, 11916, 14895, 17873, 20852, 23831, 26810, 29789, 32768, 0, +]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index cd587b45..c12828d8 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -225,6 +225,21 @@ pub(crate) struct Cdfs { pub eob_pt_256_chroma_class1: [u16; 10], pub eob_pt_64_chroma_class1: [u16; 8], pub eob_pt_16_chroma_class1: [u16; 6], + /// `128_chroma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see + /// [`Cdfs::eob_pt_16_luma_class1`] (lane-eobc1 r1). + pub eob_pt_128_chroma_class1: [u16; 9], + /// `512_luma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see + /// [`Cdfs::eob_pt_16_luma_class1`] (lane-eobc1 r1). + pub eob_pt_512_luma_class1: [u16; 11], + /// `512_chroma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see + /// [`Cdfs::eob_pt_16_luma_class1`] (lane-eobc1 r1). + pub eob_pt_512_chroma_class1: [u16; 11], + /// `1024_luma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see + /// [`Cdfs::eob_pt_16_luma_class1`] (lane-eobc1 r1). + pub eob_pt_1024_luma_class1: [u16; 12], + /// `1024_chroma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see + /// [`Cdfs::eob_pt_16_luma_class1`] (lane-eobc1 r1). + pub eob_pt_1024_chroma_class1: [u16; 12], /// The end-of-block offset bit of a 16x16 luma transform. pub eob_extra_luma_16: [[u16; 3]; 9], /// The same, for an 8x8 luma transform. @@ -637,6 +652,11 @@ impl Cdfs { reset1(&mut self.eob_pt_256_chroma_class1); reset1(&mut self.eob_pt_64_chroma_class1); reset1(&mut self.eob_pt_16_chroma_class1); + reset1(&mut self.eob_pt_128_chroma_class1); + reset1(&mut self.eob_pt_512_luma_class1); + reset1(&mut self.eob_pt_512_chroma_class1); + reset1(&mut self.eob_pt_1024_luma_class1); + reset1(&mut self.eob_pt_1024_chroma_class1); reset2(&mut self.eob_extra_luma_16); reset2(&mut self.eob_extra_luma_8); reset2(&mut self.eob_extra_luma_4); @@ -886,6 +906,41 @@ impl Cdfs { cdf::EOB_PT_16_CHROMA_CLASS1, cdf::EOB_PT_16_CHROMA_CLASS1_Q3, ), + eob_pt_128_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_128_CHROMA_CLASS1_Q0, + cdf::EOB_PT_128_CHROMA_CLASS1_Q1, + cdf::EOB_PT_128_CHROMA_CLASS1, + cdf::EOB_PT_128_CHROMA_CLASS1_Q3, + ), + eob_pt_512_luma_class1: pick( + q_ctx, + cdf::EOB_PT_512_LUMA_CLASS1_Q0, + cdf::EOB_PT_512_LUMA_CLASS1_Q1, + cdf::EOB_PT_512_LUMA_CLASS1, + cdf::EOB_PT_512_LUMA_CLASS1_Q3, + ), + eob_pt_512_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_512_CHROMA_CLASS1_Q0, + cdf::EOB_PT_512_CHROMA_CLASS1_Q1, + cdf::EOB_PT_512_CHROMA_CLASS1, + cdf::EOB_PT_512_CHROMA_CLASS1_Q3, + ), + eob_pt_1024_luma_class1: pick( + q_ctx, + cdf::EOB_PT_1024_LUMA_CLASS1_Q0, + cdf::EOB_PT_1024_LUMA_CLASS1_Q1, + cdf::EOB_PT_1024_LUMA_CLASS1, + cdf::EOB_PT_1024_LUMA_CLASS1_Q3, + ), + eob_pt_1024_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_1024_CHROMA_CLASS1_Q0, + cdf::EOB_PT_1024_CHROMA_CLASS1_Q1, + cdf::EOB_PT_1024_CHROMA_CLASS1, + cdf::EOB_PT_1024_CHROMA_CLASS1_Q3, + ), eob_extra_luma_16: pick( q_ctx, cdf::EOB_EXTRA_LUMA_16_Q0, @@ -1283,7 +1338,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_1024_luma_class1), }, // lane-comppin r4 (REVERTED, see r4's own commit message): // `Some(inter_tx_type_32)` looked wrong against aomdec's 32x32 @@ -1307,7 +1362,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_32.as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_1024_luma_class1), }, TxbSet::Luma64 => TxbTables { side: 32, @@ -1319,7 +1374,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_1024_luma_class1), }, TxbSet::Luma16 => TxbTables { side: 16, @@ -1403,7 +1458,7 @@ impl Cdfs { br: &mut self.br_luma_4, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_4.as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1), }, TxbSet::Luma4InterSet1 => TxbTables { side: 4, @@ -1439,7 +1494,7 @@ impl Cdfs { br: &mut self.br_luma_8, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_8.as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), }, TxbSet::Luma8InterSet1 => TxbTables { side: 8, @@ -1499,7 +1554,7 @@ impl Cdfs { br: &mut self.br_chroma_32, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_1024_chroma_class1), }, TxbSet::LumaRect32x16 => TxbTables { side: 32, @@ -1511,7 +1566,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_512_luma_class1), }, TxbSet::ChromaRect16x8 => TxbTables { side: 16, @@ -1523,7 +1578,7 @@ impl Cdfs { br: &mut self.br_chroma_16, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_128_chroma_class1), }, TxbSet::ChromaRect32x16 => TxbTables { side: 32, @@ -1535,7 +1590,7 @@ impl Cdfs { br: &mut self.br_chroma_32, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_512_chroma_class1), }, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b76ea5b2..9cd1607e 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1583,6 +1583,30 @@ pub(crate) fn angle_delta_hits() -> usize { pub(crate) fn tx_class1_hits() -> usize { TX_CLASS1_HITS.with(|c| c.get()) } + +// How many CHROMA coefficient blocks read their `eob_pt` from the class-1 +// (`TX_CLASS_HORIZ`/`VERT`) row, across every call on the current thread. +// Chroma never codes a `tx_type` symbol -- it inherits the colocated luma +// type verbatim (`av1_get_tx_type`, `blockd.h:1291`) -- so this fires only on +// an inter leaf whose luma picked a 1D type, the path lane-eobc1 wired +// (`eob_pt_{16,64,128,256}_chroma_class1`). +thread_local! { + static CHROMA_EOB_CLASS1_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`CHROMA_EOB_CLASS1_HITS`]. +pub(crate) fn chroma_eob_class1_hits() -> usize { + CHROMA_EOB_CLASS1_HITS.with(|c| c.get()) +} + +/// Counts one [`CHROMA_EOB_CLASS1_HITS`] when `plane_idx`'s inherited +/// `tx_type` put this block's `eob_pt` on the class-1 row. +fn note_chroma_class1(plane_idx: usize, tx_type: TxType) { + if plane_idx > 0 && TxClass::of(tx_type) != TxClass::TwoD { + CHROMA_EOB_CLASS1_HITS.with(|c| c.set(c.get() + 1)); + } +} const SUB_MI: u32 = 4; const MI: usize = 4; const SB: usize = 64; @@ -5861,6 +5885,7 @@ fn read_plane( default_tx_type, None, )?; + note_chroma_class1(plane_idx, tx_type); // A 64x64 luma block's transform covers the whole 64x64 area, but only its // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` @@ -9932,6 +9957,7 @@ fn read_inter_plane( default_tx_type, None, )?; + note_chroma_class1(plane_idx, tx_type); // lane-inter8 r1: a 64-point transform covers the whole 64x64 area but // codes only its top-left 32x32 of frequencies (spec 5.11.40) -- the // caller hands the 32-point `scan` there, and the coded corner goes into diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 402bf861..8013a717 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -4515,6 +4515,198 @@ mod tests { ); } + /// lane-eobc1 r1: an INTER sequence whose CHROMA coefficient blocks come + /// off the class-1 (`TX_CLASS_HORIZ`/`VERT`) `eob_pt` row. Chroma codes + /// no `tx_type` symbol of its own -- it inherits the colocated luma type + /// (`av1_get_tx_type`, `blockd.h:1291`) -- so an 8x8 inter leaf whose + /// luma picks `H_*`/`V_*` puts its 4x4 chroma on a CDF row this crate + /// wired for only some sizes until this round (`eob_pt_class1: None` in + /// 9 of 16 `TxbSet` arms, chroma 4x4/8x8 among them). + /// + /// The full tx set stays on (no `--reduced-tx-type-set=1`) and + /// `--enable-flip-idtx=1` is asked for explicitly; the sinusoidal-stripe + /// luma of the `tx_class1` key-frame gate is what actually resolves 1D + /// types, and the temporally-moving chroma ramp keeps chroma out of + /// `all_zero` so it has an `eob` to read at all. Attempts sweep cq and + /// stripe period; a run counts only when + /// [`decode::chroma_eob_class1_hits`] moved, and every shown frame is + /// compared pixel-exact vs ffmpeg (a decode error is a FAILURE, never a + /// SKIP -- class gate-skips-on-its-own-failure). + fn chroma_class1_eob_gate(name: &str, ten_bit: bool) { + if !have_ffmpeg() { + eprintln!("SKIP {name}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {name}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height, frame_count) = (64usize, 64usize, 4usize); + let mut refusals = Vec::new(); + for attempt in 0..12u32 { + let cq = 28 + (attempt % 4) * 10; + let period = [4u32, 6, 8][(attempt as usize / 4) % 3]; + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &format!("testsrc2=size={width}x{height}:rate=25"), + "-vf", + &format!( + "geq=lum='128+80*sin(2*PI*Y/{period})':cb='128+60*sin(2*PI*(X+8*T)/{period})':cr='128+60*sin(2*PI*(Y+8*T)/{period})',noise=alls=6:allf=t:all_seed=7" + ), + "-t", + "0.16", + "-pix_fmt", + if ten_bit { "yuv420p10le" } else { "yuv420p" }, + "-strict", + "-1", + "-f", + "yuv4mpegpipe", + "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{name}: ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let mut args: Vec = [ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cpu-used=0", + "--lag-in-frames=0", + "--auto-alt-ref=0", + "--kf-max-dist=1000", + "--error-resilient=1", + "--enable-flip-idtx=1", + "--reduced-tx-type-set=0", + "--min-partition-size=8", + "--max-partition-size=16", + // Motion tools this decoder refuses by name, kept off so a + // refusal here can only be about the coefficient path. + "--enable-global-motion=0", + "--enable-warped-motion=0", + "--enable-obmc=0", + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--loopfilter-control=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + "--obu", + "-o", + "-", + "-", + ] + .iter() + .map(|s| (*s).to_string()) + .collect(); + args.insert(1, format!("--cq-level={cq}")); + if ten_bit { + args.insert(1, "--input-bit-depth=10".to_string()); + args.insert(1, "--bit-depth=10".to_string()); + } + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "{name}: aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = decode::chroma_eob_class1_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + // A named refusal ("unsupported: ...") is another lane's arm + // and moves this attempt on; anything else is a real decode + // failure and fails the gate on the spot. + Err(e) if e.to_string().contains("unsupported") => { + refusals.push(format!("cq={cq} period={period}: {e}")); + continue; + } + Err(e) => { + let pin = std::env::temp_dir().join(format!("{name}-refused.obu")); + let _ = std::fs::write(&pin, &stream); + panic!( + "{name}: decode_stream failed on a real aomenc stream (cq={cq} period={period}): {e} (pinned at {})", + pin.display() + ); + } + }; + if decode::chroma_eob_class1_hits() == before { + refusals.push(format!( + "cq={cq} period={period}: decoded, but no chroma block read a class-1 eob_pt row" + )); + continue; + } + let ffmpeg_frames = if ten_bit { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + assert_eq!(frames.len(), frame_count, "{name}: frame count"); + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + if got.y != want.y || got.u != want.u || got.v != want.v { + let pin = std::env::temp_dir().join(format!("{name}-mismatch.obu")); + let _ = std::fs::write(&pin, &stream); + panic!( + "{name}: frame {i} mismatch vs ffmpeg (cq={cq} period={period}) -- stream pinned at {}", + pin.display() + ); + } + } + return; + } + panic!("{name}: no attempt fired a class-1 chroma eob: {refusals:?}"); + } + + #[test] + fn a_real_aomenc_inter_sequence_with_a_class1_chroma_eob_decodes_pixel_exact() { + chroma_class1_eob_gate( + "a_real_aomenc_inter_sequence_with_a_class1_chroma_eob_decodes_pixel_exact", + false, + ); + } + + #[test] + fn a_real_aomenc_10bit_inter_sequence_with_a_class1_chroma_eob_decodes_pixel_exact() { + chroma_class1_eob_gate( + "a_real_aomenc_10bit_inter_sequence_with_a_class1_chroma_eob_decodes_pixel_exact", + true, + ); + } + #[test] fn a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact() { if !have_ffmpeg() { diff --git a/scripts/extract-eob-class1.py b/scripts/extract-eob-class1.py new file mode 100644 index 00000000..4a3b8e39 --- /dev/null +++ b/scripts/extract-eob-class1.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +"""Extract libaom's eob_multi CDF rows and emit / verify our Rust consts. + +`av1_default_eob_multi{16,32,64,128,256,512,1024}_cdfs` are +`[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(n)]`; the third index is +`eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1`. This prints the +class-1 (index 1) rows as `EOB_PT___CLASS1[_Qk]` consts and, with +`--check `, diffs every const that file already defines. + +Usage: extract-eob-class1.py [--check ] +""" + +import re +import sys + +SIZES = [16, 32, 64, 128, 256, 512, 1024] +QSUF = ["_Q0", "_Q1", "", "_Q3"] + + +def parse(src: str, size: int): + m = re.search(rf"av1_default_eob_multi{size}_cdfs\b(.*?)\n\s*\n", src, re.S) + if not m: + sys.exit(f"table eob_multi{size} not found") + rows = [ + [int(x) for x in r.split(",")] + for r in re.findall(r"AOM_CDF\d+\(([^)]*)\)", m.group(1)) + ] + # flat order: q-major, then plane, then class + if len(rows) != 4 * 2 * 2: + sys.exit(f"eob_multi{size}: expected 16 rows, got {len(rows)}") + return rows + + +def consts(src): + out = {} + for size in SIZES: + rows = parse(src, size) + for q in range(4): + for p, plane in enumerate(("LUMA", "CHROMA")): + row = rows[q * 4 + p * 2 + 1] + out[f"EOB_PT_{size}_{plane}_CLASS1{QSUF[q]}"] = row + [32768, 0] + return out + + +def main(): + src = open(sys.argv[1]).read() + want = consts(src) + if "--check" in sys.argv: + rs = open(sys.argv[sys.argv.index("--check") + 1]).read() + seen = bad = 0 + for name, vals in want.items(): + m = re.search(rf"const {name}: \[u16; \d+\] =\s*(\[[^]]*\]);", rs) + if not m: + continue + seen += 1 + got = [int(x) for x in re.findall(r"\d+", m.group(1))] + if got != vals: + bad += 1 + print(f"MISMATCH {name}\n ours {got}\n aom {vals}") + print(f"checked {seen}/{len(want)} consts, {bad} mismatched") + missing = [n for n in want if f"const {n}:" not in rs] + print(f"missing {len(missing)}: {' '.join(sorted(missing))}") + sys.exit(1 if bad else 0) + for name, vals in want.items(): + body = ", ".join(str(v) for v in vals) + print(f"pub const {name}: [u16; {len(vals)}] = [{body}];") + + +main()