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.

493 lines
16KB

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