swscale_template.c 72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of Libav.
 *
 * Libav 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.
 *
 * Libav 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 Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22 23 24 25
#include <stdint.h>

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

26 27 28 29
#undef REAL_MOVNTQ
#undef MOVNTQ
#undef PREFETCH

30
#if COMPILE_TEMPLATE_MMXEXT
31 32 33 34 35
#define PREFETCH "prefetchnta"
#else
#define PREFETCH  " # nop"
#endif

36
#if COMPILE_TEMPLATE_MMXEXT
37 38 39 40 41 42 43 44
#define REAL_MOVNTQ(a,b) "movntq " #a ", " #b " \n\t"
#else
#define REAL_MOVNTQ(a,b) "movq " #a ", " #b " \n\t"
#endif
#define MOVNTQ(a,b)  REAL_MOVNTQ(a,b)

#define YSCALEYUV2PACKEDX_UV \
    __asm__ volatile(\
45
        "xor                %%"FF_REG_a", %%"FF_REG_a"  \n\t"\
46 47 48
        ".p2align                      4                \n\t"\
        "nop                                            \n\t"\
        "1:                                             \n\t"\
49 50
        "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"FF_REG_d"  \n\t"\
        "mov              (%%"FF_REG_d"), %%"FF_REG_S"  \n\t"\
51 52 53 54
        "movq      "VROUNDER_OFFSET"(%0), %%mm3         \n\t"\
        "movq                      %%mm3, %%mm4         \n\t"\
        ".p2align                      4                \n\t"\
        "2:                                             \n\t"\
55 56 57 58 59 60
        "movq            8(%%"FF_REG_d"), %%mm0         \n\t" /* filterCoeff */\
        "movq (%%"FF_REG_S", %%"FF_REG_a"), %%mm2       \n\t" /* UsrcData */\
        "add                       %6, %%"FF_REG_S"     \n\t" \
        "movq (%%"FF_REG_S", %%"FF_REG_a"), %%mm5       \n\t" /* VsrcData */\
        "add                      $16, %%"FF_REG_d"     \n\t"\
        "mov           (%%"FF_REG_d"), %%"FF_REG_S"     \n\t"\
61 62 63 64
        "pmulhw                    %%mm0, %%mm2         \n\t"\
        "pmulhw                    %%mm0, %%mm5         \n\t"\
        "paddw                     %%mm2, %%mm3         \n\t"\
        "paddw                     %%mm5, %%mm4         \n\t"\
65
        "test               %%"FF_REG_S", %%"FF_REG_S"  \n\t"\
66 67 68
        " jnz                         2b                \n\t"\

#define YSCALEYUV2PACKEDX_YA(offset,coeff,src1,src2,dst1,dst2) \
69 70
    "lea                "offset"(%0), %%"FF_REG_d"  \n\t"\
    "mov              (%%"FF_REG_d"), %%"FF_REG_S"  \n\t"\
71 72 73 74
    "movq      "VROUNDER_OFFSET"(%0), "#dst1"       \n\t"\
    "movq                    "#dst1", "#dst2"       \n\t"\
    ".p2align                      4                \n\t"\
    "2:                                             \n\t"\
75 76 77 78 79
    "movq            8(%%"FF_REG_d"), "#coeff"      \n\t" /* filterCoeff */\
    "movq  (%%"FF_REG_S", %%"FF_REG_a", 2), "#src1" \n\t" /* Y1srcData */\
    "movq 8(%%"FF_REG_S", %%"FF_REG_a", 2), "#src2" \n\t" /* Y2srcData */\
    "add                         $16, %%"FF_REG_d"  \n\t"\
    "mov              (%%"FF_REG_d"), %%"FF_REG_S"  \n\t"\
80 81 82 83
    "pmulhw                 "#coeff", "#src1"       \n\t"\
    "pmulhw                 "#coeff", "#src2"       \n\t"\
    "paddw                   "#src1", "#dst1"       \n\t"\
    "paddw                   "#src2", "#dst2"       \n\t"\
84
    "test               %%"FF_REG_S", %%"FF_REG_S"  \n\t"\
85 86 87 88 89 90 91 92 93
    " jnz                         2b                \n\t"\

#define YSCALEYUV2PACKEDX \
    YSCALEYUV2PACKEDX_UV \
    YSCALEYUV2PACKEDX_YA(LUM_MMX_FILTER_OFFSET,%%mm0,%%mm2,%%mm5,%%mm1,%%mm7) \

#define YSCALEYUV2PACKEDX_END                     \
        :: "r" (&c->redDither),                   \
            "m" (dummy), "m" (dummy), "m" (dummy),\
94
            "r" (dest), "m" (dstW_reg), "m"(uv_off) \
95
        : "%"FF_REG_a, "%"FF_REG_d, "%"FF_REG_S   \
96 97 98 99
    );

