vlc: Add header #include when the types are used
[libav.git] / libavcodec / indeo5.c
CommitLineData
5bebe94d
KS
1/*
2 * Indeo Video Interactive v5 compatible decoder
3 * Copyright (c) 2009 Maxim Poliakovski
4 *
2912e87a 5 * This file is part of Libav.
5bebe94d 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
5bebe94d
KS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
5bebe94d
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
5bebe94d
KS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
5bebe94d
KS
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
aaf47bcd 30#define BITSTREAM_READER_LE
5bebe94d 31#include "avcodec.h"
c3defda0 32#include "bitstream.h"
23944d29 33#include "ivi.h"
5bebe94d 34#include "ivi_dsp.h"
5bebe94d 35#include "indeo5data.h"
73fc82f3 36#include "vlc.h"
5bebe94d
KS
37
38/**
39 * Indeo5 frame types.
40 */
41enum {
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
5bebe94d 51/**
49bd8e4b 52 * Decode Indeo5 GOP (Group of pictures) header.
5bebe94d
KS
53 * This header is present in key frames only.
54 * It defines parameters for all frames in a GOP.
55 *
0c733da8
DB
56 * @param[in,out] ctx ptr to the decoder context
57 * @param[in] avctx ptr to the AVCodecContext
5bebe94d
KS
58 * @return result code: 0 = OK, -1 = error
59 */
aa372cf4 60static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
5bebe94d 61{
07ff7825
MP
62 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
63 int quant_mat, blk_size_changed = 0;
5bebe94d
KS
64 IVIBandDesc *band, *band1, *band2;
65 IVIPicConfig pic_conf;
66
c3defda0 67 ctx->gop_flags = bitstream_read(&ctx->bc, 8);
5bebe94d 68
c3defda0 69 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? bitstream_read(&ctx->bc, 16) : 0;
5bebe94d
KS
70
71 if (ctx->gop_flags & IVI5_IS_PROTECTED)
c3defda0 72 ctx->lock_word = bitstream_read(&ctx->bc, 32);
5bebe94d 73
c3defda0 74 tile_size = (ctx->gop_flags & 0x40) ? 64 << bitstream_read(&ctx->bc, 2) : 0;
5bebe94d
KS
75 if (tile_size > 256) {
76 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
b0eeb9d4 77 return AVERROR_INVALIDDATA;
5bebe94d
KS
78 }
79
80 /* decode number of wavelet bands */
81 /* num_levels * 3 + 1 */
c3defda0
AH
82 pic_conf.luma_bands = bitstream_read(&ctx->bc, 2) * 3 + 1;
83 pic_conf.chroma_bands = bitstream_read_bit(&ctx->bc) * 3 + 1;
5bebe94d
KS
84 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
85 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
86 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
87 pic_conf.luma_bands, pic_conf.chroma_bands);
b0eeb9d4 88 return AVERROR_INVALIDDATA;
5bebe94d
KS
89 }
90
c3defda0 91 pic_size_indx = bitstream_read(&ctx->bc, 4);
5bebe94d 92 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
c3defda0
AH
93 pic_conf.pic_height = bitstream_read(&ctx->bc, 13);
94 pic_conf.pic_width = bitstream_read(&ctx->bc, 13);
5bebe94d
KS
95 } else {
96 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
97 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
98 }
99
100 if (ctx->gop_flags & 2) {
b0eeb9d4
LB
101 avpriv_report_missing_feature(avctx, "YV12 picture format");
102 return AVERROR_PATCHWELCOME;
5bebe94d
KS
103 }
104
105 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
106 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
107
108 if (!tile_size) {
109 pic_conf.tile_height = pic_conf.pic_height;
110 pic_conf.tile_width = pic_conf.pic_width;
111 } else {
112 pic_conf.tile_height = pic_conf.tile_width = tile_size;
113 }
114
115 /* check if picture layout was changed and reallocate buffers */
28dda8a6 116 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
5ec6d152 117 result = ff_ivi_init_planes(ctx->planes, &pic_conf, 0);
b0eeb9d4 118 if (result < 0) {
5bebe94d 119 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
b0eeb9d4 120 return result;
5bebe94d
KS
121 }
122 ctx->pic_conf = pic_conf;
123 blk_size_changed = 1; /* force reallocation of the internal structures */
124 }
125
126 for (p = 0; p <= 1; p++) {
127 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
128 band = &ctx->planes[p].bands[i];
129
c3defda0 130 band->is_halfpel = bitstream_read_bit(&ctx->bc);
5bebe94d 131
c3defda0
AH
132 mb_size = bitstream_read_bit(&ctx->bc);
133 blk_size = 8 >> bitstream_read_bit(&ctx->bc);
5bebe94d
KS
134 mb_size = blk_size << !mb_size;
135
136 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
137 if (blk_size_changed) {
138 band->mb_size = mb_size;
139 band->blk_size = blk_size;
140 }
141
c3defda0 142 if (bitstream_read_bit(&ctx->bc)) {
b0eeb9d4
LB
143 avpriv_report_missing_feature(avctx, "Extended transform info");
144 return AVERROR_PATCHWELCOME;
5bebe94d
KS
145 }
146
147 /* select transform function and scan pattern according to plane and band number */
148 switch ((p << 2) + i) {
149 case 0:
dc796851
LB
150 band->inv_transform = ff_ivi_inverse_slant_8x8;
151 band->dc_transform = ff_ivi_dc_slant_2d;
152 band->scan = ff_zigzag_direct;
153 band->transform_size = 8;
5bebe94d
KS
154 break;
155
156 case 1:
dc796851
LB
157 band->inv_transform = ff_ivi_row_slant8;
158 band->dc_transform = ff_ivi_dc_row_slant;
159 band->scan = ff_ivi_vertical_scan_8x8;
160 band->transform_size = 8;
5bebe94d
KS
161 break;
162
163 case 2:
dc796851
LB
164 band->inv_transform = ff_ivi_col_slant8;
165 band->dc_transform = ff_ivi_dc_col_slant;
166 band->scan = ff_ivi_horizontal_scan_8x8;
167 band->transform_size = 8;
5bebe94d
KS
168 break;
169
170 case 3:
dc796851
LB
171 band->inv_transform = ff_ivi_put_pixels_8x8;
172 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
173 band->scan = ff_ivi_horizontal_scan_8x8;
174 band->transform_size = 8;
5bebe94d
KS
175 break;
176
177 case 4:
dc796851
LB
178 band->inv_transform = ff_ivi_inverse_slant_4x4;
179 band->dc_transform = ff_ivi_dc_slant_2d;
180 band->scan = ff_ivi_direct_scan_4x4;
181 band->transform_size = 4;
5bebe94d
KS
182 break;
183 }
184
185 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
186 band->inv_transform == ff_ivi_inverse_slant_4x4;
187
dc796851
LB
188 if (band->transform_size != band->blk_size)
189 return AVERROR_INVALIDDATA;
190
5bebe94d
KS
191 /* select dequant matrix according to plane and band number */
192 if (!p) {
07ff7825 193 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
5bebe94d 194 } else {
07ff7825
MP
195 quant_mat = 5;
196 }
197
198 if (band->blk_size == 8) {
199 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
200 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
201 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
202 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
203 } else {
204 band->intra_base = ivi5_base_quant_4x4_intra;
205 band->inter_base = ivi5_base_quant_4x4_inter;
206 band->intra_scale = ivi5_scale_quant_4x4_intra;
207 band->inter_scale = ivi5_scale_quant_4x4_inter;
5bebe94d
KS
208 }
209
c3defda0 210 if (bitstream_read(&ctx->bc, 2)) {
5bebe94d 211 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
b0eeb9d4 212 return AVERROR_INVALIDDATA;
5bebe94d
KS
213 }
214 }
215 }
216
217 /* copy chroma parameters into the 2nd chroma plane */
218 for (i = 0; i < pic_conf.chroma_bands; i++) {
219 band1 = &ctx->planes[1].bands[i];
220 band2 = &ctx->planes[2].bands[i];
221
222 band2->width = band1->width;
223 band2->height = band1->height;
224 band2->mb_size = band1->mb_size;
225 band2->blk_size = band1->blk_size;
226 band2->is_halfpel = band1->is_halfpel;
07ff7825
MP
227 band2->intra_base = band1->intra_base;
228 band2->inter_base = band1->inter_base;
229 band2->intra_scale = band1->intra_scale;
230 band2->inter_scale = band1->inter_scale;
5bebe94d
KS
231 band2->scan = band1->scan;
232 band2->inv_transform = band1->inv_transform;
233 band2->dc_transform = band1->dc_transform;
234 band2->is_2d_trans = band1->is_2d_trans;
235 }
236
237 /* reallocate internal structures if needed */
238 if (blk_size_changed) {
239 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
240 pic_conf.tile_height);
b0eeb9d4 241 if (result < 0) {
5bebe94d
KS
242 av_log(avctx, AV_LOG_ERROR,
243 "Couldn't reallocate internal structures!\n");
b0eeb9d4 244 return result;
5bebe94d
KS
245 }
246 }
247
248 if (ctx->gop_flags & 8) {
c3defda0 249 if (bitstream_read(&ctx->bc, 3)) {
5bebe94d 250 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
b0eeb9d4 251 return AVERROR_INVALIDDATA;
5bebe94d
KS
252 }
253
c3defda0
AH
254 if (bitstream_read_bit(&ctx->bc))
255 bitstream_skip(&ctx->bc, 24); /* skip transparency fill color */
5bebe94d
KS
256 }
257
c3defda0 258 bitstream_align(&ctx->bc);
5bebe94d 259
c3defda0 260 bitstream_skip(&ctx->bc, 23); /* FIXME: unknown meaning */
5bebe94d
KS
261
262 /* skip GOP extension if any */
c3defda0 263 if (bitstream_read_bit(&ctx->bc)) {
5bebe94d 264 do {
c3defda0 265 i = bitstream_read(&ctx->bc, 16);
5bebe94d
KS
266 } while (i & 0x8000);
267 }
268
c3defda0 269 bitstream_align(&ctx->bc);
5bebe94d
KS
270
271 return 0;
272}
273
274
275/**
49bd8e4b 276 * Skip a header extension.
5bebe94d 277 *
c3defda0 278 * @param[in,out] bc the Bitstream context
5bebe94d 279 */
c3defda0 280static inline void skip_hdr_extension(BitstreamContext *bc)
5bebe94d
KS
281{
282 int i, len;
283
284 do {
c3defda0
AH
285 len = bitstream_read(bc, 8);
286 for (i = 0; i < len; i++)
287 bitstream_skip(bc, 8);
5bebe94d
KS
288 } while(len);
289}
290
291
292/**
49bd8e4b 293 * Decode Indeo5 picture header.
5bebe94d 294 *
0c733da8
DB
295 * @param[in,out] ctx ptr to the decoder context
296 * @param[in] avctx ptr to the AVCodecContext
5bebe94d
KS
297 * @return result code: 0 = OK, -1 = error
298 */
aa372cf4 299static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
5bebe94d 300{
b0eeb9d4
LB
301 int ret;
302
c3defda0 303 if (bitstream_read(&ctx->bc, 5) != 0x1F) {
5bebe94d 304 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
b0eeb9d4 305 return AVERROR_INVALIDDATA;
5bebe94d
KS
306 }
307
308 ctx->prev_frame_type = ctx->frame_type;
c3defda0 309 ctx->frame_type = bitstream_read(&ctx->bc, 3);
5bebe94d
KS
310 if (ctx->frame_type >= 5) {
311 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
b0eeb9d4 312 return AVERROR_INVALIDDATA;
5bebe94d
KS
313 }
314
c3defda0 315 ctx->frame_num = bitstream_read(&ctx->bc, 8);
5bebe94d
KS
316
317 if (ctx->frame_type == FRAMETYPE_INTRA) {
b0eeb9d4 318 if ((ret = decode_gop_header(ctx, avctx)) < 0) {
89191843 319 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
28dda8a6 320 ctx->gop_invalid = 1;
b0eeb9d4 321 return ret;
89191843
MN
322 }
323 ctx->gop_invalid = 0;
5bebe94d
KS
324 }
325
326 if (ctx->frame_type != FRAMETYPE_NULL) {
c3defda0 327 ctx->frame_flags = bitstream_read(&ctx->bc, 8);
5bebe94d 328
c3defda0 329 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? bitstream_read(&ctx->bc, 24) : 0;
5bebe94d 330
c3defda0 331 ctx->checksum = (ctx->frame_flags & 0x10) ? bitstream_read(&ctx->bc, 16) : 0;
5bebe94d
KS
332
333 /* skip unknown extension if any */
334 if (ctx->frame_flags & 0x20)
c3defda0 335 skip_hdr_extension(&ctx->bc); /* XXX: untested */
5bebe94d
KS
336
337 /* decode macroblock huffman codebook */
c3defda0 338 ret = ff_ivi_dec_huff_desc(&ctx->bc, ctx->frame_flags & 0x40,
b0eeb9d4
LB
339 IVI_MB_HUFF, &ctx->mb_vlc, avctx);
340 if (ret < 0)
341 return ret;
5bebe94d 342
c3defda0 343 bitstream_skip(&ctx->bc, 3); /* FIXME: unknown meaning! */
5bebe94d
KS
344 }
345
c3defda0 346 bitstream_align(&ctx->bc);
5bebe94d
KS
347
348 return 0;
349}
350
351
352/**
49bd8e4b 353 * Decode Indeo5 band header.
5bebe94d 354 *
0c733da8
DB
355 * @param[in,out] ctx ptr to the decoder context
356 * @param[in,out] band ptr to the band descriptor
357 * @param[in] avctx ptr to the AVCodecContext
5bebe94d
KS
358 * @return result code: 0 = OK, -1 = error
359 */
aa372cf4 360static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
5bebe94d
KS
361 AVCodecContext *avctx)
362{
b0eeb9d4 363 int i, ret;
5bebe94d 364 uint8_t band_flags;
5bebe94d 365
c3defda0 366 band_flags = bitstream_read(&ctx->bc, 8);
5bebe94d
KS
367
368 if (band_flags & 1) {
369 band->is_empty = 1;
370 return 0;
371 }
372
c3defda0 373 band->data_size = (ctx->frame_flags & 0x80) ? bitstream_read(&ctx->bc, 24) : 0;
5bebe94d
KS
374
375 band->inherit_mv = band_flags & 2;
376 band->inherit_qdelta = band_flags & 8;
377 band->qdelta_present = band_flags & 4;
378 if (!band->qdelta_present) band->inherit_qdelta = 1;
379
380 /* decode rvmap probability corrections if any */
381 band->num_corr = 0; /* there are no corrections */
382 if (band_flags & 0x10) {
c3defda0 383 band->num_corr = bitstream_read(&ctx->bc, 8); /* get number of correction pairs */
5bebe94d
KS
384 if (band->num_corr > 61) {
385 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
386 band->num_corr);
b0eeb9d4 387 return AVERROR_INVALIDDATA;
5bebe94d
KS
388 }
389
390 /* read correction pairs */
391 for (i = 0; i < band->num_corr * 2; i++)
c3defda0 392 band->corr[i] = bitstream_read(&ctx->bc, 8);
5bebe94d
KS
393 }
394
395 /* select appropriate rvmap table for this band */
c3defda0 396 band->rvmap_sel = (band_flags & 0x40) ? bitstream_read(&ctx->bc, 3) : 8;
5bebe94d
KS
397
398 /* decode block huffman codebook */
c3defda0 399 ret = ff_ivi_dec_huff_desc(&ctx->bc, band_flags & 0x80, IVI_BLK_HUFF,
b0eeb9d4
LB
400 &band->blk_vlc, avctx);
401 if (ret < 0)
402 return ret;
5bebe94d 403
c3defda0 404 band->checksum_present = bitstream_read_bit(&ctx->bc);
5bebe94d 405 if (band->checksum_present)
c3defda0 406 band->checksum = bitstream_read(&ctx->bc, 16);
5bebe94d 407
c3defda0 408 band->glob_quant = bitstream_read(&ctx->bc, 5);
5bebe94d
KS
409
410 /* skip unknown extension if any */
411 if (band_flags & 0x20) { /* XXX: untested */
c3defda0
AH
412 bitstream_align(&ctx->bc);
413 skip_hdr_extension(&ctx->bc);
5bebe94d
KS
414 }
415
c3defda0 416 bitstream_align(&ctx->bc);
5bebe94d
KS
417
418 return 0;
419}
420
421
422/**
49bd8e4b 423 * Decode info (block type, cbp, quant delta, motion vector)
5bebe94d
KS
424 * for all macroblocks in the current tile.
425 *
0c733da8
DB
426 * @param[in,out] ctx ptr to the decoder context
427 * @param[in,out] band ptr to the band descriptor
428 * @param[in,out] tile ptr to the tile descriptor
429 * @param[in] avctx ptr to the AVCodecContext
5bebe94d
KS
430 * @return result code: 0 = OK, -1 = error
431 */
aa372cf4 432static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
5bebe94d
KS
433 IVITile *tile, AVCodecContext *avctx)
434{
435 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
436 mv_scale, blks_per_mb;
437 IVIMbInfo *mb, *ref_mb;
438 int row_offset = band->mb_size * band->pitch;
439
440 mb = tile->mbs;
441 ref_mb = tile->ref_mbs;
442 offs = tile->ypos * band->pitch + tile->xpos;
443
366ac22e
JG
444 if (!ref_mb &&
445 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
446 return AVERROR_INVALIDDATA;
447
2d09cdba
MN
448 if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
449 av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
450 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
451 return AVERROR_INVALIDDATA;
452 }
453
5bebe94d
KS
454 /* scale factor for motion vectors */
455 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
456 mv_x = mv_y = 0;
457
458 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
459 mb_offset = offs;
460
461 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
462 mb->xpos = x;
463 mb->ypos = y;
464 mb->buf_offs = mb_offset;
465
c3defda0 466 if (bitstream_read_bit(&ctx->bc)) {
5bebe94d
KS
467 if (ctx->frame_type == FRAMETYPE_INTRA) {
468 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
b0eeb9d4 469 return AVERROR_INVALIDDATA;
5bebe94d
KS
470 }
471 mb->type = 1; /* empty macroblocks are always INTER */
472 mb->cbp = 0; /* all blocks are empty */
473
474 mb->q_delta = 0;
475 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
c3defda0
AH
476 mb->q_delta = bitstream_read_vlc(&ctx->bc,
477 ctx->mb_vlc.tab->table,
478 IVI_VLC_BITS, 1);
5bebe94d
KS
479 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
480 }
481
482 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
483 if (band->inherit_mv){
484 /* motion vector inheritance */
485 if (mv_scale) {
486 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
487 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
488 } else {
489 mb->mv_x = ref_mb->mv_x;
490 mb->mv_y = ref_mb->mv_y;
491 }
492 }
493 } else {
494 if (band->inherit_mv) {
495 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
496 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
497 mb->type = 0; /* mb_type is always INTRA for intra-frames */
498 } else {
c3defda0 499 mb->type = bitstream_read_bit(&ctx->bc);
5bebe94d
KS
500 }
501
502 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
c3defda0 503 mb->cbp = bitstream_read(&ctx->bc, blks_per_mb);
5bebe94d
KS
504
505 mb->q_delta = 0;
506 if (band->qdelta_present) {
507 if (band->inherit_qdelta) {
508 if (ref_mb) mb->q_delta = ref_mb->q_delta;
509 } else if (mb->cbp || (!band->plane && !band->band_num &&
510 (ctx->frame_flags & 8))) {
c3defda0
AH
511 mb->q_delta = bitstream_read_vlc(&ctx->bc,
512 ctx->mb_vlc.tab->table,
513 IVI_VLC_BITS, 1);
5bebe94d
KS
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 */
c3defda0
AH
532 mv_delta = bitstream_read_vlc(&ctx->bc,
533 ctx->mb_vlc.tab->table,
534 IVI_VLC_BITS, 1);
5bebe94d 535 mv_y += IVI_TOSIGNED(mv_delta);
c3defda0
AH
536 mv_delta = bitstream_read_vlc(&ctx->bc,
537 ctx->mb_vlc.tab->table,
538 IVI_VLC_BITS, 1);
5bebe94d
KS
539 mv_x += IVI_TOSIGNED(mv_delta);
540 mb->mv_x = mv_x;
541 mb->mv_y = mv_y;
542 }
543 }
544 }
545
546 mb++;
547 if (ref_mb)
548 ref_mb++;
549 mb_offset += band->mb_size;
550 }
551
552 offs += row_offset;
553 }
554
c3defda0 555 bitstream_align(&ctx->bc);
5bebe94d
KS
556
557 return 0;
558}
559
560
561/**
49bd8e4b 562 * Switch buffers.
5bebe94d 563 *
0c733da8 564 * @param[in,out] ctx ptr to the decoder context
5bebe94d 565 */
aa372cf4 566static void switch_buffers(IVI45DecContext *ctx)
5bebe94d 567{
ace38c36 568 switch (ctx->prev_frame_type) {
5bebe94d 569 case FRAMETYPE_INTRA:
5bebe94d 570 case FRAMETYPE_INTER:
ace38c36 571 ctx->buf_switch ^= 1;
5bebe94d
KS
572 ctx->dst_buf = ctx->buf_switch;
573 ctx->ref_buf = ctx->buf_switch ^ 1;
574 break;
575 case FRAMETYPE_INTER_SCAL:
ace38c36
MP
576 if (!ctx->inter_scal) {
577 ctx->ref2_buf = 2;
578 ctx->inter_scal = 1;
5bebe94d 579 }
ace38c36
MP
580 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
581 ctx->ref_buf = ctx->ref2_buf;
5bebe94d
KS
582 break;
583 case FRAMETYPE_INTER_NOREF:
5bebe94d 584 break;
ace38c36
MP
585 }
586
587 switch (ctx->frame_type) {
588 case FRAMETYPE_INTRA:
589 ctx->buf_switch = 0;
590 /* FALLTHROUGH */
591 case FRAMETYPE_INTER:
592 ctx->inter_scal = 0;
593 ctx->dst_buf = ctx->buf_switch;
594 ctx->ref_buf = ctx->buf_switch ^ 1;
595 break;
596 case FRAMETYPE_INTER_SCAL:
597 case FRAMETYPE_INTER_NOREF:
5bebe94d 598 case FRAMETYPE_NULL:
ace38c36 599 break;
5bebe94d
KS
600 }
601}
602
603
aa372cf4
KS
604static int is_nonnull_frame(IVI45DecContext *ctx)
605{
606 return ctx->frame_type != FRAMETYPE_NULL;
607}
608
609
5bebe94d 610/**
49bd8e4b 611 * Initialize Indeo5 decoder.
5bebe94d
KS
612 */
613static av_cold int decode_init(AVCodecContext *avctx)
614{
aa372cf4 615 IVI45DecContext *ctx = avctx->priv_data;
9068f36d 616 int result;
5bebe94d 617
9068f36d 618 ff_ivi_init_static_vlc();
5bebe94d
KS
619
620 /* copy rvmap tables in our context so we can apply changes to them */
621 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
622
623 /* set the initial picture layout according to the basic profile:
624 there is only one band per plane (no scalability), only one tile (no local decoding)
625 and picture format = YVU9 */
626 ctx->pic_conf.pic_width = avctx->width;
627 ctx->pic_conf.pic_height = avctx->height;
628 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
629 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
630 ctx->pic_conf.tile_width = avctx->width;
631 ctx->pic_conf.tile_height = avctx->height;
632 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
633
5ec6d152 634 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf, 0);
5bebe94d
KS
635 if (result) {
636 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
b0eeb9d4 637 return AVERROR_INVALIDDATA;
5bebe94d
KS
638 }
639
ace38c36
MP
640 ctx->buf_switch = 0;
641 ctx->inter_scal = 0;
642
aa372cf4
KS
643 ctx->decode_pic_hdr = decode_pic_hdr;
644 ctx->decode_band_hdr = decode_band_hdr;
645 ctx->decode_mb_info = decode_mb_info;
646 ctx->switch_buffers = switch_buffers;
647 ctx->is_nonnull_frame = is_nonnull_frame;
5bebe94d 648
e121ac63
DA
649 ctx->is_indeo4 = 0;
650
716d413c 651 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
5bebe94d
KS
652
653 return 0;
654}
655
656
d36beb3f 657AVCodec ff_indeo5_decoder = {
5bebe94d 658 .name = "indeo5",
b2bed932 659 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
72415b2a 660 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 661 .id = AV_CODEC_ID_INDEO5,
aa372cf4 662 .priv_data_size = sizeof(IVI45DecContext),
5bebe94d 663 .init = decode_init,
aa372cf4
KS
664 .close = ff_ivi_decode_close,
665 .decode = ff_ivi_decode_frame,
def97856 666 .capabilities = AV_CODEC_CAP_DR1,
5bebe94d 667};