97c08fe5d2eb011674c85e7e71d22ee5980f6c63
[libav.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 libavcodec/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 AVCODEC_DSPUTIL_H
31 #define AVCODEC_DSPUTIL_H
32
33 #include "libavutil/intreadwrite.h"
34 #include "avcodec.h"
35
36
37 //#define DEBUG
38 /* dct code */
39 typedef short DCTELEM;
40 typedef int DWTELEM;
41 typedef short IDWTELEM;
42
43 void fdct_ifast (DCTELEM *data);
44 void fdct_ifast248 (DCTELEM *data);
45 void ff_jpeg_fdct_islow (DCTELEM *data);
46 void ff_fdct248_islow (DCTELEM *data);
47
48 void j_rev_dct (DCTELEM *data);
49 void j_rev_dct4 (DCTELEM *data);
50 void j_rev_dct2 (DCTELEM *data);
51 void j_rev_dct1 (DCTELEM *data);
52 void ff_wmv2_idct_c(DCTELEM *data);
53
54 void ff_fdct_mmx(DCTELEM *block);
55 void ff_fdct_mmx2(DCTELEM *block);
56 void ff_fdct_sse2(DCTELEM *block);
57
58 void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride);
59 void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride);
60 void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
61 void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride);
62 void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block);
63 void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block);
64 void ff_h264_idct_add16_c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
65 void ff_h264_idct_add16intra_c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
66 void ff_h264_idct8_add4_c(uint8_t *dst, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
67 void ff_h264_idct_add8_c(uint8_t **dest, const int *blockoffset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
68
69 void ff_vector_fmul_window_c(float *dst, const float *src0, const float *src1,
70 const float *win, float add_bias, int len);
71 void ff_float_to_int16_c(int16_t *dst, const float *src, long len);
72 void ff_float_to_int16_interleave_c(int16_t *dst, const float **src, long len, int channels);
73
74 /* encoding scans */
75 extern const uint8_t ff_alternate_horizontal_scan[64];
76 extern const uint8_t ff_alternate_vertical_scan[64];
77 extern const uint8_t ff_zigzag_direct[64];
78 extern const uint8_t ff_zigzag248_direct[64];
79
80 /* pixel operations */
81 #define MAX_NEG_CROP 1024
82
83 /* temporary */
84 extern uint32_t ff_squareTbl[512];
85 extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
86
87 /* VP3 DSP functions */
88 void ff_vp3_idct_c(DCTELEM *block/* align 16*/);
89 void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
90 void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
91
92 void ff_vp3_v_loop_filter_c(uint8_t *src, int stride, int *bounding_values);
93 void ff_vp3_h_loop_filter_c(uint8_t *src, int stride, int *bounding_values);
94
95 /* VP6 DSP functions */
96 void ff_vp6_filter_diag4_c(uint8_t *dst, uint8_t *src, int stride,
97 const int16_t *h_weights, const int16_t *v_weights);
98
99 /* Bink functions */
100 void ff_bink_idct_c (DCTELEM *block);
101 void ff_bink_idct_add_c(uint8_t *dest, int linesize, DCTELEM *block);
102 void ff_bink_idct_put_c(uint8_t *dest, int linesize, DCTELEM *block);
103
104 /* CAVS functions */
105 void ff_put_cavs_qpel8_mc00_c(uint8_t *dst, uint8_t *src, int stride);
106 void ff_avg_cavs_qpel8_mc00_c(uint8_t *dst, uint8_t *src, int stride);
107 void ff_put_cavs_qpel16_mc00_c(uint8_t *dst, uint8_t *src, int stride);
108 void ff_avg_cavs_qpel16_mc00_c(uint8_t *dst, uint8_t *src, int stride);
109
110 /* VC1 functions */
111 void ff_put_vc1_mspel_mc00_c(uint8_t *dst, const uint8_t *src, int stride, int rnd);
112 void ff_avg_vc1_mspel_mc00_c(uint8_t *dst, const uint8_t *src, int stride, int rnd);
113
114 /* EA functions */
115 void ff_ea_idct_put_c(uint8_t *dest, int linesize, DCTELEM *block);
116
117 /* 1/2^n downscaling functions from imgconvert.c */
118 void ff_img_copy_plane(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
119 void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
120 void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
121 void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
122
123 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
124 int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
125
126 /* minimum alignment rules ;)
127 If you notice errors in the align stuff, need more alignment for some ASM code
128 for some CPU or need to use a function with less aligned data then send a mail
129 to the ffmpeg-devel mailing list, ...
130
131 !warning These alignments might not match reality, (missing attribute((align))
132 stuff somewhere possible).
133 I (Michael) did not check them, these are just the alignments which I think
134 could be reached easily ...
135
136 !future video codecs might need functions with less strict alignment
137 */
138
139 /*
140 void get_pixels_c(DCTELEM *block, const uint8_t *pixels, int line_size);
141 void diff_pixels_c(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride);
142 void put_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
143 void add_pixels_clamped_c(const DCTELEM *block, uint8_t *pixels, int line_size);
144 void clear_blocks_c(DCTELEM *blocks);
145 */
146
147 /* add and put pixel (decoding) */
148 // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
149 //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4
150 typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
151 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);
152 typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
153 typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
154 typedef void (*h264_weight_func)(uint8_t *block, int stride, int log2_denom, int weight, int offset);
155 typedef void (*h264_biweight_func)(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset);
156
157 typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
158
159 #define DEF_OLD_QPEL(name)\
160 void ff_put_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
161 void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
162 void ff_avg_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
163
164 DEF_OLD_QPEL(qpel16_mc11_old_c)
165 DEF_OLD_QPEL(qpel16_mc31_old_c)
166 DEF_OLD_QPEL(qpel16_mc12_old_c)
167 DEF_OLD_QPEL(qpel16_mc32_old_c)
168 DEF_OLD_QPEL(qpel16_mc13_old_c)
169 DEF_OLD_QPEL(qpel16_mc33_old_c)
170 DEF_OLD_QPEL(qpel8_mc11_old_c)
171 DEF_OLD_QPEL(qpel8_mc31_old_c)
172 DEF_OLD_QPEL(qpel8_mc12_old_c)
173 DEF_OLD_QPEL(qpel8_mc32_old_c)
174 DEF_OLD_QPEL(qpel8_mc13_old_c)
175 DEF_OLD_QPEL(qpel8_mc33_old_c)
176
177 #define CALL_2X_PIXELS(a, b, n)\
178 static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
179 b(block , pixels , line_size, h);\
180 b(block+n, pixels+n, line_size, h);\
181 }
182
183 /* motion estimation */
184 // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2
185 // although currently h<4 is not used as functions with width <8 are neither used nor implemented
186 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))*/;
187
188
189 // for snow slices
190 typedef struct slice_buffer_s slice_buffer;
191
192 /**
193 * Scantable.
194 */
195 typedef struct ScanTable{
196 const uint8_t *scantable;
197 uint8_t permutated[64];
198 uint8_t raster_end[64];
199 #if ARCH_PPC
200 /** Used by dct_quantize_altivec to find last-non-zero */
201 DECLARE_ALIGNED(16, uint8_t, inverse)[64];
202 #endif
203 } ScanTable;
204
205 void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
206
207 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize,
208 int block_w, int block_h,
209 int src_x, int src_y, int w, int h);
210
211 /**
212 * DSPContext.
213 */
214 typedef struct DSPContext {
215 /* pixel ops : interface with DCT */
216 void (*get_pixels)(DCTELEM *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
217 void (*diff_pixels)(DCTELEM *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
218 void (*put_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
219 void (*put_signed_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
220 void (*put_pixels_nonclamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
221 void (*add_pixels_clamped)(const DCTELEM *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
222 void (*add_pixels8)(uint8_t *pixels, DCTELEM *block, int line_size);
223 void (*add_pixels4)(uint8_t *pixels, DCTELEM *block, int line_size);
224 int (*sum_abs_dctelem)(DCTELEM *block/*align 16*/);
225 /**
226 * translational global motion compensation.
227 */
228 void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
229 /**
230 * global motion compensation.
231 */
232 void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
233 int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
234 void (*clear_block)(DCTELEM *block/*align 16*/);
235 void (*clear_blocks)(DCTELEM *blocks/*align 16*/);
236 int (*pix_sum)(uint8_t * pix, int line_size);
237 int (*pix_norm1)(uint8_t * pix, int line_size);
238 // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
239
240 me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
241 me_cmp_func sse[6];
242 me_cmp_func hadamard8_diff[6];
243 me_cmp_func dct_sad[6];
244 me_cmp_func quant_psnr[6];
245 me_cmp_func bit[6];
246 me_cmp_func rd[6];
247 me_cmp_func vsad[6];
248 me_cmp_func vsse[6];
249 me_cmp_func nsse[6];
250 me_cmp_func w53[6];
251 me_cmp_func w97[6];
252 me_cmp_func dct_max[6];
253 me_cmp_func dct264_sad[6];
254
255 me_cmp_func me_pre_cmp[6];
256 me_cmp_func me_cmp[6];
257 me_cmp_func me_sub_cmp[6];
258 me_cmp_func mb_cmp[6];
259 me_cmp_func ildct_cmp[6]; //only width 16 used
260 me_cmp_func frame_skip_cmp[6]; //only width 8 used
261
262 int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
263 int size);
264
265 /**
266 * Halfpel motion compensation with rounding (a+b+1)>>1.
267 * this is an array[4][4] of motion compensation functions for 4
268 * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
269 * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
270 * @param block destination where the result is stored
271 * @param pixels source
272 * @param line_size number of bytes in a horizontal line of block
273 * @param h height
274 */
275 op_pixels_func put_pixels_tab[4][4];
276
277 /**
278 * Halfpel motion compensation with rounding (a+b+1)>>1.
279 * This is an array[4][4] of motion compensation functions for 4
280 * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
281 * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
282 * @param block destination into which the result is averaged (a+b+1)>>1
283 * @param pixels source
284 * @param line_size number of bytes in a horizontal line of block
285 * @param h height
286 */
287 op_pixels_func avg_pixels_tab[4][4];
288
289 /**
290 * Halfpel motion compensation with no rounding (a+b)>>1.
291 * this is an array[2][4] of motion compensation functions for 2
292 * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
293 * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
294 * @param block destination where the result is stored
295 * @param pixels source
296 * @param line_size number of bytes in a horizontal line of block
297 * @param h height
298 */
299 op_pixels_func put_no_rnd_pixels_tab[4][4];
300
301 /**
302 * Halfpel motion compensation with no rounding (a+b)>>1.
303 * this is an array[2][4] of motion compensation functions for 2
304 * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
305 * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
306 * @param block destination into which the result is averaged (a+b)>>1
307 * @param pixels source
308 * @param line_size number of bytes in a horizontal line of block
309 * @param h height
310 */
311 op_pixels_func avg_no_rnd_pixels_tab[4][4];
312
313 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);
314
315 /**
316 * Thirdpel motion compensation with rounding (a+b+1)>>1.
317 * this is an array[12] of motion compensation functions for the 9 thirdpe
318 * positions<br>
319 * *pixels_tab[ xthirdpel + 4*ythirdpel ]
320 * @param block destination where the result is stored
321 * @param pixels source
322 * @param line_size number of bytes in a horizontal line of block
323 * @param h height
324 */
325 tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
326 tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
327
328 qpel_mc_func put_qpel_pixels_tab[2][16];
329 qpel_mc_func avg_qpel_pixels_tab[2][16];
330 qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
331 qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
332 qpel_mc_func put_mspel_pixels_tab[8];
333
334 /**
335 * h264 Chroma MC
336 */
337 h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
338 h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
339 /* This is really one func used in VC-1 decoding */
340 h264_chroma_mc_func put_no_rnd_vc1_chroma_pixels_tab[3];
341 h264_chroma_mc_func avg_no_rnd_vc1_chroma_pixels_tab[3];
342
343 qpel_mc_func put_h264_qpel_pixels_tab[4][16];
344 qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
345
346 qpel_mc_func put_2tap_qpel_pixels_tab[4][16];
347 qpel_mc_func avg_2tap_qpel_pixels_tab[4][16];
348
349 h264_weight_func weight_h264_pixels_tab[10];
350 h264_biweight_func biweight_h264_pixels_tab[10];
351
352 /* AVS specific */
353 qpel_mc_func put_cavs_qpel_pixels_tab[2][16];
354 qpel_mc_func avg_cavs_qpel_pixels_tab[2][16];
355 void (*cavs_filter_lv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
356 void (*cavs_filter_lh)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
357 void (*cavs_filter_cv)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
358 void (*cavs_filter_ch)(uint8_t *pix, int stride, int alpha, int beta, int tc, int bs1, int bs2);
359 void (*cavs_idct8_add)(uint8_t *dst, DCTELEM *block, int stride);
360
361 me_cmp_func pix_abs[2][4];
362
363 /* huffyuv specific */
364 void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
365 void (*add_bytes_l2)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 16*/, int w);
366 void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
367 /**
368 * subtract huffyuv's variant of median prediction
369 * note, this might read from src1[-1], src2[-1]
370 */
371 void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
372 void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
373 int (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
374 void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
375 /* this might write to dst[w] */
376 void (*add_png_paeth_prediction)(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp);
377 void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
378
379 void (*h264_v_loop_filter_luma)(uint8_t *pix/*align 16*/, int stride, int alpha, int beta, int8_t *tc0);
380 void (*h264_h_loop_filter_luma)(uint8_t *pix/*align 4 */, int stride, int alpha, int beta, int8_t *tc0);
381 /* v/h_loop_filter_luma_intra: align 16 */
382 void (*h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta);
383 void (*h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta);
384 void (*h264_v_loop_filter_chroma)(uint8_t *pix/*align 8*/, int stride, int alpha, int beta, int8_t *tc0);
385 void (*h264_h_loop_filter_chroma)(uint8_t *pix/*align 4*/, int stride, int alpha, int beta, int8_t *tc0);
386 void (*h264_v_loop_filter_chroma_intra)(uint8_t *pix/*align 8*/, int stride, int alpha, int beta);
387 void (*h264_h_loop_filter_chroma_intra)(uint8_t *pix/*align 8*/, int stride, int alpha, int beta);
388 // h264_loop_filter_strength: simd only. the C version is inlined in h264.c
389 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],
390 int bidir, int edges, int step, int mask_mv0, int mask_mv1, int field);
391
392 void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
393 void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
394
395 void (*h261_loop_filter)(uint8_t *src, int stride);
396
397 void (*x8_v_loop_filter)(uint8_t *src, int stride, int qscale);
398 void (*x8_h_loop_filter)(uint8_t *src, int stride, int qscale);
399
400 void (*vp3_v_loop_filter)(uint8_t *src, int stride, int *bounding_values);
401 void (*vp3_h_loop_filter)(uint8_t *src, int stride, int *bounding_values);
402
403 void (*vp6_filter_diag4)(uint8_t *dst, uint8_t *src, int stride,
404 const int16_t *h_weights,const int16_t *v_weights);
405
406 /* assume len is a multiple of 4, and arrays are 16-byte aligned */
407 void (*vorbis_inverse_coupling)(float *mag, float *ang, int blocksize);
408 void (*ac3_downmix)(float (*samples)[256], float (*matrix)[2], int out_ch, int in_ch, int len);
409 /* no alignment needed */
410 void (*lpc_compute_autocorr)(const int32_t *data, int len, int lag, double *autoc);
411 /* assume len is a multiple of 8, and arrays are 16-byte aligned */
412 void (*vector_fmul)(float *dst, const float *src, int len);
413 void (*vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len);
414 /* assume len is a multiple of 8, and src arrays are 16-byte aligned */
415 void (*vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len);
416 /* assume len is a multiple of 4, and arrays are 16-byte aligned */
417 void (*vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, float add_bias, int len);
418 /* assume len is a multiple of 8, and arrays are 16-byte aligned */
419 void (*int32_to_float_fmul_scalar)(float *dst, const int *src, float mul, int len);
420 void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
421 /**
422 * Multiply a vector of floats by a scalar float. Source and
423 * destination vectors must overlap exactly or not at all.
424 * @param dst result vector, 16-byte aligned
425 * @param src input vector, 16-byte aligned
426 * @param mul scalar value
427 * @param len length of vector, multiple of 4
428 */
429 void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
430 int len);
431 /**
432 * Multiply a vector of floats by concatenated short vectors of
433 * floats and by a scalar float. Source and destination vectors
434 * must overlap exactly or not at all.
435 * [0]: short vectors of length 2, 8-byte aligned
436 * [1]: short vectors of length 4, 16-byte aligned
437 * @param dst output vector, 16-byte aligned
438 * @param src input vector, 16-byte aligned
439 * @param sv array of pointers to short vectors
440 * @param mul scalar value
441 * @param len number of elements in src and dst, multiple of 4
442 */
443 void (*vector_fmul_sv_scalar[2])(float *dst, const float *src,
444 const float **sv, float mul, int len);
445 /**
446 * Multiply short vectors of floats by a scalar float, store
447 * concatenated result.
448 * [0]: short vectors of length 2, 8-byte aligned
449 * [1]: short vectors of length 4, 16-byte aligned
450 * @param dst output vector, 16-byte aligned
451 * @param sv array of pointers to short vectors
452 * @param mul scalar value
453 * @param len number of output elements, multiple of 4
454 */
455 void (*sv_fmul_scalar[2])(float *dst, const float **sv,
456 float mul, int len);
457 /**
458 * Calculate the scalar product of two vectors of floats.
459 * @param v1 first vector, 16-byte aligned
460 * @param v2 second vector, 16-byte aligned
461 * @param len length of vectors, multiple of 4
462 */
463 float (*scalarproduct_float)(const float *v1, const float *v2, int len);
464 /**
465 * Calculate the sum and difference of two vectors of floats.
466 * @param v1 first input vector, sum output, 16-byte aligned
467 * @param v2 second input vector, difference output, 16-byte aligned
468 * @param len length of vectors, multiple of 4
469 */
470 void (*butterflies_float)(float *restrict v1, float *restrict v2, int len);
471
472 /* C version: convert floats from the range [384.0,386.0] to ints in [-32768,32767]
473 * simd versions: convert floats from [-32768.0,32767.0] without rescaling and arrays are 16byte aligned */
474 void (*float_to_int16)(int16_t *dst, const float *src, long len);
475 void (*float_to_int16_interleave)(int16_t *dst, const float **src, long len, int channels);
476
477 /* (I)DCT */
478 void (*fdct)(DCTELEM *block/* align 16*/);
479 void (*fdct248)(DCTELEM *block/* align 16*/);
480
481 /* IDCT really*/
482 void (*idct)(DCTELEM *block/* align 16*/);
483
484 /**
485 * block -> idct -> clip to unsigned 8 bit -> dest.
486 * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
487 * @param line_size size in bytes of a horizontal line of dest
488 */
489 void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
490
491 /**
492 * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
493 * @param line_size size in bytes of a horizontal line of dest
494 */
495 void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
496
497 /**
498 * idct input permutation.
499 * several optimized IDCTs need a permutated input (relative to the normal order of the reference
500 * IDCT)
501 * this permutation must be performed before the idct_put/add, note, normally this can be merged
502 * with the zigzag/alternate scan<br>
503 * an example to avoid confusion:
504 * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
505 * - (x -> referece dct -> reference idct -> x)
506 * - (x -> referece dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
507 * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
508 */
509 uint8_t idct_permutation[64];
510 int idct_permutation_type;
511 #define FF_NO_IDCT_PERM 1
512 #define FF_LIBMPEG2_IDCT_PERM 2
513 #define FF_SIMPLE_IDCT_PERM 3
514 #define FF_TRANSPOSE_IDCT_PERM 4
515 #define FF_PARTTRANS_IDCT_PERM 5
516 #define FF_SSE2_IDCT_PERM 6
517
518 int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
519 void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
520 #define BASIS_SHIFT 16
521 #define RECON_SHIFT 6
522
523 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
524 #define EDGE_WIDTH 16
525
526 /* h264 functions */
527 /* NOTE!!! if you implement any of h264_idct8_add, h264_idct8_add4 then you must implement all of them
528 NOTE!!! if you implement any of h264_idct_add, h264_idct_add16, h264_idct_add16intra, h264_idct_add8 then you must implement all of them
529 The reason for above, is that no 2 out of one list may use a different permutation.
530 */
531 void (*h264_idct_add)(uint8_t *dst/*align 4*/, DCTELEM *block/*align 16*/, int stride);
532 void (*h264_idct8_add)(uint8_t *dst/*align 8*/, DCTELEM *block/*align 16*/, int stride);
533 void (*h264_idct_dc_add)(uint8_t *dst/*align 4*/, DCTELEM *block/*align 16*/, int stride);
534 void (*h264_idct8_dc_add)(uint8_t *dst/*align 8*/, DCTELEM *block/*align 16*/, int stride);
535 void (*h264_dct)(DCTELEM block[4][4]);
536 void (*h264_idct_add16)(uint8_t *dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
537 void (*h264_idct8_add4)(uint8_t *dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
538 void (*h264_idct_add8)(uint8_t **dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
539 void (*h264_idct_add16intra)(uint8_t *dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
540
541 /* snow wavelet */
542 void (*vertical_compose97i)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
543 void (*horizontal_compose97i)(IDWTELEM *b, int width);
544 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);
545
546 void (*prefetch)(void *mem, int stride, int h);
547
548 void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
549
550 /* mlp/truehd functions */
551 void (*mlp_filter_channel)(int32_t *state, const int32_t *coeff,
552 int firorder, int iirorder,
553 unsigned int filter_shift, int32_t mask, int blocksize,
554 int32_t *sample_buffer);
555
556 /* vc1 functions */
557 void (*vc1_inv_trans_8x8)(DCTELEM *b);
558 void (*vc1_inv_trans_8x4)(uint8_t *dest, int line_size, DCTELEM *block);
559 void (*vc1_inv_trans_4x8)(uint8_t *dest, int line_size, DCTELEM *block);
560 void (*vc1_inv_trans_4x4)(uint8_t *dest, int line_size, DCTELEM *block);
561 void (*vc1_inv_trans_8x8_dc)(uint8_t *dest, int line_size, DCTELEM *block);
562 void (*vc1_inv_trans_8x4_dc)(uint8_t *dest, int line_size, DCTELEM *block);
563 void (*vc1_inv_trans_4x8_dc)(uint8_t *dest, int line_size, DCTELEM *block);
564 void (*vc1_inv_trans_4x4_dc)(uint8_t *dest, int line_size, DCTELEM *block);
565 void (*vc1_v_overlap)(uint8_t* src, int stride);
566 void (*vc1_h_overlap)(uint8_t* src, int stride);
567 void (*vc1_v_loop_filter4)(uint8_t *src, int stride, int pq);
568 void (*vc1_h_loop_filter4)(uint8_t *src, int stride, int pq);
569 void (*vc1_v_loop_filter8)(uint8_t *src, int stride, int pq);
570 void (*vc1_h_loop_filter8)(uint8_t *src, int stride, int pq);
571 void (*vc1_v_loop_filter16)(uint8_t *src, int stride, int pq);
572 void (*vc1_h_loop_filter16)(uint8_t *src, int stride, int pq);
573 /* put 8x8 block with bicubic interpolation and quarterpel precision
574 * last argument is actually round value instead of height
575 */
576 op_pixels_func put_vc1_mspel_pixels_tab[16];
577 op_pixels_func avg_vc1_mspel_pixels_tab[16];
578
579 /* intrax8 functions */
580 void (*x8_spatial_compensation[12])(uint8_t *src , uint8_t *dst, int linesize);
581 void (*x8_setup_spatial_compensation)(uint8_t *src, uint8_t *dst, int linesize,
582 int * range, int * sum, int edges);
583
584 /**
585 * Calculate scalar product of two vectors.
586 * @param len length of vectors, should be multiple of 16
587 * @param shift number of bits to discard from product
588 */
589 int32_t (*scalarproduct_int16)(int16_t *v1, int16_t *v2/*align 16*/, int len, int shift);
590 /* ape functions */
591 /**
592 * Calculate scalar product of v1 and v2,
593 * and v1[i] += v3[i] * mul
594 * @param len length of vectors, should be multiple of 16
595 */
596 int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, int16_t *v2, int16_t *v3, int len, int mul);
597
598 /* rv30 functions */
599 qpel_mc_func put_rv30_tpel_pixels_tab[4][16];
600 qpel_mc_func avg_rv30_tpel_pixels_tab[4][16];
601
602 /* rv40 functions */
603 qpel_mc_func put_rv40_qpel_pixels_tab[4][16];
604 qpel_mc_func avg_rv40_qpel_pixels_tab[4][16];
605 h264_chroma_mc_func put_rv40_chroma_pixels_tab[3];
606 h264_chroma_mc_func avg_rv40_chroma_pixels_tab[3];
607
608 /* bink functions */
609 op_fill_func fill_block_tab[2];
610 void (*scale_block)(const uint8_t src[64]/*align 8*/, uint8_t *dst/*align 8*/, int linesize);
611 } DSPContext;
612
613 void dsputil_static_init(void);
614 void dsputil_init(DSPContext* p, AVCodecContext *avctx);
615
616 int ff_check_alignment(void);
617
618 /**
619 * permute block according to permuatation.
620 * @param last last non zero element in scantable order
621 */
622 void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last);
623
624 void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
625
626 #define BYTE_VEC32(c) ((c)*0x01010101UL)
627
628 static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
629 {
630 return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
631 }
632
633 static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
634 {
635 return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
636 }
637
638 static inline int get_penalty_factor(int lambda, int lambda2, int type){
639 switch(type&0xFF){
640 default:
641 case FF_CMP_SAD:
642 return lambda>>FF_LAMBDA_SHIFT;
643 case FF_CMP_DCT:
644 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
645 case FF_CMP_W53:
646 return (4*lambda)>>(FF_LAMBDA_SHIFT);
647 case FF_CMP_W97:
648 return (2*lambda)>>(FF_LAMBDA_SHIFT);
649 case FF_CMP_SATD:
650 case FF_CMP_DCT264:
651 return (2*lambda)>>FF_LAMBDA_SHIFT;
652 case FF_CMP_RD:
653 case FF_CMP_PSNR:
654 case FF_CMP_SSE:
655 case FF_CMP_NSSE:
656 return lambda2>>FF_LAMBDA_SHIFT;
657 case FF_CMP_BIT:
658 return 1;
659 }
660 }
661
662 /**
663 * Empty mmx state.
664 * this must be called between any dsp function and float/double code.
665 * for example sin(); dsp->idct_put(); emms_c(); cos()
666 */
667 #define emms_c()
668
669 /* should be defined by architectures supporting
670 one or more MultiMedia extension */
671 int mm_support(void);
672 extern int mm_flags;
673
674 void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
675 void dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
676 void dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
677 void dsputil_init_mlib(DSPContext* c, AVCodecContext *avctx);
678 void dsputil_init_mmi(DSPContext* c, AVCodecContext *avctx);
679 void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
680 void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
681 void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
682 void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
683
684 void ff_cavsdsp_init(DSPContext* c, AVCodecContext *avctx);
685 void ff_rv30dsp_init(DSPContext* c, AVCodecContext *avctx);
686 void ff_rv40dsp_init(DSPContext* c, AVCodecContext *avctx);
687 void ff_vc1dsp_init(DSPContext* c, AVCodecContext *avctx);
688 void ff_intrax8dsp_init(DSPContext* c, AVCodecContext *avctx);
689 void ff_mlp_init(DSPContext* c, AVCodecContext *avctx);
690 void ff_mlp_init_x86(DSPContext* c, AVCodecContext *avctx);
691
692 #if HAVE_MMX
693
694 #undef emms_c
695
696 static inline void emms(void)
697 {
698 __asm__ volatile ("emms;":::"memory");
699 }
700
701
702 #define emms_c() \
703 {\
704 if (mm_flags & FF_MM_MMX)\
705 emms();\
706 }
707
708 #elif ARCH_ARM
709
710 #if HAVE_NEON
711 # define STRIDE_ALIGN 16
712 #endif
713
714 #elif ARCH_PPC
715
716 #define STRIDE_ALIGN 16
717
718 #elif HAVE_MMI
719
720 #define STRIDE_ALIGN 16
721
722 #else
723
724 #define mm_flags 0
725 #define mm_support() 0
726
727 #endif
728
729 #ifndef STRIDE_ALIGN
730 # define STRIDE_ALIGN 8
731 #endif
732
733 #define LOCAL_ALIGNED(a, t, v, s, ...) \
734 uint8_t la_##v[sizeof(t s __VA_ARGS__) + (a)]; \
735 t (*v) __VA_ARGS__ = (void *)FFALIGN((uintptr_t)la_##v, a)
736
737 #if HAVE_LOCAL_ALIGNED_8
738 # define LOCAL_ALIGNED_8(t, v, s, ...) DECLARE_ALIGNED(8, t, v) s __VA_ARGS__
739 #else
740 # define LOCAL_ALIGNED_8(t, v, s, ...) LOCAL_ALIGNED(8, t, v, s, __VA_ARGS__)
741 #endif
742
743 #if HAVE_LOCAL_ALIGNED_16
744 # define LOCAL_ALIGNED_16(t, v, s, ...) DECLARE_ALIGNED(16, t, v) s __VA_ARGS__
745 #else
746 # define LOCAL_ALIGNED_16(t, v, s, ...) LOCAL_ALIGNED(16, t, v, s, __VA_ARGS__)
747 #endif
748
749 /* PSNR */
750 void get_psnr(uint8_t *orig_image[3], uint8_t *coded_image[3],
751 int orig_linesize[3], int coded_linesize,
752 AVCodecContext *avctx);
753
754 #define WRAPPER8_16(name8, name16)\
755 static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
756 return name8(s, dst , src , stride, h)\
757 +name8(s, dst+8 , src+8 , stride, h);\
758 }
759
760 #define WRAPPER8_16_SQ(name8, name16)\
761 static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
762 int score=0;\
763 score +=name8(s, dst , src , stride, 8);\
764 score +=name8(s, dst+8 , src+8 , stride, 8);\
765 if(h==16){\
766 dst += 8*stride;\
767 src += 8*stride;\
768 score +=name8(s, dst , src , stride, 8);\
769 score +=name8(s, dst+8 , src+8 , stride, 8);\
770 }\
771 return score;\
772 }
773
774
775 static inline void copy_block2(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
776 {
777 int i;
778 for(i=0; i<h; i++)
779 {
780 AV_WN16(dst , AV_RN16(src ));
781 dst+=dstStride;
782 src+=srcStride;
783 }
784 }
785
786 static inline void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
787 {
788 int i;
789 for(i=0; i<h; i++)
790 {
791 AV_WN32(dst , AV_RN32(src ));
792 dst+=dstStride;
793 src+=srcStride;
794 }
795 }
796
797 static inline void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
798 {
799 int i;
800 for(i=0; i<h; i++)
801 {
802 AV_WN32(dst , AV_RN32(src ));
803 AV_WN32(dst+4 , AV_RN32(src+4 ));
804 dst+=dstStride;
805 src+=srcStride;
806 }
807 }
808
809 static inline void copy_block9(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
810 {
811 int i;
812 for(i=0; i<h; i++)
813 {
814 AV_WN32(dst , AV_RN32(src ));
815 AV_WN32(dst+4 , AV_RN32(src+4 ));
816 dst[8]= src[8];
817 dst+=dstStride;
818 src+=srcStride;
819 }
820 }
821
822 static inline void copy_block16(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
823 {
824 int i;
825 for(i=0; i<h; i++)
826 {
827 AV_WN32(dst , AV_RN32(src ));
828 AV_WN32(dst+4 , AV_RN32(src+4 ));
829 AV_WN32(dst+8 , AV_RN32(src+8 ));
830 AV_WN32(dst+12, AV_RN32(src+12));
831 dst+=dstStride;
832 src+=srcStride;
833 }
834 }
835
836 static inline void copy_block17(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
837 {
838 int i;
839 for(i=0; i<h; i++)
840 {
841 AV_WN32(dst , AV_RN32(src ));
842 AV_WN32(dst+4 , AV_RN32(src+4 ));
843 AV_WN32(dst+8 , AV_RN32(src+8 ));
844 AV_WN32(dst+12, AV_RN32(src+12));
845 dst[16]= src[16];
846 dst+=dstStride;
847 src+=srcStride;
848 }
849 }
850
851 #endif /* AVCODEC_DSPUTIL_H */