pixel.c 26.8 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * pixel.c: h264 encoder
 *****************************************************************************
4
 * Copyright (C) 2003-2008 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5
 *
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
Laurent Aimar's avatar
Laurent Aimar committed
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Laurent Aimar's avatar
Laurent Aimar committed
22 23
 *****************************************************************************/

Loren Merritt's avatar
Loren Merritt committed
24
#include "common.h"
Laurent Aimar's avatar
Laurent Aimar committed
25

26
#ifdef HAVE_MMX
27
#   include "x86/pixel.h"
Laurent Aimar's avatar
Laurent Aimar committed
28
#endif
29
#ifdef ARCH_PPC
Laurent Aimar's avatar
Laurent Aimar committed
30 31
#   include "ppc/pixel.h"
#endif
32 33 34
#ifdef ARCH_UltraSparc
#   include "sparc/pixel.h"
#endif
Laurent Aimar's avatar
Laurent Aimar committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


/****************************************************************************
 * pixel_sad_WxH
 ****************************************************************************/
#define PIXEL_SAD_C( name, lx, ly ) \
static int name( uint8_t *pix1, int i_stride_pix1,  \
                 uint8_t *pix2, int i_stride_pix2 ) \
{                                                   \
    int i_sum = 0;                                  \
    int x, y;                                       \
    for( y = 0; y < ly; y++ )                       \
    {                                               \
        for( x = 0; x < lx; x++ )                   \
        {                                           \
            i_sum += abs( pix1[x] - pix2[x] );      \
        }                                           \
        pix1 += i_stride_pix1;                      \
        pix2 += i_stride_pix2;                      \
    }                                               \
    return i_sum;                                   \
}


59 60 61 62 63 64 65
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 )
PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
Laurent Aimar's avatar
Laurent Aimar committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/****************************************************************************
 * pixel_ssd_WxH
 ****************************************************************************/
#define PIXEL_SSD_C( name, lx, ly ) \
static int name( uint8_t *pix1, int i_stride_pix1,  \
                 uint8_t *pix2, int i_stride_pix2 ) \
{                                                   \
    int i_sum = 0;                                  \
    int x, y;                                       \
    for( y = 0; y < ly; y++ )                       \
    {                                               \
        for( x = 0; x < lx; x++ )                   \
        {                                           \
            int d = pix1[x] - pix2[x];              \
            i_sum += d*d;                           \
        }                                           \
        pix1 += i_stride_pix1;                      \
        pix2 += i_stride_pix2;                      \
    }                                               \
    return i_sum;                                   \
}

90 91 92 93 94 95 96
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 )
PIXEL_SSD_C( x264_pixel_ssd_4x8,    4,  8 )
PIXEL_SSD_C( x264_pixel_ssd_4x4,    4,  4 )
97

Loren Merritt's avatar
Loren Merritt committed
98 99 100 101
int64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height )
{
    int64_t i_ssd = 0;
    int x, y;
Loren Merritt's avatar
Loren Merritt committed
102
    int align = !(((long)pix1 | (long)pix2 | i_pix1 | i_pix2) & 15);
Loren Merritt's avatar
Loren Merritt committed
103 104 105 106 107

#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 )
    {
Loren Merritt's avatar
Loren Merritt committed
108 109 110 111 112
        x = 0;
        if( align )
            for( ; x < i_width-15; x += 16 )
                SSD(PIXEL_16x16);
        for( ; x < i_width-7; x += 8 )
Loren Merritt's avatar
Loren Merritt committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            SSD(PIXEL_8x16);
    }
    if( y < i_height-7 )
        for( x = 0; x < i_width-7; x += 8 )
            SSD(PIXEL_8x8);
#undef SSD

#define SSD1 { int d = pix1[y*i_pix1+x] - pix2[y*i_pix2+x]; i_ssd += d*d; }
    if( i_width % 8 != 0 )
    {
        for( y = 0; y < (i_height & ~7); y++ )
            for( x = i_width & ~7; x < i_width; x++ )
                SSD1;
    }
    if( i_height % 8 != 0 )
    {
        for( y = i_height & ~7; y < i_height; y++ )
            for( x = 0; x < i_width; x++ )
                SSD1;
    }
#undef SSD1

    return i_ssd;
}

