Merge branch 'lane-arfcen' (f550d2e1): the temporal lambda map runs on the pyramid path; a top ARF names the ARF two groups back as its second reference
Browse files at this commit6 changed files +363 -14
- crates/ec-av1/examples/syntax_census.rs +35 -2
- crates/ec-av1/src/census.rs +7 -0
- crates/ec-av1/src/encode.rs +39 -2
- crates/ec-av1/src/encoder.rs +76 -5
- crates/ec-av1/src/motion.rs +22 -5
- lanes/arfcen.report.md +184 -0
crates/ec-av1/examples/syntax_census.rsmodified+35 -2
| @@ -116,6 +116,9 @@ fn pct(n: u64, total: u64) -> String { | |||
| 116 | format!("{:.1}%", 100.0 * n as f64 / total.max(1) as f64) | 116 | format!("{:.1}%", 100.0 * n as f64 / total.max(1) as f64) |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | +const REFS: [&str; 8] = | ||
| 120 | + ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; | ||
| 121 | + | ||
| 119 | fn report(label: &str, f: &Frame, frames: usize) { | 122 | fn report(label: &str, f: &Frame, frames: usize) { |
| 120 | let area = f.area.iter().sum::<u64>().max(1); | 123 | let area = f.area.iter().sum::<u64>().max(1); |
| 121 | println!("\n== {label} ({frames} frames, {} bytes) ==", f.bytes); | 124 | println!("\n== {label} ({frames} frames, {} bytes) ==", f.bytes); |
| @@ -126,8 +129,6 @@ fn report(label: &str, f: &Frame, frames: usize) { | |||
| 126 | pct(f.area[2], area), | 129 | pct(f.area[2], area), |
| 127 | pct(f.skip_area, area), | 130 | pct(f.skip_area, area), |
| 128 | ); | 131 | ); |
| 129 | - const REFS: [&str; 8] = | ||
| 130 | - ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; | ||
| 131 | println!( | 132 | println!( |
| 132 | " refs (area): {}", | 133 | " refs (area): {}", |
| 133 | REFS.iter() | 134 | REFS.iter() |
| @@ -252,6 +253,7 @@ fn main() { | |||
| 252 | // the frames above are coded in (a pyramid codes a hidden ARF ahead of the | 253 | // the frames above are coded in (a pyramid codes a hidden ARF ahead of the |
| 253 | // leaves that predict from it, and re-outputs it later with | 254 | // leaves that predict from it, and re-outputs it later with |
| 254 | // `show_existing_frame`) -- so it is its own table rather than a column. | 255 | // `show_existing_frame`) -- so it is its own table rather than a column. |
| 256 | + let mut psnrs: Vec<f64> = Vec::new(); | ||
| 255 | if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { | 257 | if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { |
| 256 | let mut sum = 0.0; | 258 | let mut sum = 0.0; |
| 257 | let mut rows = Vec::new(); | 259 | let mut rows = Vec::new(); |
| @@ -261,6 +263,7 @@ fn main() { | |||
| 261 | let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; | 263 | let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; |
| 262 | let want: Vec<u16> = b[..luma].iter().map(|&v| u16::from(v)).collect(); | 264 | let want: Vec<u16> = b[..luma].iter().map(|&v| u16::from(v)).collect(); |
| 263 | let p = psnr(&pic.y, &want); | 265 | let p = psnr(&pic.y, &want); |
| 266 | + psnrs.push(p); | ||
| 264 | sum += p; | 267 | sum += p; |
| 265 | rows.push(format!("{i}:{p:.2}")); | 268 | rows.push(format!("{i}:{p:.2}")); |
| 266 | } | 269 | } |
| @@ -273,6 +276,36 @@ fn main() { | |||
| 273 | ); | 276 | ); |
| 274 | } | 277 | } |
| 275 | 278 | ||
| 279 | + // lane-arfcen: one full report PER CODED FRAME, so an ARF of ours can be | ||
| 280 | + // put next to the reference encoder's ARF at the same display position. | ||
| 281 | + if std::env::var("EC_CENSUS_PERFRAME").as_deref() == Ok("1") { | ||
| 282 | + for f in &frames { | ||
| 283 | + let p = psnrs.get(f.order_hint as usize).copied().unwrap_or(f64::NAN); | ||
| 284 | + let refs: Vec<String> = (0..7) | ||
| 285 | + .filter(|i| f.refs[i + 1] > 0) | ||
| 286 | + .map(|i| { | ||
| 287 | + format!( | ||
| 288 | + "{}@{:+}", | ||
| 289 | + REFS[i + 1], | ||
| 290 | + i64::from(f.ref_hints[i]) - i64::from(f.order_hint), | ||
| 291 | + ) | ||
| 292 | + }) | ||
| 293 | + .collect(); | ||
| 294 | + report( | ||
| 295 | + &format!( | ||
| 296 | + "frame {} {} hint {} q {} PSNR-Y {p:.2} dB refs [{}]", | ||
| 297 | + f.idx, | ||
| 298 | + f.kind, | ||
| 299 | + f.order_hint, | ||
| 300 | + f.qindex, | ||
| 301 | + refs.join(" "), | ||
| 302 | + ), | ||
| 303 | + f, | ||
| 304 | + 1, | ||
| 305 | + ); | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | + | ||
| 276 | for kind in ["key", "arf", "leaf"] { | 309 | for kind in ["key", "arf", "leaf"] { |
| 277 | let set: Vec<&Frame> = frames.iter().filter(|f| f.kind == kind).collect(); | 310 | let set: Vec<&Frame> = frames.iter().filter(|f| f.kind == kind).collect(); |
| 278 | if !set.is_empty() { | 311 | if !set.is_empty() { |
crates/ec-av1/src/census.rsmodified+7 -0
| @@ -50,6 +50,11 @@ pub struct Frame { | |||
| 50 | pub kind: &'static str, | 50 | pub kind: &'static str, |
| 51 | /// `base_q_idx`. | 51 | /// `base_q_idx`. |
| 52 | pub qindex: u8, | 52 | pub qindex: u8, |
| 53 | + /// `OrderHint` of this frame, i.e. its display position (lane-arfcen). | ||
| 54 | + pub order_hint: u32, | ||
| 55 | + /// `OrderHints[LAST..=ALTREF]`, so a row can name its references' display | ||
| 56 | + /// distance rather than only which slot it read (lane-arfcen). | ||
| 57 | + pub ref_hints: [u32; 7], | ||
| 53 | /// Tile payload bytes of this frame. | 58 | /// Tile payload bytes of this frame. |
| 54 | pub bytes: usize, | 59 | pub bytes: usize, |
| 55 | /// `delta_q_present`. | 60 | /// `delta_q_present`. |
| @@ -132,6 +137,8 @@ pub(crate) fn frame_start(header: &ec_av1_syntax::FrameHeader, bytes: usize, idx | |||
| 132 | idx, | 137 | idx, |
| 133 | kind, | 138 | kind, |
| 134 | qindex: header.quantization.base_q_idx, | 139 | qindex: header.quantization.base_q_idx, |
| 140 | + order_hint: header.order_hint, | ||
| 141 | + ref_hints: header.order_hints, | ||
| 135 | bytes, | 142 | bytes, |
| 136 | delta_q: header.delta.q_present, | 143 | delta_q: header.delta.q_present, |
| 137 | segmentation: header.segmentation.enabled, | 144 | segmentation: header.segmentation.enabled, |
crates/ec-av1/src/encode.rsmodified+39 -2
| @@ -137,6 +137,26 @@ fn reach_sb_px(fctx: &crate::decode::FrameCtx) -> usize { | |||
| 137 | /// defaults below are on: +50.1 / -15.4). It spends ~5% more encode wall. | 137 | /// defaults below are on: +50.1 / -15.4). It spends ~5% more encode wall. |
| 138 | const LAMBDA_SCALE: f64 = 0.0275; | 138 | const LAMBDA_SCALE: f64 = 0.0275; |
| 139 | 139 | ||
| 140 | +/// The rate weight a HIDDEN frame (a pyramid ARF, coded but not shown) codes | ||
| 141 | +/// under, as a multiple of [`LAMBDA_SCALE`] (lane-arfcen). | ||
| 142 | +/// | ||
| 143 | +/// The ARF-vs-ARF census says our top ARF codes residual over HALF its area | ||
| 144 | +/// (rav1e's, at the same display position, over 26-30%) and comes out 1.4-2.5x | ||
| 145 | +/// its bytes at the same PSNR, while every leaf that predicts from it is | ||
| 146 | +/// still 0.4 dB behind rav1e's -- i.e. the ARF's extra fidelity is bought at | ||
| 147 | +/// a rate weight fitted on shown frames and does not propagate. This is the | ||
| 148 | +/// one knob that prices a hidden frame's rate differently from its q, which | ||
| 149 | +/// the pyramid's q offsets cannot do (they move lambda and step together). | ||
| 150 | +const HIDDEN_LAMBDA_FACTOR: f64 = 1.0; | ||
| 151 | + | ||
| 152 | +/// [`HIDDEN_LAMBDA_FACTOR`], swept by `EC_AV1_LAMBDA_HIDDEN` in any build. | ||
| 153 | +fn hidden_lambda_factor() -> f64 { | ||
| 154 | + match std::env::var("EC_AV1_LAMBDA_HIDDEN").ok().and_then(|v| v.parse().ok()) { | ||
| 155 | + Some(f) => f, | ||
| 156 | + None => HIDDEN_LAMBDA_FACTOR, | ||
| 157 | + } | ||
| 158 | +} | ||
| 159 | + | ||
| 140 | /// [`LAMBDA_SCALE`], or whatever `EC_AV1_LAMBDA` names when the sweep that | 160 | /// [`LAMBDA_SCALE`], or whatever `EC_AV1_LAMBDA` names when the sweep that |
| 141 | /// picks it is the thing running. A release build has no such knob. | 161 | /// picks it is the thing running. A release build has no such knob. |
| 142 | fn lambda_scale() -> f64 { | 162 | fn lambda_scale() -> f64 { |
| @@ -9040,7 +9060,13 @@ pub(crate) fn encode_inter_frame( | |||
| 9040 | let search = Search { | 9060 | let search = Search { |
| 9041 | base_q_idx, | 9061 | base_q_idx, |
| 9042 | deadzone, | 9062 | deadzone, |
| 9043 | - lambda: lambda_scale() * step * step, | 9063 | + lambda: lambda_scale() |
| 9064 | + * match pyramid { | ||
| 9065 | + Some(p) if !p.show_frame => hidden_lambda_factor(), | ||
| 9066 | + _ => 1.0, | ||
| 9067 | + } | ||
| 9068 | + * step | ||
| 9069 | + * step, | ||
| 9044 | modes: &KEY_FRAME_MODES, | 9070 | modes: &KEY_FRAME_MODES, |
| 9045 | top_k: prune_top_k_inter(), | 9071 | top_k: prune_top_k_inter(), |
| 9046 | screen, | 9072 | screen, |
| @@ -13604,8 +13630,19 @@ mod tests { | |||
| 13604 | // base now, not 16 ([`crate::encoder::Pyramid::leaf_q_offset`]) -- 9778 | 13630 | // base now, not 16 ([`crate::encoder::Pyramid::leaf_q_offset`]) -- 9778 |
| 13605 | // -> 9835 at q=150 and 35450 -> 35798 at q=60. | 13631 | // -> 9835 at q=150 and 35450 -> 35798 at q=60. |
| 13606 | // `EC_AV1_PYRAMID=8:-32:16:-8:-48` restores these. | 13632 | // `EC_AV1_PYRAMID=8:-32:16:-8:-48` restores these. |
| 13633 | + // Re-taken on lane-arfcen: the pyramid path hands every frame the | ||
| 13634 | + // lookahead window its temporal lambda map needs | ||
| 13635 | + // (`crate::encoder::tpl_pyramid`), which it never had, so every | ||
| 13636 | + // superblock of these four pictures is priced at its own lambda -- | ||
| 13637 | + // 9835 -> 9859 at q=150 and 35798 -> 35904 at q=60. | ||
| 13638 | + // `EC_AV1_TPL_PYRAMID=0` restores these. | ||
| 13639 | + // Re-taken again on lane-arfcen: a group's top ARF names the ARF from | ||
| 13640 | + // two groups back as its own `ALTREF_FRAME` | ||
| 13641 | + // (`crate::encoder::arf_altref`) instead of the key frame it already | ||
| 13642 | + // reads as `GOLDEN` -- 9859 -> 9853 at q=150 and 35904 -> 35866 at | ||
| 13643 | + // q=60. `EC_AV1_ARF_ALTREF=0` restores these. | ||
| 13607 | let pins: [(u8, usize, u64); 2] = | 13644 | let pins: [(u8, usize, u64); 2] = |
| 13608 | - [(150, 9835, 0xfdac_0219_4a6e_111e), (60, 35798, 0xe57e_aa3f_8b48_d206)]; | 13645 | + [(150, 9853, 0x0f6d_bbc8_b72c_ceb3), (60, 35866, 0xbd00_f4ed_e283_3ce4)]; |
| 13609 | for (q, bytes, hash) in pins { | 13646 | for (q, bytes, hash) in pins { |
| 13610 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 13647 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 13611 | assert_eq!( | 13648 | assert_eq!( |
crates/ec-av1/src/encoder.rsmodified+76 -5
| @@ -1016,6 +1016,44 @@ const MID_DEFAULT: Option<i16> = Some(-8); | |||
| 1016 | /// is gated off). | 1016 | /// is gated off). |
| 1017 | const KEY_DEFAULT: i16 = -48; | 1017 | const KEY_DEFAULT: i16 = -48; |
| 1018 | 1018 | ||
| 1019 | +/// Whether the pyramid path hands each frame a lookahead window, i.e. | ||
| 1020 | +/// whether the temporal lambda map (`encode::tpl_lambda_factors`) runs on | ||
| 1021 | +/// the DEFAULT stream at all. It did not until lane-arfcen: the pyramid | ||
| 1022 | +/// reorders pictures, so this path passed `&[]` and the map -- measured and | ||
| 1023 | +/// shipped on the flat path -- was inert on every frame of every pyramid | ||
| 1024 | +/// stream, including the ARFs that carry 54% of a long-GOP stream's bytes | ||
| 1025 | +/// (class `tool disabled in every gate recipe`). | ||
| 1026 | +fn tpl_pyramid() -> bool { | ||
| 1027 | + match std::env::var("EC_AV1_TPL_PYRAMID").ok() { | ||
| 1028 | + Some(v) => v != "0", | ||
| 1029 | + None => TPL_PYRAMID, | ||
| 1030 | + } | ||
| 1031 | +} | ||
| 1032 | + | ||
| 1033 | +/// [`tpl_pyramid`]'s default. | ||
| 1034 | +const TPL_PYRAMID: bool = true; | ||
| 1035 | + | ||
| 1036 | +/// Whether a group's TOP ARF names the ARF from TWO groups back as its own | ||
| 1037 | +/// `ALTREF_FRAME` (lane-arfcen). It has always named `GOLDEN_SLOT` there, | ||
| 1038 | +/// which for a top ARF is the key frame it already reads as `GOLDEN`, i.e. | ||
| 1039 | +/// its second reference is the same picture as its first for the first group | ||
| 1040 | +/// and adds nothing after: the ARF-vs-ARF census reads | ||
| 1041 | +/// `LAST@-8 GOLDEN@-8` on our first ARF where rav1e's reads two DISTINCT | ||
| 1042 | +/// anchors. The slot the ARF is about to refresh still holds that older | ||
| 1043 | +/// anchor while this frame is coded, so naming it costs no extra DPB slot. | ||
| 1044 | +fn arf_altref() -> bool { | ||
| 1045 | + match std::env::var("EC_AV1_ARF_ALTREF").ok() { | ||
| 1046 | + Some(v) => v != "0", | ||
| 1047 | + None => ARF_ALTREF, | ||
| 1048 | + } | ||
| 1049 | +} | ||
| 1050 | + | ||
| 1051 | +/// [`arf_altref`]'s default. ON since its long-GOP arm: film A | ||
| 1052 | +/// +42.8%/+4.5% -> +40.9%/+3.1% and film B +127.3%/+32.9% -> +125.6%/+32.0% | ||
| 1053 | +/// (BD vs libaom cpu-used 6 / vs rav1e speed 6), i.e. both films down on both | ||
| 1054 | +/// columns. | ||
| 1055 | +const ARF_ALTREF: bool = true; | ||
| 1056 | + | ||
| 1019 | /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per | 1057 | /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per |
| 1020 | /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a | 1058 | /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a |
| 1021 | /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a | 1059 | /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a |
| @@ -1595,6 +1633,10 @@ impl Av1Encoder { | |||
| 1595 | // ARF and the mid one) at different offsets, and they share the | 1633 | // ARF and the mid one) at different offsets, and they share the |
| 1596 | // rate-control slot but not the offset. | 1634 | // rate-control slot but not the offset. |
| 1597 | offset: i16, | 1635 | offset: i16, |
| 1636 | + // The lookahead window this frame's temporal lambda map is built | ||
| 1637 | + // from, already padded (lane-arfcen). Empty codes the frame at one | ||
| 1638 | + // lambda, which is what the whole pyramid path used to do. | ||
| 1639 | + lookahead: &[Picture], | ||
| 1598 | ) -> Result<Packet> { | 1640 | ) -> Result<Packet> { |
| 1599 | let render = (self.config.width, self.config.height); | 1641 | let render = (self.config.width, self.config.height); |
| 1600 | let order_hint = (order & 0x7f) as u32; | 1642 | let order_hint = (order & 0x7f) as u32; |
| @@ -1660,10 +1702,10 @@ impl Av1Encoder { | |||
| 1660 | show_frame, | 1702 | show_frame, |
| 1661 | sign_bias, | 1703 | sign_bias, |
| 1662 | }), | 1704 | }), |
| 1663 | - // The pyramid path reorders pictures, so its own buffer is not | 1705 | + // lane-arfcen: the group's own sources ARE this frame's window |
| 1664 | - // a lookahead window: the temporal lambda weighting is off on | 1706 | + // (`tpl_window`); it was `&[]` here, which switched the temporal |
| 1665 | - // this path (lane-av1tpl). | 1707 | + // lambda map off on every frame the default stream codes. |
| 1666 | - &[], | 1708 | + lookahead, |
| 1667 | ) }(); | 1709 | ) }(); |
| 1668 | // Name the pyramid position in any failure: an error out of the tile | 1710 | // Name the pyramid position in any failure: an error out of the tile |
| 1669 | // writer or the filter search is otherwise indistinguishable between | 1711 | // writer or the filter search is otherwise indistinguishable between |
| @@ -1699,6 +1741,23 @@ impl Av1Encoder { | |||
| 1699 | } | 1741 | } |
| 1700 | let group: Vec<(u64, Picture)> = std::mem::take(&mut self.pending); | 1742 | let group: Vec<(u64, Picture)> = std::mem::take(&mut self.pending); |
| 1701 | let mut packets = Vec::with_capacity(group.len() + 1); | 1743 | let mut packets = Vec::with_capacity(group.len() + 1); |
| 1744 | + // The lookahead window of the group's picture at index `pos`: the | ||
| 1745 | + // display-order successors inside this group, or -- for the group's | ||
| 1746 | + // top ARF, the last picture, whose successors are not buffered yet -- | ||
| 1747 | + // the leaves BEHIND it in reverse display order, which are exactly | ||
| 1748 | + // the pictures that predict backward from it. Padded like the frame | ||
| 1749 | + // itself, as `encode_inter_frame` wants it. | ||
| 1750 | + let tpl_window = |pos: usize, back: bool| -> Vec<Picture> { | ||
| 1751 | + let depth = crate::encode::tpl_depth(); | ||
| 1752 | + if !tpl_pyramid() || depth < 2 { | ||
| 1753 | + return Vec::new(); | ||
| 1754 | + } | ||
| 1755 | + let window: Vec<&Picture> = match back { | ||
| 1756 | + false => group[pos + 1..].iter().map(|(_, p)| p).collect(), | ||
| 1757 | + true => group[..pos].iter().rev().map(|(_, p)| p).collect(), | ||
| 1758 | + }; | ||
| 1759 | + window.into_iter().take(depth - 1).map(|p| p.padded_to(SUPERBLOCK)).collect() | ||
| 1760 | + }; | ||
| 1702 | let anchor_slot = ANCHOR_SLOTS[self.anchor]; | 1761 | let anchor_slot = ANCHOR_SLOTS[self.anchor]; |
| 1703 | let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; | 1762 | let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; |
| 1704 | let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); | 1763 | let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); |
| @@ -1715,6 +1774,7 @@ impl Av1Encoder { | |||
| 1715 | true, | 1774 | true, |
| 1716 | Level::Leaf, | 1775 | Level::Leaf, |
| 1717 | pyramid.leaf_q_offset, | 1776 | pyramid.leaf_q_offset, |
| 1777 | + &tpl_window(0, false), | ||
| 1718 | )?); | 1778 | )?); |
| 1719 | self.anchor = 1 - self.anchor; | 1779 | self.anchor = 1 - self.anchor; |
| 1720 | return Ok(packets); | 1780 | return Ok(packets); |
| @@ -1725,10 +1785,18 @@ impl Av1Encoder { | |||
| 1725 | &arf_picture, | 1785 | &arf_picture, |
| 1726 | anchor_slot, | 1786 | anchor_slot, |
| 1727 | next_anchor_slot, | 1787 | next_anchor_slot, |
| 1728 | - GOLDEN_SLOT, | 1788 | + // lane-arfcen: the slot this ARF refreshes still holds the ARF |
| 1789 | + // from two groups back, so naming it here gives the frame a | ||
| 1790 | + // SECOND, distinct past anchor instead of the key it already | ||
| 1791 | + // reads as GOLDEN. | ||
| 1792 | + match arf_altref() { | ||
| 1793 | + true => next_anchor_slot, | ||
| 1794 | + false => GOLDEN_SLOT, | ||
| 1795 | + }, | ||
| 1729 | false, | 1796 | false, |
| 1730 | Level::Arf, | 1797 | Level::Arf, |
| 1731 | pyramid.arf_q_offset, | 1798 | pyramid.arf_q_offset, |
| 1799 | + &tpl_window(group.len() - 1, true), | ||
| 1732 | )?); | 1800 | )?); |
| 1733 | let leaves = group.len() - 1; | 1801 | let leaves = group.len() - 1; |
| 1734 | // THE THIRD LEVEL: a second hidden frame at the middle of the leaf | 1802 | // THE THIRD LEVEL: a second hidden frame at the middle of the leaf |
| @@ -1761,6 +1829,7 @@ impl Av1Encoder { | |||
| 1761 | false, | 1829 | false, |
| 1762 | Level::Arf, | 1830 | Level::Arf, |
| 1763 | offset, | 1831 | offset, |
| 1832 | + &tpl_window(at, false), | ||
| 1764 | )?); | 1833 | )?); |
| 1765 | } | 1834 | } |
| 1766 | // THE FOURTH LEVEL: one hidden frame at the middle of each half of | 1835 | // THE FOURTH LEVEL: one hidden frame at the middle of each half of |
| @@ -1796,6 +1865,7 @@ impl Av1Encoder { | |||
| 1796 | false, | 1865 | false, |
| 1797 | Level::Arf, | 1866 | Level::Arf, |
| 1798 | offset, | 1867 | offset, |
| 1868 | + &tpl_window(pos, false), | ||
| 1799 | )?); | 1869 | )?); |
| 1800 | } | 1870 | } |
| 1801 | } | 1871 | } |
| @@ -1852,6 +1922,7 @@ impl Av1Encoder { | |||
| 1852 | true, | 1922 | true, |
| 1853 | Level::Leaf, | 1923 | Level::Leaf, |
| 1854 | pyramid.leaf_q_offset, | 1924 | pyramid.leaf_q_offset, |
| 1925 | + &tpl_window(i, false), | ||
| 1855 | )?); | 1926 | )?); |
| 1856 | } | 1927 | } |
| 1857 | let packet = show_existing(self, next_anchor_slot, arf_order)?; | 1928 | let packet = show_existing(self, next_anchor_slot, arf_order)?; |
crates/ec-av1/src/motion.rsmodified+22 -5
| @@ -340,17 +340,26 @@ pub(crate) fn search( | |||
| 340 | fn dist_initial_step(dist: u32) -> i32 { | 340 | fn dist_initial_step(dist: u32) -> i32 { |
| 341 | let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL }; | 341 | let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL }; |
| 342 | static K: std::sync::OnceLock<f64> = std::sync::OnceLock::new(); | 342 | static K: std::sync::OnceLock<f64> = std::sync::OnceLock::new(); |
| 343 | + // lane-arfcen: readable in ANY build, like the encoder's other search | ||
| 344 | + // margins -- the constant below was swept when the deepest reference in | ||
| 345 | + // the GOP sat at distance 4, and the arm that re-sweeps it at the | ||
| 346 | + // shipped mini-GOP of 8 runs through the release `enc_probe`. | ||
| 343 | let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() { | 347 | let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() { |
| 344 | - Some(v) if cfg!(test) => v.parse().unwrap_or(MV_DIST_SCALE), | 348 | + Some(v) => v.parse().unwrap_or(MV_DIST_SCALE), |
| 345 | - _ => MV_DIST_SCALE, | 349 | + None => MV_DIST_SCALE, |
| 346 | }); | 350 | }); |
| 347 | if dist <= 1 || k <= 0.0 { | 351 | if dist <= 1 || k <= 0.0 { |
| 348 | return base; | 352 | return base; |
| 349 | } | 353 | } |
| 350 | let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0); | 354 | let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0); |
| 351 | - // Past 4x the base the log stage spends its rounds on vectors no | 355 | + // The ceiling on that widening. `MV_DIST_CAP` x the base; swept by |
| 352 | - // reference in this encoder's GOP reaches. | 356 | + // `EC_AV1_MV_DIST_CAP`. |
| 353 | - scaled.round().clamp(f64::from(base), f64::from(base * 4)) as i32 | 357 | + static CAP: std::sync::OnceLock<i32> = std::sync::OnceLock::new(); |
| 358 | + let cap = *CAP.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_CAP").ok() { | ||
| 359 | + Some(v) => v.parse().unwrap_or(MV_DIST_CAP), | ||
| 360 | + None => MV_DIST_CAP, | ||
| 361 | + }); | ||
| 362 | + scaled.round().clamp(f64::from(base), f64::from(base * cap)) as i32 | ||
| 354 | } | 363 | } |
| 355 | 364 | ||
| 356 | /// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate | 365 | /// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate |
| @@ -362,6 +371,14 @@ fn dist_initial_step(dist: u32) -> i32 { | |||
| 362 | /// is near-tie churn, not a gradient. | 371 | /// is near-tie churn, not a gradient. |
| 363 | const MV_DIST_SCALE: f64 = 0.5; | 372 | const MV_DIST_SCALE: f64 = 0.5; |
| 364 | 373 | ||
| 374 | +/// How many times the base step [`dist_initial_step`] may widen to. Written | ||
| 375 | +/// as 4 when the deepest reference in the GOP sat at distance 4 ("past 4x the | ||
| 376 | +/// base the log stage spends its rounds on vectors no reference in this | ||
| 377 | +/// encoder's GOP reaches"), which the shipped mini-GOP of 8 falsified: the | ||
| 378 | +/// top ARF predicts at distance 8 and its own widened step (36 at | ||
| 379 | +/// [`MV_DIST_SCALE`] 0.5) was being clamped back to 32. | ||
| 380 | +const MV_DIST_CAP: i32 = 4; | ||
| 381 | + | ||
| 365 | /// [`search`], plus the running best cost after every round any of its three | 382 | /// [`search`], plus the running best cost after every round any of its three |
| 366 | /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below | 383 | /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below |
| 367 | /// checks. | 384 | /// checks. |
lanes/arfcen.report.mdadded+184 -0
| @@ -0,0 +1,184 @@ | |||
| 1 | +# lane-arfcen — why a hidden ARF of ours costs what it costs | ||
| 2 | + | ||
| 3 | +Instrument: `census::Frame` now carries `order_hint` + `ref_hints`, and | ||
| 4 | +`examples/syntax_census` prints ONE FULL REPORT PER CODED FRAME under | ||
| 5 | +`EC_CENSUS_PERFRAME=1` (display-order PSNR joined to the coded frame through | ||
| 6 | +its own order hint, references named by display DISTANCE). Both streams are | ||
| 7 | +read by the same decoder, so every number below is comparable. | ||
| 8 | + | ||
| 9 | +Arming (film B, 48 frames, the long-GOP gate's own window and seek): | ||
| 10 | + | ||
| 11 | + EC_ENC_SS=00:40:00 EC_ENC_OUT=<dir> enc_probe <film B> gate 0 48 150 | ||
| 12 | + -> 90674 B, byte-exact with bd_rate_film_long_gop's q=150 point | ||
| 13 | + ffmpeg ... -g 48 -c:v librav1e -rav1e-params speed=6:quantizer=Q:... | ||
| 14 | + EC_AV1_BITCENSUS=1 EC_CENSUS_PERFRAME=1 syntax_census <stream> source.yuv | ||
| 15 | + | ||
| 16 | +rav1e's PSNR-matched point is **quantizer 110** (45.751 dB / 75452 B), not the | ||
| 17 | +125 the charter assumed (44.990 dB / 51680 B): ours reads 45.473 dB / 90674 B. | ||
| 18 | +Interpolated to our PSNR rav1e needs ~65.6 kB, i.e. **we are +38% at matched | ||
| 19 | +quality**. | ||
| 20 | + | ||
| 21 | +## 1. Level census, same 48 display pictures | ||
| 22 | + | ||
| 23 | +| | frames | qindex | bytes | share | B/frame | PSNR-Y | | ||
| 24 | +|---|---|---|---|---|---|---| | ||
| 25 | +| ours key | 1 | 102 | 19321 | 21.6% | 19321 | 47.67 | | ||
| 26 | +| ours top ARF (dist 8) | 6 | 118 | 48126 | 53.8% | 8021 | 46.01 | | ||
| 27 | +| ours mid ARF (dist 4) | 6 | 142 | 10331 | 11.5% | 1722 | 45.79 | | ||
| 28 | +| ours leaf | 35 | 162 | 11736 | 13.1% | 335 | 45.26 | | ||
| 29 | +| rav1e key | 1 | 80 | 20763 | 28.0% | 20763 | 47.95 | | ||
| 30 | +| rav1e top ARF (dist 4) | 11 | 104 | 42590 | 57.5% | 3872 | 45.97 | | ||
| 31 | +| rav1e 2nd ARF (dist 2) | 12 | 122 | 6446 | 8.7% | 537 | 45.48 | | ||
| 32 | +| rav1e leaf | 24 | 139 | 4290 | 5.8% | 179 | 45.69 | | ||
| 33 | + | ||
| 34 | +## 2. ARF vs ARF at the SAME display position | ||
| 35 | + | ||
| 36 | +| display | ours B/q/PSNR | intra% | comp% | skip% | NEW% | rav1e B/q/PSNR | intra% | comp% | skip% | | ||
| 37 | +|---|---|---|---|---|---|---|---|---|---| | ||
| 38 | +| 8 | 8449 / 118 / 46.13 | 10.3 | 4.2 | 50.3 | 35.8 | 5945 / 104 / 45.99 | 1.8 | 0.0 | 73.7 | | ||
| 39 | +| 16 | 8889 / 118 / 46.18 | 8.8 | 6.2 | 47.7 | 34.5 | 4869 / 104 / 45.92 | 0.3 | 0.0 | 72.8 | | ||
| 40 | +| 24 | 9897 / 118 / 45.61 | 8.3 | 13.4 | 47.7 | 31.2 | 5911 / 104 / 45.28 | 0.4 | 0.0 | 73.8 | | ||
| 41 | +| 32 | 4941 / 118 / 46.62 | 1.9 | 6.6 | 51.9 | 41.5 | 2087 / 104 / 46.15 | 0.0 | 0.0 | 90.0 | | ||
| 42 | +| 40 | 7023 / 118 / 46.04 | 3.3 | 2.8 | 47.7 | 38.5 | 2854 / 104 / 45.53 | 0.1 | 0.0 | 85.4 | | ||
| 43 | +| 47/44 | 8927 / 118 / 45.50 | 4.9 | 6.9 | 48.1 | 39.1 | 3151 / 104 / 45.37 | 0.0 | 0.0 | 86.0 | | ||
| 44 | + | ||
| 45 | +Reference sets: ours `LAST@-8 GOLDEN@-8` (the first ARF names the SAME picture | ||
| 46 | +twice) then `LAST@-8 GOLDEN@-16/-24`; rav1e `LAST@-4 LAST2@-4/-8`. Coded mv | ||
| 47 | +magnitude, our top ARF: 47.1% in 2-16 px, 40.0% in 16-64 px, 0.0% over 64 px; | ||
| 48 | +rav1e's: 76.5% / 8.3% / 0.0%. | ||
| 49 | + | ||
| 50 | +## 3. Do our expensive ARFs buy cheaper leaves? No. | ||
| 51 | + | ||
| 52 | +Per display picture, the six positions our encoder codes as top ARFs against | ||
| 53 | +the same six positions in rav1e's stream, and everything else: | ||
| 54 | + | ||
| 55 | +| | our bytes | rav1e bytes | delta | | ||
| 56 | +|---|---|---|---| | ||
| 57 | +| key (1 picture) | 19321 | 20763 | -1442 | | ||
| 58 | +| our 6 ARF positions | 48126 | 24817 | **+23309** | | ||
| 59 | +| the other 41 pictures | 22067 | 28509 | -6442 | | ||
| 60 | + | ||
| 61 | +So the WHOLE +15.4 kB gap (and 6 kB more) sits in the six pictures we code at | ||
| 62 | +prediction distance 8; our non-anchor pictures are already 23% CHEAPER than | ||
| 63 | +rav1e's -- though 0.3-0.4 dB worse (leaves 45.26 vs 45.69 dB). | ||
| 64 | + | ||
| 65 | +## 4. Ranked mechanisms, and what each one measured | ||
| 66 | + | ||
| 67 | +Every arm below is `enc_probe`'s q=150 and q=90 points on film B (48 frames, | ||
| 68 | +the gate's window), read against the control's own rate/quality slope | ||
| 69 | +(3.46 dB per decade of bytes between its two points), so "better" means | ||
| 70 | +fewer bytes AT the arm's own PSNR. | ||
| 71 | + | ||
| 72 | +| # | mechanism | bytes at stake | verdict | | ||
| 73 | +|---|---|---|---| | ||
| 74 | +| 1 | anchor density (rav1e anchors every 2 pictures, ours every 4) | 22.1 kB of leaf+mid | **REFUTED** | | ||
| 75 | +| 2 | hidden-frame rate weight (ARF codes residual on 50% of area, rav1e 26-30%) | 48.1 kB of ARF | **REFUTED** | | ||
| 76 | +| 3 | motion search widening at distance 8 (`MV_DIST_SCALE`, its 4x cap) | 48.1 kB of ARF | **INERT** | | ||
| 77 | +| 4 | the temporal lambda map never ran on the pyramid path | the whole stream | **KEPT, small** | | ||
| 78 | +| 5 | ARF q offset / key allocation | — | already swept (lane-arfq, lane-keyq) | | ||
| 79 | + | ||
| 80 | +1. `EC_AV1_PYRAMID` density arms: `4:-32:12:-8:-48` 108567 B/45.809 dB and | ||
| 81 | + 613170 B/48.283 (3.8% better at q150, 3.8% WORSE at q90), `4:-24:12:-8:-48` | ||
| 82 | + 92200/45.504 and 535188/48.102 (0.4% better, 2.7% worse), `2:-32:12:-8:-48` | ||
| 83 | + 127348/45.982 and 778014/48.492 (tie, 15% worse). Denser anchors pay only | ||
| 84 | + at the low-rate end and lose at the rates the gate is scored on -- the same | ||
| 85 | + verdict lane-pyr6 got for a fourth level, now with the mini-GOP-4 shape | ||
| 86 | + (rav1e's exact structure) measured too. | ||
| 87 | +2. `EC_AV1_LAMBDA_HIDDEN` (a rate weight for hidden frames only, on top of | ||
| 88 | + the q-derived lambda -- the one knob the q offsets cannot express): | ||
| 89 | + 1.5 -> 84513/45.297 (4.8% WORSE), 2.0 -> 81454/45.160, 3.0 -> 75372/44.910, | ||
| 90 | + 0.5 -> 106859/45.733 (0.9% better at q150, 5.5% worse at q90). 1.0 is the | ||
| 91 | + optimum; our ARF's operating point is not mispriced. | ||
| 92 | +3. `EC_AV1_MV_DIST_SCALE` x `EC_AV1_MV_DIST_CAP`: (0.5,8) 90534/45.471, | ||
| 93 | + (1.0,8) 90531/45.481, (2.0,8) 90526/45.473, (1.0,16) 90650/45.474 against | ||
| 94 | + the control's 90674/45.473 -- +-0.2% bytes, +-0.01 dB. The distance-8 ARF | ||
| 95 | + is NOT search-range-bound (and its coded mv histogram, 0.0% over 64 px, is | ||
| 96 | + the content, not a clamp). | ||
| 97 | + | ||
| 98 | +## 5. The lever: the temporal lambda map was inert on every default stream | ||
| 99 | + | ||
| 100 | +`Av1Encoder::encode_pyramid_inter` passed `&[]` as the lookahead window with | ||
| 101 | +the comment "the pyramid path reorders pictures, so its own buffer is not a | ||
| 102 | +lookahead window: the temporal lambda weighting is off on this path". The | ||
| 103 | +pyramid has been the DEFAULT since lane-av1pyrdef, so the propagating tpl map | ||
| 104 | +lane-av1tpl2/3 measured and shipped (`TPL_DEPTH` 8, `TPL_STRENGTH` 0.5) has | ||
| 105 | +been running on nothing but the flat A/B arm ever since -- class | ||
| 106 | +`tool disabled in every gate recipe`. | ||
| 107 | + | ||
| 108 | +The fix is the window the group already holds: for a frame inside the leaf | ||
| 109 | +run, its display-order successors in `pending`; for the group's top ARF (the | ||
| 110 | +last picture of the group, whose successors are not buffered yet) the group's | ||
| 111 | +own leaves in REVERSE display order, which are exactly the pictures that | ||
| 112 | +predict backward from it. `EC_AV1_TPL_PYRAMID=0` restores the old behaviour. | ||
| 113 | + | ||
| 114 | +Probe arms on film B (control = the old `&[]`): | ||
| 115 | + | ||
| 116 | +| arm | q150 | q90 | vs control at equal PSNR | | ||
| 117 | +|---|---|---|---| | ||
| 118 | +| off (control) | 90674 B / 45.473 dB | 479686 B / 47.978 dB | — | | ||
| 119 | +| on, depth 8 | 91206 / 45.511 | 482161 / 47.989 | -1.9% / -0.2% | | ||
| 120 | +| on, depth 4 | 91453 / 45.506 | 481555 / 47.991 | -1.4% / -0.3% | | ||
| 121 | +| on, depth 2 | 91328 / 45.489 | 481497 / 47.990 | -0.6% / -0.3% | | ||
| 122 | + | ||
| 123 | +Wall is unchanged (101.5 s vs 101.2 s for the two-point ladder). | ||
| 124 | + | ||
| 125 | +## 6. Gate tables | ||
| 126 | + | ||
| 127 | +Long-GOP arm (`bd_rate_film_long_gop`, 48 frames, BD vs libaom cpu-used 6 / | ||
| 128 | +vs rav1e speed 6). The control arm was RUN, not quoted, and lands on the | ||
| 129 | +recorded defaults to the digit: | ||
| 130 | + | ||
| 131 | +| arm | film A | film B | | ||
| 132 | +|---|---|---| | ||
| 133 | +| control (`EC_AV1_TPL_PYRAMID=0`) | +43.1% / +4.6% | +129.0% / +34.3% | | ||
| 134 | +| **arm 1: tpl on the pyramid path** | **+42.8% / +4.5%** | **+127.3% / +32.9%** | | ||
| 135 | + | ||
| 136 | +| **arm 2: + a second past anchor on the top ARF** | **+40.9% / +3.1%** | **+125.6% / +32.0%** | | ||
| 137 | + | ||
| 138 | +Both arms pass the keep rule on their own control (every film row down on | ||
| 139 | +both columns), so both ship on by default. Together: film A +43.1/+4.6 -> | ||
| 140 | ++40.9/+3.1 and film B +129.0/+34.3 -> +125.6/+32.0, i.e. -2.2/-1.5 and | ||
| 141 | +-3.4/-2.3 points. | ||
| 142 | + | ||
| 143 | +### Arm 2: what it is | ||
| 144 | + | ||
| 145 | +The census read `refs [LAST@-8 GOLDEN@-8]` on our first top ARF -- one | ||
| 146 | +picture named twice -- and `LAST@-8 GOLDEN@-16/-24` after, against rav1e's | ||
| 147 | +`LAST@-4 LAST2@-8` (two distinct anchors of its own chain). Our top ARF's | ||
| 148 | +`ALTREF` slot was pinned to `GOLDEN_SLOT`, the key frame it already reads. | ||
| 149 | +The slot the ARF is ABOUT to refresh still holds the ARF from two groups back | ||
| 150 | +while the frame is coded, so naming that slot gives the frame a second, | ||
| 151 | +distinct past anchor for no extra DPB slot and no extra encode: | ||
| 152 | +`crate::encoder::arf_altref`, `EC_AV1_ARF_ALTREF=0` restores the old wiring. | ||
| 153 | +Probe arms on film B: 91262 B/45.518 dB and 479578 B/47.996 dB against arm | ||
| 154 | +1's 91206/45.511 and 482161/47.989 (0.4% and 0.6% better at equal PSNR). | ||
| 155 | + | ||
| 156 | +12-frame arm (`bd_rate_screen_native`), control re-run the same way: | ||
| 157 | + | ||
| 158 | +| arm | bars 1080p | bars 2160p | film A | film B | | ||
| 159 | +|---|---|---|---|---| | ||
| 160 | +| control | +1.7 / -14.5 | +12.2 / -10.2 | +37.5 / +7.6 | +54.1 / +23.9 | | ||
| 161 | +| arm 1 | +2.0 / -14.2 | +12.6 / -9.9 | +37.2 / +7.3 | +53.2 / +23.1 | | ||
| 162 | +| arm 1 + arm 2 (ships) | +2.2 / -14.0 | +12.7 / -9.8 | **+37.1 / +7.3** | **+52.5 / +22.3** | | ||
| 163 | + | ||
| 164 | +Both REAL film rows improve on both columns here too; the two "bars" rows are | ||
| 165 | +ffmpeg `testsrc2` colour bars (recorded, never a decision) and read 0.3-0.4 | ||
| 166 | +points up. | ||
| 167 | + | ||
| 168 | +Invariants at the SHIPPED configuration (both arms on): `encoder::` scoped | ||
| 169 | +33 passed / 0 failed (release binary, 780 s, the bitrate test included), | ||
| 170 | +`the_facade_codes_the_same_bytes_as_encode_sequence` 1 passed, | ||
| 171 | +`tile_bytes_do_not_depend_on_the_thread_count --include-ignored` 1 passed, | ||
| 172 | +`EC_COMP_MISMATCH=1` over the 48-frame film B encode 0 lines, ec-av1 lib | ||
| 173 | +suite 558 passed / 0 failed / 44 ignored (745 s), | ||
| 174 | +`cargo check --workspace --all-targets -j4` 0 errors and 0 ec-av1 warnings. | ||
| 175 | +Pins: 9835 -> 9859 (tpl window) -> **9853** at q=150, 35798 -> 35904 -> | ||
| 176 | +**35866** at q=60. | ||
| 177 | + | ||
| 178 | +Arm 1's own invariant run (tpl window only, before arm 2): `encoder::` scoped 33 passed / 0 failed | ||
| 179 | +(release binary, 868 s, includes the bitrate test), | ||
| 180 | +`the_facade_codes_the_same_bytes_as_encode_sequence` + | ||
| 181 | +`tile_bytes_do_not_depend_on_the_thread_count --include-ignored` 2 passed, | ||
| 182 | +`EC_COMP_MISMATCH=1` over the 48-frame film B encode: 0 lines. | ||
| 183 | +Pins re-taken (the stream moves by construction): 9835 -> **9859** at q=150 | ||
| 184 | +and 35798 -> **35904** at q=60; `EC_AV1_TPL_PYRAMID=0` restores the old pair. | ||