#define YSCALEYUV2PACKEDX_ACCURATE_UV \
    __asm__ volatile(\
100
        "xor %%"FF_REG_a", %%"FF_REG_a"                 \n\t"\
101 102 103
        ".p2align                      4                \n\t"\
        "nop                                            \n\t"\
        "1:                                             \n\t"\
104 105
        "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"FF_REG_d"  \n\t"\
        "mov              (%%"FF_REG_d"), %%"FF_REG_S"  \n\t"\
106 107 108 109 110 111
        "pxor                      %%mm4, %%mm4         \n\t"\
        "pxor                      %%mm5, %%mm5         \n\t"\
        "pxor                      %%mm6, %%mm6         \n\t"\
        "pxor                      %%mm7, %%mm7         \n\t"\
        ".p2align                      4                \n\t"\
        "2:                                             \n\t"\
112 113 114 115 116
        "movq  (%%"FF_REG_S", %%"FF_REG_a"), %%mm0      \n\t" /* UsrcData */\
        "add                             %6, %%"FF_REG_S" \n\t" \
        "movq  (%%"FF_REG_S", %%"FF_REG_a"), %%mm2      \n\t" /* VsrcData */\
        "mov "STR(APCK_PTR2)"(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
        "movq  (%%"FF_REG_S", %%"FF_REG_a"), %%mm1      \n\t" /* UsrcData */\
117 118 119
        "movq                      %%mm0, %%mm3         \n\t"\
        "punpcklwd                 %%mm1, %%mm0         \n\t"\
        "punpckhwd                 %%mm1, %%mm3         \n\t"\
120
        "movq "STR(APCK_COEF)"(%%"FF_REG_d"),%%mm1      \n\t" /* filterCoeff */\
121 122 123 124
        "pmaddwd                   %%mm1, %%mm0         \n\t"\
        "pmaddwd                   %%mm1, %%mm3         \n\t"\
        "paddd                     %%mm0, %%mm4         \n\t"\
        "paddd                     %%mm3, %%mm5         \n\t"\
125 126 127 128 129
        "add                          %6, %%"FF_REG_S"  \n\t"\
        "movq  (%%"FF_REG_S", %%"FF_REG_a"), %%mm3      \n\t" /* VsrcData */\
        "mov "STR(APCK_SIZE)"(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
        "add           $"STR(APCK_SIZE)", %%"FF_REG_d"  \n\t"\
        "test               %%"FF_REG_S", %%"FF_REG_S"  \n\t"\
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        "movq                      %%mm2, %%mm0         \n\t"\
        "punpcklwd                 %%mm3, %%mm2         \n\t"\
        "punpckhwd                 %%mm3, %%mm0         \n\t"\
        "pmaddwd                   %%mm1, %%mm2         \n\t"\
        "pmaddwd                   %%mm1, %%mm0         \n\t"\
        "paddd                     %%mm2, %%mm6         \n\t"\
        "paddd                     %%mm0, %%mm7         \n\t"\
        " jnz                         2b                \n\t"\
        "psrad                       $16, %%mm4         \n\t"\
        "psrad                       $16, %%mm5         \n\t"\
        "psrad                       $16, %%mm6         \n\t"\
        "psrad                       $16, %%mm7         \n\t"\
        "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
        "packssdw                  %%mm5, %%mm4         \n\t"\
        "packssdw                  %%mm7, %%mm6         \n\t"\
        "paddw                     %%mm0, %%mm4         \n\t"\
        "paddw                     %%mm0, %%mm6         \n\t"\
        "movq                      %%mm4, "U_TEMP"(%0)  \n\t"\
        "movq                      %%mm6, "V_TEMP"(%0)  \n\t"\

#define YSCALEYUV2PACKEDX_ACCURATE_YA(offset) \
151 152
    "lea                "offset"(%0), %%"FF_REG_d"  \n\t"\
    "mov              (%%"FF_REG_d"), %%"FF_REG_S"  \n\t"\
153 154 155 156 157 158
    "pxor                      %%mm1, %%mm1         \n\t"\
    "pxor                      %%mm5, %%mm5         \n\t"\
    "pxor                      %%mm7, %%mm7         \n\t"\
    "pxor                      %%mm6, %%mm6         \n\t"\
    ".p2align                      4                \n\t"\
    "2:                                             \n\t"\
159 160 161 162
    "movq  (%%"FF_REG_S", %%"FF_REG_a", 2), %%mm0   \n\t" /* Y1srcData */\
    "movq 8(%%"FF_REG_S", %%"FF_REG_a", 2), %%mm2   \n\t" /* Y2srcData */\
    "mov "STR(APCK_PTR2)"(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
    "movq  (%%"FF_REG_S", %%"FF_REG_a", 2), %%mm4   \n\t" /* Y1srcData */\
163 164 165
    "movq                      %%mm0, %%mm3         \n\t"\
    "punpcklwd                 %%mm4, %%mm0         \n\t"\
    "punpckhwd                 %%mm4, %%mm3         \n\t"\
166
    "movq "STR(APCK_COEF)"(%%"FF_REG_d"), %%mm4     \n\t" /* filterCoeff */\
167 168 169 170
    "pmaddwd                   %%mm4, %%mm0         \n\t"\
    "pmaddwd                   %%mm4, %%mm3         \n\t"\
    "paddd                     %%mm0, %%mm1         \n\t"\
    "paddd                     %%mm3, %%mm5         \n\t"\
171 172 173 174
    "movq 8(%%"FF_REG_S", %%"FF_REG_a", 2), %%mm3   \n\t" /* Y2srcData */\
    "mov "STR(APCK_SIZE)"(%%"FF_REG_d"), %%"FF_REG_S" \n\t"\
    "add           $"STR(APCK_SIZE)", %%"FF_REG_d"  \n\t"\
    "test               %%"FF_REG_S", %%"FF_REG_S"  \n\t"\
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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
    "movq                      %%mm2, %%mm0         \n\t"\
    "punpcklwd                 %%mm3, %%mm2         \n\t"\
    "punpckhwd                 %%mm3, %%mm0         \n\t"\
    "pmaddwd                   %%mm4, %%mm2         \n\t"\
    "pmaddwd                   %%mm4, %%mm0         \n\t"\
    "paddd                     %%mm2, %%mm7         \n\t"\
    "paddd                     %%mm0, %%mm6         \n\t"\
    " jnz                         2b                \n\t"\
    "psrad                       $16, %%mm1         \n\t"\
    "psrad                       $16, %%mm5         \n\t"\
    "psrad                       $16, %%mm7         \n\t"\
    "psrad                       $16, %%mm6         \n\t"\
    "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
    "packssdw                  %%mm5, %%mm1         \n\t"\
    "packssdw                  %%mm6, %%mm7         \n\t"\
    "paddw                     %%mm0, %%mm1         \n\t"\
    "paddw                     %%mm0, %%mm7         \n\t"\
    "movq               "U_TEMP"(%0), %%mm3         \n\t"\
    "movq               "V_TEMP"(%0), %%mm4         \n\t"\

#define YSCALEYUV2PACKEDX_ACCURATE \
    YSCALEYUV2PACKEDX_ACCURATE_UV \
    YSCALEYUV2PACKEDX_ACCURATE_YA(LUM_MMX_FILTER_OFFSET)

#define YSCALEYUV2RGBX \
    "psubw  "U_OFFSET"(%0), %%mm3       \n\t" /* (U-128)8*/\
    "psubw  "V_OFFSET"(%0), %%mm4       \n\t" /* (V-128)8*/\
    "movq            %%mm3, %%mm2       \n\t" /* (U-128)8*/\
    "movq            %%mm4, %%mm5       \n\t" /* (V-128)8*/\
    "pmulhw "UG_COEFF"(%0), %%mm3       \n\t"\
    "pmulhw "VG_COEFF"(%0), %%mm4       \n\t"\
    /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
    "pmulhw "UB_COEFF"(%0), %%mm2       \n\t"\
    "pmulhw "VR_COEFF"(%0), %%mm5       \n\t"\
    "psubw  "Y_OFFSET"(%0), %%mm1       \n\t" /* 8(Y-16)*/\
    "psubw  "Y_OFFSET"(%0), %%mm7       \n\t" /* 8(Y-16)*/\
    "pmulhw  "Y_COEFF"(%0), %%mm1       \n\t"\
    "pmulhw  "Y_COEFF"(%0), %%mm7       \n\t"\
    /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
    "paddw           %%mm3, %%mm4       \n\t"\
    "movq            %%mm2, %%mm0       \n\t"\
    "movq            %%mm5, %%mm6       \n\t"\
    "movq            %%mm4, %%mm3       \n\t"\
    "punpcklwd       %%mm2, %%mm2       \n\t"\
    "punpcklwd       %%mm5, %%mm5       \n\t"\
    "punpcklwd       %%mm4, %%mm4       \n\t"\
    "paddw           %%mm1, %%mm2       \n\t"\
    "paddw           %%mm1, %%mm5       \n\t"\
    "paddw           %%mm1, %%mm4       \n\t"\
    "punpckhwd       %%mm0, %%mm0       \n\t"\
    "punpckhwd       %%mm6, %%mm6       \n\t"\
    "punpckhwd       %%mm3, %%mm3       \n\t"\
    "paddw           %%mm7, %%mm0       \n\t"\
    "paddw           %%mm7, %%mm6       \n\t"\
    "paddw           %%mm7, %%mm3       \n\t"\
    /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
    "packuswb        %%mm0, %%mm2       \n\t"\
    "packuswb        %%mm6, %%mm5       \n\t"\
    "packuswb        %%mm3, %%mm4       \n\t"\

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#define REAL_WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t) \
    "movq       "#b", "#q2"     \n\t" /* B */\
    "movq       "#r", "#t"      \n\t" /* R */\
    "punpcklbw  "#g", "#b"      \n\t" /* GBGBGBGB 0 */\
    "punpcklbw  "#a", "#r"      \n\t" /* ARARARAR 0 */\
    "punpckhbw  "#g", "#q2"     \n\t" /* GBGBGBGB 2 */\
    "punpckhbw  "#a", "#t"      \n\t" /* ARARARAR 2 */\
    "movq       "#b", "#q0"     \n\t" /* GBGBGBGB 0 */\
    "movq      "#q2", "#q3"     \n\t" /* GBGBGBGB 2 */\
    "punpcklwd  "#r", "#q0"     \n\t" /* ARGBARGB 0 */\
    "punpckhwd  "#r", "#b"      \n\t" /* ARGBARGB 1 */\
    "punpcklwd  "#t", "#q2"     \n\t" /* ARGBARGB 2 */\
    "punpckhwd  "#t", "#q3"     \n\t" /* ARGBARGB 3 */\
\
    MOVNTQ(   q0,   (dst, index, 4))\
    MOVNTQ(    b,  8(dst, index, 4))\
    MOVNTQ(   q2, 16(dst, index, 4))\
    MOVNTQ(   q3, 24(dst, index, 4))\
\
    "add      $8, "#index"      \n\t"\
    "cmp "#dstw", "#index"      \n\t"\
    " jb      1b                \n\t"
#define WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t)  REAL_WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t)
258

259 260 261 262 263 264
static void RENAME(yuv2rgb32_X_ar)(SwsContext *c, const int16_t *lumFilter,
                                   const int16_t **lumSrc, int lumFilterSize,
                                   const int16_t *chrFilter, const int16_t **chrUSrc,
                                   const int16_t **chrVSrc,
                                   int chrFilterSize, const int16_t **alpSrc,
                                   uint8_t *dest, int dstW, int dstY)
265 266 267
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
268
    x86_reg uv_off = c->uv_off_byte;
269

270 271 272 273 274 275
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
        YSCALEYUV2PACKEDX_ACCURATE
        YSCALEYUV2RGBX
        "movq                      %%mm2, "U_TEMP"(%0)  \n\t"
        "movq                      %%mm4, "V_TEMP"(%0)  \n\t"
        "movq                      %%mm5, "Y_TEMP"(%0)  \n\t"
276
        YSCALEYUV2PACKEDX_ACCURATE_YA(ALP_MMX_FILTER_OFFSET)
277 278 279 280
        "movq               "Y_TEMP"(%0), %%mm5         \n\t"
        "psraw                        $3, %%mm1         \n\t"
        "psraw                        $3, %%mm7         \n\t"
        "packuswb                  %%mm7, %%mm1         \n\t"
281
        WRITEBGR32(%4, %5, %%FF_REGa, %%mm3, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm2, %%mm6)
282 283 284 285 286
        YSCALEYUV2PACKEDX_END
    } else {
        YSCALEYUV2PACKEDX_ACCURATE
        YSCALEYUV2RGBX
        "pcmpeqd %%mm7, %%mm7 \n\t"
287
        WRITEBGR32(%4, %5, %%FF_REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
288 289 290
        YSCALEYUV2PACKEDX_END
    }
}
291

292 293 294 295 296 297
static void RENAME(yuv2rgb32_X)(SwsContext *c, const int16_t *lumFilter,
                                const int16_t **lumSrc, int lumFilterSize,
                                const int16_t *chrFilter, const int16_t **chrUSrc,
                                const int16_t **chrVSrc,
                                int chrFilterSize, const int16_t **alpSrc,
                                uint8_t *dest, int dstW, int dstY)
298 299 300
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
301
    x86_reg uv_off = c->uv_off_byte;
302

303 304 305
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
        YSCALEYUV2PACKEDX
        YSCALEYUV2RGBX
306
        YSCALEYUV2PACKEDX_YA(ALP_MMX_FILTER_OFFSET, %%mm0, %%mm3, %%mm6, %%mm1, %%mm7)
