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.

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