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.

763 lines
28KB

  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 dsputil.h
  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 DSPUTIL_H
  29. #define DSPUTIL_H
  30. #include "avcodec.h"
  31. //#define DEBUG
  32. /* dct code */
  33. typedef short DCTELEM;
  34. typedef int DWTELEM;
  35. void fdct_ifast (DCTELEM *data);
  36. void fdct_ifast248 (DCTELEM *data);
  37. void ff_jpeg_fdct_islow (DCTELEM *data);
  38. void ff_fdct248_islow (DCTELEM *data);
  39. void j_rev_dct (DCTELEM *data);
  40. void j_rev_dct4 (DCTELEM *data);
  41. void j_rev_dct2 (DCTELEM *data);
  42. void j_rev_dct1 (DCTELEM *data);
  43. void ff_fdct_mmx(DCTELEM *block);
  44. void ff_fdct_mmx2(DCTELEM *block);
  45. void ff_fdct_sse2(DCTELEM *block);
  46. void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride);
  47. void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride);
  48. void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
  49. void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
  50. void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block);
  51. void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block);
  52. void ff_vector_fmul_add_add_c(float *dst, const float *src0, const float *src1,
  53. const float *src2, int src3, int blocksize, int step);
  54. void ff_float_to_int16_c(int16_t *dst, const float *src, int len);
  55. /* encoding scans */
  56. extern const uint8_t ff_alternate_horizontal_scan[64];
  57. extern const uint8_t ff_alternate_vertical_scan[64];
  58. extern const uint8_t ff_zigzag_direct[64];
  59. extern const uint8_t ff_zigzag248_direct[64];
  60. /* pixel operations */
  61. #define MAX_NEG_CROP 1024
  62. /* temporary */
  63. extern uint32_t ff_squareTbl[512];
  64. extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
  65. /* VP3 DSP functions */
  66. void ff_vp3_idct_c(DCTELEM *block/* align 16*/);
  67. void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  68. void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  69. /* 1/2^n downscaling functions from imgconvert.c */
  70. void ff_img_copy_plane(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  71. void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  72. void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  73. void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  74. void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
  75. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  76. /* minimum alignment rules ;)
  77. if u notice errors in the align stuff, need more alignment for some asm code for some cpu
  78. or need to use a function with less aligned data then send a mail to the ffmpeg-dev list, ...
  79. !warning these alignments might not match reallity, (missing attribute((align)) stuff somewhere possible)
  80. i (michael) didnt check them, these are just the alignents which i think could be reached easily ...
  81. !future video codecs might need functions with less strict alignment
  82. */
  83. /*
  84. void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
  85. void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
  86. void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  87. void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  88. void clear_blocks_c(DCTELEM *blocks);
  89. */
  90. /* add and put pixel (decoding) */
  91. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  92. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4
  93. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  94. 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);
  95. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  96. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  97. typedef void (*h264_weight_func)(uint8_t *block, int stride, int log2_denom, int weight, int offset);
  98. typedef void (*h264_biweight_func)(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset);
  99. #define DEF_OLD_QPEL(name)\
  100. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  101. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  102. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  103. DEF_OLD_QPEL(qpel16_mc11_old_c)
  104. DEF_OLD_QPEL(qpel16_mc31_old_c)
  105. DEF_OLD_QPEL(qpel16_mc12_old_c)
  106. DEF_OLD_QPEL(qpel16_mc32_old_c)
  107. DEF_OLD_QPEL(qpel16_mc13_old_c)
  108. DEF_OLD_QPEL(qpel16_mc33_old_c)
  109. DEF_OLD_QPEL(qpel8_mc11_old_c)
  110. DEF_OLD_QPEL(qpel8_mc31_old_c)
  111. DEF_OLD_QPEL(qpel8_mc12_old_c)
  112. DEF_OLD_QPEL(qpel8_mc32_old_c)
  113. DEF_OLD_QPEL(qpel8_mc13_old_c)
  114. DEF_OLD_QPEL(qpel8_mc33_old_c)
  115. #define CALL_2X_PIXELS(a, b, n)\
  116. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  117. b(block , pixels , line_size, h);\
  118. b(block+n, pixels+n, line_size, h);\
  119. }
  120. /* motion estimation */
  121. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2
  122. // allthough currently h<4 is not used as functions with width <8 are not used and neither implemented
  123. 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))*/;
  124. // for snow slices
  125. typedef struct slice_buffer_s slice_buffer;
  126. /**
  127. * DSPContext.
  128. */
  129. typedef struct DSPContext {
  130. /* pixel ops : interface with DCT */
  131. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  132. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  133. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  134. void (*put_signed_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  135. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  136. void (*add_pixels8)(uint8_t *pixels, DCTELEM *block, int line_size);
  137. void (*add_pixels4)(uint8_t *pixels, DCTELEM *block, int line_size);
  138. int (*sum_abs_dctelem)(DCTELEM *block/*align 16*/);
  139. /**
  140. * translational global motion compensation.
  141. */
  142. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  143. /**
  144. * global motion compensation.
  145. */
  146. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  147. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  148. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  149. int (*pix_sum)(uint8_t * pix, int line_size);
  150. int (*pix_norm1)(uint8_t * pix, int line_size);
  151. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  152. me_cmp_func sad[5]; /* identical to pix_absAxA except additional void * */
  153. me_cmp_func sse[5];
  154. me_cmp_func hadamard8_diff[5];
  155. me_cmp_func dct_sad[5];
  156. me_cmp_func quant_psnr[5];
  157. me_cmp_func bit[5];
  158. me_cmp_func rd[5];
  159. me_cmp_func vsad[5];
  160. me_cmp_func vsse[5];
  161. me_cmp_func nsse[5];
  162. me_cmp_func w53[5];
  163. me_cmp_func w97[5];
  164. me_cmp_func dct_max[5];
  165. me_cmp_func dct264_sad[5];
  166. me_cmp_func me_pre_cmp[5];
  167. me_cmp_func me_cmp[5];
  168. me_cmp_func me_sub_cmp[5];
  169. me_cmp_func mb_cmp[5];
  170. me_cmp_func ildct_cmp[5]; //only width 16 used
  171. me_cmp_func frame_skip_cmp[5]; //only width 8 used
  172. int (*ssd_int8_vs_int16)(int8_t *pix1, int16_t *pix2, int size);
  173. /**
  174. * Halfpel motion compensation with rounding (a+b+1)>>1.
  175. * this is an array[4][4] of motion compensation functions for 4
  176. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  177. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  178. * @param block destination where the result is stored
  179. * @param pixels source
  180. * @param line_size number of bytes in a horizontal line of block
  181. * @param h height
  182. */
  183. op_pixels_func put_pixels_tab[4][4];
  184. /**
  185. * Halfpel motion compensation with rounding (a+b+1)>>1.
  186. * This is an array[4][4] of motion compensation functions for 4
  187. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  188. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  189. * @param block destination into which the result is averaged (a+b+1)>>1
  190. * @param pixels source
  191. * @param line_size number of bytes in a horizontal line of block
  192. * @param h height
  193. */
  194. op_pixels_func avg_pixels_tab[4][4];
  195. /**
  196. * Halfpel motion compensation with no rounding (a+b)>>1.
  197. * this is an array[2][4] of motion compensation functions for 2
  198. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  199. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  200. * @param block destination where the result is stored
  201. * @param pixels source
  202. * @param line_size number of bytes in a horizontal line of block
  203. * @param h height
  204. */
  205. op_pixels_func put_no_rnd_pixels_tab[4][4];
  206. /**
  207. * Halfpel motion compensation with no rounding (a+b)>>1.
  208. * this is an array[2][4] of motion compensation functions for 2
  209. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  210. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  211. * @param block destination into which the result is averaged (a+b)>>1
  212. * @param pixels source
  213. * @param line_size number of bytes in a horizontal line of block
  214. * @param h height
  215. */
  216. op_pixels_func avg_no_rnd_pixels_tab[4][4];
  217. 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);
  218. /**
  219. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  220. * this is an array[12] of motion compensation functions for the 9 thirdpe
  221. * positions<br>
  222. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  223. * @param block destination where the result is stored
  224. * @param pixels source
  225. * @param line_size number of bytes in a horizontal line of block
  226. * @param h height
  227. */
  228. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  229. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  230. qpel_mc_func put_qpel_pixels_tab[2][16];
  231. qpel_mc_func avg_qpel_pixels_tab[2][16];
  232. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  233. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  234. qpel_mc_func put_mspel_pixels_tab[8];
  235. /**
  236. * h264 Chroma MC
  237. */
  238. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  239. /* This is really one func used in VC-1 decoding */
  240. h264_chroma_mc_func put_no_rnd_h264_chroma_pixels_tab[3];
  241. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  242. qpel_mc_func put_h264_qpel_pixels_tab[4][16];
  243. qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
  244. qpel_mc_func put_2tap_qpel_pixels_tab[4][16];
  245. qpel_mc_func avg_2tap_qpel_pixels_tab[4][16];
  246. h264_weight_func weight_h264_pixels_tab[10];
  247. h264_biweight_func biweight_h264_pixels_tab[10];
  248. /* AVS specific */
  249. qpel_mc_func put_cavs_qpel_pixels_tab[2][16];
  250. qpel_mc_func avg_cavs_qpel_pixels_tab[2][16];
  251. void (*cavs_filter_lv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  252. void (*cavs_filter_lh)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  253. void (*cavs_filter_cv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  254. void (*cavs_filter_ch)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  255. void (*cavs_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
  256. me_cmp_func pix_abs[2][4];
  257. /* huffyuv specific */
  258. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  259. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  260. /**
  261. * subtract huffyuv's variant of median prediction
  262. * note, this might read from src1[-1], src2[-1]
  263. */
  264. void (*sub_hfyu_median_prediction)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top);
  265. void (*bswap_buf)(uint32_t *dst, uint32_t *src, int w);
  266. void (*h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  267. void (*h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  268. void (*h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  269. void (*h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  270. void (*h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
  271. void (*h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
  272. // h264_loop_filter_strength: simd only. the C version is inlined in h264.c
  273. void (*h264_loop_filter_strength)(int16_t bS[2][4][4], uint8_t nnz[40], int8_t ref[2][40], int16_t mv[2][40][2],
  274. int bidir, int edges, int step, int mask_mv0, int mask_mv1);
  275. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  276. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  277. void (*h261_loop_filter)(uint8_t *src, int stride);
  278. /* assume len is a multiple of 4, and arrays are 16-byte aligned */
  279. void (*vorbis_inverse_coupling)(float *mag, float *ang, int blocksize);
  280. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  281. void (*vector_fmul)(float *dst, const float *src, int len);
  282. void (*vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len);
  283. /* assume len is a multiple of 8, and src arrays are 16-byte aligned */
  284. void (*vector_fmul_add_add)(float *dst, const float *src0, const float *src1, const float *src2, int src3, int len, int step);
  285. /* C version: convert floats from the range [384.0,386.0] to ints in [-32768,32767]
  286. * simd versions: convert floats from [-32768.0,32767.0] without rescaling and arrays are 16byte aligned */
  287. void (*float_to_int16)(int16_t *dst, const float *src, int len);
  288. /* (I)DCT */
  289. void (*fdct)(DCTELEM *block/* align 16*/);
  290. void (*fdct248)(DCTELEM *block/* align 16*/);
  291. /* IDCT really*/
  292. void (*idct)(DCTELEM *block/* align 16*/);
  293. /**
  294. * block -> idct -> clip to unsigned 8 bit -> dest.
  295. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  296. * @param line_size size in bytes of a horizotal line of dest
  297. */
  298. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  299. /**
  300. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  301. * @param line_size size in bytes of a horizotal line of dest
  302. */
  303. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  304. /**
  305. * idct input permutation.
  306. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  307. * IDCT)
  308. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  309. * with the zigzag/alternate scan<br>
  310. * an example to avoid confusion:
  311. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  312. * - (x -> referece dct -> reference idct -> x)
  313. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  314. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  315. */
  316. uint8_t idct_permutation[64];
  317. int idct_permutation_type;
  318. #define FF_NO_IDCT_PERM 1
  319. #define FF_LIBMPEG2_IDCT_PERM 2
  320. #define FF_SIMPLE_IDCT_PERM 3
  321. #define FF_TRANSPOSE_IDCT_PERM 4
  322. #define FF_PARTTRANS_IDCT_PERM 5
  323. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
  324. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  325. #define BASIS_SHIFT 16
  326. #define RECON_SHIFT 6
  327. /* h264 functions */
  328. void (*h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  329. void (*h264_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
  330. void (*h264_idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  331. void (*h264_idct8_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  332. void (*h264_dct)(DCTELEM block[4][4]);
  333. /* snow wavelet */
  334. void (*vertical_compose97i)(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
  335. void (*horizontal_compose97i)(DWTELEM *b, int width);
  336. void (*inner_add_yblock)(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
  337. void (*prefetch)(void *mem, int stride, int h);
  338. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  339. /* vc1 functions */
  340. void (*vc1_inv_trans_8x8)(DCTELEM *b);
  341. void (*vc1_inv_trans_8x4)(DCTELEM *b, int n);
  342. void (*vc1_inv_trans_4x8)(DCTELEM *b, int n);
  343. void (*vc1_inv_trans_4x4)(DCTELEM *b, int n);
  344. void (*vc1_v_overlap)(uint8_t* src, int stride);
  345. void (*vc1_h_overlap)(uint8_t* src, int stride);
  346. /* put 8x8 block with bicubic interpolation and quarterpel precision
  347. * last argument is actually round value instead of height
  348. */
  349. op_pixels_func put_vc1_mspel_pixels_tab[16];
  350. } DSPContext;
  351. void dsputil_static_init(void);
  352. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  353. int ff_check_alignment(void);
  354. /**
  355. * permute block according to permuatation.
  356. * @param last last non zero element in scantable order
  357. */
  358. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  359. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  360. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  361. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  362. {
  363. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  364. }
  365. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  366. {
  367. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  368. }
  369. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  370. switch(type&0xFF){
  371. default:
  372. case FF_CMP_SAD:
  373. return lambda>>FF_LAMBDA_SHIFT;
  374. case FF_CMP_DCT:
  375. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  376. case FF_CMP_W53:
  377. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  378. case FF_CMP_W97:
  379. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  380. case FF_CMP_SATD:
  381. case FF_CMP_DCT264:
  382. return (2*lambda)>>FF_LAMBDA_SHIFT;
  383. case FF_CMP_RD:
  384. case FF_CMP_PSNR:
  385. case FF_CMP_SSE:
  386. case FF_CMP_NSSE:
  387. return lambda2>>FF_LAMBDA_SHIFT;
  388. case FF_CMP_BIT:
  389. return 1;
  390. }
  391. }
  392. /**
  393. * Empty mmx state.
  394. * this must be called between any dsp function and float/double code.
  395. * for example sin(); dsp->idct_put(); emms_c(); cos()
  396. */
  397. #define emms_c()
  398. /* should be defined by architectures supporting
  399. one or more MultiMedia extension */
  400. int mm_support(void);
  401. #ifdef __GNUC__
  402. #define DECLARE_ALIGNED_16(t,v) t v __attribute__ ((aligned (16)))
  403. #else
  404. #define DECLARE_ALIGNED_16(t,v) __declspec(align(16)) t v
  405. #endif
  406. #if defined(HAVE_MMX)
  407. #undef emms_c
  408. #define MM_MMX 0x0001 /* standard MMX */
  409. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  410. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  411. #define MM_SSE 0x0008 /* SSE functions */
  412. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  413. #define MM_3DNOWEXT 0x0020 /* AMD 3DNowExt */
  414. #define MM_SSE3 0x0040 /* Prescott SSE3 functions */
  415. #define MM_SSSE3 0x0080 /* Conroe SSSE3 functions */
  416. extern int mm_flags;
  417. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  418. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  419. void put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  420. static inline void emms(void)
  421. {
  422. __asm __volatile ("emms;":::"memory");
  423. }
  424. #define emms_c() \
  425. {\
  426. if (mm_flags & MM_MMX)\
  427. emms();\
  428. }
  429. #ifdef __GNUC__
  430. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  431. #else
  432. #define DECLARE_ALIGNED_8(t,v) __declspec(align(8)) t v
  433. #endif
  434. #define STRIDE_ALIGN 8
  435. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  436. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  437. #elif defined(ARCH_ARMV4L)
  438. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  439. line optimizations */
  440. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (4)))
  441. #define STRIDE_ALIGN 4
  442. #define MM_IWMMXT 0x0100 /* XScale IWMMXT */
  443. extern int mm_flags;
  444. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  445. #elif defined(HAVE_MLIB)
  446. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  447. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  448. #define STRIDE_ALIGN 8
  449. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  450. #elif defined(ARCH_SPARC)
  451. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  452. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  453. #define STRIDE_ALIGN 8
  454. void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
  455. #elif defined(ARCH_ALPHA)
  456. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  457. #define STRIDE_ALIGN 8
  458. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  459. #elif defined(ARCH_POWERPC)
  460. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  461. extern int mm_flags;
  462. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  463. #define pixel altivec_pixel
  464. #include <altivec.h>
  465. #undef pixel
  466. #endif
  467. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (16)))
  468. #define STRIDE_ALIGN 16
  469. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  470. #elif defined(HAVE_MMI)
  471. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (16)))
  472. #define STRIDE_ALIGN 16
  473. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  474. #elif defined(ARCH_SH4)
  475. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  476. #define STRIDE_ALIGN 8
  477. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  478. #elif defined(ARCH_BFIN)
  479. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  480. #define STRIDE_ALIGN 8
  481. void dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
  482. #else
  483. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  484. #define STRIDE_ALIGN 8
  485. #endif
  486. /* PSNR */
  487. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  488. int orig_linesize[3], int coded_linesize,
  489. AVCodecContext *avctx);
  490. /* FFT computation */
  491. /* NOTE: soon integer code will be added, so you must use the
  492. FFTSample type */
  493. typedef float FFTSample;
  494. struct MDCTContext;
  495. typedef struct FFTComplex {
  496. FFTSample re, im;
  497. } FFTComplex;
  498. typedef struct FFTContext {
  499. int nbits;
  500. int inverse;
  501. uint16_t *revtab;
  502. FFTComplex *exptab;
  503. FFTComplex *exptab1; /* only used by SSE code */
  504. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  505. void (*imdct_calc)(struct MDCTContext *s, FFTSample *output,
  506. const FFTSample *input, FFTSample *tmp);
  507. } FFTContext;
  508. int ff_fft_init(FFTContext *s, int nbits, int inverse);
  509. void ff_fft_permute(FFTContext *s, FFTComplex *z);
  510. void ff_fft_calc_c(FFTContext *s, FFTComplex *z);
  511. void ff_fft_calc_sse(FFTContext *s, FFTComplex *z);
  512. void ff_fft_calc_3dn(FFTContext *s, FFTComplex *z);
  513. void ff_fft_calc_3dn2(FFTContext *s, FFTComplex *z);
  514. void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z);
  515. static inline void ff_fft_calc(FFTContext *s, FFTComplex *z)
  516. {
  517. s->fft_calc(s, z);
  518. }
  519. void ff_fft_end(FFTContext *s);
  520. /* MDCT computation */
  521. typedef struct MDCTContext {
  522. int n; /* size of MDCT (i.e. number of input data * 2) */
  523. int nbits; /* n = 2^nbits */
  524. /* pre/post rotation tables */
  525. FFTSample *tcos;
  526. FFTSample *tsin;
  527. FFTContext fft;
  528. } MDCTContext;
  529. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  530. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  531. const FFTSample *input, FFTSample *tmp);
  532. void ff_imdct_calc_3dn2(MDCTContext *s, FFTSample *output,
  533. const FFTSample *input, FFTSample *tmp);
  534. void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
  535. const FFTSample *input, FFTSample *tmp);
  536. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  537. const FFTSample *input, FFTSample *tmp);
  538. void ff_mdct_end(MDCTContext *s);
  539. #define WARPER8_16(name8, name16)\
  540. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  541. return name8(s, dst , src , stride, h)\
  542. +name8(s, dst+8 , src+8 , stride, h);\
  543. }
  544. #define WARPER8_16_SQ(name8, name16)\
  545. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  546. int score=0;\
  547. score +=name8(s, dst , src , stride, 8);\
  548. score +=name8(s, dst+8 , src+8 , stride, 8);\
  549. if(h==16){\
  550. dst += 8*stride;\
  551. src += 8*stride;\
  552. score +=name8(s, dst , src , stride, 8);\
  553. score +=name8(s, dst+8 , src+8 , stride, 8);\
  554. }\
  555. return score;\
  556. }
  557. static inline void copy_block2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  558. {
  559. int i;
  560. for(i=0; i<h; i++)
  561. {
  562. ST16(dst , LD16(src ));
  563. dst+=dstStride;
  564. src+=srcStride;
  565. }
  566. }
  567. static inline void copy_block4(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  568. {
  569. int i;
  570. for(i=0; i<h; i++)
  571. {
  572. ST32(dst , LD32(src ));
  573. dst+=dstStride;
  574. src+=srcStride;
  575. }
  576. }
  577. static inline void copy_block8(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  578. {
  579. int i;
  580. for(i=0; i<h; i++)
  581. {
  582. ST32(dst , LD32(src ));
  583. ST32(dst+4 , LD32(src+4 ));
  584. dst+=dstStride;
  585. src+=srcStride;
  586. }
  587. }
  588. static inline void copy_block9(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  589. {
  590. int i;
  591. for(i=0; i<h; i++)
  592. {
  593. ST32(dst , LD32(src ));
  594. ST32(dst+4 , LD32(src+4 ));
  595. dst[8]= src[8];
  596. dst+=dstStride;
  597. src+=srcStride;
  598. }
  599. }
  600. static inline void copy_block16(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  601. {
  602. int i;
  603. for(i=0; i<h; i++)
  604. {
  605. ST32(dst , LD32(src ));
  606. ST32(dst+4 , LD32(src+4 ));
  607. ST32(dst+8 , LD32(src+8 ));
  608. ST32(dst+12, LD32(src+12));
  609. dst+=dstStride;
  610. src+=srcStride;
  611. }
  612. }
  613. static inline void copy_block17(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
  614. {
  615. int i;
  616. for(i=0; i<h; i++)
  617. {
  618. ST32(dst , LD32(src ));
  619. ST32(dst+4 , LD32(src+4 ));
  620. ST32(dst+8 , LD32(src+8 ));
  621. ST32(dst+12, LD32(src+12));
  622. dst[16]= src[16];
  623. dst+=dstStride;
  624. src+=srcStride;
  625. }
  626. }
  627. #endif