307 308 309
        "psraw                        $3, %%mm1         \n\t"
        "psraw                        $3, %%mm7         \n\t"
        "packuswb                  %%mm7, %%mm1         \n\t"
310
        WRITEBGR32(%4, %5, %%FF_REGa, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
311 312 313 314 315
        YSCALEYUV2PACKEDX_END
    } else {
        YSCALEYUV2PACKEDX
        YSCALEYUV2RGBX
        "pcmpeqd %%mm7, %%mm7 \n\t"
316
        WRITEBGR32(%4, %5, %%FF_REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
317 318 319
        YSCALEYUV2PACKEDX_END
    }
}
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
#define REAL_WRITERGB16(dst, dstw, index) \
    "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
    "pand "MANGLE(bFC)", %%mm4  \n\t" /* G */\
    "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
    "psrlq           $3, %%mm2  \n\t"\
\
    "movq         %%mm2, %%mm1  \n\t"\
    "movq         %%mm4, %%mm3  \n\t"\
\
    "punpcklbw    %%mm7, %%mm3  \n\t"\
    "punpcklbw    %%mm5, %%mm2  \n\t"\
    "punpckhbw    %%mm7, %%mm4  \n\t"\
    "punpckhbw    %%mm5, %%mm1  \n\t"\
\
    "psllq           $3, %%mm3  \n\t"\
    "psllq           $3, %%mm4  \n\t"\
\
    "por          %%mm3, %%mm2  \n\t"\
    "por          %%mm4, %%mm1  \n\t"\
\
    MOVNTQ(%%mm2,  (dst, index, 2))\
    MOVNTQ(%%mm1, 8(dst, index, 2))\
\
    "add             $8, "#index"   \n\t"\
    "cmp        "#dstw", "#index"   \n\t"\
    " jb             1b             \n\t"
#define WRITERGB16(dst, dstw, index)  REAL_WRITERGB16(dst, dstw, index)
348

349 350 351 352 353 354
static void RENAME(yuv2rgb565_X_ar)(SwsContext *c, const int16_t *lumFilter,
                                    const int16_t **lumSrc, int lumFilterSize,
                                    const int16_t *chrFilter, const int16_t **chrUSrc,
                                    const int16_t **chrVSrc,
                                    int chrFilterSize, const int16_t **alpSrc,
                                    uint8_t *dest, int dstW, int dstY)
355 356 357
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
358
    x86_reg uv_off = c->uv_off_byte;
359

360 361 362 363 364 365 366 367 368
    YSCALEYUV2PACKEDX_ACCURATE
    YSCALEYUV2RGBX
    "pxor %%mm7, %%mm7 \n\t"
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
#ifdef DITHER1XBPP
    "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
    "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
    "paddusb "RED_DITHER"(%0), %%mm5\n\t"
#endif
369
    WRITERGB16(%4, %5, %%FF_REGa)
370 371
    YSCALEYUV2PACKEDX_END
}
372

373 374 375 376 377 378
static void RENAME(yuv2rgb565_X)(SwsContext *c, const int16_t *lumFilter,
                                 const int16_t **lumSrc, int lumFilterSize,
                                 const int16_t *chrFilter, const int16_t **chrUSrc,
                                 const int16_t **chrVSrc,
                                 int chrFilterSize, const int16_t **alpSrc,
                                 uint8_t *dest, int dstW, int dstY)
379 380 381
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
382
    x86_reg uv_off = c->uv_off_byte;
383

384 385 386 387 388 389 390 391 392
    YSCALEYUV2PACKEDX
    YSCALEYUV2RGBX
    "pxor %%mm7, %%mm7 \n\t"
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
#ifdef DITHER1XBPP
    "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
    "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
    "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
#endif
393
    WRITERGB16(%4, %5, %%FF_REGa)
394 395
    YSCALEYUV2PACKEDX_END
}
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

#define REAL_WRITERGB15(dst, dstw, index) \
    "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
    "pand "MANGLE(bF8)", %%mm4  \n\t" /* G */\
    "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
    "psrlq           $3, %%mm2  \n\t"\
    "psrlq           $1, %%mm5  \n\t"\
\
    "movq         %%mm2, %%mm1  \n\t"\
    "movq         %%mm4, %%mm3  \n\t"\
\
    "punpcklbw    %%mm7, %%mm3  \n\t"\
    "punpcklbw    %%mm5, %%mm2  \n\t"\
    "punpckhbw    %%mm7, %%mm4  \n\t"\
    "punpckhbw    %%mm5, %%mm1  \n\t"\
\
    "psllq           $2, %%mm3  \n\t"\
    "psllq           $2, %%mm4  \n\t"\
\
    "por          %%mm3, %%mm2  \n\t"\
    "por          %%mm4, %%mm1  \n\t"\
\
    MOVNTQ(%%mm2,  (dst, index, 2))\
    MOVNTQ(%%mm1, 8(dst, index, 2))\
\
    "add             $8, "#index"   \n\t"\
    "cmp        "#dstw", "#index"   \n\t"\
    " jb             1b             \n\t"
#define WRITERGB15(dst, dstw, index)  REAL_WRITERGB15(dst, dstw, index)

426 427 428 429 430 431
static void RENAME(yuv2rgb555_X_ar)(SwsContext *c, const int16_t *lumFilter,
                                    const int16_t **lumSrc, int lumFilterSize,
                                    const int16_t *chrFilter, const int16_t **chrUSrc,
                                    const int16_t **chrVSrc,
                                    int chrFilterSize, const int16_t **alpSrc,
                                    uint8_t *dest, int dstW, int dstY)
432 433 434
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
435
    x86_reg uv_off = c->uv_off_byte;
436 437 438 439 440 441 442 443 444 445

    YSCALEYUV2PACKEDX_ACCURATE
    YSCALEYUV2RGBX
    "pxor %%mm7, %%mm7 \n\t"
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
#ifdef DITHER1XBPP
    "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
    "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
    "paddusb "RED_DITHER"(%0), %%mm5\n\t"
#endif
446
    WRITERGB15(%4, %5, %%FF_REGa)
447 448 449
    YSCALEYUV2PACKEDX_END
}

450 451 452 453 454 455
static void RENAME(yuv2rgb555_X)(SwsContext *c, const int16_t *lumFilter,
                                 const int16_t **lumSrc, int lumFilterSize,
                                 const int16_t *chrFilter, const int16_t **chrUSrc,
                                 const int16_t **chrVSrc,
                                 int chrFilterSize, const int16_t **alpSrc,
                                 uint8_t *dest, int dstW, int dstY)
456 457 458
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
459
    x86_reg uv_off = c->uv_off_byte;
460 461 462 463 464 465 466 467 468 469

    YSCALEYUV2PACKEDX
    YSCALEYUV2RGBX
    "pxor %%mm7, %%mm7 \n\t"
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
#ifdef DITHER1XBPP
    "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
    "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
    "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
#endif
470
    WRITERGB15(%4, %5, %%FF_REGa)
471 472 473
    YSCALEYUV2PACKEDX_END
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
#define WRITEBGR24MMX(dst, dstw, index) \
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
    "movq      %%mm2, %%mm1     \n\t" /* B */\
    "movq      %%mm5, %%mm6     \n\t" /* R */\
    "punpcklbw %%mm4, %%mm2     \n\t" /* GBGBGBGB 0 */\
    "punpcklbw %%mm7, %%mm5     \n\t" /* 0R0R0R0R 0 */\
    "punpckhbw %%mm4, %%mm1     \n\t" /* GBGBGBGB 2 */\
    "punpckhbw %%mm7, %%mm6     \n\t" /* 0R0R0R0R 2 */\
    "movq      %%mm2, %%mm0     \n\t" /* GBGBGBGB 0 */\
    "movq      %%mm1, %%mm3     \n\t" /* GBGBGBGB 2 */\
    "punpcklwd %%mm5, %%mm0     \n\t" /* 0RGB0RGB 0 */\
    "punpckhwd %%mm5, %%mm2     \n\t" /* 0RGB0RGB 1 */\
    "punpcklwd %%mm6, %%mm1     \n\t" /* 0RGB0RGB 2 */\
    "punpckhwd %%mm6, %%mm3     \n\t" /* 0RGB0RGB 3 */\
\
    "movq      %%mm0, %%mm4     \n\t" /* 0RGB0RGB 0 */\
    "movq      %%mm2, %%mm6     \n\t" /* 0RGB0RGB 1 */\
    "movq      %%mm1, %%mm5     \n\t" /* 0RGB0RGB 2 */\
    "movq      %%mm3, %%mm7     \n\t" /* 0RGB0RGB 3 */\
\
    "psllq       $40, %%mm0     \n\t" /* RGB00000 0 */\
    "psllq       $40, %%mm2     \n\t" /* RGB00000 1 */\
    "psllq       $40, %%mm1     \n\t" /* RGB00000 2 */\
    "psllq       $40, %%mm3     \n\t" /* RGB00000 3 */\
\
    "punpckhdq %%mm4, %%mm0     \n\t" /* 0RGBRGB0 0 */\
    "punpckhdq %%mm6, %%mm2     \n\t" /* 0RGBRGB0 1 */\
    "punpckhdq %%mm5, %%mm1     \n\t" /* 0RGBRGB0 2 */\
    "punpckhdq %%mm7, %%mm3     \n\t" /* 0RGBRGB0 3 */\
