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