1/* 2 * Indeo Video Interactive v5 compatible decoder 3 * Copyright (c) 2009 Maxim Poliakovski 4 * 5 * This file is part of FFmpeg. 6 * 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. 11 * 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. 16 * 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 20 */ 21 22/** 23 * @file 24 * Indeo Video Interactive version 5 decoder 25 * 26 * Indeo5 data is usually transported within .avi or .mov files. 27 * Known FOURCCs: 'IV50' 28 */ 29 30#define ALT_BITSTREAM_READER_LE 31#include "avcodec.h" 32#include "get_bits.h" 33#include "dsputil.h" 34#include "ivi_dsp.h" 35#include "ivi_common.h" 36#include "indeo5data.h" 37 38/** 39 * Indeo5 frame types. 40 */ 41enum { 42 FRAMETYPE_INTRA = 0, 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 47}; 48 49#define IVI5_PIC_SIZE_ESC 15 50 51#define IVI5_IS_PROTECTED 0x20 52 53typedef struct { 54 GetBitContext gb; 55 AVFrame frame; 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 65 int frame_type; 66 int prev_frame_type; ///< frame type of the previous frame 67 int frame_num; 68 uint32_t pic_hdr_size; ///< picture header size in bytes 69 uint8_t frame_flags; 70 uint16_t checksum; ///< frame checksum 71 72 IVIHuffTab mb_vlc; ///< vlc table for decoding macroblock data 73 74 uint16_t gop_hdr_size; 75 uint8_t gop_flags; 76 int is_scalable; 77 uint32_t lock_word; 78 IVIPicConfig pic_conf; 79} IVI5DecContext; 80 81 82/** 83 * Decodes 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. 86 * 87 * @param ctx [in,out] ptr to the decoder context 88 * @param avctx [in] ptr to the AVCodecContext 89 * @return result code: 0 = OK, -1 = error 90 */ 91static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx) 92{ 93 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, blk_size_changed = 0; 94 IVIBandDesc *band, *band1, *band2; 95 IVIPicConfig pic_conf; 96 97 ctx->gop_flags = get_bits(&ctx->gb, 8); 98 99 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0; 100 101 if (ctx->gop_flags & IVI5_IS_PROTECTED) 102 ctx->lock_word = get_bits_long(&ctx->gb, 32); 103 104 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0; 105 if (tile_size > 256) { 106 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size); 107 return -1; 108 } 109 110 /* decode number of wavelet bands */ 111 /* num_levels * 3 + 1 */ 112 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1; 113 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1; 114 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; 115 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { 116 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", 117 pic_conf.luma_bands, pic_conf.chroma_bands); 118 return -1; 119 } 120 121 pic_size_indx = get_bits(&ctx->gb, 4); 122 if (pic_size_indx == IVI5_PIC_SIZE_ESC) { 123 pic_conf.pic_height = get_bits(&ctx->gb, 13); 124 pic_conf.pic_width = get_bits(&ctx->gb, 13); 125 } else { 126 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2; 127 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2; 128 } 129 130 if (ctx->gop_flags & 2) { 131 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n"); 132 return -1; 133 } 134 135 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; 136 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; 137 138 if (!tile_size) { 139 pic_conf.tile_height = pic_conf.pic_height; 140 pic_conf.tile_width = pic_conf.pic_width; 141 } else { 142 pic_conf.tile_height = pic_conf.tile_width = tile_size; 143 } 144 145 /* check if picture layout was changed and reallocate buffers */ 146 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) { 147 result = ff_ivi_init_planes(ctx->planes, &pic_conf); 148 if (result) { 149 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); 150 return -1; 151 } 152 ctx->pic_conf = pic_conf; 153 blk_size_changed = 1; /* force reallocation of the internal structures */ 154 } 155 156 for (p = 0; p <= 1; p++) { 157 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { 158 band = &ctx->planes[p].bands[i]; 159 160 band->is_halfpel = get_bits1(&ctx->gb); 161 162 mb_size = get_bits1(&ctx->gb); 163 blk_size = 8 >> get_bits1(&ctx->gb); 164 mb_size = blk_size << !mb_size; 165 166 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size; 167 if (blk_size_changed) { 168 band->mb_size = mb_size; 169 band->blk_size = blk_size; 170 } 171 172 if (get_bits1(&ctx->gb)) { 173 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n"); 174 return -1; 175 } 176 177 /* select transform function and scan pattern according to plane and band number */ 178 switch ((p << 2) + i) { 179 case 0: 180 band->inv_transform = ff_ivi_inverse_slant_8x8; 181 band->dc_transform = ff_ivi_dc_slant_2d; 182 band->scan = ff_zigzag_direct; 183 break; 184 185 case 1: 186 band->inv_transform = ff_ivi_row_slant8; 187 band->dc_transform = ff_ivi_dc_row_slant; 188 band->scan = ivi5_scans8x8[0]; 189 break; 190 191 case 2: 192 band->inv_transform = ff_ivi_col_slant8; 193 band->dc_transform = ff_ivi_dc_col_slant; 194 band->scan = ivi5_scans8x8[1]; 195 break; 196 197 case 3: 198 band->inv_transform = ff_ivi_put_pixels_8x8; 199 band->dc_transform = ff_ivi_put_dc_pixel_8x8; 200 band->scan = ivi5_scans8x8[1]; 201 break; 202 203 case 4: 204 band->inv_transform = ff_ivi_inverse_slant_4x4; 205 band->dc_transform = ff_ivi_dc_slant_2d; 206 band->scan = ivi5_scan4x4; 207 break; 208 } 209 210 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 || 211 band->inv_transform == ff_ivi_inverse_slant_4x4; 212 213 /* select dequant matrix according to plane and band number */ 214 if (!p) { 215 band->quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0; 216 } else { 217 band->quant_mat = 5; 218 } 219 220 if (get_bits(&ctx->gb, 2)) { 221 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n"); 222 return -1; 223 } 224 } 225 } 226 227 /* copy chroma parameters into the 2nd chroma plane */ 228 for (i = 0; i < pic_conf.chroma_bands; i++) { 229 band1 = &ctx->planes[1].bands[i]; 230 band2 = &ctx->planes[2].bands[i]; 231 232 band2->width = band1->width; 233 band2->height = band1->height; 234 band2->mb_size = band1->mb_size; 235 band2->blk_size = band1->blk_size; 236 band2->is_halfpel = band1->is_halfpel; 237 band2->quant_mat = band1->quant_mat; 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 } 243 244 /* reallocate internal structures if needed */ 245 if (blk_size_changed) { 246 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width, 247 pic_conf.tile_height); 248 if (result) { 249 av_log(avctx, AV_LOG_ERROR, 250 "Couldn't reallocate internal structures!\n"); 251 return -1; 252 } 253 } 254 255 if (ctx->gop_flags & 8) { 256 if (get_bits(&ctx->gb, 3)) { 257 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n"); 258 return -1; 259 } 260 261 if (get_bits1(&ctx->gb)) 262 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */ 263 } 264 265 align_get_bits(&ctx->gb); 266 267 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */ 268 269 /* skip GOP extension if any */ 270 if (get_bits1(&ctx->gb)) { 271 do { 272 i = get_bits(&ctx->gb, 16); 273 } while (i & 0x8000); 274 } 275 276 align_get_bits(&ctx->gb); 277 278 return 0; 279} 280 281 282/** 283 * Skips a header extension. 284 * 285 * @param gb [in,out] the GetBit context 286 */ 287static inline void skip_hdr_extension(GetBitContext *gb) 288{ 289 int i, len; 290 291 do { 292 len = get_bits(gb, 8); 293 for (i = 0; i < len; i++) skip_bits(gb, 8); 294 } while(len); 295} 296 297 298/** 299 * Decodes Indeo5 picture header. 300 * 301 * @param ctx [in,out] ptr to the decoder context 302 * @param avctx [in] ptr to the AVCodecContext 303 * @return result code: 0 = OK, -1 = error 304 */ 305static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx) 306{ 307 if (get_bits(&ctx->gb, 5) != 0x1F) { 308 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); 309 return -1; 310 } 311 312 ctx->prev_frame_type = ctx->frame_type; 313 ctx->frame_type = get_bits(&ctx->gb, 3); 314 if (ctx->frame_type >= 5) { 315 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type); 316 return -1; 317 } 318 319 ctx->frame_num = get_bits(&ctx->gb, 8); 320 321 if (ctx->frame_type == FRAMETYPE_INTRA) { 322 if (decode_gop_header(ctx, avctx)) 323 return -1; 324 } 325 326 if (ctx->frame_type != FRAMETYPE_NULL) { 327 ctx->frame_flags = get_bits(&ctx->gb, 8); 328 329 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0; 330 331 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0; 332 333 /* skip unknown extension if any */ 334 if (ctx->frame_flags & 0x20) 335 skip_hdr_extension(&ctx->gb); /* XXX: untested */ 336 337 /* decode macroblock huffman codebook */ 338 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx)) 339 return -1; 340 341 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */ 342 } 343 344 align_get_bits(&ctx->gb); 345 346 return 0; 347} 348 349 350/** 351 * Decodes Indeo5 band header. 352 * 353 * @param ctx [in,out] ptr to the decoder context 354 * @param band [in,out] ptr to the band descriptor 355 * @param avctx [in] ptr to the AVCodecContext 356 * @return result code: 0 = OK, -1 = error 357 */ 358static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band, 359 AVCodecContext *avctx) 360{ 361 int i; 362 uint8_t band_flags; 363 364 band_flags = get_bits(&ctx->gb, 8); 365 366 if (band_flags & 1) { 367 band->is_empty = 1; 368 return 0; 369 } 370 371 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0; 372 373 band->inherit_mv = band_flags & 2; 374 band->inherit_qdelta = band_flags & 8; 375 band->qdelta_present = band_flags & 4; 376 if (!band->qdelta_present) band->inherit_qdelta = 1; 377 378 /* decode rvmap probability corrections if any */ 379 band->num_corr = 0; /* there are no corrections */ 380 if (band_flags & 0x10) { 381 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ 382 if (band->num_corr > 61) { 383 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", 384 band->num_corr); 385 return -1; 386 } 387 388 /* read correction pairs */ 389 for (i = 0; i < band->num_corr * 2; i++) 390 band->corr[i] = get_bits(&ctx->gb, 8); 391 } 392 393 /* select appropriate rvmap table for this band */ 394 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8; 395 396 /* decode block huffman codebook */ 397 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx)) 398 return -1; 399 400 band->checksum_present = get_bits1(&ctx->gb); 401 if (band->checksum_present) 402 band->checksum = get_bits(&ctx->gb, 16); 403 404 band->glob_quant = get_bits(&ctx->gb, 5); 405 406 /* skip unknown extension if any */ 407 if (band_flags & 0x20) { /* XXX: untested */ 408 align_get_bits(&ctx->gb); 409 skip_hdr_extension(&ctx->gb); 410 } 411 412 align_get_bits(&ctx->gb); 413 414 return 0; 415} 416 417 418/** 419 * Decodes info (block type, cbp, quant delta, motion vector) 420 * for all macroblocks in the current tile. 421 * 422 * @param ctx [in,out] ptr to the decoder context 423 * @param band [in,out] ptr to the band descriptor 424 * @param tile [in,out] ptr to the tile descriptor 425 * @param avctx [in] ptr to the AVCodecContext 426 * @return result code: 0 = OK, -1 = error 427 */ 428static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band, 429 IVITile *tile, AVCodecContext *avctx) 430{ 431 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, 432 mv_scale, blks_per_mb; 433 IVIMbInfo *mb, *ref_mb; 434 int row_offset = band->mb_size * band->pitch; 435 436 mb = tile->mbs; 437 ref_mb = tile->ref_mbs; 438 offs = tile->ypos * band->pitch + tile->xpos; 439 440 /* scale factor for motion vectors */ 441 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); 442 mv_x = mv_y = 0; 443 444 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) { 445 mb_offset = offs; 446 447 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) { 448 mb->xpos = x; 449 mb->ypos = y; 450 mb->buf_offs = mb_offset; 451 452 if (get_bits1(&ctx->gb)) { 453 if (ctx->frame_type == FRAMETYPE_INTRA) { 454 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); 455 return -1; 456 } 457 mb->type = 1; /* empty macroblocks are always INTER */ 458 mb->cbp = 0; /* all blocks are empty */ 459 460 mb->q_delta = 0; 461 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) { 462 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, 463 IVI_VLC_BITS, 1); 464 mb->q_delta = IVI_TOSIGNED(mb->q_delta); 465 } 466 467 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ 468 if (band->inherit_mv){ 469 /* motion vector inheritance */ 470 if (mv_scale) { 471 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); 472 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); 473 } else { 474 mb->mv_x = ref_mb->mv_x; 475 mb->mv_y = ref_mb->mv_y; 476 } 477 } 478 } else { 479 if (band->inherit_mv) { 480 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ 481 } else if (ctx->frame_type == FRAMETYPE_INTRA) { 482 mb->type = 0; /* mb_type is always INTRA for intra-frames */ 483 } else { 484 mb->type = get_bits1(&ctx->gb); 485 } 486 487 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; 488 mb->cbp = get_bits(&ctx->gb, blks_per_mb); 489 490 mb->q_delta = 0; 491 if (band->qdelta_present) { 492 if (band->inherit_qdelta) { 493 if (ref_mb) mb->q_delta = ref_mb->q_delta; 494 } else if (mb->cbp || (!band->plane && !band->band_num && 495 (ctx->frame_flags & 8))) { 496 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, 497 IVI_VLC_BITS, 1); 498 mb->q_delta = IVI_TOSIGNED(mb->q_delta); 499 } 500 } 501 502 if (!mb->type) { 503 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ 504 } else { 505 if (band->inherit_mv){ 506 /* motion vector inheritance */ 507 if (mv_scale) { 508 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); 509 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); 510 } else { 511 mb->mv_x = ref_mb->mv_x; 512 mb->mv_y = ref_mb->mv_y; 513 } 514 } else { 515 /* decode motion vector deltas */ 516 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, 517 IVI_VLC_BITS, 1); 518 mv_y += IVI_TOSIGNED(mv_delta); 519 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table, 520 IVI_VLC_BITS, 1); 521 mv_x += IVI_TOSIGNED(mv_delta); 522 mb->mv_x = mv_x; 523 mb->mv_y = mv_y; 524 } 525 } 526 } 527 528 mb++; 529 if (ref_mb) 530 ref_mb++; 531 mb_offset += band->mb_size; 532 } 533 534 offs += row_offset; 535 } 536 537 align_get_bits(&ctx->gb); 538 539 return 0; 540} 541 542 543/** 544 * Decodes an Indeo5 band. 545 * 546 * @param ctx [in,out] ptr to the decoder context 547 * @param band [in,out] ptr to the band descriptor 548 * @param avctx [in] ptr to the AVCodecContext 549 * @return result code: 0 = OK, -1 = error 550 */ 551static int decode_band(IVI5DecContext *ctx, int plane_num, 552 IVIBandDesc *band, AVCodecContext *avctx) 553{ 554 int result, i, t, idx1, idx2, pos; 555 IVITile *tile; 556 557 band->buf = band->bufs[ctx->dst_buf]; 558 band->ref_buf = band->bufs[ctx->ref_buf]; 559 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3); 560 561 result = decode_band_hdr(ctx, band, avctx); 562 if (result) { 563 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n", 564 result); 565 return -1; 566 } 567 568 if (band->is_empty) { 569 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n"); 570 return -1; 571 } 572 573 if (band->blk_size == 8) { 574 band->intra_base = &ivi5_base_quant_8x8_intra[band->quant_mat][0]; 575 band->inter_base = &ivi5_base_quant_8x8_inter[band->quant_mat][0]; 576 band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0]; 577 band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0]; 578 } else { 579 band->intra_base = ivi5_base_quant_4x4_intra; 580 band->inter_base = ivi5_base_quant_4x4_inter; 581 band->intra_scale = ivi5_scale_quant_4x4_intra; 582 band->inter_scale = ivi5_scale_quant_4x4_inter; 583 } 584 585 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel]; 586 587 /* apply corrections to the selected rvmap table if present */ 588 for (i = 0; i < band->num_corr; i++) { 589 idx1 = band->corr[i*2]; 590 idx2 = band->corr[i*2+1]; 591 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); 592 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); 593 } 594 595 pos = get_bits_count(&ctx->gb); 596 597 for (t = 0; t < band->num_tiles; t++) { 598 tile = &band->tiles[t]; 599 600 tile->is_empty = get_bits1(&ctx->gb); 601 if (tile->is_empty) { 602 ff_ivi_process_empty_tile(avctx, band, tile, 603 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3)); 604 } else { 605 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb); 606 607 result = decode_mb_info(ctx, band, tile, avctx); 608 if (result < 0) 609 break; 610 611 result = ff_ivi_decode_blocks(&ctx->gb, band, tile); 612 if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) { 613 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n"); 614 break; 615 } 616 pos += tile->data_size << 3; // skip to next tile 617 } 618 } 619 620 /* restore the selected rvmap table by applying its corrections in reverse order */ 621 for (i = band->num_corr-1; i >= 0; i--) { 622 idx1 = band->corr[i*2]; 623 idx2 = band->corr[i*2+1]; 624 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); 625 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); 626 } 627 628#if IVI_DEBUG 629 if (band->checksum_present) { 630 uint16_t chksum = ivi_calc_band_checksum(band); 631 if (chksum != band->checksum) { 632 av_log(avctx, AV_LOG_ERROR, 633 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n", 634 band->plane, band->band_num, band->checksum, chksum); 635 } 636 } 637#endif 638 639 align_get_bits(&ctx->gb); 640 641 return result; 642} 643 644 645/** 646 * Switches buffers. 647 * 648 * @param ctx [in,out] ptr to the decoder context 649 * @param avctx [in] ptr to the AVCodecContext 650 */ 651static void switch_buffers(IVI5DecContext *ctx, AVCodecContext *avctx) 652{ 653 switch (ctx->prev_frame_type) { 654 case FRAMETYPE_INTRA: 655 case FRAMETYPE_INTER: 656 ctx->buf_switch ^= 1; 657 ctx->dst_buf = ctx->buf_switch; 658 ctx->ref_buf = ctx->buf_switch ^ 1; 659 break; 660 case FRAMETYPE_INTER_SCAL: 661 if (!ctx->inter_scal) { 662 ctx->ref2_buf = 2; 663 ctx->inter_scal = 1; 664 } 665 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf); 666 ctx->ref_buf = ctx->ref2_buf; 667 break; 668 case FRAMETYPE_INTER_NOREF: 669 break; 670 } 671 672 switch (ctx->frame_type) { 673 case FRAMETYPE_INTRA: 674 ctx->buf_switch = 0; 675 /* FALLTHROUGH */ 676 case FRAMETYPE_INTER: 677 ctx->inter_scal = 0; 678 ctx->dst_buf = ctx->buf_switch; 679 ctx->ref_buf = ctx->buf_switch ^ 1; 680 break; 681 case FRAMETYPE_INTER_SCAL: 682 case FRAMETYPE_INTER_NOREF: 683 case FRAMETYPE_NULL: 684 break; 685 } 686} 687 688 689/** 690 * Initializes Indeo5 decoder. 691 */ 692static av_cold int decode_init(AVCodecContext *avctx) 693{ 694 IVI5DecContext *ctx = avctx->priv_data; 695 int result; 696 697 ff_ivi_init_static_vlc(); 698 699 /* copy rvmap tables in our context so we can apply changes to them */ 700 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); 701 702 /* set the initial picture layout according to the basic profile: 703 there is only one band per plane (no scalability), only one tile (no local decoding) 704 and picture format = YVU9 */ 705 ctx->pic_conf.pic_width = avctx->width; 706 ctx->pic_conf.pic_height = avctx->height; 707 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2; 708 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2; 709 ctx->pic_conf.tile_width = avctx->width; 710 ctx->pic_conf.tile_height = avctx->height; 711 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1; 712 713 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf); 714 if (result) { 715 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n"); 716 return -1; 717 } 718 719 ctx->buf_switch = 0; 720 ctx->inter_scal = 0; 721 722 avctx->pix_fmt = PIX_FMT_YUV410P; 723 724 return 0; 725} 726 727 728/** 729 * main decoder function 730 */ 731static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, 732 AVPacket *avpkt) 733{ 734 IVI5DecContext *ctx = avctx->priv_data; 735 const uint8_t *buf = avpkt->data; 736 int buf_size = avpkt->size; 737 int result, p, b; 738 739 init_get_bits(&ctx->gb, buf, buf_size * 8); 740 ctx->frame_data = buf; 741 ctx->frame_size = buf_size; 742 743 result = decode_pic_hdr(ctx, avctx); 744 if (result) { 745 av_log(avctx, AV_LOG_ERROR, 746 "Error while decoding picture header: %d\n", result); 747 return -1; 748 } 749 750 if (ctx->gop_flags & IVI5_IS_PROTECTED) { 751 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n"); 752 return -1; 753 } 754 755 switch_buffers(ctx, avctx); 756 757 //START_TIMER; 758 759 if (ctx->frame_type != FRAMETYPE_NULL) { 760 for (p = 0; p < 3; p++) { 761 for (b = 0; b < ctx->planes[p].num_bands; b++) { 762 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx); 763 if (result) { 764 av_log(avctx, AV_LOG_ERROR, 765 "Error while decoding band: %d, plane: %d\n", b, p); 766 return -1; 767 } 768 } 769 } 770 } 771 772 //STOP_TIMER("decode_planes"); 773 774 if (ctx->frame.data[0]) 775 avctx->release_buffer(avctx, &ctx->frame); 776 777 ctx->frame.reference = 0; 778 if (avctx->get_buffer(avctx, &ctx->frame) < 0) { 779 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 780 return -1; 781 } 782 783 if (ctx->is_scalable) { 784 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4); 785 } else { 786 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]); 787 } 788 789 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]); 790 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]); 791 792 *data_size = sizeof(AVFrame); 793 *(AVFrame*)data = ctx->frame; 794 795 return buf_size; 796} 797 798 799/** 800 * Closes Indeo5 decoder and cleans up its context. 801 */ 802static av_cold int decode_close(AVCodecContext *avctx) 803{ 804 IVI5DecContext *ctx = avctx->priv_data; 805 806 ff_ivi_free_buffers(&ctx->planes[0]); 807 808 if (ctx->mb_vlc.cust_tab.table) 809 free_vlc(&ctx->mb_vlc.cust_tab); 810 811 if (ctx->frame.data[0]) 812 avctx->release_buffer(avctx, &ctx->frame); 813 814 return 0; 815} 816 817 818AVCodec indeo5_decoder = { 819 .name = "indeo5", 820 .type = AVMEDIA_TYPE_VIDEO, 821 .id = CODEC_ID_INDEO5, 822 .priv_data_size = sizeof(IVI5DecContext), 823 .init = decode_init, 824 .close = decode_close, 825 .decode = decode_frame, 826 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"), 827}; 828