]> git.nihav.org Git - nihav.git/blame - nihav-duck/src/codecs/vp6dsp.rs
add IVF demuxer for VP8 test samples
[nihav.git] / nihav-duck / src / codecs / vp6dsp.rs
CommitLineData
3952bfd9
KS
1use nihav_core::frame::*;
2use nihav_codec_support::codecs::blockdsp::edge_emu;
3
4#[allow(clippy::too_many_arguments)]
5pub fn get_block(dst: &mut [u8], dstride: usize, src: NAVideoBufferRef<u8>, comp: usize,
6 dx: usize, dy: usize, mv_x: i16, mv_y: i16)
7{
8 let (w, h) = src.get_dimensions(comp);
9 let sx = (dx as isize) + (mv_x as isize);
10 let sy = (dy as isize) + (mv_y as isize);
11
12 if (sx - 2 < 0) || (sx + 8 + 2 > (w as isize)) ||
13 (sy - 2 < 0) || (sy + 8 + 2 > (h as isize)) {
14 edge_emu(&src, sx - 2, sy - 2, 8 + 2 + 2, 8 + 2 + 2,
15 dst, dstride, comp, 0);
16 } else {
17 let sstride = src.get_stride(comp);
18 let soff = src.get_offset(comp);
19 let sdta = src.get_data();
20 let sbuf: &[u8] = sdta.as_slice();
21 let saddr = soff + ((sx - 2) as usize) + ((sy - 2) as usize) * sstride;
22 let src = &sbuf[saddr..];
23 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(12) {
24 dline[..12].copy_from_slice(&sline[..12]);
25 }
26 }
27}
28
29pub fn calc_variance(src: &[u8], stride: usize) -> u16 {
30 let mut sum = 0;
31 let mut ssum = 0;
32 for line in src.chunks(stride * 2).take(4) {
33 for el in line.iter().take(8).step_by(2) {
34 let pix = u32::from(*el);
35 sum += pix;
36 ssum += pix * pix;
37 }
38 }
39 ((ssum * 16 - sum * sum) >> 8) as u16
40}
41
42macro_rules! mc_filter {
43 (bilinear; $a: expr, $b: expr, $c: expr) => {
44 ((u16::from($a) * (8 - $c) + u16::from($b) * $c + 4) >> 3) as u8
45 };
46 (bicubic; $src: expr, $off: expr, $step: expr, $coeffs: expr) => {
47 ((i32::from($src[$off - $step] ) * i32::from($coeffs[0]) +
48 i32::from($src[$off] ) * i32::from($coeffs[1]) +
49 i32::from($src[$off + $step] ) * i32::from($coeffs[2]) +
50 i32::from($src[$off + $step * 2]) * i32::from($coeffs[3]) + 64) >> 7).min(255).max(0) as u8
51 }
52}
53
54//#[allow(snake_case)]
55pub fn mc_bilinear(dst: &mut [u8], dstride: usize, src: &[u8], mut soff: usize, sstride: usize, mx: u16, my: u16) {
56 if my == 0 {
57 for dline in dst.chunks_mut(dstride).take(8) {
58 for i in 0..8 {
59 dline[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
60 }
61 soff += sstride;
62 }
63 } else if mx == 0 {
64 for dline in dst.chunks_mut(dstride).take(8) {
65 for i in 0..8 {
66 dline[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + sstride], my);
67 }
68 soff += sstride;
69 }
70 } else {
71 let mut tmp = [0u8; 8];
72 for i in 0..8 {
73 tmp[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
74 }
75 soff += sstride;
76 for dline in dst.chunks_mut(dstride).take(8) {
77 for i in 0..8 {
78 let cur = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
79 dline[i] = mc_filter!(bilinear; tmp[i], cur, my);
80 tmp[i] = cur;
81 }
82 soff += sstride;
83 }
84 }
85}
86
87#[allow(clippy::trivially_copy_pass_by_ref)]
88pub fn mc_bicubic(dst: &mut [u8], dstride: usize, src: &[u8], mut soff: usize, sstride: usize, coeffs_w: &[i16; 4], coeffs_h: &[i16; 4]) {
89 if coeffs_h[1] == 128 {
90 for dline in dst.chunks_mut(dstride).take(8) {
91 for i in 0..8 {
92 dline[i] = mc_filter!(bicubic; src, soff + i, 1, coeffs_w);
93 }
94 soff += sstride;
95 }
96 } else if coeffs_w[1] == 128 { // horizontal-only interpolation
97 for dline in dst.chunks_mut(dstride).take(8) {
98 for i in 0..8 {
99 dline[i] = mc_filter!(bicubic; src, soff + i, sstride, coeffs_h);
100 }
101 soff += sstride;
102 }
103 } else {
104 let mut buf = [0u8; 16 * 11];
105 soff -= sstride;
106 for dline in buf.chunks_mut(16) {
107 for i in 0..8 {
108 dline[i] = mc_filter!(bicubic; src, soff + i, 1, coeffs_w);
109 }
110 soff += sstride;
111 }
112 let mut soff = 16;
113 for dline in dst.chunks_mut(dstride).take(8) {
114 for i in 0..8 {
115 dline[i] = mc_filter!(bicubic; buf, soff + i, 16, coeffs_h);
116 }
117 soff += 16;
118 }
119 }
120}