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.

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