forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

superres.rs255 lines, 12.0 KiBHistoryRawDownload
1//! AV1 spec 7.16 upscaling process (`av1_upscale_normative_rows` in
2//! libaom's `av1/common/resize.c`) -- the horizontal-only linear-filter
3//! upscale that runs on the decoded frame after deblocking and CDEF (and,
4//! once this crate supports loop restoration, BEFORE it -- libaom's
5//! `decodeframe.c` calls `superres_post_decode()` between `av1_cdef_frame`
6//! and `av1_loop_restoration_filter_frame`, not after LR; the charter's
7//! anchor note that superres runs "after loop restoration" does not match
8//! the source and is corrected here).
9//!
10//! Every constant and the 64x8 `av1_resize_filter_normative` coefficient
11//! table below is copied verbatim from libaom `aom_dsp/aom_filter.h` and
12//! `av1/common/resize.c` (`AOM_VERSION=v3.13.3`, `~/.cache/aom-oracle`).
13use crate::hits::hit;
14
15/// `RS_SUBPEL_BITS`.
16const RS_SUBPEL_BITS: u32 = 6;
17/// `RS_SCALE_SUBPEL_BITS`.
18const RS_SCALE_SUBPEL_BITS: u32 = 14;
19/// `RS_SCALE_SUBPEL_MASK`.
20const RS_SCALE_SUBPEL_MASK: i64 = (1i64 << RS_SCALE_SUBPEL_BITS) - 1;
21/// `RS_SCALE_EXTRA_BITS`.
22const RS_SCALE_EXTRA_BITS: u32 = RS_SCALE_SUBPEL_BITS - RS_SUBPEL_BITS;
23/// `RS_SCALE_EXTRA_OFF`.
24const RS_SCALE_EXTRA_OFF: i64 = 1i64 << (RS_SCALE_EXTRA_BITS - 1);
25/// `UPSCALE_NORMATIVE_TAPS`.
26const UPSCALE_NORMATIVE_TAPS: usize = 8;
27/// `FILTER_BITS`.
28const FILTER_BITS: u32 = 7;
29
30/// `av1_resize_filter_normative`, libaom `av1/common/resize.c`. 64 subpel
31/// phases x 8 taps.
32#[rustfmt::skip]
33const RESIZE_FILTER_NORMATIVE: [[i16; UPSCALE_NORMATIVE_TAPS]; 1 << RS_SUBPEL_BITS] = [
34 [0, 0, 0, 128, 0, 0, 0, 0], [0, 0, -1, 128, 2, -1, 0, 0],
35 [0, 1, -3, 127, 4, -2, 1, 0], [0, 1, -4, 127, 6, -3, 1, 0],
36 [0, 2, -6, 126, 8, -3, 1, 0], [0, 2, -7, 125, 11, -4, 1, 0],
37 [-1, 2, -8, 125, 13, -5, 2, 0], [-1, 3, -9, 124, 15, -6, 2, 0],
38 [-1, 3, -10, 123, 18, -6, 2, -1], [-1, 3, -11, 122, 20, -7, 3, -1],
39 [-1, 4, -12, 121, 22, -8, 3, -1], [-1, 4, -13, 120, 25, -9, 3, -1],
40 [-1, 4, -14, 118, 28, -9, 3, -1], [-1, 4, -15, 117, 30, -10, 4, -1],
41 [-1, 5, -16, 116, 32, -11, 4, -1], [-1, 5, -16, 114, 35, -12, 4, -1],
42 [-1, 5, -17, 112, 38, -12, 4, -1], [-1, 5, -18, 111, 40, -13, 5, -1],
43 [-1, 5, -18, 109, 43, -14, 5, -1], [-1, 6, -19, 107, 45, -14, 5, -1],
44 [-1, 6, -19, 105, 48, -15, 5, -1], [-1, 6, -19, 103, 51, -16, 5, -1],
45 [-1, 6, -20, 101, 53, -16, 6, -1], [-1, 6, -20, 99, 56, -17, 6, -1],
46 [-1, 6, -20, 97, 58, -17, 6, -1], [-1, 6, -20, 95, 61, -18, 6, -1],
47 [-2, 7, -20, 93, 64, -18, 6, -2], [-2, 7, -20, 91, 66, -19, 6, -1],
48 [-2, 7, -20, 88, 69, -19, 6, -1], [-2, 7, -20, 86, 71, -19, 6, -1],
49 [-2, 7, -20, 84, 74, -20, 7, -2], [-2, 7, -20, 81, 76, -20, 7, -1],
50 [-2, 7, -20, 79, 79, -20, 7, -2], [-1, 7, -20, 76, 81, -20, 7, -2],
51 [-2, 7, -20, 74, 84, -20, 7, -2], [-1, 6, -19, 71, 86, -20, 7, -2],
52 [-1, 6, -19, 69, 88, -20, 7, -2], [-1, 6, -19, 66, 91, -20, 7, -2],
53 [-2, 6, -18, 64, 93, -20, 7, -2], [-1, 6, -18, 61, 95, -20, 6, -1],
54 [-1, 6, -17, 58, 97, -20, 6, -1], [-1, 6, -17, 56, 99, -20, 6, -1],
55 [-1, 6, -16, 53, 101, -20, 6, -1], [-1, 5, -16, 51, 103, -19, 6, -1],
56 [-1, 5, -15, 48, 105, -19, 6, -1], [-1, 5, -14, 45, 107, -19, 6, -1],
57 [-1, 5, -14, 43, 109, -18, 5, -1], [-1, 5, -13, 40, 111, -18, 5, -1],
58 [-1, 4, -12, 38, 112, -17, 5, -1], [-1, 4, -12, 35, 114, -16, 5, -1],
59 [-1, 4, -11, 32, 116, -16, 5, -1], [-1, 4, -10, 30, 117, -15, 4, -1],
60 [-1, 3, -9, 28, 118, -14, 4, -1], [-1, 3, -9, 25, 120, -13, 4, -1],
61 [-1, 3, -8, 22, 121, -12, 4, -1], [-1, 3, -7, 20, 122, -11, 3, -1],
62 [-1, 2, -6, 18, 123, -10, 3, -1], [0, 2, -6, 15, 124, -9, 3, -1],
63 [0, 2, -5, 13, 125, -8, 2, -1], [0, 1, -4, 11, 125, -7, 2, 0],
64 [0, 1, -3, 8, 126, -6, 2, 0], [0, 1, -3, 6, 127, -4, 1, 0],
65 [0, 1, -2, 4, 127, -3, 1, 0], [0, 0, -1, 2, 128, -1, 0, 0],
66];
67
68/// `av1_get_upscale_convolve_step` (`resize.c`).
69fn upscale_convolve_step(in_length: i64, out_length: i64) -> i64 {
70 ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length
71}
72
73/// `get_upscale_convolve_x0` (`resize.c`, static there).
74fn upscale_convolve_x0(in_length: i64, out_length: i64, x_step_qn: i64) -> i64 {
75 let err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS);
76 let x0 = (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) + out_length / 2)
77 / out_length
78 + RS_SCALE_EXTRA_OFF
79 - err / 2;
80 x0.rem_euclid(1i64 << RS_SCALE_SUBPEL_BITS) & RS_SCALE_SUBPEL_MASK
81}
82
83/// `av1_convolve_horiz_rs_c` (`convolve.c`): `clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS))`.
84fn round_power_of_two(sum: i32, bits: u32) -> i32 {
85 (sum + (1 << (bits - 1))) >> bits
86}
87
88/// One row of the spec 7.16 horizontal upscale (`upscale_normative_rect` +
89/// `av1_convolve_horiz_rs_c`, single-tile case: `pad_left`/`pad_right` both
90/// true, replicating the row's own edge pixels rather than sampling a
91/// neighbour tile). `in_width` and `out_width` must both be positive;
92/// `row.len() == in_width` and `out.len() == out_width`.
93/// `bit_depth` only picks the output clamp (`clip_pixel`/`clip_pixel_highbd`
94/// in libaom's `av1_convolve_horiz_rs_c`); the filter, its `Round2` and the
95/// edge padding are bit-depth independent (spec 7.16).
96pub(crate) fn upscale_row(
97 row: &[u16],
98 real_right_margin: &[u16],
99 out_width: usize,
100 out: &mut [u16],
101 bit_depth: u32,
102) {
103 let in_width = row.len();
104 debug_assert!(in_width > 0 && out_width > 0);
105 debug_assert_eq!(out.len(), out_width);
106 let x_step_qn = upscale_convolve_step(in_width as i64, out_width as i64);
107 let x0_qn = upscale_convolve_x0(in_width as i64, out_width as i64, x_step_qn);
108
109 // Right-edge padding: r1 assumed a pure edge-replicate of `row`'s own
110 // last column (matching `upscale_normative_rect`'s single-tile
111 // in-memory fill), but the real decoder's reconstructed buffer already
112 // holds genuine decoded samples past `frame_width` out to the
113 // mi-aligned `true_width` (the coding block straddling the frame edge)
114 // -- libaom's border extension replicates from THAT last real column,
115 // not from `frame_width - 1`. `real_right_margin` (from
116 // `decode::take_last_frame_wide_margin`) supplies those real
117 // pixels first; only once it runs out (or is empty, e.g. `fw` was
118 // already mi-aligned) does this fall back to r1's replicate. Pinned
119 // column-by-column against real libaom via `scripts/superres-pin-
120 // harness.c`'s `row6-realedgeval` case (r3).
121 let pad = UPSCALE_NORMATIVE_TAPS + 2;
122 let mut padded = vec![0u16; in_width + 2 * pad];
123 padded[..pad].fill(row[0]);
124 padded[pad..pad + in_width].copy_from_slice(row);
125 let real_n = real_right_margin.len().min(pad);
126 padded[pad + in_width..pad + in_width + real_n].copy_from_slice(&real_right_margin[..real_n]);
127 let replicate_from = real_right_margin.last().copied().unwrap_or(row[in_width - 1]);
128 padded[pad + in_width + real_n..].fill(replicate_from);
129
130 // `av1_convolve_horiz_rs_c` is called with `input - 1` and itself
131 // subtracts `UPSCALE_NORMATIVE_TAPS/2 - 1` more; net offset from the
132 // real column-0 pixel is `-(UPSCALE_NORMATIVE_TAPS/2)` == -4.
133 let base = pad as i64 - (UPSCALE_NORMATIVE_TAPS as i64 / 2);
134 let mut x_qn = x0_qn;
135 for out_x in out.iter_mut() {
136 let int_pel = x_qn >> RS_SCALE_SUBPEL_BITS;
137 let filter_idx = ((x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS) as usize;
138 let filter = &RESIZE_FILTER_NORMATIVE[filter_idx];
139 let mut sum = 0i32;
140 for (k, &c) in filter.iter().enumerate() {
141 let idx = base + int_pel + k as i64;
142 sum += padded[idx as usize] as i32 * c as i32;
143 }
144 *out_x = round_power_of_two(sum, FILTER_BITS).clamp(0, (1 << bit_depth) - 1) as u16;
145 x_qn += x_step_qn;
146 }
147}
148
149/// Every row of one decoded plane, upscaled by spec 7.16 from `in_w` to
150/// `out_w` (`av1_upscale_normative_rows`; the process is horizontal-only,
151/// so the row count is unchanged). `data`/`stride` are the decoder's own
152/// reconstruction buffer, whose columns `[in_w, true_w)` hold the real
153/// decoded samples of the coding block straddling the frame edge -- libaom's
154/// border extension replicates from that last real column, so they are
155/// handed to [`upscale_row`] as its right margin (see its doc).
156pub(crate) fn upscale_plane_strided(
157 data: &[u16],
158 stride: usize,
159 in_w: usize,
160 in_h: usize,
161 true_w: usize,
162 out_w: usize,
163 bit_depth: u32,
164) -> Vec<u16> {
165 let mut out = vec![0u16; in_h * out_w];
166 for r in 0..in_h {
167 let row = &data[r * stride..r * stride + in_w];
168 let margin = &data[r * stride + in_w..r * stride + true_w.max(in_w)];
169 upscale_row(row, margin, out_w, &mut out[r * out_w..(r + 1) * out_w], bit_depth);
170 }
171 out
172}
173
174thread_local! {
175 /// Firing count for the superres gate (class `gate-blind-to-feature`):
176 /// how many whole pictures actually ran through [`upscale_plane_strided`].
177 static SUPERRES_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
178}
179
180/// Current value of [`SUPERRES_HITS`].
181#[allow(dead_code)]
182pub(crate) fn superres_hits() -> usize {
183 SUPERRES_HITS.with(|c| c.get())
184}
185
186/// Counts one whole upscaled picture (all three planes) for [`SUPERRES_HITS`].
187pub(crate) fn note_upscaled_picture() {
188 hit!(SUPERRES_HITS);
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194
195 /// Pinned against libaom's own compiled code, not a second
196 /// transcription of it: a standalone C harness linked against
197 /// `~/.cache/aom-oracle/build/libaom.a` calls the real (exported)
198 /// `av1_get_upscale_convolve_step` and `av1_convolve_horiz_rs_c` with
199 /// the real (exported) `av1_resize_filter_normative` table -- the only
200 /// piece reimplemented in C rather than called is the one-line static
201 /// `get_upscale_convolve_x0` formula, copied verbatim in both places.
202 /// This is the class `shared-oracle-blindness` guard: a bug in the
203 /// 64x8 table above or in the convolution/rounding kernel is caught
204 /// here independently of this module's own logic.
205 #[test]
206 fn upscale_row_matches_libaom_in8_out12() {
207 let row = [10u16, 20, 30, 40, 50, 60, 70, 80];
208 let mut out = [0u16; 12];
209 upscale_row(&row, &[], 12, &mut out, 8);
210 assert_eq!(out, [9, 14, 22, 29, 35, 42, 48, 55, 61, 68, 76, 81]);
211 }
212
213 #[test]
214 fn upscale_row_matches_libaom_in8_out16() {
215 let row = [10u16, 20, 30, 40, 50, 60, 70, 80];
216 let mut out = [0u16; 16];
217 upscale_row(&row, &[], 16, &mut out, 8);
218 assert_eq!(
219 out,
220 [9, 12, 17, 23, 28, 32, 37, 43, 48, 53, 58, 62, 67, 73, 78, 81]
221 );
222 }
223
224 /// A flat row must upscale to the same flat value everywhere: every
225 /// filter phase's 8 taps sum to exactly 128 (`FILTER_BITS`'s unity
226 /// gain), so a constant input passes through unchanged regardless of
227 /// which phase samples it -- a cheap sanity check independent of the
228 /// captured-row pins above.
229 #[test]
230 fn upscale_row_of_a_flat_input_is_flat() {
231 let row = [200u16; 8];
232 let mut out = [0u16; 12];
233 upscale_row(&row, &[], 12, &mut out, 8);
234 assert_eq!(out, [200u16; 12]);
235 }
236
237 /// r3: the real 43->64 failing case, pinned against real libaom
238 /// including the right-edge margin (`scripts/superres-pin-harness.c`'s
239 /// `row6-realedgeval` case -- input row + real trailing decoded pixel
240 /// 140, not a replicate of the frame-edge pixel 141). Column 62 (0
241 /// indexed) is exactly the pixel this gate mismatched by 1 before the
242 /// margin fix.
243 #[test]
244 fn upscale_row_with_real_margin_matches_libaom_in43_out64() {
245 let row: [u16; 43] = [
246 102, 102, 103, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
247 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 127, 128, 129, 130, 130, 131, 131,
248 133, 134, 136, 137, 138, 139, 140, 140, 141,
249 ];
250 let real_margin = [140u16];
251 let mut out = [0u16; 64];
252 upscale_row(&row, &real_margin, 64, &mut out, 8);
253 assert_eq!(out[62], 141, "column 62 must match libaom, not the old replicate-padded 140");
254 }
255}