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.

324 lines
12KB

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