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