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