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