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