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.

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