Merge branch 'lane-av1txbits' (59679eed): per-class pricer census; the frame-tables coefficient price ships as a knob
Browse files at this commit3 changed files +212 -9
- crates/ec-av1/src/cdf_state.rs +6 -1
- crates/ec-av1/src/encode.rs +75 -1
- crates/ec-av1/src/tile.rs +131 -7
crates/ec-av1/src/cdf_state.rsmodified+6 -1
| @@ -14,7 +14,7 @@ | |||
| 14 | use crate::cdf; | 14 | use crate::cdf; |
| 15 | 15 | ||
| 16 | /// Which of the four coefficient table sets a transform block is coded with. | 16 | /// Which of the four coefficient table sets a transform block is coded with. |
| 17 | -#[derive(Clone, Copy)] | 17 | +#[derive(Clone, Copy, Debug)] |
| 18 | pub(crate) enum TxbSet { | 18 | pub(crate) enum TxbSet { |
| 19 | /// The 32x32 luma transform of an intra 32x32 block. `get_tx_set` is | 19 | /// The 32x32 luma transform of an intra 32x32 block. `get_tx_set` is |
| 20 | /// DCT-only at this size for an intra block, so no `tx_type` symbol is | 20 | /// DCT-only at this size for an intra block, so no `tx_type` symbol is |
| @@ -303,6 +303,10 @@ pub(crate) struct TxbTables<'a> { | |||
| 303 | /// Every table a key frame's tile writer adapts. | 303 | /// Every table a key frame's tile writer adapts. |
| 304 | #[derive(Clone)] | 304 | #[derive(Clone)] |
| 305 | pub(crate) struct Cdfs { | 305 | pub(crate) struct Cdfs { |
| 306 | + /// The coefficient q-context these tables were built for (spec 8.3.2's | ||
| 307 | + /// `Get_Qctx`), so a writer can price a block against the same tables the | ||
| 308 | + /// search did (`tile::coeff_bits`). | ||
| 309 | + pub q_ctx: usize, | ||
| 306 | /// The partition symbol of a 64x64 superblock. | 310 | /// The partition symbol of a 64x64 superblock. |
| 307 | /// `partition_cdf`'s `BLOCK_128X128` rows (8 symbols). | 311 | /// `partition_cdf`'s `BLOCK_128X128` rows (8 symbols). |
| 308 | pub partition_w128: [[u16; 9]; 4], | 312 | pub partition_w128: [[u16; 9]; 4], |
| @@ -968,6 +972,7 @@ impl Cdfs { | |||
| 968 | /// quantizer, so they are the same for every `q_ctx`. | 972 | /// quantizer, so they are the same for every `q_ctx`. |
| 969 | pub fn new(q_ctx: usize) -> Cdfs { | 973 | pub fn new(q_ctx: usize) -> Cdfs { |
| 970 | Cdfs { | 974 | Cdfs { |
| 975 | + q_ctx, | ||
| 971 | partition_w128: cdf::PARTITION_W128, | 976 | partition_w128: cdf::PARTITION_W128, |
| 972 | partition_w64: cdf::PARTITION_W64, | 977 | partition_w64: cdf::PARTITION_W64, |
| 973 | partition_w32: cdf::PARTITION_W32, | 978 | partition_w32: cdf::PARTITION_W32, |
crates/ec-av1/src/encode.rsmodified+75 -1
| @@ -3821,7 +3821,14 @@ fn code_square_inter( | |||
| 3821 | // compound block's residual is small enough that the four extra | 3821 | // compound block's residual is small enough that the four extra |
| 3822 | // per-unit txb_skip/eob symbols cost more ladder bytes than this | 3822 | // per-unit txb_skip/eob symbols cost more ladder bytes than this |
| 3823 | // encoder's static-CDF estimate prices them at, so the local RD | 3823 | // encoder's static-CDF estimate prices them at, so the local RD |
| 3824 | - // win does not convert. The flat trial stays. | 3824 | + // win does not convert. lane-av1txbits REBUILT it on top of a |
| 3825 | + // search that prices against the tables the frame is really | ||
| 3826 | + // written with (`tile::arm_pricing_cdfs`, the estimate error on | ||
| 3827 | + // these very blocks measured down from +31% to +3%) and measured | ||
| 3828 | + // it neutral-to-worse again: +78.8/+95.0/+54.5 and +36.5/+56.3/ | ||
| 3829 | + // +1.4 against the +78.8/+95.1/+54.5 and +36.1/+56.4/+1.4 of the | ||
| 3830 | + // same build without it. The static-CDF estimate was NOT what | ||
| 3831 | + // held the compound var-tx trial back. The flat trial stays. | ||
| 3825 | let (levels, tx_depth, dcost) = if info.ref1.is_some() { | 3832 | let (levels, tx_depth, dcost) = if info.ref1.is_some() { |
| 3826 | luma.commit(x, y, side, &luma_new); | 3833 | luma.commit(x, y, side, &luma_new); |
| 3827 | (luma_new.levels.clone(), 0, 0.0) | 3834 | (luma_new.levels.clone(), 0, 0.0) |
| @@ -4535,6 +4542,10 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4535 | (picture.height).min(render.1), | 4542 | (picture.height).min(render.1), |
| 4536 | ); | 4543 | ); |
| 4537 | arm_seq_screen(screen); | 4544 | arm_seq_screen(screen); |
| 4545 | + // A key frame's writer really does start from the defaults, and a search | ||
| 4546 | + // worker may be carrying an inter frame's armed tables (lane-av1txbits): | ||
| 4547 | + // disarm, or a key frame's bits would depend on which thread searched it. | ||
| 4548 | + crate::tile::arm_pricing_cdfs(None); | ||
| 4538 | // spec 5.9.2 `allow_intrabc`, screen-detected key frames only: the bit | 4549 | // spec 5.9.2 `allow_intrabc`, screen-detected key frames only: the bit |
| 4539 | // forces deblocking, CDEF and loop restoration OFF for this frame | 4550 | // forces deblocking, CDEF and loop restoration OFF for this frame |
| 4540 | // (frame.rs:194/210/252/272), so it is only set when the source repeats | 4551 | // (frame.rs:194/210/252/272), so it is only set when the source repeats |
| @@ -4674,6 +4685,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4674 | let search_tile = |index: usize, | 4685 | let search_tile = |index: usize, |
| 4675 | fctx: &crate::decode::FrameCtx| | 4686 | fctx: &crate::decode::FrameCtx| |
| 4676 | -> Result<Vec<(usize, Superblock, Vec<u8>)>> { | 4687 | -> Result<Vec<(usize, Superblock, Vec<u8>)>> { |
| 4688 | + crate::tile::arm_pricing_cdfs(None); | ||
| 4677 | let rect = layout.rect(index); | 4689 | let rect = layout.rect(index); |
| 4678 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); | 4690 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); |
| 4679 | let mut chroma = [ | 4691 | let mut chroma = [ |
| @@ -4951,6 +4963,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4951 | let mut cdfs = start_cdfs.0.clone(); | 4963 | let mut cdfs = start_cdfs.0.clone(); |
| 4952 | crate::tile::arm_screen(screen); | 4964 | crate::tile::arm_screen(screen); |
| 4953 | crate::tile::arm_intrabc(allow_intrabc); | 4965 | crate::tile::arm_intrabc(allow_intrabc); |
| 4966 | + crate::tile::arm_pricing_cdfs(None); | ||
| 4954 | let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs( | 4967 | let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs( |
| 4955 | mi_cols, | 4968 | mi_cols, |
| 4956 | mi_rows, | 4969 | mi_rows, |
| @@ -6227,6 +6240,15 @@ fn search_inter_block( | |||
| 6227 | } | 6240 | } |
| 6228 | 6241 | ||
| 6229 | let best = best.expect("the search offers at least the intra modes"); | 6242 | let best = best.expect("the search offers at least the intra modes"); |
| 6243 | + // lane-av1txbits BUILT the transform-depth search this path lacks -- a | ||
| 6244 | + // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 | ||
| 6245 | + // units against its flat trial, exactly as a 16x16 winner is -- and | ||
| 6246 | + // MEASURED it flat to slightly worse: +78.7/+95.1/+54.6 vs libaom and | ||
| 6247 | + // +36.1/+56.2/+1.8 vs rav1e, against the +78.8/+95.1/+54.5 and | ||
| 6248 | + // +36.1/+56.4/+1.4 this code scores, while the split fired on 40.2% of | ||
| 6249 | + // the film's inter blocks and 17.6% of the screen's. The same shape as | ||
| 6250 | + // the compound trial set below: a local RD win that does not convert into | ||
| 6251 | + // ladder bytes. The flat 32x32 transform stays. | ||
| 6230 | luma.commit(x, y, BLOCK, &best.luma); | 6252 | luma.commit(x, y, BLOCK, &best.luma); |
| 6231 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); | 6253 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 6232 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 6254 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| @@ -6381,6 +6403,9 @@ pub(crate) fn encode_inter_frame( | |||
| 6381 | // read `(0, 0, [0; 7])` and came out 1 for every reference -- the knob | 6403 | // read `(0, 0, [0; 7])` and came out 1 for every reference -- the knob |
| 6382 | // never reached the tool). | 6404 | // never reached the tool). |
| 6383 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | 6405 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); |
| 6406 | + // The search prices coefficients against the tables this frame's writer | ||
| 6407 | + // starts from, not against the defaults (lane-av1txbits). | ||
| 6408 | + crate::tile::arm_pricing_cdfs(start_cdfs); | ||
| 6384 | if let Some(p) = pyramid { | 6409 | if let Some(p) = pyramid { |
| 6385 | header.show_frame = p.show_frame; | 6410 | header.show_frame = p.show_frame; |
| 6386 | header.showable_frame = !p.show_frame; | 6411 | header.showable_frame = !p.show_frame; |
| @@ -6501,6 +6526,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6501 | // search step) is read by the SEARCH, so every worker arms it or the | 6526 | // search step) is read by the SEARCH, so every worker arms it or the |
| 6502 | // tile's bits depend on the thread count. | 6527 | // tile's bits depend on the thread count. |
| 6503 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | 6528 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); |
| 6529 | + crate::tile::arm_pricing_cdfs(start_cdfs); | ||
| 6504 | let rect = layout.rect(index); | 6530 | let rect = layout.rect(index); |
| 6505 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); | 6531 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); |
| 6506 | let mut chroma = [ | 6532 | let mut chroma = [ |
| @@ -6971,6 +6997,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6971 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); | 6997 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); |
| 6972 | crate::tile::arm_screen(screen); | 6998 | crate::tile::arm_screen(screen); |
| 6973 | let mut cdfs = start_cdfs.0.clone(); | 6999 | let mut cdfs = start_cdfs.0.clone(); |
| 7000 | + crate::tile::arm_pricing_cdfs(Some(&cdfs)); | ||
| 6974 | // Each tile's own MV grid: a candidate scan never reaches past | 7001 | // Each tile's own MV grid: a candidate scan never reaches past |
| 6975 | // the tile's bounds, so a grid holding only this tile's own | 7002 | // the tile's bounds, so a grid holding only this tile's own |
| 6976 | // blocks reads exactly as the decoder's frame-wide one does | 7003 | // blocks reads exactly as the decoder's frame-wide one does |
| @@ -9459,6 +9486,10 @@ mod tests { | |||
| 9459 | // (compound share 8.8% -> 11.5%): a compound block carries a second | 9486 | // (compound share 8.8% -> 11.5%): a compound block carries a second |
| 9460 | // reference tree, a compound mode symbol and -- for the half-new modes | 9487 | // reference tree, a compound mode symbol and -- for the half-new modes |
| 9461 | // -- an MV residual, none of which the coefficient sum counts. | 9488 | // -- an MV residual, none of which the coefficient sum counts. |
| 9489 | + // lane-av1txbits: under `EC_AV1_PRICE_FRAME_CDFS=1` this reads +32.6% | ||
| 9490 | + // instead -- pricing against the tables the frame's writer really | ||
| 9491 | + // starts from collapses the over-price above to nothing, so the whole | ||
| 9492 | + // remaining gap is the syntax the coefficient sum leaves out. | ||
| 9462 | assert!( | 9493 | assert!( |
| 9463 | worst_under <= 0.32, | 9494 | worst_under <= 0.32, |
| 9464 | "the writer spent {:.2}% more than the search priced -- more than \ | 9495 | "the writer spent {:.2}% more than the search priced -- more than \ |
| @@ -9467,6 +9498,49 @@ mod tests { | |||
| 9467 | ); | 9498 | ); |
| 9468 | } | 9499 | } |
| 9469 | 9500 | ||
| 9501 | + /// The census behind step (1) of the pricer lane: for every transform | ||
| 9502 | + /// block the writer really codes, what the search priced those levels at | ||
| 9503 | + /// against what the writer spent on them, split by table set and non-zero | ||
| 9504 | + /// count. One average hides the interesting part -- the classes the | ||
| 9505 | + /// estimate is systematically wrong about (all-zero blocks, the | ||
| 9506 | + /// one-coefficient blocks the inter search lives on) -- so the split is | ||
| 9507 | + /// printed and only the total is asserted on. | ||
| 9508 | + #[test] | ||
| 9509 | + fn pricer_error_census_by_block_class() { | ||
| 9510 | + let fctx = &crate::decode::FrameCtx::new(); | ||
| 9511 | + let (width, height) = (256usize, 128usize); | ||
| 9512 | + let pictures: Vec<Picture> = (0..5) | ||
| 9513 | + .map(|i| panned_test_card(width, height, i * 2)) | ||
| 9514 | + .collect(); | ||
| 9515 | + crate::tile::pricer_census_on(); | ||
| 9516 | + let _ = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); | ||
| 9517 | + let rows = crate::tile::take_pricer_census(); | ||
| 9518 | + assert!(!rows.is_empty(), "the census saw no transform block"); | ||
| 9519 | + | ||
| 9520 | + println!("| set | nz | blocks | priced bits | written bits | error |"); | ||
| 9521 | + println!("|---|---|---|---|---|---|"); | ||
| 9522 | + let (mut priced_all, mut written_all) = (0.0, 0.0); | ||
| 9523 | + for ((set, bucket), (blocks, priced, written)) in &rows { | ||
| 9524 | + priced_all += priced; | ||
| 9525 | + written_all += written; | ||
| 9526 | + println!( | ||
| 9527 | + "| {set} | {} | {blocks} | {priced:.0} | {written:.0} | {:+.1}% |", | ||
| 9528 | + crate::tile::census_bucket_label(*bucket), | ||
| 9529 | + if *written > 0.0 { | ||
| 9530 | + (priced - written) / written * 100.0 | ||
| 9531 | + } else { | ||
| 9532 | + 0.0 | ||
| 9533 | + } | ||
| 9534 | + ); | ||
| 9535 | + } | ||
| 9536 | + println!( | ||
| 9537 | + "| ALL | | {} | {priced_all:.0} | {written_all:.0} | {:+.1}% |", | ||
| 9538 | + rows.values().map(|r| r.0).sum::<u64>(), | ||
| 9539 | + (priced_all - written_all) / written_all * 100.0 | ||
| 9540 | + ); | ||
| 9541 | + assert!(written_all > 0.0); | ||
| 9542 | + } | ||
| 9543 | + | ||
| 9470 | /// Low motion has to actually buy something: at least one inter frame of | 9544 | /// Low motion has to actually buy something: at least one inter frame of |
| 9471 | /// a panning sequence has to cost fewer bytes than the key frame that | 9545 | /// a panning sequence has to cost fewer bytes than the key frame that |
| 9472 | /// starts it, and the inter-block share it prints has to be non-zero -- | 9546 | /// starts it, and the inter-block share it prints has to be non-zero -- |
crates/ec-av1/src/tile.rsmodified+131 -7
| @@ -10,6 +10,8 @@ | |||
| 10 | //! off as they arrive. | 10 | //! off as they arrive. |
| 11 | 11 | ||
| 12 | use std::cell::RefCell; | 12 | use std::cell::RefCell; |
| 13 | +#[cfg(test)] | ||
| 14 | +use std::collections::BTreeMap; | ||
| 13 | use std::sync::LazyLock; | 15 | use std::sync::LazyLock; |
| 14 | 16 | ||
| 15 | use ec_core::{Error, Result}; | 17 | use ec_core::{Error, Result}; |
| @@ -3513,6 +3515,10 @@ fn write_block_planes( | |||
| 3513 | } else { | 3515 | } else { |
| 3514 | usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded) | 3516 | usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded) |
| 3515 | }; | 3517 | }; |
| 3518 | + #[cfg(test)] | ||
| 3519 | + let before = enc.tell(); | ||
| 3520 | + #[cfg(test)] | ||
| 3521 | + let q_ctx = cdfs.q_ctx; | ||
| 3516 | write_coeffs( | 3522 | write_coeffs( |
| 3517 | enc, | 3523 | enc, |
| 3518 | &mut cdfs.txb(planes[plane], mode), | 3524 | &mut cdfs.txb(planes[plane], mode), |
| @@ -3522,6 +3528,8 @@ fn write_block_planes( | |||
| 3522 | dc_sign_ctx(around[plane].dc_vote), | 3528 | dc_sign_ctx(around[plane].dc_vote), |
| 3523 | Some(plane), | 3529 | Some(plane), |
| 3524 | ); | 3530 | ); |
| 3531 | + #[cfg(test)] | ||
| 3532 | + census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before)); | ||
| 3525 | ec_rng_trace(|| { | 3533 | ec_rng_trace(|| { |
| 3526 | format!( | 3534 | format!( |
| 3527 | "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}", | 3535 | "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}", |
| @@ -3602,6 +3610,10 @@ fn write_luma_tus( | |||
| 3602 | neighbours.luma_skip_ctx(tu_mi, tx / MI) | 3610 | neighbours.luma_skip_ctx(tu_mi, tx / MI) |
| 3603 | }; | 3611 | }; |
| 3604 | let around = neighbours.around_mi(tu_mi, tx)[0]; | 3612 | let around = neighbours.around_mi(tu_mi, tx)[0]; |
| 3613 | + #[cfg(test)] | ||
| 3614 | + let before = enc.tell(); | ||
| 3615 | + #[cfg(test)] | ||
| 3616 | + let q_ctx = cdfs.q_ctx; | ||
| 3605 | write_coeffs( | 3617 | write_coeffs( |
| 3606 | enc, | 3618 | enc, |
| 3607 | &mut cdfs.txb(set, mode), | 3619 | &mut cdfs.txb(set, mode), |
| @@ -3611,6 +3623,8 @@ fn write_luma_tus( | |||
| 3611 | dc_sign_ctx(around.dc_vote), | 3623 | dc_sign_ctx(around.dc_vote), |
| 3612 | Some(0), | 3624 | Some(0), |
| 3613 | ); | 3625 | ); |
| 3626 | + #[cfg(test)] | ||
| 3627 | + census_record(set, q_ctx, &unit, f64::from(enc.tell() - before)); | ||
| 3614 | if n > 1 { | 3628 | if n > 1 { |
| 3615 | neighbours.record_mi_luma(tu_mi, tx, &unit); | 3629 | neighbours.record_mi_luma(tu_mi, tx, &unit); |
| 3616 | } | 3630 | } |
| @@ -3970,13 +3984,14 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { | |||
| 3970 | // q-context per thread beside a pristine one and restore just those from | 3984 | // q-context per thread beside a pristine one and restore just those from |
| 3971 | // it. The state the price is taken against is identical to a fresh | 3985 | // it. The state the price is taken against is identical to a fresh |
| 3972 | // `Cdfs::new(q_ctx)`, so every price is bit-identical. | 3986 | // `Cdfs::new(q_ctx)`, so every price is bit-identical. |
| 3973 | - thread_local! { | 3987 | + // lane-av1txbits: an inter frame's writer does NOT start from the |
| 3974 | - static PRICING: RefCell<[Option<Box<(Cdfs, Cdfs)>>; 4]> = | 3988 | + // defaults -- it starts from the tables the previous frame stored (spec |
| 3975 | - const { RefCell::new([None, None, None, None]) }; | 3989 | + // 7.20, `start_cdfs` in `encode_inter_frame`) -- so pricing against the |
| 3976 | - } | 3990 | + // defaults charged the search for a narrowing the writer no longer pays. |
| 3977 | - PRICING.with_borrow_mut(|slots| { | 3991 | + // The census measured that at +31% on the one-coefficient inter blocks |
| 3978 | - let slot = slots[q_ctx.min(3)] | 3992 | + // the search lives on. `arm_pricing_cdfs` puts the frame's own starting |
| 3979 | - .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx)))); | 3993 | + // tables under the price; nothing armed keeps the old default behaviour. |
| 3994 | + let price = |slot: &mut (Cdfs, Cdfs)| { | ||
| 3980 | let (pristine, scratch) = (&mut slot.0, &mut slot.1); | 3995 | let (pristine, scratch) = (&mut slot.0, &mut slot.1); |
| 3981 | let src = pristine.txb(set, DC_PRED); | 3996 | let src = pristine.txb(set, DC_PRED); |
| 3982 | let mut coding = scratch.txb(set, DC_PRED); | 3997 | let mut coding = scratch.txb(set, DC_PRED); |
| @@ -4002,9 +4017,118 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { | |||
| 4002 | let mut enc = SymbolEncoder::pricer(); | 4017 | let mut enc = SymbolEncoder::pricer(); |
| 4003 | write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); | 4018 | write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); |
| 4004 | enc.bits() | 4019 | enc.bits() |
| 4020 | + }; | ||
| 4021 | + PRICING_BASE.with_borrow_mut(|base| { | ||
| 4022 | + if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) { | ||
| 4023 | + return price(slot); | ||
| 4024 | + } | ||
| 4025 | + PRICING.with_borrow_mut(|slots| { | ||
| 4026 | + let slot = slots[q_ctx.min(3)] | ||
| 4027 | + .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx)))); | ||
| 4028 | + price(slot) | ||
| 4029 | + }) | ||
| 4005 | }) | 4030 | }) |
| 4006 | } | 4031 | } |
| 4007 | 4032 | ||
| 4033 | +thread_local! { | ||
| 4034 | + /// The default tables every price falls back to, one pristine/scratch | ||
| 4035 | + /// pair per q-context (see [`coeff_bits`] for why the pair is kept). | ||
| 4036 | + static PRICING: RefCell<[Option<Box<(Cdfs, Cdfs)>>; 4]> = | ||
| 4037 | + const { RefCell::new([None, None, None, None]) }; | ||
| 4038 | + /// The tables the frame being searched will really be written against, | ||
| 4039 | + /// armed per frame by the encoder ([`arm_pricing_cdfs`]) on every thread | ||
| 4040 | + /// that prices -- the search jobs re-arm their own worker, exactly like | ||
| 4041 | + /// the other tile thread-locals. | ||
| 4042 | + static PRICING_BASE: RefCell<Option<Box<(Cdfs, Cdfs)>>> = const { RefCell::new(None) }; | ||
| 4043 | +} | ||
| 4044 | + | ||
| 4045 | +/// Arms the tables [`coeff_bits`] prices against on this thread: the state | ||
| 4046 | +/// this frame's tile writer starts from. `None` goes back to the per-q-context | ||
| 4047 | +/// defaults (a key frame, whose writer really does start there). | ||
| 4048 | +/// | ||
| 4049 | +/// OFF by default (`EC_AV1_PRICE_FRAME_CDFS=1`). It is the more accurate | ||
| 4050 | +/// estimate by measurement -- the pricer census | ||
| 4051 | +/// (`pricer_error_census_by_block_class`) goes from +7.6% to +4.8% over every | ||
| 4052 | +/// transform block, and the one- and two-coefficient inter blocks the search | ||
| 4053 | +/// lives on from +31.4%/+22.3% to +3.1%/+3.6% -- but the accuracy does not | ||
| 4054 | +/// convert into ladder bytes on every clip: on the 640x384 gate it scores | ||
| 4055 | +/// +78.8/+95.1/+54.5 vs libaom and +36.1/+56.4/+1.4 vs rav1e against the | ||
| 4056 | +/// +79.9/+94.6/+54.5 and +37.3/+55.7/+1.1 the defaults score (film 1080p | ||
| 4057 | +/// down 1.1, film 2160p up 0.5, screen flat), and on the native crops | ||
| 4058 | +/// +18.0/+47.3/+60.7 and +0.7/+19.2/-7.3 against +18.9/+48.2/+59.3 and | ||
| 4059 | +/// +1.5/+19.8/-10.0 (both film clips down against both references, screen up | ||
| 4060 | +/// 1.4/2.7). Two clips better, one worse: it does not meet this lane's keep | ||
| 4061 | +/// rule, so it ships as a knob rather than as the default. | ||
| 4062 | +pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { | ||
| 4063 | + static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | ||
| 4064 | + let on = *ON.get_or_init(|| std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() == Ok("1")); | ||
| 4065 | + PRICING_BASE.with_borrow_mut(|slot| { | ||
| 4066 | + *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone()))); | ||
| 4067 | + }); | ||
| 4068 | +} | ||
| 4069 | + | ||
| 4070 | +/// The pricer census (`pricer_census_on`): for every transform block the tile | ||
| 4071 | +/// writer really codes, the bits it spent against the bits [`coeff_bits`] | ||
| 4072 | +/// prices those very levels at -- the search's rate term measured against the | ||
| 4073 | +/// rate it is meant to predict. Keyed by (table set, non-zero bucket) so a | ||
| 4074 | +/// class that is systematically mispriced (all-zero blocks' `txb_skip`, the | ||
| 4075 | +/// eob of a one-coefficient block, a whole size) shows up on its own row | ||
| 4076 | +/// rather than inside one average. | ||
| 4077 | +/// Thread-local, so a census cannot pick up the blocks of another test's | ||
| 4078 | +/// encode running beside it; a multi-tile, multi-threaded encode would | ||
| 4079 | +/// therefore only census the tiles this thread wrote, which is every tile of | ||
| 4080 | +/// the one-tile-one-thread encodes the gates print it from. | ||
| 4081 | +#[cfg(test)] | ||
| 4082 | +type CensusRows = BTreeMap<(String, usize), (u64, f64, f64)>; | ||
| 4083 | +#[cfg(test)] | ||
| 4084 | +thread_local! { | ||
| 4085 | + static CENSUS: RefCell<Option<CensusRows>> = const { RefCell::new(None) }; | ||
| 4086 | +} | ||
| 4087 | + | ||
| 4088 | +/// Starts a census on this thread, discarding anything a previous one left. | ||
| 4089 | +#[cfg(test)] | ||
| 4090 | +pub(crate) fn pricer_census_on() { | ||
| 4091 | + CENSUS.with_borrow_mut(|c| *c = Some(BTreeMap::new())); | ||
| 4092 | +} | ||
| 4093 | + | ||
| 4094 | +/// Stops the census and hands back its rows: (set, non-zero bucket) -> | ||
| 4095 | +/// (blocks, priced bits, written bits). | ||
| 4096 | +#[cfg(test)] | ||
| 4097 | +pub(crate) fn take_pricer_census() -> CensusRows { | ||
| 4098 | + CENSUS.with_borrow_mut(|c| c.take()).unwrap_or_default() | ||
| 4099 | +} | ||
| 4100 | + | ||
| 4101 | +/// The label of a non-zero bucket, for the tables the gates print. | ||
| 4102 | +#[cfg(test)] | ||
| 4103 | +pub(crate) fn census_bucket_label(bucket: usize) -> &'static str { | ||
| 4104 | + ["0", "1", "2", "3-4", "5-8", "9-16", "17+"][bucket] | ||
| 4105 | +} | ||
| 4106 | + | ||
| 4107 | +#[cfg(test)] | ||
| 4108 | +fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) { | ||
| 4109 | + if CENSUS.with_borrow(|c| c.is_none()) { | ||
| 4110 | + return; | ||
| 4111 | + } | ||
| 4112 | + let nz = grid.iter().filter(|&&l| l != 0).count(); | ||
| 4113 | + let bucket = match nz { | ||
| 4114 | + 0 => 0, | ||
| 4115 | + 1 => 1, | ||
| 4116 | + 2 => 2, | ||
| 4117 | + 3..=4 => 3, | ||
| 4118 | + 5..=8 => 4, | ||
| 4119 | + 9..=16 => 5, | ||
| 4120 | + _ => 6, | ||
| 4121 | + }; | ||
| 4122 | + let priced = coeff_bits(grid, set, q_ctx); | ||
| 4123 | + CENSUS.with_borrow_mut(|rows| { | ||
| 4124 | + let rows = rows.as_mut().expect("the census is on"); | ||
| 4125 | + let row = rows.entry((format!("{set:?}"), bucket)).or_default(); | ||
| 4126 | + row.0 += 1; | ||
| 4127 | + row.1 += priced; | ||
| 4128 | + row.2 += written; | ||
| 4129 | + }); | ||
| 4130 | +} | ||
| 4131 | + | ||
| 4008 | /// What the partition symbol of a block of `side` samples costs, in bits, when | 4132 | /// What the partition symbol of a block of `side` samples costs, in bits, when |
| 4009 | /// it says the block is split (or that it is not). | 4133 | /// it says the block is split (or that it is not). |
| 4010 | /// | 4134 | /// |