ec-av1 rate loop: key frame no longer slams the flat path's shared q
Browse files at this commit4b94175cf2bf40274e4cc39c95d198fa699b3762Parent: 76482df1 changed file +23 -43
- crates/ec-av1/src/encoder/mod.rs +23 -43
crates/ec-av1/src/encoder/mod.rsmodified+23 -43
| @@ -236,10 +236,6 @@ struct RateLoop { | ||
| 236 | 236 | target: [f64; 3], |
| 237 | 237 | /// Quantizer per level, same index. |
| 238 | 238 | q: [f64; 3], |
| 239 | - /// Whether the three slots are actually distinct (pyramid mode) or all | |
| 240 | - /// one shared controller (the flat path, unchanged from before levels | |
| 241 | - /// existed). | |
| 242 | - per_level: bool, | |
| 243 | 239 | /// Whether each slot has already steered off a real coded frame. The |
| 244 | 240 | /// FIRST update of a slot is the seeding one and takes the model's whole |
| 245 | 241 | /// step ([`RateLoop::update`]); every later one is clamped. |
| @@ -299,39 +295,30 @@ impl RateLoop { | ||
| 299 | 295 | const REPLAN_FLOOR: f64 = 0.25; |
| 300 | 296 | |
| 301 | 297 | fn new(target_bytes: f64, start_q: f64, pyramid: Option<Pyramid>, gop: usize) -> Self { |
| 302 | - let Some(p) = pyramid else { | |
| 303 | - return Self { | |
| 304 | - target: [target_bytes; 3], | |
| 305 | - q: [start_q; 3], | |
| 306 | - per_level: false, | |
| 307 | - seeded: [false; 3], | |
| 308 | - leaf_q_offset: 0, | |
| 309 | - key_weight: 1.0, | |
| 310 | - run_weight: 0.0, | |
| 311 | - run_bytes: 0.0, | |
| 312 | - left_weight: 0.0, | |
| 313 | - left_bytes: 0.0, | |
| 314 | - plans: [None; 5], | |
| 315 | - scale0: target_bytes, | |
| 316 | - census: census_on(), | |
| 317 | - coded: 0, | |
| 318 | - }; | |
| 319 | - }; | |
| 320 | 298 | // THE LEVEL MODEL. Every coded frame of the run is priced by its own |
| 321 | 299 | // q offset through `WEIGHT_LEVEL_GAIN`, in units of one leaf, and the |
| 322 | - // run's `gop` frame targets are shared out in those units. The old | |
| 323 | - // model priced a group as "one hidden frame worth `ARF_WEIGHT` = 2 | |
| 324 | - // leaves plus `m - 1` leaves", which was wrong twice over and the two | |
| 325 | - // errors cancelled (lane-pyr6): the group has coded TWO hidden frames | |
| 326 | - // since the mid level landed (four with | |
| 300 | + // run's `gop` frame targets are shared out in those units. The flat | |
| 301 | + // path is the same run with no hidden levels and no offsets, so the | |
| 302 | + // key frame is budgeted against the GOP there too: its overshoot is | |
| 303 | + // re-planned over the leaves, never slammed into their shared `q`. | |
| 304 | + // The old pyramid model priced a group as "one hidden frame worth | |
| 305 | + // `ARF_WEIGHT` = 2 leaves plus `m - 1` leaves", which was wrong twice | |
| 306 | + // over and the two errors cancelled (lane-pyr6): the group has coded | |
| 307 | + // TWO hidden frames since the mid level landed (four with | |
| 327 | 308 | // [`Pyramid::quarter_q_offset`]), and a hidden frame is worth far |
| 328 | 309 | // more than two leaves -- the census reads the top ARF at 24x a leaf |
| 329 | 310 | // and the mid at 5x. Both are fixed here, in one diff. |
| 311 | + let (key_q_offset, leaf_q_offset, hidden, leaves) = match pyramid { | |
| 312 | + Some(p) => { | |
| 313 | + let (hidden, leaves) = gop_shape(p, gop); | |
| 314 | + (p.key_q_offset, p.leaf_q_offset, hidden, leaves) | |
| 315 | + } | |
| 316 | + None => (0, 0, Vec::new(), gop.saturating_sub(1)), | |
| 317 | + }; | |
| 330 | 318 | let w = |offset: i16| { |
| 331 | - ((f64::from(p.leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() | |
| 319 | + ((f64::from(leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() | |
| 332 | 320 | }; |
| 333 | - let (hidden, leaves) = gop_shape(p, gop); | |
| 334 | - let key = KEY_WEIGHT * w(p.key_q_offset); | |
| 321 | + let key = KEY_WEIGHT * w(key_q_offset); | |
| 335 | 322 | let total: f64 = |
| 336 | 323 | key + leaves as f64 + hidden.iter().map(|&(o, n)| n as f64 * w(o)).sum::<f64>(); |
| 337 | 324 | // One leaf's byte target: the run is worth `gop` frame targets and |
| @@ -347,19 +334,18 @@ impl RateLoop { | ||
| 347 | 334 | // One plan per level, in coding order: key, hidden levels (at most |
| 348 | 335 | // three distinct offsets -- top, mid, quarters), leaves. |
| 349 | 336 | let mut plans = [None; 5]; |
| 350 | - plans[0] = Some(LevelPlan::new(0, p.key_q_offset, key)); | |
| 337 | + plans[0] = Some(LevelPlan::new(0, key_q_offset, key)); | |
| 351 | 338 | let mut i = 1; |
| 352 | 339 | for &(o, n) in hidden.iter().take(3) { |
| 353 | 340 | plans[i] = Some(LevelPlan::new(1, o, n as f64 * w(o))); |
| 354 | 341 | i += 1; |
| 355 | 342 | } |
| 356 | - plans[i] = Some(LevelPlan::new(2, p.leaf_q_offset, leaves as f64)); | |
| 343 | + plans[i] = Some(LevelPlan::new(2, leaf_q_offset, leaves as f64)); | |
| 357 | 344 | Self { |
| 358 | 345 | target: [scale * key, scale, scale], |
| 359 | 346 | q: [start_q; 3], |
| 360 | - per_level: true, | |
| 361 | 347 | seeded: [false; 3], |
| 362 | - leaf_q_offset: p.leaf_q_offset, | |
| 348 | + leaf_q_offset, | |
| 363 | 349 | key_weight: key, |
| 364 | 350 | run_weight: total, |
| 365 | 351 | run_bytes: target_bytes * gop.max(1) as f64, |
| @@ -386,12 +372,9 @@ impl RateLoop { | ||
| 386 | 372 | .position(|p| p.is_some_and(|p| p.slot == slot && (slot != 1 || p.offset == offset))) |
| 387 | 373 | } |
| 388 | 374 | |
| 389 | - /// Which controller a level uses: one shared slot on the flat path, three | |
| 390 | - /// separate ones under a pyramid. | |
| 375 | + /// Which controller a level uses: the key, the hidden ARF levels and the | |
| 376 | + /// leaves each steer their own `q`. | |
| 391 | 377 | fn slot(&self, level: Level) -> usize { |
| 392 | - if !self.per_level { | |
| 393 | - return 0; | |
| 394 | - } | |
| 395 | 378 | match level { |
| 396 | 379 | Level::Key => 0, |
| 397 | 380 | Level::Arf => 1, |
| @@ -423,7 +406,7 @@ impl RateLoop { | ||
| 423 | 406 | 1 => self.weight(offset), |
| 424 | 407 | _ => 1.0, |
| 425 | 408 | }; |
| 426 | - let target = match self.per_level && slot == 1 { | |
| 409 | + let target = match slot == 1 { | |
| 427 | 410 | true => self.target[slot] * frame_weight, |
| 428 | 411 | false => self.target[slot], |
| 429 | 412 | }; |
| @@ -495,9 +478,6 @@ impl RateLoop { | ||
| 495 | 478 | frame_weight: f64, |
| 496 | 479 | actual_bytes: f64, |
| 497 | 480 | ) { |
| 498 | - if !self.per_level { | |
| 499 | - return; | |
| 500 | - } | |
| 501 | 481 | if level == Level::Key { |
| 502 | 482 | self.left_weight = self.run_weight; |
| 503 | 483 | self.left_bytes = self.run_bytes; |