forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

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