One cdef_idx literal covers every 64x64 unit its block spans
Browse files at this commitf677e4cb41a5555d018430614db7e7268d253570Parent: 1ee7fc6- fix
- one cdef_idx literal covers every 64x64 unit its block spans
- the 128 residual arm ships opt-in
2 changed files +81 -14
- crates/ec-av1/src/decode.rs +36 -11
- crates/ec-av1/src/encode.rs +45 -3
crates/ec-av1/src/decode.rsmodified+36 -11
| @@ -818,7 +818,17 @@ fn maybe_read_delta_lf(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi | |||
| 818 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ | 818 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ |
| 819 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per | 819 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per |
| 820 | /// superblock, at the first non-skip block. | 820 | /// superblock, at the first non-skip block. |
| 821 | -fn maybe_read_cdef_idx(dec: &mut SymbolDecoder, mi_r: usize, mi_c: usize, skip: bool, fctx: &crate::decode::FrameCtx) { | 821 | +fn maybe_read_cdef_idx( |
| 822 | + dec: &mut SymbolDecoder, | ||
| 823 | + mi_r: usize, | ||
| 824 | + mi_c: usize, | ||
| 825 | + // The reading BLOCK's own span in 4x4 mode-info units: a block wider or | ||
| 826 | + // taller than one 64x64 CDEF unit is the first non-skip block of EVERY | ||
| 827 | + // unit it covers, so its one literal drives all of them (lane-b128r). | ||
| 828 | + (w_mi, h_mi): (usize, usize), | ||
| 829 | + skip: bool, | ||
| 830 | + fctx: &crate::decode::FrameCtx, | ||
| 831 | +) { | ||
| 822 | let bits = fctx.cdef_bits.with(|c| c.get()); | 832 | let bits = fctx.cdef_bits.with(|c| c.get()); |
| 823 | if bits == 0 || skip || fctx.cdef_transmitted.with(|c| c.get()) { | 833 | if bits == 0 || skip || fctx.cdef_transmitted.with(|c| c.get()) { |
| 824 | return; | 834 | return; |
| @@ -829,11 +839,26 @@ fn maybe_read_cdef_idx(dec: &mut SymbolDecoder, mi_r: usize, mi_c: usize, skip: | |||
| 829 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); | 839 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 830 | let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); | 840 | let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); |
| 831 | if sb_cols > 0 { | 841 | if sb_cols > 0 { |
| 842 | + // lane-b128r: libaom's four CDEF units of a 128 superblock all read | ||
| 843 | + // their strength off the MB_MODE_INFO of the first non-skip block in | ||
| 844 | + // their own unit -- and a block that COVERS several units (the 128 | ||
| 845 | + // root's own 128x128 block, the only one there is) is that block for | ||
| 846 | + // every unit it covers, so one literal drives all four. This grid is | ||
| 847 | + // per 64x64 unit, so the read stamps every unit the block covers; one | ||
| 848 | + // that is smaller than a unit stamps exactly the cell it always did. | ||
| 849 | + let (rows, cols) = ( | ||
| 850 | + (h_mi / SB_MI as usize).max(1), | ||
| 851 | + (w_mi / SB_MI as usize).max(1), | ||
| 852 | + ); | ||
| 832 | fctx.cdef_idx_grid.with(|g| { | 853 | fctx.cdef_idx_grid.with(|g| { |
| 833 | let mut g = g.borrow_mut(); | 854 | let mut g = g.borrow_mut(); |
| 834 | - let i = sb_r * sb_cols + sb_c; | 855 | + for dr in 0..rows { |
| 835 | - if i < g.len() { | 856 | + for dc in 0..cols { |
| 836 | - g[i] = idx; | 857 | + let i = (sb_r + dr) * sb_cols + sb_c + dc; |
| 858 | + if sb_c + dc < sb_cols && i < g.len() { | ||
| 859 | + g[i] = idx; | ||
| 860 | + } | ||
| 861 | + } | ||
| 837 | } | 862 | } |
| 838 | }); | 863 | }); |
| 839 | } | 864 | } |
| @@ -9066,7 +9091,7 @@ fn read_intra_mode_rect( | |||
| 9066 | if !seg_id_pre_skip(fctx) { | 9091 | if !seg_id_pre_skip(fctx) { |
| 9067 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); | 9092 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 9068 | } | 9093 | } |
| 9069 | - maybe_read_cdef_idx(dec, mi_r, mi_c, skip, fctx); | 9094 | + maybe_read_cdef_idx(dec, mi_r, mi_c, (bw / MI, bh / MI), skip, fctx); |
| 9070 | istep!("cdef", 0); | 9095 | istep!("cdef", 0); |
| 9071 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never | 9096 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| 9072 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always | 9097 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always |
| @@ -13184,7 +13209,7 @@ fn read_intra_mode( | |||
| 13184 | } | 13209 | } |
| 13185 | // spec order (see the comment below on `read_intrabc_info`): `skip`, | 13210 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 13186 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. | 13211 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 13187 | - maybe_read_cdef_idx(dec, mi_r, mi_c, skip, fctx); | 13212 | + maybe_read_cdef_idx(dec, mi_r, mi_c, (side / MI, side / MI), skip, fctx); |
| 13188 | istep!("cdef", 0); | 13213 | istep!("cdef", 0); |
| 13189 | // lane-sb128b r1: "this leaf IS the superblock" is `side == sb_size`, not | 13214 | // lane-sb128b r1: "this leaf IS the superblock" is `side == sb_size`, not |
| 13190 | // a hardcoded 64 -- a `PARTITION_NONE` 128x128 root is the whole | 13215 | // a hardcoded 64 -- a `PARTITION_NONE` 128x128 root is the whole |
| @@ -15919,7 +15944,7 @@ fn read_intra_mode_sub8( | |||
| 15919 | if !seg_id_pre_skip(fctx) { | 15944 | if !seg_id_pre_skip(fctx) { |
| 15920 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); | 15945 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 15921 | } | 15946 | } |
| 15922 | - maybe_read_cdef_idx(dec, mi_r, mi_c, skip, fctx); | 15947 | + maybe_read_cdef_idx(dec, mi_r, mi_c, (2, 2), skip, fctx); |
| 15923 | istep!("cdef", 0); | 15948 | istep!("cdef", 0); |
| 15924 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); | 15949 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 15925 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); | 15950 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| @@ -26811,7 +26836,7 @@ fn decode_inter_block( | |||
| 26811 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); | 26836 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); |
| 26812 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 26837 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 26813 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false, fctx); | 26838 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false, fctx); |
| 26814 | - maybe_read_cdef_idx(dec, rmi, cmi, skip, fctx); | 26839 | + maybe_read_cdef_idx(dec, rmi, cmi, (write_w / MI, write_h / MI), skip, fctx); |
| 26815 | // lane-inter4 r1: spec 5.11.6's `bSize == sbSize` -- a 64x32/64x16 strip | 26840 | // lane-inter4 r1: spec 5.11.6's `bSize == sbSize` -- a 64x32/64x16 strip |
| 26816 | // is NOT the whole superblock, so its `skip` never suppresses the read. | 26841 | // is NOT the whole superblock, so its `skip` never suppresses the read. |
| 26817 | // lane-sb128c r7: `sbSize` is 128 under `--sb-size=128`, so the whole-block | 26842 | // lane-sb128c r7: `sbSize` is 128 under `--sb-size=128`, so the whole-block |
| @@ -30610,7 +30635,7 @@ fn decode_inter_sub8_split4( | |||
| 30610 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); | 30635 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); |
| 30611 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 30636 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 30612 | inter_segment_id(dec, cdfs, rmi, cmi, 1, 1, skip, false, fctx); | 30637 | inter_segment_id(dec, cdfs, rmi, cmi, 1, 1, skip, false, fctx); |
| 30613 | - maybe_read_cdef_idx(dec, rmi, cmi, skip, fctx); | 30638 | + maybe_read_cdef_idx(dec, rmi, cmi, (2, 2), skip, fctx); |
| 30614 | maybe_read_delta_q(dec, cdfs, rmi, cmi, false, skip, fctx); | 30639 | maybe_read_delta_q(dec, cdfs, rmi, cmi, false, skip, fctx); |
| 30615 | maybe_read_delta_lf(dec, cdfs, rmi, cmi, false, skip, fctx); | 30640 | maybe_read_delta_lf(dec, cdfs, rmi, cmi, false, skip, fctx); |
| 30616 | let (has_above, has_left) = ( | 30641 | let (has_above, has_left) = ( |
| @@ -31653,7 +31678,7 @@ fn decode_inter_sub8_rect2( | |||
| 31653 | usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); | 31678 | usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); |
| 31654 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 31679 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 31655 | inter_segment_id(dec, cdfs, rmi, cmi, w_mi, h_mi, skip, false, fctx); | 31680 | inter_segment_id(dec, cdfs, rmi, cmi, w_mi, h_mi, skip, false, fctx); |
| 31656 | - maybe_read_cdef_idx(dec, rmi, cmi, skip, fctx); | 31681 | + maybe_read_cdef_idx(dec, rmi, cmi, (2, 2), skip, fctx); |
| 31657 | maybe_read_delta_q(dec, cdfs, rmi, cmi, false, skip, fctx); | 31682 | maybe_read_delta_q(dec, cdfs, rmi, cmi, false, skip, fctx); |
| 31658 | maybe_read_delta_lf(dec, cdfs, rmi, cmi, false, skip, fctx); | 31683 | maybe_read_delta_lf(dec, cdfs, rmi, cmi, false, skip, fctx); |
| 31659 | let (has_above, has_left) = ( | 31684 | let (has_above, has_left) = ( |
| @@ -32401,7 +32426,7 @@ fn decode_inter_block8( | |||
| 32401 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); | 32426 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 32402 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 32427 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 32403 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false, fctx); | 32428 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false, fctx); |
| 32404 | - maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip, fctx); | 32429 | + maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, (2, 2), skip, fctx); |
| 32405 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. | 32430 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. |
| 32406 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip, fctx); | 32431 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip, fctx); |
| 32407 | maybe_read_delta_lf(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip, fctx); | 32432 | maybe_read_delta_lf(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip, fctx); |
crates/ec-av1/src/encode.rsmodified+45 -3
| @@ -737,13 +737,29 @@ fn b128_root() -> bool { | |||
| 737 | /// skip arm -- four TX_64X64 luma units interleaved with a TX_32X32 chroma | 737 | /// skip arm -- four TX_64X64 luma units interleaved with a TX_32X32 chroma |
| 738 | /// pair per 64x64 mu chunk, var-tx depth 0 (the shape | 738 | /// pair per 64x64 mu chunk, var-tx depth 0 (the shape |
| 739 | /// [`crate::tile::write_inter_block_128`] codes and `decode_inter_block` | 739 | /// [`crate::tile::write_inter_block_128`] codes and `decode_inter_block` |
| 740 | -/// reads). On by default; `EC_AV1_B128RES=0` restores lane-b128's skip-only | 740 | +/// reads). Written, witnessed and byte-exact through both decoders, and |
| 741 | -/// root, which is this arm's own attribution control. | 741 | +/// **off by default** (`EC_AV1_B128RES=1` turns it on) for two measured |
| 742 | +/// reasons: | ||
| 743 | +/// | ||
| 744 | +/// * it is INERT on content: over the native 12-frame gate the RD takes it | ||
| 745 | +/// 0-2 times per frame against ~90 whole 128 roots, so the BD effect is | ||
| 746 | +/// inside the noise either way; | ||
| 747 | +/// * with it on, `bd_rate_screen_native`'s bars 2160p row q=90 frame 11 | ||
| 748 | +/// reads 8 luma samples (rows 126..127, cols 1611..1616 -- the two rows | ||
| 749 | +/// above a 128-superblock horizontal edge) where ffmpeg and this | ||
| 750 | +/// encoder's own reconstruction disagree. The residual arm's own witness | ||
| 751 | +/// clip is byte-exact through ffmpeg over 420 forced blocks at 1280x768, | ||
| 752 | +/// so the open half is a FILTER-stage interaction (deblock/CDEF at a 128 | ||
| 753 | +/// block's own edge), not the coefficient path. | ||
| 754 | +/// | ||
| 755 | +/// An inert candidate does not buy a live filter mismatch, so the default | ||
| 756 | +/// stays off until that is root-caused; the knob is the gate for it. | ||
| 742 | fn b128_residual() -> bool { | 757 | fn b128_residual() -> bool { |
| 743 | static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | 758 | static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { |
| 744 | crate::envflags::var("EC_AV1_B128RES").ok().map(|v| v != "0") | 759 | crate::envflags::var("EC_AV1_B128RES").ok().map(|v| v != "0") |
| 745 | }); | 760 | }); |
| 746 | - ENV.unwrap_or(true) && b128_root() | 761 | + (ENV.unwrap_or(false) || FORCE_B128_RESIDUAL.load(std::sync::atomic::Ordering::Relaxed)) |
| 762 | + && b128_root() | ||
| 747 | } | 763 | } |
| 748 | 764 | ||
| 749 | /// Test-only: take the 128 root's residual arm whatever it costs, so a | 765 | /// Test-only: take the 128 root's residual arm whatever it costs, so a |
| @@ -17432,6 +17448,32 @@ mod tests { | |||
| 17432 | ); | 17448 | ); |
| 17433 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { | 17449 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 17434 | if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { | 17450 | if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { |
| 17451 | + // TEMPORARY (lane-b128r bisect): the whole mismatch map. | ||
| 17452 | + let (l, r) = match plane { | ||
| 17453 | + "Y" => (&d.y, &e.reconstruction.y), | ||
| 17454 | + "U" => (&d.u, &e.reconstruction.u), | ||
| 17455 | + _ => (&d.v, &e.reconstruction.v), | ||
| 17456 | + }; | ||
| 17457 | + let w = if plane == "Y" { width } else { width / 2 }; | ||
| 17458 | + let bad: Vec<usize> = l | ||
| 17459 | + .iter() | ||
| 17460 | + .zip(r.iter()) | ||
| 17461 | + .enumerate() | ||
| 17462 | + .filter(|(_, (a, b))| a != b) | ||
| 17463 | + .map(|(i, _)| i) | ||
| 17464 | + .collect(); | ||
| 17465 | + let rows: Vec<usize> = bad.iter().map(|i| i / w).collect(); | ||
| 17466 | + let cols: Vec<usize> = bad.iter().map(|i| i % w).collect(); | ||
| 17467 | + eprintln!( | ||
| 17468 | + "B128R MISMATCH {name} q={q} frame {i} plane {plane}: {} samples, \ | ||
| 17469 | + rows {}..{}, cols {}..{}, first 8 {:?}", | ||
| 17470 | + bad.len(), | ||
| 17471 | + rows.iter().min().copied().unwrap_or(0), | ||
| 17472 | + rows.iter().max().copied().unwrap_or(0), | ||
| 17473 | + cols.iter().min().copied().unwrap_or(0), | ||
| 17474 | + cols.iter().max().copied().unwrap_or(0), | ||
| 17475 | + bad.iter().take(8).map(|i| (i / w, i % w)).collect::<Vec<_>>(), | ||
| 17476 | + ); | ||
| 17435 | panic!( | 17477 | panic!( |
| 17436 | "{name} q={q} frame {i} plane {plane} sample {s}: ffmpeg decoded \ | 17478 | "{name} q={q} frame {i} plane {plane} sample {s}: ffmpeg decoded \ |
| 17437 | {got}, the encoder reconstructed {want}" | 17479 | {got}, the encoder reconstructed {want}" |