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.

780 lines
30KB

  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. typedef short DCTELEM;
  35. void fdct_ifast (DCTELEM *data);
  36. void fdct_ifast248 (DCTELEM *data);
  37. void ff_jpeg_fdct_islow_8(DCTELEM *data);
  38. void ff_jpeg_fdct_islow_10(DCTELEM *data);
  39. void ff_fdct248_islow_8(DCTELEM *data);
  40. void ff_fdct248_islow_10(DCTELEM *data);
  41. void j_rev_dct (DCTELEM *data);
  42. void j_rev_dct4 (DCTELEM *data);
  43. void j_rev_dct2 (DCTELEM *data);
  44. void j_rev_dct1 (DCTELEM *data);
  45. void ff_wmv2_idct_c(DCTELEM *data);
  46. void ff_fdct_mmx(DCTELEM *block);
  47. void ff_fdct_mmx2(DCTELEM *block);
  48. void ff_fdct_sse2(DCTELEM *block);
  49. #define H264_IDCT(depth) \
  50. void ff_h264_idct8_add_ ## depth ## _c(uint8_t *dst, DCTELEM *block, int stride);\
  51. void ff_h264_idct_add_ ## depth ## _c(uint8_t *dst, DCTELEM *block, int stride);\
  52. void ff_h264_idct8_dc_add_ ## depth ## _c(uint8_t *dst, DCTELEM *block, int stride);\
  53. void ff_h264_idct_dc_add_ ## depth ## _c(uint8_t *dst, DCTELEM *block, int stride);\
  54. void ff_h264_idct_add16_ ## depth ## _c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);\
  55. void ff_h264_idct_add16intra_ ## depth ## _c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);\
  56. void ff_h264_idct8_add4_ ## depth ## _c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);\
  57. void ff_h264_idct_add8_422_ ## depth ## _c(uint8_t **dest, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);\
  58. void ff_h264_idct_add8_ ## depth ## _c(uint8_t **dest, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);\
  59. void ff_h264_luma_dc_dequant_idct_ ## depth ## _c(DCTELEM *output, DCTELEM *input, int qmul);\
  60. void ff_h264_chroma422_dc_dequant_idct_ ## depth ## _c(DCTELEM *block, int qmul);\
  61. void ff_h264_chroma_dc_dequant_idct_ ## depth ## _c(DCTELEM *block, int qmul);
  62. H264_IDCT( 8)
  63. H264_IDCT( 9)
  64. H264_IDCT(10)
  65. void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp);
  66. void ff_svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  67. /* encoding scans */
  68. extern const uint8_t ff_alternate_horizontal_scan[64];
  69. extern const uint8_t ff_alternate_vertical_scan[64];
  70. extern const uint8_t ff_zigzag_direct[64];
  71. extern const uint8_t ff_zigzag248_direct[64];
  72. /* pixel operations */
  73. #define MAX_NEG_CROP 1024
  74. /* temporary */
  75. extern uint32_t ff_squareTbl[512];
  76. extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
  77. #define PUTAVG_PIXELS(depth)\
  78. void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  79. void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  80. void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  81. void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
  82. PUTAVG_PIXELS( 8)
  83. PUTAVG_PIXELS( 9)
  84. PUTAVG_PIXELS(10)
  85. #define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
  86. #define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
  87. #define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
  88. #define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
  89. /* VP3 DSP functions */
  90. void ff_vp3_idct_c(DCTELEM *block/* align 16*/);
  91. void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  92. void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  93. void ff_vp3_idct_dc_add_c(uint8_t *dest/*align 8*/, int line_size, const DCTELEM *block/*align 16*/);
  94. void ff_vp3_v_loop_filter_c(uint8_t *src, int stride, int *bounding_values);
  95. void ff_vp3_h_loop_filter_c(uint8_t *src, int stride, int *bounding_values);
  96. /* EA functions */
  97. void ff_ea_idct_put_c(uint8_t *dest, int linesize, DCTELEM *block);
  98. /* RV40 functions */
  99. void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  100. void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  101. void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  102. void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  103. /* 1/2^n downscaling functions from imgconvert.c */
  104. void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  105. void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  106. void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  107. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  108. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  109. /* minimum alignment rules ;)
  110. If you notice errors in the align stuff, need more alignment for some ASM code
  111. for some CPU or need to use a function with less aligned data then send a mail
  112. to the ffmpeg-devel mailing list, ...
  113. !warning These alignments might not match reality, (missing attribute((align))
  114. stuff somewhere possible).
  115. I (Michael) did not check them, these are just the alignments which I think
  116. could be reached easily ...
  117. !future video codecs might need functions with less strict alignment
  118. */
  119. /*
  120. void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
  121. void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
  122. void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  123. void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  124. void clear_blocks_c(DCTELEM *blocks);
  125. */
  126. /* add and put pixel (decoding) */
  127. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  128. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
  129. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  130. 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);
  131. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  132. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  133. typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
  134. #define DEF_OLD_QPEL(name)\
  135. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  136. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  137. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  138. DEF_OLD_QPEL(qpel16_mc11_old_c)
  139. DEF_OLD_QPEL(qpel16_mc31_old_c)
  140. DEF_OLD_QPEL(qpel16_mc12_old_c)
  141. DEF_OLD_QPEL(qpel16_mc32_old_c)
  142. DEF_OLD_QPEL(qpel16_mc13_old_c)
  143. DEF_OLD_QPEL(qpel16_mc33_old_c)
  144. DEF_OLD_QPEL(qpel8_mc11_old_c)
  145. DEF_OLD_QPEL(qpel8_mc31_old_c)
  146. DEF_OLD_QPEL(qpel8_mc12_old_c)
  147. DEF_OLD_QPEL(qpel8_mc32_old_c)
  148. DEF_OLD_QPEL(qpel8_mc13_old_c)
  149. DEF_OLD_QPEL(qpel8_mc33_old_c)
  150. #define CALL_2X_PIXELS(a, b, n)\
  151. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  152. b(block , pixels , line_size, h);\
  153. b(block+n, pixels+n, line_size, h);\
  154. }
  155. /* motion estimation */
  156. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
  157. // although currently h<4 is not used as functions with width <8 are neither used nor implemented
  158. 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))*/;
  159. /**
  160. * Scantable.
  161. */
  162. typedef struct ScanTable{
  163. const uint8_t *scantable;
  164. uint8_t permutated[64];
  165. uint8_t raster_end[64];
  166. #if ARCH_PPC
  167. /** Used by dct_quantize_altivec to find last-non-zero */
  168. DECLARE_ALIGNED(16, uint8_t, inverse)[64];
  169. #endif
  170. } ScanTable;
  171. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  172. #define EMULATED_EDGE(depth) \
  173. void ff_emulated_edge_mc_ ## depth (uint8_t *buf, const uint8_t *src, int linesize,\
  174. int block_w, int block_h,\
  175. int src_x, int src_y, int w, int h);
  176. EMULATED_EDGE(8)
  177. EMULATED_EDGE(9)
  178. EMULATED_EDGE(10)
  179. void ff_add_pixels_clamped_c(const DCTELEM *block, uint8_t *dest, int linesize);
  180. void ff_put_pixels_clamped_c(const DCTELEM *block, uint8_t *dest, int linesize);
  181. void ff_put_signed_pixels_clamped_c(const DCTELEM *block, uint8_t *dest, int linesize);
  182. /**
  183. * DSPContext.
  184. */
  185. typedef struct DSPContext {
  186. /**
  187. * Size of DCT coefficients.
  188. */
  189. int dct_bits;
  190. /* pixel ops : interface with DCT */
  191. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  192. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  193. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  194. void (*put_signed_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  195. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  196. void (*add_pixels8)(uint8_t *pixels, DCTELEM *block, int line_size);
  197. void (*add_pixels4)(uint8_t *pixels, DCTELEM *block, int line_size);
  198. int (*sum_abs_dctelem)(DCTELEM *block/*align 16*/);
  199. /**
  200. * Motion estimation with emulated edge values.
  201. * @param buf pointer to destination buffer (unaligned)
  202. * @param src pointer to pixel source (unaligned)
  203. * @param linesize width (in pixels) for src/buf
  204. * @param block_w number of pixels (per row) to copy to buf
  205. * @param block_h nummber of pixel rows to copy to buf
  206. * @param src_x offset of src to start of row - this may be negative
  207. * @param src_y offset of src to top of image - this may be negative
  208. * @param w width of src in pixels
  209. * @param h height of src in pixels
  210. */
  211. void (*emulated_edge_mc)(uint8_t *buf, const uint8_t *src, int linesize,
  212. int block_w, int block_h,
  213. int src_x, int src_y, int w, int h);
  214. /**
  215. * translational global motion compensation.
  216. */
  217. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  218. /**
  219. * global motion compensation.
  220. */
  221. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  222. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  223. void (*clear_block)(DCTELEM *block/*align 16*/);
  224. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  225. int (*pix_sum)(uint8_t * pix, int line_size);
  226. int (*pix_norm1)(uint8_t * pix, int line_size);
  227. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  228. me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
  229. me_cmp_func sse[6];
  230. me_cmp_func hadamard8_diff[6];
  231. me_cmp_func dct_sad[6];
  232. me_cmp_func quant_psnr[6];
  233. me_cmp_func bit[6];
  234. me_cmp_func rd[6];
  235. me_cmp_func vsad[6];
  236. me_cmp_func vsse[6];
  237. me_cmp_func nsse[6];
  238. me_cmp_func w53[6];
  239. me_cmp_func w97[6];
  240. me_cmp_func dct_max[6];
  241. me_cmp_func dct264_sad[6];
  242. me_cmp_func me_pre_cmp[6];
  243. me_cmp_func me_cmp[6];
  244. me_cmp_func me_sub_cmp[6];
  245. me_cmp_func mb_cmp[6];
  246. me_cmp_func ildct_cmp[6]; //only width 16 used
  247. me_cmp_func frame_skip_cmp[6]; //only width 8 used
  248. int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
  249. int size);
  250. /**
  251. * Halfpel motion compensation with rounding (a+b+1)>>1.
  252. * this is an array[4][4] of motion compensation functions for 4
  253. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  254. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  255. * @param block destination where the result is stored
  256. * @param pixels source
  257. * @param line_size number of bytes in a horizontal line of block
  258. * @param h height
  259. */
  260. op_pixels_func put_pixels_tab[4][4];
  261. /**
  262. * Halfpel motion compensation with rounding (a+b+1)>>1.
  263. * This is an array[4][4] of motion compensation functions for 4
  264. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  265. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  266. * @param block destination into which the result is averaged (a+b+1)>>1
  267. * @param pixels source
  268. * @param line_size number of bytes in a horizontal line of block
  269. * @param h height
  270. */
  271. op_pixels_func avg_pixels_tab[4][4];
  272. /**
  273. * Halfpel motion compensation with no rounding (a+b)>>1.
  274. * this is an array[2][4] of motion compensation functions for 2
  275. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  276. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  277. * @param block destination where the result is stored
  278. * @param pixels source
  279. * @param line_size number of bytes in a horizontal line of block
  280. * @param h height
  281. */
  282. op_pixels_func put_no_rnd_pixels_tab[4][4];
  283. /**
  284. * Halfpel motion compensation with no rounding (a+b)>>1.
  285. * this is an array[2][4] of motion compensation functions for 2
  286. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  287. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  288. * @param block destination into which the result is averaged (a+b)>>1
  289. * @param pixels source
  290. * @param line_size number of bytes in a horizontal line of block
  291. * @param h height
  292. */
  293. op_pixels_func avg_no_rnd_pixels_tab[4][4];
  294. void (*put_no_rnd_pixels_l2[2])(uint8_t *block/*align width (8 or 16)*/, const uint8_t *a/*align 1*/, const uint8_t *b/*align 1*/, int line_size, int h);
  295. /**
  296. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  297. * this is an array[12] of motion compensation functions for the 9 thirdpe
  298. * positions<br>
  299. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  300. * @param block destination where the result is stored
  301. * @param pixels source
  302. * @param line_size number of bytes in a horizontal line of block
  303. * @param h height
  304. */
  305. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  306. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  307. qpel_mc_func put_qpel_pixels_tab[2][16];
  308. qpel_mc_func avg_qpel_pixels_tab[2][16];
  309. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  310. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  311. qpel_mc_func put_mspel_pixels_tab[8];
  312. /**
  313. * h264 Chroma MC
  314. */
  315. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  316. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  317. qpel_mc_func put_h264_qpel_pixels_tab[4][16];
  318. qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
  319. qpel_mc_func put_2tap_qpel_pixels_tab[4][16];
  320. qpel_mc_func avg_2tap_qpel_pixels_tab[4][16];
  321. me_cmp_func pix_abs[2][4];
  322. /* huffyuv specific */
  323. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  324. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  325. /**
  326. * subtract huffyuv's variant of median prediction
  327. * note, this might read from src1[-1], src2[-1]
  328. */
  329. void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
  330. void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
  331. int (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
  332. void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
  333. /* this might write to dst[w] */
  334. void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
  335. void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
  336. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  337. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  338. void (*h261_loop_filter)(uint8_t *src, int stride);
  339. void (*x8_v_loop_filter)(uint8_t *src, int stride, int qscale);
  340. void (*x8_h_loop_filter)(uint8_t *src, int stride, int qscale);
  341. void (*vp3_idct_dc_add)(uint8_t *dest/*align 8*/, int line_size, const DCTELEM *block/*align 16*/);
  342. void (*vp3_v_loop_filter)(uint8_t *src, int stride, int *bounding_values);
  343. void (*vp3_h_loop_filter)(uint8_t *src, int stride, int *bounding_values);
  344. /* assume len is a multiple of 4, and arrays are 16-byte aligned */
  345. void (*vorbis_inverse_coupling)(float *mag, float *ang, int blocksize);
  346. void (*ac3_downmix)(float (*samples)[256], float (*matrix)[2], int out_ch, int in_ch, int len);
  347. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  348. void (*vector_fmul)(float *dst, const float *src0, const float *src1, int len);
  349. void (*vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len);
  350. /* assume len is a multiple of 8, and src arrays are 16-byte aligned */
  351. void (*vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len);
  352. /* assume len is a multiple of 4, and arrays are 16-byte aligned */
  353. void (*vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len);
  354. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  355. void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
  356. /**
  357. * Multiply a vector of floats by a scalar float. Source and
  358. * destination vectors must overlap exactly or not at all.
  359. * @param dst result vector, 16-byte aligned
  360. * @param src input vector, 16-byte aligned
  361. * @param mul scalar value
  362. * @param len length of vector, multiple of 4
  363. */
  364. void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
  365. int len);
  366. /**
  367. * Multiply a vector of floats by a scalar float and add to
  368. * destination vector. Source and destination vectors must
  369. * overlap exactly or not at all.
  370. * @param dst result vector, 16-byte aligned
  371. * @param src input vector, 16-byte aligned
  372. * @param mul scalar value
  373. * @param len length of vector, multiple of 4
  374. */
  375. void (*vector_fmac_scalar)(float *dst, const float *src, float mul,
  376. int len);
  377. /**
  378. * Calculate the scalar product of two vectors of floats.
  379. * @param v1 first vector, 16-byte aligned
  380. * @param v2 second vector, 16-byte aligned
  381. * @param len length of vectors, multiple of 4
  382. */
  383. float (*scalarproduct_float)(const float *v1, const float *v2, int len);
  384. /**
  385. * Calculate the sum and difference of two vectors of floats.
  386. * @param v1 first input vector, sum output, 16-byte aligned
  387. * @param v2 second input vector, difference output, 16-byte aligned
  388. * @param len length of vectors, multiple of 4
  389. */
  390. void (*butterflies_float)(float *restrict v1, float *restrict v2, int len);
  391. /* (I)DCT */
  392. void (*fdct)(DCTELEM *block/* align 16*/);
  393. void (*fdct248)(DCTELEM *block/* align 16*/);
  394. /* IDCT really*/
  395. void (*idct)(DCTELEM *block/* align 16*/);
  396. /**
  397. * block -> idct -> clip to unsigned 8 bit -> dest.
  398. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  399. * @param line_size size in bytes of a horizontal line of dest
  400. */
  401. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  402. /**
  403. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  404. * @param line_size size in bytes of a horizontal line of dest
  405. */
  406. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  407. /**
  408. * idct input permutation.
  409. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  410. * IDCT)
  411. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  412. * with the zigzag/alternate scan<br>
  413. * an example to avoid confusion:
  414. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  415. * - (x -> referece dct -> reference idct -> x)
  416. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  417. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  418. */
  419. uint8_t idct_permutation[64];
  420. int idct_permutation_type;
  421. #define FF_NO_IDCT_PERM 1
  422. #define FF_LIBMPEG2_IDCT_PERM 2
  423. #define FF_SIMPLE_IDCT_PERM 3
  424. #define FF_TRANSPOSE_IDCT_PERM 4
  425. #define FF_PARTTRANS_IDCT_PERM 5
  426. #define FF_SSE2_IDCT_PERM 6
  427. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
  428. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  429. #define BASIS_SHIFT 16
  430. #define RECON_SHIFT 6
  431. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides);
  432. #define EDGE_WIDTH 16
  433. #define EDGE_TOP 1
  434. #define EDGE_BOTTOM 2
  435. void (*prefetch)(void *mem, int stride, int h);
  436. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  437. /* mlp/truehd functions */
  438. void (*mlp_filter_channel)(int32_t *state, const int32_t *coeff,
  439. int firorder, int iirorder,
  440. unsigned int filter_shift, int32_t mask, int blocksize,
  441. int32_t *sample_buffer);
  442. /* intrax8 functions */
  443. void (*x8_spatial_compensation[12])(uint8_t *src , uint8_t *dst, int linesize);
  444. void (*x8_setup_spatial_compensation)(uint8_t *src, uint8_t *dst, int linesize,
  445. int * range, int * sum, int edges);
  446. /**
  447. * Calculate scalar product of two vectors.
  448. * @param len length of vectors, should be multiple of 16
  449. * @param shift number of bits to discard from product
  450. */
  451. int32_t (*scalarproduct_int16)(const int16_t *v1, const int16_t *v2/*align 16*/, int len, int shift);
  452. /* ape functions */
  453. /**
  454. * Calculate scalar product of v1 and v2,
  455. * and v1[i] += v3[i] * mul
  456. * @param len length of vectors, should be multiple of 16
  457. */
  458. int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, const int16_t *v2, const int16_t *v3, int len, int mul);
  459. /**
  460. * Apply symmetric window in 16-bit fixed-point.
  461. * @param output destination array
  462. * constraints: 16-byte aligned
  463. * @param input source array
  464. * constraints: 16-byte aligned
  465. * @param window window array
  466. * constraints: 16-byte aligned, at least len/2 elements
  467. * @param len full window length
  468. * constraints: multiple of ? greater than zero
  469. */
  470. void (*apply_window_int16)(int16_t *output, const int16_t *input,
  471. const int16_t *window, unsigned int len);
  472. /**
  473. * Clip each element in an array of int32_t to a given minimum and maximum value.
  474. * @param dst destination array
  475. * constraints: 16-byte aligned
  476. * @param src source array
  477. * constraints: 16-byte aligned
  478. * @param min minimum value
  479. * constraints: must in the the range [-(1<<24), 1<<24]
  480. * @param max maximum value
  481. * constraints: must in the the range [-(1<<24), 1<<24]
  482. * @param len number of elements in the array
  483. * constraints: multiple of 32 greater than zero
  484. */
  485. void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
  486. int32_t max, unsigned int len);
  487. op_fill_func fill_block_tab[2];
  488. } DSPContext;
  489. void dsputil_static_init(void);
  490. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  491. int ff_check_alignment(void);
  492. /**
  493. * permute block according to permuatation.
  494. * @param last last non zero element in scantable order
  495. */
  496. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  497. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  498. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  499. #define BYTE_VEC64(c) ((c)*0x0001000100010001UL)
  500. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  501. {
  502. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  503. }
  504. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  505. {
  506. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  507. }
  508. static inline uint64_t rnd_avg64(uint64_t a, uint64_t b)
  509. {
  510. return (a | b) - (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
  511. }
  512. static inline uint64_t no_rnd_avg64(uint64_t a, uint64_t b)
  513. {
  514. return (a & b) + (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
  515. }
  516. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  517. switch(type&0xFF){
  518. default:
  519. case FF_CMP_SAD:
  520. return lambda>>FF_LAMBDA_SHIFT;
  521. case FF_CMP_DCT:
  522. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  523. case FF_CMP_W53:
  524. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  525. case FF_CMP_W97:
  526. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  527. case FF_CMP_SATD:
  528. case FF_CMP_DCT264:
  529. return (2*lambda)>>FF_LAMBDA_SHIFT;
  530. case FF_CMP_RD:
  531. case FF_CMP_PSNR:
  532. case FF_CMP_SSE:
  533. case FF_CMP_NSSE:
  534. return lambda2>>FF_LAMBDA_SHIFT;
  535. case FF_CMP_BIT:
  536. return 1;
  537. }
  538. }
  539. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  540. void dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
  541. void dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
  542. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  543. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  544. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  545. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  546. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  547. void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
  548. void ff_dsputil_init_dwt(DSPContext *c);
  549. void ff_intrax8dsp_init(DSPContext* c, AVCodecContext *avctx);
  550. void ff_mlp_init(DSPContext* c, AVCodecContext *avctx);
  551. void ff_mlp_init_x86(DSPContext* c, AVCodecContext *avctx);
  552. #if ARCH_ARM
  553. #if HAVE_NEON
  554. # define STRIDE_ALIGN 16
  555. #endif
  556. #elif ARCH_PPC
  557. #define STRIDE_ALIGN 16
  558. #elif HAVE_MMI
  559. #define STRIDE_ALIGN 16
  560. #endif
  561. #ifndef STRIDE_ALIGN
  562. # define STRIDE_ALIGN 8
  563. #endif
  564. #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
  565. uint8_t la_##v[sizeof(t s o) + (a)]; \
  566. t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
  567. #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) DECLARE_ALIGNED(a, t, v) s o
  568. #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_A(a, t, v, __VA_ARGS__,,)
  569. #if HAVE_LOCAL_ALIGNED_8
  570. # define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,)
  571. #else
  572. # define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED(8, t, v, __VA_ARGS__)
  573. #endif
  574. #if HAVE_LOCAL_ALIGNED_16
  575. # define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,)
  576. #else
  577. # define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED(16, t, v, __VA_ARGS__)
  578. #endif
  579. #define WRAPPER8_16(name8, name16)\
  580. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  581. return name8(s, dst , src , stride, h)\
  582. +name8(s, dst+8 , src+8 , stride, h);\
  583. }
  584. #define WRAPPER8_16_SQ(name8, name16)\
  585. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  586. int score=0;\
  587. score +=name8(s, dst , src , stride, 8);\
  588. score +=name8(s, dst+8 , src+8 , stride, 8);\
  589. if(h==16){\
  590. dst += 8*stride;\
  591. src += 8*stride;\
  592. score +=name8(s, dst , src , stride, 8);\
  593. score +=name8(s, dst+8 , src+8 , stride, 8);\
  594. }\
  595. return score;\
  596. }
  597. static inline void copy_block2(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  598. {
  599. int i;
  600. for(i=0; i<h; i++)
  601. {
  602. AV_WN16(dst , AV_RN16(src ));
  603. dst+=dstStride;
  604. src+=srcStride;
  605. }
  606. }
  607. static inline void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  608. {
  609. int i;
  610. for(i=0; i<h; i++)
  611. {
  612. AV_WN32(dst , AV_RN32(src ));
  613. dst+=dstStride;
  614. src+=srcStride;
  615. }
  616. }
  617. static inline void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  618. {
  619. int i;
  620. for(i=0; i<h; i++)
  621. {
  622. AV_WN32(dst , AV_RN32(src ));
  623. AV_WN32(dst+4 , AV_RN32(src+4 ));
  624. dst+=dstStride;
  625. src+=srcStride;
  626. }
  627. }
  628. static inline void copy_block9(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  629. {
  630. int i;
  631. for(i=0; i<h; i++)
  632. {
  633. AV_WN32(dst , AV_RN32(src ));
  634. AV_WN32(dst+4 , AV_RN32(src+4 ));
  635. dst[8]= src[8];
  636. dst+=dstStride;
  637. src+=srcStride;
  638. }
  639. }
  640. static inline void copy_block16(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  641. {
  642. int i;
  643. for(i=0; i<h; i++)
  644. {
  645. AV_WN32(dst , AV_RN32(src ));
  646. AV_WN32(dst+4 , AV_RN32(src+4 ));
  647. AV_WN32(dst+8 , AV_RN32(src+8 ));
  648. AV_WN32(dst+12, AV_RN32(src+12));
  649. dst+=dstStride;
  650. src+=srcStride;
  651. }
  652. }
  653. static inline void copy_block17(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  654. {
  655. int i;
  656. for(i=0; i<h; i++)
  657. {
  658. AV_WN32(dst , AV_RN32(src ));
  659. AV_WN32(dst+4 , AV_RN32(src+4 ));
  660. AV_WN32(dst+8 , AV_RN32(src+8 ));
  661. AV_WN32(dst+12, AV_RN32(src+12));
  662. dst[16]= src[16];
  663. dst+=dstStride;
  664. src+=srcStride;
  665. }
  666. }
  667. #endif /* AVCODEC_DSPUTIL_H */