From 0d3b9a0f946adb1997cb8575cdbc9121f641e94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 08:58:52 +0300 Subject: [PATCH] feat(av1): the coding pyramid decided on a long GOP -- Pyramid::default 16:-24:16 -> 8:-32:16 The shipped shape came off bd_rate_screen_native, which codes 12 frames with gop = 12; a mini-GOP is cut at every key frame, so any mini_gop over 12 codes the SAME stream there and the 16 was really "one hidden ARF per 12-picture GOP". On the new long-GOP arm (48 pictures, gop 48, the two real films, vs libaom cpu-used 6 and rav1e speed 6 at -g 48) a real 16- or 32-picture group is WORSE than an 8-picture one, and ARF -32 is an interior optimum (-40 turns back). 8:-32:16 takes 11.7 / 7.5 points off film A and 0.4 / 1.0 off film B against 16:-24:16 -- all four columns down. The price is stated in Pyramid::default and lanes/pyr4.sweep.txt: at a 12-frame GOP the new shape is 7.0 / 5.7 (film A) and 20.5 / 18.3 (film B) worse, because the best shape depends on GOP length and the long GOP is what the editor exports. Pins re-taken: 8076 -> 8755 at q=150, 27585 -> 28637 at q=60. --- crates/ec-av1/src/encode.rs | 8 ++- crates/ec-av1/src/encoder.rs | 52 +++++++++++++--- lanes/pyr4.sweep.txt | 112 +++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 lanes/pyr4.sweep.txt diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index a1fbbeb3..8ca7e30c 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -13057,8 +13057,14 @@ mod tests { // the blocks that take one code a filter-intra mode, a different // prediction and different coefficients -- 7321 -> 7373 at q=150 and // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these. + // Re-taken on lane-pyr4: the default pyramid is `8:-32:16` now, the + // long-GOP sweep's shape (`lanes/pyr4.sweep.txt`), so these four + // pictures code as one truncated group of 8 with its hidden ALTREF at + // `q-32` instead of one truncated group of 16 at `q-24`: + // 8076 -> 8755 at q=150 and 27585 -> 28637 at q=60. + // `EC_AV1_PYRAMID=16:-24:16` restores these. let pins: [(u8, usize, u64); 2] = - [(150, 8076, 0x8171_4818_e223_ab21), (60, 27585, 0xdbe7_eecd_6d64_c4f5)]; + [(150, 8755, 0xa28f_a6d2_fe29_66ae), (60, 28637, 0xcdf1_ba94_4221_c10e)]; for (q, bytes, hash) in pins { let encoded = encode_sequence(&source, q, 0.5).unwrap(); assert_eq!( diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 2dc98f1d..4c4b910d 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -585,18 +585,52 @@ impl Default for Pyramid { /// are minimal and the bound is not the answer. Deeper ARF is the same /// shape -- `-32` buys 0.6 on film A and gives 0.7 back on film B. /// - /// TWO THINGS THIS SWEEP DOES NOT SAY. The mini-GOP axis is measured + /// WHAT THAT SWEEP COULD NOT SAY, and the LONG-GOP sweep that answers it + /// (lane-pyr4, `lanes/pyr4.sweep.txt`). The table above is measured /// against a gate that codes 12 frames with `gop = 12`, and a group is - /// cut at every key frame (`encode_frames`), so `16` here is really - /// "one hidden ARF per GOP" and any mini-GOP over 12 codes the same - /// stream; on a stream with a longer GOP `16` is a genuine 16-picture - /// group, which this gate cannot measure. And the offsets were swept at - /// mini-GOP 8 before 16 won the first axis; the two joint corners at 16 - /// confirm the same gradient, but the full grid at 16 is not measured. + /// cut at every key frame (`encode_frames`), so `16` there is really + /// "one hidden ARF per GOP" and ANY mini-GOP over 12 codes the same + /// stream (class `instrument at bound`). The user's exports are long + /// GOPs, so the shape is decided on + /// [`crate::encode::tests::bd_rate_film_long_gop`] -- 48 pictures, `gop + /// = 48`, the two real films, four quantizers, vs libaom `cpu-used 6` + /// and rav1e `speed=6` at the same key interval: + /// + /// | mini_gop:arf:leaf | film A vs libaom / rav1e | film B vs libaom / rav1e | + /// |---|---|---| + /// | 32:-24:16 | +79.0 / +29.8 | +164.4 / +58.8 | + /// | 16:-24:16 (was the default) | +67.4 / +21.0 | +165.5 / +57.6 | + /// | 8:-16:16 | +63.7 / +19.0 | +167.6 / +58.6 | + /// | 4:-24:16 | +61.9 / +19.5 | +160.8 / +56.7 | + /// | 8:-24:16 | +58.5 / +15.5 | +166.1 / +56.8 | + /// | 8:-24:12 | +60.2 / +17.3 | +166.8 / +58.0 | + /// | 8:-24:20 | +57.1 / +14.0 | +166.2 / +56.6 | + /// | 8:-40:16 | +56.1 / +13.3 | +167.6 / +57.3 | + /// | 8:-32:12 | +56.5 / +14.7 | +163.0 / +56.3 | + /// | 8:-32:20 | +55.4 / +12.9 | +167.9 / +57.4 | + /// | **8:-32:16** | **+55.7 / +13.5** | **+165.1 / +56.6** | + /// + /// `8:-32:16` ships: against the old `16:-24:16` it takes 11.7 / 7.5 + /// points off film A and 0.4 / 1.0 off film B -- all four columns down. + /// A real 16- or 32-picture group is WORSE than an 8-picture one on film + /// A once the GOP is long enough for the distinction to exist, which the + /// 12-frame gate could not see; `4` was probed below the swept edge and + /// turns back up on film A. ARF `-32` is an interior optimum (`-40` + /// turns back on both films). The leaf axis has content-opposite optima + /// (film A wants `20`, film B wants `12`, within 1.1 and 4.9 points) so + /// `16` stays, being the only value down on all four columns. + /// + /// THE PRICE, stated: on the SHORT-GOP gate above (12 frames, `gop = + /// 12`, where a mini-GOP of 8 splits into 8 + 4 while 16 is one group) + /// `8:-32:16` reads film A +51.2 / +21.4 and film B +91.0 / +53.5 + /// against `16:-24:16`'s +44.2 / +15.7 and +70.5 / +35.2. The two gates + /// disagree because the best shape depends on the GOP length, and the + /// long one is what the editor exports; a GOP-length-adaptive `mini_gop` + /// is the standing follow-up, not measured here. fn default() -> Self { Self { - mini_gop: 16, - arf_q_offset: -24, + mini_gop: 8, + arf_q_offset: -32, leaf_q_offset: 16, } } diff --git a/lanes/pyr4.sweep.txt b/lanes/pyr4.sweep.txt new file mode 100644 index 00000000..ade9d0bf --- /dev/null +++ b/lanes/pyr4.sweep.txt @@ -0,0 +1,112 @@ +lane-pyr4 -- the coding pyramid decided on a LONG GOP +===================================================== + +Why. main 38753f0a shipped `Pyramid::default` = 16:-24:16 off a sweep of +`encode::tests::bd_rate_screen_native`, which codes 12 frames with gop = 12. +`Av1Encoder::encode_frames` cuts a mini-GOP at every key frame, so ANY +mini_gop over 12 codes the same stream in that gate: the shipped "16" was +measured as "one hidden ARF per 12-picture GOP", never as a real 16-picture +group (class `instrument at bound` / `gate blind to feature`). The user's +exports are long GOPs. + +The instrument. `encode::tests::bd_rate_film_long_gop` (#[ignore], new this +lane): the same native crop, three-way sample-exact decode (encoder / ffmpeg +/ our decoder) and four-quantizer ladder as `bd_rate_screen_native`, over the +two real film rows of `native_gate_clips` only, but 48 pictures with gop = 48 +(`encode_sequence` sets gop = pictures.len(); `external_ladder` passes ffmpeg +`-g 48`). Reference arms, unchanged from the 12-frame gate except that -g: + libaom-av1 -g 48 -cpu-used 6 -b:v 0 -crf {5,20,35,45} + librav1e -g 48 -rav1e-params speed=6:quantizer={50,100,150,200}: + tile_cols=1:tile_rows=1:threads=1 + ours base_q_idx {150,120,90,60}, one tile, one thread +Both wrappers honour -g (measured: a 96-frame clip at -g 48 codes exactly 2 +key frames under librav1e and under libaom-av1). +Crops: film A 1920x768, film B 1920x1024 (the 4K source, centre crop). +Wall per arm, 3 arms in parallel: ours 240-290 s per film row, libaom 33-80 s, +rav1e 51-63 s; ~13-17 minutes per arm for both rows. Raw logs: lanes/pyr4/. + +Every row below is keyed by its own log's "pyramid requested/effective" +print, never by argument order (lane-pyr3 misattributed three rows that way). +Effective == requested on all 11 arms: neither film is gated to flat. + +LONG-GOP TABLE (48 frames, gop 48). BD-rate %, lower is better. + +| mini_gop:arf:leaf | film A vs libaom | film A vs rav1e | film B vs libaom | film B vs rav1e | +|---|---|---|---|---| +| 16:-24:16 (shipped) | +67.4 | +21.0 | +165.5 | +57.6 | +| 32:-24:16 | +79.0 | +29.8 | +164.4 | +58.8 | +| 8:-24:16 | +58.5 | +15.5 | +166.1 | +56.8 | +| 4:-24:16 (probe) | +61.9 | +19.5 | +160.8 | +56.7 | +| 8:-16:16 | +63.7 | +19.0 | +167.6 | +58.6 | +| 8:-32:16 ** SHIPS **| +55.7 | +13.5 | +165.1 | +56.6 | +| 8:-40:16 (probe) | +56.1 | +13.3 | +167.6 | +57.3 | +| 8:-24:12 | +60.2 | +17.3 | +166.8 | +58.0 | +| 8:-24:20 | +57.1 | +14.0 | +166.2 | +56.6 | +| 8:-32:12 | +56.5 | +14.7 | +163.0 | +56.3 | +| 8:-32:20 | +55.4 | +12.9 | +167.9 | +57.4 | + +Axis 1, mini_gop at the shipped offsets -24:16 -- 32 / 16 / 8 / 4. +Film A is strongly ordered 8 < 4 < 16 < 32 (8 is 8.9 / 5.5 points better than +the shipped 16); film B is nearly flat over the whole axis (a 5.3-point span +on the libaom column against a 165-point level, 2.1 on rav1e). 8 was the +bottom of the requested set, so 4 was probed below it (class `search bounded +by its heuristic`): film A turns back up at 4, so 8 is an interior optimum. + +Axis 2, the offsets at mini_gop 8. Requested grid arf {-16,-24,-32} x leaf +{12,16,20}; 7 of the 9 were run plus a -40 probe. arf is the strong axis +(-16 -> -32 is 8.0 points on film A) and -32 is an interior optimum: -40 +turns back up on film A's libaom column and on both of film B's. leaf is the +weak axis (<= 3.1 points on film A, <= 4.9 on film B) and its optima are +content-opposite (film A wants 20, film B wants 12), so 16 stays -- it is the +only leaf value down on all four columns against the shipped shape. +DROPPED, wall vs value: (-16,12) and (-16,20). arf -16 is last on all four +columns at leaf 16 and the leaf axis is a third of the arf axis' size, so +neither corner can reach 8:-32:16. + +DECISION: `Pyramid::default` 16:-24:16 -> 8:-32:16. +Keep rule (both films down on both columns) is met: + film A +67.4 -> +55.7 (-11.7) +21.0 -> +13.5 (-7.5) + film B +165.5 -> +165.1 (-0.4) +57.6 -> +56.6 (-1.0) + +THE PRICE, on the record. The 12-frame gate at the winning shape, +`bd_rate_screen_native` with EC_AV1_PYRAMID=8:-32:16 (all five rows): + +| clip | vs libaom | vs rav1e | wall ours:libaom:rav1e | +|---|---|---|---| +| bars 1080p 1920x1024 | +4.8% | -11.5% | 38.9s:6.7s:16.7s | +| bars 2160p 1920x1024 | +20.5% | -3.4% | 34.5s:5.7s:16.1s | +| film A 1920x768 | +51.2% | +21.4% | 64.6s:9.9s:13.9s | +| film B 1920x1024 | +91.0% | +53.5% | 69.1s:19.1s:15.2s | +| screen capture 1920x1024 (gated flat) | +48.2% | -16.4% | 33.7s:8.8s:12.4s | + +against the shipped 16:-24:16 on the same gate (lanes/pyr3.sweep.txt): +film A +44.2 / +15.7, film B +70.5 / +35.2. So the new shape is 7.0 / 5.7 +worse on film A and 20.5 / 18.3 worse on film B AT A 12-FRAME GOP, where a +mini-GOP of 8 splits into 8 + 4 while 16 is a single group. The two gates +disagree because the best shape depends on GOP LENGTH; the long GOP is what +the editor exports, so it governs. +DEFERRED: a GOP-length-adaptive mini_gop (e.g. the largest power of two that +divides the GOP into whole groups, capped) would take both tables. Not +measured here; unblocked by one more long/short pair of arms once the rule is +written. + +VERIFICATION (lane-pyr4, at 8:-32:16) + pins re-taken: encode::tests::the_encoders_own_streams_are_byte_identical_to + _their_pins 8076 -> 8755 at q=150, 27585 -> 28637 at q=60 (the four-picture + clip codes as one truncated group of 8 with its ARF at q-32). + encode::tests::the_facade_codes_the_same_bytes_as_encode_sequence ok + tile_bytes_do_not_depend_on_the_thread_count (--include-ignored) ok + EC_COMP_MISMATCH=1 over the whole lib suite: 544 passed, 0 mismatch lines + encoder:: --include-ignored --test-threads 1: 27 passed + cargo test -p ec-av1 --release -j4: 544 passed / 0 failed / 43 ignored + cargo check --workspace --all-targets: 0 errors, 0 ec-av1 warnings + (25 warning lines, all pre-existing ec-opus / ec-vorbis) + +PRE-EXISTING, NOT THIS LANE: running the ignored encoder:: tests in PARALLEL +(`--include-ignored encoder:: ...` with the default test-thread count) fails 5 +of them -- "a reference frame selected with no picture at this frame's own +ref_frame_idx slot" and a short dav1d frame count. It reproduces identically +on main's shape (EC_AV1_PYRAMID=16:-24:16, same binary: 5 failures, a superset +of ours), and every one of them passes standalone and at --test-threads 1, so +it is a shared-state/temp-file race between those heavy gates, not a pyramid +defect. Class `PID-keyed temp path`. Log: lanes/pyr4/oldshape-combined.log.