1 use nihav_core::frame::*;
2 use nihav_codec_support::codecs::blockdsp::*;
3 use nihav_codec_support::codecs::MV;
5 #[cfg(not(debug_assertions))]
7 #[cfg(not(debug_assertions))]
9 #[cfg(debug_assertions)]
11 #[cfg(debug_assertions)]
14 pub 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,
21 pub const CHROMA_DC_SCAN: [usize; 4] = [ 0, 1, 2, 3];
22 pub const ZIGZAG: [usize; 16] = [
23 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
25 pub const ZIGZAG1: [usize; 15] = [
26 0, 3, 7, 4, 1, 2, 5, 8, 11, 12, 9, 6, 10, 13, 14
28 /*pub const IL_SCAN: [usize; 16] = [
29 0, 4, 1, 8, 12, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15
31 pub 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
42 const 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 ]
48 pub 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];
58 let mul = LEVEL_SCALE[0][qp as usize];
59 for el in blk.iter_mut() {
60 *el = el.wrapping_mul(mul) >> 1;
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;
71 macro_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);
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;
93 ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr, $h: expr) => {
95 let e1 = -$d + $f - $h - ($h >> 1);
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);
104 let f1 = e1 + (e7 >> 2);
106 let f3 = e3 + (e5 >> 2);
108 let f5 = (e3 >> 2) - e5;
110 let f7 = e7 - (e1 >> 2);
123 pub fn idct_luma_dc(blk: &mut [i16; 16], qp: u8) {
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;
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;
139 transform!(luma_dc; blk[i], blk[i + 4], blk[i + 8], blk[i + 12]);
141 for row in blk.chunks_mut(4) {
142 transform!(luma_dc; row[0], row[1], row[2], row[3]);
146 pub fn idct(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
147 const BLK_INDEX: [usize; 16] = [
153 let qidx = (qp % 6) as usize;
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;
159 for row in blk.chunks_mut(4) {
160 transform!(row[0], row[1], row[2], row[3], 0);
163 transform!(blk[i], blk[i + 4], blk[i + 8], blk[i + 12], 6);
167 pub 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)
173 *blk = [(dc + 0x20) >> 6; 16];
176 const QMAT_8X8: [[u8; 16]; 6] = [
210 pub fn dequant8x8(blk: &mut [i16; 64], slist: &[u8; 64]) {
211 for (el, &scan) in blk.iter_mut().zip(ZIGZAG8X8.iter()) {
213 *el = el.wrapping_mul(i16::from(slist[scan]));
218 pub fn idct8x8(blk: &mut [i16; 64], qp: u8) {
219 let mut tmp = [0i32; 64];
220 let qmat = &QMAT_8X8[(qp % 6) as usize];
222 let shift = qp / 6 - 6;
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])) << shift;
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() {
235 let idx = (x & 3) + (y & 3) * 4;
236 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])).wrapping_add(bias) >> shift;
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]);
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]);
246 for (dst, &src) in blk.iter_mut().zip(tmp.iter()) {
247 *dst = ((src + 0x20) >> 6) as i16;
251 pub 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;
260 pub 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;
269 pub 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;
278 fn clip8(val: i16) -> u8 { val.max(0).min(255) as u8 }
280 fn ipred_dc128(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
282 for x in 0..bsize { buf[idx + x] = 128; }
286 fn ipred_ver(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
287 let oidx = idx - stride;
289 for x in 0..bsize { buf[idx + x] = buf[oidx + x]; }
293 fn ipred_hor(buf: &mut [u8], mut idx: usize, stride: usize, bsize: usize) {
295 for x in 0..bsize { buf[idx + x] = buf[idx - 1]; }
299 fn 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;
306 for x in 0..bsize { buf[idx + x] = dc; }
310 fn 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;
316 for x in 0..bsize { buf[idx + x] = dc; }
320 fn 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;
326 for x in 0..bsize { buf[idx + x] = dc; }
331 fn 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]); }
334 fn 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]); }
338 fn ipred_4x4_ver(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
339 ipred_ver(buf, idx, stride, 4);
341 fn ipred_4x4_hor(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
342 ipred_hor(buf, idx, stride, 4);
344 fn 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);
348 t[i + 4] = u16::from(tr[i]);
352 let dst = &mut buf[idx..];
354 dst[i] = ((t[i] + 2 * t[i + 1] + t[i + 2] + 2) >> 2) as u8;
356 let dst = &mut buf[idx + stride..];
358 dst[i] = ((t[i + 1] + 2 * t[i + 2] + t[i + 3] + 2) >> 2) as u8;
360 let dst = &mut buf[idx + stride * 2..];
362 dst[i] = ((t[i + 2] + 2 * t[i + 3] + t[i + 4] + 2) >> 2) as u8;
364 let dst = &mut buf[idx + stride * 3..];
366 dst[i] = ((t[i + 3] + 2 * t[i + 4] + t[i + 5] + 2) >> 2) as u8;
369 fn 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..];
378 dst[i + j * stride] = ((l[j - i - 1] + 2 * l[j - i] + l[j - i + 1] + 2) >> 2) as u8;
380 dst[j + j * stride] = ((l[1] + 2 * l[0] + t[1] + 2) >> 2) as u8;
382 dst[i + j * stride] = ((t[i - j - 1] + 2 * t[i - j] + t[i - j + 1] + 2) >> 2) as u8;
386 fn 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..];
395 let zvr = ((2 * i) as i8) - (j as i8);
399 pix = (t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 1) >> 1;
401 pix = (t[i - (j >> 1) - 1] + 2 * t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 2) >> 2;
405 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
407 pix = (l[j] + 2 * l[j - 1] + l[j - 2] + 2) >> 2;
410 dst[i + j * stride] = pix as u8;
414 fn 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..];
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;
443 fn 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..];
452 let zhd = ((2 * j) as i8) - (i as i8);
456 pix = (l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 1) >> 1;
458 pix = (l[j - (i >> 1) - 1] + 2 * l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 2) >> 2;
462 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
464 pix = (t[i - 2] + 2 * t[i - 1] + t[i] + 2) >> 2;
467 dst[i + j * stride] = pix as u8;
471 fn 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..];
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;
497 fn ipred_4x4_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
498 ipred_dc(buf, idx, stride, 4, 3);
500 fn ipred_4x4_left_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
501 ipred_left_dc(buf, idx, stride, 4, 2);
503 fn ipred_4x4_top_dc(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
504 ipred_top_dc(buf, idx, stride, 4, 2);
506 fn ipred_4x4_dc128(buf: &mut [u8], idx: usize, stride: usize, _tr: &[u8]) {
507 ipred_dc128(buf, idx, stride, 4);
510 pub struct IPred8Context {
517 pub fn new() -> Self {
524 pub fn fill(&mut self, buf: &[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];
528 t[1..8 + 1].copy_from_slice(&buf[idx - stride..][..8]);
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];
535 let (t0, t1) = t.split_at_mut(8 + 1);
536 for el in t1.iter_mut() {
542 l[i + 1] = buf[idx - 1 + stride * i];
548 t[0] = buf[idx - 1 - stride];
549 l[0] = buf[idx - 1 - stride];
556 self.t[i] = ((u16::from(t[i]) + 2 * u16::from(t[i + 1]) + u16::from(t[i + 2]) + 2) >> 2) as u8;
559 self.l[i] = ((u16::from(l[i]) + 2 * u16::from(l[i + 1]) + u16::from(l[i + 2]) + 2) >> 2) as u8;
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
564 ((3 * u16::from(t[0]) + u16::from(t[1]) + 2) >> 2) as u8
566 ((3 * u16::from(l[0]) + u16::from(l[1]) + 2) >> 2) as u8
573 fn 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]);
578 fn 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]);
583 fn 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()) {
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]
599 fn 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()) {
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()) {
610 let diag = t[1] + 2 * t[0] + l[1];
612 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
613 for (x, pix) in row.iter_mut().take(8).enumerate() {
615 t[x - y - 1] + 2 * t[x - y] + t[x - y + 1]
617 l[y - x - 1] + 2 * l[y - x] + l[y - x + 1]
624 fn 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()) {
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()) {
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);
640 let ix = x - (y >> 1);
642 (t[ix] + t[ix + 1] + 1) >> 1
644 (t[ix - 1] + 2 * t[ix] + t[ix + 1] + 2) >> 2
646 } else if zvr == -1 {
647 (l[1] + 2 * l[0] + t[1] + 2) >> 2
650 (l[ix] + 2 * l[ix - 1] + l[ix - 2] + 2) >> 2
655 fn 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()) {
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
667 (t[ix] + 2 * t[ix + 1] + t[ix + 2] + 2) >> 2
673 fn 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()) {
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()) {
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);
689 let ix = y - (x >> 1);
691 (l[ix] + l[ix + 1] + 1) >> 1
693 (l[ix - 1] + 2 * l[ix] + l[ix + 1] + 2) >> 2
695 } else if zhd == -1 {
696 (l[1] + 2 * l[0] + t[1] + 2) >> 2
699 (t[ix] + 2 * t[ix - 1] + t[ix - 2] + 2) >> 2
704 fn 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()) {
710 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
711 for (x, pix) in row.iter_mut().take(8).enumerate() {
713 let ix = y + (x >> 1);
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
721 (l[ix] + l[ix + 1] + 1) >> 1
726 fn ipred_y_8x8_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
728 for &t in ctx.t[..8].iter() {
731 for &l in ctx.l[..8].iter() {
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) {
741 fn ipred_y_8x8_left_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
743 for &l in ctx.l[..8].iter() {
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) {
753 fn ipred_y_8x8_top_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
755 for &t in ctx.t[..8].iter() {
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) {
765 fn ipred_y_8x8_dc128(buf: &mut [u8], stride: usize, _ctx: &IPred8Context) {
766 ipred_dc128(buf, 0, stride, 8);
769 fn ipred_8x8_ver(buf: &mut [u8], idx: usize, stride: usize) {
770 ipred_ver(buf, idx, stride, 8);
772 fn ipred_8x8_hor(buf: &mut [u8], idx: usize, stride: usize) {
773 ipred_hor(buf, idx, stride, 8);
775 fn 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);
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;
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]);
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]);
798 fn 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]);
804 for row in buf[idx - 1..].chunks(stride).skip(4).take(4) {
805 left_dc1 += u16::from(row[0]);
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]);
812 for row in buf[idx..].chunks_mut(stride).skip(4).take(4) {
813 row[..8].copy_from_slice(&[dc2; 8]);
816 fn 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);
822 fn ipred_8x8_dc128(buf: &mut [u8], idx: usize, stride: usize) {
823 ipred_dc128(buf, idx, stride, 8);
825 fn ipred_8x8_plane(buf: &mut [u8], idx: usize, stride: usize) {
828 let idx0 = idx + 3 - stride;
829 let mut idx1 = idx + 4 * stride - 1;
830 let mut idx2 = idx + 2 * stride - 1;
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]));
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) {
843 for el in line.iter_mut().take(8) {
844 *el = clip8((acc >> 5) as i16);
851 fn ipred_16x16_ver(buf: &mut [u8], idx: usize, stride: usize) {
852 ipred_ver(buf, idx, stride, 16);
854 fn ipred_16x16_hor(buf: &mut [u8], idx: usize, stride: usize) {
855 ipred_hor(buf, idx, stride, 16);
857 fn ipred_16x16_dc(buf: &mut [u8], idx: usize, stride: usize) {
858 ipred_dc(buf, idx, stride, 16, 5);
860 fn ipred_16x16_left_dc(buf: &mut [u8], idx: usize, stride: usize) {
861 ipred_left_dc(buf, idx, stride, 16, 4);
863 fn ipred_16x16_top_dc(buf: &mut [u8], idx: usize, stride: usize) {
864 ipred_top_dc(buf, idx, stride, 16, 4);
866 fn ipred_16x16_dc128(buf: &mut [u8], idx: usize, stride: usize) {
867 ipred_dc128(buf, idx, stride, 16);
869 fn 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;
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]);
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]));
883 h = (5 * h + 32) >> 6;
884 v = (5 * v + 32) >> 6;
886 let mut a = 16 * (i32::from(buf[idx - 1 + 15 * stride]) + i32::from(buf[idx + 15 - stride]) + 1) - 7 * (v + h);
888 for row in buf[idx..].chunks_mut(stride).take(16) {
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);
902 pub type IPred4x4Func = fn(buf: &mut [u8], off: usize, stride: usize, tr: &[u8]);
903 pub type IPred8x8Func = fn(buf: &mut [u8], off: usize, stride: usize);
904 pub type IPred8x8LumaFunc = fn(buf: &mut [u8], stride: usize, ctx: &IPred8Context);
906 pub const IPRED4_DC128: usize = 11;
907 pub const IPRED4_DC_TOP: usize = 10;
908 pub const IPRED4_DC_LEFT: usize = 9;
909 pub const IPRED8_DC128: usize = 6;
910 pub const IPRED8_DC_TOP: usize = 5;
911 pub const IPRED8_DC_LEFT: usize = 4;
913 pub 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
920 pub 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
928 pub 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
933 pub 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
938 fn clip_u8(val: i16) -> u8 { val.max(0).min(255) as u8 }
940 pub 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);
944 let (cw, ch) = refpic.get_dimensions(1);
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();
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])
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])
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);
974 pub 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]);
981 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, coff[chroma - 1], frm.stride[chroma]);
983 } else if w == 8 && h == 8 {
984 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](frm.data, yoff, frm.stride[0]);
986 IPRED_FUNCS4X4[IPRED4_DC128](frm.data, coff[chroma - 1], frm.stride[chroma], &[128; 4]);
989 for row in frm.data[yoff..].chunks_mut(frm.stride[0]).take(h) {
990 for el in row[..w].iter_mut() {
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() {
1004 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>) {
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);
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);
1014 macro_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;
1030 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
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;
1038 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
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;
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));
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));
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]);
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);
1081 fn 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
1089 pub fn loop_filter_lumaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1091 if check_filter(dst, off, 1, alpha, beta) {
1092 loop_filter!(lumaedge; dst, off, 1, alpha, beta);
1097 pub fn loop_filter_lumaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1099 if check_filter(dst, off + x, stride, alpha, beta) {
1100 loop_filter!(lumaedge; dst, off + x, stride, alpha, beta);
1104 pub fn loop_filter_lumanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1106 if check_filter(dst, off, 1, alpha, beta) {
1107 loop_filter!(lumanormal; dst, off, 1, tc0, beta);
1112 pub fn loop_filter_lumanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1114 if check_filter(dst, off + x, stride, alpha, beta) {
1115 loop_filter!(lumanormal; dst, off + x, stride, tc0, beta);
1119 pub fn loop_filter_chromaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1121 if check_filter(dst, off, 1, alpha, beta) {
1122 loop_filter!(chromaedge; dst, off, 1);
1127 pub fn loop_filter_chromaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1129 if check_filter(dst, off + x, stride, alpha, beta) {
1130 loop_filter!(chromaedge; dst, off + x, stride);
1134 pub fn loop_filter_chromanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1136 if check_filter(dst, off, 1, alpha, beta) {
1137 loop_filter!(chromanormal; dst, off, 1, tc0);
1142 pub fn loop_filter_chromanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1144 if check_filter(dst, off + x, stride, alpha, beta) {
1145 loop_filter!(chromanormal; dst, off + x, stride, tc0);