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.

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