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.

506 lines
17KB

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