\
    "psrlq        $8, %%mm0     \n\t" /* 00RGBRGB 0 */\
    "movq      %%mm2, %%mm6     \n\t" /* 0RGBRGB0 1 */\
    "psllq       $40, %%mm2     \n\t" /* GB000000 1 */\
    "por       %%mm2, %%mm0     \n\t" /* GBRGBRGB 0 */\
    MOVNTQ(%%mm0, (dst))\
\
    "psrlq       $24, %%mm6     \n\t" /* 0000RGBR 1 */\
    "movq      %%mm1, %%mm5     \n\t" /* 0RGBRGB0 2 */\
    "psllq       $24, %%mm1     \n\t" /* BRGB0000 2 */\
    "por       %%mm1, %%mm6     \n\t" /* BRGBRGBR 1 */\
    MOVNTQ(%%mm6, 8(dst))\
\
    "psrlq       $40, %%mm5     \n\t" /* 000000RG 2 */\
    "psllq        $8, %%mm3     \n\t" /* RGBRGB00 3 */\
    "por       %%mm3, %%mm5     \n\t" /* RGBRGBRG 2 */\
    MOVNTQ(%%mm5, 16(dst))\
\
    "add         $24, "#dst"    \n\t"\
\
    "add          $8, "#index"  \n\t"\
    "cmp     "#dstw", "#index"  \n\t"\
    " jb          1b            \n\t"

527
#define WRITEBGR24MMXEXT(dst, dstw, index) \
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
    "movq "MANGLE(ff_M24A)", %%mm0 \n\t"\
    "movq "MANGLE(ff_M24C)", %%mm7 \n\t"\
    "pshufw $0x50, %%mm2, %%mm1 \n\t" /* B3 B2 B3 B2  B1 B0 B1 B0 */\
    "pshufw $0x50, %%mm4, %%mm3 \n\t" /* G3 G2 G3 G2  G1 G0 G1 G0 */\
    "pshufw $0x00, %%mm5, %%mm6 \n\t" /* R1 R0 R1 R0  R1 R0 R1 R0 */\
\
    "pand   %%mm0, %%mm1        \n\t" /*    B2        B1       B0 */\
    "pand   %%mm0, %%mm3        \n\t" /*    G2        G1       G0 */\
    "pand   %%mm7, %%mm6        \n\t" /*       R1        R0       */\
\
    "psllq     $8, %%mm3        \n\t" /* G2        G1       G0    */\
    "por    %%mm1, %%mm6        \n\t"\
    "por    %%mm3, %%mm6        \n\t"\
    MOVNTQ(%%mm6, (dst))\
\
    "psrlq     $8, %%mm4        \n\t" /* 00 G7 G6 G5  G4 G3 G2 G1 */\
    "pshufw $0xA5, %%mm2, %%mm1 \n\t" /* B5 B4 B5 B4  B3 B2 B3 B2 */\
    "pshufw $0x55, %%mm4, %%mm3 \n\t" /* G4 G3 G4 G3  G4 G3 G4 G3 */\
    "pshufw $0xA5, %%mm5, %%mm6 \n\t" /* R5 R4 R5 R4  R3 R2 R3 R2 */\
\
    "pand "MANGLE(ff_M24B)", %%mm1 \n\t" /* B5       B4        B3    */\
    "pand   %%mm7, %%mm3        \n\t" /*       G4        G3       */\
    "pand   %%mm0, %%mm6        \n\t" /*    R4        R3       R2 */\
\
    "por    %%mm1, %%mm3        \n\t" /* B5    G4 B4     G3 B3    */\
    "por    %%mm3, %%mm6        \n\t"\
    MOVNTQ(%%mm6, 8(dst))\
\
    "pshufw $0xFF, %%mm2, %%mm1 \n\t" /* B7 B6 B7 B6  B7 B6 B6 B7 */\
    "pshufw $0xFA, %%mm4, %%mm3 \n\t" /* 00 G7 00 G7  G6 G5 G6 G5 */\
    "pshufw $0xFA, %%mm5, %%mm6 \n\t" /* R7 R6 R7 R6  R5 R4 R5 R4 */\
\
    "pand   %%mm7, %%mm1        \n\t" /*       B7        B6       */\
    "pand   %%mm0, %%mm3        \n\t" /*    G7        G6       G5 */\
    "pand "MANGLE(ff_M24B)", %%mm6 \n\t" /* R7       R6        R5    */\
\
    "por    %%mm1, %%mm3        \n\t"\
    "por    %%mm3, %%mm6        \n\t"\
    MOVNTQ(%%mm6, 16(dst))\
\
    "add      $24, "#dst"       \n\t"\
\
    "add       $8, "#index"     \n\t"\
    "cmp  "#dstw", "#index"     \n\t"\
    " jb       1b               \n\t"

575
#if COMPILE_TEMPLATE_MMXEXT
576
#undef WRITEBGR24
577
#define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMXEXT(dst, dstw, index)
578 579 580 581 582
#else
#undef WRITEBGR24
#define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMX(dst, dstw, index)
#endif

583 584 585 586 587 588
static void RENAME(yuv2bgr24_X_ar)(SwsContext *c, const int16_t *lumFilter,
                                   const int16_t **lumSrc, int lumFilterSize,
                                   const int16_t *chrFilter, const int16_t **chrUSrc,
                                   const int16_t **chrVSrc,
                                   int chrFilterSize, const int16_t **alpSrc,
                                   uint8_t *dest, int dstW, int dstY)
589 590 591
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
592
    x86_reg uv_off = c->uv_off_byte;
593 594 595

    YSCALEYUV2PACKEDX_ACCURATE
    YSCALEYUV2RGBX
596 597 598 599
    "pxor                          %%mm7, %%mm7         \n\t"
    "lea (%%"FF_REG_a", %%"FF_REG_a", 2), %%"FF_REG_c"  \n\t" // FIXME optimize
    "add                              %4, %%"FF_REG_c"  \n\t"
    WRITEBGR24(%%FF_REGc, %5, %%FF_REGa)
600 601
    :: "r" (&c->redDither),
       "m" (dummy), "m" (dummy), "m" (dummy),
602
       "r" (dest), "m" (dstW_reg), "m"(uv_off)
603
    : "%"FF_REG_a, "%"FF_REG_c, "%"FF_REG_d, "%"FF_REG_S
604 605 606
    );
}

607 608 609 610 611 612
static void RENAME(yuv2bgr24_X)(SwsContext *c, const int16_t *lumFilter,
                                const int16_t **lumSrc, int lumFilterSize,
                                const int16_t *chrFilter, const int16_t **chrUSrc,
                                const int16_t **chrVSrc,
                                int chrFilterSize, const int16_t **alpSrc,
                                uint8_t *dest, int dstW, int dstY)
613 614 615
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
616
    x86_reg uv_off = c->uv_off_byte;
617 618 619

    YSCALEYUV2PACKEDX
    YSCALEYUV2RGBX
620 621 622 623
    "pxor                          %%mm7, %%mm7         \n\t"
    "lea (%%"FF_REG_a", %%"FF_REG_a", 2), %%"FF_REG_c"  \n\t" // FIXME optimize
    "add                              %4, %%"FF_REG_c"  \n\t"
    WRITEBGR24(%%FF_REGc, %5, %%FF_REGa)
624 625
    :: "r" (&c->redDither),
       "m" (dummy), "m" (dummy), "m" (dummy),
626
       "r" (dest),  "m" (dstW_reg), "m"(uv_off)
627
    : "%"FF_REG_a, "%"FF_REG_c, "%"FF_REG_d, "%"FF_REG_S
628 629 630
    );
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
#define REAL_WRITEYUY2(dst, dstw, index) \
    "packuswb  %%mm3, %%mm3     \n\t"\
    "packuswb  %%mm4, %%mm4     \n\t"\
    "packuswb  %%mm7, %%mm1     \n\t"\
    "punpcklbw %%mm4, %%mm3     \n\t"\
    "movq      %%mm1, %%mm7     \n\t"\
    "punpcklbw %%mm3, %%mm1     \n\t"\
    "punpckhbw %%mm3, %%mm7     \n\t"\
\
    MOVNTQ(%%mm1, (dst, index, 2))\
    MOVNTQ(%%mm7, 8(dst, index, 2))\
\
    "add          $8, "#index"  \n\t"\
    "cmp     "#dstw", "#index"  \n\t"\
    " jb          1b            \n\t"
#define WRITEYUY2(dst, dstw, index)  REAL_WRITEYUY2(dst, dstw, index)

648 649 650 651 652 653
static void RENAME(yuv2yuyv422_X_ar)(SwsContext *c, const int16_t *lumFilter,
                                     const int16_t **lumSrc, int lumFilterSize,
                                     const int16_t *chrFilter, const int16_t **chrUSrc,
                                     const int16_t **chrVSrc,
                                     int chrFilterSize, const int16_t **alpSrc,
                                     uint8_t *dest, int dstW, int dstY)
654 655 656
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
657
    x86_reg uv_off = c->uv_off_byte;
658

659 660 661 662 663 664
    YSCALEYUV2PACKEDX_ACCURATE
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
    "psraw $3, %%mm3    \n\t"
    "psraw $3, %%mm4    \n\t"
    "psraw $3, %%mm1    \n\t"
    "psraw $3, %%mm7    \n\t"
