2e828121f0d67be17afcd96fa4f7a7395705443a
[x262.git] / common / pixel.c
1 /*****************************************************************************
2  * pixel.c: pixel metrics
3  *****************************************************************************
4  * Copyright (C) 2003-2014 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 #   include "x86/predict.h"
33 #endif
34 #if ARCH_PPC
35 #   include "ppc/pixel.h"
36 #endif
37 #if ARCH_ARM
38 #   include "arm/pixel.h"
39 #endif
40 #if ARCH_UltraSPARC
41 #   include "sparc/pixel.h"
42 #endif
43
44
45 /****************************************************************************
46  * pixel_sad_WxH
47  ****************************************************************************/
48 #define PIXEL_SAD_C( name, lx, ly ) \
49 static int name( pixel *pix1, intptr_t i_stride_pix1,  \
50                  pixel *pix2, intptr_t i_stride_pix2 ) \
51 {                                                   \
52     int i_sum = 0;                                  \
53     for( int y = 0; y < ly; y++ )                   \
54     {                                               \
55         for( int x = 0; x < lx; x++ )               \
56         {                                           \
57             i_sum += abs( pix1[x] - pix2[x] );      \
58         }                                           \
59         pix1 += i_stride_pix1;                      \
60         pix2 += i_stride_pix2;                      \
61     }                                               \
62     return i_sum;                                   \
63 }
64
65
66 PIXEL_SAD_C( x264_pixel_sad_16x16, 16, 16 )
67 PIXEL_SAD_C( x264_pixel_sad_16x8,  16,  8 )
68 PIXEL_SAD_C( x264_pixel_sad_8x16,   8, 16 )
69 PIXEL_SAD_C( x264_pixel_sad_8x8,    8,  8 )
70 PIXEL_SAD_C( x264_pixel_sad_8x4,    8,  4 )
71 PIXEL_SAD_C( x264_pixel_sad_4x16,   4, 16 )
72 PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
73 PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
74
75 /****************************************************************************
76  * pixel_ssd_WxH
77  ****************************************************************************/
78 #define PIXEL_SSD_C( name, lx, ly ) \
79 static int name( pixel *pix1, intptr_t i_stride_pix1,  \
80                  pixel *pix2, intptr_t i_stride_pix2 ) \
81 {                                                   \
82     int i_sum = 0;                                  \
83     for( int y = 0; y < ly; y++ )                   \
84     {                                               \
85         for( int x = 0; x < lx; x++ )               \
86         {                                           \
87             int d = pix1[x] - pix2[x];              \
88             i_sum += d*d;                           \
89         }                                           \
90         pix1 += i_stride_pix1;                      \
91         pix2 += i_stride_pix2;                      \
92     }                                               \
93     return i_sum;                                   \
94 }
95
96 PIXEL_SSD_C( x264_pixel_ssd_16x16, 16, 16 )
97 PIXEL_SSD_C( x264_pixel_ssd_16x8,  16,  8 )
98 PIXEL_SSD_C( x264_pixel_ssd_8x16,   8, 16 )
99 PIXEL_SSD_C( x264_pixel_ssd_8x8,    8,  8 )
100 PIXEL_SSD_C( x264_pixel_ssd_8x4,    8,  4 )
101 PIXEL_SSD_C( x264_pixel_ssd_4x16,   4, 16 )
102 PIXEL_SSD_C( x264_pixel_ssd_4x8,    4,  8 )
103 PIXEL_SSD_C( x264_pixel_ssd_4x4,    4,  4 )
104
105 uint64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1,
106                              pixel *pix2, intptr_t i_pix2, int i_width, int i_height )
107 {
108     uint64_t i_ssd = 0;
109     int y;
110     int align = !(((intptr_t)pix1 | (intptr_t)pix2 | i_pix1 | i_pix2) & 15);
111
112 #define SSD(size) i_ssd += pf->ssd[size]( pix1 + y*i_pix1 + x, i_pix1, \
113                                           pix2 + y*i_pix2 + x, i_pix2 );
114     for( y = 0; y < i_height-15; y += 16 )
115     {
116         int x = 0;
117         if( align )
118             for( ; x < i_width-15; x += 16 )
119                 SSD(PIXEL_16x16);
120         for( ; x < i_width-7; x += 8 )
121             SSD(PIXEL_8x16);
122     }
123     if( y < i_height-7 )
124         for( int x = 0; x < i_width-7; x += 8 )
125             SSD(PIXEL_8x8);
126 #undef SSD
127
128 #define SSD1 { int d = pix1[y*i_pix1+x] - pix2[y*i_pix2+x]; i_ssd += d*d; }
129     if( i_width & 7 )
130     {
131         for( y = 0; y < (i_height & ~7); y++ )
132             for( int x = i_width & ~7; x < i_width; x++ )
133                 SSD1;
134     }
135     if( i_height & 7 )
136     {
137         for( y = i_height & ~7; y < i_height; y++ )
138             for( int x = 0; x < i_width; x++ )
139                 SSD1;
140     }
141 #undef SSD1
142
143     return i_ssd;
144 }
145
146 static void pixel_ssd_nv12_core( pixel *pixuv1, intptr_t stride1, pixel *pixuv2, intptr_t stride2,
147                                  int width, int height, uint64_t *ssd_u, uint64_t *ssd_v )
148 {
149     *ssd_u = 0, *ssd_v = 0;
150     for( int y = 0; y < height; y++, pixuv1+=stride1, pixuv2+=stride2 )
151         for( int x = 0; x < width; x++ )
152         {
153             int du = pixuv1[2*x]   - pixuv2[2*x];
154             int dv = pixuv1[2*x+1] - pixuv2[2*x+1];
155             *ssd_u += du*du;
156             *ssd_v += dv*dv;
157         }
158 }
159
160 void x264_pixel_ssd_nv12( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2,
161                           int i_width, int i_height, uint64_t *ssd_u, uint64_t *ssd_v )
162 {
163     pf->ssd_nv12_core( pix1, i_pix1, pix2, i_pix2, i_width&~7, i_height, ssd_u, ssd_v );
164     if( i_width&7 )
165     {
166         uint64_t tmp[2];
167         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] );
168         *ssd_u += tmp[0];
169         *ssd_v += tmp[1];
170     }
171 }
172
173 /****************************************************************************
174  * pixel_var_wxh
175  ****************************************************************************/
176 #define PIXEL_VAR_C( name, w, h ) \
177 static uint64_t name( pixel *pix, intptr_t i_stride ) \
178 {                                             \
179     uint32_t sum = 0, sqr = 0;                \
180     for( int y = 0; y < h; y++ )              \
181     {                                         \
182         for( int x = 0; x < w; x++ )          \
183         {                                     \
184             sum += pix[x];                    \
185             sqr += pix[x] * pix[x];           \
186         }                                     \
187         pix += i_stride;                      \
188     }                                         \
189     return sum + ((uint64_t)sqr << 32);       \
190 }
191
192 PIXEL_VAR_C( x264_pixel_var_16x16, 16, 16 )
193 PIXEL_VAR_C( x264_pixel_var_8x16,   8, 16 )
194 PIXEL_VAR_C( x264_pixel_var_8x8,    8,  8 )
195
196 /****************************************************************************
197  * pixel_var2_wxh
198  ****************************************************************************/
199 #define PIXEL_VAR2_C( name, w, h, shift ) \
200 static int name( pixel *pix1, intptr_t i_stride1, pixel *pix2, intptr_t i_stride2, int *ssd ) \
201 { \
202     uint32_t var = 0, sum = 0, sqr = 0; \
203     for( int y = 0; y < h; y++ ) \
204     { \
205         for( int x = 0; x < w; x++ ) \
206         { \
207             int diff = pix1[x] - pix2[x]; \
208             sum += diff; \
209             sqr += diff * diff; \
210         } \
211         pix1 += i_stride1; \
212         pix2 += i_stride2; \
213     } \
214     sum = abs(sum); \
215     var = sqr - ((uint64_t)sum * sum >> shift); \
216     *ssd = sqr; \
217     return var; \
218 }
219
220 PIXEL_VAR2_C( x264_pixel_var2_8x16, 8, 16, 7 )
221 PIXEL_VAR2_C( x264_pixel_var2_8x8,  8,  8, 6 )
222
223 #if BIT_DEPTH > 8
224     typedef uint32_t sum_t;
225     typedef uint64_t sum2_t;
226 #else
227     typedef uint16_t sum_t;
228     typedef uint32_t sum2_t;
229 #endif
230 #define BITS_PER_SUM (8 * sizeof(sum_t))
231
232 #define HADAMARD4(d0, d1, d2, d3, s0, s1, s2, s3) {\
233     sum2_t t0 = s0 + s1;\
234     sum2_t t1 = s0 - s1;\
235     sum2_t t2 = s2 + s3;\
236     sum2_t t3 = s2 - s3;\
237     d0 = t0 + t2;\
238     d2 = t0 - t2;\
239     d1 = t1 + t3;\
240     d3 = t1 - t3;\
241 }
242
243 // in: a pseudo-simd number of the form x+(y<<16)
244 // return: abs(x)+(abs(y)<<16)
245 static ALWAYS_INLINE sum2_t abs2( sum2_t a )
246 {
247     sum2_t s = ((a>>(BITS_PER_SUM-1))&(((sum2_t)1<<BITS_PER_SUM)+1))*((sum_t)-1);
248     return (a+s)^s;
249 }
250
251 /****************************************************************************
252  * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
253  ****************************************************************************/
254
255 static NOINLINE int x264_pixel_satd_4x4( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
256 {
257     sum2_t tmp[4][2];
258     sum2_t a0, a1, a2, a3, b0, b1;
259     sum2_t sum = 0;
260     for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
261     {
262         a0 = pix1[0] - pix2[0];
263         a1 = pix1[1] - pix2[1];
264         b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
265         a2 = pix1[2] - pix2[2];
266         a3 = pix1[3] - pix2[3];
267         b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
268         tmp[i][0] = b0 + b1;
269         tmp[i][1] = b0 - b1;
270     }
271     for( int i = 0; i < 2; i++ )
272     {
273         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
274         a0 = abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
275         sum += ((sum_t)a0) + (a0>>BITS_PER_SUM);
276     }
277     return sum >> 1;
278 }
279
280 static NOINLINE int x264_pixel_satd_8x4( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
281 {
282     sum2_t tmp[4][4];
283     sum2_t a0, a1, a2, a3;
284     sum2_t sum = 0;
285     for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
286     {
287         a0 = (pix1[0] - pix2[0]) + ((sum2_t)(pix1[4] - pix2[4]) << BITS_PER_SUM);
288         a1 = (pix1[1] - pix2[1]) + ((sum2_t)(pix1[5] - pix2[5]) << BITS_PER_SUM);
289         a2 = (pix1[2] - pix2[2]) + ((sum2_t)(pix1[6] - pix2[6]) << BITS_PER_SUM);
290         a3 = (pix1[3] - pix2[3]) + ((sum2_t)(pix1[7] - pix2[7]) << BITS_PER_SUM);
291         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
292     }
293     for( int i = 0; i < 4; i++ )
294     {
295         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
296         sum += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
297     }
298     return (((sum_t)sum) + (sum>>BITS_PER_SUM)) >> 1;
299 }
300
301 #define PIXEL_SATD_C( w, h, sub )\
302 static int x264_pixel_satd_##w##x##h( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )\
303 {\
304     int sum = sub( pix1, i_pix1, pix2, i_pix2 )\
305             + sub( pix1+4*i_pix1, i_pix1, pix2+4*i_pix2, i_pix2 );\
306     if( w==16 )\
307         sum+= sub( pix1+8, i_pix1, pix2+8, i_pix2 )\
308             + sub( pix1+8+4*i_pix1, i_pix1, pix2+8+4*i_pix2, i_pix2 );\
309     if( h==16 )\
310         sum+= sub( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )\
311             + sub( pix1+12*i_pix1, i_pix1, pix2+12*i_pix2, i_pix2 );\
312     if( w==16 && h==16 )\
313         sum+= sub( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 )\
314             + sub( pix1+8+12*i_pix1, i_pix1, pix2+8+12*i_pix2, i_pix2 );\
315     return sum;\
316 }
317 PIXEL_SATD_C( 16, 16, x264_pixel_satd_8x4 )
318 PIXEL_SATD_C( 16, 8,  x264_pixel_satd_8x4 )
319 PIXEL_SATD_C( 8,  16, x264_pixel_satd_8x4 )
320 PIXEL_SATD_C( 8,  8,  x264_pixel_satd_8x4 )
321 PIXEL_SATD_C( 4,  16, x264_pixel_satd_4x4 )
322 PIXEL_SATD_C( 4,  8,  x264_pixel_satd_4x4 )
323
324 static NOINLINE int sa8d_8x8( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
325 {
326     sum2_t tmp[8][4];
327     sum2_t a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3;
328     sum2_t sum = 0;
329     for( int i = 0; i < 8; i++, pix1 += i_pix1, pix2 += i_pix2 )
330     {
331         a0 = pix1[0] - pix2[0];
332         a1 = pix1[1] - pix2[1];
333         b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
334         a2 = pix1[2] - pix2[2];
335         a3 = pix1[3] - pix2[3];
336         b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
337         a4 = pix1[4] - pix2[4];
338         a5 = pix1[5] - pix2[5];
339         b2 = (a4+a5) + ((a4-a5)<<BITS_PER_SUM);
340         a6 = pix1[6] - pix2[6];
341         a7 = pix1[7] - pix2[7];
342         b3 = (a6+a7) + ((a6-a7)<<BITS_PER_SUM);
343         HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], b0,b1,b2,b3 );
344     }
345     for( int i = 0; i < 4; i++ )
346     {
347         HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
348         HADAMARD4( a4, a5, a6, a7, tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i] );
349         b0  = abs2(a0+a4) + abs2(a0-a4);
350         b0 += abs2(a1+a5) + abs2(a1-a5);
351         b0 += abs2(a2+a6) + abs2(a2-a6);
352         b0 += abs2(a3+a7) + abs2(a3-a7);
353         sum += (sum_t)b0 + (b0>>BITS_PER_SUM);
354     }
355     return sum;
356 }
357
358 static int x264_pixel_sa8d_8x8( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
359 {
360     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 );
361     return (sum+2)>>2;
362 }
363
364 static int x264_pixel_sa8d_16x16( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
365 {
366     int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 )
367             + sa8d_8x8( pix1+8, i_pix1, pix2+8, i_pix2 )
368             + sa8d_8x8( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )
369             + sa8d_8x8( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 );
370     return (sum+2)>>2;
371 }
372
373 static NOINLINE uint64_t pixel_hadamard_ac( pixel *pix, intptr_t stride )
374 {
375     sum2_t tmp[32];
376     sum2_t a0, a1, a2, a3, dc;
377     sum2_t sum4 = 0, sum8 = 0;
378     for( int i = 0; i < 8; i++, pix+=stride )
379     {
380         sum2_t *t = tmp + (i&3) + (i&4)*4;
381         a0 = (pix[0]+pix[1]) + ((sum2_t)(pix[0]-pix[1])<<BITS_PER_SUM);
382         a1 = (pix[2]+pix[3]) + ((sum2_t)(pix[2]-pix[3])<<BITS_PER_SUM);
383         t[0] = a0 + a1;
384         t[4] = a0 - a1;
385         a2 = (pix[4]+pix[5]) + ((sum2_t)(pix[4]-pix[5])<<BITS_PER_SUM);
386         a3 = (pix[6]+pix[7]) + ((sum2_t)(pix[6]-pix[7])<<BITS_PER_SUM);
387         t[8] = a2 + a3;
388         t[12] = a2 - a3;
389     }
390     for( int i = 0; i < 8; i++ )
391     {
392         HADAMARD4( a0, a1, a2, a3, tmp[i*4+0], tmp[i*4+1], tmp[i*4+2], tmp[i*4+3] );
393         tmp[i*4+0] = a0;
394         tmp[i*4+1] = a1;
395         tmp[i*4+2] = a2;
396         tmp[i*4+3] = a3;
397         sum4 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
398     }
399     for( int i = 0; i < 8; i++ )
400     {
401         HADAMARD4( a0,a1,a2,a3, tmp[i], tmp[8+i], tmp[16+i], tmp[24+i] );
402         sum8 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
403     }
404     dc = (sum_t)(tmp[0] + tmp[8] + tmp[16] + tmp[24]);
405     sum4 = (sum_t)sum4 + (sum4>>BITS_PER_SUM) - dc;
406     sum8 = (sum_t)sum8 + (sum8>>BITS_PER_SUM) - dc;
407     return ((uint64_t)sum8<<32) + sum4;
408 }
409
410 #define HADAMARD_AC(w,h) \
411 static uint64_t x264_pixel_hadamard_ac_##w##x##h( pixel *pix, intptr_t stride )\
412 {\
413     uint64_t sum = pixel_hadamard_ac( pix, stride );\
414     if( w==16 )\
415         sum += pixel_hadamard_ac( pix+8, stride );\
416     if( h==16 )\
417         sum += pixel_hadamard_ac( pix+8*stride, stride );\
418     if( w==16 && h==16 )\
419         sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
420     return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
421 }
422 HADAMARD_AC( 16, 16 )
423 HADAMARD_AC( 16, 8 )
424 HADAMARD_AC( 8, 16 )
425 HADAMARD_AC( 8, 8 )
426
427
428 /****************************************************************************
429  * pixel_sad_x4
430  ****************************************************************************/
431 #define SAD_X( size ) \
432 static void x264_pixel_sad_x3_##size( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
433                                       intptr_t i_stride, int scores[3] )\
434 {\
435     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
436     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
437     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
438 }\
439 static void x264_pixel_sad_x4_##size( pixel *fenc, pixel *pix0, pixel *pix1,pixel *pix2, pixel *pix3,\
440                                       intptr_t i_stride, int scores[4] )\
441 {\
442     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
443     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
444     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
445     scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
446 }
447
448 SAD_X( 16x16 )
449 SAD_X( 16x8 )
450 SAD_X( 8x16 )
451 SAD_X( 8x8 )
452 SAD_X( 8x4 )
453 SAD_X( 4x8 )
454 SAD_X( 4x4 )
455
456 #if !HIGH_BIT_DEPTH
457 #if ARCH_UltraSPARC
458 SAD_X( 16x16_vis )
459 SAD_X( 16x8_vis )
460 SAD_X( 8x16_vis )
461 SAD_X( 8x8_vis )
462 #endif
463 #endif // !HIGH_BIT_DEPTH
464
465 /****************************************************************************
466  * pixel_satd_x4
467  * no faster than single satd, but needed for satd to be a drop-in replacement for sad
468  ****************************************************************************/
469
470 #define SATD_X( size, cpu ) \
471 static void x264_pixel_satd_x3_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
472                                             intptr_t i_stride, int scores[3] )\
473 {\
474     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
475     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
476     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
477 }\
478 static void x264_pixel_satd_x4_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, pixel *pix3,\
479                                             intptr_t i_stride, int scores[4] )\
480 {\
481     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
482     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
483     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
484     scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
485 }
486 #define SATD_X_DECL6( cpu )\
487 SATD_X( 16x16, cpu )\
488 SATD_X( 16x8, cpu )\
489 SATD_X( 8x16, cpu )\
490 SATD_X( 8x8, cpu )\
491 SATD_X( 8x4, cpu )\
492 SATD_X( 4x8, cpu )
493 #define SATD_X_DECL7( cpu )\
494 SATD_X_DECL6( cpu )\
495 SATD_X( 4x4, cpu )
496
497 SATD_X_DECL7()
498 #if HAVE_MMX
499 SATD_X_DECL7( _mmx2 )
500 #if !HIGH_BIT_DEPTH
501 SATD_X_DECL6( _sse2 )
502 SATD_X_DECL7( _ssse3 )
503 SATD_X_DECL6( _ssse3_atom )
504 SATD_X_DECL7( _sse4 )
505 SATD_X_DECL7( _avx )
506 SATD_X_DECL7( _xop )
507 #endif // !HIGH_BIT_DEPTH
508 #endif
509
510 #if !HIGH_BIT_DEPTH
511 #if HAVE_ARMV6
512 SATD_X_DECL7( _neon )
513 #endif
514 #endif // !HIGH_BIT_DEPTH
515
516 #define INTRA_MBCMP_8x8( mbcmp, cpu, cpu2 )\
517 void x264_intra_##mbcmp##_x3_8x8##cpu( pixel *fenc, pixel edge[36], int res[3] )\
518 {\
519     ALIGNED_ARRAY_16( pixel, pix, [8*FDEC_STRIDE] );\
520     x264_predict_8x8_v##cpu2( pix, edge );\
521     res[0] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
522     x264_predict_8x8_h##cpu2( pix, edge );\
523     res[1] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
524     x264_predict_8x8_dc##cpu2( pix, edge );\
525     res[2] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
526 }
527
528 INTRA_MBCMP_8x8( sad,, _c )
529 INTRA_MBCMP_8x8(sa8d,, _c )
530 #if HIGH_BIT_DEPTH && HAVE_MMX
531 #define x264_predict_8x8_v_sse2 x264_predict_8x8_v_sse
532 INTRA_MBCMP_8x8( sad, _mmx2,  _c )
533 INTRA_MBCMP_8x8(sa8d, _sse2,  _sse2 )
534 #endif
535
536 #define INTRA_MBCMP( mbcmp, size, pred1, pred2, pred3, chroma, cpu, cpu2 )\
537 void x264_intra_##mbcmp##_x3_##size##chroma##cpu( pixel *fenc, pixel *fdec, int res[3] )\
538 {\
539     x264_predict_##size##chroma##_##pred1##cpu2( fdec );\
540     res[0] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
541     x264_predict_##size##chroma##_##pred2##cpu2( fdec );\
542     res[1] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
543     x264_predict_##size##chroma##_##pred3##cpu2( fdec );\
544     res[2] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
545 }
546
547 INTRA_MBCMP( sad,  4x4,   v, h, dc,  ,, _c )
548 INTRA_MBCMP(satd,  4x4,   v, h, dc,  ,, _c )
549 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c,, _c )
550 INTRA_MBCMP(satd,  8x8,  dc, h,  v, c,, _c )
551 INTRA_MBCMP( sad,  8x16, dc, h,  v, c,, _c )
552 INTRA_MBCMP(satd,  8x16, dc, h,  v, c,, _c )
553 INTRA_MBCMP( sad, 16x16,  v, h, dc,  ,, _c )
554 INTRA_MBCMP(satd, 16x16,  v, h, dc,  ,, _c )
555
556 #if HAVE_MMX
557 #if HIGH_BIT_DEPTH
558 #define x264_predict_8x8c_v_mmx2 x264_predict_8x8c_v_mmx
559 #define x264_predict_8x16c_v_mmx2 x264_predict_8x16c_v_c
560 #define x264_predict_8x8c_v_sse2 x264_predict_8x8c_v_sse
561 #define x264_predict_8x16c_v_sse2 x264_predict_8x16c_v_sse
562 #define x264_predict_16x16_v_sse2 x264_predict_16x16_v_sse
563 INTRA_MBCMP( sad,  4x4,   v, h, dc,  , _mmx2, _c )
564 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _mmx2, _mmx2 )
565 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
566 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
567 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _mmx2, _mmx2 )
568 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _sse2, _sse2 )
569 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _sse2, _sse2 )
570 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse2, _sse2 )
571 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _sse2, _sse2 )
572 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _ssse3, _sse2 )
573 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _ssse3, _sse2 )
574 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _ssse3, _sse2 )
575 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _ssse3, _sse2 )
576 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse4, _sse2 )
577 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _avx, _sse2 )
578 #else
579 #define x264_predict_8x16c_v_mmx2 x264_predict_8x16c_v_mmx
580 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
581 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
582 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _sse2, _mmx2 )
583 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse2, _mmx2 )
584 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _ssse3, _mmx2 )
585 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse4, _mmx2 )
586 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _avx, _mmx2 )
587 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _xop, _mmx2 )
588 #endif
589 #endif
590
591 // No C implementation of intra_satd_x9. See checkasm for its behavior,
592 // or see x264_mb_analyse_intra for the entirely different algorithm we
593 // use when lacking an asm implementation of it.
594
595
596
597 /****************************************************************************
598  * structural similarity metric
599  ****************************************************************************/
600 static void ssim_4x4x2_core( const pixel *pix1, intptr_t stride1,
601                              const pixel *pix2, intptr_t stride2,
602                              int sums[2][4] )
603 {
604     for( int z = 0; z < 2; z++ )
605     {
606         uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
607         for( int y = 0; y < 4; y++ )
608             for( int x = 0; x < 4; x++ )
609             {
610                 int a = pix1[x+y*stride1];
611                 int b = pix2[x+y*stride2];
612                 s1  += a;
613                 s2  += b;
614                 ss  += a*a;
615                 ss  += b*b;
616                 s12 += a*b;
617             }
618         sums[z][0] = s1;
619         sums[z][1] = s2;
620         sums[z][2] = ss;
621         sums[z][3] = s12;
622         pix1 += 4;
623         pix2 += 4;
624     }
625 }
626
627 static float ssim_end1( int s1, int s2, int ss, int s12 )
628 {
629 /* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
630  * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
631  * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
632 #if BIT_DEPTH > 9
633 #define type float
634     static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
635     static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
636 #else
637 #define type int
638     static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
639     static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
640 #endif
641     type fs1 = s1;
642     type fs2 = s2;
643     type fss = ss;
644     type fs12 = s12;
645     type vars = fss*64 - fs1*fs1 - fs2*fs2;
646     type covar = fs12*64 - fs1*fs2;
647     return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
648          / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
649 #undef type
650 }
651
652 static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
653 {
654     float ssim = 0.0;
655     for( int i = 0; i < width; i++ )
656         ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
657                            sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
658                            sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
659                            sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
660     return ssim;
661 }
662
663 float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
664                            pixel *pix1, intptr_t stride1,
665                            pixel *pix2, intptr_t stride2,
666                            int width, int height, void *buf, int *cnt )
667 {
668     int z = 0;
669     float ssim = 0.0;
670     int (*sum0)[4] = buf;
671     int (*sum1)[4] = sum0 + (width >> 2) + 3;
672     width >>= 2;
673     height >>= 2;
674     for( int y = 1; y < height; y++ )
675     {
676         for( ; z <= y; z++ )
677         {
678             XCHG( void*, sum0, sum1 );
679             for( int x = 0; x < width; x+=2 )
680                 pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
681         }
682         for( int x = 0; x < width-1; x += 4 )
683             ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
684     }
685     *cnt = (height-1) * (width-1);
686     return ssim;
687 }
688
689 static int pixel_vsad( pixel *src, intptr_t stride, int height )
690 {
691     int score = 0;
692     for( int i = 1; i < height; i++, src += stride )
693         for( int j = 0; j < 16; j++ )
694             score += abs(src[j] - src[j+stride]);
695     return score;
696 }
697
698 int x264_field_vsad( x264_t *h, int mb_x, int mb_y )
699 {
700     int score_field, score_frame;
701     int stride = h->fenc->i_stride[0];
702     int mb_stride = h->mb.i_mb_stride;
703     pixel *fenc = h->fenc->plane[0] + 16 * (mb_x + mb_y * stride);
704     int mb_xy = mb_x + mb_y*mb_stride;
705
706     /* We don't want to analyze pixels outside the frame, as it gives inaccurate results. */
707     int mbpair_height = X264_MIN( h->param.i_height - mb_y * 16, 32 );
708     score_frame  = h->pixf.vsad( fenc,          stride, mbpair_height );
709     score_field  = h->pixf.vsad( fenc,        stride*2, mbpair_height >> 1 );
710     score_field += h->pixf.vsad( fenc+stride, stride*2, mbpair_height >> 1 );
711
712     if( mb_x > 0 )
713         score_field += 512 - h->mb.field[mb_xy        -1]*1024;
714     if( mb_y > 0 )
715         score_field += 512 - h->mb.field[mb_xy-mb_stride]*1024;
716
717     return (score_field < score_frame);
718 }
719
720 static int pixel_asd8( pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2, int height )
721 {
722     int sum = 0;
723     for( int y = 0; y < height; y++, pix1 += stride1, pix2 += stride2 )
724         for( int x = 0; x < 8; x++ )
725             sum += pix1[x] - pix2[x];
726     return abs( sum );
727 }
728
729 /****************************************************************************
730  * successive elimination
731  ****************************************************************************/
732 static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
733                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
734 {
735     int nmv = 0;
736     for( int i = 0; i < width; i++, sums++ )
737     {
738         int ads = abs( enc_dc[0] - sums[0] )
739                 + abs( enc_dc[1] - sums[8] )
740                 + abs( enc_dc[2] - sums[delta] )
741                 + abs( enc_dc[3] - sums[delta+8] )
742                 + cost_mvx[i];
743         if( ads < thresh )
744             mvs[nmv++] = i;
745     }
746     return nmv;
747 }
748
749 static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
750                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
751 {
752     int nmv = 0;
753     for( int i = 0; i < width; i++, sums++ )
754     {
755         int ads = abs( enc_dc[0] - sums[0] )
756                 + abs( enc_dc[1] - sums[delta] )
757                 + cost_mvx[i];
758         if( ads < thresh )
759             mvs[nmv++] = i;
760     }
761     return nmv;
762 }
763
764 static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
765                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
766 {
767     int nmv = 0;
768     for( int i = 0; i<width; i++, sums++ )
769     {
770         int ads = abs( enc_dc[0] - sums[0] )
771                 + cost_mvx[i];
772         if( ads < thresh )
773             mvs[nmv++] = i;
774     }
775     return nmv;
776 }
777
778
779 /****************************************************************************
780  * x264_pixel_init:
781  ****************************************************************************/
782 void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
783 {
784     memset( pixf, 0, sizeof(*pixf) );
785
786 #define INIT2_NAME( name1, name2, cpu ) \
787     pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
788     pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
789 #define INIT4_NAME( name1, name2, cpu ) \
790     INIT2_NAME( name1, name2, cpu ) \
791     pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
792     pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
793 #define INIT5_NAME( name1, name2, cpu ) \
794     INIT4_NAME( name1, name2, cpu ) \
795     pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
796 #define INIT6_NAME( name1, name2, cpu ) \
797     INIT5_NAME( name1, name2, cpu ) \
798     pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
799 #define INIT7_NAME( name1, name2, cpu ) \
800     INIT6_NAME( name1, name2, cpu ) \
801     pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
802 #define INIT8_NAME( name1, name2, cpu ) \
803     INIT7_NAME( name1, name2, cpu ) \
804     pixf->name1[PIXEL_4x16]  = x264_pixel_##name2##_4x16##cpu;
805 #define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
806 #define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
807 #define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
808 #define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
809 #define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
810 #define INIT8( name, cpu ) INIT8_NAME( name, name, cpu )
811
812 #define INIT_ADS( cpu ) \
813     pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
814     pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
815     pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;
816
817     INIT8( sad, );
818     INIT8_NAME( sad_aligned, sad, );
819     INIT7( sad_x3, );
820     INIT7( sad_x4, );
821     INIT8( ssd, );
822     INIT8( satd, );
823     INIT7( satd_x3, );
824     INIT7( satd_x4, );
825     INIT4( hadamard_ac, );
826     INIT_ADS( );
827
828     pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16;
829     pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8;
830     pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
831     pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16;
832     pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
833     pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16;
834     pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8;
835
836     pixf->ssd_nv12_core = pixel_ssd_nv12_core;
837     pixf->ssim_4x4x2_core = ssim_4x4x2_core;
838     pixf->ssim_end4 = ssim_end4;
839     pixf->vsad = pixel_vsad;
840     pixf->asd8 = pixel_asd8;
841
842     pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4;
843     pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4;
844     pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8;
845     pixf->intra_sa8d_x3_8x8   = x264_intra_sa8d_x3_8x8;
846     pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c;
847     pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c;
848     pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c;
849     pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c;
850     pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16;
851     pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16;
852
853 #if HIGH_BIT_DEPTH
854 #if HAVE_MMX
855     if( cpu&X264_CPU_MMX2 )
856     {
857         INIT7( sad, _mmx2 );
858         INIT7_NAME( sad_aligned, sad, _mmx2 );
859         INIT7( sad_x3, _mmx2 );
860         INIT7( sad_x4, _mmx2 );
861         INIT8( satd, _mmx2 );
862         INIT7( satd_x3, _mmx2 );
863         INIT7( satd_x4, _mmx2 );
864         INIT4( hadamard_ac, _mmx2 );
865         INIT8( ssd, _mmx2 );
866         INIT_ADS( _mmx2 );
867
868         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_mmx2;
869         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
870         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
871 #if ARCH_X86
872         pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_mmx2;
873         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
874 #endif
875
876         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
877         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
878         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
879         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
880         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
881         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_mmx2;
882         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_mmx2;
883         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
884         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
885     }
886     if( cpu&X264_CPU_SSE2 )
887     {
888         INIT4_NAME( sad_aligned, sad, _sse2_aligned );
889         INIT5( ssd, _sse2 );
890         INIT6( satd, _sse2 );
891         pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_sse2;
892
893         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
894         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
895 #if ARCH_X86_64
896         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
897         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse2;
898 #endif
899         pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_sse2;
900         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_sse2;
901         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
902         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
903         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
904         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_sse2;
905         pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_sse2;
906         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_sse2;
907         pixf->intra_sad_x3_8x8 = x264_intra_sad_x3_8x8_sse2;
908     }
909     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
910     {
911         INIT5( sad, _sse2 );
912         INIT2( sad_x3, _sse2 );
913         INIT2( sad_x4, _sse2 );
914         INIT_ADS( _sse2 );
915
916         if( !(cpu&X264_CPU_STACK_MOD4) )
917         {
918             INIT4( hadamard_ac, _sse2 );
919         }
920         pixf->vsad = x264_pixel_vsad_sse2;
921         pixf->asd8 = x264_pixel_asd8_sse2;
922         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_sse2;
923         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_sse2;
924         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_sse2;
925         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse2;
926         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_sse2;
927     }
928     if( cpu&X264_CPU_SSE2_IS_FAST )
929     {
930         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
931         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
932         pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_sse2;
933         pixf->sad_x3[PIXEL_8x4]  = x264_pixel_sad_x3_8x4_sse2;
934         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
935         pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_sse2;
936         pixf->sad_x4[PIXEL_8x4]  = x264_pixel_sad_x4_8x4_sse2;
937     }
938     if( cpu&X264_CPU_SSSE3 )
939     {
940         INIT4_NAME( sad_aligned, sad, _ssse3_aligned );
941         pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_4x4_ssse3;
942         pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_4x8_ssse3;
943         INIT7( sad, _ssse3 );
944         INIT7( sad_x3, _ssse3 );
945         INIT7( sad_x4, _ssse3 );
946         INIT_ADS( _ssse3 );
947         INIT6( satd, _ssse3 );
948         pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_ssse3;
949
950         if( !(cpu&X264_CPU_STACK_MOD4) )
951         {
952             INIT4( hadamard_ac, _ssse3 );
953         }
954         pixf->vsad = x264_pixel_vsad_ssse3;
955         pixf->asd8 = x264_pixel_asd8_ssse3;
956         pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_ssse3;
957         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
958         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
959 #if ARCH_X86_64
960         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_ssse3;
961 #endif
962         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_ssse3;
963         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_ssse3;
964         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
965         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_ssse3;
966         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_ssse3;
967         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
968     }
969     if( cpu&X264_CPU_SSE4 )
970     {
971         INIT6( satd, _sse4 );
972         pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_sse4;
973         if( !(cpu&X264_CPU_STACK_MOD4) )
974         {
975             INIT4( hadamard_ac, _sse4 );
976         }
977         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
978         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
979 #if ARCH_X86_64
980         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse4;
981 #endif
982         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse4;
983     }
984     if( cpu&X264_CPU_AVX )
985     {
986         INIT5_NAME( sad_aligned, sad, _ssse3 ); /* AVX-capable CPUs doesn't benefit from an aligned version */
987         INIT_ADS( _avx );
988         INIT6( satd, _avx );
989         pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_avx;
990         if( !(cpu&X264_CPU_STACK_MOD4) )
991         {
992             INIT4( hadamard_ac, _avx );
993         }
994         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_avx;
995         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_avx;
996         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx;
997         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx;
998         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_avx;
999         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_avx;
1000         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_avx;
1001         pixf->ssim_end4        = x264_pixel_ssim_end4_avx;
1002 #if ARCH_X86_64
1003         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_avx;
1004 #endif
1005         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_avx;
1006     }
1007     if( cpu&X264_CPU_XOP )
1008     {
1009         pixf->vsad = x264_pixel_vsad_xop;
1010         pixf->asd8 = x264_pixel_asd8_xop;
1011     }
1012     if( cpu&X264_CPU_AVX2 )
1013     {
1014         INIT2( ssd, _avx2 );
1015         INIT2( sad, _avx2 );
1016         INIT2_NAME( sad_aligned, sad, _avx2 );
1017         INIT2( sad_x3, _avx2 );
1018         INIT2( sad_x4, _avx2 );
1019         pixf->vsad = x264_pixel_vsad_avx2;
1020         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_avx2;
1021         pixf->intra_sad_x3_8x8 = x264_intra_sad_x3_8x8_avx2;
1022     }
1023 #endif // HAVE_MMX
1024 #else // !HIGH_BIT_DEPTH
1025 #if HAVE_MMX
1026     if( cpu&X264_CPU_MMX )
1027     {
1028         INIT8( ssd, _mmx );
1029     }
1030
1031     if( cpu&X264_CPU_MMX2 )
1032     {
1033         INIT8( sad, _mmx2 );
1034         INIT8_NAME( sad_aligned, sad, _mmx2 );
1035         INIT7( sad_x3, _mmx2 );
1036         INIT7( sad_x4, _mmx2 );
1037         INIT8( satd, _mmx2 );
1038         INIT7( satd_x3, _mmx2 );
1039         INIT7( satd_x4, _mmx2 );
1040         INIT4( hadamard_ac, _mmx2 );
1041         INIT_ADS( _mmx2 );
1042         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
1043         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_mmx2;
1044         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
1045         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_mmx2;
1046 #if ARCH_X86
1047         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmx2;
1048         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmx2;
1049         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmx2;
1050         pixf->ssim_4x4x2_core = x264_pixel_ssim_4x4x2_core_mmx2;
1051         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_mmx2;
1052         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
1053         pixf->vsad = x264_pixel_vsad_mmx2;
1054
1055         if( cpu&X264_CPU_CACHELINE_32 )
1056         {
1057             INIT5( sad, _cache32_mmx2 );
1058             INIT4( sad_x3, _cache32_mmx2 );
1059             INIT4( sad_x4, _cache32_mmx2 );
1060         }
1061         else if( cpu&X264_CPU_CACHELINE_64 && !(cpu&X264_CPU_SLOW_ATOM) )
1062         {
1063             INIT5( sad, _cache64_mmx2 );
1064             INIT4( sad_x3, _cache64_mmx2 );
1065             INIT4( sad_x4, _cache64_mmx2 );
1066         }
1067 #else
1068         if( cpu&X264_CPU_CACHELINE_64 && !(cpu&X264_CPU_SLOW_ATOM) )
1069         {
1070             pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmx2;
1071             pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmx2;
1072             pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmx2;
1073             pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmx2;
1074             pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmx2;
1075             pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmx2;
1076             pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmx2;
1077         }
1078 #endif
1079         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
1080         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
1081         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_mmx2;
1082         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_mmx2;
1083         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
1084         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
1085         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
1086         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
1087         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
1088     }
1089
1090     if( cpu&X264_CPU_SSE2 )
1091     {
1092         INIT5( ssd, _sse2slow );
1093         INIT2_NAME( sad_aligned, sad, _sse2_aligned );
1094         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
1095         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_sse2;
1096         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
1097         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
1098         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
1099         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
1100 #if ARCH_X86_64
1101         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
1102         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse2;
1103 #endif
1104         pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_sse2;
1105         pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16_sse2;
1106         pixf->vsad = x264_pixel_vsad_sse2;
1107         pixf->asd8 = x264_pixel_asd8_sse2;
1108     }
1109
1110     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
1111     {
1112         INIT2( sad, _sse2 );
1113         INIT2( sad_x3, _sse2 );
1114         INIT2( sad_x4, _sse2 );
1115         INIT6( satd, _sse2 );
1116         pixf->satd[PIXEL_4x16]   = x264_pixel_satd_4x16_sse2;
1117         INIT6( satd_x3, _sse2 );
1118         INIT6( satd_x4, _sse2 );
1119         INIT4( hadamard_ac, _sse2 );
1120         INIT_ADS( _sse2 );
1121         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
1122         pixf->var[PIXEL_8x16] = x264_pixel_var_8x16_sse2;
1123         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
1124         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse2;
1125         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_sse2;
1126         if( cpu&X264_CPU_CACHELINE_64 )
1127         {
1128             INIT2( ssd, _sse2); /* faster for width 16 on p4 */
1129 #if ARCH_X86
1130             INIT2( sad, _cache64_sse2 );
1131             INIT2( sad_x3, _cache64_sse2 );
1132             INIT2( sad_x4, _cache64_sse2 );
1133 #endif
1134            if( cpu&X264_CPU_SSE2_IS_FAST )
1135            {
1136                pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_sse2;
1137                pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_sse2;
1138            }
1139         }
1140     }
1141
1142     if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
1143     {
1144         pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
1145         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
1146         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
1147         pixf->sad_x3[PIXEL_8x8] = x264_pixel_sad_x3_8x8_sse2;
1148         pixf->sad_x3[PIXEL_8x4] = x264_pixel_sad_x3_8x4_sse2;
1149         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
1150         pixf->sad_x4[PIXEL_8x8] = x264_pixel_sad_x4_8x8_sse2;
1151         pixf->sad_x4[PIXEL_8x4] = x264_pixel_sad_x4_8x4_sse2;
1152     }
1153
1154     if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
1155     {
1156         INIT2( sad, _sse3 );
1157         INIT2( sad_x3, _sse3 );
1158         INIT2( sad_x4, _sse3 );
1159     }
1160
1161     if( cpu&X264_CPU_SSSE3 )
1162     {
1163         INIT4( hadamard_ac, _ssse3 );
1164         if( !(cpu&X264_CPU_STACK_MOD4) )
1165         {
1166             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_ssse3;
1167             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_ssse3;
1168             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_ssse3;
1169 #if ARCH_X86_64
1170             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_ssse3;
1171 #endif
1172         }
1173         INIT_ADS( _ssse3 );
1174         if( cpu&X264_CPU_SLOW_ATOM )
1175         {
1176             pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3_atom;
1177             pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3_atom;
1178             INIT6( satd, _ssse3_atom );
1179             pixf->satd[PIXEL_4x16]  = x264_pixel_satd_4x16_ssse3_atom;
1180             INIT6( satd_x3, _ssse3_atom );
1181             INIT6( satd_x4, _ssse3_atom );
1182             INIT4( hadamard_ac, _ssse3_atom );
1183 #if ARCH_X86_64
1184             pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_ssse3_atom;
1185 #endif
1186         }
1187         else
1188         {
1189             INIT8( ssd, _ssse3 );
1190             pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
1191             pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
1192             INIT8( satd, _ssse3 );
1193             INIT7( satd_x3, _ssse3 );
1194             INIT7( satd_x4, _ssse3 );
1195 #if ARCH_X86_64
1196             pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_ssse3;
1197 #endif
1198         }
1199         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
1200         if( !(cpu&X264_CPU_SLOW_PSHUFB) )
1201             pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
1202         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_ssse3;
1203         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
1204         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
1205         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_ssse3;
1206         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_ssse3;
1207         pixf->asd8 = x264_pixel_asd8_ssse3;
1208         if( cpu&X264_CPU_CACHELINE_64 )
1209         {
1210             INIT2( sad, _cache64_ssse3 );
1211             INIT2( sad_x3, _cache64_ssse3 );
1212             INIT2( sad_x4, _cache64_ssse3 );
1213         }
1214         else
1215         {
1216             INIT2( sad_x3, _ssse3 );
1217             INIT5( sad_x4, _ssse3 );
1218         }
1219         if( (cpu&X264_CPU_SLOW_ATOM) || (cpu&X264_CPU_SLOW_SHUFFLE) )
1220         {
1221             INIT5( ssd, _sse2 ); /* on conroe, sse2 is faster for width8/16 */
1222         }
1223     }
1224
1225     if( cpu&X264_CPU_SSE4 )
1226     {
1227         INIT8( satd, _sse4 );
1228         INIT7( satd_x3, _sse4 );
1229         INIT7( satd_x4, _sse4 );
1230         INIT4( hadamard_ac, _sse4 );
1231         if( !(cpu&X264_CPU_STACK_MOD4) )
1232         {
1233             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_sse4;
1234             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_sse4;
1235             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_sse4;
1236 #if ARCH_X86_64
1237             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_sse4;
1238 #endif
1239         }
1240         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
1241         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
1242         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse4;
1243 #if ARCH_X86_64
1244         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse4;
1245 #endif
1246     }
1247
1248     if( cpu&X264_CPU_AVX )
1249     {
1250         INIT2_NAME( sad_aligned, sad, _sse2 ); /* AVX-capable CPUs doesn't benefit from an aligned version */
1251         INIT2( sad_x3, _avx );
1252         INIT2( sad_x4, _avx );
1253         INIT8( satd, _avx );
1254         INIT7( satd_x3, _avx );
1255         INIT7( satd_x4, _avx );
1256         INIT_ADS( _avx );
1257         INIT4( hadamard_ac, _avx );
1258         if( !(cpu&X264_CPU_STACK_MOD4) )
1259         {
1260             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_avx;
1261             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_avx;
1262             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_avx;
1263 #if ARCH_X86_64
1264             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_avx;
1265 #endif
1266         }
1267         INIT5( ssd, _avx );
1268         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_avx;
1269         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx;
1270         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_avx;
1271         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_avx;
1272         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx;
1273         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_avx;
1274         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_avx;
1275         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_avx;
1276         pixf->ssim_end4        = x264_pixel_ssim_end4_avx;
1277 #if ARCH_X86_64
1278         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_avx;
1279 #endif
1280     }
1281
1282     if( cpu&X264_CPU_XOP )
1283     {
1284         INIT7( satd, _xop );
1285         INIT7( satd_x3, _xop );
1286         INIT7( satd_x4, _xop );
1287         INIT4( hadamard_ac, _xop );
1288         if( !(cpu&X264_CPU_STACK_MOD4) )
1289         {
1290             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_xop;
1291         }
1292         INIT5( ssd, _xop );
1293         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_xop;
1294         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_xop;
1295         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_xop;
1296         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_xop;
1297         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_xop;
1298         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_xop;
1299         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_xop;
1300         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_xop;
1301 #if ARCH_X86_64
1302         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_xop;
1303 #endif
1304     }
1305
1306     if( cpu&X264_CPU_AVX2 )
1307     {
1308         INIT2( ssd, _avx2 );
1309         INIT2( sad_x3, _avx2 );
1310         INIT2( sad_x4, _avx2 );
1311         INIT4( satd, _avx2 );
1312         INIT2( hadamard_ac, _avx2 );
1313         INIT_ADS( _avx2 );
1314         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx2;
1315         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx2;
1316         pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16_avx2;
1317         pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_avx2;
1318         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_avx2;
1319         pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_avx2;
1320         pixf->intra_sad_x3_8x8c = x264_intra_sad_x3_8x8c_avx2;
1321         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_avx2;
1322 #if ARCH_X86_64
1323         pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_avx2;
1324 #endif
1325     }
1326 #endif //HAVE_MMX
1327
1328 #if HAVE_ARMV6
1329     if( cpu&X264_CPU_ARMV6 )
1330     {
1331         pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1332         pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1333         pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1334         pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1335     }
1336     if( cpu&X264_CPU_NEON )
1337     {
1338         INIT5( sad, _neon );
1339         INIT5( sad_aligned, _neon );
1340         INIT7( sad_x3, _neon );
1341         INIT7( sad_x4, _neon );
1342         INIT7( ssd, _neon );
1343         INIT7( satd, _neon );
1344         INIT7( satd_x3, _neon );
1345         INIT7( satd_x4, _neon );
1346         INIT4( hadamard_ac, _neon );
1347         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_neon;
1348         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_neon;
1349         pixf->var[PIXEL_8x8]    = x264_pixel_var_8x8_neon;
1350         pixf->var[PIXEL_16x16]  = x264_pixel_var_16x16_neon;
1351         pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_neon;
1352
1353         pixf->ssim_4x4x2_core   = x264_pixel_ssim_4x4x2_core_neon;
1354         pixf->ssim_end4         = x264_pixel_ssim_end4_neon;
1355
1356         if( cpu&X264_CPU_FAST_NEON_MRC )
1357         {
1358             pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_neon;
1359             pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_neon;
1360             pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_aligned_4x8_neon;
1361             pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_aligned_4x4_neon;
1362         }
1363         else    // really just scheduled for dual issue / A8
1364         {
1365             INIT5( sad_aligned, _neon_dual );
1366         }
1367     }
1368 #endif
1369 #endif // HIGH_BIT_DEPTH
1370 #if HAVE_ALTIVEC
1371     if( cpu&X264_CPU_ALTIVEC )
1372     {
1373         x264_pixel_altivec_init( pixf );
1374     }
1375 #endif
1376 #if !HIGH_BIT_DEPTH
1377 #if ARCH_UltraSPARC
1378     INIT4( sad, _vis );
1379     INIT4( sad_x3, _vis );
1380     INIT4( sad_x4, _vis );
1381 #endif
1382 #endif // !HIGH_BIT_DEPTH
1383
1384     pixf->ads[PIXEL_8x16] =
1385     pixf->ads[PIXEL_8x4] =
1386     pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
1387     pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
1388 }
1389