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