138

139 140 141 142
/****************************************************************************
 * pixel_var_wxh
 ****************************************************************************/
#define PIXEL_VAR_C( name, w, shift ) \
143
static int name( uint8_t *pix, int i_stride ) \
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
{                                             \
    uint32_t var = 0, sum = 0, sqr = 0;       \
    int x, y;                                 \
    for( y = 0; y < w; y++ )                  \
    {                                         \
        for( x = 0; x < w; x++ )              \
        {                                     \
            sum += pix[x];                    \
            sqr += pix[x] * pix[x];           \
        }                                     \
        pix += i_stride;                      \
    }                                         \
    var = sqr - (sum * sum >> shift);         \
    return var;                               \
}

PIXEL_VAR_C( x264_pixel_var_16x16, 16, 8 )
PIXEL_VAR_C( x264_pixel_var_8x8,    8, 6 )


Loren Merritt's avatar
Loren Merritt committed
164 165 166 167 168 169 170 171 172 173
#define HADAMARD4(d0,d1,d2,d3,s0,s1,s2,s3) {\
    int t0 = s0 + s1;\
    int t1 = s0 - s1;\
    int t2 = s2 + s3;\
    int t3 = s2 - s3;\
    d0 = t0 + t2;\
    d2 = t0 - t2;\
    d1 = t1 + t3;\
    d3 = t1 - t3;\
}
174

175 176 177
/****************************************************************************
 * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
178 179 180 181 182 183 184 185 186 187
static int pixel_satd_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height )
{
    int16_t tmp[4][4];
    int x, y;
    int i_satd = 0;

    for( y = 0; y < i_height; y += 4 )
    {
        for( x = 0; x < i_width; x += 4 )
        {
Loren Merritt's avatar
Loren Merritt committed
188 189
            int i;
            uint8_t *p1 = pix1+x, *p2 = pix2+x;
Laurent Aimar's avatar
Laurent Aimar committed
190

Loren Merritt's avatar
Loren Merritt committed
191
            for( i=0; i<4; i++, p1+=i_pix1, p2+=i_pix2 )
Laurent Aimar's avatar
Laurent Aimar committed
192
            {
Loren Merritt's avatar
Loren Merritt committed
193 194 195 196 197
                int a0 = p1[0] - p2[0];
                int a1 = p1[1] - p2[1];
                int a2 = p1[2] - p2[2];
                int a3 = p1[3] - p2[3];
                HADAMARD4( tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0,a1,a2,a3 );
Laurent Aimar's avatar
Laurent Aimar committed
198
            }
Loren Merritt's avatar
Loren Merritt committed
199
            for( i=0; i<4; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
200
            {
Loren Merritt's avatar
Loren Merritt committed
201 202 203
                int a0,a1,a2,a3;
                HADAMARD4( a0,a1,a2,a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i] );
                i_satd += abs(a0) + abs(a1) + abs(a2) + abs(a3);
Laurent Aimar's avatar
Laurent Aimar committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            }

        }
        pix1 += 4 * i_pix1;
        pix2 += 4 * i_pix2;
    }

    return i_satd / 2;
}
#define PIXEL_SATD_C( name, width, height ) \
static int name( uint8_t *pix1, int i_stride_pix1, \
                 uint8_t *pix2, int i_stride_pix2 ) \
{ \
    return pixel_satd_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ); \
}
219 220 221 222 223 224 225
PIXEL_SATD_C( x264_pixel_satd_16x16, 16, 16 )
PIXEL_SATD_C( x264_pixel_satd_16x8,  16, 8 )
PIXEL_SATD_C( x264_pixel_satd_8x16,  8, 16 )
PIXEL_SATD_C( x264_pixel_satd_8x8,   8, 8 )
PIXEL_SATD_C( x264_pixel_satd_8x4,   8, 4 )
PIXEL_SATD_C( x264_pixel_satd_4x8,   4, 8 )
PIXEL_SATD_C( x264_pixel_satd_4x4,   4, 4 )
Laurent Aimar's avatar
Laurent Aimar committed
226 227


228 229 230 231
/****************************************************************************
 * pixel_sa8d_WxH: sum of 8x8 Hadamard transformed differences
 ****************************************************************************/
