A key frame's partition search offers a 64x64 intra root
Browse files at this commitf5d8b459bfd717a43159f1f699e41de16eff6fc1Parent: 36e7040- feat
- a key frame's partition search offers a 64x64 intra root
2 changed files +212 -18
- crates/ec-av1/src/encode.rs +205 -18
- crates/ec-av1/src/speed.rs +7 -0
crates/ec-av1/src/encode.rsmodified+205 -18
| @@ -436,6 +436,45 @@ fn b64_root() -> bool { | |||
| 436 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) | 436 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) |
| 437 | } | 437 | } |
| 438 | 438 | ||
| 439 | +/// The KEY frame's own 64x64 root (lane-i64): the whole superblock searched | ||
| 440 | +/// as ONE intra block -- every mode the 32x32 search offers, one TX_64X64 | ||
| 441 | +/// luma transform (`TxbSet::Luma64`, whose coded quarter is the 32x32 corner) | ||
| 442 | +/// and one TX_32X32 per chroma plane -- and compared against the four 32x32 | ||
| 443 | +/// quadrants at the same lambda, exactly the shape the inter root | ||
| 444 | +/// ([`B64_ROOT`]) takes on an inter frame. The writer has always been able to | ||
| 445 | +/// code one (`crate::tile::sb_coeff_key_frame_tile`'s `Superblock::Whole`); | ||
| 446 | +/// nothing produced one, so the key frame -- 21.6% of a real film stream -- | ||
| 447 | +/// coded every superblock split while libaom and rav1e leave 45-54% of theirs | ||
| 448 | +/// whole. | ||
| 449 | +/// | ||
| 450 | +/// No CfL (spec `is_cfl_allowed` stops at 32x32, and `search_chroma` gates on | ||
| 451 | +/// it), no filter intra (`filter_intra_size_class` has no 64 row) and no | ||
| 452 | +/// transform-depth split (the writer's `Superblock::Whole` arm takes a 32x32 | ||
| 453 | +/// level grid, so only depth 0 is representable) -- the root is refused | ||
| 454 | +/// outright on an `allow_intrabc` frame, whose quadrant searches carry DV | ||
| 455 | +/// state a discarded trial would desync. | ||
| 456 | +/// | ||
| 457 | +/// Switched off by `EC_AV1_I64=0` in any build. | ||
| 458 | +pub(crate) const I64_ROOT: bool = true; | ||
| 459 | + | ||
| 460 | +/// How many key-frame superblocks were coded as one 64x64 intra block since | ||
| 461 | +/// the last [`take_i64_root_hits`], so a gate reports the fire rate rather | ||
| 462 | +/// than assuming it (class `gate-blind-to-feature`). | ||
| 463 | +static I64_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 464 | + | ||
| 465 | +/// The key-frame 64x64-intra-root count since the last call, and zero it. | ||
| 466 | +pub fn take_i64_root_hits() -> usize { | ||
| 467 | + I64_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 468 | +} | ||
| 469 | + | ||
| 470 | +/// [`I64_ROOT`], or what `EC_AV1_I64` names. | ||
| 471 | +fn i64_root() -> bool { | ||
| 472 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 473 | + crate::envflags::var("EC_AV1_I64").ok().map(|v| v != "0") | ||
| 474 | + }); | ||
| 475 | + ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::I64_ROOT)) | ||
| 476 | +} | ||
| 477 | + | ||
| 439 | /// Whether the 64x64 root also prices a NON-skip candidate (lane-tx64): the | 478 | /// Whether the 64x64 root also prices a NON-skip candidate (lane-tx64): the |
| 440 | /// winner's prediction through one TX_64X64 luma transform and two TX_32X32 | 479 | /// winner's prediction through one TX_64X64 luma transform and two TX_32X32 |
| 441 | /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root | 480 | /// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root |
| @@ -909,16 +948,18 @@ fn intra_predict_u8( | |||
| 909 | ) { | 948 | ) { |
| 910 | // Three heap allocations per call became three stack buffers: this runs | 949 | // Three heap allocations per call became three stack buffers: this runs |
| 911 | // once per mode per trial, and an edge is at most `bw + bh` samples while | 950 | // once per mode per trial, and an edge is at most `bw + bh` samples while |
| 912 | - // a block is at most `BLOCK * BLOCK`. Same samples either way. | 951 | + // a block is at most `SUPERBLOCK * SUPERBLOCK` (lane-i64: the key frame's |
| 913 | - let mut above_buf = [0u16; 2 * BLOCK]; | 952 | + // 64x64 intra root predicts through here too, and reads up to `2 * bw` |
| 914 | - let mut left_buf = [0u16; 2 * BLOCK]; | 953 | + // samples above). |
| 954 | + let mut above_buf = [0u16; 2 * SUPERBLOCK]; | ||
| 955 | + let mut left_buf = [0u16; 2 * SUPERBLOCK]; | ||
| 915 | // lane-av1speed2: a `BLOCK * BLOCK` scratch is 2 KB zeroed on every mode | 956 | // lane-av1speed2: a `BLOCK * BLOCK` scratch is 2 KB zeroed on every mode |
| 916 | // trial of every block, and a 4x4 block writes 32 bytes of it. The | 957 | // trial of every block, and a 4x4 block writes 32 bytes of it. The |
| 917 | // predictor fills every sample it is handed, so the zeros are dead | 958 | // predictor fills every sample it is handed, so the zeros are dead |
| 918 | // either way -- size the scratch to the block instead of to the largest | 959 | // either way -- size the scratch to the block instead of to the largest |
| 919 | // block there is. | 960 | // block there is. |
| 920 | 961 | ||
| 921 | - let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| { | 962 | + let widen = |src: &[u8], buf: &mut [u16; 2 * SUPERBLOCK]| { |
| 922 | for (d, &v) in buf[..src.len()].iter_mut().zip(src) { | 963 | for (d, &v) in buf[..src.len()].iter_mut().zip(src) { |
| 923 | *d = u16::from(v); | 964 | *d = u16::from(v); |
| 924 | } | 965 | } |
| @@ -953,8 +994,10 @@ fn intra_predict_u8( | |||
| 953 | run(&mut [0u16; 64][..n]); | 994 | run(&mut [0u16; 64][..n]); |
| 954 | } else if n <= 256 { | 995 | } else if n <= 256 { |
| 955 | run(&mut [0u16; 256][..n]); | 996 | run(&mut [0u16; 256][..n]); |
| 956 | - } else { | 997 | + } else if n <= BLOCK * BLOCK { |
| 957 | run(&mut [0u16; BLOCK * BLOCK][..n]); | 998 | run(&mut [0u16; BLOCK * BLOCK][..n]); |
| 999 | + } else { | ||
| 1000 | + run(&mut [0u16; SUPERBLOCK * SUPERBLOCK][..n]); | ||
| 958 | } | 1001 | } |
| 959 | } | 1002 | } |
| 960 | 1003 | ||
| @@ -1810,8 +1853,8 @@ impl Plane<'_> { | |||
| 1810 | y: usize, | 1853 | y: usize, |
| 1811 | side: usize, | 1854 | side: usize, |
| 1812 | reach: Reach, | 1855 | reach: Reach, |
| 1813 | - above_buf: &'a mut [u8; 2 * BLOCK], | 1856 | + above_buf: &'a mut [u8; 2 * SUPERBLOCK], |
| 1814 | - left_buf: &'a mut [u8; 2 * BLOCK], | 1857 | + left_buf: &'a mut [u8; 2 * SUPERBLOCK], |
| 1815 | ) -> (Option<&'a [u8]>, Option<&'a [u8]>, Option<u8>) { | 1858 | ) -> (Option<&'a [u8]>, Option<&'a [u8]>, Option<u8>) { |
| 1816 | // Even a block's *own* edge is clamped to the true frame bound, not | 1859 | // Even a block's *own* edge is clamped to the true frame bound, not |
| 1817 | // just a `reach` extension: libaom's `av1_predict_intra_block` | 1860 | // just a `reach` extension: libaom's `av1_predict_intra_block` |
| @@ -1970,7 +2013,7 @@ impl Plane<'_> { | |||
| 1970 | let At { | 2013 | let At { |
| 1971 | x, y, side, reach, .. | 2014 | x, y, side, reach, .. |
| 1972 | } = at; | 2015 | } = at; |
| 1973 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 2016 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * SUPERBLOCK], [0u8; 2 * SUPERBLOCK]); |
| 1974 | let (above, left, corner) = | 2017 | let (above, left, corner) = |
| 1975 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); | 2018 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); |
| 1976 | // Per-candidate scratch on the search's hottest path, so both buffers | 2019 | // Per-candidate scratch on the search's hottest path, so both buffers |
| @@ -2228,7 +2271,7 @@ impl Plane<'_> { | |||
| 2228 | }; | 2271 | }; |
| 2229 | let trial = match filter_intra { | 2272 | let trial = match filter_intra { |
| 2230 | Some(fi) => { | 2273 | Some(fi) => { |
| 2231 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 2274 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * SUPERBLOCK], [0u8; 2 * SUPERBLOCK]); |
| 2232 | let (above, left, corner) = | 2275 | let (above, left, corner) = |
| 2233 | self.edges_into(tu.x, tu.y, tx, tu.reach, &mut above_buf, &mut left_buf); | 2276 | self.edges_into(tu.x, tu.y, tx, tu.reach, &mut above_buf, &mut left_buf); |
| 2234 | let mut prediction = vec![0u8; tx * tx]; | 2277 | let mut prediction = vec![0u8; tx * tx]; |
| @@ -2444,7 +2487,7 @@ impl Plane<'_> { | |||
| 2444 | let At { | 2487 | let At { |
| 2445 | x, y, side, reach, .. | 2488 | x, y, side, reach, .. |
| 2446 | } = at; | 2489 | } = at; |
| 2447 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 2490 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * SUPERBLOCK], [0u8; 2 * SUPERBLOCK]); |
| 2448 | let (above, left, corner) = | 2491 | let (above, left, corner) = |
| 2449 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); | 2492 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); |
| 2450 | modes | 2493 | modes |
| @@ -2488,7 +2531,7 @@ impl Plane<'_> { | |||
| 2488 | let At { | 2531 | let At { |
| 2489 | x, y, side, reach, .. | 2532 | x, y, side, reach, .. |
| 2490 | } = at; | 2533 | } = at; |
| 2491 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 2534 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * SUPERBLOCK], [0u8; 2 * SUPERBLOCK]); |
| 2492 | let (above, left, corner) = | 2535 | let (above, left, corner) = |
| 2493 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); | 2536 | self.edges_into(x, y, side, reach, &mut above_buf, &mut left_buf); |
| 2494 | // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or | 2537 | // Nothing ranks by the SAD pre-pass unless `top_k` prunes by it (or |
| @@ -2629,7 +2672,9 @@ impl Plane<'_> { | |||
| 2629 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 2672 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2630 | } | 2673 | } |
| 2631 | self.commit(x, y, side, &trial); | 2674 | self.commit(x, y, side, &trial); |
| 2632 | - (coeffs(&trial.levels, side), mode, angle_delta, cost) | 2675 | + // A 64-point transform codes only its top-left 32x32 corner, which is |
| 2676 | + // the grid `code_from_prediction` hands back (lane-i64). | ||
| 2677 | + (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost) | ||
| 2633 | } | 2678 | } |
| 2634 | } | 2679 | } |
| 2635 | 2680 | ||
| @@ -4193,7 +4238,15 @@ fn code_square( | |||
| 4193 | // header did not set `allow_intrabc`. | 4238 | // header did not set `allow_intrabc`. |
| 4194 | ibc: Option<&mut Ibc>, fctx: &crate::decode::FrameCtx, | 4239 | ibc: Option<&mut Ibc>, fctx: &crate::decode::FrameCtx, |
| 4195 | ) -> (BlockCoeffs, f64) { | 4240 | ) -> (BlockCoeffs, f64) { |
| 4196 | - let (luma_set, chroma_set) = if side == BLOCK { | 4241 | + let (luma_set, chroma_set) = if side == SUPERBLOCK { |
| 4242 | + // A whole superblock coded as ONE intra block (lane-i64): one | ||
| 4243 | + // TX_64X64 luma transform, whose coded quarter is the 32x32 corner | ||
| 4244 | + // `code_from_prediction` hands back, and one TX_32X32 per chroma | ||
| 4245 | + // plane -- the same three sets the key frame writer's | ||
| 4246 | + // `Superblock::Whole` arm codes, and the same the inter 64x64 root | ||
| 4247 | + // uses (`search_skip_64`). | ||
| 4248 | + (TxbSet::Luma64, TxbSet::Chroma32) | ||
| 4249 | + } else if side == BLOCK { | ||
| 4197 | (TxbSet::Luma32, TxbSet::Chroma16) | 4250 | (TxbSet::Luma32, TxbSet::Chroma16) |
| 4198 | } else if side == 8 { | 4251 | } else if side == 8 { |
| 4199 | // An 8x8 leaf under a straddling 16x16 (lane-av1-rect). r5 correction: | 4252 | // An 8x8 leaf under a straddling 16x16 (lane-av1-rect). r5 correction: |
| @@ -4220,6 +4273,14 @@ fn code_square( | |||
| 4220 | // libaom's own ordering, and the reason it costs one extra pass per depth | 4273 | // libaom's own ordering, and the reason it costs one extra pass per depth |
| 4221 | // rather than one per (mode, depth) pair. | 4274 | // rather than one per (mode, depth) pair. |
| 4222 | let mut tx_depth = 0u8; | 4275 | let mut tx_depth = 0u8; |
| 4276 | + // A 64x64 block has no depth to search (`max_tx_depth`), but the writer | ||
| 4277 | + // still codes its `tx_depth` symbol off `TX_SIZE_CAT3`, so the incumbent | ||
| 4278 | + // pays that zero exactly as a 32x32's depth search makes it pay | ||
| 4279 | + // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison | ||
| 4280 | + // below hands the root a symbol for free. | ||
| 4281 | + if tx_select && side == SUPERBLOCK { | ||
| 4282 | + cost += search.lambda * tx_depth_bits(side, 0); | ||
| 4283 | + } | ||
| 4223 | if tx_select && max_tx_depth(side) > 0 { | 4284 | if tx_select && max_tx_depth(side) > 0 { |
| 4224 | let mut best = ( | 4285 | let mut best = ( |
| 4225 | cost + search.lambda * tx_depth_bits(side, 0), | 4286 | cost + search.lambda * tx_depth_bits(side, 0), |
| @@ -4283,7 +4344,7 @@ fn code_square( | |||
| 4283 | mode = DC_PRED as u8; | 4344 | mode = DC_PRED as u8; |
| 4284 | angle_delta = 0; | 4345 | angle_delta = 0; |
| 4285 | tx_depth = 0; | 4346 | tx_depth = 0; |
| 4286 | - luma_coeffs = coeffs(&trial.levels, side); | 4347 | + luma_coeffs = coeffs(&trial.levels, side.min(BLOCK)); |
| 4287 | luma.commit(x, y, side, &trial); | 4348 | luma.commit(x, y, side, &trial); |
| 4288 | palette = Some(pal); | 4349 | palette = Some(pal); |
| 4289 | } | 4350 | } |
| @@ -4608,7 +4669,17 @@ fn search_chroma( | |||
| 4608 | if (V_PRED..=D67_PRED).contains(&mode) { | 4669 | if (V_PRED..=D67_PRED).contains(&mode) { |
| 4609 | bits += symbol_bits(&cdf::ANGLE_DELTA[usize::from(mode - V_PRED)], 3); | 4670 | bits += symbol_bits(&cdf::ANGLE_DELTA[usize::from(mode - V_PRED)], 3); |
| 4610 | } | 4671 | } |
| 4611 | - let tx_type = crate::decode::default_intra_tx_type(mode); | 4672 | + // lane-i64: `av1_get_ext_tx_set_type` resolves a CHROMA plane at |
| 4673 | + // `tx_size_sqr >= TX_32X32` to `EXT_TX_SET_DCTONLY`, so the 32x32 | ||
| 4674 | + // chroma of a 64x64 block is `DCT_DCT` whatever | ||
| 4675 | + // `Intra_Mode_To_Tx_Type` says -- the decoder's own `side >= 32` arm | ||
| 4676 | + // (decode.rs `read_chroma_plane`), and ADST is not even defined at 32 | ||
| 4677 | + // points. | ||
| 4678 | + let tx_type = if at.side >= 32 { | ||
| 4679 | + TxType::DctDct | ||
| 4680 | + } else { | ||
| 4681 | + crate::decode::default_intra_tx_type(mode) | ||
| 4682 | + }; | ||
| 4612 | let u = chroma[0].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx); | 4683 | let u = chroma[0].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx); |
| 4613 | let v = chroma[1].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx); | 4684 | let v = chroma[1].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx); |
| 4614 | let cost = u.sse + v.sse + search.lambda * (bits + u.bits + v.bits); | 4685 | let cost = u.sse + v.sse + search.lambda * (bits + u.bits + v.bits); |
| @@ -4704,7 +4775,7 @@ fn search_chroma( | |||
| 4704 | i32::from(luma.reconstruction[ly * luma.width + lx]) | 4775 | i32::from(luma.reconstruction[ly * luma.width + lx]) |
| 4705 | }); | 4776 | }); |
| 4706 | let dc_prediction = |plane: &Plane| -> Vec<u8> { | 4777 | let dc_prediction = |plane: &Plane| -> Vec<u8> { |
| 4707 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 4778 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * SUPERBLOCK], [0u8; 2 * SUPERBLOCK]); |
| 4708 | let (above, left, corner) = | 4779 | let (above, left, corner) = |
| 4709 | plane.edges_into(at.x, at.y, at.side, at.reach, &mut above_buf, &mut left_buf); | 4780 | plane.edges_into(at.x, at.y, at.side, at.reach, &mut above_buf, &mut left_buf); |
| 4710 | let mut prediction = vec![0u8; at.side * at.side]; | 4781 | let mut prediction = vec![0u8; at.side * at.side]; |
| @@ -6535,7 +6606,58 @@ pub(crate) fn encode_key_frame_inner( | |||
| 6535 | // walks them, which for a 64x64 split into 32x32 blocks is raster | 6606 | // walks them, which for a 64x64 split into 32x32 blocks is raster |
| 6536 | // order among the quadrants that are inside the frame. | 6607 | // order among the quadrants that are inside the frame. |
| 6537 | let mut blocks = Vec::with_capacity(4); | 6608 | let mut blocks = Vec::with_capacity(4); |
| 6538 | - for quadrant in 0..4 { | 6609 | + // lane-i64: the whole superblock as ONE 64x64 intra block, tried |
| 6610 | + // BEFORE its quadrants (so both start from the same | ||
| 6611 | + // reconstruction) and compared against the four of them below -- | ||
| 6612 | + // the shape the inter frame's own 64 root takes | ||
| 6613 | + // (`search_skip_64`). Only a superblock wholly inside the true | ||
| 6614 | + // frame is offered it: the writer's `Superblock::Whole` arm | ||
| 6615 | + // refuses one the edge cuts through, and a frame that allows | ||
| 6616 | + // intrabc is refused outright ([`I64_ROOT`]) because the quadrant | ||
| 6617 | + // searches below carry DV state a discarded trial would desync. | ||
| 6618 | + let (x64, y64) = (sb_col * SUPERBLOCK, sb_row * SUPERBLOCK); | ||
| 6619 | + let sb64_legal = i64_root() | ||
| 6620 | + && ibc.is_none() | ||
| 6621 | + && sb_row * 2 + 1 < rows | ||
| 6622 | + && sb_col * 2 + 1 < cols | ||
| 6623 | + && x64 + SUPERBLOCK <= luma.true_width | ||
| 6624 | + && y64 + SUPERBLOCK <= luma.true_height; | ||
| 6625 | + let mut sb64: Option<(f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> = None; | ||
| 6626 | + if sb64_legal { | ||
| 6627 | + let base = snapshot(&luma, &chroma, (x64, y64), SUPERBLOCK); | ||
| 6628 | + let (c64, r64) = (sb_col * 4, sb_row * 4); | ||
| 6629 | + let (block, cost) = code_square( | ||
| 6630 | + &mut luma, | ||
| 6631 | + &mut chroma, | ||
| 6632 | + (x64, y64), | ||
| 6633 | + SUPERBLOCK, | ||
| 6634 | + &search, | ||
| 6635 | + &mode_bits(above_mode[c64], left_mode[r64]), | ||
| 6636 | + tx_select, | ||
| 6637 | + None, | ||
| 6638 | + fctx, | ||
| 6639 | + ); | ||
| 6640 | + let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false); | ||
| 6641 | + let after = snapshot(&luma, &chroma, (x64, y64), SUPERBLOCK); | ||
| 6642 | + restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &base); | ||
| 6643 | + sb64 = Some((cost, block, after)); | ||
| 6644 | + } | ||
| 6645 | + // A superblock whose 64x64 trial already codes almost nothing per | ||
| 6646 | + // pixel is taken outright and its quadrants are never searched -- | ||
| 6647 | + // `split_rd_breakout` one size up, at the threshold the inter | ||
| 6648 | + // root clamps to ([`b64_breakout_threshold`]), where this lever | ||
| 6649 | + // BUYS wall instead of spending it. | ||
| 6650 | + let early64 = sb64.as_ref().is_some_and(|(c, _, _)| { | ||
| 6651 | + split_rd_breakout_at(b64_breakout_threshold(), *c, SUPERBLOCK, search.lambda) | ||
| 6652 | + }); | ||
| 6653 | + // What the four quadrants cost together, including the split | ||
| 6654 | + // partition symbol at the superblock level, so the two arms are | ||
| 6655 | + // one comparable number. | ||
| 6656 | + let mut sb_cost = match sb64 { | ||
| 6657 | + Some(_) => search.lambda * crate::tile::partition_bits(SUPERBLOCK, true), | ||
| 6658 | + None => 0.0, | ||
| 6659 | + }; | ||
| 6660 | + for quadrant in 0..if early64 { 0 } else { 4 } { | ||
| 6539 | let (col, row) = (sb_col * 2 + quadrant % 2, sb_row * 2 + quadrant / 2); | 6661 | let (col, row) = (sb_col * 2 + quadrant % 2, sb_row * 2 + quadrant / 2); |
| 6540 | if col >= cols || row >= rows { | 6662 | if col >= cols || row >= rows { |
| 6541 | continue; | 6663 | continue; |
| @@ -6686,9 +6808,11 @@ pub(crate) fn encode_key_frame_inner( | |||
| 6686 | // the read path (`decode_leaf8`), so leaf8 can now win on | 6808 | // the read path (`decode_leaf8`), so leaf8 can now win on |
| 6687 | // cost anywhere, same as any other split. | 6809 | // cost anywhere, same as any other split. |
| 6688 | if !whole_legal || (split_blocks && cost_split < cost_whole) { | 6810 | if !whole_legal || (split_blocks && cost_split < cost_whole) { |
| 6811 | + sb_cost += cost_split; | ||
| 6689 | modes.extend_from_slice(&split_modes); | 6812 | modes.extend_from_slice(&split_modes); |
| 6690 | blocks.push(Quadrant::Split(split)); | 6813 | blocks.push(Quadrant::Split(split)); |
| 6691 | } else { | 6814 | } else { |
| 6815 | + sb_cost += cost_whole; | ||
| 6692 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | 6816 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); |
| 6693 | for cell in 0..2 { | 6817 | for cell in 0..2 { |
| 6694 | above_mode[c0 + cell] = whole.mode; | 6818 | above_mode[c0 + cell] = whole.mode; |
| @@ -6698,7 +6822,20 @@ pub(crate) fn encode_key_frame_inner( | |||
| 6698 | blocks.push(Quadrant::Whole(whole)); | 6822 | blocks.push(Quadrant::Whole(whole)); |
| 6699 | } | 6823 | } |
| 6700 | } | 6824 | } |
| 6701 | - coded.push((sb_row * sb_cols + sb_col, Superblock::Split(blocks), modes)); | 6825 | + let index = sb_row * sb_cols + sb_col; |
| 6826 | + match sb64 { | ||
| 6827 | + Some((cost64, block, after)) if early64 || cost64 < sb_cost => { | ||
| 6828 | + restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &after); | ||
| 6829 | + for cell in 0..4 { | ||
| 6830 | + above_mode[sb_col * 4 + cell] = block.mode; | ||
| 6831 | + left_mode[sb_row * 4 + cell] = block.mode; | ||
| 6832 | + } | ||
| 6833 | + I64_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 6834 | + let mode = block.mode; | ||
| 6835 | + coded.push((index, Superblock::Whole(block), vec![mode])); | ||
| 6836 | + } | ||
| 6837 | + _ => coded.push((index, Superblock::Split(blocks), modes)), | ||
| 6838 | + } | ||
| 6702 | } | 6839 | } |
| 6703 | } | 6840 | } |
| 6704 | let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); | 6841 | let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); |
| @@ -10922,6 +11059,56 @@ mod tests { | |||
| 10922 | 10.0 * (255.0 * 255.0 * a.len() as f64 / squared).log10() | 11059 | 10.0 * (255.0 * 255.0 * a.len() as f64 / squared).log10() |
| 10923 | } | 11060 | } |
| 10924 | 11061 | ||
| 11062 | + /// lane-i64's witness: a smooth gradient is exactly the picture a whole | ||
| 11063 | + /// 64x64 intra block predicts as well as four 32x32 ones, so the key | ||
| 11064 | + /// frame's new root fires on most superblocks -- and what BOTH decoders | ||
| 11065 | + /// produce is the encoder's own reconstruction, sample for sample. | ||
| 11066 | + /// | ||
| 11067 | + /// The fire count is the `gate-blind-to-feature` guard: without it every | ||
| 11068 | + /// assert below passes on a stream that codes no whole superblock at all. | ||
| 11069 | + #[test] | ||
| 11070 | + fn a_smooth_gradient_key_frame_codes_whole_superblocks_and_decodes_exactly() { | ||
| 11071 | + let _knobs = crate::speed::knob_read(); | ||
| 11072 | + let fctx = &crate::decode::FrameCtx::new(); | ||
| 11073 | + // 4 x 3 superblocks, all of them wholly inside the frame. | ||
| 11074 | + let (width, height) = (256usize, 192usize); | ||
| 11075 | + let mut picture = Picture::grey(width, height); | ||
| 11076 | + for y in 0..height { | ||
| 11077 | + for x in 0..width { | ||
| 11078 | + picture.y[y * width + x] = (16 + (x + y) * 200 / (width + height)) as u16; | ||
| 11079 | + } | ||
| 11080 | + } | ||
| 11081 | + for y in 0..height / 2 { | ||
| 11082 | + for x in 0..width / 2 { | ||
| 11083 | + picture.u[y * width / 2 + x] = (110 + x * 20 / (width / 2)) as u16; | ||
| 11084 | + picture.v[y * width / 2 + x] = (130 - y * 20 / (height / 2)) as u16; | ||
| 11085 | + } | ||
| 11086 | + } | ||
| 11087 | + let _ = take_i64_root_hits(); | ||
| 11088 | + let encoded = encode_key_frame_with_ctx(&picture, 120, 0.5, fctx).unwrap(); | ||
| 11089 | + let roots = take_i64_root_hits(); | ||
| 11090 | + assert!( | ||
| 11091 | + roots >= 8, | ||
| 11092 | + "a smooth-gradient key frame coded only {roots} whole 64x64 intra roots of 12" | ||
| 11093 | + ); | ||
| 11094 | + eprintln!("whole 64x64 intra roots on a smooth gradient: {roots} of 12"); | ||
| 11095 | + | ||
| 11096 | + let ours = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | ||
| 11097 | + assert_eq!(ours.len(), 1, "our decoder's frames"); | ||
| 11098 | + assert_eq!(ours[0].y, encoded.reconstruction.y, "our decoder: luma"); | ||
| 11099 | + assert_eq!(ours[0].u, encoded.reconstruction.u, "our decoder: U"); | ||
| 11100 | + assert_eq!(ours[0].v, encoded.reconstruction.v, "our decoder: V"); | ||
| 11101 | + | ||
| 11102 | + if !have_ffmpeg() { | ||
| 11103 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 11104 | + return; | ||
| 11105 | + } | ||
| 11106 | + let decoded = ffmpeg_decode(&encoded.stream, width, height); | ||
| 11107 | + assert_eq!(decoded.y, encoded.reconstruction.y, "ffmpeg: luma"); | ||
| 11108 | + assert_eq!(decoded.u, encoded.reconstruction.u, "ffmpeg: U"); | ||
| 11109 | + assert_eq!(decoded.v, encoded.reconstruction.v, "ffmpeg: V"); | ||
| 11110 | + } | ||
| 11111 | + | ||
| 10925 | /// The claim the whole encoder rests on: what a decoder produces is what | 11112 | /// The claim the whole encoder rests on: what a decoder produces is what |
| 10926 | /// the encoder said it would, sample for sample, on every plane. | 11113 | /// the encoder said it would, sample for sample, on every plane. |
| 10927 | /// | 11114 | /// |
crates/ec-av1/src/speed.rsmodified+7 -0
| @@ -169,6 +169,13 @@ pub(crate) fn at<T: Copy>(table: &[T; 11]) -> T { | |||
| 169 | /// (screen). So it is on at every preset. | 169 | /// (screen). So it is on at every preset. |
| 170 | pub(crate) const B64_ROOT: [bool; 11] = [crate::encode::B64_ROOT; 11]; | 170 | pub(crate) const B64_ROOT: [bool; 11] = [crate::encode::B64_ROOT; 11]; |
| 171 | 171 | ||
| 172 | +/// `encode::I64_ROOT`: the KEY frame's 64x64 intra root. On at every preset | ||
| 173 | +/// (see the lane-i64 report's speed-6 arm); its early-out reads | ||
| 174 | +/// `encode::b64_breakout_threshold`, the same preset-clamped 0.125 the inter | ||
| 175 | +/// root uses, so the preset's looser `SPLIT_RD` never withholds four quadrant | ||
| 176 | +/// searches here either. | ||
| 177 | +pub(crate) const I64_ROOT: [bool; 11] = [crate::encode::I64_ROOT; 11]; | ||
| 178 | + | ||
| 172 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split | 179 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 173 | /// trial is withheld. The single biggest wall lever in the tile search. | 180 | /// trial is withheld. The single biggest wall lever in the tile search. |
| 174 | pub(crate) const SPLIT_RD: [f64; 11] = [ | 181 | pub(crate) const SPLIT_RD: [f64; 11] = [ |