00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #define 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
00060 static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
00061 {
00062 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
00063 int quant_mat, blk_size_changed = 0;
00064 IVIBandDesc *band, *band1, *band2;
00065 IVIPicConfig pic_conf;
00066
00067 ctx->gop_flags = get_bits(&ctx->gb, 8);
00068
00069 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
00070
00071 if (ctx->gop_flags & IVI5_IS_PROTECTED)
00072 ctx->lock_word = get_bits_long(&ctx->gb, 32);
00073
00074 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
00075 if (tile_size > 256) {
00076 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
00077 return -1;
00078 }
00079
00080
00081
00082 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
00083 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
00084 is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00085 if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00086 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00087 pic_conf.luma_bands, pic_conf.chroma_bands);
00088 return -1;
00089 }
00090
00091 pic_size_indx = get_bits(&ctx->gb, 4);
00092 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
00093 pic_conf.pic_height = get_bits(&ctx->gb, 13);
00094 pic_conf.pic_width = get_bits(&ctx->gb, 13);
00095 } else {
00096 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
00097 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
00098 }
00099
00100 if (ctx->gop_flags & 2) {
00101 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
00102 return -1;
00103 }
00104
00105 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00106 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
00107
00108 if (!tile_size) {
00109 pic_conf.tile_height = pic_conf.pic_height;
00110 pic_conf.tile_width = pic_conf.pic_width;
00111 } else {
00112 pic_conf.tile_height = pic_conf.tile_width = tile_size;
00113 }
00114
00115
00116 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00117 result = ff_ivi_init_planes(ctx->planes, &pic_conf);
00118 if (result) {
00119 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00120 return -1;
00121 }
00122 ctx->pic_conf = pic_conf;
00123 ctx->is_scalable = is_scalable;
00124 blk_size_changed = 1;
00125 }
00126
00127 for (p = 0; p <= 1; p++) {
00128 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00129 band = &ctx->planes[p].bands[i];
00130
00131 band->is_halfpel = get_bits1(&ctx->gb);
00132
00133 mb_size = get_bits1(&ctx->gb);
00134 blk_size = 8 >> get_bits1(&ctx->gb);
00135 mb_size = blk_size << !mb_size;
00136
00137 if (p==0 && blk_size==4) {
00138 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
00139 return AVERROR_PATCHWELCOME;
00140 }
00141
00142 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
00143 if (blk_size_changed) {
00144 band->mb_size = mb_size;
00145 band->blk_size = blk_size;
00146 }
00147
00148 if (get_bits1(&ctx->gb)) {
00149 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
00150 return -1;
00151 }
00152
00153
00154 switch ((p << 2) + i) {
00155 case 0:
00156 band->inv_transform = ff_ivi_inverse_slant_8x8;
00157 band->dc_transform = ff_ivi_dc_slant_2d;
00158 band->scan = ff_zigzag_direct;
00159 band->transform_size= 8;
00160 break;
00161
00162 case 1:
00163 band->inv_transform = ff_ivi_row_slant8;
00164 band->dc_transform = ff_ivi_dc_row_slant;
00165 band->scan = ff_ivi_vertical_scan_8x8;
00166 band->transform_size= 8;
00167 break;
00168
00169 case 2:
00170 band->inv_transform = ff_ivi_col_slant8;
00171 band->dc_transform = ff_ivi_dc_col_slant;
00172 band->scan = ff_ivi_horizontal_scan_8x8;
00173 band->transform_size= 8;
00174 break;
00175
00176 case 3:
00177 band->inv_transform = ff_ivi_put_pixels_8x8;
00178 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
00179 band->scan = ff_ivi_horizontal_scan_8x8;
00180 band->transform_size= 8;
00181 break;
00182
00183 case 4:
00184 band->inv_transform = ff_ivi_inverse_slant_4x4;
00185 band->dc_transform = ff_ivi_dc_slant_2d;
00186 band->scan = ff_ivi_direct_scan_4x4;
00187 band->transform_size= 4;
00188 break;
00189 }
00190
00191 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
00192 band->inv_transform == ff_ivi_inverse_slant_4x4;
00193
00194
00195 if (!p) {
00196 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
00197 } else {
00198 quant_mat = 5;
00199 }
00200
00201 if (band->blk_size == 8) {
00202 if(quant_mat >= 5){
00203 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
00204 return -1;
00205 }
00206 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
00207 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
00208 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
00209 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
00210 } else {
00211 band->intra_base = ivi5_base_quant_4x4_intra;
00212 band->inter_base = ivi5_base_quant_4x4_inter;
00213 band->intra_scale = ivi5_scale_quant_4x4_intra;
00214 band->inter_scale = ivi5_scale_quant_4x4_inter;
00215 }
00216
00217 if (get_bits(&ctx->gb, 2)) {
00218 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
00219 return -1;
00220 }
00221 }
00222 }
00223
00224
00225 for (i = 0; i < pic_conf.chroma_bands; i++) {
00226 band1 = &ctx->planes[1].bands[i];
00227 band2 = &ctx->planes[2].bands[i];
00228
00229 band2->width = band1->width;
00230 band2->height = band1->height;
00231 band2->mb_size = band1->mb_size;
00232 band2->blk_size = band1->blk_size;
00233 band2->is_halfpel = band1->is_halfpel;
00234 band2->intra_base = band1->intra_base;
00235 band2->inter_base = band1->inter_base;
00236 band2->intra_scale = band1->intra_scale;
00237 band2->inter_scale = band1->inter_scale;
00238 band2->scan = band1->scan;
00239 band2->inv_transform = band1->inv_transform;
00240 band2->dc_transform = band1->dc_transform;
00241 band2->is_2d_trans = band1->is_2d_trans;
00242 band2->transform_size= band1->transform_size;
00243 }
00244
00245
00246 if (blk_size_changed) {
00247 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
00248 pic_conf.tile_height);
00249 if (result) {
00250 av_log(avctx, AV_LOG_ERROR,
00251 "Couldn't reallocate internal structures!\n");
00252 return -1;
00253 }
00254 }
00255
00256 if (ctx->gop_flags & 8) {
00257 if (get_bits(&ctx->gb, 3)) {
00258 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
00259 return -1;
00260 }
00261
00262 if (get_bits1(&ctx->gb))
00263 skip_bits_long(&ctx->gb, 24);
00264 }
00265
00266 align_get_bits(&ctx->gb);
00267
00268 skip_bits(&ctx->gb, 23);
00269
00270
00271 if (get_bits1(&ctx->gb)) {
00272 do {
00273 i = get_bits(&ctx->gb, 16);
00274 } while (i & 0x8000);
00275 }
00276
00277 align_get_bits(&ctx->gb);
00278
00279 return 0;
00280 }
00281
00282
00288 static inline void skip_hdr_extension(GetBitContext *gb)
00289 {
00290 int i, len;
00291
00292 do {
00293 len = get_bits(gb, 8);
00294 for (i = 0; i < len; i++) skip_bits(gb, 8);
00295 } while(len);
00296 }
00297
00298
00306 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
00307 {
00308 if (get_bits(&ctx->gb, 5) != 0x1F) {
00309 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00310 return -1;
00311 }
00312
00313 ctx->prev_frame_type = ctx->frame_type;
00314 ctx->frame_type = get_bits(&ctx->gb, 3);
00315 if (ctx->frame_type >= 5) {
00316 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
00317 return -1;
00318 }
00319
00320 ctx->frame_num = get_bits(&ctx->gb, 8);
00321
00322 if (ctx->frame_type == FRAMETYPE_INTRA) {
00323 ctx->gop_invalid = 1;
00324 if (decode_gop_header(ctx, avctx))
00325 return -1;
00326 ctx->gop_invalid = 0;
00327 }
00328
00329 if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
00330 av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scaleable stream\n");
00331 ctx->frame_type = FRAMETYPE_INTER;
00332 return AVERROR_INVALIDDATA;
00333 }
00334
00335 if (ctx->frame_type != FRAMETYPE_NULL) {
00336 ctx->frame_flags = get_bits(&ctx->gb, 8);
00337
00338 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
00339
00340 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
00341
00342
00343 if (ctx->frame_flags & 0x20)
00344 skip_hdr_extension(&ctx->gb);
00345
00346
00347 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
00348 return -1;
00349
00350 skip_bits(&ctx->gb, 3);
00351 }
00352
00353 align_get_bits(&ctx->gb);
00354
00355 return 0;
00356 }
00357
00358
00367 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
00368 AVCodecContext *avctx)
00369 {
00370 int i;
00371 uint8_t band_flags;
00372
00373 band_flags = get_bits(&ctx->gb, 8);
00374
00375 if (band_flags & 1) {
00376 band->is_empty = 1;
00377 return 0;
00378 }
00379
00380 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
00381
00382 band->inherit_mv = band_flags & 2;
00383 band->inherit_qdelta = band_flags & 8;
00384 band->qdelta_present = band_flags & 4;
00385 if (!band->qdelta_present) band->inherit_qdelta = 1;
00386
00387
00388 band->num_corr = 0;
00389 if (band_flags & 0x10) {
00390 band->num_corr = get_bits(&ctx->gb, 8);
00391 if (band->num_corr > 61) {
00392 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00393 band->num_corr);
00394 return -1;
00395 }
00396
00397
00398 for (i = 0; i < band->num_corr * 2; i++)
00399 band->corr[i] = get_bits(&ctx->gb, 8);
00400 }
00401
00402
00403 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
00404
00405
00406 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
00407 return -1;
00408
00409 band->checksum_present = get_bits1(&ctx->gb);
00410 if (band->checksum_present)
00411 band->checksum = get_bits(&ctx->gb, 16);
00412
00413 band->glob_quant = get_bits(&ctx->gb, 5);
00414
00415
00416 if (band_flags & 0x20) {
00417 align_get_bits(&ctx->gb);
00418 skip_hdr_extension(&ctx->gb);
00419 }
00420
00421 align_get_bits(&ctx->gb);
00422
00423 return 0;
00424 }
00425
00426
00437 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
00438 IVITile *tile, AVCodecContext *avctx)
00439 {
00440 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
00441 mv_scale, blks_per_mb, s;
00442 IVIMbInfo *mb, *ref_mb;
00443 int row_offset = band->mb_size * band->pitch;
00444
00445 mb = tile->mbs;
00446 ref_mb = tile->ref_mbs;
00447 offs = tile->ypos * band->pitch + tile->xpos;
00448
00449 if (!ref_mb &&
00450 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
00451 return AVERROR_INVALIDDATA;
00452
00453 if( tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size) ){
00454 av_log(avctx, AV_LOG_ERROR, "allocated tile size %d mismatches parameters %d\n",
00455 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
00456 return AVERROR_INVALIDDATA;
00457 }
00458
00459
00460 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00461 mv_x = mv_y = 0;
00462
00463 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
00464 mb_offset = offs;
00465
00466 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
00467 mb->xpos = x;
00468 mb->ypos = y;
00469 mb->buf_offs = mb_offset;
00470
00471 if (get_bits1(&ctx->gb)) {
00472 if (ctx->frame_type == FRAMETYPE_INTRA) {
00473 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00474 return -1;
00475 }
00476 mb->type = 1;
00477 mb->cbp = 0;
00478
00479 mb->q_delta = 0;
00480 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
00481 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00482 IVI_VLC_BITS, 1);
00483 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00484 }
00485
00486 mb->mv_x = mb->mv_y = 0;
00487 if (band->inherit_mv && ref_mb){
00488
00489 if (mv_scale) {
00490 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00491 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00492 } else {
00493 mb->mv_x = ref_mb->mv_x;
00494 mb->mv_y = ref_mb->mv_y;
00495 }
00496 }
00497 } else {
00498 if (band->inherit_mv && ref_mb) {
00499 mb->type = ref_mb->type;
00500 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00501 mb->type = 0;
00502 } else {
00503 mb->type = get_bits1(&ctx->gb);
00504 }
00505
00506 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00507 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00508
00509 mb->q_delta = 0;
00510 if (band->qdelta_present) {
00511 if (band->inherit_qdelta) {
00512 if (ref_mb) mb->q_delta = ref_mb->q_delta;
00513 } else if (mb->cbp || (!band->plane && !band->band_num &&
00514 (ctx->frame_flags & 8))) {
00515 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00516 IVI_VLC_BITS, 1);
00517 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00518 }
00519 }
00520
00521 if (!mb->type) {
00522 mb->mv_x = mb->mv_y = 0;
00523 } else {
00524 if (band->inherit_mv && ref_mb){
00525
00526 if (mv_scale) {
00527 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00528 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00529 } else {
00530 mb->mv_x = ref_mb->mv_x;
00531 mb->mv_y = ref_mb->mv_y;
00532 }
00533 } else {
00534
00535 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00536 IVI_VLC_BITS, 1);
00537 mv_y += IVI_TOSIGNED(mv_delta);
00538 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00539 IVI_VLC_BITS, 1);
00540 mv_x += IVI_TOSIGNED(mv_delta);
00541 mb->mv_x = mv_x;
00542 mb->mv_y = mv_y;
00543 }
00544 }
00545 }
00546
00547 s= band->is_halfpel;
00548 if (mb->type)
00549 if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
00550 x + ((mb->mv_x+s)>>s) + band->mb_size - 1
00551 + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
00552 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
00553 return AVERROR_INVALIDDATA;
00554 }
00555
00556 mb++;
00557 if (ref_mb)
00558 ref_mb++;
00559 mb_offset += band->mb_size;
00560 }
00561
00562 offs += row_offset;
00563 }
00564
00565 align_get_bits(&ctx->gb);
00566
00567 return 0;
00568 }
00569
00570
00576 static void switch_buffers(IVI45DecContext *ctx)
00577 {
00578 switch (ctx->prev_frame_type) {
00579 case FRAMETYPE_INTRA:
00580 case FRAMETYPE_INTER:
00581 ctx->buf_switch ^= 1;
00582 ctx->dst_buf = ctx->buf_switch;
00583 ctx->ref_buf = ctx->buf_switch ^ 1;
00584 break;
00585 case FRAMETYPE_INTER_SCAL:
00586 if (!ctx->inter_scal) {
00587 ctx->ref2_buf = 2;
00588 ctx->inter_scal = 1;
00589 }
00590 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
00591 ctx->ref_buf = ctx->ref2_buf;
00592 break;
00593 case FRAMETYPE_INTER_NOREF:
00594 break;
00595 }
00596
00597 switch (ctx->frame_type) {
00598 case FRAMETYPE_INTRA:
00599 ctx->buf_switch = 0;
00600
00601 case FRAMETYPE_INTER:
00602 ctx->inter_scal = 0;
00603 ctx->dst_buf = ctx->buf_switch;
00604 ctx->ref_buf = ctx->buf_switch ^ 1;
00605 break;
00606 case FRAMETYPE_INTER_SCAL:
00607 case FRAMETYPE_INTER_NOREF:
00608 case FRAMETYPE_NULL:
00609 break;
00610 }
00611 }
00612
00613
00614 static int is_nonnull_frame(IVI45DecContext *ctx)
00615 {
00616 return ctx->frame_type != FRAMETYPE_NULL;
00617 }
00618
00619
00623 static av_cold int decode_init(AVCodecContext *avctx)
00624 {
00625 IVI45DecContext *ctx = avctx->priv_data;
00626 int result;
00627
00628 ff_ivi_init_static_vlc();
00629
00630
00631 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00632
00633
00634
00635
00636 ctx->pic_conf.pic_width = avctx->width;
00637 ctx->pic_conf.pic_height = avctx->height;
00638 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
00639 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
00640 ctx->pic_conf.tile_width = avctx->width;
00641 ctx->pic_conf.tile_height = avctx->height;
00642 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
00643
00644 avcodec_get_frame_defaults(&ctx->frame);
00645
00646 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
00647 if (result) {
00648 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
00649 return -1;
00650 }
00651
00652 ctx->buf_switch = 0;
00653 ctx->inter_scal = 0;
00654
00655 ctx->decode_pic_hdr = decode_pic_hdr;
00656 ctx->decode_band_hdr = decode_band_hdr;
00657 ctx->decode_mb_info = decode_mb_info;
00658 ctx->switch_buffers = switch_buffers;
00659 ctx->is_nonnull_frame = is_nonnull_frame;
00660
00661 avctx->pix_fmt = PIX_FMT_YUV410P;
00662
00663 return 0;
00664 }
00665
00666 AVCodec ff_indeo5_decoder = {
00667 .name = "indeo5",
00668 .type = AVMEDIA_TYPE_VIDEO,
00669 .id = CODEC_ID_INDEO5,
00670 .priv_data_size = sizeof(IVI45DecContext),
00671 .init = decode_init,
00672 .close = ff_ivi_decode_close,
00673 .decode = ff_ivi_decode_frame,
00674 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
00675 };