Phenom CPU optimizations
[x262.git] / common / pixel.c
1 /*****************************************************************************
2  * pixel.c: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "common.h"
25
26 #ifdef HAVE_MMX
27 #   include "x86/pixel.h"
28 #endif
29 #ifdef ARCH_PPC
30 #   include "ppc/pixel.h"
31 #endif
32 #ifdef ARCH_UltraSparc
33 #   include "sparc/pixel.h"
34 #endif
35
36
37 /****************************************************************************
38  * pixel_sad_WxH
39  ****************************************************************************/
40 #define PIXEL_SAD_C( name, lx, ly ) \
41 static int name( uint8_t *pix1, int i_stride_pix1,  \
42                  uint8_t *pix2, int i_stride_pix2 ) \
43 {                                                   \
44     int i_sum = 0;                                  \
45     int x, y;                                       \
46     for( y = 0; y < ly; y++ )                       \
47     {                                               \
48         for( x = 0; x < lx; x++ )                   \
49         {                                           \
50             i_sum += abs( pix1[x] - pix2[x] );      \
51         }                                           \
52         pix1 += i_stride_pix1;                      \
53         pix2 += i_stride_pix2;                      \
54     }                                               \
55     return i_sum;                                   \
56 }
57
58
59 PIXEL_SAD_C( x264_pixel_sad_16x16, 16, 16 )
60 PIXEL_SAD_C( x264_pixel_sad_16x8,  16,  8 )
61 PIXEL_SAD_C( x264_pixel_sad_8x16,   8, 16 )
62 PIXEL_SAD_C( x264_pixel_sad_8x8,    8,  8 )
63 PIXEL_SAD_C( x264_pixel_sad_8x4,    8,  4 )
64 PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
65 PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
66
67
68 /****************************************************************************
69  * pixel_ssd_WxH
70  ****************************************************************************/
71 #define PIXEL_SSD_C( name, lx, ly ) \
72 static int name( uint8_t *pix1, int i_stride_pix1,  \
73                  uint8_t *pix2, int i_stride_pix2 ) \
74 {                                                   \
75     int i_sum = 0;                                  \
76     int x, y;                                       \
77     for( y = 0; y < ly; y++ )                       \
78     {                                               \
79         for( x = 0; x < lx; x++ )                   \
80         {                                           \
81             int d = pix1[x] - pix2[x];              \
82             i_sum += d*d;                           \
83         }                                           \
84         pix1 += i_stride_pix1;                      \
85         pix2 += i_stride_pix2;                      \
86     }                                               \
87     return i_sum;                                   \
88 }
89
90 PIXEL_SSD_C( x264_pixel_ssd_16x16, 16, 16 )
91 PIXEL_SSD_C( x264_pixel_ssd_16x8,  16,  8 )
92 PIXEL_SSD_C( x264_pixel_ssd_8x16,   8, 16 )
93 PIXEL_SSD_C( x264_pixel_ssd_8x8,    8,  8 )
94 PIXEL_SSD_C( x264_pixel_ssd_8x4,    8,  4 )
95 PIXEL_SSD_C( x264_pixel_ssd_4x8,    4,  8 )
96 PIXEL_SSD_C( x264_pixel_ssd_4x4,    4,  4 )
97
98 int64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height )
99 {
100     int64_t i_ssd = 0;
101     int x, y;
102     int align = !(((long)pix1 | (long)pix2 | i_pix1 | i_pix2) & 15);
103
104 #define SSD(size) i_ssd += pf->ssd[size]( pix1 + y*i_pix1 + x, i_pix1, \
105                                           pix2 + y*i_pix2 + x, i_pix2 );
106     for( y = 0; y < i_height-15; y += 16 )
107     {
108         x = 0;
109         if( align )
110             for( ; x < i_width-15; x += 16 )
111                 SSD(PIXEL_16x16);
112         for( ; x < i_width-7; x += 8 )
113             SSD(PIXEL_8x16);
114     }
115     if( y < i_height-7 )
116         for( x = 0; x < i_width-7; x += 8 )
117             SSD(PIXEL_8x8);
118 #undef SSD
119
120 #define SSD1 { int d = pix1[y*i_pix1+x] - pix2[y*i_pix2+x]; i_ssd += d*d; }
121     if( i_width % 8 != 0 )
122     {
123         for( y = 0; y < (i_height & ~7); y++ )
124             for( x = i_width & ~7; x < i_width; x++ )
125                 SSD1;
126     }
127     if( i_height % 8 != 0 )
128     {
129         for( y = i_height & ~7; y < i_height; y++ )
130             for( x = 0; x < i_width; x++ )
131                 SSD1;
132     }
133 #undef SSD1
134
135     return i_ssd;
136 }
137
138
139 /****************************************************************************
140  * pixel_var_wxh
141  ****************************************************************************/
142 #define PIXEL_VAR_C( name, w, shift ) \
143 static int name( uint8_t *pix, int i_stride, uint32_t *sad ) \
144 {                                             \
145     uint32_t var = 0, sum = 0, sqr = 0;       \
146     int x, y;                                 \
147     for( y = 0; y < w; y++ )                  \
148     {                                         \
149         for( x = 0; x < w; x++ )              \
150         {                                     \
151             sum += pix[x];                    \
152             sqr += pix[x] * pix[x];           \
153         }                                     \
154         pix += i_stride;                      \
155     }                                         \
156     var = sqr - (sum * sum >> shift);         \
157     *sad = sum;                               \
158     return var;                               \
159 }
160
161 PIXEL_VAR_C( x264_pixel_var_16x16, 16, 8 )
162 PIXEL_VAR_C( x264_pixel_var_8x8,    8, 6 )
163
164
165 #define HADAMARD4(d0,d1,d2,d3,s0,s1,s2,s3) {\
166     int t0 = s0 + s1;\
167     int t1 = s0 - s1;\
168     int t2 = s2 + s3;\
169     int t3 = s2 - s3;\
170     d0 = t0 + t2;\
171     d2 = t0 - t2;\
172     d1 = t1 + t3;\
173     d3 = t1 - t3;\
174 }
175
176 /****************************************************************************
177  * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
178  ****************************************************************************/
179 static int pixel_satd_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height )
180 {
181     int16_t tmp[4][4];
182     int x, y;
183     int i_satd = 0;
184
185     for( y = 0; y < i_height; y += 4 )
186     {
187         for( x = 0; x < i_width; x += 4 )
188         {
189             int i;
190             uint8_t *p1 = pix1+x, *p2 = pix2+x;
191
192             for( i=0; i<4; i++, p1+=i_pix1, p2+=i_pix2 )
193             {
194                 int a0 = p1[0] - p2[0];
195                 int a1 = p1[1] - p2[1];
196                 int a2 = p1[2] - p2[2];
197                 int a3 = p1[3] - p2[3];
198                 HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
199             }
200             for( i=0; i<4; i++ )
201             {
202                 int a0,a1,a2,a3;
203                 HADAMARD4( a0,a1,a2,a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
204                 i_satd += abs(a0) + abs(a1) + abs(a2) + abs(a3);
205             }
206
207         }
208         pix1 += 4 * i_pix1;
209         pix2 += 4 * i_pix2;
210     }
211
212     return i_satd / 2;
213 }
214 #define PIXEL_SATD_C( name, width, height ) \
215 static int name( uint8_t *pix1, int i_stride_pix1, \
216                  uint8_t *pix2, int i_stride_pix2 ) \
217 { \
218     return pixel_satd_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ); \
219 }
220 PIXEL_SATD_C( x264_pixel_satd_16x16, 16, 16 )
221 PIXEL_SATD_C( x264_pixel_satd_16x8,  16, 8 )
222 PIXEL_SATD_C( x264_pixel_satd_8x16,  8, 16 )
223 PIXEL_SATD_C( x264_pixel_satd_8x8,   8, 8 )
224 PIXEL_SATD_C( x264_pixel_satd_8x4,   8, 4 )
225 PIXEL_SATD_C( x264_pixel_satd_4x8,   4, 8 )
226 PIXEL_SATD_C( x264_pixel_satd_4x4,   4, 4 )
227
228
229 /****************************************************************************
230  * pixel_sa8d_WxH: sum of 8x8 Hadamard transformed differences
231  ****************************************************************************/
232 #define SA8D_1D {\
233     int b0,b1,b2,b3,b4,b5,b6,b7;\
234     HADAMARD4( b0,b1,b2,b3, SRC(0), SRC(1), SRC(2), SRC(3) );\
235     HADAMARD4( b4,b5,b6,b7, SRC(4), SRC(5), SRC(6), SRC(7) );\
236     DST(0, b0 + b4);\
237     DST(4, b0 - b4);\
238     DST(1, b1 + b5);\
239     DST(5, b1 - b5);\
240     DST(2, b2 + b6);\
241     DST(6, b2 - b6);\
242     DST(3, b3 + b7);\
243     DST(7, b3 - b7);\
244 }
245
246 static inline int pixel_sa8d_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2,
247                                   int i_width, int i_height )
248 {
249     int16_t diff[8][8];
250     int i_satd = 0;
251     int x, y;
252
253     for( y = 0; y < i_height; y += 8 )
254     {
255         for( x = 0; x < i_width; x += 8 )
256         {
257             int i;
258             uint8_t *p1 = pix1+x, *p2 = pix2+x;
259
260 #define SRC(x)     a##x
261 #define DST(x,rhs) diff[i][x] = (rhs)
262             for( i=0; i<8; i++, p1+=i_pix1, p2+=i_pix2 )
263             {
264                 int a0 = p1[0] - p2[0];
265                 int a1 = p1[1] - p2[1];
266                 int a2 = p1[2] - p2[2];
267                 int a3 = p1[3] - p2[3];
268                 int a4 = p1[4] - p2[4];
269                 int a5 = p1[5] - p2[5];
270                 int a6 = p1[6] - p2[6];
271                 int a7 = p1[7] - p2[7];
272                 SA8D_1D
273             }
274 #undef SRC
275 #undef DST
276
277 #define SRC(x)     diff[x][i]
278 #define DST(x,rhs) i_satd += abs(rhs)
279             for( i=0; i<8; i++ )
280                 SA8D_1D
281 #undef SRC
282 #undef DST
283         }
284         pix1 += 8 * i_pix1;
285         pix2 += 8 * i_pix2;
286     }
287
288     return i_satd;
289 }
290
291 #define PIXEL_SA8D_C( width, height ) \
292 static int x264_pixel_sa8d_##width##x##height( uint8_t *pix1, int i_stride_pix1, \
293                                                uint8_t *pix2, int i_stride_pix2 ) \
294 { \
295     return ( pixel_sa8d_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ) + 2 ) >> 2; \
296 }
297 PIXEL_SA8D_C( 16, 16 )
298 PIXEL_SA8D_C( 16, 8 )
299 PIXEL_SA8D_C( 8, 16 )
300 PIXEL_SA8D_C( 8, 8 )
301
302
303 static uint64_t pixel_hadamard_ac( uint8_t *pix, int stride )
304 {
305     int16_t tmp[8][8];
306     int sum4=0, sum8=0;
307     int i;
308     for( i=0; i<8; i++, pix+=stride )
309     {
310         HADAMARD4( tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i],
311                    pix[0], pix[1], pix[2], pix[3] );
312         HADAMARD4( tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i],
313                    pix[4], pix[5], pix[6], pix[7] );
314     }
315     for( i=0; i<8; i++ )
316     {
317         int a0,a1,a2,a3,a4,a5,a6,a7;
318         HADAMARD4( a0,a1,a2,a3, tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3] );
319         sum4 += abs(a0) + abs(a1) + abs(a2) + abs(a3);
320         HADAMARD4( a4,a5,a6,a7, tmp[i][4], tmp[i][5], tmp[i][6], tmp[i][7] );
321         sum4 += abs(a4) + abs(a5) + abs(a6) + abs(a7);
322         tmp[i][0] = a0 + a4;
323         tmp[i][4] = a0 - a4;
324         tmp[i][1] = a1 + a5;
325         tmp[i][5] = a1 - a5;
326         tmp[i][2] = a2 + a6;
327         tmp[i][6] = a2 - a6;
328         tmp[i][3] = a3 + a7;
329         tmp[i][7] = a3 - a7;
330     }
331     for( i=0; i<8; i++ )
332     {
333         sum8 += abs( tmp[0][i] + tmp[4][i] )
334               + abs( tmp[0][i] - tmp[4][i] )
335               + abs( tmp[1][i] + tmp[5][i] )
336               + abs( tmp[1][i] - tmp[5][i] )
337               + abs( tmp[2][i] + tmp[6][i] )
338               + abs( tmp[2][i] - tmp[6][i] )
339               + abs( tmp[3][i] + tmp[7][i] )
340               + abs( tmp[3][i] - tmp[7][i] );
341     }
342     sum4 -= tmp[0][0]+tmp[4][0];
343     sum8 -= tmp[0][0]+tmp[4][0];
344     return ((uint64_t)sum8<<32) + sum4;
345 }
346
347 #define HADAMARD_AC(w,h) \
348 static uint64_t x264_pixel_hadamard_ac_##w##x##h( uint8_t *pix, int stride )\
349 {\
350     uint64_t sum = pixel_hadamard_ac( pix, stride );\
351     if( w==16 )\
352         sum += pixel_hadamard_ac( pix+8, stride );\
353     if( h==16 )\
354         sum += pixel_hadamard_ac( pix+8*stride, stride );\
355     if( w==16 && h==16 )\
356         sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
357     return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
358 }
359 HADAMARD_AC( 16, 16 )
360 HADAMARD_AC( 16, 8 )
361 HADAMARD_AC( 8, 16 )
362 HADAMARD_AC( 8, 8 )
363
364
365 /****************************************************************************
366  * pixel_sad_x4
367  ****************************************************************************/
368 #define SAD_X( size ) \
369 static void x264_pixel_sad_x3_##size( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, int i_stride, int scores[3] )\
370 {\
371     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
372     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
373     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
374 }\
375 static void x264_pixel_sad_x4_##size( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, uint8_t *pix3, int i_stride, int scores[4] )\
376 {\
377     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
378     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
379     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
380     scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
381 }
382
383 SAD_X( 16x16 )
384 SAD_X( 16x8 )
385 SAD_X( 8x16 )
386 SAD_X( 8x8 )
387 SAD_X( 8x4 )
388 SAD_X( 4x8 )
389 SAD_X( 4x4 )
390
391 #ifdef ARCH_UltraSparc
392 SAD_X( 16x16_vis )
393 SAD_X( 16x8_vis )
394 SAD_X( 8x16_vis )
395 SAD_X( 8x8_vis )
396 #endif
397
398 /****************************************************************************
399  * pixel_satd_x4
400  * no faster than single satd, but needed for satd to be a drop-in replacement for sad
401  ****************************************************************************/
402
403 #define SATD_X( size, cpu ) \
404 static void x264_pixel_satd_x3_##size##cpu( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, int i_stride, int scores[3] )\
405 {\
406     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
407     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
408     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
409 }\
410 static void x264_pixel_satd_x4_##size##cpu( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, uint8_t *pix3, int i_stride, int scores[4] )\
411 {\
412     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
413     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
414     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
415     scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
416 }
417 #define SATD_X_DECL5( cpu )\
418 SATD_X( 16x16, cpu )\
419 SATD_X( 16x8, cpu )\
420 SATD_X( 8x16, cpu )\
421 SATD_X( 8x8, cpu )\
422 SATD_X( 8x4, cpu )
423 #define SATD_X_DECL7( cpu )\
424 SATD_X_DECL5( cpu )\
425 SATD_X( 4x8, cpu )\
426 SATD_X( 4x4, cpu )
427
428 SATD_X_DECL7()
429 #ifdef HAVE_MMX
430 SATD_X_DECL7( _mmxext )
431 SATD_X_DECL5( _sse2 )
432 SATD_X_DECL7( _ssse3 )
433 SATD_X_DECL5( _ssse3_phadd )
434 #endif
435
436 /****************************************************************************
437  * structural similarity metric
438  ****************************************************************************/
439 static void ssim_4x4x2_core( const uint8_t *pix1, int stride1,
440                              const uint8_t *pix2, int stride2,
441                              int sums[2][4])
442 {
443     int x, y, z;
444     for(z=0; z<2; z++)
445     {
446         uint32_t s1=0, s2=0, ss=0, s12=0;
447         for(y=0; y<4; y++)
448             for(x=0; x<4; x++)
449             {
450                 int a = pix1[x+y*stride1];
451                 int b = pix2[x+y*stride2];
452                 s1  += a;
453                 s2  += b;
454                 ss  += a*a;
455                 ss  += b*b;
456                 s12 += a*b;
457             }
458         sums[z][0] = s1;
459         sums[z][1] = s2;
460         sums[z][2] = ss;
461         sums[z][3] = s12;
462         pix1 += 4;
463         pix2 += 4;
464     }
465 }
466
467 static float ssim_end1( int s1, int s2, int ss, int s12 )
468 {
469     static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
470     static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
471     int vars = ss*64 - s1*s1 - s2*s2;
472     int covar = s12*64 - s1*s2;
473     return (float)(2*s1*s2 + ssim_c1) * (float)(2*covar + ssim_c2)\
474            / ((float)(s1*s1 + s2*s2 + ssim_c1) * (float)(vars + ssim_c2));
475 }
476
477 static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
478 {
479     int i;
480     float ssim = 0.0;
481     for( i = 0; i < width; i++ )
482         ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
483                            sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
484                            sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
485                            sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
486     return ssim;
487 }
488
489 float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
490                            uint8_t *pix1, int stride1,
491                            uint8_t *pix2, int stride2,
492                            int width, int height )
493 {
494     int x, y, z;
495     float ssim = 0.0;
496     int (*sum0)[4] = x264_malloc(4 * (width/4+3) * sizeof(int));
497     int (*sum1)[4] = x264_malloc(4 * (width/4+3) * sizeof(int));
498     width >>= 2;
499     height >>= 2;
500     z = 0;
501     for( y = 1; y < height; y++ )
502     {
503         for( ; z <= y; z++ )
504         {
505             XCHG( void*, sum0, sum1 );
506             for( x = 0; x < width; x+=2 )
507                 pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
508         }
509         for( x = 0; x < width-1; x += 4 )
510             ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
511     }
512     x264_free(sum0);
513     x264_free(sum1);
514     return ssim;
515 }
516
517
518 /****************************************************************************
519  * successive elimination
520  ****************************************************************************/
521 static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
522                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
523 {
524     int nmv=0, i;
525     for( i=0; i<width; i++, sums++ )
526     {
527         int ads = abs( enc_dc[0] - sums[0] )
528                 + abs( enc_dc[1] - sums[8] )
529                 + abs( enc_dc[2] - sums[delta] )
530                 + abs( enc_dc[3] - sums[delta+8] )
531                 + cost_mvx[i];
532         if( ads < thresh )
533             mvs[nmv++] = i;
534     }
535     return nmv;
536 }
537
538 static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
539                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
540 {
541     int nmv=0, i;
542     for( i=0; i<width; i++, sums++ )
543     {
544         int ads = abs( enc_dc[0] - sums[0] )
545                 + abs( enc_dc[1] - sums[delta] )
546                 + cost_mvx[i];
547         if( ads < thresh )
548             mvs[nmv++] = i;
549     }
550     return nmv;
551 }
552
553 static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
554                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
555 {
556     int nmv=0, i;
557     for( i=0; i<width; i++, sums++ )
558     {
559         int ads = abs( enc_dc[0] - sums[0] )
560                 + cost_mvx[i];
561         if( ads < thresh )
562             mvs[nmv++] = i;
563     }
564     return nmv;
565 }
566
567
568 /****************************************************************************
569  * x264_pixel_init:
570  ****************************************************************************/
571 void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
572 {
573     memset( pixf, 0, sizeof(*pixf) );
574
575 #define INIT2_NAME( name1, name2, cpu ) \
576     pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
577     pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
578 #define INIT4_NAME( name1, name2, cpu ) \
579     INIT2_NAME( name1, name2, cpu ) \
580     pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
581     pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
582 #define INIT5_NAME( name1, name2, cpu ) \
583     INIT4_NAME( name1, name2, cpu ) \
584     pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
585 #define INIT7_NAME( name1, name2, cpu ) \
586     INIT5_NAME( name1, name2, cpu ) \
587     pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;\
588     pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
589 #define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
590 #define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
591 #define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
592 #define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
593
594 #define INIT_ADS( cpu ) \
595     pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
596     pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
597     pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;
598
599     INIT7( sad, );
600     INIT7_NAME( sad_aligned, sad, );
601     INIT7( sad_x3, );
602     INIT7( sad_x4, );
603     INIT7( ssd, );
604     INIT7( satd, );
605     INIT7( satd_x3, );
606     INIT7( satd_x4, );
607     INIT4( sa8d, );
608     INIT4( hadamard_ac, );
609     INIT_ADS( );
610
611     pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
612     pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
613
614     pixf->ssim_4x4x2_core = ssim_4x4x2_core;
615     pixf->ssim_end4 = ssim_end4;
616
617 #ifdef HAVE_MMX
618     if( cpu&X264_CPU_MMX )
619     {
620         INIT7( ssd, _mmx );
621     }
622
623     if( cpu&X264_CPU_MMXEXT )
624     {
625         INIT7( sad, _mmxext );
626         INIT7_NAME( sad_aligned, sad, _mmxext );
627         INIT7( sad_x3, _mmxext );
628         INIT7( sad_x4, _mmxext );
629         INIT7( satd, _mmxext );
630         INIT7( satd_x3, _mmxext );
631         INIT7( satd_x4, _mmxext );
632         INIT4( hadamard_ac, _mmxext );
633         INIT_ADS( _mmxext );
634         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmxext;
635         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmxext;
636 #ifdef ARCH_X86
637         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmxext;
638         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmxext;
639         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmxext;
640         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_mmxext;
641
642         if( cpu&X264_CPU_CACHELINE_32 )
643         {
644             INIT5( sad, _cache32_mmxext );
645             INIT4( sad_x3, _cache32_mmxext );
646             INIT4( sad_x4, _cache32_mmxext );
647         }
648         else if( cpu&X264_CPU_CACHELINE_64 )
649         {
650             INIT5( sad, _cache64_mmxext );
651             INIT4( sad_x3, _cache64_mmxext );
652             INIT4( sad_x4, _cache64_mmxext );
653         }
654 #else
655         if( cpu&X264_CPU_CACHELINE_64 )
656         {
657             pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmxext;
658             pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmxext;
659             pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmxext;
660             pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmxext;
661             pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmxext;
662             pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmxext;
663             pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmxext;
664         }
665 #endif
666         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmxext;
667         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_mmxext;
668         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmxext;
669         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmxext;
670     }
671
672     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
673     {
674         INIT2( sad, _sse2 );
675         INIT2( sad_x3, _sse2 );
676         INIT2( sad_x4, _sse2 );
677         if( !(cpu&X264_CPU_STACK_MOD4) )
678         {
679             INIT4( hadamard_ac, _sse2 );
680         }
681         INIT_ADS( _sse2 );
682         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
683         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
684 #ifdef ARCH_X86
685         if( cpu&X264_CPU_CACHELINE_64 )
686         {
687             INIT2( sad, _cache64_sse2 );
688             INIT2( sad_x3, _cache64_sse2 );
689             INIT2( sad_x4, _cache64_sse2 );
690         }
691 #endif
692         if( cpu&X264_CPU_SSE_MISALIGN )
693         {
694             INIT2( sad_x3, _sse2_misalign );
695             INIT2( sad_x4, _sse2_misalign );
696         }
697     }
698     if( cpu&X264_CPU_SSE2 )
699     {
700         INIT5( ssd, _sse2 );
701         INIT5( satd, _sse2 );
702         INIT5( satd_x3, _sse2 );
703         INIT5( satd_x4, _sse2 );
704         INIT2_NAME( sad_aligned, sad, _sse2_aligned );
705         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
706         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
707         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
708         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
709         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
710 #ifdef ARCH_X86_64
711         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
712 #endif
713     }
714
715     if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
716     {
717         pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
718         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
719         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
720         pixf->sad_x3[PIXEL_8x8] = x264_pixel_sad_x3_8x8_sse2;
721         pixf->sad_x3[PIXEL_8x4] = x264_pixel_sad_x3_8x4_sse2;
722         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
723         pixf->sad_x4[PIXEL_8x8] = x264_pixel_sad_x4_8x8_sse2;
724         pixf->sad_x4[PIXEL_8x4] = x264_pixel_sad_x4_8x4_sse2;
725     }
726
727     if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
728     {
729         INIT2( sad, _sse3 );
730         INIT2( sad_x3, _sse3 );
731         INIT2( sad_x4, _sse3 );
732     }
733
734     if( cpu&X264_CPU_SSSE3 )
735     {
736         INIT7( satd, _ssse3 );
737         INIT7( satd_x3, _ssse3 );
738         INIT7( satd_x4, _ssse3 );
739         if( !(cpu&X264_CPU_STACK_MOD4) )
740         {
741             INIT4( hadamard_ac, _ssse3 );
742         }
743         INIT_ADS( _ssse3 );
744         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
745         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
746         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
747         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
748         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
749         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_ssse3;
750 #ifdef ARCH_X86_64
751         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_ssse3;
752 #endif
753         if( cpu&X264_CPU_CACHELINE_64 )
754         {
755             INIT2( sad, _cache64_ssse3 );
756             INIT2( sad_x3, _cache64_ssse3 );
757             INIT2( sad_x4, _cache64_ssse3 );
758         }
759         if( cpu&X264_CPU_PHADD_IS_FAST )
760         {
761             INIT5( satd, _ssse3_phadd );
762             INIT5( satd_x3, _ssse3_phadd );
763             INIT5( satd_x4, _ssse3_phadd );
764         }
765     }
766 #endif //HAVE_MMX
767
768 #ifdef ARCH_PPC
769     if( cpu&X264_CPU_ALTIVEC )
770     {
771         x264_pixel_altivec_init( pixf );
772     }
773 #endif
774 #ifdef ARCH_UltraSparc
775     INIT4( sad, _vis );
776     INIT4( sad_x3, _vis );
777     INIT4( sad_x4, _vis );
778 #endif
779
780     pixf->ads[PIXEL_8x16] =
781     pixf->ads[PIXEL_8x4] =
782     pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
783     pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
784 }
785