forge
Explore
Log in

Don't you dare go Hollow

The coding pyramid is gated on content -- a screen-content stream codes the flat bytes

Browse files at this commit

47529de254eea19ec0e31e12c71a9f014e1279deAhmet Kaan GümüşParent: d24d494
  • feat
    • the coding pyramid is gated on content -- a screen-content stream codes the flat bytes
Show full message
feat(av1): the coding pyramid is gated on content -- a screen-content stream codes the flat bytes

The pyramid lost on the OBS capture in every sweep and won on both real
films; the screen detector is 0/48 on the films and 48/48 on the capture,
so it is the signal. Av1Encoder::encode_frames decides once, on the first
picture, and drops a screen stream back to the flat path (byte-identical
to the pre-pyramid encoder). The BD gate prints the requested and the
EFFECTIVE pyramid per row.
crates/ec-av1/src/encode.rsmodified+30 -8
@@ -1080,6 +1080,21 @@ fn screen_content(y: &[u8], width: usize, true_width: usize, true_height: usize)
10801080 on
10811081 }
10821082
1083+/// Whether this SOURCE picture reads as screen content, for a caller that has
1084+/// to decide something BEFORE the frame is coded: [`crate::encoder::Av1Encoder`]
1085+/// gates its coding pyramid on this (a desktop capture keeps the flat
1086+/// one-in-one-out structure the BD gate measured it on, camera material takes
1087+/// the mini-GOP). The key frame runs the same detector again when it builds
1088+/// its header -- that is the run the [`SCREEN_FRAMES`] histogram is meant to
1089+/// count, so this probe undoes its own tick and leaves the gate prints
1090+/// reading one detection per coded frame.
1091+pub(crate) fn picture_is_screen(picture: &Picture) -> bool {
1092+ let y: Vec<u8> = picture.y.iter().map(|&v| v as u8).collect();
1093+ let on = screen_content(&y, picture.width, picture.width, picture.height);
1094+ SCREEN_FRAMES[usize::from(on)].fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
1095+ on
1096+}
1097+
10831098 /// The sequence and frame headers a picture of this size is coded under: one
10841099 /// tile, one transform size per block, no in-loop filter parameters yet
10851100 /// (`crate::encode::pick_and_apply_filters` fills the deblocking levels and
@@ -12345,15 +12360,18 @@ mod tests {
1234512360 /// own quantizer offset; without one it is the flat ladder, which
1234612361 /// `encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence`
1234712362 /// pins byte-identical to [`our_ladder`]'s, so this arm must print the
12348- /// same BD numbers. Returns the ladder, the wall, and — per pyramid level
12349- /// over the whole ladder — how many frames and how many bytes it spent.
12363+ /// same BD numbers. Returns the ladder, the wall, per pyramid level over
12364+ /// the whole ladder how many frames and how many bytes it spent, and the
12365+ /// pyramid the streams were EFFECTIVELY coded under — the content gate in
12366+ /// `Av1Encoder::encode_frames` drops a screen-content clip back to the
12367+ /// flat path, so a requested pyramid can come back `None` here.
1235012368 fn our_ladder_facade(
1235112369 name: &str,
1235212370 source: &[Picture],
1235312371 width: usize,
1235412372 height: usize,
1235512373 pyramid: Option<crate::encoder::Pyramid>,
12356- ) -> (Vec<(f64, f64)>, f64, [usize; 4], [usize; 4]) {
12374+ ) -> (Vec<(f64, f64)>, f64, [usize; 4], [usize; 4], Option<crate::encoder::Pyramid>) {
1235712375 use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level};
1235812376 let slot = |l: Level| match l {
1235912377 Level::Key => 0,
@@ -12363,6 +12381,7 @@ mod tests {
1236312381 };
1236412382 let mut ladder = Vec::new();
1236512383 let mut wall = 0.0;
12384+ let mut effective = None;
1236612385 let (mut counts, mut bytes) = ([0usize; 4], [0usize; 4]);
1236712386 for &q in &[150u8, 120, 90, 60] {
1236812387 let config = EncoderConfig {
@@ -12385,6 +12404,7 @@ mod tests {
1238512404 }
1238612405 packets.extend(enc.flush().unwrap());
1238712406 wall += start.elapsed().as_secs_f64();
12407+ effective = enc.pyramid();
1238812408 let mut stream = Vec::new();
1238912409 for p in &packets {
1239012410 counts[slot(p.level)] += 1;
@@ -12415,7 +12435,7 @@ mod tests {
1241512435 ladder.push((mean, (stream.len() as f64).log10()));
1241612436 }
1241712437 ladder.sort_by(|a, b| a.0.total_cmp(&b.0));
12418- (ladder, wall, counts, bytes)
12438+ (ladder, wall, counts, bytes, effective)
1241912439 }
1242012440
1242112441 /// Byte-exactness gate for encoder work that is meant to change only
@@ -12759,10 +12779,11 @@ mod tests {
1275912779 let (ours, ours_wall) = match (facade, pyramid_env) {
1276012780 (false, None) => our_ladder(name, &source, width, height, fctx),
1276112781 (_, pyramid) => {
12762- let (ladder, wall, counts, bytes) =
12782+ let (ladder, wall, counts, bytes, effective) =
1276312783 our_ladder_facade(name, &source, width, height, pyramid);
1276412784 eprintln!(
12765- "{name}: facade, pyramid {pyramid:?} -- frames key {} arf {} leaf {} \
12785+ "{name}: facade, pyramid requested {pyramid:?} effective \
12786+ {effective:?} -- frames key {} arf {} leaf {} \
1276612787 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",
1276712788 counts[0], counts[1], counts[2], counts[3],
1276812789 bytes[0], bytes[1], bytes[2], bytes[3],
@@ -13268,10 +13289,11 @@ mod tests {
1326813289 let (ours, ours_wall) = match pyramid_from_env() {
1326913290 None => our_ladder(name, &source, cw, ch, fctx),
1327013291 Some(pyramid) => {
13271- let (ladder, wall, counts, bytes) =
13292+ let (ladder, wall, counts, bytes, effective) =
1327213293 our_ladder_facade(name, &source, cw, ch, Some(pyramid));
1327313294 eprintln!(
13274- "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \
13295+ "{name}: pyramid requested {pyramid:?} effective {effective:?} \
13296+ -- frames key {} arf {} leaf {} \
1327513297 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",
1327613298 counts[0], counts[1], counts[2], counts[3],
1327713299 bytes[0], bytes[1], bytes[2], bytes[3],
crates/ec-av1/src/encoder.rsmodified+75 -0
@@ -669,6 +669,16 @@ impl Av1Encoder {
669669 Ok(encoder)
670670 }
671671
672+ /// The [`Pyramid`] this stream is actually coding under: what
673+ /// [`Av1Encoder::with_pyramid`] asked for, or `None` once the content gate
674+ /// in [`Av1Encoder::encode_frames`] has dropped a screen-content stream
675+ /// back to the flat path. Meaningful after the first picture has been
676+ /// handed in.
677+ #[must_use]
678+ pub fn pyramid(&self) -> Option<Pyramid> {
679+ self.pyramid
680+ }
681+
672682 /// The coded (padded-to-64) frame size every picture in this stream is
673683 /// coded at — what a decoder allocates and what `ffprobe` reports.
674684 #[must_use]
@@ -741,6 +751,24 @@ impl Av1Encoder {
741751 ),
742752 ));
743753 }
754+ // THE CONTENT GATE (lane-av1pyrgate): the pyramid is for camera
755+ // material. A desktop capture is the one content class every pyramid
756+ // sweep measured WORSE under it, and it is exactly the class the
757+ // screen detector separates (`encode::screen_content`: 0/48 frames on
758+ // both real films, 48/48 on the OBS capture), so a stream whose first
759+ // picture reads as screen content drops back to the flat path and
760+ // codes byte for byte what it coded before the pyramid existed.
761+ //
762+ // Decided ONCE, on the first picture, not per mini-GOP: the flat path
763+ // keeps its references in `reference`/`golden`/`prev2` and the pyramid
764+ // path in `dpb`, so a switch after the first frame would read slots
765+ // the other path never filled. Per-GOP re-decision needs the two
766+ // reference sets kept in step first; the gate's own recipe codes one
767+ // GOP per stream, where the two are the same decision.
768+ if self.pyramid.is_some() && self.next_index == 0 && crate::encode::picture_is_screen(picture)
769+ {
770+ self.pyramid = None;
771+ }
744772 let Some(pyramid) = self.pyramid else {
745773 // The delay queue: the pictures behind this one are the
746774 // lookahead window the one at its head is coded with, which is
@@ -1250,6 +1278,48 @@ mod tests {
12501278 }
12511279 }
12521280
1281+ /// THE CONTENT GATE (lane-av1pyrgate): the coding pyramid is asked for by
1282+ /// the caller but applied only to camera material. Over screen content
1283+ /// the encoder must drop back to the flat path and code byte for byte
1284+ /// what a flat encoder codes — that is what keeps the BD gate's screen
1285+ /// row identical to the flat baseline — and over non-screen content the
1286+ /// same request must still reorder (class `gate-blind-to-feature`: a gate
1287+ /// that only checked the screen half would pass with the pyramid deleted).
1288+ #[test]
1289+ fn the_content_gate_keeps_screen_streams_flat() {
1290+ let frames = 8usize;
1291+ let pictures: Vec<Picture> = (0..frames).map(|t| test_card(64, 64, t)).collect();
1292+ let config = EncoderConfig {
1293+ width: 64,
1294+ height: 64,
1295+ base_q_idx: 120,
1296+ gop: frames,
1297+ colour: Colour::Unspecified,
1298+ tile_cols_log2: 0,
1299+ tile_rows_log2: 0,
1300+ };
1301+ crate::encode::force_screen(Some(true));
1302+ let mut flat = Av1Encoder::new(config).unwrap();
1303+ let flat_packets = encode_all(&mut flat, &pictures);
1304+ let mut gated = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap();
1305+ let gated_packets = encode_all(&mut gated, &pictures);
1306+ assert_eq!(gated.pyramid(), None, "the gate left a screen stream on the pyramid");
1307+ assert_eq!(
1308+ concat(&gated_packets),
1309+ concat(&flat_packets),
1310+ "a screen stream's bytes are not the flat path's"
1311+ );
1312+ crate::encode::force_screen(Some(false));
1313+ let mut kept = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap();
1314+ let kept_packets = encode_all(&mut kept, &pictures);
1315+ assert_eq!(kept.pyramid(), Some(Pyramid::default()), "the gate ate a film stream");
1316+ assert!(
1317+ kept_packets.iter().any(|p| p.level == Level::Arf),
1318+ "a non-screen stream coded no hidden frame"
1319+ );
1320+ crate::encode::force_screen(None);
1321+ }
1322+
12531323 /// A key frame every `gop` pictures, inter otherwise — checked against
12541324 /// what the facade itself reports, and (below) against what `ffprobe`
12551325 /// reads back out of the coded bytes.
@@ -1752,6 +1822,11 @@ mod tests {
17521822 mini_gop: 4,
17531823 ..Pyramid::default()
17541824 };
1825+ // The content gate ([`Av1Encoder::encode_frames`]) would drop a
1826+ // screen-content stream to the flat path, and a synthetic test card
1827+ // is exactly the few-colour picture the detector fires on: this gate
1828+ // is about the reordering, so it asks for camera content explicitly.
1829+ crate::encode::force_screen(Some(false));
17551830 let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap();
17561831 let sources: Vec<Picture> = (0..9).map(|t| test_card(width, height, t * 3)).collect();
17571832 let _ = crate::encode::take_ref_frame_hits();