]> git.sesse.net Git - x264/blob - common/x86/mc-c.c
Update file headers throughout x264
[x264] / common / x86 / mc-c.c
1 /*****************************************************************************
2  * mc-c.c: h264 encoder library (Motion Compensation)
3  *****************************************************************************
4  * Copyright (C) 2003-2008 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
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "common/common.h"
30
31 /* NASM functions */
32 extern void x264_pixel_avg_16x16_sse2( uint8_t *, int, uint8_t *, int );
33 extern void x264_pixel_avg_16x8_sse2( uint8_t *, int, uint8_t *, int );
34 extern void x264_pixel_avg_16x16_mmxext( uint8_t *, int, uint8_t *, int );
35 extern void x264_pixel_avg_16x8_mmxext( uint8_t *, int, uint8_t *, int );
36 extern void x264_pixel_avg_8x16_mmxext( uint8_t *, int, uint8_t *, int );
37 extern void x264_pixel_avg_8x8_mmxext( uint8_t *, int, uint8_t *, int );
38 extern void x264_pixel_avg_8x4_mmxext( uint8_t *, int, uint8_t *, int );
39 extern void x264_pixel_avg_4x8_mmxext( uint8_t *, int, uint8_t *, int );
40 extern void x264_pixel_avg_4x4_mmxext( uint8_t *, int, uint8_t *, int );
41 extern void x264_pixel_avg_4x2_mmxext( uint8_t *, int, uint8_t *, int );
42 extern void x264_mc_copy_w4_mmx( uint8_t *, int, uint8_t *, int, int );
43 extern void x264_mc_copy_w8_mmx( uint8_t *, int, uint8_t *, int, int );
44 extern void x264_mc_copy_w16_mmx( uint8_t *, int, uint8_t *, int, int );
45 extern void x264_mc_copy_w16_sse2( uint8_t *, int, uint8_t *, int, int );
46 extern void x264_mc_copy_w16_sse3( uint8_t *, int, uint8_t *, int, int );
47 extern void x264_mc_copy_w16_aligned_sse2( uint8_t *, int, uint8_t *, int, int );
48 extern void x264_pixel_avg_weight_4x4_mmxext( uint8_t *, int, uint8_t *, int, int );
49 extern void x264_pixel_avg_weight_w8_mmxext( uint8_t *, int, uint8_t *, int, int, int );
50 extern void x264_pixel_avg_weight_w8_sse2( uint8_t *, int, uint8_t *, int, int, int );
51 extern void x264_pixel_avg_weight_w16_sse2( uint8_t *, int, uint8_t *, int, int, int );
52 extern void x264_pixel_avg_weight_w16_mmxext( uint8_t *, int, uint8_t *, int, int, int );
53 extern void x264_prefetch_fenc_mmxext( uint8_t *, int, uint8_t *, int, int );
54 extern void x264_prefetch_ref_mmxext( uint8_t *, int, int );
55 extern void x264_mc_chroma_mmxext( uint8_t *src, int i_src_stride,
56                                    uint8_t *dst, int i_dst_stride,
57                                    int dx, int dy, int i_width, int i_height );
58 extern void x264_mc_chroma_sse2( uint8_t *src, int i_src_stride,
59                                  uint8_t *dst, int i_dst_stride,
60                                  int dx, int dy, int i_width, int i_height );
61 extern void x264_mc_chroma_ssse3( uint8_t *src, int i_src_stride,
62                                   uint8_t *dst, int i_dst_stride,
63                                   int dx, int dy, int i_width, int i_height );
64 extern void x264_plane_copy_mmxext( uint8_t *, int, uint8_t *, int, int w, int h);
65 extern void *x264_memcpy_aligned_mmx( void * dst, const void * src, size_t n );
66 extern void *x264_memcpy_aligned_sse2( void * dst, const void * src, size_t n );
67 extern void x264_memzero_aligned_mmx( void * dst, int n );
68 extern void x264_memzero_aligned_sse2( void * dst, int n );
69 #define LOWRES(cpu) \
70 extern void x264_frame_init_lowres_core_##cpu( uint8_t *src0, uint8_t *dst0, uint8_t *dsth, uint8_t *dstv, uint8_t *dstc,\
71                                                int src_stride, int dst_stride, int width, int height );
72 LOWRES(mmxext)
73 LOWRES(cache32_mmxext)
74 LOWRES(sse2)
75 LOWRES(ssse3)
76
77 #define PIXEL_AVG_W(width,cpu)\
78 extern void x264_pixel_avg2_w##width##_##cpu( uint8_t *, int, uint8_t *, int, uint8_t *, int );
79 /* This declares some functions that don't exist, but that isn't a problem. */
80 #define PIXEL_AVG_WALL(cpu)\
81 PIXEL_AVG_W(4,cpu); PIXEL_AVG_W(8,cpu); PIXEL_AVG_W(12,cpu); PIXEL_AVG_W(16,cpu); PIXEL_AVG_W(20,cpu);
82
83 PIXEL_AVG_WALL(mmxext)
84 PIXEL_AVG_WALL(cache32_mmxext)
85 PIXEL_AVG_WALL(cache64_mmxext)
86 PIXEL_AVG_WALL(cache64_sse2)
87 PIXEL_AVG_WALL(sse2)
88
89 #define AVG_WEIGHT(W,H,name) \
90 void x264_pixel_avg_weight_ ## W ## x ## H ## _##name( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int i_weight_dst ) \
91 { \
92     x264_pixel_avg_weight_w ## W ## _##name( dst, i_dst, src, i_src, i_weight_dst, H ); \
93 }
94
95 AVG_WEIGHT(16,16,mmxext)
96 AVG_WEIGHT(16,8,mmxext)
97 AVG_WEIGHT(8,16,mmxext)
98 AVG_WEIGHT(8,8,mmxext)
99 AVG_WEIGHT(8,4,mmxext)
100 AVG_WEIGHT(16,16,sse2)
101 AVG_WEIGHT(16,8,sse2)
102 AVG_WEIGHT(8,16,sse2)
103 AVG_WEIGHT(8,8,sse2)
104 AVG_WEIGHT(8,4,sse2)
105
106 #define PIXEL_AVG_WTAB(instr, name1, name2, name3, name4, name5)\
107 static void (* const x264_pixel_avg_wtab_##instr[6])( uint8_t *, int, uint8_t *, int, uint8_t *, int ) =\
108 {\
109     NULL,\
110     x264_pixel_avg2_w4_##name1,\
111     x264_pixel_avg2_w8_##name2,\
112     x264_pixel_avg2_w12_##name3,\
113     x264_pixel_avg2_w16_##name4,\
114     x264_pixel_avg2_w20_##name5,\
115 };
116
117 /* w16 sse2 is faster than w12 mmx as long as the cacheline issue is resolved */
118 #define x264_pixel_avg2_w12_cache64_sse2 x264_pixel_avg2_w16_cache64_sse2
119 #define x264_pixel_avg2_w12_sse3         x264_pixel_avg2_w16_sse3
120
121 PIXEL_AVG_WTAB(mmxext, mmxext, mmxext, mmxext, mmxext, mmxext)
122 #ifdef ARCH_X86
123 PIXEL_AVG_WTAB(cache32_mmxext, mmxext, cache32_mmxext, cache32_mmxext, cache32_mmxext, cache32_mmxext)
124 #endif
125 PIXEL_AVG_WTAB(cache64_mmxext, mmxext, cache64_mmxext, cache64_mmxext, cache64_mmxext, cache64_mmxext)
126 PIXEL_AVG_WTAB(sse2, mmxext, mmxext, mmxext, sse2, sse2)
127 PIXEL_AVG_WTAB(cache64_sse2, mmxext, cache64_mmxext, cache64_sse2, cache64_sse2, cache64_sse2)
128
129 #define MC_COPY_WTAB(instr, name1, name2, name3)\
130 static void (* const x264_mc_copy_wtab_##instr[5])( uint8_t *, int, uint8_t *, int, int ) =\
131 {\
132     NULL,\
133     x264_mc_copy_w4_##name1,\
134     x264_mc_copy_w8_##name2,\
135     NULL,\
136     x264_mc_copy_w16_##name3,\
137 };
138
139 MC_COPY_WTAB(mmx,mmx,mmx,mmx)
140 MC_COPY_WTAB(sse2,mmx,mmx,sse2)
141
142 static const int hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};
143 static const int hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};
144
145 #define MC_LUMA(name,instr1,instr2)\
146 void mc_luma_##name( uint8_t *dst,    int i_dst_stride,\
147                   uint8_t *src[4], int i_src_stride,\
148                   int mvx, int mvy,\
149                   int i_width, int i_height )\
150 {\
151     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
152     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
153     uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
154     if( qpel_idx & 5 ) /* qpel interpolation needed */\
155     {\
156         uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
157         x264_pixel_avg_wtab_##instr1[i_width>>2](\
158                 dst, i_dst_stride, src1, i_src_stride,\
159                 src2, i_height );\
160     }\
161     else\
162     {\
163         x264_mc_copy_wtab_##instr2[i_width>>2](\
164                 dst, i_dst_stride, src1, i_src_stride, i_height );\
165     }\
166 }
167
168 MC_LUMA(mmxext,mmxext,mmx)
169 #ifdef ARCH_X86
170 MC_LUMA(cache32_mmxext,cache32_mmxext,mmx)
171 MC_LUMA(cache64_mmxext,cache64_mmxext,mmx)
172 #endif
173 MC_LUMA(sse2,sse2,sse2)
174 MC_LUMA(cache64_sse2,cache64_sse2,sse2)
175
176 #define GET_REF(name)\
177 uint8_t *get_ref_##name( uint8_t *dst,   int *i_dst_stride,\
178                          uint8_t *src[4], int i_src_stride,\
179                          int mvx, int mvy,\
180                          int i_width, int i_height )\
181 {\
182     int qpel_idx = ((mvy&3)<<2) + (mvx&3);\
183     int offset = (mvy>>2)*i_src_stride + (mvx>>2);\
184     uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;\
185     if( qpel_idx & 5 ) /* qpel interpolation needed */\
186     {\
187         uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);\
188         x264_pixel_avg_wtab_##name[i_width>>2](\
189                 dst, *i_dst_stride, src1, i_src_stride,\
190                 src2, i_height );\
191         return dst;\
192     }\
193     else\
194     {\
195         *i_dst_stride = i_src_stride;\
196         return src1;\
197     }\
198 }
199
200 GET_REF(mmxext)
201 #ifdef ARCH_X86
202 GET_REF(cache32_mmxext)
203 GET_REF(cache64_mmxext)
204 #endif
205 GET_REF(sse2)
206 GET_REF(cache64_sse2)
207
208 #define HPEL(align, cpu, cpuv, cpuc, cpuh)\
209 void x264_hpel_filter_v_##cpuv( uint8_t *dst, uint8_t *src, int16_t *buf, int stride, int width);\
210 void x264_hpel_filter_c_##cpuc( uint8_t *dst, int16_t *buf, int width );\
211 void x264_hpel_filter_h_##cpuh( uint8_t *dst, uint8_t *src, int width );\
212 void x264_sfence( void );\
213 void x264_hpel_filter_##cpu( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,\
214                              int stride, int width, int height )\
215 {\
216     int16_t *buf;\
217     int realign = (long)src & (align-1);\
218     src -= realign;\
219     dstv -= realign;\
220     dstc -= realign;\
221     dsth -= realign;\
222     width += realign;\
223     buf = x264_malloc((width+16)*sizeof(int16_t));\
224     while( height-- )\
225     {\
226         x264_hpel_filter_v_##cpuv( dstv, src, buf+8, stride, width );\
227         x264_hpel_filter_c_##cpuc( dstc, buf+8, width );\
228         x264_hpel_filter_h_##cpuh( dsth, src, width );\
229         dsth += stride;\
230         dstv += stride;\
231         dstc += stride;\
232         src  += stride;\
233     }\
234     x264_sfence();\
235     x264_free(buf);\
236 }
237
238 HPEL(8, mmxext, mmxext, mmxext, mmxext)
239 HPEL(16, sse2_amd, mmxext, mmxext, sse2)
240 HPEL(16, sse2, sse2, sse2, sse2)
241 HPEL(16, ssse3, sse2, ssse3, sse2)
242
243 void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
244 {
245     if( !(cpu&X264_CPU_MMX) )
246         return;
247
248     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_mmx;
249     pf->copy[PIXEL_8x8]   = x264_mc_copy_w8_mmx;
250     pf->copy[PIXEL_4x4]   = x264_mc_copy_w4_mmx;
251     pf->memcpy_aligned = x264_memcpy_aligned_mmx;
252     pf->memzero_aligned = x264_memzero_aligned_mmx;
253
254     if( !(cpu&X264_CPU_MMXEXT) )
255         return;
256
257     pf->mc_luma = mc_luma_mmxext;
258     pf->get_ref = get_ref_mmxext;
259     pf->mc_chroma = x264_mc_chroma_mmxext;
260
261     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_mmxext;
262     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_mmxext;
263     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_mmxext;
264     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_mmxext;
265     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_mmxext;
266     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_mmxext;
267     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_mmxext;
268     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_mmxext;
269
270     pf->avg_weight[PIXEL_16x16] = x264_pixel_avg_weight_16x16_mmxext;
271     pf->avg_weight[PIXEL_16x8]  = x264_pixel_avg_weight_16x8_mmxext;
272     pf->avg_weight[PIXEL_8x16]  = x264_pixel_avg_weight_8x16_mmxext;
273     pf->avg_weight[PIXEL_8x8]   = x264_pixel_avg_weight_8x8_mmxext;
274     pf->avg_weight[PIXEL_8x4]   = x264_pixel_avg_weight_8x4_mmxext;
275     pf->avg_weight[PIXEL_4x4]   = x264_pixel_avg_weight_4x4_mmxext;
276     // avg_weight_4x8 is rare and 4x2 is not used
277
278     pf->plane_copy = x264_plane_copy_mmxext;
279     pf->hpel_filter = x264_hpel_filter_mmxext;
280     pf->frame_init_lowres_core = x264_frame_init_lowres_core_mmxext;
281
282     pf->prefetch_fenc = x264_prefetch_fenc_mmxext;
283     pf->prefetch_ref  = x264_prefetch_ref_mmxext;
284
285 #ifdef ARCH_X86 // all x86_64 cpus with cacheline split issues use sse2 instead
286     if( cpu&X264_CPU_CACHELINE_32 )
287     {
288         pf->mc_luma = mc_luma_cache32_mmxext;
289         pf->get_ref = get_ref_cache32_mmxext;
290         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmxext;
291     }
292     else if( cpu&X264_CPU_CACHELINE_64 )
293     {
294         pf->mc_luma = mc_luma_cache64_mmxext;
295         pf->get_ref = get_ref_cache64_mmxext;
296         pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmxext;
297     }
298 #endif
299
300     if( !(cpu&X264_CPU_SSE2) )
301         return;
302
303     pf->memcpy_aligned = x264_memcpy_aligned_sse2;
304     pf->memzero_aligned = x264_memzero_aligned_sse2;
305     pf->hpel_filter = x264_hpel_filter_sse2_amd;
306
307     if( cpu&X264_CPU_SSE2_IS_SLOW )
308         return;
309
310     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_sse2;
311     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_sse2;
312     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_sse2;
313     if( !(cpu&X264_CPU_STACK_MOD4) )
314     {
315         pf->avg_weight[PIXEL_16x16] = x264_pixel_avg_weight_16x16_sse2;
316         pf->avg_weight[PIXEL_16x8]  = x264_pixel_avg_weight_16x8_sse2;
317         pf->avg_weight[PIXEL_8x16]  = x264_pixel_avg_weight_8x16_sse2;
318         pf->avg_weight[PIXEL_8x8]   = x264_pixel_avg_weight_8x8_sse2;
319         pf->avg_weight[PIXEL_8x4]   = x264_pixel_avg_weight_8x4_sse2;
320     }
321     pf->hpel_filter = x264_hpel_filter_sse2;
322     pf->frame_init_lowres_core = x264_frame_init_lowres_core_sse2;
323     pf->mc_chroma = x264_mc_chroma_sse2;
324
325     if( cpu&X264_CPU_SSE2_IS_FAST )
326     {
327         pf->mc_luma = mc_luma_sse2;
328         pf->get_ref = get_ref_sse2;
329         if( cpu&X264_CPU_CACHELINE_64 )
330         {
331             pf->mc_luma = mc_luma_cache64_sse2;
332             pf->get_ref = get_ref_cache64_sse2;
333         }
334     }
335
336     if( !(cpu&X264_CPU_SSSE3) )
337         return;
338
339     pf->hpel_filter = x264_hpel_filter_ssse3;
340     pf->frame_init_lowres_core = x264_frame_init_lowres_core_ssse3;
341     pf->mc_chroma = x264_mc_chroma_ssse3;
342 }