vp6: split data into separate module
[nihav.git] / nihav-duck / src / codecs / vp6.rs
CommitLineData
3584b223
KS
1use nihav_core::codecs::*;
2use nihav_core::io::bitreader::*;
b4d5b851
KS
3use nihav_codec_support::codecs::{MV, ZIGZAG};
4use nihav_codec_support::codecs::blockdsp::edge_emu;
3584b223
KS
5use super::vpcommon::*;
6use super::vp56::*;
01551716 7use super::vp6data::*;
3584b223
KS
8
9#[derive(Default)]
10struct VP6BR {
11 vpversion: u8,
12 profile: u8,
13 interlaced: bool,
14 do_pm: bool,
15 loop_mode: u8,
16 autosel_pm: bool,
17 var_thresh: u16,
18 mv_thresh: u8,
19 bicubic: bool,
20 filter_alpha: usize,
21}
22
23impl VP6BR {
24 fn new() -> Self {
25 Self::default()
26 }
27}
28
29impl VP56Parser for VP6BR {
30 fn parse_header(&mut self, bc: &mut BoolCoder) -> DecoderResult<VP56Header> {
31 let mut hdr = VP56Header::default();
32// horrible hack to match VP6 header parsing
33 let src = bc.src;
fa90ccfb 34 let mut br = BitReader::new(src, BitReaderMode::BE);
3584b223
KS
35
36 hdr.is_intra = !br.read_bool()?;
37 hdr.is_golden = hdr.is_intra;
38 hdr.quant = br.read(6)? as u8;
39 hdr.multistream = br.read_bool()?;
40 if hdr.is_intra {
41 hdr.version = br.read(5)? as u8;
42 validate!((hdr.version >= VERSION_VP60) && (hdr.version <= VERSION_VP62));
43 hdr.profile = br.read(2)? as u8;
44 validate!((hdr.profile == VP6_SIMPLE_PROFILE) || (hdr.profile == VP6_ADVANCED_PROFILE));
45 hdr.interlaced = br.read_bool()?;
46 } else {
47 hdr.version = self.vpversion;
48 hdr.profile = self.profile;
adb9c3d4 49 hdr.interlaced = self.interlaced;
3584b223
KS
50 }
51 if hdr.multistream || (hdr.profile == VP6_SIMPLE_PROFILE) {
52 hdr.offset = br.read(16)? as u16;
53 validate!(hdr.offset > if hdr.is_intra { 6 } else { 2 });
54 }
55 let bytes = br.tell() >> 3;
56 std::mem::drop(br);
57 bc.skip_bytes(bytes);
58 self.loop_mode = 0;
59 if hdr.is_intra {
60 hdr.mb_h = bc.read_bits(8) as u8;
61 hdr.mb_w = bc.read_bits(8) as u8;
62 hdr.disp_h = bc.read_bits(8) as u8;
63 hdr.disp_w = bc.read_bits(8) as u8;
64 validate!((hdr.mb_h > 0) && (hdr.mb_w > 0) && (hdr.disp_w > 0) && (hdr.disp_h > 0));
65 validate!((hdr.disp_w <= hdr.mb_w) && (hdr.disp_h <= hdr.mb_h));
66 hdr.scale = bc.read_bits(2) as u8;
67 } else {
68 hdr.is_golden = bc.read_bool();
69 if hdr.profile == VP6_ADVANCED_PROFILE {
70 self.loop_mode = bc.read_bool() as u8;
71 if self.loop_mode != 0 {
72 self.loop_mode += bc.read_bool() as u8;
73 validate!(self.loop_mode <= 1);
74 }
75 if hdr.version == VERSION_VP62 {
76 self.do_pm = bc.read_bool();
77 }
78 }
79 }
80
81 if (hdr.profile == VP6_ADVANCED_PROFILE) && (hdr.is_intra || self.do_pm) {
82 self.autosel_pm = bc.read_bool();
83 if self.autosel_pm {
84 self.var_thresh = bc.read_bits(5) as u16;
85 if hdr.version != VERSION_VP62 {
86 self.var_thresh <<= 5;
87 }
88 self.mv_thresh = bc.read_bits(3) as u8;
89 } else {
90 self.bicubic = bc.read_bool();
91 }
92 if hdr.version == VERSION_VP62 {
93 self.filter_alpha = bc.read_bits(4) as usize;
94 } else {
95 self.filter_alpha = 16;
96 }
97 }
98
99 hdr.use_huffman = bc.read_bool();
100
101 self.vpversion = hdr.version;
102 self.profile = hdr.profile;
103 self.interlaced = hdr.interlaced;
104 Ok(hdr)
105 }
106 fn decode_mv(&self, bc: &mut BoolCoder, model: &VP56MVModel) -> i16 {
3584b223
KS
107 let val = if !bc.read_prob(model.nz_prob) { // short vector
108 vp_tree!(bc, model.tree_probs[0],
109 vp_tree!(bc, model.tree_probs[1],
110 vp_tree!(bc, model.tree_probs[2], 0, 1),
111 vp_tree!(bc, model.tree_probs[3], 2, 3)),
112 vp_tree!(bc, model.tree_probs[4],
113 vp_tree!(bc, model.tree_probs[5], 4, 5),
114 vp_tree!(bc, model.tree_probs[6], 6, 7)))
115 } else {
116 let mut raw = 0;
117 for ord in LONG_VECTOR_ORDER.iter() {
118 raw |= (bc.read_prob(model.raw_probs[*ord]) as i16) << *ord;
119 }
120 if (raw & 0xF0) != 0 {
121 raw |= (bc.read_prob(model.raw_probs[3]) as i16) << 3;
122 } else {
123 raw |= 1 << 3;
124 }
125 raw
126 };
127 if (val != 0) && bc.read_prob(model.sign_prob) {
128 -val
129 } else {
130 val
131 }
132 }
133 fn reset_models(&self, models: &mut VP56Models) {
3584b223
KS
134 for (i, mdl) in models.mv_models.iter_mut().enumerate() {
135 mdl.nz_prob = NZ_PROBS[i];
136 mdl.sign_prob = 128;
137 mdl.raw_probs.copy_from_slice(&RAW_PROBS[i]);
138 mdl.tree_probs.copy_from_slice(&TREE_PROBS[i]);
139 }
140 models.vp6models.zero_run_probs.copy_from_slice(&ZERO_RUN_PROBS);
141 reset_scan(&mut models.vp6models, self.interlaced);
142 }
143 fn decode_mv_models(&self, bc: &mut BoolCoder, models: &mut [VP56MVModel; 2]) -> DecoderResult<()> {
3584b223
KS
144 for comp in 0..2 {
145 if bc.read_prob(HAS_NZ_PROB[comp]) {
146 models[comp].nz_prob = bc.read_probability();
147 }
148 if bc.read_prob(HAS_SIGN_PROB[comp]) {
149 models[comp].sign_prob = bc.read_probability();
150 }
151 }
152 for comp in 0..2 {
153 for (i, prob) in HAS_TREE_PROB[comp].iter().enumerate() {
154 if bc.read_prob(*prob) {
155 models[comp].tree_probs[i] = bc.read_probability();
156 }
157 }
158 }
159 for comp in 0..2 {
160 for (i, prob) in HAS_RAW_PROB[comp].iter().enumerate() {
161 if bc.read_prob(*prob) {
162 models[comp].raw_probs[i] = bc.read_probability();
163 }
164 }
165 }
166 Ok(())
167 }
168 fn decode_coeff_models(&self, bc: &mut BoolCoder, models: &mut VP56Models, is_intra: bool) -> DecoderResult<()> {
3584b223
KS
169 let mut def_prob = [128u8; 11];
170 for plane in 0..2 {
171 for i in 0..11 {
01551716 172 if bc.read_prob(HAS_COEF_PROBS[plane][i]) {
3584b223
KS
173 def_prob[i] = bc.read_probability();
174 models.coeff_models[plane].dc_value_probs[i] = def_prob[i];
175 } else if is_intra {
176 models.coeff_models[plane].dc_value_probs[i] = def_prob[i];
177 }
178 }
179 }
180
181 if bc.read_bool() {
182 for i in 1..64 {
01551716 183 if bc.read_prob(HAS_SCAN_UPD_PROBS[i]) {
3584b223
KS
184 models.vp6models.scan_order[i] = bc.read_bits(4) as usize;
185 }
186 }
187 update_scan(&mut models.vp6models);
188 } else {
189 reset_scan(&mut models.vp6models, self.interlaced);
190 }
191
192 for comp in 0..2 {
193 for i in 0..14 {
01551716 194 if bc.read_prob(HAS_ZERO_RUN_PROBS[comp][i]) {
3584b223
KS
195 models.vp6models.zero_run_probs[comp][i] = bc.read_probability();
196 }
197 }
198 }
199
200 for ctype in 0..3 {
201 for plane in 0..2 {
202 for group in 0..6 {
203 for i in 0..11 {
204 if bc.read_prob(VP6_AC_PROBS[ctype][plane][group][i]) {
205 def_prob[i] = bc.read_probability();
206 models.coeff_models[plane].ac_val_probs[ctype][group][i] = def_prob[i];
207 } else if is_intra {
208 models.coeff_models[plane].ac_val_probs[ctype][group][i] = def_prob[i];
209 }
210 }
211 }
212 }
213 }
214 for plane in 0..2 {
215 let mdl = &mut models.coeff_models[plane];
216 for i in 0..3 {
217 for k in 0..5 {
218 mdl.dc_token_probs[0][i][k] = rescale_prob(mdl.dc_value_probs[k], &VP6_DC_WEIGHTS[k][i], 255);
219 }
220 }
221 }
222 Ok(())
223 }
224 fn decode_block(&self, bc: &mut BoolCoder, coeffs: &mut [i16; 64], model: &VP56CoeffModel, vp6model: &VP6Models, fstate: &mut FrameState) -> DecoderResult<()> {
225 let left_ctx = fstate.coeff_cat[fstate.ctx_idx][0] as usize;
226 let top_ctx = fstate.top_ctx as usize;
227 let dc_mode = top_ctx + left_ctx;
228 let token = decode_token_bc(bc, &model.dc_token_probs[0][dc_mode], model.dc_value_probs[5], true, true);
229 let val = expand_token_bc(bc, &model.dc_value_probs, token, 6);
230 coeffs[0] = val;
231 fstate.last_idx[fstate.ctx_idx] = 0;
232
233 let mut idx = 1;
234 let mut last_val = val;
235 while idx < 64 {
236 let ac_band = VP6_IDX_TO_AC_BAND[idx];
237 let ac_mode = last_val.abs().min(2) as usize;
238 let has_nnz = (idx == 1) || (last_val != 0);
239 let token = decode_token_bc(bc, &model.ac_val_probs[ac_mode][ac_band], model.ac_val_probs[ac_mode][ac_band][5], false, has_nnz);
240 if token == 42 { break; }
241 let val = expand_token_bc(bc, &model.ac_val_probs[ac_mode][ac_band], token, 6);
242 coeffs[vp6model.zigzag[idx]] = val.wrapping_mul(fstate.ac_quant);
243 idx += 1;
244 last_val = val;
245 if val == 0 {
246 idx += decode_zero_run_bc(bc, &vp6model.zero_run_probs[if idx >= 7 { 1 } else { 0 }]);
247 validate!(idx <= 64);
248 }
249 }
250 fstate.coeff_cat[fstate.ctx_idx][0] = if coeffs[0] != 0 { 1 } else { 0 };
251 fstate.top_ctx = fstate.coeff_cat[fstate.ctx_idx][0];
252 fstate.last_idx[fstate.ctx_idx] = idx;
253 Ok(())
254 }
255 fn decode_block_huff(&self, br: &mut BitReader, coeffs: &mut [i16; 64], vp6model: &VP6Models, model: &VP6HuffModels, fstate: &mut FrameState) -> DecoderResult<()> {
256 let plane = if (fstate.plane == 0) || (fstate.plane == 3) { 0 } else { 1 };
257 let mut last_val;
258
259 if fstate.dc_zero_run[plane] == 0 {
260 let (val, eob) = decode_token_huff(br, &model.dc_token_tree[plane])?;
261 if eob {
262 return Ok(());
263 }
264 last_val = val;
265 coeffs[0] = val;
266 if val == 0 {
267 fstate.dc_zero_run[plane] = decode_eob_run_huff(br)?;
268 }
269 } else {
270 last_val = 0;
271 fstate.dc_zero_run[plane] -= 1;
272 }
273
274 if fstate.ac_zero_run[plane] > 0 {
275 fstate.ac_zero_run[plane] -= 1;
276 fstate.last_idx[fstate.ctx_idx] = 0;
277 return Ok(());
278 }
279
280 let mut idx = 1;
281 while idx < 64 {
282 let ac_band = VP6_IDX_TO_AC_BAND[idx].min(3);
283 let ac_mode = last_val.abs().min(2) as usize;
284 let (val, eob) = decode_token_huff(br, &model.ac_token_tree[plane][ac_mode][ac_band])?;
285 if eob {
286 if idx == 1 {
287 fstate.ac_zero_run[plane] = decode_eob_run_huff(br)?;
288 }
289 break;
290 }
291 coeffs[vp6model.zigzag[idx]] = val.wrapping_mul(fstate.ac_quant);
292 idx += 1;
293 last_val = val;
294 if val == 0 {
295 idx += decode_zero_run_huff(br, &model.zero_run_tree[if idx >= 7 { 1 } else { 0 }])?;
296 validate!(idx <= 64);
297 }
298 }
299
300 fstate.last_idx[fstate.ctx_idx] = idx;
301
302 Ok(())
303 }
85d7efe3 304 fn mc_block(&self, dst: &mut NASimpleVideoFrame<u8>, mut mc_buf: NAVideoBufferRef<u8>, src: NAVideoBufferRef<u8>, plane: usize, x: usize, y: usize, mv: MV, loop_str: i16) {
3584b223 305 let is_luma = (plane != 1) && (plane != 2);
85d7efe3
KS
306 let (sx, sy, mx, my, msx, msy) = if is_luma {
307 (mv.x >> 2, mv.y >> 2, (mv.x & 3) << 1, (mv.y & 3) << 1, mv.x / 4, mv.y / 4)
3584b223 308 } else {
85d7efe3 309 (mv.x >> 3, mv.y >> 3, mv.x & 7, mv.y & 7, mv.x / 8, mv.y / 8)
3584b223
KS
310 };
311 let tmp_blk = mc_buf.get_data_mut().unwrap();
b7c882c1 312 get_block(tmp_blk, 16, src, plane, x, y, sx, sy);
85d7efe3
KS
313 if (msx & 7) != 0 {
314 let foff = (8 - (sx & 7)) as usize;
315 let off = 2 + foff;
316 vp31_loop_filter(tmp_blk, off, 1, 16, 12, loop_str);
317 }
318 if (msy & 7) != 0 {
319 let foff = (8 - (sy & 7)) as usize;
320 let off = (2 + foff) * 16;
321 vp31_loop_filter(tmp_blk, off, 16, 1, 12, loop_str);
322 }
323 let copy_mode = (mx == 0) && (my == 0);
324 let mut bicubic = !copy_mode && is_luma && self.bicubic;
325 if is_luma && !copy_mode && (self.profile == VP6_ADVANCED_PROFILE) {
3584b223
KS
326 if !self.autosel_pm {
327 bicubic = true;
328 } else {
329 let mv_limit = 1 << (self.mv_thresh + 1);
330 if (mv.x.abs() <= mv_limit) && (mv.y.abs() <= mv_limit) {
85d7efe3
KS
331 let mut var_off = 16 * 2 + 2;
332 if mv.x < 0 { var_off += 1; }
333 if mv.y < 0 { var_off += 16; }
334 let var = calc_variance(&tmp_blk[var_off..], 16);
335 if var >= self.var_thresh {
336 bicubic = true;
3584b223
KS
337 }
338 }
339 }
340 }
341 let dstride = dst.stride[plane];
342 let dbuf = &mut dst.data[dst.offset[plane] + x + y * dstride..];
85d7efe3 343 if copy_mode {
3584b223
KS
344 let src = &tmp_blk[2 * 16 + 2..];
345 for (dline, sline) in dbuf.chunks_mut(dst.stride[plane]).zip(src.chunks(16)).take(8) {
47933c6d 346 dline[..8].copy_from_slice(&sline[..8]);
3584b223 347 }
85d7efe3 348 } else if bicubic {
3584b223
KS
349 let coeff_h = &VP6_BICUBIC_COEFFS[self.filter_alpha][mx as usize];
350 let coeff_v = &VP6_BICUBIC_COEFFS[self.filter_alpha][my as usize];
351 mc_bicubic(dbuf, dstride, tmp_blk, 16 * 2 + 2, 16, coeff_h, coeff_v);
352 } else {
353 mc_bilinear(dbuf, dstride, tmp_blk, 16 * 2 + 2, 16, mx as u16, my as u16);
354 }
355 }
356}
357
358fn update_scan(model: &mut VP6Models) {
359 let mut idx = 1;
360 for band in 0..16 {
361 for i in 1..64 {
362 if model.scan_order[i] == band {
363 model.scan[idx] = i;
364 idx += 1;
365 }
366 }
367 }
368 for i in 1..64 {
369 model.zigzag[i] = ZIGZAG[model.scan[i]];
370 }
371}
372
373fn reset_scan(model: &mut VP6Models, interlaced: bool) {
3584b223
KS
374 if !interlaced {
375 model.scan_order.copy_from_slice(&VP6_DEFAULT_SCAN_ORDER);
376 } else {
377 model.scan_order.copy_from_slice(&VP6_INTERLACED_SCAN_ORDER);
378 }
379 for i in 0..64 { model.scan[i] = i; }
380 model.zigzag.copy_from_slice(&ZIGZAG);
381}
382
383fn decode_token_bc(bc: &mut BoolCoder, probs: &[u8], prob34: u8, is_dc: bool, has_nnz: bool) -> u8 {
384 if has_nnz && !bc.read_prob(probs[0]) {
385 if is_dc || bc.read_prob(probs[1]) {
386 0
387 } else {
388 TOKEN_EOB
389 }
390 } else {
391 vp_tree!(bc, probs[2],
392 1,
393 vp_tree!(bc, probs[3],
394 vp_tree!(bc, probs[4],
395 2,
396 vp_tree!(bc, prob34, 3, 4)),
397 TOKEN_LARGE))
398 }
399}
400
401fn decode_zero_run_bc(bc: &mut BoolCoder, probs: &[u8; 14]) -> usize {
402 let val = vp_tree!(bc, probs[0],
403 vp_tree!(bc, probs[1],
404 vp_tree!(bc, probs[2], 0, 1),
405 vp_tree!(bc, probs[3], 2, 3)),
406 vp_tree!(bc, probs[4],
407 vp_tree!(bc, probs[5],
408 vp_tree!(bc, probs[6], 4, 5),
409 vp_tree!(bc, probs[7], 6, 7)),
410 42));
411 if val != 42 {
412 val
413 } else {
414 let mut nval = 8;
415 for i in 0..6 {
416 nval += (bc.read_prob(probs[i + 8]) as usize) << i;
417 }
418 nval
419 }
420}
421
422fn decode_token_huff(br: &mut BitReader, huff: &VP6Huff) -> DecoderResult<(i16, bool)> {
3584b223
KS
423 let tok = br.read_huff(huff)?;
424 match tok {
425 0 => Ok((0, false)),
426 1 | 2 | 3 | 4 => {
427 if !br.read_bool()? {
47933c6d 428 Ok((i16::from(tok), false))
3584b223 429 } else {
47933c6d 430 Ok((-i16::from(tok), false))
3584b223
KS
431 }
432 },
433 5 | 6 | 7 | 8 | 9 | 10 => {
434 let base = (tok - 5) as usize;
01551716 435 let add_bits = br.read(VP6_COEF_ADD_BITS[base])? as i16;
3584b223
KS
436 let val = VP56_COEF_BASE[base] + add_bits;
437 if !br.read_bool()? {
438 Ok((val, false))
439 } else {
440 Ok((-val, false))
441 }
442 },
443 _ => Ok((0, true)),
444 }
445}
446
447fn decode_eob_run_huff(br: &mut BitReader) -> DecoderResult<usize> {
448 let val = br.read(2)?;
449 match val {
450 0 => Ok(0),
451 1 => Ok(1),
452 2 => {
453 let val = br.read(2)?;
454 Ok((val as usize) + 2)
455 },
456 _ => {
457 if br.read_bool()? {
458 Ok((br.read(6)? as usize) + 10)
459 } else {
460 Ok((br.read(2)? as usize) + 6)
461 }
462 },
463 }
464}
465
466fn decode_zero_run_huff(br: &mut BitReader, huff: &VP6Huff) -> DecoderResult<usize> {
467 let val = br.read_huff(huff)?;
468 if val < 8 {
469 Ok(val as usize)
470 } else {
471 Ok((br.read(6)? as usize) + 8)
472 }
473}
474
47933c6d 475#[allow(clippy::too_many_arguments)]
3584b223
KS
476fn get_block(dst: &mut [u8], dstride: usize, src: NAVideoBufferRef<u8>, comp: usize,
477 dx: usize, dy: usize, mv_x: i16, mv_y: i16)
478{
479 let (w, h) = src.get_dimensions(comp);
480 let sx = (dx as isize) + (mv_x as isize);
481 let sy = (dy as isize) + (mv_y as isize);
482
483 if (sx - 2 < 0) || (sx + 8 + 2 > (w as isize)) ||
484 (sy - 2 < 0) || (sy + 8 + 2 > (h as isize)) {
485 edge_emu(&src, sx - 2, sy - 2, 8 + 2 + 2, 8 + 2 + 2,
86081fed 486 dst, dstride, comp, 0);
3584b223
KS
487 } else {
488 let sstride = src.get_stride(comp);
489 let soff = src.get_offset(comp);
490 let sdta = src.get_data();
491 let sbuf: &[u8] = sdta.as_slice();
492 let saddr = soff + ((sx - 2) as usize) + ((sy - 2) as usize) * sstride;
493 let src = &sbuf[saddr..];
494 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(12) {
47933c6d 495 dline[..12].copy_from_slice(&sline[..12]);
3584b223
KS
496 }
497 }
498}
499
500fn calc_variance(src: &[u8], stride: usize) -> u16 {
501 let mut sum = 0;
502 let mut ssum = 0;
85d7efe3 503 for line in src.chunks(stride * 2).take(4) {
3584b223 504 for el in line.iter().take(8).step_by(2) {
47933c6d 505 let pix = u32::from(*el);
3584b223
KS
506 sum += pix;
507 ssum += pix * pix;
508 }
509 }
510 ((ssum * 16 - sum * sum) >> 8) as u16
511}
512
513macro_rules! mc_filter {
514 (bilinear; $a: expr, $b: expr, $c: expr) => {
47933c6d 515 ((u16::from($a) * (8 - $c) + u16::from($b) * $c + 4) >> 3) as u8
3584b223
KS
516 };
517 (bicubic; $src: expr, $off: expr, $step: expr, $coeffs: expr) => {
47933c6d
KS
518 ((i32::from($src[$off - $step] ) * i32::from($coeffs[0]) +
519 i32::from($src[$off] ) * i32::from($coeffs[1]) +
520 i32::from($src[$off + $step] ) * i32::from($coeffs[2]) +
521 i32::from($src[$off + $step * 2]) * i32::from($coeffs[3]) + 64) >> 7).min(255).max(0) as u8
3584b223
KS
522 }
523}
524
525//#[allow(snake_case)]
526fn mc_bilinear(dst: &mut [u8], dstride: usize, src: &[u8], mut soff: usize, sstride: usize, mx: u16, my: u16) {
527 if my == 0 {
528 for dline in dst.chunks_mut(dstride).take(8) {
529 for i in 0..8 {
530 dline[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
531 }
532 soff += sstride;
533 }
534 } else if mx == 0 {
535 for dline in dst.chunks_mut(dstride).take(8) {
536 for i in 0..8 {
537 dline[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + sstride], my);
538 }
539 soff += sstride;
540 }
541 } else {
542 let mut tmp = [0u8; 8];
543 for i in 0..8 {
544 tmp[i] = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
545 }
546 soff += sstride;
547 for dline in dst.chunks_mut(dstride).take(8) {
548 for i in 0..8 {
549 let cur = mc_filter!(bilinear; src[soff + i], src[soff + i + 1], mx);
550 dline[i] = mc_filter!(bilinear; tmp[i], cur, my);
551 tmp[i] = cur;
552 }
553 soff += sstride;
554 }
555 }
556}
557
47933c6d 558#[allow(clippy::trivially_copy_pass_by_ref)]
3584b223
KS
559fn mc_bicubic(dst: &mut [u8], dstride: usize, src: &[u8], mut soff: usize, sstride: usize, coeffs_w: &[i16; 4], coeffs_h: &[i16; 4]) {
560 if coeffs_h[1] == 128 {
561 for dline in dst.chunks_mut(dstride).take(8) {
562 for i in 0..8 {
563 dline[i] = mc_filter!(bicubic; src, soff + i, 1, coeffs_w);
564 }
565 soff += sstride;
566 }
567 } else if coeffs_w[1] == 128 { // horizontal-only interpolation
568 for dline in dst.chunks_mut(dstride).take(8) {
569 for i in 0..8 {
570 dline[i] = mc_filter!(bicubic; src, soff + i, sstride, coeffs_h);
571 }
572 soff += sstride;
573 }
574 } else {
575 let mut buf = [0u8; 16 * 11];
576 soff -= sstride;
577 for dline in buf.chunks_mut(16) {
578 for i in 0..8 {
579 dline[i] = mc_filter!(bicubic; src, soff + i, 1, coeffs_w);
580 }
581 soff += sstride;
582 }
583 let mut soff = 16;
584 for dline in dst.chunks_mut(dstride).take(8) {
585 for i in 0..8 {
586 dline[i] = mc_filter!(bicubic; buf, soff + i, 16, coeffs_h);
587 }
588 soff += 16;
589 }
590 }
591}
592
593struct VP6Decoder {
594 dec: VP56Decoder,
595 info: NACodecInfoRef,
596 br: VP6BR,
597 has_alpha: bool,
598}
599
600impl VP6Decoder {
601 fn new(has_alpha: bool) -> Self {
602 Self {
603 dec: VP56Decoder::new(6, has_alpha, true),
604 info: NACodecInfoRef::default(),
605 br: VP6BR::new(),
606 has_alpha,
607 }
608 }
609}
610
611impl NADecoder for VP6Decoder {
612 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
613 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
614 let fmt = if !self.has_alpha {
615 YUV420_FORMAT
616 } else {
93bbc2b0 617 VP_YUVA420_FORMAT
3584b223
KS
618 };
619 let myvinfo = NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, fmt);
47933c6d 620 let myinfo = NACodecTypeInfo::Video(myvinfo);
3584b223
KS
621 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
622 self.dec.init(supp, myvinfo)?;
623 Ok(())
624 } else {
625 Err(DecoderError::InvalidData)
626 }
627 }
628 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
629 let src = pkt.get_buffer();
630
631 let (bufinfo, ftype) = self.dec.decode_frame(supp, src.as_slice(), &mut self.br)?;
632
633 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
634 frm.set_keyframe(ftype == FrameType::I);
635 frm.set_frame_type(ftype);
636 Ok(frm.into_ref())
637 }
f9be4e75
KS
638 fn flush(&mut self) {
639 self.dec.flush();
640 }
3584b223
KS
641}
642
7d57ae2f
KS
643impl NAOptionHandler for VP6Decoder {
644 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
645 fn set_options(&mut self, _options: &[NAOption]) { }
646 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
647}
648
ac818eac 649pub fn get_decoder_vp6() -> Box<dyn NADecoder + Send> {
3584b223
KS
650 Box::new(VP6Decoder::new(false))
651}
652
ac818eac 653pub fn get_decoder_vp6_alpha() -> Box<dyn NADecoder + Send> {
3584b223
KS
654 Box::new(VP6Decoder::new(true))
655}
656
657#[cfg(test)]
658mod test {
659 use nihav_core::codecs::RegisteredDecoders;
660 use nihav_core::demuxers::RegisteredDemuxers;
ce742854 661 use nihav_codec_support::test::dec_video::*;
78fb6560 662 use crate::duck_register_all_decoders;
e64739f8 663 use nihav_commonfmt::generic_register_all_demuxers;
3584b223
KS
664
665 #[test]
666 fn test_vp6() {
667 let mut dmx_reg = RegisteredDemuxers::new();
668 generic_register_all_demuxers(&mut dmx_reg);
669 let mut dec_reg = RegisteredDecoders::new();
78fb6560 670 duck_register_all_decoders(&mut dec_reg);
3584b223 671
7be4326b
KS
672 test_decoding("avi", "vp6", "assets/Duck/selection_720x576_300kBit_vp60i.avi", Some(16),
673 &dmx_reg, &dec_reg,
674 ExpectedTestResult::MD5([0x042c3e96, 0x8a9b26a2, 0x4dcbaf66, 0x1b788d03]));
3584b223 675 }
7be4326b
KS
676 #[test]
677 fn test_vp6_huff() {
678 let mut dmx_reg = RegisteredDemuxers::new();
679 generic_register_all_demuxers(&mut dmx_reg);
680 let mut dec_reg = RegisteredDecoders::new();
78fb6560 681 duck_register_all_decoders(&mut dec_reg);
7be4326b
KS
682
683 test_decoding("avi", "vp6", "assets/Duck/vp6_crash.avi", Some(4),
684 &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
685 [0xdcd70fa0, 0x0d075ce2, 0xc9e65077, 0xb003a92e],
686 [0x334abf96, 0x3a004c7a, 0x5781cd5c, 0x25c3ae5c],
687 [0x6164b851, 0x528cd8de, 0xecab7328, 0x4b49708a],
688 [0x11b048ac, 0xedb3e471, 0xd04e9399, 0x64e623e3],
689 [0x182871b1, 0x2146893a, 0x2912210e, 0x6dd592e8]]));
690 }
691 // todo find good sample for vp6a test
3584b223 692}