pixel.c 56.5 KB
Newer Older
1
/*****************************************************************************
2
 * pixel.c: pixel metrics
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2003-2017 x264 project
5
 *
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Fiona Glaser <fiona@x264.com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * 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 25
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
26 27
 *****************************************************************************/

28
#include "common.h"
29

Steven Walters's avatar
Steven Walters committed
30
#if HAVE_MMX
31
#   include "x86/pixel.h"
32
#   include "x86/predict.h"
33
#endif
Steven Walters's avatar
Steven Walters committed
34
#if ARCH_PPC
35 36
#   include "ppc/pixel.h"
#endif
Steven Walters's avatar
Steven Walters committed
37
#if ARCH_ARM
38
#   include "arm/pixel.h"
39
#   include "arm/predict.h"
40
#endif
41 42
#if ARCH_AARCH64
#   include "aarch64/pixel.h"
43
#   include "aarch64/predict.h"
44
#endif
45 46 47
#if ARCH_MIPS
#   include "mips/pixel.h"
#endif
48 49 50 51 52 53


/****************************************************************************
 * pixel_sad_WxH
 ****************************************************************************/
#define PIXEL_SAD_C( name, lx, ly ) \
54 55
static int name( pixel *pix1, intptr_t i_stride_pix1,  \
                 pixel *pix2, intptr_t i_stride_pix2 ) \
56 57
{                                                   \
    int i_sum = 0;                                  \
58
    for( int y = 0; y < ly; y++ )                   \
59
    {                                               \
60
        for( int x = 0; x < lx; x++ )               \
61 62 63 64 65 66 67 68 69 70
        {                                           \
            i_sum += abs( pix1[x] - pix2[x] );      \
        }                                           \
        pix1 += i_stride_pix1;                      \
        pix2 += i_stride_pix2;                      \
    }                                               \
    return i_sum;                                   \
}


71 72 73 74 75
PIXEL_SAD_C( x264_pixel_sad_16x16, 16, 16 )
PIXEL_SAD_C( x264_pixel_sad_16x8,  16,  8 )
PIXEL_SAD_C( x264_pixel_sad_8x16,   8, 16 )
PIXEL_SAD_C( x264_pixel_sad_8x8,    8,  8 )
PIXEL_SAD_C( x264_pixel_sad_8x4,    8,  4 )
Henrik Gramner's avatar
Henrik Gramner committed
76
PIXEL_SAD_C( x264_pixel_sad_4x16,   4, 16 )
77 78
PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
79

80 81 82 83
/****************************************************************************
 * pixel_ssd_WxH
 ****************************************************************************/
#define PIXEL_SSD_C( name, lx, ly ) \
84 85
static int name( pixel *pix1, intptr_t i_stride_pix1,  \
                 pixel *pix2, intptr_t i_stride_pix2 ) \
86 87
{                                                   \
    int i_sum = 0;                                  \
88
    for( int y = 0; y < ly; y++ )                   \
89
    {                                               \
90
        for( int x = 0; x < lx; x++ )               \
91 92 93 94 95 96 97 98 99 100
        {                                           \
            int d = pix1[x] - pix2[x];              \
            i_sum += d*d;                           \
        }                                           \
        pix1 += i_stride_pix1;                      \
        pix2 += i_stride_pix2;                      \
    }                                               \
    return i_sum;                                   \
}

101 102 103 104 105
PIXEL_SSD_C( x264_pixel_ssd_16x16, 16, 16 )
PIXEL_SSD_C( x264_pixel_ssd_16x8,  16,  8 )
PIXEL_SSD_C( x264_pixel_ssd_8x16,   8, 16 )
PIXEL_SSD_C( x264_pixel_ssd_8x8,    8,  8 )
PIXEL_SSD_C( x264_pixel_ssd_8x4,    8,  4 )
Henrik Gramner's avatar
Henrik Gramner committed
106
PIXEL_SSD_C( x264_pixel_ssd_4x16,   4, 16 )
107 108
PIXEL_SSD_C( x264_pixel_ssd_4x8,    4,  8 )
PIXEL_SSD_C( x264_pixel_ssd_4x4,    4,  4 )
109

110 111
uint64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1,
                             pixel *pix2, intptr_t i_pix2, int i_width, int i_height )
112
{
113
    uint64_t i_ssd = 0;
114
    int y;
115
    int align = !(((intptr_t)pix1 | (intptr_t)pix2 | i_pix1 | i_pix2) & 15);
116 117 118 119 120

#define SSD(size) i_ssd += pf->ssd[size]( pix1 + y*i_pix1 + x, i_pix1, \
                                          pix2 + y*i_pix2 + x, i_pix2 );
    for( y = 0; y < i_height-15; y += 16 )
    {
121
        int x = 0;
Loren Merritt's avatar
Loren Merritt committed
122 123 124 125
        if( align )
            for( ; x < i_width-15; x += 16 )
                SSD(PIXEL_16x16);
        for( ; x < i_width-7; x += 8 )
126 127 128
            SSD(PIXEL_8x16);
    }
    if( y < i_height-7 )
129
        for( int x = 0; x < i_width-7; x += 8 )
130 131 132 133
            SSD(PIXEL_8x8);
#undef SSD

#define SSD1 { int d = pix1[y*i_pix1+x] - pix2[y*i_pix2+x]; i_ssd += d*d; }
134
    if( i_width & 7 )
135 136
    {
        for( y = 0; y < (i_height & ~7); y++ )
137
            for( int x = i_width & ~7; x < i_width; x++ )
138 139
                SSD1;
    }
140
    if( i_height & 7 )
141 142
    {
        for( y = i_height & ~7; y < i_height; y++ )
143
            for( int x = 0; x < i_width; x++ )
144 145 146 147 148 149 150
                SSD1;
    }
#undef SSD1

    return i_ssd;
}

151 152
static void pixel_ssd_nv12_core( pixel *pixuv1, intptr_t stride1, pixel *pixuv2, intptr_t stride2,
                                 int width, int height, uint64_t *ssd_u, uint64_t *ssd_v )
153
{
154
    *ssd_u = 0, *ssd_v = 0;
155 156 157 158 159
    for( int y = 0; y < height; y++, pixuv1+=stride1, pixuv2+=stride2 )
        for( int x = 0; x < width; x++ )
        {
            int du = pixuv1[2*x]   - pixuv2[2*x];
            int dv = pixuv1[2*x+1] - pixuv2[2*x+1];
160 161
            *ssd_u += du*du;
            *ssd_v += dv*dv;
162 163 164
        }
}