665
    WRITEYUY2(%4, %5, %%FF_REGa)
666
    YSCALEYUV2PACKEDX_END
667 668
}

669 670 671 672 673 674
static void RENAME(yuv2yuyv422_X)(SwsContext *c, const int16_t *lumFilter,
                                  const int16_t **lumSrc, int lumFilterSize,
                                  const int16_t *chrFilter, const int16_t **chrUSrc,
                                  const int16_t **chrVSrc,
                                  int chrFilterSize, const int16_t **alpSrc,
                                  uint8_t *dest, int dstW, int dstY)
675 676 677
{
    x86_reg dummy=0;
    x86_reg dstW_reg = dstW;
678
    x86_reg uv_off = c->uv_off_byte;
679

680 681 682 683 684 685
    YSCALEYUV2PACKEDX
    /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
    "psraw $3, %%mm3    \n\t"
    "psraw $3, %%mm4    \n\t"
    "psraw $3, %%mm1    \n\t"
    "psraw $3, %%mm7    \n\t"
686
    WRITEYUY2(%4, %5, %%FF_REGa)
687
    YSCALEYUV2PACKEDX_END
688 689
}

690
#define REAL_YSCALEYUV2RGB_UV(index, c) \
691 692 693 694 695
    "xor            "#index", "#index"  \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
    "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
696
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
697 698
    "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
    "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
699
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
    "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
    "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
    "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
    "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
    "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
    "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
    "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
    "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
    "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
    "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
    "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
    "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
    "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
    "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
    /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
716

717 718 719 720 721 722 723 724 725 726 727 728 729
#define REAL_YSCALEYUV2RGB_YA(index, c, b1, b2) \
    "movq  ("#b1", "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
    "movq  ("#b2", "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
    "movq 8("#b1", "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
    "movq 8("#b2", "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
    "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
    "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
    "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
    "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
    "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
    "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
730

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
#define REAL_YSCALEYUV2RGB_COEFF(c) \
    "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
    "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
    "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
    "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
    "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
    "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
    /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
    "paddw             %%mm3, %%mm4     \n\t"\
    "movq              %%mm2, %%mm0     \n\t"\
    "movq              %%mm5, %%mm6     \n\t"\
    "movq              %%mm4, %%mm3     \n\t"\
    "punpcklwd         %%mm2, %%mm2     \n\t"\
    "punpcklwd         %%mm5, %%mm5     \n\t"\
    "punpcklwd         %%mm4, %%mm4     \n\t"\
    "paddw             %%mm1, %%mm2     \n\t"\
    "paddw             %%mm1, %%mm5     \n\t"\
    "paddw             %%mm1, %%mm4     \n\t"\
    "punpckhwd         %%mm0, %%mm0     \n\t"\
    "punpckhwd         %%mm6, %%mm6     \n\t"\
    "punpckhwd         %%mm3, %%mm3     \n\t"\
    "paddw             %%mm7, %%mm0     \n\t"\
    "paddw             %%mm7, %%mm6     \n\t"\
    "paddw             %%mm7, %%mm3     \n\t"\
    /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
    "packuswb          %%mm0, %%mm2     \n\t"\
    "packuswb          %%mm6, %%mm5     \n\t"\
    "packuswb          %%mm3, %%mm4     \n\t"\
759

760
#define YSCALEYUV2RGB_YA(index, c, b1, b2) REAL_YSCALEYUV2RGB_YA(index, c, b1, b2)
761

762 763
#define YSCALEYUV2RGB(index, c) \
    REAL_YSCALEYUV2RGB_UV(index, c) \
764 765
    REAL_YSCALEYUV2RGB_YA(index, c, %0, %1) \
    REAL_YSCALEYUV2RGB_COEFF(c)
766

767 768 769
/**
 * vertical bilinear scale YV12 to RGB
 */
770 771 772
static void RENAME(yuv2rgb32_2)(SwsContext *c, const int16_t *buf[2],
                                const int16_t *ubuf[2], const int16_t *vbuf[2],
                                const int16_t *abuf[2], uint8_t *dest,
773
                                int dstW, int yalpha, int uvalpha, int y)
774
{
775 776 777
    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];

778
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
779
        const int16_t *abuf0 = abuf[0], *abuf1 = abuf[1];
780 781
#if ARCH_X86_64
        __asm__ volatile(
782
            YSCALEYUV2RGB(%%r8, %5)
783 784 785 786 787
            YSCALEYUV2RGB_YA(%%r8, %5, %6, %7)
            "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
            "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
            "packuswb            %%mm7, %%mm1       \n\t"
            WRITEBGR32(%4, 8280(%5), %%r8, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
788
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "r" (dest),
789
               "a" (&c->redDither),
790
               "r" (abuf0), "r" (abuf1)
791 792 793 794 795 796
            : "%r8"
        );
#else
        *(const uint16_t **)(&c->u_temp)=abuf0;
        *(const uint16_t **)(&c->v_temp)=abuf1;
        __asm__ volatile(
797 798 799 800
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB(%%FF_REGBP, %5)
801 802 803 804
            "push                   %0              \n\t"
            "push                   %1              \n\t"
            "mov          "U_TEMP"(%5), %0          \n\t"
            "mov          "V_TEMP"(%5), %1          \n\t"
805
            YSCALEYUV2RGB_YA(%%FF_REGBP, %5, %0, %1)
806 807 808 809 810
            "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
            "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
            "packuswb            %%mm7, %%mm1       \n\t"
            "pop                    %1              \n\t"
            "pop                    %0              \n\t"
811 812 813
            WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
814
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
815
               "a" (&c->redDither)
816 817 818 819
        );
#endif
    } else {
        __asm__ volatile(
820 821 822 823
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB(%%FF_REGBP, %5)
824
            "pcmpeqd %%mm7, %%mm7                   \n\t"
825 826 827
            WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
828
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
829
               "a" (&c->redDither)
830 831
        );
    }
832 833
}

834 835 836
static void RENAME(yuv2bgr24_2)(SwsContext *c, const int16_t *buf[2],
                                const int16_t *ubuf[2], const int16_t *vbuf[2],
                                const int16_t *abuf[2], uint8_t *dest,
837
                                int dstW, int yalpha, int uvalpha, int y)
838
{
839 840 841
    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];

842 843
    //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
    __asm__ volatile(
844 845 846 847
        "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
        "mov           %4, %%"FF_REG_b"         \n\t"
        "push %%"FF_REG_BP"                     \n\t"
        YSCALEYUV2RGB(%%FF_REGBP, %5)
848
        "pxor    %%mm7, %%mm7                   \n\t"
849 850 851
        WRITEBGR24(%%FF_REGb, 8280(%5), %%FF_REGBP)
        "pop %%"FF_REG_BP"                      \n\t"
        "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
852
        :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
853
           "a" (&c->redDither)
854
    );
855 856
}

857 858 859
static void RENAME(yuv2rgb555_2)(SwsContext *c, const int16_t *buf[2],
                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
                                 const int16_t *abuf[2], uint8_t *dest,
860
                                 int dstW, int yalpha, int uvalpha, int y)
861
{
862 863 864
    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];

865 866
    //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
    __asm__ volatile(
867 868 869 870
        "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
        "mov           %4, %%"FF_REG_b"         \n\t"
        "push %%"FF_REG_BP"                     \n\t"
        YSCALEYUV2RGB(%%FF_REGBP, %5)
871 872 873 874 875 876 877
        "pxor    %%mm7, %%mm7                   \n\t"
        /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
#ifdef DITHER1XBPP
        "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
        "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
        "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
#endif
878 879 880
        WRITERGB15(%%FF_REGb, 8280(%5), %%FF_REGBP)
        "pop %%"FF_REG_BP"                      \n\t"
        "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
881
        :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
882
           "a" (&c->redDither)
883
    );
884 885
}

886 887 888
static void RENAME(yuv2rgb565_2)(SwsContext *c, const int16_t *buf[2],
                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
                                 const int16_t *abuf[2], uint8_t *dest,
889
                                 int dstW, int yalpha, int uvalpha, int y)
890
{
891 892 893
    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];

894 895
    //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
    __asm__ volatile(
896 897 898 899
        "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
        "mov           %4, %%"FF_REG_b"         \n\t"
        "push %%"FF_REG_BP"                     \n\t"
        YSCALEYUV2RGB(%%FF_REGBP, %5)
900 901
        "pxor    %%mm7, %%mm7                   \n\t"
        /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
902
#ifdef DITHER1XBPP
903 904 905
        "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
        "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
        "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
906
#endif
907 908 909
        WRITERGB16(%%FF_REGb, 8280(%5), %%FF_REGBP)
        "pop %%"FF_REG_BP"                      \n\t"
        "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
910
        :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
911
           "a" (&c->redDither)
912
    );
913 914
}

915
#define REAL_YSCALEYUV2PACKED(index, c) \
916 917 918 919 920 921 922 923 924 925 926
    "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0              \n\t"\
    "movq "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm1              \n\t"\
    "psraw                $3, %%mm0                           \n\t"\
    "psraw                $3, %%mm1                           \n\t"\
    "movq              %%mm0, "CHR_MMX_FILTER_OFFSET"+8("#c") \n\t"\
    "movq              %%mm1, "LUM_MMX_FILTER_OFFSET"+8("#c") \n\t"\
    "xor            "#index", "#index"                        \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
    "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
