forge
Explore
Log in

Don't you dare go Hollow

The tile writer coded every transform unit as TX_CLASS_2D

Browse files at this commit

ae80b331122c18a58ca717075d24268f365e46c9Ahmet Kaan GümüşParent: 15ab38b
  • fix
    • the tile writer coded every transform unit as TX_CLASS_2D
Show full message
fix(av1): the tile writer coded every transform unit as TX_CLASS_2D

A V_DCT/H_DCT unit is TX_CLASS_VERT/TX_CLASS_HORIZ for the decoder: its
own Mrow/Mcol scan, its own eob_pt row (eob_pt_class1) and its own
nz-map/base-range context offsets. The writer had none of that, so the
first 1-D type any search offered -- the wider reduced_tx_set = 0
alphabets -- desynced the tile from the encoder's own trial decode,
surfacing as "a reference frame selected with no picture at this frame's
own ref_frame_idx slot" (class refusal-from-own-desync).

write_coeffs now resolves the class off tx_type exactly as
decode::TxClass::of does, and takes the class scan, the class-1 eob row
and class-aware base_ctx/br_ctx from it; the pricer goes through the same
function, so a priced type is priced in its own alphabet and class.

The wide-alphabet witness is un-ignored: preset 0 codes 1820 V_DCT and
2435 H_DCT intra units and both decoders read the clip sample-exact.
crates/ec-av1/src/decode.rsmodified+4 -4
@@ -5491,7 +5491,7 @@ fn default_scan_gen(side: usize) -> Vec<u16> {
5491 /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely5491 /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely
5492 /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk5492 /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk
5493 /// transposed (column 0 down every row, then column 1, ...).5493 /// transposed (column 0 down every row, then column 1, ...).
5494-fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> {5494+pub(crate) fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> {
5495 class_scan_table_wh(side, side, class)5495 class_scan_table_wh(side, side, class)
5496 }5496 }
5497 5497
@@ -5538,14 +5538,14 @@ fn neighbour(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 {
5538 /// `txb_common.h`): everything but the two lone-axis-identity types this5538 /// `txb_common.h`): everything but the two lone-axis-identity types this
5539 /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`.5539 /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`.
5540 #[derive(Clone, Copy, PartialEq, Eq)]5540 #[derive(Clone, Copy, PartialEq, Eq)]
5541-enum TxClass {5541+pub(crate) enum TxClass {
5542 TwoD,5542 TwoD,
5543 Horiz,5543 Horiz,
5544 Vert,5544 Vert,
5545 }5545 }
5546 5546
5547 impl TxClass {5547 impl TxClass {
5548- fn of(tx_type: TxType) -> Self {5548+ pub(crate) fn of(tx_type: TxType) -> Self {
5549 match tx_type {5549 match tx_type {
5550 // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`)5550 // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`)
5551 // key on axis alone -- FLIPADST is still ADST for this purpose.5551 // key on axis alone -- FLIPADST is still ADST for this purpose.
@@ -5561,7 +5561,7 @@ impl TxClass {
5561 /// `col` for horiz, `row` for vert) that places their contexts past the 2D5561 /// `col` for horiz, `row` for vert) that places their contexts past the 2D
5562 /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total5562 /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total
5563 /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36.5563 /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36.
5564-fn nz_map_ctx_offset_1d(i: usize) -> usize {5564+pub(crate) fn nz_map_ctx_offset_1d(i: usize) -> usize {
5565 match i {5565 match i {
5566 0 => 26,5566 0 => 26,
5567 1 => 31,5567 1 => 31,
crates/ec-av1/src/encode.rsmodified+15 -11
@@ -472,17 +472,21 @@ fn inter_tx_type_search() -> InterTxSearch {
472 472
473 /// The inter luma [`TxbSet`] a `tx`-sided transform unit reads, `None` for the473 /// The inter luma [`TxbSet`] a `tx`-sided transform unit reads, `None` for the
474 /// 64-point one (it codes no `tx_type` symbol).474 /// 64-point one (it codes no `tx_type` symbol).
475-/// lane-txi: the INTER half of the widening is deliberately NOT applied475+/// lane-txw: the INTER half of the widening is still NOT applied here, and
476-/// here. A `reduced_tx_set = 0` frame's writer still codes its inter luma476+/// the reason lane-txi recorded (the `refs` lane's DPB bookkeeping) was
477-/// `tx_type` into the twelve/sixteen-symbol alphabet (the frame bit widens477+/// WRONG -- the refusal is `refusal-from-own-desync` twice over. Half of it
478-/// every luma set at once, spec `get_tx_set`), but the SEARCH keeps offering478+/// was this writer coding a `V_DCT`/`H_DCT` unit as `TX_CLASS_2D`
479-/// the two-type list: with the wider inter candidates priced, some block's479+/// (`tile::write_coeffs`, fixed by this lane, which is what unblocked the
480-/// winner selects a reference slot this encoder holds no picture in480+/// intra half). What remains is the CHROMA inheritance: an inter block's
481-/// ("a reference frame selected with no picture at this frame's own481+/// chroma transform type is the luma type reduced to the chroma size's set
482-/// ref_frame_idx slot for it", refused out of the encoder's own MC), which is482+/// (`decode::reduce_inherited_chroma_tx_type`), and (a) the encoder never
483-/// the `refs` lane's DPB bookkeeping (`encoder.rs` maps LAST2/LAST3 onto483+/// sets `FrameCtx::reduced_tx_set_inter`, so [`recode_inter_chroma`] reduces
484-/// LAST's slot), not a transform defect -- measured by bisection: the intra484+/// against the REDUCED allowance while a `reduced_tx_set = 0` frame's
485-/// half alone passes every witness, the inter half alone refuses.485+/// decoder allows the full one, and (b) `tile::write_block_planes` codes
486+/// every chroma plane as `DCT_DCT`, so an inherited 1-D type desyncs the
487+/// chroma exactly as luma did. Both are deferred with the wider inter search
488+/// itself; the intra half needs neither (an intra block's chroma type comes
489+/// off its own UV mode, never off luma).
486 fn inter_luma_set(tx: usize) -> Option<TxbSet> {490 fn inter_luma_set(tx: usize) -> Option<TxbSet> {
487 Some(match tx {491 Some(match tx {
488 4 => TxbSet::Luma4Inter,492 4 => TxbSet::Luma4Inter,
crates/ec-av1/src/encoder.rsmodified+1 -3
@@ -3822,9 +3822,7 @@ mod tests {
3822 /// (class `wrong-alphabet-same-value`: a disagreement here changes no3822 /// (class `wrong-alphabet-same-value`: a disagreement here changes no
3823 /// symbol VALUE and desyncs the arithmetic coder anyway).3823 /// symbol VALUE and desyncs the arithmetic coder anyway).
3824 #[test]3824 #[test]
3825- #[ignore = "reduced_tx_set = 0 desyncs the encoder's own trial decode when the \3825+ #[ignore = "sets process-global search levers: run it alone"]
3826- seven-type intra search is on -- bisected in lanes/txi.report.md; \
3827- the lever is off at every preset until that is closed"]
3828 fn a_wide_tx_set_clip_codes_the_new_alphabets_both_decoders_read_exactly() {3826 fn a_wide_tx_set_clip_codes_the_new_alphabets_both_decoders_read_exactly() {
3829 // Every lever below is process-global: the same exclusive lock the3827 // Every lever below is process-global: the same exclusive lock the
3830 // presets take.3828 // presets take.
crates/ec-av1/src/tile.rsmodified+108 -9
@@ -4724,6 +4724,29 @@ fn scan_of(side: usize) -> &'static Vec<u16> {
4724 }4724 }
4725 }4725 }
4726 4726
4727+/// [`scan_of`]'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling (lane-txw): the
4728+/// `Mrow_Scan`/`Mcol_Scan` a `V_DCT`/`H_DCT` unit is coded in, built off the
4729+/// DECODER's own table so the two cannot drift, cached per size and class.
4730+fn class_scan_of(side: usize, class: crate::decode::TxClass) -> &'static [u16] {
4731+ static SCANS: LazyLock<[[Vec<u16>; 2]; 4]> = LazyLock::new(|| {
4732+ use crate::decode::{TxClass, class_scan_table};
4733+ [TX4, TX8, TX16, TX32].map(|side| {
4734+ [
4735+ class_scan_table(side, TxClass::Horiz),
4736+ class_scan_table(side, TxClass::Vert),
4737+ ]
4738+ })
4739+ });
4740+ let size = match side {
4741+ TX4 => 0,
4742+ TX8 => 1,
4743+ TX16 => 2,
4744+ _ => 3,
4745+ };
4746+ let idx = usize::from(class == crate::decode::TxClass::Vert);
4747+ &SCANS[size][idx]
4748+}
4749+
4727 /// [`coeff_bits_typed`] at `DCT_DCT`, the only type the test-only pricers4750 /// [`coeff_bits_typed`] at `DCT_DCT`, the only type the test-only pricers
4728 /// ([`predicted_coeff_bits`]) model.4751 /// ([`predicted_coeff_bits`]) model.
4729 #[cfg(test)]4752 #[cfg(test)]
@@ -5233,6 +5256,22 @@ fn write_coeffs(
5233 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));5256 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));
5234 }5257 }
5235 let side = coding.side;5258 let side = coding.side;
5259+ // lane-txw ROOT CAUSE: this writer used to code every transform unit as
5260+ // `TX_CLASS_2D` -- default zigzag scan, the 2D `eob_pt` table and the 2D
5261+ // neighbour taps -- while the decoder resolves `V_DCT`/`H_DCT` to
5262+ // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`decode::TxClass::of`), which read
5263+ // their own scan (`class_scan_table`), their own `eob_pt` row
5264+ // (`eob_pt_class1`) and their own context offsets. So the first 1-D type
5265+ // the search ever offered (the wider `reduced_tx_set == 0` alphabets)
5266+ // desynced the tile from its own trial decode. The class is a property of
5267+ // the type alone, so it is resolved here, once per unit, exactly as the
5268+ // reader does it.
5269+ let class = crate::decode::TxClass::of(tx_type);
5270+ let scan: &[u16] = if class == crate::decode::TxClass::TwoD {
5271+ scan
5272+ } else {
5273+ class_scan_of(side, class)
5274+ };
5236 // The scan-order search reads the grid in scan order, one dependent load5275 // The scan-order search reads the grid in scan order, one dependent load
5237 // per position, and an all-zero block makes it walk every one of them --5276 // per position, and an all-zero block makes it walk every one of them --
5238 // which the pricer does for candidate after candidate. A straight-line5277 // which the pricer does for candidate after candidate. A straight-line
@@ -5280,7 +5319,7 @@ fn write_coeffs(
5280 }5319 }
5281 }5320 }
5282 5321
5283- write_eob(enc, coding, eob, plane);5322+ write_eob(enc, coding, eob, plane, class);
5284 5323
5285 // lane-av1speed3: `side` is 4, 8, 16 or 32, but it is a runtime value --5324 // lane-av1speed3: `side` is 4, 8, 16 or 32, but it is a runtime value --
5286 // `pos / side` compiles to a real integer division, once per coefficient,5325 // `pos / side` compiles to a real integer division, once per coefficient,
@@ -5305,7 +5344,7 @@ fn write_coeffs(
5305 });5344 });
5306 }5345 }
5307 } else {5346 } else {
5308- let ctx = base_ctx(grid, side, row, col);5347+ let ctx = base_ctx(grid, side, row, col, class);
5309 let sym = level.min(NUM_BASE_LEVELS + 1) as usize;5348 let sym = level.min(NUM_BASE_LEVELS + 1) as usize;
5310 enc.symbol(sym, &mut coding.base[ctx]);5349 enc.symbol(sym, &mut coding.base[ctx]);
5311 if plane.is_some() {5350 if plane.is_some() {
@@ -5318,7 +5357,7 @@ fn write_coeffs(
5318 }5357 }
5319 }5358 }
5320 if level > NUM_BASE_LEVELS {5359 if level > NUM_BASE_LEVELS {
5321- let ctx = br_ctx(grid, side, row, col);5360+ let ctx = br_ctx(grid, side, row, col, class);
5322 let mut remaining = level - (NUM_BASE_LEVELS + 1);5361 let mut remaining = level - (NUM_BASE_LEVELS + 1);
5323 let mut sent = 0;5362 let mut sent = 0;
5324 while sent < COEFF_BASE_RANGE {5363 while sent < COEFF_BASE_RANGE {
@@ -5379,7 +5418,13 @@ fn write_coeffs(
5379 /// The end-of-block position (spec 5.11.39): which group of scan positions the5418 /// The end-of-block position (spec 5.11.39): which group of scan positions the
5380 /// last coded coefficient falls in, then its offset inside that group — the5419 /// last coded coefficient falls in, then its offset inside that group — the
5381 /// offset's top bit from a CDF and the rest as raw bits.5420 /// offset's top bit from a CDF and the rest as raw bits.
5382-fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane: Option<usize>) {5421+fn write_eob(
5422+ enc: &mut SymbolEncoder,
5423+ coding: &mut TxbTables,
5424+ eob: usize,
5425+ plane: Option<usize>,
5426+ class: crate::decode::TxClass,
5427+) {
5383 /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of5428 /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of
5384 /// end-of-block positions covers, indexed by the group's own number.5429 /// end-of-block positions covers, indexed by the group's own number.
5385 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];5430 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];
@@ -5391,8 +5436,14 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane:
5391 .rposition(|&start| start <= eob)5436 .rposition(|&start| start <= eob)
5392 .expect("the first group starts at zero");5437 .expect("the first group starts at zero");
5393 // The groups are numbered from one, and how many of them the transform5438 // The groups are numbered from one, and how many of them the transform
5394- // reaches is the size of its own end-of-block alphabet.5439+ // reaches is the size of its own end-of-block alphabet. A
5395- enc.symbol(group - 1, coding.eob_pt);5440+ // `TX_CLASS_HORIZ`/`TX_CLASS_VERT` unit adapts a wholly separate row
5441+ // (libaom's `eob_multi_ctx`), which is what `decode::read_eob` reads.
5442+ let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) {
5443+ (crate::decode::TxClass::TwoD, _) | (_, None) => coding.eob_pt,
5444+ (_, Some(class1)) => class1,
5445+ };
5446+ enc.symbol(group - 1, eob_pt);
5396 if let Some(plane) = plane {5447 if let Some(plane) = plane {
5397 ec_rng_trace(|| {5448 ec_rng_trace(|| {
5398 format!(5449 format!(
@@ -5460,10 +5511,35 @@ fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize {
5460 /// five neighbours below and to the right of it — the ones a decoder walking5511 /// five neighbours below and to the right of it — the ones a decoder walking
5461 /// the scan backwards already has — plus a term for where in the transform it5512 /// the scan backwards already has — plus a term for where in the transform it
5462 /// sits. The DC reads context 0 whatever its neighbours carry.5513 /// sits. The DC reads context 0 whatever its neighbours carry.
5463-fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {5514+fn base_ctx(
5464- if row == 0 && col == 0 {5515+ grid: &[i32],
5516+ side: usize,
5517+ row: usize,
5518+ col: usize,
5519+ class: crate::decode::TxClass,
5520+) -> usize {
5521+ use crate::decode::TxClass;
5522+ // Only `TX_CLASS_2D` short-circuits its DC to context 0
5523+ // (`get_nz_map_ctx_from_stats`'s `(tx_class | coeff_idx) == 0`); a 1-D
5524+ // class runs the DC through the same neighbour sum as every other
5525+ // position.
5526+ if class == TxClass::TwoD && row == 0 && col == 0 {
5465 return 0;5527 return 0;
5466 }5528 }
5529+ if class != TxClass::TwoD {
5530+ let offsets: [(usize, usize); 5] = if class == TxClass::Horiz {
5531+ [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
5532+ } else {
5533+ [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)]
5534+ };
5535+ let mag: i32 = offsets
5536+ .iter()
5537+ .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
5538+ .sum();
5539+ let along = if class == TxClass::Horiz { col } else { row };
5540+ return (((mag + 1) >> 1).min(4) as usize)
5541+ + crate::decode::nz_map_ctx_offset_1d(along.min(31));
5542+ }
5467 // A coefficient two rows and two columns clear of the far edges reaches5543 // A coefficient two rows and two columns clear of the far edges reaches
5468 // all five neighbours, so the gather is five loads off one index with no5544 // all five neighbours, so the gather is five loads off one index with no
5469 // per-neighbour edge test -- which is where this writer spends a third of5545 // per-neighbour edge test -- which is where this writer spends a third of
@@ -5487,7 +5563,30 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
5487 /// The context of a coefficient's base-range tail (spec 8.3.2): the magnitudes5563 /// The context of a coefficient's base-range tail (spec 8.3.2): the magnitudes
5488 /// of its three closest neighbours below and to the right, uncapped, and a5564 /// of its three closest neighbours below and to the right, uncapped, and a
5489 /// term separating the DC, the corner of the transform, and the rest.5565 /// term separating the DC, the corner of the transform, and the rest.
5490-fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {5566+fn br_ctx(
5567+ grid: &[i32],
5568+ side: usize,
5569+ row: usize,
5570+ col: usize,
5571+ class: crate::decode::TxClass,
5572+) -> usize {
5573+ use crate::decode::TxClass;
5574+ if class != TxClass::TwoD {
5575+ let extra = if class == TxClass::Horiz {
5576+ neighbour(grid, side, row, col + 2)
5577+ } else {
5578+ neighbour(grid, side, row + 2, col)
5579+ };
5580+ let mag = neighbour(grid, side, row + 1, col).abs()
5581+ + neighbour(grid, side, row, col + 1).abs()
5582+ + extra.abs();
5583+ let mag = (((mag + 1) >> 1).min(6)) as usize;
5584+ if row == 0 && col == 0 {
5585+ return mag;
5586+ }
5587+ let near_origin = if class == TxClass::Horiz { col == 0 } else { row == 0 };
5588+ return if near_origin { mag + 7 } else { mag + 14 };
5589+ }
5491 let p = row * side + col;5590 let p = row * side + col;
5492 let mag: i32 = if row + 1 < side && col + 1 < side {5591 let mag: i32 = if row + 1 < side && col + 1 < side {
5493 grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs()5592 grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs()