/src/ffmpeg/libavcodec/indeo5.c
Line | Count | Source |
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 |
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 BITSTREAM_READER_LE |
31 | | #include "avcodec.h" |
32 | | #include "codec_internal.h" |
33 | | #include "get_bits.h" |
34 | | #include "ivi.h" |
35 | | #include "ivi_dsp.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 | 36.8k | #define IVI5_PIC_SIZE_ESC 15 |
50 | | |
51 | | /** |
52 | | * Decode Indeo5 GOP (Group of pictures) header. |
53 | | * This header is present in key frames only. |
54 | | * It defines parameters for all frames in a GOP. |
55 | | * |
56 | | * @param[in,out] ctx ptr to the decoder context |
57 | | * @param[in] avctx ptr to the AVCodecContext |
58 | | * @return result code: 0 = OK, -1 = error |
59 | | */ |
60 | | static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx) |
61 | 39.4k | { |
62 | 39.4k | int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable; |
63 | 39.4k | int quant_mat, blk_size_changed = 0; |
64 | 39.4k | IVIBandDesc *band, *band1, *band2; |
65 | 39.4k | IVIPicConfig pic_conf; |
66 | | |
67 | 39.4k | ctx->gop_flags = get_bits(&ctx->gb, 8); |
68 | | |
69 | 39.4k | ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0; |
70 | | |
71 | 39.4k | if (ctx->gop_flags & IVI5_IS_PROTECTED) |
72 | 2.54k | ctx->lock_word = get_bits_long(&ctx->gb, 32); |
73 | | |
74 | 39.4k | tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0; |
75 | 39.4k | if (tile_size > 256) { |
76 | 380 | av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size); |
77 | 380 | return AVERROR_INVALIDDATA; |
78 | 380 | } |
79 | | |
80 | | /* decode number of wavelet bands */ |
81 | | /* num_levels * 3 + 1 */ |
82 | 39.1k | pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1; |
83 | 39.1k | pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1; |
84 | 39.1k | is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; |
85 | 39.1k | if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { |
86 | 2.23k | av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", |
87 | 2.23k | pic_conf.luma_bands, pic_conf.chroma_bands); |
88 | 2.23k | return AVERROR_INVALIDDATA; |
89 | 2.23k | } |
90 | | |
91 | 36.8k | pic_size_indx = get_bits(&ctx->gb, 4); |
92 | 36.8k | if (pic_size_indx == IVI5_PIC_SIZE_ESC) { |
93 | 5.18k | pic_conf.pic_height = get_bits(&ctx->gb, 13); |
94 | 5.18k | pic_conf.pic_width = get_bits(&ctx->gb, 13); |
95 | 31.6k | } else { |
96 | 31.6k | pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2; |
97 | 31.6k | pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2; |
98 | 31.6k | } |
99 | | |
100 | 36.8k | if (ctx->gop_flags & 2) { |
101 | 1.79k | avpriv_report_missing_feature(avctx, "YV12 picture format"); |
102 | 1.79k | return AVERROR_PATCHWELCOME; |
103 | 1.79k | } |
104 | | |
105 | 35.0k | pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; |
106 | 35.0k | pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; |
107 | | |
108 | 35.0k | if (!tile_size) { |
109 | 33.9k | pic_conf.tile_height = pic_conf.pic_height; |
110 | 33.9k | pic_conf.tile_width = pic_conf.pic_width; |
111 | 33.9k | } else { |
112 | 1.15k | pic_conf.tile_height = pic_conf.tile_width = tile_size; |
113 | 1.15k | } |
114 | | |
115 | | /* check if picture layout was changed and reallocate buffers */ |
116 | 35.0k | if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) { |
117 | 17.5k | result = ff_ivi_init_planes(avctx, ctx->planes, &pic_conf, 0); |
118 | 17.5k | if (result < 0) { |
119 | 1.54k | av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); |
120 | 1.54k | return result; |
121 | 1.54k | } |
122 | 15.9k | ctx->pic_conf = pic_conf; |
123 | 15.9k | ctx->is_scalable = is_scalable; |
124 | 15.9k | blk_size_changed = 1; /* force reallocation of the internal structures */ |
125 | 15.9k | } |
126 | | |
127 | 90.0k | for (p = 0; p <= 1; p++) { |
128 | 175k | for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { |
129 | 118k | band = &ctx->planes[p].bands[i]; |
130 | | |
131 | 118k | band->is_halfpel = get_bits1(&ctx->gb); |
132 | | |
133 | 118k | mb_size = get_bits1(&ctx->gb); |
134 | 118k | blk_size = 8 >> get_bits1(&ctx->gb); |
135 | 118k | mb_size = blk_size << !mb_size; |
136 | | |
137 | 118k | if (p==0 && blk_size==4) { |
138 | 1.46k | av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n"); |
139 | 1.46k | return AVERROR_PATCHWELCOME; |
140 | 1.46k | } |
141 | | |
142 | 117k | blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size; |
143 | 117k | if (blk_size_changed) { |
144 | 52.8k | band->mb_size = mb_size; |
145 | 52.8k | band->blk_size = blk_size; |
146 | 52.8k | } |
147 | | |
148 | 117k | if (get_bits1(&ctx->gb)) { |
149 | 896 | avpriv_report_missing_feature(avctx, "Extended transform info"); |
150 | 896 | return AVERROR_PATCHWELCOME; |
151 | 896 | } |
152 | | |
153 | | /* select transform function and scan pattern according to plane and band number */ |
154 | 116k | switch ((p << 2) + i) { |
155 | 32.1k | case 0: |
156 | 32.1k | band->inv_transform = ff_ivi_inverse_slant_8x8; |
157 | 32.1k | band->dc_transform = ff_ivi_dc_slant_2d; |
158 | 32.1k | band->scan = ff_zigzag_direct; |
159 | 32.1k | band->transform_size = 8; |
160 | 32.1k | break; |
161 | | |
162 | 18.1k | case 1: |
163 | 18.1k | band->inv_transform = ff_ivi_row_slant8; |
164 | 18.1k | band->dc_transform = ff_ivi_dc_row_slant; |
165 | 18.1k | band->scan = ff_ivi_vertical_scan_8x8; |
166 | 18.1k | band->transform_size = 8; |
167 | 18.1k | break; |
168 | | |
169 | 17.9k | case 2: |
170 | 17.9k | band->inv_transform = ff_ivi_col_slant8; |
171 | 17.9k | band->dc_transform = ff_ivi_dc_col_slant; |
172 | 17.9k | band->scan = ff_ivi_horizontal_scan_8x8; |
173 | 17.9k | band->transform_size = 8; |
174 | 17.9k | break; |
175 | | |
176 | 17.6k | case 3: |
177 | 17.6k | band->inv_transform = ff_ivi_put_pixels_8x8; |
178 | 17.6k | band->dc_transform = ff_ivi_put_dc_pixel_8x8; |
179 | 17.6k | band->scan = ff_ivi_horizontal_scan_8x8; |
180 | 17.6k | band->transform_size = 8; |
181 | 17.6k | break; |
182 | | |
183 | 30.3k | case 4: |
184 | 30.3k | band->inv_transform = ff_ivi_inverse_slant_4x4; |
185 | 30.3k | band->dc_transform = ff_ivi_dc_slant_2d; |
186 | 30.3k | band->scan = ff_ivi_direct_scan_4x4; |
187 | 30.3k | band->transform_size = 4; |
188 | 30.3k | break; |
189 | 116k | } |
190 | | |
191 | 116k | band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 || |
192 | 84.1k | band->inv_transform == ff_ivi_inverse_slant_4x4; |
193 | | |
194 | 116k | if (band->transform_size != band->blk_size) { |
195 | 4.70k | av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size); |
196 | 4.70k | return AVERROR_INVALIDDATA; |
197 | 4.70k | } |
198 | | |
199 | | /* select dequant matrix according to plane and band number */ |
200 | 111k | if (!p) { |
201 | 85.8k | quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0; |
202 | 85.8k | } else { |
203 | 25.6k | quant_mat = 5; |
204 | 25.6k | } |
205 | | |
206 | 111k | if (band->blk_size == 8) { |
207 | 85.8k | if(quant_mat >= 5){ |
208 | 0 | av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat); |
209 | 0 | return -1; |
210 | 0 | } |
211 | 85.8k | band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0]; |
212 | 85.8k | band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0]; |
213 | 85.8k | band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0]; |
214 | 85.8k | band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0]; |
215 | 85.8k | } else { |
216 | 25.6k | band->intra_base = ivi5_base_quant_4x4_intra; |
217 | 25.6k | band->inter_base = ivi5_base_quant_4x4_inter; |
218 | 25.6k | band->intra_scale = ivi5_scale_quant_4x4_intra; |
219 | 25.6k | band->inter_scale = ivi5_scale_quant_4x4_inter; |
220 | 25.6k | } |
221 | | |
222 | 111k | if (get_bits(&ctx->gb, 2)) { |
223 | 854 | av_log(avctx, AV_LOG_ERROR, "End marker missing!\n"); |
224 | 854 | return AVERROR_INVALIDDATA; |
225 | 854 | } |
226 | 111k | } |
227 | 64.3k | } |
228 | | |
229 | | /* copy chroma parameters into the 2nd chroma plane */ |
230 | 51.2k | for (i = 0; i < pic_conf.chroma_bands; i++) { |
231 | 25.6k | band1 = &ctx->planes[1].bands[i]; |
232 | 25.6k | band2 = &ctx->planes[2].bands[i]; |
233 | | |
234 | 25.6k | band2->width = band1->width; |
235 | 25.6k | band2->height = band1->height; |
236 | 25.6k | band2->mb_size = band1->mb_size; |
237 | 25.6k | band2->blk_size = band1->blk_size; |
238 | 25.6k | band2->is_halfpel = band1->is_halfpel; |
239 | 25.6k | band2->intra_base = band1->intra_base; |
240 | 25.6k | band2->inter_base = band1->inter_base; |
241 | 25.6k | band2->intra_scale = band1->intra_scale; |
242 | 25.6k | band2->inter_scale = band1->inter_scale; |
243 | 25.6k | band2->scan = band1->scan; |
244 | 25.6k | band2->inv_transform = band1->inv_transform; |
245 | 25.6k | band2->dc_transform = band1->dc_transform; |
246 | 25.6k | band2->is_2d_trans = band1->is_2d_trans; |
247 | 25.6k | band2->transform_size= band1->transform_size; |
248 | 25.6k | } |
249 | | |
250 | | /* reallocate internal structures if needed */ |
251 | 25.6k | if (blk_size_changed) { |
252 | 8.85k | result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width, |
253 | 8.85k | pic_conf.tile_height); |
254 | 8.85k | if (result < 0) { |
255 | 1.37k | av_log(avctx, AV_LOG_ERROR, |
256 | 1.37k | "Couldn't reallocate internal structures!\n"); |
257 | 1.37k | return result; |
258 | 1.37k | } |
259 | 8.85k | } |
260 | | |
261 | 24.2k | if (ctx->gop_flags & 8) { |
262 | 3.26k | if (get_bits(&ctx->gb, 3)) { |
263 | 357 | av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n"); |
264 | 357 | return AVERROR_INVALIDDATA; |
265 | 357 | } |
266 | | |
267 | 2.91k | if (get_bits1(&ctx->gb)) |
268 | 1.62k | skip_bits(&ctx->gb, 24); /* skip transparency fill color */ |
269 | 2.91k | } |
270 | | |
271 | 23.8k | align_get_bits(&ctx->gb); |
272 | | |
273 | 23.8k | skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */ |
274 | | |
275 | | /* skip GOP extension if any */ |
276 | 23.8k | if (get_bits1(&ctx->gb)) { |
277 | 5.51k | do { |
278 | 5.51k | i = get_bits(&ctx->gb, 16); |
279 | 5.51k | } while (i & 0x8000); |
280 | 3.78k | } |
281 | | |
282 | 23.8k | align_get_bits(&ctx->gb); |
283 | | |
284 | 23.8k | return 0; |
285 | 24.2k | } |
286 | | |
287 | | |
288 | | /** |
289 | | * Skip a header extension. |
290 | | * |
291 | | * @param[in,out] gb the GetBit context |
292 | | */ |
293 | | static inline int skip_hdr_extension(GetBitContext *gb) |
294 | 18.5k | { |
295 | 18.5k | int i, len; |
296 | | |
297 | 1.44M | do { |
298 | 1.44M | len = get_bits(gb, 8); |
299 | 1.44M | if (8*len > get_bits_left(gb)) |
300 | 15.3k | return AVERROR_INVALIDDATA; |
301 | 8.58M | for (i = 0; i < len; i++) skip_bits(gb, 8); |
302 | 1.42M | } while(len); |
303 | | |
304 | 3.21k | return 0; |
305 | 18.5k | } |
306 | | |
307 | | |
308 | | /** |
309 | | * Decode Indeo5 picture header. |
310 | | * |
311 | | * @param[in,out] ctx ptr to the decoder context |
312 | | * @param[in] avctx ptr to the AVCodecContext |
313 | | * @return result code: 0 = OK, -1 = error |
314 | | */ |
315 | | static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx) |
316 | 168k | { |
317 | 168k | int ret; |
318 | | |
319 | 168k | if (get_bits(&ctx->gb, 5) != 0x1F) { |
320 | 5.79k | av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); |
321 | 5.79k | return AVERROR_INVALIDDATA; |
322 | 5.79k | } |
323 | | |
324 | 162k | ctx->prev_frame_type = ctx->frame_type; |
325 | 162k | ctx->frame_type = get_bits(&ctx->gb, 3); |
326 | 162k | if (ctx->frame_type >= 5) { |
327 | 715 | av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type); |
328 | 715 | ctx->frame_type = FRAMETYPE_INTRA; |
329 | 715 | return AVERROR_INVALIDDATA; |
330 | 715 | } |
331 | | |
332 | 162k | ctx->frame_num = get_bits(&ctx->gb, 8); |
333 | | |
334 | 162k | if (ctx->frame_type == FRAMETYPE_INTRA) { |
335 | 39.4k | if ((ret = decode_gop_header(ctx, avctx)) < 0) { |
336 | 15.5k | av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n"); |
337 | 15.5k | ctx->gop_invalid = 1; |
338 | 15.5k | return ret; |
339 | 15.5k | } |
340 | 23.8k | ctx->gop_invalid = 0; |
341 | 23.8k | } |
342 | | |
343 | 146k | if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) { |
344 | 427 | av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scalable stream\n"); |
345 | 427 | ctx->frame_type = FRAMETYPE_INTER; |
346 | 427 | return AVERROR_INVALIDDATA; |
347 | 427 | } |
348 | | |
349 | 146k | if (ctx->frame_type != FRAMETYPE_NULL) { |
350 | 49.7k | ctx->frame_flags = get_bits(&ctx->gb, 8); |
351 | | |
352 | 49.7k | ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits(&ctx->gb, 24) : 0; |
353 | | |
354 | 49.7k | ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0; |
355 | | |
356 | | /* skip unknown extension if any */ |
357 | 49.7k | if (ctx->frame_flags & 0x20) |
358 | 13.7k | skip_hdr_extension(&ctx->gb); /* XXX: untested */ |
359 | | |
360 | | /* decode macroblock huffman codebook */ |
361 | 49.7k | ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, |
362 | 49.7k | IVI_MB_HUFF, &ctx->mb_vlc, avctx); |
363 | 49.7k | if (ret < 0) |
364 | 1.28k | return ret; |
365 | | |
366 | 48.4k | skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */ |
367 | 48.4k | } |
368 | | |
369 | 144k | align_get_bits(&ctx->gb); |
370 | | |
371 | 144k | return 0; |
372 | 146k | } |
373 | | |
374 | | |
375 | | /** |
376 | | * Decode Indeo5 band header. |
377 | | * |
378 | | * @param[in,out] ctx ptr to the decoder context |
379 | | * @param[in,out] band ptr to the band descriptor |
380 | | * @param[in] avctx ptr to the AVCodecContext |
381 | | * @return result code: 0 = OK, -1 = error |
382 | | */ |
383 | | static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band, |
384 | | AVCodecContext *avctx) |
385 | 71.7k | { |
386 | 71.7k | int i, ret; |
387 | 71.7k | uint8_t band_flags; |
388 | | |
389 | 71.7k | band_flags = get_bits(&ctx->gb, 8); |
390 | | |
391 | 71.7k | if (band_flags & 1) { |
392 | 1.99k | band->is_empty = 1; |
393 | 1.99k | return 0; |
394 | 1.99k | } |
395 | | |
396 | 69.7k | band->data_size = (ctx->frame_flags & 0x80) ? get_bits(&ctx->gb, 24) : 0; |
397 | | |
398 | 69.7k | band->inherit_mv = band_flags & 2; |
399 | 69.7k | band->inherit_qdelta = band_flags & 8; |
400 | 69.7k | band->qdelta_present = band_flags & 4; |
401 | 69.7k | if (!band->qdelta_present) band->inherit_qdelta = 1; |
402 | | |
403 | | /* decode rvmap probability corrections if any */ |
404 | 69.7k | band->num_corr = 0; /* there are no corrections */ |
405 | 69.7k | if (band_flags & 0x10) { |
406 | 5.83k | band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ |
407 | 5.83k | if (band->num_corr > 61) { |
408 | 1.07k | av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", |
409 | 1.07k | band->num_corr); |
410 | 1.07k | return AVERROR_INVALIDDATA; |
411 | 1.07k | } |
412 | | |
413 | | /* read correction pairs */ |
414 | 129k | for (i = 0; i < band->num_corr * 2; i++) |
415 | 124k | band->corr[i] = get_bits(&ctx->gb, 8); |
416 | 4.76k | } |
417 | | |
418 | | /* select appropriate rvmap table for this band */ |
419 | 68.7k | band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8; |
420 | | |
421 | | /* decode block huffman codebook */ |
422 | 68.7k | ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, |
423 | 68.7k | &band->blk_vlc, avctx); |
424 | 68.7k | if (ret < 0) |
425 | 250 | return ret; |
426 | | |
427 | 68.4k | band->checksum_present = get_bits1(&ctx->gb); |
428 | 68.4k | if (band->checksum_present) |
429 | 13.4k | band->checksum = get_bits(&ctx->gb, 16); |
430 | | |
431 | 68.4k | band->glob_quant = get_bits(&ctx->gb, 5); |
432 | | |
433 | | /* skip unknown extension if any */ |
434 | 68.4k | if (band_flags & 0x20) { /* XXX: untested */ |
435 | 4.77k | align_get_bits(&ctx->gb); |
436 | 4.77k | skip_hdr_extension(&ctx->gb); |
437 | 4.77k | } |
438 | | |
439 | 68.4k | align_get_bits(&ctx->gb); |
440 | | |
441 | 68.4k | return 0; |
442 | 68.7k | } |
443 | | |
444 | | |
445 | | /** |
446 | | * Decode info (block type, cbp, quant delta, motion vector) |
447 | | * for all macroblocks in the current tile. |
448 | | * |
449 | | * @param[in,out] ctx ptr to the decoder context |
450 | | * @param[in,out] band ptr to the band descriptor |
451 | | * @param[in,out] tile ptr to the tile descriptor |
452 | | * @param[in] avctx ptr to the AVCodecContext |
453 | | * @return result code: 0 = OK, -1 = error |
454 | | */ |
455 | | static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band, |
456 | | IVITile *tile, AVCodecContext *avctx) |
457 | 14.5k | { |
458 | 14.5k | int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, |
459 | 14.5k | mv_scale, blks_per_mb, s; |
460 | 14.5k | IVIMbInfo *mb, *ref_mb; |
461 | 14.5k | int row_offset = band->mb_size * band->pitch; |
462 | | |
463 | 14.5k | mb = tile->mbs; |
464 | 14.5k | ref_mb = tile->ref_mbs; |
465 | 14.5k | offs = tile->ypos * band->pitch + tile->xpos; |
466 | | |
467 | 14.5k | if (!ref_mb && |
468 | 5.84k | ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv)) |
469 | 973 | return AVERROR_INVALIDDATA; |
470 | | |
471 | 13.6k | if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) { |
472 | 0 | av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n", |
473 | 0 | tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)); |
474 | 0 | return AVERROR_INVALIDDATA; |
475 | 0 | } |
476 | | |
477 | | /* scale factor for motion vectors */ |
478 | 13.6k | mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); |
479 | 13.6k | mv_x = mv_y = 0; |
480 | | |
481 | 175k | for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) { |
482 | 163k | mb_offset = offs; |
483 | | |
484 | 8.21M | for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) { |
485 | 8.05M | mb->xpos = x; |
486 | 8.05M | mb->ypos = y; |
487 | 8.05M | mb->buf_offs = mb_offset; |
488 | | |
489 | 8.05M | if (get_bits1(&ctx->gb)) { |
490 | 762k | if (ctx->frame_type == FRAMETYPE_INTRA) { |
491 | 939 | av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); |
492 | 939 | return AVERROR_INVALIDDATA; |
493 | 939 | } |
494 | 761k | mb->type = 1; /* empty macroblocks are always INTER */ |
495 | 761k | mb->cbp = 0; /* all blocks are empty */ |
496 | | |
497 | 761k | mb->q_delta = 0; |
498 | 761k | if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) { |
499 | 596k | mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
500 | 596k | IVI_VLC_BITS, 1); |
501 | 596k | mb->q_delta = IVI_TOSIGNED(mb->q_delta); |
502 | 596k | } |
503 | | |
504 | 761k | mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ |
505 | 761k | if (band->inherit_mv && ref_mb){ |
506 | | /* motion vector inheritance */ |
507 | 59.8k | if (mv_scale) { |
508 | 51.6k | mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); |
509 | 51.6k | mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); |
510 | 51.6k | } else { |
511 | 8.28k | mb->mv_x = ref_mb->mv_x; |
512 | 8.28k | mb->mv_y = ref_mb->mv_y; |
513 | 8.28k | } |
514 | 59.8k | } |
515 | 7.29M | } else { |
516 | 7.29M | if (band->inherit_mv && ref_mb) { |
517 | 1.79M | mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ |
518 | 5.49M | } else if (ctx->frame_type == FRAMETYPE_INTRA) { |
519 | 70.8k | mb->type = 0; /* mb_type is always INTRA for intra-frames */ |
520 | 5.42M | } else { |
521 | 5.42M | mb->type = get_bits1(&ctx->gb); |
522 | 5.42M | } |
523 | | |
524 | 7.29M | blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; |
525 | 7.29M | mb->cbp = get_bits(&ctx->gb, blks_per_mb); |
526 | | |
527 | 7.29M | mb->q_delta = 0; |
528 | 7.29M | if (band->qdelta_present) { |
529 | 4.31M | if (band->inherit_qdelta) { |
530 | 5.41k | if (ref_mb) mb->q_delta = ref_mb->q_delta; |
531 | 4.30M | } else if (mb->cbp || (!band->plane && !band->band_num && |
532 | 2.40M | (ctx->frame_flags & 8))) { |
533 | 1.11M | mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
534 | 1.11M | IVI_VLC_BITS, 1); |
535 | 1.11M | mb->q_delta = IVI_TOSIGNED(mb->q_delta); |
536 | 1.11M | } |
537 | 4.31M | } |
538 | | |
539 | 7.29M | if (!mb->type) { |
540 | 5.04M | mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ |
541 | 5.04M | } else { |
542 | 2.24M | if (band->inherit_mv && ref_mb){ |
543 | | /* motion vector inheritance */ |
544 | 1.79M | if (mv_scale) { |
545 | 1.77M | mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); |
546 | 1.77M | mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); |
547 | 1.77M | } else { |
548 | 15.1k | mb->mv_x = ref_mb->mv_x; |
549 | 15.1k | mb->mv_y = ref_mb->mv_y; |
550 | 15.1k | } |
551 | 1.79M | } else { |
552 | | /* decode motion vector deltas */ |
553 | 448k | mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
554 | 448k | IVI_VLC_BITS, 1); |
555 | 448k | mv_y += IVI_TOSIGNED(mv_delta); |
556 | 448k | mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, |
557 | 448k | IVI_VLC_BITS, 1); |
558 | 448k | mv_x += IVI_TOSIGNED(mv_delta); |
559 | 448k | mb->mv_x = mv_x; |
560 | 448k | mb->mv_y = mv_y; |
561 | 448k | } |
562 | 2.24M | } |
563 | 7.29M | } |
564 | | |
565 | 8.05M | s= band->is_halfpel; |
566 | 8.05M | if (mb->type) |
567 | 3.00M | if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 || |
568 | 3.00M | x + ((mb->mv_x+s)>>s) + band->mb_size - 1 |
569 | 3.00M | + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) { |
570 | 806 | av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y); |
571 | 806 | return AVERROR_INVALIDDATA; |
572 | 806 | } |
573 | | |
574 | 8.05M | mb++; |
575 | 8.05M | if (ref_mb) |
576 | 2.00M | ref_mb++; |
577 | 8.05M | mb_offset += band->mb_size; |
578 | 8.05M | } |
579 | | |
580 | 162k | offs += row_offset; |
581 | 162k | } |
582 | | |
583 | 11.8k | align_get_bits(&ctx->gb); |
584 | | |
585 | 11.8k | return 0; |
586 | 13.6k | } |
587 | | |
588 | | |
589 | | /** |
590 | | * Switch buffers. |
591 | | * |
592 | | * @param[in,out] ctx ptr to the decoder context |
593 | | */ |
594 | | static void switch_buffers(IVI45DecContext *ctx) |
595 | 136k | { |
596 | 136k | switch (ctx->prev_frame_type) { |
597 | 23.9k | case FRAMETYPE_INTRA: |
598 | 27.4k | case FRAMETYPE_INTER: |
599 | 27.4k | ctx->buf_switch ^= 1; |
600 | 27.4k | ctx->dst_buf = ctx->buf_switch; |
601 | 27.4k | ctx->ref_buf = ctx->buf_switch ^ 1; |
602 | 27.4k | break; |
603 | 2.57k | case FRAMETYPE_INTER_SCAL: |
604 | 2.57k | if (!ctx->inter_scal) { |
605 | 742 | ctx->ref2_buf = 2; |
606 | 742 | ctx->inter_scal = 1; |
607 | 742 | } |
608 | 2.57k | FFSWAP(int, ctx->dst_buf, ctx->ref2_buf); |
609 | 2.57k | ctx->ref_buf = ctx->ref2_buf; |
610 | 2.57k | break; |
611 | 10.7k | case FRAMETYPE_INTER_NOREF: |
612 | 10.7k | break; |
613 | 136k | } |
614 | | |
615 | 136k | switch (ctx->frame_type) { |
616 | 23.2k | case FRAMETYPE_INTRA: |
617 | 23.2k | ctx->buf_switch = 0; |
618 | | /* FALLTHROUGH */ |
619 | 26.9k | case FRAMETYPE_INTER: |
620 | 26.9k | ctx->inter_scal = 0; |
621 | 26.9k | ctx->dst_buf = ctx->buf_switch; |
622 | 26.9k | ctx->ref_buf = ctx->buf_switch ^ 1; |
623 | 26.9k | break; |
624 | 2.74k | case FRAMETYPE_INTER_SCAL: |
625 | 13.6k | case FRAMETYPE_INTER_NOREF: |
626 | 109k | case FRAMETYPE_NULL: |
627 | 109k | break; |
628 | 136k | } |
629 | 136k | } |
630 | | |
631 | | |
632 | | static int is_nonnull_frame(IVI45DecContext *ctx) |
633 | 199k | { |
634 | 199k | return ctx->frame_type != FRAMETYPE_NULL; |
635 | 199k | } |
636 | | |
637 | | |
638 | | /** |
639 | | * Initialize Indeo5 decoder. |
640 | | */ |
641 | | static av_cold int decode_init(AVCodecContext *avctx) |
642 | 2.01k | { |
643 | 2.01k | IVI45DecContext *ctx = avctx->priv_data; |
644 | 2.01k | int result; |
645 | | |
646 | 2.01k | ctx->gop_invalid = 1; |
647 | | |
648 | 2.01k | ff_ivi_init_static_vlc(); |
649 | | |
650 | | /* copy rvmap tables in our context so we can apply changes to them */ |
651 | 2.01k | memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); |
652 | | |
653 | | /* set the initial picture layout according to the basic profile: |
654 | | there is only one band per plane (no scalability), only one tile (no local decoding) |
655 | | and picture format = YVU9 */ |
656 | 2.01k | ctx->pic_conf.pic_width = avctx->width; |
657 | 2.01k | ctx->pic_conf.pic_height = avctx->height; |
658 | 2.01k | ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2; |
659 | 2.01k | ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2; |
660 | 2.01k | ctx->pic_conf.tile_width = avctx->width; |
661 | 2.01k | ctx->pic_conf.tile_height = avctx->height; |
662 | 2.01k | ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1; |
663 | | |
664 | 2.01k | result = ff_ivi_init_planes(avctx, ctx->planes, &ctx->pic_conf, 0); |
665 | 2.01k | if (result) { |
666 | 127 | av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n"); |
667 | 127 | return AVERROR_INVALIDDATA; |
668 | 127 | } |
669 | | |
670 | 1.88k | ctx->buf_switch = 0; |
671 | 1.88k | ctx->inter_scal = 0; |
672 | | |
673 | 1.88k | ctx->decode_pic_hdr = decode_pic_hdr; |
674 | 1.88k | ctx->decode_band_hdr = decode_band_hdr; |
675 | 1.88k | ctx->decode_mb_info = decode_mb_info; |
676 | 1.88k | ctx->switch_buffers = switch_buffers; |
677 | 1.88k | ctx->is_nonnull_frame = is_nonnull_frame; |
678 | | |
679 | 1.88k | ctx->is_indeo4 = 0; |
680 | | |
681 | 1.88k | avctx->pix_fmt = AV_PIX_FMT_YUV410P; |
682 | | |
683 | 1.88k | return 0; |
684 | 2.01k | } |
685 | | |
686 | | const FFCodec ff_indeo5_decoder = { |
687 | | .p.name = "indeo5", |
688 | | CODEC_LONG_NAME("Intel Indeo Video Interactive 5"), |
689 | | .p.type = AVMEDIA_TYPE_VIDEO, |
690 | | .p.id = AV_CODEC_ID_INDEO5, |
691 | | .priv_data_size = sizeof(IVI45DecContext), |
692 | | .init = decode_init, |
693 | | .close = ff_ivi_decode_close, |
694 | | FF_CODEC_DECODE_CB(ff_ivi_decode_frame), |
695 | | .p.capabilities = AV_CODEC_CAP_DR1, |
696 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
697 | | }; |