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.

521 lines
18KB

  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. //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4
  66. typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
  67. 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);
  68. typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  69. typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
  70. #define DEF_OLD_QPEL(name)\
  71. void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  72. void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
  73. void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
  74. DEF_OLD_QPEL(qpel16_mc11_old_c)
  75. DEF_OLD_QPEL(qpel16_mc31_old_c)
  76. DEF_OLD_QPEL(qpel16_mc12_old_c)
  77. DEF_OLD_QPEL(qpel16_mc32_old_c)
  78. DEF_OLD_QPEL(qpel16_mc13_old_c)
  79. DEF_OLD_QPEL(qpel16_mc33_old_c)
  80. DEF_OLD_QPEL(qpel8_mc11_old_c)
  81. DEF_OLD_QPEL(qpel8_mc31_old_c)
  82. DEF_OLD_QPEL(qpel8_mc12_old_c)
  83. DEF_OLD_QPEL(qpel8_mc32_old_c)
  84. DEF_OLD_QPEL(qpel8_mc13_old_c)
  85. DEF_OLD_QPEL(qpel8_mc33_old_c)
  86. #define CALL_2X_PIXELS(a, b, n)\
  87. static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
  88. b(block , pixels , line_size, h);\
  89. b(block+n, pixels+n, line_size, h);\
  90. }
  91. /* motion estimation */
  92. // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2
  93. // allthough currently h<4 is not used as functions with width <8 are not used and neither implemented
  94. 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))*/;
  95. /**
  96. * DSPContext.
  97. */
  98. typedef struct DSPContext {
  99. /* pixel ops : interface with DCT */
  100. void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
  101. void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
  102. void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  103. void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
  104. /**
  105. * translational global motion compensation.
  106. */
  107. void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
  108. /**
  109. * global motion compensation.
  110. */
  111. void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
  112. int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
  113. void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
  114. int (*pix_sum)(uint8_t * pix, int line_size);
  115. int (*pix_norm1)(uint8_t * pix, int line_size);
  116. // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
  117. me_cmp_func sad[5]; /* identical to pix_absAxA except additional void * */
  118. me_cmp_func sse[5];
  119. me_cmp_func hadamard8_diff[5];
  120. me_cmp_func dct_sad[5];
  121. me_cmp_func quant_psnr[5];
  122. me_cmp_func bit[5];
  123. me_cmp_func rd[5];
  124. me_cmp_func vsad[5];
  125. me_cmp_func vsse[5];
  126. me_cmp_func me_pre_cmp[5];
  127. me_cmp_func me_cmp[5];
  128. me_cmp_func me_sub_cmp[5];
  129. me_cmp_func mb_cmp[5];
  130. me_cmp_func ildct_cmp[5]; //only width 16 used
  131. /**
  132. * Halfpel motion compensation with rounding (a+b+1)>>1.
  133. * this is an array[4][4] of motion compensation funcions for 4
  134. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  135. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  136. * @param block destination where the result is stored
  137. * @param pixels source
  138. * @param line_size number of bytes in a horizontal line of block
  139. * @param h height
  140. */
  141. op_pixels_func put_pixels_tab[4][4];
  142. /**
  143. * Halfpel motion compensation with rounding (a+b+1)>>1.
  144. * This is an array[4][4] of motion compensation functions for 4
  145. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  146. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  147. * @param block destination into which the result is averaged (a+b+1)>>1
  148. * @param pixels source
  149. * @param line_size number of bytes in a horizontal line of block
  150. * @param h height
  151. */
  152. op_pixels_func avg_pixels_tab[4][4];
  153. /**
  154. * Halfpel motion compensation with no rounding (a+b)>>1.
  155. * this is an array[2][4] of motion compensation funcions for 2
  156. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  157. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  158. * @param block destination where the result is stored
  159. * @param pixels source
  160. * @param line_size number of bytes in a horizontal line of block
  161. * @param h height
  162. */
  163. op_pixels_func put_no_rnd_pixels_tab[2][4];
  164. /**
  165. * Halfpel motion compensation with no rounding (a+b)>>1.
  166. * this is an array[2][4] of motion compensation funcions for 2
  167. * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
  168. * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
  169. * @param block destination into which the result is averaged (a+b)>>1
  170. * @param pixels source
  171. * @param line_size number of bytes in a horizontal line of block
  172. * @param h height
  173. */
  174. op_pixels_func avg_no_rnd_pixels_tab[2][4];
  175. /**
  176. * Thirdpel motion compensation with rounding (a+b+1)>>1.
  177. * this is an array[12] of motion compensation funcions for the 9 thirdpel positions<br>
  178. * *pixels_tab[ xthirdpel + 4*ythirdpel ]
  179. * @param block destination where the result is stored
  180. * @param pixels source
  181. * @param line_size number of bytes in a horizontal line of block
  182. * @param h height
  183. */
  184. tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  185. tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
  186. qpel_mc_func put_qpel_pixels_tab[2][16];
  187. qpel_mc_func avg_qpel_pixels_tab[2][16];
  188. qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
  189. qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
  190. qpel_mc_func put_mspel_pixels_tab[8];
  191. /**
  192. * h264 Chram MC
  193. */
  194. h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
  195. h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
  196. qpel_mc_func put_h264_qpel_pixels_tab[3][16];
  197. qpel_mc_func avg_h264_qpel_pixels_tab[3][16];
  198. me_cmp_func pix_abs[2][4];
  199. /* huffyuv specific */
  200. void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
  201. void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
  202. /**
  203. * subtract huffyuv's variant of median prediction
  204. * note, this might read from src1[-1], src2[-1]
  205. */
  206. void (*sub_hfyu_median_prediction)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top);
  207. void (*bswap_buf)(uint32_t *dst, uint32_t *src, int w);
  208. void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
  209. void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
  210. /* (I)DCT */
  211. void (*fdct)(DCTELEM *block/* align 16*/);
  212. void (*fdct248)(DCTELEM *block/* align 16*/);
  213. /* IDCT really*/
  214. void (*idct)(DCTELEM *block/* align 16*/);
  215. /**
  216. * block -> idct -> clip to unsigned 8 bit -> dest.
  217. * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
  218. * @param line_size size in bytes of a horizotal line of dest
  219. */
  220. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  221. /**
  222. * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
  223. * @param line_size size in bytes of a horizotal line of dest
  224. */
  225. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  226. /**
  227. * idct input permutation.
  228. * several optimized IDCTs need a permutated input (relative to the normal order of the reference
  229. * IDCT)
  230. * this permutation must be performed before the idct_put/add, note, normally this can be merged
  231. * with the zigzag/alternate scan<br>
  232. * an example to avoid confusion:
  233. * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
  234. * - (x -> referece dct -> reference idct -> x)
  235. * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
  236. * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
  237. */
  238. uint8_t idct_permutation[64];
  239. int idct_permutation_type;
  240. #define FF_NO_IDCT_PERM 1
  241. #define FF_LIBMPEG2_IDCT_PERM 2
  242. #define FF_SIMPLE_IDCT_PERM 3
  243. #define FF_TRANSPOSE_IDCT_PERM 4
  244. } DSPContext;
  245. void dsputil_static_init(void);
  246. void dsputil_init(DSPContext* p, AVCodecContext *avctx);
  247. /**
  248. * permute block according to permuatation.
  249. * @param last last non zero element in scantable order
  250. */
  251. void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
  252. void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
  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. #define pixel altivec_pixel
  310. #include <altivec.h>
  311. #undef pixel
  312. #endif
  313. #define __align8 __attribute__ ((aligned (16)))
  314. void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
  315. #elif defined(HAVE_MMI)
  316. #define __align8 __attribute__ ((aligned (16)))
  317. void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
  318. #elif defined(ARCH_SH4)
  319. #define __align8 __attribute__ ((aligned (8)))
  320. void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
  321. #else
  322. #define __align8
  323. #endif
  324. #ifdef __GNUC__
  325. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  326. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  327. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  328. #define LD16(a) (((const struct unaligned_16 *) (a))->l)
  329. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  330. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  331. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  332. #else /* __GNUC__ */
  333. #define LD16(a) (*((uint16_t*)(a)))
  334. #define LD32(a) (*((uint32_t*)(a)))
  335. #define LD64(a) (*((uint64_t*)(a)))
  336. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  337. #endif /* !__GNUC__ */
  338. /* PSNR */
  339. void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
  340. int orig_linesize[3], int coded_linesize,
  341. AVCodecContext *avctx);
  342. /* FFT computation */
  343. /* NOTE: soon integer code will be added, so you must use the
  344. FFTSample type */
  345. typedef float FFTSample;
  346. typedef struct FFTComplex {
  347. FFTSample re, im;
  348. } FFTComplex;
  349. typedef struct FFTContext {
  350. int nbits;
  351. int inverse;
  352. uint16_t *revtab;
  353. FFTComplex *exptab;
  354. FFTComplex *exptab1; /* only used by SSE code */
  355. void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
  356. } FFTContext;
  357. int fft_init(FFTContext *s, int nbits, int inverse);
  358. void fft_permute(FFTContext *s, FFTComplex *z);
  359. void fft_calc_c(FFTContext *s, FFTComplex *z);
  360. void fft_calc_sse(FFTContext *s, FFTComplex *z);
  361. void fft_calc_altivec(FFTContext *s, FFTComplex *z);
  362. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  363. {
  364. s->fft_calc(s, z);
  365. }
  366. void fft_end(FFTContext *s);
  367. /* MDCT computation */
  368. typedef struct MDCTContext {
  369. int n; /* size of MDCT (i.e. number of input data * 2) */
  370. int nbits; /* n = 2^nbits */
  371. /* pre/post rotation tables */
  372. FFTSample *tcos;
  373. FFTSample *tsin;
  374. FFTContext fft;
  375. } MDCTContext;
  376. int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
  377. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  378. const FFTSample *input, FFTSample *tmp);
  379. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  380. const FFTSample *input, FFTSample *tmp);
  381. void ff_mdct_end(MDCTContext *s);
  382. #define WARPER8_16(name8, name16)\
  383. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  384. return name8(s, dst , src , stride, h)\
  385. +name8(s, dst+8 , src+8 , stride, h);\
  386. }
  387. #define WARPER8_16_SQ(name8, name16)\
  388. static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
  389. int score=0;\
  390. score +=name8(s, dst , src , stride, 8);\
  391. score +=name8(s, dst+8 , src+8 , stride, 8);\
  392. if(h==16){\
  393. dst += 8*stride;\
  394. src += 8*stride;\
  395. score +=name8(s, dst , src , stride, 8);\
  396. score +=name8(s, dst+8 , src+8 , stride, 8);\
  397. }\
  398. return score;\
  399. }
  400. #ifndef HAVE_LRINTF
  401. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  402. /* better than nothing implementation. */
  403. /* btw, rintf() is existing on fbsd too -- alex */
  404. static inline long int lrintf(float x)
  405. {
  406. #ifdef CONFIG_WIN32
  407. /* XXX: incorrect, but make it compile */
  408. return (int)(x);
  409. #else
  410. return (int)(rint(x));
  411. #endif
  412. }
  413. #endif
  414. #endif