Merge branch 'lane-b128' (98bde66d) into lane-b128m: 128-root PARTITION_NONE search behind EC_AV1_SB128; pins kept (default off)
Browse files at this commit4 changed files +876 -25
- crates/ec-av1/src/decode.rs +10 -10
- crates/ec-av1/src/encode.rs +408 -9
- crates/ec-av1/src/tile.rs +252 -6
- lanes/b128.report.md +206 -0
crates/ec-av1/src/decode.rsmodified+10 -10
| @@ -38505,7 +38505,7 @@ mod tests { | |||
| 38505 | 38505 | ||
| 38506 | #[test] | 38506 | #[test] |
| 38507 | fn real_yuv_round_trips_bit_exact_against_the_encoder_reconstruction() { | 38507 | fn real_yuv_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 38508 | - let fctx = &crate::decode::FrameCtx::new(); | 38508 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 38509 | for (w, h) in [(128usize, 128usize), (192, 128)] { | 38509 | for (w, h) in [(128usize, 128usize), (192, 128)] { |
| 38510 | round_trips(w, h, &[DC_PRED as u8], fctx); | 38510 | round_trips(w, h, &[DC_PRED as u8], fctx); |
| 38511 | } | 38511 | } |
| @@ -38517,7 +38517,7 @@ mod tests { | |||
| 38517 | /// decoder round adds, not just `DC_PRED`. | 38517 | /// decoder round adds, not just `DC_PRED`. |
| 38518 | #[test] | 38518 | #[test] |
| 38519 | fn every_intra_mode_round_trips_bit_exact_against_the_encoder_reconstruction() { | 38519 | fn every_intra_mode_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 38520 | - let fctx = &crate::decode::FrameCtx::new(); | 38520 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 38521 | for (w, h) in [(128usize, 128usize), (192, 128)] { | 38521 | for (w, h) in [(128usize, 128usize), (192, 128)] { |
| 38522 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); | 38522 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); |
| 38523 | } | 38523 | } |
| @@ -38528,7 +38528,7 @@ mod tests { | |||
| 38528 | /// this decoder round adds. | 38528 | /// this decoder round adds. |
| 38529 | #[test] | 38529 | #[test] |
| 38530 | fn odd_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { | 38530 | fn odd_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { |
| 38531 | - let fctx = &crate::decode::FrameCtx::new(); | 38531 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: encodes, so the decode context takes this process's superblock size |
| 38532 | for (w, h) in [(854usize, 480usize), (216, 96)] { | 38532 | for (w, h) in [(854usize, 480usize), (216, 96)] { |
| 38533 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); | 38533 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); |
| 38534 | } | 38534 | } |
| @@ -38542,7 +38542,7 @@ mod tests { | |||
| 38542 | /// bit, `partition_w8` leaves). | 38542 | /// bit, `partition_w8` leaves). |
| 38543 | #[test] | 38543 | #[test] |
| 38544 | fn leaf8_straddle_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { | 38544 | fn leaf8_straddle_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { |
| 38545 | - let fctx = &crate::decode::FrameCtx::new(); | 38545 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 38546 | for (w, h) in [(40usize, 32usize), (640, 360)] { | 38546 | for (w, h) in [(40usize, 32usize), (640, 360)] { |
| 38547 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); | 38547 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES, fctx); |
| 38548 | } | 38548 | } |
| @@ -38838,7 +38838,7 @@ mod tests { | |||
| 38838 | /// crate's own writer and reader could otherwise share. | 38838 | /// crate's own writer and reader could otherwise share. |
| 38839 | #[test] | 38839 | #[test] |
| 38840 | fn ffmpeg_and_this_decoder_agree_on_a_key_frame() { | 38840 | fn ffmpeg_and_this_decoder_agree_on_a_key_frame() { |
| 38841 | - let fctx = &crate::decode::FrameCtx::new(); | 38841 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 38842 | if !have_ffmpeg() { | 38842 | if !have_ffmpeg() { |
| 38843 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_key_frame: no ffmpeg"); | 38843 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_key_frame: no ffmpeg"); |
| 38844 | return; | 38844 | return; |
| @@ -38885,7 +38885,7 @@ mod tests { | |||
| 38885 | /// between the palette colours and the colour-index maps). | 38885 | /// between the palette colours and the colour-index maps). |
| 38886 | #[test] | 38886 | #[test] |
| 38887 | fn a_filter_intra_key_frame_decodes_pixel_exact_through_ffmpeg() { | 38887 | fn a_filter_intra_key_frame_decodes_pixel_exact_through_ffmpeg() { |
| 38888 | - let fctx = &crate::decode::FrameCtx::new(); | 38888 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 38889 | if !crate::encode::filter_intra_on() { | 38889 | if !crate::encode::filter_intra_on() { |
| 38890 | eprintln!("SKIP a_filter_intra_key_frame...: EC_AV1_FILTER_INTRA=0"); | 38890 | eprintln!("SKIP a_filter_intra_key_frame...: EC_AV1_FILTER_INTRA=0"); |
| 38891 | return; | 38891 | return; |
| @@ -38955,7 +38955,7 @@ mod tests { | |||
| 38955 | /// wire). | 38955 | /// wire). |
| 38956 | #[test] | 38956 | #[test] |
| 38957 | fn a_key_frames_tile_needs_the_headers_own_tx_select_bit() { | 38957 | fn a_key_frames_tile_needs_the_headers_own_tx_select_bit() { |
| 38958 | - let fctx = &crate::decode::FrameCtx::new(); | 38958 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: encodes, so the decode context takes this process's superblock size |
| 38959 | use crate::encode::encode_key_frame_with_ctx; | 38959 | use crate::encode::encode_key_frame_with_ctx; |
| 38960 | let (width, height) = (64usize, 64usize); | 38960 | let (width, height) = (64usize, 64usize); |
| 38961 | let picture = round_trip_test_card(width, height); | 38961 | let picture = round_trip_test_card(width, height); |
| @@ -39155,7 +39155,7 @@ mod tests { | |||
| 39155 | 39155 | ||
| 39156 | #[test] | 39156 | #[test] |
| 39157 | fn a_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { | 39157 | fn a_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 39158 | - let fctx = &crate::decode::FrameCtx::new(); | 39158 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 39159 | gop_round_trips(128, 64, fctx); | 39159 | gop_round_trips(128, 64, fctx); |
| 39160 | } | 39160 | } |
| 39161 | 39161 | ||
| @@ -39164,7 +39164,7 @@ mod tests { | |||
| 39164 | /// split path is exercised too. | 39164 | /// split path is exercised too. |
| 39165 | #[test] | 39165 | #[test] |
| 39166 | fn an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { | 39166 | fn an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 39167 | - let fctx = &crate::decode::FrameCtx::new(); | 39167 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 39168 | gop_round_trips(216, 96, fctx); | 39168 | gop_round_trips(216, 96, fctx); |
| 39169 | } | 39169 | } |
| 39170 | 39170 | ||
| @@ -39394,7 +39394,7 @@ mod tests { | |||
| 39394 | 39394 | ||
| 39395 | #[test] | 39395 | #[test] |
| 39396 | fn ffmpeg_and_this_decoder_agree_on_a_gop() { | 39396 | fn ffmpeg_and_this_decoder_agree_on_a_gop() { |
| 39397 | - let fctx = &crate::decode::FrameCtx::new(); | 39397 | + let fctx = &crate::decode::FrameCtx::for_encoder(); // lane-b128: this test ENCODES, so its decode context starts at the superblock size this process encodes at (stale-header class, as `filter_intra_on` below) |
| 39398 | if !have_ffmpeg() { | 39398 | if !have_ffmpeg() { |
| 39399 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_gop: no ffmpeg"); | 39399 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_gop: no ffmpeg"); |
| 39400 | return; | 39400 | return; |
crates/ec-av1/src/encode.rsmodified+408 -9
| @@ -722,6 +722,27 @@ pub fn take_b64_root_hits() -> usize { | |||
| 722 | B64_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | 722 | B64_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) |
| 723 | } | 723 | } |
| 724 | 724 | ||
| 725 | +/// Whether the 128 superblock root offers its `PARTITION_NONE` candidate at | ||
| 726 | +/// all ([`search_root_128`]). On by default; `EC_AV1_B128=0` turns it off, | ||
| 727 | +/// which is the attribution arm for a 128-superblock stream: it separates | ||
| 728 | +/// what the SIZE costs from what the 128x128 BLOCK buys. | ||
| 729 | +fn b128_root() -> bool { | ||
| 730 | + static ENV: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| { | ||
| 731 | + crate::envflags::var("EC_AV1_B128").ok().is_none_or(|v| v != "0") | ||
| 732 | + }); | ||
| 733 | + *ENV | ||
| 734 | +} | ||
| 735 | + | ||
| 736 | +/// How many 128x128 `PARTITION_NONE` roots the search took since the last | ||
| 737 | +/// [`take_b128_none_hits`] (class `gate-blind-to-feature`): a flat BD result | ||
| 738 | +/// is only attributable once the fire count is known. | ||
| 739 | +static B128_NONE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 740 | + | ||
| 741 | +/// The 128x128-root count since the last call, and zero it. | ||
| 742 | +pub fn take_b128_none_hits() -> usize { | ||
| 743 | + B128_NONE_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 744 | +} | ||
| 745 | + | ||
| 725 | /// Rate-distortion optimised quantisation ([`crate::tile::rdoq`], lane-rdoq). | 746 | /// Rate-distortion optimised quantisation ([`crate::tile::rdoq`], lane-rdoq). |
| 726 | /// Preset-gated: it re-prices the block once per candidate level, so it buys | 747 | /// Preset-gated: it re-prices the block once per candidate level, so it buys |
| 727 | /// bits with wall (see `crate::speed::RDOQ` for the per-preset table). | 748 | /// bits with wall (see `crate::speed::RDOQ` for the per-preset table). |
| @@ -5861,11 +5882,40 @@ fn cfl_on() -> bool { | |||
| 5861 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured | 5882 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured |
| 5862 | /// its own on/off pair -- and how every byte pin written before it still | 5883 | /// its own on/off pair -- and how every byte pin written before it still |
| 5863 | /// reproduces). It is also a speed lever: on at preset 0, off above it. | 5884 | /// reproduces). It is also a speed lever: on at preset 0, off above it. |
| 5864 | -// lane-sb128: `use_128x128_superblock` (spec 5.5.1). Off by default until | 5885 | +// lane-sb128: `use_128x128_superblock` (spec 5.5.1). `EC_AV1_SB128=0` turns |
| 5865 | -// the BD gate says otherwise; `EC_AV1_SB128=1` turns it on for the whole | 5886 | +// it off for the whole process (a sequence-header field, so it cannot change |
| 5866 | -// process (a sequence-header field, so it cannot change mid-stream anyway), | 5887 | +// mid-stream anyway), the same env-flag shape `EC_AV1_FILTER_INTRA` uses. |
| 5867 | -// the same env-flag shape `EC_AV1_FILTER_INTRA` uses. NOT an `EncoderConfig` | 5888 | +// NOT an `EncoderConfig` field: that struct is built by 90 literal sites in |
| 5868 | -// field: that struct is built by 90 literal sites in this workspace. | 5889 | +// this workspace. |
| 5890 | + | ||
| 5891 | +/// Whether a sequence codes 128x128 superblocks when the environment says | ||
| 5892 | +/// nothing. ON since lane-b128 gave the 128 root a real `PARTITION_NONE` | ||
| 5893 | +/// candidate ([`search_root_128`]): with SPLIT-only roots the size was pure | ||
| 5894 | +/// cost (lane-sb128b measured film B +24.9 -> +37.5 vs libaom), and with the | ||
| 5895 | +/// 128x128 block in it every row of the native BD gate is down or flat on | ||
| 5896 | +/// both columns (BD-rate vs libaom cpu-6 / rav1e s6, 12 frames): | ||
| 5897 | +/// | ||
| 5898 | +/// | row | 64 superblocks | 128 + the 128 root | | ||
| 5899 | +/// |---|---|---| | ||
| 5900 | +/// | bars 1080p | -2.6 / -18.3 | **-3.3 / -18.9** | | ||
| 5901 | +/// | bars 2160p | +8.8 / -13.2 | **+7.9 / -14.0** | | ||
| 5902 | +/// | film A | +20.6 / -5.1 | **+20.4 / -5.2** | | ||
| 5903 | +/// | film B | +24.8 / -2.1 | **+24.2 / -2.5** | | ||
| 5904 | +/// | screen capture | +14.6 / -33.2 | **+14.4 / -33.2** | | ||
| 5905 | +/// | ||
| 5906 | +/// and the long-GOP gate agrees (film A +24.7/-7.5 -> +24.4/-7.7, film B | ||
| 5907 | +/// +86.8/+7.4 -> +85.6/+6.6). | ||
| 5908 | +/// | ||
| 5909 | +/// STILL OFF all the same, and this is the whole reason: the 128-superblock | ||
| 5910 | +/// PLUMBING under it (lane-sb128's SPLIT-only roots) still desyncs on eleven | ||
| 5911 | +/// of this crate's own round-trip tests -- odd frame sizes, doubly-cut | ||
| 5912 | +/// roots, multi-tile layouts and the `decode_stream` GOPs -- with the | ||
| 5913 | +/// refusal lane-sb128b named ("a reference frame selected with no picture at | ||
| 5914 | +/// this frame's own ref_frame_idx slot"). Those failures REPRODUCE with the | ||
| 5915 | +/// 128x128 block switched off (`EC_AV1_B128=0`), so they are not this lane's | ||
| 5916 | +/// block; flipping the default before they are fixed would ship a broken | ||
| 5917 | +/// encoder for a 0.6-point BD win. `EC_AV1_SB128=1` turns it on. | ||
| 5918 | +pub(crate) const SB128_DEFAULT: bool = false; | ||
| 5869 | 5919 | ||
| 5870 | /// Whether this process's sequences code 128x128 superblocks. An ENCODER | 5920 | /// Whether this process's sequences code 128x128 superblocks. An ENCODER |
| 5871 | /// knob (like `speed::SPEED`), never read by a decode: read through a | 5921 | /// knob (like `speed::SPEED`), never read by a decode: read through a |
| @@ -5878,7 +5928,9 @@ pub(crate) fn sb128_on() -> bool { | |||
| 5878 | use std::sync::atomic::Ordering::Relaxed; | 5928 | use std::sync::atomic::Ordering::Relaxed; |
| 5879 | match ENC_SB128.load(Relaxed) { | 5929 | match ENC_SB128.load(Relaxed) { |
| 5880 | 2 => { | 5930 | 2 => { |
| 5881 | - let on = crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0"); | 5931 | + let on = crate::envflags::var("EC_AV1_SB128") |
| 5932 | + .ok() | ||
| 5933 | + .map_or(SB128_DEFAULT, |v| v != "0"); | ||
| 5882 | ENC_SB128.store(u8::from(on), Relaxed); | 5934 | ENC_SB128.store(u8::from(on), Relaxed); |
| 5883 | on | 5935 | on |
| 5884 | } | 5936 | } |
| @@ -9269,6 +9321,113 @@ fn predict_compound_u8( | |||
| 9269 | blended.iter().map(|&v| v as u8).collect() | 9321 | blended.iter().map(|&v| v as u8).collect() |
| 9270 | } | 9322 | } |
| 9271 | 9323 | ||
| 9324 | +/// lane-b128: the 128x128 `PARTITION_NONE` candidate for a whole 128 | ||
| 9325 | +/// superblock root, the lever cen3's L1 asks for -- one block, one mode and | ||
| 9326 | +/// one motion vector where the four 64x64 roots below spend four of each. | ||
| 9327 | +/// | ||
| 9328 | +/// SKIP only (`residual: false`): a 128x128 block's residual is four | ||
| 9329 | +/// TX_64X64 luma units interleaved with a TX_32X32 chroma pair per 64x64 mu | ||
| 9330 | +/// chunk, which neither the writer nor [`Plane::code_from_prediction`] has a | ||
| 9331 | +/// path for -- offering one here would price a block | ||
| 9332 | +/// [`crate::tile::write_inter_block_128`] refuses. Single reference only for | ||
| 9333 | +/// the same reason: the compound arm at this size is untested against the | ||
| 9334 | +/// writer's own `write_compound_block` window. | ||
| 9335 | +/// | ||
| 9336 | +/// Only a root whose whole 128x128 lies inside the true frame AND inside | ||
| 9337 | +/// this tile is offered: the writer's own root codes `PARTITION_NONE` only | ||
| 9338 | +/// where the spec's `has_rows`/`has_cols` are both set, and a straddling | ||
| 9339 | +/// block would have to be ranked over its inside part alone. | ||
| 9340 | +/// | ||
| 9341 | +/// Returns `(cost, lambda, block, reconstruction)` -- the cost already | ||
| 9342 | +/// carries the root's own `PARTITION_NONE` symbol, so the caller weighs it | ||
| 9343 | +/// against the four superblocks plus the SPLIT symbol. | ||
| 9344 | +#[allow(clippy::too_many_arguments)] | ||
| 9345 | +fn search_root_128( | ||
| 9346 | + luma: &mut Plane, | ||
| 9347 | + chroma: &mut [Plane; 2], | ||
| 9348 | + grid: &crate::mvstack::MiGrid, | ||
| 9349 | + (root_r, root_c): (usize, usize), | ||
| 9350 | + // The 32x32-block grid this tile's `blocks` vector is indexed on. | ||
| 9351 | + (rows, cols): (usize, usize), | ||
| 9352 | + // The frame's own mode-info grid, which bounds the root. | ||
| 9353 | + (mi_cols, mi_rows): (u32, u32), | ||
| 9354 | + search: Search<'_>, | ||
| 9355 | + tpl_factors: &Option<Vec<f64>>, | ||
| 9356 | + sb_q: Option<&Vec<u8>>, | ||
| 9357 | + sb_cols: usize, | ||
| 9358 | + tpl_cells_x: usize, | ||
| 9359 | + base_q_idx: u8, | ||
| 9360 | + reference: &Picture, | ||
| 9361 | + fctx: &crate::decode::FrameCtx, | ||
| 9362 | +) -> Option<(f64, f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> { | ||
| 9363 | + const SIDE: usize = SUPERBLOCK * 2; | ||
| 9364 | + const SIDE_MI: usize = SIDE / 4; | ||
| 9365 | + let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); | ||
| 9366 | + let (mi_row, mi_col) = (root_r * 16, root_c * 16); | ||
| 9367 | + if root_r * 2 + 3 >= rows | ||
| 9368 | + || root_c * 2 + 3 >= cols | ||
| 9369 | + || (mi_row + SIDE_MI) as u32 > mi_rows | ||
| 9370 | + || (mi_col + SIDE_MI) as u32 > mi_cols | ||
| 9371 | + || x128 + SIDE > luma.true_width | ||
| 9372 | + || y128 + SIDE > luma.true_height | ||
| 9373 | + { | ||
| 9374 | + return None; | ||
| 9375 | + } | ||
| 9376 | + // One lambda for the whole root, the mean of what its four superblocks | ||
| 9377 | + // would each be searched at -- the 128-vs-four comparison weighs one cost | ||
| 9378 | + // against four, so both sides have to be priced at the same lambda (the | ||
| 9379 | + // same rule the 64 root's own mean-of-sixteen tpl cells follows). | ||
| 9380 | + let cell_lambda = |sb_r: usize, sb_c: usize| match (sb_q, tpl_factors) { | ||
| 9381 | + (Some(q), _) => sb_q_search(q, sb_cols, base_q_idx, (sb_r, sb_c), search).lambda, | ||
| 9382 | + (None, Some(f)) => { | ||
| 9383 | + let mean = (0..16) | ||
| 9384 | + .map(|i| f[(sb_r * 4 + i / 4) * tpl_cells_x + sb_c * 4 + i % 4]) | ||
| 9385 | + .sum::<f64>() | ||
| 9386 | + / 16.0; | ||
| 9387 | + search.lambda * mean | ||
| 9388 | + } | ||
| 9389 | + (None, None) => search.lambda, | ||
| 9390 | + }; | ||
| 9391 | + let lambda = [(0, 0), (0, 1), (1, 0), (1, 1)] | ||
| 9392 | + .iter() | ||
| 9393 | + .map(|&(dr, dc)| cell_lambda(root_r + dr, root_c + dc)) | ||
| 9394 | + .sum::<f64>() | ||
| 9395 | + / 4.0; | ||
| 9396 | + let root_search = Search { lambda, ..search }; | ||
| 9397 | + let stack = find_mv_stack( | ||
| 9398 | + grid, | ||
| 9399 | + mi_row, | ||
| 9400 | + mi_col, | ||
| 9401 | + SIDE_MI, | ||
| 9402 | + SIDE_MI, | ||
| 9403 | + LAST_FRAME, | ||
| 9404 | + mi_cols as usize, | ||
| 9405 | + mi_rows as usize, | ||
| 9406 | + ); | ||
| 9407 | + let base = snapshot(luma, chroma, (x128, y128), SIDE); | ||
| 9408 | + let found = search_skip_64( | ||
| 9409 | + luma, | ||
| 9410 | + chroma, | ||
| 9411 | + (x128, y128), | ||
| 9412 | + SIDE, | ||
| 9413 | + &root_search, | ||
| 9414 | + false, | ||
| 9415 | + reference, | ||
| 9416 | + &stack, | ||
| 9417 | + &[], | ||
| 9418 | + fctx, | ||
| 9419 | + ); | ||
| 9420 | + let (cost, block) = found?; | ||
| 9421 | + let after = snapshot(luma, chroma, (x128, y128), SIDE); | ||
| 9422 | + restore(luma, chroma, (x128, y128), SIDE, &base); | ||
| 9423 | + Some(( | ||
| 9424 | + cost + lambda * crate::tile::partition_bits(SIDE, false), | ||
| 9425 | + lambda, | ||
| 9426 | + block, | ||
| 9427 | + after, | ||
| 9428 | + )) | ||
| 9429 | +} | ||
| 9430 | + | ||
| 9272 | /// The 64x64 `PARTITION_NONE` candidate for a whole superblock ([`B64_ROOT`]): | 9431 | /// The 64x64 `PARTITION_NONE` candidate for a whole superblock ([`B64_ROOT`]): |
| 9273 | /// single-reference LAST at `NEARESTMV`/`GLOBALMV`/`NEARMV`/`NEWMV`, coded | 9432 | /// single-reference LAST at `NEARESTMV`/`GLOBALMV`/`NEARMV`/`NEWMV`, coded |
| 9274 | /// SKIP, priced through the same `symbol_bits` path every other candidate | 9433 | /// SKIP, priced through the same `symbol_bits` path every other candidate |
| @@ -9283,6 +9442,12 @@ fn search_skip_64( | |||
| 9283 | luma: &mut Plane, | 9442 | luma: &mut Plane, |
| 9284 | chroma: &mut [Plane; 2], | 9443 | chroma: &mut [Plane; 2], |
| 9285 | (x, y): (usize, usize), | 9444 | (x, y): (usize, usize), |
| 9445 | + // The root's side in samples: [`SUPERBLOCK`] at a 64 root, twice that at | ||
| 9446 | + // the 128 one [`search_root_128`] drives (lane-b128). Every trial below | ||
| 9447 | + // is a square of this side and its half in chroma, and the residual arm | ||
| 9448 | + // is only ever asked for at 64 -- a 128 block's residual is four | ||
| 9449 | + // TX_64X64 units the writer has no path for. | ||
| 9450 | + side: usize, | ||
| 9286 | search: &Search, | 9451 | search: &Search, |
| 9287 | // Whether the non-skip arm is priced at all ([`b64_residual`], and off on | 9452 | // Whether the non-skip arm is priced at all ([`b64_residual`], and off on |
| 9288 | // screen content -- see that function's doc). | 9453 | // screen content -- see that function's doc). |
| @@ -9295,7 +9460,7 @@ fn search_skip_64( | |||
| 9295 | compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], | 9460 | compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], |
| 9296 | fctx: &crate::decode::FrameCtx, | 9461 | fctx: &crate::decode::FrameCtx, |
| 9297 | ) -> Option<(f64, BlockCoeffs)> { | 9462 | ) -> Option<(f64, BlockCoeffs)> { |
| 9298 | - let side = SUPERBLOCK; | 9463 | + debug_assert!(side == SUPERBLOCK || !residual, "a 128 root has no residual arm"); |
| 9299 | let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); | 9464 | let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); |
| 9300 | let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); | 9465 | let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); |
| 9301 | let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); | 9466 | let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); |
| @@ -11674,8 +11839,63 @@ pub(crate) fn encode_inter_frame( | |||
| 11674 | rect.mi_col1 as usize, | 11839 | rect.mi_col1 as usize, |
| 11675 | ); | 11840 | ); |
| 11676 | let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); | 11841 | let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); |
| 11677 | - for sb_r in rect.sb_row0 as usize..rect.sb_row1 as usize { | 11842 | + // lane-b128: under a 128x128 superblock the search walks the roots the |
| 11678 | - for sb_c in rect.sb_col0 as usize..rect.sb_col1 as usize { | 11843 | + // WRITER walks (`tile::sb_write_order`: TL, TR, BL, BR inside each root), |
| 11844 | + // not plain raster -- a search that visits BL before TR builds every mv | ||
| 11845 | + // stack and neighbour context off blocks the writer has not coded yet, | ||
| 11846 | + // which is what made the write-time drl clamp fire at all (lane-sb128b). | ||
| 11847 | + // At a 64 superblock the two orders are identical and this is the same | ||
| 11848 | + // raster loop as before. | ||
| 11849 | + let sb128_search = sb128_on(); | ||
| 11850 | + let root_step = if sb128_search { 2 } else { 1 }; | ||
| 11851 | + let mut roots: Vec<(usize, usize)> = Vec::new(); | ||
| 11852 | + { | ||
| 11853 | + let (r0, r1) = (rect.sb_row0 as usize, rect.sb_row1 as usize); | ||
| 11854 | + let (c0, c1) = (rect.sb_col0 as usize, rect.sb_col1 as usize); | ||
| 11855 | + let mut r = r0; | ||
| 11856 | + while r < r1 { | ||
| 11857 | + let mut c = c0; | ||
| 11858 | + while c < c1 { | ||
| 11859 | + roots.push((r, c)); | ||
| 11860 | + c += root_step; | ||
| 11861 | + } | ||
| 11862 | + r += root_step; | ||
| 11863 | + } | ||
| 11864 | + } | ||
| 11865 | + for (root_r, root_c) in roots { | ||
| 11866 | + // The 64x64 cells of this root, in the writer's own order. | ||
| 11867 | + let cells: Vec<(usize, usize)> = if sb128_search { | ||
| 11868 | + [(0, 0), (0, 1), (1, 0), (1, 1)] | ||
| 11869 | + .into_iter() | ||
| 11870 | + .map(|(dr, dc)| (root_r + dr, root_c + dc)) | ||
| 11871 | + .filter(|&(r, c)| r < rect.sb_row1 as usize && c < rect.sb_col1 as usize) | ||
| 11872 | + .collect() | ||
| 11873 | + } else { | ||
| 11874 | + vec![(root_r, root_c)] | ||
| 11875 | + }; | ||
| 11876 | + // What the four cells below really cost, against which the whole | ||
| 11877 | + // 128x128 candidate is weighed. | ||
| 11878 | + let mark128 = blocks.len(); | ||
| 11879 | + let mut root_cost = 0.0f64; | ||
| 11880 | + let root128 = (sb128_search && b128_root() && cells.len() == 4) | ||
| 11881 | + .then(|| search_root_128( | ||
| 11882 | + &mut luma, | ||
| 11883 | + &mut chroma, | ||
| 11884 | + &grid, | ||
| 11885 | + (root_r, root_c), | ||
| 11886 | + (rows, cols), | ||
| 11887 | + (header.mi_cols, header.mi_rows), | ||
| 11888 | + search, | ||
| 11889 | + &tpl_factors, | ||
| 11890 | + sb_q.as_ref(), | ||
| 11891 | + sb_cols, | ||
| 11892 | + tpl_cells_x, | ||
| 11893 | + base_q_idx, | ||
| 11894 | + reference, | ||
| 11895 | + fctx, | ||
| 11896 | + )) | ||
| 11897 | + .flatten(); | ||
| 11898 | + for (sb_r, sb_c) in cells { | ||
| 11679 | // lane-b64: the whole superblock as ONE 64x64 block, tried before | 11899 | // lane-b64: the whole superblock as ONE 64x64 block, tried before |
| 11680 | // its quadrants are (`search_skip_64`) and compared against the | 11900 | // its quadrants are (`search_skip_64`) and compared against the |
| 11681 | // four of them below. Only a superblock wholly inside the true | 11901 | // four of them below. Only a superblock wholly inside the true |
| @@ -11774,6 +11994,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11774 | &mut luma, | 11994 | &mut luma, |
| 11775 | &mut chroma, | 11995 | &mut chroma, |
| 11776 | (x64, y64), | 11996 | (x64, y64), |
| 11997 | + SUPERBLOCK, | ||
| 11777 | &sb_search, | 11998 | &sb_search, |
| 11778 | b64_residual() && !screen, | 11999 | b64_residual() && !screen, |
| 11779 | reference, | 12000 | reference, |
| @@ -12243,6 +12464,27 @@ pub(crate) fn encode_inter_frame( | |||
| 12243 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); | 12464 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); |
| 12244 | blocks.push((r32 * cols + c32, Quadrant::Covered)); | 12465 | blocks.push((r32 * cols + c32, Quadrant::Covered)); |
| 12245 | } | 12466 | } |
| 12467 | + root_cost += cost64; | ||
| 12468 | + } else { | ||
| 12469 | + root_cost += sb_cost; | ||
| 12470 | + } | ||
| 12471 | + } | ||
| 12472 | + // lane-b128: the whole 128x128 root (`PARTITION_NONE` at the root's | ||
| 12473 | + // `partition_w128` symbol) against what its four superblocks really | ||
| 12474 | + // cost, the same weighing the 64 root does one size down. Both sides | ||
| 12475 | + // carry their own partition symbol, so the comparison is complete. | ||
| 12476 | + if let Some((cost128, lambda128, block, after)) = root128 | ||
| 12477 | + && cost128 < root_cost + lambda128 * crate::tile::partition_bits(128, true) | ||
| 12478 | + { | ||
| 12479 | + B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 12480 | + let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); | ||
| 12481 | + blocks.truncate(mark128); | ||
| 12482 | + restore(&mut luma, &mut chroma, (x128, y128), SUPERBLOCK * 2, &after); | ||
| 12483 | + record_mi(&mut grid, root_r * 16, root_c * 16, 32, block.inter, block.skip); | ||
| 12484 | + blocks.push(((root_r * 2) * cols + root_c * 2, Quadrant::Whole128(block))); | ||
| 12485 | + for q in 1..16 { | ||
| 12486 | + let (r32, c32) = (root_r * 2 + q / 4, root_c * 2 + q % 4); | ||
| 12487 | + blocks.push((r32 * cols + c32, Quadrant::Covered)); | ||
| 12246 | } | 12488 | } |
| 12247 | } | 12489 | } |
| 12248 | } | 12490 | } |
| @@ -12269,6 +12511,24 @@ pub(crate) fn encode_inter_frame( | |||
| 12269 | blocks[at] = quadrant; | 12511 | blocks[at] = quadrant; |
| 12270 | } | 12512 | } |
| 12271 | 12513 | ||
| 12514 | + // lane-b128, `EC_AV1_B128_CENSUS=1`: how many of this INTER frame's 128 | ||
| 12515 | + // superblock roots the search left whole, so a flat BD result is | ||
| 12516 | + // attributable (class `gate-blind-to-feature`). A key frame codes none: | ||
| 12517 | + // only the inter writer has a 128x128 block. | ||
| 12518 | + if crate::envflags::env_flag!("EC_AV1_B128_CENSUS") { | ||
| 12519 | + let whole = blocks | ||
| 12520 | + .iter() | ||
| 12521 | + .filter(|q| matches!(q, Quadrant::Whole128(_))) | ||
| 12522 | + .count(); | ||
| 12523 | + let roots = blocks | ||
| 12524 | + .iter() | ||
| 12525 | + .filter(|q| !matches!(q, Quadrant::Covered)) | ||
| 12526 | + .count(); | ||
| 12527 | + eprintln!( | ||
| 12528 | + "B128 census: inter frame, {whole} whole 128x128 roots, {roots} coded blocks ({:.1}% of the frame's area at 128)", | ||
| 12529 | + 100.0 * (whole * 16) as f64 / blocks.len().max(1) as f64, | ||
| 12530 | + ); | ||
| 12531 | + } | ||
| 12272 | let modes = blocks | 12532 | let modes = blocks |
| 12273 | .iter() | 12533 | .iter() |
| 12274 | .flat_map(Quadrant::blocks) | 12534 | .flat_map(Quadrant::blocks) |
| @@ -12750,6 +13010,57 @@ mod tests { | |||
| 12750 | /// An [`crate::mvstack::MvStack`] with every context at zero and no | 13010 | /// An [`crate::mvstack::MvStack`] with every context at zero and no |
| 12751 | /// neighbours, for the pricer tests below to fill the one band each of | 13011 | /// neighbours, for the pricer tests below to fill the one band each of |
| 12752 | /// them is about. | 13012 | /// them is about. |
| 13013 | + /// lane-b128 task 0: every `ref_mv_idx` the `NEWMV`/`NEARMV` pricer | ||
| 13014 | + /// offers must be one [`crate::tile::write_drl_idx`] can really SIGNAL | ||
| 13015 | + /// against the same stack -- an offer the writer clamps prices the | ||
| 13016 | + /// residual against a predictor the decoder never derives, which is the | ||
| 13017 | + /// silent MV drift lane-sb128b root-caused (there fixed on the WRITER | ||
| 13018 | + /// side, so the stream stays legal; here the pricer's own offer set is | ||
| 13019 | + /// enumerated, class `enumerate-table-domain`). | ||
| 13020 | + /// | ||
| 13021 | + /// cargo test -p ec-av1 --lib -- every_drl_index_the_new_mv_pricer | ||
| 13022 | + #[test] | ||
| 13023 | + fn every_drl_index_the_new_mv_pricer_offers_is_one_the_writer_can_signal() { | ||
| 13024 | + for n in 0..=4usize { | ||
| 13025 | + let mut stack = bare_stack(); | ||
| 13026 | + for i in 0..n { | ||
| 13027 | + stack.entries.push(crate::mvstack::StackEntry { | ||
| 13028 | + mv: (8 * (i as i32 + 1), -8 * (i as i32 + 1)), | ||
| 13029 | + weight: 1, | ||
| 13030 | + }); | ||
| 13031 | + stack.drl_ctx.push(0); | ||
| 13032 | + } | ||
| 13033 | + stack.nearest_mv = stack.entries.first().map_or((0, 0), |e| e.mv); | ||
| 13034 | + stack.near_mv = stack.entries.get(1).map_or((0, 0), |e| e.mv); | ||
| 13035 | + stack.pred_mv = stack.nearest_mv; | ||
| 13036 | + // Aim the vector at each entry in turn, so every index the offer | ||
| 13037 | + // set holds is the one that wins at least once. | ||
| 13038 | + for target in 0..n.max(1) { | ||
| 13039 | + let mv = stack.entries.get(target).map_or(stack.pred_mv, |e| e.mv); | ||
| 13040 | + let (_, idx) = best_new_mv_syntax(&stack, mv) | ||
| 13041 | + .expect("a zero residual is always codable"); | ||
| 13042 | + let idx = usize::from(idx); | ||
| 13043 | + assert_eq!( | ||
| 13044 | + crate::tile::signalled_drl_idx(n, 0, idx), | ||
| 13045 | + idx, | ||
| 13046 | + "NEWMV: a {n}-entry stack cannot signal the offered drl index {idx}", | ||
| 13047 | + ); | ||
| 13048 | + } | ||
| 13049 | + // The NEARMV arm's own offer set (`search_inter_block`, | ||
| 13050 | + // `search_skip_64`): indices 1 and 2, offered only when the stack | ||
| 13051 | + // really holds that entry. | ||
| 13052 | + for idx in 1..=2usize { | ||
| 13053 | + if stack.entries.get(idx).is_some() { | ||
| 13054 | + assert_eq!( | ||
| 13055 | + crate::tile::signalled_drl_idx(n, 1, idx), | ||
| 13056 | + idx, | ||
| 13057 | + "NEARMV: a {n}-entry stack cannot signal the offered drl index {idx}", | ||
| 13058 | + ); | ||
| 13059 | + } | ||
| 13060 | + } | ||
| 13061 | + } | ||
| 13062 | + } | ||
| 13063 | + | ||
| 12753 | fn bare_stack() -> crate::mvstack::MvStack { | 13064 | fn bare_stack() -> crate::mvstack::MvStack { |
| 12754 | crate::mvstack::MvStack { | 13065 | crate::mvstack::MvStack { |
| 12755 | entries: Default::default(), | 13066 | entries: Default::default(), |
| @@ -15397,6 +15708,86 @@ mod tests { | |||
| 15397 | /// | 15708 | /// |
| 15398 | /// cargo test -p ec-av1 --release --lib -- --ignored \ | 15709 | /// cargo test -p ec-av1 --release --lib -- --ignored \ |
| 15399 | /// a_128_superblock_clip_whose_drl_index_the_write_time_stack --nocapture | 15710 | /// a_128_superblock_clip_whose_drl_index_the_write_time_stack --nocapture |
| 15711 | + /// lane-b128: the 128x128 `PARTITION_NONE` root -- one block, one mode | ||
| 15712 | + /// and one motion vector where the four 64x64 roots below spend four of | ||
| 15713 | + /// each (cen3's L1). The counter is the gate on the fixture (class | ||
| 15714 | + /// `gate-blind-to-feature`): a green round trip on a clip that never | ||
| 15715 | + /// codes a 128x128 block proves nothing. RED before the search existed | ||
| 15716 | + /// (zero such blocks coded, `tile::write_sb128_root` wrote SPLIT at every | ||
| 15717 | + /// root by construction). | ||
| 15718 | + /// | ||
| 15719 | + /// Both readers: our own decoder sample-exact against the encoder's | ||
| 15720 | + /// reconstruction (a writer/reader desync surfaces as a pixel miss or a | ||
| 15721 | + /// refusal here, not in the BD gate) AND ffmpeg as the bitstream oracle. | ||
| 15722 | + /// | ||
| 15723 | + /// `#[ignore]`: [`force_sb128`] moves a process-global. | ||
| 15724 | + /// | ||
| 15725 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | ||
| 15726 | + /// a_128_superblock_clip_whose_root_search --nocapture | ||
| 15727 | + #[test] | ||
| 15728 | + #[ignore = "sets the process-global superblock size: run it alone"] | ||
| 15729 | + fn a_128_superblock_clip_whose_root_search_codes_128x128_blocks_decodes_exact() { | ||
| 15730 | + let _knobs = crate::speed::knob_write(); | ||
| 15731 | + if !have_ffmpeg() { | ||
| 15732 | + eprintln!("SKIP: no ffmpeg"); | ||
| 15733 | + return; | ||
| 15734 | + } | ||
| 15735 | + force_sb128(true); | ||
| 15736 | + let dir = std::env::temp_dir().join(format!("ec-av1-b128-{}", std::process::id())); | ||
| 15737 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 15738 | + let clip = dir.join("smpte-1280x768.y4m"); | ||
| 15739 | + // A slow horizontal pan over a still pattern: whole 128x128 regions | ||
| 15740 | + // share one motion vector, which is exactly what a 128 root can code | ||
| 15741 | + // as one block and four 64 roots cannot. | ||
| 15742 | + let ok = Command::new("ffmpeg") | ||
| 15743 | + .args(["-v", "error", "-y", "-f", "lavfi"]) | ||
| 15744 | + .args(["-i", "smptebars=size=1600x768:rate=24"]) | ||
| 15745 | + .args(["-vf", "crop=1280:768:'min(n*2,300)':0", "-frames:v", "8"]) | ||
| 15746 | + .args(["-pix_fmt", "yuv420p"]) | ||
| 15747 | + .arg(&clip) | ||
| 15748 | + .status() | ||
| 15749 | + .expect("ffmpeg failed to run") | ||
| 15750 | + .success(); | ||
| 15751 | + assert!(ok, "ffmpeg could not write the synthetic clip"); | ||
| 15752 | + let (width, height) = (1280usize, 768usize); | ||
| 15753 | + let pictures = | ||
| 15754 | + crate::probe::source(clip.to_str().unwrap(), "0", "null", width, height, 8); | ||
| 15755 | + let _ = take_b128_none_hits(); | ||
| 15756 | + let _ = crate::tile::take_sb128_none_hits(); | ||
| 15757 | + let encoded = encode_sequence(&pictures, 150, 0.5).unwrap(); | ||
| 15758 | + let (won, coded) = (take_b128_none_hits(), crate::tile::take_sb128_none_hits()); | ||
| 15759 | + eprintln!("128x128 PARTITION_NONE blocks: search won {won}, writer coded {coded}"); | ||
| 15760 | + assert!( | ||
| 15761 | + coded > 0, | ||
| 15762 | + "no 128x128 block was coded on this clip, so it cannot witness the root search", | ||
| 15763 | + ); | ||
| 15764 | + assert_eq!(won, coded, "every 128x128 root the search took must be coded as one"); | ||
| 15765 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | ||
| 15766 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | ||
| 15767 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | ||
| 15768 | + let recon = &frame.reconstruction; | ||
| 15769 | + for (plane, got, want) in [ | ||
| 15770 | + ("luma", &dec.y, &recon.y), | ||
| 15771 | + ("U", &dec.u, &recon.u), | ||
| 15772 | + ("V", &dec.v, &recon.v), | ||
| 15773 | + ] { | ||
| 15774 | + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); | ||
| 15775 | + assert_eq!( | ||
| 15776 | + differ, 0, | ||
| 15777 | + "frame {i}: {plane} -- {differ} of {} samples differ from the encoder's \ | ||
| 15778 | + own reconstruction", | ||
| 15779 | + got.len() | ||
| 15780 | + ); | ||
| 15781 | + } | ||
| 15782 | + } | ||
| 15783 | + let ff = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); | ||
| 15784 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&ff).enumerate() { | ||
| 15785 | + assert_eq!(dec.y, frame.reconstruction.y, "frame {i}: ffmpeg luma"); | ||
| 15786 | + assert_eq!(dec.u, frame.reconstruction.u, "frame {i}: ffmpeg U"); | ||
| 15787 | + assert_eq!(dec.v, frame.reconstruction.v, "frame {i}: ffmpeg V"); | ||
| 15788 | + } | ||
| 15789 | + } | ||
| 15790 | + | ||
| 15400 | #[test] | 15791 | #[test] |
| 15401 | #[ignore = "sets the process-global superblock size: run it alone"] | 15792 | #[ignore = "sets the process-global superblock size: run it alone"] |
| 15402 | fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { | 15793 | fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { |
| @@ -16820,6 +17211,9 @@ mod tests { | |||
| 16820 | // quantizer grid -- and every decision priced against it -- moved | 17211 | // quantizer grid -- and every decision priced against it -- moved |
| 16821 | // (8325 -> 8419 / 33014 -> 33029 on its own base); re-taken once more | 17212 | // (8325 -> 8419 / 33014 -> 33029 on its own base); re-taken once more |
| 16822 | // at the merge of both lanes. | 17213 | // at the merge of both lanes. |
| 17214 | + // NOT re-taken on lane-b128: [`SB128_DEFAULT`] stays off, so the | ||
| 17215 | + // shipped stream does not move; under `EC_AV1_SB128=1` the same four | ||
| 17216 | + // pictures code through the 128 root's own `PARTITION_NONE` block. | ||
| 16823 | let pins: [(u8, usize, u64); 2] = [(150, 8252, 0x5b6e40a2b5498988), (60, 33053, 0x9a0abfc234eccc37)]; | 17217 | let pins: [(u8, usize, u64); 2] = [(150, 8252, 0x5b6e40a2b5498988), (60, 33053, 0x9a0abfc234eccc37)]; |
| 16824 | let coded: Vec<(u8, usize, u64)> = pins | 17218 | let coded: Vec<(u8, usize, u64)> = pins |
| 16825 | .iter() | 17219 | .iter() |
| @@ -18104,6 +18498,11 @@ mod tests { | |||
| 18104 | {i64_edge} on a superblock the frame edge cuts", | 18498 | {i64_edge} on a superblock the frame edge cuts", |
| 18105 | 100.0 * i64_whole as f64 / i64_offered.max(1) as f64, | 18499 | 100.0 * i64_whole as f64 / i64_offered.max(1) as f64, |
| 18106 | ); | 18500 | ); |
| 18501 | + eprintln!( | ||
| 18502 | + "{name}: 128x128 roots {} (search) / {} (writer)", | ||
| 18503 | + take_b128_none_hits(), | ||
| 18504 | + crate::tile::take_sb128_none_hits(), | ||
| 18505 | + ); | ||
| 18107 | eprintln!( | 18506 | eprintln!( |
| 18108 | "{name}: 64x64 roots {} (with a residual {}, split luma {}, compound {})", | 18507 | "{name}: 64x64 roots {} (with a residual {}, split luma {}, compound {})", |
| 18109 | take_b64_root_hits(), | 18508 | take_b64_root_hits(), |
crates/ec-av1/src/tile.rsmodified+252 -6
| @@ -175,6 +175,12 @@ fn write_sb128_root( | |||
| 175 | sb_c: u32, | 175 | sb_c: u32, |
| 176 | mi_cols: u32, | 176 | mi_cols: u32, |
| 177 | mi_rows: u32, | 177 | mi_rows: u32, |
| 178 | + // lane-b128: the partition the root's own search chose -- | ||
| 179 | + // `PARTITION_SPLIT` (the four 64x64 quadrants below, which is every root | ||
| 180 | + // this writer coded before) or `PARTITION_NONE` (one 128x128 block, | ||
| 181 | + // written by the caller right after this). Only offered at a root wholly | ||
| 182 | + // inside the frame, so the gathered edge arms below still code SPLIT. | ||
| 183 | + part: usize, | ||
| 178 | ) { | 184 | ) { |
| 179 | SB128_ROOT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 185 | SB128_ROOT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 180 | let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); | 186 | let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); |
| @@ -186,7 +192,7 @@ fn write_sb128_root( | |||
| 186 | eprintln!("WTRACE partition_w128 ctx={ctx128} cols={has_cols128} rows={has_rows128}"); | 192 | eprintln!("WTRACE partition_w128 ctx={ctx128} cols={has_cols128} rows={has_rows128}"); |
| 187 | } | 193 | } |
| 188 | match (has_cols128, has_rows128) { | 194 | match (has_cols128, has_rows128) { |
| 189 | - (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w128[ctx128]), | 195 | + (true, true) => enc.symbol(part, &mut cdfs.partition_w128[ctx128]), |
| 190 | // As at the 64 root: the gathered CDF is built for the read and | 196 | // As at the 64 root: the gathered CDF is built for the read and |
| 191 | // thrown away, so nothing adapts here. Value 1 is SPLIT. | 197 | // thrown away, so nothing adapts here. Value 1 is SPLIT. |
| 192 | (true, false) => enc.symbol_fixed( | 198 | (true, false) => enc.symbol_fixed( |
| @@ -203,6 +209,160 @@ fn write_sb128_root( | |||
| 203 | } | 209 | } |
| 204 | } | 210 | } |
| 205 | 211 | ||
| 212 | +/// lane-b128: one 128x128 inter block at a `PARTITION_NONE` 128 superblock | ||
| 213 | +/// root -- the syntax `decode_inter_block` reads for `sb128_none` (decode.rs | ||
| 214 | +/// ~35110): `skip`, `is_inter`, the reference/mode chain at the block's own | ||
| 215 | +/// 32x32-mi window, the motion mode, and the transform syntax, with the mi | ||
| 216 | +/// grid and every neighbour band published over all thirty-two mi cells. | ||
| 217 | +/// | ||
| 218 | +/// SKIP only, and refused otherwise: a 128x128 block's residual is four | ||
| 219 | +/// TX_64X64 luma units interleaved with a TX_32X32 chroma pair per 64x64 mu | ||
| 220 | +/// chunk (the chroma units reading the offset-10 `txb_skip` rows), which | ||
| 221 | +/// this writer has no path for. A skipped block writes no `cdef_idx` and no | ||
| 222 | +/// `delta_q` either, exactly as the 64 root's skip arm does. | ||
| 223 | +/// | ||
| 224 | +/// # Errors | ||
| 225 | +/// A block that is intra, or carries a residual, at this size. | ||
| 226 | +#[allow(clippy::too_many_arguments)] | ||
| 227 | +fn write_inter_block_128( | ||
| 228 | + enc: &mut SymbolEncoder, | ||
| 229 | + cdfs: &mut Cdfs, | ||
| 230 | + neighbours: &mut Neighbours, | ||
| 231 | + grid: &mut MiGrid, | ||
| 232 | + // The root's top-left position in [`SUB`] (16x16) units. | ||
| 233 | + sb_at: (usize, usize), | ||
| 234 | + block: &BlockCoeffs, | ||
| 235 | + mi_cols: u32, | ||
| 236 | + mi_rows: u32, | ||
| 237 | + tx_select: bool, | ||
| 238 | +) -> Result<()> { | ||
| 239 | + /// The block's side in samples and in 4x4 mode-info units. | ||
| 240 | + const SIDE: usize = 128; | ||
| 241 | + const SIDE_MI: usize = SIDE / MI; | ||
| 242 | + let info = block.inter.ok_or_else(|| { | ||
| 243 | + Error::unsupported("AV1 tile", "a 128x128 superblock root block is coded inter") | ||
| 244 | + })?; | ||
| 245 | + if !block.skip { | ||
| 246 | + return Err(Error::unsupported( | ||
| 247 | + "AV1 tile", | ||
| 248 | + "a 128x128 superblock root block carries a residual", | ||
| 249 | + )); | ||
| 250 | + } | ||
| 251 | + let (mi_r, mi_c) = (sb_at.0 * (SUB / MI), sb_at.1 * (SUB / MI)); | ||
| 252 | + let has_above = neighbours.has_above(mi_r); | ||
| 253 | + let has_left = neighbours.has_left(mi_c); | ||
| 254 | + let skip_ctx = | ||
| 255 | + usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); | ||
| 256 | + enc.symbol(1, &mut cdfs.skip[skip_ctx]); | ||
| 257 | + write_cdef_idx(enc, (mi_r, mi_c), true); | ||
| 258 | + write_delta_q(enc, cdfs, (mi_r, mi_c), true, true); | ||
| 259 | + let ii_ctx = intra_inter_ctx( | ||
| 260 | + has_above, | ||
| 261 | + has_left, | ||
| 262 | + neighbours.above_inter[mi_c], | ||
| 263 | + neighbours.left_inter[mi_r], | ||
| 264 | + ); | ||
| 265 | + enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); | ||
| 266 | + write_comp_mode( | ||
| 267 | + enc, | ||
| 268 | + cdfs, | ||
| 269 | + neighbours, | ||
| 270 | + (mi_r, mi_c), | ||
| 271 | + (has_above, has_left), | ||
| 272 | + info.ref1.is_some(), | ||
| 273 | + ); | ||
| 274 | + let (mv, mv1, is_new_mv) = if info.ref1.is_some() { | ||
| 275 | + write_compound_block( | ||
| 276 | + enc, | ||
| 277 | + cdfs, | ||
| 278 | + neighbours, | ||
| 279 | + grid, | ||
| 280 | + (mi_r, mi_c), | ||
| 281 | + (SIDE_MI, SIDE_MI), | ||
| 282 | + (has_above, has_left), | ||
| 283 | + info, | ||
| 284 | + (mi_cols as usize, mi_rows as usize), | ||
| 285 | + )? | ||
| 286 | + } else { | ||
| 287 | + write_single_ref( | ||
| 288 | + enc, | ||
| 289 | + cdfs, | ||
| 290 | + info.ref_frame, | ||
| 291 | + neighbours.above_ref[mi_c], | ||
| 292 | + neighbours.above_ref1[mi_c], | ||
| 293 | + neighbours.left_ref[mi_r], | ||
| 294 | + neighbours.left_ref1[mi_r], | ||
| 295 | + ); | ||
| 296 | + let stack = find_mv_stack( | ||
| 297 | + grid, | ||
| 298 | + mi_r, | ||
| 299 | + mi_c, | ||
| 300 | + SIDE_MI, | ||
| 301 | + SIDE_MI, | ||
| 302 | + info.ref_frame, | ||
| 303 | + mi_cols as usize, | ||
| 304 | + mi_rows as usize, | ||
| 305 | + ); | ||
| 306 | + let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?; | ||
| 307 | + (mv, (0, 0), is_new_mv) | ||
| 308 | + }; | ||
| 309 | + for dr in 0..SIDE_MI { | ||
| 310 | + for dc in 0..SIDE_MI { | ||
| 311 | + grid.set( | ||
| 312 | + mi_r + dr, | ||
| 313 | + mi_c + dc, | ||
| 314 | + MiInfo { | ||
| 315 | + is_inter: true, | ||
| 316 | + ref_frame: info.ref_frame, | ||
| 317 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 318 | + mv1: mv16(mv1), | ||
| 319 | + mv: mv16(mv), | ||
| 320 | + is_new_mv, | ||
| 321 | + size: SIDE_MI as u8, | ||
| 322 | + size_h: SIDE_MI as u8, | ||
| 323 | + is_global_mv0: false, | ||
| 324 | + is_global_mv1: false, | ||
| 325 | + }, | ||
| 326 | + ); | ||
| 327 | + } | ||
| 328 | + } | ||
| 329 | + if info.ref1.is_none() { | ||
| 330 | + write_motion_mode( | ||
| 331 | + enc, | ||
| 332 | + cdfs, | ||
| 333 | + grid, | ||
| 334 | + (mi_r, mi_c), | ||
| 335 | + (SIDE_MI, SIDE_MI), | ||
| 336 | + (SIDE, SIDE), | ||
| 337 | + (mi_cols as usize, mi_rows as usize), | ||
| 338 | + info.ref_frame, | ||
| 339 | + block.motion_mode, | ||
| 340 | + )?; | ||
| 341 | + } | ||
| 342 | + if tx_select { | ||
| 343 | + write_tx_syntax_inter(enc, cdfs, neighbours, (mi_r, mi_c), SIDE, true, true, 0); | ||
| 344 | + } | ||
| 345 | + let zero_grids = [ | ||
| 346 | + vec![0i32; TX32 * TX32], | ||
| 347 | + vec![0i32; TX32 * TX32], | ||
| 348 | + vec![0i32; TX32 * TX32], | ||
| 349 | + ]; | ||
| 350 | + neighbours.record(sb_at, SIDE, 0, &zero_grids); | ||
| 351 | + neighbours.record_inter(sb_at, SIDE, true, true, block_ref(block)); | ||
| 352 | + record_block_compound(neighbours, (mi_r, mi_c), SIDE, block); | ||
| 353 | + SB128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 354 | + Ok(()) | ||
| 355 | +} | ||
| 356 | + | ||
| 357 | +/// How many 128x128 `PARTITION_NONE` blocks the inter writer has coded since | ||
| 358 | +/// the last [`take_sb128_none_hits`] (class `gate-blind-to-feature`). | ||
| 359 | +static SB128_NONE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 360 | + | ||
| 361 | +/// The 128x128-block count since the last call, and zero it. | ||
| 362 | +pub fn take_sb128_none_hits() -> usize { | ||
| 363 | + SB128_NONE_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 364 | +} | ||
| 365 | + | ||
| 206 | /// `read_lr`'s write side, called once at the top of every superblock, | 366 | /// `read_lr`'s write side, called once at the top of every superblock, |
| 207 | /// before its partition symbol: writes the units this superblock's own | 367 | /// before its partition symbol: writes the units this superblock's own |
| 208 | /// mi span covers (`av1_loop_restoration_corners_in_sb`; no superres, so the | 368 | /// mi span covers (`av1_loop_restoration_corners_in_sb`; no superres, so the |
| @@ -2034,6 +2194,15 @@ pub enum Quadrant { | |||
| 2034 | /// whose coded quarter is a 32x32 level grid, and one TX_32X32 per chroma | 2194 | /// whose coded quarter is a 32x32 level grid, and one TX_32X32 per chroma |
| 2035 | /// plane (lane-tx64, `crate::encode::b64_residual`). | 2195 | /// plane (lane-tx64, `crate::encode::b64_residual`). |
| 2036 | Whole64(BlockCoeffs), | 2196 | Whole64(BlockCoeffs), |
| 2197 | + /// lane-b128: the whole 128x128 SUPERBLOCK ROOT this quadrant is the | ||
| 2198 | + /// top-left of, coded as one block (`PARTITION_NONE` at the root's | ||
| 2199 | + /// `partition_w128` symbol). Carried by the top-left 32x32 entry of the | ||
| 2200 | + /// root's sixteen; the other fifteen carry [`Covered`](Quadrant::Covered). | ||
| 2201 | + /// | ||
| 2202 | + /// SKIP only: a 128x128 block's residual is four TX_64X64 luma units | ||
| 2203 | + /// interleaved with its chroma per 64x64 mu chunk, which this writer has | ||
| 2204 | + /// no path for; [`crate::tile::write_inter_block_128`] refuses one. | ||
| 2205 | + Whole128(BlockCoeffs), | ||
| 2037 | /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's | 2206 | /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's |
| 2038 | /// top-left already coded; it carries no block of its own. | 2207 | /// top-left already coded; it carries no block of its own. |
| 2039 | Covered, | 2208 | Covered, |
| @@ -2043,7 +2212,9 @@ impl Quadrant { | |||
| 2043 | /// The blocks it carries, in the order they are coded. | 2212 | /// The blocks it carries, in the order they are coded. |
| 2044 | pub(crate) fn blocks(&self) -> &[BlockCoeffs] { | 2213 | pub(crate) fn blocks(&self) -> &[BlockCoeffs] { |
| 2045 | match self { | 2214 | match self { |
| 2046 | - Quadrant::Whole(block) | Quadrant::Whole64(block) => std::slice::from_ref(block), | 2215 | + Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => { |
| 2216 | + std::slice::from_ref(block) | ||
| 2217 | + } | ||
| 2047 | Quadrant::Split(blocks) => blocks.as_slice(), | 2218 | Quadrant::Split(blocks) => blocks.as_slice(), |
| 2048 | Quadrant::Covered => &[], | 2219 | Quadrant::Covered => &[], |
| 2049 | } | 2220 | } |
| @@ -2923,7 +3094,7 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |||
| 2923 | neighbours.start_row(); | 3094 | neighbours.start_row(); |
| 2924 | } | 3095 | } |
| 2925 | if sb128 && sb_start { | 3096 | if sb128 && sb_start { |
| 2926 | - write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | 3097 | + write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows, PARTITION_SPLIT); |
| 2927 | } | 3098 | } |
| 2928 | { | 3099 | { |
| 2929 | if !sb128 { | 3100 | if !sb128 { |
| @@ -3040,7 +3211,7 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |||
| 3040 | match quadrant { | 3211 | match quadrant { |
| 3041 | // A 64x64 root is an INTER-frame partition; a | 3212 | // A 64x64 root is an INTER-frame partition; a |
| 3042 | // key frame codes one as `Superblock::Whole`. | 3213 | // key frame codes one as `Superblock::Whole`. |
| 3043 | - Quadrant::Whole64(_) | Quadrant::Covered => { | 3214 | + Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => { |
| 3044 | return Err(Error::unsupported( | 3215 | return Err(Error::unsupported( |
| 3045 | "AV1 tile", | 3216 | "AV1 tile", |
| 3046 | "a key frame codes a 64x64 block as `Superblock::Whole`", | 3217 | "a key frame codes a 64x64 block as `Superblock::Whole`", |
| @@ -4664,6 +4835,10 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { | |||
| 4664 | .iter() | 4835 | .iter() |
| 4665 | .map(|q| { | 4836 | .map(|q| { |
| 4666 | let side = match q { | 4837 | let side = match q { |
| 4838 | + // lane-b128: a 128x128 root is SKIP by construction (the | ||
| 4839 | + // writer refuses a residual at that size), so it names no | ||
| 4840 | + // coefficients on any plane. | ||
| 4841 | + Quadrant::Whole128(_) => return 0.0, | ||
| 4667 | Quadrant::Whole(_) => 32, | 4842 | Quadrant::Whole(_) => 32, |
| 4668 | Quadrant::Split(_) => 16, | 4843 | Quadrant::Split(_) => 16, |
| 4669 | // lane-tx64: a 64x64 root that codes a residual pays for one | 4844 | // lane-tx64: a 64x64 root that codes a residual pays for one |
| @@ -5270,6 +5445,8 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { | |||
| 5270 | static CDFS: LazyLock<Cdfs> = LazyLock::new(|| Cdfs::new(2)); | 5445 | static CDFS: LazyLock<Cdfs> = LazyLock::new(|| Cdfs::new(2)); |
| 5271 | let cdfs = &*CDFS; | 5446 | let cdfs = &*CDFS; |
| 5272 | let cdf: &[u16] = match side { | 5447 | let cdf: &[u16] = match side { |
| 5448 | + // lane-b128: the 128 superblock root's own eight-symbol alphabet. | ||
| 5449 | + 128 => &cdfs.partition_w128[0], | ||
| 5273 | SB => &cdfs.partition_w64[0], | 5450 | SB => &cdfs.partition_w64[0], |
| 5274 | BLOCK => &cdfs.partition_w32[0], | 5451 | BLOCK => &cdfs.partition_w32[0], |
| 5275 | TX8 => &cdfs.partition_w8[0], | 5452 | TX8 => &cdfs.partition_w8[0], |
| @@ -6049,6 +6226,19 @@ pub(crate) fn take_drl_hits() -> [usize; 4] { | |||
| 6049 | /// signalled index 1 and priced the residual against `pred_mv`, and the | 6226 | /// signalled index 1 and priced the residual against `pred_mv`, and the |
| 6050 | /// decoder's MV silently drifted a whole frame before the drl symbols | 6227 | /// decoder's MV silently drifted a whole frame before the drl symbols |
| 6051 | /// desynced). | 6228 | /// desynced). |
| 6229 | +/// The DRL index [`write_drl_idx`]'s walk can actually SIGNAL against a stack | ||
| 6230 | +/// of `entries` candidates: at most two `drl_mode` symbols past `start`, and | ||
| 6231 | +/// each one needs a further entry to advance into (spec 5.11.24's | ||
| 6232 | +/// `for (idx = 0; idx < 2; idx++) if (NumMvFound > idx + 1)`). | ||
| 6233 | +/// | ||
| 6234 | +/// A search that offers an index this clamps prices its residual against a | ||
| 6235 | +/// predictor the decoder never derives (lane-sb128b's silent MV drift); the | ||
| 6236 | +/// pricer's own offer set is enumerated against this rule by | ||
| 6237 | +/// `crate::encode::tests::every_drl_index_the_new_mv_pricer_offers_is_one_the_writer_can_signal`. | ||
| 6238 | +pub(crate) fn signalled_drl_idx(entries: usize, start: usize, target: usize) -> usize { | ||
| 6239 | + target.min(start + 2).min(entries.saturating_sub(1)).max(start) | ||
| 6240 | +} | ||
| 6241 | + | ||
| 6052 | fn write_drl_idx( | 6242 | fn write_drl_idx( |
| 6053 | enc: &mut SymbolEncoder, | 6243 | enc: &mut SymbolEncoder, |
| 6054 | cdfs: &mut Cdfs, | 6244 | cdfs: &mut Cdfs, |
| @@ -6056,6 +6246,17 @@ fn write_drl_idx( | |||
| 6056 | start: usize, | 6246 | start: usize, |
| 6057 | target: usize, | 6247 | target: usize, |
| 6058 | ) -> usize { | 6248 | ) -> usize { |
| 6249 | + debug_assert_eq!( | ||
| 6250 | + signalled_drl_idx(stack.entries.len(), start, target), | ||
| 6251 | + { | ||
| 6252 | + let mut i = start; | ||
| 6253 | + while i < start + 2 && stack.entries.len() > i + 1 && i < target { | ||
| 6254 | + i += 1; | ||
| 6255 | + } | ||
| 6256 | + i | ||
| 6257 | + }, | ||
| 6258 | + "the drl clamp rule and the writer's own walk disagree", | ||
| 6259 | + ); | ||
| 6059 | let mut idx = start; | 6260 | let mut idx = start; |
| 6060 | while idx < start + 2 && stack.entries.len() > idx + 1 { | 6261 | while idx < start + 2 && stack.entries.len() > idx + 1 { |
| 6061 | let advance = idx < target; | 6262 | let advance = idx < target; |
| @@ -6312,6 +6513,9 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6312 | ]; | 6513 | ]; |
| 6313 | 6514 | ||
| 6314 | let sb128 = sb128_armed(); | 6515 | let sb128 = sb128_armed(); |
| 6516 | + // lane-b128: set at a root whose own `PARTITION_NONE` 128x128 block was | ||
| 6517 | + // just written, so the root's remaining 64x64 cells code nothing. | ||
| 6518 | + let mut sb128_none = false; | ||
| 6315 | // lane-sb128: at a 128 superblock the four 64x64 cells of each root are | 6519 | // lane-sb128: at a 128 superblock the four 64x64 cells of each root are |
| 6316 | // written in libaom's own TL/TR/BL/BR order, the root's restoration units | 6520 | // written in libaom's own TL/TR/BL/BR order, the root's restoration units |
| 6317 | // and partition symbol first; at a 64 one this is plain raster and every | 6521 | // and partition symbol first; at a 64 one this is plain raster and every |
| @@ -6326,8 +6530,50 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6326 | if row_start { | 6530 | if row_start { |
| 6327 | neighbours.start_row(); | 6531 | neighbours.start_row(); |
| 6328 | } | 6532 | } |
| 6533 | + if sb_start { | ||
| 6534 | + sb128_none = false; | ||
| 6535 | + } | ||
| 6536 | + // lane-b128: the remaining three 64x64 cells of a root the search | ||
| 6537 | + // left WHOLE carry no syntax at all -- the same skip the decoder's | ||
| 6538 | + // own `sb128_none` runs. | ||
| 6539 | + if sb128_none { | ||
| 6540 | + continue; | ||
| 6541 | + } | ||
| 6329 | if sb128 && sb_start { | 6542 | if sb128 && sb_start { |
| 6330 | - write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | 6543 | + // The 128x128 block a `PARTITION_NONE` root carries, held by the |
| 6544 | + // top-left 32x32 entry of the root's sixteen. | ||
| 6545 | + let root_block = ((sb_r & !1) * 2 < rows && (sb_c & !1) * 2 < cols) | ||
| 6546 | + .then(|| &blocks[(((sb_r & !1) * 2) * cols + (sb_c & !1) * 2) as usize]) | ||
| 6547 | + .and_then(|q| match q { | ||
| 6548 | + Quadrant::Whole128(block) => Some(block), | ||
| 6549 | + _ => None, | ||
| 6550 | + }); | ||
| 6551 | + write_sb128_root( | ||
| 6552 | + &mut enc, | ||
| 6553 | + &mut cdfs, | ||
| 6554 | + &neighbours, | ||
| 6555 | + sb_r, | ||
| 6556 | + sb_c, | ||
| 6557 | + mi_cols, | ||
| 6558 | + mi_rows, | ||
| 6559 | + if root_block.is_some() { PARTITION_NONE } else { PARTITION_SPLIT }, | ||
| 6560 | + ); | ||
| 6561 | + if let Some(block) = root_block { | ||
| 6562 | + let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); | ||
| 6563 | + write_inter_block_128( | ||
| 6564 | + &mut enc, | ||
| 6565 | + &mut cdfs, | ||
| 6566 | + &mut neighbours, | ||
| 6567 | + &mut grid, | ||
| 6568 | + at128, | ||
| 6569 | + block, | ||
| 6570 | + mi_cols, | ||
| 6571 | + mi_rows, | ||
| 6572 | + tx_select, | ||
| 6573 | + )?; | ||
| 6574 | + sb128_none = true; | ||
| 6575 | + continue; | ||
| 6576 | + } | ||
| 6331 | } | 6577 | } |
| 6332 | { | 6578 | { |
| 6333 | if !sb128 { | 6579 | if !sb128 { |
| @@ -6573,7 +6819,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6573 | let block = match site { | 6819 | let block = match site { |
| 6574 | // Already coded by the `Whole64` above -- but only inside | 6820 | // Already coded by the `Whole64` above -- but only inside |
| 6575 | // a superblock that really carried one. | 6821 | // a superblock that really carried one. |
| 6576 | - Quadrant::Whole64(_) | Quadrant::Covered => { | 6822 | + Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => { |
| 6577 | return Err(Error::unsupported( | 6823 | return Err(Error::unsupported( |
| 6578 | "AV1 tile", | 6824 | "AV1 tile", |
| 6579 | "a `Whole64`/`Covered` quadrant needs its superblock's \ | 6825 | "a `Whole64`/`Covered` quadrant needs its superblock's \ |
lanes/b128.report.mdadded+206 -0
| @@ -0,0 +1,206 @@ | |||
| 1 | +# lane-b128 — a real 128x128 root under `EC_AV1_SB128` | ||
| 2 | + | ||
| 3 | +Three results: | ||
| 4 | + | ||
| 5 | +1. **Task 0 (the drl pricer) is a STALE premise, and now has its enumeration | ||
| 6 | + test.** The charter's `encode.rs:8722` mis-pricing was already closed by | ||
| 7 | + the lane-sb128b merge on the WRITER side; the pricer's own offer set is | ||
| 8 | + provably clamp-free against its own stack. | ||
| 9 | +2. **The 128x128 `PARTITION_NONE` root is implemented, witnessed and | ||
| 10 | + MEASURED** — and it turns lane-sb128b's refutation around: film B | ||
| 11 | + +24.2%/−2.5% vs libaom cpu-6 / rav1e s6 with 128 superblocks ON, against | ||
| 12 | + the SPLIT-only arm's +37.5%/+7.3%. | ||
| 13 | +3. **HORZ/VERT (128x64, 64x128) are deferred** — see the disposition below. | ||
| 14 | + | ||
| 15 | +## 1. Task 0 — the drl offer set, enumerated | ||
| 16 | + | ||
| 17 | +`crate::tile::signalled_drl_idx(entries, start, target)` is the writer's | ||
| 18 | +clamp rule as one pure function (`write_drl_idx`'s own walk now carries a | ||
| 19 | +`debug_assert_eq!` against it, so the two cannot drift). The pricer's offer | ||
| 20 | +set is enumerated against it over every stack size 0..=4, for both the | ||
| 21 | +`NEWMV` (`best_new_mv_syntax`) and the `NEARMV` (`entries.get(idx)`) arms: | ||
| 22 | + | ||
| 23 | + cargo test -p ec-av1 --lib -- every_drl_index_the_new_mv_pricer | ||
| 24 | + test encode::tests::every_drl_index_the_new_mv_pricer_offers_is_one_the_writer_can_signal ... ok | ||
| 25 | + | ||
| 26 | +GREEN as written: `best_new_mv_syntax` breaks at `entries.len() <= idx`, | ||
| 27 | +which is exactly `write_drl_idx`'s `entries.len() > idx + 1` condition, so | ||
| 28 | +no offered index is ever clamped **against the same stack**. Byte effect: | ||
| 29 | +none — the pins are byte-identical (below). The residual error the report | ||
| 30 | +open item names is entirely the SEARCH-time vs WRITE-time stack divergence | ||
| 31 | +(lane-sb128b's second open item), which no change on this line can reach. | ||
| 32 | + | ||
| 33 | +## 2. The 128 root | ||
| 34 | + | ||
| 35 | +### What moved | ||
| 36 | + | ||
| 37 | +- `tile.rs` — `Quadrant::Whole128(BlockCoeffs)`; `write_sb128_root` takes the | ||
| 38 | + partition value; `write_inter_block_128` writes the block (`skip`, | ||
| 39 | + `is_inter`, the reference/mode chain at a 32x32-mi window, motion mode, tx | ||
| 40 | + syntax, mi grid + neighbour bands over all 32 mi cells); the inter tile | ||
| 41 | + loop skips the root's remaining three 64x64 cells, mirroring the decoder's | ||
| 42 | + own `sb128_none`; `partition_bits` gained the 128 row; | ||
| 43 | + `signalled_drl_idx`; `take_sb128_none_hits`. | ||
| 44 | +- `encode.rs` — `search_root_128` (the candidate, its legality and its | ||
| 45 | + lambda), `search_skip_64` parameterised by `side`, the tile search loop | ||
| 46 | + restructured into ROOTS x cells, the 128-vs-four decision, | ||
| 47 | + `take_b128_none_hits`, the `EC_AV1_B128_CENSUS=1` per-frame line. | ||
| 48 | + | ||
| 49 | +### Deliberate limits (deviations from the charter, each with its reason) | ||
| 50 | + | ||
| 51 | +- **SKIP only.** A 128x128 block's residual is four TX_64X64 luma units | ||
| 52 | + interleaved with a TX_32X32 chroma pair per 64x64 mu chunk (the chroma | ||
| 53 | + units on the offset-10 `txb_skip` rows), plus one `txfm_partition` symbol | ||
| 54 | + per chunk. Neither the writer nor `Plane::code_from_prediction` has that | ||
| 55 | + path; `write_inter_block_128` REFUSES a non-skip block rather than coding | ||
| 56 | + one wrong. `search_root_128` therefore offers `residual: false`. | ||
| 57 | +- **Single reference only.** The compound arm at 128 would go through | ||
| 58 | + `write_compound_block` at a window size it has never been exercised at. | ||
| 59 | +- **Inter frames only.** A key frame's 128 root still writes SPLIT. | ||
| 60 | +- **HORZ / VERT deferred.** Two 128x64 / 64x128 blocks need rect neighbour | ||
| 61 | + recording and rect prediction/SSE on the WRITER and SEARCH sides (the | ||
| 62 | + decoder already reads both, lane-sb128c r7); the square NONE arm reuses | ||
| 63 | + every existing square helper unchanged. | ||
| 64 | + | ||
| 65 | +### Witness (red before / green after) | ||
| 66 | + | ||
| 67 | +`encode::tests::a_128_superblock_clip_whose_root_search_codes_128x128_blocks_decodes_exact` | ||
| 68 | +(`--ignored`, it moves the process-global superblock size): | ||
| 69 | + | ||
| 70 | + cargo test -p ec-av1 --release --lib -- --ignored \ | ||
| 71 | + a_128_superblock_clip_whose_root_search --nocapture | ||
| 72 | + 128x128 PARTITION_NONE blocks: search won 406, writer coded 406 | ||
| 73 | + test ... ok | ||
| 74 | + | ||
| 75 | +8 frames, 1280x768, a slow pan over `smptebars` at `force_sb128(true)`: | ||
| 76 | +**406** 128x128 blocks coded, every frame sample-exact against the encoder's | ||
| 77 | +own reconstruction through `decode_stream` AND through ffmpeg. RED before | ||
| 78 | +this lane by construction: `write_sb128_root` wrote `PARTITION_SPLIT` at | ||
| 79 | +every root, so the count was 0 and no such block existed to decode. | ||
| 80 | + | ||
| 81 | +### Census (`EC_AV1_B128_CENSUS=1`, plus a per-row count in the gate) | ||
| 82 | + | ||
| 83 | +Per row, over the four-quantizer ladder (search took / writer coded -- the | ||
| 84 | +writer is higher because the filter search re-codes each tile): | ||
| 85 | + | ||
| 86 | +| row | 128 roots taken | writer-coded | | ||
| 87 | +|---|---|---| | ||
| 88 | +| bars 1080p | 3448 | 6013 | | ||
| 89 | +| bars 2160p | 4144 | 5827 | | ||
| 90 | +| film A | 505 | 1241 | | ||
| 91 | +| film B | 1928 | 3189 | | ||
| 92 | +| screen capture | 4736 | 4774 | | ||
| 93 | + | ||
| 94 | +Per frame on film B the census reads e.g. `100 whole 128x128 roots, 336 | ||
| 95 | +coded blocks (83.3% of the frame's area at 128)` -- the lever cen3's L1 | ||
| 96 | +asked for (38754 coded blocks vs libaom's 16178) is really being pulled. | ||
| 97 | +All of them are on INTER frames: a key frame's 128 root still writes SPLIT. | ||
| 98 | + | ||
| 99 | +## 3. The gate | ||
| 100 | + | ||
| 101 | +`bd_rate_screen_native`, 12 frames, four quantizers, BD-rate vs libaom | ||
| 102 | +`cpu-used 6` / rav1e `speed 6` (lower is better). Both arms from THIS tree, | ||
| 103 | +both controls reproduced (not taken from the charter): | ||
| 104 | + | ||
| 105 | +| row | 64 superblocks (control) | 128 + the 128 root | | ||
| 106 | +|---|---|---| | ||
| 107 | +| bars 1080p | −2.6 / −18.3 | **−3.3 / −18.9** | | ||
| 108 | +| bars 2160p | +8.8 / −13.2 | **+7.9 / −14.0** | | ||
| 109 | +| film A (1080p) | +20.6 / −5.1 | **+20.4 / −5.2** | | ||
| 110 | +| film B (2160p) | +24.8 / −2.1 | **+24.2 / −2.5** | | ||
| 111 | +| screen capture | +14.6 / −33.2 | **+14.4 / −33.2** | | ||
| 112 | + | ||
| 113 | +Long GOP (`bd_rate_film_long_gop`, 48 frames): | ||
| 114 | + | ||
| 115 | +| row | 64 superblocks | 128 + the 128 root | | ||
| 116 | +|---|---|---| | ||
| 117 | +| film A | +24.7 / −7.5 | **+24.4 / −7.7** | | ||
| 118 | +| film B | +86.8 / +7.4 | **+85.6 / +6.6** | | ||
| 119 | + | ||
| 120 | +The 2x2 the charter asked for (film B, 12 frames): | ||
| 121 | + | ||
| 122 | +| arm | vs libaom | vs rav1e | | ||
| 123 | +|---|---|---| | ||
| 124 | +| SB64, LR on (control) | +24.8% | −2.1% | | ||
| 125 | +| SB128 + 128 root, LR on | **+24.2%** | **−2.5%** | | ||
| 126 | +| SB128 + 128 root, LR off (`EC_AV1_LR=0`) | +24.6% | −2.3% | | ||
| 127 | + | ||
| 128 | +(The SB64 LR-off control was not re-run: the SB64 path is byte-identical to | ||
| 129 | +the control tree -- the pins prove it -- and lane-sb128b measured it at | ||
| 130 | ++25.4/−1.6. The forced ≥128 LR unit is no longer a loss: LR ON is the better | ||
| 131 | +arm on both columns now, the reverse of lane-sb128b's reading.) | ||
| 132 | + | ||
| 133 | +Every row of both gates is down or flat on both columns. The keep rule | ||
| 134 | +HOLDS -- and the default still does not flip. See below. | ||
| 135 | + | ||
| 136 | +## 4. Why the default stays OFF (the blocker, not this lane's) | ||
| 137 | + | ||
| 138 | +With `EC_AV1_SB128=1`, **eleven** of this crate's own round-trip tests fail, | ||
| 139 | +and they fail with the 128x128 BLOCK switched off (`EC_AV1_B128=0`) too -- | ||
| 140 | +i.e. they are lane-sb128's SPLIT-only plumbing, at sizes its own 256x128 | ||
| 141 | +witness never covered: | ||
| 142 | + | ||
| 143 | + stream::tests::decode_stream_round_trips_a_gop | ||
| 144 | + stream::tests::decode_stream_round_trips_an_odd_size_gop | ||
| 145 | + stream::tests::decode_stream_agrees_with_ffmpeg_on_a_gop | ||
| 146 | + stream::tests::decode_stream_matches_the_tile_path_on_a_key_frame | ||
| 147 | + stream::tests::streaming_decode_matches_the_collecting_one | ||
| 148 | + stream::tests::a_doubly_cut_superblock_root_round_trips_in_every_plane | ||
| 149 | + stream::tests::a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg | ||
| 150 | + encoder::tests::every_tile_layout_decodes_sample_exact_through_both_decoders | ||
| 151 | + encoder::tests::tile_bytes_do_not_depend_on_the_thread_count | ||
| 152 | + encoder::tests::a_moving_detail_clip_codes_two_delta_q_levels_both_decoders_read_exactly | ||
| 153 | + encode::tests::a_repeated_pattern_key_frame_codes_intrabc_blocks_ffmpeg_decodes_exactly | ||
| 154 | + | ||
| 155 | +`decode_stream_round_trips_a_gop` fails with exactly the refusal lane-sb128b | ||
| 156 | +root-caused ("a reference frame selected with no picture at this frame's own | ||
| 157 | +ref_frame_idx slot"), so at least one writer/reader desync is still live in | ||
| 158 | +the 128 path at odd sizes / multiple tiles. Two more | ||
| 159 | +(`encode::tests::reach_matches_libaom_has_top_right_and_has_bottom_left`, | ||
| 160 | +`every_rect_shape_reaches_what_libaom_says_over_the_whole_superblock`) are | ||
| 161 | +TEST staleness: they assert libaom's 64-superblock reach tables against a | ||
| 162 | +process whose superblock size is 128. | ||
| 163 | + | ||
| 164 | +A 0.6-point BD win does not buy shipping that, so `SB128_DEFAULT` stays | ||
| 165 | +`false` and the whole feature stays behind `EC_AV1_SB128=1`. The next lane | ||
| 166 | +is named by this list. | ||
| 167 | + | ||
| 168 | +## 5. A defect fixed on the way (class: stale header in the test harness) | ||
| 169 | + | ||
| 170 | +Ten decode round-trip tests encode with THIS process's encoder and then | ||
| 171 | +decode the tile directly with a plain `FrameCtx`, whose superblock size is | ||
| 172 | +the DECODER default (64) -- not the size the writer used. With 128 | ||
| 173 | +superblocks on, the reader skipped the root's partition symbol and the tile | ||
| 174 | +desynced at symbol 0 (found by `EC_AV1_SYMTRACE`: the reader's first symbol | ||
| 175 | +was the writer's second). `FrameCtx::for_encoder` exists for exactly this | ||
| 176 | +and is now used in all ten; the module was swept, and two of the ten were | ||
| 177 | +not failing yet. Same shape as the `filter_intra_on()` argument beside them. | ||
| 178 | + | ||
| 179 | +## Disposition | ||
| 180 | + | ||
| 181 | +- `deferred`: **HORZ / VERT at the 128 root** (two 128x64 / 64x128 blocks) -- | ||
| 182 | + the writer has no rect neighbour recording and the search no rect | ||
| 183 | + prediction/SSE at this size; the square NONE arm needed neither. Unblocked | ||
| 184 | + by a rect `record_inter`/`record` pair plus a rect trial in | ||
| 185 | + `search_root_128`. The decoder already reads both (lane-sb128c r7). | ||
| 186 | +- `deferred`: **the 128 block's residual and its compound arm** -- four | ||
| 187 | + TX_64X64 luma units interleaved with a TX_32X32 chroma pair per mu chunk, | ||
| 188 | + on the offset-10 `txb_skip` rows, plus one `txfm_partition` symbol per | ||
| 189 | + chunk. Unblocked by that writer path; `write_inter_block_128` refuses a | ||
| 190 | + non-skip block until then, so nothing can silently code one wrong. | ||
| 191 | +- `deferred`: **the intra 128 root on key frames** -- the key-frame writer | ||
| 192 | + codes SPLIT at every 128 root, as before. | ||
| 193 | +- `open`: the eleven pre-existing 128-superblock desyncs above (the default's | ||
| 194 | + blocker). | ||
| 195 | +- `open`: search-time vs write-time mv stacks still differ (lane-sb128b's own | ||
| 196 | + second open item); the writer's clamp makes it safe, not right. | ||
| 197 | + | ||
| 198 | +## Suite / checks (default OFF, the shipped state) | ||
| 199 | + | ||
| 200 | + s1 --skip stream:: 340 passed; 0 failed; 34 ignored (RC=0) | ||
| 201 | + s2 stream:: --skip 10bit 200 passed; 0 failed; 15 ignored (RC=0) | ||
| 202 | + s3 10bit 42 passed; 0 failed; 1 ignored (RC=0) | ||
| 203 | + --ignored every_speed_preset_decodes_sample_exact_through_both_decoders ok | ||
| 204 | + --ignored a_128_superblock_clip_whose_root_search... ok (406 blocks) | ||
| 205 | + the_encoders_own_streams_are_byte_identical_to_their_pins ok, and ok at EC_AV1_SPEED=6 | ||
| 206 | + cargo check --workspace --all-targets 0 errors, 0 ec-av1 warnings | ||