forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB552edc4Tahinli2 months ago2048 commits

deblock.rs341 lines, 12.1 KiBHistoryRawDownload
1//! The in-loop deblocking filter (8.7.2).
2//!
3//! An intra-only stream predicts from its own *unfiltered* reconstruction
4//! (8.4.4.2.1 reads the samples prior to deblocking), so the filter changes
5//! nothing the encoder predicts from: it runs once over the finished picture,
6//! and what it produces is what a decoder shows. That keeps the reconstruction
7//! bit-identical to a decoder's output -- and the picture-hash SEI that rides
8//! on it honest -- while buying back the block edges the transform left.
9//!
10//! Every edge in an intra picture is a boundary between two intra blocks, so
11//! `bS` is 2 wherever an edge exists at all (8.7.2.4); the whole derivation
12//! reduces to *where* the transform-block boundaries are, which is what
13//! [`TuMap`] carries.
14
15use crate::transform::chroma_qp;
16
17/// beta' by Q (table 8-12, first column).
18const BETA: [i32; 52] = [
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
20 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64,
21];
22
23/// tC' by Q (table 8-12, second column).
24const TC: [i32; 54] = [
25 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3,
26 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24,
27];
28
29/// The size of the transform block covering each 4x4 luma position, as a
30/// `log2`, over a whole picture.
31///
32/// A transform block of side `S` is aligned to a multiple of `S`, so an edge at
33/// `x` is that block's own left edge -- and therefore a filtered boundary --
34/// exactly when `x % S == 0`. One number per 4x4 unit answers the question for
35/// every edge without recording edges at all.
36pub struct TuMap {
37 log2: Vec<u8>,
38 stride: usize,
39}
40
41impl TuMap {
42 /// An empty map for a picture of `width` x `height` luma samples.
43 pub fn new(width: usize, height: usize) -> TuMap {
44 TuMap {
45 log2: vec![2; (width / 4) * (height / 4)],
46 stride: width / 4,
47 }
48 }
49
50 /// Record that the transform block of side `1 << log2` covers the square at
51 /// `(x, y)`.
52 pub fn mark(&mut self, x: usize, y: usize, log2: u8) {
53 let side = 1usize << log2;
54 for by in (y..y + side).step_by(4) {
55 for bx in (x..x + side).step_by(4) {
56 let idx = (by / 4) * self.stride + bx / 4;
57 if idx < self.log2.len() {
58 self.log2[idx] = log2;
59 }
60 }
61 }
62 }
63
64 /// Copy one wavefront worker's band, `rows` 4x4 rows of it, into place.
65 pub fn absorb_band(&mut self, band_y0: usize, rows: usize, band: &[u8]) {
66 for row in 0..rows {
67 let dst = (band_y0 / 4 + row) * self.stride;
68 let src = row * self.stride;
69 if dst + self.stride <= self.log2.len() && src + self.stride <= band.len() {
70 self.log2[dst..dst + self.stride].copy_from_slice(&band[src..src + self.stride]);
71 }
72 }
73 }
74
75 /// Whether the vertical edge at luma column `x` is a transform-block
76 /// boundary for the four rows starting at `y`.
77 fn vertical_edge(&self, x: usize, y: usize) -> bool {
78 x > 0 && x.is_multiple_of(1usize << self.log2[(y / 4) * self.stride + x / 4])
79 }
80
81 /// Whether the horizontal edge at luma row `y` is a transform-block
82 /// boundary for the four columns starting at `x`.
83 fn horizontal_edge(&self, x: usize, y: usize) -> bool {
84 y > 0 && y.is_multiple_of(1usize << self.log2[(y / 4) * self.stride + x / 4])
85 }
86}
87
88/// One line across an edge, as offsets from the boundary: `p3..p0` on one side,
89/// `q0..q3` on the other. `step` is the distance between samples across the
90/// edge, `at` the index of `q0`.
91struct Line {
92 at: usize,
93 step: usize,
94}
95
96impl Line {
97 fn p(&self, plane: &[u8], i: usize) -> i32 {
98 i32::from(plane[self.at - (i + 1) * self.step])
99 }
100
101 fn q(&self, plane: &[u8], i: usize) -> i32 {
102 i32::from(plane[self.at + i * self.step])
103 }
104
105 fn set_p(&self, plane: &mut [u8], i: usize, value: i32) {
106 plane[self.at - (i + 1) * self.step] = value.clamp(0, 255) as u8;
107 }
108
109 fn set_q(&self, plane: &mut [u8], i: usize, value: i32) {
110 plane[self.at + i * self.step] = value.clamp(0, 255) as u8;
111 }
112}
113
114fn clip3(lo: i32, hi: i32, v: i32) -> i32 {
115 v.clamp(lo, hi)
116}
117
118/// Filter one four-line luma edge segment (8.7.2.5.3).
119fn luma_segment(plane: &mut [u8], lines: [Line; 4], beta: i32, tc: i32) {
120 let d_of = |line: &Line, plane: &[u8]| {
121 (
122 (line.p(plane, 2) - 2 * line.p(plane, 1) + line.p(plane, 0)).abs(),
123 (line.q(plane, 2) - 2 * line.q(plane, 1) + line.q(plane, 0)).abs(),
124 )
125 };
126 let (dp0, dq0) = d_of(&lines[0], plane);
127 let (dp3, dq3) = d_of(&lines[3], plane);
128 let (dpq0, dpq3) = (dp0 + dq0, dp3 + dq3);
129 let d = dpq0 + dpq3;
130 if d >= beta {
131 return;
132 }
133 let (dp, dq) = (dp0 + dp3, dq0 + dq3);
134
135 // 8.7.2.5.6: both outer lines have to want the strong filter.
136 let strong_on = |line: &Line, dpq: i32, plane: &[u8]| {
137 2 * dpq < (beta >> 2)
138 && (line.p(plane, 3) - line.p(plane, 0)).abs()
139 + (line.q(plane, 0) - line.q(plane, 3)).abs()
140 < (beta >> 3)
141 && (line.p(plane, 0) - line.q(plane, 0)).abs() < ((5 * tc + 1) >> 1)
142 };
143 let strong = strong_on(&lines[0], dpq0, plane) && strong_on(&lines[3], dpq3, plane);
144 let filter_p1 = dp < ((beta + (beta >> 1)) >> 3);
145 let filter_q1 = dq < ((beta + (beta >> 1)) >> 3);
146
147 for line in &lines {
148 let p: [i32; 4] = std::array::from_fn(|i| line.p(plane, i));
149 let q: [i32; 4] = std::array::from_fn(|i| line.q(plane, i));
150 if strong {
151 let clip = |base: i32, v: i32| clip3(base - 2 * tc, base + 2 * tc, v);
152 line.set_p(
153 plane,
154 0,
155 clip(
156 p[0],
157 (p[2] + 2 * p[1] + 2 * p[0] + 2 * q[0] + q[1] + 4) >> 3,
158 ),
159 );
160 line.set_p(plane, 1, clip(p[1], (p[2] + p[1] + p[0] + q[0] + 2) >> 2));
161 line.set_p(
162 plane,
163 2,
164 clip(p[2], (2 * p[3] + 3 * p[2] + p[1] + p[0] + q[0] + 4) >> 3),
165 );
166 line.set_q(
167 plane,
168 0,
169 clip(
170 q[0],
171 (p[1] + 2 * p[0] + 2 * q[0] + 2 * q[1] + q[2] + 4) >> 3,
172 ),
173 );
174 line.set_q(plane, 1, clip(q[1], (p[0] + q[0] + q[1] + q[2] + 2) >> 2));
175 line.set_q(
176 plane,
177 2,
178 clip(q[2], (p[0] + q[0] + q[1] + 3 * q[2] + 2 * q[3] + 4) >> 3),
179 );
180 } else {
181 let delta = (9 * (q[0] - p[0]) - 3 * (q[1] - p[1]) + 8) >> 4;
182 if delta.abs() >= tc * 10 {
183 continue;
184 }
185 let delta = clip3(-tc, tc, delta);
186 line.set_p(plane, 0, p[0] + delta);
187 line.set_q(plane, 0, q[0] - delta);
188 if filter_p1 {
189 let dp = clip3(
190 -(tc >> 1),
191 tc >> 1,
192 (((p[2] + p[0] + 1) >> 1) - p[1] + delta) >> 1,
193 );
194 line.set_p(plane, 1, p[1] + dp);
195 }
196 if filter_q1 {
197 let dq = clip3(
198 -(tc >> 1),
199 tc >> 1,
200 (((q[2] + q[0] + 1) >> 1) - q[1] - delta) >> 1,
201 );
202 line.set_q(plane, 1, q[1] + dq);
203 }
204 }
205 }
206}
207
208/// Filter one chroma line across an edge (8.7.2.5.5).
209fn chroma_line(plane: &mut [u8], line: &Line, tc: i32) {
210 let (p0, p1) = (line.p(plane, 0), line.p(plane, 1));
211 let (q0, q1) = (line.q(plane, 0), line.q(plane, 1));
212 let delta = clip3(-tc, tc, (((q0 - p0) << 2) + p1 - q1 + 4) >> 3);
213 line.set_p(plane, 0, p0 + delta);
214 line.set_q(plane, 0, q0 - delta);
215}
216
217/// Run the filter over a whole picture: every vertical edge first, then every
218/// horizontal one over what the vertical pass left (8.7.2).
219///
220/// `qp` is the picture's quantisation parameter, which is every block's here --
221/// nothing in this encoder writes `cu_qp_delta`.
222pub fn deblock(
223 rec_y: &mut [u8],
224 rec_cb: &mut [u8],
225 rec_cr: &mut [u8],
226 width: usize,
227 height: usize,
228 qp: i32,
229 tus: &TuMap,
230) {
231 // bS is 2 on every edge of an intra picture, so both look-ups are constant.
232 let beta = BETA[clip3(0, 51, qp) as usize];
233 let tc_luma = TC[clip3(0, 53, qp + 2) as usize];
234 let tc_chroma = TC[clip3(0, 53, chroma_qp(qp, 0) + 2) as usize];
235 if beta == 0 && tc_luma == 0 && tc_chroma == 0 {
236 return;
237 }
238 let (cw, ch) = (width / 2, height / 2);
239
240 for x in (8..width).step_by(8) {
241 for y in (0..height).step_by(4) {
242 if tus.vertical_edge(x, y) {
243 let lines = std::array::from_fn(|i| Line {
244 at: (y + i) * width + x,
245 step: 1,
246 });
247 luma_segment(rec_y, lines, beta, tc_luma);
248 }
249 }
250 }
251 for x_c in (8..cw).step_by(8) {
252 for y_c in (0..ch).step_by(2) {
253 if tus.vertical_edge(2 * x_c, 2 * y_c) {
254 for row in 0..2 {
255 let line = Line {
256 at: (y_c + row) * cw + x_c,
257 step: 1,
258 };
259 chroma_line(rec_cb, &line, tc_chroma);
260 chroma_line(rec_cr, &line, tc_chroma);
261 }
262 }
263 }
264 }
265
266 for y in (8..height).step_by(8) {
267 for x in (0..width).step_by(4) {
268 if tus.horizontal_edge(x, y) {
269 let lines = std::array::from_fn(|i| Line {
270 at: y * width + x + i,
271 step: width,
272 });
273 luma_segment(rec_y, lines, beta, tc_luma);
274 }
275 }
276 }
277 for y_c in (8..ch).step_by(8) {
278 for x_c in (0..cw).step_by(2) {
279 if tus.horizontal_edge(2 * x_c, 2 * y_c) {
280 for col in 0..2 {
281 let line = Line {
282 at: y_c * cw + x_c + col,
283 step: cw,
284 };
285 chroma_line(rec_cb, &line, tc_chroma);
286 chroma_line(rec_cr, &line, tc_chroma);
287 }
288 }
289 }
290 }
291}
292
293#[cfg(test)]
294mod tests {
295 use super::*;
296
297 /// A step across an 8x8 transform boundary, the case the filter exists for.
298 fn step_picture(w: usize, h: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
299 let mut y = vec![0u8; w * h];
300 for row in 0..h {
301 for col in 0..w {
302 y[row * w + col] = if col < 8 { 100 } else { 130 };
303 }
304 }
305 let c = vec![128u8; w / 2 * h / 2];
306 (y, c.clone(), c)
307 }
308
309 /// The filter has to actually reach the samples: a gate that only proves
310 /// bit-exactness against a decoder stays green if the filter never fires,
311 /// because a decoder told the same thing does nothing either.
312 #[test]
313 fn an_edge_between_two_transform_blocks_is_filtered() {
314 let (w, h) = (16, 16);
315 let (mut y, mut cb, mut cr) = step_picture(w, h);
316 let before = y.clone();
317 let mut tus = TuMap::new(w, h);
318 for by in (0..h).step_by(8) {
319 for bx in (0..w).step_by(8) {
320 tus.mark(bx, by, 3);
321 }
322 }
323 deblock(&mut y, &mut cb, &mut cr, w, h, 32, &tus);
324 assert_ne!(y, before, "the edge at x=8 was left alone");
325 assert!(y[7] > before[7], "p0 should be pulled up toward q0");
326 assert!(y[8] < before[8], "q0 should be pulled down toward p0");
327 }
328
329 /// The same picture inside one transform block has no interior boundary,
330 /// so nothing may move — this is what keeps the map honest.
331 #[test]
332 fn no_transform_boundary_means_no_filtering() {
333 let (w, h) = (16, 16);
334 let (mut y, mut cb, mut cr) = step_picture(w, h);
335 let before = y.clone();
336 let mut tus = TuMap::new(w, h);
337 tus.mark(0, 0, 4);
338 deblock(&mut y, &mut cb, &mut cr, w, h, 32, &tus);
339 assert_eq!(y, before);
340 }
341}