Retain a second past leaf picture and offer it as LAST2 (off by default)
Browse files at this commitec4f10e671ffcc0a2ed0bc4b59f9e682e47788fdParent: 46b5c0f- feat
- retain a second past leaf picture and offer it as LAST2 (off by default)
3 changed files +192 -14
- crates/ec-av1/src/encode.rs +57 -10
- crates/ec-av1/src/encoder.rs +88 -4
- lanes/last2.report.md +47 -0
crates/ec-av1/src/encode.rsmodified+57 -10
| @@ -9107,14 +9107,22 @@ fn search_inter_block( | |||
| 9107 | let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); | 9107 | let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); |
| 9108 | let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); | 9108 | let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); |
| 9109 | // `write_single_ref`'s own tree, at the fixed context this pricer uses | 9109 | // `write_single_ref`'s own tree, at the fixed context this pricer uses |
| 9110 | - // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0 and GOLDEN | 9110 | + // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0, LAST2 |
| 9111 | - // p1=0,p3=1,p5=1. | 9111 | + // p1=0,p3=0,p4=1 and GOLDEN p1=0,p3=1,p5=1. A BACKWARD reference |
| 9112 | + // (`ALTREF`, p1=1,p2=1) is priced as LAST here and always has been -- | ||
| 9113 | + // wrong by two symbols, but fixing it moves every shipped stream, so it | ||
| 9114 | + // is a lever of its own and not this lane's (lanes/last2.report.md). | ||
| 9112 | let single_ref_bits = |ref_frame: i8| { | 9115 | let single_ref_bits = |ref_frame: i8| { |
| 9116 | + use crate::mvstack::{GOLDEN_FRAME, LAST2_FRAME}; | ||
| 9113 | symbol_bits(&cdf::SINGLE_REF[0][0], 0) | 9117 | symbol_bits(&cdf::SINGLE_REF[0][0], 0) |
| 9114 | - + if ref_frame == crate::mvstack::GOLDEN_FRAME { | 9118 | + + if ref_frame == GOLDEN_FRAME { |
| 9115 | symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1) | 9119 | symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1) |
| 9116 | } else { | 9120 | } else { |
| 9117 | - symbol_bits(&cdf::SINGLE_REF[0][2], 0) + symbol_bits(&cdf::SINGLE_REF[0][3], 0) | 9121 | + symbol_bits(&cdf::SINGLE_REF[0][2], 0) |
| 9122 | + + symbol_bits( | ||
| 9123 | + &cdf::SINGLE_REF[0][3], | ||
| 9124 | + usize::from(ref_frame == LAST2_FRAME), | ||
| 9125 | + ) | ||
| 9118 | } | 9126 | } |
| 9119 | }; | 9127 | }; |
| 9120 | 9128 | ||
| @@ -10563,6 +10571,12 @@ pub(crate) struct PyramidFrame { | |||
| 10563 | /// The slot `ALTREF_FRAME` reads -- a FUTURE frame for a leaf of a | 10571 | /// The slot `ALTREF_FRAME` reads -- a FUTURE frame for a leaf of a |
| 10564 | /// pyramid, which is what makes `sign_bias`'s last entry true. | 10572 | /// pyramid, which is what makes `sign_bias`'s last entry true. |
| 10565 | pub altref_slot: u8, | 10573 | pub altref_slot: u8, |
| 10574 | + /// lane-last2: the slot `LAST2_FRAME` reads -- a SECOND, older past | ||
| 10575 | + /// picture (the leaf chain alternates its refresh between two slots, so | ||
| 10576 | + /// the frame before `LAST` is still intact). `None` leaves | ||
| 10577 | + /// `ref_frame_idx[1]` naming `last_slot`, as every frame this encoder | ||
| 10578 | + /// wrote before, and the caller then passes no `last2` picture either. | ||
| 10579 | + pub last2_slot: Option<u8>, | ||
| 10566 | /// `show_frame`: false for a hidden frame, output later by a | 10580 | /// `show_frame`: false for a hidden frame, output later by a |
| 10567 | /// `show_existing_frame` header. | 10581 | /// `show_existing_frame` header. |
| 10568 | pub show_frame: bool, | 10582 | pub show_frame: bool, |
| @@ -10598,7 +10612,14 @@ pub(crate) fn encode_inter_frame( | |||
| 10598 | // The frame two back, held in the slot this frame is about to refresh | 10612 | // The frame two back, held in the slot this frame is about to refresh |
| 10599 | // (the encoder alternates 0/2): `ALTREF_FRAME`. `None` for the first two | 10613 | // (the encoder alternates 0/2): `ALTREF_FRAME`. `None` for the first two |
| 10600 | // inter frames of a GOP, where the frame two back IS the key frame. | 10614 | // inter frames of a GOP, where the frame two back IS the key frame. |
| 10601 | - altref: Option<&Picture>, fctx: &crate::decode::FrameCtx, | 10615 | + altref: Option<&Picture>, |
| 10616 | + // lane-last2: the picture in [`PyramidFrame::last2_slot`] -- the shown | ||
| 10617 | + // frame BEFORE `reference` on the leaf chain, offered to the 32x32 | ||
| 10618 | + // search as `LAST2_FRAME` through the same search path `GOLDEN`/`ALTREF` | ||
| 10619 | + // take. `None` (every caller but the pyramid leaves) codes the frame | ||
| 10620 | + // with the reference set unchanged. | ||
| 10621 | + last2: Option<&Picture>, | ||
| 10622 | + fctx: &crate::decode::FrameCtx, | ||
| 10602 | // Where this frame sits in the coding-order pyramid; `None` is the flat | 10623 | // Where this frame sits in the coding-order pyramid; `None` is the flat |
| 10603 | // one-shown-frame-per-picture stream this encoder wrote before. | 10624 | // one-shown-frame-per-picture stream this encoder wrote before. |
| 10604 | pyramid: Option<PyramidFrame>, | 10625 | pyramid: Option<PyramidFrame>, |
| @@ -10679,6 +10700,14 @@ pub(crate) fn encode_inter_frame( | |||
| 10679 | // `GOLDEN_FRAME` is the key frame, order hint 0. Only | 10700 | // `GOLDEN_FRAME` is the key frame, order hint 0. Only |
| 10680 | // `frame::write_frame_header`'s `skipModeAllowed` reads this. | 10701 | // `frame::write_frame_header`'s `skipModeAllowed` reads this. |
| 10681 | header.order_hints = order_hints; | 10702 | header.order_hints = order_hints; |
| 10703 | + // lane-last2: `inter_frame_headers_slots` points every un-named | ||
| 10704 | + // reference at `last_slot`; the pyramid's leaf chain retains a second, | ||
| 10705 | + // older picture and names it here. The caller's `sign_bias` / | ||
| 10706 | + // `order_hints` entry 1 is derived from this same slot, so the stack | ||
| 10707 | + // scans and `skipModeAllowed` read what the decoder will. | ||
| 10708 | + if let Some(slot) = pyramid.and_then(|p| p.last2_slot) { | ||
| 10709 | + header.ref_frame_idx[1] = slot; | ||
| 10710 | + } | ||
| 10682 | header.reference_select = reference_select(); | 10711 | header.reference_select = reference_select(); |
| 10683 | // Armed HERE, not only beside the tile write below: `ref_distance`'s own | 10712 | // Armed HERE, not only beside the tile write below: `ref_distance`'s own |
| 10684 | // per-reference order-hint distance is read by the block SEARCH, which | 10713 | // per-reference order-hint distance is read by the block SEARCH, which |
| @@ -10914,6 +10943,7 @@ pub(crate) fn encode_inter_frame( | |||
| 10914 | // built once per tile job (`obmc_prediction`). | 10943 | // built once per tile job (`obmc_prediction`). |
| 10915 | let mut obmc_refs: [Option<&Picture>; 8] = [None; 8]; | 10944 | let mut obmc_refs: [Option<&Picture>; 8] = [None; 8]; |
| 10916 | obmc_refs[crate::mvstack::LAST_FRAME as usize] = Some(reference); | 10945 | obmc_refs[crate::mvstack::LAST_FRAME as usize] = Some(reference); |
| 10946 | + obmc_refs[crate::mvstack::LAST2_FRAME as usize] = last2; | ||
| 10917 | obmc_refs[crate::mvstack::GOLDEN_FRAME as usize] = golden; | 10947 | obmc_refs[crate::mvstack::GOLDEN_FRAME as usize] = golden; |
| 10918 | obmc_refs[crate::mvstack::ALTREF_FRAME as usize] = altref; | 10948 | obmc_refs[crate::mvstack::ALTREF_FRAME as usize] = altref; |
| 10919 | grid.set_tile_bounds( | 10949 | grid.set_tile_bounds( |
| @@ -11105,6 +11135,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11105 | let stack = | 11135 | let stack = |
| 11106 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); | 11136 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 11107 | let extra_stacks: Vec<(i8, &Picture, MvStack)> = [ | 11137 | let extra_stacks: Vec<(i8, &Picture, MvStack)> = [ |
| 11138 | + (crate::mvstack::LAST2_FRAME, last2), | ||
| 11108 | (crate::mvstack::GOLDEN_FRAME, golden), | 11139 | (crate::mvstack::GOLDEN_FRAME, golden), |
| 11109 | (crate::mvstack::ALTREF_FRAME, altref), | 11140 | (crate::mvstack::ALTREF_FRAME, altref), |
| 11110 | ] | 11141 | ] |
| @@ -11660,8 +11691,22 @@ pub(crate) fn encode_inter_frame( | |||
| 11660 | } | 11691 | } |
| 11661 | } | 11692 | } |
| 11662 | }); | 11693 | }); |
| 11694 | + let trial_last2 = last2.map(|l| { | ||
| 11695 | + if (l.width, l.height) == (fw, fh) { | ||
| 11696 | + l.clone() | ||
| 11697 | + } else { | ||
| 11698 | + Picture { | ||
| 11699 | + width: fw, | ||
| 11700 | + height: fh, | ||
| 11701 | + y: crop_plane(&l.y, l.width, fw, fh), | ||
| 11702 | + u: crop_plane(&l.u, l.width / 2, fw / 2, fh / 2), | ||
| 11703 | + v: crop_plane(&l.v, l.width / 2, fw / 2, fh / 2), | ||
| 11704 | + } | ||
| 11705 | + } | ||
| 11706 | + }); | ||
| 11663 | let mut refs: [Option<&Picture>; 8] = [None; 8]; | 11707 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 11664 | refs[1] = Some(&trial_ref); | 11708 | refs[1] = Some(&trial_ref); |
| 11709 | + refs[2] = trial_last2.as_ref(); | ||
| 11665 | refs[4] = trial_golden.as_ref(); | 11710 | refs[4] = trial_golden.as_ref(); |
| 11666 | refs[7] = trial_altref.as_ref(); | 11711 | refs[7] = trial_altref.as_ref(); |
| 11667 | let refpix = crate::decode::RefPix::ready(refs); | 11712 | let refpix = crate::decode::RefPix::ready(refs); |
| @@ -11937,7 +11982,9 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 11937 | render, | 11982 | render, |
| 11938 | Some(&carried.0), | 11983 | Some(&carried.0), |
| 11939 | Some(&golden), | 11984 | Some(&golden), |
| 11940 | - prev2.as_ref(), fctx, | 11985 | + prev2.as_ref(), |
| 11986 | + None, | ||
| 11987 | + fctx, | ||
| 11941 | None, | 11988 | None, |
| 11942 | &lookahead, | 11989 | &lookahead, |
| 11943 | )?; | 11990 | )?; |
| @@ -14640,7 +14687,7 @@ mod tests { | |||
| 14640 | let padded_pic = picture.padded_to(SUPERBLOCK); | 14687 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 14641 | let t = Instant::now(); | 14688 | let t = Instant::now(); |
| 14642 | let inter = | 14689 | let inter = |
| 14643 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, fctx, None, &[]).unwrap(); | 14690 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, None, fctx, None, &[]).unwrap(); |
| 14644 | let inter_t = t.elapsed(); | 14691 | let inter_t = t.elapsed(); |
| 14645 | eprintln!( | 14692 | eprintln!( |
| 14646 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ | 14693 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -14739,7 +14786,7 @@ mod tests { | |||
| 14739 | stage_reset(); | 14786 | stage_reset(); |
| 14740 | let t = Instant::now(); | 14787 | let t = Instant::now(); |
| 14741 | let inter = | 14788 | let inter = |
| 14742 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, fctx, None, &[]).unwrap(); | 14789 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, None, fctx, None, &[]).unwrap(); |
| 14743 | let total_t = t.elapsed(); | 14790 | let total_t = t.elapsed(); |
| 14744 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); | 14791 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 14745 | let accounted = motion_search + transform_quant + entropy; | 14792 | let accounted = motion_search + transform_quant + entropy; |
| @@ -14919,7 +14966,7 @@ mod tests { | |||
| 14919 | 0.5, | 14966 | 0.5, |
| 14920 | 1, | 14967 | 1, |
| 14921 | flat_order_hints(1, 0, false), | 14968 | flat_order_hints(1, 0, false), |
| 14922 | - (width, height), None, None, None, fctx, None, &[], | 14969 | + (width, height), None, None, None, None, fctx, None, &[], |
| 14923 | ) | 14970 | ) |
| 14924 | .unwrap(); | 14971 | .unwrap(); |
| 14925 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); | 14972 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); |
| @@ -14937,7 +14984,7 @@ mod tests { | |||
| 14937 | 0.5, | 14984 | 0.5, |
| 14938 | 1, | 14985 | 1, |
| 14939 | flat_order_hints(1, 0, false), | 14986 | flat_order_hints(1, 0, false), |
| 14940 | - (width, height), None, None, None, fctx, None, &[], | 14987 | + (width, height), None, None, None, None, fctx, None, &[], |
| 14941 | ) | 14988 | ) |
| 14942 | .unwrap(); | 14989 | .unwrap(); |
| 14943 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); | 14990 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); |
crates/ec-av1/src/encoder.rsmodified+88 -4
| @@ -961,6 +961,28 @@ const ANCHOR_SLOTS: [u8; 2] = [3, 4]; | |||
| 961 | /// The slot the leaf chain refreshes and reads as `LAST_FRAME`. | 961 | /// The slot the leaf chain refreshes and reads as `LAST_FRAME`. |
| 962 | const LEAF_SLOT: u8 = 0; | 962 | const LEAF_SLOT: u8 = 0; |
| 963 | 963 | ||
| 964 | +/// lane-last2: the two slots the leaf chain alternates its refresh between | ||
| 965 | +/// once [`last2`] is on, so that the shown frame BEFORE `LAST_FRAME` is still | ||
| 966 | +/// in the DPB and can be named `LAST2_FRAME`. Slot 7 is the one the key frame | ||
| 967 | +/// writes and nothing has ever read back, so this costs no other level a slot; | ||
| 968 | +/// leaf `k` refreshes `LEAF_SLOTS[k % 2]`, which is the slot leaf `k - 2` | ||
| 969 | +/// wrote -- read before write, spec 7.20's decode order. | ||
| 970 | +const LEAF_SLOTS: [u8; 2] = [LEAF_SLOT, 7]; | ||
| 971 | + | ||
| 972 | +/// Whether the leaf chain retains that second past picture and offers it to | ||
| 973 | +/// the block search as `LAST2_FRAME` (`EC_AV1_LAST2=0` / `=1`). The census | ||
| 974 | +/// that priced the lever is `lanes/refs.report.md` §1; what it buys on the | ||
| 975 | +/// gate is `lanes/last2.report.md`. | ||
| 976 | +fn last2() -> bool { | ||
| 977 | + match std::env::var("EC_AV1_LAST2").ok() { | ||
| 978 | + Some(v) => v != "0", | ||
| 979 | + None => LAST2, | ||
| 980 | + } | ||
| 981 | +} | ||
| 982 | + | ||
| 983 | +/// [`last2`]'s default. | ||
| 984 | +const LAST2: bool = false; | ||
| 985 | + | ||
| 964 | /// The slot the mid-level hidden frame of a three-level group refreshes | 986 | /// The slot the mid-level hidden frame of a three-level group refreshes |
| 965 | /// ([`Pyramid::mid_q_offset`]): free of the two anchors (3, 4), of the leaf | 987 | /// ([`Pyramid::mid_q_offset`]): free of the two anchors (3, 4), of the leaf |
| 966 | /// chain (0) and of `GOLDEN_SLOT` (1), so all four levels are live at once. | 988 | /// chain (0) and of `GOLDEN_SLOT` (1), so all four levels are live at once. |
| @@ -1108,6 +1130,11 @@ pub struct Av1Encoder { | |||
| 1108 | /// Which of [`ANCHOR_SLOTS`] the group about to be coded reads as its | 1130 | /// Which of [`ANCHOR_SLOTS`] the group about to be coded reads as its |
| 1109 | /// anchor. | 1131 | /// anchor. |
| 1110 | anchor: usize, | 1132 | anchor: usize, |
| 1133 | + /// lane-last2: the [`LEAF_SLOTS`] the last two SHOWN leaves refreshed, | ||
| 1134 | + /// most recent first, across mini-GOP boundaries (the first leaf of a | ||
| 1135 | + /// group reads the anchor as `LAST_FRAME`, so its `LAST2_FRAME` is the | ||
| 1136 | + /// previous group's last leaf). Cleared by every key frame. | ||
| 1137 | + leaf_hist: Vec<u8>, | ||
| 1111 | /// Coding-order position of the next packet — a packet's `dts`. | 1138 | /// Coding-order position of the next packet — a packet's `dts`. |
| 1112 | next_dts: u64, | 1139 | next_dts: u64, |
| 1113 | /// Set by [`Av1Encoder::encode_sequence_pyramid`]: every coded frame's | 1140 | /// Set by [`Av1Encoder::encode_sequence_pyramid`]: every coded frame's |
| @@ -1197,6 +1224,7 @@ impl Av1Encoder { | |||
| 1197 | pending: Vec::new(), | 1224 | pending: Vec::new(), |
| 1198 | held: std::collections::VecDeque::new(), | 1225 | held: std::collections::VecDeque::new(), |
| 1199 | anchor: 0, | 1226 | anchor: 0, |
| 1227 | + leaf_hist: Vec::new(), | ||
| 1200 | next_dts: 0, | 1228 | next_dts: 0, |
| 1201 | collected: None, | 1229 | collected: None, |
| 1202 | }) | 1230 | }) |
| @@ -1468,6 +1496,7 @@ impl Av1Encoder { | |||
| 1468 | self.carried_cdfs.as_ref().map(|c| &c.0), | 1496 | self.carried_cdfs.as_ref().map(|c| &c.0), |
| 1469 | self.golden.as_ref(), | 1497 | self.golden.as_ref(), |
| 1470 | self.prev2.as_ref(), | 1498 | self.prev2.as_ref(), |
| 1499 | + None, | ||
| 1471 | &self.fctx, | 1500 | &self.fctx, |
| 1472 | None, | 1501 | None, |
| 1473 | &lookahead.iter().map(|n| n.padded_to(SUPERBLOCK)).collect::<Vec<_>>(), | 1502 | &lookahead.iter().map(|n| n.padded_to(SUPERBLOCK)).collect::<Vec<_>>(), |
| @@ -1607,6 +1636,9 @@ impl Av1Encoder { | |||
| 1607 | )?; | 1636 | )?; |
| 1608 | self.refresh(&[0, 1, 2, 3, 4, 5, 6, 7], &encoded, 0); | 1637 | self.refresh(&[0, 1, 2, 3, 4, 5, 6, 7], &encoded, 0); |
| 1609 | self.anchor = 0; | 1638 | self.anchor = 0; |
| 1639 | + // Every leaf slot now holds the key frame, which `GOLDEN_FRAME` | ||
| 1640 | + // already names: the chain starts empty again. | ||
| 1641 | + self.leaf_hist.clear(); | ||
| 1610 | let cropped = crop_encoded(&encoded, render.0, render.1); | 1642 | let cropped = crop_encoded(&encoded, render.0, render.1); |
| 1611 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1643 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 1612 | rate_loop.update(Level::Key, 0, cropped.stream.len()); | 1644 | rate_loop.update(Level::Key, 0, cropped.stream.len()); |
| @@ -1626,6 +1658,10 @@ impl Av1Encoder { | |||
| 1626 | last_slot: u8, | 1658 | last_slot: u8, |
| 1627 | self_slot: u8, | 1659 | self_slot: u8, |
| 1628 | altref_slot: u8, | 1660 | altref_slot: u8, |
| 1661 | + // lane-last2: the slot read as `LAST2_FRAME`. `None` -- and any slot | ||
| 1662 | + // holding no picture, or the same one `LAST_FRAME` reads -- leaves | ||
| 1663 | + // the reference set exactly as it was before this lane. | ||
| 1664 | + last2_slot: Option<u8>, | ||
| 1629 | show_frame: bool, | 1665 | show_frame: bool, |
| 1630 | level: Level, | 1666 | level: Level, |
| 1631 | // The quantizer offset comes from the CALLER, not from `level`: a | 1667 | // The quantizer offset comes from the CALLER, not from `level`: a |
| @@ -1659,8 +1695,19 @@ impl Av1Encoder { | |||
| 1659 | .as_ref() | 1695 | .as_ref() |
| 1660 | .is_some_and(|s| s.order_hint > order_hint) | 1696 | .is_some_and(|s| s.order_hint > order_hint) |
| 1661 | }; | 1697 | }; |
| 1698 | + // A LAST2 slot only counts when it holds a picture that is not the | ||
| 1699 | + // one `LAST_FRAME` already names; otherwise the frame codes the | ||
| 1700 | + // reference set it always did. | ||
| 1701 | + let last2_slot = last2_slot | ||
| 1702 | + .filter(|&s| s != last_slot && self.dpb[s as usize].is_some()); | ||
| 1662 | let slots = [ | 1703 | let slots = [ |
| 1663 | - last_slot, last_slot, last_slot, GOLDEN_SLOT, last_slot, last_slot, altref_slot, | 1704 | + last_slot, |
| 1705 | + last2_slot.unwrap_or(last_slot), | ||
| 1706 | + last_slot, | ||
| 1707 | + GOLDEN_SLOT, | ||
| 1708 | + last_slot, | ||
| 1709 | + last_slot, | ||
| 1710 | + altref_slot, | ||
| 1664 | ]; | 1711 | ]; |
| 1665 | for (i, slot) in slots.into_iter().enumerate() { | 1712 | for (i, slot) in slots.into_iter().enumerate() { |
| 1666 | sign_bias[i] = ahead(slot, &self.dpb); | 1713 | sign_bias[i] = ahead(slot, &self.dpb); |
| @@ -1678,6 +1725,7 @@ impl Av1Encoder { | |||
| 1678 | .ok_or_else(|| { | 1725 | .ok_or_else(|| { |
| 1679 | Error::unsupported("AV1 encode", "an inter frame needs a previous reconstruction") | 1726 | Error::unsupported("AV1 encode", "an inter frame needs a previous reconstruction") |
| 1680 | })?; | 1727 | })?; |
| 1728 | + let last2 = last2_slot.and_then(|s| self.dpb[s as usize].as_ref().map(|d| d.picture.clone())); | ||
| 1681 | let golden = self.dpb[GOLDEN_SLOT as usize].as_ref().map(|s| s.picture.clone()); | 1729 | let golden = self.dpb[GOLDEN_SLOT as usize].as_ref().map(|s| s.picture.clone()); |
| 1682 | let altref = (altref_slot != GOLDEN_SLOT) | 1730 | let altref = (altref_slot != GOLDEN_SLOT) |
| 1683 | .then(|| self.dpb[altref_slot as usize].as_ref().map(|s| s.picture.clone())) | 1731 | .then(|| self.dpb[altref_slot as usize].as_ref().map(|s| s.picture.clone())) |
| @@ -1694,11 +1742,13 @@ impl Av1Encoder { | |||
| 1694 | start_cdfs.as_ref(), | 1742 | start_cdfs.as_ref(), |
| 1695 | golden.as_ref(), | 1743 | golden.as_ref(), |
| 1696 | altref.as_ref(), | 1744 | altref.as_ref(), |
| 1745 | + last2.as_ref(), | ||
| 1697 | &self.fctx, | 1746 | &self.fctx, |
| 1698 | Some(crate::encode::PyramidFrame { | 1747 | Some(crate::encode::PyramidFrame { |
| 1699 | last_slot, | 1748 | last_slot, |
| 1700 | self_slot, | 1749 | self_slot, |
| 1701 | altref_slot, | 1750 | altref_slot, |
| 1751 | + last2_slot, | ||
| 1702 | show_frame, | 1752 | show_frame, |
| 1703 | sign_bias, | 1753 | sign_bias, |
| 1704 | }), | 1754 | }), |
| @@ -1715,7 +1765,8 @@ impl Av1Encoder { | |||
| 1715 | "AV1 encode", | 1765 | "AV1 encode", |
| 1716 | format!( | 1766 | format!( |
| 1717 | "{level:?} frame at order {order} (last slot {last_slot}, self {self_slot}, \ | 1767 | "{level:?} frame at order {order} (last slot {last_slot}, self {self_slot}, \ |
| 1718 | - altref {altref_slot}, shown {show_frame}, golden {}, altref pic {}): {e}", | 1768 | + altref {altref_slot}, last2 {last2_slot:?}, shown {show_frame}, golden {}, \ |
| 1769 | + altref pic {}): {e}", | ||
| 1719 | golden.is_some(), | 1770 | golden.is_some(), |
| 1720 | altref.is_some(), | 1771 | altref.is_some(), |
| 1721 | ), | 1772 | ), |
| @@ -1758,6 +1809,13 @@ impl Av1Encoder { | |||
| 1758 | }; | 1809 | }; |
| 1759 | window.into_iter().take(depth - 1).map(|p| p.padded_to(SUPERBLOCK)).collect() | 1810 | window.into_iter().take(depth - 1).map(|p| p.padded_to(SUPERBLOCK)).collect() |
| 1760 | }; | 1811 | }; |
| 1812 | + // lane-last2: the slot a frame reading `last_slot` as `LAST_FRAME` | ||
| 1813 | + // names as `LAST2_FRAME` -- the most recent shown leaf that is not | ||
| 1814 | + // the one it already reads. `None` with the lever off, or before two | ||
| 1815 | + // distinct past pictures exist. | ||
| 1816 | + let last2_of = |hist: &[u8], last_slot: u8| -> Option<u8> { | ||
| 1817 | + last2().then(|| hist.iter().copied().find(|&s| s != last_slot)).flatten() | ||
| 1818 | + }; | ||
| 1761 | let anchor_slot = ANCHOR_SLOTS[self.anchor]; | 1819 | let anchor_slot = ANCHOR_SLOTS[self.anchor]; |
| 1762 | let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; | 1820 | let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; |
| 1763 | let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); | 1821 | let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); |
| @@ -1771,6 +1829,7 @@ impl Av1Encoder { | |||
| 1771 | anchor_slot, | 1829 | anchor_slot, |
| 1772 | next_anchor_slot, | 1830 | next_anchor_slot, |
| 1773 | GOLDEN_SLOT, | 1831 | GOLDEN_SLOT, |
| 1832 | + last2_of(&self.leaf_hist, anchor_slot), | ||
| 1774 | true, | 1833 | true, |
| 1775 | Level::Leaf, | 1834 | Level::Leaf, |
| 1776 | pyramid.leaf_q_offset, | 1835 | pyramid.leaf_q_offset, |
| @@ -1793,6 +1852,10 @@ impl Av1Encoder { | |||
| 1793 | true => next_anchor_slot, | 1852 | true => next_anchor_slot, |
| 1794 | false => GOLDEN_SLOT, | 1853 | false => GOLDEN_SLOT, |
| 1795 | }, | 1854 | }, |
| 1855 | + // The ARF levels take no LAST2: the anchor pair already gives a | ||
| 1856 | + // top ARF two distinct past anchors (`arf_altref`), and the mid | ||
| 1857 | + // and quarter frames have only the one past anchor in the DPB. | ||
| 1858 | + None, | ||
| 1796 | false, | 1859 | false, |
| 1797 | Level::Arf, | 1860 | Level::Arf, |
| 1798 | pyramid.arf_q_offset, | 1861 | pyramid.arf_q_offset, |
| @@ -1826,6 +1889,7 @@ impl Av1Encoder { | |||
| 1826 | anchor_slot, | 1889 | anchor_slot, |
| 1827 | MID_SLOT, | 1890 | MID_SLOT, |
| 1828 | next_anchor_slot, | 1891 | next_anchor_slot, |
| 1892 | + None, | ||
| 1829 | false, | 1893 | false, |
| 1830 | Level::Arf, | 1894 | Level::Arf, |
| 1831 | offset, | 1895 | offset, |
| @@ -1862,6 +1926,7 @@ impl Av1Encoder { | |||
| 1862 | last_slot, | 1926 | last_slot, |
| 1863 | QUARTER_SLOTS[i], | 1927 | QUARTER_SLOTS[i], |
| 1864 | altref_slot, | 1928 | altref_slot, |
| 1929 | + None, | ||
| 1865 | false, | 1930 | false, |
| 1866 | Level::Arf, | 1931 | Level::Arf, |
| 1867 | offset, | 1932 | offset, |
| @@ -1902,23 +1967,42 @@ impl Av1Encoder { | |||
| 1902 | packets.push(packet); | 1967 | packets.push(packet); |
| 1903 | continue; | 1968 | continue; |
| 1904 | } | 1969 | } |
| 1970 | + // The previous shown leaf's own slot: `LEAF_SLOT` with the | ||
| 1971 | + // lever off, and with it on whichever of [`LEAF_SLOTS`] that leaf | ||
| 1972 | + // refreshed. | ||
| 1973 | + let prev_leaf = self.leaf_hist.first().copied().unwrap_or(LEAF_SLOT); | ||
| 1905 | let last_slot = match i { | 1974 | let last_slot = match i { |
| 1906 | 0 => anchor_slot, | 1975 | 0 => anchor_slot, |
| 1907 | i => inner | 1976 | i => inner |
| 1908 | .iter() | 1977 | .iter() |
| 1909 | .find(|(at, _)| at + 1 == i) | 1978 | .find(|(at, _)| at + 1 == i) |
| 1910 | - .map_or(LEAF_SLOT, |&(_, slot)| slot), | 1979 | + .map_or(prev_leaf, |&(_, slot)| slot), |
| 1911 | }; | 1980 | }; |
| 1981 | + // Alternate the refresh: this leaf takes the leaf slot the | ||
| 1982 | + // PREVIOUS leaf did not, which is the one the leaf before that | ||
| 1983 | + // wrote and which this frame may still read as `LAST2_FRAME` | ||
| 1984 | + // (read before write). | ||
| 1985 | + let self_slot = match last2() { | ||
| 1986 | + true => LEAF_SLOTS | ||
| 1987 | + .into_iter() | ||
| 1988 | + .find(|&s| Some(s) != self.leaf_hist.first().copied()) | ||
| 1989 | + .unwrap_or(LEAF_SLOT), | ||
| 1990 | + false => LEAF_SLOT, | ||
| 1991 | + }; | ||
| 1992 | + let last2_slot = last2_of(&self.leaf_hist, last_slot); | ||
| 1912 | let altref_slot = inner | 1993 | let altref_slot = inner |
| 1913 | .iter() | 1994 | .iter() |
| 1914 | .find(|(at, _)| *at > i) | 1995 | .find(|(at, _)| *at > i) |
| 1915 | .map_or(next_anchor_slot, |&(_, slot)| slot); | 1996 | .map_or(next_anchor_slot, |&(_, slot)| slot); |
| 1997 | + self.leaf_hist.insert(0, self_slot); | ||
| 1998 | + self.leaf_hist.truncate(2); | ||
| 1916 | packets.push(self.encode_pyramid_inter( | 1999 | packets.push(self.encode_pyramid_inter( |
| 1917 | *order, | 2000 | *order, |
| 1918 | picture, | 2001 | picture, |
| 1919 | last_slot, | 2002 | last_slot, |
| 1920 | - LEAF_SLOT, | 2003 | + self_slot, |
| 1921 | altref_slot, | 2004 | altref_slot, |
| 2005 | + last2_slot, | ||
| 1922 | true, | 2006 | true, |
| 1923 | Level::Leaf, | 2007 | Level::Leaf, |
| 1924 | pyramid.leaf_q_offset, | 2008 | pyramid.leaf_q_offset, |
lanes/last2.report.mdadded+47 -0
| @@ -0,0 +1,47 @@ | |||
| 1 | +# lane-last2 — a second past reference (LAST2) on the pyramid | ||
| 2 | + | ||
| 3 | +Worktree `edith_codecs-last2`, branch `lane-last2` off main e6df5196. | ||
| 4 | +Charter: `lanes/refs.report.md` §6. Every gate arm is the prebuilt release | ||
| 5 | +lib-test binary running `bd_rate_screen_native` (12 pictures, `gop 12`, four | ||
| 6 | +quantizers, native `gate_crop` window), one film per arm | ||
| 7 | +(`EC_AV1_NATIVE_FILM` / `EC_AV1_NATIVE_FILM4K` / `EC_AV1_NATIVE_SCREEN`); every | ||
| 8 | +row is read off that log's own header line. | ||
| 9 | + | ||
| 10 | +## 0. The charter's ARF step is a stale premise (read this first) | ||
| 11 | + | ||
| 12 | +The charter orders "ARF level first". At the ARF level there is nothing to | ||
| 13 | +build: a group's top ARF ALREADY names two distinct past anchors -- | ||
| 14 | +`last_slot` = `ANCHOR_SLOTS[anchor]` (the previous group's top ARF, lag | ||
| 15 | +`mini_gop`) and, since lane-arfcen's `arf_altref` shipped ON, | ||
| 16 | +`altref_slot` = `ANCHOR_SLOTS[1 - anchor]`, which still holds the top ARF from | ||
| 17 | +TWO groups back (lag 2 * `mini_gop`) when the frame is coded. Those are | ||
| 18 | +exactly the census's (4, 8) ARF lags, and that reference is offered to the | ||
| 19 | +32x32 search through the `extra` path WITH its own `NEWMV` search | ||
| 20 | +(`EXTRA_REF_NEW_MV` = true, margin `EXTRA_NEW_SKIP_MARGIN` = 0.35). So the | ||
| 21 | +census's ARF row (film A 10.31%, film B 8.38% of prediction SAD) is a lever | ||
| 22 | +already banked by `arf_altref`, not an open one; naming the same slot again as | ||
| 23 | +`LAST2_FRAME` would offer the same picture twice. | ||
| 24 | + | ||
| 25 | +The open half of the census is the LEAF chain (film A 6.99%, film B 7.25%): | ||
| 26 | +a leaf has exactly one past picture in the DPB (`LEAF_SLOT`, or the mid / | ||
| 27 | +quarter hidden frame behind it) plus the key as `GOLDEN`. That is what this | ||
| 28 | +lane builds. | ||
| 29 | + | ||
| 30 | +## 1. The slot map | ||
| 31 | + | ||
| 32 | +| slot | holds | written by | read as | | ||
| 33 | +|---|---|---|---| | ||
| 34 | +| 0, 7 (`LEAF_SLOTS`) | the last two shown leaves | every leaf, alternating | `LAST_FRAME` and `LAST2_FRAME` of the next leaf | | ||
| 35 | +| 1 (`GOLDEN_SLOT`) | the key frame | the key | `GOLDEN_FRAME` everywhere | | ||
| 36 | +| 2 (`MID_SLOT`) | the mid ARF | the mid ARF | `LAST`/`ALTREF` of the leaves and quarters around it | | ||
| 37 | +| 3, 4 (`ANCHOR_SLOTS`) | this group's anchor and the next | the key, each top ARF | anchor = `LAST`, next = `ALTREF` | | ||
| 38 | +| 5, 6 (`QUARTER_SLOTS`) | the two quarter ARFs (off by default) | the quarter ARFs | `LAST`/`ALTREF` of the leaves beside them | | ||
| 39 | + | ||
| 40 | +Leaf `k` refreshes `LEAF_SLOTS[k % 2]`, i.e. the slot leaf `k - 2` wrote -- | ||
| 41 | +read before write. `Av1Encoder::leaf_hist` carries the last two leaf slots | ||
| 42 | +ACROSS mini-GOP boundaries (the first leaf of a group reads the anchor as | ||
| 43 | +`LAST`, so its `LAST2` is the previous group's last leaf) and is cleared by | ||
| 44 | +every key frame (which writes all eight slots with one picture `GOLDEN` | ||
| 45 | +already names). A leaf whose `LAST` is a hidden frame reads the previous leaf | ||
| 46 | +as `LAST2`. The hidden levels pass `None`: see §0. | ||
| 47 | + | ||