A sub-8x8 rect leaf reads a tx_depth symbol -- TX_MODE_SELECT writes one for BLOCK_4X8/8X4 too
Browse files at this commit88fd2ae1eaaf26bf02cd441f624661c273344b76Parent: d471e3e- fix
- a sub-8x8 rect leaf reads a tx_depth symbol -- TX_MODE_SELECT writes one for BLOCK_4X8/8X4 too
2 changed files +161 -3
- crates/ec-av1/src/decode.rs +148 -3
- crates/ec-av1/src/stream.rs +13 -0
crates/ec-av1/src/decode.rsmodified+148 -3
| @@ -944,6 +944,16 @@ pub(crate) fn sub8_split_hits() -> usize { | ||
| 944 | 944 | thread_local! { |
| 945 | 945 | static TX4X8_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 946 | 946 | static TX8X4_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 947 | + /// Sub-8x8 rect leaves whose `tx_depth` symbol resolved to 1 -- the leaf's | |
| 948 | + /// two 4x4 transform units, predicted one after the other. A separate | |
| 949 | + /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so | |
| 950 | + /// the gate can prove THIS path fired. | |
| 951 | + static RECT8_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 952 | +} | |
| 953 | + | |
| 954 | +/// Current value of [`RECT8_SPLIT_TX_HITS`]. | |
| 955 | +pub(crate) fn rect8_split_tx_hits() -> usize { | |
| 956 | + RECT8_SPLIT_TX_HITS.with(|c| c.get()) | |
| 947 | 957 | } |
| 948 | 958 | |
| 949 | 959 | /// Current value of [`TX4X8_CODED_HITS`]. |
| @@ -1713,8 +1723,16 @@ fn read_coeffs_rect( | ||
| 1713 | 1723 | sign_ctx: usize, |
| 1714 | 1724 | default_tx_type: TxType, |
| 1715 | 1725 | ) -> Result<(Vec<i32>, TxType)> { |
| 1726 | + let rect_trace = std::env::var_os("EC_TRACE_COEFF").is_some(); | |
| 1716 | 1727 | let mut grid = vec![0i32; w * h]; |
| 1717 | 1728 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 1729 | + if rect_trace { | |
| 1730 | + eprintln!( | |
| 1731 | + "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} all_zero={} rng={}", | |
| 1732 | + all_zero as i32, | |
| 1733 | + dec.debug_state().0 | |
| 1734 | + ); | |
| 1735 | + } | |
| 1718 | 1736 | if all_zero { |
| 1719 | 1737 | return Ok((grid, TxType::DctDct)); |
| 1720 | 1738 | } |
| @@ -1735,12 +1753,18 @@ fn read_coeffs_rect( | ||
| 1735 | 1753 | _ => TxType::from_symbol(t), |
| 1736 | 1754 | } |
| 1737 | 1755 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 1756 | + if rect_trace { | |
| 1757 | + eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); | |
| 1758 | + } | |
| 1738 | 1759 | } |
| 1739 | 1760 | let class = TxClass::of(tx_type); |
| 1740 | 1761 | if class != TxClass::TwoD { |
| 1741 | 1762 | TX_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 1742 | 1763 | } |
| 1743 | 1764 | let eob = read_eob(dec, coding, class); |
| 1765 | + if rect_trace { | |
| 1766 | + eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={}", dec.debug_state().0); | |
| 1767 | + } | |
| 1744 | 1768 | let class_scan; |
| 1745 | 1769 | let scan: &[u16] = if class == TxClass::TwoD { |
| 1746 | 1770 | scan |
| @@ -1754,10 +1778,24 @@ fn read_coeffs_rect( | ||
| 1754 | 1778 | let (row, col) = (pos / w, pos % w); |
| 1755 | 1779 | let level = if scan_idx == eob - 1 { |
| 1756 | 1780 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 1757 | - dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1 | |
| 1781 | + let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; | |
| 1782 | + if rect_trace { | |
| 1783 | + eprintln!( | |
| 1784 | + "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", | |
| 1785 | + dec.debug_state().0 | |
| 1786 | + ); | |
| 1787 | + } | |
| 1788 | + v | |
| 1758 | 1789 | } else { |
| 1759 | 1790 | let ctx = base_ctx_rect(&levels, w, h, row, col, class); |
| 1760 | - dec.symbol(&mut coding.base[ctx]) as i32 | |
| 1791 | + let v = dec.symbol(&mut coding.base[ctx]) as i32; | |
| 1792 | + if rect_trace { | |
| 1793 | + eprintln!( | |
| 1794 | + "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", | |
| 1795 | + dec.debug_state().0 | |
| 1796 | + ); | |
| 1797 | + } | |
| 1798 | + v | |
| 1761 | 1799 | }; |
| 1762 | 1800 | let level = if level > NUM_BASE_LEVELS { |
| 1763 | 1801 | let ctx = br_ctx_rect(&levels, w, h, row, col, class); |
| @@ -1765,6 +1803,12 @@ fn read_coeffs_rect( | ||
| 1765 | 1803 | let mut sent = 0; |
| 1766 | 1804 | loop { |
| 1767 | 1805 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 1806 | + if rect_trace { | |
| 1807 | + eprintln!( | |
| 1808 | + "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}", | |
| 1809 | + dec.debug_state().0 | |
| 1810 | + ); | |
| 1811 | + } | |
| 1768 | 1812 | level += k; |
| 1769 | 1813 | sent += BR_STEP; |
| 1770 | 1814 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| @@ -6058,6 +6102,7 @@ fn decode_leaf_rect8( | ||
| 6058 | 6102 | base_q_idx: u8, |
| 6059 | 6103 | enable_filter_intra: bool, |
| 6060 | 6104 | allow_intrabc: bool, |
| 6105 | + tx_select: bool, | |
| 6061 | 6106 | reduced_tx_set: bool, |
| 6062 | 6107 | ) -> Result<(usize, usize)> { |
| 6063 | 6108 | let (r, c) = outer_at; |
| @@ -6099,6 +6144,26 @@ fn decode_leaf_rect8( | ||
| 6099 | 6144 | "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", |
| 6100 | 6145 | )); |
| 6101 | 6146 | } |
| 6147 | + // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4 | |
| 6148 | + // leaf too, and is read for every intra block, skipped or not. Its | |
| 6149 | + // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` -- | |
| 6150 | + // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the | |
| 6151 | + // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol | |
| 6152 | + // `tx_size_cat2` the 32x16 strips read -- with the rect context | |
| 6153 | + // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh). | |
| 6154 | + // Missing this read consumed one symbol fewer than the encoder wrote | |
| 6155 | + // and desynced the tile at the very first rect leaf (class | |
| 6156 | + // `symbol-consumption-gap`). | |
| 6157 | + let depth = if tx_select { | |
| 6158 | + let ctx = tx_size_context_rect(neighbours, lmi, bw, bh); | |
| 6159 | + dec.symbol(&mut cdfs.tx_size_cat0[ctx]) | |
| 6160 | + } else { | |
| 6161 | + 0 | |
| 6162 | + }; | |
| 6163 | + if depth != 0 { | |
| 6164 | + TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); | |
| 6165 | + RECT8_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); | |
| 6166 | + } | |
| 6102 | 6167 | leaf_modes[i] = mode; |
| 6103 | 6168 | leaf_skips[i] = skip; |
| 6104 | 6169 | neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode); |
| @@ -6108,6 +6173,83 @@ fn decode_leaf_rect8( | ||
| 6108 | 6173 | } |
| 6109 | 6174 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 6110 | 6175 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 6176 | + // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into | |
| 6177 | + // two 4x4 transform units along its long axis, each predicted from | |
| 6178 | + // the previous unit's reconstruction and reading its own `txb_skip` | |
| 6179 | + // context -- `decode_leaf8`'s own depth-1 loop, two units instead of | |
| 6180 | + // four. The per-TU path writes its own neighbour state, so the | |
| 6181 | + // block-level write below is skipped for it. | |
| 6182 | + let split = depth != 0; | |
| 6183 | + if split { | |
| 6184 | + let mut done = 0usize; | |
| 6185 | + for tu in 0..2usize { | |
| 6186 | + let tu_mi = if vert { | |
| 6187 | + (lmi.0 + tu, lmi.1) | |
| 6188 | + } else { | |
| 6189 | + (lmi.0, lmi.1 + tu) | |
| 6190 | + }; | |
| 6191 | + let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); | |
| 6192 | + let tu_around = neighbours.around_mi(tu_mi, 4)[0]; | |
| 6193 | + let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height); | |
| 6194 | + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); | |
| 6195 | + if skip { | |
| 6196 | + // A skipped leaf still predicts per transform unit: the | |
| 6197 | + // second 4x4 takes its above/left edge from the first | |
| 6198 | + // one's reconstruction, which is NOT what a single 4x8 | |
| 6199 | + // prediction of the same mode produces. | |
| 6200 | + let zeros = vec![0i32; 16]; | |
| 6201 | + y.reconstruct( | |
| 6202 | + tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, None, | |
| 6203 | + smooth_neighbor, | |
| 6204 | + ); | |
| 6205 | + neighbours.record_mi_luma(tu_mi, 4, &zeros); | |
| 6206 | + continue; | |
| 6207 | + } | |
| 6208 | + let tu_grid = read_plane( | |
| 6209 | + dec, | |
| 6210 | + cdfs, | |
| 6211 | + if reduced_tx_set { | |
| 6212 | + TxbSet::Luma4 | |
| 6213 | + } else { | |
| 6214 | + TxbSet::Luma4Set1 | |
| 6215 | + }, | |
| 6216 | + scan4, | |
| 6217 | + 0, | |
| 6218 | + tu_around, | |
| 6219 | + mode, | |
| 6220 | + mode, | |
| 6221 | + angle_delta_y, | |
| 6222 | + tu_reach, | |
| 6223 | + y, | |
| 6224 | + tu_px, | |
| 6225 | + tu_py, | |
| 6226 | + 4, | |
| 6227 | + TX4, | |
| 6228 | + base_q_idx, | |
| 6229 | + None, | |
| 6230 | + None, | |
| 6231 | + Some(tu_skip_ctx), | |
| 6232 | + smooth_neighbor, | |
| 6233 | + )?; | |
| 6234 | + if tu_grid.iter().any(|&l| l != 0) { | |
| 6235 | + done += 1; | |
| 6236 | + } | |
| 6237 | + neighbours.record_mi_luma(tu_mi, 4, &tu_grid); | |
| 6238 | + } | |
| 6239 | + if done > 0 { | |
| 6240 | + if vert { | |
| 6241 | + TX4X8_CODED_HITS.with(|h| h.set(h.get() + 1)); | |
| 6242 | + } else { | |
| 6243 | + TX8X4_CODED_HITS.with(|h| h.set(h.get() + 1)); | |
| 6244 | + } | |
| 6245 | + } | |
| 6246 | + neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); | |
| 6247 | + neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0); | |
| 6248 | + if has_chroma { | |
| 6249 | + last_uv = chroma; | |
| 6250 | + } | |
| 6251 | + continue; | |
| 6252 | + } | |
| 6111 | 6253 | let grid = if skip { |
| 6112 | 6254 | let zeros = vec![0i32; bw * bh]; |
| 6113 | 6255 | y.reconstruct_rect( |
| @@ -6172,7 +6314,8 @@ fn decode_leaf_rect8( | ||
| 6172 | 6314 | } |
| 6173 | 6315 | } |
| 6174 | 6316 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 6175 | - neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, bw as u8, bh as u8, 0); | |
| 6317 | + let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) }; | |
| 6318 | + neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0); | |
| 6176 | 6319 | if has_chroma { |
| 6177 | 6320 | last_uv = chroma; |
| 6178 | 6321 | } |
| @@ -7969,6 +8112,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 7969 | 8112 | base_q_idx, |
| 7970 | 8113 | enable_filter_intra, |
| 7971 | 8114 | allow_intrabc, |
| 8115 | + tx_select, | |
| 7972 | 8116 | reduced_tx_set, |
| 7973 | 8117 | )? |
| 7974 | 8118 | } else { |
| @@ -8095,6 +8239,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 8095 | 8239 | base_q_idx, |
| 8096 | 8240 | enable_filter_intra, |
| 8097 | 8241 | allow_intrabc, |
| 8242 | + tx_select, | |
| 8098 | 8243 | reduced_tx_set, |
| 8099 | 8244 | )? |
| 8100 | 8245 | } else { |
crates/ec-av1/src/stream.rsmodified+13 -0
| @@ -7458,6 +7458,7 @@ mod tests { | ||
| 7458 | 7458 | ] { |
| 7459 | 7459 | let mut refusals = Vec::new(); |
| 7460 | 7460 | let mut fired_runs = 0u32; |
| 7461 | + let mut split_tx = 0usize; | |
| 7461 | 7462 | for attempt in 0..40u32 { |
| 7462 | 7463 | let seed = 300 + attempt; |
| 7463 | 7464 | let cq = 6 + (attempt % 4) * 2; |
| @@ -7556,6 +7557,7 @@ mod tests { | ||
| 7556 | 7557 | let stream = out.stdout; |
| 7557 | 7558 | let vert_before = decode::tx4x8_coded_hits(); |
| 7558 | 7559 | let horz_before = decode::tx8x4_coded_hits(); |
| 7560 | + let split_before = decode::rect8_split_tx_hits(); | |
| 7559 | 7561 | let tiles_before = decode::tile_hits(); |
| 7560 | 7562 | let frames = match decode_stream(&stream) { |
| 7561 | 7563 | Ok(frames) => frames, |
| @@ -7601,6 +7603,7 @@ mod tests { | ||
| 7601 | 7603 | "V vs ffmpeg (seed={seed} cq={cq} depth={bit_depth})" |
| 7602 | 7604 | ); |
| 7603 | 7605 | fired_runs += 1; |
| 7606 | + split_tx += decode::rect8_split_tx_hits() - split_before; | |
| 7604 | 7607 | if fired_runs >= 4 { |
| 7605 | 7608 | break; |
| 7606 | 7609 | } |
| @@ -7611,6 +7614,16 @@ mod tests { | ||
| 7611 | 7614 | ({width}x{height} depth={bit_depth} tiles={tile_args:?}):\n{}", |
| 7612 | 7615 | refusals.join("\n") |
| 7613 | 7616 | ); |
| 7617 | + // aomenc's default `--enable-tx-size-search=1` makes TX_MODE_SELECT | |
| 7618 | + // the frame's tx mode, so every sub-8x8 rect leaf carries a | |
| 7619 | + // `tx_depth` symbol; depth 1 (two 4x4 transform units predicted one | |
| 7620 | + // after the other) is the half of that path a whole-leaf transform | |
| 7621 | + // would silently get wrong, so the gate proves it fired. | |
| 7622 | + assert!( | |
| 7623 | + split_tx > 0, | |
| 7624 | + "no sub-8x8 rect leaf used a split (depth-1) transform in the \ | |
| 7625 | + {fired_runs} pixel-exact runs ({width}x{height} depth={bit_depth})" | |
| 7626 | + ); | |
| 7614 | 7627 | } |
| 7615 | 7628 | } |
| 7616 | 7629 |