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 "indeo4data.h"
00037
00041 enum {
00042 FRAMETYPE_INTRA = 0,
00043 FRAMETYPE_BIDIR1 = 1,
00044 FRAMETYPE_INTER = 2,
00045 FRAMETYPE_BIDIR = 3,
00046 FRAMETYPE_INTER_NOREF = 4,
00047 FRAMETYPE_NULL_FIRST = 5,
00048 FRAMETYPE_NULL_LAST = 6
00049 };
00050
00051 #define IVI4_PIC_SIZE_ESC 7
00052
00053
00054 static const struct {
00055 InvTransformPtr *inv_trans;
00056 DCTransformPtr *dc_trans;
00057 int is_2d_trans;
00058 } transforms[18] = {
00059 { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 },
00060 { NULL, NULL, 0 },
00061 { NULL, NULL, 0 },
00062 { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 },
00063 { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 },
00064 { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 },
00065 { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 },
00066 { NULL, NULL, 0 },
00067 { NULL, NULL, 0 },
00068 { NULL, NULL, 0 },
00069 { NULL, NULL, 0 },
00070 { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 },
00071 { NULL, NULL, 0 },
00072 { NULL, NULL, 0 },
00073 { NULL, NULL, 0 },
00074 { NULL, NULL, 0 },
00075 { NULL, NULL, 0 },
00076 { NULL, NULL, 0 },
00077 };
00078
00089 static int decode_plane_subdivision(GetBitContext *gb)
00090 {
00091 int i;
00092
00093 switch (get_bits(gb, 2)) {
00094 case 3:
00095 return 1;
00096 case 2:
00097 for (i = 0; i < 4; i++)
00098 if (get_bits(gb, 2) != 3)
00099 return 0;
00100 return 4;
00101 default:
00102 return 0;
00103 }
00104 }
00105
00106 static inline int scale_tile_size(int def_size, int size_factor)
00107 {
00108 return size_factor == 15 ? def_size : (size_factor + 1) << 5;
00109 }
00110
00118 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
00119 {
00120 int pic_size_indx, i, p;
00121 IVIPicConfig pic_conf;
00122
00123 if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
00124 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00125 return AVERROR_INVALIDDATA;
00126 }
00127
00128 ctx->prev_frame_type = ctx->frame_type;
00129 ctx->frame_type = get_bits(&ctx->gb, 3);
00130 if (ctx->frame_type == 7) {
00131 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
00132 return AVERROR_INVALIDDATA;
00133 }
00134
00135 #if IVI4_STREAM_ANALYSER
00136 if ( ctx->frame_type == FRAMETYPE_BIDIR1
00137 || ctx->frame_type == FRAMETYPE_BIDIR)
00138 ctx->has_b_frames = 1;
00139 #endif
00140
00141 ctx->transp_status = get_bits1(&ctx->gb);
00142 #if IVI4_STREAM_ANALYSER
00143 if (ctx->transp_status) {
00144 ctx->has_transp = 1;
00145 }
00146 #endif
00147
00148
00149 if (get_bits1(&ctx->gb)) {
00150 av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
00151 return AVERROR_INVALIDDATA;
00152 }
00153
00154 ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
00155
00156
00157 if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {
00158 av_dlog(avctx, "Null frame encountered!\n");
00159 return 0;
00160 }
00161
00162
00163
00164
00165 if (get_bits1(&ctx->gb)) {
00166 skip_bits_long(&ctx->gb, 32);
00167 av_dlog(avctx, "Password-protected clip!\n");
00168 }
00169
00170 pic_size_indx = get_bits(&ctx->gb, 3);
00171 if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
00172 pic_conf.pic_height = get_bits(&ctx->gb, 16);
00173 pic_conf.pic_width = get_bits(&ctx->gb, 16);
00174 } else {
00175 pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
00176 pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
00177 }
00178
00179
00180 if (get_bits1(&ctx->gb)) {
00181 pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
00182 pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
00183 #if IVI4_STREAM_ANALYSER
00184 ctx->uses_tiling = 1;
00185 #endif
00186 } else {
00187 pic_conf.tile_height = pic_conf.pic_height;
00188 pic_conf.tile_width = pic_conf.pic_width;
00189 }
00190
00191
00192 if (get_bits(&ctx->gb, 2)) {
00193 av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
00194 return AVERROR_INVALIDDATA;
00195 }
00196 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00197 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
00198
00199
00200 pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
00201 if (pic_conf.luma_bands)
00202 pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
00203 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00204 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00205 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00206 pic_conf.luma_bands, pic_conf.chroma_bands);
00207 return AVERROR_INVALIDDATA;
00208 }
00209
00210
00211 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00212 if (ff_ivi_init_planes(ctx->planes, &pic_conf)) {
00213 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00214 ctx->pic_conf.luma_bands = 0;
00215 return AVERROR(ENOMEM);
00216 }
00217
00218 ctx->pic_conf = pic_conf;
00219
00220
00221 for (p = 0; p <= 2; p++) {
00222 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00223 ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
00224 ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
00225 }
00226 }
00227
00228 if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
00229 ctx->pic_conf.tile_height)) {
00230 av_log(avctx, AV_LOG_ERROR,
00231 "Couldn't reallocate internal structures!\n");
00232 return AVERROR(ENOMEM);
00233 }
00234 }
00235
00236 ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
00237
00238
00239 if (get_bits1(&ctx->gb))
00240 skip_bits(&ctx->gb, 8);
00241
00242
00243 if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
00244 ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
00245 return AVERROR_INVALIDDATA;
00246
00247 ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00248
00249 ctx->in_imf = get_bits1(&ctx->gb);
00250 ctx->in_q = get_bits1(&ctx->gb);
00251
00252 ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
00253
00254
00255 ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
00256
00257 ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
00258
00259
00260 while (get_bits1(&ctx->gb)) {
00261 av_dlog(avctx, "Pic hdr extension encountered!\n");
00262 skip_bits(&ctx->gb, 8);
00263 }
00264
00265 if (get_bits1(&ctx->gb)) {
00266 av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
00267 }
00268
00269 align_get_bits(&ctx->gb);
00270
00271 return 0;
00272 }
00273
00274
00283 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
00284 AVCodecContext *avctx)
00285 {
00286 int plane, band_num, indx, transform_id, scan_indx;
00287 int i;
00288
00289 plane = get_bits(&ctx->gb, 2);
00290 band_num = get_bits(&ctx->gb, 4);
00291 if (band->plane != plane || band->band_num != band_num) {
00292 av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
00293 return AVERROR_INVALIDDATA;
00294 }
00295
00296 band->is_empty = get_bits1(&ctx->gb);
00297 if (!band->is_empty) {
00298
00299
00300 if (get_bits1(&ctx->gb))
00301 skip_bits(&ctx->gb, 16);
00302
00303 band->is_halfpel = get_bits(&ctx->gb, 2);
00304 if (band->is_halfpel >= 2) {
00305 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
00306 band->is_halfpel);
00307 return AVERROR_INVALIDDATA;
00308 }
00309 #if IVI4_STREAM_ANALYSER
00310 if (!band->is_halfpel)
00311 ctx->uses_fullpel = 1;
00312 #endif
00313
00314 band->checksum_present = get_bits1(&ctx->gb);
00315 if (band->checksum_present)
00316 band->checksum = get_bits(&ctx->gb, 16);
00317
00318 indx = get_bits(&ctx->gb, 2);
00319 if (indx == 3) {
00320 av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
00321 return AVERROR_INVALIDDATA;
00322 }
00323 band->mb_size = 16 >> indx;
00324 band->blk_size = 8 >> (indx >> 1);
00325
00326 band->inherit_mv = get_bits1(&ctx->gb);
00327 band->inherit_qdelta = get_bits1(&ctx->gb);
00328
00329 band->glob_quant = get_bits(&ctx->gb, 5);
00330
00331 if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {
00332 transform_id = get_bits(&ctx->gb, 5);
00333 if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
00334 !transforms[transform_id].inv_trans) {
00335 av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
00336 return AVERROR_PATCHWELCOME;
00337 }
00338 if ((transform_id >= 7 && transform_id <= 9) ||
00339 transform_id == 17) {
00340 av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
00341 return AVERROR_PATCHWELCOME;
00342 }
00343
00344 #if IVI4_STREAM_ANALYSER
00345 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
00346 ctx->uses_haar = 1;
00347 #endif
00348
00349 band->inv_transform = transforms[transform_id].inv_trans;
00350 band->dc_transform = transforms[transform_id].dc_trans;
00351 band->is_2d_trans = transforms[transform_id].is_2d_trans;
00352 if (transform_id < 10)
00353 band->transform_size = 8;
00354 else
00355 band->transform_size = 4;
00356
00357 if (band->blk_size != band->transform_size)
00358 return AVERROR_INVALIDDATA;
00359
00360 scan_indx = get_bits(&ctx->gb, 4);
00361 if (scan_indx == 15) {
00362 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
00363 return AVERROR_INVALIDDATA;
00364 }
00365 if (scan_indx > 4 && scan_indx < 10) {
00366 if (band->blk_size != 4)
00367 return AVERROR_INVALIDDATA;
00368 } else if (band->blk_size != 8)
00369 return AVERROR_INVALIDDATA;
00370
00371 band->scan = scan_index_to_tab[scan_indx];
00372
00373 band->quant_mat = get_bits(&ctx->gb, 5);
00374 if (band->quant_mat == 31) {
00375 av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
00376 return AVERROR_INVALIDDATA;
00377 }
00378 if (band->quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
00379 av_log_ask_for_sample(avctx, "Quantization matrix %d",
00380 band->quant_mat);
00381 return AVERROR_INVALIDDATA;
00382 }
00383 }
00384
00385
00386 if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,
00387 &band->blk_vlc, avctx))
00388 return AVERROR_INVALIDDATA;
00389
00390
00391 band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00392
00393
00394 band->num_corr = 0;
00395 if (get_bits1(&ctx->gb)) {
00396 band->num_corr = get_bits(&ctx->gb, 8);
00397 if (band->num_corr > 61) {
00398 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00399 band->num_corr);
00400 return AVERROR_INVALIDDATA;
00401 }
00402
00403
00404 for (i = 0; i < band->num_corr * 2; i++)
00405 band->corr[i] = get_bits(&ctx->gb, 8);
00406 }
00407 }
00408
00409 if (band->blk_size == 8) {
00410 band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
00411 band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
00412 } else {
00413 band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
00414 band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
00415 }
00416
00417
00418 band->intra_scale = NULL;
00419 band->inter_scale = NULL;
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, blks_per_mb,
00441 mv_scale, mb_type_bits;
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 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00450 mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;
00451
00452
00453 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00454 mv_x = mv_y = 0;
00455
00456 for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
00457 mb_offset = offs;
00458
00459 for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
00460 mb->xpos = x;
00461 mb->ypos = y;
00462 mb->buf_offs = mb_offset;
00463
00464 if (get_bits1(&ctx->gb)) {
00465 if (ctx->frame_type == FRAMETYPE_INTRA) {
00466 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00467 return AVERROR_INVALIDDATA;
00468 }
00469 mb->type = 1;
00470 mb->cbp = 0;
00471
00472 mb->q_delta = 0;
00473 if (!band->plane && !band->band_num && ctx->in_q) {
00474 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00475 IVI_VLC_BITS, 1);
00476 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00477 }
00478
00479 mb->mv_x = mb->mv_y = 0;
00480 if (band->inherit_mv && ref_mb) {
00481
00482 if (mv_scale) {
00483 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00484 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00485 } else {
00486 mb->mv_x = ref_mb->mv_x;
00487 mb->mv_y = ref_mb->mv_y;
00488 }
00489 }
00490 } else {
00491 if (band->inherit_mv) {
00492
00493 if (!ref_mb)
00494 return AVERROR_INVALIDDATA;
00495 mb->type = ref_mb->type;
00496 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00497 mb->type = 0;
00498 } else {
00499 mb->type = get_bits(&ctx->gb, mb_type_bits);
00500 }
00501
00502 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00503
00504 mb->q_delta = 0;
00505 if (band->inherit_qdelta) {
00506 if (ref_mb) mb->q_delta = ref_mb->q_delta;
00507 } else if (mb->cbp || (!band->plane && !band->band_num &&
00508 ctx->in_q)) {
00509 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00510 IVI_VLC_BITS, 1);
00511 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00512 }
00513
00514 if (!mb->type) {
00515 mb->mv_x = mb->mv_y = 0;
00516 } else {
00517 if (band->inherit_mv) {
00518 if (ref_mb)
00519
00520 if (mv_scale) {
00521 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00522 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00523 } else {
00524 mb->mv_x = ref_mb->mv_x;
00525 mb->mv_y = ref_mb->mv_y;
00526 }
00527 } else {
00528
00529 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00530 IVI_VLC_BITS, 1);
00531 mv_y += IVI_TOSIGNED(mv_delta);
00532 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00533 IVI_VLC_BITS, 1);
00534 mv_x += IVI_TOSIGNED(mv_delta);
00535 mb->mv_x = mv_x;
00536 mb->mv_y = mv_y;
00537 }
00538 }
00539 }
00540
00541 mb++;
00542 if (ref_mb)
00543 ref_mb++;
00544 mb_offset += band->mb_size;
00545 }
00546
00547 offs += row_offset;
00548 }
00549
00550 align_get_bits(&ctx->gb);
00551
00552 return 0;
00553 }
00554
00555
00561 static void switch_buffers(IVI45DecContext *ctx)
00562 {
00563 switch (ctx->prev_frame_type) {
00564 case FRAMETYPE_INTRA:
00565 case FRAMETYPE_INTER:
00566 ctx->buf_switch ^= 1;
00567 ctx->dst_buf = ctx->buf_switch;
00568 ctx->ref_buf = ctx->buf_switch ^ 1;
00569 break;
00570 case FRAMETYPE_INTER_NOREF:
00571 break;
00572 }
00573
00574 switch (ctx->frame_type) {
00575 case FRAMETYPE_INTRA:
00576 ctx->buf_switch = 0;
00577
00578 case FRAMETYPE_INTER:
00579 ctx->dst_buf = ctx->buf_switch;
00580 ctx->ref_buf = ctx->buf_switch ^ 1;
00581 break;
00582 case FRAMETYPE_INTER_NOREF:
00583 case FRAMETYPE_NULL_FIRST:
00584 case FRAMETYPE_NULL_LAST:
00585 break;
00586 }
00587 }
00588
00589
00590 static int is_nonnull_frame(IVI45DecContext *ctx)
00591 {
00592 return ctx->frame_type < FRAMETYPE_NULL_FIRST;
00593 }
00594
00595
00596 static av_cold int decode_init(AVCodecContext *avctx)
00597 {
00598 IVI45DecContext *ctx = avctx->priv_data;
00599
00600 ff_ivi_init_static_vlc();
00601
00602
00603 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00604
00605
00606
00607 ctx->pic_conf.pic_width = 0;
00608 ctx->pic_conf.pic_height = 0;
00609
00610 avctx->pix_fmt = PIX_FMT_YUV410P;
00611
00612 ctx->decode_pic_hdr = decode_pic_hdr;
00613 ctx->decode_band_hdr = decode_band_hdr;
00614 ctx->decode_mb_info = decode_mb_info;
00615 ctx->switch_buffers = switch_buffers;
00616 ctx->is_nonnull_frame = is_nonnull_frame;
00617
00618 return 0;
00619 }
00620
00621
00622 AVCodec ff_indeo4_decoder = {
00623 .name = "indeo4",
00624 .type = AVMEDIA_TYPE_VIDEO,
00625 .id = CODEC_ID_INDEO4,
00626 .priv_data_size = sizeof(IVI45DecContext),
00627 .init = decode_init,
00628 .close = ff_ivi_decode_close,
00629 .decode = ff_ivi_decode_frame,
00630 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
00631 };