Merge branch 'lane-edge128' (d06c5ee6): the CDEF search prices one cdef_idx per block the syntax carries, not one per 64x64 unit; the 128 residual arm's 8-sample edge mismatch was encoder reconstruction only, pins hold at 8290 / 33227
Browse files at this commit4 changed files +337 -28
- crates/ec-av1/src/encode.rs +93 -26
- crates/ec-av1/src/filter_search.rs +39 -2
- crates/ec-av1/src/tile.rs +38 -0
- lanes/edge128.report.md +167 -0
crates/ec-av1/src/encode.rsmodified+93 -26
| @@ -8564,6 +8564,9 @@ pub(crate) fn encode_key_frame_inner( | ||
| 8564 | 8564 | &mut tiles, |
| 8565 | 8565 | search.lambda, |
| 8566 | 8566 | fctx, |
| 8567 | + // A key frame codes no 128x128 root block (the writer has no intra | |
| 8568 | + // path at that size), so every unit carries its own literal. | |
| 8569 | + &[], | |
| 8567 | 8570 | // Every re-code starts from this frame's OWN starting tables, never |
| 8568 | 8571 | // from where the first write left them: the second tile is the same |
| 8569 | 8572 | // symbol sequence plus new ones, so it must adapt from the same |
| @@ -8656,6 +8659,10 @@ fn pick_and_apply_filters( | ||
| 8656 | 8659 | tiles: &mut Vec<Vec<u8>>, |
| 8657 | 8660 | lambda: f64, |
| 8658 | 8661 | fctx: &crate::decode::FrameCtx, |
| 8662 | + // lane-edge128: `crate::tile::cdef_unit_owner` for the blocks these tiles | |
| 8663 | + // carry -- empty (the identity map) for a frame whose every block covers | |
| 8664 | + // one 64x64 CDEF unit. | |
| 8665 | + cdef_owner: &[u32], | |
| 8659 | 8666 | recode: impl Fn( |
| 8660 | 8667 | u8, |
| 8661 | 8668 | usize, |
| @@ -8708,6 +8715,7 @@ fn pick_and_apply_filters( | ||
| 8708 | 8715 | damping, |
| 8709 | 8716 | (sb_cols, sb_rows), |
| 8710 | 8717 | lambda, |
| 8718 | + cdef_owner, | |
| 8711 | 8719 | ); |
| 8712 | 8720 | drop(ft); |
| 8713 | 8721 | let (lf, cdef, idx_grid, picture) = search_result?; |
| @@ -13228,6 +13236,7 @@ pub(crate) fn encode_inter_frame( | ||
| 13228 | 13236 | &mut tiles, |
| 13229 | 13237 | search.lambda, |
| 13230 | 13238 | fctx, |
| 13239 | + &crate::tile::cdef_unit_owner(&blocks, mi_cols, mi_rows), | |
| 13231 | 13240 | // As in `encode_key_frame_inner`, but this frame's end-of-tile tables |
| 13232 | 13241 | // are what the NEXT frame writes from, so the winning re-code |
| 13233 | 13242 | // publishes its own into `end_cdfs` -- the first write's are stale |
| @@ -16376,6 +16385,90 @@ mod tests { | ||
| 16376 | 16385 | } |
| 16377 | 16386 | } |
| 16378 | 16387 | |
| 16388 | + /// lane-edge128: a 128x128 root block WITH a residual on a clip whose | |
| 16389 | + /// CDEF search codes a per-64x64 `cdef_idx` list. `read_cdef` (spec | |
| 16390 | + /// 5.11.56) codes ONE literal for the block that first reaches a unit and | |
| 16391 | + /// copies it over every unit the block SPANS, so the four units of a 128 | |
| 16392 | + /// root share one index -- while the encoder's CDEF search prices a | |
| 16393 | + /// preset per unit and its filter replay applied all four. Only the first | |
| 16394 | + /// reached the stream, so every decoder filtered the other three with the | |
| 16395 | + /// origin's strengths and the encoder's reconstruction (the picture the | |
| 16396 | + /// next frame predicts from) was a filter apart from what a decoder | |
| 16397 | + /// produces. RED before this lane on the native BD gate's bars 2160p row | |
| 16398 | + /// (`EC_AV1_B128RES=1`, q=90, frame 11: 8 luma and 702 chroma samples, | |
| 16399 | + /// the rows above the 128 superblock's own edge); the stream itself was | |
| 16400 | + /// always right -- ffmpeg and our own decoder agreed with each other and | |
| 16401 | + /// disagreed with the encoder. | |
| 16402 | + /// | |
| 16403 | + /// `take_cdef_covered_units` is the gate on the fixture (class | |
| 16404 | + /// `gate-blind-to-feature`): a clip whose CDEF picks one strength pair | |
| 16405 | + /// for the whole frame codes no literal at all and witnesses nothing. | |
| 16406 | + /// | |
| 16407 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 16408 | + /// a_128_root_residual_block_under_a_per_unit_cdef --nocapture | |
| 16409 | + #[test] | |
| 16410 | + #[ignore = "sets the process-global superblock size: run it alone"] | |
| 16411 | + fn a_128_root_residual_block_under_a_per_unit_cdef_list_decodes_exact() { | |
| 16412 | + let _knobs = crate::speed::knob_write(); | |
| 16413 | + if !have_ffmpeg() { | |
| 16414 | + eprintln!("SKIP: no ffmpeg"); | |
| 16415 | + return; | |
| 16416 | + } | |
| 16417 | + force_sb128(true); | |
| 16418 | + let dir = std::env::temp_dir().join(format!("ec-av1-edge128-{}", std::process::id())); | |
| 16419 | + std::fs::create_dir_all(&dir).unwrap(); | |
| 16420 | + let clip = dir.join("testsrc2-1280x768.y4m"); | |
| 16421 | + // Detailed synthetic content: its CDEF search spends more than one | |
| 16422 | + // strength pair, which is what puts a `cdef_idx` literal in the tile. | |
| 16423 | + let ok = Command::new("ffmpeg") | |
| 16424 | + .args(["-v", "error", "-y", "-f", "lavfi"]) | |
| 16425 | + .args(["-i", "testsrc2=size=1280x768:rate=24"]) | |
| 16426 | + .args(["-frames:v", "12", "-pix_fmt", "yuv420p"]) | |
| 16427 | + .arg(&clip) | |
| 16428 | + .status() | |
| 16429 | + .expect("ffmpeg failed to run") | |
| 16430 | + .success(); | |
| 16431 | + assert!(ok, "ffmpeg could not write the synthetic clip"); | |
| 16432 | + let (width, height) = (1280usize, 768usize); | |
| 16433 | + let pictures = | |
| 16434 | + crate::probe::source(clip.to_str().unwrap(), "0", "null", width, height, 12); | |
| 16435 | + force_b128_root(true); | |
| 16436 | + force_b128_residual(true); | |
| 16437 | + let _ = crate::tile::take_sb128_residual_hits(); | |
| 16438 | + let _ = crate::filter_search::take_cdef_covered_units(); | |
| 16439 | + let encoded = encode_sequence(&pictures, 90, 0.5).unwrap(); | |
| 16440 | + let coded = crate::tile::take_sb128_residual_hits(); | |
| 16441 | + let covered = crate::filter_search::take_cdef_covered_units(); | |
| 16442 | + eprintln!( | |
| 16443 | + "128x128 blocks WITH a residual: writer coded {coded}; 64x64 CDEF units taking \ | |
| 16444 | + another unit's literal: {covered}" | |
| 16445 | + ); | |
| 16446 | + assert!(coded > 0, "no 128x128 block carried a residual on this clip"); | |
| 16447 | + assert!( | |
| 16448 | + covered > 0, | |
| 16449 | + "no 64x64 unit took a non-zero preset from another unit's literal, so this clip \ | |
| 16450 | + cannot witness the rule", | |
| 16451 | + ); | |
| 16452 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | |
| 16453 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | |
| 16454 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | |
| 16455 | + for (plane, got, want) in [ | |
| 16456 | + ("luma", &dec.y, &frame.reconstruction.y), | |
| 16457 | + ("U", &dec.u, &frame.reconstruction.u), | |
| 16458 | + ("V", &dec.v, &frame.reconstruction.v), | |
| 16459 | + ] { | |
| 16460 | + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); | |
| 16461 | + assert_eq!(differ, 0, "frame {i}: {plane} -- {differ} samples differ"); | |
| 16462 | + } | |
| 16463 | + } | |
| 16464 | + let ff = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); | |
| 16465 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&ff).enumerate() { | |
| 16466 | + assert_eq!(dec.y, frame.reconstruction.y, "frame {i}: ffmpeg luma"); | |
| 16467 | + assert_eq!(dec.u, frame.reconstruction.u, "frame {i}: ffmpeg U"); | |
| 16468 | + assert_eq!(dec.v, frame.reconstruction.v, "frame {i}: ffmpeg V"); | |
| 16469 | + } | |
| 16470 | + } | |
| 16471 | + | |
| 16379 | 16472 | /// lane-b128r: the COMPOUND arm at the 128 root -- one 128x128 block |
| 16380 | 16473 | /// naming `LAST` plus `GOLDEN`/`ALTREF`, through the same |
| 16381 | 16474 | /// `write_compound_block` chain every smaller compound block takes, at a |
| @@ -17606,32 +17699,6 @@ mod tests { | ||
| 17606 | 17699 | ); |
| 17607 | 17700 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 17608 | 17701 | if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { |
| 17609 | - // TEMPORARY (lane-b128r bisect): the whole mismatch map. | |
| 17610 | - let (l, r) = match plane { | |
| 17611 | - "Y" => (&d.y, &e.reconstruction.y), | |
| 17612 | - "U" => (&d.u, &e.reconstruction.u), | |
| 17613 | - _ => (&d.v, &e.reconstruction.v), | |
| 17614 | - }; | |
| 17615 | - let w = if plane == "Y" { width } else { width / 2 }; | |
| 17616 | - let bad: Vec<usize> = l | |
| 17617 | - .iter() | |
| 17618 | - .zip(r.iter()) | |
| 17619 | - .enumerate() | |
| 17620 | - .filter(|(_, (a, b))| a != b) | |
| 17621 | - .map(|(i, _)| i) | |
| 17622 | - .collect(); | |
| 17623 | - let rows: Vec<usize> = bad.iter().map(|i| i / w).collect(); | |
| 17624 | - let cols: Vec<usize> = bad.iter().map(|i| i % w).collect(); | |
| 17625 | - eprintln!( | |
| 17626 | - "B128R MISMATCH {name} q={q} frame {i} plane {plane}: {} samples, \ | |
| 17627 | - rows {}..{}, cols {}..{}, first 8 {:?}", | |
| 17628 | - bad.len(), | |
| 17629 | - rows.iter().min().copied().unwrap_or(0), | |
| 17630 | - rows.iter().max().copied().unwrap_or(0), | |
| 17631 | - cols.iter().min().copied().unwrap_or(0), | |
| 17632 | - cols.iter().max().copied().unwrap_or(0), | |
| 17633 | - bad.iter().take(8).map(|i| (i / w, i % w)).collect::<Vec<_>>(), | |
| 17634 | - ); | |
| 17635 | 17702 | panic!( |
| 17636 | 17703 | "{name} q={q} frame {i} plane {plane} sample {s}: ffmpeg decoded \ |
| 17637 | 17704 | {got}, the encoder reconstructed {want}" |
crates/ec-av1/src/filter_search.rsmodified+39 -2
| @@ -48,6 +48,18 @@ thread_local! { | ||
| 48 | 48 | const { std::cell::RefCell::new(Vec::new()) }; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | +/// lane-edge128: 64x64 CDEF units that took a NON-ZERO preset index from | |
| 52 | +/// another unit's `cdef_idx` literal -- the units of a whole 128x128 root | |
| 53 | +/// block, the only shape whose literal covers more than one unit. Zero makes | |
| 54 | +/// any witness for that rule vacuous (class `gate-blind-to-feature`). | |
| 55 | +static CDEF_COVERED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 56 | + | |
| 57 | +/// Takes and clears [`CDEF_COVERED`]. | |
| 58 | +#[allow(dead_code)] // read from the `#[cfg(test)]` witness | |
| 59 | +pub(crate) fn take_cdef_covered_units() -> usize { | |
| 60 | + CDEF_COVERED.swap(0, std::sync::atomic::Ordering::Relaxed) | |
| 61 | +} | |
| 62 | + | |
| 51 | 63 | /// Takes and clears [`PRESET_COUNTS`]. |
| 52 | 64 | #[allow(dead_code)] // read from the `#[cfg(test)]` BD gate |
| 53 | 65 | pub(crate) fn take_cdef_presets() -> Vec<(u8, Vec<usize>)> { |
| @@ -203,6 +215,12 @@ pub(crate) fn pick_filters( | ||
| 203 | 215 | damping: u8, |
| 204 | 216 | (sb_cols, sb_rows): (usize, usize), |
| 205 | 217 | lambda: f64, |
| 218 | + // lane-edge128: which unit's `cdef_idx` literal covers each 64x64 unit | |
| 219 | + // (`crate::tile::cdef_unit_owner`). A unit covered by ANOTHER unit's | |
| 220 | + // literal -- the four units of a whole 128x128 root block -- cannot be | |
| 221 | + // given a preset of its own, so its error is scored into its owner's and | |
| 222 | + // it takes its owner's index. Empty means the identity map. | |
| 223 | + cdef_owner: &[u32], | |
| 206 | 224 | ) -> Result<(LoopFilterParams, CdefParams, Vec<u8>, Picture)> { |
| 207 | 225 | let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2)); |
| 208 | 226 | let nsb = sb_cols * sb_rows; |
| @@ -252,11 +270,19 @@ pub(crate) fn pick_filters( | ||
| 252 | 270 | st.chroma.push((ckey, (sse, units))); |
| 253 | 271 | } |
| 254 | 272 | drop(sst); |
| 255 | - let (sse_y, sse_uv, sse64) = { | |
| 273 | + let (sse_y, sse_uv, mut sse64): (u64, u64, Vec<u64>) = { | |
| 256 | 274 | let l = &st.luma.iter().find(|(k, _)| *k == lkey).expect("just inserted").1; |
| 257 | 275 | let c = &st.chroma.iter().find(|(k, _)| *k == ckey).expect("just inserted").1; |
| 258 | 276 | (l.0, c.0, l.1.iter().zip(&c.1).map(|(a, b)| a + b).collect()) |
| 259 | 277 | }; |
| 278 | + // A unit that codes no literal of its own carries its error into the | |
| 279 | + // one that does, so every choice below is made per GROUP. | |
| 280 | + for (u, &owner) in cdef_owner.iter().enumerate() { | |
| 281 | + if owner as usize != u { | |
| 282 | + sse64[owner as usize] += sse64[u]; | |
| 283 | + sse64[u] = 0; | |
| 284 | + } | |
| 285 | + } | |
| 260 | 286 | st.trials.push(Trial { cand, picture, sse_y, sse_uv, sse64 }); |
| 261 | 287 | Ok(st.trials.len() - 1) |
| 262 | 288 | }; |
| @@ -382,7 +408,7 @@ pub(crate) fn pick_filters( | ||
| 382 | 408 | bits = b; |
| 383 | 409 | } |
| 384 | 410 | } |
| 385 | - let grid: Vec<u8> = if bits == 0 { | |
| 411 | + let mut grid: Vec<u8> = if bits == 0 { | |
| 386 | 412 | Vec::new() |
| 387 | 413 | } else { |
| 388 | 414 | let n = 1usize << bits; |
| @@ -394,6 +420,17 @@ pub(crate) fn pick_filters( | ||
| 394 | 420 | }) |
| 395 | 421 | .collect() |
| 396 | 422 | }; |
| 423 | + // The covered units scored zero above, so they would all read preset 0 | |
| 424 | + // here: give them the index their owner's literal actually carries. | |
| 425 | + if !grid.is_empty() { | |
| 426 | + for u in 0..nsb { | |
| 427 | + let owner = cdef_owner.get(u).map_or(u, |&o| o as usize); | |
| 428 | + if owner != u && grid[owner] != 0 { | |
| 429 | + CDEF_COVERED.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 430 | + } | |
| 431 | + grid[u] = grid[owner]; | |
| 432 | + } | |
| 433 | + } | |
| 397 | 434 | let mut params = cdef(best, damping); |
| 398 | 435 | params.bits = bits; |
| 399 | 436 | for (i, c) in presets.iter().take(1usize << bits).enumerate() { |
crates/ec-av1/src/tile.rsmodified+38 -0
| @@ -669,6 +669,44 @@ impl TileLayout { | ||
| 669 | 669 | } |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | +/// lane-edge128: which 64x64 CDEF unit's `cdef_idx` literal covers each unit | |
| 673 | +/// of this frame, in the same raster order [`arm_cdef_idx`]'s grid is in. | |
| 674 | +/// | |
| 675 | +/// `read_cdef` (spec 5.11.56) codes ONE literal for the block that first | |
| 676 | +/// reaches a unit and copies it over every unit that block SPANS, so a whole | |
| 677 | +/// 128x128 root block puts one literal in the stream for all four of its | |
| 678 | +/// units -- the encoder's CDEF search, which prices a preset per unit, may | |
| 679 | +/// not hand those four units different presets: only the first would be | |
| 680 | +/// coded, and every decoder (ours and ffmpeg's alike) would apply it to all | |
| 681 | +/// four while the encoder's own filter replay applied four different ones. | |
| 682 | +/// That is the shape this map exists to collapse | |
| 683 | +/// ([`crate::filter_search::pick_filters`]). | |
| 684 | +/// | |
| 685 | +/// Identity everywhere else: every block this writer codes at 64x64 or below | |
| 686 | +/// covers exactly one unit. | |
| 687 | +pub(crate) fn cdef_unit_owner(blocks: &[Quadrant], mi_cols: u32, mi_rows: u32) -> Vec<u32> { | |
| 688 | + let (cols, rows) = block_grid(mi_cols, mi_rows); | |
| 689 | + let (sb_cols, sb_rows) = (cols.div_ceil(2) as usize, rows.div_ceil(2) as usize); | |
| 690 | + let mut owner: Vec<u32> = (0..(sb_cols * sb_rows) as u32).collect(); | |
| 691 | + for sb_r in (0..sb_rows).step_by(2) { | |
| 692 | + for sb_c in (0..sb_cols).step_by(2) { | |
| 693 | + let root = ((sb_r * 2) < rows as usize && (sb_c * 2) < cols as usize) | |
| 694 | + .then(|| &blocks[(sb_r * 2) * cols as usize + sb_c * 2]) | |
| 695 | + .is_some_and(|q| matches!(q, Quadrant::Whole128(_))); | |
| 696 | + if !root { | |
| 697 | + continue; | |
| 698 | + } | |
| 699 | + let origin = (sb_r * sb_cols + sb_c) as u32; | |
| 700 | + for r in sb_r..(sb_r + 2).min(sb_rows) { | |
| 701 | + for c in sb_c..(sb_c + 2).min(sb_cols) { | |
| 702 | + owner[r * sb_cols + c] = origin; | |
| 703 | + } | |
| 704 | + } | |
| 705 | + } | |
| 706 | + } | |
| 707 | + owner | |
| 708 | +} | |
| 709 | + | |
| 672 | 710 | /// One frame's per-superblock `cdef_idx` plan (see [`arm_cdef_idx`]). |
| 673 | 711 | struct CdefIdxPlan { |
| 674 | 712 | /// The header's `cdef_bits`; never 0 while armed. |
lanes/edge128.report.mdadded+167 -0
| @@ -0,0 +1,167 @@ | ||
| 1 | +# lane-edge128 — the 8-sample diff at the 128 superblock's edge | |
| 2 | + | |
| 3 | +The charter's premise was that reader and writer shared a wrong assumption | |
| 4 | +about something read per 64x64 unit under a 128 block (deblock levels, tx | |
| 5 | +sizes, skip, `delta_lf`, LR). **They do not.** The bitstream was always | |
| 6 | +right: on the exact stream that fails, ffmpeg's decode and this crate's | |
| 7 | +`decode_stream` are byte-identical over all 12 frames and all three planes. | |
| 8 | +What disagreed with both of them is the ENCODER's own reconstruction — the | |
| 9 | +picture the next frame predicts from — and the stage is CDEF, on the | |
| 10 | +encoder's side of it alone. | |
| 11 | + | |
| 12 | +## 1. Reproduced | |
| 13 | + | |
| 14 | +`bd_rate_screen_native` with `EC_AV1_B128RES=1 EC_AV1_NATIVE_FILM4K=1`: | |
| 15 | + | |
| 16 | + B128R MISMATCH bars 2160p q=90 frame 11 plane Y: 8 samples, | |
| 17 | + rows 126..127, cols 1611..1616 | |
| 18 | + panicked: sample 243531: ffmpeg decoded 106, the encoder reconstructed 107 | |
| 19 | + | |
| 20 | +The gate's own q=90 stream (dumped from the failing run) is **byte-identical** | |
| 21 | +to the one `enc_probe` writes for that row (411920 bytes, `fnv1a | |
| 22 | +d36717e86823f28f`), so the defect reproduces outside the gate. Decoding that | |
| 23 | +stream two ways: | |
| 24 | + | |
| 25 | +| pair | differing samples, frame 11 | | |
| 26 | +|---|---| | |
| 27 | +| ffmpeg vs `decode_stream` | **0** (and 0 over the whole 12-frame sequence) | | |
| 28 | +| encoder reconstruction vs either decoder | **710** — Y 8, U 9, V 693 | | |
| 29 | + | |
| 30 | +The luma 8 the gate reports are the visible corner of a 710-sample block: | |
| 31 | +V rows 0..63 cols 801..815 and U rows 62..63 cols 802..809 in chroma | |
| 32 | +samples, i.e. **luma columns 1600..1663 of the 128x128 superblock at columns | |
| 33 | +1536..1663** — the RIGHT half of one 128 superblock, its whole height. Not a | |
| 34 | +band along an edge: two whole 64x64 CDEF units. | |
| 35 | + | |
| 36 | +## 2. The first divergent stage, and the rule | |
| 37 | + | |
| 38 | +There is no divergent DECODE stage — the pre-filter, post-deblock and | |
| 39 | +post-CDEF planes are moot when both decoders agree byte for byte with each | |
| 40 | +other. The divergence is between the stream and the encoder's | |
| 41 | +`Encoded::reconstruction`, which is not a decode of the final tile at all: | |
| 42 | +`pick_and_apply_filters` gets it from `crate::decode::replay_final`, a | |
| 43 | +replay of deblock+CDEF over the captured pre-filter planes driven by the | |
| 44 | +CDEF search's own per-64x64 `idx_grid`. | |
| 45 | + | |
| 46 | +**The rule the encoder broke** — `read_cdef`, spec 5.11.56: | |
| 47 | + | |
| 48 | + cdefSize4 = Num_4x4_Blocks_Wide[BLOCK_64X64] | |
| 49 | + r = MiRow & ~(cdefSize4-1); c = MiCol & ~(cdefSize4-1) | |
| 50 | + if (cdef_idx[r][c] == -1) { | |
| 51 | + cdef_idx[r][c] = L(cdef_bits) | |
| 52 | + for (i = r; i < r + h4; i += cdefSize4) | |
| 53 | + for (j = c; j < c + w4; j += cdefSize4) | |
| 54 | + cdef_idx[i][j] = cdef_idx[r][c] // every unit the BLOCK spans | |
| 55 | + } | |
| 56 | + | |
| 57 | +One literal per block, copied over every 64x64 unit that block spans. A | |
| 58 | +whole 128x128 root block therefore puts **one** `cdef_idx` in the stream for | |
| 59 | +all **four** of its units — which is exactly what `tile::write_cdef_idx` | |
| 60 | +writes (one literal per superblock, at the first non-skip block) and exactly | |
| 61 | +what `decode::maybe_read_cdef_idx` reads back since lane-b128r stamped the | |
| 62 | +block's mi span. Both are right. | |
| 63 | + | |
| 64 | +`filter_search::pick_filters` priced a preset per 64x64 unit and handed all | |
| 65 | +four to the replay. Only the origin unit's index reached the tile, so every | |
| 66 | +decoder filtered the other three with the origin's strengths while the | |
| 67 | +encoder's reconstruction carried three other strength pairs. Frame 11 is | |
| 68 | +where the residual 128 root and a per-unit `cdef_idx` list finally met. | |
| 69 | + | |
| 70 | +**Class: `plan finer than its syntax`** — an encoder-side per-unit plan for | |
| 71 | +a parameter the bitstream can only carry once per block. It is invisible to | |
| 72 | +every stream-level gate (the stream is legal and decodes identically | |
| 73 | +everywhere); it surfaces only where the encoder's reconstruction is compared | |
| 74 | +against a decoder's, and then as a reference-chain error, not a syntax one. | |
| 75 | + | |
| 76 | +## 3. The fix (one shared map, both consumers) | |
| 77 | + | |
| 78 | +`tile::cdef_unit_owner(blocks, mi_cols, mi_rows)` maps each 64x64 unit to | |
| 79 | +the unit whose literal covers it — identity for every block of one unit or | |
| 80 | +less, and the root's top-left unit for the four units of a `Whole128` root. | |
| 81 | +`pick_filters` takes it and decides per GROUP: a covered unit's per-unit SSE | |
| 82 | +is summed into its owner's before the preset histogram and the `cdef_bits` | |
| 83 | +cost are read off it, and after the grid is built each covered unit takes | |
| 84 | +its owner's index. The identity map (`&[]`, the key-frame path, which has no | |
| 85 | +128 root) is the behaviour this function always had. | |
| 86 | + | |
| 87 | +The map is built where the coded blocks are (`encode_inter_frame`) and | |
| 88 | +travels through `pick_and_apply_filters` to the search, so the writer's | |
| 89 | +`recode` and the encoder's filter replay read the same indices a decoder | |
| 90 | +does. | |
| 91 | + | |
| 92 | +## 4. Same-shape sweep — every per-64x64-unit plan under a 128 block | |
| 93 | + | |
| 94 | +Three writer-side plans are keyed per 64x64 unit; the sweep is all three: | |
| 95 | + | |
| 96 | +| plan | state | | |
| 97 | +|---|---| | |
| 98 | +| `cdef_idx` (`arm_cdef_idx`) | **the defect, fixed here** | | |
| 99 | +| `delta_q` (`arm_delta_q`) | already correct: `write_delta_q` gates on the 128-wide superblock (lane-b128m) AND `encode_inter_frame` collapses `sb_q` to the root's top-left cell before the search quantizes with it — the same shape, closed one lane earlier | | |
| 100 | +| loop restoration (`arm_lr`) | not per-64 at all: `lr_unit()` is 128 under `EC_AV1_SB128`, `write_lr` writes it once at the root, and `pick_restoration` builds its grid from the same `lr_unit()` | | |
| 101 | + | |
| 102 | +The reads the charter suspected — deblock filter levels, the tx-size lookup | |
| 103 | +for a TX_64X64 unit inside a 128 block, `delta_lf`, the per-unit CDEF skip | |
| 104 | +decision — are all in the DECODER, which the encoder runs verbatim as its | |
| 105 | +own filter stage, so reader and writer cannot disagree there by | |
| 106 | +construction; and ffmpeg agreeing with `decode_stream` sample for sample on | |
| 107 | +this exact stream clears them empirically as well. | |
| 108 | + | |
| 109 | +## 5. Witness | |
| 110 | + | |
| 111 | +`encode::tests::a_128_root_residual_block_under_a_per_unit_cdef_list_decodes_exact` | |
| 112 | +(`--ignored`, 128 superblocks + the 128 root + its residual arm forced), | |
| 113 | +`testsrc2` 1280x768, 12 frames, q=90: | |
| 114 | + | |
| 115 | + 128x128 blocks WITH a residual: writer coded 1440; | |
| 116 | + 64x64 CDEF units taking another unit's literal: 168 | |
| 117 | + test ... ok | |
| 118 | + | |
| 119 | +Both readers, every frame, every plane: `decode_stream` and ffmpeg against | |
| 120 | +the encoder's own reconstruction. `take_cdef_covered_units` is the gate on | |
| 121 | +the fixture (class `gate-blind-to-feature`): a clip whose CDEF spends one | |
| 122 | +strength pair codes no literal and would witness nothing. | |
| 123 | + | |
| 124 | +**RED before the fix** (collapse disabled on this same tree, everything else | |
| 125 | +identical): | |
| 126 | + | |
| 127 | + assertion `left == right` failed: frame 1: U -- 521 samples differ | |
| 128 | + | |
| 129 | +## 6. The residual arm, re-measured with the fix | |
| 130 | + | |
| 131 | +`bd_rate_screen_native`, 12 frames, `EC_AV1_B128RES=1` (control = the | |
| 132 | +standing table, i.e. the arm off): | |
| 133 | + | |
| 134 | +| row | control | + the residual arm, fixed | | |
| 135 | +|---|---|---| | |
| 136 | +| bars 2160p | +8.6 / −13.7 | +8.6 / −13.7 | | |
| 137 | +| film B (2160p) | +23.6 / −3.1 | +23.4 / −3.2 | | |
| 138 | + | |
| 139 | +This is the first COMPLETE measurement of the arm — before the fix the row | |
| 140 | +panicked instead of finishing. It stays **behind `EC_AV1_B128RES=1`**: the | |
| 141 | +arm is still inert on content (0–2 blocks per frame against ~90 whole 128 | |
| 142 | +roots) and the two rows are inside the flat band. What the lane buys is not | |
| 143 | +the arm; it is that the encoder's reconstruction is no longer a filter apart | |
| 144 | +from what a decoder produces the moment a 128 root carries a residual. | |
| 145 | + | |
| 146 | +**The default encoder is byte-identical**: both pins hold at default and at | |
| 147 | +`EC_AV1_SPEED=6`. They must — a default 128 root is always skipped, CDEF | |
| 148 | +does not filter a unit whose blocks are all skip, and the fold it now goes | |
| 149 | +through adds candidate-independent constants that cannot move an argmin. | |
| 150 | + | |
| 151 | +## 7. Suite / checks | |
| 152 | + | |
| 153 | + s1 --skip stream:: 344 passed; 0 failed; 38 ignored RC=0 | |
| 154 | + s2 stream:: --skip 10bit 202 passed; 0 failed; 15 ignored RC=0 | |
| 155 | + s3 10bit 42 passed; 0 failed; 1 ignored RC=0 | |
| 156 | + --ignored every_speed_preset_decodes_sample_exact_through_both_decoders ok | |
| 157 | + --ignored a_128_root_residual_block_under_a_per_unit_cdef_list... ok (1440 blocks, 168 covered units) | |
| 158 | + --ignored a_128_superblock_clip_whose_root_search... ok | |
| 159 | + --ignored a_128_root_block_with_a_real_residual... ok | |
| 160 | + --ignored a_128_root_compound_block... ok | |
| 161 | + the_encoders_own_streams_are_byte_identical_to_their_pins ok, and ok at EC_AV1_SPEED=6 | |
| 162 | + cargo check --workspace --all-targets -j4 0 errors, 0 ec-av1 warnings | |
| 163 | + | |
| 164 | +## Pins | |
| 165 | + | |
| 166 | +NOT re-taken: 8364 / 33257 stand, at default and at `EC_AV1_SPEED=6`. The | |
| 167 | +fix cannot reach a default stream (see §6). |