The rate loop seeds each level's quantizer off its first coded frame
Browse files at this commit12e6ab7227a9fd5b75514116ccb3ea5f03b0bf72Parent: a005252- fix
- the rate loop seeds each level's quantizer off its first coded frame
1 changed file +50 -7
- crates/ec-av1/src/encoder.rs +50 -7
crates/ec-av1/src/encoder.rsmodified+50 -7
| @@ -191,6 +191,10 @@ struct RateLoop { | ||
| 191 | 191 | /// one shared controller (the flat path, unchanged from before levels |
| 192 | 192 | /// existed). |
| 193 | 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 | 200 | impl RateLoop { |
| @@ -209,6 +213,7 @@ impl RateLoop { | ||
| 209 | 213 | target: [target_bytes; 3], |
| 210 | 214 | q: [start_q; 3], |
| 211 | 215 | per_level: false, |
| 216 | + seeded: [false; 3], | |
| 212 | 217 | }; |
| 213 | 218 | }; |
| 214 | 219 | // A group of `m` pictures gets `m * target_bytes`, split one |
| @@ -219,6 +224,7 @@ impl RateLoop { | ||
| 219 | 224 | target: [target_bytes * KEY_WEIGHT, share * ARF_WEIGHT, share], |
| 220 | 225 | q: [start_q; 3], |
| 221 | 226 | per_level: true, |
| 227 | + seeded: [false; 3], | |
| 222 | 228 | } |
| 223 | 229 | } |
| 224 | 230 | |
| @@ -249,7 +255,23 @@ impl RateLoop { | ||
| 249 | 255 | return; |
| 250 | 256 | } |
| 251 | 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 | 275 | self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0); |
| 254 | 276 | } |
| 255 | 277 | } |
| @@ -1914,6 +1936,13 @@ mod tests { | ||
| 1914 | 1936 | /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real |
| 1915 | 1937 | /// clip's full range of content (so a scene cut can't be the one frame |
| 1916 | 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 | 1946 | #[test] |
| 1918 | 1947 | fn bytes_per_frame_controller_never_oscillates_past_its_clamp() { |
| 1919 | 1948 | let Some(pictures) = h264_clip_frames(640, 384, 24) else { |
| @@ -1934,17 +1963,31 @@ mod tests { | ||
| 1934 | 1963 | let mut enc = |
| 1935 | 1964 | Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap(); |
| 1936 | 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 | 1972 | enc.encode_frames(picture).unwrap(); |
| 1939 | 1973 | let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf); |
| 1940 | 1974 | 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 | + } | |
| 1946 | 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 | 1993 | fn psnr(a: &[u16], b: &[u16]) -> f64 { |