FFmpeg
vp6.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * VP6 compatible video decoder
24  *
25  * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
26  * - upper 4 bits: difference between encoded width and visible width
27  * - lower 4 bits: difference between encoded height and visible height
28  */
29 
30 #include <stdlib.h>
31 
32 #include "avcodec.h"
33 #include "codec_internal.h"
34 #include "decode.h"
35 #include "get_bits.h"
36 #include "huffman.h"
37 
38 #include "vp56.h"
39 #include "vp56data.h"
40 #include "vp6data.h"
41 #include "vpx_rac.h"
42 
43 #define VP6_MAX_HUFF_SIZE 12
44 #define AC_DC_HUFF_BITS 10
45 #define RUN_HUFF_BITS 8
46 
47 static int vp6_parse_coeff(VP56Context *s);
48 static int vp6_parse_coeff_huffman(VP56Context *s);
49 
50 static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
51 {
52  VPXRangeCoder *c = &s->c;
53  int parse_filter_info = 0;
54  int coeff_offset = 0;
55  int vrt_shift = 0;
56  int sub_version;
57  int rows, cols;
58  int res = 0;
59  int ret;
60  int separated_coeff = buf[0] & 1;
61 
62  if (!(buf[0] & 0x80))
63  s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
64  else
65  s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
66  ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
67 
68  if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
69  sub_version = buf[1] >> 3;
70  if (sub_version > 8)
71  return AVERROR_INVALIDDATA;
72  s->filter_header = buf[1] & 0x06;
73  s->interlaced = buf[1] & 1;
74  if (s->interlaced)
75  s->def_coeff_reorder = vp6_il_coeff_reorder;
76  else
77  s->def_coeff_reorder = vp6_def_coeff_reorder;
78  if (separated_coeff || !s->filter_header) {
79  coeff_offset = AV_RB16(buf+2) - 2;
80  buf += 2;
81  buf_size -= 2;
82  }
83 
84  rows = buf[2]; /* number of stored macroblock rows */
85  cols = buf[3]; /* number of stored macroblock cols */
86  /* buf[4] is number of displayed macroblock rows */
87  /* buf[5] is number of displayed macroblock cols */
88  if (!rows || !cols) {
89  av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
90  return AVERROR_INVALIDDATA;
91  }
92 
93  if (!s->macroblocks || /* first frame */
94  16*cols != s->avctx->coded_width ||
95  16*rows != s->avctx->coded_height) {
96  if (s->avctx->extradata_size == 0 &&
97  FFALIGN(s->avctx->width, 16) == 16 * cols &&
98  FFALIGN(s->avctx->height, 16) == 16 * rows) {
99  // We assume this is properly signalled container cropping,
100  // in an F4V file. Just set the coded_width/height, don't
101  // touch the cropped ones.
102  s->avctx->coded_width = 16 * cols;
103  s->avctx->coded_height = 16 * rows;
104  } else {
105  ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
106  if (ret < 0)
107  return ret;
108 
109  if (s->avctx->extradata_size == 1) {
110  s->avctx->width -= s->avctx->extradata[0] >> 4;
111  s->avctx->height -= s->avctx->extradata[0] & 0x0F;
112  }
113  }
114  res = VP56_SIZE_CHANGE;
115  }
116 
117  ret = ff_vpx_init_range_decoder(c, buf+6, buf_size-6);
118  if (ret < 0)
119  goto fail;
120  vp56_rac_gets(c, 2);
121 
122  parse_filter_info = s->filter_header;
123  if (sub_version < 8)
124  vrt_shift = 5;
125  s->sub_version = sub_version;
126  s->golden_frame = 0;
127  } else {
128  if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
129  return AVERROR_INVALIDDATA;
130 
131  if (separated_coeff || !s->filter_header) {
132  coeff_offset = AV_RB16(buf+1) - 2;
133  buf += 2;
134  buf_size -= 2;
135  }
136  ret = ff_vpx_init_range_decoder(c, buf+1, buf_size-1);
137  if (ret < 0)
138  return ret;
139 
140  s->golden_frame = vpx_rac_get(c);
141  if (s->filter_header) {
142  s->deblock_filtering = vpx_rac_get(c);
143  if (s->deblock_filtering)
144  vpx_rac_get(c);
145  if (s->sub_version > 7)
146  parse_filter_info = vpx_rac_get(c);
147  }
148  }
149 
150  if (parse_filter_info) {
151  if (vpx_rac_get(c)) {
152  s->filter_mode = 2;
153  s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
154  s->max_vector_length = 2 << vp56_rac_gets(c, 3);
155  } else if (vpx_rac_get(c)) {
156  s->filter_mode = 1;
157  } else {
158  s->filter_mode = 0;
159  }
160  if (s->sub_version > 7)
161  s->filter_selection = vp56_rac_gets(c, 4);
162  else
163  s->filter_selection = 16;
164  }
165 
166  s->use_huffman = vpx_rac_get(c);
167 
168  s->parse_coeff = vp6_parse_coeff;
169  if (coeff_offset) {
170  buf += coeff_offset;
171  buf_size -= coeff_offset;
172  if (buf_size < 0) {
174  goto fail;
175  }
176  if (s->use_huffman) {
177  s->parse_coeff = vp6_parse_coeff_huffman;
178  ret = init_get_bits8(&s->gb, buf, buf_size);
179  if (ret < 0)
180  return ret;
181  } else {
182  ret = ff_vpx_init_range_decoder(&s->cc, buf, buf_size);
183  if (ret < 0)
184  goto fail;
185  s->ccp = &s->cc;
186  }
187  } else {
188  s->ccp = &s->c;
189  }
190 
191  return res;
192 fail:
193  if (res == VP56_SIZE_CHANGE)
194  ff_set_dimensions(s->avctx, 0, 0);
195  return ret;
196 }
197 
198 static void vp6_coeff_order_table_init(VP56Context *s)
199 {
200  int i, pos, idx = 1;
201 
202  s->modelp->coeff_index_to_pos[0] = 0;
203  for (i=0; i<16; i++)
204  for (pos=1; pos<64; pos++)
205  if (s->modelp->coeff_reorder[pos] == i)
206  s->modelp->coeff_index_to_pos[idx++] = pos;
207 
208  for (idx = 0; idx < 64; idx++) {
209  int max = 0;
210  for (i = 0; i <= idx; i++) {
211  int v = s->modelp->coeff_index_to_pos[i];
212  if (v > max)
213  max = v;
214  }
215  if (s->sub_version > 6)
216  max++;
217  s->modelp->coeff_index_to_idct_selector[idx] = max;
218  }
219 }
220 
221 static void vp6_default_models_init(VP56Context *s)
222 {
223  VP56Model *model = s->modelp;
224 
225  model->vector_dct[0] = 0xA2;
226  model->vector_dct[1] = 0xA4;
227  model->vector_sig[0] = 0x80;
228  model->vector_sig[1] = 0x80;
229 
230  memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
231  memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
232  memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
233  memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
234  memcpy(model->coeff_reorder, s->def_coeff_reorder, sizeof(model->coeff_reorder));
235 
237 }
238 
239 static void vp6_parse_vector_models(VP56Context *s)
240 {
241  VPXRangeCoder *c = &s->c;
242  VP56Model *model = s->modelp;
243  int comp, node;
244 
245  for (comp=0; comp<2; comp++) {
247  model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
249  model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
250  }
251 
252  for (comp=0; comp<2; comp++)
253  for (node=0; node<7; node++)
255  model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
256 
257  for (comp=0; comp<2; comp++)
258  for (node=0; node<8; node++)
260  model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
261 }
262 
263 /* nodes must ascend by count, but with descending symbol order */
264 static int vp6_huff_cmp(const void *va, const void *vb)
265 {
266  const Node *a = va, *b = vb;
267  return (a->count - b->count)*16 + (b->sym - a->sym);
268 }
269 
270 static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
271  const uint8_t *map, unsigned size,
272  int nb_bits, VLC *vlc)
273 {
274  Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
275  int a, b, i;
276 
277  /* first compute probabilities from model */
278  tmp[0].count = 256;
279  for (i=0; i<size-1; i++) {
280  a = tmp[i].count * coeff_model[i] >> 8;
281  b = tmp[i].count * (255 - coeff_model[i]) >> 8;
282  nodes[map[2*i ]].count = a + !a;
283  nodes[map[2*i+1]].count = b + !b;
284  }
285 
286  ff_vlc_free(vlc);
287  /* then build the huffman tree according to probabilities */
288  return ff_huff_build_tree(s->avctx, vlc, size, nb_bits,
289  nodes, vp6_huff_cmp,
291 }
292 
293 static int vp6_parse_coeff_models(VP56Context *s)
294 {
295  VPXRangeCoder *c = &s->c;
296  VP56Model *model = s->modelp;
297  int def_prob[11];
298  int node, cg, ctx, pos;
299  int ct; /* code type */
300  int pt; /* plane type (0 for Y, 1 for U or V) */
301  int ret;
302 
303  memset(def_prob, 0x80, sizeof(def_prob));
304 
305  for (pt=0; pt<2; pt++)
306  for (node=0; node<11; node++)
307  if (vpx_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
308  def_prob[node] = vp56_rac_gets_nn(c, 7);
309  model->coeff_dccv[pt][node] = def_prob[node];
310  } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
311  model->coeff_dccv[pt][node] = def_prob[node];
312  }
313 
314  if (vpx_rac_get(c)) {
315  for (pos=1; pos<64; pos++)
317  model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
319  }
320 
321  for (cg=0; cg<2; cg++)
322  for (node=0; node<14; node++)
323  if (vpx_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
324  model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
325 
326  for (ct=0; ct<3; ct++)
327  for (pt=0; pt<2; pt++)
328  for (cg=0; cg<6; cg++)
329  for (node=0; node<11; node++)
330  if (vpx_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
331  def_prob[node] = vp56_rac_gets_nn(c, 7);
332  model->coeff_ract[pt][ct][cg][node] = def_prob[node];
333  } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
334  model->coeff_ract[pt][ct][cg][node] = def_prob[node];
335  }
336 
337  if (s->use_huffman) {
338  for (pt=0; pt<2; pt++) {
341  &s->dccv_vlc[pt]);
342  if (ret < 0)
343  return ret;
346  &s->runv_vlc[pt]);
347  if (ret < 0)
348  return ret;
349  for (ct=0; ct<3; ct++)
350  for (int cg = 0; cg < 4; cg++) {
351  ret = vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
352  vp6_huff_coeff_map, 12,
354  &s->ract_vlc[pt][ct][cg]);
355  if (ret < 0)
356  return ret;
357  }
358  }
359  memset(s->nb_null, 0, sizeof(s->nb_null));
360  } else {
361  /* coeff_dcct is a linear combination of coeff_dccv */
362  for (pt=0; pt<2; pt++)
363  for (ctx=0; ctx<3; ctx++)
364  for (node=0; node<5; node++)
365  model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
366  }
367  return 0;
368 }
369 
370 static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
371 {
372  VPXRangeCoder *c = &s->c;
373  VP56Model *model = s->modelp;
374  int comp;
375 
376  *vect = (VP56mv) {0,0};
377  if (s->vector_candidate_pos < 2)
378  *vect = s->vector_candidate[0];
379 
380  for (comp=0; comp<2; comp++) {
381  int i, delta = 0;
382 
383  if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
384  static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
385  for (i=0; i<sizeof(prob_order); i++) {
386  int j = prob_order[i];
387  delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
388  }
389  if (delta & 0xF0)
390  delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
391  else
392  delta |= 8;
393  } else {
395  model->vector_pdv[comp]);
396  }
397 
399  delta = -delta;
400 
401  if (!comp)
402  vect->x += delta;
403  else
404  vect->y += delta;
405  }
406 }
407 
408 /**
409  * Read number of consecutive blocks with null DC or AC.
410  * This value is < 74.
411  */
412 static unsigned vp6_get_nb_null(VP56Context *s)
413 {
414  unsigned val = get_bits(&s->gb, 2);
415  if (val == 2)
416  val += get_bits(&s->gb, 2);
417  else if (val == 3) {
418  val = get_bits1(&s->gb) << 2;
419  val = 6+val + get_bits(&s->gb, 2+val);
420  }
421  return val;
422 }
423 
424 static int vp6_parse_coeff_huffman(VP56Context *s)
425 {
426  VP56Model *model = s->modelp;
427  uint8_t *permute = s->idct_scantable;
428  VLC *vlc_coeff;
429  int sign, coeff_idx;
430  int b, cg, idx;
431  int pt = 0; /* plane type (0 for Y, 1 for U or V) */
432 
433  for (b=0; b<6; b++) {
434  int ct = 0; /* code type */
435  if (b > 3) pt = 1;
436  vlc_coeff = &s->dccv_vlc[pt];
437 
438  for (coeff_idx = 0;;) {
439  int run = 1;
440  if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
441  s->nb_null[coeff_idx][pt]--;
442  if (coeff_idx)
443  break;
444  } else {
445  if (get_bits_left(&s->gb) <= 0)
446  return AVERROR_INVALIDDATA;
447  int coeff = get_vlc2(&s->gb, vlc_coeff->table, AC_DC_HUFF_BITS, 2);
448  if (coeff == 0) {
449  if (coeff_idx) {
450  int pt = (coeff_idx >= 6);
451  run += get_vlc2(&s->gb, s->runv_vlc[pt].table, RUN_HUFF_BITS, 1);
452  if (run >= 9)
453  run += get_bits(&s->gb, 6);
454  } else
455  s->nb_null[0][pt] = vp6_get_nb_null(s);
456  ct = 0;
457  } else if (coeff == 11) { /* end of block */
458  if (coeff_idx == 1) /* first AC coeff ? */
459  s->nb_null[1][pt] = vp6_get_nb_null(s);
460  break;
461  } else {
462  int coeff2 = ff_vp56_coeff_bias[coeff];
463  if (coeff > 4)
464  coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
465  ct = 1 + (coeff2 > 1);
466  sign = get_bits1(&s->gb);
467  coeff2 = (coeff2 ^ -sign) + sign;
468  if (coeff_idx)
469  coeff2 *= s->dequant_ac;
470  idx = model->coeff_index_to_pos[coeff_idx];
471  s->block_coeff[b][permute[idx]] = coeff2;
472  }
473  }
474  coeff_idx+=run;
475  if (coeff_idx >= 64)
476  break;
477  cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
478  vlc_coeff = &s->ract_vlc[pt][ct][cg];
479  }
480  s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
481  }
482  return 0;
483 }
484 
485 static int vp6_parse_coeff(VP56Context *s)
486 {
487  VPXRangeCoder *c = s->ccp;
488  VP56Model *model = s->modelp;
489  uint8_t *permute = s->idct_scantable;
490  uint8_t *model1, *model2, *model3;
491  int coeff, sign, coeff_idx;
492  int b, i, cg, idx, ctx;
493  int pt = 0; /* plane type (0 for Y, 1 for U or V) */
494 
495  if (vpx_rac_is_end(c)) {
496  av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
497  return AVERROR_INVALIDDATA;
498  }
499 
500  for (b=0; b<6; b++) {
501  int ct = 1; /* code type */
502  int run = 1;
503 
504  if (b > 3) pt = 1;
505 
506  ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
507  + s->above_blocks[s->above_block_idx[b]].not_null_dc;
508  model1 = model->coeff_dccv[pt];
509  model2 = model->coeff_dcct[pt][ctx];
510 
511  coeff_idx = 0;
512  for (;;) {
513  if ((coeff_idx>1 && ct==0) || vpx_rac_get_prob_branchy(c, model2[0])) {
514  /* parse a coeff */
515  if (vpx_rac_get_prob_branchy(c, model2[2])) {
516  if (vpx_rac_get_prob_branchy(c, model2[3])) {
517  idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
518  coeff = ff_vp56_coeff_bias[idx+5];
519  for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
521  } else {
522  if (vpx_rac_get_prob_branchy(c, model2[4]))
523  coeff = 3 + vpx_rac_get_prob(c, model1[5]);
524  else
525  coeff = 2;
526  }
527  ct = 2;
528  } else {
529  ct = 1;
530  coeff = 1;
531  }
532  sign = vpx_rac_get(c);
533  coeff = (coeff ^ -sign) + sign;
534  if (coeff_idx)
535  coeff *= s->dequant_ac;
536  idx = model->coeff_index_to_pos[coeff_idx];
537  s->block_coeff[b][permute[idx]] = coeff;
538  run = 1;
539  } else {
540  /* parse a run */
541  ct = 0;
542  if (coeff_idx > 0) {
543  if (!vpx_rac_get_prob_branchy(c, model2[1]))
544  break;
545 
546  model3 = model->coeff_runv[coeff_idx >= 6];
547  run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
548  if (!run)
549  for (run=9, i=0; i<6; i++)
550  run += vpx_rac_get_prob(c, model3[i+8]) << i;
551  }
552  }
553  coeff_idx += run;
554  if (coeff_idx >= 64)
555  break;
556  cg = vp6_coeff_groups[coeff_idx];
557  model1 = model2 = model->coeff_ract[pt][ct][cg];
558  }
559 
560  s->left_block[ff_vp56_b6to4[b]].not_null_dc =
561  s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
562  s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
563  }
564  return 0;
565 }
566 
567 static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
568 {
569  int sum = 0, square_sum = 0;
570  int y, x;
571 
572  for (y=0; y<8; y+=2) {
573  for (x=0; x<8; x+=2) {
574  sum += src[x];
575  square_sum += src[x]*src[x];
576  }
577  src += 2*stride;
578  }
579  return (16*square_sum - sum*sum) >> 8;
580 }
581 
582 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
583  int delta, const int16_t *weights)
584 {
585  int x, y;
586 
587  for (y=0; y<8; y++) {
588  for (x=0; x<8; x++) {
589  dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
590  + src[x ] * weights[1]
591  + src[x+delta ] * weights[2]
592  + src[x+2*delta] * weights[3] + 64) >> 7);
593  }
594  src += stride;
595  dst += stride;
596  }
597 }
598 
599 static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
600  ptrdiff_t stride, int h_weight, int v_weight)
601 {
602  uint8_t *tmp = s->edge_emu_buffer+16;
603  s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
604  s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
605 }
606 
607 static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
608  int offset1, int offset2, ptrdiff_t stride,
609  VP56mv mv, int mask, int select, int luma)
610 {
611  int filter4 = 0;
612  int x8 = mv.x & mask;
613  int y8 = mv.y & mask;
614 
615  if (luma) {
616  x8 *= 2;
617  y8 *= 2;
618  filter4 = s->filter_mode;
619  if (filter4 == 2) {
620  if (s->max_vector_length &&
621  (FFABS(mv.x) > s->max_vector_length ||
622  FFABS(mv.y) > s->max_vector_length)) {
623  filter4 = 0;
624  } else if (s->sample_variance_threshold
625  && (vp6_block_variance(src+offset1, stride)
626  < s->sample_variance_threshold)) {
627  filter4 = 0;
628  }
629  }
630  }
631 
632  if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
633  offset1 = offset2;
634  }
635 
636  if (filter4) {
637  if (!y8) { /* left or right combine */
638  vp6_filter_hv4(dst, src+offset1, stride, 1,
639  vp6_block_copy_filter[select][x8]);
640  } else if (!x8) { /* above or below combine */
641  vp6_filter_hv4(dst, src+offset1, stride, stride,
642  vp6_block_copy_filter[select][y8]);
643  } else {
644  s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
645  vp6_block_copy_filter[select][x8],
646  vp6_block_copy_filter[select][y8]);
647  }
648  } else {
649  if (!x8 || !y8) {
650  s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
651  } else {
652  vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
653  }
654  }
655 }
656 
658  VP56Context *s, int flip, int has_alpha)
659 {
660  int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
661  if (ret < 0)
662  return ret;
663 
664  ff_vp6dsp_init(&s->vp56dsp);
665 
666  s->deblock_filtering = 0;
667  s->vp56_coord_div = vp6_coord_div;
668  s->parse_vector_adjustment = vp6_parse_vector_adjustment;
669  s->filter = vp6_filter;
670  s->default_models_init = vp6_default_models_init;
671  s->parse_vector_models = vp6_parse_vector_models;
672  s->parse_coeff_models = vp6_parse_coeff_models;
673  s->parse_header = vp6_parse_header;
674 
675  return 0;
676 }
677 
679 {
680  VP56Context *s = avctx->priv_data;
681  int ret;
682 
684  avctx->codec_id == AV_CODEC_ID_VP6A);
685  if (ret < 0)
686  return ret;
687 
688  if (s->has_alpha) {
689  /* Can only happen for ff_vp6a_decoder */
690  s->alpha_context = &s[1];
691  ret = vp6_decode_init_context(avctx, s->alpha_context,
692  s->flip == -1, s->has_alpha);
693  if (ret < 0)
694  return ret;
695  }
696 
697  return 0;
698 }
699 
700 static av_cold void vp6_decode_free_context(VP56Context *s);
701 
703 {
704  VP56Context *s = avctx->priv_data;
705 
707 
708  if (s->alpha_context) {
709  vp6_decode_free_context(s->alpha_context);
710  s->alpha_context = NULL;
711  }
712 
713  return 0;
714 }
715 
716 static av_cold void vp6_decode_free_context(VP56Context *s)
717 {
719 
720  for (int pt = 0; pt < 2; ++pt) {
721  ff_vlc_free(&s->dccv_vlc[pt]);
722  ff_vlc_free(&s->runv_vlc[pt]);
723  for (int ct = 0; ct < 3; ++ct)
724  for (int cg = 0; cg < 4; ++cg)
725  ff_vlc_free(&s->ract_vlc[pt][ct][cg]);
726  }
727 }
728 
730  .p.name = "vp6",
731  CODEC_LONG_NAME("On2 VP6"),
732  .p.type = AVMEDIA_TYPE_VIDEO,
733  .p.id = AV_CODEC_ID_VP6,
734  .priv_data_size = sizeof(VP56Context),
738  .p.capabilities = AV_CODEC_CAP_DR1,
739  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
740 };
741 
742 /* flash version, not flipped upside-down */
744  .p.name = "vp6f",
745  CODEC_LONG_NAME("On2 VP6 (Flash version)"),
746  .p.type = AVMEDIA_TYPE_VIDEO,
747  .p.id = AV_CODEC_ID_VP6F,
748  .priv_data_size = sizeof(VP56Context),
752  .p.capabilities = AV_CODEC_CAP_DR1,
753  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
754 };
755 
756 /* flash version, not flipped upside-down, with alpha channel */
758  .p.name = "vp6a",
759  CODEC_LONG_NAME("On2 VP6 (Flash version, with alpha channel)"),
760  .p.type = AVMEDIA_TYPE_VIDEO,
761  .p.id = AV_CODEC_ID_VP6A,
762  .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
766  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
767  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
768 };
vp56_rac_get_tree
static av_always_inline int vp56_rac_get_tree(VPXRangeCoder *c, const VP56Tree *tree, const uint8_t *probs)
Definition: vp56.h:249
AV_CODEC_ID_VP6F
@ AV_CODEC_ID_VP6F
Definition: codec_id.h:144
vp6_def_fdv_vector_model
static const uint8_t vp6_def_fdv_vector_model[2][8]
Definition: vp6data.h:33
vp6_block_copy_filter
static const int16_t vp6_block_copy_filter[17][8][4]
Definition: vp6data.h:162
av_clip
#define av_clip
Definition: common.h:100
VP56mv::x
int16_t x
Definition: vp56.h:68
vp56data.h
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:678
vp6_decode_free
static av_cold int vp6_decode_free(AVCodecContext *avctx)
Definition: vp6.c:702
Node
Definition: agm.c:898
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:79
VP56Model::mb_types_stats
uint8_t mb_types_stats[3][10][2]
Definition: vp56.h:112
vp6_coeff_order_table_init
static void vp6_coeff_order_table_init(VP56Context *s)
Definition: vp6.c:198
ff_vp56_coeff_bit_length
const uint8_t ff_vp56_coeff_bit_length[]
Definition: vp56data.c:68
mv
static const int8_t mv[256][2]
Definition: 4xm.c:81
mask
int mask
Definition: mediacodecdec_common.c:154
AC_DC_HUFF_BITS
#define AC_DC_HUFF_BITS
Definition: vp6.c:44
AV_CODEC_ID_VP6
@ AV_CODEC_ID_VP6
Definition: codec_id.h:143
ff_vp56_init_context
av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s, int flip, int has_alpha)
Initializes an VP56Context.
Definition: vp56.c:819
b
#define b
Definition: input.c:42
permute
static void permute(int16_t dst[64], const int16_t src[64], enum idct_permutation_type perm_type)
Definition: dct.c:153
FFCodec
Definition: codec_internal.h:127
ff_vp6f_decoder
const FFCodec ff_vp6f_decoder
Definition: vp6.c:743
max
#define max(a, b)
Definition: cuda_runtime.h:33
vp6_filter_diag2
static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src, ptrdiff_t stride, int h_weight, int v_weight)
Definition: vp6.c:599
VP56Model::coeff_index_to_pos
uint8_t coeff_index_to_pos[64]
Definition: vp56.h:99
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:91
VP56Model::coeff_ract
uint8_t coeff_ract[2][3][6][11]
Definition: vp56.h:107
VPXRangeCoder
Definition: vpx_rac.h:35
ff_vp6_decoder
const FFCodec ff_vp6_decoder
Definition: vp6.c:729
vp6_def_coeff_reorder
static const uint8_t vp6_def_coeff_reorder[]
Definition: vp6data.h:43
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
fail
#define fail()
Definition: checkasm.h:195
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_vp56_b6to4
const uint8_t ff_vp56_b6to4[]
Definition: vp56data.c:29
VP56_SIZE_CHANGE
#define VP56_SIZE_CHANGE
Definition: vp56.h:72
vp6_huff_cmp
static int vp6_huff_cmp(const void *va, const void *vb)
Definition: vp6.c:264
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:528
vp56_rac_gets_nn
static av_unused int vp56_rac_gets_nn(VPXRangeCoder *c, int bits)
Definition: vp56.h:242
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
vp6_huff_coeff_map
static const uint8_t vp6_huff_coeff_map[]
Definition: vp6data.h:315
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
s
#define s(width, name)
Definition: cbs_vp9.c:198
vp6_huff_run_map
static const uint8_t vp6_huff_run_map[]
Definition: vp6data.h:319
vp6_dccv_pct
static const uint8_t vp6_dccv_pct[2][11]
Definition: vp6data.h:85
vp6_def_runv_coeff_model
static const uint8_t vp6_def_runv_coeff_model[2][14]
Definition: vp6data.h:65
ff_vp6a_decoder
const FFCodec ff_vp6a_decoder
Definition: vp6.c:757
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
ff_vp56_free_context
av_cold int ff_vp56_free_context(VP56Context *s)
Definition: vp56.c:870
get_bits.h
VP56mv::y
int16_t y
Definition: vp56.h:69
VP56mv
Definition: vp56.h:67
vp6_build_huff_tree
static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], const uint8_t *map, unsigned size, int nb_bits, VLC *vlc)
Definition: vp6.c:270
VP56Model::coeff_dccv
uint8_t coeff_dccv[2][11]
Definition: vp56.h:106
VP56Model::coeff_dcct
uint8_t coeff_dcct[2][36][5]
Definition: vp56.h:109
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:441
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
vp56.h
vp6_pcr_tree
static const VP56Tree vp6_pcr_tree[]
Definition: vp6data.h:301
vp6_get_nb_null
static unsigned vp6_get_nb_null(VP56Context *s)
Read number of consecutive blocks with null DC or AC.
Definition: vp6.c:412
VP56Model::vector_sig
uint8_t vector_sig[2]
Definition: vp56.h:101
VP56Model::coeff_runv
uint8_t coeff_runv[2][14]
Definition: vp56.h:110
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:204
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
ff_vp56_def_mb_types_stats
const uint8_t ff_vp56_def_mb_types_stats[3][10][2]
Definition: vp56data.c:40
vp6_parse_header
static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
Definition: vp6.c:50
vp6_decode_init_context
static av_cold int vp6_decode_init_context(AVCodecContext *avctx, VP56Context *s, int flip, int has_alpha)
Definition: vp6.c:657
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
ff_vp6dsp_init
void ff_vp6dsp_init(VP56DSPContext *s)
vp6_parse_vector_adjustment
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
Definition: vp6.c:370
vp6_sig_dct_pct
static const uint8_t vp6_sig_dct_pct[2][2]
Definition: vp6data.h:70
vp6_fdv_pct
static const uint8_t vp6_fdv_pct[2][8]
Definition: vp6data.h:80
AV_CODEC_ID_VP6A
@ AV_CODEC_ID_VP6A
Definition: codec_id.h:158
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:635
vp6_coord_div
static const uint8_t vp6_coord_div[]
Definition: vp6data.h:313
close
av_cold void CBS_FUNC() close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:146
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
vp6_filter_hv4
static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride, int delta, const int16_t *weights)
Definition: vp6.c:582
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
flip
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:131
codec_internal.h
vp6_default_models_init
static void vp6_default_models_init(VP56Context *s)
Definition: vp6.c:221
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
ff_vp56_coeff_parse_table
const uint8_t ff_vp56_coeff_parse_table[6][11]
Definition: vp56data.c:31
size
int size
Definition: twinvq_data.h:10344
vp6_parse_coeff_models
static int vp6_parse_coeff_models(VP56Context *s)
Definition: vp6.c:293
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
VP6_MAX_HUFF_SIZE
#define VP6_MAX_HUFF_SIZE
Definition: vp6.c:43
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:99
pt
int pt
Definition: rtp.c:35
vp56_rac_gets
static int vp56_rac_gets(VPXRangeCoder *c, int bits)
vp56 specific range coder implementation
Definition: vp56.h:230
ff_vp56_pva_tree
const VP56Tree ff_vp56_pva_tree[]
Definition: vp56data.c:49
VP56Model::coeff_index_to_idct_selector
uint8_t coeff_index_to_idct_selector[64]
Definition: vp56.h:100
vpx_rac_is_end
static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c)
returns 1 if the end of the stream has been reached, 0 otherwise.
Definition: vpx_rac.h:51
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
vp6_decode_init
static av_cold int vp6_decode_init(AVCodecContext *avctx)
Definition: vp6.c:678
vpx_rac.h
vp6_il_coeff_reorder
static const uint8_t vp6_il_coeff_reorder[]
Definition: vp6data.h:54
RUN_HUFF_BITS
#define RUN_HUFF_BITS
Definition: vp6.c:45
weights
static const int weights[]
Definition: hevc_pel.c:32
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
vpx_rac_get_prob_branchy
static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
Definition: vpx_rac.h:99
ff_vp56_pc_tree
const VP56Tree ff_vp56_pc_tree[]
Definition: vp56data.c:59
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
avcodec.h
stride
#define stride
Definition: h264pred_template.c:536
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:580
ret
ret
Definition: filter_design.txt:187
ff_huff_build_tree
int ff_huff_build_tree(void *logctx, VLC *vlc, int nb_codes, int nb_bits, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
Definition: huffman.c:155
vp6_filter
static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src, int offset1, int offset2, ptrdiff_t stride, VP56mv mv, int mask, int select, int luma)
Definition: vp6.c:607
ff_vp56_coeff_bias
const uint8_t ff_vp56_coeff_bias[]
Definition: vp56data.c:67
ff_vpx_init_range_decoder
int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size)
Definition: vpx_rac.c:42
pos
unsigned int pos
Definition: spdifenc.c:414
VP56Model::vector_fdv
uint8_t vector_fdv[2][8]
Definition: vp56.h:105
ff_vp56_decode_frame
int ff_vp56_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: vp56.c:607
VP56_FRAME_CURRENT
@ VP56_FRAME_CURRENT
Definition: vp56.h:44
AVCodecContext
main external API structure.
Definition: avcodec.h:431
vp6_block_variance
static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
Definition: vp6.c:567
vp6_coeff_reorder_pct
static const uint8_t vp6_coeff_reorder_pct[]
Definition: vp6data.h:90
vp6_parse_vector_models
static void vp6_parse_vector_models(VP56Context *s)
Definition: vp6.c:239
VP56Model
Definition: vp56.h:97
VLC
Definition: vlc.h:50
ff_vp56_init_dequant
void ff_vp56_init_dequant(VP56Context *s, int quantizer)
Definition: vp56.c:36
vp6_dccv_lc
static const int vp6_dccv_lc[3][5][2]
Definition: vp6data.h:145
huffman.h
vp6_pdv_pct
static const uint8_t vp6_pdv_pct[2][7]
Definition: vp6data.h:75
VLC::table
VLCElem * table
Definition: vlc.h:52
av_clip_uint8
#define av_clip_uint8
Definition: common.h:106
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
vpx_rac_get
static av_always_inline int vpx_rac_get(VPXRangeCoder *c)
Definition: vpx_rac.h:117
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
VP56Model::vector_dct
uint8_t vector_dct[2]
Definition: vp56.h:102
vp6data.h
vp6_coeff_groups
static const uint8_t vp6_coeff_groups[]
Definition: vp6data.h:151
vp6_def_pdv_vector_model
static const uint8_t vp6_def_pdv_vector_model[2][7]
Definition: vp6data.h:38
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
vp6_parse_coeff_huffman
static int vp6_parse_coeff_huffman(VP56Context *s)
Definition: vp6.c:424
Node::count
uint32_t count
Definition: huffman.h:36
vp6_decode_free_context
static av_cold void vp6_decode_free_context(VP56Context *s)
Definition: vp6.c:716
vp6_parse_coeff
static int vp6_parse_coeff(VP56Context *s)
Definition: vp6.c:485
vpx_rac_get_prob
#define vpx_rac_get_prob
Definition: vpx_rac.h:82
FF_HUFFMAN_FLAG_HNODE_FIRST
#define FF_HUFFMAN_FLAG_HNODE_FIRST
Definition: huffman.h:39
src
#define src
Definition: vp8dsp.c:248
VP56Model::vector_pdv
uint8_t vector_pdv[2][7]
Definition: vp56.h:104
vp6_runv_pct
static const uint8_t vp6_runv_pct[2][14]
Definition: vp6data.h:101
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
vp6_ract_pct
static const uint8_t vp6_ract_pct[3][2][6][11]
Definition: vp6data.h:106
VP56Model::coeff_reorder
uint8_t coeff_reorder[64]
Definition: vp56.h:98