forge
Explore
Log in

Don't you dare go Hollow

Lossless key frames decode exact

Browse files at this commit

1e1a080d84acb9451ecf92a14228205c8cb61486Ahmet Kaan GümüşParent: b0db8e9
  • feat
    • lossless key frames decode exact
    • lossless inter refused by name
Show full message
feat(av1): lossless key frames decode exact; lossless inter refused by name

Key/intra-only lossless frames decode sample-exact at 8 and 10 bit (64x64,
256x128 8-bit, 192x96 10-bit libaom -crf 0 streams, 0 sample diffs vs
ffmpeg). The inter path's luma leaves are synthesized as TX_4X4 but its
chroma plane is still one transform per block, so a lossless INTER frame is
refused by name and the refusal inventory carries it. The old refusal gate
becomes a_lossless_libaom_key_frame_decodes_sample_exact.
crates/ec-av1/src/decode.rsmodified+24 -0
@@ -17533,6 +17533,18 @@ fn read_block_tx_size(
1753317533 is_inter: bool,
1753417534 skip: bool, fctx: &crate::decode::FrameCtx,
1753517535 ) -> Result<(usize, Option<Vec<(usize, usize, usize, usize)>>)> {
17536+ // lane-lossless: TX_4X4 over the whole block, and NO var-tx tree -- libaom
17537+ // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the
17538+ // `read_tx_size` arm, which returns TX_4X4 before it reads anything.
17539+ if lossless(fctx) {
17540+ let mut leaves = Vec::new();
17541+ for row in 0..side / MI {
17542+ for col in 0..side / MI {
17543+ leaves.push((row, col, 4, 4));
17544+ }
17545+ }
17546+ return Ok((4, Some(leaves)));
17547+ }
1753617548 if !fctx.tx_select_inter.with(std::cell::Cell::get) {
1753717549 // lane-sb128c r1: `max_txsize_rect_lookup[BLOCK_128X128]` is TX_64X64,
1753817550 // so a 128x128 inter block is FOUR luma units even with `tx_mode ==
@@ -17972,6 +17984,15 @@ fn read_block_tx_size_rect(
1797217984 (mi_cols, mi_rows): (usize, usize),
1797317985 skip: bool, fctx: &crate::decode::FrameCtx,
1797417986 ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> {
17987+ if lossless(fctx) {
17988+ let mut leaves = Vec::new();
17989+ for row in 0..bh / MI {
17990+ for col in 0..bw / MI {
17991+ leaves.push((row, col, 4, 4));
17992+ }
17993+ }
17994+ return Ok(Some(leaves));
17995+ }
1797517996 if skip {
1797617997 // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records
1797717998 // its own BLOCK size -- width above, height left -- not a transform.
@@ -18261,6 +18282,9 @@ fn read_inter_luma8(
1826118282 reduced_tx_set: bool,
1826218283 split: bool, fctx: &crate::decode::FrameCtx,
1826318284 ) -> Result<(Grid, TxType)> {
18285+ // lane-lossless: an 8x8 inter block is FOUR 4x4 units (libaom
18286+ // `read_block_tx_size`'s lossless TX_4X4), tree or no tree.
18287+ let split = split || lossless(fctx);
1826418288 if !split {
1826518289 return read_inter_plane(
1826618290 dec,
crates/ec-av1/src/refusal_inventory.rsmodified+12 -11
@@ -57,15 +57,16 @@ const REFUSALS: &[&str] = &[
5757 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",
5858 "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)",
5959 "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)",
60- // lane-dpm1: libaom codes his screen-capture row at `-crf 5` with
61- // `base_q_idx == 0`, i.e. LOSSLESS -- TX_4X4 with the Walsh-Hadamard
62- // transform over every block (no `tx_depth` symbol at all) and
63- // `is_cfl_allowed` narrowed to `plane_bsize == BLOCK_4X4`, which changes
64- // the `uv_mode` alphabet. Nothing here consumes the header's `lossless`,
65- // so the first block of such a frame read `uv_mode` = UV_CFL_PRED where
66- // aomdec reads V_PRED. Refused by name (gate
67- // `a_lossless_libaom_stream_is_refused_by_name`) until the path exists.
68- "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented",
60+ // lane-lossless: the KEY/intra-only half of a lossless frame (base_q_idx
61+ // 0) now decodes sample-exact -- TX_4X4 with the Walsh-Hadamard transform
62+ // on every plane, no `tx_type` symbol, `is_cfl_allowed` narrowed to a
63+ // BLOCK_4X4 chroma plane block. What is left is the INTER chroma walk: an
64+ // inter block's chroma plane is still read as ONE transform per block
65+ // here, so a lossless inter frame is refused by name (gate
66+ // `a_lossless_inter_frame_is_refused_by_name`). A frame whose segments
67+ // disagree is refused separately: every lossless rule is per segment.
68+ "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
69+ "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",
6970 "a frame OBU with no tile group",
7071 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",
7172 "a frame with no mode-info grid",
@@ -285,8 +286,8 @@ const PROVEN: &[(&str, &str)] = &[
285286 "an_inter_frame_opening_a_stream_is_refused_by_name",
286287 ),
287288 (
288- "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented",
289- "a_lossless_libaom_stream_is_refused_by_name",
289+ "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
290+ "a_lossless_inter_frame_is_refused_by_name",
290291 ),
291292 // lane-t900 r25, enumeration: a motion_mode/obmc symbol is read only under
292293 // `is_motion_variation_allowed_bsize` (min side >= 8), and each of the 17
crates/ec-av1/src/stream.rsmodified+99 -27
@@ -1617,6 +1617,19 @@ fn decode_frame(
16171617 "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",
16181618 ));
16191619 }
1620+ // lane-lossless r1 ships the KEY/intra-only half: an inter block's chroma
1621+ // plane is still read as ONE transform per block here, where a lossless
1622+ // frame codes it as 4x4 units like everything else (libaom
1623+ // `read_block_tx_size` returns TX_4X4 for every plane). The luma side of
1624+ // the inter path is already converted (`read_block_tx_size`'s synthesized
1625+ // leaves); the chroma walk and its mu-chunk order are the remaining work,
1626+ // so refuse by name rather than return silently wrong pixels.
1627+ if coded_lossless && header.frame_type == FrameType::Inter {
1628+ return Err(Error::unsupported(
1629+ "AV1 decode_stream",
1630+ "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
1631+ ));
1632+ }
16201633 crate::decode::set_lossless(coded_lossless, fctx);
16211634 crate::decode::set_bit_depth(bit_depth, fctx);
16221635 // spec 7.16: this frame's superres state, for the filter chain --
@@ -3994,18 +4007,20 @@ pub(crate) mod tests {
39944007 }
39954008 }
39964009
3997- /// lane-dpm1: the lossless refusal is a CLAIM, so it gets a stream that
3998- /// reaches it. libaom at `-crf 0` codes `base_q_idx == 0` -- the same
3999- /// lossless frame his screen-capture row gets at `-crf 5` -- where the tile
4000- /// syntax is TX_4X4 + Walsh-Hadamard and `is_cfl_allowed` narrows to
4001- /// `plane_bsize == BLOCK_4X4`. Before the refusal our decoder returned a
4002- /// picture that was wrong from luma sample 0 (measured on that row: 126
4003- /// against ffmpeg's 33, the first block's `uv_mode` read off the CfL
4004- /// alphabet). The gate asserts the stream really is lossless and that we
4005- /// refuse it BY NAME rather than decode it wrong.
4010+ /// lane-lossless: the refusal became an implementation, so its gate became
4011+ /// a DECODE-EXACT one (class `refusal-lifted-without-a-gate`). libaom at
4012+ /// `-crf 0` codes `base_q_idx == 0` -- the same lossless frame his
4013+ /// screen-capture row gets at `-crf 5` -- where every plane is TX_4X4 with
4014+ /// the Walsh-Hadamard transform, no `tx_type` symbol is coded
4015+ /// (`read_tx_type`, `decodetxb.c`: "No need to read transform type for
4016+ /// lossless mode(qindex==0)") and `is_cfl_allowed` (`blockd.h`) narrows to
4017+ /// a BLOCK_4X4 chroma plane block. The gate asserts the stream really is
4018+ /// lossless (class `gate-blind-to-feature`), that every KEY frame decodes
4019+ /// sample-exact against ffmpeg, and that the still-unimplemented lossless
4020+ /// INTER frame is refused BY NAME rather than decoded wrong.
40064021 #[test]
4007- fn a_lossless_libaom_stream_is_refused_by_name() {
4008- const NAME: &str = "a_lossless_libaom_stream_is_refused_by_name";
4022+ fn a_lossless_libaom_key_frame_decodes_sample_exact() {
4023+ const NAME: &str = "a_lossless_libaom_key_frame_decodes_sample_exact";
40094024 if !have_ffmpeg() {
40104025 eprintln!("SKIP {NAME}: no ffmpeg");
40114026 return;
@@ -4023,17 +4038,21 @@ pub(crate) mod tests {
40234038 // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.
40244039 let src = std::env::temp_dir().join(format!("ec-av1-lossless-{}.yuv", std::process::id()));
40254040 std::fs::write(&src, &raw).expect("raw source");
4026- let out = Command::new("ffmpeg")
4027- .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
4028- .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4029- .arg(&src)
4030- .args(["-an", "-threads", "1", "-g", "2", "-c:v", "libaom-av1"])
4031- .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])
4032- .stdin(Stdio::null())
4033- .stdout(Stdio::piped())
4034- .stderr(Stdio::piped())
4035- .output()
4036- .expect("ffmpeg failed to run");
4041+ let encode = |gop: &str| {
4042+ let out = Command::new("ffmpeg")
4043+ .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
4044+ .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4045+ .arg(&src)
4046+ .args(["-an", "-threads", "1", "-g", gop, "-c:v", "libaom-av1"])
4047+ .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])
4048+ .stdin(Stdio::null())
4049+ .stdout(Stdio::piped())
4050+ .stderr(Stdio::piped())
4051+ .output()
4052+ .expect("ffmpeg failed to run");
4053+ out
4054+ };
4055+ let out = encode("1");
40374056 let _ = std::fs::remove_file(&src);
40384057 if !out.status.success() {
40394058 eprintln!(
@@ -4054,15 +4073,68 @@ pub(crate) mod tests {
40544073 ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h,
40554074 _ => continue,
40564075 };
4057- if header.lossless.iter().any(|&l| l) {
4076+ if header.lossless.iter().all(|&l| l) {
40584077 lossless += 1;
40594078 }
40604079 }
4061- assert!(lossless > 0, "{NAME} went blind: libaom coded no lossless frame at -crf 0");
4062- match decode_stream(&stream) {
4063- Ok(_) => panic!("{NAME}: a lossless stream decoded instead of being refused"),
4080+ assert_eq!(lossless, frames, "{NAME} went blind: libaom coded no lossless key frame at -crf 0");
4081+ let ours = decode_stream(&stream).expect("a lossless key-frame stream decodes");
4082+ let refs = ffmpeg_decode_sequence(&stream, w, h, frames);
4083+ assert_eq!(ours.len(), frames, "{NAME}: frame count");
4084+ for (i, (got, want)) in ours.iter().zip(refs.iter()).enumerate() {
4085+ for (plane, (g, r)) in [(&got.y, &want.y), (&got.u, &want.u), (&got.v, &want.v)]
4086+ .iter()
4087+ .enumerate()
4088+ {
4089+ let bad = g.iter().zip(r.iter()).filter(|(a, b)| a != b).count();
4090+ assert_eq!(bad, 0, "{NAME}: frame {i} plane {plane}: {bad} samples differ");
4091+ }
4092+ }
4093+ }
4094+
4095+ /// The other half of lane-lossless r1: a lossless INTER frame is refused BY
4096+ /// NAME (its chroma plane is still one transform per block here), never
4097+ /// decoded into silently wrong pixels -- the refusal this gate pins is the
4098+ /// one `refusal_inventory` carries.
4099+ #[test]
4100+ fn a_lossless_inter_frame_is_refused_by_name() {
4101+ const NAME: &str = "a_lossless_inter_frame_is_refused_by_name";
4102+ if !have_ffmpeg() {
4103+ eprintln!("SKIP {NAME}: no ffmpeg");
4104+ return;
4105+ }
4106+ let (w, h, frames) = (128usize, 64usize, 4usize);
4107+ let mut raw = Vec::with_capacity(frames * w * h * 3 / 2);
4108+ for f in 0..frames {
4109+ for row in 0..h {
4110+ for col in 0..w {
4111+ raw.push(((col * 7 + row * 13 + f * 31) % 251) as u8);
4112+ }
4113+ }
4114+ raw.extend(std::iter::repeat_n(128u8, w * h / 2));
4115+ }
4116+ let src = std::env::temp_dir().join(format!("ec-av1-lossless-i-{}.yuv", std::process::id()));
4117+ std::fs::write(&src, &raw).expect("raw source");
4118+ let out = Command::new("ffmpeg")
4119+ .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
4120+ .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4121+ .arg(&src)
4122+ .args(["-an", "-threads", "1", "-g", "4", "-c:v", "libaom-av1"])
4123+ .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])
4124+ .stdin(Stdio::null())
4125+ .stdout(Stdio::piped())
4126+ .stderr(Stdio::piped())
4127+ .output()
4128+ .expect("ffmpeg failed to run");
4129+ let _ = std::fs::remove_file(&src);
4130+ if !out.status.success() {
4131+ eprintln!("SKIP {NAME}: ffmpeg has no libaom-av1 encoder");
4132+ return;
4133+ }
4134+ match decode_stream(&out.stdout) {
4135+ Ok(_) => panic!("{NAME}: a lossless inter stream decoded instead of being refused"),
40644136 Err(e) => assert!(
4065- format!("{e}").contains("a lossless frame (qindex 0)"),
4137+ format!("{e}").contains("a lossless INTER frame"),
40664138 "{NAME}: refused for the wrong reason: {e}"
40674139 ),
40684140 }
crates/ec-av1/src/transform.rsmodified+11 -10
@@ -2934,19 +2934,20 @@ mod lossless_tx_tests {
29342934 fn fwht(residual: &[i32]) -> Vec<i32> {
29352935 let mut t = [0i32; 16];
29362936 for i in 0..4 {
2937- let (a, b, c, d) = undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);
2938- t[i * 4] = a;
2939- t[i * 4 + 1] = c;
2940- t[i * 4 + 2] = d;
2941- t[i * 4 + 3] = b;
2937+ let (a, b, c, d) =
2938+ undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);
2939+ t[i] = a;
2940+ t[4 + i] = c;
2941+ t[8 + i] = d;
2942+ t[12 + i] = b;
29422943 }
29432944 let mut dq = vec![0i32; 16];
29442945 for i in 0..4 {
2945- let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]);
2946- dq[i] = a * 4;
2947- dq[4 + i] = c * 4;
2948- dq[8 + i] = d * 4;
2949- dq[12 + i] = b * 4;
2946+ let (a, b, c, d) = undo(t[i * 4], t[i * 4 + 1], t[i * 4 + 2], t[i * 4 + 3]);
2947+ dq[i * 4] = a * 4;
2948+ dq[i * 4 + 1] = c * 4;
2949+ dq[i * 4 + 2] = d * 4;
2950+ dq[i * 4 + 3] = b * 4;
29502951 }
29512952 dq
29522953 }