Merge branch 'lane-pyr4' (e0a8b8a2): a 48-frame long-GOP film gate; the default pyramid is 8:-32:16, the long-GOP optimum
Browse files at this commit3 changed files +208 -20
- crates/ec-av1/src/encode.rs +53 -11
- crates/ec-av1/src/encoder.rs +43 -9
- lanes/pyr4.sweep.txt +112 -0
crates/ec-av1/src/encode.rsmodified+53 -11
| @@ -13348,14 +13348,14 @@ mod tests { | ||
| 13348 | 13348 | // the blocks that take one code a filter-intra mode, a different |
| 13349 | 13349 | // prediction and different coefficients -- 7321 -> 7373 at q=150 and |
| 13350 | 13350 | // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these. |
| 13351 | - // Re-taken on lane-b64: the inter search offers the whole superblock | |
| 13352 | - // as one 64x64 `PARTITION_NONE` block coded skip ([`B64_ROOT`]), and | |
| 13353 | - // every superblock that takes one codes a single partition symbol, | |
| 13354 | - // one mode/mv chain and no residual where four quadrants used to -- | |
| 13355 | - // 8076 -> 7291 at q=150 and 27585 -> 27466 at q=60. `EC_AV1_B64=0` | |
| 13356 | - // restores these. | |
| 13351 | + // Re-taken on lane-b64 (64x64 skip root, `EC_AV1_B64=0` restores | |
| 13352 | + // 8076 / 27585 under the 16:-24:16 pyramid) and again at the lane-pyr4 | |
| 13353 | + // merge: the default pyramid is `8:-32:16` now, the long-GOP sweep's | |
| 13354 | + // shape (`lanes/pyr4.sweep.txt`), so these four pictures code as one | |
| 13355 | + // truncated group of 8 with its hidden ALTREF at `q-32`. | |
| 13356 | + // `EC_AV1_PYRAMID=16:-24:16` restores 7291 / 27466. | |
| 13357 | 13357 | let pins: [(u8, usize, u64); 2] = |
| 13358 | - [(150, 7291, 0xa411_97e8_8a80_2a1b), (60, 27466, 0xdc87_26a3_c625_46ce)]; | |
| 13358 | + [(150, 8446, 0xaf24_d4bc_c9a3_9819), (60, 28548, 0x1459_61a1_f2b6_0f05)]; | |
| 13359 | 13359 | for (q, bytes, hash) in pins { |
| 13360 | 13360 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 13361 | 13361 | assert_eq!( |
| @@ -14412,17 +14412,59 @@ mod tests { | ||
| 14412 | 14412 | #[test] |
| 14413 | 14413 | #[ignore = "the native-resolution BD arm: minutes per row, needs ffmpeg"] |
| 14414 | 14414 | fn bd_rate_screen_native() { |
| 14415 | + native_bd_arm("bd_rate_screen_native", 12, false); | |
| 14416 | + } | |
| 14417 | + | |
| 14418 | + /// The LONG-GOP arm of the native BD gate: the same recipe as | |
| 14419 | + /// [`bd_rate_screen_native`] over the two real film rows only, but 48 | |
| 14420 | + /// pictures with `gop = 48` (`encode_sequence` sets `gop = | |
| 14421 | + /// pictures.len()`, and `external_ladder` passes ffmpeg `-g 48`, which | |
| 14422 | + /// reaches libaom as `--kf-max-dist=48` and rav1e as | |
| 14423 | + /// `key_frame_interval=48`), so every encoder sees one key frame and 47 | |
| 14424 | + /// inter pictures. | |
| 14425 | + /// | |
| 14426 | + /// WHY IT EXISTS: `bd_rate_screen_native` codes 12 frames with `gop = | |
| 14427 | + /// 12`, and `Av1Encoder::encode_frames` cuts a mini-GOP at every key | |
| 14428 | + /// frame (`encoder.rs`), so ANY [`crate::encoder::Pyramid::mini_gop`] | |
| 14429 | + /// above 12 codes the same stream there -- the shipped `16` was measured | |
| 14430 | + /// as "one hidden ARF per 12-picture GOP", never as a real 16-picture | |
| 14431 | + /// group (class `instrument at bound` / `gate blind to feature`). The | |
| 14432 | + /// user's exports are long GOPs, so the pyramid shape is decided here. | |
| 14433 | + /// | |
| 14434 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 14435 | + /// bd_rate_film_long_gop --nocapture | |
| 14436 | + /// | |
| 14437 | + /// `EC_AV1_NATIVE_FILM=1` keeps only film A, `EC_AV1_NATIVE_FILM4K=1` | |
| 14438 | + /// only film B; `EC_AV1_PYRAMID=<mini_gop>:<arf>:<leaf>` picks the shape. | |
| 14439 | + /// Read every row off the log's own "pyramid requested/effective" print, | |
| 14440 | + /// never off the argument order of the runs (lane-pyr3 misattributed | |
| 14441 | + /// three rows that way). | |
| 14442 | + /// | |
| 14443 | + /// Table: `lanes/pyr4.sweep.txt`. | |
| 14444 | + #[test] | |
| 14445 | + #[ignore = "the long-GOP native BD arm: ~7 minutes per film row, needs ffmpeg"] | |
| 14446 | + fn bd_rate_film_long_gop() { | |
| 14447 | + native_bd_arm("bd_rate_film_long_gop", 48, true); | |
| 14448 | + } | |
| 14449 | + | |
| 14450 | + /// The shared driver of the two native BD arms above: `frames` pictures | |
| 14451 | + /// per row (the sequence's `gop` is the same number, so one key frame), | |
| 14452 | + /// four quantizers, `films_only` dropping every row but the two real | |
| 14453 | + /// films. | |
| 14454 | + fn native_bd_arm(label: &str, frames: usize, films_only: bool) { | |
| 14415 | 14455 | if !have_ffmpeg() { |
| 14416 | - eprintln!("SKIP bd_rate_screen_native: no ffmpeg"); | |
| 14456 | + eprintln!("SKIP {label}: no ffmpeg"); | |
| 14417 | 14457 | return; |
| 14418 | 14458 | } |
| 14419 | - let clips = native_gate_clips(); | |
| 14459 | + let mut clips = native_gate_clips(); | |
| 14460 | + if films_only { | |
| 14461 | + clips.retain(|(name, _, _)| name.starts_with("film ")); | |
| 14462 | + } | |
| 14420 | 14463 | if clips.is_empty() { |
| 14421 | - eprintln!("SKIP bd_rate_screen_native: no clip"); | |
| 14464 | + eprintln!("SKIP {label}: no clip"); | |
| 14422 | 14465 | return; |
| 14423 | 14466 | } |
| 14424 | 14467 | |
| 14425 | - let frames = 12usize; | |
| 14426 | 14468 | let aom_points: Vec<Vec<String>> = [5, 20, 35, 45] |
| 14427 | 14469 | .iter() |
| 14428 | 14470 | .map(|q| { |
crates/ec-av1/src/encoder.rsmodified+43 -9
| @@ -585,18 +585,52 @@ impl Default for Pyramid { | ||
| 585 | 585 | /// are minimal and the bound is not the answer. Deeper ARF is the same |
| 586 | 586 | /// shape -- `-32` buys 0.6 on film A and gives 0.7 back on film B. |
| 587 | 587 | /// |
| 588 | - /// TWO THINGS THIS SWEEP DOES NOT SAY. The mini-GOP axis is measured | |
| 588 | + /// WHAT THAT SWEEP COULD NOT SAY, and the LONG-GOP sweep that answers it | |
| 589 | + /// (lane-pyr4, `lanes/pyr4.sweep.txt`). The table above is measured | |
| 589 | 590 | /// against a gate that codes 12 frames with `gop = 12`, and a group is |
| 590 | - /// cut at every key frame (`encode_frames`), so `16` here is really | |
| 591 | - /// "one hidden ARF per GOP" and any mini-GOP over 12 codes the same | |
| 592 | - /// stream; on a stream with a longer GOP `16` is a genuine 16-picture | |
| 593 | - /// group, which this gate cannot measure. And the offsets were swept at | |
| 594 | - /// mini-GOP 8 before 16 won the first axis; the two joint corners at 16 | |
| 595 | - /// confirm the same gradient, but the full grid at 16 is not measured. | |
| 591 | + /// cut at every key frame (`encode_frames`), so `16` there is really | |
| 592 | + /// "one hidden ARF per GOP" and ANY mini-GOP over 12 codes the same | |
| 593 | + /// stream (class `instrument at bound`). The user's exports are long | |
| 594 | + /// GOPs, so the shape is decided on | |
| 595 | + /// [`crate::encode::tests::bd_rate_film_long_gop`] -- 48 pictures, `gop | |
| 596 | + /// = 48`, the two real films, four quantizers, vs libaom `cpu-used 6` | |
| 597 | + /// and rav1e `speed=6` at the same key interval: | |
| 598 | + /// | |
| 599 | + /// | mini_gop:arf:leaf | film A vs libaom / rav1e | film B vs libaom / rav1e | | |
| 600 | + /// |---|---|---| | |
| 601 | + /// | 32:-24:16 | +79.0 / +29.8 | +164.4 / +58.8 | | |
| 602 | + /// | 16:-24:16 (was the default) | +67.4 / +21.0 | +165.5 / +57.6 | | |
| 603 | + /// | 8:-16:16 | +63.7 / +19.0 | +167.6 / +58.6 | | |
| 604 | + /// | 4:-24:16 | +61.9 / +19.5 | +160.8 / +56.7 | | |
| 605 | + /// | 8:-24:16 | +58.5 / +15.5 | +166.1 / +56.8 | | |
| 606 | + /// | 8:-24:12 | +60.2 / +17.3 | +166.8 / +58.0 | | |
| 607 | + /// | 8:-24:20 | +57.1 / +14.0 | +166.2 / +56.6 | | |
| 608 | + /// | 8:-40:16 | +56.1 / +13.3 | +167.6 / +57.3 | | |
| 609 | + /// | 8:-32:12 | +56.5 / +14.7 | +163.0 / +56.3 | | |
| 610 | + /// | 8:-32:20 | +55.4 / +12.9 | +167.9 / +57.4 | | |
| 611 | + /// | **8:-32:16** | **+55.7 / +13.5** | **+165.1 / +56.6** | | |
| 612 | + /// | |
| 613 | + /// `8:-32:16` ships: against the old `16:-24:16` it takes 11.7 / 7.5 | |
| 614 | + /// points off film A and 0.4 / 1.0 off film B -- all four columns down. | |
| 615 | + /// A real 16- or 32-picture group is WORSE than an 8-picture one on film | |
| 616 | + /// A once the GOP is long enough for the distinction to exist, which the | |
| 617 | + /// 12-frame gate could not see; `4` was probed below the swept edge and | |
| 618 | + /// turns back up on film A. ARF `-32` is an interior optimum (`-40` | |
| 619 | + /// turns back on both films). The leaf axis has content-opposite optima | |
| 620 | + /// (film A wants `20`, film B wants `12`, within 1.1 and 4.9 points) so | |
| 621 | + /// `16` stays, being the only value down on all four columns. | |
| 622 | + /// | |
| 623 | + /// THE PRICE, stated: on the SHORT-GOP gate above (12 frames, `gop = | |
| 624 | + /// 12`, where a mini-GOP of 8 splits into 8 + 4 while 16 is one group) | |
| 625 | + /// `8:-32:16` reads film A +51.2 / +21.4 and film B +91.0 / +53.5 | |
| 626 | + /// against `16:-24:16`'s +44.2 / +15.7 and +70.5 / +35.2. The two gates | |
| 627 | + /// disagree because the best shape depends on the GOP length, and the | |
| 628 | + /// long one is what the editor exports; a GOP-length-adaptive `mini_gop` | |
| 629 | + /// is the standing follow-up, not measured here. | |
| 596 | 630 | fn default() -> Self { |
| 597 | 631 | Self { |
| 598 | - mini_gop: 16, | |
| 599 | - arf_q_offset: -24, | |
| 632 | + mini_gop: 8, | |
| 633 | + arf_q_offset: -32, | |
| 600 | 634 | leaf_q_offset: 16, |
| 601 | 635 | } |
| 602 | 636 | } |
lanes/pyr4.sweep.txtadded+112 -0
| @@ -0,0 +1,112 @@ | ||
| 1 | +lane-pyr4 -- the coding pyramid decided on a LONG GOP | |
| 2 | +===================================================== | |
| 3 | + | |
| 4 | +Why. main 38753f0a shipped `Pyramid::default` = 16:-24:16 off a sweep of | |
| 5 | +`encode::tests::bd_rate_screen_native`, which codes 12 frames with gop = 12. | |
| 6 | +`Av1Encoder::encode_frames` cuts a mini-GOP at every key frame, so ANY | |
| 7 | +mini_gop over 12 codes the same stream in that gate: the shipped "16" was | |
| 8 | +measured as "one hidden ARF per 12-picture GOP", never as a real 16-picture | |
| 9 | +group (class `instrument at bound` / `gate blind to feature`). The user's | |
| 10 | +exports are long GOPs. | |
| 11 | + | |
| 12 | +The instrument. `encode::tests::bd_rate_film_long_gop` (#[ignore], new this | |
| 13 | +lane): the same native crop, three-way sample-exact decode (encoder / ffmpeg | |
| 14 | +/ our decoder) and four-quantizer ladder as `bd_rate_screen_native`, over the | |
| 15 | +two real film rows of `native_gate_clips` only, but 48 pictures with gop = 48 | |
| 16 | +(`encode_sequence` sets gop = pictures.len(); `external_ladder` passes ffmpeg | |
| 17 | +`-g 48`). Reference arms, unchanged from the 12-frame gate except that -g: | |
| 18 | + libaom-av1 -g 48 -cpu-used 6 -b:v 0 -crf {5,20,35,45} | |
| 19 | + librav1e -g 48 -rav1e-params speed=6:quantizer={50,100,150,200}: | |
| 20 | + tile_cols=1:tile_rows=1:threads=1 | |
| 21 | + ours base_q_idx {150,120,90,60}, one tile, one thread | |
| 22 | +Both wrappers honour -g (measured: a 96-frame clip at -g 48 codes exactly 2 | |
| 23 | +key frames under librav1e and under libaom-av1). | |
| 24 | +Crops: film A 1920x768, film B 1920x1024 (the 4K source, centre crop). | |
| 25 | +Wall per arm, 3 arms in parallel: ours 240-290 s per film row, libaom 33-80 s, | |
| 26 | +rav1e 51-63 s; ~13-17 minutes per arm for both rows. Raw logs: lanes/pyr4/. | |
| 27 | + | |
| 28 | +Every row below is keyed by its own log's "pyramid requested/effective" | |
| 29 | +print, never by argument order (lane-pyr3 misattributed three rows that way). | |
| 30 | +Effective == requested on all 11 arms: neither film is gated to flat. | |
| 31 | + | |
| 32 | +LONG-GOP TABLE (48 frames, gop 48). BD-rate %, lower is better. | |
| 33 | + | |
| 34 | +| mini_gop:arf:leaf | film A vs libaom | film A vs rav1e | film B vs libaom | film B vs rav1e | | |
| 35 | +|---|---|---|---|---| | |
| 36 | +| 16:-24:16 (shipped) | +67.4 | +21.0 | +165.5 | +57.6 | | |
| 37 | +| 32:-24:16 | +79.0 | +29.8 | +164.4 | +58.8 | | |
| 38 | +| 8:-24:16 | +58.5 | +15.5 | +166.1 | +56.8 | | |
| 39 | +| 4:-24:16 (probe) | +61.9 | +19.5 | +160.8 | +56.7 | | |
| 40 | +| 8:-16:16 | +63.7 | +19.0 | +167.6 | +58.6 | | |
| 41 | +| 8:-32:16 ** SHIPS **| +55.7 | +13.5 | +165.1 | +56.6 | | |
| 42 | +| 8:-40:16 (probe) | +56.1 | +13.3 | +167.6 | +57.3 | | |
| 43 | +| 8:-24:12 | +60.2 | +17.3 | +166.8 | +58.0 | | |
| 44 | +| 8:-24:20 | +57.1 | +14.0 | +166.2 | +56.6 | | |
| 45 | +| 8:-32:12 | +56.5 | +14.7 | +163.0 | +56.3 | | |
| 46 | +| 8:-32:20 | +55.4 | +12.9 | +167.9 | +57.4 | | |
| 47 | + | |
| 48 | +Axis 1, mini_gop at the shipped offsets -24:16 -- 32 / 16 / 8 / 4. | |
| 49 | +Film A is strongly ordered 8 < 4 < 16 < 32 (8 is 8.9 / 5.5 points better than | |
| 50 | +the shipped 16); film B is nearly flat over the whole axis (a 5.3-point span | |
| 51 | +on the libaom column against a 165-point level, 2.1 on rav1e). 8 was the | |
| 52 | +bottom of the requested set, so 4 was probed below it (class `search bounded | |
| 53 | +by its heuristic`): film A turns back up at 4, so 8 is an interior optimum. | |
| 54 | + | |
| 55 | +Axis 2, the offsets at mini_gop 8. Requested grid arf {-16,-24,-32} x leaf | |
| 56 | +{12,16,20}; 7 of the 9 were run plus a -40 probe. arf is the strong axis | |
| 57 | +(-16 -> -32 is 8.0 points on film A) and -32 is an interior optimum: -40 | |
| 58 | +turns back up on film A's libaom column and on both of film B's. leaf is the | |
| 59 | +weak axis (<= 3.1 points on film A, <= 4.9 on film B) and its optima are | |
| 60 | +content-opposite (film A wants 20, film B wants 12), so 16 stays -- it is the | |
| 61 | +only leaf value down on all four columns against the shipped shape. | |
| 62 | +DROPPED, wall vs value: (-16,12) and (-16,20). arf -16 is last on all four | |
| 63 | +columns at leaf 16 and the leaf axis is a third of the arf axis' size, so | |
| 64 | +neither corner can reach 8:-32:16. | |
| 65 | + | |
| 66 | +DECISION: `Pyramid::default` 16:-24:16 -> 8:-32:16. | |
| 67 | +Keep rule (both films down on both columns) is met: | |
| 68 | + film A +67.4 -> +55.7 (-11.7) +21.0 -> +13.5 (-7.5) | |
| 69 | + film B +165.5 -> +165.1 (-0.4) +57.6 -> +56.6 (-1.0) | |
| 70 | + | |
| 71 | +THE PRICE, on the record. The 12-frame gate at the winning shape, | |
| 72 | +`bd_rate_screen_native` with EC_AV1_PYRAMID=8:-32:16 (all five rows): | |
| 73 | + | |
| 74 | +| clip | vs libaom | vs rav1e | wall ours:libaom:rav1e | | |
| 75 | +|---|---|---|---| | |
| 76 | +| bars 1080p 1920x1024 | +4.8% | -11.5% | 38.9s:6.7s:16.7s | | |
| 77 | +| bars 2160p 1920x1024 | +20.5% | -3.4% | 34.5s:5.7s:16.1s | | |
| 78 | +| film A 1920x768 | +51.2% | +21.4% | 64.6s:9.9s:13.9s | | |
| 79 | +| film B 1920x1024 | +91.0% | +53.5% | 69.1s:19.1s:15.2s | | |
| 80 | +| screen capture 1920x1024 (gated flat) | +48.2% | -16.4% | 33.7s:8.8s:12.4s | | |
| 81 | + | |
| 82 | +against the shipped 16:-24:16 on the same gate (lanes/pyr3.sweep.txt): | |
| 83 | +film A +44.2 / +15.7, film B +70.5 / +35.2. So the new shape is 7.0 / 5.7 | |
| 84 | +worse on film A and 20.5 / 18.3 worse on film B AT A 12-FRAME GOP, where a | |
| 85 | +mini-GOP of 8 splits into 8 + 4 while 16 is a single group. The two gates | |
| 86 | +disagree because the best shape depends on GOP LENGTH; the long GOP is what | |
| 87 | +the editor exports, so it governs. | |
| 88 | +DEFERRED: a GOP-length-adaptive mini_gop (e.g. the largest power of two that | |
| 89 | +divides the GOP into whole groups, capped) would take both tables. Not | |
| 90 | +measured here; unblocked by one more long/short pair of arms once the rule is | |
| 91 | +written. | |
| 92 | + | |
| 93 | +VERIFICATION (lane-pyr4, at 8:-32:16) | |
| 94 | + pins re-taken: encode::tests::the_encoders_own_streams_are_byte_identical_to | |
| 95 | + _their_pins 8076 -> 8755 at q=150, 27585 -> 28637 at q=60 (the four-picture | |
| 96 | + clip codes as one truncated group of 8 with its ARF at q-32). | |
| 97 | + encode::tests::the_facade_codes_the_same_bytes_as_encode_sequence ok | |
| 98 | + tile_bytes_do_not_depend_on_the_thread_count (--include-ignored) ok | |
| 99 | + EC_COMP_MISMATCH=1 over the whole lib suite: 544 passed, 0 mismatch lines | |
| 100 | + encoder:: --include-ignored --test-threads 1: 27 passed | |
| 101 | + cargo test -p ec-av1 --release -j4: 544 passed / 0 failed / 43 ignored | |
| 102 | + cargo check --workspace --all-targets: 0 errors, 0 ec-av1 warnings | |
| 103 | + (25 warning lines, all pre-existing ec-opus / ec-vorbis) | |
| 104 | + | |
| 105 | +PRE-EXISTING, NOT THIS LANE: running the ignored encoder:: tests in PARALLEL | |
| 106 | +(`--include-ignored encoder:: ...` with the default test-thread count) fails 5 | |
| 107 | +of them -- "a reference frame selected with no picture at this frame's own | |
| 108 | +ref_frame_idx slot" and a short dav1d frame count. It reproduces identically | |
| 109 | +on main's shape (EC_AV1_PYRAMID=16:-24:16, same binary: 5 failures, a superset | |
| 110 | +of ours), and every one of them passes standalone and at --test-threads 1, so | |
| 111 | +it is a shared-state/temp-file race between those heavy gates, not a pyramid | |
| 112 | +defect. Class `PID-keyed temp path`. Log: lanes/pyr4/oldshape-combined.log. |