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 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
00138 if (blk_size_changed) {
00139 band->mb_size = mb_size;
00140 band->blk_size = blk_size;
00141 }
00142
00143 if (get_bits1(&ctx->gb)) {
00144 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
00145 return -1;
00146 }
00147
00148
00149 switch ((p << 2) + i) {
00150 case 0:
00151 band->inv_transform = ff_ivi_inverse_slant_8x8;
00152 band->dc_transform = ff_ivi_dc_slant_2d;
00153 band->scan = ff_zigzag_direct;
00154 break;
00155
00156 case 1:
00157 band->inv_transform = ff_ivi_row_slant8;
00158 band->dc_transform = ff_ivi_dc_row_slant;
00159 band->scan = ff_ivi_vertical_scan_8x8;
00160 break;
00161
00162 case 2:
00163 band->inv_transform = ff_ivi_col_slant8;
00164 band->dc_transform = ff_ivi_dc_col_slant;
00165 band->scan = ff_ivi_horizontal_scan_8x8;
00166 break;
00167
00168 case 3:
00169 band->inv_transform = ff_ivi_put_pixels_8x8;
00170 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
00171 band->scan = ff_ivi_horizontal_scan_8x8;
00172 break;
00173
00174 case 4:
00175 band->inv_transform = ff_ivi_inverse_slant_4x4;
00176 band->dc_transform = ff_ivi_dc_slant_2d;
00177 band->scan = ff_ivi_direct_scan_4x4;
00178 break;
00179 }
00180
00181 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
00182 band->inv_transform == ff_ivi_inverse_slant_4x4;
00183
00184
00185 if (!p) {
00186 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
00187 } else {
00188 quant_mat = 5;
00189 }
00190
00191 if (band->blk_size == 8) {
00192 if(quant_mat >= 5){
00193 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
00194 return -1;
00195 }
00196 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
00197 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
00198 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
00199 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
00200 } else {
00201 band->intra_base = ivi5_base_quant_4x4_intra;
00202 band->inter_base = ivi5_base_quant_4x4_inter;
00203 band->intra_scale = ivi5_scale_quant_4x4_intra;
00204 band->inter_scale = ivi5_scale_quant_4x4_inter;
00205 }
00206
00207 if (get_bits(&ctx->gb, 2)) {
00208 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
00209 return -1;
00210 }
00211 }
00212 }
00213
00214
00215 for (i = 0; i < pic_conf.chroma_bands; i++) {
00216 band1 = &ctx->planes[1].bands[i];
00217 band2 = &ctx->planes[2].bands[i];
00218
00219 band2->width = band1->width;
00220 band2->height = band1->height;
00221 band2->mb_size = band1->mb_size;
00222 band2->blk_size = band1->blk_size;
00223 band2->is_halfpel = band1->is_halfpel;
00224 band2->intra_base = band1->intra_base;
00225 band2->inter_base = band1->inter_base;
00226 band2->intra_scale = band1->intra_scale;
00227 band2->inter_scale = band1->inter_scale;
00228 band2->scan = band1->scan;
00229 band2->inv_transform = band1->inv_transform;
00230 band2->dc_transform = band1->dc_transform;
00231 band2->is_2d_trans = band1->is_2d_trans;
00232 }
00233
00234
00235 if (blk_size_changed) {
00236 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
00237 pic_conf.tile_height);
00238 if (result) {
00239 av_log(avctx, AV_LOG_ERROR,
00240 "Couldn't reallocate internal structures!\n");
00241 return -1;
00242 }
00243 }
00244
00245 if (ctx->gop_flags & 8) {
00246 if (get_bits(&ctx->gb, 3)) {
00247 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
00248 return -1;
00249 }
00250
00251 if (get_bits1(&ctx->gb))
00252 skip_bits_long(&ctx->gb, 24);
00253 }
00254
00255 align_get_bits(&ctx->gb);
00256
00257 skip_bits(&ctx->gb, 23);
00258
00259
00260 if (get_bits1(&ctx->gb)) {
00261 do {
00262 i = get_bits(&ctx->gb, 16);
00263 } while (i & 0x8000);
00264 }
00265
00266 align_get_bits(&ctx->gb);
00267
00268 return 0;
00269 }
00270
00271
00277 static inline void skip_hdr_extension(GetBitContext *gb)
00278 {
00279 int i, len;
00280
00281 do {
00282 len = get_bits(gb, 8);
00283 for (i = 0; i < len; i++) skip_bits(gb, 8);
00284 } while(len);
00285 }
00286
00287
00295 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
00296 {
00297 if (get_bits(&ctx->gb, 5) != 0x1F) {
00298 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00299 return -1;
00300 }
00301
00302 ctx->prev_frame_type = ctx->frame_type;
00303 ctx->frame_type = get_bits(&ctx->gb, 3);
00304 if (ctx->frame_type >= 5) {
00305 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
00306 return -1;
00307 }
00308
00309 ctx->frame_num = get_bits(&ctx->gb, 8);
00310
00311 if (ctx->frame_type == FRAMETYPE_INTRA) {
00312 ctx->gop_invalid = 1;
00313 if (decode_gop_header(ctx, avctx)) {
00314 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
00315 return AVERROR_INVALIDDATA;
00316 }
00317 ctx->gop_invalid = 0;
00318 }
00319
00320 if (ctx->frame_type != FRAMETYPE_NULL) {
00321 ctx->frame_flags = get_bits(&ctx->gb, 8);
00322
00323 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
00324
00325 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
00326
00327
00328 if (ctx->frame_flags & 0x20)
00329 skip_hdr_extension(&ctx->gb);
00330
00331
00332 if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
00333 return -1;
00334
00335 skip_bits(&ctx->gb, 3);
00336 }
00337
00338 align_get_bits(&ctx->gb);
00339
00340 return 0;
00341 }
00342
00343
00352 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
00353 AVCodecContext *avctx)
00354 {
00355 int i;
00356 uint8_t band_flags;
00357
00358 band_flags = get_bits(&ctx->gb, 8);
00359
00360 if (band_flags & 1) {
00361 band->is_empty = 1;
00362 return 0;
00363 }
00364
00365 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
00366
00367 band->inherit_mv = band_flags & 2;
00368 band->inherit_qdelta = band_flags & 8;
00369 band->qdelta_present = band_flags & 4;
00370 if (!band->qdelta_present) band->inherit_qdelta = 1;
00371
00372
00373 band->num_corr = 0;
00374 if (band_flags & 0x10) {
00375 band->num_corr = get_bits(&ctx->gb, 8);
00376 if (band->num_corr > 61) {
00377 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00378 band->num_corr);
00379 return -1;
00380 }
00381
00382
00383 for (i = 0; i < band->num_corr * 2; i++)
00384 band->corr[i] = get_bits(&ctx->gb, 8);
00385 }
00386
00387
00388 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
00389
00390
00391 if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
00392 return -1;
00393
00394 band->checksum_present = get_bits1(&ctx->gb);
00395 if (band->checksum_present)
00396 band->checksum = get_bits(&ctx->gb, 16);
00397
00398 band->glob_quant = get_bits(&ctx->gb, 5);
00399
00400
00401 if (band_flags & 0x20) {
00402 align_get_bits(&ctx->gb);
00403 skip_hdr_extension(&ctx->gb);
00404 }
00405
00406 align_get_bits(&ctx->gb);
00407
00408 return 0;
00409 }
00410
00411
00422 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
00423 IVITile *tile, AVCodecContext *avctx)
00424 {
00425 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
00426 mv_scale, blks_per_mb;
00427 IVIMbInfo *mb, *ref_mb;
00428 int row_offset = band->mb_size * band->pitch;
00429
00430 mb = tile->mbs;
00431 ref_mb = tile->ref_mbs;
00432 offs = tile->ypos * band->pitch + tile->xpos;
00433
00434 if (!ref_mb &&
00435 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
00436 return AVERROR_INVALIDDATA;
00437
00438 if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
00439 av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
00440 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
00441 return AVERROR_INVALIDDATA;
00442 }
00443
00444
00445 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00446 mv_x = mv_y = 0;
00447
00448 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
00449 mb_offset = offs;
00450
00451 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
00452 mb->xpos = x;
00453 mb->ypos = y;
00454 mb->buf_offs = mb_offset;
00455
00456 if (get_bits1(&ctx->gb)) {
00457 if (ctx->frame_type == FRAMETYPE_INTRA) {
00458 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00459 return -1;
00460 }
00461 mb->type = 1;
00462 mb->cbp = 0;
00463
00464 mb->q_delta = 0;
00465 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
00466 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00467 IVI_VLC_BITS, 1);
00468 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00469 }
00470
00471 mb->mv_x = mb->mv_y = 0;
00472 if (band->inherit_mv && ref_mb){
00473
00474 if (mv_scale) {
00475 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00476 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00477 } else {
00478 mb->mv_x = ref_mb->mv_x;
00479 mb->mv_y = ref_mb->mv_y;
00480 }
00481 }
00482 } else {
00483 if (band->inherit_mv && ref_mb) {
00484 mb->type = ref_mb->type;
00485 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00486 mb->type = 0;
00487 } else {
00488 mb->type = get_bits1(&ctx->gb);
00489 }
00490
00491 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00492 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00493
00494 mb->q_delta = 0;
00495 if (band->qdelta_present) {
00496 if (band->inherit_qdelta) {
00497 if (ref_mb) mb->q_delta = ref_mb->q_delta;
00498 } else if (mb->cbp || (!band->plane && !band->band_num &&
00499 (ctx->frame_flags & 8))) {
00500 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00501 IVI_VLC_BITS, 1);
00502 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00503 }
00504 }
00505
00506 if (!mb->type) {
00507 mb->mv_x = mb->mv_y = 0;
00508 } else {
00509 if (band->inherit_mv && ref_mb){
00510
00511 if (mv_scale) {
00512 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00513 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00514 } else {
00515 mb->mv_x = ref_mb->mv_x;
00516 mb->mv_y = ref_mb->mv_y;
00517 }
00518 } else {
00519
00520 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00521 IVI_VLC_BITS, 1);
00522 mv_y += IVI_TOSIGNED(mv_delta);
00523 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00524 IVI_VLC_BITS, 1);
00525 mv_x += IVI_TOSIGNED(mv_delta);
00526 mb->mv_x = mv_x;
00527 mb->mv_y = mv_y;
00528 }
00529 }
00530 }
00531
00532 mb++;
00533 if (ref_mb)
00534 ref_mb++;
00535 mb_offset += band->mb_size;
00536 }
00537
00538 offs += row_offset;
00539 }
00540
00541 align_get_bits(&ctx->gb);
00542
00543 return 0;
00544 }
00545
00546
00552 static void switch_buffers(IVI45DecContext *ctx)
00553 {
00554 switch (ctx->prev_frame_type) {
00555 case FRAMETYPE_INTRA:
00556 case FRAMETYPE_INTER:
00557 ctx->buf_switch ^= 1;
00558 ctx->dst_buf = ctx->buf_switch;
00559 ctx->ref_buf = ctx->buf_switch ^ 1;
00560 break;
00561 case FRAMETYPE_INTER_SCAL:
00562 if (!ctx->inter_scal) {
00563 ctx->ref2_buf = 2;
00564 ctx->inter_scal = 1;
00565 }
00566 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
00567 ctx->ref_buf = ctx->ref2_buf;
00568 break;
00569 case FRAMETYPE_INTER_NOREF:
00570 break;
00571 }
00572
00573 switch (ctx->frame_type) {
00574 case FRAMETYPE_INTRA:
00575 ctx->buf_switch = 0;
00576
00577 case FRAMETYPE_INTER:
00578 ctx->inter_scal = 0;
00579 ctx->dst_buf = ctx->buf_switch;
00580 ctx->ref_buf = ctx->buf_switch ^ 1;
00581 break;
00582 case FRAMETYPE_INTER_SCAL:
00583 case FRAMETYPE_INTER_NOREF:
00584 case FRAMETYPE_NULL:
00585 break;
00586 }
00587 }
00588
00589
00590 static int is_nonnull_frame(IVI45DecContext *ctx)
00591 {
00592 return ctx->frame_type != FRAMETYPE_NULL;
00593 }
00594
00595
00599 static av_cold int decode_init(AVCodecContext *avctx)
00600 {
00601 IVI45DecContext *ctx = avctx->priv_data;
00602 int result;
00603
00604 ff_ivi_init_static_vlc();
00605
00606
00607 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00608
00609
00610
00611
00612 ctx->pic_conf.pic_width = avctx->width;
00613 ctx->pic_conf.pic_height = avctx->height;
00614 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
00615 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
00616 ctx->pic_conf.tile_width = avctx->width;
00617 ctx->pic_conf.tile_height = avctx->height;
00618 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
00619
00620 avcodec_get_frame_defaults(&ctx->frame);
00621
00622 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
00623 if (result) {
00624 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
00625 return -1;
00626 }
00627
00628 ctx->buf_switch = 0;
00629 ctx->inter_scal = 0;
00630
00631 ctx->decode_pic_hdr = decode_pic_hdr;
00632 ctx->decode_band_hdr = decode_band_hdr;
00633 ctx->decode_mb_info = decode_mb_info;
00634 ctx->switch_buffers = switch_buffers;
00635 ctx->is_nonnull_frame = is_nonnull_frame;
00636
00637 avctx->pix_fmt = PIX_FMT_YUV410P;
00638
00639 return 0;
00640 }
00641
00642
00643 AVCodec ff_indeo5_decoder = {
00644 .name = "indeo5",
00645 .type = AVMEDIA_TYPE_VIDEO,
00646 .id = CODEC_ID_INDEO5,
00647 .priv_data_size = sizeof(IVI45DecContext),
00648 .init = decode_init,
00649 .close = ff_ivi_decode_close,
00650 .decode = ff_ivi_decode_frame,
00651 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
00652 };