1 /*****************************************************************************
2 * mc-c.c: x86 motion compensation
3 *****************************************************************************
4 * Copyright (C) 2003-2011 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
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.
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.
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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
32 #include "common/common.h"
35 #define DECL_SUF( func, args )\
36 void func##_mmx2 args;\
37 void func##_sse2 args;\
38 void func##_ssse3 args;
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 ))
50 #define MC_WEIGHT(w,type) \
51 void x264_mc_weight_w##w##_##type( pixel *,int, pixel *,int, const x264_weight_t *,int );
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 ); \
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 )
67 MC_WEIGHT_OFFSET( 8, sse2 )
72 MC_WEIGHT( 12, ssse3 )
73 MC_WEIGHT( 16, ssse3 )
74 MC_WEIGHT( 20, ssse3 )
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 );
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 );
154 MC_CHROMA(sse2_misalign)
156 MC_CHROMA(ssse3_cache64)
158 MC_CHROMA(avx_cache64)
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 );
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);
175 PIXEL_AVG_WALL(cache32_mmx2)
176 PIXEL_AVG_WALL(cache64_mmx2)
177 PIXEL_AVG_WALL(cache64_sse2)
179 PIXEL_AVG_WALL(sse2_misalign)
180 PIXEL_AVG_WALL(cache64_ssse3)
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 ) =\
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,\
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
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
207 PIXEL_AVG_WTAB(mmx2, mmx2, mmx2, mmx2, mmx2, mmx2)
209 PIXEL_AVG_WTAB(sse2, mmx2, sse2, sse2, sse2, sse2)
210 #else // !HIGH_BIT_DEPTH
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)
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
221 #define MC_COPY_WTAB(instr, name1, name2, name3)\
222 static void (* const x264_mc_copy_wtab_##instr[5])( pixel *, int, pixel *, int, int ) =\
225 x264_mc_copy_w4_##name1,\
226 x264_mc_copy_w8_##name2,\
228 x264_mc_copy_w16_##name3,\
231 MC_COPY_WTAB(mmx,mmx,mmx,mmx)
233 MC_COPY_WTAB(sse2,mmx,sse2,sse2)
235 MC_COPY_WTAB(sse2,mmx,mmx,sse2)
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 ) =\
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,\
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)
257 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
259 if( w->i_scale == 1<<w->i_denom )
261 if( w->i_offset < 0 )
262 w->weightfn = h->mc.offsetsub;
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));
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++ )
276 w->cacheb[i] = i&1 ? den3 : den2;
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)
288 static void x264_weight_cache_mmx2( x264_t *h, x264_weight_t *w )
293 if( w->i_scale == 1<<w->i_denom )
295 if( w->i_offset < 0 )
296 w->weightfn = h->mc.offsetsub;
298 w->weightfn = h->mc.offsetadd;
299 memset( w->cachea, abs(w->i_offset), sizeof(w->cachea) );
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++ )
306 w->cachea[i] = w->i_scale;
311 static void x264_weight_cache_ssse3( x264_t *h, x264_weight_t *w )
314 if( w->i_scale == 1<<w->i_denom )
316 if( w->i_offset < 0 )
317 w->weightfn = h->mc.offsetsub;
319 w->weightfn = h->mc.offsetadd;
321 memset( w->cachea, abs( w->i_offset ), sizeof(w->cachea) );
324 w->weightfn = h->mc.weight;
325 den1 = w->i_scale << (8 - w->i_denom);
326 for( i = 0; i < 8; i++ )
329 w->cacheb[i] = w->i_offset;
332 #endif // !HIGH_BIT_DEPTH
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};
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,\
341 int i_width, int i_height, const x264_weight_t *weight )\
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 */\
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,\
352 if( weight->weightfn )\
353 weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );\
355 else if( weight->weightfn )\
356 weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );\
358 x264_mc_copy_wtab_##instr2[i_width>>2](dst, i_dst_stride, src1, i_src_stride, i_height );\
361 MC_LUMA(mmx2,mmx2,mmx)
362 MC_LUMA(sse2,sse2,sse2)
365 MC_LUMA(cache32_mmx2,cache32_mmx2,mmx)
366 MC_LUMA(cache64_mmx2,cache64_mmx2,mmx)
368 MC_LUMA(cache64_sse2,cache64_sse2,sse2)
369 MC_LUMA(cache64_ssse3,cache64_ssse3,sse2)
370 #endif // !HIGH_BIT_DEPTH
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,\
376 int i_width, int i_height, const x264_weight_t *weight )\
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 */\
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,\
387 if( weight->weightfn )\
388 weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );\
391 else if( weight->weightfn )\
393 weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );\
398 *i_dst_stride = i_src_stride;\
407 GET_REF(cache32_mmx2)
408 GET_REF(cache64_mmx2)
410 GET_REF(sse2_misalign)
411 GET_REF(cache64_sse2)
412 GET_REF(cache64_ssse3)
413 #endif // !HIGH_BIT_DEPTH
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 )\
422 int realign = (intptr_t)src & (align-1);\
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 );\
441 HPEL(8, mmx2, mmx2, mmx2, mmx2)
443 HPEL(16, sse2, sse2, sse2, sse2)
444 #else // !HIGH_BIT_DEPTH
445 HPEL(16, sse2_amd, mmx2, mmx2, sse2)
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 );
451 HPEL(16, sse2, sse2, sse2, sse2)
452 HPEL(16, ssse3, ssse3, ssse3, ssse3)
453 HPEL(16, avx, avx, avx, ssse3)
455 HPEL(16, sse2_misalign, sse2, sse2_misalign, sse2)
456 #endif // HIGH_BIT_DEPTH
458 static void x264_plane_copy_mmx2( pixel *dst, int i_dst, pixel *src, int i_src, int w, int h )
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) );
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 );
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 )\
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 );\
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 );\
493 PLANE_INTERLEAVE(mmx2)
494 PLANE_INTERLEAVE(sse2)
496 PLANE_INTERLEAVE(avx)
499 void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
501 if( !(cpu&X264_CPU_MMX) )
504 pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_mmx;
505 pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_mmx;
507 pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_mmx;
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;
518 if( !(cpu&X264_CPU_MMX2) )
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;
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;
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;
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;
548 pf->frame_init_lowres_core = x264_frame_init_lowres_core_mmx2;
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;
556 if( !(cpu&X264_CPU_SSE2) )
559 pf->frame_init_lowres_core = x264_frame_init_lowres_core_sse2;
561 pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_sse2;
562 pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_sse2;
564 pf->plane_copy_interleave = x264_plane_copy_interleave_sse2;
565 pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_sse2;
567 if( cpu&X264_CPU_SSE2_IS_FAST )
569 pf->get_ref = get_ref_sse2;
570 pf->mc_luma = mc_luma_sse2;
571 pf->hpel_filter = x264_hpel_filter_sse2;
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;
583 if( cpu&X264_CPU_SSE2_IS_SLOW )
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;
596 pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_sse2;
597 pf->weight = x264_mc_weight_wtab_sse2;
599 if( !(cpu&X264_CPU_STACK_MOD4) )
600 pf->mc_chroma = x264_mc_chroma_sse2;
602 if( !(cpu&X264_CPU_SSSE3) )
605 pf->frame_init_lowres_core = x264_frame_init_lowres_core_ssse3;
607 if( (cpu&X264_CPU_SHUFFLE_IS_FAST) && !(cpu&X264_CPU_SLOW_ATOM) )
608 pf->integral_init4v = x264_integral_init4v_ssse3;
610 if( !(cpu&X264_CPU_AVX) )
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;
619 if( !(cpu&X264_CPU_STACK_MOD4) )
620 pf->mc_chroma = x264_mc_chroma_avx;
621 #else // !HIGH_BIT_DEPTH
623 #if ARCH_X86 // all x86_64 cpus with cacheline split issues use sse2 instead
624 if( cpu&X264_CPU_CACHELINE_32 )
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;
630 else if( cpu&X264_CPU_CACHELINE_64 )
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;
638 if( !(cpu&X264_CPU_SSE2) )
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;
648 if( cpu&X264_CPU_SSE2_IS_SLOW )
651 pf->weight = x264_mc_weight_wtab_sse2;
652 if( !(cpu&X264_CPU_SLOW_ATOM) )
654 pf->offsetadd = x264_mc_offsetadd_wtab_sse2;
655 pf->offsetsub = x264_mc_offsetsub_wtab_sse2;
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;
671 if( cpu&X264_CPU_SSE2_IS_FAST )
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 )
682 pf->mc_luma = mc_luma_cache64_sse2;
683 pf->get_ref = get_ref_cache64_sse2;
685 if( cpu&X264_CPU_SSE_MISALIGN )
687 pf->get_ref = get_ref_sse2_misalign;
688 if( !(cpu&X264_CPU_STACK_MOD4) )
689 pf->mc_chroma = x264_mc_chroma_sse2_misalign;
693 if( !(cpu&X264_CPU_SSSE3) )
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;
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;
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;
715 if( cpu&X264_CPU_CACHELINE_64 )
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;
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;
727 if( (cpu&X264_CPU_SHUFFLE_IS_FAST) && !(cpu&X264_CPU_SLOW_ATOM) )
728 pf->integral_init4v = x264_integral_init4v_ssse3;
730 if( !(cpu&X264_CPU_SSE4) )
733 pf->integral_init4h = x264_integral_init4h_sse4;
734 pf->integral_init8h = x264_integral_init8h_sse4;
736 if( !(cpu&X264_CPU_AVX) )
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
745 if( !(cpu&X264_CPU_AVX) )
747 pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_avx;
749 if( !(cpu&X264_CPU_FMA4) )
751 pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_fma4;