2 * Indeo Video Interactive v5 compatible decoder
3 * Copyright (c) 2009 Maxim Poliakovski
5 * This file is part of FFmpeg.
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.
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.
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
24 * Indeo Video Interactive version 5 decoder
26 * Indeo5 data is usually transported within .avi or .mov files.
27 * Known FOURCCs: 'IV50'
30 #define BITSTREAM_READER_LE
35 #include "ivi_common.h"
36 #include "indeo5data.h"
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
49 #define IVI5_PIC_SIZE_ESC 15
51 #define IVI5_IS_PROTECTED 0x20
56 RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables
57 IVIPlaneDesc planes[3]; ///< color planes
58 const uint8_t *frame_data; ///< input frame data pointer
59 int buf_switch; ///< used to switch between three buffers
60 int inter_scal; ///< signals a sequence of scalable inter frames
61 int dst_buf; ///< buffer index for the currently decoded frame
62 int ref_buf; ///< inter frame reference buffer index
63 int ref2_buf; ///< temporal storage for switching buffers
64 uint32_t frame_size; ///< frame size in bytes
66 int prev_frame_type; ///< frame type of the previous frame
68 uint32_t pic_hdr_size; ///< picture header size in bytes
70 uint16_t checksum; ///< frame checksum
72 IVIHuffTab mb_vlc; ///< vlc table for decoding macroblock data
74 uint16_t gop_hdr_size;
78 IVIPicConfig pic_conf;
86 * Decode Indeo5 GOP (Group of pictures) header.
87 * This header is present in key frames only.
88 * It defines parameters for all frames in a GOP.
90 * @param[in,out] ctx ptr to the decoder context
91 * @param[in] avctx ptr to the AVCodecContext
92 * @return result code: 0 = OK, -1 = error
94 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
96 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
97 int quant_mat, blk_size_changed = 0;
98 IVIBandDesc *band, *band1, *band2;
99 IVIPicConfig pic_conf;
101 ctx->gop_flags = get_bits(&ctx->gb, 8);
103 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
105 if (ctx->gop_flags & IVI5_IS_PROTECTED)
106 ctx->lock_word = get_bits_long(&ctx->gb, 32);
108 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
109 if (tile_size > 256) {
110 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
114 /* decode number of wavelet bands */
115 /* num_levels * 3 + 1 */
116 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
117 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
118 is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
119 if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
120 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
121 pic_conf.luma_bands, pic_conf.chroma_bands);
125 pic_size_indx = get_bits(&ctx->gb, 4);
126 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
127 pic_conf.pic_height = get_bits(&ctx->gb, 13);
128 pic_conf.pic_width = get_bits(&ctx->gb, 13);
130 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
131 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
134 if (ctx->gop_flags & 2) {
135 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
139 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
140 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
143 pic_conf.tile_height = pic_conf.pic_height;
144 pic_conf.tile_width = pic_conf.pic_width;
146 pic_conf.tile_height = pic_conf.tile_width = tile_size;
149 /* check if picture layout was changed and reallocate buffers */
150 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
151 result = ff_ivi_init_planes(ctx->planes, &pic_conf);
153 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
156 ctx->pic_conf = pic_conf;
157 ctx->is_scalable = is_scalable;
158 blk_size_changed = 1; /* force reallocation of the internal structures */
161 for (p = 0; p <= 1; p++) {
162 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
163 band = &ctx->planes[p].bands[i];
165 band->is_halfpel = get_bits1(&ctx->gb);
167 mb_size = get_bits1(&ctx->gb);
168 blk_size = 8 >> get_bits1(&ctx->gb);
169 mb_size = blk_size << !mb_size;
171 if (p==0 && blk_size==4) {
172 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
173 return AVERROR_PATCHWELCOME;
176 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
177 if (blk_size_changed) {
178 band->mb_size = mb_size;
179 band->blk_size = blk_size;
182 if (get_bits1(&ctx->gb)) {
183 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
187 /* select transform function and scan pattern according to plane and band number */
188 switch ((p << 2) + i) {
190 band->inv_transform = ff_ivi_inverse_slant_8x8;
191 band->dc_transform = ff_ivi_dc_slant_2d;
192 band->scan = ff_zigzag_direct;
193 band->transform_size= 8;
197 band->inv_transform = ff_ivi_row_slant8;
198 band->dc_transform = ff_ivi_dc_row_slant;
199 band->scan = ff_ivi_vertical_scan_8x8;
200 band->transform_size= 8;
204 band->inv_transform = ff_ivi_col_slant8;
205 band->dc_transform = ff_ivi_dc_col_slant;
206 band->scan = ff_ivi_horizontal_scan_8x8;
207 band->transform_size= 8;
211 band->inv_transform = ff_ivi_put_pixels_8x8;
212 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
213 band->scan = ff_ivi_horizontal_scan_8x8;
214 band->transform_size= 8;
218 band->inv_transform = ff_ivi_inverse_slant_4x4;
219 band->dc_transform = ff_ivi_dc_slant_2d;
220 band->scan = ff_ivi_direct_scan_4x4;
221 band->transform_size= 4;
225 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
226 band->inv_transform == ff_ivi_inverse_slant_4x4;
228 /* select dequant matrix according to plane and band number */
230 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
235 if (band->blk_size == 8) {
236 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
237 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
238 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
239 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
241 band->intra_base = ivi5_base_quant_4x4_intra;
242 band->inter_base = ivi5_base_quant_4x4_inter;
243 band->intra_scale = ivi5_scale_quant_4x4_intra;
244 band->inter_scale = ivi5_scale_quant_4x4_inter;
247 if (get_bits(&ctx->gb, 2)) {
248 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
254 /* copy chroma parameters into the 2nd chroma plane */
255 for (i = 0; i < pic_conf.chroma_bands; i++) {
256 band1 = &ctx->planes[1].bands[i];
257 band2 = &ctx->planes[2].bands[i];
259 band2->width = band1->width;
260 band2->height = band1->height;
261 band2->mb_size = band1->mb_size;
262 band2->blk_size = band1->blk_size;
263 band2->is_halfpel = band1->is_halfpel;
264 band2->intra_base = band1->intra_base;
265 band2->inter_base = band1->inter_base;
266 band2->intra_scale = band1->intra_scale;
267 band2->inter_scale = band1->inter_scale;
268 band2->scan = band1->scan;
269 band2->inv_transform = band1->inv_transform;
270 band2->dc_transform = band1->dc_transform;
271 band2->is_2d_trans = band1->is_2d_trans;
272 band2->transform_size= band1->transform_size;
275 /* reallocate internal structures if needed */
276 if (blk_size_changed) {
277 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
278 pic_conf.tile_height);
280 av_log(avctx, AV_LOG_ERROR,
281 "Couldn't reallocate internal structures!\n");
286 if (ctx->gop_flags & 8) {
287 if (get_bits(&ctx->gb, 3)) {
288 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
292 if (get_bits1(&ctx->gb))
293 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
296 align_get_bits(&ctx->gb);
298 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
300 /* skip GOP extension if any */
301 if (get_bits1(&ctx->gb)) {
303 i = get_bits(&ctx->gb, 16);
304 } while (i & 0x8000);
307 align_get_bits(&ctx->gb);
314 * Skip a header extension.
316 * @param[in,out] gb the GetBit context
318 static inline void skip_hdr_extension(GetBitContext *gb)
323 len = get_bits(gb, 8);
324 for (i = 0; i < len; i++) skip_bits(gb, 8);
330 * Decode Indeo5 picture header.
332 * @param[in,out] ctx ptr to the decoder context
333 * @param[in] avctx ptr to the AVCodecContext
334 * @return result code: 0 = OK, -1 = error
336 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
338 if (get_bits(&ctx->gb, 5) != 0x1F) {
339 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
343 ctx->prev_frame_type = ctx->frame_type;
344 ctx->frame_type = get_bits(&ctx->gb, 3);
345 if (ctx->frame_type >= 5) {
346 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
350 ctx->frame_num = get_bits(&ctx->gb, 8);
352 if (ctx->frame_type == FRAMETYPE_INTRA) {
353 ctx->gop_invalid = 1;
354 if (decode_gop_header(ctx, avctx))
356 ctx->gop_invalid = 0;
359 if (ctx->frame_type != FRAMETYPE_NULL) {
360 ctx->frame_flags = get_bits(&ctx->gb, 8);
362 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
364 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
366 /* skip unknown extension if any */
367 if (ctx->frame_flags & 0x20)
368 skip_hdr_extension(&ctx->gb); /* XXX: untested */
370 /* decode macroblock huffman codebook */
371 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
374 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
377 align_get_bits(&ctx->gb);
384 * Decode Indeo5 band header.
386 * @param[in,out] ctx ptr to the decoder context
387 * @param[in,out] band ptr to the band descriptor
388 * @param[in] avctx ptr to the AVCodecContext
389 * @return result code: 0 = OK, -1 = error
391 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
392 AVCodecContext *avctx)
397 band_flags = get_bits(&ctx->gb, 8);
399 if (band_flags & 1) {
404 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
406 band->inherit_mv = band_flags & 2;
407 band->inherit_qdelta = band_flags & 8;
408 band->qdelta_present = band_flags & 4;
409 if (!band->qdelta_present) band->inherit_qdelta = 1;
411 /* decode rvmap probability corrections if any */
412 band->num_corr = 0; /* there are no corrections */
413 if (band_flags & 0x10) {
414 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
415 if (band->num_corr > 61) {
416 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
421 /* read correction pairs */
422 for (i = 0; i < band->num_corr * 2; i++)
423 band->corr[i] = get_bits(&ctx->gb, 8);
426 /* select appropriate rvmap table for this band */
427 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
429 /* decode block huffman codebook */
430 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
433 band->checksum_present = get_bits1(&ctx->gb);
434 if (band->checksum_present)
435 band->checksum = get_bits(&ctx->gb, 16);
437 band->glob_quant = get_bits(&ctx->gb, 5);
439 /* skip unknown extension if any */
440 if (band_flags & 0x20) { /* XXX: untested */
441 align_get_bits(&ctx->gb);
442 skip_hdr_extension(&ctx->gb);
445 align_get_bits(&ctx->gb);
452 * Decode info (block type, cbp, quant delta, motion vector)
453 * for all macroblocks in the current tile.
455 * @param[in,out] ctx ptr to the decoder context
456 * @param[in,out] band ptr to the band descriptor
457 * @param[in,out] tile ptr to the tile descriptor
458 * @param[in] avctx ptr to the AVCodecContext
459 * @return result code: 0 = OK, -1 = error
461 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
462 IVITile *tile, AVCodecContext *avctx)
464 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
465 mv_scale, blks_per_mb, s;
466 IVIMbInfo *mb, *ref_mb;
467 int row_offset = band->mb_size * band->pitch;
470 ref_mb = tile->ref_mbs;
471 offs = tile->ypos * band->pitch + tile->xpos;
474 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
475 return AVERROR_INVALIDDATA;
477 if( tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size) ){
478 av_log(avctx, AV_LOG_ERROR, "allocated tile size %d mismatches parameters %d\n",
479 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
480 return AVERROR_INVALIDDATA;
483 /* scale factor for motion vectors */
484 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
487 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
490 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
493 mb->buf_offs = mb_offset;
495 if (get_bits1(&ctx->gb)) {
496 if (ctx->frame_type == FRAMETYPE_INTRA) {
497 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
500 mb->type = 1; /* empty macroblocks are always INTER */
501 mb->cbp = 0; /* all blocks are empty */
504 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
505 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
507 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
510 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
511 if (band->inherit_mv && ref_mb){
512 /* motion vector inheritance */
514 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
515 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
517 mb->mv_x = ref_mb->mv_x;
518 mb->mv_y = ref_mb->mv_y;
522 if (band->inherit_mv && ref_mb) {
523 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
524 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
525 mb->type = 0; /* mb_type is always INTRA for intra-frames */
527 mb->type = get_bits1(&ctx->gb);
530 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
531 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
534 if (band->qdelta_present) {
535 if (band->inherit_qdelta) {
536 if (ref_mb) mb->q_delta = ref_mb->q_delta;
537 } else if (mb->cbp || (!band->plane && !band->band_num &&
538 (ctx->frame_flags & 8))) {
539 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
541 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
546 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
548 if (band->inherit_mv && ref_mb){
549 /* motion vector inheritance */
551 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
552 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
554 mb->mv_x = ref_mb->mv_x;
555 mb->mv_y = ref_mb->mv_y;
558 /* decode motion vector deltas */
559 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
561 mv_y += IVI_TOSIGNED(mv_delta);
562 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
564 mv_x += IVI_TOSIGNED(mv_delta);
573 if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
574 x + ((mb->mv_x+s)>>s) + band->mb_size - 1
575 + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
576 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
577 return AVERROR_INVALIDDATA;
583 mb_offset += band->mb_size;
589 align_get_bits(&ctx->gb);
596 * Decode an Indeo5 band.
598 * @param[in,out] ctx ptr to the decoder context
599 * @param[in,out] band ptr to the band descriptor
600 * @param[in] avctx ptr to the AVCodecContext
601 * @return result code: 0 = OK, -1 = error
603 static int decode_band(IVI5DecContext *ctx, int plane_num,
604 IVIBandDesc *band, AVCodecContext *avctx)
606 int result, i, t, idx1, idx2, pos;
609 band->buf = band->bufs[ctx->dst_buf];
610 band->ref_buf = band->bufs[ctx->ref_buf];
611 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
613 result = decode_band_hdr(ctx, band, avctx);
615 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
620 if (band->is_empty) {
621 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
625 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
627 /* apply corrections to the selected rvmap table if present */
628 for (i = 0; i < band->num_corr; i++) {
629 idx1 = band->corr[i*2];
630 idx2 = band->corr[i*2+1];
631 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
632 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
635 pos = get_bits_count(&ctx->gb);
637 for (t = 0; t < band->num_tiles; t++) {
638 tile = &band->tiles[t];
640 tile->is_empty = get_bits1(&ctx->gb);
641 if (tile->is_empty) {
642 ff_ivi_process_empty_tile(avctx, band, tile,
643 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
645 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
647 result = decode_mb_info(ctx, band, tile, avctx);
651 result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
652 if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
653 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
656 pos += tile->data_size << 3; // skip to next tile
660 /* restore the selected rvmap table by applying its corrections in reverse order */
661 for (i = band->num_corr-1; i >= 0; i--) {
662 idx1 = band->corr[i*2];
663 idx2 = band->corr[i*2+1];
664 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
665 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
669 if (band->checksum_present) {
670 uint16_t chksum = ivi_calc_band_checksum(band);
671 if (chksum != band->checksum) {
672 av_log(avctx, AV_LOG_ERROR,
673 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
674 band->plane, band->band_num, band->checksum, chksum);
679 align_get_bits(&ctx->gb);
688 * @param[in,out] ctx ptr to the decoder context
690 static void switch_buffers(IVI5DecContext *ctx)
692 switch (ctx->prev_frame_type) {
693 case FRAMETYPE_INTRA:
694 case FRAMETYPE_INTER:
695 ctx->buf_switch ^= 1;
696 ctx->dst_buf = ctx->buf_switch;
697 ctx->ref_buf = ctx->buf_switch ^ 1;
699 case FRAMETYPE_INTER_SCAL:
700 if (!ctx->inter_scal) {
704 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
705 ctx->ref_buf = ctx->ref2_buf;
707 case FRAMETYPE_INTER_NOREF:
711 switch (ctx->frame_type) {
712 case FRAMETYPE_INTRA:
715 case FRAMETYPE_INTER:
717 ctx->dst_buf = ctx->buf_switch;
718 ctx->ref_buf = ctx->buf_switch ^ 1;
720 case FRAMETYPE_INTER_SCAL:
721 case FRAMETYPE_INTER_NOREF:
729 * Initialize Indeo5 decoder.
731 static av_cold int decode_init(AVCodecContext *avctx)
733 IVI5DecContext *ctx = avctx->priv_data;
736 ff_ivi_init_static_vlc();
738 /* copy rvmap tables in our context so we can apply changes to them */
739 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
741 /* set the initial picture layout according to the basic profile:
742 there is only one band per plane (no scalability), only one tile (no local decoding)
743 and picture format = YVU9 */
744 ctx->pic_conf.pic_width = avctx->width;
745 ctx->pic_conf.pic_height = avctx->height;
746 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
747 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
748 ctx->pic_conf.tile_width = avctx->width;
749 ctx->pic_conf.tile_height = avctx->height;
750 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
752 avcodec_get_frame_defaults(&ctx->frame);
754 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
756 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
763 avctx->pix_fmt = PIX_FMT_YUV410P;
770 * main decoder function
772 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
775 IVI5DecContext *ctx = avctx->priv_data;
776 const uint8_t *buf = avpkt->data;
777 int buf_size = avpkt->size;
780 init_get_bits(&ctx->gb, buf, buf_size * 8);
781 ctx->frame_data = buf;
782 ctx->frame_size = buf_size;
784 result = decode_pic_hdr(ctx, avctx);
785 if (result || ctx->gop_invalid) {
786 av_log(avctx, AV_LOG_ERROR,
787 "Error while decoding picture header: %d\n", result);
791 if (ctx->gop_flags & IVI5_IS_PROTECTED) {
792 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
800 if (ctx->frame_type != FRAMETYPE_NULL) {
801 ctx->buf_invalid[ctx->dst_buf] = 1;
802 for (p = 0; p < 3; p++) {
803 for (b = 0; b < ctx->planes[p].num_bands; b++) {
804 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
806 av_log(avctx, AV_LOG_ERROR,
807 "Error while decoding band: %d, plane: %d\n", b, p);
812 ctx->buf_invalid[ctx->dst_buf] = 0;
814 if (ctx->buf_invalid[ctx->dst_buf])
817 //STOP_TIMER("decode_planes"); }
819 if (ctx->frame.data[0])
820 avctx->release_buffer(avctx, &ctx->frame);
822 if( avctx->width != ctx->planes[0].width
823 || avctx->height != ctx->planes[0].height)
824 avcodec_set_dimensions(avctx, ctx->planes[0].width, ctx->planes[0].height);
826 ctx->frame.reference = 0;
827 if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
828 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
832 if (ctx->is_scalable) {
833 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
835 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
838 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
839 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
841 *data_size = sizeof(AVFrame);
842 *(AVFrame*)data = ctx->frame;
849 * Close Indeo5 decoder and clean up its context.
851 static av_cold int decode_close(AVCodecContext *avctx)
853 IVI5DecContext *ctx = avctx->priv_data;
855 ff_ivi_free_buffers(&ctx->planes[0]);
857 if (ctx->mb_vlc.cust_tab.table)
858 ff_free_vlc(&ctx->mb_vlc.cust_tab);
860 if (ctx->frame.data[0])
861 avctx->release_buffer(avctx, &ctx->frame);
867 AVCodec ff_indeo5_decoder = {
869 .type = AVMEDIA_TYPE_VIDEO,
870 .id = CODEC_ID_INDEO5,
871 .priv_data_size = sizeof(IVI5DecContext),
873 .close = decode_close,
874 .decode = decode_frame,
875 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),