165 166
void x264_pixel_ssd_nv12( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2,
                          int i_width, int i_height, uint64_t *ssd_u, uint64_t *ssd_v )
167
{
168
    pf->ssd_nv12_core( pix1, i_pix1, pix2, i_pix2, i_width&~7, i_height, ssd_u, ssd_v );
169
    if( i_width&7 )
170 171 172 173 174 175
    {
        uint64_t tmp[2];
        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] );
        *ssd_u += tmp[0];
        *ssd_v += tmp[1];
    }
176
}
177

178 179 180
/****************************************************************************
 * pixel_var_wxh
 ****************************************************************************/
Henrik Gramner's avatar
Henrik Gramner committed
181
#define PIXEL_VAR_C( name, w, h ) \
182
static uint64_t name( pixel *pix, intptr_t i_stride ) \
183
{                                             \
Fiona Glaser's avatar
Fiona Glaser committed
184
    uint32_t sum = 0, sqr = 0;                \
Henrik Gramner's avatar
Henrik Gramner committed
185
    for( int y = 0; y < h; y++ )              \
186
    {                                         \
187
        for( int x = 0; x < w; x++ )          \
188 189 190 191 192 193
        {                                     \
            sum += pix[x];                    \
            sqr += pix[x] * pix[x];           \
        }                                     \
        pix += i_stride;                      \
    }                                         \
Fiona Glaser's avatar
Fiona Glaser committed
194
    return sum + ((uint64_t)sqr << 32);       \
195 196
}

Henrik Gramner's avatar
Henrik Gramner committed
197 198 199
PIXEL_VAR_C( x264_pixel_var_16x16, 16, 16 )
PIXEL_VAR_C( x264_pixel_var_8x16,   8, 16 )
PIXEL_VAR_C( x264_pixel_var_8x8,    8,  8 )
200

201 202 203
/****************************************************************************
 * pixel_var2_wxh
 ****************************************************************************/
204
#define PIXEL_VAR2_C( name, w, h, shift ) \
205
static int name( pixel *pix1, intptr_t i_stride1, pixel *pix2, intptr_t i_stride2, int *ssd ) \
Henrik Gramner's avatar
Henrik Gramner committed
206
{ \
207
    int var = 0, sum = 0, sqr = 0; \
Henrik Gramner's avatar
Henrik Gramner committed
208 209 210 211 212 213 214 215 216 217 218
    for( int y = 0; y < h; y++ ) \
    { \
        for( int x = 0; x < w; x++ ) \
        { \
            int diff = pix1[x] - pix2[x]; \
            sum += diff; \
            sqr += diff * diff; \
        } \
        pix1 += i_stride1; \
        pix2 += i_stride2; \
    } \
219
    var = sqr - ((int64_t)sum * sum >> shift); \
Henrik Gramner's avatar
Henrik Gramner committed
220 221
    *ssd = sqr; \
    return var; \
222 223
}

224 225
PIXEL_VAR2_C( x264_pixel_var2_8x16, 8, 16, 7 )
PIXEL_VAR2_C( x264_pixel_var2_8x8,  8,  8, 6 )
Henrik Gramner's avatar
Henrik Gramner committed
226

227 228 229 230 231 232 233 234
#if BIT_DEPTH > 8
    typedef uint32_t sum_t;
    typedef uint64_t sum2_t;
#else
    typedef uint16_t sum_t;
    typedef uint32_t sum2_t;
#endif
#define BITS_PER_SUM (8 * sizeof(sum_t))
235

236
#define HADAMARD4(d0, d1, d2, d3, s0, s1, s2, s3) {\
237 238 239 240
    sum2_t t0 = s0 + s1;\
    sum2_t t1 = s0 - s1;\
    sum2_t t2 = s2 + s3;\
    sum2_t t3 = s2 - s3;\
Loren Merritt's avatar
Loren Merritt committed
241 242 243 244 245
    d0 = t0 + t2;\
    d2 = t0 - t2;\
    d1 = t1 + t3;\
    d3 = t1 - t3;\
}
246

247 248
// in: a pseudo-simd number of the form x+(y<<16)
// return: abs(x)+(abs(y)<<16)
249
static ALWAYS_INLINE sum2_t abs2( sum2_t a )
250
{
251
    sum2_t s = ((a>>(BITS_PER_SUM-1))&(((sum2_t)1<<BITS_PER_SUM)+1))*((sum_t)-1);
252 253 254
    return (a+s)^s;
}

255 256 257
/****************************************************************************
 * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
 ****************************************************************************/
258

