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