pixel.c 14.9 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * pixel.c: h264 encoder
 *****************************************************************************
 * Copyright (C) 2003 Laurent Aimar
 * $Id: pixel.c,v 1.1 2004/06/03 19:27:07 fenrir Exp $
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

24 25 26 27 28
#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#include <inttypes.h>
#endif
Laurent Aimar's avatar
Laurent Aimar committed
29 30
#include <stdlib.h>
#include <string.h>
31
#include <stdarg.h>
Laurent Aimar's avatar
Laurent Aimar committed
32

33
#include "x264.h"
Laurent Aimar's avatar
Laurent Aimar committed
34
#include "pixel.h"
35
#include "clip1.h"
Laurent Aimar's avatar
Laurent Aimar committed
36 37 38 39

#ifdef HAVE_MMXEXT
#   include "i386/pixel.h"
#endif
40
#ifdef ARCH_PPC
Laurent Aimar's avatar
Laurent Aimar committed
41 42
#   include "ppc/pixel.h"
#endif
43 44 45
#ifdef ARCH_UltraSparc
#   include "sparc/pixel.h"
#endif
Laurent Aimar's avatar
Laurent Aimar committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


/****************************************************************************
 * 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;                                   \
}


PIXEL_SAD_C( pixel_sad_16x16, 16, 16 )
PIXEL_SAD_C( pixel_sad_16x8,  16,  8 )
PIXEL_SAD_C( pixel_sad_8x16,   8, 16 )
PIXEL_SAD_C( pixel_sad_8x8,    8,  8 )
PIXEL_SAD_C( pixel_sad_8x4,    8,  4 )
PIXEL_SAD_C( pixel_sad_4x8,    4,  8 )
PIXEL_SAD_C( pixel_sad_4x4,    4,  4 )

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

/****************************************************************************
 * 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;                                   \
}

PIXEL_SSD_C( pixel_ssd_16x16, 16, 16 )
PIXEL_SSD_C( pixel_ssd_16x8,  16,  8 )
PIXEL_SSD_C( pixel_ssd_8x16,   8, 16 )
PIXEL_SSD_C( pixel_ssd_8x8,    8,  8 )
PIXEL_SSD_C( pixel_ssd_8x4,    8,  4 )
PIXEL_SSD_C( pixel_ssd_4x8,    4,  8 )
PIXEL_SSD_C( pixel_ssd_4x4,    4,  4 )


110 111
static inline void pixel_sub_wxh( int16_t *diff, int i_size,
                                  uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 )
Laurent Aimar's avatar
Laurent Aimar committed
112 113
{
    int y, x;
114
    for( y = 0; y < i_size; y++ )
Laurent Aimar's avatar
Laurent Aimar committed
115
    {
116
        for( x = 0; x < i_size; x++ )
Laurent Aimar's avatar
Laurent Aimar committed
117
        {
118
            diff[x + y*i_size] = pix1[x] - pix2[x];
Laurent Aimar's avatar
Laurent Aimar committed
119 120 121 122 123 124
        }
        pix1 += i_pix1;
        pix2 += i_pix2;
    }
}

125 126 127 128

/****************************************************************************
 * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
129 130 131 132 133 134 135 136 137 138 139 140 141
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];
    int16_t diff[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 )
        {
            int d;

142
            pixel_sub_wxh( (int16_t*)diff, 4, &pix1[x], i_pix1, &pix2[x], i_pix2 );
Laurent Aimar's avatar
Laurent Aimar committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

            for( d = 0; d < 4; d++ )
            {
                int s01, s23;
                int d01, d23;

                s01 = diff[d][0] + diff[d][1]; s23 = diff[d][2] + diff[d][3];
                d01 = diff[d][0] - diff[d][1]; d23 = diff[d][2] - diff[d][3];

                tmp[d][0] = s01 + s23;
                tmp[d][1] = s01 - s23;
                tmp[d][2] = d01 - d23;
                tmp[d][3] = d01 + d23;
            }
            for( d = 0; d < 4; d++ )
            {
                int s01, s23;
                int d01, d23;

                s01 = tmp[0][d] + tmp[1][d]; s23 = tmp[2][d] + tmp[3][d];
                d01 = tmp[0][d] - tmp[1][d]; d23 = tmp[2][d] - tmp[3][d];

                i_satd += abs( s01 + s23 ) + abs( s01 - s23 ) + abs( d01 - d23 ) + abs( d01 + d23 );
            }

        }
        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 ); \
}
PIXEL_SATD_C( pixel_satd_16x16, 16, 16 )
PIXEL_SATD_C( pixel_satd_16x8,  16, 8 )
PIXEL_SATD_C( pixel_satd_8x16,  8, 16 )
PIXEL_SATD_C( pixel_satd_8x8,   8, 8 )
PIXEL_SATD_C( pixel_satd_8x4,   8, 4 )
PIXEL_SATD_C( pixel_satd_4x8,   4, 8 )
PIXEL_SATD_C( pixel_satd_4x4,   4, 4 )


190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/****************************************************************************
 * pixel_sa8d_WxH: sum of 8x8 Hadamard transformed differences
 ****************************************************************************/
