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