]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsputil.h
14c5db03bbbb2a6efd9b7e008a2791f3a6f0f753
[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 /* encoding scans */
54 extern const uint8_t ff_alternate_horizontal_scan[64];
55 extern const uint8_t ff_alternate_vertical_scan[64];
56 extern const uint8_t ff_zigzag_direct[64];
57 extern const uint8_t ff_zigzag248_direct[64];
58
59 /* pixel operations */
60 #define MAX_NEG_CROP 1024
61
62 /* temporary */
63 extern uint32_t ff_squareTbl[512];
64 extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
65
66 #define PUTAVG_PIXELS(depth)\
67 void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
68 void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
69 void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
70 void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
71
72 PUTAVG_PIXELS( 8)
73 PUTAVG_PIXELS( 9)
74 PUTAVG_PIXELS(10)
75
76 #define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
77 #define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
78 #define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
79 #define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
80
81 /* RV40 functions */
82 void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
83 void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
84 void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
85 void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
86
87 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
88               int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
89
90 /* minimum alignment rules ;)
91 If you notice errors in the align stuff, need more alignment for some ASM code
92 for some CPU or need to use a function with less aligned data then send a mail
93 to the libav-devel mailing list, ...
94
95 !warning These alignments might not match reality, (missing attribute((align))
96 stuff somewhere possible).
97 I (Michael) did not check them, these are just the alignments which I think
98 could be reached easily ...
99
100 !future video codecs might need functions with less strict alignment
101 */
102
103 /* add and put pixel (decoding) */
104 // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
105 //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
106 typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, ptrdiff_t line_size, int h);
107 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);
108 typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
109
110 typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
111
112 #define DEF_OLD_QPEL(name)\
113 void ff_put_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
114 void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
115 void ff_avg_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
116
117 DEF_OLD_QPEL(qpel16_mc11_old_c)
118 DEF_OLD_QPEL(qpel16_mc31_old_c)
119 DEF_OLD_QPEL(qpel16_mc12_old_c)
120 DEF_OLD_QPEL(qpel16_mc32_old_c)
121 DEF_OLD_QPEL(qpel16_mc13_old_c)
122 DEF_OLD_QPEL(qpel16_mc33_old_c)
123 DEF_OLD_QPEL(qpel8_mc11_old_c)
124 DEF_OLD_QPEL(qpel8_mc31_old_c)
125 DEF_OLD_QPEL(qpel8_mc12_old_c)
126 DEF_OLD_QPEL(qpel8_mc32_old_c)
127 DEF_OLD_QPEL(qpel8_mc13_old_c)
128 DEF_OLD_QPEL(qpel8_mc33_old_c)
129
130 #define CALL_2X_PIXELS(a, b, n)\
131 static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\
132     b(block  , pixels  , line_size, h);\
133     b(block+n, pixels+n, line_size, h);\
134 }
135
136 /* motion estimation */
137 // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
138 // although currently h<4 is not used as functions with width <8 are neither used nor implemented
139 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))*/;
140
141 /**
142  * Scantable.
143  */
144 typedef struct ScanTable{
145     const uint8_t *scantable;
146     uint8_t permutated[64];
147     uint8_t raster_end[64];
148 } ScanTable;
149
150 void ff_init_scantable(uint8_t *, ScanTable *st, const uint8_t *src_scantable);
151 void ff_init_scantable_permutation(uint8_t *idct_permutation,
152                                    int idct_permutation_type);
153
154 /**
155  * DSPContext.
156  */
157 typedef struct DSPContext {
158     /**
159      * Size of DCT coefficients.
160      */
161     int dct_bits;
162
163     /* pixel ops : interface with DCT */
164     void (*get_pixels)(int16_t *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
165     void (*diff_pixels)(int16_t *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
166     void (*put_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
167     void (*put_signed_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
168     void (*add_pixels_clamped)(const int16_t *block/*align 16*/, uint8_t *pixels/*align 8*/, int line_size);
169     void (*add_pixels8)(uint8_t *pixels, int16_t *block, int line_size);
170     void (*add_pixels4)(uint8_t *pixels, int16_t *block, int line_size);
171     int (*sum_abs_dctelem)(int16_t *block/*align 16*/);
172     /**
173      * translational global motion compensation.
174      */
175     void (*gmc1)(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int srcStride, int h, int x16, int y16, int rounder);
176     /**
177      * global motion compensation.
178      */
179     void (*gmc )(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int ox, int oy,
180                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
181     void (*clear_block)(int16_t *block/*align 16*/);
182     void (*clear_blocks)(int16_t *blocks/*align 16*/);
183     int (*pix_sum)(uint8_t * pix, int line_size);
184     int (*pix_norm1)(uint8_t * pix, int line_size);
185 // 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4
186
187     me_cmp_func sad[6]; /* identical to pix_absAxA except additional void * */
188     me_cmp_func sse[6];
189     me_cmp_func hadamard8_diff[6];
190     me_cmp_func dct_sad[6];
191     me_cmp_func quant_psnr[6];
192     me_cmp_func bit[6];
193     me_cmp_func rd[6];
194     me_cmp_func vsad[6];
195     me_cmp_func vsse[6];
196     me_cmp_func nsse[6];
197     me_cmp_func dct_max[6];
198     me_cmp_func dct264_sad[6];
199
200     me_cmp_func me_pre_cmp[6];
201     me_cmp_func me_cmp[6];
202     me_cmp_func me_sub_cmp[6];
203     me_cmp_func mb_cmp[6];
204     me_cmp_func ildct_cmp[6]; //only width 16 used
205     me_cmp_func frame_skip_cmp[6]; //only width 8 used
206
207     int (*ssd_int8_vs_int16)(const int8_t *pix1, const int16_t *pix2,
208                              int size);
209
210     /**
211      * Halfpel motion compensation with rounding (a+b+1)>>1.
212      * this is an array[4][4] of motion compensation functions for 4
213      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
214      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
215      * @param block destination where the result is stored
216      * @param pixels source
217      * @param line_size number of bytes in a horizontal line of block
218      * @param h height
219      */
220     op_pixels_func put_pixels_tab[4][4];
221
222     /**
223      * Halfpel motion compensation with rounding (a+b+1)>>1.
224      * This is an array[4][4] of motion compensation functions for 4
225      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
226      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
227      * @param block destination into which the result is averaged (a+b+1)>>1
228      * @param pixels source
229      * @param line_size number of bytes in a horizontal line of block
230      * @param h height
231      */
232     op_pixels_func avg_pixels_tab[4][4];
233
234     /**
235      * Halfpel motion compensation with no rounding (a+b)>>1.
236      * this is an array[2][4] of motion compensation functions for 2
237      * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
238      * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
239      * @param block destination where the result is stored
240      * @param pixels source
241      * @param line_size number of bytes in a horizontal line of block
242      * @param h height
243      */
244     op_pixels_func put_no_rnd_pixels_tab[2][4];
245
246     /**
247      * Halfpel motion compensation with no rounding (a+b)>>1.
248      * this is an array[4] of motion compensation functions for 1
249      * horizontal blocksize (16) and the 4 halfpel positions<br>
250      * *pixels_tab[0][ xhalfpel + 2*yhalfpel ]
251      * @param block destination into which the result is averaged (a+b)>>1
252      * @param pixels source
253      * @param line_size number of bytes in a horizontal line of block
254      * @param h height
255      */
256     op_pixels_func avg_no_rnd_pixels_tab[4];
257
258     /**
259      * Thirdpel motion compensation with rounding (a+b+1)>>1.
260      * this is an array[12] of motion compensation functions for the 9 thirdpe
261      * positions<br>
262      * *pixels_tab[ xthirdpel + 4*ythirdpel ]
263      * @param block destination where the result is stored
264      * @param pixels source
265      * @param line_size number of bytes in a horizontal line of block
266      * @param h height
267      */
268     tpel_mc_func put_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
269     tpel_mc_func avg_tpel_pixels_tab[11]; //FIXME individual func ptr per width?
270
271     qpel_mc_func put_qpel_pixels_tab[2][16];
272     qpel_mc_func avg_qpel_pixels_tab[2][16];
273     qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16];
274     qpel_mc_func put_mspel_pixels_tab[8];
275
276     me_cmp_func pix_abs[2][4];
277
278     /* huffyuv specific */
279     void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w);
280     void (*diff_bytes)(uint8_t *dst/*align 16*/, uint8_t *src1/*align 16*/, uint8_t *src2/*align 1*/,int w);
281     /**
282      * subtract huffyuv's variant of median prediction
283      * note, this might read from src1[-1], src2[-1]
284      */
285     void (*sub_hfyu_median_prediction)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w, int *left, int *left_top);
286     void (*add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
287     int  (*add_hfyu_left_prediction)(uint8_t *dst, const uint8_t *src, int w, int left);
288     void (*add_hfyu_left_prediction_bgr32)(uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue, int *alpha);
289     void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
290     void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
291
292     void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
293     void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
294
295     void (*h261_loop_filter)(uint8_t *src, int stride);
296
297     /* assume len is a multiple of 8, and arrays are 16-byte aligned */
298     void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
299
300     /* (I)DCT */
301     void (*fdct)(int16_t *block/* align 16*/);
302     void (*fdct248)(int16_t *block/* align 16*/);
303
304     /* IDCT really*/
305     void (*idct)(int16_t *block/* align 16*/);
306
307     /**
308      * block -> idct -> clip to unsigned 8 bit -> dest.
309      * (-1392, 0, 0, ...) -> idct -> (-174, -174, ...) -> put -> (0, 0, ...)
310      * @param line_size size in bytes of a horizontal line of dest
311      */
312     void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
313
314     /**
315      * block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
316      * @param line_size size in bytes of a horizontal line of dest
317      */
318     void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, int16_t *block/*align 16*/);
319
320     /**
321      * idct input permutation.
322      * several optimized IDCTs need a permutated input (relative to the normal order of the reference
323      * IDCT)
324      * this permutation must be performed before the idct_put/add, note, normally this can be merged
325      * with the zigzag/alternate scan<br>
326      * an example to avoid confusion:
327      * - (->decode coeffs -> zigzag reorder -> dequant -> reference idct ->...)
328      * - (x -> reference dct -> reference idct -> x)
329      * - (x -> reference dct -> simple_mmx_perm = idct_permutation -> simple_idct_mmx -> x)
330      * - (->decode coeffs -> zigzag reorder -> simple_mmx_perm -> dequant -> simple_idct_mmx ->...)
331      */
332     uint8_t idct_permutation[64];
333     int idct_permutation_type;
334 #define FF_NO_IDCT_PERM 1
335 #define FF_LIBMPEG2_IDCT_PERM 2
336 #define FF_SIMPLE_IDCT_PERM 3
337 #define FF_TRANSPOSE_IDCT_PERM 4
338 #define FF_PARTTRANS_IDCT_PERM 5
339 #define FF_SSE2_IDCT_PERM 6
340
341     int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale);
342     void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale);
343 #define BASIS_SHIFT 16
344 #define RECON_SHIFT 6
345
346     void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides);
347 #define EDGE_WIDTH 16
348 #define EDGE_TOP    1
349 #define EDGE_BOTTOM 2
350
351     void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
352
353     /**
354      * Calculate scalar product of two vectors.
355      * @param len length of vectors, should be multiple of 16
356      */
357     int32_t (*scalarproduct_int16)(const int16_t *v1, const int16_t *v2/*align 16*/, int len);
358     /* ape functions */
359     /**
360      * Calculate scalar product of v1 and v2,
361      * and v1[i] += v3[i] * mul
362      * @param len length of vectors, should be multiple of 16
363      */
364     int32_t (*scalarproduct_and_madd_int16)(int16_t *v1/*align 16*/, const int16_t *v2, const int16_t *v3, int len, int mul);
365
366     /**
367      * Apply symmetric window in 16-bit fixed-point.
368      * @param output destination array
369      *               constraints: 16-byte aligned
370      * @param input  source array
371      *               constraints: 16-byte aligned
372      * @param window window array
373      *               constraints: 16-byte aligned, at least len/2 elements
374      * @param len    full window length
375      *               constraints: multiple of ? greater than zero
376      */
377     void (*apply_window_int16)(int16_t *output, const int16_t *input,
378                                const int16_t *window, unsigned int len);
379
380     /**
381      * Clip each element in an array of int32_t to a given minimum and maximum value.
382      * @param dst  destination array
383      *             constraints: 16-byte aligned
384      * @param src  source array
385      *             constraints: 16-byte aligned
386      * @param min  minimum value
387      *             constraints: must be in the range [-(1 << 24), 1 << 24]
388      * @param max  maximum value
389      *             constraints: must be in the range [-(1 << 24), 1 << 24]
390      * @param len  number of elements in the array
391      *             constraints: multiple of 32 greater than zero
392      */
393     void (*vector_clip_int32)(int32_t *dst, const int32_t *src, int32_t min,
394                               int32_t max, unsigned int len);
395
396     op_fill_func fill_block_tab[2];
397 } DSPContext;
398
399 void ff_dsputil_static_init(void);
400 void ff_dsputil_init(DSPContext* p, AVCodecContext *avctx);
401
402 int ff_check_alignment(void);
403
404 void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
405
406 void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
407 void ff_dsputil_init_arm(DSPContext* c, AVCodecContext *avctx);
408 void ff_dsputil_init_bfin(DSPContext* c, AVCodecContext *avctx);
409 void ff_dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx);
410 void ff_dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx);
411 void ff_dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
412 void ff_dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
413
414 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
415 #   define STRIDE_ALIGN 16
416 #else
417 #   define STRIDE_ALIGN 8
418 #endif
419
420 #endif /* AVCODEC_DSPUTIL_H */