The coding pyramid is gated on content -- a screen-content stream codes the flat bytes
Browse files at this commit47529de254eea19ec0e31e12c71a9f014e1279deParent: d24d494- feat
- the coding pyramid is gated on content -- a screen-content stream codes the flat bytes
2 changed files +105 -8
- crates/ec-av1/src/encode.rs +30 -8
- crates/ec-av1/src/encoder.rs +75 -0
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 | 1080 | 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 | 1098 | /// The sequence and frame headers a picture of this size is coded under: one |
| 1084 | 1099 | /// tile, one transform size per block, no in-loop filter parameters yet |
| 1085 | 1100 | /// (`crate::encode::pick_and_apply_filters` fills the deblocking levels and |
| @@ -12345,15 +12360,18 @@ mod tests { | ||
| 12345 | 12360 | /// own quantizer offset; without one it is the flat ladder, which |
| 12346 | 12361 | /// `encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence` |
| 12347 | 12362 | /// 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. | |
| 12350 | 12368 | fn our_ladder_facade( |
| 12351 | 12369 | name: &str, |
| 12352 | 12370 | source: &[Picture], |
| 12353 | 12371 | width: usize, |
| 12354 | 12372 | height: usize, |
| 12355 | 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 | 12375 | use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level}; |
| 12358 | 12376 | let slot = |l: Level| match l { |
| 12359 | 12377 | Level::Key => 0, |
| @@ -12363,6 +12381,7 @@ mod tests { | ||
| 12363 | 12381 | }; |
| 12364 | 12382 | let mut ladder = Vec::new(); |
| 12365 | 12383 | let mut wall = 0.0; |
| 12384 | + let mut effective = None; | |
| 12366 | 12385 | let (mut counts, mut bytes) = ([0usize; 4], [0usize; 4]); |
| 12367 | 12386 | for &q in &[150u8, 120, 90, 60] { |
| 12368 | 12387 | let config = EncoderConfig { |
| @@ -12385,6 +12404,7 @@ mod tests { | ||
| 12385 | 12404 | } |
| 12386 | 12405 | packets.extend(enc.flush().unwrap()); |
| 12387 | 12406 | wall += start.elapsed().as_secs_f64(); |
| 12407 | + effective = enc.pyramid(); | |
| 12388 | 12408 | let mut stream = Vec::new(); |
| 12389 | 12409 | for p in &packets { |
| 12390 | 12410 | counts[slot(p.level)] += 1; |
| @@ -12415,7 +12435,7 @@ mod tests { | ||
| 12415 | 12435 | ladder.push((mean, (stream.len() as f64).log10())); |
| 12416 | 12436 | } |
| 12417 | 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 | 12441 | /// Byte-exactness gate for encoder work that is meant to change only |
| @@ -12759,10 +12779,11 @@ mod tests { | ||
| 12759 | 12779 | let (ours, ours_wall) = match (facade, pyramid_env) { |
| 12760 | 12780 | (false, None) => our_ladder(name, &source, width, height, fctx), |
| 12761 | 12781 | (_, pyramid) => { |
| 12762 | - let (ladder, wall, counts, bytes) = | |
| 12782 | + let (ladder, wall, counts, bytes, effective) = | |
| 12763 | 12783 | our_ladder_facade(name, &source, width, height, pyramid); |
| 12764 | 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 | 12787 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |
| 12767 | 12788 | counts[0], counts[1], counts[2], counts[3], |
| 12768 | 12789 | bytes[0], bytes[1], bytes[2], bytes[3], |
| @@ -13268,10 +13289,11 @@ mod tests { | ||
| 13268 | 13289 | let (ours, ours_wall) = match pyramid_from_env() { |
| 13269 | 13290 | None => our_ladder(name, &source, cw, ch, fctx), |
| 13270 | 13291 | Some(pyramid) => { |
| 13271 | - let (ladder, wall, counts, bytes) = | |
| 13292 | + let (ladder, wall, counts, bytes, effective) = | |
| 13272 | 13293 | our_ladder_facade(name, &source, cw, ch, Some(pyramid)); |
| 13273 | 13294 | eprintln!( |
| 13274 | - "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ | |
| 13295 | + "{name}: pyramid requested {pyramid:?} effective {effective:?} \ | |
| 13296 | + -- frames key {} arf {} leaf {} \ | |
| 13275 | 13297 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |
| 13276 | 13298 | counts[0], counts[1], counts[2], counts[3], |
| 13277 | 13299 | bytes[0], bytes[1], bytes[2], bytes[3], |
crates/ec-av1/src/encoder.rsmodified+75 -0
| @@ -669,6 +669,16 @@ impl Av1Encoder { | ||
| 669 | 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 | 682 | /// The coded (padded-to-64) frame size every picture in this stream is |
| 673 | 683 | /// coded at — what a decoder allocates and what `ffprobe` reports. |
| 674 | 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 | 772 | let Some(pyramid) = self.pyramid else { |
| 745 | 773 | // The delay queue: the pictures behind this one are the |
| 746 | 774 | // 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 | 1323 | /// A key frame every `gop` pictures, inter otherwise — checked against |
| 1254 | 1324 | /// what the facade itself reports, and (below) against what `ffprobe` |
| 1255 | 1325 | /// reads back out of the coded bytes. |
| @@ -1752,6 +1822,11 @@ mod tests { | ||
| 1752 | 1822 | mini_gop: 4, |
| 1753 | 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 | 1830 | let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap(); |
| 1756 | 1831 | let sources: Vec<Picture> = (0..9).map(|t| test_card(width, height, t * 3)).collect(); |
| 1757 | 1832 | let _ = crate::encode::take_ref_frame_hits(); |