A 128x128 block codes its planes per 64x64 mu chunk, and its chroma units use libaom's offset-10 txb_skip rows
Browse files at this commit69826a6db3a6da68f1df51606a48d993f65d46ceParent: 4bf07db- fix
- a 128x128 block codes its planes per 64x64 mu chunk, and its chroma units use libaom's offset-10 txb_skip rows
5 changed files +70 -31
- crates/ec-av1/src/cdf.rs +10 -0
- crates/ec-av1/src/cdf_state.rs +16 -3
- crates/ec-av1/src/decode.rs +44 -24
- crates/ec-av1/src/refusal_inventory.rs +0 -1
- crates/ec-av1/src/stream.rs +0 -3
crates/ec-av1/src/cdf.rsmodified+10 -0
| @@ -3161,6 +3161,16 @@ pub const TXB_SKIP_CHROMA_16_Q3: [[u16; 3]; 3] = | |||
| 3161 | /// TXB_SKIP_CHROMA_32, q-context 3: the same table as [`TXB_SKIP_CHROMA_32`], for `base_q_idx` 121..=255. | 3161 | /// TXB_SKIP_CHROMA_32, q-context 3: the same table as [`TXB_SKIP_CHROMA_32`], for `base_q_idx` 121..=255. |
| 3162 | pub const TXB_SKIP_CHROMA_32_Q3: [[u16; 3]; 3] = | 3162 | pub const TXB_SKIP_CHROMA_32_Q3: [[u16; 3]; 3] = |
| 3163 | [[4656, 32768, 0], [16074, 32768, 0], [24704, 32768, 0]]; | 3163 | [[4656, 32768, 0], [16074, 32768, 0], [24704, 32768, 0]]; |
| 3164 | + | ||
| 3165 | +/// `av1_default_txb_skip_cdfs[q][TX_32X32][10..13]` (token_cdfs.h): libaom's | ||
| 3166 | +/// chroma `txb_skip_ctx` offset is 10 instead of 7 when the plane block is | ||
| 3167 | +/// LARGER than the transform (`get_txb_ctx`), which in AV1 happens only for a | ||
| 3168 | +/// 128x128 block's four TX_32X32 chroma units -- the offset-7 rows above are | ||
| 3169 | +/// every other case (lane-sb128b r3). | ||
| 3170 | +pub const TXB_SKIP_CHROMA_32_BIG_Q0: [[u16; 3]; 3] = [[146, 32768, 0], [5132, 32768, 0], [31657, 32768, 0]]; | ||
| 3171 | +pub const TXB_SKIP_CHROMA_32_BIG_Q1: [[u16; 3]; 3] = [[641, 32768, 0], [22265, 32768, 0], [31452, 32768, 0]]; | ||
| 3172 | +pub const TXB_SKIP_CHROMA_32_BIG: [[u16; 3]; 3] = [[3082, 32768, 0], [20982, 32768, 0], [29443, 32768, 0]]; | ||
| 3173 | +pub const TXB_SKIP_CHROMA_32_BIG_Q3: [[u16; 3]; 3] = [[1806, 32768, 0], [14645, 32768, 0], [25336, 32768, 0]]; | ||
| 3164 | /// EOB_EXTRA_LUMA_16, q-context 3: the same table as [`EOB_EXTRA_LUMA_16`], for `base_q_idx` 121..=255. | 3174 | /// EOB_EXTRA_LUMA_16, q-context 3: the same table as [`EOB_EXTRA_LUMA_16`], for `base_q_idx` 121..=255. |
| 3165 | pub const EOB_EXTRA_LUMA_16_Q3: [[u16; 3]; 9] = [ | 3175 | pub const EOB_EXTRA_LUMA_16_Q3: [[u16; 3]; 9] = [ |
| 3166 | [19941, 32768, 0], | 3176 | [19941, 32768, 0], |
crates/ec-av1/src/cdf_state.rsmodified+16 -3
| @@ -383,7 +383,10 @@ pub(crate) struct Cdfs { | |||
| 383 | /// The all-zero flag of a 16x16 chroma transform. | 383 | /// The all-zero flag of a 16x16 chroma transform. |
| 384 | pub txb_skip_chroma_16: [[u16; 3]; 3], | 384 | pub txb_skip_chroma_16: [[u16; 3]; 3], |
| 385 | /// The all-zero flag of a 32x32 chroma transform. | 385 | /// The all-zero flag of a 32x32 chroma transform. |
| 386 | - pub txb_skip_chroma_32: [[u16; 3]; 3], | 386 | + /// SIX contexts, not three: rows 0..3 are libaom's `txb_skip_ctx` 7..9 |
| 387 | + /// (chroma transform == its plane block) and rows 3..6 its 10..12 (plane | ||
| 388 | + /// block larger, i.e. a 128x128 block's four TX_32X32 chroma units). | ||
| 389 | + pub txb_skip_chroma_32: [[u16; 3]; 6], | ||
| 387 | /// The end-of-block group of a luma transform of 1024 positions. | 390 | /// The end-of-block group of a luma transform of 1024 positions. |
| 388 | pub eob_pt_1024_luma: [u16; 12], | 391 | pub eob_pt_1024_luma: [u16; 12], |
| 389 | /// The same, for chroma. | 392 | /// The same, for chroma. |
| @@ -1237,13 +1240,23 @@ impl Cdfs { | |||
| 1237 | cdf::TXB_SKIP_CHROMA_16, | 1240 | cdf::TXB_SKIP_CHROMA_16, |
| 1238 | cdf::TXB_SKIP_CHROMA_16_Q3, | 1241 | cdf::TXB_SKIP_CHROMA_16_Q3, |
| 1239 | ), | 1242 | ), |
| 1240 | - txb_skip_chroma_32: pick( | 1243 | + txb_skip_chroma_32: { |
| 1244 | + let small = pick( | ||
| 1241 | q_ctx, | 1245 | q_ctx, |
| 1242 | cdf::TXB_SKIP_CHROMA_32_Q0, | 1246 | cdf::TXB_SKIP_CHROMA_32_Q0, |
| 1243 | cdf::TXB_SKIP_CHROMA_32_Q1, | 1247 | cdf::TXB_SKIP_CHROMA_32_Q1, |
| 1244 | cdf::TXB_SKIP_CHROMA_32, | 1248 | cdf::TXB_SKIP_CHROMA_32, |
| 1245 | cdf::TXB_SKIP_CHROMA_32_Q3, | 1249 | cdf::TXB_SKIP_CHROMA_32_Q3, |
| 1246 | - ), | 1250 | + ); |
| 1251 | + let big = pick( | ||
| 1252 | + q_ctx, | ||
| 1253 | + cdf::TXB_SKIP_CHROMA_32_BIG_Q0, | ||
| 1254 | + cdf::TXB_SKIP_CHROMA_32_BIG_Q1, | ||
| 1255 | + cdf::TXB_SKIP_CHROMA_32_BIG, | ||
| 1256 | + cdf::TXB_SKIP_CHROMA_32_BIG_Q3, | ||
| 1257 | + ); | ||
| 1258 | + [small[0], small[1], small[2], big[0], big[1], big[2]] | ||
| 1259 | + }, | ||
| 1247 | eob_pt_1024_luma: pick( | 1260 | eob_pt_1024_luma: pick( |
| 1248 | q_ctx, | 1261 | q_ctx, |
| 1249 | cdf::EOB_PT_1024_LUMA_Q0, | 1262 | cdf::EOB_PT_1024_LUMA_Q0, |
crates/ec-av1/src/decode.rsmodified+44 -24
| @@ -8633,7 +8633,12 @@ fn read_plane( | |||
| 8633 | let skip_ctx = if plane_idx == 0 { | 8633 | let skip_ctx = if plane_idx == 0 { |
| 8634 | luma_skip_ctx.unwrap_or(0) | 8634 | luma_skip_ctx.unwrap_or(0) |
| 8635 | } else { | 8635 | } else { |
| 8636 | - usize::from(around.0) + usize::from(around.1) | 8636 | + // libaom `get_txb_ctx`: a chroma unit's context is |
| 8637 | + // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the | ||
| 8638 | + // plane block is bigger than the transform. Our chroma tables hold the | ||
| 8639 | + // offset-7 rows first, so the caller adds 3 for the bigger-block case | ||
| 8640 | + // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3). | ||
| 8641 | + usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) | ||
| 8637 | }; | 8642 | }; |
| 8638 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's | 8643 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's |
| 8639 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, | 8644 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, |
| @@ -9135,8 +9140,27 @@ fn decode_block( | |||
| 9135 | // the earlier tiles in this same block already wrote -- the same | 9140 | // the earlier tiles in this same block already wrote -- the same |
| 9136 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. | 9141 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. |
| 9137 | let n_axis = side / logical_tx; | 9142 | let n_axis = side / logical_tx; |
| 9138 | - for tu_row in 0..n_axis { | 9143 | + let cn = (chroma_side / chroma_tx).max(1); |
| 9139 | - for tu_col in 0..n_axis { | 9144 | + // libaom's transform-block loop is over 64x64 luma "mu" chunks |
| 9145 | + // (`decode_token_recon_block`: `mu_blocks_wide/high` = | ||
| 9146 | + // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk | ||
| 9147 | + // before moving to the next one. Below 128 a block is a single chunk, | ||
| 9148 | + // so this is byte-for-byte the all-luma-then-all-chroma order that has | ||
| 9149 | + // always run; at 128 the four quadrants interleave Y,U,V per quadrant | ||
| 9150 | + // and reading all four luma units first desyncs the tile at the second | ||
| 9151 | + // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`). | ||
| 9152 | + let nchunk = (side / 64).max(1); | ||
| 9153 | + // `cn > 1` means the uv plane block is larger than the uv transform, | ||
| 9154 | + // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10. | ||
| 9155 | + let chroma_ctx_offset = if cn > 1 { Some(3) } else { None }; | ||
| 9156 | + let tpc = (n_axis / nchunk).max(1); | ||
| 9157 | + let cpc = (cn / nchunk).max(1); | ||
| 9158 | + let mut last_grids = [Vec::new(), Vec::new()]; | ||
| 9159 | + let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new(); | ||
| 9160 | + for ch_row in 0..nchunk { | ||
| 9161 | + for ch_col in 0..nchunk { | ||
| 9162 | + for tu_row in ch_row * tpc..(ch_row + 1) * tpc { | ||
| 9163 | + for tu_col in ch_col * tpc..(ch_col + 1) * tpc { | ||
| 9140 | let tu_mi = ( | 9164 | let tu_mi = ( |
| 9141 | mi_r + tu_row * (logical_tx / MI), | 9165 | mi_r + tu_row * (logical_tx / MI), |
| 9142 | mi_c + tu_col * (logical_tx / MI), | 9166 | mi_c + tu_col * (logical_tx / MI), |
| @@ -9219,19 +9243,17 @@ fn decode_block( | |||
| 9219 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); | 9243 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 9220 | } | 9244 | } |
| 9221 | } | 9245 | } |
| 9246 | + // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where | ||
| 9247 | + // this still runs after every luma unit of the block. | ||
| 9222 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 9248 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 9223 | // The block's uv plane block is `chroma_side` across but its uv | 9249 | // The block's uv plane block is `chroma_side` across but its uv |
| 9224 | // transform is `chroma_tx` (`av1_get_max_uv_txsize` = | 9250 | // transform is `chroma_tx` (`av1_get_max_uv_txsize` = |
| 9225 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): | 9251 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): |
| 9226 | // identical at every size up to 64x64, but a 128x128 block's 64x64 uv | 9252 | // identical at every size up to 64x64, but a 128x128 block's 64x64 uv |
| 9227 | // plane block adjusts TX_64X64 down to TX_32X32, so each plane is | 9253 | // plane block adjusts TX_64X64 down to TX_32X32, so each plane is |
| 9228 | - // FOUR units (lane-sb128b r2). `cn == 1` is byte-for-byte the single | 9254 | + // FOUR units (lane-sb128b r2) -- one per mu chunk. |
| 9229 | - // whole-plane read this always did. | 9255 | + for cu_row in ch_row * cpc..(ch_row + 1) * cpc { |
| 9230 | - let cn = (chroma_side / chroma_tx).max(1); | 9256 | + for cu_col in ch_col * cpc..(ch_col + 1) * cpc { |
| 9231 | - let mut last_grids = [Vec::new(), Vec::new()]; | ||
| 9232 | - let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new(); | ||
| 9233 | - for cu_row in 0..cn { | ||
| 9234 | - for cu_col in 0..cn { | ||
| 9235 | // The chroma context arrays are indexed in LUMA mi, so this | 9257 | // The chroma context arrays are indexed in LUMA mi, so this |
| 9236 | // unit's anchor is its own luma quadrant: `chroma_tx` chroma | 9258 | // unit's anchor is its own luma quadrant: `chroma_tx` chroma |
| 9237 | // pixels span `2 * chroma_tx` luma ones. | 9259 | // pixels span `2 * chroma_tx` luma ones. |
| @@ -9259,7 +9281,7 @@ fn decode_block( | |||
| 9259 | uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y, | 9281 | uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y, |
| 9260 | chroma_tx, chroma_tx, base_q_idx, | 9282 | chroma_tx, chroma_tx, base_q_idx, |
| 9261 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), | 9283 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 9262 | - None, None, smooth_neighbor_uv, | 9284 | + None, chroma_ctx_offset, smooth_neighbor_uv, |
| 9263 | )?; | 9285 | )?; |
| 9264 | if let Some((_, vb)) = &palette_uv_bufs { | 9286 | if let Some((_, vb)) = &palette_uv_bufs { |
| 9265 | set_palette_pred(vb.clone()); | 9287 | set_palette_pred(vb.clone()); |
| @@ -9269,15 +9291,25 @@ fn decode_block( | |||
| 9269 | uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y, | 9291 | uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y, |
| 9270 | chroma_tx, chroma_tx, base_q_idx, | 9292 | chroma_tx, chroma_tx, base_q_idx, |
| 9271 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), | 9293 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 9272 | - None, None, smooth_neighbor_uv, | 9294 | + None, chroma_ctx_offset, smooth_neighbor_uv, |
| 9273 | )?; | 9295 | )?; |
| 9274 | if cn > 1 { | 9296 | if cn > 1 { |
| 9275 | CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); | 9297 | CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 9298 | + // Immediately, so the NEXT unit of this same block reads | ||
| 9299 | + // this one's coefficient context (libaom's chroma | ||
| 9300 | + // `txb_skip_ctx` for the second quadrant is 11, i.e. base | ||
| 9301 | + // 1 from this left neighbour -- measured against aomdec). | ||
| 9302 | + // The same writes are replayed after `record_split_luma` | ||
| 9303 | + // below, which would otherwise clobber them whole-block. | ||
| 9304 | + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid); | ||
| 9305 | + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid); | ||
| 9276 | units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone())); | 9306 | units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone())); |
| 9277 | } | 9307 | } |
| 9278 | last_grids = [u_grid, v_grid]; | 9308 | last_grids = [u_grid, v_grid]; |
| 9279 | } | 9309 | } |
| 9280 | } | 9310 | } |
| 9311 | + } | ||
| 9312 | + } | ||
| 9281 | // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma | 9313 | // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma |
| 9282 | // context write; at `cn > 1` the per-unit writes above are what the | 9314 | // context write; at `cn > 1` the per-unit writes above are what the |
| 9283 | // next block reads, so they run after this (last writer wins). | 9315 | // next block reads, so they run after this (last writer wins). |
| @@ -12342,18 +12374,6 @@ fn read_sb128_root( | |||
| 12342 | PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); | 12374 | PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 12343 | } else { | 12375 | } else { |
| 12344 | PART128_NONE_HITS.with(|c| c.set(c.get() + 1)); | 12376 | PART128_NONE_HITS.with(|c| c.set(c.get() + 1)); |
| 12345 | - // lane-sb128b r2 RESIDUAL, measured (gate | ||
| 12346 | - // `a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact`, | ||
| 12347 | - // 384x384 cq63 seed 42, a 76-byte near-flat frame): the chroma TU loop | ||
| 12348 | - // landed and the tile decodes end to end, but frame 0 luma differs from | ||
| 12349 | - // ffmpeg at 139162/147456 samples -- ffmpeg's row 0 is a constant 115 | ||
| 12350 | - // where ours ramps 115 -> 112 across the first TX_64X64 unit, i.e. our | ||
| 12351 | - // first luma unit reads AC coefficients libaom never wrote. The DC and | ||
| 12352 | - // the mode agree; the divergence is inside that unit's coefficient | ||
| 12353 | - // read. Refuse rather than emit wrong pixels. | ||
| 12354 | - return Err(unsupported( | ||
| 12355 | - "a 128x128 PARTITION_NONE block (four TX_64X64 luma and four TX_32X32 chroma units all decode, but the first unit's coefficients already carry AC where libaom has DC only -- lane-sb128b r2's open entropy residual)", | ||
| 12356 | - )); | ||
| 12357 | } | 12377 | } |
| 12358 | Ok(part128) | 12378 | Ok(part128) |
| 12359 | } | 12379 | } |
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
| @@ -43,7 +43,6 @@ const REFUSALS: &[&str] = &[ | |||
| 43 | "a tx_type symbol outside its CDF's own set: {t}", | 43 | "a tx_type symbol outside its CDF's own set: {t}", |
| 44 | "an INTER 32x32 partition type this decoder does not code (value={part32})", | 44 | "an INTER 32x32 partition type this decoder does not code (value={part32})", |
| 45 | "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", | 45 | "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", |
| 46 | - "a 128x128 PARTITION_NONE block (four TX_64X64 luma and four TX_32X32 chroma units all decode, but the first unit's coefficients already carry AC where libaom has DC only -- lane-sb128b r2's open entropy residual)", | ||
| 47 | "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)", | 46 | "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)", |
| 48 | "a 128x128 superblock PARTITION_NONE root on an inter frame (only the key-frame path codes a whole 128x128 block)", | 47 | "a 128x128 superblock PARTITION_NONE root on an inter frame (only the key-frame path codes a whole 128x128 block)", |
| 49 | "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)", | 48 | "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)", |
crates/ec-av1/src/stream.rsmodified+0 -3
| @@ -18014,9 +18014,6 @@ mod tests { | |||
| 18014 | /// Smooth gradients at a high `--cq-level` are what make the RD search | 18014 | /// Smooth gradients at a high `--cq-level` are what make the RD search |
| 18015 | /// keep a whole 128x128 block; `part128_none_hits` HARD-asserts it did. | 18015 | /// keep a whole 128x128 block; `part128_none_hits` HARD-asserts it did. |
| 18016 | #[test] | 18016 | #[test] |
| 18017 | - #[ignore = "lane-sb128b r2 reproducer: the 128 NONE luma AND chroma units all \ | ||
| 18018 | - decode, the first luma unit still reads AC libaom never wrote, so the \ | ||
| 18019 | - product path refuses the arm by name"] | ||
| 18020 | fn a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact() { | 18017 | fn a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact() { |
| 18021 | const NAME: &str = | 18018 | const NAME: &str = |
| 18022 | "a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact"; | 18019 | "a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact"; |