yuv2rgb_template.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * software YUV to RGB converter
 *
 * Copyright (C) 2001-2007 Michael Niedermayer
 *           (c) 2010 Konstantin Shishkov
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

24 25 26 27 28
#include <stdint.h>

#include "libavutil/x86/asm.h"
#include "libswscale/swscale_internal.h"

29 30 31 32
#undef MOVNTQ
#undef EMMS
#undef SFENCE

33
#if COMPILE_TEMPLATE_MMXEXT
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#define MOVNTQ "movntq"
#define SFENCE "sfence"
#else
#define MOVNTQ "movq"
#define SFENCE " # nop"
#endif

#define REG_BLUE  "0"
#define REG_RED   "1"
#define REG_GREEN "2"
#define REG_ALPHA "3"

#define YUV2RGB_LOOP(depth)                                          \
    h_size = (c->dstW + 7) & ~7;                                     \
    if (h_size * depth > FFABS(dstStride[0]))                        \
        h_size -= 8;                                                 \
                                                                     \
51
    vshift = c->srcFormat != AV_PIX_FMT_YUV422P;                        \
52 53 54 55 56
                                                                     \
    __asm__ volatile ("pxor %mm4, %mm4\n\t");                        \
    for (y = 0; y < srcSliceH; y++) {                                \
        uint8_t *image    = dst[0] + (y + srcSliceY) * dstStride[0]; \
        const uint8_t *py = src[0] +               y * srcStride[0]; \
57 58
        const uint8_t *pu = src[1] +   (y >> vshift) * srcStride[1]; \
        const uint8_t *pv = src[2] +   (y >> vshift) * srcStride[2]; \
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 110 111 112 113 114 115 116 117 118 119 120 121
        x86_reg index = -h_size / 2;                                 \

#define YUV2RGB_INITIAL_LOAD          \
    __asm__ volatile (                \
        "movq (%5, %0, 2), %%mm6\n\t" \
        "movd    (%2, %0), %%mm0\n\t" \
        "movd    (%3, %0), %%mm1\n\t" \
        "1: \n\t"                     \

/* YUV2RGB core
 * Conversion is performed in usual way:
 * R = Y' * Ycoef + Vred * V'
 * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
 * B = Y' * Ycoef               + Ublue * U'
 *
 * where X' = X * 8 - Xoffset (multiplication is performed to increase
 * precision a bit).
 * Since it operates in YUV420 colorspace, Y component is additionally
 * split into Y1 and Y2 for even and odd pixels.
 *
 * Input:
 * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
 * Output:
 * mm1 - R, mm2 - G, mm0 - B
 */
#define YUV2RGB                                  \
    /* convert Y, U, V into Y1', Y2', U', V' */  \
    "movq      %%mm6, %%mm7\n\t"                 \
    "punpcklbw %%mm4, %%mm0\n\t"                 \
    "punpcklbw %%mm4, %%mm1\n\t"                 \
    "pand     "MANGLE(mmx_00ffw)", %%mm6\n\t"    \
    "psrlw     $8,    %%mm7\n\t"                 \
    "psllw     $3,    %%mm0\n\t"                 \
    "psllw     $3,    %%mm1\n\t"                 \
    "psllw     $3,    %%mm6\n\t"                 \
    "psllw     $3,    %%mm7\n\t"                 \
    "psubsw   "U_OFFSET"(%4), %%mm0\n\t"         \
    "psubsw   "V_OFFSET"(%4), %%mm1\n\t"         \
    "psubw    "Y_OFFSET"(%4), %%mm6\n\t"         \
    "psubw    "Y_OFFSET"(%4), %%mm7\n\t"         \
\
     /* multiply by coefficients */              \
    "movq      %%mm0, %%mm2\n\t"                 \
    "movq      %%mm1, %%mm3\n\t"                 \
    "pmulhw   "UG_COEFF"(%4), %%mm2\n\t"         \
    "pmulhw   "VG_COEFF"(%4), %%mm3\n\t"         \
    "pmulhw   "Y_COEFF" (%4), %%mm6\n\t"         \
    "pmulhw   "Y_COEFF" (%4), %%mm7\n\t"         \
    "pmulhw   "UB_COEFF"(%4), %%mm0\n\t"         \
    "pmulhw   "VR_COEFF"(%4), %%mm1\n\t"         \
    "paddsw    %%mm3, %%mm2\n\t"                 \
    /* now: mm0 = UB, mm1 = VR, mm2 = CG */      \
    /*      mm6 = Y1, mm7 = Y2 */                \
\
    /* produce RGB */                            \
    "movq      %%mm7, %%mm3\n\t"                 \
    "movq      %%mm7, %%mm5\n\t"                 \
    "paddsw    %%mm0, %%mm3\n\t"                 \
    "paddsw    %%mm1, %%mm5\n\t"                 \
    "paddsw    %%mm2, %%mm7\n\t"                 \
    "paddsw    %%mm6, %%mm0\n\t"                 \
    "paddsw    %%mm6, %%mm1\n\t"                 \
    "paddsw    %%mm6, %%mm2\n\t"                 \
122 123

#define RGB_PACK_INTERLEAVE                  \
124
    /* pack and interleave even/odd pixels */    \
125 126
    "packuswb  %%mm1, %%mm0\n\t"                 \
    "packuswb  %%mm5, %%mm3\n\t"                 \
127
    "packuswb  %%mm2, %%mm2\n\t"                 \
128
    "movq      %%mm0, %%mm1\n\n"                 \
129 130
    "packuswb  %%mm7, %%mm7\n\t"                 \
    "punpcklbw %%mm3, %%mm0\n\t"                 \
131
    "punpckhbw %%mm3, %%mm1\n\t"                 \
132 133 134 135 136 137 138 139 140 141
    "punpcklbw %%mm7, %%mm2\n\t"                 \

#define YUV2RGB_ENDLOOP(depth)                   \
    "movq 8 (%5, %0, 2), %%mm6\n\t"              \
    "movd 4 (%3, %0),    %%mm1\n\t"              \
    "movd 4 (%2, %0),    %%mm0\n\t"              \
    "add $"AV_STRINGIFY(depth * 8)", %1\n\t"     \
    "add  $4, %0\n\t"                            \
    "js   1b\n\t"                                \

142
#if COMPILE_TEMPLATE_MMXEXT
143
#undef RGB_PACK24_B_OPERANDS
144
#define RGB_PACK24_B_OPERANDS NAMED_CONSTRAINTS_ARRAY_ADD(mask1101,mask0110,mask0100,mask0010,mask1001)
145
#else
146
#undef RGB_PACK24_B_OPERANDS
147 148 149
#define RGB_PACK24_B_OPERANDS
#endif

150 151 152 153
#define YUV2RGB_OPERANDS                                          \
        : "+r" (index), "+r" (image)                              \
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
          "r" (py - 2*index)                                      \
154 155
          NAMED_CONSTRAINTS_ADD(mmx_00ffw,pb_03,pb_07,mmx_redmask,pb_e0) \
          RGB_PACK24_B_OPERANDS                                   \
156
        : "memory"                                                \
157 158 159 160 161 162 163
        );                                                        \
    }                                                             \

