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