forge
Explore
Log in

Don't you dare go Hollow

Filter-intra predictor (spec 7.11.2.3), refusal lifted

Browse files at this commit

f90ea1e936b42ffbf678fbf9eca25ef8acf8aa0bAhmet Kaan GümüşParent: 6efce81
  • feat
    • filter-intra predictor (spec 7.11.2.3), refusal lifted
Show full message
feat(av1): filter-intra predictor (spec 7.11.2.3), refusal lifted

Recursive filter intra: 4x2 sub-blocks, the 7-tap av1_filter_intra_taps
table, five modes. use_filter_intra==1 blocks now predict instead of
erroring. Existing suite green; the real-stream gate that proves the
path against ffmpeg lands next.
crates/ec-av1/src/decode.rsmodified+83 -27
@@ -624,7 +624,7 @@ fn read_intra_mode(
624624 cfl: bool,
625625 side: usize,
626626 enable_filter_intra: bool,
627-) -> Result<(bool, usize, Option<(i32, i32)>)> {
627+) -> Result<(bool, usize, Option<(i32, i32)>, Option<usize>)> {
628628 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
629629 let skip = dec.symbol(&mut cdfs.skip[0]) != 0;
630630 if trace {
@@ -669,6 +669,7 @@ fn read_intra_mode(
669669 // of the tile (the pinned CfL stream's actual bug: partition/skip/mode
670670 // matched bit-exact, then `eob_pt` read 5 instead of 6, because this
671671 // symbol's bits were never consumed).
672+ let mut filter_intra = None;
672673 if mode == DC_PRED
673674 && enable_filter_intra
674675 && let Some(class) = filter_intra_size_class(side)
@@ -681,18 +682,14 @@ fn read_intra_mode(
681682 );
682683 }
683684 if use_filter_intra {
684- // libaom still reads `filter_intra_mode` to stay in sync, but
685- // this decoder does not implement filter-intra prediction
686- // (spec 7.11.2.3's 5-tap recursive filter) -- refuse rather
687- // than silently miscode the reconstruction, the same pattern
688- // as this crate's other `Error::unsupported` refusals.
689- let _ = dec.symbol(&mut cdfs.filter_intra_mode);
690- return Err(unsupported(
691- "a filter-intra block (this decoder does not implement the 5.11.14 recursive predictor)",
692- ));
685+ let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
686+ if trace {
687+ eprintln!("TRACE filter_intra_mode value={fi_mode}");
688+ }
689+ filter_intra = Some(fi_mode);
693690 }
694691 }
695- Ok((skip, mode, alpha))
692+ Ok((skip, mode, alpha, filter_intra))
696693 }
697694
698695 /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth
@@ -830,17 +827,29 @@ impl PlaneBuf {
830827 reach: Reach,
831828 residual: &[i32],
832829 cfl: Option<(i32, &[i32])>,
830+ filter_intra: Option<usize>,
833831 ) {
834832 let (above, left, corner) = self.edges(x, y, side, reach);
835833 let mut prediction = vec![0u8; side * side];
836- predict(
837- mode as u8,
838- above.as_deref(),
839- left.as_deref(),
840- corner,
841- side,
842- &mut prediction,
843- );
834+ if let Some(fi_mode) = filter_intra {
835+ crate::intra::predict_filter_intra(
836+ fi_mode,
837+ above.as_deref(),
838+ left.as_deref(),
839+ corner,
840+ side,
841+ &mut prediction,
842+ );
843+ } else {
844+ predict(
845+ mode as u8,
846+ above.as_deref(),
847+ left.as_deref(),
848+ corner,
849+ side,
850+ &mut prediction,
851+ );
852+ }
844853 for row in 0..side {
845854 for col in 0..side {
846855 let idx = row * side + col;
@@ -878,6 +887,7 @@ fn read_plane(
878887 tx_side: usize,
879888 base_q_idx: u8,
880889 cfl: Option<(i32, &[i32])>,
890+ filter_intra: Option<usize>,
881891 ) -> Result<Vec<i32>> {
882892 let skip_ctx = if plane_idx == 0 {
883893 0
@@ -911,7 +921,7 @@ fn read_plane(
911921 &residual[..4.min(residual.len())]
912922 );
913923 }
914- plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl);
924+ plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl, filter_intra);
915925 Ok(if tx_side == side {
916926 residual_grid_placeholder(&levels, side)
917927 } else {
@@ -951,7 +961,7 @@ fn decode_block(
951961 ) -> Result<()> {
952962 let (r, c) = at;
953963 let (px, py) = (c * SUB, r * SUB);
954- let (skip, mode, alpha) = read_intra_mode(
964+ let (skip, mode, alpha, filter_intra) = read_intra_mode(
955965 dec,
956966 cdfs,
957967 neighbours.above_mode[c],
@@ -967,7 +977,16 @@ fn decode_block(
967977 if skip {
968978 // A skipped block codes no residual syntax at all (spec 5.11.34):
969979 // straight prediction, on every plane.
970- y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None);
980+ y.reconstruct(
981+ px,
982+ py,
983+ side,
984+ mode,
985+ reach,
986+ &vec![0i32; side * side],
987+ None,
988+ filter_intra,
989+ );
971990 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
972991 u.reconstruct(
973992 cpx,
@@ -977,6 +996,7 @@ fn decode_block(
977996 Reach::none(),
978997 &vec![0i32; chroma_side * chroma_side],
979998 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
999+ None,
9801000 );
9811001 v.reconstruct(
9821002 cpx,
@@ -986,6 +1006,7 @@ fn decode_block(
9861006 Reach::none(),
9871007 &vec![0i32; chroma_side * chroma_side],
9881008 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1009+ None,
9891010 );
9901011 luma_grid = vec![0i32; side * side];
9911012 u_grid = vec![0i32; chroma_side * chroma_side];
@@ -994,7 +1015,7 @@ fn decode_block(
9941015 let around = neighbours.around(at, side);
9951016 luma_grid = read_plane(
9961017 dec, cdfs, luma_set, scans.0, 0, around[0], mode, mode, reach, y, px, py, side,
997- luma_tx, base_q_idx, None,
1018+ luma_tx, base_q_idx, None, filter_intra,
9981019 )?;
9991020 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
10001021 u_grid = read_plane(
@@ -1014,6 +1035,7 @@ fn decode_block(
10141035 chroma_tx,
10151036 base_q_idx,
10161037 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1038+ None,
10171039 )?;
10181040 v_grid = read_plane(
10191041 dec,
@@ -1032,6 +1054,7 @@ fn decode_block(
10321054 chroma_tx,
10331055 base_q_idx,
10341056 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1057+ None,
10351058 )?;
10361059 }
10371060 neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);
@@ -1076,7 +1099,7 @@ fn decode_leaf8(
10761099 // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
10771100 // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other
10781101 // `decode_block` caller at 16x16 and up.
1079- let (skip, mode, alpha) = read_intra_mode(
1102+ let (skip, mode, alpha, filter_intra) = read_intra_mode(
10801103 dec,
10811104 cdfs,
10821105 above_mode,
@@ -1090,7 +1113,7 @@ fn decode_leaf8(
10901113 let (cpx, cpy) = (px / 2, py / 2);
10911114 let (luma_grid, u_grid, v_grid);
10921115 if skip {
1093- y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64], None);
1116+ y.reconstruct(px, py, 8, mode, reach, &vec![0i32; 64], None, filter_intra);
10941117 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
10951118 u.reconstruct(
10961119 cpx,
@@ -1100,6 +1123,7 @@ fn decode_leaf8(
11001123 Reach::none(),
11011124 &vec![0i32; 16],
11021125 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1126+ None,
11031127 );
11041128 v.reconstruct(
11051129 cpx,
@@ -1109,6 +1133,7 @@ fn decode_leaf8(
11091133 Reach::none(),
11101134 &vec![0i32; 16],
11111135 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1136+ None,
11121137 );
11131138 luma_grid = vec![0i32; 64];
11141139 u_grid = vec![0i32; 16];
@@ -1132,6 +1157,7 @@ fn decode_leaf8(
11321157 TX8,
11331158 base_q_idx,
11341159 None,
1160+ filter_intra,
11351161 )?;
11361162 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
11371163 u_grid = read_plane(
@@ -1151,6 +1177,7 @@ fn decode_leaf8(
11511177 TX4,
11521178 base_q_idx,
11531179 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1180+ None,
11541181 )?;
11551182 v_grid = read_plane(
11561183 dec,
@@ -1169,6 +1196,7 @@ fn decode_leaf8(
11691196 TX4,
11701197 base_q_idx,
11711198 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1199+ None,
11721200 )?;
11731201 }
11741202 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
@@ -2265,7 +2293,16 @@ fn decode_inter_block(
22652293
22662294 let reach = Reach::of(side, px, py, y.width, y.height);
22672295 if skip {
2268- y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side], None);
2296+ y.reconstruct(
2297+ px,
2298+ py,
2299+ side,
2300+ mode,
2301+ reach,
2302+ &vec![0i32; side * side],
2303+ None,
2304+ None,
2305+ );
22692306 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
22702307 u.reconstruct(
22712308 cpx,
@@ -2275,6 +2312,7 @@ fn decode_inter_block(
22752312 Reach::none(),
22762313 &vec![0i32; chroma_side * chroma_side],
22772314 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
2315+ None,
22782316 );
22792317 v.reconstruct(
22802318 cpx,
@@ -2284,6 +2322,7 @@ fn decode_inter_block(
22842322 Reach::none(),
22852323 &vec![0i32; chroma_side * chroma_side],
22862324 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
2325+ None,
22872326 );
22882327 luma_grid = vec![0i32; side * side];
22892328 u_grid = vec![0i32; chroma_side * chroma_side];
@@ -2307,6 +2346,7 @@ fn decode_inter_block(
23072346 luma_tx,
23082347 base_q_idx,
23092348 None,
2349+ None,
23102350 )?;
23112351 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
23122352 u_grid = read_plane(
@@ -2326,6 +2366,7 @@ fn decode_inter_block(
23262366 chroma_tx,
23272367 base_q_idx,
23282368 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
2369+ None,
23292370 )?;
23302371 v_grid = read_plane(
23312372 dec,
@@ -2344,6 +2385,7 @@ fn decode_inter_block(
23442385 chroma_tx,
23452386 base_q_idx,
23462387 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
2388+ None,
23472389 )?;
23482390 }
23492391 }
@@ -2634,7 +2676,16 @@ fn decode_inter_block8(
26342676
26352677 let reach = Reach::of(SIDE, px, py, y.width, y.height);
26362678 if skip {
2637- y.reconstruct(px, py, SIDE, mode, reach, &vec![0i32; SIDE * SIDE], None);
2679+ y.reconstruct(
2680+ px,
2681+ py,
2682+ SIDE,
2683+ mode,
2684+ reach,
2685+ &vec![0i32; SIDE * SIDE],
2686+ None,
2687+ None,
2688+ );
26382689 u.reconstruct(
26392690 cpx,
26402691 cpy,
@@ -2643,6 +2694,7 @@ fn decode_inter_block8(
26432694 Reach::none(),
26442695 &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
26452696 None,
2697+ None,
26462698 );
26472699 v.reconstruct(
26482700 cpx,
@@ -2652,6 +2704,7 @@ fn decode_inter_block8(
26522704 Reach::none(),
26532705 &vec![0i32; CHROMA_SIDE * CHROMA_SIDE],
26542706 None,
2707+ None,
26552708 );
26562709 luma_grid = vec![0i32; SIDE * SIDE];
26572710 u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
@@ -2675,6 +2728,7 @@ fn decode_inter_block8(
26752728 TX8,
26762729 base_q_idx,
26772730 None,
2731+ None,
26782732 )?;
26792733 u_grid = read_plane(
26802734 dec,
@@ -2693,6 +2747,7 @@ fn decode_inter_block8(
26932747 TX4,
26942748 base_q_idx,
26952749 None,
2750+ None,
26962751 )?;
26972752 v_grid = read_plane(
26982753 dec,
@@ -2711,6 +2766,7 @@ fn decode_inter_block8(
27112766 TX4,
27122767 base_q_idx,
27132768 None,
2769+ None,
27142770 )?;
27152771 }
27162772 }
crates/ec-av1/src/intra.rsmodified+133 -0
@@ -341,6 +341,139 @@ fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 {
341341 }
342342 }
343343
344+/// `Intra_Filter_Taps` (spec 7.11.2.3 / libaom `av1_filter_intra_taps`,
345+/// `av1/common/reconintra.c`): five recursive-filter modes (DC/V/H/D157/Paeth
346+/// variants, in that order), each predicting an 8-tap block of eight output
347+/// samples from seven already-known neighbours.
348+const FILTER_INTRA_TAPS: [[[i32; 7]; 8]; 5] = [
349+ [
350+ [-6, 10, 0, 0, 0, 12, 0],
351+ [-5, 2, 10, 0, 0, 9, 0],
352+ [-3, 1, 1, 10, 0, 7, 0],
353+ [-3, 1, 1, 2, 10, 5, 0],
354+ [-4, 6, 0, 0, 0, 2, 12],
355+ [-3, 2, 6, 0, 0, 2, 9],
356+ [-3, 2, 2, 6, 0, 2, 7],
357+ [-3, 1, 2, 2, 6, 3, 5],
358+ ],
359+ [
360+ [-10, 16, 0, 0, 0, 10, 0],
361+ [-6, 0, 16, 0, 0, 6, 0],
362+ [-4, 0, 0, 16, 0, 4, 0],
363+ [-2, 0, 0, 0, 16, 2, 0],
364+ [-10, 16, 0, 0, 0, 0, 10],
365+ [-6, 0, 16, 0, 0, 0, 6],
366+ [-4, 0, 0, 16, 0, 0, 4],
367+ [-2, 0, 0, 0, 16, 0, 2],
368+ ],
369+ [
370+ [-8, 8, 0, 0, 0, 16, 0],
371+ [-8, 0, 8, 0, 0, 16, 0],
372+ [-8, 0, 0, 8, 0, 16, 0],
373+ [-8, 0, 0, 0, 8, 16, 0],
374+ [-4, 4, 0, 0, 0, 0, 16],
375+ [-4, 0, 4, 0, 0, 0, 16],
376+ [-4, 0, 0, 4, 0, 0, 16],
377+ [-4, 0, 0, 0, 4, 0, 16],
378+ ],
379+ [
380+ [-2, 8, 0, 0, 0, 10, 0],
381+ [-1, 3, 8, 0, 0, 6, 0],
382+ [-1, 2, 3, 8, 0, 4, 0],
383+ [0, 1, 2, 3, 8, 2, 0],
384+ [-1, 4, 0, 0, 0, 3, 10],
385+ [-1, 3, 4, 0, 0, 4, 6],
386+ [-1, 2, 3, 4, 0, 4, 4],
387+ [-1, 2, 2, 3, 4, 3, 3],
388+ ],
389+ [
390+ [-12, 14, 0, 0, 0, 14, 0],
391+ [-10, 0, 14, 0, 0, 12, 0],
392+ [-9, 0, 0, 14, 0, 11, 0],
393+ [-8, 0, 0, 0, 14, 10, 0],
394+ [-10, 12, 0, 0, 0, 0, 14],
395+ [-9, 1, 12, 0, 0, 0, 12],
396+ [-8, 0, 0, 12, 0, 1, 11],
397+ [-7, 0, 0, 1, 12, 1, 9],
398+ ],
399+];
400+
401+/// `FILTER_INTRA_SCALE_BITS` (libaom `reconintra.h`): the taps' fixed-point
402+/// shift.
403+const FILTER_INTRA_SCALE_BITS: u32 = 4;
404+
405+/// Recursive filter-intra prediction (spec 7.11.2.3, libaom
406+/// `av1_filter_intra_predictor_c`): walks 4x2 sub-blocks left-to-right,
407+/// top-to-bottom, each of the eight output samples a linear combination of
408+/// seven already-decoded or already-predicted neighbours (never past the
409+/// block's own top row / left column -- unlike the directional modes, this
410+/// one never reaches beyond `side` samples of either edge).
411+///
412+/// `mode` is `0..=4`, indexing [`FILTER_INTRA_TAPS`]/`FILTER_DC_PRED`
413+/// through `FILTER_PAETH_PRED`. `above`/`left`/`corner` are exactly
414+/// [`predict`]'s edge arguments -- a side that does not exist is filled from
415+/// the side that does and a side that runs out is extended by repeating its
416+/// last sample, the same [`Edges::build`] rule every other mode uses, even
417+/// though this mode itself never reaches past its own `side` samples of
418+/// either edge.
419+///
420+/// # Panics
421+/// Panics when `dst` is not `side * side` long, or `side` is not a multiple
422+/// of 4 (spec `av1_filter_intra_allowed_bsize` never offers this mode past
423+/// 32x32, and only on square blocks: 4, 8, 16 or 32).
424+pub fn predict_filter_intra(
425+ mode: usize,
426+ above: Option<&[u8]>,
427+ left: Option<&[u8]>,
428+ corner: Option<u8>,
429+ side: usize,
430+ dst: &mut [u8],
431+) {
432+ assert_eq!(dst.len(), side * side, "the destination is the block");
433+ assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32");
434+ let edges = Edges::build(above, left, corner, side);
435+ let taps = &FILTER_INTRA_TAPS[mode];
436+ // A (side+1)-square buffer: row 0 / column 0 hold the corner and the
437+ // above/left edges, `buffer[r+1][c+1]` the block's own sample at (r, c).
438+ let mut buffer = vec![0i32; (side + 1) * (side + 1)];
439+ let at = |buffer: &[i32], r: usize, c: usize| buffer[r * (side + 1) + c];
440+ buffer[0] = edges.above(-1);
441+ for c in 0..side {
442+ buffer[c + 1] = edges.above(c as i32);
443+ }
444+ for r in 0..side {
445+ buffer[(r + 1) * (side + 1)] = edges.left(r as i32);
446+ }
447+ let mut r = 1;
448+ while r < side + 1 {
449+ let mut c = 1;
450+ while c < side + 1 {
451+ let p0 = at(&buffer, r - 1, c - 1);
452+ let p1 = at(&buffer, r - 1, c);
453+ let p2 = at(&buffer, r - 1, c + 1);
454+ let p3 = at(&buffer, r - 1, c + 2);
455+ let p4 = at(&buffer, r - 1, c + 3);
456+ let p5 = at(&buffer, r, c - 1);
457+ let p6 = at(&buffer, r + 1, c - 1);
458+ let p = [p0, p1, p2, p3, p4, p5, p6];
459+ for k in 0..8 {
460+ let (r_off, c_off) = (k >> 2, k & 3);
461+ let pr: i32 = taps[k].iter().zip(&p).map(|(t, s)| t * s).sum();
462+ let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, 255);
463+ let idx = (r + r_off) * (side + 1) + c + c_off;
464+ buffer[idx] = value;
465+ }
466+ c += 4;
467+ }
468+ r += 2;
469+ }
470+ for row in 0..side {
471+ for col in 0..side {
472+ dst[row * side + col] = at(&buffer, row + 1, col + 1) as u8;
473+ }
474+ }
475+}
476+
344477 /// `paeth_predict` (spec 7.11.2.2): of the three neighbours, the one nearest
345478 /// the gradient they describe.
346479 fn paeth(above: i32, left: i32, corner: i32) -> i32 {