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