#define YUV2RGB_OPERANDS_ALPHA                                    \
        : "+r" (index), "+r" (image)                              \
        : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
          "r" (py - 2*index), "r" (pa - 2*index)                  \
164
          NAMED_CONSTRAINTS_ADD(mmx_00ffw)                        \
165
        : "memory"                                                \
166 167 168 169
        );                                                        \
    }                                                             \

#define YUV2RGB_ENDFUNC                          \
170 171
    __asm__ volatile (SFENCE"\n\t"               \
                    "emms    \n\t");             \
172 173
    return srcSliceH;                            \

174 175
#define IF0(x)
#define IF1(x) x
176

177
#define RGB_PACK16(gmask, is15)                  \
178 179
    "pand      "MANGLE(mmx_redmask)", %%mm0\n\t" \
    "pand      "MANGLE(mmx_redmask)", %%mm1\n\t" \
180 181 182
    "movq      %%mm2,     %%mm3\n\t"             \
    "psllw   $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
    "psrlw   $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
183
    "psrlw     $3,        %%mm0\n\t"             \
184 185 186
    IF##is15("psrlw  $1,  %%mm1\n\t")            \
    "pand "MANGLE(pb_e0)", %%mm2\n\t"            \
    "pand "MANGLE(gmask)", %%mm3\n\t"            \
187
    "por       %%mm2,     %%mm0\n\t"             \
188 189 190 191
    "por       %%mm3,     %%mm1\n\t"             \
    "movq      %%mm0,     %%mm2\n\t"             \
    "punpcklbw %%mm1,     %%mm0\n\t"             \
    "punpckhbw %%mm1,     %%mm2\n\t"             \
