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 "indeo4data.h"
00037
00038 #define IVI4_STREAM_ANALYSER 0
00039 #define IVI4_DEBUG_CHECKSUM 0
00040
00044 enum {
00045 FRAMETYPE_INTRA = 0,
00046 FRAMETYPE_BIDIR1 = 1,
00047 FRAMETYPE_INTER = 2,
00048 FRAMETYPE_BIDIR = 3,
00049 FRAMETYPE_INTER_NOREF = 4,
00050 FRAMETYPE_NULL_FIRST = 5,
00051 FRAMETYPE_NULL_LAST = 6
00052 };
00053
00054 #define IVI4_PIC_SIZE_ESC 7
00055
00056
00057 typedef struct {
00058 GetBitContext gb;
00059 AVFrame frame;
00060 RVMapDesc rvmap_tabs[9];
00061
00062 uint32_t frame_num;
00063 int frame_type;
00064 int prev_frame_type;
00065 uint32_t data_size;
00066 int is_scalable;
00067 int transp_status;
00068
00069 IVIPicConfig pic_conf;
00070 IVIPlaneDesc planes[3];
00071
00072 int buf_switch;
00073 int dst_buf;
00074 int ref_buf;
00075
00076 IVIHuffTab mb_vlc;
00077 IVIHuffTab blk_vlc;
00078
00079 uint16_t checksum;
00080
00081 uint8_t rvmap_sel;
00082 uint8_t in_imf;
00083 uint8_t in_q;
00084 uint8_t pic_glob_quant;
00085 uint8_t unknown1;
00086
00087 #if IVI4_STREAM_ANALYSER
00088 uint8_t has_b_frames;
00089 uint8_t has_transp;
00090 uint8_t uses_tiling;
00091 uint8_t uses_haar;
00092 uint8_t uses_fullpel;
00093 #endif
00094 } IVI4DecContext;
00095
00096
00097 struct {
00098 InvTransformPtr *inv_trans;
00099 DCTransformPtr *dc_trans;
00100 int is_2d_trans;
00101 } transforms[18] = {
00102 { ff_ivi_inverse_haar_8x8, ff_ivi_dc_haar_2d, 1 },
00103 { NULL, NULL, 0 },
00104 { NULL, NULL, 0 },
00105 { ff_ivi_put_pixels_8x8, ff_ivi_put_dc_pixel_8x8, 1 },
00106 { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d, 1 },
00107 { ff_ivi_row_slant8, ff_ivi_dc_row_slant, 1 },
00108 { ff_ivi_col_slant8, ff_ivi_dc_col_slant, 1 },
00109 { NULL, NULL, 0 },
00110 { NULL, NULL, 0 },
00111 { NULL, NULL, 0 },
00112 { NULL, NULL, 0 },
00113 { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d, 1 },
00114 { NULL, NULL, 0 },
00115 { NULL, NULL, 0 },
00116 { NULL, NULL, 0 },
00117 { NULL, NULL, 0 },
00118 { NULL, NULL, 0 },
00119 { NULL, NULL, 0 },
00120 };
00121
00132 static int decode_plane_subdivision(GetBitContext *gb)
00133 {
00134 int i;
00135
00136 switch (get_bits(gb, 2)) {
00137 case 3:
00138 return 1;
00139 case 2:
00140 for (i = 0; i < 4; i++)
00141 if (get_bits(gb, 2) != 3)
00142 return 0;
00143 return 4;
00144 default:
00145 return 0;
00146 }
00147 }
00148
00149 static inline int scale_tile_size(int def_size, int size_factor)
00150 {
00151 return (size_factor == 15 ? def_size : (size_factor + 1) << 5);
00152 }
00153
00161 static int decode_pic_hdr(IVI4DecContext *ctx, AVCodecContext *avctx)
00162 {
00163 int pic_size_indx, val, i, p;
00164 IVIPicConfig pic_conf;
00165
00166 if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
00167 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00168 return AVERROR_INVALIDDATA;
00169 }
00170
00171 ctx->prev_frame_type = ctx->frame_type;
00172 ctx->frame_type = get_bits(&ctx->gb, 3);
00173 if (ctx->frame_type == 7) {
00174 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
00175 return AVERROR_INVALIDDATA;
00176 }
00177
00178 #if IVI4_STREAM_ANALYSER
00179 if ( ctx->frame_type == FRAMETYPE_BIDIR1
00180 || ctx->frame_type == FRAMETYPE_BIDIR)
00181 ctx->has_b_frames = 1;
00182 #endif
00183
00184 ctx->transp_status = get_bits1(&ctx->gb);
00185 #if IVI4_STREAM_ANALYSER
00186 if (ctx->transp_status) {
00187 ctx->has_transp = 1;
00188 }
00189 #endif
00190
00191
00192 if (get_bits1(&ctx->gb)) {
00193 av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
00194 return AVERROR_INVALIDDATA;
00195 }
00196
00197 ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
00198
00199
00200 if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {
00201 av_dlog(avctx, "Null frame encountered!\n");
00202 return 0;
00203 }
00204
00205
00206
00207
00208 if (get_bits1(&ctx->gb)) {
00209 skip_bits_long(&ctx->gb, 32);
00210 av_dlog(avctx, "Password-protected clip!\n");
00211 }
00212
00213 pic_size_indx = get_bits(&ctx->gb, 3);
00214 if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
00215 pic_conf.pic_height = get_bits(&ctx->gb, 16);
00216 pic_conf.pic_width = get_bits(&ctx->gb, 16);
00217 } else {
00218 pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
00219 pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
00220 }
00221
00222
00223 if (get_bits1(&ctx->gb)) {
00224 pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
00225 pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
00226 #if IVI4_STREAM_ANALYSER
00227 ctx->uses_tiling = 1;
00228 #endif
00229 } else {
00230 pic_conf.tile_height = pic_conf.pic_height;
00231 pic_conf.tile_width = pic_conf.pic_width;
00232 }
00233
00234
00235 if (get_bits(&ctx->gb, 2)) {
00236 av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
00237 return AVERROR_INVALIDDATA;
00238 }
00239 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00240 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
00241
00242
00243 pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
00244 if (pic_conf.luma_bands)
00245 pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
00246 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00247 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00248 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00249 pic_conf.luma_bands, pic_conf.chroma_bands);
00250 return AVERROR_INVALIDDATA;
00251 }
00252
00253
00254 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00255 if (ff_ivi_init_planes(ctx->planes, &pic_conf)) {
00256 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00257 return AVERROR(ENOMEM);
00258 }
00259
00260 ctx->pic_conf = pic_conf;
00261
00262
00263 for (p = 0; p <= 2; p++) {
00264 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00265 ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
00266 ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
00267 }
00268 }
00269
00270 if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
00271 ctx->pic_conf.tile_height)) {
00272 av_log(avctx, AV_LOG_ERROR,
00273 "Couldn't reallocate internal structures!\n");
00274 return AVERROR(ENOMEM);
00275 }
00276 }
00277
00278 ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
00279
00280
00281 if (get_bits1(&ctx->gb))
00282 skip_bits(&ctx->gb, 8);
00283
00284
00285 if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
00286 ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
00287 return AVERROR_INVALIDDATA;
00288
00289 ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00290
00291 ctx->in_imf = get_bits1(&ctx->gb);
00292 ctx->in_q = get_bits1(&ctx->gb);
00293
00294 ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
00295
00296
00297 ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
00298
00299 ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
00300
00301
00302 while (get_bits1(&ctx->gb)) {
00303 av_dlog(avctx, "Pic hdr extension encountered!\n");
00304 val = get_bits(&ctx->gb, 8);
00305 }
00306
00307 if (get_bits1(&ctx->gb)) {
00308 av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
00309 }
00310
00311 align_get_bits(&ctx->gb);
00312
00313 return 0;
00314 }
00315
00316
00325 static int decode_band_hdr(IVI4DecContext *ctx, IVIBandDesc *band,
00326 AVCodecContext *avctx)
00327 {
00328 int plane, band_num, hdr_size, indx, transform_id, scan_indx;
00329 int i;
00330
00331 plane = get_bits(&ctx->gb, 2);
00332 band_num = get_bits(&ctx->gb, 4);
00333 if (band->plane != plane || band->band_num != band_num) {
00334 av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
00335 return AVERROR_INVALIDDATA;
00336 }
00337
00338 band->is_empty = get_bits1(&ctx->gb);
00339 if (!band->is_empty) {
00340 hdr_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 4;
00341
00342 band->is_halfpel = get_bits(&ctx->gb, 2);
00343 if (band->is_halfpel >= 2) {
00344 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
00345 band->is_halfpel);
00346 return AVERROR_INVALIDDATA;
00347 }
00348 #if IVI4_STREAM_ANALYSER
00349 if (!band->is_halfpel)
00350 ctx->uses_fullpel = 1;
00351 #endif
00352
00353 band->checksum_present = get_bits1(&ctx->gb);
00354 if (band->checksum_present)
00355 band->checksum = get_bits(&ctx->gb, 16);
00356
00357 indx = get_bits(&ctx->gb, 2);
00358 if (indx == 3) {
00359 av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
00360 return AVERROR_INVALIDDATA;
00361 }
00362 band->mb_size = 16 >> indx;
00363 band->blk_size = 8 >> (indx >> 1);
00364
00365 band->inherit_mv = get_bits1(&ctx->gb);
00366 band->inherit_qdelta = get_bits1(&ctx->gb);
00367
00368 band->glob_quant = get_bits(&ctx->gb, 5);
00369
00370 if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {
00371 transform_id = get_bits(&ctx->gb, 5);
00372 if (!transforms[transform_id].inv_trans) {
00373 av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
00374 return AVERROR_PATCHWELCOME;
00375 }
00376 if ((transform_id >= 7 && transform_id <= 9) ||
00377 transform_id == 17) {
00378 av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
00379 return AVERROR_PATCHWELCOME;
00380 }
00381
00382 #if IVI4_STREAM_ANALYSER
00383 if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
00384 ctx->uses_haar = 1;
00385 #endif
00386
00387 band->inv_transform = transforms[transform_id].inv_trans;
00388 band->dc_transform = transforms[transform_id].dc_trans;
00389 band->is_2d_trans = transforms[transform_id].is_2d_trans;
00390
00391 scan_indx = get_bits(&ctx->gb, 4);
00392 if (scan_indx == 15) {
00393 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
00394 return AVERROR_INVALIDDATA;
00395 }
00396 band->scan = scan_index_to_tab[scan_indx];
00397
00398 band->quant_mat = get_bits(&ctx->gb, 5);
00399 if (band->quant_mat == 31) {
00400 av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
00401 return AVERROR_INVALIDDATA;
00402 }
00403 }
00404
00405
00406 if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,
00407 &band->blk_vlc, avctx))
00408 return AVERROR_INVALIDDATA;
00409
00410
00411 band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00412
00413
00414 band->num_corr = 0;
00415 if (get_bits1(&ctx->gb)) {
00416 band->num_corr = get_bits(&ctx->gb, 8);
00417 if (band->num_corr > 61) {
00418 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00419 band->num_corr);
00420 return AVERROR_INVALIDDATA;
00421 }
00422
00423
00424 for (i = 0; i < band->num_corr * 2; i++)
00425 band->corr[i] = get_bits(&ctx->gb, 8);
00426 }
00427 }
00428
00429 if (band->blk_size == 8) {
00430 band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
00431 band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
00432 } else {
00433 band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
00434 band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
00435 }
00436
00437
00438 band->intra_scale = NULL;
00439 band->inter_scale = NULL;
00440
00441 align_get_bits(&ctx->gb);
00442
00443 return 0;
00444 }
00445
00446
00457 static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,
00458 IVITile *tile, AVCodecContext *avctx)
00459 {
00460 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
00461 mv_scale, mb_type_bits;
00462 IVIMbInfo *mb, *ref_mb;
00463 int row_offset = band->mb_size * band->pitch;
00464
00465 mb = tile->mbs;
00466 ref_mb = tile->ref_mbs;
00467 offs = tile->ypos * band->pitch + tile->xpos;
00468
00469 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00470 mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;
00471
00472
00473 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00474 mv_x = mv_y = 0;
00475
00476 if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {
00477 av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);
00478 return -1;
00479 }
00480
00481 for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
00482 mb_offset = offs;
00483
00484 for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
00485 mb->xpos = x;
00486 mb->ypos = y;
00487 mb->buf_offs = mb_offset;
00488
00489 if (get_bits1(&ctx->gb)) {
00490 if (ctx->frame_type == FRAMETYPE_INTRA) {
00491 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00492 return AVERROR_INVALIDDATA;
00493 }
00494 mb->type = 1;
00495 mb->cbp = 0;
00496
00497 mb->q_delta = 0;
00498 if (!band->plane && !band->band_num && ctx->in_q) {
00499 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00500 IVI_VLC_BITS, 1);
00501 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00502 }
00503
00504 mb->mv_x = mb->mv_y = 0;
00505 if (band->inherit_mv) {
00506
00507 if (mv_scale) {
00508 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00509 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00510 } else {
00511 mb->mv_x = ref_mb->mv_x;
00512 mb->mv_y = ref_mb->mv_y;
00513 }
00514 }
00515 } else {
00516 if (band->inherit_mv) {
00517 mb->type = ref_mb->type;
00518 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00519 mb->type = 0;
00520 } else {
00521 mb->type = get_bits(&ctx->gb, mb_type_bits);
00522 }
00523
00524 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00525
00526 mb->q_delta = 0;
00527 if (band->inherit_qdelta) {
00528 if (ref_mb) mb->q_delta = ref_mb->q_delta;
00529 } else if (mb->cbp || (!band->plane && !band->band_num &&
00530 ctx->in_q)) {
00531 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00532 IVI_VLC_BITS, 1);
00533 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00534 }
00535
00536 if (!mb->type) {
00537 mb->mv_x = mb->mv_y = 0;
00538 } else {
00539 if (band->inherit_mv) {
00540
00541 if (mv_scale) {
00542 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00543 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00544 } else {
00545 mb->mv_x = ref_mb->mv_x;
00546 mb->mv_y = ref_mb->mv_y;
00547 }
00548 } else {
00549
00550 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00551 IVI_VLC_BITS, 1);
00552 mv_y += IVI_TOSIGNED(mv_delta);
00553 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00554 IVI_VLC_BITS, 1);
00555 mv_x += IVI_TOSIGNED(mv_delta);
00556 mb->mv_x = mv_x;
00557 mb->mv_y = mv_y;
00558 }
00559 }
00560 }
00561
00562 mb++;
00563 if (ref_mb)
00564 ref_mb++;
00565 mb_offset += band->mb_size;
00566 }
00567
00568 offs += row_offset;
00569 }
00570
00571 align_get_bits(&ctx->gb);
00572
00573 return 0;
00574 }
00575
00576
00585 static int decode_band(IVI4DecContext *ctx, int plane_num,
00586 IVIBandDesc *band, AVCodecContext *avctx)
00587 {
00588 int result, i, t, pos, idx1, idx2;
00589 IVITile *tile;
00590
00591 band->buf = band->bufs[ctx->dst_buf];
00592 band->ref_buf = band->bufs[ctx->ref_buf];
00593
00594 result = decode_band_hdr(ctx, band, avctx);
00595 if (result) {
00596 av_log(avctx, AV_LOG_ERROR, "Error decoding band header\n");
00597 return result;
00598 }
00599
00600 if (band->is_empty) {
00601 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00602 return AVERROR_INVALIDDATA;
00603 }
00604
00605 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
00606
00607
00608 for (i = 0; i < band->num_corr; i++) {
00609 idx1 = band->corr[i * 2];
00610 idx2 = band->corr[i * 2 + 1];
00611 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00612 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00613 }
00614
00615 pos = get_bits_count(&ctx->gb);
00616
00617 for (t = 0; t < band->num_tiles; t++) {
00618 tile = &band->tiles[t];
00619
00620 tile->is_empty = get_bits1(&ctx->gb);
00621 if (tile->is_empty) {
00622 ff_ivi_process_empty_tile(avctx, band, tile,
00623 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00624 av_dlog(avctx, "Empty tile encountered!\n");
00625 } else {
00626 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00627 if (!tile->data_size) {
00628 av_log(avctx, AV_LOG_ERROR, "Tile data size is zero!\n");
00629 return AVERROR_INVALIDDATA;
00630 }
00631
00632 result = decode_mb_info(ctx, band, tile, avctx);
00633 if (result < 0)
00634 break;
00635
00636 result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00637 if (result < 0 || ((get_bits_count(&ctx->gb) - pos) >> 3) != tile->data_size) {
00638 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00639 break;
00640 }
00641
00642 pos += tile->data_size << 3;
00643 }
00644 }
00645
00646
00647 for (i = band->num_corr - 1; i >= 0; i--) {
00648 idx1 = band->corr[i * 2];
00649 idx2 = band->corr[i * 2 + 1];
00650 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00651 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00652 }
00653
00654 #if defined(DEBUG) && IVI4_DEBUG_CHECKSUM
00655 if (band->checksum_present) {
00656 uint16_t chksum = ivi_calc_band_checksum(band);
00657 if (chksum != band->checksum) {
00658 av_log(avctx, AV_LOG_ERROR,
00659 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
00660 band->plane, band->band_num, band->checksum, chksum);
00661 }
00662 }
00663 #endif
00664
00665 align_get_bits(&ctx->gb);
00666
00667 return 0;
00668 }
00669
00670
00671 static av_cold int decode_init(AVCodecContext *avctx)
00672 {
00673 IVI4DecContext *ctx = avctx->priv_data;
00674
00675 ff_ivi_init_static_vlc();
00676
00677
00678 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00679
00680
00681
00682 ctx->pic_conf.pic_width = 0;
00683 ctx->pic_conf.pic_height = 0;
00684
00685 avctx->pix_fmt = PIX_FMT_YUV410P;
00686
00687 return 0;
00688 }
00689
00690
00696 static void switch_buffers(IVI4DecContext *ctx)
00697 {
00698 switch (ctx->prev_frame_type) {
00699 case FRAMETYPE_INTRA:
00700 case FRAMETYPE_INTER:
00701 ctx->buf_switch ^= 1;
00702 ctx->dst_buf = ctx->buf_switch;
00703 ctx->ref_buf = ctx->buf_switch ^ 1;
00704 break;
00705 case FRAMETYPE_INTER_NOREF:
00706 break;
00707 }
00708
00709 switch (ctx->frame_type) {
00710 case FRAMETYPE_INTRA:
00711 ctx->buf_switch = 0;
00712
00713 case FRAMETYPE_INTER:
00714 ctx->dst_buf = ctx->buf_switch;
00715 ctx->ref_buf = ctx->buf_switch ^ 1;
00716 break;
00717 case FRAMETYPE_INTER_NOREF:
00718 case FRAMETYPE_NULL_FIRST:
00719 case FRAMETYPE_NULL_LAST:
00720 break;
00721 }
00722 }
00723
00724
00725 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00726 AVPacket *avpkt)
00727 {
00728 IVI4DecContext *ctx = avctx->priv_data;
00729 const uint8_t *buf = avpkt->data;
00730 int buf_size = avpkt->size;
00731 int result, p, b;
00732
00733 init_get_bits(&ctx->gb, buf, buf_size * 8);
00734
00735 result = decode_pic_hdr(ctx, avctx);
00736 if (result) {
00737 av_log(avctx, AV_LOG_ERROR, "Error decoding picture header\n");
00738 return result;
00739 }
00740
00741 switch_buffers(ctx);
00742
00743 if (ctx->frame_type < FRAMETYPE_NULL_FIRST) {
00744 for (p = 0; p < 3; p++) {
00745 for (b = 0; b < ctx->planes[p].num_bands; b++) {
00746 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00747 if (result) {
00748 av_log(avctx, AV_LOG_ERROR,
00749 "Error decoding band: %d, plane: %d\n", b, p);
00750 return result;
00751 }
00752 }
00753 }
00754 }
00755
00756
00757
00758
00759 if (ctx->frame_type == FRAMETYPE_INTRA) {
00760 while (get_bits(&ctx->gb, 8));
00761 skip_bits_long(&ctx->gb, 64);
00762 if (get_bits_left(&ctx->gb) > 18 && show_bits(&ctx->gb, 18) == 0x3FFF8)
00763 av_log(avctx, AV_LOG_ERROR, "Buffer contains IP frames!\n");
00764 }
00765
00766 if (ctx->frame.data[0])
00767 avctx->release_buffer(avctx, &ctx->frame);
00768
00769 ctx->frame.reference = 0;
00770 if ((result = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
00771 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00772 return result;
00773 }
00774
00775 if (ctx->is_scalable) {
00776 ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00777 } else {
00778 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00779 }
00780
00781 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00782 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00783
00784 *data_size = sizeof(AVFrame);
00785 *(AVFrame*)data = ctx->frame;
00786
00787 return buf_size;
00788 }
00789
00790
00791 static av_cold int decode_close(AVCodecContext *avctx)
00792 {
00793 IVI4DecContext *ctx = avctx->priv_data;
00794
00795 ff_ivi_free_buffers(&ctx->planes[0]);
00796
00797 if (ctx->frame.data[0])
00798 avctx->release_buffer(avctx, &ctx->frame);
00799
00800 #if IVI4_STREAM_ANALYSER
00801 if (ctx->is_scalable)
00802 av_log(avctx, AV_LOG_ERROR, "This video uses scalability mode!\n");
00803 if (ctx->uses_tiling)
00804 av_log(avctx, AV_LOG_ERROR, "This video uses local decoding!\n");
00805 if (ctx->has_b_frames)
00806 av_log(avctx, AV_LOG_ERROR, "This video contains B-frames!\n");
00807 if (ctx->has_transp)
00808 av_log(avctx, AV_LOG_ERROR, "Transparency mode is enabled!\n");
00809 if (ctx->uses_haar)
00810 av_log(avctx, AV_LOG_ERROR, "This video uses Haar transform!\n");
00811 if (ctx->uses_fullpel)
00812 av_log(avctx, AV_LOG_ERROR, "This video uses fullpel motion vectors!\n");
00813 #endif
00814
00815 return 0;
00816 }
00817
00818
00819 AVCodec ff_indeo4_decoder = {
00820 .name = "indeo4",
00821 .type = AVMEDIA_TYPE_VIDEO,
00822 .id = CODEC_ID_INDEO4,
00823 .priv_data_size = sizeof(IVI4DecContext),
00824 .init = decode_init,
00825 .close = decode_close,
00826 .decode = decode_frame,
00827 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
00828 };