1 use nihav_core::codecs::*;
2 use nihav_core::codecs::blockdsp::*;
4 #[derive(Clone,Copy,Debug,PartialEq)]
19 pub const VP_REF_INTER: u8 = 1;
20 pub const VP_REF_GOLDEN: u8 = 2;
24 pub fn is_intra(self) -> bool { self == VPMBType::Intra }
25 pub fn get_ref_id(self) -> u8 {
30 VPMBType::InterNearest |
32 VPMBType::InterFourMV => VP_REF_INTER,
38 impl Default for VPMBType {
39 fn default() -> Self { VPMBType::Intra }
43 pub struct VPShuffler {
44 lastframe: Option<NAVideoBufferRef<u8>>,
45 goldframe: Option<NAVideoBufferRef<u8>>,
49 pub fn new() -> Self { VPShuffler { lastframe: None, goldframe: None } }
50 pub fn clear(&mut self) { self.lastframe = None; self.goldframe = None; }
51 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
52 self.lastframe = Some(buf);
54 pub fn add_golden_frame(&mut self, buf: NAVideoBufferRef<u8>) {
55 self.goldframe = Some(buf);
57 pub fn get_last(&mut self) -> Option<NAVideoBufferRef<u8>> {
58 if let Some(ref frm) = self.lastframe {
64 pub fn get_golden(&mut self) -> Option<NAVideoBufferRef<u8>> {
65 if let Some(ref frm) = self.goldframe {
71 pub fn has_refs(&self) -> bool {
72 self.lastframe.is_some()
76 pub const VP56_COEF_BASE: [i16; 6] = [ 5, 7, 11, 19, 35, 67 ];
77 pub const VP56_COEF_ADD_PROBS: [[u8; 12]; 6] = [
78 [ 159, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
79 [ 165, 145, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
80 [ 173, 148, 140, 128, 0, 0, 0, 0, 0, 0, 0, 0 ],
81 [ 176, 155, 140, 135, 128, 0, 0, 0, 0, 0, 0, 0 ],
82 [ 180, 157, 141, 134, 130, 128, 0, 0, 0, 0, 0, 0 ],
83 [ 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 128 ],
87 pub struct BoolCoder<'a> {
96 impl<'a> BoolCoder<'a> {
97 pub fn new(src: &'a [u8]) -> DecoderResult<Self> {
98 if src.len() < 3 { return Err(DecoderError::ShortData); }
99 let value = ((src[0] as u32) << 24) | ((src[1] as u32) << 16) | ((src[2] as u32) << 8) | (src[3] as u32);
100 Ok(Self { src, pos: 4, value, range: 255, bits: 8 })
102 pub fn read_bool(&mut self) -> bool {
105 pub fn read_prob(&mut self, prob: u8) -> bool {
107 let split = 1 + (((self.range - 1) * (prob as u32)) >> 8);
109 if self.value < (split << 24) {
114 self.value -= split << 24;
119 pub fn read_bits(&mut self, bits: u8) -> u32 {
122 val = (val << 1) | (self.read_prob(128) as u32);
126 pub fn read_byte(&mut self) -> u8 {
129 val = (val << 1) | (self.read_prob(128) as u8);
133 pub fn read_sbits(&mut self, bits: u8) -> i32 {
134 let mut val = if self.read_prob(128) { -1i32 } else { 0i32 };
136 val = (val << 1) | (self.read_prob(128) as i32);
140 pub fn read_probability(&mut self) -> u8 {
141 let val = self.read_bits(7) as u8;
148 fn renorm(&mut self) {
149 let shift = self.range.leading_zeros() & 7;
150 self.range <<= shift;
151 self.value <<= shift;
152 self.bits -= shift as i32;
153 if (self.bits <= 0) && (self.pos < self.src.len()) {
154 self.value |= (self.src[self.pos] as u32) << (-self.bits as u8);
158 /* while self.range < 0x80 {
162 if (self.bits <= 0) && (self.pos < self.src.len()) {
163 self.value |= self.src[self.pos] as u32;
169 pub fn skip_bytes(&mut self, nbytes: usize) {
172 if self.pos < self.src.len() {
173 self.value |= self.src[self.pos] as u32;
181 pub fn rescale_prob(prob: u8, weights: &[i16; 2], maxval: i32) -> u8 {
182 ((((prob as i32) * (weights[0] as i32) + 128) >> 8) + (weights[1] as i32)).min(maxval).max(1) as u8
186 macro_rules! vp_tree {
187 ($bc: expr, $prob: expr, $node1: expr, $node2: expr) => {
188 if !$bc.read_prob($prob) {
194 ($leaf: expr) => { $leaf }
197 const C1S7: i32 = 64277;
198 const C2S6: i32 = 60547;
199 const C3S5: i32 = 54491;
200 const C4S4: i32 = 46341;
201 const C5S3: i32 = 36410;
202 const C6S2: i32 = 25080;
203 const C7S1: i32 = 12785;
205 fn mul16(a: i32, b: i32) -> i32 {
209 macro_rules! idct_step {
210 ($s0:expr, $s1:expr, $s2:expr, $s3:expr, $s4:expr, $s5:expr, $s6:expr, $s7:expr,
211 $d0:expr, $d1:expr, $d2:expr, $d3:expr, $d4:expr, $d5:expr, $d6:expr, $d7:expr,
212 $bias:expr, $shift:expr, $otype:ty) => {
213 let t_a = mul16(C1S7, i32::from($s1)) + mul16(C7S1, i32::from($s7));
214 let t_b = mul16(C7S1, i32::from($s1)) - mul16(C1S7, i32::from($s7));
215 let t_c = mul16(C3S5, i32::from($s3)) + mul16(C5S3, i32::from($s5));
216 let t_d = mul16(C3S5, i32::from($s5)) - mul16(C5S3, i32::from($s3));
217 let t_a1 = mul16(C4S4, t_a - t_c);
218 let t_b1 = mul16(C4S4, t_b - t_d);
221 let t_e = mul16(C4S4, i32::from($s0 + $s4)) + $bias;
222 let t_f = mul16(C4S4, i32::from($s0 - $s4)) + $bias;
223 let t_g = mul16(C2S6, i32::from($s2)) + mul16(C6S2, i32::from($s6));
224 let t_h = mul16(C6S2, i32::from($s2)) - mul16(C2S6, i32::from($s6));
225 let t_e1 = t_e - t_g;
227 let t_a = t_f + t_a1;
228 let t_f = t_f - t_a1;
229 let t_b = t_b1 - t_h;
230 let t_h = t_b1 + t_h;
232 $d0 = ((t_g + t_c) >> $shift) as $otype;
233 $d7 = ((t_g - t_c) >> $shift) as $otype;
234 $d1 = ((t_a + t_h) >> $shift) as $otype;
235 $d2 = ((t_a - t_h) >> $shift) as $otype;
236 $d3 = ((t_e1 + t_d) >> $shift) as $otype;
237 $d4 = ((t_e1 - t_d) >> $shift) as $otype;
238 $d5 = ((t_f + t_b) >> $shift) as $otype;
239 $d6 = ((t_f - t_b) >> $shift) as $otype;
243 pub fn vp_idct(coeffs: &mut [i16; 64]) {
244 let mut tmp = [0i32; 64];
245 for (src, dst) in coeffs.chunks(8).zip(tmp.chunks_mut(8)) {
246 idct_step!(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
247 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7], 0, 0, i32);
252 idct_step!(src[0 * 8 + i], src[1 * 8 + i], src[2 * 8 + i], src[3 * 8 + i],
253 src[4 * 8 + i], src[5 * 8 + i], src[6 * 8 + i], src[7 * 8 + i],
254 dst[0 * 8 + i], dst[1 * 8 + i], dst[2 * 8 + i], dst[3 * 8 + i],
255 dst[4 * 8 + i], dst[5 * 8 + i], dst[6 * 8 + i], dst[7 * 8 + i], 8, 4, i16);
259 pub fn vp_idct_dc(coeffs: &mut [i16; 64]) {
260 let dc = ((mul16(C4S4, mul16(C4S4, i32::from(coeffs[0]))) + 8) >> 4) as i16;
266 pub fn unquant(coeffs: &mut [i16; 64], qmat: &[i16; 64]) {
268 coeffs[i] = coeffs[i].wrapping_mul(qmat[i]);
272 pub fn vp_put_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
274 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
277 frm.data[off + x] = (coeffs[x + y * 8] + 128).min(255).max(0) as u8;
279 off += frm.stride[plane];
283 pub fn vp_put_block_ilace(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
285 let mut off = frm.offset[plane] + bx * 8 + ((by & !1) * 8 + (by & 1)) * frm.stride[plane];
288 frm.data[off + x] = (coeffs[x + y * 8] + 128).min(255).max(0) as u8;
290 off += frm.stride[plane] * 2;
294 pub fn vp_put_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
296 let dc = (coeffs[0] + 128).min(255).max(0) as u8;
297 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
300 frm.data[off + x] = dc;
302 off += frm.stride[plane];
306 pub fn vp_add_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
308 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
311 frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
313 off += frm.stride[plane];
317 pub fn vp_add_block_ilace(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
319 let mut off = frm.offset[plane] + bx * 8 + ((by & !1) * 8 + (by & 1)) * frm.stride[plane];
322 frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
324 off += frm.stride[plane] * 2;
328 pub fn vp_add_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
331 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
334 frm.data[off + x] = (dc + (frm.data[off + x] as i16)).min(255).max(0) as u8;
336 off += frm.stride[plane];
340 pub fn vp31_loop_filter(data: &mut [u8], mut off: usize, step: usize, stride: usize,
341 len: usize, loop_str: i16) {
343 let a = data[off - step * 2] as i16;
344 let b = data[off - step] as i16;
345 let c = data[off] as i16;
346 let d = data[off + step] as i16;
347 let mut diff = ((a - d) + 3 * (c - b) + 4) >> 3;
348 if diff.abs() >= 2 * loop_str {
350 } else if diff.abs() >= loop_str {
352 diff = -diff - 2 * loop_str;
354 diff = -diff + 2 * loop_str;
358 data[off - step] = (b + diff).max(0).min(255) as u8;
359 data[off] = (c - diff).max(0).min(255) as u8;
366 pub fn vp_copy_block(dst: &mut NASimpleVideoFrame<u8>, src: NAVideoBufferRef<u8>, comp: usize,
367 dx: usize, dy: usize, mv_x: i16, mv_y: i16,
368 preborder: usize, postborder: usize, loop_str: i16,
369 mode: usize, interp: &[BlkInterpFunc], mut mc_buf: NAVideoBufferRef<u8>)
371 let sx = (dx as isize) + (mv_x as isize);
372 let sy = (dy as isize) + (mv_y as isize);
373 if ((sx | sy) & 7) == 0 {
374 copy_block(dst, src, comp, dx, dy, mv_x, mv_y, 8, 8, preborder, postborder, mode, interp);
377 let pre = preborder.max(2);
378 let post = postborder.max(1);
379 let bsize = 8 + pre + post;
380 let src_x = sx - (pre as isize);
381 let src_y = sy - (pre as isize);
383 let tmp_buf = NASimpleVideoFrame::from_video_buf(&mut mc_buf).unwrap();
384 edge_emu(src.as_ref(), src_x, src_y, bsize, bsize, &mut tmp_buf.data[tmp_buf.offset[comp]..], tmp_buf.stride[comp], comp);
385 // copy_block(&mut tmp_buf, src, comp, 0, 0, src_x as i16, src_y as i16,
386 // bsize, bsize, 0, 0, 0, interp);
388 let foff = (8 - (sx & 7)) as usize;
389 let off = pre + foff + tmp_buf.offset[comp];
390 vp31_loop_filter(tmp_buf.data, off, 1, tmp_buf.stride[comp], bsize, loop_str);
393 let foff = (8 - (sy & 7)) as usize;
394 let off = (pre + foff) * tmp_buf.stride[comp] + tmp_buf.offset[comp];
395 vp31_loop_filter(tmp_buf.data, off, tmp_buf.stride[comp], 1, bsize, loop_str);
398 let dxoff = (pre as i16) - (dx as i16);
399 let dyoff = (pre as i16) - (dy as i16);
400 copy_block(dst, mc_buf, comp, dx, dy, dxoff, dyoff, 8, 8, preborder, postborder, mode, interp);
403 fn vp3_interp00(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
408 for x in 0..bw { dst[didx + x] = src[sidx + x]; }
414 fn vp3_interp01(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
419 for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + 1] as u16)) >> 1) as u8; }
425 fn vp3_interp10(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
430 for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + sstride] as u16)) >> 1) as u8; }
436 fn vp3_interp1x(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
442 dst[didx + x] = (((src[sidx + x] as u16) +
443 (src[sidx + x + sstride + 1] as u16)) >> 1) as u8;
450 fn vp3_interp1y(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
456 dst[didx + x] = (((src[sidx + x + 1] as u16) +
457 (src[sidx + x + sstride] as u16)) >> 1) as u8;
464 pub const VP3_INTERP_FUNCS: &[blockdsp::BlkInterpFunc] = &[ vp3_interp00, vp3_interp01, vp3_interp10, vp3_interp1x, vp3_interp1y ];