vp56: move DCT coeffs base and probabilities to common
[nihav.git] / nihav-duck / src / codecs / vpcommon.rs
CommitLineData
5b24175d 1use nihav_core::codecs::*;
8e4b2f44 2use nihav_core::codecs::blockdsp::*;
5b24175d
KS
3
4#[derive(Clone,Copy,Debug,PartialEq)]
5#[allow(dead_code)]
6pub enum VPMBType {
7 Intra,
8 InterNoMV,
9 InterMV,
10 InterNearest,
11 InterNear,
12 InterFourMV,
13 GoldenNoMV,
14 GoldenMV,
15 GoldenNearest,
16 GoldenNear,
17}
18
3584b223
KS
19pub const VP_REF_INTER: u8 = 1;
20pub const VP_REF_GOLDEN: u8 = 2;
21
5b24175d
KS
22#[allow(dead_code)]
23impl VPMBType {
24 pub fn is_intra(self) -> bool { self == VPMBType::Intra }
25 pub fn get_ref_id(self) -> u8 {
26 match self {
27 VPMBType::Intra => 0,
28 VPMBType::InterNoMV |
29 VPMBType::InterMV |
30 VPMBType::InterNearest |
31 VPMBType::InterNear |
3584b223
KS
32 VPMBType::InterFourMV => VP_REF_INTER,
33 _ => VP_REF_GOLDEN,
5b24175d
KS
34 }
35 }
36}
37
38impl Default for VPMBType {
39 fn default() -> Self { VPMBType::Intra }
40}
41
42#[derive(Default)]
43pub struct VPShuffler {
44 lastframe: Option<NAVideoBufferRef<u8>>,
45 goldframe: Option<NAVideoBufferRef<u8>>,
46}
47
48impl VPShuffler {
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);
53 }
54 pub fn add_golden_frame(&mut self, buf: NAVideoBufferRef<u8>) {
55 self.goldframe = Some(buf);
56 }
57 pub fn get_last(&mut self) -> Option<NAVideoBufferRef<u8>> {
58 if let Some(ref frm) = self.lastframe {
59 Some(frm.clone())
60 } else {
61 None
62 }
63 }
64 pub fn get_golden(&mut self) -> Option<NAVideoBufferRef<u8>> {
65 if let Some(ref frm) = self.goldframe {
66 Some(frm.clone())
67 } else {
68 None
69 }
70 }
71}
72
3f67638d
KS
73pub const VP56_COEF_BASE: [i16; 6] = [ 5, 7, 11, 19, 35, 67 ];
74pub const VP56_COEF_ADD_PROBS: [[u8; 12]; 6] = [
75 [ 159, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
76 [ 165, 145, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
77 [ 173, 148, 140, 128, 0, 0, 0, 0, 0, 0, 0, 0 ],
78 [ 176, 155, 140, 135, 128, 0, 0, 0, 0, 0, 0, 0 ],
79 [ 180, 157, 141, 134, 130, 128, 0, 0, 0, 0, 0, 0 ],
80 [ 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 128 ],
81];
82
3584b223
KS
83#[allow(dead_code)]
84pub struct BoolCoder<'a> {
85 pub src: &'a [u8],
86 pos: usize,
87 value: u32,
88 range: u32,
89 bits: i32,
90}
91
92#[allow(dead_code)]
93impl<'a> BoolCoder<'a> {
94 pub fn new(src: &'a [u8]) -> DecoderResult<Self> {
95 if src.len() < 3 { return Err(DecoderError::ShortData); }
96 let value = ((src[0] as u32) << 24) | ((src[1] as u32) << 16) | ((src[2] as u32) << 8) | (src[3] as u32);
97 Ok(Self { src, pos: 4, value, range: 255, bits: 8 })
98 }
99 pub fn read_bool(&mut self) -> bool {
100 self.read_prob(128)
101 }
102 pub fn read_prob(&mut self, prob: u8) -> bool {
103 self.renorm();
104 let split = 1 + (((self.range - 1) * (prob as u32)) >> 8);
105 let bit;
106 if self.value < (split << 24) {
107 self.range = split;
108 bit = false;
109 } else {
110 self.range -= split;
111 self.value -= split << 24;
112 bit = true;
113 }
114 bit
115 }
116 pub fn read_bits(&mut self, bits: u8) -> u32 {
117 let mut val = 0u32;
118 for _ in 0..bits {
119 val = (val << 1) | (self.read_prob(128) as u32);
120 }
121 val
122 }
123 pub fn read_probability(&mut self) -> u8 {
124 let val = self.read_bits(7) as u8;
125 if val == 0 {
126 1
127 } else {
128 val << 1
129 }
130 }
131 fn renorm(&mut self) {
132 let shift = self.range.leading_zeros() & 7;
133 self.range <<= shift;
134 self.value <<= shift;
135 self.bits -= shift as i32;
136 if (self.bits <= 0) && (self.pos < self.src.len()) {
137 self.value |= (self.src[self.pos] as u32) << (-self.bits as u8);
138 self.pos += 1;
139 self.bits += 8;
140 }
141/* while self.range < 0x80 {
142 self.range <<= 1;
143 self.value <<= 1;
144 self.bits -= 1;
145 if (self.bits <= 0) && (self.pos < self.src.len()) {
146 self.value |= self.src[self.pos] as u32;
147 self.pos += 1;
148 self.bits = 8;
149 }
150 }*/
151 }
152 pub fn skip_bytes(&mut self, nbytes: usize) {
153 for _ in 0..nbytes {
154 self.value <<= 8;
155 if self.pos < self.src.len() {
156 self.value |= self.src[self.pos] as u32;
157 self.pos += 1;
158 }
159 }
160 }
161}
162
163#[allow(dead_code)]
164pub fn rescale_prob(prob: u8, weights: &[i16; 2], maxval: i32) -> u8 {
165 ((((prob as i32) * (weights[0] as i32) + 128) >> 8) + (weights[1] as i32)).min(maxval).max(1) as u8
166}
167
168#[macro_export]
169macro_rules! vp_tree {
170 ($bc: expr, $prob: expr, $node1: expr, $node2: expr) => {
171 if !$bc.read_prob($prob) {
172 $node1
173 } else {
174 $node2
175 }
176 };
177 ($leaf: expr) => { $leaf }
178}
179
5b24175d
KS
180const C1S7: i32 = 64277;
181const C2S6: i32 = 60547;
182const C3S5: i32 = 54491;
183const C4S4: i32 = 46341;
184const C5S3: i32 = 36410;
185const C6S2: i32 = 25080;
186const C7S1: i32 = 12785;
187
188fn mul16(a: i32, b: i32) -> i32 {
189 (a * b) >> 16
190}
191
192macro_rules! idct_step {
193 ($s0:expr, $s1:expr, $s2:expr, $s3:expr, $s4:expr, $s5:expr, $s6:expr, $s7:expr,
194 $d0:expr, $d1:expr, $d2:expr, $d3:expr, $d4:expr, $d5:expr, $d6:expr, $d7:expr,
195 $bias:expr, $shift:expr, $otype:ty) => {
196 let t_a = mul16(C1S7, i32::from($s1)) + mul16(C7S1, i32::from($s7));
197 let t_b = mul16(C7S1, i32::from($s1)) - mul16(C1S7, i32::from($s7));
198 let t_c = mul16(C3S5, i32::from($s3)) + mul16(C5S3, i32::from($s5));
199 let t_d = mul16(C3S5, i32::from($s5)) - mul16(C5S3, i32::from($s3));
200 let t_a1 = mul16(C4S4, t_a - t_c);
201 let t_b1 = mul16(C4S4, t_b - t_d);
202 let t_c = t_a + t_c;
203 let t_d = t_b + t_d;
204 let t_e = mul16(C4S4, i32::from($s0 + $s4)) + $bias;
205 let t_f = mul16(C4S4, i32::from($s0 - $s4)) + $bias;
206 let t_g = mul16(C2S6, i32::from($s2)) + mul16(C6S2, i32::from($s6));
207 let t_h = mul16(C6S2, i32::from($s2)) - mul16(C2S6, i32::from($s6));
208 let t_e1 = t_e - t_g;
209 let t_g = t_e + t_g;
210 let t_a = t_f + t_a1;
211 let t_f = t_f - t_a1;
212 let t_b = t_b1 - t_h;
213 let t_h = t_b1 + t_h;
214
215 $d0 = ((t_g + t_c) >> $shift) as $otype;
216 $d7 = ((t_g - t_c) >> $shift) as $otype;
217 $d1 = ((t_a + t_h) >> $shift) as $otype;
218 $d2 = ((t_a - t_h) >> $shift) as $otype;
219 $d3 = ((t_e1 + t_d) >> $shift) as $otype;
220 $d4 = ((t_e1 - t_d) >> $shift) as $otype;
221 $d5 = ((t_f + t_b) >> $shift) as $otype;
222 $d6 = ((t_f - t_b) >> $shift) as $otype;
223 }
224}
225
226pub fn vp_idct(coeffs: &mut [i16; 64]) {
227 let mut tmp = [0i32; 64];
228 for (src, dst) in coeffs.chunks(8).zip(tmp.chunks_mut(8)) {
229 idct_step!(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
230 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7], 0, 0, i32);
231 }
232 let src = &tmp;
233 let dst = coeffs;
234 for i in 0..8 {
235 idct_step!(src[0 * 8 + i], src[1 * 8 + i], src[2 * 8 + i], src[3 * 8 + i],
236 src[4 * 8 + i], src[5 * 8 + i], src[6 * 8 + i], src[7 * 8 + i],
237 dst[0 * 8 + i], dst[1 * 8 + i], dst[2 * 8 + i], dst[3 * 8 + i],
238 dst[4 * 8 + i], dst[5 * 8 + i], dst[6 * 8 + i], dst[7 * 8 + i], 8, 4, i16);
239 }
240}
241
242pub fn vp_idct_dc(coeffs: &mut [i16; 64]) {
243 let dc = ((mul16(C4S4, mul16(C4S4, i32::from(coeffs[0]))) + 8) >> 4) as i16;
244 for i in 0..64 {
245 coeffs[i] = dc;
246 }
247}
248
249pub fn unquant(coeffs: &mut [i16; 64], qmat: &[i16; 64]) {
250 for i in 1..64 {
251 coeffs[i] = coeffs[i].wrapping_mul(qmat[i]);
252 }
253}
254
255pub fn vp_put_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
256 vp_idct(coeffs);
257 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
258 for y in 0..8 {
259 for x in 0..8 {
260 frm.data[off + x] = (coeffs[x + y * 8] + 128).min(255).max(0) as u8;
261 }
262 off += frm.stride[plane];
263 }
264}
265
3584b223
KS
266pub fn vp_put_block_ilace(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
267 vp_idct(coeffs);
268 let mut off = frm.offset[plane] + bx * 8 + ((by & !1) * 8 + (by & 1)) * frm.stride[plane];
269 for y in 0..8 {
270 for x in 0..8 {
271 frm.data[off + x] = (coeffs[x + y * 8] + 128).min(255).max(0) as u8;
272 }
273 off += frm.stride[plane] * 2;
274 }
275}
276
5b24175d
KS
277pub fn vp_put_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
278 vp_idct_dc(coeffs);
279 let dc = (coeffs[0] + 128).min(255).max(0) as u8;
280 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
281 for _ in 0..8 {
282 for x in 0..8 {
283 frm.data[off + x] = dc;
284 }
285 off += frm.stride[plane];
286 }
287}
288
289pub fn vp_add_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
290 vp_idct(coeffs);
291 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
292 for y in 0..8 {
293 for x in 0..8 {
294 frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
295 }
296 off += frm.stride[plane];
297 }
298}
299
3584b223
KS
300pub fn vp_add_block_ilace(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
301 vp_idct(coeffs);
302 let mut off = frm.offset[plane] + bx * 8 + ((by & !1) * 8 + (by & 1)) * frm.stride[plane];
303 for y in 0..8 {
304 for x in 0..8 {
305 frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
306 }
307 off += frm.stride[plane] * 2;
308 }
309}
310
5b24175d
KS
311pub fn vp_add_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize, frm: &mut NASimpleVideoFrame<u8>) {
312 vp_idct_dc(coeffs);
313 let dc = coeffs[0];
314 let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
315 for _ in 0..8 {
316 for x in 0..8 {
317 frm.data[off + x] = (dc + (frm.data[off + x] as i16)).min(255).max(0) as u8;
318 }
319 off += frm.stride[plane];
320 }
321}
8d8ddfe1
KS
322
323pub fn vp31_loop_filter(data: &mut [u8], mut off: usize, step: usize, stride: usize,
324 len: usize, loop_str: i16) {
325 for _ in 0..len {
326 let a = data[off - step * 2] as i16;
327 let b = data[off - step] as i16;
328 let c = data[off] as i16;
329 let d = data[off + step] as i16;
330 let mut diff = ((a - d) + 3 * (c - b) + 4) >> 3;
331 if diff.abs() >= 2 * loop_str {
332 diff = 0;
333 } else if diff.abs() >= loop_str {
334 if diff < 0 {
335 diff = -diff - 2 * loop_str;
336 } else {
337 diff = -diff + 2 * loop_str;
338 }
339 }
340 if diff != 0 {
341 data[off - step] = (b + diff).max(0).min(255) as u8;
342 data[off] = (c - diff).max(0).min(255) as u8;
343 }
344
345 off += stride;
346 }
347}
348
8e4b2f44
KS
349pub fn vp_copy_block(dst: &mut NASimpleVideoFrame<u8>, src: NAVideoBufferRef<u8>, comp: usize,
350 dx: usize, dy: usize, mv_x: i16, mv_y: i16,
351 preborder: usize, postborder: usize, loop_str: i16,
352 mode: usize, interp: &[BlkInterpFunc], mut mc_buf: NAVideoBufferRef<u8>)
353{
354 let sx = (dx as isize) + (mv_x as isize);
355 let sy = (dy as isize) + (mv_y as isize);
356 if ((sx | sy) & 7) == 0 {
357 copy_block(dst, src, comp, dx, dy, mv_x, mv_y, 8, 8, preborder, postborder, mode, interp);
358 return;
359 }
360 let pre = preborder.max(2);
361 let post = postborder.max(1);
362 let bsize = 8 + pre + post;
363 let src_x = sx - (pre as isize);
364 let src_y = sy - (pre as isize);
365 {
366 let mut tmp_buf = NASimpleVideoFrame::from_video_buf(&mut mc_buf).unwrap();
367 copy_block(&mut tmp_buf, src, comp, 0, 0, src_x as i16, src_y as i16,
368 bsize, bsize, 0, 0, 0, interp);
369 if (sy & 7) != 0 {
370 let foff = (8 - (sy & 7)) as usize;
371 let off = (pre + foff) * tmp_buf.stride[comp];
372 vp31_loop_filter(tmp_buf.data, off, tmp_buf.stride[comp], 1, bsize, loop_str);
373 }
374 if (sx & 7) != 0 {
375 let foff = (8 - (sx & 7)) as usize;
376 let off = pre + foff;
377 vp31_loop_filter(tmp_buf.data, off, 1, tmp_buf.stride[comp], bsize, loop_str);
378 }
379 }
380 let dxoff = (pre as i16) - (dx as i16);
381 let dyoff = (pre as i16) - (dy as i16);
382 copy_block(dst, mc_buf, comp, dx, dy, dxoff, dyoff, 8, 8, preborder, postborder, 0/* mode*/, interp);
383}
3584b223
KS
384
385fn vp3_interp00(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
386{
387 let mut didx = 0;
388 let mut sidx = 0;
389 for _ in 0..bh {
390 for x in 0..bw { dst[didx + x] = src[sidx + x]; }
391 didx += dstride;
392 sidx += sstride;
393 }
394}
395
396fn vp3_interp01(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
397{
398 let mut didx = 0;
399 let mut sidx = 0;
400 for _ in 0..bh {
401 for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + 1] as u16)) >> 1) as u8; }
402 didx += dstride;
403 sidx += sstride;
404 }
405}
406
407fn vp3_interp10(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
408{
409 let mut didx = 0;
410 let mut sidx = 0;
411 for _ in 0..bh {
412 for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + sstride] as u16)) >> 1) as u8; }
413 didx += dstride;
414 sidx += sstride;
415 }
416}
417
418fn vp3_interp11(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
419{
420 let mut didx = 0;
421 let mut sidx = 0;
422 for _ in 0..bh {
423 for x in 0..bw {
424 dst[didx + x] = (((src[sidx + x] as u16) +
425 (src[sidx + x + 1] as u16) +
426 (src[sidx + x + sstride] as u16) +
427 (src[sidx + x + sstride + 1] as u16)) >> 2) as u8;
428 }
429 didx += dstride;
430 sidx += sstride;
431 }
432}
433
434pub const VP3_INTERP_FUNCS: &[blockdsp::BlkInterpFunc] = &[ vp3_interp00, vp3_interp01, vp3_interp10, vp3_interp11 ];
435