#define SA8D_1D {\
    const int a0 = SRC(0) + SRC(4);\
    const int a4 = SRC(0) - SRC(4);\
    const int a1 = SRC(1) + SRC(5);\
    const int a5 = SRC(1) - SRC(5);\
    const int a2 = SRC(2) + SRC(6);\
    const int a6 = SRC(2) - SRC(6);\
    const int a3 = SRC(3) + SRC(7);\
    const int a7 = SRC(3) - SRC(7);\
    const int b0 = a0 + a2;\
    const int b2 = a0 - a2;\
    const int b1 = a1 + a3;\
    const int b3 = a1 - a3;\
    const int b4 = a4 + a6;\
    const int b6 = a4 - a6;\
    const int b5 = a5 + a7;\
    const int b7 = a5 - a7;\
    DST(0, b0 + b1);\
    DST(1, b0 - b1);\
    DST(2, b2 + b3);\
    DST(3, b2 - b3);\
    DST(4, b4 + b5);\
    DST(5, b4 - b5);\
    DST(6, b6 + b7);\
    DST(7, b6 - b7);\
}

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;
            pixel_sub_wxh( (int16_t*)diff, 8, pix1+x, i_pix1, pix2+x, i_pix2 );

#define SRC(x)     diff[i][x]
#define DST(x,rhs) diff[i][x] = (rhs)
            for( i = 0; i < 8; i++ )
                SA8D_1D
#undef SRC
#undef DST

#define SRC(x)     diff[x][i]
#define DST(x,rhs) i_satd += abs(rhs)
            for( i = 0; i < 8; i++ )
                SA8D_1D
#undef SRC
#undef DST
        }
        pix1 += 8 * i_pix1;
        pix2 += 8 * i_pix2;
    }

    return i_satd;
}

#define PIXEL_SA8D_C( width, height ) \
static int pixel_sa8d_##width##x##height( uint8_t *pix1, int i_stride_pix1, \
                 uint8_t *pix2, int i_stride_pix2 ) \
{ \
    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 )


Laurent Aimar's avatar
Laurent Aimar committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static inline void pixel_avg_wxh( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int width, int height )
{
    int x, y;
    for( y = 0; y < height; y++ )
    {
        for( x = 0; x < width; x++ )
        {
            dst[x] = ( dst[x] + src[x] + 1 ) >> 1;
        }
        dst += i_dst;
        src += i_src;
    }
}


#define PIXEL_AVG_C( name, width, height ) \
static void name( uint8_t *pix1, int i_stride_pix1, \
                  uint8_t *pix2, int i_stride_pix2 ) \
{ \
    pixel_avg_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ); \
}
PIXEL_AVG_C( pixel_avg_16x16, 16, 16 )
PIXEL_AVG_C( pixel_avg_16x8,  16, 8 )
PIXEL_AVG_C( pixel_avg_8x16,  8, 16 )
PIXEL_AVG_C( pixel_avg_8x8,   8, 8 )
PIXEL_AVG_C( pixel_avg_8x4,   8, 4 )
PIXEL_AVG_C( pixel_avg_4x8,   4, 8 )
PIXEL_AVG_C( pixel_avg_4x4,   4, 4 )
295 296 297 298 299 300 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
PIXEL_AVG_C( pixel_avg_4x2,   4, 2 )
PIXEL_AVG_C( pixel_avg_2x4,   2, 4 )
PIXEL_AVG_C( pixel_avg_2x2,   2, 2 )


/* Implicit weighted bipred only:
 * assumes log2_denom = 5, offset = 0, weight1 + weight2 = 64 */
