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 "indeo5data.h"
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
48 #define IVI5_PIC_SIZE_ESC 15
51 * Decode Indeo5 GOP (Group of pictures) header.
52 * This header is present in key frames only.
53 * It defines parameters for all frames in a GOP.
55 * @param[in,out] ctx ptr to the decoder context
56 * @param[in] avctx ptr to the AVCodecContext
57 * @return result code: 0 = OK, -1 = error
59 static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
61 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
62 int quant_mat, blk_size_changed = 0;
63 IVIBandDesc *band, *band1, *band2;
64 IVIPicConfig pic_conf;
66 ctx->gop_flags = get_bits(&ctx->gb, 8);
68 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
70 if (ctx->gop_flags & IVI5_IS_PROTECTED)
71 ctx->lock_word = get_bits_long(&ctx->gb, 32);
73 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
74 if (tile_size > 256) {
75 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
76 return AVERROR_INVALIDDATA;
79 /* decode number of wavelet bands */
80 /* num_levels * 3 + 1 */
81 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
82 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
83 is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
84 if (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);
87 return AVERROR_INVALIDDATA;
90 pic_size_indx = get_bits(&ctx->gb, 4);
91 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
92 pic_conf.pic_height = get_bits(&ctx->gb, 13);
93 pic_conf.pic_width = get_bits(&ctx->gb, 13);
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;
99 if (ctx->gop_flags & 2) {
100 avpriv_report_missing_feature(avctx, "YV12 picture format");
101 return AVERROR_PATCHWELCOME;
104 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
105 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
108 pic_conf.tile_height = pic_conf.pic_height;
109 pic_conf.tile_width = pic_conf.pic_width;
111 pic_conf.tile_height = pic_conf.tile_width = tile_size;
114 /* check if picture layout was changed and reallocate buffers */
115 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
116 result = ff_ivi_init_planes(avctx, ctx->planes, &pic_conf, 0);
118 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
121 ctx->pic_conf = pic_conf;
122 ctx->is_scalable = is_scalable;
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 if (p==0 && blk_size==4) {
137 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
138 return AVERROR_PATCHWELCOME;
141 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
142 if (blk_size_changed) {
143 band->mb_size = mb_size;
144 band->blk_size = blk_size;
147 if (get_bits1(&ctx->gb)) {
148 avpriv_report_missing_feature(avctx, "Extended transform info");
149 return AVERROR_PATCHWELCOME;
152 /* select transform function and scan pattern according to plane and band number */
153 switch ((p << 2) + i) {
155 band->inv_transform = ff_ivi_inverse_slant_8x8;
156 band->dc_transform = ff_ivi_dc_slant_2d;
157 band->scan = ff_zigzag_direct;
158 band->transform_size = 8;
162 band->inv_transform = ff_ivi_row_slant8;
163 band->dc_transform = ff_ivi_dc_row_slant;
164 band->scan = ff_ivi_vertical_scan_8x8;
165 band->transform_size = 8;
169 band->inv_transform = ff_ivi_col_slant8;
170 band->dc_transform = ff_ivi_dc_col_slant;
171 band->scan = ff_ivi_horizontal_scan_8x8;
172 band->transform_size = 8;
176 band->inv_transform = ff_ivi_put_pixels_8x8;
177 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
178 band->scan = ff_ivi_horizontal_scan_8x8;
179 band->transform_size = 8;
183 band->inv_transform = ff_ivi_inverse_slant_4x4;
184 band->dc_transform = ff_ivi_dc_slant_2d;
185 band->scan = ff_ivi_direct_scan_4x4;
186 band->transform_size = 4;
190 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
191 band->inv_transform == ff_ivi_inverse_slant_4x4;
193 if (band->transform_size != band->blk_size) {
194 av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
195 return AVERROR_INVALIDDATA;
198 /* select dequant matrix according to plane and band number */
200 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
205 if (band->blk_size == 8) {
207 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
210 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
211 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
212 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
213 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
215 band->intra_base = ivi5_base_quant_4x4_intra;
216 band->inter_base = ivi5_base_quant_4x4_inter;
217 band->intra_scale = ivi5_scale_quant_4x4_intra;
218 band->inter_scale = ivi5_scale_quant_4x4_inter;
221 if (get_bits(&ctx->gb, 2)) {
222 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
223 return AVERROR_INVALIDDATA;
228 /* copy chroma parameters into the 2nd chroma plane */
229 for (i = 0; i < pic_conf.chroma_bands; i++) {
230 band1 = &ctx->planes[1].bands[i];
231 band2 = &ctx->planes[2].bands[i];
233 band2->width = band1->width;
234 band2->height = band1->height;
235 band2->mb_size = band1->mb_size;
236 band2->blk_size = band1->blk_size;
237 band2->is_halfpel = band1->is_halfpel;
238 band2->intra_base = band1->intra_base;
239 band2->inter_base = band1->inter_base;
240 band2->intra_scale = band1->intra_scale;
241 band2->inter_scale = band1->inter_scale;
242 band2->scan = band1->scan;
243 band2->inv_transform = band1->inv_transform;
244 band2->dc_transform = band1->dc_transform;
245 band2->is_2d_trans = band1->is_2d_trans;
246 band2->transform_size= band1->transform_size;
249 /* reallocate internal structures if needed */
250 if (blk_size_changed) {
251 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
252 pic_conf.tile_height);
254 av_log(avctx, AV_LOG_ERROR,
255 "Couldn't reallocate internal structures!\n");
260 if (ctx->gop_flags & 8) {
261 if (get_bits(&ctx->gb, 3)) {
262 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
263 return AVERROR_INVALIDDATA;
266 if (get_bits1(&ctx->gb))
267 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
270 align_get_bits(&ctx->gb);
272 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
274 /* skip GOP extension if any */
275 if (get_bits1(&ctx->gb)) {
277 i = get_bits(&ctx->gb, 16);
278 } while (i & 0x8000);
281 align_get_bits(&ctx->gb);
288 * Skip a header extension.
290 * @param[in,out] gb the GetBit context
292 static inline int skip_hdr_extension(GetBitContext *gb)
297 len = get_bits(gb, 8);
298 if (8*len > get_bits_left(gb))
299 return AVERROR_INVALIDDATA;
300 for (i = 0; i < len; i++) skip_bits(gb, 8);
308 * Decode Indeo5 picture header.
310 * @param[in,out] ctx ptr to the decoder context
311 * @param[in] avctx ptr to the AVCodecContext
312 * @return result code: 0 = OK, -1 = error
314 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
318 if (get_bits(&ctx->gb, 5) != 0x1F) {
319 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
320 return AVERROR_INVALIDDATA;
323 ctx->prev_frame_type = ctx->frame_type;
324 ctx->frame_type = get_bits(&ctx->gb, 3);
325 if (ctx->frame_type >= 5) {
326 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
327 ctx->frame_type = FRAMETYPE_INTRA;
328 return AVERROR_INVALIDDATA;
331 ctx->frame_num = get_bits(&ctx->gb, 8);
333 if (ctx->frame_type == FRAMETYPE_INTRA) {
334 if ((ret = decode_gop_header(ctx, avctx)) < 0) {
335 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
336 ctx->gop_invalid = 1;
339 ctx->gop_invalid = 0;
342 if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
343 av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scalable stream\n");
344 ctx->frame_type = FRAMETYPE_INTER;
345 return AVERROR_INVALIDDATA;
348 if (ctx->frame_type != FRAMETYPE_NULL) {
349 ctx->frame_flags = get_bits(&ctx->gb, 8);
351 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
353 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
355 /* skip unknown extension if any */
356 if (ctx->frame_flags & 0x20)
357 skip_hdr_extension(&ctx->gb); /* XXX: untested */
359 /* decode macroblock huffman codebook */
360 ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40,
361 IVI_MB_HUFF, &ctx->mb_vlc, avctx);
365 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
368 align_get_bits(&ctx->gb);
375 * Decode Indeo5 band header.
377 * @param[in,out] ctx ptr to the decoder context
378 * @param[in,out] band ptr to the band descriptor
379 * @param[in] avctx ptr to the AVCodecContext
380 * @return result code: 0 = OK, -1 = error
382 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
383 AVCodecContext *avctx)
388 band_flags = get_bits(&ctx->gb, 8);
390 if (band_flags & 1) {
395 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
397 band->inherit_mv = band_flags & 2;
398 band->inherit_qdelta = band_flags & 8;
399 band->qdelta_present = band_flags & 4;
400 if (!band->qdelta_present) band->inherit_qdelta = 1;
402 /* decode rvmap probability corrections if any */
403 band->num_corr = 0; /* there are no corrections */
404 if (band_flags & 0x10) {
405 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
406 if (band->num_corr > 61) {
407 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
409 return AVERROR_INVALIDDATA;
412 /* read correction pairs */
413 for (i = 0; i < band->num_corr * 2; i++)
414 band->corr[i] = get_bits(&ctx->gb, 8);
417 /* select appropriate rvmap table for this band */
418 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
420 /* decode block huffman codebook */
421 ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF,
422 &band->blk_vlc, avctx);
426 band->checksum_present = get_bits1(&ctx->gb);
427 if (band->checksum_present)
428 band->checksum = get_bits(&ctx->gb, 16);
430 band->glob_quant = get_bits(&ctx->gb, 5);
432 /* skip unknown extension if any */
433 if (band_flags & 0x20) { /* XXX: untested */
434 align_get_bits(&ctx->gb);
435 skip_hdr_extension(&ctx->gb);
438 align_get_bits(&ctx->gb);
445 * Decode info (block type, cbp, quant delta, motion vector)
446 * for all macroblocks in the current tile.
448 * @param[in,out] ctx ptr to the decoder context
449 * @param[in,out] band ptr to the band descriptor
450 * @param[in,out] tile ptr to the tile descriptor
451 * @param[in] avctx ptr to the AVCodecContext
452 * @return result code: 0 = OK, -1 = error
454 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
455 IVITile *tile, AVCodecContext *avctx)
457 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
458 mv_scale, blks_per_mb, s;
459 IVIMbInfo *mb, *ref_mb;
460 int row_offset = band->mb_size * band->pitch;
463 ref_mb = tile->ref_mbs;
464 offs = tile->ypos * band->pitch + tile->xpos;
467 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
468 return AVERROR_INVALIDDATA;
470 if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
471 av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
472 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
473 return AVERROR_INVALIDDATA;
476 /* scale factor for motion vectors */
477 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
480 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
483 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
486 mb->buf_offs = mb_offset;
488 if (get_bits1(&ctx->gb)) {
489 if (ctx->frame_type == FRAMETYPE_INTRA) {
490 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
491 return AVERROR_INVALIDDATA;
493 mb->type = 1; /* empty macroblocks are always INTER */
494 mb->cbp = 0; /* all blocks are empty */
497 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
498 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
500 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
503 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
504 if (band->inherit_mv && ref_mb){
505 /* motion vector inheritance */
507 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
508 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
510 mb->mv_x = ref_mb->mv_x;
511 mb->mv_y = ref_mb->mv_y;
515 if (band->inherit_mv && ref_mb) {
516 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
517 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
518 mb->type = 0; /* mb_type is always INTRA for intra-frames */
520 mb->type = get_bits1(&ctx->gb);
523 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
524 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
527 if (band->qdelta_present) {
528 if (band->inherit_qdelta) {
529 if (ref_mb) mb->q_delta = ref_mb->q_delta;
530 } else if (mb->cbp || (!band->plane && !band->band_num &&
531 (ctx->frame_flags & 8))) {
532 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
534 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
539 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
541 if (band->inherit_mv && ref_mb){
542 /* motion vector inheritance */
544 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
545 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
547 mb->mv_x = ref_mb->mv_x;
548 mb->mv_y = ref_mb->mv_y;
551 /* decode motion vector deltas */
552 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
554 mv_y += IVI_TOSIGNED(mv_delta);
555 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
557 mv_x += IVI_TOSIGNED(mv_delta);
566 if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
567 x + ((mb->mv_x+s)>>s) + band->mb_size - 1
568 + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
569 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
570 return AVERROR_INVALIDDATA;
576 mb_offset += band->mb_size;
582 align_get_bits(&ctx->gb);
591 * @param[in,out] ctx ptr to the decoder context
593 static void switch_buffers(IVI45DecContext *ctx)
595 switch (ctx->prev_frame_type) {
596 case FRAMETYPE_INTRA:
597 case FRAMETYPE_INTER:
598 ctx->buf_switch ^= 1;
599 ctx->dst_buf = ctx->buf_switch;
600 ctx->ref_buf = ctx->buf_switch ^ 1;
602 case FRAMETYPE_INTER_SCAL:
603 if (!ctx->inter_scal) {
607 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
608 ctx->ref_buf = ctx->ref2_buf;
610 case FRAMETYPE_INTER_NOREF:
614 switch (ctx->frame_type) {
615 case FRAMETYPE_INTRA:
618 case FRAMETYPE_INTER:
620 ctx->dst_buf = ctx->buf_switch;
621 ctx->ref_buf = ctx->buf_switch ^ 1;
623 case FRAMETYPE_INTER_SCAL:
624 case FRAMETYPE_INTER_NOREF:
631 static int is_nonnull_frame(IVI45DecContext *ctx)
633 return ctx->frame_type != FRAMETYPE_NULL;
638 * Initialize Indeo5 decoder.
640 static av_cold int decode_init(AVCodecContext *avctx)
642 IVI45DecContext *ctx = avctx->priv_data;
645 ctx->gop_invalid = 1;
647 ff_ivi_init_static_vlc();
649 /* copy rvmap tables in our context so we can apply changes to them */
650 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
652 /* set the initial picture layout according to the basic profile:
653 there is only one band per plane (no scalability), only one tile (no local decoding)
654 and picture format = YVU9 */
655 ctx->pic_conf.pic_width = avctx->width;
656 ctx->pic_conf.pic_height = avctx->height;
657 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
658 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
659 ctx->pic_conf.tile_width = avctx->width;
660 ctx->pic_conf.tile_height = avctx->height;
661 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
663 result = ff_ivi_init_planes(avctx, ctx->planes, &ctx->pic_conf, 0);
665 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
666 return AVERROR_INVALIDDATA;
672 ctx->decode_pic_hdr = decode_pic_hdr;
673 ctx->decode_band_hdr = decode_band_hdr;
674 ctx->decode_mb_info = decode_mb_info;
675 ctx->switch_buffers = switch_buffers;
676 ctx->is_nonnull_frame = is_nonnull_frame;
680 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
685 AVCodec ff_indeo5_decoder = {
687 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
688 .type = AVMEDIA_TYPE_VIDEO,
689 .id = AV_CODEC_ID_INDEO5,
690 .priv_data_size = sizeof(IVI45DecContext),
692 .close = ff_ivi_decode_close,
693 .decode = ff_ivi_decode_frame,
694 .capabilities = AV_CODEC_CAP_DR1,