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
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, is_scalable;
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);
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 is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
85 if (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);
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 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
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)) {
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 ctx->is_scalable = is_scalable;
124 blk_size_changed = 1; /* force reallocation of the internal structures */
127 for (p = 0; p <= 1; p++) {
128 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
129 band = &ctx->planes[p].bands[i];
131 band->is_halfpel = get_bits1(&ctx->gb);
133 mb_size = get_bits1(&ctx->gb);
134 blk_size = 8 >> get_bits1(&ctx->gb);
135 mb_size = blk_size << !mb_size;
137 if (p==0 && blk_size==4) {
138 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
139 return AVERROR_PATCHWELCOME;
142 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
143 if (blk_size_changed) {
144 band->mb_size = mb_size;
145 band->blk_size = blk_size;
148 if (get_bits1(&ctx->gb)) {
149 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
153 /* select transform function and scan pattern according to plane and band number */
154 switch ((p << 2) + i) {
156 band->inv_transform = ff_ivi_inverse_slant_8x8;
157 band->dc_transform = ff_ivi_dc_slant_2d;
158 band->scan = ff_zigzag_direct;
159 band->transform_size= 8;
163 band->inv_transform = ff_ivi_row_slant8;
164 band->dc_transform = ff_ivi_dc_row_slant;
165 band->scan = ff_ivi_vertical_scan_8x8;
166 band->transform_size= 8;
170 band->inv_transform = ff_ivi_col_slant8;
171 band->dc_transform = ff_ivi_dc_col_slant;
172 band->scan = ff_ivi_horizontal_scan_8x8;
173 band->transform_size= 8;
177 band->inv_transform = ff_ivi_put_pixels_8x8;
178 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
179 band->scan = ff_ivi_horizontal_scan_8x8;
180 band->transform_size= 8;
184 band->inv_transform = ff_ivi_inverse_slant_4x4;
185 band->dc_transform = ff_ivi_dc_slant_2d;
186 band->scan = ff_ivi_direct_scan_4x4;
187 band->transform_size= 4;
191 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
192 band->inv_transform == ff_ivi_inverse_slant_4x4;
194 /* select dequant matrix according to plane and band number */
196 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
201 if (band->blk_size == 8) {
203 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
206 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
207 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
208 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
209 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
211 band->intra_base = ivi5_base_quant_4x4_intra;
212 band->inter_base = ivi5_base_quant_4x4_inter;
213 band->intra_scale = ivi5_scale_quant_4x4_intra;
214 band->inter_scale = ivi5_scale_quant_4x4_inter;
217 if (get_bits(&ctx->gb, 2)) {
218 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
224 /* copy chroma parameters into the 2nd chroma plane */
225 for (i = 0; i < pic_conf.chroma_bands; i++) {
226 band1 = &ctx->planes[1].bands[i];
227 band2 = &ctx->planes[2].bands[i];
229 band2->width = band1->width;
230 band2->height = band1->height;
231 band2->mb_size = band1->mb_size;
232 band2->blk_size = band1->blk_size;
233 band2->is_halfpel = band1->is_halfpel;
234 band2->intra_base = band1->intra_base;
235 band2->inter_base = band1->inter_base;
236 band2->intra_scale = band1->intra_scale;
237 band2->inter_scale = band1->inter_scale;
238 band2->scan = band1->scan;
239 band2->inv_transform = band1->inv_transform;
240 band2->dc_transform = band1->dc_transform;
241 band2->is_2d_trans = band1->is_2d_trans;
242 band2->transform_size= band1->transform_size;
245 /* reallocate internal structures if needed */
246 if (blk_size_changed) {
247 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
248 pic_conf.tile_height);
250 av_log(avctx, AV_LOG_ERROR,
251 "Couldn't reallocate internal structures!\n");
256 if (ctx->gop_flags & 8) {
257 if (get_bits(&ctx->gb, 3)) {
258 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
262 if (get_bits1(&ctx->gb))
263 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
266 align_get_bits(&ctx->gb);
268 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
270 /* skip GOP extension if any */
271 if (get_bits1(&ctx->gb)) {
273 i = get_bits(&ctx->gb, 16);
274 } while (i & 0x8000);
277 align_get_bits(&ctx->gb);
284 * Skip a header extension.
286 * @param[in,out] gb the GetBit context
288 static inline void skip_hdr_extension(GetBitContext *gb)
293 len = get_bits(gb, 8);
294 for (i = 0; i < len; i++) skip_bits(gb, 8);
300 * Decode Indeo5 picture header.
302 * @param[in,out] ctx ptr to the decoder context
303 * @param[in] avctx ptr to the AVCodecContext
304 * @return result code: 0 = OK, -1 = error
306 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
308 if (get_bits(&ctx->gb, 5) != 0x1F) {
309 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
313 ctx->prev_frame_type = ctx->frame_type;
314 ctx->frame_type = get_bits(&ctx->gb, 3);
315 if (ctx->frame_type >= 5) {
316 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
320 ctx->frame_num = get_bits(&ctx->gb, 8);
322 if (ctx->frame_type == FRAMETYPE_INTRA) {
323 ctx->gop_invalid = 1;
324 if (decode_gop_header(ctx, avctx)) {
325 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
326 return AVERROR_INVALIDDATA;
328 ctx->gop_invalid = 0;
331 if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
332 av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scaleable stream\n");
333 ctx->frame_type = FRAMETYPE_INTER;
334 return AVERROR_INVALIDDATA;
337 if (ctx->frame_type != FRAMETYPE_NULL) {
338 ctx->frame_flags = get_bits(&ctx->gb, 8);
340 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
342 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
344 /* skip unknown extension if any */
345 if (ctx->frame_flags & 0x20)
346 skip_hdr_extension(&ctx->gb); /* XXX: untested */
348 /* decode macroblock huffman codebook */
349 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
352 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
355 align_get_bits(&ctx->gb);
362 * Decode Indeo5 band header.
364 * @param[in,out] ctx ptr to the decoder context
365 * @param[in,out] band ptr to the band descriptor
366 * @param[in] avctx ptr to the AVCodecContext
367 * @return result code: 0 = OK, -1 = error
369 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
370 AVCodecContext *avctx)
375 band_flags = get_bits(&ctx->gb, 8);
377 if (band_flags & 1) {
382 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
384 band->inherit_mv = band_flags & 2;
385 band->inherit_qdelta = band_flags & 8;
386 band->qdelta_present = band_flags & 4;
387 if (!band->qdelta_present) band->inherit_qdelta = 1;
389 /* decode rvmap probability corrections if any */
390 band->num_corr = 0; /* there are no corrections */
391 if (band_flags & 0x10) {
392 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
393 if (band->num_corr > 61) {
394 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
399 /* read correction pairs */
400 for (i = 0; i < band->num_corr * 2; i++)
401 band->corr[i] = get_bits(&ctx->gb, 8);
404 /* select appropriate rvmap table for this band */
405 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
407 /* decode block huffman codebook */
408 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
411 band->checksum_present = get_bits1(&ctx->gb);
412 if (band->checksum_present)
413 band->checksum = get_bits(&ctx->gb, 16);
415 band->glob_quant = get_bits(&ctx->gb, 5);
417 /* skip unknown extension if any */
418 if (band_flags & 0x20) { /* XXX: untested */
419 align_get_bits(&ctx->gb);
420 skip_hdr_extension(&ctx->gb);
423 align_get_bits(&ctx->gb);
430 * Decode info (block type, cbp, quant delta, motion vector)
431 * for all macroblocks in the current tile.
433 * @param[in,out] ctx ptr to the decoder context
434 * @param[in,out] band ptr to the band descriptor
435 * @param[in,out] tile ptr to the tile descriptor
436 * @param[in] avctx ptr to the AVCodecContext
437 * @return result code: 0 = OK, -1 = error
439 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
440 IVITile *tile, AVCodecContext *avctx)
442 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
443 mv_scale, blks_per_mb, s;
444 IVIMbInfo *mb, *ref_mb;
445 int row_offset = band->mb_size * band->pitch;
448 ref_mb = tile->ref_mbs;
449 offs = tile->ypos * band->pitch + tile->xpos;
452 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
453 return AVERROR_INVALIDDATA;
455 if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
456 av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
457 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
458 return AVERROR_INVALIDDATA;
461 /* scale factor for motion vectors */
462 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
465 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
468 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
471 mb->buf_offs = mb_offset;
473 if (get_bits1(&ctx->gb)) {
474 if (ctx->frame_type == FRAMETYPE_INTRA) {
475 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
478 mb->type = 1; /* empty macroblocks are always INTER */
479 mb->cbp = 0; /* all blocks are empty */
482 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
483 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
485 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
488 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
489 if (band->inherit_mv && ref_mb){
490 /* motion vector inheritance */
492 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
493 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
495 mb->mv_x = ref_mb->mv_x;
496 mb->mv_y = ref_mb->mv_y;
500 if (band->inherit_mv && ref_mb) {
501 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
502 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
503 mb->type = 0; /* mb_type is always INTRA for intra-frames */
505 mb->type = get_bits1(&ctx->gb);
508 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
509 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
512 if (band->qdelta_present) {
513 if (band->inherit_qdelta) {
514 if (ref_mb) mb->q_delta = ref_mb->q_delta;
515 } else if (mb->cbp || (!band->plane && !band->band_num &&
516 (ctx->frame_flags & 8))) {
517 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
519 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
524 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
526 if (band->inherit_mv && ref_mb){
527 /* motion vector inheritance */
529 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
530 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
532 mb->mv_x = ref_mb->mv_x;
533 mb->mv_y = ref_mb->mv_y;
536 /* decode motion vector deltas */
537 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
539 mv_y += IVI_TOSIGNED(mv_delta);
540 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
542 mv_x += IVI_TOSIGNED(mv_delta);
551 if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
552 x + ((mb->mv_x+s)>>s) + band->mb_size - 1
553 + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
554 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
555 return AVERROR_INVALIDDATA;
561 mb_offset += band->mb_size;
567 align_get_bits(&ctx->gb);
576 * @param[in,out] ctx ptr to the decoder context
578 static void switch_buffers(IVI45DecContext *ctx)
580 switch (ctx->prev_frame_type) {
581 case FRAMETYPE_INTRA:
582 case FRAMETYPE_INTER:
583 ctx->buf_switch ^= 1;
584 ctx->dst_buf = ctx->buf_switch;
585 ctx->ref_buf = ctx->buf_switch ^ 1;
587 case FRAMETYPE_INTER_SCAL:
588 if (!ctx->inter_scal) {
592 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
593 ctx->ref_buf = ctx->ref2_buf;
595 case FRAMETYPE_INTER_NOREF:
599 switch (ctx->frame_type) {
600 case FRAMETYPE_INTRA:
603 case FRAMETYPE_INTER:
605 ctx->dst_buf = ctx->buf_switch;
606 ctx->ref_buf = ctx->buf_switch ^ 1;
608 case FRAMETYPE_INTER_SCAL:
609 case FRAMETYPE_INTER_NOREF:
616 static int is_nonnull_frame(IVI45DecContext *ctx)
618 return ctx->frame_type != FRAMETYPE_NULL;
623 * Initialize Indeo5 decoder.
625 static av_cold int decode_init(AVCodecContext *avctx)
627 IVI45DecContext *ctx = avctx->priv_data;
630 ff_ivi_init_static_vlc();
632 /* copy rvmap tables in our context so we can apply changes to them */
633 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
635 /* set the initial picture layout according to the basic profile:
636 there is only one band per plane (no scalability), only one tile (no local decoding)
637 and picture format = YVU9 */
638 ctx->pic_conf.pic_width = avctx->width;
639 ctx->pic_conf.pic_height = avctx->height;
640 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
641 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
642 ctx->pic_conf.tile_width = avctx->width;
643 ctx->pic_conf.tile_height = avctx->height;
644 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
646 avcodec_get_frame_defaults(&ctx->frame);
648 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
650 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
657 ctx->decode_pic_hdr = decode_pic_hdr;
658 ctx->decode_band_hdr = decode_band_hdr;
659 ctx->decode_mb_info = decode_mb_info;
660 ctx->switch_buffers = switch_buffers;
661 ctx->is_nonnull_frame = is_nonnull_frame;
663 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
668 AVCodec ff_indeo5_decoder = {
670 .type = AVMEDIA_TYPE_VIDEO,
671 .id = AV_CODEC_ID_INDEO5,
672 .priv_data_size = sizeof(IVI45DecContext),
674 .close = ff_ivi_decode_close,
675 .decode = ff_ivi_decode_frame,
676 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
677 .capabilities = CODEC_CAP_DR1,