1/*
2 * SIMD-optimized halfpel functions
3 * Copyright (c) 2000, 2001 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 * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23 */
24
25#include "config_components.h"
26
27#include "libavutil/attributes.h"
28#include "libavutil/cpu.h"
29#include "libavutil/x86/cpu.h"
30#include "libavcodec/avcodec.h"
31#include "libavcodec/hpeldsp.h"
32#include "libavcodec/pixels.h"
33#include "fpel.h"
34#include "hpeldsp.h"
35
36void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
37                              ptrdiff_t line_size, int h);
38void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
39                               ptrdiff_t line_size, int h);
40void ff_put_pixels16_x2_sse2(uint8_t *block, const uint8_t *pixels,
41                             ptrdiff_t line_size, int h);
42void ff_avg_pixels16_x2_sse2(uint8_t *block, const uint8_t *pixels,
43                             ptrdiff_t line_size, int h);
44void ff_put_pixels16_y2_sse2(uint8_t *block, const uint8_t *pixels,
45                             ptrdiff_t line_size, int h);
46void ff_avg_pixels16_y2_sse2(uint8_t *block, const uint8_t *pixels,
47                             ptrdiff_t line_size, int h);
48void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
49                                     ptrdiff_t line_size, int h);
50void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
51                              ptrdiff_t line_size, int h);
52void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
53                                     ptrdiff_t line_size, int h);
54void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
55                              ptrdiff_t line_size, int h);
56void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
57                              ptrdiff_t line_size, int h);
58void ff_avg_approx_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
59                                      ptrdiff_t line_size, int h);
60
61#define avg_pixels16_mmx        ff_avg_pixels16_mmx
62#define put_pixels8_mmx         ff_put_pixels8_mmx
63#define put_pixels16_mmx        ff_put_pixels16_mmx
64#define put_pixels8_xy2_mmx     ff_put_pixels8_xy2_mmx
65#define avg_no_rnd_pixels16_mmx ff_avg_pixels16_mmx
66#define put_no_rnd_pixels8_mmx  ff_put_pixels8_mmx
67#define put_no_rnd_pixels16_mmx ff_put_pixels16_mmx
68
69#if HAVE_INLINE_ASM
70
71/***********************************/
72/* MMX no rounding */
73#define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
74#define SET_RND  MOVQ_WONE
75#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
76#define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
77#define STATIC static
78
79#include "rnd_template.c"
80#include "hpeldsp_rnd_template.c"
81
82#undef DEF
83#undef SET_RND
84#undef PAVGBP
85#undef PAVGB
86#undef STATIC
87
88#if HAVE_MMX
89CALL_2X_PIXELS(avg_no_rnd_pixels16_y2_mmx, avg_no_rnd_pixels8_y2_mmx, 8)
90CALL_2X_PIXELS(put_no_rnd_pixels16_y2_mmx, put_no_rnd_pixels8_y2_mmx, 8)
91
92CALL_2X_PIXELS(avg_no_rnd_pixels16_xy2_mmx, avg_no_rnd_pixels8_xy2_mmx, 8)
93CALL_2X_PIXELS(put_no_rnd_pixels16_xy2_mmx, put_no_rnd_pixels8_xy2_mmx, 8)
94#endif
95
96/***********************************/
97/* MMX rounding */
98
99#define SET_RND  MOVQ_WTWO
100#define DEF(x, y) ff_ ## x ## _ ## y ## _mmx
101#define STATIC
102#define NO_AVG
103
104#include "rnd_template.c"
105
106#undef NO_AVG
107#undef DEF
108#undef SET_RND
109
110#if HAVE_MMX
111CALL_2X_PIXELS(put_pixels16_xy2_mmx, ff_put_pixels8_xy2_mmx, 8)
112#endif
113
114#endif /* HAVE_INLINE_ASM */
115
116
117#if HAVE_X86ASM
118
119#define HPELDSP_AVG_PIXELS16(CPUEXT)                      \
120    CALL_2X_PIXELS(put_no_rnd_pixels16_x2 ## CPUEXT, ff_put_no_rnd_pixels8_x2 ## CPUEXT, 8) \
121    CALL_2X_PIXELS(put_pixels16_y2        ## CPUEXT, ff_put_pixels8_y2        ## CPUEXT, 8) \
122    CALL_2X_PIXELS(put_no_rnd_pixels16_y2 ## CPUEXT, ff_put_no_rnd_pixels8_y2 ## CPUEXT, 8) \
123    CALL_2X_PIXELS(avg_pixels16           ## CPUEXT, ff_avg_pixels8           ## CPUEXT, 8) \
124    CALL_2X_PIXELS(avg_pixels16_x2        ## CPUEXT, ff_avg_pixels8_x2        ## CPUEXT, 8) \
125    CALL_2X_PIXELS(avg_pixels16_y2        ## CPUEXT, ff_avg_pixels8_y2        ## CPUEXT, 8) \
126    CALL_2X_PIXELS(avg_pixels16_xy2       ## CPUEXT, ff_avg_pixels8_xy2       ## CPUEXT, 8) \
127    CALL_2X_PIXELS(avg_approx_pixels16_xy2## CPUEXT, ff_avg_approx_pixels8_xy2## CPUEXT, 8)
128
129HPELDSP_AVG_PIXELS16(_mmxext)
130
131#endif /* HAVE_X86ASM */
132
133#define SET_HPEL_FUNCS_EXT(PFX, IDX, SIZE, CPU)                             \
134    if (HAVE_MMX_EXTERNAL)                                                  \
135        c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _ ## CPU
136
137#if HAVE_MMX_INLINE
138#define SET_HPEL_FUNCS03(PFX, IDX, SIZE, CPU)                                   \
139    do {                                                                        \
140        SET_HPEL_FUNCS_EXT(PFX, IDX, SIZE, CPU);                                \
141        c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
142    } while (0)
143#define SET_HPEL_FUNCS12(PFX, IDX, SIZE, CPU)                                   \
144    do {                                                                        \
145        c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
146        c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
147    } while (0)
148#else
149#define SET_HPEL_FUNCS03(PFX, IDX, SIZE, CPU) SET_HPEL_FUNCS_EXT(PFX, IDX, SIZE, CPU)
150#define SET_HPEL_FUNCS12(PFX, IDX, SIZE, CPU) ((void)0)
151#endif
152#define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
153    do {                                                                        \
154        SET_HPEL_FUNCS03(PFX, IDX, SIZE, CPU);                                  \
155        SET_HPEL_FUNCS12(PFX, IDX, SIZE, CPU);                                  \
156    } while (0)
157
158static void hpeldsp_init_mmx(HpelDSPContext *c, int flags)
159{
160    SET_HPEL_FUNCS03(put,      [0], 16, mmx);
161    SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
162    SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
163    SET_HPEL_FUNCS03(put,      [1],  8, mmx);
164    SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
165}
166
167static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags)
168{
169#if HAVE_MMXEXT_EXTERNAL
170    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
171    c->put_pixels_tab[0][2] = put_pixels16_y2_mmxext;
172
173    c->avg_pixels_tab[0][0] = avg_pixels16_mmxext;
174    c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmxext;
175    c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmxext;
176    c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmxext;
177
178    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
179    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
180
181    c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
182    c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
183    c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
184    c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
185
186    if (!(flags & AV_CODEC_FLAG_BITEXACT)) {
187        c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmxext;
188        c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmxext;
189        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
190        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
191
192        c->avg_pixels_tab[0][3] = avg_approx_pixels16_xy2_mmxext;
193        c->avg_pixels_tab[1][3] = ff_avg_approx_pixels8_xy2_mmxext;
194    }
195#endif /* HAVE_MMXEXT_EXTERNAL */
196}
197
198static void hpeldsp_init_sse2_fast(HpelDSPContext *c, int flags)
199{
200#if HAVE_SSE2_EXTERNAL
201    c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
202    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
203    c->put_pixels_tab[0][1]        = ff_put_pixels16_x2_sse2;
204    c->put_pixels_tab[0][2]        = ff_put_pixels16_y2_sse2;
205    c->put_pixels_tab[0][3]        = ff_put_pixels16_xy2_sse2;
206    c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
207    c->avg_pixels_tab[0][1]        = ff_avg_pixels16_x2_sse2;
208    c->avg_pixels_tab[0][2]        = ff_avg_pixels16_y2_sse2;
209    c->avg_pixels_tab[0][3]        = ff_avg_pixels16_xy2_sse2;
210#endif /* HAVE_SSE2_EXTERNAL */
211}
212
213static void hpeldsp_init_ssse3(HpelDSPContext *c, int flags)
214{
215#if HAVE_SSSE3_EXTERNAL
216    c->put_pixels_tab[0][3]            = ff_put_pixels16_xy2_ssse3;
217    c->avg_pixels_tab[0][3]            = ff_avg_pixels16_xy2_ssse3;
218    c->put_pixels_tab[1][3]            = ff_put_pixels8_xy2_ssse3;
219    c->avg_pixels_tab[1][3]            = ff_avg_pixels8_xy2_ssse3;
220#endif
221}
222
223av_cold void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
224{
225    int cpu_flags = av_get_cpu_flags();
226
227    if (INLINE_MMX(cpu_flags))
228        hpeldsp_init_mmx(c, flags);
229
230    if (EXTERNAL_MMXEXT(cpu_flags))
231        hpeldsp_init_mmxext(c, flags);
232
233    if (EXTERNAL_SSE2_FAST(cpu_flags))
234        hpeldsp_init_sse2_fast(c, flags);
235
236    if (EXTERNAL_SSSE3(cpu_flags))
237        hpeldsp_init_ssse3(c, flags);
238
239    if (CONFIG_VP3_DECODER)
240        ff_hpeldsp_vp3_init_x86(c, cpu_flags, flags);
241}
242