Merge branch 'main' into lane-av1comp4
Browse files at this commit4 changed files +912 -13
- crates/ec-av1/src/decode.rs +10 -6
- crates/ec-av1/src/encode.rs +621 -5
- crates/ec-av1/src/stream.rs +2 -1
- crates/ec-av1/src/tile.rs +279 -1
crates/ec-av1/src/decode.rsmodified+10 -6
| @@ -37549,8 +37549,8 @@ mod tests { | ||
| 37549 | 37549 | &encoded.loop_filter, |
| 37550 | 37550 | encoded.tx_select, |
| 37551 | 37551 | true, |
| 37552 | - false, | |
| 37553 | - false, | |
| 37552 | + encoded.screen, | |
| 37553 | + encoded.allow_intrabc, | |
| 37554 | 37554 | &encoded.loop_restoration, fctx, |
| 37555 | 37555 | ) |
| 37556 | 37556 | .unwrap(); |
| @@ -37593,8 +37593,8 @@ mod tests { | ||
| 37593 | 37593 | &encoded.loop_filter, |
| 37594 | 37594 | tx_select, |
| 37595 | 37595 | true, |
| 37596 | - false, | |
| 37597 | - false, | |
| 37596 | + encoded.screen, | |
| 37597 | + encoded.allow_intrabc, | |
| 37598 | 37598 | &encoded.loop_restoration, |
| 37599 | 37599 | fctx, |
| 37600 | 37600 | ) |
| @@ -37681,7 +37681,9 @@ mod tests { | ||
| 37681 | 37681 | // This key frame's own `allow_screen_content_tools`: its intra |
| 37682 | 37682 | // blocks carry the palette syntax under it (stale-header class). |
| 37683 | 37683 | key.screen, |
| 37684 | - false, | |
| 37684 | + // ... and its own `allow_intrabc`: every intra block carries a | |
| 37685 | + // `use_intrabc` symbol under it (stale-header class). | |
| 37686 | + key.allow_intrabc, | |
| 37685 | 37687 | &key.loop_restoration, fctx, |
| 37686 | 37688 | ) |
| 37687 | 37689 | .unwrap(); |
| @@ -38021,7 +38023,9 @@ mod tests { | ||
| 38021 | 38023 | // This key frame's own `allow_screen_content_tools`: its intra |
| 38022 | 38024 | // blocks carry the palette syntax under it (stale-header class). |
| 38023 | 38025 | key.screen, |
| 38024 | - false, | |
| 38026 | + // ... and its own `allow_intrabc`: every intra block carries a | |
| 38027 | + // `use_intrabc` symbol under it (stale-header class). | |
| 38028 | + key.allow_intrabc, | |
| 38025 | 38029 | &key.loop_restoration, fctx, |
| 38026 | 38030 | ) |
| 38027 | 38031 | .unwrap(); |
crates/ec-av1/src/encode.rsmodified+621 -5
| @@ -681,6 +681,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En | ||
| 681 | 681 | next_cdfs: encoded.next_cdfs.clone(), |
| 682 | 682 | loop_filter: encoded.loop_filter, |
| 683 | 683 | loop_restoration: encoded.loop_restoration, |
| 684 | + allow_intrabc: encoded.allow_intrabc, | |
| 684 | 685 | cdef: encoded.cdef, |
| 685 | 686 | } |
| 686 | 687 | } |
| @@ -760,6 +761,24 @@ pub struct Encoded { | ||
| 760 | 761 | /// filters themselves are coded in `tile`, so a decode of it under a |
| 761 | 762 | /// stale `RESTORE_NONE` header desyncs at the first superblock. |
| 762 | 763 | pub(crate) loop_restoration: LoopRestorationParams, |
| 764 | + /// This frame header's `allow_intrabc` -- every intra block of the tile | |
| 765 | + /// carries a `use_intrabc` symbol under it, so a raw tile decode that | |
| 766 | + /// guessed `false` would desync at the first block (same class as | |
| 767 | + /// `screen`/`tx_select`). | |
| 768 | + pub(crate) allow_intrabc: bool, | |
| 769 | +} | |
| 770 | + | |
| 771 | +thread_local! { | |
| 772 | + /// The share of 16x16 source blocks [`intrabc_worth_it`] found a repeat | |
| 773 | + /// for on the last key frame encoded on this thread -- read by the gates, | |
| 774 | + /// which print it beside the block counts (class `gate-blind-to-feature`). | |
| 775 | + static IBC_SHARE: std::cell::Cell<f64> = const { std::cell::Cell::new(0.0) }; | |
| 776 | +} | |
| 777 | + | |
| 778 | +/// [`IBC_SHARE`] for the last key frame encoded on this thread. | |
| 779 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 780 | +pub(crate) fn intrabc_source_share() -> f64 { | |
| 781 | + IBC_SHARE.with(std::cell::Cell::get) | |
| 763 | 782 | } |
| 764 | 783 | |
| 765 | 784 | thread_local! { |
| @@ -2495,6 +2514,421 @@ fn palette_candidates(block: &[u8]) -> Vec<crate::tile::PaletteY> { | ||
| 2495 | 2514 | } |
| 2496 | 2515 | |
| 2497 | 2516 | #[allow(clippy::too_many_arguments)] |
| 2517 | +// --------------------------------------------------------------------------- | |
| 2518 | +// lane-av1ibc: intra block copy (spec 5.11.13 `use_intrabc`, 7.11.4 DV | |
| 2519 | +// validity). A key frame that detected screen content and finds enough exact | |
| 2520 | +// repeats in its own source sets `allow_intrabc`, which the spec forces every | |
| 2521 | +// in-loop filter OFF for (frame.rs:194/210/252/272) -- so the bit only earns | |
| 2522 | +// its place when the copied blocks pay back the deblock/CDEF/LR the frame | |
| 2523 | +// gives up. `EC_AV1_INTRABC=0` turns the whole arm off. | |
| 2524 | +// --------------------------------------------------------------------------- | |
| 2525 | + | |
| 2526 | +/// Whether intra block copy is offered at all. **Off by default** -- | |
| 2527 | +/// `EC_AV1_INTRABC=1` turns it on. The syntax is complete and proven against | |
| 2528 | +/// ffmpeg (`a_repeated_pattern_key_frame_codes_intrabc_blocks_ffmpeg_decodes_exactly`), | |
| 2529 | +/// but on the gate's real screen capture it MEASURED WORSE: setting | |
| 2530 | +/// `allow_intrabc` costs the key frame its deblocking, CDEF and loop | |
| 2531 | +/// restoration (spec 5.9.11/5.9.19/5.9.20) and the copied blocks do not pay | |
| 2532 | +/// that back -- screen BD +59.1/+5.1 against the +54.5/+1.1 base (lane-av1ibc, | |
| 2533 | +/// `probe_intrabc_key_frame` has the per-key-frame table). Also off on a frame | |
| 2534 | +/// that detected no screen content. | |
| 2535 | +fn intrabc_enabled() -> bool { | |
| 2536 | + if let Some(forced) = INTRABC_FORCE.with(std::cell::Cell::get) { | |
| 2537 | + return forced; | |
| 2538 | + } | |
| 2539 | + static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | |
| 2540 | + *ON.get_or_init(|| std::env::var("EC_AV1_INTRABC").ok().as_deref() == Some("1")) | |
| 2541 | +} | |
| 2542 | + | |
| 2543 | +thread_local! { | |
| 2544 | + /// Overrides [`intrabc_enabled`] for the calling thread -- the ablation | |
| 2545 | + /// probe runs both arms in one process, and the suite is one process, so | |
| 2546 | + /// an environment variable cannot separate them (same reason | |
| 2547 | + /// [`SCREEN_FORCE`] exists). | |
| 2548 | + static INTRABC_FORCE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) }; | |
| 2549 | +} | |
| 2550 | + | |
| 2551 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 2552 | +pub(crate) fn force_intrabc(value: Option<bool>) { | |
| 2553 | + INTRABC_FORCE.with(|c| c.set(value)); | |
| 2554 | +} | |
| 2555 | + | |
| 2556 | +/// How many of a screen frame's 16x16 blocks must have an exact repeat | |
| 2557 | +/// somewhere else in the source before `allow_intrabc` is worth the in-loop | |
| 2558 | +/// filters it costs, in percent (`EC_AV1_INTRABC_PCT`). Unswept-decision- | |
| 2559 | +/// constant class: `probe_intrabc_threshold` measures the arm at several | |
| 2560 | +/// values. | |
| 2561 | +fn intrabc_pct() -> usize { | |
| 2562 | + static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); | |
| 2563 | + *N.get_or_init(|| { | |
| 2564 | + std::env::var("EC_AV1_INTRABC_PCT") | |
| 2565 | + .ok() | |
| 2566 | + .and_then(|v| v.parse().ok()) | |
| 2567 | + .unwrap_or(10) | |
| 2568 | + }) | |
| 2569 | +} | |
| 2570 | + | |
| 2571 | +/// The pixel grid intrabc source positions are hashed on | |
| 2572 | +/// (`EC_AV1_INTRABC_STEP`): every DV this encoder writes is a multiple of it, | |
| 2573 | +/// which keeps the block-hash table (and so the search) linear in the frame | |
| 2574 | +/// at a `step`-th of the density. It must stay EVEN -- a chroma plane copies | |
| 2575 | +/// at `dv / 2` full pel, so an odd DV would land half-pel and need the | |
| 2576 | +/// bilinear interpolation decode.rs applies but this encoder does not. | |
| 2577 | +fn intrabc_step() -> usize { | |
| 2578 | + static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new(); | |
| 2579 | + *N.get_or_init(|| { | |
| 2580 | + let n: usize = std::env::var("EC_AV1_INTRABC_STEP") | |
| 2581 | + .ok() | |
| 2582 | + .and_then(|v| v.parse().ok()) | |
| 2583 | + .unwrap_or(4); | |
| 2584 | + if n % 2 == 0 { n.max(2) } else { 4 } | |
| 2585 | + }) | |
| 2586 | +} | |
| 2587 | + | |
| 2588 | +/// The side of the luma square the block-hash table is keyed on. | |
| 2589 | +const IBC_HASH: usize = 16; | |
| 2590 | + | |
| 2591 | +/// libaom `INTRABC_DELAY_PIXELS / 64` -- how many 64x64 superblocks behind | |
| 2592 | +/// the current one the source's bottom-right corner must already be. | |
| 2593 | +const INTRABC_DELAY_SB64: i32 = 4; | |
| 2594 | + | |
| 2595 | +/// FNV-1a over one `IBC_HASH`-square of `plane` at `(x, y)`. | |
| 2596 | +fn ibc_hash(plane: &[u8], width: usize, x: usize, y: usize) -> u64 { | |
| 2597 | + let mut h: u64 = 0xcbf2_9ce4_8422_2325; | |
| 2598 | + for row in 0..IBC_HASH { | |
| 2599 | + let base = (y + row) * width + x; | |
| 2600 | + for &v in &plane[base..base + IBC_HASH] { | |
| 2601 | + h ^= u64::from(v); | |
| 2602 | + h = h.wrapping_mul(0x100_0000_01b3); | |
| 2603 | + } | |
| 2604 | + } | |
| 2605 | + h | |
| 2606 | +} | |
| 2607 | + | |
| 2608 | +/// Every `intrabc_step()`-aligned position of `plane` inside | |
| 2609 | +/// `(x0, y0)..(x1, y1)`, keyed by [`ibc_hash`]. | |
| 2610 | +fn ibc_index( | |
| 2611 | + table: &mut std::collections::HashMap<u64, Vec<(u32, u32)>>, | |
| 2612 | + plane: &[u8], | |
| 2613 | + width: usize, | |
| 2614 | + (x0, y0): (usize, usize), | |
| 2615 | + (x1, y1): (usize, usize), | |
| 2616 | +) { | |
| 2617 | + let step = intrabc_step(); | |
| 2618 | + if x1 < x0 + IBC_HASH || y1 < y0 + IBC_HASH { | |
| 2619 | + return; | |
| 2620 | + } | |
| 2621 | + let mut y = y0.next_multiple_of(step); | |
| 2622 | + while y + IBC_HASH <= y1 { | |
| 2623 | + let mut x = x0.next_multiple_of(step); | |
| 2624 | + while x + IBC_HASH <= x1 { | |
| 2625 | + table | |
| 2626 | + .entry(ibc_hash(plane, width, x, y)) | |
| 2627 | + .or_default() | |
| 2628 | + .push((x as u32, y as u32)); | |
| 2629 | + x += step; | |
| 2630 | + } | |
| 2631 | + y += step; | |
| 2632 | + } | |
| 2633 | +} | |
| 2634 | + | |
| 2635 | +/// Whether `screen`-detected source `y` repeats itself enough to be worth | |
| 2636 | +/// `allow_intrabc` -- the share of its 16-aligned 16x16 blocks whose exact | |
| 2637 | +/// content appears at some other position, against [`intrabc_pct`]. Returns | |
| 2638 | +/// the share as well, for the gate's own print. | |
| 2639 | +fn intrabc_worth_it(y: &[u8], width: usize, true_width: usize, true_height: usize) -> (bool, f64) { | |
| 2640 | + if true_width < 4 * 64 || true_height < 2 * 64 { | |
| 2641 | + return (false, 0.0); | |
| 2642 | + } | |
| 2643 | + let mut table = std::collections::HashMap::new(); | |
| 2644 | + ibc_index(&mut table, y, width, (0, 0), (true_width, true_height)); | |
| 2645 | + let (mut total, mut matched) = (0usize, 0usize); | |
| 2646 | + let mut by = 0; | |
| 2647 | + while by + IBC_HASH <= true_height { | |
| 2648 | + let mut bx = 0; | |
| 2649 | + while bx + IBC_HASH <= true_width { | |
| 2650 | + total += 1; | |
| 2651 | + if let Some(hits) = table.get(&ibc_hash(y, width, bx, by)) { | |
| 2652 | + if hits.iter().any(|&(hx, hy)| (hx as usize, hy as usize) != (bx, by)) { | |
| 2653 | + matched += 1; | |
| 2654 | + } | |
| 2655 | + } | |
| 2656 | + bx += IBC_HASH; | |
| 2657 | + } | |
| 2658 | + by += IBC_HASH; | |
| 2659 | + } | |
| 2660 | + let share = if total == 0 { 0.0 } else { matched as f64 / total as f64 }; | |
| 2661 | + (share * 100.0 >= intrabc_pct() as f64, share) | |
| 2662 | +} | |
| 2663 | + | |
| 2664 | +/// One tile's intrabc search state: a block-hash table over the part of the | |
| 2665 | +/// tile's own reconstruction that is already final (whole superblock rows -- | |
| 2666 | +/// the in-loop filters are off on this frame, so a coded superblock row never | |
| 2667 | +/// changes again), plus the last DV chosen, which the RD price uses as its | |
| 2668 | +/// predictor estimate. | |
| 2669 | +struct Ibc { | |
| 2670 | + /// Keyed on the SOURCE, exactly as libaom's `av1_intrabc_hash` is: the | |
| 2671 | + /// source repeats exactly where a lossily coded reconstruction only | |
| 2672 | + /// nearly does, so an exact-match table over the reconstruction finds | |
| 2673 | + /// almost nothing. The candidate is then SCORED against the | |
| 2674 | + /// reconstruction ([`ibc_sse`]), which is what a decoder will copy. | |
| 2675 | + table: std::collections::HashMap<u64, Vec<(u32, u32)>>, | |
| 2676 | + /// The tile's own luma rect, in samples. | |
| 2677 | + tile: (usize, usize, usize, usize), | |
| 2678 | + /// The DV the previous intrabc block of this tile took -- the *estimate* | |
| 2679 | + /// of decode.rs `read_intrabc_dv`'s predictor the RD price is taken | |
| 2680 | + /// against. The writer computes the true predictor off its own mi grid | |
| 2681 | + /// (`crate::tile::intrabc_dv_pred`); a wrong estimate here can only cost | |
| 2682 | + /// a slightly mispriced candidate, never a wrong bitstream. | |
| 2683 | + last_dv: Option<(i32, i32)>, | |
| 2684 | + /// How many blocks took intrabc, and how many hash lookups found at least | |
| 2685 | + /// one valid candidate -- the gate's own fire counts. | |
| 2686 | + hits: usize, | |
| 2687 | + lookups: usize, | |
| 2688 | + found: usize, | |
| 2689 | +} | |
| 2690 | + | |
| 2691 | +impl Ibc { | |
| 2692 | + fn new(tile: (usize, usize, usize, usize), source: &[u8], width: usize) -> Self { | |
| 2693 | + let mut table = std::collections::HashMap::new(); | |
| 2694 | + ibc_index(&mut table, source, width, (tile.0, tile.1), (tile.2, tile.3)); | |
| 2695 | + Self { | |
| 2696 | + table, | |
| 2697 | + tile, | |
| 2698 | + last_dv: None, | |
| 2699 | + hits: 0, | |
| 2700 | + lookups: 0, | |
| 2701 | + found: 0, | |
| 2702 | + } | |
| 2703 | + } | |
| 2704 | + | |
| 2705 | + /// libaom `av1_is_dv_valid` for a `w`x`h` block at `(x, y)` under `dv` | |
| 2706 | + /// (1/8 pel), with this lane's own extra restriction on top: the source | |
| 2707 | + /// must lie entirely in a COMPLETED superblock row, which is exactly the | |
| 2708 | + /// region [`Self::fill_to`] indexes and a strict subset of what the spec | |
| 2709 | + /// allows. corner-cut, ceiling named: same-superblock-row sources (the | |
| 2710 | + /// wavefront's own diagonal) are legal and not searched; the upgrade path | |
| 2711 | + /// is to index the current row's finished superblocks too. | |
| 2712 | + fn dv_valid(&self, x: usize, y: usize, w: usize, h: usize, dv: (i32, i32)) -> bool { | |
| 2713 | + let (dr, dc) = (dv.0 / 8, dv.1 / 8); | |
| 2714 | + if dv.0 % 8 != 0 || dv.1 % 8 != 0 || dr % 2 != 0 || dc % 2 != 0 { | |
| 2715 | + return false; | |
| 2716 | + } | |
| 2717 | + let (tx0, ty0, tx1, ty1) = ( | |
| 2718 | + self.tile.0 as i32, | |
| 2719 | + self.tile.1 as i32, | |
| 2720 | + self.tile.2 as i32, | |
| 2721 | + self.tile.3 as i32, | |
| 2722 | + ); | |
| 2723 | + let (sx, sy) = (x as i32 + dc, y as i32 + dr); | |
| 2724 | + let (w, h) = (w as i32, h as i32); | |
| 2725 | + if sx < tx0 || sy < ty0 || sx + w > tx1 || sy + h > ty1 { | |
| 2726 | + return false; | |
| 2727 | + } | |
| 2728 | + let active_sb_row = (y as i32 - ty0) >> 6; | |
| 2729 | + let active_sb_col = (x as i32 - tx0) >> 6; | |
| 2730 | + let src_sb_row = (sy + h - 1 - ty0) >> 6; | |
| 2731 | + let src_sb_col = (sx + w - 1 - tx0) >> 6; | |
| 2732 | + // This lane's own restriction (see the doc comment). | |
| 2733 | + if src_sb_row >= active_sb_row { | |
| 2734 | + return false; | |
| 2735 | + } | |
| 2736 | + let total = ((tx1 - tx0 - 1) >> 6) + 1; | |
| 2737 | + if src_sb_row * total + src_sb_col >= active_sb_row * total + active_sb_col - INTRABC_DELAY_SB64 | |
| 2738 | + { | |
| 2739 | + return false; | |
| 2740 | + } | |
| 2741 | + // libaom's wavefront: only the top-left cone of the frame is legal. | |
| 2742 | + let wf = (1 + INTRABC_DELAY_SB64) * (active_sb_row - src_sb_row); | |
| 2743 | + src_sb_col < active_sb_col - INTRABC_DELAY_SB64 + wf | |
| 2744 | + } | |
| 2745 | +} | |
| 2746 | + | |
| 2747 | +/// How many block searches ran, how many found at least one VALID candidate, | |
| 2748 | +/// and how many won on rate-distortion -- the hash hit rate a gate prints | |
| 2749 | +/// (class `gate-blind-to-feature`: "the search ran" and "the search found | |
| 2750 | +/// something" and "the block took it" are three different claims). | |
| 2751 | +static IBC_SEARCH: [std::sync::atomic::AtomicUsize; 3] = | |
| 2752 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 3]; | |
| 2753 | + | |
| 2754 | +impl Drop for Ibc { | |
| 2755 | + fn drop(&mut self) { | |
| 2756 | + for (slot, n) in [self.lookups, self.found, self.hits].into_iter().enumerate() { | |
| 2757 | + IBC_SEARCH[slot].fetch_add(n, std::sync::atomic::Ordering::Relaxed); | |
| 2758 | + } | |
| 2759 | + } | |
| 2760 | +} | |
| 2761 | + | |
| 2762 | +/// Reads [`IBC_SEARCH`] and zeroes it, so a gate can attribute the counts to | |
| 2763 | +/// its own encode. | |
| 2764 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 2765 | +pub(crate) fn take_intrabc_search() -> [usize; 3] { | |
| 2766 | + std::array::from_fn(|i| IBC_SEARCH[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 2767 | +} | |
| 2768 | + | |
| 2769 | +/// [`crate::tile::write_dv_component`]'s static price -- the same shape as | |
| 2770 | +/// [`mv_component_bits`] without the `mv_fr` symbol, which `force_integer_mv` | |
| 2771 | +/// infers rather than codes. | |
| 2772 | +fn dv_component_bits(diff: i32) -> f64 { | |
| 2773 | + let z = diff.unsigned_abs() as i32 - 1; | |
| 2774 | + let mut bits = symbol_bits(&cdf::MV_SIGN, usize::from(diff < 0)); | |
| 2775 | + let class = mv_class_of(z); | |
| 2776 | + bits += symbol_bits(&cdf::MV_CLASS, class); | |
| 2777 | + let local = z - mv_class_base(class); | |
| 2778 | + if class == 0 { | |
| 2779 | + bits += symbol_bits(&cdf::MV_CLASS0_BIT, ((local >> 3) & 1) as usize); | |
| 2780 | + } else { | |
| 2781 | + let d = local >> 3; | |
| 2782 | + for i in 0..class { | |
| 2783 | + bits += symbol_bits(&cdf::MV_BIT[i], ((d >> i) & 1) as usize); | |
| 2784 | + } | |
| 2785 | + } | |
| 2786 | + bits | |
| 2787 | +} | |
| 2788 | + | |
| 2789 | +/// What one block vector costs against `pred`, plus the `skip` and | |
| 2790 | +/// `use_intrabc` flags the block carries and the ordinary intra block does | |
| 2791 | +/// not pay in this currency. | |
| 2792 | +fn dv_bits(dv: (i32, i32), pred: (i32, i32)) -> f64 { | |
| 2793 | + let diff = (dv.0 - pred.0, dv.1 - pred.1); | |
| 2794 | + let joint = match (diff.0 != 0, diff.1 != 0) { | |
| 2795 | + (false, false) => 0, | |
| 2796 | + (false, true) => 1, | |
| 2797 | + (true, false) => 2, | |
| 2798 | + (true, true) => 3, | |
| 2799 | + }; | |
| 2800 | + let mut bits = symbol_bits(&cdf::MV_JOINT, joint) + 2.0; | |
| 2801 | + if diff.0 != 0 { | |
| 2802 | + bits += dv_component_bits(diff.0); | |
| 2803 | + } | |
| 2804 | + if diff.1 != 0 { | |
| 2805 | + bits += dv_component_bits(diff.1); | |
| 2806 | + } | |
| 2807 | + bits | |
| 2808 | +} | |
| 2809 | + | |
| 2810 | +/// The squared error of copying the `side`-square block at `(sx, sy)` of each | |
| 2811 | +/// plane's reconstruction onto the source block at `(x, y)`, luma and both | |
| 2812 | +/// chroma planes together -- what an intrabc block's whole (residual-free) | |
| 2813 | +/// distortion is. | |
| 2814 | +fn ibc_sse( | |
| 2815 | + luma: &Plane, | |
| 2816 | + chroma: &[Plane; 2], | |
| 2817 | + (x, y): (usize, usize), | |
| 2818 | + (sx, sy): (usize, usize), | |
| 2819 | + side: usize, | |
| 2820 | +) -> f64 { | |
| 2821 | + let mut sse = 0.0f64; | |
| 2822 | + for row in 0..side { | |
| 2823 | + let src = &luma.source[(y + row) * luma.width + x..][..side]; | |
| 2824 | + let rec = &luma.reconstruction[(sy + row) * luma.width + sx..][..side]; | |
| 2825 | + for (&s, &r) in src.iter().zip(rec) { | |
| 2826 | + let d = f64::from(i32::from(s) - i32::from(r)); | |
| 2827 | + sse += d * d; | |
| 2828 | + } | |
| 2829 | + } | |
| 2830 | + let half = side / 2; | |
| 2831 | + for plane in chroma { | |
| 2832 | + for row in 0..half { | |
| 2833 | + let src = &plane.source[(y / 2 + row) * plane.width + x / 2..][..half]; | |
| 2834 | + let rec = &plane.reconstruction[(sy / 2 + row) * plane.width + sx / 2..][..half]; | |
| 2835 | + for (&s, &r) in src.iter().zip(rec) { | |
| 2836 | + let d = f64::from(i32::from(s) - i32::from(r)); | |
| 2837 | + sse += d * d; | |
| 2838 | + } | |
| 2839 | + } | |
| 2840 | + } | |
| 2841 | + sse | |
| 2842 | +} | |
| 2843 | + | |
| 2844 | +/// Copies the `side`-square block at `(sx, sy)` of each plane's | |
| 2845 | +/// reconstruction onto `(x, y)` -- an intrabc block's whole reconstruction, | |
| 2846 | +/// since it carries no residual (`skip`). Full-pel in both planes by | |
| 2847 | +/// construction ([`intrabc_step`] keeps every DV even), which is what makes | |
| 2848 | +/// this a plain copy where decode.rs runs the bilinear kernel over the same | |
| 2849 | +/// samples. | |
| 2850 | +fn ibc_commit( | |
| 2851 | + luma: &mut Plane, | |
| 2852 | + chroma: &mut [Plane; 2], | |
| 2853 | + (x, y): (usize, usize), | |
| 2854 | + (sx, sy): (usize, usize), | |
| 2855 | + side: usize, | |
| 2856 | +) { | |
| 2857 | + for row in 0..side { | |
| 2858 | + let from = (sy + row) * luma.width + sx; | |
| 2859 | + let to = (y + row) * luma.width + x; | |
| 2860 | + let copied: Vec<u8> = luma.reconstruction[from..from + side].to_vec(); | |
| 2861 | + luma.reconstruction[to..to + side].copy_from_slice(&copied); | |
| 2862 | + } | |
| 2863 | + let half = side / 2; | |
| 2864 | + for plane in chroma { | |
| 2865 | + for row in 0..half { | |
| 2866 | + let from = (sy / 2 + row) * plane.width + sx / 2; | |
| 2867 | + let to = (y / 2 + row) * plane.width + x / 2; | |
| 2868 | + let copied: Vec<u8> = plane.reconstruction[from..from + half].to_vec(); | |
| 2869 | + plane.reconstruction[to..to + half].copy_from_slice(&copied); | |
| 2870 | + } | |
| 2871 | + } | |
| 2872 | +} | |
| 2873 | + | |
| 2874 | +/// Searches this block's own DV: every hash-table position whose 16x16 luma | |
| 2875 | +/// square equals the block's source square, plus the previous block's DV as a | |
| 2876 | +/// local fallback, scored by [`ibc_sse`] against the DV's own price. Returns | |
| 2877 | +/// the winning `(dv, cost)`, or `None` when nothing valid was found. | |
| 2878 | +fn ibc_search( | |
| 2879 | + ibc: &mut Ibc, | |
| 2880 | + luma: &Plane, | |
| 2881 | + chroma: &[Plane; 2], | |
| 2882 | + (x, y): (usize, usize), | |
| 2883 | + side: usize, | |
| 2884 | + lambda: f64, | |
| 2885 | +) -> Option<((i32, i32), f64, (usize, usize))> { | |
| 2886 | + let pred = ibc.last_dv.unwrap_or((0, -(64 + 256) * 8)); | |
| 2887 | + let mut best: Option<((i32, i32), f64, (usize, usize))> = None; | |
| 2888 | + let consider = |ibc: &Ibc, sx: usize, sy: usize, best: &mut Option<((i32, i32), f64, (usize, usize))>| { | |
| 2889 | + let dv = ( | |
| 2890 | + (sy as i32 - y as i32) * 8, | |
| 2891 | + (sx as i32 - x as i32) * 8, | |
| 2892 | + ); | |
| 2893 | + if !ibc.dv_valid(x, y, side, side, dv) { | |
| 2894 | + return; | |
| 2895 | + } | |
| 2896 | + let cost = ibc_sse(luma, chroma, (x, y), (sx, sy), side) + lambda * dv_bits(dv, pred); | |
| 2897 | + if best.as_ref().is_none_or(|b| cost < b.1) { | |
| 2898 | + *best = Some((dv, cost, (sx, sy))); | |
| 2899 | + } | |
| 2900 | + }; | |
| 2901 | + ibc.lookups += 1; | |
| 2902 | + let key = ibc_hash(luma.source, luma.width, x, y); | |
| 2903 | + // The table is filled in raster order, so the entries BEFORE this block's | |
| 2904 | + // own position are exactly the already-coded ones -- and the nearest of | |
| 2905 | + // them costs the fewest DV bits. Walking backwards from the block's own | |
| 2906 | + // slot is what makes the search look up-and-left; taking the newest | |
| 2907 | + // entries outright only ever finds candidates below and to the right, | |
| 2908 | + // every one of which `dv_valid` rejects. | |
| 2909 | + let hits: Vec<(u32, u32)> = ibc | |
| 2910 | + .table | |
| 2911 | + .get(&key) | |
| 2912 | + .map(|h| { | |
| 2913 | + let end = h.partition_point(|&(hx, hy)| (hy, hx) < (y as u32, x as u32)); | |
| 2914 | + h[..end].iter().rev().take(32).copied().collect() | |
| 2915 | + }) | |
| 2916 | + .unwrap_or_default(); | |
| 2917 | + for (hx, hy) in hits { | |
| 2918 | + consider(&*ibc, hx as usize, hy as usize, &mut best); | |
| 2919 | + } | |
| 2920 | + if let Some(dv) = ibc.last_dv { | |
| 2921 | + let (sx, sy) = (x as i32 + dv.1 / 8, y as i32 + dv.0 / 8); | |
| 2922 | + if sx >= 0 && sy >= 0 { | |
| 2923 | + consider(&*ibc, sx as usize, sy as usize, &mut best); | |
| 2924 | + } | |
| 2925 | + } | |
| 2926 | + if best.is_some() { | |
| 2927 | + ibc.found += 1; | |
| 2928 | + } | |
| 2929 | + best | |
| 2930 | +} | |
| 2931 | + | |
| 2498 | 2932 | fn code_square( |
| 2499 | 2933 | luma: &mut Plane, |
| 2500 | 2934 | chroma: &mut [Plane; 2], |
| @@ -2505,7 +2939,10 @@ fn code_square( | ||
| 2505 | 2939 | // The frame header's `tx_mode == TxMode::Select`: the block then searches |
| 2506 | 2940 | // its own transform depth ([`Plane::code_tx_depth`]) instead of coding one |
| 2507 | 2941 | // transform over its whole side. |
| 2508 | - tx_select: bool, fctx: &crate::decode::FrameCtx, | |
| 2942 | + tx_select: bool, | |
| 2943 | + // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose | |
| 2944 | + // header did not set `allow_intrabc`. | |
| 2945 | + ibc: Option<&mut Ibc>, fctx: &crate::decode::FrameCtx, | |
| 2509 | 2946 | ) -> (BlockCoeffs, f64) { |
| 2510 | 2947 | let (luma_set, chroma_set) = if side == BLOCK { |
| 2511 | 2948 | (TxbSet::Luma32, TxbSet::Chroma16) |
| @@ -2609,6 +3046,29 @@ fn code_square( | ||
| 2609 | 3046 | let ([u, v], uv_mode, chroma_cost, palette_uv) = |
| 2610 | 3047 | search_chroma(chroma, (x, y), side, chroma_set, search, mode, fctx); |
| 2611 | 3048 | cost += chroma_cost; |
| 3049 | + | |
| 3050 | + // The intrabc arm is priced against the WHOLE block -- luma and both | |
| 3051 | + // chroma planes, residual-free -- so it is compared after the chroma | |
| 3052 | + // search has added its own cost, not against the luma decision alone. | |
| 3053 | + if let Some(ibc) = ibc { | |
| 3054 | + if let Some((dv, cost_ibc, src)) = | |
| 3055 | + ibc_search(ibc, luma, chroma, (x, y), side, search.lambda) | |
| 3056 | + { | |
| 3057 | + if cost_ibc < cost { | |
| 3058 | + ibc_commit(luma, chroma, (x, y), src, side); | |
| 3059 | + ibc.last_dv = Some(dv); | |
| 3060 | + ibc.hits += 1; | |
| 3061 | + return ( | |
| 3062 | + BlockCoeffs { | |
| 3063 | + skip: true, | |
| 3064 | + dv: Some(dv), | |
| 3065 | + ..BlockCoeffs::default() | |
| 3066 | + }, | |
| 3067 | + cost_ibc, | |
| 3068 | + ); | |
| 3069 | + } | |
| 3070 | + } | |
| 3071 | + } | |
| 2612 | 3072 | ( |
| 2613 | 3073 | BlockCoeffs { |
| 2614 | 3074 | u, |
| @@ -2923,6 +3383,7 @@ fn code_square_inter( | ||
| 2923 | 3383 | search, |
| 2924 | 3384 | mode_bits, |
| 2925 | 3385 | tx_select() && tx_select_inter(), |
| 3386 | + None, | |
| 2926 | 3387 | fctx, |
| 2927 | 3388 | ); |
| 2928 | 3389 | let luma_set = if side == 8 { |
| @@ -3376,6 +3837,7 @@ fn code_square_inter( | ||
| 3376 | 3837 | uv_mode: DC_PRED, |
| 3377 | 3838 | skip: skip_new, |
| 3378 | 3839 | eight: None, |
| 3840 | + dv: None, | |
| 3379 | 3841 | palette: None, |
| 3380 | 3842 | palette_uv: None, |
| 3381 | 3843 | tx_depth, |
| @@ -3407,6 +3869,7 @@ fn code_square_inter( | ||
| 3407 | 3869 | uv_mode: DC_PRED, |
| 3408 | 3870 | skip, |
| 3409 | 3871 | eight: None, |
| 3872 | + dv: None, | |
| 3410 | 3873 | palette: None, |
| 3411 | 3874 | palette_uv: None, |
| 3412 | 3875 | tx_depth, |
| @@ -4053,8 +4516,30 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4053 | 4516 | (picture.height).min(render.1), |
| 4054 | 4517 | ); |
| 4055 | 4518 | arm_seq_screen(screen); |
| 4519 | + // spec 5.9.2 `allow_intrabc`, screen-detected key frames only: the bit | |
| 4520 | + // forces deblocking, CDEF and loop restoration OFF for this frame | |
| 4521 | + // (frame.rs:194/210/252/272), so it is only set when the source repeats | |
| 4522 | + // itself enough for the copied blocks to pay that back | |
| 4523 | + // ([`intrabc_worth_it`]). | |
| 4524 | + // Single-tile frames only: decode.rs keeps ONE frame-scoped intrabc mi | |
| 4525 | + // grid across every tile while this writer builds one per tile, so a | |
| 4526 | + // multi-tile allow_intrabc frame would predict its DVs off a different | |
| 4527 | + // grid than the decoder does. corner-cut, ceiling named: the upgrade | |
| 4528 | + // path is a frame-scoped grid shared by the tile writers. | |
| 4529 | + let (allow_intrabc, ibc_share) = if screen && intrabc_enabled() && armed_tiles() == (0, 0) { | |
| 4530 | + intrabc_worth_it( | |
| 4531 | + &picture.y.iter().map(|&v| v as u8).collect::<Vec<u8>>(), | |
| 4532 | + picture.width, | |
| 4533 | + picture.width.min(render.0), | |
| 4534 | + picture.height.min(render.1), | |
| 4535 | + ) | |
| 4536 | + } else { | |
| 4537 | + (false, 0.0) | |
| 4538 | + }; | |
| 4539 | + IBC_SHARE.with(|c| c.set(ibc_share)); | |
| 4056 | 4540 | let (seq, mut header) = key_frame_headers_colour(render.0, render.1, base_q_idx, color_config)?; |
| 4057 | 4541 | header.allow_screen_content_tools = screen; |
| 4542 | + header.allow_intrabc = allow_intrabc; | |
| 4058 | 4543 | header.render_width = render.0 as u32; |
| 4059 | 4544 | header.render_height = render.1 as u32; |
| 4060 | 4545 | // `TxMode::Select`: every block codes a `tx_depth` symbol and may split |
| @@ -4179,6 +4664,18 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4179 | 4664 | clip_planes_to_tile(&mut luma, &mut chroma, rect); |
| 4180 | 4665 | let mut above_mode = vec![DC_PRED; cols * 2]; |
| 4181 | 4666 | let mut left_mode = vec![DC_PRED; rows * 2]; |
| 4667 | + let mut ibc = allow_intrabc.then(|| { | |
| 4668 | + Ibc::new( | |
| 4669 | + ( | |
| 4670 | + rect.mi_col0 as usize * 4, | |
| 4671 | + rect.mi_row0 as usize * 4, | |
| 4672 | + (rect.mi_col1 as usize * 4).min(true_width), | |
| 4673 | + (rect.mi_row1 as usize * 4).min(true_height), | |
| 4674 | + ), | |
| 4675 | + &picture_y8, | |
| 4676 | + frame_width, | |
| 4677 | + ) | |
| 4678 | + }); | |
| 4182 | 4679 | let mut coded: Vec<(usize, Superblock, Vec<u8>)> = Vec::new(); |
| 4183 | 4680 | for sb_row in rect.sb_row0 as usize..rect.sb_row1 as usize { |
| 4184 | 4681 | for sb_col in rect.sb_col0 as usize..rect.sb_col1 as usize { |
| @@ -4266,7 +4763,8 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4266 | 4763 | BLOCK, |
| 4267 | 4764 | &search, |
| 4268 | 4765 | &mode_bits(above_mode[c0], left_mode[r0]), |
| 4269 | - tx_select, fctx, | |
| 4766 | + tx_select, | |
| 4767 | + ibc.as_mut(), fctx, | |
| 4270 | 4768 | ); |
| 4271 | 4769 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 4272 | 4770 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -4300,7 +4798,8 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4300 | 4798 | SUB, |
| 4301 | 4799 | &search, |
| 4302 | 4800 | &mode_bits(above_mode[sc], left_mode[sr]), |
| 4303 | - tx_select, fctx, | |
| 4801 | + tx_select, | |
| 4802 | + ibc.as_mut(), fctx, | |
| 4304 | 4803 | ); |
| 4305 | 4804 | cost_split += cost; |
| 4306 | 4805 | split_modes.push(block.mode); |
| @@ -4332,7 +4831,8 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4332 | 4831 | 8, |
| 4333 | 4832 | &search, |
| 4334 | 4833 | &leaf_mode_bits, |
| 4335 | - tx_select, fctx, | |
| 4834 | + tx_select, | |
| 4835 | + None, fctx, | |
| 4336 | 4836 | ); |
| 4337 | 4837 | cost_split += cost; |
| 4338 | 4838 | split_modes.push(leaf.mode); |
| @@ -4431,6 +4931,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4431 | 4931 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); |
| 4432 | 4932 | let mut cdfs = start_cdfs.0.clone(); |
| 4433 | 4933 | crate::tile::arm_screen(screen); |
| 4934 | + crate::tile::arm_intrabc(allow_intrabc); | |
| 4434 | 4935 | let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs( |
| 4435 | 4936 | mi_cols, |
| 4436 | 4937 | mi_rows, |
| @@ -4455,7 +4956,11 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4455 | 4956 | &mut luma, |
| 4456 | 4957 | &mut chroma, |
| 4457 | 4958 | [&picture_y8, &picture_u8, &picture_v8], |
| 4458 | - true, | |
| 4959 | + // spec 5.9.11/5.9.19/5.9.20: `allow_intrabc` forces deblocking, CDEF | |
| 4960 | + // and loop restoration off and codes none of their parameters | |
| 4961 | + // (frame.rs:194/210/252/272) -- there is no filter to search for, and | |
| 4962 | + // the encoder's reconstruction is already what a decoder produces. | |
| 4963 | + !allow_intrabc, | |
| 4459 | 4964 | &mut tiles, |
| 4460 | 4965 | search.lambda, |
| 4461 | 4966 | fctx, |
| @@ -4516,6 +5021,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 4516 | 5021 | loop_filter: header.loop_filter, |
| 4517 | 5022 | cdef: header.cdef, |
| 4518 | 5023 | loop_restoration: header.loop_restoration, |
| 5024 | + allow_intrabc, | |
| 4519 | 5025 | }) |
| 4520 | 5026 | } |
| 4521 | 5027 | |
| @@ -5715,6 +6221,7 @@ fn search_inter_block( | ||
| 5715 | 6221 | skip: best.skip, |
| 5716 | 6222 | inter: best.inter, |
| 5717 | 6223 | eight: None, |
| 6224 | + dv: None, | |
| 5718 | 6225 | palette: None, |
| 5719 | 6226 | palette_uv: None, |
| 5720 | 6227 | tx_depth: 0, |
| @@ -6605,6 +7112,9 @@ pub(crate) fn encode_inter_frame( | ||
| 6605 | 7112 | loop_filter: header.loop_filter, |
| 6606 | 7113 | cdef: header.cdef, |
| 6607 | 7114 | loop_restoration: header.loop_restoration, |
| 7115 | + // An inter frame never allows intrabc (spec 5.9.2 codes the bit on | |
| 7116 | + // intra frames only). | |
| 7117 | + allow_intrabc: false, | |
| 6608 | 7118 | }) |
| 6609 | 7119 | } |
| 6610 | 7120 | |
| @@ -7213,6 +7723,111 @@ mod tests { | ||
| 7213 | 7723 | assert!(!film.screen, "the detector fired on the film-shaped test card"); |
| 7214 | 7724 | } |
| 7215 | 7725 | |
| 7726 | + /// The lane's keep-rule measurement: the gate's own screen capture, one | |
| 7727 | + /// KEY FRAME, coded with and without `allow_intrabc` at four quantizers. | |
| 7728 | + /// Setting the bit forces deblocking, CDEF and loop restoration off for | |
| 7729 | + /// that frame, so the table is exactly the filter loss against the bytes | |
| 7730 | + /// the copied blocks save. | |
| 7731 | + #[test] | |
| 7732 | + #[ignore = "needs the real-library manifest and ffmpeg"] | |
| 7733 | + fn probe_intrabc_key_frame() { | |
| 7734 | + let fctx = &crate::decode::FrameCtx::new(); | |
| 7735 | + let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | |
| 7736 | + .join("../../fixtures"); | |
| 7737 | + let Ok(manifest) = std::fs::read_to_string(fixtures.join("real-library-manifest.tsv")) else { | |
| 7738 | + eprintln!("SKIP probe_intrabc_key_frame: no real-library manifest"); | |
| 7739 | + return; | |
| 7740 | + }; | |
| 7741 | + let Some(clip) = manifest | |
| 7742 | + .lines() | |
| 7743 | + .skip(1) | |
| 7744 | + .filter_map(|l| l.split('\t').next()) | |
| 7745 | + .find(|p| p.contains("/OBS/") && p.ends_with(".mkv") && std::path::Path::new(p).exists()) | |
| 7746 | + else { | |
| 7747 | + eprintln!("SKIP probe_intrabc_key_frame: no OBS recording"); | |
| 7748 | + return; | |
| 7749 | + }; | |
| 7750 | + let (width, height) = (640usize, 384usize); | |
| 7751 | + let picture = clip_frame(clip, "0", width, height); | |
| 7752 | + eprintln!("| base_q_idx | intrabc | bytes | PSNR | blocks | searches/found/won |"); | |
| 7753 | + for q in [5u8, 20, 35, 45] { | |
| 7754 | + for on in [false, true] { | |
| 7755 | + force_intrabc(Some(on)); | |
| 7756 | + let _ = crate::tile::take_intrabc_hits(); | |
| 7757 | + let _ = take_intrabc_search(); | |
| 7758 | + let encoded = encode_key_frame_with_ctx(&picture, q, 0.5, fctx).unwrap(); | |
| 7759 | + let hits = crate::tile::take_intrabc_hits(); | |
| 7760 | + let search = take_intrabc_search(); | |
| 7761 | + eprintln!( | |
| 7762 | + "| {q} | {} | {} | {:.2} dB | {} (dv hist {:?}) | {}/{}/{} |", | |
| 7763 | + if encoded.allow_intrabc { "on" } else { "off" }, | |
| 7764 | + encoded.stream.len(), | |
| 7765 | + psnr_all(&encoded.reconstruction, &picture), | |
| 7766 | + hits[0], | |
| 7767 | + &hits[1..], | |
| 7768 | + search[0], search[1], search[2], | |
| 7769 | + ); | |
| 7770 | + } | |
| 7771 | + } | |
| 7772 | + force_intrabc(None); | |
| 7773 | + } | |
| 7774 | + | |
| 7775 | + /// lane-av1ibc's own end-to-end check: a repeated-pattern screen card | |
| 7776 | + /// sets `allow_intrabc` (so every in-loop filter is off), blocks really | |
| 7777 | + /// take a block vector (fire count + DV histogram, class | |
| 7778 | + /// `gate-blind-to-feature`), and both our own decoder and ffmpeg -- | |
| 7779 | + /// libaom's reader of the `use_intrabc`/`assign_dv` syntax -- reconstruct | |
| 7780 | + /// exactly what the encoder did. | |
| 7781 | + #[test] | |
| 7782 | + fn a_repeated_pattern_key_frame_codes_intrabc_blocks_ffmpeg_decodes_exactly() { | |
| 7783 | + let fctx = &crate::decode::FrameCtx::new(); | |
| 7784 | + // Wide enough for the wavefront rule (`INTRABC_DELAY_SB64` = 4 | |
| 7785 | + // superblocks of 64) and tall enough for a source superblock row | |
| 7786 | + // above the blocks that copy from it. | |
| 7787 | + let (width, height) = (512usize, 256usize); | |
| 7788 | + let picture = screen_card(width, height); | |
| 7789 | + let _ = crate::tile::take_intrabc_hits(); | |
| 7790 | + // The default is OFF (it measured worse on the real screen capture -- | |
| 7791 | + // see [`intrabc_enabled`]); this gate proves the SYNTAX, so it turns | |
| 7792 | + // the arm on for its own encode and puts it back straight after. | |
| 7793 | + force_intrabc(Some(true)); | |
| 7794 | + let encoded = encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap(); | |
| 7795 | + force_intrabc(None); | |
| 7796 | + assert!( | |
| 7797 | + encoded.allow_intrabc, | |
| 7798 | + "allow_intrabc was not set on a fully periodic screen card (source repeat share {:.1}%)", | |
| 7799 | + intrabc_source_share() * 100.0 | |
| 7800 | + ); | |
| 7801 | + let hits = crate::tile::take_intrabc_hits(); | |
| 7802 | + eprintln!( | |
| 7803 | + "intrabc blocks {} DV magnitude histogram (1,2,4,8,16,32+ px) {:?}, source repeat share {:.1}%", | |
| 7804 | + hits[0], | |
| 7805 | + &hits[1..], | |
| 7806 | + intrabc_source_share() * 100.0 | |
| 7807 | + ); | |
| 7808 | + let search = take_intrabc_search(); | |
| 7809 | + eprintln!( | |
| 7810 | + "intrabc searches {} found-valid {} won-RD {}", | |
| 7811 | + search[0], search[1], search[2] | |
| 7812 | + ); | |
| 7813 | + assert!( | |
| 7814 | + hits[0] > 0, | |
| 7815 | + "no block took a block vector -- the `use_intrabc` syntax is unproven: {hits:?}" | |
| 7816 | + ); | |
| 7817 | + let via_us = crate::stream::decode_stream(&encoded.stream).unwrap(); | |
| 7818 | + assert_eq!(via_us[0].y, encoded.reconstruction.y, "our decoder: luma"); | |
| 7819 | + assert_eq!(via_us[0].u, encoded.reconstruction.u, "our decoder: U"); | |
| 7820 | + assert_eq!(via_us[0].v, encoded.reconstruction.v, "our decoder: V"); | |
| 7821 | + if !have_ffmpeg() { | |
| 7822 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | |
| 7823 | + return; | |
| 7824 | + } | |
| 7825 | + let decoded = ffmpeg_decode(&encoded.stream, width, height); | |
| 7826 | + assert_eq!(decoded.y, encoded.reconstruction.y, "ffmpeg: luma"); | |
| 7827 | + assert_eq!(decoded.u, encoded.reconstruction.u, "ffmpeg: U"); | |
| 7828 | + assert_eq!(decoded.v, encoded.reconstruction.v, "ffmpeg: V"); | |
| 7829 | + } | |
| 7830 | + | |
| 7216 | 7831 | /// The minimal repro that isolated the inter-residual desync: a key frame |
| 7217 | 7832 | /// followed by one inter frame whose single superblock carries one |
| 7218 | 7833 | /// `NEARESTMV` block (`mv == (0, 0)`) with exactly one nonzero luma |
| @@ -7249,6 +7864,7 @@ mod tests { | ||
| 7249 | 7864 | uv_mode: 0, |
| 7250 | 7865 | skip: false, |
| 7251 | 7866 | eight: None, |
| 7867 | + dv: None, | |
| 7252 | 7868 | palette: None, |
| 7253 | 7869 | palette_uv: None, |
| 7254 | 7870 | tx_depth: 0, |
crates/ec-av1/src/stream.rsmodified+2 -1
| @@ -3005,7 +3005,8 @@ pub(crate) mod tests { | ||
| 3005 | 3005 | // `false` desyncs at the first one (class: test asserts |
| 3006 | 3006 | // against a stale header). |
| 3007 | 3007 | encoded.screen, |
| 3008 | - false, | |
| 3008 | + // ... and its own `allow_intrabc` (stale-header class). | |
| 3009 | + encoded.allow_intrabc, | |
| 3009 | 3010 | &encoded.loop_restoration, fctx, |
| 3010 | 3011 | ) |
| 3011 | 3012 | .unwrap(); |
crates/ec-av1/src/tile.rsmodified+279 -1
| @@ -314,6 +314,54 @@ fn screen_armed() -> bool { | ||
| 314 | 314 | SCREEN.with(std::cell::Cell::get) |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | +thread_local! { | |
| 318 | + /// This frame header's `allow_intrabc` (spec 5.9.2), armed exactly like | |
| 319 | + /// [`SCREEN`]: with it every intra block of the tile carries a | |
| 320 | + /// `use_intrabc` symbol right after `skip`/`cdef`/`delta_q` (decode.rs | |
| 321 | + /// `read_intra_mode`), so a tile written without it and read with it | |
| 322 | + /// desyncs at the first block. | |
| 323 | + static INTRABC: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | |
| 324 | + /// The mi grid the DV predictor is built off (decode.rs | |
| 325 | + /// `INTRABC_MI_GRID`/`record_intrabc_mi`): every coded block publishes | |
| 326 | + /// its own footprint into it, an intrabc one also its DV as an | |
| 327 | + /// `INTRA_FRAME` candidate. `Some` only while an `allow_intrabc` tile is | |
| 328 | + /// being written. | |
| 329 | + static INTRABC_GRID: std::cell::RefCell<Option<(crate::mvstack::MiGrid, usize, usize)>> = | |
| 330 | + const { std::cell::RefCell::new(None) }; | |
| 331 | +} | |
| 332 | + | |
| 333 | +/// Arms the frame's `allow_intrabc` bit for the next tile written on this | |
| 334 | +/// thread; cleared by [`CdefIdxGuard`] when that writer returns. | |
| 335 | +pub(crate) fn arm_intrabc(on: bool) { | |
| 336 | + INTRABC.with(|c| c.set(on)); | |
| 337 | +} | |
| 338 | + | |
| 339 | +fn intrabc_armed() -> bool { | |
| 340 | + INTRABC.with(std::cell::Cell::get) | |
| 341 | +} | |
| 342 | + | |
| 343 | +/// How many blocks this writer coded `use_intrabc == 1`, and the histogram of | |
| 344 | +/// their DV magnitudes in full pels (index 1..=6 = 1, 2, 4, 8, 16, 32 pels | |
| 345 | +/// and up, by the larger component) -- the fire count a gate prints so that | |
| 346 | +/// "intrabc is on" is a measurement rather than a claim (class | |
| 347 | +/// `gate-blind-to-feature`). Index 0 is the block count. | |
| 348 | +static INTRABC_HITS: [std::sync::atomic::AtomicUsize; 7] = | |
| 349 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 7]; | |
| 350 | + | |
| 351 | +fn note_intrabc(dv: (i32, i32)) { | |
| 352 | + INTRABC_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 353 | + let px = (dv.0.abs() / 8).max(dv.1.abs() / 8).max(1) as usize; | |
| 354 | + let bucket = (usize::BITS - px.leading_zeros()) as usize; | |
| 355 | + INTRABC_HITS[bucket.clamp(1, 6)].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 356 | +} | |
| 357 | + | |
| 358 | +/// Reads [`INTRABC_HITS`] and zeroes it, so a gate can attribute the counts | |
| 359 | +/// to its own encode. | |
| 360 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 361 | +pub(crate) fn take_intrabc_hits() -> [usize; 7] { | |
| 362 | + std::array::from_fn(|i| INTRABC_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 363 | +} | |
| 364 | + | |
| 317 | 365 | thread_local! { |
| 318 | 366 | /// This frame header's own `reference_select` bit (spec 5.9.22), armed |
| 319 | 367 | /// the same way `arm_sign_bias` arms the sign-bias table rather than |
| @@ -708,6 +756,8 @@ impl Drop for CdefIdxGuard { | ||
| 708 | 756 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); |
| 709 | 757 | SCREEN.with(|c| c.set(false)); |
| 710 | 758 | REFERENCE_SELECT.with(|c| c.set(false)); |
| 759 | + INTRABC.with(|c| c.set(false)); | |
| 760 | + INTRABC_GRID.with(|c| *c.borrow_mut() = None); | |
| 711 | 761 | ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); |
| 712 | 762 | } |
| 713 | 763 | } |
| @@ -1213,6 +1263,19 @@ pub struct BlockCoeffs { | ||
| 1213 | 1263 | /// `luma` then carries the whole block's levels in BLOCK coordinates, |
| 1214 | 1264 | /// which the writer slices per transform unit. |
| 1215 | 1265 | pub tx_depth: u8, |
| 1266 | + /// This block's intra block-copy vector (spec 5.11.13 `use_intrabc` -> | |
| 1267 | + /// `assign_dv`), in the spec's 1/8-pel `(row, col)` units, or `None` for | |
| 1268 | + /// an ordinary intra block. Only a key frame whose header set | |
| 1269 | + /// `allow_intrabc` ([`arm_intrabc`]) may carry one; the block is then | |
| 1270 | + /// coded `skip` with no residual, no mode, no palette -- exactly what | |
| 1271 | + /// decode.rs `read_intra_mode` reads back and returns early on. | |
| 1272 | + /// | |
| 1273 | + /// lane-av1ibc corner-cut, ceiling named: a residual-carrying intrabc | |
| 1274 | + /// block needs the INTER coefficient tables plus the var-tx tree | |
| 1275 | + /// decode.rs `decode_block`'s `intrabc_vartx` arm reads; the upgrade path | |
| 1276 | + /// is to route the block through [`write_tx_syntax_inter`] instead of | |
| 1277 | + /// returning early here. | |
| 1278 | + pub dv: Option<(i32, i32)>, | |
| 1216 | 1279 | } |
| 1217 | 1280 | |
| 1218 | 1281 | /// A block's luma palette: its base colours (ascending, only the first |
| @@ -2219,6 +2282,20 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | ||
| 2219 | 2282 | // Consumes whatever `arm_cdef_idx`/`arm_lr` armed, on every exit path. |
| 2220 | 2283 | let _cdef_idx = CdefIdxGuard; |
| 2221 | 2284 | check_blocks(mi_cols, mi_rows)?; |
| 2285 | + // The DV predictor's own mi grid, per tile exactly as decode.rs builds it | |
| 2286 | + // per frame (`decode_key_frame_tile_lr`'s `intrabc_mi_grid`): a tile that | |
| 2287 | + // allows no intrabc arms none and every `record_intrabc_mi` below is a | |
| 2288 | + // no-op. | |
| 2289 | + if intrabc_armed() { | |
| 2290 | + // No `set_tile_bounds`: decode.rs builds this grid for the WHOLE | |
| 2291 | + // frame with the default (whole-grid) bounds, so the writer must | |
| 2292 | + // too. `encode_key_frame_inner` only allows intrabc on a | |
| 2293 | + // single-tile frame, where the two grids are the same grid. | |
| 2294 | + let grid = crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize); | |
| 2295 | + INTRABC_GRID.with(|g| { | |
| 2296 | + *g.borrow_mut() = Some((grid, mi_cols as usize, mi_rows as usize)); | |
| 2297 | + }); | |
| 2298 | + } | |
| 2222 | 2299 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 2223 | 2300 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 2224 | 2301 | if superblocks.len() != (sb_cols * sb_rows) as usize { |
| @@ -2872,6 +2949,170 @@ pub(crate) fn palette_uv_bits(pal: &PaletteUv, side: usize) -> f64 { | ||
| 2872 | 2949 | /// skip flag, its luma intra mode against the CDF its neighbours' modes pick, |
| 2873 | 2950 | /// the angle a directional mode is steered by, and its chroma mode. Hands back |
| 2874 | 2951 | /// the luma mode, which is what the blocks beside it read. |
| 2952 | +/// The DV predictor decode.rs `read_intrabc_dv` derives, mirrored line for | |
| 2953 | +/// line off the writer's own copy of the intrabc mi grid: the `INTRA_FRAME` | |
| 2954 | +/// stack's `nearest_mv` (or `near_mv` when that is zero), or -- when both are | |
| 2955 | +/// -- `av1_find_ref_dv`'s fallback one superblock up, or 256 pixels plus one | |
| 2956 | +/// superblock to the left on the tile's first superblock row, then floored to | |
| 2957 | +/// full pel. Returns `(0, 0)` when no grid is armed, which no `allow_intrabc` | |
| 2958 | +/// tile ever reaches. | |
| 2959 | +fn intrabc_dv_pred(mi_r: usize, mi_c: usize, side: usize) -> (i32, i32) { | |
| 2960 | + const INTRABC_DELAY_PIXELS: i32 = 256; | |
| 2961 | + let n4 = side / MI; | |
| 2962 | + let stack_pred = INTRABC_GRID.with(|g| { | |
| 2963 | + let g = g.borrow(); | |
| 2964 | + g.as_ref().map(|(grid, mi_cols, mi_rows)| { | |
| 2965 | + let stack = crate::mvstack::find_mv_stack( | |
| 2966 | + grid, mi_r, mi_c, n4, n4, 0, /* INTRA_FRAME */ | |
| 2967 | + *mi_cols, *mi_rows, | |
| 2968 | + ); | |
| 2969 | + if stack.nearest_mv == (0, 0) { | |
| 2970 | + stack.near_mv | |
| 2971 | + } else { | |
| 2972 | + stack.nearest_mv | |
| 2973 | + } | |
| 2974 | + }) | |
| 2975 | + }); | |
| 2976 | + let mut pred = stack_pred.unwrap_or((0, 0)); | |
| 2977 | + if pred == (0, 0) { | |
| 2978 | + // 64x64 superblocks: the only size this encoder's sequence header | |
| 2979 | + // signals (`use_128x128_superblock: false`), so `mib_size` is 16 mi. | |
| 2980 | + // The tile row start is 0, exactly as decode.rs `read_intrabc_dv` | |
| 2981 | + // takes it -- both are written for the single-tile-row streams this | |
| 2982 | + // encoder produces. | |
| 2983 | + let sb_mi = 16i32; | |
| 2984 | + let sb_px = sb_mi * MI as i32; | |
| 2985 | + pred = if (mi_r as i32) < sb_mi { | |
| 2986 | + (0, -(sb_px + INTRABC_DELAY_PIXELS) * 8) | |
| 2987 | + } else { | |
| 2988 | + (-sb_px * 8, 0) | |
| 2989 | + }; | |
| 2990 | + } | |
| 2991 | + ((pred.0 >> 3) * 8, (pred.1 >> 3) * 8) | |
| 2992 | +} | |
| 2993 | + | |
| 2994 | +/// Publishes one coded block into [`INTRABC_GRID`], mirroring decode.rs | |
| 2995 | +/// `record_intrabc_mi`: every block contributes its size, an intrabc one also | |
| 2996 | +/// its DV as an `INTRA_FRAME` candidate. | |
| 2997 | +fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>) { | |
| 2998 | + INTRABC_GRID.with(|g| { | |
| 2999 | + let mut g = g.borrow_mut(); | |
| 3000 | + let Some((grid, _, _)) = g.as_mut() else { | |
| 3001 | + return; | |
| 3002 | + }; | |
| 3003 | + let info = crate::mvstack::MiInfo { | |
| 3004 | + is_inter: dv.is_some(), | |
| 3005 | + ref_frame: 0, | |
| 3006 | + ref_frame1: crate::mvstack::NO_REF1, | |
| 3007 | + mv: crate::mvstack::mv16(dv.unwrap_or((0, 0))), | |
| 3008 | + mv1: (0, 0), | |
| 3009 | + is_new_mv: dv.is_some(), | |
| 3010 | + is_global_mv0: false, | |
| 3011 | + is_global_mv1: false, | |
| 3012 | + size: n4 as u8, | |
| 3013 | + size_h: n4 as u8, | |
| 3014 | + }; | |
| 3015 | + for r in mi_r..mi_r + n4 { | |
| 3016 | + for c in mi_c..mi_c + n4 { | |
| 3017 | + grid.set(r, c, info); | |
| 3018 | + } | |
| 3019 | + } | |
| 3020 | + }); | |
| 3021 | +} | |
| 3022 | + | |
| 3023 | +/// One full-pel block-vector component, [`write_mv_component`]'s | |
| 3024 | +/// `force_integer_mv` twin: decode.rs `read_mv_component` infers `mv_fr`/ | |
| 3025 | +/// `mv_class0_fr` as 3 and the high-precision bit as 1 under | |
| 3026 | +/// `force_integer_mv`, so neither symbol is written here -- writing them | |
| 3027 | +/// would leave the decoder two symbols behind from this block on. | |
| 3028 | +fn write_dv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i32) -> Result<()> { | |
| 3029 | + let mag = diff.unsigned_abs() as i32; | |
| 3030 | + if mag % 8 != 0 { | |
| 3031 | + return Err(Error::unsupported( | |
| 3032 | + "AV1 tile", | |
| 3033 | + "a block vector is coded at full-pel precision only", | |
| 3034 | + )); | |
| 3035 | + } | |
| 3036 | + enc.symbol(usize::from(diff < 0), &mut c.sign); | |
| 3037 | + let z = mag - 1; | |
| 3038 | + let class = mv_class_of(z); | |
| 3039 | + enc.symbol(class, &mut c.class); | |
| 3040 | + let local = z - mv_class_base(class); | |
| 3041 | + if class == 0 { | |
| 3042 | + enc.symbol(((local >> 3) & 1) as usize, &mut c.class0_bit); | |
| 3043 | + } else { | |
| 3044 | + let d = local >> 3; | |
| 3045 | + for i in 0..class { | |
| 3046 | + enc.symbol(((d >> i) & 1) as usize, &mut c.bit[i]); | |
| 3047 | + } | |
| 3048 | + } | |
| 3049 | + Ok(()) | |
| 3050 | +} | |
| 3051 | + | |
| 3052 | +/// One intrabc block's DV as a residual against [`intrabc_dv_pred`], off the | |
| 3053 | +/// `dv` nmv context (decode.rs `read_intrabc_dv` -> `read_mv`). | |
| 3054 | +fn write_dv(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, dv: (i32, i32), pred: (i32, i32)) -> Result<()> { | |
| 3055 | + let diff = (dv.0 - pred.0, dv.1 - pred.1); | |
| 3056 | + let joint = match (diff.0 != 0, diff.1 != 0) { | |
| 3057 | + (false, false) => 0, | |
| 3058 | + (false, true) => 1, | |
| 3059 | + (true, false) => 2, | |
| 3060 | + (true, true) => 3, | |
| 3061 | + }; | |
| 3062 | + enc.symbol(joint, &mut cdfs.dv_joint); | |
| 3063 | + if diff.0 != 0 { | |
| 3064 | + write_dv_component(enc, &mut cdfs.dv_comp[0], diff.0)?; | |
| 3065 | + } | |
| 3066 | + if diff.1 != 0 { | |
| 3067 | + write_dv_component(enc, &mut cdfs.dv_comp[1], diff.1)?; | |
| 3068 | + } | |
| 3069 | + Ok(()) | |
| 3070 | +} | |
| 3071 | + | |
| 3072 | +/// The mode-info half of an intrabc block (decode.rs `read_intra_mode`'s | |
| 3073 | +/// early return): `skip`, `cdef_idx`, `use_intrabc` and the DV, then nothing | |
| 3074 | +/// -- no y/uv mode, no angle delta, no palette, and (because it is skipped) | |
| 3075 | +/// no transform size and no residual. Publishes the block into every | |
| 3076 | +/// neighbour band an ordinary block leaves behind, at the DC_PRED/uncoded/ | |
| 3077 | +/// skipped state a decoder reads back for it. | |
| 3078 | +fn write_intrabc_block( | |
| 3079 | + enc: &mut SymbolEncoder, | |
| 3080 | + cdfs: &mut Cdfs, | |
| 3081 | + neighbours: &mut Neighbours, | |
| 3082 | + dv: (i32, i32), | |
| 3083 | + mi: (usize, usize), | |
| 3084 | + side: usize, | |
| 3085 | +) -> Result<()> { | |
| 3086 | + let skip_ctx = usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]); | |
| 3087 | + enc.symbol(1, &mut cdfs.skip[skip_ctx]); | |
| 3088 | + write_cdef_idx(enc, mi, true); | |
| 3089 | + enc.symbol(1, &mut cdfs.intrabc); | |
| 3090 | + write_dv(enc, cdfs, dv, intrabc_dv_pred(mi.0, mi.1, side))?; | |
| 3091 | + note_intrabc(dv); | |
| 3092 | + // The bands: DC_PRED mode (what the decoder forces), no coefficients | |
| 3093 | + // anywhere (skip), the block's own side for the partition context, the | |
| 3094 | + // largest transform (`side.min(64)`, decode.rs's `intrabc_skip_tx` arm) | |
| 3095 | + // for the deblock grid, and `skip` itself for the next block's skip | |
| 3096 | + // context. | |
| 3097 | + // One zero level per plane: `neighbour_state` reads `grid[0]` for the DC | |
| 3098 | + // sign vote, and a skipped block leaves every context cleared. | |
| 3099 | + let empty: [Vec<i32>; 3] = [vec![0], vec![0], vec![0]]; | |
| 3100 | + for cell in 0..(side / SUB).max(1) { | |
| 3101 | + let (r, c) = (mi.0 / (SUB / MI), mi.1 / (SUB / MI)); | |
| 3102 | + neighbours.above_mode[c + cell] = DC_PRED; | |
| 3103 | + neighbours.left_mode[r + cell] = DC_PRED; | |
| 3104 | + neighbours.above_side[c + cell] = side; | |
| 3105 | + neighbours.left_side[r + cell] = side; | |
| 3106 | + } | |
| 3107 | + neighbours.record_mi_planes(mi, side, &empty, true); | |
| 3108 | + neighbours.record_tx(mi, side, side.min(64)); | |
| 3109 | + for cell in 0..side / MI { | |
| 3110 | + neighbours.above_skip[mi.1 + cell] = true; | |
| 3111 | + neighbours.left_skip[mi.0 + cell] = true; | |
| 3112 | + } | |
| 3113 | + Ok(()) | |
| 3114 | +} | |
| 3115 | + | |
| 2875 | 3116 | fn write_intra_mode( |
| 2876 | 3117 | enc: &mut SymbolEncoder, |
| 2877 | 3118 | cdfs: &mut Cdfs, |
| @@ -2888,8 +3129,18 @@ fn write_intra_mode( | ||
| 2888 | 3129 | side: usize, |
| 2889 | 3130 | ) -> usize { |
| 2890 | 3131 | let mode = usize::from(block.mode); |
| 2891 | - enc.symbol(0, &mut cdfs.skip[0]); | |
| 3132 | + // `av1_get_skip_txfm_context` (decode.rs `Neighbours::skip_txfm_ctx`): | |
| 3133 | + // above + left. Every block this writer codes outside an `allow_intrabc` | |
| 3134 | + // frame is unskipped, so both bands stay false and this stays 0 -- the | |
| 3135 | + // literal every stream before this lane was written with. | |
| 3136 | + let skip_ctx = usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]); | |
| 3137 | + enc.symbol(0, &mut cdfs.skip[skip_ctx]); | |
| 2892 | 3138 | write_cdef_idx(enc, mi, false); |
| 3139 | + // `read_intrabc_info` (spec 5.11.13): the flag is read for EVERY intra | |
| 3140 | + // block of an `allow_intrabc` frame, not only the ones that use it. | |
| 3141 | + if intrabc_armed() { | |
| 3142 | + enc.symbol(0, &mut cdfs.intrabc); | |
| 3143 | + } | |
| 2893 | 3144 | enc.symbol( |
| 2894 | 3145 | mode, |
| 2895 | 3146 | &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]], |
| @@ -3090,6 +3341,11 @@ fn write_block( | ||
| 3090 | 3341 | ) -> Result<()> { |
| 3091 | 3342 | let (r, c) = at; |
| 3092 | 3343 | let at_mi = (r * (SUB / MI), c * (SUB / MI)); |
| 3344 | + if let Some(dv) = block.dv { | |
| 3345 | + write_intrabc_block(enc, cdfs, neighbours, dv, at_mi, side)?; | |
| 3346 | + record_intrabc_mi(at_mi.0, at_mi.1, side / MI, Some(dv)); | |
| 3347 | + return Ok(()); | |
| 3348 | + } | |
| 3093 | 3349 | let (above_mode, left_mode) = (neighbours.above_mode[c], neighbours.left_mode[r]); |
| 3094 | 3350 | let mode = write_intra_mode( |
| 3095 | 3351 | enc, |
| @@ -3129,9 +3385,24 @@ fn write_block( | ||
| 3129 | 3385 | tx_select, |
| 3130 | 3386 | ); |
| 3131 | 3387 | neighbours.record_planes(at, side, mode, grids, !split); |
| 3388 | + clear_skip_band(neighbours, at_mi, side); | |
| 3389 | + record_intrabc_mi(at_mi.0, at_mi.1, side / MI, None); | |
| 3132 | 3390 | Ok(()) |
| 3133 | 3391 | } |
| 3134 | 3392 | |
| 3393 | +/// An unskipped block's own `skip` publication: only an `allow_intrabc` | |
| 3394 | +/// frame ever sets these bands, so a frame without one leaves them exactly | |
| 3395 | +/// as they were before this lane (all false). | |
| 3396 | +fn clear_skip_band(neighbours: &mut Neighbours, mi: (usize, usize), side: usize) { | |
| 3397 | + if !intrabc_armed() { | |
| 3398 | + return; | |
| 3399 | + } | |
| 3400 | + for cell in 0..side / MI { | |
| 3401 | + neighbours.above_skip[mi.1 + cell] = false; | |
| 3402 | + neighbours.left_skip[mi.0 + cell] = false; | |
| 3403 | + } | |
| 3404 | +} | |
| 3405 | + | |
| 3135 | 3406 | /// Writes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect): its own |
| 3136 | 3407 | /// luma transform and 4x4 chroma transforms, coded exactly like |
| 3137 | 3408 | /// [`write_block`] but reading its intra-mode context from the *enclosing* |
| @@ -3155,6 +3426,11 @@ fn write_leaf8( | ||
| 3155 | 3426 | all_scans: &[Vec<u16>; 4], |
| 3156 | 3427 | ) -> Result<usize> { |
| 3157 | 3428 | let (r, c) = outer_at; |
| 3429 | + if let Some(dv) = block.dv { | |
| 3430 | + write_intrabc_block(enc, cdfs, neighbours, dv, leaf_mi, 8)?; | |
| 3431 | + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, Some(dv)); | |
| 3432 | + return Ok(DC_PRED); | |
| 3433 | + } | |
| 3158 | 3434 | let mut above_mode = neighbours.above_mode[c]; |
| 3159 | 3435 | let mut left_mode = neighbours.left_mode[r]; |
| 3160 | 3436 | // The previous leaf sits directly above (same column, two mi rows up) |
| @@ -3204,6 +3480,8 @@ fn write_leaf8( | ||
| 3204 | 3480 | tx_select, |
| 3205 | 3481 | ); |
| 3206 | 3482 | neighbours.record_mi_planes(leaf_mi, 8, grids, !split); |
| 3483 | + clear_skip_band(neighbours, leaf_mi, 8); | |
| 3484 | + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, None); | |
| 3207 | 3485 | Ok(mode) |
| 3208 | 3486 | } |
| 3209 | 3487 |