forge
Explore
Log in

Don't you dare go Hollow

A lossless key frame decodes sample-exact

Browse files at this commit

b0db8e99a119d75fb309fd3bc9189b61b672a31fAhmet Kaan GümüşParent: 27c4ad5
  • feat
    • a lossless key frame decodes sample-exact
Show full message
feat(av1): a lossless key frame decodes sample-exact

The rect strip readers take the per-transform-unit walk on a lossless frame
(depth_to_tx_wh returns TX_4X4 before it looks at the block), their chroma is
coded as 4x4 units plane-major with get_txb_ctx's +10 offset rows, and the
Walsh-Hadamard orientation is pinned by measurement. A 64x64 libaom -crf 0
key frame now decodes sample-exact against ffmpeg (6488 entropy steps
identical to aomdec's EC_TRACE_COEFF, 0 sample diffs).
crates/ec-av1/src/decode.rsmodified+57 -5
@@ -9565,6 +9565,58 @@ fn decode_rect_split(
9565 m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx,9565 m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx,
9566 );9566 );
9567 (zero.clone(), zero)9567 (zero.clone(), zero)
9568+ } else if lossless(fctx) {
9569+ // lane-lossless: the strip's chroma plane block is coded as 4x4
9570+ // transform units too (libaom `read_tx_size` returns TX_4X4 for every
9571+ // plane), PLANE-MAJOR inside the block -- U's units first, then V's
9572+ // (`decode_token_recon_block`'s mu-chunk loop, measured against
9573+ // aomdec's `EC_TRACE_COEFF` on a `-crf 0` stream: plane=1, plane=1,
9574+ // plane=2, plane=2 for a 16x8 strip). `get_txb_ctx` adds its `+10`
9575+ // offset (our `+3` row base) whenever the plane block is bigger than
9576+ // the transform.
9577+ let scan4 = default_scan(4);
9578+ let offset = (chroma_w > 4 || chroma_h > 4).then_some(3);
9579+ let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4);
9580+ let mut last = [Grid::Zero(16), Grid::Zero(16)];
9581+ for plane_idx in 1..=2 {
9582+ for cu_row in 0..ch_n {
9583+ for cu_col in 0..cw_n {
9584+ let luma_span = 8;
9585+ let cu_mi = (
9586+ mi_r + cu_row * (luma_span / MI),
9587+ mi_c + cu_col * (luma_span / MI),
9588+ );
9589+ let cu_around = neighbours.around_mi(cu_mi, luma_span);
9590+ let cu_reach = tu_reach(
9591+ bw, bh,
9592+ cu_col * luma_span, cu_row * luma_span, luma_span,
9593+ block_reach, px, py, y.width, y.height, fctx,
9594+ );
9595+ let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4);
9596+ if let Some((ub, vb)) = &m.palette_uv {
9597+ set_palette_pred(
9598+ if plane_idx == 1 { ub.clone() } else { vb.clone() },
9599+ fctx,
9600+ );
9601+ }
9602+ let plane = if plane_idx == 1 { &mut *u } else { &mut *v };
9603+ let grid = read_plane(
9604+ dec, cdfs, TxbSet::Chroma4, &scan4, plane_idx,
9605+ cu_around[plane_idx], m.mode, m.uv_predict_mode,
9606+ m.angle_delta_uv, cu_reach, plane, cu_x, cu_y, 4, 4,
9607+ base_q_idx,
9608+ m.alpha.zip(ac).map(|((au, av), ac)| {
9609+ (if plane_idx == 1 { au } else { av }, ac)
9610+ }),
9611+ None, offset, m.smooth_neighbor_uv, fctx,
9612+ )?;
9613+ neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_idx, &grid);
9614+ last[plane_idx - 1] = grid;
9615+ }
9616+ }
9617+ }
9618+ let [ug, vg] = last;
9619+ (ug, vg)
9568 } else {9620 } else {
9569 let (chroma_set, chroma_scan) = chroma.expect("refused above when None");9621 let (chroma_set, chroma_scan) = chroma.expect("refused above when None");
9570 // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size9622 // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size
@@ -10281,7 +10333,7 @@ fn decode_intra_rect_in_inter(
10281 } else {10333 } else {
10282 010334 0
10283 };10335 };
10284- if depth != 0 {10336+ if depth != 0 || lossless(fctx) {
10285 hit!(TX_DEPTH_HITS);10337 hit!(TX_DEPTH_HITS);
10286 // lane-intrasplit r1: a split intra strip in an inter frame walks the10338 // lane-intrasplit r1: a split intra strip in an inter frame walks the
10287 // key frame's own per-TU path ([`decode_rect_split`] below); what was10339 // key frame's own per-TU path ([`decode_rect_split`] below); what was
@@ -10494,7 +10546,7 @@ fn decode_block_rect(
10494 } else {10546 } else {
10495 010547 0
10496 };10548 };
10497- if depth != 0 {10549+ if depth != 0 || lossless(fctx) {
10498 hit!(TX_DEPTH_HITS);10550 hit!(TX_DEPTH_HITS);
10499 // lane-rectsplit r1: a split transform is predicted and reconstructed10551 // lane-rectsplit r1: a split transform is predicted and reconstructed
10500 // per transform unit, each unit taking its edges from the previous10552 // per transform unit, each unit taking its edges from the previous
@@ -11000,7 +11052,7 @@ fn decode_leaf_rect(
11000 } else {11052 } else {
11001 011053 0
11002 };11054 };
11003- if depth != 0 {11055+ if depth != 0 || lossless(fctx) {
11004 // The same per-transform-unit walk the bigger strips take11056 // The same per-transform-unit walk the bigger strips take
11005 // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at11057 // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at
11006 // depth 2), addressed at this leaf's own mi position.11058 // depth 2), addressed at this leaf's own mi position.
@@ -11342,7 +11394,7 @@ fn decode_block_rect4(
11342 depth_to_tx_wh(bw, bh, depth, fctx),11394 depth_to_tx_wh(bw, bh, depth, fctx),
11343 (bw, bh), fctx,11395 (bw, bh), fctx,
11344 );11396 );
11345- if depth != 0 {11397+ if depth != 0 || lossless(fctx) {
11346 // lane-rectsplitx r1: the per-transform-unit walk, at this strip's11398 // lane-rectsplitx r1: the per-transform-unit walk, at this strip's
11347 // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a11399 // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a
11348 // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X1611400 // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X16
@@ -12358,7 +12410,7 @@ fn decode_block_rect64(
12358 depth_to_tx_wh(bw, bh, depth, fctx),12410 depth_to_tx_wh(bw, bh, depth, fctx),
12359 (bw, bh), fctx,12411 (bw, bh), fctx,
12360 );12412 );
12361- if depth != 0 {12413+ if depth != 0 || lossless(fctx) {
12362 hit!(TX_DEPTH_HITS);12414 hit!(TX_DEPTH_HITS);
12363 // lane-rectsplit r1/r4: the superblock-level strip splits its12415 // lane-rectsplit r1/r4: the superblock-level strip splits its
12364 // transform through the very same per-unit path as its 32x32-level12416 // transform through the very same per-unit path as its 32x32-level
crates/ec-av1/src/transform.rsmodified+23 -16
@@ -2849,10 +2849,16 @@ mod tests {
2849 /// dequantization exactly -- that is what makes the round trip lossless.2849 /// dequantization exactly -- that is what makes the round trip lossless.
2850 pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> {2850 pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> {
2851 debug_assert_eq!(dq.len(), 16);2851 debug_assert_eq!(dq.len(), 16);
2852+ // libaom runs pass 1 down the COLUMNS (`ip[4 * k]`, `ip++`) and pass 2
2853+ // along the intermediate's ROWS writing dest COLUMNS (`dest[stride * k]`,
2854+ // `dest++`); transposing both passes -- rows first, then columns, output
2855+ // in place -- is the same map and is what a raster `dq`/residual pair
2856+ // wants here. MEASURED: the other orientation reconstructs sample 0 and
2857+ // the DC-only units correctly and drifts by +-1 everywhere else.
2852 let mut out = [0i32; 16];2858 let mut out = [0i32; 16];
2853 for i in 0..4 {2859 for i in 0..4 {
2854- let ip = &dq[i * 4..];2860+ let (mut a1, mut c1, mut d1, mut b1) =
2855- let (mut a1, mut c1, mut d1, mut b1) = (ip[0] >> 2, ip[1] >> 2, ip[2] >> 2, ip[3] >> 2);2861+ (dq[i * 4] >> 2, dq[i * 4 + 1] >> 2, dq[i * 4 + 2] >> 2, dq[i * 4 + 3] >> 2);
2856 a1 += c1;2862 a1 += c1;
2857 d1 -= b1;2863 d1 -= b1;
2858 let e1 = (a1 - d1) >> 1;2864 let e1 = (a1 - d1) >> 1;
@@ -2867,7 +2873,8 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> {
2867 }2873 }
2868 let mut res = vec![0i32; 16];2874 let mut res = vec![0i32; 16];
2869 for i in 0..4 {2875 for i in 0..4 {
2870- let (mut a1, mut c1, mut d1, mut b1) = (out[i], out[4 + i], out[8 + i], out[12 + i]);2876+ let (mut a1, mut c1, mut d1, mut b1) =
2877+ (out[i], out[4 + i], out[8 + i], out[12 + i]);
2871 a1 += c1;2878 a1 += c1;
2872 d1 -= b1;2879 d1 -= b1;
2873 let e1 = (a1 - d1) >> 1;2880 let e1 = (a1 - d1) >> 1;
@@ -2925,23 +2932,23 @@ mod lossless_tx_tests {
2925 (a1 - c, b, c, d1 + b)2932 (a1 - c, b, c, d1 + b)
2926 }2933 }
2927 fn fwht(residual: &[i32]) -> Vec<i32> {2934 fn fwht(residual: &[i32]) -> Vec<i32> {
2928- let mut t = residual.to_vec();2935+ let mut t = [0i32; 16];
2929 for i in 0..4 {2936 for i in 0..4 {
2930- let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]);2937+ let (a, b, c, d) = undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]);
2931- t[i] = a;2938+ t[i * 4] = a;
2932- t[4 + i] = c;2939+ t[i * 4 + 1] = c;
2933- t[8 + i] = d;2940+ t[i * 4 + 2] = d;
2934- t[12 + i] = b;2941+ t[i * 4 + 3] = b;
2935 }2942 }
2936- let mut out = vec![0i32; 16];2943+ let mut dq = vec![0i32; 16];
2937 for i in 0..4 {2944 for i in 0..4 {
2938- let (a, b, c, d) = undo(t[i * 4], t[i * 4 + 1], t[i * 4 + 2], t[i * 4 + 3]);2945+ let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]);
2939- out[i * 4] = a * 4;2946+ dq[i] = a * 4;
2940- out[i * 4 + 1] = c * 4;2947+ dq[4 + i] = c * 4;
2941- out[i * 4 + 2] = d * 4;2948+ dq[8 + i] = d * 4;
2942- out[i * 4 + 3] = b * 4;2949+ dq[12 + i] = b * 4;
2943 }2950 }
2944- out2951+ dq
2945 }2952 }
2946 let mut state = 0x1234_5678u32;2953 let mut state = 0x1234_5678u32;
2947 for _ in 0..200 {2954 for _ in 0..200 {