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.

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