FFmpeg
vp3.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2004 The FFmpeg project
3  * Copyright (C) 2019 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * On2 VP3/VP4 Video Decoder
25  *
26  * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
27  * For more information about the VP3 coding process, visit:
28  * http://wiki.multimedia.cx/index.php?title=On2_VP3
29  *
30  * Theora decoder by Alex Beregszaszi
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "libavutil/imgutils.h"
38 #include "libavutil/mem_internal.h"
39 
40 #include "avcodec.h"
41 #include "get_bits.h"
42 #include "hpeldsp.h"
43 #include "internal.h"
44 #include "mathops.h"
45 #include "thread.h"
46 #include "videodsp.h"
47 #include "vp3data.h"
48 #include "vp4data.h"
49 #include "vp3dsp.h"
50 #include "xiph.h"
51 
52 #define VP3_MV_VLC_BITS 6
53 #define VP4_MV_VLC_BITS 6
54 #define SUPERBLOCK_VLC_BITS 6
55 
56 #define FRAGMENT_PIXELS 8
57 
58 // FIXME split things out into their own arrays
59 typedef struct Vp3Fragment {
60  int16_t dc;
61  uint8_t coding_method;
62  uint8_t qpi;
63 } Vp3Fragment;
64 
65 #define SB_NOT_CODED 0
66 #define SB_PARTIALLY_CODED 1
67 #define SB_FULLY_CODED 2
68 
69 // This is the maximum length of a single long bit run that can be encoded
70 // for superblock coding or block qps. Theora special-cases this to read a
71 // bit instead of flipping the current bit to allow for runs longer than 4129.
72 #define MAXIMUM_LONG_BIT_RUN 4129
73 
74 #define MODE_INTER_NO_MV 0
75 #define MODE_INTRA 1
76 #define MODE_INTER_PLUS_MV 2
77 #define MODE_INTER_LAST_MV 3
78 #define MODE_INTER_PRIOR_LAST 4
79 #define MODE_USING_GOLDEN 5
80 #define MODE_GOLDEN_MV 6
81 #define MODE_INTER_FOURMV 7
82 #define CODING_MODE_COUNT 8
83 
84 /* special internal mode */
85 #define MODE_COPY 8
86 
87 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
88 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
89 
90 
91 /* There are 6 preset schemes, plus a free-form scheme */
92 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
93  /* scheme 1: Last motion vector dominates */
98 
99  /* scheme 2 */
104 
105  /* scheme 3 */
110 
111  /* scheme 4 */
116 
117  /* scheme 5: No motion vector dominates */
122 
123  /* scheme 6 */
128 };
129 
130 static const uint8_t hilbert_offset[16][2] = {
131  { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
132  { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
133  { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
134  { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
135 };
136 
137 enum {
143 };
144 
145 static const uint8_t vp4_pred_block_type_map[8] = {
154 };
155 
156 typedef struct {
157  int dc;
158  int type;
159 } VP4Predictor;
160 
161 #define MIN_DEQUANT_VAL 2
162 
163 typedef struct HuffEntry {
164  uint8_t len, sym;
165 } HuffEntry;
166 
167 typedef struct HuffTable {
169  uint8_t nb_entries;
170 } HuffTable;
171 
172 typedef struct Vp3DecodeContext {
175  int version;
176  int width, height;
181  int keyframe;
182  uint8_t idct_permutation[64];
183  uint8_t idct_scantable[64];
187  DECLARE_ALIGNED(16, int16_t, block)[64];
191 
192  int qps[3];
193  int nqps;
194  int last_qps[3];
195 
205  unsigned char *superblock_coding;
206 
207  int macroblock_count; /* y macroblock count */
213  int yuv_macroblock_count; /* y+u+v macroblock count */
214 
218 
221  int data_offset[3];
222  uint8_t offset_x;
223  uint8_t offset_y;
225 
226  int8_t (*motion_val[2])[2];
227 
228  /* tables */
229  uint16_t coded_dc_scale_factor[2][64];
230  uint32_t coded_ac_scale_factor[64];
231  uint8_t base_matrix[384][64];
232  uint8_t qr_count[2][3];
233  uint8_t qr_size[2][3][64];
234  uint16_t qr_base[2][3][64];
235 
236  /**
237  * This is a list of all tokens in bitstream order. Reordering takes place
238  * by pulling from each level during IDCT. As a consequence, IDCT must be
239  * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
240  * otherwise. The 32 different tokens with up to 12 bits of extradata are
241  * collapsed into 3 types, packed as follows:
242  * (from the low to high bits)
243  *
244  * 2 bits: type (0,1,2)
245  * 0: EOB run, 14 bits for run length (12 needed)
246  * 1: zero run, 7 bits for run length
247  * 7 bits for the next coefficient (3 needed)
248  * 2: coefficient, 14 bits (11 needed)
249  *
250  * Coefficients are signed, so are packed in the highest bits for automatic
251  * sign extension.
252  */
253  int16_t *dct_tokens[3][64];
254  int16_t *dct_tokens_base;
255 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
256 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
257 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
258 
259  /**
260  * number of blocks that contain DCT coefficients at
261  * the given level or higher
262  */
263  int num_coded_frags[3][64];
265 
266  /* this is a list of indexes into the all_fragments array indicating
267  * which of the fragments are coded */
269 
273 
274  /* The first 16 of the following VLCs are for the dc coefficients;
275  the others are four groups of 16 VLCs each for ac coefficients. */
276  VLC coeff_vlc[5 * 16];
277 
278  VLC superblock_run_length_vlc; /* version < 2 */
279  VLC fragment_run_length_vlc; /* version < 2 */
280  VLC block_pattern_vlc[2]; /* version >= 2*/
282  VLC motion_vector_vlc; /* version < 2 */
283  VLC vp4_mv_vlc[2][7]; /* version >=2 */
284 
285  /* these arrays need to be on 16-byte boundaries since SSE2 operations
286  * index into them */
287  DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
288 
289  /* This table contains superblock_count * 16 entries. Each set of 16
290  * numbers corresponds to the fragment indexes 0..15 of the superblock.
291  * An entry will be -1 to indicate that no entry corresponds to that
292  * index. */
294 
295  /* This is an array that indicates how a particular macroblock
296  * is coded. */
297  unsigned char *macroblock_coding;
298 
299  uint8_t *edge_emu_buffer;
300 
301  /* Huffman decode */
303 
304  uint8_t filter_limit_values[64];
306 
307  VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
309 
310 /************************************************************************
311  * VP3 specific functions
312  ************************************************************************/
313 
314 static av_cold void free_tables(AVCodecContext *avctx)
315 {
316  Vp3DecodeContext *s = avctx->priv_data;
317 
318  av_freep(&s->superblock_coding);
319  av_freep(&s->all_fragments);
320  av_freep(&s->nkf_coded_fragment_list);
321  av_freep(&s->kf_coded_fragment_list);
322  av_freep(&s->dct_tokens_base);
323  av_freep(&s->superblock_fragments);
324  av_freep(&s->macroblock_coding);
325  av_freep(&s->dc_pred_row);
326  av_freep(&s->motion_val[0]);
327  av_freep(&s->motion_val[1]);
328 }
329 
330 static void vp3_decode_flush(AVCodecContext *avctx)
331 {
332  Vp3DecodeContext *s = avctx->priv_data;
333 
334  if (s->golden_frame.f)
335  ff_thread_release_buffer(avctx, &s->golden_frame);
336  if (s->last_frame.f)
337  ff_thread_release_buffer(avctx, &s->last_frame);
338  if (s->current_frame.f)
339  ff_thread_release_buffer(avctx, &s->current_frame);
340 }
341 
343 {
344  Vp3DecodeContext *s = avctx->priv_data;
345  int i, j;
346 
347  free_tables(avctx);
348  av_freep(&s->edge_emu_buffer);
349 
350  s->theora_tables = 0;
351 
352  /* release all frames */
353  vp3_decode_flush(avctx);
354  av_frame_free(&s->current_frame.f);
355  av_frame_free(&s->last_frame.f);
356  av_frame_free(&s->golden_frame.f);
357 
358  for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++)
359  ff_free_vlc(&s->coeff_vlc[i]);
360 
361  ff_free_vlc(&s->superblock_run_length_vlc);
362  ff_free_vlc(&s->fragment_run_length_vlc);
363  ff_free_vlc(&s->mode_code_vlc);
364  ff_free_vlc(&s->motion_vector_vlc);
365 
366  for (j = 0; j < 2; j++)
367  for (i = 0; i < 7; i++)
368  ff_free_vlc(&s->vp4_mv_vlc[j][i]);
369 
370  for (i = 0; i < 2; i++)
371  ff_free_vlc(&s->block_pattern_vlc[i]);
372  return 0;
373 }
374 
375 /**
376  * This function sets up all of the various blocks mappings:
377  * superblocks <-> fragments, macroblocks <-> fragments,
378  * superblocks <-> macroblocks
379  *
380  * @return 0 is successful; returns 1 if *anything* went wrong.
381  */
383 {
384  int sb_x, sb_y, plane;
385  int x, y, i, j = 0;
386 
387  for (plane = 0; plane < 3; plane++) {
388  int sb_width = plane ? s->c_superblock_width
389  : s->y_superblock_width;
390  int sb_height = plane ? s->c_superblock_height
391  : s->y_superblock_height;
392  int frag_width = s->fragment_width[!!plane];
393  int frag_height = s->fragment_height[!!plane];
394 
395  for (sb_y = 0; sb_y < sb_height; sb_y++)
396  for (sb_x = 0; sb_x < sb_width; sb_x++)
397  for (i = 0; i < 16; i++) {
398  x = 4 * sb_x + hilbert_offset[i][0];
399  y = 4 * sb_y + hilbert_offset[i][1];
400 
401  if (x < frag_width && y < frag_height)
402  s->superblock_fragments[j++] = s->fragment_start[plane] +
403  y * frag_width + x;
404  else
405  s->superblock_fragments[j++] = -1;
406  }
407  }
408 
409  return 0; /* successful path out */
410 }
411 
412 /*
413  * This function sets up the dequantization tables used for a particular
414  * frame.
415  */
416 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
417 {
418  int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
419  int i, plane, inter, qri, bmi, bmj, qistart;
420 
421  for (inter = 0; inter < 2; inter++) {
422  for (plane = 0; plane < 3; plane++) {
423  int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
424  int sum = 0;
425  for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
426  sum += s->qr_size[inter][plane][qri];
427  if (s->qps[qpi] <= sum)
428  break;
429  }
430  qistart = sum - s->qr_size[inter][plane][qri];
431  bmi = s->qr_base[inter][plane][qri];
432  bmj = s->qr_base[inter][plane][qri + 1];
433  for (i = 0; i < 64; i++) {
434  int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
435  2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
436  s->qr_size[inter][plane][qri]) /
437  (2 * s->qr_size[inter][plane][qri]);
438 
439  int qmin = 8 << (inter + !i);
440  int qscale = i ? ac_scale_factor : dc_scale_factor;
441  int qbias = (1 + inter) * 3;
442  s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
443  (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
444  : (qscale * (coeff - qbias) / 100 + qbias) * 4;
445  }
446  /* all DC coefficients use the same quant so as not to interfere
447  * with DC prediction */
448  s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
449  }
450  }
451 }
452 
453 /*
454  * This function initializes the loop filter boundary limits if the frame's
455  * quality index is different from the previous frame's.
456  *
457  * The filter_limit_values may not be larger than 127.
458  */
460 {
461  ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
462 }
463 
464 /*
465  * This function unpacks all of the superblock/macroblock/fragment coding
466  * information from the bitstream.
467  */
469 {
470  int superblock_starts[3] = {
471  0, s->u_superblock_start, s->v_superblock_start
472  };
473  int bit = 0;
474  int current_superblock = 0;
475  int current_run = 0;
476  int num_partial_superblocks = 0;
477 
478  int i, j;
479  int current_fragment;
480  int plane;
481  int plane0_num_coded_frags = 0;
482 
483  if (s->keyframe) {
484  memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
485  } else {
486  /* unpack the list of partially-coded superblocks */
487  bit = get_bits1(gb) ^ 1;
488  current_run = 0;
489 
490  while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
491  if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
492  bit = get_bits1(gb);
493  else
494  bit ^= 1;
495 
496  current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
498  if (current_run == 34)
499  current_run += get_bits(gb, 12);
500 
501  if (current_run > s->superblock_count - current_superblock) {
502  av_log(s->avctx, AV_LOG_ERROR,
503  "Invalid partially coded superblock run length\n");
504  return -1;
505  }
506 
507  memset(s->superblock_coding + current_superblock, bit, current_run);
508 
509  current_superblock += current_run;
510  if (bit)
511  num_partial_superblocks += current_run;
512  }
513 
514  /* unpack the list of fully coded superblocks if any of the blocks were
515  * not marked as partially coded in the previous step */
516  if (num_partial_superblocks < s->superblock_count) {
517  int superblocks_decoded = 0;
518 
519  current_superblock = 0;
520  bit = get_bits1(gb) ^ 1;
521  current_run = 0;
522 
523  while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
524  get_bits_left(gb) > 0) {
525  if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
526  bit = get_bits1(gb);
527  else
528  bit ^= 1;
529 
530  current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
532  if (current_run == 34)
533  current_run += get_bits(gb, 12);
534 
535  for (j = 0; j < current_run; current_superblock++) {
536  if (current_superblock >= s->superblock_count) {
537  av_log(s->avctx, AV_LOG_ERROR,
538  "Invalid fully coded superblock run length\n");
539  return -1;
540  }
541 
542  /* skip any superblocks already marked as partially coded */
543  if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
544  s->superblock_coding[current_superblock] = 2 * bit;
545  j++;
546  }
547  }
548  superblocks_decoded += current_run;
549  }
550  }
551 
552  /* if there were partial blocks, initialize bitstream for
553  * unpacking fragment codings */
554  if (num_partial_superblocks) {
555  current_run = 0;
556  bit = get_bits1(gb);
557  /* toggle the bit because as soon as the first run length is
558  * fetched the bit will be toggled again */
559  bit ^= 1;
560  }
561  }
562 
563  /* figure out which fragments are coded; iterate through each
564  * superblock (all planes) */
565  s->total_num_coded_frags = 0;
566  memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
567 
568  s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
569  : s->nkf_coded_fragment_list;
570 
571  for (plane = 0; plane < 3; plane++) {
572  int sb_start = superblock_starts[plane];
573  int sb_end = sb_start + (plane ? s->c_superblock_count
574  : s->y_superblock_count);
575  int num_coded_frags = 0;
576 
577  if (s->keyframe) {
578  if (s->num_kf_coded_fragment[plane] == -1) {
579  for (i = sb_start; i < sb_end; i++) {
580  /* iterate through all 16 fragments in a superblock */
581  for (j = 0; j < 16; j++) {
582  /* if the fragment is in bounds, check its coding status */
583  current_fragment = s->superblock_fragments[i * 16 + j];
584  if (current_fragment != -1) {
585  s->coded_fragment_list[plane][num_coded_frags++] =
586  current_fragment;
587  }
588  }
589  }
590  s->num_kf_coded_fragment[plane] = num_coded_frags;
591  } else
592  num_coded_frags = s->num_kf_coded_fragment[plane];
593  } else {
594  for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
595  if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
596  return AVERROR_INVALIDDATA;
597  }
598  /* iterate through all 16 fragments in a superblock */
599  for (j = 0; j < 16; j++) {
600  /* if the fragment is in bounds, check its coding status */
601  current_fragment = s->superblock_fragments[i * 16 + j];
602  if (current_fragment != -1) {
603  int coded = s->superblock_coding[i];
604 
605  if (coded == SB_PARTIALLY_CODED) {
606  /* fragment may or may not be coded; this is the case
607  * that cares about the fragment coding runs */
608  if (current_run-- == 0) {
609  bit ^= 1;
610  current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
611  }
612  coded = bit;
613  }
614 
615  if (coded) {
616  /* default mode; actual mode will be decoded in
617  * the next phase */
618  s->all_fragments[current_fragment].coding_method =
620  s->coded_fragment_list[plane][num_coded_frags++] =
621  current_fragment;
622  } else {
623  /* not coded; copy this fragment from the prior frame */
624  s->all_fragments[current_fragment].coding_method =
625  MODE_COPY;
626  }
627  }
628  }
629  }
630  }
631  if (!plane)
632  plane0_num_coded_frags = num_coded_frags;
633  s->total_num_coded_frags += num_coded_frags;
634  for (i = 0; i < 64; i++)
635  s->num_coded_frags[plane][i] = num_coded_frags;
636  if (plane < 2)
637  s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
638  num_coded_frags;
639  }
640  return 0;
641 }
642 
643 #define BLOCK_X (2 * mb_x + (k & 1))
644 #define BLOCK_Y (2 * mb_y + (k >> 1))
645 
646 #if CONFIG_VP4_DECODER
647 /**
648  * @return number of blocks, or > yuv_macroblock_count on error.
649  * return value is always >= 1.
650  */
651 static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
652 {
653  int v = 1;
654  int bits;
655  while ((bits = show_bits(gb, 9)) == 0x1ff) {
656  skip_bits(gb, 9);
657  v += 256;
658  if (v > s->yuv_macroblock_count) {
659  av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
660  return v;
661  }
662  }
663 #define body(n) { \
664  skip_bits(gb, 2 + n); \
665  v += (1 << n) + get_bits(gb, n); }
666 #define thresh(n) (0x200 - (0x80 >> n))
667 #define else_if(n) else if (bits < thresh(n)) body(n)
668  if (bits < 0x100) {
669  skip_bits(gb, 1);
670  } else if (bits < thresh(0)) {
671  skip_bits(gb, 2);
672  v += 1;
673  }
674  else_if(1)
675  else_if(2)
676  else_if(3)
677  else_if(4)
678  else_if(5)
679  else_if(6)
680  else body(7)
681 #undef body
682 #undef thresh
683 #undef else_if
684  return v;
685 }
686 
687 static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table)
688 {
689  int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2);
690  *next_block_pattern_table = vp4_block_pattern_table_selector[v];
691  return v + 1;
692 }
693 
694 static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
695 {
696  int plane, i, j, k, fragment;
697  int next_block_pattern_table;
698  int bit, current_run, has_partial;
699 
700  memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
701 
702  if (s->keyframe)
703  return 0;
704 
705  has_partial = 0;
706  bit = get_bits1(gb);
707  for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
708  if (get_bits_left(gb) <= 0)
709  return AVERROR_INVALIDDATA;
710  current_run = vp4_get_mb_count(s, gb);
711  if (current_run > s->yuv_macroblock_count - i)
712  return -1;
713  memset(s->superblock_coding + i, 2 * bit, current_run);
714  bit ^= 1;
715  has_partial |= bit;
716  }
717 
718  if (has_partial) {
719  if (get_bits_left(gb) <= 0)
720  return AVERROR_INVALIDDATA;
721  bit = get_bits1(gb);
722  current_run = vp4_get_mb_count(s, gb);
723  for (i = 0; i < s->yuv_macroblock_count; i++) {
724  if (!s->superblock_coding[i]) {
725  if (!current_run) {
726  bit ^= 1;
727  current_run = vp4_get_mb_count(s, gb);
728  }
729  s->superblock_coding[i] = bit;
730  current_run--;
731  }
732  }
733  if (current_run) /* handle situation when vp4_get_mb_count() fails */
734  return -1;
735  }
736 
737  next_block_pattern_table = 0;
738  i = 0;
739  for (plane = 0; plane < 3; plane++) {
740  int sb_x, sb_y;
741  int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
742  int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
743  int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
744  int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
745  int fragment_width = s->fragment_width[!!plane];
746  int fragment_height = s->fragment_height[!!plane];
747 
748  for (sb_y = 0; sb_y < sb_height; sb_y++) {
749  for (sb_x = 0; sb_x < sb_width; sb_x++) {
750  for (j = 0; j < 4; j++) {
751  int mb_x = 2 * sb_x + (j >> 1);
752  int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
753  int mb_coded, pattern, coded;
754 
755  if (mb_x >= mb_width || mb_y >= mb_height)
756  continue;
757 
758  mb_coded = s->superblock_coding[i++];
759 
760  if (mb_coded == SB_FULLY_CODED)
761  pattern = 0xF;
762  else if (mb_coded == SB_PARTIALLY_CODED)
763  pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
764  else
765  pattern = 0;
766 
767  for (k = 0; k < 4; k++) {
768  if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
769  continue;
770  fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
771  coded = pattern & (8 >> k);
772  /* MODE_INTER_NO_MV is the default for coded fragments.
773  the actual method is decoded in the next phase. */
774  s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
775  }
776  }
777  }
778  }
779  }
780  return 0;
781 }
782 #endif
783 
784 /*
785  * This function unpacks all the coding mode data for individual macroblocks
786  * from the bitstream.
787  */
789 {
790  int i, j, k, sb_x, sb_y;
791  int scheme;
792  int current_macroblock;
793  int current_fragment;
794  int coding_mode;
795  int custom_mode_alphabet[CODING_MODE_COUNT];
796  const int *alphabet;
797  Vp3Fragment *frag;
798 
799  if (s->keyframe) {
800  for (i = 0; i < s->fragment_count; i++)
801  s->all_fragments[i].coding_method = MODE_INTRA;
802  } else {
803  /* fetch the mode coding scheme for this frame */
804  scheme = get_bits(gb, 3);
805 
806  /* is it a custom coding scheme? */
807  if (scheme == 0) {
808  for (i = 0; i < 8; i++)
809  custom_mode_alphabet[i] = MODE_INTER_NO_MV;
810  for (i = 0; i < 8; i++)
811  custom_mode_alphabet[get_bits(gb, 3)] = i;
812  alphabet = custom_mode_alphabet;
813  } else
814  alphabet = ModeAlphabet[scheme - 1];
815 
816  /* iterate through all of the macroblocks that contain 1 or more
817  * coded fragments */
818  for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
819  for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
820  if (get_bits_left(gb) <= 0)
821  return -1;
822 
823  for (j = 0; j < 4; j++) {
824  int mb_x = 2 * sb_x + (j >> 1);
825  int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
826  current_macroblock = mb_y * s->macroblock_width + mb_x;
827 
828  if (mb_x >= s->macroblock_width ||
829  mb_y >= s->macroblock_height)
830  continue;
831 
832  /* coding modes are only stored if the macroblock has
833  * at least one luma block coded, otherwise it must be
834  * INTER_NO_MV */
835  for (k = 0; k < 4; k++) {
836  current_fragment = BLOCK_Y *
837  s->fragment_width[0] + BLOCK_X;
838  if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
839  break;
840  }
841  if (k == 4) {
842  s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
843  continue;
844  }
845 
846  /* mode 7 means get 3 bits for each coding mode */
847  if (scheme == 7)
848  coding_mode = get_bits(gb, 3);
849  else
850  coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
851 
852  s->macroblock_coding[current_macroblock] = coding_mode;
853  for (k = 0; k < 4; k++) {
854  frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
855  if (frag->coding_method != MODE_COPY)
856  frag->coding_method = coding_mode;
857  }
858 
859 #define SET_CHROMA_MODES \
860  if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
861  frag[s->fragment_start[1]].coding_method = coding_mode; \
862  if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
863  frag[s->fragment_start[2]].coding_method = coding_mode;
864 
865  if (s->chroma_y_shift) {
866  frag = s->all_fragments + mb_y *
867  s->fragment_width[1] + mb_x;
869  } else if (s->chroma_x_shift) {
870  frag = s->all_fragments +
871  2 * mb_y * s->fragment_width[1] + mb_x;
872  for (k = 0; k < 2; k++) {
874  frag += s->fragment_width[1];
875  }
876  } else {
877  for (k = 0; k < 4; k++) {
878  frag = s->all_fragments +
879  BLOCK_Y * s->fragment_width[1] + BLOCK_X;
881  }
882  }
883  }
884  }
885  }
886  }
887 
888  return 0;
889 }
890 
891 static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
892 {
893  int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table,
894  VP4_MV_VLC_BITS, 2);
895  return last_motion < 0 ? -v : v;
896 }
897 
898 /*
899  * This function unpacks all the motion vectors for the individual
900  * macroblocks from the bitstream.
901  */
903 {
904  int j, k, sb_x, sb_y;
905  int coding_mode;
906  int motion_x[4];
907  int motion_y[4];
908  int last_motion_x = 0;
909  int last_motion_y = 0;
910  int prior_last_motion_x = 0;
911  int prior_last_motion_y = 0;
912  int last_gold_motion_x = 0;
913  int last_gold_motion_y = 0;
914  int current_macroblock;
915  int current_fragment;
916  int frag;
917 
918  if (s->keyframe)
919  return 0;
920 
921  /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
922  coding_mode = s->version < 2 ? get_bits1(gb) : 2;
923 
924  /* iterate through all of the macroblocks that contain 1 or more
925  * coded fragments */
926  for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
927  for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
928  if (get_bits_left(gb) <= 0)
929  return -1;
930 
931  for (j = 0; j < 4; j++) {
932  int mb_x = 2 * sb_x + (j >> 1);
933  int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
934  current_macroblock = mb_y * s->macroblock_width + mb_x;
935 
936  if (mb_x >= s->macroblock_width ||
937  mb_y >= s->macroblock_height ||
938  s->macroblock_coding[current_macroblock] == MODE_COPY)
939  continue;
940 
941  switch (s->macroblock_coding[current_macroblock]) {
942  case MODE_GOLDEN_MV:
943  if (coding_mode == 2) { /* VP4 */
944  last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x);
945  last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y);
946  break;
947  } /* otherwise fall through */
948  case MODE_INTER_PLUS_MV:
949  /* all 6 fragments use the same motion vector */
950  if (coding_mode == 0) {
951  motion_x[0] = get_vlc2(gb, s->motion_vector_vlc.table,
952  VP3_MV_VLC_BITS, 2);
953  motion_y[0] = get_vlc2(gb, s->motion_vector_vlc.table,
954  VP3_MV_VLC_BITS, 2);
955  } else if (coding_mode == 1) {
956  motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
957  motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
958  } else { /* VP4 */
959  motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
960  motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
961  }
962 
963  /* vector maintenance, only on MODE_INTER_PLUS_MV */
964  if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
965  prior_last_motion_x = last_motion_x;
966  prior_last_motion_y = last_motion_y;
967  last_motion_x = motion_x[0];
968  last_motion_y = motion_y[0];
969  }
970  break;
971 
972  case MODE_INTER_FOURMV:
973  /* vector maintenance */
974  prior_last_motion_x = last_motion_x;
975  prior_last_motion_y = last_motion_y;
976 
977  /* fetch 4 vectors from the bitstream, one for each
978  * Y fragment, then average for the C fragment vectors */
979  for (k = 0; k < 4; k++) {
980  current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
981  if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
982  if (coding_mode == 0) {
983  motion_x[k] = get_vlc2(gb, s->motion_vector_vlc.table,
984  VP3_MV_VLC_BITS, 2);
985  motion_y[k] = get_vlc2(gb, s->motion_vector_vlc.table,
986  VP3_MV_VLC_BITS, 2);
987  } else if (coding_mode == 1) {
988  motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
989  motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
990  } else { /* VP4 */
991  motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
992  motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
993  }
994  last_motion_x = motion_x[k];
995  last_motion_y = motion_y[k];
996  } else {
997  motion_x[k] = 0;
998  motion_y[k] = 0;
999  }
1000  }
1001  break;
1002 
1003  case MODE_INTER_LAST_MV:
1004  /* all 6 fragments use the last motion vector */
1005  motion_x[0] = last_motion_x;
1006  motion_y[0] = last_motion_y;
1007 
1008  /* no vector maintenance (last vector remains the
1009  * last vector) */
1010  break;
1011 
1012  case MODE_INTER_PRIOR_LAST:
1013  /* all 6 fragments use the motion vector prior to the
1014  * last motion vector */
1015  motion_x[0] = prior_last_motion_x;
1016  motion_y[0] = prior_last_motion_y;
1017 
1018  /* vector maintenance */
1019  prior_last_motion_x = last_motion_x;
1020  prior_last_motion_y = last_motion_y;
1021  last_motion_x = motion_x[0];
1022  last_motion_y = motion_y[0];
1023  break;
1024 
1025  default:
1026  /* covers intra, inter without MV, golden without MV */
1027  motion_x[0] = 0;
1028  motion_y[0] = 0;
1029 
1030  /* no vector maintenance */
1031  break;
1032  }
1033 
1034  /* assign the motion vectors to the correct fragments */
1035  for (k = 0; k < 4; k++) {
1036  current_fragment =
1037  BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1038  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1039  s->motion_val[0][current_fragment][0] = motion_x[k];
1040  s->motion_val[0][current_fragment][1] = motion_y[k];
1041  } else {
1042  s->motion_val[0][current_fragment][0] = motion_x[0];
1043  s->motion_val[0][current_fragment][1] = motion_y[0];
1044  }
1045  }
1046 
1047  if (s->chroma_y_shift) {
1048  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1049  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1050  motion_x[2] + motion_x[3], 2);
1051  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1052  motion_y[2] + motion_y[3], 2);
1053  }
1054  if (s->version <= 2) {
1055  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1056  motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1057  }
1058  frag = mb_y * s->fragment_width[1] + mb_x;
1059  s->motion_val[1][frag][0] = motion_x[0];
1060  s->motion_val[1][frag][1] = motion_y[0];
1061  } else if (s->chroma_x_shift) {
1062  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1063  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1064  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1065  motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1066  motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1067  } else {
1068  motion_x[1] = motion_x[0];
1069  motion_y[1] = motion_y[0];
1070  }
1071  if (s->version <= 2) {
1072  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1073  motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1074  }
1075  frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1076  for (k = 0; k < 2; k++) {
1077  s->motion_val[1][frag][0] = motion_x[k];
1078  s->motion_val[1][frag][1] = motion_y[k];
1079  frag += s->fragment_width[1];
1080  }
1081  } else {
1082  for (k = 0; k < 4; k++) {
1083  frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1084  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1085  s->motion_val[1][frag][0] = motion_x[k];
1086  s->motion_val[1][frag][1] = motion_y[k];
1087  } else {
1088  s->motion_val[1][frag][0] = motion_x[0];
1089  s->motion_val[1][frag][1] = motion_y[0];
1090  }
1091  }
1092  }
1093  }
1094  }
1095  }
1096 
1097  return 0;
1098 }
1099 
1101 {
1102  int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
1103  int num_blocks = s->total_num_coded_frags;
1104 
1105  for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1106  i = blocks_decoded = num_blocks_at_qpi = 0;
1107 
1108  bit = get_bits1(gb) ^ 1;
1109  run_length = 0;
1110 
1111  do {
1112  if (run_length == MAXIMUM_LONG_BIT_RUN)
1113  bit = get_bits1(gb);
1114  else
1115  bit ^= 1;
1116 
1117  run_length = get_vlc2(gb, s->superblock_run_length_vlc.table,
1118  SUPERBLOCK_VLC_BITS, 2);
1119  if (run_length == 34)
1120  run_length += get_bits(gb, 12);
1121  blocks_decoded += run_length;
1122 
1123  if (!bit)
1124  num_blocks_at_qpi += run_length;
1125 
1126  for (j = 0; j < run_length; i++) {
1127  if (i >= s->total_num_coded_frags)
1128  return -1;
1129 
1130  if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1131  s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1132  j++;
1133  }
1134  }
1135  } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1136 
1137  num_blocks -= num_blocks_at_qpi;
1138  }
1139 
1140  return 0;
1141 }
1142 
1143 static inline int get_eob_run(GetBitContext *gb, int token)
1144 {
1145  int v = eob_run_table[token].base;
1146  if (eob_run_table[token].bits)
1147  v += get_bits(gb, eob_run_table[token].bits);
1148  return v;
1149 }
1150 
1151 static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1152 {
1153  int bits_to_get, zero_run;
1154 
1155  bits_to_get = coeff_get_bits[token];
1156  if (bits_to_get)
1157  bits_to_get = get_bits(gb, bits_to_get);
1158  *coeff = coeff_tables[token][bits_to_get];
1159 
1160  zero_run = zero_run_base[token];
1161  if (zero_run_get_bits[token])
1162  zero_run += get_bits(gb, zero_run_get_bits[token]);
1163 
1164  return zero_run;
1165 }
1166 
1167 /*
1168  * This function is called by unpack_dct_coeffs() to extract the VLCs from
1169  * the bitstream. The VLCs encode tokens which are used to unpack DCT
1170  * data. This function unpacks all the VLCs for either the Y plane or both
1171  * C planes, and is called for DC coefficients or different AC coefficient
1172  * levels (since different coefficient types require different VLC tables.
1173  *
1174  * This function returns a residual eob run. E.g, if a particular token gave
1175  * instructions to EOB the next 5 fragments and there were only 2 fragments
1176  * left in the current fragment range, 3 would be returned so that it could
1177  * be passed into the next call to this same function.
1178  */
1180  VLC *table, int coeff_index,
1181  int plane,
1182  int eob_run)
1183 {
1184  int i, j = 0;
1185  int token;
1186  int zero_run = 0;
1187  int16_t coeff = 0;
1188  int blocks_ended;
1189  int coeff_i = 0;
1190  int num_coeffs = s->num_coded_frags[plane][coeff_index];
1191  int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1192 
1193  /* local references to structure members to avoid repeated dereferences */
1194  int *coded_fragment_list = s->coded_fragment_list[plane];
1195  Vp3Fragment *all_fragments = s->all_fragments;
1196  VLC_TYPE(*vlc_table)[2] = table->table;
1197 
1198  if (num_coeffs < 0) {
1199  av_log(s->avctx, AV_LOG_ERROR,
1200  "Invalid number of coefficients at level %d\n", coeff_index);
1201  return AVERROR_INVALIDDATA;
1202  }
1203 
1204  if (eob_run > num_coeffs) {
1205  coeff_i =
1206  blocks_ended = num_coeffs;
1207  eob_run -= num_coeffs;
1208  } else {
1209  coeff_i =
1210  blocks_ended = eob_run;
1211  eob_run = 0;
1212  }
1213 
1214  // insert fake EOB token to cover the split between planes or zzi
1215  if (blocks_ended)
1216  dct_tokens[j++] = blocks_ended << 2;
1217 
1218  while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1219  /* decode a VLC into a token */
1220  token = get_vlc2(gb, vlc_table, 11, 3);
1221  /* use the token to get a zero run, a coefficient, and an eob run */
1222  if ((unsigned) token <= 6U) {
1223  eob_run = get_eob_run(gb, token);
1224  if (!eob_run)
1225  eob_run = INT_MAX;
1226 
1227  // record only the number of blocks ended in this plane,
1228  // any spill will be recorded in the next plane.
1229  if (eob_run > num_coeffs - coeff_i) {
1230  dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1231  blocks_ended += num_coeffs - coeff_i;
1232  eob_run -= num_coeffs - coeff_i;
1233  coeff_i = num_coeffs;
1234  } else {
1235  dct_tokens[j++] = TOKEN_EOB(eob_run);
1236  blocks_ended += eob_run;
1237  coeff_i += eob_run;
1238  eob_run = 0;
1239  }
1240  } else if (token >= 0) {
1241  zero_run = get_coeff(gb, token, &coeff);
1242 
1243  if (zero_run) {
1244  dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1245  } else {
1246  // Save DC into the fragment structure. DC prediction is
1247  // done in raster order, so the actual DC can't be in with
1248  // other tokens. We still need the token in dct_tokens[]
1249  // however, or else the structure collapses on itself.
1250  if (!coeff_index)
1251  all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1252 
1253  dct_tokens[j++] = TOKEN_COEFF(coeff);
1254  }
1255 
1256  if (coeff_index + zero_run > 64) {
1257  av_log(s->avctx, AV_LOG_DEBUG,
1258  "Invalid zero run of %d with %d coeffs left\n",
1259  zero_run, 64 - coeff_index);
1260  zero_run = 64 - coeff_index;
1261  }
1262 
1263  // zero runs code multiple coefficients,
1264  // so don't try to decode coeffs for those higher levels
1265  for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1266  s->num_coded_frags[plane][i]--;
1267  coeff_i++;
1268  } else {
1269  av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1270  return -1;
1271  }
1272  }
1273 
1274  if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1275  av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1276 
1277  // decrement the number of blocks that have higher coefficients for each
1278  // EOB run at this level
1279  if (blocks_ended)
1280  for (i = coeff_index + 1; i < 64; i++)
1281  s->num_coded_frags[plane][i] -= blocks_ended;
1282 
1283  // setup the next buffer
1284  if (plane < 2)
1285  s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1286  else if (coeff_index < 63)
1287  s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1288 
1289  return eob_run;
1290 }
1291 
1293  int first_fragment,
1294  int fragment_width,
1295  int fragment_height);
1296 /*
1297  * This function unpacks all of the DCT coefficient data from the
1298  * bitstream.
1299  */
1301 {
1302  int i;
1303  int dc_y_table;
1304  int dc_c_table;
1305  int ac_y_table;
1306  int ac_c_table;
1307  int residual_eob_run = 0;
1308  VLC *y_tables[64];
1309  VLC *c_tables[64];
1310 
1311  s->dct_tokens[0][0] = s->dct_tokens_base;
1312 
1313  if (get_bits_left(gb) < 16)
1314  return AVERROR_INVALIDDATA;
1315 
1316  /* fetch the DC table indexes */
1317  dc_y_table = get_bits(gb, 4);
1318  dc_c_table = get_bits(gb, 4);
1319 
1320  /* unpack the Y plane DC coefficients */
1321  residual_eob_run = unpack_vlcs(s, gb, &s->coeff_vlc[dc_y_table], 0,
1322  0, residual_eob_run);
1323  if (residual_eob_run < 0)
1324  return residual_eob_run;
1325  if (get_bits_left(gb) < 8)
1326  return AVERROR_INVALIDDATA;
1327 
1328  /* reverse prediction of the Y-plane DC coefficients */
1329  reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1330 
1331  /* unpack the C plane DC coefficients */
1332  residual_eob_run = unpack_vlcs(s, gb, &s->coeff_vlc[dc_c_table], 0,
1333  1, residual_eob_run);
1334  if (residual_eob_run < 0)
1335  return residual_eob_run;
1336  residual_eob_run = unpack_vlcs(s, gb, &s->coeff_vlc[dc_c_table], 0,
1337  2, residual_eob_run);
1338  if (residual_eob_run < 0)
1339  return residual_eob_run;
1340 
1341  /* reverse prediction of the C-plane DC coefficients */
1342  if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1343  reverse_dc_prediction(s, s->fragment_start[1],
1344  s->fragment_width[1], s->fragment_height[1]);
1345  reverse_dc_prediction(s, s->fragment_start[2],
1346  s->fragment_width[1], s->fragment_height[1]);
1347  }
1348 
1349  if (get_bits_left(gb) < 8)
1350  return AVERROR_INVALIDDATA;
1351  /* fetch the AC table indexes */
1352  ac_y_table = get_bits(gb, 4);
1353  ac_c_table = get_bits(gb, 4);
1354 
1355  /* build tables of AC VLC tables */
1356  for (i = 1; i <= 5; i++) {
1357  /* AC VLC table group 1 */
1358  y_tables[i] = &s->coeff_vlc[ac_y_table + 16];
1359  c_tables[i] = &s->coeff_vlc[ac_c_table + 16];
1360  }
1361  for (i = 6; i <= 14; i++) {
1362  /* AC VLC table group 2 */
1363  y_tables[i] = &s->coeff_vlc[ac_y_table + 32];
1364  c_tables[i] = &s->coeff_vlc[ac_c_table + 32];
1365  }
1366  for (i = 15; i <= 27; i++) {
1367  /* AC VLC table group 3 */
1368  y_tables[i] = &s->coeff_vlc[ac_y_table + 48];
1369  c_tables[i] = &s->coeff_vlc[ac_c_table + 48];
1370  }
1371  for (i = 28; i <= 63; i++) {
1372  /* AC VLC table group 4 */
1373  y_tables[i] = &s->coeff_vlc[ac_y_table + 64];
1374  c_tables[i] = &s->coeff_vlc[ac_c_table + 64];
1375  }
1376 
1377  /* decode all AC coefficients */
1378  for (i = 1; i <= 63; i++) {
1379  residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1380  0, residual_eob_run);
1381  if (residual_eob_run < 0)
1382  return residual_eob_run;
1383 
1384  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1385  1, residual_eob_run);
1386  if (residual_eob_run < 0)
1387  return residual_eob_run;
1388  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1389  2, residual_eob_run);
1390  if (residual_eob_run < 0)
1391  return residual_eob_run;
1392  }
1393 
1394  return 0;
1395 }
1396 
1397 #if CONFIG_VP4_DECODER
1398 /**
1399  * eob_tracker[] is instead of TOKEN_EOB(value)
1400  * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1401  *
1402  * @return < 0 on error
1403  */
1404 static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1405  VLC *vlc_tables[64],
1406  int plane, int eob_tracker[64], int fragment)
1407 {
1408  int token;
1409  int zero_run = 0;
1410  int16_t coeff = 0;
1411  int coeff_i = 0;
1412  int eob_run;
1413 
1414  while (!eob_tracker[coeff_i]) {
1415  if (get_bits_left(gb) < 1)
1416  return AVERROR_INVALIDDATA;
1417 
1418  token = get_vlc2(gb, vlc_tables[coeff_i]->table, 11, 3);
1419 
1420  /* use the token to get a zero run, a coefficient, and an eob run */
1421  if ((unsigned) token <= 6U) {
1422  eob_run = get_eob_run(gb, token);
1423  *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1424  eob_tracker[coeff_i] = eob_run - 1;
1425  return 0;
1426  } else if (token >= 0) {
1427  zero_run = get_coeff(gb, token, &coeff);
1428 
1429  if (zero_run) {
1430  if (coeff_i + zero_run > 64) {
1431  av_log(s->avctx, AV_LOG_DEBUG,
1432  "Invalid zero run of %d with %d coeffs left\n",
1433  zero_run, 64 - coeff_i);
1434  zero_run = 64 - coeff_i;
1435  }
1436  *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1437  coeff_i += zero_run;
1438  } else {
1439  if (!coeff_i)
1440  s->all_fragments[fragment].dc = coeff;
1441 
1442  *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1443  }
1444  coeff_i++;
1445  if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
1446  return 0; /* stop */
1447  } else {
1448  av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1449  return -1;
1450  }
1451  }
1452  *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1453  eob_tracker[coeff_i]--;
1454  return 0;
1455 }
1456 
1457 static void vp4_dc_predictor_reset(VP4Predictor *p)
1458 {
1459  p->dc = 0;
1460  p->type = VP4_DC_UNDEFINED;
1461 }
1462 
1463 static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1464 {
1465  int i, j;
1466 
1467  for (i = 0; i < 4; i++)
1468  dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1469 
1470  for (j = 1; j < 5; j++)
1471  for (i = 0; i < 4; i++)
1472  vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1473 }
1474 
1475 static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1476 {
1477  int i;
1478 
1479  for (i = 0; i < 4; i++)
1480  s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1481 
1482  for (i = 1; i < 5; i++)
1483  dc_pred[i][0] = dc_pred[i][4];
1484 }
1485 
1486 /* note: dc_pred points to the current block */
1487 static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1488 {
1489  int count = 0;
1490  int dc = 0;
1491 
1492  if (dc_pred[-6].type == type) {
1493  dc += dc_pred[-6].dc;
1494  count++;
1495  }
1496 
1497  if (dc_pred[6].type == type) {
1498  dc += dc_pred[6].dc;
1499  count++;
1500  }
1501 
1502  if (count != 2 && dc_pred[-1].type == type) {
1503  dc += dc_pred[-1].dc;
1504  count++;
1505  }
1506 
1507  if (count != 2 && dc_pred[1].type == type) {
1508  dc += dc_pred[1].dc;
1509  count++;
1510  }
1511 
1512  /* using division instead of shift to correctly handle negative values */
1513  return count == 2 ? dc / 2 : last_dc[type];
1514 }
1515 
1516 static void vp4_set_tokens_base(Vp3DecodeContext *s)
1517 {
1518  int plane, i;
1519  int16_t *base = s->dct_tokens_base;
1520  for (plane = 0; plane < 3; plane++) {
1521  for (i = 0; i < 64; i++) {
1522  s->dct_tokens[plane][i] = base;
1523  base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1524  }
1525  }
1526 }
1527 
1528 static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1529 {
1530  int i, j;
1531  int dc_y_table;
1532  int dc_c_table;
1533  int ac_y_table;
1534  int ac_c_table;
1535  VLC *tables[2][64];
1536  int plane, sb_y, sb_x;
1537  int eob_tracker[64];
1538  VP4Predictor dc_pred[6][6];
1539  int last_dc[NB_VP4_DC_TYPES];
1540 
1541  if (get_bits_left(gb) < 16)
1542  return AVERROR_INVALIDDATA;
1543 
1544  /* fetch the DC table indexes */
1545  dc_y_table = get_bits(gb, 4);
1546  dc_c_table = get_bits(gb, 4);
1547 
1548  ac_y_table = get_bits(gb, 4);
1549  ac_c_table = get_bits(gb, 4);
1550 
1551  /* build tables of DC/AC VLC tables */
1552 
1553  /* DC table group */
1554  tables[0][0] = &s->coeff_vlc[dc_y_table];
1555  tables[1][0] = &s->coeff_vlc[dc_c_table];
1556  for (i = 1; i <= 5; i++) {
1557  /* AC VLC table group 1 */
1558  tables[0][i] = &s->coeff_vlc[ac_y_table + 16];
1559  tables[1][i] = &s->coeff_vlc[ac_c_table + 16];
1560  }
1561  for (i = 6; i <= 14; i++) {
1562  /* AC VLC table group 2 */
1563  tables[0][i] = &s->coeff_vlc[ac_y_table + 32];
1564  tables[1][i] = &s->coeff_vlc[ac_c_table + 32];
1565  }
1566  for (i = 15; i <= 27; i++) {
1567  /* AC VLC table group 3 */
1568  tables[0][i] = &s->coeff_vlc[ac_y_table + 48];
1569  tables[1][i] = &s->coeff_vlc[ac_c_table + 48];
1570  }
1571  for (i = 28; i <= 63; i++) {
1572  /* AC VLC table group 4 */
1573  tables[0][i] = &s->coeff_vlc[ac_y_table + 64];
1574  tables[1][i] = &s->coeff_vlc[ac_c_table + 64];
1575  }
1576 
1577  vp4_set_tokens_base(s);
1578 
1579  memset(last_dc, 0, sizeof(last_dc));
1580 
1581  for (plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
1582  memset(eob_tracker, 0, sizeof(eob_tracker));
1583 
1584  /* initialise dc prediction */
1585  for (i = 0; i < s->fragment_width[!!plane]; i++)
1586  vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1587 
1588  for (j = 0; j < 6; j++)
1589  for (i = 0; i < 6; i++)
1590  vp4_dc_predictor_reset(&dc_pred[j][i]);
1591 
1592  for (sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
1593  for (sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
1594  vp4_dc_pred_before(s, dc_pred, sb_x);
1595  for (j = 0; j < 16; j++) {
1596  int hx = hilbert_offset[j][0];
1597  int hy = hilbert_offset[j][1];
1598  int x = 4 * sb_x + hx;
1599  int y = 4 * sb_y + hy;
1600  VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1601  int fragment, dc_block_type;
1602 
1603  if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
1604  continue;
1605 
1606  fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1607 
1608  if (s->all_fragments[fragment].coding_method == MODE_COPY)
1609  continue;
1610 
1611  if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
1612  return -1;
1613 
1614  dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1615 
1616  s->all_fragments[fragment].dc +=
1617  vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1618 
1619  this_dc_pred->type = dc_block_type,
1620  this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1621  }
1622  vp4_dc_pred_after(s, dc_pred, sb_x);
1623  }
1624  }
1625  }
1626 
1627  vp4_set_tokens_base(s);
1628 
1629  return 0;
1630 }
1631 #endif
1632 
1633 /*
1634  * This function reverses the DC prediction for each coded fragment in
1635  * the frame. Much of this function is adapted directly from the original
1636  * VP3 source code.
1637  */
1638 #define COMPATIBLE_FRAME(x) \
1639  (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1640 #define DC_COEFF(u) s->all_fragments[u].dc
1641 
1643  int first_fragment,
1644  int fragment_width,
1645  int fragment_height)
1646 {
1647 #define PUL 8
1648 #define PU 4
1649 #define PUR 2
1650 #define PL 1
1651 
1652  int x, y;
1653  int i = first_fragment;
1654 
1655  int predicted_dc;
1656 
1657  /* DC values for the left, up-left, up, and up-right fragments */
1658  int vl, vul, vu, vur;
1659 
1660  /* indexes for the left, up-left, up, and up-right fragments */
1661  int l, ul, u, ur;
1662 
1663  /*
1664  * The 6 fields mean:
1665  * 0: up-left multiplier
1666  * 1: up multiplier
1667  * 2: up-right multiplier
1668  * 3: left multiplier
1669  */
1670  static const int predictor_transform[16][4] = {
1671  { 0, 0, 0, 0 },
1672  { 0, 0, 0, 128 }, // PL
1673  { 0, 0, 128, 0 }, // PUR
1674  { 0, 0, 53, 75 }, // PUR|PL
1675  { 0, 128, 0, 0 }, // PU
1676  { 0, 64, 0, 64 }, // PU |PL
1677  { 0, 128, 0, 0 }, // PU |PUR
1678  { 0, 0, 53, 75 }, // PU |PUR|PL
1679  { 128, 0, 0, 0 }, // PUL
1680  { 0, 0, 0, 128 }, // PUL|PL
1681  { 64, 0, 64, 0 }, // PUL|PUR
1682  { 0, 0, 53, 75 }, // PUL|PUR|PL
1683  { 0, 128, 0, 0 }, // PUL|PU
1684  { -104, 116, 0, 116 }, // PUL|PU |PL
1685  { 24, 80, 24, 0 }, // PUL|PU |PUR
1686  { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1687  };
1688 
1689  /* This table shows which types of blocks can use other blocks for
1690  * prediction. For example, INTRA is the only mode in this table to
1691  * have a frame number of 0. That means INTRA blocks can only predict
1692  * from other INTRA blocks. There are 2 golden frame coding types;
1693  * blocks encoding in these modes can only predict from other blocks
1694  * that were encoded with these 1 of these 2 modes. */
1695  static const unsigned char compatible_frame[9] = {
1696  1, /* MODE_INTER_NO_MV */
1697  0, /* MODE_INTRA */
1698  1, /* MODE_INTER_PLUS_MV */
1699  1, /* MODE_INTER_LAST_MV */
1700  1, /* MODE_INTER_PRIOR_MV */
1701  2, /* MODE_USING_GOLDEN */
1702  2, /* MODE_GOLDEN_MV */
1703  1, /* MODE_INTER_FOUR_MV */
1704  3 /* MODE_COPY */
1705  };
1706  int current_frame_type;
1707 
1708  /* there is a last DC predictor for each of the 3 frame types */
1709  short last_dc[3];
1710 
1711  int transform = 0;
1712 
1713  vul =
1714  vu =
1715  vur =
1716  vl = 0;
1717  last_dc[0] =
1718  last_dc[1] =
1719  last_dc[2] = 0;
1720 
1721  /* for each fragment row... */
1722  for (y = 0; y < fragment_height; y++) {
1723  /* for each fragment in a row... */
1724  for (x = 0; x < fragment_width; x++, i++) {
1725 
1726  /* reverse prediction if this block was coded */
1727  if (s->all_fragments[i].coding_method != MODE_COPY) {
1728  current_frame_type =
1729  compatible_frame[s->all_fragments[i].coding_method];
1730 
1731  transform = 0;
1732  if (x) {
1733  l = i - 1;
1734  vl = DC_COEFF(l);
1735  if (COMPATIBLE_FRAME(l))
1736  transform |= PL;
1737  }
1738  if (y) {
1739  u = i - fragment_width;
1740  vu = DC_COEFF(u);
1741  if (COMPATIBLE_FRAME(u))
1742  transform |= PU;
1743  if (x) {
1744  ul = i - fragment_width - 1;
1745  vul = DC_COEFF(ul);
1746  if (COMPATIBLE_FRAME(ul))
1747  transform |= PUL;
1748  }
1749  if (x + 1 < fragment_width) {
1750  ur = i - fragment_width + 1;
1751  vur = DC_COEFF(ur);
1752  if (COMPATIBLE_FRAME(ur))
1753  transform |= PUR;
1754  }
1755  }
1756 
1757  if (transform == 0) {
1758  /* if there were no fragments to predict from, use last
1759  * DC saved */
1760  predicted_dc = last_dc[current_frame_type];
1761  } else {
1762  /* apply the appropriate predictor transform */
1763  predicted_dc =
1764  (predictor_transform[transform][0] * vul) +
1765  (predictor_transform[transform][1] * vu) +
1766  (predictor_transform[transform][2] * vur) +
1767  (predictor_transform[transform][3] * vl);
1768 
1769  predicted_dc /= 128;
1770 
1771  /* check for outranging on the [ul u l] and
1772  * [ul u ur l] predictors */
1773  if ((transform == 15) || (transform == 13)) {
1774  if (FFABS(predicted_dc - vu) > 128)
1775  predicted_dc = vu;
1776  else if (FFABS(predicted_dc - vl) > 128)
1777  predicted_dc = vl;
1778  else if (FFABS(predicted_dc - vul) > 128)
1779  predicted_dc = vul;
1780  }
1781  }
1782 
1783  /* at long last, apply the predictor */
1784  DC_COEFF(i) += predicted_dc;
1785  /* save the DC */
1786  last_dc[current_frame_type] = DC_COEFF(i);
1787  }
1788  }
1789  }
1790 }
1791 
1792 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1793  int ystart, int yend)
1794 {
1795  int x, y;
1796  int *bounding_values = s->bounding_values_array + 127;
1797 
1798  int width = s->fragment_width[!!plane];
1799  int height = s->fragment_height[!!plane];
1800  int fragment = s->fragment_start[plane] + ystart * width;
1801  ptrdiff_t stride = s->current_frame.f->linesize[plane];
1802  uint8_t *plane_data = s->current_frame.f->data[plane];
1803  if (!s->flipped_image)
1804  stride = -stride;
1805  plane_data += s->data_offset[plane] + 8 * ystart * stride;
1806 
1807  for (y = ystart; y < yend; y++) {
1808  for (x = 0; x < width; x++) {
1809  /* This code basically just deblocks on the edges of coded blocks.
1810  * However, it has to be much more complicated because of the
1811  * brain damaged deblock ordering used in VP3/Theora. Order matters
1812  * because some pixels get filtered twice. */
1813  if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1814  /* do not perform left edge filter for left columns frags */
1815  if (x > 0) {
1816  s->vp3dsp.h_loop_filter(
1817  plane_data + 8 * x,
1818  stride, bounding_values);
1819  }
1820 
1821  /* do not perform top edge filter for top row fragments */
1822  if (y > 0) {
1823  s->vp3dsp.v_loop_filter(
1824  plane_data + 8 * x,
1825  stride, bounding_values);
1826  }
1827 
1828  /* do not perform right edge filter for right column
1829  * fragments or if right fragment neighbor is also coded
1830  * in this frame (it will be filtered in next iteration) */
1831  if ((x < width - 1) &&
1832  (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1833  s->vp3dsp.h_loop_filter(
1834  plane_data + 8 * x + 8,
1835  stride, bounding_values);
1836  }
1837 
1838  /* do not perform bottom edge filter for bottom row
1839  * fragments or if bottom fragment neighbor is also coded
1840  * in this frame (it will be filtered in the next row) */
1841  if ((y < height - 1) &&
1842  (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1843  s->vp3dsp.v_loop_filter(
1844  plane_data + 8 * x + 8 * stride,
1845  stride, bounding_values);
1846  }
1847  }
1848 
1849  fragment++;
1850  }
1851  plane_data += 8 * stride;
1852  }
1853 }
1854 
1855 /**
1856  * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1857  * for the next block in coding order
1858  */
1859 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1860  int plane, int inter, int16_t block[64])
1861 {
1862  int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1863  uint8_t *perm = s->idct_scantable;
1864  int i = 0;
1865 
1866  do {
1867  int token = *s->dct_tokens[plane][i];
1868  switch (token & 3) {
1869  case 0: // EOB
1870  if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1871  s->dct_tokens[plane][i]++;
1872  else
1873  *s->dct_tokens[plane][i] = token & ~3;
1874  goto end;
1875  case 1: // zero run
1876  s->dct_tokens[plane][i]++;
1877  i += (token >> 2) & 0x7f;
1878  if (i > 63) {
1879  av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1880  return i;
1881  }
1882  block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1883  i++;
1884  break;
1885  case 2: // coeff
1886  block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1887  s->dct_tokens[plane][i++]++;
1888  break;
1889  default: // shouldn't happen
1890  return i;
1891  }
1892  } while (i < 64);
1893  // return value is expected to be a valid level
1894  i--;
1895 end:
1896  // the actual DC+prediction is in the fragment structure
1897  block[0] = frag->dc * s->qmat[0][inter][plane][0];
1898  return i;
1899 }
1900 
1901 /**
1902  * called when all pixels up to row y are complete
1903  */
1905 {
1906  int h, cy, i;
1908 
1909  if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1910  int y_flipped = s->flipped_image ? s->height - y : y;
1911 
1912  /* At the end of the frame, report INT_MAX instead of the height of
1913  * the frame. This makes the other threads' ff_thread_await_progress()
1914  * calls cheaper, because they don't have to clip their values. */
1915  ff_thread_report_progress(&s->current_frame,
1916  y_flipped == s->height ? INT_MAX
1917  : y_flipped - 1,
1918  0);
1919  }
1920 
1921  if (!s->avctx->draw_horiz_band)
1922  return;
1923 
1924  h = y - s->last_slice_end;
1925  s->last_slice_end = y;
1926  y -= h;
1927 
1928  if (!s->flipped_image)
1929  y = s->height - y - h;
1930 
1931  cy = y >> s->chroma_y_shift;
1932  offset[0] = s->current_frame.f->linesize[0] * y;
1933  offset[1] = s->current_frame.f->linesize[1] * cy;
1934  offset[2] = s->current_frame.f->linesize[2] * cy;
1935  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1936  offset[i] = 0;
1937 
1938  emms_c();
1939  s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1940 }
1941 
1942 /**
1943  * Wait for the reference frame of the current fragment.
1944  * The progress value is in luma pixel rows.
1945  */
1947  int motion_y, int y)
1948 {
1949  ThreadFrame *ref_frame;
1950  int ref_row;
1951  int border = motion_y & 1;
1952 
1953  if (fragment->coding_method == MODE_USING_GOLDEN ||
1954  fragment->coding_method == MODE_GOLDEN_MV)
1955  ref_frame = &s->golden_frame;
1956  else
1957  ref_frame = &s->last_frame;
1958 
1959  ref_row = y + (motion_y >> 1);
1960  ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1961 
1962  ff_thread_await_progress(ref_frame, ref_row, 0);
1963 }
1964 
1965 #if CONFIG_VP4_DECODER
1966 /**
1967  * @return non-zero if temp (edge_emu_buffer) was populated
1968  */
1969 static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1970  uint8_t * motion_source, int stride, int src_x, int src_y, uint8_t *temp)
1971 {
1972  int motion_shift = plane ? 4 : 2;
1973  int subpel_mask = plane ? 3 : 1;
1974  int *bounding_values = s->bounding_values_array + 127;
1975 
1976  int i;
1977  int x, y;
1978  int x2, y2;
1979  int x_subpel, y_subpel;
1980  int x_offset, y_offset;
1981 
1982  int block_width = plane ? 8 : 16;
1983  int plane_width = s->width >> (plane && s->chroma_x_shift);
1984  int plane_height = s->height >> (plane && s->chroma_y_shift);
1985 
1986 #define loop_stride 12
1987  uint8_t loop[12 * loop_stride];
1988 
1989  /* using division instead of shift to correctly handle negative values */
1990  x = 8 * bx + motion_x / motion_shift;
1991  y = 8 * by + motion_y / motion_shift;
1992 
1993  x_subpel = motion_x & subpel_mask;
1994  y_subpel = motion_y & subpel_mask;
1995 
1996  if (x_subpel || y_subpel) {
1997  x--;
1998  y--;
1999 
2000  if (x_subpel)
2001  x = FFMIN(x, x + FFSIGN(motion_x));
2002 
2003  if (y_subpel)
2004  y = FFMIN(y, y + FFSIGN(motion_y));
2005 
2006  x2 = x + block_width;
2007  y2 = y + block_width;
2008 
2009  if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
2010  return 0;
2011 
2012  x_offset = (-(x + 2) & 7) + 2;
2013  y_offset = (-(y + 2) & 7) + 2;
2014 
2015  if (x_offset > 8 + x_subpel && y_offset > 8 + y_subpel)
2016  return 0;
2017 
2018  s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2019  loop_stride, stride,
2020  12, 12, src_x - 1, src_y - 1,
2021  plane_width,
2022  plane_height);
2023 
2024  if (x_offset <= 8 + x_subpel)
2025  ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2026 
2027  if (y_offset <= 8 + y_subpel)
2028  ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2029 
2030  } else {
2031 
2032  x_offset = -x & 7;
2033  y_offset = -y & 7;
2034 
2035  if (!x_offset && !y_offset)
2036  return 0;
2037 
2038  s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2039  loop_stride, stride,
2040  12, 12, src_x - 1, src_y - 1,
2041  plane_width,
2042  plane_height);
2043 
2044 #define safe_loop_filter(name, ptr, stride, bounding_values) \
2045  if ((uintptr_t)(ptr) & 7) \
2046  s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
2047  else \
2048  s->vp3dsp.name(ptr, stride, bounding_values);
2049 
2050  if (x_offset)
2051  safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
2052 
2053  if (y_offset)
2054  safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
2055  }
2056 
2057  for (i = 0; i < 9; i++)
2058  memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2059 
2060  return 1;
2061 }
2062 #endif
2063 
2064 /*
2065  * Perform the final rendering for a particular slice of data.
2066  * The slice number ranges from 0..(c_superblock_height - 1).
2067  */
2068 static void render_slice(Vp3DecodeContext *s, int slice)
2069 {
2070  int x, y, i, j, fragment;
2071  int16_t *block = s->block;
2072  int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2073  int motion_halfpel_index;
2074  uint8_t *motion_source;
2075  int plane, first_pixel;
2076 
2077  if (slice >= s->c_superblock_height)
2078  return;
2079 
2080  for (plane = 0; plane < 3; plane++) {
2081  uint8_t *output_plane = s->current_frame.f->data[plane] +
2082  s->data_offset[plane];
2083  uint8_t *last_plane = s->last_frame.f->data[plane] +
2084  s->data_offset[plane];
2085  uint8_t *golden_plane = s->golden_frame.f->data[plane] +
2086  s->data_offset[plane];
2087  ptrdiff_t stride = s->current_frame.f->linesize[plane];
2088  int plane_width = s->width >> (plane && s->chroma_x_shift);
2089  int plane_height = s->height >> (plane && s->chroma_y_shift);
2090  int8_t(*motion_val)[2] = s->motion_val[!!plane];
2091 
2092  int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
2093  int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
2094  int slice_width = plane ? s->c_superblock_width
2095  : s->y_superblock_width;
2096 
2097  int fragment_width = s->fragment_width[!!plane];
2098  int fragment_height = s->fragment_height[!!plane];
2099  int fragment_start = s->fragment_start[plane];
2100 
2101  int do_await = !plane && HAVE_THREADS &&
2102  (s->avctx->active_thread_type & FF_THREAD_FRAME);
2103 
2104  if (!s->flipped_image)
2105  stride = -stride;
2106  if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2107  continue;
2108 
2109  /* for each superblock row in the slice (both of them)... */
2110  for (; sb_y < slice_height; sb_y++) {
2111  /* for each superblock in a row... */
2112  for (sb_x = 0; sb_x < slice_width; sb_x++) {
2113  /* for each block in a superblock... */
2114  for (j = 0; j < 16; j++) {
2115  x = 4 * sb_x + hilbert_offset[j][0];
2116  y = 4 * sb_y + hilbert_offset[j][1];
2117  fragment = y * fragment_width + x;
2118 
2119  i = fragment_start + fragment;
2120 
2121  // bounds check
2122  if (x >= fragment_width || y >= fragment_height)
2123  continue;
2124 
2125  first_pixel = 8 * y * stride + 8 * x;
2126 
2127  if (do_await &&
2128  s->all_fragments[i].coding_method != MODE_INTRA)
2129  await_reference_row(s, &s->all_fragments[i],
2130  motion_val[fragment][1],
2131  (16 * y) >> s->chroma_y_shift);
2132 
2133  /* transform if this block was coded */
2134  if (s->all_fragments[i].coding_method != MODE_COPY) {
2135  if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2136  (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2137  motion_source = golden_plane;
2138  else
2139  motion_source = last_plane;
2140 
2141  motion_source += first_pixel;
2142  motion_halfpel_index = 0;
2143 
2144  /* sort out the motion vector if this fragment is coded
2145  * using a motion vector method */
2146  if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2147  (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2148  int src_x, src_y;
2149  int standard_mc = 1;
2150  motion_x = motion_val[fragment][0];
2151  motion_y = motion_val[fragment][1];
2152 #if CONFIG_VP4_DECODER
2153  if (plane && s->version >= 2) {
2154  motion_x = (motion_x >> 1) | (motion_x & 1);
2155  motion_y = (motion_y >> 1) | (motion_y & 1);
2156  }
2157 #endif
2158 
2159  src_x = (motion_x >> 1) + 8 * x;
2160  src_y = (motion_y >> 1) + 8 * y;
2161 
2162  motion_halfpel_index = motion_x & 0x01;
2163  motion_source += (motion_x >> 1);
2164 
2165  motion_halfpel_index |= (motion_y & 0x01) << 1;
2166  motion_source += ((motion_y >> 1) * stride);
2167 
2168 #if CONFIG_VP4_DECODER
2169  if (s->version >= 2) {
2170  uint8_t *temp = s->edge_emu_buffer;
2171  if (stride < 0)
2172  temp -= 8 * stride;
2173  if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) {
2174  motion_source = temp;
2175  standard_mc = 0;
2176  }
2177  }
2178 #endif
2179 
2180  if (standard_mc && (
2181  src_x < 0 || src_y < 0 ||
2182  src_x + 9 >= plane_width ||
2183  src_y + 9 >= plane_height)) {
2184  uint8_t *temp = s->edge_emu_buffer;
2185  if (stride < 0)
2186  temp -= 8 * stride;
2187 
2188  s->vdsp.emulated_edge_mc(temp, motion_source,
2189  stride, stride,
2190  9, 9, src_x, src_y,
2191  plane_width,
2192  plane_height);
2193  motion_source = temp;
2194  }
2195  }
2196 
2197  /* first, take care of copying a block from either the
2198  * previous or the golden frame */
2199  if (s->all_fragments[i].coding_method != MODE_INTRA) {
2200  /* Note, it is possible to implement all MC cases
2201  * with put_no_rnd_pixels_l2 which would look more
2202  * like the VP3 source but this would be slower as
2203  * put_no_rnd_pixels_tab is better optimized */
2204  if (motion_halfpel_index != 3) {
2205  s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2206  output_plane + first_pixel,
2207  motion_source, stride, 8);
2208  } else {
2209  /* d is 0 if motion_x and _y have the same sign,
2210  * else -1 */
2211  int d = (motion_x ^ motion_y) >> 31;
2212  s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
2213  motion_source - d,
2214  motion_source + stride + 1 + d,
2215  stride, 8);
2216  }
2217  }
2218 
2219  /* invert DCT and place (or add) in final output */
2220 
2221  if (s->all_fragments[i].coding_method == MODE_INTRA) {
2222  vp3_dequant(s, s->all_fragments + i,
2223  plane, 0, block);
2224  s->vp3dsp.idct_put(output_plane + first_pixel,
2225  stride,
2226  block);
2227  } else {
2228  if (vp3_dequant(s, s->all_fragments + i,
2229  plane, 1, block)) {
2230  s->vp3dsp.idct_add(output_plane + first_pixel,
2231  stride,
2232  block);
2233  } else {
2234  s->vp3dsp.idct_dc_add(output_plane + first_pixel,
2235  stride, block);
2236  }
2237  }
2238  } else {
2239  /* copy directly from the previous frame */
2240  s->hdsp.put_pixels_tab[1][0](
2241  output_plane + first_pixel,
2242  last_plane + first_pixel,
2243  stride, 8);
2244  }
2245  }
2246  }
2247 
2248  // Filter up to the last row in the superblock row
2249  if (s->version < 2 && !s->skip_loop_filter)
2250  apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
2251  FFMIN(4 * sb_y + 3, fragment_height - 1));
2252  }
2253  }
2254 
2255  /* this looks like a good place for slice dispatch... */
2256  /* algorithm:
2257  * if (slice == s->macroblock_height - 1)
2258  * dispatch (both last slice & 2nd-to-last slice);
2259  * else if (slice > 0)
2260  * dispatch (slice - 1);
2261  */
2262 
2263  vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
2264  s->height - 16));
2265 }
2266 
2267 /// Allocate tables for per-frame data in Vp3DecodeContext
2269 {
2270  Vp3DecodeContext *s = avctx->priv_data;
2271  int y_fragment_count, c_fragment_count;
2272 
2273  free_tables(avctx);
2274 
2275  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2276  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2277 
2278  /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
2279  s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count));
2280  s->all_fragments = av_calloc(s->fragment_count, sizeof(*s->all_fragments));
2281 
2282  s-> kf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2283  s->nkf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int));
2284  memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
2285 
2286  s->dct_tokens_base = av_calloc(s->fragment_count,
2287  64 * sizeof(*s->dct_tokens_base));
2288  s->motion_val[0] = av_calloc(y_fragment_count, sizeof(*s->motion_val[0]));
2289  s->motion_val[1] = av_calloc(c_fragment_count, sizeof(*s->motion_val[1]));
2290 
2291  /* work out the block mapping tables */
2292  s->superblock_fragments = av_calloc(s->superblock_count, 16 * sizeof(int));
2293  s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
2294 
2295  s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
2296 
2297  if (!s->superblock_coding || !s->all_fragments ||
2298  !s->dct_tokens_base || !s->kf_coded_fragment_list ||
2299  !s->nkf_coded_fragment_list ||
2300  !s->superblock_fragments || !s->macroblock_coding ||
2301  !s->dc_pred_row ||
2302  !s->motion_val[0] || !s->motion_val[1]) {
2303  return -1;
2304  }
2305 
2307 
2308  return 0;
2309 }
2310 
2312 {
2313  s->current_frame.f = av_frame_alloc();
2314  s->last_frame.f = av_frame_alloc();
2315  s->golden_frame.f = av_frame_alloc();
2316 
2317  if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f)
2318  return AVERROR(ENOMEM);
2319 
2320  return 0;
2321 }
2322 
2324 {
2325  Vp3DecodeContext *s = avctx->priv_data;
2326  int i, inter, plane, ret;
2327  int c_width;
2328  int c_height;
2329  int y_fragment_count, c_fragment_count;
2330 #if CONFIG_VP4_DECODER
2331  int j;
2332 #endif
2333 
2334  ret = init_frames(s);
2335  if (ret < 0)
2336  return ret;
2337 
2338  if (avctx->codec_tag == MKTAG('V', 'P', '4', '0')) {
2339  s->version = 3;
2340 #if !CONFIG_VP4_DECODER
2341  av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2343 #endif
2344  } else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
2345  s->version = 0;
2346  else
2347  s->version = 1;
2348 
2349  s->avctx = avctx;
2350  s->width = FFALIGN(avctx->coded_width, 16);
2351  s->height = FFALIGN(avctx->coded_height, 16);
2352  if (avctx->codec_id != AV_CODEC_ID_THEORA)
2353  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2355  ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
2356  ff_videodsp_init(&s->vdsp, 8);
2357  ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
2358 
2359  for (i = 0; i < 64; i++) {
2360 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
2361  s->idct_permutation[i] = TRANSPOSE(i);
2362  s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
2363 #undef TRANSPOSE
2364  }
2365 
2366  /* initialize to an impossible value which will force a recalculation
2367  * in the first frame decode */
2368  for (i = 0; i < 3; i++)
2369  s->qps[i] = -1;
2370 
2371  ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
2372  if (ret)
2373  return ret;
2374 
2375  s->y_superblock_width = (s->width + 31) / 32;
2376  s->y_superblock_height = (s->height + 31) / 32;
2377  s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2378 
2379  /* work out the dimensions for the C planes */
2380  c_width = s->width >> s->chroma_x_shift;
2381  c_height = s->height >> s->chroma_y_shift;
2382  s->c_superblock_width = (c_width + 31) / 32;
2383  s->c_superblock_height = (c_height + 31) / 32;
2384  s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2385 
2386  s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
2387  s->u_superblock_start = s->y_superblock_count;
2388  s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
2389 
2390  s->macroblock_width = (s->width + 15) / 16;
2391  s->macroblock_height = (s->height + 15) / 16;
2392  s->macroblock_count = s->macroblock_width * s->macroblock_height;
2393  s->c_macroblock_width = (c_width + 15) / 16;
2394  s->c_macroblock_height = (c_height + 15) / 16;
2395  s->c_macroblock_count = s->c_macroblock_width * s->c_macroblock_height;
2396  s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count;
2397 
2398  s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
2399  s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
2400  s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
2401  s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
2402 
2403  /* fragment count covers all 8x8 blocks for all 3 planes */
2404  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2405  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2406  s->fragment_count = y_fragment_count + 2 * c_fragment_count;
2407  s->fragment_start[1] = y_fragment_count;
2408  s->fragment_start[2] = y_fragment_count + c_fragment_count;
2409 
2410  if (!s->theora_tables) {
2411  const uint8_t (*bias_tabs)[32][2];
2412 
2413  for (i = 0; i < 64; i++) {
2414  s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i];
2415  s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i];
2416  s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i];
2417  s->base_matrix[0][i] = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i];
2418  s->base_matrix[1][i] = s->version < 2 ? vp31_intra_c_dequant[i] : vp4_generic_dequant[i];
2419  s->base_matrix[2][i] = s->version < 2 ? vp31_inter_dequant[i] : vp4_generic_dequant[i];
2420  s->filter_limit_values[i] = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i];
2421  }
2422 
2423  for (inter = 0; inter < 2; inter++) {
2424  for (plane = 0; plane < 3; plane++) {
2425  s->qr_count[inter][plane] = 1;
2426  s->qr_size[inter][plane][0] = 63;
2427  s->qr_base[inter][plane][0] =
2428  s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
2429  }
2430  }
2431 
2432  /* init VLC tables */
2433  bias_tabs = CONFIG_VP4_DECODER && s->version >= 2 ? vp4_bias : vp3_bias;
2434  for (int i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
2435  ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, 32,
2436  &bias_tabs[i][0][1], 2,
2437  &bias_tabs[i][0][0], 2, 1,
2438  0, 0, avctx);
2439  if (ret < 0)
2440  return ret;
2441  }
2442  } else {
2443  for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
2444  const HuffTable *tab = &s->huffman_table[i];
2445 
2446  ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, tab->nb_entries,
2447  &tab->entries[0].len, sizeof(*tab->entries),
2448  &tab->entries[0].sym, sizeof(*tab->entries), 1,
2449  0, 0, avctx);
2450  if (ret < 0)
2451  return ret;
2452  }
2453  }
2454 
2455  ret = ff_init_vlc_from_lengths(&s->superblock_run_length_vlc, SUPERBLOCK_VLC_BITS, 34,
2457  NULL, 0, 0, 1, 0, avctx);
2458  if (ret < 0)
2459  return ret;
2460 
2461  ret = ff_init_vlc_from_lengths(&s->fragment_run_length_vlc, 5, 30,
2463  NULL, 0, 0, 0, 0, avctx);
2464  if (ret < 0)
2465  return ret;
2466 
2467  ret = ff_init_vlc_from_lengths(&s->mode_code_vlc, 3, 8,
2468  mode_code_vlc_len, 1,
2469  NULL, 0, 0, 0, 0, avctx);
2470  if (ret < 0)
2471  return ret;
2472 
2473  ret = ff_init_vlc_from_lengths(&s->motion_vector_vlc, VP3_MV_VLC_BITS, 63,
2474  &motion_vector_vlc_table[0][1], 2,
2475  &motion_vector_vlc_table[0][0], 2, 1,
2476  -31, 0, avctx);
2477  if (ret < 0)
2478  return ret;
2479 
2480 #if CONFIG_VP4_DECODER
2481  for (j = 0; j < 2; j++)
2482  for (i = 0; i < 7; i++) {
2483  ret = ff_init_vlc_from_lengths(&s->vp4_mv_vlc[j][i], VP4_MV_VLC_BITS, 63,
2484  &vp4_mv_vlc[j][i][0][1], 2,
2485  &vp4_mv_vlc[j][i][0][0], 2, 1, -31,
2486  0, avctx);
2487  if (ret < 0)
2488  return ret;
2489  }
2490 
2491  /* version >= 2 */
2492  for (i = 0; i < 2; i++)
2493  if ((ret = init_vlc(&s->block_pattern_vlc[i], 3, 14,
2494  &vp4_block_pattern_vlc[i][0][1], 2, 1,
2495  &vp4_block_pattern_vlc[i][0][0], 2, 1, 0)) < 0)
2496  return ret;
2497 #endif
2498 
2499  return allocate_tables(avctx);
2500 }
2501 
2502 /// Release and shuffle frames after decode finishes
2503 static int update_frames(AVCodecContext *avctx)
2504 {
2505  Vp3DecodeContext *s = avctx->priv_data;
2506  int ret = 0;
2507 
2508  /* shuffle frames (last = current) */
2509  ff_thread_release_buffer(avctx, &s->last_frame);
2510  ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
2511  if (ret < 0)
2512  goto fail;
2513 
2514  if (s->keyframe) {
2515  ff_thread_release_buffer(avctx, &s->golden_frame);
2516  ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
2517  }
2518 
2519 fail:
2520  ff_thread_release_buffer(avctx, &s->current_frame);
2521  return ret;
2522 }
2523 
2524 #if HAVE_THREADS
2525 static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
2526 {
2527  ff_thread_release_buffer(s->avctx, dst);
2528  if (src->f->data[0])
2529  return ff_thread_ref_frame(dst, src);
2530  return 0;
2531 }
2532 
2533 static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
2534 {
2535  int ret;
2536  if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
2537  (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
2538  (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
2539  return ret;
2540  return 0;
2541 }
2542 
2543 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2544 {
2545  Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
2546  int qps_changed = 0, i, err;
2547 
2548  if (!s1->current_frame.f->data[0] ||
2549  s->width != s1->width || s->height != s1->height) {
2550  if (s != s1)
2551  ref_frames(s, s1);
2552  return -1;
2553  }
2554 
2555  if (s != s1) {
2556  // copy previous frame data
2557  if ((err = ref_frames(s, s1)) < 0)
2558  return err;
2559 
2560  s->keyframe = s1->keyframe;
2561 
2562  // copy qscale data if necessary
2563  for (i = 0; i < 3; i++) {
2564  if (s->qps[i] != s1->qps[1]) {
2565  qps_changed = 1;
2566  memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2567  }
2568  }
2569 
2570  if (s->qps[0] != s1->qps[0])
2571  memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2572  sizeof(s->bounding_values_array));
2573 
2574  if (qps_changed) {
2575  memcpy(s->qps, s1->qps, sizeof(s->qps));
2576  memcpy(s->last_qps, s1->last_qps, sizeof(s->last_qps));
2577  s->nqps = s1->nqps;
2578  }
2579  }
2580 
2581  return update_frames(dst);
2582 }
2583 #endif
2584 
2586  void *data, int *got_frame,
2587  AVPacket *avpkt)
2588 {
2589  AVFrame *frame = data;
2590  const uint8_t *buf = avpkt->data;
2591  int buf_size = avpkt->size;
2592  Vp3DecodeContext *s = avctx->priv_data;
2593  GetBitContext gb;
2594  int i, ret;
2595 
2596  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2597  return ret;
2598 
2599 #if CONFIG_THEORA_DECODER
2600  if (s->theora && get_bits1(&gb)) {
2601  int type = get_bits(&gb, 7);
2602  skip_bits_long(&gb, 6*8); /* "theora" */
2603 
2604  if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2605  av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2606  return AVERROR_PATCHWELCOME;
2607  }
2608  if (type == 0) {
2609  vp3_decode_end(avctx);
2610  ret = theora_decode_header(avctx, &gb);
2611 
2612  if (ret >= 0)
2613  ret = vp3_decode_init(avctx);
2614  if (ret < 0) {
2615  vp3_decode_end(avctx);
2616  return ret;
2617  }
2618  return buf_size;
2619  } else if (type == 2) {
2620  vp3_decode_end(avctx);
2621  ret = theora_decode_tables(avctx, &gb);
2622  if (ret >= 0)
2623  ret = vp3_decode_init(avctx);
2624  if (ret < 0) {
2625  vp3_decode_end(avctx);
2626  return ret;
2627  }
2628  return buf_size;
2629  }
2630 
2631  av_log(avctx, AV_LOG_ERROR,
2632  "Header packet passed to frame decoder, skipping\n");
2633  return -1;
2634  }
2635 #endif
2636 
2637  s->keyframe = !get_bits1(&gb);
2638  if (!s->all_fragments) {
2639  av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2640  return -1;
2641  }
2642  if (!s->theora)
2643  skip_bits(&gb, 1);
2644  for (i = 0; i < 3; i++)
2645  s->last_qps[i] = s->qps[i];
2646 
2647  s->nqps = 0;
2648  do {
2649  s->qps[s->nqps++] = get_bits(&gb, 6);
2650  } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2651  for (i = s->nqps; i < 3; i++)
2652  s->qps[i] = -1;
2653 
2654  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2655  av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2656  s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2657 
2658  s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2659  avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2660  : AVDISCARD_NONKEY);
2661 
2662  if (s->qps[0] != s->last_qps[0])
2664 
2665  for (i = 0; i < s->nqps; i++)
2666  // reinit all dequantizers if the first one changed, because
2667  // the DC of the first quantizer must be used for all matrices
2668  if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2669  init_dequantizer(s, i);
2670 
2671  if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2672  return buf_size;
2673 
2674  s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2676  s->current_frame.f->key_frame = s->keyframe;
2677  if ((ret = ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
2678  goto error;
2679 
2680  if (!s->edge_emu_buffer)
2681  s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2682 
2683  if (s->keyframe) {
2684  if (!s->theora) {
2685  skip_bits(&gb, 4); /* width code */
2686  skip_bits(&gb, 4); /* height code */
2687  if (s->version) {
2688  int version = get_bits(&gb, 5);
2689 #if !CONFIG_VP4_DECODER
2690  if (version >= 2) {
2691  av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n");
2693  }
2694 #endif
2695  s->version = version;
2696  if (avctx->frame_number == 0)
2697  av_log(s->avctx, AV_LOG_DEBUG,
2698  "VP version: %d\n", s->version);
2699  }
2700  }
2701  if (s->version || s->theora) {
2702  if (get_bits1(&gb))
2703  av_log(s->avctx, AV_LOG_ERROR,
2704  "Warning, unsupported keyframe coding type?!\n");
2705  skip_bits(&gb, 2); /* reserved? */
2706 
2707 #if CONFIG_VP4_DECODER
2708  if (s->version >= 2) {
2709  int mb_height, mb_width;
2710  int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
2711 
2712  mb_height = get_bits(&gb, 8);
2713  mb_width = get_bits(&gb, 8);
2714  if (mb_height != s->macroblock_height ||
2715  mb_width != s->macroblock_width)
2716  avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
2717 
2718  mb_width_mul = get_bits(&gb, 5);
2719  mb_width_div = get_bits(&gb, 3);
2720  mb_height_mul = get_bits(&gb, 5);
2721  mb_height_div = get_bits(&gb, 3);
2722  if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
2723  avpriv_request_sample(s->avctx, "unexpected macroblock dimension multipler/divider");
2724 
2725  if (get_bits(&gb, 2))
2726  avpriv_request_sample(s->avctx, "unknown bits");
2727  }
2728 #endif
2729  }
2730  } else {
2731  if (!s->golden_frame.f->data[0]) {
2732  av_log(s->avctx, AV_LOG_WARNING,
2733  "vp3: first frame not a keyframe\n");
2734 
2735  s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2736  if ((ret = ff_thread_get_buffer(avctx, &s->golden_frame,
2737  AV_GET_BUFFER_FLAG_REF)) < 0)
2738  goto error;
2739  ff_thread_release_buffer(avctx, &s->last_frame);
2740  if ((ret = ff_thread_ref_frame(&s->last_frame,
2741  &s->golden_frame)) < 0)
2742  goto error;
2743  ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2744  }
2745  }
2746 
2747  memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2748  ff_thread_finish_setup(avctx);
2749 
2750  if (s->version < 2) {
2751  if ((ret = unpack_superblocks(s, &gb)) < 0) {
2752  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2753  goto error;
2754  }
2755 #if CONFIG_VP4_DECODER
2756  } else {
2757  if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) {
2758  av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
2759  goto error;
2760  }
2761 #endif
2762  }
2763  if ((ret = unpack_modes(s, &gb)) < 0) {
2764  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2765  goto error;
2766  }
2767  if (ret = unpack_vectors(s, &gb)) {
2768  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2769  goto error;
2770  }
2771  if ((ret = unpack_block_qpis(s, &gb)) < 0) {
2772  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2773  goto error;
2774  }
2775 
2776  if (s->version < 2) {
2777  if ((ret = unpack_dct_coeffs(s, &gb)) < 0) {
2778  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2779  goto error;
2780  }
2781 #if CONFIG_VP4_DECODER
2782  } else {
2783  if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) {
2784  av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
2785  goto error;
2786  }
2787 #endif
2788  }
2789 
2790  for (i = 0; i < 3; i++) {
2791  int height = s->height >> (i && s->chroma_y_shift);
2792  if (s->flipped_image)
2793  s->data_offset[i] = 0;
2794  else
2795  s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2796  }
2797 
2798  s->last_slice_end = 0;
2799  for (i = 0; i < s->c_superblock_height; i++)
2800  render_slice(s, i);
2801 
2802  // filter the last row
2803  if (s->version < 2)
2804  for (i = 0; i < 3; i++) {
2805  int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2806  apply_loop_filter(s, i, row, row + 1);
2807  }
2808  vp3_draw_horiz_band(s, s->height);
2809 
2810  /* output frame, offset as needed */
2811  if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2812  return ret;
2813 
2814  frame->crop_left = s->offset_x;
2815  frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
2816  frame->crop_top = s->offset_y;
2817  frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2818 
2819  *got_frame = 1;
2820 
2821  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2822  ret = update_frames(avctx);
2823  if (ret < 0)
2824  return ret;
2825  }
2826 
2827  return buf_size;
2828 
2829 error:
2830  ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2831 
2832  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2833  av_frame_unref(s->current_frame.f);
2834 
2835  return ret;
2836 }
2837 
2838 static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length,
2839  AVCodecContext *avctx)
2840 {
2841  if (get_bits1(gb)) {
2842  int token;
2843  if (huff->nb_entries >= 32) { /* overflow */
2844  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2845  return -1;
2846  }
2847  token = get_bits(gb, 5);
2848  ff_dlog(avctx, "code length %d, curr entry %d, token %d\n",
2849  length, huff->nb_entries, token);
2850  huff->entries[huff->nb_entries++] = (HuffEntry){ length, token };
2851  } else {
2852  /* The following bound follows from the fact that nb_entries <= 32. */
2853  if (length >= 31) { /* overflow */
2854  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2855  return -1;
2856  }
2857  length++;
2858  if (read_huffman_tree(huff, gb, length, avctx))
2859  return -1;
2860  if (read_huffman_tree(huff, gb, length, avctx))
2861  return -1;
2862  }
2863  return 0;
2864 }
2865 
2866 #if CONFIG_THEORA_DECODER
2867 static const enum AVPixelFormat theora_pix_fmts[4] = {
2869 };
2870 
2871 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2872 {
2873  Vp3DecodeContext *s = avctx->priv_data;
2874  int visible_width, visible_height, colorspace;
2875  uint8_t offset_x = 0, offset_y = 0;
2876  int ret;
2877  AVRational fps, aspect;
2878 
2879  if (get_bits_left(gb) < 206)
2880  return AVERROR_INVALIDDATA;
2881 
2882  s->theora_header = 0;
2883  s->theora = get_bits(gb, 24);
2884  av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2885  if (!s->theora) {
2886  s->theora = 1;
2887  avpriv_request_sample(s->avctx, "theora 0");
2888  }
2889 
2890  /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2891  * but previous versions have the image flipped relative to vp3 */
2892  if (s->theora < 0x030200) {
2893  s->flipped_image = 1;
2894  av_log(avctx, AV_LOG_DEBUG,
2895  "Old (<alpha3) Theora bitstream, flipped image\n");
2896  }
2897 
2898  visible_width =
2899  s->width = get_bits(gb, 16) << 4;
2900  visible_height =
2901  s->height = get_bits(gb, 16) << 4;
2902 
2903  if (s->theora >= 0x030200) {
2904  visible_width = get_bits(gb, 24);
2905  visible_height = get_bits(gb, 24);
2906 
2907  offset_x = get_bits(gb, 8); /* offset x */
2908  offset_y = get_bits(gb, 8); /* offset y, from bottom */
2909  }
2910 
2911  /* sanity check */
2912  if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2913  visible_width + offset_x > s->width ||
2914  visible_height + offset_y > s->height) {
2915  av_log(avctx, AV_LOG_ERROR,
2916  "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2917  visible_width, visible_height, offset_x, offset_y,
2918  s->width, s->height);
2919  return AVERROR_INVALIDDATA;
2920  }
2921 
2922  fps.num = get_bits_long(gb, 32);
2923  fps.den = get_bits_long(gb, 32);
2924  if (fps.num && fps.den) {
2925  if (fps.num < 0 || fps.den < 0) {
2926  av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2927  return AVERROR_INVALIDDATA;
2928  }
2929  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2930  fps.den, fps.num, 1 << 30);
2931  }
2932 
2933  aspect.num = get_bits(gb, 24);
2934  aspect.den = get_bits(gb, 24);
2935  if (aspect.num && aspect.den) {
2937  &avctx->sample_aspect_ratio.den,
2938  aspect.num, aspect.den, 1 << 30);
2939  ff_set_sar(avctx, avctx->sample_aspect_ratio);
2940  }
2941 
2942  if (s->theora < 0x030200)
2943  skip_bits(gb, 5); /* keyframe frequency force */
2944  colorspace = get_bits(gb, 8);
2945  skip_bits(gb, 24); /* bitrate */
2946 
2947  skip_bits(gb, 6); /* quality hint */
2948 
2949  if (s->theora >= 0x030200) {
2950  skip_bits(gb, 5); /* keyframe frequency force */
2951  avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2952  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2953  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2954  return AVERROR_INVALIDDATA;
2955  }
2956  skip_bits(gb, 3); /* reserved */
2957  } else
2958  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2959 
2960  ret = ff_set_dimensions(avctx, s->width, s->height);
2961  if (ret < 0)
2962  return ret;
2963  if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
2964  avctx->width = visible_width;
2965  avctx->height = visible_height;
2966  // translate offsets from theora axis ([0,0] lower left)
2967  // to normal axis ([0,0] upper left)
2968  s->offset_x = offset_x;
2969  s->offset_y = s->height - visible_height - offset_y;
2970  }
2971 
2972  if (colorspace == 1)
2974  else if (colorspace == 2)
2976 
2977  if (colorspace == 1 || colorspace == 2) {
2978  avctx->colorspace = AVCOL_SPC_BT470BG;
2979  avctx->color_trc = AVCOL_TRC_BT709;
2980  }
2981 
2982  s->theora_header = 1;
2983  return 0;
2984 }
2985 
2986 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2987 {
2988  Vp3DecodeContext *s = avctx->priv_data;
2989  int i, n, matrices, inter, plane, ret;
2990 
2991  if (!s->theora_header)
2992  return AVERROR_INVALIDDATA;
2993 
2994  if (s->theora >= 0x030200) {
2995  n = get_bits(gb, 3);
2996  /* loop filter limit values table */
2997  if (n)
2998  for (i = 0; i < 64; i++)
2999  s->filter_limit_values[i] = get_bits(gb, n);
3000  }
3001 
3002  if (s->theora >= 0x030200)
3003  n = get_bits(gb, 4) + 1;
3004  else
3005  n = 16;
3006  /* quality threshold table */
3007  for (i = 0; i < 64; i++)
3008  s->coded_ac_scale_factor[i] = get_bits(gb, n);
3009 
3010  if (s->theora >= 0x030200)
3011  n = get_bits(gb, 4) + 1;
3012  else
3013  n = 16;
3014  /* dc scale factor table */
3015  for (i = 0; i < 64; i++)
3016  s->coded_dc_scale_factor[0][i] =
3017  s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
3018 
3019  if (s->theora >= 0x030200)
3020  matrices = get_bits(gb, 9) + 1;
3021  else
3022  matrices = 3;
3023 
3024  if (matrices > 384) {
3025  av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
3026  return -1;
3027  }
3028 
3029  for (n = 0; n < matrices; n++)
3030  for (i = 0; i < 64; i++)
3031  s->base_matrix[n][i] = get_bits(gb, 8);
3032 
3033  for (inter = 0; inter <= 1; inter++) {
3034  for (plane = 0; plane <= 2; plane++) {
3035  int newqr = 1;
3036  if (inter || plane > 0)
3037  newqr = get_bits1(gb);
3038  if (!newqr) {
3039  int qtj, plj;
3040  if (inter && get_bits1(gb)) {
3041  qtj = 0;
3042  plj = plane;
3043  } else {
3044  qtj = (3 * inter + plane - 1) / 3;
3045  plj = (plane + 2) % 3;
3046  }
3047  s->qr_count[inter][plane] = s->qr_count[qtj][plj];
3048  memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
3049  sizeof(s->qr_size[0][0]));
3050  memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
3051  sizeof(s->qr_base[0][0]));
3052  } else {
3053  int qri = 0;
3054  int qi = 0;
3055 
3056  for (;;) {
3057  i = get_bits(gb, av_log2(matrices - 1) + 1);
3058  if (i >= matrices) {
3059  av_log(avctx, AV_LOG_ERROR,
3060  "invalid base matrix index\n");
3061  return -1;
3062  }
3063  s->qr_base[inter][plane][qri] = i;
3064  if (qi >= 63)
3065  break;
3066  i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
3067  s->qr_size[inter][plane][qri++] = i;
3068  qi += i;
3069  }
3070 
3071  if (qi > 63) {
3072  av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
3073  return -1;
3074  }
3075  s->qr_count[inter][plane] = qri;
3076  }
3077  }
3078  }
3079 
3080  /* Huffman tables */
3081  for (int i = 0; i < FF_ARRAY_ELEMS(s->huffman_table); i++) {
3082  s->huffman_table[i].nb_entries = 0;
3083  if ((ret = read_huffman_tree(&s->huffman_table[i], gb, 0, avctx)) < 0)
3084  return ret;
3085  }
3086 
3087  s->theora_tables = 1;
3088 
3089  return 0;
3090 }
3091 
3092 static av_cold int theora_decode_init(AVCodecContext *avctx)
3093 {
3094  Vp3DecodeContext *s = avctx->priv_data;
3095  GetBitContext gb;
3096  int ptype;
3097  const uint8_t *header_start[3];
3098  int header_len[3];
3099  int i;
3100  int ret;
3101 
3102  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3103 
3104  s->theora = 1;
3105 
3106  if (!avctx->extradata_size) {
3107  av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
3108  return -1;
3109  }
3110 
3112  42, header_start, header_len) < 0) {
3113  av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
3114  return -1;
3115  }
3116 
3117  for (i = 0; i < 3; i++) {
3118  if (header_len[i] <= 0)
3119  continue;
3120  ret = init_get_bits8(&gb, header_start[i], header_len[i]);
3121  if (ret < 0)
3122  return ret;
3123 
3124  ptype = get_bits(&gb, 8);
3125 
3126  if (!(ptype & 0x80)) {
3127  av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
3128 // return -1;
3129  }
3130 
3131  // FIXME: Check for this as well.
3132  skip_bits_long(&gb, 6 * 8); /* "theora" */
3133 
3134  switch (ptype) {
3135  case 0x80:
3136  if (theora_decode_header(avctx, &gb) < 0)
3137  return -1;
3138  break;
3139  case 0x81:
3140 // FIXME: is this needed? it breaks sometimes
3141 // theora_decode_comments(avctx, gb);
3142  break;
3143  case 0x82:
3144  if (theora_decode_tables(avctx, &gb))
3145  return -1;
3146  break;
3147  default:
3148  av_log(avctx, AV_LOG_ERROR,
3149  "Unknown Theora config packet: %d\n", ptype & ~0x80);
3150  break;
3151  }
3152  if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
3153  av_log(avctx, AV_LOG_WARNING,
3154  "%d bits left in packet %X\n",
3155  8 * header_len[i] - get_bits_count(&gb), ptype);
3156  if (s->theora < 0x030200)
3157  break;
3158  }
3159 
3160  return vp3_decode_init(avctx);
3161 }
3162 
3163 const AVCodec ff_theora_decoder = {
3164  .name = "theora",
3165  .long_name = NULL_IF_CONFIG_SMALL("Theora"),
3166  .type = AVMEDIA_TYPE_VIDEO,
3167  .id = AV_CODEC_ID_THEORA,
3168  .priv_data_size = sizeof(Vp3DecodeContext),
3169  .init = theora_decode_init,
3170  .close = vp3_decode_end,
3175  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3178 };
3179 #endif
3180 
3182  .name = "vp3",
3183  .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
3184  .type = AVMEDIA_TYPE_VIDEO,
3185  .id = AV_CODEC_ID_VP3,
3186  .priv_data_size = sizeof(Vp3DecodeContext),
3187  .init = vp3_decode_init,
3188  .close = vp3_decode_end,
3193  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3196 };
3197 
3198 #if CONFIG_VP4_DECODER
3199 const AVCodec ff_vp4_decoder = {
3200  .name = "vp4",
3201  .long_name = NULL_IF_CONFIG_SMALL("On2 VP4"),
3202  .type = AVMEDIA_TYPE_VIDEO,
3203  .id = AV_CODEC_ID_VP4,
3204  .priv_data_size = sizeof(Vp3DecodeContext),
3205  .init = vp3_decode_init,
3206  .close = vp3_decode_end,
3211  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3214 };
3215 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
vp4_ac_scale_factor
static const uint16_t vp4_ac_scale_factor[64]
Definition: vp4data.h:64
vp4data.h
AVCodec
AVCodec.
Definition: codec.h:202
PUL
#define PUL
allocate_tables
static av_cold int allocate_tables(AVCodecContext *avctx)
Allocate tables for per-frame data in Vp3DecodeContext.
Definition: vp3.c:2268
stride
int stride
Definition: mace.c:144
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:292
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
Vp3Fragment::dc
int16_t dc
Definition: vp3.c:60
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: internal.h:42
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
av_clip
#define av_clip
Definition: common.h:96
Vp3DecodeContext::offset_x
uint8_t offset_x
Definition: vp3.c:222
Vp3DecodeContext::mode_code_vlc
VLC mode_code_vlc
Definition: vp3.c:281
VP3DSPContext
Definition: vp3dsp.h:25
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:850
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
vp3_decode_flush
static void vp3_decode_flush(AVCodecContext *avctx)
Definition: vp3.c:330
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:960
mem_internal.h
Vp3DecodeContext::c_macroblock_height
int c_macroblock_height
Definition: vp3.c:212
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:866
zero_run_base
static const uint8_t zero_run_base[32]
Definition: vp3data.h:146
MODE_INTER_PRIOR_LAST
#define MODE_INTER_PRIOR_LAST
Definition: vp3.c:78
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
VP4Predictor
Definition: vp3.c:156
Vp3DecodeContext::idct_scantable
uint8_t idct_scantable[64]
Definition: vp3.c:183
HuffEntry::len
uint8_t len
Definition: exr.c:94
VP4_DC_GOLDEN
@ VP4_DC_GOLDEN
Definition: vp3.c:140
VP4Predictor::dc
int dc
Definition: vp3.c:157
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:547
mode_code_vlc_len
static const uint8_t mode_code_vlc_len[8]
Definition: vp3data.h:110
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:220
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
read_huffman_tree
static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length, AVCodecContext *avctx)
Definition: vp3.c:2838
PUR
#define PUR
vp3dsp.h
Vp3DecodeContext::motion_vector_vlc
VLC motion_vector_vlc
Definition: vp3.c:282
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:953
Vp3DecodeContext::superblock_run_length_vlc
VLC superblock_run_length_vlc
Definition: vp3.c:278
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
NB_VP4_DC_TYPES
@ NB_VP4_DC_TYPES
Definition: vp3.c:141
ff_vp3dsp_set_bounding_values
void ff_vp3dsp_set_bounding_values(int *bounding_values_array, int filter_limit)
Definition: vp3dsp.c:476
init_vlc
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:143
Vp3DecodeContext::all_fragments
Vp3Fragment * all_fragments
Definition: vp3.c:219
Vp3DecodeContext::filter_limit_values
uint8_t filter_limit_values[64]
Definition: vp3.c:304
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:798
base
uint8_t base
Definition: vp3data.h:141
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
Vp3Fragment::coding_method
uint8_t coding_method
Definition: vp3.c:61
VP4_DC_INTRA
@ VP4_DC_INTRA
Definition: vp3.c:138
thread.h
ff_thread_await_progress
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_thread_await_progress() has been called on them. reget_buffer() and buffer age optimizations no longer work. *The contents of buffers must not be written to after ff_thread_report_progress() has been called on them. This includes draw_edges(). Porting codecs to frame threading
unpack_superblocks
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:468
render_slice
static void render_slice(Vp3DecodeContext *s, int slice)
Definition: vp3.c:2068
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1303
Vp3DecodeContext::height
int height
Definition: vp3.c:176
vp3_dequant
static int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, int plane, int inter, int16_t block[64])
Pull DCT tokens from the 64 levels to decode and dequant the coefficients for the next block in codin...
Definition: vp3.c:1859
AV_CODEC_FLAG2_IGNORE_CROP
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:310
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
fragment
Definition: dashdec.c:36
Vp3DecodeContext::y_superblock_count
int y_superblock_count
Definition: vp3.c:199
xiph.h
init
static int init
Definition: av_tx.c:47
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
Vp3DecodeContext::bounding_values_array
int bounding_values_array[256+2]
Definition: vp3.c:305
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1710
Vp3DecodeContext::superblock_fragments
int * superblock_fragments
Definition: vp3.c:293
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:529
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
get_coeff
static int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
Definition: vp3.c:1151
Vp3DecodeContext::qr_count
uint8_t qr_count[2][3]
Definition: vp3.c:232
VLC_TYPE
#define VLC_TYPE
Definition: vlc.h:24
U
#define U(x)
Definition: vp56_arith.h:37
Vp3DecodeContext::hdsp
HpelDSPContext hdsp
Definition: vp3.c:184
vp4_mv_vlc
static const uint8_t vp4_mv_vlc[2][7][63][2]
Definition: vp4data.h:112
BLOCK_Y
#define BLOCK_Y
Definition: vp3.c:644
Vp3DecodeContext::y_superblock_width
int y_superblock_width
Definition: vp3.c:197
fail
#define fail()
Definition: checkasm.h:127
CODING_MODE_COUNT
#define CODING_MODE_COUNT
Definition: vp3.c:82
FFSIGN
#define FFSIGN(a)
Definition: common.h:66
GetBitContext
Definition: get_bits.h:62
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames. The frames must then be freed with ff_thread_release_buffer(). Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
SET_CHROMA_MODES
#define SET_CHROMA_MODES
tables
Writing a table generator This documentation is preliminary Parts of the API are not good and should be changed Basic concepts A table generator consists of two *_tablegen c and *_tablegen h The h file will provide the variable declarations and initialization code for the tables
Definition: tablegen.txt:10
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:463
perm
perm
Definition: f_perms.c:74
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2688
MODE_INTER_LAST_MV
#define MODE_INTER_LAST_MV
Definition: vp3.c:77
Vp3DecodeContext::y_superblock_height
int y_superblock_height
Definition: vp3.c:198
vp31_intra_c_dequant
static const uint8_t vp31_intra_c_dequant[64]
Definition: vp3data.h:42
ff_videodsp_init
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
Vp3DecodeContext::offset_y
uint8_t offset_y
Definition: vp3.c:223
Vp3DecodeContext::theora
int theora
Definition: vp3.c:174
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:571
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
loop
static int loop
Definition: ffplay.c:339
TRANSPOSE
#define TRANSPOSE(x)
AVRational::num
int num
Numerator.
Definition: rational.h:59
Vp3DecodeContext::num_kf_coded_fragment
int num_kf_coded_fragment[3]
Definition: vp3.c:272
ff_init_vlc_from_lengths
int ff_init_vlc_from_lengths(VLC *vlc_arg, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: bitstream.c:381
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
TOKEN_ZERO_RUN
#define TOKEN_ZERO_RUN(coeff, zero_run)
Definition: vp3.c:256
vp4_pred_block_type_map
static const uint8_t vp4_pred_block_type_map[8]
Definition: vp3.c:145
vp3_decode_frame
static int vp3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp3.c:2585
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:946
motion_vector_vlc_table
static const uint8_t motion_vector_vlc_table[63][2]
Definition: vp3data.h:114
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
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:678
theora_decode_tables
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
hilbert_offset
static const uint8_t hilbert_offset[16][2]
Definition: vp3.c:130
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:588
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
Vp3DecodeContext::fragment_height
int fragment_height[2]
Definition: vp3.c:217
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
init_loop_filter
static void init_loop_filter(Vp3DecodeContext *s)
Definition: vp3.c:459
Vp3DecodeContext::fragment_run_length_vlc
VLC fragment_run_length_vlc
Definition: vp3.c:279
Vp3DecodeContext::vp4_mv_vlc
VLC vp4_mv_vlc[2][7]
Definition: vp3.c:283
vp4_mv_table_selector
static const uint8_t vp4_mv_table_selector[32]
Definition: vp4data.h:105
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:361
s1
#define s1
Definition: regdef.h:38
HuffTable::nb_entries
uint8_t nb_entries
Definition: vp3.c:169
init_block_mapping
static int init_block_mapping(Vp3DecodeContext *s)
This function sets up all of the various blocks mappings: superblocks <-> fragments,...
Definition: vp3.c:382
SB_PARTIALLY_CODED
#define SB_PARTIALLY_CODED
Definition: vp3.c:66
bits
uint8_t bits
Definition: vp3data.h:141
SB_NOT_CODED
#define SB_NOT_CODED
Definition: vp3.c:65
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
Vp3Fragment::qpi
uint8_t qpi
Definition: vp3.c:62
get_bits.h
reverse_dc_prediction
static void reverse_dc_prediction(Vp3DecodeContext *s, int first_fragment, int fragment_width, int fragment_height)
Definition: vp3.c:1642
ff_vp4_decoder
const AVCodec ff_vp4_decoder
unpack_dct_coeffs
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:1300
ModeAlphabet
static const int ModeAlphabet[6][CODING_MODE_COUNT]
Definition: vp3.c:92
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
RSHIFT
#define RSHIFT(a, b)
Definition: common.h:47
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:476
ff_hpeldsp_init
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
MODE_USING_GOLDEN
#define MODE_USING_GOLDEN
Definition: vp3.c:79
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:393
Vp3DecodeContext::macroblock_width
int macroblock_width
Definition: vp3.c:208
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
Vp3DecodeContext::idct_permutation
uint8_t idct_permutation[64]
Definition: vp3.c:182
if
if(ret)
Definition: filter_design.txt:179
init_dequantizer
static void init_dequantizer(Vp3DecodeContext *s, int qpi)
Definition: vp3.c:416
MODE_INTER_FOURMV
#define MODE_INTER_FOURMV
Definition: vp3.c:81
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:54
Vp3DecodeContext::c_superblock_width
int c_superblock_width
Definition: vp3.c:200
coeff_tables
static const int16_t *const coeff_tables[32]
Definition: vp3data.h:345
Vp3DecodeContext::offset_x_warned
int offset_x_warned
Definition: vp3.c:224
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
Vp3DecodeContext::block_pattern_vlc
VLC block_pattern_vlc[2]
Definition: vp3.c:280
init_frames
static av_cold int init_frames(Vp3DecodeContext *s)
Definition: vp3.c:2311
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
HuffTable
Used to store optimal huffman encoding results.
Definition: mjpegenc_huffman.h:69
PU
#define PU
unpack_modes
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:788
transform
static const int8_t transform[32][32]
Definition: hevcdsp.c:27
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
unpack_vlcs
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, VLC *table, int coeff_index, int plane, int eob_run)
Definition: vp3.c:1179
Vp3DecodeContext::superblock_count
int superblock_count
Definition: vp3.c:196
ff_vp3dsp_h_loop_filter_12
void ff_vp3dsp_h_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
theora_decode_header
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
fragment_run_length_vlc_len
static const uint8_t fragment_run_length_vlc_len[30]
Definition: vp3data.h:105
src
#define src
Definition: vp8dsp.c:255
vp4_bias
static const uint8_t vp4_bias[5 *16][32][2]
Definition: vp4data.h:329
mathops.h
Vp3DecodeContext::theora_header
int theora_header
Definition: vp3.c:174
TOKEN_COEFF
#define TOKEN_COEFF(coeff)
Definition: vp3.c:257
vp4_y_dc_scale_factor
static const uint8_t vp4_y_dc_scale_factor[64]
Definition: vp4data.h:42
ONLY_IF_THREADS_ENABLED
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:156
Vp3DecodeContext::skip_loop_filter
int skip_loop_filter
Definition: vp3.c:190
update_frames
static int update_frames(AVCodecContext *avctx)
Release and shuffle frames after decode finishes.
Definition: vp3.c:2503
Vp3DecodeContext::last_qps
int last_qps[3]
Definition: vp3.c:194
AV_CODEC_ID_VP4
@ AV_CODEC_ID_VP4
Definition: codec_id.h:295
FF_CODEC_CAP_EXPORTS_CROPPING
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: internal.h:68
vp31_ac_scale_factor
static const uint16_t vp31_ac_scale_factor[64]
Definition: vp3data.h:76
Vp3DecodeContext::qr_size
uint8_t qr_size[2][3][64]
Definition: vp3.c:233
DC_COEFF
#define DC_COEFF(u)
Definition: vp3.c:1640
Vp3DecodeContext::vp3dsp
VP3DSPContext vp3dsp
Definition: vp3.c:186
Vp3DecodeContext::flipped_image
int flipped_image
Definition: vp3.c:188
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
vp31_intra_y_dequant
static const uint8_t vp31_intra_y_dequant[64]
Definition: vp3data.h:29
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread_frame.c:1056
ff_vp3dsp_v_loop_filter_12
void ff_vp3dsp_v_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:29
HpelDSPContext
Half-pel DSP context.
Definition: hpeldsp.h:45
Vp3DecodeContext::fragment_width
int fragment_width[2]
Definition: vp3.c:216
Vp3DecodeContext::total_num_coded_frags
int total_num_coded_frags
Definition: vp3.c:264
SB_FULLY_CODED
#define SB_FULLY_CODED
Definition: vp3.c:67
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:53
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:470
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
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:243
AVPacket::size
int size
Definition: packet.h:374
fixed_motion_vector_table
static const int8_t fixed_motion_vector_table[64]
Definition: vp3data.h:128
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
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
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:325
unpack_vectors
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:902
vp4_get_mv
static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
Definition: vp3.c:891
vlc_tables
static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2]
Definition: imc.c:119
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:318
VP4_DC_UNDEFINED
@ VP4_DC_UNDEFINED
Definition: vp3.c:142
Vp3DecodeContext::dct_tokens
int16_t * dct_tokens[3][64]
This is a list of all tokens in bitstream order.
Definition: vp3.c:253
ff_vp3_decoder
const AVCodec ff_vp3_decoder
Definition: vp3.c:3181
Vp3DecodeContext::coded_dc_scale_factor
uint16_t coded_dc_scale_factor[2][64]
Definition: vp3.c:229
Vp3DecodeContext::qps
int qps[3]
Definition: vp3.c:192
Vp3DecodeContext::current_frame
ThreadFrame current_frame
Definition: vp3.c:180
Vp3DecodeContext::block
int16_t block[64]
Definition: vp3.c:187
height
#define height
Vp3DecodeContext::chroma_y_shift
int chroma_y_shift
Definition: vp3.c:177
Vp3DecodeContext::data_offset
int data_offset[3]
Definition: vp3.c:221
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
Vp3DecodeContext::macroblock_coding
unsigned char * macroblock_coding
Definition: vp3.c:297
version
version
Definition: libkvazaar.c:313
vp3data.h
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:496
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1451
Vp3DecodeContext::avctx
AVCodecContext * avctx
Definition: vp3.c:173
AV_CODEC_ID_VP3
@ AV_CODEC_ID_VP3
Definition: codec_id.h:79
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:116
Vp3DecodeContext::nkf_coded_fragment_list
int * nkf_coded_fragment_list
Definition: vp3.c:271
Vp3DecodeContext::keyframe
int keyframe
Definition: vp3.c:181
MODE_INTRA
#define MODE_INTRA
Definition: vp3.c:75
apply_loop_filter
static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
Definition: vp3.c:1792
Vp3DecodeContext::macroblock_height
int macroblock_height
Definition: vp3.c:209
ff_vp3dsp_init
av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
Definition: vp3dsp.c:448
Vp3DecodeContext::yuv_macroblock_count
int yuv_macroblock_count
Definition: vp3.c:213
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
Vp3DecodeContext::edge_emu_buffer
uint8_t * edge_emu_buffer
Definition: vp3.c:299
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:447
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: internal.h:50
Vp3DecodeContext::c_macroblock_count
int c_macroblock_count
Definition: vp3.c:210
AV_CODEC_ID_THEORA
@ AV_CODEC_ID_THEORA
Definition: codec_id.h:80
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
superblock_run_length_vlc_lens
static const uint8_t superblock_run_length_vlc_lens[34]
Definition: vp3data.h:98
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:435
Vp3DecodeContext::macroblock_count
int macroblock_count
Definition: vp3.c:207
SUPERBLOCK_VLC_BITS
#define SUPERBLOCK_VLC_BITS
Definition: vp3.c:54
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
update_thread_context
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have update_thread_context() run it in the next thread. Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little speed gain at this point but it should work. If there are inter-frame dependencies
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:974
Vp3DecodeContext::v_superblock_start
int v_superblock_start
Definition: vp3.c:204
Vp3DecodeContext::c_superblock_height
int c_superblock_height
Definition: vp3.c:201
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
VP4_MV_VLC_BITS
#define VP4_MV_VLC_BITS
Definition: vp3.c:53
Vp3DecodeContext::coded_fragment_list
int * coded_fragment_list[3]
Definition: vp3.c:268
avcodec.h
Vp3DecodeContext::c_superblock_count
int c_superblock_count
Definition: vp3.c:202
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
PL
#define PL
AVCOL_PRI_BT470M
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:474
ret
ret
Definition: filter_design.txt:187
Vp3DecodeContext::theora_tables
int theora_tables
Definition: vp3.c:174
free_tables
static av_cold void free_tables(AVCodecContext *avctx)
Definition: vp3.c:314
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
MODE_INTER_PLUS_MV
#define MODE_INTER_PLUS_MV
Definition: vp3.c:76
Vp3DecodeContext::num_coded_frags
int num_coded_frags[3][64]
number of blocks that contain DCT coefficients at the given level or higher
Definition: vp3.c:263
vp4_block_pattern_table_selector
static const uint8_t vp4_block_pattern_table_selector[14]
Definition: vp4data.h:86
ff_thread_finish_setup
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards. If some code can 't be moved
Vp3DecodeContext::golden_frame
ThreadFrame golden_frame
Definition: vp3.c:178
Vp3DecodeContext::chroma_x_shift
int chroma_x_shift
Definition: vp3.c:177
BLOCK_X
#define BLOCK_X
Definition: vp3.c:643
MODE_COPY
#define MODE_COPY
Definition: vp3.c:85
Vp3DecodeContext
Definition: vp3.c:172
vp4_filter_limit_values
static const uint8_t vp4_filter_limit_values[64]
Definition: vp4data.h:75
ff_set_sar
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:101
MODE_GOLDEN_MV
#define MODE_GOLDEN_MV
Definition: vp3.c:80
FRAGMENT_PIXELS
#define FRAGMENT_PIXELS
Definition: vp3.c:56
AVCodecContext
main external API structure.
Definition: avcodec.h:383
ThreadFrame
Definition: thread.h:34
vp3_draw_horiz_band
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
called when all pixels up to row y are complete
Definition: vp3.c:1904
AVCHROMA_LOC_CENTER
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:619
vp4_generic_dequant
static const uint8_t vp4_generic_dequant[64]
Definition: vp4data.h:31
zero_run_get_bits
static const uint8_t zero_run_get_bits[32]
Definition: vp3data.h:153
AVRational::den
int den
Denominator.
Definition: rational.h:60
await_reference_row
static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
Wait for the reference frame of the current fragment.
Definition: vp3.c:1946
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
VLC
Definition: vlc.h:26
Vp3DecodeContext::coded_ac_scale_factor
uint32_t coded_ac_scale_factor[64]
Definition: vp3.c:230
output_plane
static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, ptrdiff_t dst_pitch, int dst_height)
Convert and output the current plane.
Definition: indeo3.c:1026
HuffEntry
Definition: exr.c:93
vp31_inter_dequant
static const uint8_t vp31_inter_dequant[64]
Definition: vp3data.h:54
temp
else temp
Definition: vf_mcdeint.c:248
body
static void body(uint32_t ABCD[4], const uint8_t *src, size_t nblocks)
Definition: md5.c:101
ff_theora_decoder
const AVCodec ff_theora_decoder
vp4_block_pattern_vlc
static const uint8_t vp4_block_pattern_vlc[2][14][2]
Definition: vp4data.h:90
avpriv_split_xiph_headers
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition: xiph.c:24
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
MODE_INTER_NO_MV
#define MODE_INTER_NO_MV
Definition: vp3.c:74
VideoDSPContext
Definition: videodsp.h:41
HuffEntry::sym
uint8_t sym
Definition: vp3.c:164
Vp3DecodeContext::coeff_vlc
VLC coeff_vlc[5 *16]
Definition: vp3.c:276
Vp3DecodeContext::superblock_coding
unsigned char * superblock_coding
Definition: vp3.c:205
COMPATIBLE_FRAME
#define COMPATIBLE_FRAME(x)
Definition: vp3.c:1638
AVERROR_DECODER_NOT_FOUND
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:54
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:571
Vp3DecodeContext::last_frame
ThreadFrame last_frame
Definition: vp3.c:179
Vp3DecodeContext::fragment_start
int fragment_start[3]
Definition: vp3.c:220
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
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:86
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:272
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: internal.h:77
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
vp3_bias
static const uint8_t vp3_bias[5 *16][32][2]
Definition: vp3data.h:383
get_eob_run
static int get_eob_run(GetBitContext *gb, int token)
Definition: vp3.c:1143
HuffTable::entries
HuffEntry entries[32]
Definition: vp3.c:168
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:408
Vp3DecodeContext::huffman_table
HuffTable huffman_table[5 *16]
Definition: vp3.c:302
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
vp31_filter_limit_values
static const uint8_t vp31_filter_limit_values[64]
Definition: vp3data.h:87
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
VP4Predictor::type
int type
Definition: vp3.c:158
VP4_DC_INTER
@ VP4_DC_INTER
Definition: vp3.c:139
vp3_decode_init
static av_cold int vp3_decode_init(AVCodecContext *avctx)
Definition: vp3.c:2323
Vp3DecodeContext::base_matrix
uint8_t base_matrix[384][64]
Definition: vp3.c:231
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
videodsp.h
VP3_MV_VLC_BITS
#define VP3_MV_VLC_BITS
Definition: vp3.c:52
Vp3DecodeContext::fragment_count
int fragment_count
Definition: vp3.c:215
vp31_dc_scale_factor
static const uint8_t vp31_dc_scale_factor[64]
Definition: vp3data.h:65
d
d
Definition: ffmpeg_filter.c:153
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
imgutils.h
hpeldsp.h
Vp3DecodeContext::width
int width
Definition: vp3.c:176
Vp3DecodeContext::kf_coded_fragment_list
int * kf_coded_fragment_list
Definition: vp3.c:270
AV_CODEC_CAP_DRAW_HORIZ_BAND
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: codec.h:44
unpack_block_qpis
static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:1100
Vp3DecodeContext::qr_base
uint16_t qr_base[2][3][64]
Definition: vp3.c:234
vp3_decode_end
static av_cold int vp3_decode_end(AVCodecContext *avctx)
Definition: vp3.c:342
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:78
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2038
vp4_uv_dc_scale_factor
static const uint8_t vp4_uv_dc_scale_factor[64]
Definition: vp4data.h:53
MAXIMUM_LONG_BIT_RUN
#define MAXIMUM_LONG_BIT_RUN
Definition: vp3.c:72
Vp3DecodeContext::version
int version
Definition: vp3.c:175
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
Vp3DecodeContext::motion_val
int8_t(*[2] motion_val)[2]
Definition: vp3.c:226
eob_run_table
static const struct @164 eob_run_table[7]
Vp3DecodeContext::last_slice_end
int last_slice_end
Definition: vp3.c:189
Vp3DecodeContext::dc_pred_row
VP4Predictor * dc_pred_row
Definition: vp3.c:307
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
Vp3DecodeContext::u_superblock_start
int u_superblock_start
Definition: vp3.c:203
coeff_get_bits
static const uint8_t coeff_get_bits[32]
Definition: vp3data.h:161
Vp3DecodeContext::dct_tokens_base
int16_t * dct_tokens_base
Definition: vp3.c:254
Vp3Fragment
Definition: vp3.c:59
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:753
Vp3DecodeContext::nqps
int nqps
Definition: vp3.c:193
Vp3DecodeContext::qmat
int16_t qmat[3][2][3][64]
qmat[qpi][is_inter][plane]
Definition: vp3.c:287
Vp3DecodeContext::vdsp
VideoDSPContext vdsp
Definition: vp3.c:185
TOKEN_EOB
#define TOKEN_EOB(eob_run)
Definition: vp3.c:255
Vp3DecodeContext::c_macroblock_width
int c_macroblock_width
Definition: vp3.c:211