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.

466 lines
18KB

  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 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. * 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 "libavutil/intreadwrite.h"
  31. #include "avcodec.h"
  32. //#define DEBUG
  33. /* dct code */
  34. void ff_fdct_ifast(int16_t *data);
  35. void ff_fdct_ifast248(int16_t *data);
  36. void ff_jpeg_fdct_islow_8(int16_t *data);
  37. void ff_jpeg_fdct_islow_10(int16_t *data);
  38. void ff_fdct248_islow_8(int16_t *data);
  39. void ff_fdct248_islow_10(int16_t *data);
  40. void ff_j_rev_dct(int16_t *data);
  41. void ff_j_rev_dct4(int16_t *data);
  42. void ff_j_rev_dct2(int16_t *data);
  43. void ff_j_rev_dct1(int16_t *data);
  44. void ff_fdct_mmx(int16_t *block);
  45. void ff_fdct_mmxext(int16_t *block);
  46. void ff_fdct_sse2(int16_t *block);
  47. /* encoding scans */
  48. extern const uint8_t ff_alternate_horizontal_scan[64];
  49. extern const uint8_t ff_alternate_vertical_scan[64];
  50. extern const uint8_t ff_zigzag_direct[64];
  51. extern const uint8_t ff_zigzag248_direct[64];
  52. /* pixel operations */
  53. #define MAX_NEG_CROP 1024
  54. /* temporary */
  55. extern uint32_t ff_squareTbl[512];
  56. extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
  57. #define PUTAVG_PIXELS(depth)\
  58. void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  59. void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  60. void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  61. void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
  62. PUTAVG_PIXELS( 8)
  63. PUTAVG_PIXELS( 9)
  64. PUTAVG_PIXELS(10)
  65. PUTAVG_PIXELS(12)
  66. PUTAVG_PIXELS(14)
  67. #define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
  68. #define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
  69. #define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
  70. #define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
  71. /* RV40 functions */
  72. void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  73. void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  74. void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  75. void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  76. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  77. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  78. /* minimum alignment rules ;)
  79. If you notice errors in the align stuff, need more alignment for some ASM code
  80. for some CPU or need to use a function with less aligned data then send a mail
  81. to the ffmpeg-devel mailing list, ...
  82. !warning These alignments might not match reality, (missing attribute((align))
  83. stuff somewhere possible).
  84. I (Michael) did not check them, these are just the alignments which I think
  85. could be reached easily ...
  86. !future video codecs might need functions with less strict alignment
  87. */
  88. /* add and put pixel (decoding) */
  89. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  90. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
  91. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, ptrdiff_t line_size, int h);
  92. typedef void (*tpel_mc_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int w, int h);
  93. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  94. typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
  95. #define DEF_OLD_QPEL(name)\
  96. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  97. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  98. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  99. DEF_OLD_QPEL(qpel16_mc11_old_c)
  100. DEF_OLD_QPEL(qpel16_mc31_old_c)
  101. DEF_OLD_QPEL(qpel16_mc12_old_c)
  102. DEF_OLD_QPEL(qpel16_mc32_old_c)
  103. DEF_OLD_QPEL(qpel16_mc13_old_c)
  104. DEF_OLD_QPEL(qpel16_mc33_old_c)
  105. DEF_OLD_QPEL(qpel8_mc11_old_c)
  106. DEF_OLD_QPEL(qpel8_mc31_old_c)
  107. DEF_OLD_QPEL(qpel8_mc12_old_c)
  108. DEF_OLD_QPEL(qpel8_mc32_old_c)
  109. DEF_OLD_QPEL(qpel8_mc13_old_c)
  110. DEF_OLD_QPEL(qpel8_mc33_old_c)
  111. #define CALL_2X_PIXELS(a, b, n)\
  112. static void a(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  113. b(block , pixels , line_size, h);\
  114. b(block+n, pixels+n, line_size, h);\
  115. }
  116. /* motion estimation */
  117. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
  118. // although currently h<4 is not used as functions with width <8 are neither used nor implemented
  119. typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size, int h)/* __attribute__ ((const))*/;
  120. /**
  121. * Scantable.
  122. */
  123. typedef struct ScanTable{
  124. const uint8_t *scantable;
  125. uint8_t permutated[64];
  126. uint8_t raster_end[64];
  127. } ScanTable;
  128. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  129. void ff_init_scantable_permutation(uint8_t *idct_permutation,
  130. int idct_permutation_type);
  131. #define EMULATED_EDGE(depth) \
  132. void ff_emulated_edge_mc_ ## depth (uint8_t *buf, const uint8_t *src, ptrdiff_t linesize,\
  133. int block_w, int block_h,\
  134. int src_x, int src_y, int w, int h);
  135. EMULATED_EDGE(8)
  136. EMULATED_EDGE(16)
  137. /**
  138. * DSPContext.
  139. */
  140. typedef struct DSPContext {
  141. /**
  142. * Size of DCT coefficients.
  143. */
  144. int dct_bits;
  145. /* pixel ops : interface with DCT */
  146. void (*get_pixels)(int16_t *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  147. void (*diff_pixels)(int16_t *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  148. void (*put_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  149. void (*put_signed_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  150. void (*add_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  151. void (*add_pixels8)(uint8_t *pixels, int16_t *block, int line_size);
  152. void (*add_pixels4)(uint8_t *pixels, int16_t *block, int line_size);
  153. int (*sum_abs_dctelem)(int16_t *block/*align 16*/);
  154. /**
  155. * translational global motion compensation.
  156. */
  157. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  158. /**
  159. * global motion compensation.
  160. */
  161. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  162. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  163. void (*clear_block)(int16_t *block/*align 16*/);
  164. void (*clear_blocks)(int16_t *blocks/*align 16*/);
  165. int (*pix_sum)(uint8_t * pix, int line_size);
  166. int (*pix_norm1)(uint8_t * pix, int line_size);
  167. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  168. me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
  169. me_cmp_func sse[6];
  170. me_cmp_func hadamard8_diff[6];
  171. me_cmp_func dct_sad[6];
  172. me_cmp_func quant_psnr[6];
  173. me_cmp_func bit[6];
  174. me_cmp_func rd[6];
  175. me_cmp_func vsad[6];
  176. me_cmp_func vsse[6];
  177. me_cmp_func nsse[6];
  178. me_cmp_func w53[6];
  179. me_cmp_func w97[6];
  180. me_cmp_func dct_max[6];
  181. me_cmp_func dct264_sad[6];
  182. me_cmp_func me_pre_cmp[6];
  183. me_cmp_func me_cmp[6];
  184. me_cmp_func me_sub_cmp[6];
  185. me_cmp_func mb_cmp[6];
  186. me_cmp_func ildct_cmp[6]; //only width 16 used
  187. me_cmp_func frame_skip_cmp[6]; //only width 8 used
  188. int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
  189. int size);
  190. /**
  191. * Halfpel motion compensation with rounding (a+b+1)>>1.
  192. * this is an array[4][4] of motion compensation functions for 4
  193. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  194. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  195. * @param block destination where the result is stored
  196. * @param pixels source
  197. * @param line_size number of bytes in a horizontal line of block
  198. * @param h height
  199. */
  200. op_pixels_func put_pixels_tab[4][4];
  201. /**
  202. * Halfpel motion compensation with rounding (a+b+1)>>1.
  203. * This is an array[4][4] of motion compensation functions for 4
  204. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  205. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  206. * @param block destination into which the result is averaged (a+b+1)>>1
  207. * @param pixels source
  208. * @param line_size number of bytes in a horizontal line of block
  209. * @param h height
  210. */
  211. op_pixels_func avg_pixels_tab[4][4];
  212. /**
  213. * Halfpel motion compensation with no rounding (a+b)>>1.
  214. * this is an array[2][4] of motion compensation functions for 2
  215. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  216. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  217. * @param block destination where the result is stored
  218. * @param pixels source
  219. * @param line_size number of bytes in a horizontal line of block
  220. * @param h height
  221. */
  222. op_pixels_func put_no_rnd_pixels_tab[4][4];
  223. /**
  224. * Halfpel motion compensation with no rounding (a+b)>>1.
  225. * this is an array[4] of motion compensation functions for 1
  226. * horizontal blocksize (16) and the 4 halfpel positions<br>
  227. * *pixels_tab[0][ xhalfpel + 2*yhalfpel ]
  228. * @param block destination into which the result is averaged (a+b)>>1
  229. * @param pixels source
  230. * @param line_size number of bytes in a horizontal line of block
  231. * @param h height
  232. */
  233. op_pixels_func avg_no_rnd_pixels_tab[4];
  234. /**
  235. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  236. * this is an array[12] of motion compensation functions for the 9 thirdpe
  237. * positions<br>
  238. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  239. * @param block destination where the result is stored
  240. * @param pixels source
  241. * @param line_size number of bytes in a horizontal line of block
  242. * @param h height
  243. */
  244. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  245. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  246. qpel_mc_func put_qpel_pixels_tab[2][16];
  247. qpel_mc_func avg_qpel_pixels_tab[2][16];
  248. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  249. qpel_mc_func put_mspel_pixels_tab[8];
  250. me_cmp_func pix_abs[2][4];
  251. /* huffyuv specific */
  252. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  253. void (*diff_bytes)(uint8_t *dst/*align 16*/, const uint8_t *src1/*align 16*/, const uint8_t *src2/*align 1*/,int w);
  254. /**
  255. * subtract huffyuv's variant of median prediction
  256. * note, this might read from src1[-1], src2[-1]
  257. */
  258. void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
  259. void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
  260. int (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
  261. void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
  262. /* this might write to dst[w] */
  263. void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
  264. void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
  265. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  266. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  267. void (*h261_loop_filter)(uint8_t *src, int stride);
  268. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  269. void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
  270. /* (I)DCT */
  271. void (*fdct)(int16_t *block/* align 16*/);
  272. void (*fdct248)(int16_t *block/* align 16*/);
  273. /* IDCT really*/
  274. void (*idct)(int16_t *block/* align 16*/);
  275. /**
  276. * block -> idct -> clip to unsigned 8 bit -> dest.
  277. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  278. * @param line_size size in bytes of a horizontal line of dest
  279. */
  280. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
  281. /**
  282. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  283. * @param line_size size in bytes of a horizontal line of dest
  284. */
  285. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
  286. /**
  287. * idct input permutation.
  288. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  289. * IDCT)
  290. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  291. * with the zigzag/alternate scan<br>
  292. * an example to avoid confusion:
  293. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  294. * - (x -> reference dct -> reference idct -> x)
  295. * - (x -> reference dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  296. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  297. */
  298. uint8_t idct_permutation[64];
  299. int idct_permutation_type;
  300. #define FF_NO_IDCT_PERM 1
  301. #define FF_LIBMPEG2_IDCT_PERM 2
  302. #define FF_SIMPLE_IDCT_PERM 3
  303. #define FF_TRANSPOSE_IDCT_PERM 4
  304. #define FF_PARTTRANS_IDCT_PERM 5
  305. #define FF_SSE2_IDCT_PERM 6
  306. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
  307. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  308. #define BASIS_SHIFT 16
  309. #define RECON_SHIFT 6
  310. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides);
  311. #define EDGE_WIDTH 16
  312. #define EDGE_TOP 1
  313. #define EDGE_BOTTOM 2
  314. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  315. /**
  316. * Calculate scalar product of two vectors.
  317. * @param len length of vectors, should be multiple of 16
  318. */
  319. int32_t (*scalarproduct_int16)(const int16_t *v1, const int16_t *v2/*align 16*/, int len);
  320. /* ape functions */
  321. /**
  322. * Calculate scalar product of v1 and v2,
  323. * and v1[i] += v3[i] * mul
  324. * @param len length of vectors, should be multiple of 16
  325. */
  326. int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, const int16_t *v2, const int16_t *v3, int len, int mul);
  327. /**
  328. * Apply symmetric window in 16-bit fixed-point.
  329. * @param output destination array
  330. * constraints: 16-byte aligned
  331. * @param input source array
  332. * constraints: 16-byte aligned
  333. * @param window window array
  334. * constraints: 16-byte aligned, at least len/2 elements
  335. * @param len full window length
  336. * constraints: multiple of ? greater than zero
  337. */
  338. void (*apply_window_int16)(int16_t *output, const int16_t *input,
  339. const int16_t *window, unsigned int len);
  340. /**
  341. * Clip each element in an array of int32_t to a given minimum and maximum value.
  342. * @param dst destination array
  343. * constraints: 16-byte aligned
  344. * @param src source array
  345. * constraints: 16-byte aligned
  346. * @param min minimum value
  347. * constraints: must be in the range [-(1 << 24), 1 << 24]
  348. * @param max maximum value
  349. * constraints: must be in the range [-(1 << 24), 1 << 24]
  350. * @param len number of elements in the array
  351. * constraints: multiple of 32 greater than zero
  352. */
  353. void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
  354. int32_t max, unsigned int len);
  355. op_fill_func fill_block_tab[2];
  356. } DSPContext;
  357. void ff_dsputil_static_init(void);
  358. void ff_dsputil_init(DSPContext* p, AVCodecContext *avctx);
  359. attribute_deprecated void dsputil_init(DSPContext* c, AVCodecContext *avctx);
  360. int ff_check_alignment(void);
  361. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  362. void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  363. void ff_dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
  364. void ff_dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
  365. void ff_dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  366. void ff_dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  367. void ff_dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  368. void ff_dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
  369. void ff_dsputil_init_dwt(DSPContext *c);
  370. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  371. # define STRIDE_ALIGN 16
  372. #else
  373. # define STRIDE_ALIGN 8
  374. #endif
  375. // Some broken preprocessors need a second expansion
  376. // to be forced to tokenize __VA_ARGS__
  377. #define E(x) x
  378. #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
  379. uint8_t la_##v[sizeof(t s o) + (a)]; \
  380. t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
  381. #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) \
  382. DECLARE_ALIGNED(a, t, la_##v) s o; \
  383. t (*v) o = la_##v
  384. #define LOCAL_ALIGNED(a, t, v, ...) E(LOCAL_ALIGNED_A(a, t, v, __VA_ARGS__,,))
  385. #if HAVE_LOCAL_ALIGNED_8
  386. # define LOCAL_ALIGNED_8(t, v, ...) E(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
  387. #else
  388. # define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED(8, t, v, __VA_ARGS__)
  389. #endif
  390. #if HAVE_LOCAL_ALIGNED_16
  391. # define LOCAL_ALIGNED_16(t, v, ...) E(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
  392. #else
  393. # define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED(16, t, v, __VA_ARGS__)
  394. #endif
  395. #endif /* AVCODEC_DSPUTIL_H */