forge
Explore
Log in

Don't you dare go Hollow

wip(av1): CfL CDFs, alpha reads, and prediction math — r2 checkpoint

Browse files at this commit

e37bffe7c0dfa13171c1568fb029c2cef210db87Ahmet Kaan GümüşParent: b5e06f8
CfL sign/alpha CDFs converted from AOM_ICDF storage, read_cfl_alphas
wired, cfl.c subsample/average/alpha math ported. The pinned CfL-stream
gate stays ignored: its LUMA decodes near-flat where ffmpeg shows a
gradient, so a bug upstream of the chroma math remains — r3 isolates.
crates/ec-av1/src/cdf.rsmodified+38 -0
@@ -305,6 +305,44 @@ pub const UV_MODE_CFL: [[u16; 15]; 13] = [
305 ],305 ],
306 ];306 ];
307 307
308+/// `Default_Cfl_Sign_Cdf` (spec 9.4): the joint sign of a `UV_CFL_PRED`
309+/// block's U/V alpha (spec 5.11.45's `cfl_alpha_signs`) — libaom's
310+/// `default_cfl_sign_cdf` (entropymode.c), whose `AOM_CDF8(a0..a6)` arguments
311+/// are this convention's cumulative points verbatim (only the *storage*, not
312+/// the spec's own table, is `AOM_ICDF`-inverted).
313+pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 32768, 0];
314+
315+/// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane
316+/// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by
317+/// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`,
318+/// libaom's cfl.h) — libaom's `default_cfl_alpha_cdf`.
319+pub const CFL_ALPHA: [[u16; 17]; 6] = [
320+ [
321+ 7637, 20719, 31401, 32481, 32657, 32688, 32692, 32696, 32700, 32704, 32708, 32712, 32716,
322+ 32720, 32724, 32768, 0,
323+ ],
324+ [
325+ 14365, 23603, 28135, 31168, 32167, 32395, 32487, 32573, 32620, 32647, 32668, 32672, 32676,
326+ 32680, 32684, 32768, 0,
327+ ],
328+ [
329+ 11532, 22380, 28445, 31360, 32349, 32523, 32584, 32649, 32673, 32677, 32681, 32685, 32689,
330+ 32693, 32697, 32768, 0,
331+ ],
332+ [
333+ 26990, 31402, 32282, 32571, 32692, 32696, 32700, 32704, 32708, 32712, 32716, 32720, 32724,
334+ 32728, 32732, 32768, 0,
335+ ],
336+ [
337+ 17248, 26058, 28904, 30608, 31305, 31877, 32126, 32321, 32394, 32464, 32516, 32560, 32576,
338+ 32593, 32622, 32768, 0,
339+ ],
340+ [
341+ 14738, 21678, 25779, 27901, 29024, 30302, 30980, 31843, 32144, 32413, 32520, 32594, 32622,
342+ 32656, 32660, 32768, 0,
343+ ],
344+];
345+
308 /// `Default_Txb_Skip_Cdf[2][3][0]` (spec 9.4): the all-zero flag of a 32x32346 /// `Default_Txb_Skip_Cdf[2][3][0]` (spec 9.4): the all-zero flag of a 32x32
309 /// luma transform block that covers its whole plane block, which is context 0.347 /// luma transform block that covers its whole plane block, which is context 0.
310 pub const TXB_SKIP_LUMA_32: [u16; 3] = [30669, 32768, 0];348 pub const TXB_SKIP_LUMA_32: [u16; 3] = [30669, 32768, 0];
crates/ec-av1/src/cdf_state.rsmodified+6 -0
@@ -98,6 +98,10 @@ pub(crate) struct Cdfs {
98 pub uv_mode_no_cfl: [[u16; 14]; 13],98 pub uv_mode_no_cfl: [[u16; 14]; 13],
99 /// The chroma mode of a block that is offered it.99 /// The chroma mode of a block that is offered it.
100 pub uv_mode_cfl: [[u16; 15]; 13],100 pub uv_mode_cfl: [[u16; 15]; 13],
101+ /// The joint sign of a `UV_CFL_PRED` block's U/V alpha.
102+ pub cfl_sign: [u16; 9],
103+ /// A `UV_CFL_PRED` block's per-plane alpha magnitude, by joint-sign context.
104+ pub cfl_alpha: [[u16; 17]; 6],
101 /// The angle a directional mode is nudged by.105 /// The angle a directional mode is nudged by.
102 pub angle_delta: [[u16; 8]; 8],106 pub angle_delta: [[u16; 8]; 8],
103 /// The one-context all-zero flag of a 16x16 luma transform.107 /// The one-context all-zero flag of a 16x16 luma transform.
@@ -297,6 +301,8 @@ impl Cdfs {
297 kf_y_mode: cdf::KF_Y_MODE,301 kf_y_mode: cdf::KF_Y_MODE,
298 uv_mode_no_cfl: cdf::UV_MODE_NO_CFL,302 uv_mode_no_cfl: cdf::UV_MODE_NO_CFL,
299 uv_mode_cfl: cdf::UV_MODE_CFL,303 uv_mode_cfl: cdf::UV_MODE_CFL,
304+ cfl_sign: cdf::CFL_SIGN,
305+ cfl_alpha: cdf::CFL_ALPHA,
300 angle_delta: cdf::ANGLE_DELTA,306 angle_delta: cdf::ANGLE_DELTA,
301 txb_skip_luma_16: [pick(307 txb_skip_luma_16: [pick(
302 q_ctx,308 q_ctx,
crates/ec-av1/src/decode.rsmodified+146 -20
@@ -510,15 +510,18 @@ impl Neighbours {
510 /// exactly — including its skip context, which the real key-frame writer510 /// exactly — including its skip context, which the real key-frame writer
511 /// always writes at a fixed context of zero (it never tracks a neighbour-based511 /// always writes at a fixed context of zero (it never tracks a neighbour-based
512 /// one for intra blocks; that is the inter-frame writer's rule instead).512 /// one for intra blocks; that is the inter-frame writer's rule instead).
513-/// Refuses a nonzero angle delta or a chroma-from-luma mode: this decoder513+/// Refuses a nonzero angle delta: this decoder reconstructs the thirteen
514-/// reconstructs the thirteen luma modes at their base angle, DC-only chroma.514+/// luma modes at their base angle only. Chroma is DC-only or, where CFL is
515+/// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the
516+/// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for
517+/// plain DC chroma.
515 fn read_intra_mode(518 fn read_intra_mode(
516 dec: &mut SymbolDecoder,519 dec: &mut SymbolDecoder,
517 cdfs: &mut Cdfs,520 cdfs: &mut Cdfs,
518 above_mode: usize,521 above_mode: usize,
519 left_mode: usize,522 left_mode: usize,
520 cfl: bool,523 cfl: bool,
521-) -> Result<(bool, usize)> {524+) -> Result<(bool, usize, Option<(i32, i32)>)> {
522 let skip = dec.symbol(&mut cdfs.skip[0]) != 0;525 let skip = dec.symbol(&mut cdfs.skip[0]) != 0;
523 let mode =526 let mode =
524 dec.symbol(&mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]]);527 dec.symbol(&mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]]);
@@ -535,10 +538,85 @@ fn read_intra_mode(
535 } else {538 } else {
536 dec.symbol(&mut cdfs.uv_mode_no_cfl[mode])539 dec.symbol(&mut cdfs.uv_mode_no_cfl[mode])
537 };540 };
538- if uv_mode != DC_PRED {541+ let alpha = if uv_mode == DC_PRED {
539- return Err(unsupported("a chroma-from-luma mode (round 2)"));542+ None
543+ } else if cfl && uv_mode == UV_CFL_PRED {
544+ Some(read_cfl_alphas(dec, cdfs))
545+ } else {
546+ return Err(unsupported("a directional chroma mode (round 2)"));
547+ };
548+ Ok((skip, mode, alpha))
549+}
550+
551+/// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth
552+/// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary
553+/// intra modes.
554+const UV_CFL_PRED: usize = 13;
555+
556+/// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec
557+/// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol
558+/// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context
559+/// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the
560+/// joint sign the same way. Returns each plane's final signed `alpha_q3`
561+/// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0.
562+fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) {
563+ let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32;
564+ let sign_u = ((joint_sign + 1) * 11) >> 5;
565+ let sign_v = (joint_sign + 1) - 3 * sign_u;
566+ let alpha_u = if sign_u == 0 {
567+ 0
568+ } else {
569+ let ctx_u = (joint_sign + 1 - 3) as usize;
570+ let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1;
571+ if sign_u == 2 { mag } else { -mag }
572+ };
573+ let alpha_v = if sign_v == 0 {
574+ 0
575+ } else {
576+ let ctx_v = (sign_v * 3 + sign_u - 3) as usize;
577+ let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1;
578+ if sign_v == 2 { mag } else { -mag }
579+ };
580+ (alpha_u, alpha_v)
581+}
582+
583+/// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c):
584+/// the reconstructed luma samples under a `side`-square block, subsampled
585+/// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average
586+/// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`)
587+/// to give the AC values [`cfl_scaled`] scales by alpha.
588+fn cfl_ac_q3(y: &PlaneBuf, px: usize, py: usize, side: usize) -> Vec<i32> {
589+ let cside = side / 2;
590+ let mut ac = vec![0i32; cside * cside];
591+ let mut sum = 0i32;
592+ for cy in 0..cside {
593+ for cx in 0..cside {
594+ let (lx, ly) = (px + cx * 2, py + cy * 2);
595+ let q3 = (i32::from(y.data[ly * y.width + lx])
596+ + i32::from(y.data[ly * y.width + lx + 1])
597+ + i32::from(y.data[(ly + 1) * y.width + lx])
598+ + i32::from(y.data[(ly + 1) * y.width + lx + 1]))
599+ << 1;
600+ ac[cy * cside + cx] = q3;
601+ sum += q3;
602+ }
603+ }
604+ let num_pel = (cside * cside) as i32;
605+ let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros();
606+ ac.iter_mut().for_each(|v| *v -= avg);
607+ ac
608+}
609+
610+/// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6
611+/// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest,
612+/// ties away from zero on the shifted-out sign).
613+fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 {
614+ let v = alpha_q3 * ac_q3;
615+ if v >= 0 {
616+ (v + 32) >> 6
617+ } else {
618+ -((-v + 32) >> 6)
540 }619 }
541- Ok((skip, mode))
542 }620 }
543 621
544 /// One plane's reconstruction buffer, `width * height` samples, and the622 /// One plane's reconstruction buffer, `width * height` samples, and the
@@ -592,6 +670,10 @@ impl PlaneBuf {
592 670
593 /// Predicts under `mode` then adds `residual` (side*side, raster),671 /// Predicts under `mode` then adds `residual` (side*side, raster),
594 /// writing the clamped reconstruction back into the plane at `(x, y)`.672 /// writing the clamped reconstruction back into the plane at `(x, y)`.
673+ /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block),
674+ /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the
675+ /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual
676+ /// order (cfl.c).
595 fn reconstruct(677 fn reconstruct(
596 &mut self,678 &mut self,
597 x: usize,679 x: usize,
@@ -600,6 +682,7 @@ impl PlaneBuf {
600 mode: usize,682 mode: usize,
601 reach: Reach,683 reach: Reach,
602 residual: &[i32],684 residual: &[i32],
685+ cfl: Option<(i32, &[i32])>,
603 ) {686 ) {
604 let (above, left, corner) = self.edges(x, y, side, reach);687 let (above, left, corner) = self.edges(x, y, side, reach);
605 let mut prediction = vec![0u8; side * side];688 let mut prediction = vec![0u8; side * side];
@@ -613,8 +696,12 @@ impl PlaneBuf {
613 );696 );
614 for row in 0..side {697 for row in 0..side {
615 for col in 0..side {698 for col in 0..side {
616- let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col])699+ let idx = row * side + col;
617- .clamp(0, 255) as u8;700+ let mut base = i32::from(prediction[idx]);
701+ if let Some((alpha_q3, ac_q3)) = cfl {
702+ base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255);
703+ }
704+ let sample = (base + residual[idx]).clamp(0, 255) as u8;
618 self.data[(y + row) * self.width + x + col] = sample;705 self.data[(y + row) * self.width + x + col] = sample;
619 }706 }
620 }707 }
@@ -643,6 +730,7 @@ fn read_plane(
643 side: usize,730 side: usize,
644 tx_side: usize,731 tx_side: usize,
645 base_q_idx: u8,732 base_q_idx: u8,
733+ cfl: Option<(i32, &[i32])>,
646 ) -> Result<Vec<i32>> {734 ) -> Result<Vec<i32>> {
647 let skip_ctx = if plane_idx == 0 {735 let skip_ctx = if plane_idx == 0 {
648 0736 0
@@ -669,7 +757,7 @@ fn read_plane(
669 full757 full
670 };758 };
671 let residual = dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), tx_type);759 let residual = dequant_and_inverse_typed(&levels, side, 8, i32::from(base_q_idx), tx_type);
672- plane.reconstruct(x, y, side, predict_mode, reach, &residual);760+ plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl);
673 Ok(if tx_side == side {761 Ok(if tx_side == side {
674 residual_grid_placeholder(&levels, side)762 residual_grid_placeholder(&levels, side)
675 } else {763 } else {
@@ -708,7 +796,7 @@ fn decode_block(
708 ) -> Result<()> {796 ) -> Result<()> {
709 let (r, c) = at;797 let (r, c) = at;
710 let (px, py) = (c * SUB, r * SUB);798 let (px, py) = (c * SUB, r * SUB);
711- let (skip, mode) = read_intra_mode(799+ let (skip, mode, alpha) = read_intra_mode(
712 dec,800 dec,
713 cdfs,801 cdfs,
714 neighbours.above_mode[c],802 neighbours.above_mode[c],
@@ -722,7 +810,8 @@ fn decode_block(
722 if skip {810 if skip {
723 // A skipped block codes no residual syntax at all (spec 5.11.34):811 // A skipped block codes no residual syntax at all (spec 5.11.34):
724 // straight prediction, on every plane.812 // straight prediction, on every plane.
725- y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side]);813+ y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None);
814+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
726 u.reconstruct(815 u.reconstruct(
727 cpx,816 cpx,
728 cpy,817 cpy,
@@ -730,6 +819,7 @@ fn decode_block(
730 DC_PRED,819 DC_PRED,
731 Reach::none(),820 Reach::none(),
732 &vec![0i32; chroma_side * chroma_side],821 &vec![0i32; chroma_side * chroma_side],
822+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
733 );823 );
734 v.reconstruct(824 v.reconstruct(
735 cpx,825 cpx,
@@ -738,6 +828,7 @@ fn decode_block(
738 DC_PRED,828 DC_PRED,
739 Reach::none(),829 Reach::none(),
740 &vec![0i32; chroma_side * chroma_side],830 &vec![0i32; chroma_side * chroma_side],
831+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
741 );832 );
742 luma_grid = vec![0i32; side * side];833 luma_grid = vec![0i32; side * side];
743 u_grid = vec![0i32; chroma_side * chroma_side];834 u_grid = vec![0i32; chroma_side * chroma_side];
@@ -746,8 +837,9 @@ fn decode_block(
746 let around = neighbours.around(at, side);837 let around = neighbours.around(at, side);
747 luma_grid = read_plane(838 luma_grid = read_plane(
748 dec, cdfs, luma_set, scans.0, 0, around[0], mode, mode, reach, y, px, py, side,839 dec, cdfs, luma_set, scans.0, 0, around[0], mode, mode, reach, y, px, py, side,
749- luma_tx, base_q_idx,840+ luma_tx, base_q_idx, None,
750 )?;841 )?;
842+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
751 u_grid = read_plane(843 u_grid = read_plane(
752 dec,844 dec,
753 cdfs,845 cdfs,
@@ -764,6 +856,7 @@ fn decode_block(
764 chroma_side,856 chroma_side,
765 chroma_tx,857 chroma_tx,
766 base_q_idx,858 base_q_idx,
859+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
767 )?;860 )?;
768 v_grid = read_plane(861 v_grid = read_plane(
769 dec,862 dec,
@@ -781,6 +874,7 @@ fn decode_block(
781 chroma_side,874 chroma_side,
782 chroma_tx,875 chroma_tx,
783 base_q_idx,876 base_q_idx,
877+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
784 )?;878 )?;
785 }879 }
786 neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);880 neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);
@@ -823,15 +917,32 @@ fn decode_leaf8(
823 // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec917 // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
824 // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other918 // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other
825 // `decode_block` caller at 16x16 and up.919 // `decode_block` caller at 16x16 and up.
826- let (skip, mode) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?;920+ let (skip, mode, alpha) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?;
827 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);921 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
828 let reach = Reach::of(8, px, py, y.width, y.height);922 let reach = Reach::of(8, px, py, y.width, y.height);
829 let (cpx, cpy) = (px / 2, py / 2);923 let (cpx, cpy) = (px / 2, py / 2);
830 let (luma_grid, u_grid, v_grid);924 let (luma_grid, u_grid, v_grid);
831 if skip {925 if skip {
832- y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64]);926+ y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64], None);
833- u.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]);927+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
834- v.reconstruct(cpx, cpy, 4, DC_PRED, Reach::none(), &vec![0i32; 16]);928+ u.reconstruct(
929+ cpx,
930+ cpy,
931+ 4,
932+ DC_PRED,
933+ Reach::none(),
934+ &vec![0i32; 16],
935+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
936+ );
937+ v.reconstruct(
938+ cpx,
939+ cpy,
940+ 4,
941+ DC_PRED,
942+ Reach::none(),
943+ &vec![0i32; 16],
944+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
945+ );
835 luma_grid = vec![0i32; 64];946 luma_grid = vec![0i32; 64];
836 u_grid = vec![0i32; 16];947 u_grid = vec![0i32; 16];
837 v_grid = vec![0i32; 16];948 v_grid = vec![0i32; 16];
@@ -853,7 +964,9 @@ fn decode_leaf8(
853 8,964 8,
854 TX8,965 TX8,
855 base_q_idx,966 base_q_idx,
967+ None,
856 )?;968 )?;
969+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
857 u_grid = read_plane(970 u_grid = read_plane(
858 dec,971 dec,
859 cdfs,972 cdfs,
@@ -870,6 +983,7 @@ fn decode_leaf8(
870 4,983 4,
871 TX4,984 TX4,
872 base_q_idx,985 base_q_idx,
986+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
873 )?;987 )?;
874 v_grid = read_plane(988 v_grid = read_plane(
875 dec,989 dec,
@@ -887,6 +1001,7 @@ fn decode_leaf8(
887 4,1001 4,
888 TX4,1002 TX4,
889 base_q_idx,1003 base_q_idx,
1004+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
890 )?;1005 )?;
891 }1006 }
892 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);1007 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
@@ -1641,9 +1756,13 @@ fn decode_inter_block(
1641 }1756 }
1642 }1757 }
1643 let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);1758 let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
1644- if uv_mode != DC_PRED {1759+ let alpha = if uv_mode == DC_PRED {
1645- return Err(unsupported("a chroma-from-luma mode (round 2)"));1760+ None
1646- }1761+ } else if uv_mode == UV_CFL_PRED {
1762+ Some(read_cfl_alphas(dec, cdfs))
1763+ } else {
1764+ return Err(unsupported("a directional chroma mode (round 2)"));
1765+ };
1647 mode_for_tx = mode;1766 mode_for_tx = mode;
1648 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);1767 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
1649 for dr in 0..side / 4 {1768 for dr in 0..side / 4 {
@@ -1664,7 +1783,8 @@ fn decode_inter_block(
1664 1783
1665 let reach = Reach::of(side, px, py, y.width, y.height);1784 let reach = Reach::of(side, px, py, y.width, y.height);
1666 if skip {1785 if skip {
1667- y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side]);1786+ y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None);
1787+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
1668 u.reconstruct(1788 u.reconstruct(
1669 cpx,1789 cpx,
1670 cpy,1790 cpy,
@@ -1672,6 +1792,7 @@ fn decode_inter_block(
1672 DC_PRED,1792 DC_PRED,
1673 Reach::none(),1793 Reach::none(),
1674 &vec![0i32; chroma_side * chroma_side],1794 &vec![0i32; chroma_side * chroma_side],
1795+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1675 );1796 );
1676 v.reconstruct(1797 v.reconstruct(
1677 cpx,1798 cpx,
@@ -1680,6 +1801,7 @@ fn decode_inter_block(
1680 DC_PRED,1801 DC_PRED,
1681 Reach::none(),1802 Reach::none(),
1682 &vec![0i32; chroma_side * chroma_side],1803 &vec![0i32; chroma_side * chroma_side],
1804+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1683 );1805 );
1684 luma_grid = vec![0i32; side * side];1806 luma_grid = vec![0i32; side * side];
1685 u_grid = vec![0i32; chroma_side * chroma_side];1807 u_grid = vec![0i32; chroma_side * chroma_side];
@@ -1702,7 +1824,9 @@ fn decode_inter_block(
1702 side,1824 side,
1703 luma_tx,1825 luma_tx,
1704 base_q_idx,1826 base_q_idx,
1827+ None,
1705 )?;1828 )?;
1829+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
1706 u_grid = read_plane(1830 u_grid = read_plane(
1707 dec,1831 dec,
1708 cdfs,1832 cdfs,
@@ -1719,6 +1843,7 @@ fn decode_inter_block(
1719 chroma_side,1843 chroma_side,
1720 chroma_tx,1844 chroma_tx,
1721 base_q_idx,1845 base_q_idx,
1846+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1722 )?;1847 )?;
1723 v_grid = read_plane(1848 v_grid = read_plane(
1724 dec,1849 dec,
@@ -1736,6 +1861,7 @@ fn decode_inter_block(
1736 chroma_side,1861 chroma_side,
1737 chroma_tx,1862 chroma_tx,
1738 base_q_idx,1863 base_q_idx,
1864+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1739 )?;1865 )?;
1740 }1866 }
1741 }1867 }
crates/ec-av1/src/stream.rsmodified+46 -0
@@ -481,6 +481,52 @@ mod tests {
481 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");481 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
482 }482 }
483 483
484+ /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but at
485+ /// 32x32 (below `is_cfl_allowed`'s 32x32 luma bound as a *single*
486+ /// undivided block, spec 5.11.5) rather than 64x64 -- a whole 64x64 SB
487+ /// is never CFL-eligible itself, only its <=32x32 partitions, and
488+ /// `testsrc2`/`gradients` at 64x64 with rect/ab/1to4/angle-delta search
489+ /// disabled reliably pick a whole-SB `PARTITION_NONE` (probed
490+ /// lane-av1real r2), so no fixture at that size exercises CfL at all.
491+ /// A 32x32 frame is exactly the whole-block, CFL-allowed case, and
492+ /// lane-av1real r2 found `gradients` there reliably selects
493+ /// `UV_CFL_PRED` (`uv_mode=13`) across a crf sweep -- `testsrc2` there
494+ /// stays DC. Proves the CfL port this round added, not just DC-chroma
495+ /// decode: pixel-exact against ffmpeg's own decode, not just "decoded".
496+ #[test]
497+ #[ignore = "r2 checkpoint: LUMA (not chroma) decodes near-flat 126-129 where ffmpeg shows a 41-82 gradient on the pinned CfL stream — the remaining bug is upstream of the CfL chroma math this round added; isolate the luma path first (r3)"]
498+ fn a_real_libaom_cfl_stream_decodes_pixel_exact() {
499+ if !have_ffmpeg() {
500+ eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg");
501+ return;
502+ }
503+ let (width, height) = (32, 32);
504+ let stream = libaom_encode_with(
505+ "gradients=size=32x32:c0=red:c1=blue:c2=green:rate=1:seed=42",
506+ width,
507+ height,
508+ 15,
509+ &[
510+ "-threads",
511+ "1",
512+ "-row-mt",
513+ "0",
514+ "-tile-columns",
515+ "0",
516+ "-tile-rows",
517+ "0",
518+ "-enable-cdef",
519+ "0",
520+ ],
521+ )
522+ .expect("ffmpeg encode");
523+ let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream");
524+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1);
525+ assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg");
526+ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg");
527+ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
528+ }
529+
484 /// scratch: isolate a pinned mismatching stream's first divergent pixel.530 /// scratch: isolate a pinned mismatching stream's first divergent pixel.
485 #[test]531 #[test]
486 #[ignore]532 #[ignore]