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.

444 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_static_init(void);
  209. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  210. /**
  211. * permute block according to permuatation.
  212. * @param last last non zero element in scantable order
  213. */
  214. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  215. /**
  216. * Empty mmx state.
  217. * this must be called between any dsp function and float/double code.
  218. * for example sin(); dsp->idct_put(); emms_c(); cos()
  219. */
  220. #define emms_c()
  221. /* should be defined by architectures supporting
  222. one or more MultiMedia extension */
  223. int mm_support(void);
  224. #if defined(HAVE_MMX)
  225. #undef emms_c
  226. #define MM_MMX 0x0001 /* standard MMX */
  227. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  228. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  229. #define MM_SSE 0x0008 /* SSE functions */
  230. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  231. extern int mm_flags;
  232. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  233. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  234. static inline void emms(void)
  235. {
  236. __asm __volatile ("emms;":::"memory");
  237. }
  238. #define emms_c() \
  239. {\
  240. if (mm_flags & MM_MMX)\
  241. emms();\
  242. }
  243. #define __align8 __attribute__ ((aligned (8)))
  244. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  245. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  246. #elif defined(ARCH_ARMV4L)
  247. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  248. line ptimizations */
  249. #define __align8 __attribute__ ((aligned (4)))
  250. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  251. #elif defined(HAVE_MLIB)
  252. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  253. #define __align8 __attribute__ ((aligned (8)))
  254. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  255. #elif defined(ARCH_ALPHA)
  256. #define __align8 __attribute__ ((aligned (8)))
  257. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  258. #elif defined(ARCH_POWERPC)
  259. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  260. extern int mm_flags;
  261. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  262. #include <altivec.h>
  263. #endif
  264. #define __align8 __attribute__ ((aligned (16)))
  265. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  266. #elif defined(HAVE_MMI)
  267. #define __align8 __attribute__ ((aligned (16)))
  268. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  269. #else
  270. #define __align8
  271. #endif
  272. #ifdef __GNUC__
  273. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  274. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  275. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  276. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  277. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  278. #else /* __GNUC__ */
  279. #define LD32(a) (*((uint32_t*)(a)))
  280. #define LD64(a) (*((uint64_t*)(a)))
  281. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  282. #endif /* !__GNUC__ */
  283. /* PSNR */
  284. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  285. int orig_linesize[3], int coded_linesize,
  286. AVCodecContext *avctx);
  287. /* FFT computation */
  288. /* NOTE: soon integer code will be added, so you must use the
  289. FFTSample type */
  290. typedef float FFTSample;
  291. typedef struct FFTComplex {
  292. FFTSample re, im;
  293. } FFTComplex;
  294. typedef struct FFTContext {
  295. int nbits;
  296. int inverse;
  297. uint16_t *revtab;
  298. FFTComplex *exptab;
  299. FFTComplex *exptab1; /* only used by SSE code */
  300. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  301. } FFTContext;
  302. int fft_init(FFTContext *s, int nbits, int inverse);
  303. void fft_permute(FFTContext *s, FFTComplex *z);
  304. void fft_calc_c(FFTContext *s, FFTComplex *z);
  305. void fft_calc_sse(FFTContext *s, FFTComplex *z);
  306. void fft_calc_altivec(FFTContext *s, FFTComplex *z);
  307. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  308. {
  309. s->fft_calc(s, z);
  310. }
  311. void fft_end(FFTContext *s);
  312. /* MDCT computation */
  313. typedef struct MDCTContext {
  314. int n; /* size of MDCT (i.e. number of input data * 2) */
  315. int nbits; /* n = 2^nbits */
  316. /* pre/post rotation tables */
  317. FFTSample *tcos;
  318. FFTSample *tsin;
  319. FFTContext fft;
  320. } MDCTContext;
  321. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  322. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  323. const FFTSample *input, FFTSample *tmp);
  324. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  325. const FFTSample *input, FFTSample *tmp);
  326. void ff_mdct_end(MDCTContext *s);
  327. #define WARPER88_1616(name8, name16)\
  328. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride){\
  329. return name8(s, dst , src , stride)\
  330. +name8(s, dst+8 , src+8 , stride)\
  331. +name8(s, dst +8*stride, src +8*stride, stride)\
  332. +name8(s, dst+8+8*stride, src+8+8*stride, stride);\
  333. }
  334. #ifndef HAVE_LRINTF
  335. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  336. /* better than nothing implementation. */
  337. /* btw, rintf() is existing on fbsd too -- alex */
  338. static inline long int lrintf(float x)
  339. {
  340. #ifdef CONFIG_WIN32
  341. /* XXX: incorrect, but make it compile */
  342. return (int)(x);
  343. #else
  344. return (int)(rint(x));
  345. #endif
  346. }
  347. #endif
  348. #endif