diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 26d296ac..53cc82b8 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -11711,7 +11711,7 @@ pub(crate) fn arf_temporal_filter(src: &Picture, window: &[Picture], strength: f let neighbours: Vec<&Picture> = window .iter() .filter(|p| p.width == src.width && p.height == src.height) - .take(2) + .take(crate::encoder::arf_tf_window()) .collect(); if neighbours.is_empty() || strength <= 0.0 { return src.clone(); @@ -17914,7 +17914,13 @@ mod tests { // inter frame that carries `reference_select` codes different blocks // -- 8354 -> 8364 bytes at q=150 and 33222 -> 33257 at q=60. // `EC_AV1_B128COMP=0` restores 8354 / 33222 exactly. - let pins: [(u8, usize, u64); 2] = [(150, 8364, 0xcb2ad7936d5127c5), (60, 33257, 0x87db250ba556e5a6)]; + // Re-taken on lane-arftf: that filter's strength swept on the deciding + // gate lands at 2, not 3 ([`crate::speed::ARF_TF`]), so the filtered + // anchors' pixels moved once more: 8218 -> 8307 bytes at q=150 and + // 33017 at q=60 (the same LENGTH, different bytes). + // Re-taken at the merge of lane-arftf (ARF filter strength 2) and + // lane-rlclamp onto the 128 compound default. + let pins: [(u8, usize, u64); 2] = [(150, 8290, 0x92cb11033d1f5813), (60, 33227, 0x57ee6b1f8eacd881)]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index e0ba725a..e320148c 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1215,6 +1215,29 @@ fn arf_tf_strength() -> f64 { .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF)) } +/// lane-arftf: the same filter's strength on the MID ARF, whose own window +/// is the display-FUTURE leaves of its group (`tpl_window(at, false)`). +/// `EC_AV1_ARF_TF_MID=` overrides [`crate::speed::ARF_TF_MID`]; +/// `0` is off. +fn arf_tf_mid_strength() -> f64 { + std::env::var("EC_AV1_ARF_TF_MID") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF_MID)) +} + +/// lane-arftf: how many of the window's pictures the filter averages in +/// ([`crate::speed::ARF_TF_WIN`], `EC_AV1_ARF_TF_WIN=`). The top ARF's +/// window holds only DISPLAY-PAST neighbours -- it is the group's last +/// picture and the next group's sources are not buffered when it is coded -- +/// so this widens the filter backwards only. +pub(crate) fn arf_tf_window() -> usize { + std::env::var("EC_AV1_ARF_TF_WIN") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF_WIN)) +} + fn tpl_pyramid() -> bool { match std::env::var("EC_AV1_TPL_PYRAMID").ok() { Some(v) => v != "0", @@ -1938,15 +1961,20 @@ impl Av1Encoder { // ... and never at the very top of the quality range, where the // anchor's own quantizer has hit the rate loop's floor // ([`crate::speed::ARF_TF_QMIN`]). + let tf = match dq_level { + crate::encode::DqLevel::TopArf => arf_tf_strength(), + // lane-arftf: the mid ARF gets its own strength -- its window is + // the group's later leaves, i.e. the filter runs FORWARD there. + crate::encode::DqLevel::MidArf => arf_tf_mid_strength(), + crate::encode::DqLevel::Leaf => 0.0, + }; let tf = match base_q_idx >= crate::speed::ARF_TF_QMIN { - true => arf_tf_strength(), + true => tf, false => 0.0, }; - let source = match (tf > 0.0, dq_level) { - (true, crate::encode::DqLevel::TopArf) => { - crate::encode::arf_temporal_filter(&source, lookahead, tf) - } - _ => source, + let source = match tf > 0.0 { + true => crate::encode::arf_temporal_filter(&source, lookahead, tf), + false => source, }; let encoded = || -> Result { encode_inter_frame( &source, diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index 5ed6f668..300d9444 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -393,7 +393,47 @@ pub(crate) const DQ_LEVEL_K: [f64; 3] = [1.5, 1.0, 1.0]; /// (`lanes/arfpred.report.md` section 3). Hard filtering does remove 6-7% of /// the stream's bytes and gives all of it back in quality, because the leaves /// still carry the grain the anchor no longer has. -pub(crate) const ARF_TF: [f64; 11] = [3.0; 11]; +/// +/// lane-arftf SWEPT IT ON THE DECIDING GATE (48 pictures, film A / film B, +/// BD vs libaom `cpu-used 6` / rav1e `speed 6`, lower is better) -- the probe +/// of lane-arfpred mispredicts this lever's sign, so only these count: +/// +/// | strength | film A | film B | +/// |---|---|---| +/// | 1 | +22.6/-8.1 | +81.6/+4.8 | +/// | **2 (ships)** | **+22.2/-8.2** | **+78.6/+2.7** | +/// | 3 (was) | +22.7/-7.9 | +79.0/+2.7 | +/// | 5 | +23.9/-7.4 | +83.6/+5.1 | +/// +/// The curve is single-peaked at 2 and bracketed on both sides; 2 is down on +/// all four columns against the shipped 3 (film A -0.5/-0.3, film B +/// -0.4/0.0). +pub(crate) const ARF_TF: [f64; 11] = [2.0; 11]; + +/// lane-arftf: the same filter on the MID ARF (the second hidden frame of a +/// group), whose window is the group's later leaves -- so it filters FORWARD +/// where the top ARF filters backward. `EC_AV1_ARF_TF_MID=` +/// overrides it; `0` is off, which is where the long-GOP gate left it: +/// filtering the mid ARF at the same strength as the top one reads film A +/// +22.2/-8.3 and film B +78.9/+2.6 against the top-only +22.2/-8.2 and +/// +78.6/+2.7 -- inside the noise on three columns and 0.3 WORSE on film B's +/// libaom one, so the mechanism does not pay a second time one level down. +pub(crate) const ARF_TF_MID: [f64; 11] = [0.0; 11]; + +/// lane-arftf: how many window pictures [`crate::encode::arf_temporal_filter`] +/// averages in. The top ARF is its group's LAST picture and the next group's +/// sources are not buffered when it is coded, so the window holds only +/// display-PAST neighbours and this widens the filter backwards only (a +/// symmetric +-2 needs a one-group lookahead the encoder does not have). +/// `EC_AV1_ARF_TF_WIN=` overrides it. +/// +/// 4 (past-only +-2) is REFUTED on the long-GOP gate at strength 2: film A +/// +22.8/-7.8 and film B +78.9/+2.8 against 2's +22.2/-8.2 and +78.6/+2.7 -- +/// worse on three of the four columns. The census's 14.3% SAD removal was +/// measured with two neighbours on EACH side; four pictures of one-sided +/// past is a different (and worse) window, and the anchor drifts away from +/// the leaves it has to predict. +pub(crate) const ARF_TF_WIN: [usize; 11] = [2; 11]; /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- /// libaom scales `arnr` strength with the quantizer and stops filtering at diff --git a/lanes/arftf.report.md b/lanes/arftf.report.md new file mode 100644 index 00000000..9b55f3ea --- /dev/null +++ b/lanes/arftf.report.md @@ -0,0 +1,116 @@ +# lane-arftf — the ARF temporal filter's strength, window and level + +Worktree `edith_codecs-arftf`, branch `lane-arftf` off main `8d9d056b`. +Deciding gate `encode::tests::bd_rate_film_long_gop` (48 pictures, both film +rows, BD vs libaom `cpu-used 6` / rav1e `speed 6`, lower is better). +**Control RUN, not quoted: film A +22.7 / −7.9, film B +79.0 / +2.7 — the +charter's numbers to the digit.** + +lane-arfpred shipped the filter at strength 3 with only that one point on the +gate; its two-point probe mispredicted the SIGN of this lever, so every arm +here is a gate arm. + +## 1. Strength sweep — the shipped point moves 3 → 2 + +| strength | film A | film B | +|---|---|---| +| 1 | +22.6 / −8.1 | +81.6 / +4.8 | +| 2 **(ships)** | **+22.2 / −8.2** | **+78.6 / +2.7** | +| 3 (was) | +22.7 / −7.9 | +79.0 / +2.7 | +| 5 | +23.9 / −7.4 | +83.6 / +5.1 | + +Single-peaked and BRACKETED on both sides. 2 is down on all four columns +against the shipped 3 (film A −0.5 / −0.3, film B −0.4 / 0.0), which clears +the keep rule outright. Both neighbours are worse on the film B libaom +column by 2.6 (s1) and 5.0 (s5), so this peak is not a noise artefact. + +## 2. The ±2 window — the future half is NOT buffered, past-only ±2 loses + +`Av1Encoder::drain_pending` codes the group's top ARF as the group's LAST +picture (`group.last()`), and `encode_frames` pushes into `pending` and drains +the whole group before the next picture arrives, so **when the top ARF is +coded no picture after it exists in the encoder at all**. `tpl_window(pos, +true)` hands it `group[..pos]` reversed — display-PAST neighbours only, up to +`tpl_depth() − 1` of them (7 at the default preset). A symmetric ±2 needs a +one-group lookahead this encoder does not have; that is the blocker, and it +is a picture-buffer change, not a filter change. + +What IS available is more past, so the charter's fallback was measured: +`speed::ARF_TF_WIN` (new, `EC_AV1_ARF_TF_WIN=`) widens the neighbour count +from the literal 2 to any n. + +| window | film A | film B | +|---|---|---| +| 2 past (ships) | +22.2 / −8.2 | +78.6 / +2.7 | +| 4 past (±2 one-sided) | +22.8 / −7.8 | +78.9 / +2.8 | + +REFUTED on three of the four columns. `arf_pred_census`'s 14.3% SAD removal +was measured with two neighbours on EACH side; four pictures of one-sided past +is a different window, and the anchor drifts away from the leaves that have to +predict from it. + +## 3. The mid ARF — filtering it a second level down does not pay + +`speed::ARF_TF_MID` (new, `EC_AV1_ARF_TF_MID=`) runs the same filter +on the group's second hidden frame, whose own window is `tpl_window(at, +false)` — the group's LATER leaves, i.e. it filters forward where the top ARF +filters backward, and those pictures ARE buffered. + +| arm | film A | film B | +|---|---|---| +| top ARF only, s = 2 | +22.2 / −8.2 | +78.6 / +2.7 | +| top + mid, both s = 2 | +22.2 / −8.3 | +78.9 / +2.6 | + +Inside the noise on three columns and 0.3 WORSE on film B's libaom one, so it +ships OFF (`ARF_TF_MID = 0`). Deviation from the charter, named: the mid arm +was run at strength 2, not 3, because the sweep had already put 2 above 3 at +the top level and a 3 there would have confounded the level with the strength. + +Per-block weight refinement was left alone (charter: out of scope unless +one line — it is not). + +## 4. Guard rows — `bd_rate_screen_native`, 12 pictures, all five rows + +| clip | control (s = 3) | s = 2 | Δ | +|---|---|---|---| +| bars 1080p | −2.9 / −18.6 | −3.2 / −18.8 | −0.3 / −0.2 | +| bars 2160p | +9.1 / −13.3 | +9.1 / −13.4 | 0.0 / −0.1 | +| film A | +18.3 / −6.0 | +18.5 / −5.9 | +0.2 / +0.1 | +| film B | +24.6 / −2.1 | +24.6 / −2.0 | 0.0 / +0.1 | +| screen capture | +14.6 / −33.2 | +14.6 / −33.2 | byte-identical | + +Control reproduces the charter's numbers exactly. Nothing moves by more than +0.3; the two bars rows actually improve, and the screen sequence codes no +pyramid ARF at all so the filter never runs there. + +## 5. Invariants + +* Byte pins re-taken (the filtered anchors' pixels move by construction): + `(150, 8218 → 8307)`, and `(60, 33017)` keeps its LENGTH with a different + hash. Green at the default AND at `EC_AV1_SPEED=6`. +* `encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames` is + green in the split suite below (it sits near its bound at the rate loop's + quantizer floor; `ARF_TF_QMIN = 16` still keeps the filter off there). +* Split suite on the shipped default: `--skip stream::` **343 passed / 0 + failed**, `stream:: --skip 10bit` **202 passed / 0 failed**, `10bit` + **42 passed / 0 failed** (587 in all). +* `--ignored --exact + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` + 1 passed. +* `cargo check --workspace --all-targets -j4`: 0 errors, 0 `ec-av1` warnings + (the 25 warnings are `ec-opus` / `ec-vorbis`, pre-existing). + +## 6. What this lane did NOT do + +* `deferred: the symmetric ±2 window — the census's best filter shape needs + the two DISPLAY-FUTURE neighbours of the top ARF, which the encoder does + not hold when it codes it — unblocked by a lane that gives + Av1Encoder::drain_pending one group of lookahead (buffer group N+1's + sources before coding group N's ARF).` +* `deferred: per-block weight refinement (libaom weights each 16x16 block by + its own error against each neighbour separately, and uses up to 7 frames) — + out of the charter's scope and not a one-liner — unblocked by a lane that + ports libaom's temporal_filter weighting.` +* `deferred: a mid-ARF strength of its own (only 2 was measured there, and it + was flat-to-worse) — unblocked by a lane with spare long-GOP arms; the knob + ships wired (EC_AV1_ARF_TF_MID) so the arm is env-only.`