forge
Explore
Log in

Don't you dare go Hollow

Lane-rlclamp report — the rate loop at its quantizer floor

Browse files at this commit

7af78e609ab187d45723c2abeb7bccdef47fe6f3Ahmet Kaan GümüşParent: 83903ac
  • docs
    • lane-rlclamp report — the rate loop at its quantizer floor
Show full message
docs(av1): lane-rlclamp report — the rate loop at its quantizer floor
lanes/rlclamp.report.mdadded+153 -0
@@ -0,0 +1,153 @@
1+# lane-rlclamp — the pyramid rate loop at its quantizer floor
2+
3+Worktree `edith_codecs-rlclamp`, branch `lane-rlclamp` off main `8d9d056b`.
4+Two fix commits, head `6da9cd06`. Class: **[[vorbis-rate-loop-windup]]** — a
5+flat/clamped spend the loop cannot correct — with a second instance of
6+*the re-plan feeds the level that cannot spend* found and fixed inside it.
7+
8+## 0. The defect, reproduced
9+
10+`encoder::tests::bitrate_target_lands_within_5_percent_over_48_frames`, base
11+`8d9d056b`, census on (`EC_AV1_RL_CENSUS=1`, this lane's new instrument) and
12+the redistribution ablated (`EC_AV1_RL_NOREDIST=1` = the old code):
13+**2 Mbit/s with the pyramid lands −4.8%**, and the 12 hidden frames code the
14+same bytes at 1536 kbit/s and at 2 Mbit/s (22348 B/frame in both) — the anchor
15+is at its floor and the extra 464 kbit/s has nowhere to go.
16+
17+## 1. Census — `EC_AV1_RL_CENSUS=1`
18+
19+One stderr row per frame the loop steers: frame index, level, slot, that
20+level's byte target, coded bytes, the slot's `base q`, the quantizer the frame
21+WANTED (`raw_q` = base + level offset), the one it got (`eff_q`, after the
22+coder's 1..=255 clamp), whether the clamp bound, and the run's remainder.
23+
24+```
25+RLCENSUS n=2 level=Arf slot=1 target=13291 actual=25961 base_q=12.0 raw_q=-8 eff_q=1 bound=true left_bytes=485435 left_w=111.3
26+RLCENSUS n=3 level=Leaf slot=2 target=4246 actual=1265 base_q=21.4 raw_q=112 eff_q=112 bound=false left_bytes=459474 left_w=108.2
27+```
28+
29+Where the clamp binds, per arm (48 pictures, 640x384, gop 48):
30+
31+| arm | frames | bound frames | which |
32+|---|---|---|---|
33+| 384 kbit/s pyramid | 43 steered | 0 | — |
34+| 768 kbit/s pyramid | 48 | 0 | — |
35+| 1536 kbit/s pyramid | 48 | 11 | every hidden frame after the first |
36+| 2 Mbit/s pyramid | 48 | 11 | top ARF `raw_q` −20..−31, mid ARF `raw_q` −8, both coded at `eff_q` 1 |
37+| every flat arm | 48 | 0 | the flat path has no offset, so nothing clamps |
38+
39+**Where the shortfall went (before).** The re-plan divided the run's whole
40+remainder by the whole remaining WEIGHT, and the hidden levels carry 88 of the
41+run's 153 weight units — so most of each frame's unspent bytes were handed
42+straight back to the level that had just failed to spend them. The leaf target
43+sat at 4246 → 4230 → 4376 → 4630 B for the first 33 frames of the 2 Mbit/s run
44+(leaves coding 3.7-5.1 kB against it) and only ran away at the end —
45+15049, 17135, 20335, 24537 B over the last five frames, where the leaf slot's
46+own `q` had reached 0 and it coded 12.7 kB frames it could no longer be paid
47+for. 24 kB of target at frame 46 is the shortfall arriving too late to spend.
48+
49+## 2. The fix
50+
51+`RateLoop` now carries one `LevelPlan` per LEVEL of the run (key, each distinct
52+hidden offset, leaves): its share of the run's weight, how much of that is
53+still to code, whether its last frame was at the coder's bound, and the
54+leaf-equivalent bytes it measured there. `replan` holds every bound level at
55+its MEASURED rate, takes its remaining weight out of the division, and
56+re-solves the scale over the levels that can still move — libaom's
57+`rc_pick_q_and_bounds` carrying `vbr_bits_off_target` against per-frame bounds,
58+in this encoder's own level model. The carry is bounded at BOTH ends
59+(`REPLAN_FLOOR` 0.25x, `REPLAN_CLAMP` 4x a level's opening share) and every key
60+frame resets each level's remaining weight to its whole share, so no debt
61+outlives a run.
62+
63+**Per level and not per slot — the first cut was wrong and its number is
64+here.** Slot 1 mixes the top ARF (offset −32) and the mid ARF (−8): at the
65+floor both code ~24 kB, at weights 4x apart, so the mid's leaf-equivalent rate
66+projected onto the top's remaining weight drove the remainder NEGATIVE, every
67+target to zero, and `update` returns on a non-positive target — the loop stopped
68+dead after three frames and the 1536 kbit/s pyramid arm landed **−20.7%**
69+(`bitrate-after.log`, commit `92fde72a`). The per-level plan plus the floor is
70+what commit `6da9cd06` ships; the floor alone makes a zero target impossible.
71+
72+## 3. Unit test — red then green
73+
74+`encoder::tests::a_bound_level_hands_its_unspent_bytes_to_the_others`: a
75+synthetic per-level model. The key codes its plan exactly, then ONE hidden
76+frame is coded at `raw_q` 0 spending half its plan, and the leaves' target must
77+equal the run's remainder with every remaining frame OF THAT LEVEL priced at
78+the measured rate and its slot-mates still free to follow the plan.
79+
80+* old division (`EC_AV1_RL_NOREDIST=1`): `leaf target 658.415 is not the exact
81+ remainder 1034.458 (naive division would give 658.415)` — **FAILED**
82+* fixed: **ok** (1 passed)
83+
84+Two more arms in the same test bound the carry: hidden levels spending 1 byte
85+each may not push the leaf target past 4x its opening share, and hidden levels
86+spending 40x may not push it below 0.25x (a zero target would stop the loop —
87+the regression above).
88+
89+## 4. The gate, before and after
90+
91+`bitrate_target_lands_within_5_percent_over_48_frames`, all four rates, pyramid
92+off and on. Before = base `8d9d056b` (`EC_AV1_RL_NOREDIST=1`, the old
93+division); after = `6da9cd06`.
94+
95+| rate | pyramid | before | after |
96+|---|---|---|---|
97+| 384 kbit/s | off | 375164 bps (−2.3%) | 375164 bps (−2.3%) |
98+| 384 kbit/s | on | 396884 bps (+3.4%) | 392740 bps (**+2.3%**) |
99+| 768 kbit/s | off | 749204 bps (−2.4%) | 749204 bps (−2.4%) |
100+| 768 kbit/s | on | 768076 bps (+0.0%) | 768076 bps (+0.0%) |
101+| 1536 kbit/s | off | 1490612 bps (−3.0%) | 1490612 bps (−3.0%) |
102+| 1536 kbit/s | on | 1536020 bps (+0.0%) | 1536824 bps (+0.1%) |
103+| 2 Mbit/s | off | 1957592 bps (−2.1%) | 1957592 bps (−2.1%) |
104+| 2 Mbit/s | on | **1904600 bps (−4.8%)** | **2000760 bps (+0.0%)** |
105+
106+Every flat arm is byte-identical (the flat path has one slot, no offsets and no
107+bound, and `replan` returns before any of this). Of the four pyramid arms, two
108+are byte-identical, one moves +1.1 points closer to target (384 kbit/s: the
109+`REPLAN_FLOOR`/`CLAMP` bound its late plan), and the clamped one is fixed. The
110+2 Mbit/s leaves now code 6370 B/frame against a 7100 B target held from frame
111+11 onward, instead of 5683 B against a target that only arrived at frame 43.
112+
113+**The parked tree.** `hold/arftf-merge-c46e7005` (the ARF temporal filter at
114+strength 2) with both fix commits cherry-picked: 2 Mbit/s pyramid
115+**2001028 bps (+0.1%)**, against the −5.1% the defect report read there. All
116+eight of its arms pass (`hold-after.log`).
117+
118+## 5. The rest of the gate — nothing else moved
119+
120+The CQ streams carry NO `RateLoop` at all (`rate_loop: None` unless the caller
121+asks for `BytesPerFrame`/`Bitrate`), so every long-GOP/pin/preset stream is
122+untouched by construction, and the runs confirm it:
123+
124+* `encode::tests::the_encoders_own_streams_are_byte_identical_to_their_pins`
125+ ok — pins `(150, 8218, 0xd533c8ea14caae6e)` and
126+ `(60, 33017, 0xbcedb694dcfe9146)` unchanged, not re-taken.
127+* `encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders`
128+ (`--ignored --exact`): 1 passed.
129+* split suite, `cargo test -p ec-av1 --lib`: s1 (`--skip stream::`) 344 passed
130+ 0 failed 34 ignored RC=0; s2 (`stream:: --skip 10bit`) 202 passed 0 failed
131+ 15 ignored RC=0; s3 (`10bit`) 42 passed 0 failed 1 ignored RC=0.
132+* `cargo check --workspace --all-targets -j4`: RC=0, 0 errors, 0 ec-av1
133+ warnings.
134+
135+EVIDENCE: $HOME/.cache/rlclamp/{bitrate-before,bitrate-after,bitrate-after2,hold-after,s1,s2,s3,preset,check}.log |
136+EC_AV1_RL_CENSUS=1 [EC_AV1_RL_NOREDIST=1] cargo test -p ec-av1 --lib bitrate_target_lands --nocapture, then the split suite + preset + workspace check |
137+2 Mbit/s pyramid 1904600 → 2000760 bps (−4.8% → +0.0%), 8/8 arms inside ±5%, pins unchanged, 588 tests green
138+
139+## 6. Knobs this lane leaves
140+
141+* `EC_AV1_RL_CENSUS=1` — the per-frame rate-loop census above.
142+* `EC_AV1_RL_NOREDIST=1` — the OLD division, kept as the before/after arm: it
143+ is what makes every number in this report reproducible in one binary.
144+
145+## 7. Open
146+
147+* The leaf level has its own floor that this fix does not name: `q[slot]` is
148+ clamped to 0, so a leaf's effective quantizer cannot go below
149+ `leaf_q_offset` (12) and `bound` — which reads the coder's 1..=255 clamp —
150+ never fires for it. It cost nothing here (the leaves reached 6.4 kB/frame at
151+ `eff_q` 21-39, far off that floor), but a target much past 2.4 Mbit/s on this
152+ clip would hit it, and then no redistribution is left: every level is at its
153+ floor and the encoder simply cannot spend more.