259
static NOINLINE int x264_pixel_satd_4x4( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
260
{
261 262 263
    sum2_t tmp[4][2];
    sum2_t a0, a1, a2, a3, b0, b1;
    sum2_t sum = 0;
264
    for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
265
    {
266 267
        a0 = pix1[0] - pix2[0];
        a1 = pix1[1] - pix2[1];
268
        b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
269 270
        a2 = pix1[2] - pix2[2];
        a3 = pix1[3] - pix2[3];
271
        b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
272 273
        tmp[i][0] = b0 + b1;
        tmp[i][1] = b0 - b1;
274
    }
275
    for( int i = 0; i < 2; i++ )
276
    {
277
        HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
278
        a0 = abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
279
        sum += ((sum_t)a0) + (a0>>BITS_PER_SUM);
280 281
    }
    return sum >> 1;
282 283
}

284
static NOINLINE int x264_pixel_satd_8x4( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
285
{
286 287 288
    sum2_t tmp[4][4];
    sum2_t a0, a1, a2, a3;
    sum2_t sum = 0;
289
    for( int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2 )
290
    {
291 292 293 294
        a0 = (pix1[0] - pix2[0]) + ((sum2_t)(pix1[4] - pix2[4]) << BITS_PER_SUM);
        a1 = (pix1[1] - pix2[1]) + ((sum2_t)(pix1[5] - pix2[5]) << BITS_PER_SUM);
        a2 = (pix1[2] - pix2[2]) + ((sum2_t)(pix1[6] - pix2[6]) << BITS_PER_SUM);
        a3 = (pix1[3] - pix2[3]) + ((sum2_t)(pix1[7] - pix2[7]) << BITS_PER_SUM);
295 296
        HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
    }
297
    for( int i = 0; i < 4; i++ )
298
    {
299
        HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
300 301
        sum += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
    }
302
    return (((sum_t)sum) + (sum>>BITS_PER_SUM)) >> 1;
303
}
304

305
#define PIXEL_SATD_C( w, h, sub )\
306
static int x264_pixel_satd_##w##x##h( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )\
307 308 309 310 311 312 313 314 315 316 317 318 319
{\
    int sum = sub( pix1, i_pix1, pix2, i_pix2 )\
            + sub( pix1+4*i_pix1, i_pix1, pix2+4*i_pix2, i_pix2 );\
    if( w==16 )\
        sum+= sub( pix1+8, i_pix1, pix2+8, i_pix2 )\
            + sub( pix1+8+4*i_pix1, i_pix1, pix2+8+4*i_pix2, i_pix2 );\
    if( h==16 )\
        sum+= sub( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )\
            + sub( pix1+12*i_pix1, i_pix1, pix2+12*i_pix2, i_pix2 );\
    if( w==16 && h==16 )\
        sum+= sub( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 )\
            + sub( pix1+8+12*i_pix1, i_pix1, pix2+8+12*i_pix2, i_pix2 );\
    return sum;\
320
}
321 322 323 324
PIXEL_SATD_C( 16, 16, x264_pixel_satd_8x4 )
PIXEL_SATD_C( 16, 8,  x264_pixel_satd_8x4 )
PIXEL_SATD_C( 8,  16, x264_pixel_satd_8x4 )
PIXEL_SATD_C( 8,  8,  x264_pixel_satd_8x4 )
Henrik Gramner's avatar
Henrik Gramner committed
325
PIXEL_SATD_C( 4,  16, x264_pixel_satd_4x4 )
326
PIXEL_SATD_C( 4,  8,  x264_pixel_satd_4x4 )
327

328
static NOINLINE int sa8d_8x8( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
329
{
330 331 332
    sum2_t tmp[8][4];
    sum2_t a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3;
    sum2_t sum = 0;
333
    for( int i = 0; i < 8; i++, pix1 += i_pix1, pix2 += i_pix2 )
334
    {
335 336
        a0 = pix1[0] - pix2[0];
        a1 = pix1[1] - pix2[1];
337
        b0 = (a0+a1) + ((a0-a1)<<BITS_PER_SUM);
338 339
        a2 = pix1[2] - pix2[2];
        a3 = pix1[3] - pix2[3];
340
        b1 = (a2+a3) + ((a2-a3)<<BITS_PER_SUM);
341 342
        a4 = pix1[4] - pix2[4];
        a5 = pix1[5] - pix2[5];
343
        b2 = (a4+a5) + ((a4-a5)<<BITS_PER_SUM);
344 345
        a6 = pix1[6] - pix2[6];
        a7 = pix1[7] - pix2[7];
346
        b3 = (a6+a7) + ((a6-a7)<<BITS_PER_SUM);
347
        HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], b0,b1,b2,b3 );
348
    }
349
    for( int i = 0; i < 4; i++ )
350
    {
351 352
        HADAMARD4( a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
        HADAMARD4( a4, a5, a6, a7, tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i] );
353 354 355 356
        b0  = abs2(a0+a4) + abs2(a0-a4);
        b0 += abs2(a1+a5) + abs2(a1-a5);
        b0 += abs2(a2+a6) + abs2(a2-a6);
        b0 += abs2(a3+a7) + abs2(a3-a7);
357
        sum += (sum_t)b0 + (b0>>BITS_PER_SUM);
358 359 360
    }
    return sum;
}
361

362
static int x264_pixel_sa8d_8x8( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
363 364 365
{
    int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 );
    return (sum+2)>>2;
366 367
}

368
static int x264_pixel_sa8d_16x16( pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2 )
369 370 371 372 373 374
{
    int sum = sa8d_8x8( pix1, i_pix1, pix2, i_pix2 )
            + sa8d_8x8( pix1+8, i_pix1, pix2+8, i_pix2 )
            + sa8d_8x8( pix1+8*i_pix1, i_pix1, pix2+8*i_pix2, i_pix2 )
            + sa8d_8x8( pix1+8+8*i_pix1, i_pix1, pix2+8+8*i_pix2, i_pix2 );
    return (sum+2)>>2;
375 376
}

377
static NOINLINE uint64_t pixel_hadamard_ac( pixel *pix, intptr_t stride )
Loren Merritt's avatar
Loren Merritt committed
378
{
379 380 381
    sum2_t tmp[32];
    sum2_t a0, a1, a2, a3, dc;
    sum2_t sum4 = 0, sum8 = 0;
382
    for( int i = 0; i < 8; i++, pix+=stride )
Loren Merritt's avatar
Loren Merritt committed
383
    {
384 385 386
        sum2_t *t = tmp + (i&3) + (i&4)*4;
        a0 = (pix[0]+pix[1]) + ((sum2_t)(pix[0]-pix[1])<<BITS_PER_SUM);
        a1 = (pix[2]+pix[3]) + ((sum2_t)(pix[2]-pix[3])<<BITS_PER_SUM);
387 388
        t[0] = a0 + a1;
        t[4] = a0 - a1;
389 390
        a2 = (pix[4]+pix[5]) + ((sum2_t)(pix[4]-pix[5])<<BITS_PER_SUM);
        a3 = (pix[6]+pix[7]) + ((sum2_t)(pix[6]-pix[7])<<BITS_PER_SUM);
391 392
        t[8] = a2 + a3;
        t[12] = a2 - a3;
Loren Merritt's avatar
Loren Merritt committed
393
    }
394
    for( int i = 0; i < 8; i++ )
Loren Merritt's avatar
Loren Merritt committed
395
    {
396
        HADAMARD4( a0, a1, a2, a3, tmp[i*4+0], tmp[i*4+1], tmp[i*4+2], tmp[i*4+3] );
397 398 399 400 401
        tmp[i*4+0] = a0;
        tmp[i*4+1] = a1;
        tmp[i*4+2] = a2;
        tmp[i*4+3] = a3;
        sum4 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
Loren Merritt's avatar
Loren Merritt committed
402
    }
403
    for( int i = 0; i < 8; i++ )
Loren Merritt's avatar
Loren Merritt committed
404
    {
405 406
        HADAMARD4( a0,a1,a2,a3, tmp[i], tmp[8+i], tmp[16+i], tmp[24+i] );
        sum8 += abs2(a0) + abs2(a1) + abs2(a2) + abs2(a3);
Loren Merritt's avatar
Loren Merritt committed
407
    }
408 409 410
    dc = (sum_t)(tmp[0] + tmp[8] + tmp[16] + tmp[24]);
    sum4 = (sum_t)sum4 + (sum4>>BITS_PER_SUM) - dc;
    sum8 = (sum_t)sum8 + (sum8>>BITS_PER_SUM) - dc;
Loren Merritt's avatar
Loren Merritt committed
411 412 413 414
    return ((uint64_t)sum8<<32) + sum4;
}

#define HADAMARD_AC(w,h) \
415
static uint64_t x264_pixel_hadamard_ac_##w##x##h( pixel *pix, intptr_t stride )\
Loren Merritt's avatar
Loren Merritt committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
{\
    uint64_t sum = pixel_hadamard_ac( pix, stride );\
    if( w==16 )\
        sum += pixel_hadamard_ac( pix+8, stride );\
    if( h==16 )\
        sum += pixel_hadamard_ac( pix+8*stride, stride );\
    if( w==16 && h==16 )\
        sum += pixel_hadamard_ac( pix+8*stride+8, stride );\
    return ((sum>>34)<<32) + ((uint32_t)sum>>1);\
}
HADAMARD_AC( 16, 16 )
HADAMARD_AC( 16, 8 )
HADAMARD_AC( 8, 16 )
HADAMARD_AC( 8, 8 )


Loren Merritt's avatar
Loren Merritt committed
432 433 434
/****************************************************************************
 * pixel_sad_x4
 ****************************************************************************/
435
#define SAD_X( size ) \
436 437
static void x264_pixel_sad_x3_##size( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
                                      intptr_t i_stride, int scores[3] )\
