]> git.nihav.org Git - nihav.git/blame_incremental - nihav-itu/src/codecs/h264/dsp/mod.rs
core/scale: add options support
[nihav.git] / nihav-itu / src / codecs / h264 / dsp / mod.rs
... / ...
CommitLineData
1use nihav_core::frame::*;
2use nihav_codec_support::codecs::blockdsp::*;
3use nihav_codec_support::codecs::MV;
4
5#[cfg(not(debug_assertions))]
6mod release;
7#[cfg(not(debug_assertions))]
8use release::*;
9#[cfg(debug_assertions)]
10mod debug;
11#[cfg(debug_assertions)]
12use debug::*;
13
14pub const CHROMA_QUANTS: [u8; 52] = [
15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30,
17 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38,
18 39, 39, 39, 39
19];
20
21pub const CHROMA_DC_SCAN: [usize; 4] = [ 0, 1, 2, 3];
22pub const ZIGZAG: [usize; 16] = [
23 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
24];
25pub const ZIGZAG1: [usize; 15] = [
26 0, 3, 7, 4, 1, 2, 5, 8, 11, 12, 9, 6, 10, 13, 14
27];
28/*pub const IL_SCAN: [usize; 16] = [
29 0, 4, 1, 8, 12, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15
30];*/
31pub const ZIGZAG8X8: [usize; 64] = [
32 0, 1, 8, 16, 9, 2, 3, 10,
33 17, 24, 32, 25, 18, 11, 4, 5,
34 12, 19, 26, 33, 40, 48, 41, 34,
35 27, 20, 13, 6, 7, 14, 21, 28,
36 35, 42, 49, 56, 57, 50, 43, 36,
37 29, 22, 15, 23, 30, 37, 44, 51,
38 58, 59, 52, 45, 38, 31, 39, 46,
39 53, 60, 61, 54, 47, 55, 62, 63
40];
41
42const LEVEL_SCALE: [[i16; 6]; 3] = [
43 [ 10, 11, 13, 14, 16, 18 ],
44 [ 16, 18, 20, 23, 25, 29 ],
45 [ 13, 14, 16, 18, 20, 23 ]
46];
47
48pub fn chroma_dc_transform(blk: &mut [i16; 4], qp: u8) {
49 let t0 = blk[0] + blk[2];
50 let t1 = blk[0] - blk[2];
51 let t2 = blk[1] + blk[3];
52 let t3 = blk[1] - blk[3];
53 blk[0] = t0 + t2;
54 blk[1] = t0 - t2;
55 blk[2] = t1 + t3;
56 blk[3] = t1 - t3;
57 if qp < 6 {
58 let mul = LEVEL_SCALE[0][qp as usize];
59 for el in blk.iter_mut() {
60 *el = el.wrapping_mul(mul) >> 1;
61 }
62 } else {
63 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
64 let shift = qp / 6 - 1;
65 for el in blk.iter_mut() {
66 *el = el.wrapping_mul(mul) << shift;
67 }
68 }
69}
70
71macro_rules! transform {
72 (luma_dc; $a: expr, $b: expr, $c: expr, $d: expr) => ({
73 let t0 = $a.wrapping_add($c);
74 let t1 = $a.wrapping_sub($c);
75 let t2 = $b.wrapping_add($d);
76 let t3 = $b.wrapping_sub($d);
77 $a = t0.wrapping_add(t2);
78 $b = t1.wrapping_add(t3);
79 $c = t1.wrapping_sub(t3);
80 $d = t0.wrapping_sub(t2);
81 });
82 ($a: expr, $b: expr, $c: expr, $d: expr, $shift: expr) => ({
83 let t0 = $a.wrapping_add($c);
84 let t1 = $a.wrapping_sub($c);
85 let t2 = ($b >> 1).wrapping_sub($d);
86 let t3 = $b.wrapping_add($d >> 1);
87 let bias = 1 << $shift >> 1;
88 $a = t0.wrapping_add(t3).wrapping_add(bias) >> $shift;
89 $b = t1.wrapping_add(t2).wrapping_add(bias) >> $shift;
90 $c = t1.wrapping_sub(t2).wrapping_add(bias) >> $shift;
91 $d = t0.wrapping_sub(t3).wrapping_add(bias) >> $shift;
92 });
93 ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr, $h: expr) => {
94 let e0 = $a + $e;
95 let e1 = -$d + $f - $h - ($h >> 1);
96 let e2 = $a - $e;
97 let e3 = $b + $h - $d - ($d >> 1);
98 let e4 = ($c >> 1) - $g;
99 let e5 = -$b + $h + $f + ($f >> 1);
100 let e6 = $c + ($g >> 1);
101 let e7 = $d + $f + $b + ($b >> 1);
102
103 let f0 = e0 + e6;
104 let f1 = e1 + (e7 >> 2);
105 let f2 = e2 + e4;
106 let f3 = e3 + (e5 >> 2);
107 let f4 = e2 - e4;
108 let f5 = (e3 >> 2) - e5;
109 let f6 = e0 - e6;
110 let f7 = e7 - (e1 >> 2);
111
112 $a = f0 + f7;
113 $b = f2 + f5;
114 $c = f4 + f3;
115 $d = f6 + f1;
116 $e = f6 - f1;
117 $f = f4 - f3;
118 $g = f2 - f5;
119 $h = f0 - f7;
120 };
121}
122
123pub fn idct_luma_dc(blk: &mut [i16; 16], qp: u8) {
124 if qp < 12 {
125 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
126 let shift = 2 - qp / 6;
127 let bias = 1 << shift >> 1;
128 for el in blk.iter_mut() {
129 *el = el.wrapping_mul(mul).wrapping_add(bias) >> shift;
130 }
131 } else {
132 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
133 let shift = qp / 6 - 2;
134 for el in blk.iter_mut() {
135 *el = el.wrapping_mul(mul) << shift;
136 }
137 }
138 for i in 0..4 {
139 transform!(luma_dc; blk[i], blk[i + 4], blk[i + 8], blk[i + 12]);
140 }
141 for row in blk.chunks_mut(4) {
142 transform!(luma_dc; row[0], row[1], row[2], row[3]);
143 }
144}
145
146pub fn idct(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
147 const BLK_INDEX: [usize; 16] = [
148 0, 2, 0, 2,
149 2, 1, 2, 1,
150 0, 2, 0, 2,
151 2, 1, 2, 1
152 ];
153 let qidx = (qp % 6) as usize;
154 let shift = qp / 6;
155 let start = if quant_dc { 0 } else { 1 };
156 for (el, &idx) in blk.iter_mut().zip(BLK_INDEX.iter()).skip(start) {
157 *el = (*el * LEVEL_SCALE[idx][qidx]) << shift;
158 }
159 for i in 0..4 {
160 transform!(blk[i], blk[i + 4], blk[i + 8], blk[i + 12], 0);
161 }
162 for row in blk.chunks_mut(4) {
163 transform!(row[0], row[1], row[2], row[3], 6);
164 }
165}
166
167pub fn idct_dc(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
168 let dc = if quant_dc {
169 (blk[0] * LEVEL_SCALE[0][(qp % 6) as usize]) << (qp / 6)
170 } else {
171 blk[0]
172 };
173 *blk = [(dc + 0x20) >> 6; 16];
174}
175
176const QMAT_8X8: [[u8; 16]; 6] = [
177 [
178 20, 19, 25, 24,
179 19, 18, 24, 18,
180 25, 24, 32, 24,
181 24, 18, 24, 18
182 ], [
183 22, 21, 28, 26,
184 21, 19, 26, 19,
185 28, 26, 35, 26,
186 26, 19, 26, 19
187 ], [
188 26, 24, 33, 31,
189 24, 23, 31, 23,
190 33, 31, 42, 31,
191 31, 23, 31, 23
192 ], [
193 28, 26, 35, 33,
194 26, 25, 33, 25,
195 35, 33, 45, 33,
196 33, 25, 33, 25
197 ], [
198 32, 30, 40, 38,
199 30, 28, 38, 28,
200 40, 38, 51, 38,
201 38, 28, 38, 28
202 ], [
203 36, 34, 46, 43,
204 34, 32, 43, 32,
205 46, 43, 58, 43,
206 43, 32, 43, 32
207 ]
208];
209
210pub fn dequant8x8(blk: &mut [i16; 64], slist: &[u8; 64]) {
211 for (el, &scan) in blk.iter_mut().zip(ZIGZAG8X8.iter()) {
212 if *el != 0 {
213 *el = el.wrapping_mul(i16::from(slist[scan]));
214 }
215 }
216}
217
218pub fn idct8x8(blk: &mut [i16; 64], qp: u8) {
219 let mut tmp = [0i32; 64];
220 let qmat = &QMAT_8X8[(qp % 6) as usize];
221 if qp >= 36 {
222 let shift = qp / 6 - 6;
223 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
224 let x = i & 7;
225 let y = i >> 3;
226 let idx = (x & 3) + (y & 3) * 4;
227 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])) << shift;
228 }
229 } else {
230 let shift = 6 - qp / 6;
231 let bias = (1 << shift) >> 1;
232 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
233 let x = i & 7;
234 let y = i >> 3;
235 let idx = (x & 3) + (y & 3) * 4;
236 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])).wrapping_add(bias) >> shift;
237 }
238 }
239 for row in tmp.chunks_mut(8) {
240 transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
241 }
242 for col in 0..8 {
243 transform!(tmp[col], tmp[col + 8], tmp[col + 8 * 2], tmp[col + 8 * 3],
244 tmp[col + 8 * 4], tmp[col + 8 * 5], tmp[col + 8 * 6], tmp[col + 8 * 7]);
245 }
246 for (dst, &src) in blk.iter_mut().zip(tmp.iter()) {
247 *dst = ((src + 0x20) >> 6) as i16;
248 }
249}
250
251pub fn add_coeffs(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16]) {
252 let out = &mut dst[offset..][..stride * 3 + 4];
253 for (line, src) in out.chunks_mut(stride).take(4).zip(coeffs.chunks(4)) {
254 for (dst, src) in line.iter_mut().take(4).zip(src.iter()) {
255 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
256 }
257 }
258}
259
260pub fn add_coeffs8(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16; 64]) {
261 let out = &mut dst[offset..];
262 for (line, src) in out.chunks_mut(stride).take(8).zip(coeffs.chunks(8)) {
263 for (dst, src) in line.iter_mut().take(8).zip(src.iter()) {
264 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
265 }
266 }
267}
268
269pub fn avg(dst: &mut [u8], dstride: usize,
270 src: &[u8], sstride: usize, bw: usize, bh: usize) {
271 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(bh) {
272 for (dst, src) in dline.iter_mut().zip(sline.iter()).take(bw) {
273 *dst = ((u16::from(*dst) + u16::from(*src) + 1) >> 1) as u8;
274 }
275 }
276}
277
278fn clip8(val: i16) -> u8 { val.max(0).min(255) as u8 }
279
280fn ipred_dc128(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
281 for _ in 0..bsize {
282 for x in 0..bsize { buf[idx + x] = 128; }
283 idx += stride;
284 }
285}
286fn ipred_ver(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
287 let oidx = idx - stride;
288 for _ in 0..bsize {
289 for x in 0..bsize { buf[idx + x] = buf[oidx + x]; }
290 idx += stride;
291 }
292}
293fn ipred_hor(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
294 for _ in 0..bsize {
295 for x in 0..bsize { buf[idx + x] = buf[idx - 1]; }
296 idx += stride;
297 }
298}
299fn ipred_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
300 let mut adc: u16 = 0;
301 for i in 0..bsize { adc += u16::from(buf[idx - stride + i]); }
302 for i in 0..bsize { adc += u16::from(buf[idx - 1 + i * stride]); }
303 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
304
305 for _ in 0..bsize {
306 for x in 0..bsize { buf[idx + x] = dc; }
307 idx += stride;
308 }
309}
310fn ipred_left_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
311 let mut adc: u16 = 0;
312 for i in 0..bsize { adc += u16::from(buf[idx - 1 + i * stride]); }
313 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
314
315 for _ in 0..bsize {
316 for x in 0..bsize { buf[idx + x] = dc; }
317 idx += stride;
318 }
319}
320fn ipred_top_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
321 let mut adc: u16 = 0;
322 for i in 0..bsize { adc += u16::from(buf[idx - stride + i]); }
323 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
324
325 for _ in 0..bsize {
326 for x in 0..bsize { buf[idx + x] = dc; }
327 idx += stride;
328 }
329}
330
331fn load_top(dst: &mut [u16], buf: &mut [u8], idx: usize, stride: usize, len: usize) {
332 for i in 0..len { dst[i] = u16::from(buf[idx - stride + i]); }
333}
334fn load_left(dst: &mut [u16], buf: &mut [u8], idx: usize, stride: usize, len: usize) {
335 for i in 0..len { dst[i] = u16::from(buf[idx - 1 + i * stride]); }
336}
337
338fn ipred_4x4_ver(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
339 ipred_ver(buf, idx, stride, 4);
340}
341fn ipred_4x4_hor(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
342 ipred_hor(buf, idx, stride, 4);
343}
344fn ipred_4x4_diag_down_left(buf: &mut [u8], idx: usize, stride: usize, tr: &[u8]) {
345 let mut t: [u16; 9] = [0; 9];
346 load_top(&mut t, buf, idx, stride, 4);
347 for i in 0..4 {
348 t[i + 4] = u16::from(tr[i]);
349 }
350 t[8] = t[7];
351
352 let dst = &mut buf[idx..];
353 for i in 0..4 {
354 dst[i] = ((t[i] + 2 * t[i + 1] + t[i + 2] + 2) >> 2) as u8;
355 }
356 let dst = &mut buf[idx + stride..];
357 for i in 0..4 {
358 dst[i] = ((t[i + 1] + 2 * t[i + 2] + t[i + 3] + 2) >> 2) as u8;
359 }
360 let dst = &mut buf[idx + stride * 2..];
361 for i in 0..4 {
362 dst[i] = ((t[i + 2] + 2 * t[i + 3] + t[i + 4] + 2) >> 2) as u8;
363 }
364 let dst = &mut buf[idx + stride * 3..];
365 for i in 0..4 {
366 dst[i] = ((t[i + 3] + 2 * t[i + 4] + t[i + 5] + 2) >> 2) as u8;
367 }
368}
369fn ipred_4x4_diag_down_right(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
370 let mut t: [u16; 5] = [0; 5];
371 let mut l: [u16; 5] = [0; 5];
372 load_top(&mut t, buf, idx - 1, stride, 5);
373 load_left(&mut l, buf, idx - stride, stride, 5);
374 let dst = &mut buf[idx..];
375
376 for j in 0..4 {
377 for i in 0..j {
378 dst[i + j * stride] = ((l[j - i - 1] + 2 * l[j - i] + l[j - i + 1] + 2) >> 2) as u8;
379 }
380 dst[j + j * stride] = ((l[1] + 2 * l[0] + t[1] + 2) >> 2) as u8;
381 for i in (j+1)..4 {
382 dst[i + j * stride] = ((t[i - j - 1] + 2 * t[i - j] + t[i - j + 1] + 2) >> 2) as u8;
383 }
384 }
385}
386fn ipred_4x4_ver_right(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
387 let mut t: [u16; 5] = [0; 5];
388 let mut l: [u16; 5] = [0; 5];
389 load_top(&mut t, buf, idx - 1, stride, 5);
390 load_left(&mut l, buf, idx - stride, stride, 5);
391 let dst = &mut buf[idx..];
392
393 for j in 0..4 {
394 for i in 0..4 {
395 let zvr = ((2 * i) as i8) - (j as i8);
396 let pix;
397 if zvr >= 0 {
398 if (zvr & 1) == 0 {
399 pix = (t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 1) >> 1;
400 } else {
401 pix = (t[i - (j >> 1) - 1] + 2 * t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 2) >> 2;
402 }
403 } else {
404 if zvr == -1 {
405 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
406 } else {
407 pix = (l[j] + 2 * l[j - 1] + l[j - 2] + 2) >> 2;
408 }
409 }
410 dst[i + j * stride] = pix as u8;
411 }
412 }
413}
414fn ipred_4x4_ver_left(buf: &mut [u8], idx: usize, stride: usize, tr: &[u8]) {
415 let mut t: [u16; 8] = [0; 8];
416 load_top(&mut t, buf, idx, stride, 4);
417 for i in 0..4 { t[i + 4] = u16::from(tr[i]); }
418 let dst = &mut buf[idx..];
419
420 dst[0 + 0 * stride] = ((t[0] + t[1] + 1) >> 1) as u8;
421 let pix = ((t[1] + t[2] + 1) >> 1) as u8;
422 dst[1 + 0 * stride] = pix;
423 dst[0 + 2 * stride] = pix;
424 let pix = ((t[2] + t[3] + 1) >> 1) as u8;
425 dst[2 + 0 * stride] = pix;
426 dst[1 + 2 * stride] = pix;
427 let pix = ((t[3] + t[4] + 1) >> 1) as u8;
428 dst[3 + 0 * stride] = pix;
429 dst[2 + 2 * stride] = pix;
430 dst[3 + 2 * stride] = ((t[4] + t[5] + 1) >> 1) as u8;
431 dst[0 + 1 * stride] = ((t[0] + 2*t[1] + t[2] + 2) >> 2) as u8;
432 let pix = ((t[1] + 2*t[2] + t[3] + 2) >> 2) as u8;
433 dst[1 + 1 * stride] = pix;
434 dst[0 + 3 * stride] = pix;
435 let pix = ((t[2] + 2*t[3] + t[4] + 2) >> 2) as u8;
436 dst[2 + 1 * stride] = pix;
437 dst[1 + 3 * stride] = pix;
438 let pix = ((t[3] + 2*t[4] + t[5] + 2) >> 2) as u8;
439 dst[3 + 1 * stride] = pix;
440 dst[2 + 3 * stride] = pix;
441 dst[3 + 3 * stride] = ((t[4] + 2*t[5] + t[6] + 2) >> 2) as u8;
442}
443fn ipred_4x4_hor_down(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
444 let mut t: [u16; 5] = [0; 5];
445 let mut l: [u16; 5] = [0; 5];
446 load_top(&mut t, buf, idx - 1, stride, 5);
447 load_left(&mut l, buf, idx - stride, stride, 5);
448 let dst = &mut buf[idx..];
449
450 for j in 0..4 {
451 for i in 0..4 {
452 let zhd = ((2 * j) as i8) - (i as i8);
453 let pix;
454 if zhd >= 0 {
455 if (zhd & 1) == 0 {
456 pix = (l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 1) >> 1;
457 } else {
458 pix = (l[j - (i >> 1) - 1] + 2 * l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 2) >> 2;
459 }
460 } else {
461 if zhd == -1 {
462 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
463 } else {
464 pix = (t[i - 2] + 2 * t[i - 1] + t[i] + 2) >> 2;
465 }
466 }
467 dst[i + j * stride] = pix as u8;
468 }
469 }
470}
471fn ipred_4x4_hor_up(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
472 let mut l: [u16; 8] = [0; 8];
473 load_left(&mut l, buf, idx, stride, 8);
474 let dst = &mut buf[idx..];
475
476 dst[0 + 0 * stride] = ((l[0] + l[1] + 1) >> 1) as u8;
477 dst[1 + 0 * stride] = ((l[0] + 2*l[1] + l[2] + 2) >> 2) as u8;
478 let pix = ((l[1] + l[2] + 1) >> 1) as u8;
479 dst[2 + 0 * stride] = pix;
480 dst[0 + 1 * stride] = pix;
481 let pix = ((l[1] + 2*l[2] + l[3] + 2) >> 2) as u8;
482 dst[3 + 0 * stride] = pix;
483 dst[1 + 1 * stride] = pix;
484 let pix = ((l[2] + l[3] + 1) >> 1) as u8;
485 dst[2 + 1 * stride] = pix;
486 dst[0 + 2 * stride] = pix;
487 let pix = ((l[2] + 3*l[3] + 2) >> 2) as u8;
488 dst[3 + 1 * stride] = pix;
489 dst[1 + 2 * stride] = pix;
490 dst[3 + 2 * stride] = l[3] as u8;
491 dst[1 + 3 * stride] = l[3] as u8;
492 dst[0 + 3 * stride] = l[3] as u8;
493 dst[2 + 2 * stride] = l[3] as u8;
494 dst[2 + 3 * stride] = l[3] as u8;
495 dst[3 + 3 * stride] = l[3] as u8;
496}
497fn ipred_4x4_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
498 ipred_dc(buf, idx, stride, 4, 3);
499}
500fn ipred_4x4_left_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
501 ipred_left_dc(buf, idx, stride, 4, 2);
502}
503fn ipred_4x4_top_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
504 ipred_top_dc(buf, idx, stride, 4, 2);
505}
506fn ipred_4x4_dc128(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
507 ipred_dc128(buf, idx, stride, 4);
508}
509
510pub struct IPred8Context {
511 pub t: [u8; 16],
512 pub l: [u8; 8],
513 pub tl: u8,
514}
515
516impl IPred8Context {
517 pub fn new() -> Self {
518 Self {
519 t: [128; 16],
520 l: [128; 8],
521 tl: 128,
522 }
523 }
524 pub fn fill(&mut self, buf: &mut [u8], idx: usize, stride: usize, has_t: bool, has_tr: bool, has_l: bool, has_tl: bool) {
525 let mut t = [0x80u8; 19];
526 let mut l = [0x80u8; 11];
527 if has_t {
528 t[1..8 + 1].copy_from_slice(&buf[idx - stride..][..8]);
529 }
530 if has_tr {
531 t[8 + 1..16 + 1].copy_from_slice(&buf[idx - stride + 8..][..8]);
532 t[16 + 1] = t[15 + 1];
533 t[17 + 1] = t[15 + 1];
534 } else {
535 let (t0, t1) = t.split_at_mut(8 + 1);
536 for el in t1.iter_mut() {
537 *el = t0[7 + 1];
538 }
539 }
540 if has_l {
541 for i in 0..8 {
542 l[i + 1] = buf[idx - 1 + stride * i];
543 }
544 l[8 + 1] = l[7 + 1];
545 l[9 + 1] = l[7 + 1];
546 }
547 if has_tl {
548 t[0] = buf[idx - 1 - stride];
549 l[0] = buf[idx - 1 - stride];
550 } else {
551 t[0] = t[1];
552 l[0] = l[1];
553 }
554
555 for i in 0..16 {
556 self.t[i] = ((u16::from(t[i]) + 2 * u16::from(t[i + 1]) + u16::from(t[i + 2]) + 2) >> 2) as u8;
557 }
558 for i in 0..8 {
559 self.l[i] = ((u16::from(l[i]) + 2 * u16::from(l[i + 1]) + u16::from(l[i + 2]) + 2) >> 2) as u8;
560 }
561 self.tl = if has_t && has_l {
562 ((u16::from(t[1]) + 2 * u16::from(t[0]) + u16::from(l[1]) + 2) >> 2) as u8
563 } else if has_t {
564 ((3 * u16::from(t[0]) + u16::from(t[1]) + 2) >> 2) as u8
565 } else if has_l {
566 ((3 * u16::from(l[0]) + u16::from(l[1]) + 2) >> 2) as u8
567 } else {
568 t[0]
569 };
570 }
571}
572
573fn ipred_y_8x8_ver(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
574 for row in buf.chunks_mut(stride).take(8) {
575 row[..8].copy_from_slice(&ctx.t[..8]);
576 }
577}
578fn ipred_y_8x8_hor(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
579 for (row, &l) in buf.chunks_mut(stride).zip(ctx.l.iter()).take(8) {
580 row[..8].copy_from_slice(&[l; 8]);
581 }
582}
583fn ipred_y_8x8_diag_down_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
584 let mut t = [0u16; 16];
585 for (dt, &st) in t.iter_mut().zip(ctx.t.iter()) {
586 *dt = u16::from(st);
587 }
588
589 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
590 for (x, pix) in row.iter_mut().take(8).enumerate() {
591 *pix = ((if (x != 7) || (y != 7) {
592 t[x + y] + 2 * t[x + y + 1] + t[x + y + 2]
593 } else {
594 t[14] + 3 * t[15]
595 } + 2) >> 2) as u8;
596 }
597 }
598}
599fn ipred_y_8x8_diag_down_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
600 let mut t = [0u16; 9];
601 t[0] = u16::from(ctx.tl);
602 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
603 *dt = u16::from(st);
604 }
605 let mut l = [0u16; 9];
606 l[0] = u16::from(ctx.tl);
607 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
608 *dl = u16::from(sl);
609 }
610 let diag = t[1] + 2 * t[0] + l[1];
611
612 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
613 for (x, pix) in row.iter_mut().take(8).enumerate() {
614 *pix = ((if x > y {
615 t[x - y - 1] + 2 * t[x - y] + t[x - y + 1]
616 } else if x < y {
617 l[y - x - 1] + 2 * l[y - x] + l[y - x + 1]
618 } else {
619 diag
620 } + 2) >> 2) as u8;
621 }
622 }
623}
624fn ipred_y_8x8_ver_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
625 let mut t = [0u16; 9];
626 t[0] = u16::from(ctx.tl);
627 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
628 *dt = u16::from(st);
629 }
630 let mut l = [0u16; 9];
631 l[0] = u16::from(ctx.tl);
632 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
633 *dl = u16::from(sl);
634 }
635
636 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
637 for (x, pix) in row.iter_mut().take(8).enumerate() {
638 let zvr = 2 * (x as i8) - (y as i8);
639 *pix = if zvr >= 0 {
640 let ix = x - (y >> 1);
641 if (zvr & 1) == 0 {
642 (t[ix] + t[ix + 1] + 1) >> 1
643 } else {
644 (t[ix - 1] + 2 * t[ix] + t[ix + 1] + 2) >> 2
645 }
646 } else if zvr == -1 {
647 (l[1] + 2 * l[0] + t[0] + 2) >> 2
648 } else {
649 let ix = y - 2 * x;
650 (l[ix] + 2 * l[ix - 1] + l[ix - 2] + 2) >> 2
651 } as u8;
652 }
653 }
654}
655fn ipred_y_8x8_ver_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
656 let mut t = [0u16; 16];
657 for (dt, &st) in t.iter_mut().zip(ctx.t.iter()) {
658 *dt = u16::from(st);
659 }
660
661 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
662 for (x, pix) in row.iter_mut().take(8).enumerate() {
663 let ix = x + (y >> 1);
664 *pix = if (y & 1) == 0 {
665 (t[ix] + t[ix + 1] + 1) >> 1
666 } else {
667 (t[ix] + 2 * t[ix + 1] + t[ix + 2] + 2) >> 2
668 } as u8;
669 }
670 }
671
672}
673fn ipred_y_8x8_hor_down(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
674 let mut t = [0u16; 9];
675 t[0] = u16::from(ctx.tl);
676 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
677 *dt = u16::from(st);
678 }
679 let mut l = [0u16; 9];
680 l[0] = u16::from(ctx.tl);
681 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
682 *dl = u16::from(sl);
683 }
684
685 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
686 for (x, pix) in row.iter_mut().take(8).enumerate() {
687 let zhd = 2 * (y as i8) - (x as i8);
688 *pix = if zhd >= 0 {
689 let ix = y - (x >> 1);
690 if (zhd & 1) == 0 {
691 (l[ix] + l[ix + 1] + 1) >> 1
692 } else {
693 (l[ix - 1] + 2 * l[ix] + l[ix + 1] + 2) >> 2
694 }
695 } else if zhd == -1 {
696 (l[1] + 2 * l[0] + t[0] + 2) >> 2
697 } else {
698 let ix = x - 2 * y;
699 (t[ix] + 2 * t[ix - 1] + t[ix - 2] + 2) >> 2
700 } as u8;
701 }
702 }
703}
704fn ipred_y_8x8_hor_up(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
705 let mut l = [0u16; 8];
706 for (dl, &sl) in l.iter_mut().zip(ctx.l.iter()) {
707 *dl = u16::from(sl);
708 }
709
710 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
711 for (x, pix) in row.iter_mut().take(8).enumerate() {
712 let zhu = x + 2 * y;
713 let ix = y + (x >> 1);
714 *pix = if zhu > 13 {
715 l[7]
716 } else if zhu == 13 {
717 (l[6] + 3 * l[7] + 2) >> 2
718 } else if (zhu & 1) != 0 {
719 (l[ix] + 2 * l[ix + 1] + l[ix + 2] + 2) >> 2
720 } else {
721 (l[ix] + l[ix + 1] + 1) >> 1
722 } as u8;
723 }
724 }
725}
726fn ipred_y_8x8_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
727 let mut sum = 0u16;
728 for &t in ctx.t[..8].iter() {
729 sum += u16::from(t);
730 }
731 for &l in ctx.l[..8].iter() {
732 sum += u16::from(l);
733 }
734 let dc = ((sum + 8) >> 4) as u8;
735 for row in buf.chunks_mut(stride).take(8) {
736 for pix in row.iter_mut().take(8) {
737 *pix = dc;
738 }
739 }
740}
741fn ipred_y_8x8_left_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
742 let mut sum = 0u16;
743 for &l in ctx.l[..8].iter() {
744 sum += u16::from(l);
745 }
746 let dc = ((sum + 4) >> 3) as u8;
747 for row in buf.chunks_mut(stride).take(8) {
748 for pix in row.iter_mut().take(8) {
749 *pix = dc;
750 }
751 }
752}
753fn ipred_y_8x8_top_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
754 let mut sum = 0u16;
755 for &t in ctx.t[..8].iter() {
756 sum += u16::from(t);
757 }
758 let dc = ((sum + 4) >> 3) as u8;
759 for row in buf.chunks_mut(stride).take(8) {
760 for pix in row.iter_mut().take(8) {
761 *pix = dc;
762 }
763 }
764}
765fn ipred_y_8x8_dc128(buf: &mut [u8], stride: usize, _ctx: &IPred8Context) {
766 ipred_dc128(buf, 0, stride, 8);
767}
768
769fn ipred_8x8_ver(buf: &mut [u8], idx: usize, stride: usize) {
770 ipred_ver(buf, idx, stride, 8);
771}
772fn ipred_8x8_hor(buf: &mut [u8], idx: usize, stride: usize) {
773 ipred_hor(buf, idx, stride, 8);
774}
775fn ipred_8x8_dc(buf: &mut [u8], idx: usize, stride: usize) {
776 let mut t: [u16; 8] = [0; 8];
777 load_top(&mut t, buf, idx, stride, 8);
778 let mut l: [u16; 8] = [0; 8];
779 load_left(&mut l, buf, idx, stride, 8);
780
781 let dc0 = ((t[0] + t[1] + t[2] + t[3] + l[0] + l[1] + l[2] + l[3] + 4) >> 3) as u8;
782 let sum1 = t[4] + t[5] + t[6] + t[7];
783 let dc1 = ((sum1 + 2) >> 2) as u8;
784 let sum2 = l[4] + l[5] + l[6] + l[7];
785 let dc2 = ((sum2 + 2) >> 2) as u8;
786 let dc3 = ((sum1 + sum2 + 4) >> 3) as u8;
787
788 let dst = &mut buf[idx..];
789 for row in dst.chunks_mut(stride).take(4) {
790 row[..4].copy_from_slice(&[dc0; 4]);
791 row[4..8].copy_from_slice(&[dc1; 4]);
792 }
793 for row in dst.chunks_mut(stride).skip(4).take(4) {
794 row[..4].copy_from_slice(&[dc2; 4]);
795 row[4..8].copy_from_slice(&[dc3; 4]);
796 }
797}
798fn ipred_8x8_left_dc(buf: &mut [u8], idx: usize, stride: usize) {
799 let mut left_dc0 = 0;
800 let mut left_dc1 = 0;
801 for row in buf[idx - 1..].chunks(stride).take(4) {
802 left_dc0 += u16::from(row[0]);
803 }
804 for row in buf[idx - 1..].chunks(stride).skip(4).take(4) {
805 left_dc1 += u16::from(row[0]);
806 }
807 let dc0 = ((left_dc0 + 2) >> 2) as u8;
808 let dc2 = ((left_dc1 + 2) >> 2) as u8;
809 for row in buf[idx..].chunks_mut(stride).take(4) {
810 row[..8].copy_from_slice(&[dc0; 8]);
811 }
812 for row in buf[idx..].chunks_mut(stride).skip(4).take(4) {
813 row[..8].copy_from_slice(&[dc2; 8]);
814 }
815}
816fn ipred_8x8_top_dc(buf: &mut [u8], idx: usize, stride: usize) {
817 ipred_top_dc(buf, idx, stride, 4, 2);
818 ipred_top_dc(buf, idx + 4, stride, 4, 2);
819 ipred_top_dc(buf, idx + 4 * stride, stride, 4, 2);
820 ipred_top_dc(buf, idx + 4 + 4 * stride, stride, 4, 2);
821}
822fn ipred_8x8_dc128(buf: &mut [u8], idx: usize, stride: usize) {
823 ipred_dc128(buf, idx, stride, 8);
824}
825fn ipred_8x8_plane(buf: &mut [u8], idx: usize, stride: usize) {
826 let mut h: i32 = 0;
827 let mut v: i32 = 0;
828 let idx0 = idx + 3 - stride;
829 let mut idx1 = idx + 4 * stride - 1;
830 let mut idx2 = idx + 2 * stride - 1;
831 for i in 0..4 {
832 let i1 = (i + 1) as i32;
833 h += i1 * (i32::from(buf[idx0 + i + 1]) - i32::from(buf[idx0 - i - 1]));
834 v += i1 * (i32::from(buf[idx1]) - i32::from(buf[idx2]));
835 idx1 += stride;
836 idx2 -= stride;
837 }
838 let b = (17 * h + 16) >> 5;
839 let c = (17 * v + 16) >> 5;
840 let mut a = 16 * (i32::from(buf[idx - 1 + 7 * stride]) + i32::from(buf[idx + 7 - stride])) - 3 * (b + c) + 16;
841 for line in buf[idx..].chunks_mut(stride).take(8) {
842 let mut acc = a;
843 for el in line.iter_mut().take(8) {
844 *el = clip8((acc >> 5) as i16);
845 acc += b;
846 }
847 a += c;
848 }
849}
850
851fn ipred_16x16_ver(buf: &mut [u8], idx: usize, stride: usize) {
852 ipred_ver(buf, idx, stride, 16);
853}
854fn ipred_16x16_hor(buf: &mut [u8], idx: usize, stride: usize) {
855 ipred_hor(buf, idx, stride, 16);
856}
857fn ipred_16x16_dc(buf: &mut [u8], idx: usize, stride: usize) {
858 ipred_dc(buf, idx, stride, 16, 5);
859}
860fn ipred_16x16_left_dc(buf: &mut [u8], idx: usize, stride: usize) {
861 ipred_left_dc(buf, idx, stride, 16, 4);
862}
863fn ipred_16x16_top_dc(buf: &mut [u8], idx: usize, stride: usize) {
864 ipred_top_dc(buf, idx, stride, 16, 4);
865}
866fn ipred_16x16_dc128(buf: &mut [u8], idx: usize, stride: usize) {
867 ipred_dc128(buf, idx, stride, 16);
868}
869fn ipred_16x16_plane(buf: &mut [u8], idx: usize, stride: usize) {
870 let idx0 = idx + 7 - stride;
871 let mut idx1 = idx + 8 * stride - 1;
872 let mut idx2 = idx1 - 2 * stride;
873
874 let mut h = i32::from(buf[idx0 + 1]) - i32::from(buf[idx0 - 1]);
875 let mut v = i32::from(buf[idx1]) - i32::from(buf[idx2]);
876
877 for k in 2..9 {
878 idx1 += stride;
879 idx2 -= stride;
880 h += (k as i32) * (i32::from(buf[idx0 + k]) - i32::from(buf[idx0 - k]));
881 v += (k as i32) * (i32::from(buf[idx1]) - i32::from(buf[idx2]));
882 }
883 h = (5 * h + 32) >> 6;
884 v = (5 * v + 32) >> 6;
885
886 let mut a = 16 * (i32::from(buf[idx - 1 + 15 * stride]) + i32::from(buf[idx + 15 - stride]) + 1) - 7 * (v + h);
887
888 for row in buf[idx..].chunks_mut(stride).take(16) {
889 let mut b = a;
890 a += v;
891
892 for dst in row.chunks_exact_mut(4).take(4) {
893 dst[0] = clip8(((b ) >> 5) as i16);
894 dst[1] = clip8(((b + h) >> 5) as i16);
895 dst[2] = clip8(((b + 2*h) >> 5) as i16);
896 dst[3] = clip8(((b + 3*h) >> 5) as i16);
897 b += h * 4;
898 }
899 }
900}
901
902pub type IPred4x4Func = fn(buf: &mut [u8], off: usize, stride: usize, tr: &[u8]);
903pub type IPred8x8Func = fn(buf: &mut [u8], off: usize, stride: usize);
904pub type IPred8x8LumaFunc = fn(buf: &mut [u8], stride: usize, ctx: &IPred8Context);
905
906pub const IPRED4_DC128: usize = 11;
907pub const IPRED4_DC_TOP: usize = 10;
908pub const IPRED4_DC_LEFT: usize = 9;
909pub const IPRED8_DC128: usize = 6;
910pub const IPRED8_DC_TOP: usize = 5;
911pub const IPRED8_DC_LEFT: usize = 4;
912
913pub const IPRED_FUNCS4X4: [IPred4x4Func; 12] = [
914 ipred_4x4_ver, ipred_4x4_hor, ipred_4x4_dc,
915 ipred_4x4_diag_down_left, ipred_4x4_diag_down_right,
916 ipred_4x4_ver_right, ipred_4x4_hor_down, ipred_4x4_ver_left, ipred_4x4_hor_up,
917 ipred_4x4_left_dc, ipred_4x4_top_dc, ipred_4x4_dc128
918];
919
920pub const IPRED_FUNCS8X8_LUMA: [IPred8x8LumaFunc; 12] = [
921 ipred_y_8x8_ver, ipred_y_8x8_hor, ipred_y_8x8_dc,
922 ipred_y_8x8_diag_down_left, ipred_y_8x8_diag_down_right,
923 ipred_y_8x8_ver_right, ipred_y_8x8_hor_down,
924 ipred_y_8x8_ver_left, ipred_y_8x8_hor_up,
925 ipred_y_8x8_left_dc, ipred_y_8x8_top_dc, ipred_y_8x8_dc128
926];
927
928pub const IPRED_FUNCS8X8_CHROMA: [IPred8x8Func; 7] = [
929 ipred_8x8_dc, ipred_8x8_hor, ipred_8x8_ver, ipred_8x8_plane,
930 ipred_8x8_left_dc, ipred_8x8_top_dc, ipred_8x8_dc128
931];
932
933pub const IPRED_FUNCS16X16: [IPred8x8Func; 7] = [
934 ipred_16x16_ver, ipred_16x16_hor, ipred_16x16_dc, ipred_16x16_plane,
935 ipred_16x16_left_dc, ipred_16x16_top_dc, ipred_16x16_dc128
936];
937
938fn clip_u8(val: i16) -> u8 { val.max(0).min(255) as u8 }
939
940pub fn do_mc(frm: &mut NASimpleVideoFrame<u8>, refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
941 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
942 copy_block(frm, refpic.clone(), 0, xpos, ypos, mv.x >> 2, mv.y >> 2, w, h, 2, 3, mode, H264_LUMA_INTERP);
943
944 let (cw, ch) = refpic.get_dimensions(1);
945 let mvx = mv.x >> 3;
946 let mvy = mv.y >> 3;
947 let dx = (mv.x & 7) as u16;
948 let dy = (mv.y & 7) as u16;
949 let mut ebuf = [0u8; 18 * 9];
950 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
951 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
952 let suoff = refpic.get_offset(1);
953 let svoff = refpic.get_offset(2);
954 let sustride = refpic.get_stride(1);
955 let svstride = refpic.get_stride(2);
956 let src = refpic.get_data();
957 let cbw = w / 2;
958 let cbh = h / 2;
959 let (csrc, cstride) = if (src_x < 0) || (src_x + (cbw as isize) + 1 > (cw as isize)) || (src_y < 0) || (src_y + (cbh as isize) + 1 > (ch as isize)) {
960 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf, 18, 1, 4);
961 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf[9..], 18, 2, 4);
962 ([&ebuf, &ebuf[9..]], [18, 18])
963 } else {
964 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
965 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
966 [sustride, svstride])
967 };
968 for chroma in 1..3 {
969 let off = frm.offset[chroma] + xpos / 2 + (ypos / 2) * frm.stride[chroma];
970 chroma_interp(&mut frm.data[off..], frm.stride[chroma], csrc[chroma - 1], cstride[chroma - 1], dx, dy, cbw, cbh);
971 }
972}
973
974pub fn gray_block(frm: &mut NASimpleVideoFrame<u8>, x: usize, y: usize, w: usize, h: usize) {
975 let yoff = frm.offset[0] + x + y * frm.stride[0];
976 let coff = [frm.offset[1] + x / 2 + y / 2 * frm.stride[1],
977 frm.offset[2] + x / 2 + y / 2 * frm.stride[2]];
978 if w == 16 && h == 16 {
979 IPRED_FUNCS16X16[IPRED8_DC128](frm.data, yoff, frm.stride[0]);
980 for chroma in 1..2 {
981 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, coff[chroma - 1], frm.stride[chroma]);
982 }
983 } else if w == 8 && h == 8 {
984 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, yoff, frm.stride[0]);
985 for chroma in 1..2 {
986 IPRED_FUNCS4X4[IPRED4_DC128](frm.data, coff[chroma - 1], frm.stride[chroma], &[128; 4]);
987 }
988 } else {
989 for row in frm.data[yoff..].chunks_mut(frm.stride[0]).take(h) {
990 for el in row[..w].iter_mut() {
991 *el = 128;
992 }
993 }
994 for chroma in 0..2 {
995 for row in frm.data[coff[chroma]..].chunks_mut(frm.stride[chroma + 1]).take(h / 2) {
996 for el in row[..w / 2].iter_mut() {
997 *el = 128;
998 }
999 }
1000 }
1001 }
1002}
1003
1004pub fn do_mc_avg(frm: &mut NASimpleVideoFrame<u8>, refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV, avg_buf: &mut NAVideoBufferRef<u8>) {
1005 let mut afrm = NASimpleVideoFrame::from_video_buf(avg_buf).unwrap();
1006 let amv = MV { x: mv.x + (xpos as i16) * 4, y: mv.y + (ypos as i16) * 4 };
1007 do_mc(&mut afrm, refpic, 0, 0, w, h, amv);
1008 for comp in 0..3 {
1009 let shift = if comp == 0 { 0 } else { 1 };
1010 avg(&mut frm.data[frm.offset[comp] + (xpos >> shift) + (ypos >> shift) * frm.stride[comp]..], frm.stride[comp], &afrm.data[afrm.offset[comp]..], afrm.stride[comp], w >> shift, h >> shift);
1011 }
1012}
1013
1014macro_rules! loop_filter {
1015 (lumaedge; $buf: expr, $off: expr, $step: expr, $alpha: expr, $beta: expr) => {
1016 let p2 = i16::from($buf[$off - $step * 3]);
1017 let p1 = i16::from($buf[$off - $step * 2]);
1018 let p0 = i16::from($buf[$off - $step]);
1019 let q0 = i16::from($buf[$off]);
1020 let q1 = i16::from($buf[$off + $step]);
1021 let q2 = i16::from($buf[$off + $step * 2]);
1022 let a_p = (p2 - p0).abs() < $beta;
1023 let a_q = (q2 - q0).abs() < $beta;
1024 if a_p && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1025 let p3 = i16::from($buf[$off - $step * 4]);
1026 $buf[$off - $step * 3] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
1027 $buf[$off - $step * 2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
1028 $buf[$off - $step] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
1029 } else {
1030 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1031 }
1032 if a_q && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1033 let q3 = i16::from($buf[$off + $step * 3]);
1034 $buf[$off] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
1035 $buf[$off + $step] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
1036 $buf[$off + $step * 2] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
1037 } else {
1038 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1039 }
1040 };
1041 (chromaedge; $buf: expr, $off: expr, $step: expr) => {
1042 let p1 = i16::from($buf[$off - $step * 2]);
1043 let p0 = i16::from($buf[$off - $step]);
1044 let q0 = i16::from($buf[$off]);
1045 let q1 = i16::from($buf[$off + $step]);
1046 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1047 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1048 };
1049 (lumanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr, $beta: expr) => {
1050 let p2 = i16::from($buf[$off - $step * 3]);
1051 let p1 = i16::from($buf[$off - $step * 2]);
1052 let p0 = i16::from($buf[$off - $step]);
1053 let q0 = i16::from($buf[$off]);
1054 let q1 = i16::from($buf[$off + $step]);
1055 let q2 = i16::from($buf[$off + $step * 2]);
1056 let a_p = (p2 - p0).abs() < $beta;
1057 let a_q = (q2 - q0).abs() < $beta;
1058 let tc = $tc0 + (a_p as i16) + (a_q as i16);
1059 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1060 if a_p && ($tc0 > 0) {
1061 $buf[$off - $step * 2] = clip8(p1 + ((p2 + ((p0 + q0 + 1) >> 1) - p1 * 2) >> 1).max(-$tc0).min($tc0));
1062 }
1063 $buf[$off - $step] = clip8(p0 + delta);
1064 $buf[$off] = clip8(q0 - delta);
1065 if a_q && ($tc0 > 0) {
1066 $buf[$off + $step] = clip8(q1 + ((q2 + ((p0 + q0 + 1) >> 1) - q1 * 2) >> 1).max(-$tc0).min($tc0));
1067 }
1068 };
1069 (chromanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr) => {
1070 let p1 = i16::from($buf[$off - $step * 2]);
1071 let p0 = i16::from($buf[$off - $step]);
1072 let q0 = i16::from($buf[$off]);
1073 let q1 = i16::from($buf[$off + $step]);
1074 let tc = $tc0 + 1;
1075 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1076 $buf[$off - $step] = clip8(p0 + delta);
1077 $buf[$off] = clip8(q0 - delta);
1078 }
1079}
1080
1081fn check_filter(buf: &[u8], off: usize, step: usize, alpha: i16, beta: i16) -> bool {
1082 let p1 = i16::from(buf[off - step * 2]);
1083 let p0 = i16::from(buf[off - step]);
1084 let q0 = i16::from(buf[off]);
1085 let q1 = i16::from(buf[off + step]);
1086 (p0 - q0).abs() < alpha && (p1 - p0).abs() < beta && (q1 - q0).abs() < beta
1087}
1088
1089pub fn loop_filter_lumaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1090 for _ in 0..4 {
1091 if check_filter(dst, off, 1, alpha, beta) {
1092 loop_filter!(lumaedge; dst, off, 1, alpha, beta);
1093 }
1094 off += stride;
1095 }
1096}
1097pub fn loop_filter_lumaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1098 for x in 0..4 {
1099 if check_filter(dst, off + x, stride, alpha, beta) {
1100 loop_filter!(lumaedge; dst, off + x, stride, alpha, beta);
1101 }
1102 }
1103}
1104pub fn loop_filter_lumanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1105 for _ in 0..4 {
1106 if check_filter(dst, off, 1, alpha, beta) {
1107 loop_filter!(lumanormal; dst, off, 1, tc0, beta);
1108 }
1109 off += stride;
1110 }
1111}
1112pub fn loop_filter_lumanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1113 for x in 0..4 {
1114 if check_filter(dst, off + x, stride, alpha, beta) {
1115 loop_filter!(lumanormal; dst, off + x, stride, tc0, beta);
1116 }
1117 }
1118}
1119pub fn loop_filter_chromaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1120 for _ in 0..4 {
1121 if check_filter(dst, off, 1, alpha, beta) {
1122 loop_filter!(chromaedge; dst, off, 1);
1123 }
1124 off += stride;
1125 }
1126}
1127pub fn loop_filter_chromaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1128 for x in 0..4 {
1129 if check_filter(dst, off + x, stride, alpha, beta) {
1130 loop_filter!(chromaedge; dst, off + x, stride);
1131 }
1132 }
1133}
1134pub fn loop_filter_chromanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1135 for _ in 0..4 {
1136 if check_filter(dst, off, 1, alpha, beta) {
1137 loop_filter!(chromanormal; dst, off, 1, tc0);
1138 }
1139 off += stride;
1140 }
1141}
1142pub fn loop_filter_chromanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1143 for x in 0..4 {
1144 if check_filter(dst, off + x, stride, alpha, beta) {
1145 loop_filter!(chromanormal; dst, off + x, stride, tc0);
1146 }
1147 }
1148}