2 * Copyright (C) 2003-2004 The FFmpeg project
4 * This file is part of FFmpeg.
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.
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.
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
23 * On2 VP3 Video Decoder
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
29 * Theora decoder by Alex Beregszaszi
36 #include "libavutil/imgutils.h"
49 #define FRAGMENT_PIXELS 8
51 // FIXME split things out into their own arrays
52 typedef struct Vp3Fragment {
54 uint8_t coding_method;
58 #define SB_NOT_CODED 0
59 #define SB_PARTIALLY_CODED 1
60 #define SB_FULLY_CODED 2
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
67 #define MODE_INTER_NO_MV 0
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
77 /* special internal mode */
80 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
81 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
84 /* There are 6 preset schemes, plus a free-form scheme */
85 static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
86 /* scheme 1: Last motion vector dominates */
87 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
88 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
89 MODE_INTRA, MODE_USING_GOLDEN,
90 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
93 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
94 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
95 MODE_INTRA, MODE_USING_GOLDEN,
96 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
99 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
100 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
101 MODE_INTRA, MODE_USING_GOLDEN,
102 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
105 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
106 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
107 MODE_INTRA, MODE_USING_GOLDEN,
108 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
110 /* scheme 5: No motion vector dominates */
111 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
112 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
113 MODE_INTRA, MODE_USING_GOLDEN,
114 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
117 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
118 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
119 MODE_INTER_PLUS_MV, MODE_INTRA,
120 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
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 }
130 #define MIN_DEQUANT_VAL 2
132 typedef struct Vp3DecodeContext {
133 AVCodecContext *avctx;
134 int theora, theora_tables, theora_header;
137 int chroma_x_shift, chroma_y_shift;
138 ThreadFrame golden_frame;
139 ThreadFrame last_frame;
140 ThreadFrame current_frame;
142 uint8_t idct_permutation[64];
143 uint8_t idct_scantable[64];
145 VideoDSPContext vdsp;
146 VP3DSPContext vp3dsp;
147 DECLARE_ALIGNED(16, int16_t, block)[64];
150 int skip_loop_filter;
156 int superblock_count;
157 int y_superblock_width;
158 int y_superblock_height;
159 int y_superblock_count;
160 int c_superblock_width;
161 int c_superblock_height;
162 int c_superblock_count;
163 int u_superblock_start;
164 int v_superblock_start;
165 unsigned char *superblock_coding;
167 int macroblock_count;
168 int macroblock_width;
169 int macroblock_height;
172 int fragment_width[2];
173 int fragment_height[2];
175 Vp3Fragment *all_fragments;
176 int fragment_start[3];
182 int8_t (*motion_val[2])[2];
185 uint16_t coded_dc_scale_factor[64];
186 uint32_t coded_ac_scale_factor[64];
187 uint8_t base_matrix[384][64];
188 uint8_t qr_count[2][3];
189 uint8_t qr_size[2][3][64];
190 uint16_t qr_base[2][3][64];
193 * This is a list of all tokens in bitstream order. Reordering takes place
194 * by pulling from each level during IDCT. As a consequence, IDCT must be
195 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
196 * otherwise. The 32 different tokens with up to 12 bits of extradata are
197 * collapsed into 3 types, packed as follows:
198 * (from the low to high bits)
200 * 2 bits: type (0,1,2)
201 * 0: EOB run, 14 bits for run length (12 needed)
202 * 1: zero run, 7 bits for run length
203 * 7 bits for the next coefficient (3 needed)
204 * 2: coefficient, 14 bits (11 needed)
206 * Coefficients are signed, so are packed in the highest bits for automatic
209 int16_t *dct_tokens[3][64];
210 int16_t *dct_tokens_base;
211 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
212 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
213 #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
216 * number of blocks that contain DCT coefficients at
217 * the given level or higher
219 int num_coded_frags[3][64];
220 int total_num_coded_frags;
222 /* this is a list of indexes into the all_fragments array indicating
223 * which of the fragments are coded */
224 int *coded_fragment_list[3];
232 VLC superblock_run_length_vlc;
233 VLC fragment_run_length_vlc;
235 VLC motion_vector_vlc;
237 /* these arrays need to be on 16-byte boundaries since SSE2 operations
239 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
241 /* This table contains superblock_count * 16 entries. Each set of 16
242 * numbers corresponds to the fragment indexes 0..15 of the superblock.
243 * An entry will be -1 to indicate that no entry corresponds to that
245 int *superblock_fragments;
247 /* This is an array that indicates how a particular macroblock
249 unsigned char *macroblock_coding;
251 uint8_t *edge_emu_buffer;
258 uint32_t huffman_table[80][32][2];
260 uint8_t filter_limit_values[64];
261 DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
264 /************************************************************************
265 * VP3 specific functions
266 ************************************************************************/
268 static av_cold void free_tables(AVCodecContext *avctx)
270 Vp3DecodeContext *s = avctx->priv_data;
272 av_freep(&s->superblock_coding);
273 av_freep(&s->all_fragments);
274 av_freep(&s->coded_fragment_list[0]);
275 av_freep(&s->dct_tokens_base);
276 av_freep(&s->superblock_fragments);
277 av_freep(&s->macroblock_coding);
278 av_freep(&s->motion_val[0]);
279 av_freep(&s->motion_val[1]);
282 static void vp3_decode_flush(AVCodecContext *avctx)
284 Vp3DecodeContext *s = avctx->priv_data;
286 if (s->golden_frame.f)
287 ff_thread_release_buffer(avctx, &s->golden_frame);
289 ff_thread_release_buffer(avctx, &s->last_frame);
290 if (s->current_frame.f)
291 ff_thread_release_buffer(avctx, &s->current_frame);
294 static av_cold int vp3_decode_end(AVCodecContext *avctx)
296 Vp3DecodeContext *s = avctx->priv_data;
300 av_freep(&s->edge_emu_buffer);
302 s->theora_tables = 0;
304 /* release all frames */
305 vp3_decode_flush(avctx);
306 av_frame_free(&s->current_frame.f);
307 av_frame_free(&s->last_frame.f);
308 av_frame_free(&s->golden_frame.f);
310 if (avctx->internal->is_copy)
313 for (i = 0; i < 16; i++) {
314 ff_free_vlc(&s->dc_vlc[i]);
315 ff_free_vlc(&s->ac_vlc_1[i]);
316 ff_free_vlc(&s->ac_vlc_2[i]);
317 ff_free_vlc(&s->ac_vlc_3[i]);
318 ff_free_vlc(&s->ac_vlc_4[i]);
321 ff_free_vlc(&s->superblock_run_length_vlc);
322 ff_free_vlc(&s->fragment_run_length_vlc);
323 ff_free_vlc(&s->mode_code_vlc);
324 ff_free_vlc(&s->motion_vector_vlc);
330 * This function sets up all of the various blocks mappings:
331 * superblocks <-> fragments, macroblocks <-> fragments,
332 * superblocks <-> macroblocks
334 * @return 0 is successful; returns 1 if *anything* went wrong.
336 static int init_block_mapping(Vp3DecodeContext *s)
338 int sb_x, sb_y, plane;
341 for (plane = 0; plane < 3; plane++) {
342 int sb_width = plane ? s->c_superblock_width
343 : s->y_superblock_width;
344 int sb_height = plane ? s->c_superblock_height
345 : s->y_superblock_height;
346 int frag_width = s->fragment_width[!!plane];
347 int frag_height = s->fragment_height[!!plane];
349 for (sb_y = 0; sb_y < sb_height; sb_y++)
350 for (sb_x = 0; sb_x < sb_width; sb_x++)
351 for (i = 0; i < 16; i++) {
352 x = 4 * sb_x + hilbert_offset[i][0];
353 y = 4 * sb_y + hilbert_offset[i][1];
355 if (x < frag_width && y < frag_height)
356 s->superblock_fragments[j++] = s->fragment_start[plane] +
359 s->superblock_fragments[j++] = -1;
363 return 0; /* successful path out */
367 * This function sets up the dequantization tables used for a particular
370 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
372 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
373 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
374 int i, plane, inter, qri, bmi, bmj, qistart;
376 for (inter = 0; inter < 2; inter++) {
377 for (plane = 0; plane < 3; plane++) {
379 for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
380 sum += s->qr_size[inter][plane][qri];
381 if (s->qps[qpi] <= sum)
384 qistart = sum - s->qr_size[inter][plane][qri];
385 bmi = s->qr_base[inter][plane][qri];
386 bmj = s->qr_base[inter][plane][qri + 1];
387 for (i = 0; i < 64; i++) {
388 int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
389 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
390 s->qr_size[inter][plane][qri]) /
391 (2 * s->qr_size[inter][plane][qri]);
393 int qmin = 8 << (inter + !i);
394 int qscale = i ? ac_scale_factor : dc_scale_factor;
396 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
397 av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
399 /* all DC coefficients use the same quant so as not to interfere
400 * with DC prediction */
401 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
407 * This function initializes the loop filter boundary limits if the frame's
408 * quality index is different from the previous frame's.
410 * The filter_limit_values may not be larger than 127.
412 static void init_loop_filter(Vp3DecodeContext *s)
414 int *bounding_values = s->bounding_values_array + 127;
419 filter_limit = s->filter_limit_values[s->qps[0]];
420 av_assert0(filter_limit < 128U);
422 /* set up the bounding values */
423 memset(s->bounding_values_array, 0, 256 * sizeof(int));
424 for (x = 0; x < filter_limit; x++) {
425 bounding_values[-x] = -x;
426 bounding_values[x] = x;
428 for (x = value = filter_limit; x < 128 && value; x++, value--) {
429 bounding_values[ x] = value;
430 bounding_values[-x] = -value;
433 bounding_values[128] = value;
434 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
438 * This function unpacks all of the superblock/macroblock/fragment coding
439 * information from the bitstream.
441 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
443 int superblock_starts[3] = {
444 0, s->u_superblock_start, s->v_superblock_start
447 int current_superblock = 0;
449 int num_partial_superblocks = 0;
452 int current_fragment;
456 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
458 /* unpack the list of partially-coded superblocks */
459 bit = get_bits1(gb) ^ 1;
462 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
463 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
468 current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
470 if (current_run == 34)
471 current_run += get_bits(gb, 12);
473 if (current_run > s->superblock_count - current_superblock) {
474 av_log(s->avctx, AV_LOG_ERROR,
475 "Invalid partially coded superblock run length\n");
479 memset(s->superblock_coding + current_superblock, bit, current_run);
481 current_superblock += current_run;
483 num_partial_superblocks += current_run;
486 /* unpack the list of fully coded superblocks if any of the blocks were
487 * not marked as partially coded in the previous step */
488 if (num_partial_superblocks < s->superblock_count) {
489 int superblocks_decoded = 0;
491 current_superblock = 0;
492 bit = get_bits1(gb) ^ 1;
495 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
496 get_bits_left(gb) > 0) {
497 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
502 current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
504 if (current_run == 34)
505 current_run += get_bits(gb, 12);
507 for (j = 0; j < current_run; current_superblock++) {
508 if (current_superblock >= s->superblock_count) {
509 av_log(s->avctx, AV_LOG_ERROR,
510 "Invalid fully coded superblock run length\n");
514 /* skip any superblocks already marked as partially coded */
515 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
516 s->superblock_coding[current_superblock] = 2 * bit;
520 superblocks_decoded += current_run;
524 /* if there were partial blocks, initialize bitstream for
525 * unpacking fragment codings */
526 if (num_partial_superblocks) {
529 /* toggle the bit because as soon as the first run length is
530 * fetched the bit will be toggled again */
535 /* figure out which fragments are coded; iterate through each
536 * superblock (all planes) */
537 s->total_num_coded_frags = 0;
538 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
540 for (plane = 0; plane < 3; plane++) {
541 int sb_start = superblock_starts[plane];
542 int sb_end = sb_start + (plane ? s->c_superblock_count
543 : s->y_superblock_count);
544 int num_coded_frags = 0;
546 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
547 /* iterate through all 16 fragments in a superblock */
548 for (j = 0; j < 16; j++) {
549 /* if the fragment is in bounds, check its coding status */
550 current_fragment = s->superblock_fragments[i * 16 + j];
551 if (current_fragment != -1) {
552 int coded = s->superblock_coding[i];
554 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
555 /* fragment may or may not be coded; this is the case
556 * that cares about the fragment coding runs */
557 if (current_run-- == 0) {
559 current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
565 /* default mode; actual mode will be decoded in
567 s->all_fragments[current_fragment].coding_method =
569 s->coded_fragment_list[plane][num_coded_frags++] =
572 /* not coded; copy this fragment from the prior frame */
573 s->all_fragments[current_fragment].coding_method =
579 s->total_num_coded_frags += num_coded_frags;
580 for (i = 0; i < 64; i++)
581 s->num_coded_frags[plane][i] = num_coded_frags;
583 s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
590 * This function unpacks all the coding mode data for individual macroblocks
591 * from the bitstream.
593 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
595 int i, j, k, sb_x, sb_y;
597 int current_macroblock;
598 int current_fragment;
600 int custom_mode_alphabet[CODING_MODE_COUNT];
605 for (i = 0; i < s->fragment_count; i++)
606 s->all_fragments[i].coding_method = MODE_INTRA;
608 /* fetch the mode coding scheme for this frame */
609 scheme = get_bits(gb, 3);
611 /* is it a custom coding scheme? */
613 for (i = 0; i < 8; i++)
614 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
615 for (i = 0; i < 8; i++)
616 custom_mode_alphabet[get_bits(gb, 3)] = i;
617 alphabet = custom_mode_alphabet;
619 alphabet = ModeAlphabet[scheme - 1];
621 /* iterate through all of the macroblocks that contain 1 or more
623 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
624 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
625 if (get_bits_left(gb) <= 0)
628 for (j = 0; j < 4; j++) {
629 int mb_x = 2 * sb_x + (j >> 1);
630 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
631 current_macroblock = mb_y * s->macroblock_width + mb_x;
633 if (mb_x >= s->macroblock_width ||
634 mb_y >= s->macroblock_height)
637 #define BLOCK_X (2 * mb_x + (k & 1))
638 #define BLOCK_Y (2 * mb_y + (k >> 1))
639 /* coding modes are only stored if the macroblock has
640 * at least one luma block coded, otherwise it must be
642 for (k = 0; k < 4; k++) {
643 current_fragment = BLOCK_Y *
644 s->fragment_width[0] + BLOCK_X;
645 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
649 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
653 /* mode 7 means get 3 bits for each coding mode */
655 coding_mode = get_bits(gb, 3);
657 coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
659 s->macroblock_coding[current_macroblock] = coding_mode;
660 for (k = 0; k < 4; k++) {
661 frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
662 if (frag->coding_method != MODE_COPY)
663 frag->coding_method = coding_mode;
666 #define SET_CHROMA_MODES \
667 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
668 frag[s->fragment_start[1]].coding_method = coding_mode; \
669 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
670 frag[s->fragment_start[2]].coding_method = coding_mode;
672 if (s->chroma_y_shift) {
673 frag = s->all_fragments + mb_y *
674 s->fragment_width[1] + mb_x;
676 } else if (s->chroma_x_shift) {
677 frag = s->all_fragments +
678 2 * mb_y * s->fragment_width[1] + mb_x;
679 for (k = 0; k < 2; k++) {
681 frag += s->fragment_width[1];
684 for (k = 0; k < 4; k++) {
685 frag = s->all_fragments +
686 BLOCK_Y * s->fragment_width[1] + BLOCK_X;
699 * This function unpacks all the motion vectors for the individual
700 * macroblocks from the bitstream.
702 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
704 int j, k, sb_x, sb_y;
708 int last_motion_x = 0;
709 int last_motion_y = 0;
710 int prior_last_motion_x = 0;
711 int prior_last_motion_y = 0;
712 int current_macroblock;
713 int current_fragment;
719 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
720 coding_mode = get_bits1(gb);
722 /* iterate through all of the macroblocks that contain 1 or more
724 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
725 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
726 if (get_bits_left(gb) <= 0)
729 for (j = 0; j < 4; j++) {
730 int mb_x = 2 * sb_x + (j >> 1);
731 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
732 current_macroblock = mb_y * s->macroblock_width + mb_x;
734 if (mb_x >= s->macroblock_width ||
735 mb_y >= s->macroblock_height ||
736 s->macroblock_coding[current_macroblock] == MODE_COPY)
739 switch (s->macroblock_coding[current_macroblock]) {
740 case MODE_INTER_PLUS_MV:
742 /* all 6 fragments use the same motion vector */
743 if (coding_mode == 0) {
744 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
745 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
747 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
748 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
751 /* vector maintenance, only on MODE_INTER_PLUS_MV */
752 if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
753 prior_last_motion_x = last_motion_x;
754 prior_last_motion_y = last_motion_y;
755 last_motion_x = motion_x[0];
756 last_motion_y = motion_y[0];
760 case MODE_INTER_FOURMV:
761 /* vector maintenance */
762 prior_last_motion_x = last_motion_x;
763 prior_last_motion_y = last_motion_y;
765 /* fetch 4 vectors from the bitstream, one for each
766 * Y fragment, then average for the C fragment vectors */
767 for (k = 0; k < 4; k++) {
768 current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
769 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
770 if (coding_mode == 0) {
771 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
772 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
774 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
775 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
777 last_motion_x = motion_x[k];
778 last_motion_y = motion_y[k];
786 case MODE_INTER_LAST_MV:
787 /* all 6 fragments use the last motion vector */
788 motion_x[0] = last_motion_x;
789 motion_y[0] = last_motion_y;
791 /* no vector maintenance (last vector remains the
795 case MODE_INTER_PRIOR_LAST:
796 /* all 6 fragments use the motion vector prior to the
797 * last motion vector */
798 motion_x[0] = prior_last_motion_x;
799 motion_y[0] = prior_last_motion_y;
801 /* vector maintenance */
802 prior_last_motion_x = last_motion_x;
803 prior_last_motion_y = last_motion_y;
804 last_motion_x = motion_x[0];
805 last_motion_y = motion_y[0];
809 /* covers intra, inter without MV, golden without MV */
813 /* no vector maintenance */
817 /* assign the motion vectors to the correct fragments */
818 for (k = 0; k < 4; k++) {
820 BLOCK_Y * s->fragment_width[0] + BLOCK_X;
821 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
822 s->motion_val[0][current_fragment][0] = motion_x[k];
823 s->motion_val[0][current_fragment][1] = motion_y[k];
825 s->motion_val[0][current_fragment][0] = motion_x[0];
826 s->motion_val[0][current_fragment][1] = motion_y[0];
830 if (s->chroma_y_shift) {
831 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
832 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
833 motion_x[2] + motion_x[3], 2);
834 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
835 motion_y[2] + motion_y[3], 2);
837 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
838 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
839 frag = mb_y * s->fragment_width[1] + mb_x;
840 s->motion_val[1][frag][0] = motion_x[0];
841 s->motion_val[1][frag][1] = motion_y[0];
842 } else if (s->chroma_x_shift) {
843 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
844 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
845 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
846 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
847 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
849 motion_x[1] = motion_x[0];
850 motion_y[1] = motion_y[0];
852 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
853 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
855 frag = 2 * mb_y * s->fragment_width[1] + mb_x;
856 for (k = 0; k < 2; k++) {
857 s->motion_val[1][frag][0] = motion_x[k];
858 s->motion_val[1][frag][1] = motion_y[k];
859 frag += s->fragment_width[1];
862 for (k = 0; k < 4; k++) {
863 frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
864 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
865 s->motion_val[1][frag][0] = motion_x[k];
866 s->motion_val[1][frag][1] = motion_y[k];
868 s->motion_val[1][frag][0] = motion_x[0];
869 s->motion_val[1][frag][1] = motion_y[0];
880 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
882 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
883 int num_blocks = s->total_num_coded_frags;
885 for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
886 i = blocks_decoded = num_blocks_at_qpi = 0;
888 bit = get_bits1(gb) ^ 1;
892 if (run_length == MAXIMUM_LONG_BIT_RUN)
897 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
898 if (run_length == 34)
899 run_length += get_bits(gb, 12);
900 blocks_decoded += run_length;
903 num_blocks_at_qpi += run_length;
905 for (j = 0; j < run_length; i++) {
906 if (i >= s->total_num_coded_frags)
909 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
910 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
914 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
916 num_blocks -= num_blocks_at_qpi;
923 * This function is called by unpack_dct_coeffs() to extract the VLCs from
924 * the bitstream. The VLCs encode tokens which are used to unpack DCT
925 * data. This function unpacks all the VLCs for either the Y plane or both
926 * C planes, and is called for DC coefficients or different AC coefficient
927 * levels (since different coefficient types require different VLC tables.
929 * This function returns a residual eob run. E.g, if a particular token gave
930 * instructions to EOB the next 5 fragments and there were only 2 fragments
931 * left in the current fragment range, 3 would be returned so that it could
932 * be passed into the next call to this same function.
934 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
935 VLC *table, int coeff_index,
946 int num_coeffs = s->num_coded_frags[plane][coeff_index];
947 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
949 /* local references to structure members to avoid repeated dereferences */
950 int *coded_fragment_list = s->coded_fragment_list[plane];
951 Vp3Fragment *all_fragments = s->all_fragments;
952 VLC_TYPE(*vlc_table)[2] = table->table;
955 av_log(s->avctx, AV_LOG_ERROR,
956 "Invalid number of coefficients at level %d\n", coeff_index);
958 if (eob_run > num_coeffs) {
960 blocks_ended = num_coeffs;
961 eob_run -= num_coeffs;
964 blocks_ended = eob_run;
968 // insert fake EOB token to cover the split between planes or zzi
970 dct_tokens[j++] = blocks_ended << 2;
972 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
973 /* decode a VLC into a token */
974 token = get_vlc2(gb, vlc_table, 11, 3);
975 /* use the token to get a zero run, a coefficient, and an eob run */
976 if ((unsigned) token <= 6U) {
977 eob_run = eob_run_base[token];
978 if (eob_run_get_bits[token])
979 eob_run += get_bits(gb, eob_run_get_bits[token]);
981 // record only the number of blocks ended in this plane,
982 // any spill will be recorded in the next plane.
983 if (eob_run > num_coeffs - coeff_i) {
984 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
985 blocks_ended += num_coeffs - coeff_i;
986 eob_run -= num_coeffs - coeff_i;
987 coeff_i = num_coeffs;
989 dct_tokens[j++] = TOKEN_EOB(eob_run);
990 blocks_ended += eob_run;
994 } else if (token >= 0) {
995 bits_to_get = coeff_get_bits[token];
997 bits_to_get = get_bits(gb, bits_to_get);
998 coeff = coeff_tables[token][bits_to_get];
1000 zero_run = zero_run_base[token];
1001 if (zero_run_get_bits[token])
1002 zero_run += get_bits(gb, zero_run_get_bits[token]);
1005 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1007 // Save DC into the fragment structure. DC prediction is
1008 // done in raster order, so the actual DC can't be in with
1009 // other tokens. We still need the token in dct_tokens[]
1010 // however, or else the structure collapses on itself.
1012 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1014 dct_tokens[j++] = TOKEN_COEFF(coeff);
1017 if (coeff_index + zero_run > 64) {
1018 av_log(s->avctx, AV_LOG_DEBUG,
1019 "Invalid zero run of %d with %d coeffs left\n",
1020 zero_run, 64 - coeff_index);
1021 zero_run = 64 - coeff_index;
1024 // zero runs code multiple coefficients,
1025 // so don't try to decode coeffs for those higher levels
1026 for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1027 s->num_coded_frags[plane][i]--;
1030 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1035 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1036 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1038 // decrement the number of blocks that have higher coefficients for each
1039 // EOB run at this level
1041 for (i = coeff_index + 1; i < 64; i++)
1042 s->num_coded_frags[plane][i] -= blocks_ended;
1044 // setup the next buffer
1046 s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1047 else if (coeff_index < 63)
1048 s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1053 static void reverse_dc_prediction(Vp3DecodeContext *s,
1056 int fragment_height);
1058 * This function unpacks all of the DCT coefficient data from the
1061 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1068 int residual_eob_run = 0;
1072 s->dct_tokens[0][0] = s->dct_tokens_base;
1074 /* fetch the DC table indexes */
1075 dc_y_table = get_bits(gb, 4);
1076 dc_c_table = get_bits(gb, 4);
1078 /* unpack the Y plane DC coefficients */
1079 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1080 0, residual_eob_run);
1081 if (residual_eob_run < 0)
1082 return residual_eob_run;
1084 /* reverse prediction of the Y-plane DC coefficients */
1085 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1087 /* unpack the C plane DC coefficients */
1088 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1089 1, residual_eob_run);
1090 if (residual_eob_run < 0)
1091 return residual_eob_run;
1092 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1093 2, residual_eob_run);
1094 if (residual_eob_run < 0)
1095 return residual_eob_run;
1097 /* reverse prediction of the C-plane DC coefficients */
1098 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1099 reverse_dc_prediction(s, s->fragment_start[1],
1100 s->fragment_width[1], s->fragment_height[1]);
1101 reverse_dc_prediction(s, s->fragment_start[2],
1102 s->fragment_width[1], s->fragment_height[1]);
1105 /* fetch the AC table indexes */
1106 ac_y_table = get_bits(gb, 4);
1107 ac_c_table = get_bits(gb, 4);
1109 /* build tables of AC VLC tables */
1110 for (i = 1; i <= 5; i++) {
1111 y_tables[i] = &s->ac_vlc_1[ac_y_table];
1112 c_tables[i] = &s->ac_vlc_1[ac_c_table];
1114 for (i = 6; i <= 14; i++) {
1115 y_tables[i] = &s->ac_vlc_2[ac_y_table];
1116 c_tables[i] = &s->ac_vlc_2[ac_c_table];
1118 for (i = 15; i <= 27; i++) {
1119 y_tables[i] = &s->ac_vlc_3[ac_y_table];
1120 c_tables[i] = &s->ac_vlc_3[ac_c_table];
1122 for (i = 28; i <= 63; i++) {
1123 y_tables[i] = &s->ac_vlc_4[ac_y_table];
1124 c_tables[i] = &s->ac_vlc_4[ac_c_table];
1127 /* decode all AC coefficients */
1128 for (i = 1; i <= 63; i++) {
1129 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1130 0, residual_eob_run);
1131 if (residual_eob_run < 0)
1132 return residual_eob_run;
1134 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1135 1, residual_eob_run);
1136 if (residual_eob_run < 0)
1137 return residual_eob_run;
1138 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1139 2, residual_eob_run);
1140 if (residual_eob_run < 0)
1141 return residual_eob_run;
1148 * This function reverses the DC prediction for each coded fragment in
1149 * the frame. Much of this function is adapted directly from the original
1152 #define COMPATIBLE_FRAME(x) \
1153 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1154 #define DC_COEFF(u) s->all_fragments[u].dc
1156 static void reverse_dc_prediction(Vp3DecodeContext *s,
1159 int fragment_height)
1167 int i = first_fragment;
1171 /* DC values for the left, up-left, up, and up-right fragments */
1172 int vl, vul, vu, vur;
1174 /* indexes for the left, up-left, up, and up-right fragments */
1178 * The 6 fields mean:
1179 * 0: up-left multiplier
1181 * 2: up-right multiplier
1182 * 3: left multiplier
1184 static const int predictor_transform[16][4] = {
1186 { 0, 0, 0, 128 }, // PL
1187 { 0, 0, 128, 0 }, // PUR
1188 { 0, 0, 53, 75 }, // PUR|PL
1189 { 0, 128, 0, 0 }, // PU
1190 { 0, 64, 0, 64 }, // PU |PL
1191 { 0, 128, 0, 0 }, // PU |PUR
1192 { 0, 0, 53, 75 }, // PU |PUR|PL
1193 { 128, 0, 0, 0 }, // PUL
1194 { 0, 0, 0, 128 }, // PUL|PL
1195 { 64, 0, 64, 0 }, // PUL|PUR
1196 { 0, 0, 53, 75 }, // PUL|PUR|PL
1197 { 0, 128, 0, 0 }, // PUL|PU
1198 { -104, 116, 0, 116 }, // PUL|PU |PL
1199 { 24, 80, 24, 0 }, // PUL|PU |PUR
1200 { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1203 /* This table shows which types of blocks can use other blocks for
1204 * prediction. For example, INTRA is the only mode in this table to
1205 * have a frame number of 0. That means INTRA blocks can only predict
1206 * from other INTRA blocks. There are 2 golden frame coding types;
1207 * blocks encoding in these modes can only predict from other blocks
1208 * that were encoded with these 1 of these 2 modes. */
1209 static const unsigned char compatible_frame[9] = {
1210 1, /* MODE_INTER_NO_MV */
1212 1, /* MODE_INTER_PLUS_MV */
1213 1, /* MODE_INTER_LAST_MV */
1214 1, /* MODE_INTER_PRIOR_MV */
1215 2, /* MODE_USING_GOLDEN */
1216 2, /* MODE_GOLDEN_MV */
1217 1, /* MODE_INTER_FOUR_MV */
1220 int current_frame_type;
1222 /* there is a last DC predictor for each of the 3 frame types */
1235 /* for each fragment row... */
1236 for (y = 0; y < fragment_height; y++) {
1237 /* for each fragment in a row... */
1238 for (x = 0; x < fragment_width; x++, i++) {
1240 /* reverse prediction if this block was coded */
1241 if (s->all_fragments[i].coding_method != MODE_COPY) {
1242 current_frame_type =
1243 compatible_frame[s->all_fragments[i].coding_method];
1249 if (COMPATIBLE_FRAME(l))
1253 u = i - fragment_width;
1255 if (COMPATIBLE_FRAME(u))
1258 ul = i - fragment_width - 1;
1260 if (COMPATIBLE_FRAME(ul))
1263 if (x + 1 < fragment_width) {
1264 ur = i - fragment_width + 1;
1266 if (COMPATIBLE_FRAME(ur))
1271 if (transform == 0) {
1272 /* if there were no fragments to predict from, use last
1274 predicted_dc = last_dc[current_frame_type];
1276 /* apply the appropriate predictor transform */
1278 (predictor_transform[transform][0] * vul) +
1279 (predictor_transform[transform][1] * vu) +
1280 (predictor_transform[transform][2] * vur) +
1281 (predictor_transform[transform][3] * vl);
1283 predicted_dc /= 128;
1285 /* check for outranging on the [ul u l] and
1286 * [ul u ur l] predictors */
1287 if ((transform == 15) || (transform == 13)) {
1288 if (FFABS(predicted_dc - vu) > 128)
1290 else if (FFABS(predicted_dc - vl) > 128)
1292 else if (FFABS(predicted_dc - vul) > 128)
1297 /* at long last, apply the predictor */
1298 DC_COEFF(i) += predicted_dc;
1300 last_dc[current_frame_type] = DC_COEFF(i);
1306 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1307 int ystart, int yend)
1310 int *bounding_values = s->bounding_values_array + 127;
1312 int width = s->fragment_width[!!plane];
1313 int height = s->fragment_height[!!plane];
1314 int fragment = s->fragment_start[plane] + ystart * width;
1315 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1316 uint8_t *plane_data = s->current_frame.f->data[plane];
1317 if (!s->flipped_image)
1319 plane_data += s->data_offset[plane] + 8 * ystart * stride;
1321 for (y = ystart; y < yend; y++) {
1322 for (x = 0; x < width; x++) {
1323 /* This code basically just deblocks on the edges of coded blocks.
1324 * However, it has to be much more complicated because of the
1325 * brain damaged deblock ordering used in VP3/Theora. Order matters
1326 * because some pixels get filtered twice. */
1327 if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1328 /* do not perform left edge filter for left columns frags */
1330 s->vp3dsp.h_loop_filter(
1332 stride, bounding_values);
1335 /* do not perform top edge filter for top row fragments */
1337 s->vp3dsp.v_loop_filter(
1339 stride, bounding_values);
1342 /* do not perform right edge filter for right column
1343 * fragments or if right fragment neighbor is also coded
1344 * in this frame (it will be filtered in next iteration) */
1345 if ((x < width - 1) &&
1346 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1347 s->vp3dsp.h_loop_filter(
1348 plane_data + 8 * x + 8,
1349 stride, bounding_values);
1352 /* do not perform bottom edge filter for bottom row
1353 * fragments or if bottom fragment neighbor is also coded
1354 * in this frame (it will be filtered in the next row) */
1355 if ((y < height - 1) &&
1356 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1357 s->vp3dsp.v_loop_filter(
1358 plane_data + 8 * x + 8 * stride,
1359 stride, bounding_values);
1365 plane_data += 8 * stride;
1370 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1371 * for the next block in coding order
1373 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1374 int plane, int inter, int16_t block[64])
1376 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1377 uint8_t *perm = s->idct_scantable;
1381 int token = *s->dct_tokens[plane][i];
1382 switch (token & 3) {
1384 if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1385 s->dct_tokens[plane][i]++;
1387 *s->dct_tokens[plane][i] = token & ~3;
1390 s->dct_tokens[plane][i]++;
1391 i += (token >> 2) & 0x7f;
1393 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1396 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1400 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1401 s->dct_tokens[plane][i++]++;
1403 default: // shouldn't happen
1407 // return value is expected to be a valid level
1410 // the actual DC+prediction is in the fragment structure
1411 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1416 * called when all pixels up to row y are complete
1418 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1421 int offset[AV_NUM_DATA_POINTERS];
1423 if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1424 int y_flipped = s->flipped_image ? s->height - y : y;
1426 /* At the end of the frame, report INT_MAX instead of the height of
1427 * the frame. This makes the other threads' ff_thread_await_progress()
1428 * calls cheaper, because they don't have to clip their values. */
1429 ff_thread_report_progress(&s->current_frame,
1430 y_flipped == s->height ? INT_MAX
1435 if (!s->avctx->draw_horiz_band)
1438 h = y - s->last_slice_end;
1439 s->last_slice_end = y;
1442 if (!s->flipped_image)
1443 y = s->height - y - h;
1445 cy = y >> s->chroma_y_shift;
1446 offset[0] = s->current_frame.f->linesize[0] * y;
1447 offset[1] = s->current_frame.f->linesize[1] * cy;
1448 offset[2] = s->current_frame.f->linesize[2] * cy;
1449 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1453 s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1457 * Wait for the reference frame of the current fragment.
1458 * The progress value is in luma pixel rows.
1460 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment,
1461 int motion_y, int y)
1463 ThreadFrame *ref_frame;
1465 int border = motion_y & 1;
1467 if (fragment->coding_method == MODE_USING_GOLDEN ||
1468 fragment->coding_method == MODE_GOLDEN_MV)
1469 ref_frame = &s->golden_frame;
1471 ref_frame = &s->last_frame;
1473 ref_row = y + (motion_y >> 1);
1474 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1476 ff_thread_await_progress(ref_frame, ref_row, 0);
1480 * Perform the final rendering for a particular slice of data.
1481 * The slice number ranges from 0..(c_superblock_height - 1).
1483 static void render_slice(Vp3DecodeContext *s, int slice)
1485 int x, y, i, j, fragment;
1486 int16_t *block = s->block;
1487 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1488 int motion_halfpel_index;
1489 uint8_t *motion_source;
1490 int plane, first_pixel;
1492 if (slice >= s->c_superblock_height)
1495 for (plane = 0; plane < 3; plane++) {
1496 uint8_t *output_plane = s->current_frame.f->data[plane] +
1497 s->data_offset[plane];
1498 uint8_t *last_plane = s->last_frame.f->data[plane] +
1499 s->data_offset[plane];
1500 uint8_t *golden_plane = s->golden_frame.f->data[plane] +
1501 s->data_offset[plane];
1502 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1503 int plane_width = s->width >> (plane && s->chroma_x_shift);
1504 int plane_height = s->height >> (plane && s->chroma_y_shift);
1505 int8_t(*motion_val)[2] = s->motion_val[!!plane];
1507 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1508 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1509 int slice_width = plane ? s->c_superblock_width
1510 : s->y_superblock_width;
1512 int fragment_width = s->fragment_width[!!plane];
1513 int fragment_height = s->fragment_height[!!plane];
1514 int fragment_start = s->fragment_start[plane];
1516 int do_await = !plane && HAVE_THREADS &&
1517 (s->avctx->active_thread_type & FF_THREAD_FRAME);
1519 if (!s->flipped_image)
1521 if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1524 /* for each superblock row in the slice (both of them)... */
1525 for (; sb_y < slice_height; sb_y++) {
1526 /* for each superblock in a row... */
1527 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1528 /* for each block in a superblock... */
1529 for (j = 0; j < 16; j++) {
1530 x = 4 * sb_x + hilbert_offset[j][0];
1531 y = 4 * sb_y + hilbert_offset[j][1];
1532 fragment = y * fragment_width + x;
1534 i = fragment_start + fragment;
1537 if (x >= fragment_width || y >= fragment_height)
1540 first_pixel = 8 * y * stride + 8 * x;
1543 s->all_fragments[i].coding_method != MODE_INTRA)
1544 await_reference_row(s, &s->all_fragments[i],
1545 motion_val[fragment][1],
1546 (16 * y) >> s->chroma_y_shift);
1548 /* transform if this block was coded */
1549 if (s->all_fragments[i].coding_method != MODE_COPY) {
1550 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1551 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1552 motion_source = golden_plane;
1554 motion_source = last_plane;
1556 motion_source += first_pixel;
1557 motion_halfpel_index = 0;
1559 /* sort out the motion vector if this fragment is coded
1560 * using a motion vector method */
1561 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1562 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1564 motion_x = motion_val[fragment][0];
1565 motion_y = motion_val[fragment][1];
1567 src_x = (motion_x >> 1) + 8 * x;
1568 src_y = (motion_y >> 1) + 8 * y;
1570 motion_halfpel_index = motion_x & 0x01;
1571 motion_source += (motion_x >> 1);
1573 motion_halfpel_index |= (motion_y & 0x01) << 1;
1574 motion_source += ((motion_y >> 1) * stride);
1576 if (src_x < 0 || src_y < 0 ||
1577 src_x + 9 >= plane_width ||
1578 src_y + 9 >= plane_height) {
1579 uint8_t *temp = s->edge_emu_buffer;
1583 s->vdsp.emulated_edge_mc(temp, motion_source,
1588 motion_source = temp;
1592 /* first, take care of copying a block from either the
1593 * previous or the golden frame */
1594 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1595 /* Note, it is possible to implement all MC cases
1596 * with put_no_rnd_pixels_l2 which would look more
1597 * like the VP3 source but this would be slower as
1598 * put_no_rnd_pixels_tab is better optimized */
1599 if (motion_halfpel_index != 3) {
1600 s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1601 output_plane + first_pixel,
1602 motion_source, stride, 8);
1604 /* d is 0 if motion_x and _y have the same sign,
1606 int d = (motion_x ^ motion_y) >> 31;
1607 s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
1609 motion_source + stride + 1 + d,
1614 /* invert DCT and place (or add) in final output */
1616 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1617 vp3_dequant(s, s->all_fragments + i,
1619 s->vp3dsp.idct_put(output_plane + first_pixel,
1623 if (vp3_dequant(s, s->all_fragments + i,
1625 s->vp3dsp.idct_add(output_plane + first_pixel,
1629 s->vp3dsp.idct_dc_add(output_plane + first_pixel,
1634 /* copy directly from the previous frame */
1635 s->hdsp.put_pixels_tab[1][0](
1636 output_plane + first_pixel,
1637 last_plane + first_pixel,
1643 // Filter up to the last row in the superblock row
1644 if (!s->skip_loop_filter)
1645 apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
1646 FFMIN(4 * sb_y + 3, fragment_height - 1));
1650 /* this looks like a good place for slice dispatch... */
1652 * if (slice == s->macroblock_height - 1)
1653 * dispatch (both last slice & 2nd-to-last slice);
1654 * else if (slice > 0)
1655 * dispatch (slice - 1);
1658 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
1662 /// Allocate tables for per-frame data in Vp3DecodeContext
1663 static av_cold int allocate_tables(AVCodecContext *avctx)
1665 Vp3DecodeContext *s = avctx->priv_data;
1666 int y_fragment_count, c_fragment_count;
1670 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1671 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1673 s->superblock_coding = av_mallocz(s->superblock_count);
1674 s->all_fragments = av_mallocz_array(s->fragment_count, sizeof(Vp3Fragment));
1676 s->coded_fragment_list[0] = av_mallocz_array(s->fragment_count, sizeof(int));
1678 s->dct_tokens_base = av_mallocz_array(s->fragment_count,
1679 64 * sizeof(*s->dct_tokens_base));
1680 s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
1681 s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
1683 /* work out the block mapping tables */
1684 s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
1685 s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
1687 if (!s->superblock_coding || !s->all_fragments ||
1688 !s->dct_tokens_base || !s->coded_fragment_list[0] ||
1689 !s->superblock_fragments || !s->macroblock_coding ||
1690 !s->motion_val[0] || !s->motion_val[1]) {
1691 vp3_decode_end(avctx);
1695 init_block_mapping(s);
1700 static av_cold int init_frames(Vp3DecodeContext *s)
1702 s->current_frame.f = av_frame_alloc();
1703 s->last_frame.f = av_frame_alloc();
1704 s->golden_frame.f = av_frame_alloc();
1706 if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
1707 av_frame_free(&s->current_frame.f);
1708 av_frame_free(&s->last_frame.f);
1709 av_frame_free(&s->golden_frame.f);
1710 return AVERROR(ENOMEM);
1716 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1718 Vp3DecodeContext *s = avctx->priv_data;
1719 int i, inter, plane, ret;
1722 int y_fragment_count, c_fragment_count;
1724 ret = init_frames(s);
1728 avctx->internal->allocate_progress = 1;
1730 if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
1736 s->width = FFALIGN(avctx->coded_width, 16);
1737 s->height = FFALIGN(avctx->coded_height, 16);
1738 if (avctx->codec_id != AV_CODEC_ID_THEORA)
1739 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1740 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1741 ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
1742 ff_videodsp_init(&s->vdsp, 8);
1743 ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
1745 for (i = 0; i < 64; i++) {
1746 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
1747 s->idct_permutation[i] = TRANSPOSE(i);
1748 s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
1752 /* initialize to an impossible value which will force a recalculation
1753 * in the first frame decode */
1754 for (i = 0; i < 3; i++)
1757 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1759 s->y_superblock_width = (s->width + 31) / 32;
1760 s->y_superblock_height = (s->height + 31) / 32;
1761 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1763 /* work out the dimensions for the C planes */
1764 c_width = s->width >> s->chroma_x_shift;
1765 c_height = s->height >> s->chroma_y_shift;
1766 s->c_superblock_width = (c_width + 31) / 32;
1767 s->c_superblock_height = (c_height + 31) / 32;
1768 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1770 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1771 s->u_superblock_start = s->y_superblock_count;
1772 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1774 s->macroblock_width = (s->width + 15) / 16;
1775 s->macroblock_height = (s->height + 15) / 16;
1776 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1778 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1779 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1780 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
1781 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1783 /* fragment count covers all 8x8 blocks for all 3 planes */
1784 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1785 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1786 s->fragment_count = y_fragment_count + 2 * c_fragment_count;
1787 s->fragment_start[1] = y_fragment_count;
1788 s->fragment_start[2] = y_fragment_count + c_fragment_count;
1790 if (!s->theora_tables) {
1791 for (i = 0; i < 64; i++) {
1792 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1793 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1794 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1795 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1796 s->base_matrix[2][i] = vp31_inter_dequant[i];
1797 s->filter_limit_values[i] = vp31_filter_limit_values[i];
1800 for (inter = 0; inter < 2; inter++) {
1801 for (plane = 0; plane < 3; plane++) {
1802 s->qr_count[inter][plane] = 1;
1803 s->qr_size[inter][plane][0] = 63;
1804 s->qr_base[inter][plane][0] =
1805 s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1809 /* init VLC tables */
1810 for (i = 0; i < 16; i++) {
1812 init_vlc(&s->dc_vlc[i], 11, 32,
1813 &dc_bias[i][0][1], 4, 2,
1814 &dc_bias[i][0][0], 4, 2, 0);
1816 /* group 1 AC histograms */
1817 init_vlc(&s->ac_vlc_1[i], 11, 32,
1818 &ac_bias_0[i][0][1], 4, 2,
1819 &ac_bias_0[i][0][0], 4, 2, 0);
1821 /* group 2 AC histograms */
1822 init_vlc(&s->ac_vlc_2[i], 11, 32,
1823 &ac_bias_1[i][0][1], 4, 2,
1824 &ac_bias_1[i][0][0], 4, 2, 0);
1826 /* group 3 AC histograms */
1827 init_vlc(&s->ac_vlc_3[i], 11, 32,
1828 &ac_bias_2[i][0][1], 4, 2,
1829 &ac_bias_2[i][0][0], 4, 2, 0);
1831 /* group 4 AC histograms */
1832 init_vlc(&s->ac_vlc_4[i], 11, 32,
1833 &ac_bias_3[i][0][1], 4, 2,
1834 &ac_bias_3[i][0][0], 4, 2, 0);
1837 for (i = 0; i < 16; i++) {
1839 if (init_vlc(&s->dc_vlc[i], 11, 32,
1840 &s->huffman_table[i][0][1], 8, 4,
1841 &s->huffman_table[i][0][0], 8, 4, 0) < 0)
1844 /* group 1 AC histograms */
1845 if (init_vlc(&s->ac_vlc_1[i], 11, 32,
1846 &s->huffman_table[i + 16][0][1], 8, 4,
1847 &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
1850 /* group 2 AC histograms */
1851 if (init_vlc(&s->ac_vlc_2[i], 11, 32,
1852 &s->huffman_table[i + 16 * 2][0][1], 8, 4,
1853 &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
1856 /* group 3 AC histograms */
1857 if (init_vlc(&s->ac_vlc_3[i], 11, 32,
1858 &s->huffman_table[i + 16 * 3][0][1], 8, 4,
1859 &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
1862 /* group 4 AC histograms */
1863 if (init_vlc(&s->ac_vlc_4[i], 11, 32,
1864 &s->huffman_table[i + 16 * 4][0][1], 8, 4,
1865 &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
1870 init_vlc(&s->superblock_run_length_vlc, 6, 34,
1871 &superblock_run_length_vlc_table[0][1], 4, 2,
1872 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1874 init_vlc(&s->fragment_run_length_vlc, 5, 30,
1875 &fragment_run_length_vlc_table[0][1], 4, 2,
1876 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1878 init_vlc(&s->mode_code_vlc, 3, 8,
1879 &mode_code_vlc_table[0][1], 2, 1,
1880 &mode_code_vlc_table[0][0], 2, 1, 0);
1882 init_vlc(&s->motion_vector_vlc, 6, 63,
1883 &motion_vector_vlc_table[0][1], 2, 1,
1884 &motion_vector_vlc_table[0][0], 2, 1, 0);
1886 return allocate_tables(avctx);
1889 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1893 /// Release and shuffle frames after decode finishes
1894 static int update_frames(AVCodecContext *avctx)
1896 Vp3DecodeContext *s = avctx->priv_data;
1899 /* shuffle frames (last = current) */
1900 ff_thread_release_buffer(avctx, &s->last_frame);
1901 ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
1906 ff_thread_release_buffer(avctx, &s->golden_frame);
1907 ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
1911 ff_thread_release_buffer(avctx, &s->current_frame);
1915 static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
1917 ff_thread_release_buffer(s->avctx, dst);
1918 if (src->f->data[0])
1919 return ff_thread_ref_frame(dst, src);
1923 static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
1926 if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
1927 (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
1928 (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
1934 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1936 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1937 int qps_changed = 0, i, err;
1939 #define copy_fields(to, from, start_field, end_field) \
1940 memcpy(&to->start_field, &from->start_field, \
1941 (char *) &to->end_field - (char *) &to->start_field)
1943 if (!s1->current_frame.f->data[0] ||
1944 s->width != s1->width || s->height != s1->height) {
1951 if (!s->current_frame.f)
1952 return AVERROR(ENOMEM);
1953 // init tables if the first frame hasn't been decoded
1954 if (!s->current_frame.f->data[0]) {
1955 int y_fragment_count, c_fragment_count;
1957 err = allocate_tables(dst);
1960 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1961 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1962 memcpy(s->motion_val[0], s1->motion_val[0],
1963 y_fragment_count * sizeof(*s->motion_val[0]));
1964 memcpy(s->motion_val[1], s1->motion_val[1],
1965 c_fragment_count * sizeof(*s->motion_val[1]));
1968 // copy previous frame data
1969 if ((err = ref_frames(s, s1)) < 0)
1972 s->keyframe = s1->keyframe;
1974 // copy qscale data if necessary
1975 for (i = 0; i < 3; i++) {
1976 if (s->qps[i] != s1->qps[1]) {
1978 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1982 if (s->qps[0] != s1->qps[0])
1983 memcpy(&s->bounding_values_array, &s1->bounding_values_array,
1984 sizeof(s->bounding_values_array));
1987 copy_fields(s, s1, qps, superblock_count);
1991 return update_frames(dst);
1995 static int vp3_decode_frame(AVCodecContext *avctx,
1996 void *data, int *got_frame,
1999 const uint8_t *buf = avpkt->data;
2000 int buf_size = avpkt->size;
2001 Vp3DecodeContext *s = avctx->priv_data;
2005 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
2008 #if CONFIG_THEORA_DECODER
2009 if (s->theora && get_bits1(&gb)) {
2010 int type = get_bits(&gb, 7);
2011 skip_bits_long(&gb, 6*8); /* "theora" */
2013 if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2014 av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2015 return AVERROR_PATCHWELCOME;
2018 vp3_decode_end(avctx);
2019 ret = theora_decode_header(avctx, &gb);
2022 ret = vp3_decode_init(avctx);
2024 vp3_decode_end(avctx);
2028 } else if (type == 2) {
2029 vp3_decode_end(avctx);
2030 ret = theora_decode_tables(avctx, &gb);
2032 ret = vp3_decode_init(avctx);
2034 vp3_decode_end(avctx);
2040 av_log(avctx, AV_LOG_ERROR,
2041 "Header packet passed to frame decoder, skipping\n");
2046 s->keyframe = !get_bits1(&gb);
2047 if (!s->all_fragments) {
2048 av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2053 for (i = 0; i < 3; i++)
2054 s->last_qps[i] = s->qps[i];
2058 s->qps[s->nqps++] = get_bits(&gb, 6);
2059 } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2060 for (i = s->nqps; i < 3; i++)
2063 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2064 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2065 s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2067 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2068 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2069 : AVDISCARD_NONKEY);
2071 if (s->qps[0] != s->last_qps[0])
2072 init_loop_filter(s);
2074 for (i = 0; i < s->nqps; i++)
2075 // reinit all dequantizers if the first one changed, because
2076 // the DC of the first quantizer must be used for all matrices
2077 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2078 init_dequantizer(s, i);
2080 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2083 s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2084 : AV_PICTURE_TYPE_P;
2085 s->current_frame.f->key_frame = s->keyframe;
2086 if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0)
2089 if (!s->edge_emu_buffer)
2090 s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2094 skip_bits(&gb, 4); /* width code */
2095 skip_bits(&gb, 4); /* height code */
2097 s->version = get_bits(&gb, 5);
2098 if (avctx->frame_number == 0)
2099 av_log(s->avctx, AV_LOG_DEBUG,
2100 "VP version: %d\n", s->version);
2103 if (s->version || s->theora) {
2105 av_log(s->avctx, AV_LOG_ERROR,
2106 "Warning, unsupported keyframe coding type?!\n");
2107 skip_bits(&gb, 2); /* reserved? */
2110 if (!s->golden_frame.f->data[0]) {
2111 av_log(s->avctx, AV_LOG_WARNING,
2112 "vp3: first frame not a keyframe\n");
2114 s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2115 if (ff_thread_get_buffer(avctx, &s->golden_frame,
2116 AV_GET_BUFFER_FLAG_REF) < 0)
2118 ff_thread_release_buffer(avctx, &s->last_frame);
2119 if ((ret = ff_thread_ref_frame(&s->last_frame,
2120 &s->golden_frame)) < 0)
2122 ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2126 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2127 ff_thread_finish_setup(avctx);
2129 if (unpack_superblocks(s, &gb)) {
2130 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2133 if (unpack_modes(s, &gb)) {
2134 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2137 if (unpack_vectors(s, &gb)) {
2138 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2141 if (unpack_block_qpis(s, &gb)) {
2142 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2145 if (unpack_dct_coeffs(s, &gb)) {
2146 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2150 for (i = 0; i < 3; i++) {
2151 int height = s->height >> (i && s->chroma_y_shift);
2152 if (s->flipped_image)
2153 s->data_offset[i] = 0;
2155 s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2158 s->last_slice_end = 0;
2159 for (i = 0; i < s->c_superblock_height; i++)
2162 // filter the last row
2163 for (i = 0; i < 3; i++) {
2164 int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2165 apply_loop_filter(s, i, row, row + 1);
2167 vp3_draw_horiz_band(s, s->height);
2169 /* output frame, offset as needed */
2170 if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2172 for (i = 0; i < 3; i++) {
2173 AVFrame *dst = data;
2174 int off = (s->offset_x >> (i && s->chroma_y_shift)) +
2175 (s->offset_y >> (i && s->chroma_y_shift)) * dst->linesize[i];
2176 dst->data[i] += off;
2180 if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2181 ret = update_frames(avctx);
2189 ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2191 if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2192 av_frame_unref(s->current_frame.f);
2197 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2199 Vp3DecodeContext *s = avctx->priv_data;
2201 if (get_bits1(gb)) {
2203 if (s->entries >= 32) { /* overflow */
2204 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2207 token = get_bits(gb, 5);
2208 ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2209 s->hti, s->hbits, token, s->entries, s->huff_code_size);
2210 s->huffman_table[s->hti][token][0] = s->hbits;
2211 s->huffman_table[s->hti][token][1] = s->huff_code_size;
2214 if (s->huff_code_size >= 32) { /* overflow */
2215 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2218 s->huff_code_size++;
2220 if (read_huffman_tree(avctx, gb))
2223 if (read_huffman_tree(avctx, gb))
2226 s->huff_code_size--;
2232 static int vp3_init_thread_copy(AVCodecContext *avctx)
2234 Vp3DecodeContext *s = avctx->priv_data;
2236 s->superblock_coding = NULL;
2237 s->all_fragments = NULL;
2238 s->coded_fragment_list[0] = NULL;
2239 s->dct_tokens_base = NULL;
2240 s->superblock_fragments = NULL;
2241 s->macroblock_coding = NULL;
2242 s->motion_val[0] = NULL;
2243 s->motion_val[1] = NULL;
2244 s->edge_emu_buffer = NULL;
2246 return init_frames(s);
2250 #if CONFIG_THEORA_DECODER
2251 static const enum AVPixelFormat theora_pix_fmts[4] = {
2252 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2255 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2257 Vp3DecodeContext *s = avctx->priv_data;
2258 int visible_width, visible_height, colorspace;
2259 uint8_t offset_x = 0, offset_y = 0;
2261 AVRational fps, aspect;
2263 s->theora_header = 0;
2264 s->theora = get_bits_long(gb, 24);
2265 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2267 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2268 * but previous versions have the image flipped relative to vp3 */
2269 if (s->theora < 0x030200) {
2270 s->flipped_image = 1;
2271 av_log(avctx, AV_LOG_DEBUG,
2272 "Old (<alpha3) Theora bitstream, flipped image\n");
2276 s->width = get_bits(gb, 16) << 4;
2278 s->height = get_bits(gb, 16) << 4;
2280 if (s->theora >= 0x030200) {
2281 visible_width = get_bits_long(gb, 24);
2282 visible_height = get_bits_long(gb, 24);
2284 offset_x = get_bits(gb, 8); /* offset x */
2285 offset_y = get_bits(gb, 8); /* offset y, from bottom */
2289 if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
2290 visible_width + offset_x > s->width ||
2291 visible_height + offset_y > s->height) {
2292 av_log(avctx, AV_LOG_ERROR,
2293 "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2294 visible_width, visible_height, offset_x, offset_y,
2295 s->width, s->height);
2296 return AVERROR_INVALIDDATA;
2299 fps.num = get_bits_long(gb, 32);
2300 fps.den = get_bits_long(gb, 32);
2301 if (fps.num && fps.den) {
2302 if (fps.num < 0 || fps.den < 0) {
2303 av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2304 return AVERROR_INVALIDDATA;
2306 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
2307 fps.den, fps.num, 1 << 30);
2310 aspect.num = get_bits_long(gb, 24);
2311 aspect.den = get_bits_long(gb, 24);
2312 if (aspect.num && aspect.den) {
2313 av_reduce(&avctx->sample_aspect_ratio.num,
2314 &avctx->sample_aspect_ratio.den,
2315 aspect.num, aspect.den, 1 << 30);
2316 ff_set_sar(avctx, avctx->sample_aspect_ratio);
2319 if (s->theora < 0x030200)
2320 skip_bits(gb, 5); /* keyframe frequency force */
2321 colorspace = get_bits(gb, 8);
2322 skip_bits(gb, 24); /* bitrate */
2324 skip_bits(gb, 6); /* quality hint */
2326 if (s->theora >= 0x030200) {
2327 skip_bits(gb, 5); /* keyframe frequency force */
2328 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2329 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2330 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2331 return AVERROR_INVALIDDATA;
2333 skip_bits(gb, 3); /* reserved */
2335 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2337 ret = ff_set_dimensions(avctx, s->width, s->height);
2340 if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
2341 avctx->width = visible_width;
2342 avctx->height = visible_height;
2343 // translate offsets from theora axis ([0,0] lower left)
2344 // to normal axis ([0,0] upper left)
2345 s->offset_x = offset_x;
2346 s->offset_y = s->height - visible_height - offset_y;
2348 if ((s->offset_x & 0x1F) && !(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
2349 s->offset_x &= ~0x1F;
2350 if (!s->offset_x_warned) {
2351 s->offset_x_warned = 1;
2352 av_log(avctx, AV_LOG_WARNING, "Reducing offset_x from %d to %d"
2353 "chroma samples to preserve alignment.\n",
2354 offset_x, s->offset_x);
2359 if (colorspace == 1)
2360 avctx->color_primaries = AVCOL_PRI_BT470M;
2361 else if (colorspace == 2)
2362 avctx->color_primaries = AVCOL_PRI_BT470BG;
2364 if (colorspace == 1 || colorspace == 2) {
2365 avctx->colorspace = AVCOL_SPC_BT470BG;
2366 avctx->color_trc = AVCOL_TRC_BT709;
2369 s->theora_header = 1;
2373 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2375 Vp3DecodeContext *s = avctx->priv_data;
2376 int i, n, matrices, inter, plane;
2378 if (!s->theora_header)
2379 return AVERROR_INVALIDDATA;
2381 if (s->theora >= 0x030200) {
2382 n = get_bits(gb, 3);
2383 /* loop filter limit values table */
2385 for (i = 0; i < 64; i++)
2386 s->filter_limit_values[i] = get_bits(gb, n);
2389 if (s->theora >= 0x030200)
2390 n = get_bits(gb, 4) + 1;
2393 /* quality threshold table */
2394 for (i = 0; i < 64; i++)
2395 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2397 if (s->theora >= 0x030200)
2398 n = get_bits(gb, 4) + 1;
2401 /* dc scale factor table */
2402 for (i = 0; i < 64; i++)
2403 s->coded_dc_scale_factor[i] = get_bits(gb, n);
2405 if (s->theora >= 0x030200)
2406 matrices = get_bits(gb, 9) + 1;
2410 if (matrices > 384) {
2411 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2415 for (n = 0; n < matrices; n++)
2416 for (i = 0; i < 64; i++)
2417 s->base_matrix[n][i] = get_bits(gb, 8);
2419 for (inter = 0; inter <= 1; inter++) {
2420 for (plane = 0; plane <= 2; plane++) {
2422 if (inter || plane > 0)
2423 newqr = get_bits1(gb);
2426 if (inter && get_bits1(gb)) {
2430 qtj = (3 * inter + plane - 1) / 3;
2431 plj = (plane + 2) % 3;
2433 s->qr_count[inter][plane] = s->qr_count[qtj][plj];
2434 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
2435 sizeof(s->qr_size[0][0]));
2436 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
2437 sizeof(s->qr_base[0][0]));
2443 i = get_bits(gb, av_log2(matrices - 1) + 1);
2444 if (i >= matrices) {
2445 av_log(avctx, AV_LOG_ERROR,
2446 "invalid base matrix index\n");
2449 s->qr_base[inter][plane][qri] = i;
2452 i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
2453 s->qr_size[inter][plane][qri++] = i;
2458 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2461 s->qr_count[inter][plane] = qri;
2466 /* Huffman tables */
2467 for (s->hti = 0; s->hti < 80; s->hti++) {
2469 s->huff_code_size = 1;
2470 if (!get_bits1(gb)) {
2472 if (read_huffman_tree(avctx, gb))
2475 if (read_huffman_tree(avctx, gb))
2480 s->theora_tables = 1;
2485 static av_cold int theora_decode_init(AVCodecContext *avctx)
2487 Vp3DecodeContext *s = avctx->priv_data;
2490 const uint8_t *header_start[3];
2495 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2499 if (!avctx->extradata_size) {
2500 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2504 if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2505 42, header_start, header_len) < 0) {
2506 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2510 for (i = 0; i < 3; i++) {
2511 if (header_len[i] <= 0)
2513 ret = init_get_bits8(&gb, header_start[i], header_len[i]);
2517 ptype = get_bits(&gb, 8);
2519 if (!(ptype & 0x80)) {
2520 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2524 // FIXME: Check for this as well.
2525 skip_bits_long(&gb, 6 * 8); /* "theora" */
2529 if (theora_decode_header(avctx, &gb) < 0)
2533 // FIXME: is this needed? it breaks sometimes
2534 // theora_decode_comments(avctx, gb);
2537 if (theora_decode_tables(avctx, &gb))
2541 av_log(avctx, AV_LOG_ERROR,
2542 "Unknown Theora config packet: %d\n", ptype & ~0x80);
2545 if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
2546 av_log(avctx, AV_LOG_WARNING,
2547 "%d bits left in packet %X\n",
2548 8 * header_len[i] - get_bits_count(&gb), ptype);
2549 if (s->theora < 0x030200)
2553 return vp3_decode_init(avctx);
2556 AVCodec ff_theora_decoder = {
2558 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2559 .type = AVMEDIA_TYPE_VIDEO,
2560 .id = AV_CODEC_ID_THEORA,
2561 .priv_data_size = sizeof(Vp3DecodeContext),
2562 .init = theora_decode_init,
2563 .close = vp3_decode_end,
2564 .decode = vp3_decode_frame,
2565 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
2566 AV_CODEC_CAP_FRAME_THREADS,
2567 .flush = vp3_decode_flush,
2568 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2569 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2573 AVCodec ff_vp3_decoder = {
2575 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2576 .type = AVMEDIA_TYPE_VIDEO,
2577 .id = AV_CODEC_ID_VP3,
2578 .priv_data_size = sizeof(Vp3DecodeContext),
2579 .init = vp3_decode_init,
2580 .close = vp3_decode_end,
2581 .decode = vp3_decode_frame,
2582 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
2583 AV_CODEC_CAP_FRAME_THREADS,
2584 .flush = vp3_decode_flush,
2585 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2586 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),