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 
45 static int vp6_parse_coeff(VP56Context *s);
46 static int vp6_parse_coeff_huffman(VP56Context *s);
47 
48 static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
49 {
50  VPXRangeCoder *c = &s->c;
51  int parse_filter_info = 0;
52  int coeff_offset = 0;
53  int vrt_shift = 0;
54  int sub_version;
55  int rows, cols;
56  int res = 0;
57  int ret;
58  int separated_coeff = buf[0] & 1;
59 
60  if (!(buf[0] & 0x80))
61  s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY;
62  else
63  s->frames[VP56_FRAME_CURRENT]->flags &= ~AV_FRAME_FLAG_KEY;
64  ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
65 
66  if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
67  sub_version = buf[1] >> 3;
68  if (sub_version > 8)
69  return AVERROR_INVALIDDATA;
70  s->filter_header = buf[1] & 0x06;
71  s->interlaced = buf[1] & 1;
72  if (s->interlaced)
73  s->def_coeff_reorder = vp6_il_coeff_reorder;
74  else
75  s->def_coeff_reorder = vp6_def_coeff_reorder;
76  if (separated_coeff || !s->filter_header) {
77  coeff_offset = AV_RB16(buf+2) - 2;
78  buf += 2;
79  buf_size -= 2;
80  }
81 
82  rows = buf[2]; /* number of stored macroblock rows */
83  cols = buf[3]; /* number of stored macroblock cols */
84  /* buf[4] is number of displayed macroblock rows */
85  /* buf[5] is number of displayed macroblock cols */
86  if (!rows || !cols) {
87  av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
88  return AVERROR_INVALIDDATA;
89  }
90 
91  if (!s->macroblocks || /* first frame */
92  16*cols != s->avctx->coded_width ||
93  16*rows != s->avctx->coded_height) {
94  if (s->avctx->extradata_size == 0 &&
95  FFALIGN(s->avctx->width, 16) == 16 * cols &&
96  FFALIGN(s->avctx->height, 16) == 16 * rows) {
97  // We assume this is properly signalled container cropping,
98  // in an F4V file. Just set the coded_width/height, don't
99  // touch the cropped ones.
100  s->avctx->coded_width = 16 * cols;
101  s->avctx->coded_height = 16 * rows;
102  } else {
103  ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
104  if (ret < 0)
105  return ret;
106 
107  if (s->avctx->extradata_size == 1) {
108  s->avctx->width -= s->avctx->extradata[0] >> 4;
109  s->avctx->height -= s->avctx->extradata[0] & 0x0F;
110  }
111  }
112  res = VP56_SIZE_CHANGE;
113  }
114 
115  ret = ff_vpx_init_range_decoder(c, buf+6, buf_size-6);
116  if (ret < 0)
117  goto fail;
118  vp56_rac_gets(c, 2);
119 
120  parse_filter_info = s->filter_header;
121  if (sub_version < 8)
122  vrt_shift = 5;
123  s->sub_version = sub_version;
124  s->golden_frame = 0;
125  } else {
126  if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
127  return AVERROR_INVALIDDATA;
128 
129  if (separated_coeff || !s->filter_header) {
130  coeff_offset = AV_RB16(buf+1) - 2;
131  buf += 2;
132  buf_size -= 2;
133  }
134  ret = ff_vpx_init_range_decoder(c, buf+1, buf_size-1);
135  if (ret < 0)
136  return ret;
137 
138  s->golden_frame = vpx_rac_get(c);
139  if (s->filter_header) {
140  s->deblock_filtering = vpx_rac_get(c);
141  if (s->deblock_filtering)
142  vpx_rac_get(c);
143  if (s->sub_version > 7)
144  parse_filter_info = vpx_rac_get(c);
145  }
146  }
147 
148  if (parse_filter_info) {
149  if (vpx_rac_get(c)) {
150  s->filter_mode = 2;
151  s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
152  s->max_vector_length = 2 << vp56_rac_gets(c, 3);
153  } else if (vpx_rac_get(c)) {
154  s->filter_mode = 1;
155  } else {
156  s->filter_mode = 0;
157  }
158  if (s->sub_version > 7)
159  s->filter_selection = vp56_rac_gets(c, 4);
160  else
161  s->filter_selection = 16;
162  }
163 
164  s->use_huffman = vpx_rac_get(c);
165 
166  s->parse_coeff = vp6_parse_coeff;
167  if (coeff_offset) {
168  buf += coeff_offset;
169  buf_size -= coeff_offset;
170  if (buf_size < 0) {
172  goto fail;
173  }
174  if (s->use_huffman) {
175  s->parse_coeff = vp6_parse_coeff_huffman;
176  ret = init_get_bits8(&s->gb, buf, buf_size);
177  if (ret < 0)
178  return ret;
179  } else {
180  ret = ff_vpx_init_range_decoder(&s->cc, buf, buf_size);
181  if (ret < 0)
182  goto fail;
183  s->ccp = &s->cc;
184  }
185  } else {
186  s->ccp = &s->c;
187  }
188 
189  return res;
190 fail:
191  if (res == VP56_SIZE_CHANGE)
192  ff_set_dimensions(s->avctx, 0, 0);
193  return ret;
194 }
195 
196 static void vp6_coeff_order_table_init(VP56Context *s)
197 {
198  int i, pos, idx = 1;
199 
200  s->modelp->coeff_index_to_pos[0] = 0;
201  for (i=0; i<16; i++)
202  for (pos=1; pos<64; pos++)
203  if (s->modelp->coeff_reorder[pos] == i)
204  s->modelp->coeff_index_to_pos[idx++] = pos;
205 
206  for (idx = 0; idx < 64; idx++) {
207  int max = 0;
208  for (i = 0; i <= idx; i++) {
209  int v = s->modelp->coeff_index_to_pos[i];
210  if (v > max)
211  max = v;
212  }
213  if (s->sub_version > 6)
214  max++;
215  s->modelp->coeff_index_to_idct_selector[idx] = max;
216  }
217 }
218 
219 static void vp6_default_models_init(VP56Context *s)
220 {
221  VP56Model *model = s->modelp;
222 
223  model->vector_dct[0] = 0xA2;
224  model->vector_dct[1] = 0xA4;
225  model->vector_sig[0] = 0x80;
226  model->vector_sig[1] = 0x80;
227 
228  memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
229  memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
230  memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
231  memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
232  memcpy(model->coeff_reorder, s->def_coeff_reorder, sizeof(model->coeff_reorder));
233 
235 }
236 
237 static void vp6_parse_vector_models(VP56Context *s)
238 {
239  VPXRangeCoder *c = &s->c;
240  VP56Model *model = s->modelp;
241  int comp, node;
242 
243  for (comp=0; comp<2; comp++) {
245  model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
247  model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
248  }
249 
250  for (comp=0; comp<2; comp++)
251  for (node=0; node<7; node++)
253  model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
254 
255  for (comp=0; comp<2; comp++)
256  for (node=0; node<8; node++)
258  model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
259 }
260 
261 /* nodes must ascend by count, but with descending symbol order */
262 static int vp6_huff_cmp(const void *va, const void *vb)
263 {
264  const Node *a = va, *b = vb;
265  return (a->count - b->count)*16 + (b->sym - a->sym);
266 }
267 
268 static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
269  const uint8_t *map, unsigned size, VLC *vlc)
270 {
271  Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
272  int a, b, i;
273 
274  /* first compute probabilities from model */
275  tmp[0].count = 256;
276  for (i=0; i<size-1; i++) {
277  a = tmp[i].count * coeff_model[i] >> 8;
278  b = tmp[i].count * (255 - coeff_model[i]) >> 8;
279  nodes[map[2*i ]].count = a + !a;
280  nodes[map[2*i+1]].count = b + !b;
281  }
282 
283  ff_vlc_free(vlc);
284  /* then build the huffman tree according to probabilities */
285  return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
286  nodes, vp6_huff_cmp,
288 }
289 
290 static int vp6_parse_coeff_models(VP56Context *s)
291 {
292  VPXRangeCoder *c = &s->c;
293  VP56Model *model = s->modelp;
294  int def_prob[11];
295  int node, cg, ctx, pos;
296  int ct; /* code type */
297  int pt; /* plane type (0 for Y, 1 for U or V) */
298 
299  memset(def_prob, 0x80, sizeof(def_prob));
300 
301  for (pt=0; pt<2; pt++)
302  for (node=0; node<11; node++)
303  if (vpx_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
304  def_prob[node] = vp56_rac_gets_nn(c, 7);
305  model->coeff_dccv[pt][node] = def_prob[node];
306  } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
307  model->coeff_dccv[pt][node] = def_prob[node];
308  }
309 
310  if (vpx_rac_get(c)) {
311  for (pos=1; pos<64; pos++)
313  model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
315  }
316 
317  for (cg=0; cg<2; cg++)
318  for (node=0; node<14; node++)
319  if (vpx_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
320  model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
321 
322  for (ct=0; ct<3; ct++)
323  for (pt=0; pt<2; pt++)
324  for (cg=0; cg<6; cg++)
325  for (node=0; node<11; node++)
326  if (vpx_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
327  def_prob[node] = vp56_rac_gets_nn(c, 7);
328  model->coeff_ract[pt][ct][cg][node] = def_prob[node];
329  } else if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY) {
330  model->coeff_ract[pt][ct][cg][node] = def_prob[node];
331  }
332 
333  if (s->use_huffman) {
334  for (pt=0; pt<2; pt++) {
335  if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
336  vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
337  return -1;
338  if (vp6_build_huff_tree(s, model->coeff_runv[pt],
339  vp6_huff_run_map, 9, &s->runv_vlc[pt]))
340  return -1;
341  for (ct=0; ct<3; ct++)
342  for (cg = 0; cg < 6; cg++)
343  if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
344  vp6_huff_coeff_map, 12,
345  &s->ract_vlc[pt][ct][cg]))
346  return -1;
347  }
348  memset(s->nb_null, 0, sizeof(s->nb_null));
349  } else {
350  /* coeff_dcct is a linear combination of coeff_dccv */
351  for (pt=0; pt<2; pt++)
352  for (ctx=0; ctx<3; ctx++)
353  for (node=0; node<5; node++)
354  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);
355  }
356  return 0;
357 }
358 
359 static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
360 {
361  VPXRangeCoder *c = &s->c;
362  VP56Model *model = s->modelp;
363  int comp;
364 
365  *vect = (VP56mv) {0,0};
366  if (s->vector_candidate_pos < 2)
367  *vect = s->vector_candidate[0];
368 
369  for (comp=0; comp<2; comp++) {
370  int i, delta = 0;
371 
372  if (vpx_rac_get_prob_branchy(c, model->vector_dct[comp])) {
373  static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
374  for (i=0; i<sizeof(prob_order); i++) {
375  int j = prob_order[i];
376  delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
377  }
378  if (delta & 0xF0)
379  delta |= vpx_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
380  else
381  delta |= 8;
382  } else {
384  model->vector_pdv[comp]);
385  }
386 
388  delta = -delta;
389 
390  if (!comp)
391  vect->x += delta;
392  else
393  vect->y += delta;
394  }
395 }
396 
397 /**
398  * Read number of consecutive blocks with null DC or AC.
399  * This value is < 74.
400  */
401 static unsigned vp6_get_nb_null(VP56Context *s)
402 {
403  unsigned val = get_bits(&s->gb, 2);
404  if (val == 2)
405  val += get_bits(&s->gb, 2);
406  else if (val == 3) {
407  val = get_bits1(&s->gb) << 2;
408  val = 6+val + get_bits(&s->gb, 2+val);
409  }
410  return val;
411 }
412 
413 static int vp6_parse_coeff_huffman(VP56Context *s)
414 {
415  VP56Model *model = s->modelp;
416  uint8_t *permute = s->idct_scantable;
417  VLC *vlc_coeff;
418  int coeff, sign, coeff_idx;
419  int b, cg, idx;
420  int pt = 0; /* plane type (0 for Y, 1 for U or V) */
421 
422  for (b=0; b<6; b++) {
423  int ct = 0; /* code type */
424  if (b > 3) pt = 1;
425  vlc_coeff = &s->dccv_vlc[pt];
426 
427  for (coeff_idx = 0;;) {
428  int run = 1;
429  if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
430  s->nb_null[coeff_idx][pt]--;
431  if (coeff_idx)
432  break;
433  } else {
434  if (get_bits_left(&s->gb) <= 0)
435  return AVERROR_INVALIDDATA;
436  coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
437  if (coeff == 0) {
438  if (coeff_idx) {
439  int pt = (coeff_idx >= 6);
440  run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
441  if (run >= 9)
442  run += get_bits(&s->gb, 6);
443  } else
444  s->nb_null[0][pt] = vp6_get_nb_null(s);
445  ct = 0;
446  } else if (coeff == 11) { /* end of block */
447  if (coeff_idx == 1) /* first AC coeff ? */
448  s->nb_null[1][pt] = vp6_get_nb_null(s);
449  break;
450  } else {
451  int coeff2 = ff_vp56_coeff_bias[coeff];
452  if (coeff > 4)
453  coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
454  ct = 1 + (coeff2 > 1);
455  sign = get_bits1(&s->gb);
456  coeff2 = (coeff2 ^ -sign) + sign;
457  if (coeff_idx)
458  coeff2 *= s->dequant_ac;
459  idx = model->coeff_index_to_pos[coeff_idx];
460  s->block_coeff[b][permute[idx]] = coeff2;
461  }
462  }
463  coeff_idx+=run;
464  if (coeff_idx >= 64)
465  break;
466  cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
467  vlc_coeff = &s->ract_vlc[pt][ct][cg];
468  }
469  s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
470  }
471  return 0;
472 }
473 
474 static int vp6_parse_coeff(VP56Context *s)
475 {
476  VPXRangeCoder *c = s->ccp;
477  VP56Model *model = s->modelp;
478  uint8_t *permute = s->idct_scantable;
479  uint8_t *model1, *model2, *model3;
480  int coeff, sign, coeff_idx;
481  int b, i, cg, idx, ctx;
482  int pt = 0; /* plane type (0 for Y, 1 for U or V) */
483 
484  if (vpx_rac_is_end(c)) {
485  av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
486  return AVERROR_INVALIDDATA;
487  }
488 
489  for (b=0; b<6; b++) {
490  int ct = 1; /* code type */
491  int run = 1;
492 
493  if (b > 3) pt = 1;
494 
495  ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
496  + s->above_blocks[s->above_block_idx[b]].not_null_dc;
497  model1 = model->coeff_dccv[pt];
498  model2 = model->coeff_dcct[pt][ctx];
499 
500  coeff_idx = 0;
501  for (;;) {
502  if ((coeff_idx>1 && ct==0) || vpx_rac_get_prob_branchy(c, model2[0])) {
503  /* parse a coeff */
504  if (vpx_rac_get_prob_branchy(c, model2[2])) {
505  if (vpx_rac_get_prob_branchy(c, model2[3])) {
506  idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
507  coeff = ff_vp56_coeff_bias[idx+5];
508  for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
510  } else {
511  if (vpx_rac_get_prob_branchy(c, model2[4]))
512  coeff = 3 + vpx_rac_get_prob(c, model1[5]);
513  else
514  coeff = 2;
515  }
516  ct = 2;
517  } else {
518  ct = 1;
519  coeff = 1;
520  }
521  sign = vpx_rac_get(c);
522  coeff = (coeff ^ -sign) + sign;
523  if (coeff_idx)
524  coeff *= s->dequant_ac;
525  idx = model->coeff_index_to_pos[coeff_idx];
526  s->block_coeff[b][permute[idx]] = coeff;
527  run = 1;
528  } else {
529  /* parse a run */
530  ct = 0;
531  if (coeff_idx > 0) {
532  if (!vpx_rac_get_prob_branchy(c, model2[1]))
533  break;
534 
535  model3 = model->coeff_runv[coeff_idx >= 6];
536  run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
537  if (!run)
538  for (run=9, i=0; i<6; i++)
539  run += vpx_rac_get_prob(c, model3[i+8]) << i;
540  }
541  }
542  coeff_idx += run;
543  if (coeff_idx >= 64)
544  break;
545  cg = vp6_coeff_groups[coeff_idx];
546  model1 = model2 = model->coeff_ract[pt][ct][cg];
547  }
548 
549  s->left_block[ff_vp56_b6to4[b]].not_null_dc =
550  s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
551  s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
552  }
553  return 0;
554 }
555 
556 static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
557 {
558  int sum = 0, square_sum = 0;
559  int y, x;
560 
561  for (y=0; y<8; y+=2) {
562  for (x=0; x<8; x+=2) {
563  sum += src[x];
564  square_sum += src[x]*src[x];
565  }
566  src += 2*stride;
567  }
568  return (16*square_sum - sum*sum) >> 8;
569 }
570 
571 static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
572  int delta, const int16_t *weights)
573 {
574  int x, y;
575 
576  for (y=0; y<8; y++) {
577  for (x=0; x<8; x++) {
578  dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
579  + src[x ] * weights[1]
580  + src[x+delta ] * weights[2]
581  + src[x+2*delta] * weights[3] + 64) >> 7);
582  }
583  src += stride;
584  dst += stride;
585  }
586 }
587 
588 static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
589  ptrdiff_t stride, int h_weight, int v_weight)
590 {
591  uint8_t *tmp = s->edge_emu_buffer+16;
592  s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
593  s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
594 }
595 
596 static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
597  int offset1, int offset2, ptrdiff_t stride,
598  VP56mv mv, int mask, int select, int luma)
599 {
600  int filter4 = 0;
601  int x8 = mv.x & mask;
602  int y8 = mv.y & mask;
603 
604  if (luma) {
605  x8 *= 2;
606  y8 *= 2;
607  filter4 = s->filter_mode;
608  if (filter4 == 2) {
609  if (s->max_vector_length &&
610  (FFABS(mv.x) > s->max_vector_length ||
611  FFABS(mv.y) > s->max_vector_length)) {
612  filter4 = 0;
613  } else if (s->sample_variance_threshold
614  && (vp6_block_variance(src+offset1, stride)
615  < s->sample_variance_threshold)) {
616  filter4 = 0;
617  }
618  }
619  }
620 
621  if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
622  offset1 = offset2;
623  }
624 
625  if (filter4) {
626  if (!y8) { /* left or right combine */
627  vp6_filter_hv4(dst, src+offset1, stride, 1,
628  vp6_block_copy_filter[select][x8]);
629  } else if (!x8) { /* above or below combine */
630  vp6_filter_hv4(dst, src+offset1, stride, stride,
631  vp6_block_copy_filter[select][y8]);
632  } else {
633  s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
634  vp6_block_copy_filter[select][x8],
635  vp6_block_copy_filter[select][y8]);
636  }
637  } else {
638  if (!x8 || !y8) {
639  s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
640  } else {
641  vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
642  }
643  }
644 }
645 
647  VP56Context *s, int flip, int has_alpha)
648 {
649  int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
650  if (ret < 0)
651  return ret;
652 
653  ff_vp6dsp_init(&s->vp56dsp);
654 
655  s->deblock_filtering = 0;
656  s->vp56_coord_div = vp6_coord_div;
657  s->parse_vector_adjustment = vp6_parse_vector_adjustment;
658  s->filter = vp6_filter;
659  s->default_models_init = vp6_default_models_init;
660  s->parse_vector_models = vp6_parse_vector_models;
661  s->parse_coeff_models = vp6_parse_coeff_models;
662  s->parse_header = vp6_parse_header;
663 
664  return 0;
665 }
666 
668 {
669  VP56Context *s = avctx->priv_data;
670  int ret;
671 
673  avctx->codec_id == AV_CODEC_ID_VP6A);
674  if (ret < 0)
675  return ret;
676 
677  if (s->has_alpha) {
678  /* Can only happen for ff_vp6a_decoder */
679  s->alpha_context = &s[1];
680  ret = vp6_decode_init_context(avctx, s->alpha_context,
681  s->flip == -1, s->has_alpha);
682  if (ret < 0)
683  return ret;
684  }
685 
686  return 0;
687 }
688 
689 static av_cold void vp6_decode_free_context(VP56Context *s);
690 
692 {
693  VP56Context *s = avctx->priv_data;
694 
696 
697  if (s->alpha_context) {
698  vp6_decode_free_context(s->alpha_context);
699  s->alpha_context = NULL;
700  }
701 
702  return 0;
703 }
704 
705 static av_cold void vp6_decode_free_context(VP56Context *s)
706 {
707  int pt, ct, cg;
708 
710 
711  for (pt=0; pt<2; pt++) {
712  ff_vlc_free(&s->dccv_vlc[pt]);
713  ff_vlc_free(&s->runv_vlc[pt]);
714  for (ct=0; ct<3; ct++)
715  for (cg=0; cg<6; cg++)
716  ff_vlc_free(&s->ract_vlc[pt][ct][cg]);
717  }
718 }
719 
721  .p.name = "vp6",
722  CODEC_LONG_NAME("On2 VP6"),
723  .p.type = AVMEDIA_TYPE_VIDEO,
724  .p.id = AV_CODEC_ID_VP6,
725  .priv_data_size = sizeof(VP56Context),
727  .close = vp6_decode_free,
729  .p.capabilities = AV_CODEC_CAP_DR1,
730  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
731 };
732 
733 /* flash version, not flipped upside-down */
735  .p.name = "vp6f",
736  CODEC_LONG_NAME("On2 VP6 (Flash version)"),
737  .p.type = AVMEDIA_TYPE_VIDEO,
738  .p.id = AV_CODEC_ID_VP6F,
739  .priv_data_size = sizeof(VP56Context),
741  .close = vp6_decode_free,
743  .p.capabilities = AV_CODEC_CAP_DR1,
744  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
745 };
746 
747 /* flash version, not flipped upside-down, with alpha channel */
749  .p.name = "vp6a",
750  CODEC_LONG_NAME("On2 VP6 (Flash version, with alpha channel)"),
751  .p.type = AVMEDIA_TYPE_VIDEO,
752  .p.id = AV_CODEC_ID_VP6A,
753  .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
755  .close = vp6_decode_free,
757  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
758  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
759 };
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:43
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
vp6_decode_free
static av_cold int vp6_decode_free(AVCodecContext *avctx)
Definition: vp6.c:691
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:81
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:196
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
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
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:41
permute
static void permute(int16_t dst[64], const int16_t src[64], enum idct_permutation_type perm_type)
Definition: dct.c:152
FFCodec
Definition: codec_internal.h:127
ff_vp6f_decoder
const FFCodec ff_vp6f_decoder
Definition: vp6.c:734
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:588
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:94
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:720
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:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
fail
#define fail()
Definition: checkasm.h:188
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:262
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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:545
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:640
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:311
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:748
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
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:296
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
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:401
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
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:48
vp6_decode_init_context
static av_cold int vp6_decode_init_context(AVCodecContext *avctx, VP56Context *s, int flip, int has_alpha)
Definition: vp6.c:646
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
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:359
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:652
vp6_coord_div
static const uint8_t vp6_coord_div[]
Definition: vp6data.h:313
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:571
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:219
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
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:290
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:114
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
FF_HUFFMAN_BITS
#define FF_HUFFMAN_BITS
Definition: huffman.h:41
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:667
vpx_rac.h
vp6_il_coeff_reorder
static const uint8_t vp6_il_coeff_reorder[]
Definition: vp6data.h:54
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:194
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
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:159
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:596
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:451
vp6_block_variance
static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
Definition: vp6.c:556
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:237
VP56Model
Definition: vp56.h:97
VLC
Definition: vlc.h:36
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:38
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
vp6_build_huff_tree
static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], const uint8_t *map, unsigned size, VLC *vlc)
Definition: vp6.c:268
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:478
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:413
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:705
vp6_parse_coeff
static int vp6_parse_coeff(VP56Context *s)
Definition: vp6.c:474
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