vlc: Add header #include when the types are used
[libav.git] / libavcodec / indeo4.c
CommitLineData
adfe0c94
KS
1/*
2 * Indeo Video Interactive v4 compatible decoder
3 * Copyright (c) 2009-2011 Maxim Poliakovski
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 4 decoder
25 *
26 * Indeo 4 data is usually transported within .avi or .mov files.
27 * Known FOURCCs: 'IV41'
28 */
29
30#define BITSTREAM_READER_LE
31#include "avcodec.h"
c3defda0 32#include "bitstream.h"
adfe0c94 33#include "indeo4data.h"
6a85dfc8 34#include "internal.h"
b6686629
DB
35#include "ivi.h"
36#include "ivi_dsp.h"
73fc82f3 37#include "vlc.h"
adfe0c94 38
adfe0c94
KS
39#define IVI4_PIC_SIZE_ESC 7
40
41
08bab32c 42static const struct {
adfe0c94
KS
43 InvTransformPtr *inv_trans;
44 DCTransformPtr *dc_trans;
45 int is_2d_trans;
46} transforms[18] = {
47 { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 },
6647aa04
KS
48 { ff_ivi_row_haar8, ff_ivi_dc_haar_2d, 0 },
49 { ff_ivi_col_haar8, ff_ivi_dc_haar_2d, 0 },
adfe0c94
KS
50 { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 },
51 { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 },
52 { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 },
53 { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 },
54 { NULL, NULL, 0 }, /* inverse DCT 8x8 */
55 { NULL, NULL, 0 }, /* inverse DCT 8x1 */
56 { NULL, NULL, 0 }, /* inverse DCT 1x8 */
6647aa04 57 { ff_ivi_inverse_haar_4x4, ff_ivi_dc_haar_2d, 1 },
adfe0c94
KS
58 { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 },
59 { NULL, NULL, 0 }, /* no transform 4x4 */
6647aa04
KS
60 { ff_ivi_row_haar4, ff_ivi_dc_haar_2d, 0 },
61 { ff_ivi_col_haar4, ff_ivi_dc_haar_2d, 0 },
62 { ff_ivi_row_slant4, ff_ivi_dc_row_slant, 0 },
63 { ff_ivi_col_slant4, ff_ivi_dc_col_slant, 0 },
adfe0c94
KS
64 { NULL, NULL, 0 }, /* inverse DCT 4x4 */
65};
66
67/**
68 * Decode subdivision of a plane.
69 * This is a simplified version that checks for two supported subdivisions:
70 * - 1 wavelet band per plane, size factor 1:1, code pattern: 3
71 * - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
72 * Anything else is either unsupported or corrupt.
73 *
c3defda0 74 * @param[in,out] bc the Bitstream context
adfe0c94
KS
75 * @return number of wavelet bands or 0 on error
76 */
c3defda0 77static int decode_plane_subdivision(BitstreamContext *bc)
adfe0c94
KS
78{
79 int i;
80
c3defda0 81 switch (bitstream_read(bc, 2)) {
adfe0c94
KS
82 case 3:
83 return 1;
84 case 2:
85 for (i = 0; i < 4; i++)
c3defda0 86 if (bitstream_read(bc, 2) != 3)
adfe0c94
KS
87 return 0;
88 return 4;
89 default:
90 return 0;
91 }
92}
93
94static inline int scale_tile_size(int def_size, int size_factor)
95{
d4b63054 96 return size_factor == 15 ? def_size : (size_factor + 1) << 5;
adfe0c94
KS
97}
98
99/**
100 * Decode Indeo 4 picture header.
101 *
102 * @param[in,out] ctx pointer to the decoder context
103 * @param[in] avctx pointer to the AVCodecContext
104 * @return result code: 0 = OK, negative number = error
105 */
aa372cf4 106static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
adfe0c94 107{
1fbd3d30 108 int pic_size_indx, i, p;
adfe0c94
KS
109 IVIPicConfig pic_conf;
110
c3defda0 111 if (bitstream_read(&ctx->bc, 18) != 0x3FFF8) {
adfe0c94
KS
112 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
113 return AVERROR_INVALIDDATA;
114 }
115
116 ctx->prev_frame_type = ctx->frame_type;
c3defda0 117 ctx->frame_type = bitstream_read(&ctx->bc, 3);
adfe0c94
KS
118 if (ctx->frame_type == 7) {
119 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
120 return AVERROR_INVALIDDATA;
121 }
122
3df0d200 123 if (ctx->frame_type == IVI4_FRAMETYPE_BIDIR)
adfe0c94 124 ctx->has_b_frames = 1;
adfe0c94 125
c3defda0 126 ctx->has_transp = bitstream_read_bit(&ctx->bc);
adfe0c94
KS
127
128 /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
c3defda0 129 if (bitstream_read_bit(&ctx->bc)) {
adfe0c94
KS
130 av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
131 return AVERROR_INVALIDDATA;
132 }
133
c3defda0 134 ctx->data_size = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 24) : 0;
adfe0c94
KS
135
136 /* null frames don't contain anything else so we just return */
3df0d200 137 if (ctx->frame_type >= IVI4_FRAMETYPE_NULL_FIRST) {
6a85dfc8 138 ff_dlog(avctx, "Null frame encountered!\n");
adfe0c94
KS
139 return 0;
140 }
141
142 /* Check key lock status. If enabled - ignore lock word. */
143 /* Usually we have to prompt the user for the password, but */
144 /* we don't do that because Indeo 4 videos can be decoded anyway */
c3defda0
AH
145 if (bitstream_read_bit(&ctx->bc)) {
146 bitstream_skip(&ctx->bc, 32);
6a85dfc8 147 ff_dlog(avctx, "Password-protected clip!\n");
adfe0c94
KS
148 }
149
c3defda0 150 pic_size_indx = bitstream_read(&ctx->bc, 3);
adfe0c94 151 if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
c3defda0
AH
152 pic_conf.pic_height = bitstream_read(&ctx->bc, 16);
153 pic_conf.pic_width = bitstream_read(&ctx->bc, 16);
adfe0c94
KS
154 } else {
155 pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
156 pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
157 }
158
159 /* Decode tile dimensions. */
c3defda0 160 ctx->uses_tiling = bitstream_read_bit(&ctx->bc);
64250d94 161 if (ctx->uses_tiling) {
c3defda0
AH
162 pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, bitstream_read(&ctx->bc, 4));
163 pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, bitstream_read(&ctx->bc, 4));
adfe0c94
KS
164 } else {
165 pic_conf.tile_height = pic_conf.pic_height;
166 pic_conf.tile_width = pic_conf.pic_width;
167 }
168
169 /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
c3defda0 170 if (bitstream_read(&ctx->bc, 2)) {
adfe0c94
KS
171 av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
172 return AVERROR_INVALIDDATA;
173 }
174 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
175 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
176
177 /* decode subdivision of the planes */
c3defda0 178 pic_conf.luma_bands = decode_plane_subdivision(&ctx->bc);
adfe0c94 179 if (pic_conf.luma_bands)
c3defda0 180 pic_conf.chroma_bands = decode_plane_subdivision(&ctx->bc);
adfe0c94
KS
181 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
182 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
183 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
184 pic_conf.luma_bands, pic_conf.chroma_bands);
185 return AVERROR_INVALIDDATA;
186 }
187
188 /* check if picture layout was changed and reallocate buffers */
189 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
5ec6d152 190 if (ff_ivi_init_planes(ctx->planes, &pic_conf, 1)) {
adfe0c94 191 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
28dda8a6 192 ctx->pic_conf.luma_bands = 0;
adfe0c94
KS
193 return AVERROR(ENOMEM);
194 }
195
196 ctx->pic_conf = pic_conf;
197
198 /* set default macroblock/block dimensions */
199 for (p = 0; p <= 2; p++) {
200 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
201 ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
202 ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
203 }
204 }
205
206 if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
207 ctx->pic_conf.tile_height)) {
208 av_log(avctx, AV_LOG_ERROR,
209 "Couldn't reallocate internal structures!\n");
210 return AVERROR(ENOMEM);
211 }
212 }
213
c3defda0 214 ctx->frame_num = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 20) : 0;
adfe0c94
KS
215
216 /* skip decTimeEst field if present */
c3defda0
AH
217 if (bitstream_read_bit(&ctx->bc))
218 bitstream_skip(&ctx->bc, 8);
adfe0c94
KS
219
220 /* decode macroblock and block huffman codebooks */
c3defda0
AH
221 if (ff_ivi_dec_huff_desc(&ctx->bc, bitstream_read_bit(&ctx->bc), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
222 ff_ivi_dec_huff_desc(&ctx->bc, bitstream_read_bit(&ctx->bc), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
adfe0c94
KS
223 return AVERROR_INVALIDDATA;
224
c3defda0 225 ctx->rvmap_sel = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 3) : 8;
adfe0c94 226
c3defda0
AH
227 ctx->in_imf = bitstream_read_bit(&ctx->bc);
228 ctx->in_q = bitstream_read_bit(&ctx->bc);
adfe0c94 229
c3defda0 230 ctx->pic_glob_quant = bitstream_read(&ctx->bc, 5);
adfe0c94
KS
231
232 /* TODO: ignore this parameter if unused */
c3defda0 233 ctx->unknown1 = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 3) : 0;
adfe0c94 234
c3defda0 235 ctx->checksum = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 16) : 0;
adfe0c94
KS
236
237 /* skip picture header extension if any */
c3defda0 238 while (bitstream_read_bit(&ctx->bc)) {
6a85dfc8 239 ff_dlog(avctx, "Pic hdr extension encountered!\n");
c3defda0 240 bitstream_skip(&ctx->bc, 8);
adfe0c94
KS
241 }
242
c3defda0 243 if (bitstream_read_bit(&ctx->bc)) {
adfe0c94
KS
244 av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
245 }
246
c3defda0 247 bitstream_align(&ctx->bc);
adfe0c94
KS
248
249 return 0;
250}
251
252
253/**
254 * Decode Indeo 4 band header.
255 *
256 * @param[in,out] ctx pointer to the decoder context
257 * @param[in,out] band pointer to the band descriptor
258 * @param[in] avctx pointer to the AVCodecContext
259 * @return result code: 0 = OK, negative number = error
260 */
aa372cf4 261static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
adfe0c94
KS
262 AVCodecContext *avctx)
263{
1fbd3d30 264 int plane, band_num, indx, transform_id, scan_indx;
adfe0c94
KS
265 int i;
266
c3defda0
AH
267 plane = bitstream_read(&ctx->bc, 2);
268 band_num = bitstream_read(&ctx->bc, 4);
adfe0c94
KS
269 if (band->plane != plane || band->band_num != band_num) {
270 av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
271 return AVERROR_INVALIDDATA;
272 }
273
c3defda0 274 band->is_empty = bitstream_read_bit(&ctx->bc);
adfe0c94 275 if (!band->is_empty) {
0cb83c56 276 int old_blk_size = band->blk_size;
1fbd3d30
KS
277 /* skip header size
278 * If header size is not given, header size is 4 bytes. */
c3defda0
AH
279 if (bitstream_read_bit(&ctx->bc))
280 bitstream_skip(&ctx->bc, 16);
adfe0c94 281
c3defda0 282 band->is_halfpel = bitstream_read(&ctx->bc, 2);
adfe0c94
KS
283 if (band->is_halfpel >= 2) {
284 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
285 band->is_halfpel);
286 return AVERROR_INVALIDDATA;
287 }
adfe0c94
KS
288 if (!band->is_halfpel)
289 ctx->uses_fullpel = 1;
adfe0c94 290
c3defda0 291 band->checksum_present = bitstream_read_bit(&ctx->bc);
adfe0c94 292 if (band->checksum_present)
c3defda0 293 band->checksum = bitstream_read(&ctx->bc, 16);
adfe0c94 294
c3defda0 295 indx = bitstream_read(&ctx->bc, 2);
adfe0c94
KS
296 if (indx == 3) {
297 av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
298 return AVERROR_INVALIDDATA;
299 }
300 band->mb_size = 16 >> indx;
301 band->blk_size = 8 >> (indx >> 1);
302
c3defda0
AH
303 band->inherit_mv = bitstream_read_bit(&ctx->bc);
304 band->inherit_qdelta = bitstream_read_bit(&ctx->bc);
adfe0c94 305
c3defda0 306 band->glob_quant = bitstream_read(&ctx->bc, 5);
adfe0c94 307
c3defda0
AH
308 if (!bitstream_read_bit(&ctx->bc) || ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
309 transform_id = bitstream_read(&ctx->bc, 5);
68fd077f
RB
310 if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
311 !transforms[transform_id].inv_trans) {
6d97484d 312 avpriv_request_sample(avctx, "Transform %d", transform_id);
adfe0c94
KS
313 return AVERROR_PATCHWELCOME;
314 }
315 if ((transform_id >= 7 && transform_id <= 9) ||
316 transform_id == 17) {
6d97484d 317 avpriv_request_sample(avctx, "DCT transform");
adfe0c94
KS
318 return AVERROR_PATCHWELCOME;
319 }
320
adfe0c94
KS
321 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
322 ctx->uses_haar = 1;
adfe0c94
KS
323
324 band->inv_transform = transforms[transform_id].inv_trans;
325 band->dc_transform = transforms[transform_id].dc_trans;
326 band->is_2d_trans = transforms[transform_id].is_2d_trans;
dc796851
LB
327 if (transform_id < 10)
328 band->transform_size = 8;
329 else
330 band->transform_size = 4;
331
332 if (band->blk_size != band->transform_size)
333 return AVERROR_INVALIDDATA;
adfe0c94 334
c3defda0 335 scan_indx = bitstream_read(&ctx->bc, 4);
adfe0c94
KS
336 if (scan_indx == 15) {
337 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
338 return AVERROR_INVALIDDATA;
339 }
cd78e934
LB
340 if (scan_indx > 4 && scan_indx < 10) {
341 if (band->blk_size != 4)
342 return AVERROR_INVALIDDATA;
343 } else if (band->blk_size != 8)
344 return AVERROR_INVALIDDATA;
345
adfe0c94
KS
346 band->scan = scan_index_to_tab[scan_indx];
347
c3defda0 348 band->quant_mat = bitstream_read(&ctx->bc, 5);
6255ccf7 349 if (band->quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
c9ef6b09
LB
350
351 if (band->quant_mat == 31)
352 av_log(avctx, AV_LOG_ERROR,
353 "Custom quant matrix encountered!\n");
354 else
355 avpriv_request_sample(avctx, "Quantization matrix %d",
356 band->quant_mat);
357 band->quant_mat = -1;
6255ccf7
LB
358 return AVERROR_INVALIDDATA;
359 }
0cb83c56
LB
360 } else {
361 if (old_blk_size != band->blk_size) {
362 av_log(avctx, AV_LOG_ERROR,
363 "The band block size does not match the configuration "
364 "inherited\n");
365 return AVERROR_INVALIDDATA;
366 }
c9ef6b09
LB
367 if (band->quant_mat < 0) {
368 av_log(avctx, AV_LOG_ERROR, "Invalid quant_mat inherited\n");
369 return AVERROR_INVALIDDATA;
370 }
adfe0c94
KS
371 }
372
373 /* decode block huffman codebook */
c3defda0 374 if (!bitstream_read_bit(&ctx->bc))
2cf5d291
KS
375 band->blk_vlc.tab = ctx->blk_vlc.tab;
376 else
c3defda0 377 if (ff_ivi_dec_huff_desc(&ctx->bc, 1, IVI_BLK_HUFF,
2cf5d291
KS
378 &band->blk_vlc, avctx))
379 return AVERROR_INVALIDDATA;
adfe0c94
KS
380
381 /* select appropriate rvmap table for this band */
c3defda0 382 band->rvmap_sel = bitstream_read_bit(&ctx->bc) ? bitstream_read(&ctx->bc, 3) : 8;
adfe0c94
KS
383
384 /* decode rvmap probability corrections if any */
385 band->num_corr = 0; /* there is no corrections */
c3defda0
AH
386 if (bitstream_read_bit(&ctx->bc)) {
387 band->num_corr = bitstream_read(&ctx->bc, 8); /* get number of correction pairs */
adfe0c94
KS
388 if (band->num_corr > 61) {
389 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
390 band->num_corr);
391 return AVERROR_INVALIDDATA;
392 }
393
394 /* read correction pairs */
395 for (i = 0; i < band->num_corr * 2; i++)
c3defda0 396 band->corr[i] = bitstream_read(&ctx->bc, 8);
adfe0c94
KS
397 }
398 }
399
400 if (band->blk_size == 8) {
401 band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
402 band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
403 } else {
404 band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
405 band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
406 }
407
408 /* Indeo 4 doesn't use scale tables */
409 band->intra_scale = NULL;
410 band->inter_scale = NULL;
411
c3defda0 412 bitstream_align(&ctx->bc);
adfe0c94
KS
413
414 return 0;
415}
416
417
418/**
419 * Decode information (block type, cbp, quant delta, motion vector)
420 * for all macroblocks in the current tile.
421 *
422 * @param[in,out] ctx pointer to the decoder context
423 * @param[in,out] band pointer to the band descriptor
424 * @param[in,out] tile pointer to the tile descriptor
425 * @param[in] avctx pointer to the AVCodecContext
426 * @return result code: 0 = OK, negative number = error
427 */
aa372cf4 428static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
adfe0c94
KS
429 IVITile *tile, AVCodecContext *avctx)
430{
431 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
432 mv_scale, mb_type_bits;
433 IVIMbInfo *mb, *ref_mb;
434 int row_offset = band->mb_size * band->pitch;
435
436 mb = tile->mbs;
437 ref_mb = tile->ref_mbs;
438 offs = tile->ypos * band->pitch + tile->xpos;
439
440 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
3df0d200 441 mb_type_bits = ctx->frame_type == IVI4_FRAMETYPE_BIDIR ? 2 : 1;
adfe0c94
KS
442
443 /* scale factor for motion vectors */
444 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
445 mv_x = mv_y = 0;
446
447 for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
448 mb_offset = offs;
449
450 for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
451 mb->xpos = x;
452 mb->ypos = y;
453 mb->buf_offs = mb_offset;
5ec6d152
DA
454 mb->b_mv_x =
455 mb->b_mv_y = 0;
adfe0c94 456
c3defda0 457 if (bitstream_read_bit(&ctx->bc)) {
3df0d200 458 if (ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
adfe0c94
KS
459 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
460 return AVERROR_INVALIDDATA;
461 }
462 mb->type = 1; /* empty macroblocks are always INTER */
463 mb->cbp = 0; /* all blocks are empty */
464
465 mb->q_delta = 0;
466 if (!band->plane && !band->band_num && ctx->in_q) {
c3defda0
AH
467 mb->q_delta = bitstream_read_vlc(&ctx->bc,
468 ctx->mb_vlc.tab->table,
469 IVI_VLC_BITS, 1);
adfe0c94
KS
470 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
471 }
472
473 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
8435bca0 474 if (band->inherit_mv && ref_mb) {
adfe0c94
KS
475 /* motion vector inheritance */
476 if (mv_scale) {
477 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
478 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
479 } else {
480 mb->mv_x = ref_mb->mv_x;
481 mb->mv_y = ref_mb->mv_y;
482 }
483 }
484 } else {
485 if (band->inherit_mv) {
8435bca0
LB
486 /* copy mb_type from corresponding reference mb */
487 if (!ref_mb)
488 return AVERROR_INVALIDDATA;
489 mb->type = ref_mb->type;
3df0d200
DA
490 } else if (ctx->frame_type == IVI4_FRAMETYPE_INTRA ||
491 ctx->frame_type == IVI4_FRAMETYPE_INTRA1) {
adfe0c94
KS
492 mb->type = 0; /* mb_type is always INTRA for intra-frames */
493 } else {
c3defda0 494 mb->type = bitstream_read(&ctx->bc, mb_type_bits);
adfe0c94
KS
495 }
496
c3defda0 497 mb->cbp = bitstream_read(&ctx->bc, blks_per_mb);
adfe0c94
KS
498
499 mb->q_delta = 0;
500 if (band->inherit_qdelta) {
501 if (ref_mb) mb->q_delta = ref_mb->q_delta;
502 } else if (mb->cbp || (!band->plane && !band->band_num &&
503 ctx->in_q)) {
c3defda0
AH
504 mb->q_delta = bitstream_read_vlc(&ctx->bc,
505 ctx->mb_vlc.tab->table,
506 IVI_VLC_BITS, 1);
adfe0c94
KS
507 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
508 }
509
510 if (!mb->type) {
511 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
512 } else {
513 if (band->inherit_mv) {
8435bca0
LB
514 if (ref_mb)
515 /* motion vector inheritance */
516 if (mv_scale) {
517 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
518 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
519 } else {
520 mb->mv_x = ref_mb->mv_x;
521 mb->mv_y = ref_mb->mv_y;
522 }
adfe0c94
KS
523 } else {
524 /* decode motion vector deltas */
c3defda0
AH
525 mv_delta = bitstream_read_vlc(&ctx->bc,
526 ctx->mb_vlc.tab->table,
527 IVI_VLC_BITS, 1);
adfe0c94 528 mv_y += IVI_TOSIGNED(mv_delta);
c3defda0
AH
529 mv_delta = bitstream_read_vlc(&ctx->bc,
530 ctx->mb_vlc.tab->table,
531 IVI_VLC_BITS, 1);
adfe0c94
KS
532 mv_x += IVI_TOSIGNED(mv_delta);
533 mb->mv_x = mv_x;
534 mb->mv_y = mv_y;
5ec6d152 535 if (mb->type == 3) {
c3defda0
AH
536 mv_delta = bitstream_read_vlc(&ctx->bc,
537 ctx->mb_vlc.tab->table,
538 IVI_VLC_BITS, 1);
5ec6d152 539 mv_y += IVI_TOSIGNED(mv_delta);
c3defda0
AH
540 mv_delta = bitstream_read_vlc(&ctx->bc,
541 ctx->mb_vlc.tab->table,
542 IVI_VLC_BITS, 1);
5ec6d152
DA
543 mv_x += IVI_TOSIGNED(mv_delta);
544 mb->b_mv_x = -mv_x;
545 mb->b_mv_y = -mv_y;
546 }
547 }
548 if (mb->type == 2) {
549 mb->b_mv_x = -mb->mv_x;
550 mb->b_mv_y = -mb->mv_y;
551 mb->mv_x = 0;
552 mb->mv_y = 0;
adfe0c94
KS
553 }
554 }
555 }
556
557 mb++;
558 if (ref_mb)
559 ref_mb++;
560 mb_offset += band->mb_size;
561 }
562
563 offs += row_offset;
564 }
565
c3defda0 566 bitstream_align(&ctx->bc);
adfe0c94
KS
567
568 return 0;
569}
570
571
572/**
adfe0c94
KS
573 * Rearrange decoding and reference buffers.
574 *
575 * @param[in,out] ctx pointer to the decoder context
576 */
aa372cf4 577static void switch_buffers(IVI45DecContext *ctx)
adfe0c94 578{
5ec6d152
DA
579 int is_prev_ref = 0, is_ref = 0;
580
adfe0c94 581 switch (ctx->prev_frame_type) {
3df0d200
DA
582 case IVI4_FRAMETYPE_INTRA:
583 case IVI4_FRAMETYPE_INTRA1:
584 case IVI4_FRAMETYPE_INTER:
5ec6d152 585 is_prev_ref = 1;
adfe0c94
KS
586 break;
587 }
588
589 switch (ctx->frame_type) {
3df0d200
DA
590 case IVI4_FRAMETYPE_INTRA:
591 case IVI4_FRAMETYPE_INTRA1:
3df0d200 592 case IVI4_FRAMETYPE_INTER:
5ec6d152 593 is_ref = 1;
adfe0c94
KS
594 break;
595 }
5ec6d152
DA
596
597 if (is_prev_ref && is_ref) {
598 FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
599 } else if (is_prev_ref) {
600 FFSWAP(int, ctx->ref_buf, ctx->b_ref_buf);
601 FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
602 }
adfe0c94
KS
603}
604
605
aa372cf4 606static int is_nonnull_frame(IVI45DecContext *ctx)
adfe0c94 607{
3df0d200 608 return ctx->frame_type < IVI4_FRAMETYPE_NULL_FIRST;
adfe0c94
KS
609}
610
611
aa372cf4 612static av_cold int decode_init(AVCodecContext *avctx)
adfe0c94 613{
aa372cf4 614 IVI45DecContext *ctx = avctx->priv_data;
adfe0c94 615
aa372cf4 616 ff_ivi_init_static_vlc();
adfe0c94 617
aa372cf4
KS
618 /* copy rvmap tables in our context so we can apply changes to them */
619 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
adfe0c94 620
aa372cf4
KS
621 /* Force allocation of the internal buffers */
622 /* during picture header decoding. */
623 ctx->pic_conf.pic_width = 0;
624 ctx->pic_conf.pic_height = 0;
625
716d413c 626 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
aa372cf4
KS
627
628 ctx->decode_pic_hdr = decode_pic_hdr;
629 ctx->decode_band_hdr = decode_band_hdr;
630 ctx->decode_mb_info = decode_mb_info;
631 ctx->switch_buffers = switch_buffers;
632 ctx->is_nonnull_frame = is_nonnull_frame;
adfe0c94 633
e121ac63 634 ctx->is_indeo4 = 1;
6202e2fe 635 ctx->show_indeo4_info = 1;
e121ac63 636
5ec6d152
DA
637 ctx->dst_buf = 0;
638 ctx->ref_buf = 1;
639 ctx->b_ref_buf = 3; /* buffer 2 is used for scalability mode */
f76e3669
DA
640 ctx->p_frame = av_frame_alloc();
641 if (!ctx->p_frame)
642 return AVERROR(ENOMEM);
643
adfe0c94
KS
644 return 0;
645}
646
647
648AVCodec ff_indeo4_decoder = {
649 .name = "indeo4",
b2bed932 650 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
adfe0c94 651 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 652 .id = AV_CODEC_ID_INDEO4,
aa372cf4 653 .priv_data_size = sizeof(IVI45DecContext),
adfe0c94 654 .init = decode_init,
aa372cf4
KS
655 .close = ff_ivi_decode_close,
656 .decode = ff_ivi_decode_frame,
def97856 657 .capabilities = AV_CODEC_CAP_DR1,
adfe0c94 658};