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.

476 lines
16KB

  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. * note, many functions in here may use MMX which trashes the FPU state, it is
  23. * absolutely necessary to call emms_c() between dsp & float/double code
  24. */
  25. #ifndef DSPUTIL_H
  26. #define DSPUTIL_H
  27. #include "common.h"
  28. #include "avcodec.h"
  29. //#define DEBUG
  30. /* dct code */
  31. typedef short DCTELEM;
  32. //typedef int DCTELEM;
  33. void fdct_ifast (DCTELEM *data);
  34. void ff_jpeg_fdct_islow (DCTELEM *data);
  35. void j_rev_dct (DCTELEM *data);
  36. void ff_fdct_mmx(DCTELEM *block);
  37. /* encoding scans */
  38. extern const uint8_t ff_alternate_horizontal_scan[64];
  39. extern const uint8_t ff_alternate_vertical_scan[64];
  40. extern const uint8_t ff_zigzag_direct[64];
  41. /* pixel operations */
  42. #define MAX_NEG_CROP 384
  43. /* temporary */
  44. extern uint32_t squareTbl[512];
  45. extern uint8_t cropTbl[256 + 2 * MAX_NEG_CROP];
  46. /* minimum alignment rules ;)
  47. if u notice errors in the align stuff, need more alignment for some asm code for some cpu
  48. or need to use a function with less aligned data then send a mail to the ffmpeg-dev list, ...
  49. !warning these alignments might not match reallity, (missing attribute((align)) stuff somewhere possible)
  50. i (michael) didnt check them, these are just the alignents which i think could be reached easily ...
  51. !future video codecs might need functions with less strict alignment
  52. */
  53. /*
  54. void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
  55. void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
  56. void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  57. void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  58. void clear_blocks_c(DCTELEM *blocks);
  59. */
  60. /* add and put pixel (decoding) */
  61. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  62. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  63. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  64. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  65. #define DEF_OLD_QPEL(name)\
  66. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  67. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  68. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  69. DEF_OLD_QPEL(qpel16_mc11_old_c)
  70. DEF_OLD_QPEL(qpel16_mc31_old_c)
  71. DEF_OLD_QPEL(qpel16_mc12_old_c)
  72. DEF_OLD_QPEL(qpel16_mc32_old_c)
  73. DEF_OLD_QPEL(qpel16_mc13_old_c)
  74. DEF_OLD_QPEL(qpel16_mc33_old_c)
  75. DEF_OLD_QPEL(qpel8_mc11_old_c)
  76. DEF_OLD_QPEL(qpel8_mc31_old_c)
  77. DEF_OLD_QPEL(qpel8_mc12_old_c)
  78. DEF_OLD_QPEL(qpel8_mc32_old_c)
  79. DEF_OLD_QPEL(qpel8_mc13_old_c)
  80. DEF_OLD_QPEL(qpel8_mc33_old_c)
  81. #define CALL_2X_PIXELS(a, b, n)\
  82. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  83. b(block , pixels , line_size, h);\
  84. b(block+n, pixels+n, line_size, h);\
  85. }
  86. /* motion estimation */
  87. typedef int (*op_pixels_abs_func)(uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size)/* __attribute__ ((const))*/;
  88. 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))*/;
  89. /**
  90. * DSPContext.
  91. */
  92. typedef struct DSPContext {
  93. /* pixel ops : interface with DCT */
  94. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  95. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  96. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  97. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  98. /**
  99. * translational global motion compensation.
  100. */
  101. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  102. /**
  103. * global motion compensation.
  104. */
  105. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  106. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  107. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  108. int (*pix_sum)(uint8_t * pix, int line_size);
  109. int (*pix_norm1)(uint8_t * pix, int line_size);
  110. me_cmp_func sad[2]; /* identical to pix_absAxA except additional void * */
  111. me_cmp_func sse[2];
  112. me_cmp_func hadamard8_diff[2];
  113. me_cmp_func dct_sad[2];
  114. me_cmp_func quant_psnr[2];
  115. me_cmp_func bit[2];
  116. me_cmp_func rd[2];
  117. int (*hadamard8_abs )(uint8_t *src, int stride, int mean);
  118. me_cmp_func me_pre_cmp[11];
  119. me_cmp_func me_cmp[11];
  120. me_cmp_func me_sub_cmp[11];
  121. me_cmp_func mb_cmp[11];
  122. /* maybe create an array for 16/8 functions */
  123. /**
  124. * Halfpel motion compensation with rounding (a+b+1)>>1.
  125. * this is an array[2][4] of motion compensation funcions for 2
  126. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  127. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  128. * @param block destination where the result is stored
  129. * @param pixels source
  130. * @param line_size number of bytes in a horizontal line of block
  131. * @param h height
  132. */
  133. op_pixels_func put_pixels_tab[2][4];
  134. /**
  135. * Halfpel motion compensation with rounding (a+b+1)>>1.
  136. * this is an array[2][4] of motion compensation funcions for 2
  137. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  138. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  139. * @param block destination into which the result is averaged (a+b+1)>>1
  140. * @param pixels source
  141. * @param line_size number of bytes in a horizontal line of block
  142. * @param h height
  143. */
  144. op_pixels_func avg_pixels_tab[2][4];
  145. /**
  146. * Halfpel motion compensation with no rounding (a+b)>>1.
  147. * this is an array[2][4] of motion compensation funcions for 2
  148. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  149. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  150. * @param block destination where the result is stored
  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 put_no_rnd_pixels_tab[2][4];
  156. /**
  157. * Halfpel motion compensation with no rounding (a+b)>>1.
  158. * this is an array[2][4] of motion compensation funcions for 2
  159. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  160. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  161. * @param block destination into which the result is averaged (a+b)>>1
  162. * @param pixels source
  163. * @param line_size number of bytes in a horizontal line of block
  164. * @param h height
  165. */
  166. op_pixels_func avg_no_rnd_pixels_tab[2][4];
  167. qpel_mc_func put_qpel_pixels_tab[2][16];
  168. qpel_mc_func avg_qpel_pixels_tab[2][16];
  169. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  170. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  171. qpel_mc_func put_mspel_pixels_tab[8];
  172. /**
  173. * h264 Chram MC
  174. */
  175. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  176. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  177. qpel_mc_func put_h264_qpel_pixels_tab[3][16];
  178. qpel_mc_func avg_h264_qpel_pixels_tab[3][16];
  179. op_pixels_abs_func pix_abs16x16;
  180. op_pixels_abs_func pix_abs16x16_x2;
  181. op_pixels_abs_func pix_abs16x16_y2;
  182. op_pixels_abs_func pix_abs16x16_xy2;
  183. op_pixels_abs_func pix_abs8x8;
  184. op_pixels_abs_func pix_abs8x8_x2;
  185. op_pixels_abs_func pix_abs8x8_y2;
  186. op_pixels_abs_func pix_abs8x8_xy2;
  187. /* huffyuv specific */
  188. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  189. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  190. /* (I)DCT */
  191. void (*fdct)(DCTELEM *block/* align 16*/);
  192. /**
  193. * block -> idct -> clip to unsigned 8 bit -> dest.
  194. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  195. * @param line_size size in bytes of a horizotal line of dest
  196. */
  197. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  198. /**
  199. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  200. * @param line_size size in bytes of a horizotal line of dest
  201. */
  202. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  203. /**
  204. * idct input permutation.
  205. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  206. * IDCT)
  207. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  208. * with the zigzag/alternate scan<br>
  209. * an example to avoid confusion:
  210. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  211. * - (x -> referece dct -> reference idct -> x)
  212. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  213. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  214. */
  215. uint8_t idct_permutation[64];
  216. int idct_permutation_type;
  217. #define FF_NO_IDCT_PERM 1
  218. #define FF_LIBMPEG2_IDCT_PERM 2
  219. #define FF_SIMPLE_IDCT_PERM 3
  220. #define FF_TRANSPOSE_IDCT_PERM 4
  221. } DSPContext;
  222. void dsputil_static_init(void);
  223. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  224. /**
  225. * permute block according to permuatation.
  226. * @param last last non zero element in scantable order
  227. */
  228. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  229. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  230. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  231. {
  232. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  233. }
  234. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  235. {
  236. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  237. }
  238. /**
  239. * Empty mmx state.
  240. * this must be called between any dsp function and float/double code.
  241. * for example sin(); dsp->idct_put(); emms_c(); cos()
  242. */
  243. #define emms_c()
  244. /* should be defined by architectures supporting
  245. one or more MultiMedia extension */
  246. int mm_support(void);
  247. #if defined(HAVE_MMX)
  248. #undef emms_c
  249. #define MM_MMX 0x0001 /* standard MMX */
  250. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  251. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  252. #define MM_SSE 0x0008 /* SSE functions */
  253. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  254. extern int mm_flags;
  255. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  256. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  257. static inline void emms(void)
  258. {
  259. __asm __volatile ("emms;":::"memory");
  260. }
  261. #define emms_c() \
  262. {\
  263. if (mm_flags & MM_MMX)\
  264. emms();\
  265. }
  266. #define __align8 __attribute__ ((aligned (8)))
  267. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  268. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  269. #elif defined(ARCH_ARMV4L)
  270. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  271. line ptimizations */
  272. #define __align8 __attribute__ ((aligned (4)))
  273. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  274. #elif defined(HAVE_MLIB)
  275. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  276. #define __align8 __attribute__ ((aligned (8)))
  277. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  278. #elif defined(ARCH_ALPHA)
  279. #define __align8 __attribute__ ((aligned (8)))
  280. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  281. #elif defined(ARCH_POWERPC)
  282. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  283. extern int mm_flags;
  284. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  285. #include <altivec.h>
  286. #endif
  287. #define __align8 __attribute__ ((aligned (16)))
  288. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  289. #elif defined(HAVE_MMI)
  290. #define __align8 __attribute__ ((aligned (16)))
  291. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  292. #elif defined(ARCH_SH4)
  293. #define __align8 __attribute__ ((aligned (8)))
  294. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  295. #else
  296. #define __align8
  297. #endif
  298. #ifdef __GNUC__
  299. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  300. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  301. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  302. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  303. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  304. #else /* __GNUC__ */
  305. #define LD32(a) (*((uint32_t*)(a)))
  306. #define LD64(a) (*((uint64_t*)(a)))
  307. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  308. #endif /* !__GNUC__ */
  309. /* PSNR */
  310. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  311. int orig_linesize[3], int coded_linesize,
  312. AVCodecContext *avctx);
  313. /* FFT computation */
  314. /* NOTE: soon integer code will be added, so you must use the
  315. FFTSample type */
  316. typedef float FFTSample;
  317. typedef struct FFTComplex {
  318. FFTSample re, im;
  319. } FFTComplex;
  320. typedef struct FFTContext {
  321. int nbits;
  322. int inverse;
  323. uint16_t *revtab;
  324. FFTComplex *exptab;
  325. FFTComplex *exptab1; /* only used by SSE code */
  326. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  327. } FFTContext;
  328. int fft_init(FFTContext *s, int nbits, int inverse);
  329. void fft_permute(FFTContext *s, FFTComplex *z);
  330. void fft_calc_c(FFTContext *s, FFTComplex *z);
  331. void fft_calc_sse(FFTContext *s, FFTComplex *z);
  332. void fft_calc_altivec(FFTContext *s, FFTComplex *z);
  333. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  334. {
  335. s->fft_calc(s, z);
  336. }
  337. void fft_end(FFTContext *s);
  338. /* MDCT computation */
  339. typedef struct MDCTContext {
  340. int n; /* size of MDCT (i.e. number of input data * 2) */
  341. int nbits; /* n = 2^nbits */
  342. /* pre/post rotation tables */
  343. FFTSample *tcos;
  344. FFTSample *tsin;
  345. FFTContext fft;
  346. } MDCTContext;
  347. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  348. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  349. const FFTSample *input, FFTSample *tmp);
  350. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  351. const FFTSample *input, FFTSample *tmp);
  352. void ff_mdct_end(MDCTContext *s);
  353. #define WARPER88_1616(name8, name16)\
  354. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride){\
  355. return name8(s, dst , src , stride)\
  356. +name8(s, dst+8 , src+8 , stride)\
  357. +name8(s, dst +8*stride, src +8*stride, stride)\
  358. +name8(s, dst+8+8*stride, src+8+8*stride, stride);\
  359. }
  360. #ifndef HAVE_LRINTF
  361. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  362. /* better than nothing implementation. */
  363. /* btw, rintf() is existing on fbsd too -- alex */
  364. static inline long int lrintf(float x)
  365. {
  366. #ifdef CONFIG_WIN32
  367. /* XXX: incorrect, but make it compile */
  368. return (int)(x);
  369. #else
  370. return (int)(rint(x));
  371. #endif
  372. }
  373. #endif
  374. #endif