diff --git a/crates/ec-av1/examples/probe_txsel.rs b/crates/ec-av1/examples/probe_txsel.rs deleted file mode 100644 index f0cfdb2e..00000000 --- a/crates/ec-av1/examples/probe_txsel.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::io::Write; -use std::process::{Command, Stdio}; - -fn main() { - let path = std::env::args() - .nth(1) - .unwrap_or_else(|| "/tmp/mb.obu".into()); - let stream = std::fs::read(&path).expect("read stream"); - let frames = match ec_av1::stream::decode_stream(&stream) { - Ok(f) => f, - Err(e) => { - eprintln!("decode error: {e}"); - std::process::exit(1); - } - }; - let (width, height) = (64usize, 64usize); - let mut child = Command::new("ffmpeg") - .args([ - "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", - ]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .expect("ffmpeg spawn"); - child - .stdin - .take() - .unwrap() - .write_all(&stream) - .expect("write stream"); - let out = child.wait_with_output().expect("ffmpeg run"); - if !out.status.success() { - eprintln!("ffmpeg refused: {}", String::from_utf8_lossy(&out.stderr)); - std::process::exit(1); - } - let luma = width * height; - let chroma = luma / 4; - let y_ref = &out.stdout[..luma]; - let u_ref = &out.stdout[luma..luma + chroma]; - let v_ref = &out.stdout[luma + chroma..luma + 2 * chroma]; - - let pic = &frames[0]; - let y_match = pic.y == y_ref; - let u_match = pic.u == u_ref; - let v_match = pic.v == v_ref; - println!("decoded ok, y_match={y_match} u_match={u_match} v_match={v_match}"); - if !y_match { - for i in 0..luma { - if pic.y[i] != y_ref[i] { - println!( - "first luma mismatch at idx {i} (row {} col {}): ours={} ref={}", - i / width, - i % width, - pic.y[i], - y_ref[i] - ); - break; - } - } - } - std::process::exit(if y_match && u_match && v_match { 0 } else { 2 }); -} diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 87184f0f..9d4f5b61 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -41,6 +41,10 @@ pub(crate) enum TxbSet { /// block (lane-av1-rect). An `is_inter` leaf reads /// [`Luma8Inter`](TxbSet::Luma8Inter) instead. Luma8, + /// [`Luma8`](TxbSet::Luma8)'s `reduced_tx_set: false` counterpart (round + /// 8): the same coefficient tables, but the seven-symbol `TX_SET_INTRA_1` + /// `tx_type` table rather than `Luma8`'s five-symbol reduced one. + Luma8Set1, /// The 8x8 luma transform of an `is_inter` 8x8 leaf under a straddling /// 16x16 block (lane-av1inter8): the same coefficient tables /// [`Luma8`](TxbSet::Luma8) reads, but `get_tx_set` (spec 5.11.48) @@ -225,6 +229,13 @@ pub(crate) struct Cdfs { pub intra_tx_type_16: [[u16; 6]; 13], /// The transform type of an 8x8 intra luma transform, by luma mode. pub intra_tx_type_8: [[u16; 6]; 13], + /// The transform type of an 8x8 intra luma transform, by luma mode, when + /// `reduced_tx_set` is false (spec `TX_SET_INTRA_1`, seven types) -- + /// [`Self::intra_tx_type_8`]'s five-type table is only the + /// `reduced_tx_set` row (round 8: a real `aomenc` stream with + /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's + /// wrong-length CDF). + pub intra_tx_type_8_set1: [[u16; 8]; 13], /// The transform type of an `is_inter` 32x32 luma transform (spec /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. @@ -430,6 +441,7 @@ impl Cdfs { reset2(&mut self.dc_sign_luma); reset2(&mut self.intra_tx_type_16); reset2(&mut self.intra_tx_type_8); + reset2(&mut self.intra_tx_type_8_set1); reset1(&mut self.inter_tx_type_32); reset1(&mut self.inter_tx_type_16); reset1(&mut self.inter_tx_type_8); @@ -795,6 +807,7 @@ impl Cdfs { dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8, + intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8, @@ -886,6 +899,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()), }, + TxbSet::Luma8Set1 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_64_luma, + eob_extra: &mut self.eob_extra_luma_8, + base: &mut self.base_luma_8, + base_eob: &mut self.base_eob_luma_8, + br: &mut self.br_luma_8, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()), + }, TxbSet::Luma8Inter => TxbTables { side: 8, txb_skip: &mut self.txb_skip_luma_8, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d813310e..de893e05 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -288,13 +288,20 @@ fn read_coeffs( } let mut tx_type = TxType::DctDct; if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { + // The CDF row's own width names its set: 7 symbols (8 slots) is + // `TX_SET_INTRA_1`, 5 (6 slots) the reduced `TX_SET_INTRA_2` (the + // inter sets we read share set 2's symbol order). + let set1 = tx_type_cdf.len() == 8; let t = dec.symbol(tx_type_cdf); if trace { - eprintln!("TRACE tx_type value={t}"); + eprintln!("TRACE tx_type value={t} set1={set1}"); } - tx_type = TxType::from_symbol(t).ok_or_else(|| { - unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")) - })?; + tx_type = if set1 { + TxType::from_symbol_set1(t) + } else { + TxType::from_symbol(t) + } + .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; } let eob = read_eob(dec, coding); @@ -1165,6 +1172,7 @@ fn decode_block( scan16: &[u16], scan8: &[u16], tx_select: bool, + reduced_tx_set: bool, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); @@ -1196,7 +1204,7 @@ fn decode_block( (side, luma_tx) }; let luma_set = if tx_select { - txbset_for(logical_tx) + txbset_for(logical_tx, reduced_tx_set) } else { luma_set }; @@ -1433,6 +1441,7 @@ fn decode_leaf8( base_q_idx: u8, enable_filter_intra: bool, tx_select: bool, + reduced_tx_set: bool, ) -> Result { let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; @@ -1504,7 +1513,11 @@ fn decode_leaf8( luma_grid = read_plane( dec, cdfs, - TxbSet::Luma8, + if reduced_tx_set { + TxbSet::Luma8 + } else { + TxbSet::Luma8Set1 + }, scans.0, 0, around[0], @@ -1856,13 +1869,19 @@ fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [ /// The [`TxbSet`] a resolved luma transform side reads its coefficient tables /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching -/// set. -fn txbset_for(tx_px: usize) -> TxbSet { +/// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at +/// `TX_8X8`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` regardless of +/// the flag (`av1_get_ext_tx_set_type`'s `tx_size_sqr == TX_16X16` branch +/// ignores `use_reduced_set` once it is past the `TX_32X32`/reduced-set +/// checks), so only the 8x8 case has a second table +/// ([`TxbSet::Luma8Set1`]). +fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { match tx_px { 64 => TxbSet::Luma64, 32 => TxbSet::Luma32, 16 => TxbSet::Luma16, - 8 => TxbSet::Luma8, + 8 if reduced_tx_set => TxbSet::Luma8, + 8 => TxbSet::Luma8Set1, _ => unreachable!("read_tx_size never resolves a block's own side to anything else"), } } @@ -2408,6 +2427,7 @@ pub fn decode_key_frame_tile( cdef: &CdefParams, loop_filter: &LoopFilterParams, tx_select: bool, + reduced_tx_set: bool, ) -> Result { decode_key_frame_tile_with_cdfs( data, @@ -2421,6 +2441,7 @@ pub fn decode_key_frame_tile( loop_filter, None, tx_select, + reduced_tx_set, ) .map(|(picture, _)| picture) } @@ -2445,6 +2466,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( loop_filter: &LoopFilterParams, initial_cdfs: Option, tx_select: bool, + reduced_tx_set: bool, ) -> Result<(Picture, Cdfs)> { if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); @@ -2554,6 +2576,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan16, &scan8, tx_select, + reduced_tx_set, )?; } PARTITION_SPLIT => { @@ -2615,6 +2638,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan16, &scan8, tx_select, + reduced_tx_set, )?; } PARTITION_SPLIT => { @@ -2662,6 +2686,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan16, &scan8, tx_select, + reduced_tx_set, )?; continue; } @@ -2706,6 +2731,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( base_q_idx, enable_filter_intra, tx_select, + reduced_tx_set, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -2769,6 +2795,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( base_q_idx, enable_filter_intra, tx_select, + reduced_tx_set, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -4168,7 +4195,8 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false + false, + true, ) .is_err() ); @@ -4210,6 +4238,7 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + true, ) .unwrap(); assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); @@ -4292,6 +4321,7 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + true, ) .unwrap(); assert!( @@ -4476,6 +4506,7 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + true, ) .unwrap(); assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); @@ -4547,6 +4578,7 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + true, ) .unwrap(); assert_eq!( @@ -4821,6 +4853,7 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + true, ) .unwrap(); assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 2fc5e8cd..ec9afea8 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -215,6 +215,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { &header.loop_filter, initial_cdfs, header.tx_mode == TxMode::Select, + header.reduced_tx_set, )? } else { let last_slot = header.ref_frame_idx[0] as usize; @@ -341,6 +342,8 @@ mod tests { &ec_av1_syntax::CdefParams::default(), &ec_av1_syntax::LoopFilterParams::default(), false, + // Our own encoder always writes `reduced_tx_set: true`. + true, ) .unwrap(); let via_stream = decode_stream(&encoded.stream).unwrap(); @@ -1036,9 +1039,13 @@ mod tests { // search to prefer a split transform over the block's own full // size -- `mandelbrot`'s fractal boundary gives every attempt // (a different `start_scale`) sharp, varied-frequency edges. + // start_scale 5.0 is the proven first try (decodes pixel-exact + // AND reads the seven-symbol set-1 tx_type rows on TX8 TUs); + // later attempts walk down for variety when aomenc's RD flips + // elsewhere. let source = format!( "mandelbrot=size=64x64:start_scale={}:rate=25", - 3.0 - f64::from(attempt) * 0.06 + 5.0 - f64::from(attempt) * 0.06 ); let y4m = Command::new("ffmpeg") .args([ @@ -1084,6 +1091,13 @@ mod tests { "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", + // Every non-DC intra tool off: directional chroma and + // angle deltas are separate round-2 gaps, and DC-only + // residual is exactly what gives the RD search a reason + // to split a transform. + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-cdef=0", // Loop restoration is a named refusal (aomenc's RD picks @@ -1091,9 +1105,14 @@ mod tests { // whose test targets a different surface. "--enable-restoration=0", "--max-partition-size=32", + // 16x16 blocks are what split into the TX8 TUs whose + // set-1 tx_type rows this gate exists to prove; some + // scales resolve depth 2 (TX4, a named refusal) and the + // attempt loop just moves on. + "--min-partition-size=16", "--enable-palette=0", "--enable-intrabc=0", - "--enable-cfl-intra=1", + "--enable-cfl-intra=0", "--obu", "-o", "-", diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index ae89866c..7ad050ec 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -314,6 +314,12 @@ pub enum TxType { AdstDct, /// DCT down the column axis, ADST across the row axis. DctAdst, + /// DCT down the column axis, identity across the row axis + /// (`V_DCT` -- `TX_SET_INTRA_1` only). + VDct, + /// Identity down the column axis, DCT across the row axis + /// (`H_DCT` -- `TX_SET_INTRA_1` only). + HDct, } impl TxType { @@ -329,6 +335,24 @@ impl TxType { } } + /// The inverse of `Tx_Type_Intra_Inv_Set1`'s CDF symbol order + /// (`av1_ext_tx_inv[EXT_TX_SET_DTT4_IDTX_1DDCT]`, entropymode.h: + /// `{9, 0, 10, 11, 3, 1, 2}` = IDTX, DCT_DCT, V_DCT, H_DCT, ADST_ADST, + /// ADST_DCT, DCT_ADST) -- the seven-type set a `reduced_tx_set == 0` + /// frame's small intra transforms read from. + pub fn from_symbol_set1(t: usize) -> Option { + match t { + 0 => Some(Self::Idtx), + 1 => Some(Self::DctDct), + 2 => Some(Self::VDct), + 3 => Some(Self::HDct), + 4 => Some(Self::AdstAdst), + 5 => Some(Self::AdstDct), + 6 => Some(Self::DctAdst), + _ => None, + } + } + /// `(row, col)`: the transform `inverse_transform_2d_typed`'s row pass /// (`htx_tab`, spec/libaom "horizontal") and column pass (`vtx_tab`, /// "vertical") each run. Read off libaom's `av1_inv_txfm2d.c` `vtx_tab`/ @@ -343,6 +367,9 @@ impl TxType { Self::AdstAdst => (Adst, Adst), Self::AdstDct => (Dct, Adst), Self::DctAdst => (Adst, Dct), + // `av1_inv_txfm2d.c`: V_DCT vtx=DCT htx=IDTX, H_DCT the mirror. + Self::VDct => (Identity, Dct), + Self::HDct => (Dct, Identity), } } }