Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: patched-ffmpeg-mt/libavcodec/vp3.c

Issue 789004: ffmpeg roll of source to mar 9 version... (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/ffmpeg/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2003-2004 the ffmpeg project 2 * Copyright (C) 2003-2004 the ffmpeg project
3 * 3 *
4 * This file is part of FFmpeg. 4 * This file is part of FFmpeg.
5 * 5 *
6 * FFmpeg is free software; you can redistribute it and/or 6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public 7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version. 9 * version 2.1 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 27 matching lines...) Expand all
38 #include "get_bits.h" 38 #include "get_bits.h"
39 39
40 #include "vp3data.h" 40 #include "vp3data.h"
41 #include "xiph.h" 41 #include "xiph.h"
42 #include "thread.h" 42 #include "thread.h"
43 43
44 #define FRAGMENT_PIXELS 8 44 #define FRAGMENT_PIXELS 8
45 45
46 static av_cold int vp3_decode_end(AVCodecContext *avctx); 46 static av_cold int vp3_decode_end(AVCodecContext *avctx);
47 47
48 typedef struct Coeff {
49 struct Coeff *next;
50 DCTELEM coeff;
51 uint8_t index;
52 } Coeff;
53
54 //FIXME split things out into their own arrays 48 //FIXME split things out into their own arrays
55 typedef struct Vp3Fragment { 49 typedef struct Vp3Fragment {
56 Coeff *next_coeff; 50 int16_t dc;
57 /* address of first pixel taking into account which plane the fragment
58 * lives on as well as the plane stride */
59 int first_pixel;
60 /* the y component of first_pixel */
61 int first_row;
62 /* this is the macroblock that the fragment belongs to */
63 uint16_t macroblock;
64 uint8_t coding_method; 51 uint8_t coding_method;
65 int8_t motion_x; 52 int8_t motion_x;
66 int8_t motion_y; 53 int8_t motion_y;
67 uint8_t qpi; 54 uint8_t qpi;
68 } Vp3Fragment; 55 } Vp3Fragment;
69 56
70 #define SB_NOT_CODED 0 57 #define SB_NOT_CODED 0
71 #define SB_PARTIALLY_CODED 1 58 #define SB_PARTIALLY_CODED 1
72 #define SB_FULLY_CODED 2 59 #define SB_FULLY_CODED 2
73 60
61 // This is the maximum length of a single long bit run that can be encoded
62 // for superblock coding or block qps. Theora special-cases this to read a
63 // bit instead of flipping the current bit to allow for runs longer than 4129.
64 #define MAXIMUM_LONG_BIT_RUN 4129
65
74 #define MODE_INTER_NO_MV 0 66 #define MODE_INTER_NO_MV 0
75 #define MODE_INTRA 1 67 #define MODE_INTRA 1
76 #define MODE_INTER_PLUS_MV 2 68 #define MODE_INTER_PLUS_MV 2
77 #define MODE_INTER_LAST_MV 3 69 #define MODE_INTER_LAST_MV 3
78 #define MODE_INTER_PRIOR_LAST 4 70 #define MODE_INTER_PRIOR_LAST 4
79 #define MODE_USING_GOLDEN 5 71 #define MODE_USING_GOLDEN 5
80 #define MODE_GOLDEN_MV 6 72 #define MODE_GOLDEN_MV 6
81 #define MODE_INTER_FOURMV 7 73 #define MODE_INTER_FOURMV 7
82 #define CODING_MODE_COUNT 8 74 #define CODING_MODE_COUNT 8
83 75
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 110 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
119 111
120 /* scheme 6 */ 112 /* scheme 6 */
121 { MODE_INTER_NO_MV, MODE_USING_GOLDEN, 113 { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
122 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, 114 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
123 MODE_INTER_PLUS_MV, MODE_INTRA, 115 MODE_INTER_PLUS_MV, MODE_INTRA,
124 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, 116 MODE_GOLDEN_MV, MODE_INTER_FOURMV },
125 117
126 }; 118 };
127 119
120 static const uint8_t hilbert_offset[16][2] = {
121 {0,0}, {1,0}, {1,1}, {0,1},
122 {0,2}, {0,3}, {1,3}, {1,2},
123 {2,2}, {2,3}, {3,3}, {3,2},
124 {3,1}, {2,1}, {2,0}, {3,0}
125 };
126
128 #define MIN_DEQUANT_VAL 2 127 #define MIN_DEQUANT_VAL 2
129 128
130 typedef struct Vp3DecodeContext { 129 typedef struct Vp3DecodeContext {
131 AVCodecContext *avctx; 130 AVCodecContext *avctx;
132 int theora, theora_tables; 131 int theora, theora_tables;
133 int version; 132 int version;
134 int width, height; 133 int width, height;
135 AVFrame golden_frame; 134 AVFrame golden_frame;
136 AVFrame last_frame; 135 AVFrame last_frame;
137 AVFrame current_frame; 136 AVFrame current_frame;
138 int keyframe; 137 int keyframe;
139 DSPContext dsp; 138 DSPContext dsp;
140 int flipped_image; 139 int flipped_image;
140 int last_slice_end;
141 141
142 int qps[3]; 142 int qps[3];
143 int nqps; 143 int nqps;
144 int last_qps[3]; 144 int last_qps[3];
145 145
146 int superblock_count; 146 int superblock_count;
147 int y_superblock_width; 147 int y_superblock_width;
148 int y_superblock_height; 148 int y_superblock_height;
149 int y_superblock_count;
149 int c_superblock_width; 150 int c_superblock_width;
150 int c_superblock_height; 151 int c_superblock_height;
152 int c_superblock_count;
151 int u_superblock_start; 153 int u_superblock_start;
152 int v_superblock_start; 154 int v_superblock_start;
153 unsigned char *superblock_coding; 155 unsigned char *superblock_coding;
154 156
155 int macroblock_count; 157 int macroblock_count;
156 int macroblock_width; 158 int macroblock_width;
157 int macroblock_height; 159 int macroblock_height;
158 160
159 int fragment_count; 161 int fragment_count;
160 int fragment_width; 162 int fragment_width;
161 int fragment_height; 163 int fragment_height;
162 164
163 Vp3Fragment *all_fragments; 165 Vp3Fragment *all_fragments;
164 uint8_t *coeff_counts;
165 Coeff *coeffs;
166 Coeff *next_coeff;
167 int fragment_start[3]; 166 int fragment_start[3];
167 int data_offset[3];
168 168
169 ScanTable scantable; 169 ScanTable scantable;
170 170
171 /* tables */ 171 /* tables */
172 uint16_t coded_dc_scale_factor[64]; 172 uint16_t coded_dc_scale_factor[64];
173 uint32_t coded_ac_scale_factor[64]; 173 uint32_t coded_ac_scale_factor[64];
174 uint8_t base_matrix[384][64]; 174 uint8_t base_matrix[384][64];
175 uint8_t qr_count[2][3]; 175 uint8_t qr_count[2][3];
176 uint8_t qr_size [2][3][64]; 176 uint8_t qr_size [2][3][64];
177 uint16_t qr_base[2][3][64]; 177 uint16_t qr_base[2][3][64];
178 178
179 /**
180 * This is a list of all tokens in bitstream order. Reordering takes place
181 * by pulling from each level during IDCT. As a consequence, IDCT must be
182 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
183 * otherwise. The 32 different tokens with up to 12 bits of extradata are
184 * collapsed into 3 types, packed as follows:
185 * (from the low to high bits)
186 *
187 * 2 bits: type (0,1,2)
188 * 0: EOB run, 14 bits for run length (12 needed)
189 * 1: zero run, 7 bits for run length
190 * 7 bits for the next coefficient (3 needed)
191 * 2: coefficient, 14 bits (11 needed)
192 *
193 * Coefficients are signed, so are packed in the highest bits for automatic
194 * sign extension.
195 */
196 int16_t *dct_tokens[3][64];
197 int16_t *dct_tokens_base;
198 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
199 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
200 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
201
202 /**
203 * number of blocks that contain DCT coefficients at the given level or high er
204 */
205 int num_coded_frags[3][64];
206 int total_num_coded_frags;
207
179 /* this is a list of indexes into the all_fragments array indicating 208 /* this is a list of indexes into the all_fragments array indicating
180 * which of the fragments are coded */ 209 * which of the fragments are coded */
181 int *coded_fragment_list; 210 int *coded_fragment_list[3];
182 int coded_fragment_list_index;
183 int pixel_addresses_initialized;
184
185 /* track which fragments have already been decoded; called 'fast'
186 * because this data structure avoids having to iterate through every
187 * fragment in coded_fragment_list; once a fragment has been fully
188 * decoded, it is removed from this list */
189 int *fast_fragment_list;
190 int fragment_list_y_head;
191 int fragment_list_c_head;
192 211
193 VLC dc_vlc[16]; 212 VLC dc_vlc[16];
194 VLC ac_vlc_1[16]; 213 VLC ac_vlc_1[16];
195 VLC ac_vlc_2[16]; 214 VLC ac_vlc_2[16];
196 VLC ac_vlc_3[16]; 215 VLC ac_vlc_3[16];
197 VLC ac_vlc_4[16]; 216 VLC ac_vlc_4[16];
198 217
199 VLC superblock_run_length_vlc; 218 VLC superblock_run_length_vlc;
200 VLC fragment_run_length_vlc; 219 VLC fragment_run_length_vlc;
201 VLC mode_code_vlc; 220 VLC mode_code_vlc;
202 VLC motion_vector_vlc; 221 VLC motion_vector_vlc;
203 222
204 /* these arrays need to be on 16-byte boundaries since SSE2 operations 223 /* these arrays need to be on 16-byte boundaries since SSE2 operations
205 * index into them */ 224 * index into them */
206 DECLARE_ALIGNED_16(int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][p lane] 225 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][ plane]
207 226
208 /* This table contains superblock_count * 16 entries. Each set of 16 227 /* This table contains superblock_count * 16 entries. Each set of 16
209 * numbers corresponds to the fragment indexes 0..15 of the superblock. 228 * numbers corresponds to the fragment indexes 0..15 of the superblock.
210 * An entry will be -1 to indicate that no entry corresponds to that 229 * An entry will be -1 to indicate that no entry corresponds to that
211 * index. */ 230 * index. */
212 int *superblock_fragments; 231 int *superblock_fragments;
213 232
214 /* This table contains superblock_count * 4 entries. Each set of 4
215 * numbers corresponds to the macroblock indexes 0..3 of the superblock.
216 * An entry will be -1 to indicate that no entry corresponds to that
217 * index. */
218 int *superblock_macroblocks;
219
220 /* This table contains macroblock_count * 6 entries. Each set of 6
221 * numbers corresponds to the fragment indexes 0..5 which comprise
222 * the macroblock (4 Y fragments and 2 C fragments). */
223 int *macroblock_fragments;
224 /* This is an array that indicates how a particular macroblock 233 /* This is an array that indicates how a particular macroblock
225 * is coded. */ 234 * is coded. */
226 unsigned char *macroblock_coding; 235 unsigned char *macroblock_coding;
227 236
228 int first_coded_y_fragment;
229 int first_coded_c_fragment;
230 int last_coded_y_fragment;
231 int last_coded_c_fragment;
232
233 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc 237 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
234 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16 238 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
235 239
236 /* Huffman decode */ 240 /* Huffman decode */
237 int hti; 241 int hti;
238 unsigned int hbits; 242 unsigned int hbits;
239 int entries; 243 int entries;
240 int huff_code_size; 244 int huff_code_size;
241 uint16_t huffman_table[80][32][2]; 245 uint16_t huffman_table[80][32][2];
242 246
243 uint8_t filter_limit_values[64]; 247 uint8_t filter_limit_values[64];
244 DECLARE_ALIGNED_8(int, bounding_values_array)[256+2]; 248 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
245 } Vp3DecodeContext; 249 } Vp3DecodeContext;
246 250
247 /************************************************************************ 251 /************************************************************************
248 * VP3 specific functions 252 * VP3 specific functions
249 ************************************************************************/ 253 ************************************************************************/
250 254
251 /* 255 /*
252 * This function sets up all of the various blocks mappings: 256 * This function sets up all of the various blocks mappings:
253 * superblocks <-> fragments, macroblocks <-> fragments, 257 * superblocks <-> fragments, macroblocks <-> fragments,
254 * superblocks <-> macroblocks 258 * superblocks <-> macroblocks
255 * 259 *
256 * Returns 0 is successful; returns 1 if *anything* went wrong. 260 * Returns 0 is successful; returns 1 if *anything* went wrong.
257 */ 261 */
258 static int init_block_mapping(Vp3DecodeContext *s) 262 static int init_block_mapping(Vp3DecodeContext *s)
259 { 263 {
260 int i, j; 264 int i, j;
261 signed int hilbert_walk_mb[4]; 265 signed int hilbert_walk_mb[4];
262 266
263 int current_fragment = 0; 267 int current_fragment = 0;
264 int current_width = 0; 268 int current_width = 0;
265 int current_height = 0; 269 int current_height = 0;
266 int right_edge = 0; 270 int right_edge = 0;
267 int bottom_edge = 0; 271 int bottom_edge = 0;
268 int superblock_row_inc = 0; 272 int superblock_row_inc = 0;
269 int mapping_index = 0; 273 int mapping_index = 0;
270 274
271 int current_macroblock;
272 int c_fragment;
273
274 static const signed char travel_width[16] = { 275 static const signed char travel_width[16] = {
275 1, 1, 0, -1, 276 1, 1, 0, -1,
276 0, 0, 1, 0, 277 0, 0, 1, 0,
277 1, 0, 1, 0, 278 1, 0, 1, 0,
278 0, -1, 0, 1 279 0, -1, 0, 1
279 }; 280 };
280 281
281 static const signed char travel_height[16] = { 282 static const signed char travel_height[16] = {
282 0, 0, 1, 0, 283 0, 0, 1, 0,
283 1, 1, 0, -1, 284 1, 1, 0, -1,
284 0, 1, 0, -1, 285 0, 1, 0, -1,
285 -1, 0, -1, 0 286 -1, 0, -1, 0
286 }; 287 };
287 288
288 static const signed char travel_width_mb[4] = {
289 1, 0, 1, 0
290 };
291
292 static const signed char travel_height_mb[4] = {
293 0, 1, 0, -1
294 };
295
296 hilbert_walk_mb[0] = 1; 289 hilbert_walk_mb[0] = 1;
297 hilbert_walk_mb[1] = s->macroblock_width; 290 hilbert_walk_mb[1] = s->macroblock_width;
298 hilbert_walk_mb[2] = 1; 291 hilbert_walk_mb[2] = 1;
299 hilbert_walk_mb[3] = -s->macroblock_width; 292 hilbert_walk_mb[3] = -s->macroblock_width;
300 293
301 /* iterate through each superblock (all planes) and map the fragments */ 294 /* iterate through each superblock (all planes) and map the fragments */
302 for (i = 0; i < s->superblock_count; i++) { 295 for (i = 0; i < s->superblock_count; i++) {
303 /* time to re-assign the limits? */ 296 /* time to re-assign the limits? */
304 if (i == 0) { 297 if (i == 0) {
305 298
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 (current_height < bottom_edge)) { 355 (current_height < bottom_edge)) {
363 s->superblock_fragments[mapping_index] = current_fragment; 356 s->superblock_fragments[mapping_index] = current_fragment;
364 } else { 357 } else {
365 s->superblock_fragments[mapping_index] = -1; 358 s->superblock_fragments[mapping_index] = -1;
366 } 359 }
367 360
368 mapping_index++; 361 mapping_index++;
369 } 362 }
370 } 363 }
371 364
372 /* initialize the superblock <-> macroblock mapping; iterate through
373 * all of the Y plane superblocks to build this mapping */
374 right_edge = s->macroblock_width;
375 bottom_edge = s->macroblock_height;
376 current_width = -1;
377 current_height = 0;
378 superblock_row_inc = s->macroblock_width -
379 (s->y_superblock_width * 2 - s->macroblock_width);
380 mapping_index = 0;
381 current_macroblock = -1;
382 for (i = 0; i < s->u_superblock_start; i++) {
383
384 if (current_width >= right_edge - 1) {
385 /* reset width and move to next superblock row */
386 current_width = -1;
387 current_height += 2;
388
389 /* macroblock is now at the start of a new superblock row */
390 current_macroblock += superblock_row_inc;
391 }
392
393 /* iterate through each potential macroblock in the superblock */
394 for (j = 0; j < 4; j++) {
395 current_macroblock += hilbert_walk_mb[j];
396 current_width += travel_width_mb[j];
397 current_height += travel_height_mb[j];
398
399 /* check if the macroblock is in bounds */
400 if ((current_width < right_edge) &&
401 (current_height < bottom_edge)) {
402 s->superblock_macroblocks[mapping_index] = current_macroblock;
403 } else {
404 s->superblock_macroblocks[mapping_index] = -1;
405 }
406
407 mapping_index++;
408 }
409 }
410
411 /* initialize the macroblock <-> fragment mapping */
412 current_fragment = 0;
413 current_macroblock = 0;
414 mapping_index = 0;
415 for (i = 0; i < s->fragment_height; i += 2) {
416
417 for (j = 0; j < s->fragment_width; j += 2) {
418
419 s->all_fragments[current_fragment].macroblock = current_macroblock;
420 s->macroblock_fragments[mapping_index++] = current_fragment;
421
422 if (j + 1 < s->fragment_width) {
423 s->all_fragments[current_fragment + 1].macroblock = current_macr oblock;
424 s->macroblock_fragments[mapping_index++] = current_fragment + 1;
425 } else
426 s->macroblock_fragments[mapping_index++] = -1;
427
428 if (i + 1 < s->fragment_height) {
429 s->all_fragments[current_fragment + s->fragment_width].macrobloc k =
430 current_macroblock;
431 s->macroblock_fragments[mapping_index++] =
432 current_fragment + s->fragment_width;
433 } else
434 s->macroblock_fragments[mapping_index++] = -1;
435
436 if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
437 s->all_fragments[current_fragment + s->fragment_width + 1].macro block =
438 current_macroblock;
439 s->macroblock_fragments[mapping_index++] =
440 current_fragment + s->fragment_width + 1;
441 } else
442 s->macroblock_fragments[mapping_index++] = -1;
443
444 /* C planes */
445 c_fragment = s->fragment_start[1] +
446 (i * s->fragment_width / 4) + (j / 2);
447 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
448 s->macroblock_fragments[mapping_index++] = c_fragment;
449
450 c_fragment = s->fragment_start[2] +
451 (i * s->fragment_width / 4) + (j / 2);
452 s->all_fragments[c_fragment].macroblock = s->macroblock_count;
453 s->macroblock_fragments[mapping_index++] = c_fragment;
454
455 if (j + 2 <= s->fragment_width)
456 current_fragment += 2;
457 else
458 current_fragment++;
459 current_macroblock++;
460 }
461
462 current_fragment += s->fragment_width;
463 }
464
465 return 0; /* successful path out */ 365 return 0; /* successful path out */
466 } 366 }
467 367
468 /* 368 /*
469 * This function wipes out all of the fragment data. 369 * This function wipes out all of the fragment data.
470 */ 370 */
471 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb) 371 static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
472 { 372 {
473 int i; 373 int i;
474 374
475 /* zero out all of the fragment information */ 375 /* zero out all of the fragment information */
476 s->coded_fragment_list_index = 0;
477 for (i = 0; i < s->fragment_count; i++) { 376 for (i = 0; i < s->fragment_count; i++) {
478 s->coeff_counts[i] = 0;
479 s->all_fragments[i].motion_x = 127; 377 s->all_fragments[i].motion_x = 127;
480 s->all_fragments[i].motion_y = 127; 378 s->all_fragments[i].motion_y = 127;
481 s->all_fragments[i].next_coeff= NULL; 379 s->all_fragments[i].dc = 0;
482 s->all_fragments[i].qpi = 0; 380 s->all_fragments[i].qpi = 0;
483 s->coeffs[i].index=
484 s->coeffs[i].coeff=0;
485 s->coeffs[i].next= NULL;
486 } 381 }
487 } 382 }
488 383
489 /* 384 /*
490 * This function sets up the dequantization tables used for a particular 385 * This function sets up the dequantization tables used for a particular
491 * frame. 386 * frame.
492 */ 387 */
493 static void init_dequantizer(Vp3DecodeContext *s, int qpi) 388 static void init_dequantizer(Vp3DecodeContext *s, int qpi)
494 { 389 {
495 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]]; 390 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 bounding_values[128] = value; 450 bounding_values[128] = value;
556 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202; 451 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
557 } 452 }
558 453
559 /* 454 /*
560 * This function unpacks all of the superblock/macroblock/fragment coding 455 * This function unpacks all of the superblock/macroblock/fragment coding
561 * information from the bitstream. 456 * information from the bitstream.
562 */ 457 */
563 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) 458 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
564 { 459 {
460 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
565 int bit = 0; 461 int bit = 0;
566 int current_superblock = 0; 462 int current_superblock = 0;
567 int current_run = 0; 463 int current_run = 0;
568 int decode_fully_flags = 0; 464 int num_partial_superblocks = 0;
569 int decode_partial_blocks = 0;
570 int first_c_fragment_seen;
571 465
572 int i, j; 466 int i, j;
573 int current_fragment; 467 int current_fragment;
468 int plane;
574 469
575 if (s->keyframe) { 470 if (s->keyframe) {
576 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count); 471 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
577 472
578 } else { 473 } else {
579 474
580 /* unpack the list of partially-coded superblocks */ 475 /* unpack the list of partially-coded superblocks */
581 bit = get_bits1(gb); 476 bit = get_bits1(gb);
582 /* toggle the bit because as soon as the first run length is
583 * fetched the bit will be toggled again */
584 bit ^= 1;
585 while (current_superblock < s->superblock_count) { 477 while (current_superblock < s->superblock_count) {
586 if (current_run-- == 0) {
587 bit ^= 1;
588 current_run = get_vlc2(gb, 478 current_run = get_vlc2(gb,
589 s->superblock_run_length_vlc.table, 6, 2); 479 s->superblock_run_length_vlc.table, 6, 2) + 1;
590 if (current_run == 33) 480 if (current_run == 34)
591 current_run += get_bits(gb, 12); 481 current_run += get_bits(gb, 12);
592 482
593 /* if any of the superblocks are not partially coded, flag 483 if (current_superblock + current_run > s->superblock_count) {
594 * a boolean to decode the list of fully-coded superblocks */ 484 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblo ck run length\n");
595 if (bit == 0) { 485 return -1;
596 decode_fully_flags = 1; 486 }
597 } else {
598 487
599 /* make a note of the fact that there are partially coded 488 memset(s->superblock_coding + current_superblock, bit, current_run);
600 * superblocks */ 489
601 decode_partial_blocks = 1; 490 current_superblock += current_run;
602 } 491 if (bit)
603 } 492 num_partial_superblocks += current_run;
604 s->superblock_coding[current_superblock++] = bit; 493
494 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
495 bit = get_bits1(gb);
496 else
497 bit ^= 1;
605 } 498 }
606 499
607 /* unpack the list of fully coded superblocks if any of the blocks were 500 /* unpack the list of fully coded superblocks if any of the blocks were
608 * not marked as partially coded in the previous step */ 501 * not marked as partially coded in the previous step */
609 if (decode_fully_flags) { 502 if (num_partial_superblocks < s->superblock_count) {
503 int superblocks_decoded = 0;
610 504
611 current_superblock = 0; 505 current_superblock = 0;
612 current_run = 0;
613 bit = get_bits1(gb); 506 bit = get_bits1(gb);
614 /* toggle the bit because as soon as the first run length is 507 while (superblocks_decoded < s->superblock_count - num_partial_super blocks) {
615 * fetched the bit will be toggled again */ 508 current_run = get_vlc2(gb,
616 bit ^= 1; 509 s->superblock_run_length_vlc.table, 6, 2) + 1;
617 while (current_superblock < s->superblock_count) { 510 if (current_run == 34)
511 current_run += get_bits(gb, 12);
512
513 for (j = 0; j < current_run; current_superblock++) {
514 if (current_superblock >= s->superblock_count) {
515 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded supe rblock run length\n");
516 return -1;
517 }
618 518
619 /* skip any superblocks already marked as partially coded */ 519 /* skip any superblocks already marked as partially coded */
620 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) { 520 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
521 s->superblock_coding[current_superblock] = 2*bit;
522 j++;
523 }
524 }
525 superblocks_decoded += current_run;
621 526
622 if (current_run-- == 0) { 527 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
623 bit ^= 1; 528 bit = get_bits1(gb);
624 current_run = get_vlc2(gb, 529 else
625 s->superblock_run_length_vlc.table, 6, 2); 530 bit ^= 1;
626 if (current_run == 33)
627 current_run += get_bits(gb, 12);
628 }
629 s->superblock_coding[current_superblock] = 2*bit;
630 }
631 current_superblock++;
632 } 531 }
633 } 532 }
634 533
635 /* if there were partial blocks, initialize bitstream for 534 /* if there were partial blocks, initialize bitstream for
636 * unpacking fragment codings */ 535 * unpacking fragment codings */
637 if (decode_partial_blocks) { 536 if (num_partial_superblocks) {
638 537
639 current_run = 0; 538 current_run = 0;
640 bit = get_bits1(gb); 539 bit = get_bits1(gb);
641 /* toggle the bit because as soon as the first run length is 540 /* toggle the bit because as soon as the first run length is
642 * fetched the bit will be toggled again */ 541 * fetched the bit will be toggled again */
643 bit ^= 1; 542 bit ^= 1;
644 } 543 }
645 } 544 }
646 545
647 /* figure out which fragments are coded; iterate through each 546 /* figure out which fragments are coded; iterate through each
648 * superblock (all planes) */ 547 * superblock (all planes) */
649 s->coded_fragment_list_index = 0; 548 s->total_num_coded_frags = 0;
650 s->next_coeff= s->coeffs + s->fragment_count;
651 s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
652 s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
653 first_c_fragment_seen = 0;
654 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count); 549 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
655 for (i = 0; i < s->superblock_count; i++) { 550
551 for (plane = 0; plane < 3; plane++) {
552 int sb_start = superblock_starts[plane];
553 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock _count);
554 int num_coded_frags = 0;
555
556 for (i = sb_start; i < sb_end; i++) {
656 557
657 /* iterate through all 16 fragments in a superblock */ 558 /* iterate through all 16 fragments in a superblock */
658 for (j = 0; j < 16; j++) { 559 for (j = 0; j < 16; j++) {
659 560
660 /* if the fragment is in bounds, check its coding status */ 561 /* if the fragment is in bounds, check its coding status */
661 current_fragment = s->superblock_fragments[i * 16 + j]; 562 current_fragment = s->superblock_fragments[i * 16 + j];
662 if (current_fragment >= s->fragment_count) { 563 if (current_fragment >= s->fragment_count) {
663 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n", 564 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
664 current_fragment, s->fragment_count); 565 current_fragment, s->fragment_count);
665 return 1; 566 return 1;
666 } 567 }
667 if (current_fragment != -1) { 568 if (current_fragment != -1) {
668 if (s->superblock_coding[i] == SB_NOT_CODED) { 569 int coded = s->superblock_coding[i];
669 570
670 /* copy all the fragments from the prior frame */ 571 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
671 s->all_fragments[current_fragment].coding_method =
672 MODE_COPY;
673
674 } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
675 572
676 /* fragment may or may not be coded; this is the case 573 /* fragment may or may not be coded; this is the case
677 * that cares about the fragment coding runs */ 574 * that cares about the fragment coding runs */
678 if (current_run-- == 0) { 575 if (current_run-- == 0) {
679 bit ^= 1; 576 bit ^= 1;
680 current_run = get_vlc2(gb, 577 current_run = get_vlc2(gb,
681 s->fragment_run_length_vlc.table, 5, 2); 578 s->fragment_run_length_vlc.table, 5, 2);
682 } 579 }
580 coded = bit;
581 }
683 582
684 if (bit) { 583 if (coded) {
685 /* default mode; actual mode will be decoded in 584 /* default mode; actual mode will be decoded in
686 * the next phase */ 585 * the next phase */
687 s->all_fragments[current_fragment].coding_method = 586 s->all_fragments[current_fragment].coding_method =
688 MODE_INTER_NO_MV; 587 MODE_INTER_NO_MV;
689 s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment; 588 s->coded_fragment_list[plane][num_coded_frags++] =
690 s->coded_fragment_list[s->coded_fragment_list_index] =
691 current_fragment; 589 current_fragment;
692 if ((current_fragment >= s->fragment_start[1]) &&
693 (s->last_coded_y_fragment == -1) &&
694 (!first_c_fragment_seen)) {
695 s->first_coded_c_fragment = s->coded_fragment_list_i ndex;
696 s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
697 first_c_fragment_seen = 1;
698 }
699 s->coded_fragment_list_index++;
700 s->macroblock_coding[s->all_fragments[current_fragment]. macroblock] = MODE_INTER_NO_MV;
701 } else { 590 } else {
702 /* not coded; copy this fragment from the prior frame */ 591 /* not coded; copy this fragment from the prior frame */
703 s->all_fragments[current_fragment].coding_method = 592 s->all_fragments[current_fragment].coding_method =
704 MODE_COPY; 593 MODE_COPY;
705 } 594 }
706
707 } else {
708
709 /* fragments are fully coded in this superblock; actual
710 * coding will be determined in next step */
711 s->all_fragments[current_fragment].coding_method =
712 MODE_INTER_NO_MV;
713 s->all_fragments[current_fragment].next_coeff= s->coeffs + c urrent_fragment;
714 s->coded_fragment_list[s->coded_fragment_list_index] =
715 current_fragment;
716 if ((current_fragment >= s->fragment_start[1]) &&
717 (s->last_coded_y_fragment == -1) &&
718 (!first_c_fragment_seen)) {
719 s->first_coded_c_fragment = s->coded_fragment_list_index ;
720 s->last_coded_y_fragment = s->first_coded_c_fragment - 1 ;
721 first_c_fragment_seen = 1;
722 }
723 s->coded_fragment_list_index++;
724 s->macroblock_coding[s->all_fragments[current_fragment].macr oblock] = MODE_INTER_NO_MV;
725 }
726 } 595 }
727 } 596 }
728 } 597 }
729 598 s->total_num_coded_frags += num_coded_frags;
730 if (!first_c_fragment_seen) 599 for (i = 0; i < 64; i++)
731 /* only Y fragments coded in this frame */ 600 s->num_coded_frags[plane][i] = num_coded_frags;
732 s->last_coded_y_fragment = s->coded_fragment_list_index - 1; 601 if (plane < 2)
733 else 602 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + nu m_coded_frags;
734 /* end the list of coded C fragments */
735 s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
736
737 for (i = 0; i < s->fragment_count - 1; i++) {
738 s->fast_fragment_list[i] = i + 1;
739 } 603 }
740 s->fast_fragment_list[s->fragment_count - 1] = -1;
741
742 if (s->last_coded_y_fragment == -1)
743 s->fragment_list_y_head = -1;
744 else {
745 s->fragment_list_y_head = s->first_coded_y_fragment;
746 s->fast_fragment_list[s->last_coded_y_fragment] = -1;
747 }
748
749 if (s->last_coded_c_fragment == -1)
750 s->fragment_list_c_head = -1;
751 else {
752 s->fragment_list_c_head = s->first_coded_c_fragment;
753 s->fast_fragment_list[s->last_coded_c_fragment] = -1;
754 }
755
756 return 0; 604 return 0;
757 } 605 }
758 606
759 /* 607 /*
760 * This function unpacks all the coding mode data for individual macroblocks 608 * This function unpacks all the coding mode data for individual macroblocks
761 * from the bitstream. 609 * from the bitstream.
762 */ 610 */
763 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) 611 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
764 { 612 {
765 int i, j, k; 613 int i, j, k, sb_x, sb_y;
766 int scheme; 614 int scheme;
767 int current_macroblock; 615 int current_macroblock;
768 int current_fragment; 616 int current_fragment;
769 int coding_mode; 617 int coding_mode;
770 int custom_mode_alphabet[CODING_MODE_COUNT]; 618 int custom_mode_alphabet[CODING_MODE_COUNT];
619 const int *alphabet;
771 620
772 if (s->keyframe) { 621 if (s->keyframe) {
773 for (i = 0; i < s->fragment_count; i++) 622 for (i = 0; i < s->fragment_count; i++)
774 s->all_fragments[i].coding_method = MODE_INTRA; 623 s->all_fragments[i].coding_method = MODE_INTRA;
775 624
776 } else { 625 } else {
777 626
778 /* fetch the mode coding scheme for this frame */ 627 /* fetch the mode coding scheme for this frame */
779 scheme = get_bits(gb, 3); 628 scheme = get_bits(gb, 3);
780 629
781 /* is it a custom coding scheme? */ 630 /* is it a custom coding scheme? */
782 if (scheme == 0) { 631 if (scheme == 0) {
783 for (i = 0; i < 8; i++) 632 for (i = 0; i < 8; i++)
784 custom_mode_alphabet[i] = MODE_INTER_NO_MV; 633 custom_mode_alphabet[i] = MODE_INTER_NO_MV;
785 for (i = 0; i < 8; i++) 634 for (i = 0; i < 8; i++)
786 custom_mode_alphabet[get_bits(gb, 3)] = i; 635 custom_mode_alphabet[get_bits(gb, 3)] = i;
787 } 636 alphabet = custom_mode_alphabet;
637 } else
638 alphabet = ModeAlphabet[scheme-1];
788 639
789 /* iterate through all of the macroblocks that contain 1 or more 640 /* iterate through all of the macroblocks that contain 1 or more
790 * coded fragments */ 641 * coded fragments */
791 for (i = 0; i < s->u_superblock_start; i++) { 642 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
643 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
792 644
793 for (j = 0; j < 4; j++) { 645 for (j = 0; j < 4; j++) {
794 current_macroblock = s->superblock_macroblocks[i * 4 + j]; 646 int mb_x = 2*sb_x + (j>>1);
795 if ((current_macroblock == -1) || 647 int mb_y = 2*sb_y + (((j>>1)+j)&1);
796 (s->macroblock_coding[current_macroblock] == MODE_COPY)) 648 current_macroblock = mb_y * s->macroblock_width + mb_x;
649
650 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
797 continue; 651 continue;
798 if (current_macroblock >= s->macroblock_count) { 652
799 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad ma croblock number (%d >= %d)\n", 653 #define BLOCK_X (2*mb_x + (k&1))
800 current_macroblock, s->macroblock_count); 654 #define BLOCK_Y (2*mb_y + (k>>1))
801 return 1; 655 /* coding modes are only stored if the macroblock has at least o ne
656 * luma block coded, otherwise it must be INTER_NO_MV */
657 for (k = 0; k < 4; k++) {
658 current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
659 if (s->all_fragments[current_fragment].coding_method != MODE _COPY)
660 break;
661 }
662 if (k == 4) {
663 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
664 continue;
802 } 665 }
803 666
804 /* mode 7 means get 3 bits for each coding mode */ 667 /* mode 7 means get 3 bits for each coding mode */
805 if (scheme == 7) 668 if (scheme == 7)
806 coding_mode = get_bits(gb, 3); 669 coding_mode = get_bits(gb, 3);
807 else if(scheme == 0)
808 coding_mode = custom_mode_alphabet
809 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
810 else 670 else
811 coding_mode = ModeAlphabet[scheme-1] 671 coding_mode = alphabet
812 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; 672 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
813 673
814 s->macroblock_coding[current_macroblock] = coding_mode; 674 s->macroblock_coding[current_macroblock] = coding_mode;
815 for (k = 0; k < 6; k++) { 675 for (k = 0; k < 4; k++) {
816 current_fragment = 676 current_fragment =
817 s->macroblock_fragments[current_macroblock * 6 + k]; 677 BLOCK_Y*s->fragment_width + BLOCK_X;
818 if (current_fragment == -1) 678 if (s->all_fragments[current_fragment].coding_method !=
819 continue; 679 MODE_COPY)
820 if (current_fragment >= s->fragment_count) { 680 s->all_fragments[current_fragment].coding_method =
821 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): ba d fragment number (%d >= %d)\n", 681 coding_mode;
822 current_fragment, s->fragment_count); 682 }
823 return 1; 683 for (k = 0; k < 2; k++) {
824 } 684 current_fragment = s->fragment_start[k+1] +
685 mb_y*(s->fragment_width>>1) + mb_x;
825 if (s->all_fragments[current_fragment].coding_method != 686 if (s->all_fragments[current_fragment].coding_method !=
826 MODE_COPY) 687 MODE_COPY)
827 s->all_fragments[current_fragment].coding_method = 688 s->all_fragments[current_fragment].coding_method =
828 coding_mode; 689 coding_mode;
829 } 690 }
830 } 691 }
692 }
831 } 693 }
832 } 694 }
833 695
834 return 0; 696 return 0;
835 } 697 }
836 698
837 /* 699 /*
838 * This function unpacks all the motion vectors for the individual 700 * This function unpacks all the motion vectors for the individual
839 * macroblocks from the bitstream. 701 * macroblocks from the bitstream.
840 */ 702 */
841 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) 703 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
842 { 704 {
843 int i, j, k, l; 705 int j, k, sb_x, sb_y;
844 int coding_mode; 706 int coding_mode;
845 int motion_x[6]; 707 int motion_x[6];
846 int motion_y[6]; 708 int motion_y[6];
847 int last_motion_x = 0; 709 int last_motion_x = 0;
848 int last_motion_y = 0; 710 int last_motion_y = 0;
849 int prior_last_motion_x = 0; 711 int prior_last_motion_x = 0;
850 int prior_last_motion_y = 0; 712 int prior_last_motion_y = 0;
851 int current_macroblock; 713 int current_macroblock;
852 int current_fragment; 714 int current_fragment;
853 715
854 if (s->keyframe) 716 if (s->keyframe)
855 return 0; 717 return 0;
856 718
857 memset(motion_x, 0, 6 * sizeof(int)); 719 memset(motion_x, 0, 6 * sizeof(int));
858 memset(motion_y, 0, 6 * sizeof(int)); 720 memset(motion_y, 0, 6 * sizeof(int));
859 721
860 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */ 722 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
861 coding_mode = get_bits1(gb); 723 coding_mode = get_bits1(gb);
862 724
863 /* iterate through all of the macroblocks that contain 1 or more 725 /* iterate through all of the macroblocks that contain 1 or more
864 * coded fragments */ 726 * coded fragments */
865 for (i = 0; i < s->u_superblock_start; i++) { 727 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
728 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
866 729
867 for (j = 0; j < 4; j++) { 730 for (j = 0; j < 4; j++) {
868 current_macroblock = s->superblock_macroblocks[i * 4 + j]; 731 int mb_x = 2*sb_x + (j>>1);
869 if ((current_macroblock == -1) || 732 int mb_y = 2*sb_y + (((j>>1)+j)&1);
733 current_macroblock = mb_y * s->macroblock_width + mb_x;
734
735 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
870 (s->macroblock_coding[current_macroblock] == MODE_COPY)) 736 (s->macroblock_coding[current_macroblock] == MODE_COPY))
871 continue; 737 continue;
872 if (current_macroblock >= s->macroblock_count) {
873 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macr oblock number (%d >= %d)\n",
874 current_macroblock, s->macroblock_count);
875 return 1;
876 }
877 738
878 current_fragment = s->macroblock_fragments[current_macroblock * 6];
879 if (current_fragment >= s->fragment_count) {
880 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad frag ment number (%d >= %d\n",
881 current_fragment, s->fragment_count);
882 return 1;
883 }
884 switch (s->macroblock_coding[current_macroblock]) { 739 switch (s->macroblock_coding[current_macroblock]) {
885 740
886 case MODE_INTER_PLUS_MV: 741 case MODE_INTER_PLUS_MV:
887 case MODE_GOLDEN_MV: 742 case MODE_GOLDEN_MV:
888 /* all 6 fragments use the same motion vector */ 743 /* all 6 fragments use the same motion vector */
889 if (coding_mode == 0) { 744 if (coding_mode == 0) {
890 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vec tor_vlc.table, 6, 2)]; 745 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vec tor_vlc.table, 6, 2)];
891 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vec tor_vlc.table, 6, 2)]; 746 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vec tor_vlc.table, 6, 2)];
892 } else { 747 } else {
893 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; 748 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
(...skipping 12 matching lines...) Expand all
906 761
907 case MODE_INTER_FOURMV: 762 case MODE_INTER_FOURMV:
908 /* vector maintenance */ 763 /* vector maintenance */
909 prior_last_motion_x = last_motion_x; 764 prior_last_motion_x = last_motion_x;
910 prior_last_motion_y = last_motion_y; 765 prior_last_motion_y = last_motion_y;
911 766
912 /* fetch 4 vectors from the bitstream, one for each 767 /* fetch 4 vectors from the bitstream, one for each
913 * Y fragment, then average for the C fragment vectors */ 768 * Y fragment, then average for the C fragment vectors */
914 motion_x[4] = motion_y[4] = 0; 769 motion_x[4] = motion_y[4] = 0;
915 for (k = 0; k < 4; k++) { 770 for (k = 0; k < 4; k++) {
916 for (l = 0; l < s->coded_fragment_list_index; l++) 771 current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
917 if (s->coded_fragment_list[l] == s->macroblock_fragments [6*current_macroblock + k]) 772 if (s->all_fragments[current_fragment].coding_method != MODE _COPY) {
918 break;
919 if (l < s->coded_fragment_list_index) {
920 if (coding_mode == 0) { 773 if (coding_mode == 0) {
921 motion_x[k] = motion_vector_table[get_vlc2(gb, s->mo tion_vector_vlc.table, 6, 2)]; 774 motion_x[k] = motion_vector_table[get_vlc2(gb, s->mo tion_vector_vlc.table, 6, 2)];
922 motion_y[k] = motion_vector_table[get_vlc2(gb, s->mo tion_vector_vlc.table, 6, 2)]; 775 motion_y[k] = motion_vector_table[get_vlc2(gb, s->mo tion_vector_vlc.table, 6, 2)];
923 } else { 776 } else {
924 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; 777 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
925 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; 778 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
926 } 779 }
927 last_motion_x = motion_x[k]; 780 last_motion_x = motion_x[k];
928 last_motion_y = motion_y[k]; 781 last_motion_y = motion_y[k];
929 } else { 782 } else {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 default: 818 default:
966 /* covers intra, inter without MV, golden without MV */ 819 /* covers intra, inter without MV, golden without MV */
967 motion_x[0] = 0; 820 motion_x[0] = 0;
968 motion_y[0] = 0; 821 motion_y[0] = 0;
969 822
970 /* no vector maintenance */ 823 /* no vector maintenance */
971 break; 824 break;
972 } 825 }
973 826
974 /* assign the motion vectors to the correct fragments */ 827 /* assign the motion vectors to the correct fragments */
975 for (k = 0; k < 6; k++) { 828 for (k = 0; k < 4; k++) {
976 current_fragment = 829 current_fragment =
977 s->macroblock_fragments[current_macroblock * 6 + k]; 830 BLOCK_Y*s->fragment_width + BLOCK_X;
978 if (current_fragment == -1)
979 continue;
980 if (current_fragment >= s->fragment_count) {
981 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
982 current_fragment, s->fragment_count);
983 return 1;
984 }
985 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURM V) { 831 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURM V) {
986 s->all_fragments[current_fragment].motion_x = motion_x[k]; 832 s->all_fragments[current_fragment].motion_x = motion_x[k];
987 s->all_fragments[current_fragment].motion_y = motion_y[k]; 833 s->all_fragments[current_fragment].motion_y = motion_y[k];
988 } else { 834 } else {
989 s->all_fragments[current_fragment].motion_x = motion_x[0]; 835 s->all_fragments[current_fragment].motion_x = motion_x[0];
990 s->all_fragments[current_fragment].motion_y = motion_y[0]; 836 s->all_fragments[current_fragment].motion_y = motion_y[0];
991 } 837 }
992 } 838 }
839 for (k = 0; k < 2; k++) {
840 current_fragment = s->fragment_start[k+1] +
841 mb_y*(s->fragment_width>>1) + mb_x;
842 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURM V) {
843 s->all_fragments[current_fragment].motion_x = motion_x[k+4];
844 s->all_fragments[current_fragment].motion_y = motion_y[k+4];
845 } else {
846 s->all_fragments[current_fragment].motion_x = motion_x[0];
847 s->all_fragments[current_fragment].motion_y = motion_y[0];
848 }
849 }
993 } 850 }
851 }
994 } 852 }
995 853
996 return 0; 854 return 0;
997 } 855 }
998 856
999 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) 857 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
1000 { 858 {
1001 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi; 859 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
1002 int num_blocks = s->coded_fragment_list_index; 860 int num_blocks = s->total_num_coded_frags;
1003 861
1004 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) { 862 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
1005 i = blocks_decoded = num_blocks_at_qpi = 0; 863 i = blocks_decoded = num_blocks_at_qpi = 0;
1006 864
1007 bit = get_bits1(gb); 865 bit = get_bits1(gb);
1008 866
1009 do { 867 do {
1010 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1; 868 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
1011 if (run_length == 34) 869 if (run_length == 34)
1012 run_length += get_bits(gb, 12); 870 run_length += get_bits(gb, 12);
1013 blocks_decoded += run_length; 871 blocks_decoded += run_length;
1014 872
1015 if (!bit) 873 if (!bit)
1016 num_blocks_at_qpi += run_length; 874 num_blocks_at_qpi += run_length;
1017 875
1018 for (j = 0; j < run_length; i++) { 876 for (j = 0; j < run_length; i++) {
1019 if (i >= s->coded_fragment_list_index) 877 if (i >= s->total_num_coded_frags)
1020 return -1; 878 return -1;
1021 879
1022 if (s->all_fragments[s->coded_fragment_list[i]].qpi == qpi) { 880 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
1023 s->all_fragments[s->coded_fragment_list[i]].qpi += bit; 881 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
1024 j++; 882 j++;
1025 } 883 }
1026 } 884 }
1027 885
1028 if (run_length == 4129) 886 if (run_length == MAXIMUM_LONG_BIT_RUN)
1029 bit = get_bits1(gb); 887 bit = get_bits1(gb);
1030 else 888 else
1031 bit ^= 1; 889 bit ^= 1;
1032 } while (blocks_decoded < num_blocks); 890 } while (blocks_decoded < num_blocks);
1033 891
1034 num_blocks -= num_blocks_at_qpi; 892 num_blocks -= num_blocks_at_qpi;
1035 } 893 }
1036 894
1037 return 0; 895 return 0;
1038 } 896 }
1039 897
1040 /* 898 /*
1041 * This function is called by unpack_dct_coeffs() to extract the VLCs from 899 * This function is called by unpack_dct_coeffs() to extract the VLCs from
1042 * the bitstream. The VLCs encode tokens which are used to unpack DCT 900 * the bitstream. The VLCs encode tokens which are used to unpack DCT
1043 * data. This function unpacks all the VLCs for either the Y plane or both 901 * data. This function unpacks all the VLCs for either the Y plane or both
1044 * C planes, and is called for DC coefficients or different AC coefficient 902 * C planes, and is called for DC coefficients or different AC coefficient
1045 * levels (since different coefficient types require different VLC tables. 903 * levels (since different coefficient types require different VLC tables.
1046 * 904 *
1047 * This function returns a residual eob run. E.g, if a particular token gave 905 * This function returns a residual eob run. E.g, if a particular token gave
1048 * instructions to EOB the next 5 fragments and there were only 2 fragments 906 * instructions to EOB the next 5 fragments and there were only 2 fragments
1049 * left in the current fragment range, 3 would be returned so that it could 907 * left in the current fragment range, 3 would be returned so that it could
1050 * be passed into the next call to this same function. 908 * be passed into the next call to this same function.
1051 */ 909 */
1052 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, 910 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
1053 VLC *table, int coeff_index, 911 VLC *table, int coeff_index,
1054 int y_plane, 912 int plane,
1055 int eob_run) 913 int eob_run)
1056 { 914 {
1057 int i; 915 int i, j = 0;
1058 int token; 916 int token;
1059 int zero_run = 0; 917 int zero_run = 0;
1060 DCTELEM coeff = 0; 918 DCTELEM coeff = 0;
1061 Vp3Fragment *fragment;
1062 int bits_to_get; 919 int bits_to_get;
1063 int next_fragment; 920 int blocks_ended;
1064 int previous_fragment; 921 int coeff_i = 0;
1065 int fragment_num; 922 int num_coeffs = s->num_coded_frags[plane][coeff_index];
1066 int *list_head; 923 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
1067 924
1068 #ifdef ALT_BITSTREAM_READER 925 #ifdef ALT_BITSTREAM_READER
1069 if (gb->buffer_exhausted && gb->index > gb->size_in_bits) 926 if (gb->buffer_exhausted && gb->index > gb->size_in_bits)
1070 return 0; 927 return 0;
1071 #endif 928 #endif
1072 929
1073 /* local references to structure members to avoid repeated deferences */ 930 /* local references to structure members to avoid repeated deferences */
1074 uint8_t *perm= s->scantable.permutated; 931 int *coded_fragment_list = s->coded_fragment_list[plane];
1075 int *coded_fragment_list = s->coded_fragment_list;
1076 Vp3Fragment *all_fragments = s->all_fragments; 932 Vp3Fragment *all_fragments = s->all_fragments;
1077 uint8_t *coeff_counts = s->coeff_counts;
1078 VLC_TYPE (*vlc_table)[2] = table->table; 933 VLC_TYPE (*vlc_table)[2] = table->table;
1079 int *fast_fragment_list = s->fast_fragment_list;
1080 934
1081 if (y_plane) { 935 if (num_coeffs < 0)
1082 next_fragment = s->fragment_list_y_head; 936 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level % d\n", coeff_index);
1083 list_head = &s->fragment_list_y_head; 937
938 if (eob_run > num_coeffs) {
939 coeff_i = blocks_ended = num_coeffs;
940 eob_run -= num_coeffs;
1084 } else { 941 } else {
1085 next_fragment = s->fragment_list_c_head; 942 coeff_i = blocks_ended = eob_run;
1086 list_head = &s->fragment_list_c_head; 943 eob_run = 0;
1087 } 944 }
1088 945
1089 i = next_fragment; 946 // insert fake EOB token to cover the split between planes or zzi
1090 previous_fragment = -1; /* this indicates that the previous fragment is act ually the list head */ 947 if (blocks_ended)
1091 while (i != -1) { 948 dct_tokens[j++] = blocks_ended << 2;
1092 fragment_num = coded_fragment_list[i];
1093 949
1094 if (coeff_counts[fragment_num] > coeff_index) { 950 while (coeff_i < num_coeffs) {
1095 previous_fragment = i;
1096 i = fast_fragment_list[i];
1097 continue;
1098 }
1099 fragment = &all_fragments[fragment_num];
1100
1101 if (!eob_run) {
1102 /* decode a VLC into a token */ 951 /* decode a VLC into a token */
1103 token = get_vlc2(gb, vlc_table, 5, 3); 952 token = get_vlc2(gb, vlc_table, 5, 3);
1104 /* use the token to get a zero run, a coefficient, and an eob run */ 953 /* use the token to get a zero run, a coefficient, and an eob run */
1105 if (token <= 6) { 954 if (token <= 6) {
1106 eob_run = eob_run_base[token]; 955 eob_run = eob_run_base[token];
1107 if (eob_run_get_bits[token]) 956 if (eob_run_get_bits[token])
1108 eob_run += get_bits(gb, eob_run_get_bits[token]); 957 eob_run += get_bits(gb, eob_run_get_bits[token]);
1109 coeff = zero_run = 0; 958
959 // record only the number of blocks ended in this plane,
960 // any spill will be recorded in the next plane.
961 if (eob_run > num_coeffs - coeff_i) {
962 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
963 blocks_ended += num_coeffs - coeff_i;
964 eob_run -= num_coeffs - coeff_i;
965 coeff_i = num_coeffs;
966 } else {
967 dct_tokens[j++] = TOKEN_EOB(eob_run);
968 blocks_ended += eob_run;
969 coeff_i += eob_run;
970 eob_run = 0;
971 }
1110 } else { 972 } else {
1111 bits_to_get = coeff_get_bits[token]; 973 bits_to_get = coeff_get_bits[token];
1112 if (bits_to_get) 974 if (bits_to_get)
1113 bits_to_get = get_bits(gb, bits_to_get); 975 bits_to_get = get_bits(gb, bits_to_get);
1114 coeff = coeff_tables[token][bits_to_get]; 976 coeff = coeff_tables[token][bits_to_get];
1115 977
1116 zero_run = zero_run_base[token]; 978 zero_run = zero_run_base[token];
1117 if (zero_run_get_bits[token]) 979 if (zero_run_get_bits[token])
1118 zero_run += get_bits(gb, zero_run_get_bits[token]); 980 zero_run += get_bits(gb, zero_run_get_bits[token]);
981
982 if (zero_run) {
983 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
984 } else {
985 // Save DC into the fragment structure. DC prediction is
986 // done in raster order, so the actual DC can't be in with
987 // other tokens. We still need the token in dct_tokens[]
988 // however, or else the structure collapses on itself.
989 if (!coeff_index)
990 all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
991
992 dct_tokens[j++] = TOKEN_COEFF(coeff);
993 }
994
995 if (coeff_index + zero_run > 64) {
996 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
997 " %d coeffs left\n", zero_run, 64-coeff_index);
998 zero_run = 64 - coeff_index;
999 }
1000
1001 // zero runs code multiple coefficients,
1002 // so don't try to decode coeffs for those higher levels
1003 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
1004 s->num_coded_frags[plane][i]--;
1005 coeff_i++;
1119 } 1006 }
1120 } 1007 }
1121 1008
1122 if (!eob_run) { 1009 if (blocks_ended > s->num_coded_frags[plane][coeff_index])
1123 coeff_counts[fragment_num] += zero_run; 1010 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
1124 if (coeff_counts[fragment_num] < 64){
1125 fragment->next_coeff->coeff= coeff;
1126 fragment->next_coeff->index= perm[coeff_counts[fragment_num]++]; //FIXME perm here already?
1127 fragment->next_coeff->next= s->next_coeff;
1128 s->next_coeff->next=NULL;
1129 fragment->next_coeff= s->next_coeff++;
1130 }
1131 /* previous fragment is now this fragment */
1132 previous_fragment = i;
1133 } else {
1134 coeff_counts[fragment_num] |= 128;
1135 eob_run--;
1136 /* remove this fragment from the list */
1137 if (previous_fragment != -1)
1138 fast_fragment_list[previous_fragment] = fast_fragment_list[i];
1139 else
1140 *list_head = fast_fragment_list[i];
1141 /* previous fragment remains unchanged */
1142 }
1143 1011
1144 i = fast_fragment_list[i]; 1012 // decrement the number of blocks that have higher coeffecients for each
1145 } 1013 // EOB run at this level
1014 if (blocks_ended)
1015 for (i = coeff_index+1; i < 64; i++)
1016 s->num_coded_frags[plane][i] -= blocks_ended;
1017
1018 // setup the next buffer
1019 if (plane < 2)
1020 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
1021 else if (coeff_index < 63)
1022 s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
1146 1023
1147 return eob_run; 1024 return eob_run;
1148 } 1025 }
1149 1026
1150 static void reverse_dc_prediction(Vp3DecodeContext *s, 1027 static void reverse_dc_prediction(Vp3DecodeContext *s,
1151 int first_fragment, 1028 int first_fragment,
1152 int fragment_width, 1029 int fragment_width,
1153 int fragment_height); 1030 int fragment_height);
1154 /* 1031 /*
1155 * This function unpacks all of the DCT coefficient data from the 1032 * This function unpacks all of the DCT coefficient data from the
1156 * bitstream. 1033 * bitstream.
1157 */ 1034 */
1158 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) 1035 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1159 { 1036 {
1160 int i; 1037 int i;
1161 int dc_y_table; 1038 int dc_y_table;
1162 int dc_c_table; 1039 int dc_c_table;
1163 int ac_y_table; 1040 int ac_y_table;
1164 int ac_c_table; 1041 int ac_c_table;
1165 int residual_eob_run = 0; 1042 int residual_eob_run = 0;
1166 VLC *y_tables[64]; 1043 VLC *y_tables[64];
1167 VLC *c_tables[64]; 1044 VLC *c_tables[64];
1168 1045
1046 s->dct_tokens[0][0] = s->dct_tokens_base;
1047
1169 /* fetch the DC table indexes */ 1048 /* fetch the DC table indexes */
1170 dc_y_table = get_bits(gb, 4); 1049 dc_y_table = get_bits(gb, 4);
1171 dc_c_table = get_bits(gb, 4); 1050 dc_c_table = get_bits(gb, 4);
1172 1051
1173 /* unpack the Y plane DC coefficients */ 1052 /* unpack the Y plane DC coefficients */
1174 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, 1053 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
1175 1, residual_eob_run); 1054 0, residual_eob_run);
1176 1055
1177 /* reverse prediction of the Y-plane DC coefficients */ 1056 /* reverse prediction of the Y-plane DC coefficients */
1178 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height); 1057 reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
1179 1058
1180 /* unpack the C plane DC coefficients */ 1059 /* unpack the C plane DC coefficients */
1181 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, 1060 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1182 0, residual_eob_run); 1061 1, residual_eob_run);
1062 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1063 2, residual_eob_run);
1183 1064
1184 /* reverse prediction of the C-plane DC coefficients */ 1065 /* reverse prediction of the C-plane DC coefficients */
1185 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) 1066 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1186 { 1067 {
1187 reverse_dc_prediction(s, s->fragment_start[1], 1068 reverse_dc_prediction(s, s->fragment_start[1],
1188 s->fragment_width / 2, s->fragment_height / 2); 1069 s->fragment_width / 2, s->fragment_height / 2);
1189 reverse_dc_prediction(s, s->fragment_start[2], 1070 reverse_dc_prediction(s, s->fragment_start[2],
1190 s->fragment_width / 2, s->fragment_height / 2); 1071 s->fragment_width / 2, s->fragment_height / 2);
1191 } 1072 }
1192 1073
(...skipping 14 matching lines...) Expand all
1207 y_tables[i] = &s->ac_vlc_3[ac_y_table]; 1088 y_tables[i] = &s->ac_vlc_3[ac_y_table];
1208 c_tables[i] = &s->ac_vlc_3[ac_c_table]; 1089 c_tables[i] = &s->ac_vlc_3[ac_c_table];
1209 } 1090 }
1210 for (i = 28; i <= 63; i++) { 1091 for (i = 28; i <= 63; i++) {
1211 y_tables[i] = &s->ac_vlc_4[ac_y_table]; 1092 y_tables[i] = &s->ac_vlc_4[ac_y_table];
1212 c_tables[i] = &s->ac_vlc_4[ac_c_table]; 1093 c_tables[i] = &s->ac_vlc_4[ac_c_table];
1213 } 1094 }
1214 1095
1215 /* decode all AC coefficents */ 1096 /* decode all AC coefficents */
1216 for (i = 1; i <= 63; i++) { 1097 for (i = 1; i <= 63; i++) {
1217 if (s->fragment_list_y_head != -1)
1218 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, 1098 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1099 0, residual_eob_run);
1100
1101 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1219 1, residual_eob_run); 1102 1, residual_eob_run);
1220
1221 if (s->fragment_list_c_head != -1)
1222 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, 1103 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1223 0, residual_eob_run); 1104 2, residual_eob_run);
1224 } 1105 }
1225 1106
1226 #ifdef ALT_BITSTREAM_READER 1107 #ifdef ALT_BITSTREAM_READER
1227 if (gb->buffer_exhausted && gb->index > gb->size_in_bits) 1108 if (gb->buffer_exhausted && gb->index > gb->size_in_bits)
1228 return 1; 1109 return 1;
1229 #endif 1110 #endif
1230 1111
1231 return 0; 1112 return 0;
1232 } 1113 }
1233 1114
1234 /* 1115 /*
1235 * This function reverses the DC prediction for each coded fragment in 1116 * This function reverses the DC prediction for each coded fragment in
1236 * the frame. Much of this function is adapted directly from the original 1117 * the frame. Much of this function is adapted directly from the original
1237 * VP3 source code. 1118 * VP3 source code.
1238 */ 1119 */
1239 #define COMPATIBLE_FRAME(x) \ 1120 #define COMPATIBLE_FRAME(x) \
1240 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type) 1121 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1241 #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do som ethin to simplify this 1122 #define DC_COEFF(u) s->all_fragments[u].dc
1242 1123
1243 static void reverse_dc_prediction(Vp3DecodeContext *s, 1124 static void reverse_dc_prediction(Vp3DecodeContext *s,
1244 int first_fragment, 1125 int first_fragment,
1245 int fragment_width, 1126 int fragment_width,
1246 int fragment_height) 1127 int fragment_height)
1247 { 1128 {
1248 1129
1249 #define PUL 8 1130 #define PUL 8
1250 #define PU 4 1131 #define PU 4
1251 #define PUR 2 1132 #define PUR 2
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1375 if (FFABS(predicted_dc - vu) > 128) 1256 if (FFABS(predicted_dc - vu) > 128)
1376 predicted_dc = vu; 1257 predicted_dc = vu;
1377 else if (FFABS(predicted_dc - vl) > 128) 1258 else if (FFABS(predicted_dc - vl) > 128)
1378 predicted_dc = vl; 1259 predicted_dc = vl;
1379 else if (FFABS(predicted_dc - vul) > 128) 1260 else if (FFABS(predicted_dc - vul) > 128)
1380 predicted_dc = vul; 1261 predicted_dc = vul;
1381 } 1262 }
1382 } 1263 }
1383 1264
1384 /* at long last, apply the predictor */ 1265 /* at long last, apply the predictor */
1385 if(s->coeffs[i].index){ 1266 DC_COEFF(i) += predicted_dc;
1386 *s->next_coeff= s->coeffs[i];
1387 s->coeffs[i].index=0;
1388 s->coeffs[i].coeff=0;
1389 s->coeffs[i].next= s->next_coeff++;
1390 }
1391 s->coeffs[i].coeff += predicted_dc;
1392 /* save the DC */ 1267 /* save the DC */
1393 last_dc[current_frame_type] = DC_COEFF(i); 1268 last_dc[current_frame_type] = DC_COEFF(i);
1394 if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
1395 s->coeff_counts[i]= 129;
1396 // s->all_fragments[i].next_coeff= s->next_coeff;
1397 s->coeffs[i].next= s->next_coeff;
1398 (s->next_coeff++)->next=NULL;
1399 }
1400 } 1269 }
1401 } 1270 }
1402 } 1271 }
1403 } 1272 }
1404 1273
1274 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int ye nd)
1275 {
1276 int x, y;
1277 int *bounding_values= s->bounding_values_array+127;
1278
1279 int width = s->fragment_width >> !!plane;
1280 int height = s->fragment_height >> !!plane;
1281 int fragment = s->fragment_start [plane] + ystart * width;
1282 int stride = s->current_frame.linesize[plane];
1283 uint8_t *plane_data = s->current_frame.data [plane];
1284 if (!s->flipped_image) stride = -stride;
1285 plane_data += s->data_offset[plane] + 8*ystart*stride;
1286
1287 for (y = ystart; y < yend; y++) {
1288
1289 for (x = 0; x < width; x++) {
1290 /* This code basically just deblocks on the edges of coded blocks.
1291 * However, it has to be much more complicated because of the
1292 * braindamaged deblock ordering used in VP3/Theora. Order matters
1293 * because some pixels get filtered twice. */
1294 if( s->all_fragments[fragment].coding_method != MODE_COPY )
1295 {
1296 /* do not perform left edge filter for left columns frags */
1297 if (x > 0) {
1298 s->dsp.vp3_h_loop_filter(
1299 plane_data + 8*x,
1300 stride, bounding_values);
1301 }
1302
1303 /* do not perform top edge filter for top row fragments */
1304 if (y > 0) {
1305 s->dsp.vp3_v_loop_filter(
1306 plane_data + 8*x,
1307 stride, bounding_values);
1308 }
1309
1310 /* do not perform right edge filter for right column
1311 * fragments or if right fragment neighbor is also coded
1312 * in this frame (it will be filtered in next iteration) */
1313 if ((x < width - 1) &&
1314 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
1315 s->dsp.vp3_h_loop_filter(
1316 plane_data + 8*x + 8,
1317 stride, bounding_values);
1318 }
1319
1320 /* do not perform bottom edge filter for bottom row
1321 * fragments or if bottom fragment neighbor is also coded
1322 * in this frame (it will be filtered in the next row) */
1323 if ((y < height - 1) &&
1324 (s->all_fragments[fragment + width].coding_method == MODE_CO PY)) {
1325 s->dsp.vp3_v_loop_filter(
1326 plane_data + 8*x + 8*stride,
1327 stride, bounding_values);
1328 }
1329 }
1330
1331 fragment++;
1332 }
1333 plane_data += 8*stride;
1334 }
1335 }
1336
1337 /**
1338 * Pulls DCT tokens from the 64 levels to decode and dequant the coefficients
1339 * for the next block in coding order
1340 */
1341 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
1342 int plane, int inter, DCTELEM block[64])
1343 {
1344 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
1345 uint8_t *perm = s->scantable.permutated;
1346 int i = 0;
1347
1348 do {
1349 int token = *s->dct_tokens[plane][i];
1350 switch (token & 3) {
1351 case 0: // EOB
1352 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0
1353 s->dct_tokens[plane][i]++;
1354 else
1355 *s->dct_tokens[plane][i] = token & ~3;
1356 goto end;
1357 case 1: // zero run
1358 s->dct_tokens[plane][i]++;
1359 i += (token >> 2) & 0x7f;
1360 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1361 i++;
1362 break;
1363 case 2: // coeff
1364 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1365 s->dct_tokens[plane][i++]++;
1366 break;
1367 default:
1368 av_log(s->avctx, AV_LOG_ERROR, "internal: invalid token type\n");
1369 return i;
1370 }
1371 } while (i < 64);
1372 end:
1373 // the actual DC+prediction is in the fragment structure
1374 block[0] = frag->dc * s->qmat[0][inter][plane][0];
1375 return i;
1376 }
1377
1378 /**
1379 * called when all pixels up to row y are complete
1380 */
1381 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1382 {
1383 int h, cy;
1384 int offset[4];
1385
1386 if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1387 int y_flipped = s->flipped_image ? s->height-y : y;
1388
1389 ff_thread_report_progress(&s->current_frame, y_flipped==s->height ? s->h eight : y_flipped-1, 0);
1390 }
1391
1392 if(s->avctx->draw_horiz_band==NULL)
1393 return;
1394
1395 h= y - s->last_slice_end;
1396 y -= h;
1397
1398 if (!s->flipped_image) {
1399 if (y == 0)
1400 h -= s->height - s->avctx->height; // account for non-mod16
1401 y = s->height - y - h;
1402 }
1403
1404 cy = y >> 1;
1405 offset[0] = s->current_frame.linesize[0]*y;
1406 offset[1] = s->current_frame.linesize[1]*cy;
1407 offset[2] = s->current_frame.linesize[2]*cy;
1408 offset[3] = 0;
1409
1410 emms_c();
1411 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1412 s->last_slice_end= y + h;
1413 }
1414
1405 /** 1415 /**
1406 * Wait for the reference frame of a fragment. 1416 * Wait for the reference frame of a fragment.
1407 * Units used are pixel rows with chroma after luma rows. 1417 * Units used are luma pixel rows.
1408 */ 1418 */
1409 static void await_reference_row(Vp3DecodeContext *s, int plane, Vp3Fragment *fra gment) 1419 static void await_reference_row(Vp3DecodeContext *s, int y, Vp3Fragment *fragmen t)
1410 { 1420 {
1411 AVFrame *ref_frame; 1421 AVFrame *ref_frame;
1412 int border = fragment->motion_y&1; 1422 int border = fragment->motion_y&1;
1413 int scale = 1 + !!plane; 1423 int max_row = s->height;
1414 int max_row = s->height * 2;
1415 int ref_row; 1424 int ref_row;
1416 1425
1417 if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME)) 1426 if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1418 return; 1427 return;
1419 1428
1420 if (fragment->coding_method == MODE_USING_GOLDEN || 1429 if (fragment->coding_method == MODE_USING_GOLDEN ||
1421 fragment->coding_method == MODE_GOLDEN_MV) 1430 fragment->coding_method == MODE_GOLDEN_MV)
1422 ref_frame = &s->golden_frame; 1431 ref_frame = &s->golden_frame;
1423 else 1432 else
1424 ref_frame = &s->last_frame; 1433 ref_frame = &s->last_frame;
1425 1434
1426 ref_row = fragment->first_row + (fragment->motion_y >> scale); 1435 ref_row = y + (fragment->motion_y >> 1);
1427 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border); 1436 ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1428 1437
1429 ff_thread_await_progress(ref_frame, FFMIN(ref_row, max_row), 0); 1438 ff_thread_await_progress(ref_frame, FFMIN(ref_row, max_row), 0);
1430 } 1439 }
1431 1440
1432 /* 1441 /*
1433 * Perform the final rendering for a particular slice of data. 1442 * Perform the final rendering for a particular slice of data.
1434 * The slice number ranges from 0..(macroblock_height - 1). 1443 * The slice number ranges from 0..(c_superblock_height - 1).
1435 */ 1444 */
1436 static void render_slice(Vp3DecodeContext *s, int slice) 1445 static void render_slice(Vp3DecodeContext *s, int slice)
1437 { 1446 {
1438 int x; 1447 int x, y, i, j;
1439 int16_t *dequantizer; 1448 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
1440 DECLARE_ALIGNED_16(DCTELEM, block)[64];
1441 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; 1449 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1442 int motion_halfpel_index; 1450 int motion_halfpel_index;
1443 uint8_t *motion_source; 1451 uint8_t *motion_source;
1444 int plane; 1452 int plane, first_pixel;
1445 int current_macroblock_entry = slice * s->macroblock_width * 6;
1446 1453
1447 if (slice >= s->macroblock_height) 1454 if (slice >= s->c_superblock_height)
1448 return; 1455 return;
1449 1456
1450 for (plane = 0; plane < 3; plane++) { 1457 for (plane = 0; plane < 3; plane++) {
1451 uint8_t *output_plane = s->current_frame.data [plane]; 1458 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offse t[plane];
1452 uint8_t * last_plane = s-> last_frame.data [plane]; 1459 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offse t[plane];
1453 uint8_t *golden_plane = s-> golden_frame.data [plane]; 1460 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offse t[plane];
1454 int stride = s->current_frame.linesize[plane]; 1461 int stride = s->current_frame.linesize[plane];
1455 int plane_width = s->width >> !!plane; 1462 int plane_width = s->width >> !!plane;
1456 int plane_height = s->height >> !!plane; 1463 int plane_height = s->height >> !!plane;
1457 int y = slice * FRAGMENT_PIXELS << !plane ; 1464
1458 int slice_height = y + (FRAGMENT_PIXELS << !plane); 1465 int sb_x, sb_y = slice << !plane;
1459 int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!p lane]; 1466 int slice_height = sb_y + (plane ? 1 : 2);
1467 int slice_width = plane ? s->c_superblock_width : s->y_superblock_ width;
1468
1469 int fragment_width = s->fragment_width >> !!plane;
1470 int fragment_height = s->fragment_height >> !!plane;
1471 int fragment_start = s->fragment_start[plane];
1460 1472
1461 if (!s->flipped_image) stride = -stride; 1473 if (!s->flipped_image) stride = -stride;
1474 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1475 continue;
1462 1476
1463 1477
1464 if(FFABS(stride) > 2048) 1478 if(FFABS(stride) > 2048)
1465 return; //various tables are fixed size 1479 return; //various tables are fixed size
1466 1480
1467 /* for each fragment row in the slice (both of them)... */ 1481 /* for each superblock row in the slice (both of them)... */
1468 for (; y < slice_height; y += 8) { 1482 for (; sb_y < slice_height; sb_y++) {
1469 1483
1470 /* for each fragment in a row... */ 1484 /* for each superblock in a row... */
1471 for (x = 0; x < plane_width; x += 8, i++) { 1485 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1472 1486
1473 if ((i < 0) || (i >= s->fragment_count)) { 1487 /* for each block in a superblock... */
1474 av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fr agment number (%d)\n", i); 1488 for (j = 0; j < 16; j++) {
1475 return; 1489 x = 4*sb_x + hilbert_offset[j][0];
1476 } 1490 y = 4*sb_y + hilbert_offset[j][1];
1477 1491
1478 if (s->all_fragments[i].coding_method != MODE_INTRA) 1492 i = fragment_start + y*fragment_width + x;
1479 await_reference_row(s, plane, &s->all_fragments[i]); 1493
1494 // bounds check
1495 if (x >= fragment_width || y >= fragment_height)
1496 continue;
1497
1498 first_pixel = 8*y*stride + 8*x;
1499
1500 if (s->all_fragments[i].coding_method != MODE_INTRA && !plane)
1501 await_reference_row(s, 8*y, &s->all_fragments[i]);
1480 1502
1481 /* transform if this block was coded */ 1503 /* transform if this block was coded */
1482 if ((s->all_fragments[i].coding_method != MODE_COPY) && 1504 if (s->all_fragments[i].coding_method != MODE_COPY) {
1483 !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) { 1505 int intra = s->all_fragments[i].coding_method == MODE_INTRA;
1484 1506
1485 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || 1507 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
1486 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) 1508 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
1487 motion_source= golden_plane; 1509 motion_source= golden_plane;
1488 else 1510 else
1489 motion_source= last_plane; 1511 motion_source= last_plane;
1490 1512
1491 motion_source += s->all_fragments[i].first_pixel; 1513 motion_source += first_pixel;
1492 motion_halfpel_index = 0; 1514 motion_halfpel_index = 0;
1493 1515
1494 /* sort out the motion vector if this fragment is coded 1516 /* sort out the motion vector if this fragment is coded
1495 * using a motion vector method */ 1517 * using a motion vector method */
1496 if ((s->all_fragments[i].coding_method > MODE_INTRA) && 1518 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1497 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN) ) { 1519 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN) ) {
1498 int src_x, src_y; 1520 int src_x, src_y;
1499 motion_x = s->all_fragments[i].motion_x; 1521 motion_x = s->all_fragments[i].motion_x;
1500 motion_y = s->all_fragments[i].motion_y; 1522 motion_y = s->all_fragments[i].motion_y;
1501 if(plane){ 1523 if(plane){
1502 motion_x= (motion_x>>1) | (motion_x&1); 1524 motion_x= (motion_x>>1) | (motion_x&1);
1503 motion_y= (motion_y>>1) | (motion_y&1); 1525 motion_y= (motion_y>>1) | (motion_y&1);
1504 } 1526 }
1505 1527
1506 src_x= (motion_x>>1) + x; 1528 src_x= (motion_x>>1) + 8*x;
1507 src_y= (motion_y>>1) + y; 1529 src_y= (motion_y>>1) + 8*y;
1508 if ((motion_x == 127) || (motion_y == 127)) 1530 if ((motion_x == 127) || (motion_y == 127))
1509 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid m otion vector! (%X, %X)\n", motion_x, motion_y); 1531 av_log(s->avctx, AV_LOG_ERROR, " help! got invalid m otion vector! (%X, %X)\n", motion_x, motion_y);
1510 1532
1511 motion_halfpel_index = motion_x & 0x01; 1533 motion_halfpel_index = motion_x & 0x01;
1512 motion_source += (motion_x >> 1); 1534 motion_source += (motion_x >> 1);
1513 1535
1514 motion_halfpel_index |= (motion_y & 0x01) << 1; 1536 motion_halfpel_index |= (motion_y & 0x01) << 1;
1515 motion_source += ((motion_y >> 1) * stride); 1537 motion_source += ((motion_y >> 1) * stride);
1516 1538
1517 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src _y + 9 >= plane_height){ 1539 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src _y + 9 >= plane_height){
1518 uint8_t *temp= s->edge_emu_buffer; 1540 uint8_t *temp= s->edge_emu_buffer;
1519 if(stride<0) temp -= 9*stride; 1541 if(stride<0) temp -= 9*stride;
1520 else temp += 9*stride; 1542 else temp += 9*stride;
1521 1543
1522 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height); 1544 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1523 motion_source= temp; 1545 motion_source= temp;
1524 } 1546 }
1525 } 1547 }
1526 1548
1527 1549
1528 /* first, take care of copying a block from either the 1550 /* first, take care of copying a block from either the
1529 * previous or the golden frame */ 1551 * previous or the golden frame */
1530 if (s->all_fragments[i].coding_method != MODE_INTRA) { 1552 if (s->all_fragments[i].coding_method != MODE_INTRA) {
1531 /* Note, it is possible to implement all MC cases with 1553 /* Note, it is possible to implement all MC cases with
1532 put_no_rnd_pixels_l2 which would look more like the 1554 put_no_rnd_pixels_l2 which would look more like the
1533 VP3 source but this would be slower as 1555 VP3 source but this would be slower as
1534 put_no_rnd_pixels_tab is better optimzed */ 1556 put_no_rnd_pixels_tab is better optimzed */
1535 if(motion_halfpel_index != 3){ 1557 if(motion_halfpel_index != 3){
1536 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index ]( 1558 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index ](
1537 output_plane + s->all_fragments[i].first_pixel, 1559 output_plane + first_pixel,
1538 motion_source, stride, 8); 1560 motion_source, stride, 8);
1539 }else{ 1561 }else{
1540 int d= (motion_x ^ motion_y)>>31; // d is 0 if motio n_x and _y have the same sign, else -1 1562 int d= (motion_x ^ motion_y)>>31; // d is 0 if motio n_x and _y have the same sign, else -1
1541 s->dsp.put_no_rnd_pixels_l2[1]( 1563 s->dsp.put_no_rnd_pixels_l2[1](
1542 output_plane + s->all_fragments[i].first_pixel, 1564 output_plane + first_pixel,
1543 motion_source - d, 1565 motion_source - d,
1544 motion_source + stride + 1 + d, 1566 motion_source + stride + 1 + d,
1545 stride, 8); 1567 stride, 8);
1546 } 1568 }
1547 dequantizer = s->qmat[s->all_fragments[i].qpi][1][plane] ;
1548 }else{
1549 dequantizer = s->qmat[s->all_fragments[i].qpi][0][plane] ;
1550 } 1569 }
1551 1570
1552 /* dequantize the DCT coefficients */
1553 if(s->avctx->idct_algo==FF_IDCT_VP3){
1554 Coeff *coeff= s->coeffs + i;
1555 s->dsp.clear_block(block); 1571 s->dsp.clear_block(block);
1556 while(coeff->next){ 1572 vp3_dequant(s, s->all_fragments + i, plane, !intra, bloc k);
1557 block[coeff->index]= coeff->coeff * dequantizer[coef f->index];
1558 coeff= coeff->next;
1559 }
1560 }else{
1561 Coeff *coeff= s->coeffs + i;
1562 s->dsp.clear_block(block);
1563 while(coeff->next){
1564 block[coeff->index]= (coeff->coeff * dequantizer[coe ff->index] + 2)>>2;
1565 coeff= coeff->next;
1566 }
1567 }
1568 1573
1569 /* invert DCT and place (or add) in final output */ 1574 /* invert DCT and place (or add) in final output */
1570 1575
1571 if (s->all_fragments[i].coding_method == MODE_INTRA) { 1576 if (s->all_fragments[i].coding_method == MODE_INTRA) {
1572 if(s->avctx->idct_algo!=FF_IDCT_VP3) 1577 if(s->avctx->idct_algo!=FF_IDCT_VP3)
1573 block[0] += 128<<3; 1578 block[0] += 128<<3;
1574 s->dsp.idct_put( 1579 s->dsp.idct_put(
1575 output_plane + s->all_fragments[i].first_pixel, 1580 output_plane + first_pixel,
1576 stride, 1581 stride,
1577 block); 1582 block);
1578 } else { 1583 } else {
1579 s->dsp.idct_add( 1584 s->dsp.idct_add(
1580 output_plane + s->all_fragments[i].first_pixel, 1585 output_plane + first_pixel,
1581 stride, 1586 stride,
1582 block); 1587 block);
1583 } 1588 }
1584 } else { 1589 } else {
1585 1590
1586 /* copy directly from the previous frame */ 1591 /* copy directly from the previous frame */
1587 s->dsp.put_pixels_tab[1][0]( 1592 s->dsp.put_pixels_tab[1][0](
1588 output_plane + s->all_fragments[i].first_pixel, 1593 output_plane + first_pixel,
1589 last_plane + s->all_fragments[i].first_pixel, 1594 last_plane + first_pixel,
1590 stride, 8); 1595 stride, 8);
1591 1596
1592 } 1597 }
1593 #if 0
1594 /* perform the left edge filter if:
1595 * - the fragment is not on the left column
1596 * - the fragment is coded in this frame
1597 * - the fragment is not coded in this frame but the left
1598 * fragment is coded in this frame (this is done instead
1599 * of a right edge filter when rendering the left fragment
1600 * since this fragment is not available yet) */
1601 if ((x > 0) &&
1602 ((s->all_fragments[i].coding_method != MODE_COPY) ||
1603 ((s->all_fragments[i].coding_method == MODE_COPY) &&
1604 (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
1605 horizontal_filter(
1606 output_plane + s->all_fragments[i].first_pixel + 7*strid e,
1607 -stride, s->bounding_values_array + 127);
1608 } 1598 }
1599 }
1609 1600
1610 /* perform the top edge filter if: 1601 // Filter up to the last row in the superblock row
1611 * - the fragment is not on the top row 1602 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragmen t_height-1));
1612 * - the fragment is coded in this frame
1613 * - the fragment is not coded in this frame but the above
1614 * fragment is coded in this frame (this is done instead
1615 * of a bottom edge filter when rendering the above
1616 * fragment since this fragment is not available yet) */
1617 if ((y > 0) &&
1618 ((s->all_fragments[i].coding_method != MODE_COPY) ||
1619 ((s->all_fragments[i].coding_method == MODE_COPY) &&
1620 (s->all_fragments[i - fragment_width].coding_method != MOD E_COPY)) )) {
1621 vertical_filter(
1622 output_plane + s->all_fragments[i].first_pixel - stride,
1623 -stride, s->bounding_values_array + 127);
1624 }
1625 #endif
1626 }
1627 } 1603 }
1628 } 1604 }
1629 1605
1630 /* this looks like a good place for slice dispatch... */ 1606 /* this looks like a good place for slice dispatch... */
1631 /* algorithm: 1607 /* algorithm:
1632 * if (slice == s->macroblock_height - 1) 1608 * if (slice == s->macroblock_height - 1)
1633 * dispatch (both last slice & 2nd-to-last slice); 1609 * dispatch (both last slice & 2nd-to-last slice);
1634 * else if (slice > 0) 1610 * else if (slice > 0)
1635 * dispatch (slice - 1); 1611 * dispatch (slice - 1);
1636 */ 1612 */
1637 1613
1638 emms_c(); 1614 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16));
1639 }
1640
1641 static void apply_loop_filter(Vp3DecodeContext *s)
1642 {
1643 int plane;
1644 int x, y;
1645 int *bounding_values= s->bounding_values_array+127;
1646 int rows = 0;
1647
1648 #if 0
1649 int bounding_values_array[256];
1650 int filter_limit;
1651
1652 /* find the right loop limit value */
1653 for (x = 63; x >= 0; x--) {
1654 if (vp31_ac_scale_factor[x] >= s->quality_index)
1655 break;
1656 }
1657 filter_limit = vp31_filter_limit_values[s->quality_index];
1658
1659 /* set up the bounding values */
1660 memset(bounding_values_array, 0, 256 * sizeof(int));
1661 for (x = 0; x < filter_limit; x++) {
1662 bounding_values[-x - filter_limit] = -filter_limit + x;
1663 bounding_values[-x] = -x;
1664 bounding_values[x] = x;
1665 bounding_values[x + filter_limit] = filter_limit - x;
1666 }
1667 #endif
1668
1669 for (plane = 0; plane < 3; plane++) {
1670 int width = s->fragment_width >> !!plane;
1671 int height = s->fragment_height >> !!plane;
1672 int fragment = s->fragment_start [plane];
1673 int stride = s->current_frame.linesize[plane];
1674 uint8_t *plane_data = s->current_frame.data [plane];
1675 if (!s->flipped_image) stride = -stride;
1676
1677 for (y = 0; y < height; y++) {
1678
1679 for (x = 0; x < width; x++) {
1680 /* This code basically just deblocks on the edges of coded block s.
1681 * However, it has to be much more complicated because of the
1682 * braindamaged deblock ordering used in VP3/Theora. Order matte rs
1683 * because some pixels get filtered twice. */
1684 if( s->all_fragments[fragment].coding_method != MODE_COPY )
1685 {
1686 /* do not perform left edge filter for left columns frags */
1687 if (x > 0) {
1688 s->dsp.vp3_h_loop_filter(
1689 plane_data + s->all_fragments[fragment].first_pixel,
1690 stride, bounding_values);
1691 }
1692
1693 /* do not perform top edge filter for top row fragments */
1694 if (y > 0) {
1695 s->dsp.vp3_v_loop_filter(
1696 plane_data + s->all_fragments[fragment].first_pixel,
1697 stride, bounding_values);
1698 }
1699
1700 /* do not perform right edge filter for right column
1701 * fragments or if right fragment neighbor is also coded
1702 * in this frame (it will be filtered in next iteration) */
1703 if ((x < width - 1) &&
1704 (s->all_fragments[fragment + 1].coding_method == MODE_CO PY)) {
1705 s->dsp.vp3_h_loop_filter(
1706 plane_data + s->all_fragments[fragment + 1].first_pi xel,
1707 stride, bounding_values);
1708 }
1709
1710 /* do not perform bottom edge filter for bottom row
1711 * fragments or if bottom fragment neighbor is also coded
1712 * in this frame (it will be filtered in the next row) */
1713 if ((y < height - 1) &&
1714 (s->all_fragments[fragment + width].coding_method == MOD E_COPY)) {
1715 s->dsp.vp3_v_loop_filter(
1716 plane_data + s->all_fragments[fragment + width].firs t_pixel,
1717 stride, bounding_values);
1718 }
1719 }
1720
1721 fragment++;
1722 }
1723
1724 ff_thread_report_progress(&s->current_frame, rows, 0);
1725 rows += FRAGMENT_PIXELS;
1726 }
1727
1728 //needs to be called twice to catch the last row in a plane
1729 ff_thread_report_progress(&s->current_frame, rows, 0);
1730 }
1731 }
1732
1733 /*
1734 * This function computes the first pixel addresses for each fragment.
1735 * This function needs to be invoked after the first frame is allocated
1736 * so that it has access to the plane strides.
1737 */
1738 static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
1739 {
1740 #define Y_INITIAL(chroma_shift) s->flipped_image ? 1 : s->fragment_height >> c hroma_shift
1741 #define Y_FINISHED(chroma_shift) s->flipped_image ? y <= s->fragment_height >> c hroma_shift : y > 0
1742
1743 int i, x, y;
1744 const int y_inc = s->flipped_image ? 1 : -1;
1745 int rows = 0;
1746
1747 /* figure out the first pixel addresses for each of the fragments */
1748 /* Y plane */
1749 i = 0;
1750 for (y = Y_INITIAL(0); Y_FINISHED(0); y += y_inc) {
1751 for (x = 0; x < s->fragment_width; x++) {
1752 s->all_fragments[i].first_pixel =
1753 s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
1754 s->golden_frame.linesize[0] +
1755 x * FRAGMENT_PIXELS;
1756 s->all_fragments[i++].first_row = rows * FRAGMENT_PIXELS;
1757 }
1758 rows++;
1759 }
1760
1761 /* U plane */
1762 i = s->fragment_start[1];
1763 for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
1764 for (x = 0; x < s->fragment_width / 2; x++) {
1765 s->all_fragments[i].first_pixel =
1766 s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
1767 s->golden_frame.linesize[1] +
1768 x * FRAGMENT_PIXELS;
1769 s->all_fragments[i++].first_row = rows * FRAGMENT_PIXELS;
1770 }
1771 rows++;
1772 }
1773
1774 /* V plane */
1775 i = s->fragment_start[2];
1776 for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
1777 for (x = 0; x < s->fragment_width / 2; x++) {
1778 s->all_fragments[i].first_pixel =
1779 s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
1780 s->golden_frame.linesize[2] +
1781 x * FRAGMENT_PIXELS;
1782 s->all_fragments[i++].first_row = rows * FRAGMENT_PIXELS;
1783 }
1784 rows++;
1785 }
1786 } 1615 }
1787 1616
1788 /// Allocate tables for frame data in Vp3DecodeContext 1617 /// Allocate tables for frame data in Vp3DecodeContext
1789 static av_cold int allocate_tables(AVCodecContext *avctx) 1618 static av_cold int allocate_tables(AVCodecContext *avctx)
1790 { 1619 {
1791 Vp3DecodeContext *s = avctx->priv_data; 1620 Vp3DecodeContext *s = avctx->priv_data;
1792 1621
1793 s->superblock_coding = av_malloc(s->superblock_count); 1622 s->superblock_coding = av_malloc(s->superblock_count);
1794 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); 1623 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
1795 s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts)); 1624 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
1796 s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65); 1625 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_ base));
1797 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
1798 s->fast_fragment_list = av_malloc(s->fragment_count * sizeof(int));
1799 1626
1800 /* work out the block mapping tables */ 1627 /* work out the block mapping tables */
1801 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int)); 1628 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
1802 s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int)) ;
1803 s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
1804 s->macroblock_coding = av_malloc(s->macroblock_count + 1); 1629 s->macroblock_coding = av_malloc(s->macroblock_count + 1);
1805 1630
1806 if (!s->superblock_coding || !s->all_fragments || !s->coeff_counts || 1631 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
1807 !s->coeffs || !s->coded_fragment_list || !s->fast_fragment_list || 1632 !s->coded_fragment_list[0] || !s->superblock_fragments || !s->macroblock _coding) {
1808 » » !s->superblock_fragments || !s->superblock_macroblocks ||
1809 !s->macroblock_fragments || !s->macroblock_coding) {
1810 vp3_decode_end(avctx); 1633 vp3_decode_end(avctx);
1811 return -1; 1634 return -1;
1812 } 1635 }
1636
1813 init_block_mapping(s); 1637 init_block_mapping(s);
1814 1638
1815 return 0; 1639 return 0;
1816 } 1640 }
1817 1641
1818 /* 1642 /*
1819 * This is the ffmpeg/libavcodec API init function. 1643 * This is the ffmpeg/libavcodec API init function.
1820 */ 1644 */
1821 static av_cold int vp3_decode_init(AVCodecContext *avctx) 1645 static av_cold int vp3_decode_init(AVCodecContext *avctx)
1822 { 1646 {
1823 Vp3DecodeContext *s = avctx->priv_data; 1647 Vp3DecodeContext *s = avctx->priv_data;
1824 int i, inter, plane; 1648 int i, inter, plane;
1825 int c_width; 1649 int c_width;
1826 int c_height; 1650 int c_height;
1827 int y_superblock_count;
1828 int c_superblock_count;
1829 1651
1830 if (avctx->codec_tag == MKTAG('V','P','3','0')) 1652 if (avctx->codec_tag == MKTAG('V','P','3','0'))
1831 s->version = 0; 1653 s->version = 0;
1832 else 1654 else
1833 s->version = 1; 1655 s->version = 1;
1834 1656
1835 s->avctx = avctx; 1657 s->avctx = avctx;
1836 s->width = FFALIGN(avctx->width, 16); 1658 s->width = FFALIGN(avctx->width, 16);
1837 s->height = FFALIGN(avctx->height, 16); 1659 s->height = FFALIGN(avctx->height, 16);
1838 avctx->pix_fmt = PIX_FMT_YUV420P; 1660 avctx->pix_fmt = PIX_FMT_YUV420P;
1839 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 1661 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1840 if(avctx->idct_algo==FF_IDCT_AUTO) 1662 if(avctx->idct_algo==FF_IDCT_AUTO)
1841 avctx->idct_algo=FF_IDCT_VP3; 1663 avctx->idct_algo=FF_IDCT_VP3;
1842 dsputil_init(&s->dsp, avctx); 1664 dsputil_init(&s->dsp, avctx);
1843 1665
1844 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); 1666 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
1845 1667
1846 /* initialize to an impossible value which will force a recalculation 1668 /* initialize to an impossible value which will force a recalculation
1847 * in the first frame decode */ 1669 * in the first frame decode */
1848 for (i = 0; i < 3; i++) 1670 for (i = 0; i < 3; i++)
1849 s->qps[i] = -1; 1671 s->qps[i] = -1;
1850 1672
1851 s->y_superblock_width = (s->width + 31) / 32; 1673 s->y_superblock_width = (s->width + 31) / 32;
1852 s->y_superblock_height = (s->height + 31) / 32; 1674 s->y_superblock_height = (s->height + 31) / 32;
1853 y_superblock_count = s->y_superblock_width * s->y_superblock_height; 1675 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1854 1676
1855 /* work out the dimensions for the C planes */ 1677 /* work out the dimensions for the C planes */
1856 c_width = s->width / 2; 1678 c_width = s->width / 2;
1857 c_height = s->height / 2; 1679 c_height = s->height / 2;
1858 s->c_superblock_width = (c_width + 31) / 32; 1680 s->c_superblock_width = (c_width + 31) / 32;
1859 s->c_superblock_height = (c_height + 31) / 32; 1681 s->c_superblock_height = (c_height + 31) / 32;
1860 c_superblock_count = s->c_superblock_width * s->c_superblock_height; 1682 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1861 1683
1862 s->superblock_count = y_superblock_count + (c_superblock_count * 2); 1684 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1863 s->u_superblock_start = y_superblock_count; 1685 s->u_superblock_start = s->y_superblock_count;
1864 s->v_superblock_start = s->u_superblock_start + c_superblock_count; 1686 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
1865 1687
1866 s->macroblock_width = (s->width + 15) / 16; 1688 s->macroblock_width = (s->width + 15) / 16;
1867 s->macroblock_height = (s->height + 15) / 16; 1689 s->macroblock_height = (s->height + 15) / 16;
1868 s->macroblock_count = s->macroblock_width * s->macroblock_height; 1690 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1869 1691
1870 s->fragment_width = s->width / FRAGMENT_PIXELS; 1692 s->fragment_width = s->width / FRAGMENT_PIXELS;
1871 s->fragment_height = s->height / FRAGMENT_PIXELS; 1693 s->fragment_height = s->height / FRAGMENT_PIXELS;
1872 1694
1873 /* fragment count covers all 8x8 blocks for all 3 planes */ 1695 /* fragment count covers all 8x8 blocks for all 3 planes */
1874 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2; 1696 s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
1875 s->fragment_start[1] = s->fragment_width * s->fragment_height; 1697 s->fragment_start[1] = s->fragment_width * s->fragment_height;
1876 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4; 1698 s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
1877 1699
1878 s->pixel_addresses_initialized = 0;
1879
1880 if (!s->theora_tables) 1700 if (!s->theora_tables)
1881 { 1701 {
1882 for (i = 0; i < 64; i++) { 1702 for (i = 0; i < 64; i++) {
1883 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; 1703 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
1884 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; 1704 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
1885 s->base_matrix[0][i] = vp31_intra_y_dequant[i]; 1705 s->base_matrix[0][i] = vp31_intra_y_dequant[i];
1886 s->base_matrix[1][i] = vp31_intra_c_dequant[i]; 1706 s->base_matrix[1][i] = vp31_intra_c_dequant[i];
1887 s->base_matrix[2][i] = vp31_inter_dequant[i]; 1707 s->base_matrix[2][i] = vp31_inter_dequant[i];
1888 s->filter_limit_values[i] = vp31_filter_limit_values[i]; 1708 s->filter_limit_values[i] = vp31_filter_limit_values[i];
1889 } 1709 }
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1989 return -1; 1809 return -1;
1990 } 1810 }
1991 1811
1992 /// Release and shuffle frames after decode finishes 1812 /// Release and shuffle frames after decode finishes
1993 static void update_frames(AVCodecContext *avctx) 1813 static void update_frames(AVCodecContext *avctx)
1994 { 1814 {
1995 Vp3DecodeContext *s = avctx->priv_data; 1815 Vp3DecodeContext *s = avctx->priv_data;
1996 1816
1997 /* release the last frame, if it is allocated and if it is not the 1817 /* release the last frame, if it is allocated and if it is not the
1998 * golden frame */ 1818 * golden frame */
1999 if ((s->last_frame.data[0]) && 1819 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
2000 (s->last_frame.data[0] != s->golden_frame.data[0]))
2001 ff_thread_release_buffer(avctx, &s->last_frame); 1820 ff_thread_release_buffer(avctx, &s->last_frame);
2002 1821
2003 /* shuffle frames (last = current) */ 1822 /* shuffle frames (last = current) */
2004 s->last_frame= s->current_frame; 1823 s->last_frame= s->current_frame;
1824
1825 if (s->keyframe) {
1826 if (s->golden_frame.data[0])
1827 ff_thread_release_buffer(avctx, &s->golden_frame);
1828 s->golden_frame = s->current_frame;
1829 s->last_frame.type = FF_BUFFER_TYPE_COPY;
1830 }
1831
2005 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this r eleased frame */ 1832 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this r eleased frame */
2006 } 1833 }
2007 1834
2008 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, & from->start_field, (char*)&to->end_field - (char*)&to->start_field) 1835 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, & from->start_field, (char*)&to->end_field - (char*)&to->start_field)
2009 static int vp3_update_thread_context(AVCodecContext *dst, AVCodecContext *src) 1836 static int vp3_update_thread_context(AVCodecContext *dst, AVCodecContext *src)
2010 { 1837 {
2011 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data; 1838 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
2012 int qps_changed = 0, i, err; 1839 int qps_changed = 0, i, err;
2013 1840
2014 if (!s1->pixel_addresses_initialized 1841 if (!s1->current_frame.data[0]
2015 ||s->width != s1->width 1842 ||s->width != s1->width
2016 ||s->height!= s1->height) 1843 ||s->height!= s1->height)
2017 return -1; 1844 return -1;
2018 1845
2019 if (s != s1) { 1846 if (s != s1) {
2020 // init tables the first time 1847 // init tables if the first frame hasn't been decoded
2021 if (!s->pixel_addresses_initialized) { 1848 if (!s->current_frame.data[0]) {
2022 s->avctx = dst; 1849 s->avctx = dst;
2023 err = allocate_tables(dst); 1850 err = allocate_tables(dst);
2024 if (err) 1851 if (err)
2025 return err; 1852 return err;
2026 memcpy(s->all_fragments, s1->all_fragments, s->fragment_count * size of(Vp3Fragment)); 1853 memcpy(s->all_fragments, s1->all_fragments, s->fragment_count * size of(Vp3Fragment));
2027 s->pixel_addresses_initialized = s1->pixel_addresses_initialized;
2028 } 1854 }
2029 1855
2030 // copy previous frame data 1856 // copy previous frame data
2031 copy_fields(s, s1, golden_frame, keyframe); 1857 copy_fields(s, s1, golden_frame, dsp);
2032 1858
2033 // copy qscale data if necessary 1859 // copy qscale data if necessary
2034 for (i = 0; i < 3; i++) { 1860 for (i = 0; i < 3; i++) {
2035 if (s->qps[i] != s1->qps[1]) { 1861 if (s->qps[i] != s1->qps[1]) {
2036 qps_changed = 1; 1862 qps_changed = 1;
2037 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i])); 1863 memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
2038 } 1864 }
2039 } 1865 }
2040 1866
2041 if (s->qps[0] != s1->qps[0]) { 1867 if (s->qps[0] != s1->qps[0]) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
2096 1922
2097 for (i = 0; i < s->nqps; i++) 1923 for (i = 0; i < s->nqps; i++)
2098 // reinit all dequantizers if the first one changed, because 1924 // reinit all dequantizers if the first one changed, because
2099 // the DC of the first quantizer must be used for all matrices 1925 // the DC of the first quantizer must be used for all matrices
2100 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) 1926 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2101 init_dequantizer(s, i); 1927 init_dequantizer(s, i);
2102 1928
2103 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) 1929 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
2104 return buf_size; 1930 return buf_size;
2105 1931
1932 s->current_frame.reference = 3;
1933 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1934 if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
1935 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1936 goto error;
1937 }
1938
2106 if (s->keyframe) { 1939 if (s->keyframe) {
2107 if (!s->theora) 1940 if (!s->theora)
2108 { 1941 {
2109 skip_bits(&gb, 4); /* width code */ 1942 skip_bits(&gb, 4); /* width code */
2110 skip_bits(&gb, 4); /* height code */ 1943 skip_bits(&gb, 4); /* height code */
2111 if (s->version) 1944 if (s->version)
2112 { 1945 {
2113 s->version = get_bits(&gb, 5); 1946 s->version = get_bits(&gb, 5);
2114 if (avctx->frame_number == 0) 1947 if (avctx->frame_number == 0)
2115 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->versio n); 1948 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->versio n);
2116 } 1949 }
2117 } 1950 }
2118 if (s->version || s->theora) 1951 if (s->version || s->theora)
2119 { 1952 {
2120 if (get_bits1(&gb)) 1953 if (get_bits1(&gb))
2121 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyfram e coding type?!\n"); 1954 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyfram e coding type?!\n");
2122 skip_bits(&gb, 2); /* reserved? */ 1955 skip_bits(&gb, 2); /* reserved? */
2123 } 1956 }
1957 } else {
1958 if (!s->golden_frame.data[0]) {
1959 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n" );
2124 1960
2125 if (s->last_frame.data[0] == s->golden_frame.data[0]) { 1961 s->golden_frame.reference = 3;
2126 if (s->golden_frame.data[0]) 1962 s->golden_frame.pict_type = FF_I_TYPE;
2127 ff_thread_release_buffer(avctx, &s->golden_frame); 1963 if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
2128 s->last_frame= s->golden_frame; /* ensure that we catch any access t o this released frame */ 1964 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2129 } else { 1965 goto error;
2130 if (s->golden_frame.data[0]) 1966 }
2131 ff_thread_release_buffer(avctx, &s->golden_frame); 1967 s->last_frame = s->golden_frame;
2132 if (s->last_frame.data[0]) 1968 s->last_frame.type = FF_BUFFER_TYPE_COPY;
2133 ff_thread_release_buffer(avctx, &s->last_frame);
2134 }
2135
2136 s->golden_frame.reference = 3;
2137 if(ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
2138 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2139 return -1;
2140 }
2141
2142 /* golden frame is also the current frame */
2143 s->current_frame= s->golden_frame;
2144
2145 /* time to figure out pixel addresses? */
2146 if (!s->pixel_addresses_initialized)
2147 {
2148 vp3_calculate_pixel_addresses(s);
2149 s->pixel_addresses_initialized = 1;
2150 }
2151 } else {
2152 /* allocate a new current frame */
2153 s->current_frame.reference = 3;
2154 if (!s->pixel_addresses_initialized || !s->golden_frame.data[0]) {
2155 av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
2156 return -1;
2157 }
2158 if(ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
2159 av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
2160 return -1;
2161 } 1969 }
2162 } 1970 }
2163 1971
2164 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame 1972 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
2165 s->current_frame.qstride= 0; 1973 s->current_frame.qstride= 0;
2166 1974
1975 init_frame(s, &gb);
2167 ff_thread_finish_setup(avctx); 1976 ff_thread_finish_setup(avctx);
2168 1977
2169 init_frame(s, &gb);
2170
2171 if (unpack_superblocks(s, &gb)){ 1978 if (unpack_superblocks(s, &gb)){
2172 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); 1979 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
2173 goto error; 1980 goto error;
2174 } 1981 }
2175 if (unpack_modes(s, &gb)){ 1982 if (unpack_modes(s, &gb)){
2176 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); 1983 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
2177 goto error; 1984 goto error;
2178 } 1985 }
2179 if (unpack_vectors(s, &gb)){ 1986 if (unpack_vectors(s, &gb)){
2180 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); 1987 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
2181 goto error; 1988 goto error;
2182 } 1989 }
2183 if (unpack_block_qpis(s, &gb)){ 1990 if (unpack_block_qpis(s, &gb)){
2184 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); 1991 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
2185 goto error; 1992 goto error;
2186 } 1993 }
2187 if (unpack_dct_coeffs(s, &gb)){ 1994 if (unpack_dct_coeffs(s, &gb)){
2188 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); 1995 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
2189 goto error; 1996 goto error;
2190 } 1997 }
2191 1998
2192 for (i = 0; i < s->macroblock_height; i++) 1999 for (i = 0; i < 3; i++) {
2000 if (s->flipped_image)
2001 s->data_offset[i] = 0;
2002 else
2003 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize [i];
2004 }
2005
2006 s->last_slice_end = 0;
2007 for (i = 0; i < s->c_superblock_height; i++)
2193 render_slice(s, i); 2008 render_slice(s, i);
2194 2009
2195 apply_loop_filter(s); 2010 // filter the last row
2011 for (i = 0; i < 3; i++) {
2012 int row = (s->height >> (3+!!i)) - 1;
2013 apply_loop_filter(s, i, row, row+1);
2014 }
2015 vp3_draw_horiz_band(s, s->height);
2196 2016
2197 *data_size=sizeof(AVFrame); 2017 *data_size=sizeof(AVFrame);
2198 *(AVFrame*)data= s->current_frame; 2018 *(AVFrame*)data= s->current_frame;
2199 2019
2200 if (!HAVE_PTHREADS || !(avctx->active_thread_type&FF_THREAD_FRAME)) 2020 if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
2201 update_frames(avctx); 2021 update_frames(avctx);
2202 2022
2203 return buf_size; 2023 return buf_size;
2024
2204 error: 2025 error:
2205 ff_thread_report_progress(&s->current_frame, INT_MAX, 0); 2026 ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
2027
2028 if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
2029 avctx->release_buffer(avctx, &s->current_frame);
2030
2206 return -1; 2031 return -1;
2207 } 2032 }
2208 2033
2209 /* 2034 /*
2210 * This is the ffmpeg/libavcodec API module cleanup function. 2035 * This is the ffmpeg/libavcodec API module cleanup function.
2211 */ 2036 */
2212 static av_cold int vp3_decode_end(AVCodecContext *avctx) 2037 static av_cold int vp3_decode_end(AVCodecContext *avctx)
2213 { 2038 {
2214 Vp3DecodeContext *s = avctx->priv_data; 2039 Vp3DecodeContext *s = avctx->priv_data;
2215 int i; 2040 int i;
2216 2041
2217 av_free(s->superblock_coding); 2042 av_free(s->superblock_coding);
2218 av_free(s->all_fragments); 2043 av_free(s->all_fragments);
2219 av_free(s->coeff_counts); 2044 av_free(s->coded_fragment_list[0]);
2220 av_free(s->coeffs); 2045 av_free(s->dct_tokens_base);
2221 av_free(s->coded_fragment_list);
2222 av_free(s->fast_fragment_list);
2223 av_free(s->superblock_fragments); 2046 av_free(s->superblock_fragments);
2224 av_free(s->superblock_macroblocks);
2225 av_free(s->macroblock_fragments);
2226 av_free(s->macroblock_coding); 2047 av_free(s->macroblock_coding);
2227 2048
2228 if (avctx->is_copy) return 0; 2049 if (avctx->is_copy) return 0;
2229 2050
2230 for (i = 0; i < 16; i++) { 2051 for (i = 0; i < 16; i++) {
2231 free_vlc(&s->dc_vlc[i]); 2052 free_vlc(&s->dc_vlc[i]);
2232 free_vlc(&s->ac_vlc_1[i]); 2053 free_vlc(&s->ac_vlc_1[i]);
2233 free_vlc(&s->ac_vlc_2[i]); 2054 free_vlc(&s->ac_vlc_2[i]);
2234 free_vlc(&s->ac_vlc_3[i]); 2055 free_vlc(&s->ac_vlc_3[i]);
2235 free_vlc(&s->ac_vlc_4[i]); 2056 free_vlc(&s->ac_vlc_4[i]);
2236 } 2057 }
2237 2058
2238 free_vlc(&s->superblock_run_length_vlc); 2059 free_vlc(&s->superblock_run_length_vlc);
2239 free_vlc(&s->fragment_run_length_vlc); 2060 free_vlc(&s->fragment_run_length_vlc);
2240 free_vlc(&s->mode_code_vlc); 2061 free_vlc(&s->mode_code_vlc);
2241 free_vlc(&s->motion_vector_vlc); 2062 free_vlc(&s->motion_vector_vlc);
2242 2063
2243 /* release all frames */ 2064 /* release all frames */
2244 if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data [0]) 2065 if (s->golden_frame.data[0])
2245 ff_thread_release_buffer(avctx, &s->golden_frame); 2066 ff_thread_release_buffer(avctx, &s->golden_frame);
2246 if (s->last_frame.data[0]) 2067 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
2247 ff_thread_release_buffer(avctx, &s->last_frame); 2068 ff_thread_release_buffer(avctx, &s->last_frame);
2248 /* no need to release the current_frame since it will always be pointing 2069 /* no need to release the current_frame since it will always be pointing
2249 * to the same frame as either the golden or last frame */ 2070 * to the same frame as either the golden or last frame */
2250 2071
2251 return 0; 2072 return 0;
2252 } 2073 }
2253 2074
2254 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) 2075 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2255 { 2076 {
2256 Vp3DecodeContext *s = avctx->priv_data; 2077 Vp3DecodeContext *s = avctx->priv_data;
(...skipping 25 matching lines...) Expand all
2282 s->hbits >>= 1; 2103 s->hbits >>= 1;
2283 s->huff_code_size--; 2104 s->huff_code_size--;
2284 } 2105 }
2285 return 0; 2106 return 0;
2286 } 2107 }
2287 2108
2288 #if CONFIG_THEORA_DECODER 2109 #if CONFIG_THEORA_DECODER
2289 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) 2110 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
2290 { 2111 {
2291 Vp3DecodeContext *s = avctx->priv_data; 2112 Vp3DecodeContext *s = avctx->priv_data;
2292 int visible_width, visible_height; 2113 int visible_width, visible_height, colorspace;
2293 2114
2294 s->theora = get_bits_long(gb, 24); 2115 s->theora = get_bits_long(gb, 24);
2295 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora); 2116 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
2296 2117
2297 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ 2118 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
2298 /* but previous versions have the image flipped relative to vp3 */ 2119 /* but previous versions have the image flipped relative to vp3 */
2299 if (s->theora < 0x030200) 2120 if (s->theora < 0x030200)
2300 { 2121 {
2301 s->flipped_image = 1; 2122 s->flipped_image = 1;
2302 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped ima ge\n"); 2123 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped ima ge\n");
2303 } 2124 }
2304 2125
2305 visible_width = s->width = get_bits(gb, 16) << 4; 2126 visible_width = s->width = get_bits(gb, 16) << 4;
2306 visible_height = s->height = get_bits(gb, 16) << 4; 2127 visible_height = s->height = get_bits(gb, 16) << 4;
2307 2128
2308 if(avcodec_check_dimensions(avctx, s->width, s->height)){ 2129 if(avcodec_check_dimensions(avctx, s->width, s->height)){
2309 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s- >height); 2130 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s- >height);
2310 s->width= s->height= 0; 2131 s->width= s->height= 0;
2311 return -1; 2132 return -1;
2312 } 2133 }
2313 2134
2314 if (s->theora >= 0x030400)
2315 {
2316 skip_bits(gb, 32); /* total number of superblocks in a frame */
2317 // fixme, the next field is 36bits long
2318 skip_bits(gb, 32); /* total number of blocks in a frame */
2319 skip_bits(gb, 4); /* total number of blocks in a frame */
2320 skip_bits(gb, 32); /* total number of macroblocks in a frame */
2321 }
2322
2323 if (s->theora >= 0x030200) { 2135 if (s->theora >= 0x030200) {
2324 visible_width = get_bits_long(gb, 24); 2136 visible_width = get_bits_long(gb, 24);
2325 visible_height = get_bits_long(gb, 24); 2137 visible_height = get_bits_long(gb, 24);
2326 2138
2327 skip_bits(gb, 8); /* offset x */ 2139 skip_bits(gb, 8); /* offset x */
2328 skip_bits(gb, 8); /* offset y */ 2140 skip_bits(gb, 8); /* offset y */
2329 } 2141 }
2330 2142
2331 skip_bits(gb, 32); /* fps numerator */ 2143 skip_bits(gb, 32); /* fps numerator */
2332 skip_bits(gb, 32); /* fps denumerator */ 2144 skip_bits(gb, 32); /* fps denumerator */
2333 skip_bits(gb, 24); /* aspect numerator */ 2145 skip_bits(gb, 24); /* aspect numerator */
2334 skip_bits(gb, 24); /* aspect denumerator */ 2146 skip_bits(gb, 24); /* aspect denumerator */
2335 2147
2336 if (s->theora < 0x030200) 2148 if (s->theora < 0x030200)
2337 skip_bits(gb, 5); /* keyframe frequency force */ 2149 skip_bits(gb, 5); /* keyframe frequency force */
2338 skip_bits(gb, 8); /* colorspace */ 2150 colorspace = get_bits(gb, 8);
2339 if (s->theora >= 0x030400)
2340 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
2341 skip_bits(gb, 24); /* bitrate */ 2151 skip_bits(gb, 24); /* bitrate */
2342 2152
2343 skip_bits(gb, 6); /* quality hint */ 2153 skip_bits(gb, 6); /* quality hint */
2344 2154
2345 if (s->theora >= 0x030200) 2155 if (s->theora >= 0x030200)
2346 { 2156 {
2347 skip_bits(gb, 5); /* keyframe frequency force */ 2157 skip_bits(gb, 5); /* keyframe frequency force */
2348 2158 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
2349 if (s->theora < 0x030400) 2159 skip_bits(gb, 3); /* reserved */
2350 skip_bits(gb, 5); /* spare bits */
2351 } 2160 }
2352 2161
2353 // align_get_bits(gb); 2162 // align_get_bits(gb);
2354 2163
2355 if ( visible_width <= s->width && visible_width > s->width-16 2164 if ( visible_width <= s->width && visible_width > s->width-16
2356 && visible_height <= s->height && visible_height > s->height-16) 2165 && visible_height <= s->height && visible_height > s->height-16)
2357 avcodec_set_dimensions(avctx, visible_width, visible_height); 2166 avcodec_set_dimensions(avctx, visible_width, visible_height);
2358 else 2167 else
2359 avcodec_set_dimensions(avctx, s->width, s->height); 2168 avcodec_set_dimensions(avctx, s->width, s->height);
2360 2169
2170 if (colorspace == 1) {
2171 avctx->color_primaries = AVCOL_PRI_BT470M;
2172 } else if (colorspace == 2) {
2173 avctx->color_primaries = AVCOL_PRI_BT470BG;
2174 }
2175 if (colorspace == 1 || colorspace == 2) {
2176 avctx->colorspace = AVCOL_SPC_BT470BG;
2177 avctx->color_trc = AVCOL_TRC_BT709;
2178 }
2179
2361 return 0; 2180 return 0;
2362 } 2181 }
2363 2182
2364 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) 2183 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
2365 { 2184 {
2366 Vp3DecodeContext *s = avctx->priv_data; 2185 Vp3DecodeContext *s = avctx->priv_data;
2367 int i, n, matrices, inter, plane; 2186 int i, n, matrices, inter, plane;
2368 2187
2369 if (s->theora >= 0x030200) { 2188 if (s->theora >= 0x030200) {
2370 n = get_bits(gb, 3); 2189 n = get_bits(gb, 3);
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
2538 2357
2539 AVCodec theora_decoder = { 2358 AVCodec theora_decoder = {
2540 "theora", 2359 "theora",
2541 CODEC_TYPE_VIDEO, 2360 CODEC_TYPE_VIDEO,
2542 CODEC_ID_THEORA, 2361 CODEC_ID_THEORA,
2543 sizeof(Vp3DecodeContext), 2362 sizeof(Vp3DecodeContext),
2544 theora_decode_init, 2363 theora_decode_init,
2545 NULL, 2364 NULL,
2546 vp3_decode_end, 2365 vp3_decode_end,
2547 vp3_decode_frame, 2366 vp3_decode_frame,
2548 CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, 2367 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2549 NULL, 2368 NULL,
2550 .long_name = NULL_IF_CONFIG_SMALL("Theora"), 2369 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2551 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context) 2370 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2552 }; 2371 };
2553 #endif 2372 #endif
2554 2373
2555 AVCodec vp3_decoder = { 2374 AVCodec vp3_decoder = {
2556 "vp3", 2375 "vp3",
2557 CODEC_TYPE_VIDEO, 2376 CODEC_TYPE_VIDEO,
2558 CODEC_ID_VP3, 2377 CODEC_ID_VP3,
2559 sizeof(Vp3DecodeContext), 2378 sizeof(Vp3DecodeContext),
2560 vp3_decode_init, 2379 vp3_decode_init,
2561 NULL, 2380 NULL,
2562 vp3_decode_end, 2381 vp3_decode_end,
2563 vp3_decode_frame, 2382 vp3_decode_frame,
2564 CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, 2383 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2565 NULL, 2384 NULL,
2566 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"), 2385 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2567 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context), 2386 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2568 }; 2387 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698