2 * Indeo Video Interactive v5 compatible decoder
3 * Copyright (c) 2009 Maxim Poliakovski
5 * This file is part of Libav.
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.
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.
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
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;
83 * Decode Indeo5 GOP (Group of pictures) header.
84 * This header is present in key frames only.
85 * It defines parameters for all frames in a GOP.
87 * @param[in,out] ctx ptr to the decoder context
88 * @param[in] avctx ptr to the AVCodecContext
89 * @return result code: 0 = OK, -1 = error
91 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
93 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
94 int quant_mat, blk_size_changed = 0;
95 IVIBandDesc *band, *band1, *band2;
96 IVIPicConfig pic_conf;
98 ctx->gop_flags = get_bits(&ctx->gb, 8);
100 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
102 if (ctx->gop_flags & IVI5_IS_PROTECTED)
103 ctx->lock_word = get_bits_long(&ctx->gb, 32);
105 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
106 if (tile_size > 256) {
107 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
111 /* decode number of wavelet bands */
112 /* num_levels * 3 + 1 */
113 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
114 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
115 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
116 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
117 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
118 pic_conf.luma_bands, pic_conf.chroma_bands);
122 pic_size_indx = get_bits(&ctx->gb, 4);
123 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
124 pic_conf.pic_height = get_bits(&ctx->gb, 13);
125 pic_conf.pic_width = get_bits(&ctx->gb, 13);
127 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
128 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
131 if (ctx->gop_flags & 2) {
132 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
136 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
137 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
140 pic_conf.tile_height = pic_conf.pic_height;
141 pic_conf.tile_width = pic_conf.pic_width;
143 pic_conf.tile_height = pic_conf.tile_width = tile_size;
146 /* check if picture layout was changed and reallocate buffers */
147 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
148 result = ff_ivi_init_planes(ctx->planes, &pic_conf);
150 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
153 ctx->pic_conf = pic_conf;
154 blk_size_changed = 1; /* force reallocation of the internal structures */
157 for (p = 0; p <= 1; p++) {
158 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
159 band = &ctx->planes[p].bands[i];
161 band->is_halfpel = get_bits1(&ctx->gb);
163 mb_size = get_bits1(&ctx->gb);
164 blk_size = 8 >> get_bits1(&ctx->gb);
165 mb_size = blk_size << !mb_size;
167 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
168 if (blk_size_changed) {
169 band->mb_size = mb_size;
170 band->blk_size = blk_size;
173 if (get_bits1(&ctx->gb)) {
174 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
178 /* select transform function and scan pattern according to plane and band number */
179 switch ((p << 2) + i) {
181 band->inv_transform = ff_ivi_inverse_slant_8x8;
182 band->dc_transform = ff_ivi_dc_slant_2d;
183 band->scan = ff_zigzag_direct;
187 band->inv_transform = ff_ivi_row_slant8;
188 band->dc_transform = ff_ivi_dc_row_slant;
189 band->scan = ff_ivi_vertical_scan_8x8;
193 band->inv_transform = ff_ivi_col_slant8;
194 band->dc_transform = ff_ivi_dc_col_slant;
195 band->scan = ff_ivi_horizontal_scan_8x8;
199 band->inv_transform = ff_ivi_put_pixels_8x8;
200 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
201 band->scan = ff_ivi_horizontal_scan_8x8;
205 band->inv_transform = ff_ivi_inverse_slant_4x4;
206 band->dc_transform = ff_ivi_dc_slant_2d;
207 band->scan = ff_ivi_direct_scan_4x4;
211 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
212 band->inv_transform == ff_ivi_inverse_slant_4x4;
214 /* select dequant matrix according to plane and band number */
216 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
221 if (band->blk_size == 8) {
222 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
223 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
224 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
225 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
227 band->intra_base = ivi5_base_quant_4x4_intra;
228 band->inter_base = ivi5_base_quant_4x4_inter;
229 band->intra_scale = ivi5_scale_quant_4x4_intra;
230 band->inter_scale = ivi5_scale_quant_4x4_inter;
233 if (get_bits(&ctx->gb, 2)) {
234 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
240 /* copy chroma parameters into the 2nd chroma plane */
241 for (i = 0; i < pic_conf.chroma_bands; i++) {
242 band1 = &ctx->planes[1].bands[i];
243 band2 = &ctx->planes[2].bands[i];
245 band2->width = band1->width;
246 band2->height = band1->height;
247 band2->mb_size = band1->mb_size;
248 band2->blk_size = band1->blk_size;
249 band2->is_halfpel = band1->is_halfpel;
250 band2->intra_base = band1->intra_base;
251 band2->inter_base = band1->inter_base;
252 band2->intra_scale = band1->intra_scale;
253 band2->inter_scale = band1->inter_scale;
254 band2->scan = band1->scan;
255 band2->inv_transform = band1->inv_transform;
256 band2->dc_transform = band1->dc_transform;
257 band2->is_2d_trans = band1->is_2d_trans;
260 /* reallocate internal structures if needed */
261 if (blk_size_changed) {
262 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
263 pic_conf.tile_height);
265 av_log(avctx, AV_LOG_ERROR,
266 "Couldn't reallocate internal structures!\n");
271 if (ctx->gop_flags & 8) {
272 if (get_bits(&ctx->gb, 3)) {
273 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
277 if (get_bits1(&ctx->gb))
278 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
281 align_get_bits(&ctx->gb);
283 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
285 /* skip GOP extension if any */
286 if (get_bits1(&ctx->gb)) {
288 i = get_bits(&ctx->gb, 16);
289 } while (i & 0x8000);
292 align_get_bits(&ctx->gb);
299 * Skip a header extension.
301 * @param[in,out] gb the GetBit context
303 static inline void skip_hdr_extension(GetBitContext *gb)
308 len = get_bits(gb, 8);
309 for (i = 0; i < len; i++) skip_bits(gb, 8);
315 * Decode Indeo5 picture header.
317 * @param[in,out] ctx ptr to the decoder context
318 * @param[in] avctx ptr to the AVCodecContext
319 * @return result code: 0 = OK, -1 = error
321 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
323 if (get_bits(&ctx->gb, 5) != 0x1F) {
324 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
328 ctx->prev_frame_type = ctx->frame_type;
329 ctx->frame_type = get_bits(&ctx->gb, 3);
330 if (ctx->frame_type >= 5) {
331 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
335 ctx->frame_num = get_bits(&ctx->gb, 8);
337 if (ctx->frame_type == FRAMETYPE_INTRA) {
338 if (decode_gop_header(ctx, avctx))
342 if (ctx->frame_type != FRAMETYPE_NULL) {
343 ctx->frame_flags = get_bits(&ctx->gb, 8);
345 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
347 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
349 /* skip unknown extension if any */
350 if (ctx->frame_flags & 0x20)
351 skip_hdr_extension(&ctx->gb); /* XXX: untested */
353 /* decode macroblock huffman codebook */
354 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
357 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
360 align_get_bits(&ctx->gb);
367 * Decode Indeo5 band header.
369 * @param[in,out] ctx ptr to the decoder context
370 * @param[in,out] band ptr to the band descriptor
371 * @param[in] avctx ptr to the AVCodecContext
372 * @return result code: 0 = OK, -1 = error
374 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
375 AVCodecContext *avctx)
380 band_flags = get_bits(&ctx->gb, 8);
382 if (band_flags & 1) {
387 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
389 band->inherit_mv = band_flags & 2;
390 band->inherit_qdelta = band_flags & 8;
391 band->qdelta_present = band_flags & 4;
392 if (!band->qdelta_present) band->inherit_qdelta = 1;
394 /* decode rvmap probability corrections if any */
395 band->num_corr = 0; /* there are no corrections */
396 if (band_flags & 0x10) {
397 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
398 if (band->num_corr > 61) {
399 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
404 /* read correction pairs */
405 for (i = 0; i < band->num_corr * 2; i++)
406 band->corr[i] = get_bits(&ctx->gb, 8);
409 /* select appropriate rvmap table for this band */
410 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
412 /* decode block huffman codebook */
413 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
416 band->checksum_present = get_bits1(&ctx->gb);
417 if (band->checksum_present)
418 band->checksum = get_bits(&ctx->gb, 16);
420 band->glob_quant = get_bits(&ctx->gb, 5);
422 /* skip unknown extension if any */
423 if (band_flags & 0x20) { /* XXX: untested */
424 align_get_bits(&ctx->gb);
425 skip_hdr_extension(&ctx->gb);
428 align_get_bits(&ctx->gb);
435 * Decode info (block type, cbp, quant delta, motion vector)
436 * for all macroblocks in the current tile.
438 * @param[in,out] ctx ptr to the decoder context
439 * @param[in,out] band ptr to the band descriptor
440 * @param[in,out] tile ptr to the tile descriptor
441 * @param[in] avctx ptr to the AVCodecContext
442 * @return result code: 0 = OK, -1 = error
444 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
445 IVITile *tile, AVCodecContext *avctx)
447 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
448 mv_scale, blks_per_mb;
449 IVIMbInfo *mb, *ref_mb;
450 int row_offset = band->mb_size * band->pitch;
453 ref_mb = tile->ref_mbs;
454 offs = tile->ypos * band->pitch + tile->xpos;
457 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
458 return AVERROR_INVALIDDATA;
460 /* scale factor for motion vectors */
461 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
464 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
467 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
470 mb->buf_offs = mb_offset;
472 if (get_bits1(&ctx->gb)) {
473 if (ctx->frame_type == FRAMETYPE_INTRA) {
474 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
477 mb->type = 1; /* empty macroblocks are always INTER */
478 mb->cbp = 0; /* all blocks are empty */
481 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
482 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
484 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
487 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
488 if (band->inherit_mv){
489 /* motion vector inheritance */
491 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
492 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
494 mb->mv_x = ref_mb->mv_x;
495 mb->mv_y = ref_mb->mv_y;
499 if (band->inherit_mv) {
500 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
501 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
502 mb->type = 0; /* mb_type is always INTRA for intra-frames */
504 mb->type = get_bits1(&ctx->gb);
507 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
508 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
511 if (band->qdelta_present) {
512 if (band->inherit_qdelta) {
513 if (ref_mb) mb->q_delta = ref_mb->q_delta;
514 } else if (mb->cbp || (!band->plane && !band->band_num &&
515 (ctx->frame_flags & 8))) {
516 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
518 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
523 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
525 if (band->inherit_mv){
526 /* motion vector inheritance */
528 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
529 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
531 mb->mv_x = ref_mb->mv_x;
532 mb->mv_y = ref_mb->mv_y;
535 /* decode motion vector deltas */
536 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
538 mv_y += IVI_TOSIGNED(mv_delta);
539 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
541 mv_x += IVI_TOSIGNED(mv_delta);
551 mb_offset += band->mb_size;
557 align_get_bits(&ctx->gb);
564 * Decode an Indeo5 band.
566 * @param[in,out] ctx ptr to the decoder context
567 * @param[in,out] band ptr to the band descriptor
568 * @param[in] avctx ptr to the AVCodecContext
569 * @return result code: 0 = OK, -1 = error
571 static int decode_band(IVI5DecContext *ctx, int plane_num,
572 IVIBandDesc *band, AVCodecContext *avctx)
574 int result, i, t, idx1, idx2, pos;
577 band->buf = band->bufs[ctx->dst_buf];
578 band->ref_buf = band->bufs[ctx->ref_buf];
579 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
581 result = decode_band_hdr(ctx, band, avctx);
583 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
588 if (band->is_empty) {
589 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
593 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
595 /* apply corrections to the selected rvmap table if present */
596 for (i = 0; i < band->num_corr; i++) {
597 idx1 = band->corr[i*2];
598 idx2 = band->corr[i*2+1];
599 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
600 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
603 pos = get_bits_count(&ctx->gb);
605 for (t = 0; t < band->num_tiles; t++) {
606 tile = &band->tiles[t];
608 tile->is_empty = get_bits1(&ctx->gb);
609 if (tile->is_empty) {
610 ff_ivi_process_empty_tile(avctx, band, tile,
611 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
613 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
615 result = decode_mb_info(ctx, band, tile, avctx);
619 result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
620 if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
621 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
624 pos += tile->data_size << 3; // skip to next tile
628 /* restore the selected rvmap table by applying its corrections in reverse order */
629 for (i = band->num_corr-1; i >= 0; i--) {
630 idx1 = band->corr[i*2];
631 idx2 = band->corr[i*2+1];
632 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
633 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
637 if (band->checksum_present) {
638 uint16_t chksum = ivi_calc_band_checksum(band);
639 if (chksum != band->checksum) {
640 av_log(avctx, AV_LOG_ERROR,
641 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
642 band->plane, band->band_num, band->checksum, chksum);
647 align_get_bits(&ctx->gb);
656 * @param[in,out] ctx ptr to the decoder context
658 static void switch_buffers(IVI5DecContext *ctx)
660 switch (ctx->prev_frame_type) {
661 case FRAMETYPE_INTRA:
662 case FRAMETYPE_INTER:
663 ctx->buf_switch ^= 1;
664 ctx->dst_buf = ctx->buf_switch;
665 ctx->ref_buf = ctx->buf_switch ^ 1;
667 case FRAMETYPE_INTER_SCAL:
668 if (!ctx->inter_scal) {
672 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
673 ctx->ref_buf = ctx->ref2_buf;
675 case FRAMETYPE_INTER_NOREF:
679 switch (ctx->frame_type) {
680 case FRAMETYPE_INTRA:
683 case FRAMETYPE_INTER:
685 ctx->dst_buf = ctx->buf_switch;
686 ctx->ref_buf = ctx->buf_switch ^ 1;
688 case FRAMETYPE_INTER_SCAL:
689 case FRAMETYPE_INTER_NOREF:
697 * Initialize Indeo5 decoder.
699 static av_cold int decode_init(AVCodecContext *avctx)
701 IVI5DecContext *ctx = avctx->priv_data;
704 ff_ivi_init_static_vlc();
706 /* copy rvmap tables in our context so we can apply changes to them */
707 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
709 /* set the initial picture layout according to the basic profile:
710 there is only one band per plane (no scalability), only one tile (no local decoding)
711 and picture format = YVU9 */
712 ctx->pic_conf.pic_width = avctx->width;
713 ctx->pic_conf.pic_height = avctx->height;
714 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
715 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
716 ctx->pic_conf.tile_width = avctx->width;
717 ctx->pic_conf.tile_height = avctx->height;
718 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
720 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
722 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
729 avctx->pix_fmt = PIX_FMT_YUV410P;
736 * main decoder function
738 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
741 IVI5DecContext *ctx = avctx->priv_data;
742 const uint8_t *buf = avpkt->data;
743 int buf_size = avpkt->size;
746 init_get_bits(&ctx->gb, buf, buf_size * 8);
747 ctx->frame_data = buf;
748 ctx->frame_size = buf_size;
750 result = decode_pic_hdr(ctx, avctx);
752 av_log(avctx, AV_LOG_ERROR,
753 "Error while decoding picture header: %d\n", result);
757 if (ctx->gop_flags & IVI5_IS_PROTECTED) {
758 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
766 if (ctx->frame_type != FRAMETYPE_NULL) {
767 for (p = 0; p < 3; p++) {
768 for (b = 0; b < ctx->planes[p].num_bands; b++) {
769 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
771 av_log(avctx, AV_LOG_ERROR,
772 "Error while decoding band: %d, plane: %d\n", b, p);
779 //STOP_TIMER("decode_planes"); }
781 if (ctx->frame.data[0])
782 avctx->release_buffer(avctx, &ctx->frame);
784 ctx->frame.reference = 0;
785 if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
786 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
790 if (ctx->is_scalable) {
791 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
793 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
796 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
797 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
799 *data_size = sizeof(AVFrame);
800 *(AVFrame*)data = ctx->frame;
807 * Close Indeo5 decoder and clean up its context.
809 static av_cold int decode_close(AVCodecContext *avctx)
811 IVI5DecContext *ctx = avctx->priv_data;
813 ff_ivi_free_buffers(&ctx->planes[0]);
815 if (ctx->mb_vlc.cust_tab.table)
816 ff_free_vlc(&ctx->mb_vlc.cust_tab);
818 if (ctx->frame.data[0])
819 avctx->release_buffer(avctx, &ctx->frame);
825 AVCodec ff_indeo5_decoder = {
827 .type = AVMEDIA_TYPE_VIDEO,
828 .id = CODEC_ID_INDEO5,
829 .priv_data_size = sizeof(IVI5DecContext),
831 .close = decode_close,
832 .decode = decode_frame,
833 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),