Mercurial > libavcodec.hg
annotate indeo5.c @ 11245:1e9ff636c3db libavcodec
Make Bink decoder to stop decoding planes after all bits are used.
This prevents crashes during decoding grayscale Bink files like
samples from Impossible Creatures game demo.
author | kostya |
---|---|
date | Mon, 22 Feb 2010 12:35:12 +0000 |
parents | 384b6a615a92 |
children | b75449aaea3e |
rev | line source |
---|---|
11107 | 1 /* |
2 * Indeo Video Interactive v5 compatible decoder | |
3 * Copyright (c) 2009 Maxim Poliakovski | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @file libavcodec/indeo5.c | |
24 * Indeo Video Interactive version 5 decoder | |
25 * | |
26 * Indeo5 data is usually transported within .avi or .mov files. | |
27 * Known FOURCCs: 'IV50' | |
28 */ | |
29 | |
30 #define ALT_BITSTREAM_READER_LE | |
31 #include "avcodec.h" | |
32 #include "get_bits.h" | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
33 #include "dsputil.h" |
11107 | 34 #include "ivi_dsp.h" |
35 #include "ivi_common.h" | |
36 #include "indeo5data.h" | |
37 | |
38 /** | |
39 * Indeo5 frame types. | |
40 */ | |
41 enum { | |
42 FRAMETYPE_INTRA = 0, | |
43 FRAMETYPE_INTER = 1, ///< non-droppable P-frame | |
44 FRAMETYPE_INTER_SCAL = 2, ///< droppable P-frame used in the scalability mode | |
45 FRAMETYPE_INTER_NOREF = 3, ///< droppable P-frame | |
46 FRAMETYPE_NULL = 4 ///< empty frame with no data | |
47 }; | |
48 | |
49 #define IVI5_PIC_SIZE_ESC 15 | |
50 | |
51 #define IVI5_IS_PROTECTED 0x20 | |
52 | |
53 typedef struct { | |
54 GetBitContext gb; | |
55 AVFrame frame; | |
56 RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables | |
57 IVIPlaneDesc planes[3]; ///< color planes | |
58 const uint8_t *frame_data; ///< input frame data pointer | |
59 int buf_switch; ///< used to switch between three buffers | |
60 int dst_buf; | |
61 int ref_buf; | |
62 uint32_t frame_size; ///< frame size in bytes | |
63 int frame_type; | |
64 int prev_frame_type; ///< frame type of the previous frame | |
65 int frame_num; | |
66 uint32_t pic_hdr_size; ///< picture header size in bytes | |
67 uint8_t frame_flags; | |
68 uint16_t checksum; ///< frame checksum | |
69 | |
70 int16_t mb_huff_sel; ///< MB huffman table selector | |
71 IVIHuffDesc mb_huff_desc; ///< MB table descriptor associated with the selector above | |
72 VLC *mb_vlc; ///< ptr to the vlc table for decoding macroblock data | |
73 VLC mb_vlc_cust; ///< custom macroblock vlc table | |
74 | |
75 uint16_t gop_hdr_size; | |
76 uint8_t gop_flags; | |
77 int is_scalable; | |
78 uint32_t lock_word; | |
79 IVIPicConfig pic_conf; | |
80 } IVI5DecContext; | |
81 | |
82 //! static vlc tables (initialized at startup) | |
83 static VLC mb_vlc_tabs [8]; | |
84 static VLC blk_vlc_tabs[8]; | |
85 | |
86 | |
87 /** | |
88 * Decodes Indeo5 GOP (Group of pictures) header. | |
89 * This header is present in key frames only. | |
90 * It defines parameters for all frames in a GOP. | |
91 * | |
92 * @param ctx [in,out] ptr to the decoder context | |
93 * @param avctx [in] ptr to the AVCodecContext | |
94 * @return result code: 0 = OK, -1 = error | |
95 */ | |
96 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx) | |
97 { | |
98 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, blk_size_changed = 0; | |
99 IVIBandDesc *band, *band1, *band2; | |
100 IVIPicConfig pic_conf; | |
101 | |
102 ctx->gop_flags = get_bits(&ctx->gb, 8); | |
103 | |
104 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0; | |
105 | |
106 if (ctx->gop_flags & IVI5_IS_PROTECTED) | |
107 ctx->lock_word = get_bits_long(&ctx->gb, 32); | |
108 | |
109 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0; | |
110 if (tile_size > 256) { | |
111 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size); | |
112 return -1; | |
113 } | |
114 | |
115 /* decode number of wavelet bands */ | |
116 /* num_levels * 3 + 1 */ | |
117 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1; | |
118 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1; | |
119 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; | |
120 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { | |
121 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", | |
122 pic_conf.luma_bands, pic_conf.chroma_bands); | |
123 return -1; | |
124 } | |
125 | |
126 pic_size_indx = get_bits(&ctx->gb, 4); | |
127 if (pic_size_indx == IVI5_PIC_SIZE_ESC) { | |
128 pic_conf.pic_height = get_bits(&ctx->gb, 13); | |
129 pic_conf.pic_width = get_bits(&ctx->gb, 13); | |
130 } else { | |
131 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2; | |
132 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2; | |
133 } | |
134 | |
135 if (ctx->gop_flags & 2) { | |
136 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n"); | |
137 return -1; | |
138 } | |
139 | |
140 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; | |
141 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; | |
142 | |
143 if (!tile_size) { | |
144 pic_conf.tile_height = pic_conf.pic_height; | |
145 pic_conf.tile_width = pic_conf.pic_width; | |
146 } else { | |
147 pic_conf.tile_height = pic_conf.tile_width = tile_size; | |
148 } | |
149 | |
150 /* check if picture layout was changed and reallocate buffers */ | |
151 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) { | |
152 result = ff_ivi_init_planes(ctx->planes, &pic_conf); | |
153 if (result) { | |
154 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); | |
155 return -1; | |
156 } | |
157 ctx->pic_conf = pic_conf; | |
158 blk_size_changed = 1; /* force reallocation of the internal structures */ | |
159 } | |
160 | |
161 for (p = 0; p <= 1; p++) { | |
162 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { | |
163 band = &ctx->planes[p].bands[i]; | |
164 | |
165 band->is_halfpel = get_bits1(&ctx->gb); | |
166 | |
167 mb_size = get_bits1(&ctx->gb); | |
168 blk_size = 8 >> get_bits1(&ctx->gb); | |
169 mb_size = blk_size << !mb_size; | |
170 | |
171 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size; | |
172 if (blk_size_changed) { | |
173 band->mb_size = mb_size; | |
174 band->blk_size = blk_size; | |
175 } | |
176 | |
177 if (get_bits1(&ctx->gb)) { | |
178 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n"); | |
179 return -1; | |
180 } | |
181 | |
182 /* select transform function and scan pattern according to plane and band number */ | |
183 switch ((p << 2) + i) { | |
184 case 0: | |
185 band->inv_transform = ff_ivi_inverse_slant_8x8; | |
186 band->dc_transform = ff_ivi_dc_slant_2d; | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
187 band->scan = ff_zigzag_direct; |
11107 | 188 break; |
189 | |
190 case 1: | |
191 band->inv_transform = ff_ivi_row_slant8; | |
192 band->dc_transform = ff_ivi_dc_row_slant; | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
193 band->scan = ivi5_scans8x8[0]; |
11107 | 194 break; |
195 | |
196 case 2: | |
197 band->inv_transform = ff_ivi_col_slant8; | |
198 band->dc_transform = ff_ivi_dc_col_slant; | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
199 band->scan = ivi5_scans8x8[1]; |
11107 | 200 break; |
201 | |
202 case 3: | |
203 band->inv_transform = ff_ivi_put_pixels_8x8; | |
204 band->dc_transform = ff_ivi_put_dc_pixel_8x8; | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
205 band->scan = ivi5_scans8x8[1]; |
11107 | 206 break; |
207 | |
208 case 4: | |
209 band->inv_transform = ff_ivi_inverse_slant_4x4; | |
210 band->dc_transform = ff_ivi_dc_slant_2d; | |
211 band->scan = ivi5_scan4x4; | |
212 break; | |
213 } | |
214 | |
215 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 || | |
216 band->inv_transform == ff_ivi_inverse_slant_4x4; | |
217 | |
218 /* select dequant matrix according to plane and band number */ | |
219 if (!p) { | |
220 band->quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0; | |
221 } else { | |
222 band->quant_mat = 5; | |
223 } | |
224 | |
225 if (get_bits(&ctx->gb, 2)) { | |
226 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n"); | |
227 return -1; | |
228 } | |
229 } | |
230 } | |
231 | |
232 /* copy chroma parameters into the 2nd chroma plane */ | |
233 for (i = 0; i < pic_conf.chroma_bands; i++) { | |
234 band1 = &ctx->planes[1].bands[i]; | |
235 band2 = &ctx->planes[2].bands[i]; | |
236 | |
237 band2->width = band1->width; | |
238 band2->height = band1->height; | |
239 band2->mb_size = band1->mb_size; | |
240 band2->blk_size = band1->blk_size; | |
241 band2->is_halfpel = band1->is_halfpel; | |
242 band2->quant_mat = band1->quant_mat; | |
243 band2->scan = band1->scan; | |
244 band2->inv_transform = band1->inv_transform; | |
245 band2->dc_transform = band1->dc_transform; | |
246 band2->is_2d_trans = band1->is_2d_trans; | |
247 } | |
248 | |
249 /* reallocate internal structures if needed */ | |
250 if (blk_size_changed) { | |
251 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width, | |
252 pic_conf.tile_height); | |
253 if (result) { | |
254 av_log(avctx, AV_LOG_ERROR, | |
255 "Couldn't reallocate internal structures!\n"); | |
256 return -1; | |
257 } | |
258 } | |
259 | |
260 if (ctx->gop_flags & 8) { | |
261 if (get_bits(&ctx->gb, 3)) { | |
262 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n"); | |
263 return -1; | |
264 } | |
265 | |
266 if (get_bits1(&ctx->gb)) | |
267 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */ | |
268 } | |
269 | |
270 align_get_bits(&ctx->gb); | |
271 | |
272 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */ | |
273 | |
274 /* skip GOP extension if any */ | |
275 if (get_bits1(&ctx->gb)) { | |
276 do { | |
277 i = get_bits(&ctx->gb, 16); | |
278 } while (i & 0x8000); | |
279 } | |
280 | |
281 align_get_bits(&ctx->gb); | |
282 | |
283 return 0; | |
284 } | |
285 | |
286 | |
287 /** | |
288 * Skips a header extension. | |
289 * | |
290 * @param gb [in,out] the GetBit context | |
291 */ | |
292 static inline void skip_hdr_extension(GetBitContext *gb) | |
293 { | |
294 int i, len; | |
295 | |
296 do { | |
297 len = get_bits(gb, 8); | |
298 for (i = 0; i < len; i++) skip_bits(gb, 8); | |
299 } while(len); | |
300 } | |
301 | |
302 | |
303 /** | |
304 * Decodes Indeo5 picture header. | |
305 * | |
306 * @param ctx [in,out] ptr to the decoder context | |
307 * @param avctx [in] ptr to the AVCodecContext | |
308 * @return result code: 0 = OK, -1 = error | |
309 */ | |
310 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx) | |
311 { | |
312 int result; | |
313 IVIHuffDesc new_huff; | |
314 | |
315 if (get_bits(&ctx->gb, 5) != 0x1F) { | |
316 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); | |
317 return -1; | |
318 } | |
319 | |
320 ctx->prev_frame_type = ctx->frame_type; | |
321 ctx->frame_type = get_bits(&ctx->gb, 3); | |
322 if (ctx->frame_type >= 5) { | |
323 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type); | |
324 return -1; | |
325 } | |
326 | |
327 ctx->frame_num = get_bits(&ctx->gb, 8); | |
328 | |
329 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
330 if (decode_gop_header(ctx, avctx)) | |
331 return -1; | |
332 } | |
333 | |
334 if (ctx->frame_type != FRAMETYPE_NULL) { | |
335 ctx->frame_flags = get_bits(&ctx->gb, 8); | |
336 | |
337 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0; | |
338 | |
339 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0; | |
340 | |
341 /* skip unknown extension if any */ | |
342 if (ctx->frame_flags & 0x20) | |
343 skip_hdr_extension(&ctx->gb); /* XXX: untested */ | |
344 | |
345 /* decode macroblock huffman codebook */ | |
346 if (ctx->frame_flags & 0x40) { | |
347 ctx->mb_huff_sel = ff_ivi_dec_huff_desc(&ctx->gb, &new_huff); | |
348 if (ctx->mb_huff_sel != 7) { | |
349 ctx->mb_vlc = &mb_vlc_tabs[ctx->mb_huff_sel]; | |
350 } else { | |
351 if (ff_ivi_huff_desc_cmp(&new_huff, &ctx->mb_huff_desc)) { | |
352 ff_ivi_huff_desc_copy(&ctx->mb_huff_desc, &new_huff); | |
353 | |
354 if (ctx->mb_vlc_cust.table) | |
355 free_vlc(&ctx->mb_vlc_cust); | |
356 result = ff_ivi_create_huff_from_desc(&ctx->mb_huff_desc, | |
357 &ctx->mb_vlc_cust, 0); | |
358 if (result) { | |
359 av_log(avctx, AV_LOG_ERROR, "Error while initializing custom macroblock vlc table!\n"); | |
360 return -1; | |
361 } | |
362 } | |
363 ctx->mb_vlc = &ctx->mb_vlc_cust; | |
364 } | |
365 } else { | |
366 ctx->mb_vlc = &mb_vlc_tabs[7]; /* select the default macroblock huffman table */ | |
367 } | |
368 | |
369 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */ | |
370 } | |
371 | |
372 align_get_bits(&ctx->gb); | |
373 | |
374 return 0; | |
375 } | |
376 | |
377 | |
378 /** | |
379 * Decodes Indeo5 band header. | |
380 * | |
381 * @param ctx [in,out] ptr to the decoder context | |
382 * @param band [in,out] ptr to the band descriptor | |
383 * @param avctx [in] ptr to the AVCodecContext | |
384 * @return result code: 0 = OK, -1 = error | |
385 */ | |
386 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band, | |
387 AVCodecContext *avctx) | |
388 { | |
389 int i, result; | |
390 uint8_t band_flags; | |
391 IVIHuffDesc new_huff; | |
392 | |
393 band_flags = get_bits(&ctx->gb, 8); | |
394 | |
395 if (band_flags & 1) { | |
396 band->is_empty = 1; | |
397 return 0; | |
398 } | |
399 | |
400 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0; | |
401 | |
402 band->inherit_mv = band_flags & 2; | |
403 band->inherit_qdelta = band_flags & 8; | |
404 band->qdelta_present = band_flags & 4; | |
405 if (!band->qdelta_present) band->inherit_qdelta = 1; | |
406 | |
407 /* decode rvmap probability corrections if any */ | |
408 band->num_corr = 0; /* there are no corrections */ | |
409 if (band_flags & 0x10) { | |
410 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ | |
411 if (band->num_corr > 61) { | |
412 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", | |
413 band->num_corr); | |
414 return -1; | |
415 } | |
416 | |
417 /* read correction pairs */ | |
418 for (i = 0; i < band->num_corr * 2; i++) | |
419 band->corr[i] = get_bits(&ctx->gb, 8); | |
420 } | |
421 | |
422 /* select appropriate rvmap table for this band */ | |
423 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8; | |
424 | |
425 /* decode block huffman codebook */ | |
426 if (band_flags & 0x80) { | |
427 band->huff_sel = ff_ivi_dec_huff_desc(&ctx->gb, &new_huff); | |
428 if (band->huff_sel != 7) { | |
429 band->blk_vlc = &blk_vlc_tabs[band->huff_sel]; | |
430 } else { | |
431 if (ff_ivi_huff_desc_cmp(&new_huff, &band->huff_desc)) { | |
432 ff_ivi_huff_desc_copy(&band->huff_desc, &new_huff); | |
433 | |
434 if (band->blk_vlc_cust.table) | |
435 free_vlc(&band->blk_vlc_cust); | |
436 result = ff_ivi_create_huff_from_desc(&band->huff_desc, | |
437 &band->blk_vlc_cust, 0); | |
438 if (result) { | |
439 av_log(avctx, AV_LOG_ERROR, "Error while initializing custom block vlc table!\n"); | |
440 return -1; | |
441 } | |
442 } | |
443 band->blk_vlc = &band->blk_vlc_cust; | |
444 } | |
445 } else { | |
446 band->blk_vlc = &blk_vlc_tabs[7]; /* select the default macroblock huffman table */ | |
447 } | |
448 | |
449 band->checksum_present = get_bits1(&ctx->gb); | |
450 if (band->checksum_present) | |
451 band->checksum = get_bits(&ctx->gb, 16); | |
452 | |
453 band->glob_quant = get_bits(&ctx->gb, 5); | |
454 | |
455 /* skip unknown extension if any */ | |
456 if (band_flags & 0x20) { /* XXX: untested */ | |
457 align_get_bits(&ctx->gb); | |
458 skip_hdr_extension(&ctx->gb); | |
459 } | |
460 | |
461 align_get_bits(&ctx->gb); | |
462 | |
463 return 0; | |
464 } | |
465 | |
466 | |
467 /** | |
468 * Decodes info (block type, cbp, quant delta, motion vector) | |
469 * for all macroblocks in the current tile. | |
470 * | |
471 * @param ctx [in,out] ptr to the decoder context | |
472 * @param band [in,out] ptr to the band descriptor | |
473 * @param tile [in,out] ptr to the tile descriptor | |
474 * @param avctx [in] ptr to the AVCodecContext | |
475 * @return result code: 0 = OK, -1 = error | |
476 */ | |
477 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band, | |
478 IVITile *tile, AVCodecContext *avctx) | |
479 { | |
480 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, | |
481 mv_scale, blks_per_mb; | |
482 IVIMbInfo *mb, *ref_mb; | |
483 int row_offset = band->mb_size * band->pitch; | |
484 | |
485 mb = tile->mbs; | |
486 ref_mb = tile->ref_mbs; | |
487 offs = tile->ypos * band->pitch + tile->xpos; | |
488 | |
489 /* scale factor for motion vectors */ | |
490 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); | |
491 mv_x = mv_y = 0; | |
492 | |
493 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) { | |
494 mb_offset = offs; | |
495 | |
496 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) { | |
497 mb->xpos = x; | |
498 mb->ypos = y; | |
499 mb->buf_offs = mb_offset; | |
500 | |
501 if (get_bits1(&ctx->gb)) { | |
502 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
503 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); | |
504 return -1; | |
505 } | |
506 mb->type = 1; /* empty macroblocks are always INTER */ | |
507 mb->cbp = 0; /* all blocks are empty */ | |
508 | |
509 mb->q_delta = 0; | |
510 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) { | |
511 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
512 IVI_VLC_BITS, 1); | |
513 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
514 } | |
515 | |
516 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ | |
517 if (band->inherit_mv){ | |
518 /* motion vector inheritance */ | |
519 if (mv_scale) { | |
520 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
521 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
522 } else { | |
523 mb->mv_x = ref_mb->mv_x; | |
524 mb->mv_y = ref_mb->mv_y; | |
525 } | |
526 } | |
527 } else { | |
528 if (band->inherit_mv) { | |
529 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ | |
530 } else if (ctx->frame_type == FRAMETYPE_INTRA) { | |
531 mb->type = 0; /* mb_type is always INTRA for intra-frames */ | |
532 } else { | |
533 mb->type = get_bits1(&ctx->gb); | |
534 } | |
535 | |
536 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; | |
537 mb->cbp = get_bits(&ctx->gb, blks_per_mb); | |
538 | |
539 mb->q_delta = 0; | |
540 if (band->qdelta_present) { | |
541 if (band->inherit_qdelta) { | |
542 if (ref_mb) mb->q_delta = ref_mb->q_delta; | |
543 } else if (mb->cbp || (!band->plane && !band->band_num && | |
544 (ctx->frame_flags & 8))) { | |
545 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
546 IVI_VLC_BITS, 1); | |
547 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
548 } | |
549 } | |
550 | |
551 if (!mb->type) { | |
552 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ | |
553 } else { | |
554 if (band->inherit_mv){ | |
555 /* motion vector inheritance */ | |
556 if (mv_scale) { | |
557 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
558 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
559 } else { | |
560 mb->mv_x = ref_mb->mv_x; | |
561 mb->mv_y = ref_mb->mv_y; | |
562 } | |
563 } else { | |
564 /* decode motion vector deltas */ | |
565 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
566 IVI_VLC_BITS, 1); | |
567 mv_y += IVI_TOSIGNED(mv_delta); | |
568 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
569 IVI_VLC_BITS, 1); | |
570 mv_x += IVI_TOSIGNED(mv_delta); | |
571 mb->mv_x = mv_x; | |
572 mb->mv_y = mv_y; | |
573 } | |
574 } | |
575 } | |
576 | |
577 mb++; | |
578 if (ref_mb) | |
579 ref_mb++; | |
580 mb_offset += band->mb_size; | |
581 } | |
582 | |
583 offs += row_offset; | |
584 } | |
585 | |
586 align_get_bits(&ctx->gb); | |
587 | |
588 return 0; | |
589 } | |
590 | |
591 | |
592 /** | |
593 * Decodes an Indeo5 band. | |
594 * | |
595 * @param ctx [in,out] ptr to the decoder context | |
596 * @param band [in,out] ptr to the band descriptor | |
597 * @param avctx [in] ptr to the AVCodecContext | |
598 * @return result code: 0 = OK, -1 = error | |
599 */ | |
600 static int decode_band(IVI5DecContext *ctx, int plane_num, | |
601 IVIBandDesc *band, AVCodecContext *avctx) | |
602 { | |
603 int result, i, t, idx1, idx2; | |
604 IVITile *tile; | |
605 | |
606 band->buf = band->bufs[ctx->dst_buf]; | |
607 band->ref_buf = band->bufs[ctx->ref_buf]; | |
608 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3); | |
609 | |
610 result = decode_band_hdr(ctx, band, avctx); | |
611 if (result) { | |
612 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n", | |
613 result); | |
614 return -1; | |
615 } | |
616 | |
617 if (band->is_empty) { | |
618 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n"); | |
619 return -1; | |
620 } | |
621 | |
622 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel]; | |
623 | |
624 /* apply corrections to the selected rvmap table if present */ | |
625 for (i = 0; i < band->num_corr; i++) { | |
626 idx1 = band->corr[i*2]; | |
627 idx2 = band->corr[i*2+1]; | |
628 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); | |
629 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); | |
630 } | |
631 | |
632 for (t = 0; t < band->num_tiles; t++) { | |
633 tile = &band->tiles[t]; | |
634 | |
635 tile->is_empty = get_bits1(&ctx->gb); | |
636 if (tile->is_empty) { | |
637 ff_ivi_process_empty_tile(avctx, band, tile, | |
638 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3)); | |
639 align_get_bits(&ctx->gb); | |
640 } else { | |
641 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb); | |
642 | |
643 result = decode_mb_info(ctx, band, tile, avctx); | |
644 if (result < 0) | |
645 break; | |
646 | |
647 if (band->blk_size == 8) { | |
648 band->intra_base = &ivi5_base_quant_8x8_intra[band->quant_mat][0]; | |
649 band->inter_base = &ivi5_base_quant_8x8_inter[band->quant_mat][0]; | |
650 band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0]; | |
651 band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0]; | |
652 } else { | |
653 band->intra_base = ivi5_base_quant_4x4_intra; | |
654 band->inter_base = ivi5_base_quant_4x4_inter; | |
655 band->intra_scale = ivi5_scale_quant_4x4_intra; | |
656 band->inter_scale = ivi5_scale_quant_4x4_inter; | |
657 } | |
658 | |
659 result = ff_ivi_decode_blocks(&ctx->gb, band, tile); | |
660 if (result < 0) { | |
661 av_log(avctx, AV_LOG_ERROR, "Corrupted blocks data encountered!\n"); | |
662 break; | |
663 } | |
664 } | |
665 } | |
666 | |
667 /* restore the selected rvmap table by applying its corrections in reverse order */ | |
668 for (i = band->num_corr-1; i >= 0; i--) { | |
669 idx1 = band->corr[i*2]; | |
670 idx2 = band->corr[i*2+1]; | |
671 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); | |
672 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); | |
673 } | |
674 | |
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
675 #if IVI_DEBUG |
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
676 if (band->checksum_present) { |
11121
1d4aeef800d4
Move 'chksum' declaration to the only block where that variable is used
kostya
parents:
11120
diff
changeset
|
677 uint16_t chksum = ivi_calc_band_checksum(band); |
11107 | 678 if (chksum != band->checksum) { |
679 av_log(avctx, AV_LOG_ERROR, | |
680 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n", | |
681 band->plane, band->band_num, band->checksum, chksum); | |
682 } | |
683 } | |
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
684 #endif |
11107 | 685 |
686 return result; | |
687 } | |
688 | |
689 | |
690 /** | |
691 * Switches buffers. | |
692 * | |
693 * @param ctx [in,out] ptr to the decoder context | |
694 * @param avctx [in] ptr to the AVCodecContext | |
695 */ | |
696 static void switch_buffers(IVI5DecContext *ctx, AVCodecContext *avctx) | |
697 { | |
698 switch (ctx->frame_type) { | |
699 case FRAMETYPE_INTRA: | |
700 ctx->buf_switch = 0; | |
701 ctx->dst_buf = 0; | |
702 ctx->ref_buf = 0; | |
703 break; | |
704 case FRAMETYPE_INTER: | |
705 ctx->buf_switch &= 1; | |
706 /* swap buffers only if there were no droppable frames */ | |
707 if (ctx->prev_frame_type != FRAMETYPE_INTER_NOREF && | |
708 ctx->prev_frame_type != FRAMETYPE_INTER_SCAL) | |
709 ctx->buf_switch ^= 1; | |
710 ctx->dst_buf = ctx->buf_switch; | |
711 ctx->ref_buf = ctx->buf_switch ^ 1; | |
712 break; | |
713 case FRAMETYPE_INTER_SCAL: | |
714 if (ctx->prev_frame_type == FRAMETYPE_INTER_NOREF) | |
715 break; | |
716 if (ctx->prev_frame_type != FRAMETYPE_INTER_SCAL) { | |
717 ctx->buf_switch ^= 1; | |
718 ctx->dst_buf = ctx->buf_switch; | |
719 ctx->ref_buf = ctx->buf_switch ^ 1; | |
720 } else { | |
721 ctx->buf_switch ^= 2; | |
722 ctx->dst_buf = 2; | |
723 ctx->ref_buf = ctx->buf_switch & 1; | |
724 if (!(ctx->buf_switch & 2)) | |
725 FFSWAP(int, ctx->dst_buf, ctx->ref_buf); | |
726 } | |
727 break; | |
728 case FRAMETYPE_INTER_NOREF: | |
729 if (ctx->prev_frame_type == FRAMETYPE_INTER_SCAL) { | |
730 ctx->buf_switch ^= 2; | |
731 ctx->dst_buf = 2; | |
732 ctx->ref_buf = ctx->buf_switch & 1; | |
733 if (!(ctx->buf_switch & 2)) | |
734 FFSWAP(int, ctx->dst_buf, ctx->ref_buf); | |
735 } else { | |
736 ctx->buf_switch ^= 1; | |
737 ctx->dst_buf = ctx->buf_switch & 1; | |
738 ctx->ref_buf = (ctx->buf_switch & 1) ^ 1; | |
739 } | |
740 break; | |
741 case FRAMETYPE_NULL: | |
742 return; | |
743 default: | |
744 av_log(avctx, AV_LOG_ERROR, "unsupported frame type: %d\n", ctx->frame_type); | |
745 } | |
746 } | |
747 | |
748 | |
749 /** | |
750 * Initializes Indeo5 decoder. | |
751 */ | |
752 static av_cold int decode_init(AVCodecContext *avctx) | |
753 { | |
754 IVI5DecContext *ctx = avctx->priv_data; | |
755 int i, result; | |
756 | |
757 /* initialize static vlc tables for macroblock/block signals */ | |
758 for (i = 0; i < 8; i++) { | |
759 ff_ivi_create_huff_from_desc(&ff_ivi_mb_huff_desc[i], &mb_vlc_tabs[i], 1); | |
760 ff_ivi_create_huff_from_desc(&ff_ivi_blk_huff_desc[i], &blk_vlc_tabs[i], 1); | |
761 } | |
762 | |
763 /* copy rvmap tables in our context so we can apply changes to them */ | |
764 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); | |
765 | |
766 /* set the initial picture layout according to the basic profile: | |
767 there is only one band per plane (no scalability), only one tile (no local decoding) | |
768 and picture format = YVU9 */ | |
769 ctx->pic_conf.pic_width = avctx->width; | |
770 ctx->pic_conf.pic_height = avctx->height; | |
771 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2; | |
772 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2; | |
773 ctx->pic_conf.tile_width = avctx->width; | |
774 ctx->pic_conf.tile_height = avctx->height; | |
775 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1; | |
776 | |
777 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf); | |
778 if (result) { | |
779 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n"); | |
780 return -1; | |
781 } | |
782 | |
783 avctx->pix_fmt = PIX_FMT_YUV410P; | |
784 | |
785 return 0; | |
786 } | |
787 | |
788 | |
789 /** | |
790 * main decoder function | |
791 */ | |
792 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, | |
793 AVPacket *avpkt) | |
794 { | |
795 IVI5DecContext *ctx = avctx->priv_data; | |
796 const uint8_t *buf = avpkt->data; | |
797 int buf_size = avpkt->size; | |
798 int result, p, b; | |
799 | |
800 init_get_bits(&ctx->gb, buf, buf_size * 8); | |
801 ctx->frame_data = buf; | |
802 ctx->frame_size = buf_size; | |
803 | |
804 result = decode_pic_hdr(ctx, avctx); | |
805 if (result) { | |
806 av_log(avctx, AV_LOG_ERROR, | |
807 "Error while decoding picture header: %d\n", result); | |
808 return -1; | |
809 } | |
810 | |
811 if (ctx->gop_flags & IVI5_IS_PROTECTED) { | |
812 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n"); | |
813 return -1; | |
814 } | |
815 | |
816 switch_buffers(ctx, avctx); | |
817 | |
818 //START_TIMER; | |
819 | |
820 if (ctx->frame_type == FRAMETYPE_NULL) { | |
821 ctx->frame_type = ctx->prev_frame_type; | |
822 } else { | |
823 for (p = 0; p < 3; p++) { | |
824 for (b = 0; b < ctx->planes[p].num_bands; b++) { | |
825 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx); | |
826 if (result) { | |
827 av_log(avctx, AV_LOG_ERROR, | |
828 "Error while decoding band: %d, plane: %d\n", b, p); | |
829 return -1; | |
830 } | |
831 } | |
832 } | |
833 } | |
834 | |
835 //STOP_TIMER("decode_planes"); | |
836 | |
837 if (ctx->frame.data[0]) | |
838 avctx->release_buffer(avctx, &ctx->frame); | |
839 | |
840 ctx->frame.reference = 0; | |
841 if (avctx->get_buffer(avctx, &ctx->frame) < 0) { | |
842 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
843 return -1; | |
844 } | |
845 | |
846 if (ctx->is_scalable) { | |
847 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4); | |
848 } else { | |
849 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]); | |
850 } | |
851 | |
852 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]); | |
853 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]); | |
854 | |
855 *data_size = sizeof(AVFrame); | |
856 *(AVFrame*)data = ctx->frame; | |
857 | |
858 return buf_size; | |
859 } | |
860 | |
861 | |
862 /** | |
863 * Closes Indeo5 decoder and cleans up its context. | |
864 */ | |
865 static av_cold int decode_close(AVCodecContext *avctx) | |
866 { | |
867 IVI5DecContext *ctx = avctx->priv_data; | |
868 | |
869 ff_ivi_free_buffers(&ctx->planes[0]); | |
870 | |
871 if (ctx->frame.data[0]) | |
872 avctx->release_buffer(avctx, &ctx->frame); | |
873 | |
874 return 0; | |
875 } | |
876 | |
877 | |
878 AVCodec indeo5_decoder = { | |
879 .name = "indeo5", | |
880 .type = CODEC_TYPE_VIDEO, | |
881 .id = CODEC_ID_INDEO5, | |
882 .priv_data_size = sizeof(IVI5DecContext), | |
883 .init = decode_init, | |
884 .close = decode_close, | |
885 .decode = decode_frame, | |
886 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"), | |
887 }; |