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;
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];
179 int8_t (*motion_val[2])[2];
182 uint16_t coded_dc_scale_factor[64];
183 uint32_t coded_ac_scale_factor[64];
184 uint8_t base_matrix[384][64];
185 uint8_t qr_count[2][3];
186 uint8_t qr_size[2][3][64];
187 uint16_t qr_base[2][3][64];
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)
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)
203 * Coefficients are signed, so are packed in the highest bits for automatic
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)
213 * number of blocks that contain DCT coefficients at
214 * the given level or higher
216 int num_coded_frags[3][64];
217 int total_num_coded_frags;
219 /* this is a list of indexes into the all_fragments array indicating
220 * which of the fragments are coded */
221 int *coded_fragment_list[3];
229 VLC superblock_run_length_vlc;
230 VLC fragment_run_length_vlc;
232 VLC motion_vector_vlc;
234 /* these arrays need to be on 16-byte boundaries since SSE2 operations
236 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
238 /* This table contains superblock_count * 16 entries. Each set of 16
239 * numbers corresponds to the fragment indexes 0..15 of the superblock.
240 * An entry will be -1 to indicate that no entry corresponds to that
242 int *superblock_fragments;
244 /* This is an array that indicates how a particular macroblock
246 unsigned char *macroblock_coding;
248 uint8_t *edge_emu_buffer;
255 uint32_t huffman_table[80][32][2];
257 uint8_t filter_limit_values[64];
258 DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
261 /************************************************************************
262 * VP3 specific functions
263 ************************************************************************/
265 static av_cold void free_tables(AVCodecContext *avctx)
267 Vp3DecodeContext *s = avctx->priv_data;
269 av_freep(&s->superblock_coding);
270 av_freep(&s->all_fragments);
271 av_freep(&s->coded_fragment_list[0]);
272 av_freep(&s->dct_tokens_base);
273 av_freep(&s->superblock_fragments);
274 av_freep(&s->macroblock_coding);
275 av_freep(&s->motion_val[0]);
276 av_freep(&s->motion_val[1]);
279 static void vp3_decode_flush(AVCodecContext *avctx)
281 Vp3DecodeContext *s = avctx->priv_data;
283 if (s->golden_frame.f)
284 ff_thread_release_buffer(avctx, &s->golden_frame);
286 ff_thread_release_buffer(avctx, &s->last_frame);
287 if (s->current_frame.f)
288 ff_thread_release_buffer(avctx, &s->current_frame);
291 static av_cold int vp3_decode_end(AVCodecContext *avctx)
293 Vp3DecodeContext *s = avctx->priv_data;
297 av_freep(&s->edge_emu_buffer);
299 s->theora_tables = 0;
301 /* release all frames */
302 vp3_decode_flush(avctx);
303 av_frame_free(&s->current_frame.f);
304 av_frame_free(&s->last_frame.f);
305 av_frame_free(&s->golden_frame.f);
307 if (avctx->internal->is_copy)
310 for (i = 0; i < 16; i++) {
311 ff_free_vlc(&s->dc_vlc[i]);
312 ff_free_vlc(&s->ac_vlc_1[i]);
313 ff_free_vlc(&s->ac_vlc_2[i]);
314 ff_free_vlc(&s->ac_vlc_3[i]);
315 ff_free_vlc(&s->ac_vlc_4[i]);
318 ff_free_vlc(&s->superblock_run_length_vlc);
319 ff_free_vlc(&s->fragment_run_length_vlc);
320 ff_free_vlc(&s->mode_code_vlc);
321 ff_free_vlc(&s->motion_vector_vlc);
327 * This function sets up all of the various blocks mappings:
328 * superblocks <-> fragments, macroblocks <-> fragments,
329 * superblocks <-> macroblocks
331 * @return 0 is successful; returns 1 if *anything* went wrong.
333 static int init_block_mapping(Vp3DecodeContext *s)
335 int sb_x, sb_y, plane;
338 for (plane = 0; plane < 3; plane++) {
339 int sb_width = plane ? s->c_superblock_width
340 : s->y_superblock_width;
341 int sb_height = plane ? s->c_superblock_height
342 : s->y_superblock_height;
343 int frag_width = s->fragment_width[!!plane];
344 int frag_height = s->fragment_height[!!plane];
346 for (sb_y = 0; sb_y < sb_height; sb_y++)
347 for (sb_x = 0; sb_x < sb_width; sb_x++)
348 for (i = 0; i < 16; i++) {
349 x = 4 * sb_x + hilbert_offset[i][0];
350 y = 4 * sb_y + hilbert_offset[i][1];
352 if (x < frag_width && y < frag_height)
353 s->superblock_fragments[j++] = s->fragment_start[plane] +
356 s->superblock_fragments[j++] = -1;
360 return 0; /* successful path out */
364 * This function sets up the dequantization tables used for a particular
367 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
369 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
370 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
371 int i, plane, inter, qri, bmi, bmj, qistart;
373 for (inter = 0; inter < 2; inter++) {
374 for (plane = 0; plane < 3; plane++) {
376 for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
377 sum += s->qr_size[inter][plane][qri];
378 if (s->qps[qpi] <= sum)
381 qistart = sum - s->qr_size[inter][plane][qri];
382 bmi = s->qr_base[inter][plane][qri];
383 bmj = s->qr_base[inter][plane][qri + 1];
384 for (i = 0; i < 64; i++) {
385 int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
386 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
387 s->qr_size[inter][plane][qri]) /
388 (2 * s->qr_size[inter][plane][qri]);
390 int qmin = 8 << (inter + !i);
391 int qscale = i ? ac_scale_factor : dc_scale_factor;
393 s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
394 av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
396 /* all DC coefficients use the same quant so as not to interfere
397 * with DC prediction */
398 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
404 * This function initializes the loop filter boundary limits if the frame's
405 * quality index is different from the previous frame's.
407 * The filter_limit_values may not be larger than 127.
409 static void init_loop_filter(Vp3DecodeContext *s)
411 int *bounding_values = s->bounding_values_array + 127;
416 filter_limit = s->filter_limit_values[s->qps[0]];
417 av_assert0(filter_limit < 128U);
419 /* set up the bounding values */
420 memset(s->bounding_values_array, 0, 256 * sizeof(int));
421 for (x = 0; x < filter_limit; x++) {
422 bounding_values[-x] = -x;
423 bounding_values[x] = x;
425 for (x = value = filter_limit; x < 128 && value; x++, value--) {
426 bounding_values[ x] = value;
427 bounding_values[-x] = -value;
430 bounding_values[128] = value;
431 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
435 * This function unpacks all of the superblock/macroblock/fragment coding
436 * information from the bitstream.
438 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
440 int superblock_starts[3] = {
441 0, s->u_superblock_start, s->v_superblock_start
444 int current_superblock = 0;
446 int num_partial_superblocks = 0;
449 int current_fragment;
453 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
455 /* unpack the list of partially-coded superblocks */
456 bit = get_bits1(gb) ^ 1;
459 while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
460 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
465 current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
467 if (current_run == 34)
468 current_run += get_bits(gb, 12);
470 if (current_superblock + current_run > s->superblock_count) {
471 av_log(s->avctx, AV_LOG_ERROR,
472 "Invalid partially coded superblock run length\n");
476 memset(s->superblock_coding + current_superblock, bit, current_run);
478 current_superblock += current_run;
480 num_partial_superblocks += current_run;
483 /* unpack the list of fully coded superblocks if any of the blocks were
484 * not marked as partially coded in the previous step */
485 if (num_partial_superblocks < s->superblock_count) {
486 int superblocks_decoded = 0;
488 current_superblock = 0;
489 bit = get_bits1(gb) ^ 1;
492 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
493 get_bits_left(gb) > 0) {
494 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
499 current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
501 if (current_run == 34)
502 current_run += get_bits(gb, 12);
504 for (j = 0; j < current_run; current_superblock++) {
505 if (current_superblock >= s->superblock_count) {
506 av_log(s->avctx, AV_LOG_ERROR,
507 "Invalid fully coded superblock run length\n");
511 /* skip any superblocks already marked as partially coded */
512 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
513 s->superblock_coding[current_superblock] = 2 * bit;
517 superblocks_decoded += current_run;
521 /* if there were partial blocks, initialize bitstream for
522 * unpacking fragment codings */
523 if (num_partial_superblocks) {
526 /* toggle the bit because as soon as the first run length is
527 * fetched the bit will be toggled again */
532 /* figure out which fragments are coded; iterate through each
533 * superblock (all planes) */
534 s->total_num_coded_frags = 0;
535 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
537 for (plane = 0; plane < 3; plane++) {
538 int sb_start = superblock_starts[plane];
539 int sb_end = sb_start + (plane ? s->c_superblock_count
540 : s->y_superblock_count);
541 int num_coded_frags = 0;
543 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
544 /* iterate through all 16 fragments in a superblock */
545 for (j = 0; j < 16; j++) {
546 /* if the fragment is in bounds, check its coding status */
547 current_fragment = s->superblock_fragments[i * 16 + j];
548 if (current_fragment != -1) {
549 int coded = s->superblock_coding[i];
551 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
552 /* fragment may or may not be coded; this is the case
553 * that cares about the fragment coding runs */
554 if (current_run-- == 0) {
556 current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
562 /* default mode; actual mode will be decoded in
564 s->all_fragments[current_fragment].coding_method =
566 s->coded_fragment_list[plane][num_coded_frags++] =
569 /* not coded; copy this fragment from the prior frame */
570 s->all_fragments[current_fragment].coding_method =
576 s->total_num_coded_frags += num_coded_frags;
577 for (i = 0; i < 64; i++)
578 s->num_coded_frags[plane][i] = num_coded_frags;
580 s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
587 * This function unpacks all the coding mode data for individual macroblocks
588 * from the bitstream.
590 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
592 int i, j, k, sb_x, sb_y;
594 int current_macroblock;
595 int current_fragment;
597 int custom_mode_alphabet[CODING_MODE_COUNT];
602 for (i = 0; i < s->fragment_count; i++)
603 s->all_fragments[i].coding_method = MODE_INTRA;
605 /* fetch the mode coding scheme for this frame */
606 scheme = get_bits(gb, 3);
608 /* is it a custom coding scheme? */
610 for (i = 0; i < 8; i++)
611 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
612 for (i = 0; i < 8; i++)
613 custom_mode_alphabet[get_bits(gb, 3)] = i;
614 alphabet = custom_mode_alphabet;
616 alphabet = ModeAlphabet[scheme - 1];
618 /* iterate through all of the macroblocks that contain 1 or more
620 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
621 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
622 if (get_bits_left(gb) <= 0)
625 for (j = 0; j < 4; j++) {
626 int mb_x = 2 * sb_x + (j >> 1);
627 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
628 current_macroblock = mb_y * s->macroblock_width + mb_x;
630 if (mb_x >= s->macroblock_width ||
631 mb_y >= s->macroblock_height)
634 #define BLOCK_X (2 * mb_x + (k & 1))
635 #define BLOCK_Y (2 * mb_y + (k >> 1))
636 /* coding modes are only stored if the macroblock has
637 * at least one luma block coded, otherwise it must be
639 for (k = 0; k < 4; k++) {
640 current_fragment = BLOCK_Y *
641 s->fragment_width[0] + BLOCK_X;
642 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
646 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
650 /* mode 7 means get 3 bits for each coding mode */
652 coding_mode = get_bits(gb, 3);
654 coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
656 s->macroblock_coding[current_macroblock] = coding_mode;
657 for (k = 0; k < 4; k++) {
658 frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
659 if (frag->coding_method != MODE_COPY)
660 frag->coding_method = coding_mode;
663 #define SET_CHROMA_MODES \
664 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
665 frag[s->fragment_start[1]].coding_method = coding_mode; \
666 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
667 frag[s->fragment_start[2]].coding_method = coding_mode;
669 if (s->chroma_y_shift) {
670 frag = s->all_fragments + mb_y *
671 s->fragment_width[1] + mb_x;
673 } else if (s->chroma_x_shift) {
674 frag = s->all_fragments +
675 2 * mb_y * s->fragment_width[1] + mb_x;
676 for (k = 0; k < 2; k++) {
678 frag += s->fragment_width[1];
681 for (k = 0; k < 4; k++) {
682 frag = s->all_fragments +
683 BLOCK_Y * s->fragment_width[1] + BLOCK_X;
696 * This function unpacks all the motion vectors for the individual
697 * macroblocks from the bitstream.
699 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
701 int j, k, sb_x, sb_y;
705 int last_motion_x = 0;
706 int last_motion_y = 0;
707 int prior_last_motion_x = 0;
708 int prior_last_motion_y = 0;
709 int current_macroblock;
710 int current_fragment;
716 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
717 coding_mode = get_bits1(gb);
719 /* iterate through all of the macroblocks that contain 1 or more
721 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
722 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
723 if (get_bits_left(gb) <= 0)
726 for (j = 0; j < 4; j++) {
727 int mb_x = 2 * sb_x + (j >> 1);
728 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
729 current_macroblock = mb_y * s->macroblock_width + mb_x;
731 if (mb_x >= s->macroblock_width ||
732 mb_y >= s->macroblock_height ||
733 s->macroblock_coding[current_macroblock] == MODE_COPY)
736 switch (s->macroblock_coding[current_macroblock]) {
737 case MODE_INTER_PLUS_MV:
739 /* all 6 fragments use the same motion vector */
740 if (coding_mode == 0) {
741 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
742 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
744 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
745 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
748 /* vector maintenance, only on MODE_INTER_PLUS_MV */
749 if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
750 prior_last_motion_x = last_motion_x;
751 prior_last_motion_y = last_motion_y;
752 last_motion_x = motion_x[0];
753 last_motion_y = motion_y[0];
757 case MODE_INTER_FOURMV:
758 /* vector maintenance */
759 prior_last_motion_x = last_motion_x;
760 prior_last_motion_y = last_motion_y;
762 /* fetch 4 vectors from the bitstream, one for each
763 * Y fragment, then average for the C fragment vectors */
764 for (k = 0; k < 4; k++) {
765 current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
766 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
767 if (coding_mode == 0) {
768 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
769 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
771 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
772 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
774 last_motion_x = motion_x[k];
775 last_motion_y = motion_y[k];
783 case MODE_INTER_LAST_MV:
784 /* all 6 fragments use the last motion vector */
785 motion_x[0] = last_motion_x;
786 motion_y[0] = last_motion_y;
788 /* no vector maintenance (last vector remains the
792 case MODE_INTER_PRIOR_LAST:
793 /* all 6 fragments use the motion vector prior to the
794 * last motion vector */
795 motion_x[0] = prior_last_motion_x;
796 motion_y[0] = prior_last_motion_y;
798 /* vector maintenance */
799 prior_last_motion_x = last_motion_x;
800 prior_last_motion_y = last_motion_y;
801 last_motion_x = motion_x[0];
802 last_motion_y = motion_y[0];
806 /* covers intra, inter without MV, golden without MV */
810 /* no vector maintenance */
814 /* assign the motion vectors to the correct fragments */
815 for (k = 0; k < 4; k++) {
817 BLOCK_Y * s->fragment_width[0] + BLOCK_X;
818 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
819 s->motion_val[0][current_fragment][0] = motion_x[k];
820 s->motion_val[0][current_fragment][1] = motion_y[k];
822 s->motion_val[0][current_fragment][0] = motion_x[0];
823 s->motion_val[0][current_fragment][1] = motion_y[0];
827 if (s->chroma_y_shift) {
828 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
829 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
830 motion_x[2] + motion_x[3], 2);
831 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
832 motion_y[2] + motion_y[3], 2);
834 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
835 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
836 frag = mb_y * s->fragment_width[1] + mb_x;
837 s->motion_val[1][frag][0] = motion_x[0];
838 s->motion_val[1][frag][1] = motion_y[0];
839 } else if (s->chroma_x_shift) {
840 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
841 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
842 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
843 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
844 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
846 motion_x[1] = motion_x[0];
847 motion_y[1] = motion_y[0];
849 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
850 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
852 frag = 2 * mb_y * s->fragment_width[1] + mb_x;
853 for (k = 0; k < 2; k++) {
854 s->motion_val[1][frag][0] = motion_x[k];
855 s->motion_val[1][frag][1] = motion_y[k];
856 frag += s->fragment_width[1];
859 for (k = 0; k < 4; k++) {
860 frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
861 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
862 s->motion_val[1][frag][0] = motion_x[k];
863 s->motion_val[1][frag][1] = motion_y[k];
865 s->motion_val[1][frag][0] = motion_x[0];
866 s->motion_val[1][frag][1] = motion_y[0];
877 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
879 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
880 int num_blocks = s->total_num_coded_frags;
882 for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
883 i = blocks_decoded = num_blocks_at_qpi = 0;
885 bit = get_bits1(gb) ^ 1;
889 if (run_length == MAXIMUM_LONG_BIT_RUN)
894 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
895 if (run_length == 34)
896 run_length += get_bits(gb, 12);
897 blocks_decoded += run_length;
900 num_blocks_at_qpi += run_length;
902 for (j = 0; j < run_length; i++) {
903 if (i >= s->total_num_coded_frags)
906 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
907 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
911 } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
913 num_blocks -= num_blocks_at_qpi;
920 * This function is called by unpack_dct_coeffs() to extract the VLCs from
921 * the bitstream. The VLCs encode tokens which are used to unpack DCT
922 * data. This function unpacks all the VLCs for either the Y plane or both
923 * C planes, and is called for DC coefficients or different AC coefficient
924 * levels (since different coefficient types require different VLC tables.
926 * This function returns a residual eob run. E.g, if a particular token gave
927 * instructions to EOB the next 5 fragments and there were only 2 fragments
928 * left in the current fragment range, 3 would be returned so that it could
929 * be passed into the next call to this same function.
931 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
932 VLC *table, int coeff_index,
943 int num_coeffs = s->num_coded_frags[plane][coeff_index];
944 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
946 /* local references to structure members to avoid repeated deferences */
947 int *coded_fragment_list = s->coded_fragment_list[plane];
948 Vp3Fragment *all_fragments = s->all_fragments;
949 VLC_TYPE(*vlc_table)[2] = table->table;
952 av_log(s->avctx, AV_LOG_ERROR,
953 "Invalid number of coefficents at level %d\n", coeff_index);
955 if (eob_run > num_coeffs) {
957 blocks_ended = num_coeffs;
958 eob_run -= num_coeffs;
961 blocks_ended = eob_run;
965 // insert fake EOB token to cover the split between planes or zzi
967 dct_tokens[j++] = blocks_ended << 2;
969 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
970 /* decode a VLC into a token */
971 token = get_vlc2(gb, vlc_table, 11, 3);
972 /* use the token to get a zero run, a coefficient, and an eob run */
973 if ((unsigned) token <= 6U) {
974 eob_run = eob_run_base[token];
975 if (eob_run_get_bits[token])
976 eob_run += get_bits(gb, eob_run_get_bits[token]);
978 // record only the number of blocks ended in this plane,
979 // any spill will be recorded in the next plane.
980 if (eob_run > num_coeffs - coeff_i) {
981 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
982 blocks_ended += num_coeffs - coeff_i;
983 eob_run -= num_coeffs - coeff_i;
984 coeff_i = num_coeffs;
986 dct_tokens[j++] = TOKEN_EOB(eob_run);
987 blocks_ended += eob_run;
991 } else if (token >= 0) {
992 bits_to_get = coeff_get_bits[token];
994 bits_to_get = get_bits(gb, bits_to_get);
995 coeff = coeff_tables[token][bits_to_get];
997 zero_run = zero_run_base[token];
998 if (zero_run_get_bits[token])
999 zero_run += get_bits(gb, zero_run_get_bits[token]);
1002 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
1004 // Save DC into the fragment structure. DC prediction is
1005 // done in raster order, so the actual DC can't be in with
1006 // other tokens. We still need the token in dct_tokens[]
1007 // however, or else the structure collapses on itself.
1009 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
1011 dct_tokens[j++] = TOKEN_COEFF(coeff);
1014 if (coeff_index + zero_run > 64) {
1015 av_log(s->avctx, AV_LOG_DEBUG,
1016 "Invalid zero run of %d with %d coeffs left\n",
1017 zero_run, 64 - coeff_index);
1018 zero_run = 64 - coeff_index;
1021 // zero runs code multiple coefficients,
1022 // so don't try to decode coeffs for those higher levels
1023 for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1024 s->num_coded_frags[plane][i]--;
1027 av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
1032 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1033 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1035 // decrement the number of blocks that have higher coeffecients for each
1036 // EOB run at this level
1038 for (i = coeff_index + 1; i < 64; i++)
1039 s->num_coded_frags[plane][i] -= blocks_ended;
1041 // setup the next buffer
1043 s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1044 else if (coeff_index < 63)
1045 s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1050 static void reverse_dc_prediction(Vp3DecodeContext *s,
1053 int fragment_height);
1055 * This function unpacks all of the DCT coefficient data from the
1058 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1065 int residual_eob_run = 0;
1069 s->dct_tokens[0][0] = s->dct_tokens_base;
1071 /* fetch the DC table indexes */
1072 dc_y_table = get_bits(gb, 4);
1073 dc_c_table = get_bits(gb, 4);
1075 /* unpack the Y plane DC coefficients */
1076 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1077 0, residual_eob_run);
1078 if (residual_eob_run < 0)
1079 return residual_eob_run;
1081 /* reverse prediction of the Y-plane DC coefficients */
1082 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
1084 /* unpack the C plane DC coefficients */
1085 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1086 1, residual_eob_run);
1087 if (residual_eob_run < 0)
1088 return residual_eob_run;
1089 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1090 2, residual_eob_run);
1091 if (residual_eob_run < 0)
1092 return residual_eob_run;
1094 /* reverse prediction of the C-plane DC coefficients */
1095 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
1096 reverse_dc_prediction(s, s->fragment_start[1],
1097 s->fragment_width[1], s->fragment_height[1]);
1098 reverse_dc_prediction(s, s->fragment_start[2],
1099 s->fragment_width[1], s->fragment_height[1]);
1102 /* fetch the AC table indexes */
1103 ac_y_table = get_bits(gb, 4);
1104 ac_c_table = get_bits(gb, 4);
1106 /* build tables of AC VLC tables */
1107 for (i = 1; i <= 5; i++) {
1108 y_tables[i] = &s->ac_vlc_1[ac_y_table];
1109 c_tables[i] = &s->ac_vlc_1[ac_c_table];
1111 for (i = 6; i <= 14; i++) {
1112 y_tables[i] = &s->ac_vlc_2[ac_y_table];
1113 c_tables[i] = &s->ac_vlc_2[ac_c_table];
1115 for (i = 15; i <= 27; i++) {
1116 y_tables[i] = &s->ac_vlc_3[ac_y_table];
1117 c_tables[i] = &s->ac_vlc_3[ac_c_table];
1119 for (i = 28; i <= 63; i++) {
1120 y_tables[i] = &s->ac_vlc_4[ac_y_table];
1121 c_tables[i] = &s->ac_vlc_4[ac_c_table];
1124 /* decode all AC coefficents */
1125 for (i = 1; i <= 63; i++) {
1126 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1127 0, residual_eob_run);
1128 if (residual_eob_run < 0)
1129 return residual_eob_run;
1131 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1132 1, residual_eob_run);
1133 if (residual_eob_run < 0)
1134 return residual_eob_run;
1135 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1136 2, residual_eob_run);
1137 if (residual_eob_run < 0)
1138 return residual_eob_run;
1145 * This function reverses the DC prediction for each coded fragment in
1146 * the frame. Much of this function is adapted directly from the original
1149 #define COMPATIBLE_FRAME(x) \
1150 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1151 #define DC_COEFF(u) s->all_fragments[u].dc
1153 static void reverse_dc_prediction(Vp3DecodeContext *s,
1156 int fragment_height)
1164 int i = first_fragment;
1168 /* DC values for the left, up-left, up, and up-right fragments */
1169 int vl, vul, vu, vur;
1171 /* indexes for the left, up-left, up, and up-right fragments */
1175 * The 6 fields mean:
1176 * 0: up-left multiplier
1178 * 2: up-right multiplier
1179 * 3: left multiplier
1181 static const int predictor_transform[16][4] = {
1183 { 0, 0, 0, 128 }, // PL
1184 { 0, 0, 128, 0 }, // PUR
1185 { 0, 0, 53, 75 }, // PUR|PL
1186 { 0, 128, 0, 0 }, // PU
1187 { 0, 64, 0, 64 }, // PU |PL
1188 { 0, 128, 0, 0 }, // PU |PUR
1189 { 0, 0, 53, 75 }, // PU |PUR|PL
1190 { 128, 0, 0, 0 }, // PUL
1191 { 0, 0, 0, 128 }, // PUL|PL
1192 { 64, 0, 64, 0 }, // PUL|PUR
1193 { 0, 0, 53, 75 }, // PUL|PUR|PL
1194 { 0, 128, 0, 0 }, // PUL|PU
1195 { -104, 116, 0, 116 }, // PUL|PU |PL
1196 { 24, 80, 24, 0 }, // PUL|PU |PUR
1197 { -104, 116, 0, 116 } // PUL|PU |PUR|PL
1200 /* This table shows which types of blocks can use other blocks for
1201 * prediction. For example, INTRA is the only mode in this table to
1202 * have a frame number of 0. That means INTRA blocks can only predict
1203 * from other INTRA blocks. There are 2 golden frame coding types;
1204 * blocks encoding in these modes can only predict from other blocks
1205 * that were encoded with these 1 of these 2 modes. */
1206 static const unsigned char compatible_frame[9] = {
1207 1, /* MODE_INTER_NO_MV */
1209 1, /* MODE_INTER_PLUS_MV */
1210 1, /* MODE_INTER_LAST_MV */
1211 1, /* MODE_INTER_PRIOR_MV */
1212 2, /* MODE_USING_GOLDEN */
1213 2, /* MODE_GOLDEN_MV */
1214 1, /* MODE_INTER_FOUR_MV */
1217 int current_frame_type;
1219 /* there is a last DC predictor for each of the 3 frame types */
1232 /* for each fragment row... */
1233 for (y = 0; y < fragment_height; y++) {
1234 /* for each fragment in a row... */
1235 for (x = 0; x < fragment_width; x++, i++) {
1237 /* reverse prediction if this block was coded */
1238 if (s->all_fragments[i].coding_method != MODE_COPY) {
1239 current_frame_type =
1240 compatible_frame[s->all_fragments[i].coding_method];
1246 if (COMPATIBLE_FRAME(l))
1250 u = i - fragment_width;
1252 if (COMPATIBLE_FRAME(u))
1255 ul = i - fragment_width - 1;
1257 if (COMPATIBLE_FRAME(ul))
1260 if (x + 1 < fragment_width) {
1261 ur = i - fragment_width + 1;
1263 if (COMPATIBLE_FRAME(ur))
1268 if (transform == 0) {
1269 /* if there were no fragments to predict from, use last
1271 predicted_dc = last_dc[current_frame_type];
1273 /* apply the appropriate predictor transform */
1275 (predictor_transform[transform][0] * vul) +
1276 (predictor_transform[transform][1] * vu) +
1277 (predictor_transform[transform][2] * vur) +
1278 (predictor_transform[transform][3] * vl);
1280 predicted_dc /= 128;
1282 /* check for outranging on the [ul u l] and
1283 * [ul u ur l] predictors */
1284 if ((transform == 15) || (transform == 13)) {
1285 if (FFABS(predicted_dc - vu) > 128)
1287 else if (FFABS(predicted_dc - vl) > 128)
1289 else if (FFABS(predicted_dc - vul) > 128)
1294 /* at long last, apply the predictor */
1295 DC_COEFF(i) += predicted_dc;
1297 last_dc[current_frame_type] = DC_COEFF(i);
1303 static void apply_loop_filter(Vp3DecodeContext *s, int plane,
1304 int ystart, int yend)
1307 int *bounding_values = s->bounding_values_array + 127;
1309 int width = s->fragment_width[!!plane];
1310 int height = s->fragment_height[!!plane];
1311 int fragment = s->fragment_start[plane] + ystart * width;
1312 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1313 uint8_t *plane_data = s->current_frame.f->data[plane];
1314 if (!s->flipped_image)
1316 plane_data += s->data_offset[plane] + 8 * ystart * stride;
1318 for (y = ystart; y < yend; y++) {
1319 for (x = 0; x < width; x++) {
1320 /* This code basically just deblocks on the edges of coded blocks.
1321 * However, it has to be much more complicated because of the
1322 * braindamaged deblock ordering used in VP3/Theora. Order matters
1323 * because some pixels get filtered twice. */
1324 if (s->all_fragments[fragment].coding_method != MODE_COPY) {
1325 /* do not perform left edge filter for left columns frags */
1327 s->vp3dsp.h_loop_filter(
1329 stride, bounding_values);
1332 /* do not perform top edge filter for top row fragments */
1334 s->vp3dsp.v_loop_filter(
1336 stride, bounding_values);
1339 /* do not perform right edge filter for right column
1340 * fragments or if right fragment neighbor is also coded
1341 * in this frame (it will be filtered in next iteration) */
1342 if ((x < width - 1) &&
1343 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1344 s->vp3dsp.h_loop_filter(
1345 plane_data + 8 * x + 8,
1346 stride, bounding_values);
1349 /* do not perform bottom edge filter for bottom row
1350 * fragments or if bottom fragment neighbor is also coded
1351 * in this frame (it will be filtered in the next row) */
1352 if ((y < height - 1) &&
1353 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1354 s->vp3dsp.v_loop_filter(
1355 plane_data + 8 * x + 8 * stride,
1356 stride, bounding_values);
1362 plane_data += 8 * stride;
1367 * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
1368 * for the next block in coding order
1370 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1371 int plane, int inter, int16_t block[64])
1373 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1374 uint8_t *perm = s->idct_scantable;
1378 int token = *s->dct_tokens[plane][i];
1379 switch (token & 3) {
1381 if (--token < 4) // 0-3 are token types so the EOB run must now be 0
1382 s->dct_tokens[plane][i]++;
1384 *s->dct_tokens[plane][i] = token & ~3;
1387 s->dct_tokens[plane][i]++;
1388 i += (token >> 2) & 0x7f;
1390 av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
1393 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1397 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1398 s->dct_tokens[plane][i++]++;
1400 default: // shouldn't happen
1404 // return value is expected to be a valid level
1407 // the actual DC+prediction is in the fragment structure
1408 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1413 * called when all pixels up to row y are complete
1415 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1418 int offset[AV_NUM_DATA_POINTERS];
1420 if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
1421 int y_flipped = s->flipped_image ? s->avctx->height - y : y;
1423 /* At the end of the frame, report INT_MAX instead of the height of
1424 * the frame. This makes the other threads' ff_thread_await_progress()
1425 * calls cheaper, because they don't have to clip their values. */
1426 ff_thread_report_progress(&s->current_frame,
1427 y_flipped == s->avctx->height ? INT_MAX
1432 if (s->avctx->draw_horiz_band == NULL)
1435 h = y - s->last_slice_end;
1436 s->last_slice_end = y;
1439 if (!s->flipped_image)
1440 y = s->avctx->height - y - h;
1442 cy = y >> s->chroma_y_shift;
1443 offset[0] = s->current_frame.f->linesize[0] * y;
1444 offset[1] = s->current_frame.f->linesize[1] * cy;
1445 offset[2] = s->current_frame.f->linesize[2] * cy;
1446 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1450 s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
1454 * Wait for the reference frame of the current fragment.
1455 * The progress value is in luma pixel rows.
1457 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment,
1458 int motion_y, int y)
1460 ThreadFrame *ref_frame;
1462 int border = motion_y & 1;
1464 if (fragment->coding_method == MODE_USING_GOLDEN ||
1465 fragment->coding_method == MODE_GOLDEN_MV)
1466 ref_frame = &s->golden_frame;
1468 ref_frame = &s->last_frame;
1470 ref_row = y + (motion_y >> 1);
1471 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1473 ff_thread_await_progress(ref_frame, ref_row, 0);
1477 * Perform the final rendering for a particular slice of data.
1478 * The slice number ranges from 0..(c_superblock_height - 1).
1480 static void render_slice(Vp3DecodeContext *s, int slice)
1482 int x, y, i, j, fragment;
1483 int16_t *block = s->block;
1484 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1485 int motion_halfpel_index;
1486 uint8_t *motion_source;
1487 int plane, first_pixel;
1489 if (slice >= s->c_superblock_height)
1492 for (plane = 0; plane < 3; plane++) {
1493 uint8_t *output_plane = s->current_frame.f->data[plane] +
1494 s->data_offset[plane];
1495 uint8_t *last_plane = s->last_frame.f->data[plane] +
1496 s->data_offset[plane];
1497 uint8_t *golden_plane = s->golden_frame.f->data[plane] +
1498 s->data_offset[plane];
1499 ptrdiff_t stride = s->current_frame.f->linesize[plane];
1500 int plane_width = s->width >> (plane && s->chroma_x_shift);
1501 int plane_height = s->height >> (plane && s->chroma_y_shift);
1502 int8_t(*motion_val)[2] = s->motion_val[!!plane];
1504 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1505 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1506 int slice_width = plane ? s->c_superblock_width
1507 : s->y_superblock_width;
1509 int fragment_width = s->fragment_width[!!plane];
1510 int fragment_height = s->fragment_height[!!plane];
1511 int fragment_start = s->fragment_start[plane];
1513 int do_await = !plane && HAVE_THREADS &&
1514 (s->avctx->active_thread_type & FF_THREAD_FRAME);
1516 if (!s->flipped_image)
1518 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1521 /* for each superblock row in the slice (both of them)... */
1522 for (; sb_y < slice_height; sb_y++) {
1523 /* for each superblock in a row... */
1524 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1525 /* for each block in a superblock... */
1526 for (j = 0; j < 16; j++) {
1527 x = 4 * sb_x + hilbert_offset[j][0];
1528 y = 4 * sb_y + hilbert_offset[j][1];
1529 fragment = y * fragment_width + x;
1531 i = fragment_start + fragment;
1534 if (x >= fragment_width || y >= fragment_height)
1537 first_pixel = 8 * y * stride + 8 * x;
1540 s->all_fragments[i].coding_method != MODE_INTRA)
1541 await_reference_row(s, &s->all_fragments[i],
1542 motion_val[fragment][1],
1543 (16 * y) >> s->chroma_y_shift);
1545 /* transform if this block was coded */
1546 if (s->all_fragments[i].coding_method != MODE_COPY) {
1547 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1548 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1549 motion_source = golden_plane;
1551 motion_source = last_plane;
1553 motion_source += first_pixel;
1554 motion_halfpel_index = 0;
1556 /* sort out the motion vector if this fragment is coded
1557 * using a motion vector method */
1558 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1559 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1561 motion_x = motion_val[fragment][0];
1562 motion_y = motion_val[fragment][1];
1564 src_x = (motion_x >> 1) + 8 * x;
1565 src_y = (motion_y >> 1) + 8 * y;
1567 motion_halfpel_index = motion_x & 0x01;
1568 motion_source += (motion_x >> 1);
1570 motion_halfpel_index |= (motion_y & 0x01) << 1;
1571 motion_source += ((motion_y >> 1) * stride);
1573 if (src_x < 0 || src_y < 0 ||
1574 src_x + 9 >= plane_width ||
1575 src_y + 9 >= plane_height) {
1576 uint8_t *temp = s->edge_emu_buffer;
1580 s->vdsp.emulated_edge_mc(temp, motion_source,
1585 motion_source = temp;
1589 /* first, take care of copying a block from either the
1590 * previous or the golden frame */
1591 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1592 /* Note, it is possible to implement all MC cases
1593 * with put_no_rnd_pixels_l2 which would look more
1594 * like the VP3 source but this would be slower as
1595 * put_no_rnd_pixels_tab is better optimzed */
1596 if (motion_halfpel_index != 3) {
1597 s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
1598 output_plane + first_pixel,
1599 motion_source, stride, 8);
1601 /* d is 0 if motion_x and _y have the same sign,
1603 int d = (motion_x ^ motion_y) >> 31;
1604 s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
1606 motion_source + stride + 1 + d,
1611 /* invert DCT and place (or add) in final output */
1613 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1614 vp3_dequant(s, s->all_fragments + i,
1616 s->vp3dsp.idct_put(output_plane + first_pixel,
1620 if (vp3_dequant(s, s->all_fragments + i,
1622 s->vp3dsp.idct_add(output_plane + first_pixel,
1626 s->vp3dsp.idct_dc_add(output_plane + first_pixel,
1631 /* copy directly from the previous frame */
1632 s->hdsp.put_pixels_tab[1][0](
1633 output_plane + first_pixel,
1634 last_plane + first_pixel,
1640 // Filter up to the last row in the superblock row
1641 if (!s->skip_loop_filter)
1642 apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
1643 FFMIN(4 * sb_y + 3, fragment_height - 1));
1647 /* this looks like a good place for slice dispatch... */
1649 * if (slice == s->macroblock_height - 1)
1650 * dispatch (both last slice & 2nd-to-last slice);
1651 * else if (slice > 0)
1652 * dispatch (slice - 1);
1655 vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
1659 /// Allocate tables for per-frame data in Vp3DecodeContext
1660 static av_cold int allocate_tables(AVCodecContext *avctx)
1662 Vp3DecodeContext *s = avctx->priv_data;
1663 int y_fragment_count, c_fragment_count;
1667 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1668 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1670 s->superblock_coding = av_mallocz(s->superblock_count);
1671 s->all_fragments = av_mallocz_array(s->fragment_count, sizeof(Vp3Fragment));
1673 s->coded_fragment_list[0] = av_mallocz_array(s->fragment_count, sizeof(int));
1675 s->dct_tokens_base = av_mallocz_array(s->fragment_count,
1676 64 * sizeof(*s->dct_tokens_base));
1677 s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
1678 s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
1680 /* work out the block mapping tables */
1681 s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
1682 s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
1684 if (!s->superblock_coding || !s->all_fragments ||
1685 !s->dct_tokens_base || !s->coded_fragment_list[0] ||
1686 !s->superblock_fragments || !s->macroblock_coding ||
1687 !s->motion_val[0] || !s->motion_val[1]) {
1688 vp3_decode_end(avctx);
1692 init_block_mapping(s);
1697 static av_cold int init_frames(Vp3DecodeContext *s)
1699 s->current_frame.f = av_frame_alloc();
1700 s->last_frame.f = av_frame_alloc();
1701 s->golden_frame.f = av_frame_alloc();
1703 if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
1704 av_frame_free(&s->current_frame.f);
1705 av_frame_free(&s->last_frame.f);
1706 av_frame_free(&s->golden_frame.f);
1707 return AVERROR(ENOMEM);
1713 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1715 Vp3DecodeContext *s = avctx->priv_data;
1716 int i, inter, plane, ret;
1719 int y_fragment_count, c_fragment_count;
1721 ret = init_frames(s);
1725 avctx->internal->allocate_progress = 1;
1727 if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
1733 s->width = FFALIGN(avctx->width, 16);
1734 s->height = FFALIGN(avctx->height, 16);
1735 if (avctx->codec_id != AV_CODEC_ID_THEORA)
1736 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1737 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1738 ff_hpeldsp_init(&s->hdsp, avctx->flags | CODEC_FLAG_BITEXACT);
1739 ff_videodsp_init(&s->vdsp, 8);
1740 ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
1742 for (i = 0; i < 64; i++) {
1743 #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
1744 s->idct_permutation[i] = TRANSPOSE(i);
1745 s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
1749 /* initialize to an impossible value which will force a recalculation
1750 * in the first frame decode */
1751 for (i = 0; i < 3; i++)
1754 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1756 s->y_superblock_width = (s->width + 31) / 32;
1757 s->y_superblock_height = (s->height + 31) / 32;
1758 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1760 /* work out the dimensions for the C planes */
1761 c_width = s->width >> s->chroma_x_shift;
1762 c_height = s->height >> s->chroma_y_shift;
1763 s->c_superblock_width = (c_width + 31) / 32;
1764 s->c_superblock_height = (c_height + 31) / 32;
1765 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1767 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1768 s->u_superblock_start = s->y_superblock_count;
1769 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1771 s->macroblock_width = (s->width + 15) / 16;
1772 s->macroblock_height = (s->height + 15) / 16;
1773 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1775 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1776 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1777 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
1778 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1780 /* fragment count covers all 8x8 blocks for all 3 planes */
1781 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1782 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1783 s->fragment_count = y_fragment_count + 2 * c_fragment_count;
1784 s->fragment_start[1] = y_fragment_count;
1785 s->fragment_start[2] = y_fragment_count + c_fragment_count;
1787 if (!s->theora_tables) {
1788 for (i = 0; i < 64; i++) {
1789 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1790 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1791 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1792 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1793 s->base_matrix[2][i] = vp31_inter_dequant[i];
1794 s->filter_limit_values[i] = vp31_filter_limit_values[i];
1797 for (inter = 0; inter < 2; inter++) {
1798 for (plane = 0; plane < 3; plane++) {
1799 s->qr_count[inter][plane] = 1;
1800 s->qr_size[inter][plane][0] = 63;
1801 s->qr_base[inter][plane][0] =
1802 s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1806 /* init VLC tables */
1807 for (i = 0; i < 16; i++) {
1809 init_vlc(&s->dc_vlc[i], 11, 32,
1810 &dc_bias[i][0][1], 4, 2,
1811 &dc_bias[i][0][0], 4, 2, 0);
1813 /* group 1 AC histograms */
1814 init_vlc(&s->ac_vlc_1[i], 11, 32,
1815 &ac_bias_0[i][0][1], 4, 2,
1816 &ac_bias_0[i][0][0], 4, 2, 0);
1818 /* group 2 AC histograms */
1819 init_vlc(&s->ac_vlc_2[i], 11, 32,
1820 &ac_bias_1[i][0][1], 4, 2,
1821 &ac_bias_1[i][0][0], 4, 2, 0);
1823 /* group 3 AC histograms */
1824 init_vlc(&s->ac_vlc_3[i], 11, 32,
1825 &ac_bias_2[i][0][1], 4, 2,
1826 &ac_bias_2[i][0][0], 4, 2, 0);
1828 /* group 4 AC histograms */
1829 init_vlc(&s->ac_vlc_4[i], 11, 32,
1830 &ac_bias_3[i][0][1], 4, 2,
1831 &ac_bias_3[i][0][0], 4, 2, 0);
1834 for (i = 0; i < 16; i++) {
1836 if (init_vlc(&s->dc_vlc[i], 11, 32,
1837 &s->huffman_table[i][0][1], 8, 4,
1838 &s->huffman_table[i][0][0], 8, 4, 0) < 0)
1841 /* group 1 AC histograms */
1842 if (init_vlc(&s->ac_vlc_1[i], 11, 32,
1843 &s->huffman_table[i + 16][0][1], 8, 4,
1844 &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
1847 /* group 2 AC histograms */
1848 if (init_vlc(&s->ac_vlc_2[i], 11, 32,
1849 &s->huffman_table[i + 16 * 2][0][1], 8, 4,
1850 &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
1853 /* group 3 AC histograms */
1854 if (init_vlc(&s->ac_vlc_3[i], 11, 32,
1855 &s->huffman_table[i + 16 * 3][0][1], 8, 4,
1856 &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
1859 /* group 4 AC histograms */
1860 if (init_vlc(&s->ac_vlc_4[i], 11, 32,
1861 &s->huffman_table[i + 16 * 4][0][1], 8, 4,
1862 &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
1867 init_vlc(&s->superblock_run_length_vlc, 6, 34,
1868 &superblock_run_length_vlc_table[0][1], 4, 2,
1869 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1871 init_vlc(&s->fragment_run_length_vlc, 5, 30,
1872 &fragment_run_length_vlc_table[0][1], 4, 2,
1873 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1875 init_vlc(&s->mode_code_vlc, 3, 8,
1876 &mode_code_vlc_table[0][1], 2, 1,
1877 &mode_code_vlc_table[0][0], 2, 1, 0);
1879 init_vlc(&s->motion_vector_vlc, 6, 63,
1880 &motion_vector_vlc_table[0][1], 2, 1,
1881 &motion_vector_vlc_table[0][0], 2, 1, 0);
1883 return allocate_tables(avctx);
1886 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1890 /// Release and shuffle frames after decode finishes
1891 static int update_frames(AVCodecContext *avctx)
1893 Vp3DecodeContext *s = avctx->priv_data;
1896 /* shuffle frames (last = current) */
1897 ff_thread_release_buffer(avctx, &s->last_frame);
1898 ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
1903 ff_thread_release_buffer(avctx, &s->golden_frame);
1904 ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
1908 ff_thread_release_buffer(avctx, &s->current_frame);
1912 static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
1914 ff_thread_release_buffer(s->avctx, dst);
1915 if (src->f->data[0])
1916 return ff_thread_ref_frame(dst, src);
1920 static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
1923 if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
1924 (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
1925 (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
1930 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1932 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1933 int qps_changed = 0, i, err;
1935 #define copy_fields(to, from, start_field, end_field) \
1936 memcpy(&to->start_field, &from->start_field, \
1937 (char *) &to->end_field - (char *) &to->start_field)
1939 if (!s1->current_frame.f->data[0] ||
1940 s->width != s1->width || s->height != s1->height) {
1947 // init tables if the first frame hasn't been decoded
1948 if (!s->current_frame.f->data[0]) {
1949 int y_fragment_count, c_fragment_count;
1951 err = allocate_tables(dst);
1954 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1955 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1956 memcpy(s->motion_val[0], s1->motion_val[0],
1957 y_fragment_count * sizeof(*s->motion_val[0]));
1958 memcpy(s->motion_val[1], s1->motion_val[1],
1959 c_fragment_count * sizeof(*s->motion_val[1]));
1962 // copy previous frame data
1963 if ((err = ref_frames(s, s1)) < 0)
1966 s->keyframe = s1->keyframe;
1968 // copy qscale data if necessary
1969 for (i = 0; i < 3; i++) {
1970 if (s->qps[i] != s1->qps[1]) {
1972 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1976 if (s->qps[0] != s1->qps[0])
1977 memcpy(&s->bounding_values_array, &s1->bounding_values_array,
1978 sizeof(s->bounding_values_array));
1981 copy_fields(s, s1, qps, superblock_count);
1985 return update_frames(dst);
1988 static int vp3_decode_frame(AVCodecContext *avctx,
1989 void *data, int *got_frame,
1992 const uint8_t *buf = avpkt->data;
1993 int buf_size = avpkt->size;
1994 Vp3DecodeContext *s = avctx->priv_data;
1998 init_get_bits(&gb, buf, buf_size * 8);
2000 #if CONFIG_THEORA_DECODER
2001 if (s->theora && get_bits1(&gb)) {
2002 int type = get_bits(&gb, 7);
2003 skip_bits_long(&gb, 6*8); /* "theora" */
2005 if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
2006 av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2007 return AVERROR_PATCHWELCOME;
2010 vp3_decode_end(avctx);
2011 ret = theora_decode_header(avctx, &gb);
2014 vp3_decode_end(avctx);
2016 ret = vp3_decode_init(avctx);
2018 } else if (type == 2) {
2019 ret = theora_decode_tables(avctx, &gb);
2021 vp3_decode_end(avctx);
2023 ret = vp3_decode_init(avctx);
2027 av_log(avctx, AV_LOG_ERROR,
2028 "Header packet passed to frame decoder, skipping\n");
2033 s->keyframe = !get_bits1(&gb);
2034 if (!s->all_fragments) {
2035 av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
2040 for (i = 0; i < 3; i++)
2041 s->last_qps[i] = s->qps[i];
2045 s->qps[s->nqps++] = get_bits(&gb, 6);
2046 } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
2047 for (i = s->nqps; i < 3; i++)
2050 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2051 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2052 s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
2054 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
2055 avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
2056 : AVDISCARD_NONKEY);
2058 if (s->qps[0] != s->last_qps[0])
2059 init_loop_filter(s);
2061 for (i = 0; i < s->nqps; i++)
2062 // reinit all dequantizers if the first one changed, because
2063 // the DC of the first quantizer must be used for all matrices
2064 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2065 init_dequantizer(s, i);
2067 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2070 s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2071 : AV_PICTURE_TYPE_P;
2072 s->current_frame.f->key_frame = s->keyframe;
2073 if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0)
2076 if (!s->edge_emu_buffer)
2077 s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
2081 skip_bits(&gb, 4); /* width code */
2082 skip_bits(&gb, 4); /* height code */
2084 s->version = get_bits(&gb, 5);
2085 if (avctx->frame_number == 0)
2086 av_log(s->avctx, AV_LOG_DEBUG,
2087 "VP version: %d\n", s->version);
2090 if (s->version || s->theora) {
2092 av_log(s->avctx, AV_LOG_ERROR,
2093 "Warning, unsupported keyframe coding type?!\n");
2094 skip_bits(&gb, 2); /* reserved? */
2097 if (!s->golden_frame.f->data[0]) {
2098 av_log(s->avctx, AV_LOG_WARNING,
2099 "vp3: first frame not a keyframe\n");
2101 s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
2102 if (ff_thread_get_buffer(avctx, &s->golden_frame,
2103 AV_GET_BUFFER_FLAG_REF) < 0)
2105 ff_thread_release_buffer(avctx, &s->last_frame);
2106 if ((ret = ff_thread_ref_frame(&s->last_frame,
2107 &s->golden_frame)) < 0)
2109 ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
2113 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
2114 ff_thread_finish_setup(avctx);
2116 if (unpack_superblocks(s, &gb)) {
2117 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2120 if (unpack_modes(s, &gb)) {
2121 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2124 if (unpack_vectors(s, &gb)) {
2125 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2128 if (unpack_block_qpis(s, &gb)) {
2129 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2132 if (unpack_dct_coeffs(s, &gb)) {
2133 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2137 for (i = 0; i < 3; i++) {
2138 int height = s->height >> (i && s->chroma_y_shift);
2139 if (s->flipped_image)
2140 s->data_offset[i] = 0;
2142 s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
2145 s->last_slice_end = 0;
2146 for (i = 0; i < s->c_superblock_height; i++)
2149 // filter the last row
2150 for (i = 0; i < 3; i++) {
2151 int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
2152 apply_loop_filter(s, i, row, row + 1);
2154 vp3_draw_horiz_band(s, s->avctx->height);
2156 if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
2160 if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
2161 ret = update_frames(avctx);
2169 ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2171 if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
2172 av_frame_unref(s->current_frame.f);
2177 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2179 Vp3DecodeContext *s = avctx->priv_data;
2181 if (get_bits1(gb)) {
2183 if (s->entries >= 32) { /* overflow */
2184 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2187 token = get_bits(gb, 5);
2188 av_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
2189 s->hti, s->hbits, token, s->entries, s->huff_code_size);
2190 s->huffman_table[s->hti][token][0] = s->hbits;
2191 s->huffman_table[s->hti][token][1] = s->huff_code_size;
2194 if (s->huff_code_size >= 32) { /* overflow */
2195 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
2198 s->huff_code_size++;
2200 if (read_huffman_tree(avctx, gb))
2203 if (read_huffman_tree(avctx, gb))
2206 s->huff_code_size--;
2211 static int vp3_init_thread_copy(AVCodecContext *avctx)
2213 Vp3DecodeContext *s = avctx->priv_data;
2215 s->superblock_coding = NULL;
2216 s->all_fragments = NULL;
2217 s->coded_fragment_list[0] = NULL;
2218 s->dct_tokens_base = NULL;
2219 s->superblock_fragments = NULL;
2220 s->macroblock_coding = NULL;
2221 s->motion_val[0] = NULL;
2222 s->motion_val[1] = NULL;
2223 s->edge_emu_buffer = NULL;
2225 return init_frames(s);
2228 #if CONFIG_THEORA_DECODER
2229 static const enum AVPixelFormat theora_pix_fmts[4] = {
2230 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
2233 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2235 Vp3DecodeContext *s = avctx->priv_data;
2236 int visible_width, visible_height, colorspace;
2237 int offset_x = 0, offset_y = 0;
2239 AVRational fps, aspect;
2241 s->theora = get_bits_long(gb, 24);
2242 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2244 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
2245 * but previous versions have the image flipped relative to vp3 */
2246 if (s->theora < 0x030200) {
2247 s->flipped_image = 1;
2248 av_log(avctx, AV_LOG_DEBUG,
2249 "Old (<alpha3) Theora bitstream, flipped image\n");
2253 s->width = get_bits(gb, 16) << 4;
2255 s->height = get_bits(gb, 16) << 4;
2257 if (s->theora >= 0x030200) {
2258 visible_width = get_bits_long(gb, 24);
2259 visible_height = get_bits_long(gb, 24);
2261 offset_x = get_bits(gb, 8); /* offset x */
2262 offset_y = get_bits(gb, 8); /* offset y, from bottom */
2265 fps.num = get_bits_long(gb, 32);
2266 fps.den = get_bits_long(gb, 32);
2267 if (fps.num && fps.den) {
2268 if (fps.num < 0 || fps.den < 0) {
2269 av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
2270 return AVERROR_INVALIDDATA;
2272 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
2273 fps.den, fps.num, 1 << 30);
2276 aspect.num = get_bits_long(gb, 24);
2277 aspect.den = get_bits_long(gb, 24);
2278 if (aspect.num && aspect.den) {
2279 av_reduce(&avctx->sample_aspect_ratio.num,
2280 &avctx->sample_aspect_ratio.den,
2281 aspect.num, aspect.den, 1 << 30);
2284 if (s->theora < 0x030200)
2285 skip_bits(gb, 5); /* keyframe frequency force */
2286 colorspace = get_bits(gb, 8);
2287 skip_bits(gb, 24); /* bitrate */
2289 skip_bits(gb, 6); /* quality hint */
2291 if (s->theora >= 0x030200) {
2292 skip_bits(gb, 5); /* keyframe frequency force */
2293 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
2294 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
2295 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
2296 return AVERROR_INVALIDDATA;
2298 skip_bits(gb, 3); /* reserved */
2301 // align_get_bits(gb);
2303 if (visible_width <= s->width && visible_width > s->width - 16 &&
2304 visible_height <= s->height && visible_height > s->height - 16 &&
2305 !offset_x && (offset_y == s->height - visible_height))
2306 ret = ff_set_dimensions(avctx, visible_width, visible_height);
2308 ret = ff_set_dimensions(avctx, s->width, s->height);
2312 if (colorspace == 1)
2313 avctx->color_primaries = AVCOL_PRI_BT470M;
2314 else if (colorspace == 2)
2315 avctx->color_primaries = AVCOL_PRI_BT470BG;
2317 if (colorspace == 1 || colorspace == 2) {
2318 avctx->colorspace = AVCOL_SPC_BT470BG;
2319 avctx->color_trc = AVCOL_TRC_BT709;
2325 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2327 Vp3DecodeContext *s = avctx->priv_data;
2328 int i, n, matrices, inter, plane;
2330 if (s->theora >= 0x030200) {
2331 n = get_bits(gb, 3);
2332 /* loop filter limit values table */
2334 for (i = 0; i < 64; i++)
2335 s->filter_limit_values[i] = get_bits(gb, n);
2338 if (s->theora >= 0x030200)
2339 n = get_bits(gb, 4) + 1;
2342 /* quality threshold table */
2343 for (i = 0; i < 64; i++)
2344 s->coded_ac_scale_factor[i] = get_bits(gb, n);
2346 if (s->theora >= 0x030200)
2347 n = get_bits(gb, 4) + 1;
2350 /* dc scale factor table */
2351 for (i = 0; i < 64; i++)
2352 s->coded_dc_scale_factor[i] = get_bits(gb, n);
2354 if (s->theora >= 0x030200)
2355 matrices = get_bits(gb, 9) + 1;
2359 if (matrices > 384) {
2360 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
2364 for (n = 0; n < matrices; n++)
2365 for (i = 0; i < 64; i++)
2366 s->base_matrix[n][i] = get_bits(gb, 8);
2368 for (inter = 0; inter <= 1; inter++) {
2369 for (plane = 0; plane <= 2; plane++) {
2371 if (inter || plane > 0)
2372 newqr = get_bits1(gb);
2375 if (inter && get_bits1(gb)) {
2379 qtj = (3 * inter + plane - 1) / 3;
2380 plj = (plane + 2) % 3;
2382 s->qr_count[inter][plane] = s->qr_count[qtj][plj];
2383 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
2384 sizeof(s->qr_size[0][0]));
2385 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
2386 sizeof(s->qr_base[0][0]));
2392 i = get_bits(gb, av_log2(matrices - 1) + 1);
2393 if (i >= matrices) {
2394 av_log(avctx, AV_LOG_ERROR,
2395 "invalid base matrix index\n");
2398 s->qr_base[inter][plane][qri] = i;
2401 i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
2402 s->qr_size[inter][plane][qri++] = i;
2407 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
2410 s->qr_count[inter][plane] = qri;
2415 /* Huffman tables */
2416 for (s->hti = 0; s->hti < 80; s->hti++) {
2418 s->huff_code_size = 1;
2419 if (!get_bits1(gb)) {
2421 if (read_huffman_tree(avctx, gb))
2424 if (read_huffman_tree(avctx, gb))
2429 s->theora_tables = 1;
2434 static av_cold int theora_decode_init(AVCodecContext *avctx)
2436 Vp3DecodeContext *s = avctx->priv_data;
2439 uint8_t *header_start[3];
2443 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2447 if (!avctx->extradata_size) {
2448 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
2452 if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2453 42, header_start, header_len) < 0) {
2454 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2458 for (i = 0; i < 3; i++) {
2459 if (header_len[i] <= 0)
2461 init_get_bits(&gb, header_start[i], header_len[i] * 8);
2463 ptype = get_bits(&gb, 8);
2465 if (!(ptype & 0x80)) {
2466 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
2470 // FIXME: Check for this as well.
2471 skip_bits_long(&gb, 6 * 8); /* "theora" */
2475 if (theora_decode_header(avctx, &gb) < 0)
2479 // FIXME: is this needed? it breaks sometimes
2480 // theora_decode_comments(avctx, gb);
2483 if (theora_decode_tables(avctx, &gb))
2487 av_log(avctx, AV_LOG_ERROR,
2488 "Unknown Theora config packet: %d\n", ptype & ~0x80);
2491 if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
2492 av_log(avctx, AV_LOG_WARNING,
2493 "%d bits left in packet %X\n",
2494 8 * header_len[i] - get_bits_count(&gb), ptype);
2495 if (s->theora < 0x030200)
2499 return vp3_decode_init(avctx);
2502 AVCodec ff_theora_decoder = {
2504 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2505 .type = AVMEDIA_TYPE_VIDEO,
2506 .id = AV_CODEC_ID_THEORA,
2507 .priv_data_size = sizeof(Vp3DecodeContext),
2508 .init = theora_decode_init,
2509 .close = vp3_decode_end,
2510 .decode = vp3_decode_frame,
2511 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
2512 CODEC_CAP_FRAME_THREADS,
2513 .flush = vp3_decode_flush,
2514 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2515 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2519 AVCodec ff_vp3_decoder = {
2521 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2522 .type = AVMEDIA_TYPE_VIDEO,
2523 .id = AV_CODEC_ID_VP3,
2524 .priv_data_size = sizeof(Vp3DecodeContext),
2525 .init = vp3_decode_init,
2526 .close = vp3_decode_end,
2527 .decode = vp3_decode_frame,
2528 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
2529 CODEC_CAP_FRAME_THREADS,
2530 .flush = vp3_decode_flush,
2531 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2532 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),