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