438 439 440 441 442
{\
    scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
    scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
    scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
}\
443 444
static void x264_pixel_sad_x4_##size( pixel *fenc, pixel *pix0, pixel *pix1,pixel *pix2, pixel *pix3,\
                                      intptr_t i_stride, int scores[4] )\
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
{\
    scores[0] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix0, i_stride );\
    scores[1] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix1, i_stride );\
    scores[2] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix2, i_stride );\
    scores[3] = x264_pixel_sad_##size( fenc, FENC_STRIDE, pix3, i_stride );\
}

SAD_X( 16x16 )
SAD_X( 16x8 )
SAD_X( 8x16 )
SAD_X( 8x8 )
SAD_X( 8x4 )
SAD_X( 4x8 )
SAD_X( 4x4 )

460 461 462 463 464 465
/****************************************************************************
 * pixel_satd_x4
 * no faster than single satd, but needed for satd to be a drop-in replacement for sad
 ****************************************************************************/

#define SATD_X( size, cpu ) \
466 467
static void x264_pixel_satd_x3_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2,\
                                            intptr_t i_stride, int scores[3] )\
468 469 470 471 472
{\
    scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
    scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
    scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
}\
473 474
static void x264_pixel_satd_x4_##size##cpu( pixel *fenc, pixel *pix0, pixel *pix1, pixel *pix2, pixel *pix3,\
                                            intptr_t i_stride, int scores[4] )\
475 476 477 478 479 480
{\
    scores[0] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix0, i_stride );\
    scores[1] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix1, i_stride );\
    scores[2] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix2, i_stride );\
    scores[3] = x264_pixel_satd_##size##cpu( fenc, FENC_STRIDE, pix3, i_stride );\
}
481
#define SATD_X_DECL6( cpu )\
482 483 484 485
SATD_X( 16x16, cpu )\
SATD_X( 16x8, cpu )\
SATD_X( 8x16, cpu )\
SATD_X( 8x8, cpu )\
486 487
SATD_X( 8x4, cpu )\
SATD_X( 4x8, cpu )
488
#define SATD_X_DECL7( cpu )\
489
SATD_X_DECL6( cpu )\
490 491 492
SATD_X( 4x4, cpu )

SATD_X_DECL7()
Steven Walters's avatar
Steven Walters committed
493
#if HAVE_MMX
494
SATD_X_DECL7( _mmx2 )
495
#if !HIGH_BIT_DEPTH
496
SATD_X_DECL6( _sse2 )
Fiona Glaser's avatar
Fiona Glaser committed
497
SATD_X_DECL7( _ssse3 )
498
SATD_X_DECL6( _ssse3_atom )
499
SATD_X_DECL7( _sse4 )
500
SATD_X_DECL7( _avx )
501
SATD_X_DECL7( _xop )
502
#endif // !HIGH_BIT_DEPTH
503 504
#endif

505
#if !HIGH_BIT_DEPTH
506
#if HAVE_ARMV6 || ARCH_AARCH64
507 508
SATD_X_DECL7( _neon )
#endif
509
#endif // !HIGH_BIT_DEPTH
510

511
#define INTRA_MBCMP_8x8( mbcmp, cpu, cpu2 )\
512
void x264_intra_##mbcmp##_x3_8x8##cpu( pixel *fenc, pixel edge[36], int res[3] )\
513
{\
514
    ALIGNED_ARRAY_16( pixel, pix, [8*FDEC_STRIDE] );\
515
    x264_predict_8x8_v##cpu2( pix, edge );\
516
    res[0] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
517
    x264_predict_8x8_h##cpu2( pix, edge );\
518
    res[1] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
519
    x264_predict_8x8_dc##cpu2( pix, edge );\
520
    res[2] = x264_pixel_##mbcmp##_8x8##cpu( pix, FDEC_STRIDE, fenc, FENC_STRIDE );\
521 522
}

523 524
INTRA_MBCMP_8x8( sad,, _c )
INTRA_MBCMP_8x8(sa8d,, _c )
525
#if HIGH_BIT_DEPTH && HAVE_MMX
526
#define x264_predict_8x8_v_sse2 x264_predict_8x8_v_sse
527 528
INTRA_MBCMP_8x8( sad, _mmx2,  _c )
INTRA_MBCMP_8x8(sa8d, _sse2,  _sse2 )
529
#endif
530
#if !HIGH_BIT_DEPTH && (HAVE_ARMV6 || ARCH_AARCH64)
531 532 533
INTRA_MBCMP_8x8( sad, _neon, _neon )
INTRA_MBCMP_8x8(sa8d, _neon, _neon )
#endif
534

535 536
#define INTRA_MBCMP( mbcmp, size, pred1, pred2, pred3, chroma, cpu, cpu2 )\
void x264_intra_##mbcmp##_x3_##size##chroma##cpu( pixel *fenc, pixel *fdec, int res[3] )\
537
{\
538 539 540 541 542 543
    x264_predict_##size##chroma##_##pred1##cpu2( fdec );\
    res[0] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
    x264_predict_##size##chroma##_##pred2##cpu2( fdec );\
    res[1] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
    x264_predict_##size##chroma##_##pred3##cpu2( fdec );\
    res[2] = x264_pixel_##mbcmp##_##size##cpu( fdec, FDEC_STRIDE, fenc, FENC_STRIDE );\
544 545
}

