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