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
52 * Decode Indeo5 GOP (Group of pictures) header.
53 * This header is present in key frames only.
54 * It defines parameters for all frames in a GOP.
56 * @param[in,out] ctx ptr to the decoder context
57 * @param[in] avctx ptr to the AVCodecContext
58 * @return result code: 0 = OK, -1 = error
60 static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
62 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
63 int quant_mat, blk_size_changed = 0;
64 IVIBandDesc *band, *band1, *band2;
65 IVIPicConfig pic_conf;
67 ctx->gop_flags = get_bits(&ctx->gb, 8);
69 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
71 if (ctx->gop_flags & IVI5_IS_PROTECTED)
72 ctx->lock_word = get_bits_long(&ctx->gb, 32);
74 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
75 if (tile_size > 256) {
76 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
77 return AVERROR_INVALIDDATA;
80 /* decode number of wavelet bands */
81 /* num_levels * 3 + 1 */
82 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
83 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
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);
88 return AVERROR_INVALIDDATA;
91 pic_size_indx = get_bits(&ctx->gb, 4);
92 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
93 pic_conf.pic_height = get_bits(&ctx->gb, 13);
94 pic_conf.pic_width = get_bits(&ctx->gb, 13);
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;
100 if (ctx->gop_flags & 2) {
101 avpriv_report_missing_feature(avctx, "YV12 picture format");
102 return AVERROR_PATCHWELCOME;
105 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
106 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
109 pic_conf.tile_height = pic_conf.pic_height;
110 pic_conf.tile_width = pic_conf.pic_width;
112 pic_conf.tile_height = pic_conf.tile_width = tile_size;
115 /* check if picture layout was changed and reallocate buffers */
116 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
117 result = ff_ivi_init_planes(ctx->planes, &pic_conf);
119 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
122 ctx->pic_conf = pic_conf;
123 blk_size_changed = 1; /* force reallocation of the internal structures */
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];
130 band->is_halfpel = get_bits1(&ctx->gb);
132 mb_size = get_bits1(&ctx->gb);
133 blk_size = 8 >> get_bits1(&ctx->gb);
134 mb_size = blk_size << !mb_size;
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;
142 if (get_bits1(&ctx->gb)) {
143 avpriv_report_missing_feature(avctx, "Extended transform info");
144 return AVERROR_PATCHWELCOME;
147 /* select transform function and scan pattern according to plane and band number */
148 switch ((p << 2) + i) {
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;
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;
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;
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;
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;
185 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
186 band->inv_transform == ff_ivi_inverse_slant_4x4;
188 if (band->transform_size != band->blk_size)
189 return AVERROR_INVALIDDATA;
191 /* select dequant matrix according to plane and band number */
193 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
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];
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;
210 if (get_bits(&ctx->gb, 2)) {
211 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
212 return AVERROR_INVALIDDATA;
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];
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;
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;
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;
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);
242 av_log(avctx, AV_LOG_ERROR,
243 "Couldn't reallocate internal structures!\n");
248 if (ctx->gop_flags & 8) {
249 if (get_bits(&ctx->gb, 3)) {
250 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
251 return AVERROR_INVALIDDATA;
254 if (get_bits1(&ctx->gb))
255 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
258 align_get_bits(&ctx->gb);
260 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
262 /* skip GOP extension if any */
263 if (get_bits1(&ctx->gb)) {
265 i = get_bits(&ctx->gb, 16);
266 } while (i & 0x8000);
269 align_get_bits(&ctx->gb);
276 * Skip a header extension.
278 * @param[in,out] gb the GetBit context
280 static inline void skip_hdr_extension(GetBitContext *gb)
285 len = get_bits(gb, 8);
286 for (i = 0; i < len; i++) skip_bits(gb, 8);
292 * Decode Indeo5 picture header.
294 * @param[in,out] ctx ptr to the decoder context
295 * @param[in] avctx ptr to the AVCodecContext
296 * @return result code: 0 = OK, -1 = error
298 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
302 if (get_bits(&ctx->gb, 5) != 0x1F) {
303 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
304 return AVERROR_INVALIDDATA;
307 ctx->prev_frame_type = ctx->frame_type;
308 ctx->frame_type = get_bits(&ctx->gb, 3);
309 if (ctx->frame_type >= 5) {
310 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
311 return AVERROR_INVALIDDATA;
314 ctx->frame_num = get_bits(&ctx->gb, 8);
316 if (ctx->frame_type == FRAMETYPE_INTRA) {
317 if ((ret = decode_gop_header(ctx, avctx)) < 0) {
318 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
319 ctx->gop_invalid = 1;
322 ctx->gop_invalid = 0;
325 if (ctx->frame_type != FRAMETYPE_NULL) {
326 ctx->frame_flags = get_bits(&ctx->gb, 8);
328 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
330 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
332 /* skip unknown extension if any */
333 if (ctx->frame_flags & 0x20)
334 skip_hdr_extension(&ctx->gb); /* XXX: untested */
336 /* decode macroblock huffman codebook */
337 ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40,
338 IVI_MB_HUFF, &ctx->mb_vlc, avctx);
342 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
345 align_get_bits(&ctx->gb);
352 * Decode Indeo5 band header.
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
357 * @return result code: 0 = OK, -1 = error
359 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
360 AVCodecContext *avctx)
365 band_flags = get_bits(&ctx->gb, 8);
367 if (band_flags & 1) {
372 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
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;
379 /* decode rvmap probability corrections if any */
380 band->num_corr = 0; /* there are no corrections */
381 if (band_flags & 0x10) {
382 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
383 if (band->num_corr > 61) {
384 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
386 return AVERROR_INVALIDDATA;
389 /* read correction pairs */
390 for (i = 0; i < band->num_corr * 2; i++)
391 band->corr[i] = get_bits(&ctx->gb, 8);
394 /* select appropriate rvmap table for this band */
395 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
397 /* decode block huffman codebook */
398 ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF,
399 &band->blk_vlc, avctx);
403 band->checksum_present = get_bits1(&ctx->gb);
404 if (band->checksum_present)
405 band->checksum = get_bits(&ctx->gb, 16);
407 band->glob_quant = get_bits(&ctx->gb, 5);
409 /* skip unknown extension if any */
410 if (band_flags & 0x20) { /* XXX: untested */
411 align_get_bits(&ctx->gb);
412 skip_hdr_extension(&ctx->gb);
415 align_get_bits(&ctx->gb);
422 * Decode info (block type, cbp, quant delta, motion vector)
423 * for all macroblocks in the current tile.
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
429 * @return result code: 0 = OK, -1 = error
431 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
432 IVITile *tile, AVCodecContext *avctx)
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;
440 ref_mb = tile->ref_mbs;
441 offs = tile->ypos * band->pitch + tile->xpos;
444 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
445 return AVERROR_INVALIDDATA;
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;
453 /* scale factor for motion vectors */
454 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
457 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
460 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
463 mb->buf_offs = mb_offset;
465 if (get_bits1(&ctx->gb)) {
466 if (ctx->frame_type == FRAMETYPE_INTRA) {
467 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
468 return AVERROR_INVALIDDATA;
470 mb->type = 1; /* empty macroblocks are always INTER */
471 mb->cbp = 0; /* all blocks are empty */
474 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
475 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
477 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
480 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
481 if (band->inherit_mv){
482 /* motion vector inheritance */
484 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
485 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
487 mb->mv_x = ref_mb->mv_x;
488 mb->mv_y = ref_mb->mv_y;
492 if (band->inherit_mv) {
493 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
494 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
495 mb->type = 0; /* mb_type is always INTRA for intra-frames */
497 mb->type = get_bits1(&ctx->gb);
500 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
501 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
504 if (band->qdelta_present) {
505 if (band->inherit_qdelta) {
506 if (ref_mb) mb->q_delta = ref_mb->q_delta;
507 } else if (mb->cbp || (!band->plane && !band->band_num &&
508 (ctx->frame_flags & 8))) {
509 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
511 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
516 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
518 if (band->inherit_mv){
519 /* motion vector inheritance */
521 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
522 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
524 mb->mv_x = ref_mb->mv_x;
525 mb->mv_y = ref_mb->mv_y;
528 /* decode motion vector deltas */
529 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
531 mv_y += IVI_TOSIGNED(mv_delta);
532 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
534 mv_x += IVI_TOSIGNED(mv_delta);
544 mb_offset += band->mb_size;
550 align_get_bits(&ctx->gb);
559 * @param[in,out] ctx ptr to the decoder context
561 static void switch_buffers(IVI45DecContext *ctx)
563 switch (ctx->prev_frame_type) {
564 case FRAMETYPE_INTRA:
565 case FRAMETYPE_INTER:
566 ctx->buf_switch ^= 1;
567 ctx->dst_buf = ctx->buf_switch;
568 ctx->ref_buf = ctx->buf_switch ^ 1;
570 case FRAMETYPE_INTER_SCAL:
571 if (!ctx->inter_scal) {
575 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
576 ctx->ref_buf = ctx->ref2_buf;
578 case FRAMETYPE_INTER_NOREF:
582 switch (ctx->frame_type) {
583 case FRAMETYPE_INTRA:
586 case FRAMETYPE_INTER:
588 ctx->dst_buf = ctx->buf_switch;
589 ctx->ref_buf = ctx->buf_switch ^ 1;
591 case FRAMETYPE_INTER_SCAL:
592 case FRAMETYPE_INTER_NOREF:
599 static int is_nonnull_frame(IVI45DecContext *ctx)
601 return ctx->frame_type != FRAMETYPE_NULL;
606 * Initialize Indeo5 decoder.
608 static av_cold int decode_init(AVCodecContext *avctx)
610 IVI45DecContext *ctx = avctx->priv_data;
613 ff_ivi_init_static_vlc();
615 /* copy rvmap tables in our context so we can apply changes to them */
616 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
618 /* set the initial picture layout according to the basic profile:
619 there is only one band per plane (no scalability), only one tile (no local decoding)
620 and picture format = YVU9 */
621 ctx->pic_conf.pic_width = avctx->width;
622 ctx->pic_conf.pic_height = avctx->height;
623 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
624 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
625 ctx->pic_conf.tile_width = avctx->width;
626 ctx->pic_conf.tile_height = avctx->height;
627 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
629 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
631 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
632 return AVERROR_INVALIDDATA;
638 ctx->decode_pic_hdr = decode_pic_hdr;
639 ctx->decode_band_hdr = decode_band_hdr;
640 ctx->decode_mb_info = decode_mb_info;
641 ctx->switch_buffers = switch_buffers;
642 ctx->is_nonnull_frame = is_nonnull_frame;
644 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
650 AVCodec ff_indeo5_decoder = {
652 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
653 .type = AVMEDIA_TYPE_VIDEO,
654 .id = AV_CODEC_ID_INDEO5,
655 .priv_data_size = sizeof(IVI45DecContext),
657 .close = ff_ivi_decode_close,
658 .decode = ff_ivi_decode_frame,
659 .capabilities = CODEC_CAP_DR1,