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.

301 lines
12KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  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. * DSP utils.
  25. * Note, many functions in here may use MMX which trashes the FPU state, it is
  26. * absolutely necessary to call emms_c() between DSP & float/double code.
  27. */
  28. #ifndef AVCODEC_DSPUTIL_H
  29. #define AVCODEC_DSPUTIL_H
  30. #include "avcodec.h"
  31. extern uint32_t ff_square_tab[512];
  32. void ff_put_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
  33. void ff_avg_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
  34. void ff_put_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
  35. void ff_avg_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
  36. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  37. int dxx, int dxy, int dyx, int dyy, int shift, int r,
  38. int width, int height);
  39. /* minimum alignment rules ;)
  40. * If you notice errors in the align stuff, need more alignment for some ASM code
  41. * for some CPU or need to use a function with less aligned data then send a mail
  42. * to the libav-devel mailing list, ...
  43. *
  44. * !warning These alignments might not match reality, (missing attribute((align))
  45. * stuff somewhere possible).
  46. * I (Michael) did not check them, these are just the alignments which I think
  47. * could be reached easily ...
  48. *
  49. * !future video codecs might need functions with less strict alignment
  50. */
  51. /* add and put pixel (decoding)
  52. * Block sizes for op_pixels_func are 8x4,8x8 16x8 16x16.
  53. * h for op_pixels_func is limited to { width / 2, width },
  54. * but never larger than 16 and never smaller than 4. */
  55. typedef void (*qpel_mc_func)(uint8_t *dst /* align width (8 or 16) */,
  56. uint8_t *src /* align 1 */, ptrdiff_t stride);
  57. typedef void (*op_fill_func)(uint8_t *block /* align width (8 or 16) */,
  58. uint8_t value, int line_size, int h);
  59. #define DEF_OLD_QPEL(name) \
  60. void ff_put_ ## name(uint8_t *dst /* align width (8 or 16) */, \
  61. uint8_t *src /* align 1 */, ptrdiff_t stride); \
  62. void ff_put_no_rnd_ ## name(uint8_t *dst /* align width (8 or 16) */, \
  63. uint8_t *src /* align 1 */, ptrdiff_t stride); \
  64. void ff_avg_ ## name(uint8_t *dst /* align width (8 or 16) */, \
  65. uint8_t *src /* align 1 */, ptrdiff_t stride);
  66. DEF_OLD_QPEL(qpel16_mc11_old_c)
  67. DEF_OLD_QPEL(qpel16_mc31_old_c)
  68. DEF_OLD_QPEL(qpel16_mc12_old_c)
  69. DEF_OLD_QPEL(qpel16_mc32_old_c)
  70. DEF_OLD_QPEL(qpel16_mc13_old_c)
  71. DEF_OLD_QPEL(qpel16_mc33_old_c)
  72. DEF_OLD_QPEL(qpel8_mc11_old_c)
  73. DEF_OLD_QPEL(qpel8_mc31_old_c)
  74. DEF_OLD_QPEL(qpel8_mc12_old_c)
  75. DEF_OLD_QPEL(qpel8_mc32_old_c)
  76. DEF_OLD_QPEL(qpel8_mc13_old_c)
  77. DEF_OLD_QPEL(qpel8_mc33_old_c)
  78. struct MpegEncContext;
  79. /* Motion estimation:
  80. * h is limited to { width / 2, width, 2 * width },
  81. * but never larger than 16 and never smaller than 2.
  82. * Although currently h < 4 is not used as functions with
  83. * width < 8 are neither used nor implemented. */
  84. typedef int (*me_cmp_func)(struct MpegEncContext *c,
  85. uint8_t *blk1 /* align width (8 or 16) */,
  86. uint8_t *blk2 /* align 1 */, int line_size, int h);
  87. /**
  88. * Scantable.
  89. */
  90. typedef struct ScanTable {
  91. const uint8_t *scantable;
  92. uint8_t permutated[64];
  93. uint8_t raster_end[64];
  94. } ScanTable;
  95. void ff_init_scantable(uint8_t *permutation, ScanTable *st,
  96. const uint8_t *src_scantable);
  97. void ff_init_scantable_permutation(uint8_t *idct_permutation,
  98. int idct_permutation_type);
  99. /**
  100. * DSPContext.
  101. */
  102. typedef struct DSPContext {
  103. /* pixel ops : interface with DCT */
  104. void (*get_pixels)(int16_t *block /* align 16 */,
  105. const uint8_t *pixels /* align 8 */,
  106. int line_size);
  107. void (*diff_pixels)(int16_t *block /* align 16 */,
  108. const uint8_t *s1 /* align 8 */,
  109. const uint8_t *s2 /* align 8 */,
  110. int stride);
  111. void (*put_pixels_clamped)(const int16_t *block /* align 16 */,
  112. uint8_t *pixels /* align 8 */,
  113. int line_size);
  114. void (*put_signed_pixels_clamped)(const int16_t *block /* align 16 */,
  115. uint8_t *pixels /* align 8 */,
  116. int line_size);
  117. void (*add_pixels_clamped)(const int16_t *block /* align 16 */,
  118. uint8_t *pixels /* align 8 */,
  119. int line_size);
  120. void (*add_pixels8)(uint8_t *pixels, int16_t *block, int line_size);
  121. int (*sum_abs_dctelem)(int16_t *block /* align 16 */);
  122. /**
  123. * translational global motion compensation.
  124. */
  125. void (*gmc1)(uint8_t *dst /* align 8 */, uint8_t *src /* align 1 */,
  126. int srcStride, int h, int x16, int y16, int rounder);
  127. /**
  128. * global motion compensation.
  129. */
  130. void (*gmc)(uint8_t *dst /* align 8 */, uint8_t *src /* align 1 */,
  131. int stride, int h, int ox, int oy,
  132. int dxx, int dxy, int dyx, int dyy,
  133. int shift, int r, int width, int height);
  134. void (*clear_block)(int16_t *block /* align 16 */);
  135. void (*clear_blocks)(int16_t *blocks /* align 16 */);
  136. int (*pix_sum)(uint8_t *pix, int line_size);
  137. int (*pix_norm1)(uint8_t *pix, int line_size);
  138. me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
  139. me_cmp_func sse[6];
  140. me_cmp_func hadamard8_diff[6];
  141. me_cmp_func dct_sad[6];
  142. me_cmp_func quant_psnr[6];
  143. me_cmp_func bit[6];
  144. me_cmp_func rd[6];
  145. me_cmp_func vsad[6];
  146. me_cmp_func vsse[6];
  147. me_cmp_func nsse[6];
  148. me_cmp_func dct_max[6];
  149. me_cmp_func dct264_sad[6];
  150. me_cmp_func me_pre_cmp[6];
  151. me_cmp_func me_cmp[6];
  152. me_cmp_func me_sub_cmp[6];
  153. me_cmp_func mb_cmp[6];
  154. me_cmp_func ildct_cmp[6]; // only width 16 used
  155. me_cmp_func frame_skip_cmp[6]; // only width 8 used
  156. qpel_mc_func put_qpel_pixels_tab[2][16];
  157. qpel_mc_func avg_qpel_pixels_tab[2][16];
  158. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  159. qpel_mc_func put_mspel_pixels_tab[8];
  160. me_cmp_func pix_abs[2][4];
  161. void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
  162. void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
  163. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  164. void (*vector_clipf)(float *dst /* align 16 */,
  165. const float *src /* align 16 */,
  166. float min, float max, int len /* align 16 */);
  167. /* (I)DCT */
  168. void (*fdct)(int16_t *block /* align 16 */);
  169. void (*fdct248)(int16_t *block /* align 16 */);
  170. /* IDCT really */
  171. void (*idct)(int16_t *block /* align 16 */);
  172. /**
  173. * block -> idct -> clip to unsigned 8 bit -> dest.
  174. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  175. * @param line_size size in bytes of a horizontal line of dest
  176. */
  177. void (*idct_put)(uint8_t *dest /* align 8 */,
  178. int line_size, int16_t *block /* align 16 */);
  179. /**
  180. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  181. * @param line_size size in bytes of a horizontal line of dest
  182. */
  183. void (*idct_add)(uint8_t *dest /* align 8 */,
  184. int line_size, int16_t *block /* align 16 */);
  185. /**
  186. * IDCT input permutation.
  187. * Several optimized IDCTs need a permutated input (relative to the
  188. * normal order of the reference IDCT).
  189. * This permutation must be performed before the idct_put/add.
  190. * Note, normally this can be merged with the zigzag/alternate scan<br>
  191. * An example to avoid confusion:
  192. * - (->decode coeffs -> zigzag reorder -> dequant -> reference IDCT -> ...)
  193. * - (x -> reference DCT -> reference IDCT -> x)
  194. * - (x -> reference DCT -> simple_mmx_perm = idct_permutation
  195. * -> simple_idct_mmx -> x)
  196. * - (-> decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant
  197. * -> simple_idct_mmx -> ...)
  198. */
  199. uint8_t idct_permutation[64];
  200. int idct_permutation_type;
  201. #define FF_NO_IDCT_PERM 1
  202. #define FF_LIBMPEG2_IDCT_PERM 2
  203. #define FF_SIMPLE_IDCT_PERM 3
  204. #define FF_TRANSPOSE_IDCT_PERM 4
  205. #define FF_PARTTRANS_IDCT_PERM 5
  206. #define FF_SSE2_IDCT_PERM 6
  207. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64],
  208. int16_t basis[64], int scale);
  209. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  210. #define BASIS_SHIFT 16
  211. #define RECON_SHIFT 6
  212. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height,
  213. int w, int h, int sides);
  214. #define EDGE_WIDTH 16
  215. #define EDGE_TOP 1
  216. #define EDGE_BOTTOM 2
  217. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src,
  218. int src_wrap, int width, int height);
  219. /**
  220. * Calculate scalar product of two vectors.
  221. * @param len length of vectors, should be multiple of 16
  222. */
  223. int32_t (*scalarproduct_int16)(const int16_t *v1,
  224. const int16_t *v2 /* align 16 */, int len);
  225. /* ape functions */
  226. /**
  227. * Calculate scalar product of v1 and v2,
  228. * and v1[i] += v3[i] * mul
  229. * @param len length of vectors, should be multiple of 16
  230. */
  231. int32_t (*scalarproduct_and_madd_int16)(int16_t *v1 /* align 16 */,
  232. const int16_t *v2,
  233. const int16_t *v3,
  234. int len, int mul);
  235. /**
  236. * Clip each element in an array of int32_t to a given minimum and
  237. * maximum value.
  238. * @param dst destination array
  239. * constraints: 16-byte aligned
  240. * @param src source array
  241. * constraints: 16-byte aligned
  242. * @param min minimum value
  243. * constraints: must be in the range [-(1 << 24), 1 << 24]
  244. * @param max maximum value
  245. * constraints: must be in the range [-(1 << 24), 1 << 24]
  246. * @param len number of elements in the array
  247. * constraints: multiple of 32 greater than zero
  248. */
  249. void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
  250. int32_t max, unsigned int len);
  251. op_fill_func fill_block_tab[2];
  252. } DSPContext;
  253. void ff_dsputil_static_init(void);
  254. void ff_dsputil_init(DSPContext *p, AVCodecContext *avctx);
  255. void ff_set_cmp(DSPContext *c, me_cmp_func *cmp, int type);
  256. void ff_dsputil_init_arm(DSPContext *c, AVCodecContext *avctx,
  257. unsigned high_bit_depth);
  258. void ff_dsputil_init_bfin(DSPContext *c, AVCodecContext *avctx,
  259. unsigned high_bit_depth);
  260. void ff_dsputil_init_ppc(DSPContext *c, AVCodecContext *avctx,
  261. unsigned high_bit_depth);
  262. void ff_dsputil_init_x86(DSPContext *c, AVCodecContext *avctx,
  263. unsigned high_bit_depth);
  264. #endif /* AVCODEC_DSPUTIL_H */