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 
39 #include "avcodec.h"
40 #include "get_bits.h"
41 #include "hpeldsp.h"
42 #include "internal.h"
43 #include "mathops.h"
44 #include "thread.h"
45 #include "videodsp.h"
46 #include "vp3data.h"
47 #include "vp4data.h"
48 #include "vp3dsp.h"
49 #include "xiph.h"
50 
51 #define FRAGMENT_PIXELS 8
52 
53 // FIXME split things out into their own arrays
54 typedef struct Vp3Fragment {
55  int16_t dc;
58 } Vp3Fragment;
59 
60 #define SB_NOT_CODED 0
61 #define SB_PARTIALLY_CODED 1
62 #define SB_FULLY_CODED 2
63 
64 // This is the maximum length of a single long bit run that can be encoded
65 // for superblock coding or block qps. Theora special-cases this to read a
66 // bit instead of flipping the current bit to allow for runs longer than 4129.
67 #define MAXIMUM_LONG_BIT_RUN 4129
68 
69 #define MODE_INTER_NO_MV 0
70 #define MODE_INTRA 1
71 #define MODE_INTER_PLUS_MV 2
72 #define MODE_INTER_LAST_MV 3
73 #define MODE_INTER_PRIOR_LAST 4
74 #define MODE_USING_GOLDEN 5
75 #define MODE_GOLDEN_MV 6
76 #define MODE_INTER_FOURMV 7
77 #define CODING_MODE_COUNT 8
78 
79 /* special internal mode */
80 #define MODE_COPY 8
81 
82 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
83 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
84 
85 
86 /* There are 6 preset schemes, plus a free-form scheme */
87 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
88  /* scheme 1: Last motion vector dominates */
93 
94  /* scheme 2 */
98  MODE_GOLDEN_MV, MODE_INTER_FOURMV },
99 
100  /* scheme 3 */
104  MODE_GOLDEN_MV, MODE_INTER_FOURMV },
105 
106  /* scheme 4 */
110  MODE_GOLDEN_MV, MODE_INTER_FOURMV },
111 
112  /* scheme 5: No motion vector dominates */
116  MODE_GOLDEN_MV, MODE_INTER_FOURMV },
117 
118  /* scheme 6 */
122  MODE_GOLDEN_MV, MODE_INTER_FOURMV },
123 };
124 
125 static const uint8_t hilbert_offset[16][2] = {
126  { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
127  { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
128  { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
129  { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
130 };
131 
132 enum {
138 };
139 
140 static const uint8_t vp4_pred_block_type_map[8] = {
149 };
150 
151 typedef struct {
152  int dc;
153  int type;
154 } VP4Predictor;
155 
156 #define MIN_DEQUANT_VAL 2
157 
158 typedef struct Vp3DecodeContext {
161  int version;
162  int width, height;
163  int chroma_x_shift, chroma_y_shift;
167  int keyframe;
168  uint8_t idct_permutation[64];
169  uint8_t idct_scantable[64];
173  DECLARE_ALIGNED(16, int16_t, block)[64];
177 
178  int qps[3];
179  int nqps;
180  int last_qps[3];
181 
191  unsigned char *superblock_coding;
192 
193  int macroblock_count; /* y macroblock count */
199  int yuv_macroblock_count; /* y+u+v macroblock count */
200 
202  int fragment_width[2];
203  int fragment_height[2];
204 
206  int fragment_start[3];
207  int data_offset[3];
211 
212  int8_t (*motion_val[2])[2];
213 
214  /* tables */
215  uint16_t coded_dc_scale_factor[2][64];
216  uint32_t coded_ac_scale_factor[64];
217  uint8_t base_matrix[384][64];
218  uint8_t qr_count[2][3];
219  uint8_t qr_size[2][3][64];
220  uint16_t qr_base[2][3][64];
221 
222  /**
223  * This is a list of all tokens in bitstream order. Reordering takes place
224  * by pulling from each level during IDCT. As a consequence, IDCT must be
225  * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
226  * otherwise. The 32 different tokens with up to 12 bits of extradata are
227  * collapsed into 3 types, packed as follows:
228  * (from the low to high bits)
229  *
230  * 2 bits: type (0,1,2)
231  * 0: EOB run, 14 bits for run length (12 needed)
232  * 1: zero run, 7 bits for run length
233  * 7 bits for the next coefficient (3 needed)
234  * 2: coefficient, 14 bits (11 needed)
235  *
236  * Coefficients are signed, so are packed in the highest bits for automatic
237  * sign extension.
238  */
239  int16_t *dct_tokens[3][64];
240  int16_t *dct_tokens_base;
241 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
242 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
243 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
244 
245  /**
246  * number of blocks that contain DCT coefficients at
247  * the given level or higher
248  */
249  int num_coded_frags[3][64];
251 
252  /* this is a list of indexes into the all_fragments array indicating
253  * which of the fragments are coded */
254  int *coded_fragment_list[3];
255 
258  int num_kf_coded_fragment[3];
259 
260  VLC dc_vlc[16];
261  VLC ac_vlc_1[16];
262  VLC ac_vlc_2[16];
263  VLC ac_vlc_3[16];
264  VLC ac_vlc_4[16];
265 
266  VLC superblock_run_length_vlc; /* version < 2 */
267  VLC fragment_run_length_vlc; /* version < 2 */
268  VLC block_pattern_vlc[2]; /* version >= 2*/
270  VLC motion_vector_vlc; /* version < 2 */
271  VLC vp4_mv_vlc[2][7]; /* version >=2 */
272 
273  /* these arrays need to be on 16-byte boundaries since SSE2 operations
274  * index into them */
275  DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
276 
277  /* This table contains superblock_count * 16 entries. Each set of 16
278  * numbers corresponds to the fragment indexes 0..15 of the superblock.
279  * An entry will be -1 to indicate that no entry corresponds to that
280  * index. */
282 
283  /* This is an array that indicates how a particular macroblock
284  * is coded. */
285  unsigned char *macroblock_coding;
286 
288 
289  /* Huffman decode */
290  int hti;
291  unsigned int hbits;
292  int entries;
294  uint32_t huffman_table[80][32][2];
295 
296  uint8_t filter_limit_values[64];
297  DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
298 
299  VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
301 
302 /************************************************************************
303  * VP3 specific functions
304  ************************************************************************/
305 
306 static av_cold void free_tables(AVCodecContext *avctx)
307 {
308  Vp3DecodeContext *s = avctx->priv_data;
309 
311  av_freep(&s->all_fragments);
317  av_freep(&s->dc_pred_row);
318  av_freep(&s->motion_val[0]);
319  av_freep(&s->motion_val[1]);
320 }
321 
322 static void vp3_decode_flush(AVCodecContext *avctx)
323 {
324  Vp3DecodeContext *s = avctx->priv_data;
325 
326  if (s->golden_frame.f)
328  if (s->last_frame.f)
330  if (s->current_frame.f)
332 }
333 
335 {
336  Vp3DecodeContext *s = avctx->priv_data;
337  int i, j;
338 
339  free_tables(avctx);
341 
342  s->theora_tables = 0;
343 
344  /* release all frames */
345  vp3_decode_flush(avctx);
349 
350  if (avctx->internal->is_copy)
351  return 0;
352 
353  for (i = 0; i < 16; i++) {
354  ff_free_vlc(&s->dc_vlc[i]);
355  ff_free_vlc(&s->ac_vlc_1[i]);
356  ff_free_vlc(&s->ac_vlc_2[i]);
357  ff_free_vlc(&s->ac_vlc_3[i]);
358  ff_free_vlc(&s->ac_vlc_4[i]);
359  }
360 
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++)
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)
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  */
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 {
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] = {
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) {
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,
497  6, 2) + 1;
498  if (current_run == 34)
499  current_run += get_bits(gb, 12);
500 
501  if (current_run > s->superblock_count - current_superblock) {
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,
531  6, 2) + 1;
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) {
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;
567 
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  if (v == -1) {
691  av_log(s->avctx, AV_LOG_ERROR, "Invalid block pattern\n");
692  *next_block_pattern_table = 0;
693  return 0;
694  }
695  *next_block_pattern_table = vp4_block_pattern_table_selector[v];
696  return v + 1;
697 }
698 
699 static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
700 {
701  int plane, i, j, k, fragment;
702  int next_block_pattern_table;
703  int bit, current_run, has_partial;
704 
706 
707  if (s->keyframe)
708  return 0;
709 
710  has_partial = 0;
711  bit = get_bits1(gb);
712  for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
713  current_run = vp4_get_mb_count(s, gb);
714  if (current_run > s->yuv_macroblock_count - i)
715  return -1;
716  memset(s->superblock_coding + i, 2 * bit, current_run);
717  bit ^= 1;
718  has_partial |= bit;
719  }
720 
721  if (has_partial) {
722  bit = get_bits1(gb);
723  current_run = vp4_get_mb_count(s, gb);
724  for (i = 0; i < s->yuv_macroblock_count; i++) {
725  if (!s->superblock_coding[i]) {
726  if (!current_run) {
727  bit ^= 1;
728  current_run = vp4_get_mb_count(s, gb);
729  }
730  s->superblock_coding[i] = bit;
731  current_run--;
732  }
733  }
734  if (current_run) /* handle situation when vp4_get_mb_count() fails */
735  return -1;
736  }
737 
738  next_block_pattern_table = 0;
739  i = 0;
740  for (plane = 0; plane < 3; plane++) {
741  int sb_x, sb_y;
742  int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
743  int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
744  int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
745  int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
746  int fragment_width = s->fragment_width[!!plane];
747  int fragment_height = s->fragment_height[!!plane];
748 
749  for (sb_y = 0; sb_y < sb_height; sb_y++) {
750  for (sb_x = 0; sb_x < sb_width; sb_x++) {
751  for (j = 0; j < 4; j++) {
752  int mb_x = 2 * sb_x + (j >> 1);
753  int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
754  int mb_coded, pattern, coded;
755 
756  if (mb_x >= mb_width || mb_y >= mb_height)
757  continue;
758 
759  mb_coded = s->superblock_coding[i++];
760 
761  if (mb_coded == SB_FULLY_CODED)
762  pattern = 0xF;
763  else if (mb_coded == SB_PARTIALLY_CODED)
764  pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
765  else
766  pattern = 0;
767 
768  for (k = 0; k < 4; k++) {
769  if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
770  continue;
771  fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
772  coded = pattern & (8 >> k);
773  /* MODE_INTER_NO_MV is the default for coded fragments.
774  the actual method is decoded in the next phase. */
775  s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
776  }
777  }
778  }
779  }
780  }
781  return 0;
782 }
783 #endif
784 
785 /*
786  * This function unpacks all the coding mode data for individual macroblocks
787  * from the bitstream.
788  */
790 {
791  int i, j, k, sb_x, sb_y;
792  int scheme;
793  int current_macroblock;
794  int current_fragment;
795  int coding_mode;
796  int custom_mode_alphabet[CODING_MODE_COUNT];
797  const int *alphabet;
798  Vp3Fragment *frag;
799 
800  if (s->keyframe) {
801  for (i = 0; i < s->fragment_count; i++)
803  } else {
804  /* fetch the mode coding scheme for this frame */
805  scheme = get_bits(gb, 3);
806 
807  /* is it a custom coding scheme? */
808  if (scheme == 0) {
809  for (i = 0; i < 8; i++)
810  custom_mode_alphabet[i] = MODE_INTER_NO_MV;
811  for (i = 0; i < 8; i++)
812  custom_mode_alphabet[get_bits(gb, 3)] = i;
813  alphabet = custom_mode_alphabet;
814  } else
815  alphabet = ModeAlphabet[scheme - 1];
816 
817  /* iterate through all of the macroblocks that contain 1 or more
818  * coded fragments */
819  for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
820  for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
821  if (get_bits_left(gb) <= 0)
822  return -1;
823 
824  for (j = 0; j < 4; j++) {
825  int mb_x = 2 * sb_x + (j >> 1);
826  int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
827  current_macroblock = mb_y * s->macroblock_width + mb_x;
828 
829  if (mb_x >= s->macroblock_width ||
830  mb_y >= s->macroblock_height)
831  continue;
832 
833  /* coding modes are only stored if the macroblock has
834  * at least one luma block coded, otherwise it must be
835  * INTER_NO_MV */
836  for (k = 0; k < 4; k++) {
837  current_fragment = BLOCK_Y *
838  s->fragment_width[0] + BLOCK_X;
839  if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
840  break;
841  }
842  if (k == 4) {
843  s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
844  continue;
845  }
846 
847  /* mode 7 means get 3 bits for each coding mode */
848  if (scheme == 7)
849  coding_mode = get_bits(gb, 3);
850  else
851  coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
852 
853  s->macroblock_coding[current_macroblock] = coding_mode;
854  for (k = 0; k < 4; k++) {
855  frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
856  if (frag->coding_method != MODE_COPY)
857  frag->coding_method = coding_mode;
858  }
859 
860 #define SET_CHROMA_MODES \
861  if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
862  frag[s->fragment_start[1]].coding_method = coding_mode; \
863  if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
864  frag[s->fragment_start[2]].coding_method = coding_mode;
865 
866  if (s->chroma_y_shift) {
867  frag = s->all_fragments + mb_y *
868  s->fragment_width[1] + mb_x;
870  } else if (s->chroma_x_shift) {
871  frag = s->all_fragments +
872  2 * mb_y * s->fragment_width[1] + mb_x;
873  for (k = 0; k < 2; k++) {
875  frag += s->fragment_width[1];
876  }
877  } else {
878  for (k = 0; k < 4; k++) {
879  frag = s->all_fragments +
880  BLOCK_Y * s->fragment_width[1] + BLOCK_X;
882  }
883  }
884  }
885  }
886  }
887  }
888 
889  return 0;
890 }
891 
892 static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
893 {
894  int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, 6, 2) - 31;
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] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
952  motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
953  } else if (coding_mode == 1) {
954  motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
955  motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
956  } else { /* VP4 */
957  motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
958  motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
959  }
960 
961  /* vector maintenance, only on MODE_INTER_PLUS_MV */
962  if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
963  prior_last_motion_x = last_motion_x;
964  prior_last_motion_y = last_motion_y;
965  last_motion_x = motion_x[0];
966  last_motion_y = motion_y[0];
967  }
968  break;
969 
970  case MODE_INTER_FOURMV:
971  /* vector maintenance */
972  prior_last_motion_x = last_motion_x;
973  prior_last_motion_y = last_motion_y;
974 
975  /* fetch 4 vectors from the bitstream, one for each
976  * Y fragment, then average for the C fragment vectors */
977  for (k = 0; k < 4; k++) {
978  current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
979  if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
980  if (coding_mode == 0) {
981  motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
982  motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
983  } else if (coding_mode == 1) {
984  motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
985  motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
986  } else { /* VP4 */
987  motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
988  motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
989  }
990  last_motion_x = motion_x[k];
991  last_motion_y = motion_y[k];
992  } else {
993  motion_x[k] = 0;
994  motion_y[k] = 0;
995  }
996  }
997  break;
998 
999  case MODE_INTER_LAST_MV:
1000  /* all 6 fragments use the last motion vector */
1001  motion_x[0] = last_motion_x;
1002  motion_y[0] = last_motion_y;
1003 
1004  /* no vector maintenance (last vector remains the
1005  * last vector) */
1006  break;
1007 
1008  case MODE_INTER_PRIOR_LAST:
1009  /* all 6 fragments use the motion vector prior to the
1010  * last motion vector */
1011  motion_x[0] = prior_last_motion_x;
1012  motion_y[0] = prior_last_motion_y;
1013 
1014  /* vector maintenance */
1015  prior_last_motion_x = last_motion_x;
1016  prior_last_motion_y = last_motion_y;
1017  last_motion_x = motion_x[0];
1018  last_motion_y = motion_y[0];
1019  break;
1020 
1021  default:
1022  /* covers intra, inter without MV, golden without MV */
1023  motion_x[0] = 0;
1024  motion_y[0] = 0;
1025 
1026  /* no vector maintenance */
1027  break;
1028  }
1029 
1030  /* assign the motion vectors to the correct fragments */
1031  for (k = 0; k < 4; k++) {
1032  current_fragment =
1033  BLOCK_Y * s->fragment_width[0] + BLOCK_X;
1034  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1035  s->motion_val[0][current_fragment][0] = motion_x[k];
1036  s->motion_val[0][current_fragment][1] = motion_y[k];
1037  } else {
1038  s->motion_val[0][current_fragment][0] = motion_x[0];
1039  s->motion_val[0][current_fragment][1] = motion_y[0];
1040  }
1041  }
1042 
1043  if (s->chroma_y_shift) {
1044  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1045  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
1046  motion_x[2] + motion_x[3], 2);
1047  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
1048  motion_y[2] + motion_y[3], 2);
1049  }
1050  if (s->version <= 2) {
1051  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1052  motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
1053  }
1054  frag = mb_y * s->fragment_width[1] + mb_x;
1055  s->motion_val[1][frag][0] = motion_x[0];
1056  s->motion_val[1][frag][1] = motion_y[0];
1057  } else if (s->chroma_x_shift) {
1058  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1059  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
1060  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
1061  motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
1062  motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
1063  } else {
1064  motion_x[1] = motion_x[0];
1065  motion_y[1] = motion_y[0];
1066  }
1067  if (s->version <= 2) {
1068  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
1069  motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
1070  }
1071  frag = 2 * mb_y * s->fragment_width[1] + mb_x;
1072  for (k = 0; k < 2; k++) {
1073  s->motion_val[1][frag][0] = motion_x[k];
1074  s->motion_val[1][frag][1] = motion_y[k];
1075  frag += s->fragment_width[1];
1076  }
1077  } else {
1078  for (k = 0; k < 4; k++) {
1079  frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
1080  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
1081  s->motion_val[1][frag][0] = motion_x[k];
1082  s->motion_val[1][frag][1] = motion_y[k];
1083  } else {
1084  s->motion_val[1][frag][0] = motion_x[0];
1085  s->motion_val[1][frag][1] = motion_y[0];
1086  }
1087  }
1088  }
1089  }
1090  }
1091  }
1092 
1093  return 0;
1094 }
1095 
1097 {
1098  int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
1099  int num_blocks = s->total_num_coded_frags;
1100 
1101  for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
1102  i = blocks_decoded = num_blocks_at_qpi = 0;
1103 
1104  bit = get_bits1(gb) ^ 1;
1105  run_length = 0;
1106 
1107  do {
1108  if (run_length == MAXIMUM_LONG_BIT_RUN)
1109  bit = get_bits1(gb);
1110  else
1111  bit ^= 1;
1112 
1113  run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
1114  if (run_length == 34)
1115  run_length += get_bits(gb, 12);
1116  blocks_decoded += run_length;
1117 
1118  if (!bit)
1119  num_blocks_at_qpi += run_length;
1120 
1121  for (j = 0; j < run_length; i++) {
1122  if (i >= s->total_num_coded_frags)
1123  return -1;
1124 
1125  if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1126  s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1127  j++;
1128  }
1129  }
1130  } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
1131 
1132  num_blocks -= num_blocks_at_qpi;
1133  }
1134 
1135  return 0;
1136 }
1137 
1138 static inline int get_eob_run(GetBitContext *gb, int token)
1139 {
1140  int v = eob_run_table[token].base;
1141  if (eob_run_table[token].bits)
1142  v += get_bits(gb, eob_run_table[token].bits);
1143  return v;
1144 }
1145 
1146 static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
1147 {
1148  int bits_to_get, zero_run;
1149 
1150  bits_to_get = coeff_get_bits[token];
1151  if (bits_to_get)
1152  bits_to_get = get_bits(gb, bits_to_get);
1153  *coeff = coeff_tables[token][bits_to_get];
1154 
1155  zero_run = zero_run_base[token];
1156  if (zero_run_get_bits[token])
1157  zero_run += get_bits(gb, zero_run_get_bits[token]);
1158 
1159  return zero_run;
1160 }
1161 
1162 /*
1163  * This function is called by unpack_dct_coeffs() to extract the VLCs from
1164  * the bitstream. The VLCs encode tokens which are used to unpack DCT
1165  * data. This function unpacks all the VLCs for either the Y plane or both
1166  * C planes, and is called for DC coefficients or different AC coefficient
1167  * levels (since different coefficient types require different VLC tables.
1168  *
1169  * This function returns a residual eob run. E.g, if a particular token gave
1170  * instructions to EOB the next 5 fragments and there were only 2 fragments
1171  * left in the current fragment range, 3 would be returned so that it could
1172  * be passed into the next call to this same function.
1173  */
1175  VLC *table, int coeff_index,
1176  int plane,
1177  int eob_run)
1178 {
1179  int i, j = 0;
1180  int token;
1181  int zero_run = 0;
1182  int16_t coeff = 0;
1183  int blocks_ended;
1184  int coeff_i = 0;
1185  int num_coeffs = s->num_coded_frags[plane][coeff_index];
1186  int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1187 
1188  /* local references to structure members to avoid repeated dereferences */
1189  int *coded_fragment_list = s->coded_fragment_list[plane];
1190  Vp3Fragment *all_fragments = s->all_fragments;
1191  VLC_TYPE(*vlc_table)[2] = table->table;
1192 
1193  if (num_coeffs < 0) {
1195  "Invalid number of coefficients at level %d\n", coeff_index);
1196  return AVERROR_INVALIDDATA;
1197  }
1198 
1199  if (eob_run > num_coeffs) {
1200  coeff_i =
1201  blocks_ended = num_coeffs;
1202  eob_run -= num_coeffs;
1203  } else {
1204  coeff_i =
1205  blocks_ended = eob_run;
1206  eob_run = 0;
1207  }
1208 
1209  // insert fake EOB token to cover the split between planes or zzi
1210  if (blocks_ended)
1211  dct_tokens[j++] = blocks_ended << 2;
1212 
1213  while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
1214  /* decode a VLC into a token */
1215  token = get_vlc2(gb, vlc_table, 11, 3);
1216  /* use the token to get a zero run, a coefficient, and an eob run */
1217  if ((unsigned) token <= 6U) {
1218  eob_run = get_eob_run(gb, token);
1219  if (!eob_run)
1220  eob_run = INT_MAX;
1221 
1222  // record only the number of blocks ended in this plane,
1223  // any spill will be recorded in the next plane.
1224  if (eob_run > num_coeffs - coeff_i) {
1225  dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
1226  blocks_ended += num_coeffs - coeff_i;
1227  eob_run -= num_coeffs - coeff_i;
1228  coeff_i = num_coeffs;
1229  } else {
1230  dct_tokens[j++] = TOKEN_EOB(eob_run);
1231  blocks_ended += eob_run;
1232  coeff_i += eob_run;
1233  eob_run = 0;
1234  }
1235  } else if (token >= 0) {
1236  zero_run = get_coeff(gb, token, &coeff);
1237 
1238  if (zero_run) {
1239  dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1240  } else {
1241  // Save DC into the fragment structure. DC prediction is
1242  // done in raster order, so the actual DC can't be in with
1243  // other tokens. We still need the token in dct_tokens[]
1244  // however, or else the structure collapses on itself.
1245  if (!coeff_index)
1246  all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1247 
1248  dct_tokens[j++] = TOKEN_COEFF(coeff);
1249  }
1250 
1251  if (coeff_index + zero_run > 64) {
1253  "Invalid zero run of %d with %d coeffs left\n",
1254  zero_run, 64 - coeff_index);
1255  zero_run = 64 - coeff_index;
1256  }
1257 
1258  // zero runs code multiple coefficients,
1259  // so don't try to decode coeffs for those higher levels
1260  for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1261  s->num_coded_frags[plane][i]--;
1262  coeff_i++;
1263  } else {
1264  av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1265  return -1;
1266  }
1267  }
1268 
1269  if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1270  av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1271 
1272  // decrement the number of blocks that have higher coefficients for each
1273  // EOB run at this level
1274  if (blocks_ended)
1275  for (i = coeff_index + 1; i < 64; i++)
1276  s->num_coded_frags[plane][i] -= blocks_ended;
1277 
1278  // setup the next buffer
1279  if (plane < 2)
1280  s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1281  else if (coeff_index < 63)
1282  s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1283 
1284  return eob_run;
1285 }
1286 
1288  int first_fragment,
1289  int fragment_width,
1290  int fragment_height);
1291 /*
1292  * This function unpacks all of the DCT coefficient data from the
1293  * bitstream.
1294  */
1296 {
1297  int i;
1298  int dc_y_table;
1299  int dc_c_table;
1300  int ac_y_table;
1301  int ac_c_table;
1302  int residual_eob_run = 0;
1303  VLC *y_tables[64];
1304  VLC *c_tables[64];
1305 
1306  s->dct_tokens[0][0] = s->dct_tokens_base;
1307 
1308  if (get_bits_left(gb) < 16)
1309  return AVERROR_INVALIDDATA;
1310 
1311  /* fetch the DC table indexes */
1312  dc_y_table = get_bits(gb, 4);
1313  dc_c_table = get_bits(gb, 4);
1314 
1315  /* unpack the Y plane DC coefficients */
1316  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1317  0, residual_eob_run);
1318  if (residual_eob_run < 0)
1319  return residual_eob_run;
1320  if (get_bits_left(gb) < 8)
1321  return AVERROR_INVALIDDATA;
1322 
1323  /* reverse prediction of the Y-plane DC coefficients */
1325 
1326  /* unpack the C plane DC coefficients */
1327  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1328  1, residual_eob_run);
1329  if (residual_eob_run < 0)
1330  return residual_eob_run;
1331  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1332  2, residual_eob_run);
1333  if (residual_eob_run < 0)
1334  return residual_eob_run;
1335 
1336  /* reverse prediction of the C-plane DC coefficients */
1337  if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1339  s->fragment_width[1], s->fragment_height[1]);
1341  s->fragment_width[1], s->fragment_height[1]);
1342  }
1343 
1344  if (get_bits_left(gb) < 8)
1345  return AVERROR_INVALIDDATA;
1346  /* fetch the AC table indexes */
1347  ac_y_table = get_bits(gb, 4);
1348  ac_c_table = get_bits(gb, 4);
1349 
1350  /* build tables of AC VLC tables */
1351  for (i = 1; i <= 5; i++) {
1352  y_tables[i] = &s->ac_vlc_1[ac_y_table];
1353  c_tables[i] = &s->ac_vlc_1[ac_c_table];
1354  }
1355  for (i = 6; i <= 14; i++) {
1356  y_tables[i] = &s->ac_vlc_2[ac_y_table];
1357  c_tables[i] = &s->ac_vlc_2[ac_c_table];
1358  }
1359  for (i = 15; i <= 27; i++) {
1360  y_tables[i] = &s->ac_vlc_3[ac_y_table];
1361  c_tables[i] = &s->ac_vlc_3[ac_c_table];
1362  }
1363  for (i = 28; i <= 63; i++) {
1364  y_tables[i] = &s->ac_vlc_4[ac_y_table];
1365  c_tables[i] = &s->ac_vlc_4[ac_c_table];
1366  }
1367 
1368  /* decode all AC coefficients */
1369  for (i = 1; i <= 63; i++) {
1370  residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1371  0, residual_eob_run);
1372  if (residual_eob_run < 0)
1373  return residual_eob_run;
1374 
1375  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1376  1, residual_eob_run);
1377  if (residual_eob_run < 0)
1378  return residual_eob_run;
1379  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1380  2, residual_eob_run);
1381  if (residual_eob_run < 0)
1382  return residual_eob_run;
1383  }
1384 
1385  return 0;
1386 }
1387 
1388 #if CONFIG_VP4_DECODER
1389 /**
1390  * eob_tracker[] is instead of TOKEN_EOB(value)
1391  * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
1392  *
1393  * @return < 0 on error
1394  */
1395 static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1396  VLC *vlc_tables[64],
1397  int plane, int eob_tracker[64], int fragment)
1398 {
1399  int token;
1400  int zero_run = 0;
1401  int16_t coeff = 0;
1402  int coeff_i = 0;
1403  int eob_run;
1404 
1405  while (!eob_tracker[coeff_i]) {
1406 
1407  token = get_vlc2(gb, vlc_tables[coeff_i]->table, 11, 3);
1408 
1409  /* use the token to get a zero run, a coefficient, and an eob run */
1410  if ((unsigned) token <= 6U) {
1411  eob_run = get_eob_run(gb, token);
1412  *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1413  eob_tracker[coeff_i] = eob_run - 1;
1414  return 0;
1415  } else if (token >= 0) {
1416  zero_run = get_coeff(gb, token, &coeff);
1417 
1418  if (zero_run) {
1419  if (coeff_i + zero_run > 64) {
1421  "Invalid zero run of %d with %d coeffs left\n",
1422  zero_run, 64 - coeff_i);
1423  zero_run = 64 - coeff_i;
1424  }
1425  *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
1426  coeff_i += zero_run;
1427  } else {
1428  if (!coeff_i)
1429  s->all_fragments[fragment].dc = coeff;
1430 
1431  *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
1432  }
1433  coeff_i++;
1434  if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
1435  return 0; /* stop */
1436  } else {
1437  av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1438  return -1;
1439  }
1440  }
1441  *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
1442  eob_tracker[coeff_i]--;
1443  return 0;
1444 }
1445 
1446 static void vp4_dc_predictor_reset(VP4Predictor *p)
1447 {
1448  p->dc = 0;
1449  p->type = VP4_DC_UNDEFINED;
1450 }
1451 
1452 static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1453 {
1454  int i, j;
1455 
1456  for (i = 0; i < 4; i++)
1457  dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
1458 
1459  for (j = 1; j < 5; j++)
1460  for (i = 0; i < 4; i++)
1461  vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
1462 }
1463 
1464 static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
1465 {
1466  int i;
1467 
1468  for (i = 0; i < 4; i++)
1469  s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
1470 
1471  for (i = 1; i < 5; i++)
1472  dc_pred[i][0] = dc_pred[i][4];
1473 }
1474 
1475 /* note: dc_pred points to the current block */
1476 static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
1477 {
1478  int count = 0;
1479  int dc = 0;
1480 
1481  if (dc_pred[-6].type == type) {
1482  dc += dc_pred[-6].dc;
1483  count++;
1484  }
1485 
1486  if (dc_pred[6].type == type) {
1487  dc += dc_pred[6].dc;
1488  count++;
1489  }
1490 
1491  if (count != 2 && dc_pred[-1].type == type) {
1492  dc += dc_pred[-1].dc;
1493  count++;
1494  }
1495 
1496  if (count != 2 && dc_pred[1].type == type) {
1497  dc += dc_pred[1].dc;
1498  count++;
1499  }
1500 
1501  /* using division instead of shift to correctly handle negative values */
1502  return count == 2 ? dc / 2 : last_dc[type];
1503 }
1504 
1505 static void vp4_set_tokens_base(Vp3DecodeContext *s)
1506 {
1507  int plane, i;
1508  int16_t *base = s->dct_tokens_base;
1509  for (plane = 0; plane < 3; plane++) {
1510  for (i = 0; i < 64; i++) {
1511  s->dct_tokens[plane][i] = base;
1512  base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
1513  }
1514  }
1515 }
1516 
1517 static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1518 {
1519  int i, j;
1520  int dc_y_table;
1521  int dc_c_table;
1522  int ac_y_table;
1523  int ac_c_table;
1524  VLC *tables[2][64];
1525  int plane, sb_y, sb_x;
1526  int eob_tracker[64];
1527  VP4Predictor dc_pred[6][6];
1528  int last_dc[NB_VP4_DC_TYPES];
1529 
1530  if (get_bits_left(gb) < 16)
1531  return AVERROR_INVALIDDATA;
1532 
1533  /* fetch the DC table indexes */
1534  dc_y_table = get_bits(gb, 4);
1535  dc_c_table = get_bits(gb, 4);
1536 
1537  ac_y_table = get_bits(gb, 4);
1538  ac_c_table = get_bits(gb, 4);
1539 
1540  /* build tables of DC/AC VLC tables */
1541 
1542  tables[0][0] = &s->dc_vlc[dc_y_table];
1543  tables[1][0] = &s->dc_vlc[dc_c_table];
1544  for (i = 1; i <= 5; i++) {
1545  tables[0][i] = &s->ac_vlc_1[ac_y_table];
1546  tables[1][i] = &s->ac_vlc_1[ac_c_table];
1547  }
1548  for (i = 6; i <= 14; i++) {
1549  tables[0][i] = &s->ac_vlc_2[ac_y_table];
1550  tables[1][i] = &s->ac_vlc_2[ac_c_table];
1551  }
1552  for (i = 15; i <= 27; i++) {
1553  tables[0][i] = &s->ac_vlc_3[ac_y_table];
1554  tables[1][i] = &s->ac_vlc_3[ac_c_table];
1555  }
1556  for (i = 28; i <= 63; i++) {
1557  tables[0][i] = &s->ac_vlc_4[ac_y_table];
1558  tables[1][i] = &s->ac_vlc_4[ac_c_table];
1559  }
1560 
1561  vp4_set_tokens_base(s);
1562 
1563  memset(last_dc, 0, sizeof(last_dc));
1564 
1565  for (plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
1566  memset(eob_tracker, 0, sizeof(eob_tracker));
1567 
1568  /* initialise dc prediction */
1569  for (i = 0; i < s->fragment_width[!!plane]; i++)
1570  vp4_dc_predictor_reset(&s->dc_pred_row[i]);
1571 
1572  for (j = 0; j < 6; j++)
1573  for (i = 0; i < 6; i++)
1574  vp4_dc_predictor_reset(&dc_pred[j][i]);
1575 
1576  for (sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
1577  for (sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
1578  vp4_dc_pred_before(s, dc_pred, sb_x);
1579  for (j = 0; j < 16; j++) {
1580  int hx = hilbert_offset[j][0];
1581  int hy = hilbert_offset[j][1];
1582  int x = 4 * sb_x + hx;
1583  int y = 4 * sb_y + hy;
1584  VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
1585  int fragment, dc_block_type;
1586 
1587  if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
1588  continue;
1589 
1590  fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
1591 
1592  if (s->all_fragments[fragment].coding_method == MODE_COPY)
1593  continue;
1594 
1595  if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
1596  return -1;
1597 
1598  dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
1599 
1600  s->all_fragments[fragment].dc +=
1601  vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
1602 
1603  this_dc_pred->type = dc_block_type,
1604  this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
1605  }
1606  vp4_dc_pred_after(s, dc_pred, sb_x);
1607  }
1608  }
1609  }
1610 
1611  vp4_set_tokens_base(s);
1612 
1613  return 0;
1614 }
1615 #endif
1616 
1617 /*
1618  * This function reverses the DC prediction for each coded fragment in
1619  * the frame. Much of this function is adapted directly from the original
1620  * VP3 source code.
1621  */
1622 #define COMPATIBLE_FRAME(x) \
1623  (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1624 #define DC_COEFF(u) s->all_fragments[u].dc
1625 
1627  int first_fragment,
1628  int fragment_width,
1629  int fragment_height)
1630 {
1631 #define PUL 8
1632 #define PU 4
1633 #define PUR 2
1634 #define PL 1
1635 
1636  int x, y;
1637  int i = first_fragment;
1638 
1639  int predicted_dc;
1640 
1641  /* DC values for the left, up-left, up, and up-right fragments */
1642  int vl, vul, vu, vur;
1643 
1644  /* indexes for the left, up-left, up, and up-right fragments */
1645  int l, ul, u, ur;
1646 
1647  /*
1648  * The 6 fields mean:
1649  * 0: up-left multiplier
1650  * 1: up multiplier
1651  * 2: up-right multiplier
1652  * 3: left multiplier
1653  */
1654  static const int predictor_transform[16][4] = {
1655  { 0, 0, 0, 0 },
1656  { 0, 0, 0, 128 }, // PL
1657  { 0, 0, 128, 0 }, // PUR
1658  { 0, 0, 53, 75 }, // PUR|PL
1659  { 0, 128, 0, 0 }, // PU
1660  { 0, 64, 0, 64 }, // PU |PL
1661  { 0, 128, 0, 0 }, // PU |PUR
1662  { 0, 0, 53, 75 }, // PU |PUR|PL
1663  { 128, 0, 0, 0 }, // PUL
1664  { 0, 0, 0, 128 }, // PUL|PL
1665  { 64, 0, 64, 0 }, // PUL|PUR
1666  { 0, 0, 53, 75 }, // PUL|PUR|PL
1667  { 0, 128, 0, 0 }, // PUL|PU
1668  { -104, 116, 0, 116 }, // PUL|PU |PL
1669  { 24, 80, 24, 0 }, // PUL|PU |PUR
1670  { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1671  };
1672 
1673  /* This table shows which types of blocks can use other blocks for
1674  * prediction. For example, INTRA is the only mode in this table to
1675  * have a frame number of 0. That means INTRA blocks can only predict
1676  * from other INTRA blocks. There are 2 golden frame coding types;
1677  * blocks encoding in these modes can only predict from other blocks
1678  * that were encoded with these 1 of these 2 modes. */
1679  static const unsigned char compatible_frame[9] = {
1680  1, /* MODE_INTER_NO_MV */
1681  0, /* MODE_INTRA */
1682  1, /* MODE_INTER_PLUS_MV */
1683  1, /* MODE_INTER_LAST_MV */
1684  1, /* MODE_INTER_PRIOR_MV */
1685  2, /* MODE_USING_GOLDEN */
1686  2, /* MODE_GOLDEN_MV */
1687  1, /* MODE_INTER_FOUR_MV */
1688  3 /* MODE_COPY */
1689  };
1690  int current_frame_type;
1691 
1692  /* there is a last DC predictor for each of the 3 frame types */
1693  short last_dc[3];
1694 
1695  int transform = 0;
1696 
1697  vul =
1698  vu =
1699  vur =
1700  vl = 0;
1701  last_dc[0] =
1702  last_dc[1] =
1703  last_dc[2] = 0;
1704 
1705  /* for each fragment row... */
1706  for (y = 0; y < fragment_height; y++) {
1707  /* for each fragment in a row... */
1708  for (x = 0; x < fragment_width; x++, i++) {
1709 
1710  /* reverse prediction if this block was coded */
1711  if (s->all_fragments[i].coding_method != MODE_COPY) {
1712  current_frame_type =
1713  compatible_frame[s->all_fragments[i].coding_method];
1714 
1715  transform = 0;
1716  if (x) {
1717  l = i - 1;
1718  vl = DC_COEFF(l);
1719  if (COMPATIBLE_FRAME(l))
1720  transform |= PL;
1721  }
1722  if (y) {
1723  u = i - fragment_width;
1724  vu = DC_COEFF(u);
1725  if (COMPATIBLE_FRAME(u))
1726  transform |= PU;
1727  if (x) {
1728  ul = i - fragment_width - 1;
1729  vul = DC_COEFF(ul);
1730  if (COMPATIBLE_FRAME(ul))
1731  transform |= PUL;
1732  }
1733  if (x + 1 < fragment_width) {
1734  ur = i - fragment_width + 1;
1735  vur = DC_COEFF(ur);
1736  if (COMPATIBLE_FRAME(ur))
1737  transform |= PUR;
1738  }
1739  }
1740 
1741  if (transform == 0) {
1742  /* if there were no fragments to predict from, use last
1743  * DC saved */
1744  predicted_dc = last_dc[current_frame_type];
1745  } else {
1746  /* apply the appropriate predictor transform */
1747  predicted_dc =
1748  (predictor_transform[transform][0] * vul) +
1749  (predictor_transform[transform][1] * vu) +
1750  (predictor_transform[transform][2] * vur) +
1751  (predictor_transform[transform][3] * vl);
1752 
1753  predicted_dc /= 128;
1754 
1755  /* check for outranging on the [ul u l] and
1756  * [ul u ur l] predictors */
1757  if ((transform == 15) || (transform == 13)) {
1758  if (FFABS(predicted_dc - vu) > 128)
1759  predicted_dc = vu;
1760  else if (FFABS(predicted_dc - vl) > 128)
1761  predicted_dc = vl;
1762  else if (FFABS(predicted_dc - vul) > 128)
1763  predicted_dc = vul;
1764  }
1765  }
1766 
1767  /* at long last, apply the predictor */
1768  DC_COEFF(i) += predicted_dc;
1769  /* save the DC */
1770  last_dc[current_frame_type] = DC_COEFF(i);
1771  }
1772  }
1773  }
1774 }
1775 
1777  int ystart, int yend)
1778 {
1779  int x, y;
1780  int *bounding_values = s->bounding_values_array + 127;
1781 
1782  int width = s->fragment_width[!!plane];
1783  int height = s->fragment_height[!!plane];
1784  int fragment = s->fragment_start[plane] + ystart * width;
1785  ptrdiff_t stride = s->current_frame.f->linesize[plane];
1786  uint8_t *plane_data = s->current_frame.f->data[plane];
1787  if (!s->flipped_image)
1788  stride = -stride;
1789  plane_data += s->data_offset[plane] + 8 * ystart * stride;
1790 
1791  for (y = ystart; y < yend; y++) {
1792  for (x = 0; x < width; x++) {
1793  /* This code basically just deblocks on the edges of coded blocks.
1794  * However, it has to be much more complicated because of the
1795  * brain damaged deblock ordering used in VP3/Theora. Order matters
1796  * because some pixels get filtered twice. */
1797  if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1798  /* do not perform left edge filter for left columns frags */
1799  if (x > 0) {
1800  s->vp3dsp.h_loop_filter(
1801  plane_data + 8 * x,
1802  stride, bounding_values);
1803  }
1804 
1805  /* do not perform top edge filter for top row fragments */
1806  if (y > 0) {
1807  s->vp3dsp.v_loop_filter(
1808  plane_data + 8 * x,
1809  stride, bounding_values);
1810  }
1811 
1812  /* do not perform right edge filter for right column
1813  * fragments or if right fragment neighbor is also coded
1814  * in this frame (it will be filtered in next iteration) */
1815  if ((x < width - 1) &&
1816  (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1817  s->vp3dsp.h_loop_filter(
1818  plane_data + 8 * x + 8,
1819  stride, bounding_values);
1820  }
1821 
1822  /* do not perform bottom edge filter for bottom row
1823  * fragments or if bottom fragment neighbor is also coded
1824  * in this frame (it will be filtered in the next row) */
1825  if ((y < height - 1) &&
1826  (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1827  s->vp3dsp.v_loop_filter(
1828  plane_data + 8 * x + 8 * stride,
1829  stride, bounding_values);
1830  }
1831  }
1832 
1833  fragment++;
1834  }
1835  plane_data += 8 * stride;
1836  }
1837 }
1838 
1839 /**
1840  * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1841  * for the next block in coding order
1842  */
1843 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1844  int plane, int inter, int16_t block[64])
1845 {
1846  int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1847  uint8_t *perm = s->idct_scantable;
1848  int i = 0;
1849 
1850  do {
1851  int token = *s->dct_tokens[plane][i];
1852  switch (token & 3) {
1853  case 0: // EOB
1854  if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1855  s->dct_tokens[plane][i]++;
1856  else
1857  *s->dct_tokens[plane][i] = token & ~3;
1858  goto end;
1859  case 1: // zero run
1860  s->dct_tokens[plane][i]++;
1861  i += (token >> 2) & 0x7f;
1862  if (i > 63) {
1863  av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1864  return i;
1865  }
1866  block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1867  i++;
1868  break;
1869  case 2: // coeff
1870  block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1871  s->dct_tokens[plane][i++]++;
1872  break;
1873  default: // shouldn't happen
1874  return i;
1875  }
1876  } while (i < 64);
1877  // return value is expected to be a valid level
1878  i--;
1879 end:
1880  // the actual DC+prediction is in the fragment structure
1881  block[0] = frag->dc * s->qmat[0][inter][plane][0];
1882  return i;
1883 }
1884 
1885 /**
1886  * called when all pixels up to row y are complete
1887  */
1889 {
1890  int h, cy, i;
1892 
1893  if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1894  int y_flipped = s->flipped_image ? s->height - y : y;
1895 
1896  /* At the end of the frame, report INT_MAX instead of the height of
1897  * the frame. This makes the other threads' ff_thread_await_progress()
1898  * calls cheaper, because they don't have to clip their values. */
1900  y_flipped == s->height ? INT_MAX
1901  : y_flipped - 1,
1902  0);
1903  }
1904 
1905  if (!s->avctx->draw_horiz_band)
1906  return;
1907 
1908  h = y - s->last_slice_end;
1909  s->last_slice_end = y;
1910  y -= h;
1911 
1912  if (!s->flipped_image)
1913  y = s->height - y - h;
1914 
1915  cy = y >> s->chroma_y_shift;
1916  offset[0] = s->current_frame.f->linesize[0] * y;
1917  offset[1] = s->current_frame.f->linesize[1] * cy;
1918  offset[2] = s->current_frame.f->linesize[2] * cy;
1919  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1920  offset[i] = 0;
1921 
1922  emms_c();
1923  s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1924 }
1925 
1926 /**
1927  * Wait for the reference frame of the current fragment.
1928  * The progress value is in luma pixel rows.
1929  */
1931  int motion_y, int y)
1932 {
1933  ThreadFrame *ref_frame;
1934  int ref_row;
1935  int border = motion_y & 1;
1936 
1937  if (fragment->coding_method == MODE_USING_GOLDEN ||
1938  fragment->coding_method == MODE_GOLDEN_MV)
1939  ref_frame = &s->golden_frame;
1940  else
1941  ref_frame = &s->last_frame;
1942 
1943  ref_row = y + (motion_y >> 1);
1944  ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1945 
1946  ff_thread_await_progress(ref_frame, ref_row, 0);
1947 }
1948 
1949 #if CONFIG_VP4_DECODER
1950 /**
1951  * @return non-zero if temp (edge_emu_buffer) was populated
1952  */
1953 static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
1954  uint8_t * motion_source, int stride, int src_x, int src_y, uint8_t *temp)
1955 {
1956  int motion_shift = plane ? 4 : 2;
1957  int subpel_mask = plane ? 3 : 1;
1958  int *bounding_values = s->bounding_values_array + 127;
1959 
1960  int i;
1961  int x, y;
1962  int x2, y2;
1963  int x_subpel, y_subpel;
1964  int x_offset, y_offset;
1965 
1966  int block_width = plane ? 8 : 16;
1967  int plane_width = s->width >> (plane && s->chroma_x_shift);
1968  int plane_height = s->height >> (plane && s->chroma_y_shift);
1969 
1970 #define loop_stride 12
1971  uint8_t loop[12 * loop_stride];
1972 
1973  /* using division instead of shift to correctly handle negative values */
1974  x = 8 * bx + motion_x / motion_shift;
1975  y = 8 * by + motion_y / motion_shift;
1976 
1977  x_subpel = motion_x & subpel_mask;
1978  y_subpel = motion_y & subpel_mask;
1979 
1980  if (x_subpel || y_subpel) {
1981  x--;
1982  y--;
1983 
1984  if (x_subpel)
1985  x = FFMIN(x, x + FFSIGN(motion_x));
1986 
1987  if (y_subpel)
1988  y = FFMIN(y, y + FFSIGN(motion_y));
1989 
1990  x2 = x + block_width;
1991  y2 = y + block_width;
1992 
1993  if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
1994  return 0;
1995 
1996  x_offset = (-(x + 2) & 7) + 2;
1997  y_offset = (-(y + 2) & 7) + 2;
1998 
1999  if (x_offset > 8 + x_subpel && y_offset > 8 + y_subpel)
2000  return 0;
2001 
2002  s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2003  loop_stride, stride,
2004  12, 12, src_x - 1, src_y - 1,
2005  plane_width,
2006  plane_height);
2007 
2008  if (x_offset <= 8 + x_subpel)
2009  ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
2010 
2011  if (y_offset <= 8 + y_subpel)
2012  ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
2013 
2014  } else {
2015 
2016  x_offset = -x & 7;
2017  y_offset = -y & 7;
2018 
2019  if (!x_offset && !y_offset)
2020  return 0;
2021 
2022  s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
2023  loop_stride, stride,
2024  12, 12, src_x - 1, src_y - 1,
2025  plane_width,
2026  plane_height);
2027 
2028  if (x_offset)
2029  s->vp3dsp.h_loop_filter(loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
2030 
2031  if (y_offset)
2032  s->vp3dsp.v_loop_filter(loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
2033  }
2034 
2035  for (i = 0; i < 9; i++)
2036  memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
2037 
2038  return 1;
2039 }
2040 #endif
2041 
2042 /*
2043  * Perform the final rendering for a particular slice of data.
2044  * The slice number ranges from 0..(c_superblock_height - 1).
2045  */
2046 static void render_slice(Vp3DecodeContext *s, int slice)
2047 {
2048  int x, y, i, j, fragment;
2049  int16_t *block = s->block;
2050  int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
2051  int motion_halfpel_index;
2052  uint8_t *motion_source;
2053  int plane, first_pixel;
2054 
2055  if (slice >= s->c_superblock_height)
2056  return;
2057 
2058  for (plane = 0; plane < 3; plane++) {
2060  s->data_offset[plane];
2061  uint8_t *last_plane = s->last_frame.f->data[plane] +
2062  s->data_offset[plane];
2063  uint8_t *golden_plane = s->golden_frame.f->data[plane] +
2064  s->data_offset[plane];
2065  ptrdiff_t stride = s->current_frame.f->linesize[plane];
2066  int plane_width = s->width >> (plane && s->chroma_x_shift);
2067  int plane_height = s->height >> (plane && s->chroma_y_shift);
2068  int8_t(*motion_val)[2] = s->motion_val[!!plane];
2069 
2070  int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
2071  int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
2072  int slice_width = plane ? s->c_superblock_width
2073  : s->y_superblock_width;
2074 
2075  int fragment_width = s->fragment_width[!!plane];
2076  int fragment_height = s->fragment_height[!!plane];
2077  int fragment_start = s->fragment_start[plane];
2078 
2079  int do_await = !plane && HAVE_THREADS &&
2081 
2082  if (!s->flipped_image)
2083  stride = -stride;
2084  if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2085  continue;
2086 
2087  /* for each superblock row in the slice (both of them)... */
2088  for (; sb_y < slice_height; sb_y++) {
2089  /* for each superblock in a row... */
2090  for (sb_x = 0; sb_x < slice_width; sb_x++) {
2091  /* for each block in a superblock... */
2092  for (j = 0; j < 16; j++) {
2093  x = 4 * sb_x + hilbert_offset[j][0];
2094  y = 4 * sb_y + hilbert_offset[j][1];
2095  fragment = y * fragment_width + x;
2096 
2097  i = fragment_start + fragment;
2098 
2099  // bounds check
2100  if (x >= fragment_width || y >= fragment_height)
2101  continue;
2102 
2103  first_pixel = 8 * y * stride + 8 * x;
2104 
2105  if (do_await &&
2108  motion_val[fragment][1],
2109  (16 * y) >> s->chroma_y_shift);
2110 
2111  /* transform if this block was coded */
2112  if (s->all_fragments[i].coding_method != MODE_COPY) {
2115  motion_source = golden_plane;
2116  else
2117  motion_source = last_plane;
2118 
2119  motion_source += first_pixel;
2120  motion_halfpel_index = 0;
2121 
2122  /* sort out the motion vector if this fragment is coded
2123  * using a motion vector method */
2124  if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2126  int src_x, src_y;
2127  int standard_mc = 1;
2128  motion_x = motion_val[fragment][0];
2129  motion_y = motion_val[fragment][1];
2130 #if CONFIG_VP4_DECODER
2131  if (plane && s->version >= 2) {
2132  motion_x = (motion_x >> 1) | (motion_x & 1);
2133  motion_y = (motion_y >> 1) | (motion_y & 1);
2134  }
2135 #endif
2136 
2137  src_x = (motion_x >> 1) + 8 * x;
2138  src_y = (motion_y >> 1) + 8 * y;
2139 
2140  motion_halfpel_index = motion_x & 0x01;
2141  motion_source += (motion_x >> 1);
2142 
2143  motion_halfpel_index |= (motion_y & 0x01) << 1;
2144  motion_source += ((motion_y >> 1) * stride);
2145 
2146 #if CONFIG_VP4_DECODER
2147  if (s->version >= 2) {
2149  if (stride < 0)
2150  temp -= 8 * stride;
2151  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)) {
2152  motion_source = temp;
2153  standard_mc = 0;
2154  }
2155  }
2156 #endif
2157 
2158  if (standard_mc && (
2159  src_x < 0 || src_y < 0 ||
2160  src_x + 9 >= plane_width ||
2161  src_y + 9 >= plane_height)) {
2163  if (stride < 0)
2164  temp -= 8 * stride;
2165 
2166  s->vdsp.emulated_edge_mc(temp, motion_source,
2167  stride, stride,
2168  9, 9, src_x, src_y,
2169  plane_width,
2170  plane_height);
2171  motion_source = temp;
2172  }
2173  }
2174 
2175  /* first, take care of copying a block from either the
2176  * previous or the golden frame */
2177  if (s->all_fragments[i].coding_method != MODE_INTRA) {
2178  /* Note, it is possible to implement all MC cases
2179  * with put_no_rnd_pixels_l2 which would look more
2180  * like the VP3 source but this would be slower as
2181  * put_no_rnd_pixels_tab is better optimized */
2182  if (motion_halfpel_index != 3) {
2183  s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2184  output_plane + first_pixel,
2185  motion_source, stride, 8);
2186  } else {
2187  /* d is 0 if motion_x and _y have the same sign,
2188  * else -1 */
2189  int d = (motion_x ^ motion_y) >> 31;
2190  s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
2191  motion_source - d,
2192  motion_source + stride + 1 + d,
2193  stride, 8);
2194  }
2195  }
2196 
2197  /* invert DCT and place (or add) in final output */
2198 
2199  if (s->all_fragments[i].coding_method == MODE_INTRA) {
2200  vp3_dequant(s, s->all_fragments + i,
2201  plane, 0, block);
2202  s->vp3dsp.idct_put(output_plane + first_pixel,
2203  stride,
2204  block);
2205  } else {
2206  if (vp3_dequant(s, s->all_fragments + i,
2207  plane, 1, block)) {
2208  s->vp3dsp.idct_add(output_plane + first_pixel,
2209  stride,
2210  block);
2211  } else {
2212  s->vp3dsp.idct_dc_add(output_plane + first_pixel,
2213  stride, block);
2214  }
2215  }
2216  } else {
2217  /* copy directly from the previous frame */
2218  s->hdsp.put_pixels_tab[1][0](
2219  output_plane + first_pixel,
2220  last_plane + first_pixel,
2221  stride, 8);
2222  }
2223  }
2224  }
2225 
2226  // Filter up to the last row in the superblock row
2227  if (s->version < 2 && !s->skip_loop_filter)
2228  apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
2229  FFMIN(4 * sb_y + 3, fragment_height - 1));
2230  }
2231  }
2232 
2233  /* this looks like a good place for slice dispatch... */
2234  /* algorithm:
2235  * if (slice == s->macroblock_height - 1)
2236  * dispatch (both last slice & 2nd-to-last slice);
2237  * else if (slice > 0)
2238  * dispatch (slice - 1);
2239  */
2240 
2241  vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
2242  s->height - 16));
2243 }
2244 
2245 /// Allocate tables for per-frame data in Vp3DecodeContext
2247 {
2248  Vp3DecodeContext *s = avctx->priv_data;
2249  int y_fragment_count, c_fragment_count;
2250 
2251  free_tables(avctx);
2252 
2253  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2254  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2255 
2256  /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
2259 
2260  s-> kf_coded_fragment_list = av_mallocz_array(s->fragment_count, sizeof(int));
2262  memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
2263 
2265  64 * sizeof(*s->dct_tokens_base));
2266  s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
2267  s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
2268 
2269  /* work out the block mapping tables */
2270  s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
2272 
2273  s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
2274 
2275  if (!s->superblock_coding || !s->all_fragments ||
2279  !s->dc_pred_row ||
2280  !s->motion_val[0] || !s->motion_val[1]) {
2281  vp3_decode_end(avctx);
2282  return -1;
2283  }
2284 
2285  init_block_mapping(s);
2286 
2287  return 0;
2288 }
2289 
2291 {
2293  s->last_frame.f = av_frame_alloc();
2294  s->golden_frame.f = av_frame_alloc();
2295 
2296  if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
2298  av_frame_free(&s->last_frame.f);
2300  return AVERROR(ENOMEM);
2301  }
2302 
2303  return 0;
2304 }
2305 
2307 {
2308  Vp3DecodeContext *s = avctx->priv_data;
2309  int i, inter, plane, ret;
2310  int c_width;
2311  int c_height;
2312  int y_fragment_count, c_fragment_count;
2313 #if CONFIG_VP4_DECODER
2314  int j;
2315 #endif
2316 
2317  ret = init_frames(s);
2318  if (ret < 0)
2319  return ret;
2320 
2321  avctx->internal->allocate_progress = 1;
2322 
2323  if (avctx->codec_tag == MKTAG('V', 'P', '4', '0'))
2324  s->version = 3;
2325  else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
2326  s->version = 0;
2327  else
2328  s->version = 1;
2329 
2330  s->avctx = avctx;
2331  s->width = FFALIGN(avctx->coded_width, 16);
2332  s->height = FFALIGN(avctx->coded_height, 16);
2333  if (avctx->codec_id != AV_CODEC_ID_THEORA)
2334  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2337  ff_videodsp_init(&s->vdsp, 8);
2338  ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
2339 
2340  for (i = 0; i < 64; i++) {
2341 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
2342  s->idct_permutation[i] = TRANSPOSE(i);
2344 #undef TRANSPOSE
2345  }
2346 
2347  /* initialize to an impossible value which will force a recalculation
2348  * in the first frame decode */
2349  for (i = 0; i < 3; i++)
2350  s->qps[i] = -1;
2351 
2353  if (ret)
2354  return ret;
2355 
2356  s->y_superblock_width = (s->width + 31) / 32;
2357  s->y_superblock_height = (s->height + 31) / 32;
2359 
2360  /* work out the dimensions for the C planes */
2361  c_width = s->width >> s->chroma_x_shift;
2362  c_height = s->height >> s->chroma_y_shift;
2363  s->c_superblock_width = (c_width + 31) / 32;
2364  s->c_superblock_height = (c_height + 31) / 32;
2366 
2370 
2371  s->macroblock_width = (s->width + 15) / 16;
2372  s->macroblock_height = (s->height + 15) / 16;
2374  s->c_macroblock_width = (c_width + 15) / 16;
2375  s->c_macroblock_height = (c_height + 15) / 16;
2378 
2379  s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
2380  s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
2381  s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
2382  s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
2383 
2384  /* fragment count covers all 8x8 blocks for all 3 planes */
2385  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2386  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2387  s->fragment_count = y_fragment_count + 2 * c_fragment_count;
2388  s->fragment_start[1] = y_fragment_count;
2389  s->fragment_start[2] = y_fragment_count + c_fragment_count;
2390 
2391  if (!s->theora_tables) {
2392  for (i = 0; i < 64; i++) {
2400  }
2401 
2402  for (inter = 0; inter < 2; inter++) {
2403  for (plane = 0; plane < 3; plane++) {
2404  s->qr_count[inter][plane] = 1;
2405  s->qr_size[inter][plane][0] = 63;
2406  s->qr_base[inter][plane][0] =
2407  s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
2408  }
2409  }
2410 
2411  /* init VLC tables */
2412  if (s->version < 2) {
2413  for (i = 0; i < 16; i++) {
2414  /* DC histograms */
2415  init_vlc(&s->dc_vlc[i], 11, 32,
2416  &dc_bias[i][0][1], 4, 2,
2417  &dc_bias[i][0][0], 4, 2, 0);
2418 
2419  /* group 1 AC histograms */
2420  init_vlc(&s->ac_vlc_1[i], 11, 32,
2421  &ac_bias_0[i][0][1], 4, 2,
2422  &ac_bias_0[i][0][0], 4, 2, 0);
2423 
2424  /* group 2 AC histograms */
2425  init_vlc(&s->ac_vlc_2[i], 11, 32,
2426  &ac_bias_1[i][0][1], 4, 2,
2427  &ac_bias_1[i][0][0], 4, 2, 0);
2428 
2429  /* group 3 AC histograms */
2430  init_vlc(&s->ac_vlc_3[i], 11, 32,
2431  &ac_bias_2[i][0][1], 4, 2,
2432  &ac_bias_2[i][0][0], 4, 2, 0);
2433 
2434  /* group 4 AC histograms */
2435  init_vlc(&s->ac_vlc_4[i], 11, 32,
2436  &ac_bias_3[i][0][1], 4, 2,
2437  &ac_bias_3[i][0][0], 4, 2, 0);
2438  }
2439 #if CONFIG_VP4_DECODER
2440  } else { /* version >= 2 */
2441  for (i = 0; i < 16; i++) {
2442  /* DC histograms */
2443  init_vlc(&s->dc_vlc[i], 11, 32,
2444  &vp4_dc_bias[i][0][1], 4, 2,
2445  &vp4_dc_bias[i][0][0], 4, 2, 0);
2446 
2447  /* group 1 AC histograms */
2448  init_vlc(&s->ac_vlc_1[i], 11, 32,
2449  &vp4_ac_bias_0[i][0][1], 4, 2,
2450  &vp4_ac_bias_0[i][0][0], 4, 2, 0);
2451 
2452  /* group 2 AC histograms */
2453  init_vlc(&s->ac_vlc_2[i], 11, 32,
2454  &vp4_ac_bias_1[i][0][1], 4, 2,
2455  &vp4_ac_bias_1[i][0][0], 4, 2, 0);
2456 
2457  /* group 3 AC histograms */
2458  init_vlc(&s->ac_vlc_3[i], 11, 32,
2459  &vp4_ac_bias_2[i][0][1], 4, 2,
2460  &vp4_ac_bias_2[i][0][0], 4, 2, 0);
2461 
2462  /* group 4 AC histograms */
2463  init_vlc(&s->ac_vlc_4[i], 11, 32,
2464  &vp4_ac_bias_3[i][0][1], 4, 2,
2465  &vp4_ac_bias_3[i][0][0], 4, 2, 0);
2466  }
2467 #endif
2468  }
2469  } else {
2470  for (i = 0; i < 16; i++) {
2471  /* DC histograms */
2472  if (init_vlc(&s->dc_vlc[i], 11, 32,
2473  &s->huffman_table[i][0][1], 8, 4,
2474  &s->huffman_table[i][0][0], 8, 4, 0) < 0)
2475  goto vlc_fail;
2476 
2477  /* group 1 AC histograms */
2478  if (init_vlc(&s->ac_vlc_1[i], 11, 32,
2479  &s->huffman_table[i + 16][0][1], 8, 4,
2480  &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
2481  goto vlc_fail;
2482 
2483  /* group 2 AC histograms */
2484  if (init_vlc(&s->ac_vlc_2[i], 11, 32,
2485  &s->huffman_table[i + 16 * 2][0][1], 8, 4,
2486  &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
2487  goto vlc_fail;
2488 
2489  /* group 3 AC histograms */
2490  if (init_vlc(&s->ac_vlc_3[i], 11, 32,
2491  &s->huffman_table[i + 16 * 3][0][1], 8, 4,
2492  &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
2493  goto vlc_fail;
2494 
2495  /* group 4 AC histograms */
2496  if (init_vlc(&s->ac_vlc_4[i], 11, 32,
2497  &s->huffman_table[i + 16 * 4][0][1], 8, 4,
2498  &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
2499  goto vlc_fail;
2500  }
2501  }
2502 
2504  &superblock_run_length_vlc_table[0][1], 4, 2,
2505  &superblock_run_length_vlc_table[0][0], 4, 2, 0);
2506 
2507  init_vlc(&s->fragment_run_length_vlc, 5, 30,
2508  &fragment_run_length_vlc_table[0][1], 4, 2,
2509  &fragment_run_length_vlc_table[0][0], 4, 2, 0);
2510 
2511  init_vlc(&s->mode_code_vlc, 3, 8,
2512  &mode_code_vlc_table[0][1], 2, 1,
2513  &mode_code_vlc_table[0][0], 2, 1, 0);
2514 
2515  init_vlc(&s->motion_vector_vlc, 6, 63,
2516  &motion_vector_vlc_table[0][1], 2, 1,
2517  &motion_vector_vlc_table[0][0], 2, 1, 0);
2518 
2519 #if CONFIG_VP4_DECODER
2520  for (j = 0; j < 2; j++)
2521  for (i = 0; i < 7; i++)
2522  init_vlc(&s->vp4_mv_vlc[j][i], 6, 63,
2523  &vp4_mv_vlc[j][i][0][1], 4, 2,
2524  &vp4_mv_vlc[j][i][0][0], 4, 2, 0);
2525 
2526  /* version >= 2 */
2527  for (i = 0; i < 2; i++)
2528  init_vlc(&s->block_pattern_vlc[i], 3, 14,
2529  &vp4_block_pattern_vlc[i][0][1], 2, 1,
2530  &vp4_block_pattern_vlc[i][0][0], 2, 1, 0);
2531 #endif
2532 
2533  return allocate_tables(avctx);
2534 
2535 vlc_fail:
2536  av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
2537  return -1;
2538 }
2539 
2540 /// Release and shuffle frames after decode finishes
2541 static int update_frames(AVCodecContext *avctx)
2542 {
2543  Vp3DecodeContext *s = avctx->priv_data;
2544  int ret = 0;
2545 
2546  /* shuffle frames (last = current) */
2549  if (ret < 0)
2550  goto fail;
2551 
2552  if (s->keyframe) {
2555  }
2556 
2557 fail:
2559  return ret;
2560 }
2561 
2562 #if HAVE_THREADS
2563 static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
2564 {
2566  if (src->f->data[0])
2567  return ff_thread_ref_frame(dst, src);
2568  return 0;
2569 }
2570 
2571 static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
2572 {
2573  int ret;
2574  if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
2575  (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
2576  (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
2577  return ret;
2578  return 0;
2579 }
2580 
2581 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2582 {
2583  Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
2584  int qps_changed = 0, i, err;
2585 
2586 #define copy_fields(to, from, start_field, end_field) \
2587  memcpy(&to->start_field, &from->start_field, \
2588  (char *) &to->end_field - (char *) &to->start_field)
2589 
2590  if (!s1->current_frame.f->data[0] ||
2591  s->width != s1->width || s->height != s1->height) {
2592  if (s != s1)
2593  ref_frames(s, s1);
2594  return -1;
2595  }
2596 
2597  if (s != s1) {
2598  if (!s->current_frame.f)
2599  return AVERROR(ENOMEM);
2600  // init tables if the first frame hasn't been decoded
2601  if (!s->current_frame.f->data[0]) {
2602  int y_fragment_count, c_fragment_count;
2603  s->avctx = dst;
2604  err = allocate_tables(dst);
2605  if (err)
2606  return err;
2607  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
2608  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
2609  memcpy(s->motion_val[0], s1->motion_val[0],
2610  y_fragment_count * sizeof(*s->motion_val[0]));
2611  memcpy(s->motion_val[1], s1->motion_val[1],
2612  c_fragment_count * sizeof(*s->motion_val[1]));
2613  }
2614 
2615  // copy previous frame data
2616  if ((err = ref_frames(s, s1)) < 0)
2617  return err;
2618 
2619  s->keyframe = s1->keyframe;
2620 
2621  // copy qscale data if necessary
2622  for (i = 0; i < 3; i++) {
2623  if (s->qps[i] != s1->qps[1]) {
2624  qps_changed = 1;
2625  memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2626  }
2627  }
2628 
2629  if (s->qps[0] != s1->qps[0])
2630  memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2631  sizeof(s->bounding_values_array));
2632 
2633  if (qps_changed)
2634  copy_fields(s, s1, qps, superblock_count);
2635 #undef copy_fields
2636  }
2637 
2638  return update_frames(dst);
2639 }
2640 #endif
2641 
2643  void *data, int *got_frame,
2644  AVPacket *avpkt)
2645 {
2646  AVFrame *frame = data;
2647  const uint8_t *buf = avpkt->data;
2648  int buf_size = avpkt->size;
2649  Vp3DecodeContext *s = avctx->priv_data;
2650  GetBitContext gb;
2651  int i, ret;
2652 
2653  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2654  return ret;
2655 
2656 #if CONFIG_THEORA_DECODER
2657  if (s->theora && get_bits1(&gb)) {
2658  int type = get_bits(&gb, 7);
2659  skip_bits_long(&gb, 6*8); /* "theora" */
2660 
2662  av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2663  return AVERROR_PATCHWELCOME;
2664  }
2665  if (type == 0) {
2666  vp3_decode_end(avctx);
2667  ret = theora_decode_header(avctx, &gb);
2668 
2669  if (ret >= 0)
2670  ret = vp3_decode_init(avctx);
2671  if (ret < 0) {
2672  vp3_decode_end(avctx);
2673  return ret;
2674  }
2675  return buf_size;
2676  } else if (type == 2) {
2677  vp3_decode_end(avctx);
2678  ret = theora_decode_tables(avctx, &gb);
2679  if (ret >= 0)
2680  ret = vp3_decode_init(avctx);
2681  if (ret < 0) {
2682  vp3_decode_end(avctx);
2683  return ret;
2684  }
2685  return buf_size;
2686  }
2687 
2688  av_log(avctx, AV_LOG_ERROR,
2689  "Header packet passed to frame decoder, skipping\n");
2690  return -1;
2691  }
2692 #endif
2693 
2694  s->keyframe = !get_bits1(&gb);
2695  if (!s->all_fragments) {
2696  av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2697  return -1;
2698  }
2699  if (!s->theora)
2700  skip_bits(&gb, 1);
2701  for (i = 0; i < 3; i++)
2702  s->last_qps[i] = s->qps[i];
2703 
2704  s->nqps = 0;
2705  do {
2706  s->qps[s->nqps++] = get_bits(&gb, 6);
2707  } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2708  for (i = s->nqps; i < 3; i++)
2709  s->qps[i] = -1;
2710 
2711  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2712  av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2713  s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2714 
2715  s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2716  avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2717  : AVDISCARD_NONKEY);
2718 
2719  if (s->qps[0] != s->last_qps[0])
2720  init_loop_filter(s);
2721 
2722  for (i = 0; i < s->nqps; i++)
2723  // reinit all dequantizers if the first one changed, because
2724  // the DC of the first quantizer must be used for all matrices
2725  if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2726  init_dequantizer(s, i);
2727 
2728  if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2729  return buf_size;
2730 
2733  s->current_frame.f->key_frame = s->keyframe;
2735  goto error;
2736 
2737  if (!s->edge_emu_buffer)
2739 
2740  if (s->keyframe) {
2741  if (!s->theora) {
2742  skip_bits(&gb, 4); /* width code */
2743  skip_bits(&gb, 4); /* height code */
2744  if (s->version) {
2745  s->version = get_bits(&gb, 5);
2746  if (avctx->frame_number == 0)
2748  "VP version: %d\n", s->version);
2749  }
2750  }
2751  if (s->version || s->theora) {
2752  if (get_bits1(&gb))
2754  "Warning, unsupported keyframe coding type?!\n");
2755  skip_bits(&gb, 2); /* reserved? */
2756 
2757 #if CONFIG_VP4_DECODER
2758  if (s->version >= 2) {
2759  int mb_height, mb_width;
2760  int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
2761 
2762  mb_height = get_bits(&gb, 8);
2763  mb_width = get_bits(&gb, 8);
2764  if (mb_height != s->macroblock_height ||
2765  mb_width != s->macroblock_width)
2766  avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
2767 
2768  mb_width_mul = get_bits(&gb, 5);
2769  mb_width_div = get_bits(&gb, 3);
2770  mb_height_mul = get_bits(&gb, 5);
2771  mb_height_div = get_bits(&gb, 3);
2772  if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
2773  avpriv_request_sample(s->avctx, "unexpected macroblock dimension multipler/divider");
2774 
2775  if (get_bits(&gb, 2))
2776  avpriv_request_sample(s->avctx, "unknown bits");
2777  }
2778 #endif
2779  }
2780  } else {
2781  if (!s->golden_frame.f->data[0]) {
2783  "vp3: first frame not a keyframe\n");
2784 
2786  if (ff_thread_get_buffer(avctx, &s->golden_frame,
2788  goto error;
2790  if ((ret = ff_thread_ref_frame(&s->last_frame,
2791  &s->golden_frame)) < 0)
2792  goto error;
2793  ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2794  }
2795  }
2796 
2797  memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2798  ff_thread_finish_setup(avctx);
2799 
2800  if (s->version < 2) {
2801  if (unpack_superblocks(s, &gb)) {
2802  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2803  goto error;
2804  }
2805 #if CONFIG_VP4_DECODER
2806  } else {
2807  if (vp4_unpack_macroblocks(s, &gb)) {
2808  av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
2809  goto error;
2810  }
2811 #endif
2812  }
2813  if (unpack_modes(s, &gb)) {
2814  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2815  goto error;
2816  }
2817  if (unpack_vectors(s, &gb)) {
2818  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2819  goto error;
2820  }
2821  if (unpack_block_qpis(s, &gb)) {
2822  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2823  goto error;
2824  }
2825 
2826  if (s->version < 2) {
2827  if (unpack_dct_coeffs(s, &gb)) {
2828  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2829  goto error;
2830  }
2831 #if CONFIG_VP4_DECODER
2832  } else {
2833  if (vp4_unpack_dct_coeffs(s, &gb)) {
2834  av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
2835  goto error;
2836  }
2837 #endif
2838  }
2839 
2840  for (i = 0; i < 3; i++) {
2841  int height = s->height >> (i && s->chroma_y_shift);
2842  if (s->flipped_image)
2843  s->data_offset[i] = 0;
2844  else
2845  s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2846  }
2847 
2848  s->last_slice_end = 0;
2849  for (i = 0; i < s->c_superblock_height; i++)
2850  render_slice(s, i);
2851 
2852  // filter the last row
2853  if (s->version < 2)
2854  for (i = 0; i < 3; i++) {
2855  int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2856  apply_loop_filter(s, i, row, row + 1);
2857  }
2858  vp3_draw_horiz_band(s, s->height);
2859 
2860  /* output frame, offset as needed */
2861  if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2862  return ret;
2863 
2864  frame->crop_left = s->offset_x;
2865  frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
2866  frame->crop_top = s->offset_y;
2867  frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2868 
2869  *got_frame = 1;
2870 
2871  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2872  ret = update_frames(avctx);
2873  if (ret < 0)
2874  return ret;
2875  }
2876 
2877  return buf_size;
2878 
2879 error:
2880  ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2881 
2882  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2884 
2885  return -1;
2886 }
2887 
2889 {
2890  Vp3DecodeContext *s = avctx->priv_data;
2891 
2892  if (get_bits1(gb)) {
2893  int token;
2894  if (s->entries >= 32) { /* overflow */
2895  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2896  return -1;
2897  }
2898  token = get_bits(gb, 5);
2899  ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2900  s->hti, s->hbits, token, s->entries, s->huff_code_size);
2901  s->huffman_table[s->hti][token][0] = s->hbits;
2902  s->huffman_table[s->hti][token][1] = s->huff_code_size;
2903  s->entries++;
2904  } else {
2905  if (s->huff_code_size >= 32) { /* overflow */
2906  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2907  return -1;
2908  }
2909  s->huff_code_size++;
2910  s->hbits <<= 1;
2911  if (read_huffman_tree(avctx, gb))
2912  return -1;
2913  s->hbits |= 1;
2914  if (read_huffman_tree(avctx, gb))
2915  return -1;
2916  s->hbits >>= 1;
2917  s->huff_code_size--;
2918  }
2919  return 0;
2920 }
2921 
2922 #if HAVE_THREADS
2923 static int vp3_init_thread_copy(AVCodecContext *avctx)
2924 {
2925  Vp3DecodeContext *s = avctx->priv_data;
2926 
2927  s->superblock_coding = NULL;
2928  s->all_fragments = NULL;
2929  s->coded_fragment_list[0] = NULL;
2930  s-> kf_coded_fragment_list= NULL;
2932  s->dct_tokens_base = NULL;
2934  s->macroblock_coding = NULL;
2935  s->motion_val[0] = NULL;
2936  s->motion_val[1] = NULL;
2937  s->edge_emu_buffer = NULL;
2938  s->dc_pred_row = NULL;
2939 
2940  return init_frames(s);
2941 }
2942 #endif
2943 
2944 #if CONFIG_THEORA_DECODER
2945 static const enum AVPixelFormat theora_pix_fmts[4] = {
2947 };
2948 
2949 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2950 {
2951  Vp3DecodeContext *s = avctx->priv_data;
2952  int visible_width, visible_height, colorspace;
2953  uint8_t offset_x = 0, offset_y = 0;
2954  int ret;
2955  AVRational fps, aspect;
2956 
2957  s->theora_header = 0;
2958  s->theora = get_bits_long(gb, 24);
2959  av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2960 
2961  /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2962  * but previous versions have the image flipped relative to vp3 */
2963  if (s->theora < 0x030200) {
2964  s->flipped_image = 1;
2965  av_log(avctx, AV_LOG_DEBUG,
2966  "Old (<alpha3) Theora bitstream, flipped image\n");
2967  }
2968 
2969  visible_width =
2970  s->width = get_bits(gb, 16) << 4;
2971  visible_height =
2972  s->height = get_bits(gb, 16) << 4;
2973 
2974  if (s->theora >= 0x030200) {
2975  visible_width = get_bits_long(gb, 24);
2976  visible_height = get_bits_long(gb, 24);
2977 
2978  offset_x = get_bits(gb, 8); /* offset x */
2979  offset_y = get_bits(gb, 8); /* offset y, from bottom */
2980  }
2981 
2982  /* sanity check */
2983  if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2984  visible_width + offset_x > s->width ||
2985  visible_height + offset_y > s->height) {
2986  av_log(avctx, AV_LOG_ERROR,
2987  "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2988  visible_width, visible_height, offset_x, offset_y,
2989  s->width, s->height);
2990  return AVERROR_INVALIDDATA;
2991  }
2992 
2993  fps.num = get_bits_long(gb, 32);
2994  fps.den = get_bits_long(gb, 32);
2995  if (fps.num && fps.den) {
2996  if (fps.num < 0 || fps.den < 0) {
2997  av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2998  return AVERROR_INVALIDDATA;
2999  }
3000  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
3001  fps.den, fps.num, 1 << 30);
3002  }
3003 
3004  aspect.num = get_bits_long(gb, 24);
3005  aspect.den = get_bits_long(gb, 24);
3006  if (aspect.num && aspect.den) {
3008  &avctx->sample_aspect_ratio.den,
3009  aspect.num, aspect.den, 1 << 30);
3010  ff_set_sar(avctx, avctx->sample_aspect_ratio);
3011  }
3012 
3013  if (s->theora < 0x030200)
3014  skip_bits(gb, 5); /* keyframe frequency force */
3015  colorspace = get_bits(gb, 8);
3016  skip_bits(gb, 24); /* bitrate */
3017 
3018  skip_bits(gb, 6); /* quality hint */
3019 
3020  if (s->theora >= 0x030200) {
3021  skip_bits(gb, 5); /* keyframe frequency force */
3022  avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
3023  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
3024  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
3025  return AVERROR_INVALIDDATA;
3026  }
3027  skip_bits(gb, 3); /* reserved */
3028  } else
3029  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3030 
3031  ret = ff_set_dimensions(avctx, s->width, s->height);
3032  if (ret < 0)
3033  return ret;
3034  if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
3035  avctx->width = visible_width;
3036  avctx->height = visible_height;
3037  // translate offsets from theora axis ([0,0] lower left)
3038  // to normal axis ([0,0] upper left)
3039  s->offset_x = offset_x;
3040  s->offset_y = s->height - visible_height - offset_y;
3041  }
3042 
3043  if (colorspace == 1)
3045  else if (colorspace == 2)
3047 
3048  if (colorspace == 1 || colorspace == 2) {
3049  avctx->colorspace = AVCOL_SPC_BT470BG;
3050  avctx->color_trc = AVCOL_TRC_BT709;
3051  }
3052 
3053  s->theora_header = 1;
3054  return 0;
3055 }
3056 
3057 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
3058 {
3059  Vp3DecodeContext *s = avctx->priv_data;
3060  int i, n, matrices, inter, plane;
3061 
3062  if (!s->theora_header)
3063  return AVERROR_INVALIDDATA;
3064 
3065  if (s->theora >= 0x030200) {
3066  n = get_bits(gb, 3);
3067  /* loop filter limit values table */
3068  if (n)
3069  for (i = 0; i < 64; i++)
3070  s->filter_limit_values[i] = get_bits(gb, n);
3071  }
3072 
3073  if (s->theora >= 0x030200)
3074  n = get_bits(gb, 4) + 1;
3075  else
3076  n = 16;
3077  /* quality threshold table */
3078  for (i = 0; i < 64; i++)
3079  s->coded_ac_scale_factor[i] = get_bits(gb, n);
3080 
3081  if (s->theora >= 0x030200)
3082  n = get_bits(gb, 4) + 1;
3083  else
3084  n = 16;
3085  /* dc scale factor table */
3086  for (i = 0; i < 64; i++)
3087  s->coded_dc_scale_factor[0][i] =
3088  s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
3089 
3090  if (s->theora >= 0x030200)
3091  matrices = get_bits(gb, 9) + 1;
3092  else
3093  matrices = 3;
3094 
3095  if (matrices > 384) {
3096  av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
3097  return -1;
3098  }
3099 
3100  for (n = 0; n < matrices; n++)
3101  for (i = 0; i < 64; i++)
3102  s->base_matrix[n][i] = get_bits(gb, 8);
3103 
3104  for (inter = 0; inter <= 1; inter++) {
3105  for (plane = 0; plane <= 2; plane++) {
3106  int newqr = 1;
3107  if (inter || plane > 0)
3108  newqr = get_bits1(gb);
3109  if (!newqr) {
3110  int qtj, plj;
3111  if (inter && get_bits1(gb)) {
3112  qtj = 0;
3113  plj = plane;
3114  } else {
3115  qtj = (3 * inter + plane - 1) / 3;
3116  plj = (plane + 2) % 3;
3117  }
3118  s->qr_count[inter][plane] = s->qr_count[qtj][plj];
3119  memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
3120  sizeof(s->qr_size[0][0]));
3121  memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
3122  sizeof(s->qr_base[0][0]));
3123  } else {
3124  int qri = 0;
3125  int qi = 0;
3126 
3127  for (;;) {
3128  i = get_bits(gb, av_log2(matrices - 1) + 1);
3129  if (i >= matrices) {
3130  av_log(avctx, AV_LOG_ERROR,
3131  "invalid base matrix index\n");
3132  return -1;
3133  }
3134  s->qr_base[inter][plane][qri] = i;
3135  if (qi >= 63)
3136  break;
3137  i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
3138  s->qr_size[inter][plane][qri++] = i;
3139  qi += i;
3140  }
3141 
3142  if (qi > 63) {
3143  av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
3144  return -1;
3145  }
3146  s->qr_count[inter][plane] = qri;
3147  }
3148  }
3149  }
3150 
3151  /* Huffman tables */
3152  for (s->hti = 0; s->hti < 80; s->hti++) {
3153  s->entries = 0;
3154  s->huff_code_size = 1;
3155  if (!get_bits1(gb)) {
3156  s->hbits = 0;
3157  if (read_huffman_tree(avctx, gb))
3158  return -1;
3159  s->hbits = 1;
3160  if (read_huffman_tree(avctx, gb))
3161  return -1;
3162  }
3163  }
3164 
3165  s->theora_tables = 1;
3166 
3167  return 0;
3168 }
3169 
3170 static av_cold int theora_decode_init(AVCodecContext *avctx)
3171 {
3172  Vp3DecodeContext *s = avctx->priv_data;
3173  GetBitContext gb;
3174  int ptype;
3175  const uint8_t *header_start[3];
3176  int header_len[3];
3177  int i;
3178  int ret;
3179 
3180  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3181 
3182  s->theora = 1;
3183 
3184  if (!avctx->extradata_size) {
3185  av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
3186  return -1;
3187  }
3188 
3190  42, header_start, header_len) < 0) {
3191  av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
3192  return -1;
3193  }
3194 
3195  for (i = 0; i < 3; i++) {
3196  if (header_len[i] <= 0)
3197  continue;
3198  ret = init_get_bits8(&gb, header_start[i], header_len[i]);
3199  if (ret < 0)
3200  return ret;
3201 
3202  ptype = get_bits(&gb, 8);
3203 
3204  if (!(ptype & 0x80)) {
3205  av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
3206 // return -1;
3207  }
3208 
3209  // FIXME: Check for this as well.
3210  skip_bits_long(&gb, 6 * 8); /* "theora" */
3211 
3212  switch (ptype) {
3213  case 0x80:
3214  if (theora_decode_header(avctx, &gb) < 0)
3215  return -1;
3216  break;
3217  case 0x81:
3218 // FIXME: is this needed? it breaks sometimes
3219 // theora_decode_comments(avctx, gb);
3220  break;
3221  case 0x82:
3222  if (theora_decode_tables(avctx, &gb))
3223  return -1;
3224  break;
3225  default:
3226  av_log(avctx, AV_LOG_ERROR,
3227  "Unknown Theora config packet: %d\n", ptype & ~0x80);
3228  break;
3229  }
3230  if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
3231  av_log(avctx, AV_LOG_WARNING,
3232  "%d bits left in packet %X\n",
3233  8 * header_len[i] - get_bits_count(&gb), ptype);
3234  if (s->theora < 0x030200)
3235  break;
3236  }
3237 
3238  return vp3_decode_init(avctx);
3239 }
3240 
3242  .name = "theora",
3243  .long_name = NULL_IF_CONFIG_SMALL("Theora"),
3244  .type = AVMEDIA_TYPE_VIDEO,
3245  .id = AV_CODEC_ID_THEORA,
3246  .priv_data_size = sizeof(Vp3DecodeContext),
3247  .init = theora_decode_init,
3248  .close = vp3_decode_end,
3253  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
3254  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3255  .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING,
3256 };
3257 #endif
3258 
3260  .name = "vp3",
3261  .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
3262  .type = AVMEDIA_TYPE_VIDEO,
3263  .id = AV_CODEC_ID_VP3,
3264  .priv_data_size = sizeof(Vp3DecodeContext),
3265  .init = vp3_decode_init,
3266  .close = vp3_decode_end,
3271  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
3272  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3273 };
3274 
3275 #if CONFIG_VP4_DECODER
3277  .name = "vp4",
3278  .long_name = NULL_IF_CONFIG_SMALL("On2 VP4"),
3279  .type = AVMEDIA_TYPE_VIDEO,
3280  .id = AV_CODEC_ID_VP4,
3281  .priv_data_size = sizeof(Vp3DecodeContext),
3282  .init = vp3_decode_init,
3283  .close = vp3_decode_end,
3288  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
3289  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
3290 };
3291 #endif
int plane
Definition: avisynth_c.h:384
#define BLOCK_Y
Definition: vp3.c:644
AVCodec ff_vp4_decoder
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
int last_slice_end
Definition: vp3.c:175
#define NULL
Definition: coverity.c:32
uint8_t idct_scantable[64]
Definition: vp3.c:169
AVRational framerate
Definition: avcodec.h:3101
discard all frames except keyframes
Definition: avcodec.h:810
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_NUM_DATA_POINTERS
Definition: frame.h:269
int16_t qmat[3][2][3][64]
qmat[qpi][is_inter][plane]
Definition: vp3.c:275
static int init_block_mapping(Vp3DecodeContext *s)
This function sets up all of the various blocks mappings: superblocks <-> fragments, macroblocks <-> fragments, superblocks <-> macroblocks.
Definition: vp3.c:382
#define SB_NOT_CODED
Definition: vp3.c:60
#define copy_fields(s, e)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
#define TOKEN_EOB(eob_run)
Definition: vp3.c:241
static void render_slice(Vp3DecodeContext *s, int slice)
Definition: vp3.c:2046
static const uint16_t vp4_dc_bias[16][32][2]
Definition: vp4data.h:371
#define PUR
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int y_superblock_count
Definition: vp3.c:185
static void flush(AVCodecContext *avctx)
int bounding_values_array[256+2]
Definition: vp3.c:297
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1753
void(* put_no_rnd_pixels_l2)(uint8_t *dst, const uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h)
Copy 8xH pixels from source to destination buffer using a bilinear filter with no rounding (i...
Definition: vp3dsp.h:36
static const int8_t vp31_intra_c_dequant[64]
Definition: vp3data.h:42
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
Definition: vp3.c:1146
uint16_t qr_base[2][3][64]
Definition: vp3.c:220
AVFrame * f
Definition: thread.h:35
else temp
Definition: vf_mcdeint.c:256
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:104
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
VLC mode_code_vlc
Definition: vp3.c:269
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
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 add an init_thread_copy() which re-allocates them for other threads.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
int y_superblock_width
Definition: vp3.c:183
static const uint16_t fragment_run_length_vlc_table[30][2]
Definition: vp3data.h:119
HpelDSPContext hdsp
Definition: vp3.c:170
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:502
#define avpriv_request_sample(...)
#define MODE_INTER_PLUS_MV
Definition: vp3.c:71
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1478
static const int8_t vp31_intra_y_dequant[64]
Definition: vp3data.h:29
static av_cold int init_frames(Vp3DecodeContext *s)
Definition: vp3.c:2290
int u_superblock_start
Definition: vp3.c:189
#define BLOCK_X
Definition: vp3.c:643
int av_log2(unsigned v)
Definition: intmath.c:26
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:1944
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:789
static const uint8_t zero_run_base[32]
Definition: vp3data.h:207
void(* v_loop_filter)(uint8_t *src, ptrdiff_t stride, int *bounding_values)
Definition: vp3dsp.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
uint8_t coding_method
Definition: vp3.c:56
static av_cold int vp3_decode_init(AVCodecContext *avctx)
Definition: vp3.c:2306
GLint GLenum type
Definition: opengl_enc.c:104
static const uint8_t coeff_get_bits[32]
Definition: vp3data.h:222
int num_kf_coded_fragment[3]
Definition: vp3.c:258
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:468
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static const uint16_t vp4_ac_scale_factor[64]
Definition: vp4data.h:64
static void reverse_dc_prediction(Vp3DecodeContext *s, int first_fragment, int fragment_width, int fragment_height)
Definition: vp3.c:1626
discard all
Definition: avcodec.h:811
VLC ac_vlc_4[16]
Definition: vp3.c:264
VLC motion_vector_vlc
Definition: vp3.c:270
static av_cold int vp3_decode_end(AVCodecContext *avctx)
Definition: vp3.c:334
int huff_code_size
Definition: vp3.c:293
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
#define src
Definition: vp8dsp.c:254
static const uint16_t vp4_ac_bias_0[16][32][2]
Definition: vp4data.h:534
int * superblock_fragments
Definition: vp3.c:281
VLC superblock_run_length_vlc
Definition: vp3.c:266
AVCodec.
Definition: avcodec.h:3477
#define MAXIMUM_LONG_BIT_RUN
Definition: vp3.c:67
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static const uint16_t ac_bias_3[16][32][2]
Definition: vp3data.h:2633
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, &#39;draw_horiz_band&#39; is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1800
static const uint16_t dc_bias[16][32][2]
Definition: vp3data.h:445
Vp3Fragment * all_fragments
Definition: vp3.c:205
static void init_loop_filter(Vp3DecodeContext *s)
Definition: vp3.c:459
uint8_t base
Definition: vp3data.h:202
#define COMPATIBLE_FRAME(x)
Definition: vp3.c:1622
int dc
Definition: vp3.c:152
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
uint8_t offset_y
Definition: vp3.c:209
int y_superblock_height
Definition: vp3.c:184
#define TRANSPOSE(x)
The exact code depends on how similar the blocks are and how related they are to the block
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:902
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
VLC ac_vlc_1[16]
Definition: vp3.c:261
#define TOKEN_ZERO_RUN(coeff, zero_run)
Definition: vp3.c:242
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2647
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
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:1843
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int FUNC() huffman_table(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawHuffmanTable *current)
unsigned int hbits
Definition: vp3.c:291
Multithreading support functions.
int macroblock_width
Definition: vp3.c:194
uint8_t idct_permutation[64]
Definition: vp3.c:168
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:443
static void init_dequantizer(Vp3DecodeContext *s, int qpi)
Definition: vp3.c:416
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
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:1027
uint8_t qpi
Definition: vp3.c:57
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static void vp3_decode_flush(AVCodecContext *avctx)
Definition: vp3.c:322
#define DC_COEFF(u)
Definition: vp3.c:1624
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
#define height
uint8_t * data
Definition: avcodec.h:1477
uint8_t filter_limit_values[64]
Definition: vp3.c:296
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1794
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:119
#define ff_dlog(a,...)
bitstream reader API header.
VLC ac_vlc_2[16]
Definition: vp3.c:262
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
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:883
static const uint8_t mode_code_vlc_table[8][2]
Definition: vp3data.h:144
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2203
#define FFALIGN(x, a)
Definition: macros.h:48
#define MODE_INTRA
Definition: vp3.c:70
#define av_log(a,...)
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:1295
static const uint16_t table[]
Definition: prosumer.c:206
static void body(uint32_t ABCD[4], const uint8_t *src, int nblocks)
Definition: md5.c:101
static const uint16_t ac_bias_1[16][32][2]
Definition: vp3data.h:1539
int height
Definition: vp3.c:162
static const uint8_t vp4_pred_block_type_map[8]
Definition: vp3.c:140
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int vp3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp3.c:2642
static const uint8_t motion_vector_vlc_table[63][2]
Definition: vp3data.h:151
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:448
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:946
VP3DSPContext vp3dsp
Definition: vp3.c:172
int c_superblock_width
Definition: vp3.c:186
uint8_t qr_count[2][3]
Definition: vp3.c:218
int fragment_height[2]
Definition: vp3.c:203
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:136
VLC ac_vlc_3[16]
Definition: vp3.c:263
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define CODING_MODE_COUNT
Definition: vp3.c:77
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:2550
void(* idct_add)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:42
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2839
static const int8_t fixed_motion_vector_table[64]
Definition: vp3data.h:189
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1645
void(* h_loop_filter)(uint8_t *src, ptrdiff_t stride, int *bounding_values)
Definition: vp3dsp.h:45
AVCodec ff_theora_decoder
int theora
Definition: vp3.c:160
static av_cold void free_tables(AVCodecContext *avctx)
Definition: vp3.c:306
const char * name
Name of the codec implementation.
Definition: avcodec.h:3484
uint8_t bits
Definition: vp3data.h:202
int theora_header
Definition: vp3.c:160
GLsizei count
Definition: opengl_enc.c:108
#define FFMAX(a, b)
Definition: common.h:94
uint16_t coded_dc_scale_factor[2][64]
Definition: vp3.c:215
int qps[3]
Definition: vp3.c:178
#define fail()
Definition: checkasm.h:120
static const int ModeAlphabet[6][CODING_MODE_COUNT]
Definition: vp3.c:87
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1037
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: internal.h:66
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
static const int16_t *const coeff_tables[32]
Definition: vp3data.h:407
int chroma_y_shift
Definition: vp3.c:163
int flipped_image
Definition: vp3.c:174
unsigned char * macroblock_coding
Definition: vp3.c:285
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:282
Half-pel DSP context.
Definition: hpeldsp.h:45
int fragment_width[2]
Definition: vp3.c:202
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:975
int type
Definition: vp3.c:153
#define SET_CHROMA_MODES
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:351
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:908
VLC block_pattern_vlc[2]
Definition: vp3.c:268
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2831
#define FFMIN(a, b)
Definition: common.h:96
VLC fragment_run_length_vlc
Definition: vp3.c:267
VLC vp4_mv_vlc[2][7]
Definition: vp3.c:271
#define PU
#define width
#define FFSIGN(a)
Definition: common.h:73
int macroblock_height
Definition: vp3.c:195
int width
picture width / height.
Definition: avcodec.h:1738
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
#define SB_PARTIALLY_CODED
Definition: vp3.c:61
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, VLC *table, int coeff_index, int plane, int eob_run)
Definition: vp3.c:1174
int yuv_macroblock_count
Definition: vp3.c:199
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:450
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
uint8_t * edge_emu_buffer
Definition: vp3.c:287
static const uint8_t vp4_mv_table_selector[32]
Definition: vp4data.h:105
static const uint16_t vp4_ac_bias_3[16][32][2]
Definition: vp4data.h:1023
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2175
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
static const uint16_t vp31_ac_scale_factor[64]
Definition: vp3data.h:76
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
perm
Definition: f_perms.c:74
static const int8_t motion_vector_table[63]
Definition: vp3data.h:179
#define MODE_COPY
Definition: vp3.c:80
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
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
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:797
static const uint16_t ac_bias_2[16][32][2]
Definition: vp3data.h:2086
int n
Definition: avisynth_c.h:760
static const uint8_t hilbert_offset[16][2]
Definition: vp3.c:125
static const uint8_t vp4_y_dc_scale_factor[64]
Definition: vp4data.h:42
int macroblock_count
Definition: vp3.c:193
int c_superblock_height
Definition: vp3.c:187
void ff_vp3dsp_h_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
static void error(const char *err)
int offset_x_warned
Definition: vp3.c:210
int total_num_coded_frags
Definition: vp3.c:250
void(* idct_dc_add)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:43
int c_superblock_count
Definition: vp3.c:188
if(ret)
AVCodec ff_vp3_decoder
Definition: vp3.c:3259
VP4 video decoder.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
Definition: vp3.c:1776
static const int8_t transform[32][32]
Definition: hevcdsp.c:27
also ITU-R BT1361
Definition: pixfmt.h:469
static const uint8_t vp4_filter_limit_values[64]
Definition: vp4data.h:75
Half-pel DSP functions.
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int superblock_count
Definition: vp3.c:182
static const uint8_t vp4_block_pattern_table_selector[14]
Definition: vp4data.h:86
Libavcodec external API header.
int entries
Definition: vp3.c:292
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 add an so the codec calls ff_thread_report set AVCodecInternal allocate_progress The frames must then be freed with ff_thread_release_buffer().Otherwise leave it at zero and 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
static const uint16_t ac_bias_0[16][32][2]
Definition: vp3data.h:992
enum AVCodecID codec_id
Definition: avcodec.h:1575
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
int16_t * dct_tokens[3][64]
This is a list of all tokens in bitstream order.
Definition: vp3.c:239
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int skip_loop_filter
Definition: vp3.c:176
static int loop
Definition: ffplay.c:340
int debug
debug
Definition: avcodec.h:2646
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
ThreadFrame current_frame
Definition: vp3.c:166
main external API structure.
Definition: avcodec.h:1565
#define RSHIFT(a, b)
Definition: common.h:54
int last_qps[3]
Definition: vp3.c:180
static const uint8_t vp4_generic_dequant[64]
Definition: vp4data.h:31
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1590
uint8_t qr_size[2][3][64]
Definition: vp3.c:219
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
#define PUL
static av_cold int allocate_tables(AVCodecContext *avctx)
Allocate tables for per-frame data in Vp3DecodeContext.
Definition: vp3.c:2246
int data_offset[3]
Definition: vp3.c:207
void * buf
Definition: avisynth_c.h:766
int extradata_size
Definition: avcodec.h:1667
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
int coded_height
Definition: avcodec.h:1753
op_pixels_func put_no_rnd_pixels_tab[4][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:82
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define SB_FULLY_CODED
Definition: vp3.c:62
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2189
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2182
static const struct @175 eob_run_table[7]
int * nkf_coded_fragment_list
Definition: vp3.c:257
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
int num_coded_frags[3][64]
number of blocks that contain DCT coefficients at the given level or higher
Definition: vp3.c:249
int keyframe
Definition: vp3.c:167
#define TOKEN_COEFF(coeff)
Definition: vp3.c:243
#define s1
Definition: regdef.h:38
static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
Definition: vp3.c:892
static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2]
Definition: imc.c:120
#define MODE_GOLDEN_MV
Definition: vp3.c:75
static const uint8_t vp31_dc_scale_factor[64]
Definition: vp3data.h:65
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:151
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
#define FRAGMENT_PIXELS
Definition: vp3.c:51
static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
Definition: vp3.c:2888
static int update_frames(AVCodecContext *avctx)
Release and shuffle frames after decode finishes.
Definition: vp3.c:2541
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:8
static const uint16_t superblock_run_length_vlc_table[34][2]
Definition: vp3data.h:98
#define MODE_USING_GOLDEN
Definition: vp3.c:74
uint32_t huffman_table[80][32][2]
Definition: vp3.c:294
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
#define MODE_INTER_FOURMV
Definition: vp3.c:76
static const uint8_t vp4_uv_dc_scale_factor[64]
Definition: vp4data.h:53
int16_t block[64]
Definition: vp3.c:173
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
int v_superblock_start
Definition: vp3.c:190
static const uint8_t vp4_block_pattern_vlc[2][14][2]
Definition: vp4data.h:90
static int theora_header(AVFormatContext *s, int idx)
int version
Definition: vp3.c:161
static const uint16_t vp4_ac_bias_2[16][32][2]
Definition: vp4data.h:860
void ff_vp3dsp_v_loop_filter_12(uint8_t *first_pixel, ptrdiff_t stride, int *bounding_values)
int * coded_fragment_list[3]
Definition: vp3.c:254
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
unsigned char * superblock_coding
Definition: vp3.c:191
common internal api header.
ThreadFrame last_frame
Definition: vp3.c:165
int16_t * dct_tokens_base
Definition: vp3.c:240
AVCodecContext * avctx
Definition: vp3.c:159
static const int8_t vp31_inter_dequant[64]
Definition: vp3data.h:54
static int get_eob_run(GetBitContext *gb, int token)
Definition: vp3.c:1138
VideoDSPContext vdsp
Definition: vp3.c:171
int c_macroblock_width
Definition: vp3.c:197
static const uint16_t vp4_ac_bias_1[16][32][2]
Definition: vp4data.h:697
int den
Denominator.
Definition: rational.h:60
int c_macroblock_count
Definition: vp3.c:196
Core video DSP helper functions.
uint8_t base_matrix[384][64]
Definition: vp3.c:217
void ff_vp3dsp_set_bounding_values(int *bounding_values_array, int filter_limit)
Definition: vp3dsp.c:473
int fragment_count
Definition: vp3.c:201
void * priv_data
Definition: avcodec.h:1592
static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:1096
int * kf_coded_fragment_list
Definition: vp3.c:256
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:1930
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1600
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:346
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
int c_macroblock_height
Definition: vp3.c:198
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1652
#define MODE_INTER_PRIOR_LAST
Definition: vp3.c:73
#define MODE_INTER_NO_MV
Definition: vp3.c:69
VP4Predictor * dc_pred_row
Definition: vp3.c:299
int fragment_start[3]
Definition: vp3.c:206
int theora_tables
Definition: vp3.c:160
#define av_freep(p)
static const uint16_t vp4_mv_vlc[2][7][63][2]
Definition: vp4data.h:112
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:544
#define VLC_TYPE
Definition: vlc.h:24
#define MODE_INTER_LAST_MV
Definition: vp3.c:72
#define av_malloc_array(a, b)
ThreadFrame golden_frame
Definition: vp3.c:164
int chroma_x_shift
Definition: vp3.c:163
void(* idct_put)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:41
#define stride
av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
Definition: vp3dsp.c:445
static const uint8_t vp31_filter_limit_values[64]
Definition: vp3data.h:87
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
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1454
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
called when all pixels up to row y are complete
Definition: vp3.c:1888
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1176
int16_t dc
Definition: vp3.c:55
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
uint8_t offset_x
Definition: vp3.c:208
for(j=16;j >0;--j)
uint32_t coded_ac_scale_factor[64]
Definition: vp3.c:216
static const uint8_t zero_run_get_bits[32]
Definition: vp3data.h:214
Predicted.
Definition: avutil.h:275
VLC dc_vlc[16]
Definition: vp3.c:260
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
#define PL
int8_t(*[2] motion_val)[2]
Definition: vp3.c:212