libavcodec/indeo4.c
Go to the documentation of this file.
00001 /*
00002  * Indeo Video Interactive v4 compatible decoder
00003  * Copyright (c) 2009-2011 Maxim Poliakovski
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 
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 static const 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 }, /* inverse Haar 8x1 */
00104     { NULL, NULL, 0 }, /* inverse Haar 1x8 */
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 }, /* inverse DCT 8x8 */
00110     { NULL, NULL, 0 }, /* inverse DCT 8x1 */
00111     { NULL, NULL, 0 }, /* inverse DCT 1x8 */
00112     { NULL, NULL, 0 }, /* inverse Haar 4x4 */
00113     { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d,      1 },
00114     { NULL, NULL, 0 }, /* no transform 4x4 */
00115     { NULL, NULL, 0 }, /* inverse Haar 1x4 */
00116     { NULL, NULL, 0 }, /* inverse Haar 4x1 */
00117     { NULL, NULL, 0 }, /* inverse slant 1x4 */
00118     { NULL, NULL, 0 }, /* inverse slant 4x1 */
00119     { NULL, NULL, 0 }, /* inverse DCT 4x4 */
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, 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     /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
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     /* null frames don't contain anything else so we just return */
00200     if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {
00201         av_dlog(avctx, "Null frame encountered!\n");
00202         return 0;
00203     }
00204 
00205     /* Check key lock status. If enabled - ignore lock word.         */
00206     /* Usually we have to prompt the user for the password, but      */
00207     /* we don't do that because Indeo 4 videos can be decoded anyway */
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     /* Decode tile dimensions. */
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     /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
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     /* decode subdivision of the planes */
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     /* check if picture layout was changed and reallocate buffers */
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         /* set default macroblock/block dimensions */
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     /* skip decTimeEst field if present */
00281     if (get_bits1(&ctx->gb))
00282         skip_bits(&ctx->gb, 8);
00283 
00284     /* decode macroblock and block huffman codebooks */
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     /* TODO: ignore this parameter if unused */
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     /* skip picture header extension if any */
00302     while (get_bits1(&ctx->gb)) {
00303         av_dlog(avctx, "Pic hdr extension encountered!\n");
00304         skip_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, 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         /* skip header size
00341          * If header size is not given, header size is 4 bytes. */
00342         if (get_bits1(&ctx->gb))
00343             skip_bits(&ctx->gb, 16);
00344 
00345         band->is_halfpel = get_bits(&ctx->gb, 2);
00346         if (band->is_halfpel >= 2) {
00347             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
00348                    band->is_halfpel);
00349             return AVERROR_INVALIDDATA;
00350         }
00351 #if IVI4_STREAM_ANALYSER
00352         if (!band->is_halfpel)
00353             ctx->uses_fullpel = 1;
00354 #endif
00355 
00356         band->checksum_present = get_bits1(&ctx->gb);
00357         if (band->checksum_present)
00358             band->checksum = get_bits(&ctx->gb, 16);
00359 
00360         indx = get_bits(&ctx->gb, 2);
00361         if (indx == 3) {
00362             av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
00363             return AVERROR_INVALIDDATA;
00364         }
00365         band->mb_size  = 16 >> indx;
00366         band->blk_size = 8 >> (indx >> 1);
00367 
00368         band->inherit_mv     = get_bits1(&ctx->gb);
00369         band->inherit_qdelta = get_bits1(&ctx->gb);
00370 
00371         band->glob_quant = get_bits(&ctx->gb, 5);
00372 
00373         if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {
00374             transform_id = get_bits(&ctx->gb, 5);
00375             if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
00376                 !transforms[transform_id].inv_trans) {
00377                 av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
00378                 return AVERROR_PATCHWELCOME;
00379             }
00380             if ((transform_id >= 7 && transform_id <= 9) ||
00381                  transform_id == 17) {
00382                 av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
00383                 return AVERROR_PATCHWELCOME;
00384             }
00385 
00386 #if IVI4_STREAM_ANALYSER
00387             if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
00388                 ctx->uses_haar = 1;
00389 #endif
00390 
00391             band->inv_transform = transforms[transform_id].inv_trans;
00392             band->dc_transform  = transforms[transform_id].dc_trans;
00393             band->is_2d_trans   = transforms[transform_id].is_2d_trans;
00394 
00395             scan_indx = get_bits(&ctx->gb, 4);
00396             if (scan_indx == 15) {
00397                 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
00398                 return AVERROR_INVALIDDATA;
00399             }
00400             band->scan = scan_index_to_tab[scan_indx];
00401 
00402             band->quant_mat = get_bits(&ctx->gb, 5);
00403             if (band->quant_mat == 31) {
00404                 av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
00405                 return AVERROR_INVALIDDATA;
00406             }
00407         }
00408 
00409         /* decode block huffman codebook */
00410         if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,
00411                                  &band->blk_vlc, avctx))
00412             return AVERROR_INVALIDDATA;
00413 
00414         /* select appropriate rvmap table for this band */
00415         band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00416 
00417         /* decode rvmap probability corrections if any */
00418         band->num_corr = 0; /* there is no corrections */
00419         if (get_bits1(&ctx->gb)) {
00420             band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
00421             if (band->num_corr > 61) {
00422                 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00423                        band->num_corr);
00424                 return AVERROR_INVALIDDATA;
00425             }
00426 
00427             /* read correction pairs */
00428             for (i = 0; i < band->num_corr * 2; i++)
00429                 band->corr[i] = get_bits(&ctx->gb, 8);
00430         }
00431     }
00432 
00433     if (band->blk_size == 8) {
00434         band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
00435         band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
00436     } else {
00437         band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
00438         band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
00439     }
00440 
00441     /* Indeo 4 doesn't use scale tables */
00442     band->intra_scale = NULL;
00443     band->inter_scale = NULL;
00444 
00445     align_get_bits(&ctx->gb);
00446 
00447     return 0;
00448 }
00449 
00450 
00461 static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,
00462                           IVITile *tile, AVCodecContext *avctx)
00463 {
00464     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
00465                 mv_scale, mb_type_bits;
00466     IVIMbInfo   *mb, *ref_mb;
00467     int         row_offset = band->mb_size * band->pitch;
00468 
00469     mb     = tile->mbs;
00470     ref_mb = tile->ref_mbs;
00471     offs   = tile->ypos * band->pitch + tile->xpos;
00472 
00473     blks_per_mb  = band->mb_size   != band->blk_size  ? 4 : 1;
00474     mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;
00475 
00476     /* scale factor for motion vectors */
00477     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00478     mv_x = mv_y = 0;
00479 
00480     if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {
00481         av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);
00482         return -1;
00483     }
00484 
00485     for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
00486         mb_offset = offs;
00487 
00488         for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
00489             mb->xpos     = x;
00490             mb->ypos     = y;
00491             mb->buf_offs = mb_offset;
00492 
00493             if (get_bits1(&ctx->gb)) {
00494                 if (ctx->frame_type == FRAMETYPE_INTRA) {
00495                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00496                     return AVERROR_INVALIDDATA;
00497                 }
00498                 mb->type = 1; /* empty macroblocks are always INTER */
00499                 mb->cbp  = 0; /* all blocks are empty */
00500 
00501                 mb->q_delta = 0;
00502                 if (!band->plane && !band->band_num && ctx->in_q) {
00503                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00504                                            IVI_VLC_BITS, 1);
00505                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00506                 }
00507 
00508                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
00509                 if (band->inherit_mv) {
00510                     /* motion vector inheritance */
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                 }
00519             } else {
00520                 if (band->inherit_mv) {
00521                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
00522                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00523                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
00524                 } else {
00525                     mb->type = get_bits(&ctx->gb, mb_type_bits);
00526                 }
00527 
00528                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00529 
00530                 mb->q_delta = 0;
00531                 if (band->inherit_qdelta) {
00532                     if (ref_mb) mb->q_delta = ref_mb->q_delta;
00533                 } else if (mb->cbp || (!band->plane && !band->band_num &&
00534                            ctx->in_q)) {
00535                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00536                                            IVI_VLC_BITS, 1);
00537                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00538                 }
00539 
00540                 if (!mb->type) {
00541                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
00542                 } else {
00543                     if (band->inherit_mv) {
00544                         /* motion vector inheritance */
00545                         if (mv_scale) {
00546                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00547                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00548                         } else {
00549                             mb->mv_x = ref_mb->mv_x;
00550                             mb->mv_y = ref_mb->mv_y;
00551                         }
00552                     } else {
00553                         /* decode motion vector deltas */
00554                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00555                                             IVI_VLC_BITS, 1);
00556                         mv_y += IVI_TOSIGNED(mv_delta);
00557                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00558                                             IVI_VLC_BITS, 1);
00559                         mv_x += IVI_TOSIGNED(mv_delta);
00560                         mb->mv_x = mv_x;
00561                         mb->mv_y = mv_y;
00562                     }
00563                 }
00564             }
00565 
00566             mb++;
00567             if (ref_mb)
00568                 ref_mb++;
00569             mb_offset += band->mb_size;
00570         }
00571 
00572         offs += row_offset;
00573     }
00574 
00575     align_get_bits(&ctx->gb);
00576 
00577     return 0;
00578 }
00579 
00580 
00589 static int decode_band(IVI4DecContext *ctx, int plane_num,
00590                        IVIBandDesc *band, AVCodecContext *avctx)
00591 {
00592     int         result, i, t, pos, idx1, idx2;
00593     IVITile     *tile;
00594 
00595     band->buf     = band->bufs[ctx->dst_buf];
00596     band->ref_buf = band->bufs[ctx->ref_buf];
00597 
00598     result = decode_band_hdr(ctx, band, avctx);
00599     if (result) {
00600         av_log(avctx, AV_LOG_ERROR, "Error decoding band header\n");
00601         return result;
00602     }
00603 
00604     if (band->is_empty) {
00605         av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00606         return AVERROR_INVALIDDATA;
00607     }
00608 
00609     band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
00610 
00611     /* apply corrections to the selected rvmap table if present */
00612     for (i = 0; i < band->num_corr; i++) {
00613         idx1 = band->corr[i * 2];
00614         idx2 = band->corr[i * 2 + 1];
00615         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00616         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00617     }
00618 
00619     pos = get_bits_count(&ctx->gb);
00620 
00621     for (t = 0; t < band->num_tiles; t++) {
00622         tile = &band->tiles[t];
00623 
00624         tile->is_empty = get_bits1(&ctx->gb);
00625         if (tile->is_empty) {
00626             ff_ivi_process_empty_tile(avctx, band, tile,
00627                                       (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00628             av_dlog(avctx, "Empty tile encountered!\n");
00629         } else {
00630             tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00631             if (!tile->data_size) {
00632                 av_log(avctx, AV_LOG_ERROR, "Tile data size is zero!\n");
00633                 return AVERROR_INVALIDDATA;
00634             }
00635 
00636             result = decode_mb_info(ctx, band, tile, avctx);
00637             if (result < 0)
00638                 break;
00639 
00640             result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00641             if (result < 0 || ((get_bits_count(&ctx->gb) - pos) >> 3) != tile->data_size) {
00642                 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00643                 break;
00644             }
00645 
00646             pos += tile->data_size << 3; // skip to next tile
00647         }
00648     }
00649 
00650     /* restore the selected rvmap table by applying its corrections in reverse order */
00651     for (i = band->num_corr - 1; i >= 0; i--) {
00652         idx1 = band->corr[i * 2];
00653         idx2 = band->corr[i * 2 + 1];
00654         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00655         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00656     }
00657 
00658 #if defined(DEBUG) && IVI4_DEBUG_CHECKSUM
00659     if (band->checksum_present) {
00660         uint16_t chksum = ivi_calc_band_checksum(band);
00661         if (chksum != band->checksum) {
00662             av_log(avctx, AV_LOG_ERROR,
00663                    "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
00664                    band->plane, band->band_num, band->checksum, chksum);
00665         }
00666     }
00667 #endif
00668 
00669     align_get_bits(&ctx->gb);
00670 
00671     return 0;
00672 }
00673 
00674 
00675 static av_cold int decode_init(AVCodecContext *avctx)
00676 {
00677     IVI4DecContext *ctx = avctx->priv_data;
00678 
00679     ff_ivi_init_static_vlc();
00680 
00681     /* copy rvmap tables in our context so we can apply changes to them */
00682     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00683 
00684     /* Force allocation of the internal buffers */
00685     /* during picture header decoding.          */
00686     ctx->pic_conf.pic_width  = 0;
00687     ctx->pic_conf.pic_height = 0;
00688 
00689     avctx->pix_fmt = PIX_FMT_YUV410P;
00690 
00691     return 0;
00692 }
00693 
00694 
00700 static void switch_buffers(IVI4DecContext *ctx)
00701 {
00702     switch (ctx->prev_frame_type) {
00703     case FRAMETYPE_INTRA:
00704     case FRAMETYPE_INTER:
00705         ctx->buf_switch ^= 1;
00706         ctx->dst_buf     = ctx->buf_switch;
00707         ctx->ref_buf     = ctx->buf_switch ^ 1;
00708         break;
00709     case FRAMETYPE_INTER_NOREF:
00710         break;
00711     }
00712 
00713     switch (ctx->frame_type) {
00714     case FRAMETYPE_INTRA:
00715         ctx->buf_switch = 0;
00716         /* FALLTHROUGH */
00717     case FRAMETYPE_INTER:
00718         ctx->dst_buf = ctx->buf_switch;
00719         ctx->ref_buf = ctx->buf_switch ^ 1;
00720         break;
00721     case FRAMETYPE_INTER_NOREF:
00722     case FRAMETYPE_NULL_FIRST:
00723     case FRAMETYPE_NULL_LAST:
00724         break;
00725     }
00726 }
00727 
00728 
00729 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00730                         AVPacket *avpkt)
00731 {
00732     IVI4DecContext  *ctx = avctx->priv_data;
00733     const uint8_t   *buf = avpkt->data;
00734     int             buf_size = avpkt->size;
00735     int             result, p, b;
00736 
00737     init_get_bits(&ctx->gb, buf, buf_size * 8);
00738 
00739     result = decode_pic_hdr(ctx, avctx);
00740     if (result) {
00741         av_log(avctx, AV_LOG_ERROR, "Error decoding picture header\n");
00742         return result;
00743     }
00744 
00745     switch_buffers(ctx);
00746 
00747     if (ctx->frame_type < FRAMETYPE_NULL_FIRST) {
00748         for (p = 0; p < 3; p++) {
00749             for (b = 0; b < ctx->planes[p].num_bands; b++) {
00750                 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00751                 if (result) {
00752                     av_log(avctx, AV_LOG_ERROR,
00753                            "Error decoding band: %d, plane: %d\n", b, p);
00754                     return result;
00755                 }
00756             }
00757         }
00758     }
00759 
00760     /* If the bidirectional mode is enabled, next I and the following P frame will */
00761     /* be sent together. Unfortunately the approach below seems to be the only way */
00762     /* to handle the B-frames mode. That's exactly the same Intel decoders do.     */
00763     if (ctx->frame_type == FRAMETYPE_INTRA) {
00764         while (get_bits(&ctx->gb, 8)); // skip version string
00765         skip_bits_long(&ctx->gb, 64);  // skip padding, TODO: implement correct 8-bytes alignment
00766         if (get_bits_left(&ctx->gb) > 18 && show_bits(&ctx->gb, 18) == 0x3FFF8)
00767             av_log(avctx, AV_LOG_ERROR, "Buffer contains IP frames!\n");
00768     }
00769 
00770     if (ctx->frame.data[0])
00771         avctx->release_buffer(avctx, &ctx->frame);
00772 
00773     ctx->frame.reference = 0;
00774     if ((result = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
00775         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00776         return result;
00777     }
00778 
00779     if (ctx->is_scalable) {
00780         ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00781     } else {
00782         ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00783     }
00784 
00785     ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00786     ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00787 
00788     *data_size = sizeof(AVFrame);
00789     *(AVFrame*)data = ctx->frame;
00790 
00791     return buf_size;
00792 }
00793 
00794 
00795 static av_cold int decode_close(AVCodecContext *avctx)
00796 {
00797     IVI4DecContext *ctx = avctx->priv_data;
00798 
00799     ff_ivi_free_buffers(&ctx->planes[0]);
00800 
00801     if (ctx->frame.data[0])
00802         avctx->release_buffer(avctx, &ctx->frame);
00803 
00804 #if IVI4_STREAM_ANALYSER
00805     if (ctx->is_scalable)
00806         av_log(avctx, AV_LOG_ERROR, "This video uses scalability mode!\n");
00807     if (ctx->uses_tiling)
00808         av_log(avctx, AV_LOG_ERROR, "This video uses local decoding!\n");
00809     if (ctx->has_b_frames)
00810         av_log(avctx, AV_LOG_ERROR, "This video contains B-frames!\n");
00811     if (ctx->has_transp)
00812         av_log(avctx, AV_LOG_ERROR, "Transparency mode is enabled!\n");
00813     if (ctx->uses_haar)
00814         av_log(avctx, AV_LOG_ERROR, "This video uses Haar transform!\n");
00815     if (ctx->uses_fullpel)
00816         av_log(avctx, AV_LOG_ERROR, "This video uses fullpel motion vectors!\n");
00817 #endif
00818 
00819     return 0;
00820 }
00821 
00822 
00823 AVCodec ff_indeo4_decoder = {
00824     .name           = "indeo4",
00825     .type           = AVMEDIA_TYPE_VIDEO,
00826     .id             = CODEC_ID_INDEO4,
00827     .priv_data_size = sizeof(IVI4DecContext),
00828     .init           = decode_init,
00829     .close          = decode_close,
00830     .decode         = decode_frame,
00831     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
00832 };