forge
Explore
Log in

Don't you dare go Hollow

A fourth pyramid level -- hidden quarter-point ARFs

Browse files at this commit

e9fd8a52492bf5a62b98a7d3350c5d6f94ab53b8Ahmet Kaan GümüşParent: 5918fdb
  • feat
    • a fourth pyramid level -- hidden quarter-point ARFs
Show full message
feat(av1): a fourth pyramid level -- hidden quarter-point ARFs

Within a mini-GOP the leaf run now carries two more hidden frames, at the
middle of each half (pictures 2 and 6 of eight), coded at their own
quarter_q_offset -- the sixth EC_AV1_PYRAMID field, off by default until
the long-GOP sweep picks its offset. Every leaf names the nearest hidden
frame behind it as LAST_FRAME and the nearest one ahead as ALTREF_FRAME,
which the leaf loop now derives from one sorted list of the group's inner
hidden frames instead of the mid frame's index (byte-identical at three
levels and at two). The quarters take DPB slots 5 and 6; the whole slot
map is documented on QUARTER_SLOTS. RateLoop prices a group as h hidden
frames plus mini_gop - h leaves instead of assuming one hidden frame.
crates/ec-av1/src/encoder.rsmodified+146 -18
@@ -234,9 +234,13 @@ impl RateLoop {
234 false => target_bytes,234 false => target_bytes,
235 };235 };
236 // A group of `m` pictures gets `m * rest`, split one `ARF_WEIGHT`236 // A group of `m` pictures gets `m * rest`, split one `ARF_WEIGHT`
237- // share to the hidden frame and one each to the `m - 1` leaves, so237+ // share to EACH of its `h` hidden frames and one each to the `m - h`
238- // the group's own average is `rest` exactly.238+ // leaves, so the group's own average is `rest` exactly. `h` is the
239- let share = rest * m / (m - 1.0 + ARF_WEIGHT);239+ // pyramid's depth, not 1: a four-level group of eight codes four
240+ // hidden frames, and pricing that group as one ARF plus seven leaves
241+ // gave the ARF slot a target its four frames could not share.
242+ let h = p.hidden_per_group(gop);
243+ let share = rest * m / (m - h + h * ARF_WEIGHT);
240 Self {244 Self {
241 target: [target_bytes * key, share * ARF_WEIGHT, share],245 target: [target_bytes * key, share * ARF_WEIGHT, share],
242 q: [start_q; 3],246 q: [start_q; 3],
@@ -405,6 +409,16 @@ pub struct Pyramid {
405 /// nearest good reference. Ignored by a group of fewer than four409 /// nearest good reference. Ignored by a group of fewer than four
406 /// pictures, where there is no leaf on both sides of a midpoint.410 /// pictures, where there is no leaf on both sides of a midpoint.
407 pub mid_q_offset: Option<i16>,411 pub mid_q_offset: Option<i16>,
412+ /// THE FOURTH LEVEL, or `None` for the three-level pyramid: the offset
413+ /// the two hidden QUARTER-POINT ARFs are coded at (between
414+ /// [`Pyramid::mid_q_offset`] and [`Pyramid::leaf_q_offset`], so the group
415+ /// reads key -> top ARF -> mid ARF -> quarter ARFs -> leaves). In a group
416+ /// of eight they sit at pictures 2 and 6, halving again the distance from
417+ /// a leaf to its nearest good reference; every leaf then names the
418+ /// nearest hidden frame on each side. Ignored by a group with fewer than
419+ /// seven leaves, where a quarter point has no leaf on both sides, and --
420+ /// like the mid level -- by a run no longer than one mini-GOP.
421+ pub quarter_q_offset: Option<i16>,
408 /// Added to `base_q_idx` for the GOP's KEY FRAME — negative, since every422 /// Added to `base_q_idx` for the GOP's KEY FRAME — negative, since every
409 /// frame of the run reads it, directly or through an ARF. The 48-frame423 /// frame of the run reads it, directly or through an ARF. The 48-frame
410 /// census (`lanes/census-longgop.md`) found ours coded at base q while424 /// census (`lanes/census-longgop.md`) found ours coded at base q while
@@ -481,8 +495,26 @@ impl Pyramid {
481 },495 },
482 // A fifth field is the key frame's own offset.496 // A fifth field is the key frame's own offset.
483 key_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.key_q_offset),497 key_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.key_q_offset),
498+ // A sixth field is the quarter level's offset, `off` for none.
499+ quarter_q_offset: match f.next() {
500+ None => d.quarter_q_offset,
501+ Some("off") => None,
502+ Some(v) => v.parse().ok().or(d.quarter_q_offset),
503+ },
484 })504 })
485 }505 }
506+
507+ /// How many hidden frames a FULL mini-GOP of this shape codes, in a run
508+ /// of `gop` pictures: the top ARF, plus the mid one and the two
509+ /// quarter-point ones when they are switched on. Mirrors the gating in
510+ /// [`Av1Encoder::drain_pending`] -- the rate loop prices the group from
511+ /// it ([`RateLoop::new`]), so the two must move together.
512+ fn hidden_per_group(&self, gop: usize) -> f64 {
513+ let long_run = gop > self.mini_gop + self.mini_gop / 2;
514+ let mid = long_run && self.mid_q_offset.is_some() && self.mini_gop >= 4;
515+ let quarters = mid && self.quarter_q_offset.is_some() && self.mini_gop >= 8;
516+ 1.0 + f64::from(mid) + 2.0 * f64::from(quarters)
517+ }
486 }518 }
487 519
488 impl Default for Pyramid {520 impl Default for Pyramid {
@@ -771,6 +803,7 @@ impl Default for Pyramid {
771 leaf_q_offset: 12,803 leaf_q_offset: 12,
772 mid_q_offset: MID_DEFAULT,804 mid_q_offset: MID_DEFAULT,
773 key_q_offset: KEY_DEFAULT,805 key_q_offset: KEY_DEFAULT,
806+ quarter_q_offset: QUARTER_DEFAULT,
774 }807 }
775 }808 }
776 }809 }
@@ -807,6 +840,30 @@ const LEAF_SLOT: u8 = 0;
807 /// chain (0) and of `GOLDEN_SLOT` (1), so all four levels are live at once.840 /// chain (0) and of `GOLDEN_SLOT` (1), so all four levels are live at once.
808 const MID_SLOT: u8 = 2;841 const MID_SLOT: u8 = 2;
809 842
843+/// The two slots the QUARTER-POINT hidden frames refresh
844+/// ([`Pyramid::quarter_q_offset`]). THE WHOLE SLOT MAP, with all four levels
845+/// live at once:
846+///
847+/// | slot | holds | written by | read as |
848+/// |---|---|---|---|
849+/// | 0 (`LEAF_SLOT`) | the previous shown leaf | every leaf | `LAST_FRAME` of the next leaf |
850+/// | 1 (`GOLDEN_SLOT`) | the key frame | the key | `GOLDEN_FRAME` everywhere |
851+/// | 2 (`MID_SLOT`) | the mid ARF | the mid ARF | `LAST_FRAME` / `ALTREF_FRAME` of the leaves and quarters around it |
852+/// | 3, 4 (`ANCHOR_SLOTS`) | this group's anchor and the next one (the top ARF) | the key, each top ARF | anchor = `LAST_FRAME`, next = `ALTREF_FRAME` |
853+/// | 5, 6 (`QUARTER_SLOTS`) | the first and second quarter ARF | the quarter ARFs | `LAST_FRAME` / `ALTREF_FRAME` of the leaves beside them |
854+/// | 7 | unused (the key's copy is never read back) | the key | -- |
855+///
856+/// A frame names exactly two of them plus `GOLDEN_SLOT`
857+/// ([`Av1Encoder::encode_pyramid_inter`] fills all seven `ref_frame_idx`
858+/// entries from `last_slot`/`altref_slot`), and `refresh_frame_flags` is the
859+/// single `self_slot` bit, so no level ever overwrites a slot another level
860+/// is still reading.
861+const QUARTER_SLOTS: [u8; 2] = [5, 6];
862+
863+/// [`Pyramid::default`]'s fourth level (`None` = the three-level pyramid).
864+/// The long-GOP sweep's winner (`lanes/pyr6.sweep.txt`).
865+const QUARTER_DEFAULT: Option<i16> = None;
866+
810 /// [`Pyramid::default`]'s third level (`None` = the two-level pyramid), the867 /// [`Pyramid::default`]'s third level (`None` = the two-level pyramid), the
811 /// long-GOP sweep's winner (`lanes/pyr5.sweep.txt`): every one of the four868 /// long-GOP sweep's winner (`lanes/pyr5.sweep.txt`): every one of the four
812 /// long-GOP columns improves against the two-level shape, film B (the wider869 /// long-GOP columns improves against the two-level shape, film B (the wider
@@ -1579,6 +1636,54 @@ impl Av1Encoder {
1579 offset,1636 offset,
1580 )?);1637 )?);
1581 }1638 }
1639+ // THE FOURTH LEVEL: one hidden frame at the middle of each half of
1640+ // the leaf run, so no leaf is more than one picture from a hidden
1641+ // reference on either side -- the depth the references run (rav1e
1642+ // speed 6 codes 23 hidden frames per 48 pictures, libaom four levels
1643+ // per 16-picture group) and the lever three levels had run out of.
1644+ // It hangs off the mid level, and it needs a leaf on both sides of
1645+ // both quarter points, which a run of fewer than seven leaves cannot
1646+ // give.
1647+ let quarters = match (mid, pyramid.quarter_q_offset) {
1648+ (Some((at, _)), Some(offset)) if leaves >= 7 => {
1649+ Some(([(at - 1) / 2, (at + leaves) / 2], offset))
1650+ }
1651+ _ => None,
1652+ };
1653+ if let Some((at, offset)) = quarters {
1654+ // The first quarter predicts forward off the group's anchor and
1655+ // backward off the mid ARF; the second starts FROM the mid ARF
1656+ // and reads the group's top ARF backward.
1657+ for (i, &pos) in at.iter().enumerate() {
1658+ let (order, picture) = group[pos].clone();
1659+ let (last_slot, altref_slot) = match i {
1660+ 0 => (anchor_slot, MID_SLOT),
1661+ _ => (MID_SLOT, next_anchor_slot),
1662+ };
1663+ packets.push(self.encode_pyramid_inter(
1664+ order,
1665+ &picture,
1666+ last_slot,
1667+ QUARTER_SLOTS[i],
1668+ altref_slot,
1669+ false,
1670+ Level::Arf,
1671+ offset,
1672+ )?);
1673+ }
1674+ }
1675+ // Every hidden frame INSIDE the leaf run, by leaf index, in display
1676+ // order: what a leaf reads forward (the nearest one behind it) and
1677+ // backward (the nearest one ahead of it, the group's top ARF when
1678+ // there is none).
1679+ let mut inner: Vec<(usize, u8)> = Vec::new();
1680+ if let Some((at, _)) = mid {
1681+ inner.push((at, MID_SLOT));
1682+ }
1683+ if let Some((at, _)) = quarters {
1684+ inner.extend(at.iter().zip(QUARTER_SLOTS).map(|(&at, slot)| (at, slot)));
1685+ }
1686+ inner.sort_unstable();
1582 let (seq, _) = crate::encode::key_frame_headers(1687 let (seq, _) = crate::encode::key_frame_headers(
1583 self.config.width,1688 self.config.width,
1584 self.config.height,1689 self.config.height,
@@ -1590,26 +1695,27 @@ impl Av1Encoder {
1590 Ok(encoder.packet(data, false, order, Level::ShowExisting))1695 Ok(encoder.packet(data, false, order, Level::ShowExisting))
1591 };1696 };
1592 // The leaves, in display order, so every `show_existing_frame` lands1697 // The leaves, in display order, so every `show_existing_frame` lands
1593- // in its own display position. A leaf before the mid frame names it1698+ // in its own display position. Each leaf names the nearest hidden
1594- // as `ALTREF_FRAME` (the nearer backward reference); the first leaf1699+ // frame BEHIND it as `LAST_FRAME` (the group's anchor for the first
1595- // after it starts its chain FROM it.1700+ // leaf, otherwise the previous leaf) and the nearest one AHEAD as
1701+ // `ALTREF_FRAME` (the group's top ARF when none is closer).
1596 for (i, (order, picture)) in group[..leaves].iter().enumerate() {1702 for (i, (order, picture)) in group[..leaves].iter().enumerate() {
1597- if let Some((at, _)) = mid {1703+ if let Some(&(_, slot)) = inner.iter().find(|(at, _)| *at == i) {
1598- if i == at {1704+ let packet = show_existing(self, slot, *order)?;
1599- let packet = show_existing(self, MID_SLOT, *order)?;
1600 packets.push(packet);1705 packets.push(packet);
1601 continue;1706 continue;
1602 }1707 }
1603- }1708+ let last_slot = match i {
1604- let last_slot = match (i, mid) {1709+ 0 => anchor_slot,
1605- (0, _) => anchor_slot,1710+ i => inner
1606- (i, Some((at, _))) if i == at + 1 => MID_SLOT,1711+ .iter()
1607- _ => LEAF_SLOT,1712+ .find(|(at, _)| at + 1 == i)
1608- };1713+ .map_or(LEAF_SLOT, |&(_, slot)| slot),
1609- let altref_slot = match mid {
1610- Some((at, _)) if i < at => MID_SLOT,
1611- _ => next_anchor_slot,
1612 };1714 };
1715+ let altref_slot = inner
1716+ .iter()
1717+ .find(|(at, _)| *at > i)
1718+ .map_or(next_anchor_slot, |&(_, slot)| slot);
1613 packets.push(self.encode_pyramid_inter(1719 packets.push(self.encode_pyramid_inter(
1614 *order,1720 *order,
1615 picture,1721 picture,
@@ -2457,6 +2563,28 @@ mod tests {
2457 );2563 );
2458 }2564 }
2459 2565
2566+ /// The same round trip over a FOUR-level group
2567+ /// ([`Pyramid::quarter_q_offset`]): a mini-GOP of eight now codes four
2568+ /// hidden frames -- the top ARF at picture 8, the mid at 4 and the two
2569+ /// quarters at 2 and 6 -- each re-output by its own
2570+ /// `show_existing_frame`. Every leaf reads a different pair of DPB slots
2571+ /// than it did at three levels, so a slot map that lets one level
2572+ /// overwrite a picture another is still reading shows up here as a
2573+ /// display-order mismatch against ffmpeg.
2574+ #[test]
2575+ fn a_four_level_pyramid_stream_decodes_in_display_order() {
2576+ let _knobs = crate::speed::knob_read();
2577+ pyramid_round_trip(
2578+ Pyramid {
2579+ mini_gop: 8,
2580+ mid_q_offset: Some(-8),
2581+ quarter_q_offset: Some(4),
2582+ ..Pyramid::default()
2583+ },
2584+ 4,
2585+ );
2586+ }
2587+
2460 /// [`Pyramid::key_q_offset`] reaches the key frame: the whole run reads2588 /// [`Pyramid::key_q_offset`] reaches the key frame: the whole run reads
2461 /// the key, so an offset that never arrived would be invisible in the BD2589 /// the key, so an offset that never arrived would be invisible in the BD
2462 /// table except as "no change" (class symbol-consumption-gap). A finer2590 /// table except as "no change" (class symbol-consumption-gap). A finer
lanes/pyr6-arm.shadded+9 -0
@@ -0,0 +1,9 @@
1+#!/usr/bin/env bash
2+# pyr6 lane: one BD arm on the prebuilt release binary.
3+# usage: pyr6-arm.sh <unit> <log> <test> [<pyramid spec>|control]
4+unit=$1; log=$2; test=$3; spec=$4
5+bin=$HOME/.cache/cargo-target-pyr6/release/deps/ec_av1-8fef0196ab10338d
6+env=""
7+[ "$spec" = control ] || env="EC_AV1_PYRAMID=$spec"
8+systemd-run --user --unit "$unit" -p MemoryMax=8G --collect bash -c \
9+ "cd $HOME/Documents/Code/Rust/edith_codecs-pyr6; $env $bin --ignored --nocapture --test-threads 1 --exact $test > $log 2>&1; echo RC=\$? >> $log"
lanes/pyr6/ctrl-12.logadded+6 -0
@@ -0,0 +1,6 @@
1+
2+running 1 test
3+test encode::tests::bd_rate_screen_native ...
4+native crop, 12 frames, gop=12; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128
5+| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) |
6+|---|---|---|---|---|
lanes/pyr6/ctrl-long.logadded+6 -0
@@ -0,0 +1,6 @@
1+
2+running 1 test
3+test encode::tests::bd_rate_film_long_gop ...
4+native crop, 48 frames, gop=48; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128
5+| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) |
6+|---|---|---|---|---|