A 64x64 PARTITION_NONE skip root for the inter search
Browse files at this commit5bcc6a5ccce573d6c7d0f9fbb658c16ac9810d9aParent: 09cb0af- feat
- a 64x64 PARTITION_NONE skip root for the inter search
4 changed files +515 -7
- crates/ec-av1/src/encode.rs +292 -1
- crates/ec-av1/src/encoder.rs +59 -0
- crates/ec-av1/src/speed.rs +6 -0
- crates/ec-av1/src/tile.rs +158 -6
crates/ec-av1/src/encode.rsmodified+292 -1
| @@ -376,6 +376,40 @@ fn split_inter_blocks() -> bool { | |||
| 376 | } | 376 | } |
| 377 | } | 377 | } |
| 378 | 378 | ||
| 379 | +/// Whether the inter search offers the whole superblock as ONE 64x64 block | ||
| 380 | +/// (`PARTITION_NONE` at `BLOCK_64X64`), coded SKIP, against the four 32x32 | ||
| 381 | +/// quadrants it would otherwise always split into. | ||
| 382 | +/// | ||
| 383 | +/// The census (`lanes/census-filmB.md`) is what points here: rav1e speed 6 | ||
| 384 | +/// codes 45.2% of its blocks at 64x64 -- 73.8% of its LEAF blocks, 99.2% of | ||
| 385 | +/// them skip at 328 bytes a frame -- while every block this encoder wrote was | ||
| 386 | +/// 32x32 or smaller, which put our mode+mv+partition+tx_size spend at 162,669 | ||
| 387 | +/// bits against rav1e's 61,988, about 46% of the film B gap. | ||
| 388 | +/// | ||
| 389 | +/// SKIP-only: a 64x64 residual needs a forward TX_64X64 the encoder does not | ||
| 390 | +/// have (`transform.rs` tops out at 32x32), and the writer refuses any other | ||
| 391 | +/// shape (`crate::tile::Quadrant::Whole64`). Switched off by `EC_AV1_B64=0` | ||
| 392 | +/// in any build. | ||
| 393 | +pub(crate) const B64_ROOT: bool = true; | ||
| 394 | + | ||
| 395 | +/// How many superblocks took the 64x64 root since the last | ||
| 396 | +/// [`take_b64_root_hits`], so a gate reports how often it fires rather than | ||
| 397 | +/// assuming it does (class `gate-blind-to-feature`). | ||
| 398 | +static B64_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 399 | + | ||
| 400 | +/// The 64x64-root count since the last call, and zero it. | ||
| 401 | +pub fn take_b64_root_hits() -> usize { | ||
| 402 | + B64_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 403 | +} | ||
| 404 | + | ||
| 405 | +/// [`B64_ROOT`], or what `EC_AV1_B64` names. | ||
| 406 | +fn b64_root() -> bool { | ||
| 407 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 408 | + crate::envflags::var("EC_AV1_B64").ok().map(|v| v != "0") | ||
| 409 | + }); | ||
| 410 | + ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) | ||
| 411 | +} | ||
| 412 | + | ||
| 379 | /// Whether the encoder searches loop restoration at all (spec 7.17): one | 413 | /// Whether the encoder searches loop restoration at all (spec 7.17): one |
| 380 | /// Wiener filter per 64x64 luma restoration unit, picked from | 414 | /// Wiener filter per 64x64 luma restoration unit, picked from |
| 381 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a | 415 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a |
| @@ -7135,6 +7169,180 @@ fn mc_trial_compound( | |||
| 7135 | /// [`find_mv_stack`] is public and this function is handed the same `MvStack` | 7169 | /// [`find_mv_stack`] is public and this function is handed the same `MvStack` |
| 7136 | /// the tile writer derives from the same grid state. | 7170 | /// the tile writer derives from the same grid state. |
| 7137 | #[allow(clippy::too_many_arguments)] | 7171 | #[allow(clippy::too_many_arguments)] |
| 7172 | +/// One prediction of `side` x `side` samples from `reference`, as `u8` -- | ||
| 7173 | +/// [`mc_trial`]'s prediction half, on the heap because a 64x64 luma block is | ||
| 7174 | +/// four times the stack scratch that function sizes for [`BLOCK`]. | ||
| 7175 | +fn predict_u8( | ||
| 7176 | + reference: &[u16], | ||
| 7177 | + stride: usize, | ||
| 7178 | + ref_width: usize, | ||
| 7179 | + ref_height: usize, | ||
| 7180 | + (x, y): (usize, usize), | ||
| 7181 | + side: usize, | ||
| 7182 | + mv: (i32, i32), | ||
| 7183 | + luma: bool, | ||
| 7184 | + fctx: &crate::decode::FrameCtx, | ||
| 7185 | +) -> Vec<u8> { | ||
| 7186 | + let mut prediction = vec![0u16; side * side]; | ||
| 7187 | + mc::predict( | ||
| 7188 | + reference, | ||
| 7189 | + stride, | ||
| 7190 | + ref_width, | ||
| 7191 | + ref_height, | ||
| 7192 | + mv_to_q4(x, mv.1, luma), | ||
| 7193 | + mv_to_q4(y, mv.0, luma), | ||
| 7194 | + side, | ||
| 7195 | + side, | ||
| 7196 | + &mut prediction, | ||
| 7197 | + fctx, | ||
| 7198 | + ); | ||
| 7199 | + prediction.iter().map(|&v| v as u8).collect() | ||
| 7200 | +} | ||
| 7201 | + | ||
| 7202 | +/// The 64x64 `PARTITION_NONE` candidate for a whole superblock ([`B64_ROOT`]): | ||
| 7203 | +/// single-reference LAST at `NEARESTMV`/`GLOBALMV`/`NEARMV`/`NEWMV`, coded | ||
| 7204 | +/// SKIP, priced through the same `symbol_bits` path every other candidate | ||
| 7205 | +/// uses. The winner's prediction is committed to the planes (it IS the | ||
| 7206 | +/// reconstruction -- a skip block has no residual); the caller restores them | ||
| 7207 | +/// if the four quadrants come out cheaper. | ||
| 7208 | +/// | ||
| 7209 | +/// `None` when no candidate could be coded at all (a `NEWMV` residual the | ||
| 7210 | +/// writer cannot name is simply not offered, and the other modes always are, | ||
| 7211 | +/// so this only happens if every candidate is refused). | ||
| 7212 | +fn search_skip_64( | ||
| 7213 | + luma: &mut Plane, | ||
| 7214 | + chroma: &mut [Plane; 2], | ||
| 7215 | + (x, y): (usize, usize), | ||
| 7216 | + search: &Search, | ||
| 7217 | + reference: &Picture, | ||
| 7218 | + stack: &MvStack, | ||
| 7219 | + fctx: &crate::decode::FrameCtx, | ||
| 7220 | +) -> Option<(f64, BlockCoeffs)> { | ||
| 7221 | + let side = SUPERBLOCK; | ||
| 7222 | + let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); | ||
| 7223 | + let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); | ||
| 7224 | + let ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) | ||
| 7225 | + + symbol_bits(&cdf::SINGLE_REF[0][2], 0) | ||
| 7226 | + + symbol_bits(&cdf::SINGLE_REF[0][3], 0); | ||
| 7227 | + // What every candidate pays whatever its vector: `skip` coded 1, then | ||
| 7228 | + // `is_inter` coded 1, then the LAST chain above. | ||
| 7229 | + let fixed = symbol_bits(&cdf::SKIP[0], 1) + symbol_bits(&cdf::INTRA_INTER[0], 1) + ref_bits; | ||
| 7230 | + let info_of = |mode: InterMode, mv: (i32, i32), ref_mv_idx: u8| InterInfo { | ||
| 7231 | + ref1: None, | ||
| 7232 | + mv1: (0, 0), | ||
| 7233 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 7234 | + mode, | ||
| 7235 | + mv, | ||
| 7236 | + ref_mv_idx, | ||
| 7237 | + }; | ||
| 7238 | + let mut cands: Vec<(f64, InterInfo)> = vec![ | ||
| 7239 | + ( | ||
| 7240 | + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), | ||
| 7241 | + info_of(InterMode::NearestMv, stack.nearest_mv, 0), | ||
| 7242 | + ), | ||
| 7243 | + ( | ||
| 7244 | + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), | ||
| 7245 | + info_of(InterMode::GlobalMv, (0, 0), 0), | ||
| 7246 | + ), | ||
| 7247 | + ]; | ||
| 7248 | + let near_bits = not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 1); | ||
| 7249 | + for idx in 1..=2usize { | ||
| 7250 | + if let Some(e) = stack.entries.get(idx) { | ||
| 7251 | + cands.push(( | ||
| 7252 | + near_bits + drl_bits_for(stack, 1, idx), | ||
| 7253 | + info_of(InterMode::NearMv, e.mv, idx as u8), | ||
| 7254 | + )); | ||
| 7255 | + } | ||
| 7256 | + } | ||
| 7257 | + if leaf_new_mv() { | ||
| 7258 | + let source_block = luma.source_block(x, y, side); | ||
| 7259 | + let (seeds, seed_n) = mv_seeds(stack); | ||
| 7260 | + let found = motion::search( | ||
| 7261 | + &reference.y, | ||
| 7262 | + reference.width, | ||
| 7263 | + luma.true_width, | ||
| 7264 | + luma.true_height, | ||
| 7265 | + &source_block, | ||
| 7266 | + x, | ||
| 7267 | + y, | ||
| 7268 | + side, | ||
| 7269 | + side, | ||
| 7270 | + stack.pred_mv, | ||
| 7271 | + &seeds[..seed_n], | ||
| 7272 | + search.lambda, | ||
| 7273 | + fctx, | ||
| 7274 | + 1, | ||
| 7275 | + ); | ||
| 7276 | + let mv = round_to_valid_mv(found.mv, stack.pred_mv); | ||
| 7277 | + if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(stack, mv) { | ||
| 7278 | + cands.push(( | ||
| 7279 | + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, | ||
| 7280 | + info_of(InterMode::NewMv, mv, ref_mv_idx), | ||
| 7281 | + )); | ||
| 7282 | + } | ||
| 7283 | + } | ||
| 7284 | + | ||
| 7285 | + let mut best: Option<(f64, InterInfo, [Trial; 3])> = None; | ||
| 7286 | + for (bits, info) in cands { | ||
| 7287 | + let trials = [ | ||
| 7288 | + (0usize, x, y, side, true), | ||
| 7289 | + (1, x / 2, y / 2, side / 2, false), | ||
| 7290 | + (2, x / 2, y / 2, side / 2, false), | ||
| 7291 | + ] | ||
| 7292 | + .map(|(plane, px, py, s, is_luma)| { | ||
| 7293 | + let (samples, stride, w, h) = match plane { | ||
| 7294 | + 0 => (&reference.y, reference.width, luma.true_width, luma.true_height), | ||
| 7295 | + 1 => ( | ||
| 7296 | + &reference.u, | ||
| 7297 | + reference.width / 2, | ||
| 7298 | + chroma[0].true_width, | ||
| 7299 | + chroma[0].true_height, | ||
| 7300 | + ), | ||
| 7301 | + _ => ( | ||
| 7302 | + &reference.v, | ||
| 7303 | + reference.width / 2, | ||
| 7304 | + chroma[1].true_width, | ||
| 7305 | + chroma[1].true_height, | ||
| 7306 | + ), | ||
| 7307 | + }; | ||
| 7308 | + let prediction = predict_u8(samples, stride, w, h, (px, py), s, info.mv, is_luma, fctx); | ||
| 7309 | + let target: &Plane = if plane == 0 { luma } else { &chroma[plane - 1] }; | ||
| 7310 | + // `skip` true: the trial is the prediction itself plus its SSE, | ||
| 7311 | + // so no transform of any size is involved (the reason a 64x64 | ||
| 7312 | + // root can be coded at all before a forward TX_64X64 exists). | ||
| 7313 | + target.code_from_prediction( | ||
| 7314 | + px, | ||
| 7315 | + py, | ||
| 7316 | + s, | ||
| 7317 | + &prediction, | ||
| 7318 | + true, | ||
| 7319 | + search.base_q_idx, | ||
| 7320 | + search.deadzone, | ||
| 7321 | + TxbSet::Luma32Inter, | ||
| 7322 | + ) | ||
| 7323 | + }); | ||
| 7324 | + let cost = trials[0].sse | ||
| 7325 | + + trials[1].sse | ||
| 7326 | + + trials[2].sse | ||
| 7327 | + + search.lambda * (fixed + bits); | ||
| 7328 | + if best.as_ref().is_none_or(|b| cost < b.0) { | ||
| 7329 | + best = Some((cost, info, trials)); | ||
| 7330 | + } | ||
| 7331 | + } | ||
| 7332 | + let (cost, info, trials) = best?; | ||
| 7333 | + luma.commit(x, y, side, &trials[0]); | ||
| 7334 | + chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]); | ||
| 7335 | + chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]); | ||
| 7336 | + Some(( | ||
| 7337 | + cost, | ||
| 7338 | + BlockCoeffs { | ||
| 7339 | + skip: true, | ||
| 7340 | + inter: Some(info), | ||
| 7341 | + ..BlockCoeffs::default() | ||
| 7342 | + }, | ||
| 7343 | + )) | ||
| 7344 | +} | ||
| 7345 | + | ||
| 7138 | fn search_inter_block( | 7346 | fn search_inter_block( |
| 7139 | luma: &mut Plane, | 7347 | luma: &mut Plane, |
| 7140 | chroma: &mut [Plane; 2], | 7348 | chroma: &mut [Plane; 2], |
| @@ -8776,7 +8984,68 @@ pub(crate) fn encode_inter_frame( | |||
| 8776 | let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); | 8984 | let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); |
| 8777 | for sb_r in rect.sb_row0 as usize..rect.sb_row1 as usize { | 8985 | for sb_r in rect.sb_row0 as usize..rect.sb_row1 as usize { |
| 8778 | for sb_c in rect.sb_col0 as usize..rect.sb_col1 as usize { | 8986 | for sb_c in rect.sb_col0 as usize..rect.sb_col1 as usize { |
| 8779 | - for quadrant in 0..4 { | 8987 | + // lane-b64: the whole superblock as ONE 64x64 block, tried before |
| 8988 | + // its quadrants are (`search_skip_64`) and compared against the | ||
| 8989 | + // four of them below. Only a superblock wholly inside the true | ||
| 8990 | + // frame is offered it: a 64x64 root whose right or bottom part | ||
| 8991 | + // hangs over the edge is legal AV1 (`has_cols`/`has_rows` only | ||
| 8992 | + // ask for half), but its trial would score prediction the frame | ||
| 8993 | + // does not have -- corner-cut, ceiling = the edge superblocks of | ||
| 8994 | + // a frame whose size is not a multiple of 64, which stay split. | ||
| 8995 | + let (x64, y64) = (sb_c * SUPERBLOCK, sb_r * SUPERBLOCK); | ||
| 8996 | + let sb64_legal = b64_root() | ||
| 8997 | + && sb_r * 2 + 1 < rows | ||
| 8998 | + && sb_c * 2 + 1 < cols | ||
| 8999 | + && (sb_c as u32 + 1) * crate::tile::SB_MI <= header.mi_cols | ||
| 9000 | + && (sb_r as u32 + 1) * crate::tile::SB_MI <= header.mi_rows | ||
| 9001 | + && x64 + SUPERBLOCK <= luma.true_width | ||
| 9002 | + && y64 + SUPERBLOCK <= luma.true_height; | ||
| 9003 | + // One lambda for the whole superblock, the same mean the 32x32 | ||
| 9004 | + // level takes over its own four tpl cells: the 64-vs-four-32 | ||
| 9005 | + // comparison below weighs one cost against four, so both sides | ||
| 9006 | + // have to be priced at the same lambda. | ||
| 9007 | + let sb_search = match &tpl_factors { | ||
| 9008 | + Some(f) if sb64_legal => { | ||
| 9009 | + let mean = (0..16) | ||
| 9010 | + .map(|i| f[(sb_r * 4 + i / 4) * tpl_cells_x + sb_c * 4 + i % 4]) | ||
| 9011 | + .sum::<f64>() | ||
| 9012 | + / 16.0; | ||
| 9013 | + Search { lambda: search.lambda * mean, ..search } | ||
| 9014 | + } | ||
| 9015 | + _ => search, | ||
| 9016 | + }; | ||
| 9017 | + let mark = blocks.len(); | ||
| 9018 | + let mut sb64: Option<(f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> = None; | ||
| 9019 | + let mut sb_cost = 0.0f64; | ||
| 9020 | + if sb64_legal { | ||
| 9021 | + let base = snapshot(&luma, &chroma, (x64, y64), SUPERBLOCK); | ||
| 9022 | + let (mi_row, mi_col) = (sb_r * 16, sb_c * 16); | ||
| 9023 | + let stack = | ||
| 9024 | + find_mv_stack(&grid, mi_row, mi_col, 16, 16, LAST_FRAME, mi_cols, mi_rows); | ||
| 9025 | + if let Some((cost, block)) = search_skip_64( | ||
| 9026 | + &mut luma, | ||
| 9027 | + &mut chroma, | ||
| 9028 | + (x64, y64), | ||
| 9029 | + &sb_search, | ||
| 9030 | + reference, | ||
| 9031 | + &stack, | ||
| 9032 | + fctx, | ||
| 9033 | + ) { | ||
| 9034 | + let after = snapshot(&luma, &chroma, (x64, y64), SUPERBLOCK); | ||
| 9035 | + restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &base); | ||
| 9036 | + let cost = cost | ||
| 9037 | + + sb_search.lambda * crate::tile::partition_bits(SUPERBLOCK, false); | ||
| 9038 | + sb64 = Some((cost, block, after)); | ||
| 9039 | + } | ||
| 9040 | + } | ||
| 9041 | + // A superblock whose 64x64 trial already codes almost nothing per | ||
| 9042 | + // pixel is taken outright and its quadrants are never searched -- | ||
| 9043 | + // the same `split_rd_breakout` shape the 32x32 level uses one | ||
| 9044 | + // size up, and where this lever BUYS wall instead of spending it. | ||
| 9045 | + let early64 = sb64 | ||
| 9046 | + .as_ref() | ||
| 9047 | + .is_some_and(|(c, _, _)| split_rd_breakout(*c, SUPERBLOCK, sb_search.lambda)); | ||
| 9048 | + for quadrant in 0..if early64 { 0 } else { 4 } { | ||
| 8780 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 9049 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 8781 | // lane-av1tpl2: this 32x32 block's own lambda -- every RD | 9050 | // lane-av1tpl2: this 32x32 block's own lambda -- every RD |
| 8782 | // decision under it (partition, mode, tx, motion) reads | 9051 | // decision under it (partition, mode, tx, motion) reads |
| @@ -9061,12 +9330,14 @@ pub(crate) fn encode_inter_frame( | |||
| 9061 | // grid, and their reconstruction is what the | 9330 | // grid, and their reconstruction is what the |
| 9062 | // planes hold. | 9331 | // planes hold. |
| 9063 | partition_hit(1); | 9332 | partition_hit(1); |
| 9333 | + sb_cost += cost; | ||
| 9064 | blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); | 9334 | blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); |
| 9065 | continue; | 9335 | continue; |
| 9066 | } | 9336 | } |
| 9067 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | 9337 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); |
| 9068 | } | 9338 | } |
| 9069 | partition_hit(0); | 9339 | partition_hit(0); |
| 9340 | + sb_cost += cost_whole; | ||
| 9070 | record_mi(&mut grid, mi_row, mi_col, 8, block.inter); | 9341 | record_mi(&mut grid, mi_row, mi_col, 8, block.inter); |
| 9071 | blocks.push((r32 * cols + c32, Quadrant::Whole(block))); | 9342 | blocks.push((r32 * cols + c32, Quadrant::Whole(block))); |
| 9072 | } else { | 9343 | } else { |
| @@ -9185,6 +9456,26 @@ pub(crate) fn encode_inter_frame( | |||
| 9185 | blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); | 9456 | blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); |
| 9186 | } | 9457 | } |
| 9187 | } | 9458 | } |
| 9459 | + // The 64x64 root against what its four quadrants really cost. | ||
| 9460 | + if let Some((cost64, block, after)) = sb64 | ||
| 9461 | + && (early64 | ||
| 9462 | + || cost64 | ||
| 9463 | + < sb_cost | ||
| 9464 | + + sb_search.lambda * crate::tile::partition_bits(SUPERBLOCK, true)) | ||
| 9465 | + { | ||
| 9466 | + B64_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 9467 | + blocks.truncate(mark); | ||
| 9468 | + restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &after); | ||
| 9469 | + // Overwrites every 16x16 mi cell the quadrant searches | ||
| 9470 | + // published under this superblock, so the next block's stack | ||
| 9471 | + // is built off the block that was really coded. | ||
| 9472 | + record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter); | ||
| 9473 | + blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block))); | ||
| 9474 | + for q in 1..4 { | ||
| 9475 | + let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); | ||
| 9476 | + blocks.push((r32 * cols + c32, Quadrant::Covered)); | ||
| 9477 | + } | ||
| 9478 | + } | ||
| 9188 | } | 9479 | } |
| 9189 | } | 9480 | } |
| 9190 | let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); | 9481 | let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); |
crates/ec-av1/src/encoder.rsmodified+59 -0
| @@ -2222,6 +2222,65 @@ mod tests { | |||
| 2222 | /// with each other and disagree with nothing -- so the ffmpeg half is | 2222 | /// with each other and disagree with nothing -- so the ffmpeg half is |
| 2223 | /// the one that catches a tile boundary the writer respected and the | 2223 | /// the one that catches a tile boundary the writer respected and the |
| 2224 | /// spec does not, and the parser half catches the reverse. | 2224 | /// spec does not, and the parser half catches the reverse. |
| 2225 | + | ||
| 2226 | + /// A STATIC clip: every inter frame predicts perfectly from the last, so | ||
| 2227 | + /// the search takes the 64x64 root ([`crate::encode::B64_ROOT`]) instead | ||
| 2228 | + /// of coding four 32x32 quadrants, and the stream both decoders | ||
| 2229 | + /// reconstruct is still sample-exact against the encoder's own | ||
| 2230 | + /// reconstruction. The counter is the point (class | ||
| 2231 | + /// `gate-blind-to-feature`): without it a green three-way compare says | ||
| 2232 | + /// nothing about whether a single 64x64 block was ever written. | ||
| 2233 | + #[test] | ||
| 2234 | + fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { | ||
| 2235 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 2236 | + let (width, height) = (256usize, 128usize); | ||
| 2237 | + let still = test_card(width, height, 0); | ||
| 2238 | + let sources: Vec<Picture> = (0..3).map(|_| still.clone()).collect(); | ||
| 2239 | + let config = EncoderConfig { | ||
| 2240 | + width, | ||
| 2241 | + height, | ||
| 2242 | + base_q_idx: 120, | ||
| 2243 | + gop: 8, | ||
| 2244 | + colour: Colour::Bt709Limited, | ||
| 2245 | + tile_cols_log2: 0, | ||
| 2246 | + tile_rows_log2: 0, | ||
| 2247 | + }; | ||
| 2248 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 2249 | + let mut stream = Vec::new(); | ||
| 2250 | + let _ = crate::encode::take_b64_root_hits(); | ||
| 2251 | + for packet in encode_all(&mut enc, &sources) { | ||
| 2252 | + stream.extend_from_slice(&packet.data); | ||
| 2253 | + } | ||
| 2254 | + let roots = crate::encode::take_b64_root_hits(); | ||
| 2255 | + // 8 superblocks per frame, two inter frames. | ||
| 2256 | + assert!( | ||
| 2257 | + roots > 0, | ||
| 2258 | + "a static clip coded no 64x64 root at all (of 16 inter superblocks)" | ||
| 2259 | + ); | ||
| 2260 | + eprintln!("64x64 roots on a static clip: {roots} of 16 inter superblocks"); | ||
| 2261 | + | ||
| 2262 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 2263 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | ||
| 2264 | + if !have_ffmpeg() { | ||
| 2265 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 2266 | + return; | ||
| 2267 | + } | ||
| 2268 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 2269 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | ||
| 2270 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 2271 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 2272 | + if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) { | ||
| 2273 | + panic!( | ||
| 2274 | + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 2275 | + at % width, | ||
| 2276 | + at / width, | ||
| 2277 | + got[at], | ||
| 2278 | + b[at], | ||
| 2279 | + ); | ||
| 2280 | + } | ||
| 2281 | + } | ||
| 2282 | + } | ||
| 2283 | + | ||
| 2225 | #[test] | 2284 | #[test] |
| 2226 | fn every_tile_layout_decodes_sample_exact_through_both_decoders() { | 2285 | fn every_tile_layout_decodes_sample_exact_through_both_decoders() { |
| 2227 | let (width, height) = (640usize, 384usize); | 2286 | let (width, height) = (640usize, 384usize); |
crates/ec-av1/src/speed.rsmodified+6 -0
| @@ -143,6 +143,12 @@ pub(crate) fn at<T: Copy>(table: &[T; 11]) -> T { | |||
| 143 | // table for what each step measured). | 143 | // table for what each step measured). |
| 144 | // --------------------------------------------------------------------------- | 144 | // --------------------------------------------------------------------------- |
| 145 | 145 | ||
| 146 | +/// `encode::B64_ROOT`: the 64x64 `PARTITION_NONE` skip trial at the | ||
| 147 | +/// superblock root. On at every preset -- it is a wall SAVER as well as a bit | ||
| 148 | +/// saver, since a superblock the trial takes is never searched at 32x32 (see | ||
| 149 | +/// the lane report for the measured wall at preset 0 and 6). | ||
| 150 | +pub(crate) const B64_ROOT: [bool; 11] = [crate::encode::B64_ROOT; 11]; | ||
| 151 | + | ||
| 146 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split | 152 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 147 | /// trial is withheld. The single biggest wall lever in the tile search. | 153 | /// trial is withheld. The single biggest wall lever in the tile search. |
| 148 | pub(crate) const SPLIT_RD: [f64; 11] = [ | 154 | pub(crate) const SPLIT_RD: [f64; 11] = [ |
crates/ec-av1/src/tile.rsmodified+158 -6
| @@ -1131,7 +1131,7 @@ pub(crate) const INTRA_MODE_CTX: [usize; INTRA_MODES] = [0, 1, 2, 3, 4, 4, 4, 4, | |||
| 1131 | /// +3, so `MAX_ANGLE_DELTA` is the middle of it. | 1131 | /// +3, so `MAX_ANGLE_DELTA` is the middle of it. |
| 1132 | pub(crate) const ANGLE_DELTA_ZERO: usize = 3; | 1132 | pub(crate) const ANGLE_DELTA_ZERO: usize = 3; |
| 1133 | /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off. | 1133 | /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off. |
| 1134 | -const SB_MI: u32 = 16; | 1134 | +pub(crate) const SB_MI: u32 = 16; |
| 1135 | 1135 | ||
| 1136 | /// `NUM_BASE_LEVELS` (spec 3): levels above this carry a base-range tail. | 1136 | /// `NUM_BASE_LEVELS` (spec 3): levels above this carry a base-range tail. |
| 1137 | const NUM_BASE_LEVELS: i32 = 2; | 1137 | const NUM_BASE_LEVELS: i32 = 2; |
| @@ -1764,14 +1764,28 @@ pub enum Quadrant { | |||
| 1764 | /// The four 16x16 blocks it is split into, in raster order, each with a | 1764 | /// The four 16x16 blocks it is split into, in raster order, each with a |
| 1765 | /// 16x16 luma transform and an 8x8 transform per chroma plane. | 1765 | /// 16x16 luma transform and an 8x8 transform per chroma plane. |
| 1766 | Split(Vec<BlockCoeffs>), | 1766 | Split(Vec<BlockCoeffs>), |
| 1767 | + /// lane-b64: not a quadrant at all -- the whole 64x64 superblock this | ||
| 1768 | + /// quadrant is the TOP-LEFT of, coded as one block (`PARTITION_NONE` at | ||
| 1769 | + /// `BLOCK_64X64`). Only the top-left quadrant of a superblock carries it | ||
| 1770 | + /// and the other three carry [`Covered`](Quadrant::Covered), so the | ||
| 1771 | + /// per-32x32-entry shape the inter writer takes is unchanged. | ||
| 1772 | + /// | ||
| 1773 | + /// The block is coded SKIP and single-reference: a 64x64 residual needs | ||
| 1774 | + /// the forward TX_64X64 the encoder does not have (`transform.rs` tops | ||
| 1775 | + /// out at 32x32), and the writer refuses anything else. | ||
| 1776 | + Whole64(BlockCoeffs), | ||
| 1777 | + /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's | ||
| 1778 | + /// top-left already coded; it carries no block of its own. | ||
| 1779 | + Covered, | ||
| 1767 | } | 1780 | } |
| 1768 | 1781 | ||
| 1769 | impl Quadrant { | 1782 | impl Quadrant { |
| 1770 | /// The blocks it carries, in the order they are coded. | 1783 | /// The blocks it carries, in the order they are coded. |
| 1771 | pub(crate) fn blocks(&self) -> &[BlockCoeffs] { | 1784 | pub(crate) fn blocks(&self) -> &[BlockCoeffs] { |
| 1772 | match self { | 1785 | match self { |
| 1773 | - Quadrant::Whole(block) => std::slice::from_ref(block), | 1786 | + Quadrant::Whole(block) | Quadrant::Whole64(block) => std::slice::from_ref(block), |
| 1774 | Quadrant::Split(blocks) => blocks.as_slice(), | 1787 | Quadrant::Split(blocks) => blocks.as_slice(), |
| 1788 | + Quadrant::Covered => &[], | ||
| 1775 | } | 1789 | } |
| 1776 | } | 1790 | } |
| 1777 | } | 1791 | } |
| @@ -2746,6 +2760,14 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |||
| 2746 | ) | 2760 | ) |
| 2747 | }); | 2761 | }); |
| 2748 | match quadrant { | 2762 | match quadrant { |
| 2763 | + // A 64x64 root is an INTER-frame partition; a | ||
| 2764 | + // key frame codes one as `Superblock::Whole`. | ||
| 2765 | + Quadrant::Whole64(_) | Quadrant::Covered => { | ||
| 2766 | + return Err(Error::unsupported( | ||
| 2767 | + "AV1 tile", | ||
| 2768 | + "a key frame codes a 64x64 block as `Superblock::Whole`", | ||
| 2769 | + )); | ||
| 2770 | + } | ||
| 2749 | Quadrant::Whole(block) => { | 2771 | Quadrant::Whole(block) => { |
| 2750 | if !has_cols32 || !has_rows32 { | 2772 | if !has_cols32 || !has_rows32 { |
| 2751 | return Err(Error::unsupported( | 2773 | return Err(Error::unsupported( |
| @@ -4343,6 +4365,9 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { | |||
| 4343 | let side = match q { | 4365 | let side = match q { |
| 4344 | Quadrant::Whole(_) => 32, | 4366 | Quadrant::Whole(_) => 32, |
| 4345 | Quadrant::Split(_) => 16, | 4367 | Quadrant::Split(_) => 16, |
| 4368 | + // A 64x64 root is skip-only, so it carries no coefficient | ||
| 4369 | + // bits to predict at all (`Quadrant::Whole64`). | ||
| 4370 | + Quadrant::Whole64(_) | Quadrant::Covered => return 0.0, | ||
| 4346 | }; | 4371 | }; |
| 4347 | q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::<f64>() | 4372 | q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::<f64>() |
| 4348 | }) | 4373 | }) |
| @@ -5559,15 +5584,133 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5559 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); | 5584 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 5560 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock | 5585 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock |
| 5561 | // whose bottom or right half falls outside the true frame cannot | 5586 | // whose bottom or right half falls outside the true frame cannot |
| 5562 | - // be left whole, but this writer only ever splits a superblock | 5587 | + // be left whole, so the only question there is which of the three |
| 5563 | - // into its four 32x32 quadrants (never `PARTITION_NONE` at 64x64), | 5588 | + // partition symbols the split takes — same three-way signaling as |
| 5564 | - // so the only question is which of the three partition symbols | ||
| 5565 | - // that split takes — same three-way signaling as | ||
| 5566 | // `sb_coeff_key_frame_tile`'s superblock level. | 5589 | // `sb_coeff_key_frame_tile`'s superblock level. |
| 5567 | let (has_cols, has_rows) = ( | 5590 | let (has_cols, has_rows) = ( |
| 5568 | sb_c * SB_MI + SB_MI / 2 < mi_cols, | 5591 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 5569 | sb_r * SB_MI + SB_MI / 2 < mi_rows, | 5592 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 5570 | ); | 5593 | ); |
| 5594 | + // lane-b64: a superblock the search left WHOLE — one 64x64 block | ||
| 5595 | + // at `PARTITION_NONE`, carried by its top-left quadrant's entry | ||
| 5596 | + // ([`Quadrant::Whole64`]). Coded skip and single-reference: no | ||
| 5597 | + // residual, so no TX_64X64 coefficients and no `cdef_idx`. | ||
| 5598 | + let sb64 = ((sb_r * 2) < rows && (sb_c * 2) < cols) | ||
| 5599 | + .then(|| &blocks[((sb_r * 2) * cols + sb_c * 2) as usize]) | ||
| 5600 | + .and_then(|q| match q { | ||
| 5601 | + Quadrant::Whole64(block) => Some(block), | ||
| 5602 | + _ => None, | ||
| 5603 | + }); | ||
| 5604 | + if let Some(block) = sb64 { | ||
| 5605 | + if !has_cols || !has_rows { | ||
| 5606 | + return Err(Error::unsupported( | ||
| 5607 | + "AV1 tile", | ||
| 5608 | + "a superblock that is half outside the true frame cannot be left whole", | ||
| 5609 | + )); | ||
| 5610 | + } | ||
| 5611 | + let info = block.inter.ok_or_else(|| { | ||
| 5612 | + Error::unsupported("AV1 tile", "a 64x64 root block is coded inter") | ||
| 5613 | + })?; | ||
| 5614 | + if !block.skip || info.ref1.is_some() { | ||
| 5615 | + return Err(Error::unsupported( | ||
| 5616 | + "AV1 tile", | ||
| 5617 | + "a 64x64 root block is coded skip and single-reference \ | ||
| 5618 | + (a 64x64 residual needs a forward TX_64X64)", | ||
| 5619 | + )); | ||
| 5620 | + } | ||
| 5621 | + enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[sb_ctx]); | ||
| 5622 | + let (mi_r, mi_c) = (sb_at.0 * (SUB / MI), sb_at.1 * (SUB / MI)); | ||
| 5623 | + let has_above = neighbours.has_above(mi_r); | ||
| 5624 | + let has_left = neighbours.has_left(mi_c); | ||
| 5625 | + let skip_ctx = usize::from(neighbours.above_skip[mi_c]) | ||
| 5626 | + + usize::from(neighbours.left_skip[mi_r]); | ||
| 5627 | + enc.symbol(1, &mut cdfs.skip[skip_ctx]); | ||
| 5628 | + write_cdef_idx(&mut enc, (mi_r, mi_c), true); | ||
| 5629 | + let ii_ctx = intra_inter_ctx( | ||
| 5630 | + has_above, | ||
| 5631 | + has_left, | ||
| 5632 | + neighbours.above_inter[mi_c], | ||
| 5633 | + neighbours.left_inter[mi_r], | ||
| 5634 | + ); | ||
| 5635 | + enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); | ||
| 5636 | + write_comp_mode( | ||
| 5637 | + &mut enc, | ||
| 5638 | + &mut cdfs, | ||
| 5639 | + &neighbours, | ||
| 5640 | + (mi_r, mi_c), | ||
| 5641 | + (has_above, has_left), | ||
| 5642 | + false, | ||
| 5643 | + ); | ||
| 5644 | + write_single_ref( | ||
| 5645 | + &mut enc, | ||
| 5646 | + &mut cdfs, | ||
| 5647 | + info.ref_frame, | ||
| 5648 | + neighbours.above_ref[mi_c], | ||
| 5649 | + neighbours.above_ref1[mi_c], | ||
| 5650 | + neighbours.left_ref[mi_r], | ||
| 5651 | + neighbours.left_ref1[mi_r], | ||
| 5652 | + ); | ||
| 5653 | + let stack = find_mv_stack( | ||
| 5654 | + &grid, | ||
| 5655 | + mi_r, | ||
| 5656 | + mi_c, | ||
| 5657 | + SB_MI as usize, | ||
| 5658 | + SB_MI as usize, | ||
| 5659 | + info.ref_frame, | ||
| 5660 | + mi_cols as usize, | ||
| 5661 | + mi_rows as usize, | ||
| 5662 | + ); | ||
| 5663 | + let (mv, is_new_mv) = write_inter_mode(&mut enc, &mut cdfs, info, &stack)?; | ||
| 5664 | + for dr in 0..SB_MI as usize { | ||
| 5665 | + for dc in 0..SB_MI as usize { | ||
| 5666 | + grid.set( | ||
| 5667 | + mi_r + dr, | ||
| 5668 | + mi_c + dc, | ||
| 5669 | + MiInfo { | ||
| 5670 | + is_inter: true, | ||
| 5671 | + ref_frame: info.ref_frame, | ||
| 5672 | + ref_frame1: NO_REF1, | ||
| 5673 | + mv1: (0, 0), | ||
| 5674 | + mv: mv16(mv), | ||
| 5675 | + is_new_mv, | ||
| 5676 | + size: SB_MI as u8, | ||
| 5677 | + size_h: SB_MI as u8, | ||
| 5678 | + is_global_mv0: false, | ||
| 5679 | + is_global_mv1: false, | ||
| 5680 | + }, | ||
| 5681 | + ); | ||
| 5682 | + } | ||
| 5683 | + } | ||
| 5684 | + write_motion_mode( | ||
| 5685 | + &mut enc, | ||
| 5686 | + &mut cdfs, | ||
| 5687 | + &grid, | ||
| 5688 | + (mi_r, mi_c), | ||
| 5689 | + (SB_MI as usize, SB_MI as usize), | ||
| 5690 | + (SB, SB), | ||
| 5691 | + (mi_cols as usize, mi_rows as usize), | ||
| 5692 | + info.ref_frame, | ||
| 5693 | + block.motion_mode, | ||
| 5694 | + )?; | ||
| 5695 | + if tx_select { | ||
| 5696 | + // A skipped inter block writes no `txfm_split` at all -- | ||
| 5697 | + // this only publishes the max transform size the reader | ||
| 5698 | + // infers for it (`write_tx_syntax_inter`). | ||
| 5699 | + write_tx_syntax_inter( | ||
| 5700 | + &mut enc, | ||
| 5701 | + &mut cdfs, | ||
| 5702 | + &mut neighbours, | ||
| 5703 | + (mi_r, mi_c), | ||
| 5704 | + SB, | ||
| 5705 | + true, | ||
| 5706 | + true, | ||
| 5707 | + 0, | ||
| 5708 | + ); | ||
| 5709 | + } | ||
| 5710 | + neighbours.record(sb_at, SB, 0, &zero_grids); | ||
| 5711 | + neighbours.record_inter(sb_at, SB, true, true, block_ref(block)); | ||
| 5712 | + continue; | ||
| 5713 | + } | ||
| 5571 | match (has_cols, has_rows) { | 5714 | match (has_cols, has_rows) { |
| 5572 | (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]), | 5715 | (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]), |
| 5573 | (true, false) => { | 5716 | (true, false) => { |
| @@ -5600,6 +5743,15 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5600 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), | 5743 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 5601 | ); | 5744 | ); |
| 5602 | let block = match site { | 5745 | let block = match site { |
| 5746 | + // Already coded by the `Whole64` above -- but only inside | ||
| 5747 | + // a superblock that really carried one. | ||
| 5748 | + Quadrant::Whole64(_) | Quadrant::Covered => { | ||
| 5749 | + return Err(Error::unsupported( | ||
| 5750 | + "AV1 tile", | ||
| 5751 | + "a `Whole64`/`Covered` quadrant needs its superblock's \ | ||
| 5752 | + top-left entry to be the `Whole64`", | ||
| 5753 | + )); | ||
| 5754 | + } | ||
| 5603 | Quadrant::Whole(block) => { | 5755 | Quadrant::Whole(block) => { |
| 5604 | if !has_cols32 || !has_rows32 { | 5756 | if !has_cols32 || !has_rows32 { |
| 5605 | return Err(Error::unsupported( | 5757 | return Err(Error::unsupported( |