SSE2 high bit depth SSIM functions
[x262.git] / common / pixel.c
1 /*****************************************************************************
2  * pixel.c: pixel metrics
3  *****************************************************************************
4  * Copyright (C) 2003-2010 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jason Garrett-Glaser <darkshikari@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29
30 #if HAVE_MMX
31 #   include "x86/pixel.h"
32 #endif
33 #if ARCH_PPC
34 #   include "ppc/pixel.h"
35 #endif
36 #if ARCH_ARM
37 #   include "arm/pixel.h"
38 #endif
39 #if ARCH_UltraSPARC
40 #   include "sparc/pixel.h"
41 #endif
42
43
44 /****************************************************************************
45  * pixel_sad_WxH
46  ****************************************************************************/
47 #define PIXEL_SAD_C( name, lx, ly ) \
48 static int name( pixel *pix1, int i_stride_pix1,  \
49                  pixel *pix2, int i_stride_pix2 ) \
50 {                                                   \
51     int i_sum = 0;                                  \
52     for( int y = 0; y < ly; y++ )                   \
53     {                                               \
54         for( int x = 0; x < lx; x++ )               \
55         {                                           \
56             i_sum += abs( pix1[x] - pix2[x] );      \
57         }                                           \
58         pix1 += i_stride_pix1;                      \
59         pix2 += i_stride_pix2;                      \
60     }                                               \
61     return i_sum;                                   \
62 }
63
64
65 PIXEL_SAD_C( x264_pixel_sad_16x16, 16, 16 )
66 PIXEL_SAD_C( x264_pixel_sad_16x8,  16,  8 )
67 PIXEL_SAD_C( x264_pixel_sad_8x16,   8, 16 )
68 PIXEL_SAD_C( x264_pixel_sad_8x8,    8,  8 )
69 PIXEL_SAD_C( x264_pixel_sad_8x4,    8,  4 )
70 PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
71 PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
72
73
74 /****************************************************************************
75  * pixel_ssd_WxH
76  ****************************************************************************/
77 #define PIXEL_SSD_C( name, lx, ly ) \
78 static int name( pixel *pix1, int i_stride_pix1,  \
79                  pixel *pix2, int i_stride_pix2 ) \
80 {                                                   \
81     int i_sum = 0;                                  \
82     for( int y = 0; y < ly; y++ )                   \
83     {                                               \
84         for( int x = 0; x < lx; x++ )               \
85         {                                           \
86             int d = pix1[x] - pix2[x];              \
87             i_sum += d*d;                           \
88         }                                           \
89         pix1 += i_stride_pix1;                      \
90         pix2 += i_stride_pix2;                      \
91     }                                               \
92     return i_sum;                                   \
93 }
94
95 PIXEL_SSD_C( x264_pixel_ssd_16x16, 16, 16 )
96 PIXEL_SSD_C( x264_pixel_ssd_16x8,  16,  8 )
97 PIXEL_SSD_C( x264_pixel_ssd_8x16,   8, 16 )
98 PIXEL_SSD_C( x264_pixel_ssd_8x8,    8,  8 )
99 PIXEL_SSD_C( x264_pixel_ssd_8x4,    8,  4 )
100 PIXEL_SSD_C( x264_pixel_ssd_4x8,    4,  8 )
101 PIXEL_SSD_C( x264_pixel_ssd_4x4,    4,  4 )
102
103 uint64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, pixel *pix1, int i_pix1, pixel *pix2, int i_pix2, int i_width, int i_height )
104 {
105     uint64_t i_ssd = 0;
106     int y;
107     int align = !(((intptr_t)pix1 | (intptr_t)pix2 | i_pix1 | i_pix2) & 15);
108
109 #define SSD(size) i_ssd += pf->ssd[size]( pix1 + y*i_pix1 + x, i_pix1, \
110                                           pix2 + y*i_pix2 + x, i_pix2 );
111     for( y = 0; y < i_height-15; y += 16 )
112     {
113         int x = 0;
114         if( align )
115             for( ; x < i_width-15; x += 16 )
116                 SSD(PIXEL_16x16);
117         for( ; x < i_width-7; x += 8 )
118             SSD(PIXEL_8x16);
119     }
120     if( y < i_height-7 )
121         for( int x = 0; x < i_width-7; x += 8 )
122             SSD(PIXEL_8x8);
123 #undef SSD
124
125 #define SSD1 { int d = pix1[y*i_pix1+x] - pix2[y*i_pix2+x]; i_ssd += d*d; }
126     if( i_width & 7 )
127     {
128         for( y = 0; y < (i_height & ~7); y++ )
129             for( int x = i_width & ~7; x < i_width; x++ )
130                 SSD1;
131     }
132     if( i_height & 7 )
133     {
134         for( y = i_height & ~7; y < i_height; y++ )
135             for( int x = 0; x < i_width; x++ )
136                 SSD1;
137     }
138 #undef SSD1
139
140     return i_ssd;
141 }
142
143 static void pixel_ssd_nv12_core( pixel *pixuv1, int stride1, pixel *pixuv2, int stride2, int width, int height, uint64_t *ssd_u, uint64_t *ssd_v )
144 {
145     *ssd_u = 0, *ssd_v = 0;
146     for( int y = 0; y < height; y++, pixuv1+=stride1, pixuv2+=stride2 )
147         for( int x = 0; x < width; x++ )
148         {
149             int du = pixuv1[2*x]   - pixuv2[2*x];
150             int dv = pixuv1[2*x+1] - pixuv2[2*x+1];
151             *ssd_u += du*du;
152             *ssd_v += dv*dv;
153         }
154 }
155
156 void x264_pixel_ssd_nv12( x264_pixel_function_t *pf, pixel *pix1, int i_pix1, pixel *pix2, int i_pix2, int i_width, int i_height, uint64_t *ssd_u, uint64_t *ssd_v )
157 {
158     pf->ssd_nv12_core( pix1, i_pix1, pix2, i_pix2, i_width&~7, i_height, ssd_u, ssd_v );
159     if( i_width&7 )
160     {
161         uint64_t tmp[2];
162         pixel_ssd_nv12_core( pix1+(i_width&~7), i_pix1, pix2+(i_width&~7), i_pix2, i_width&7, i_height, &tmp[0], &tmp[1] );
163         *ssd_u += tmp[0];
164         *ssd_v += tmp[1];
165     }
166 }
167
168 /****************************************************************************
169  * pixel_var_wxh
170  ****************************************************************************/
171 #define PIXEL_VAR_C( name, w ) \
172 static uint64_t name( pixel *pix, int i_stride ) \
173 {                                             \
174     uint32_t sum = 0, sqr = 0;                \
175     for( int y = 0; y < w; y++ )              \
176     {                                         \
177         for( int x = 0; x < w; x++ )          \
178         {                                     \
179             sum += pix[x];                    \
180             sqr += pix[x] * pix[x];           \
181         }                                     \
182         pix += i_stride;                      \
183     }                                         \
184     return sum + ((uint64_t)sqr << 32);       \
185 }
186
187 PIXEL_VAR_C( x264_pixel_var_16x16, 16 )
188 PIXEL_VAR_C( x264_pixel_var_8x8,    8 )
189
190 /****************************************************************************
191  * pixel_var2_wxh
192  ****************************************************************************/
193 static int pixel_var2_8x8( pixel *pix1, int i_stride1, pixel *pix2, int i_stride2, int *ssd )
194 {
195     uint32_t var = 0, sum = 0, sqr = 0;
196     for( int y = 0; y < 8; y++ )
197     {
198         for( int x = 0; x < 8; x++ )
199         {
200             int diff = pix1[x] - pix2[x];
201             sum += diff;
202             sqr += diff * diff;
203         }
204         pix1 += i_stride1;
205         pix2 += i_stride2;
206     }
207     sum = abs(sum);
208     var = sqr - ((uint64_t)sum * sum >> 6);
209     *ssd = sqr;
210     return var;
211 }
212
213 #if BIT_DEPTH > 8
214     typedef uint32_t sum_t;
215     typedef uint64_t sum2_t;
216 #else
217     typedef uint16_t sum_t;
218     typedef uint32_t sum2_t;
219 #endif
220 #define BITS_PER_SUM (8 * sizeof(sum_t))
221
222 #define HADAMARD4(d0, d1, d2, d3, s0, s1, s2, s3) {\
223     sum2_t t0 = s0 + s1;\
224     sum2_t t1 = s0 - s1;\
225     sum2_t t2 = s2 + s3;\
226     sum2_t t3 = s2 - s3;\
227     d0 = t0 + t2;\
228     d2 = t0 - t2;\
229     d1 = t1 + t3;\
230     d3 = t1 - t3;\
231 }
232
233 // in: a pseudo-simd number of the form x+(y<<16)
234 // return: abs(x)+(abs(y)<<16)
235 static ALWAYS_INLINE sum2_t abs2( sum2_t a )
236 {
237     sum2_t s = ((a>>(BITS_PER_SUM-1))&(((sum2_t)1<<BITS_PER_SUM)+1))*((sum_t)-1);
238     return (a+s)^s;
239 }
240
241 /****************************************************************************
242  * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
243  ****************************************************************************/
244
245 static NOINLINE int x264_pixel_satd_4x4( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )
246 {
247     sum2_t tmp[4][2];
248     sum2_t a0, a1, a2, a3, b0, b1;
249     sum2_t sum = 0;
250     for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
251     {
252         a0 = pix1[0] - pix2[0];
253         a1 = pix1[1] - pix2[1];
254         b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
255         a2 = pix1[2] - pix2[2];
256         a3 = pix1[3] - pix2[3];
257         b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
258         tmp[i][0] = b0 + b1;
259         tmp[i][1] = b0 - b1;
260     }
261     for( int i = 0; i < 2; i++ )
262     {
263         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
264         a0 = abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
265         sum += ((sum_t)a0) + (a0>>BITS_PER_SUM);
266     }
267     return sum >> 1;
268 }
269
270 static NOINLINE int x264_pixel_satd_8x4( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )
271 {
272     sum2_t tmp[4][4];
273     sum2_t a0, a1, a2, a3;
274     sum2_t sum = 0;
275     for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
276     {
277         a0 = (pix1[0] - pix2[0]) + ((sum2_t)(pix1[4] - pix2[4]) << BITS_PER_SUM);
278         a1 = (pix1[1] - pix2[1]) + ((sum2_t)(pix1[5] - pix2[5]) << BITS_PER_SUM);
279         a2 = (pix1[2] - pix2[2]) + ((sum2_t)(pix1[6] - pix2[6]) << BITS_PER_SUM);
280         a3 = (pix1[3] - pix2[3]) + ((sum2_t)(pix1[7] - pix2[7]) << BITS_PER_SUM);
281         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
282     }
283     for( int i = 0; i < 4; i++ )
284     {
285         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
286         sum += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
287     }
288     return (((sum_t)sum) + (sum>>BITS_PER_SUM)) >> 1;
289 }
290
291 #define PIXEL_SATD_C( w, h, sub )\
292 static int x264_pixel_satd_##w##x##h( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )\
293 {\
294     int sum = sub( pix1, i_pix1, pix2, i_pix2 )\
295             + sub( pix1+4*i_pix1, i_pix1, pix2+4*i_pix2, i_pix2 );\
296     if( w==16 )\
297         sum+= sub( pix1+8, i_pix1, pix2+8, i_pix2 )\
298             + sub( pix1+8+4*i_pix1, i_pix1, pix2+8+4*i_pix2, i_pix2 );\
299     if( h==16 )\
300         sum+= sub( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )\
301             + sub( pix1+12*i_pix1, i_pix1, pix2+12*i_pix2, i_pix2 );\
302     if( w==16 && h==16 )\
303         sum+= sub( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 )\
304             + sub( pix1+8+12*i_pix1, i_pix1, pix2+8+12*i_pix2, i_pix2 );\
305     return sum;\
306 }
307 PIXEL_SATD_C( 16, 16, x264_pixel_satd_8x4 )
308 PIXEL_SATD_C( 16, 8,  x264_pixel_satd_8x4 )
309 PIXEL_SATD_C( 8,  16, x264_pixel_satd_8x4 )
310 PIXEL_SATD_C( 8,  8,  x264_pixel_satd_8x4 )
311 PIXEL_SATD_C( 4,  8,  x264_pixel_satd_4x4 )
312
313
314 static NOINLINE int sa8d_8x8( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )
315 {
316     sum2_t tmp[8][4];
317     sum2_t a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3;
318     sum2_t sum = 0;
319     for( int i = 0; i < 8; i++, pix1 += i_pix1, pix2 += i_pix2 )
320     {
321         a0 = pix1[0] - pix2[0];
322         a1 = pix1[1] - pix2[1];
323         b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
324         a2 = pix1[2] - pix2[2];
325         a3 = pix1[3] - pix2[3];
326         b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
327         a4 = pix1[4] - pix2[4];
328         a5 = pix1[5] - pix2[5];
329         b2 = (a4+a5) + ((a4-a5)<<BITS_PER_SUM);
330         a6 = pix1[6] - pix2[6];
331         a7 = pix1[7] - pix2[7];
332         b3 = (a6+a7) + ((a6-a7)<<BITS_PER_SUM);
333         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], b0,b1,b2,b3 );
334     }
335     for( int i = 0; i < 4; i++ )
336     {
337         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
338         HADAMARD4( a4, a5, a6, a7, tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i] );
339         b0  = abs2(a0+a4) + abs2(a0-a4);
340         b0 += abs2(a1+a5) + abs2(a1-a5);
341         b0 += abs2(a2+a6) + abs2(a2-a6);
342         b0 += abs2(a3+a7) + abs2(a3-a7);
343         sum += (sum_t)b0 + (b0>>BITS_PER_SUM);
344     }
345     return sum;
346 }
347
348 static int x264_pixel_sa8d_8x8( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )
349 {
350     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 );
351     return (sum+2)>>2;
352 }
353
354 static int x264_pixel_sa8d_16x16( pixel *pix1, int i_pix1, pixel *pix2, int i_pix2 )
355 {
356     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 )
357             + sa8d_8x8( pix1+8, i_pix1, pix2+8, i_pix2 )
358             + sa8d_8x8( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )
359             + sa8d_8x8( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 );
360     return (sum+2)>>2;
361 }
362
363
364 static NOINLINE uint64_t pixel_hadamard_ac( pixel *pix, int stride )
365 {
366     sum2_t tmp[32];
367     sum2_t a0, a1, a2, a3, dc;
368     sum2_t sum4 = 0, sum8 = 0;
369     for( int i = 0; i < 8; i++, pix+=stride )
370     {
371         sum2_t *t = tmp + (i&3) + (i&4)*4;
372         a0 = (pix[0]+pix[1]) + ((sum2_t)(pix[0]-pix[1])<<BITS_PER_SUM);
373         a1 = (pix[2]+pix[3]) + ((sum2_t)(pix[2]-pix[3])<<BITS_PER_SUM);
374         t[0] = a0 + a1;
375         t[4] = a0 - a1;
376         a2 = (pix[4]+pix[5]) + ((sum2_t)(pix[4]-pix[5])<<BITS_PER_SUM);
377         a3 = (pix[6]+pix[7]) + ((sum2_t)(pix[6]-pix[7])<<BITS_PER_SUM);
378         t[8] = a2 + a3;
379         t[12] = a2 - a3;
380     }
381     for( int i = 0; i < 8; i++ )
382     {
383         HADAMARD4( a0, a1, a2, a3, tmp[i*4+0], tmp[i*4+1], tmp[i*4+2], tmp[i*4+3] );
384         tmp[i*4+0] = a0;
385         tmp[i*4+1] = a1;
386         tmp[i*4+2] = a2;
387         tmp[i*4+3] = a3;
388         sum4 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
389     }
390     for( int i = 0; i < 8; i++ )
391     {
392         HADAMARD4( a0,a1,a2,a3, tmp[i], tmp[8+i], tmp[16+i], tmp[24+i] );
393         sum8 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
394     }
395     dc = (sum_t)(tmp[0] + tmp[8] + tmp[16] + tmp[24]);
396     sum4 = (sum_t)sum4 + (sum4>>BITS_PER_SUM) - dc;
397     sum8 = (sum_t)sum8 + (sum8>>BITS_PER_SUM) - dc;
398     return ((uint64_t)sum8<<32) + sum4;
399 }
400
401 #define HADAMARD_AC(w,h) \
402 static uint64_t x264_pixel_hadamard_ac_##w##x##h( pixel *pix, int stride )\
403 {\
404     uint64_t sum = pixel_hadamard_ac( pix, stride );\
405     if( w==16 )\
406         sum += pixel_hadamard_ac( pix+8, stride );\
407     if( h==16 )\
408         sum += pixel_hadamard_ac( pix+8*stride, stride );\
409     if( w==16 && h==16 )\
410         sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
411     return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
412 }
413 HADAMARD_AC( 16, 16 )
414 HADAMARD_AC( 16, 8 )
415 HADAMARD_AC( 8, 16 )
416 HADAMARD_AC( 8, 8 )
417
418
419 /****************************************************************************
420  * pixel_sad_x4
421  ****************************************************************************/
422 #define SAD_X( size ) \
423 static void x264_pixel_sad_x3_##size( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, int i_stride, int scores[3] )\
424 {\
425     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
426     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
427     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
428 }\
429 static void x264_pixel_sad_x4_##size( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, pixel *pix3, int i_stride, int scores[4] )\
430 {\
431     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
432     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
433     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
434     scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
435 }
436
437 SAD_X( 16x16 )
438 SAD_X( 16x8 )
439 SAD_X( 8x16 )
440 SAD_X( 8x8 )
441 SAD_X( 8x4 )
442 SAD_X( 4x8 )
443 SAD_X( 4x4 )
444
445 #if !HIGH_BIT_DEPTH
446 #if ARCH_UltraSPARC
447 SAD_X( 16x16_vis )
448 SAD_X( 16x8_vis )
449 SAD_X( 8x16_vis )
450 SAD_X( 8x8_vis )
451 #endif
452 #endif // !HIGH_BIT_DEPTH
453
454 /****************************************************************************
455  * pixel_satd_x4
456  * no faster than single satd, but needed for satd to be a drop-in replacement for sad
457  ****************************************************************************/
458
459 #define SATD_X( size, cpu ) \
460 static void x264_pixel_satd_x3_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, int i_stride, int scores[3] )\
461 {\
462     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
463     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
464     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
465 }\
466 static void x264_pixel_satd_x4_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, pixel *pix3, int i_stride, int scores[4] )\
467 {\
468     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
469     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
470     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
471     scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
472 }
473 #define SATD_X_DECL6( cpu )\
474 SATD_X( 16x16, cpu )\
475 SATD_X( 16x8, cpu )\
476 SATD_X( 8x16, cpu )\
477 SATD_X( 8x8, cpu )\
478 SATD_X( 8x4, cpu )\
479 SATD_X( 4x8, cpu )
480 #define SATD_X_DECL7( cpu )\
481 SATD_X_DECL6( cpu )\
482 SATD_X( 4x4, cpu )
483
484 SATD_X_DECL7()
485 #if HAVE_MMX
486 SATD_X_DECL7( _mmxext )
487 #if !HIGH_BIT_DEPTH
488 SATD_X_DECL6( _sse2 )
489 SATD_X_DECL7( _ssse3 )
490 SATD_X_DECL7( _sse4 )
491 #endif // !HIGH_BIT_DEPTH
492 #endif
493
494 #if !HIGH_BIT_DEPTH
495 #if HAVE_ARMV6
496 SATD_X_DECL7( _neon )
497 #endif
498 #endif // !HIGH_BIT_DEPTH
499
500 #define INTRA_MBCMP_8x8( mbcmp, cpu )\
501 void x264_intra_##mbcmp##_x3_8x8##cpu( pixel *fenc, pixel edge[33], int res[3] )\
502 {\
503     ALIGNED_ARRAY_16( pixel, pix, [8*FDEC_STRIDE] );\
504     x264_predict_8x8_v_c( pix, edge );\
505     res[0] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
506     x264_predict_8x8_h_c( pix, edge );\
507     res[1] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
508     x264_predict_8x8_dc_c( pix, edge );\
509     res[2] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
510 }
511
512 INTRA_MBCMP_8x8( sad, )
513 INTRA_MBCMP_8x8(sa8d, )
514 #if HIGH_BIT_DEPTH && HAVE_MMX
515 INTRA_MBCMP_8x8( sad, _mmxext)
516 INTRA_MBCMP_8x8( sad, _sse2  )
517 INTRA_MBCMP_8x8( sad, _ssse3 )
518 INTRA_MBCMP_8x8(sa8d, _sse2  )
519 #endif
520
521 #define INTRA_MBCMP( mbcmp, size, pred1, pred2, pred3, chroma, cpu )\
522 void x264_intra_##mbcmp##_x3_##size##x##size##chroma##cpu( pixel *fenc, pixel *fdec, int res[3] )\
523 {\
524     x264_predict_##size##x##size##chroma##_##pred1##_c( fdec );\
525     res[0] = x264_pixel_##mbcmp##_##size##x##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
526     x264_predict_##size##x##size##chroma##_##pred2##_c( fdec );\
527     res[1] = x264_pixel_##mbcmp##_##size##x##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
528     x264_predict_##size##x##size##chroma##_##pred3##_c( fdec );\
529     res[2] = x264_pixel_##mbcmp##_##size##x##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
530 }
531
532 INTRA_MBCMP( sad,  4,  v, h, dc,  , )
533 INTRA_MBCMP(satd,  4,  v, h, dc,  , )
534 INTRA_MBCMP( sad,  8, dc, h,  v, c, )
535 INTRA_MBCMP(satd,  8, dc, h,  v, c, )
536 INTRA_MBCMP( sad, 16,  v, h, dc,  , )
537 INTRA_MBCMP(satd, 16,  v, h, dc,  , )
538
539 #if HIGH_BIT_DEPTH && HAVE_MMX
540 INTRA_MBCMP( sad,  4,  v, h, dc,  , _mmxext)
541 INTRA_MBCMP(satd,  4,  v, h, dc,  , _mmxext)
542 INTRA_MBCMP( sad,  8, dc, h,  v, c, _mmxext)
543 INTRA_MBCMP(satd,  8, dc, h,  v, c, _mmxext)
544 INTRA_MBCMP( sad, 16,  v, h, dc,  , _mmxext)
545 INTRA_MBCMP(satd, 16,  v, h, dc,  , _mmxext)
546 INTRA_MBCMP( sad,  8, dc, h,  v, c, _sse2  )
547 INTRA_MBCMP( sad, 16,  v, h, dc,  , _sse2  )
548 INTRA_MBCMP( sad,  8, dc, h,  v, c, _ssse3 )
549 INTRA_MBCMP( sad, 16,  v, h, dc,  , _ssse3 )
550 #endif
551
552 /****************************************************************************
553  * structural similarity metric
554  ****************************************************************************/
555 static void ssim_4x4x2_core( const pixel *pix1, int stride1,
556                              const pixel *pix2, int stride2,
557                              int sums[2][4])
558 {
559     for( int z = 0; z < 2; z++ )
560     {
561         uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
562         for( int y = 0; y < 4; y++ )
563             for( int x = 0; x < 4; x++ )
564             {
565                 int a = pix1[x+y*stride1];
566                 int b = pix2[x+y*stride2];
567                 s1  += a;
568                 s2  += b;
569                 ss  += a*a;
570                 ss  += b*b;
571                 s12 += a*b;
572             }
573         sums[z][0] = s1;
574         sums[z][1] = s2;
575         sums[z][2] = ss;
576         sums[z][3] = s12;
577         pix1 += 4;
578         pix2 += 4;
579     }
580 }
581
582 static float ssim_end1( int s1, int s2, int ss, int s12 )
583 {
584 /* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
585  * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
586  * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
587 #if BIT_DEPTH > 9
588 #define type float
589     static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
590     static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
591 #else
592 #define type int
593     static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
594     static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
595 #endif
596     type fs1 = s1;
597     type fs2 = s2;
598     type fss = ss;
599     type fs12 = s12;
600     type vars = fss*64 - fs1*fs1 - fs2*fs2;
601     type covar = fs12*64 - fs1*fs2;
602     return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
603          / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
604 #undef type
605 }
606
607 static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
608 {
609     float ssim = 0.0;
610     for( int i = 0; i < width; i++ )
611         ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
612                            sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
613                            sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
614                            sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
615     return ssim;
616 }
617
618 float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
619                            pixel *pix1, int stride1,
620                            pixel *pix2, int stride2,
621                            int width, int height, void *buf )
622 {
623     int z = 0;
624     float ssim = 0.0;
625     int (*sum0)[4] = buf;
626     int (*sum1)[4] = sum0 + (width >> 2) + 3;
627     width >>= 2;
628     height >>= 2;
629     for( int y = 1; y < height; y++ )
630     {
631         for( ; z <= y; z++ )
632         {
633             XCHG( void*, sum0, sum1 );
634             for( int x = 0; x < width; x+=2 )
635                 pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
636         }
637         for( int x = 0; x < width-1; x += 4 )
638             ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
639     }
640     return ssim;
641 }
642
643
644 /****************************************************************************
645  * successive elimination
646  ****************************************************************************/
647 static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
648                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
649 {
650     int nmv = 0;
651     for( int i = 0; i < width; i++, sums++ )
652     {
653         int ads = abs( enc_dc[0] - sums[0] )
654                 + abs( enc_dc[1] - sums[8] )
655                 + abs( enc_dc[2] - sums[delta] )
656                 + abs( enc_dc[3] - sums[delta+8] )
657                 + cost_mvx[i];
658         if( ads < thresh )
659             mvs[nmv++] = i;
660     }
661     return nmv;
662 }
663
664 static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
665                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
666 {
667     int nmv = 0;
668     for( int i = 0; i < width; i++, sums++ )
669     {
670         int ads = abs( enc_dc[0] - sums[0] )
671                 + abs( enc_dc[1] - sums[delta] )
672                 + cost_mvx[i];
673         if( ads < thresh )
674             mvs[nmv++] = i;
675     }
676     return nmv;
677 }
678
679 static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
680                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
681 {
682     int nmv = 0;
683     for( int i = 0; i<width; i++, sums++ )
684     {
685         int ads = abs( enc_dc[0] - sums[0] )
686                 + cost_mvx[i];
687         if( ads < thresh )
688             mvs[nmv++] = i;
689     }
690     return nmv;
691 }
692
693
694 /****************************************************************************
695  * x264_pixel_init:
696  ****************************************************************************/
697 void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
698 {
699     memset( pixf, 0, sizeof(*pixf) );
700
701 #define INIT2_NAME( name1, name2, cpu ) \
702     pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
703     pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
704 #define INIT4_NAME( name1, name2, cpu ) \
705     INIT2_NAME( name1, name2, cpu ) \
706     pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
707     pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
708 #define INIT5_NAME( name1, name2, cpu ) \
709     INIT4_NAME( name1, name2, cpu ) \
710     pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
711 #define INIT6_NAME( name1, name2, cpu ) \
712     INIT5_NAME( name1, name2, cpu ) \
713     pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
714 #define INIT7_NAME( name1, name2, cpu ) \
715     INIT6_NAME( name1, name2, cpu ) \
716     pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
717 #define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
718 #define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
719 #define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
720 #define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
721 #define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
722
723 #define INIT_ADS( cpu ) \
724     pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
725     pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
726     pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;
727
728     INIT7( sad, );
729     INIT7_NAME( sad_aligned, sad, );
730     INIT7( sad_x3, );
731     INIT7( sad_x4, );
732     INIT7( ssd, );
733     INIT7( satd, );
734     INIT7( satd_x3, );
735     INIT7( satd_x4, );
736     INIT4( hadamard_ac, );
737     INIT_ADS( );
738
739     pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16;
740     pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8;
741     pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
742     pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
743
744     pixf->ssd_nv12_core = pixel_ssd_nv12_core;
745     pixf->ssim_4x4x2_core = ssim_4x4x2_core;
746     pixf->ssim_end4 = ssim_end4;
747     pixf->var2_8x8 = pixel_var2_8x8;
748
749     pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4;
750     pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4;
751     pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8;
752     pixf->intra_sa8d_x3_8x8   = x264_intra_sa8d_x3_8x8;
753     pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c;
754     pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c;
755     pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16;
756     pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16;
757
758 #if HIGH_BIT_DEPTH
759 #if HAVE_MMX
760     if( cpu&X264_CPU_MMXEXT )
761     {
762         INIT7( sad, _mmxext );
763         INIT7( sad_x3, _mmxext );
764         INIT7( sad_x4, _mmxext );
765         INIT7( satd, _mmxext );
766         INIT7( satd_x3, _mmxext );
767         INIT7( satd_x4, _mmxext );
768         INIT4( hadamard_ac, _mmxext );
769         INIT7( ssd, _mmxext );
770         INIT_ADS( _mmxext );
771
772         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_mmxext;
773         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmxext;
774         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmxext;
775         pixf->var2_8x8 = x264_pixel_var2_8x8_mmxext;
776
777         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmxext;
778         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmxext;
779         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmxext;
780         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmxext;
781         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmxext;
782         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmxext;
783         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmxext;
784     }
785     if( cpu&X264_CPU_SSE2 )
786     {
787         INIT4_NAME( sad_aligned, sad, _sse2_aligned );
788         INIT5( ssd, _sse2 );
789
790         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
791         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
792 #if ARCH_X86_64
793         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
794 #endif
795         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_sse2;
796         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
797         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
798         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
799         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
800         pixf->var2_8x8 = x264_pixel_var2_8x8_sse2;
801     }
802     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
803     {
804         INIT5( sad, _sse2 );
805         INIT2( sad_x3, _sse2 );
806         INIT2( sad_x4, _sse2 );
807         INIT_ADS( _sse2 );
808
809         if( !(cpu&X264_CPU_STACK_MOD4) )
810         {
811             INIT4( hadamard_ac, _sse2 );
812         }
813
814         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_sse2;
815         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_sse2;
816         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_sse2;
817     }
818     if( cpu&X264_CPU_SSE2_IS_FAST )
819     {
820         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
821         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
822         pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_sse2;
823         pixf->sad_x3[PIXEL_8x4]  = x264_pixel_sad_x3_8x4_sse2;
824         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
825         pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_sse2;
826         pixf->sad_x4[PIXEL_8x4]  = x264_pixel_sad_x4_8x4_sse2;
827     }
828     if( cpu&X264_CPU_SSSE3 )
829     {
830         INIT4_NAME( sad_aligned, sad, _ssse3_aligned );
831         INIT7( sad, _ssse3 );
832         INIT7( sad_x3, _ssse3 );
833         INIT7( sad_x4, _ssse3 );
834         INIT_ADS( _ssse3 );
835
836         if( !(cpu&X264_CPU_STACK_MOD4) )
837         {
838             INIT4( hadamard_ac, _ssse3 );
839         }
840
841         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
842         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
843         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_ssse3;
844         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
845         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
846     }
847 #endif // HAVE_MMX
848 #else // !HIGH_BIT_DEPTH
849 #if HAVE_MMX
850     if( cpu&X264_CPU_MMX )
851     {
852         INIT7( ssd, _mmx );
853     }
854
855     if( cpu&X264_CPU_MMXEXT )
856     {
857         INIT7( sad, _mmxext );
858         INIT7_NAME( sad_aligned, sad, _mmxext );
859         INIT7( sad_x3, _mmxext );
860         INIT7( sad_x4, _mmxext );
861         INIT7( satd, _mmxext );
862         INIT7( satd_x3, _mmxext );
863         INIT7( satd_x4, _mmxext );
864         INIT4( hadamard_ac, _mmxext );
865         INIT_ADS( _mmxext );
866         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmxext;
867         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmxext;
868         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_mmxext;
869 #if ARCH_X86
870         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmxext;
871         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmxext;
872         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmxext;
873         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_mmxext;
874         pixf->var2_8x8 = x264_pixel_var2_8x8_mmxext;
875
876         if( cpu&X264_CPU_CACHELINE_32 )
877         {
878             INIT5( sad, _cache32_mmxext );
879             INIT4( sad_x3, _cache32_mmxext );
880             INIT4( sad_x4, _cache32_mmxext );
881         }
882         else if( cpu&X264_CPU_CACHELINE_64 )
883         {
884             INIT5( sad, _cache64_mmxext );
885             INIT4( sad_x3, _cache64_mmxext );
886             INIT4( sad_x4, _cache64_mmxext );
887         }
888 #else
889         if( cpu&X264_CPU_CACHELINE_64 )
890         {
891             pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmxext;
892             pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmxext;
893             pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmxext;
894             pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmxext;
895             pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmxext;
896             pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmxext;
897             pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmxext;
898         }
899 #endif
900         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmxext;
901         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmxext;
902         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmxext;
903         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmxext;
904         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmxext;
905         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmxext;
906         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmxext;
907     }
908
909     if( cpu&X264_CPU_SSE2 )
910     {
911         INIT5( ssd, _sse2slow );
912         INIT2_NAME( sad_aligned, sad, _sse2_aligned );
913         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
914         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_sse2;
915         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
916         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
917         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
918         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
919 #if ARCH_X86_64
920         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
921 #endif
922         pixf->var2_8x8 = x264_pixel_var2_8x8_sse2;
923     }
924
925     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
926     {
927         INIT2( sad, _sse2 );
928         INIT2( sad_x3, _sse2 );
929         INIT2( sad_x4, _sse2 );
930         INIT6( satd, _sse2 );
931         INIT6( satd_x3, _sse2 );
932         INIT6( satd_x4, _sse2 );
933         if( !(cpu&X264_CPU_STACK_MOD4) )
934         {
935             INIT4( hadamard_ac, _sse2 );
936         }
937         INIT_ADS( _sse2 );
938         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
939         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
940         if( cpu&X264_CPU_CACHELINE_64 )
941         {
942             INIT2( ssd, _sse2); /* faster for width 16 on p4 */
943 #if ARCH_X86
944             INIT2( sad, _cache64_sse2 );
945             INIT2( sad_x3, _cache64_sse2 );
946             INIT2( sad_x4, _cache64_sse2 );
947 #endif
948            if( cpu&X264_CPU_SSE2_IS_FAST )
949            {
950                pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_sse2;
951                pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_sse2;
952            }
953         }
954
955         if( cpu&X264_CPU_SSE_MISALIGN )
956         {
957             INIT2( sad_x3, _sse2_misalign );
958             INIT2( sad_x4, _sse2_misalign );
959         }
960     }
961
962     if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
963     {
964         pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
965         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
966         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
967         pixf->sad_x3[PIXEL_8x8] = x264_pixel_sad_x3_8x8_sse2;
968         pixf->sad_x3[PIXEL_8x4] = x264_pixel_sad_x3_8x4_sse2;
969         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
970         pixf->sad_x4[PIXEL_8x8] = x264_pixel_sad_x4_8x8_sse2;
971         pixf->sad_x4[PIXEL_8x4] = x264_pixel_sad_x4_8x4_sse2;
972     }
973
974     if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
975     {
976         INIT2( sad, _sse3 );
977         INIT2( sad_x3, _sse3 );
978         INIT2( sad_x4, _sse3 );
979     }
980
981     if( cpu&X264_CPU_SSSE3 )
982     {
983         if( !(cpu&X264_CPU_STACK_MOD4) )
984         {
985             INIT4( hadamard_ac, _ssse3 );
986         }
987         INIT_ADS( _ssse3 );
988         if( !(cpu&X264_CPU_SLOW_ATOM) )
989         {
990             INIT7( ssd, _ssse3 );
991             pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
992             pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
993             INIT7( satd, _ssse3 );
994             INIT7( satd_x3, _ssse3 );
995             INIT7( satd_x4, _ssse3 );
996         }
997         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
998         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
999         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
1000         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
1001         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_ssse3;
1002 #if ARCH_X86_64
1003         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_ssse3;
1004 #endif
1005         pixf->var2_8x8 = x264_pixel_var2_8x8_ssse3;
1006         if( cpu&X264_CPU_CACHELINE_64 )
1007         {
1008             INIT2( sad, _cache64_ssse3 );
1009             INIT2( sad_x3, _cache64_ssse3 );
1010             INIT2( sad_x4, _cache64_ssse3 );
1011         }
1012         if( cpu&X264_CPU_SLOW_ATOM || !(cpu&X264_CPU_SHUFFLE_IS_FAST) )
1013         {
1014             INIT5( ssd, _sse2 ); /* on conroe, sse2 is faster for width8/16 */
1015         }
1016     }
1017
1018     if( cpu&X264_CPU_SSE4 )
1019     {
1020         INIT7( satd, _sse4 );
1021         INIT7( satd_x3, _sse4 );
1022         INIT7( satd_x4, _sse4 );
1023         if( !(cpu&X264_CPU_STACK_MOD4) )
1024         {
1025             INIT4( hadamard_ac, _sse4 );
1026         }
1027         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
1028         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
1029         pixf->intra_sad_x3_4x4 = x264_intra_sad_x3_4x4_sse4;
1030         /* Slower on Conroe, so only enable under SSE4 */
1031         pixf->intra_sad_x3_8x8  = x264_intra_sad_x3_8x8_ssse3;
1032     }
1033 #endif //HAVE_MMX
1034
1035 #if HAVE_ARMV6
1036     if( cpu&X264_CPU_ARMV6 )
1037     {
1038         pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1039         pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1040         pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1041         pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1042     }
1043     if( cpu&X264_CPU_NEON )
1044     {
1045         INIT5( sad, _neon );
1046         INIT5( sad_aligned, _neon );
1047         INIT7( sad_x3, _neon );
1048         INIT7( sad_x4, _neon );
1049         INIT7( ssd, _neon );
1050         INIT7( satd, _neon );
1051         INIT7( satd_x3, _neon );
1052         INIT7( satd_x4, _neon );
1053         INIT4( hadamard_ac, _neon );
1054         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_neon;
1055         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_neon;
1056         pixf->var[PIXEL_8x8]    = x264_pixel_var_8x8_neon;
1057         pixf->var[PIXEL_16x16]  = x264_pixel_var_16x16_neon;
1058         pixf->var2_8x8          = x264_pixel_var2_8x8_neon;
1059
1060         pixf->ssim_4x4x2_core   = x264_pixel_ssim_4x4x2_core_neon;
1061         pixf->ssim_end4         = x264_pixel_ssim_end4_neon;
1062
1063         if( cpu&X264_CPU_FAST_NEON_MRC )
1064         {
1065             pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_neon;
1066             pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_neon;
1067             pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_aligned_4x8_neon;
1068             pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_aligned_4x4_neon;
1069         }
1070         else    // really just scheduled for dual issue / A8
1071         {
1072             INIT5( sad_aligned, _neon_dual );
1073         }
1074     }
1075 #endif
1076 #endif // HIGH_BIT_DEPTH
1077 #if HAVE_ALTIVEC
1078     if( cpu&X264_CPU_ALTIVEC )
1079     {
1080         x264_pixel_altivec_init( pixf );
1081     }
1082 #endif
1083 #if !HIGH_BIT_DEPTH
1084 #if ARCH_UltraSPARC
1085     INIT4( sad, _vis );
1086     INIT4( sad_x3, _vis );
1087     INIT4( sad_x4, _vis );
1088 #endif
1089 #endif // !HIGH_BIT_DEPTH
1090
1091     pixf->ads[PIXEL_8x16] =
1092     pixf->ads[PIXEL_8x4] =
1093     pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
1094     pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
1095 }
1096