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.

522 lines
18KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file dsputil.h
  22. * DSP utils.
  23. * note, many functions in here may use MMX which trashes the FPU state, it is
  24. * absolutely necessary to call emms_c() between dsp & float/double code
  25. */
  26. #ifndef DSPUTIL_H
  27. #define DSPUTIL_H
  28. #include "common.h"
  29. #include "avcodec.h"
  30. //#define DEBUG
  31. /* dct code */
  32. typedef short DCTELEM;
  33. void fdct_ifast (DCTELEM *data);
  34. void fdct_ifast248 (DCTELEM *data);
  35. void ff_jpeg_fdct_islow (DCTELEM *data);
  36. void ff_fdct248_islow (DCTELEM *data);
  37. void j_rev_dct (DCTELEM *data);
  38. void ff_fdct_mmx(DCTELEM *block);
  39. void ff_fdct_mmx2(DCTELEM *block);
  40. /* encoding scans */
  41. extern const uint8_t ff_alternate_horizontal_scan[64];
  42. extern const uint8_t ff_alternate_vertical_scan[64];
  43. extern const uint8_t ff_zigzag_direct[64];
  44. extern const uint8_t ff_zigzag248_direct[64];
  45. /* pixel operations */
  46. #define MAX_NEG_CROP 384
  47. /* temporary */
  48. extern uint32_t squareTbl[512];
  49. extern uint8_t cropTbl[256 + 2 * MAX_NEG_CROP];
  50. /* minimum alignment rules ;)
  51. if u notice errors in the align stuff, need more alignment for some asm code for some cpu
  52. or need to use a function with less aligned data then send a mail to the ffmpeg-dev list, ...
  53. !warning these alignments might not match reallity, (missing attribute((align)) stuff somewhere possible)
  54. i (michael) didnt check them, these are just the alignents which i think could be reached easily ...
  55. !future video codecs might need functions with less strict alignment
  56. */
  57. /*
  58. void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
  59. void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
  60. void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  61. void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
  62. void clear_blocks_c(DCTELEM *blocks);
  63. */
  64. /* add and put pixel (decoding) */
  65. // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
  66. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4
  67. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  68. 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);
  69. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  70. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  71. #define DEF_OLD_QPEL(name)\
  72. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  73. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  74. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  75. DEF_OLD_QPEL(qpel16_mc11_old_c)
  76. DEF_OLD_QPEL(qpel16_mc31_old_c)
  77. DEF_OLD_QPEL(qpel16_mc12_old_c)
  78. DEF_OLD_QPEL(qpel16_mc32_old_c)
  79. DEF_OLD_QPEL(qpel16_mc13_old_c)
  80. DEF_OLD_QPEL(qpel16_mc33_old_c)
  81. DEF_OLD_QPEL(qpel8_mc11_old_c)
  82. DEF_OLD_QPEL(qpel8_mc31_old_c)
  83. DEF_OLD_QPEL(qpel8_mc12_old_c)
  84. DEF_OLD_QPEL(qpel8_mc32_old_c)
  85. DEF_OLD_QPEL(qpel8_mc13_old_c)
  86. DEF_OLD_QPEL(qpel8_mc33_old_c)
  87. #define CALL_2X_PIXELS(a, b, n)\
  88. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  89. b(block , pixels , line_size, h);\
  90. b(block+n, pixels+n, line_size, h);\
  91. }
  92. /* motion estimation */
  93. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2
  94. // allthough currently h<4 is not used as functions with width <8 are not used and neither implemented
  95. typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size, int h)/* __attribute__ ((const))*/;
  96. /**
  97. * DSPContext.
  98. */
  99. typedef struct DSPContext {
  100. /* pixel ops : interface with DCT */
  101. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  102. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  103. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  104. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  105. /**
  106. * translational global motion compensation.
  107. */
  108. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  109. /**
  110. * global motion compensation.
  111. */
  112. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  113. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  114. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  115. int (*pix_sum)(uint8_t * pix, int line_size);
  116. int (*pix_norm1)(uint8_t * pix, int line_size);
  117. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  118. me_cmp_func sad[5]; /* identical to pix_absAxA except additional void * */
  119. me_cmp_func sse[5];
  120. me_cmp_func hadamard8_diff[5];
  121. me_cmp_func dct_sad[5];
  122. me_cmp_func quant_psnr[5];
  123. me_cmp_func bit[5];
  124. me_cmp_func rd[5];
  125. me_cmp_func vsad[5];
  126. me_cmp_func vsse[5];
  127. me_cmp_func me_pre_cmp[5];
  128. me_cmp_func me_cmp[5];
  129. me_cmp_func me_sub_cmp[5];
  130. me_cmp_func mb_cmp[5];
  131. me_cmp_func ildct_cmp[5]; //only width 16 used
  132. /**
  133. * Halfpel motion compensation with rounding (a+b+1)>>1.
  134. * this is an array[4][4] of motion compensation funcions for 4
  135. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  136. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  137. * @param block destination where the result is stored
  138. * @param pixels source
  139. * @param line_size number of bytes in a horizontal line of block
  140. * @param h height
  141. */
  142. op_pixels_func put_pixels_tab[4][4];
  143. /**
  144. * Halfpel motion compensation with rounding (a+b+1)>>1.
  145. * This is an array[4][4] of motion compensation functions for 4
  146. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  147. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  148. * @param block destination into which the result is averaged (a+b+1)>>1
  149. * @param pixels source
  150. * @param line_size number of bytes in a horizontal line of block
  151. * @param h height
  152. */
  153. op_pixels_func avg_pixels_tab[4][4];
  154. /**
  155. * Halfpel motion compensation with no rounding (a+b)>>1.
  156. * this is an array[2][4] of motion compensation funcions for 2
  157. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  158. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  159. * @param block destination where the result is stored
  160. * @param pixels source
  161. * @param line_size number of bytes in a horizontal line of block
  162. * @param h height
  163. */
  164. op_pixels_func put_no_rnd_pixels_tab[2][4];
  165. /**
  166. * Halfpel motion compensation with no rounding (a+b)>>1.
  167. * this is an array[2][4] of motion compensation funcions for 2
  168. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  169. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  170. * @param block destination into which the result is averaged (a+b)>>1
  171. * @param pixels source
  172. * @param line_size number of bytes in a horizontal line of block
  173. * @param h height
  174. */
  175. op_pixels_func avg_no_rnd_pixels_tab[2][4];
  176. /**
  177. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  178. * this is an array[12] of motion compensation funcions for the 9 thirdpel positions<br>
  179. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  180. * @param block destination where the result is stored
  181. * @param pixels source
  182. * @param line_size number of bytes in a horizontal line of block
  183. * @param h height
  184. */
  185. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  186. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  187. qpel_mc_func put_qpel_pixels_tab[2][16];
  188. qpel_mc_func avg_qpel_pixels_tab[2][16];
  189. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  190. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  191. qpel_mc_func put_mspel_pixels_tab[8];
  192. /**
  193. * h264 Chram MC
  194. */
  195. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  196. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  197. qpel_mc_func put_h264_qpel_pixels_tab[3][16];
  198. qpel_mc_func avg_h264_qpel_pixels_tab[3][16];
  199. me_cmp_func pix_abs[2][4];
  200. /* huffyuv specific */
  201. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  202. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  203. /**
  204. * subtract huffyuv's variant of median prediction
  205. * note, this might read from src1[-1], src2[-1]
  206. */
  207. void (*sub_hfyu_median_prediction)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top);
  208. void (*bswap_buf)(uint32_t *dst, uint32_t *src, int w);
  209. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  210. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  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. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  254. #define BYTE_VEC32(c) ((c)*0x01010101UL)
  255. static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
  256. {
  257. return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  258. }
  259. static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
  260. {
  261. return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
  262. }
  263. /**
  264. * Empty mmx state.
  265. * this must be called between any dsp function and float/double code.
  266. * for example sin(); dsp->idct_put(); emms_c(); cos()
  267. */
  268. #define emms_c()
  269. /* should be defined by architectures supporting
  270. one or more MultiMedia extension */
  271. int mm_support(void);
  272. #if defined(HAVE_MMX)
  273. #undef emms_c
  274. #define MM_MMX 0x0001 /* standard MMX */
  275. #define MM_3DNOW 0x0004 /* AMD 3DNOW */
  276. #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
  277. #define MM_SSE 0x0008 /* SSE functions */
  278. #define MM_SSE2 0x0010 /* PIV SSE2 functions */
  279. extern int mm_flags;
  280. void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  281. void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
  282. static inline void emms(void)
  283. {
  284. __asm __volatile ("emms;":::"memory");
  285. }
  286. #define emms_c() \
  287. {\
  288. if (mm_flags & MM_MMX)\
  289. emms();\
  290. }
  291. #define __align8 __attribute__ ((aligned (8)))
  292. void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
  293. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
  294. #elif defined(ARCH_ARMV4L)
  295. /* This is to use 4 bytes read to the IDCT pointers for some 'zero'
  296. line ptimizations */
  297. #define __align8 __attribute__ ((aligned (4)))
  298. void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
  299. #elif defined(HAVE_MLIB)
  300. /* SPARC/VIS IDCT needs 8-byte aligned DCT blocks */
  301. #define __align8 __attribute__ ((aligned (8)))
  302. void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
  303. #elif defined(ARCH_ALPHA)
  304. #define __align8 __attribute__ ((aligned (8)))
  305. void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
  306. #elif defined(ARCH_POWERPC)
  307. #define MM_ALTIVEC 0x0001 /* standard AltiVec */
  308. extern int mm_flags;
  309. #if defined(HAVE_ALTIVEC) && !defined(CONFIG_DARWIN)
  310. #define pixel altivec_pixel
  311. #include <altivec.h>
  312. #undef pixel
  313. #endif
  314. #define __align8 __attribute__ ((aligned (16)))
  315. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  316. #elif defined(HAVE_MMI)
  317. #define __align8 __attribute__ ((aligned (16)))
  318. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  319. #elif defined(ARCH_SH4)
  320. #define __align8 __attribute__ ((aligned (8)))
  321. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  322. #else
  323. #define __align8
  324. #endif
  325. #ifdef __GNUC__
  326. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  327. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  328. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  329. #define LD16(a) (((const struct unaligned_16 *) (a))->l)
  330. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  331. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  332. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  333. #else /* __GNUC__ */
  334. #define LD16(a) (*((uint16_t*)(a)))
  335. #define LD32(a) (*((uint32_t*)(a)))
  336. #define LD64(a) (*((uint64_t*)(a)))
  337. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  338. #endif /* !__GNUC__ */
  339. /* PSNR */
  340. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  341. int orig_linesize[3], int coded_linesize,
  342. AVCodecContext *avctx);
  343. /* FFT computation */
  344. /* NOTE: soon integer code will be added, so you must use the
  345. FFTSample type */
  346. typedef float FFTSample;
  347. typedef struct FFTComplex {
  348. FFTSample re, im;
  349. } FFTComplex;
  350. typedef struct FFTContext {
  351. int nbits;
  352. int inverse;
  353. uint16_t *revtab;
  354. FFTComplex *exptab;
  355. FFTComplex *exptab1; /* only used by SSE code */
  356. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  357. } FFTContext;
  358. int fft_init(FFTContext *s, int nbits, int inverse);
  359. void fft_permute(FFTContext *s, FFTComplex *z);
  360. void fft_calc_c(FFTContext *s, FFTComplex *z);
  361. void fft_calc_sse(FFTContext *s, FFTComplex *z);
  362. void fft_calc_altivec(FFTContext *s, FFTComplex *z);
  363. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  364. {
  365. s->fft_calc(s, z);
  366. }
  367. void fft_end(FFTContext *s);
  368. /* MDCT computation */
  369. typedef struct MDCTContext {
  370. int n; /* size of MDCT (i.e. number of input data * 2) */
  371. int nbits; /* n = 2^nbits */
  372. /* pre/post rotation tables */
  373. FFTSample *tcos;
  374. FFTSample *tsin;
  375. FFTContext fft;
  376. } MDCTContext;
  377. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  378. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  379. const FFTSample *input, FFTSample *tmp);
  380. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  381. const FFTSample *input, FFTSample *tmp);
  382. void ff_mdct_end(MDCTContext *s);
  383. #define WARPER8_16(name8, name16)\
  384. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  385. return name8(s, dst , src , stride, h)\
  386. +name8(s, dst+8 , src+8 , stride, h);\
  387. }
  388. #define WARPER8_16_SQ(name8, name16)\
  389. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  390. int score=0;\
  391. score +=name8(s, dst , src , stride, 8);\
  392. score +=name8(s, dst+8 , src+8 , stride, 8);\
  393. if(h==16){\
  394. dst += 8*stride;\
  395. src += 8*stride;\
  396. score +=name8(s, dst , src , stride, 8);\
  397. score +=name8(s, dst+8 , src+8 , stride, 8);\
  398. }\
  399. return score;\
  400. }
  401. #ifndef HAVE_LRINTF
  402. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  403. /* better than nothing implementation. */
  404. /* btw, rintf() is existing on fbsd too -- alex */
  405. static inline long int lrintf(float x)
  406. {
  407. #ifdef CONFIG_WIN32
  408. /* XXX: incorrect, but make it compile */
  409. return (int)(x);
  410. #else
  411. return (int)(rint(x));
  412. #endif
  413. }
  414. #endif
  415. #endif