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