#define op_scale2(x) dst[x] = x264_clip_uint8( (dst[x]*i_weight1 + src[x]*i_weight2 + (1<<5)) >> 6 )
static inline void pixel_avg_weight_wxh( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int width, int height, int i_weight1 ){
    int y;
    const int i_weight2 = 64 - i_weight1;
    for(y=0; y<height; y++, dst += i_dst, src += i_src){
        op_scale2(0);
        op_scale2(1);
        if(width==2) continue;
        op_scale2(2);
        op_scale2(3);
        if(width==4) continue;
        op_scale2(4);
        op_scale2(5);
        op_scale2(6);
        op_scale2(7);
        if(width==8) continue;
        op_scale2(8);
        op_scale2(9);
        op_scale2(10);
        op_scale2(11);
        op_scale2(12);
        op_scale2(13);
        op_scale2(14);
        op_scale2(15);
    }
}

#define PIXEL_AVG_WEIGHT_C( width, height ) \
static void pixel_avg_weight_##width##x##height( \
                uint8_t *pix1, int i_stride_pix1, \
                uint8_t *pix2, int i_stride_pix2, int i_weight1 ) \
{ \
    pixel_avg_weight_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height, i_weight1 ); \
}

PIXEL_AVG_WEIGHT_C(16,16)
PIXEL_AVG_WEIGHT_C(16,8)
PIXEL_AVG_WEIGHT_C(8,16)
PIXEL_AVG_WEIGHT_C(8,8)
PIXEL_AVG_WEIGHT_C(8,4)
PIXEL_AVG_WEIGHT_C(4,8)
PIXEL_AVG_WEIGHT_C(4,4)
PIXEL_AVG_WEIGHT_C(4,2)
PIXEL_AVG_WEIGHT_C(2,4)
PIXEL_AVG_WEIGHT_C(2,2)
#undef op_scale2
#undef PIXEL_AVG_WEIGHT_C
Laurent Aimar's avatar
Laurent Aimar committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362

/****************************************************************************
 * x264_pixel_init:
 ****************************************************************************/
