| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use crate::ctu::Pel; |
| 16 | use crate::transform::chroma_qp_bd; |
| 17 | |
| 18 | |
| 19 | const 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 | |
| 25 | const 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 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub struct TuMap { |
| 38 | log2: Vec<u8>, |
| 39 | stride: usize, |
| 40 | } |
| 41 | |
| 42 | impl TuMap { |
| 43 | |
| 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 | |
| 52 | |
| 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 | |
| 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 | |
| 77 | |
| 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 | |
| 83 | |
| 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 | |
| 90 | |
| 91 | |
| 92 | struct Line { |
| 93 | at: usize, |
| 94 | step: usize, |
| 95 | } |
| 96 | |
| 97 | impl 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 | |
| 115 | fn clip3(lo: i32, hi: i32, v: i32) -> i32 { |
| 116 | v.clamp(lo, hi) |
| 117 | } |
| 118 | |
| 119 | |
| 120 | fn 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 | |
| 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 | |
| 224 | fn 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 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | #[allow(clippy::too_many_arguments)] |
| 238 | pub 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 | |
| 249 | let scale = 1i32 << (bit_depth - 8); |
| 250 | let max = (1i32 << bit_depth) - 1; |
| 251 | |
| 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)] |
| 314 | mod tests { |
| 315 | use super::*; |
| 316 | |
| 317 | |
| 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 | |
| 330 | |
| 331 | |
| 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 | |
| 350 | |
| 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 | } |