cut over to using new VP3 DSP functions and remove the old ones; bring
[libav.git] / libavcodec / vp3.c
1 /*
2 * Copyright (C) 2003-2004 the ffmpeg project
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
19 * For more information about the VP3 coding process, visit:
20 * http://www.pcisys.net/~melanson/codecs/
21 *
22 * Theora decoder by Alex Beregszaszi
23 *
24 */
25
26 /**
27 * @file vp3.c
28 * On2 VP3 Video Decoder
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include "common.h"
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "mpegvideo.h"
40
41 #include "vp3data.h"
42
43 #define FRAGMENT_PIXELS 8
44
45 /*
46 * Debugging Variables
47 *
48 * Define one or more of the following compile-time variables to 1 to obtain
49 * elaborate information about certain aspects of the decoding process.
50 *
51 * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
52 * DEBUG_VP3: high-level decoding flow
53 * DEBUG_INIT: initialization parameters
54 * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
55 * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
56 * DEBUG_MODES: unpacking the coding modes for individual fragments
57 * DEBUG_VECTORS: display the motion vectors
58 * DEBUG_TOKEN: display exhaustive information about each DCT token
59 * DEBUG_VLC: display the VLCs as they are extracted from the stream
60 * DEBUG_DC_PRED: display the process of reversing DC prediction
61 * DEBUG_IDCT: show every detail of the IDCT process
62 */
63
64 #define KEYFRAMES_ONLY 0
65
66 #define DEBUG_VP3 0
67 #define DEBUG_INIT 0
68 #define DEBUG_DEQUANTIZERS 0
69 #define DEBUG_BLOCK_CODING 0
70 #define DEBUG_MODES 0
71 #define DEBUG_VECTORS 0
72 #define DEBUG_TOKEN 0
73 #define DEBUG_VLC 0
74 #define DEBUG_DC_PRED 0
75 #define DEBUG_IDCT 0
76
77 #if DEBUG_VP3
78 #define debug_vp3 printf
79 #else
80 static inline void debug_vp3(const char *format, ...) { }
81 #endif
82
83 #if DEBUG_INIT
84 #define debug_init printf
85 #else
86 static inline void debug_init(const char *format, ...) { }
87 #endif
88
89 #if DEBUG_DEQUANTIZERS
90 #define debug_dequantizers printf
91 #else
92 static inline void debug_dequantizers(const char *format, ...) { }
93 #endif
94
95 #if DEBUG_BLOCK_CODING
96 #define debug_block_coding printf
97 #else
98 static inline void debug_block_coding(const char *format, ...) { }
99 #endif
100
101 #if DEBUG_MODES
102 #define debug_modes printf
103 #else
104 static inline void debug_modes(const char *format, ...) { }
105 #endif
106
107 #if DEBUG_VECTORS
108 #define debug_vectors printf
109 #else
110 static inline void debug_vectors(const char *format, ...) { }
111 #endif
112
113 #if DEBUG_TOKEN
114 #define debug_token printf
115 #else
116 static inline void debug_token(const char *format, ...) { }
117 #endif
118
119 #if DEBUG_VLC
120 #define debug_vlc printf
121 #else
122 static inline void debug_vlc(const char *format, ...) { }
123 #endif
124
125 #if DEBUG_DC_PRED
126 #define debug_dc_pred printf
127 #else
128 static inline void debug_dc_pred(const char *format, ...) { }
129 #endif
130
131 #if DEBUG_IDCT
132 #define debug_idct printf
133 #else
134 static inline void debug_idct(const char *format, ...) { }
135 #endif
136
137 typedef struct Vp3Fragment {
138 DCTELEM coeffs[64];
139 int coding_method;
140 int coeff_count;
141 int last_coeff;
142 int motion_x;
143 int motion_y;
144 /* address of first pixel taking into account which plane the fragment
145 * lives on as well as the plane stride */
146 int first_pixel;
147 /* this is the macroblock that the fragment belongs to */
148 int macroblock;
149 } Vp3Fragment;
150
151 #define SB_NOT_CODED 0
152 #define SB_PARTIALLY_CODED 1
153 #define SB_FULLY_CODED 2
154
155 #define MODE_INTER_NO_MV 0
156 #define MODE_INTRA 1
157 #define MODE_INTER_PLUS_MV 2
158 #define MODE_INTER_LAST_MV 3
159 #define MODE_INTER_PRIOR_LAST 4
160 #define MODE_USING_GOLDEN 5
161 #define MODE_GOLDEN_MV 6
162 #define MODE_INTER_FOURMV 7
163 #define CODING_MODE_COUNT 8
164
165 /* special internal mode */
166 #define MODE_COPY 8
167
168 /* There are 6 preset schemes, plus a free-form scheme */
169 static int ModeAlphabet[7][CODING_MODE_COUNT] =
170 {
171 /* this is the custom scheme */
172 { 0, 0, 0, 0, 0, 0, 0, 0 },
173
174 /* scheme 1: Last motion vector dominates */
175 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
176 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
177 MODE_INTRA, MODE_USING_GOLDEN,
178 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
179
180 /* scheme 2 */
181 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
182 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
183 MODE_INTRA, MODE_USING_GOLDEN,
184 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
185
186 /* scheme 3 */
187 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
188 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
189 MODE_INTRA, MODE_USING_GOLDEN,
190 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
191
192 /* scheme 4 */
193 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
194 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
195 MODE_INTRA, MODE_USING_GOLDEN,
196 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
197
198 /* scheme 5: No motion vector dominates */
199 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
200 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
201 MODE_INTRA, MODE_USING_GOLDEN,
202 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
203
204 /* scheme 6 */
205 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
206 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
207 MODE_INTER_PLUS_MV, MODE_INTRA,
208 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
209
210 };
211
212 #define MIN_DEQUANT_VAL 2
213
214 typedef struct Vp3DecodeContext {
215 AVCodecContext *avctx;
216 int theora, theora_tables;
217 int version;
218 int width, height;
219 AVFrame golden_frame;
220 AVFrame last_frame;
221 AVFrame current_frame;
222 int keyframe;
223 DSPContext dsp;
224 int flipped_image;
225
226 int quality_index;
227 int last_quality_index;
228
229 int superblock_count;
230 int superblock_width;
231 int superblock_height;
232 int y_superblock_width;
233 int y_superblock_height;
234 int c_superblock_width;
235 int c_superblock_height;
236 int u_superblock_start;
237 int v_superblock_start;
238 unsigned char *superblock_coding;
239
240 int macroblock_count;
241 int macroblock_width;
242 int macroblock_height;
243
244 int fragment_count;
245 int fragment_width;
246 int fragment_height;
247
248 Vp3Fragment *all_fragments;
249 int u_fragment_start;
250 int v_fragment_start;
251
252 /* tables */
253 uint16_t coded_dc_scale_factor[64];
254 uint32_t coded_ac_scale_factor[64];
255 uint16_t coded_intra_y_dequant[64];
256 uint16_t coded_intra_c_dequant[64];
257 uint16_t coded_inter_dequant[64];
258
259 /* this is a list of indices into the all_fragments array indicating
260 * which of the fragments are coded */
261 int *coded_fragment_list;
262 int coded_fragment_list_index;
263 int pixel_addresses_inited;
264
265 VLC dc_vlc[16];
266 VLC ac_vlc_1[16];
267 VLC ac_vlc_2[16];
268 VLC ac_vlc_3[16];
269 VLC ac_vlc_4[16];
270
271 int16_t intra_y_dequant[64];
272 int16_t intra_c_dequant[64];
273 int16_t inter_dequant[64];
274
275 /* This table contains superblock_count * 16 entries. Each set of 16
276 * numbers corresponds to the fragment indices 0..15 of the superblock.
277 * An entry will be -1 to indicate that no entry corresponds to that
278 * index. */
279 int *superblock_fragments;
280
281 /* This table contains superblock_count * 4 entries. Each set of 4
282 * numbers corresponds to the macroblock indices 0..3 of the superblock.
283 * An entry will be -1 to indicate that no entry corresponds to that
284 * index. */
285 int *superblock_macroblocks;
286
287 /* This table contains macroblock_count * 6 entries. Each set of 6
288 * numbers corresponds to the fragment indices 0..5 which comprise
289 * the macroblock (4 Y fragments and 2 C fragments). */
290 int *macroblock_fragments;
291 /* This is an array that indicates how a particular macroblock
292 * is coded. */
293 unsigned char *macroblock_coding;
294
295 int first_coded_y_fragment;
296 int first_coded_c_fragment;
297 int last_coded_y_fragment;
298 int last_coded_c_fragment;
299
300 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
301 uint8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
302 } Vp3DecodeContext;
303
304 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb);
305 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb);
306
307 /************************************************************************
308 * VP3 specific functions
309 ************************************************************************/
310
311 /*
312 * This function sets up all of the various blocks mappings:
313 * superblocks <-> fragments, macroblocks <-> fragments,
314 * superblocks <-> macroblocks
315 *
316 * Returns 0 is successful; returns 1 if *anything* went wrong.
317 */
318 static int init_block_mapping(Vp3DecodeContext *s)
319 {
320 int i, j;
321 signed int hilbert_walk_y[16];
322 signed int hilbert_walk_c[16];
323 signed int hilbert_walk_mb[4];
324
325 int current_fragment = 0;
326 int current_width = 0;
327 int current_height = 0;
328 int right_edge = 0;
329 int bottom_edge = 0;
330 int superblock_row_inc = 0;
331 int *hilbert = NULL;
332 int mapping_index = 0;
333
334 int current_macroblock;
335 int c_fragment;
336
337 signed char travel_width[16] = {
338 1, 1, 0, -1,
339 0, 0, 1, 0,
340 1, 0, 1, 0,
341 0, -1, 0, 1
342 };
343
344 signed char travel_height[16] = {
345 0, 0, 1, 0,
346 1, 1, 0, -1,
347 0, 1, 0, -1,
348 -1, 0, -1, 0
349 };
350
351 signed char travel_width_mb[4] = {
352 1, 0, 1, 0
353 };
354
355 signed char travel_height_mb[4] = {
356 0, 1, 0, -1
357 };
358
359 debug_vp3(" vp3: initialize block mapping tables\n");
360
361 /* figure out hilbert pattern per these frame dimensions */
362 hilbert_walk_y[0] = 1;
363 hilbert_walk_y[1] = 1;
364 hilbert_walk_y[2] = s->fragment_width;
365 hilbert_walk_y[3] = -1;
366 hilbert_walk_y[4] = s->fragment_width;
367 hilbert_walk_y[5] = s->fragment_width;
368 hilbert_walk_y[6] = 1;
369 hilbert_walk_y[7] = -s->fragment_width;
370 hilbert_walk_y[8] = 1;
371 hilbert_walk_y[9] = s->fragment_width;
372 hilbert_walk_y[10] = 1;
373 hilbert_walk_y[11] = -s->fragment_width;
374 hilbert_walk_y[12] = -s->fragment_width;
375 hilbert_walk_y[13] = -1;
376 hilbert_walk_y[14] = -s->fragment_width;
377 hilbert_walk_y[15] = 1;
378
379 hilbert_walk_c[0] = 1;
380 hilbert_walk_c[1] = 1;
381 hilbert_walk_c[2] = s->fragment_width / 2;
382 hilbert_walk_c[3] = -1;
383 hilbert_walk_c[4] = s->fragment_width / 2;
384 hilbert_walk_c[5] = s->fragment_width / 2;
385 hilbert_walk_c[6] = 1;
386 hilbert_walk_c[7] = -s->fragment_width / 2;
387 hilbert_walk_c[8] = 1;
388 hilbert_walk_c[9] = s->fragment_width / 2;
389 hilbert_walk_c[10] = 1;
390 hilbert_walk_c[11] = -s->fragment_width / 2;
391 hilbert_walk_c[12] = -s->fragment_width / 2;
392 hilbert_walk_c[13] = -1;
393 hilbert_walk_c[14] = -s->fragment_width / 2;
394 hilbert_walk_c[15] = 1;
395
396 hilbert_walk_mb[0] = 1;
397 hilbert_walk_mb[1] = s->macroblock_width;
398 hilbert_walk_mb[2] = 1;
399 hilbert_walk_mb[3] = -s->macroblock_width;
400
401 /* iterate through each superblock (all planes) and map the fragments */
402 for (i = 0; i < s->superblock_count; i++) {
403 debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
404 i, s->u_superblock_start, s->v_superblock_start);
405
406 /* time to re-assign the limits? */
407 if (i == 0) {
408
409 /* start of Y superblocks */
410 right_edge = s->fragment_width;
411 bottom_edge = s->fragment_height;
412 current_width = -1;
413 current_height = 0;
414 superblock_row_inc = 3 * s->fragment_width -
415 (s->y_superblock_width * 4 - s->fragment_width);
416 hilbert = hilbert_walk_y;
417
418 /* the first operation for this variable is to advance by 1 */
419 current_fragment = -1;
420
421 } else if (i == s->u_superblock_start) {
422
423 /* start of U superblocks */
424 right_edge = s->fragment_width / 2;
425 bottom_edge = s->fragment_height / 2;
426 current_width = -1;
427 current_height = 0;
428 superblock_row_inc = 3 * (s->fragment_width / 2) -
429 (s->c_superblock_width * 4 - s->fragment_width / 2);
430 hilbert = hilbert_walk_c;
431
432 /* the first operation for this variable is to advance by 1 */
433 current_fragment = s->u_fragment_start - 1;
434
435 } else if (i == s->v_superblock_start) {
436
437 /* start of V superblocks */
438 right_edge = s->fragment_width / 2;
439 bottom_edge = s->fragment_height / 2;
440 current_width = -1;
441 current_height = 0;
442 superblock_row_inc = 3 * (s->fragment_width / 2) -
443 (s->c_superblock_width * 4 - s->fragment_width / 2);
444 hilbert = hilbert_walk_c;
445
446 /* the first operation for this variable is to advance by 1 */
447 current_fragment = s->v_fragment_start - 1;
448
449 }
450
451 if (current_width >= right_edge - 1) {
452 /* reset width and move to next superblock row */
453 current_width = -1;
454 current_height += 4;
455
456 /* fragment is now at the start of a new superblock row */
457 current_fragment += superblock_row_inc;
458 }
459
460 /* iterate through all 16 fragments in a superblock */
461 for (j = 0; j < 16; j++) {
462 current_fragment += hilbert[j];
463 current_width += travel_width[j];
464 current_height += travel_height[j];
465
466 /* check if the fragment is in bounds */
467 if ((current_width < right_edge) &&
468 (current_height < bottom_edge)) {
469 s->superblock_fragments[mapping_index] = current_fragment;
470 debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
471 s->superblock_fragments[mapping_index], i, j,
472 current_width, right_edge, current_height, bottom_edge);
473 } else {
474 s->superblock_fragments[mapping_index] = -1;
475 debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
476 i, j,
477 current_width, right_edge, current_height, bottom_edge);
478 }
479
480 mapping_index++;
481 }
482 }
483
484 /* initialize the superblock <-> macroblock mapping; iterate through
485 * all of the Y plane superblocks to build this mapping */
486 right_edge = s->macroblock_width;
487 bottom_edge = s->macroblock_height;
488 current_width = -1;
489 current_height = 0;
490 superblock_row_inc = s->macroblock_width -
491 (s->y_superblock_width * 2 - s->macroblock_width);;
492 hilbert = hilbert_walk_mb;
493 mapping_index = 0;
494 current_macroblock = -1;
495 for (i = 0; i < s->u_superblock_start; i++) {
496
497 if (current_width >= right_edge - 1) {
498 /* reset width and move to next superblock row */
499 current_width = -1;
500 current_height += 2;
501
502 /* macroblock is now at the start of a new superblock row */
503 current_macroblock += superblock_row_inc;
504 }
505
506 /* iterate through each potential macroblock in the superblock */
507 for (j = 0; j < 4; j++) {
508 current_macroblock += hilbert_walk_mb[j];
509 current_width += travel_width_mb[j];
510 current_height += travel_height_mb[j];
511
512 /* check if the macroblock is in bounds */
513 if ((current_width < right_edge) &&
514 (current_height < bottom_edge)) {
515 s->superblock_macroblocks[mapping_index] = current_macroblock;
516 debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
517 s->superblock_macroblocks[mapping_index], i, j,
518 current_width, right_edge, current_height, bottom_edge);
519 } else {
520 s->superblock_macroblocks[mapping_index] = -1;
521 debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
522 i, j,
523 current_width, right_edge, current_height, bottom_edge);
524 }
525
526 mapping_index++;
527 }
528 }
529
530 /* initialize the macroblock <-> fragment mapping */
531 current_fragment = 0;
532 current_macroblock = 0;
533 mapping_index = 0;
534 for (i = 0; i < s->fragment_height; i += 2) {
535
536 for (j = 0; j < s->fragment_width; j += 2) {
537
538 debug_init(" macroblock %d contains fragments: ", current_macroblock);
539 s->all_fragments[current_fragment].macroblock = current_macroblock;
540 s->macroblock_fragments[mapping_index++] = current_fragment;
541 debug_init("%d ", current_fragment);
542
543 if (j + 1 < s->fragment_width) {
544 s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
545 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
546 debug_init("%d ", current_fragment + 1);
547 } else
548 s->macroblock_fragments[mapping_index++] = -1;
549
550 if (i + 1 < s->fragment_height) {
551 s->all_fragments[current_fragment + s->fragment_width].macroblock =
552 current_macroblock;
553 s->macroblock_fragments[mapping_index++] =
554 current_fragment + s->fragment_width;
555 debug_init("%d ", current_fragment + s->fragment_width);
556 } else
557 s->macroblock_fragments[mapping_index++] = -1;
558
559 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
560 s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
561 current_macroblock;
562 s->macroblock_fragments[mapping_index++] =
563 current_fragment + s->fragment_width + 1;
564 debug_init("%d ", current_fragment + s->fragment_width + 1);
565 } else
566 s->macroblock_fragments[mapping_index++] = -1;
567
568 /* C planes */
569 c_fragment = s->u_fragment_start +
570 (i * s->fragment_width / 4) + (j / 2);
571 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
572 s->macroblock_fragments[mapping_index++] = c_fragment;
573 debug_init("%d ", c_fragment);
574
575 c_fragment = s->v_fragment_start +
576 (i * s->fragment_width / 4) + (j / 2);
577 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
578 s->macroblock_fragments[mapping_index++] = c_fragment;
579 debug_init("%d ", c_fragment);
580
581 debug_init("\n");
582
583 if (j + 2 <= s->fragment_width)
584 current_fragment += 2;
585 else
586 current_fragment++;
587 current_macroblock++;
588 }
589
590 current_fragment += s->fragment_width;
591 }
592
593 return 0; /* successful path out */
594 }
595
596 /*
597 * This function unpacks a single token (which should be in the range 0..31)
598 * and returns a zero run (number of zero coefficients in current DCT matrix
599 * before next non-zero coefficient), the next DCT coefficient, and the
600 * number of consecutive, non-EOB'd DCT blocks to EOB.
601 */
602 static void unpack_token(GetBitContext *gb, int token, int *zero_run,
603 DCTELEM *coeff, int *eob_run)
604 {
605 int sign;
606
607 *zero_run = 0;
608 *eob_run = 0;
609 *coeff = 0;
610
611 debug_token(" vp3 token %d: ", token);
612 switch (token) {
613
614 case 0:
615 debug_token("DCT_EOB_TOKEN, EOB next block\n");
616 *eob_run = 1;
617 break;
618
619 case 1:
620 debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
621 *eob_run = 2;
622 break;
623
624 case 2:
625 debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
626 *eob_run = 3;
627 break;
628
629 case 3:
630 debug_token("DCT_REPEAT_RUN_TOKEN, ");
631 *eob_run = get_bits(gb, 2) + 4;
632 debug_token("EOB the next %d blocks\n", *eob_run);
633 break;
634
635 case 4:
636 debug_token("DCT_REPEAT_RUN2_TOKEN, ");
637 *eob_run = get_bits(gb, 3) + 8;
638 debug_token("EOB the next %d blocks\n", *eob_run);
639 break;
640
641 case 5:
642 debug_token("DCT_REPEAT_RUN3_TOKEN, ");
643 *eob_run = get_bits(gb, 4) + 16;
644 debug_token("EOB the next %d blocks\n", *eob_run);
645 break;
646
647 case 6:
648 debug_token("DCT_REPEAT_RUN4_TOKEN, ");
649 *eob_run = get_bits(gb, 12);
650 debug_token("EOB the next %d blocks\n", *eob_run);
651 break;
652
653 case 7:
654 debug_token("DCT_SHORT_ZRL_TOKEN, ");
655 /* note that this token actually indicates that (3 extra bits) + 1 0s
656 * should be output; this case specifies a run of (3 EBs) 0s and a
657 * coefficient of 0. */
658 *zero_run = get_bits(gb, 3);
659 *coeff = 0;
660 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
661 break;
662
663 case 8:
664 debug_token("DCT_ZRL_TOKEN, ");
665 /* note that this token actually indicates that (6 extra bits) + 1 0s
666 * should be output; this case specifies a run of (6 EBs) 0s and a
667 * coefficient of 0. */
668 *zero_run = get_bits(gb, 6);
669 *coeff = 0;
670 debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
671 break;
672
673 case 9:
674 debug_token("ONE_TOKEN, output 1\n");
675 *coeff = 1;
676 break;
677
678 case 10:
679 debug_token("MINUS_ONE_TOKEN, output -1\n");
680 *coeff = -1;
681 break;
682
683 case 11:
684 debug_token("TWO_TOKEN, output 2\n");
685 *coeff = 2;
686 break;
687
688 case 12:
689 debug_token("MINUS_TWO_TOKEN, output -2\n");
690 *coeff = -2;
691 break;
692
693 case 13:
694 case 14:
695 case 15:
696 case 16:
697 debug_token("LOW_VAL_TOKENS, ");
698 if (get_bits(gb, 1))
699 *coeff = -(3 + (token - 13));
700 else
701 *coeff = 3 + (token - 13);
702 debug_token("output %d\n", *coeff);
703 break;
704
705 case 17:
706 debug_token("DCT_VAL_CATEGORY3, ");
707 sign = get_bits(gb, 1);
708 *coeff = 7 + get_bits(gb, 1);
709 if (sign)
710 *coeff = -(*coeff);
711 debug_token("output %d\n", *coeff);
712 break;
713
714 case 18:
715 debug_token("DCT_VAL_CATEGORY4, ");
716 sign = get_bits(gb, 1);
717 *coeff = 9 + get_bits(gb, 2);
718 if (sign)
719 *coeff = -(*coeff);
720 debug_token("output %d\n", *coeff);
721 break;
722
723 case 19:
724 debug_token("DCT_VAL_CATEGORY5, ");
725 sign = get_bits(gb, 1);
726 *coeff = 13 + get_bits(gb, 3);
727 if (sign)
728 *coeff = -(*coeff);
729 debug_token("output %d\n", *coeff);
730 break;
731
732 case 20:
733 debug_token("DCT_VAL_CATEGORY6, ");
734 sign = get_bits(gb, 1);
735 *coeff = 21 + get_bits(gb, 4);
736 if (sign)
737 *coeff = -(*coeff);
738 debug_token("output %d\n", *coeff);
739 break;
740
741 case 21:
742 debug_token("DCT_VAL_CATEGORY7, ");
743 sign = get_bits(gb, 1);
744 *coeff = 37 + get_bits(gb, 5);
745 if (sign)
746 *coeff = -(*coeff);
747 debug_token("output %d\n", *coeff);
748 break;
749
750 case 22:
751 debug_token("DCT_VAL_CATEGORY8, ");
752 sign = get_bits(gb, 1);
753 *coeff = 69 + get_bits(gb, 9);
754 if (sign)
755 *coeff = -(*coeff);
756 debug_token("output %d\n", *coeff);
757 break;
758
759 case 23:
760 case 24:
761 case 25:
762 case 26:
763 case 27:
764 debug_token("DCT_RUN_CATEGORY1, ");
765 *zero_run = token - 22;
766 if (get_bits(gb, 1))
767 *coeff = -1;
768 else
769 *coeff = 1;
770 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
771 break;
772
773 case 28:
774 debug_token("DCT_RUN_CATEGORY1B, ");
775 if (get_bits(gb, 1))
776 *coeff = -1;
777 else
778 *coeff = 1;
779 *zero_run = 6 + get_bits(gb, 2);
780 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
781 break;
782
783 case 29:
784 debug_token("DCT_RUN_CATEGORY1C, ");
785 if (get_bits(gb, 1))
786 *coeff = -1;
787 else
788 *coeff = 1;
789 *zero_run = 10 + get_bits(gb, 3);
790 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
791 break;
792
793 case 30:
794 debug_token("DCT_RUN_CATEGORY2, ");
795 sign = get_bits(gb, 1);
796 *coeff = 2 + get_bits(gb, 1);
797 if (sign)
798 *coeff = -(*coeff);
799 *zero_run = 1;
800 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
801 break;
802
803 case 31:
804 debug_token("DCT_RUN_CATEGORY2, ");
805 sign = get_bits(gb, 1);
806 *coeff = 2 + get_bits(gb, 1);
807 if (sign)
808 *coeff = -(*coeff);
809 *zero_run = 2 + get_bits(gb, 1);
810 debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
811 break;
812
813 default:
814 av_log(NULL, AV_LOG_ERROR, " vp3: help! Got a bad token: %d > 31\n", token);
815 break;
816
817 }
818 }
819
820 /*
821 * This function wipes out all of the fragment data.
822 */
823 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
824 {
825 int i;
826
827 /* zero out all of the fragment information */
828 s->coded_fragment_list_index = 0;
829 for (i = 0; i < s->fragment_count; i++) {
830 memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
831 s->all_fragments[i].coeff_count = 0;
832 s->all_fragments[i].last_coeff = 0;
833 s->all_fragments[i].motion_x = 0xbeef;
834 s->all_fragments[i].motion_y = 0xbeef;
835 }
836 }
837
838 /*
839 * This function sets of the dequantization tables used for a particular
840 * frame.
841 */
842 static void init_dequantizer(Vp3DecodeContext *s)
843 {
844
845 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
846 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
847 int i, j;
848
849 debug_vp3(" vp3: initializing dequantization tables\n");
850
851 /*
852 * Scale dequantizers:
853 *
854 * quantizer * sf
855 * --------------
856 * 100
857 *
858 * where sf = dc_scale_factor for DC quantizer
859 * or ac_scale_factor for AC quantizer
860 *
861 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
862 */
863 #define SCALER 4
864
865 /* scale DC quantizers */
866 s->intra_y_dequant[0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100;
867 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
868 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
869 s->intra_y_dequant[0] *= SCALER;
870
871 s->intra_c_dequant[0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100;
872 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
873 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
874 s->intra_c_dequant[0] *= SCALER;
875
876 s->inter_dequant[0] = s->coded_inter_dequant[0] * dc_scale_factor / 100;
877 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
878 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
879 s->inter_dequant[0] *= SCALER;
880
881 /* scale AC quantizers, zigzag at the same time in preparation for
882 * the dequantization phase */
883 for (i = 1; i < 64; i++) {
884
885 j = zigzag_index[i];
886
887 s->intra_y_dequant[j] = s->coded_intra_y_dequant[i] * ac_scale_factor / 100;
888 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
889 s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
890 s->intra_y_dequant[j] *= SCALER;
891
892 s->intra_c_dequant[j] = s->coded_intra_c_dequant[i] * ac_scale_factor / 100;
893 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
894 s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
895 s->intra_c_dequant[j] *= SCALER;
896
897 s->inter_dequant[j] = s->coded_inter_dequant[i] * ac_scale_factor / 100;
898 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
899 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
900 s->inter_dequant[j] *= SCALER;
901 }
902
903 memset(s->qscale_table, (FFMAX(s->intra_y_dequant[1], s->intra_c_dequant[1])+8)/16, 512); //FIXME finetune
904
905 /* print debug information as requested */
906 debug_dequantizers("intra Y dequantizers:\n");
907 for (i = 0; i < 8; i++) {
908 for (j = i * 8; j < i * 8 + 8; j++) {
909 debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
910 }
911 debug_dequantizers("\n");
912 }
913 debug_dequantizers("\n");
914
915 debug_dequantizers("intra C dequantizers:\n");
916 for (i = 0; i < 8; i++) {
917 for (j = i * 8; j < i * 8 + 8; j++) {
918 debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
919 }
920 debug_dequantizers("\n");
921 }
922 debug_dequantizers("\n");
923
924 debug_dequantizers("interframe dequantizers:\n");
925 for (i = 0; i < 8; i++) {
926 for (j = i * 8; j < i * 8 + 8; j++) {
927 debug_dequantizers(" %4d,", s->inter_dequant[j]);
928 }
929 debug_dequantizers("\n");
930 }
931 debug_dequantizers("\n");
932 }
933
934 /*
935 * This function is used to fetch runs of 1s or 0s from the bitstream for
936 * use in determining which superblocks are fully and partially coded.
937 *
938 * Codeword RunLength
939 * 0 1
940 * 10x 2-3
941 * 110x 4-5
942 * 1110xx 6-9
943 * 11110xxx 10-17
944 * 111110xxxx 18-33
945 * 111111xxxxxxxxxxxx 34-4129
946 */
947 static int get_superblock_run_length(GetBitContext *gb)
948 {
949
950 if (get_bits(gb, 1) == 0)
951 return 1;
952
953 else if (get_bits(gb, 1) == 0)
954 return (2 + get_bits(gb, 1));
955
956 else if (get_bits(gb, 1) == 0)
957 return (4 + get_bits(gb, 1));
958
959 else if (get_bits(gb, 1) == 0)
960 return (6 + get_bits(gb, 2));
961
962 else if (get_bits(gb, 1) == 0)
963 return (10 + get_bits(gb, 3));
964
965 else if (get_bits(gb, 1) == 0)
966 return (18 + get_bits(gb, 4));
967
968 else
969 return (34 + get_bits(gb, 12));
970
971 }
972
973 /*
974 * This function is used to fetch runs of 1s or 0s from the bitstream for
975 * use in determining which particular fragments are coded.
976 *
977 * Codeword RunLength
978 * 0x 1-2
979 * 10x 3-4
980 * 110x 5-6
981 * 1110xx 7-10
982 * 11110xx 11-14
983 * 11111xxxx 15-30
984 */
985 static int get_fragment_run_length(GetBitContext *gb)
986 {
987
988 if (get_bits(gb, 1) == 0)
989 return (1 + get_bits(gb, 1));
990
991 else if (get_bits(gb, 1) == 0)
992 return (3 + get_bits(gb, 1));
993
994 else if (get_bits(gb, 1) == 0)
995 return (5 + get_bits(gb, 1));
996
997 else if (get_bits(gb, 1) == 0)
998 return (7 + get_bits(gb, 2));
999
1000 else if (get_bits(gb, 1) == 0)
1001 return (11 + get_bits(gb, 2));
1002
1003 else
1004 return (15 + get_bits(gb, 4));
1005
1006 }
1007
1008 /*
1009 * This function decodes a VLC from the bitstream and returns a number
1010 * that ranges from 0..7. The number indicates which of the 8 coding
1011 * modes to use.
1012 *
1013 * VLC Number
1014 * 0 0
1015 * 10 1
1016 * 110 2
1017 * 1110 3
1018 * 11110 4
1019 * 111110 5
1020 * 1111110 6
1021 * 1111111 7
1022 *
1023 */
1024 static int get_mode_code(GetBitContext *gb)
1025 {
1026
1027 if (get_bits(gb, 1) == 0)
1028 return 0;
1029
1030 else if (get_bits(gb, 1) == 0)
1031 return 1;
1032
1033 else if (get_bits(gb, 1) == 0)
1034 return 2;
1035
1036 else if (get_bits(gb, 1) == 0)
1037 return 3;
1038
1039 else if (get_bits(gb, 1) == 0)
1040 return 4;
1041
1042 else if (get_bits(gb, 1) == 0)
1043 return 5;
1044
1045 else if (get_bits(gb, 1) == 0)
1046 return 6;
1047
1048 else
1049 return 7;
1050
1051 }
1052
1053 /*
1054 * This function extracts a motion vector from the bitstream using a VLC
1055 * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
1056 * taken depending on the value on those 3 bits:
1057 *
1058 * 0: return 0
1059 * 1: return 1
1060 * 2: return -1
1061 * 3: if (next bit is 1) return -2, else return 2
1062 * 4: if (next bit is 1) return -3, else return 3
1063 * 5: return 4 + (next 2 bits), next bit is sign
1064 * 6: return 8 + (next 3 bits), next bit is sign
1065 * 7: return 16 + (next 4 bits), next bit is sign
1066 */
1067 static int get_motion_vector_vlc(GetBitContext *gb)
1068 {
1069 int bits;
1070
1071 bits = get_bits(gb, 3);
1072
1073 switch(bits) {
1074
1075 case 0:
1076 bits = 0;
1077 break;
1078
1079 case 1:
1080 bits = 1;
1081 break;
1082
1083 case 2:
1084 bits = -1;
1085 break;
1086
1087 case 3:
1088 if (get_bits(gb, 1) == 0)
1089 bits = 2;
1090 else
1091 bits = -2;
1092 break;
1093
1094 case 4:
1095 if (get_bits(gb, 1) == 0)
1096 bits = 3;
1097 else
1098 bits = -3;
1099 break;
1100
1101 case 5:
1102 bits = 4 + get_bits(gb, 2);
1103 if (get_bits(gb, 1) == 1)
1104 bits = -bits;
1105 break;
1106
1107 case 6:
1108 bits = 8 + get_bits(gb, 3);
1109 if (get_bits(gb, 1) == 1)
1110 bits = -bits;
1111 break;
1112
1113 case 7:
1114 bits = 16 + get_bits(gb, 4);
1115 if (get_bits(gb, 1) == 1)
1116 bits = -bits;
1117 break;
1118
1119 }
1120
1121 return bits;
1122 }
1123
1124 /*
1125 * This function fetches a 5-bit number from the stream followed by
1126 * a sign and calls it a motion vector.
1127 */
1128 static int get_motion_vector_fixed(GetBitContext *gb)
1129 {
1130
1131 int bits;
1132
1133 bits = get_bits(gb, 5);
1134
1135 if (get_bits(gb, 1) == 1)
1136 bits = -bits;
1137
1138 return bits;
1139 }
1140
1141 /*
1142 * This function unpacks all of the superblock/macroblock/fragment coding
1143 * information from the bitstream.
1144 */
1145 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
1146 {
1147 int bit = 0;
1148 int current_superblock = 0;
1149 int current_run = 0;
1150 int decode_fully_flags = 0;
1151 int decode_partial_blocks = 0;
1152 int first_c_fragment_seen;
1153
1154 int i, j;
1155 int current_fragment;
1156
1157 debug_vp3(" vp3: unpacking superblock coding\n");
1158
1159 if (s->keyframe) {
1160
1161 debug_vp3(" keyframe-- all superblocks are fully coded\n");
1162 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
1163
1164 } else {
1165
1166 /* unpack the list of partially-coded superblocks */
1167 bit = get_bits(gb, 1);
1168 /* toggle the bit because as soon as the first run length is
1169 * fetched the bit will be toggled again */
1170 bit ^= 1;
1171 while (current_superblock < s->superblock_count) {
1172 if (current_run == 0) {
1173 bit ^= 1;
1174 current_run = get_superblock_run_length(gb);
1175 debug_block_coding(" setting superblocks %d..%d to %s\n",
1176 current_superblock,
1177 current_superblock + current_run - 1,
1178 (bit) ? "partially coded" : "not coded");
1179
1180 /* if any of the superblocks are not partially coded, flag
1181 * a boolean to decode the list of fully-coded superblocks */
1182 if (bit == 0) {
1183 decode_fully_flags = 1;
1184 } else {
1185
1186 /* make a note of the fact that there are partially coded
1187 * superblocks */
1188 decode_partial_blocks = 1;
1189 }
1190 }
1191 s->superblock_coding[current_superblock++] =
1192 (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
1193 current_run--;
1194 }
1195
1196 /* unpack the list of fully coded superblocks if any of the blocks were
1197 * not marked as partially coded in the previous step */
1198 if (decode_fully_flags) {
1199
1200 current_superblock = 0;
1201 current_run = 0;
1202 bit = get_bits(gb, 1);
1203 /* toggle the bit because as soon as the first run length is
1204 * fetched the bit will be toggled again */
1205 bit ^= 1;
1206 while (current_superblock < s->superblock_count) {
1207
1208 /* skip any superblocks already marked as partially coded */
1209 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
1210
1211 if (current_run == 0) {
1212 bit ^= 1;
1213 current_run = get_superblock_run_length(gb);
1214 }
1215
1216 debug_block_coding(" setting superblock %d to %s\n",
1217 current_superblock,
1218 (bit) ? "fully coded" : "not coded");
1219 s->superblock_coding[current_superblock] =
1220 (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
1221 current_run--;
1222 }
1223 current_superblock++;
1224 }
1225 }
1226
1227 /* if there were partial blocks, initialize bitstream for
1228 * unpacking fragment codings */
1229 if (decode_partial_blocks) {
1230
1231 current_run = 0;
1232 bit = get_bits(gb, 1);
1233 /* toggle the bit because as soon as the first run length is
1234 * fetched the bit will be toggled again */
1235 bit ^= 1;
1236 }
1237 }
1238
1239 /* figure out which fragments are coded; iterate through each
1240 * superblock (all planes) */
1241 s->coded_fragment_list_index = 0;
1242 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
1243 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
1244 first_c_fragment_seen = 0;
1245 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
1246 for (i = 0; i < s->superblock_count; i++) {
1247
1248 /* iterate through all 16 fragments in a superblock */
1249 for (j = 0; j < 16; j++) {
1250
1251 /* if the fragment is in bounds, check its coding status */
1252 current_fragment = s->superblock_fragments[i * 16 + j];
1253 if (current_fragment >= s->fragment_count) {
1254 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
1255 current_fragment, s->fragment_count);
1256 return 1;
1257 }
1258 if (current_fragment != -1) {
1259 if (s->superblock_coding[i] == SB_NOT_CODED) {
1260
1261 /* copy all the fragments from the prior frame */
1262 s->all_fragments[current_fragment].coding_method =
1263 MODE_COPY;
1264
1265 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
1266
1267 /* fragment may or may not be coded; this is the case
1268 * that cares about the fragment coding runs */
1269 if (current_run == 0) {
1270 bit ^= 1;
1271 current_run = get_fragment_run_length(gb);
1272 }
1273
1274 if (bit) {
1275 /* default mode; actual mode will be decoded in
1276 * the next phase */
1277 s->all_fragments[current_fragment].coding_method =
1278 MODE_INTER_NO_MV;
1279 s->coded_fragment_list[s->coded_fragment_list_index] =
1280 current_fragment;
1281 if ((current_fragment >= s->u_fragment_start) &&
1282 (s->last_coded_y_fragment == -1) &&
1283 (!first_c_fragment_seen)) {
1284 s->first_coded_c_fragment = s->coded_fragment_list_index;
1285 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1286 first_c_fragment_seen = 1;
1287 }
1288 s->coded_fragment_list_index++;
1289 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1290 debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
1291 i, current_fragment);
1292 } else {
1293 /* not coded; copy this fragment from the prior frame */
1294 s->all_fragments[current_fragment].coding_method =
1295 MODE_COPY;
1296 debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
1297 i, current_fragment);
1298 }
1299
1300 current_run--;
1301
1302 } else {
1303
1304 /* fragments are fully coded in this superblock; actual
1305 * coding will be determined in next step */
1306 s->all_fragments[current_fragment].coding_method =
1307 MODE_INTER_NO_MV;
1308 s->coded_fragment_list[s->coded_fragment_list_index] =
1309 current_fragment;
1310 if ((current_fragment >= s->u_fragment_start) &&
1311 (s->last_coded_y_fragment == -1) &&
1312 (!first_c_fragment_seen)) {
1313 s->first_coded_c_fragment = s->coded_fragment_list_index;
1314 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
1315 first_c_fragment_seen = 1;
1316 }
1317 s->coded_fragment_list_index++;
1318 s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
1319 debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
1320 i, current_fragment);
1321 }
1322 }
1323 }
1324 }
1325
1326 if (!first_c_fragment_seen)
1327 /* only Y fragments coded in this frame */
1328 s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
1329 else
1330 /* end the list of coded C fragments */
1331 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
1332
1333 debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
1334 s->coded_fragment_list_index,
1335 s->first_coded_y_fragment,
1336 s->last_coded_y_fragment,
1337 s->first_coded_c_fragment,
1338 s->last_coded_c_fragment);
1339
1340 return 0;
1341 }
1342
1343 /*
1344 * This function unpacks all the coding mode data for individual macroblocks
1345 * from the bitstream.
1346 */
1347 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
1348 {
1349 int i, j, k;
1350 int scheme;
1351 int current_macroblock;
1352 int current_fragment;
1353 int coding_mode;
1354
1355 debug_vp3(" vp3: unpacking encoding modes\n");
1356
1357 if (s->keyframe) {
1358 debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
1359
1360 for (i = 0; i < s->fragment_count; i++)
1361 s->all_fragments[i].coding_method = MODE_INTRA;
1362
1363 } else {
1364
1365 /* fetch the mode coding scheme for this frame */
1366 scheme = get_bits(gb, 3);
1367 debug_modes(" using mode alphabet %d\n", scheme);
1368
1369 /* is it a custom coding scheme? */
1370 if (scheme == 0) {
1371 debug_modes(" custom mode alphabet ahead:\n");
1372 for (i = 0; i < 8; i++)
1373 ModeAlphabet[scheme][get_bits(gb, 3)] = i;
1374 }
1375
1376 for (i = 0; i < 8; i++)
1377 debug_modes(" mode[%d][%d] = %d\n", scheme, i,
1378 ModeAlphabet[scheme][i]);
1379
1380 /* iterate through all of the macroblocks that contain 1 or more
1381 * coded fragments */
1382 for (i = 0; i < s->u_superblock_start; i++) {
1383
1384 for (j = 0; j < 4; j++) {
1385 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1386 if ((current_macroblock == -1) ||
1387 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1388 continue;
1389 if (current_macroblock >= s->macroblock_count) {
1390 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
1391 current_macroblock, s->macroblock_count);
1392 return 1;
1393 }
1394
1395 /* mode 7 means get 3 bits for each coding mode */
1396 if (scheme == 7)
1397 coding_mode = get_bits(gb, 3);
1398 else
1399 coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
1400
1401 s->macroblock_coding[current_macroblock] = coding_mode;
1402 for (k = 0; k < 6; k++) {
1403 current_fragment =
1404 s->macroblock_fragments[current_macroblock * 6 + k];
1405 if (current_fragment == -1)
1406 continue;
1407 if (current_fragment >= s->fragment_count) {
1408 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
1409 current_fragment, s->fragment_count);
1410 return 1;
1411 }
1412 if (s->all_fragments[current_fragment].coding_method !=
1413 MODE_COPY)
1414 s->all_fragments[current_fragment].coding_method =
1415 coding_mode;
1416 }
1417
1418 debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
1419 s->macroblock_fragments[current_macroblock * 6], coding_mode);
1420 }
1421 }
1422 }
1423
1424 return 0;
1425 }
1426
1427 /*
1428 * This function unpacks all the motion vectors for the individual
1429 * macroblocks from the bitstream.
1430 */
1431 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
1432 {
1433 int i, j, k;
1434 int coding_mode;
1435 int motion_x[6];
1436 int motion_y[6];
1437 int last_motion_x = 0;
1438 int last_motion_y = 0;
1439 int prior_last_motion_x = 0;
1440 int prior_last_motion_y = 0;
1441 int current_macroblock;
1442 int current_fragment;
1443
1444 debug_vp3(" vp3: unpacking motion vectors\n");
1445 if (s->keyframe) {
1446
1447 debug_vp3(" keyframe-- there are no motion vectors\n");
1448
1449 } else {
1450
1451 memset(motion_x, 0, 6 * sizeof(int));
1452 memset(motion_y, 0, 6 * sizeof(int));
1453
1454 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
1455 coding_mode = get_bits(gb, 1);
1456 debug_vectors(" using %s scheme for unpacking motion vectors\n",
1457 (coding_mode == 0) ? "VLC" : "fixed-length");
1458
1459 /* iterate through all of the macroblocks that contain 1 or more
1460 * coded fragments */
1461 for (i = 0; i < s->u_superblock_start; i++) {
1462
1463 for (j = 0; j < 4; j++) {
1464 current_macroblock = s->superblock_macroblocks[i * 4 + j];
1465 if ((current_macroblock == -1) ||
1466 (s->macroblock_coding[current_macroblock] == MODE_COPY))
1467 continue;
1468 if (current_macroblock >= s->macroblock_count) {
1469 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
1470 current_macroblock, s->macroblock_count);
1471 return 1;
1472 }
1473
1474 current_fragment = s->macroblock_fragments[current_macroblock * 6];
1475 if (current_fragment >= s->fragment_count) {
1476 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
1477 current_fragment, s->fragment_count);
1478 return 1;
1479 }
1480 switch (s->macroblock_coding[current_macroblock]) {
1481
1482 case MODE_INTER_PLUS_MV:
1483 case MODE_GOLDEN_MV:
1484 /* all 6 fragments use the same motion vector */
1485 if (coding_mode == 0) {
1486 motion_x[0] = get_motion_vector_vlc(gb);
1487 motion_y[0] = get_motion_vector_vlc(gb);
1488 } else {
1489 motion_x[0] = get_motion_vector_fixed(gb);
1490 motion_y[0] = get_motion_vector_fixed(gb);
1491 }
1492 for (k = 1; k < 6; k++) {
1493 motion_x[k] = motion_x[0];
1494 motion_y[k] = motion_y[0];
1495 }
1496
1497 /* vector maintenance, only on MODE_INTER_PLUS_MV */
1498 if (s->macroblock_coding[current_macroblock] ==
1499 MODE_INTER_PLUS_MV) {
1500 prior_last_motion_x = last_motion_x;
1501 prior_last_motion_y = last_motion_y;
1502 last_motion_x = motion_x[0];
1503 last_motion_y = motion_y[0];
1504 }
1505 break;
1506
1507 case MODE_INTER_FOURMV:
1508 /* fetch 4 vectors from the bitstream, one for each
1509 * Y fragment, then average for the C fragment vectors */
1510 motion_x[4] = motion_y[4] = 0;
1511 for (k = 0; k < 4; k++) {
1512 if (coding_mode == 0) {
1513 motion_x[k] = get_motion_vector_vlc(gb);
1514 motion_y[k] = get_motion_vector_vlc(gb);
1515 } else {
1516 motion_x[k] = get_motion_vector_fixed(gb);
1517 motion_y[k] = get_motion_vector_fixed(gb);
1518 }
1519 motion_x[4] += motion_x[k];
1520 motion_y[4] += motion_y[k];
1521 }
1522
1523 if (motion_x[4] >= 0)
1524 motion_x[4] = (motion_x[4] + 2) / 4;
1525 else
1526 motion_x[4] = (motion_x[4] - 2) / 4;
1527 motion_x[5] = motion_x[4];
1528
1529 if (motion_y[4] >= 0)
1530 motion_y[4] = (motion_y[4] + 2) / 4;
1531 else
1532 motion_y[4] = (motion_y[4] - 2) / 4;
1533 motion_y[5] = motion_y[4];
1534
1535 /* vector maintenance; vector[3] is treated as the
1536 * last vector in this case */
1537 prior_last_motion_x = last_motion_x;
1538 prior_last_motion_y = last_motion_y;
1539 last_motion_x = motion_x[3];
1540 last_motion_y = motion_y[3];
1541 break;
1542
1543 case MODE_INTER_LAST_MV:
1544 /* all 6 fragments use the last motion vector */
1545 motion_x[0] = last_motion_x;
1546 motion_y[0] = last_motion_y;
1547 for (k = 1; k < 6; k++) {
1548 motion_x[k] = motion_x[0];
1549 motion_y[k] = motion_y[0];
1550 }
1551
1552 /* no vector maintenance (last vector remains the
1553 * last vector) */
1554 break;
1555
1556 case MODE_INTER_PRIOR_LAST:
1557 /* all 6 fragments use the motion vector prior to the
1558 * last motion vector */
1559 motion_x[0] = prior_last_motion_x;
1560 motion_y[0] = prior_last_motion_y;
1561 for (k = 1; k < 6; k++) {
1562 motion_x[k] = motion_x[0];
1563 motion_y[k] = motion_y[0];
1564 }
1565
1566 /* vector maintenance */
1567 prior_last_motion_x = last_motion_x;
1568 prior_last_motion_y = last_motion_y;
1569 last_motion_x = motion_x[0];
1570 last_motion_y = motion_y[0];
1571 break;
1572
1573 default:
1574 /* covers intra, inter without MV, golden without MV */
1575 memset(motion_x, 0, 6 * sizeof(int));
1576 memset(motion_y, 0, 6 * sizeof(int));
1577
1578 /* no vector maintenance */
1579 break;
1580 }
1581
1582 /* assign the motion vectors to the correct fragments */
1583 debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
1584 current_fragment,
1585 s->macroblock_coding[current_macroblock]);
1586 for (k = 0; k < 6; k++) {
1587 current_fragment =
1588 s->macroblock_fragments[current_macroblock * 6 + k];
1589 if (current_fragment == -1)
1590 continue;
1591 if (current_fragment >= s->fragment_count) {
1592 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
1593 current_fragment, s->fragment_count);
1594 return 1;
1595 }
1596 s->all_fragments[current_fragment].motion_x = motion_x[k];
1597 s->all_fragments[current_fragment].motion_y = motion_y[k];
1598 debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
1599 k, current_fragment, motion_x[k], motion_y[k]);
1600 }
1601 }
1602 }
1603 }
1604
1605 return 0;
1606 }
1607
1608 /*
1609 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1610 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1611 * data. This function unpacks all the VLCs for either the Y plane or both
1612 * C planes, and is called for DC coefficients or different AC coefficient
1613 * levels (since different coefficient types require different VLC tables.
1614 *
1615 * This function returns a residual eob run. E.g, if a particular token gave
1616 * instructions to EOB the next 5 fragments and there were only 2 fragments
1617 * left in the current fragment range, 3 would be returned so that it could
1618 * be passed into the next call to this same function.
1619 */
1620 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1621 VLC *table, int coeff_index,
1622 int first_fragment, int last_fragment,
1623 int eob_run)
1624 {
1625 int i;
1626 int token;
1627 int zero_run;
1628 DCTELEM coeff;
1629 Vp3Fragment *fragment;
1630
1631 if ((first_fragment >= s->fragment_count) ||
1632 (last_fragment >= s->fragment_count)) {
1633
1634 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
1635 first_fragment, last_fragment);
1636 return 0;
1637 }
1638
1639 for (i = first_fragment; i <= last_fragment; i++) {
1640
1641 fragment = &s->all_fragments[s->coded_fragment_list[i]];
1642 if (fragment->coeff_count > coeff_index)
1643 continue;
1644
1645 if (!eob_run) {
1646 /* decode a VLC into a token */
1647 token = get_vlc2(gb, table->table, 5, 3);
1648 debug_vlc(" token = %2d, ", token);
1649 /* use the token to get a zero run, a coefficient, and an eob run */
1650 unpack_token(gb, token, &zero_run, &coeff, &eob_run);
1651 }
1652
1653 if (!eob_run) {
1654 fragment->coeff_count += zero_run;
1655 if (fragment->coeff_count < 64)
1656 fragment->coeffs[fragment->coeff_count++] = coeff;
1657 debug_vlc(" fragment %d coeff = %d\n",
1658 s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
1659 } else {
1660 fragment->last_coeff = fragment->coeff_count;
1661 fragment->coeff_count = 64;
1662 debug_vlc(" fragment %d eob with %d coefficients\n",
1663 s->coded_fragment_list[i], fragment->last_coeff);
1664 eob_run--;
1665 }
1666 }
1667
1668 return eob_run;
1669 }
1670
1671 /*
1672 * This function unpacks all of the DCT coefficient data from the
1673 * bitstream.
1674 */
1675 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1676 {
1677 int i;
1678 int dc_y_table;
1679 int dc_c_table;
1680 int ac_y_table;
1681 int ac_c_table;
1682 int residual_eob_run = 0;
1683
1684 /* fetch the DC table indices */
1685 dc_y_table = get_bits(gb, 4);
1686 dc_c_table = get_bits(gb, 4);
1687
1688 /* unpack the Y plane DC coefficients */
1689 debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
1690 dc_y_table);
1691 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1692 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1693
1694 /* unpack the C plane DC coefficients */
1695 debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
1696 dc_c_table);
1697 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1698 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1699
1700 /* fetch the AC table indices */
1701 ac_y_table = get_bits(gb, 4);
1702 ac_c_table = get_bits(gb, 4);
1703
1704 /* unpack the group 1 AC coefficients (coeffs 1-5) */
1705 for (i = 1; i <= 5; i++) {
1706
1707 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1708 i, ac_y_table);
1709 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
1710 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1711
1712 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1713 i, ac_c_table);
1714 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
1715 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1716 }
1717
1718 /* unpack the group 2 AC coefficients (coeffs 6-14) */
1719 for (i = 6; i <= 14; i++) {
1720
1721 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1722 i, ac_y_table);
1723 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
1724 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1725
1726 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1727 i, ac_c_table);
1728 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
1729 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1730 }
1731
1732 /* unpack the group 3 AC coefficients (coeffs 15-27) */
1733 for (i = 15; i <= 27; i++) {
1734
1735 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1736 i, ac_y_table);
1737 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
1738 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1739
1740 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1741 i, ac_c_table);
1742 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
1743 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1744 }
1745
1746 /* unpack the group 4 AC coefficients (coeffs 28-63) */
1747 for (i = 28; i <= 63; i++) {
1748
1749 debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
1750 i, ac_y_table);
1751 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
1752 s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
1753
1754 debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
1755 i, ac_c_table);
1756 residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
1757 s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
1758 }
1759
1760 return 0;
1761 }
1762
1763 /*
1764 * This function reverses the DC prediction for each coded fragment in
1765 * the frame. Much of this function is adapted directly from the original
1766 * VP3 source code.
1767 */
1768 #define COMPATIBLE_FRAME(x) \
1769 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1770 #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
1771 static inline int iabs (int x) { return ((x < 0) ? -x : x); }
1772
1773 static void reverse_dc_prediction(Vp3DecodeContext *s,
1774 int first_fragment,
1775 int fragment_width,
1776 int fragment_height)
1777 {
1778
1779 #define PUL 8
1780 #define PU 4
1781 #define PUR 2
1782 #define PL 1
1783
1784 int x, y;
1785 int i = first_fragment;
1786
1787 /*
1788 * Fragment prediction groups:
1789 *
1790 * 32222222226
1791 * 10000000004
1792 * 10000000004
1793 * 10000000004
1794 * 10000000004
1795 *
1796 * Note: Groups 5 and 7 do not exist as it would mean that the
1797 * fragment's x coordinate is both 0 and (width - 1) at the same time.
1798 */
1799 int predictor_group;
1800 short predicted_dc;
1801
1802 /* validity flags for the left, up-left, up, and up-right fragments */
1803 int fl, ful, fu, fur;
1804
1805 /* DC values for the left, up-left, up, and up-right fragments */
1806 int vl, vul, vu, vur;
1807
1808 /* indices for the left, up-left, up, and up-right fragments */
1809 int l, ul, u, ur;
1810
1811 /*
1812 * The 6 fields mean:
1813 * 0: up-left multiplier
1814 * 1: up multiplier
1815 * 2: up-right multiplier
1816 * 3: left multiplier
1817 * 4: mask
1818 * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
1819 */
1820 int predictor_transform[16][6] = {
1821 { 0, 0, 0, 0, 0, 0 },
1822 { 0, 0, 0, 1, 0, 0 }, // PL
1823 { 0, 0, 1, 0, 0, 0 }, // PUR
1824 { 0, 0, 53, 75, 127, 7 }, // PUR|PL
1825 { 0, 1, 0, 0, 0, 0 }, // PU
1826 { 0, 1, 0, 1, 1, 1 }, // PU|PL
1827 { 0, 1, 0, 0, 0, 0 }, // PU|PUR
1828 { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
1829 { 1, 0, 0, 0, 0, 0 }, // PUL
1830 { 0, 0, 0, 1, 0, 0 }, // PUL|PL
1831 { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
1832 { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
1833 { 0, 1, 0, 0, 0, 0 }, // PUL|PU
1834 {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
1835 { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
1836 {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
1837 };
1838
1839 /* This table shows which types of blocks can use other blocks for
1840 * prediction. For example, INTRA is the only mode in this table to
1841 * have a frame number of 0. That means INTRA blocks can only predict
1842 * from other INTRA blocks. There are 2 golden frame coding types;
1843 * blocks encoding in these modes can only predict from other blocks
1844 * that were encoded with these 1 of these 2 modes. */
1845 unsigned char compatible_frame[8] = {
1846 1, /* MODE_INTER_NO_MV */
1847 0, /* MODE_INTRA */
1848 1, /* MODE_INTER_PLUS_MV */
1849 1, /* MODE_INTER_LAST_MV */
1850 1, /* MODE_INTER_PRIOR_MV */
1851 2, /* MODE_USING_GOLDEN */
1852 2, /* MODE_GOLDEN_MV */
1853 1 /* MODE_INTER_FOUR_MV */
1854 };
1855 int current_frame_type;
1856
1857 /* there is a last DC predictor for each of the 3 frame types */
1858 short last_dc[3];
1859
1860 int transform = 0;
1861
1862 debug_vp3(" vp3: reversing DC prediction\n");
1863
1864 vul = vu = vur = vl = 0;
1865 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1866
1867 /* for each fragment row... */
1868 for (y = 0; y < fragment_height; y++) {
1869
1870 /* for each fragment in a row... */
1871 for (x = 0; x < fragment_width; x++, i++) {
1872
1873 /* reverse prediction if this block was coded */
1874 if (s->all_fragments[i].coding_method != MODE_COPY) {
1875
1876 current_frame_type =
1877 compatible_frame[s->all_fragments[i].coding_method];
1878 predictor_group = (x == 0) + ((y == 0) << 1) +
1879 ((x + 1 == fragment_width) << 2);
1880 debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
1881 i, predictor_group, s->all_fragments[i].coeffs[0]);
1882
1883 switch (predictor_group) {
1884
1885 case 0:
1886 /* main body of fragments; consider all 4 possible
1887 * fragments for prediction */
1888
1889 /* calculate the indices of the predicting fragments */
1890 ul = i - fragment_width - 1;
1891 u = i - fragment_width;
1892 ur = i - fragment_width + 1;
1893 l = i - 1;
1894
1895 /* fetch the DC values for the predicting fragments */
1896 vul = s->all_fragments[ul].coeffs[0];
1897 vu = s->all_fragments[u].coeffs[0];
1898 vur = s->all_fragments[ur].coeffs[0];
1899 vl = s->all_fragments[l].coeffs[0];
1900
1901 /* figure out which fragments are valid */
1902 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1903 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1904 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1905 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1906
1907 /* decide which predictor transform to use */
1908 transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
1909
1910 break;
1911
1912 case 1:
1913 /* left column of fragments, not including top corner;
1914 * only consider up and up-right fragments */
1915
1916 /* calculate the indices of the predicting fragments */
1917 u = i - fragment_width;
1918 ur = i - fragment_width + 1;
1919
1920 /* fetch the DC values for the predicting fragments */
1921 vu = s->all_fragments[u].coeffs[0];
1922 vur = s->all_fragments[ur].coeffs[0];
1923
1924 /* figure out which fragments are valid */
1925 fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
1926 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1927
1928 /* decide which predictor transform to use */
1929 transform = (fu*PU) | (fur*PUR);
1930
1931 break;
1932
1933 case 2:
1934 case 6:
1935 /* top row of fragments, not including top-left frag;
1936 * only consider the left fragment for prediction */
1937
1938 /* calculate the indices of the predicting fragments */
1939 l = i - 1;
1940
1941 /* fetch the DC values for the predicting fragments */
1942 vl = s->all_fragments[l].coeffs[0];
1943
1944 /* figure out which fragments are valid */
1945 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1946
1947 /* decide which predictor transform to use */
1948 transform = (fl*PL);
1949
1950 break;
1951
1952 case 3:
1953 /* top-left fragment */
1954
1955 /* nothing to predict from in this case */
1956 transform = 0;
1957
1958 break;
1959
1960 case 4:
1961 /* right column of fragments, not including top corner;
1962 * consider up-left, up, and left fragments for
1963 * prediction */
1964
1965 /* calculate the indices of the predicting fragments */
1966 ul = i - fragment_width - 1;
1967 u = i - fragment_width;
1968 l = i - 1;
1969
1970 /* fetch the DC values for the predicting fragments */
1971 vul = s->all_fragments[ul].coeffs[0];
1972 vu = s->all_fragments[u].coeffs[0];
1973 vl = s->all_fragments[l].coeffs[0];
1974
1975 /* figure out which fragments are valid */
1976 ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
1977 fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
1978 fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
1979
1980 /* decide which predictor transform to use */
1981 transform = (fl*PL) | (fu*PU) | (ful*PUL);
1982
1983 break;
1984
1985 }
1986
1987 debug_dc_pred("transform = %d, ", transform);
1988
1989 if (transform == 0) {
1990
1991 /* if there were no fragments to predict from, use last
1992 * DC saved */
1993 s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
1994 debug_dc_pred("from last DC (%d) = %d\n",
1995 current_frame_type, s->all_fragments[i].coeffs[0]);
1996
1997 } else {
1998
1999 /* apply the appropriate predictor transform */
2000 predicted_dc =
2001 (predictor_transform[transform][0] * vul) +
2002 (predictor_transform[transform][1] * vu) +
2003 (predictor_transform[transform][2] * vur) +
2004 (predictor_transform[transform][3] * vl);
2005
2006 /* if there is a shift value in the transform, add
2007 * the sign bit before the shift */
2008 if (predictor_transform[transform][5] != 0) {
2009 predicted_dc += ((predicted_dc >> 15) &
2010 predictor_transform[transform][4]);
2011 predicted_dc >>= predictor_transform[transform][5];
2012 }
2013
2014 /* check for outranging on the [ul u l] and
2015 * [ul u ur l] predictors */
2016 if ((transform == 13) || (transform == 15)) {
2017 if (iabs(predicted_dc - vu) > 128)
2018 predicted_dc = vu;
2019 else if (iabs(predicted_dc - vl) > 128)
2020 predicted_dc = vl;
2021 else if (iabs(predicted_dc - vul) > 128)
2022 predicted_dc = vul;
2023 }
2024
2025 /* at long last, apply the predictor */
2026 s->all_fragments[i].coeffs[0] += predicted_dc;
2027 debug_dc_pred("from pred DC = %d\n",
2028 s->all_fragments[i].coeffs[0]);
2029 }
2030
2031 /* save the DC */
2032 last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
2033 }
2034 }
2035 }
2036 }
2037
2038 /*
2039 * This function performs the final rendering of each fragment's data
2040 * onto the output frame.
2041 */
2042 static void render_fragments(Vp3DecodeContext *s,
2043 int first_fragment,
2044 int width,
2045 int height,
2046 int plane /* 0 = Y, 1 = U, 2 = V */)
2047 {
2048 int x, y;
2049 int m, n;
2050 int i = first_fragment;
2051 int16_t *dequantizer;
2052 unsigned char *output_plane;
2053 unsigned char *last_plane;
2054 unsigned char *golden_plane;
2055 int stride;
2056 int motion_x, motion_y;
2057 int upper_motion_limit, lower_motion_limit;
2058 int motion_halfpel_index;
2059 uint8_t *motion_source;
2060
2061 debug_vp3(" vp3: rendering final fragments for %s\n",
2062 (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
2063
2064 /* set up plane-specific parameters */
2065 if (plane == 0) {
2066 dequantizer = s->intra_y_dequant;
2067 output_plane = s->current_frame.data[0];
2068 last_plane = s->last_frame.data[0];
2069 golden_plane = s->golden_frame.data[0];
2070 stride = s->current_frame.linesize[0];
2071 if (!s->flipped_image) stride = -stride;
2072 upper_motion_limit = 7 * s->current_frame.linesize[0];
2073 lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
2074 } else if (plane == 1) {
2075 dequantizer = s->intra_c_dequant;
2076 output_plane = s->current_frame.data[1];
2077 last_plane = s->last_frame.data[1];
2078 golden_plane = s->golden_frame.data[1];
2079 stride = s->current_frame.linesize[1];
2080 if (!s->flipped_image) stride = -stride;
2081 upper_motion_limit = 7 * s->current_frame.linesize[1];
2082 lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
2083 } else {
2084 dequantizer = s->intra_c_dequant;
2085 output_plane = s->current_frame.data[2];
2086 last_plane = s->last_frame.data[2];
2087 golden_plane = s->golden_frame.data[2];
2088 stride = s->current_frame.linesize[2];
2089 if (!s->flipped_image) stride = -stride;
2090 upper_motion_limit = 7 * s->current_frame.linesize[2];
2091 lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
2092 }
2093
2094 /* for each fragment row... */
2095 for (y = 0; y < height; y += 8) {
2096
2097 /* for each fragment in a row... */
2098 for (x = 0; x < width; x += 8, i++) {
2099
2100 if ((i < 0) || (i >= s->fragment_count)) {
2101 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_fragments(): bad fragment number (%d)\n", i);
2102 return;
2103 }
2104
2105 /* transform if this block was coded */
2106 if ((s->all_fragments[i].coding_method != MODE_COPY) &&
2107 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
2108
2109 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
2110 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
2111 motion_source= golden_plane;
2112 else
2113 motion_source= last_plane;
2114
2115 motion_source += s->all_fragments[i].first_pixel;
2116 motion_halfpel_index = 0;
2117
2118 /* sort out the motion vector if this fragment is coded
2119 * using a motion vector method */
2120 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
2121 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
2122 int src_x, src_y;
2123 motion_x = s->all_fragments[i].motion_x;
2124 motion_y = s->all_fragments[i].motion_y;
2125 if(plane){
2126 motion_x= (motion_x>>1) | (motion_x&1);
2127 motion_y= (motion_y>>1) | (motion_y&1);
2128 }
2129
2130 src_x= (motion_x>>1) + x;
2131 src_y= (motion_y>>1) + y;
2132 if ((motion_x == 0xbeef) || (motion_y == 0xbeef))
2133 av_log(s->avctx, AV_LOG_ERROR, " help! got beefy vector! (%X, %X)\n", motion_x, motion_y);
2134
2135 motion_halfpel_index = motion_x & 0x01;
2136 motion_source += (motion_x >> 1);
2137
2138 // motion_y = -motion_y;
2139 motion_halfpel_index |= (motion_y & 0x01) << 1;
2140 motion_source += ((motion_y >> 1) * stride);
2141
2142 if(src_x<0 || src_y<0 || src_x + 9 >= width || src_y + 9 >= height){
2143 uint8_t *temp= s->edge_emu_buffer;
2144 if(stride<0) temp -= 9*stride;
2145 else temp += 9*stride;
2146
2147 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, width, height);
2148 motion_source= temp;
2149 }
2150 }
2151
2152
2153 /* first, take care of copying a block from either the
2154 * previous or the golden frame */
2155 if (s->all_fragments[i].coding_method != MODE_INTRA) {
2156 //Note, it is possible to implement all MC cases with put_no_rnd_pixels_l2 which would look more like the VP3 source but this would be slower as put_no_rnd_pixels_tab is better optimzed
2157 if(motion_halfpel_index != 3){
2158 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
2159 output_plane + s->all_fragments[i].first_pixel,
2160 motion_source, stride, 8);
2161 }else{
2162 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
2163 s->dsp.put_no_rnd_pixels_l2[1](
2164 output_plane + s->all_fragments[i].first_pixel,
2165 motion_source - d,
2166 motion_source + stride + 1 + d,
2167 stride, 8);
2168 }
2169 }
2170
2171 /* dequantize the DCT coefficients */
2172 debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
2173 i, s->all_fragments[i].coding_method,
2174 s->all_fragments[i].coeffs[0], dequantizer[0]);
2175
2176 /* invert DCT and place (or add) in final output */
2177 if (s->all_fragments[i].coding_method == MODE_INTRA) {
2178 s->dsp.vp3_idct_put(s->all_fragments[i].coeffs,
2179 dequantizer,
2180 s->all_fragments[i].coeff_count,
2181 output_plane + s->all_fragments[i].first_pixel,
2182 stride);
2183 } else {
2184 s->dsp.vp3_idct_add(s->all_fragments[i].coeffs,
2185 dequantizer,
2186 s->all_fragments[i].coeff_count,
2187 output_plane + s->all_fragments[i].first_pixel,
2188 stride);
2189 }
2190
2191 debug_idct("block after idct_%s():\n",
2192 (s->all_fragments[i].coding_method == MODE_INTRA)?
2193 "put" : "add");
2194 for (m = 0; m < 8; m++) {
2195 for (n = 0; n < 8; n++) {
2196 debug_idct(" %3d", *(output_plane +
2197 s->all_fragments[i].first_pixel + (m * stride + n)));
2198 }
2199 debug_idct("\n");
2200 }
2201 debug_idct("\n");
2202
2203 } else {
2204
2205 /* copy directly from the previous frame */
2206 s->dsp.put_pixels_tab[1][0](
2207 output_plane + s->all_fragments[i].first_pixel,
2208 last_plane + s->all_fragments[i].first_pixel,
2209 stride, 8);
2210
2211 }
2212 }
2213 }
2214
2215 emms_c();
2216
2217 }
2218
2219 /*
2220 * This function computes the first pixel addresses for each fragment.
2221 * This function needs to be invoked after the first frame is allocated
2222 * so that it has access to the plane strides.
2223 */
2224 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
2225 {
2226
2227 int i, x, y;
2228
2229 /* figure out the first pixel addresses for each of the fragments */
2230 /* Y plane */
2231 i = 0;
2232 for (y = s->fragment_height; y > 0; y--) {
2233 for (x = 0; x < s->fragment_width; x++) {
2234 s->all_fragments[i++].first_pixel =
2235 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2236 s->golden_frame.linesize[0] +
2237 x * FRAGMENT_PIXELS;
2238 debug_init(" fragment %d, first pixel @ %d\n",
2239 i-1, s->all_fragments[i-1].first_pixel);
2240 }
2241 }
2242
2243 /* U plane */
2244 i = s->u_fragment_start;
2245 for (y = s->fragment_height / 2; y > 0; y--) {
2246 for (x = 0; x < s->fragment_width / 2; x++) {
2247 s->all_fragments[i++].first_pixel =
2248 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2249 s->golden_frame.linesize[1] +
2250 x * FRAGMENT_PIXELS;
2251 debug_init(" fragment %d, first pixel @ %d\n",
2252 i-1, s->all_fragments[i-1].first_pixel);
2253 }
2254 }
2255
2256 /* V plane */
2257 i = s->v_fragment_start;
2258 for (y = s->fragment_height / 2; y > 0; y--) {
2259 for (x = 0; x < s->fragment_width / 2; x++) {
2260 s->all_fragments[i++].first_pixel =
2261 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2262 s->golden_frame.linesize[2] +
2263 x * FRAGMENT_PIXELS;
2264 debug_init(" fragment %d, first pixel @ %d\n",
2265 i-1, s->all_fragments[i-1].first_pixel);
2266 }
2267 }
2268 }
2269
2270 /* FIXME: this should be merged with the above! */
2271 static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
2272 {
2273
2274 int i, x, y;
2275
2276 /* figure out the first pixel addresses for each of the fragments */
2277 /* Y plane */
2278 i = 0;
2279 for (y = 1; y <= s->fragment_height; y++) {
2280 for (x = 0; x < s->fragment_width; x++) {
2281 s->all_fragments[i++].first_pixel =
2282 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
2283 s->golden_frame.linesize[0] +
2284 x * FRAGMENT_PIXELS;
2285 debug_init(" fragment %d, first pixel @ %d\n",
2286 i-1, s->all_fragments[i-1].first_pixel);
2287 }
2288 }
2289
2290 /* U plane */
2291 i = s->u_fragment_start;
2292 for (y = 1; y <= s->fragment_height / 2; y++) {
2293 for (x = 0; x < s->fragment_width / 2; x++) {
2294 s->all_fragments[i++].first_pixel =
2295 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
2296 s->golden_frame.linesize[1] +
2297 x * FRAGMENT_PIXELS;
2298 debug_init(" fragment %d, first pixel @ %d\n",
2299 i-1, s->all_fragments[i-1].first_pixel);
2300 }
2301 }
2302
2303 /* V plane */
2304 i = s->v_fragment_start;
2305 for (y = 1; y <= s->fragment_height / 2; y++) {
2306 for (x = 0; x < s->fragment_width / 2; x++) {
2307 s->all_fragments[i++].first_pixel =
2308 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
2309 s->golden_frame.linesize[2] +
2310 x * FRAGMENT_PIXELS;
2311 debug_init(" fragment %d, first pixel @ %d\n",
2312 i-1, s->all_fragments[i-1].first_pixel);
2313 }
2314 }
2315 }
2316
2317 /*
2318 * This is the ffmpeg/libavcodec API init function.
2319 */
2320 static int vp3_decode_init(AVCodecContext *avctx)
2321 {
2322 Vp3DecodeContext *s = avctx->priv_data;
2323 int i;
2324 int c_width;
2325 int c_height;
2326 int y_superblock_count;
2327 int c_superblock_count;
2328
2329 if (avctx->codec_tag == MKTAG('V','P','3','0'))
2330 s->version = 0;
2331 else
2332 s->version = 1;
2333
2334 s->avctx = avctx;
2335 #if 0
2336 s->width = avctx->width;
2337 s->height = avctx->height;
2338 #else
2339 s->width = (avctx->width + 15) & 0xFFFFFFF0;
2340 s->height = (avctx->height + 15) & 0xFFFFFFF0;
2341 #endif
2342 avctx->pix_fmt = PIX_FMT_YUV420P;
2343 avctx->has_b_frames = 0;
2344 dsputil_init(&s->dsp, avctx);
2345 s->dsp.vp3_dsp_init();
2346
2347 /* initialize to an impossible value which will force a recalculation
2348 * in the first frame decode */
2349 s->quality_index = -1;
2350
2351 s->y_superblock_width = (s->width + 31) / 32;
2352 s->y_superblock_height = (s->height + 31) / 32;
2353 y_superblock_count = s->y_superblock_width * s->y_superblock_height;
2354
2355 /* work out the dimensions for the C planes */
2356 c_width = s->width / 2;
2357 c_height = s->height / 2;
2358 s->c_superblock_width = (c_width + 31) / 32;
2359 s->c_superblock_height = (c_height + 31) / 32;
2360 c_superblock_count = s->c_superblock_width * s->c_superblock_height;
2361
2362 s->superblock_count = y_superblock_count + (c_superblock_count * 2);
2363 s->u_superblock_start = y_superblock_count;
2364 s->v_superblock_start = s->u_superblock_start + c_superblock_count;
2365 s->superblock_coding = av_malloc(s->superblock_count);
2366
2367 s->macroblock_width = (s->width + 15) / 16;
2368 s->macroblock_height = (s->height + 15) / 16;
2369 s->macroblock_count = s->macroblock_width * s->macroblock_height;
2370
2371 s->fragment_width = s->width / FRAGMENT_PIXELS;
2372 s->fragment_height = s->height / FRAGMENT_PIXELS;
2373
2374 /* fragment count covers all 8x8 blocks for all 3 planes */
2375 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
2376 s->u_fragment_start = s->fragment_width * s->fragment_height;
2377 s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
2378
2379 debug_init(" Y plane: %d x %d\n", s->width, s->height);
2380 debug_init(" C plane: %d x %d\n", c_width, c_height);
2381 debug_init(" Y superblocks: %d x %d, %d total\n",
2382 s->y_superblock_width, s->y_superblock_height, y_superblock_count);
2383 debug_init(" C superblocks: %d x %d, %d total\n",
2384 s->c_superblock_width, s->c_superblock_height, c_superblock_count);
2385 debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
2386 s->superblock_count, s->u_superblock_start, s->v_superblock_start);
2387 debug_init(" macroblocks: %d x %d, %d total\n",
2388 s->macroblock_width, s->macroblock_height, s->macroblock_count);
2389 debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
2390 s->fragment_count,
2391 s->fragment_width,
2392 s->fragment_height,
2393 s->u_fragment_start,
2394 s->v_fragment_start);
2395
2396 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
2397 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
2398 s->pixel_addresses_inited = 0;
2399
2400 if (!s->theora_tables)
2401 {
2402 for (i = 0; i < 64; i++)
2403 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
2404 for (i = 0; i < 64; i++)
2405 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
2406 for (i = 0; i < 64; i++)
2407 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i];
2408 for (i = 0; i < 64; i++)
2409 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i];
2410 for (i = 0; i < 64; i++)
2411 s->coded_inter_dequant[i] = vp31_inter_dequant[i];
2412 }
2413
2414 /* init VLC tables */
2415 for (i = 0; i < 16; i++) {
2416
2417 /* DC histograms */
2418 init_vlc(&s->dc_vlc[i], 5, 32,
2419 &dc_bias[i][0][1], 4, 2,
2420 &dc_bias[i][0][0], 4, 2);
2421
2422 /* group 1 AC histograms */
2423 init_vlc(&s->ac_vlc_1[i], 5, 32,
2424 &ac_bias_0[i][0][1], 4, 2,
2425 &ac_bias_0[i][0][0], 4, 2);
2426
2427 /* group 2 AC histograms */
2428 init_vlc(&s->ac_vlc_2[i], 5, 32,
2429 &ac_bias_1[i][0][1], 4, 2,
2430 &ac_bias_1[i][0][0], 4, 2);
2431
2432 /* group 3 AC histograms */
2433 init_vlc(&s->ac_vlc_3[i], 5, 32,
2434 &ac_bias_2[i][0][1], 4, 2,
2435 &ac_bias_2[i][0][0], 4, 2);
2436
2437 /* group 4 AC histograms */
2438 init_vlc(&s->ac_vlc_4[i], 5, 32,
2439 &ac_bias_3[i][0][1], 4, 2,
2440 &ac_bias_3[i][0][0], 4, 2);
2441 }
2442
2443 /* build quantization zigzag table */
2444 for (i = 0; i < 64; i++)
2445 zigzag_index[dezigzag_index[i]] = i;
2446
2447 /* work out the block mapping tables */
2448 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
2449 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
2450 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
2451 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
2452 init_block_mapping(s);
2453
2454 for (i = 0; i < 3; i++) {
2455 s->current_frame.data[i] = NULL;
2456 s->last_frame.data[i] = NULL;
2457 s->golden_frame.data[i] = NULL;
2458 }
2459
2460 return 0;
2461 }
2462
2463 /*
2464 * This is the ffmpeg/libavcodec API frame decode function.
2465 */
2466 static int vp3_decode_frame(AVCodecContext *avctx,
2467 void *data, int *data_size,
2468 uint8_t *buf, int buf_size)
2469 {
2470 Vp3DecodeContext *s = avctx->priv_data;
2471 GetBitContext gb;
2472 static int counter = 0;
2473
2474 *data_size = 0;
2475
2476 init_get_bits(&gb, buf, buf_size * 8);
2477
2478 if (s->theora && get_bits1(&gb))
2479 {
2480 int ptype = get_bits(&gb, 7);
2481
2482 skip_bits(&gb, 6*8); /* "theora" */
2483
2484 switch(ptype)
2485 {
2486 case 1:
2487 theora_decode_comments(avctx, gb);
2488 break;
2489 case 2:
2490 theora_decode_tables(avctx, gb);
2491 init_dequantizer(s);
2492 break;
2493 default:
2494 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
2495 }
2496 return buf_size;
2497 }
2498
2499 s->keyframe = !get_bits1(&gb);
2500 if (!s->theora)
2501 skip_bits(&gb, 1);
2502 s->last_quality_index = s->quality_index;
2503 s->quality_index = get_bits(&gb, 6);
2504 if (s->theora >= 0x030300)
2505 skip_bits1(&gb);
2506
2507 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2508 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
2509 s->keyframe?"key":"", counter, s->quality_index);
2510 counter++;
2511
2512 if (s->quality_index != s->last_quality_index)
2513 init_dequantizer(s);
2514
2515 if (s->keyframe) {
2516 if (!s->theora)
2517 {
2518 skip_bits(&gb, 4); /* width code */
2519 skip_bits(&gb, 4); /* height code */
2520 if (s->version)
2521 {
2522 s->version = get_bits(&gb, 5);
2523 if (counter == 1)
2524 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
2525 }
2526 }
2527 if (s->version || s->theora)
2528 {
2529 if (get_bits1(&gb))
2530 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
2531 skip_bits(&gb, 2); /* reserved? */
2532 }
2533
2534 if (s->last_frame.data[0] == s->golden_frame.data[0]) {
2535 if (s->golden_frame.data[0])
2536 avctx->release_buffer(avctx, &s->golden_frame);
2537 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
2538 } else {
2539 if (s->golden_frame.data[0])
2540 avctx->release_buffer(avctx, &s->golden_frame);
2541 if (s->last_frame.data[0])
2542 avctx->release_buffer(avctx, &s->last_frame);
2543 }
2544
2545 s->golden_frame.reference = 3;
2546 if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
2547 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2548 return -1;
2549 }
2550
2551 /* golden frame is also the current frame */
2552 memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
2553
2554 /* time to figure out pixel addresses? */
2555 if (!s->pixel_addresses_inited)
2556 {
2557 if (!s->flipped_image)
2558 vp3_calculate_pixel_addresses(s);
2559 else
2560 theora_calculate_pixel_addresses(s);
2561 }
2562 } else {
2563 /* allocate a new current frame */
2564 s->current_frame.reference = 3;
2565 if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
2566 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2567 return -1;
2568 }
2569 }
2570
2571 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2572 s->current_frame.qstride= 0;
2573
2574 init_frame(s, &gb);
2575
2576 #if KEYFRAMES_ONLY
2577 if (!s->keyframe) {
2578
2579 memcpy(s->current_frame.data[0], s->golden_frame.data[0],
2580 s->current_frame.linesize[0] * s->height);
2581 memcpy(s->current_frame.data[1], s->golden_frame.data[1],
2582 s->current_frame.linesize[1] * s->height / 2);
2583 memcpy(s->current_frame.data[2], s->golden_frame.data[2],
2584 s->current_frame.linesize[2] * s->height / 2);
2585
2586 } else {
2587 #endif
2588
2589 if (unpack_superblocks(s, &gb) ||
2590 unpack_modes(s, &gb) ||
2591 unpack_vectors(s, &gb) ||
2592 unpack_dct_coeffs(s, &gb)) {
2593
2594 av_log(s->avctx, AV_LOG_ERROR, " vp3: could not decode frame\n");
2595 return -1;
2596 }
2597
2598 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
2599 render_fragments(s, 0, s->width, s->height, 0);
2600
2601 if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
2602 reverse_dc_prediction(s, s->u_fragment_start,
2603 s->fragment_width / 2, s->fragment_height / 2);
2604 reverse_dc_prediction(s, s->v_fragment_start,
2605 s->fragment_width / 2, s->fragment_height / 2);
2606 render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
2607 render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
2608 } else {
2609 memset(s->current_frame.data[1], 0x80, s->width * s->height / 4);
2610 memset(s->current_frame.data[2], 0x80, s->width * s->height / 4);
2611 }
2612
2613 #if KEYFRAMES_ONLY
2614 }
2615 #endif
2616
2617 *data_size=sizeof(AVFrame);
2618 *(AVFrame*)data= s->current_frame;
2619
2620 /* release the last frame, if it is allocated and if it is not the
2621 * golden frame */
2622 if ((s->last_frame.data[0]) &&
2623 (s->last_frame.data[0] != s->golden_frame.data[0]))
2624 avctx->release_buffer(avctx, &s->last_frame);
2625
2626 /* shuffle frames (last = current) */
2627 memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
2628 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
2629
2630 return buf_size;
2631 }
2632
2633 /*
2634 * This is the ffmpeg/libavcodec API module cleanup function.
2635 */
2636 static int vp3_decode_end(AVCodecContext *avctx)
2637 {
2638 Vp3DecodeContext *s = avctx->priv_data;
2639
2640 av_free(s->all_fragments);
2641 av_free(s->coded_fragment_list);
2642 av_free(s->superblock_fragments);
2643 av_free(s->superblock_macroblocks);
2644 av_free(s->macroblock_fragments);
2645 av_free(s->macroblock_coding);
2646
2647 /* release all frames */
2648 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
2649 avctx->release_buffer(avctx, &s->golden_frame);
2650 if (s->last_frame.data[0])
2651 avctx->release_buffer(avctx, &s->last_frame);
2652 /* no need to release the current_frame since it will always be pointing
2653 * to the same frame as either the golden or last frame */
2654
2655 return 0;
2656 }
2657
2658 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb)
2659 {
2660 Vp3DecodeContext *s = avctx->priv_data;
2661 int major, minor, micro;
2662
2663 major = get_bits(&gb, 8); /* version major */
2664 minor = get_bits(&gb, 8); /* version minor */
2665 micro = get_bits(&gb, 8); /* version micro */
2666 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %d.%d.%d\n",
2667 major, minor, micro);
2668
2669 /* FIXME: endianess? */
2670 s->theora = (major << 16) | (minor << 8) | micro;
2671
2672 /* 3.3.0 aka alpha3 has the same frame orientation as original vp3 */
2673 /* but previous versions have the image flipped relative to vp3 */
2674 if (s->theora < 0x030300)
2675 {
2676 s->flipped_image = 1;
2677 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
2678 }
2679
2680 s->width = get_bits(&gb, 16) << 4;
2681 s->height = get_bits(&gb, 16) << 4;
2682
2683 skip_bits(&gb, 24); /* frame width */
2684 skip_bits(&gb, 24); /* frame height */
2685
2686 skip_bits(&gb, 8); /* offset x */
2687 skip_bits(&gb, 8); /* offset y */
2688
2689 skip_bits(&gb, 32); /* fps numerator */
2690 skip_bits(&gb, 32); /* fps denumerator */
2691 skip_bits(&gb, 24); /* aspect numerator */
2692 skip_bits(&gb, 24); /* aspect denumerator */
2693
2694 if (s->theora < 0x030300)
2695 skip_bits(&gb, 5); /* keyframe frequency force */
2696 skip_bits(&gb, 8); /* colorspace */
2697 skip_bits(&gb, 24); /* bitrate */
2698
2699 skip_bits(&gb, 6); /* last(?) quality index */
2700
2701 if (s->theora >= 0x030300)
2702 {
2703 skip_bits(&gb, 5); /* keyframe frequency force */
2704 skip_bits(&gb, 5); /* spare bits */
2705 }
2706
2707 // align_get_bits(&gb);
2708
2709 avctx->width = s->width;
2710 avctx->height = s->height;
2711
2712 vp3_decode_init(avctx);
2713
2714 return 0;
2715 }
2716
2717 static int theora_decode_comments(AVCodecContext *avctx, GetBitContext gb)
2718 {
2719 int nb_comments, i, tmp;
2720
2721 tmp = get_bits(&gb, 32);
2722 tmp = be2me_32(tmp);
2723 while(tmp--)
2724 skip_bits(&gb, 8);
2725
2726 nb_comments = get_bits(&gb, 32);
2727 nb_comments = be2me_32(nb_comments);
2728 for (i = 0; i < nb_comments; i++)
2729 {
2730 tmp = get_bits(&gb, 32);
2731 tmp = be2me_32(tmp);
2732 while(tmp--)
2733 skip_bits(&gb, 8);
2734 }
2735
2736 return 0;
2737 }
2738
2739 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb)
2740 {
2741 Vp3DecodeContext *s = avctx->priv_data;
2742 int i;
2743
2744 /* quality threshold table */
2745 for (i = 0; i < 64; i++)
2746 s->coded_ac_scale_factor[i] = get_bits(&gb, 16);
2747
2748 /* dc scale factor table */
2749 for (i = 0; i < 64; i++)
2750 s->coded_dc_scale_factor[i] = get_bits(&gb, 16);
2751
2752 /* y coeffs */
2753 for (i = 0; i < 64; i++)
2754 s->coded_intra_y_dequant[i] = get_bits(&gb, 8);
2755
2756 /* uv coeffs */
2757 for (i = 0; i < 64; i++)
2758 s->coded_intra_c_dequant[i] = get_bits(&gb, 8);
2759
2760 /* inter coeffs */
2761 for (i = 0; i < 64; i++)
2762 s->coded_inter_dequant[i] = get_bits(&gb, 8);
2763
2764 /* FIXME: read huffmann tree.. */
2765
2766 s->theora_tables = 1;
2767
2768 return 0;
2769 }
2770
2771 static int theora_decode_init(AVCodecContext *avctx)
2772 {
2773 Vp3DecodeContext *s = avctx->priv_data;
2774 GetBitContext gb;
2775 int ptype;
2776
2777 s->theora = 1;
2778
2779 if (!avctx->extradata_size)
2780 return -1;
2781
2782 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
2783
2784 ptype = get_bits(&gb, 8);
2785 debug_vp3("Theora headerpacket type: %x\n", ptype);
2786
2787 if (!(ptype & 0x80))
2788 return -1;
2789
2790 skip_bits(&gb, 6*8); /* "theora" */
2791
2792 switch(ptype)
2793 {
2794 case 0x80:
2795 theora_decode_header(avctx, gb);
2796 vp3_decode_init(avctx);
2797 break;
2798 case 0x81:
2799 theora_decode_comments(avctx, gb);
2800 break;
2801 case 0x82:
2802 theora_decode_tables(avctx, gb);
2803 break;
2804 }
2805
2806 return 0;
2807 }
2808
2809 AVCodec vp3_decoder = {
2810 "vp3",
2811 CODEC_TYPE_VIDEO,
2812 CODEC_ID_VP3,
2813 sizeof(Vp3DecodeContext),
2814 vp3_decode_init,
2815 NULL,
2816 vp3_decode_end,
2817 vp3_decode_frame,
2818 0,
2819 NULL
2820 };
2821
2822 AVCodec theora_decoder = {
2823 "theora",
2824 CODEC_TYPE_VIDEO,
2825 CODEC_ID_THEORA,
2826 sizeof(Vp3DecodeContext),
2827 theora_decode_init,
2828 NULL,
2829 vp3_decode_end,
2830 vp3_decode_frame,
2831 0,
2832 NULL
2833 };