927
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
928 929
    "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
    "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
930
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
    "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
    "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
    "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
    "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
    "psraw                $7, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
    "psraw                $7, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
    "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
    "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
    "movq  (%0, "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
    "movq  (%1, "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
    "movq 8(%0, "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
    "movq 8(%1, "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
    "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
    "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
    "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
    "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
    "psraw                $7, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "psraw                $7, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
    "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
952

953
#define YSCALEYUV2PACKED(index, c)  REAL_YSCALEYUV2PACKED(index, c)
954

955 956 957
static void RENAME(yuv2yuyv422_2)(SwsContext *c, const int16_t *buf[2],
                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
                                  const int16_t *abuf[2], uint8_t *dest,
958
                                  int dstW, int yalpha, int uvalpha, int y)
959
{
960 961 962
    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];

963 964
    //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
    __asm__ volatile(
965 966 967 968 969 970 971
        "mov %%"FF_REG_b", "ESP_OFFSET"(%5)        \n\t"
        "mov           %4, %%"FF_REG_b"            \n\t"
        "push %%"FF_REG_BP"                        \n\t"
        YSCALEYUV2PACKED(%%FF_REGBP, %5)
        WRITEYUY2(%%FF_REGb, 8280(%5), %%FF_REGBP)
        "pop %%"FF_REG_BP"                         \n\t"
        "mov "ESP_OFFSET"(%5), %%"FF_REG_b"        \n\t"
972
        :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
973
           "a" (&c->redDither)
974
    );
975 976
}

977
#define REAL_YSCALEYUV2RGB1(index, c) \
978 979 980 981
    "xor            "#index", "#index"  \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
982
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
983
    "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
984
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
    "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
    "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
    "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
    "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
    "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
    "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
    "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
    /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
    "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
    "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
    "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
    "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
    "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
    "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
    "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
    "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
    /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
    "paddw             %%mm3, %%mm4     \n\t"\
    "movq              %%mm2, %%mm0     \n\t"\
    "movq              %%mm5, %%mm6     \n\t"\
    "movq              %%mm4, %%mm3     \n\t"\
    "punpcklwd         %%mm2, %%mm2     \n\t"\
    "punpcklwd         %%mm5, %%mm5     \n\t"\
    "punpcklwd         %%mm4, %%mm4     \n\t"\
    "paddw             %%mm1, %%mm2     \n\t"\
    "paddw             %%mm1, %%mm5     \n\t"\
    "paddw             %%mm1, %%mm4     \n\t"\
    "punpckhwd         %%mm0, %%mm0     \n\t"\
    "punpckhwd         %%mm6, %%mm6     \n\t"\
    "punpckhwd         %%mm3, %%mm3     \n\t"\
    "paddw             %%mm7, %%mm0     \n\t"\
    "paddw             %%mm7, %%mm6     \n\t"\
    "paddw             %%mm7, %%mm3     \n\t"\
    /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
    "packuswb          %%mm0, %%mm2     \n\t"\
    "packuswb          %%mm6, %%mm5     \n\t"\
    "packuswb          %%mm3, %%mm4     \n\t"\
1025

1026
#define YSCALEYUV2RGB1(index, c)  REAL_YSCALEYUV2RGB1(index, c)
1027

1028
// do vertical chrominance interpolation
1029
#define REAL_YSCALEYUV2RGB1b(index, c) \
1030 1031 1032 1033 1034
    "xor            "#index", "#index"  \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
    "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1035
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1036 1037
    "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
    "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1038
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
    "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
    "psrlw                $5, %%mm3     \n\t" /*FIXME might overflow*/\
    "psrlw                $5, %%mm4     \n\t" /*FIXME might overflow*/\
    "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
    "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
    "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
    "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
    "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
    "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
    /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
    "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
    "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
    "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
    "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
    "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
    "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
    "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
    "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
    "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
    /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
    "paddw             %%mm3, %%mm4     \n\t"\
    "movq              %%mm2, %%mm0     \n\t"\
    "movq              %%mm5, %%mm6     \n\t"\
    "movq              %%mm4, %%mm3     \n\t"\
    "punpcklwd         %%mm2, %%mm2     \n\t"\
    "punpcklwd         %%mm5, %%mm5     \n\t"\
    "punpcklwd         %%mm4, %%mm4     \n\t"\
    "paddw             %%mm1, %%mm2     \n\t"\
    "paddw             %%mm1, %%mm5     \n\t"\
    "paddw             %%mm1, %%mm4     \n\t"\
    "punpckhwd         %%mm0, %%mm0     \n\t"\
    "punpckhwd         %%mm6, %%mm6     \n\t"\
    "punpckhwd         %%mm3, %%mm3     \n\t"\
    "paddw             %%mm7, %%mm0     \n\t"\
    "paddw             %%mm7, %%mm6     \n\t"\
    "paddw             %%mm7, %%mm3     \n\t"\
    /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
    "packuswb          %%mm0, %%mm2     \n\t"\
    "packuswb          %%mm6, %%mm5     \n\t"\
    "packuswb          %%mm3, %%mm4     \n\t"\

1082
#define YSCALEYUV2RGB1b(index, c)  REAL_YSCALEYUV2RGB1b(index, c)
1083 1084 1085 1086 1087 1088 1089 1090

#define REAL_YSCALEYUV2RGB1_ALPHA(index) \
    "movq  (%1, "#index", 2), %%mm7     \n\t" /* abuf0[index  ]     */\
    "movq 8(%1, "#index", 2), %%mm1     \n\t" /* abuf0[index+4]     */\
    "psraw                $7, %%mm7     \n\t" /* abuf0[index  ] >>7 */\
    "psraw                $7, %%mm1     \n\t" /* abuf0[index+4] >>7 */\
    "packuswb          %%mm1, %%mm7     \n\t"
#define YSCALEYUV2RGB1_ALPHA(index) REAL_YSCALEYUV2RGB1_ALPHA(index)
1091 1092

/**
1093
 * YV12 to RGB without scaling or interpolating
1094
 */
1095
static void RENAME(yuv2rgb32_1)(SwsContext *c, const int16_t *buf0,
1096
                                const int16_t *ubuf[2], const int16_t *vbuf[2],
1097 1098
                                const int16_t *abuf0, uint8_t *dest,
                                int dstW, int uvalpha, int y)
1099
{
1100
    const int16_t *ubuf0 = ubuf[0];
1101
    const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1102

1103
    if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1104
        const int16_t *ubuf1 = ubuf[0];
1105
        if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1106
            __asm__ volatile(
1107 1108 1109 1110 1111 1112 1113 1114
                "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
                "mov           %4, %%"FF_REG_b"         \n\t"
                "push %%"FF_REG_BP"                     \n\t"
                YSCALEYUV2RGB1(%%FF_REGBP, %5)
                YSCALEYUV2RGB1_ALPHA(%%FF_REGBP)
                WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
                "pop %%"FF_REG_BP"                      \n\t"
                "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1115
                :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1116
                   "a" (&c->redDither)
1117
            );
1118
        } else {
1119
            __asm__ volatile(
1120 1121 1122 1123
                "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
                "mov           %4, %%"FF_REG_b"         \n\t"
                "push %%"FF_REG_BP"                     \n\t"
                YSCALEYUV2RGB1(%%FF_REGBP, %5)
1124
                "pcmpeqd %%mm7, %%mm7                   \n\t"
1125 1126 1127
                WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
                "pop %%"FF_REG_BP"                      \n\t"
                "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1128
                :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1129
                   "a" (&c->redDither)
1130
            );
1131 1132
        }
    } else {
1133
        const int16_t *ubuf1 = ubuf[1];
1134
        if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1135
            __asm__ volatile(
1136 1137 1138 1139 1140 1141 1142 1143
                "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
                "mov           %4, %%"FF_REG_b"         \n\t"
                "push %%"FF_REG_BP"                     \n\t"
                YSCALEYUV2RGB1b(%%FF_REGBP, %5)
                YSCALEYUV2RGB1_ALPHA(%%FF_REGBP)
                WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
                "pop %%"FF_REG_BP"                      \n\t"
                "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1144
                :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1145
                   "a" (&c->redDither)
1146
            );
1147
        } else {
1148
            __asm__ volatile(
1149 1150 1151 1152
                "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
                "mov           %4, %%"FF_REG_b"         \n\t"
                "push %%"FF_REG_BP"                     \n\t"
                YSCALEYUV2RGB1b(%%FF_REGBP, %5)
1153
                "pcmpeqd %%mm7, %%mm7                   \n\t"
1154 1155 1156
                WRITEBGR32(%%FF_REGb, 8280(%5), %%FF_REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
                "pop %%"FF_REG_BP"                      \n\t"
                "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1157
                :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1158
                   "a" (&c->redDither)
1159
            );
1160
        }
1161
    }
1162 1163
}

1164
static void RENAME(yuv2bgr24_1)(SwsContext *c, const int16_t *buf0,
1165
                                const int16_t *ubuf[2], const int16_t *vbuf[2],
1166 1167
                                const int16_t *abuf0, uint8_t *dest,
                                int dstW, int uvalpha, int y)