192
    MOVNTQ "   %%mm0,      (%1)\n\t"             \
193
    MOVNTQ "   %%mm2,     8(%1)\n\t"             \
194 195 196 197 198 199

#define DITHER_RGB                               \
    "paddusb "BLUE_DITHER"(%4),  %%mm0\n\t"      \
    "paddusb "GREEN_DITHER"(%4), %%mm2\n\t"      \
    "paddusb "RED_DITHER"(%4),   %%mm1\n\t"      \

200
#if !COMPILE_TEMPLATE_MMXEXT
201 202 203 204 205
static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
206
    int y, h_size, vshift;
207 208 209 210 211 212 213 214 215 216 217

    YUV2RGB_LOOP(2)

#ifdef DITHER1XBPP
        c->blueDither  = ff_dither8[y       & 1];
        c->greenDither = ff_dither8[y       & 1];
        c->redDither   = ff_dither8[(y + 1) & 1];
#endif

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
218
        RGB_PACK_INTERLEAVE
219 220 221
#ifdef DITHER1XBPP
        DITHER_RGB
#endif
222
        RGB_PACK16(pb_03, 1)
223 224 225 226 227 228 229 230 231 232 233

    YUV2RGB_ENDLOOP(2)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
234
    int y, h_size, vshift;
235 236 237 238 239 240 241 242 243 244 245

    YUV2RGB_LOOP(2)

#ifdef DITHER1XBPP
        c->blueDither  = ff_dither8[y       & 1];
        c->greenDither = ff_dither4[y       & 1];
        c->redDither   = ff_dither8[(y + 1) & 1];
#endif

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
246
        RGB_PACK_INTERLEAVE
247 248 249
#ifdef DITHER1XBPP
        DITHER_RGB
#endif
250
        RGB_PACK16(pb_07, 0)
251 252 253 254 255

    YUV2RGB_ENDLOOP(2)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}
256
#endif /* !COMPILE_TEMPLATE_MMXEXT */
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271
#define RGB_PACK24(blue, red)\
    "packuswb  %%mm3,      %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
    "packuswb  %%mm5,      %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
    "packuswb  %%mm7,      %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
    "movq      %%mm"red",  %%mm3 \n"\
    "movq      %%mm"blue", %%mm6 \n"\
    "psrlq     $32,        %%mm"red" \n" /* R1 R3 R5 R7 */\
    "punpcklbw %%mm2,      %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
    "punpcklbw %%mm"red",  %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
    "movq      %%mm3,      %%mm5 \n"\
    "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
    "punpcklwd %%mm6,      %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
    "punpckhwd %%mm6,      %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
    RGB_PACK24_B
272

273
#if COMPILE_TEMPLATE_MMXEXT
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
#undef RGB_PACK24_B
#define RGB_PACK24_B\
    "pshufw    $0xc6,  %%mm2, %%mm1 \n"\
    "pshufw    $0x84,  %%mm3, %%mm6 \n"\
    "pshufw    $0x38,  %%mm5, %%mm7 \n"\
    "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
    "movq      %%mm1,         %%mm0 \n"\
    "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
    "movq      %%mm1,         %%mm2 \n"\
    "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
    "psrlq       $48,         %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
    "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
    "psllq       $32,         %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
    "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
    "por       %%mm3,         %%mm1 \n"\
    "por       %%mm6,         %%mm0 \n"\
    "por       %%mm5,         %%mm1 \n"\
    "por       %%mm7,         %%mm2 \n"\
    MOVNTQ"    %%mm0,          (%1) \n"\
    MOVNTQ"    %%mm1,         8(%1) \n"\
    MOVNTQ"    %%mm2,        16(%1) \n"\

#else
#undef RGB_PACK24_B
#define RGB_PACK24_B\
    "movd      %%mm3,       (%1) \n" /* R0 G0 B0 R1 */\
    "movd      %%mm2,      4(%1) \n" /* G1 B1 */\
    "psrlq     $32,        %%mm3 \n"\
    "psrlq     $16,        %%mm2 \n"\
    "movd      %%mm3,      6(%1) \n" /* R2 G2 B2 R3 */\
    "movd      %%mm2,     10(%1) \n" /* G3 B3 */\
    "psrlq     $16,        %%mm2 \n"\
    "movd      %%mm5,     12(%1) \n" /* R4 G4 B4 R5 */\
    "movd      %%mm2,     16(%1) \n" /* G5 B5 */\
    "psrlq     $32,        %%mm5 \n"\
    "movd      %%mm2,     20(%1) \n" /* -- -- G7 B7 */\
    "movd      %%mm5,     18(%1) \n" /* R6 G6 B6 R7 */\

