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