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 {
191 /// one shared controller (the flat path, unchanged from before levels191 /// one shared controller (the flat path, unchanged from before levels
192 /// existed).192 /// existed).
193 per_level: bool,193 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],
194 }198 }
195 199
196 impl RateLoop {200 impl RateLoop {
@@ -209,6 +213,7 @@ impl RateLoop {
209 target: [target_bytes; 3],213 target: [target_bytes; 3],
210 q: [start_q; 3],214 q: [start_q; 3],
211 per_level: false,215 per_level: false,
216+ seeded: [false; 3],
212 };217 };
213 };218 };
214 // A group of `m` pictures gets `m * target_bytes`, split one219 // A group of `m` pictures gets `m * target_bytes`, split one
@@ -219,6 +224,7 @@ impl RateLoop {
219 target: [target_bytes * KEY_WEIGHT, share * ARF_WEIGHT, share],224 target: [target_bytes * KEY_WEIGHT, share * ARF_WEIGHT, share],
220 q: [start_q; 3],225 q: [start_q; 3],
221 per_level: true,226 per_level: true,
227+ seeded: [false; 3],
222 }228 }
223 }229 }
224 230
@@ -249,7 +255,23 @@ impl RateLoop {
249 return;255 return;
250 }256 }
251 let ratio = actual_bytes as f64 / self.target[slot];257 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+ };
253 self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0);275 self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0);
254 }276 }
255 }277 }
@@ -1914,6 +1936,13 @@ mod tests {
1914 /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real1936 /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real
1915 /// clip's full range of content (so a scene cut can't be the one frame1937 /// clip's full range of content (so a scene cut can't be the one frame
1916 /// that breaks the bound).1938 /// 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.
1917 #[test]1946 #[test]
1918 fn bytes_per_frame_controller_never_oscillates_past_its_clamp() {1947 fn bytes_per_frame_controller_never_oscillates_past_its_clamp() {
1919 let Some(pictures) = h264_clip_frames(640, 384, 24) else {1948 let Some(pictures) = h264_clip_frames(640, 384, 24) else {
@@ -1934,17 +1963,31 @@ mod tests {
1934 let mut enc =1963 let mut enc =
1935 Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap();1964 Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap();
1936 let mut prev_q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf);1965 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() {
1938 enc.encode_frames(picture).unwrap();1972 enc.encode_frames(picture).unwrap();
1939 let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf);1973 let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf);
1940 let step = (i32::from(q) - i32::from(prev_q)).abs();1974 let step = (i32::from(q) - i32::from(prev_q)).abs();
1941- assert!(1975+ if step > 0 && !seeded {
1942- f64::from(step) <= RateLoop::STEP_CLAMP + 1.0, // +1 for u8 rounding1976+ seeded = true;
1943- "q stepped from {prev_q} to {q}, past the {} clamp",1977+ seed_frame = Some(frame);
1944- RateLoop::STEP_CLAMP1978+ } else {
1945- );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+ }
1946 prev_q = q;1985 prev_q = q;
1947 }1986 }
1987+ assert!(
1988+ seed_frame.is_some(),
1989+ "the controller never steered at all -- the bound below is vacuous"
1990+ );
1948 }1991 }
1949 1992
1950 fn psnr(a: &[u16], b: &[u16]) -> f64 {1993 fn psnr(a: &[u16], b: &[u16]) -> f64 {