FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vp3.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2004 The FFmpeg project
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * On2 VP3 Video Decoder
24  *
25  * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
26  * For more information about the VP3 coding process, visit:
27  * http://wiki.multimedia.cx/index.php?title=On2_VP3
28  *
29  * Theora decoder by Alex Beregszaszi
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "libavutil/imgutils.h"
37 
38 #include "avcodec.h"
39 #include "get_bits.h"
40 #include "hpeldsp.h"
41 #include "internal.h"
42 #include "mathops.h"
43 #include "thread.h"
44 #include "videodsp.h"
45 #include "vp3data.h"
46 #include "vp3dsp.h"
47 #include "xiph.h"
48 
49 #define FRAGMENT_PIXELS 8
50 
51 // FIXME split things out into their own arrays
52 typedef struct Vp3Fragment {
53  int16_t dc;
56 } Vp3Fragment;
57 
58 #define SB_NOT_CODED 0
59 #define SB_PARTIALLY_CODED 1
60 #define SB_FULLY_CODED 2
61 
62 // This is the maximum length of a single long bit run that can be encoded
63 // for superblock coding or block qps. Theora special-cases this to read a
64 // bit instead of flipping the current bit to allow for runs longer than 4129.
65 #define MAXIMUM_LONG_BIT_RUN 4129
66 
67 #define MODE_INTER_NO_MV 0
68 #define MODE_INTRA 1
69 #define MODE_INTER_PLUS_MV 2
70 #define MODE_INTER_LAST_MV 3
71 #define MODE_INTER_PRIOR_LAST 4
72 #define MODE_USING_GOLDEN 5
73 #define MODE_GOLDEN_MV 6
74 #define MODE_INTER_FOURMV 7
75 #define CODING_MODE_COUNT 8
76 
77 /* special internal mode */
78 #define MODE_COPY 8
79 
80 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
81 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
82 
83 
84 /* There are 6 preset schemes, plus a free-form scheme */
85 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
86  /* scheme 1: Last motion vector dominates */
91 
92  /* scheme 2 */
97 
98  /* scheme 3 */
103 
104  /* scheme 4 */
109 
110  /* scheme 5: No motion vector dominates */
115 
116  /* scheme 6 */
121 };
122 
123 static const uint8_t hilbert_offset[16][2] = {
124  { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
125  { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
126  { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
127  { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
128 };
129 
130 #define MIN_DEQUANT_VAL 2
131 
132 typedef struct Vp3DecodeContext {
135  int version;
136  int width, height;
141  int keyframe;
147  DECLARE_ALIGNED(16, int16_t, block)[64];
151 
152  int qps[3];
153  int nqps;
154  int last_qps[3];
155 
165  unsigned char *superblock_coding;
166 
170 
174 
177  int data_offset[3];
181 
182  int8_t (*motion_val[2])[2];
183 
184  /* tables */
185  uint16_t coded_dc_scale_factor[64];
186  uint32_t coded_ac_scale_factor[64];
189  uint8_t qr_size[2][3][64];
190  uint16_t qr_base[2][3][64];
191 
192  /**
193  * This is a list of all tokens in bitstream order. Reordering takes place
194  * by pulling from each level during IDCT. As a consequence, IDCT must be
195  * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
196  * otherwise. The 32 different tokens with up to 12 bits of extradata are
197  * collapsed into 3 types, packed as follows:
198  * (from the low to high bits)
199  *
200  * 2 bits: type (0,1,2)
201  * 0: EOB run, 14 bits for run length (12 needed)
202  * 1: zero run, 7 bits for run length
203  * 7 bits for the next coefficient (3 needed)
204  * 2: coefficient, 14 bits (11 needed)
205  *
206  * Coefficients are signed, so are packed in the highest bits for automatic
207  * sign extension.
208  */
209  int16_t *dct_tokens[3][64];
210  int16_t *dct_tokens_base;
211 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
212 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
213 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
214 
215  /**
216  * number of blocks that contain DCT coefficients at
217  * the given level or higher
218  */
219  int num_coded_frags[3][64];
221 
222  /* this is a list of indexes into the all_fragments array indicating
223  * which of the fragments are coded */
225 
226  VLC dc_vlc[16];
231 
236 
237  /* these arrays need to be on 16-byte boundaries since SSE2 operations
238  * index into them */
239  DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
240 
241  /* This table contains superblock_count * 16 entries. Each set of 16
242  * numbers corresponds to the fragment indexes 0..15 of the superblock.
243  * An entry will be -1 to indicate that no entry corresponds to that
244  * index. */
246 
247  /* This is an array that indicates how a particular macroblock
248  * is coded. */
249  unsigned char *macroblock_coding;
250 
252 
253  /* Huffman decode */
254  int hti;
255  unsigned int hbits;
256  int entries;
258  uint32_t huffman_table[80][32][2];
259 
263 
264 /************************************************************************
265  * VP3 specific functions
266  ************************************************************************/
267 
268 static av_cold void free_tables(AVCodecContext *avctx)
269 {
270  Vp3DecodeContext *s = avctx->priv_data;
271 
273  av_freep(&s->all_fragments);
278  av_freep(&s->motion_val[0]);
279  av_freep(&s->motion_val[1]);
280 }
281 
282 static void vp3_decode_flush(AVCodecContext *avctx)
283 {
284  Vp3DecodeContext *s = avctx->priv_data;
285 
286  if (s->golden_frame.f)
288  if (s->last_frame.f)
290  if (s->current_frame.f)
292 }
293 
295 {
296  Vp3DecodeContext *s = avctx->priv_data;
297  int i;
298 
299  free_tables(avctx);
301 
302  s->theora_tables = 0;
303 
304  /* release all frames */
305  vp3_decode_flush(avctx);
309 
310  if (avctx->internal->is_copy)
311  return 0;
312 
313  for (i = 0; i < 16; i++) {
314  ff_free_vlc(&s->dc_vlc[i]);
315  ff_free_vlc(&s->ac_vlc_1[i]);
316  ff_free_vlc(&s->ac_vlc_2[i]);
317  ff_free_vlc(&s->ac_vlc_3[i]);
318  ff_free_vlc(&s->ac_vlc_4[i]);
319  }
320 
325 
326  return 0;
327 }
328 
329 /**
330  * This function sets up all of the various blocks mappings:
331  * superblocks <-> fragments, macroblocks <-> fragments,
332  * superblocks <-> macroblocks
333  *
334  * @return 0 is successful; returns 1 if *anything* went wrong.
335  */
337 {
338  int sb_x, sb_y, plane;
339  int x, y, i, j = 0;
340 
341  for (plane = 0; plane < 3; plane++) {
342  int sb_width = plane ? s->c_superblock_width
343  : s->y_superblock_width;
344  int sb_height = plane ? s->c_superblock_height
345  : s->y_superblock_height;
346  int frag_width = s->fragment_width[!!plane];
347  int frag_height = s->fragment_height[!!plane];
348 
349  for (sb_y = 0; sb_y < sb_height; sb_y++)
350  for (sb_x = 0; sb_x < sb_width; sb_x++)
351  for (i = 0; i < 16; i++) {
352  x = 4 * sb_x + hilbert_offset[i][0];
353  y = 4 * sb_y + hilbert_offset[i][1];
354 
355  if (x < frag_width && y < frag_height)
357  y * frag_width + x;
358  else
359  s->superblock_fragments[j++] = -1;
360  }
361  }
362 
363  return 0; /* successful path out */
364 }
365 
366 /*
367  * This function sets up the dequantization tables used for a particular
368  * frame.
369  */
370 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
371 {
372  int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
373  int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
374  int i, plane, inter, qri, bmi, bmj, qistart;
375 
376  for (inter = 0; inter < 2; inter++) {
377  for (plane = 0; plane < 3; plane++) {
378  int sum = 0;
379  for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
380  sum += s->qr_size[inter][plane][qri];
381  if (s->qps[qpi] <= sum)
382  break;
383  }
384  qistart = sum - s->qr_size[inter][plane][qri];
385  bmi = s->qr_base[inter][plane][qri];
386  bmj = s->qr_base[inter][plane][qri + 1];
387  for (i = 0; i < 64; i++) {
388  int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
389  2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
390  s->qr_size[inter][plane][qri]) /
391  (2 * s->qr_size[inter][plane][qri]);
392 
393  int qmin = 8 << (inter + !i);
394  int qscale = i ? ac_scale_factor : dc_scale_factor;
395 
396  s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
397  av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
398  }
399  /* all DC coefficients use the same quant so as not to interfere
400  * with DC prediction */
401  s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
402  }
403  }
404 }
405 
406 /*
407  * This function initializes the loop filter boundary limits if the frame's
408  * quality index is different from the previous frame's.
409  *
410  * The filter_limit_values may not be larger than 127.
411  */
413 {
414  int *bounding_values = s->bounding_values_array + 127;
415  int filter_limit;
416  int x;
417  int value;
418 
419  filter_limit = s->filter_limit_values[s->qps[0]];
420  av_assert0(filter_limit < 128U);
421 
422  /* set up the bounding values */
423  memset(s->bounding_values_array, 0, 256 * sizeof(int));
424  for (x = 0; x < filter_limit; x++) {
425  bounding_values[-x] = -x;
426  bounding_values[x] = x;
427  }
428  for (x = value = filter_limit; x < 128 && value; x++, value--) {
429  bounding_values[ x] = value;
430  bounding_values[-x] = -value;
431  }
432  if (value)
433  bounding_values[128] = value;
434  bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
435 }
436 
437 /*
438  * This function unpacks all of the superblock/macroblock/fragment coding
439  * information from the bitstream.
440  */
442 {
443  int superblock_starts[3] = {
445  };
446  int bit = 0;
447  int current_superblock = 0;
448  int current_run = 0;
449  int num_partial_superblocks = 0;
450 
451  int i, j;
452  int current_fragment;
453  int plane;
454 
455  if (s->keyframe) {
457  } else {
458  /* unpack the list of partially-coded superblocks */
459  bit = get_bits1(gb) ^ 1;
460  current_run = 0;
461 
462  while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
463  if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
464  bit = get_bits1(gb);
465  else
466  bit ^= 1;
467 
468  current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
469  6, 2) + 1;
470  if (current_run == 34)
471  current_run += get_bits(gb, 12);
472 
473  if (current_run > s->superblock_count - current_superblock) {
475  "Invalid partially coded superblock run length\n");
476  return -1;
477  }
478 
479  memset(s->superblock_coding + current_superblock, bit, current_run);
480 
481  current_superblock += current_run;
482  if (bit)
483  num_partial_superblocks += current_run;
484  }
485 
486  /* unpack the list of fully coded superblocks if any of the blocks were
487  * not marked as partially coded in the previous step */
488  if (num_partial_superblocks < s->superblock_count) {
489  int superblocks_decoded = 0;
490 
491  current_superblock = 0;
492  bit = get_bits1(gb) ^ 1;
493  current_run = 0;
494 
495  while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
496  get_bits_left(gb) > 0) {
497  if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
498  bit = get_bits1(gb);
499  else
500  bit ^= 1;
501 
502  current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
503  6, 2) + 1;
504  if (current_run == 34)
505  current_run += get_bits(gb, 12);
506 
507  for (j = 0; j < current_run; current_superblock++) {
508  if (current_superblock >= s->superblock_count) {
510  "Invalid fully coded superblock run length\n");
511  return -1;
512  }
513 
514  /* skip any superblocks already marked as partially coded */
515  if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
516  s->superblock_coding[current_superblock] = 2 * bit;
517  j++;
518  }
519  }
520  superblocks_decoded += current_run;
521  }
522  }
523 
524  /* if there were partial blocks, initialize bitstream for
525  * unpacking fragment codings */
526  if (num_partial_superblocks) {
527  current_run = 0;
528  bit = get_bits1(gb);
529  /* toggle the bit because as soon as the first run length is
530  * fetched the bit will be toggled again */
531  bit ^= 1;
532  }
533  }
534 
535  /* figure out which fragments are coded; iterate through each
536  * superblock (all planes) */
537  s->total_num_coded_frags = 0;
539 
540  for (plane = 0; plane < 3; plane++) {
541  int sb_start = superblock_starts[plane];
542  int sb_end = sb_start + (plane ? s->c_superblock_count
543  : s->y_superblock_count);
544  int num_coded_frags = 0;
545 
546  for (i = sb_start; i < sb_end; i++) {
547  if (get_bits_left(gb) < ((s->total_num_coded_frags + num_coded_frags) >> 2)) {
548  return AVERROR_INVALIDDATA;
549  }
550  /* iterate through all 16 fragments in a superblock */
551  for (j = 0; j < 16; j++) {
552  /* if the fragment is in bounds, check its coding status */
553  current_fragment = s->superblock_fragments[i * 16 + j];
554  if (current_fragment != -1) {
555  int coded = s->superblock_coding[i];
556 
557  if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
558  /* fragment may or may not be coded; this is the case
559  * that cares about the fragment coding runs */
560  if (current_run-- == 0) {
561  bit ^= 1;
562  current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
563  }
564  coded = bit;
565  }
566 
567  if (coded) {
568  /* default mode; actual mode will be decoded in
569  * the next phase */
570  s->all_fragments[current_fragment].coding_method =
572  s->coded_fragment_list[plane][num_coded_frags++] =
573  current_fragment;
574  } else {
575  /* not coded; copy this fragment from the prior frame */
576  s->all_fragments[current_fragment].coding_method =
577  MODE_COPY;
578  }
579  }
580  }
581  }
582  s->total_num_coded_frags += num_coded_frags;
583  for (i = 0; i < 64; i++)
584  s->num_coded_frags[plane][i] = num_coded_frags;
585  if (plane < 2)
586  s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
587  num_coded_frags;
588  }
589  return 0;
590 }
591 
592 /*
593  * This function unpacks all the coding mode data for individual macroblocks
594  * from the bitstream.
595  */
597 {
598  int i, j, k, sb_x, sb_y;
599  int scheme;
600  int current_macroblock;
601  int current_fragment;
602  int coding_mode;
603  int custom_mode_alphabet[CODING_MODE_COUNT];
604  const int *alphabet;
605  Vp3Fragment *frag;
606 
607  if (s->keyframe) {
608  for (i = 0; i < s->fragment_count; i++)
610  } else {
611  /* fetch the mode coding scheme for this frame */
612  scheme = get_bits(gb, 3);
613 
614  /* is it a custom coding scheme? */
615  if (scheme == 0) {
616  for (i = 0; i < 8; i++)
617  custom_mode_alphabet[i] = MODE_INTER_NO_MV;
618  for (i = 0; i < 8; i++)
619  custom_mode_alphabet[get_bits(gb, 3)] = i;
620  alphabet = custom_mode_alphabet;
621  } else
622  alphabet = ModeAlphabet[scheme - 1];
623 
624  /* iterate through all of the macroblocks that contain 1 or more
625  * coded fragments */
626  for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
627  for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
628  if (get_bits_left(gb) <= 0)
629  return -1;
630 
631  for (j = 0; j < 4; j++) {
632  int mb_x = 2 * sb_x + (j >> 1);
633  int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
634  current_macroblock = mb_y * s->macroblock_width + mb_x;
635 
636  if (mb_x >= s->macroblock_width ||
637  mb_y >= s->macroblock_height)
638  continue;
639 
640 #define BLOCK_X (2 * mb_x + (k & 1))
641 #define BLOCK_Y (2 * mb_y + (k >> 1))
642  /* coding modes are only stored if the macroblock has
643  * at least one luma block coded, otherwise it must be
644  * INTER_NO_MV */
645  for (k = 0; k < 4; k++) {
646  current_fragment = BLOCK_Y *
647  s->fragment_width[0] + BLOCK_X;
648  if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
649  break;
650  }
651  if (k == 4) {
652  s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
653  continue;
654  }
655 
656  /* mode 7 means get 3 bits for each coding mode */
657  if (scheme == 7)
658  coding_mode = get_bits(gb, 3);
659  else
660  coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
661 
662  s->macroblock_coding[current_macroblock] = coding_mode;
663  for (k = 0; k < 4; k++) {
664  frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
665  if (frag->coding_method != MODE_COPY)
666  frag->coding_method = coding_mode;
667  }
668 
669 #define SET_CHROMA_MODES \
670  if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
671  frag[s->fragment_start[1]].coding_method = coding_mode; \
672  if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
673  frag[s->fragment_start[2]].coding_method = coding_mode;
674 
675  if (s->chroma_y_shift) {
676  frag = s->all_fragments + mb_y *
677  s->fragment_width[1] + mb_x;
679  } else if (s->chroma_x_shift) {
680  frag = s->all_fragments +
681  2 * mb_y * s->fragment_width[1] + mb_x;
682  for (k = 0; k < 2; k++) {
684  frag += s->fragment_width[1];
685  }
686  } else {
687  for (k = 0; k < 4; k++) {
688  frag = s->all_fragments +
689  BLOCK_Y * s->fragment_width[1] + BLOCK_X;
691  }
692  }
693  }
694  }
695  }
696  }
697 
698  return 0;
699 }
700 
701 /*
702  * This function unpacks all the motion vectors for the individual
703  * macroblocks from the bitstream.
704  */
706 {
707  int j, k, sb_x, sb_y;
708  int coding_mode;
709  int motion_x[4];
710  int motion_y[4];
711  int last_motion_x = 0;
712  int last_motion_y = 0;
713  int prior_last_motion_x = 0;
714  int prior_last_motion_y = 0;
715  int current_macroblock;
716  int current_fragment;
717  int frag;
718 
719  if (s->keyframe)
720  return 0;
721 
722  /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
723  coding_mode = get_bits1(gb);
724 
725  /* iterate through all of the macroblocks that contain 1 or more
726  * coded fragments */
727  for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
728  for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
729  if (get_bits_left(gb) <= 0)
730  return -1;
731 
732  for (j = 0; j < 4; j++) {
733  int mb_x = 2 * sb_x + (j >> 1);
734  int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
735  current_macroblock = mb_y * s->macroblock_width + mb_x;
736 
737  if (mb_x >= s->macroblock_width ||
738  mb_y >= s->macroblock_height ||
739  s->macroblock_coding[current_macroblock] == MODE_COPY)
740  continue;
741 
742  switch (s->macroblock_coding[current_macroblock]) {
743  case MODE_INTER_PLUS_MV:
744  case MODE_GOLDEN_MV:
745  /* all 6 fragments use the same motion vector */
746  if (coding_mode == 0) {
747  motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
748  motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
749  } else {
750  motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
751  motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
752  }
753 
754  /* vector maintenance, only on MODE_INTER_PLUS_MV */
755  if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
756  prior_last_motion_x = last_motion_x;
757  prior_last_motion_y = last_motion_y;
758  last_motion_x = motion_x[0];
759  last_motion_y = motion_y[0];
760  }
761  break;
762 
763  case MODE_INTER_FOURMV:
764  /* vector maintenance */
765  prior_last_motion_x = last_motion_x;
766  prior_last_motion_y = last_motion_y;
767 
768  /* fetch 4 vectors from the bitstream, one for each
769  * Y fragment, then average for the C fragment vectors */
770  for (k = 0; k < 4; k++) {
771  current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
772  if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
773  if (coding_mode == 0) {
774  motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
775  motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
776  } else {
777  motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
778  motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
779  }
780  last_motion_x = motion_x[k];
781  last_motion_y = motion_y[k];
782  } else {
783  motion_x[k] = 0;
784  motion_y[k] = 0;
785  }
786  }
787  break;
788 
789  case MODE_INTER_LAST_MV:
790  /* all 6 fragments use the last motion vector */
791  motion_x[0] = last_motion_x;
792  motion_y[0] = last_motion_y;
793 
794  /* no vector maintenance (last vector remains the
795  * last vector) */
796  break;
797 
799  /* all 6 fragments use the motion vector prior to the
800  * last motion vector */
801  motion_x[0] = prior_last_motion_x;
802  motion_y[0] = prior_last_motion_y;
803 
804  /* vector maintenance */
805  prior_last_motion_x = last_motion_x;
806  prior_last_motion_y = last_motion_y;
807  last_motion_x = motion_x[0];
808  last_motion_y = motion_y[0];
809  break;
810 
811  default:
812  /* covers intra, inter without MV, golden without MV */
813  motion_x[0] = 0;
814  motion_y[0] = 0;
815 
816  /* no vector maintenance */
817  break;
818  }
819 
820  /* assign the motion vectors to the correct fragments */
821  for (k = 0; k < 4; k++) {
822  current_fragment =
823  BLOCK_Y * s->fragment_width[0] + BLOCK_X;
824  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
825  s->motion_val[0][current_fragment][0] = motion_x[k];
826  s->motion_val[0][current_fragment][1] = motion_y[k];
827  } else {
828  s->motion_val[0][current_fragment][0] = motion_x[0];
829  s->motion_val[0][current_fragment][1] = motion_y[0];
830  }
831  }
832 
833  if (s->chroma_y_shift) {
834  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
835  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
836  motion_x[2] + motion_x[3], 2);
837  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
838  motion_y[2] + motion_y[3], 2);
839  }
840  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
841  motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
842  frag = mb_y * s->fragment_width[1] + mb_x;
843  s->motion_val[1][frag][0] = motion_x[0];
844  s->motion_val[1][frag][1] = motion_y[0];
845  } else if (s->chroma_x_shift) {
846  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
847  motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
848  motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
849  motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
850  motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
851  } else {
852  motion_x[1] = motion_x[0];
853  motion_y[1] = motion_y[0];
854  }
855  motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
856  motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
857 
858  frag = 2 * mb_y * s->fragment_width[1] + mb_x;
859  for (k = 0; k < 2; k++) {
860  s->motion_val[1][frag][0] = motion_x[k];
861  s->motion_val[1][frag][1] = motion_y[k];
862  frag += s->fragment_width[1];
863  }
864  } else {
865  for (k = 0; k < 4; k++) {
866  frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
867  if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
868  s->motion_val[1][frag][0] = motion_x[k];
869  s->motion_val[1][frag][1] = motion_y[k];
870  } else {
871  s->motion_val[1][frag][0] = motion_x[0];
872  s->motion_val[1][frag][1] = motion_y[0];
873  }
874  }
875  }
876  }
877  }
878  }
879 
880  return 0;
881 }
882 
884 {
885  int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
886  int num_blocks = s->total_num_coded_frags;
887 
888  for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
889  i = blocks_decoded = num_blocks_at_qpi = 0;
890 
891  bit = get_bits1(gb) ^ 1;
892  run_length = 0;
893 
894  do {
895  if (run_length == MAXIMUM_LONG_BIT_RUN)
896  bit = get_bits1(gb);
897  else
898  bit ^= 1;
899 
900  run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
901  if (run_length == 34)
902  run_length += get_bits(gb, 12);
903  blocks_decoded += run_length;
904 
905  if (!bit)
906  num_blocks_at_qpi += run_length;
907 
908  for (j = 0; j < run_length; i++) {
909  if (i >= s->total_num_coded_frags)
910  return -1;
911 
912  if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
913  s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
914  j++;
915  }
916  }
917  } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
918 
919  num_blocks -= num_blocks_at_qpi;
920  }
921 
922  return 0;
923 }
924 
925 /*
926  * This function is called by unpack_dct_coeffs() to extract the VLCs from
927  * the bitstream. The VLCs encode tokens which are used to unpack DCT
928  * data. This function unpacks all the VLCs for either the Y plane or both
929  * C planes, and is called for DC coefficients or different AC coefficient
930  * levels (since different coefficient types require different VLC tables.
931  *
932  * This function returns a residual eob run. E.g, if a particular token gave
933  * instructions to EOB the next 5 fragments and there were only 2 fragments
934  * left in the current fragment range, 3 would be returned so that it could
935  * be passed into the next call to this same function.
936  */
938  VLC *table, int coeff_index,
939  int plane,
940  int eob_run)
941 {
942  int i, j = 0;
943  int token;
944  int zero_run = 0;
945  int16_t coeff = 0;
946  int bits_to_get;
947  int blocks_ended;
948  int coeff_i = 0;
949  int num_coeffs = s->num_coded_frags[plane][coeff_index];
950  int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
951 
952  /* local references to structure members to avoid repeated dereferences */
953  int *coded_fragment_list = s->coded_fragment_list[plane];
954  Vp3Fragment *all_fragments = s->all_fragments;
955  VLC_TYPE(*vlc_table)[2] = table->table;
956 
957  if (num_coeffs < 0) {
959  "Invalid number of coefficients at level %d\n", coeff_index);
960  return AVERROR_INVALIDDATA;
961  }
962 
963  if (eob_run > num_coeffs) {
964  coeff_i =
965  blocks_ended = num_coeffs;
966  eob_run -= num_coeffs;
967  } else {
968  coeff_i =
969  blocks_ended = eob_run;
970  eob_run = 0;
971  }
972 
973  // insert fake EOB token to cover the split between planes or zzi
974  if (blocks_ended)
975  dct_tokens[j++] = blocks_ended << 2;
976 
977  while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
978  /* decode a VLC into a token */
979  token = get_vlc2(gb, vlc_table, 11, 3);
980  /* use the token to get a zero run, a coefficient, and an eob run */
981  if ((unsigned) token <= 6U) {
982  eob_run = eob_run_base[token];
983  if (eob_run_get_bits[token])
984  eob_run += get_bits(gb, eob_run_get_bits[token]);
985 
986  if (!eob_run)
987  eob_run = INT_MAX;
988 
989  // record only the number of blocks ended in this plane,
990  // any spill will be recorded in the next plane.
991  if (eob_run > num_coeffs - coeff_i) {
992  dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
993  blocks_ended += num_coeffs - coeff_i;
994  eob_run -= num_coeffs - coeff_i;
995  coeff_i = num_coeffs;
996  } else {
997  dct_tokens[j++] = TOKEN_EOB(eob_run);
998  blocks_ended += eob_run;
999  coeff_i += eob_run;
1000  eob_run = 0;
1001  }
1002  } else if (token >= 0) {
1003  bits_to_get = coeff_get_bits[token];
1004  if (bits_to_get)
1005  bits_to_get = get_bits(gb, bits_to_get);
1006  coeff = coeff_tables[token][bits_to_get];
1007 
1008  zero_run = zero_run_base[token];
1009  if (zero_run_get_bits[token])
1010  zero_run += get_bits(gb, zero_run_get_bits[token]);
1011 
1012  if (zero_run) {
1013  dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1014  } else {
1015  // Save DC into the fragment structure. DC prediction is
1016  // done in raster order, so the actual DC can't be in with
1017  // other tokens. We still need the token in dct_tokens[]
1018  // however, or else the structure collapses on itself.
1019  if (!coeff_index)
1020  all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1021 
1022  dct_tokens[j++] = TOKEN_COEFF(coeff);
1023  }
1024 
1025  if (coeff_index + zero_run > 64) {
1027  "Invalid zero run of %d with %d coeffs left\n",
1028  zero_run, 64 - coeff_index);
1029  zero_run = 64 - coeff_index;
1030  }
1031 
1032  // zero runs code multiple coefficients,
1033  // so don't try to decode coeffs for those higher levels
1034  for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1035  s->num_coded_frags[plane][i]--;
1036  coeff_i++;
1037  } else {
1038  av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1039  return -1;
1040  }
1041  }
1042 
1043  if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1044  av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1045 
1046  // decrement the number of blocks that have higher coefficients for each
1047  // EOB run at this level
1048  if (blocks_ended)
1049  for (i = coeff_index + 1; i < 64; i++)
1050  s->num_coded_frags[plane][i] -= blocks_ended;
1051 
1052  // setup the next buffer
1053  if (plane < 2)
1054  s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1055  else if (coeff_index < 63)
1056  s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1057 
1058  return eob_run;
1059 }
1060 
1062  int first_fragment,
1063  int fragment_width,
1064  int fragment_height);
1065 /*
1066  * This function unpacks all of the DCT coefficient data from the
1067  * bitstream.
1068  */
1070 {
1071  int i;
1072  int dc_y_table;
1073  int dc_c_table;
1074  int ac_y_table;
1075  int ac_c_table;
1076  int residual_eob_run = 0;
1077  VLC *y_tables[64];
1078  VLC *c_tables[64];
1079 
1080  s->dct_tokens[0][0] = s->dct_tokens_base;
1081 
1082  if (get_bits_left(gb) < 16)
1083  return AVERROR_INVALIDDATA;
1084 
1085  /* fetch the DC table indexes */
1086  dc_y_table = get_bits(gb, 4);
1087  dc_c_table = get_bits(gb, 4);
1088 
1089  /* unpack the Y plane DC coefficients */
1090  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1091  0, residual_eob_run);
1092  if (residual_eob_run < 0)
1093  return residual_eob_run;
1094  if (get_bits_left(gb) < 8)
1095  return AVERROR_INVALIDDATA;
1096 
1097  /* reverse prediction of the Y-plane DC coefficients */
1099 
1100  /* unpack the C plane DC coefficients */
1101  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1102  1, residual_eob_run);
1103  if (residual_eob_run < 0)
1104  return residual_eob_run;
1105  residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1106  2, residual_eob_run);
1107  if (residual_eob_run < 0)
1108  return residual_eob_run;
1109 
1110  /* reverse prediction of the C-plane DC coefficients */
1111  if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1113  s->fragment_width[1], s->fragment_height[1]);
1115  s->fragment_width[1], s->fragment_height[1]);
1116  }
1117 
1118  if (get_bits_left(gb) < 8)
1119  return AVERROR_INVALIDDATA;
1120  /* fetch the AC table indexes */
1121  ac_y_table = get_bits(gb, 4);
1122  ac_c_table = get_bits(gb, 4);
1123 
1124  /* build tables of AC VLC tables */
1125  for (i = 1; i <= 5; i++) {
1126  y_tables[i] = &s->ac_vlc_1[ac_y_table];
1127  c_tables[i] = &s->ac_vlc_1[ac_c_table];
1128  }
1129  for (i = 6; i <= 14; i++) {
1130  y_tables[i] = &s->ac_vlc_2[ac_y_table];
1131  c_tables[i] = &s->ac_vlc_2[ac_c_table];
1132  }
1133  for (i = 15; i <= 27; i++) {
1134  y_tables[i] = &s->ac_vlc_3[ac_y_table];
1135  c_tables[i] = &s->ac_vlc_3[ac_c_table];
1136  }
1137  for (i = 28; i <= 63; i++) {
1138  y_tables[i] = &s->ac_vlc_4[ac_y_table];
1139  c_tables[i] = &s->ac_vlc_4[ac_c_table];
1140  }
1141 
1142  /* decode all AC coefficients */
1143  for (i = 1; i <= 63; i++) {
1144  residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1145  0, residual_eob_run);
1146  if (residual_eob_run < 0)
1147  return residual_eob_run;
1148 
1149  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1150  1, residual_eob_run);
1151  if (residual_eob_run < 0)
1152  return residual_eob_run;
1153  residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1154  2, residual_eob_run);
1155  if (residual_eob_run < 0)
1156  return residual_eob_run;
1157  }
1158 
1159  return 0;
1160 }
1161 
1162 /*
1163  * This function reverses the DC prediction for each coded fragment in
1164  * the frame. Much of this function is adapted directly from the original
1165  * VP3 source code.
1166  */
1167 #define COMPATIBLE_FRAME(x) \
1168  (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1169 #define DC_COEFF(u) s->all_fragments[u].dc
1170 
1172  int first_fragment,
1173  int fragment_width,
1174  int fragment_height)
1175 {
1176 #define PUL 8
1177 #define PU 4
1178 #define PUR 2
1179 #define PL 1
1180 
1181  int x, y;
1182  int i = first_fragment;
1183 
1184  int predicted_dc;
1185 
1186  /* DC values for the left, up-left, up, and up-right fragments */
1187  int vl, vul, vu, vur;
1188 
1189  /* indexes for the left, up-left, up, and up-right fragments */
1190  int l, ul, u, ur;
1191 
1192  /*
1193  * The 6 fields mean:
1194  * 0: up-left multiplier
1195  * 1: up multiplier
1196  * 2: up-right multiplier
1197  * 3: left multiplier
1198  */
1199  static const int predictor_transform[16][4] = {
1200  { 0, 0, 0, 0 },
1201  { 0, 0, 0, 128 }, // PL
1202  { 0, 0, 128, 0 }, // PUR
1203  { 0, 0, 53, 75 }, // PUR|PL
1204  { 0, 128, 0, 0 }, // PU
1205  { 0, 64, 0, 64 }, // PU |PL
1206  { 0, 128, 0, 0 }, // PU |PUR
1207  { 0, 0, 53, 75 }, // PU |PUR|PL
1208  { 128, 0, 0, 0 }, // PUL
1209  { 0, 0, 0, 128 }, // PUL|PL
1210  { 64, 0, 64, 0 }, // PUL|PUR
1211  { 0, 0, 53, 75 }, // PUL|PUR|PL
1212  { 0, 128, 0, 0 }, // PUL|PU
1213  { -104, 116, 0, 116 }, // PUL|PU |PL
1214  { 24, 80, 24, 0 }, // PUL|PU |PUR
1215  { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1216  };
1217 
1218  /* This table shows which types of blocks can use other blocks for
1219  * prediction. For example, INTRA is the only mode in this table to
1220  * have a frame number of 0. That means INTRA blocks can only predict
1221  * from other INTRA blocks. There are 2 golden frame coding types;
1222  * blocks encoding in these modes can only predict from other blocks
1223  * that were encoded with these 1 of these 2 modes. */
1224  static const unsigned char compatible_frame[9] = {
1225  1, /* MODE_INTER_NO_MV */
1226  0, /* MODE_INTRA */
1227  1, /* MODE_INTER_PLUS_MV */
1228  1, /* MODE_INTER_LAST_MV */
1229  1, /* MODE_INTER_PRIOR_MV */
1230  2, /* MODE_USING_GOLDEN */
1231  2, /* MODE_GOLDEN_MV */
1232  1, /* MODE_INTER_FOUR_MV */
1233  3 /* MODE_COPY */
1234  };
1235  int current_frame_type;
1236 
1237  /* there is a last DC predictor for each of the 3 frame types */
1238  short last_dc[3];
1239 
1240  int transform = 0;
1241 
1242  vul =
1243  vu =
1244  vur =
1245  vl = 0;
1246  last_dc[0] =
1247  last_dc[1] =
1248  last_dc[2] = 0;
1249 
1250  /* for each fragment row... */
1251  for (y = 0; y < fragment_height; y++) {
1252  /* for each fragment in a row... */
1253  for (x = 0; x < fragment_width; x++, i++) {
1254 
1255  /* reverse prediction if this block was coded */
1256  if (s->all_fragments[i].coding_method != MODE_COPY) {
1257  current_frame_type =
1258  compatible_frame[s->all_fragments[i].coding_method];
1259 
1260  transform = 0;
1261  if (x) {
1262  l = i - 1;
1263  vl = DC_COEFF(l);
1264  if (COMPATIBLE_FRAME(l))
1265  transform |= PL;
1266  }
1267  if (y) {
1268  u = i - fragment_width;
1269  vu = DC_COEFF(u);
1270  if (COMPATIBLE_FRAME(u))
1271  transform |= PU;
1272  if (x) {
1273  ul = i - fragment_width - 1;
1274  vul = DC_COEFF(ul);
1275  if (COMPATIBLE_FRAME(ul))
1276  transform |= PUL;
1277  }
1278  if (x + 1 < fragment_width) {
1279  ur = i - fragment_width + 1;
1280  vur = DC_COEFF(ur);
1281  if (COMPATIBLE_FRAME(ur))
1282  transform |= PUR;
1283  }
1284  }
1285 
1286  if (transform == 0) {
1287  /* if there were no fragments to predict from, use last
1288  * DC saved */
1289  predicted_dc = last_dc[current_frame_type];
1290  } else {
1291  /* apply the appropriate predictor transform */
1292  predicted_dc =
1293  (predictor_transform[transform][0] * vul) +
1294  (predictor_transform[transform][1] * vu) +
1295  (predictor_transform[transform][2] * vur) +
1296  (predictor_transform[transform][3] * vl);
1297 
1298  predicted_dc /= 128;
1299 
1300  /* check for outranging on the [ul u l] and
1301  * [ul u ur l] predictors */
1302  if ((transform == 15) || (transform == 13)) {
1303  if (FFABS(predicted_dc - vu) > 128)
1304  predicted_dc = vu;
1305  else if (FFABS(predicted_dc - vl) > 128)
1306  predicted_dc = vl;
1307  else if (FFABS(predicted_dc - vul) > 128)
1308  predicted_dc = vul;
1309  }
1310  }
1311 
1312  /* at long last, apply the predictor */
1313  DC_COEFF(i) += predicted_dc;
1314  /* save the DC */
1315  last_dc[current_frame_type] = DC_COEFF(i);
1316  }
1317  }
1318  }
1319 }
1320 
1322  int ystart, int yend)
1323 {
1324  int x, y;
1325  int *bounding_values = s->bounding_values_array + 127;
1326 
1327  int width = s->fragment_width[!!plane];
1328  int height = s->fragment_height[!!plane];
1329  int fragment = s->fragment_start[plane] + ystart * width;
1330  ptrdiff_t stride = s->current_frame.f->linesize[plane];
1331  uint8_t *plane_data = s->current_frame.f->data[plane];
1332  if (!s->flipped_image)
1333  stride = -stride;
1334  plane_data += s->data_offset[plane] + 8 * ystart * stride;
1335 
1336  for (y = ystart; y < yend; y++) {
1337  for (x = 0; x < width; x++) {
1338  /* This code basically just deblocks on the edges of coded blocks.
1339  * However, it has to be much more complicated because of the
1340  * brain damaged deblock ordering used in VP3/Theora. Order matters
1341  * because some pixels get filtered twice. */
1342  if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1343  /* do not perform left edge filter for left columns frags */
1344  if (x > 0) {
1345  s->vp3dsp.h_loop_filter(
1346  plane_data + 8 * x,
1347  stride, bounding_values);
1348  }
1349 
1350  /* do not perform top edge filter for top row fragments */
1351  if (y > 0) {
1352  s->vp3dsp.v_loop_filter(
1353  plane_data + 8 * x,
1354  stride, bounding_values);
1355  }
1356 
1357  /* do not perform right edge filter for right column
1358  * fragments or if right fragment neighbor is also coded
1359  * in this frame (it will be filtered in next iteration) */
1360  if ((x < width - 1) &&
1361  (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1362  s->vp3dsp.h_loop_filter(
1363  plane_data + 8 * x + 8,
1364  stride, bounding_values);
1365  }
1366 
1367  /* do not perform bottom edge filter for bottom row
1368  * fragments or if bottom fragment neighbor is also coded
1369  * in this frame (it will be filtered in the next row) */
1370  if ((y < height - 1) &&
1371  (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1372  s->vp3dsp.v_loop_filter(
1373  plane_data + 8 * x + 8 * stride,
1374  stride, bounding_values);
1375  }
1376  }
1377 
1378  fragment++;
1379  }
1380  plane_data += 8 * stride;
1381  }
1382 }
1383 
1384 /**
1385  * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1386  * for the next block in coding order
1387  */
1388 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1389  int plane, int inter, int16_t block[64])
1390 {
1391  int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1392  uint8_t *perm = s->idct_scantable;
1393  int i = 0;
1394 
1395  do {
1396  int token = *s->dct_tokens[plane][i];
1397  switch (token & 3) {
1398  case 0: // EOB
1399  if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1400  s->dct_tokens[plane][i]++;
1401  else
1402  *s->dct_tokens[plane][i] = token & ~3;
1403  goto end;
1404  case 1: // zero run
1405  s->dct_tokens[plane][i]++;
1406  i += (token >> 2) & 0x7f;
1407  if (i > 63) {
1408  av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1409  return i;
1410  }
1411  block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1412  i++;
1413  break;
1414  case 2: // coeff
1415  block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1416  s->dct_tokens[plane][i++]++;
1417  break;
1418  default: // shouldn't happen
1419  return i;
1420  }
1421  } while (i < 64);
1422  // return value is expected to be a valid level
1423  i--;
1424 end:
1425  // the actual DC+prediction is in the fragment structure
1426  block[0] = frag->dc * s->qmat[0][inter][plane][0];
1427  return i;
1428 }
1429 
1430 /**
1431  * called when all pixels up to row y are complete
1432  */
1434 {
1435  int h, cy, i;
1437 
1438  if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1439  int y_flipped = s->flipped_image ? s->height - y : y;
1440 
1441  /* At the end of the frame, report INT_MAX instead of the height of
1442  * the frame. This makes the other threads' ff_thread_await_progress()
1443  * calls cheaper, because they don't have to clip their values. */
1445  y_flipped == s->height ? INT_MAX
1446  : y_flipped - 1,
1447  0);
1448  }
1449 
1450  if (!s->avctx->draw_horiz_band)
1451  return;
1452 
1453  h = y - s->last_slice_end;
1454  s->last_slice_end = y;
1455  y -= h;
1456 
1457  if (!s->flipped_image)
1458  y = s->height - y - h;
1459 
1460  cy = y >> s->chroma_y_shift;
1461  offset[0] = s->current_frame.f->linesize[0] * y;
1462  offset[1] = s->current_frame.f->linesize[1] * cy;
1463  offset[2] = s->current_frame.f->linesize[2] * cy;
1464  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1465  offset[i] = 0;
1466 
1467  emms_c();
1468  s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1469 }
1470 
1471 /**
1472  * Wait for the reference frame of the current fragment.
1473  * The progress value is in luma pixel rows.
1474  */
1476  int motion_y, int y)
1477 {
1479  int ref_row;
1480  int border = motion_y & 1;
1481 
1482  if (fragment->coding_method == MODE_USING_GOLDEN ||
1483  fragment->coding_method == MODE_GOLDEN_MV)
1484  ref_frame = &s->golden_frame;
1485  else
1486  ref_frame = &s->last_frame;
1487 
1488  ref_row = y + (motion_y >> 1);
1489  ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1490 
1491  ff_thread_await_progress(ref_frame, ref_row, 0);
1492 }
1493 
1494 /*
1495  * Perform the final rendering for a particular slice of data.
1496  * The slice number ranges from 0..(c_superblock_height - 1).
1497  */
1498 static void render_slice(Vp3DecodeContext *s, int slice)
1499 {
1500  int x, y, i, j, fragment;
1501  int16_t *block = s->block;
1502  int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1503  int motion_halfpel_index;
1504  uint8_t *motion_source;
1505  int plane, first_pixel;
1506 
1507  if (slice >= s->c_superblock_height)
1508  return;
1509 
1510  for (plane = 0; plane < 3; plane++) {
1512  s->data_offset[plane];
1513  uint8_t *last_plane = s->last_frame.f->data[plane] +
1514  s->data_offset[plane];
1515  uint8_t *golden_plane = s->golden_frame.f->data[plane] +
1516  s->data_offset[plane];
1517  ptrdiff_t stride = s->current_frame.f->linesize[plane];
1518  int plane_width = s->width >> (plane && s->chroma_x_shift);
1519  int plane_height = s->height >> (plane && s->chroma_y_shift);
1520  int8_t(*motion_val)[2] = s->motion_val[!!plane];
1521 
1522  int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1523  int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1524  int slice_width = plane ? s->c_superblock_width
1525  : s->y_superblock_width;
1526 
1527  int fragment_width = s->fragment_width[!!plane];
1528  int fragment_height = s->fragment_height[!!plane];
1529  int fragment_start = s->fragment_start[plane];
1530 
1531  int do_await = !plane && HAVE_THREADS &&
1533 
1534  if (!s->flipped_image)
1535  stride = -stride;
1536  if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1537  continue;
1538 
1539  /* for each superblock row in the slice (both of them)... */
1540  for (; sb_y < slice_height; sb_y++) {
1541  /* for each superblock in a row... */
1542  for (sb_x = 0; sb_x < slice_width; sb_x++) {
1543  /* for each block in a superblock... */
1544  for (j = 0; j < 16; j++) {
1545  x = 4 * sb_x + hilbert_offset[j][0];
1546  y = 4 * sb_y + hilbert_offset[j][1];
1547  fragment = y * fragment_width + x;
1548 
1549  i = fragment_start + fragment;
1550 
1551  // bounds check
1552  if (x >= fragment_width || y >= fragment_height)
1553  continue;
1554 
1555  first_pixel = 8 * y * stride + 8 * x;
1556 
1557  if (do_await &&
1560  motion_val[fragment][1],
1561  (16 * y) >> s->chroma_y_shift);
1562 
1563  /* transform if this block was coded */
1564  if (s->all_fragments[i].coding_method != MODE_COPY) {
1567  motion_source = golden_plane;
1568  else
1569  motion_source = last_plane;
1570 
1571  motion_source += first_pixel;
1572  motion_halfpel_index = 0;
1573 
1574  /* sort out the motion vector if this fragment is coded
1575  * using a motion vector method */
1576  if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1578  int src_x, src_y;
1579  motion_x = motion_val[fragment][0];
1580  motion_y = motion_val[fragment][1];
1581 
1582  src_x = (motion_x >> 1) + 8 * x;
1583  src_y = (motion_y >> 1) + 8 * y;
1584 
1585  motion_halfpel_index = motion_x & 0x01;
1586  motion_source += (motion_x >> 1);
1587 
1588  motion_halfpel_index |= (motion_y & 0x01) << 1;
1589  motion_source += ((motion_y >> 1) * stride);
1590 
1591  if (src_x < 0 || src_y < 0 ||
1592  src_x + 9 >= plane_width ||
1593  src_y + 9 >= plane_height) {
1595  if (stride < 0)
1596  temp -= 8 * stride;
1597 
1598  s->vdsp.emulated_edge_mc(temp, motion_source,
1599  stride, stride,
1600  9, 9, src_x, src_y,
1601  plane_width,
1602  plane_height);
1603  motion_source = temp;
1604  }
1605  }
1606 
1607  /* first, take care of copying a block from either the
1608  * previous or the golden frame */
1609  if (s->all_fragments[i].coding_method != MODE_INTRA) {
1610  /* Note, it is possible to implement all MC cases
1611  * with put_no_rnd_pixels_l2 which would look more
1612  * like the VP3 source but this would be slower as
1613  * put_no_rnd_pixels_tab is better optimized */
1614  if (motion_halfpel_index != 3) {
1615  s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1616  output_plane + first_pixel,
1617  motion_source, stride, 8);
1618  } else {
1619  /* d is 0 if motion_x and _y have the same sign,
1620  * else -1 */
1621  int d = (motion_x ^ motion_y) >> 31;
1622  s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
1623  motion_source - d,
1624  motion_source + stride + 1 + d,
1625  stride, 8);
1626  }
1627  }
1628 
1629  /* invert DCT and place (or add) in final output */
1630 
1631  if (s->all_fragments[i].coding_method == MODE_INTRA) {
1632  vp3_dequant(s, s->all_fragments + i,
1633  plane, 0, block);
1634  s->vp3dsp.idct_put(output_plane + first_pixel,
1635  stride,
1636  block);
1637  } else {
1638  if (vp3_dequant(s, s->all_fragments + i,
1639  plane, 1, block)) {
1640  s->vp3dsp.idct_add(output_plane + first_pixel,
1641  stride,
1642  block);
1643  } else {
1644  s->vp3dsp.idct_dc_add(output_plane + first_pixel,
1645  stride, block);
1646  }
1647  }
1648  } else {
1649  /* copy directly from the previous frame */
1650  s->hdsp.put_pixels_tab[1][0](
1651  output_plane + first_pixel,
1652  last_plane + first_pixel,
1653  stride, 8);
1654  }
1655  }
1656  }
1657 
1658  // Filter up to the last row in the superblock row
1659  if (!s->skip_loop_filter)
1660  apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
1661  FFMIN(4 * sb_y + 3, fragment_height - 1));
1662  }
1663  }
1664 
1665  /* this looks like a good place for slice dispatch... */
1666  /* algorithm:
1667  * if (slice == s->macroblock_height - 1)
1668  * dispatch (both last slice & 2nd-to-last slice);
1669  * else if (slice > 0)
1670  * dispatch (slice - 1);
1671  */
1672 
1673  vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
1674  s->height - 16));
1675 }
1676 
1677 /// Allocate tables for per-frame data in Vp3DecodeContext
1679 {
1680  Vp3DecodeContext *s = avctx->priv_data;
1681  int y_fragment_count, c_fragment_count;
1682 
1683  free_tables(avctx);
1684 
1685  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1686  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1687 
1690 
1691  s->coded_fragment_list[0] = av_mallocz_array(s->fragment_count, sizeof(int));
1692 
1694  64 * sizeof(*s->dct_tokens_base));
1695  s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
1696  s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
1697 
1698  /* work out the block mapping tables */
1699  s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
1701 
1702  if (!s->superblock_coding || !s->all_fragments ||
1703  !s->dct_tokens_base || !s->coded_fragment_list[0] ||
1705  !s->motion_val[0] || !s->motion_val[1]) {
1706  vp3_decode_end(avctx);
1707  return -1;
1708  }
1709 
1710  init_block_mapping(s);
1711 
1712  return 0;
1713 }
1714 
1716 {
1718  s->last_frame.f = av_frame_alloc();
1719  s->golden_frame.f = av_frame_alloc();
1720 
1721  if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
1723  av_frame_free(&s->last_frame.f);
1725  return AVERROR(ENOMEM);
1726  }
1727 
1728  return 0;
1729 }
1730 
1732 {
1733  Vp3DecodeContext *s = avctx->priv_data;
1734  int i, inter, plane, ret;
1735  int c_width;
1736  int c_height;
1737  int y_fragment_count, c_fragment_count;
1738 
1739  ret = init_frames(s);
1740  if (ret < 0)
1741  return ret;
1742 
1743  avctx->internal->allocate_progress = 1;
1744 
1745  if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
1746  s->version = 0;
1747  else
1748  s->version = 1;
1749 
1750  s->avctx = avctx;
1751  s->width = FFALIGN(avctx->coded_width, 16);
1752  s->height = FFALIGN(avctx->coded_height, 16);
1753  if (avctx->codec_id != AV_CODEC_ID_THEORA)
1754  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1757  ff_videodsp_init(&s->vdsp, 8);
1758  ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
1759 
1760  for (i = 0; i < 64; i++) {
1761 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
1762  s->idct_permutation[i] = TRANSPOSE(i);
1764 #undef TRANSPOSE
1765  }
1766 
1767  /* initialize to an impossible value which will force a recalculation
1768  * in the first frame decode */
1769  for (i = 0; i < 3; i++)
1770  s->qps[i] = -1;
1771 
1773  if (ret)
1774  return ret;
1775 
1776  s->y_superblock_width = (s->width + 31) / 32;
1777  s->y_superblock_height = (s->height + 31) / 32;
1779 
1780  /* work out the dimensions for the C planes */
1781  c_width = s->width >> s->chroma_x_shift;
1782  c_height = s->height >> s->chroma_y_shift;
1783  s->c_superblock_width = (c_width + 31) / 32;
1784  s->c_superblock_height = (c_height + 31) / 32;
1786 
1790 
1791  s->macroblock_width = (s->width + 15) / 16;
1792  s->macroblock_height = (s->height + 15) / 16;
1794 
1795  s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1796  s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1797  s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
1798  s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1799 
1800  /* fragment count covers all 8x8 blocks for all 3 planes */
1801  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1802  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1803  s->fragment_count = y_fragment_count + 2 * c_fragment_count;
1804  s->fragment_start[1] = y_fragment_count;
1805  s->fragment_start[2] = y_fragment_count + c_fragment_count;
1806 
1807  if (!s->theora_tables) {
1808  for (i = 0; i < 64; i++) {
1811  s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1812  s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1813  s->base_matrix[2][i] = vp31_inter_dequant[i];
1815  }
1816 
1817  for (inter = 0; inter < 2; inter++) {
1818  for (plane = 0; plane < 3; plane++) {
1819  s->qr_count[inter][plane] = 1;
1820  s->qr_size[inter][plane][0] = 63;
1821  s->qr_base[inter][plane][0] =
1822  s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1823  }
1824  }
1825 
1826  /* init VLC tables */
1827  for (i = 0; i < 16; i++) {
1828  /* DC histograms */
1829  init_vlc(&s->dc_vlc[i], 11, 32,
1830  &dc_bias[i][0][1], 4, 2,
1831  &dc_bias[i][0][0], 4, 2, 0);
1832 
1833  /* group 1 AC histograms */
1834  init_vlc(&s->ac_vlc_1[i], 11, 32,
1835  &ac_bias_0[i][0][1], 4, 2,
1836  &ac_bias_0[i][0][0], 4, 2, 0);
1837 
1838  /* group 2 AC histograms */
1839  init_vlc(&s->ac_vlc_2[i], 11, 32,
1840  &ac_bias_1[i][0][1], 4, 2,
1841  &ac_bias_1[i][0][0], 4, 2, 0);
1842 
1843  /* group 3 AC histograms */
1844  init_vlc(&s->ac_vlc_3[i], 11, 32,
1845  &ac_bias_2[i][0][1], 4, 2,
1846  &ac_bias_2[i][0][0], 4, 2, 0);
1847 
1848  /* group 4 AC histograms */
1849  init_vlc(&s->ac_vlc_4[i], 11, 32,
1850  &ac_bias_3[i][0][1], 4, 2,
1851  &ac_bias_3[i][0][0], 4, 2, 0);
1852  }
1853  } else {
1854  for (i = 0; i < 16; i++) {
1855  /* DC histograms */
1856  if (init_vlc(&s->dc_vlc[i], 11, 32,
1857  &s->huffman_table[i][0][1], 8, 4,
1858  &s->huffman_table[i][0][0], 8, 4, 0) < 0)
1859  goto vlc_fail;
1860 
1861  /* group 1 AC histograms */
1862  if (init_vlc(&s->ac_vlc_1[i], 11, 32,
1863  &s->huffman_table[i + 16][0][1], 8, 4,
1864  &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
1865  goto vlc_fail;
1866 
1867  /* group 2 AC histograms */
1868  if (init_vlc(&s->ac_vlc_2[i], 11, 32,
1869  &s->huffman_table[i + 16 * 2][0][1], 8, 4,
1870  &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
1871  goto vlc_fail;
1872 
1873  /* group 3 AC histograms */
1874  if (init_vlc(&s->ac_vlc_3[i], 11, 32,
1875  &s->huffman_table[i + 16 * 3][0][1], 8, 4,
1876  &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
1877  goto vlc_fail;
1878 
1879  /* group 4 AC histograms */
1880  if (init_vlc(&s->ac_vlc_4[i], 11, 32,
1881  &s->huffman_table[i + 16 * 4][0][1], 8, 4,
1882  &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
1883  goto vlc_fail;
1884  }
1885  }
1886 
1888  &superblock_run_length_vlc_table[0][1], 4, 2,
1889  &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1890 
1891  init_vlc(&s->fragment_run_length_vlc, 5, 30,
1892  &fragment_run_length_vlc_table[0][1], 4, 2,
1893  &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1894 
1895  init_vlc(&s->mode_code_vlc, 3, 8,
1896  &mode_code_vlc_table[0][1], 2, 1,
1897  &mode_code_vlc_table[0][0], 2, 1, 0);
1898 
1899  init_vlc(&s->motion_vector_vlc, 6, 63,
1900  &motion_vector_vlc_table[0][1], 2, 1,
1901  &motion_vector_vlc_table[0][0], 2, 1, 0);
1902 
1903  return allocate_tables(avctx);
1904 
1905 vlc_fail:
1906  av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1907  return -1;
1908 }
1909 
1910 /// Release and shuffle frames after decode finishes
1911 static int update_frames(AVCodecContext *avctx)
1912 {
1913  Vp3DecodeContext *s = avctx->priv_data;
1914  int ret = 0;
1915 
1916  /* shuffle frames (last = current) */
1919  if (ret < 0)
1920  goto fail;
1921 
1922  if (s->keyframe) {
1925  }
1926 
1927 fail:
1929  return ret;
1930 }
1931 
1933 {
1935  if (src->f->data[0])
1936  return ff_thread_ref_frame(dst, src);
1937  return 0;
1938 }
1939 
1941 {
1942  int ret;
1943  if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
1944  (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
1945  (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
1946  return ret;
1947  return 0;
1948 }
1949 
1950 #if HAVE_THREADS
1951 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1952 {
1953  Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1954  int qps_changed = 0, i, err;
1955 
1956 #define copy_fields(to, from, start_field, end_field) \
1957  memcpy(&to->start_field, &from->start_field, \
1958  (char *) &to->end_field - (char *) &to->start_field)
1959 
1960  if (!s1->current_frame.f->data[0] ||
1961  s->width != s1->width || s->height != s1->height) {
1962  if (s != s1)
1963  ref_frames(s, s1);
1964  return -1;
1965  }
1966 
1967  if (s != s1) {
1968  if (!s->current_frame.f)
1969  return AVERROR(ENOMEM);
1970  // init tables if the first frame hasn't been decoded
1971  if (!s->current_frame.f->data[0]) {
1972  int y_fragment_count, c_fragment_count;
1973  s->avctx = dst;
1974  err = allocate_tables(dst);
1975  if (err)
1976  return err;
1977  y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1978  c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1979  memcpy(s->motion_val[0], s1->motion_val[0],
1980  y_fragment_count * sizeof(*s->motion_val[0]));
1981  memcpy(s->motion_val[1], s1->motion_val[1],
1982  c_fragment_count * sizeof(*s->motion_val[1]));
1983  }
1984 
1985  // copy previous frame data
1986  if ((err = ref_frames(s, s1)) < 0)
1987  return err;
1988 
1989  s->keyframe = s1->keyframe;
1990 
1991  // copy qscale data if necessary
1992  for (i = 0; i < 3; i++) {
1993  if (s->qps[i] != s1->qps[1]) {
1994  qps_changed = 1;
1995  memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1996  }
1997  }
1998 
1999  if (s->qps[0] != s1->qps[0])
2000  memcpy(&s->bounding_values_array, &s1->bounding_values_array,
2001  sizeof(s->bounding_values_array));
2002 
2003  if (qps_changed)
2004  copy_fields(s, s1, qps, superblock_count);
2005 #undef copy_fields
2006  }
2007 
2008  return update_frames(dst);
2009 }
2010 #endif
2011 
2013  void *data, int *got_frame,
2014  AVPacket *avpkt)
2015 {
2016  AVFrame *frame = data;
2017  const uint8_t *buf = avpkt->data;
2018  int buf_size = avpkt->size;
2019  Vp3DecodeContext *s = avctx->priv_data;
2020  GetBitContext gb;
2021  int i, ret;
2022 
2023  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2024  return ret;
2025 
2026 #if CONFIG_THEORA_DECODER
2027  if (s->theora && get_bits1(&gb)) {
2028  int type = get_bits(&gb, 7);
2029  skip_bits_long(&gb, 6*8); /* "theora" */
2030 
2032  av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2033  return AVERROR_PATCHWELCOME;
2034  }
2035  if (type == 0) {
2036  vp3_decode_end(avctx);
2037  ret = theora_decode_header(avctx, &gb);
2038 
2039  if (ret >= 0)
2040  ret = vp3_decode_init(avctx);
2041  if (ret < 0) {
2042  vp3_decode_end(avctx);
2043  return ret;
2044  }
2045  return buf_size;
2046  } else if (type == 2) {
2047  vp3_decode_end(avctx);
2048  ret = theora_decode_tables(avctx, &gb);
2049  if (ret >= 0)
2050  ret = vp3_decode_init(avctx);
2051  if (ret < 0) {
2052  vp3_decode_end(avctx);
2053  return ret;
2054  }
2055  return buf_size;
2056  }
2057 
2058  av_log(avctx, AV_LOG_ERROR,
2059  "Header packet passed to frame decoder, skipping\n");
2060  return -1;
2061  }
2062 #endif
2063 
2064  s->keyframe = !get_bits1(&gb);
2065  if (!s->all_fragments) {
2066  av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2067  return -1;
2068  }
2069  if (!s->theora)
2070  skip_bits(&gb, 1);
2071  for (i = 0; i < 3; i++)
2072  s->last_qps[i] = s->qps[i];
2073 
2074  s->nqps = 0;
2075  do {
2076  s->qps[s->nqps++] = get_bits(&gb, 6);
2077  } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2078  for (i = s->nqps; i < 3; i++)
2079  s->qps[i] = -1;
2080 
2081  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2082  av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2083  s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2084 
2085  s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2086  avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2087  : AVDISCARD_NONKEY);
2088 
2089  if (s->qps[0] != s->last_qps[0])
2090  init_loop_filter(s);
2091 
2092  for (i = 0; i < s->nqps; i++)
2093  // reinit all dequantizers if the first one changed, because
2094  // the DC of the first quantizer must be used for all matrices
2095  if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2096  init_dequantizer(s, i);
2097 
2098  if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2099  return buf_size;
2100 
2103  s->current_frame.f->key_frame = s->keyframe;
2105  goto error;
2106 
2107  if (!s->edge_emu_buffer)
2109 
2110  if (s->keyframe) {
2111  if (!s->theora) {
2112  skip_bits(&gb, 4); /* width code */
2113  skip_bits(&gb, 4); /* height code */
2114  if (s->version) {
2115  s->version = get_bits(&gb, 5);
2116  if (avctx->frame_number == 0)
2118  "VP version: %d\n", s->version);
2119  }
2120  }
2121  if (s->version || s->theora) {
2122  if (get_bits1(&gb))
2124  "Warning, unsupported keyframe coding type?!\n");
2125  skip_bits(&gb, 2); /* reserved? */
2126  }
2127  } else {
2128  if (!s->golden_frame.f->data[0]) {
2130  "vp3: first frame not a keyframe\n");
2131 
2133  if (ff_thread_get_buffer(avctx, &s->golden_frame,
2135  goto error;
2137  if ((ret = ff_thread_ref_frame(&s->last_frame,
2138  &s->golden_frame)) < 0)
2139  goto error;
2140  ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2141  }
2142  }
2143 
2144  memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2145  ff_thread_finish_setup(avctx);
2146 
2147  if (unpack_superblocks(s, &gb)) {
2148  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2149  goto error;
2150  }
2151  if (unpack_modes(s, &gb)) {
2152  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2153  goto error;
2154  }
2155  if (unpack_vectors(s, &gb)) {
2156  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2157  goto error;
2158  }
2159  if (unpack_block_qpis(s, &gb)) {
2160  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2161  goto error;
2162  }
2163  if (unpack_dct_coeffs(s, &gb)) {
2164  av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2165  goto error;
2166  }
2167 
2168  for (i = 0; i < 3; i++) {
2169  int height = s->height >> (i && s->chroma_y_shift);
2170  if (s->flipped_image)
2171  s->data_offset[i] = 0;
2172  else
2173  s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2174  }
2175 
2176  s->last_slice_end = 0;
2177  for (i = 0; i < s->c_superblock_height; i++)
2178  render_slice(s, i);
2179 
2180  // filter the last row
2181  for (i = 0; i < 3; i++) {
2182  int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2183  apply_loop_filter(s, i, row, row + 1);
2184  }
2185  vp3_draw_horiz_band(s, s->height);
2186 
2187  /* output frame, offset as needed */
2188  if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2189  return ret;
2190 
2191  frame->crop_left = s->offset_x;
2192  frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
2193  frame->crop_top = s->offset_y;
2194  frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
2195 
2196  *got_frame = 1;
2197 
2198  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2199  ret = update_frames(avctx);
2200  if (ret < 0)
2201  return ret;
2202  }
2203 
2204  return buf_size;
2205 
2206 error:
2207  ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2208 
2209  if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2211 
2212  return -1;
2213 }
2214 
2216 {
2217  Vp3DecodeContext *s = avctx->priv_data;
2218 
2219  if (get_bits1(gb)) {
2220  int token;
2221  if (s->entries >= 32) { /* overflow */
2222  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2223  return -1;
2224  }
2225  token = get_bits(gb, 5);
2226  ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2227  s->hti, s->hbits, token, s->entries, s->huff_code_size);
2228  s->huffman_table[s->hti][token][0] = s->hbits;
2229  s->huffman_table[s->hti][token][1] = s->huff_code_size;
2230  s->entries++;
2231  } else {
2232  if (s->huff_code_size >= 32) { /* overflow */
2233  av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2234  return -1;
2235  }
2236  s->huff_code_size++;
2237  s->hbits <<= 1;
2238  if (read_huffman_tree(avctx, gb))
2239  return -1;
2240  s->hbits |= 1;
2241  if (read_huffman_tree(avctx, gb))
2242  return -1;
2243  s->hbits >>= 1;
2244  s->huff_code_size--;
2245  }
2246  return 0;
2247 }
2248 
2249 #if HAVE_THREADS
2250 static int vp3_init_thread_copy(AVCodecContext *avctx)
2251 {
2252  Vp3DecodeContext *s = avctx->priv_data;
2253 
2254  s->superblock_coding = NULL;
2255  s->all_fragments = NULL;
2256  s->coded_fragment_list[0] = NULL;
2257  s->dct_tokens_base = NULL;
2259  s->macroblock_coding = NULL;
2260  s->motion_val[0] = NULL;
2261  s->motion_val[1] = NULL;
2262  s->edge_emu_buffer = NULL;
2263 
2264  return init_frames(s);
2265 }
2266 #endif
2267 
2268 #if CONFIG_THEORA_DECODER
2269 static const enum AVPixelFormat theora_pix_fmts[4] = {
2271 };
2272 
2273 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2274 {
2275  Vp3DecodeContext *s = avctx->priv_data;
2276  int visible_width, visible_height, colorspace;
2277  uint8_t offset_x = 0, offset_y = 0;
2278  int ret;
2279  AVRational fps, aspect;
2280 
2281  s->theora_header = 0;
2282  s->theora = get_bits_long(gb, 24);
2283  av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2284 
2285  /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2286  * but previous versions have the image flipped relative to vp3 */
2287  if (s->theora < 0x030200) {
2288  s->flipped_image = 1;
2289  av_log(avctx, AV_LOG_DEBUG,
2290  "Old (<alpha3) Theora bitstream, flipped image\n");
2291  }
2292 
2293  visible_width =
2294  s->width = get_bits(gb, 16) << 4;
2295  visible_height =
2296  s->height = get_bits(gb, 16) << 4;
2297 
2298  if (s->theora >= 0x030200) {
2299  visible_width = get_bits_long(gb, 24);
2300  visible_height = get_bits_long(gb, 24);
2301 
2302  offset_x = get_bits(gb, 8); /* offset x */
2303  offset_y = get_bits(gb, 8); /* offset y, from bottom */
2304  }
2305 
2306  /* sanity check */
2307  if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2308  visible_width + offset_x > s->width ||
2309  visible_height + offset_y > s->height) {
2310  av_log(avctx, AV_LOG_ERROR,
2311  "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2312  visible_width, visible_height, offset_x, offset_y,
2313  s->width, s->height);
2314  return AVERROR_INVALIDDATA;
2315  }
2316 
2317  fps.num = get_bits_long(gb, 32);
2318  fps.den = get_bits_long(gb, 32);
2319  if (fps.num && fps.den) {
2320  if (fps.num < 0 || fps.den < 0) {
2321  av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2322  return AVERROR_INVALIDDATA;
2323  }
2324  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2325  fps.den, fps.num, 1 << 30);
2326  }
2327 
2328  aspect.num = get_bits_long(gb, 24);
2329  aspect.den = get_bits_long(gb, 24);
2330  if (aspect.num && aspect.den) {
2332  &avctx->sample_aspect_ratio.den,
2333  aspect.num, aspect.den, 1 << 30);
2334  ff_set_sar(avctx, avctx->sample_aspect_ratio);
2335  }
2336 
2337  if (s->theora < 0x030200)
2338  skip_bits(gb, 5); /* keyframe frequency force */
2339  colorspace = get_bits(gb, 8);
2340  skip_bits(gb, 24); /* bitrate */
2341 
2342  skip_bits(gb, 6); /* quality hint */
2343 
2344  if (s->theora >= 0x030200) {
2345  skip_bits(gb, 5); /* keyframe frequency force */
2346  avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2347  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2348  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2349  return AVERROR_INVALIDDATA;
2350  }
2351  skip_bits(gb, 3); /* reserved */
2352  } else
2353  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2354 
2355  ret = ff_set_dimensions(avctx, s->width, s->height);
2356  if (ret < 0)
2357  return ret;
2358  if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
2359  avctx->width = visible_width;
2360  avctx->height = visible_height;
2361  // translate offsets from theora axis ([0,0] lower left)
2362  // to normal axis ([0,0] upper left)
2363  s->offset_x = offset_x;
2364  s->offset_y = s->height - visible_height - offset_y;
2365  }
2366 
2367  if (colorspace == 1)
2369  else if (colorspace == 2)
2371 
2372  if (colorspace == 1 || colorspace == 2) {
2373  avctx->colorspace = AVCOL_SPC_BT470BG;
2374  avctx->color_trc = AVCOL_TRC_BT709;
2375  }
2376 
2377  s->theora_header = 1;
2378  return 0;
2379 }
2380 
2381 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2382 {
2383  Vp3DecodeContext *s = avctx->priv_data;
2384  int i, n, matrices, inter, plane;
2385 
2386  if (!s->theora_header)
2387  return AVERROR_INVALIDDATA;
2388 
2389  if (s->theora >= 0x030200) {
2390  n = get_bits(gb, 3);
2391  /* loop filter limit values table */
2392  if (n)
2393  for (i = 0; i < 64; i++)
2394  s->filter_limit_values[i] = get_bits(gb, n);
2395  }
2396 
2397  if (s->theora >= 0x030200)
2398  n = get_bits(gb, 4) + 1;
2399  else
2400  n = 16;
2401  /* quality threshold table */
2402  for (i = 0; i < 64; i++)
2403  s->coded_ac_scale_factor[i] = get_bits(gb, n);
2404 
2405  if (s->theora >= 0x030200)
2406  n = get_bits(gb, 4) + 1;
2407  else
2408  n = 16;
2409  /* dc scale factor table */
2410  for (i = 0; i < 64; i++)
2411  s->coded_dc_scale_factor[i] = get_bits(gb, n);
2412 
2413  if (s->theora >= 0x030200)
2414  matrices = get_bits(gb, 9) + 1;
2415  else
2416  matrices = 3;
2417 
2418  if (matrices > 384) {
2419  av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2420  return -1;
2421  }
2422 
2423  for (n = 0; n < matrices; n++)
2424  for (i = 0; i < 64; i++)
2425  s->base_matrix[n][i] = get_bits(gb, 8);
2426 
2427  for (inter = 0; inter <= 1; inter++) {
2428  for (plane = 0; plane <= 2; plane++) {
2429  int newqr = 1;
2430  if (inter || plane > 0)
2431  newqr = get_bits1(gb);
2432  if (!newqr) {
2433  int qtj, plj;
2434  if (inter && get_bits1(gb)) {
2435  qtj = 0;
2436  plj = plane;
2437  } else {
2438  qtj = (3 * inter + plane - 1) / 3;
2439  plj = (plane + 2) % 3;
2440  }
2441  s->qr_count[inter][plane] = s->qr_count[qtj][plj];
2442  memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
2443  sizeof(s->qr_size[0][0]));
2444  memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
2445  sizeof(s->qr_base[0][0]));
2446  } else {
2447  int qri = 0;
2448  int qi = 0;
2449 
2450  for (;;) {
2451  i = get_bits(gb, av_log2(matrices - 1) + 1);
2452  if (i >= matrices) {
2453  av_log(avctx, AV_LOG_ERROR,
2454  "invalid base matrix index\n");
2455  return -1;
2456  }
2457  s->qr_base[inter][plane][qri] = i;
2458  if (qi >= 63)
2459  break;
2460  i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
2461  s->qr_size[inter][plane][qri++] = i;
2462  qi += i;
2463  }
2464 
2465  if (qi > 63) {
2466  av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2467  return -1;
2468  }
2469  s->qr_count[inter][plane] = qri;
2470  }
2471  }
2472  }
2473 
2474  /* Huffman tables */
2475  for (s->hti = 0; s->hti < 80; s->hti++) {
2476  s->entries = 0;
2477  s->huff_code_size = 1;
2478  if (!get_bits1(gb)) {
2479  s->hbits = 0;
2480  if (read_huffman_tree(avctx, gb))
2481  return -1;
2482  s->hbits = 1;
2483  if (read_huffman_tree(avctx, gb))
2484  return -1;
2485  }
2486  }
2487 
2488  s->theora_tables = 1;
2489 
2490  return 0;
2491 }
2492 
2493 static av_cold int theora_decode_init(AVCodecContext *avctx)
2494 {
2495  Vp3DecodeContext *s = avctx->priv_data;
2496  GetBitContext gb;
2497  int ptype;
2498  const uint8_t *header_start[3];
2499  int header_len[3];
2500  int i;
2501  int ret;
2502 
2503  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2504 
2505  s->theora = 1;
2506 
2507  if (!avctx->extradata_size) {
2508  av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2509  return -1;
2510  }
2511 
2513  42, header_start, header_len) < 0) {
2514  av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2515  return -1;
2516  }
2517 
2518  for (i = 0; i < 3; i++) {
2519  if (header_len[i] <= 0)
2520  continue;
2521  ret = init_get_bits8(&gb, header_start[i], header_len[i]);
2522  if (ret < 0)
2523  return ret;
2524 
2525  ptype = get_bits(&gb, 8);
2526 
2527  if (!(ptype & 0x80)) {
2528  av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2529 // return -1;
2530  }
2531 
2532  // FIXME: Check for this as well.
2533  skip_bits_long(&gb, 6 * 8); /* "theora" */
2534 
2535  switch (ptype) {
2536  case 0x80:
2537  if (theora_decode_header(avctx, &gb) < 0)
2538  return -1;
2539  break;
2540  case 0x81:
2541 // FIXME: is this needed? it breaks sometimes
2542 // theora_decode_comments(avctx, gb);
2543  break;
2544  case 0x82:
2545  if (theora_decode_tables(avctx, &gb))
2546  return -1;
2547  break;
2548  default:
2549  av_log(avctx, AV_LOG_ERROR,
2550  "Unknown Theora config packet: %d\n", ptype & ~0x80);
2551  break;
2552  }
2553  if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
2554  av_log(avctx, AV_LOG_WARNING,
2555  "%d bits left in packet %X\n",
2556  8 * header_len[i] - get_bits_count(&gb), ptype);
2557  if (s->theora < 0x030200)
2558  break;
2559  }
2560 
2561  return vp3_decode_init(avctx);
2562 }
2563 
2565  .name = "theora",
2566  .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2567  .type = AVMEDIA_TYPE_VIDEO,
2568  .id = AV_CODEC_ID_THEORA,
2569  .priv_data_size = sizeof(Vp3DecodeContext),
2570  .init = theora_decode_init,
2571  .close = vp3_decode_end,
2576  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2577  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
2578  .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING,
2579 };
2580 #endif
2581 
2583  .name = "vp3",
2584  .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2585  .type = AVMEDIA_TYPE_VIDEO,
2586  .id = AV_CODEC_ID_VP3,
2587  .priv_data_size = sizeof(Vp3DecodeContext),
2588  .init = vp3_decode_init,
2589  .close = vp3_decode_end,
2594  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2595  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
2596 };
int plane
Definition: avisynth_c.h:422
#define BLOCK_Y
void(* put_no_rnd_pixels_l2)(uint8_t *dst, const uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h)
Copy 8xH pixels from source to destination buffer using a bilinear filter with no rounding (i...
Definition: vp3dsp.h:36
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
int last_slice_end
Definition: vp3.c:149
#define NULL
Definition: coverity.c:32
uint8_t idct_scantable[64]
Definition: vp3.c:143
AVRational framerate
Definition: avcodec.h:3040
discard all frames except keyframes
Definition: avcodec.h:793
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_NUM_DATA_POINTERS
Definition: frame.h:219
int16_t qmat[3][2][3][64]
qmat[qpi][is_inter][plane]
Definition: vp3.c:239
static int init_block_mapping(Vp3DecodeContext *s)
This function sets up all of the various blocks mappings: superblocks <-> fragments, macroblocks <-> fragments, superblocks <-> macroblocks.
Definition: vp3.c:336
#define SB_NOT_CODED
Definition: vp3.c:58
#define copy_fields(s, e)
static const uint8_t eob_run_base[7]
Definition: vp3data.h:201
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
#define TOKEN_EOB(eob_run)
Definition: vp3.c:211
static void render_slice(Vp3DecodeContext *s, int slice)
Definition: vp3.c:1498
#define PUR
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int y_superblock_count
Definition: vp3.c:159
static void flush(AVCodecContext *avctx)
int bounding_values_array[256+2]
Definition: vp3.c:261
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1705
static const int8_t vp31_intra_c_dequant[64]
Definition: vp3data.h:42
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
uint16_t qr_base[2][3][64]
Definition: vp3.c:190
AVFrame * f
Definition: thread.h:35
else temp
Definition: vf_mcdeint.c:256
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:212
VLC mode_code_vlc
Definition: vp3.c:234
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
int y_superblock_width
Definition: vp3.c:157
static const uint16_t fragment_run_length_vlc_table[30][2]
Definition: vp3data.h:119
HpelDSPContext hdsp
Definition: vp3.c:144
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:483
#define MODE_INTER_PLUS_MV
Definition: vp3.c:69
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1431
static const int8_t vp31_intra_y_dequant[64]
Definition: vp3data.h:29
static av_cold int init_frames(Vp3DecodeContext *s)
Definition: vp3.c:1715
int u_superblock_start
Definition: vp3.c:163
#define BLOCK_X
int av_log2(unsigned v)
Definition: intmath.c:26
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1896
static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:596
void(* v_loop_filter)(uint8_t *src, ptrdiff_t stride, int *bounding_values)
Definition: vp3dsp.h:44
static const uint8_t zero_run_base[32]
Definition: vp3data.h:208
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
uint8_t coding_method
Definition: vp3.c:54
static av_cold int vp3_decode_init(AVCodecContext *avctx)
Definition: vp3.c:1731
static const uint8_t coeff_get_bits[32]
Definition: vp3data.h:223
static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:441
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static void reverse_dc_prediction(Vp3DecodeContext *s, int first_fragment, int fragment_width, int fragment_height)
Definition: vp3.c:1171
discard all
Definition: avcodec.h:794
VLC ac_vlc_4[16]
Definition: vp3.c:230
size_t crop_bottom
Definition: frame.h:578
VLC motion_vector_vlc
Definition: vp3.c:235
static av_cold int vp3_decode_end(AVCodecContext *avctx)
Definition: vp3.c:294
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
int huff_code_size
Definition: vp3.c:257
#define src
Definition: vp8dsp.c:254
int * superblock_fragments
Definition: vp3.c:245
VLC superblock_run_length_vlc
Definition: vp3.c:232
AVCodec.
Definition: avcodec.h:3408
static const uint32_t vp31_ac_scale_factor[64]
Definition: vp3data.h:76
#define MAXIMUM_LONG_BIT_RUN
Definition: vp3.c:65
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static const uint16_t ac_bias_3[16][32][2]
Definition: vp3data.h:2634
static const uint16_t dc_bias[16][32][2]
Definition: vp3data.h:446
Vp3Fragment * all_fragments
Definition: vp3.c:175
static void init_loop_filter(Vp3DecodeContext *s)
Definition: vp3.c:412
#define COMPATIBLE_FRAME(x)
Definition: vp3.c:1167
static int16_t block[64]
Definition: dct.c:115
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t offset_y
Definition: vp3.c:179
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
int y_superblock_height
Definition: vp3.c:158
#define TRANSPOSE(x)
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:705
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
VLC ac_vlc_1[16]
Definition: vp3.c:227
#define TOKEN_ZERO_RUN(coeff, zero_run)
Definition: vp3.c:212
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2599
size_t crop_left
Definition: frame.h:579
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:1388
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
unsigned int hbits
Definition: vp3.c:255
Multithreading support functions.
int macroblock_width
Definition: vp3.c:168
uint8_t idct_permutation[64]
Definition: vp3.c:142
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:441
static void init_dequantizer(Vp3DecodeContext *s, int qpi)
Definition: vp3.c:370
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, ptrdiff_t dst_pitch, int dst_height)
Convert and output the current plane.
Definition: indeo3.c:1027
uint8_t qpi
Definition: vp3.c:55
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static void vp3_decode_flush(AVCodecContext *avctx)
Definition: vp3.c:282
static AVFrame * frame
#define DC_COEFF(u)
Definition: vp3.c:1169
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
#define height
uint8_t * data
Definition: avcodec.h:1430
uint8_t filter_limit_values[64]
Definition: vp3.c:260
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1784
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:119
#define ff_dlog(a,...)
bitstream reader API header.
VLC ac_vlc_2[16]
Definition: vp3.c:228
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
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:861
static const uint8_t mode_code_vlc_table[8][2]
Definition: vp3data.h:144
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2155
#define FFALIGN(x, a)
Definition: macros.h:48
#define MODE_INTRA
Definition: vp3.c:68
#define av_log(a,...)
static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:1069
static const uint16_t ac_bias_1[16][32][2]
Definition: vp3data.h:1540
int height
Definition: vp3.c:136
#define U(x)
Definition: vp56_arith.h:37
static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
Definition: vp3.c:1940
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:596
static int vp3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp3.c:2012
static const uint8_t motion_vector_vlc_table[63][2]
Definition: vp3data.h:151
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:429
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:924
VP3DSPContext vp3dsp
Definition: vp3.c:146
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
int c_superblock_width
Definition: vp3.c:160
uint8_t qr_count[2][3]
Definition: vp3.c:188
int fragment_height[2]
Definition: vp3.c:173
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:136
#define AVERROR(e)
Definition: error.h:43
VLC ac_vlc_3[16]
Definition: vp3.c:229
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define CODING_MODE_COUNT
Definition: vp3.c:75
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:2412
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2788
static const int8_t fixed_motion_vector_table[64]
Definition: vp3data.h:189
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
uint16_t width
Definition: gdv.c:47
AVCodec ff_theora_decoder
int theora
Definition: vp3.c:134
static av_cold void free_tables(AVCodecContext *avctx)
Definition: vp3.c:268
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
int theora_header
Definition: vp3.c:134
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
int qps[3]
Definition: vp3.c:152
#define fail()
Definition: checkasm.h:117
static const int ModeAlphabet[6][CODING_MODE_COUNT]
Definition: vp3.c:85
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1015
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: internal.h:66
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
static const int16_t *const coeff_tables[32]
Definition: vp3data.h:408
size_t crop_top
Definition: frame.h:577
int chroma_y_shift
Definition: vp3.c:137
int flipped_image
Definition: vp3.c:148
unsigned char * macroblock_coding
Definition: vp3.c:249
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
void(* h_loop_filter)(uint8_t *src, ptrdiff_t stride, int *bounding_values)
Definition: vp3dsp.h:45
static const uint8_t eob_run_get_bits[7]
Definition: vp3data.h:204
Half-pel DSP context.
Definition: hpeldsp.h:45
int fragment_width[2]
Definition: vp3.c:172
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:953
void(* idct_dc_add)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:43
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1752
#define SET_CHROMA_MODES
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:886
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2780
#define FFMIN(a, b)
Definition: common.h:96
VLC fragment_run_length_vlc
Definition: vp3.c:233
#define PU
int macroblock_height
Definition: vp3.c:169
int width
picture width / height.
Definition: avcodec.h:1690
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define SB_PARTIALLY_CODED
Definition: vp3.c:59
static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, VLC *table, int coeff_index, int plane, int eob_run)
Definition: vp3.c:937
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:431
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
uint8_t * edge_emu_buffer
Definition: vp3.c:251
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2127
perm
Definition: f_perms.c:74
static const int8_t motion_vector_table[63]
Definition: vp3data.h:179
#define MODE_COPY
Definition: vp3.c:78
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use. ...
Definition: xiph.c:24
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:563
static const uint16_t ac_bias_2[16][32][2]
Definition: vp3data.h:2087
int n
Definition: avisynth_c.h:684
static const uint8_t hilbert_offset[16][2]
Definition: vp3.c:123
int macroblock_count
Definition: vp3.c:167
int c_superblock_height
Definition: vp3.c:161
static void error(const char *err)
int offset_x_warned
Definition: vp3.c:180
int total_num_coded_frags
Definition: vp3.c:220
int c_superblock_count
Definition: vp3.c:162
AVCodec ff_vp3_decoder
Definition: vp3.c:2582
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
Definition: vp3.c:1321
static const int8_t transform[32][32]
Definition: hevcdsp.c:27
also ITU-R BT1361
Definition: pixfmt.h:450
Half-pel DSP functions.
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int superblock_count
Definition: vp3.c:156
Libavcodec external API header.
int entries
Definition: vp3.c:256
static const uint16_t ac_bias_0[16][32][2]
Definition: vp3data.h:993
enum AVCodecID codec_id
Definition: avcodec.h:1528
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
int16_t * dct_tokens[3][64]
This is a list of all tokens in bitstream order.
Definition: vp3.c:209
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:464
int skip_loop_filter
Definition: vp3.c:150
int debug
debug
Definition: avcodec.h:2598
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
ThreadFrame current_frame
Definition: vp3.c:140
main external API structure.
Definition: avcodec.h:1518
#define RSHIFT(a, b)
Definition: common.h:54
int last_qps[3]
Definition: vp3.c:154
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1543
uint8_t qr_size[2][3][64]
Definition: vp3.c:189
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
#define PUL
static av_cold int allocate_tables(AVCodecContext *avctx)
Allocate tables for per-frame data in Vp3DecodeContext.
Definition: vp3.c:1678
int data_offset[3]
Definition: vp3.c:177
void * buf
Definition: avisynth_c.h:690
size_t crop_right
Definition: frame.h:580
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1619
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
int coded_height
Definition: avcodec.h:1705
op_pixels_func put_no_rnd_pixels_tab[4][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:82
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:314
#define SB_FULLY_CODED
Definition: vp3.c:60
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2141
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2134
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
int num_coded_frags[3][64]
number of blocks that contain DCT coefficients at the given level or higher
Definition: vp3.c:219
int keyframe
Definition: vp3.c:141
#define TOKEN_COEFF(coeff)
Definition: vp3.c:213
#define s1
Definition: regdef.h:38
#define MODE_GOLDEN_MV
Definition: vp3.c:73
static const uint8_t vp31_dc_scale_factor[64]
Definition: vp3data.h:65
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:151
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:354
#define FRAGMENT_PIXELS
Definition: vp3.c:49
static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
Definition: vp3.c:2215
static int update_frames(AVCodecContext *avctx)
Release and shuffle frames after decode finishes.
Definition: vp3.c:1911
static const uint16_t superblock_run_length_vlc_table[34][2]
Definition: vp3data.h:98
#define MODE_USING_GOLDEN
Definition: vp3.c:72
uint32_t huffman_table[80][32][2]
Definition: vp3.c:258
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:551
void(* idct_put)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:41
#define MODE_INTER_FOURMV
Definition: vp3.c:74
int16_t block[64]
Definition: vp3.c:147
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
void(* idct_add)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vp3dsp.h:42
int v_superblock_start
Definition: vp3.c:164
int version
Definition: vp3.c:135
int * coded_fragment_list[3]
Definition: vp3.c:224
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
unsigned char * superblock_coding
Definition: vp3.c:165
common internal api header.
ThreadFrame last_frame
Definition: vp3.c:139
if(ret< 0)
Definition: vf_mcdeint.c:279
int16_t * dct_tokens_base
Definition: vp3.c:210
static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
Definition: vp3.c:1932
AVCodecContext * avctx
Definition: vp3.c:133
static const int8_t vp31_inter_dequant[64]
Definition: vp3data.h:54
VideoDSPContext vdsp
Definition: vp3.c:145
uint16_t coded_dc_scale_factor[64]
Definition: vp3.c:185
int den
Denominator.
Definition: rational.h:60
Core video DSP helper functions.
uint8_t base_matrix[384][64]
Definition: vp3.c:187
int fragment_count
Definition: vp3.c:171
void * priv_data
Definition: avcodec.h:1545
static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
Definition: vp3.c:883
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:1475
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1553
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1605
#define MODE_INTER_PRIOR_LAST
Definition: vp3.c:71
#define MODE_INTER_NO_MV
Definition: vp3.c:67
int fragment_start[3]
Definition: vp3.c:176
int theora_tables
Definition: vp3.c:134
#define av_freep(p)
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:525
#define VLC_TYPE
Definition: vlc.h:24
#define MODE_INTER_LAST_MV
Definition: vp3.c:70
ThreadFrame golden_frame
Definition: vp3.c:138
int chroma_x_shift
Definition: vp3.c:137
#define stride
av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
Definition: vp3dsp.c:280
static const uint8_t vp31_filter_limit_values[64]
Definition: vp3data.h:87
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1407
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
called when all pixels up to row y are complete
Definition: vp3.c:1433
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1135
int16_t dc
Definition: vp3.c:53
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
uint8_t offset_x
Definition: vp3.c:178
uint32_t coded_ac_scale_factor[64]
Definition: vp3.c:186
static const uint8_t zero_run_get_bits[32]
Definition: vp3data.h:215
Predicted.
Definition: avutil.h:275
VLC dc_vlc[16]
Definition: vp3.c:226
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
#define PL
int8_t(*[2] motion_val)[2]
Definition: vp3.c:182