A 64x64 root may be taken at a superblock the frame edge cuts
Browse files at this commitcb5978f451dc162928b17d8c4e55c1dda9aaeb24Parent: e1f6d28- feat
- a 64x64 root may be taken at a superblock the frame edge cuts
2 changed files +159 -12
- crates/ec-av1/src/encode.rs +104 -12
- crates/ec-av1/src/encoder.rs +55 -0
crates/ec-av1/src/encode.rsmodified+104 -12
| @@ -517,6 +517,32 @@ pub fn take_b64_var_tx_hits() -> usize { | |||
| 517 | B64_VAR_TX_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | 517 | B64_VAR_TX_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) |
| 518 | } | 518 | } |
| 519 | 519 | ||
| 520 | +/// Whether a superblock the true frame edge CUTS THROUGH may be left whole | ||
| 521 | +/// (lane-b64b). AV1 asks only that the block's own half is inside | ||
| 522 | +/// (`has_cols`/`has_rows`, spec `decode_partition`), and the writer already | ||
| 523 | +/// codes that case; what was missing was a trial whose SSE is scored over the | ||
| 524 | +/// INSIDE part alone ([`inside_sse`]) instead of over the padding the encoder | ||
| 525 | +/// invented past the edge. Both gate films are superblock-aligned, so this | ||
| 526 | +/// only reaches the user's own arbitrary export sizes. | ||
| 527 | +/// | ||
| 528 | +/// Off by `EC_AV1_B64EDGE=0`. | ||
| 529 | +fn b64_edge() -> bool { | ||
| 530 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 531 | + crate::envflags::var("EC_AV1_B64EDGE").ok().map(|v| v != "0") | ||
| 532 | + }); | ||
| 533 | + ENV.unwrap_or(true) && b64_root() | ||
| 534 | +} | ||
| 535 | + | ||
| 536 | +/// How many 64x64 roots the frame edge cut through since the last | ||
| 537 | +/// [`take_b64_edge_hits`] (class `gate-blind-to-feature`: both gate films are | ||
| 538 | +/// superblock-aligned, so nothing but a synthetic size fires this). | ||
| 539 | +static B64_EDGE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 540 | + | ||
| 541 | +/// The edge 64x64-root count since the last call, and zero it. | ||
| 542 | +pub fn take_b64_edge_hits() -> usize { | ||
| 543 | + B64_EDGE_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 544 | +} | ||
| 545 | + | ||
| 520 | /// Whether the encoder searches loop restoration at all (spec 7.17): one | 546 | /// Whether the encoder searches loop restoration at all (spec 7.17): one |
| 521 | /// Wiener filter per 64x64 luma restoration unit, picked from | 547 | /// Wiener filter per 64x64 luma restoration unit, picked from |
| 522 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a | 548 | /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a |
| @@ -5982,6 +6008,46 @@ where | |||
| 5982 | /// of the frame's reconstruction because a tile's prediction never reads a | 6008 | /// of the frame's reconstruction because a tile's prediction never reads a |
| 5983 | /// sample outside its own rectangle, and every sample inside it is written | 6009 | /// sample outside its own rectangle, and every sample inside it is written |
| 5984 | /// before it is read. | 6010 | /// before it is read. |
| 6011 | +/// [`Plane::block_sse`] over the part of a block that is INSIDE the true | ||
| 6012 | +/// frame. A block the edge cuts through is still coded whole -- the decoder | ||
| 6013 | +/// reconstructs its outside samples too and the display crop throws them away | ||
| 6014 | +/// -- so scoring them would rank a 64x64 root by how well it predicts padding | ||
| 6015 | +/// the source does not have (lane-b64b). | ||
| 6016 | +fn inside_sse(plane: &Plane, x: usize, y: usize, side: usize, reconstruction: &[u8]) -> f64 { | ||
| 6017 | + let rows = side.min(plane.true_height.saturating_sub(y)); | ||
| 6018 | + let cols = side.min(plane.true_width.saturating_sub(x)); | ||
| 6019 | + let mut sse = 0u64; | ||
| 6020 | + for row in 0..rows { | ||
| 6021 | + let source = &plane.source[(y + row) * plane.width + x..][..cols]; | ||
| 6022 | + for (col, &s) in source.iter().enumerate() { | ||
| 6023 | + let d = u32::from(s.abs_diff(reconstruction[row * side + col])); | ||
| 6024 | + sse += u64::from(d * d); | ||
| 6025 | + } | ||
| 6026 | + } | ||
| 6027 | + sse as f64 | ||
| 6028 | +} | ||
| 6029 | + | ||
| 6030 | +/// Rescores one 64x64 root trial set (luma and the two chroma planes) over | ||
| 6031 | +/// the inside part of a superblock the frame edge cuts through. | ||
| 6032 | +fn clip_sb64_sse( | ||
| 6033 | + trials: &mut [Trial; 3], | ||
| 6034 | + luma: &Plane, | ||
| 6035 | + chroma: &[Plane; 2], | ||
| 6036 | + (x, y): (usize, usize), | ||
| 6037 | + side: usize, | ||
| 6038 | +) { | ||
| 6039 | + trials[0].sse = inside_sse(luma, x, y, side, &trials[0].reconstruction); | ||
| 6040 | + for plane in 0..2 { | ||
| 6041 | + trials[plane + 1].sse = inside_sse( | ||
| 6042 | + &chroma[plane], | ||
| 6043 | + x / 2, | ||
| 6044 | + y / 2, | ||
| 6045 | + side / 2, | ||
| 6046 | + &trials[plane + 1].reconstruction, | ||
| 6047 | + ); | ||
| 6048 | + } | ||
| 6049 | +} | ||
| 6050 | + | ||
| 5985 | fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { | 6051 | fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { |
| 5986 | Plane { | 6052 | Plane { |
| 5987 | source, | 6053 | source, |
| @@ -7503,9 +7569,12 @@ fn search_skip_64( | |||
| 7503 | } | 7569 | } |
| 7504 | } | 7570 | } |
| 7505 | 7571 | ||
| 7572 | + // A superblock the true frame edge cuts through: every trial below is | ||
| 7573 | + // ranked over its inside part only ([`clip_sb64_sse`]). | ||
| 7574 | + let edge = x + side > luma.true_width || y + side > luma.true_height; | ||
| 7506 | let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; | 7575 | let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; |
| 7507 | for (bits, info) in cands { | 7576 | for (bits, info) in cands { |
| 7508 | - let trials = [ | 7577 | + let mut trials = [ |
| 7509 | (0usize, x, y, side, true), | 7578 | (0usize, x, y, side, true), |
| 7510 | (1, x / 2, y / 2, side / 2, false), | 7579 | (1, x / 2, y / 2, side / 2, false), |
| 7511 | (2, x / 2, y / 2, side / 2, false), | 7580 | (2, x / 2, y / 2, side / 2, false), |
| @@ -7542,6 +7611,9 @@ fn search_skip_64( | |||
| 7542 | TxbSet::Luma32Inter, | 7611 | TxbSet::Luma32Inter, |
| 7543 | ) | 7612 | ) |
| 7544 | }); | 7613 | }); |
| 7614 | + if edge { | ||
| 7615 | + clip_sb64_sse(&mut trials, luma, chroma, (x, y), side); | ||
| 7616 | + } | ||
| 7545 | let cost = trials[0].sse | 7617 | let cost = trials[0].sse |
| 7546 | + trials[1].sse | 7618 | + trials[1].sse |
| 7547 | + trials[2].sse | 7619 | + trials[2].sse |
| @@ -7614,7 +7686,7 @@ fn search_skip_64( | |||
| 7614 | ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | 7686 | ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); |
| 7615 | ccands.dedup_by_key(|c| c.0); | 7687 | ccands.dedup_by_key(|c| c.0); |
| 7616 | for (mvs, bits, info) in ccands { | 7688 | for (mvs, bits, info) in ccands { |
| 7617 | - let trials = [ | 7689 | + let mut trials = [ |
| 7618 | (0usize, x, y, side, true), | 7690 | (0usize, x, y, side, true), |
| 7619 | (1, x / 2, y / 2, side / 2, false), | 7691 | (1, x / 2, y / 2, side / 2, false), |
| 7620 | (2, x / 2, y / 2, side / 2, false), | 7692 | (2, x / 2, y / 2, side / 2, false), |
| @@ -7658,6 +7730,9 @@ fn search_skip_64( | |||
| 7658 | TxbSet::Luma32Inter, | 7730 | TxbSet::Luma32Inter, |
| 7659 | ) | 7731 | ) |
| 7660 | }); | 7732 | }); |
| 7733 | + if edge { | ||
| 7734 | + clip_sb64_sse(&mut trials, luma, chroma, (x, y), side); | ||
| 7735 | + } | ||
| 7661 | let syntax = fixed - ref_bits + bits; | 7736 | let syntax = fixed - ref_bits + bits; |
| 7662 | let cost = | 7737 | let cost = |
| 7663 | trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax; | 7738 | trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax; |
| @@ -7690,7 +7765,7 @@ fn search_skip_64( | |||
| 7690 | let mut luma_levels: Option<(Vec<i32>, u8)> = None; | 7765 | let mut luma_levels: Option<(Vec<i32>, u8)> = None; |
| 7691 | if residual { | 7766 | if residual { |
| 7692 | let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; | 7767 | let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; |
| 7693 | - let residual_trials = [0usize, 1, 2].map(|plane| { | 7768 | + let mut residual_trials = [0usize, 1, 2].map(|plane| { |
| 7694 | let (target, px, py, s) = if plane == 0 { | 7769 | let (target, px, py, s) = if plane == 0 { |
| 7695 | (&*luma, x, y, side) | 7770 | (&*luma, x, y, side) |
| 7696 | } else { | 7771 | } else { |
| @@ -7707,6 +7782,9 @@ fn search_skip_64( | |||
| 7707 | sets[plane], | 7782 | sets[plane], |
| 7708 | ) | 7783 | ) |
| 7709 | }); | 7784 | }); |
| 7785 | + if edge { | ||
| 7786 | + clip_sb64_sse(&mut residual_trials, luma, chroma, (x, y), side); | ||
| 7787 | + } | ||
| 7710 | let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); | 7788 | let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); |
| 7711 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); | 7789 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); |
| 7712 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the | 7790 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the |
| @@ -9425,19 +9503,30 @@ pub(crate) fn encode_inter_frame( | |||
| 9425 | // lane-b64: the whole superblock as ONE 64x64 block, tried before | 9503 | // lane-b64: the whole superblock as ONE 64x64 block, tried before |
| 9426 | // its quadrants are (`search_skip_64`) and compared against the | 9504 | // its quadrants are (`search_skip_64`) and compared against the |
| 9427 | // four of them below. Only a superblock wholly inside the true | 9505 | // four of them below. Only a superblock wholly inside the true |
| 9428 | - // frame is offered it: a 64x64 root whose right or bottom part | 9506 | + // frame is offered it, and (lane-b64b, `EC_AV1_B64EDGE`) one the |
| 9429 | - // hangs over the edge is legal AV1 (`has_cols`/`has_rows` only | 9507 | + // edge CUTS THROUGH: the spec's own `has_cols`/`has_rows` ask |
| 9430 | - // ask for half), but its trial would score prediction the frame | 9508 | + // only that the block's half is inside, and the trial is ranked |
| 9431 | - // does not have -- corner-cut, ceiling = the edge superblocks of | 9509 | + // over the inside part alone ([`clip_sb64_sse`]). |
| 9432 | - // a frame whose size is not a multiple of 64, which stay split. | ||
| 9433 | let (x64, y64) = (sb_c * SUPERBLOCK, sb_r * SUPERBLOCK); | 9510 | let (x64, y64) = (sb_c * SUPERBLOCK, sb_r * SUPERBLOCK); |
| 9434 | - let sb64_legal = b64_root() | 9511 | + let whole_inside = (sb_c as u32 + 1) * crate::tile::SB_MI <= header.mi_cols |
| 9435 | - && sb_r * 2 + 1 < rows | ||
| 9436 | - && sb_c * 2 + 1 < cols | ||
| 9437 | - && (sb_c as u32 + 1) * crate::tile::SB_MI <= header.mi_cols | ||
| 9438 | && (sb_r as u32 + 1) * crate::tile::SB_MI <= header.mi_rows | 9512 | && (sb_r as u32 + 1) * crate::tile::SB_MI <= header.mi_rows |
| 9439 | && x64 + SUPERBLOCK <= luma.true_width | 9513 | && x64 + SUPERBLOCK <= luma.true_width |
| 9440 | && y64 + SUPERBLOCK <= luma.true_height; | 9514 | && y64 + SUPERBLOCK <= luma.true_height; |
| 9515 | + let edge_inside = b64_edge() | ||
| 9516 | + && crate::tile::has_half( | ||
| 9517 | + sb_c as u32 * crate::tile::SB_MI, | ||
| 9518 | + crate::tile::SB_MI, | ||
| 9519 | + header.mi_cols, | ||
| 9520 | + ) | ||
| 9521 | + && crate::tile::has_half( | ||
| 9522 | + sb_r as u32 * crate::tile::SB_MI, | ||
| 9523 | + crate::tile::SB_MI, | ||
| 9524 | + header.mi_rows, | ||
| 9525 | + ); | ||
| 9526 | + let sb64_legal = b64_root() | ||
| 9527 | + && sb_r * 2 + 1 < rows | ||
| 9528 | + && sb_c * 2 + 1 < cols | ||
| 9529 | + && (whole_inside || edge_inside); | ||
| 9441 | // One lambda for the whole superblock, the same mean the 32x32 | 9530 | // One lambda for the whole superblock, the same mean the 32x32 |
| 9442 | // level takes over its own four tpl cells: the 64-vs-four-32 | 9531 | // level takes over its own four tpl cells: the 64-vs-four-32 |
| 9443 | // comparison below weighs one cost against four, so both sides | 9532 | // comparison below weighs one cost against four, so both sides |
| @@ -9953,6 +10042,9 @@ pub(crate) fn encode_inter_frame( | |||
| 9953 | if block.inter.is_some_and(|i| i.ref1.is_some()) { | 10042 | if block.inter.is_some_and(|i| i.ref1.is_some()) { |
| 9954 | B64_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 10043 | B64_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 9955 | } | 10044 | } |
| 10045 | + if !whole_inside { | ||
| 10046 | + B64_EDGE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 10047 | + } | ||
| 9956 | blocks.truncate(mark); | 10048 | blocks.truncate(mark); |
| 9957 | restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &after); | 10049 | restore(&mut luma, &mut chroma, (x64, y64), SUPERBLOCK, &after); |
| 9958 | // Overwrites every 16x16 mi cell the quadrant searches | 10050 | // Overwrites every 16x16 mi cell the quadrant searches |
crates/ec-av1/src/encoder.rsmodified+55 -0
| @@ -2846,6 +2846,61 @@ mod tests { | |||
| 2846 | } | 2846 | } |
| 2847 | } | 2847 | } |
| 2848 | 2848 | ||
| 2849 | + /// A frame whose size is NOT a multiple of a superblock (lane-b64b): | ||
| 2850 | + /// 232x168 leaves the last superblock column and row cut by the true | ||
| 2851 | + /// frame edge, with more than half of each inside -- which is exactly | ||
| 2852 | + /// what AV1's `has_cols`/`has_rows` allow to stay whole. Both gate films | ||
| 2853 | + /// are superblock-aligned, so nothing but a synthetic size reaches this | ||
| 2854 | + /// path at all (class `gate-blind-to-feature`), and the user's own | ||
| 2855 | + /// exports are arbitrary sizes. | ||
| 2856 | + #[test] | ||
| 2857 | + fn a_non_superblock_aligned_clip_codes_edge_64x64_roots_and_decodes_sample_exact() { | ||
| 2858 | + let _knobs = crate::speed::knob_write(); | ||
| 2859 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 2860 | + let (width, height) = (232usize, 168usize); | ||
| 2861 | + let still = test_card(width, height, 0); | ||
| 2862 | + let sources: Vec<Picture> = (0..3).map(|_| still.clone()).collect(); | ||
| 2863 | + let config = EncoderConfig { | ||
| 2864 | + width, | ||
| 2865 | + height, | ||
| 2866 | + base_q_idx: 120, | ||
| 2867 | + gop: 8, | ||
| 2868 | + colour: Colour::Bt709Limited, | ||
| 2869 | + tile_cols_log2: 0, | ||
| 2870 | + tile_rows_log2: 0, | ||
| 2871 | + }; | ||
| 2872 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 2873 | + let mut stream = Vec::new(); | ||
| 2874 | + let _ = crate::encode::take_b64_edge_hits(); | ||
| 2875 | + for packet in encode_all(&mut enc, &sources) { | ||
| 2876 | + stream.extend_from_slice(&packet.data); | ||
| 2877 | + } | ||
| 2878 | + let edges = crate::encode::take_b64_edge_hits(); | ||
| 2879 | + assert!(edges > 0, "no 64x64 root was taken at a superblock the frame edge cuts"); | ||
| 2880 | + eprintln!("edge 64x64 roots: {edges}"); | ||
| 2881 | + | ||
| 2882 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 2883 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | ||
| 2884 | + if !have_ffmpeg() { | ||
| 2885 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 2886 | + return; | ||
| 2887 | + } | ||
| 2888 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 2889 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | ||
| 2890 | + for (i, (ours_i, theirs_i)) in ours.iter().zip(&theirs).enumerate() { | ||
| 2891 | + let got: Vec<u8> = ours_i.y.iter().map(|&v| v as u8).collect(); | ||
| 2892 | + if let Some(at) = got.iter().zip(theirs_i).position(|(x, y)| x != y) { | ||
| 2893 | + panic!( | ||
| 2894 | + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 2895 | + at % width, | ||
| 2896 | + at / width, | ||
| 2897 | + got[at], | ||
| 2898 | + theirs_i[at], | ||
| 2899 | + ); | ||
| 2900 | + } | ||
| 2901 | + } | ||
| 2902 | + } | ||
| 2903 | + | ||
| 2849 | /// A clip whose inter residual is HIGH-FREQUENCY (lane-b64b): every | 2904 | /// A clip whose inter residual is HIGH-FREQUENCY (lane-b64b): every |
| 2850 | /// frame adds a 4-pixel checkerboard, so the 64x64 root's prediction is | 2905 | /// frame adds a 4-pixel checkerboard, so the 64x64 root's prediction is |
| 2851 | /// off by exactly the detail a TX_64X64 throws away (it codes only the | 2906 | /// off by exactly the detail a TX_64X64 throws away (it codes only the |