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.

502 lines
17KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file dsputil.h
  21. * DSP utils.
  22. * note, many functions in here may use MMX which trashes the FPU state, it is
  23. * absolutely necessary to call emms_c() between dsp & float/double code
  24. */
  25. #ifndef DSPUTIL_H
  26. #define DSPUTIL_H
  27. #include "common.h"
  28. #include "avcodec.h"
  29. //#define DEBUG
  30. /* dct code */
  31. typedef short DCTELEM;
  32. void fdct_ifast (DCTELEM *data);
  33. void ff_jpeg_fdct_islow (DCTELEM *data);
  34. void j_rev_dct (DCTELEM *data);
  35. void ff_fdct_mmx(DCTELEM *block);
  36. /* encoding scans */
  37. extern const uint8_t ff_alternate_horizontal_scan[64];
  38. extern const uint8_t ff_alternate_vertical_scan[64];
  39. extern const uint8_t ff_zigzag_direct[64];
  40. /* pixel operations */
  41. #define MAX_NEG_CROP 384
  42. /* temporary */
  43. extern uint32_t squareTbl[512];
  44. extern uint8_t cropTbl[256 + 2 * MAX_NEG_CROP];
  45. /* minimum alignment rules ;)
  46. if u notice errors in the align stuff, need more alignment for some asm code for some cpu
  47. or need to use a function with less aligned data then send a mail to the ffmpeg-dev list, ...
  48. !warning these alignments might not match reallity, (missing attribute((align)) stuff somewhere possible)
  49. i (michael) didnt check them, these are just the alignents which i think could be reached easily ...
  50. !future video codecs might need functions with less strict alignment
  51. */
  52. /*
  53. void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
  54. void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
  55. void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  56. void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  57. void clear_blocks_c(DCTELEM *blocks);
  58. */
  59. /* add and put pixel (decoding) */
  60. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  61. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  62. 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);
  63. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  64. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  65. #define DEF_OLD_QPEL(name)\
  66. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  67. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  68. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  69. DEF_OLD_QPEL(qpel16_mc11_old_c)
  70. DEF_OLD_QPEL(qpel16_mc31_old_c)
  71. DEF_OLD_QPEL(qpel16_mc12_old_c)
  72. DEF_OLD_QPEL(qpel16_mc32_old_c)
  73. DEF_OLD_QPEL(qpel16_mc13_old_c)
  74. DEF_OLD_QPEL(qpel16_mc33_old_c)
  75. DEF_OLD_QPEL(qpel8_mc11_old_c)
  76. DEF_OLD_QPEL(qpel8_mc31_old_c)
  77. DEF_OLD_QPEL(qpel8_mc12_old_c)
  78. DEF_OLD_QPEL(qpel8_mc32_old_c)
  79. DEF_OLD_QPEL(qpel8_mc13_old_c)
  80. DEF_OLD_QPEL(qpel8_mc33_old_c)
  81. #define CALL_2X_PIXELS(a, b, n)\
  82. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  83. b(block , pixels , line_size, h);\
  84. b(block+n, pixels+n, line_size, h);\
  85. }
  86. /* motion estimation */
  87. typedef int (*op_pixels_abs_func)(uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size)/* __attribute__ ((const))*/;
  88. typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size)/* __attribute__ ((const))*/;
  89. /**
  90. * DSPContext.
  91. */
  92. typedef struct DSPContext {
  93. /* pixel ops : interface with DCT */
  94. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  95. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  96. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  97. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  98. /**
  99. * translational global motion compensation.
  100. */
  101. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  102. /**
  103. * global motion compensation.
  104. */
  105. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  106. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  107. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  108. int (*pix_sum)(uint8_t * pix, int line_size);
  109. int (*pix_norm1)(uint8_t * pix, int line_size);
  110. me_cmp_func sad[2]; /* identical to pix_absAxA except additional void * */
  111. me_cmp_func sse[2];
  112. me_cmp_func hadamard8_diff[2];
  113. me_cmp_func dct_sad[2];
  114. me_cmp_func quant_psnr[2];
  115. me_cmp_func bit[2];
  116. me_cmp_func rd[2];
  117. int (*hadamard8_abs )(uint8_t *src, int stride, int mean);
  118. me_cmp_func me_pre_cmp[11];
  119. me_cmp_func me_cmp[11];
  120. me_cmp_func me_sub_cmp[11];
  121. me_cmp_func mb_cmp[11];
  122. /* maybe create an array for 16/8/4/2 functions */
  123. /**
  124. * Halfpel motion compensation with rounding (a+b+1)>>1.
  125. * this is an array[4][4] of motion compensation funcions for 4
  126. * horizontal blocksizes (2,4,8,16) and the 4 halfpel positions<br>
  127. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  128. * @param block destination where the result is stored
  129. * @param pixels source
  130. * @param line_size number of bytes in a horizontal line of block
  131. * @param h height
  132. */
  133. op_pixels_func put_pixels_tab[4][4];
  134. /**
  135. * Halfpel motion compensation with rounding (a+b+1)>>1.
  136. * This is an array[4][4] of motion compensation functions for 4
  137. * horizontal blocksizes (2,4,8,16) and the 4 halfpel positions<br>
  138. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  139. * @param block destination into which the result is averaged (a+b+1)>>1
  140. * @param pixels source
  141. * @param line_size number of bytes in a horizontal line of block
  142. * @param h height
  143. */
  144. op_pixels_func avg_pixels_tab[4][4];
  145. /**
  146. * Halfpel motion compensation with no rounding (a+b)>>1.
  147. * this is an array[2][4] of motion compensation funcions for 2
  148. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  149. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  150. * @param block destination where the result is stored
  151. * @param pixels source
  152. * @param line_size number of bytes in a horizontal line of block
  153. * @param h height
  154. */
  155. op_pixels_func put_no_rnd_pixels_tab[2][4];
  156. /**
  157. * Halfpel motion compensation with no rounding (a+b)>>1.
  158. * this is an array[2][4] of motion compensation funcions for 2
  159. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  160. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  161. * @param block destination into which the result is averaged (a+b)>>1
  162. * @param pixels source
  163. * @param line_size number of bytes in a horizontal line of block
  164. * @param h height
  165. */
  166. op_pixels_func avg_no_rnd_pixels_tab[2][4];
  167. /**
  168. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  169. * this is an array[12] of motion compensation funcions for the 9 thirdpel positions<br>
  170. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  171. * @param block destination where the result is stored
  172. * @param pixels source
  173. * @param line_size number of bytes in a horizontal line of block
  174. * @param h height
  175. */
  176. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  177. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  178. qpel_mc_func put_qpel_pixels_tab[2][16];
  179. qpel_mc_func avg_qpel_pixels_tab[2][16];
  180. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  181. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  182. qpel_mc_func put_mspel_pixels_tab[8];
  183. /**
  184. * h264 Chram MC
  185. */
  186. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  187. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  188. qpel_mc_func put_h264_qpel_pixels_tab[3][16];
  189. qpel_mc_func avg_h264_qpel_pixels_tab[3][16];
  190. op_pixels_abs_func pix_abs16x16;
  191. op_pixels_abs_func pix_abs16x16_x2;
  192. op_pixels_abs_func pix_abs16x16_y2;
  193. op_pixels_abs_func pix_abs16x16_xy2;
  194. op_pixels_abs_func pix_abs8x8;
  195. op_pixels_abs_func pix_abs8x8_x2;
  196. op_pixels_abs_func pix_abs8x8_y2;
  197. op_pixels_abs_func pix_abs8x8_xy2;
  198. /* huffyuv specific */
  199. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  200. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  201. void (*bswap_buf)(uint32_t *dst, uint32_t *src, int w);
  202. /* (I)DCT */
  203. void (*fdct)(DCTELEM *block/* align 16*/);
  204. /* IDCT really*/
  205. void (*idct)(DCTELEM *block/* align 16*/);
  206. /**
  207. * block -> idct -> clip to unsigned 8 bit -> dest.
  208. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  209. * @param line_size size in bytes of a horizotal line of dest
  210. */
  211. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  212. /**
  213. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  214. * @param line_size size in bytes of a horizotal line of dest
  215. */
  216. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  217. /**
  218. * idct input permutation.
  219. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  220. * IDCT)
  221. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  222. * with the zigzag/alternate scan<br>
  223. * an example to avoid confusion:
  224. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  225. * - (x -> referece dct -> reference idct -> x)
  226. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  227. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  228. */
  229. uint8_t idct_permutation[64];
  230. int idct_permutation_type;
  231. #define FF_NO_IDCT_PERM 1
  232. #define FF_LIBMPEG2_IDCT_PERM 2
  233. #define FF_SIMPLE_IDCT_PERM 3
  234. #define FF_TRANSPOSE_IDCT_PERM 4
  235. } DSPContext;
  236. void dsputil_static_init(void);
  237. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  238. /**
  239. * permute block according to permuatation.
  240. * @param last last non zero element in scantable order
  241. */
  242. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  243. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  244. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  245. {
  246. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  247. }
  248. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  249. {
  250. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  251. }
  252. /**
  253. * Empty mmx state.
  254. * this must be called between any dsp function and float/double code.
  255. * for example sin(); dsp->idct_put(); emms_c(); cos()
  256. */
  257. #define emms_c()
  258. /* should be defined by architectures supporting
  259. one or more MultiMedia extension */
  260. int mm_support(void);
  261. #if defined(HAVE_MMX)
  262. #undef emms_c
  263. #define MM_MMX 0x0001 /* standard MMX */
  264. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  265. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  266. #define MM_SSE 0x0008 /* SSE functions */
  267. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  268. extern int mm_flags;
  269. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  270. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  271. static inline void emms(void)
  272. {
  273. __asm __volatile ("emms;":::"memory");
  274. }
  275. #define emms_c() \
  276. {\
  277. if (mm_flags & MM_MMX)\
  278. emms();\
  279. }
  280. #define __align8 __attribute__ ((aligned (8)))
  281. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  282. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  283. #elif defined(ARCH_ARMV4L)
  284. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  285. line ptimizations */
  286. #define __align8 __attribute__ ((aligned (4)))
  287. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  288. #elif defined(HAVE_MLIB)
  289. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  290. #define __align8 __attribute__ ((aligned (8)))
  291. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  292. #elif defined(ARCH_ALPHA)
  293. #define __align8 __attribute__ ((aligned (8)))
  294. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  295. #elif defined(ARCH_POWERPC)
  296. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  297. extern int mm_flags;
  298. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  299. #include <altivec.h>
  300. #endif
  301. #define __align8 __attribute__ ((aligned (16)))
  302. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  303. #elif defined(HAVE_MMI)
  304. #define __align8 __attribute__ ((aligned (16)))
  305. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  306. #elif defined(ARCH_SH4)
  307. #define __align8 __attribute__ ((aligned (8)))
  308. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  309. #else
  310. #define __align8
  311. #endif
  312. #ifdef __GNUC__
  313. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  314. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  315. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  316. #define LD16(a) (((const struct unaligned_16 *) (a))->l)
  317. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  318. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  319. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  320. #else /* __GNUC__ */
  321. #define LD16(a) (*((uint16_t*)(a)))
  322. #define LD32(a) (*((uint32_t*)(a)))
  323. #define LD64(a) (*((uint64_t*)(a)))
  324. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  325. #endif /* !__GNUC__ */
  326. /* PSNR */
  327. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  328. int orig_linesize[3], int coded_linesize,
  329. AVCodecContext *avctx);
  330. /* FFT computation */
  331. /* NOTE: soon integer code will be added, so you must use the
  332. FFTSample type */
  333. typedef float FFTSample;
  334. typedef struct FFTComplex {
  335. FFTSample re, im;
  336. } FFTComplex;
  337. typedef struct FFTContext {
  338. int nbits;
  339. int inverse;
  340. uint16_t *revtab;
  341. FFTComplex *exptab;
  342. FFTComplex *exptab1; /* only used by SSE code */
  343. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  344. } FFTContext;
  345. int fft_init(FFTContext *s, int nbits, int inverse);
  346. void fft_permute(FFTContext *s, FFTComplex *z);
  347. void fft_calc_c(FFTContext *s, FFTComplex *z);
  348. void fft_calc_sse(FFTContext *s, FFTComplex *z);
  349. void fft_calc_altivec(FFTContext *s, FFTComplex *z);
  350. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  351. {
  352. s->fft_calc(s, z);
  353. }
  354. void fft_end(FFTContext *s);
  355. /* MDCT computation */
  356. typedef struct MDCTContext {
  357. int n; /* size of MDCT (i.e. number of input data * 2) */
  358. int nbits; /* n = 2^nbits */
  359. /* pre/post rotation tables */
  360. FFTSample *tcos;
  361. FFTSample *tsin;
  362. FFTContext fft;
  363. } MDCTContext;
  364. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  365. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  366. const FFTSample *input, FFTSample *tmp);
  367. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  368. const FFTSample *input, FFTSample *tmp);
  369. void ff_mdct_end(MDCTContext *s);
  370. #define WARPER88_1616(name8, name16)\
  371. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride){\
  372. return name8(s, dst , src , stride)\
  373. +name8(s, dst+8 , src+8 , stride)\
  374. +name8(s, dst +8*stride, src +8*stride, stride)\
  375. +name8(s, dst+8+8*stride, src+8+8*stride, stride);\
  376. }
  377. #ifndef HAVE_LRINTF
  378. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  379. /* better than nothing implementation. */
  380. /* btw, rintf() is existing on fbsd too -- alex */
  381. static inline long int lrintf(float x)
  382. {
  383. #ifdef CONFIG_WIN32
  384. /* XXX: incorrect, but make it compile */
  385. return (int)(x);
  386. #else
  387. return (int)(rint(x));
  388. #endif
  389. }
  390. #endif
  391. #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
  392. static inline float floorf(float f) {
  393. return floor(f);
  394. }
  395. #endif
  396. #endif