forge
Explore
Log in

Don't you dare go Hollow

The rate loop seeds each level's quantizer off its first coded frame

Browse files at this commit

12e6ab7227a9fd5b75514116ccb3ea5f03b0bf72Ahmet Kaan GümüşParent: a005252
  • fix
    • the rate loop seeds each level's quantizer off its first coded frame
Show full message
fix(av1): the rate loop seeds each level's quantizer off its first coded frame

`STEP_CLAMP` protects an operating point the slot has not found yet: every
slot starts at the caller's `base_q_idx` guess (100 in the gate) and the
clamp made the loop WALK to the right quantizer at twelve steps a frame.
Traced over the 48-frame bitrate gate, the pyramid's ARF slot spent five of
its twelve frames pinned at -STEP_CLAMP and the stream landed 10.2% under
target. That is convergence lag, not steady-state offset -- this loop's
fixed point is actual == target exactly -- so the fix is one unclamped
acquisition step per slot, not an accumulated error term: nothing is carried
between frames and the windup bound still holds from the second frame on.

  bits/s   pyramid   before    after
  768000   false     -0.1%     -1.8%
  768000   true      +0.7%     +1.1%
  1536000  false     -3.6%     -3.0%
  1536000  true     -10.2%     -2.7%

bytes_per_frame_controller_never_oscillates_past_its_clamp now exempts the
acquisition step alone (the first frame q moves at all -- the lookahead
queue means that is not frame zero) and asserts the clamp on every frame
after it.
crates/ec-av1/src/encoder.rsmodified+50 -7
@@ -191,6 +191,10 @@ struct RateLoop {
191191 /// one shared controller (the flat path, unchanged from before levels
192192 /// existed).
193193 per_level: bool,
194+ /// Whether each slot has already steered off a real coded frame. The
195+ /// FIRST update of a slot is the seeding one and takes the model's whole
196+ /// step ([`RateLoop::update`]); every later one is clamped.
197+ seeded: [bool; 3],
194198 }
195199
196200 impl RateLoop {
@@ -209,6 +213,7 @@ impl RateLoop {
209213 target: [target_bytes; 3],
210214 q: [start_q; 3],
211215 per_level: false,
216+ seeded: [false; 3],
212217 };
213218 };
214219 // A group of `m` pictures gets `m * target_bytes`, split one
@@ -219,6 +224,7 @@ impl RateLoop {
219224 target: [target_bytes * KEY_WEIGHT, share * ARF_WEIGHT, share],
220225 q: [start_q; 3],
221226 per_level: true,
227+ seeded: [false; 3],
222228 }
223229 }
224230
@@ -249,7 +255,23 @@ impl RateLoop {
249255 return;
250256 }
251257 let ratio = actual_bytes as f64 / self.target[slot];
252- let step = (ratio.ln() * Self::GAIN).clamp(-Self::STEP_CLAMP, Self::STEP_CLAMP);
258+ // `STEP_CLAMP` protects an operating point this slot has not found
259+ // yet: its `q` is still the caller's `base_q_idx` guess, and the
260+ // clamp makes the loop WALK to the right quantizer at twelve steps a
261+ // frame. Measured on the 48-frame bitrate gate, the pyramid's ARF
262+ // slot spent five of its twelve frames pinned at `-STEP_CLAMP` and
263+ // the stream landed -10.2% under target -- convergence lag, not a
264+ // steady-state offset (the fixed point of this loop is `actual ==
265+ // target` exactly). So a slot's FIRST frame takes the model's whole
266+ // step and seeds `q` where the log-linear slope says it belongs; from
267+ // the second frame on the clamp is back, and nothing is accumulated
268+ // between frames, so [[vorbis-rate-loop-windup]]'s bound still holds.
269+ let raw = ratio.ln() * Self::GAIN;
270+ let step = if std::mem::replace(&mut self.seeded[slot], true) {
271+ raw.clamp(-Self::STEP_CLAMP, Self::STEP_CLAMP)
272+ } else {
273+ raw
274+ };
253275 self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0);
254276 }
255277 }
@@ -1914,6 +1936,13 @@ mod tests {
19141936 /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real
19151937 /// clip's full range of content (so a scene cut can't be the one frame
19161938 /// that breaks the bound).
1939+ ///
1940+ /// The SEEDING frame is the one exemption ([`RateLoop::update`]): a slot
1941+ /// that has coded nothing yet is still sitting on the caller's
1942+ /// `base_q_idx` guess, and its first step is the acquisition jump, not
1943+ /// windup. Exactly one frame is exempt -- the loop below asserts the
1944+ /// bound on every frame after it, which is where a scene cut would
1945+ /// break it.
19171946 #[test]
19181947 fn bytes_per_frame_controller_never_oscillates_past_its_clamp() {
19191948 let Some(pictures) = h264_clip_frames(640, 384, 24) else {
@@ -1934,17 +1963,31 @@ mod tests {
19341963 let mut enc =
19351964 Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap();
19361965 let mut prev_q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf);
1937- for picture in &pictures {
1966+ // The first frame this encoder EMITS is several `encode_frames` calls
1967+ // in (the lookahead queue holds the earlier ones), so the seeding
1968+ // step is not frame zero -- it is the first frame `q` moves at all.
1969+ let mut seeded = false;
1970+ let mut seed_frame = None;
1971+ for (frame, picture) in pictures.iter().enumerate() {
19381972 enc.encode_frames(picture).unwrap();
19391973 let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf);
19401974 let step = (i32::from(q) - i32::from(prev_q)).abs();
1941- assert!(
1942- f64::from(step) <= RateLoop::STEP_CLAMP + 1.0, // +1 for u8 rounding
1943- "q stepped from {prev_q} to {q}, past the {} clamp",
1944- RateLoop::STEP_CLAMP
1945- );
1975+ if step > 0 && !seeded {
1976+ seeded = true;
1977+ seed_frame = Some(frame);
1978+ } else {
1979+ assert!(
1980+ f64::from(step) <= RateLoop::STEP_CLAMP + 1.0, // +1 for u8 rounding
1981+ "frame {frame}: q stepped from {prev_q} to {q}, past the {} clamp",
1982+ RateLoop::STEP_CLAMP
1983+ );
1984+ }
19461985 prev_q = q;
19471986 }
1987+ assert!(
1988+ seed_frame.is_some(),
1989+ "the controller never steered at all -- the bound below is vacuous"
1990+ );
19481991 }
19491992
19501993 fn psnr(a: &[u16], b: &[u16]) -> f64 {