void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
{
    pixf->sad[PIXEL_16x16] = pixel_sad_16x16;
    pixf->sad[PIXEL_16x8]  = pixel_sad_16x8;
    pixf->sad[PIXEL_8x16]  = pixel_sad_8x16;
    pixf->sad[PIXEL_8x8]   = pixel_sad_8x8;
    pixf->sad[PIXEL_8x4]   = pixel_sad_8x4;
    pixf->sad[PIXEL_4x8]   = pixel_sad_4x8;
    pixf->sad[PIXEL_4x4]   = pixel_sad_4x4;

363 364 365 366 367 368 369 370
    pixf->ssd[PIXEL_16x16] = pixel_ssd_16x16;
    pixf->ssd[PIXEL_16x8]  = pixel_ssd_16x8;
    pixf->ssd[PIXEL_8x16]  = pixel_ssd_8x16;
    pixf->ssd[PIXEL_8x8]   = pixel_ssd_8x8;
    pixf->ssd[PIXEL_8x4]   = pixel_ssd_8x4;
    pixf->ssd[PIXEL_4x8]   = pixel_ssd_4x8;
    pixf->ssd[PIXEL_4x4]   = pixel_ssd_4x4;

Laurent Aimar's avatar
Laurent Aimar committed
371 372 373 374 375 376 377 378
    pixf->satd[PIXEL_16x16]= pixel_satd_16x16;
    pixf->satd[PIXEL_16x8] = pixel_satd_16x8;
    pixf->satd[PIXEL_8x16] = pixel_satd_8x16;
    pixf->satd[PIXEL_8x8]  = pixel_satd_8x8;
    pixf->satd[PIXEL_8x4]  = pixel_satd_8x4;
    pixf->satd[PIXEL_4x8]  = pixel_satd_4x8;
    pixf->satd[PIXEL_4x4]  = pixel_satd_4x4;

379 380 381 382 383
    pixf->sa8d[PIXEL_16x16]= pixel_sa8d_16x16;
    pixf->sa8d[PIXEL_16x8] = pixel_sa8d_16x8;
    pixf->sa8d[PIXEL_8x16] = pixel_sa8d_8x16;
    pixf->sa8d[PIXEL_8x8]  = pixel_sa8d_8x8;

Laurent Aimar's avatar
Laurent Aimar committed
384 385 386 387 388 389 390
    pixf->avg[PIXEL_16x16]= pixel_avg_16x16;
    pixf->avg[PIXEL_16x8] = pixel_avg_16x8;
    pixf->avg[PIXEL_8x16] = pixel_avg_8x16;
    pixf->avg[PIXEL_8x8]  = pixel_avg_8x8;
    pixf->avg[PIXEL_8x4]  = pixel_avg_8x4;
    pixf->avg[PIXEL_4x8]  = pixel_avg_4x8;
    pixf->avg[PIXEL_4x4]  = pixel_avg_4x4;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    pixf->avg[PIXEL_4x2]  = pixel_avg_4x2;
    pixf->avg[PIXEL_2x4]  = pixel_avg_2x4;
    pixf->avg[PIXEL_2x2]  = pixel_avg_2x2;
    
    pixf->avg_weight[PIXEL_16x16]= pixel_avg_weight_16x16;
    pixf->avg_weight[PIXEL_16x8] = pixel_avg_weight_16x8;
    pixf->avg_weight[PIXEL_8x16] = pixel_avg_weight_8x16;
    pixf->avg_weight[PIXEL_8x8]  = pixel_avg_weight_8x8;
    pixf->avg_weight[PIXEL_8x4]  = pixel_avg_weight_8x4;
    pixf->avg_weight[PIXEL_4x8]  = pixel_avg_weight_4x8;
    pixf->avg_weight[PIXEL_4x4]  = pixel_avg_weight_4x4;
    pixf->avg_weight[PIXEL_4x2]  = pixel_avg_weight_4x2;
    pixf->avg_weight[PIXEL_2x4]  = pixel_avg_weight_2x4;
    pixf->avg_weight[PIXEL_2x2]  = pixel_avg_weight_2x2;

Laurent Aimar's avatar
Laurent Aimar committed
406 407 408 409 410 411 412 413 414 415 416
#ifdef HAVE_MMXEXT
    if( cpu&X264_CPU_MMXEXT )
    {
        pixf->sad[PIXEL_16x16] = x264_pixel_sad_16x16_mmxext;
        pixf->sad[PIXEL_16x8 ] = x264_pixel_sad_16x8_mmxext;
        pixf->sad[PIXEL_8x16 ] = x264_pixel_sad_8x16_mmxext;
        pixf->sad[PIXEL_8x8  ] = x264_pixel_sad_8x8_mmxext;
        pixf->sad[PIXEL_8x4  ] = x264_pixel_sad_8x4_mmxext;
        pixf->sad[PIXEL_4x8  ] = x264_pixel_sad_4x8_mmxext;
        pixf->sad[PIXEL_4x4]   = x264_pixel_sad_4x4_mmxext;

417 418 419 420 421 422 423 424
        pixf->ssd[PIXEL_16x16] = x264_pixel_ssd_16x16_mmxext;
        pixf->ssd[PIXEL_16x8]  = x264_pixel_ssd_16x8_mmxext;
        pixf->ssd[PIXEL_8x16]  = x264_pixel_ssd_8x16_mmxext;
        pixf->ssd[PIXEL_8x8]   = x264_pixel_ssd_8x8_mmxext;
        pixf->ssd[PIXEL_8x4]   = x264_pixel_ssd_8x4_mmxext;
        pixf->ssd[PIXEL_4x8]   = x264_pixel_ssd_4x8_mmxext;
        pixf->ssd[PIXEL_4x4]   = x264_pixel_ssd_4x4_mmxext;
  
Laurent Aimar's avatar
Laurent Aimar committed
425 426 427 428 429 430 431 432 433
        pixf->satd[PIXEL_16x16]= x264_pixel_satd_16x16_mmxext;
        pixf->satd[PIXEL_16x8] = x264_pixel_satd_16x8_mmxext;
        pixf->satd[PIXEL_8x16] = x264_pixel_satd_8x16_mmxext;
        pixf->satd[PIXEL_8x8]  = x264_pixel_satd_8x8_mmxext;
        pixf->satd[PIXEL_8x4]  = x264_pixel_satd_8x4_mmxext;
        pixf->satd[PIXEL_4x8]  = x264_pixel_satd_4x8_mmxext;
        pixf->satd[PIXEL_4x4]  = x264_pixel_satd_4x4_mmxext;
    }
#endif
434
#ifdef ARCH_PPC
Laurent Aimar's avatar
Laurent Aimar committed
435 436 437 438 439
    if( cpu&X264_CPU_ALTIVEC )
    {
        x264_pixel_altivec_init( pixf );
    }
#endif
440
#ifdef ARCH_UltraSparc
Laurent Aimar's avatar
Laurent Aimar committed
441 442 443 444
      pixf->sad[PIXEL_8x8]   = x264_pixel_sad_8x8_vis;
      pixf->sad[PIXEL_8x16]  = x264_pixel_sad_8x16_vis;
      pixf->sad[PIXEL_16x8]  = x264_pixel_sad_16x8_vis;
      pixf->sad[PIXEL_16x16] = x264_pixel_sad_16x16_vis;
445
#endif
Laurent Aimar's avatar
Laurent Aimar committed
446 447
}