546 547 548 549
INTRA_MBCMP( sad,  4x4,   v, h, dc,  ,, _c )
INTRA_MBCMP(satd,  4x4,   v, h, dc,  ,, _c )
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c,, _c )
INTRA_MBCMP(satd,  8x8,  dc, h,  v, c,, _c )
Henrik Gramner's avatar
Henrik Gramner committed
550 551
INTRA_MBCMP( sad,  8x16, dc, h,  v, c,, _c )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c,, _c )
552 553
INTRA_MBCMP( sad, 16x16,  v, h, dc,  ,, _c )
INTRA_MBCMP(satd, 16x16,  v, h, dc,  ,, _c )
554

Fiona Glaser's avatar
Fiona Glaser committed
555 556
#if HAVE_MMX
#if HIGH_BIT_DEPTH
557 558
#define x264_predict_8x8c_v_mmx2 x264_predict_8x8c_v_mmx
#define x264_predict_8x16c_v_mmx2 x264_predict_8x16c_v_c
559
#define x264_predict_16x16_dc_mmx2 x264_predict_16x16_dc_c
560 561 562
#define x264_predict_8x8c_v_sse2 x264_predict_8x8c_v_sse
#define x264_predict_8x16c_v_sse2 x264_predict_8x16c_v_sse
#define x264_predict_16x16_v_sse2 x264_predict_16x16_v_sse
563
INTRA_MBCMP( sad,  4x4,   v, h, dc,  , _mmx2, _c )
564 565 566
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _mmx2, _mmx2 )
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
567 568
INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _mmx2, _mmx2 )
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _sse2, _sse2 )
569 570
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _sse2, _sse2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse2, _sse2 )
571 572
INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _sse2, _sse2 )
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _ssse3, _sse2 )
573 574
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _ssse3, _sse2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _ssse3, _sse2 )
575
INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _ssse3, _sse2 )
576 577
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse4, _sse2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _avx, _sse2 )
Fiona Glaser's avatar
Fiona Glaser committed
578 579 580 581 582 583 584 585 586 587 588
#else
#define x264_predict_8x16c_v_mmx2 x264_predict_8x16c_v_mmx
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _mmx2, _mmx2 )
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _sse2, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse2, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _ssse3, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _sse4, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _avx, _mmx2 )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _xop, _mmx2 )
#endif
589
#endif
590
#if !HIGH_BIT_DEPTH && HAVE_ARMV6
591 592
INTRA_MBCMP( sad,  4x4,   v, h, dc,  , _neon, _armv6 )
INTRA_MBCMP(satd,  4x4,   v, h, dc,  , _neon, _armv6 )
593 594 595 596 597 598 599
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _neon, _neon )
INTRA_MBCMP(satd,  8x8,  dc, h,  v, c, _neon, _neon )
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _neon, _c )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _neon, _c )
INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _neon, _neon )
INTRA_MBCMP(satd, 16x16,  v, h, dc,  , _neon, _neon )
#endif
600
#if !HIGH_BIT_DEPTH && ARCH_AARCH64
601 602 603 604
INTRA_MBCMP( sad,  4x4,   v, h, dc,  , _neon, _neon )
INTRA_MBCMP(satd,  4x4,   v, h, dc,  , _neon, _neon )
INTRA_MBCMP( sad,  8x8,  dc, h,  v, c, _neon, _neon )
INTRA_MBCMP(satd,  8x8,  dc, h,  v, c, _neon, _neon )
605 606
INTRA_MBCMP( sad,  8x16, dc, h,  v, c, _neon, _neon )
INTRA_MBCMP(satd,  8x16, dc, h,  v, c, _neon, _neon )
607 608
INTRA_MBCMP( sad, 16x16,  v, h, dc,  , _neon, _neon )
INTRA_MBCMP(satd, 16x16,  v, h, dc,  , _neon, _neon )
609
#endif
610

611 612 613 614 615 616
// No C implementation of intra_satd_x9. See checkasm for its behavior,
// or see x264_mb_analyse_intra for the entirely different algorithm we
// use when lacking an asm implementation of it.



Loren Merritt's avatar
Loren Merritt committed
617 618 619
/****************************************************************************
 * structural similarity metric
 ****************************************************************************/
620 621 622
static void ssim_4x4x2_core( const pixel *pix1, intptr_t stride1,
                             const pixel *pix2, intptr_t stride2,
                             int sums[2][4] )
623
{
624
    for( int z = 0; z < 2; z++ )
625
    {
626 627 628
        uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
        for( int y = 0; y < 4; y++ )
            for( int x = 0; x < 4; x++ )
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
            {
                int a = pix1[x+y*stride1];
                int b = pix2[x+y*stride2];
                s1  += a;
                s2  += b;
                ss  += a*a;
                ss  += b*b;
                s12 += a*b;
            }
        sums[z][0] = s1;
        sums[z][1] = s2;
        sums[z][2] = ss;
        sums[z][3] = s12;
        pix1 += 4;
        pix2 += 4;
    }
}

