]> git.sesse.net Git - x264/blob - common/x86/mc-c.c
Fix crash when using libx264.dll compiled with ICL for X86_64
[x264] / common / x86 / mc-c.c
1 /*****************************************************************************
2  * mc-c.c: x86 motion compensation
3  *****************************************************************************
4  * Copyright (C) 2003-2012 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "common/common.h"
33 #include "mc.h"
34
35 #define DECL_SUF( func, args )\
36     void func##_mmx2 args;\
37     void func##_sse2 args;\
38     void func##_ssse3 args;
39
40 DECL_SUF( x264_pixel_avg_16x16, ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
41 DECL_SUF( x264_pixel_avg_16x8,  ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
42 DECL_SUF( x264_pixel_avg_8x16,  ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
43 DECL_SUF( x264_pixel_avg_8x8,   ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
44 DECL_SUF( x264_pixel_avg_8x4,   ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
45 DECL_SUF( x264_pixel_avg_4x16,  ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
46 DECL_SUF( x264_pixel_avg_4x8,   ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
47 DECL_SUF( x264_pixel_avg_4x4,   ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
48 DECL_SUF( x264_pixel_avg_4x2,   ( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t, int ))
49
50 #define MC_WEIGHT(w,type) \
51     void x264_mc_weight_w##w##_##type( pixel *, intptr_t, pixel *, intptr_t, const x264_weight_t *, int );
52
53 #define MC_WEIGHT_OFFSET(w,type) \
54     void x264_mc_offsetadd_w##w##_##type( pixel *, intptr_t, pixel *, intptr_t, const x264_weight_t *, int ); \
55     void x264_mc_offsetsub_w##w##_##type( pixel *, intptr_t, pixel *, intptr_t, const x264_weight_t *, int ); \
56     MC_WEIGHT(w,type)
57
58 MC_WEIGHT_OFFSET( 4, mmx2 )
59 MC_WEIGHT_OFFSET( 8, mmx2 )
60 MC_WEIGHT_OFFSET( 12, mmx2 )
61 MC_WEIGHT_OFFSET( 16, mmx2 )
62 MC_WEIGHT_OFFSET( 20, mmx2 )
63 MC_WEIGHT_OFFSET( 12, sse2 )
64 MC_WEIGHT_OFFSET( 16, sse2 )
65 MC_WEIGHT_OFFSET( 20, sse2 )
66 #if HIGH_BIT_DEPTH
67 MC_WEIGHT_OFFSET( 8, sse2 )
68 #endif
69 MC_WEIGHT( 8, sse2  )
70 MC_WEIGHT( 4, ssse3 )
71 MC_WEIGHT( 8, ssse3 )
72 MC_WEIGHT( 12, ssse3 )
73 MC_WEIGHT( 16, ssse3 )
74 MC_WEIGHT( 20, ssse3 )
75 #undef MC_OFFSET
76 #undef MC_WEIGHT
77
78 void x264_mc_copy_w4_mmx  ( pixel *, intptr_t, pixel *, intptr_t, int );
79 void x264_mc_copy_w8_mmx  ( pixel *, intptr_t, pixel *, intptr_t, int );
80 void x264_mc_copy_w8_sse2 ( pixel *, intptr_t, pixel *, intptr_t, int );
81 void x264_mc_copy_w16_mmx ( pixel *, intptr_t, pixel *, intptr_t, int );
82 void x264_mc_copy_w16_sse2( pixel *, intptr_t, pixel *, intptr_t, int );
83 void x264_mc_copy_w16_aligned_sse2( pixel *, intptr_t, pixel *, intptr_t, int );
84 void x264_prefetch_fenc_420_mmx2( pixel *, intptr_t, pixel *, intptr_t, int );
85 void x264_prefetch_fenc_422_mmx2( pixel *, intptr_t, pixel *, intptr_t, int );
86 void x264_prefetch_ref_mmx2( pixel *, intptr_t, int );
87 void x264_plane_copy_core_mmx2( pixel *, intptr_t, pixel *, intptr_t, int w, int h );
88 void x264_plane_copy_c( pixel *, intptr_t, pixel *, intptr_t, int w, int h );
89 void x264_plane_copy_interleave_core_mmx2( pixel *dst,  intptr_t i_dst,
90                                            pixel *srcu, intptr_t i_srcu,
91                                            pixel *srcv, intptr_t i_srcv, int w, int h );
92 void x264_plane_copy_interleave_core_sse2( pixel *dst,  intptr_t i_dst,
93                                            pixel *srcu, intptr_t i_srcu,
94                                            pixel *srcv, intptr_t i_srcv, int w, int h );
95 void x264_plane_copy_interleave_core_avx( pixel *dst,  intptr_t i_dst,
96                                           pixel *srcu, intptr_t i_srcu,
97                                           pixel *srcv, intptr_t i_srcv, int w, int h );
98 void x264_plane_copy_interleave_c( pixel *dst,  intptr_t i_dst,
99                                    pixel *srcu, intptr_t i_srcu,
100                                    pixel *srcv, intptr_t i_srcv, int w, int h );
101 void x264_plane_copy_deinterleave_mmx( pixel *dstu, intptr_t i_dstu,
102                                        pixel *dstv, intptr_t i_dstv,
103                                        pixel *src,  intptr_t i_src, int w, int h );
104 void x264_plane_copy_deinterleave_sse2( pixel *dstu, intptr_t i_dstu,
105                                         pixel *dstv, intptr_t i_dstv,
106                                         pixel *src,  intptr_t i_src, int w, int h );
107 void x264_plane_copy_deinterleave_ssse3( uint8_t *dstu, intptr_t i_dstu,
108                                          uint8_t *dstv, intptr_t i_dstv,
109                                          uint8_t *src,  intptr_t i_src, int w, int h );
110 void x264_plane_copy_deinterleave_avx( uint16_t *dstu, intptr_t i_dstu,
111                                        uint16_t *dstv, intptr_t i_dstv,
112                                        uint16_t *src,  intptr_t i_src, int w, int h );
113 void x264_store_interleave_chroma_mmx2( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );
114 void x264_store_interleave_chroma_sse2( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );
115 void x264_store_interleave_chroma_avx ( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );
116 void x264_load_deinterleave_chroma_fenc_mmx ( pixel *dst, pixel *src, intptr_t i_src, int height );
117 void x264_load_deinterleave_chroma_fenc_sse2( pixel *dst, pixel *src, intptr_t i_src, int height );
118 void x264_load_deinterleave_chroma_fenc_ssse3( uint8_t *dst, uint8_t *src, intptr_t i_src, int height );
119 void x264_load_deinterleave_chroma_fenc_avx( uint16_t *dst, uint16_t *src, intptr_t i_src, int height );
120 void x264_load_deinterleave_chroma_fdec_mmx ( pixel *dst, pixel *src, intptr_t i_src, int height );
121 void x264_load_deinterleave_chroma_fdec_sse2( pixel *dst, pixel *src, intptr_t i_src, int height );
122 void x264_load_deinterleave_chroma_fdec_ssse3( uint8_t *dst, uint8_t *src, intptr_t i_src, int height );
123 void x264_load_deinterleave_chroma_fdec_avx( uint16_t *dst, uint16_t *src, intptr_t i_src, int height );
124 void *x264_memcpy_aligned_mmx ( void *dst, const void *src, size_t n );
125 void *x264_memcpy_aligned_sse2( void *dst, const void *src, size_t n );
126 void x264_memzero_aligned_mmx ( void *dst, size_t n );
127 void x264_memzero_aligned_sse2( void *dst, size_t n );
128 void x264_integral_init4h_sse4( uint16_t *sum, uint8_t *pix, intptr_t stride );
129 void x264_integral_init8h_sse4( uint16_t *sum, uint8_t *pix, intptr_t stride );
130 void x264_integral_init8h_avx ( uint16_t *sum, uint8_t *pix, intptr_t stride );
131 void x264_integral_init4v_mmx  ( uint16_t *sum8, uint16_t *sum4, intptr_t stride );
132 void x264_integral_init4v_sse2 ( uint16_t *sum8, uint16_t *sum4, intptr_t stride );
133 void x264_integral_init4v_ssse3( uint16_t *sum8, uint16_t *sum4, intptr_t stride );
134 void x264_integral_init8v_mmx ( uint16_t *sum8, intptr_t stride );
135 void x264_integral_init8v_sse2( uint16_t *sum8, intptr_t stride );
136 void x264_mbtree_propagate_cost_sse2( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
137                                       uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
138 void x264_mbtree_propagate_cost_avx ( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
139                                       uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
140 void x264_mbtree_propagate_cost_fma4( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
141                                       uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
142
143 #define MC_CHROMA(cpu)\
144 void x264_mc_chroma_##cpu( pixel *dstu, pixel *dstv, intptr_t i_dst, pixel *src, intptr_t i_src,\
145                            int dx, int dy, int i_width, int i_height );
146 MC_CHROMA(mmx2)
147 MC_CHROMA(sse2)
148 MC_CHROMA(sse2_misalign)
149 MC_CHROMA(ssse3)
150 MC_CHROMA(ssse3_cache64)
151 MC_CHROMA(avx)
152 MC_CHROMA(avx_cache64)
153
154 #define LOWRES(cpu)\
155 void x264_frame_init_lowres_core_##cpu( pixel *src0, pixel *dst0, pixel *dsth, pixel *dstv, pixel *dstc,\
156                                         intptr_t src_stride, intptr_t dst_stride, int width, int height );
157 LOWRES(mmx2)
158 LOWRES(cache32_mmx2)
159 LOWRES(sse2)
160 LOWRES(ssse3)
161 LOWRES(avx)
162 LOWRES(xop)
163
164 #define PIXEL_AVG_W(width,cpu)\
165 void x264_pixel_avg2_w##width##_##cpu( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t );
166 /* This declares some functions that don't exist, but that isn't a problem. */
167 #define PIXEL_AVG_WALL(cpu)\
168 PIXEL_AVG_W(4,cpu); PIXEL_AVG_W(8,cpu); PIXEL_AVG_W(10,cpu); PIXEL_AVG_W(12,cpu); PIXEL_AVG_W(16,cpu); PIXEL_AVG_W(18,cpu); PIXEL_AVG_W(20,cpu);
169
170 PIXEL_AVG_WALL(mmx2)
171 PIXEL_AVG_WALL(cache32_mmx2)
172 PIXEL_AVG_WALL(cache64_mmx2)
173 PIXEL_AVG_WALL(cache64_sse2)
174 PIXEL_AVG_WALL(sse2)
175 PIXEL_AVG_WALL(sse2_misalign)
176 PIXEL_AVG_WALL(cache64_ssse3)
177
178 #define PIXEL_AVG_WTAB(instr, name1, name2, name3, name4, name5)\
179 static void (* const x264_pixel_avg_wtab_##instr[6])( pixel *, intptr_t, pixel *, intptr_t, pixel *, intptr_t ) =\
180 {\
181     NULL,\
182     x264_pixel_avg2_w4_##name1,\
183     x264_pixel_avg2_w8_##name2,\
184     x264_pixel_avg2_w12_##name3,\
185     x264_pixel_avg2_w16_##name4,\
186     x264_pixel_avg2_w20_##name5,\
187 };
188
189 #if HIGH_BIT_DEPTH
190 /* we can replace w12/w20 with w10/w18 as only 9/17 pixels in fact are important */
191 #define x264_pixel_avg2_w12_mmx2       x264_pixel_avg2_w10_mmx2
192 #define x264_pixel_avg2_w20_mmx2       x264_pixel_avg2_w18_mmx2
193 #define x264_pixel_avg2_w12_sse2         x264_pixel_avg2_w10_sse2
194 #define x264_pixel_avg2_w20_sse2         x264_pixel_avg2_w18_sse2
195 #else
196 /* w16 sse2 is faster than w12 mmx as long as the cacheline issue is resolved */
197 #define x264_pixel_avg2_w12_cache64_ssse3 x264_pixel_avg2_w16_cache64_ssse3
198 #define x264_pixel_avg2_w12_cache64_sse2 x264_pixel_avg2_w16_cache64_sse2
199 #define x264_pixel_avg2_w12_sse3         x264_pixel_avg2_w16_sse3
200 #define x264_pixel_avg2_w12_sse2         x264_pixel_avg2_w16_sse2
201 #endif // HIGH_BIT_DEPTH
202
203 PIXEL_AVG_WTAB(mmx2, mmx2, mmx2, mmx2, mmx2, mmx2)
204 #if HIGH_BIT_DEPTH
205 PIXEL_AVG_WTAB(sse2, mmx2, sse2, sse2, sse2, sse2)
206 #else // !HIGH_BIT_DEPTH
207 #if ARCH_X86
208 PIXEL_AVG_WTAB(cache32_mmx2, mmx2, cache32_mmx2, cache32_mmx2, cache32_mmx2, cache32_mmx2)
209 PIXEL_AVG_WTAB(cache64_mmx2, mmx2, cache64_mmx2, cache64_mmx2, cache64_mmx2, cache64_mmx2)
210 #endif
211 PIXEL_AVG_WTAB(sse2, mmx2, mmx2, sse2, sse2, sse2)
212 PIXEL_AVG_WTAB(sse2_misalign, mmx2, mmx2, sse2, sse2, sse2_misalign)
213 PIXEL_AVG_WTAB(cache64_sse2, mmx2, cache64_mmx2, cache64_sse2, cache64_sse2, cache64_sse2)
214 PIXEL_AVG_WTAB(cache64_ssse3, mmx2, cache64_mmx2, cache64_ssse3, cache64_ssse3, cache64_sse2)
215 #endif // HIGH_BIT_DEPTH
216
217 #define MC_COPY_WTAB(instr, name1, name2, name3)\
218 static void (* const x264_mc_copy_wtab_##instr[5])( pixel *, intptr_t, pixel *, intptr_t, int ) =\
219 {\
220     NULL,\
221     x264_mc_copy_w4_##name1,\
222     x264_mc_copy_w8_##name2,\
223     NULL,\
224     x264_mc_copy_w16_##name3,\
225 };
226
227 MC_COPY_WTAB(mmx,mmx,mmx,mmx)
228 #if HIGH_BIT_DEPTH
229 MC_COPY_WTAB(sse2,mmx,sse2,sse2)
230 #else
231 MC_COPY_WTAB(sse2,mmx,mmx,sse2)
232 #endif
233
234 #define MC_WEIGHT_WTAB(function, instr, name1, name2, w12version)\
235     static void (* x264_mc_##function##_wtab_##instr[6])( pixel *, intptr_t, pixel *, intptr_t, const x264_weight_t *, int ) =\
236 {\
237     x264_mc_##function##_w4_##name1,\
238     x264_mc_##function##_w4_##name1,\
239     x264_mc_##function##_w8_##name2,\
240     x264_mc_##function##_w##w12version##_##instr,\
241     x264_mc_##function##_w16_##instr,\
242     x264_mc_##function##_w20_##instr,\
243 };
244
245 #if HIGH_BIT_DEPTH
246 MC_WEIGHT_WTAB(weight,mmx2,mmx2,mmx2,12)
247 MC_WEIGHT_WTAB(offsetadd,mmx2,mmx2,mmx2,12)
248 MC_WEIGHT_WTAB(offsetsub,mmx2,mmx2,mmx2,12)
249 MC_WEIGHT_WTAB(weight,sse2,mmx2,sse2,12)
250 MC_WEIGHT_WTAB(offsetadd,sse2,mmx2,sse2,16)
251 MC_WEIGHT_WTAB(offsetsub,sse2,mmx2,sse2,16)
252
253 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
254 {
255     if( w->i_scale == 1<<w->i_denom )
256     {
257         if( w->i_offset < 0 )
258             w->weightfn = h->mc.offsetsub;
259         else
260             w->weightfn = h->mc.offsetadd;
261         for( int i = 0; i < 8; i++ )
262             w->cachea[i] = abs(w->i_offset<<(BIT_DEPTH-8));
263         return;
264     }
265     w->weightfn = h->mc.weight;
266     int den1 = 1<<w->i_denom;
267     int den2 = w->i_scale<<1;
268     int den3 = 1+(w->i_offset<<(BIT_DEPTH-8+1));
269     for( int i = 0; i < 8; i++ )
270     {
271         w->cachea[i] = den1;
272         w->cacheb[i] = i&1 ? den3 : den2;
273     }
274 }
275 #else
276 MC_WEIGHT_WTAB(weight,mmx2,mmx2,mmx2,12)
277 MC_WEIGHT_WTAB(offsetadd,mmx2,mmx2,mmx2,12)
278 MC_WEIGHT_WTAB(offsetsub,mmx2,mmx2,mmx2,12)
279 MC_WEIGHT_WTAB(weight,sse2,mmx2,sse2,16)
280 MC_WEIGHT_WTAB(offsetadd,sse2,mmx2,mmx2,16)
281 MC_WEIGHT_WTAB(offsetsub,sse2,mmx2,mmx2,16)
282 MC_WEIGHT_WTAB(weight,ssse3,ssse3,ssse3,16)
283
284 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
285 {
286     int i;
287     int16_t den1;
288
289     if( w->i_scale == 1<<w->i_denom )
290     {
291         if( w->i_offset < 0 )
292             w->weightfn = h->mc.offsetsub;
293         else
294             w->weightfn = h->mc.offsetadd;
295         memset( w->cachea, abs(w->i_offset), sizeof(w->cachea) );
296         return;
297     }
298     w->weightfn = h->mc.weight;
299     den1 = 1 << (w->i_denom - 1) | w->i_offset << w->i_denom;
300     for( i = 0; i < 8; i++ )
301     {
302         w->cachea[i] = w->i_scale;
303         w->cacheb[i] = den1;
304     }
305 }
306
307 static void x264_weight_cache_ssse3( x264_t *h, x264_weight_t *w )
308 {
309     int i, den1;
310     if( w->i_scale == 1<<w->i_denom )
311     {
312         if( w->i_offset < 0 )
313             w->weightfn = h->mc.offsetsub;
314         else
315             w->weightfn = h->mc.offsetadd;
316
317         memset( w->cachea, abs( w->i_offset ), sizeof(w->cachea) );
318         return;
319     }
320     w->weightfn = h->mc.weight;
321     den1 = w->i_scale << (8 - w->i_denom);
322     for( i = 0; i < 8; i++ )
323     {
324         w->cachea[i] = den1;
325         w->cacheb[i] = w->i_offset;
326     }
327 }
328 #endif // !HIGH_BIT_DEPTH
329
330 static const uint8_t hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};
331 static const uint8_t hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};
332
333 #define MC_LUMA(name,instr1,instr2)\
334 static void mc_luma_##name( pixel *dst,    intptr_t i_dst_stride,\
335                             pixel *src[4], intptr_t i_src_stride,\
336                             int mvx, int mvy,\
337                             int i_width, int i_height, const x264_weight_t *weight )\
338 {\
339     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
340     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
341     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
342     if( qpel_idx & 5 ) /* qpel interpolation needed */\
343     {\
344         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
345         x264_pixel_avg_wtab_##instr1[i_width>>2](\
346                 dst, i_dst_stride, src1, i_src_stride,\
347                 src2, i_height );\
348         if( weight->weightfn )\
349             weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );\
350     }\
351     else if( weight->weightfn )\
352         weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );\
353     else\
354         x264_mc_copy_wtab_##instr2[i_width>>2](dst, i_dst_stride, src1, i_src_stride, i_height );\
355 }
356
357 MC_LUMA(mmx2,mmx2,mmx)
358 MC_LUMA(sse2,sse2,sse2)
359 #if !HIGH_BIT_DEPTH
360 #if ARCH_X86
361 MC_LUMA(cache32_mmx2,cache32_mmx2,mmx)
362 MC_LUMA(cache64_mmx2,cache64_mmx2,mmx)
363 #endif
364 MC_LUMA(cache64_sse2,cache64_sse2,sse2)
365 MC_LUMA(cache64_ssse3,cache64_ssse3,sse2)
366 #endif // !HIGH_BIT_DEPTH
367
368 #define GET_REF(name)\
369 static pixel *get_ref_##name( pixel *dst,   intptr_t *i_dst_stride,\
370                               pixel *src[4], intptr_t i_src_stride,\
371                               int mvx, int mvy,\
372                               int i_width, int i_height, const x264_weight_t *weight )\
373 {\
374     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
375     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
376     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
377     if( qpel_idx & 5 ) /* qpel interpolation needed */\
378     {\
379         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
380         x264_pixel_avg_wtab_##name[i_width>>2](\
381                 dst, *i_dst_stride, src1, i_src_stride,\
382                 src2, i_height );\
383         if( weight->weightfn )\
384             weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );\
385         return dst;\
386     }\
387     else if( weight->weightfn )\
388     {\
389         weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );\
390         return dst;\
391     }\
392     else\
393     {\
394         *i_dst_stride = i_src_stride;\
395         return src1;\
396     }\
397 }
398
399 GET_REF(mmx2)
400 GET_REF(sse2)
401 #if !HIGH_BIT_DEPTH
402 #if ARCH_X86
403 GET_REF(cache32_mmx2)
404 GET_REF(cache64_mmx2)
405 #endif
406 GET_REF(sse2_misalign)
407 GET_REF(cache64_sse2)
408 GET_REF(cache64_ssse3)
409 #endif // !HIGH_BIT_DEPTH
410
411 #define HPEL(align, cpu, cpuv, cpuc, cpuh)\
412 void x264_hpel_filter_v_##cpuv( pixel *dst, pixel *src, int16_t *buf, intptr_t stride, intptr_t width);\
413 void x264_hpel_filter_c_##cpuc( pixel *dst, int16_t *buf, intptr_t width );\
414 void x264_hpel_filter_h_##cpuh( pixel *dst, pixel *src, intptr_t width );\
415 static void x264_hpel_filter_##cpu( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,\
416                                     intptr_t stride, int width, int height, int16_t *buf )\
417 {\
418     intptr_t realign = (intptr_t)src & (align-1);\
419     src -= realign;\
420     dstv -= realign;\
421     dstc -= realign;\
422     dsth -= realign;\
423     width += realign;\
424     while( height-- )\
425     {\
426         x264_hpel_filter_v_##cpuv( dstv, src, buf+8, stride, width );\
427         x264_hpel_filter_c_##cpuc( dstc, buf+8, width );\
428         x264_hpel_filter_h_##cpuh( dsth, src, width );\
429         dsth += stride;\
430         dstv += stride;\
431         dstc += stride;\
432         src  += stride;\
433     }\
434     x264_sfence();\
435 }
436
437 HPEL(8, mmx2, mmx2, mmx2, mmx2)
438 #if HIGH_BIT_DEPTH
439 HPEL(16, sse2, sse2, sse2, sse2)
440 #else // !HIGH_BIT_DEPTH
441 HPEL(16, sse2_amd, mmx2, mmx2, sse2)
442 #if ARCH_X86_64
443 void x264_hpel_filter_sse2 ( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, intptr_t stride, int width, int height, int16_t *buf );
444 void x264_hpel_filter_ssse3( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, intptr_t stride, int width, int height, int16_t *buf );
445 void x264_hpel_filter_avx  ( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, intptr_t stride, int width, int height, int16_t *buf );
446 #else
447 HPEL(16, sse2, sse2, sse2, sse2)
448 HPEL(16, ssse3, ssse3, ssse3, ssse3)
449 HPEL(16, avx, avx, avx, avx)
450 #endif
451 HPEL(16, sse2_misalign, sse2, sse2_misalign, sse2)
452 #endif // HIGH_BIT_DEPTH
453
454 static void x264_plane_copy_mmx2( pixel *dst, intptr_t i_dst, pixel *src, intptr_t i_src, int w, int h )
455 {
456     int c_w = 16/sizeof(pixel) - 1;
457     if( w < 256 ) { // tiny resolutions don't want non-temporal hints. dunno the exact threshold.
458         x264_plane_copy_c( dst, i_dst, src, i_src, w, h );
459     } else if( !(w&c_w) ) {
460         x264_plane_copy_core_mmx2( dst, i_dst, src, i_src, w, h );
461     } else if( i_src > 0 ) {
462         // have to use plain memcpy on the last line (in memory order) to avoid overreading src
463         x264_plane_copy_core_mmx2( dst, i_dst, src, i_src, (w+c_w)&~c_w, h-1 );
464         memcpy( dst+i_dst*(h-1), src+i_src*(h-1), w*sizeof(pixel) );
465     } else {
466         memcpy( dst, src, w*sizeof(pixel) );
467         x264_plane_copy_core_mmx2( dst+i_dst, i_dst, src+i_src, i_src, (w+c_w)&~c_w, h-1 );
468     }
469 }
470
471 #define PLANE_INTERLEAVE(cpu) \
472 static void x264_plane_copy_interleave_##cpu( pixel *dst,  intptr_t i_dst,\
473                                               pixel *srcu, intptr_t i_srcu,\
474                                               pixel *srcv, intptr_t i_srcv, int w, int h )\
475 {\
476     if( !(w&15) ) {\
477         x264_plane_copy_interleave_core_##cpu( dst, i_dst, srcu, i_srcu, srcv, i_srcv, w, h );\
478     } else if( w < 16 || (i_srcu ^ i_srcv) ) {\
479         x264_plane_copy_interleave_c( dst, i_dst, srcu, i_srcu, srcv, i_srcv, w, h );\
480     } else if( i_srcu > 0 ) {\
481         x264_plane_copy_interleave_core_##cpu( dst, i_dst, srcu, i_srcu, srcv, i_srcv, (w+15)&~15, h-1 );\
482         x264_plane_copy_interleave_c( dst+i_dst*(h-1), 0, srcu+i_srcu*(h-1), 0, srcv+i_srcv*(h-1), 0, w, 1 );\
483     } else {\
484         x264_plane_copy_interleave_c( dst, 0, srcu, 0, srcv, 0, w, 1 );\
485         x264_plane_copy_interleave_core_##cpu( dst+i_dst, i_dst, srcu+i_srcu, i_srcu, srcv+i_srcv, i_srcv, (w+15)&~15, h-1 );\
486     }\
487 }
488
489 PLANE_INTERLEAVE(mmx2)
490 PLANE_INTERLEAVE(sse2)
491 #if HIGH_BIT_DEPTH
492 PLANE_INTERLEAVE(avx)
493 #endif
494
495 void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
496 {
497     if( !(cpu&X264_CPU_MMX) )
498         return;
499
500     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_mmx;
501     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_mmx;
502
503     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_mmx;
504
505     pf->copy_16x16_unaligned = x264_mc_copy_w16_mmx;
506     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_mmx;
507     pf->copy[PIXEL_8x8]   = x264_mc_copy_w8_mmx;
508     pf->copy[PIXEL_4x4]   = x264_mc_copy_w4_mmx;
509     pf->memcpy_aligned  = x264_memcpy_aligned_mmx;
510     pf->memzero_aligned = x264_memzero_aligned_mmx;
511     pf->integral_init4v = x264_integral_init4v_mmx;
512     pf->integral_init8v = x264_integral_init8v_mmx;
513
514     if( !(cpu&X264_CPU_MMX2) )
515         return;
516
517     pf->prefetch_fenc_420 = x264_prefetch_fenc_420_mmx2;
518     pf->prefetch_fenc_422 = x264_prefetch_fenc_422_mmx2;
519     pf->prefetch_ref  = x264_prefetch_ref_mmx2;
520
521     pf->plane_copy = x264_plane_copy_mmx2;
522     pf->plane_copy_interleave = x264_plane_copy_interleave_mmx2;
523     pf->store_interleave_chroma = x264_store_interleave_chroma_mmx2;
524
525     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_mmx2;
526     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_mmx2;
527     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_mmx2;
528     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_mmx2;
529     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_mmx2;
530     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_mmx2;
531     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_mmx2;
532     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_mmx2;
533     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_mmx2;
534
535     pf->mc_luma = mc_luma_mmx2;
536     pf->get_ref = get_ref_mmx2;
537     pf->mc_chroma = x264_mc_chroma_mmx2;
538     pf->hpel_filter = x264_hpel_filter_mmx2;
539     pf->weight = x264_mc_weight_wtab_mmx2;
540     pf->weight_cache = x264_weight_cache_mmx2;
541     pf->offsetadd = x264_mc_offsetadd_wtab_mmx2;
542     pf->offsetsub = x264_mc_offsetsub_wtab_mmx2;
543
544     pf->frame_init_lowres_core = x264_frame_init_lowres_core_mmx2;
545
546 #if HIGH_BIT_DEPTH
547 #if ARCH_X86 // all x86_64 cpus with cacheline split issues use sse2 instead
548     if( cpu&(X264_CPU_CACHELINE_32|X264_CPU_CACHELINE_64) )
549         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmx2;
550 #endif
551
552     if( !(cpu&X264_CPU_SSE2) )
553         return;
554
555     pf->frame_init_lowres_core = x264_frame_init_lowres_core_sse2;
556
557     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_sse2;
558     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_sse2;
559
560     pf->plane_copy_interleave   = x264_plane_copy_interleave_sse2;
561     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_sse2;
562
563     if( cpu&X264_CPU_SSE2_IS_FAST )
564     {
565         pf->get_ref = get_ref_sse2;
566         pf->mc_luma = mc_luma_sse2;
567         pf->hpel_filter = x264_hpel_filter_sse2;
568     }
569
570     pf->memcpy_aligned  = x264_memcpy_aligned_sse2;
571     pf->memzero_aligned = x264_memzero_aligned_sse2;
572     pf->integral_init4v = x264_integral_init4v_sse2;
573     pf->integral_init8v = x264_integral_init8v_sse2;
574     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_sse2;
575     pf->store_interleave_chroma = x264_store_interleave_chroma_sse2;
576     pf->offsetadd = x264_mc_offsetadd_wtab_sse2;
577     pf->offsetsub = x264_mc_offsetsub_wtab_sse2;
578
579     if( cpu&X264_CPU_SSE2_IS_SLOW )
580         return;
581
582     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_sse2;
583     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_sse2;
584     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_sse2;
585     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_sse2;
586     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_sse2;
587     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_sse2;
588     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_sse2;
589     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_sse2;
590     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_sse2;
591
592     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_sse2;
593     pf->weight = x264_mc_weight_wtab_sse2;
594
595     if( !(cpu&X264_CPU_STACK_MOD4) )
596         pf->mc_chroma = x264_mc_chroma_sse2;
597
598     if( !(cpu&X264_CPU_SSSE3) )
599         return;
600
601     pf->frame_init_lowres_core = x264_frame_init_lowres_core_ssse3;
602
603     if( (cpu&X264_CPU_SHUFFLE_IS_FAST) && !(cpu&X264_CPU_SLOW_ATOM) )
604         pf->integral_init4v = x264_integral_init4v_ssse3;
605
606     if( !(cpu&X264_CPU_AVX) )
607         return;
608
609     pf->frame_init_lowres_core = x264_frame_init_lowres_core_avx;
610     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_avx;
611     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_avx;
612     pf->plane_copy_interleave        = x264_plane_copy_interleave_avx;
613     pf->plane_copy_deinterleave      = x264_plane_copy_deinterleave_avx;
614     pf->store_interleave_chroma      = x264_store_interleave_chroma_avx;
615
616     if( !(cpu&X264_CPU_STACK_MOD4) )
617         pf->mc_chroma = x264_mc_chroma_avx;
618
619     if( cpu&X264_CPU_XOP )
620         pf->frame_init_lowres_core = x264_frame_init_lowres_core_xop;
621 #else // !HIGH_BIT_DEPTH
622
623 #if ARCH_X86 // all x86_64 cpus with cacheline split issues use sse2 instead
624     if( cpu&X264_CPU_CACHELINE_32 )
625     {
626         pf->mc_luma = mc_luma_cache32_mmx2;
627         pf->get_ref = get_ref_cache32_mmx2;
628         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmx2;
629     }
630     else if( cpu&X264_CPU_CACHELINE_64 )
631     {
632         pf->mc_luma = mc_luma_cache64_mmx2;
633         pf->get_ref = get_ref_cache64_mmx2;
634         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmx2;
635     }
636 #endif
637
638     if( !(cpu&X264_CPU_SSE2) )
639         return;
640
641     pf->memcpy_aligned = x264_memcpy_aligned_sse2;
642     pf->memzero_aligned = x264_memzero_aligned_sse2;
643     pf->integral_init4v = x264_integral_init4v_sse2;
644     pf->integral_init8v = x264_integral_init8v_sse2;
645     pf->hpel_filter = x264_hpel_filter_sse2_amd;
646     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_sse2;
647
648     if( cpu&X264_CPU_SSE2_IS_SLOW )
649         return;
650
651     pf->weight = x264_mc_weight_wtab_sse2;
652     if( !(cpu&X264_CPU_SLOW_ATOM) )
653     {
654         pf->offsetadd = x264_mc_offsetadd_wtab_sse2;
655         pf->offsetsub = x264_mc_offsetsub_wtab_sse2;
656     }
657
658     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_sse2;
659     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_sse2;
660     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_sse2;
661     pf->avg[PIXEL_8x16] = x264_pixel_avg_8x16_sse2;
662     pf->avg[PIXEL_8x8]  = x264_pixel_avg_8x8_sse2;
663     pf->avg[PIXEL_8x4]  = x264_pixel_avg_8x4_sse2;
664     pf->hpel_filter = x264_hpel_filter_sse2;
665     if( cpu&X264_CPU_SSE_MISALIGN )
666         pf->hpel_filter = x264_hpel_filter_sse2_misalign;
667     pf->frame_init_lowres_core = x264_frame_init_lowres_core_sse2;
668     if( !(cpu&X264_CPU_STACK_MOD4) )
669         pf->mc_chroma = x264_mc_chroma_sse2;
670
671     if( cpu&X264_CPU_SSE2_IS_FAST )
672     {
673         pf->store_interleave_chroma = x264_store_interleave_chroma_sse2; // FIXME sse2fast? sse2medium?
674         pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_sse2;
675         pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_sse2;
676         pf->plane_copy_interleave   = x264_plane_copy_interleave_sse2;
677         pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_sse2;
678         pf->mc_luma = mc_luma_sse2;
679         pf->get_ref = get_ref_sse2;
680         if( cpu&X264_CPU_CACHELINE_64 )
681         {
682             pf->mc_luma = mc_luma_cache64_sse2;
683             pf->get_ref = get_ref_cache64_sse2;
684         }
685         if( cpu&X264_CPU_SSE_MISALIGN )
686         {
687             pf->get_ref = get_ref_sse2_misalign;
688             if( !(cpu&X264_CPU_STACK_MOD4) )
689                 pf->mc_chroma = x264_mc_chroma_sse2_misalign;
690         }
691     }
692
693     if( !(cpu&X264_CPU_SSSE3) )
694         return;
695
696     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_ssse3;
697     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_ssse3;
698     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_ssse3;
699     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_ssse3;
700     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_ssse3;
701     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_ssse3;
702     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_ssse3;
703     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_ssse3;
704     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_ssse3;
705
706     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_ssse3;
707     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_ssse3;
708     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_ssse3;
709
710     pf->hpel_filter = x264_hpel_filter_ssse3;
711     pf->frame_init_lowres_core = x264_frame_init_lowres_core_ssse3;
712     if( !(cpu&X264_CPU_STACK_MOD4) )
713         pf->mc_chroma = x264_mc_chroma_ssse3;
714
715     if( cpu&X264_CPU_CACHELINE_64 )
716     {
717         if( !(cpu&X264_CPU_STACK_MOD4) )
718             pf->mc_chroma = x264_mc_chroma_ssse3_cache64;
719         pf->mc_luma = mc_luma_cache64_ssse3;
720         pf->get_ref = get_ref_cache64_ssse3;
721
722         /* ssse3 weight is slower on Nehalem, so only assign here. */
723         pf->weight_cache = x264_weight_cache_ssse3;
724         pf->weight = x264_mc_weight_wtab_ssse3;
725     }
726
727     if( (cpu&X264_CPU_SHUFFLE_IS_FAST) && !(cpu&X264_CPU_SLOW_ATOM) )
728         pf->integral_init4v = x264_integral_init4v_ssse3;
729
730     if( !(cpu&X264_CPU_SSE4) )
731         return;
732
733     pf->integral_init4h = x264_integral_init4h_sse4;
734     pf->integral_init8h = x264_integral_init8h_sse4;
735
736     if( !(cpu&X264_CPU_AVX) )
737         return;
738
739     pf->frame_init_lowres_core = x264_frame_init_lowres_core_avx;
740     pf->integral_init8h = x264_integral_init8h_avx;
741     pf->hpel_filter = x264_hpel_filter_avx;
742
743     /* ssse3 weight seems to be faster again on Sandy Bridge and Bulldozer. */
744     pf->weight_cache = x264_weight_cache_ssse3;
745     pf->weight = x264_mc_weight_wtab_ssse3;
746     if( !(cpu&X264_CPU_STACK_MOD4) )
747         pf->mc_chroma = x264_mc_chroma_avx;
748
749     if( cpu&X264_CPU_XOP )
750         pf->frame_init_lowres_core = x264_frame_init_lowres_core_xop;
751 #endif // HIGH_BIT_DEPTH
752
753     if( !(cpu&X264_CPU_AVX) )
754         return;
755     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_avx;
756
757     if( !(cpu&X264_CPU_FMA4) )
758         return;
759     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_fma4;
760 }