forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

deblock_mbaff.rs530 lines, 17.9 KiBHistoryRawDownload
1//! In-loop deblocking of an MBAFF frame (clause 8.7 with MbaffFrameFlag 1).
2//!
3//! Macroblocks are filtered in macroblock address order: pair by pair, top
4//! macroblock then bottom. The progressive filter in [`crate::decoder`] has
5//! one fixed geometry; here the geometry depends on whether the macroblock
6//! and its neighbours are frame or field macroblocks:
7//!
8//! * a field macroblock filters its horizontal edges along the rows of its own
9//! parity (a row step of two picture rows);
10//! * the left macroblock edge pairs every row of the current macroblock with
11//! the sample to its left in the *same picture row*, which belongs to
12//! different left macroblocks from row to row when the pairs differ in mode,
13//! so its strength and QP are derived per row;
14//! * the top edge of a frame macroblock under a field pair is filtered twice,
15//! once per parity, against the two field macroblocks of the pair above;
16//! * `mixedModeEdgeFlag` edges never get a strength below 1.
17//!
18//! This is the plain per-sample reading of the clause, not the fast path: the
19//! progressive filter stays the one the speed budget is spent on.
20
21use crate::deblock::{chroma_qp, edge_params, filter_chroma_line, filter_luma_line};
22use crate::decoder::{BsSide, bs_between, bs_side};
23use crate::dpb::{PicPlane, Picture, SliceParams};
24use crate::entropy::{FLAG_DECODED, FLAG_PCM, FLAG_TRANS8X8};
25use crate::sample::Sample;
26
27/// Boundary strength of clause 8.7.2.1 for MBAFF frames.
28#[allow(clippy::too_many_arguments)]
29fn bs_mbaff(
30 p: &BsSide,
31 q: &BsSide,
32 mb_edge: bool,
33 vertical: bool,
34 p_field: bool,
35 q_field: bool,
36) -> u8 {
37 if p.intra || q.intra {
38 return if mb_edge && (vertical || (!p_field && !q_field)) {
39 4
40 } else {
41 3
42 };
43 }
44 if p.coded || q.coded {
45 return 2;
46 }
47 if p_field != q_field {
48 return 1; // mixedModeEdgeFlag
49 }
50 // Neither intra nor coded: only the motion comparison is left. A field
51 // macroblock compares vertical vectors in field units (limit 2).
52 bs_between(p, q, false, q_field, vertical)
53}
54
55/// One row (vertical edge) or column (horizontal edge) group of an edge: the
56/// macroblock on the p side and the strength.
57#[derive(Clone, Copy)]
58struct Seg {
59 p_slot: usize,
60 bs: u8,
61}
62
63/// Filter an MBAFF frame in place.
64pub(crate) fn deblock_mbaff<S: Sample>(pic: &mut Picture<S>) {
65 let w = pic.mb_w;
66 let pairs = w * pic.mb_h / 2;
67 for pair in 0..pairs {
68 let (px, pr) = (pair % w, pair / w);
69 for b in 0..2 {
70 deblock_mb(pic, px, 2 * pr + b);
71 }
72 }
73}
74
75#[allow(clippy::needless_range_loop)]
76fn deblock_mb<S: Sample>(pic: &mut Picture<S>, px: usize, slot_y: usize) {
77 let w = pic.mb_w;
78 let slot = slot_y * w + px;
79 if pic.mb_flags[slot] & FLAG_DECODED == 0 {
80 return;
81 }
82 let sid = pic.mb_slice[slot];
83 let sp: SliceParams = pic.slices[sid as usize];
84 if sp.disable_deblock_idc == 1 {
85 return;
86 }
87 let bd = pic.bd;
88 let max = (1i32 << bd) - 1;
89 let qp_bd = 6 * (bd as i32 - 8);
90 let pr = slot_y / 2;
91 let b = slot_y & 1;
92 let q_field = pic.mb_field[slot] != 0;
93 let (y0, step) = if q_field {
94 (pr * 32 + b, 2usize)
95 } else {
96 (slot_y * 16, 1)
97 };
98 let cmb_h = pic.cmb_h;
99 let cat = pic.cat;
100 let c422 = cat == 2;
101 let cy0 = if q_field {
102 pr * 2 * cmb_h + b
103 } else {
104 slot_y * cmb_h
105 };
106 let (bx0, by0) = (px * 4, slot_y * 4);
107
108 let decoded = |s: usize| pic.mb_flags[s] & FLAG_DECODED != 0;
109 let cross_ok = |s: usize| sp.disable_deblock_idc != 2 || pic.mb_slice[s] == sid;
110 let qp_of = |s: usize| -> i32 {
111 if pic.mb_flags[s] & FLAG_PCM != 0 {
112 0
113 } else {
114 i32::from(pic.mb_qp[s])
115 }
116 };
117 let internal = |e: usize| e.is_multiple_of(2) || pic.mb_flags[slot] & FLAG_TRANS8X8 == 0;
118
119 // ---- boundary strengths ----
120 // Vertical edges: per luma row. Edge 0 is the left macroblock edge.
121 let mut bs_v = [[0u8; 16]; 4];
122 // The p-side macroblock of each row of the left edge (luma) and of each
123 // chroma row.
124 let mut left_p = [0usize; 16];
125 let mut left_pc = [0usize; 16];
126 let mut filter_left = false;
127 if px > 0 {
128 let lt = (2 * pr) * w + px - 1;
129 if decoded(lt) && cross_ok(lt) {
130 filter_left = true;
131 let lf = pic.mb_field[lt] != 0;
132 for k in 0..16 {
133 let rp = if q_field { 2 * k + b } else { 16 * b + k };
134 let (ps, prow) = if lf {
135 (lt + (rp & 1) * w, rp >> 1)
136 } else {
137 (lt + (rp >= 16) as usize * w, rp % 16)
138 };
139 left_p[k] = ps;
140 let pside = bs_side(pic, (px - 1) * 4 + 3, (ps / w) * 4 + prow / 4);
141 let qside = bs_side(pic, bx0, by0 + k / 4);
142 bs_v[0][k] = bs_mbaff(&pside, &qside, true, true, lf, q_field);
143 }
144 for k in 0..cmb_h {
145 let rp = if q_field { 2 * k + b } else { cmb_h * b + k };
146 left_pc[k] = if lf {
147 lt + (rp & 1) * w
148 } else {
149 lt + (rp >= cmb_h) as usize * w
150 };
151 }
152 }
153 }
154 let cur_intra = pic.mb_flags[slot] & crate::entropy::FLAG_INTER == 0;
155 for e in 1..4 {
156 if !internal(e) {
157 continue;
158 }
159 for j in 0..4 {
160 let bs = if cur_intra {
161 3
162 } else {
163 bs_mbaff(
164 &bs_side(pic, bx0 + e - 1, by0 + j),
165 &bs_side(pic, bx0 + e, by0 + j),
166 false,
167 true,
168 q_field,
169 q_field,
170 )
171 };
172 bs_v[e][j * 4..j * 4 + 4].fill(bs);
173 }
174 }
175 // Horizontal edges: per 4-column segment. Edge 0 may be two edges.
176 let mut top: [Option<[Seg; 4]>; 2] = [None, None];
177 // Row step and first q row offset of the top edge passes.
178 let mut top_two = false;
179 let mut top_pstep = step; // row step of the top edge (field mode: 2)
180 {
181 let mut single = |p_slot: usize, p_field: bool, pstep: usize, pass: usize| {
182 let mut segs = [Seg { p_slot, bs: 0 }; 4];
183 for j in 0..4 {
184 let pside = bs_side(pic, bx0 + j, (p_slot / w) * 4 + 3);
185 let qside = bs_side(pic, bx0 + j, by0);
186 segs[j].bs = bs_mbaff(&pside, &qside, true, false, p_field, q_field);
187 }
188 top[pass] = Some(segs);
189 top_pstep = pstep;
190 };
191 if !q_field && b == 1 {
192 single(slot - w, false, 1, 0);
193 } else if pr > 0 {
194 let at = (2 * (pr - 1)) * w + px;
195 if decoded(at) && cross_ok(at) {
196 let af = pic.mb_field[at] != 0;
197 if !q_field {
198 if !af {
199 single(at + w, false, 1, 0);
200 } else {
201 top_two = true;
202 single(at, true, 2, 0);
203 single(at + w, true, 2, 1);
204 }
205 } else if af {
206 single(at + b * w, true, 2, 0);
207 } else {
208 single(at + w, false, 2, 0);
209 }
210 }
211 }
212 }
213 let mut bs_h = [[0u8; 4]; 4];
214 for e in 1..4 {
215 // 4:2:2 chroma filters the horizontal edges luma skips under the 8x8
216 // transform, on the strength those luma edges would have had.
217 if !internal(e) && !c422 {
218 continue;
219 }
220 for j in 0..4 {
221 bs_h[e][j] = if cur_intra {
222 3
223 } else {
224 bs_mbaff(
225 &bs_side(pic, bx0 + j, by0 + e - 1),
226 &bs_side(pic, bx0 + j, by0 + e),
227 false,
228 false,
229 q_field,
230 q_field,
231 )
232 };
233 }
234 }
235
236 // ---- luma (and Cb/Cr for ChromaArrayType 3, coded like luma) ----
237 for comp in 0..if cat == 3 { 3 } else { 1 } {
238 let stride = plane_of(pic, comp).stride;
239 let qpf = |s: usize| -> i32 {
240 if comp == 0 {
241 qp_of(s)
242 } else {
243 let sl = pic.slices[pic.mb_slice[s] as usize];
244 let off = if comp == 1 {
245 sl.cb_qp_offset
246 } else {
247 sl.cr_qp_offset
248 };
249 let q = if pic.mb_flags[s] & FLAG_PCM != 0 {
250 0
251 } else {
252 i32::from(pic.mb_qp[s])
253 };
254 chroma_qp(q, off, qp_bd)
255 }
256 };
257 let lossless = |s: usize| {
258 pic.bypass_enabled
259 && pic.mb_flags[s] & FLAG_PCM == 0
260 && i32::from(pic.mb_qp[s]) + qp_bd == 0
261 };
262 let vstride = stride * step;
263 let qp_q = qpf(slot);
264 // Vertical edges.
265 for e in 0..4 {
266 if e == 0 && !filter_left {
267 continue;
268 }
269 if e > 0 && !internal(e) {
270 continue;
271 }
272 for k in 0..16 {
273 let bs = bs_v[e][k];
274 if bs == 0 {
275 continue;
276 }
277 let qp_p = if e == 0 { qpf(left_p[k]) } else { qp_q };
278 let params = edge_params(
279 (qp_p + qp_q + 1) >> 1,
280 sp.alpha_offset,
281 sp.beta_offset,
282 bs,
283 bd,
284 );
285 if params.alpha == 0 || params.beta == 0 {
286 continue;
287 }
288 let idx = match comp {
289 0 => &pic.y,
290 1 => &pic.cb,
291 _ => &pic.cr,
292 }
293 .at(px * 16 + e * 4, y0 + k * step);
294 let (pl, ql) = (e == 0 && lossless(left_p[k]), lossless(slot));
295 protect_line(
296 &mut match comp {
297 0 => &mut pic.y,
298 1 => &mut pic.cb,
299 _ => &mut pic.cr,
300 }
301 .data,
302 idx,
303 1,
304 pl,
305 ql,
306 |d| filter_luma_line(d, max, idx, 1, &params),
307 );
308 }
309 }
310 // Horizontal edges.
311 for e in 0..4 {
312 if e > 0 && !internal(e) {
313 continue;
314 }
315 let passes = if e == 0 {
316 if top_two { 2 } else { 1 }
317 } else {
318 1
319 };
320 for pass in 0..passes {
321 let segs: [Seg; 4] = if e == 0 {
322 match top[pass] {
323 Some(s) => s,
324 None => continue,
325 }
326 } else {
327 let mut s = [Seg {
328 p_slot: slot,
329 bs: 0,
330 }; 4];
331 for j in 0..4 {
332 s[j].bs = bs_h[e][j];
333 }
334 s
335 };
336 let (q0_row, row_step) = if e == 0 {
337 (y0 + if top_two { pass } else { 0 }, top_pstep)
338 } else {
339 (y0 + e * 4 * step, step)
340 };
341 let hs = stride * row_step;
342 for j in 0..4 {
343 let bs = segs[j].bs;
344 if bs == 0 {
345 continue;
346 }
347 let qp_p = if e == 0 { qpf(segs[j].p_slot) } else { qp_q };
348 let params = edge_params(
349 (qp_p + qp_q + 1) >> 1,
350 sp.alpha_offset,
351 sp.beta_offset,
352 bs,
353 bd,
354 );
355 if params.alpha == 0 || params.beta == 0 {
356 continue;
357 }
358 for kx in 0..4 {
359 let idx = match comp {
360 0 => &pic.y,
361 1 => &pic.cb,
362 _ => &pic.cr,
363 }
364 .at(px * 16 + j * 4 + kx, q0_row);
365 let (pl, ql) = (e == 0 && lossless(segs[j].p_slot), lossless(slot));
366 protect_line(
367 &mut match comp {
368 0 => &mut pic.y,
369 1 => &mut pic.cb,
370 _ => &mut pic.cr,
371 }
372 .data,
373 idx,
374 hs,
375 pl,
376 ql,
377 |d| filter_luma_line(d, max, idx, hs, &params),
378 );
379 }
380 }
381 }
382 }
383 let _ = vstride;
384 }
385
386 // ---- chroma ----
387 for comp in 0..if matches!(cat, 1 | 2) { 2 } else { 0 } {
388 let off_sel = |s: &SliceParams| {
389 if comp == 0 {
390 s.cb_qp_offset
391 } else {
392 s.cr_qp_offset
393 }
394 };
395 let cqp = |s: usize| -> i32 {
396 let sl = pic.slices[pic.mb_slice[s] as usize];
397 if pic.mb_flags[s] & FLAG_PCM != 0 {
398 chroma_qp(0, off_sel(&sl), qp_bd)
399 } else {
400 chroma_qp(i32::from(pic.mb_qp[s]), off_sel(&sl), qp_bd)
401 }
402 };
403 let cq_q = cqp(slot);
404 let plane: &PicPlane<S> = if comp == 0 { &pic.cb } else { &pic.cr };
405 let stride = plane.stride;
406 // Vertical edges (chroma x = 0, 4 <-> luma edges 0, 2).
407 for e in 0..2 {
408 if e == 0 && !filter_left {
409 continue;
410 }
411 for k in 0..cmb_h {
412 // The luma row whose strength a chroma row inherits.
413 let lrow = if c422 {
414 k
415 } else if q_field && e == 0 && {
416 let lt = (2 * pr) * w + px - 1;
417 pic.mb_field[lt] == 0
418 } {
419 k * 2
420 } else {
421 (k >> 1) * 4 + (k & 1)
422 };
423 let bs = bs_v[e * 2][lrow];
424 if bs == 0 {
425 continue;
426 }
427 let cq_p = if e == 0 { cqp(left_pc[k]) } else { cq_q };
428 let params = edge_params(
429 (cq_p + cq_q + 1) >> 1,
430 sp.alpha_offset,
431 sp.beta_offset,
432 bs,
433 bd,
434 );
435 if params.alpha == 0 || params.beta == 0 {
436 continue;
437 }
438 let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
439 let idx = plane.at(px * 8 + e * 4, cy0 + k * step);
440 filter_chroma_line(&mut plane.data, max, idx, 1, &params);
441 }
442 }
443 // Horizontal edges (chroma y = 0, 4 <-> luma edges 0, 2; for 4:2:2
444 // chroma y = 0, 4, 8, 12 <-> luma edges 0..3).
445 for e in 0..if c422 { 4 } else { 2 } {
446 let passes = if e == 0 {
447 if top_two { 2 } else { 1 }
448 } else {
449 1
450 };
451 for pass in 0..passes {
452 let segs: [Seg; 4] = if e == 0 {
453 match top[pass] {
454 Some(s) => s,
455 None => continue,
456 }
457 } else {
458 let mut s = [Seg {
459 p_slot: slot,
460 bs: 0,
461 }; 4];
462 for j in 0..4 {
463 s[j].bs = bs_h[if c422 { e } else { 2 }][j];
464 }
465 s
466 };
467 let (q0_row, row_step) = if e == 0 {
468 (cy0 + if top_two { pass } else { 0 }, top_pstep)
469 } else {
470 (cy0 + (if c422 { e * 4 } else { 4 }) * step, step)
471 };
472 let hs = stride * row_step;
473 for k in 0..8 {
474 let seg = segs[k / 2];
475 if seg.bs == 0 {
476 continue;
477 }
478 let cq_p = if e == 0 { cqp(seg.p_slot) } else { cq_q };
479 let params = edge_params(
480 (cq_p + cq_q + 1) >> 1,
481 sp.alpha_offset,
482 sp.beta_offset,
483 seg.bs,
484 bd,
485 );
486 if params.alpha == 0 || params.beta == 0 {
487 continue;
488 }
489 let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
490 let idx = plane.at(px * 8 + k, q0_row);
491 filter_chroma_line(&mut plane.data, max, idx, hs, &params);
492 }
493 }
494 }
495 }
496}
497
498fn plane_of<S: Sample>(pic: &Picture<S>, comp: usize) -> &PicPlane<S> {
499 match comp {
500 0 => &pic.y,
501 1 => &pic.cb,
502 _ => &pic.cr,
503 }
504}
505
506/// Run one line filter, then put back the samples of a lossless side
507/// (8.7.2.2: transform-bypass macroblocks are never modified).
508fn protect_line<S: Sample>(
509 data: &mut [S],
510 idx: usize,
511 step: usize,
512 p_lossless: bool,
513 q_lossless: bool,
514 f: impl FnOnce(&mut [S]),
515) {
516 if !p_lossless && !q_lossless {
517 f(data);
518 return;
519 }
520 let mut saved = [data[idx]; 8];
521 for (i, v) in saved.iter_mut().enumerate() {
522 *v = data[idx + i * step - 4 * step];
523 }
524 f(data);
525 for (i, v) in saved.iter().enumerate() {
526 if (i < 4 && p_lossless) || (i >= 4 && q_lossless) {
527 data[idx + i * step - 4 * step] = *v;
528 }
529 }
530}