]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/dsputil.h
float_to_int16_interleave: change src to an array of pointers instead of assuming...
[frescor/ffmpeg.git] / libavcodec / dsputil.h
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 file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file dsputil.h
25  * DSP utils.
26  * note, many functions in here may use MMX which trashes the FPU state, it is
27  * absolutely necessary to call emms_c() between dsp & float/double code
28  */
29
30 #ifndef FFMPEG_DSPUTIL_H
31 #define FFMPEG_DSPUTIL_H
32
33 #include "avcodec.h"
34
35
36 //#define DEBUG
37 /* dct code */
38 typedef short DCTELEM;
39 typedef int DWTELEM;
40 typedef short IDWTELEM;
41
42 void fdct_ifast (DCTELEM *data);
43 void fdct_ifast248 (DCTELEM *data);
44 void ff_jpeg_fdct_islow (DCTELEM *data);
45 void ff_fdct248_islow (DCTELEM *data);
46
47 void j_rev_dct (DCTELEM *data);
48 void j_rev_dct4 (DCTELEM *data);
49 void j_rev_dct2 (DCTELEM *data);
50 void j_rev_dct1 (DCTELEM *data);
51 void ff_wmv2_idct_c(DCTELEM *data);
52
53 void ff_fdct_mmx(DCTELEM *block);
54 void ff_fdct_mmx2(DCTELEM *block);
55 void ff_fdct_sse2(DCTELEM *block);
56
57 void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride);
58 void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride);
59 void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
60 void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
61 void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block);
62 void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block);
63
64 void ff_vector_fmul_add_add_c(float *dst, const float *src0, const float *src1,
65                               const float *src2, int src3, int blocksize, int step);
66 void ff_vector_fmul_window_c(float *dst, const float *src0, const float *src1,
67                              const float *win, float add_bias, int len);
68 void ff_float_to_int16_c(int16_t *dst, const float *src, long len);
69
70 /* encoding scans */
71 extern const uint8_t ff_alternate_horizontal_scan[64];
72 extern const uint8_t ff_alternate_vertical_scan[64];
73 extern const uint8_t ff_zigzag_direct[64];
74 extern const uint8_t ff_zigzag248_direct[64];
75
76 /* pixel operations */
77 #define MAX_NEG_CROP 1024
78
79 /* temporary */
80 extern uint32_t ff_squareTbl[512];
81 extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
82
83 /* VP3 DSP functions */
84 void ff_vp3_idct_c(DCTELEM *block/* align 16*/);
85 void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
86 void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
87
88 /* 1/2^n downscaling functions from imgconvert.c */
89 void ff_img_copy_plane(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
90 void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
91 void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
92 void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
93
94 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
95               int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
96
97 /* minimum alignment rules ;)
98 If you notice errors in the align stuff, need more alignment for some ASM code
99 for some CPU or need to use a function with less aligned data then send a mail
100 to the ffmpeg-devel mailing list, ...
101
102 !warning These alignments might not match reality, (missing attribute((align))
103 stuff somewhere possible).
104 I (Michael) did not check them, these are just the alignments which I think
105 could be reached easily ...
106
107 !future video codecs might need functions with less strict alignment
108 */
109
110 /*
111 void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
112 void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
113 void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
114 void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
115 void clear_blocks_c(DCTELEM *blocks);
116 */
117
118 /* add and put pixel (decoding) */
119 // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
120 //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4
121 typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
122 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);
123 typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
124 typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
125 typedef void (*h264_weight_func)(uint8_t *block, int stride, int log2_denom, int weight, int offset);
126 typedef void (*h264_biweight_func)(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset);
127
128 #define DEF_OLD_QPEL(name)\
129 void ff_put_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
130 void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
131 void ff_avg_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
132
133 DEF_OLD_QPEL(qpel16_mc11_old_c)
134 DEF_OLD_QPEL(qpel16_mc31_old_c)
135 DEF_OLD_QPEL(qpel16_mc12_old_c)
136 DEF_OLD_QPEL(qpel16_mc32_old_c)
137 DEF_OLD_QPEL(qpel16_mc13_old_c)
138 DEF_OLD_QPEL(qpel16_mc33_old_c)
139 DEF_OLD_QPEL(qpel8_mc11_old_c)
140 DEF_OLD_QPEL(qpel8_mc31_old_c)
141 DEF_OLD_QPEL(qpel8_mc12_old_c)
142 DEF_OLD_QPEL(qpel8_mc32_old_c)
143 DEF_OLD_QPEL(qpel8_mc13_old_c)
144 DEF_OLD_QPEL(qpel8_mc33_old_c)
145
146 #define CALL_2X_PIXELS(a, b, n)\
147 static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
148     b(block  , pixels  , line_size, h);\
149     b(block+n, pixels+n, line_size, h);\
150 }
151
152 /* motion estimation */
153 // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2
154 // although currently h<4 is not used as functions with width <8 are neither used nor implemented
155 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))*/;
156
157
158 // for snow slices
159 typedef struct slice_buffer_s slice_buffer;
160
161 /**
162  * Scantable.
163  */
164 typedef struct ScanTable{
165     const uint8_t *scantable;
166     uint8_t permutated[64];
167     uint8_t raster_end[64];
168 #ifdef ARCH_POWERPC
169                 /** Used by dct_quantize_altivec to find last-non-zero */
170     DECLARE_ALIGNED(16, uint8_t, inverse[64]);
171 #endif
172 } ScanTable;
173
174 void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
175
176 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize,
177                          int block_w, int block_h,
178                          int src_x, int src_y, int w, int h);
179
180 /**
181  * DSPContext.
182  */
183 typedef struct DSPContext {
184     /* pixel ops : interface with DCT */
185     void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
186     void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
187     void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
188     void (*put_signed_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
189     void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
190     void (*add_pixels8)(uint8_t *pixels, DCTELEM *block, int line_size);
191     void (*add_pixels4)(uint8_t *pixels, DCTELEM *block, int line_size);
192     int (*sum_abs_dctelem)(DCTELEM *block/*align 16*/);
193     /**
194      * translational global motion compensation.
195      */
196     void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
197     /**
198      * global motion compensation.
199      */
200     void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
201                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
202     void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
203     int (*pix_sum)(uint8_t * pix, int line_size);
204     int (*pix_norm1)(uint8_t * pix, int line_size);
205 // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
206
207     me_cmp_func sad[5]; /* identical to pix_absAxA except additional void * */
208     me_cmp_func sse[5];
209     me_cmp_func hadamard8_diff[5];
210     me_cmp_func dct_sad[5];
211     me_cmp_func quant_psnr[5];
212     me_cmp_func bit[5];
213     me_cmp_func rd[5];
214     me_cmp_func vsad[5];
215     me_cmp_func vsse[5];
216     me_cmp_func nsse[5];
217     me_cmp_func w53[5];
218     me_cmp_func w97[5];
219     me_cmp_func dct_max[5];
220     me_cmp_func dct264_sad[5];
221
222     me_cmp_func me_pre_cmp[5];
223     me_cmp_func me_cmp[5];
224     me_cmp_func me_sub_cmp[5];
225     me_cmp_func mb_cmp[5];
226     me_cmp_func ildct_cmp[5]; //only width 16 used
227     me_cmp_func frame_skip_cmp[5]; //only width 8 used
228
229     int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
230                              int size);
231
232     /**
233      * Halfpel motion compensation with rounding (a+b+1)>>1.
234      * this is an array[4][4] of motion compensation functions for 4
235      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
236      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
237      * @param block destination where the result is stored
238      * @param pixels source
239      * @param line_size number of bytes in a horizontal line of block
240      * @param h height
241      */
242     op_pixels_func put_pixels_tab[4][4];
243
244     /**
245      * Halfpel motion compensation with rounding (a+b+1)>>1.
246      * This is an array[4][4] of motion compensation functions for 4
247      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
248      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
249      * @param block destination into which the result is averaged (a+b+1)>>1
250      * @param pixels source
251      * @param line_size number of bytes in a horizontal line of block
252      * @param h height
253      */
254     op_pixels_func avg_pixels_tab[4][4];
255
256     /**
257      * Halfpel motion compensation with no rounding (a+b)>>1.
258      * this is an array[2][4] of motion compensation functions for 2
259      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
260      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
261      * @param block destination where the result is stored
262      * @param pixels source
263      * @param line_size number of bytes in a horizontal line of block
264      * @param h height
265      */
266     op_pixels_func put_no_rnd_pixels_tab[4][4];
267
268     /**
269      * Halfpel motion compensation with no rounding (a+b)>>1.
270      * this is an array[2][4] of motion compensation functions for 2
271      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
272      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
273      * @param block destination into which the result is averaged (a+b)>>1
274      * @param pixels source
275      * @param line_size number of bytes in a horizontal line of block
276      * @param h height
277      */
278     op_pixels_func avg_no_rnd_pixels_tab[4][4];
279
280     void (*put_no_rnd_pixels_l2[2])(uint8_t *block/*align width (8 or 16)*/, const uint8_t *a/*align 1*/, const uint8_t *b/*align 1*/, int line_size, int h);
281
282     /**
283      * Thirdpel motion compensation with rounding (a+b+1)>>1.
284      * this is an array[12] of motion compensation functions for the 9 thirdpe
285      * positions<br>
286      * *pixels_tab[ xthirdpel + 4*ythirdpel ]
287      * @param block destination where the result is stored
288      * @param pixels source
289      * @param line_size number of bytes in a horizontal line of block
290      * @param h height
291      */
292     tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
293     tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
294
295     qpel_mc_func put_qpel_pixels_tab[2][16];
296     qpel_mc_func avg_qpel_pixels_tab[2][16];
297     qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
298     qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
299     qpel_mc_func put_mspel_pixels_tab[8];
300
301     /**
302      * h264 Chroma MC
303      */
304     h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
305     /* This is really one func used in VC-1 decoding */
306     h264_chroma_mc_func put_no_rnd_h264_chroma_pixels_tab[3];
307     h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
308
309     qpel_mc_func put_h264_qpel_pixels_tab[4][16];
310     qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
311
312     qpel_mc_func put_2tap_qpel_pixels_tab[4][16];
313     qpel_mc_func avg_2tap_qpel_pixels_tab[4][16];
314
315     h264_weight_func weight_h264_pixels_tab[10];
316     h264_biweight_func biweight_h264_pixels_tab[10];
317
318     /* AVS specific */
319     qpel_mc_func put_cavs_qpel_pixels_tab[2][16];
320     qpel_mc_func avg_cavs_qpel_pixels_tab[2][16];
321     void (*cavs_filter_lv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
322     void (*cavs_filter_lh)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
323     void (*cavs_filter_cv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
324     void (*cavs_filter_ch)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
325     void (*cavs_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
326
327     me_cmp_func pix_abs[2][4];
328
329     /* huffyuv specific */
330     void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
331     void (*add_bytes_l2)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 16*/, int w);
332     void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
333     /**
334      * subtract huffyuv's variant of median prediction
335      * note, this might read from src1[-1], src2[-1]
336      */
337     void (*sub_hfyu_median_prediction)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top);
338     /* this might write to dst[w] */
339     void (*add_png_paeth_prediction)(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp);
340     void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
341
342     void (*h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
343     void (*h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
344     void (*h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
345     void (*h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0);
346     void (*h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
347     void (*h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta);
348     // h264_loop_filter_strength: simd only. the C version is inlined in h264.c
349     void (*h264_loop_filter_strength)(int16_t bS[2][4][4], uint8_t nnz[40], int8_t ref[2][40], int16_t mv[2][40][2],
350                                       int bidir, int edges, int step, int mask_mv0, int mask_mv1);
351
352     void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
353     void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
354
355     void (*h261_loop_filter)(uint8_t *src, int stride);
356
357     void (*x8_v_loop_filter)(uint8_t *src, int stride, int qscale);
358     void (*x8_h_loop_filter)(uint8_t *src, int stride, int qscale);
359
360     /* assume len is a multiple of 4, and arrays are 16-byte aligned */
361     void (*vorbis_inverse_coupling)(float *mag, float *ang, int blocksize);
362     /* no alignment needed */
363     void (*flac_compute_autocorr)(const int32_t *data, int len, int lag, double *autoc);
364     /* assume len is a multiple of 8, and arrays are 16-byte aligned */
365     void (*vector_fmul)(float *dst, const float *src, int len);
366     void (*vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len);
367     /* assume len is a multiple of 8, and src arrays are 16-byte aligned */
368     void (*vector_fmul_add_add)(float *dst, const float *src0, const float *src1, const float *src2, int src3, int len, int step);
369     /* assume len is a multiple of 4, and arrays are 16-byte aligned */
370     void (*vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, float add_bias, int len);
371
372     /* C version: convert floats from the range [384.0,386.0] to ints in [-32768,32767]
373      * simd versions: convert floats from [-32768.0,32767.0] without rescaling and arrays are 16byte aligned */
374     void (*float_to_int16)(int16_t *dst, const float *src, long len);
375     void (*float_to_int16_interleave)(int16_t *dst, const float **src, long len, int channels);
376
377     /* (I)DCT */
378     void (*fdct)(DCTELEM *block/* align 16*/);
379     void (*fdct248)(DCTELEM *block/* align 16*/);
380
381     /* IDCT really*/
382     void (*idct)(DCTELEM *block/* align 16*/);
383
384     /**
385      * block -> idct -> clip to unsigned 8 bit -> dest.
386      * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
387      * @param line_size size in bytes of a horizontal line of dest
388      */
389     void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
390
391     /**
392      * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
393      * @param line_size size in bytes of a horizontal line of dest
394      */
395     void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
396
397     /**
398      * idct input permutation.
399      * several optimized IDCTs need a permutated input (relative to the normal order of the reference
400      * IDCT)
401      * this permutation must be performed before the idct_put/add, note, normally this can be merged
402      * with the zigzag/alternate scan<br>
403      * an example to avoid confusion:
404      * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
405      * - (x -> referece dct -> reference idct -> x)
406      * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
407      * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
408      */
409     uint8_t idct_permutation[64];
410     int idct_permutation_type;
411 #define FF_NO_IDCT_PERM 1
412 #define FF_LIBMPEG2_IDCT_PERM 2
413 #define FF_SIMPLE_IDCT_PERM 3
414 #define FF_TRANSPOSE_IDCT_PERM 4
415 #define FF_PARTTRANS_IDCT_PERM 5
416 #define FF_SSE2_IDCT_PERM 6
417
418     int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
419     void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
420 #define BASIS_SHIFT 16
421 #define RECON_SHIFT 6
422
423     void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
424 #define EDGE_WIDTH 16
425
426     /* h264 functions */
427     void (*h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride);
428     void (*h264_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
429     void (*h264_idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
430     void (*h264_idct8_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
431     void (*h264_dct)(DCTELEM block[4][4]);
432
433     /* snow wavelet */
434     void (*vertical_compose97i)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
435     void (*horizontal_compose97i)(IDWTELEM *b, int width);
436     void (*inner_add_yblock)(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
437
438     void (*prefetch)(void *mem, int stride, int h);
439
440     void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
441
442     /* vc1 functions */
443     void (*vc1_inv_trans_8x8)(DCTELEM *b);
444     void (*vc1_inv_trans_8x4)(uint8_t *dest, int line_size, DCTELEM *block);
445     void (*vc1_inv_trans_4x8)(uint8_t *dest, int line_size, DCTELEM *block);
446     void (*vc1_inv_trans_4x4)(uint8_t *dest, int line_size, DCTELEM *block);
447     void (*vc1_v_overlap)(uint8_t* src, int stride);
448     void (*vc1_h_overlap)(uint8_t* src, int stride);
449     /* put 8x8 block with bicubic interpolation and quarterpel precision
450      * last argument is actually round value instead of height
451      */
452     op_pixels_func put_vc1_mspel_pixels_tab[16];
453
454     /* intrax8 functions */
455     void (*x8_spatial_compensation[12])(uint8_t *src , uint8_t *dst, int linesize);
456     void (*x8_setup_spatial_compensation)(uint8_t *src, uint8_t *dst, int linesize,
457            int * range, int * sum,  int edges);
458
459     /* ape functions */
460     /**
461      * Add contents of the second vector to the first one.
462      * @param len length of vectors, should be multiple of 16
463      */
464     void (*add_int16)(int16_t *v1/*align 16*/, int16_t *v2, int len);
465     /**
466      * Add contents of the second vector to the first one.
467      * @param len length of vectors, should be multiple of 16
468      */
469     void (*sub_int16)(int16_t *v1/*align 16*/, int16_t *v2, int len);
470     /**
471      * Calculate scalar product of two vectors.
472      * @param len length of vectors, should be multiple of 16
473      * @param shift number of bits to discard from product
474      */
475     int32_t (*scalarproduct_int16)(int16_t *v1, int16_t *v2/*align 16*/, int len, int shift);
476 } DSPContext;
477
478 void dsputil_static_init(void);
479 void dsputil_init(DSPContext* p, AVCodecContext *avctx);
480
481 int ff_check_alignment(void);
482
483 /**
484  * permute block according to permuatation.
485  * @param last last non zero element in scantable order
486  */
487 void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
488
489 void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
490
491 #define         BYTE_VEC32(c)   ((c)*0x01010101UL)
492
493 static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
494 {
495     return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
496 }
497
498 static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
499 {
500     return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
501 }
502
503 static inline int get_penalty_factor(int lambda, int lambda2, int type){
504     switch(type&0xFF){
505     default:
506     case FF_CMP_SAD:
507         return lambda>>FF_LAMBDA_SHIFT;
508     case FF_CMP_DCT:
509         return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
510     case FF_CMP_W53:
511         return (4*lambda)>>(FF_LAMBDA_SHIFT);
512     case FF_CMP_W97:
513         return (2*lambda)>>(FF_LAMBDA_SHIFT);
514     case FF_CMP_SATD:
515     case FF_CMP_DCT264:
516         return (2*lambda)>>FF_LAMBDA_SHIFT;
517     case FF_CMP_RD:
518     case FF_CMP_PSNR:
519     case FF_CMP_SSE:
520     case FF_CMP_NSSE:
521         return lambda2>>FF_LAMBDA_SHIFT;
522     case FF_CMP_BIT:
523         return 1;
524     }
525 }
526
527 /**
528  * Empty mmx state.
529  * this must be called between any dsp function and float/double code.
530  * for example sin(); dsp->idct_put(); emms_c(); cos()
531  */
532 #define emms_c()
533
534 /* should be defined by architectures supporting
535    one or more MultiMedia extension */
536 int mm_support(void);
537
538 void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
539 void dsputil_init_armv4l(DSPContext* c, AVCodecContext *avctx);
540 void dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
541 void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
542 void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
543 void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
544 void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
545 void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
546 void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
547
548 #define DECLARE_ALIGNED_16(t, v) DECLARE_ALIGNED(16, t, v)
549
550 #if defined(HAVE_MMX)
551
552 #undef emms_c
553
554 #define MM_MMX    0x0001 /* standard MMX */
555 #define MM_3DNOW  0x0004 /* AMD 3DNOW */
556 #define MM_MMXEXT 0x0002 /* SSE integer functions or AMD MMX ext */
557 #define MM_SSE    0x0008 /* SSE functions */
558 #define MM_SSE2   0x0010 /* PIV SSE2 functions */
559 #define MM_3DNOWEXT  0x0020 /* AMD 3DNowExt */
560 #define MM_SSE3   0x0040 /* Prescott SSE3 functions */
561 #define MM_SSSE3  0x0080 /* Conroe SSSE3 functions */
562
563 extern int mm_flags;
564
565 void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
566 void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
567 void put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size);
568
569 static inline void emms(void)
570 {
571     asm volatile ("emms;":::"memory");
572 }
573
574
575 #define emms_c() \
576 {\
577     if (mm_flags & MM_MMX)\
578         emms();\
579 }
580
581 void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
582
583 #elif defined(ARCH_ARMV4L)
584
585 #define MM_IWMMXT    0x0100 /* XScale IWMMXT */
586
587 extern int mm_flags;
588
589 #elif defined(ARCH_POWERPC)
590
591 #define MM_ALTIVEC    0x0001 /* standard AltiVec */
592
593 extern int mm_flags;
594
595 #define DECLARE_ALIGNED_8(t, v) DECLARE_ALIGNED(16, t, v)
596 #define STRIDE_ALIGN 16
597
598 #elif defined(HAVE_MMI)
599
600 #define DECLARE_ALIGNED_8(t, v) DECLARE_ALIGNED(16, t, v)
601 #define STRIDE_ALIGN 16
602
603 #else
604
605 #define mm_flags 0
606 #define mm_support() 0
607
608 #endif
609
610 #ifndef DECLARE_ALIGNED_8
611 #   define DECLARE_ALIGNED_8(t, v) DECLARE_ALIGNED(8, t, v)
612 #endif
613
614 #ifndef STRIDE_ALIGN
615 #   define STRIDE_ALIGN 8
616 #endif
617
618 /* PSNR */
619 void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
620               int orig_linesize[3], int coded_linesize,
621               AVCodecContext *avctx);
622
623 /* FFT computation */
624
625 /* NOTE: soon integer code will be added, so you must use the
626    FFTSample type */
627 typedef float FFTSample;
628
629 struct MDCTContext;
630
631 typedef struct FFTComplex {
632     FFTSample re, im;
633 } FFTComplex;
634
635 typedef struct FFTContext {
636     int nbits;
637     int inverse;
638     uint16_t *revtab;
639     FFTComplex *exptab;
640     FFTComplex *exptab1; /* only used by SSE code */
641     void (*fft_calc)(struct FFTContext *s, FFTComplex *z);
642     void (*imdct_calc)(struct MDCTContext *s, FFTSample *output,
643                        const FFTSample *input, FFTSample *tmp);
644     void (*imdct_half)(struct MDCTContext *s, FFTSample *output,
645                        const FFTSample *input, FFTSample *tmp);
646 } FFTContext;
647
648 int ff_fft_init(FFTContext *s, int nbits, int inverse);
649 void ff_fft_permute(FFTContext *s, FFTComplex *z);
650 void ff_fft_calc_c(FFTContext *s, FFTComplex *z);
651 void ff_fft_calc_sse(FFTContext *s, FFTComplex *z);
652 void ff_fft_calc_3dn(FFTContext *s, FFTComplex *z);
653 void ff_fft_calc_3dn2(FFTContext *s, FFTComplex *z);
654 void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z);
655
656 static inline void ff_fft_calc(FFTContext *s, FFTComplex *z)
657 {
658     s->fft_calc(s, z);
659 }
660 void ff_fft_end(FFTContext *s);
661
662 /* MDCT computation */
663
664 typedef struct MDCTContext {
665     int n;  /* size of MDCT (i.e. number of input data * 2) */
666     int nbits; /* n = 2^nbits */
667     /* pre/post rotation tables */
668     FFTSample *tcos;
669     FFTSample *tsin;
670     FFTContext fft;
671 } MDCTContext;
672
673 /**
674  * Generate a Kaiser-Bessel Derived Window.
675  * @param   window  pointer to half window
676  * @param   alpha   determines window shape
677  * @param   n       size of half window
678  */
679 void ff_kbd_window_init(float *window, float alpha, int n);
680
681 /**
682  * Generate a sine window.
683  * @param   window  pointer to half window
684  * @param   n       size of half window
685  */
686 void ff_sine_window_init(float *window, int n);
687
688 int ff_mdct_init(MDCTContext *s, int nbits, int inverse);
689 void ff_imdct_calc(MDCTContext *s, FFTSample *output,
690                 const FFTSample *input, FFTSample *tmp);
691 void ff_imdct_half(MDCTContext *s, FFTSample *output,
692                    const FFTSample *input, FFTSample *tmp);
693 void ff_imdct_calc_3dn2(MDCTContext *s, FFTSample *output,
694                         const FFTSample *input, FFTSample *tmp);
695 void ff_imdct_half_3dn2(MDCTContext *s, FFTSample *output,
696                         const FFTSample *input, FFTSample *tmp);
697 void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
698                        const FFTSample *input, FFTSample *tmp);
699 void ff_imdct_half_sse(MDCTContext *s, FFTSample *output,
700                        const FFTSample *input, FFTSample *tmp);
701 void ff_mdct_calc(MDCTContext *s, FFTSample *out,
702                const FFTSample *input, FFTSample *tmp);
703 void ff_mdct_end(MDCTContext *s);
704
705 #define WRAPPER8_16(name8, name16)\
706 static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
707     return name8(s, dst           , src           , stride, h)\
708           +name8(s, dst+8         , src+8         , stride, h);\
709 }
710
711 #define WRAPPER8_16_SQ(name8, name16)\
712 static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
713     int score=0;\
714     score +=name8(s, dst           , src           , stride, 8);\
715     score +=name8(s, dst+8         , src+8         , stride, 8);\
716     if(h==16){\
717         dst += 8*stride;\
718         src += 8*stride;\
719         score +=name8(s, dst           , src           , stride, 8);\
720         score +=name8(s, dst+8         , src+8         , stride, 8);\
721     }\
722     return score;\
723 }
724
725
726 static inline void copy_block2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
727 {
728     int i;
729     for(i=0; i<h; i++)
730     {
731         AV_WN16(dst   , AV_RN16(src   ));
732         dst+=dstStride;
733         src+=srcStride;
734     }
735 }
736
737 static inline void copy_block4(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
738 {
739     int i;
740     for(i=0; i<h; i++)
741     {
742         AV_WN32(dst   , AV_RN32(src   ));
743         dst+=dstStride;
744         src+=srcStride;
745     }
746 }
747
748 static inline void copy_block8(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
749 {
750     int i;
751     for(i=0; i<h; i++)
752     {
753         AV_WN32(dst   , AV_RN32(src   ));
754         AV_WN32(dst+4 , AV_RN32(src+4 ));
755         dst+=dstStride;
756         src+=srcStride;
757     }
758 }
759
760 static inline void copy_block9(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
761 {
762     int i;
763     for(i=0; i<h; i++)
764     {
765         AV_WN32(dst   , AV_RN32(src   ));
766         AV_WN32(dst+4 , AV_RN32(src+4 ));
767         dst[8]= src[8];
768         dst+=dstStride;
769         src+=srcStride;
770     }
771 }
772
773 static inline void copy_block16(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
774 {
775     int i;
776     for(i=0; i<h; i++)
777     {
778         AV_WN32(dst   , AV_RN32(src   ));
779         AV_WN32(dst+4 , AV_RN32(src+4 ));
780         AV_WN32(dst+8 , AV_RN32(src+8 ));
781         AV_WN32(dst+12, AV_RN32(src+12));
782         dst+=dstStride;
783         src+=srcStride;
784     }
785 }
786
787 static inline void copy_block17(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h)
788 {
789     int i;
790     for(i=0; i<h; i++)
791     {
792         AV_WN32(dst   , AV_RN32(src   ));
793         AV_WN32(dst+4 , AV_RN32(src+4 ));
794         AV_WN32(dst+8 , AV_RN32(src+8 ));
795         AV_WN32(dst+12, AV_RN32(src+12));
796         dst[16]= src[16];
797         dst+=dstStride;
798         src+=srcStride;
799     }
800 }
801
802 #endif /* FFMPEG_DSPUTIL_H */