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(
17533 is_inter: bool,17533 is_inter: bool,
17534 skip: bool, fctx: &crate::decode::FrameCtx,17534 skip: bool, fctx: &crate::decode::FrameCtx,
17535 ) -> Result<(usize, Option<Vec<(usize, usize, usize, usize)>>)> {17535 ) -> 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+ }
17536 if !fctx.tx_select_inter.with(std::cell::Cell::get) {17548 if !fctx.tx_select_inter.with(std::cell::Cell::get) {
17537 // lane-sb128c r1: `max_txsize_rect_lookup[BLOCK_128X128]` is TX_64X64,17549 // lane-sb128c r1: `max_txsize_rect_lookup[BLOCK_128X128]` is TX_64X64,
17538 // so a 128x128 inter block is FOUR luma units even with `tx_mode ==17550 // so a 128x128 inter block is FOUR luma units even with `tx_mode ==
@@ -17972,6 +17984,15 @@ fn read_block_tx_size_rect(
17972 (mi_cols, mi_rows): (usize, usize),17984 (mi_cols, mi_rows): (usize, usize),
17973 skip: bool, fctx: &crate::decode::FrameCtx,17985 skip: bool, fctx: &crate::decode::FrameCtx,
17974 ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> {17986 ) -> 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+ }
17975 if skip {17996 if skip {
17976 // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records17997 // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records
17977 // its own BLOCK size -- width above, height left -- not a transform.17998 // its own BLOCK size -- width above, height left -- not a transform.
@@ -18261,6 +18282,9 @@ fn read_inter_luma8(
18261 reduced_tx_set: bool,18282 reduced_tx_set: bool,
18262 split: bool, fctx: &crate::decode::FrameCtx,18283 split: bool, fctx: &crate::decode::FrameCtx,
18263 ) -> Result<(Grid, TxType)> {18284 ) -> 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);
18264 if !split {18288 if !split {
18265 return read_inter_plane(18289 return read_inter_plane(
18266 dec,18290 dec,
crates/ec-av1/src/refusal_inventory.rsmodified+12 -11
@@ -57,15 +57,16 @@ const REFUSALS: &[&str] = &[
57 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",57 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",
58 "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)",58 "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)",
59 "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)",59 "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` with60+ // lane-lossless: the KEY/intra-only half of a lossless frame (base_q_idx
61- // `base_q_idx == 0`, i.e. LOSSLESS -- TX_4X4 with the Walsh-Hadamard61+ // 0) now decodes sample-exact -- TX_4X4 with the Walsh-Hadamard transform
62- // transform over every block (no `tx_depth` symbol at all) and62+ // on every plane, no `tx_type` symbol, `is_cfl_allowed` narrowed to a
63- // `is_cfl_allowed` narrowed to `plane_bsize == BLOCK_4X4`, which changes63+ // BLOCK_4X4 chroma plane block. What is left is the INTER chroma walk: an
64- // the `uv_mode` alphabet. Nothing here consumes the header's `lossless`,64+ // inter block's chroma plane is still read as ONE transform per block
65- // so the first block of such a frame read `uv_mode` = UV_CFL_PRED where65+ // here, so a lossless inter frame is refused by name (gate
66- // aomdec reads V_PRED. Refused by name (gate66+ // `a_lossless_inter_frame_is_refused_by_name`). A frame whose segments
67- // `a_lossless_libaom_stream_is_refused_by_name`) until the path exists.67+ // disagree is refused separately: every lossless rule is per segment.
68- "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented",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)",
69 "a frame OBU with no tile group",70 "a frame OBU with no tile group",
70 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",71 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",
71 "a frame with no mode-info grid",72 "a frame with no mode-info grid",
@@ -285,8 +286,8 @@ const PROVEN: &[(&str, &str)] = &[
285 "an_inter_frame_opening_a_stream_is_refused_by_name",286 "an_inter_frame_opening_a_stream_is_refused_by_name",
286 ),287 ),
287 (288 (
288- "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented",289+ "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
289- "a_lossless_libaom_stream_is_refused_by_name",290+ "a_lossless_inter_frame_is_refused_by_name",
290 ),291 ),
291 // lane-t900 r25, enumeration: a motion_mode/obmc symbol is read only under292 // lane-t900 r25, enumeration: a motion_mode/obmc symbol is read only under
292 // `is_motion_variation_allowed_bsize` (min side >= 8), and each of the 17293 // `is_motion_variation_allowed_bsize` (min side >= 8), and each of the 17
crates/ec-av1/src/stream.rsmodified+89 -17
@@ -1617,6 +1617,19 @@ fn decode_frame(
1617 "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",1617 "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",
1618 ));1618 ));
1619 }1619 }
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+ }
1620 crate::decode::set_lossless(coded_lossless, fctx);1633 crate::decode::set_lossless(coded_lossless, fctx);
1621 crate::decode::set_bit_depth(bit_depth, fctx);1634 crate::decode::set_bit_depth(bit_depth, fctx);
1622 // spec 7.16: this frame's superres state, for the filter chain --1635 // spec 7.16: this frame's superres state, for the filter chain --
@@ -3994,18 +4007,20 @@ pub(crate) mod tests {
3994 }4007 }
3995 }4008 }
3996 4009
3997- /// lane-dpm1: the lossless refusal is a CLAIM, so it gets a stream that4010+ /// lane-lossless: the refusal became an implementation, so its gate became
3998- /// reaches it. libaom at `-crf 0` codes `base_q_idx == 0` -- the same4011+ /// a DECODE-EXACT one (class `refusal-lifted-without-a-gate`). libaom at
3999- /// lossless frame his screen-capture row gets at `-crf 5` -- where the tile4012+ /// `-crf 0` codes `base_q_idx == 0` -- the same lossless frame his
4000- /// syntax is TX_4X4 + Walsh-Hadamard and `is_cfl_allowed` narrows to4013+ /// screen-capture row gets at `-crf 5` -- where every plane is TX_4X4 with
4001- /// `plane_bsize == BLOCK_4X4`. Before the refusal our decoder returned a4014+ /// the Walsh-Hadamard transform, no `tx_type` symbol is coded
4002- /// picture that was wrong from luma sample 0 (measured on that row: 1264015+ /// (`read_tx_type`, `decodetxb.c`: "No need to read transform type for
4003- /// against ffmpeg's 33, the first block's `uv_mode` read off the CfL4016+ /// lossless mode(qindex==0)") and `is_cfl_allowed` (`blockd.h`) narrows to
4004- /// alphabet). The gate asserts the stream really is lossless and that we4017+ /// a BLOCK_4X4 chroma plane block. The gate asserts the stream really is
4005- /// refuse it BY NAME rather than decode it wrong.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.
4006 #[test]4021 #[test]
4007- fn a_lossless_libaom_stream_is_refused_by_name() {4022+ fn a_lossless_libaom_key_frame_decodes_sample_exact() {
4008- const NAME: &str = "a_lossless_libaom_stream_is_refused_by_name";4023+ const NAME: &str = "a_lossless_libaom_key_frame_decodes_sample_exact";
4009 if !have_ffmpeg() {4024 if !have_ffmpeg() {
4010 eprintln!("SKIP {NAME}: no ffmpeg");4025 eprintln!("SKIP {NAME}: no ffmpeg");
4011 return;4026 return;
@@ -4023,17 +4038,21 @@ pub(crate) mod tests {
4023 // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.4038 // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.
4024 let src = std::env::temp_dir().join(format!("ec-av1-lossless-{}.yuv", std::process::id()));4039 let src = std::env::temp_dir().join(format!("ec-av1-lossless-{}.yuv", std::process::id()));
4025 std::fs::write(&src, &raw).expect("raw source");4040 std::fs::write(&src, &raw).expect("raw source");
4041+ let encode = |gop: &str| {
4026 let out = Command::new("ffmpeg")4042 let out = Command::new("ffmpeg")
4027 .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])4043 .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
4028 .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])4044 .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4029 .arg(&src)4045 .arg(&src)
4030- .args(["-an", "-threads", "1", "-g", "2", "-c:v", "libaom-av1"])4046+ .args(["-an", "-threads", "1", "-g", gop, "-c:v", "libaom-av1"])
4031 .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])4047 .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])
4032 .stdin(Stdio::null())4048 .stdin(Stdio::null())
4033 .stdout(Stdio::piped())4049 .stdout(Stdio::piped())
4034 .stderr(Stdio::piped())4050 .stderr(Stdio::piped())
4035 .output()4051 .output()
4036 .expect("ffmpeg failed to run");4052 .expect("ffmpeg failed to run");
4053+ out
4054+ };
4055+ let out = encode("1");
4037 let _ = std::fs::remove_file(&src);4056 let _ = std::fs::remove_file(&src);
4038 if !out.status.success() {4057 if !out.status.success() {
4039 eprintln!(4058 eprintln!(
@@ -4054,15 +4073,68 @@ pub(crate) mod tests {
4054 ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h,4073 ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h,
4055 _ => continue,4074 _ => continue,
4056 };4075 };
4057- if header.lossless.iter().any(|&l| l) {4076+ if header.lossless.iter().all(|&l| l) {
4058 lossless += 1;4077 lossless += 1;
4059 }4078 }
4060 }4079 }
4061- assert!(lossless > 0, "{NAME} went blind: libaom coded no lossless frame at -crf 0");4080+ assert_eq!(lossless, frames, "{NAME} went blind: libaom coded no lossless key frame at -crf 0");
4062- match decode_stream(&stream) {4081+ let ours = decode_stream(&stream).expect("a lossless key-frame stream decodes");
4063- Ok(_) => panic!("{NAME}: a lossless stream decoded instead of being refused"),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"),
4064 Err(e) => assert!(4136 Err(e) => assert!(
4065- format!("{e}").contains("a lossless frame (qindex 0)"),4137+ format!("{e}").contains("a lossless INTER frame"),
4066 "{NAME}: refused for the wrong reason: {e}"4138 "{NAME}: refused for the wrong reason: {e}"
4067 ),4139 ),
4068 }4140 }
crates/ec-av1/src/transform.rsmodified+11 -10
@@ -2934,19 +2934,20 @@ mod lossless_tx_tests {
2934 fn fwht(residual: &[i32]) -> Vec<i32> {2934 fn fwht(residual: &[i32]) -> Vec<i32> {
2935 let mut t = [0i32; 16];2935 let mut t = [0i32; 16];
2936 for i in 0..4 {2936 for i in 0..4 {
2937- let (a, b, c, d) = undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);2937+ let (a, b, c, d) =
2938- t[i * 4] = a;2938+ undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);
2939- t[i * 4 + 1] = c;2939+ t[i] = a;
2940- t[i * 4 + 2] = d;2940+ t[4 + i] = c;
2941- t[i * 4 + 3] = b;2941+ t[8 + i] = d;
2942+ t[12 + i] = b;
2942 }2943 }
2943 let mut dq = vec![0i32; 16];2944 let mut dq = vec![0i32; 16];
2944 for i in 0..4 {2945 for i in 0..4 {
2945- let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]);2946+ let (a, b, c, d) = undo(t[i * 4], t[i * 4 + 1], t[i * 4 + 2], t[i * 4 + 3]);
2946- dq[i] = a * 4;2947+ dq[i * 4] = a * 4;
2947- dq[4 + i] = c * 4;2948+ dq[i * 4 + 1] = c * 4;
2948- dq[8 + i] = d * 4;2949+ dq[i * 4 + 2] = d * 4;
2949- dq[12 + i] = b * 4;2950+ dq[i * 4 + 3] = b * 4;
2950 }2951 }
2951 dq2952 dq
2952 }2953 }