Merge branch 'lane-rlclamp' (6da9cd06): a rate-loop level at its quantizer bound hands its bytes to the other levels, priced per level; the 2 Mbps pyramid arm lands inside the bitrate gate
Browse files at this commit1 changed file +223 -7
- crates/ec-av1/src/encoder.rs +223 -7
crates/ec-av1/src/encoder.rsmodified+223 -7
| @@ -181,6 +181,34 @@ const WEIGHT_LEVEL_GAIN: f64 = 0.0557; | |||
| 181 | /// keeps growing with reference depth). | 181 | /// keeps growing with reference depth). |
| 182 | const KEY_WEIGHT: f64 = 1.08; | 182 | const KEY_WEIGHT: f64 = 1.08; |
| 183 | 183 | ||
| 184 | +/// `EC_AV1_RL_CENSUS=1` — one stderr line per frame the rate loop steers. | ||
| 185 | +fn census_on() -> bool { | ||
| 186 | + std::env::var("EC_AV1_RL_CENSUS").is_ok_and(|v| v == "1") | ||
| 187 | +} | ||
| 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 | + | ||
| 184 | /// The closed-loop controller behind [`RateTarget::BytesPerFrame`] and | 212 | /// The closed-loop controller behind [`RateTarget::BytesPerFrame`] and |
| 185 | /// [`RateTarget::Bitrate`]: a proportional step on `base_q_idx`, sized from | 213 | /// [`RateTarget::Bitrate`]: a proportional step on `base_q_idx`, sized from |
| 186 | /// 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 |
| @@ -223,6 +251,23 @@ struct RateLoop { | |||
| 223 | /// What is left of both, ahead of the next frame. | 251 | /// What is left of both, ahead of the next frame. |
| 224 | left_weight: f64, | 252 | left_weight: f64, |
| 225 | left_bytes: f64, | 253 | left_bytes: f64, |
| 254 | + /// The run's shape, ONE ENTRY PER LEVEL: the key, each distinct hidden | ||
| 255 | + /// offset, the leaves. A level whose effective quantizer has hit a bound | ||
| 256 | + /// cannot follow the plan, so [`RateLoop::replan`] prices its remaining | ||
| 257 | + /// weight at what it MEASURABLY spends and re-solves the run's budget | ||
| 258 | + /// over the levels that can still move. Per LEVEL and not per slot: the | ||
| 259 | + /// hidden slot mixes the top and mid ARFs, whose leaf-equivalent rates | ||
| 260 | + /// differ by 4x at the floor (both code the same bytes at `q_idx` 1, at | ||
| 261 | + /// weights that differ by that much), and projecting one onto the other's | ||
| 262 | + /// remaining weight drove the whole plan negative. | ||
| 263 | + plans: [Option<LevelPlan>; 5], | ||
| 264 | + /// The plan's scale at construction — the WINDUP BOUND on the re-plan: | ||
| 265 | + /// a level held at its bound hands its unspent bytes to the others, but | ||
| 266 | + /// no level's target may exceed this many times its own opening share. | ||
| 267 | + scale0: f64, | ||
| 268 | + /// `EC_AV1_RL_CENSUS=1`: one line per coded frame on stderr. | ||
| 269 | + census: bool, | ||
| 270 | + coded: usize, | ||
| 226 | } | 271 | } |
| 227 | 272 | ||
| 228 | impl RateLoop { | 273 | impl RateLoop { |
| @@ -234,6 +279,17 @@ impl RateLoop { | |||
| 234 | /// The calibration sweep's own slope (see the struct doc), inverted to | 279 | /// The calibration sweep's own slope (see the struct doc), inverted to |
| 235 | /// convert a bytes ratio into a `q_idx` step. | 280 | /// convert a bytes ratio into a `q_idx` step. |
| 236 | const GAIN: f64 = 1.0 / 0.0154; | 281 | const GAIN: f64 = 1.0 / 0.0154; |
| 282 | + /// The windup bound on [`RateLoop::replan`]: however much a bound level | ||
| 283 | + /// fails to spend, no other level's target may grow past this multiple of | ||
| 284 | + /// its opening share. Without it a run whose hidden levels are all at the | ||
| 285 | + /// floor would keep piling the whole run's budget onto the leaves. | ||
| 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; | ||
| 237 | 293 | ||
| 238 | 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 { |
| 239 | let Some(p) = pyramid else { | 295 | let Some(p) = pyramid else { |
| @@ -248,6 +304,10 @@ impl RateLoop { | |||
| 248 | run_bytes: 0.0, | 304 | run_bytes: 0.0, |
| 249 | left_weight: 0.0, | 305 | left_weight: 0.0, |
| 250 | left_bytes: 0.0, | 306 | left_bytes: 0.0, |
| 307 | + plans: [None; 5], | ||
| 308 | + scale0: target_bytes, | ||
| 309 | + census: census_on(), | ||
| 310 | + coded: 0, | ||
| 251 | }; | 311 | }; |
| 252 | }; | 312 | }; |
| 253 | // THE LEVEL MODEL. Every coded frame of the run is priced by its own | 313 | // THE LEVEL MODEL. Every coded frame of the run is priced by its own |
| @@ -278,6 +338,16 @@ impl RateLoop { | |||
| 278 | // `w(offset)`. Measuring each hidden frame against its OWN predicted | 338 | // `w(offset)`. Measuring each hidden frame against its OWN predicted |
| 279 | // size is what keeps the shared slot from oscillating between the | 339 | // size is what keeps the shared slot from oscillating between the |
| 280 | // levels it mixes. | 340 | // levels it mixes. |
| 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)); | ||
| 281 | Self { | 351 | Self { |
| 282 | target: [scale * key, scale, scale], | 352 | target: [scale * key, scale, scale], |
| 283 | q: [start_q; 3], | 353 | q: [start_q; 3], |
| @@ -289,6 +359,10 @@ impl RateLoop { | |||
| 289 | run_bytes: target_bytes * gop.max(1) as f64, | 359 | run_bytes: target_bytes * gop.max(1) as f64, |
| 290 | left_weight: total, | 360 | left_weight: total, |
| 291 | left_bytes: target_bytes * gop.max(1) as f64, | 361 | left_bytes: target_bytes * gop.max(1) as f64, |
| 362 | + plans, | ||
| 363 | + scale0: scale, | ||
| 364 | + census: census_on(), | ||
| 365 | + coded: 0, | ||
| 292 | } | 366 | } |
| 293 | } | 367 | } |
| 294 | 368 | ||
| @@ -298,6 +372,14 @@ impl RateLoop { | |||
| 298 | ((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() |
| 299 | } | 373 | } |
| 300 | 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 | + | ||
| 301 | /// 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 |
| 302 | /// separate ones under a pyramid. | 384 | /// separate ones under a pyramid. |
| 303 | fn slot(&self, level: Level) -> usize { | 385 | fn slot(&self, level: Level) -> usize { |
| @@ -319,7 +401,11 @@ impl RateLoop { | |||
| 319 | 401 | ||
| 320 | /// Steers this level's `q` toward its own target from the frame's actual | 402 | /// Steers this level's `q` toward its own target from the frame's actual |
| 321 | /// coded size. | 403 | /// coded size. |
| 322 | - fn update(&mut self, level: Level, offset: i16, actual_bytes: usize) { | 404 | + /// |
| 405 | + /// `raw_q` is the quantizer the frame WANTED (`base_q_idx + offset`, | ||
| 406 | + /// before the coder's own 1..=255 clamp): outside that range the level is | ||
| 407 | + /// at its bound and its share of the budget has to move elsewhere. | ||
| 408 | + fn update(&mut self, level: Level, offset: i16, raw_q: i16, actual_bytes: usize) { | ||
| 323 | let slot = self.slot(level); | 409 | let slot = self.slot(level); |
| 324 | if self.target[slot] <= 0.0 || actual_bytes == 0 { | 410 | if self.target[slot] <= 0.0 || actual_bytes == 0 { |
| 325 | return; | 411 | return; |
| @@ -354,7 +440,25 @@ impl RateLoop { | |||
| 354 | raw | 440 | raw |
| 355 | }; | 441 | }; |
| 356 | 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); |
| 357 | - self.replan(level, frame_weight, actual_bytes as f64); | 443 | + let bound = !(1..=255).contains(&raw_q); |
| 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 | + } | ||
| 448 | + if self.census { | ||
| 449 | + eprintln!( | ||
| 450 | + "RLCENSUS n={} level={level:?} slot={slot} target={target:.0} actual={actual_bytes} \ | ||
| 451 | + base_q={:.1} raw_q={raw_q} eff_q={} bound={} left_bytes={:.0} left_w={:.1}", | ||
| 452 | + self.coded, | ||
| 453 | + self.q[slot], | ||
| 454 | + raw_q.clamp(1, 255), | ||
| 455 | + bound, | ||
| 456 | + self.left_bytes, | ||
| 457 | + self.left_weight, | ||
| 458 | + ); | ||
| 459 | + self.coded += 1; | ||
| 460 | + } | ||
| 461 | + self.replan(level, slot, offset, frame_weight, actual_bytes as f64); | ||
| 358 | } | 462 | } |
| 359 | 463 | ||
| 360 | /// 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 |
| @@ -374,22 +478,56 @@ impl RateLoop { | |||
| 374 | /// 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 |
| 375 | /// 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, |
| 376 | /// so no debt outlives one run. | 480 | /// so no debt outlives one run. |
| 377 | - fn replan(&mut self, level: Level, frame_weight: f64, actual_bytes: f64) { | 481 | + fn replan(&mut self, level: Level, slot: usize, offset: i16, frame_weight: f64, actual_bytes: f64) { |
| 378 | if !self.per_level { | 482 | if !self.per_level { |
| 379 | return; | 483 | return; |
| 380 | } | 484 | } |
| 381 | if level == Level::Key { | 485 | if level == Level::Key { |
| 382 | self.left_weight = self.run_weight; | 486 | self.left_weight = self.run_weight; |
| 383 | self.left_bytes = self.run_bytes; | 487 | self.left_bytes = self.run_bytes; |
| 488 | + for plan in self.plans.iter_mut().flatten() { | ||
| 489 | + plan.left = plan.weight; | ||
| 490 | + } | ||
| 384 | } | 491 | } |
| 385 | self.left_weight -= frame_weight; | 492 | self.left_weight -= frame_weight; |
| 386 | self.left_bytes -= actual_bytes; | 493 | self.left_bytes -= actual_bytes; |
| 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 | + } | ||
| 387 | // 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 |
| 388 | // left, the division is noise. | 498 | // left, the division is noise. |
| 389 | if self.left_weight <= 1.0 { | 499 | if self.left_weight <= 1.0 { |
| 390 | return; | 500 | return; |
| 391 | } | 501 | } |
| 392 | - let scale = (self.left_bytes / self.left_weight).max(0.0); | 502 | + // A BOUND level is held at what it measurably spends and its weight |
| 503 | + // is taken out of the division, so the bytes it cannot spend land on | ||
| 504 | + // the levels that can (and, at the ceiling, the other way round). The | ||
| 505 | + // old code divided the whole remainder by the whole remaining weight, | ||
| 506 | + // which handed the biggest share of the shortfall straight back to | ||
| 507 | + // the level that had just failed to spend it -- at the default | ||
| 508 | + // pyramid the hidden levels carry most of the run's weight, so most | ||
| 509 | + // of the surplus evaporated there every frame. | ||
| 510 | + // ABLATION (`EC_AV1_RL_NOREDIST=1`, this lane's before/after arm): | ||
| 511 | + // the old division, whole remainder over whole remaining weight. | ||
| 512 | + if std::env::var("EC_AV1_RL_NOREDIST").is_ok_and(|v| v == "1") { | ||
| 513 | + let scale = (self.left_bytes / self.left_weight).max(0.0); | ||
| 514 | + self.target = [scale * self.key_weight, scale, scale]; | ||
| 515 | + return; | ||
| 516 | + } | ||
| 517 | + let (mut bytes, mut weight) = (self.left_bytes, 0.0); | ||
| 518 | + for plan in self.plans.iter().flatten() { | ||
| 519 | + if plan.bound { | ||
| 520 | + bytes -= plan.rate * plan.left; | ||
| 521 | + } else { | ||
| 522 | + weight += plan.left; | ||
| 523 | + } | ||
| 524 | + } | ||
| 525 | + if weight <= 1.0 { | ||
| 526 | + // Every movable level is spent: nothing left to re-solve over. | ||
| 527 | + return; | ||
| 528 | + } | ||
| 529 | + let scale = (bytes / weight) | ||
| 530 | + .clamp(self.scale0 * Self::REPLAN_FLOOR, self.scale0 * Self::REPLAN_CLAMP); | ||
| 393 | self.target = [scale * self.key_weight, scale, scale]; | 531 | self.target = [scale * self.key_weight, scale, scale]; |
| 394 | } | 532 | } |
| 395 | } | 533 | } |
| @@ -1567,7 +1705,7 @@ impl Av1Encoder { | |||
| 1567 | let level = if is_key { Level::Key } else { Level::Leaf }; | 1705 | let level = if is_key { Level::Key } else { Level::Leaf }; |
| 1568 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1706 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 1569 | // The flat path codes every frame at the slot's own q, no offset. | 1707 | // The flat path codes every frame at the slot's own q, no offset. |
| 1570 | - rate_loop.update(level, 0, cropped.stream.len()); | 1708 | + rate_loop.update(level, 0, i16::from(base_q_idx), cropped.stream.len()); |
| 1571 | } | 1709 | } |
| 1572 | Ok(self.packet(cropped.stream, is_key, order, level)) | 1710 | Ok(self.packet(cropped.stream, is_key, order, level)) |
| 1573 | } | 1711 | } |
| @@ -1692,7 +1830,7 @@ impl Av1Encoder { | |||
| 1692 | self.leaf_hist.clear(); | 1830 | self.leaf_hist.clear(); |
| 1693 | let cropped = crop_encoded(&encoded, render.0, render.1); | 1831 | let cropped = crop_encoded(&encoded, render.0, render.1); |
| 1694 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1832 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 1695 | - rate_loop.update(Level::Key, 0, cropped.stream.len()); | 1833 | + rate_loop.update(Level::Key, 0, base + offset, cropped.stream.len()); |
| 1696 | } | 1834 | } |
| 1697 | if let Some(collected) = self.collected.as_mut() { | 1835 | if let Some(collected) = self.collected.as_mut() { |
| 1698 | collected.push((order, cropped.clone())); | 1836 | collected.push((order, cropped.clone())); |
| @@ -1855,7 +1993,7 @@ impl Av1Encoder { | |||
| 1855 | self.refresh(&[self_slot], &encoded, order_hint); | 1993 | self.refresh(&[self_slot], &encoded, order_hint); |
| 1856 | let cropped = crop_encoded(&encoded, render.0, render.1); | 1994 | let cropped = crop_encoded(&encoded, render.0, render.1); |
| 1857 | if let Some(rate_loop) = self.rate_loop.as_mut() { | 1995 | if let Some(rate_loop) = self.rate_loop.as_mut() { |
| 1858 | - rate_loop.update(level, offset, cropped.stream.len()); | 1996 | + rate_loop.update(level, offset, base + offset, cropped.stream.len()); |
| 1859 | } | 1997 | } |
| 1860 | if let Some(collected) = self.collected.as_mut() { | 1998 | if let Some(collected) = self.collected.as_mut() { |
| 1861 | collected.push((order, cropped.clone())); | 1999 | collected.push((order, cropped.clone())); |
| @@ -2224,6 +2362,84 @@ mod tests { | |||
| 2224 | } | 2362 | } |
| 2225 | } | 2363 | } |
| 2226 | 2364 | ||
| 2365 | + /// A level at its quantizer BOUND hands its unspent bytes to the levels | ||
| 2366 | + /// that can still move ([[vorbis-rate-loop-windup]]'s class in the | ||
| 2367 | + /// small), and it does so AT ONCE, not once the bound level's frames are | ||
| 2368 | + /// all coded: the loop is driven by a synthetic per-level model where the | ||
| 2369 | + /// first hidden frame is coded at `raw_q` 0 (past the coder's own 1..=255 | ||
| 2370 | + /// clamp) and spends half its plan, and the leaves' target must jump to | ||
| 2371 | + /// the run's remainder with EVERY remaining hidden frame priced at that | ||
| 2372 | + /// measured rate. The old division -- whole remainder over whole | ||
| 2373 | + /// remaining weight -- handed most of the shortfall straight back to the | ||
| 2374 | + /// hidden slot (which carries most of the pyramid's weight and cannot | ||
| 2375 | + /// spend it), so the leaves only saw the surplus at the end of the run, | ||
| 2376 | + /// when there was nothing left to code with it. | ||
| 2377 | + #[test] | ||
| 2378 | + fn a_bound_level_hands_its_unspent_bytes_to_the_others() { | ||
| 2379 | + let pyramid = Pyramid::default(); | ||
| 2380 | + let (gop, target_bytes) = (48usize, 2_000.0); | ||
| 2381 | + let (hidden, leaves) = gop_shape(pyramid, gop); | ||
| 2382 | + let run_bytes = target_bytes * gop as f64; | ||
| 2383 | + let mut rl = RateLoop::new(target_bytes, 100.0, Some(pyramid), gop); | ||
| 2384 | + let scale0 = rl.target[2]; | ||
| 2385 | + let hidden_weight: f64 = hidden.iter().map(|&(o, n)| n as f64 * rl.weight(o)).sum(); | ||
| 2386 | + // The key codes exactly its plan; the first hidden frame is at the | ||
| 2387 | + // floor and spends half of its. | ||
| 2388 | + let key_bytes = rl.target[0].round(); | ||
| 2389 | + rl.update(Level::Key, 0, 100, key_bytes as usize); | ||
| 2390 | + let (offset, n0) = hidden[0]; | ||
| 2391 | + let arf_bytes = (0.5 * scale0 * rl.weight(offset)).round(); | ||
| 2392 | + rl.update(Level::Arf, offset, 0, arf_bytes as usize); | ||
| 2393 | + // Every frame of THAT LEVEL still to come is priced at what the bound | ||
| 2394 | + // one measured -- and only that level: its slot-mates (the mid ARF) | ||
| 2395 | + // sit at their own offset and are still free to follow the plan. | ||
| 2396 | + // Everything else shares the rest. | ||
| 2397 | + let want = (run_bytes - key_bytes - arf_bytes * n0 as f64) | ||
| 2398 | + / (leaves as f64 + hidden_weight - n0 as f64 * rl.weight(offset)); | ||
| 2399 | + assert!( | ||
| 2400 | + want < scale0 * RateLoop::REPLAN_CLAMP, | ||
| 2401 | + "this arm must not reach the windup bound: {want:.1} vs {:.1}", | ||
| 2402 | + scale0 * RateLoop::REPLAN_CLAMP | ||
| 2403 | + ); | ||
| 2404 | + assert!( | ||
| 2405 | + (rl.target[2] - want).abs() < 1e-6, | ||
| 2406 | + "leaf target {:.3} is not the exact remainder {want:.3} (naive division would \ | ||
| 2407 | + give {:.3})", | ||
| 2408 | + rl.target[2], | ||
| 2409 | + (run_bytes - key_bytes - arf_bytes) / (leaves as f64 + hidden_weight - rl.weight(offset)) | ||
| 2410 | + ); | ||
| 2411 | + // And the re-plan stays BOUNDED: hidden levels that spend nothing at | ||
| 2412 | + // all cannot run the leaves' target away. | ||
| 2413 | + let mut rl = RateLoop::new(target_bytes, 100.0, Some(pyramid), gop); | ||
| 2414 | + rl.update(Level::Key, 0, 100, key_bytes as usize); | ||
| 2415 | + for &(offset, n) in &hidden { | ||
| 2416 | + for _ in 0..n { | ||
| 2417 | + rl.update(Level::Arf, offset, 0, 1); | ||
| 2418 | + } | ||
| 2419 | + } | ||
| 2420 | + assert!( | ||
| 2421 | + rl.target[2] <= scale0 * RateLoop::REPLAN_CLAMP + 1e-9, | ||
| 2422 | + "the re-plan is unbounded: leaf target {:.1} past {:.1}", | ||
| 2423 | + rl.target[2], | ||
| 2424 | + scale0 * RateLoop::REPLAN_CLAMP | ||
| 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 | + ); | ||
| 2441 | + } | ||
| 2442 | + | ||
| 2227 | /// 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 |
| 2228 | /// GOP the fixed rule already coded alone: 47 inter pictures are | 2444 | /// GOP the fixed rule already coded alone: 47 inter pictures are |
| 2229 | /// 8 x 5 + 7 under both rules (so a 48-picture GOP is byte-identical), | 2445 | /// 8 x 5 + 7 under both rules (so a 48-picture GOP is byte-identical), |