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