static float ssim_end1( int s1, int s2, int ss, int s12 )
{
649 650 651 652 653 654 655 656 657
/* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
 * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
 * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
#if BIT_DEPTH > 9
#define type float
    static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
    static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
#else
#define type int
658 659
    static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
    static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
660 661 662 663 664 665 666 667 668 669
#endif
    type fs1 = s1;
    type fs2 = s2;
    type fss = ss;
    type fs12 = s12;
    type vars = fss*64 - fs1*fs1 - fs2*fs2;
    type covar = fs12*64 - fs1*fs2;
    return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
         / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
#undef type
670 671 672 673 674
}

static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
{
    float ssim = 0.0;
675
    for( int i = 0; i < width; i++ )
676 677 678 679 680 681 682 683
        ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
                           sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
                           sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
                           sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
    return ssim;
}

float x264_pixel_ssim_wxh( x264_pixel_function_t *pf,
684 685
                           pixel *pix1, intptr_t stride1,
                           pixel *pix2, intptr_t stride2,
686
                           int width, int height, void *buf, int *cnt )
687
{
688
    int z = 0;
689
    float ssim = 0.0;
690
    int (*sum0)[4] = buf;
691
    int (*sum1)[4] = sum0 + (width >> 2) + 3;
692 693
    width >>= 2;
    height >>= 2;
694
    for( int y = 1; y < height; y++ )
695 696 697 698
    {
        for( ; z <= y; z++ )
        {
            XCHG( void*, sum0, sum1 );
699
            for( int x = 0; x < width; x+=2 )
700 701
                pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
        }
702
        for( int x = 0; x < width-1; x += 4 )
703 704
            ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
    }
705
    *cnt = (height-1) * (width-1);
706
    return ssim;
707 708
}

709
static int pixel_vsad( pixel *src, intptr_t stride, int height )
710 711
{
    int score = 0;
712
    for( int i = 1; i < height; i++, src += stride )
713 714 715 716
        for( int j = 0; j < 16; j++ )
            score += abs(src[j] - src[j+stride]);
    return score;
}
717

718
int x264_field_vsad( x264_t *h, int mb_x, int mb_y )
719 720
{
    int score_field, score_frame;
721
    int stride = h->fenc->i_stride[0];
722
    int mb_stride = h->mb.i_mb_stride;
723
    pixel *fenc = h->fenc->plane[0] + 16 * (mb_x + mb_y * stride);
724
    int mb_xy = mb_x + mb_y*mb_stride;
725 726 727 728 729 730 731

    /* We don't want to analyze pixels outside the frame, as it gives inaccurate results. */
    int mbpair_height = X264_MIN( h->param.i_height - mb_y * 16, 32 );
    score_frame  = h->pixf.vsad( fenc,          stride, mbpair_height );
    score_field  = h->pixf.vsad( fenc,        stride*2, mbpair_height >> 1 );
    score_field += h->pixf.vsad( fenc+stride, stride*2, mbpair_height >> 1 );

732 733 734 735 736
    if( mb_x > 0 )
        score_field += 512 - h->mb.field[mb_xy        -1]*1024;
    if( mb_y > 0 )
        score_field += 512 - h->mb.field[mb_xy-mb_stride]*1024;

737 738 739
    return (score_field < score_frame);
}

740 741 742 743 744 745 746 747 748
static int pixel_asd8( pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2, int height )
{
    int sum = 0;
    for( int y = 0; y < height; y++, pix1 += stride1, pix2 += stride2 )
        for( int x = 0; x < 8; x++ )
            sum += pix1[x] - pix2[x];
    return abs( sum );
}

Loren Merritt's avatar
Loren Merritt committed
749 750 751
/****************************************************************************
 * successive elimination
 ****************************************************************************/
752 753
static int x264_pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
                            uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
Loren Merritt's avatar
Loren Merritt committed
754
{
755 756
    int nmv = 0;
    for( int i = 0; i < width; i++, sums++ )
757 758 759 760 761 762 763 764 765 766
    {
        int ads = abs( enc_dc[0] - sums[0] )
                + abs( enc_dc[1] - sums[8] )
                + abs( enc_dc[2] - sums[delta] )
                + abs( enc_dc[3] - sums[delta+8] )
                + cost_mvx[i];
        if( ads < thresh )
            mvs[nmv++] = i;
    }
    return nmv;
Loren Merritt's avatar
Loren Merritt committed
767 768
}

