A fourth pyramid level -- hidden quarter-point ARFs
Browse files at this commite9fd8a52492bf5a62b98a7d3350c5d6f94ab53b8Parent: 5918fdb- feat
- a fourth pyramid level -- hidden quarter-point ARFs
4 changed files +169 -20
- crates/ec-av1/src/encoder.rs +148 -20
- lanes/pyr6-arm.sh +9 -0
- lanes/pyr6/ctrl-12.log +6 -0
- lanes/pyr6/ctrl-long.log +6 -0
crates/ec-av1/src/encoder.rsmodified+148 -20
| @@ -234,9 +234,13 @@ impl RateLoop { | ||
| 234 | 234 | false => target_bytes, |
| 235 | 235 | }; |
| 236 | 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, so | |
| 238 | - // the group's own average is `rest` exactly. | |
| 239 | - let share = rest * m / (m - 1.0 + ARF_WEIGHT); | |
| 237 | + // share to EACH of its `h` hidden frames and one each to the `m - h` | |
| 238 | + // leaves, so the group's own average is `rest` exactly. `h` is the | |
| 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 | 244 | Self { |
| 241 | 245 | target: [target_bytes * key, share * ARF_WEIGHT, share], |
| 242 | 246 | q: [start_q; 3], |
| @@ -405,6 +409,16 @@ pub struct Pyramid { | ||
| 405 | 409 | /// nearest good reference. Ignored by a group of fewer than four |
| 406 | 410 | /// pictures, where there is no leaf on both sides of a midpoint. |
| 407 | 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 | 422 | /// Added to `base_q_idx` for the GOP's KEY FRAME — negative, since every |
| 409 | 423 | /// frame of the run reads it, directly or through an ARF. The 48-frame |
| 410 | 424 | /// census (`lanes/census-longgop.md`) found ours coded at base q while |
| @@ -481,8 +495,26 @@ impl Pyramid { | ||
| 481 | 495 | }, |
| 482 | 496 | // A fifth field is the key frame's own offset. |
| 483 | 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 | 520 | impl Default for Pyramid { |
| @@ -771,6 +803,7 @@ impl Default for Pyramid { | ||
| 771 | 803 | leaf_q_offset: 12, |
| 772 | 804 | mid_q_offset: MID_DEFAULT, |
| 773 | 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 | 840 | /// chain (0) and of `GOLDEN_SLOT` (1), so all four levels are live at once. |
| 808 | 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 | 867 | /// [`Pyramid::default`]'s third level (`None` = the two-level pyramid), the |
| 811 | 868 | /// long-GOP sweep's winner (`lanes/pyr5.sweep.txt`): every one of the four |
| 812 | 869 | /// long-GOP columns improves against the two-level shape, film B (the wider |
| @@ -1579,6 +1636,54 @@ impl Av1Encoder { | ||
| 1579 | 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 | 1687 | let (seq, _) = crate::encode::key_frame_headers( |
| 1583 | 1688 | self.config.width, |
| 1584 | 1689 | self.config.height, |
| @@ -1590,26 +1695,27 @@ impl Av1Encoder { | ||
| 1590 | 1695 | Ok(encoder.packet(data, false, order, Level::ShowExisting)) |
| 1591 | 1696 | }; |
| 1592 | 1697 | // The leaves, in display order, so every `show_existing_frame` lands |
| 1593 | - // in its own display position. A leaf before the mid frame names it | |
| 1594 | - // as `ALTREF_FRAME` (the nearer backward reference); the first leaf | |
| 1595 | - // after it starts its chain FROM it. | |
| 1698 | + // in its own display position. Each leaf names the nearest hidden | |
| 1699 | + // frame BEHIND it as `LAST_FRAME` (the group's anchor for the first | |
| 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 | 1702 | for (i, (order, picture)) in group[..leaves].iter().enumerate() { |
| 1597 | - if let Some((at, _)) = mid { | |
| 1598 | - if i == at { | |
| 1599 | - let packet = show_existing(self, MID_SLOT, *order)?; | |
| 1600 | - packets.push(packet); | |
| 1601 | - continue; | |
| 1602 | - } | |
| 1703 | + if let Some(&(_, slot)) = inner.iter().find(|(at, _)| *at == i) { | |
| 1704 | + let packet = show_existing(self, slot, *order)?; | |
| 1705 | + packets.push(packet); | |
| 1706 | + continue; | |
| 1603 | 1707 | } |
| 1604 | - let last_slot = match (i, mid) { | |
| 1605 | - (0, _) => anchor_slot, | |
| 1606 | - (i, Some((at, _))) if i == at + 1 => MID_SLOT, | |
| 1607 | - _ => LEAF_SLOT, | |
| 1608 | - }; | |
| 1609 | - let altref_slot = match mid { | |
| 1610 | - Some((at, _)) if i < at => MID_SLOT, | |
| 1611 | - _ => next_anchor_slot, | |
| 1708 | + let last_slot = match i { | |
| 1709 | + 0 => anchor_slot, | |
| 1710 | + i => inner | |
| 1711 | + .iter() | |
| 1712 | + .find(|(at, _)| at + 1 == i) | |
| 1713 | + .map_or(LEAF_SLOT, |&(_, slot)| slot), | |
| 1612 | 1714 | }; |
| 1715 | + let altref_slot = inner | |
| 1716 | + .iter() | |
| 1717 | + .find(|(at, _)| *at > i) | |
| 1718 | + .map_or(next_anchor_slot, |&(_, slot)| slot); | |
| 1613 | 1719 | packets.push(self.encode_pyramid_inter( |
| 1614 | 1720 | *order, |
| 1615 | 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 | 2588 | /// [`Pyramid::key_q_offset`] reaches the key frame: the whole run reads |
| 2461 | 2589 | /// the key, so an offset that never arrived would be invisible in the BD |
| 2462 | 2590 | /// 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 | +|---|---|---|---|---| |