Mercurial > libavcodec.hg
annotate indeo5.c @ 11916:73f4fd490f2a libavcodec
Make "topright" argument to pred4x4() const.
Patch by David Conrad <lessen42 gmail com>.
author | rbultje |
---|---|
date | Tue, 22 Jun 2010 19:12:54 +0000 |
parents | 238cfd740f10 |
children | fdafbcef52f5 |
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 /** | |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11560
diff
changeset
|
23 * @file |
11107 | 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 | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
60 int inter_scal; ///< signals a sequence of scalable inter frames |
11503 | 61 int dst_buf; ///< buffer index for the currently decoded frame |
62 int ref_buf; ///< inter frame reference buffer index | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
63 int ref2_buf; ///< temporal storage for switching buffers |
11107 | 64 uint32_t frame_size; ///< frame size in bytes |
65 int frame_type; | |
66 int prev_frame_type; ///< frame type of the previous frame | |
67 int frame_num; | |
68 uint32_t pic_hdr_size; ///< picture header size in bytes | |
69 uint8_t frame_flags; | |
70 uint16_t checksum; ///< frame checksum | |
71 | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
72 IVIHuffTab mb_vlc; ///< vlc table for decoding macroblock data |
11107 | 73 |
74 uint16_t gop_hdr_size; | |
75 uint8_t gop_flags; | |
76 int is_scalable; | |
77 uint32_t lock_word; | |
78 IVIPicConfig pic_conf; | |
79 } IVI5DecContext; | |
80 | |
81 | |
82 /** | |
83 * Decodes Indeo5 GOP (Group of pictures) header. | |
84 * This header is present in key frames only. | |
85 * It defines parameters for all frames in a GOP. | |
86 * | |
87 * @param ctx [in,out] ptr to the decoder context | |
88 * @param avctx [in] ptr to the AVCodecContext | |
89 * @return result code: 0 = OK, -1 = error | |
90 */ | |
91 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx) | |
92 { | |
11872
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
93 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
94 int quant_mat, blk_size_changed = 0; |
11107 | 95 IVIBandDesc *band, *band1, *band2; |
96 IVIPicConfig pic_conf; | |
97 | |
98 ctx->gop_flags = get_bits(&ctx->gb, 8); | |
99 | |
100 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0; | |
101 | |
102 if (ctx->gop_flags & IVI5_IS_PROTECTED) | |
103 ctx->lock_word = get_bits_long(&ctx->gb, 32); | |
104 | |
105 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0; | |
106 if (tile_size > 256) { | |
107 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size); | |
108 return -1; | |
109 } | |
110 | |
111 /* decode number of wavelet bands */ | |
112 /* num_levels * 3 + 1 */ | |
113 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1; | |
114 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1; | |
115 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; | |
116 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { | |
117 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", | |
118 pic_conf.luma_bands, pic_conf.chroma_bands); | |
119 return -1; | |
120 } | |
121 | |
122 pic_size_indx = get_bits(&ctx->gb, 4); | |
123 if (pic_size_indx == IVI5_PIC_SIZE_ESC) { | |
124 pic_conf.pic_height = get_bits(&ctx->gb, 13); | |
125 pic_conf.pic_width = get_bits(&ctx->gb, 13); | |
126 } else { | |
127 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2; | |
128 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2; | |
129 } | |
130 | |
131 if (ctx->gop_flags & 2) { | |
132 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n"); | |
133 return -1; | |
134 } | |
135 | |
136 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; | |
137 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; | |
138 | |
139 if (!tile_size) { | |
140 pic_conf.tile_height = pic_conf.pic_height; | |
141 pic_conf.tile_width = pic_conf.pic_width; | |
142 } else { | |
143 pic_conf.tile_height = pic_conf.tile_width = tile_size; | |
144 } | |
145 | |
146 /* check if picture layout was changed and reallocate buffers */ | |
147 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) { | |
148 result = ff_ivi_init_planes(ctx->planes, &pic_conf); | |
149 if (result) { | |
150 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); | |
151 return -1; | |
152 } | |
153 ctx->pic_conf = pic_conf; | |
154 blk_size_changed = 1; /* force reallocation of the internal structures */ | |
155 } | |
156 | |
157 for (p = 0; p <= 1; p++) { | |
158 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { | |
159 band = &ctx->planes[p].bands[i]; | |
160 | |
161 band->is_halfpel = get_bits1(&ctx->gb); | |
162 | |
163 mb_size = get_bits1(&ctx->gb); | |
164 blk_size = 8 >> get_bits1(&ctx->gb); | |
165 mb_size = blk_size << !mb_size; | |
166 | |
167 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size; | |
168 if (blk_size_changed) { | |
169 band->mb_size = mb_size; | |
170 band->blk_size = blk_size; | |
171 } | |
172 | |
173 if (get_bits1(&ctx->gb)) { | |
174 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n"); | |
175 return -1; | |
176 } | |
177 | |
178 /* select transform function and scan pattern according to plane and band number */ | |
179 switch ((p << 2) + i) { | |
180 case 0: | |
181 band->inv_transform = ff_ivi_inverse_slant_8x8; | |
182 band->dc_transform = ff_ivi_dc_slant_2d; | |
11213
384b6a615a92
remove ivi5_scans8x8[0], it duplicates ff_zigzag_direct
stefang
parents:
11121
diff
changeset
|
183 band->scan = ff_zigzag_direct; |
11107 | 184 break; |
185 | |
186 case 1: | |
187 band->inv_transform = ff_ivi_row_slant8; | |
188 band->dc_transform = ff_ivi_dc_row_slant; | |
11778
31dae9841fe1
Add the forgotten ff_ prefix to the shareable scan patterns.
maxim
parents:
11772
diff
changeset
|
189 band->scan = ff_ivi_vertical_scan_8x8; |
11107 | 190 break; |
191 | |
192 case 2: | |
193 band->inv_transform = ff_ivi_col_slant8; | |
194 band->dc_transform = ff_ivi_dc_col_slant; | |
11778
31dae9841fe1
Add the forgotten ff_ prefix to the shareable scan patterns.
maxim
parents:
11772
diff
changeset
|
195 band->scan = ff_ivi_horizontal_scan_8x8; |
11107 | 196 break; |
197 | |
198 case 3: | |
199 band->inv_transform = ff_ivi_put_pixels_8x8; | |
200 band->dc_transform = ff_ivi_put_dc_pixel_8x8; | |
11778
31dae9841fe1
Add the forgotten ff_ prefix to the shareable scan patterns.
maxim
parents:
11772
diff
changeset
|
201 band->scan = ff_ivi_horizontal_scan_8x8; |
11107 | 202 break; |
203 | |
204 case 4: | |
205 band->inv_transform = ff_ivi_inverse_slant_4x4; | |
206 band->dc_transform = ff_ivi_dc_slant_2d; | |
11778
31dae9841fe1
Add the forgotten ff_ prefix to the shareable scan patterns.
maxim
parents:
11772
diff
changeset
|
207 band->scan = ff_ivi_direct_scan_4x4; |
11107 | 208 break; |
209 } | |
210 | |
211 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 || | |
212 band->inv_transform == ff_ivi_inverse_slant_4x4; | |
213 | |
214 /* select dequant matrix according to plane and band number */ | |
215 if (!p) { | |
11872
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
216 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0; |
11107 | 217 } else { |
11872
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
218 quant_mat = 5; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
219 } |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
220 |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
221 if (band->blk_size == 8) { |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
222 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0]; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
223 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0]; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
224 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0]; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
225 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0]; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
226 } else { |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
227 band->intra_base = ivi5_base_quant_4x4_intra; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
228 band->inter_base = ivi5_base_quant_4x4_inter; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
229 band->intra_scale = ivi5_scale_quant_4x4_intra; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
230 band->inter_scale = ivi5_scale_quant_4x4_inter; |
11107 | 231 } |
232 | |
233 if (get_bits(&ctx->gb, 2)) { | |
234 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n"); | |
235 return -1; | |
236 } | |
237 } | |
238 } | |
239 | |
240 /* copy chroma parameters into the 2nd chroma plane */ | |
241 for (i = 0; i < pic_conf.chroma_bands; i++) { | |
242 band1 = &ctx->planes[1].bands[i]; | |
243 band2 = &ctx->planes[2].bands[i]; | |
244 | |
245 band2->width = band1->width; | |
246 band2->height = band1->height; | |
247 band2->mb_size = band1->mb_size; | |
248 band2->blk_size = band1->blk_size; | |
249 band2->is_halfpel = band1->is_halfpel; | |
11872
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
250 band2->intra_base = band1->intra_base; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
251 band2->inter_base = band1->inter_base; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
252 band2->intra_scale = band1->intra_scale; |
238cfd740f10
Move indeo5-specific quant table selection to decode_gop_hdr.
maxim
parents:
11862
diff
changeset
|
253 band2->inter_scale = band1->inter_scale; |
11107 | 254 band2->scan = band1->scan; |
255 band2->inv_transform = band1->inv_transform; | |
256 band2->dc_transform = band1->dc_transform; | |
257 band2->is_2d_trans = band1->is_2d_trans; | |
258 } | |
259 | |
260 /* reallocate internal structures if needed */ | |
261 if (blk_size_changed) { | |
262 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width, | |
263 pic_conf.tile_height); | |
264 if (result) { | |
265 av_log(avctx, AV_LOG_ERROR, | |
266 "Couldn't reallocate internal structures!\n"); | |
267 return -1; | |
268 } | |
269 } | |
270 | |
271 if (ctx->gop_flags & 8) { | |
272 if (get_bits(&ctx->gb, 3)) { | |
273 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n"); | |
274 return -1; | |
275 } | |
276 | |
277 if (get_bits1(&ctx->gb)) | |
278 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */ | |
279 } | |
280 | |
281 align_get_bits(&ctx->gb); | |
282 | |
283 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */ | |
284 | |
285 /* skip GOP extension if any */ | |
286 if (get_bits1(&ctx->gb)) { | |
287 do { | |
288 i = get_bits(&ctx->gb, 16); | |
289 } while (i & 0x8000); | |
290 } | |
291 | |
292 align_get_bits(&ctx->gb); | |
293 | |
294 return 0; | |
295 } | |
296 | |
297 | |
298 /** | |
299 * Skips a header extension. | |
300 * | |
301 * @param gb [in,out] the GetBit context | |
302 */ | |
303 static inline void skip_hdr_extension(GetBitContext *gb) | |
304 { | |
305 int i, len; | |
306 | |
307 do { | |
308 len = get_bits(gb, 8); | |
309 for (i = 0; i < len; i++) skip_bits(gb, 8); | |
310 } while(len); | |
311 } | |
312 | |
313 | |
314 /** | |
315 * Decodes Indeo5 picture header. | |
316 * | |
317 * @param ctx [in,out] ptr to the decoder context | |
318 * @param avctx [in] ptr to the AVCodecContext | |
319 * @return result code: 0 = OK, -1 = error | |
320 */ | |
321 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx) | |
322 { | |
323 if (get_bits(&ctx->gb, 5) != 0x1F) { | |
324 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); | |
325 return -1; | |
326 } | |
327 | |
328 ctx->prev_frame_type = ctx->frame_type; | |
329 ctx->frame_type = get_bits(&ctx->gb, 3); | |
330 if (ctx->frame_type >= 5) { | |
331 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type); | |
332 return -1; | |
333 } | |
334 | |
335 ctx->frame_num = get_bits(&ctx->gb, 8); | |
336 | |
337 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
338 if (decode_gop_header(ctx, avctx)) | |
339 return -1; | |
340 } | |
341 | |
342 if (ctx->frame_type != FRAMETYPE_NULL) { | |
343 ctx->frame_flags = get_bits(&ctx->gb, 8); | |
344 | |
345 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0; | |
346 | |
347 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0; | |
348 | |
349 /* skip unknown extension if any */ | |
350 if (ctx->frame_flags & 0x20) | |
351 skip_hdr_extension(&ctx->gb); /* XXX: untested */ | |
352 | |
353 /* decode macroblock huffman codebook */ | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
354 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx)) |
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
355 return -1; |
11107 | 356 |
357 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */ | |
358 } | |
359 | |
360 align_get_bits(&ctx->gb); | |
361 | |
362 return 0; | |
363 } | |
364 | |
365 | |
366 /** | |
367 * Decodes Indeo5 band header. | |
368 * | |
369 * @param ctx [in,out] ptr to the decoder context | |
370 * @param band [in,out] ptr to the band descriptor | |
371 * @param avctx [in] ptr to the AVCodecContext | |
372 * @return result code: 0 = OK, -1 = error | |
373 */ | |
374 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band, | |
375 AVCodecContext *avctx) | |
376 { | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
377 int i; |
11107 | 378 uint8_t band_flags; |
379 | |
380 band_flags = get_bits(&ctx->gb, 8); | |
381 | |
382 if (band_flags & 1) { | |
383 band->is_empty = 1; | |
384 return 0; | |
385 } | |
386 | |
387 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0; | |
388 | |
389 band->inherit_mv = band_flags & 2; | |
390 band->inherit_qdelta = band_flags & 8; | |
391 band->qdelta_present = band_flags & 4; | |
392 if (!band->qdelta_present) band->inherit_qdelta = 1; | |
393 | |
394 /* decode rvmap probability corrections if any */ | |
395 band->num_corr = 0; /* there are no corrections */ | |
396 if (band_flags & 0x10) { | |
397 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ | |
398 if (band->num_corr > 61) { | |
399 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", | |
400 band->num_corr); | |
401 return -1; | |
402 } | |
403 | |
404 /* read correction pairs */ | |
405 for (i = 0; i < band->num_corr * 2; i++) | |
406 band->corr[i] = get_bits(&ctx->gb, 8); | |
407 } | |
408 | |
409 /* select appropriate rvmap table for this band */ | |
410 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8; | |
411 | |
412 /* decode block huffman codebook */ | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
413 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx)) |
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
414 return -1; |
11107 | 415 |
416 band->checksum_present = get_bits1(&ctx->gb); | |
417 if (band->checksum_present) | |
418 band->checksum = get_bits(&ctx->gb, 16); | |
419 | |
420 band->glob_quant = get_bits(&ctx->gb, 5); | |
421 | |
422 /* skip unknown extension if any */ | |
423 if (band_flags & 0x20) { /* XXX: untested */ | |
424 align_get_bits(&ctx->gb); | |
425 skip_hdr_extension(&ctx->gb); | |
426 } | |
427 | |
428 align_get_bits(&ctx->gb); | |
429 | |
430 return 0; | |
431 } | |
432 | |
433 | |
434 /** | |
435 * Decodes info (block type, cbp, quant delta, motion vector) | |
436 * for all macroblocks in the current tile. | |
437 * | |
438 * @param ctx [in,out] ptr to the decoder context | |
439 * @param band [in,out] ptr to the band descriptor | |
440 * @param tile [in,out] ptr to the tile descriptor | |
441 * @param avctx [in] ptr to the AVCodecContext | |
442 * @return result code: 0 = OK, -1 = error | |
443 */ | |
444 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band, | |
445 IVITile *tile, AVCodecContext *avctx) | |
446 { | |
447 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, | |
448 mv_scale, blks_per_mb; | |
449 IVIMbInfo *mb, *ref_mb; | |
450 int row_offset = band->mb_size * band->pitch; | |
451 | |
452 mb = tile->mbs; | |
453 ref_mb = tile->ref_mbs; | |
454 offs = tile->ypos * band->pitch + tile->xpos; | |
455 | |
456 /* scale factor for motion vectors */ | |
457 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); | |
458 mv_x = mv_y = 0; | |
459 | |
460 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) { | |
461 mb_offset = offs; | |
462 | |
463 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) { | |
464 mb->xpos = x; | |
465 mb->ypos = y; | |
466 mb->buf_offs = mb_offset; | |
467 | |
468 if (get_bits1(&ctx->gb)) { | |
469 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
470 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); | |
471 return -1; | |
472 } | |
473 mb->type = 1; /* empty macroblocks are always INTER */ | |
474 mb->cbp = 0; /* all blocks are empty */ | |
475 | |
476 mb->q_delta = 0; | |
477 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) { | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
478 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
11107 | 479 IVI_VLC_BITS, 1); |
480 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
481 } | |
482 | |
483 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ | |
484 if (band->inherit_mv){ | |
485 /* motion vector inheritance */ | |
486 if (mv_scale) { | |
487 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
488 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
489 } else { | |
490 mb->mv_x = ref_mb->mv_x; | |
491 mb->mv_y = ref_mb->mv_y; | |
492 } | |
493 } | |
494 } else { | |
495 if (band->inherit_mv) { | |
496 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ | |
497 } else if (ctx->frame_type == FRAMETYPE_INTRA) { | |
498 mb->type = 0; /* mb_type is always INTRA for intra-frames */ | |
499 } else { | |
500 mb->type = get_bits1(&ctx->gb); | |
501 } | |
502 | |
503 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; | |
504 mb->cbp = get_bits(&ctx->gb, blks_per_mb); | |
505 | |
506 mb->q_delta = 0; | |
507 if (band->qdelta_present) { | |
508 if (band->inherit_qdelta) { | |
509 if (ref_mb) mb->q_delta = ref_mb->q_delta; | |
510 } else if (mb->cbp || (!band->plane && !band->band_num && | |
511 (ctx->frame_flags & 8))) { | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
512 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
11107 | 513 IVI_VLC_BITS, 1); |
514 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
515 } | |
516 } | |
517 | |
518 if (!mb->type) { | |
519 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ | |
520 } else { | |
521 if (band->inherit_mv){ | |
522 /* motion vector inheritance */ | |
523 if (mv_scale) { | |
524 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
525 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
526 } else { | |
527 mb->mv_x = ref_mb->mv_x; | |
528 mb->mv_y = ref_mb->mv_y; | |
529 } | |
530 } else { | |
531 /* decode motion vector deltas */ | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
532 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
11107 | 533 IVI_VLC_BITS, 1); |
534 mv_y += IVI_TOSIGNED(mv_delta); | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
535 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
11107 | 536 IVI_VLC_BITS, 1); |
537 mv_x += IVI_TOSIGNED(mv_delta); | |
538 mb->mv_x = mv_x; | |
539 mb->mv_y = mv_y; | |
540 } | |
541 } | |
542 } | |
543 | |
544 mb++; | |
545 if (ref_mb) | |
546 ref_mb++; | |
547 mb_offset += band->mb_size; | |
548 } | |
549 | |
550 offs += row_offset; | |
551 } | |
552 | |
553 align_get_bits(&ctx->gb); | |
554 | |
555 return 0; | |
556 } | |
557 | |
558 | |
559 /** | |
560 * Decodes an Indeo5 band. | |
561 * | |
562 * @param ctx [in,out] ptr to the decoder context | |
563 * @param band [in,out] ptr to the band descriptor | |
564 * @param avctx [in] ptr to the AVCodecContext | |
565 * @return result code: 0 = OK, -1 = error | |
566 */ | |
567 static int decode_band(IVI5DecContext *ctx, int plane_num, | |
568 IVIBandDesc *band, AVCodecContext *avctx) | |
569 { | |
11403
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
570 int result, i, t, idx1, idx2, pos; |
11107 | 571 IVITile *tile; |
572 | |
573 band->buf = band->bufs[ctx->dst_buf]; | |
574 band->ref_buf = band->bufs[ctx->ref_buf]; | |
575 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3); | |
576 | |
577 result = decode_band_hdr(ctx, band, avctx); | |
578 if (result) { | |
579 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n", | |
580 result); | |
581 return -1; | |
582 } | |
583 | |
584 if (band->is_empty) { | |
585 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n"); | |
586 return -1; | |
587 } | |
588 | |
589 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel]; | |
590 | |
591 /* apply corrections to the selected rvmap table if present */ | |
592 for (i = 0; i < band->num_corr; i++) { | |
593 idx1 = band->corr[i*2]; | |
594 idx2 = band->corr[i*2+1]; | |
595 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); | |
596 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); | |
597 } | |
598 | |
11403
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
599 pos = get_bits_count(&ctx->gb); |
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
600 |
11107 | 601 for (t = 0; t < band->num_tiles; t++) { |
602 tile = &band->tiles[t]; | |
603 | |
604 tile->is_empty = get_bits1(&ctx->gb); | |
605 if (tile->is_empty) { | |
606 ff_ivi_process_empty_tile(avctx, band, tile, | |
607 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3)); | |
608 } else { | |
609 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb); | |
610 | |
611 result = decode_mb_info(ctx, band, tile, avctx); | |
612 if (result < 0) | |
613 break; | |
614 | |
615 result = ff_ivi_decode_blocks(&ctx->gb, band, tile); | |
11403
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
616 if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) { |
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
617 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n"); |
11107 | 618 break; |
619 } | |
11403
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
620 pos += tile->data_size << 3; // skip to next tile |
11107 | 621 } |
622 } | |
623 | |
624 /* restore the selected rvmap table by applying its corrections in reverse order */ | |
625 for (i = band->num_corr-1; i >= 0; 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 | |
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
632 #if IVI_DEBUG |
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
633 if (band->checksum_present) { |
11121
1d4aeef800d4
Move 'chksum' declaration to the only block where that variable is used
kostya
parents:
11120
diff
changeset
|
634 uint16_t chksum = ivi_calc_band_checksum(band); |
11107 | 635 if (chksum != band->checksum) { |
636 av_log(avctx, AV_LOG_ERROR, | |
637 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n", | |
638 band->plane, band->band_num, band->checksum, chksum); | |
639 } | |
640 } | |
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
641 #endif |
11107 | 642 |
11403
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
643 align_get_bits(&ctx->gb); |
128f82b41fc1
Make Indeo 5 decoder more robust on bitstream errors.
kostya
parents:
11386
diff
changeset
|
644 |
11107 | 645 return result; |
646 } | |
647 | |
648 | |
649 /** | |
650 * Switches buffers. | |
651 * | |
652 * @param ctx [in,out] ptr to the decoder context | |
653 */ | |
11862 | 654 static void switch_buffers(IVI5DecContext *ctx) |
11107 | 655 { |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
656 switch (ctx->prev_frame_type) { |
11107 | 657 case FRAMETYPE_INTRA: |
658 case FRAMETYPE_INTER: | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
659 ctx->buf_switch ^= 1; |
11107 | 660 ctx->dst_buf = ctx->buf_switch; |
661 ctx->ref_buf = ctx->buf_switch ^ 1; | |
662 break; | |
663 case FRAMETYPE_INTER_SCAL: | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
664 if (!ctx->inter_scal) { |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
665 ctx->ref2_buf = 2; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
666 ctx->inter_scal = 1; |
11107 | 667 } |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
668 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf); |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
669 ctx->ref_buf = ctx->ref2_buf; |
11107 | 670 break; |
671 case FRAMETYPE_INTER_NOREF: | |
672 break; | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
673 } |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
674 |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
675 switch (ctx->frame_type) { |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
676 case FRAMETYPE_INTRA: |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
677 ctx->buf_switch = 0; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
678 /* FALLTHROUGH */ |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
679 case FRAMETYPE_INTER: |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
680 ctx->inter_scal = 0; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
681 ctx->dst_buf = ctx->buf_switch; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
682 ctx->ref_buf = ctx->buf_switch ^ 1; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
683 break; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
684 case FRAMETYPE_INTER_SCAL: |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
685 case FRAMETYPE_INTER_NOREF: |
11107 | 686 case FRAMETYPE_NULL: |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
687 break; |
11107 | 688 } |
689 } | |
690 | |
691 | |
692 /** | |
693 * Initializes Indeo5 decoder. | |
694 */ | |
695 static av_cold int decode_init(AVCodecContext *avctx) | |
696 { | |
697 IVI5DecContext *ctx = avctx->priv_data; | |
11246
b75449aaea3e
Macroblock and block Huffman code sets are to be used by both Indeo 4 and
kostya
parents:
11213
diff
changeset
|
698 int result; |
11107 | 699 |
11246
b75449aaea3e
Macroblock and block Huffman code sets are to be used by both Indeo 4 and
kostya
parents:
11213
diff
changeset
|
700 ff_ivi_init_static_vlc(); |
11107 | 701 |
702 /* copy rvmap tables in our context so we can apply changes to them */ | |
703 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); | |
704 | |
705 /* set the initial picture layout according to the basic profile: | |
706 there is only one band per plane (no scalability), only one tile (no local decoding) | |
707 and picture format = YVU9 */ | |
708 ctx->pic_conf.pic_width = avctx->width; | |
709 ctx->pic_conf.pic_height = avctx->height; | |
710 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2; | |
711 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2; | |
712 ctx->pic_conf.tile_width = avctx->width; | |
713 ctx->pic_conf.tile_height = avctx->height; | |
714 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1; | |
715 | |
716 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf); | |
717 if (result) { | |
718 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n"); | |
719 return -1; | |
720 } | |
721 | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
722 ctx->buf_switch = 0; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
723 ctx->inter_scal = 0; |
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
724 |
11107 | 725 avctx->pix_fmt = PIX_FMT_YUV410P; |
726 | |
727 return 0; | |
728 } | |
729 | |
730 | |
731 /** | |
732 * main decoder function | |
733 */ | |
734 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, | |
735 AVPacket *avpkt) | |
736 { | |
737 IVI5DecContext *ctx = avctx->priv_data; | |
738 const uint8_t *buf = avpkt->data; | |
739 int buf_size = avpkt->size; | |
740 int result, p, b; | |
741 | |
742 init_get_bits(&ctx->gb, buf, buf_size * 8); | |
743 ctx->frame_data = buf; | |
744 ctx->frame_size = buf_size; | |
745 | |
746 result = decode_pic_hdr(ctx, avctx); | |
747 if (result) { | |
748 av_log(avctx, AV_LOG_ERROR, | |
749 "Error while decoding picture header: %d\n", result); | |
750 return -1; | |
751 } | |
752 | |
753 if (ctx->gop_flags & IVI5_IS_PROTECTED) { | |
754 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n"); | |
755 return -1; | |
756 } | |
757 | |
11862 | 758 switch_buffers(ctx); |
11107 | 759 |
760 //START_TIMER; | |
761 | |
11504
d44678e484a3
Correct reference buffer switching in Indeo 5 decoder.
kostya
parents:
11503
diff
changeset
|
762 if (ctx->frame_type != FRAMETYPE_NULL) { |
11107 | 763 for (p = 0; p < 3; p++) { |
764 for (b = 0; b < ctx->planes[p].num_bands; b++) { | |
765 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx); | |
766 if (result) { | |
767 av_log(avctx, AV_LOG_ERROR, | |
768 "Error while decoding band: %d, plane: %d\n", b, p); | |
769 return -1; | |
770 } | |
771 } | |
772 } | |
773 } | |
774 | |
775 //STOP_TIMER("decode_planes"); | |
776 | |
777 if (ctx->frame.data[0]) | |
778 avctx->release_buffer(avctx, &ctx->frame); | |
779 | |
780 ctx->frame.reference = 0; | |
781 if (avctx->get_buffer(avctx, &ctx->frame) < 0) { | |
782 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
783 return -1; | |
784 } | |
785 | |
786 if (ctx->is_scalable) { | |
787 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4); | |
788 } else { | |
789 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]); | |
790 } | |
791 | |
792 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]); | |
793 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]); | |
794 | |
795 *data_size = sizeof(AVFrame); | |
796 *(AVFrame*)data = ctx->frame; | |
797 | |
798 return buf_size; | |
799 } | |
800 | |
801 | |
802 /** | |
803 * Closes Indeo5 decoder and cleans up its context. | |
804 */ | |
805 static av_cold int decode_close(AVCodecContext *avctx) | |
806 { | |
807 IVI5DecContext *ctx = avctx->priv_data; | |
808 | |
809 ff_ivi_free_buffers(&ctx->planes[0]); | |
810 | |
11309
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
811 if (ctx->mb_vlc.cust_tab.table) |
d617766bf19b
Encapsulate VLC information needed for decoding blocks and macroblocks in
kostya
parents:
11255
diff
changeset
|
812 free_vlc(&ctx->mb_vlc.cust_tab); |
11255
f65b3f7186b5
10l trocadero: Indeo 5 decoder did not free custom VLCs for macroblock and
kostya
parents:
11246
diff
changeset
|
813 |
11107 | 814 if (ctx->frame.data[0]) |
815 avctx->release_buffer(avctx, &ctx->frame); | |
816 | |
817 return 0; | |
818 } | |
819 | |
820 | |
821 AVCodec indeo5_decoder = { | |
822 .name = "indeo5", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
11504
diff
changeset
|
823 .type = AVMEDIA_TYPE_VIDEO, |
11107 | 824 .id = CODEC_ID_INDEO5, |
825 .priv_data_size = sizeof(IVI5DecContext), | |
826 .init = decode_init, | |
827 .close = decode_close, | |
828 .decode = decode_frame, | |
829 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"), | |
830 }; |