64-bit cabac optimizations
[x262.git] / common / pixel.c
1 /*****************************************************************************
2  * pixel.c: pixel metrics
3  *****************************************************************************
4  * Copyright (C) 2003-2013 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
374 static NOINLINE uint64_t pixel_hadamard_ac( pixel *pix, intptr_t stride )
375 {
376     sum2_t tmp[32];
377     sum2_t a0, a1, a2, a3, dc;
378     sum2_t sum4 = 0, sum8 = 0;
379     for( int i = 0; i < 8; i++, pix+=stride )
380     {
381         sum2_t *t = tmp + (i&3) + (i&4)*4;
382         a0 = (pix[0]+pix[1]) + ((sum2_t)(pix[0]-pix[1])<<BITS_PER_SUM);
383         a1 = (pix[2]+pix[3]) + ((sum2_t)(pix[2]-pix[3])<<BITS_PER_SUM);
384         t[0] = a0 + a1;
385         t[4] = a0 - a1;
386         a2 = (pix[4]+pix[5]) + ((sum2_t)(pix[4]-pix[5])<<BITS_PER_SUM);
387         a3 = (pix[6]+pix[7]) + ((sum2_t)(pix[6]-pix[7])<<BITS_PER_SUM);
388         t[8] = a2 + a3;
389         t[12] = a2 - a3;
390     }
391     for( int i = 0; i < 8; i++ )
392     {
393         HADAMARD4( a0, a1, a2, a3, tmp[i*4+0], tmp[i*4+1], tmp[i*4+2], tmp[i*4+3] );
394         tmp[i*4+0] = a0;
395         tmp[i*4+1] = a1;
396         tmp[i*4+2] = a2;
397         tmp[i*4+3] = a3;
398         sum4 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
399     }
400     for( int i = 0; i < 8; i++ )
401     {
402         HADAMARD4( a0,a1,a2,a3, tmp[i], tmp[8+i], tmp[16+i], tmp[24+i] );
403         sum8 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
404     }
405     dc = (sum_t)(tmp[0] + tmp[8] + tmp[16] + tmp[24]);
406     sum4 = (sum_t)sum4 + (sum4>>BITS_PER_SUM) - dc;
407     sum8 = (sum_t)sum8 + (sum8>>BITS_PER_SUM) - dc;
408     return ((uint64_t)sum8<<32) + sum4;
409 }
410
411 #define HADAMARD_AC(w,h) \
412 static uint64_t x264_pixel_hadamard_ac_##w##x##h( pixel *pix, intptr_t stride )\
413 {\
414     uint64_t sum = pixel_hadamard_ac( pix, stride );\
415     if( w==16 )\
416         sum += pixel_hadamard_ac( pix+8, stride );\
417     if( h==16 )\
418         sum += pixel_hadamard_ac( pix+8*stride, stride );\
419     if( w==16 && h==16 )\
420         sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
421     return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
422 }
423 HADAMARD_AC( 16, 16 )
424 HADAMARD_AC( 16, 8 )
425 HADAMARD_AC( 8, 16 )
426 HADAMARD_AC( 8, 8 )
427
428
429 /****************************************************************************
430  * pixel_sad_x4
431  ****************************************************************************/
432 #define SAD_X( size ) \
433 static void x264_pixel_sad_x3_##size( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
434                                       intptr_t i_stride, int scores[3] )\
435 {\
436     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
437     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
438     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
439 }\
440 static void x264_pixel_sad_x4_##size( pixel *fenc, pixel *pix0, pixel *pix1,pixel *pix2, pixel *pix3,\
441                                       intptr_t i_stride, int scores[4] )\
442 {\
443     scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
444     scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
445     scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
446     scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
447 }
448
449 SAD_X( 16x16 )
450 SAD_X( 16x8 )
451 SAD_X( 8x16 )
452 SAD_X( 8x8 )
453 SAD_X( 8x4 )
454 SAD_X( 4x8 )
455 SAD_X( 4x4 )
456
457 #if !HIGH_BIT_DEPTH
458 #if ARCH_UltraSPARC
459 SAD_X( 16x16_vis )
460 SAD_X( 16x8_vis )
461 SAD_X( 8x16_vis )
462 SAD_X( 8x8_vis )
463 #endif
464 #endif // !HIGH_BIT_DEPTH
465
466 /****************************************************************************
467  * pixel_satd_x4
468  * no faster than single satd, but needed for satd to be a drop-in replacement for sad
469  ****************************************************************************/
470
471 #define SATD_X( size, cpu ) \
472 static void x264_pixel_satd_x3_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
473                                             intptr_t i_stride, int scores[3] )\
474 {\
475     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
476     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
477     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
478 }\
479 static void x264_pixel_satd_x4_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, pixel *pix3,\
480                                             intptr_t i_stride, int scores[4] )\
481 {\
482     scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
483     scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
484     scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
485     scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
486 }
487 #define SATD_X_DECL6( cpu )\
488 SATD_X( 16x16, cpu )\
489 SATD_X( 16x8, cpu )\
490 SATD_X( 8x16, cpu )\
491 SATD_X( 8x8, cpu )\
492 SATD_X( 8x4, cpu )\
493 SATD_X( 4x8, cpu )
494 #define SATD_X_DECL7( cpu )\
495 SATD_X_DECL6( cpu )\
496 SATD_X( 4x4, cpu )
497
498 SATD_X_DECL7()
499 #if HAVE_MMX
500 SATD_X_DECL7( _mmx2 )
501 #if !HIGH_BIT_DEPTH
502 SATD_X_DECL6( _sse2 )
503 SATD_X_DECL7( _ssse3 )
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 INTRA_MBCMP_8x8( sad, _mmx2,  _c )
532 INTRA_MBCMP_8x8(sa8d, _sse2,  _sse2 )
533 #endif
534
535 #define INTRA_MBCMP( mbcmp, size, pred1, pred2, pred3, chroma, cpu, cpu2 )\
536 void x264_intra_##mbcmp##_x3_##size##chroma##cpu( pixel *fenc, pixel *fdec, int res[3] )\
537 {\
538     x264_predict_##size##chroma##_##pred1##cpu2( fdec );\
539     res[0] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
540     x264_predict_##size##chroma##_##pred2##cpu2( fdec );\
541     res[1] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
542     x264_predict_##size##chroma##_##pred3##cpu2( fdec );\
543     res[2] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
544 }
545
546 INTRA_MBCMP( sad,  4x4,   v, h, dc,  ,, _c )
547 INTRA_MBCMP(satd,  4x4,   v, h, dc,  ,, _c )
548 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c,, _c )
549 INTRA_MBCMP(satd,  8x8,  dc, h,  v, c,, _c )
550 INTRA_MBCMP( sad,  8x16, dc, h,  v, c,, _c )
551 INTRA_MBCMP(satd,  8x16, dc, h,  v, c,, _c )
552 INTRA_MBCMP( sad, 16x16,  v, h, dc,  ,, _c )
553 INTRA_MBCMP(satd, 16x16,  v, h, dc,  ,, _c )
554
555 #if HAVE_MMX
556 #if HIGH_BIT_DEPTH
557 INTRA_MBCMP( sad,  4x4,   v, h, dc,  , _mmx2, _c )
558 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _mmx2, _c )
559 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _mmx2, _mmx2 )
560 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _sse2, _sse2 )
561 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _sse2, _sse2 )
562 INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _ssse3, _sse2 )
563 INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _ssse3, _sse2 )
564 #else
565 #define x264_predict_8x16c_v_mmx2 x264_predict_8x16c_v_mmx
566 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
567 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
568 INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _sse2, _mmx2 )
569 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse2, _mmx2 )
570 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _ssse3, _mmx2 )
571 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse4, _mmx2 )
572 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _avx, _mmx2 )
573 INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _xop, _mmx2 )
574 #endif
575 #endif
576
577 // No C implementation of intra_satd_x9. See checkasm for its behavior,
578 // or see x264_mb_analyse_intra for the entirely different algorithm we
579 // use when lacking an asm implementation of it.
580
581
582
583 /****************************************************************************
584  * structural similarity metric
585  ****************************************************************************/
586 static void ssim_4x4x2_core( const pixel *pix1, intptr_t stride1,
587                              const pixel *pix2, intptr_t stride2,
588                              int sums[2][4] )
589 {
590     for( int z = 0; z < 2; z++ )
591     {
592         uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
593         for( int y = 0; y < 4; y++ )
594             for( int x = 0; x < 4; x++ )
595             {
596                 int a = pix1[x+y*stride1];
597                 int b = pix2[x+y*stride2];
598                 s1  += a;
599                 s2  += b;
600                 ss  += a*a;
601                 ss  += b*b;
602                 s12 += a*b;
603             }
604         sums[z][0] = s1;
605         sums[z][1] = s2;
606         sums[z][2] = ss;
607         sums[z][3] = s12;
608         pix1 += 4;
609         pix2 += 4;
610     }
611 }
612
613 static float ssim_end1( int s1, int s2, int ss, int s12 )
614 {
615 /* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
616  * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
617  * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
618 #if BIT_DEPTH > 9
619 #define type float
620     static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
621     static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
622 #else
623 #define type int
624     static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
625     static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
626 #endif
627     type fs1 = s1;
628     type fs2 = s2;
629     type fss = ss;
630     type fs12 = s12;
631     type vars = fss*64 - fs1*fs1 - fs2*fs2;
632     type covar = fs12*64 - fs1*fs2;
633     return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
634          / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
635 #undef type
636 }
637
638 static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
639 {
640     float ssim = 0.0;
641     for( int i = 0; i < width; i++ )
642         ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
643                            sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
644                            sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
645                            sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
646     return ssim;
647 }
648
649 float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
650                            pixel *pix1, intptr_t stride1,
651                            pixel *pix2, intptr_t stride2,
652                            int width, int height, void *buf, int *cnt )
653 {
654     int z = 0;
655     float ssim = 0.0;
656     int (*sum0)[4] = buf;
657     int (*sum1)[4] = sum0 + (width >> 2) + 3;
658     width >>= 2;
659     height >>= 2;
660     for( int y = 1; y < height; y++ )
661     {
662         for( ; z <= y; z++ )
663         {
664             XCHG( void*, sum0, sum1 );
665             for( int x = 0; x < width; x+=2 )
666                 pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
667         }
668         for( int x = 0; x < width-1; x += 4 )
669             ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
670     }
671     *cnt = (height-1) * (width-1);
672     return ssim;
673 }
674
675 static int pixel_vsad( pixel *src, intptr_t stride, int height )
676 {
677     int score = 0;
678     for( int i = 1; i < height; i++, src += stride )
679         for( int j = 0; j < 16; j++ )
680             score += abs(src[j] - src[j+stride]);
681     return score;
682 }
683
684 int x264_field_vsad( x264_t *h, int mb_x, int mb_y )
685 {
686     int score_field, score_frame;
687     int stride = h->fenc->i_stride[0];
688     int mb_stride = h->mb.i_mb_stride;
689     pixel *fenc = h->fenc->plane[0] + 16 * (mb_x + mb_y * stride);
690     int mb_xy = mb_x + mb_y*mb_stride;
691
692     /* We don't want to analyze pixels outside the frame, as it gives inaccurate results. */
693     int mbpair_height = X264_MIN( h->param.i_height - mb_y * 16, 32 );
694     score_frame  = h->pixf.vsad( fenc,          stride, mbpair_height );
695     score_field  = h->pixf.vsad( fenc,        stride*2, mbpair_height >> 1 );
696     score_field += h->pixf.vsad( fenc+stride, stride*2, mbpair_height >> 1 );
697
698     if( mb_x > 0 )
699         score_field += 512 - h->mb.field[mb_xy        -1]*1024;
700     if( mb_y > 0 )
701         score_field += 512 - h->mb.field[mb_xy-mb_stride]*1024;
702
703     return (score_field < score_frame);
704 }
705
706 static int pixel_asd8( pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2, int height )
707 {
708     int sum = 0;
709     for( int y = 0; y < height; y++, pix1 += stride1, pix2 += stride2 )
710         for( int x = 0; x < 8; x++ )
711             sum += pix1[x] - pix2[x];
712     return abs( sum );
713 }
714
715 /****************************************************************************
716  * successive elimination
717  ****************************************************************************/
718 static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
719                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
720 {
721     int nmv = 0;
722     for( int i = 0; i < width; i++, sums++ )
723     {
724         int ads = abs( enc_dc[0] - sums[0] )
725                 + abs( enc_dc[1] - sums[8] )
726                 + abs( enc_dc[2] - sums[delta] )
727                 + abs( enc_dc[3] - sums[delta+8] )
728                 + cost_mvx[i];
729         if( ads < thresh )
730             mvs[nmv++] = i;
731     }
732     return nmv;
733 }
734
735 static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
736                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
737 {
738     int nmv = 0;
739     for( int i = 0; i < width; i++, sums++ )
740     {
741         int ads = abs( enc_dc[0] - sums[0] )
742                 + abs( enc_dc[1] - sums[delta] )
743                 + cost_mvx[i];
744         if( ads < thresh )
745             mvs[nmv++] = i;
746     }
747     return nmv;
748 }
749
750 static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
751                             uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
752 {
753     int nmv = 0;
754     for( int i = 0; i<width; i++, sums++ )
755     {
756         int ads = abs( enc_dc[0] - sums[0] )
757                 + cost_mvx[i];
758         if( ads < thresh )
759             mvs[nmv++] = i;
760     }
761     return nmv;
762 }
763
764
765 /****************************************************************************
766  * x264_pixel_init:
767  ****************************************************************************/
768 void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
769 {
770     memset( pixf, 0, sizeof(*pixf) );
771
772 #define INIT2_NAME( name1, name2, cpu ) \
773     pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
774     pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
775 #define INIT4_NAME( name1, name2, cpu ) \
776     INIT2_NAME( name1, name2, cpu ) \
777     pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
778     pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
779 #define INIT5_NAME( name1, name2, cpu ) \
780     INIT4_NAME( name1, name2, cpu ) \
781     pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
782 #define INIT6_NAME( name1, name2, cpu ) \
783     INIT5_NAME( name1, name2, cpu ) \
784     pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
785 #define INIT7_NAME( name1, name2, cpu ) \
786     INIT6_NAME( name1, name2, cpu ) \
787     pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
788 #define INIT8_NAME( name1, name2, cpu ) \
789     INIT7_NAME( name1, name2, cpu ) \
790     pixf->name1[PIXEL_4x16]  = x264_pixel_##name2##_4x16##cpu;
791 #define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
792 #define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
793 #define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
794 #define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
795 #define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
796 #define INIT8( name, cpu ) INIT8_NAME( name, name, cpu )
797
798 #define INIT_ADS( cpu ) \
799     pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
800     pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
801     pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;
802
803     INIT8( sad, );
804     INIT8_NAME( sad_aligned, sad, );
805     INIT7( sad_x3, );
806     INIT7( sad_x4, );
807     INIT8( ssd, );
808     INIT8( satd, );
809     INIT7( satd_x3, );
810     INIT7( satd_x4, );
811     INIT4( hadamard_ac, );
812     INIT_ADS( );
813
814     pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16;
815     pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8;
816     pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
817     pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16;
818     pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
819     pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16;
820     pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8;
821
822     pixf->ssd_nv12_core = pixel_ssd_nv12_core;
823     pixf->ssim_4x4x2_core = ssim_4x4x2_core;
824     pixf->ssim_end4 = ssim_end4;
825     pixf->vsad = pixel_vsad;
826     pixf->asd8 = pixel_asd8;
827
828     pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4;
829     pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4;
830     pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8;
831     pixf->intra_sa8d_x3_8x8   = x264_intra_sa8d_x3_8x8;
832     pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c;
833     pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c;
834     pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c;
835     pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c;
836     pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16;
837     pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16;
838
839 #if HIGH_BIT_DEPTH
840 #if HAVE_MMX
841     if( cpu&X264_CPU_MMX2 )
842     {
843         INIT7( sad, _mmx2 );
844         INIT7( sad_x3, _mmx2 );
845         INIT7( sad_x4, _mmx2 );
846         INIT8( satd, _mmx2 );
847         INIT7( satd_x3, _mmx2 );
848         INIT7( satd_x4, _mmx2 );
849         INIT4( hadamard_ac, _mmx2 );
850         INIT8( ssd, _mmx2 );
851         INIT_ADS( _mmx2 );
852
853         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_mmx2;
854         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
855         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
856 #if ARCH_X86
857         pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_mmx2;
858         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
859 #endif
860
861         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
862         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
863         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
864         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
865         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
866         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
867         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
868     }
869     if( cpu&X264_CPU_SSE2 )
870     {
871         INIT4_NAME( sad_aligned, sad, _sse2_aligned );
872         INIT5( ssd, _sse2 );
873
874         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
875         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
876 #if ARCH_X86_64
877         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
878 #endif
879         pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_sse2;
880         pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_sse2;
881         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
882         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
883         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
884         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_sse2;
885         pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_sse2;
886         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_sse2;
887         pixf->intra_sad_x3_8x8 = x264_intra_sad_x3_8x8_sse2;
888     }
889     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
890     {
891         INIT5( sad, _sse2 );
892         INIT2( sad_x3, _sse2 );
893         INIT2( sad_x4, _sse2 );
894         INIT_ADS( _sse2 );
895
896         if( !(cpu&X264_CPU_STACK_MOD4) )
897         {
898             INIT4( hadamard_ac, _sse2 );
899         }
900         pixf->vsad = x264_pixel_vsad_sse2;
901         pixf->asd8 = x264_pixel_asd8_sse2;
902         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_sse2;
903         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_sse2;
904         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_sse2;
905     }
906     if( cpu&X264_CPU_SSE2_IS_FAST )
907     {
908         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
909         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
910         pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_sse2;
911         pixf->sad_x3[PIXEL_8x4]  = x264_pixel_sad_x3_8x4_sse2;
912         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
913         pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_sse2;
914         pixf->sad_x4[PIXEL_8x4]  = x264_pixel_sad_x4_8x4_sse2;
915     }
916     if( cpu&X264_CPU_SSSE3 )
917     {
918         INIT4_NAME( sad_aligned, sad, _ssse3_aligned );
919         INIT7( sad, _ssse3 );
920         INIT7( sad_x3, _ssse3 );
921         INIT7( sad_x4, _ssse3 );
922         INIT_ADS( _ssse3 );
923
924         if( !(cpu&X264_CPU_STACK_MOD4) )
925         {
926             INIT4( hadamard_ac, _ssse3 );
927         }
928         pixf->vsad = x264_pixel_vsad_ssse3;
929         pixf->asd8 = x264_pixel_asd8_ssse3;
930         pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_ssse3;
931         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
932         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
933         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_ssse3;
934         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_ssse3;
935         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
936         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
937     }
938     if( cpu&X264_CPU_SSE4 )
939     {
940         if( !(cpu&X264_CPU_STACK_MOD4) )
941         {
942             INIT4( hadamard_ac, _sse4 );
943         }
944         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
945         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
946     }
947     if( cpu&X264_CPU_AVX )
948     {
949         INIT_ADS( _avx );
950         if( !(cpu&X264_CPU_STACK_MOD4) )
951         {
952             INIT4( hadamard_ac, _avx );
953         }
954         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_avx;
955         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_avx;
956         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx;
957         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx;
958         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_avx;
959         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_avx;
960         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_avx;
961         pixf->ssim_end4        = x264_pixel_ssim_end4_avx;
962     }
963     if( cpu&X264_CPU_XOP )
964     {
965         pixf->vsad = x264_pixel_vsad_xop;
966         pixf->asd8 = x264_pixel_asd8_xop;
967     }
968 #endif // HAVE_MMX
969 #else // !HIGH_BIT_DEPTH
970 #if HAVE_MMX
971     if( cpu&X264_CPU_MMX )
972     {
973         INIT8( ssd, _mmx );
974     }
975
976     if( cpu&X264_CPU_MMX2 )
977     {
978         INIT8( sad, _mmx2 );
979         INIT8_NAME( sad_aligned, sad, _mmx2 );
980         INIT7( sad_x3, _mmx2 );
981         INIT7( sad_x4, _mmx2 );
982         INIT8( satd, _mmx2 );
983         INIT7( satd_x3, _mmx2 );
984         INIT7( satd_x4, _mmx2 );
985         INIT4( hadamard_ac, _mmx2 );
986         INIT_ADS( _mmx2 );
987         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
988         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_mmx2;
989         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
990         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_mmx2;
991 #if ARCH_X86
992         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmx2;
993         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmx2;
994         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmx2;
995         pixf->ssim_4x4x2_core = x264_pixel_ssim_4x4x2_core_mmx2;
996         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_mmx2;
997         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
998         pixf->vsad = x264_pixel_vsad_mmx2;
999
1000         if( cpu&X264_CPU_CACHELINE_32 )
1001         {
1002             INIT5( sad, _cache32_mmx2 );
1003             INIT4( sad_x3, _cache32_mmx2 );
1004             INIT4( sad_x4, _cache32_mmx2 );
1005         }
1006         else if( cpu&X264_CPU_CACHELINE_64 )
1007         {
1008             INIT5( sad, _cache64_mmx2 );
1009             INIT4( sad_x3, _cache64_mmx2 );
1010             INIT4( sad_x4, _cache64_mmx2 );
1011         }
1012 #else
1013         if( cpu&X264_CPU_CACHELINE_64 )
1014         {
1015             pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmx2;
1016             pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmx2;
1017             pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmx2;
1018             pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmx2;
1019             pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmx2;
1020             pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmx2;
1021             pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmx2;
1022         }
1023 #endif
1024         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
1025         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
1026         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_mmx2;
1027         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_mmx2;
1028         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
1029         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
1030         pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
1031         pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
1032         pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
1033     }
1034
1035     if( cpu&X264_CPU_SSE2 )
1036     {
1037         INIT5( ssd, _sse2slow );
1038         INIT2_NAME( sad_aligned, sad, _sse2_aligned );
1039         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
1040         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_sse2;
1041         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
1042         pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
1043         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
1044         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
1045 #if ARCH_X86_64
1046         pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
1047 #endif
1048         pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_sse2;
1049         pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16_sse2;
1050         pixf->vsad = x264_pixel_vsad_sse2;
1051         pixf->asd8 = x264_pixel_asd8_sse2;
1052     }
1053
1054     if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
1055     {
1056         INIT2( sad, _sse2 );
1057         INIT2( sad_x3, _sse2 );
1058         INIT2( sad_x4, _sse2 );
1059         INIT6( satd, _sse2 );
1060         pixf->satd[PIXEL_4x16]   = x264_pixel_satd_4x16_sse2;
1061         INIT6( satd_x3, _sse2 );
1062         INIT6( satd_x4, _sse2 );
1063         if( !(cpu&X264_CPU_STACK_MOD4) )
1064         {
1065             INIT4( hadamard_ac, _sse2 );
1066         }
1067         INIT_ADS( _sse2 );
1068         pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
1069         pixf->var[PIXEL_8x16] = x264_pixel_var_8x16_sse2;
1070         pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
1071         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse2;
1072         pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_sse2;
1073         if( cpu&X264_CPU_CACHELINE_64 )
1074         {
1075             INIT2( ssd, _sse2); /* faster for width 16 on p4 */
1076 #if ARCH_X86
1077             INIT2( sad, _cache64_sse2 );
1078             INIT2( sad_x3, _cache64_sse2 );
1079             INIT2( sad_x4, _cache64_sse2 );
1080 #endif
1081            if( cpu&X264_CPU_SSE2_IS_FAST )
1082            {
1083                pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_sse2;
1084                pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_sse2;
1085            }
1086         }
1087
1088         if( cpu&X264_CPU_SSE_MISALIGN )
1089         {
1090             INIT2( sad_x3, _sse2_misalign );
1091             INIT2( sad_x4, _sse2_misalign );
1092         }
1093     }
1094
1095     if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
1096     {
1097         pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
1098         pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
1099         pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
1100         pixf->sad_x3[PIXEL_8x8] = x264_pixel_sad_x3_8x8_sse2;
1101         pixf->sad_x3[PIXEL_8x4] = x264_pixel_sad_x3_8x4_sse2;
1102         pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
1103         pixf->sad_x4[PIXEL_8x8] = x264_pixel_sad_x4_8x8_sse2;
1104         pixf->sad_x4[PIXEL_8x4] = x264_pixel_sad_x4_8x4_sse2;
1105     }
1106
1107     if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
1108     {
1109         INIT2( sad, _sse3 );
1110         INIT2( sad_x3, _sse3 );
1111         INIT2( sad_x4, _sse3 );
1112     }
1113
1114     if( cpu&X264_CPU_SSSE3 )
1115     {
1116         if( !(cpu&X264_CPU_STACK_MOD4) )
1117         {
1118             INIT4( hadamard_ac, _ssse3 );
1119             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_ssse3;
1120             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_ssse3;
1121             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_ssse3;
1122 #if ARCH_X86_64
1123             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_ssse3;
1124 #endif
1125         }
1126         INIT_ADS( _ssse3 );
1127         if( !(cpu&X264_CPU_SLOW_ATOM) )
1128         {
1129             INIT8( ssd, _ssse3 );
1130             pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
1131             pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
1132             INIT8( satd, _ssse3 );
1133             INIT7( satd_x3, _ssse3 );
1134             INIT7( satd_x4, _ssse3 );
1135         }
1136         pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
1137         pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
1138         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_ssse3;
1139         pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
1140         pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
1141         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_ssse3;
1142         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_ssse3;
1143         pixf->asd8 = x264_pixel_asd8_ssse3;
1144         if( cpu&X264_CPU_CACHELINE_64 )
1145         {
1146             INIT2( sad, _cache64_ssse3 );
1147             INIT2( sad_x3, _cache64_ssse3 );
1148             INIT2( sad_x4, _cache64_ssse3 );
1149         }
1150         if( cpu&X264_CPU_SLOW_ATOM || !(cpu&X264_CPU_SHUFFLE_IS_FAST) )
1151         {
1152             INIT5( ssd, _sse2 ); /* on conroe, sse2 is faster for width8/16 */
1153         }
1154     }
1155
1156     if( cpu&X264_CPU_SSE4 )
1157     {
1158         INIT8( satd, _sse4 );
1159         INIT7( satd_x3, _sse4 );
1160         INIT7( satd_x4, _sse4 );
1161         if( !(cpu&X264_CPU_STACK_MOD4) )
1162         {
1163             INIT4( hadamard_ac, _sse4 );
1164             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_sse4;
1165             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_sse4;
1166             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_sse4;
1167 #if ARCH_X86_64
1168             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_sse4;
1169 #endif
1170         }
1171         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
1172         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
1173         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse4;
1174     }
1175
1176     if( cpu&X264_CPU_AVX )
1177     {
1178         INIT8( satd, _avx );
1179         INIT7( satd_x3, _avx );
1180         INIT7( satd_x4, _avx );
1181         INIT_ADS( _avx );
1182         if( !(cpu&X264_CPU_STACK_MOD4) )
1183         {
1184             INIT4( hadamard_ac, _avx );
1185             pixf->intra_sad_x9_4x4  = x264_intra_sad_x9_4x4_avx;
1186             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_avx;
1187             pixf->intra_sad_x9_8x8  = x264_intra_sad_x9_8x8_avx;
1188 #if ARCH_X86_64
1189             pixf->intra_sa8d_x9_8x8 = x264_intra_sa8d_x9_8x8_avx;
1190 #endif
1191         }
1192         INIT5( ssd, _avx );
1193         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_avx;
1194         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx;
1195         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_avx;
1196         pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_avx;
1197         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx;
1198         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_avx;
1199         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_avx;
1200         pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_avx;
1201         pixf->ssim_end4        = x264_pixel_ssim_end4_avx;
1202     }
1203
1204     if( cpu&X264_CPU_XOP )
1205     {
1206         INIT7( satd, _xop );
1207         INIT7( satd_x3, _xop );
1208         INIT7( satd_x4, _xop );
1209         if( !(cpu&X264_CPU_STACK_MOD4) )
1210         {
1211             INIT4( hadamard_ac, _xop );
1212             pixf->intra_satd_x9_4x4 = x264_intra_satd_x9_4x4_xop;
1213         }
1214         INIT5( ssd, _xop );
1215         pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_xop;
1216         pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_xop;
1217         pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_xop;
1218         pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_xop;
1219         pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_xop;
1220         pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_xop;
1221         pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_xop;
1222         pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_xop;
1223     }
1224 #endif //HAVE_MMX
1225
1226 #if HAVE_ARMV6
1227     if( cpu&X264_CPU_ARMV6 )
1228     {
1229         pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1230         pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1231         pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_4x8_armv6;
1232         pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_4x4_armv6;
1233     }
1234     if( cpu&X264_CPU_NEON )
1235     {
1236         INIT5( sad, _neon );
1237         INIT5( sad_aligned, _neon );
1238         INIT7( sad_x3, _neon );
1239         INIT7( sad_x4, _neon );
1240         INIT7( ssd, _neon );
1241         INIT7( satd, _neon );
1242         INIT7( satd_x3, _neon );
1243         INIT7( satd_x4, _neon );
1244         INIT4( hadamard_ac, _neon );
1245         pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_neon;
1246         pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_neon;
1247         pixf->var[PIXEL_8x8]    = x264_pixel_var_8x8_neon;
1248         pixf->var[PIXEL_16x16]  = x264_pixel_var_16x16_neon;
1249         pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_neon;
1250
1251         pixf->ssim_4x4x2_core   = x264_pixel_ssim_4x4x2_core_neon;
1252         pixf->ssim_end4         = x264_pixel_ssim_end4_neon;
1253
1254         if( cpu&X264_CPU_FAST_NEON_MRC )
1255         {
1256             pixf->sad[PIXEL_4x8] = x264_pixel_sad_4x8_neon;
1257             pixf->sad[PIXEL_4x4] = x264_pixel_sad_4x4_neon;
1258             pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_aligned_4x8_neon;
1259             pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_aligned_4x4_neon;
1260         }
1261         else    // really just scheduled for dual issue / A8
1262         {
1263             INIT5( sad_aligned, _neon_dual );
1264         }
1265     }
1266 #endif
1267 #endif // HIGH_BIT_DEPTH
1268 #if HAVE_ALTIVEC
1269     if( cpu&X264_CPU_ALTIVEC )
1270     {
1271         x264_pixel_altivec_init( pixf );
1272     }
1273 #endif
1274 #if !HIGH_BIT_DEPTH
1275 #if ARCH_UltraSPARC
1276     INIT4( sad, _vis );
1277     INIT4( sad_x3, _vis );
1278     INIT4( sad_x4, _vis );
1279 #endif
1280 #endif // !HIGH_BIT_DEPTH
1281
1282     pixf->ads[PIXEL_8x16] =
1283     pixf->ads[PIXEL_8x4] =
1284     pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
1285     pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
1286 }
1287