Vastly faster SATD/SA8D/Hadamard_AC/SSD/DCT/IDCT
[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 = !(((intptr_t)pix1 | (intptr_t)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 ) \
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     return var;                               \
158 }
159
160 PIXEL_VAR_C( x264_pixel_var_16x16, 16, 8 )
161 PIXEL_VAR_C( x264_pixel_var_8x8,    8, 6 )
162
163
164 #define HADAMARD4(d0,d1,d2,d3,s0,s1,s2,s3) {\
165     int t0 = s0 + s1;\
166     int t1 = s0 - s1;\
167     int t2 = s2 + s3;\
168     int t3 = s2 - s3;\
169     d0 = t0 + t2;\
170     d2 = t0 - t2;\
171     d1 = t1 + t3;\
172     d3 = t1 - t3;\
173 }
174
175 // in: a pseudo-simd number of the form x+(y<<16)
176 // return: abs(x)+(abs(y)<<16)
177 static ALWAYS_INLINE uint32_t abs2( uint32_t a )
178 {
179     uint32_t s = ((a>>15)&0x10001)*0xffff;
180     return (a+s)^s;
181 }
182
183 /****************************************************************************
184  * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
185  ****************************************************************************/
186
187 static NOINLINE int x264_pixel_satd_4x4( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
188 {
189     uint32_t tmp[4][2];
190     uint32_t a0,a1,a2,a3,b0,b1;
191     int sum=0, i;
192     for( i=0; i<4; i++, pix1+=i_pix1, pix2+=i_pix2 )
193     {
194         a0 = pix1[0] - pix2[0];
195         a1 = pix1[1] - pix2[1];
196         b0 = (a0+a1) + ((a0-a1)<<16);
197         a2 = pix1[2] - pix2[2];
198         a3 = pix1[3] - pix2[3];
199         b1 = (a2+a3) + ((a2-a3)<<16);
200         tmp[i][0] = b0 + b1;
201         tmp[i][1] = b0 - b1;
202     }
203     for( i=0; i<2; i++ )
204     {
205         HADAMARD4( a0,a1,a2,a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
206         a0 = abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
207         sum += ((uint16_t)a0) + (a0>>16);
208     }
209     return sum >> 1;
210 }
211
212 static NOINLINE int x264_pixel_satd_8x4( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
213 {
214     uint32_t tmp[4][4];
215     uint32_t a0,a1,a2,a3;
216     int sum=0, i;
217     for( i=0; i<4; i++, pix1+=i_pix1, pix2+=i_pix2 )
218     {
219         a0 = (pix1[0] - pix2[0]) + ((pix1[4] - pix2[4]) << 16);
220         a1 = (pix1[1] - pix2[1]) + ((pix1[5] - pix2[5]) << 16);
221         a2 = (pix1[2] - pix2[2]) + ((pix1[6] - pix2[6]) << 16);
222         a3 = (pix1[3] - pix2[3]) + ((pix1[7] - pix2[7]) << 16);
223         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
224     }
225     for( i=0; i<4; i++ )
226     {
227         HADAMARD4( a0,a1,a2,a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
228         sum += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
229     }
230     return (((uint16_t)sum) + ((uint32_t)sum>>16)) >> 1;
231 }
232
233 #define PIXEL_SATD_C( w, h, sub )\
234 static int x264_pixel_satd_##w##x##h( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )\
235 {\
236     int sum = sub( pix1, i_pix1, pix2, i_pix2 )\
237             + sub( pix1+4*i_pix1, i_pix1, pix2+4*i_pix2, i_pix2 );\
238     if( w==16 )\
239         sum+= sub( pix1+8, i_pix1, pix2+8, i_pix2 )\
240             + sub( pix1+8+4*i_pix1, i_pix1, pix2+8+4*i_pix2, i_pix2 );\
241     if( h==16 )\
242         sum+= sub( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )\
243             + sub( pix1+12*i_pix1, i_pix1, pix2+12*i_pix2, i_pix2 );\
244     if( w==16 && h==16 )\
245         sum+= sub( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 )\
246             + sub( pix1+8+12*i_pix1, i_pix1, pix2+8+12*i_pix2, i_pix2 );\
247     return sum;\
248 }
249 PIXEL_SATD_C( 16, 16, x264_pixel_satd_8x4 )
250 PIXEL_SATD_C( 16, 8,  x264_pixel_satd_8x4 )
251 PIXEL_SATD_C( 8,  16, x264_pixel_satd_8x4 )
252 PIXEL_SATD_C( 8,  8,  x264_pixel_satd_8x4 )
253 PIXEL_SATD_C( 4,  8,  x264_pixel_satd_4x4 )
254
255
256 static NOINLINE int sa8d_8x8( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
257 {
258     uint32_t tmp[8][4];
259     uint32_t a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3;
260     int sum=0, i;
261     for( i=0; i<8; i++, pix1+=i_pix1, pix2+=i_pix2 )
262     {
263         a0 = pix1[0] - pix2[0];
264         a1 = pix1[1] - pix2[1];
265         b0 = (a0+a1) + ((a0-a1)<<16);
266         a2 = pix1[2] - pix2[2];
267         a3 = pix1[3] - pix2[3];
268         b1 = (a2+a3) + ((a2-a3)<<16);
269         a4 = pix1[4] - pix2[4];
270         a5 = pix1[5] - pix2[5];
271         b2 = (a4+a5) + ((a4-a5)<<16);
272         a6 = pix1[6] - pix2[6];
273         a7 = pix1[7] - pix2[7];
274         b3 = (a6+a7) + ((a6-a7)<<16);
275         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], b0,b1,b2,b3 );
276     }
277     for( i=0; i<4; i++ )
278     {
279         HADAMARD4( a0,a1,a2,a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
280         HADAMARD4( a4,a5,a6,a7, tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i] );
281         b0  = abs2(a0+a4) + abs2(a0-a4);
282         b0 += abs2(a1+a5) + abs2(a1-a5);
283         b0 += abs2(a2+a6) + abs2(a2-a6);
284         b0 += abs2(a3+a7) + abs2(a3-a7);
285         sum += (uint16_t)b0 + (b0>>16);
286     }
287     return sum;
288 }
289
290 static int x264_pixel_sa8d_8x8( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
291 {
292     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 );
293     return (sum+2)>>2;
294 }
295
296 static int x264_pixel_sa8d_16x16( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
297 {
298     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 )
299             + sa8d_8x8( pix1+8, i_pix1, pix2+8, i_pix2 )
300             + sa8d_8x8( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )
301             + sa8d_8x8( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 );
302     return (sum+2)>>2;
303 }
304
305
306 static NOINLINE uint64_t pixel_hadamard_ac( uint8_t *pix, int stride )
307 {
308     uint32_t tmp[32];
309     uint32_t a0,a1,a2,a3,dc;
310     int sum4=0, sum8=0, i;
311     for( i=0; i<8; i++, pix+=stride )
312     {
313         uint32_t *t = tmp + (i&3) + (i&4)*4;
314         a0 = (pix[0]+pix[1]) + ((pix[0]-pix[1])<<16);
315         a1 = (pix[2]+pix[3]) + ((pix[2]-pix[3])<<16);
316         t[0] = a0 + a1;
317         t[4] = a0 - a1;
318         a2 = (pix[4]+pix[5]) + ((pix[4]-pix[5])<<16);
319         a3 = (pix[6]+pix[7]) + ((pix[6]-pix[7])<<16);
320         t[8] = a2 + a3;
321         t[12] = a2 - a3;
322     }
323     for( i=0; i<8; i++ )
324     {
325         HADAMARD4( a0,a1,a2,a3, tmp[i*4+0], tmp[i*4+1], tmp[i*4+2], tmp[i*4+3] );
326         tmp[i*4+0] = a0;
327         tmp[i*4+1] = a1;
328         tmp[i*4+2] = a2;
329         tmp[i*4+3] = a3;
330         sum4 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
331     }
332     for( i=0; i<8; i++ )
333     {
334         HADAMARD4( a0,a1,a2,a3, tmp[i], tmp[8+i], tmp[16+i], tmp[24+i] );
335         sum8 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
336     }
337     dc = (uint16_t)(tmp[0] + tmp[8] + tmp[16] + tmp[24]);
338     sum4 = (uint16_t)sum4 + ((uint32_t)sum4>>16) - dc;
339     sum8 = (uint16_t)sum8 + ((uint32_t)sum8>>16) - dc;
340     return ((uint64_t)sum8<<32) + sum4;
341 }
342
343 #define HADAMARD_AC(w,h) \
344 static uint64_t x264_pixel_hadamard_ac_##w##x##h( uint8_t *pix, int stride )\
345 {\
346     uint64_t sum = pixel_hadamard_ac( pix, stride );\
347     if( w==16 )\
348         sum += pixel_hadamard_ac( pix+8, stride );\
349     if( h==16 )\
350         sum += pixel_hadamard_ac( pix+8*stride, stride );\
351     if( w==16 && h==16 )\
352         sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
353     return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
354 }
355 HADAMARD_AC( 16, 16 )
356 HADAMARD_AC( 16, 8 )
357 HADAMARD_AC( 8, 16 )
358 HADAMARD_AC( 8, 8 )
359
360
361 /****************************************************************************
362  * pixel_sad_x4
363  ****************************************************************************/
364 #define SAD_X( size ) \
365 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] )\
366 {\
367     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
368     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
369     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
370 }\
371 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] )\
372 {\
373     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
374     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
375     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
376     scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
377 }
378
379 SAD_X( 16x16 )
380 SAD_X( 16x8 )
381 SAD_X( 8x16 )
382 SAD_X( 8x8 )
383 SAD_X( 8x4 )
384 SAD_X( 4x8 )
385 SAD_X( 4x4 )
386
387 #ifdef ARCH_UltraSparc
388 SAD_X( 16x16_vis )
389 SAD_X( 16x8_vis )
390 SAD_X( 8x16_vis )
391 SAD_X( 8x8_vis )
392 #endif
393
394 /****************************************************************************
395  * pixel_satd_x4
396  * no faster than single satd, but needed for satd to be a drop-in replacement for sad
397  ****************************************************************************/
398
399 #define SATD_X( size, cpu ) \
400 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] )\
401 {\
402     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
403     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
404     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
405 }\
406 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] )\
407 {\
408     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
409     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
410     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
411     scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
412 }
413 #define SATD_X_DECL6( cpu )\
414 SATD_X( 16x16, cpu )\
415 SATD_X( 16x8, cpu )\
416 SATD_X( 8x16, cpu )\
417 SATD_X( 8x8, cpu )\
418 SATD_X( 8x4, cpu )\
419 SATD_X( 4x8, cpu )
420 #define SATD_X_DECL7( cpu )\
421 SATD_X_DECL6( cpu )\
422 SATD_X( 4x4, cpu )
423
424 SATD_X_DECL7()
425 #ifdef HAVE_MMX
426 SATD_X_DECL7( _mmxext )
427 SATD_X_DECL6( _sse2 )
428 SATD_X_DECL7( _ssse3 )
429 SATD_X_DECL7( _sse4 )
430 #endif
431
432 /****************************************************************************
433  * structural similarity metric
434  ****************************************************************************/
435 static void ssim_4x4x2_core( const uint8_t *pix1, int stride1,
436                              const uint8_t *pix2, int stride2,
437                              int sums[2][4])
438 {
439     int x, y, z;
440     for(z=0; z<2; z++)
441     {
442         uint32_t s1=0, s2=0, ss=0, s12=0;
443         for(y=0; y<4; y++)
444             for(x=0; x<4; x++)
445             {
446                 int a = pix1[x+y*stride1];
447                 int b = pix2[x+y*stride2];
448                 s1  += a;
449                 s2  += b;
450                 ss  += a*a;
451                 ss  += b*b;
452                 s12 += a*b;
453             }
454         sums[z][0] = s1;
455         sums[z][1] = s2;
456         sums[z][2] = ss;
457         sums[z][3] = s12;
458         pix1 += 4;
459         pix2 += 4;
460     }
461 }
462
463 static float ssim_end1( int s1, int s2, int ss, int s12 )
464 {
465     static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
466     static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
467     int vars = ss*64 - s1*s1 - s2*s2;
468     int covar = s12*64 - s1*s2;
469     return (float)(2*s1*s2 + ssim_c1) * (float)(2*covar + ssim_c2)\
470            / ((float)(s1*s1 + s2*s2 + ssim_c1) * (float)(vars + ssim_c2));
471 }
472
473 static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
474 {
475     int i;
476     float ssim = 0.0;
477     for( i = 0; i < width; i++ )
478         ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
479                            sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
480                            sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
481                            sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
482     return ssim;
483 }
484
485 float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
486                            uint8_t *pix1, int stride1,
487                            uint8_t *pix2, int stride2,
488                            int width, int height, void *buf )
489 {
490     int x, y, z;
491     float ssim = 0.0;
492     int (*sum0)[4] = buf;
493     int (*sum1)[4] = sum0 + width/4+3;
494     width >>= 2;
495     height >>= 2;
496     z = 0;
497     for( y = 1; y < height; y++ )
498     {
499         for( ; z <= y; z++ )
500         {
501             XCHG( void*, sum0, sum1 );
502             for( x = 0; x < width; x+=2 )
503                 pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
504         }
505         for( x = 0; x < width-1; x += 4 )
506             ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
507     }
508     return ssim;
509 }
510
511
512 /****************************************************************************
513  * successive elimination
514  ****************************************************************************/
515 static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
516                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
517 {
518     int nmv=0, i;
519     for( i=0; i<width; i++, sums++ )
520     {
521         int ads = abs( enc_dc[0] - sums[0] )
522                 + abs( enc_dc[1] - sums[8] )
523                 + abs( enc_dc[2] - sums[delta] )
524                 + abs( enc_dc[3] - sums[delta+8] )
525                 + cost_mvx[i];
526         if( ads < thresh )
527             mvs[nmv++] = i;
528     }
529     return nmv;
530 }
531
532 static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
533                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
534 {
535     int nmv=0, i;
536     for( i=0; i<width; i++, sums++ )
537     {
538         int ads = abs( enc_dc[0] - sums[0] )
539                 + abs( enc_dc[1] - sums[delta] )
540                 + cost_mvx[i];
541         if( ads < thresh )
542             mvs[nmv++] = i;
543     }
544     return nmv;
545 }
546
547 static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
548                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
549 {
550     int nmv=0, i;
551     for( i=0; i<width; i++, sums++ )
552     {
553         int ads = abs( enc_dc[0] - sums[0] )
554                 + cost_mvx[i];
555         if( ads < thresh )
556             mvs[nmv++] = i;
557     }
558     return nmv;
559 }
560
561
562 /****************************************************************************
563  * x264_pixel_init:
564  ****************************************************************************/
565 void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
566 {
567     memset( pixf, 0, sizeof(*pixf) );
568
569 #define INIT2_NAME( name1, name2, cpu ) \
570     pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
571     pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
572 #define INIT4_NAME( name1, name2, cpu ) \
573     INIT2_NAME( name1, name2, cpu ) \
574     pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
575     pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
576 #define INIT5_NAME( name1, name2, cpu ) \
577     INIT4_NAME( name1, name2, cpu ) \
578     pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
579 #define INIT6_NAME( name1, name2, cpu ) \
580     INIT5_NAME( name1, name2, cpu ) \
581     pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
582 #define INIT7_NAME( name1, name2, cpu ) \
583     INIT6_NAME( name1, name2, cpu ) \
584     pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
585 #define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
586 #define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
587 #define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
588 #define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
589 #define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
590
591 #define INIT_ADS( cpu ) \
592     pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
593     pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
594     pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;
595
596     INIT7( sad, );
597     INIT7_NAME( sad_aligned, sad, );
598     INIT7( sad_x3, );
599     INIT7( sad_x4, );
600     INIT7( ssd, );
601     INIT7( satd, );
602     INIT7( satd_x3, );
603     INIT7( satd_x4, );
604     INIT4( hadamard_ac, );
605     INIT_ADS( );
606
607     pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16;
608     pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8;
609     pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
610     pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
611
612     pixf->ssim_4x4x2_core = ssim_4x4x2_core;
613     pixf->ssim_end4 = ssim_end4;
614
615 #ifdef HAVE_MMX
616     if( cpu&X264_CPU_MMX )
617     {
618         INIT7( ssd, _mmx );
619     }
620
621     if( cpu&X264_CPU_MMXEXT )
622     {
623         INIT7( sad, _mmxext );
624         INIT7_NAME( sad_aligned, sad, _mmxext );
625         INIT7( sad_x3, _mmxext );
626         INIT7( sad_x4, _mmxext );
627         INIT7( satd, _mmxext );
628         INIT7( satd_x3, _mmxext );
629         INIT7( satd_x4, _mmxext );
630         INIT4( hadamard_ac, _mmxext );
631         INIT_ADS( _mmxext );
632         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmxext;
633         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmxext;
634 #ifdef ARCH_X86
635         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmxext;
636         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmxext;
637         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmxext;
638         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_mmxext;
639
640         if( cpu&X264_CPU_CACHELINE_32 )
641         {
642             INIT5( sad, _cache32_mmxext );
643             INIT4( sad_x3, _cache32_mmxext );
644             INIT4( sad_x4, _cache32_mmxext );
645         }
646         else if( cpu&X264_CPU_CACHELINE_64 )
647         {
648             INIT5( sad, _cache64_mmxext );
649             INIT4( sad_x3, _cache64_mmxext );
650             INIT4( sad_x4, _cache64_mmxext );
651         }
652 #else
653         if( cpu&X264_CPU_CACHELINE_64 )
654         {
655             pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmxext;
656             pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmxext;
657             pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmxext;
658             pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmxext;
659             pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmxext;
660             pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmxext;
661             pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmxext;
662         }
663 #endif
664         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmxext;
665         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_mmxext;
666         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmxext;
667         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmxext;
668     }
669
670     if( cpu&X264_CPU_SSE2 )
671     {
672         INIT5( ssd, _sse2slow );
673         INIT2_NAME( sad_aligned, sad, _sse2_aligned );
674         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
675         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
676         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
677         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
678         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
679 #ifdef ARCH_X86_64
680         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
681 #endif
682     }
683
684     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
685     {
686         INIT2( sad, _sse2 );
687         INIT2( sad_x3, _sse2 );
688         INIT2( sad_x4, _sse2 );
689         INIT6( satd, _sse2 );
690         INIT6( satd_x3, _sse2 );
691         INIT6( satd_x4, _sse2 );
692         if( !(cpu&X264_CPU_STACK_MOD4) )
693         {
694             INIT4( hadamard_ac, _sse2 );
695         }
696         INIT_ADS( _sse2 );
697         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
698         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
699         if( cpu&X264_CPU_CACHELINE_64 )
700         {
701             INIT2( ssd, _sse2); /* faster for width 16 on p4 */
702 #ifdef ARCH_X86
703             INIT2( sad, _cache64_sse2 );
704             INIT2( sad_x3, _cache64_sse2 );
705             INIT2( sad_x4, _cache64_sse2 );
706 #endif
707            if( cpu&X264_CPU_SSE2_IS_FAST )
708            {
709                pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_sse2;
710                pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_sse2;
711            }
712         }
713
714         if( cpu&X264_CPU_SSE_MISALIGN )
715         {
716             INIT2( sad_x3, _sse2_misalign );
717             INIT2( sad_x4, _sse2_misalign );
718         }
719     }
720
721     if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
722     {
723         pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
724         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
725         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
726         pixf->sad_x3[PIXEL_8x8] = x264_pixel_sad_x3_8x8_sse2;
727         pixf->sad_x3[PIXEL_8x4] = x264_pixel_sad_x3_8x4_sse2;
728         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
729         pixf->sad_x4[PIXEL_8x8] = x264_pixel_sad_x4_8x8_sse2;
730         pixf->sad_x4[PIXEL_8x4] = x264_pixel_sad_x4_8x4_sse2;
731     }
732
733     if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
734     {
735         INIT2( sad, _sse3 );
736         INIT2( sad_x3, _sse3 );
737         INIT2( sad_x4, _sse3 );
738     }
739
740     if( cpu&X264_CPU_SSSE3 )
741     {
742         INIT7( ssd, _ssse3 );
743         INIT7( satd, _ssse3 );
744         INIT7( satd_x3, _ssse3 );
745         INIT7( satd_x4, _ssse3 );
746         if( !(cpu&X264_CPU_STACK_MOD4) )
747         {
748             INIT4( hadamard_ac, _ssse3 );
749         }
750         INIT_ADS( _ssse3 );
751         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
752         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
753         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
754         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
755         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
756         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_ssse3;
757 #ifdef ARCH_X86_64
758         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_ssse3;
759 #endif
760         if( cpu&X264_CPU_CACHELINE_64 )
761         {
762             INIT2( sad, _cache64_ssse3 );
763             INIT2( sad_x3, _cache64_ssse3 );
764             INIT2( sad_x4, _cache64_ssse3 );
765         }
766         if( !(cpu&X264_CPU_PHADD_IS_FAST) )
767         {
768             INIT5( ssd, _sse2 ); /* on conroe, sse2 is faster for width8/16 */
769         }
770     }
771
772     if( cpu&X264_CPU_SSE4 )
773     {
774         INIT7( satd, _sse4 );
775         INIT7( satd_x3, _sse4 );
776         INIT7( satd_x4, _sse4 );
777         if( !(cpu&X264_CPU_STACK_MOD4) )
778         {
779             INIT4( hadamard_ac, _sse4 );
780         }
781         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
782         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
783     }
784 #endif //HAVE_MMX
785
786 #ifdef ARCH_PPC
787     if( cpu&X264_CPU_ALTIVEC )
788     {
789         x264_pixel_altivec_init( pixf );
790     }
791 #endif
792 #ifdef ARCH_UltraSparc
793     INIT4( sad, _vis );
794     INIT4( sad_x3, _vis );
795     INIT4( sad_x4, _vis );
796 #endif
797
798     pixf->ads[PIXEL_8x16] =
799     pixf->ads[PIXEL_8x4] =
800     pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
801     pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
802 }
803