forge
Explore
Log in

Don't you dare go Hollow

ec-av1 rate loop: key frame no longer slams the flat path's shared q

Browse files at this commit

4b94175cf2bf40274e4cc39c95d198fa699b3762Ahmet Kaan GümüşParent: 76482df
Root cause (encoder/mod.rs RateLoop): the flat path (no pyramid) had one
shared q slot for key and leaves (slot() returned 0 for all levels) and no
key budget. The key frame's first update takes the unclamped seed step
(q += ln(22289/4000)/0.0154 ~ +111), so q hit 255 right after the key;
the following inter frames then ramped down at STEP_CLAMP=12/frame, which
matches the reported sizes ramp 45,109,219,272,428,550,... on Sintel
640x384 (derived from the code path, not yet checked with ffprobe).
Fix: the flat path builds the same run plan as the pyramid (key plan + leaf
plan, gop budget, replan over the GOP), each level steers its own q, and
per_level is gone. The key overshoot is spread over the GOP instead of the
leaves' quantizer. gop=1 output is unchanged.
crates/ec-av1/src/encoder/mod.rsmodified+23 -43
@@ -236,10 +236,6 @@ struct RateLoop {
236 target: [f64; 3],236 target: [f64; 3],
237 /// Quantizer per level, same index.237 /// Quantizer per level, same index.
238 q: [f64; 3],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 /// Whether each slot has already steered off a real coded frame. The239 /// Whether each slot has already steered off a real coded frame. The
244 /// FIRST update of a slot is the seeding one and takes the model's whole240 /// FIRST update of a slot is the seeding one and takes the model's whole
245 /// step ([`RateLoop::update`]); every later one is clamped.241 /// step ([`RateLoop::update`]); every later one is clamped.
@@ -299,39 +295,30 @@ impl RateLoop {
299 const REPLAN_FLOOR: f64 = 0.25;295 const REPLAN_FLOOR: f64 = 0.25;
300 296
301 fn new(target_bytes: f64, start_q: f64, pyramid: Option<Pyramid>, gop: usize) -> Self {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 // THE LEVEL MODEL. Every coded frame of the run is priced by its own298 // THE LEVEL MODEL. Every coded frame of the run is priced by its own
321 // q offset through `WEIGHT_LEVEL_GAIN`, in units of one leaf, and the299 // 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 old300+ // run's `gop` frame targets are shared out in those units. The flat
323- // model priced a group as "one hidden frame worth `ARF_WEIGHT` = 2301+ // path is the same run with no hidden levels and no offsets, so the
324- // leaves plus `m - 1` leaves", which was wrong twice over and the two302+ // key frame is budgeted against the GOP there too: its overshoot is
325- // errors cancelled (lane-pyr6): the group has coded TWO hidden frames303+ // re-planned over the leaves, never slammed into their shared `q`.
326- // since the mid level landed (four with304+ // 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 // [`Pyramid::quarter_q_offset`]), and a hidden frame is worth far308 // [`Pyramid::quarter_q_offset`]), and a hidden frame is worth far
328 // more than two leaves -- the census reads the top ARF at 24x a leaf309 // more than two leaves -- the census reads the top ARF at 24x a leaf
329 // and the mid at 5x. Both are fixed here, in one diff.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 let w = |offset: i16| {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);321+ let key = KEY_WEIGHT * w(key_q_offset);
334- let key = KEY_WEIGHT * w(p.key_q_offset);
335 let total: f64 =322 let total: f64 =
336 key + leaves as f64 + hidden.iter().map(|&(o, n)| n as f64 * w(o)).sum::<f64>();323 key + leaves as f64 + hidden.iter().map(|&(o, n)| n as f64 * w(o)).sum::<f64>();
337 // One leaf's byte target: the run is worth `gop` frame targets and324 // One leaf's byte target: the run is worth `gop` frame targets and
@@ -347,19 +334,18 @@ impl RateLoop {
347 // One plan per level, in coding order: key, hidden levels (at most334 // One plan per level, in coding order: key, hidden levels (at most
348 // three distinct offsets -- top, mid, quarters), leaves.335 // three distinct offsets -- top, mid, quarters), leaves.
349 let mut plans = [None; 5];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 let mut i = 1;338 let mut i = 1;
352 for &(o, n) in hidden.iter().take(3) {339 for &(o, n) in hidden.iter().take(3) {
353 plans[i] = Some(LevelPlan::new(1, o, n as f64 * w(o)));340 plans[i] = Some(LevelPlan::new(1, o, n as f64 * w(o)));
354 i += 1;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 Self {344 Self {
358 target: [scale * key, scale, scale],345 target: [scale * key, scale, scale],
359 q: [start_q; 3],346 q: [start_q; 3],
360- per_level: true,
361 seeded: [false; 3],347 seeded: [false; 3],
362- leaf_q_offset: p.leaf_q_offset,348+ leaf_q_offset,
363 key_weight: key,349 key_weight: key,
364 run_weight: total,350 run_weight: total,
365 run_bytes: target_bytes * gop.max(1) as f64,351 run_bytes: target_bytes * gop.max(1) as f64,
@@ -386,12 +372,9 @@ impl RateLoop {
386 .position(|p| p.is_some_and(|p| p.slot == slot && (slot != 1 || p.offset == offset)))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, three375+ /// Which controller a level uses: the key, the hidden ARF levels and the
390- /// separate ones under a pyramid.376+ /// leaves each steer their own `q`.
391 fn slot(&self, level: Level) -> usize {377 fn slot(&self, level: Level) -> usize {
392- if !self.per_level {
393- return 0;
394- }
395 match level {378 match level {
396 Level::Key => 0,379 Level::Key => 0,
397 Level::Arf => 1,380 Level::Arf => 1,
@@ -423,7 +406,7 @@ impl RateLoop {
423 1 => self.weight(offset),406 1 => self.weight(offset),
424 _ => 1.0,407 _ => 1.0,
425 };408 };
426- let target = match self.per_level && slot == 1 {409+ let target = match slot == 1 {
427 true => self.target[slot] * frame_weight,410 true => self.target[slot] * frame_weight,
428 false => self.target[slot],411 false => self.target[slot],
429 };412 };
@@ -495,9 +478,6 @@ impl RateLoop {
495 frame_weight: f64,478 frame_weight: f64,
496 actual_bytes: f64,479 actual_bytes: f64,
497 ) {480 ) {
498- if !self.per_level {
499- return;
500- }
501 if level == Level::Key {481 if level == Level::Key {
502 self.left_weight = self.run_weight;482 self.left_weight = self.run_weight;
503 self.left_bytes = self.run_bytes;483 self.left_bytes = self.run_bytes;