769 770
static int x264_pixel_ads2( int enc_dc[2], uint16_t *sums, int delta,
                            uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
Loren Merritt's avatar
Loren Merritt committed
771
{
772 773
    int nmv = 0;
    for( int i = 0; i < width; i++, sums++ )
774 775 776 777 778 779 780 781
    {
        int ads = abs( enc_dc[0] - sums[0] )
                + abs( enc_dc[1] - sums[delta] )
                + cost_mvx[i];
        if( ads < thresh )
            mvs[nmv++] = i;
    }
    return nmv;
Loren Merritt's avatar
Loren Merritt committed
782 783
}

784 785
static int x264_pixel_ads1( int enc_dc[1], uint16_t *sums, int delta,
                            uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
Loren Merritt's avatar
Loren Merritt committed
786
{
787 788
    int nmv = 0;
    for( int i = 0; i<width; i++, sums++ )
789 790 791 792 793 794 795
    {
        int ads = abs( enc_dc[0] - sums[0] )
                + cost_mvx[i];
        if( ads < thresh )
            mvs[nmv++] = i;
    }
    return nmv;
Loren Merritt's avatar
Loren Merritt committed
796 797 798
}


799 800 801 802 803
/****************************************************************************
 * x264_pixel_init:
 ****************************************************************************/
void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
{
804 805
    memset( pixf, 0, sizeof(*pixf) );

806 807 808 809 810 811 812 813 814 815
#define INIT2_NAME( name1, name2, cpu ) \
    pixf->name1[PIXEL_16x16] = x264_pixel_##name2##_16x16##cpu;\
    pixf->name1[PIXEL_16x8]  = x264_pixel_##name2##_16x8##cpu;
#define INIT4_NAME( name1, name2, cpu ) \
    INIT2_NAME( name1, name2, cpu ) \
    pixf->name1[PIXEL_8x16]  = x264_pixel_##name2##_8x16##cpu;\
    pixf->name1[PIXEL_8x8]   = x264_pixel_##name2##_8x8##cpu;
#define INIT5_NAME( name1, name2, cpu ) \
    INIT4_NAME( name1, name2, cpu ) \
    pixf->name1[PIXEL_8x4]   = x264_pixel_##name2##_8x4##cpu;
816
#define INIT6_NAME( name1, name2, cpu ) \
817
    INIT5_NAME( name1, name2, cpu ) \
818 819 820
    pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
#define INIT7_NAME( name1, name2, cpu ) \
    INIT6_NAME( name1, name2, cpu ) \
821
    pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
Henrik Gramner's avatar
Henrik Gramner committed
822 823 824
#define INIT8_NAME( name1, name2, cpu ) \
    INIT7_NAME( name1, name2, cpu ) \
    pixf->name1[PIXEL_4x16]  = x264_pixel_##name2##_4x16##cpu;
825 826 827
#define INIT2( name, cpu ) INIT2_NAME( name, name, cpu )
#define INIT4( name, cpu ) INIT4_NAME( name, name, cpu )
#define INIT5( name, cpu ) INIT5_NAME( name, name, cpu )
828
#define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
829
#define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
Henrik Gramner's avatar
Henrik Gramner committed
830
#define INIT8( name, cpu ) INIT8_NAME( name, name, cpu )
831

832 833 834 835 836
#define INIT_ADS( cpu ) \
    pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\
    pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\
    pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu;

Henrik Gramner's avatar
Henrik Gramner committed
837 838
    INIT8( sad, );
    INIT8_NAME( sad_aligned, sad, );
839 840
    INIT7( sad_x3, );
    INIT7( sad_x4, );
Henrik Gramner's avatar
Henrik Gramner committed
841 842
    INIT8( ssd, );
    INIT8( satd, );
843 844
    INIT7( satd_x3, );
    INIT7( satd_x4, );
Loren Merritt's avatar
Loren Merritt committed
845
    INIT4( hadamard_ac, );
846
    INIT_ADS( );
847

848 849
    pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16;
    pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8;
850
    pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
Henrik Gramner's avatar
Henrik Gramner committed
851
    pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16;
852
    pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;
Henrik Gramner's avatar
Henrik Gramner committed
853 854
    pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16;
    pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8;
855

856
    pixf->ssd_nv12_core = pixel_ssd_nv12_core;
857 858
    pixf->ssim_4x4x2_core = ssim_4x4x2_core;
    pixf->ssim_end4 = ssim_end4;
859
    pixf->vsad = pixel_vsad;
860
    pixf->asd8 = pixel_asd8;
861

862 863 864 865 866 867
    pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4;
    pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4;
    pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8;
    pixf->intra_sa8d_x3_8x8   = x264_intra_sa8d_x3_8x8;
    pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c;
    pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c;
Henrik Gramner's avatar
Henrik Gramner committed
868 869
    pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c;
    pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c;
870 871 872
    pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16;
    pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16;

873
#if HIGH_BIT_DEPTH
874
#if HAVE_MMX
875
    if( cpu&X264_CPU_MMX2 )
876
    {
877
        INIT7( sad, _mmx2 );
878
        INIT7_NAME( sad_aligned, sad, _mmx2 );
879 880
        INIT7( sad_x3, _mmx2 );
        INIT7( sad_x4, _mmx2 );
Fiona Glaser's avatar
Fiona Glaser committed
881
        INIT8( satd, _mmx2 );
882 883 884
        INIT7( satd_x3, _mmx2 );
        INIT7( satd_x4, _mmx2 );
        INIT4( hadamard_ac, _mmx2 );
Fiona Glaser's avatar
Fiona Glaser committed
885
        INIT8( ssd, _mmx2 );
886 887 888 889
        INIT_ADS( _mmx2 );

        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
Fiona Glaser's avatar
Fiona Glaser committed
890
#if ARCH_X86
Henrik Gramner's avatar
Henrik Gramner committed
891
        pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_mmx2;
Fiona Glaser's avatar
Fiona Glaser committed
892 893
        pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
#endif
894 895 896 897 898 899

        pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
        pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
        pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
        pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
        pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
900 901
        pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_mmx2;
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_mmx2;
902 903
        pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
        pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
904 905 906 907 908
    }
    if( cpu&X264_CPU_SSE2 )
    {
        INIT4_NAME( sad_aligned, sad, _sse2_aligned );
        INIT5( ssd, _sse2 );
909 910
        INIT6( satd, _sse2 );
        pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_sse2;
911 912 913 914 915

        pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
        pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
#if ARCH_X86_64
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
916
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse2;
917
#endif
918
        pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_sse2;
919
        pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_sse2;
920 921
        pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
        pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
922
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
Henrik Gramner's avatar
Henrik Gramner committed
923 924
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_sse2;
        pixf->var2[PIXEL_8x8]  = x264_pixel_var2_8x8_sse2;
Fiona Glaser's avatar
Fiona Glaser committed
925
        pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_sse2;
926
        pixf->intra_sad_x3_8x8 = x264_intra_sad_x3_8x8_sse2;
927 928 929 930 931 932 933 934 935 936 937 938
    }
    if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
    {
        INIT5( sad, _sse2 );
        INIT2( sad_x3, _sse2 );
        INIT2( sad_x4, _sse2 );
        INIT_ADS( _sse2 );

        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _sse2 );
        }
Ilia's avatar
Ilia committed
939
        pixf->vsad = x264_pixel_vsad_sse2;
940
        pixf->asd8 = x264_pixel_asd8_sse2;
941 942
        pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_sse2;
        pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_sse2;
943 944
        pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_sse2;
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse2;
945 946 947 948 949 950 951 952 953 954 955 956 957 958
        pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_sse2;
    }
    if( cpu&X264_CPU_SSE2_IS_FAST )
    {
        pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
        pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_sse2;
        pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_sse2;
        pixf->sad_x3[PIXEL_8x4]  = x264_pixel_sad_x3_8x4_sse2;
        pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_sse2;
        pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_sse2;
        pixf->sad_x4[PIXEL_8x4]  = x264_pixel_sad_x4_8x4_sse2;
    }
    if( cpu&X264_CPU_SSSE3 )
    {
959
        INIT4_NAME( sad_aligned, sad, _ssse3_aligned );
960 961
        pixf->sad_aligned[PIXEL_4x4] = x264_pixel_sad_4x4_ssse3;
        pixf->sad_aligned[PIXEL_4x8] = x264_pixel_sad_4x8_ssse3;
962 963 964 965
        INIT7( sad, _ssse3 );
        INIT7( sad_x3, _ssse3 );
        INIT7( sad_x4, _ssse3 );
        INIT_ADS( _ssse3 );
966 967
        INIT6( satd, _ssse3 );
        pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_ssse3;
968 969 970 971 972

        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _ssse3 );
        }
Ilia's avatar
Ilia committed
973
        pixf->vsad = x264_pixel_vsad_ssse3;
974
        pixf->asd8 = x264_pixel_asd8_ssse3;
975
        pixf->intra_sad_x3_4x4  = x264_intra_sad_x3_4x4_ssse3;
976 977
        pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
        pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
978 979 980
#if ARCH_X86_64
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_ssse3;
#endif
981
        pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_ssse3;
982 983
        pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_ssse3;
        pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_ssse3;
984 985
        pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_ssse3;
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_ssse3;
986 987
        pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
    }
988 989
    if( cpu&X264_CPU_SSE4 )
    {
990 991
        INIT6( satd, _sse4 );
        pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_sse4;
992 993 994 995 996 997
        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _sse4 );
        }
        pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_sse4;
        pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_sse4;
998 999 1000
#if ARCH_X86_64
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse4;
#endif
1001
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_sse4;
1002 1003 1004
    }
    if( cpu&X264_CPU_AVX )
    {
1005
        INIT5_NAME( sad_aligned, sad, _ssse3 ); /* AVX-capable CPUs doesn't benefit from an aligned version */
1006
        INIT_ADS( _avx );
1007 1008
        INIT6( satd, _avx );
        pixf->satd[PIXEL_4x16] = x264_pixel_satd_4x16_avx;
1009 1010 1011 1012
        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _avx );
        }