#define SA8D_1D {\
Loren Merritt's avatar
Loren Merritt committed
232 233 234 235 236 237 238 239 240 241 242
    int b0,b1,b2,b3,b4,b5,b6,b7;\
    HADAMARD4( b0,b1,b2,b3, SRC(0), SRC(1), SRC(2), SRC(3) );\
    HADAMARD4( b4,b5,b6,b7, SRC(4), SRC(5), SRC(6), SRC(7) );\
    DST(0, b0 + b4);\
    DST(4, b0 - b4);\
    DST(1, b1 + b5);\
    DST(5, b1 - b5);\
    DST(2, b2 + b6);\
    DST(6, b2 - b6);\
    DST(3, b3 + b7);\
    DST(7, b3 - b7);\
243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

static inline int pixel_sa8d_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2,
                                  int i_width, int i_height )
{
    int16_t diff[8][8];
    int i_satd = 0;
    int x, y;

    for( y = 0; y < i_height; y += 8 )
    {
        for( x = 0; x < i_width; x += 8 )
        {
            int i;
Loren Merritt's avatar
Loren Merritt committed
257
            uint8_t *p1 = pix1+x, *p2 = pix2+x;
258

Loren Merritt's avatar
Loren Merritt committed
259
#define SRC(x)     a##x
260
#define DST(x,rhs) diff[i][x] = (rhs)
Loren Merritt's avatar
Loren Merritt committed
261 262 263 264 265 266 267 268 269 270
            for( i=0; i<8; i++, p1+=i_pix1, p2+=i_pix2 )
            {
                int a0 = p1[0] - p2[0];
                int a1 = p1[1] - p2[1];
                int a2 = p1[2] - p2[2];
                int a3 = p1[3] - p2[3];
                int a4 = p1[4] - p2[4];
                int a5 = p1[5] - p2[5];
                int a6 = p1[6] - p2[6];
                int a7 = p1[7] - p2[7];
271
                SA8D_1D
Loren Merritt's avatar
Loren Merritt committed
272
            }
273 274 275 276 277
#undef SRC
#undef DST

#define SRC(x)     diff[x][i]
#define DST(x,rhs) i_satd += abs(rhs)
Loren Merritt's avatar
Loren Merritt committed
278
            for( i=0; i<8; i++ )
279 280 281 282 283 284 285 286 287 288 289 290
                SA8D_1D
#undef SRC
#undef DST
        }
        pix1 += 8 * i_pix1;
        pix2 += 8 * i_pix2;
    }

    return i_satd;
}

#define PIXEL_SA8D_C( width, height ) \
291 292
static int x264_pixel_sa8d_##width##x##height( uint8_t *pix1, int i_stride_pix1, \
                                               uint8_t *pix2, int i_stride_pix2 ) \
293 294 295 296 297 298 299 300
{ \
    return ( pixel_sa8d_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ) + 2 ) >> 2; \
}
PIXEL_SA8D_C( 16, 16 )
PIXEL_SA8D_C( 16, 8 )
PIXEL_SA8D_C( 8, 16 )
PIXEL_SA8D_C( 8, 8 )

Loren Merritt's avatar
Loren Merritt committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