#endif
318 319 320 321 322 323

static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
324
    int y, h_size, vshift;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

    YUV2RGB_LOOP(3)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK24(REG_BLUE, REG_RED)

    YUV2RGB_ENDLOOP(3)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
342
    int y, h_size, vshift;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

    YUV2RGB_LOOP(3)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
        RGB_PACK24(REG_RED, REG_BLUE)

    YUV2RGB_ENDLOOP(3)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}


#define SET_EMPTY_ALPHA                                                      \
    "pcmpeqd   %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \

#define LOAD_ALPHA                                   \
    "movq      (%6, %0, 2),     %%mm"REG_ALPHA"\n\t" \

#define RGB_PACK32(red, green, blue, alpha)  \
    "movq      %%mm"blue",  %%mm5\n\t"       \
    "movq      %%mm"red",   %%mm6\n\t"       \
    "punpckhbw %%mm"green", %%mm5\n\t"       \
    "punpcklbw %%mm"green", %%mm"blue"\n\t"  \
    "punpckhbw %%mm"alpha", %%mm6\n\t"       \
    "punpcklbw %%mm"alpha", %%mm"red"\n\t"   \
    "movq      %%mm"blue",  %%mm"green"\n\t" \
    "movq      %%mm5,       %%mm"alpha"\n\t" \
    "punpcklwd %%mm"red",   %%mm"blue"\n\t"  \
    "punpckhwd %%mm"red",   %%mm"green"\n\t" \
    "punpcklwd %%mm6,       %%mm5\n\t"       \
    "punpckhwd %%mm6,       %%mm"alpha"\n\t" \
    MOVNTQ "   %%mm"blue",   0(%1)\n\t"      \
    MOVNTQ "   %%mm"green",  8(%1)\n\t"      \
    MOVNTQ "   %%mm5,       16(%1)\n\t"      \
    MOVNTQ "   %%mm"alpha", 24(%1)\n\t"      \

380
#if !COMPILE_TEMPLATE_MMXEXT
381 382 383 384 385
static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
386
    int y, h_size, vshift;
387 388 389 390 391

    YUV2RGB_LOOP(4)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
392
        RGB_PACK_INTERLEAVE
393 394 395 396 397 398 399 400
        SET_EMPTY_ALPHA
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

401
#if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
402 403 404 405 406
static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
                                        int srcStride[],
                                        int srcSliceY, int srcSliceH,
                                        uint8_t *dst[], int dstStride[])
{
407
    int y, h_size, vshift;
408 409 410 411 412 413

    YUV2RGB_LOOP(4)

        const uint8_t *pa = src[3] + y * srcStride[3];
        YUV2RGB_INITIAL_LOAD
        YUV2RGB
414
        RGB_PACK_INTERLEAVE
415 416 417 418 419 420 421
        LOAD_ALPHA
        RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS_ALPHA
    YUV2RGB_ENDFUNC
}
422
#endif
423 424 425 426 427 428

static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
                                       int srcStride[],
                                       int srcSliceY, int srcSliceH,
                                       uint8_t *dst[], int dstStride[])
{
429
    int y, h_size, vshift;
430 431 432 433 434

    YUV2RGB_LOOP(4)

        YUV2RGB_INITIAL_LOAD
        YUV2RGB
435
        RGB_PACK_INTERLEAVE
436 437 438 439 440 441 442 443
        SET_EMPTY_ALPHA
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS
    YUV2RGB_ENDFUNC
}

444
#if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
445 446 447 448 449
static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
                                        int srcStride[],
                                        int srcSliceY, int srcSliceH,
                                        uint8_t *dst[], int dstStride[])
{
450
    int y, h_size, vshift;
451 452 453 454 455 456

    YUV2RGB_LOOP(4)

        const uint8_t *pa = src[3] + y * srcStride[3];
        YUV2RGB_INITIAL_LOAD
        YUV2RGB
457
        RGB_PACK_INTERLEAVE
458 459 460 461 462 463 464
        LOAD_ALPHA
        RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)

    YUV2RGB_ENDLOOP(4)
    YUV2RGB_OPERANDS_ALPHA
    YUV2RGB_ENDFUNC
}
465 466
#endif

467
#endif /* !COMPILE_TEMPLATE_MMXEXT */