wip(av1): lane-lookahead drain/group-coding restructure in progress, snapshot (orchestrator)
Browse files at this commit676662710318a834240de8cc3fcb17c2e9e2fab4Parent: a1a87773 changed files +180 -7
- crates/ec-av1/src/encoder.rs +8 -0
- crates/ec-av1/src/speed.rs +31 -7
- lanes/lookahead.report.md +141 -0
crates/ec-av1/src/encoder.rsmodified+8 -0
| @@ -3687,6 +3687,13 @@ mod tests { | ||
| 3687 | 3687 | #[test] |
| 3688 | 3688 | fn the_lookahead_holds_one_group_and_still_codes_every_picture() { |
| 3689 | 3689 | let _knobs = crate::speed::knob_write(); |
| 3690 | + // The preset is PROCESS-GLOBAL and another test may have left it | |
| 3691 | + // anywhere: at a high one every block of this fixture codes as skip | |
| 3692 | + // (17 pictures in 493 bytes) and the source filter cannot change a | |
| 3693 | + // byte, which failed the future-half assertion below in the suite and | |
| 3694 | + // passed it standalone (class process-global-knob-races-tests). | |
| 3695 | + let was = crate::speed::speed(); | |
| 3696 | + crate::speed::set_speed(0); | |
| 3690 | 3697 | crate::encode::force_screen(Some(false)); |
| 3691 | 3698 | let (width, height) = (128usize, 128usize); |
| 3692 | 3699 | let run = |n: usize, gop: usize, on: bool, fut: usize| -> (Vec<u8>, Vec<Packet>) { |
| @@ -3758,6 +3765,7 @@ mod tests { | ||
| 3758 | 3765 | assert!(got == *b, "symmetric arm, display frame {i}: ours differs from ffmpeg"); |
| 3759 | 3766 | } |
| 3760 | 3767 | } |
| 3768 | + crate::speed::set_speed(was); | |
| 3761 | 3769 | } |
| 3762 | 3770 | |
| 3763 | 3771 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { |
crates/ec-av1/src/speed.rsmodified+31 -7
| @@ -433,23 +433,47 @@ pub(crate) const ARF_TF_MID: [f64; 11] = [0.0; 11]; | ||
| 433 | 433 | /// measured with two neighbours on EACH side; four pictures of one-sided |
| 434 | 434 | /// past is a different (and worse) window, and the anchor drifts away from |
| 435 | 435 | /// the leaves it has to predict. |
| 436 | -pub(crate) const ARF_TF_WIN: [usize; 11] = [2; 11]; | |
| 436 | +/// | |
| 437 | +/// lane-lookahead gave the encoder the future half ([`LOOKAHEAD`]) and swept | |
| 438 | +/// the SYMMETRIC window (this many past neighbours and [`ARF_TF_WIN_FUT`] | |
| 439 | +/// future ones) on the long-GOP gate at strength 2: | |
| 440 | +/// | |
| 441 | +/// | window | film A | film B | | |
| 442 | +/// |---|---|---| | |
| 443 | +/// | +-0 (2 past, the control) | +22.0/-8.4 | +76.5/+1.2 | | |
| 444 | +/// | +-1 | +21.4/-8.8 | +74.3/+0.3 | | |
| 445 | +/// | +-2 | +21.2/-8.8 | +72.3/-0.9 | | |
| 446 | +/// | **+-3 (ships)** | **+21.2/-8.8** | **+72.1/-1.2** | | |
| 447 | +/// | +-4 | +21.5/-8.7 | +72.5/-1.0 | | |
| 448 | +/// | |
| 449 | +/// Bracketed on both sides and down on all four columns against the control | |
| 450 | +/// (film A -0.8/-0.4, film B -4.4/-2.4). The census's shape is confirmed: | |
| 451 | +/// what a wider window cost past-only, it buys symmetric. | |
| 452 | +pub(crate) const ARF_TF_WIN: [usize; 11] = [3; 11]; | |
| 437 | 453 | |
| 438 | 454 | /// lane-lookahead: how many DISPLAY-FUTURE neighbours the top ARF's temporal |
| 439 | 455 | /// filter averages in, on top of [`ARF_TF_WIN`] past ones. Non-zero needs |
| 440 | 456 | /// [`LOOKAHEAD`] -- the future pictures are the next group's sources, which |
| 441 | 457 | /// the encoder buffers only with the lookahead on. |
| 442 | -/// `EC_AV1_ARF_TF_WIN_FUT=<n>` overrides it. | |
| 443 | -pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [0; 11]; | |
| 458 | +/// `EC_AV1_ARF_TF_WIN_FUT=<n>` overrides it. The sweep is [`ARF_TF_WIN`]'s | |
| 459 | +/// table (the two move together, +-n); strength 3 at +-2 loses to strength 2 | |
| 460 | +/// (film A +21.5/-8.7, film B +73.1/-0.7 against +21.2/-8.8 and +72.3/-0.9), | |
| 461 | +/// so [`ARF_TF`] stays where lane-arftf put it. | |
| 462 | +pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [3; 11]; | |
| 444 | 463 | |
| 445 | 464 | /// lane-lookahead: whether [`crate::encoder::Av1Encoder`] holds one whole |
| 446 | 465 | /// mini-GOP of lookahead -- the next group's sources are buffered before the |
| 447 | 466 | /// current group's top ARF is coded, so that ARF has display-FUTURE pictures |
| 448 | -/// available. Costs one group of latency (`mini_gop` pictures, 8 by default: | |
| 449 | -/// ~99 MB of buffered source at 4K 8-bit 4:2:0) and nothing else: with | |
| 467 | +/// available. Costs one group of latency (`mini_gop` pictures, 8 by default) | |
| 468 | +/// and one group of source buffer -- [`crate::encode::Picture`] holds u16 | |
| 469 | +/// samples, so a 3840x2160 4:2:0 picture is 24.9 MB and the group is 199 MB | |
| 470 | +/// on top of the group the encoder already buffered -- and nothing else: with | |
| 450 | 471 | /// [`ARF_TF_WIN_FUT`] at 0 nothing reads the future half and the streams are |
| 451 | -/// byte-identical. `EC_AV1_LOOKAHEAD=0|1` overrides it. | |
| 452 | -pub(crate) const LOOKAHEAD: bool = false; | |
| 472 | +/// byte-identical. `EC_AV1_LOOKAHEAD=0|1` overrides it. What it BUYS is | |
| 473 | +/// [`ARF_TF_WIN_FUT`]: the symmetric filter window is the long-GOP gate's | |
| 474 | +/// film A -0.8/-0.4 and film B -4.4/-2.4, and the buffer is also what a | |
| 475 | +/// two-pass-like allocation would need. | |
| 476 | +pub(crate) const LOOKAHEAD: bool = true; | |
| 453 | 477 | |
| 454 | 478 | /// lane-arfpred: the qindex below which the ARF temporal filter is OFF -- |
| 455 | 479 | /// libaom scales `arnr` strength with the quantizer and stops filtering at |
lanes/lookahead.report.mdadded+141 -0
| @@ -0,0 +1,141 @@ | ||
| 1 | +# lane-lookahead — one mini-GOP of lookahead, and the ARF filter's future half | |
| 2 | + | |
| 3 | +Worktree `edith_codecs-lookahead`, branch `lane-lookahead` off main `eb2af4ee`. | |
| 4 | +Deciding gate `encode::tests::bd_rate_film_long_gop` (48 pictures, both film | |
| 5 | +rows, BD vs libaom `cpu-used 6` / rav1e `speed 6`, lower is better). | |
| 6 | +**Control RUN, not quoted: film A +22.0 / −8.4, film B +76.5 / +1.2** — the | |
| 7 | +charter's +22.2/−8.2 and +77.5/+1.7 to within the gate's own noise. | |
| 8 | + | |
| 9 | +## 1. The machinery — one group held back | |
| 10 | + | |
| 11 | +`Av1Encoder::drain_pending` used to code the group it had just closed. It now | |
| 12 | +hands that group to a new `ready` slot and codes the group ALREADY there, | |
| 13 | +passing the just-closed group's sources as its display-FUTURE window; the | |
| 14 | +group's top ARF is its last picture in display order, so every picture of the | |
| 15 | +next group is a future neighbour of it. `code_group` (the old `drain_pending` | |
| 16 | +body) takes the group and that future slice as arguments, and `drain_all` | |
| 17 | +codes both buffered groups in coding order — what a key frame (the key | |
| 18 | +picture itself is then the last group's future) and `flush` (nothing behind | |
| 19 | +it) both need. | |
| 20 | + | |
| 21 | +* **Latency**: one `mini_gop`, 8 pictures at the default pyramid, on top of | |
| 22 | + the group the encoder already buffered. | |
| 23 | +* **Memory**: `Picture` holds u16 samples, so a 3840x2160 4:2:0 source is | |
| 24 | + 24.9 MB and the extra group is **199 MB at 4K** (56 MB at 1080p). Nothing | |
| 25 | + else is retained: `ready` holds sources, not reconstructions. | |
| 26 | +* Output picture order, timestamps and the `show_existing_frame` headers are | |
| 27 | + untouched — the packets are the same packets, emitted one group later. | |
| 28 | + | |
| 29 | +`speed::LOOKAHEAD` (`EC_AV1_LOOKAHEAD=0|1`) switches it off: the open group is | |
| 30 | +then coded straight away, which is byte for byte the old encoder. | |
| 31 | + | |
| 32 | +### The witness — `encoder::tests::the_lookahead_holds_one_group_and_still_codes_every_picture` | |
| 33 | + | |
| 34 | +At 1, 7, 8, 9 and 17 pictures, at GOP 32 (one key) and GOP 8 (keys drain the | |
| 35 | +buffer mid-run), with a gradient-plus-noise fixture: | |
| 36 | + | |
| 37 | +* the stream is **byte-identical** to the same run with the lookahead off, and | |
| 38 | + the packet (order, key, level) list is identical — the delay changes nothing | |
| 39 | + about WHAT is coded while the future window is empty; | |
| 40 | +* every run decodes to **exactly its picture count** through `decode_stream` | |
| 41 | + AND through ffmpeg, **sample-exact between the two**, so the flush, the | |
| 42 | + short final group (1 picture = one shown leaf, no hidden frame) and the | |
| 43 | + display order all survive the delay; | |
| 44 | +* with ONE future neighbour the stream **MOVES** — the buffered picture is | |
| 45 | + really consumed ([[gate-blind-to-feature]]: everything above would pass over | |
| 46 | + a lookahead nothing reads) — and still decodes sample-exact. | |
| 47 | + | |
| 48 | +RED first: in the split suite (not standalone) the future-half assertion | |
| 49 | +failed with "493 B both" — another test had left the process-global preset | |
| 50 | +high, every block coded as skip and the source filter could not change a byte | |
| 51 | +(class `process-global-knob-races-tests`). The test pins `set_speed(0)` under | |
| 52 | +`knob_write` now, the way the byte pins do; green in the suite below. | |
| 53 | + | |
| 54 | +The gate is a second witness: `native_bd_arm` asserts our decoder's | |
| 55 | +display-order count and ffmpeg sample-exactness for every one of its 48-picture | |
| 56 | +streams, and all six long-GOP arms below ran RC=0. | |
| 57 | + | |
| 58 | +## 2. The symmetric filter window — it pays, and it is bracketed | |
| 59 | + | |
| 60 | +`speed::ARF_TF_WIN` is the PAST neighbour count, new `speed::ARF_TF_WIN_FUT` | |
| 61 | +(`EC_AV1_ARF_TF_WIN_FUT=<n>`) the FUTURE one; the caller now sizes the window | |
| 62 | +and `encode::arf_temporal_filter` takes all of it. The tpl lookahead window is | |
| 63 | +a separate argument (`encode_pyramid_inter`'s new `future` reaches the source | |
| 64 | +filter only), so this arm is the filter's window and nothing else. | |
| 65 | + | |
| 66 | +Long-GOP gate, strength 2 (lane-arftf's shipped point): | |
| 67 | + | |
| 68 | +| window | film A | film B | | |
| 69 | +|---|---|---| | |
| 70 | +| ±0 = 2 past (control) | +22.0 / −8.4 | +76.5 / +1.2 | | |
| 71 | +| ±1 | +21.4 / −8.8 | +74.3 / +0.3 | | |
| 72 | +| ±2 | +21.2 / −8.8 | +72.3 / −0.9 | | |
| 73 | +| **±3 (ships)** | **+21.2 / −8.8** | **+72.1 / −1.2** | | |
| 74 | +| ±4 | +21.5 / −8.7 | +72.5 / −1.0 | | |
| 75 | + | |
| 76 | +**Down on all four columns against the control** (film A −0.8/−0.4, film B | |
| 77 | +−4.4/−2.4), which clears the keep rule outright, and BRACKETED on both sides — | |
| 78 | +±4 is worse than ±3 on all four, so this is not a result at its own search | |
| 79 | +edge. `arf_pred_census`'s reading is confirmed: what a wider window cost | |
| 80 | +past-only (lane-arftf refuted 4 past at +22.8/−7.8, +78.9/+2.8) it buys | |
| 81 | +symmetric. | |
| 82 | + | |
| 83 | +Deviation from the charter, named: ±3 and ±4 were run on top of the chartered | |
| 84 | +±1/±2 because ±2 was the best point of a range with nothing above it. | |
| 85 | + | |
| 86 | +Strength re-swept at ±2, as chartered: | |
| 87 | + | |
| 88 | +| arm | film A | film B | | |
| 89 | +|---|---|---| | |
| 90 | +| ±2, strength 2 | +21.2 / −8.8 | +72.3 / −0.9 | | |
| 91 | +| ±2, strength 3 | +21.5 / −8.7 | +73.1 / −0.7 | | |
| 92 | + | |
| 93 | +Worse on three columns, so `ARF_TF` stays at 2. | |
| 94 | + | |
| 95 | +## 3. Guard rows — `bd_rate_screen_native`, 12 pictures, all five rows | |
| 96 | + | |
| 97 | +| clip | control | shipped (lookahead + ±3) | Δ | | |
| 98 | +|---|---|---|---| | |
| 99 | +| bars 1080p | −3.5 / −19.0 | −3.4 / −19.0 | +0.1 / 0.0 | | |
| 100 | +| bars 2160p | +8.6 / −13.7 | +8.6 / −13.7 | 0.0 / 0.0 | | |
| 101 | +| film A | +17.9 / −6.4 | +17.9 / −6.3 | 0.0 / +0.1 | | |
| 102 | +| film B | +22.9 / −3.6 | +22.8 / −3.6 | −0.1 / 0.0 | | |
| 103 | +| screen capture | +14.4 / −33.2 | +14.4 / −33.2 | byte-identical | | |
| 104 | + | |
| 105 | +Nothing moves by more than 0.1. Measured fact worth keeping: at 12 pictures | |
| 106 | +`group_target` absorbs the tail into ONE group of 11, so there is no next | |
| 107 | +group to buffer and **the 12-frame gate cannot see this lever at all** — a | |
| 108 | +±2 arm run at that length was byte-identical to its control on every row. The | |
| 109 | +rows above move only through the wider PAST window (2 → 3). | |
| 110 | + | |
| 111 | +## 4. Invariants | |
| 112 | + | |
| 113 | +* **Byte pins UNCHANGED**, green at the default and at `EC_AV1_SPEED=6`: | |
| 114 | + `(150, 8290, 0x92cb11033d1f5813)`, `(60, 33227, 0x57ee6b1f8eacd881)`. Not a | |
| 115 | + miss — the pin fixture is four pictures through `encode_sequence`, one | |
| 116 | + truncated group whose ARF has two past neighbours and no next group at all, | |
| 117 | + so neither the lookahead nor a 3-wide window can reach it. | |
| 118 | +* `encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames` | |
| 119 | + green and NOT near its bound any more: pyramid arms −0.1% (768 kbps), +0.0% | |
| 120 | + (1536 kbps), +0.0% (2 Mbps); flat arms −2.5% / −2.8% / −1.8%. | |
| 121 | +* Split suite at the shipped default: `--skip stream::` **346 passed / 0 | |
| 122 | + failed**, `stream:: --skip 10bit` **202 passed / 0 failed**, `10bit` **42 | |
| 123 | + passed / 0 failed** (590 in all). | |
| 124 | +* `--ignored --exact | |
| 125 | + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` | |
| 126 | + 1 passed. | |
| 127 | +* `cargo check --workspace --all-targets -j4`: 0 errors, 0 `ec-av1` warnings. | |
| 128 | + | |
| 129 | +## 5. What this lane did NOT do | |
| 130 | + | |
| 131 | +* `deferred: the tpl window at the ARF (the charter's task 3) — the buffered | |
| 132 | + future pictures reach the SOURCE filter only; encode_pyramid_inter still | |
| 133 | + gets tpl_window(pos, true), the display-past leaves, for its temporal | |
| 134 | + lambda map — unblocked by a lane with long-GOP arms to spend, and it is now | |
| 135 | + a one-argument change (pass the same future slice as lookahead).` | |
| 136 | +* `deferred: two-pass-like allocation over the buffered group (the rate loop | |
| 137 | + still plans per level from gop_shape, blind to the group it holds) — out of | |
| 138 | + this charter's scope; the buffer it needs now exists.` | |
| 139 | +* `deferred: per-block weight refinement in arf_temporal_filter (libaom | |
| 140 | + weights each 16x16 block against each neighbour separately) — untouched | |
| 141 | + since lane-arftf.` |