• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 } IVI5DecContext;
00080 
00081 
00091 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
00092 {
00093     int             result, i, p, tile_size, pic_size_indx, mb_size, blk_size, blk_size_changed = 0;
00094     IVIBandDesc     *band, *band1, *band2;
00095     IVIPicConfig    pic_conf;
00096 
00097     ctx->gop_flags = get_bits(&ctx->gb, 8);
00098 
00099     ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
00100 
00101     if (ctx->gop_flags & IVI5_IS_PROTECTED)
00102         ctx->lock_word = get_bits_long(&ctx->gb, 32);
00103 
00104     tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
00105     if (tile_size > 256) {
00106         av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
00107         return -1;
00108     }
00109 
00110     /* decode number of wavelet bands */
00111     /* num_levels * 3 + 1 */
00112     pic_conf.luma_bands   = get_bits(&ctx->gb, 2) * 3 + 1;
00113     pic_conf.chroma_bands = get_bits1(&ctx->gb)   * 3 + 1;
00114     ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
00115     if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
00116         av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
00117                pic_conf.luma_bands, pic_conf.chroma_bands);
00118         return -1;
00119     }
00120 
00121     pic_size_indx = get_bits(&ctx->gb, 4);
00122     if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
00123         pic_conf.pic_height = get_bits(&ctx->gb, 13);
00124         pic_conf.pic_width  = get_bits(&ctx->gb, 13);
00125     } else {
00126         pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
00127         pic_conf.pic_width  = ivi5_common_pic_sizes[pic_size_indx * 2    ] << 2;
00128     }
00129 
00130     if (ctx->gop_flags & 2) {
00131         av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
00132         return -1;
00133     }
00134 
00135     pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
00136     pic_conf.chroma_width  = (pic_conf.pic_width  + 3) >> 2;
00137 
00138     if (!tile_size) {
00139         pic_conf.tile_height = pic_conf.pic_height;
00140         pic_conf.tile_width  = pic_conf.pic_width;
00141     } else {
00142         pic_conf.tile_height = pic_conf.tile_width = tile_size;
00143     }
00144 
00145     /* check if picture layout was changed and reallocate buffers */
00146     if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
00147         result = ff_ivi_init_planes(ctx->planes, &pic_conf);
00148         if (result) {
00149             av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
00150             return -1;
00151         }
00152         ctx->pic_conf = pic_conf;
00153         blk_size_changed = 1; /* force reallocation of the internal structures */
00154     }
00155 
00156     for (p = 0; p <= 1; p++) {
00157         for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
00158             band = &ctx->planes[p].bands[i];
00159 
00160             band->is_halfpel = get_bits1(&ctx->gb);
00161 
00162             mb_size  = get_bits1(&ctx->gb);
00163             blk_size = 8 >> get_bits1(&ctx->gb);
00164             mb_size  = blk_size << !mb_size;
00165 
00166             blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
00167             if (blk_size_changed) {
00168                 band->mb_size  = mb_size;
00169                 band->blk_size = blk_size;
00170             }
00171 
00172             if (get_bits1(&ctx->gb)) {
00173                 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
00174                 return -1;
00175             }
00176 
00177             /* select transform function and scan pattern according to plane and band number */
00178             switch ((p << 2) + i) {
00179             case 0:
00180                 band->inv_transform = ff_ivi_inverse_slant_8x8;
00181                 band->dc_transform  = ff_ivi_dc_slant_2d;
00182                 band->scan          = ff_zigzag_direct;
00183                 break;
00184 
00185             case 1:
00186                 band->inv_transform = ff_ivi_row_slant8;
00187                 band->dc_transform  = ff_ivi_dc_row_slant;
00188                 band->scan          = ivi5_scans8x8[0];
00189                 break;
00190 
00191             case 2:
00192                 band->inv_transform = ff_ivi_col_slant8;
00193                 band->dc_transform  = ff_ivi_dc_col_slant;
00194                 band->scan          = ivi5_scans8x8[1];
00195                 break;
00196 
00197             case 3:
00198                 band->inv_transform = ff_ivi_put_pixels_8x8;
00199                 band->dc_transform  = ff_ivi_put_dc_pixel_8x8;
00200                 band->scan          = ivi5_scans8x8[1];
00201                 break;
00202 
00203             case 4:
00204                 band->inv_transform = ff_ivi_inverse_slant_4x4;
00205                 band->dc_transform  = ff_ivi_dc_slant_2d;
00206                 band->scan          = ivi5_scan4x4;
00207                 break;
00208             }
00209 
00210             band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
00211                                 band->inv_transform == ff_ivi_inverse_slant_4x4;
00212 
00213             /* select dequant matrix according to plane and band number */
00214             if (!p) {
00215                 band->quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
00216             } else {
00217                 band->quant_mat = 5;
00218             }
00219 
00220             if (get_bits(&ctx->gb, 2)) {
00221                 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
00222                 return -1;
00223             }
00224         }
00225     }
00226 
00227     /* copy chroma parameters into the 2nd chroma plane */
00228     for (i = 0; i < pic_conf.chroma_bands; i++) {
00229         band1 = &ctx->planes[1].bands[i];
00230         band2 = &ctx->planes[2].bands[i];
00231 
00232         band2->width         = band1->width;
00233         band2->height        = band1->height;
00234         band2->mb_size       = band1->mb_size;
00235         band2->blk_size      = band1->blk_size;
00236         band2->is_halfpel    = band1->is_halfpel;
00237         band2->quant_mat     = band1->quant_mat;
00238         band2->scan          = band1->scan;
00239         band2->inv_transform = band1->inv_transform;
00240         band2->dc_transform  = band1->dc_transform;
00241         band2->is_2d_trans   = band1->is_2d_trans;
00242     }
00243 
00244     /* reallocate internal structures if needed */
00245     if (blk_size_changed) {
00246         result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
00247                                    pic_conf.tile_height);
00248         if (result) {
00249             av_log(avctx, AV_LOG_ERROR,
00250                    "Couldn't reallocate internal structures!\n");
00251             return -1;
00252         }
00253     }
00254 
00255     if (ctx->gop_flags & 8) {
00256         if (get_bits(&ctx->gb, 3)) {
00257             av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
00258             return -1;
00259         }
00260 
00261         if (get_bits1(&ctx->gb))
00262             skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
00263     }
00264 
00265     align_get_bits(&ctx->gb);
00266 
00267     skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
00268 
00269     /* skip GOP extension if any */
00270     if (get_bits1(&ctx->gb)) {
00271         do {
00272             i = get_bits(&ctx->gb, 16);
00273         } while (i & 0x8000);
00274     }
00275 
00276     align_get_bits(&ctx->gb);
00277 
00278     return 0;
00279 }
00280 
00281 
00287 static inline void skip_hdr_extension(GetBitContext *gb)
00288 {
00289     int i, len;
00290 
00291     do {
00292         len = get_bits(gb, 8);
00293         for (i = 0; i < len; i++) skip_bits(gb, 8);
00294     } while(len);
00295 }
00296 
00297 
00305 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
00306 {
00307     if (get_bits(&ctx->gb, 5) != 0x1F) {
00308         av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
00309         return -1;
00310     }
00311 
00312     ctx->prev_frame_type = ctx->frame_type;
00313     ctx->frame_type      = get_bits(&ctx->gb, 3);
00314     if (ctx->frame_type >= 5) {
00315         av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
00316         return -1;
00317     }
00318 
00319     ctx->frame_num = get_bits(&ctx->gb, 8);
00320 
00321     if (ctx->frame_type == FRAMETYPE_INTRA) {
00322         if (decode_gop_header(ctx, avctx))
00323             return -1;
00324     }
00325 
00326     if (ctx->frame_type != FRAMETYPE_NULL) {
00327         ctx->frame_flags = get_bits(&ctx->gb, 8);
00328 
00329         ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
00330 
00331         ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
00332 
00333         /* skip unknown extension if any */
00334         if (ctx->frame_flags & 0x20)
00335             skip_hdr_extension(&ctx->gb); /* XXX: untested */
00336 
00337         /* decode macroblock huffman codebook */
00338         if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
00339             return -1;
00340 
00341         skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
00342     }
00343 
00344     align_get_bits(&ctx->gb);
00345 
00346     return 0;
00347 }
00348 
00349 
00358 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
00359                            AVCodecContext *avctx)
00360 {
00361     int         i;
00362     uint8_t     band_flags;
00363 
00364     band_flags = get_bits(&ctx->gb, 8);
00365 
00366     if (band_flags & 1) {
00367         band->is_empty = 1;
00368         return 0;
00369     }
00370 
00371     band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
00372 
00373     band->inherit_mv     = band_flags & 2;
00374     band->inherit_qdelta = band_flags & 8;
00375     band->qdelta_present = band_flags & 4;
00376     if (!band->qdelta_present) band->inherit_qdelta = 1;
00377 
00378     /* decode rvmap probability corrections if any */
00379     band->num_corr = 0; /* there are no corrections */
00380     if (band_flags & 0x10) {
00381         band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
00382         if (band->num_corr > 61) {
00383             av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
00384                    band->num_corr);
00385             return -1;
00386         }
00387 
00388         /* read correction pairs */
00389         for (i = 0; i < band->num_corr * 2; i++)
00390             band->corr[i] = get_bits(&ctx->gb, 8);
00391     }
00392 
00393     /* select appropriate rvmap table for this band */
00394     band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
00395 
00396     /* decode block huffman codebook */
00397     if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
00398         return -1;
00399 
00400     band->checksum_present = get_bits1(&ctx->gb);
00401     if (band->checksum_present)
00402         band->checksum = get_bits(&ctx->gb, 16);
00403 
00404     band->glob_quant = get_bits(&ctx->gb, 5);
00405 
00406     /* skip unknown extension if any */
00407     if (band_flags & 0x20) { /* XXX: untested */
00408         align_get_bits(&ctx->gb);
00409         skip_hdr_extension(&ctx->gb);
00410     }
00411 
00412     align_get_bits(&ctx->gb);
00413 
00414     return 0;
00415 }
00416 
00417 
00428 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
00429                           IVITile *tile, AVCodecContext *avctx)
00430 {
00431     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
00432                 mv_scale, blks_per_mb;
00433     IVIMbInfo   *mb, *ref_mb;
00434     int         row_offset = band->mb_size * band->pitch;
00435 
00436     mb     = tile->mbs;
00437     ref_mb = tile->ref_mbs;
00438     offs   = tile->ypos * band->pitch + tile->xpos;
00439 
00440     /* scale factor for motion vectors */
00441     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
00442     mv_x = mv_y = 0;
00443 
00444     for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
00445         mb_offset = offs;
00446 
00447         for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
00448             mb->xpos     = x;
00449             mb->ypos     = y;
00450             mb->buf_offs = mb_offset;
00451 
00452             if (get_bits1(&ctx->gb)) {
00453                 if (ctx->frame_type == FRAMETYPE_INTRA) {
00454                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
00455                     return -1;
00456                 }
00457                 mb->type = 1; /* empty macroblocks are always INTER */
00458                 mb->cbp  = 0; /* all blocks are empty */
00459 
00460                 mb->q_delta = 0;
00461                 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
00462                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00463                                            IVI_VLC_BITS, 1);
00464                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00465                 }
00466 
00467                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
00468                 if (band->inherit_mv){
00469                     /* motion vector inheritance */
00470                     if (mv_scale) {
00471                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00472                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00473                     } else {
00474                         mb->mv_x = ref_mb->mv_x;
00475                         mb->mv_y = ref_mb->mv_y;
00476                     }
00477                 }
00478             } else {
00479                 if (band->inherit_mv) {
00480                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
00481                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
00482                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
00483                 } else {
00484                     mb->type = get_bits1(&ctx->gb);
00485                 }
00486 
00487                 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
00488                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
00489 
00490                 mb->q_delta = 0;
00491                 if (band->qdelta_present) {
00492                     if (band->inherit_qdelta) {
00493                         if (ref_mb) mb->q_delta = ref_mb->q_delta;
00494                     } else if (mb->cbp || (!band->plane && !band->band_num &&
00495                                            (ctx->frame_flags & 8))) {
00496                         mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00497                                                IVI_VLC_BITS, 1);
00498                         mb->q_delta = IVI_TOSIGNED(mb->q_delta);
00499                     }
00500                 }
00501 
00502                 if (!mb->type) {
00503                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
00504                 } else {
00505                     if (band->inherit_mv){
00506                         /* motion vector inheritance */
00507                         if (mv_scale) {
00508                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
00509                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
00510                         } else {
00511                             mb->mv_x = ref_mb->mv_x;
00512                             mb->mv_y = ref_mb->mv_y;
00513                         }
00514                     } else {
00515                         /* decode motion vector deltas */
00516                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00517                                             IVI_VLC_BITS, 1);
00518                         mv_y += IVI_TOSIGNED(mv_delta);
00519                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
00520                                             IVI_VLC_BITS, 1);
00521                         mv_x += IVI_TOSIGNED(mv_delta);
00522                         mb->mv_x = mv_x;
00523                         mb->mv_y = mv_y;
00524                     }
00525                 }
00526             }
00527 
00528             mb++;
00529             if (ref_mb)
00530                 ref_mb++;
00531             mb_offset += band->mb_size;
00532         }
00533 
00534         offs += row_offset;
00535     }
00536 
00537     align_get_bits(&ctx->gb);
00538 
00539     return 0;
00540 }
00541 
00542 
00551 static int decode_band(IVI5DecContext *ctx, int plane_num,
00552                        IVIBandDesc *band, AVCodecContext *avctx)
00553 {
00554     int         result, i, t, idx1, idx2, pos;
00555     IVITile     *tile;
00556 
00557     band->buf     = band->bufs[ctx->dst_buf];
00558     band->ref_buf = band->bufs[ctx->ref_buf];
00559     band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
00560 
00561     result = decode_band_hdr(ctx, band, avctx);
00562     if (result) {
00563         av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
00564                result);
00565         return -1;
00566     }
00567 
00568     if (band->is_empty) {
00569         av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
00570         return -1;
00571     }
00572 
00573     if (band->blk_size == 8) {
00574         band->intra_base  = &ivi5_base_quant_8x8_intra[band->quant_mat][0];
00575         band->inter_base  = &ivi5_base_quant_8x8_inter[band->quant_mat][0];
00576         band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0];
00577         band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0];
00578     } else {
00579         band->intra_base  = ivi5_base_quant_4x4_intra;
00580         band->inter_base  = ivi5_base_quant_4x4_inter;
00581         band->intra_scale = ivi5_scale_quant_4x4_intra;
00582         band->inter_scale = ivi5_scale_quant_4x4_inter;
00583     }
00584 
00585     band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
00586 
00587     /* apply corrections to the selected rvmap table if present */
00588     for (i = 0; i < band->num_corr; i++) {
00589         idx1 = band->corr[i*2];
00590         idx2 = band->corr[i*2+1];
00591         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00592         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00593     }
00594 
00595     pos = get_bits_count(&ctx->gb);
00596 
00597     for (t = 0; t < band->num_tiles; t++) {
00598         tile = &band->tiles[t];
00599 
00600         tile->is_empty = get_bits1(&ctx->gb);
00601         if (tile->is_empty) {
00602             ff_ivi_process_empty_tile(avctx, band, tile,
00603                                       (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
00604         } else {
00605             tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
00606 
00607             result = decode_mb_info(ctx, band, tile, avctx);
00608             if (result < 0)
00609                 break;
00610 
00611             result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
00612             if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
00613                 av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
00614                 break;
00615             }
00616             pos += tile->data_size << 3; // skip to next tile
00617         }
00618     }
00619 
00620     /* restore the selected rvmap table by applying its corrections in reverse order */
00621     for (i = band->num_corr-1; i >= 0; i--) {
00622         idx1 = band->corr[i*2];
00623         idx2 = band->corr[i*2+1];
00624         FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
00625         FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
00626     }
00627 
00628 #if IVI_DEBUG
00629     if (band->checksum_present) {
00630         uint16_t chksum = ivi_calc_band_checksum(band);
00631         if (chksum != band->checksum) {
00632             av_log(avctx, AV_LOG_ERROR,
00633                    "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
00634                    band->plane, band->band_num, band->checksum, chksum);
00635         }
00636     }
00637 #endif
00638 
00639     align_get_bits(&ctx->gb);
00640 
00641     return result;
00642 }
00643 
00644 
00651 static void switch_buffers(IVI5DecContext *ctx, AVCodecContext *avctx)
00652 {
00653     switch (ctx->prev_frame_type) {
00654     case FRAMETYPE_INTRA:
00655     case FRAMETYPE_INTER:
00656         ctx->buf_switch ^= 1;
00657         ctx->dst_buf = ctx->buf_switch;
00658         ctx->ref_buf = ctx->buf_switch ^ 1;
00659         break;
00660     case FRAMETYPE_INTER_SCAL:
00661         if (!ctx->inter_scal) {
00662             ctx->ref2_buf   = 2;
00663             ctx->inter_scal = 1;
00664         }
00665         FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
00666         ctx->ref_buf = ctx->ref2_buf;
00667         break;
00668     case FRAMETYPE_INTER_NOREF:
00669         break;
00670     }
00671 
00672     switch (ctx->frame_type) {
00673     case FRAMETYPE_INTRA:
00674         ctx->buf_switch = 0;
00675         /* FALLTHROUGH */
00676     case FRAMETYPE_INTER:
00677         ctx->inter_scal = 0;
00678         ctx->dst_buf = ctx->buf_switch;
00679         ctx->ref_buf = ctx->buf_switch ^ 1;
00680         break;
00681     case FRAMETYPE_INTER_SCAL:
00682     case FRAMETYPE_INTER_NOREF:
00683     case FRAMETYPE_NULL:
00684         break;
00685     }
00686 }
00687 
00688 
00692 static av_cold int decode_init(AVCodecContext *avctx)
00693 {
00694     IVI5DecContext  *ctx = avctx->priv_data;
00695     int             result;
00696 
00697     ff_ivi_init_static_vlc();
00698 
00699     /* copy rvmap tables in our context so we can apply changes to them */
00700     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
00701 
00702     /* set the initial picture layout according to the basic profile:
00703        there is only one band per plane (no scalability), only one tile (no local decoding)
00704        and picture format = YVU9 */
00705     ctx->pic_conf.pic_width     = avctx->width;
00706     ctx->pic_conf.pic_height    = avctx->height;
00707     ctx->pic_conf.chroma_width  = (avctx->width  + 3) >> 2;
00708     ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
00709     ctx->pic_conf.tile_width    = avctx->width;
00710     ctx->pic_conf.tile_height   = avctx->height;
00711     ctx->pic_conf.luma_bands    = ctx->pic_conf.chroma_bands = 1;
00712 
00713     result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
00714     if (result) {
00715         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
00716         return -1;
00717     }
00718 
00719     ctx->buf_switch = 0;
00720     ctx->inter_scal = 0;
00721 
00722     avctx->pix_fmt = PIX_FMT_YUV410P;
00723 
00724     return 0;
00725 }
00726 
00727 
00731 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00732                         AVPacket *avpkt)
00733 {
00734     IVI5DecContext  *ctx = avctx->priv_data;
00735     const uint8_t   *buf = avpkt->data;
00736     int             buf_size = avpkt->size;
00737     int             result, p, b;
00738 
00739     init_get_bits(&ctx->gb, buf, buf_size * 8);
00740     ctx->frame_data = buf;
00741     ctx->frame_size = buf_size;
00742 
00743     result = decode_pic_hdr(ctx, avctx);
00744     if (result) {
00745         av_log(avctx, AV_LOG_ERROR,
00746                "Error while decoding picture header: %d\n", result);
00747         return -1;
00748     }
00749 
00750     if (ctx->gop_flags & IVI5_IS_PROTECTED) {
00751         av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
00752         return -1;
00753     }
00754 
00755     switch_buffers(ctx, avctx);
00756 
00757     //START_TIMER;
00758 
00759     if (ctx->frame_type != FRAMETYPE_NULL) {
00760         for (p = 0; p < 3; p++) {
00761             for (b = 0; b < ctx->planes[p].num_bands; b++) {
00762                 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
00763                 if (result) {
00764                     av_log(avctx, AV_LOG_ERROR,
00765                            "Error while decoding band: %d, plane: %d\n", b, p);
00766                     return -1;
00767                 }
00768             }
00769         }
00770     }
00771 
00772     //STOP_TIMER("decode_planes");
00773 
00774     if (ctx->frame.data[0])
00775         avctx->release_buffer(avctx, &ctx->frame);
00776 
00777     ctx->frame.reference = 0;
00778     if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
00779         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00780         return -1;
00781     }
00782 
00783     if (ctx->is_scalable) {
00784         ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
00785     } else {
00786         ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
00787     }
00788 
00789     ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
00790     ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
00791 
00792     *data_size = sizeof(AVFrame);
00793     *(AVFrame*)data = ctx->frame;
00794 
00795     return buf_size;
00796 }
00797 
00798 
00802 static av_cold int decode_close(AVCodecContext *avctx)
00803 {
00804     IVI5DecContext *ctx = avctx->priv_data;
00805 
00806     ff_ivi_free_buffers(&ctx->planes[0]);
00807 
00808     if (ctx->mb_vlc.cust_tab.table)
00809         free_vlc(&ctx->mb_vlc.cust_tab);
00810 
00811     if (ctx->frame.data[0])
00812         avctx->release_buffer(avctx, &ctx->frame);
00813 
00814     return 0;
00815 }
00816 
00817 
00818 AVCodec indeo5_decoder = {
00819     .name           = "indeo5",
00820     .type           = AVMEDIA_TYPE_VIDEO,
00821     .id             = CODEC_ID_INDEO5,
00822     .priv_data_size = sizeof(IVI5DecContext),
00823     .init           = decode_init,
00824     .close          = decode_close,
00825     .decode         = decode_frame,
00826     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
00827 };

Generated on Fri Sep 16 2011 17:17:38 for FFmpeg by  doxygen 1.7.1