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