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.

620 lines
23KB

  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 "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_fdct_mmx(int16_t *block);
  42. void ff_fdct_mmxext(int16_t *block);
  43. void ff_fdct_sse2(int16_t *block);
  44. #define H264_IDCT(depth) \
  45. void ff_h264_idct8_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
  46. void ff_h264_idct_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
  47. void ff_h264_idct8_dc_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
  48. void ff_h264_idct_dc_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
  49. void ff_h264_idct_add16_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
  50. void ff_h264_idct_add16intra_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
  51. void ff_h264_idct8_add4_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
  52. void ff_h264_idct_add8_422_ ## depth ## _c(uint8_t **dest, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
  53. void ff_h264_idct_add8_ ## depth ## _c(uint8_t **dest, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
  54. void ff_h264_luma_dc_dequant_idct_ ## depth ## _c(int16_t *output, int16_t *input, int qmul);\
  55. void ff_h264_chroma422_dc_dequant_idct_ ## depth ## _c(int16_t *block, int qmul);\
  56. void ff_h264_chroma_dc_dequant_idct_ ## depth ## _c(int16_t *block, int qmul);
  57. H264_IDCT( 8)
  58. H264_IDCT( 9)
  59. H264_IDCT(10)
  60. void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp);
  61. void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block, int stride, int qp, int dc);
  62. /* encoding scans */
  63. extern const uint8_t ff_alternate_horizontal_scan[64];
  64. extern const uint8_t ff_alternate_vertical_scan[64];
  65. extern const uint8_t ff_zigzag_direct[64];
  66. extern const uint8_t ff_zigzag248_direct[64];
  67. /* pixel operations */
  68. #define MAX_NEG_CROP 1024
  69. /* temporary */
  70. extern uint32_t ff_squareTbl[512];
  71. extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
  72. #define PUTAVG_PIXELS(depth)\
  73. void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  74. void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  75. void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
  76. void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
  77. PUTAVG_PIXELS( 8)
  78. PUTAVG_PIXELS( 9)
  79. PUTAVG_PIXELS(10)
  80. #define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
  81. #define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
  82. #define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
  83. #define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
  84. /* RV40 functions */
  85. void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  86. void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  87. void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  88. void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
  89. /* 1/2^n downscaling functions from imgconvert.c */
  90. void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  91. void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  92. void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  93. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  94. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  95. /* minimum alignment rules ;)
  96. If you notice errors in the align stuff, need more alignment for some ASM code
  97. for some CPU or need to use a function with less aligned data then send a mail
  98. to the libav-devel mailing list, ...
  99. !warning These alignments might not match reality, (missing attribute((align))
  100. stuff somewhere possible).
  101. I (Michael) did not check them, these are just the alignments which I think
  102. could be reached easily ...
  103. !future video codecs might need functions with less strict alignment
  104. */
  105. /*
  106. void get_pixels_c(int16_t *block, const uint8_t *pixels, int line_size);
  107. void diff_pixels_c(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride);
  108. void put_pixels_clamped_c(const int16_t *block, uint8_t *pixels, int line_size);
  109. void add_pixels_clamped_c(const int16_t *block, uint8_t *pixels, int line_size);
  110. void clear_blocks_c(int16_t *blocks);
  111. */
  112. /* add and put pixel (decoding) */
  113. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  114. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
  115. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  116. 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);
  117. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  118. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  119. typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
  120. #define DEF_OLD_QPEL(name)\
  121. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  122. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  123. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  124. DEF_OLD_QPEL(qpel16_mc11_old_c)
  125. DEF_OLD_QPEL(qpel16_mc31_old_c)
  126. DEF_OLD_QPEL(qpel16_mc12_old_c)
  127. DEF_OLD_QPEL(qpel16_mc32_old_c)
  128. DEF_OLD_QPEL(qpel16_mc13_old_c)
  129. DEF_OLD_QPEL(qpel16_mc33_old_c)
  130. DEF_OLD_QPEL(qpel8_mc11_old_c)
  131. DEF_OLD_QPEL(qpel8_mc31_old_c)
  132. DEF_OLD_QPEL(qpel8_mc12_old_c)
  133. DEF_OLD_QPEL(qpel8_mc32_old_c)
  134. DEF_OLD_QPEL(qpel8_mc13_old_c)
  135. DEF_OLD_QPEL(qpel8_mc33_old_c)
  136. #define CALL_2X_PIXELS(a, b, n)\
  137. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  138. b(block , pixels , line_size, h);\
  139. b(block+n, pixels+n, line_size, h);\
  140. }
  141. /* motion estimation */
  142. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
  143. // although currently h<4 is not used as functions with width <8 are neither used nor implemented
  144. 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))*/;
  145. /**
  146. * Scantable.
  147. */
  148. typedef struct ScanTable{
  149. const uint8_t *scantable;
  150. uint8_t permutated[64];
  151. uint8_t raster_end[64];
  152. } ScanTable;
  153. void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
  154. void ff_init_scantable_permutation(uint8_t *idct_permutation,
  155. int idct_permutation_type);
  156. /**
  157. * DSPContext.
  158. */
  159. typedef struct DSPContext {
  160. /**
  161. * Size of DCT coefficients.
  162. */
  163. int dct_bits;
  164. /* pixel ops : interface with DCT */
  165. void (*get_pixels)(int16_t *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  166. void (*diff_pixels)(int16_t *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  167. void (*put_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  168. void (*put_signed_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  169. void (*add_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  170. void (*add_pixels8)(uint8_t *pixels, int16_t *block, int line_size);
  171. void (*add_pixels4)(uint8_t *pixels, int16_t *block, int line_size);
  172. int (*sum_abs_dctelem)(int16_t *block/*align 16*/);
  173. /**
  174. * translational global motion compensation.
  175. */
  176. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  177. /**
  178. * global motion compensation.
  179. */
  180. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  181. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  182. void (*clear_block)(int16_t *block/*align 16*/);
  183. void (*clear_blocks)(int16_t *blocks/*align 16*/);
  184. int (*pix_sum)(uint8_t * pix, int line_size);
  185. int (*pix_norm1)(uint8_t * pix, int line_size);
  186. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  187. me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
  188. me_cmp_func sse[6];
  189. me_cmp_func hadamard8_diff[6];
  190. me_cmp_func dct_sad[6];
  191. me_cmp_func quant_psnr[6];
  192. me_cmp_func bit[6];
  193. me_cmp_func rd[6];
  194. me_cmp_func vsad[6];
  195. me_cmp_func vsse[6];
  196. me_cmp_func nsse[6];
  197. me_cmp_func dct_max[6];
  198. me_cmp_func dct264_sad[6];
  199. me_cmp_func me_pre_cmp[6];
  200. me_cmp_func me_cmp[6];
  201. me_cmp_func me_sub_cmp[6];
  202. me_cmp_func mb_cmp[6];
  203. me_cmp_func ildct_cmp[6]; //only width 16 used
  204. me_cmp_func frame_skip_cmp[6]; //only width 8 used
  205. int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
  206. int size);
  207. /**
  208. * Halfpel motion compensation with rounding (a+b+1)>>1.
  209. * this is an array[4][4] of motion compensation functions for 4
  210. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  211. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  212. * @param block destination where the result is stored
  213. * @param pixels source
  214. * @param line_size number of bytes in a horizontal line of block
  215. * @param h height
  216. */
  217. op_pixels_func put_pixels_tab[4][4];
  218. /**
  219. * Halfpel motion compensation with rounding (a+b+1)>>1.
  220. * This is an array[4][4] of motion compensation functions for 4
  221. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  222. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  223. * @param block destination into which the result is averaged (a+b+1)>>1
  224. * @param pixels source
  225. * @param line_size number of bytes in a horizontal line of block
  226. * @param h height
  227. */
  228. op_pixels_func avg_pixels_tab[4][4];
  229. /**
  230. * Halfpel motion compensation with no rounding (a+b)>>1.
  231. * this is an array[2][4] of motion compensation functions for 2
  232. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  233. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  234. * @param block destination where the result is stored
  235. * @param pixels source
  236. * @param line_size number of bytes in a horizontal line of block
  237. * @param h height
  238. */
  239. op_pixels_func put_no_rnd_pixels_tab[2][4];
  240. /**
  241. * Halfpel motion compensation with no rounding (a+b)>>1.
  242. * this is an array[4] of motion compensation functions for 1
  243. * horizontal blocksize (16) and the 4 halfpel positions<br>
  244. * *pixels_tab[0][ xhalfpel + 2*yhalfpel ]
  245. * @param block destination into which the result is averaged (a+b)>>1
  246. * @param pixels source
  247. * @param line_size number of bytes in a horizontal line of block
  248. * @param h height
  249. */
  250. op_pixels_func avg_no_rnd_pixels_tab[4];
  251. /**
  252. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  253. * this is an array[12] of motion compensation functions for the 9 thirdpe
  254. * positions<br>
  255. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  256. * @param block destination where the result is stored
  257. * @param pixels source
  258. * @param line_size number of bytes in a horizontal line of block
  259. * @param h height
  260. */
  261. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  262. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  263. qpel_mc_func put_qpel_pixels_tab[2][16];
  264. qpel_mc_func avg_qpel_pixels_tab[2][16];
  265. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  266. qpel_mc_func put_mspel_pixels_tab[8];
  267. /**
  268. * h264 Chroma MC
  269. */
  270. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  271. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  272. me_cmp_func pix_abs[2][4];
  273. /* huffyuv specific */
  274. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  275. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  276. /**
  277. * subtract huffyuv's variant of median prediction
  278. * note, this might read from src1[-1], src2[-1]
  279. */
  280. void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
  281. void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
  282. int (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
  283. void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
  284. void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
  285. void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
  286. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  287. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  288. void (*h261_loop_filter)(uint8_t *src, int stride);
  289. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  290. void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
  291. /* (I)DCT */
  292. void (*fdct)(int16_t *block/* align 16*/);
  293. void (*fdct248)(int16_t *block/* align 16*/);
  294. /* IDCT really*/
  295. void (*idct)(int16_t *block/* align 16*/);
  296. /**
  297. * block -> idct -> clip to unsigned 8 bit -> dest.
  298. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  299. * @param line_size size in bytes of a horizontal line of dest
  300. */
  301. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
  302. /**
  303. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  304. * @param line_size size in bytes of a horizontal line of dest
  305. */
  306. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
  307. /**
  308. * idct input permutation.
  309. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  310. * IDCT)
  311. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  312. * with the zigzag/alternate scan<br>
  313. * an example to avoid confusion:
  314. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  315. * - (x -> reference dct -> reference idct -> x)
  316. * - (x -> reference dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  317. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  318. */
  319. uint8_t idct_permutation[64];
  320. int idct_permutation_type;
  321. #define FF_NO_IDCT_PERM 1
  322. #define FF_LIBMPEG2_IDCT_PERM 2
  323. #define FF_SIMPLE_IDCT_PERM 3
  324. #define FF_TRANSPOSE_IDCT_PERM 4
  325. #define FF_PARTTRANS_IDCT_PERM 5
  326. #define FF_SSE2_IDCT_PERM 6
  327. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
  328. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  329. #define BASIS_SHIFT 16
  330. #define RECON_SHIFT 6
  331. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides);
  332. #define EDGE_WIDTH 16
  333. #define EDGE_TOP 1
  334. #define EDGE_BOTTOM 2
  335. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  336. /**
  337. * Calculate scalar product of two vectors.
  338. * @param len length of vectors, should be multiple of 16
  339. */
  340. int32_t (*scalarproduct_int16)(const int16_t *v1, const int16_t *v2/*align 16*/, int len);
  341. /* ape functions */
  342. /**
  343. * Calculate scalar product of v1 and v2,
  344. * and v1[i] += v3[i] * mul
  345. * @param len length of vectors, should be multiple of 16
  346. */
  347. int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, const int16_t *v2, const int16_t *v3, int len, int mul);
  348. /**
  349. * Apply symmetric window in 16-bit fixed-point.
  350. * @param output destination array
  351. * constraints: 16-byte aligned
  352. * @param input source array
  353. * constraints: 16-byte aligned
  354. * @param window window array
  355. * constraints: 16-byte aligned, at least len/2 elements
  356. * @param len full window length
  357. * constraints: multiple of ? greater than zero
  358. */
  359. void (*apply_window_int16)(int16_t *output, const int16_t *input,
  360. const int16_t *window, unsigned int len);
  361. /**
  362. * Clip each element in an array of int32_t to a given minimum and maximum value.
  363. * @param dst destination array
  364. * constraints: 16-byte aligned
  365. * @param src source array
  366. * constraints: 16-byte aligned
  367. * @param min minimum value
  368. * constraints: must be in the range [-(1 << 24), 1 << 24]
  369. * @param max maximum value
  370. * constraints: must be in the range [-(1 << 24), 1 << 24]
  371. * @param len number of elements in the array
  372. * constraints: multiple of 32 greater than zero
  373. */
  374. void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
  375. int32_t max, unsigned int len);
  376. op_fill_func fill_block_tab[2];
  377. } DSPContext;
  378. void ff_dsputil_static_init(void);
  379. void ff_dsputil_init(DSPContext* p, AVCodecContext *avctx);
  380. int ff_check_alignment(void);
  381. /**
  382. * permute block according to permuatation.
  383. * @param last last non zero element in scantable order
  384. */
  385. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last);
  386. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  387. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  388. #define BYTE_VEC64(c) ((c)*0x0001000100010001UL)
  389. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  390. {
  391. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  392. }
  393. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  394. {
  395. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  396. }
  397. static inline uint64_t rnd_avg64(uint64_t a, uint64_t b)
  398. {
  399. return (a | b) - (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
  400. }
  401. static inline uint64_t no_rnd_avg64(uint64_t a, uint64_t b)
  402. {
  403. return (a & b) + (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
  404. }
  405. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  406. switch(type&0xFF){
  407. default:
  408. case FF_CMP_SAD:
  409. return lambda>>FF_LAMBDA_SHIFT;
  410. case FF_CMP_DCT:
  411. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  412. case FF_CMP_SATD:
  413. case FF_CMP_DCT264:
  414. return (2*lambda)>>FF_LAMBDA_SHIFT;
  415. case FF_CMP_RD:
  416. case FF_CMP_PSNR:
  417. case FF_CMP_SSE:
  418. case FF_CMP_NSSE:
  419. return lambda2>>FF_LAMBDA_SHIFT;
  420. case FF_CMP_BIT:
  421. return 1;
  422. }
  423. }
  424. void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  425. void ff_dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
  426. void ff_dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
  427. void ff_dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  428. void ff_dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  429. void ff_dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  430. void ff_dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
  431. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  432. # define STRIDE_ALIGN 16
  433. #else
  434. # define STRIDE_ALIGN 8
  435. #endif
  436. // Some broken preprocessors need a second expansion
  437. // to be forced to tokenize __VA_ARGS__
  438. #define E(x) x
  439. #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
  440. uint8_t la_##v[sizeof(t s o) + (a)]; \
  441. t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
  442. #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) \
  443. DECLARE_ALIGNED(a, t, la_##v) s o; \
  444. t (*v) o = la_##v
  445. #define LOCAL_ALIGNED(a, t, v, ...) E(LOCAL_ALIGNED_A(a, t, v, __VA_ARGS__,,))
  446. #if HAVE_LOCAL_ALIGNED_8
  447. # define LOCAL_ALIGNED_8(t, v, ...) E(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
  448. #else
  449. # define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED(8, t, v, __VA_ARGS__)
  450. #endif
  451. #if HAVE_LOCAL_ALIGNED_16
  452. # define LOCAL_ALIGNED_16(t, v, ...) E(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
  453. #else
  454. # define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED(16, t, v, __VA_ARGS__)
  455. #endif
  456. #define WRAPPER8_16_SQ(name8, name16)\
  457. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  458. int score=0;\
  459. score +=name8(s, dst , src , stride, 8);\
  460. score +=name8(s, dst+8 , src+8 , stride, 8);\
  461. if(h==16){\
  462. dst += 8*stride;\
  463. src += 8*stride;\
  464. score +=name8(s, dst , src , stride, 8);\
  465. score +=name8(s, dst+8 , src+8 , stride, 8);\
  466. }\
  467. return score;\
  468. }
  469. static inline void copy_block2(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  470. {
  471. int i;
  472. for(i=0; i<h; i++)
  473. {
  474. AV_COPY16U(dst, src);
  475. dst+=dstStride;
  476. src+=srcStride;
  477. }
  478. }
  479. static inline void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  480. {
  481. int i;
  482. for(i=0; i<h; i++)
  483. {
  484. AV_COPY32U(dst, src);
  485. dst+=dstStride;
  486. src+=srcStride;
  487. }
  488. }
  489. static inline void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  490. {
  491. int i;
  492. for(i=0; i<h; i++)
  493. {
  494. AV_COPY64U(dst, src);
  495. dst+=dstStride;
  496. src+=srcStride;
  497. }
  498. }
  499. static inline void copy_block9(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  500. {
  501. int i;
  502. for(i=0; i<h; i++)
  503. {
  504. AV_COPY64U(dst, src);
  505. dst[8]= src[8];
  506. dst+=dstStride;
  507. src+=srcStride;
  508. }
  509. }
  510. static inline void copy_block16(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  511. {
  512. int i;
  513. for(i=0; i<h; i++)
  514. {
  515. AV_COPY128U(dst, src);
  516. dst+=dstStride;
  517. src+=srcStride;
  518. }
  519. }
  520. static inline void copy_block17(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
  521. {
  522. int i;
  523. for(i=0; i<h; i++)
  524. {
  525. AV_COPY128U(dst, src);
  526. dst[16]= src[16];
  527. dst+=dstStride;
  528. src+=srcStride;
  529. }
  530. }
  531. #endif /* AVCODEC_DSPUTIL_H */