• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 (!transforms[transform_id].inv_trans) {
00376                 av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
00377                 return AVERROR_PATCHWELCOME;
00378             }
00379             if ((transform_id >= 7 && transform_id <= 9) ||
00380                  transform_id == 17) {
00381                 av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
00382                 return AVERROR_PATCHWELCOME;
00383             }
00384 
00385 #if IVI4_STREAM_ANALYSER
00386             if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
00387                 ctx->uses_haar = 1;
00388 #endif
00389 
00390             band->inv_transform = transforms[transform_id].inv_trans;
00391             band->dc_transform  = transforms[transform_id].dc_trans;
00392             band->is_2d_trans   = transforms[transform_id].is_2d_trans;
00393 
00394             scan_indx = get_bits(&ctx->gb, 4);
00395             if (scan_indx == 15) {
00396                 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
00397                 return AVERROR_INVALIDDATA;
00398             }
00399             band->scan = scan_index_to_tab[scan_indx];
00400 
00401             band->quant_mat = get_bits(&ctx->gb, 5);
00402             if (band->quant_mat == 31) {
00403                 av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
00404                 return AVERROR_INVALIDDATA;
00405             }
00406         }
00407 
00408         /* decode block huffman codebook */
00409         if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,
00410                                  &band->blk_vlc, avctx))
00411             return AVERROR_INVALIDDATA;
00412 
00413         /* select appropriate rvmap table for this band */
00414         band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
00415 
00416         /* decode rvmap probability corrections if any */
00417         band->num_corr = 0; /* there is no corrections */
00418         if (get_bits1(&ctx->gb)) {
00419             band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
00420             if (band->num_corr > 61) {
00421                 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00422                        band->num_corr);
00423                 return AVERROR_INVALIDDATA;
00424             }
00425 
00426             /* read correction pairs */
00427             for (i = 0; i < band->num_corr * 2; i++)
00428                 band->corr[i] = get_bits(&ctx->gb, 8);
00429         }
00430     }
00431 
00432     if (band->blk_size == 8) {
00433         band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
00434         band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
00435     } else {
00436         band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
00437         band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
00438     }
00439 
00440     /* Indeo 4 doesn't use scale tables */
00441     band->intra_scale = NULL;
00442     band->inter_scale = NULL;
00443 
00444     align_get_bits(&ctx->gb);
00445 
00446     return 0;
00447 }
00448 
00449 
00460 static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,
00461                           IVITile *tile, AVCodecContext *avctx)
00462 {
00463     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
00464                 mv_scale, mb_type_bits;
00465     IVIMbInfo   *mb, *ref_mb;
00466     int         row_offset = band->mb_size * band->pitch;
00467 
00468     mb     = tile->mbs;
00469     ref_mb = tile->ref_mbs;
00470     offs   = tile->ypos * band->pitch + tile->xpos;
00471 
00472     blks_per_mb  = band->mb_size   != band->blk_size  ? 4 : 1;
00473     mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;
00474 
00475     /* scale factor for motion vectors */
00476     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00477     mv_x = mv_y = 0;
00478 
00479     for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
00480         mb_offset = offs;
00481 
00482         for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
00483             mb->xpos     = x;
00484             mb->ypos     = y;
00485             mb->buf_offs = mb_offset;
00486 
00487             if (get_bits1(&ctx->gb)) {
00488                 if (ctx->frame_type == FRAMETYPE_INTRA) {
00489                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00490                     return AVERROR_INVALIDDATA;
00491                 }
00492                 mb->type = 1; /* empty macroblocks are always INTER */
00493                 mb->cbp  = 0; /* all blocks are empty */
00494 
00495                 mb->q_delta = 0;
00496                 if (!band->plane && !band->band_num && ctx->in_q) {
00497                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00498                                            IVI_VLC_BITS, 1);
00499                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00500                 }
00501 
00502                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
00503                 if (band->inherit_mv) {
00504                     /* motion vector inheritance */
00505                     if (mv_scale) {
00506                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00507                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00508                     } else {
00509                         mb->mv_x = ref_mb->mv_x;
00510                         mb->mv_y = ref_mb->mv_y;
00511                     }
00512                 }
00513             } else {
00514                 if (band->inherit_mv) {
00515                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
00516                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00517                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
00518                 } else {
00519                     mb->type = get_bits(&ctx->gb, mb_type_bits);
00520                 }
00521 
00522                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00523 
00524                 mb->q_delta = 0;
00525                 if (band->inherit_qdelta) {
00526                     if (ref_mb) mb->q_delta = ref_mb->q_delta;
00527                 } else if (mb->cbp || (!band->plane && !band->band_num &&
00528                            ctx->in_q)) {
00529                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00530                                            IVI_VLC_BITS, 1);
00531                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00532                 }
00533 
00534                 if (!mb->type) {
00535                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
00536                 } else {
00537                     if (band->inherit_mv) {
00538                         /* motion vector inheritance */
00539                         if (mv_scale) {
00540                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00541                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00542                         } else {
00543                             mb->mv_x = ref_mb->mv_x;
00544                             mb->mv_y = ref_mb->mv_y;
00545                         }
00546                     } else {
00547                         /* decode motion vector deltas */
00548                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00549                                             IVI_VLC_BITS, 1);
00550                         mv_y += IVI_TOSIGNED(mv_delta);
00551                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00552                                             IVI_VLC_BITS, 1);
00553                         mv_x += IVI_TOSIGNED(mv_delta);
00554                         mb->mv_x = mv_x;
00555                         mb->mv_y = mv_y;
00556                     }
00557                 }
00558             }
00559 
00560             mb++;
00561             if (ref_mb)
00562                 ref_mb++;
00563             mb_offset += band->mb_size;
00564         }
00565 
00566         offs += row_offset;
00567     }
00568 
00569     align_get_bits(&ctx->gb);
00570 
00571     return 0;
00572 }
00573 
00574 
00583 static int decode_band(IVI4DecContext *ctx, int plane_num,
00584                        IVIBandDesc *band, AVCodecContext *avctx)
00585 {
00586     int         result, i, t, pos, idx1, idx2;
00587     IVITile     *tile;
00588 
00589     band->buf     = band->bufs[ctx->dst_buf];
00590     band->ref_buf = band->bufs[ctx->ref_buf];
00591 
00592     result = decode_band_hdr(ctx, band, avctx);
00593     if (result) {
00594         av_log(avctx, AV_LOG_ERROR, "Error decoding band header\n");
00595         return result;
00596     }
00597 
00598     if (band->is_empty) {
00599         av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00600         return AVERROR_INVALIDDATA;
00601     }
00602 
00603     band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
00604 
00605     /* apply corrections to the selected rvmap table if present */
00606     for (i = 0; i < band->num_corr; i++) {
00607         idx1 = band->corr[i * 2];
00608         idx2 = band->corr[i * 2 + 1];
00609         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00610         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00611     }
00612 
00613     pos = get_bits_count(&ctx->gb);
00614 
00615     for (t = 0; t < band->num_tiles; t++) {
00616         tile = &band->tiles[t];
00617 
00618         tile->is_empty = get_bits1(&ctx->gb);
00619         if (tile->is_empty) {
00620             ff_ivi_process_empty_tile(avctx, band, tile,
00621                                       (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00622             av_dlog(avctx, "Empty tile encountered!\n");
00623         } else {
00624             tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00625             if (!tile->data_size) {
00626                 av_log(avctx, AV_LOG_ERROR, "Tile data size is zero!\n");
00627                 return AVERROR_INVALIDDATA;
00628             }
00629 
00630             result = decode_mb_info(ctx, band, tile, avctx);
00631             if (result < 0)
00632                 break;
00633 
00634             result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00635             if (result < 0 || ((get_bits_count(&ctx->gb) - pos) >> 3) != tile->data_size) {
00636                 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00637                 break;
00638             }
00639 
00640             pos += tile->data_size << 3; // skip to next tile
00641         }
00642     }
00643 
00644     /* restore the selected rvmap table by applying its corrections in reverse order */
00645     for (i = band->num_corr - 1; i >= 0; i--) {
00646         idx1 = band->corr[i * 2];
00647         idx2 = band->corr[i * 2 + 1];
00648         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00649         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00650     }
00651 
00652 #if defined(DEBUG) && IVI4_DEBUG_CHECKSUM
00653     if (band->checksum_present) {
00654         uint16_t chksum = ivi_calc_band_checksum(band);
00655         if (chksum != band->checksum) {
00656             av_log(avctx, AV_LOG_ERROR,
00657                    "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
00658                    band->plane, band->band_num, band->checksum, chksum);
00659         }
00660     }
00661 #endif
00662 
00663     align_get_bits(&ctx->gb);
00664 
00665     return 0;
00666 }
00667 
00668 
00669 static av_cold int decode_init(AVCodecContext *avctx)
00670 {
00671     IVI4DecContext *ctx = avctx->priv_data;
00672 
00673     ff_ivi_init_static_vlc();
00674 
00675     /* copy rvmap tables in our context so we can apply changes to them */
00676     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00677 
00678     /* Force allocation of the internal buffers */
00679     /* during picture header decoding.          */
00680     ctx->pic_conf.pic_width  = 0;
00681     ctx->pic_conf.pic_height = 0;
00682 
00683     avctx->pix_fmt = PIX_FMT_YUV410P;
00684 
00685     return 0;
00686 }
00687 
00688 
00694 static void switch_buffers(IVI4DecContext *ctx)
00695 {
00696     switch (ctx->prev_frame_type) {
00697     case FRAMETYPE_INTRA:
00698     case FRAMETYPE_INTER:
00699         ctx->buf_switch ^= 1;
00700         ctx->dst_buf     = ctx->buf_switch;
00701         ctx->ref_buf     = ctx->buf_switch ^ 1;
00702         break;
00703     case FRAMETYPE_INTER_NOREF:
00704         break;
00705     }
00706 
00707     switch (ctx->frame_type) {
00708     case FRAMETYPE_INTRA:
00709         ctx->buf_switch = 0;
00710         /* FALLTHROUGH */
00711     case FRAMETYPE_INTER:
00712         ctx->dst_buf = ctx->buf_switch;
00713         ctx->ref_buf = ctx->buf_switch ^ 1;
00714         break;
00715     case FRAMETYPE_INTER_NOREF:
00716     case FRAMETYPE_NULL_FIRST:
00717     case FRAMETYPE_NULL_LAST:
00718         break;
00719     }
00720 }
00721 
00722 
00723 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00724                         AVPacket *avpkt)
00725 {
00726     IVI4DecContext  *ctx = avctx->priv_data;
00727     const uint8_t   *buf = avpkt->data;
00728     int             buf_size = avpkt->size;
00729     int             result, p, b;
00730 
00731     init_get_bits(&ctx->gb, buf, buf_size * 8);
00732 
00733     result = decode_pic_hdr(ctx, avctx);
00734     if (result) {
00735         av_log(avctx, AV_LOG_ERROR, "Error decoding picture header\n");
00736         return result;
00737     }
00738 
00739     switch_buffers(ctx);
00740 
00741     if (ctx->frame_type < FRAMETYPE_NULL_FIRST) {
00742         for (p = 0; p < 3; p++) {
00743             for (b = 0; b < ctx->planes[p].num_bands; b++) {
00744                 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00745                 if (result) {
00746                     av_log(avctx, AV_LOG_ERROR,
00747                            "Error decoding band: %d, plane: %d\n", b, p);
00748                     return result;
00749                 }
00750             }
00751         }
00752     }
00753 
00754     /* If the bidirectional mode is enabled, next I and the following P frame will */
00755     /* be sent together. Unfortunately the approach below seems to be the only way */
00756     /* to handle the B-frames mode. That's exactly the same Intel decoders do.     */
00757     if (ctx->frame_type == FRAMETYPE_INTRA) {
00758         while (get_bits(&ctx->gb, 8)); // skip version string
00759         skip_bits_long(&ctx->gb, 64);  // skip padding, TODO: implement correct 8-bytes alignment
00760         if (get_bits_left(&ctx->gb) > 18 && show_bits(&ctx->gb, 18) == 0x3FFF8)
00761             av_log(avctx, AV_LOG_ERROR, "Buffer contains IP frames!\n");
00762     }
00763 
00764     if (ctx->frame.data[0])
00765         avctx->release_buffer(avctx, &ctx->frame);
00766 
00767     ctx->frame.reference = 0;
00768     if ((result = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
00769         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00770         return result;
00771     }
00772 
00773     if (ctx->is_scalable) {
00774         ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00775     } else {
00776         ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00777     }
00778 
00779     ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00780     ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00781 
00782     *data_size = sizeof(AVFrame);
00783     *(AVFrame*)data = ctx->frame;
00784 
00785     return buf_size;
00786 }
00787 
00788 
00789 static av_cold int decode_close(AVCodecContext *avctx)
00790 {
00791     IVI4DecContext *ctx = avctx->priv_data;
00792 
00793     ff_ivi_free_buffers(&ctx->planes[0]);
00794 
00795     if (ctx->frame.data[0])
00796         avctx->release_buffer(avctx, &ctx->frame);
00797 
00798 #if IVI4_STREAM_ANALYSER
00799     if (ctx->is_scalable)
00800         av_log(avctx, AV_LOG_ERROR, "This video uses scalability mode!\n");
00801     if (ctx->uses_tiling)
00802         av_log(avctx, AV_LOG_ERROR, "This video uses local decoding!\n");
00803     if (ctx->has_b_frames)
00804         av_log(avctx, AV_LOG_ERROR, "This video contains B-frames!\n");
00805     if (ctx->has_transp)
00806         av_log(avctx, AV_LOG_ERROR, "Transparency mode is enabled!\n");
00807     if (ctx->uses_haar)
00808         av_log(avctx, AV_LOG_ERROR, "This video uses Haar transform!\n");
00809     if (ctx->uses_fullpel)
00810         av_log(avctx, AV_LOG_ERROR, "This video uses fullpel motion vectors!\n");
00811 #endif
00812 
00813     return 0;
00814 }
00815 
00816 
00817 AVCodec ff_indeo4_decoder = {
00818     .name           = "indeo4",
00819     .type           = AVMEDIA_TYPE_VIDEO,
00820     .id             = CODEC_ID_INDEO4,
00821     .priv_data_size = sizeof(IVI4DecContext),
00822     .init           = decode_init,
00823     .close          = decode_close,
00824     .decode         = decode_frame,
00825     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
00826 };
Generated on Sat Mar 17 2012 12:57:45 for Libav by doxygen 1.7.1