yading@10: /* yading@10: * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) yading@10: * yading@10: * Copyright (c) 2009 Maxim Poliakovski yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * This file contains structures and macros shared by both Indeo4 and yading@10: * Indeo5 decoders. yading@10: */ yading@10: yading@10: #ifndef AVCODEC_IVI_COMMON_H yading@10: #define AVCODEC_IVI_COMMON_H yading@10: yading@10: #include "avcodec.h" yading@10: #include "get_bits.h" yading@10: #include yading@10: yading@10: #define IVI_VLC_BITS 13 ///< max number of bits of the ivi's huffman codes yading@10: #define IVI4_STREAM_ANALYSER 0 yading@10: #define IVI5_IS_PROTECTED 0x20 yading@10: yading@10: /** yading@10: * huffman codebook descriptor yading@10: */ yading@10: typedef struct IVIHuffDesc { yading@10: int32_t num_rows; yading@10: uint8_t xbits[16]; yading@10: } IVIHuffDesc; yading@10: yading@10: /** yading@10: * macroblock/block huffman table descriptor yading@10: */ yading@10: typedef struct IVIHuffTab { yading@10: int32_t tab_sel; /// index of one of the predefined tables yading@10: /// or "7" for custom one yading@10: VLC *tab; /// pointer to the table associated with tab_sel yading@10: yading@10: /// the following are used only when tab_sel == 7 yading@10: IVIHuffDesc cust_desc; /// custom Huffman codebook descriptor yading@10: VLC cust_tab; /// vlc table for custom codebook yading@10: } IVIHuffTab; yading@10: yading@10: enum { yading@10: IVI_MB_HUFF = 0, /// Huffman table is used for coding macroblocks yading@10: IVI_BLK_HUFF = 1 /// Huffman table is used for coding blocks yading@10: }; yading@10: yading@10: yading@10: /** yading@10: * Common scan patterns (defined in ivi_common.c) yading@10: */ yading@10: extern const uint8_t ff_ivi_vertical_scan_8x8[64]; yading@10: extern const uint8_t ff_ivi_horizontal_scan_8x8[64]; yading@10: extern const uint8_t ff_ivi_direct_scan_4x4[16]; yading@10: yading@10: yading@10: /** yading@10: * Declare inverse transform function types yading@10: */ yading@10: typedef void (InvTransformPtr)(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags); yading@10: typedef void (DCTransformPtr) (const int32_t *in, int16_t *out, uint32_t pitch, int blk_size); yading@10: yading@10: yading@10: /** yading@10: * run-value (RLE) table descriptor yading@10: */ yading@10: typedef struct RVMapDesc { yading@10: uint8_t eob_sym; ///< end of block symbol yading@10: uint8_t esc_sym; ///< escape symbol yading@10: uint8_t runtab[256]; yading@10: int8_t valtab[256]; yading@10: } RVMapDesc; yading@10: yading@10: extern const RVMapDesc ff_ivi_rvmap_tabs[9]; yading@10: yading@10: yading@10: /** yading@10: * information for Indeo macroblock (16x16, 8x8 or 4x4) yading@10: */ yading@10: typedef struct IVIMbInfo { yading@10: int16_t xpos; yading@10: int16_t ypos; yading@10: uint32_t buf_offs; ///< address in the output buffer for this mb yading@10: uint8_t type; ///< macroblock type: 0 - INTRA, 1 - INTER yading@10: uint8_t cbp; ///< coded block pattern yading@10: int8_t q_delta; ///< quant delta yading@10: int8_t mv_x; ///< motion vector (x component) yading@10: int8_t mv_y; ///< motion vector (y component) yading@10: } IVIMbInfo; yading@10: yading@10: yading@10: /** yading@10: * information for Indeo tile yading@10: */ yading@10: typedef struct IVITile { yading@10: int xpos; yading@10: int ypos; yading@10: int width; yading@10: int height; yading@10: int mb_size; yading@10: int is_empty; ///< = 1 if this tile doesn't contain any data yading@10: int data_size; ///< size of the data in bytes yading@10: int num_MBs; ///< number of macroblocks in this tile yading@10: IVIMbInfo *mbs; ///< array of macroblock descriptors yading@10: IVIMbInfo *ref_mbs; ///< ptr to the macroblock descriptors of the reference tile yading@10: } IVITile; yading@10: yading@10: yading@10: /** yading@10: * information for Indeo wavelet band yading@10: */ yading@10: typedef struct IVIBandDesc { yading@10: int plane; ///< plane number this band belongs to yading@10: int band_num; ///< band number yading@10: int width; yading@10: int height; yading@10: int aheight; ///< aligned band height yading@10: const uint8_t *data_ptr; ///< ptr to the first byte of the band data yading@10: int data_size; ///< size of the band data yading@10: int16_t *buf; ///< pointer to the output buffer for this band yading@10: int16_t *ref_buf; ///< pointer to the reference frame buffer (for motion compensation) yading@10: int16_t *bufs[3]; ///< array of pointers to the band buffers yading@10: int pitch; ///< pitch associated with the buffers above yading@10: int is_empty; ///< = 1 if this band doesn't contain any data yading@10: int mb_size; ///< macroblock size yading@10: int blk_size; ///< block size yading@10: int is_halfpel; ///< precision of the motion compensation: 0 - fullpel, 1 - halfpel yading@10: int inherit_mv; ///< tells if motion vector is inherited from reference macroblock yading@10: int inherit_qdelta; ///< tells if quantiser delta is inherited from reference macroblock yading@10: int qdelta_present; ///< tells if Qdelta signal is present in the bitstream (Indeo5 only) yading@10: int quant_mat; ///< dequant matrix index yading@10: int glob_quant; ///< quant base for this band yading@10: const uint8_t *scan; ///< ptr to the scan pattern yading@10: int scan_size; ///< size of the scantable yading@10: yading@10: IVIHuffTab blk_vlc; ///< vlc table for decoding block data yading@10: yading@10: int num_corr; ///< number of correction entries yading@10: uint8_t corr[61*2]; ///< rvmap correction pairs yading@10: int rvmap_sel; ///< rvmap table selector yading@10: RVMapDesc *rv_map; ///< ptr to the RLE table for this band yading@10: int num_tiles; ///< number of tiles in this band yading@10: IVITile *tiles; ///< array of tile descriptors yading@10: InvTransformPtr *inv_transform; yading@10: DCTransformPtr *dc_transform; yading@10: int is_2d_trans; ///< 1 indicates that the two-dimensional inverse transform is used yading@10: int transform_size; ///< block size of the transform yading@10: int32_t checksum; ///< for debug purposes yading@10: int checksum_present; yading@10: int bufsize; ///< band buffer size in bytes yading@10: const uint16_t *intra_base; ///< quantization matrix for intra blocks yading@10: const uint16_t *inter_base; ///< quantization matrix for inter blocks yading@10: const uint8_t *intra_scale; ///< quantization coefficient for intra blocks yading@10: const uint8_t *inter_scale; ///< quantization coefficient for inter blocks yading@10: } IVIBandDesc; yading@10: yading@10: yading@10: /** yading@10: * color plane (luma or chroma) information yading@10: */ yading@10: typedef struct IVIPlaneDesc { yading@10: uint16_t width; yading@10: uint16_t height; yading@10: uint8_t num_bands; ///< number of bands this plane subdivided into yading@10: IVIBandDesc *bands; ///< array of band descriptors yading@10: } IVIPlaneDesc; yading@10: yading@10: yading@10: typedef struct IVIPicConfig { yading@10: uint16_t pic_width; yading@10: uint16_t pic_height; yading@10: uint16_t chroma_width; yading@10: uint16_t chroma_height; yading@10: uint16_t tile_width; yading@10: uint16_t tile_height; yading@10: uint8_t luma_bands; yading@10: uint8_t chroma_bands; yading@10: } IVIPicConfig; yading@10: yading@10: typedef struct IVI45DecContext { yading@10: GetBitContext gb; yading@10: RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables yading@10: yading@10: uint32_t frame_num; yading@10: int frame_type; yading@10: int prev_frame_type; ///< frame type of the previous frame yading@10: uint32_t data_size; ///< size of the frame data in bytes from picture header yading@10: int is_scalable; yading@10: int transp_status; ///< transparency mode status: 1 - enabled yading@10: const uint8_t *frame_data; ///< input frame data pointer yading@10: int inter_scal; ///< signals a sequence of scalable inter frames yading@10: uint32_t frame_size; ///< frame size in bytes yading@10: uint32_t pic_hdr_size; ///< picture header size in bytes yading@10: uint8_t frame_flags; yading@10: uint16_t checksum; ///< frame checksum yading@10: yading@10: IVIPicConfig pic_conf; yading@10: IVIPlaneDesc planes[3]; ///< color planes yading@10: yading@10: int buf_switch; ///< used to switch between three buffers yading@10: int dst_buf; ///< buffer index for the currently decoded frame yading@10: int ref_buf; ///< inter frame reference buffer index yading@10: int ref2_buf; ///< temporal storage for switching buffers yading@10: yading@10: IVIHuffTab mb_vlc; ///< current macroblock table descriptor yading@10: IVIHuffTab blk_vlc; ///< current block table descriptor yading@10: yading@10: uint8_t rvmap_sel; yading@10: uint8_t in_imf; yading@10: uint8_t in_q; ///< flag for explicitly stored quantiser delta yading@10: uint8_t pic_glob_quant; yading@10: uint8_t unknown1; yading@10: yading@10: uint16_t gop_hdr_size; yading@10: uint8_t gop_flags; yading@10: uint32_t lock_word; yading@10: yading@10: #if IVI4_STREAM_ANALYSER yading@10: uint8_t has_b_frames; yading@10: uint8_t has_transp; yading@10: uint8_t uses_tiling; yading@10: uint8_t uses_haar; yading@10: uint8_t uses_fullpel; yading@10: #endif yading@10: yading@10: int (*decode_pic_hdr) (struct IVI45DecContext *ctx, AVCodecContext *avctx); yading@10: int (*decode_band_hdr) (struct IVI45DecContext *ctx, IVIBandDesc *band, AVCodecContext *avctx); yading@10: int (*decode_mb_info) (struct IVI45DecContext *ctx, IVIBandDesc *band, IVITile *tile, AVCodecContext *avctx); yading@10: void (*switch_buffers) (struct IVI45DecContext *ctx); yading@10: int (*is_nonnull_frame)(struct IVI45DecContext *ctx); yading@10: yading@10: int gop_invalid; yading@10: int buf_invalid[3]; yading@10: } IVI45DecContext; yading@10: yading@10: /** compare some properties of two pictures */ yading@10: static inline int ivi_pic_config_cmp(IVIPicConfig *str1, IVIPicConfig *str2) yading@10: { yading@10: return str1->pic_width != str2->pic_width || str1->pic_height != str2->pic_height || yading@10: str1->chroma_width != str2->chroma_width || str1->chroma_height != str2->chroma_height || yading@10: str1->tile_width != str2->tile_width || str1->tile_height != str2->tile_height || yading@10: str1->luma_bands != str2->luma_bands || str1->chroma_bands != str2->chroma_bands; yading@10: } yading@10: yading@10: /** calculate number of tiles in a stride */ yading@10: #define IVI_NUM_TILES(stride, tile_size) (((stride) + (tile_size) - 1) / (tile_size)) yading@10: yading@10: /** calculate number of macroblocks in a tile */ yading@10: #define IVI_MBs_PER_TILE(tile_width, tile_height, mb_size) \ yading@10: ((((tile_width) + (mb_size) - 1) / (mb_size)) * (((tile_height) + (mb_size) - 1) / (mb_size))) yading@10: yading@10: /** convert unsigned values into signed ones (the sign is in the LSB) */ yading@10: #define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1))) yading@10: yading@10: /** scale motion vector */ yading@10: static inline int ivi_scale_mv(int mv, int mv_scale) yading@10: { yading@10: return (mv + (mv > 0) + (mv_scale - 1)) >> mv_scale; yading@10: } yading@10: yading@10: /** yading@10: * Initialize static codes used for macroblock and block decoding. yading@10: */ yading@10: void ff_ivi_init_static_vlc(void); yading@10: yading@10: /** yading@10: * Decode a huffman codebook descriptor from the bitstream yading@10: * and select specified huffman table. yading@10: * yading@10: * @param[in,out] gb the GetBit context yading@10: * @param[in] desc_coded flag signalling if table descriptor was coded yading@10: * @param[in] which_tab codebook purpose (IVI_MB_HUFF or IVI_BLK_HUFF) yading@10: * @param[out] huff_tab pointer to the descriptor of the selected table yading@10: * @param[in] avctx AVCodecContext pointer yading@10: * @return zero on success, negative value otherwise yading@10: */ yading@10: int ff_ivi_dec_huff_desc(GetBitContext *gb, int desc_coded, int which_tab, yading@10: IVIHuffTab *huff_tab, AVCodecContext *avctx); yading@10: yading@10: /** yading@10: * Initialize planes (prepares descriptors, allocates buffers etc). yading@10: * yading@10: * @param[in,out] planes pointer to the array of the plane descriptors yading@10: * @param[in] cfg pointer to the ivi_pic_config structure describing picture layout yading@10: * @return result code: 0 - OK yading@10: */ yading@10: int ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg); yading@10: yading@10: /** yading@10: * Initialize tile and macroblock descriptors. yading@10: * yading@10: * @param[in,out] planes pointer to the array of the plane descriptors yading@10: * @param[in] tile_width tile width yading@10: * @param[in] tile_height tile height yading@10: * @return result code: 0 - OK yading@10: */ yading@10: int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height); yading@10: yading@10: int ff_ivi_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, yading@10: AVPacket *avpkt); yading@10: int ff_ivi_decode_close(AVCodecContext *avctx); yading@10: yading@10: #endif /* AVCODEC_IVI_COMMON_H */