static uint64_t pixel_hadamard_ac( uint8_t *pix, int stride )
{
    int16_t tmp[8][8];
    int sum4=0, sum8=0;
    int i;
    for( i=0; i<8; i++, pix+=stride )
    {
        HADAMARD4( tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i],
                   pix[0], pix[1], pix[2], pix[3] );
        HADAMARD4( tmp[4][i], tmp[5][i], tmp[6][i], tmp[7][i],
                   pix[4], pix[5], pix[6], pix[7] );
    }
    for( i=0; i<8; i++ )
    {
        int a0,a1,a2,a3,a4,a5,a6,a7;
        HADAMARD4( a0,a1,a2,a3, tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3] );
        sum4 += abs(a0) + abs(a1) + abs(a2) + abs(a3);
        HADAMARD4( a4,a5,a6,a7, tmp[i][4], tmp[i][5], tmp[i][6], tmp[i][7] );
        sum4 += abs(a4) + abs(a5) + abs(a6) + abs(a7);
        tmp[i][0] = a0 + a4;
        tmp[i][4] = a0 - a4;
        tmp[i][1] = a1 + a5;
        tmp[i][5] = a1 - a5;
        tmp[i][2] = a2 + a6;
        tmp[i][6] = a2 - a6;
        tmp[i][3] = a3 + a7;
        tmp[i][7] = a3 - a7;
    }
    for( i=0; i<8; i++ )
    {
        sum8 += abs( tmp[0][i] + tmp[4][i] )
              + abs( tmp[0][i] - tmp[4][i] )
              + abs( tmp[1][i] + tmp[5][i] )
              + abs( tmp[1][i] - tmp[5][i] )
              + abs( tmp[2][i] + tmp[6][i] )
              + abs( tmp[2][i] - tmp[6][i] )
              + abs( tmp[3][i] + tmp[7][i] )
              + abs( tmp[3][i] - tmp[7][i] );
    }
    sum4 -= tmp[0][0]+tmp[4][0];
    sum8 -= tmp[0][0]+tmp[4][0];
    return ((uint64_t)sum8<<32) + sum4;
}

#define HADAMARD_AC(w,h) \
static uint64_t x264_pixel_hadamard_ac_##w##x##h( uint8_t *pix, int stride )\
{\
    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
364 365 366
/****************************************************************************
 * pixel_sad_x4
 ****************************************************************************/
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
#define SAD_X( size ) \
static void x264_pixel_sad_x3_##size( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, int i_stride, int scores[3] )\
{\
    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 );\
}\
static void x264_pixel_sad_x4_##size( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, uint8_t *pix3, int i_stride, int scores[4] )\
{\
    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 )

