ivi_common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
  3. *
  4. * Copyright (c) 2009 Maxim Poliakovski
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * This file contains structures and macros shared by both Indeo4 and
  25. * Indeo5 decoders.
  26. */
  27. #ifndef AVCODEC_IVI_COMMON_H
  28. #define AVCODEC_IVI_COMMON_H
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include <stdint.h>
  32. #define IVI_VLC_BITS 13 ///< max number of bits of the ivi's huffman codes
  33. /**
  34. * huffman codebook descriptor
  35. */
  36. typedef struct {
  37. int32_t num_rows;
  38. uint8_t xbits[16];
  39. } IVIHuffDesc;
  40. /**
  41. * macroblock/block huffman table descriptor
  42. */
  43. typedef struct {
  44. int32_t tab_sel; /// index of one of the predefined tables
  45. /// or "7" for custom one
  46. VLC *tab; /// pointer to the table associated with tab_sel
  47. //! the following are used only when tab_sel == 7
  48. IVIHuffDesc cust_desc; /// custom Huffman codebook descriptor
  49. VLC cust_tab; /// vlc table for custom codebook
  50. } IVIHuffTab;
  51. enum {
  52. IVI_MB_HUFF = 0, /// Huffman table is used for coding macroblocks
  53. IVI_BLK_HUFF = 1 /// Huffman table is used for coding blocks
  54. };
  55. extern VLC ff_ivi_mb_vlc_tabs [8]; ///< static macroblock Huffman tables
  56. extern VLC ff_ivi_blk_vlc_tabs[8]; ///< static block Huffman tables
  57. /**
  58. * Common scan patterns (defined in ivi_common.c)
  59. */
  60. extern const uint8_t ff_ivi_vertical_scan_8x8[64];
  61. extern const uint8_t ff_ivi_horizontal_scan_8x8[64];
  62. extern const uint8_t ff_ivi_direct_scan_4x4[16];
  63. /**
  64. * Declare inverse transform function types
  65. */
  66. typedef void (InvTransformPtr)(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags);
  67. typedef void (DCTransformPtr) (const int32_t *in, int16_t *out, uint32_t pitch, int blk_size);
  68. /**
  69. * run-value (RLE) table descriptor
  70. */
  71. typedef struct {
  72. uint8_t eob_sym; ///< end of block symbol
  73. uint8_t esc_sym; ///< escape symbol
  74. uint8_t runtab[256];
  75. int8_t valtab[256];
  76. } RVMapDesc;
  77. extern const RVMapDesc ff_ivi_rvmap_tabs[9];
  78. /**
  79. * information for Indeo macroblock (16x16, 8x8 or 4x4)
  80. */
  81. typedef struct {
  82. int16_t xpos;
  83. int16_t ypos;
  84. uint32_t buf_offs; ///< address in the output buffer for this mb
  85. uint8_t type; ///< macroblock type: 0 - INTRA, 1 - INTER
  86. uint8_t cbp; ///< coded block pattern
  87. int8_t q_delta; ///< quant delta
  88. int8_t mv_x; ///< motion vector (x component)
  89. int8_t mv_y; ///< motion vector (y component)
  90. } IVIMbInfo;
  91. /**
  92. * information for Indeo tile
  93. */
  94. typedef struct {
  95. int xpos;
  96. int ypos;
  97. int width;
  98. int height;
  99. int is_empty; ///< = 1 if this tile doesn't contain any data
  100. int data_size; ///< size of the data in bytes
  101. int num_MBs; ///< number of macroblocks in this tile
  102. IVIMbInfo *mbs; ///< array of macroblock descriptors
  103. IVIMbInfo *ref_mbs; ///< ptr to the macroblock descriptors of the reference tile
  104. } IVITile;
  105. /**
  106. * information for Indeo wavelet band
  107. */
  108. typedef struct {
  109. int plane; ///< plane number this band belongs to
  110. int band_num; ///< band number
  111. int width;
  112. int height;
  113. const uint8_t *data_ptr; ///< ptr to the first byte of the band data
  114. int data_size; ///< size of the band data
  115. int16_t *buf; ///< pointer to the output buffer for this band
  116. int16_t *ref_buf; ///< pointer to the reference frame buffer (for motion compensation)
  117. int16_t *bufs[3]; ///< array of pointers to the band buffers
  118. int pitch; ///< pitch associated with the buffers above
  119. int is_empty; ///< = 1 if this band doesn't contain any data
  120. int mb_size; ///< macroblock size
  121. int blk_size; ///< block size
  122. int is_halfpel; ///< precision of the motion compensation: 0 - fullpel, 1 - halfpel
  123. int inherit_mv; ///< tells if motion vector is inherited from reference macroblock
  124. int inherit_qdelta; ///< tells if quantiser delta is inherited from reference macroblock
  125. int qdelta_present; ///< tells if Qdelta signal is present in the bitstream (Indeo5 only)
  126. int quant_mat; ///< dequant matrix index
  127. int glob_quant; ///< quant base for this band
  128. const uint8_t *scan; ///< ptr to the scan pattern
  129. IVIHuffTab blk_vlc; ///< vlc table for decoding block data
  130. int num_corr; ///< number of correction entries
  131. uint8_t corr[61*2]; ///< rvmap correction pairs
  132. int rvmap_sel; ///< rvmap table selector
  133. RVMapDesc *rv_map; ///< ptr to the RLE table for this band
  134. int num_tiles; ///< number of tiles in this band
  135. IVITile *tiles; ///< array of tile descriptors
  136. InvTransformPtr *inv_transform;
  137. DCTransformPtr *dc_transform;
  138. int is_2d_trans; ///< 1 indicates that the two-dimensional inverse transform is used
  139. int32_t checksum; ///< for debug purposes
  140. int checksum_present;
  141. int bufsize; ///< band buffer size in bytes
  142. const uint16_t *intra_base; ///< quantization matrix for intra blocks
  143. const uint16_t *inter_base; ///< quantization matrix for inter blocks
  144. const uint8_t *intra_scale; ///< quantization coefficient for intra blocks
  145. const uint8_t *inter_scale; ///< quantization coefficient for inter blocks
  146. } IVIBandDesc;
  147. /**
  148. * color plane (luma or chroma) information
  149. */
  150. typedef struct {
  151. uint16_t width;
  152. uint16_t height;
  153. uint8_t num_bands; ///< number of bands this plane subdivided into
  154. IVIBandDesc *bands; ///< array of band descriptors
  155. } IVIPlaneDesc;
  156. typedef struct {
  157. uint16_t pic_width;
  158. uint16_t pic_height;
  159. uint16_t chroma_width;
  160. uint16_t chroma_height;
  161. uint16_t tile_width;
  162. uint16_t tile_height;
  163. uint8_t luma_bands;
  164. uint8_t chroma_bands;
  165. } IVIPicConfig;
  166. /** compare some properties of two pictures */
  167. static inline int ivi_pic_config_cmp(IVIPicConfig *str1, IVIPicConfig *str2)
  168. {
  169. return (str1->pic_width != str2->pic_width || str1->pic_height != str2->pic_height ||
  170. str1->chroma_width != str2->chroma_width || str1->chroma_height != str2->chroma_height ||
  171. str1->tile_width != str2->tile_width || str1->tile_height != str2->tile_height ||
  172. str1->luma_bands != str2->luma_bands || str1->chroma_bands != str2->chroma_bands);
  173. }
  174. /** calculate number of tiles in a stride */
  175. #define IVI_NUM_TILES(stride, tile_size) (((stride) + (tile_size) - 1) / (tile_size))
  176. /** calculate number of macroblocks in a tile */
  177. #define IVI_MBs_PER_TILE(tile_width, tile_height, mb_size) \
  178. ((((tile_width) + (mb_size) - 1) / (mb_size)) * (((tile_height) + (mb_size) - 1) / (mb_size)))
  179. /** convert unsigned values into signed ones (the sign is in the LSB) */
  180. #define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1)))
  181. /** scale motion vector */
  182. static inline int ivi_scale_mv(int mv, int mv_scale)
  183. {
  184. return (mv + (mv > 0) + (mv_scale - 1)) >> mv_scale;
  185. }
  186. /**
  187. * Generate a huffman codebook from the given descriptor
  188. * and convert it into the Libav VLC table.
  189. *
  190. * @param[in] cb pointer to codebook descriptor
  191. * @param[out] vlc where to place the generated VLC table
  192. * @param[in] flag flag: 1 - for static or 0 for dynamic tables
  193. * @return result code: 0 - OK, -1 = error (invalid codebook descriptor)
  194. */
  195. int ff_ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag);
  196. /**
  197. * Initialize static codes used for macroblock and block decoding.
  198. */
  199. void ff_ivi_init_static_vlc(void);
  200. /**
  201. * Decode a huffman codebook descriptor from the bitstream
  202. * and select specified huffman table.
  203. *
  204. * @param[in,out] gb the GetBit context
  205. * @param[in] desc_coded flag signalling if table descriptor was coded
  206. * @param[in] which_tab codebook purpose (IVI_MB_HUFF or IVI_BLK_HUFF)
  207. * @param[out] huff_tab pointer to the descriptor of the selected table
  208. * @param[in] avctx AVCodecContext pointer
  209. * @return zero on success, negative value otherwise
  210. */
  211. int ff_ivi_dec_huff_desc(GetBitContext *gb, int desc_coded, int which_tab,
  212. IVIHuffTab *huff_tab, AVCodecContext *avctx);
  213. /**
  214. * Compare two huffman codebook descriptors.
  215. *
  216. * @param[in] desc1 ptr to the 1st descriptor to compare
  217. * @param[in] desc2 ptr to the 2nd descriptor to compare
  218. * @return comparison result: 0 - equal, 1 - not equal
  219. */
  220. int ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2);
  221. /**
  222. * Copy huffman codebook descriptors.
  223. *
  224. * @param[out] dst ptr to the destination descriptor
  225. * @param[in] src ptr to the source descriptor
  226. */
  227. void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src);
  228. /**
  229. * Initialize planes (prepares descriptors, allocates buffers etc).
  230. *
  231. * @param[in,out] planes pointer to the array of the plane descriptors
  232. * @param[in] cfg pointer to the ivi_pic_config structure describing picture layout
  233. * @return result code: 0 - OK
  234. */
  235. int ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg);
  236. /**
  237. * Free planes, bands and macroblocks buffers.
  238. *
  239. * @param[in] planes pointer to the array of the plane descriptors
  240. */
  241. void ff_ivi_free_buffers(IVIPlaneDesc *planes);
  242. /**
  243. * Initialize tile and macroblock descriptors.
  244. *
  245. * @param[in,out] planes pointer to the array of the plane descriptors
  246. * @param[in] tile_width tile width
  247. * @param[in] tile_height tile height
  248. * @return result code: 0 - OK
  249. */
  250. int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height);
  251. /**
  252. * Decode size of the tile data.
  253. * The size is stored as a variable-length field having the following format:
  254. * if (tile_data_size < 255) than this field is only one byte long
  255. * if (tile_data_size >= 255) than this field four is byte long: 0xFF X1 X2 X3
  256. * where X1-X3 is size of the tile data
  257. *
  258. * @param[in,out] gb the GetBit context
  259. * @return size of the tile data in bytes
  260. */
  261. int ff_ivi_dec_tile_data_size(GetBitContext *gb);
  262. /**
  263. * Decode block data:
  264. * extract huffman-coded transform coefficients from the bitstream,
  265. * dequantize them, apply inverse transform and motion compensation
  266. * in order to reconstruct the picture.
  267. *
  268. * @param[in,out] gb the GetBit context
  269. * @param[in] band pointer to the band descriptor
  270. * @param[in] tile pointer to the tile descriptor
  271. * @return result code: 0 - OK, -1 = error (corrupted blocks data)
  272. */
  273. int ff_ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile);
  274. /**
  275. * Handle empty tiles by performing data copying and motion
  276. * compensation respectively.
  277. *
  278. * @param[in] avctx ptr to the AVCodecContext
  279. * @param[in] band pointer to the band descriptor
  280. * @param[in] tile pointer to the tile descriptor
  281. * @param[in] mv_scale scaling factor for motion vectors
  282. */
  283. void ff_ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
  284. IVITile *tile, int32_t mv_scale);
  285. /**
  286. * Convert and output the current plane.
  287. * This conversion is done by adding back the bias value of 128
  288. * (subtracted in the encoder) and clipping the result.
  289. *
  290. * @param[in] plane pointer to the descriptor of the plane being processed
  291. * @param[out] dst pointer to the buffer receiving converted pixels
  292. * @param[in] dst_pitch pitch for moving to the next y line
  293. */
  294. void ff_ivi_output_plane(IVIPlaneDesc *plane, uint8_t *dst, int dst_pitch);
  295. /**
  296. * Calculate band checksum from band data.
  297. */
  298. uint16_t ivi_calc_band_checksum (IVIBandDesc *band);
  299. /**
  300. * Verify that band data lies in range.
  301. */
  302. int ivi_check_band (IVIBandDesc *band, const uint8_t *ref, int pitch);
  303. #endif /* AVCODEC_IVI_COMMON_H */