1168
{
1169
    const int16_t *ubuf0 = ubuf[0];
1170
    const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1171 1172

    if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1173
        const int16_t *ubuf1 = ubuf[0];
1174
        __asm__ volatile(
1175 1176 1177 1178
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1(%%FF_REGBP, %5)
1179
            "pxor    %%mm7, %%mm7                   \n\t"
1180 1181 1182
            WRITEBGR24(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1183
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1184
               "a" (&c->redDither)
1185 1186
        );
    } else {
1187
        const int16_t *ubuf1 = ubuf[1];
1188
        __asm__ volatile(
1189 1190 1191 1192
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1b(%%FF_REGBP, %5)
1193
            "pxor    %%mm7, %%mm7                   \n\t"
1194 1195 1196
            WRITEBGR24(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1197
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1198
               "a" (&c->redDither)
1199 1200
        );
    }
1201 1202
}

1203
static void RENAME(yuv2rgb555_1)(SwsContext *c, const int16_t *buf0,
1204
                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
1205 1206
                                 const int16_t *abuf0, uint8_t *dest,
                                 int dstW, int uvalpha, int y)
1207
{
1208
    const int16_t *ubuf0 = ubuf[0];
1209
    const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1210 1211

    if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1212
        const int16_t *ubuf1 = ubuf[0];
1213
        __asm__ volatile(
1214 1215 1216 1217
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1(%%FF_REGBP, %5)
1218 1219
            "pxor    %%mm7, %%mm7                   \n\t"
            /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1220
#ifdef DITHER1XBPP
1221 1222 1223
            "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
            "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
            "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1224
#endif
1225 1226 1227
            WRITERGB15(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1228
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1229
               "a" (&c->redDither)
1230 1231
        );
    } else {
1232
        const int16_t *ubuf1 = ubuf[1];
1233
        __asm__ volatile(
1234 1235 1236 1237
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1b(%%FF_REGBP, %5)
1238 1239
            "pxor    %%mm7, %%mm7                   \n\t"
            /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1240
#ifdef DITHER1XBPP
1241 1242 1243
            "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
            "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
            "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1244
#endif
1245 1246 1247
            WRITERGB15(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1248
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1249
               "a" (&c->redDither)
1250 1251
        );
    }
1252
}
1253

1254
static void RENAME(yuv2rgb565_1)(SwsContext *c, const int16_t *buf0,
1255
                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
1256 1257
                                 const int16_t *abuf0, uint8_t *dest,
                                 int dstW, int uvalpha, int y)
1258
{
1259
    const int16_t *ubuf0 = ubuf[0];
1260
    const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1261 1262

    if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1263
        const int16_t *ubuf1 = ubuf[0];
1264
        __asm__ volatile(
1265 1266 1267 1268
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1(%%FF_REGBP, %5)
1269 1270
            "pxor    %%mm7, %%mm7                   \n\t"
            /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1271
#ifdef DITHER1XBPP
1272 1273 1274
            "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
            "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
            "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1275
#endif
1276 1277 1278
            WRITERGB16(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1279
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1280
               "a" (&c->redDither)
1281 1282
        );
    } else {
1283
        const int16_t *ubuf1 = ubuf[1];
1284
        __asm__ volatile(
1285 1286 1287 1288
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2RGB1b(%%FF_REGBP, %5)
1289 1290
            "pxor    %%mm7, %%mm7                   \n\t"
            /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1291
#ifdef DITHER1XBPP
1292 1293 1294
            "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
            "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
            "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1295
#endif
1296 1297 1298
            WRITERGB16(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1299
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1300
               "a" (&c->redDither)
1301 1302 1303
        );
    }
}
1304

1305
#define REAL_YSCALEYUV2PACKED1(index, c) \
1306 1307 1308 1309
    "xor            "#index", "#index"  \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
1310
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1311
    "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
1312
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1313 1314 1315 1316 1317 1318
    "psraw                $7, %%mm3     \n\t" \
    "psraw                $7, %%mm4     \n\t" \
    "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
    "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
    "psraw                $7, %%mm1     \n\t" \
    "psraw                $7, %%mm7     \n\t" \
1319

1320
#define YSCALEYUV2PACKED1(index, c)  REAL_YSCALEYUV2PACKED1(index, c)
1321

1322
#define REAL_YSCALEYUV2PACKED1b(index, c) \
1323 1324 1325 1326 1327
    "xor "#index", "#index"             \n\t"\
    ".p2align              4            \n\t"\
    "1:                                 \n\t"\
    "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
    "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1328
    "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1329 1330
    "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
    "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1331
    "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1332 1333 1334 1335 1336 1337 1338 1339
    "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
    "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
    "psrlw                $8, %%mm3     \n\t" \
    "psrlw                $8, %%mm4     \n\t" \
    "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
    "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
    "psraw                $7, %%mm1     \n\t" \
    "psraw                $7, %%mm7     \n\t"
1340
#define YSCALEYUV2PACKED1b(index, c)  REAL_YSCALEYUV2PACKED1b(index, c)
1341

1342
static void RENAME(yuv2yuyv422_1)(SwsContext *c, const int16_t *buf0,
1343
                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
1344 1345
                                  const int16_t *abuf0, uint8_t *dest,
                                  int dstW, int uvalpha, int y)
1346
{
1347
    const int16_t *ubuf0 = ubuf[0];
1348
    const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1349 1350

    if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1351
        const int16_t *ubuf1 = ubuf[0];
1352
        __asm__ volatile(
1353 1354 1355 1356 1357 1358 1359
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2PACKED1(%%FF_REGBP, %5)
            WRITEYUY2(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1360
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1361
               "a" (&c->redDither)
1362 1363
        );
    } else {
1364
        const int16_t *ubuf1 = ubuf[1];
1365
        __asm__ volatile(
1366 1367 1368 1369 1370 1371 1372
            "mov %%"FF_REG_b", "ESP_OFFSET"(%5)     \n\t"
            "mov           %4, %%"FF_REG_b"         \n\t"
            "push %%"FF_REG_BP"                     \n\t"
            YSCALEYUV2PACKED1b(%%FF_REGBP, %5)
            WRITEYUY2(%%FF_REGb, 8280(%5), %%FF_REGBP)
            "pop %%"FF_REG_BP"                      \n\t"
            "mov "ESP_OFFSET"(%5), %%"FF_REG_b"     \n\t"
1373
            :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1374
               "a" (&c->redDither)
1375 1376
        );
    }
1377 1378
}

1379
#if COMPILE_TEMPLATE_MMXEXT
1380 1381 1382
static void RENAME(hyscale_fast)(SwsContext *c, int16_t *dst,
                                 int dstWidth, const uint8_t *src,
                                 int srcW, int xInc)
1383
{
1384
    int32_t *filterPos = c->hLumFilterPos;
1385
    int16_t *filter    = c->hLumFilter;
1386
    void    *mmxextFilterCode = c->lumMmxextFilterCode;
1387 1388
    int i;
#if defined(PIC)
1389 1390 1391 1392
    uint64_t ebxsave;
#endif
#if ARCH_X86_64
    uint64_t retsave;
1393
#endif
1394

1395
    __asm__ volatile(
1396
#if defined(PIC)
1397
        "mov            %%"FF_REG_b", %5            \n\t"
1398
#if ARCH_X86_64
1399 1400
        "mov               -8(%%rsp), %%"FF_REG_a"  \n\t"
        "mov            %%"FF_REG_a", %6            \n\t"
1401 1402 1403
#endif
#else
#if ARCH_X86_64
1404 1405
        "mov               -8(%%rsp), %%"FF_REG_a" \n\t"
        "mov            %%"FF_REG_a", %5           \n\t"
1406
#endif
1407
#endif
1408 1409 1410 1411 1412 1413 1414 1415 1416
        "pxor                  %%mm7, %%mm7        \n\t"
        "mov                      %0, %%"FF_REG_c" \n\t"
        "mov                      %1, %%"FF_REG_D" \n\t"
        "mov                      %2, %%"FF_REG_d" \n\t"
        "mov                      %3, %%"FF_REG_b" \n\t"
        "xor            %%"FF_REG_a", %%"FF_REG_a" \n\t" // i
        PREFETCH"     (%%"FF_REG_c")               \n\t"
        PREFETCH"   32(%%"FF_REG_c")               \n\t"
        PREFETCH"   64(%%"FF_REG_c")               \n\t"
1417 1418

#if ARCH_X86_64
1419
#define CALL_MMXEXT_FILTER_CODE \
1420 1421 1422 1423 1424 1425
        "movl               (%%"FF_REG_b"), %%esi        \n\t"\
        "call                          *%4               \n\t"\
        "movl (%%"FF_REG_b", %%"FF_REG_a"), %%esi        \n\t"\
        "add                  %%"FF_REG_S", %%"FF_REG_c" \n\t"\
        "add                  %%"FF_REG_a", %%"FF_REG_D" \n\t"\
        "xor                  %%"FF_REG_a", %%"FF_REG_a" \n\t"\
1426 1427

#else
1428
#define CALL_MMXEXT_FILTER_CODE \
1429 1430 1431 1432 1433
        "movl               (%%"FF_REG_b"), %%esi        \n\t"\
        "call                          *%4               \n\t"\
        "addl (%%"FF_REG_b", %%"FF_REG_a"), %%"FF_REG_c" \n\t"\
        "add                  %%"FF_REG_a", %%"FF_REG_D" \n\t"\
        "xor                  %%"FF_REG_a", %%"FF_REG_a" \n\t"\
1434 1435 1436

#endif /* ARCH_X86_64 */

1437 1438 1439 1440 1441 1442 1443 1444
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
1445 1446

#if defined(PIC)
1447
        "mov                      %5, %%"FF_REG_b"  \n\t"
1448
#if ARCH_X86_64
1449 1450
        "mov                      %6, %%"FF_REG_a"  \n\t"
        "mov            %%"FF_REG_a", -8(%%rsp)     \n\t"
1451 1452 1453
#endif
#else
#if ARCH_X86_64
1454 1455
        "mov                      %5, %%"FF_REG_a"  \n\t"
        "mov            %%"FF_REG_a", -8(%%rsp)     \n\t"
1456
#endif
1457
#endif
1458
        :: "m" (src), "m" (dst), "m" (filter), "m" (filterPos),
1459
           "m" (mmxextFilterCode)
1460
#if defined(PIC)
1461
          ,"m" (ebxsave)
1462 1463 1464
#endif
#if ARCH_X86_64
          ,"m"(retsave)
1465
#endif
1466
        : "%"FF_REG_a, "%"FF_REG_c, "%"FF_REG_d, "%"FF_REG_S, "%"FF_REG_D
1467
#if !defined(PIC)
1468
         ,"%"FF_REG_b
1469
#endif
1470 1471 1472 1473
    );

    for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
        dst[i] = src[srcW-1]*128;
1474 1475
}

1476 1477 1478
static void RENAME(hcscale_fast)(SwsContext *c, int16_t *dst1, int16_t *dst2,
                                 int dstWidth, const uint8_t *src1,
                                 const uint8_t *src2, int srcW, int xInc)
1479
{
1480
    int32_t *filterPos = c->hChrFilterPos;
1481
    int16_t *filter    = c->hChrFilter;
1482
    void    *mmxextFilterCode = c->chrMmxextFilterCode;
1483 1484 1485 1486
    int i;
#if defined(PIC)
    DECLARE_ALIGNED(8, uint64_t, ebxsave);
#endif
1487 1488 1489
#if ARCH_X86_64
    DECLARE_ALIGNED(8, uint64_t, retsave);
#endif
1490

1491
    __asm__ volatile(
1492
#if defined(PIC)
1493
        "mov       %%"FF_REG_b", %7             \n\t"
1494
#if ARCH_X86_64
1495 1496
        "mov          -8(%%rsp), %%"FF_REG_a"   \n\t"
        "mov       %%"FF_REG_a", %8             \n\t"
1497 1498 1499
#endif
#else
#if ARCH_X86_64
1500 1501
        "mov          -8(%%rsp), %%"FF_REG_a"   \n\t"
        "mov       %%"FF_REG_a", %7             \n\t"
1502
#endif
1503
#endif
1504 1505 1506 1507 1508 1509 1510 1511 1512
        "pxor             %%mm7, %%mm7          \n\t"
        "mov                 %0, %%"FF_REG_c"   \n\t"
        "mov                 %1, %%"FF_REG_D"   \n\t"
        "mov                 %2, %%"FF_REG_d"   \n\t"
        "mov                 %3, %%"FF_REG_b"   \n\t"
        "xor       %%"FF_REG_a", %%"FF_REG_a"   \n\t" // i
        PREFETCH"   (%%"FF_REG_c")              \n\t"
        PREFETCH" 32(%%"FF_REG_c")              \n\t"
        PREFETCH" 64(%%"FF_REG_c")              \n\t"
1513

1514 1515 1516 1517
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
1518 1519 1520 1521 1522 1523
        "xor       %%"FF_REG_a", %%"FF_REG_a"   \n\t" // i
        "mov                 %5, %%"FF_REG_c"   \n\t" // src
        "mov                 %6, %%"FF_REG_D"   \n\t" // buf2
        PREFETCH"   (%%"FF_REG_c")              \n\t"
        PREFETCH" 32(%%"FF_REG_c")              \n\t"
        PREFETCH" 64(%%"FF_REG_c")              \n\t"
1524

1525 1526 1527 1528
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
        CALL_MMXEXT_FILTER_CODE
1529 1530

#if defined(PIC)
1531
        "mov                 %7, %%"FF_REG_b"   \n\t"
1532
#if ARCH_X86_64
1533 1534
        "mov                 %8, %%"FF_REG_a"   \n\t"
        "mov       %%"FF_REG_a", -8(%%rsp)      \n\t"
1535 1536 1537
#endif
#else
#if ARCH_X86_64
1538 1539
        "mov                 %7, %%"FF_REG_a"   \n\t"
        "mov       %%"FF_REG_a", -8(%%rsp)      \n\t"
1540
#endif
1541
#endif
1542
        :: "m" (src1), "m" (dst1), "m" (filter), "m" (filterPos),
1543
           "m" (mmxextFilterCode), "m" (src2), "m"(dst2)
1544
#if defined(PIC)
1545
          ,"m" (ebxsave)
1546 1547 1548
#endif
#if ARCH_X86_64
          ,"m"(retsave)
1549
#endif
1550
        : "%"FF_REG_a, "%"FF_REG_c, "%"FF_REG_d, "%"FF_REG_S, "%"FF_REG_D
1551
#if !defined(PIC)
1552
         ,"%"FF_REG_b
1553
#endif
1554 1555 1556
    );

    for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
1557 1558
        dst1[i] = src1[srcW-1]*128;
        dst2[i] = src2[srcW-1]*128;
1559
    }
1560
}
1561
#endif /* COMPILE_TEMPLATE_MMXEXT */
1562

1563
static av_cold void RENAME(sws_init_swscale)(SwsContext *c)
1564
{
1565
    enum AVPixelFormat dstFormat = c->dstFormat;
1566

1567
    if (!is16BPS(dstFormat) && !is9_15BPS(dstFormat) &&
1568
        dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21) {
1569 1570 1571 1572
        if (!(c->flags & SWS_BITEXACT)) {
            if (c->flags & SWS_ACCURATE_RND) {
                if (!(c->flags & SWS_FULL_CHR_H_INT)) {
                    switch (c->dstFormat) {
1573 1574 1575 1576 1577
                    case AV_PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X_ar);   break;
                    case AV_PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X_ar);   break;
                    case AV_PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X_ar);  break;
                    case AV_PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X_ar);  break;
                    case AV_PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X_ar); break;
1578 1579 1580 1581 1582 1583
                    default: break;
                    }
                }
            } else {
                if (!(c->flags & SWS_FULL_CHR_H_INT)) {
                    switch (c->dstFormat) {
1584 1585 1586 1587 1588
                    case AV_PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X);   break;
                    case AV_PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X);   break;
                    case AV_PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X);  break;
                    case AV_PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X);  break;
                    case AV_PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X); break;
