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.

356 lines
11KB

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