Superres upscaler, pinned against real libaom code
Browse files at this commitc262edf253608de100b0cc2648848ceb464426b5Parent: 08cce92- feat
- superres upscaler, pinned against real libaom code
2 changed files +191 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/superres.rs +190 -0
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -30,6 +30,7 @@ pub mod mvstack; | |||
| 30 | pub mod obu; | 30 | pub mod obu; |
| 31 | pub mod quant; | 31 | pub mod quant; |
| 32 | pub mod sequence; | 32 | pub mod sequence; |
| 33 | +mod superres; | ||
| 33 | pub mod stream; | 34 | pub mod stream; |
| 34 | pub mod tile; | 35 | pub mod tile; |
| 35 | pub mod transform; | 36 | pub mod transform; |
crates/ec-av1/src/superres.rsadded+190 -0
| @@ -0,0 +1,190 @@ | |||
| 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`). | ||
| 13 | + | ||
| 14 | +/// `RS_SUBPEL_BITS`. | ||
| 15 | +const RS_SUBPEL_BITS: u32 = 6; | ||
| 16 | +/// `RS_SCALE_SUBPEL_BITS`. | ||
| 17 | +const RS_SCALE_SUBPEL_BITS: u32 = 14; | ||
| 18 | +/// `RS_SCALE_SUBPEL_MASK`. | ||
| 19 | +const RS_SCALE_SUBPEL_MASK: i64 = (1i64 << RS_SCALE_SUBPEL_BITS) - 1; | ||
| 20 | +/// `RS_SCALE_EXTRA_BITS`. | ||
| 21 | +const RS_SCALE_EXTRA_BITS: u32 = RS_SCALE_SUBPEL_BITS - RS_SUBPEL_BITS; | ||
| 22 | +/// `RS_SCALE_EXTRA_OFF`. | ||
| 23 | +const RS_SCALE_EXTRA_OFF: i64 = 1i64 << (RS_SCALE_EXTRA_BITS - 1); | ||
| 24 | +/// `UPSCALE_NORMATIVE_TAPS`. | ||
| 25 | +const UPSCALE_NORMATIVE_TAPS: usize = 8; | ||
| 26 | +/// `FILTER_BITS`. | ||
| 27 | +const FILTER_BITS: u32 = 7; | ||
| 28 | + | ||
| 29 | +/// `av1_resize_filter_normative`, libaom `av1/common/resize.c`. 64 subpel | ||
| 30 | +/// phases x 8 taps. | ||
| 31 | +#[rustfmt::skip] | ||
| 32 | +const RESIZE_FILTER_NORMATIVE: [[i16; UPSCALE_NORMATIVE_TAPS]; 1 << RS_SUBPEL_BITS] = [ | ||
| 33 | + [0, 0, 0, 128, 0, 0, 0, 0], [0, 0, -1, 128, 2, -1, 0, 0], | ||
| 34 | + [0, 1, -3, 127, 4, -2, 1, 0], [0, 1, -4, 127, 6, -3, 1, 0], | ||
| 35 | + [0, 2, -6, 126, 8, -3, 1, 0], [0, 2, -7, 125, 11, -4, 1, 0], | ||
| 36 | + [-1, 2, -8, 125, 13, -5, 2, 0], [-1, 3, -9, 124, 15, -6, 2, 0], | ||
| 37 | + [-1, 3, -10, 123, 18, -6, 2, -1], [-1, 3, -11, 122, 20, -7, 3, -1], | ||
| 38 | + [-1, 4, -12, 121, 22, -8, 3, -1], [-1, 4, -13, 120, 25, -9, 3, -1], | ||
| 39 | + [-1, 4, -14, 118, 28, -9, 3, -1], [-1, 4, -15, 117, 30, -10, 4, -1], | ||
| 40 | + [-1, 5, -16, 116, 32, -11, 4, -1], [-1, 5, -16, 114, 35, -12, 4, -1], | ||
| 41 | + [-1, 5, -17, 112, 38, -12, 4, -1], [-1, 5, -18, 111, 40, -13, 5, -1], | ||
| 42 | + [-1, 5, -18, 109, 43, -14, 5, -1], [-1, 6, -19, 107, 45, -14, 5, -1], | ||
| 43 | + [-1, 6, -19, 105, 48, -15, 5, -1], [-1, 6, -19, 103, 51, -16, 5, -1], | ||
| 44 | + [-1, 6, -20, 101, 53, -16, 6, -1], [-1, 6, -20, 99, 56, -17, 6, -1], | ||
| 45 | + [-1, 6, -20, 97, 58, -17, 6, -1], [-1, 6, -20, 95, 61, -18, 6, -1], | ||
| 46 | + [-2, 7, -20, 93, 64, -18, 6, -2], [-2, 7, -20, 91, 66, -19, 6, -1], | ||
| 47 | + [-2, 7, -20, 88, 69, -19, 6, -1], [-2, 7, -20, 86, 71, -19, 6, -1], | ||
| 48 | + [-2, 7, -20, 84, 74, -20, 7, -2], [-2, 7, -20, 81, 76, -20, 7, -1], | ||
| 49 | + [-2, 7, -20, 79, 79, -20, 7, -2], [-1, 7, -20, 76, 81, -20, 7, -2], | ||
| 50 | + [-2, 7, -20, 74, 84, -20, 7, -2], [-1, 6, -19, 71, 86, -20, 7, -2], | ||
| 51 | + [-1, 6, -19, 69, 88, -20, 7, -2], [-1, 6, -19, 66, 91, -20, 7, -2], | ||
| 52 | + [-2, 6, -18, 64, 93, -20, 7, -2], [-1, 6, -18, 61, 95, -20, 6, -1], | ||
| 53 | + [-1, 6, -17, 58, 97, -20, 6, -1], [-1, 6, -17, 56, 99, -20, 6, -1], | ||
| 54 | + [-1, 6, -16, 53, 101, -20, 6, -1], [-1, 5, -16, 51, 103, -19, 6, -1], | ||
| 55 | + [-1, 5, -15, 48, 105, -19, 6, -1], [-1, 5, -14, 45, 107, -19, 6, -1], | ||
| 56 | + [-1, 5, -14, 43, 109, -18, 5, -1], [-1, 5, -13, 40, 111, -18, 5, -1], | ||
| 57 | + [-1, 4, -12, 38, 112, -17, 5, -1], [-1, 4, -12, 35, 114, -16, 5, -1], | ||
| 58 | + [-1, 4, -11, 32, 116, -16, 5, -1], [-1, 4, -10, 30, 117, -15, 4, -1], | ||
| 59 | + [-1, 3, -9, 28, 118, -14, 4, -1], [-1, 3, -9, 25, 120, -13, 4, -1], | ||
| 60 | + [-1, 3, -8, 22, 121, -12, 4, -1], [-1, 3, -7, 20, 122, -11, 3, -1], | ||
| 61 | + [-1, 2, -6, 18, 123, -10, 3, -1], [0, 2, -6, 15, 124, -9, 3, -1], | ||
| 62 | + [0, 2, -5, 13, 125, -8, 2, -1], [0, 1, -4, 11, 125, -7, 2, 0], | ||
| 63 | + [0, 1, -3, 8, 126, -6, 2, 0], [0, 1, -3, 6, 127, -4, 1, 0], | ||
| 64 | + [0, 1, -2, 4, 127, -3, 1, 0], [0, 0, -1, 2, 128, -1, 0, 0], | ||
| 65 | +]; | ||
| 66 | + | ||
| 67 | +/// `av1_get_upscale_convolve_step` (`resize.c`). | ||
| 68 | +fn upscale_convolve_step(in_length: i64, out_length: i64) -> i64 { | ||
| 69 | + ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | +/// `get_upscale_convolve_x0` (`resize.c`, static there). | ||
| 73 | +fn upscale_convolve_x0(in_length: i64, out_length: i64, x_step_qn: i64) -> i64 { | ||
| 74 | + let err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS); | ||
| 75 | + let x0 = (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) + out_length / 2) | ||
| 76 | + / out_length | ||
| 77 | + + RS_SCALE_EXTRA_OFF | ||
| 78 | + - err / 2; | ||
| 79 | + x0.rem_euclid(1i64 << RS_SCALE_SUBPEL_BITS) & RS_SCALE_SUBPEL_MASK | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +/// `av1_convolve_horiz_rs_c` (`convolve.c`): `clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS))`. | ||
| 83 | +fn round_power_of_two(sum: i32, bits: u32) -> i32 { | ||
| 84 | + (sum + (1 << (bits - 1))) >> bits | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +/// One row of the spec 7.16 horizontal upscale (`upscale_normative_rect` + | ||
| 88 | +/// `av1_convolve_horiz_rs_c`, single-tile case: `pad_left`/`pad_right` both | ||
| 89 | +/// true, replicating the row's own edge pixels rather than sampling a | ||
| 90 | +/// neighbour tile). `in_width` and `out_width` must both be positive; | ||
| 91 | +/// `row.len() == in_width` and `out.len() == out_width`. | ||
| 92 | +pub(crate) fn upscale_row(row: &[u8], out_width: usize, out: &mut [u8]) { | ||
| 93 | + let in_width = row.len(); | ||
| 94 | + debug_assert!(in_width > 0 && out_width > 0); | ||
| 95 | + debug_assert_eq!(out.len(), out_width); | ||
| 96 | + let x_step_qn = upscale_convolve_step(in_width as i64, out_width as i64); | ||
| 97 | + let x0_qn = upscale_convolve_x0(in_width as i64, out_width as i64, x_step_qn); | ||
| 98 | + | ||
| 99 | + // Edge-replicated padding: the real decoder samples out-of-frame | ||
| 100 | + // columns from the reference frame buffer's own border margin; | ||
| 101 | + // `upscale_normative_rect` fills that margin with `input[0]`/ | ||
| 102 | + // `input[width-1]` for a single-tile column before calling the | ||
| 103 | + // convolver. `UPSCALE_NORMATIVE_TAPS/2 + 1` columns are provably | ||
| 104 | + // enough (the C source's own `border_cols`); this pads generously. | ||
| 105 | + let pad = UPSCALE_NORMATIVE_TAPS + 2; | ||
| 106 | + let mut padded = vec![0u8; in_width + 2 * pad]; | ||
| 107 | + padded[..pad].fill(row[0]); | ||
| 108 | + padded[pad..pad + in_width].copy_from_slice(row); | ||
| 109 | + padded[pad + in_width..].fill(row[in_width - 1]); | ||
| 110 | + | ||
| 111 | + // `av1_convolve_horiz_rs_c` is called with `input - 1` and itself | ||
| 112 | + // subtracts `UPSCALE_NORMATIVE_TAPS/2 - 1` more; net offset from the | ||
| 113 | + // real column-0 pixel is `-(UPSCALE_NORMATIVE_TAPS/2)` == -4. | ||
| 114 | + let base = pad as i64 - (UPSCALE_NORMATIVE_TAPS as i64 / 2); | ||
| 115 | + let mut x_qn = x0_qn; | ||
| 116 | + for out_x in out.iter_mut() { | ||
| 117 | + let int_pel = x_qn >> RS_SCALE_SUBPEL_BITS; | ||
| 118 | + let filter_idx = ((x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS) as usize; | ||
| 119 | + let filter = &RESIZE_FILTER_NORMATIVE[filter_idx]; | ||
| 120 | + let mut sum = 0i32; | ||
| 121 | + for (k, &c) in filter.iter().enumerate() { | ||
| 122 | + let idx = base + int_pel + k as i64; | ||
| 123 | + sum += padded[idx as usize] as i32 * c as i32; | ||
| 124 | + } | ||
| 125 | + *out_x = round_power_of_two(sum, FILTER_BITS).clamp(0, 255) as u8; | ||
| 126 | + x_qn += x_step_qn; | ||
| 127 | + } | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +/// Spec 7.16, applied to a whole plane: `av1_upscale_normative_rows` | ||
| 131 | +/// widened to every row (`height2 == height`, no vertical scaling -- the | ||
| 132 | +/// spec only widens columns). `rows` is the plane's row-major byte buffer | ||
| 133 | +/// at `in_width`; returns a new buffer at `out_width`. | ||
| 134 | +pub(crate) fn upscale_plane(rows: &[u8], height: usize, in_width: usize, out_width: usize) -> Vec<u8> { | ||
| 135 | + debug_assert_eq!(rows.len(), height * in_width); | ||
| 136 | + let mut out = vec![0u8; height * out_width]; | ||
| 137 | + for r in 0..height { | ||
| 138 | + let src = &rows[r * in_width..(r + 1) * in_width]; | ||
| 139 | + let dst = &mut out[r * out_width..(r + 1) * out_width]; | ||
| 140 | + upscale_row(src, out_width, dst); | ||
| 141 | + } | ||
| 142 | + out | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +#[cfg(test)] | ||
| 146 | +mod tests { | ||
| 147 | + use super::*; | ||
| 148 | + | ||
| 149 | + /// Pinned against libaom's own compiled code, not a second | ||
| 150 | + /// transcription of it: a standalone C harness linked against | ||
| 151 | + /// `~/.cache/aom-oracle/build/libaom.a` calls the real (exported) | ||
| 152 | + /// `av1_get_upscale_convolve_step` and `av1_convolve_horiz_rs_c` with | ||
| 153 | + /// the real (exported) `av1_resize_filter_normative` table -- the only | ||
| 154 | + /// piece reimplemented in C rather than called is the one-line static | ||
| 155 | + /// `get_upscale_convolve_x0` formula, copied verbatim in both places. | ||
| 156 | + /// This is the class `shared-oracle-blindness` guard: a bug in the | ||
| 157 | + /// 64x8 table above or in the convolution/rounding kernel is caught | ||
| 158 | + /// here independently of this module's own logic. | ||
| 159 | + #[test] | ||
| 160 | + fn upscale_row_matches_libaom_in8_out12() { | ||
| 161 | + let row = [10u8, 20, 30, 40, 50, 60, 70, 80]; | ||
| 162 | + let mut out = [0u8; 12]; | ||
| 163 | + upscale_row(&row, 12, &mut out); | ||
| 164 | + assert_eq!(out, [9, 14, 22, 29, 35, 42, 48, 55, 61, 68, 76, 81]); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + #[test] | ||
| 168 | + fn upscale_row_matches_libaom_in8_out16() { | ||
| 169 | + let row = [10u8, 20, 30, 40, 50, 60, 70, 80]; | ||
| 170 | + let mut out = [0u8; 16]; | ||
| 171 | + upscale_row(&row, 16, &mut out); | ||
| 172 | + assert_eq!( | ||
| 173 | + out, | ||
| 174 | + [9, 12, 17, 23, 28, 32, 37, 43, 48, 53, 58, 62, 67, 73, 78, 81] | ||
| 175 | + ); | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + /// A flat row must upscale to the same flat value everywhere: every | ||
| 179 | + /// filter phase's 8 taps sum to exactly 128 (`FILTER_BITS`'s unity | ||
| 180 | + /// gain), so a constant input passes through unchanged regardless of | ||
| 181 | + /// which phase samples it -- a cheap sanity check independent of the | ||
| 182 | + /// captured-row pins above. | ||
| 183 | + #[test] | ||
| 184 | + fn upscale_row_of_a_flat_input_is_flat() { | ||
| 185 | + let row = [200u8; 8]; | ||
| 186 | + let mut out = [0u8; 12]; | ||
| 187 | + upscale_row(&row, 12, &mut out); | ||
| 188 | + assert_eq!(out, [200u8; 12]); | ||
| 189 | + } | ||
| 190 | +} | ||