vp3: remove unneeded error, this is internal and doesn't happen
[libav.git] / libavcodec / vp3.c
CommitLineData
d86053a4 1/*
67335dbc 2 * Copyright (C) 2003-2004 the ffmpeg project
d86053a4 3 *
b78e7197
DB
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
d86053a4
MM
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
d86053a4 10 *
b78e7197 11 * FFmpeg is distributed in the hope that it will be useful,
d86053a4
MM
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
b78e7197 17 * License along with FFmpeg; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d86053a4
MM
19 */
20
21/**
bad5537e 22 * @file libavcodec/vp3.c
d86053a4 23 * On2 VP3 Video Decoder
0ad72bdd
MM
24 *
25 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
26 * For more information about the VP3 coding process, visit:
9db5bdfa 27 * http://wiki.multimedia.cx/index.php?title=On2_VP3
0ad72bdd
MM
28 *
29 * Theora decoder by Alex Beregszaszi
d86053a4
MM
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
d86053a4 35
d86053a4
MM
36#include "avcodec.h"
37#include "dsputil.h"
9106a698 38#include "get_bits.h"
d86053a4
MM
39
40#include "vp3data.h"
da91ed59 41#include "xiph.h"
d86053a4
MM
42
43#define FRAGMENT_PIXELS 8
44
0efbd068
MM
45static av_cold int vp3_decode_end(AVCodecContext *avctx);
46
7beddb12 47//FIXME split things out into their own arrays
d86053a4 48typedef struct Vp3Fragment {
c72625f2 49 int16_t dc;
288774bb 50 uint8_t coding_method;
288774bb
MN
51 int8_t motion_x;
52 int8_t motion_y;
f2264fa5 53 uint8_t qpi;
d86053a4
MM
54} Vp3Fragment;
55
56#define SB_NOT_CODED 0
57#define SB_PARTIALLY_CODED 1
58#define SB_FULLY_CODED 2
59
ecb51b25
DC
60// This is the maximum length of a single long bit run that can be encoded
61// for superblock coding or block qps. Theora special-cases this to read a
62// bit instead of flipping the current bit to allow for runs longer than 4129.
63#define MAXIMUM_LONG_BIT_RUN 4129
64
d86053a4
MM
65#define MODE_INTER_NO_MV 0
66#define MODE_INTRA 1
67#define MODE_INTER_PLUS_MV 2
68#define MODE_INTER_LAST_MV 3
69#define MODE_INTER_PRIOR_LAST 4
70#define MODE_USING_GOLDEN 5
71#define MODE_GOLDEN_MV 6
72#define MODE_INTER_FOURMV 7
73#define CODING_MODE_COUNT 8
74
75/* special internal mode */
76#define MODE_COPY 8
77
78/* There are 6 preset schemes, plus a free-form scheme */
e8e47435 79static const int ModeAlphabet[6][CODING_MODE_COUNT] =
d86053a4 80{
d86053a4 81 /* scheme 1: Last motion vector dominates */
115329f1 82 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
d86053a4 83 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
115329f1 84 MODE_INTRA, MODE_USING_GOLDEN,
d86053a4
MM
85 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
86
87 /* scheme 2 */
115329f1 88 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
d86053a4 89 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
115329f1 90 MODE_INTRA, MODE_USING_GOLDEN,
d86053a4
MM
91 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
92
93 /* scheme 3 */
115329f1 94 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
d86053a4 95 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
115329f1 96 MODE_INTRA, MODE_USING_GOLDEN,
d86053a4
MM
97 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
98
99 /* scheme 4 */
115329f1 100 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
d86053a4 101 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
115329f1 102 MODE_INTRA, MODE_USING_GOLDEN,
d86053a4
MM
103 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
104
105 /* scheme 5: No motion vector dominates */
115329f1 106 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
d86053a4 107 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
115329f1 108 MODE_INTRA, MODE_USING_GOLDEN,
d86053a4
MM
109 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
110
111 /* scheme 6 */
115329f1 112 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
d86053a4 113 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
115329f1 114 MODE_INTER_PLUS_MV, MODE_INTRA,
d86053a4
MM
115 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
116
117};
118
7a095ea6
DC
119static const uint8_t hilbert_offset[16][2] = {
120 {0,0}, {1,0}, {1,1}, {0,1},
121 {0,2}, {0,3}, {1,3}, {1,2},
122 {2,2}, {2,3}, {3,3}, {3,2},
123 {3,1}, {2,1}, {2,0}, {3,0}
124};
125
d86053a4
MM
126#define MIN_DEQUANT_VAL 2
127
128typedef struct Vp3DecodeContext {
129 AVCodecContext *avctx;
f44ee2c3 130 int theora, theora_tables;
3c3f113e 131 int version;
d86053a4 132 int width, height;
d86053a4
MM
133 AVFrame golden_frame;
134 AVFrame last_frame;
135 AVFrame current_frame;
136 int keyframe;
137 DSPContext dsp;
9a7ad925 138 int flipped_image;
a8de3901 139 int last_slice_end;
d86053a4 140
f2264fa5
DC
141 int qps[3];
142 int nqps;
143 int last_qps[3];
d86053a4
MM
144
145 int superblock_count;
892fc83e
MM
146 int y_superblock_width;
147 int y_superblock_height;
35c28d23 148 int y_superblock_count;
892fc83e
MM
149 int c_superblock_width;
150 int c_superblock_height;
35c28d23 151 int c_superblock_count;
d86053a4
MM
152 int u_superblock_start;
153 int v_superblock_start;
154 unsigned char *superblock_coding;
155
156 int macroblock_count;
157 int macroblock_width;
158 int macroblock_height;
159
160 int fragment_count;
57783884
DC
161 int fragment_width[2];
162 int fragment_height[2];
d86053a4
MM
163
164 Vp3Fragment *all_fragments;
1abbf64e 165 int fragment_start[3];
735acf56 166 int data_offset[3];
115329f1 167
36af0c95 168 ScanTable scantable;
115329f1 169
f44ee2c3
AB
170 /* tables */
171 uint16_t coded_dc_scale_factor[64];
67335dbc 172 uint32_t coded_ac_scale_factor[64];
ae1dd8e1
MN
173 uint8_t base_matrix[384][64];
174 uint8_t qr_count[2][3];
175 uint8_t qr_size [2][3][64];
176 uint16_t qr_base[2][3][64];
d86053a4 177
c72625f2
DC
178 /**
179 * This is a list of all tokens in bitstream order. Reordering takes place
180 * by pulling from each level during IDCT. As a consequence, IDCT must be
181 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
182 * otherwise. The 32 different tokens with up to 12 bits of extradata are
183 * collapsed into 3 types, packed as follows:
184 * (from the low to high bits)
185 *
186 * 2 bits: type (0,1,2)
187 * 0: EOB run, 14 bits for run length (12 needed)
188 * 1: zero run, 7 bits for run length
189 * 7 bits for the next coefficient (3 needed)
190 * 2: coefficient, 14 bits (11 needed)
191 *
192 * Coefficients are signed, so are packed in the highest bits for automatic
193 * sign extension.
194 */
195 int16_t *dct_tokens[3][64];
196 int16_t *dct_tokens_base;
197#define TOKEN_EOB(eob_run) ((eob_run) << 2)
198#define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
199#define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
200
201 /**
202 * number of blocks that contain DCT coefficients at the given level or higher
203 */
204 int num_coded_frags[3][64];
205 int total_num_coded_frags;
206
f4433de9 207 /* this is a list of indexes into the all_fragments array indicating
d86053a4 208 * which of the fragments are coded */
c72625f2 209 int *coded_fragment_list[3];
098523eb 210
d86053a4
MM
211 VLC dc_vlc[16];
212 VLC ac_vlc_1[16];
213 VLC ac_vlc_2[16];
214 VLC ac_vlc_3[16];
215 VLC ac_vlc_4[16];
216
0ad72bdd
MM
217 VLC superblock_run_length_vlc;
218 VLC fragment_run_length_vlc;
219 VLC mode_code_vlc;
220 VLC motion_vector_vlc;
221
38acbc3c
MM
222 /* these arrays need to be on 16-byte boundaries since SSE2 operations
223 * index into them */
84dc2d8a 224 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane]
d86053a4
MM
225
226 /* This table contains superblock_count * 16 entries. Each set of 16
f4433de9 227 * numbers corresponds to the fragment indexes 0..15 of the superblock.
d86053a4
MM
228 * An entry will be -1 to indicate that no entry corresponds to that
229 * index. */
230 int *superblock_fragments;
231
115329f1 232 /* This is an array that indicates how a particular macroblock
74c0ac12 233 * is coded. */
96a7e73b 234 unsigned char *macroblock_coding;
d86053a4 235
a2f11b3c 236 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
191e8ca7 237 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
39922395 238
f44b08a5
MM
239 /* Huffman decode */
240 int hti;
241 unsigned int hbits;
242 int entries;
243 int huff_code_size;
244 uint16_t huffman_table[80][32][2];
245
51ace577 246 uint8_t filter_limit_values[64];
84dc2d8a 247 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
d86053a4
MM
248} Vp3DecodeContext;
249
250/************************************************************************
251 * VP3 specific functions
252 ************************************************************************/
253
254/*
255 * This function sets up all of the various blocks mappings:
256 * superblocks <-> fragments, macroblocks <-> fragments,
257 * superblocks <-> macroblocks
892fc83e
MM
258 *
259 * Returns 0 is successful; returns 1 if *anything* went wrong.
d86053a4 260 */
115329f1 261static int init_block_mapping(Vp3DecodeContext *s)
d86053a4 262{
a16389c1
DC
263 int sb_x, sb_y, plane;
264 int x, y, i, j = 0;
265
266 for (plane = 0; plane < 3; plane++) {
267 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
268 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
57783884
DC
269 int frag_width = s->fragment_width[!!plane];
270 int frag_height = s->fragment_height[!!plane];
a16389c1
DC
271
272 for (sb_y = 0; sb_y < sb_height; sb_y++)
273 for (sb_x = 0; sb_x < sb_width; sb_x++)
274 for (i = 0; i < 16; i++) {
275 x = 4*sb_x + hilbert_offset[i][0];
276 y = 4*sb_y + hilbert_offset[i][1];
277
278 if (x < frag_width && y < frag_height)
279 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
280 else
281 s->superblock_fragments[j++] = -1;
282 }
d86053a4
MM
283 }
284
892fc83e 285 return 0; /* successful path out */
d86053a4
MM
286}
287
288/*
f44b08a5 289 * This function sets up the dequantization tables used for a particular
d86053a4
MM
290 * frame.
291 */
f2264fa5 292static void init_dequantizer(Vp3DecodeContext *s, int qpi)
d86053a4 293{
f2264fa5
DC
294 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
295 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
36c32bdd 296 int i, plane, inter, qri, bmi, bmj, qistart;
d86053a4 297
ae1dd8e1
MN
298 for(inter=0; inter<2; inter++){
299 for(plane=0; plane<3; plane++){
300 int sum=0;
301 for(qri=0; qri<s->qr_count[inter][plane]; qri++){
302 sum+= s->qr_size[inter][plane][qri];
f2264fa5 303 if(s->qps[qpi] <= sum)
ae1dd8e1
MN
304 break;
305 }
306 qistart= sum - s->qr_size[inter][plane][qri];
307 bmi= s->qr_base[inter][plane][qri ];
308 bmj= s->qr_base[inter][plane][qri+1];
309 for(i=0; i<64; i++){
f2264fa5
DC
310 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
311 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
ae1dd8e1
MN
312 + s->qr_size[inter][plane][qri])
313 / (2*s->qr_size[inter][plane][qri]);
314
a14ab4e4 315 int qmin= 8<<(inter + !i);
ae1dd8e1
MN
316 int qscale= i ? ac_scale_factor : dc_scale_factor;
317
f2264fa5 318 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
ae1dd8e1 319 }
f2264fa5
DC
320 // all DC coefficients use the same quant so as not to interfere with DC prediction
321 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
ae1dd8e1 322 }
d86053a4 323 }
115329f1 324
f2264fa5 325 memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512); //FIXME finetune
d86053a4
MM
326}
327
328/*
f44b08a5
MM
329 * This function initializes the loop filter boundary limits if the frame's
330 * quality index is different from the previous frame's.
7fa5f999
RD
331 *
332 * The filter_limit_values may not be larger than 127.
f44b08a5
MM
333 */
334static void init_loop_filter(Vp3DecodeContext *s)
335{
336 int *bounding_values= s->bounding_values_array+127;
337 int filter_limit;
338 int x;
7fa5f999 339 int value;
f44b08a5 340
f2264fa5 341 filter_limit = s->filter_limit_values[s->qps[0]];
f44b08a5
MM
342
343 /* set up the bounding values */
344 memset(s->bounding_values_array, 0, 256 * sizeof(int));
345 for (x = 0; x < filter_limit; x++) {
f44b08a5
MM
346 bounding_values[-x] = -x;
347 bounding_values[x] = x;
f44b08a5 348 }
7fa5f999
RD
349 for (x = value = filter_limit; x < 128 && value; x++, value--) {
350 bounding_values[ x] = value;
351 bounding_values[-x] = -value;
352 }
353 if (value)
354 bounding_values[128] = value;
357f45d9 355 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
f44b08a5
MM
356}
357
358/*
115329f1 359 * This function unpacks all of the superblock/macroblock/fragment coding
d86053a4
MM
360 * information from the bitstream.
361 */
892fc83e 362static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
d86053a4 363{
e2720b6b 364 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
d86053a4
MM
365 int bit = 0;
366 int current_superblock = 0;
367 int current_run = 0;
855c720c 368 int num_partial_superblocks = 0;
d86053a4
MM
369
370 int i, j;
371 int current_fragment;
35c28d23 372 int plane;
d86053a4 373
d86053a4 374 if (s->keyframe) {
d86053a4
MM
375 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
376
377 } else {
378
379 /* unpack the list of partially-coded superblocks */
5fc32c27 380 bit = get_bits1(gb);
d86053a4 381 while (current_superblock < s->superblock_count) {
115329f1 382 current_run = get_vlc2(gb,
33dbc1b7
DC
383 s->superblock_run_length_vlc.table, 6, 2) + 1;
384 if (current_run == 34)
d8278bab 385 current_run += get_bits(gb, 12);
d86053a4 386
33dbc1b7
DC
387 if (current_superblock + current_run > s->superblock_count) {
388 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
389 return -1;
390 }
391
392 memset(s->superblock_coding + current_superblock, bit, current_run);
393
394 current_superblock += current_run;
855c720c
DC
395 if (bit)
396 num_partial_superblocks += current_run;
33dbc1b7 397
a8926476
DC
398 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
399 bit = get_bits1(gb);
400 else
33dbc1b7 401 bit ^= 1;
d86053a4
MM
402 }
403
404 /* unpack the list of fully coded superblocks if any of the blocks were
405 * not marked as partially coded in the previous step */
855c720c
DC
406 if (num_partial_superblocks < s->superblock_count) {
407 int superblocks_decoded = 0;
d86053a4
MM
408
409 current_superblock = 0;
5fc32c27 410 bit = get_bits1(gb);
855c720c 411 while (superblocks_decoded < s->superblock_count - num_partial_superblocks) {
115329f1 412 current_run = get_vlc2(gb,
855c720c
DC
413 s->superblock_run_length_vlc.table, 6, 2) + 1;
414 if (current_run == 34)
d8278bab 415 current_run += get_bits(gb, 12);
855c720c
DC
416
417 for (j = 0; j < current_run; current_superblock++) {
418 if (current_superblock >= s->superblock_count) {
419 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
420 return -1;
d86053a4 421 }
855c720c
DC
422
423 /* skip any superblocks already marked as partially coded */
424 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
b5da3635 425 s->superblock_coding[current_superblock] = 2*bit;
855c720c
DC
426 j++;
427 }
d86053a4 428 }
855c720c
DC
429 superblocks_decoded += current_run;
430
a8926476
DC
431 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
432 bit = get_bits1(gb);
433 else
855c720c 434 bit ^= 1;
d86053a4
MM
435 }
436 }
437
438 /* if there were partial blocks, initialize bitstream for
439 * unpacking fragment codings */
855c720c 440 if (num_partial_superblocks) {
d86053a4
MM
441
442 current_run = 0;
5fc32c27 443 bit = get_bits1(gb);
115329f1 444 /* toggle the bit because as soon as the first run length is
d86053a4
MM
445 * fetched the bit will be toggled again */
446 bit ^= 1;
447 }
448 }
449
450 /* figure out which fragments are coded; iterate through each
451 * superblock (all planes) */
c72625f2 452 s->total_num_coded_frags = 0;
96a7e73b 453 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
35c28d23
DC
454
455 for (plane = 0; plane < 3; plane++) {
e2720b6b 456 int sb_start = superblock_starts[plane];
35c28d23 457 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
c72625f2 458 int num_coded_frags = 0;
35c28d23
DC
459
460 for (i = sb_start; i < sb_end; i++) {
d86053a4
MM
461
462 /* iterate through all 16 fragments in a superblock */
463 for (j = 0; j < 16; j++) {
464
465 /* if the fragment is in bounds, check its coding status */
466 current_fragment = s->superblock_fragments[i * 16 + j];
467 if (current_fragment != -1) {
6cb35b45 468 int coded = s->superblock_coding[i];
d86053a4 469
6cb35b45 470 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
d86053a4
MM
471
472 /* fragment may or may not be coded; this is the case
473 * that cares about the fragment coding runs */
b5da3635 474 if (current_run-- == 0) {
d86053a4 475 bit ^= 1;
115329f1 476 current_run = get_vlc2(gb,
b5da3635 477 s->fragment_run_length_vlc.table, 5, 2);
d86053a4 478 }
6cb35b45
DC
479 coded = bit;
480 }
d86053a4 481
6cb35b45 482 if (coded) {
115329f1 483 /* default mode; actual mode will be decoded in
22493ab9 484 * the next phase */
115329f1 485 s->all_fragments[current_fragment].coding_method =
d86053a4 486 MODE_INTER_NO_MV;
c72625f2 487 s->coded_fragment_list[plane][num_coded_frags++] =
d86053a4 488 current_fragment;
d86053a4
MM
489 } else {
490 /* not coded; copy this fragment from the prior frame */
491 s->all_fragments[current_fragment].coding_method =
492 MODE_COPY;
d86053a4 493 }
d86053a4
MM
494 }
495 }
496 }
c72625f2
DC
497 s->total_num_coded_frags += num_coded_frags;
498 for (i = 0; i < 64; i++)
499 s->num_coded_frags[plane][i] = num_coded_frags;
500 if (plane < 2)
501 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
35c28d23 502 }
892fc83e 503 return 0;
d86053a4
MM
504}
505
506/*
507 * This function unpacks all the coding mode data for individual macroblocks
508 * from the bitstream.
509 */
892fc83e 510static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
d86053a4 511{
19cd517d 512 int i, j, k, sb_x, sb_y;
d86053a4
MM
513 int scheme;
514 int current_macroblock;
515 int current_fragment;
516 int coding_mode;
e8e47435 517 int custom_mode_alphabet[CODING_MODE_COUNT];
7c2e31d1 518 const int *alphabet;
d86053a4 519
d86053a4 520 if (s->keyframe) {
d86053a4
MM
521 for (i = 0; i < s->fragment_count; i++)
522 s->all_fragments[i].coding_method = MODE_INTRA;
523
524 } else {
525
526 /* fetch the mode coding scheme for this frame */
527 scheme = get_bits(gb, 3);
d86053a4
MM
528
529 /* is it a custom coding scheme? */
530 if (scheme == 0) {
d86053a4 531 for (i = 0; i < 8; i++)
2c823b3c
AC
532 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
533 for (i = 0; i < 8; i++)
e8e47435 534 custom_mode_alphabet[get_bits(gb, 3)] = i;
7c2e31d1
DC
535 alphabet = custom_mode_alphabet;
536 } else
537 alphabet = ModeAlphabet[scheme-1];
d86053a4 538
d86053a4
MM
539 /* iterate through all of the macroblocks that contain 1 or more
540 * coded fragments */
19cd517d
DC
541 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
542 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
d86053a4
MM
543
544 for (j = 0; j < 4; j++) {
19cd517d
DC
545 int mb_x = 2*sb_x + (j>>1);
546 int mb_y = 2*sb_y + (((j>>1)+j)&1);
547 current_macroblock = mb_y * s->macroblock_width + mb_x;
548
15675ce6 549 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
d86053a4
MM
550 continue;
551
ea676144
DC
552#define BLOCK_X (2*mb_x + (k&1))
553#define BLOCK_Y (2*mb_y + (k>>1))
15675ce6
DC
554 /* coding modes are only stored if the macroblock has at least one
555 * luma block coded, otherwise it must be INTER_NO_MV */
556 for (k = 0; k < 4; k++) {
57783884 557 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
15675ce6
DC
558 if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
559 break;
560 }
561 if (k == 4) {
562 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
563 continue;
564 }
ea676144 565
d86053a4
MM
566 /* mode 7 means get 3 bits for each coding mode */
567 if (scheme == 7)
568 coding_mode = get_bits(gb, 3);
569 else
7c2e31d1 570 coding_mode = alphabet
0ad72bdd 571 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
d86053a4 572
96a7e73b 573 s->macroblock_coding[current_macroblock] = coding_mode;
ea676144 574 for (k = 0; k < 4; k++) {
115329f1 575 current_fragment =
57783884 576 BLOCK_Y*s->fragment_width[0] + BLOCK_X;
ea676144
DC
577 if (s->all_fragments[current_fragment].coding_method !=
578 MODE_COPY)
579 s->all_fragments[current_fragment].coding_method =
580 coding_mode;
581 }
582 for (k = 0; k < 2; k++) {
583 current_fragment = s->fragment_start[k+1] +
57783884 584 mb_y*s->fragment_width[1] + mb_x;
115329f1 585 if (s->all_fragments[current_fragment].coding_method !=
d86053a4
MM
586 MODE_COPY)
587 s->all_fragments[current_fragment].coding_method =
588 coding_mode;
589 }
d86053a4 590 }
19cd517d 591 }
d86053a4
MM
592 }
593 }
892fc83e
MM
594
595 return 0;
44ae98dd
MM
596}
597
598/*
d86053a4
MM
599 * This function unpacks all the motion vectors for the individual
600 * macroblocks from the bitstream.
601 */
892fc83e 602static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
d86053a4 603{
eb691ef2 604 int j, k, sb_x, sb_y;
d86053a4 605 int coding_mode;
1c183aa4
DC
606 int motion_x[4];
607 int motion_y[4];
d86053a4
MM
608 int last_motion_x = 0;
609 int last_motion_y = 0;
610 int prior_last_motion_x = 0;
611 int prior_last_motion_y = 0;
612 int current_macroblock;
613 int current_fragment;
614
6599e2a7 615 if (s->keyframe)
6298f49f 616 return 0;
10f38380 617
1ae4518d
DC
618 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
619 coding_mode = get_bits1(gb);
d86053a4 620
1ae4518d
DC
621 /* iterate through all of the macroblocks that contain 1 or more
622 * coded fragments */
19cd517d
DC
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++) {
d86053a4 625
1ae4518d 626 for (j = 0; j < 4; j++) {
19cd517d
DC
627 int mb_x = 2*sb_x + (j>>1);
628 int mb_y = 2*sb_y + (((j>>1)+j)&1);
629 current_macroblock = mb_y * s->macroblock_width + mb_x;
630
631 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
1ae4518d
DC
632 (s->macroblock_coding[current_macroblock] == MODE_COPY))
633 continue;
d86053a4 634
1ae4518d
DC
635 switch (s->macroblock_coding[current_macroblock]) {
636
637 case MODE_INTER_PLUS_MV:
638 case MODE_GOLDEN_MV:
639 /* all 6 fragments use the same motion vector */
640 if (coding_mode == 0) {
641 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
642 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
643 } else {
644 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
645 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
892fc83e 646 }
7f9926a4 647
1ae4518d
DC
648 /* vector maintenance, only on MODE_INTER_PLUS_MV */
649 if (s->macroblock_coding[current_macroblock] ==
650 MODE_INTER_PLUS_MV) {
e32e2d56
AJ
651 prior_last_motion_x = last_motion_x;
652 prior_last_motion_y = last_motion_y;
1ae4518d
DC
653 last_motion_x = motion_x[0];
654 last_motion_y = motion_y[0];
655 }
656 break;
657
658 case MODE_INTER_FOURMV:
659 /* vector maintenance */
660 prior_last_motion_x = last_motion_x;
661 prior_last_motion_y = last_motion_y;
662
663 /* fetch 4 vectors from the bitstream, one for each
664 * Y fragment, then average for the C fragment vectors */
1ae4518d 665 for (k = 0; k < 4; k++) {
57783884 666 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
eb691ef2 667 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
1ae4518d
DC
668 if (coding_mode == 0) {
669 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
670 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
feaf1a73 671 } else {
1ae4518d
DC
672 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
673 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
feaf1a73 674 }
1ae4518d
DC
675 last_motion_x = motion_x[k];
676 last_motion_y = motion_y[k];
677 } else {
678 motion_x[k] = 0;
679 motion_y[k] = 0;
d86053a4 680 }
1ae4518d 681 }
1ae4518d
DC
682 break;
683
684 case MODE_INTER_LAST_MV:
685 /* all 6 fragments use the last motion vector */
686 motion_x[0] = last_motion_x;
687 motion_y[0] = last_motion_y;
d86053a4 688
1ae4518d
DC
689 /* no vector maintenance (last vector remains the
690 * last vector) */
691 break;
692
693 case MODE_INTER_PRIOR_LAST:
694 /* all 6 fragments use the motion vector prior to the
695 * last motion vector */
696 motion_x[0] = prior_last_motion_x;
697 motion_y[0] = prior_last_motion_y;
d86053a4 698
1ae4518d
DC
699 /* vector maintenance */
700 prior_last_motion_x = last_motion_x;
701 prior_last_motion_y = last_motion_y;
702 last_motion_x = motion_x[0];
703 last_motion_y = motion_y[0];
704 break;
44ae98dd 705
1ae4518d
DC
706 default:
707 /* covers intra, inter without MV, golden without MV */
e6e32bdc
MM
708 motion_x[0] = 0;
709 motion_y[0] = 0;
44ae98dd 710
1ae4518d
DC
711 /* no vector maintenance */
712 break;
713 }
d86053a4 714
1ae4518d 715 /* assign the motion vectors to the correct fragments */
ea676144 716 for (k = 0; k < 4; k++) {
1ae4518d 717 current_fragment =
57783884 718 BLOCK_Y*s->fragment_width[0] + BLOCK_X;
e6e32bdc 719 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
da8af938
MM
720 s->all_fragments[current_fragment].motion_x = motion_x[k];
721 s->all_fragments[current_fragment].motion_y = motion_y[k];
e6e32bdc
MM
722 } else {
723 s->all_fragments[current_fragment].motion_x = motion_x[0];
724 s->all_fragments[current_fragment].motion_y = motion_y[0];
725 }
d86053a4 726 }
1c183aa4
DC
727 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
728 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
729 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
730 }
ea676144
DC
731 for (k = 0; k < 2; k++) {
732 current_fragment = s->fragment_start[k+1] +
57783884 733 mb_y*s->fragment_width[1] + mb_x;
ea676144
DC
734 s->all_fragments[current_fragment].motion_x = motion_x[0];
735 s->all_fragments[current_fragment].motion_y = motion_y[0];
ea676144 736 }
d86053a4 737 }
19cd517d 738 }
1ae4518d 739 }
892fc83e
MM
740
741 return 0;
d86053a4
MM
742}
743
f2264fa5
DC
744static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
745{
746 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
c72625f2 747 int num_blocks = s->total_num_coded_frags;
f2264fa5
DC
748
749 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
750 i = blocks_decoded = num_blocks_at_qpi = 0;
751
752 bit = get_bits1(gb);
753
754 do {
755 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
756 if (run_length == 34)
757 run_length += get_bits(gb, 12);
758 blocks_decoded += run_length;
759
760 if (!bit)
761 num_blocks_at_qpi += run_length;
762
763 for (j = 0; j < run_length; i++) {
c72625f2 764 if (i >= s->total_num_coded_frags)
f2264fa5
DC
765 return -1;
766
c72625f2
DC
767 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
768 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
f2264fa5
DC
769 j++;
770 }
771 }
772
ecb51b25 773 if (run_length == MAXIMUM_LONG_BIT_RUN)
f2264fa5
DC
774 bit = get_bits1(gb);
775 else
776 bit ^= 1;
777 } while (blocks_decoded < num_blocks);
778
779 num_blocks -= num_blocks_at_qpi;
780 }
781
782 return 0;
783}
784
115329f1 785/*
d86053a4
MM
786 * This function is called by unpack_dct_coeffs() to extract the VLCs from
787 * the bitstream. The VLCs encode tokens which are used to unpack DCT
788 * data. This function unpacks all the VLCs for either the Y plane or both
789 * C planes, and is called for DC coefficients or different AC coefficient
790 * levels (since different coefficient types require different VLC tables.
791 *
792 * This function returns a residual eob run. E.g, if a particular token gave
793 * instructions to EOB the next 5 fragments and there were only 2 fragments
794 * left in the current fragment range, 3 would be returned so that it could
795 * be passed into the next call to this same function.
796 */
797static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
798 VLC *table, int coeff_index,
c72625f2 799 int plane,
d86053a4
MM
800 int eob_run)
801{
c72625f2 802 int i, j = 0;
d86053a4 803 int token;
d3076955
MM
804 int zero_run = 0;
805 DCTELEM coeff = 0;
d3076955 806 int bits_to_get;
c72625f2
DC
807 int blocks_ended;
808 int coeff_i = 0;
809 int num_coeffs = s->num_coded_frags[plane][coeff_index];
810 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
d86053a4 811
ee3d7f58 812 /* local references to structure members to avoid repeated deferences */
c72625f2 813 int *coded_fragment_list = s->coded_fragment_list[plane];
ee3d7f58 814 Vp3Fragment *all_fragments = s->all_fragments;
ee3d7f58
MM
815 VLC_TYPE (*vlc_table)[2] = table->table;
816
c72625f2
DC
817 if (num_coeffs < 0)
818 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
819
820 if (eob_run > num_coeffs) {
821 coeff_i = blocks_ended = num_coeffs;
822 eob_run -= num_coeffs;
098523eb 823 } else {
c72625f2
DC
824 coeff_i = blocks_ended = eob_run;
825 eob_run = 0;
74c0ac12
MM
826 }
827
c72625f2
DC
828 // insert fake EOB token to cover the split between planes or zzi
829 if (blocks_ended)
830 dct_tokens[j++] = blocks_ended << 2;
d86053a4 831
f50dafa8 832 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
d86053a4 833 /* decode a VLC into a token */
ee3d7f58 834 token = get_vlc2(gb, vlc_table, 5, 3);
d86053a4 835 /* use the token to get a zero run, a coefficient, and an eob run */
d3076955
MM
836 if (token <= 6) {
837 eob_run = eob_run_base[token];
838 if (eob_run_get_bits[token])
839 eob_run += get_bits(gb, eob_run_get_bits[token]);
c72625f2
DC
840
841 // record only the number of blocks ended in this plane,
842 // any spill will be recorded in the next plane.
843 if (eob_run > num_coeffs - coeff_i) {
844 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
845 blocks_ended += num_coeffs - coeff_i;
846 eob_run -= num_coeffs - coeff_i;
847 coeff_i = num_coeffs;
848 } else {
849 dct_tokens[j++] = TOKEN_EOB(eob_run);
850 blocks_ended += eob_run;
851 coeff_i += eob_run;
852 eob_run = 0;
853 }
d3076955
MM
854 } else {
855 bits_to_get = coeff_get_bits[token];
428984b0
MM
856 if (bits_to_get)
857 bits_to_get = get_bits(gb, bits_to_get);
858 coeff = coeff_tables[token][bits_to_get];
d3076955
MM
859
860 zero_run = zero_run_base[token];
861 if (zero_run_get_bits[token])
862 zero_run += get_bits(gb, zero_run_get_bits[token]);
d86053a4 863
c72625f2
DC
864 if (zero_run) {
865 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
866 } else {
867 // Save DC into the fragment structure. DC prediction is
868 // done in raster order, so the actual DC can't be in with
869 // other tokens. We still need the token in dct_tokens[]
870 // however, or else the structure collapses on itself.
871 if (!coeff_index)
872 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
873
874 dct_tokens[j++] = TOKEN_COEFF(coeff);
875 }
876
877 if (coeff_index + zero_run > 64) {
01f9640b 878 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
c72625f2
DC
879 " %d coeffs left\n", zero_run, 64-coeff_index);
880 zero_run = 64 - coeff_index;
881 }
098523eb 882
c72625f2
DC
883 // zero runs code multiple coefficients,
884 // so don't try to decode coeffs for those higher levels
885 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
886 s->num_coded_frags[plane][i]--;
887 coeff_i++;
888 }
d86053a4
MM
889 }
890
c72625f2
DC
891 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
892 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
893
894 // decrement the number of blocks that have higher coeffecients for each
895 // EOB run at this level
896 if (blocks_ended)
897 for (i = coeff_index+1; i < 64; i++)
898 s->num_coded_frags[plane][i] -= blocks_ended;
899
900 // setup the next buffer
901 if (plane < 2)
902 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
903 else if (coeff_index < 63)
904 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
905
d86053a4
MM
906 return eob_run;
907}
908
138fe832
MM
909static void reverse_dc_prediction(Vp3DecodeContext *s,
910 int first_fragment,
911 int fragment_width,
912 int fragment_height);
d86053a4
MM
913/*
914 * This function unpacks all of the DCT coefficient data from the
915 * bitstream.
916 */
892fc83e 917static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
d86053a4
MM
918{
919 int i;
920 int dc_y_table;
921 int dc_c_table;
922 int ac_y_table;
923 int ac_c_table;
924 int residual_eob_run = 0;
9d8bb031
MM
925 VLC *y_tables[64];
926 VLC *c_tables[64];
d86053a4 927
c72625f2
DC
928 s->dct_tokens[0][0] = s->dct_tokens_base;
929
f4433de9 930 /* fetch the DC table indexes */
d86053a4
MM
931 dc_y_table = get_bits(gb, 4);
932 dc_c_table = get_bits(gb, 4);
933
934 /* unpack the Y plane DC coefficients */
115329f1 935 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
c72625f2 936 0, residual_eob_run);
d86053a4 937
138fe832 938 /* reverse prediction of the Y-plane DC coefficients */
57783884 939 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
138fe832 940
d86053a4 941 /* unpack the C plane DC coefficients */
d86053a4 942 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
c72625f2
DC
943 1, residual_eob_run);
944 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
945 2, residual_eob_run);
d86053a4 946
138fe832
MM
947 /* reverse prediction of the C-plane DC coefficients */
948 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
949 {
950 reverse_dc_prediction(s, s->fragment_start[1],
57783884 951 s->fragment_width[1], s->fragment_height[1]);
138fe832 952 reverse_dc_prediction(s, s->fragment_start[2],
57783884 953 s->fragment_width[1], s->fragment_height[1]);
138fe832
MM
954 }
955
f4433de9 956 /* fetch the AC table indexes */
d86053a4
MM
957 ac_y_table = get_bits(gb, 4);
958 ac_c_table = get_bits(gb, 4);
959
9d8bb031 960 /* build tables of AC VLC tables */
d86053a4 961 for (i = 1; i <= 5; i++) {
9d8bb031
MM
962 y_tables[i] = &s->ac_vlc_1[ac_y_table];
963 c_tables[i] = &s->ac_vlc_1[ac_c_table];
d86053a4 964 }
d86053a4 965 for (i = 6; i <= 14; i++) {
9d8bb031
MM
966 y_tables[i] = &s->ac_vlc_2[ac_y_table];
967 c_tables[i] = &s->ac_vlc_2[ac_c_table];
d86053a4 968 }
d86053a4 969 for (i = 15; i <= 27; i++) {
9d8bb031
MM
970 y_tables[i] = &s->ac_vlc_3[ac_y_table];
971 c_tables[i] = &s->ac_vlc_3[ac_c_table];
d86053a4 972 }
d86053a4 973 for (i = 28; i <= 63; i++) {
9d8bb031
MM
974 y_tables[i] = &s->ac_vlc_4[ac_y_table];
975 c_tables[i] = &s->ac_vlc_4[ac_c_table];
976 }
977
978 /* decode all AC coefficents */
979 for (i = 1; i <= 63; i++) {
9d8bb031 980 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
c72625f2 981 0, residual_eob_run);
d86053a4 982
9d8bb031 983 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
c72625f2
DC
984 1, residual_eob_run);
985 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
986 2, residual_eob_run);
d86053a4 987 }
892fc83e
MM
988
989 return 0;
d86053a4
MM
990}
991
992/*
993 * This function reverses the DC prediction for each coded fragment in
115329f1 994 * the frame. Much of this function is adapted directly from the original
d86053a4
MM
995 * VP3 source code.
996 */
997#define COMPATIBLE_FRAME(x) \
998 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
c72625f2 999#define DC_COEFF(u) s->all_fragments[u].dc
d86053a4
MM
1000
1001static void reverse_dc_prediction(Vp3DecodeContext *s,
1002 int first_fragment,
1003 int fragment_width,
115329f1 1004 int fragment_height)
d86053a4
MM
1005{
1006
1007#define PUL 8
1008#define PU 4
1009#define PUR 2
1010#define PL 1
1011
1012 int x, y;
1013 int i = first_fragment;
1014
59ef342b 1015 int predicted_dc;
d86053a4 1016
d86053a4
MM
1017 /* DC values for the left, up-left, up, and up-right fragments */
1018 int vl, vul, vu, vur;
1019
f4433de9 1020 /* indexes for the left, up-left, up, and up-right fragments */
d86053a4
MM
1021 int l, ul, u, ur;
1022
115329f1 1023 /*
d86053a4
MM
1024 * The 6 fields mean:
1025 * 0: up-left multiplier
1026 * 1: up multiplier
1027 * 2: up-right multiplier
1028 * 3: left multiplier
d86053a4 1029 */
bb991087 1030 static const int predictor_transform[16][4] = {
006ff1ca
MN
1031 { 0, 0, 0, 0},
1032 { 0, 0, 0,128}, // PL
1033 { 0, 0,128, 0}, // PUR
1034 { 0, 0, 53, 75}, // PUR|PL
1035 { 0,128, 0, 0}, // PU
1036 { 0, 64, 0, 64}, // PU|PL
1037 { 0,128, 0, 0}, // PU|PUR
1038 { 0, 0, 53, 75}, // PU|PUR|PL
1039 {128, 0, 0, 0}, // PUL
1040 { 0, 0, 0,128}, // PUL|PL
1041 { 64, 0, 64, 0}, // PUL|PUR
1042 { 0, 0, 53, 75}, // PUL|PUR|PL
1043 { 0,128, 0, 0}, // PUL|PU
1044 {-104,116, 0,116}, // PUL|PU|PL
1045 { 24, 80, 24, 0}, // PUL|PU|PUR
1046 {-104,116, 0,116} // PUL|PU|PUR|PL
d86053a4
MM
1047 };
1048
1049 /* This table shows which types of blocks can use other blocks for
1050 * prediction. For example, INTRA is the only mode in this table to
1051 * have a frame number of 0. That means INTRA blocks can only predict
115329f1 1052 * from other INTRA blocks. There are 2 golden frame coding types;
d86053a4
MM
1053 * blocks encoding in these modes can only predict from other blocks
1054 * that were encoded with these 1 of these 2 modes. */
50ba3fd7 1055 static const unsigned char compatible_frame[9] = {
d86053a4
MM
1056 1, /* MODE_INTER_NO_MV */
1057 0, /* MODE_INTRA */
1058 1, /* MODE_INTER_PLUS_MV */
1059 1, /* MODE_INTER_LAST_MV */
1060 1, /* MODE_INTER_PRIOR_MV */
1061 2, /* MODE_USING_GOLDEN */
1062 2, /* MODE_GOLDEN_MV */
50ba3fd7
JGG
1063 1, /* MODE_INTER_FOUR_MV */
1064 3 /* MODE_COPY */
d86053a4
MM
1065 };
1066 int current_frame_type;
1067
1068 /* there is a last DC predictor for each of the 3 frame types */
1069 short last_dc[3];
1070
1071 int transform = 0;
1072
d86053a4
MM
1073 vul = vu = vur = vl = 0;
1074 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1075
1076 /* for each fragment row... */
1077 for (y = 0; y < fragment_height; y++) {
1078
1079 /* for each fragment in a row... */
1080 for (x = 0; x < fragment_width; x++, i++) {
1081
1082 /* reverse prediction if this block was coded */
1083 if (s->all_fragments[i].coding_method != MODE_COPY) {
1084
115329f1 1085 current_frame_type =
d86053a4 1086 compatible_frame[s->all_fragments[i].coding_method];
d86053a4 1087
f72f8a77
MN
1088 transform= 0;
1089 if(x){
1090 l= i-1;
7beddb12 1091 vl = DC_COEFF(l);
50ba3fd7 1092 if(COMPATIBLE_FRAME(l))
006ff1ca 1093 transform |= PL;
f72f8a77
MN
1094 }
1095 if(y){
1096 u= i-fragment_width;
7beddb12 1097 vu = DC_COEFF(u);
50ba3fd7 1098 if(COMPATIBLE_FRAME(u))
006ff1ca 1099 transform |= PU;
f72f8a77
MN
1100 if(x){
1101 ul= i-fragment_width-1;
1102 vul = DC_COEFF(ul);
50ba3fd7 1103 if(COMPATIBLE_FRAME(ul))
006ff1ca 1104 transform |= PUL;
f72f8a77
MN
1105 }
1106 if(x + 1 < fragment_width){
1107 ur= i-fragment_width+1;
1108 vur = DC_COEFF(ur);
50ba3fd7 1109 if(COMPATIBLE_FRAME(ur))
006ff1ca 1110 transform |= PUR;
f72f8a77 1111 }
d86053a4
MM
1112 }
1113
d86053a4
MM
1114 if (transform == 0) {
1115
1116 /* if there were no fragments to predict from, use last
1117 * DC saved */
7beddb12 1118 predicted_dc = last_dc[current_frame_type];
d86053a4
MM
1119 } else {
1120
1121 /* apply the appropriate predictor transform */
1122 predicted_dc =
1123 (predictor_transform[transform][0] * vul) +
1124 (predictor_transform[transform][1] * vu) +
1125 (predictor_transform[transform][2] * vur) +
1126 (predictor_transform[transform][3] * vl);
1127
684d9e36 1128 predicted_dc /= 128;
d86053a4
MM
1129
1130 /* check for outranging on the [ul u l] and
1131 * [ul u ur l] predictors */
c11cb375 1132 if ((transform == 15) || (transform == 13)) {
c26abfa5 1133 if (FFABS(predicted_dc - vu) > 128)
d86053a4 1134 predicted_dc = vu;
c26abfa5 1135 else if (FFABS(predicted_dc - vl) > 128)
d86053a4 1136 predicted_dc = vl;
c26abfa5 1137 else if (FFABS(predicted_dc - vul) > 128)
d86053a4
MM
1138 predicted_dc = vul;
1139 }
d86053a4
MM
1140 }
1141
7beddb12 1142 /* at long last, apply the predictor */
c72625f2 1143 DC_COEFF(i) += predicted_dc;
d86053a4 1144 /* save the DC */
7beddb12 1145 last_dc[current_frame_type] = DC_COEFF(i);
d86053a4
MM
1146 }
1147 }
1148 }
1149}
1150
256c0662 1151static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
fe313556 1152{
fe313556
DC
1153 int x, y;
1154 int *bounding_values= s->bounding_values_array+127;
1155
57783884
DC
1156 int width = s->fragment_width[!!plane];
1157 int height = s->fragment_height[!!plane];
621f9a40
DC
1158 int fragment = s->fragment_start [plane] + ystart * width;
1159 int stride = s->current_frame.linesize[plane];
1160 uint8_t *plane_data = s->current_frame.data [plane];
1161 if (!s->flipped_image) stride = -stride;
735acf56 1162 plane_data += s->data_offset[plane] + 8*ystart*stride;
621f9a40
DC
1163
1164 for (y = ystart; y < yend; y++) {
1165
1166 for (x = 0; x < width; x++) {
1167 /* This code basically just deblocks on the edges of coded blocks.
1168 * However, it has to be much more complicated because of the
1169 * braindamaged deblock ordering used in VP3/Theora. Order matters
1170 * because some pixels get filtered twice. */
1171 if( s->all_fragments[fragment].coding_method != MODE_COPY )
1172 {
1173 /* do not perform left edge filter for left columns frags */
1174 if (x > 0) {
1175 s->dsp.vp3_h_loop_filter(
735acf56 1176 plane_data + 8*x,
621f9a40
DC
1177 stride, bounding_values);
1178 }
fe313556 1179
621f9a40
DC
1180 /* do not perform top edge filter for top row fragments */
1181 if (y > 0) {
1182 s->dsp.vp3_v_loop_filter(
735acf56 1183 plane_data + 8*x,
621f9a40
DC
1184 stride, bounding_values);
1185 }
fe313556 1186
621f9a40
DC
1187 /* do not perform right edge filter for right column
1188 * fragments or if right fragment neighbor is also coded
1189 * in this frame (it will be filtered in next iteration) */
1190 if ((x < width - 1) &&
1191 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1192 s->dsp.vp3_h_loop_filter(
735acf56 1193 plane_data + 8*x + 8,
621f9a40 1194 stride, bounding_values);
fe313556
DC
1195 }
1196
621f9a40
DC
1197 /* do not perform bottom edge filter for bottom row
1198 * fragments or if bottom fragment neighbor is also coded
1199 * in this frame (it will be filtered in the next row) */
1200 if ((y < height - 1) &&
1201 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
1202 s->dsp.vp3_v_loop_filter(
735acf56 1203 plane_data + 8*x + 8*stride,
621f9a40
DC
1204 stride, bounding_values);
1205 }
fe313556 1206 }
621f9a40
DC
1207
1208 fragment++;
fe313556 1209 }
735acf56 1210 plane_data += 8*stride;
621f9a40 1211 }
fe313556
DC
1212}
1213
a8de3901 1214/**
c72625f2
DC
1215 * Pulls DCT tokens from the 64 levels to decode and dequant the coefficients
1216 * for the next block in coding order
1217 */
1218static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1219 int plane, int inter, DCTELEM block[64])
1220{
1221 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1222 uint8_t *perm = s->scantable.permutated;
1223 int i = 0;
1224
1225 do {
1226 int token = *s->dct_tokens[plane][i];
1227 switch (token & 3) {
1228 case 0: // EOB
1229 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0
1230 s->dct_tokens[plane][i]++;
1231 else
1232 *s->dct_tokens[plane][i] = token & ~3;
1233 goto end;
1234 case 1: // zero run
1235 s->dct_tokens[plane][i]++;
1236 i += (token >> 2) & 0x7f;
1237 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1238 i++;
1239 break;
1240 case 2: // coeff
1241 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1242 s->dct_tokens[plane][i++]++;
1243 break;
1244 default:
1245 av_log(s->avctx, AV_LOG_ERROR, "internal: invalid token type\n");
1246 return i;
1247 }
1248 } while (i < 64);
1249end:
1250 // the actual DC+prediction is in the fragment structure
1251 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1252 return i;
1253}
1254
1255/**
a8de3901
DC
1256 * called when all pixels up to row y are complete
1257 */
1258static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1259{
1260 int h, cy;
1261 int offset[4];
1262
1263 if(s->avctx->draw_horiz_band==NULL)
1264 return;
1265
1266 h= y - s->last_slice_end;
1267 y -= h;
1268
1269 if (!s->flipped_image) {
1270 if (y == 0)
1271 h -= s->height - s->avctx->height; // account for non-mod16
1272 y = s->height - y - h;
1273 }
1274
1275 cy = y >> 1;
1276 offset[0] = s->current_frame.linesize[0]*y;
1277 offset[1] = s->current_frame.linesize[1]*cy;
1278 offset[2] = s->current_frame.linesize[2]*cy;
1279 offset[3] = 0;
1280
1281 emms_c();
1282 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1283 s->last_slice_end= y + h;
1284}
1285
d86053a4 1286/*
dc4b78d9 1287 * Perform the final rendering for a particular slice of data.
7a095ea6 1288 * The slice number ranges from 0..(c_superblock_height - 1).
dc4b78d9
MM
1289 */
1290static void render_slice(Vp3DecodeContext *s, int slice)
1291{
7a095ea6 1292 int x, y, i, j;
40d11227 1293 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
dc4b78d9 1294 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
dc4b78d9
MM
1295 int motion_halfpel_index;
1296 uint8_t *motion_source;
7a095ea6 1297 int plane, first_pixel;
dc4b78d9 1298
7a095ea6 1299 if (slice >= s->c_superblock_height)
dc4b78d9
MM
1300 return;
1301
1302 for (plane = 0; plane < 3; plane++) {
735acf56
DC
1303 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
1304 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
1305 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
1abbf64e
MN
1306 int stride = s->current_frame.linesize[plane];
1307 int plane_width = s->width >> !!plane;
1308 int plane_height = s->height >> !!plane;
7a095ea6
DC
1309
1310 int sb_x, sb_y = slice << !plane;
1311 int slice_height = sb_y + (plane ? 1 : 2);
1312 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
1313
57783884
DC
1314 int fragment_width = s->fragment_width[!!plane];
1315 int fragment_height = s->fragment_height[!!plane];
7a095ea6 1316 int fragment_start = s->fragment_start[plane];
1abbf64e
MN
1317
1318 if (!s->flipped_image) stride = -stride;
161e8cf4
DC
1319 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1320 continue;
dc4b78d9 1321
115329f1 1322
c26abfa5 1323 if(FFABS(stride) > 2048)
dc4b78d9
MM
1324 return; //various tables are fixed size
1325
7a095ea6
DC
1326 /* for each superblock row in the slice (both of them)... */
1327 for (; sb_y < slice_height; sb_y++) {
dc4b78d9 1328
7a095ea6
DC
1329 /* for each superblock in a row... */
1330 for (sb_x = 0; sb_x < slice_width; sb_x++) {
dc4b78d9 1331
7a095ea6
DC
1332 /* for each block in a superblock... */
1333 for (j = 0; j < 16; j++) {
1334 x = 4*sb_x + hilbert_offset[j][0];
1335 y = 4*sb_y + hilbert_offset[j][1];
1336
1337 i = fragment_start + y*fragment_width + x;
1338
1339 // bounds check
1340 if (x >= fragment_width || y >= fragment_height)
1341 continue;
1342
1343 first_pixel = 8*y*stride + 8*x;
dc4b78d9
MM
1344
1345 /* transform if this block was coded */
161e8cf4 1346 if (s->all_fragments[i].coding_method != MODE_COPY) {
c72625f2 1347 int intra = s->all_fragments[i].coding_method == MODE_INTRA;
dc4b78d9
MM
1348
1349 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1350 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1351 motion_source= golden_plane;
115329f1 1352 else
dc4b78d9
MM
1353 motion_source= last_plane;
1354
735acf56 1355 motion_source += first_pixel;
dc4b78d9
MM
1356 motion_halfpel_index = 0;
1357
1358 /* sort out the motion vector if this fragment is coded
1359 * using a motion vector method */
1360 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1361 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1362 int src_x, src_y;
1363 motion_x = s->all_fragments[i].motion_x;
1364 motion_y = s->all_fragments[i].motion_y;
1365 if(plane){
1366 motion_x= (motion_x>>1) | (motion_x&1);
1367 motion_y= (motion_y>>1) | (motion_y&1);
1368 }
1369
7a095ea6
DC
1370 src_x= (motion_x>>1) + 8*x;
1371 src_y= (motion_y>>1) + 8*y;
dc4b78d9
MM
1372
1373 motion_halfpel_index = motion_x & 0x01;
1374 motion_source += (motion_x >> 1);
1375
1376 motion_halfpel_index |= (motion_y & 0x01) << 1;
1377 motion_source += ((motion_y >> 1) * stride);
1378
1379 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1380 uint8_t *temp= s->edge_emu_buffer;
1381 if(stride<0) temp -= 9*stride;
1382 else temp += 9*stride;
1383
1384 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1385 motion_source= temp;
1386 }
1387 }
115329f1 1388
dc4b78d9
MM
1389
1390 /* first, take care of copying a block from either the
1391 * previous or the golden frame */
1392 if (s->all_fragments[i].coding_method != MODE_INTRA) {
115329f1
DB
1393 /* Note, it is possible to implement all MC cases with
1394 put_no_rnd_pixels_l2 which would look more like the
1395 VP3 source but this would be slower as
dc4b78d9
MM
1396 put_no_rnd_pixels_tab is better optimzed */
1397 if(motion_halfpel_index != 3){
1398 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
735acf56 1399 output_plane + first_pixel,
dc4b78d9
MM
1400 motion_source, stride, 8);
1401 }else{
1402 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
1403 s->dsp.put_no_rnd_pixels_l2[1](
735acf56 1404 output_plane + first_pixel,
115329f1
DB
1405 motion_source - d,
1406 motion_source + stride + 1 + d,
dc4b78d9
MM
1407 stride, 8);
1408 }
dc4b78d9
MM
1409 }
1410
5fecfb7d 1411 s->dsp.clear_block(block);
c72625f2 1412 vp3_dequant(s, s->all_fragments + i, plane, !intra, block);
dc4b78d9
MM
1413
1414 /* invert DCT and place (or add) in final output */
115329f1 1415
dc4b78d9
MM
1416 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1417 if(s->avctx->idct_algo!=FF_IDCT_VP3)
1418 block[0] += 128<<3;
1419 s->dsp.idct_put(
735acf56 1420 output_plane + first_pixel,
dc4b78d9
MM
1421 stride,
1422 block);
1423 } else {
1424 s->dsp.idct_add(
735acf56 1425 output_plane + first_pixel,
dc4b78d9
MM
1426 stride,
1427 block);
1428 }
dc4b78d9
MM
1429 } else {
1430
1431 /* copy directly from the previous frame */
1432 s->dsp.put_pixels_tab[1][0](
735acf56
DC
1433 output_plane + first_pixel,
1434 last_plane + first_pixel,
dc4b78d9
MM
1435 stride, 8);
1436
1437 }
7a095ea6 1438 }
dc4b78d9 1439 }
7a095ea6
DC
1440
1441 // Filter up to the last row in the superblock row
1442 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
dc4b78d9
MM
1443 }
1444 }
1445
dc4b78d9
MM
1446 /* this looks like a good place for slice dispatch... */
1447 /* algorithm:
dc4b78d9 1448 * if (slice == s->macroblock_height - 1)
f44b08a5
MM
1449 * dispatch (both last slice & 2nd-to-last slice);
1450 * else if (slice > 0)
1451 * dispatch (slice - 1);
dc4b78d9
MM
1452 */
1453
d63a7187 1454 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16));
dc4b78d9
MM
1455}
1456
115329f1 1457/*
d86053a4
MM
1458 * This is the ffmpeg/libavcodec API init function.
1459 */
98a6fff9 1460static av_cold int vp3_decode_init(AVCodecContext *avctx)
d86053a4
MM
1461{
1462 Vp3DecodeContext *s = avctx->priv_data;
ae1dd8e1 1463 int i, inter, plane;
892fc83e
MM
1464 int c_width;
1465 int c_height;
d86053a4 1466
3c3f113e 1467 if (avctx->codec_tag == MKTAG('V','P','3','0'))
bb270c08 1468 s->version = 0;
3c3f113e 1469 else
bb270c08 1470 s->version = 1;
3c3f113e 1471
d86053a4 1472 s->avctx = avctx;
ef516f73
DC
1473 s->width = FFALIGN(avctx->width, 16);
1474 s->height = FFALIGN(avctx->height, 16);
d86053a4 1475 avctx->pix_fmt = PIX_FMT_YUV420P;
580a7465 1476 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
8b6103da
MN
1477 if(avctx->idct_algo==FF_IDCT_AUTO)
1478 avctx->idct_algo=FF_IDCT_VP3;
d86053a4 1479 dsputil_init(&s->dsp, avctx);
115329f1 1480
36af0c95 1481 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
d86053a4
MM
1482
1483 /* initialize to an impossible value which will force a recalculation
1484 * in the first frame decode */
f2264fa5
DC
1485 for (i = 0; i < 3; i++)
1486 s->qps[i] = -1;
d86053a4 1487
892fc83e
MM
1488 s->y_superblock_width = (s->width + 31) / 32;
1489 s->y_superblock_height = (s->height + 31) / 32;
35c28d23 1490 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
892fc83e
MM
1491
1492 /* work out the dimensions for the C planes */
1493 c_width = s->width / 2;
1494 c_height = s->height / 2;
1495 s->c_superblock_width = (c_width + 31) / 32;
1496 s->c_superblock_height = (c_height + 31) / 32;
35c28d23 1497 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
892fc83e 1498
35c28d23
DC
1499 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1500 s->u_superblock_start = s->y_superblock_count;
1501 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
d86053a4
MM
1502 s->superblock_coding = av_malloc(s->superblock_count);
1503
1504 s->macroblock_width = (s->width + 15) / 16;
1505 s->macroblock_height = (s->height + 15) / 16;
1506 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1507
57783884
DC
1508 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1509 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1510 s->fragment_width[1] = s->fragment_width[0] >> 1;
1511 s->fragment_height[1] = s->fragment_height[0] >> 1;
d86053a4
MM
1512
1513 /* fragment count covers all 8x8 blocks for all 3 planes */
57783884
DC
1514 s->fragment_count = s->fragment_width[0] * s->fragment_height[0] * 3 / 2;
1515 s->fragment_start[1] = s->fragment_width[0] * s->fragment_height[0];
1516 s->fragment_start[2] = s->fragment_width[0] * s->fragment_height[0] * 5 / 4;
d86053a4 1517
d86053a4 1518 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
c72625f2
DC
1519 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
1520 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
1521 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
1522 !s->coded_fragment_list[0]) {
0efbd068
MM
1523 vp3_decode_end(avctx);
1524 return -1;
1525 }
d86053a4 1526
f44ee2c3
AB
1527 if (!s->theora_tables)
1528 {
2287c100 1529 for (i = 0; i < 64; i++) {
bb270c08 1530 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
bb270c08 1531 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
ae1dd8e1 1532 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
ae1dd8e1 1533 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
ae1dd8e1 1534 s->base_matrix[2][i] = vp31_inter_dequant[i];
bb270c08 1535 s->filter_limit_values[i] = vp31_filter_limit_values[i];
2287c100 1536 }
f44ee2c3 1537
ae1dd8e1
MN
1538 for(inter=0; inter<2; inter++){
1539 for(plane=0; plane<3; plane++){
1540 s->qr_count[inter][plane]= 1;
1541 s->qr_size [inter][plane][0]= 63;
1542 s->qr_base [inter][plane][0]=
1543 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1544 }
1545 }
1546
39922395
MM
1547 /* init VLC tables */
1548 for (i = 0; i < 16; i++) {
1549
1550 /* DC histograms */
1551 init_vlc(&s->dc_vlc[i], 5, 32,
1552 &dc_bias[i][0][1], 4, 2,
1553 &dc_bias[i][0][0], 4, 2, 0);
1554
1555 /* group 1 AC histograms */
1556 init_vlc(&s->ac_vlc_1[i], 5, 32,
1557 &ac_bias_0[i][0][1], 4, 2,
1558 &ac_bias_0[i][0][0], 4, 2, 0);
1559
1560 /* group 2 AC histograms */
1561 init_vlc(&s->ac_vlc_2[i], 5, 32,
1562 &ac_bias_1[i][0][1], 4, 2,
1563 &ac_bias_1[i][0][0], 4, 2, 0);
1564
1565 /* group 3 AC histograms */
1566 init_vlc(&s->ac_vlc_3[i], 5, 32,
1567 &ac_bias_2[i][0][1], 4, 2,
1568 &ac_bias_2[i][0][0], 4, 2, 0);
1569
1570 /* group 4 AC histograms */
1571 init_vlc(&s->ac_vlc_4[i], 5, 32,
1572 &ac_bias_3[i][0][1], 4, 2,
1573 &ac_bias_3[i][0][0], 4, 2, 0);
1574 }
1575 } else {
1576 for (i = 0; i < 16; i++) {
1577
1578 /* DC histograms */
c4b7b8bf 1579 if (init_vlc(&s->dc_vlc[i], 5, 32,
39922395 1580 &s->huffman_table[i][0][1], 4, 2,
c4b7b8bf
RD
1581 &s->huffman_table[i][0][0], 4, 2, 0) < 0)
1582 goto vlc_fail;
39922395
MM
1583
1584 /* group 1 AC histograms */
c4b7b8bf 1585 if (init_vlc(&s->ac_vlc_1[i], 5, 32,
39922395 1586 &s->huffman_table[i+16][0][1], 4, 2,
c4b7b8bf
RD
1587 &s->huffman_table[i+16][0][0], 4, 2, 0) < 0)
1588 goto vlc_fail;
39922395
MM
1589
1590 /* group 2 AC histograms */
c4b7b8bf 1591 if (init_vlc(&s->ac_vlc_2[i], 5, 32,
39922395 1592 &s->huffman_table[i+16*2][0][1], 4, 2,
c4b7b8bf
RD
1593 &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0)
1594 goto vlc_fail;
39922395
MM
1595
1596 /* group 3 AC histograms */
c4b7b8bf 1597 if (init_vlc(&s->ac_vlc_3[i], 5, 32,
39922395 1598 &s->huffman_table[i+16*3][0][1], 4, 2,
c4b7b8bf
RD
1599 &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0)
1600 goto vlc_fail;
39922395
MM
1601
1602 /* group 4 AC histograms */
c4b7b8bf 1603 if (init_vlc(&s->ac_vlc_4[i], 5, 32,
39922395 1604 &s->huffman_table[i+16*4][0][1], 4, 2,
c4b7b8bf
RD
1605 &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0)
1606 goto vlc_fail;
39922395 1607 }
d86053a4
MM
1608 }
1609
d8278bab
MM
1610 init_vlc(&s->superblock_run_length_vlc, 6, 34,
1611 &superblock_run_length_vlc_table[0][1], 4, 2,
1612 &superblock_run_length_vlc_table[0][0], 4, 2, 0);
1613
dd36b667 1614 init_vlc(&s->fragment_run_length_vlc, 5, 30,
0ad72bdd
MM
1615 &fragment_run_length_vlc_table[0][1], 4, 2,
1616 &fragment_run_length_vlc_table[0][0], 4, 2, 0);
1617
1618 init_vlc(&s->mode_code_vlc, 3, 8,
1619 &mode_code_vlc_table[0][1], 2, 1,
1620 &mode_code_vlc_table[0][0], 2, 1, 0);
1621
1622 init_vlc(&s->motion_vector_vlc, 6, 63,
1623 &motion_vector_vlc_table[0][1], 2, 1,
1624 &motion_vector_vlc_table[0][0], 2, 1, 0);
1625
d86053a4
MM
1626 /* work out the block mapping tables */
1627 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
96a7e73b 1628 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
ea676144 1629 if (!s->superblock_fragments || !s->macroblock_coding) {
0efbd068
MM
1630 vp3_decode_end(avctx);
1631 return -1;
1632 }
d86053a4
MM
1633 init_block_mapping(s);
1634
44ae98dd
MM
1635 for (i = 0; i < 3; i++) {
1636 s->current_frame.data[i] = NULL;
1637 s->last_frame.data[i] = NULL;
1638 s->golden_frame.data[i] = NULL;
61873c4a
MM
1639 }
1640
d86053a4 1641 return 0;
c4b7b8bf
RD
1642
1643vlc_fail:
1644 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
1645 return -1;
d86053a4
MM
1646}
1647
1648/*
1649 * This is the ffmpeg/libavcodec API frame decode function.
1650 */
115329f1 1651static int vp3_decode_frame(AVCodecContext *avctx,
d86053a4 1652 void *data, int *data_size,
7a00bbad 1653 AVPacket *avpkt)
d86053a4 1654{
7a00bbad
TB
1655 const uint8_t *buf = avpkt->data;
1656 int buf_size = avpkt->size;
d86053a4
MM
1657 Vp3DecodeContext *s = avctx->priv_data;
1658 GetBitContext gb;
1659 static int counter = 0;
dc4b78d9 1660 int i;
d86053a4 1661
d86053a4 1662 init_get_bits(&gb, buf, buf_size * 8);
115329f1 1663
f44ee2c3
AB
1664 if (s->theora && get_bits1(&gb))
1665 {
bb270c08
DB
1666 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
1667 return -1;
f44ee2c3 1668 }
3c3f113e
AB
1669
1670 s->keyframe = !get_bits1(&gb);
1671 if (!s->theora)
bb270c08 1672 skip_bits(&gb, 1);
f2264fa5
DC
1673 for (i = 0; i < 3; i++)
1674 s->last_qps[i] = s->qps[i];
efea8528 1675
f2264fa5 1676 s->nqps=0;
efea8528 1677 do{
f2264fa5
DC
1678 s->qps[s->nqps++]= get_bits(&gb, 6);
1679 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
1680 for (i = s->nqps; i < 3; i++)
1681 s->qps[i] = -1;
d86053a4 1682
f8830383 1683 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
bb270c08 1684 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
f2264fa5 1685 s->keyframe?"key":"", counter, s->qps[0]);
d86053a4
MM
1686 counter++;
1687
f2264fa5 1688 if (s->qps[0] != s->last_qps[0])
f44b08a5 1689 init_loop_filter(s);
f2264fa5
DC
1690
1691 for (i = 0; i < s->nqps; i++)
1692 // reinit all dequantizers if the first one changed, because
1693 // the DC of the first quantizer must be used for all matrices
1694 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
1695 init_dequantizer(s, i);
642d7e84 1696
068e82ba
DC
1697 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
1698 return buf_size;
1699
739b5090 1700 s->current_frame.reference = 3;
ff65969f 1701 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
739b5090
DC
1702 if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
1703 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
e72d2d12 1704 goto error;
739b5090
DC
1705 }
1706
d86053a4 1707 if (s->keyframe) {
bb270c08
DB
1708 if (!s->theora)
1709 {
1710 skip_bits(&gb, 4); /* width code */
1711 skip_bits(&gb, 4); /* height code */
1712 if (s->version)
1713 {
1714 s->version = get_bits(&gb, 5);
1715 if (counter == 1)
1716 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
1717 }
1718 }
1719 if (s->version || s->theora)
1720 {
1721 if (get_bits1(&gb))
1722 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
1723 skip_bits(&gb, 2); /* reserved? */
1724 }
d86053a4 1725 } else {
735acf56 1726 if (!s->golden_frame.data[0]) {
7a4e8b59 1727 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
ff65969f 1728
7a4e8b59 1729 s->golden_frame.reference = 3;
ff65969f 1730 s->golden_frame.pict_type = FF_I_TYPE;
7a4e8b59
DC
1731 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
1732 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1733 goto error;
1734 }
1735 s->last_frame = s->golden_frame;
1736 s->last_frame.type = FF_BUFFER_TYPE_COPY;
d86053a4 1737 }
d86053a4
MM
1738 }
1739
b928ec64
MN
1740 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
1741 s->current_frame.qstride= 0;
1742
703acd54 1743 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
d86053a4 1744
220a6f40
MN
1745 if (unpack_superblocks(s, &gb)){
1746 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
e72d2d12 1747 goto error;
220a6f40 1748 }
220a6f40
MN
1749 if (unpack_modes(s, &gb)){
1750 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
e72d2d12 1751 goto error;
220a6f40 1752 }
220a6f40
MN
1753 if (unpack_vectors(s, &gb)){
1754 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
e72d2d12 1755 goto error;
220a6f40 1756 }
f2264fa5
DC
1757 if (unpack_block_qpis(s, &gb)){
1758 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
e72d2d12 1759 goto error;
f2264fa5 1760 }
220a6f40
MN
1761 if (unpack_dct_coeffs(s, &gb)){
1762 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
e72d2d12 1763 goto error;
892fc83e 1764 }
735acf56
DC
1765
1766 for (i = 0; i < 3; i++) {
1767 if (s->flipped_image)
1768 s->data_offset[i] = 0;
1769 else
1770 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i];
1771 }
d86053a4 1772
a8de3901 1773 s->last_slice_end = 0;
7a095ea6 1774 for (i = 0; i < s->c_superblock_height; i++)
dc4b78d9 1775 render_slice(s, i);
d86053a4 1776
256c0662
DC
1777 // filter the last row
1778 for (i = 0; i < 3; i++) {
1779 int row = (s->height >> (3+!!i)) - 1;
1780 apply_loop_filter(s, i, row, row+1);
1781 }
a8de3901 1782 vp3_draw_horiz_band(s, s->height);
892fc83e 1783
d86053a4
MM
1784 *data_size=sizeof(AVFrame);
1785 *(AVFrame*)data= s->current_frame;
1786
44ae98dd
MM
1787 /* release the last frame, if it is allocated and if it is not the
1788 * golden frame */
0c9afacb 1789 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
44ae98dd 1790 avctx->release_buffer(avctx, &s->last_frame);
d86053a4 1791
61873c4a 1792 /* shuffle frames (last = current) */
4501e678 1793 s->last_frame= s->current_frame;
739b5090
DC
1794
1795 if (s->keyframe) {
1796 if (s->golden_frame.data[0])
1797 avctx->release_buffer(avctx, &s->golden_frame);
1798 s->golden_frame = s->current_frame;
0c9afacb 1799 s->last_frame.type = FF_BUFFER_TYPE_COPY;
739b5090
DC
1800 }
1801
8e39d4a7 1802 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
d86053a4
MM
1803
1804 return buf_size;
e72d2d12
DC
1805
1806error:
1807 if (s->current_frame.data[0])
1808 avctx->release_buffer(avctx, &s->current_frame);
1809 return -1;
d86053a4
MM
1810}
1811
1812/*
1813 * This is the ffmpeg/libavcodec API module cleanup function.
1814 */
98a6fff9 1815static av_cold int vp3_decode_end(AVCodecContext *avctx)
d86053a4
MM
1816{
1817 Vp3DecodeContext *s = avctx->priv_data;
6f4e2b5a 1818 int i;
d86053a4 1819
6f4e2b5a 1820 av_free(s->superblock_coding);
d86053a4 1821 av_free(s->all_fragments);
c72625f2
DC
1822 av_free(s->coded_fragment_list[0]);
1823 av_free(s->dct_tokens_base);
d86053a4 1824 av_free(s->superblock_fragments);
96a7e73b 1825 av_free(s->macroblock_coding);
115329f1 1826
6f4e2b5a
MR
1827 for (i = 0; i < 16; i++) {
1828 free_vlc(&s->dc_vlc[i]);
1829 free_vlc(&s->ac_vlc_1[i]);
1830 free_vlc(&s->ac_vlc_2[i]);
1831 free_vlc(&s->ac_vlc_3[i]);
1832 free_vlc(&s->ac_vlc_4[i]);
1833 }
1834
1835 free_vlc(&s->superblock_run_length_vlc);
1836 free_vlc(&s->fragment_run_length_vlc);
1837 free_vlc(&s->mode_code_vlc);
1838 free_vlc(&s->motion_vector_vlc);
1839
d86053a4 1840 /* release all frames */
0c9afacb 1841 if (s->golden_frame.data[0])
892fc83e 1842 avctx->release_buffer(avctx, &s->golden_frame);
0c9afacb 1843 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
892fc83e
MM
1844 avctx->release_buffer(avctx, &s->last_frame);
1845 /* no need to release the current_frame since it will always be pointing
1846 * to the same frame as either the golden or last frame */
d86053a4
MM
1847
1848 return 0;
1849}
1850
39922395
MM
1851static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
1852{
1853 Vp3DecodeContext *s = avctx->priv_data;
1854
5fc32c27 1855 if (get_bits1(gb)) {
39922395
MM
1856 int token;
1857 if (s->entries >= 32) { /* overflow */
1858 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
1859 return -1;
1860 }
1861 token = get_bits(gb, 5);
1862 //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
1863 s->huffman_table[s->hti][token][0] = s->hbits;
1864 s->huffman_table[s->hti][token][1] = s->huff_code_size;
1865 s->entries++;
1866 }
1867 else {
1868 if (s->huff_code_size >= 32) {/* overflow */
1869 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
1870 return -1;
1871 }
1872 s->huff_code_size++;
1873 s->hbits <<= 1;
00bbe276
AC
1874 if (read_huffman_tree(avctx, gb))
1875 return -1;
39922395 1876 s->hbits |= 1;
00bbe276
AC
1877 if (read_huffman_tree(avctx, gb))
1878 return -1;
39922395
MM
1879 s->hbits >>= 1;
1880 s->huff_code_size--;
1881 }
1882 return 0;
1883}
1884
b250f9c6 1885#if CONFIG_THEORA_DECODER
e278056f 1886static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
f44ee2c3
AB
1887{
1888 Vp3DecodeContext *s = avctx->priv_data;
ea3c2d53 1889 int visible_width, visible_height, colorspace;
9a7ad925 1890
e278056f 1891 s->theora = get_bits_long(gb, 24);
356306ac 1892 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
105c3d25 1893
ba7ee4a4 1894 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
9a7ad925 1895 /* but previous versions have the image flipped relative to vp3 */
ba7ee4a4 1896 if (s->theora < 0x030200)
9a7ad925 1897 {
bb270c08 1898 s->flipped_image = 1;
9a7ad925
AB
1899 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
1900 }
f44ee2c3 1901
277e3e53
DC
1902 visible_width = s->width = get_bits(gb, 16) << 4;
1903 visible_height = s->height = get_bits(gb, 16) << 4;
115329f1 1904
0ecca7a4 1905 if(avcodec_check_dimensions(avctx, s->width, s->height)){
7146d2c2 1906 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
0ecca7a4
MN
1907 s->width= s->height= 0;
1908 return -1;
1909 }
7146d2c2 1910
277e3e53 1911 if (s->theora >= 0x030200) {
a0ce2d1b
DC
1912 visible_width = get_bits_long(gb, 24);
1913 visible_height = get_bits_long(gb, 24);
c0f716b8 1914
ba4816a0
AJ
1915 skip_bits(gb, 8); /* offset x */
1916 skip_bits(gb, 8); /* offset y */
1917 }
f44ee2c3 1918
e278056f
MN
1919 skip_bits(gb, 32); /* fps numerator */
1920 skip_bits(gb, 32); /* fps denumerator */
1921 skip_bits(gb, 24); /* aspect numerator */
1922 skip_bits(gb, 24); /* aspect denumerator */
115329f1 1923
ba7ee4a4 1924 if (s->theora < 0x030200)
e278056f 1925 skip_bits(gb, 5); /* keyframe frequency force */
ea3c2d53 1926 colorspace = get_bits(gb, 8);
e278056f 1927 skip_bits(gb, 24); /* bitrate */
f44ee2c3 1928
e278056f 1929 skip_bits(gb, 6); /* quality hint */
115329f1 1930
ba7ee4a4 1931 if (s->theora >= 0x030200)
105c3d25 1932 {
e278056f 1933 skip_bits(gb, 5); /* keyframe frequency force */
337f5c6e
DC
1934 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
1935 skip_bits(gb, 3); /* reserved */
105c3d25 1936 }
115329f1 1937
e278056f 1938// align_get_bits(gb);
115329f1 1939
c0f716b8
AJ
1940 if ( visible_width <= s->width && visible_width > s->width-16
1941 && visible_height <= s->height && visible_height > s->height-16)
1942 avcodec_set_dimensions(avctx, visible_width, visible_height);
1943 else
1944 avcodec_set_dimensions(avctx, s->width, s->height);
f44ee2c3 1945
ea3c2d53
DC
1946 if (colorspace == 1) {
1947 avctx->color_primaries = AVCOL_PRI_BT470M;
1948 } else if (colorspace == 2) {
1949 avctx->color_primaries = AVCOL_PRI_BT470BG;
1950 }
1951 if (colorspace == 1 || colorspace == 2) {
1952 avctx->colorspace = AVCOL_SPC_BT470BG;
1953 avctx->color_trc = AVCOL_TRC_BT709;
1954 }
1955
f44ee2c3
AB
1956 return 0;
1957}
1958
e278056f 1959static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
f44ee2c3
AB
1960{
1961 Vp3DecodeContext *s = avctx->priv_data;
ae1dd8e1 1962 int i, n, matrices, inter, plane;
ba7ee4a4
MC
1963
1964 if (s->theora >= 0x030200) {
e278056f 1965 n = get_bits(gb, 3);
9c7154c7 1966 /* loop filter limit values table */
e13cca4b 1967 for (i = 0; i < 64; i++) {
e278056f 1968 s->filter_limit_values[i] = get_bits(gb, n);
e13cca4b
RD
1969 if (s->filter_limit_values[i] > 127) {
1970 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]);
1971 s->filter_limit_values[i] = 127;
1972 }
1973 }
ba7ee4a4 1974 }
115329f1 1975
ba7ee4a4 1976 if (s->theora >= 0x030200)
e278056f 1977 n = get_bits(gb, 4) + 1;
ba7ee4a4
MC
1978 else
1979 n = 16;
f44ee2c3
AB
1980 /* quality threshold table */
1981 for (i = 0; i < 64; i++)
e278056f 1982 s->coded_ac_scale_factor[i] = get_bits(gb, n);
f44ee2c3 1983
ba7ee4a4 1984 if (s->theora >= 0x030200)
e278056f 1985 n = get_bits(gb, 4) + 1;
ba7ee4a4
MC
1986 else
1987 n = 16;
f44ee2c3
AB
1988 /* dc scale factor table */
1989 for (i = 0; i < 64; i++)
e278056f 1990 s->coded_dc_scale_factor[i] = get_bits(gb, n);
f44ee2c3 1991
ba7ee4a4 1992 if (s->theora >= 0x030200)
e278056f 1993 matrices = get_bits(gb, 9) + 1;
ba7ee4a4 1994 else
2da2ba03 1995 matrices = 3;
f44ee2c3 1996
ae1dd8e1
MN
1997 if(matrices > 384){
1998 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
1999 return -1;
2000 }
3c3f113e 2001
ae1dd8e1 2002 for(n=0; n<matrices; n++){
bb270c08 2003 for (i = 0; i < 64; i++)
ae1dd8e1
MN
2004 s->base_matrix[n][i]= get_bits(gb, 8);
2005 }
2da2ba03 2006
ae1dd8e1
MN
2007 for (inter = 0; inter <= 1; inter++) {
2008 for (plane = 0; plane <= 2; plane++) {
2009 int newqr= 1;
2010 if (inter || plane > 0)
5fc32c27 2011 newqr = get_bits1(gb);
39922395 2012 if (!newqr) {
ae1dd8e1 2013 int qtj, plj;
5fc32c27 2014 if(inter && get_bits1(gb)){
ae1dd8e1
MN
2015 qtj = 0;
2016 plj = plane;
2017 }else{
2018 qtj= (3*inter + plane - 1) / 3;
2019 plj= (plane + 2) % 3;
2020 }
2021 s->qr_count[inter][plane]= s->qr_count[qtj][plj];
2022 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
2023 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
2024 } else {
2025 int qri= 0;
39922395 2026 int qi = 0;
ae1dd8e1
MN
2027
2028 for(;;){
2029 i= get_bits(gb, av_log2(matrices-1)+1);
2030 if(i>= matrices){
2031 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
2032 return -1;
2033 }
2034 s->qr_base[inter][plane][qri]= i;
2035 if(qi >= 63)
2036 break;
2037 i = get_bits(gb, av_log2(63-qi)+1) + 1;
2038 s->qr_size[inter][plane][qri++]= i;
2039 qi += i;
39922395 2040 }
ae1dd8e1 2041
2da2ba03 2042 if (qi > 63) {
7146d2c2 2043 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
bb270c08
DB
2044 return -1;
2045 }
ae1dd8e1 2046 s->qr_count[inter][plane]= qri;
39922395
MM
2047 }
2048 }
2049 }
2050
2da2ba03 2051 /* Huffman tables */
39922395
MM
2052 for (s->hti = 0; s->hti < 80; s->hti++) {
2053 s->entries = 0;
2054 s->huff_code_size = 1;
5fc32c27 2055 if (!get_bits1(gb)) {
39922395 2056 s->hbits = 0;
00bbe276
AC
2057 if(read_huffman_tree(avctx, gb))
2058 return -1;
39922395 2059 s->hbits = 1;
00bbe276
AC
2060 if(read_huffman_tree(avctx, gb))
2061 return -1;
39922395
MM
2062 }
2063 }
115329f1 2064
f44ee2c3 2065 s->theora_tables = 1;
115329f1 2066
f44ee2c3
AB
2067 return 0;
2068}
2069
5ef251e5 2070static av_cold int theora_decode_init(AVCodecContext *avctx)
f44ee2c3
AB
2071{
2072 Vp3DecodeContext *s = avctx->priv_data;
2073 GetBitContext gb;
2074 int ptype;
da91ed59
AJ
2075 uint8_t *header_start[3];
2076 int header_len[3];
2077 int i;
115329f1 2078
f44ee2c3
AB
2079 s->theora = 1;
2080
2081 if (!avctx->extradata_size)
7146d2c2
AB
2082 {
2083 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
bb270c08 2084 return -1;
7146d2c2 2085 }
f44ee2c3 2086
da91ed59
AJ
2087 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2088 42, header_start, header_len) < 0) {
2089 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2090 return -1;
2091 }
ee89b2b9 2092
da91ed59 2093 for(i=0;i<3;i++) {
fa6f2751 2094 init_get_bits(&gb, header_start[i], header_len[i] * 8);
f44ee2c3
AB
2095
2096 ptype = get_bits(&gb, 8);
115329f1 2097
7146d2c2
AB
2098 if (!(ptype & 0x80))
2099 {
2100 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
e278056f 2101// return -1;
115329f1 2102 }
7146d2c2 2103
3700dab4 2104 // FIXME: Check for this as well.
0a8dedc9 2105 skip_bits_long(&gb, 6*8); /* "theora" */
115329f1 2106
f44ee2c3
AB
2107 switch(ptype)
2108 {
2109 case 0x80:
e278056f 2110 theora_decode_header(avctx, &gb);
bb270c08
DB
2111 break;
2112 case 0x81:
2da2ba03 2113// FIXME: is this needed? it breaks sometimes
bb270c08
DB
2114// theora_decode_comments(avctx, gb);
2115 break;
2116 case 0x82:
00bbe276
AC
2117 if (theora_decode_tables(avctx, &gb))
2118 return -1;
bb270c08
DB
2119 break;
2120 default:
2121 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
2122 break;
f44ee2c3 2123 }
12ce1f3f
DC
2124 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
2125 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
116d866c
MC
2126 if (s->theora < 0x030200)
2127 break;
ee89b2b9 2128 }
f44ee2c3 2129
c79c960a 2130 return vp3_decode_init(avctx);
f44ee2c3
AB
2131}
2132
6f6a3e2a
DB
2133AVCodec theora_decoder = {
2134 "theora",
d86053a4 2135 CODEC_TYPE_VIDEO,
6f6a3e2a 2136 CODEC_ID_THEORA,
d86053a4 2137 sizeof(Vp3DecodeContext),
6f6a3e2a 2138 theora_decode_init,
d86053a4
MM
2139 NULL,
2140 vp3_decode_end,
2141 vp3_decode_frame,
a8de3901 2142 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
d5202e4f 2143 NULL,
fe4bf374 2144 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
d86053a4 2145};
6f6a3e2a 2146#endif
f44ee2c3 2147
6f6a3e2a
DB
2148AVCodec vp3_decoder = {
2149 "vp3",
f44ee2c3 2150 CODEC_TYPE_VIDEO,
6f6a3e2a 2151 CODEC_ID_VP3,
f44ee2c3 2152 sizeof(Vp3DecodeContext),
6f6a3e2a 2153 vp3_decode_init,
f44ee2c3
AB
2154 NULL,
2155 vp3_decode_end,
2156 vp3_decode_frame,
a8de3901 2157 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
d5202e4f 2158 NULL,
fe4bf374 2159 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
f44ee2c3 2160};