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.

683 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. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  236. qpel_mc_func put_h264_qpel_pixels_tab[4][16];
  237. qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
  238. h264_weight_func weight_h264_pixels_tab[10];
  239. h264_biweight_func biweight_h264_pixels_tab[10];
  240. /* AVS specific */
  241. qpel_mc_func put_cavs_qpel_pixels_tab[2][16];
  242. qpel_mc_func avg_cavs_qpel_pixels_tab[2][16];
  243. void (*cavs_filter_lv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  244. void (*cavs_filter_lh)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  245. void (*cavs_filter_cv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  246. void (*cavs_filter_ch)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
  247. void (*cavs_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
  248. me_cmp_func pix_abs[2][4];
  249. /* huffyuv specific */
  250. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  251. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  252. /**
  253. * subtract huffyuv's variant of median prediction
  254. * note, this might read from src1[-1], src2[-1]
  255. */
  256. void (*sub_hfyu_median_prediction)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top);
  257. void (*bswap_buf)(uint32_t *dst, uint32_t *src, int w);
  258. void (*h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  259. void (*h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  260. void (*h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  261. void (*h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
  262. void (*h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
  263. void (*h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
  264. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  265. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  266. void (*h261_loop_filter)(uint8_t *src, int stride);
  267. /* assume len is a multiple of 4, and arrays are 16-byte aligned */
  268. void (*vorbis_inverse_coupling)(float *mag, float *ang, int blocksize);
  269. void (*vector_fmul)(float *dst, const float *src, int len);
  270. /* assume len is a multiple of 8, and arrays are 16-byte aligned */
  271. void (*vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len);
  272. /* assume len is a multiple of 8, and src arrays are 16-byte aligned */
  273. void (*vector_fmul_add_add)(float *dst, const float *src0, const float *src1, const float *src2, int src3, int len, int step);
  274. /* C version: convert floats from the range [384.0,386.0] to ints in [-32768,32767]
  275. * asm versions: convert floats from [-32768.0,32767.0] without rescaling */
  276. void (*float_to_int16)(int16_t *dst, const float *src, int len);
  277. /* (I)DCT */
  278. void (*fdct)(DCTELEM *block/* align 16*/);
  279. void (*fdct248)(DCTELEM *block/* align 16*/);
  280. /* IDCT really*/
  281. void (*idct)(DCTELEM *block/* align 16*/);
  282. /**
  283. * block -> idct -> clip to unsigned 8 bit -> dest.
  284. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  285. * @param line_size size in bytes of a horizotal line of dest
  286. */
  287. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  288. /**
  289. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  290. * @param line_size size in bytes of a horizotal line of dest
  291. */
  292. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  293. /**
  294. * idct input permutation.
  295. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  296. * IDCT)
  297. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  298. * with the zigzag/alternate scan<br>
  299. * an example to avoid confusion:
  300. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  301. * - (x -> referece dct -> reference idct -> x)
  302. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  303. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  304. */
  305. uint8_t idct_permutation[64];
  306. int idct_permutation_type;
  307. #define FF_NO_IDCT_PERM 1
  308. #define FF_LIBMPEG2_IDCT_PERM 2
  309. #define FF_SIMPLE_IDCT_PERM 3
  310. #define FF_TRANSPOSE_IDCT_PERM 4
  311. #define FF_PARTTRANS_IDCT_PERM 5
  312. int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
  313. void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
  314. #define BASIS_SHIFT 16
  315. #define RECON_SHIFT 6
  316. void (*h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  317. void (*h264_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
  318. void (*h264_idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  319. void (*h264_idct8_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  320. /* snow wavelet */
  321. void (*vertical_compose97i)(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
  322. void (*horizontal_compose97i)(DWTELEM *b, int width);
  323. 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);
  324. void (*prefetch)(void *mem, int stride, int h);
  325. void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
  326. /* vc1 functions */
  327. void (*vc1_inv_trans_8x8)(DCTELEM *b);
  328. void (*vc1_inv_trans_8x4)(DCTELEM *b, int n);
  329. void (*vc1_inv_trans_4x8)(DCTELEM *b, int n);
  330. void (*vc1_inv_trans_4x4)(DCTELEM *b, int n);
  331. void (*vc1_v_overlap)(uint8_t* src, int stride, int rnd);
  332. void (*vc1_h_overlap)(uint8_t* src, int stride, int rnd);
  333. /* put 8x8 block with bicubic interpolation and quarterpel precision
  334. * last argument is actually round value instead of height
  335. */
  336. op_pixels_func put_vc1_mspel_pixels_tab[16];
  337. } DSPContext;
  338. void dsputil_static_init(void);
  339. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  340. /**
  341. * permute block according to permuatation.
  342. * @param last last non zero element in scantable order
  343. */
  344. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  345. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  346. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  347. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  348. {
  349. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  350. }
  351. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  352. {
  353. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  354. }
  355. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  356. switch(type&0xFF){
  357. default:
  358. case FF_CMP_SAD:
  359. return lambda>>FF_LAMBDA_SHIFT;
  360. case FF_CMP_DCT:
  361. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  362. case FF_CMP_W53:
  363. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  364. case FF_CMP_W97:
  365. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  366. case FF_CMP_SATD:
  367. case FF_CMP_DCT264:
  368. return (2*lambda)>>FF_LAMBDA_SHIFT;
  369. case FF_CMP_RD:
  370. case FF_CMP_PSNR:
  371. case FF_CMP_SSE:
  372. case FF_CMP_NSSE:
  373. return lambda2>>FF_LAMBDA_SHIFT;
  374. case FF_CMP_BIT:
  375. return 1;
  376. }
  377. }
  378. /**
  379. * Empty mmx state.
  380. * this must be called between any dsp function and float/double code.
  381. * for example sin(); dsp->idct_put(); emms_c(); cos()
  382. */
  383. #define emms_c()
  384. /* should be defined by architectures supporting
  385. one or more MultiMedia extension */
  386. int mm_support(void);
  387. #ifdef __GNUC__
  388. #define DECLARE_ALIGNED_16(t,v) t v __attribute__ ((aligned (16)))
  389. #else
  390. #define DECLARE_ALIGNED_16(t,v) __declspec(align(16)) t v
  391. #endif
  392. #if defined(HAVE_MMX)
  393. #undef emms_c
  394. #define MM_MMX 0x0001 /* standard MMX */
  395. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  396. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  397. #define MM_SSE 0x0008 /* SSE functions */
  398. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  399. #define MM_3DNOWEXT 0x0020 /* AMD 3DNowExt */
  400. #define MM_SSE3 0x0040 /* Prescott SSE3 functions */
  401. extern int mm_flags;
  402. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  403. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  404. void put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  405. static inline void emms(void)
  406. {
  407. __asm __volatile ("emms;":::"memory");
  408. }
  409. #define emms_c() \
  410. {\
  411. if (mm_flags & MM_MMX)\
  412. emms();\
  413. }
  414. #ifdef __GNUC__
  415. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  416. #else
  417. #define DECLARE_ALIGNED_8(t,v) __declspec(align(8)) t v
  418. #endif
  419. #define STRIDE_ALIGN 8
  420. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  421. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  422. #elif defined(ARCH_ARMV4L)
  423. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  424. line optimizations */
  425. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (4)))
  426. #define STRIDE_ALIGN 4
  427. #define MM_IWMMXT 0x0100 /* XScale IWMMXT */
  428. extern int mm_flags;
  429. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  430. #elif defined(HAVE_MLIB)
  431. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  432. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  433. #define STRIDE_ALIGN 8
  434. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  435. #elif defined(ARCH_SPARC)
  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_vis(DSPContext* c, AVCodecContext *avctx);
  440. #elif defined(ARCH_ALPHA)
  441. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  442. #define STRIDE_ALIGN 8
  443. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  444. #elif defined(ARCH_POWERPC)
  445. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  446. extern int mm_flags;
  447. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  448. #define pixel altivec_pixel
  449. #include <altivec.h>
  450. #undef pixel
  451. #endif
  452. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (16)))
  453. #define STRIDE_ALIGN 16
  454. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  455. #elif defined(HAVE_MMI)
  456. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (16)))
  457. #define STRIDE_ALIGN 16
  458. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  459. #elif defined(ARCH_SH4)
  460. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  461. #define STRIDE_ALIGN 8
  462. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  463. #else
  464. #define DECLARE_ALIGNED_8(t,v) t v __attribute__ ((aligned (8)))
  465. #define STRIDE_ALIGN 8
  466. #endif
  467. #ifdef __GNUC__
  468. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  469. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  470. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  471. #define LD16(a) (((const struct unaligned_16 *) (a))->l)
  472. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  473. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  474. #define ST16(a, b) (((struct unaligned_16 *) (a))->l) = (b)
  475. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  476. #else /* __GNUC__ */
  477. #define LD16(a) (*((uint16_t*)(a)))
  478. #define LD32(a) (*((uint32_t*)(a)))
  479. #define LD64(a) (*((uint64_t*)(a)))
  480. #define ST16(a, b) *((uint16_t*)(a)) = (b)
  481. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  482. #endif /* !__GNUC__ */
  483. /* PSNR */
  484. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  485. int orig_linesize[3], int coded_linesize,
  486. AVCodecContext *avctx);
  487. /* FFT computation */
  488. /* NOTE: soon integer code will be added, so you must use the
  489. FFTSample type */
  490. typedef float FFTSample;
  491. struct MDCTContext;
  492. typedef struct FFTComplex {
  493. FFTSample re, im;
  494. } FFTComplex;
  495. typedef struct FFTContext {
  496. int nbits;
  497. int inverse;
  498. uint16_t *revtab;
  499. FFTComplex *exptab;
  500. FFTComplex *exptab1; /* only used by SSE code */
  501. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  502. void (*imdct_calc)(struct MDCTContext *s, FFTSample *output,
  503. const FFTSample *input, FFTSample *tmp);
  504. } FFTContext;
  505. int ff_fft_init(FFTContext *s, int nbits, int inverse);
  506. void ff_fft_permute(FFTContext *s, FFTComplex *z);
  507. void ff_fft_calc_c(FFTContext *s, FFTComplex *z);
  508. void ff_fft_calc_sse(FFTContext *s, FFTComplex *z);
  509. void ff_fft_calc_3dn(FFTContext *s, FFTComplex *z);
  510. void ff_fft_calc_3dn2(FFTContext *s, FFTComplex *z);
  511. void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z);
  512. static inline void ff_fft_calc(FFTContext *s, FFTComplex *z)
  513. {
  514. s->fft_calc(s, z);
  515. }
  516. void ff_fft_end(FFTContext *s);
  517. /* MDCT computation */
  518. typedef struct MDCTContext {
  519. int n; /* size of MDCT (i.e. number of input data * 2) */
  520. int nbits; /* n = 2^nbits */
  521. /* pre/post rotation tables */
  522. FFTSample *tcos;
  523. FFTSample *tsin;
  524. FFTContext fft;
  525. } MDCTContext;
  526. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  527. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  528. const FFTSample *input, FFTSample *tmp);
  529. void ff_imdct_calc_3dn2(MDCTContext *s, FFTSample *output,
  530. const FFTSample *input, FFTSample *tmp);
  531. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  532. const FFTSample *input, FFTSample *tmp);
  533. void ff_mdct_end(MDCTContext *s);
  534. #define WARPER8_16(name8, name16)\
  535. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  536. return name8(s, dst , src , stride, h)\
  537. +name8(s, dst+8 , src+8 , stride, h);\
  538. }
  539. #define WARPER8_16_SQ(name8, name16)\
  540. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  541. int score=0;\
  542. score +=name8(s, dst , src , stride, 8);\
  543. score +=name8(s, dst+8 , src+8 , stride, 8);\
  544. if(h==16){\
  545. dst += 8*stride;\
  546. src += 8*stride;\
  547. score +=name8(s, dst , src , stride, 8);\
  548. score +=name8(s, dst+8 , src+8 , stride, 8);\
  549. }\
  550. return score;\
  551. }
  552. #endif