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.

688 lines
26KB

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