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.

392 lines
12KB

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