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

libavcodec/indeo5.c

Go to the documentation of this file.
00001 /*
00002  * Indeo Video Interactive v5 compatible decoder
00003  * Copyright (c) 2009 Maxim Poliakovski
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 "indeo5data.h"
00037 
00041 enum {
00042     FRAMETYPE_INTRA       = 0,
00043     FRAMETYPE_INTER       = 1,  
00044     FRAMETYPE_INTER_SCAL  = 2,  
00045     FRAMETYPE_INTER_NOREF = 3,  
00046     FRAMETYPE_NULL        = 4   
00047 };
00048 
00049 #define IVI5_PIC_SIZE_ESC       15
00050 
00051 #define IVI5_IS_PROTECTED       0x20
00052 
00053 typedef struct {
00054     GetBitContext   gb;
00055     AVFrame         frame;
00056     RVMapDesc       rvmap_tabs[9];   
00057     IVIPlaneDesc    planes[3];       
00058     const uint8_t   *frame_data;     
00059     int             buf_switch;      
00060     int             inter_scal;      
00061     int             dst_buf;         
00062     int             ref_buf;         
00063     int             ref2_buf;        
00064     uint32_t        frame_size;      
00065     int             frame_type;
00066     int             prev_frame_type; 
00067     int             frame_num;
00068     uint32_t        pic_hdr_size;    
00069     uint8_t         frame_flags;
00070     uint16_t        checksum;        
00071 
00072     IVIHuffTab      mb_vlc;          
00073 
00074     uint16_t        gop_hdr_size;
00075     uint8_t         gop_flags;
00076     int             is_scalable;
00077     uint32_t        lock_word;
00078     IVIPicConfig    pic_conf;
00079 
00080     int gop_invalid;
00081 } IVI5DecContext;
00082 
00083 
00093 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
00094 {
00095     int             result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
00096     int             quant_mat, blk_size_changed = 0;
00097     IVIBandDesc     *band, *band1, *band2;
00098     IVIPicConfig    pic_conf;
00099 
00100     ctx->gop_flags = get_bits(&ctx->gb, 8);
00101 
00102     ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
00103 
00104     if (ctx->gop_flags & IVI5_IS_PROTECTED)
00105         ctx->lock_word = get_bits_long(&ctx->gb, 32);
00106 
00107     tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
00108     if (tile_size > 256) {
00109         av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
00110         return -1;
00111     }
00112 
00113     /* decode number of wavelet bands */
00114     /* num_levels * 3 + 1 */
00115     pic_conf.luma_bands   = get_bits(&ctx->gb, 2) * 3 + 1;
00116     pic_conf.chroma_bands = get_bits1(&ctx->gb)   * 3 + 1;
00117     ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00118     if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00119         av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00120                pic_conf.luma_bands, pic_conf.chroma_bands);
00121         return -1;
00122     }
00123 
00124     pic_size_indx = get_bits(&ctx->gb, 4);
00125     if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
00126         pic_conf.pic_height = get_bits(&ctx->gb, 13);
00127         pic_conf.pic_width  = get_bits(&ctx->gb, 13);
00128     } else {
00129         pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
00130         pic_conf.pic_width  = ivi5_common_pic_sizes[pic_size_indx * 2    ] << 2;
00131     }
00132 
00133     if (ctx->gop_flags & 2) {
00134         av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
00135         return -1;
00136     }
00137 
00138     pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00139     pic_conf.chroma_width  = (pic_conf.pic_width  + 3) >> 2;
00140 
00141     if (!tile_size) {
00142         pic_conf.tile_height = pic_conf.pic_height;
00143         pic_conf.tile_width  = pic_conf.pic_width;
00144     } else {
00145         pic_conf.tile_height = pic_conf.tile_width = tile_size;
00146     }
00147 
00148     /* check if picture layout was changed and reallocate buffers */
00149     if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00150         result = ff_ivi_init_planes(ctx->planes, &pic_conf);
00151         if (result) {
00152             av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00153             return -1;
00154         }
00155         ctx->pic_conf = pic_conf;
00156         blk_size_changed = 1; /* force reallocation of the internal structures */
00157     }
00158 
00159     for (p = 0; p <= 1; p++) {
00160         for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00161             band = &ctx->planes[p].bands[i];
00162 
00163             band->is_halfpel = get_bits1(&ctx->gb);
00164 
00165             mb_size  = get_bits1(&ctx->gb);
00166             blk_size = 8 >> get_bits1(&ctx->gb);
00167             mb_size  = blk_size << !mb_size;
00168 
00169             blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
00170             if (blk_size_changed) {
00171                 band->mb_size  = mb_size;
00172                 band->blk_size = blk_size;
00173             }
00174 
00175             if (get_bits1(&ctx->gb)) {
00176                 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
00177                 return -1;
00178             }
00179 
00180             /* select transform function and scan pattern according to plane and band number */
00181             switch ((p << 2) + i) {
00182             case 0:
00183                 band->inv_transform = ff_ivi_inverse_slant_8x8;
00184                 band->dc_transform  = ff_ivi_dc_slant_2d;
00185                 band->scan          = ff_zigzag_direct;
00186                 break;
00187 
00188             case 1:
00189                 band->inv_transform = ff_ivi_row_slant8;
00190                 band->dc_transform  = ff_ivi_dc_row_slant;
00191                 band->scan          = ff_ivi_vertical_scan_8x8;
00192                 break;
00193 
00194             case 2:
00195                 band->inv_transform = ff_ivi_col_slant8;
00196                 band->dc_transform  = ff_ivi_dc_col_slant;
00197                 band->scan          = ff_ivi_horizontal_scan_8x8;
00198                 break;
00199 
00200             case 3:
00201                 band->inv_transform = ff_ivi_put_pixels_8x8;
00202                 band->dc_transform  = ff_ivi_put_dc_pixel_8x8;
00203                 band->scan          = ff_ivi_horizontal_scan_8x8;
00204                 break;
00205 
00206             case 4:
00207                 band->inv_transform = ff_ivi_inverse_slant_4x4;
00208                 band->dc_transform  = ff_ivi_dc_slant_2d;
00209                 band->scan          = ff_ivi_direct_scan_4x4;
00210                 break;
00211             }
00212 
00213             band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
00214                                 band->inv_transform == ff_ivi_inverse_slant_4x4;
00215 
00216             /* select dequant matrix according to plane and band number */
00217             if (!p) {
00218                 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
00219             } else {
00220                 quant_mat = 5;
00221             }
00222 
00223             if (band->blk_size == 8) {
00224                 if(quant_mat >= 5){
00225                     av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
00226                     return -1;
00227                 }
00228                 band->intra_base  = &ivi5_base_quant_8x8_intra[quant_mat][0];
00229                 band->inter_base  = &ivi5_base_quant_8x8_inter[quant_mat][0];
00230                 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
00231                 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
00232             } else {
00233                 band->intra_base  = ivi5_base_quant_4x4_intra;
00234                 band->inter_base  = ivi5_base_quant_4x4_inter;
00235                 band->intra_scale = ivi5_scale_quant_4x4_intra;
00236                 band->inter_scale = ivi5_scale_quant_4x4_inter;
00237             }
00238 
00239             if (get_bits(&ctx->gb, 2)) {
00240                 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
00241                 return -1;
00242             }
00243         }
00244     }
00245 
00246     /* copy chroma parameters into the 2nd chroma plane */
00247     for (i = 0; i < pic_conf.chroma_bands; i++) {
00248         band1 = &ctx->planes[1].bands[i];
00249         band2 = &ctx->planes[2].bands[i];
00250 
00251         band2->width         = band1->width;
00252         band2->height        = band1->height;
00253         band2->mb_size       = band1->mb_size;
00254         band2->blk_size      = band1->blk_size;
00255         band2->is_halfpel    = band1->is_halfpel;
00256         band2->intra_base    = band1->intra_base;
00257         band2->inter_base    = band1->inter_base;
00258         band2->intra_scale   = band1->intra_scale;
00259         band2->inter_scale   = band1->inter_scale;
00260         band2->scan          = band1->scan;
00261         band2->inv_transform = band1->inv_transform;
00262         band2->dc_transform  = band1->dc_transform;
00263         band2->is_2d_trans   = band1->is_2d_trans;
00264     }
00265 
00266     /* reallocate internal structures if needed */
00267     if (blk_size_changed) {
00268         result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
00269                                    pic_conf.tile_height);
00270         if (result) {
00271             av_log(avctx, AV_LOG_ERROR,
00272                    "Couldn't reallocate internal structures!\n");
00273             return -1;
00274         }
00275     }
00276 
00277     if (ctx->gop_flags & 8) {
00278         if (get_bits(&ctx->gb, 3)) {
00279             av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
00280             return -1;
00281         }
00282 
00283         if (get_bits1(&ctx->gb))
00284             skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
00285     }
00286 
00287     align_get_bits(&ctx->gb);
00288 
00289     skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
00290 
00291     /* skip GOP extension if any */
00292     if (get_bits1(&ctx->gb)) {
00293         do {
00294             i = get_bits(&ctx->gb, 16);
00295         } while (i & 0x8000);
00296     }
00297 
00298     align_get_bits(&ctx->gb);
00299 
00300     return 0;
00301 }
00302 
00303 
00309 static inline void skip_hdr_extension(GetBitContext *gb)
00310 {
00311     int i, len;
00312 
00313     do {
00314         len = get_bits(gb, 8);
00315         for (i = 0; i < len; i++) skip_bits(gb, 8);
00316     } while(len);
00317 }
00318 
00319 
00327 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
00328 {
00329     if (get_bits(&ctx->gb, 5) != 0x1F) {
00330         av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00331         return -1;
00332     }
00333 
00334     ctx->prev_frame_type = ctx->frame_type;
00335     ctx->frame_type      = get_bits(&ctx->gb, 3);
00336     if (ctx->frame_type >= 5) {
00337         av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
00338         return -1;
00339     }
00340 
00341     ctx->frame_num = get_bits(&ctx->gb, 8);
00342 
00343     if (ctx->frame_type == FRAMETYPE_INTRA) {
00344         ctx->gop_invalid = 1;
00345         if (decode_gop_header(ctx, avctx)) {
00346             av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
00347             return AVERROR_INVALIDDATA;
00348         }
00349         ctx->gop_invalid = 0;
00350     }
00351 
00352     if (ctx->frame_type != FRAMETYPE_NULL) {
00353         ctx->frame_flags = get_bits(&ctx->gb, 8);
00354 
00355         ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
00356 
00357         ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
00358 
00359         /* skip unknown extension if any */
00360         if (ctx->frame_flags & 0x20)
00361             skip_hdr_extension(&ctx->gb); /* XXX: untested */
00362 
00363         /* decode macroblock huffman codebook */
00364         if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
00365             return -1;
00366 
00367         skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
00368     }
00369 
00370     align_get_bits(&ctx->gb);
00371 
00372     return 0;
00373 }
00374 
00375 
00384 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
00385                            AVCodecContext *avctx)
00386 {
00387     int         i;
00388     uint8_t     band_flags;
00389 
00390     band_flags = get_bits(&ctx->gb, 8);
00391 
00392     if (band_flags & 1) {
00393         band->is_empty = 1;
00394         return 0;
00395     }
00396 
00397     band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
00398 
00399     band->inherit_mv     = band_flags & 2;
00400     band->inherit_qdelta = band_flags & 8;
00401     band->qdelta_present = band_flags & 4;
00402     if (!band->qdelta_present) band->inherit_qdelta = 1;
00403 
00404     /* decode rvmap probability corrections if any */
00405     band->num_corr = 0; /* there are no corrections */
00406     if (band_flags & 0x10) {
00407         band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
00408         if (band->num_corr > 61) {
00409             av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00410                    band->num_corr);
00411             return -1;
00412         }
00413 
00414         /* read correction pairs */
00415         for (i = 0; i < band->num_corr * 2; i++)
00416             band->corr[i] = get_bits(&ctx->gb, 8);
00417     }
00418 
00419     /* select appropriate rvmap table for this band */
00420     band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
00421 
00422     /* decode block huffman codebook */
00423     if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
00424         return -1;
00425 
00426     band->checksum_present = get_bits1(&ctx->gb);
00427     if (band->checksum_present)
00428         band->checksum = get_bits(&ctx->gb, 16);
00429 
00430     band->glob_quant = get_bits(&ctx->gb, 5);
00431 
00432     /* skip unknown extension if any */
00433     if (band_flags & 0x20) { /* XXX: untested */
00434         align_get_bits(&ctx->gb);
00435         skip_hdr_extension(&ctx->gb);
00436     }
00437 
00438     align_get_bits(&ctx->gb);
00439 
00440     return 0;
00441 }
00442 
00443 
00454 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
00455                           IVITile *tile, AVCodecContext *avctx)
00456 {
00457     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
00458                 mv_scale, blks_per_mb;
00459     IVIMbInfo   *mb, *ref_mb;
00460     int         row_offset = band->mb_size * band->pitch;
00461 
00462     mb     = tile->mbs;
00463     ref_mb = tile->ref_mbs;
00464     offs   = tile->ypos * band->pitch + tile->xpos;
00465 
00466     if (!ref_mb &&
00467         ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
00468         return AVERROR_INVALIDDATA;
00469 
00470     if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
00471         av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
00472                tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
00473         return AVERROR_INVALIDDATA;
00474     }
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     for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
00481         mb_offset = offs;
00482 
00483         for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
00484             mb->xpos     = x;
00485             mb->ypos     = y;
00486             mb->buf_offs = mb_offset;
00487 
00488             if (get_bits1(&ctx->gb)) {
00489                 if (ctx->frame_type == FRAMETYPE_INTRA) {
00490                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00491                     return -1;
00492                 }
00493                 mb->type = 1; /* empty macroblocks are always INTER */
00494                 mb->cbp  = 0; /* all blocks are empty */
00495 
00496                 mb->q_delta = 0;
00497                 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
00498                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00499                                            IVI_VLC_BITS, 1);
00500                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00501                 }
00502 
00503                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
00504                 if (band->inherit_mv){
00505                     /* motion vector inheritance */
00506                     if (mv_scale) {
00507                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00508                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00509                     } else {
00510                         mb->mv_x = ref_mb->mv_x;
00511                         mb->mv_y = ref_mb->mv_y;
00512                     }
00513                 }
00514             } else {
00515                 if (band->inherit_mv) {
00516                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
00517                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00518                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
00519                 } else {
00520                     mb->type = get_bits1(&ctx->gb);
00521                 }
00522 
00523                 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00524                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00525 
00526                 mb->q_delta = 0;
00527                 if (band->qdelta_present) {
00528                     if (band->inherit_qdelta) {
00529                         if (ref_mb) mb->q_delta = ref_mb->q_delta;
00530                     } else if (mb->cbp || (!band->plane && !band->band_num &&
00531                                            (ctx->frame_flags & 8))) {
00532                         mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00533                                                IVI_VLC_BITS, 1);
00534                         mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00535                     }
00536                 }
00537 
00538                 if (!mb->type) {
00539                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
00540                 } else {
00541                     if (band->inherit_mv){
00542                         /* motion vector inheritance */
00543                         if (mv_scale) {
00544                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00545                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00546                         } else {
00547                             mb->mv_x = ref_mb->mv_x;
00548                             mb->mv_y = ref_mb->mv_y;
00549                         }
00550                     } else {
00551                         /* decode motion vector deltas */
00552                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00553                                             IVI_VLC_BITS, 1);
00554                         mv_y += IVI_TOSIGNED(mv_delta);
00555                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00556                                             IVI_VLC_BITS, 1);
00557                         mv_x += IVI_TOSIGNED(mv_delta);
00558                         mb->mv_x = mv_x;
00559                         mb->mv_y = mv_y;
00560                     }
00561                 }
00562             }
00563 
00564             mb++;
00565             if (ref_mb)
00566                 ref_mb++;
00567             mb_offset += band->mb_size;
00568         }
00569 
00570         offs += row_offset;
00571     }
00572 
00573     align_get_bits(&ctx->gb);
00574 
00575     return 0;
00576 }
00577 
00578 
00587 static int decode_band(IVI5DecContext *ctx, int plane_num,
00588                        IVIBandDesc *band, AVCodecContext *avctx)
00589 {
00590     int         result, i, t, idx1, idx2, pos;
00591     IVITile     *tile;
00592 
00593     band->buf     = band->bufs[ctx->dst_buf];
00594     band->ref_buf = band->bufs[ctx->ref_buf];
00595     band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
00596 
00597     result = decode_band_hdr(ctx, band, avctx);
00598     if (result) {
00599         av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
00600                result);
00601         return -1;
00602     }
00603 
00604     if (band->is_empty) {
00605         av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00606         return -1;
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             result = ff_ivi_process_empty_tile(avctx, band, tile,
00627                                       (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00628             if (result < 0)
00629                 break;
00630         } else {
00631             tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00632 
00633             result = decode_mb_info(ctx, band, tile, avctx);
00634             if (result < 0)
00635                 break;
00636 
00637             result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00638             if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
00639                 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00640                 break;
00641             }
00642             pos += tile->data_size << 3; // skip to next tile
00643         }
00644     }
00645 
00646     /* restore the selected rvmap table by applying its corrections in reverse order */
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 #ifdef DEBUG
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 result;
00668 }
00669 
00670 
00676 static void switch_buffers(IVI5DecContext *ctx)
00677 {
00678     switch (ctx->prev_frame_type) {
00679     case FRAMETYPE_INTRA:
00680     case FRAMETYPE_INTER:
00681         ctx->buf_switch ^= 1;
00682         ctx->dst_buf = ctx->buf_switch;
00683         ctx->ref_buf = ctx->buf_switch ^ 1;
00684         break;
00685     case FRAMETYPE_INTER_SCAL:
00686         if (!ctx->inter_scal) {
00687             ctx->ref2_buf   = 2;
00688             ctx->inter_scal = 1;
00689         }
00690         FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
00691         ctx->ref_buf = ctx->ref2_buf;
00692         break;
00693     case FRAMETYPE_INTER_NOREF:
00694         break;
00695     }
00696 
00697     switch (ctx->frame_type) {
00698     case FRAMETYPE_INTRA:
00699         ctx->buf_switch = 0;
00700         /* FALLTHROUGH */
00701     case FRAMETYPE_INTER:
00702         ctx->inter_scal = 0;
00703         ctx->dst_buf = ctx->buf_switch;
00704         ctx->ref_buf = ctx->buf_switch ^ 1;
00705         break;
00706     case FRAMETYPE_INTER_SCAL:
00707     case FRAMETYPE_INTER_NOREF:
00708     case FRAMETYPE_NULL:
00709         break;
00710     }
00711 }
00712 
00713 
00717 static av_cold int decode_init(AVCodecContext *avctx)
00718 {
00719     IVI5DecContext  *ctx = avctx->priv_data;
00720     int             result;
00721 
00722     ff_ivi_init_static_vlc();
00723 
00724     /* copy rvmap tables in our context so we can apply changes to them */
00725     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00726 
00727     /* set the initial picture layout according to the basic profile:
00728        there is only one band per plane (no scalability), only one tile (no local decoding)
00729        and picture format = YVU9 */
00730     ctx->pic_conf.pic_width     = avctx->width;
00731     ctx->pic_conf.pic_height    = avctx->height;
00732     ctx->pic_conf.chroma_width  = (avctx->width  + 3) >> 2;
00733     ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
00734     ctx->pic_conf.tile_width    = avctx->width;
00735     ctx->pic_conf.tile_height   = avctx->height;
00736     ctx->pic_conf.luma_bands    = ctx->pic_conf.chroma_bands = 1;
00737 
00738     avcodec_get_frame_defaults(&ctx->frame);
00739 
00740     result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
00741     if (result) {
00742         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
00743         return -1;
00744     }
00745 
00746     ctx->buf_switch = 0;
00747     ctx->inter_scal = 0;
00748 
00749     avctx->pix_fmt = PIX_FMT_YUV410P;
00750 
00751     return 0;
00752 }
00753 
00754 
00758 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00759                         AVPacket *avpkt)
00760 {
00761     IVI5DecContext  *ctx = avctx->priv_data;
00762     const uint8_t   *buf = avpkt->data;
00763     int             buf_size = avpkt->size;
00764     int             result, p, b;
00765 
00766     init_get_bits(&ctx->gb, buf, buf_size * 8);
00767     ctx->frame_data = buf;
00768     ctx->frame_size = buf_size;
00769 
00770     result = decode_pic_hdr(ctx, avctx);
00771     if (result) {
00772         av_log(avctx, AV_LOG_ERROR,
00773                "Error while decoding picture header: %d\n", result);
00774         return -1;
00775     }
00776     if (ctx->gop_invalid)
00777         return AVERROR_INVALIDDATA;
00778 
00779     if (ctx->gop_flags & IVI5_IS_PROTECTED) {
00780         av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
00781         return -1;
00782     }
00783 
00784     switch_buffers(ctx);
00785 
00786     //START_TIMER;
00787 
00788     if (ctx->frame_type != FRAMETYPE_NULL) {
00789         for (p = 0; p < 3; p++) {
00790             for (b = 0; b < ctx->planes[p].num_bands; b++) {
00791                 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00792                 if (result) {
00793                     av_log(avctx, AV_LOG_ERROR,
00794                            "Error while decoding band: %d, plane: %d\n", b, p);
00795                     return -1;
00796                 }
00797             }
00798         }
00799     }
00800 
00801     //STOP_TIMER("decode_planes");
00802 
00803     if (ctx->frame.data[0])
00804         avctx->release_buffer(avctx, &ctx->frame);
00805 
00806     ctx->frame.reference = 0;
00807     if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
00808         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00809         return -1;
00810     }
00811 
00812     if (ctx->is_scalable) {
00813         ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00814     } else {
00815         ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00816     }
00817 
00818     ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00819     ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00820 
00821     *data_size = sizeof(AVFrame);
00822     *(AVFrame*)data = ctx->frame;
00823 
00824     return buf_size;
00825 }
00826 
00827 
00831 static av_cold int decode_close(AVCodecContext *avctx)
00832 {
00833     IVI5DecContext *ctx = avctx->priv_data;
00834 
00835     ff_ivi_free_buffers(&ctx->planes[0]);
00836 
00837     if (ctx->mb_vlc.cust_tab.table)
00838         free_vlc(&ctx->mb_vlc.cust_tab);
00839 
00840     if (ctx->frame.data[0])
00841         avctx->release_buffer(avctx, &ctx->frame);
00842 
00843     return 0;
00844 }
00845 
00846 
00847 AVCodec ff_indeo5_decoder = {
00848     .name           = "indeo5",
00849     .type           = AVMEDIA_TYPE_VIDEO,
00850     .id             = CODEC_ID_INDEO5,
00851     .priv_data_size = sizeof(IVI5DecContext),
00852     .init           = decode_init,
00853     .close          = decode_close,
00854     .decode         = decode_frame,
00855     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
00856 };

Generated on Fri Feb 22 2013 07:24:27 for FFmpeg by  doxygen 1.7.1