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)
1080 on1080 on
1081 }1081 }
1082 1082
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+
1083 /// The sequence and frame headers a picture of this size is coded under: one1098 /// The sequence and frame headers a picture of this size is coded under: one
1084 /// tile, one transform size per block, no in-loop filter parameters yet1099 /// tile, one transform size per block, no in-loop filter parameters yet
1085 /// (`crate::encode::pick_and_apply_filters` fills the deblocking levels and1100 /// (`crate::encode::pick_and_apply_filters` fills the deblocking levels and
@@ -12345,15 +12360,18 @@ mod tests {
12345 /// own quantizer offset; without one it is the flat ladder, which12360 /// own quantizer offset; without one it is the flat ladder, which
12346 /// `encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence`12361 /// `encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence`
12347 /// pins byte-identical to [`our_ladder`]'s, so this arm must print the12362 /// 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 level12363+ /// same BD numbers. Returns the ladder, the wall, per pyramid level over
12349- /// over the whole ladder — how many frames and how many bytes it spent.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.
12350 fn our_ladder_facade(12368 fn our_ladder_facade(
12351 name: &str,12369 name: &str,
12352 source: &[Picture],12370 source: &[Picture],
12353 width: usize,12371 width: usize,
12354 height: usize,12372 height: usize,
12355 pyramid: Option<crate::encoder::Pyramid>,12373 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>) {
12357 use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level};12375 use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level};
12358 let slot = |l: Level| match l {12376 let slot = |l: Level| match l {
12359 Level::Key => 0,12377 Level::Key => 0,
@@ -12363,6 +12381,7 @@ mod tests {
12363 };12381 };
12364 let mut ladder = Vec::new();12382 let mut ladder = Vec::new();
12365 let mut wall = 0.0;12383 let mut wall = 0.0;
12384+ let mut effective = None;
12366 let (mut counts, mut bytes) = ([0usize; 4], [0usize; 4]);12385 let (mut counts, mut bytes) = ([0usize; 4], [0usize; 4]);
12367 for &q in &[150u8, 120, 90, 60] {12386 for &q in &[150u8, 120, 90, 60] {
12368 let config = EncoderConfig {12387 let config = EncoderConfig {
@@ -12385,6 +12404,7 @@ mod tests {
12385 }12404 }
12386 packets.extend(enc.flush().unwrap());12405 packets.extend(enc.flush().unwrap());
12387 wall += start.elapsed().as_secs_f64();12406 wall += start.elapsed().as_secs_f64();
12407+ effective = enc.pyramid();
12388 let mut stream = Vec::new();12408 let mut stream = Vec::new();
12389 for p in &packets {12409 for p in &packets {
12390 counts[slot(p.level)] += 1;12410 counts[slot(p.level)] += 1;
@@ -12415,7 +12435,7 @@ mod tests {
12415 ladder.push((mean, (stream.len() as f64).log10()));12435 ladder.push((mean, (stream.len() as f64).log10()));
12416 }12436 }
12417 ladder.sort_by(|a, b| a.0.total_cmp(&b.0));12437 ladder.sort_by(|a, b| a.0.total_cmp(&b.0));
12418- (ladder, wall, counts, bytes)12438+ (ladder, wall, counts, bytes, effective)
12419 }12439 }
12420 12440
12421 /// Byte-exactness gate for encoder work that is meant to change only12441 /// Byte-exactness gate for encoder work that is meant to change only
@@ -12759,10 +12779,11 @@ mod tests {
12759 let (ours, ours_wall) = match (facade, pyramid_env) {12779 let (ours, ours_wall) = match (facade, pyramid_env) {
12760 (false, None) => our_ladder(name, &source, width, height, fctx),12780 (false, None) => our_ladder(name, &source, width, height, fctx),
12761 (_, pyramid) => {12781 (_, pyramid) => {
12762- let (ladder, wall, counts, bytes) =12782+ let (ladder, wall, counts, bytes, effective) =
12763 our_ladder_facade(name, &source, width, height, pyramid);12783 our_ladder_facade(name, &source, width, height, pyramid);
12764 eprintln!(12784 eprintln!(
12765- "{name}: facade, pyramid {pyramid:?} -- frames key {} arf {} leaf {} \12785+ "{name}: facade, pyramid requested {pyramid:?} effective \
12786+ {effective:?} -- frames key {} arf {} leaf {} \
12766 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",12787 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",
12767 counts[0], counts[1], counts[2], counts[3],12788 counts[0], counts[1], counts[2], counts[3],
12768 bytes[0], bytes[1], bytes[2], bytes[3],12789 bytes[0], bytes[1], bytes[2], bytes[3],
@@ -13268,10 +13289,11 @@ mod tests {
13268 let (ours, ours_wall) = match pyramid_from_env() {13289 let (ours, ours_wall) = match pyramid_from_env() {
13269 None => our_ladder(name, &source, cw, ch, fctx),13290 None => our_ladder(name, &source, cw, ch, fctx),
13270 Some(pyramid) => {13291 Some(pyramid) => {
13271- let (ladder, wall, counts, bytes) =13292+ let (ladder, wall, counts, bytes, effective) =
13272 our_ladder_facade(name, &source, cw, ch, Some(pyramid));13293 our_ladder_facade(name, &source, cw, ch, Some(pyramid));
13273 eprintln!(13294 eprintln!(
13274- "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \13295+ "{name}: pyramid requested {pyramid:?} effective {effective:?} \
13296+ -- frames key {} arf {} leaf {} \
13275 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",13297 show_existing {}; bytes key {} arf {} leaf {} show_existing {}",
13276 counts[0], counts[1], counts[2], counts[3],13298 counts[0], counts[1], counts[2], counts[3],
13277 bytes[0], bytes[1], bytes[2], bytes[3],13299 bytes[0], bytes[1], bytes[2], bytes[3],
crates/ec-av1/src/encoder.rsmodified+75 -0
@@ -669,6 +669,16 @@ impl Av1Encoder {
669 Ok(encoder)669 Ok(encoder)
670 }670 }
671 671
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+
672 /// The coded (padded-to-64) frame size every picture in this stream is682 /// The coded (padded-to-64) frame size every picture in this stream is
673 /// coded at — what a decoder allocates and what `ffprobe` reports.683 /// coded at — what a decoder allocates and what `ffprobe` reports.
674 #[must_use]684 #[must_use]
@@ -741,6 +751,24 @@ impl Av1Encoder {
741 ),751 ),
742 ));752 ));
743 }753 }
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+ }
744 let Some(pyramid) = self.pyramid else {772 let Some(pyramid) = self.pyramid else {
745 // The delay queue: the pictures behind this one are the773 // The delay queue: the pictures behind this one are the
746 // lookahead window the one at its head is coded with, which is774 // lookahead window the one at its head is coded with, which is
@@ -1250,6 +1278,48 @@ mod tests {
1250 }1278 }
1251 }1279 }
1252 1280
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+
1253 /// A key frame every `gop` pictures, inter otherwise — checked against1323 /// A key frame every `gop` pictures, inter otherwise — checked against
1254 /// what the facade itself reports, and (below) against what `ffprobe`1324 /// what the facade itself reports, and (below) against what `ffprobe`
1255 /// reads back out of the coded bytes.1325 /// reads back out of the coded bytes.
@@ -1752,6 +1822,11 @@ mod tests {
1752 mini_gop: 4,1822 mini_gop: 4,
1753 ..Pyramid::default()1823 ..Pyramid::default()
1754 };1824 };
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));
1755 let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap();1830 let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap();
1756 let sources: Vec<Picture> = (0..9).map(|t| test_card(width, height, t * 3)).collect();1831 let sources: Vec<Picture> = (0..9).map(|t| test_card(width, height, t * 3)).collect();
1757 let _ = crate::encode::take_ref_frame_hits();1832 let _ = crate::encode::take_ref_frame_hits();