00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #define ALT_BITSTREAM_READER_LE
00031 #include "avcodec.h"
00032 #include "get_bits.h"
00033 #include "dsputil.h"
00034 #include "ivi_dsp.h"
00035 #include "ivi_common.h"
00036 #include "indeo5data.h"
00037
00041 enum {
00042 FRAMETYPE_INTRA = 0,
00043 FRAMETYPE_INTER = 1,
00044 FRAMETYPE_INTER_SCAL = 2,
00045 FRAMETYPE_INTER_NOREF = 3,
00046 FRAMETYPE_NULL = 4
00047 };
00048
00049 #define IVI5_PIC_SIZE_ESC 15
00050
00051 #define IVI5_IS_PROTECTED 0x20
00052
00053 typedef struct {
00054 GetBitContext gb;
00055 AVFrame frame;
00056 RVMapDesc rvmap_tabs[9];
00057 IVIPlaneDesc planes[3];
00058 const uint8_t *frame_data;
00059 int buf_switch;
00060 int inter_scal;
00061 int dst_buf;
00062 int ref_buf;
00063 int ref2_buf;
00064 uint32_t frame_size;
00065 int frame_type;
00066 int prev_frame_type;
00067 int frame_num;
00068 uint32_t pic_hdr_size;
00069 uint8_t frame_flags;
00070 uint16_t checksum;
00071
00072 IVIHuffTab mb_vlc;
00073
00074 uint16_t gop_hdr_size;
00075 uint8_t gop_flags;
00076 int is_scalable;
00077 uint32_t lock_word;
00078 IVIPicConfig pic_conf;
00079 } IVI5DecContext;
00080
00081
00091 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
00092 {
00093 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
00094 int quant_mat, blk_size_changed = 0;
00095 IVIBandDesc *band, *band1, *band2;
00096 IVIPicConfig pic_conf;
00097
00098 ctx->gop_flags = get_bits(&ctx->gb, 8);
00099
00100 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
00101
00102 if (ctx->gop_flags & IVI5_IS_PROTECTED)
00103 ctx->lock_word = get_bits_long(&ctx->gb, 32);
00104
00105 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
00106 if (tile_size > 256) {
00107 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
00108 return -1;
00109 }
00110
00111
00112
00113 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
00114 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
00115 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00116 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00117 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00118 pic_conf.luma_bands, pic_conf.chroma_bands);
00119 return -1;
00120 }
00121
00122 pic_size_indx = get_bits(&ctx->gb, 4);
00123 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
00124 pic_conf.pic_height = get_bits(&ctx->gb, 13);
00125 pic_conf.pic_width = get_bits(&ctx->gb, 13);
00126 } else {
00127 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
00128 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
00129 }
00130
00131 if (ctx->gop_flags & 2) {
00132 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
00133 return -1;
00134 }
00135
00136 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00137 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
00138
00139 if (!tile_size) {
00140 pic_conf.tile_height = pic_conf.pic_height;
00141 pic_conf.tile_width = pic_conf.pic_width;
00142 } else {
00143 pic_conf.tile_height = pic_conf.tile_width = tile_size;
00144 }
00145
00146
00147 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00148 result = ff_ivi_init_planes(ctx->planes, &pic_conf);
00149 if (result) {
00150 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00151 return -1;
00152 }
00153 ctx->pic_conf = pic_conf;
00154 blk_size_changed = 1;
00155 }
00156
00157 for (p = 0; p <= 1; p++) {
00158 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00159 band = &ctx->planes[p].bands[i];
00160
00161 band->is_halfpel = get_bits1(&ctx->gb);
00162
00163 mb_size = get_bits1(&ctx->gb);
00164 blk_size = 8 >> get_bits1(&ctx->gb);
00165 mb_size = blk_size << !mb_size;
00166
00167 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
00168 if (blk_size_changed) {
00169 band->mb_size = mb_size;
00170 band->blk_size = blk_size;
00171 }
00172
00173 if (get_bits1(&ctx->gb)) {
00174 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
00175 return -1;
00176 }
00177
00178
00179 switch ((p << 2) + i) {
00180 case 0:
00181 band->inv_transform = ff_ivi_inverse_slant_8x8;
00182 band->dc_transform = ff_ivi_dc_slant_2d;
00183 band->scan = ff_zigzag_direct;
00184 break;
00185
00186 case 1:
00187 band->inv_transform = ff_ivi_row_slant8;
00188 band->dc_transform = ff_ivi_dc_row_slant;
00189 band->scan = ff_ivi_vertical_scan_8x8;
00190 break;
00191
00192 case 2:
00193 band->inv_transform = ff_ivi_col_slant8;
00194 band->dc_transform = ff_ivi_dc_col_slant;
00195 band->scan = ff_ivi_horizontal_scan_8x8;
00196 break;
00197
00198 case 3:
00199 band->inv_transform = ff_ivi_put_pixels_8x8;
00200 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
00201 band->scan = ff_ivi_horizontal_scan_8x8;
00202 break;
00203
00204 case 4:
00205 band->inv_transform = ff_ivi_inverse_slant_4x4;
00206 band->dc_transform = ff_ivi_dc_slant_2d;
00207 band->scan = ff_ivi_direct_scan_4x4;
00208 break;
00209 }
00210
00211 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
00212 band->inv_transform == ff_ivi_inverse_slant_4x4;
00213
00214
00215 if (!p) {
00216 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
00217 } else {
00218 quant_mat = 5;
00219 }
00220
00221 if (band->blk_size == 8) {
00222 if(quant_mat >= 5){
00223 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
00224 return -1;
00225 }
00226 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
00227 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
00228 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
00229 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
00230 } else {
00231 band->intra_base = ivi5_base_quant_4x4_intra;
00232 band->inter_base = ivi5_base_quant_4x4_inter;
00233 band->intra_scale = ivi5_scale_quant_4x4_intra;
00234 band->inter_scale = ivi5_scale_quant_4x4_inter;
00235 }
00236
00237 if (get_bits(&ctx->gb, 2)) {
00238 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
00239 return -1;
00240 }
00241 }
00242 }
00243
00244
00245 for (i = 0; i < pic_conf.chroma_bands; i++) {
00246 band1 = &ctx->planes[1].bands[i];
00247 band2 = &ctx->planes[2].bands[i];
00248
00249 band2->width = band1->width;
00250 band2->height = band1->height;
00251 band2->mb_size = band1->mb_size;
00252 band2->blk_size = band1->blk_size;
00253 band2->is_halfpel = band1->is_halfpel;
00254 band2->intra_base = band1->intra_base;
00255 band2->inter_base = band1->inter_base;
00256 band2->intra_scale = band1->intra_scale;
00257 band2->inter_scale = band1->inter_scale;
00258 band2->scan = band1->scan;
00259 band2->inv_transform = band1->inv_transform;
00260 band2->dc_transform = band1->dc_transform;
00261 band2->is_2d_trans = band1->is_2d_trans;
00262 }
00263
00264
00265 if (blk_size_changed) {
00266 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
00267 pic_conf.tile_height);
00268 if (result) {
00269 av_log(avctx, AV_LOG_ERROR,
00270 "Couldn't reallocate internal structures!\n");
00271 return -1;
00272 }
00273 }
00274
00275 if (ctx->gop_flags & 8) {
00276 if (get_bits(&ctx->gb, 3)) {
00277 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
00278 return -1;
00279 }
00280
00281 if (get_bits1(&ctx->gb))
00282 skip_bits_long(&ctx->gb, 24);
00283 }
00284
00285 align_get_bits(&ctx->gb);
00286
00287 skip_bits(&ctx->gb, 23);
00288
00289
00290 if (get_bits1(&ctx->gb)) {
00291 do {
00292 i = get_bits(&ctx->gb, 16);
00293 } while (i & 0x8000);
00294 }
00295
00296 align_get_bits(&ctx->gb);
00297
00298 return 0;
00299 }
00300
00301
00307 static inline void skip_hdr_extension(GetBitContext *gb)
00308 {
00309 int i, len;
00310
00311 do {
00312 len = get_bits(gb, 8);
00313 for (i = 0; i < len; i++) skip_bits(gb, 8);
00314 } while(len);
00315 }
00316
00317
00325 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
00326 {
00327 if (get_bits(&ctx->gb, 5) != 0x1F) {
00328 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00329 return -1;
00330 }
00331
00332 ctx->prev_frame_type = ctx->frame_type;
00333 ctx->frame_type = get_bits(&ctx->gb, 3);
00334 if (ctx->frame_type >= 5) {
00335 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
00336 return -1;
00337 }
00338
00339 ctx->frame_num = get_bits(&ctx->gb, 8);
00340
00341 if (ctx->frame_type == FRAMETYPE_INTRA) {
00342 if (decode_gop_header(ctx, avctx))
00343 return -1;
00344 }
00345
00346 if (ctx->frame_type != FRAMETYPE_NULL) {
00347 ctx->frame_flags = get_bits(&ctx->gb, 8);
00348
00349 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
00350
00351 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
00352
00353
00354 if (ctx->frame_flags & 0x20)
00355 skip_hdr_extension(&ctx->gb);
00356
00357
00358 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
00359 return -1;
00360
00361 skip_bits(&ctx->gb, 3);
00362 }
00363
00364 align_get_bits(&ctx->gb);
00365
00366 return 0;
00367 }
00368
00369
00378 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
00379 AVCodecContext *avctx)
00380 {
00381 int i;
00382 uint8_t band_flags;
00383
00384 band_flags = get_bits(&ctx->gb, 8);
00385
00386 if (band_flags & 1) {
00387 band->is_empty = 1;
00388 return 0;
00389 }
00390
00391 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
00392
00393 band->inherit_mv = band_flags & 2;
00394 band->inherit_qdelta = band_flags & 8;
00395 band->qdelta_present = band_flags & 4;
00396 if (!band->qdelta_present) band->inherit_qdelta = 1;
00397
00398
00399 band->num_corr = 0;
00400 if (band_flags & 0x10) {
00401 band->num_corr = get_bits(&ctx->gb, 8);
00402 if (band->num_corr > 61) {
00403 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00404 band->num_corr);
00405 return -1;
00406 }
00407
00408
00409 for (i = 0; i < band->num_corr * 2; i++)
00410 band->corr[i] = get_bits(&ctx->gb, 8);
00411 }
00412
00413
00414 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
00415
00416
00417 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
00418 return -1;
00419
00420 band->checksum_present = get_bits1(&ctx->gb);
00421 if (band->checksum_present)
00422 band->checksum = get_bits(&ctx->gb, 16);
00423
00424 band->glob_quant = get_bits(&ctx->gb, 5);
00425
00426
00427 if (band_flags & 0x20) {
00428 align_get_bits(&ctx->gb);
00429 skip_hdr_extension(&ctx->gb);
00430 }
00431
00432 align_get_bits(&ctx->gb);
00433
00434 return 0;
00435 }
00436
00437
00448 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
00449 IVITile *tile, AVCodecContext *avctx)
00450 {
00451 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
00452 mv_scale, blks_per_mb;
00453 IVIMbInfo *mb, *ref_mb;
00454 int row_offset = band->mb_size * band->pitch;
00455
00456 mb = tile->mbs;
00457 ref_mb = tile->ref_mbs;
00458 offs = tile->ypos * band->pitch + tile->xpos;
00459
00460
00461 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00462 mv_x = mv_y = 0;
00463
00464 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
00465 mb_offset = offs;
00466
00467 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
00468 mb->xpos = x;
00469 mb->ypos = y;
00470 mb->buf_offs = mb_offset;
00471
00472 if (get_bits1(&ctx->gb)) {
00473 if (ctx->frame_type == FRAMETYPE_INTRA) {
00474 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00475 return -1;
00476 }
00477 mb->type = 1;
00478 mb->cbp = 0;
00479
00480 mb->q_delta = 0;
00481 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
00482 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00483 IVI_VLC_BITS, 1);
00484 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00485 }
00486
00487 mb->mv_x = mb->mv_y = 0;
00488 if (band->inherit_mv){
00489
00490 if (mv_scale) {
00491 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00492 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00493 } else {
00494 mb->mv_x = ref_mb->mv_x;
00495 mb->mv_y = ref_mb->mv_y;
00496 }
00497 }
00498 } else {
00499 if (band->inherit_mv) {
00500 mb->type = ref_mb->type;
00501 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00502 mb->type = 0;
00503 } else {
00504 mb->type = get_bits1(&ctx->gb);
00505 }
00506
00507 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00508 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00509
00510 mb->q_delta = 0;
00511 if (band->qdelta_present) {
00512 if (band->inherit_qdelta) {
00513 if (ref_mb) mb->q_delta = ref_mb->q_delta;
00514 } else if (mb->cbp || (!band->plane && !band->band_num &&
00515 (ctx->frame_flags & 8))) {
00516 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00517 IVI_VLC_BITS, 1);
00518 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00519 }
00520 }
00521
00522 if (!mb->type) {
00523 mb->mv_x = mb->mv_y = 0;
00524 } else {
00525 if (band->inherit_mv){
00526
00527 if (mv_scale) {
00528 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00529 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00530 } else {
00531 mb->mv_x = ref_mb->mv_x;
00532 mb->mv_y = ref_mb->mv_y;
00533 }
00534 } else {
00535
00536 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00537 IVI_VLC_BITS, 1);
00538 mv_y += IVI_TOSIGNED(mv_delta);
00539 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00540 IVI_VLC_BITS, 1);
00541 mv_x += IVI_TOSIGNED(mv_delta);
00542 mb->mv_x = mv_x;
00543 mb->mv_y = mv_y;
00544 }
00545 }
00546 }
00547
00548 mb++;
00549 if (ref_mb)
00550 ref_mb++;
00551 mb_offset += band->mb_size;
00552 }
00553
00554 offs += row_offset;
00555 }
00556
00557 align_get_bits(&ctx->gb);
00558
00559 return 0;
00560 }
00561
00562
00571 static int decode_band(IVI5DecContext *ctx, int plane_num,
00572 IVIBandDesc *band, AVCodecContext *avctx)
00573 {
00574 int result, i, t, idx1, idx2, pos;
00575 IVITile *tile;
00576
00577 band->buf = band->bufs[ctx->dst_buf];
00578 band->ref_buf = band->bufs[ctx->ref_buf];
00579 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
00580
00581 result = decode_band_hdr(ctx, band, avctx);
00582 if (result) {
00583 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
00584 result);
00585 return -1;
00586 }
00587
00588 if (band->is_empty) {
00589 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00590 return -1;
00591 }
00592
00593 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
00594
00595
00596 for (i = 0; i < band->num_corr; i++) {
00597 idx1 = band->corr[i*2];
00598 idx2 = band->corr[i*2+1];
00599 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00600 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00601 }
00602
00603 pos = get_bits_count(&ctx->gb);
00604
00605 for (t = 0; t < band->num_tiles; t++) {
00606 tile = &band->tiles[t];
00607
00608 tile->is_empty = get_bits1(&ctx->gb);
00609 if (tile->is_empty) {
00610 ff_ivi_process_empty_tile(avctx, band, tile,
00611 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00612 } else {
00613 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00614
00615 result = decode_mb_info(ctx, band, tile, avctx);
00616 if (result < 0)
00617 break;
00618
00619 result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00620 if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
00621 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00622 break;
00623 }
00624 pos += tile->data_size << 3;
00625 }
00626 }
00627
00628
00629 for (i = band->num_corr-1; i >= 0; i--) {
00630 idx1 = band->corr[i*2];
00631 idx2 = band->corr[i*2+1];
00632 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00633 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00634 }
00635
00636 #ifdef DEBUG
00637 if (band->checksum_present) {
00638 uint16_t chksum = ivi_calc_band_checksum(band);
00639 if (chksum != band->checksum) {
00640 av_log(avctx, AV_LOG_ERROR,
00641 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
00642 band->plane, band->band_num, band->checksum, chksum);
00643 }
00644 }
00645 #endif
00646
00647 align_get_bits(&ctx->gb);
00648
00649 return result;
00650 }
00651
00652
00658 static void switch_buffers(IVI5DecContext *ctx)
00659 {
00660 switch (ctx->prev_frame_type) {
00661 case FRAMETYPE_INTRA:
00662 case FRAMETYPE_INTER:
00663 ctx->buf_switch ^= 1;
00664 ctx->dst_buf = ctx->buf_switch;
00665 ctx->ref_buf = ctx->buf_switch ^ 1;
00666 break;
00667 case FRAMETYPE_INTER_SCAL:
00668 if (!ctx->inter_scal) {
00669 ctx->ref2_buf = 2;
00670 ctx->inter_scal = 1;
00671 }
00672 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
00673 ctx->ref_buf = ctx->ref2_buf;
00674 break;
00675 case FRAMETYPE_INTER_NOREF:
00676 break;
00677 }
00678
00679 switch (ctx->frame_type) {
00680 case FRAMETYPE_INTRA:
00681 ctx->buf_switch = 0;
00682
00683 case FRAMETYPE_INTER:
00684 ctx->inter_scal = 0;
00685 ctx->dst_buf = ctx->buf_switch;
00686 ctx->ref_buf = ctx->buf_switch ^ 1;
00687 break;
00688 case FRAMETYPE_INTER_SCAL:
00689 case FRAMETYPE_INTER_NOREF:
00690 case FRAMETYPE_NULL:
00691 break;
00692 }
00693 }
00694
00695
00699 static av_cold int decode_init(AVCodecContext *avctx)
00700 {
00701 IVI5DecContext *ctx = avctx->priv_data;
00702 int result;
00703
00704 ff_ivi_init_static_vlc();
00705
00706
00707 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00708
00709
00710
00711
00712 ctx->pic_conf.pic_width = avctx->width;
00713 ctx->pic_conf.pic_height = avctx->height;
00714 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
00715 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
00716 ctx->pic_conf.tile_width = avctx->width;
00717 ctx->pic_conf.tile_height = avctx->height;
00718 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
00719
00720 avcodec_get_frame_defaults(&ctx->frame);
00721
00722 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
00723 if (result) {
00724 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
00725 return -1;
00726 }
00727
00728 ctx->buf_switch = 0;
00729 ctx->inter_scal = 0;
00730
00731 avctx->pix_fmt = PIX_FMT_YUV410P;
00732
00733 return 0;
00734 }
00735
00736
00740 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00741 AVPacket *avpkt)
00742 {
00743 IVI5DecContext *ctx = avctx->priv_data;
00744 const uint8_t *buf = avpkt->data;
00745 int buf_size = avpkt->size;
00746 int result, p, b;
00747
00748 init_get_bits(&ctx->gb, buf, buf_size * 8);
00749 ctx->frame_data = buf;
00750 ctx->frame_size = buf_size;
00751
00752 result = decode_pic_hdr(ctx, avctx);
00753 if (result) {
00754 av_log(avctx, AV_LOG_ERROR,
00755 "Error while decoding picture header: %d\n", result);
00756 return -1;
00757 }
00758
00759 if (ctx->gop_flags & IVI5_IS_PROTECTED) {
00760 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
00761 return -1;
00762 }
00763
00764 switch_buffers(ctx);
00765
00766
00767
00768 if (ctx->frame_type != FRAMETYPE_NULL) {
00769 for (p = 0; p < 3; p++) {
00770 for (b = 0; b < ctx->planes[p].num_bands; b++) {
00771 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00772 if (result) {
00773 av_log(avctx, AV_LOG_ERROR,
00774 "Error while decoding band: %d, plane: %d\n", b, p);
00775 return -1;
00776 }
00777 }
00778 }
00779 }
00780
00781
00782
00783 if (ctx->frame.data[0])
00784 avctx->release_buffer(avctx, &ctx->frame);
00785
00786 ctx->frame.reference = 0;
00787 if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
00788 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00789 return -1;
00790 }
00791
00792 if (ctx->is_scalable) {
00793 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00794 } else {
00795 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00796 }
00797
00798 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00799 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00800
00801 *data_size = sizeof(AVFrame);
00802 *(AVFrame*)data = ctx->frame;
00803
00804 return buf_size;
00805 }
00806
00807
00811 static av_cold int decode_close(AVCodecContext *avctx)
00812 {
00813 IVI5DecContext *ctx = avctx->priv_data;
00814
00815 ff_ivi_free_buffers(&ctx->planes[0]);
00816
00817 if (ctx->mb_vlc.cust_tab.table)
00818 free_vlc(&ctx->mb_vlc.cust_tab);
00819
00820 if (ctx->frame.data[0])
00821 avctx->release_buffer(avctx, &ctx->frame);
00822
00823 return 0;
00824 }
00825
00826
00827 AVCodec ff_indeo5_decoder = {
00828 .name = "indeo5",
00829 .type = AVMEDIA_TYPE_VIDEO,
00830 .id = CODEC_ID_INDEO5,
00831 .priv_data_size = sizeof(IVI5DecContext),
00832 .init = decode_init,
00833 .close = decode_close,
00834 .decode = decode_frame,
00835 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
00836 };