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