1589 1590 1591
                    default: break;
                    }
                }
1592 1593
            }
        }
1594
        if (!(c->flags & SWS_FULL_CHR_H_INT)) {
1595
            switch (c->dstFormat) {
1596
            case AV_PIX_FMT_RGB32:
1597 1598 1599
                c->yuv2packed1 = RENAME(yuv2rgb32_1);
                c->yuv2packed2 = RENAME(yuv2rgb32_2);
                break;
1600
            case AV_PIX_FMT_BGR24:
1601 1602 1603
                c->yuv2packed1 = RENAME(yuv2bgr24_1);
                c->yuv2packed2 = RENAME(yuv2bgr24_2);
                break;
1604
            case AV_PIX_FMT_RGB555:
1605 1606 1607
                c->yuv2packed1 = RENAME(yuv2rgb555_1);
                c->yuv2packed2 = RENAME(yuv2rgb555_2);
                break;
1608
            case AV_PIX_FMT_RGB565:
1609 1610 1611
                c->yuv2packed1 = RENAME(yuv2rgb565_1);
                c->yuv2packed2 = RENAME(yuv2rgb565_2);
                break;
1612
            case AV_PIX_FMT_YUYV422:
1613 1614 1615
                c->yuv2packed1 = RENAME(yuv2yuyv422_1);
                c->yuv2packed2 = RENAME(yuv2yuyv422_2);
                break;
1616
            default:
1617
                break;
1618
            }
1619
        }
1620
    }
1621

1622
    if (c->srcBpc == 8 && c->dstBpc <= 12) {
1623
    // Use the new MMX scaler if the MMXEXT one can't be used (it is faster than the x86 ASM one).
1624
#if COMPILE_TEMPLATE_MMXEXT
1625
    if (c->flags & SWS_FAST_BILINEAR && c->canMMXEXTBeUsed) {
1626 1627 1628
        c->hyscale_fast = RENAME(hyscale_fast);
        c->hcscale_fast = RENAME(hcscale_fast);
    } else {
1629
#endif /* COMPILE_TEMPLATE_MMXEXT */
1630 1631
        c->hyscale_fast = NULL;
        c->hcscale_fast = NULL;
1632
#if COMPILE_TEMPLATE_MMXEXT
1633
    }
1634
#endif /* COMPILE_TEMPLATE_MMXEXT */
1635
    }
1636
}