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