1 use nihav_core::codecs::*;
3 #[derive(Clone,Copy,Debug,PartialEq)]
20 pub fn is_intra(self) -> bool { self == VPMBType::Intra }
21 pub fn get_ref_id(self) -> u8 {
26 VPMBType::InterNearest |
28 VPMBType::InterFourMV => 1,
34 impl Default for VPMBType {
35 fn default() -> Self { VPMBType::Intra }
39 pub struct VPShuffler {
40 lastframe: Option<NAVideoBufferRef<u8>>,
41 goldframe: Option<NAVideoBufferRef<u8>>,
45 pub fn new() -> Self { VPShuffler { lastframe: None, goldframe: None } }
46 pub fn clear(&mut self) { self.lastframe = None; self.goldframe = None; }
47 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
48 self.lastframe = Some(buf);
50 pub fn add_golden_frame(&mut self, buf: NAVideoBufferRef<u8>) {
51 self.goldframe = Some(buf);
53 pub fn get_last(&mut self) -> Option<NAVideoBufferRef<u8>> {
54 if let Some(ref frm) = self.lastframe {
60 pub fn get_golden(&mut self) -> Option<NAVideoBufferRef<u8>> {
61 if let Some(ref frm) = self.goldframe {
69 const C1S7: i32 = 64277;
70 const C2S6: i32 = 60547;
71 const C3S5: i32 = 54491;
72 const C4S4: i32 = 46341;
73 const C5S3: i32 = 36410;
74 const C6S2: i32 = 25080;
75 const C7S1: i32 = 12785;
77 fn mul16(a: i32, b: i32) -> i32 {
81 macro_rules! idct_step {
82 ($s0:expr, $s1:expr, $s2:expr, $s3:expr, $s4:expr, $s5:expr, $s6:expr, $s7:expr,
83 $d0:expr, $d1:expr, $d2:expr, $d3:expr, $d4:expr, $d5:expr, $d6:expr, $d7:expr,
84 $bias:expr, $shift:expr, $otype:ty) => {
85 let t_a = mul16(C1S7, i32::from($s1)) + mul16(C7S1, i32::from($s7));
86 let t_b = mul16(C7S1, i32::from($s1)) - mul16(C1S7, i32::from($s7));
87 let t_c = mul16(C3S5, i32::from($s3)) + mul16(C5S3, i32::from($s5));
88 let t_d = mul16(C3S5, i32::from($s5)) - mul16(C5S3, i32::from($s3));
89 let t_a1 = mul16(C4S4, t_a - t_c);
90 let t_b1 = mul16(C4S4, t_b - t_d);
93 let t_e = mul16(C4S4, i32::from($s0 + $s4)) + $bias;
94 let t_f = mul16(C4S4, i32::from($s0 - $s4)) + $bias;
95 let t_g = mul16(C2S6, i32::from($s2)) + mul16(C6S2, i32::from($s6));
96 let t_h = mul16(C6S2, i32::from($s2)) - mul16(C2S6, i32::from($s6));
100 let t_f = t_f - t_a1;
101 let t_b = t_b1 - t_h;
102 let t_h = t_b1 + t_h;
104 $d0 = ((t_g + t_c) >> $shift) as $otype;
105 $d7 = ((t_g - t_c) >> $shift) as $otype;
106 $d1 = ((t_a + t_h) >> $shift) as $otype;
107 $d2 = ((t_a - t_h) >> $shift) as $otype;
108 $d3 = ((t_e1 + t_d) >> $shift) as $otype;
109 $d4 = ((t_e1 - t_d) >> $shift) as $otype;
110 $d5 = ((t_f + t_b) >> $shift) as $otype;
111 $d6 = ((t_f - t_b) >> $shift) as $otype;
115 pub fn vp_idct(coeffs: &mut [i16; 64]) {
116 let mut tmp = [0i32; 64];
117 for (src, dst) in coeffs.chunks(8).zip(tmp.chunks_mut(8)) {
118 idct_step!(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
119 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7], 0, 0, i32);
124 idct_step!(src[0 * 8 + i], src[1 * 8 + i], src[2 * 8 + i], src[3 * 8 + i],
125 src[4 * 8 + i], src[5 * 8 + i], src[6 * 8 + i], src[7 * 8 + i],
126 dst[0 * 8 + i], dst[1 * 8 + i], dst[2 * 8 + i], dst[3 * 8 + i],
127 dst[4 * 8 + i], dst[5 * 8 + i], dst[6 * 8 + i], dst[7 * 8 + i], 8, 4, i16);
131 pub fn vp_idct_dc(coeffs: &mut [i16; 64]) {
132 let dc = ((mul16(C4S4, mul16(C4S4, i32::from(coeffs[0]))) + 8) >> 4) as i16;
138 pub fn unquant(coeffs: &mut [i16; 64], qmat: &[i16; 64]) {
140 coeffs[i] = coeffs[i].wrapping_mul(qmat[i]);
144 pub fn vp_put_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
146 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
149 frm.data[off + x] = (coeffs[x + y * 8] + 128).min(255).max(0) as u8;
151 off += frm.stride[plane];
155 pub fn vp_put_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
157 let dc = (coeffs[0] + 128).min(255).max(0) as u8;
158 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
161 frm.data[off + x] = dc;
163 off += frm.stride[plane];
167 pub fn vp_add_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
169 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
172 frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
174 off += frm.stride[plane];
178 pub fn vp_add_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
181 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
184 frm.data[off + x] = (dc + (frm.data[off + x] as i16)).min(255).max(0) as u8;
186 off += frm.stride[plane];