]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsputil.h
1e0366bf9e8ac130fabd87a97dd4fde1ed52fe87
[ffmpeg] / 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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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
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
40 void ff_fdct_ifast(int16_t *data);
41 void ff_fdct_ifast248(int16_t *data);
42 void ff_jpeg_fdct_islow_8(int16_t *data);
43 void ff_jpeg_fdct_islow_10(int16_t *data);
44 void ff_fdct248_islow_8(int16_t *data);
45 void ff_fdct248_islow_10(int16_t *data);
46
47 void ff_j_rev_dct(int16_t *data);
48
49 void ff_fdct_mmx(int16_t *block);
50 void ff_fdct_mmxext(int16_t *block);
51 void ff_fdct_sse2(int16_t *block);
52
53 #define H264_IDCT(depth) \
54 void ff_h264_idct8_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
55 void ff_h264_idct_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
56 void ff_h264_idct8_dc_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
57 void ff_h264_idct_dc_add_ ## depth ## _c(uint8_t *dst, int16_t *block, int stride);\
58 void ff_h264_idct_add16_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
59 void ff_h264_idct_add16intra_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
60 void ff_h264_idct8_add4_ ## depth ## _c(uint8_t *dst, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
61 void ff_h264_idct_add8_422_ ## depth ## _c(uint8_t **dest, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
62 void ff_h264_idct_add8_ ## depth ## _c(uint8_t **dest, const int *blockoffset, int16_t *block, int stride, const uint8_t nnzc[6*8]);\
63 void ff_h264_luma_dc_dequant_idct_ ## depth ## _c(int16_t *output, int16_t *input, int qmul);\
64 void ff_h264_chroma422_dc_dequant_idct_ ## depth ## _c(int16_t *block, int qmul);\
65 void ff_h264_chroma_dc_dequant_idct_ ## depth ## _c(int16_t *block, int qmul);
66
67 H264_IDCT( 8)
68 H264_IDCT( 9)
69 H264_IDCT(10)
70
71 void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp);
72 void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block, int stride, int qp, int dc);
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 #define PUTAVG_PIXELS(depth)\
88 void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
89 void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
90 void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
91 void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
92
93 PUTAVG_PIXELS( 8)
94 PUTAVG_PIXELS( 9)
95 PUTAVG_PIXELS(10)
96
97 #define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
98 #define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
99 #define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
100 #define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
101
102 /* RV40 functions */
103 void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
104 void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
105 void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
106 void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
107
108 /* 1/2^n downscaling functions from imgconvert.c */
109 void ff_shrink22(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
110 void ff_shrink44(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
111 void ff_shrink88(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
112
113 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
114               int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
115
116 /* minimum alignment rules ;)
117 If you notice errors in the align stuff, need more alignment for some ASM code
118 for some CPU or need to use a function with less aligned data then send a mail
119 to the libav-devel mailing list, ...
120
121 !warning These alignments might not match reality, (missing attribute((align))
122 stuff somewhere possible).
123 I (Michael) did not check them, these are just the alignments which I think
124 could be reached easily ...
125
126 !future video codecs might need functions with less strict alignment
127 */
128
129 /*
130 void get_pixels_c(int16_t *block, const uint8_t *pixels, int line_size);
131 void diff_pixels_c(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride);
132 void put_pixels_clamped_c(const int16_t *block, uint8_t *pixels, int line_size);
133 void add_pixels_clamped_c(const int16_t *block, uint8_t *pixels, int line_size);
134 void clear_blocks_c(int16_t *blocks);
135 */
136
137 /* add and put pixel (decoding) */
138 // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
139 //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
140 typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
141 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);
142 typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
143 typedef void (*h264_chroma_mc_func)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x, int y);
144
145 typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
146
147 #define DEF_OLD_QPEL(name)\
148 void ff_put_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
149 void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
150 void ff_avg_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
151
152 DEF_OLD_QPEL(qpel16_mc11_old_c)
153 DEF_OLD_QPEL(qpel16_mc31_old_c)
154 DEF_OLD_QPEL(qpel16_mc12_old_c)
155 DEF_OLD_QPEL(qpel16_mc32_old_c)
156 DEF_OLD_QPEL(qpel16_mc13_old_c)
157 DEF_OLD_QPEL(qpel16_mc33_old_c)
158 DEF_OLD_QPEL(qpel8_mc11_old_c)
159 DEF_OLD_QPEL(qpel8_mc31_old_c)
160 DEF_OLD_QPEL(qpel8_mc12_old_c)
161 DEF_OLD_QPEL(qpel8_mc32_old_c)
162 DEF_OLD_QPEL(qpel8_mc13_old_c)
163 DEF_OLD_QPEL(qpel8_mc33_old_c)
164
165 #define CALL_2X_PIXELS(a, b, n)\
166 static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
167     b(block  , pixels  , line_size, h);\
168     b(block+n, pixels+n, line_size, h);\
169 }
170
171 /* motion estimation */
172 // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
173 // although currently h<4 is not used as functions with width <8 are neither used nor implemented
174 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))*/;
175
176 /**
177  * Scantable.
178  */
179 typedef struct ScanTable{
180     const uint8_t *scantable;
181     uint8_t permutated[64];
182     uint8_t raster_end[64];
183 } ScanTable;
184
185 void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
186 void ff_init_scantable_permutation(uint8_t *idct_permutation,
187                                    int idct_permutation_type);
188
189 /**
190  * DSPContext.
191  */
192 typedef struct DSPContext {
193     /**
194      * Size of DCT coefficients.
195      */
196     int dct_bits;
197
198     /* pixel ops : interface with DCT */
199     void (*get_pixels)(int16_t *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
200     void (*diff_pixels)(int16_t *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
201     void (*put_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
202     void (*put_signed_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
203     void (*add_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
204     void (*add_pixels8)(uint8_t *pixels, int16_t *block, int line_size);
205     void (*add_pixels4)(uint8_t *pixels, int16_t *block, int line_size);
206     int (*sum_abs_dctelem)(int16_t *block/*align 16*/);
207     /**
208      * translational global motion compensation.
209      */
210     void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
211     /**
212      * global motion compensation.
213      */
214     void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
215                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
216     void (*clear_block)(int16_t *block/*align 16*/);
217     void (*clear_blocks)(int16_t *blocks/*align 16*/);
218     int (*pix_sum)(uint8_t * pix, int line_size);
219     int (*pix_norm1)(uint8_t * pix, int line_size);
220 // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
221
222     me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
223     me_cmp_func sse[6];
224     me_cmp_func hadamard8_diff[6];
225     me_cmp_func dct_sad[6];
226     me_cmp_func quant_psnr[6];
227     me_cmp_func bit[6];
228     me_cmp_func rd[6];
229     me_cmp_func vsad[6];
230     me_cmp_func vsse[6];
231     me_cmp_func nsse[6];
232     me_cmp_func dct_max[6];
233     me_cmp_func dct264_sad[6];
234
235     me_cmp_func me_pre_cmp[6];
236     me_cmp_func me_cmp[6];
237     me_cmp_func me_sub_cmp[6];
238     me_cmp_func mb_cmp[6];
239     me_cmp_func ildct_cmp[6]; //only width 16 used
240     me_cmp_func frame_skip_cmp[6]; //only width 8 used
241
242     int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
243                              int size);
244
245     /**
246      * Halfpel motion compensation with rounding (a+b+1)>>1.
247      * this is an array[4][4] of motion compensation functions for 4
248      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
249      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
250      * @param block destination where the result is stored
251      * @param pixels source
252      * @param line_size number of bytes in a horizontal line of block
253      * @param h height
254      */
255     op_pixels_func put_pixels_tab[4][4];
256
257     /**
258      * Halfpel motion compensation with rounding (a+b+1)>>1.
259      * This is an array[4][4] of motion compensation functions for 4
260      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
261      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
262      * @param block destination into which the result is averaged (a+b+1)>>1
263      * @param pixels source
264      * @param line_size number of bytes in a horizontal line of block
265      * @param h height
266      */
267     op_pixels_func avg_pixels_tab[4][4];
268
269     /**
270      * Halfpel motion compensation with no rounding (a+b)>>1.
271      * this is an array[2][4] of motion compensation functions for 2
272      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
273      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
274      * @param block destination where the result is stored
275      * @param pixels source
276      * @param line_size number of bytes in a horizontal line of block
277      * @param h height
278      */
279     op_pixels_func put_no_rnd_pixels_tab[4][4];
280
281     /**
282      * Halfpel motion compensation with no rounding (a+b)>>1.
283      * this is an array[2][4] of motion compensation functions for 2
284      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
285      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
286      * @param block destination into which the result is averaged (a+b)>>1
287      * @param pixels source
288      * @param line_size number of bytes in a horizontal line of block
289      * @param h height
290      */
291     op_pixels_func avg_no_rnd_pixels_tab[4][4];
292
293     /**
294      * Thirdpel motion compensation with rounding (a+b+1)>>1.
295      * this is an array[12] of motion compensation functions for the 9 thirdpe
296      * positions<br>
297      * *pixels_tab[ xthirdpel + 4*ythirdpel ]
298      * @param block destination where the result is stored
299      * @param pixels source
300      * @param line_size number of bytes in a horizontal line of block
301      * @param h height
302      */
303     tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
304     tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
305
306     qpel_mc_func put_qpel_pixels_tab[2][16];
307     qpel_mc_func avg_qpel_pixels_tab[2][16];
308     qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
309     qpel_mc_func avg_no_rnd_qpel_pixels_tab[2][16];
310     qpel_mc_func put_mspel_pixels_tab[8];
311
312     /**
313      * h264 Chroma MC
314      */
315     h264_chroma_mc_func put_h264_chroma_pixels_tab[3];
316     h264_chroma_mc_func avg_h264_chroma_pixels_tab[3];
317
318     qpel_mc_func put_h264_qpel_pixels_tab[4][16];
319     qpel_mc_func avg_h264_qpel_pixels_tab[4][16];
320
321     me_cmp_func pix_abs[2][4];
322
323     /* huffyuv specific */
324     void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
325     void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
326     /**
327      * subtract huffyuv's variant of median prediction
328      * note, this might read from src1[-1], src2[-1]
329      */
330     void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
331     void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
332     int  (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
333     void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
334     void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
335     void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
336
337     void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
338     void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
339
340     void (*h261_loop_filter)(uint8_t *src, int stride);
341
342     /* assume len is a multiple of 8, and arrays are 16-byte aligned */
343     void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
344
345     /* (I)DCT */
346     void (*fdct)(int16_t *block/* align 16*/);
347     void (*fdct248)(int16_t *block/* align 16*/);
348
349     /* IDCT really*/
350     void (*idct)(int16_t *block/* align 16*/);
351
352     /**
353      * block -> idct -> clip to unsigned 8 bit -> dest.
354      * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
355      * @param line_size size in bytes of a horizontal line of dest
356      */
357     void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
358
359     /**
360      * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
361      * @param line_size size in bytes of a horizontal line of dest
362      */
363     void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
364
365     /**
366      * idct input permutation.
367      * several optimized IDCTs need a permutated input (relative to the normal order of the reference
368      * IDCT)
369      * this permutation must be performed before the idct_put/add, note, normally this can be merged
370      * with the zigzag/alternate scan<br>
371      * an example to avoid confusion:
372      * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
373      * - (x -> reference dct -> reference idct -> x)
374      * - (x -> reference dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
375      * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
376      */
377     uint8_t idct_permutation[64];
378     int idct_permutation_type;
379 #define FF_NO_IDCT_PERM 1
380 #define FF_LIBMPEG2_IDCT_PERM 2
381 #define FF_SIMPLE_IDCT_PERM 3
382 #define FF_TRANSPOSE_IDCT_PERM 4
383 #define FF_PARTTRANS_IDCT_PERM 5
384 #define FF_SSE2_IDCT_PERM 6
385
386     int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
387     void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
388 #define BASIS_SHIFT 16
389 #define RECON_SHIFT 6
390
391     void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides);
392 #define EDGE_WIDTH 16
393 #define EDGE_TOP    1
394 #define EDGE_BOTTOM 2
395
396     void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
397
398     /**
399      * Calculate scalar product of two vectors.
400      * @param len length of vectors, should be multiple of 16
401      */
402     int32_t (*scalarproduct_int16)(const int16_t *v1, const int16_t *v2/*align 16*/, int len);
403     /* ape functions */
404     /**
405      * Calculate scalar product of v1 and v2,
406      * and v1[i] += v3[i] * mul
407      * @param len length of vectors, should be multiple of 16
408      */
409     int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, const int16_t *v2, const int16_t *v3, int len, int mul);
410
411     /**
412      * Apply symmetric window in 16-bit fixed-point.
413      * @param output destination array
414      *               constraints: 16-byte aligned
415      * @param input  source array
416      *               constraints: 16-byte aligned
417      * @param window window array
418      *               constraints: 16-byte aligned, at least len/2 elements
419      * @param len    full window length
420      *               constraints: multiple of ? greater than zero
421      */
422     void (*apply_window_int16)(int16_t *output, const int16_t *input,
423                                const int16_t *window, unsigned int len);
424
425     /**
426      * Clip each element in an array of int32_t to a given minimum and maximum value.
427      * @param dst  destination array
428      *             constraints: 16-byte aligned
429      * @param src  source array
430      *             constraints: 16-byte aligned
431      * @param min  minimum value
432      *             constraints: must be in the range [-(1 << 24), 1 << 24]
433      * @param max  maximum value
434      *             constraints: must be in the range [-(1 << 24), 1 << 24]
435      * @param len  number of elements in the array
436      *             constraints: multiple of 32 greater than zero
437      */
438     void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
439                               int32_t max, unsigned int len);
440
441     op_fill_func fill_block_tab[2];
442 } DSPContext;
443
444 void ff_dsputil_static_init(void);
445 void ff_dsputil_init(DSPContext* p, AVCodecContext *avctx);
446
447 int ff_check_alignment(void);
448
449 /**
450  * permute block according to permuatation.
451  * @param last last non zero element in scantable order
452  */
453 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last);
454
455 void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
456
457 #define         BYTE_VEC32(c)   ((c)*0x01010101UL)
458 #define         BYTE_VEC64(c)   ((c)*0x0001000100010001UL)
459
460 static inline uint32_t rnd_avg32(uint32_t a, uint32_t b)
461 {
462     return (a | b) - (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
463 }
464
465 static inline uint32_t no_rnd_avg32(uint32_t a, uint32_t b)
466 {
467     return (a & b) + (((a ^ b) & ~BYTE_VEC32(0x01)) >> 1);
468 }
469
470 static inline uint64_t rnd_avg64(uint64_t a, uint64_t b)
471 {
472     return (a | b) - (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
473 }
474
475 static inline uint64_t no_rnd_avg64(uint64_t a, uint64_t b)
476 {
477     return (a & b) + (((a ^ b) & ~BYTE_VEC64(0x01)) >> 1);
478 }
479
480 static inline int get_penalty_factor(int lambda, int lambda2, int type){
481     switch(type&0xFF){
482     default:
483     case FF_CMP_SAD:
484         return lambda>>FF_LAMBDA_SHIFT;
485     case FF_CMP_DCT:
486         return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
487     case FF_CMP_SATD:
488     case FF_CMP_DCT264:
489         return (2*lambda)>>FF_LAMBDA_SHIFT;
490     case FF_CMP_RD:
491     case FF_CMP_PSNR:
492     case FF_CMP_SSE:
493     case FF_CMP_NSSE:
494         return lambda2>>FF_LAMBDA_SHIFT;
495     case FF_CMP_BIT:
496         return 1;
497     }
498 }
499
500 void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
501 void ff_dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
502 void ff_dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
503 void ff_dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
504 void ff_dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
505 void ff_dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
506 void ff_dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
507
508 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
509 #   define STRIDE_ALIGN 16
510 #else
511 #   define STRIDE_ALIGN 8
512 #endif
513
514 // Some broken preprocessors need a second expansion
515 // to be forced to tokenize __VA_ARGS__
516 #define E(x) x
517
518 #define LOCAL_ALIGNED_A(a, t, v, s, o, ...)             \
519     uint8_t la_##v[sizeof(t s o) + (a)];                \
520     t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
521
522 #define LOCAL_ALIGNED_D(a, t, v, s, o, ...)             \
523     DECLARE_ALIGNED(a, t, la_##v) s o;                  \
524     t (*v) o = la_##v
525
526 #define LOCAL_ALIGNED(a, t, v, ...) E(LOCAL_ALIGNED_A(a, t, v, __VA_ARGS__,,))
527
528 #if HAVE_LOCAL_ALIGNED_8
529 #   define LOCAL_ALIGNED_8(t, v, ...) E(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
530 #else
531 #   define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED(8, t, v, __VA_ARGS__)
532 #endif
533
534 #if HAVE_LOCAL_ALIGNED_16
535 #   define LOCAL_ALIGNED_16(t, v, ...) E(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
536 #else
537 #   define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED(16, t, v, __VA_ARGS__)
538 #endif
539
540 #define WRAPPER8_16_SQ(name8, name16)\
541 static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\
542     int score=0;\
543     score +=name8(s, dst           , src           , stride, 8);\
544     score +=name8(s, dst+8         , src+8         , stride, 8);\
545     if(h==16){\
546         dst += 8*stride;\
547         src += 8*stride;\
548         score +=name8(s, dst           , src           , stride, 8);\
549         score +=name8(s, dst+8         , src+8         , stride, 8);\
550     }\
551     return score;\
552 }
553
554
555 static inline void copy_block2(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
556 {
557     int i;
558     for(i=0; i<h; i++)
559     {
560         AV_COPY16U(dst, src);
561         dst+=dstStride;
562         src+=srcStride;
563     }
564 }
565
566 static inline void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
567 {
568     int i;
569     for(i=0; i<h; i++)
570     {
571         AV_COPY32U(dst, src);
572         dst+=dstStride;
573         src+=srcStride;
574     }
575 }
576
577 static inline void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
578 {
579     int i;
580     for(i=0; i<h; i++)
581     {
582         AV_COPY64U(dst, src);
583         dst+=dstStride;
584         src+=srcStride;
585     }
586 }
587
588 static inline void copy_block9(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
589 {
590     int i;
591     for(i=0; i<h; i++)
592     {
593         AV_COPY64U(dst, src);
594         dst[8]= src[8];
595         dst+=dstStride;
596         src+=srcStride;
597     }
598 }
599
600 static inline void copy_block16(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
601 {
602     int i;
603     for(i=0; i<h; i++)
604     {
605         AV_COPY128U(dst, src);
606         dst+=dstStride;
607         src+=srcStride;
608     }
609 }
610
611 static inline void copy_block17(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
612 {
613     int i;
614     for(i=0; i<h; i++)
615     {
616         AV_COPY128U(dst, src);
617         dst[16]= src[16];
618         dst+=dstStride;
619         src+=srcStride;
620     }
621 }
622
623 #endif /* AVCODEC_DSPUTIL_H */