#ifdef ARCH_UltraSparc
SAD_X( 16x16_vis )
SAD_X( 16x8_vis )
SAD_X( 8x16_vis )
SAD_X( 8x8_vis )
#endif
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
/****************************************************************************
 * 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 ) \
static void x264_pixel_satd_x3_##size##cpu( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, int i_stride, int scores[3] )\
{\
    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 );\
}\
static void x264_pixel_satd_x4_##size##cpu( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, uint8_t *pix3, int i_stride, int scores[4] )\
{\
    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 );\
}
416
#define SATD_X_DECL6( cpu )\
417 418 419 420
SATD_X( 16x16, cpu )\
SATD_X( 16x8, cpu )\
SATD_X( 8x16, cpu )\
SATD_X( 8x8, cpu )\
421 422
SATD_X( 8x4, cpu )\
SATD_X( 4x8, cpu )
423
#define SATD_X_DECL7( cpu )\
424
SATD_X_DECL6( cpu )\
425 426 427 428 429
SATD_X( 4x4, cpu )

SATD_X_DECL7()
#ifdef HAVE_MMX
SATD_X_DECL7( _mmxext )
430
SATD_X_DECL6( _sse2 )
Fiona Glaser's avatar
Fiona Glaser committed
431
SATD_X_DECL7( _ssse3 )
432
SATD_X_DECL6( _ssse3_phadd )
433 434
#endif

Loren Merritt's avatar
Loren Merritt committed
435 436 437
/****************************************************************************
 * structural similarity metric
 ****************************************************************************/
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
static void ssim_4x4x2_core( const uint8_t *pix1, int stride1,
                             const uint8_t *pix2, int stride2,
                             int sums[2][4])
{
    int x, y, z;
    for(z=0; z<2; z++)
    {
        uint32_t s1=0, s2=0, ss=0, s12=0;
        for(y=0; y<4; y++)
            for(x=0; x<4; x++)
            {
                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 )
{
    static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
    static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
    int vars = ss*64 - s1*s1 - s2*s2;
    int covar = s12*64 - s1*s2;
    return (float)(2*s1*s2 + ssim_c1) * (float)(2*covar + ssim_c2)\
           / ((float)(s1*s1 + s2*s2 + ssim_c1) * (float)(vars + ssim_c2));
}

static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
{
    int i;
    float ssim = 0.0;
    for( i = 0; i < width; i++ )
        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,
                           uint8_t *pix1, int stride1,
                           uint8_t *pix2, int stride2,
                           int width, int height )
{
    int x, y, z;
    float ssim = 0.0;
495 496
    int (*sum0)[4] = x264_malloc(4 * (width/4+3) * sizeof(int));
    int (*sum1)[4] = x264_malloc(4 * (width/4+3) * sizeof(int));
497 498 499 500 501 502 503 504 505 506 507 508 509 510
    width >>= 2;
    height >>= 2;
    z = 0;
    for( y = 1; y < height; y++ )
    {
        for( ; z <= y; z++ )
        {
            XCHG( void*, sum0, sum1 );
            for( x = 0; x < width; x+=2 )
                pf->ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
        }
        for( x = 0; x < width-1; x += 4 )
            ssim += pf->ssim_end4( sum0+x, sum1+x, X264_MIN(4,width-x-1) );
    }
511 512
    x264_free(sum0);
    x264_free(sum1);
513
    return ssim;
514 515 516
}


Loren Merritt's avatar
Loren Merritt committed
517 518 519
/****************************************************************************
 * successive elimination
 ****************************************************************************/
520 521
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
522
{
523
    int nmv=0, i;
Loren Merritt's avatar
Loren Merritt committed
524
    for( i=0; i<width; i++, sums++ )
525 526 527 528 529 530 531 532 533 534
    {
        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
535 536
}

537 538
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
539
{
540
    int nmv=0, i;
Loren Merritt's avatar
Loren Merritt committed
541
    for( i=0; i<width; i++, sums++ )
542 543 544 545 546 547 548 549
    {
        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
550 551
}

552 553
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
554
{
555
    int nmv=0, i;
Loren Merritt's avatar
Loren Merritt committed
556
    for( i=0; i<width; i++, sums++ )
557 558 559 560 561 562 563
    {
        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
564 565 566
}


Laurent Aimar's avatar
Laurent Aimar committed
567 568 569 570 571
/****************************************************************************
 * x264_pixel_init:
 ****************************************************************************/
void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
{
572 573
    memset( pixf, 0, sizeof(*pixf) );

574 575 576 577 578 579 580 581 582 583
#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;
584
#define INIT6_NAME( name1, name2, cpu ) \
585
    INIT5_NAME( name1, name2, cpu ) \
586 587 588
    pixf->name1[PIXEL_4x8]   = x264_pixel_##name2##_4x8##cpu;
#define INIT7_NAME( name1, name2, cpu ) \
    INIT6_NAME( name1, name2, cpu ) \
589 590 591 592
    pixf->name1[PIXEL_4x4]   = x264_pixel_##name2##_4x4##cpu;
#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 )
593
#define INIT6( name, cpu ) INIT6_NAME( name, name, cpu )
594
#define INIT7( name, cpu ) INIT7_NAME( name, name, cpu )
595

596 597 598 599 600
#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;

601
    INIT7( sad, );
602
    INIT7_NAME( sad_aligned, sad, );
603 604 605 606
    INIT7( sad_x3, );
    INIT7( sad_x4, );
    INIT7( ssd, );
    INIT7( satd, );
607 608
    INIT7( satd_x3, );
    INIT7( satd_x4, );
609
    INIT4( sa8d, );
Loren Merritt's avatar
Loren Merritt committed
610
    INIT4( hadamard_ac, );
611
    INIT_ADS( );
612

613 614 615
    pixf->var[PIXEL_16x16] = x264_pixel_var_16x16;
    pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8;

616 617
    pixf->ssim_4x4x2_core = ssim_4x4x2_core;
    pixf->ssim_end4 = ssim_end4;
618

619
#ifdef HAVE_MMX
620 621
    if( cpu&X264_CPU_MMX )
    {
622
        INIT7( ssd, _mmx );
623 624
    }

Laurent Aimar's avatar
Laurent Aimar committed
625 626
    if( cpu&X264_CPU_MMXEXT )
    {
627
        INIT7( sad, _mmxext );
628
        INIT7_NAME( sad_aligned, sad, _mmxext );
629 630 631
        INIT7( sad_x3, _mmxext );
        INIT7( sad_x4, _mmxext );
        INIT7( satd, _mmxext );
632 633
        INIT7( satd_x3, _mmxext );
        INIT7( satd_x4, _mmxext );
Loren Merritt's avatar
Loren Merritt committed
634
        INIT4( hadamard_ac, _mmxext );
635
        INIT_ADS( _mmxext );
636 637
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_mmxext;
        pixf->var[PIXEL_8x8]   = x264_pixel_var_8x8_mmxext;
638 639 640
#ifdef ARCH_X86
        pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmxext;
        pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_mmxext;
641
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_mmxext;
642
        pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_mmxext;
643

644
        if( cpu&X264_CPU_CACHELINE_32 )
645
        {
646 647 648 649 650 651 652 653 654
            INIT5( sad, _cache32_mmxext );
            INIT4( sad_x3, _cache32_mmxext );
            INIT4( sad_x4, _cache32_mmxext );
        }
        else if( cpu&X264_CPU_CACHELINE_64 )
        {
            INIT5( sad, _cache64_mmxext );
            INIT4( sad_x3, _cache64_mmxext );
            INIT4( sad_x4, _cache64_mmxext );
655 656
        }
#else
657
        if( cpu&X264_CPU_CACHELINE_64 )
658 659 660 661 662 663 664 665 666
        {
            pixf->sad[PIXEL_8x16] = x264_pixel_sad_8x16_cache64_mmxext;
            pixf->sad[PIXEL_8x8]  = x264_pixel_sad_8x8_cache64_mmxext;
            pixf->sad[PIXEL_8x4]  = x264_pixel_sad_8x4_cache64_mmxext;
            pixf->sad_x3[PIXEL_8x16] = x264_pixel_sad_x3_8x16_cache64_mmxext;
            pixf->sad_x3[PIXEL_8x8]  = x264_pixel_sad_x3_8x8_cache64_mmxext;
            pixf->sad_x4[PIXEL_8x16] = x264_pixel_sad_x4_8x16_cache64_mmxext;
            pixf->sad_x4[PIXEL_8x8]  = x264_pixel_sad_x4_8x8_cache64_mmxext;
        }
667
#endif
668
        pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_mmxext;
669
        pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_mmxext;
670 671
        pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_mmxext;
        pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_mmxext;
Laurent Aimar's avatar
Laurent Aimar committed
672
    }
673

674
    if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_SLOW) )
675
    {
676 677 678
        INIT2( sad, _sse2 );
        INIT2( sad_x3, _sse2 );
        INIT2( sad_x4, _sse2 );
679 680 681 682
        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _sse2 );
        }
683
        INIT_ADS( _sse2 );
684
        pixf->var[PIXEL_8x8] = x264_pixel_var_8x8_sse2;
685
        pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16_sse2;
686
#ifdef ARCH_X86
687
        if( cpu&X264_CPU_CACHELINE_64 )
688 689 690 691 692 693
        {
            INIT2( sad, _cache64_sse2 );
            INIT2( sad_x3, _cache64_sse2 );
            INIT2( sad_x4, _cache64_sse2 );
        }
#endif
Fiona Glaser's avatar
Fiona Glaser committed
694 695 696 697 698
        if( cpu&X264_CPU_SSE_MISALIGN )
        {
            INIT2( sad_x3, _sse2_misalign );
            INIT2( sad_x4, _sse2_misalign );
        }
699 700 701
    }
    if( cpu&X264_CPU_SSE2 )
    {
Loren Merritt's avatar
Loren Merritt committed
702
        INIT5( ssd, _sse2 );
703 704 705 706 707 708 709 710 711 712 713 714
        if( cpu&X264_CPU_SSE2_IS_FAST )
        {
            INIT6( satd, _sse2 );
            INIT6( satd_x3, _sse2 );
            INIT6( satd_x4, _sse2 );
        }
        else
        {
            INIT5( satd, _sse2 );
            INIT5( satd_x3, _sse2 );
            INIT5( satd_x4, _sse2 );
        }
715
        INIT2_NAME( sad_aligned, sad, _sse2_aligned );
716
        pixf->var[PIXEL_16x16] = x264_pixel_var_16x16_sse2;
717 718
        pixf->ssim_4x4x2_core  = x264_pixel_ssim_4x4x2_core_sse2;
        pixf->ssim_end4        = x264_pixel_ssim_end4_sse2;
719 720
        pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_sse2;
        pixf->sa8d[PIXEL_8x8]   = x264_pixel_sa8d_8x8_sse2;
721
#ifdef ARCH_X86_64
722
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_sse2;
723 724 725
#endif
    }

726 727 728 729 730 731 732 733 734 735 736 737
    if( cpu&X264_CPU_SSE2_IS_FAST && !(cpu&X264_CPU_CACHELINE_64) )
    {
        pixf->sad_aligned[PIXEL_8x16] = x264_pixel_sad_8x16_sse2;
        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;
    }

738
    if( (cpu&X264_CPU_SSE3) && (cpu&X264_CPU_CACHELINE_64) )
739 740 741 742 743 744
    {
        INIT2( sad, _sse3 );
        INIT2( sad_x3, _sse3 );
        INIT2( sad_x4, _sse3 );
    }

745 746
    if( cpu&X264_CPU_SSSE3 )
    {
Fiona Glaser's avatar
Fiona Glaser committed
747 748 749
        INIT7( satd, _ssse3 );
        INIT7( satd_x3, _ssse3 );
        INIT7( satd_x4, _ssse3 );
750 751 752 753
        if( !(cpu&X264_CPU_STACK_MOD4) )
        {
            INIT4( hadamard_ac, _ssse3 );
        }
754
        INIT_ADS( _ssse3 );
755 756
        pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3;
        pixf->sa8d[PIXEL_8x8]  = x264_pixel_sa8d_8x8_ssse3;
757
        pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16_ssse3;
758
        pixf->intra_sad_x3_16x16  = x264_intra_sad_x3_16x16_ssse3;
759 760 761 762
        pixf->intra_satd_x3_8x8c  = x264_intra_satd_x3_8x8c_ssse3;
        pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4_ssse3;
#ifdef ARCH_X86_64
        pixf->intra_sa8d_x3_8x8 = x264_intra_sa8d_x3_8x8_ssse3;
Loren Merritt's avatar
Loren Merritt committed
763
#endif
764
        if( cpu&X264_CPU_CACHELINE_64 )
765 766 767 768 769
        {
            INIT2( sad, _cache64_ssse3 );
            INIT2( sad_x3, _cache64_ssse3 );
            INIT2( sad_x4, _cache64_ssse3 );
        }
770 771
        if( cpu&X264_CPU_PHADD_IS_FAST )
        {
772 773 774
            INIT6( satd, _ssse3_phadd );
            INIT6( satd_x3, _ssse3_phadd );
            INIT6( satd_x4, _ssse3_phadd );
775
        }
776
    }
777 778 779 780 781 782

    if( cpu&X264_CPU_SSE4 )
    {
        pixf->ssd[PIXEL_4x8] = x264_pixel_ssd_4x8_sse4;
        pixf->ssd[PIXEL_4x4] = x264_pixel_ssd_4x4_sse4;
    }
783
#endif //HAVE_MMX
784

785
#ifdef ARCH_PPC
Laurent Aimar's avatar
Laurent Aimar committed
786 787 788 789 790
    if( cpu&X264_CPU_ALTIVEC )
    {
        x264_pixel_altivec_init( pixf );
    }
#endif
791
#ifdef ARCH_UltraSparc
792 793 794
    INIT4( sad, _vis );
    INIT4( sad_x3, _vis );
    INIT4( sad_x4, _vis );
795
#endif
Loren Merritt's avatar
Loren Merritt committed
796 797 798 799 800

    pixf->ads[PIXEL_8x16] =
    pixf->ads[PIXEL_8x4] =
    pixf->ads[PIXEL_4x8] = pixf->ads[PIXEL_16x8];
    pixf->ads[PIXEL_4x4] = pixf->ads[PIXEL_8x8];
Laurent Aimar's avatar
Laurent Aimar committed
801 802
}