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.

443 lines
14KB

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