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