libavcodec/ivi_common.h
Go to the documentation of this file.
00001 /*
00002  * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
00003  *
00004  * Copyright (c) 2009 Maxim Poliakovski
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00029 #ifndef AVCODEC_IVI_COMMON_H
00030 #define AVCODEC_IVI_COMMON_H
00031 
00032 #include "avcodec.h"
00033 #include "get_bits.h"
00034 #include <stdint.h>
00035 
00036 #define IVI_VLC_BITS 13 ///< max number of bits of the ivi's huffman codes
00037 #define IVI4_STREAM_ANALYSER    0
00038 #define IVI5_IS_PROTECTED       0x20
00039 
00043 typedef struct {
00044     int32_t     num_rows;
00045     uint8_t     xbits[16];
00046 } IVIHuffDesc;
00047 
00051 typedef struct {
00052     int32_t     tab_sel;    
00053 
00054     VLC         *tab;       
00055 
00057     IVIHuffDesc cust_desc;  
00058     VLC         cust_tab;   
00059 } IVIHuffTab;
00060 
00061 enum {
00062     IVI_MB_HUFF   = 0,      
00063     IVI_BLK_HUFF  = 1       
00064 };
00065 
00066 extern VLC ff_ivi_mb_vlc_tabs [8]; 
00067 extern VLC ff_ivi_blk_vlc_tabs[8]; 
00068 
00069 
00073 extern const uint8_t ff_ivi_vertical_scan_8x8[64];
00074 extern const uint8_t ff_ivi_horizontal_scan_8x8[64];
00075 extern const uint8_t ff_ivi_direct_scan_4x4[16];
00076 
00077 
00081 typedef void (InvTransformPtr)(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags);
00082 typedef void (DCTransformPtr) (const int32_t *in, int16_t *out, uint32_t pitch, int blk_size);
00083 
00084 
00088 typedef struct {
00089     uint8_t     eob_sym; 
00090     uint8_t     esc_sym; 
00091     uint8_t     runtab[256];
00092     int8_t      valtab[256];
00093 } RVMapDesc;
00094 
00095 extern const RVMapDesc ff_ivi_rvmap_tabs[9];
00096 
00097 
00101 typedef struct {
00102     int16_t     xpos;
00103     int16_t     ypos;
00104     uint32_t    buf_offs; 
00105     uint8_t     type;     
00106     uint8_t     cbp;      
00107     int8_t      q_delta;  
00108     int8_t      mv_x;     
00109     int8_t      mv_y;     
00110 } IVIMbInfo;
00111 
00112 
00116 typedef struct {
00117     int         xpos;
00118     int         ypos;
00119     int         width;
00120     int         height;
00121     int         mb_size;
00122     int         is_empty;  
00123     int         data_size; 
00124     int         num_MBs;   
00125     IVIMbInfo   *mbs;      
00126     IVIMbInfo   *ref_mbs;  
00127 } IVITile;
00128 
00129 
00133 typedef struct {
00134     int             plane;          
00135     int             band_num;       
00136     int             width;
00137     int             height;
00138     int             aheight;        
00139     const uint8_t   *data_ptr;      
00140     int             data_size;      
00141     int16_t         *buf;           
00142     int16_t         *ref_buf;       
00143     int16_t         *bufs[3];       
00144     int             pitch;          
00145     int             is_empty;       
00146     int             mb_size;        
00147     int             blk_size;       
00148     int             is_halfpel;     
00149     int             inherit_mv;     
00150     int             inherit_qdelta; 
00151     int             qdelta_present; 
00152     int             quant_mat;      
00153     int             glob_quant;     
00154     const uint8_t   *scan;          
00155 
00156     IVIHuffTab      blk_vlc;        
00157 
00158     int             num_corr;       
00159     uint8_t         corr[61*2];     
00160     int             rvmap_sel;      
00161     RVMapDesc       *rv_map;        
00162     int             num_tiles;      
00163     IVITile         *tiles;         
00164     InvTransformPtr *inv_transform;
00165     int             transform_size;
00166     DCTransformPtr  *dc_transform;
00167     int             is_2d_trans;    
00168     int32_t         checksum;       
00169     int             checksum_present;
00170     int             bufsize;        
00171     const uint16_t  *intra_base;    
00172     const uint16_t  *inter_base;    
00173     const uint8_t   *intra_scale;   
00174     const uint8_t   *inter_scale;   
00175 } IVIBandDesc;
00176 
00177 
00181 typedef struct {
00182     uint16_t    width;
00183     uint16_t    height;
00184     uint8_t     num_bands;  
00185     IVIBandDesc *bands;     
00186 } IVIPlaneDesc;
00187 
00188 
00189 typedef struct {
00190     uint16_t    pic_width;
00191     uint16_t    pic_height;
00192     uint16_t    chroma_width;
00193     uint16_t    chroma_height;
00194     uint16_t    tile_width;
00195     uint16_t    tile_height;
00196     uint8_t     luma_bands;
00197     uint8_t     chroma_bands;
00198 } IVIPicConfig;
00199 
00200 typedef struct IVI45DecContext {
00201     GetBitContext   gb;
00202     AVFrame         frame;
00203     RVMapDesc       rvmap_tabs[9];   
00204 
00205     uint32_t        frame_num;
00206     int             frame_type;
00207     int             prev_frame_type; 
00208     uint32_t        data_size;       
00209     int             is_scalable;
00210     int             transp_status;   
00211     const uint8_t   *frame_data;     
00212     int             inter_scal;      
00213     uint32_t        frame_size;      
00214     uint32_t        pic_hdr_size;    
00215     uint8_t         frame_flags;
00216     uint16_t        checksum;        
00217 
00218     IVIPicConfig    pic_conf;
00219     IVIPlaneDesc    planes[3];       
00220 
00221     int             buf_switch;      
00222     int             dst_buf;         
00223     int             ref_buf;         
00224     int             ref2_buf;        
00225 
00226     IVIHuffTab      mb_vlc;          
00227     IVIHuffTab      blk_vlc;         
00228 
00229     uint8_t         rvmap_sel;
00230     uint8_t         in_imf;
00231     uint8_t         in_q;            
00232     uint8_t         pic_glob_quant;
00233     uint8_t         unknown1;
00234 
00235     uint16_t        gop_hdr_size;
00236     uint8_t         gop_flags;
00237     uint32_t        lock_word;
00238 
00239 #if IVI4_STREAM_ANALYSER
00240     uint8_t         has_b_frames;
00241     uint8_t         has_transp;
00242     uint8_t         uses_tiling;
00243     uint8_t         uses_haar;
00244     uint8_t         uses_fullpel;
00245 #endif
00246 
00247     int             (*decode_pic_hdr)  (struct IVI45DecContext *ctx, AVCodecContext *avctx);
00248     int             (*decode_band_hdr) (struct IVI45DecContext *ctx, IVIBandDesc *band, AVCodecContext *avctx);
00249     int             (*decode_mb_info)  (struct IVI45DecContext *ctx, IVIBandDesc *band, IVITile *tile, AVCodecContext *avctx);
00250     void            (*switch_buffers)  (struct IVI45DecContext *ctx);
00251     int             (*is_nonnull_frame)(struct IVI45DecContext *ctx);
00252 
00253     int gop_invalid;
00254 } IVI45DecContext;
00255 
00257 static inline int ivi_pic_config_cmp(IVIPicConfig *str1, IVIPicConfig *str2)
00258 {
00259     return str1->pic_width    != str2->pic_width    || str1->pic_height    != str2->pic_height    ||
00260            str1->chroma_width != str2->chroma_width || str1->chroma_height != str2->chroma_height ||
00261            str1->tile_width   != str2->tile_width   || str1->tile_height   != str2->tile_height   ||
00262            str1->luma_bands   != str2->luma_bands   || str1->chroma_bands  != str2->chroma_bands;
00263 }
00264 
00266 #define IVI_NUM_TILES(stride, tile_size) (((stride) + (tile_size) - 1) / (tile_size))
00267 
00269 #define IVI_MBs_PER_TILE(tile_width, tile_height, mb_size) \
00270     ((((tile_width) + (mb_size) - 1) / (mb_size)) * (((tile_height) + (mb_size) - 1) / (mb_size)))
00271 
00273 #define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1)))
00274 
00276 static inline int ivi_scale_mv(int mv, int mv_scale)
00277 {
00278     return (mv + (mv > 0) + (mv_scale - 1)) >> mv_scale;
00279 }
00280 
00290 int  ff_ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag);
00291 
00295 void ff_ivi_init_static_vlc(void);
00296 
00308 int  ff_ivi_dec_huff_desc(GetBitContext *gb, int desc_coded, int which_tab,
00309                           IVIHuffTab *huff_tab, AVCodecContext *avctx);
00310 
00318 int  ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2);
00319 
00326 void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src);
00327 
00335 int  ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg);
00336 
00342 void ff_ivi_free_buffers(IVIPlaneDesc *planes);
00343 
00352 int  ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height);
00353 
00364 int  ff_ivi_dec_tile_data_size(GetBitContext *gb);
00365 
00377 int  ff_ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile);
00378 
00388 void ff_ivi_output_plane(IVIPlaneDesc *plane, uint8_t *dst, int dst_pitch);
00389 
00393 uint16_t ivi_calc_band_checksum (IVIBandDesc *band);
00394 
00398 int ivi_check_band (IVIBandDesc *band, const uint8_t *ref, int pitch);
00399 
00400 int ff_ivi_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00401                         AVPacket *avpkt);
00402 av_cold int ff_ivi_decode_close(AVCodecContext *avctx);
00403 
00404 #endif /* AVCODEC_IVI_COMMON_H */