]> git.sesse.net Git - x264/blob - common/x86/mc-c.c
Split prefetch_fenc between colorspaces
[x264] / common / x86 / mc-c.c
1 /*****************************************************************************
2  * mc-c.c: x86 motion compensation
3  *****************************************************************************
4  * Copyright (C) 2003-2011 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 *, int, pixel *, int, pixel *, int, int ))
41 DECL_SUF( x264_pixel_avg_16x8,  ( pixel *, int, pixel *, int, pixel *, int, int ))
42 DECL_SUF( x264_pixel_avg_8x16,  ( pixel *, int, pixel *, int, pixel *, int, int ))
43 DECL_SUF( x264_pixel_avg_8x8,   ( pixel *, int, pixel *, int, pixel *, int, int ))
44 DECL_SUF( x264_pixel_avg_8x4,   ( pixel *, int, pixel *, int, pixel *, int, int ))
45 DECL_SUF( x264_pixel_avg_4x16,  ( pixel *, int, pixel *, int, pixel *, int, int ))
46 DECL_SUF( x264_pixel_avg_4x8,   ( pixel *, int, pixel *, int, pixel *, int, int ))
47 DECL_SUF( x264_pixel_avg_4x4,   ( pixel *, int, pixel *, int, pixel *, int, int ))
48 DECL_SUF( x264_pixel_avg_4x2,   ( pixel *, int, pixel *, int, pixel *, int, int ))
49
50 #define MC_WEIGHT(w,type) \
51     void x264_mc_weight_w##w##_##type( pixel *,int, pixel *,int, const x264_weight_t *,int );
52
53 #define MC_WEIGHT_OFFSET(w,type) \
54     void x264_mc_offsetadd_w##w##_##type( pixel *,int, pixel *,int, const x264_weight_t *,int ); \
55     void x264_mc_offsetsub_w##w##_##type( pixel *,int, pixel *,int, 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 MC_WEIGHT( 4, avx )
76 MC_WEIGHT( 8, avx )
77 MC_WEIGHT( 12, avx )
78 MC_WEIGHT( 16, avx )
79 MC_WEIGHT( 20, avx )
80 #undef MC_OFFSET
81 #undef MC_WEIGHT
82
83 void x264_mc_copy_w4_mmx( pixel *, int, pixel *, int, int );
84 void x264_mc_copy_w8_mmx( pixel *, int, pixel *, int, int );
85 void x264_mc_copy_w8_sse2( pixel *, int, pixel *, int, int );
86 void x264_mc_copy_w16_mmx( pixel *, int, pixel *, int, int );
87 void x264_mc_copy_w16_sse2( pixel *, int, pixel *, int, int );
88 void x264_mc_copy_w16_aligned_sse2( pixel *, int, pixel *, int, int );
89 void x264_prefetch_fenc_420_mmx2( pixel *, int, pixel *, int, int );
90 void x264_prefetch_fenc_422_mmx2( pixel *, int, pixel *, int, int );
91 void x264_prefetch_ref_mmx2( pixel *, int, int );
92 void x264_plane_copy_core_mmx2( pixel *, int, pixel *, int, int w, int h);
93 void x264_plane_copy_c( pixel *, int, pixel *, int, int w, int h );
94 void x264_plane_copy_interleave_core_mmx2( pixel *dst, int i_dst,
95                                              pixel *srcu, int i_srcu,
96                                              pixel *srcv, int i_srcv, int w, int h );
97 void x264_plane_copy_interleave_core_sse2( pixel *dst, int i_dst,
98                                            pixel *srcu, int i_srcu,
99                                            pixel *srcv, int i_srcv, int w, int h );
100 void x264_plane_copy_interleave_core_avx( pixel *dst, int i_dst,
101                                            pixel *srcu, int i_srcu,
102                                            pixel *srcv, int i_srcv, int w, int h );
103 void x264_plane_copy_interleave_c( pixel *dst, int i_dst,
104                                    pixel *srcu, int i_srcu,
105                                    pixel *srcv, int i_srcv, int w, int h );
106 void x264_plane_copy_deinterleave_mmx( pixel *dstu, int i_dstu,
107                                        pixel *dstv, int i_dstv,
108                                        pixel *src, int i_src, int w, int h );
109 void x264_plane_copy_deinterleave_sse2( pixel *dstu, int i_dstu,
110                                         pixel *dstv, int i_dstv,
111                                         pixel *src, int i_src, int w, int h );
112 void x264_plane_copy_deinterleave_ssse3( uint8_t *dstu, int i_dstu,
113                                          uint8_t *dstv, int i_dstv,
114                                          uint8_t *src, int i_src, int w, int h );
115 void x264_plane_copy_deinterleave_avx( uint16_t *dstu, int i_dstu,
116                                          uint16_t *dstv, int i_dstv,
117                                          uint16_t *src, int i_src, int w, int h );
118 void x264_store_interleave_chroma_mmx2( pixel *dst, int i_dst, pixel *srcu, pixel *srcv, int height );
119 void x264_store_interleave_chroma_sse2( pixel *dst, int i_dst, pixel *srcu, pixel *srcv, int height );
120 void x264_store_interleave_chroma_avx( pixel *dst, int i_dst, pixel *srcu, pixel *srcv, int height );
121 void x264_load_deinterleave_chroma_fenc_mmx( pixel *dst, pixel *src, int i_src, int height );
122 void x264_load_deinterleave_chroma_fenc_sse2( pixel *dst, pixel *src, int i_src, int height );
123 void x264_load_deinterleave_chroma_fenc_ssse3( uint8_t *dst, uint8_t *src, int i_src, int height );
124 void x264_load_deinterleave_chroma_fenc_avx( uint16_t *dst, uint16_t *src, int i_src, int height );
125 void x264_load_deinterleave_chroma_fdec_mmx( pixel *dst, pixel *src, int i_src, int height );
126 void x264_load_deinterleave_chroma_fdec_sse2( pixel *dst, pixel *src, int i_src, int height );
127 void x264_load_deinterleave_chroma_fdec_ssse3( uint8_t *dst, uint8_t *src, int i_src, int height );
128 void x264_load_deinterleave_chroma_fdec_avx( uint16_t *dst, uint16_t *src, int i_src, int height );
129 void *x264_memcpy_aligned_mmx( void * dst, const void * src, size_t n );
130 void *x264_memcpy_aligned_sse2( void * dst, const void * src, size_t n );
131 void x264_memzero_aligned_mmx( void * dst, int n );
132 void x264_memzero_aligned_sse2( void * dst, int n );
133 void x264_integral_init4h_sse4( uint16_t *sum, uint8_t *pix, int stride );
134 void x264_integral_init8h_sse4( uint16_t *sum, uint8_t *pix, int stride );
135 void x264_integral_init8h_avx ( uint16_t *sum, uint8_t *pix, int stride );
136 void x264_integral_init4v_mmx( uint16_t *sum8, uint16_t *sum4, int stride );
137 void x264_integral_init4v_sse2( uint16_t *sum8, uint16_t *sum4, int stride );
138 void x264_integral_init8v_mmx( uint16_t *sum8, int stride );
139 void x264_integral_init8v_sse2( uint16_t *sum8, int stride );
140 void x264_integral_init4v_ssse3( uint16_t *sum8, uint16_t *sum4, int stride );
141 void x264_mbtree_propagate_cost_sse2( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
142                                       uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
143 void x264_mbtree_propagate_cost_avx( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
144                                      uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
145 void x264_mbtree_propagate_cost_fma4( int *dst, uint16_t *propagate_in, uint16_t *intra_costs,
146                                       uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );
147
148 #define MC_CHROMA(cpu)\
149 void x264_mc_chroma_##cpu( pixel *dstu, pixel *dstv, int i_dst,\
150                            pixel *src, int i_src,\
151                            int dx, int dy, int i_width, int i_height );
152 MC_CHROMA(mmx2)
153 MC_CHROMA(sse2)
154 MC_CHROMA(sse2_misalign)
155 MC_CHROMA(ssse3)
156 MC_CHROMA(ssse3_cache64)
157 MC_CHROMA(avx)
158 MC_CHROMA(avx_cache64)
159
160 #define LOWRES(cpu)\
161 void x264_frame_init_lowres_core_##cpu( pixel *src0, pixel *dst0, pixel *dsth, pixel *dstv, pixel *dstc,\
162                                         int src_stride, int dst_stride, int width, int height );
163 LOWRES(mmx2)
164 LOWRES(cache32_mmx2)
165 LOWRES(sse2)
166 LOWRES(ssse3)
167
168 #define PIXEL_AVG_W(width,cpu)\
169 void x264_pixel_avg2_w##width##_##cpu( pixel *, int, pixel *, int, pixel *, int );
170 /* This declares some functions that don't exist, but that isn't a problem. */
171 #define PIXEL_AVG_WALL(cpu)\
172 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);
173
174 PIXEL_AVG_WALL(mmx2)
175 PIXEL_AVG_WALL(cache32_mmx2)
176 PIXEL_AVG_WALL(cache64_mmx2)
177 PIXEL_AVG_WALL(cache64_sse2)
178 PIXEL_AVG_WALL(sse2)
179 PIXEL_AVG_WALL(sse2_misalign)
180 PIXEL_AVG_WALL(cache64_ssse3)
181
182 #define PIXEL_AVG_WTAB(instr, name1, name2, name3, name4, name5)\
183 static void (* const x264_pixel_avg_wtab_##instr[6])( pixel *, int, pixel *, int, pixel *, int ) =\
184 {\
185     NULL,\
186     x264_pixel_avg2_w4_##name1,\
187     x264_pixel_avg2_w8_##name2,\
188     x264_pixel_avg2_w12_##name3,\
189     x264_pixel_avg2_w16_##name4,\
190     x264_pixel_avg2_w20_##name5,\
191 };
192
193 #if HIGH_BIT_DEPTH
194 /* we can replace w12/w20 with w10/w18 as only 9/17 pixels in fact are important */
195 #define x264_pixel_avg2_w12_mmx2       x264_pixel_avg2_w10_mmx2
196 #define x264_pixel_avg2_w20_mmx2       x264_pixel_avg2_w18_mmx2
197 #define x264_pixel_avg2_w12_sse2         x264_pixel_avg2_w10_sse2
198 #define x264_pixel_avg2_w20_sse2         x264_pixel_avg2_w18_sse2
199 #else
200 /* w16 sse2 is faster than w12 mmx as long as the cacheline issue is resolved */
201 #define x264_pixel_avg2_w12_cache64_ssse3 x264_pixel_avg2_w16_cache64_ssse3
202 #define x264_pixel_avg2_w12_cache64_sse2 x264_pixel_avg2_w16_cache64_sse2
203 #define x264_pixel_avg2_w12_sse3         x264_pixel_avg2_w16_sse3
204 #define x264_pixel_avg2_w12_sse2         x264_pixel_avg2_w16_sse2
205 #endif // HIGH_BIT_DEPTH
206
207 PIXEL_AVG_WTAB(mmx2, mmx2, mmx2, mmx2, mmx2, mmx2)
208 #if HIGH_BIT_DEPTH
209 PIXEL_AVG_WTAB(sse2, mmx2, sse2, sse2, sse2, sse2)
210 #else // !HIGH_BIT_DEPTH
211 #if ARCH_X86
212 PIXEL_AVG_WTAB(cache32_mmx2, mmx2, cache32_mmx2, cache32_mmx2, cache32_mmx2, cache32_mmx2)
213 PIXEL_AVG_WTAB(cache64_mmx2, mmx2, cache64_mmx2, cache64_mmx2, cache64_mmx2, cache64_mmx2)
214 #endif
215 PIXEL_AVG_WTAB(sse2, mmx2, mmx2, sse2, sse2, sse2)
216 PIXEL_AVG_WTAB(sse2_misalign, mmx2, mmx2, sse2, sse2, sse2_misalign)
217 PIXEL_AVG_WTAB(cache64_sse2, mmx2, cache64_mmx2, cache64_sse2, cache64_sse2, cache64_sse2)
218 PIXEL_AVG_WTAB(cache64_ssse3, mmx2, cache64_mmx2, cache64_ssse3, cache64_ssse3, cache64_sse2)
219 #endif // HIGH_BIT_DEPTH
220
221 #define MC_COPY_WTAB(instr, name1, name2, name3)\
222 static void (* const x264_mc_copy_wtab_##instr[5])( pixel *, int, pixel *, int, int ) =\
223 {\
224     NULL,\
225     x264_mc_copy_w4_##name1,\
226     x264_mc_copy_w8_##name2,\
227     NULL,\
228     x264_mc_copy_w16_##name3,\
229 };
230
231 MC_COPY_WTAB(mmx,mmx,mmx,mmx)
232 #if HIGH_BIT_DEPTH
233 MC_COPY_WTAB(sse2,mmx,sse2,sse2)
234 #else
235 MC_COPY_WTAB(sse2,mmx,mmx,sse2)
236 #endif
237
238 #define MC_WEIGHT_WTAB(function, instr, name1, name2, w12version)\
239     static void (* x264_mc_##function##_wtab_##instr[6])( pixel *, int, pixel *, int, const x264_weight_t *, int ) =\
240 {\
241     x264_mc_##function##_w4_##name1,\
242     x264_mc_##function##_w4_##name1,\
243     x264_mc_##function##_w8_##name2,\
244     x264_mc_##function##_w##w12version##_##instr,\
245     x264_mc_##function##_w16_##instr,\
246     x264_mc_##function##_w20_##instr,\
247 };
248
249 #if HIGH_BIT_DEPTH
250 MC_WEIGHT_WTAB(weight,mmx2,mmx2,mmx2,12)
251 MC_WEIGHT_WTAB(offsetadd,mmx2,mmx2,mmx2,12)
252 MC_WEIGHT_WTAB(offsetsub,mmx2,mmx2,mmx2,12)
253 MC_WEIGHT_WTAB(weight,sse2,mmx2,sse2,12)
254 MC_WEIGHT_WTAB(offsetadd,sse2,mmx2,sse2,16)
255 MC_WEIGHT_WTAB(offsetsub,sse2,mmx2,sse2,16)
256
257 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
258 {
259     if( w->i_scale == 1<<w->i_denom )
260     {
261         if( w->i_offset < 0 )
262             w->weightfn = h->mc.offsetsub;
263         else
264             w->weightfn = h->mc.offsetadd;
265         for( int i = 0; i < 8; i++ )
266             w->cachea[i] = abs(w->i_offset<<(BIT_DEPTH-8));
267         return;
268     }
269     w->weightfn = h->mc.weight;
270     int den1 = 1<<w->i_denom;
271     int den2 = w->i_scale<<1;
272     int den3 = 1+(w->i_offset<<(BIT_DEPTH-8+1));
273     for( int i = 0; i < 8; i++ )
274     {
275         w->cachea[i] = den1;
276         w->cacheb[i] = i&1 ? den3 : den2;
277     }
278 }
279 #else
280 MC_WEIGHT_WTAB(weight,mmx2,mmx2,mmx2,12)
281 MC_WEIGHT_WTAB(offsetadd,mmx2,mmx2,mmx2,12)
282 MC_WEIGHT_WTAB(offsetsub,mmx2,mmx2,mmx2,12)
283 MC_WEIGHT_WTAB(weight,sse2,mmx2,sse2,16)
284 MC_WEIGHT_WTAB(offsetadd,sse2,mmx2,mmx2,16)
285 MC_WEIGHT_WTAB(offsetsub,sse2,mmx2,mmx2,16)
286 MC_WEIGHT_WTAB(weight,ssse3,ssse3,ssse3,16)
287
288 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
289 {
290     int i;
291     int16_t den1;
292
293     if( w->i_scale == 1<<w->i_denom )
294     {
295         if( w->i_offset < 0 )
296             w->weightfn = h->mc.offsetsub;
297         else
298             w->weightfn = h->mc.offsetadd;
299         memset( w->cachea, abs(w->i_offset), sizeof(w->cachea) );
300         return;
301     }
302     w->weightfn = h->mc.weight;
303     den1 = 1 << (w->i_denom - 1) | w->i_offset << w->i_denom;
304     for( i = 0; i < 8; i++ )
305     {
306         w->cachea[i] = w->i_scale;
307         w->cacheb[i] = den1;
308     }
309 }
310
311 static void x264_weight_cache_ssse3( x264_t *h, x264_weight_t *w )
312 {
313     int i, den1;
314     if( w->i_scale == 1<<w->i_denom )
315     {
316         if( w->i_offset < 0 )
317             w->weightfn = h->mc.offsetsub;
318         else
319             w->weightfn = h->mc.offsetadd;
320
321         memset( w->cachea, abs( w->i_offset ), sizeof(w->cachea) );
322         return;
323     }
324     w->weightfn = h->mc.weight;
325     den1 = w->i_scale << (8 - w->i_denom);
326     for( i = 0; i < 8; i++ )
327     {
328         w->cachea[i] = den1;
329         w->cacheb[i] = w->i_offset;
330     }
331 }
332 #endif // !HIGH_BIT_DEPTH
333
334 static const uint8_t hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};
335 static const uint8_t hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};
336
337 #define MC_LUMA(name,instr1,instr2)\
338 static void mc_luma_##name( pixel *dst,    int i_dst_stride,\
339                   pixel *src[4], int i_src_stride,\
340                   int mvx, int mvy,\
341                   int i_width, int i_height, const x264_weight_t *weight )\
342 {\
343     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
344     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
345     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
346     if( qpel_idx & 5 ) /* qpel interpolation needed */\
347     {\
348         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
349         x264_pixel_avg_wtab_##instr1[i_width>>2](\
350                 dst, i_dst_stride, src1, i_src_stride,\
351                 src2, i_height );\
352         if( weight->weightfn )\
353             weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );\
354     }\
355     else if( weight->weightfn )\
356         weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );\
357     else\
358         x264_mc_copy_wtab_##instr2[i_width>>2](dst, i_dst_stride, src1, i_src_stride, i_height );\
359 }
360
361 MC_LUMA(mmx2,mmx2,mmx)
362 MC_LUMA(sse2,sse2,sse2)
363 #if !HIGH_BIT_DEPTH
364 #if ARCH_X86
365 MC_LUMA(cache32_mmx2,cache32_mmx2,mmx)
366 MC_LUMA(cache64_mmx2,cache64_mmx2,mmx)
367 #endif
368 MC_LUMA(cache64_sse2,cache64_sse2,sse2)
369 MC_LUMA(cache64_ssse3,cache64_ssse3,sse2)
370 #endif // !HIGH_BIT_DEPTH
371
372 #define GET_REF(name)\
373 static pixel *get_ref_##name( pixel *dst,   int *i_dst_stride,\
374                          pixel *src[4], int i_src_stride,\
375                          int mvx, int mvy,\
376                          int i_width, int i_height, const x264_weight_t *weight )\
377 {\
378     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
379     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
380     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
381     if( qpel_idx & 5 ) /* qpel interpolation needed */\
382     {\
383         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
384         x264_pixel_avg_wtab_##name[i_width>>2](\
385                 dst, *i_dst_stride, src1, i_src_stride,\
386                 src2, i_height );\
387         if( weight->weightfn )\
388             weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );\
389         return dst;\
390     }\
391     else if( weight->weightfn )\
392     {\
393         weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );\
394         return dst;\
395     }\
396     else\
397     {\
398         *i_dst_stride = i_src_stride;\
399         return src1;\
400     }\
401 }
402
403 GET_REF(mmx2)
404 GET_REF(sse2)
405 #if !HIGH_BIT_DEPTH
406 #if ARCH_X86
407 GET_REF(cache32_mmx2)
408 GET_REF(cache64_mmx2)
409 #endif
410 GET_REF(sse2_misalign)
411 GET_REF(cache64_sse2)
412 GET_REF(cache64_ssse3)
413 #endif // !HIGH_BIT_DEPTH
414
415 #define HPEL(align, cpu, cpuv, cpuc, cpuh)\
416 void x264_hpel_filter_v_##cpuv( pixel *dst, pixel *src, int16_t *buf, int stride, int width);\
417 void x264_hpel_filter_c_##cpuc( pixel *dst, int16_t *buf, int width );\
418 void x264_hpel_filter_h_##cpuh( pixel *dst, pixel *src, int width );\
419 static void x264_hpel_filter_##cpu( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,\
420                              int stride, int width, int height, int16_t *buf )\
421 {\
422     int realign = (intptr_t)src & (align-1);\
423     src -= realign;\
424     dstv -= realign;\
425     dstc -= realign;\
426     dsth -= realign;\
427     width += realign;\
428     while( height-- )\
429     {\
430         x264_hpel_filter_v_##cpuv( dstv, src, buf+8, stride, width );\
431         x264_hpel_filter_c_##cpuc( dstc, buf+8, width );\
432         x264_hpel_filter_h_##cpuh( dsth, src, width );\
433         dsth += stride;\
434         dstv += stride;\
435         dstc += stride;\
436         src  += stride;\
437     }\
438     x264_sfence();\
439 }
440
441 HPEL(8, mmx2, mmx2, mmx2, mmx2)
442 #if HIGH_BIT_DEPTH
443 HPEL(16, sse2, sse2, sse2, sse2)
444 #else // !HIGH_BIT_DEPTH
445 HPEL(16, sse2_amd, mmx2, mmx2, sse2)
446 #if ARCH_X86_64
447 void x264_hpel_filter_sse2( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, int stride, int width, int height, int16_t *buf );
448 void x264_hpel_filter_ssse3( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, int stride, int width, int height, int16_t *buf );
449 void x264_hpel_filter_avx( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src, int stride, int width, int height, int16_t *buf );
450 #else
451 HPEL(16, sse2, sse2, sse2, sse2)
452 HPEL(16, ssse3, ssse3, ssse3, ssse3)
453 HPEL(16, avx, avx, avx, ssse3)
454 #endif
455 HPEL(16, sse2_misalign, sse2, sse2_misalign, sse2)
456 #endif // HIGH_BIT_DEPTH
457
458 static void x264_plane_copy_mmx2( pixel *dst, int i_dst, pixel *src, int i_src, int w, int h )
459 {
460     int c_w = 16/sizeof(pixel) - 1;
461     if( w < 256 ) { // tiny resolutions don't want non-temporal hints. dunno the exact threshold.
462         x264_plane_copy_c( dst, i_dst, src, i_src, w, h );
463     } else if( !(w&c_w) ) {
464         x264_plane_copy_core_mmx2( dst, i_dst, src, i_src, w, h );
465     } else if( i_src > 0 ) {
466         // have to use plain memcpy on the last line (in memory order) to avoid overreading src
467         x264_plane_copy_core_mmx2( dst, i_dst, src, i_src, (w+c_w)&~c_w, h-1 );
468         memcpy( dst+i_dst*(h-1), src+i_src*(h-1), w*sizeof(pixel) );
469     } else {
470         memcpy( dst, src, w*sizeof(pixel) );
471         x264_plane_copy_core_mmx2( dst+i_dst, i_dst, src+i_src, i_src, (w+c_w)&~c_w, h-1 );
472     }
473 }
474
475 #define PLANE_INTERLEAVE(cpu) \
476 static void x264_plane_copy_interleave_##cpu( pixel *dst, int i_dst,\
477                                               pixel *srcu, int i_srcu,\
478                                               pixel *srcv, int i_srcv, int w, int h )\
479 {\
480     if( !(w&15) ) {\
481         x264_plane_copy_interleave_core_##cpu( dst, i_dst, srcu, i_srcu, srcv, i_srcv, w, h );\
482     } else if( w < 16 || (i_srcu ^ i_srcv) ) {\
483         x264_plane_copy_interleave_c( dst, i_dst, srcu, i_srcu, srcv, i_srcv, w, h );\
484     } else if( i_srcu > 0 ) {\
485         x264_plane_copy_interleave_core_##cpu( dst, i_dst, srcu, i_srcu, srcv, i_srcv, (w+15)&~15, h-1 );\
486         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 );\
487     } else {\
488         x264_plane_copy_interleave_c( dst, 0, srcu, 0, srcv, 0, w, 1 );\
489         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 );\
490     }\
491 }
492
493 PLANE_INTERLEAVE(mmx2)
494 PLANE_INTERLEAVE(sse2)
495 #if HIGH_BIT_DEPTH
496 PLANE_INTERLEAVE(avx)
497 #endif
498
499 void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
500 {
501     if( !(cpu&X264_CPU_MMX) )
502         return;
503
504     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_mmx;
505     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_mmx;
506
507     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_mmx;
508
509     pf->copy_16x16_unaligned = x264_mc_copy_w16_mmx;
510     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_mmx;
511     pf->copy[PIXEL_8x8]   = x264_mc_copy_w8_mmx;
512     pf->copy[PIXEL_4x4]   = x264_mc_copy_w4_mmx;
513     pf->memcpy_aligned  = x264_memcpy_aligned_mmx;
514     pf->memzero_aligned = x264_memzero_aligned_mmx;
515     pf->integral_init4v = x264_integral_init4v_mmx;
516     pf->integral_init8v = x264_integral_init8v_mmx;
517
518     if( !(cpu&X264_CPU_MMX2) )
519         return;
520
521     pf->prefetch_fenc_420 = x264_prefetch_fenc_420_mmx2;
522     pf->prefetch_fenc_422 = x264_prefetch_fenc_422_mmx2;
523     pf->prefetch_ref  = x264_prefetch_ref_mmx2;
524
525     pf->plane_copy = x264_plane_copy_mmx2;
526     pf->plane_copy_interleave = x264_plane_copy_interleave_mmx2;
527     pf->store_interleave_chroma = x264_store_interleave_chroma_mmx2;
528
529     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_mmx2;
530     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_mmx2;
531     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_mmx2;
532     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_mmx2;
533     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_mmx2;
534     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_mmx2;
535     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_mmx2;
536     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_mmx2;
537     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_mmx2;
538
539     pf->mc_luma = mc_luma_mmx2;
540     pf->get_ref = get_ref_mmx2;
541     pf->mc_chroma = x264_mc_chroma_mmx2;
542     pf->hpel_filter = x264_hpel_filter_mmx2;
543     pf->weight = x264_mc_weight_wtab_mmx2;
544     pf->weight_cache = x264_weight_cache_mmx2;
545     pf->offsetadd = x264_mc_offsetadd_wtab_mmx2;
546     pf->offsetsub = x264_mc_offsetsub_wtab_mmx2;
547
548     pf->frame_init_lowres_core = x264_frame_init_lowres_core_mmx2;
549
550 #if HIGH_BIT_DEPTH
551 #if ARCH_X86 // all x86_64 cpus with cacheline split issues use sse2 instead
552     if( cpu&(X264_CPU_CACHELINE_32|X264_CPU_CACHELINE_64) )
553         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmx2;
554 #endif
555
556     if( !(cpu&X264_CPU_SSE2) )
557         return;
558
559     pf->frame_init_lowres_core = x264_frame_init_lowres_core_sse2;
560
561     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_sse2;
562     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_sse2;
563
564     pf->plane_copy_interleave   = x264_plane_copy_interleave_sse2;
565     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_sse2;
566
567     if( cpu&X264_CPU_SSE2_IS_FAST )
568     {
569         pf->get_ref = get_ref_sse2;
570         pf->mc_luma = mc_luma_sse2;
571         pf->hpel_filter = x264_hpel_filter_sse2;
572     }
573
574     pf->memcpy_aligned  = x264_memcpy_aligned_sse2;
575     pf->memzero_aligned = x264_memzero_aligned_sse2;
576     pf->integral_init4v = x264_integral_init4v_sse2;
577     pf->integral_init8v = x264_integral_init8v_sse2;
578     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_sse2;
579     pf->store_interleave_chroma = x264_store_interleave_chroma_sse2;
580     pf->offsetadd = x264_mc_offsetadd_wtab_sse2;
581     pf->offsetsub = x264_mc_offsetsub_wtab_sse2;
582
583     if( cpu&X264_CPU_SSE2_IS_SLOW )
584         return;
585
586     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_sse2;
587     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_sse2;
588     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_sse2;
589     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_sse2;
590     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_sse2;
591     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_sse2;
592     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_sse2;
593     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_sse2;
594     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_sse2;
595
596     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_sse2;
597     pf->weight = x264_mc_weight_wtab_sse2;
598
599     if( !(cpu&X264_CPU_STACK_MOD4) )
600         pf->mc_chroma = x264_mc_chroma_sse2;
601
602     if( !(cpu&X264_CPU_SSSE3) )
603         return;
604
605     pf->frame_init_lowres_core = x264_frame_init_lowres_core_ssse3;
606
607     if( (cpu&X264_CPU_SHUFFLE_IS_FAST) && !(cpu&X264_CPU_SLOW_ATOM) )
608         pf->integral_init4v = x264_integral_init4v_ssse3;
609
610     if( !(cpu&X264_CPU_AVX) )
611         return;
612
613     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_avx;
614     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_avx;
615     pf->plane_copy_interleave        = x264_plane_copy_interleave_avx;
616     pf->plane_copy_deinterleave      = x264_plane_copy_deinterleave_avx;
617     pf->store_interleave_chroma      = x264_store_interleave_chroma_avx;
618
619     if( !(cpu&X264_CPU_STACK_MOD4) )
620         pf->mc_chroma = x264_mc_chroma_avx;
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->integral_init8h = x264_integral_init8h_avx;
740     pf->hpel_filter = x264_hpel_filter_avx;
741     if( !(cpu&X264_CPU_STACK_MOD4) )
742         pf->mc_chroma = x264_mc_chroma_avx;
743 #endif // HIGH_BIT_DEPTH
744
745     if( !(cpu&X264_CPU_AVX) )
746         return;
747     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_avx;
748
749     if( !(cpu&X264_CPU_FMA4) )
750         return;
751     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_fma4;
752 }