Price coefficients against the frame's real tables on non-screen frames
Browse files at this commit7639e56e8c16a9ae7b581b45ccdfa46461c6eda5Parent: 1dee32d- feat
- price coefficients against the frame's real tables on non-screen frames
2 changed files +127 -11
- crates/ec-av1/src/encode.rs +96 -8
- crates/ec-av1/src/tile.rs +31 -3
crates/ec-av1/src/encode.rsmodified+96 -8
| @@ -4545,7 +4545,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4545 | // A key frame's writer really does start from the defaults, and a search | 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): | 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. | 4547 | // disarm, or a key frame's bits would depend on which thread searched it. |
| 4548 | - crate::tile::arm_pricing_cdfs(None); | 4548 | + crate::tile::arm_pricing_cdfs(None, false); |
| 4549 | // 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 |
| 4550 | // forces deblocking, CDEF and loop restoration OFF for this frame | 4550 | // forces deblocking, CDEF and loop restoration OFF for this frame |
| 4551 | // (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 |
| @@ -4685,7 +4685,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4685 | let search_tile = |index: usize, | 4685 | let search_tile = |index: usize, |
| 4686 | fctx: &crate::decode::FrameCtx| | 4686 | fctx: &crate::decode::FrameCtx| |
| 4687 | -> Result<Vec<(usize, Superblock, Vec<u8>)>> { | 4687 | -> Result<Vec<(usize, Superblock, Vec<u8>)>> { |
| 4688 | - crate::tile::arm_pricing_cdfs(None); | 4688 | + crate::tile::arm_pricing_cdfs(None, false); |
| 4689 | let rect = layout.rect(index); | 4689 | let rect = layout.rect(index); |
| 4690 | 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); |
| 4691 | let mut chroma = [ | 4691 | let mut chroma = [ |
| @@ -4963,7 +4963,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4963 | let mut cdfs = start_cdfs.0.clone(); | 4963 | let mut cdfs = start_cdfs.0.clone(); |
| 4964 | crate::tile::arm_screen(screen); | 4964 | crate::tile::arm_screen(screen); |
| 4965 | crate::tile::arm_intrabc(allow_intrabc); | 4965 | crate::tile::arm_intrabc(allow_intrabc); |
| 4966 | - crate::tile::arm_pricing_cdfs(None); | 4966 | + crate::tile::arm_pricing_cdfs(None, false); |
| 4967 | let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs( | 4967 | let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs( |
| 4968 | mi_cols, | 4968 | mi_cols, |
| 4969 | mi_rows, | 4969 | mi_rows, |
| @@ -6403,9 +6403,6 @@ pub(crate) fn encode_inter_frame( | |||
| 6403 | // 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 |
| 6404 | // never reached the tool). | 6404 | // never reached the tool). |
| 6405 | 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); | ||
| 6409 | if let Some(p) = pyramid { | 6406 | if let Some(p) = pyramid { |
| 6410 | header.show_frame = p.show_frame; | 6407 | header.show_frame = p.show_frame; |
| 6411 | header.showable_frame = !p.show_frame; | 6408 | header.showable_frame = !p.show_frame; |
| @@ -6434,6 +6431,11 @@ pub(crate) fn encode_inter_frame( | |||
| 6434 | let screen = seq_screen() | 6431 | let screen = seq_screen() |
| 6435 | && screen_content(&picture_y8, picture.width, true_width, true_height); | 6432 | && screen_content(&picture_y8, picture.width, true_width, true_height); |
| 6436 | header.allow_screen_content_tools = screen; | 6433 | header.allow_screen_content_tools = screen; |
| 6434 | + // The search prices coefficients against the tables this frame's writer | ||
| 6435 | + // starts from, not against the defaults (lane-av1txbits) -- but only on a | ||
| 6436 | + // frame the screen detector said no to (lane-av1price2): armed HERE, after | ||
| 6437 | + // `screen` exists, and again on every search/tile worker below. | ||
| 6438 | + crate::tile::arm_pricing_cdfs(start_cdfs, screen); | ||
| 6437 | let mut luma = Plane { | 6439 | let mut luma = Plane { |
| 6438 | source: &picture_y8, | 6440 | source: &picture_y8, |
| 6439 | reconstruction: vec![128; picture_y8.len()], | 6441 | reconstruction: vec![128; picture_y8.len()], |
| @@ -6526,7 +6528,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6526 | // search step) is read by the SEARCH, so every worker arms it or the | 6528 | // search step) is read by the SEARCH, so every worker arms it or the |
| 6527 | // tile's bits depend on the thread count. | 6529 | // tile's bits depend on the thread count. |
| 6528 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | 6530 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); |
| 6529 | - crate::tile::arm_pricing_cdfs(start_cdfs); | 6531 | + crate::tile::arm_pricing_cdfs(start_cdfs, screen); |
| 6530 | let rect = layout.rect(index); | 6532 | let rect = layout.rect(index); |
| 6531 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); | 6533 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); |
| 6532 | let mut chroma = [ | 6534 | let mut chroma = [ |
| @@ -6997,7 +6999,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6997 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); | 6999 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); |
| 6998 | crate::tile::arm_screen(screen); | 7000 | crate::tile::arm_screen(screen); |
| 6999 | let mut cdfs = start_cdfs.0.clone(); | 7001 | let mut cdfs = start_cdfs.0.clone(); |
| 7000 | - crate::tile::arm_pricing_cdfs(Some(&cdfs)); | 7002 | + crate::tile::arm_pricing_cdfs(Some(&cdfs), screen); |
| 7001 | // Each tile's own MV grid: a candidate scan never reaches past | 7003 | // Each tile's own MV grid: a candidate scan never reaches past |
| 7002 | // the tile's bounds, so a grid holding only this tile's own | 7004 | // the tile's bounds, so a grid holding only this tile's own |
| 7003 | // blocks reads exactly as the decoder's frame-wide one does | 7005 | // blocks reads exactly as the decoder's frame-wide one does |
| @@ -9541,6 +9543,80 @@ mod tests { | |||
| 9541 | assert!(written_all > 0.0); | 9543 | assert!(written_all > 0.0); |
| 9542 | } | 9544 | } |
| 9543 | 9545 | ||
| 9546 | + /// Step (2) of the pricer lane: the same census, but on the REAL clips | ||
| 9547 | + /// the BD gate scores -- one film clip and the screen capture -- so the | ||
| 9548 | + /// question the film/screen split raises can be answered from data. Real | ||
| 9549 | + /// tables (`EC_AV1_PRICE_FRAME_CDFS=1`) make both film clips smaller and | ||
| 9550 | + /// the screen capture larger, and the census says why: on a screen | ||
| 9551 | + /// capture the frame's own starting tables are the tables of a frame that | ||
| 9552 | + /// coded PALETTE and long runs of zero blocks, so they are far from the | ||
| 9553 | + /// tables the writer ends the frame with; the pricer error does not fall | ||
| 9554 | + /// the way it does on film. | ||
| 9555 | + /// | ||
| 9556 | + /// Not a gate -- it prints. Run it once per mode: | ||
| 9557 | + /// EC_AV1_PRICE_FRAME_CDFS=0 cargo test -p ec-av1 --release --lib -- \ | ||
| 9558 | + /// --ignored pricer_error_census_on_clips --nocapture | ||
| 9559 | + #[test] | ||
| 9560 | + #[ignore = "reads the real library, needs ffmpeg"] | ||
| 9561 | + fn pricer_error_census_on_clips() { | ||
| 9562 | + if !have_ffmpeg() { | ||
| 9563 | + eprintln!("SKIP pricer_error_census_on_clips: no ffmpeg"); | ||
| 9564 | + return; | ||
| 9565 | + } | ||
| 9566 | + let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures"); | ||
| 9567 | + let mut clips: Vec<(String, std::path::PathBuf)> = Vec::new(); | ||
| 9568 | + let film = fixtures.join("video/h264-1080p-23.976-8bit.mp4"); | ||
| 9569 | + if film.exists() { | ||
| 9570 | + clips.push(("film 1080p".to_string(), film)); | ||
| 9571 | + } | ||
| 9572 | + if let Ok(manifest) = std::fs::read_to_string(fixtures.join("real-library-manifest.tsv")) | ||
| 9573 | + && let Some(p) = manifest | ||
| 9574 | + .lines() | ||
| 9575 | + .skip(1) | ||
| 9576 | + .filter_map(|l| l.split('\t').next()) | ||
| 9577 | + .find(|p| p.contains("/OBS/") && p.ends_with(".mkv") && std::path::Path::new(p).exists()) | ||
| 9578 | + { | ||
| 9579 | + clips.push(("screen".to_string(), std::path::PathBuf::from(p))); | ||
| 9580 | + } | ||
| 9581 | + assert!(!clips.is_empty(), "no source clip available"); | ||
| 9582 | + let (width, height, frames) = (640usize, 384usize, 6usize); | ||
| 9583 | + for (name, path) in &clips { | ||
| 9584 | + let fctx = &crate::decode::FrameCtx::new(); | ||
| 9585 | + let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); | ||
| 9586 | + SCREEN_FRAMES.iter().for_each(|c| { | ||
| 9587 | + c.store(0, std::sync::atomic::Ordering::Relaxed); | ||
| 9588 | + }); | ||
| 9589 | + let _ = crate::tile::take_pricing_hits(); | ||
| 9590 | + crate::tile::pricer_census_on(); | ||
| 9591 | + let _ = encode_sequence_with_ctx(&source, 100, 0.5, fctx).unwrap(); | ||
| 9592 | + let rows = crate::tile::take_pricer_census(); | ||
| 9593 | + let (price_default, price_real) = crate::tile::take_pricing_hits(); | ||
| 9594 | + let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); | ||
| 9595 | + println!( | ||
| 9596 | + "\n## {name} ({frames} frames at {width}x{height}, q=100): screen frames \ | ||
| 9597 | + {screen_on}, pricer armings default={price_default} real={price_real}" | ||
| 9598 | + ); | ||
| 9599 | + println!("| set | nz | blocks | priced bits | written bits | error |"); | ||
| 9600 | + println!("|---|---|---|---|---|---|"); | ||
| 9601 | + let (mut priced_all, mut written_all) = (0.0, 0.0); | ||
| 9602 | + for ((set, bucket), (blocks, priced, written)) in &rows { | ||
| 9603 | + priced_all += priced; | ||
| 9604 | + written_all += written; | ||
| 9605 | + println!( | ||
| 9606 | + "| {set} | {} | {blocks} | {priced:.0} | {written:.0} | {:+.1}% |", | ||
| 9607 | + crate::tile::census_bucket_label(*bucket), | ||
| 9608 | + if *written > 0.0 { (priced - written) / written * 100.0 } else { 0.0 } | ||
| 9609 | + ); | ||
| 9610 | + } | ||
| 9611 | + println!( | ||
| 9612 | + "| ALL | | {} | {priced_all:.0} | {written_all:.0} | {:+.1}% |", | ||
| 9613 | + rows.values().map(|r| r.0).sum::<u64>(), | ||
| 9614 | + (priced_all - written_all) / written_all * 100.0 | ||
| 9615 | + ); | ||
| 9616 | + assert!(written_all > 0.0, "{name}: the census saw no transform block"); | ||
| 9617 | + } | ||
| 9618 | + } | ||
| 9619 | + | ||
| 9544 | /// Low motion has to actually buy something: at least one inter frame of | 9620 | /// Low motion has to actually buy something: at least one inter frame of |
| 9545 | /// a panning sequence has to cost fewer bytes than the key frame that | 9621 | /// a panning sequence has to cost fewer bytes than the key frame that |
| 9546 | /// starts it, and the inter-block share it prints has to be non-zero -- | 9622 | /// starts it, and the inter-block share it prints has to be non-zero -- |
| @@ -10862,6 +10938,12 @@ mod tests { | |||
| 10862 | // and of what size. A film clip must print `screen frames on=0`. | 10938 | // and of what size. A film clip must print `screen frames on=0`. |
| 10863 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); | 10939 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); |
| 10864 | let screen_off = SCREEN_FRAMES[0].load(std::sync::atomic::Ordering::Relaxed); | 10940 | let screen_off = SCREEN_FRAMES[0].load(std::sync::atomic::Ordering::Relaxed); |
| 10941 | + // Which tables the SEARCH priced against, per frame arming | ||
| 10942 | + // (`tile::arm_pricing_cdfs`): the default tables or the frame's | ||
| 10943 | + // real starting ones. A film clip must show every inter arming on | ||
| 10944 | + // the real side, a screen clip none of them. | ||
| 10945 | + let (price_default, price_real) = crate::tile::take_pricing_hits(); | ||
| 10946 | + eprintln!("{name}: pricer armings default={price_default} real={price_real}"); | ||
| 10865 | let palette = crate::tile::take_palette_hits(); | 10947 | let palette = crate::tile::take_palette_hits(); |
| 10866 | eprintln!( | 10948 | eprintln!( |
| 10867 | "{name}: screen frames on={screen_on} off={screen_off}; palette blocks {} sizes {}", | 10949 | "{name}: screen frames on={screen_on} off={screen_off}; palette blocks {} sizes {}", |
| @@ -11132,6 +11214,12 @@ mod tests { | |||
| 11132 | // native resolution, over this clip's four encodes. | 11214 | // native resolution, over this clip's four encodes. |
| 11133 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); | 11215 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); |
| 11134 | let screen_off = SCREEN_FRAMES[0].load(std::sync::atomic::Ordering::Relaxed); | 11216 | let screen_off = SCREEN_FRAMES[0].load(std::sync::atomic::Ordering::Relaxed); |
| 11217 | + // Which tables the SEARCH priced against, per frame arming | ||
| 11218 | + // (`tile::arm_pricing_cdfs`): the default tables or the frame's | ||
| 11219 | + // real starting ones. A film clip must show every inter arming on | ||
| 11220 | + // the real side, a screen clip none of them. | ||
| 11221 | + let (price_default, price_real) = crate::tile::take_pricing_hits(); | ||
| 11222 | + eprintln!("{name}: pricer armings default={price_default} real={price_real}"); | ||
| 11135 | let palette = crate::tile::take_palette_hits(); | 11223 | let palette = crate::tile::take_palette_hits(); |
| 11136 | let palette_uv = crate::tile::take_palette_uv_hits(); | 11224 | let palette_uv = crate::tile::take_palette_uv_hits(); |
| 11137 | let ibc = crate::tile::take_intrabc_hits(); | 11225 | let ibc = crate::tile::take_intrabc_hits(); |
crates/ec-av1/src/tile.rsmodified+31 -3
| @@ -13,6 +13,7 @@ use std::cell::RefCell; | |||
| 13 | #[cfg(test)] | 13 | #[cfg(test)] |
| 14 | use std::collections::BTreeMap; | 14 | use std::collections::BTreeMap; |
| 15 | use std::sync::LazyLock; | 15 | use std::sync::LazyLock; |
| 16 | +use std::sync::atomic::{AtomicUsize, Ordering}; | ||
| 16 | 17 | ||
| 17 | use ec_core::{Error, Result}; | 18 | use ec_core::{Error, Result}; |
| 18 | 19 | ||
| @@ -4059,14 +4060,41 @@ thread_local! { | |||
| 4059 | /// +1.5/+19.8/-10.0 (both film clips down against both references, screen up | 4060 | /// +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 | /// 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 | /// rule, so it ships as a knob rather than as the default. |
| 4062 | -pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { | 4063 | +pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) { |
| 4063 | - static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | 4064 | + static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new(); |
| 4064 | - let on = *ON.get_or_init(|| std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() == Ok("1")); | 4065 | + let mode = *MODE.get_or_init(|| match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() { |
| 4066 | + Ok("1") => 1, | ||
| 4067 | + Ok("2") => 2, | ||
| 4068 | + _ => 0, | ||
| 4069 | + }); | ||
| 4070 | + let on = match mode { | ||
| 4071 | + 0 => false, | ||
| 4072 | + 1 => true, | ||
| 4073 | + _ => !screen, | ||
| 4074 | + }; | ||
| 4075 | + PRICING_HITS[usize::from(on && base.is_some())].fetch_add(1, Ordering::Relaxed); | ||
| 4065 | PRICING_BASE.with_borrow_mut(|slot| { | 4076 | PRICING_BASE.with_borrow_mut(|slot| { |
| 4066 | *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone()))); | 4077 | *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone()))); |
| 4067 | }); | 4078 | }); |
| 4068 | } | 4079 | } |
| 4069 | 4080 | ||
| 4081 | +/// How many times a frame's search was armed with the DEFAULT tables and how | ||
| 4082 | +/// many with the frame's REAL starting tables ([`arm_pricing_cdfs`]), so a | ||
| 4083 | +/// gate can print which side of the screen gate every frame landed on. Counts | ||
| 4084 | +/// arming calls, not frames: a multi-tile frame arms one worker per tile. | ||
| 4085 | +pub(crate) static PRICING_HITS: [AtomicUsize; 2] = | ||
| 4086 | + [AtomicUsize::new(0), AtomicUsize::new(0)]; | ||
| 4087 | + | ||
| 4088 | +/// Reads and clears [`PRICING_HITS`]: (default-table armings, real-table | ||
| 4089 | +/// armings). | ||
| 4090 | +#[cfg(test)] | ||
| 4091 | +pub(crate) fn take_pricing_hits() -> (usize, usize) { | ||
| 4092 | + ( | ||
| 4093 | + PRICING_HITS[0].swap(0, Ordering::Relaxed), | ||
| 4094 | + PRICING_HITS[1].swap(0, Ordering::Relaxed), | ||
| 4095 | + ) | ||
| 4096 | +} | ||
| 4097 | + | ||
| 4070 | /// The pricer census (`pricer_census_on`): for every transform block the tile | 4098 | /// 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`] | 4099 | /// 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 | 4100 | /// prices those very levels at -- the search's rate term measured against the |