1 use nihav_core::frame::*;
2 use nihav_codec_support::codecs::blockdsp::*;
3 use nihav_codec_support::codecs::MV;
5 pub const CHROMA_QUANTS: [u8; 52] = [
6 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30,
8 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38,
12 pub const CHROMA_DC_SCAN: [usize; 4] = [ 0, 1, 2, 3];
13 pub const ZIGZAG: [usize; 16] = [
14 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
16 pub const ZIGZAG1: [usize; 15] = [
17 0, 3, 7, 4, 1, 2, 5, 8, 11, 12, 9, 6, 10, 13, 14
19 /*pub const IL_SCAN: [usize; 16] = [
20 0, 4, 1, 8, 12, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15
22 pub const ZIGZAG8X8: [usize; 64] = [
23 0, 1, 8, 16, 9, 2, 3, 10,
24 17, 24, 32, 25, 18, 11, 4, 5,
25 12, 19, 26, 33, 40, 48, 41, 34,
26 27, 20, 13, 6, 7, 14, 21, 28,
27 35, 42, 49, 56, 57, 50, 43, 36,
28 29, 22, 15, 23, 30, 37, 44, 51,
29 58, 59, 52, 45, 38, 31, 39, 46,
30 53, 60, 61, 54, 47, 55, 62, 63
33 const LEVEL_SCALE: [[i16; 6]; 3] = [
34 [ 10, 11, 13, 14, 16, 18 ],
35 [ 16, 18, 20, 23, 25, 29 ],
36 [ 13, 14, 16, 18, 20, 23 ]
39 pub fn chroma_dc_transform(blk: &mut [i16; 4], qp: u8) {
40 let t0 = blk[0] + blk[2];
41 let t1 = blk[0] - blk[2];
42 let t2 = blk[1] + blk[3];
43 let t3 = blk[1] - blk[3];
49 let mul = i16::from(LEVEL_SCALE[0][qp as usize]);
50 for el in blk.iter_mut() {
51 *el = el.wrapping_mul(mul) >> 1;
54 let mul = i16::from(LEVEL_SCALE[0][(qp % 6) as usize]);
55 let shift = qp / 6 - 1;
56 for el in blk.iter_mut() {
57 *el = el.wrapping_mul(mul) << shift;
62 macro_rules! transform {
63 (luma_dc; $a: expr, $b: expr, $c: expr, $d: expr) => ({
64 let t0 = $a.wrapping_add($c);
65 let t1 = $a.wrapping_sub($c);
66 let t2 = $b.wrapping_add($d);
67 let t3 = $b.wrapping_sub($d);
68 $a = t0.wrapping_add(t2);
69 $b = t1.wrapping_add(t3);
70 $c = t1.wrapping_sub(t3);
71 $d = t0.wrapping_sub(t2);
73 ($a: expr, $b: expr, $c: expr, $d: expr, $shift: expr) => ({
74 let t0 = $a.wrapping_add($c);
75 let t1 = $a.wrapping_sub($c);
76 let t2 = ($b >> 1).wrapping_sub($d);
77 let t3 = $b.wrapping_add($d >> 1);
78 let bias = 1 << $shift >> 1;
79 $a = t0.wrapping_add(t3).wrapping_add(bias) >> $shift;
80 $b = t1.wrapping_add(t2).wrapping_add(bias) >> $shift;
81 $c = t1.wrapping_sub(t2).wrapping_add(bias) >> $shift;
82 $d = t0.wrapping_sub(t3).wrapping_add(bias) >> $shift;
84 ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr, $h: expr) => {
86 let e1 = -$d + $f - $h - ($h >> 1);
88 let e3 = $b + $h - $d - ($d >> 1);
89 let e4 = ($c >> 1) - $g;
90 let e5 = -$b + $h + $f + ($f >> 1);
91 let e6 = $c + ($g >> 1);
92 let e7 = $d + $f + $b + ($b >> 1);
95 let f1 = e1 + (e7 >> 2);
97 let f3 = e3 + (e5 >> 2);
99 let f5 = (e3 >> 2) - e5;
101 let f7 = e7 - (e1 >> 2);
114 pub fn idct_luma_dc(blk: &mut [i16; 16], qp: u8) {
116 let mul = i16::from(LEVEL_SCALE[0][(qp % 6) as usize]);
117 let shift = 2 - qp / 6;
118 let bias = 1 << shift >> 1;
119 for el in blk.iter_mut() {
120 *el = el.wrapping_mul(mul).wrapping_add(bias) >> shift;
123 let mul = i16::from(LEVEL_SCALE[0][(qp % 6) as usize]);
124 let shift = qp / 6 - 2;
125 for el in blk.iter_mut() {
126 *el = el.wrapping_mul(mul) << shift;
130 transform!(luma_dc; blk[i], blk[i + 4], blk[i + 8], blk[i + 12]);
132 for row in blk.chunks_mut(4) {
133 transform!(luma_dc; row[0], row[1], row[2], row[3]);
137 pub fn idct(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
138 const BLK_INDEX: [usize; 16] = [
144 let qidx = (qp % 6) as usize;
146 let start = if quant_dc { 0 } else { 1 };
147 for (el, &idx) in blk.iter_mut().zip(BLK_INDEX.iter()).skip(start) {
148 *el = (*el * LEVEL_SCALE[idx][qidx]) << shift;
151 transform!(blk[i], blk[i + 4], blk[i + 8], blk[i + 12], 0);
153 for row in blk.chunks_mut(4) {
154 transform!(row[0], row[1], row[2], row[3], 6);
158 pub fn idct_dc(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
159 let dc = if quant_dc {
160 (blk[0] * LEVEL_SCALE[0][(qp % 6) as usize]) << (qp / 6)
164 *blk = [(dc + 0x20) >> 6; 16];
167 const QMAT_8X8: [[u8; 16]; 6] = [
201 pub fn dequant8x8(blk: &mut [i16; 64], slist: &[u8; 64]) {
202 for (el, &scan) in blk.iter_mut().zip(ZIGZAG8X8.iter()) {
204 *el = el.wrapping_mul(i16::from(slist[scan]));
209 pub fn idct8x8(blk: &mut [i16; 64], qp: u8) {
210 let mut tmp = [0i32; 64];
211 let qmat = &QMAT_8X8[(qp % 6) as usize];
213 let shift = qp / 6 - 6;
214 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
217 let idx = (x & 3) + (y & 3) * 4;
218 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])) << shift;
221 let shift = 6 - qp / 6;
222 let bias = (1 << shift) >> 1;
223 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
226 let idx = (x & 3) + (y & 3) * 4;
227 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])).wrapping_add(bias) >> shift;
230 for row in tmp.chunks_mut(8) {
231 transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
234 transform!(tmp[col], tmp[col + 8], tmp[col + 8 * 2], tmp[col + 8 * 3],
235 tmp[col + 8 * 4], tmp[col + 8 * 5], tmp[col + 8 * 6], tmp[col + 8 * 7]);
237 for (dst, &src) in blk.iter_mut().zip(tmp.iter()) {
238 *dst = ((src + 0x20) >> 6) as i16;
242 pub fn add_coeffs(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16]) {
243 let out = &mut dst[offset..][..stride * 3 + 4];
244 for (line, src) in out.chunks_mut(stride).take(4).zip(coeffs.chunks(4)) {
245 for (dst, src) in line.iter_mut().take(4).zip(src.iter()) {
246 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
251 pub fn add_coeffs8(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16; 64]) {
252 let out = &mut dst[offset..];
253 for (line, src) in out.chunks_mut(stride).take(8).zip(coeffs.chunks(8)) {
254 for (dst, src) in line.iter_mut().take(8).zip(src.iter()) {
255 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
260 pub fn avg(dst: &mut [u8], dstride: usize,
261 src: &[u8], sstride: usize, bw: usize, bh: usize) {
262 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(bh) {
263 for (dst, src) in dline.iter_mut().zip(sline.iter()).take(bw) {
264 *dst = ((u16::from(*dst) + u16::from(*src) + 1) >> 1) as u8;
269 fn clip8(val: i16) -> u8 { val.max(0).min(255) as u8 }
271 fn ipred_dc128(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
273 for x in 0..bsize { buf[idx + x] = 128; }
277 fn ipred_ver(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
278 let oidx = idx - stride;
280 for x in 0..bsize { buf[idx + x] = buf[oidx + x]; }
284 fn ipred_hor(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
286 for x in 0..bsize { buf[idx + x] = buf[idx - 1]; }
290 fn ipred_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
291 let mut adc: u16 = 0;
292 for i in 0..bsize { adc += u16::from(buf[idx - stride + i]); }
293 for i in 0..bsize { adc += u16::from(buf[idx - 1 + i * stride]); }
294 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
297 for x in 0..bsize { buf[idx + x] = dc; }
301 fn ipred_left_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
302 let mut adc: u16 = 0;
303 for i in 0..bsize { adc += u16::from(buf[idx - 1 + i * stride]); }
304 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
307 for x in 0..bsize { buf[idx + x] = dc; }
311 fn ipred_top_dc(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize, shift: u8) {
312 let mut adc: u16 = 0;
313 for i in 0..bsize { adc += u16::from(buf[idx - stride + i]); }
314 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
317 for x in 0..bsize { buf[idx + x] = dc; }
322 fn load_top(dst: &mut [u16], buf: &mut [u8], idx: usize, stride: usize, len: usize) {
323 for i in 0..len { dst[i] = u16::from(buf[idx - stride + i]); }
325 fn load_left(dst: &mut [u16], buf: &mut [u8], idx: usize, stride: usize, len: usize) {
326 for i in 0..len { dst[i] = u16::from(buf[idx - 1 + i * stride]); }
329 fn ipred_4x4_ver(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
330 ipred_ver(buf, idx, stride, 4);
332 fn ipred_4x4_hor(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
333 ipred_hor(buf, idx, stride, 4);
335 fn ipred_4x4_diag_down_left(buf: &mut [u8], idx: usize, stride: usize, tr: &[u8]) {
336 let mut t: [u16; 9] = [0; 9];
337 load_top(&mut t, buf, idx, stride, 4);
339 t[i + 4] = u16::from(tr[i]);
343 let dst = &mut buf[idx..];
345 dst[i] = ((t[i] + 2 * t[i + 1] + t[i + 2] + 2) >> 2) as u8;
347 let dst = &mut buf[idx + stride..];
349 dst[i] = ((t[i + 1] + 2 * t[i + 2] + t[i + 3] + 2) >> 2) as u8;
351 let dst = &mut buf[idx + stride * 2..];
353 dst[i] = ((t[i + 2] + 2 * t[i + 3] + t[i + 4] + 2) >> 2) as u8;
355 let dst = &mut buf[idx + stride * 3..];
357 dst[i] = ((t[i + 3] + 2 * t[i + 4] + t[i + 5] + 2) >> 2) as u8;
360 fn ipred_4x4_diag_down_right(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
361 let mut t: [u16; 5] = [0; 5];
362 let mut l: [u16; 5] = [0; 5];
363 load_top(&mut t, buf, idx - 1, stride, 5);
364 load_left(&mut l, buf, idx - stride, stride, 5);
365 let dst = &mut buf[idx..];
369 dst[i + j * stride] = ((l[j - i - 1] + 2 * l[j - i] + l[j - i + 1] + 2) >> 2) as u8;
371 dst[j + j * stride] = ((l[1] + 2 * l[0] + t[1] + 2) >> 2) as u8;
373 dst[i + j * stride] = ((t[i - j - 1] + 2 * t[i - j] + t[i - j + 1] + 2) >> 2) as u8;
377 fn ipred_4x4_ver_right(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
378 let mut t: [u16; 5] = [0; 5];
379 let mut l: [u16; 5] = [0; 5];
380 load_top(&mut t, buf, idx - 1, stride, 5);
381 load_left(&mut l, buf, idx - stride, stride, 5);
382 let dst = &mut buf[idx..];
386 let zvr = ((2 * i) as i8) - (j as i8);
390 pix = (t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 1) >> 1;
392 pix = (t[i - (j >> 1) - 1] + 2 * t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 2) >> 2;
396 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
398 pix = (l[j] + 2 * l[j - 1] + l[j - 2] + 2) >> 2;
401 dst[i + j * stride] = pix as u8;
405 fn ipred_4x4_ver_left(buf: &mut [u8], idx: usize, stride: usize, tr: &[u8]) {
406 let mut t: [u16; 8] = [0; 8];
407 load_top(&mut t, buf, idx, stride, 4);
408 for i in 0..4 { t[i + 4] = u16::from(tr[i]); }
409 let dst = &mut buf[idx..];
411 dst[0 + 0 * stride] = ((t[0] + t[1] + 1) >> 1) as u8;
412 let pix = ((t[1] + t[2] + 1) >> 1) as u8;
413 dst[1 + 0 * stride] = pix;
414 dst[0 + 2 * stride] = pix;
415 let pix = ((t[2] + t[3] + 1) >> 1) as u8;
416 dst[2 + 0 * stride] = pix;
417 dst[1 + 2 * stride] = pix;
418 let pix = ((t[3] + t[4] + 1) >> 1) as u8;
419 dst[3 + 0 * stride] = pix;
420 dst[2 + 2 * stride] = pix;
421 dst[3 + 2 * stride] = ((t[4] + t[5] + 1) >> 1) as u8;
422 dst[0 + 1 * stride] = ((t[0] + 2*t[1] + t[2] + 2) >> 2) as u8;
423 let pix = ((t[1] + 2*t[2] + t[3] + 2) >> 2) as u8;
424 dst[1 + 1 * stride] = pix;
425 dst[0 + 3 * stride] = pix;
426 let pix = ((t[2] + 2*t[3] + t[4] + 2) >> 2) as u8;
427 dst[2 + 1 * stride] = pix;
428 dst[1 + 3 * stride] = pix;
429 let pix = ((t[3] + 2*t[4] + t[5] + 2) >> 2) as u8;
430 dst[3 + 1 * stride] = pix;
431 dst[2 + 3 * stride] = pix;
432 dst[3 + 3 * stride] = ((t[4] + 2*t[5] + t[6] + 2) >> 2) as u8;
434 fn ipred_4x4_hor_down(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
435 let mut t: [u16; 5] = [0; 5];
436 let mut l: [u16; 5] = [0; 5];
437 load_top(&mut t, buf, idx - 1, stride, 5);
438 load_left(&mut l, buf, idx - stride, stride, 5);
439 let dst = &mut buf[idx..];
443 let zhd = ((2 * j) as i8) - (i as i8);
447 pix = (l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 1) >> 1;
449 pix = (l[j - (i >> 1) - 1] + 2 * l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 2) >> 2;
453 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
455 pix = (t[i - 2] + 2 * t[i - 1] + t[i] + 2) >> 2;
458 dst[i + j * stride] = pix as u8;
462 fn ipred_4x4_hor_up(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
463 let mut l: [u16; 8] = [0; 8];
464 load_left(&mut l, buf, idx, stride, 8);
465 let dst = &mut buf[idx..];
467 dst[0 + 0 * stride] = ((l[0] + l[1] + 1) >> 1) as u8;
468 dst[1 + 0 * stride] = ((l[0] + 2*l[1] + l[2] + 2) >> 2) as u8;
469 let pix = ((l[1] + l[2] + 1) >> 1) as u8;
470 dst[2 + 0 * stride] = pix;
471 dst[0 + 1 * stride] = pix;
472 let pix = ((l[1] + 2*l[2] + l[3] + 2) >> 2) as u8;
473 dst[3 + 0 * stride] = pix;
474 dst[1 + 1 * stride] = pix;
475 let pix = ((l[2] + l[3] + 1) >> 1) as u8;
476 dst[2 + 1 * stride] = pix;
477 dst[0 + 2 * stride] = pix;
478 let pix = ((l[2] + 3*l[3] + 2) >> 2) as u8;
479 dst[3 + 1 * stride] = pix;
480 dst[1 + 2 * stride] = pix;
481 dst[3 + 2 * stride] = l[3] as u8;
482 dst[1 + 3 * stride] = l[3] as u8;
483 dst[0 + 3 * stride] = l[3] as u8;
484 dst[2 + 2 * stride] = l[3] as u8;
485 dst[2 + 3 * stride] = l[3] as u8;
486 dst[3 + 3 * stride] = l[3] as u8;
488 fn ipred_4x4_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
489 ipred_dc(buf, idx, stride, 4, 3);
491 fn ipred_4x4_left_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
492 ipred_left_dc(buf, idx, stride, 4, 2);
494 fn ipred_4x4_top_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
495 ipred_top_dc(buf, idx, stride, 4, 2);
497 fn ipred_4x4_dc128(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
498 ipred_dc128(buf, idx, stride, 4);
501 pub struct IPred8Context {
508 pub fn new() -> Self {
515 pub fn fill(&mut self, buf: &mut [u8], idx: usize, stride: usize, has_t: bool, has_tr: bool, has_l: bool, has_tl: bool) {
516 let mut t = [0x80u8; 19];
517 let mut l = [0x80u8; 11];
519 t[1..8 + 1].copy_from_slice(&buf[idx - stride..][..8]);
522 t[8 + 1..16 + 1].copy_from_slice(&buf[idx - stride + 8..][..8]);
523 t[16 + 1] = t[15 + 1];
524 t[17 + 1] = t[15 + 1];
526 let (t0, t1) = t.split_at_mut(8 + 1);
527 for el in t1.iter_mut() {
533 l[i + 1] = buf[idx - 1 + stride * i];
539 t[0] = buf[idx - 1 - stride];
540 l[0] = buf[idx - 1 - stride];
547 self.t[i] = ((u16::from(t[i]) + 2 * u16::from(t[i + 1]) + u16::from(t[i + 2]) + 2) >> 2) as u8;
550 self.l[i] = ((u16::from(l[i]) + 2 * u16::from(l[i + 1]) + u16::from(l[i + 2]) + 2) >> 2) as u8;
552 self.tl = if has_t && has_l {
553 ((u16::from(t[1]) + 2 * u16::from(t[0]) + u16::from(l[1]) + 2) >> 2) as u8
555 ((3 * u16::from(t[0]) + u16::from(t[1]) + 2) >> 2) as u8
557 ((3 * u16::from(l[0]) + u16::from(l[1]) + 2) >> 2) as u8
564 fn ipred_y_8x8_ver(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
565 for row in buf.chunks_mut(stride).take(8) {
566 row[..8].copy_from_slice(&ctx.t[..8]);
569 fn ipred_y_8x8_hor(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
570 for (row, &l) in buf.chunks_mut(stride).zip(ctx.l.iter()).take(8) {
571 row[..8].copy_from_slice(&[l; 8]);
574 fn ipred_y_8x8_diag_down_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
575 let mut t = [0u16; 16];
576 for (dt, &st) in t.iter_mut().zip(ctx.t.iter()) {
580 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
581 for (x, pix) in row.iter_mut().take(8).enumerate() {
582 *pix = ((if (x != 7) || (y != 7) {
583 t[x + y] + 2 * t[x + y + 1] + t[x + y + 2]
590 fn ipred_y_8x8_diag_down_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
591 let mut t = [0u16; 9];
592 t[0] = u16::from(ctx.tl);
593 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
596 let mut l = [0u16; 9];
597 l[0] = u16::from(ctx.tl);
598 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
601 let diag = t[1] + 2 * t[0] + l[1];
603 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
604 for (x, pix) in row.iter_mut().take(8).enumerate() {
606 t[x - y - 1] + 2 * t[x - y] + t[x - y + 1]
608 l[y - x - 1] + 2 * l[y - x] + l[y - x + 1]
615 fn ipred_y_8x8_ver_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
616 let mut t = [0u16; 9];
617 t[0] = u16::from(ctx.tl);
618 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
621 let mut l = [0u16; 9];
622 l[0] = u16::from(ctx.tl);
623 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
627 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
628 for (x, pix) in row.iter_mut().take(8).enumerate() {
629 let zvr = 2 * (x as i8) - (y as i8);
631 let ix = x - (y >> 1);
633 (t[ix] + t[ix + 1] + 1) >> 1
635 (t[ix - 1] + 2 * t[ix] + t[ix + 1] + 2) >> 2
637 } else if zvr == -1 {
638 (l[1] + 2 * l[0] + t[0] + 2) >> 2
641 (l[ix] + 2 * l[ix - 1] + l[ix - 2] + 2) >> 2
646 fn ipred_y_8x8_ver_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
647 let mut t = [0u16; 16];
648 for (dt, &st) in t.iter_mut().zip(ctx.t.iter()) {
652 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
653 for (x, pix) in row.iter_mut().take(8).enumerate() {
654 let ix = x + (y >> 1);
655 *pix = if (y & 1) == 0 {
656 (t[ix] + t[ix + 1] + 1) >> 1
658 (t[ix] + 2 * t[ix + 1] + t[ix + 2] + 2) >> 2
664 fn ipred_y_8x8_hor_down(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
665 let mut t = [0u16; 9];
666 t[0] = u16::from(ctx.tl);
667 for (dt, &st) in t[1..].iter_mut().zip(ctx.t.iter()) {
670 let mut l = [0u16; 9];
671 l[0] = u16::from(ctx.tl);
672 for (dl, &sl) in l[1..].iter_mut().zip(ctx.l.iter()) {
676 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
677 for (x, pix) in row.iter_mut().take(8).enumerate() {
678 let zhd = 2 * (y as i8) - (x as i8);
680 let ix = y - (x >> 1);
682 (l[ix] + l[ix + 1] + 1) >> 1
684 (l[ix - 1] + 2 * l[ix] + l[ix + 1] + 2) >> 2
686 } else if zhd == -1 {
687 (l[1] + 2 * l[0] + t[0] + 2) >> 2
690 (t[ix] + 2 * t[ix - 1] + t[ix - 2] + 2) >> 2
695 fn ipred_y_8x8_hor_up(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
696 let mut l = [0u16; 8];
697 for (dl, &sl) in l.iter_mut().zip(ctx.l.iter()) {
701 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
702 for (x, pix) in row.iter_mut().take(8).enumerate() {
704 let ix = y + (x >> 1);
707 } else if zhu == 13 {
708 (l[6] + 3 * l[7] + 2) >> 2
709 } else if (zhu & 1) != 0 {
710 (l[ix] + 2 * l[ix + 1] + l[ix + 2] + 2) >> 2
712 (l[ix] + l[ix + 1] + 1) >> 1
717 fn ipred_y_8x8_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
719 for &t in ctx.t[..8].iter() {
722 for &l in ctx.l[..8].iter() {
725 let dc = ((sum + 8) >> 4) as u8;
726 for row in buf.chunks_mut(stride).take(8) {
727 for pix in row.iter_mut().take(8) {
732 fn ipred_y_8x8_left_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
734 for &l in ctx.l[..8].iter() {
737 let dc = ((sum + 4) >> 3) as u8;
738 for row in buf.chunks_mut(stride).take(8) {
739 for pix in row.iter_mut().take(8) {
744 fn ipred_y_8x8_top_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
746 for &t in ctx.t[..8].iter() {
749 let dc = ((sum + 4) >> 3) as u8;
750 for row in buf.chunks_mut(stride).take(8) {
751 for pix in row.iter_mut().take(8) {
756 fn ipred_y_8x8_dc128(buf: &mut [u8], stride: usize, _ctx: &IPred8Context) {
757 ipred_dc128(buf, 0, stride, 8);
760 fn ipred_8x8_ver(buf: &mut [u8], idx: usize, stride: usize) {
761 ipred_ver(buf, idx, stride, 8);
763 fn ipred_8x8_hor(buf: &mut [u8], idx: usize, stride: usize) {
764 ipred_hor(buf, idx, stride, 8);
766 fn ipred_8x8_dc(buf: &mut [u8], idx: usize, stride: usize) {
767 let mut t: [u16; 8] = [0; 8];
768 load_top(&mut t, buf, idx, stride, 8);
769 let mut l: [u16; 8] = [0; 8];
770 load_left(&mut l, buf, idx, stride, 8);
772 let dc0 = ((t[0] + t[1] + t[2] + t[3] + l[0] + l[1] + l[2] + l[3] + 4) >> 3) as u8;
773 let sum1 = t[4] + t[5] + t[6] + t[7];
774 let dc1 = ((sum1 + 2) >> 2) as u8;
775 let sum2 = l[4] + l[5] + l[6] + l[7];
776 let dc2 = ((sum2 + 2) >> 2) as u8;
777 let dc3 = ((sum1 + sum2 + 4) >> 3) as u8;
779 let dst = &mut buf[idx..];
780 for row in dst.chunks_mut(stride).take(4) {
781 row[..4].copy_from_slice(&[dc0; 4]);
782 row[4..8].copy_from_slice(&[dc1; 4]);
784 for row in dst.chunks_mut(stride).skip(4).take(4) {
785 row[..4].copy_from_slice(&[dc2; 4]);
786 row[4..8].copy_from_slice(&[dc3; 4]);
789 fn ipred_8x8_left_dc(buf: &mut [u8], idx: usize, stride: usize) {
790 let mut left_dc0 = 0;
791 let mut left_dc1 = 0;
792 for row in buf[idx - 1..].chunks(stride).take(4) {
793 left_dc0 += u16::from(row[0]);
795 for row in buf[idx - 1..].chunks(stride).skip(4).take(4) {
796 left_dc1 += u16::from(row[0]);
798 let dc0 = ((left_dc0 + 2) >> 2) as u8;
799 let dc2 = ((left_dc1 + 2) >> 2) as u8;
800 for row in buf[idx..].chunks_mut(stride).take(4) {
801 row[..8].copy_from_slice(&[dc0; 8]);
803 for row in buf[idx..].chunks_mut(stride).skip(4).take(4) {
804 row[..8].copy_from_slice(&[dc2; 8]);
807 fn ipred_8x8_top_dc(buf: &mut [u8], idx: usize, stride: usize) {
808 ipred_top_dc(buf, idx, stride, 4, 2);
809 ipred_top_dc(buf, idx + 4, stride, 4, 2);
810 ipred_top_dc(buf, idx + 4 * stride, stride, 4, 2);
811 ipred_top_dc(buf, idx + 4 + 4 * stride, stride, 4, 2);
813 fn ipred_8x8_dc128(buf: &mut [u8], idx: usize, stride: usize) {
814 ipred_dc128(buf, idx, stride, 8);
816 fn ipred_8x8_plane(buf: &mut [u8], idx: usize, stride: usize) {
819 let idx0 = idx + 3 - stride;
820 let mut idx1 = idx + 4 * stride - 1;
821 let mut idx2 = idx + 2 * stride - 1;
823 let i1 = (i + 1) as i32;
824 h += i1 * (i32::from(buf[idx0 + i + 1]) - i32::from(buf[idx0 - i - 1]));
825 v += i1 * (i32::from(buf[idx1]) - i32::from(buf[idx2]));
829 let b = (17 * h + 16) >> 5;
830 let c = (17 * v + 16) >> 5;
831 let mut a = 16 * (i32::from(buf[idx - 1 + 7 * stride]) + i32::from(buf[idx + 7 - stride])) - 3 * (b + c) + 16;
832 for line in buf[idx..].chunks_mut(stride).take(8) {
834 for el in line.iter_mut().take(8) {
835 *el = clip8((acc >> 5) as i16);
842 fn ipred_16x16_ver(buf: &mut [u8], idx: usize, stride: usize) {
843 ipred_ver(buf, idx, stride, 16);
845 fn ipred_16x16_hor(buf: &mut [u8], idx: usize, stride: usize) {
846 ipred_hor(buf, idx, stride, 16);
848 fn ipred_16x16_dc(buf: &mut [u8], idx: usize, stride: usize) {
849 ipred_dc(buf, idx, stride, 16, 5);
851 fn ipred_16x16_left_dc(buf: &mut [u8], idx: usize, stride: usize) {
852 ipred_left_dc(buf, idx, stride, 16, 4);
854 fn ipred_16x16_top_dc(buf: &mut [u8], idx: usize, stride: usize) {
855 ipred_top_dc(buf, idx, stride, 16, 4);
857 fn ipred_16x16_dc128(buf: &mut [u8], idx: usize, stride: usize) {
858 ipred_dc128(buf, idx, stride, 16);
860 fn ipred_16x16_plane(buf: &mut [u8], idx: usize, stride: usize) {
861 let idx0 = idx + 7 - stride;
862 let mut idx1 = idx + 8 * stride - 1;
863 let mut idx2 = idx1 - 2 * stride;
865 let mut h = i32::from(buf[idx0 + 1]) - i32::from(buf[idx0 - 1]);
866 let mut v = i32::from(buf[idx1]) - i32::from(buf[idx2]);
871 h += (k as i32) * (i32::from(buf[idx0 + k]) - i32::from(buf[idx0 - k]));
872 v += (k as i32) * (i32::from(buf[idx1]) - i32::from(buf[idx2]));
874 h = (5 * h + 32) >> 6;
875 v = (5 * v + 32) >> 6;
877 let mut a = 16 * (i32::from(buf[idx - 1 + 15 * stride]) + i32::from(buf[idx + 15 - stride]) + 1) - 7 * (v + h);
879 for row in buf[idx..].chunks_mut(stride).take(16) {
883 for dst in row.chunks_exact_mut(4).take(4) {
884 dst[0] = clip8(((b ) >> 5) as i16);
885 dst[1] = clip8(((b + h) >> 5) as i16);
886 dst[2] = clip8(((b + 2*h) >> 5) as i16);
887 dst[3] = clip8(((b + 3*h) >> 5) as i16);
893 pub type IPred4x4Func = fn(buf: &mut [u8], off: usize, stride: usize, tr: &[u8]);
894 pub type IPred8x8Func = fn(buf: &mut [u8], off: usize, stride: usize);
895 pub type IPred8x8LumaFunc = fn(buf: &mut [u8], stride: usize, ctx: &IPred8Context);
897 pub const IPRED4_DC128: usize = 11;
898 pub const IPRED4_DC_TOP: usize = 10;
899 pub const IPRED4_DC_LEFT: usize = 9;
900 pub const IPRED8_DC128: usize = 6;
901 pub const IPRED8_DC_TOP: usize = 5;
902 pub const IPRED8_DC_LEFT: usize = 4;
904 pub const IPRED_FUNCS4X4: [IPred4x4Func; 12] = [
905 ipred_4x4_ver, ipred_4x4_hor, ipred_4x4_dc,
906 ipred_4x4_diag_down_left, ipred_4x4_diag_down_right,
907 ipred_4x4_ver_right, ipred_4x4_hor_down, ipred_4x4_ver_left, ipred_4x4_hor_up,
908 ipred_4x4_left_dc, ipred_4x4_top_dc, ipred_4x4_dc128
911 pub const IPRED_FUNCS8X8_LUMA: [IPred8x8LumaFunc; 12] = [
912 ipred_y_8x8_ver, ipred_y_8x8_hor, ipred_y_8x8_dc,
913 ipred_y_8x8_diag_down_left, ipred_y_8x8_diag_down_right,
914 ipred_y_8x8_ver_right, ipred_y_8x8_hor_down,
915 ipred_y_8x8_ver_left, ipred_y_8x8_hor_up,
916 ipred_y_8x8_left_dc, ipred_y_8x8_top_dc, ipred_y_8x8_dc128
919 pub const IPRED_FUNCS8X8_CHROMA: [IPred8x8Func; 7] = [
920 ipred_8x8_dc, ipred_8x8_hor, ipred_8x8_ver, ipred_8x8_plane,
921 ipred_8x8_left_dc, ipred_8x8_top_dc, ipred_8x8_dc128
924 pub const IPRED_FUNCS16X16: [IPred8x8Func; 7] = [
925 ipred_16x16_ver, ipred_16x16_hor, ipred_16x16_dc, ipred_16x16_plane,
926 ipred_16x16_left_dc, ipred_16x16_top_dc, ipred_16x16_dc128
929 fn clip_u8(val: i16) -> u8 { val.max(0).min(255) as u8 }
931 const TMP_BUF_STRIDE: usize = 32;
933 fn interp_block1(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize, hor: bool, avg0: bool) {
934 let step = if hor { 1 } else { sstride };
936 let avgidx = if avg0 { step * 2 } else { step * 3 };
937 for dline in dst.chunks_mut(dstride).take(h) {
938 for (x, pix) in dline.iter_mut().take(w).enumerate() {
939 let t = clip_u8(( i16::from(src[idx + x])
940 - 5 * i16::from(src[idx + x + step])
941 + 20 * i16::from(src[idx + x + step * 2])
942 + 20 * i16::from(src[idx + x + step * 3])
943 - 5 * i16::from(src[idx + x + step * 4])
944 + i16::from(src[idx + x + step * 5])
946 *pix = ((u16::from(t) + u16::from(src[idx + x + avgidx]) + 1) >> 1) as u8;
952 fn interp_block2(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize, hor: bool) {
953 let step = if hor { 1 } else { sstride };
955 for dline in dst.chunks_mut(dstride).take(h) {
956 for (x, pix) in dline.iter_mut().take(w).enumerate() {
957 *pix = clip_u8(( i16::from(src[idx + x])
958 - 5 * i16::from(src[idx + x + step])
959 + 20 * i16::from(src[idx + x + step * 2])
960 + 20 * i16::from(src[idx + x + step * 3])
961 - 5 * i16::from(src[idx + x + step * 4])
962 + i16::from(src[idx + x + step * 5])
969 fn mc_avg_tmp(dst: &mut [u8], dstride: usize, w: usize, h: usize, tmp: &[u8], tmp2: &[u8]) {
970 for (dline, (sline0, sline1)) in dst.chunks_mut(dstride).zip(tmp.chunks(TMP_BUF_STRIDE).zip(tmp2.chunks(TMP_BUF_STRIDE))).take(h) {
971 for (pix, (&a, &b)) in dline.iter_mut().zip(sline0.iter().zip(sline1.iter())).take(w) {
972 *pix = ((u16::from(a) + u16::from(b) + 1) >> 1) as u8;
977 fn h264_mc00(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
978 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(h) {
979 dline[..w].copy_from_slice(&sline[..w]);
983 fn h264_mc01(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
984 interp_block1(dst, dstride, &src[sstride * 2..], sstride, w, h, true, true);
987 fn h264_mc02(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
988 interp_block2(dst, dstride, &src[sstride * 2..], sstride, w, h, true);
991 fn h264_mc03(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
992 interp_block1(dst, dstride, &src[sstride * 2..], sstride, w, h, true, false);
995 fn h264_mc10(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
996 interp_block1(dst, dstride, &src[2..], sstride, w, h, false, true);
999 fn h264_mc11(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1000 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1001 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1002 h264_mc02(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1003 h264_mc20(&mut tmp2, TMP_BUF_STRIDE, src, sstride, w, h);
1004 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1007 fn h264_mc12(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1008 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1009 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1010 h264_mc02(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1011 h264_mc22(&mut tmp2, TMP_BUF_STRIDE, src, sstride, w, h);
1012 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1015 fn h264_mc13(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1016 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1017 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1018 h264_mc02(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1019 h264_mc20(&mut tmp2, TMP_BUF_STRIDE, &src[1..], sstride, w, h);
1020 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1023 fn h264_mc20(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1024 interp_block2(dst, dstride, &src[2..], sstride, w, h, false);
1027 fn h264_mc21(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1028 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1029 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1030 h264_mc22(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1031 h264_mc20(&mut tmp2, TMP_BUF_STRIDE, src, sstride, w, h);
1032 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1035 fn h264_mc22(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1036 let mut tmp = [0i32; TMP_BUF_STRIDE * 16];
1038 for dline in tmp.chunks_mut(TMP_BUF_STRIDE).take(h) {
1039 for (x, pix) in dline.iter_mut().take(w + 5).enumerate() {
1040 *pix = i32::from(src[idx + x])
1041 - 5 * i32::from(src[idx + x + sstride])
1042 + 20 * i32::from(src[idx + x + sstride * 2])
1043 + 20 * i32::from(src[idx + x + sstride * 3])
1044 - 5 * i32::from(src[idx + x + sstride * 4])
1045 + i32::from(src[idx + x + sstride * 5]);
1049 for (dline, sline) in dst.chunks_mut(dstride).zip(tmp.chunks(TMP_BUF_STRIDE)).take(h) {
1050 for (x, pix) in dline.iter_mut().take(w).enumerate() {
1051 *pix = clip8(((sline[x] - 5 * sline[x + 1] + 20 * sline[x + 2] + 20 * sline[x + 3] - 5 * sline[x + 4] + sline[x + 5] + 512) >> 10) as i16);
1056 fn h264_mc23(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1057 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1058 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1059 h264_mc22(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1060 h264_mc20(&mut tmp2, TMP_BUF_STRIDE, &src[1..], sstride, w, h);
1061 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1064 fn h264_mc30(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1065 interp_block1(dst, dstride, &src[2..], sstride, w, h, false, false);
1068 fn h264_mc31(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1069 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1070 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1071 h264_mc20(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1072 h264_mc02(&mut tmp2, TMP_BUF_STRIDE, &src[sstride..], sstride, w, h);
1073 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1076 fn h264_mc32(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1077 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1078 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1079 h264_mc22(&mut tmp, TMP_BUF_STRIDE, src, sstride, w, h);
1080 h264_mc02(&mut tmp2, TMP_BUF_STRIDE, &src[sstride..], sstride, w, h);
1081 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1084 fn h264_mc33(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, w: usize, h: usize) {
1085 let mut tmp = [0u8; TMP_BUF_STRIDE * 16];
1086 let mut tmp2 = [0u8; TMP_BUF_STRIDE * 16];
1087 h264_mc20(&mut tmp, TMP_BUF_STRIDE, &src[1..], sstride, w, h);
1088 h264_mc02(&mut tmp2, TMP_BUF_STRIDE, &src[sstride..], sstride, w, h);
1089 mc_avg_tmp(dst, dstride, w, h, &tmp, &tmp2);
1093 fn chroma_interp(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, dx: u16, dy: u16, w: usize, h: usize) {
1099 let src1 = &src[sstride..];
1100 for (drow, (line0, line1)) in dst.chunks_mut(dstride).zip(src.chunks(sstride).zip(src1.chunks(sstride))).take(h) {
1101 let mut a = line0[0];
1102 let mut c = line1[0];
1103 for (pix, (&b, &d)) in drow.iter_mut().take(w).zip(line0[1..].iter().zip(line1[1..].iter())) {
1104 *pix = ((u16::from(a) * a0 * b0 + u16::from(b) * a1 * b0 + u16::from(c) * a0 * b1 + u16::from(d) * a1 * b1 + 0x20) >> 6) as u8;
1111 const H264_LUMA_INTERP: &[BlkInterpFunc] = &[
1112 h264_mc00, h264_mc01, h264_mc02, h264_mc03,
1113 h264_mc10, h264_mc11, h264_mc12, h264_mc13,
1114 h264_mc20, h264_mc21, h264_mc22, h264_mc23,
1115 h264_mc30, h264_mc31, h264_mc32, h264_mc33
1118 pub fn do_mc(frm: &mut NASimpleVideoFrame<u8>, refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
1119 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
1120 copy_block(frm, refpic.clone(), 0, xpos, ypos, mv.x >> 2, mv.y >> 2, w, h, 2, 3, mode, H264_LUMA_INTERP);
1122 let (cw, ch) = refpic.get_dimensions(1);
1123 let mvx = mv.x >> 3;
1124 let mvy = mv.y >> 3;
1125 let dx = (mv.x & 7) as u16;
1126 let dy = (mv.y & 7) as u16;
1127 let mut ebuf = [0u8; 18 * 9];
1128 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
1129 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
1130 let suoff = refpic.get_offset(1);
1131 let svoff = refpic.get_offset(2);
1132 let sustride = refpic.get_stride(1);
1133 let svstride = refpic.get_stride(2);
1134 let src = refpic.get_data();
1137 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)) {
1138 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf, 18, 1, 4);
1139 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf[9..], 18, 2, 4);
1140 ([&ebuf, &ebuf[9..]], [18, 18])
1142 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
1143 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
1144 [sustride, svstride])
1146 for chroma in 1..3 {
1147 let off = frm.offset[chroma] + xpos / 2 + (ypos / 2) * frm.stride[chroma];
1148 chroma_interp(&mut frm.data[off..], frm.stride[chroma], csrc[chroma - 1], cstride[chroma - 1], dx, dy, cbw, cbh);
1152 pub fn gray_block(frm: &mut NASimpleVideoFrame<u8>, x: usize, y: usize, w: usize, h: usize) {
1153 let yoff = frm.offset[0] + x + y * frm.stride[0];
1154 let coff = [frm.offset[1] + x / 2 + y / 2 * frm.stride[1],
1155 frm.offset[2] + x / 2 + y / 2 * frm.stride[2]];
1156 if w == 16 && h == 16 {
1157 IPRED_FUNCS16X16[IPRED8_DC128](frm.data, yoff, frm.stride[0]);
1158 for chroma in 1..2 {
1159 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, coff[chroma - 1], frm.stride[chroma]);
1161 } else if w == 8 && h == 8 {
1162 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, yoff, frm.stride[0]);
1163 for chroma in 1..2 {
1164 IPRED_FUNCS4X4[IPRED4_DC128](frm.data, coff[chroma - 1], frm.stride[chroma], &[128; 4]);
1167 for row in frm.data[yoff..].chunks_mut(frm.stride[0]).take(h) {
1168 for el in row[..w].iter_mut() {
1172 for chroma in 0..2 {
1173 for row in frm.data[coff[chroma]..].chunks_mut(frm.stride[chroma + 1]).take(h / 2) {
1174 for el in row[..w / 2].iter_mut() {
1182 pub 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>) {
1183 let mut afrm = NASimpleVideoFrame::from_video_buf(avg_buf).unwrap();
1184 let amv = MV { x: mv.x + (xpos as i16) * 4, y: mv.y + (ypos as i16) * 4 };
1185 do_mc(&mut afrm, refpic, 0, 0, w, h, amv);
1187 let shift = if comp == 0 { 0 } else { 1 };
1188 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);
1192 macro_rules! loop_filter {
1193 (lumaedge; $buf: expr, $off: expr, $step: expr, $alpha: expr, $beta: expr) => {
1194 let p2 = i16::from($buf[$off - $step * 3]);
1195 let p1 = i16::from($buf[$off - $step * 2]);
1196 let p0 = i16::from($buf[$off - $step]);
1197 let q0 = i16::from($buf[$off]);
1198 let q1 = i16::from($buf[$off + $step]);
1199 let q2 = i16::from($buf[$off + $step * 2]);
1200 let a_p = (p2 - p0).abs() < $beta;
1201 let a_q = (q2 - q0).abs() < $beta;
1202 if a_p && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1203 let p3 = i16::from($buf[$off - $step * 4]);
1204 $buf[$off - $step * 3] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
1205 $buf[$off - $step * 2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
1206 $buf[$off - $step] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
1208 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1210 if a_q && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1211 let q3 = i16::from($buf[$off + $step * 3]);
1212 $buf[$off] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
1213 $buf[$off + $step] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
1214 $buf[$off + $step * 2] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
1216 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1219 (chromaedge; $buf: expr, $off: expr, $step: expr) => {
1220 let p1 = i16::from($buf[$off - $step * 2]);
1221 let p0 = i16::from($buf[$off - $step]);
1222 let q0 = i16::from($buf[$off]);
1223 let q1 = i16::from($buf[$off + $step]);
1224 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1225 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1227 (lumanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr, $beta: expr) => {
1228 let p2 = i16::from($buf[$off - $step * 3]);
1229 let p1 = i16::from($buf[$off - $step * 2]);
1230 let p0 = i16::from($buf[$off - $step]);
1231 let q0 = i16::from($buf[$off]);
1232 let q1 = i16::from($buf[$off + $step]);
1233 let q2 = i16::from($buf[$off + $step * 2]);
1234 let a_p = (p2 - p0).abs() < $beta;
1235 let a_q = (q2 - q0).abs() < $beta;
1236 let tc = $tc0 + (a_p as i16) + (a_q as i16);
1237 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1238 if a_p && ($tc0 > 0) {
1239 $buf[$off - $step * 2] = clip8(p1 + ((p2 + ((p0 + q0 + 1) >> 1) - p1 * 2) >> 1).max(-$tc0).min($tc0));
1241 $buf[$off - $step] = clip8(p0 + delta);
1242 $buf[$off] = clip8(q0 - delta);
1243 if a_q && ($tc0 > 0) {
1244 $buf[$off + $step] = clip8(q1 + ((q2 + ((p0 + q0 + 1) >> 1) - q1 * 2) >> 1).max(-$tc0).min($tc0));
1247 (chromanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr) => {
1248 let p1 = i16::from($buf[$off - $step * 2]);
1249 let p0 = i16::from($buf[$off - $step]);
1250 let q0 = i16::from($buf[$off]);
1251 let q1 = i16::from($buf[$off + $step]);
1253 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1254 $buf[$off - $step] = clip8(p0 + delta);
1255 $buf[$off] = clip8(q0 - delta);
1259 fn check_filter(buf: &[u8], off: usize, step: usize, alpha: i16, beta: i16) -> bool {
1260 let p1 = i16::from(buf[off - step * 2]);
1261 let p0 = i16::from(buf[off - step]);
1262 let q0 = i16::from(buf[off]);
1263 let q1 = i16::from(buf[off + step]);
1264 (p0 - q0).abs() < alpha && (p1 - p0).abs() < beta && (q1 - q0).abs() < beta
1267 pub fn loop_filter_lumaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1269 if check_filter(dst, off, 1, alpha, beta) {
1270 loop_filter!(lumaedge; dst, off, 1, alpha, beta);
1275 pub fn loop_filter_lumaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1277 if check_filter(dst, off + x, stride, alpha, beta) {
1278 loop_filter!(lumaedge; dst, off + x, stride, alpha, beta);
1282 pub fn loop_filter_lumanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1284 if check_filter(dst, off, 1, alpha, beta) {
1285 loop_filter!(lumanormal; dst, off, 1, tc0, beta);
1290 pub fn loop_filter_lumanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1292 if check_filter(dst, off + x, stride, alpha, beta) {
1293 loop_filter!(lumanormal; dst, off + x, stride, tc0, beta);
1297 pub fn loop_filter_chromaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1299 if check_filter(dst, off, 1, alpha, beta) {
1300 loop_filter!(chromaedge; dst, off, 1);
1305 pub fn loop_filter_chromaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1307 if check_filter(dst, off + x, stride, alpha, beta) {
1308 loop_filter!(chromaedge; dst, off + x, stride);
1312 pub fn loop_filter_chromanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1314 if check_filter(dst, off, 1, alpha, beta) {
1315 loop_filter!(chromanormal; dst, off, 1, tc0);
1320 pub fn loop_filter_chromanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1322 if check_filter(dst, off + x, stride, alpha, beta) {
1323 loop_filter!(chromanormal; dst, off + x, stride, tc0);