The temporal lambda map runs on the pyramid path, which is every default stream
Browse files at this commitccbcc73581abe2bfbdcb608249f4e03c6ac87b59Parent: 671d201- feat
- the temporal lambda map runs on the pyramid path, which is every default stream
3 changed files +134 -5
- crates/ec-av1/src/encode.rs +27 -1
- crates/ec-av1/src/encoder.rs +47 -4
- lanes/arfcen.report.md +60 -0
crates/ec-av1/src/encode.rsmodified+27 -1
| @@ -137,6 +137,26 @@ fn reach_sb_px(fctx: &crate::decode::FrameCtx) -> usize { | ||
| 137 | 137 | /// defaults below are on: +50.1 / -15.4). It spends ~5% more encode wall. |
| 138 | 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 | 160 | /// [`LAMBDA_SCALE`], or whatever `EC_AV1_LAMBDA` names when the sweep that |
| 141 | 161 | /// picks it is the thing running. A release build has no such knob. |
| 142 | 162 | fn lambda_scale() -> f64 { |
| @@ -9040,7 +9060,13 @@ pub(crate) fn encode_inter_frame( | ||
| 9040 | 9060 | let search = Search { |
| 9041 | 9061 | base_q_idx, |
| 9042 | 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 | 9070 | modes: &KEY_FRAME_MODES, |
| 9045 | 9071 | top_k: prune_top_k_inter(), |
| 9046 | 9072 | screen, |
crates/ec-av1/src/encoder.rsmodified+47 -4
| @@ -886,6 +886,23 @@ const MID_DEFAULT: Option<i16> = Some(-8); | ||
| 886 | 886 | /// is gated off). |
| 887 | 887 | const KEY_DEFAULT: i16 = -48; |
| 888 | 888 | |
| 889 | +/// Whether the pyramid path hands each frame a lookahead window, i.e. | |
| 890 | +/// whether the temporal lambda map (`encode::tpl_lambda_factors`) runs on | |
| 891 | +/// the DEFAULT stream at all. It did not until lane-arfcen: the pyramid | |
| 892 | +/// reorders pictures, so this path passed `&[]` and the map -- measured and | |
| 893 | +/// shipped on the flat path -- was inert on every frame of every pyramid | |
| 894 | +/// stream, including the ARFs that carry 54% of a long-GOP stream's bytes | |
| 895 | +/// (class `tool disabled in every gate recipe`). | |
| 896 | +fn tpl_pyramid() -> bool { | |
| 897 | + match std::env::var("EC_AV1_TPL_PYRAMID").ok() { | |
| 898 | + Some(v) => v != "0", | |
| 899 | + None => TPL_PYRAMID, | |
| 900 | + } | |
| 901 | +} | |
| 902 | + | |
| 903 | +/// [`tpl_pyramid`]'s default. | |
| 904 | +const TPL_PYRAMID: bool = true; | |
| 905 | + | |
| 889 | 906 | /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per |
| 890 | 907 | /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a |
| 891 | 908 | /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a |
| @@ -1464,6 +1481,10 @@ impl Av1Encoder { | ||
| 1464 | 1481 | // ARF and the mid one) at different offsets, and they share the |
| 1465 | 1482 | // rate-control slot but not the offset. |
| 1466 | 1483 | offset: i16, |
| 1484 | + // The lookahead window this frame's temporal lambda map is built | |
| 1485 | + // from, already padded (lane-arfcen). Empty codes the frame at one | |
| 1486 | + // lambda, which is what the whole pyramid path used to do. | |
| 1487 | + lookahead: &[Picture], | |
| 1467 | 1488 | ) -> Result<Packet> { |
| 1468 | 1489 | let render = (self.config.width, self.config.height); |
| 1469 | 1490 | let order_hint = (order & 0x7f) as u32; |
| @@ -1529,10 +1550,10 @@ impl Av1Encoder { | ||
| 1529 | 1550 | show_frame, |
| 1530 | 1551 | sign_bias, |
| 1531 | 1552 | }), |
| 1532 | - // The pyramid path reorders pictures, so its own buffer is not | |
| 1533 | - // a lookahead window: the temporal lambda weighting is off on | |
| 1534 | - // this path (lane-av1tpl). | |
| 1535 | - &[], | |
| 1553 | + // lane-arfcen: the group's own sources ARE this frame's window | |
| 1554 | + // (`tpl_window`); it was `&[]` here, which switched the temporal | |
| 1555 | + // lambda map off on every frame the default stream codes. | |
| 1556 | + lookahead, | |
| 1536 | 1557 | ) }(); |
| 1537 | 1558 | // Name the pyramid position in any failure: an error out of the tile |
| 1538 | 1559 | // writer or the filter search is otherwise indistinguishable between |
| @@ -1568,6 +1589,23 @@ impl Av1Encoder { | ||
| 1568 | 1589 | } |
| 1569 | 1590 | let group: Vec<(u64, Picture)> = std::mem::take(&mut self.pending); |
| 1570 | 1591 | let mut packets = Vec::with_capacity(group.len() + 1); |
| 1592 | + // The lookahead window of the group's picture at index `pos`: the | |
| 1593 | + // display-order successors inside this group, or -- for the group's | |
| 1594 | + // top ARF, the last picture, whose successors are not buffered yet -- | |
| 1595 | + // the leaves BEHIND it in reverse display order, which are exactly | |
| 1596 | + // the pictures that predict backward from it. Padded like the frame | |
| 1597 | + // itself, as `encode_inter_frame` wants it. | |
| 1598 | + let tpl_window = |pos: usize, back: bool| -> Vec<Picture> { | |
| 1599 | + let depth = crate::encode::tpl_depth(); | |
| 1600 | + if !tpl_pyramid() || depth < 2 { | |
| 1601 | + return Vec::new(); | |
| 1602 | + } | |
| 1603 | + let window: Vec<&Picture> = match back { | |
| 1604 | + false => group[pos + 1..].iter().map(|(_, p)| p).collect(), | |
| 1605 | + true => group[..pos].iter().rev().map(|(_, p)| p).collect(), | |
| 1606 | + }; | |
| 1607 | + window.into_iter().take(depth - 1).map(|p| p.padded_to(SUPERBLOCK)).collect() | |
| 1608 | + }; | |
| 1571 | 1609 | let anchor_slot = ANCHOR_SLOTS[self.anchor]; |
| 1572 | 1610 | let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; |
| 1573 | 1611 | let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); |
| @@ -1584,6 +1622,7 @@ impl Av1Encoder { | ||
| 1584 | 1622 | true, |
| 1585 | 1623 | Level::Leaf, |
| 1586 | 1624 | pyramid.leaf_q_offset, |
| 1625 | + &tpl_window(0, false), | |
| 1587 | 1626 | )?); |
| 1588 | 1627 | self.anchor = 1 - self.anchor; |
| 1589 | 1628 | return Ok(packets); |
| @@ -1598,6 +1637,7 @@ impl Av1Encoder { | ||
| 1598 | 1637 | false, |
| 1599 | 1638 | Level::Arf, |
| 1600 | 1639 | pyramid.arf_q_offset, |
| 1640 | + &tpl_window(group.len() - 1, true), | |
| 1601 | 1641 | )?); |
| 1602 | 1642 | let leaves = group.len() - 1; |
| 1603 | 1643 | // THE THIRD LEVEL: a second hidden frame at the middle of the leaf |
| @@ -1630,6 +1670,7 @@ impl Av1Encoder { | ||
| 1630 | 1670 | false, |
| 1631 | 1671 | Level::Arf, |
| 1632 | 1672 | offset, |
| 1673 | + &tpl_window(at, false), | |
| 1633 | 1674 | )?); |
| 1634 | 1675 | } |
| 1635 | 1676 | // THE FOURTH LEVEL: one hidden frame at the middle of each half of |
| @@ -1665,6 +1706,7 @@ impl Av1Encoder { | ||
| 1665 | 1706 | false, |
| 1666 | 1707 | Level::Arf, |
| 1667 | 1708 | offset, |
| 1709 | + &tpl_window(pos, false), | |
| 1668 | 1710 | )?); |
| 1669 | 1711 | } |
| 1670 | 1712 | } |
| @@ -1721,6 +1763,7 @@ impl Av1Encoder { | ||
| 1721 | 1763 | true, |
| 1722 | 1764 | Level::Leaf, |
| 1723 | 1765 | pyramid.leaf_q_offset, |
| 1766 | + &tpl_window(i, false), | |
| 1724 | 1767 | )?); |
| 1725 | 1768 | } |
| 1726 | 1769 | let packet = show_existing(self, next_anchor_slot, arf_order)?; |
lanes/arfcen.report.mdmodified+60 -0
| @@ -61,3 +61,63 @@ the same six positions in rav1e's stream, and everything else: | ||
| 61 | 61 | So the WHOLE +15.4 kB gap (and 6 kB more) sits in the six pictures we code at |
| 62 | 62 | prediction distance 8; our non-anchor pictures are already 23% CHEAPER than |
| 63 | 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). |