You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
13KB

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