Price a bound rate-loop level per LEVEL, not per controller slot
Browse files at this commit83903acdfe71b917d128b8ab1b33b835779e2378Parent: 366d5d6- fix
- price a bound rate-loop level per LEVEL, not per controller slot
1 changed file +99 -37
- crates/ec-av1/src/encoder.rs +99 -37
crates/ec-av1/src/encoder.rsmodified+99 -37
| @@ -186,6 +186,29 @@ fn census_on() -> bool { | |||
| 186 | std::env::var("EC_AV1_RL_CENSUS").is_ok_and(|v| v == "1") | 186 | std::env::var("EC_AV1_RL_CENSUS").is_ok_and(|v| v == "1") |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | +/// One level of a run, as the re-plan sees it: how much of the run's weight | ||
| 190 | +/// it carries, how much of that is still to be coded, and -- once its | ||
| 191 | +/// effective quantizer has hit the coder's 1..=255 bound -- the leaf- | ||
| 192 | +/// equivalent bytes it actually spends there. | ||
| 193 | +#[derive(Debug, Clone, Copy)] | ||
| 194 | +struct LevelPlan { | ||
| 195 | + /// The controller slot this level shares ([`RateLoop::slot`]). | ||
| 196 | + slot: usize, | ||
| 197 | + /// The level's own `base_q_idx` offset: what tells two hidden levels | ||
| 198 | + /// apart inside slot 1. | ||
| 199 | + offset: i16, | ||
| 200 | + weight: f64, | ||
| 201 | + left: f64, | ||
| 202 | + bound: bool, | ||
| 203 | + rate: f64, | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +impl LevelPlan { | ||
| 207 | + fn new(slot: usize, offset: i16, weight: f64) -> Self { | ||
| 208 | + Self { slot, offset, weight, left: weight, bound: false, rate: 0.0 } | ||
| 209 | + } | ||
| 210 | +} | ||
| 211 | + | ||
| 189 | /// The closed-loop controller behind [`RateTarget::BytesPerFrame`] and | 212 | /// The closed-loop controller behind [`RateTarget::BytesPerFrame`] and |
| 190 | /// [`RateTarget::Bitrate`]: a proportional step on `base_q_idx`, sized from | 213 | /// [`RateTarget::Bitrate`]: a proportional step on `base_q_idx`, sized from |
| 191 | /// the calibration sweep's own log-linear slope (bytes roughly halve every | 214 | /// the calibration sweep's own log-linear slope (bytes roughly halve every |
| @@ -228,17 +251,16 @@ struct RateLoop { | |||
| 228 | /// What is left of both, ahead of the next frame. | 251 | /// What is left of both, ahead of the next frame. |
| 229 | left_weight: f64, | 252 | left_weight: f64, |
| 230 | left_bytes: f64, | 253 | left_bytes: f64, |
| 231 | - /// The same weights split PER SLOT ([`RateLoop::slot`]): the run's, and | 254 | + /// The run's shape, ONE ENTRY PER LEVEL: the key, each distinct hidden |
| 232 | - /// what is left of it. A level whose effective quantizer has hit a bound | 255 | + /// offset, the leaves. A level whose effective quantizer has hit a bound |
| 233 | /// cannot follow the plan, so [`RateLoop::replan`] prices its remaining | 256 | /// cannot follow the plan, so [`RateLoop::replan`] prices its remaining |
| 234 | /// weight at what it MEASURABLY spends and re-solves the run's budget | 257 | /// weight at what it MEASURABLY spends and re-solves the run's budget |
| 235 | - /// over the levels that can still move. | 258 | + /// over the levels that can still move. Per LEVEL and not per slot: the |
| 236 | - run_weight_slot: [f64; 3], | 259 | + /// hidden slot mixes the top and mid ARFs, whose leaf-equivalent rates |
| 237 | - left_weight_slot: [f64; 3], | 260 | + /// differ by 4x at the floor (both code the same bytes at `q_idx` 1, at |
| 238 | - /// Per slot: whether its last coded frame's `base_q_idx + offset` hit the | 261 | + /// weights that differ by that much), and projecting one onto the other's |
| 239 | - /// 1..=255 bound, and the leaf-equivalent bytes it spends while it does. | 262 | + /// remaining weight drove the whole plan negative. |
| 240 | - bound: [bool; 3], | 263 | + plans: [Option<LevelPlan>; 5], |
| 241 | - bound_rate: [f64; 3], | ||
| 242 | /// The plan's scale at construction — the WINDUP BOUND on the re-plan: | 264 | /// The plan's scale at construction — the WINDUP BOUND on the re-plan: |
| 243 | /// a level held at its bound hands its unspent bytes to the others, but | 265 | /// a level held at its bound hands its unspent bytes to the others, but |
| 244 | /// no level's target may exceed this many times its own opening share. | 266 | /// no level's target may exceed this many times its own opening share. |
| @@ -262,6 +284,12 @@ impl RateLoop { | |||
| 262 | /// its opening share. Without it a run whose hidden levels are all at the | 284 | /// its opening share. Without it a run whose hidden levels are all at the |
| 263 | /// floor would keep piling the whole run's budget onto the leaves. | 285 | /// floor would keep piling the whole run's budget onto the leaves. |
| 264 | const REPLAN_CLAMP: f64 = 4.0; | 286 | const REPLAN_CLAMP: f64 = 4.0; |
| 287 | + /// The same bound the other way: a re-plan may not drive a level's target | ||
| 288 | + /// below this multiple of its opening share. A target of zero would STOP | ||
| 289 | + /// the loop outright ([`RateLoop::update`] returns on a non-positive | ||
| 290 | + /// target), which is how the first cut of this fix froze every level of a | ||
| 291 | + /// run at whatever quantizer it had reached. | ||
| 292 | + const REPLAN_FLOOR: f64 = 0.25; | ||
| 265 | 293 | ||
| 266 | fn new(target_bytes: f64, start_q: f64, pyramid: Option<Pyramid>, gop: usize) -> Self { | 294 | fn new(target_bytes: f64, start_q: f64, pyramid: Option<Pyramid>, gop: usize) -> Self { |
| 267 | let Some(p) = pyramid else { | 295 | let Some(p) = pyramid else { |
| @@ -276,10 +304,7 @@ impl RateLoop { | |||
| 276 | run_bytes: 0.0, | 304 | run_bytes: 0.0, |
| 277 | left_weight: 0.0, | 305 | left_weight: 0.0, |
| 278 | left_bytes: 0.0, | 306 | left_bytes: 0.0, |
| 279 | - run_weight_slot: [0.0; 3], | 307 | + plans: [None; 5], |
| 280 | - left_weight_slot: [0.0; 3], | ||
| 281 | - bound: [false; 3], | ||
| 282 | - bound_rate: [0.0; 3], | ||
| 283 | scale0: target_bytes, | 308 | scale0: target_bytes, |
| 284 | census: census_on(), | 309 | census: census_on(), |
| 285 | coded: 0, | 310 | coded: 0, |
| @@ -313,7 +338,16 @@ impl RateLoop { | |||
| 313 | // `w(offset)`. Measuring each hidden frame against its OWN predicted | 338 | // `w(offset)`. Measuring each hidden frame against its OWN predicted |
| 314 | // size is what keeps the shared slot from oscillating between the | 339 | // size is what keeps the shared slot from oscillating between the |
| 315 | // levels it mixes. | 340 | // levels it mixes. |
| 316 | - let hidden_weight: f64 = hidden.iter().map(|&(o, n)| n as f64 * w(o)).sum(); | 341 | + // One plan per level, in coding order: key, hidden levels (at most |
| 342 | + // three distinct offsets -- top, mid, quarters), leaves. | ||
| 343 | + let mut plans = [None; 5]; | ||
| 344 | + plans[0] = Some(LevelPlan::new(0, p.key_q_offset, key)); | ||
| 345 | + let mut i = 1; | ||
| 346 | + for &(o, n) in hidden.iter().take(3) { | ||
| 347 | + plans[i] = Some(LevelPlan::new(1, o, n as f64 * w(o))); | ||
| 348 | + i += 1; | ||
| 349 | + } | ||
| 350 | + plans[i] = Some(LevelPlan::new(2, p.leaf_q_offset, leaves as f64)); | ||
| 317 | Self { | 351 | Self { |
| 318 | target: [scale * key, scale, scale], | 352 | target: [scale * key, scale, scale], |
| 319 | q: [start_q; 3], | 353 | q: [start_q; 3], |
| @@ -325,10 +359,7 @@ impl RateLoop { | |||
| 325 | run_bytes: target_bytes * gop.max(1) as f64, | 359 | run_bytes: target_bytes * gop.max(1) as f64, |
| 326 | left_weight: total, | 360 | left_weight: total, |
| 327 | left_bytes: target_bytes * gop.max(1) as f64, | 361 | left_bytes: target_bytes * gop.max(1) as f64, |
| 328 | - run_weight_slot: [key, hidden_weight, leaves as f64], | 362 | + plans, |
| 329 | - left_weight_slot: [key, hidden_weight, leaves as f64], | ||
| 330 | - bound: [false; 3], | ||
| 331 | - bound_rate: [0.0; 3], | ||
| 332 | scale0: scale, | 363 | scale0: scale, |
| 333 | census: census_on(), | 364 | census: census_on(), |
| 334 | coded: 0, | 365 | coded: 0, |
| @@ -341,6 +372,14 @@ impl RateLoop { | |||
| 341 | ((f64::from(self.leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() | 372 | ((f64::from(self.leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() |
| 342 | } | 373 | } |
| 343 | 374 | ||
| 375 | + /// Which [`LevelPlan`] a coded frame belongs to. Only the hidden slot | ||
| 376 | + /// holds more than one level, so only there does the offset decide. | ||
| 377 | + fn plan_of(&self, slot: usize, offset: i16) -> Option<usize> { | ||
| 378 | + self.plans | ||
| 379 | + .iter() | ||
| 380 | + .position(|p| p.is_some_and(|p| p.slot == slot && (slot != 1 || p.offset == offset))) | ||
| 381 | + } | ||
| 382 | + | ||
| 344 | /// Which controller a level uses: one shared slot on the flat path, three | 383 | /// Which controller a level uses: one shared slot on the flat path, three |
| 345 | /// separate ones under a pyramid. | 384 | /// separate ones under a pyramid. |
| 346 | fn slot(&self, level: Level) -> usize { | 385 | fn slot(&self, level: Level) -> usize { |
| @@ -401,8 +440,11 @@ impl RateLoop { | |||
| 401 | raw | 440 | raw |
| 402 | }; | 441 | }; |
| 403 | self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0); | 442 | self.q[slot] = (self.q[slot] + step).clamp(0.0, 255.0); |
| 404 | - self.bound[slot] = !(1..=255).contains(&raw_q); | 443 | + let bound = !(1..=255).contains(&raw_q); |
| 405 | - self.bound_rate[slot] = actual_bytes as f64 / frame_weight.max(f64::EPSILON); | 444 | + if let Some(plan) = self.plan_of(slot, offset).and_then(|i| self.plans[i].as_mut()) { |
| 445 | + plan.bound = bound; | ||
| 446 | + plan.rate = actual_bytes as f64 / frame_weight.max(f64::EPSILON); | ||
| 447 | + } | ||
| 406 | if self.census { | 448 | if self.census { |
| 407 | eprintln!( | 449 | eprintln!( |
| 408 | "RLCENSUS n={} level={level:?} slot={slot} target={target:.0} actual={actual_bytes} \ | 450 | "RLCENSUS n={} level={level:?} slot={slot} target={target:.0} actual={actual_bytes} \ |
| @@ -410,13 +452,13 @@ impl RateLoop { | |||
| 410 | self.coded, | 452 | self.coded, |
| 411 | self.q[slot], | 453 | self.q[slot], |
| 412 | raw_q.clamp(1, 255), | 454 | raw_q.clamp(1, 255), |
| 413 | - self.bound[slot], | 455 | + bound, |
| 414 | self.left_bytes, | 456 | self.left_bytes, |
| 415 | self.left_weight, | 457 | self.left_weight, |
| 416 | ); | 458 | ); |
| 417 | self.coded += 1; | 459 | self.coded += 1; |
| 418 | } | 460 | } |
| 419 | - self.replan(level, slot, frame_weight, actual_bytes as f64); | 461 | + self.replan(level, slot, offset, frame_weight, actual_bytes as f64); |
| 420 | } | 462 | } |
| 421 | 463 | ||
| 422 | /// Re-plans the REST OF THE RUN from what it has actually spent: the | 464 | /// Re-plans the REST OF THE RUN from what it has actually spent: the |
| @@ -436,18 +478,22 @@ impl RateLoop { | |||
| 436 | /// clamped proportional step, the re-plan only moves the TARGET the next | 478 | /// clamped proportional step, the re-plan only moves the TARGET the next |
| 437 | /// frame is measured against, and every key frame resets the whole plan, | 479 | /// frame is measured against, and every key frame resets the whole plan, |
| 438 | /// so no debt outlives one run. | 480 | /// so no debt outlives one run. |
| 439 | - fn replan(&mut self, level: Level, slot: usize, frame_weight: f64, actual_bytes: f64) { | 481 | + fn replan(&mut self, level: Level, slot: usize, offset: i16, frame_weight: f64, actual_bytes: f64) { |
| 440 | if !self.per_level { | 482 | if !self.per_level { |
| 441 | return; | 483 | return; |
| 442 | } | 484 | } |
| 443 | if level == Level::Key { | 485 | if level == Level::Key { |
| 444 | self.left_weight = self.run_weight; | 486 | self.left_weight = self.run_weight; |
| 445 | self.left_bytes = self.run_bytes; | 487 | self.left_bytes = self.run_bytes; |
| 446 | - self.left_weight_slot = self.run_weight_slot; | 488 | + for plan in self.plans.iter_mut().flatten() { |
| 489 | + plan.left = plan.weight; | ||
| 490 | + } | ||
| 447 | } | 491 | } |
| 448 | self.left_weight -= frame_weight; | 492 | self.left_weight -= frame_weight; |
| 449 | self.left_bytes -= actual_bytes; | 493 | self.left_bytes -= actual_bytes; |
| 450 | - self.left_weight_slot[slot] = (self.left_weight_slot[slot] - frame_weight).max(0.0); | 494 | + if let Some(plan) = self.plan_of(slot, offset).and_then(|i| self.plans[i].as_mut()) { |
| 495 | + plan.left = (plan.left - frame_weight).max(0.0); | ||
| 496 | + } | ||
| 451 | // The tail of a run is not re-planned: under a leaf's worth of weight | 497 | // The tail of a run is not re-planned: under a leaf's worth of weight |
| 452 | // left, the division is noise. | 498 | // left, the division is noise. |
| 453 | if self.left_weight <= 1.0 { | 499 | if self.left_weight <= 1.0 { |
| @@ -469,18 +515,19 @@ impl RateLoop { | |||
| 469 | return; | 515 | return; |
| 470 | } | 516 | } |
| 471 | let (mut bytes, mut weight) = (self.left_bytes, 0.0); | 517 | let (mut bytes, mut weight) = (self.left_bytes, 0.0); |
| 472 | - for s in 0..3 { | 518 | + for plan in self.plans.iter().flatten() { |
| 473 | - if self.bound[s] { | 519 | + if plan.bound { |
| 474 | - bytes -= self.bound_rate[s] * self.left_weight_slot[s]; | 520 | + bytes -= plan.rate * plan.left; |
| 475 | } else { | 521 | } else { |
| 476 | - weight += self.left_weight_slot[s]; | 522 | + weight += plan.left; |
| 477 | } | 523 | } |
| 478 | } | 524 | } |
| 479 | if weight <= 1.0 { | 525 | if weight <= 1.0 { |
| 480 | // Every movable level is spent: nothing left to re-solve over. | 526 | // Every movable level is spent: nothing left to re-solve over. |
| 481 | return; | 527 | return; |
| 482 | } | 528 | } |
| 483 | - let scale = (bytes / weight).max(0.0).min(self.scale0 * Self::REPLAN_CLAMP); | 529 | + let scale = (bytes / weight) |
| 530 | + .clamp(self.scale0 * Self::REPLAN_FLOOR, self.scale0 * Self::REPLAN_CLAMP); | ||
| 484 | self.target = [scale * self.key_weight, scale, scale]; | 531 | self.target = [scale * self.key_weight, scale, scale]; |
| 485 | } | 532 | } |
| 486 | } | 533 | } |
| @@ -2340,15 +2387,15 @@ mod tests { | |||
| 2340 | // floor and spends half of its. | 2387 | // floor and spends half of its. |
| 2341 | let key_bytes = rl.target[0].round(); | 2388 | let key_bytes = rl.target[0].round(); |
| 2342 | rl.update(Level::Key, 0, 100, key_bytes as usize); | 2389 | rl.update(Level::Key, 0, 100, key_bytes as usize); |
| 2343 | - let (offset, _) = hidden[0]; | 2390 | + let (offset, n0) = hidden[0]; |
| 2344 | let arf_bytes = (0.5 * scale0 * rl.weight(offset)).round(); | 2391 | let arf_bytes = (0.5 * scale0 * rl.weight(offset)).round(); |
| 2345 | rl.update(Level::Arf, offset, 0, arf_bytes as usize); | 2392 | rl.update(Level::Arf, offset, 0, arf_bytes as usize); |
| 2346 | - // Every hidden frame still to come is priced at the rate the bound | 2393 | + // Every frame of THAT LEVEL still to come is priced at what the bound |
| 2347 | - // one measured; the leaves get the whole rest. | 2394 | + // one measured -- and only that level: its slot-mates (the mid ARF) |
| 2348 | - let rate = arf_bytes / rl.weight(offset); | 2395 | + // sit at their own offset and are still free to follow the plan. |
| 2349 | - let want = | 2396 | + // Everything else shares the rest. |
| 2350 | - (run_bytes - key_bytes - arf_bytes - rate * (hidden_weight - rl.weight(offset))) | 2397 | + let want = (run_bytes - key_bytes - arf_bytes * n0 as f64) |
| 2351 | - / leaves as f64; | 2398 | + / (leaves as f64 + hidden_weight - n0 as f64 * rl.weight(offset)); |
| 2352 | assert!( | 2399 | assert!( |
| 2353 | want < scale0 * RateLoop::REPLAN_CLAMP, | 2400 | want < scale0 * RateLoop::REPLAN_CLAMP, |
| 2354 | "this arm must not reach the windup bound: {want:.1} vs {:.1}", | 2401 | "this arm must not reach the windup bound: {want:.1} vs {:.1}", |
| @@ -2376,6 +2423,21 @@ mod tests { | |||
| 2376 | rl.target[2], | 2423 | rl.target[2], |
| 2377 | scale0 * RateLoop::REPLAN_CLAMP | 2424 | scale0 * RateLoop::REPLAN_CLAMP |
| 2378 | ); | 2425 | ); |
| 2426 | + // ... and BOTH ways: a re-plan that reached zero would stop the loop | ||
| 2427 | + // dead (`update` returns on a non-positive target). | ||
| 2428 | + let mut rl = RateLoop::new(target_bytes, 100.0, Some(pyramid), gop); | ||
| 2429 | + rl.update(Level::Key, 0, 100, key_bytes as usize); | ||
| 2430 | + for &(offset, n) in &hidden { | ||
| 2431 | + for _ in 0..n { | ||
| 2432 | + rl.update(Level::Arf, offset, 0, 40 * key_bytes as usize); | ||
| 2433 | + } | ||
| 2434 | + } | ||
| 2435 | + assert!( | ||
| 2436 | + rl.target[2] >= scale0 * RateLoop::REPLAN_FLOOR - 1e-9, | ||
| 2437 | + "the re-plan froze the loop: leaf target {:.1} under {:.1}", | ||
| 2438 | + rl.target[2], | ||
| 2439 | + scale0 * RateLoop::REPLAN_FLOOR | ||
| 2440 | + ); | ||
| 2379 | } | 2441 | } |
| 2380 | 2442 | ||
| 2381 | /// The mini-GOP layout leaves no short tail group, and leaves the long | 2443 | /// The mini-GOP layout leaves no short tail group, and leaves the long |