1013
        pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_avx;
1014 1015 1016 1017 1018 1019 1020
        pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_avx;
        pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_avx;
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx;
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_avx;
        pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_avx;
        pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_avx;
        pixf->ssim_end4        = x264_pixel_ssim_end4_avx;
1021 1022 1023
#if ARCH_X86_64
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_avx;
#endif
1024
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_avx;
1025
    }
Ilia's avatar
Ilia committed
1026 1027
    if( cpu&X264_CPU_XOP )
    {
1028 1029
        INIT5( sad_x3, _xop );
        INIT5( sad_x4, _xop );
1030
        pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_xop;
1031 1032
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_xop;
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_xop;
Ilia's avatar
Ilia committed
1033
        pixf->vsad = x264_pixel_vsad_xop;
1034
        pixf->asd8 = x264_pixel_asd8_xop;
1035 1036 1037
#if ARCH_X86_64
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_xop;
#endif
Ilia's avatar
Ilia committed
1038
    }
1039 1040
    if( cpu&X264_CPU_AVX2 )
    {
1041
        INIT2( ssd, _avx2 );
1042 1043
        INIT2( sad, _avx2 );
        INIT2_NAME( sad_aligned, sad, _avx2 );
1044 1045
        INIT2( sad_x3, _avx2 );
        INIT2( sad_x4, _avx2 );
1046
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_avx2;
1047
        pixf->vsad = x264_pixel_vsad_avx2;
1048
        pixf->ssd_nv12_core = x264_pixel_ssd_nv12_core_avx2;
1049
        pixf->intra_sad_x3_8x8 = x264_intra_sad_x3_8x8_avx2;
1050
    }
1051
#endif // HAVE_MMX
1052
#else // !HIGH_BIT_DEPTH
Steven Walters's avatar
Steven Walters committed
1053
#if HAVE_MMX
1054 1055
    if( cpu&X264_CPU_MMX )
    {
1056
        INIT8( ssd, _mmx );
1057 1058
    }

1059
    if( cpu&X264_CPU_MMX2 )
1060
    {
1061 1062
        INIT8( sad, _mmx2 );
        INIT8_NAME( sad_aligned, sad, _mmx2 );
1063 1064
        INIT7( sad_x3, _mmx2 );
        INIT7( sad_x4, _mmx2 );
Fiona Glaser's avatar
Fiona Glaser committed
1065
        INIT8( satd, _mmx2 );
1066 1067 1068 1069 1070
        INIT7( satd_x3, _mmx2 );
        INIT7( satd_x4, _mmx2 );
        INIT4( hadamard_ac, _mmx2 );
        INIT_ADS( _mmx2 );
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmx2;
1071
        pixf->var[PIXEL_8x16]  = x264_pixel_var_8x16_mmx2;
1072
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmx2;
Steven Walters's avatar
Steven Walters committed
1073
#if ARCH_X86
1074 1075 1076 1077
        pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmx2;
        pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmx2;
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmx2;
        pixf->ssim_4x4x2_core = x264_pixel_ssim_4x4x2_core_mmx2;
Henrik Gramner's avatar
Henrik Gramner committed
1078
        pixf->var2[PIXEL_8x8] = x264_pixel_var2_8x8_mmx2;
Fiona Glaser's avatar
Fiona Glaser committed
1079
        pixf->var2[PIXEL_8x16] = x264_pixel_var2_8x16_mmx2;
1080
        pixf->vsad = x264_pixel_vsad_mmx2;
1081

1082
        if( cpu&X264_CPU_CACHELINE_32 )
1083
        {
1084 1085 1086
            INIT5( sad, _cache32_mmx2 );
            INIT4( sad_x3, _cache32_mmx2 );
            INIT4( sad_x4, _cache32_mmx2 );
1087
        }
1088
        else if( cpu&X264_CPU_CACHELINE_64 && !(cpu&X264_CPU_SLOW_ATOM) )
1089
        {
1090 1091 1092
            INIT5( sad, _cache64_mmx2 );
            INIT4( sad_x3, _cache64_mmx2 );
            INIT4( sad_x4, _cache64_mmx2 );
1093 1094
        }
#else
1095
        if( cpu&X264_CPU_CACHELINE_64 && !(cpu&X264_CPU_SLOW_ATOM) )
1096
        {
1097 1098 1099 1100 1101 1102 1103
            pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmx2;
            pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmx2;
            pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmx2;
            pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmx2;
            pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmx2;
            pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmx2;
            pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmx2;
1104
        }
1105
#endif
1106 1107
        pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmx2;
        pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_mmx2;
Fiona Glaser's avatar
Fiona Glaser committed
1108 1109
        pixf->intra_satd_x3_8x16c = x264_intra_satd_x3_8x16c_mmx2;
        pixf->intra_sad_x3_8x16c  = x264_intra_sad_x3_8x16c_mmx2;
1110 1111 1112 1113 1114
        pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmx2;
        pixf->intra_sad_x3_8x8c   = x264_intra_sad_x3_8x8c_mmx2;
        pixf->intra_sad_x3_8x8    = x264_intra_sad_x3_8x8_mmx2;
        pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmx2;
        pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4_mmx2;
1115
    }
1116

1117 1118 1119 1120 1121
    if( cpu&X264_CPU_SSE2 )
    {
        INIT5( ssd, _sse2slow );
        INIT2_NAME( sad_aligned, sad, _sse2_aligned );
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
1122
        pixf->ssd_nv12_core    = x264_pixel_ssd_nv12_core_sse2;
1123 1124 1125 1126
        pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
        pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
        pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
        pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
Steven Walters's avatar
Steven Walters committed
1127
#if ARCH_X86_64
1128
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
1129
        pixf->sa8d_satd[PIXEL_16x16] = x264_pixel_sa8d_satd_16x16_sse2;
1130
#endif
Henrik Gramner's avatar
Henrik Gramner committed
1131
        pixf->var2[PIXEL_8x8]   = x264_pixel_var2_8x8_sse2;
Fiona Glaser's avatar
Fiona Glaser committed
1132
        pixf->var2[PIXEL_8x16]  = x264_pixel_var2_8x16_sse2;
1133
        pixf->vsad = x264_pixel_vsad_sse2;
1134
        pixf->asd8 = x264_pixel_asd8_sse2;
1135 1136
    }

1137
    if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
1138
    {
1139 1140 1141
        INIT2( sad, _sse2 );
        INIT2( sad_x3, _sse2 );
        INIT2( sad_x4, _sse2 );