rgb2rgb_template.c 104 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 24 25 26 27
/*
 * software RGB to RGB converter
 * pluralize by software PAL8 to RGB converter
 *              software YUV to YUV converter
 *              software YUV to RGB converter
 * Written by Nick Kurshev.
 * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
 * lot of big-endian byte order fixes by Alex Beregszaszi
 *
 * 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
 */

#include <stddef.h>
28
#include <stdint.h>
29

30
#include "libavutil/attributes.h"
31
#include "libavutil/x86/asm.h"
32

33 34 35 36 37 38 39 40 41
#undef PREFETCH
#undef MOVNTQ
#undef EMMS
#undef SFENCE
#undef PAVGB

#if COMPILE_TEMPLATE_AMD3DNOW
#define PREFETCH  "prefetch"
#define PAVGB     "pavgusb"
42
#elif COMPILE_TEMPLATE_MMXEXT
43 44 45 46 47 48 49 50 51 52 53 54 55
#define PREFETCH "prefetchnta"
#define PAVGB     "pavgb"
#else
#define PREFETCH  " # nop"
#endif

#if COMPILE_TEMPLATE_AMD3DNOW
/* On K6 femms is faster than emms. On K7 femms is directly mapped to emms. */
#define EMMS     "femms"
#else
#define EMMS     "emms"
#endif

56
#if COMPILE_TEMPLATE_MMXEXT
57 58 59 60 61 62 63
#define MOVNTQ "movntq"
#define SFENCE "sfence"
#else
#define MOVNTQ "movq"
#define SFENCE " # nop"
#endif

64 65 66 67
#if !COMPILE_TEMPLATE_SSE2

#if !COMPILE_TEMPLATE_AMD3DNOW

68
static inline void RENAME(rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int src_size)
69 70 71 72 73 74 75 76 77 78 79
{
    uint8_t *dest = dst;
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    mm_end = end - 23;
    __asm__ volatile("movq        %0, %%mm7"::"m"(mask32a):"memory");
    while (s < mm_end) {
        __asm__ volatile(
80 81 82 83 84 85 86 87 88
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "punpckldq  3(%1), %%mm0    \n\t"
            "movd       6(%1), %%mm1    \n\t"
            "punpckldq  9(%1), %%mm1    \n\t"
            "movd      12(%1), %%mm2    \n\t"
            "punpckldq 15(%1), %%mm2    \n\t"
            "movd      18(%1), %%mm3    \n\t"
            "punpckldq 21(%1), %%mm3    \n\t"
89 90 91 92
            "por        %%mm7, %%mm0    \n\t"
            "por        %%mm7, %%mm1    \n\t"
            "por        %%mm7, %%mm2    \n\t"
            "por        %%mm7, %%mm3    \n\t"
93 94 95 96 97
            MOVNTQ"     %%mm0,   (%0)   \n\t"
            MOVNTQ"     %%mm1,  8(%0)   \n\t"
            MOVNTQ"     %%mm2, 16(%0)   \n\t"
            MOVNTQ"     %%mm3, 24(%0)"
            :: "r"(dest), "r"(s)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            :"memory");
        dest += 32;
        s += 24;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        *dest++ = *s++;
        *dest++ = *s++;
        *dest++ = *s++;
        *dest++ = 255;
    }
}

#define STORE_BGR24_MMX \
            "psrlq         $8, %%mm2    \n\t" \
            "psrlq         $8, %%mm3    \n\t" \
            "psrlq         $8, %%mm6    \n\t" \
            "psrlq         $8, %%mm7    \n\t" \
            "pand "MANGLE(mask24l)", %%mm0\n\t" \
            "pand "MANGLE(mask24l)", %%mm1\n\t" \
            "pand "MANGLE(mask24l)", %%mm4\n\t" \
            "pand "MANGLE(mask24l)", %%mm5\n\t" \
            "pand "MANGLE(mask24h)", %%mm2\n\t" \
            "pand "MANGLE(mask24h)", %%mm3\n\t" \
            "pand "MANGLE(mask24h)", %%mm6\n\t" \
            "pand "MANGLE(mask24h)", %%mm7\n\t" \
            "por        %%mm2, %%mm0    \n\t" \
            "por        %%mm3, %%mm1    \n\t" \
            "por        %%mm6, %%mm4    \n\t" \
            "por        %%mm7, %%mm5    \n\t" \
 \
            "movq       %%mm1, %%mm2    \n\t" \
            "movq       %%mm4, %%mm3    \n\t" \
            "psllq        $48, %%mm2    \n\t" \
            "psllq        $32, %%mm3    \n\t" \
            "por        %%mm2, %%mm0    \n\t" \
            "psrlq        $16, %%mm1    \n\t" \
            "psrlq        $32, %%mm4    \n\t" \
            "psllq        $16, %%mm5    \n\t" \
            "por        %%mm3, %%mm1    \n\t" \
            "por        %%mm5, %%mm4    \n\t" \
 \
141 142 143
            MOVNTQ"     %%mm0,   (%0)    \n\t" \
            MOVNTQ"     %%mm1,  8(%0)    \n\t" \
            MOVNTQ"     %%mm4, 16(%0)"
144 145


146
static inline void RENAME(rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int src_size)
147 148 149 150 151 152 153 154 155 156
{
    uint8_t *dest = dst;
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    mm_end = end - 31;
    while (s < mm_end) {
        __asm__ volatile(
157 158 159 160 161
            PREFETCH"  32(%1)           \n\t"
            "movq        (%1), %%mm0    \n\t"
            "movq       8(%1), %%mm1    \n\t"
            "movq      16(%1), %%mm4    \n\t"
            "movq      24(%1), %%mm5    \n\t"
162 163 164 165 166
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm1, %%mm3    \n\t"
            "movq       %%mm4, %%mm6    \n\t"
            "movq       %%mm5, %%mm7    \n\t"
            STORE_BGR24_MMX
167
            :: "r"(dest), "r"(s)
168
              NAMED_CONSTRAINTS_ADD(mask24l,mask24h)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            :"memory");
        dest += 24;
        s += 32;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        *dest++ = *s++;
        *dest++ = *s++;
        *dest++ = *s++;
        s++;
    }
}

/*
 original by Strepto/Astral
 ported to gcc & bugfixed: A'rpi
186
 MMXEXT, 3DNOW optimization by Nick Kurshev
187 188
 32-bit C version, and and&add trick by Michael Niedermayer
*/
189
static inline void RENAME(rgb15to16)(const uint8_t *src, uint8_t *dst, int src_size)
190 191 192 193 194 195 196 197 198 199 200
{
    register const uint8_t* s=src;
    register uint8_t* d=dst;
    register const uint8_t *end;
    const uint8_t *mm_end;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s));
    __asm__ volatile("movq        %0, %%mm4"::"m"(mask15s));
    mm_end = end - 15;
    while (s<mm_end) {
        __asm__ volatile(
201 202 203
            PREFETCH" 32(%1)        \n\t"
            "movq      (%1), %%mm0  \n\t"
            "movq     8(%1), %%mm2  \n\t"
204 205 206 207 208 209
            "movq     %%mm0, %%mm1  \n\t"
            "movq     %%mm2, %%mm3  \n\t"
            "pand     %%mm4, %%mm0  \n\t"
            "pand     %%mm4, %%mm2  \n\t"
            "paddw    %%mm1, %%mm0  \n\t"
            "paddw    %%mm3, %%mm2  \n\t"
210 211 212
            MOVNTQ"   %%mm0,  (%0)  \n\t"
            MOVNTQ"   %%mm2, 8(%0)"
            :: "r"(d), "r"(s)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        );
        d+=16;
        s+=16;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    mm_end = end - 3;
    while (s < mm_end) {
        register unsigned x= *((const uint32_t *)s);
        *((uint32_t *)d) = (x&0x7FFF7FFF) + (x&0x7FE07FE0);
        d+=4;
        s+=4;
    }
    if (s < end) {
        register unsigned short x= *((const uint16_t *)s);
        *((uint16_t *)d) = (x&0x7FFF) + (x&0x7FE0);
    }
}

232
static inline void RENAME(rgb16to15)(const uint8_t *src, uint8_t *dst, int src_size)
233 234 235 236 237 238 239 240 241 242 243 244
{
    register const uint8_t* s=src;
    register uint8_t* d=dst;
    register const uint8_t *end;
    const uint8_t *mm_end;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s));
    __asm__ volatile("movq        %0, %%mm7"::"m"(mask15rg));
    __asm__ volatile("movq        %0, %%mm6"::"m"(mask15b));
    mm_end = end - 15;
    while (s<mm_end) {
        __asm__ volatile(
245 246 247
            PREFETCH" 32(%1)        \n\t"
            "movq      (%1), %%mm0  \n\t"
            "movq     8(%1), %%mm2  \n\t"
248 249 250 251 252 253 254 255 256 257
            "movq     %%mm0, %%mm1  \n\t"
            "movq     %%mm2, %%mm3  \n\t"
            "psrlq       $1, %%mm0  \n\t"
            "psrlq       $1, %%mm2  \n\t"
            "pand     %%mm7, %%mm0  \n\t"
            "pand     %%mm7, %%mm2  \n\t"
            "pand     %%mm6, %%mm1  \n\t"
            "pand     %%mm6, %%mm3  \n\t"
            "por      %%mm1, %%mm0  \n\t"
            "por      %%mm3, %%mm2  \n\t"
258 259 260
            MOVNTQ"   %%mm0,  (%0)  \n\t"
            MOVNTQ"   %%mm2, 8(%0)"
            :: "r"(d), "r"(s)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        );
        d+=16;
        s+=16;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    mm_end = end - 3;
    while (s < mm_end) {
        register uint32_t x= *((const uint32_t*)s);
        *((uint32_t *)d) = ((x>>1)&0x7FE07FE0) | (x&0x001F001F);
        s+=4;
        d+=4;
    }
    if (s < end) {
        register uint16_t x= *((const uint16_t*)s);
        *((uint16_t *)d) = ((x>>1)&0x7FE0) | (x&0x001F);
    }
}

280
static inline void RENAME(rgb32to16)(const uint8_t *src, uint8_t *dst, int src_size)
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 318 319 320 321 322 323 324 325 326 327 328 329
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    mm_end = end - 15;
    __asm__ volatile(
        "movq           %3, %%mm5   \n\t"
        "movq           %4, %%mm6   \n\t"
        "movq           %5, %%mm7   \n\t"
        "jmp 2f                     \n\t"
        ".p2align        4          \n\t"
        "1:                         \n\t"
        PREFETCH"   32(%1)          \n\t"
        "movd         (%1), %%mm0   \n\t"
        "movd        4(%1), %%mm3   \n\t"
        "punpckldq   8(%1), %%mm0   \n\t"
        "punpckldq  12(%1), %%mm3   \n\t"
        "movq        %%mm0, %%mm1   \n\t"
        "movq        %%mm3, %%mm4   \n\t"
        "pand        %%mm6, %%mm0   \n\t"
        "pand        %%mm6, %%mm3   \n\t"
        "pmaddwd     %%mm7, %%mm0   \n\t"
        "pmaddwd     %%mm7, %%mm3   \n\t"
        "pand        %%mm5, %%mm1   \n\t"
        "pand        %%mm5, %%mm4   \n\t"
        "por         %%mm1, %%mm0   \n\t"
        "por         %%mm4, %%mm3   \n\t"
        "psrld          $5, %%mm0   \n\t"
        "pslld         $11, %%mm3   \n\t"
        "por         %%mm3, %%mm0   \n\t"
        MOVNTQ"      %%mm0, (%0)    \n\t"
        "add           $16,  %1     \n\t"
        "add            $8,  %0     \n\t"
        "2:                         \n\t"
        "cmp            %2,  %1     \n\t"
        " jb            1b          \n\t"
        : "+r" (d), "+r"(s)
        : "r" (mm_end), "m" (mask3216g), "m" (mask3216br), "m" (mul3216)
    );
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register int rgb = *(const uint32_t*)s; s += 4;
        *d++ = ((rgb&0xFF)>>3) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>8);
    }
}

330
static inline void RENAME(rgb32tobgr16)(const uint8_t *src, uint8_t *dst, int src_size)
331 332 333 334 335 336 337 338 339 340 341 342 343 344
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq          %0, %%mm7    \n\t"
        "movq          %1, %%mm6    \n\t"
        ::"m"(red_16mask),"m"(green_16mask));
    mm_end = end - 15;
    while (s < mm_end) {
        __asm__ volatile(
345 346 347 348 349
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "movd       4(%1), %%mm3    \n\t"
            "punpckldq  8(%1), %%mm0    \n\t"
            "punpckldq 12(%1), %%mm3    \n\t"
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
            "movq       %%mm0, %%mm1    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm3, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "psllq         $8, %%mm0    \n\t"
            "psllq         $8, %%mm3    \n\t"
            "pand       %%mm7, %%mm0    \n\t"
            "pand       %%mm7, %%mm3    \n\t"
            "psrlq         $5, %%mm1    \n\t"
            "psrlq         $5, %%mm4    \n\t"
            "pand       %%mm6, %%mm1    \n\t"
            "pand       %%mm6, %%mm4    \n\t"
            "psrlq        $19, %%mm2    \n\t"
            "psrlq        $19, %%mm5    \n\t"
            "pand          %2, %%mm2    \n\t"
            "pand          %2, %%mm5    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            "psllq        $16, %%mm3    \n\t"
            "por        %%mm3, %%mm0    \n\t"
372 373
            MOVNTQ"     %%mm0, (%0)     \n\t"
            :: "r"(d),"r"(s),"m"(blue_16mask):"memory");
374 375 376 377 378 379 380 381 382 383 384
        d += 4;
        s += 16;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register int rgb = *(const uint32_t*)s; s += 4;
        *d++ = ((rgb&0xF8)<<8) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>19);
    }
}

385
static inline void RENAME(rgb32to15)(const uint8_t *src, uint8_t *dst, int src_size)
386 387 388 389 390 391 392 393 394 395 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 426 427 428 429 430 431 432 433 434
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    mm_end = end - 15;
    __asm__ volatile(
        "movq           %3, %%mm5   \n\t"
        "movq           %4, %%mm6   \n\t"
        "movq           %5, %%mm7   \n\t"
        "jmp            2f          \n\t"
        ".p2align        4          \n\t"
        "1:                         \n\t"
        PREFETCH"   32(%1)          \n\t"
        "movd         (%1), %%mm0   \n\t"
        "movd        4(%1), %%mm3   \n\t"
        "punpckldq   8(%1), %%mm0   \n\t"
        "punpckldq  12(%1), %%mm3   \n\t"
        "movq        %%mm0, %%mm1   \n\t"
        "movq        %%mm3, %%mm4   \n\t"
        "pand        %%mm6, %%mm0   \n\t"
        "pand        %%mm6, %%mm3   \n\t"
        "pmaddwd     %%mm7, %%mm0   \n\t"
        "pmaddwd     %%mm7, %%mm3   \n\t"
        "pand        %%mm5, %%mm1   \n\t"
        "pand        %%mm5, %%mm4   \n\t"
        "por         %%mm1, %%mm0   \n\t"
        "por         %%mm4, %%mm3   \n\t"
        "psrld          $6, %%mm0   \n\t"
        "pslld         $10, %%mm3   \n\t"
        "por         %%mm3, %%mm0   \n\t"
        MOVNTQ"      %%mm0, (%0)    \n\t"
        "add           $16,  %1     \n\t"
        "add            $8,  %0     \n\t"
        "2:                         \n\t"
        "cmp            %2,  %1     \n\t"
        " jb            1b          \n\t"
        : "+r" (d), "+r"(s)
        : "r" (mm_end), "m" (mask3215g), "m" (mask3216br), "m" (mul3215)
    );
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register int rgb = *(const uint32_t*)s; s += 4;
        *d++ = ((rgb&0xFF)>>3) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>9);
    }
}

435
static inline void RENAME(rgb32tobgr15)(const uint8_t *src, uint8_t *dst, int src_size)
436 437 438 439 440 441 442 443 444 445 446 447 448 449
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq          %0, %%mm7    \n\t"
        "movq          %1, %%mm6    \n\t"
        ::"m"(red_15mask),"m"(green_15mask));
    mm_end = end - 15;
    while (s < mm_end) {
        __asm__ volatile(
450 451 452 453 454
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "movd       4(%1), %%mm3    \n\t"
            "punpckldq  8(%1), %%mm0    \n\t"
            "punpckldq 12(%1), %%mm3    \n\t"
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            "movq       %%mm0, %%mm1    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm3, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "psllq         $7, %%mm0    \n\t"
            "psllq         $7, %%mm3    \n\t"
            "pand       %%mm7, %%mm0    \n\t"
            "pand       %%mm7, %%mm3    \n\t"
            "psrlq         $6, %%mm1    \n\t"
            "psrlq         $6, %%mm4    \n\t"
            "pand       %%mm6, %%mm1    \n\t"
            "pand       %%mm6, %%mm4    \n\t"
            "psrlq        $19, %%mm2    \n\t"
            "psrlq        $19, %%mm5    \n\t"
            "pand          %2, %%mm2    \n\t"
            "pand          %2, %%mm5    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            "psllq        $16, %%mm3    \n\t"
            "por        %%mm3, %%mm0    \n\t"
477 478
            MOVNTQ"     %%mm0, (%0)     \n\t"
            ::"r"(d),"r"(s),"m"(blue_15mask):"memory");
479 480 481 482 483 484 485 486 487 488 489
        d += 4;
        s += 16;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register int rgb = *(const uint32_t*)s; s += 4;
        *d++ = ((rgb&0xF8)<<7) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>19);
    }
}

490
static inline void RENAME(rgb24tobgr16)(const uint8_t *src, uint8_t *dst, int src_size)
491 492 493 494 495 496 497 498 499 500 501 502 503 504
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq         %0, %%mm7     \n\t"
        "movq         %1, %%mm6     \n\t"
        ::"m"(red_16mask),"m"(green_16mask));
    mm_end = end - 11;
    while (s < mm_end) {
        __asm__ volatile(
505 506 507 508 509
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "movd       3(%1), %%mm3    \n\t"
            "punpckldq  6(%1), %%mm0    \n\t"
            "punpckldq  9(%1), %%mm3    \n\t"
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
            "movq       %%mm0, %%mm1    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm3, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "psrlq         $3, %%mm0    \n\t"
            "psrlq         $3, %%mm3    \n\t"
            "pand          %2, %%mm0    \n\t"
            "pand          %2, %%mm3    \n\t"
            "psrlq         $5, %%mm1    \n\t"
            "psrlq         $5, %%mm4    \n\t"
            "pand       %%mm6, %%mm1    \n\t"
            "pand       %%mm6, %%mm4    \n\t"
            "psrlq         $8, %%mm2    \n\t"
            "psrlq         $8, %%mm5    \n\t"
            "pand       %%mm7, %%mm2    \n\t"
            "pand       %%mm7, %%mm5    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            "psllq        $16, %%mm3    \n\t"
            "por        %%mm3, %%mm0    \n\t"
532 533
            MOVNTQ"     %%mm0, (%0)     \n\t"
            ::"r"(d),"r"(s),"m"(blue_16mask):"memory");
534 535 536 537 538 539 540 541 542 543 544 545 546
        d += 4;
        s += 12;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        const int b = *s++;
        const int g = *s++;
        const int r = *s++;
        *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
    }
}

547
static inline void RENAME(rgb24to16)(const uint8_t *src, uint8_t *dst, int src_size)
548 549 550 551 552 553 554 555 556 557 558 559 560 561
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq         %0, %%mm7     \n\t"
        "movq         %1, %%mm6     \n\t"
        ::"m"(red_16mask),"m"(green_16mask));
    mm_end = end - 15;
    while (s < mm_end) {
        __asm__ volatile(
562 563 564 565 566
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "movd       3(%1), %%mm3    \n\t"
            "punpckldq  6(%1), %%mm0    \n\t"
            "punpckldq  9(%1), %%mm3    \n\t"
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
            "movq       %%mm0, %%mm1    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm3, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "psllq         $8, %%mm0    \n\t"
            "psllq         $8, %%mm3    \n\t"
            "pand       %%mm7, %%mm0    \n\t"
            "pand       %%mm7, %%mm3    \n\t"
            "psrlq         $5, %%mm1    \n\t"
            "psrlq         $5, %%mm4    \n\t"
            "pand       %%mm6, %%mm1    \n\t"
            "pand       %%mm6, %%mm4    \n\t"
            "psrlq        $19, %%mm2    \n\t"
            "psrlq        $19, %%mm5    \n\t"
            "pand          %2, %%mm2    \n\t"
            "pand          %2, %%mm5    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            "psllq        $16, %%mm3    \n\t"
            "por        %%mm3, %%mm0    \n\t"
589 590
            MOVNTQ"     %%mm0, (%0)     \n\t"
            ::"r"(d),"r"(s),"m"(blue_16mask):"memory");
591 592 593 594 595 596 597 598 599 600 601 602 603
        d += 4;
        s += 12;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        const int r = *s++;
        const int g = *s++;
        const int b = *s++;
        *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
    }
}

604
static inline void RENAME(rgb24tobgr15)(const uint8_t *src, uint8_t *dst, int src_size)
605 606 607 608 609 610 611 612 613 614 615 616 617 618
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq          %0, %%mm7    \n\t"
        "movq          %1, %%mm6    \n\t"
        ::"m"(red_15mask),"m"(green_15mask));
    mm_end = end - 11;
    while (s < mm_end) {
        __asm__ volatile(
619 620 621 622 623
            PREFETCH"  32(%1)           \n\t"
            "movd        (%1), %%mm0    \n\t"
            "movd       3(%1), %%mm3    \n\t"
            "punpckldq  6(%1), %%mm0    \n\t"
            "punpckldq  9(%1), %%mm3    \n\t"
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
            "movq       %%mm0, %%mm1    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm3, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "psrlq         $3, %%mm0    \n\t"
            "psrlq         $3, %%mm3    \n\t"
            "pand          %2, %%mm0    \n\t"
            "pand          %2, %%mm3    \n\t"
            "psrlq         $6, %%mm1    \n\t"
            "psrlq         $6, %%mm4    \n\t"
            "pand       %%mm6, %%mm1    \n\t"
            "pand       %%mm6, %%mm4    \n\t"
            "psrlq         $9, %%mm2    \n\t"
            "psrlq         $9, %%mm5    \n\t"
            "pand       %%mm7, %%mm2    \n\t"
            "pand       %%mm7, %%mm5    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            "psllq        $16, %%mm3    \n\t"
            "por        %%mm3, %%mm0    \n\t"
646 647
            MOVNTQ"     %%mm0, (%0)     \n\t"
            ::"r"(d),"r"(s),"m"(blue_15mask):"memory");
648 649 650 651 652 653 654 655 656 657 658 659 660
        d += 4;
        s += 12;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        const int b = *s++;
        const int g = *s++;
        const int r = *s++;
        *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
    }
}

661
static inline void RENAME(rgb24to15)(const uint8_t *src, uint8_t *dst, int src_size)
662 663 664 665 666 667 668 669 670 671 672 673 674 675
{
    const uint8_t *s = src;
    const uint8_t *end;
    const uint8_t *mm_end;
    uint16_t *d = (uint16_t *)dst;
    end = s + src_size;
    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");
    __asm__ volatile(
        "movq         %0, %%mm7     \n\t"
        "movq         %1, %%mm6     \n\t"
        ::"m"(red_15mask),"m"(green_15mask));
    mm_end = end - 15;
    while (s < mm_end) {
        __asm__ volatile(
676 677 678 679 680
            PREFETCH" 32(%1)            \n\t"
            "movd       (%1), %%mm0     \n\t"
            "movd      3(%1), %%mm3     \n\t"
            "punpckldq 6(%1), %%mm0     \n\t"
            "punpckldq 9(%1), %%mm3     \n\t"
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
            "movq      %%mm0, %%mm1     \n\t"
            "movq      %%mm0, %%mm2     \n\t"
            "movq      %%mm3, %%mm4     \n\t"
            "movq      %%mm3, %%mm5     \n\t"
            "psllq        $7, %%mm0     \n\t"
            "psllq        $7, %%mm3     \n\t"
            "pand      %%mm7, %%mm0     \n\t"
            "pand      %%mm7, %%mm3     \n\t"
            "psrlq        $6, %%mm1     \n\t"
            "psrlq        $6, %%mm4     \n\t"
            "pand      %%mm6, %%mm1     \n\t"
            "pand      %%mm6, %%mm4     \n\t"
            "psrlq       $19, %%mm2     \n\t"
            "psrlq       $19, %%mm5     \n\t"
            "pand         %2, %%mm2     \n\t"
            "pand         %2, %%mm5     \n\t"
            "por       %%mm1, %%mm0     \n\t"
            "por       %%mm4, %%mm3     \n\t"
            "por       %%mm2, %%mm0     \n\t"
            "por       %%mm5, %%mm3     \n\t"
            "psllq       $16, %%mm3     \n\t"
            "por       %%mm3, %%mm0     \n\t"
703 704
            MOVNTQ"    %%mm0, (%0)      \n\t"
            ::"r"(d),"r"(s),"m"(blue_15mask):"memory");
705 706 707 708 709 710 711 712 713 714 715 716 717
        d += 4;
        s += 12;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        const int r = *s++;
        const int g = *s++;
        const int b = *s++;
        *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
    }
}

718
static inline void RENAME(rgb15tobgr24)(const uint8_t *src, uint8_t *dst, int src_size)
719 720 721 722 723 724 725 726 727 728
{
    const uint16_t *end;
    const uint16_t *mm_end;
    uint8_t *d = dst;
    const uint16_t *s = (const uint16_t*)src;
    end = s + src_size/2;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    mm_end = end - 7;
    while (s < mm_end) {
        __asm__ volatile(
729 730 731 732
            PREFETCH"  32(%1)           \n\t"
            "movq        (%1), %%mm0    \n\t"
            "movq        (%1), %%mm1    \n\t"
            "movq        (%1), %%mm2    \n\t"
733 734 735
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
736
            "psllq         $5, %%mm0    \n\t"
737 738 739
            "pmulhw        "MANGLE(mul15_mid)", %%mm0    \n\t"
            "pmulhw        "MANGLE(mul15_mid)", %%mm1    \n\t"
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
            "movq       %%mm0, %%mm3    \n\t"
            "movq       %%mm1, %%mm4    \n\t"
            "movq       %%mm2, %%mm5    \n\t"
            "punpcklwd     %5, %%mm0    \n\t"
            "punpcklwd     %5, %%mm1    \n\t"
            "punpcklwd     %5, %%mm2    \n\t"
            "punpckhwd     %5, %%mm3    \n\t"
            "punpckhwd     %5, %%mm4    \n\t"
            "punpckhwd     %5, %%mm5    \n\t"
            "psllq         $8, %%mm1    \n\t"
            "psllq        $16, %%mm2    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "psllq         $8, %%mm4    \n\t"
            "psllq        $16, %%mm5    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm5, %%mm3    \n\t"

            "movq       %%mm0, %%mm6    \n\t"
            "movq       %%mm3, %%mm7    \n\t"

761 762 763
            "movq       8(%1), %%mm0    \n\t"
            "movq       8(%1), %%mm1    \n\t"
            "movq       8(%1), %%mm2    \n\t"
764 765 766
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
767
            "psllq         $5, %%mm0    \n\t"
768 769 770
            "pmulhw        "MANGLE(mul15_mid)", %%mm0    \n\t"
            "pmulhw        "MANGLE(mul15_mid)", %%mm1    \n\t"
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
            "movq       %%mm0, %%mm3    \n\t"
            "movq       %%mm1, %%mm4    \n\t"
            "movq       %%mm2, %%mm5    \n\t"
            "punpcklwd     %5, %%mm0    \n\t"
            "punpcklwd     %5, %%mm1    \n\t"
            "punpcklwd     %5, %%mm2    \n\t"
            "punpckhwd     %5, %%mm3    \n\t"
            "punpckhwd     %5, %%mm4    \n\t"
            "punpckhwd     %5, %%mm5    \n\t"
            "psllq         $8, %%mm1    \n\t"
            "psllq        $16, %%mm2    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "psllq         $8, %%mm4    \n\t"
            "psllq        $16, %%mm5    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm5, %%mm3    \n\t"

            :"=m"(*d)
790
            :"r"(s),"m"(mask15b),"m"(mask15g),"m"(mask15r), "m"(mmx_null)
791
             NAMED_CONSTRAINTS_ADD(mul15_mid,mul15_hi)
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
            :"memory");
        /* borrowed 32 to 24 */
        __asm__ volatile(
            "movq       %%mm0, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "movq       %%mm6, %%mm0    \n\t"
            "movq       %%mm7, %%mm1    \n\t"

            "movq       %%mm4, %%mm6    \n\t"
            "movq       %%mm5, %%mm7    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm1, %%mm3    \n\t"

            STORE_BGR24_MMX

807
            :: "r"(d), "m"(*s)
808
              NAMED_CONSTRAINTS_ADD(mask24l,mask24h)
809 810 811 812 813 814 815 816 817
            :"memory");
        d += 24;
        s += 8;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register uint16_t bgr;
        bgr = *s++;
818 819 820
        *d++ = ((bgr&0x1F)<<3) | ((bgr&0x1F)>>2);
        *d++ = ((bgr&0x3E0)>>2) | ((bgr&0x3E0)>>7);
        *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
821 822 823
    }
}

824
static inline void RENAME(rgb16tobgr24)(const uint8_t *src, uint8_t *dst, int src_size)
825 826 827 828 829 830 831 832 833 834
{
    const uint16_t *end;
    const uint16_t *mm_end;
    uint8_t *d = (uint8_t *)dst;
    const uint16_t *s = (const uint16_t *)src;
    end = s + src_size/2;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    mm_end = end - 7;
    while (s < mm_end) {
        __asm__ volatile(
835 836 837 838
            PREFETCH"  32(%1)           \n\t"
            "movq        (%1), %%mm0    \n\t"
            "movq        (%1), %%mm1    \n\t"
            "movq        (%1), %%mm2    \n\t"
839 840 841
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
842 843
            "psllq         $5, %%mm0    \n\t"
            "psrlq         $1, %%mm2    \n\t"
844 845 846
            "pmulhw        "MANGLE(mul15_mid)", %%mm0    \n\t"
            "pmulhw        "MANGLE(mul16_mid)", %%mm1    \n\t"
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
            "movq       %%mm0, %%mm3    \n\t"
            "movq       %%mm1, %%mm4    \n\t"
            "movq       %%mm2, %%mm5    \n\t"
            "punpcklwd     %5, %%mm0    \n\t"
            "punpcklwd     %5, %%mm1    \n\t"
            "punpcklwd     %5, %%mm2    \n\t"
            "punpckhwd     %5, %%mm3    \n\t"
            "punpckhwd     %5, %%mm4    \n\t"
            "punpckhwd     %5, %%mm5    \n\t"
            "psllq         $8, %%mm1    \n\t"
            "psllq        $16, %%mm2    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "psllq         $8, %%mm4    \n\t"
            "psllq        $16, %%mm5    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm5, %%mm3    \n\t"

            "movq       %%mm0, %%mm6    \n\t"
            "movq       %%mm3, %%mm7    \n\t"

868 869 870
            "movq       8(%1), %%mm0    \n\t"
            "movq       8(%1), %%mm1    \n\t"
            "movq       8(%1), %%mm2    \n\t"
871 872 873
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
874 875
            "psllq         $5, %%mm0    \n\t"
            "psrlq         $1, %%mm2    \n\t"
876 877 878
            "pmulhw        "MANGLE(mul15_mid)", %%mm0    \n\t"
            "pmulhw        "MANGLE(mul16_mid)", %%mm1    \n\t"
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
            "movq       %%mm0, %%mm3    \n\t"
            "movq       %%mm1, %%mm4    \n\t"
            "movq       %%mm2, %%mm5    \n\t"
            "punpcklwd     %5, %%mm0    \n\t"
            "punpcklwd     %5, %%mm1    \n\t"
            "punpcklwd     %5, %%mm2    \n\t"
            "punpckhwd     %5, %%mm3    \n\t"
            "punpckhwd     %5, %%mm4    \n\t"
            "punpckhwd     %5, %%mm5    \n\t"
            "psllq         $8, %%mm1    \n\t"
            "psllq        $16, %%mm2    \n\t"
            "por        %%mm1, %%mm0    \n\t"
            "por        %%mm2, %%mm0    \n\t"
            "psllq         $8, %%mm4    \n\t"
            "psllq        $16, %%mm5    \n\t"
            "por        %%mm4, %%mm3    \n\t"
            "por        %%mm5, %%mm3    \n\t"
            :"=m"(*d)
897
            :"r"(s),"m"(mask16b),"m"(mask16g),"m"(mask16r),"m"(mmx_null)
898
             NAMED_CONSTRAINTS_ADD(mul15_mid,mul16_mid,mul15_hi)
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
            :"memory");
        /* borrowed 32 to 24 */
        __asm__ volatile(
            "movq       %%mm0, %%mm4    \n\t"
            "movq       %%mm3, %%mm5    \n\t"
            "movq       %%mm6, %%mm0    \n\t"
            "movq       %%mm7, %%mm1    \n\t"

            "movq       %%mm4, %%mm6    \n\t"
            "movq       %%mm5, %%mm7    \n\t"
            "movq       %%mm0, %%mm2    \n\t"
            "movq       %%mm1, %%mm3    \n\t"

            STORE_BGR24_MMX

914
            :: "r"(d), "m"(*s)
915
              NAMED_CONSTRAINTS_ADD(mask24l,mask24h)
916 917 918 919 920 921 922 923 924
            :"memory");
        d += 24;
        s += 8;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register uint16_t bgr;
        bgr = *s++;
925 926 927
        *d++ = ((bgr&0x1F)<<3) | ((bgr&0x1F)>>2);
        *d++ = ((bgr&0x7E0)>>3) | ((bgr&0x7E0)>>9);
        *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    }
}

/*
 * mm0 = 00 B3 00 B2 00 B1 00 B0
 * mm1 = 00 G3 00 G2 00 G1 00 G0
 * mm2 = 00 R3 00 R2 00 R1 00 R0
 * mm6 = FF FF FF FF FF FF FF FF
 * mm7 = 00 00 00 00 00 00 00 00
 */
#define PACK_RGB32 \
    "packuswb   %%mm7, %%mm0    \n\t" /* 00 00 00 00 B3 B2 B1 B0 */ \
    "packuswb   %%mm7, %%mm1    \n\t" /* 00 00 00 00 G3 G2 G1 G0 */ \
    "packuswb   %%mm7, %%mm2    \n\t" /* 00 00 00 00 R3 R2 R1 R0 */ \
    "punpcklbw  %%mm1, %%mm0    \n\t" /* G3 B3 G2 B2 G1 B1 G0 B0 */ \
    "punpcklbw  %%mm6, %%mm2    \n\t" /* FF R3 FF R2 FF R1 FF R0 */ \
    "movq       %%mm0, %%mm3    \n\t"                               \
    "punpcklwd  %%mm2, %%mm0    \n\t" /* FF R1 G1 B1 FF R0 G0 B0 */ \
    "punpckhwd  %%mm2, %%mm3    \n\t" /* FF R3 G3 B3 FF R2 G2 B2 */ \
947 948
    MOVNTQ"     %%mm0,  (%0)    \n\t"                               \
    MOVNTQ"     %%mm3, 8(%0)    \n\t"                               \
949

950
static inline void RENAME(rgb15to32)(const uint8_t *src, uint8_t *dst, int src_size)
951 952 953 954 955 956 957 958 959 960 961 962
{
    const uint16_t *end;
    const uint16_t *mm_end;
    uint8_t *d = dst;
    const uint16_t *s = (const uint16_t *)src;
    end = s + src_size/2;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    __asm__ volatile("pxor    %%mm7,%%mm7    \n\t":::"memory");
    __asm__ volatile("pcmpeqd %%mm6,%%mm6    \n\t":::"memory");
    mm_end = end - 3;
    while (s < mm_end) {
        __asm__ volatile(
963 964 965 966
            PREFETCH"  32(%1)           \n\t"
            "movq        (%1), %%mm0    \n\t"
            "movq        (%1), %%mm1    \n\t"
            "movq        (%1), %%mm2    \n\t"
967 968 969
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
970 971 972
            "psllq         $5, %%mm0    \n\t"
            "pmulhw        %5, %%mm0    \n\t"
            "pmulhw        %5, %%mm1    \n\t"
973
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
974
            PACK_RGB32
975
            ::"r"(d),"r"(s),"m"(mask15b),"m"(mask15g),"m"(mask15r) ,"m"(mul15_mid)
976
              NAMED_CONSTRAINTS_ADD(mul15_hi)
977 978 979 980 981 982 983 984 985
            :"memory");
        d += 16;
        s += 4;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register uint16_t bgr;
        bgr = *s++;
986 987 988
        *d++ = ((bgr&0x1F)<<3) | ((bgr&0x1F)>>2);
        *d++ = ((bgr&0x3E0)>>2) | ((bgr&0x3E0)>>7);
        *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
989 990 991 992
        *d++ = 255;
    }
}

993
static inline void RENAME(rgb16to32)(const uint8_t *src, uint8_t *dst, int src_size)
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
{
    const uint16_t *end;
    const uint16_t *mm_end;
    uint8_t *d = dst;
    const uint16_t *s = (const uint16_t*)src;
    end = s + src_size/2;
    __asm__ volatile(PREFETCH"    %0"::"m"(*s):"memory");
    __asm__ volatile("pxor    %%mm7,%%mm7    \n\t":::"memory");
    __asm__ volatile("pcmpeqd %%mm6,%%mm6    \n\t":::"memory");
    mm_end = end - 3;
    while (s < mm_end) {
        __asm__ volatile(
1006 1007 1008 1009
            PREFETCH"  32(%1)           \n\t"
            "movq        (%1), %%mm0    \n\t"
            "movq        (%1), %%mm1    \n\t"
            "movq        (%1), %%mm2    \n\t"
1010 1011 1012
            "pand          %2, %%mm0    \n\t"
            "pand          %3, %%mm1    \n\t"
            "pand          %4, %%mm2    \n\t"
1013 1014 1015
            "psllq         $5, %%mm0    \n\t"
            "psrlq         $1, %%mm2    \n\t"
            "pmulhw        %5, %%mm0    \n\t"
1016 1017
            "pmulhw        "MANGLE(mul16_mid)", %%mm1    \n\t"
            "pmulhw        "MANGLE(mul15_hi)", %%mm2    \n\t"
1018
            PACK_RGB32
1019
            ::"r"(d),"r"(s),"m"(mask16b),"m"(mask16g),"m"(mask16r),"m"(mul15_mid)
1020
              NAMED_CONSTRAINTS_ADD(mul16_mid,mul15_hi)
1021 1022 1023 1024 1025 1026 1027 1028 1029
            :"memory");
        d += 16;
        s += 4;
    }
    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");
    while (s < end) {
        register uint16_t bgr;
        bgr = *s++;
1030 1031 1032
        *d++ = ((bgr&0x1F)<<3) | ((bgr&0x1F)>>2);
        *d++ = ((bgr&0x7E0)>>3) | ((bgr&0x7E0)>>9);
        *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
1033 1034 1035 1036
        *d++ = 255;
    }
}

1037
static inline void RENAME(rgb24tobgr24)(const uint8_t *src, uint8_t *dst, int src_size)
1038 1039 1040 1041
{
    unsigned i;
    x86_reg mmx_size= 23 - src_size;
    __asm__ volatile (
1042
        "test             %%"FF_REG_a", %%"FF_REG_a"    \n\t"
1043 1044 1045 1046 1047 1048
        "jns                     2f                     \n\t"
        "movq     "MANGLE(mask24r)", %%mm5              \n\t"
        "movq     "MANGLE(mask24g)", %%mm6              \n\t"
        "movq     "MANGLE(mask24b)", %%mm7              \n\t"
        ".p2align                 4                     \n\t"
        "1:                                             \n\t"
1049 1050 1051 1052
        PREFETCH" 32(%1, %%"FF_REG_a")                  \n\t"
        "movq    (%1, %%"FF_REG_a"), %%mm0              \n\t" // BGR BGR BG
        "movq    (%1, %%"FF_REG_a"), %%mm1              \n\t" // BGR BGR BG
        "movq   2(%1, %%"FF_REG_a"), %%mm2              \n\t" // R BGR BGR B
1053 1054 1055 1056 1057 1058
        "psllq                  $16, %%mm0              \n\t" // 00 BGR BGR
        "pand                 %%mm5, %%mm0              \n\t"
        "pand                 %%mm6, %%mm1              \n\t"
        "pand                 %%mm7, %%mm2              \n\t"
        "por                  %%mm0, %%mm1              \n\t"
        "por                  %%mm2, %%mm1              \n\t"
1059 1060 1061 1062
        "movq   6(%1, %%"FF_REG_a"), %%mm0              \n\t" // BGR BGR BG
        MOVNTQ"               %%mm1,(%2, %%"FF_REG_a")  \n\t" // RGB RGB RG
        "movq   8(%1, %%"FF_REG_a"), %%mm1              \n\t" // R BGR BGR B
        "movq  10(%1, %%"FF_REG_a"), %%mm2              \n\t" // GR BGR BGR
1063 1064 1065 1066 1067
        "pand                 %%mm7, %%mm0              \n\t"
        "pand                 %%mm5, %%mm1              \n\t"
        "pand                 %%mm6, %%mm2              \n\t"
        "por                  %%mm0, %%mm1              \n\t"
        "por                  %%mm2, %%mm1              \n\t"
1068 1069 1070 1071
        "movq  14(%1, %%"FF_REG_a"), %%mm0              \n\t" // R BGR BGR B
        MOVNTQ"               %%mm1, 8(%2, %%"FF_REG_a")\n\t" // B RGB RGB R
        "movq  16(%1, %%"FF_REG_a"), %%mm1              \n\t" // GR BGR BGR
        "movq  18(%1, %%"FF_REG_a"), %%mm2              \n\t" // BGR BGR BG
1072 1073 1074 1075 1076
        "pand                 %%mm6, %%mm0              \n\t"
        "pand                 %%mm7, %%mm1              \n\t"
        "pand                 %%mm5, %%mm2              \n\t"
        "por                  %%mm0, %%mm1              \n\t"
        "por                  %%mm2, %%mm1              \n\t"
1077 1078
        MOVNTQ"               %%mm1, 16(%2, %%"FF_REG_a") \n\t"
        "add                    $24, %%"FF_REG_a"       \n\t"
1079 1080 1081 1082
        " js                     1b                     \n\t"
        "2:                                             \n\t"
        : "+a" (mmx_size)
        : "r" (src-mmx_size), "r"(dst-mmx_size)
1083
          NAMED_CONSTRAINTS_ADD(mask24r,mask24g,mask24b)
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    );

    __asm__ volatile(SFENCE:::"memory");
    __asm__ volatile(EMMS:::"memory");

    if (mmx_size==23) return; //finished, was multiple of 8

    src+= src_size;
    dst+= src_size;
    src_size= 23-mmx_size;
    src-= src_size;
    dst-= src_size;
    for (i=0; i<src_size; i+=3) {
        register uint8_t x;
        x          = src[i + 2];
        dst[i + 1] = src[i + 1];
        dst[i + 2] = src[i + 0];
        dst[i + 0] = x;
    }
}

static inline void RENAME(yuvPlanartoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1106 1107
                                           int width, int height,
                                           int lumStride, int chromStride, int dstStride, int vertLumPerChroma)
1108
{
1109
    int y;
1110 1111 1112 1113
    const x86_reg chromWidth= width>>1;
    for (y=0; y<height; y++) {
        //FIXME handle 2 lines at once (fewer prefetches, reuse some chroma, but very likely memory-limited anyway)
        __asm__ volatile(
1114
            "xor                 %%"FF_REG_a", %%"FF_REG_a" \n\t"
1115 1116
            ".p2align                    4              \n\t"
            "1:                                         \n\t"
1117 1118 1119 1120
            PREFETCH" 32(%1, %%"FF_REG_a", 2)           \n\t"
            PREFETCH" 32(%2, %%"FF_REG_a")              \n\t"
            PREFETCH" 32(%3, %%"FF_REG_a")              \n\t"
            "movq       (%2, %%"FF_REG_a"), %%mm0       \n\t" // U(0)
1121
            "movq                    %%mm0, %%mm2       \n\t" // U(0)
1122
            "movq       (%3, %%"FF_REG_a"), %%mm1       \n\t" // V(0)
1123 1124 1125
            "punpcklbw               %%mm1, %%mm0       \n\t" // UVUV UVUV(0)
            "punpckhbw               %%mm1, %%mm2       \n\t" // UVUV UVUV(8)

1126 1127
            "movq     (%1, %%"FF_REG_a",2), %%mm3       \n\t" // Y(0)
            "movq    8(%1, %%"FF_REG_a",2), %%mm5       \n\t" // Y(8)
1128 1129 1130 1131 1132 1133 1134
            "movq                    %%mm3, %%mm4       \n\t" // Y(0)
            "movq                    %%mm5, %%mm6       \n\t" // Y(8)
            "punpcklbw               %%mm0, %%mm3       \n\t" // YUYV YUYV(0)
            "punpckhbw               %%mm0, %%mm4       \n\t" // YUYV YUYV(4)
            "punpcklbw               %%mm2, %%mm5       \n\t" // YUYV YUYV(8)
            "punpckhbw               %%mm2, %%mm6       \n\t" // YUYV YUYV(12)

1135 1136 1137 1138
            MOVNTQ"                  %%mm3,   (%0, %%"FF_REG_a", 4)    \n\t"
            MOVNTQ"                  %%mm4,  8(%0, %%"FF_REG_a", 4)    \n\t"
            MOVNTQ"                  %%mm5, 16(%0, %%"FF_REG_a", 4)    \n\t"
            MOVNTQ"                  %%mm6, 24(%0, %%"FF_REG_a", 4)    \n\t"
1139

1140 1141 1142
            "add                        $8, %%"FF_REG_a" \n\t"
            "cmp                        %4, %%"FF_REG_a" \n\t"
            " jb                        1b               \n\t"
1143
            ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
1144
            : "%"FF_REG_a
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
        );
        if ((y&(vertLumPerChroma-1)) == vertLumPerChroma-1) {
            usrc += chromStride;
            vsrc += chromStride;
        }
        ysrc += lumStride;
        dst  += dstStride;
    }
    __asm__(EMMS"       \n\t"
            SFENCE"     \n\t"
            :::"memory");
}

/**
 * Height should be a multiple of 2 and width should be a multiple of 16.
 * (If this is a problem for anyone then tell me, and I will fix it.)
 */
static inline void RENAME(yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1163 1164
                                      int width, int height,
                                      int lumStride, int chromStride, int dstStride)
1165 1166 1167 1168 1169 1170
{
    //FIXME interpolate chroma
    RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
}

static inline void RENAME(yuvPlanartouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1171 1172
                                           int width, int height,
                                           int lumStride, int chromStride, int dstStride, int vertLumPerChroma)
1173
{
1174
    int y;
1175 1176 1177 1178
    const x86_reg chromWidth= width>>1;
    for (y=0; y<height; y++) {
        //FIXME handle 2 lines at once (fewer prefetches, reuse some chroma, but very likely memory-limited anyway)
        __asm__ volatile(
1179
            "xor             %%"FF_REG_a", %%"FF_REG_a" \n\t"
1180 1181
            ".p2align                   4               \n\t"
            "1:                                         \n\t"
1182 1183 1184 1185
            PREFETCH" 32(%1, %%"FF_REG_a", 2)           \n\t"
            PREFETCH" 32(%2, %%"FF_REG_a")              \n\t"
            PREFETCH" 32(%3, %%"FF_REG_a")              \n\t"
            "movq      (%2, %%"FF_REG_a"), %%mm0        \n\t" // U(0)
1186
            "movq                   %%mm0, %%mm2        \n\t" // U(0)
1187
            "movq      (%3, %%"FF_REG_a"), %%mm1        \n\t" // V(0)
1188 1189 1190
            "punpcklbw              %%mm1, %%mm0        \n\t" // UVUV UVUV(0)
            "punpckhbw              %%mm1, %%mm2        \n\t" // UVUV UVUV(8)

1191 1192
            "movq    (%1, %%"FF_REG_a",2), %%mm3        \n\t" // Y(0)
            "movq   8(%1, %%"FF_REG_a",2), %%mm5        \n\t" // Y(8)
1193 1194 1195 1196 1197 1198 1199
            "movq                   %%mm0, %%mm4        \n\t" // Y(0)
            "movq                   %%mm2, %%mm6        \n\t" // Y(8)
            "punpcklbw              %%mm3, %%mm0        \n\t" // YUYV YUYV(0)
            "punpckhbw              %%mm3, %%mm4        \n\t" // YUYV YUYV(4)
            "punpcklbw              %%mm5, %%mm2        \n\t" // YUYV YUYV(8)
            "punpckhbw              %%mm5, %%mm6        \n\t" // YUYV YUYV(12)

1200 1201 1202 1203
            MOVNTQ"                 %%mm0,   (%0, %%"FF_REG_a", 4)     \n\t"
            MOVNTQ"                 %%mm4,  8(%0, %%"FF_REG_a", 4)     \n\t"
            MOVNTQ"                 %%mm2, 16(%0, %%"FF_REG_a", 4)     \n\t"
            MOVNTQ"                 %%mm6, 24(%0, %%"FF_REG_a", 4)     \n\t"
1204

1205 1206
            "add                       $8, %%"FF_REG_a" \n\t"
            "cmp                       %4, %%"FF_REG_a" \n\t"
1207 1208
            " jb                       1b               \n\t"
            ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
1209
            : "%"FF_REG_a
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
        );
        if ((y&(vertLumPerChroma-1)) == vertLumPerChroma-1) {
            usrc += chromStride;
            vsrc += chromStride;
        }
        ysrc += lumStride;
        dst += dstStride;
    }
    __asm__(EMMS"       \n\t"
            SFENCE"     \n\t"
            :::"memory");
}

/**
 * Height should be a multiple of 2 and width should be a multiple of 16
 * (If this is a problem for anyone then tell me, and I will fix it.)
 */
static inline void RENAME(yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1228 1229
                                      int width, int height,
                                      int lumStride, int chromStride, int dstStride)
1230 1231 1232 1233 1234 1235 1236 1237 1238
{
    //FIXME interpolate chroma
    RENAME(yuvPlanartouyvy)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
}

/**
 * Width should be a multiple of 16.
 */
static inline void RENAME(yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1239 1240
                                         int width, int height,
                                         int lumStride, int chromStride, int dstStride)
1241 1242 1243 1244 1245 1246 1247 1248
{
    RENAME(yuvPlanartouyvy)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 1);
}

/**
 * Width should be a multiple of 16.
 */
static inline void RENAME(yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1249 1250
                                         int width, int height,
                                         int lumStride, int chromStride, int dstStride)
1251 1252 1253 1254 1255 1256 1257 1258 1259
{
    RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 1);
}

/**
 * Height should be a multiple of 2 and width should be a multiple of 16.
 * (If this is a problem for anyone then tell me, and I will fix it.)
 */
static inline void RENAME(yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1260 1261
                                      int width, int height,
                                      int lumStride, int chromStride, int srcStride)
1262
{
1263
    int y;
1264 1265 1266
    const x86_reg chromWidth= width>>1;
    for (y=0; y<height; y+=2) {
        __asm__ volatile(
1267
            "xor              %%"FF_REG_a", %%"FF_REG_a"\n\t"
1268 1269 1270 1271
            "pcmpeqw                 %%mm7, %%mm7       \n\t"
            "psrlw                      $8, %%mm7       \n\t" // FF,00,FF,00...
            ".p2align                    4              \n\t"
            "1:                \n\t"
1272 1273 1274
            PREFETCH" 64(%0, %%"FF_REG_a", 4)           \n\t"
            "movq    (%0, %%"FF_REG_a", 4), %%mm0       \n\t" // YUYV YUYV(0)
            "movq   8(%0, %%"FF_REG_a", 4), %%mm1       \n\t" // YUYV YUYV(4)
1275 1276 1277 1278 1279 1280 1281 1282 1283
            "movq                    %%mm0, %%mm2       \n\t" // YUYV YUYV(0)
            "movq                    %%mm1, %%mm3       \n\t" // YUYV YUYV(4)
            "psrlw                      $8, %%mm0       \n\t" // U0V0 U0V0(0)
            "psrlw                      $8, %%mm1       \n\t" // U0V0 U0V0(4)
            "pand                    %%mm7, %%mm2       \n\t" // Y0Y0 Y0Y0(0)
            "pand                    %%mm7, %%mm3       \n\t" // Y0Y0 Y0Y0(4)
            "packuswb                %%mm1, %%mm0       \n\t" // UVUV UVUV(0)
            "packuswb                %%mm3, %%mm2       \n\t" // YYYY YYYY(0)

1284
            MOVNTQ"                  %%mm2, (%1, %%"FF_REG_a", 2) \n\t"
1285

1286 1287
            "movq  16(%0, %%"FF_REG_a", 4), %%mm1       \n\t" // YUYV YUYV(8)
            "movq  24(%0, %%"FF_REG_a", 4), %%mm2       \n\t" // YUYV YUYV(12)
1288 1289 1290 1291 1292 1293 1294 1295 1296
            "movq                    %%mm1, %%mm3       \n\t" // YUYV YUYV(8)
            "movq                    %%mm2, %%mm4       \n\t" // YUYV YUYV(12)
            "psrlw                      $8, %%mm1       \n\t" // U0V0 U0V0(8)
            "psrlw                      $8, %%mm2       \n\t" // U0V0 U0V0(12)
            "pand                    %%mm7, %%mm3       \n\t" // Y0Y0 Y0Y0(8)
            "pand                    %%mm7, %%mm4       \n\t" // Y0Y0 Y0Y0(12)
            "packuswb                %%mm2, %%mm1       \n\t" // UVUV UVUV(8)
            "packuswb                %%mm4, %%mm3       \n\t" // YYYY YYYY(8)

1297
            MOVNTQ"                  %%mm3, 8(%1, %%"FF_REG_a", 2) \n\t"
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

            "movq                    %%mm0, %%mm2       \n\t" // UVUV UVUV(0)
            "movq                    %%mm1, %%mm3       \n\t" // UVUV UVUV(8)
            "psrlw                      $8, %%mm0       \n\t" // V0V0 V0V0(0)
            "psrlw                      $8, %%mm1       \n\t" // V0V0 V0V0(8)
            "pand                    %%mm7, %%mm2       \n\t" // U0U0 U0U0(0)
            "pand                    %%mm7, %%mm3       \n\t" // U0U0 U0U0(8)
            "packuswb                %%mm1, %%mm0       \n\t" // VVVV VVVV(0)
            "packuswb                %%mm3, %%mm2       \n\t" // UUUU UUUU(0)

1308 1309
            MOVNTQ"                  %%mm0, (%3, %%"FF_REG_a")     \n\t"
            MOVNTQ"                  %%mm2, (%2, %%"FF_REG_a")     \n\t"
1310

1311 1312 1313
            "add                        $8, %%"FF_REG_a" \n\t"
            "cmp                        %4, %%"FF_REG_a" \n\t"
            " jb                        1b               \n\t"
1314
            ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1315
            : "memory", "%"FF_REG_a
1316 1317 1318 1319 1320 1321
        );

        ydst += lumStride;
        src  += srcStride;

        __asm__ volatile(
1322
            "xor              %%"FF_REG_a", %%"FF_REG_a"\n\t"
1323 1324
            ".p2align                    4              \n\t"
            "1:                                         \n\t"
1325 1326 1327 1328 1329
            PREFETCH" 64(%0, %%"FF_REG_a", 4)           \n\t"
            "movq    (%0, %%"FF_REG_a", 4), %%mm0       \n\t" // YUYV YUYV(0)
            "movq   8(%0, %%"FF_REG_a", 4), %%mm1       \n\t" // YUYV YUYV(4)
            "movq  16(%0, %%"FF_REG_a", 4), %%mm2       \n\t" // YUYV YUYV(8)
            "movq  24(%0, %%"FF_REG_a", 4), %%mm3       \n\t" // YUYV YUYV(12)
1330 1331 1332 1333 1334 1335 1336
            "pand                    %%mm7, %%mm0       \n\t" // Y0Y0 Y0Y0(0)
            "pand                    %%mm7, %%mm1       \n\t" // Y0Y0 Y0Y0(4)
            "pand                    %%mm7, %%mm2       \n\t" // Y0Y0 Y0Y0(8)
            "pand                    %%mm7, %%mm3       \n\t" // Y0Y0 Y0Y0(12)
            "packuswb                %%mm1, %%mm0       \n\t" // YYYY YYYY(0)
            "packuswb                %%mm3, %%mm2       \n\t" // YYYY YYYY(8)

1337 1338
            MOVNTQ"                  %%mm0,  (%1, %%"FF_REG_a", 2) \n\t"
            MOVNTQ"                  %%mm2, 8(%1, %%"FF_REG_a", 2) \n\t"
1339

1340 1341
            "add                        $8, %%"FF_REG_a"\n\t"
            "cmp                        %4, %%"FF_REG_a"\n\t"
1342 1343 1344
            " jb                        1b              \n\t"

            ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1345
            : "memory", "%"FF_REG_a
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
        );
        udst += chromStride;
        vdst += chromStride;
        ydst += lumStride;
        src  += srcStride;
    }
    __asm__ volatile(EMMS"       \n\t"
                     SFENCE"     \n\t"
                     :::"memory");
}
1356
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
1357

1358
#if COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW
1359
static inline void RENAME(planar2x)(const uint8_t *src, uint8_t *dst, int srcWidth, int srcHeight, int srcStride, int dstStride)
1360
{
1361
    int x,y;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

    dst[0]= src[0];

    // first line
    for (x=0; x<srcWidth-1; x++) {
        dst[2*x+1]= (3*src[x] +   src[x+1])>>2;
        dst[2*x+2]= (  src[x] + 3*src[x+1])>>2;
    }
    dst[2*srcWidth-1]= src[srcWidth-1];

    dst+= dstStride;

    for (y=1; y<srcHeight; y++) {
1375 1376 1377
        x86_reg mmxSize= srcWidth&~15;

        if (mmxSize) {
1378
        __asm__ volatile(
1379
            "mov                       %4, %%"FF_REG_a" \n\t"
1380
            "movq        "MANGLE(mmx_ff)", %%mm0    \n\t"
1381
            "movq      (%0, %%"FF_REG_a"), %%mm4    \n\t"
1382 1383 1384 1385
            "movq                   %%mm4, %%mm2    \n\t"
            "psllq                     $8, %%mm4    \n\t"
            "pand                   %%mm0, %%mm2    \n\t"
            "por                    %%mm2, %%mm4    \n\t"
1386
            "movq      (%1, %%"FF_REG_a"), %%mm5    \n\t"
1387 1388 1389 1390 1391
            "movq                   %%mm5, %%mm3    \n\t"
            "psllq                     $8, %%mm5    \n\t"
            "pand                   %%mm0, %%mm3    \n\t"
            "por                    %%mm3, %%mm5    \n\t"
            "1:                                     \n\t"
1392 1393 1394 1395
            "movq      (%0, %%"FF_REG_a"), %%mm0    \n\t"
            "movq      (%1, %%"FF_REG_a"), %%mm1    \n\t"
            "movq     1(%0, %%"FF_REG_a"), %%mm2    \n\t"
            "movq     1(%1, %%"FF_REG_a"), %%mm3    \n\t"
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
            PAVGB"                  %%mm0, %%mm5    \n\t"
            PAVGB"                  %%mm0, %%mm3    \n\t"
            PAVGB"                  %%mm0, %%mm5    \n\t"
            PAVGB"                  %%mm0, %%mm3    \n\t"
            PAVGB"                  %%mm1, %%mm4    \n\t"
            PAVGB"                  %%mm1, %%mm2    \n\t"
            PAVGB"                  %%mm1, %%mm4    \n\t"
            PAVGB"                  %%mm1, %%mm2    \n\t"
            "movq                   %%mm5, %%mm7    \n\t"
            "movq                   %%mm4, %%mm6    \n\t"
            "punpcklbw              %%mm3, %%mm5    \n\t"
            "punpckhbw              %%mm3, %%mm7    \n\t"
            "punpcklbw              %%mm2, %%mm4    \n\t"
            "punpckhbw              %%mm2, %%mm6    \n\t"
1410 1411 1412 1413 1414 1415 1416 1417
            MOVNTQ"                 %%mm5,  (%2, %%"FF_REG_a", 2)  \n\t"
            MOVNTQ"                 %%mm7, 8(%2, %%"FF_REG_a", 2)  \n\t"
            MOVNTQ"                 %%mm4,  (%3, %%"FF_REG_a", 2)  \n\t"
            MOVNTQ"                 %%mm6, 8(%3, %%"FF_REG_a", 2)  \n\t"
            "add                       $8, %%"FF_REG_a"            \n\t"
            "movq    -1(%0, %%"FF_REG_a"), %%mm4    \n\t"
            "movq    -1(%1, %%"FF_REG_a"), %%mm5    \n\t"
            " js                       1b           \n\t"
1418 1419 1420
            :: "r" (src + mmxSize  ), "r" (src + srcStride + mmxSize  ),
               "r" (dst + mmxSize*2), "r" (dst + dstStride + mmxSize*2),
               "g" (-mmxSize)
1421
               NAMED_CONSTRAINTS_ADD(mmx_ff)
1422
            : "%"FF_REG_a
1423
        );
1424 1425 1426 1427 1428
        } else {
            mmxSize = 1;
            dst[0]         = (src[0] * 3 + src[srcStride]) >> 2;
            dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
        }
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

        for (x=mmxSize-1; x<srcWidth-1; x++) {
            dst[2*x          +1]= (3*src[x+0] +   src[x+srcStride+1])>>2;
            dst[2*x+dstStride+2]= (  src[x+0] + 3*src[x+srcStride+1])>>2;
            dst[2*x+dstStride+1]= (  src[x+1] + 3*src[x+srcStride  ])>>2;
            dst[2*x          +2]= (3*src[x+1] +   src[x+srcStride  ])>>2;
        }
        dst[srcWidth*2 -1            ]= (3*src[srcWidth-1] +   src[srcWidth-1 + srcStride])>>2;
        dst[srcWidth*2 -1 + dstStride]= (  src[srcWidth-1] + 3*src[srcWidth-1 + srcStride])>>2;

        dst+=dstStride*2;
        src+=srcStride;
    }

    // last line
    dst[0]= src[0];

    for (x=0; x<srcWidth-1; x++) {
        dst[2*x+1]= (3*src[x] +   src[x+1])>>2;
        dst[2*x+2]= (  src[x] + 3*src[x+1])>>2;
    }
    dst[2*srcWidth-1]= src[srcWidth-1];

    __asm__ volatile(EMMS"       \n\t"
                     SFENCE"     \n\t"
                     :::"memory");
}
1456
#endif /* COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW */
1457

1458
#if !COMPILE_TEMPLATE_AMD3DNOW
1459 1460 1461 1462 1463 1464 1465
/**
 * Height should be a multiple of 2 and width should be a multiple of 16.
 * (If this is a problem for anyone then tell me, and I will fix it.)
 * Chrominance data is only taken from every second line, others are ignored.
 * FIXME: Write HQ version.
 */
static inline void RENAME(uyvytoyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1466 1467
                                      int width, int height,
                                      int lumStride, int chromStride, int srcStride)
1468
{
1469
    int y;
1470 1471 1472
    const x86_reg chromWidth= width>>1;
    for (y=0; y<height; y+=2) {
        __asm__ volatile(
1473
            "xor          %%"FF_REG_a", %%"FF_REG_a" \n\t"
1474 1475 1476 1477
            "pcmpeqw             %%mm7, %%mm7   \n\t"
            "psrlw                  $8, %%mm7   \n\t" // FF,00,FF,00...
            ".p2align                4          \n\t"
            "1:                                 \n\t"
1478 1479 1480
            PREFETCH" 64(%0, %%"FF_REG_a", 4)          \n\t"
            "movq       (%0, %%"FF_REG_a", 4), %%mm0   \n\t" // UYVY UYVY(0)
            "movq      8(%0, %%"FF_REG_a", 4), %%mm1   \n\t" // UYVY UYVY(4)
1481 1482 1483 1484 1485 1486 1487 1488 1489
            "movq                %%mm0, %%mm2   \n\t" // UYVY UYVY(0)
            "movq                %%mm1, %%mm3   \n\t" // UYVY UYVY(4)
            "pand                %%mm7, %%mm0   \n\t" // U0V0 U0V0(0)
            "pand                %%mm7, %%mm1   \n\t" // U0V0 U0V0(4)
            "psrlw                  $8, %%mm2   \n\t" // Y0Y0 Y0Y0(0)
            "psrlw                  $8, %%mm3   \n\t" // Y0Y0 Y0Y0(4)
            "packuswb            %%mm1, %%mm0   \n\t" // UVUV UVUV(0)
            "packuswb            %%mm3, %%mm2   \n\t" // YYYY YYYY(0)

1490
            MOVNTQ"              %%mm2,  (%1, %%"FF_REG_a", 2) \n\t"
1491

1492 1493
            "movq     16(%0, %%"FF_REG_a", 4), %%mm1   \n\t" // UYVY UYVY(8)
            "movq     24(%0, %%"FF_REG_a", 4), %%mm2   \n\t" // UYVY UYVY(12)
1494 1495 1496 1497 1498 1499 1500 1501 1502
            "movq                %%mm1, %%mm3   \n\t" // UYVY UYVY(8)
            "movq                %%mm2, %%mm4   \n\t" // UYVY UYVY(12)
            "pand                %%mm7, %%mm1   \n\t" // U0V0 U0V0(8)
            "pand                %%mm7, %%mm2   \n\t" // U0V0 U0V0(12)
            "psrlw                  $8, %%mm3   \n\t" // Y0Y0 Y0Y0(8)
            "psrlw                  $8, %%mm4   \n\t" // Y0Y0 Y0Y0(12)
            "packuswb            %%mm2, %%mm1   \n\t" // UVUV UVUV(8)
            "packuswb            %%mm4, %%mm3   \n\t" // YYYY YYYY(8)

1503
            MOVNTQ"              %%mm3, 8(%1, %%"FF_REG_a", 2) \n\t"
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

            "movq                %%mm0, %%mm2   \n\t" // UVUV UVUV(0)
            "movq                %%mm1, %%mm3   \n\t" // UVUV UVUV(8)
            "psrlw                  $8, %%mm0   \n\t" // V0V0 V0V0(0)
            "psrlw                  $8, %%mm1   \n\t" // V0V0 V0V0(8)
            "pand                %%mm7, %%mm2   \n\t" // U0U0 U0U0(0)
            "pand                %%mm7, %%mm3   \n\t" // U0U0 U0U0(8)
            "packuswb            %%mm1, %%mm0   \n\t" // VVVV VVVV(0)
            "packuswb            %%mm3, %%mm2   \n\t" // UUUU UUUU(0)

1514 1515
            MOVNTQ"              %%mm0, (%3, %%"FF_REG_a") \n\t"
            MOVNTQ"              %%mm2, (%2, %%"FF_REG_a") \n\t"
1516

1517 1518 1519
            "add                    $8, %%"FF_REG_a" \n\t"
            "cmp                    %4, %%"FF_REG_a" \n\t"
            " jb                    1b               \n\t"
1520
            ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1521
            : "memory", "%"FF_REG_a
1522 1523 1524 1525 1526 1527
        );

        ydst += lumStride;
        src  += srcStride;

        __asm__ volatile(
1528 1529 1530 1531 1532 1533 1534 1535
            "xor          %%"FF_REG_a", %%"FF_REG_a"  \n\t"
            ".p2align                4                \n\t"
            "1:                                       \n\t"
            PREFETCH" 64(%0, %%"FF_REG_a", 4)         \n\t"
            "movq       (%0, %%"FF_REG_a", 4), %%mm0  \n\t" // YUYV YUYV(0)
            "movq      8(%0, %%"FF_REG_a", 4), %%mm1  \n\t" // YUYV YUYV(4)
            "movq     16(%0, %%"FF_REG_a", 4), %%mm2  \n\t" // YUYV YUYV(8)
            "movq     24(%0, %%"FF_REG_a", 4), %%mm3  \n\t" // YUYV YUYV(12)
1536 1537 1538 1539 1540 1541 1542
            "psrlw                  $8, %%mm0   \n\t" // Y0Y0 Y0Y0(0)
            "psrlw                  $8, %%mm1   \n\t" // Y0Y0 Y0Y0(4)
            "psrlw                  $8, %%mm2   \n\t" // Y0Y0 Y0Y0(8)
            "psrlw                  $8, %%mm3   \n\t" // Y0Y0 Y0Y0(12)
            "packuswb            %%mm1, %%mm0   \n\t" // YYYY YYYY(0)
            "packuswb            %%mm3, %%mm2   \n\t" // YYYY YYYY(8)

1543 1544
            MOVNTQ"              %%mm0,  (%1, %%"FF_REG_a", 2) \n\t"
            MOVNTQ"              %%mm2, 8(%1, %%"FF_REG_a", 2) \n\t"
1545

1546 1547 1548
            "add                    $8, %%"FF_REG_a" \n\t"
            "cmp                    %4, %%"FF_REG_a" \n\t"
            " jb                    1b               \n\t"
1549 1550

            ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1551
            : "memory", "%"FF_REG_a
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
        );
        udst += chromStride;
        vdst += chromStride;
        ydst += lumStride;
        src  += srcStride;
    }
    __asm__ volatile(EMMS"       \n\t"
                     SFENCE"     \n\t"
                     :::"memory");
}
1562
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
1563 1564 1565 1566 1567 1568 1569 1570

/**
 * Height should be a multiple of 2 and width should be a multiple of 2.
 * (If this is a problem for anyone then tell me, and I will fix it.)
 * Chrominance data is only taken from every second line,
 * others are ignored in the C version.
 * FIXME: Write HQ version.
 */
1571
#if HAVE_7REGS
1572
static inline void RENAME(rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1573
                                       int width, int height,
1574 1575
                                       int lumStride, int chromStride, int srcStride,
                                       int32_t *rgb2yuv)
1576
{
1577 1578 1579
#define BGR2Y_IDX "16*4+16*32"
#define BGR2U_IDX "16*4+16*33"
#define BGR2V_IDX "16*4+16*34"
1580
    int y;
1581
    const x86_reg chromWidth= width>>1;
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591

    if (height > 2) {
        ff_rgb24toyv12_c(src, ydst, udst, vdst, width, 2, lumStride, chromStride, srcStride, rgb2yuv);
        src  += 2*srcStride;
        ydst += 2*lumStride;
        udst += chromStride;
        vdst += chromStride;
        height -= 2;
    }

1592
    for (y=0; y<height-2; y+=2) {
1593
        int i;
1594 1595
        for (i=0; i<2; i++) {
            __asm__ volatile(
1596
                "mov                        %2, %%"FF_REG_a"\n\t"
1597
                "movq          "BGR2Y_IDX"(%3), %%mm6       \n\t"
1598 1599
                "movq       "MANGLE(ff_w1111)", %%mm5       \n\t"
                "pxor                    %%mm7, %%mm7       \n\t"
1600
                "lea (%%"FF_REG_a", %%"FF_REG_a", 2), %%"FF_REG_d" \n\t"
1601 1602
                ".p2align                    4              \n\t"
                "1:                                         \n\t"
1603 1604 1605
                PREFETCH" 64(%0, %%"FF_REG_d")              \n\t"
                "movd       (%0, %%"FF_REG_d"), %%mm0       \n\t"
                "movd      3(%0, %%"FF_REG_d"), %%mm1       \n\t"
1606 1607
                "punpcklbw               %%mm7, %%mm0       \n\t"
                "punpcklbw               %%mm7, %%mm1       \n\t"
1608 1609
                "movd      6(%0, %%"FF_REG_d"), %%mm2       \n\t"
                "movd      9(%0, %%"FF_REG_d"), %%mm3       \n\t"
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
                "punpcklbw               %%mm7, %%mm2       \n\t"
                "punpcklbw               %%mm7, %%mm3       \n\t"
                "pmaddwd                 %%mm6, %%mm0       \n\t"
                "pmaddwd                 %%mm6, %%mm1       \n\t"
                "pmaddwd                 %%mm6, %%mm2       \n\t"
                "pmaddwd                 %%mm6, %%mm3       \n\t"
                "psrad                      $8, %%mm0       \n\t"
                "psrad                      $8, %%mm1       \n\t"
                "psrad                      $8, %%mm2       \n\t"
                "psrad                      $8, %%mm3       \n\t"
                "packssdw                %%mm1, %%mm0       \n\t"
                "packssdw                %%mm3, %%mm2       \n\t"
                "pmaddwd                 %%mm5, %%mm0       \n\t"
                "pmaddwd                 %%mm5, %%mm2       \n\t"
                "packssdw                %%mm2, %%mm0       \n\t"
                "psraw                      $7, %%mm0       \n\t"

1627 1628
                "movd     12(%0, %%"FF_REG_d"), %%mm4       \n\t"
                "movd     15(%0, %%"FF_REG_d"), %%mm1       \n\t"
1629 1630
                "punpcklbw               %%mm7, %%mm4       \n\t"
                "punpcklbw               %%mm7, %%mm1       \n\t"
1631 1632
                "movd     18(%0, %%"FF_REG_d"), %%mm2       \n\t"
                "movd     21(%0, %%"FF_REG_d"), %%mm3       \n\t"
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
                "punpcklbw               %%mm7, %%mm2       \n\t"
                "punpcklbw               %%mm7, %%mm3       \n\t"
                "pmaddwd                 %%mm6, %%mm4       \n\t"
                "pmaddwd                 %%mm6, %%mm1       \n\t"
                "pmaddwd                 %%mm6, %%mm2       \n\t"
                "pmaddwd                 %%mm6, %%mm3       \n\t"
                "psrad                      $8, %%mm4       \n\t"
                "psrad                      $8, %%mm1       \n\t"
                "psrad                      $8, %%mm2       \n\t"
                "psrad                      $8, %%mm3       \n\t"
                "packssdw                %%mm1, %%mm4       \n\t"
                "packssdw                %%mm3, %%mm2       \n\t"
                "pmaddwd                 %%mm5, %%mm4       \n\t"
                "pmaddwd                 %%mm5, %%mm2       \n\t"
1647
                "add                       $24, %%"FF_REG_d"\n\t"
1648 1649 1650 1651 1652 1653
                "packssdw                %%mm2, %%mm4       \n\t"
                "psraw                      $7, %%mm4       \n\t"

                "packuswb                %%mm4, %%mm0       \n\t"
                "paddusb "MANGLE(ff_bgr2YOffset)", %%mm0    \n\t"

1654 1655 1656
                MOVNTQ"                  %%mm0, (%1, %%"FF_REG_a") \n\t"
                "add                        $8,      %%"FF_REG_a"  \n\t"
                " js                        1b                     \n\t"
1657
                : : "r" (src+width*3), "r" (ydst+width), "g" ((x86_reg)-width), "r"(rgb2yuv)
1658
                  NAMED_CONSTRAINTS_ADD(ff_w1111,ff_bgr2YOffset)
1659
                : "%"FF_REG_a, "%"FF_REG_d
1660 1661 1662 1663 1664 1665
            );
            ydst += lumStride;
            src  += srcStride;
        }
        src -= srcStride*2;
        __asm__ volatile(
1666
            "mov                        %4, %%"FF_REG_a"\n\t"
1667
            "movq       "MANGLE(ff_w1111)", %%mm5       \n\t"
1668
            "movq          "BGR2U_IDX"(%5), %%mm6       \n\t"
1669
            "pxor                    %%mm7, %%mm7       \n\t"
1670 1671
            "lea (%%"FF_REG_a", %%"FF_REG_a", 2), %%"FF_REG_d" \n\t"
            "add              %%"FF_REG_d", %%"FF_REG_d"\n\t"
1672 1673
            ".p2align                    4              \n\t"
            "1:                                         \n\t"
1674 1675
            PREFETCH" 64(%0, %%"FF_REG_d")              \n\t"
            PREFETCH" 64(%1, %%"FF_REG_d")              \n\t"
1676
#if COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW
1677 1678 1679 1680
            "movq       (%0, %%"FF_REG_d"), %%mm0       \n\t"
            "movq       (%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movq      6(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movq      6(%1, %%"FF_REG_d"), %%mm3       \n\t"
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
            PAVGB"                   %%mm1, %%mm0       \n\t"
            PAVGB"                   %%mm3, %%mm2       \n\t"
            "movq                    %%mm0, %%mm1       \n\t"
            "movq                    %%mm2, %%mm3       \n\t"
            "psrlq                     $24, %%mm0       \n\t"
            "psrlq                     $24, %%mm2       \n\t"
            PAVGB"                   %%mm1, %%mm0       \n\t"
            PAVGB"                   %%mm3, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm0       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
#else
1692 1693 1694 1695
            "movd       (%0, %%"FF_REG_d"), %%mm0       \n\t"
            "movd       (%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movd      3(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movd      3(%1, %%"FF_REG_d"), %%mm3       \n\t"
1696 1697 1698 1699 1700 1701 1702
            "punpcklbw               %%mm7, %%mm0       \n\t"
            "punpcklbw               %%mm7, %%mm1       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm3       \n\t"
            "paddw                   %%mm1, %%mm0       \n\t"
            "paddw                   %%mm3, %%mm2       \n\t"
            "paddw                   %%mm2, %%mm0       \n\t"
1703 1704 1705 1706
            "movd      6(%0, %%"FF_REG_d"), %%mm4       \n\t"
            "movd      6(%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movd      9(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movd      9(%1, %%"FF_REG_d"), %%mm3       \n\t"
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
            "punpcklbw               %%mm7, %%mm4       \n\t"
            "punpcklbw               %%mm7, %%mm1       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm3       \n\t"
            "paddw                   %%mm1, %%mm4       \n\t"
            "paddw                   %%mm3, %%mm2       \n\t"
            "paddw                   %%mm4, %%mm2       \n\t"
            "psrlw                      $2, %%mm0       \n\t"
            "psrlw                      $2, %%mm2       \n\t"
#endif
1717 1718
            "movq          "BGR2V_IDX"(%5), %%mm1       \n\t"
            "movq          "BGR2V_IDX"(%5), %%mm3       \n\t"
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734

            "pmaddwd                 %%mm0, %%mm1       \n\t"
            "pmaddwd                 %%mm2, %%mm3       \n\t"
            "pmaddwd                 %%mm6, %%mm0       \n\t"
            "pmaddwd                 %%mm6, %%mm2       \n\t"
            "psrad                      $8, %%mm0       \n\t"
            "psrad                      $8, %%mm1       \n\t"
            "psrad                      $8, %%mm2       \n\t"
            "psrad                      $8, %%mm3       \n\t"
            "packssdw                %%mm2, %%mm0       \n\t"
            "packssdw                %%mm3, %%mm1       \n\t"
            "pmaddwd                 %%mm5, %%mm0       \n\t"
            "pmaddwd                 %%mm5, %%mm1       \n\t"
            "packssdw                %%mm1, %%mm0       \n\t" // V1 V0 U1 U0
            "psraw                      $7, %%mm0       \n\t"

1735
#if COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW
1736 1737 1738 1739
            "movq     12(%0, %%"FF_REG_d"), %%mm4       \n\t"
            "movq     12(%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movq     18(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movq     18(%1, %%"FF_REG_d"), %%mm3       \n\t"
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
            PAVGB"                   %%mm1, %%mm4       \n\t"
            PAVGB"                   %%mm3, %%mm2       \n\t"
            "movq                    %%mm4, %%mm1       \n\t"
            "movq                    %%mm2, %%mm3       \n\t"
            "psrlq                     $24, %%mm4       \n\t"
            "psrlq                     $24, %%mm2       \n\t"
            PAVGB"                   %%mm1, %%mm4       \n\t"
            PAVGB"                   %%mm3, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm4       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
#else
1751 1752 1753 1754
            "movd     12(%0, %%"FF_REG_d"), %%mm4       \n\t"
            "movd     12(%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movd     15(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movd     15(%1, %%"FF_REG_d"), %%mm3       \n\t"
1755 1756 1757 1758 1759 1760 1761
            "punpcklbw               %%mm7, %%mm4       \n\t"
            "punpcklbw               %%mm7, %%mm1       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm3       \n\t"
            "paddw                   %%mm1, %%mm4       \n\t"
            "paddw                   %%mm3, %%mm2       \n\t"
            "paddw                   %%mm2, %%mm4       \n\t"
1762 1763 1764 1765
            "movd     18(%0, %%"FF_REG_d"), %%mm5       \n\t"
            "movd     18(%1, %%"FF_REG_d"), %%mm1       \n\t"
            "movd     21(%0, %%"FF_REG_d"), %%mm2       \n\t"
            "movd     21(%1, %%"FF_REG_d"), %%mm3       \n\t"
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
            "punpcklbw               %%mm7, %%mm5       \n\t"
            "punpcklbw               %%mm7, %%mm1       \n\t"
            "punpcklbw               %%mm7, %%mm2       \n\t"
            "punpcklbw               %%mm7, %%mm3       \n\t"
            "paddw                   %%mm1, %%mm5       \n\t"
            "paddw                   %%mm3, %%mm2       \n\t"
            "paddw                   %%mm5, %%mm2       \n\t"
            "movq       "MANGLE(ff_w1111)", %%mm5       \n\t"
            "psrlw                      $2, %%mm4       \n\t"
            "psrlw                      $2, %%mm2       \n\t"
#endif
1777 1778
            "movq          "BGR2V_IDX"(%5), %%mm1       \n\t"
            "movq          "BGR2V_IDX"(%5), %%mm3       \n\t"
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791

            "pmaddwd                 %%mm4, %%mm1       \n\t"
            "pmaddwd                 %%mm2, %%mm3       \n\t"
            "pmaddwd                 %%mm6, %%mm4       \n\t"
            "pmaddwd                 %%mm6, %%mm2       \n\t"
            "psrad                      $8, %%mm4       \n\t"
            "psrad                      $8, %%mm1       \n\t"
            "psrad                      $8, %%mm2       \n\t"
            "psrad                      $8, %%mm3       \n\t"
            "packssdw                %%mm2, %%mm4       \n\t"
            "packssdw                %%mm3, %%mm1       \n\t"
            "pmaddwd                 %%mm5, %%mm4       \n\t"
            "pmaddwd                 %%mm5, %%mm1       \n\t"
1792
            "add                       $24, %%"FF_REG_d"\n\t"
1793 1794 1795 1796 1797 1798 1799 1800
            "packssdw                %%mm1, %%mm4       \n\t" // V3 V2 U3 U2
            "psraw                      $7, %%mm4       \n\t"

            "movq                    %%mm0, %%mm1           \n\t"
            "punpckldq               %%mm4, %%mm0           \n\t"
            "punpckhdq               %%mm4, %%mm1           \n\t"
            "packsswb                %%mm1, %%mm0           \n\t"
            "paddb "MANGLE(ff_bgr2UVOffset)", %%mm0         \n\t"
1801 1802 1803 1804 1805
            "movd                    %%mm0, (%2, %%"FF_REG_a") \n\t"
            "punpckhdq               %%mm0, %%mm0              \n\t"
            "movd                    %%mm0, (%3, %%"FF_REG_a") \n\t"
            "add                        $4, %%"FF_REG_a"       \n\t"
            " js                        1b              \n\t"
1806
            : : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth), "r"(rgb2yuv)
1807
              NAMED_CONSTRAINTS_ADD(ff_w1111,ff_bgr2UVOffset)
1808
            : "%"FF_REG_a, "%"FF_REG_d
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
        );

        udst += chromStride;
        vdst += chromStride;
        src  += srcStride*2;
    }

    __asm__ volatile(EMMS"       \n\t"
                     SFENCE"     \n\t"
                     :::"memory");

1820
     ff_rgb24toyv12_c(src, ydst, udst, vdst, width, height-y, lumStride, chromStride, srcStride, rgb2yuv);
1821
}
1822
#endif /* HAVE_7REGS */
1823
#endif /* !COMPILE_TEMPLATE_SSE2 */
1824

1825
#if !COMPILE_TEMPLATE_AMD3DNOW && !COMPILE_TEMPLATE_AVX
1826
static void RENAME(interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dest,
1827 1828
                                    int width, int height, int src1Stride,
                                    int src2Stride, int dstStride)
1829
{
1830
    int h;
1831 1832

    for (h=0; h < height; h++) {
1833
        int w;
1834

1835
        if (width >= 16) {
1836
#if COMPILE_TEMPLATE_SSE2
1837
            if (!((((intptr_t)src1) | ((intptr_t)src2) | ((intptr_t)dest))&15)) {
1838
        __asm__(
1839
            "xor              %%"FF_REG_a", %%"FF_REG_a"  \n\t"
1840
            "1:                                     \n\t"
1841 1842 1843 1844 1845
            PREFETCH" 64(%1, %%"FF_REG_a")          \n\t"
            PREFETCH" 64(%2, %%"FF_REG_a")          \n\t"
            "movdqa  (%1, %%"FF_REG_a"), %%xmm0     \n\t"
            "movdqa  (%1, %%"FF_REG_a"), %%xmm1     \n\t"
            "movdqa  (%2, %%"FF_REG_a"), %%xmm2     \n\t"
1846 1847
            "punpcklbw           %%xmm2, %%xmm0     \n\t"
            "punpckhbw           %%xmm2, %%xmm1     \n\t"
1848 1849 1850 1851
            "movntdq             %%xmm0,   (%0, %%"FF_REG_a", 2) \n\t"
            "movntdq             %%xmm1, 16(%0, %%"FF_REG_a", 2) \n\t"
            "add                    $16, %%"FF_REG_a"            \n\t"
            "cmp                     %3, %%"FF_REG_a"            \n\t"
1852 1853
            " jb                     1b             \n\t"
            ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
1854
            : "memory", XMM_CLOBBERS("xmm0", "xmm1", "xmm2",) "%"FF_REG_a
1855
        );
1856 1857
            } else
#endif
1858
        __asm__(
1859
            "xor %%"FF_REG_a", %%"FF_REG_a"         \n\t"
1860
            "1:                                     \n\t"
1861 1862 1863 1864
            PREFETCH" 64(%1, %%"FF_REG_a")          \n\t"
            PREFETCH" 64(%2, %%"FF_REG_a")          \n\t"
            "movq    (%1, %%"FF_REG_a"), %%mm0      \n\t"
            "movq   8(%1, %%"FF_REG_a"), %%mm2      \n\t"
1865 1866
            "movq                 %%mm0, %%mm1      \n\t"
            "movq                 %%mm2, %%mm3      \n\t"
1867 1868
            "movq    (%2, %%"FF_REG_a"), %%mm4      \n\t"
            "movq   8(%2, %%"FF_REG_a"), %%mm5      \n\t"
1869 1870 1871 1872
            "punpcklbw            %%mm4, %%mm0      \n\t"
            "punpckhbw            %%mm4, %%mm1      \n\t"
            "punpcklbw            %%mm5, %%mm2      \n\t"
            "punpckhbw            %%mm5, %%mm3      \n\t"
1873 1874 1875 1876 1877 1878 1879
            MOVNTQ"               %%mm0,   (%0, %%"FF_REG_a", 2) \n\t"
            MOVNTQ"               %%mm1,  8(%0, %%"FF_REG_a", 2) \n\t"
            MOVNTQ"               %%mm2, 16(%0, %%"FF_REG_a", 2) \n\t"
            MOVNTQ"               %%mm3, 24(%0, %%"FF_REG_a", 2) \n\t"
            "add                    $16, %%"FF_REG_a"            \n\t"
            "cmp                     %3, %%"FF_REG_a"            \n\t"
            " jb                     1b                          \n\t"
1880
            ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
1881
            : "memory", "%"FF_REG_a
1882
        );
1883 1884

        }
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
        for (w= (width&(~15)); w < width; w++) {
            dest[2*w+0] = src1[w];
            dest[2*w+1] = src2[w];
        }
        dest += dstStride;
        src1 += src1Stride;
        src2 += src2Stride;
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
            );
}
1899
#endif /* !COMPILE_TEMPLATE_AMD3DNOW && !COMPILE_TEMPLATE_AVX */
1900

1901
#if !COMPILE_TEMPLATE_AVX || HAVE_AVX_EXTERNAL
1902
#if !COMPILE_TEMPLATE_AMD3DNOW && (ARCH_X86_32 || COMPILE_TEMPLATE_SSE2) && COMPILE_TEMPLATE_MMXEXT == COMPILE_TEMPLATE_SSE2 && HAVE_X86ASM
1903
void RENAME(ff_nv12ToUV)(uint8_t *dstU, uint8_t *dstV,
1904 1905 1906 1907
                         const uint8_t *unused,
                         const uint8_t *src1,
                         const uint8_t *src2,
                         int w,
1908
                         uint32_t *unused2);
1909 1910 1911 1912 1913 1914
static void RENAME(deinterleaveBytes)(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
                                      int width, int height, int srcStride,
                                      int dst1Stride, int dst2Stride)
{
    int h;

1915
    for (h = 0; h < height; h++) {
1916
        RENAME(ff_nv12ToUV)(dst1, dst2, NULL, src, NULL, width, NULL);
1917
        src  += srcStride;
1918 1919 1920 1921
        dst1 += dst1Stride;
        dst2 += dst2Stride;
    }
    __asm__(
1922
#if !COMPILE_TEMPLATE_SSE2
1923
            EMMS"       \n\t"
1924
#endif
1925 1926 1927 1928
            SFENCE"     \n\t"
            ::: "memory"
            );
}
1929
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
1930
#endif /* !COMPILE_TEMPLATE_AVX || HAVE_AVX_EXTERNAL */
1931

1932 1933
#if !COMPILE_TEMPLATE_SSE2
#if !COMPILE_TEMPLATE_AMD3DNOW
1934 1935
static inline void RENAME(vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
                                       uint8_t *dst1, uint8_t *dst2,
1936 1937 1938
                                       int width, int height,
                                       int srcStride1, int srcStride2,
                                       int dstStride1, int dstStride2)
1939
{
1940 1941
    x86_reg x, y;
    int w,h;
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    w=width/2; h=height/2;
    __asm__ volatile(
        PREFETCH" %0    \n\t"
        PREFETCH" %1    \n\t"
        ::"m"(*(src1+srcStride1)),"m"(*(src2+srcStride2)):"memory");
    for (y=0;y<h;y++) {
        const uint8_t* s1=src1+srcStride1*(y>>1);
        uint8_t* d=dst1+dstStride1*y;
        x=0;
        for (;x<w-31;x+=32) {
            __asm__ volatile(
1953 1954 1955 1956 1957
                PREFETCH"   32(%1,%2)        \n\t"
                "movq         (%1,%2), %%mm0 \n\t"
                "movq        8(%1,%2), %%mm2 \n\t"
                "movq       16(%1,%2), %%mm4 \n\t"
                "movq       24(%1,%2), %%mm6 \n\t"
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
                "movq      %%mm0, %%mm1 \n\t"
                "movq      %%mm2, %%mm3 \n\t"
                "movq      %%mm4, %%mm5 \n\t"
                "movq      %%mm6, %%mm7 \n\t"
                "punpcklbw %%mm0, %%mm0 \n\t"
                "punpckhbw %%mm1, %%mm1 \n\t"
                "punpcklbw %%mm2, %%mm2 \n\t"
                "punpckhbw %%mm3, %%mm3 \n\t"
                "punpcklbw %%mm4, %%mm4 \n\t"
                "punpckhbw %%mm5, %%mm5 \n\t"
                "punpcklbw %%mm6, %%mm6 \n\t"
                "punpckhbw %%mm7, %%mm7 \n\t"
1970 1971 1972 1973 1974 1975 1976 1977 1978
                MOVNTQ"    %%mm0,   (%0,%2,2)  \n\t"
                MOVNTQ"    %%mm1,  8(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm2, 16(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm3, 24(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm4, 32(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm5, 40(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm6, 48(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm7, 56(%0,%2,2)"
                :: "r"(d), "r"(s1), "r"(x)
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
                :"memory");
        }
        for (;x<w;x++) d[2*x]=d[2*x+1]=s1[x];
    }
    for (y=0;y<h;y++) {
        const uint8_t* s2=src2+srcStride2*(y>>1);
        uint8_t* d=dst2+dstStride2*y;
        x=0;
        for (;x<w-31;x+=32) {
            __asm__ volatile(
1989 1990 1991 1992 1993
                PREFETCH"   32(%1,%2)        \n\t"
                "movq         (%1,%2), %%mm0 \n\t"
                "movq        8(%1,%2), %%mm2 \n\t"
                "movq       16(%1,%2), %%mm4 \n\t"
                "movq       24(%1,%2), %%mm6 \n\t"
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
                "movq      %%mm0, %%mm1 \n\t"
                "movq      %%mm2, %%mm3 \n\t"
                "movq      %%mm4, %%mm5 \n\t"
                "movq      %%mm6, %%mm7 \n\t"
                "punpcklbw %%mm0, %%mm0 \n\t"
                "punpckhbw %%mm1, %%mm1 \n\t"
                "punpcklbw %%mm2, %%mm2 \n\t"
                "punpckhbw %%mm3, %%mm3 \n\t"
                "punpcklbw %%mm4, %%mm4 \n\t"
                "punpckhbw %%mm5, %%mm5 \n\t"
                "punpcklbw %%mm6, %%mm6 \n\t"
                "punpckhbw %%mm7, %%mm7 \n\t"
2006 2007 2008 2009 2010 2011 2012 2013 2014
                MOVNTQ"    %%mm0,   (%0,%2,2)  \n\t"
                MOVNTQ"    %%mm1,  8(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm2, 16(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm3, 24(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm4, 32(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm5, 40(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm6, 48(%0,%2,2)  \n\t"
                MOVNTQ"    %%mm7, 56(%0,%2,2)"
                :: "r"(d), "r"(s2), "r"(x)
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
                :"memory");
        }
        for (;x<w;x++) d[2*x]=d[2*x+1]=s2[x];
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}

static inline void RENAME(yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
                                        uint8_t *dst,
2028 2029 2030
                                        int width, int height,
                                        int srcStride1, int srcStride2,
                                        int srcStride3, int dstStride)
2031 2032
{
    x86_reg x;
2033
    int y,w,h;
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
    w=width/2; h=height;
    for (y=0;y<h;y++) {
        const uint8_t* yp=src1+srcStride1*y;
        const uint8_t* up=src2+srcStride2*(y>>2);
        const uint8_t* vp=src3+srcStride3*(y>>2);
        uint8_t* d=dst+dstStride*y;
        x=0;
        for (;x<w-7;x+=8) {
            __asm__ volatile(
                PREFETCH"   32(%1, %0)          \n\t"
                PREFETCH"   32(%2, %0)          \n\t"
                PREFETCH"   32(%3, %0)          \n\t"
                "movq      (%1, %0, 4), %%mm0   \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
                "movq         (%2, %0), %%mm1   \n\t" /* U0U1U2U3U4U5U6U7 */
                "movq         (%3, %0), %%mm2   \n\t" /* V0V1V2V3V4V5V6V7 */
                "movq            %%mm0, %%mm3   \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
                "movq            %%mm1, %%mm4   \n\t" /* U0U1U2U3U4U5U6U7 */
                "movq            %%mm2, %%mm5   \n\t" /* V0V1V2V3V4V5V6V7 */
                "punpcklbw       %%mm1, %%mm1   \n\t" /* U0U0 U1U1 U2U2 U3U3 */
                "punpcklbw       %%mm2, %%mm2   \n\t" /* V0V0 V1V1 V2V2 V3V3 */
                "punpckhbw       %%mm4, %%mm4   \n\t" /* U4U4 U5U5 U6U6 U7U7 */
                "punpckhbw       %%mm5, %%mm5   \n\t" /* V4V4 V5V5 V6V6 V7V7 */

                "movq            %%mm1, %%mm6   \n\t"
                "punpcklbw       %%mm2, %%mm1   \n\t" /* U0V0 U0V0 U1V1 U1V1*/
                "punpcklbw       %%mm1, %%mm0   \n\t" /* Y0U0 Y1V0 Y2U0 Y3V0*/
                "punpckhbw       %%mm1, %%mm3   \n\t" /* Y4U1 Y5V1 Y6U1 Y7V1*/
                MOVNTQ"          %%mm0,  (%4, %0, 8)    \n\t"
                MOVNTQ"          %%mm3, 8(%4, %0, 8)    \n\t"

                "punpckhbw       %%mm2, %%mm6   \n\t" /* U2V2 U2V2 U3V3 U3V3*/
                "movq     8(%1, %0, 4), %%mm0   \n\t"
                "movq            %%mm0, %%mm3   \n\t"
                "punpcklbw       %%mm6, %%mm0   \n\t" /* Y U2 Y V2 Y U2 Y V2*/
                "punpckhbw       %%mm6, %%mm3   \n\t" /* Y U3 Y V3 Y U3 Y V3*/
                MOVNTQ"          %%mm0, 16(%4, %0, 8)   \n\t"
                MOVNTQ"          %%mm3, 24(%4, %0, 8)   \n\t"

                "movq            %%mm4, %%mm6   \n\t"
                "movq    16(%1, %0, 4), %%mm0   \n\t"
                "movq            %%mm0, %%mm3   \n\t"
                "punpcklbw       %%mm5, %%mm4   \n\t"
                "punpcklbw       %%mm4, %%mm0   \n\t" /* Y U4 Y V4 Y U4 Y V4*/
                "punpckhbw       %%mm4, %%mm3   \n\t" /* Y U5 Y V5 Y U5 Y V5*/
                MOVNTQ"          %%mm0, 32(%4, %0, 8)   \n\t"
                MOVNTQ"          %%mm3, 40(%4, %0, 8)   \n\t"

                "punpckhbw       %%mm5, %%mm6   \n\t"
                "movq    24(%1, %0, 4), %%mm0   \n\t"
                "movq            %%mm0, %%mm3   \n\t"
                "punpcklbw       %%mm6, %%mm0   \n\t" /* Y U6 Y V6 Y U6 Y V6*/
                "punpckhbw       %%mm6, %%mm3   \n\t" /* Y U7 Y V7 Y U7 Y V7*/
                MOVNTQ"          %%mm0, 48(%4, %0, 8)   \n\t"
                MOVNTQ"          %%mm3, 56(%4, %0, 8)   \n\t"

                : "+r" (x)
                : "r"(yp), "r" (up), "r"(vp), "r"(d)
                :"memory");
        }
        for (; x<w; x++) {
2094
            const int x2 = x<<2;
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
            d[8*x+0] = yp[x2];
            d[8*x+1] = up[x];
            d[8*x+2] = yp[x2+1];
            d[8*x+3] = vp[x];
            d[8*x+4] = yp[x2+2];
            d[8*x+5] = up[x];
            d[8*x+6] = yp[x2+3];
            d[8*x+7] = vp[x];
        }
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}
2111
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

static void RENAME(extract_even)(const uint8_t *src, uint8_t *dst, x86_reg count)
{
    dst +=   count;
    src += 2*count;
    count= - count;

    if(count <= -16) {
        count += 15;
        __asm__ volatile(
            "pcmpeqw       %%mm7, %%mm7        \n\t"
            "psrlw            $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq -30(%1, %0, 2), %%mm0        \n\t"
            "movq -22(%1, %0, 2), %%mm1        \n\t"
            "movq -14(%1, %0, 2), %%mm2        \n\t"
            "movq  -6(%1, %0, 2), %%mm3        \n\t"
            "pand          %%mm7, %%mm0        \n\t"
            "pand          %%mm7, %%mm1        \n\t"
            "pand          %%mm7, %%mm2        \n\t"
            "pand          %%mm7, %%mm3        \n\t"
            "packuswb      %%mm1, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm2        \n\t"
            MOVNTQ"        %%mm0,-15(%2, %0)   \n\t"
            MOVNTQ"        %%mm2,- 7(%2, %0)   \n\t"
            "add             $16, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src), "r"(dst)
        );
        count -= 15;
    }
    while(count<0) {
        dst[count]= src[2*count];
        count++;
    }
}

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
static void RENAME(extract_odd)(const uint8_t *src, uint8_t *dst, x86_reg count)
{
    src ++;
    dst +=   count;
    src += 2*count;
    count= - count;

    if(count < -16) {
        count += 16;
        __asm__ volatile(
            "pcmpeqw       %%mm7, %%mm7        \n\t"
            "psrlw            $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq -32(%1, %0, 2), %%mm0        \n\t"
            "movq -24(%1, %0, 2), %%mm1        \n\t"
            "movq -16(%1, %0, 2), %%mm2        \n\t"
            "movq  -8(%1, %0, 2), %%mm3        \n\t"
            "pand          %%mm7, %%mm0        \n\t"
            "pand          %%mm7, %%mm1        \n\t"
            "pand          %%mm7, %%mm2        \n\t"
            "pand          %%mm7, %%mm3        \n\t"
            "packuswb      %%mm1, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm2        \n\t"
            MOVNTQ"        %%mm0,-16(%2, %0)   \n\t"
            MOVNTQ"        %%mm2,- 8(%2, %0)   \n\t"
            "add             $16, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src), "r"(dst)
        );
        count -= 16;
    }
    while(count<0) {
        dst[count]= src[2*count];
        count++;
    }
}

2188
#if !COMPILE_TEMPLATE_AMD3DNOW
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
static void RENAME(extract_even2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
{
    dst0+=   count;
    dst1+=   count;
    src += 4*count;
    count= - count;
    if(count <= -8) {
        count += 7;
        __asm__ volatile(
            "pcmpeqw       %%mm7, %%mm7        \n\t"
            "psrlw            $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq -28(%1, %0, 4), %%mm0        \n\t"
            "movq -20(%1, %0, 4), %%mm1        \n\t"
            "movq -12(%1, %0, 4), %%mm2        \n\t"
            "movq  -4(%1, %0, 4), %%mm3        \n\t"
            "pand          %%mm7, %%mm0        \n\t"
            "pand          %%mm7, %%mm1        \n\t"
            "pand          %%mm7, %%mm2        \n\t"
            "pand          %%mm7, %%mm3        \n\t"
            "packuswb      %%mm1, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm2        \n\t"
            "movq          %%mm0, %%mm1        \n\t"
            "movq          %%mm2, %%mm3        \n\t"
            "psrlw            $8, %%mm0        \n\t"
            "psrlw            $8, %%mm2        \n\t"
            "pand          %%mm7, %%mm1        \n\t"
            "pand          %%mm7, %%mm3        \n\t"
            "packuswb      %%mm2, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm1        \n\t"
            MOVNTQ"        %%mm0,- 7(%3, %0)   \n\t"
            MOVNTQ"        %%mm1,- 7(%2, %0)   \n\t"
            "add              $8, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src), "r"(dst0), "r"(dst1)
        );
        count -= 7;
    }
    while(count<0) {
        dst0[count]= src[4*count+0];
        dst1[count]= src[4*count+2];
        count++;
    }
}
2234
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288

static void RENAME(extract_even2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
{
    dst0 +=   count;
    dst1 +=   count;
    src0 += 4*count;
    src1 += 4*count;
    count= - count;
#ifdef PAVGB
    if(count <= -8) {
        count += 7;
        __asm__ volatile(
            "pcmpeqw        %%mm7, %%mm7        \n\t"
            "psrlw             $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq  -28(%1, %0, 4), %%mm0        \n\t"
            "movq  -20(%1, %0, 4), %%mm1        \n\t"
            "movq  -12(%1, %0, 4), %%mm2        \n\t"
            "movq   -4(%1, %0, 4), %%mm3        \n\t"
            PAVGB" -28(%2, %0, 4), %%mm0        \n\t"
            PAVGB" -20(%2, %0, 4), %%mm1        \n\t"
            PAVGB" -12(%2, %0, 4), %%mm2        \n\t"
            PAVGB" - 4(%2, %0, 4), %%mm3        \n\t"
            "pand           %%mm7, %%mm0        \n\t"
            "pand           %%mm7, %%mm1        \n\t"
            "pand           %%mm7, %%mm2        \n\t"
            "pand           %%mm7, %%mm3        \n\t"
            "packuswb       %%mm1, %%mm0        \n\t"
            "packuswb       %%mm3, %%mm2        \n\t"
            "movq           %%mm0, %%mm1        \n\t"
            "movq           %%mm2, %%mm3        \n\t"
            "psrlw             $8, %%mm0        \n\t"
            "psrlw             $8, %%mm2        \n\t"
            "pand           %%mm7, %%mm1        \n\t"
            "pand           %%mm7, %%mm3        \n\t"
            "packuswb       %%mm2, %%mm0        \n\t"
            "packuswb       %%mm3, %%mm1        \n\t"
            MOVNTQ"         %%mm0,- 7(%4, %0)   \n\t"
            MOVNTQ"         %%mm1,- 7(%3, %0)   \n\t"
            "add               $8, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
        );
        count -= 7;
    }
#endif
    while(count<0) {
        dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
        dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
        count++;
    }
}

2289
#if !COMPILE_TEMPLATE_AMD3DNOW
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
static void RENAME(extract_odd2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
{
    dst0+=   count;
    dst1+=   count;
    src += 4*count;
    count= - count;
    if(count <= -8) {
        count += 7;
        __asm__ volatile(
            "pcmpeqw       %%mm7, %%mm7        \n\t"
            "psrlw            $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq -28(%1, %0, 4), %%mm0        \n\t"
            "movq -20(%1, %0, 4), %%mm1        \n\t"
            "movq -12(%1, %0, 4), %%mm2        \n\t"
            "movq  -4(%1, %0, 4), %%mm3        \n\t"
            "psrlw            $8, %%mm0        \n\t"
            "psrlw            $8, %%mm1        \n\t"
            "psrlw            $8, %%mm2        \n\t"
            "psrlw            $8, %%mm3        \n\t"
            "packuswb      %%mm1, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm2        \n\t"
            "movq          %%mm0, %%mm1        \n\t"
            "movq          %%mm2, %%mm3        \n\t"
            "psrlw            $8, %%mm0        \n\t"
            "psrlw            $8, %%mm2        \n\t"
            "pand          %%mm7, %%mm1        \n\t"
            "pand          %%mm7, %%mm3        \n\t"
            "packuswb      %%mm2, %%mm0        \n\t"
            "packuswb      %%mm3, %%mm1        \n\t"
            MOVNTQ"        %%mm0,- 7(%3, %0)   \n\t"
            MOVNTQ"        %%mm1,- 7(%2, %0)   \n\t"
            "add              $8, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src), "r"(dst0), "r"(dst1)
        );
        count -= 7;
    }
    src++;
    while(count<0) {
        dst0[count]= src[4*count+0];
        dst1[count]= src[4*count+2];
        count++;
    }
}
2336
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393

static void RENAME(extract_odd2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
{
    dst0 +=   count;
    dst1 +=   count;
    src0 += 4*count;
    src1 += 4*count;
    count= - count;
#ifdef PAVGB
    if(count <= -8) {
        count += 7;
        __asm__ volatile(
            "pcmpeqw        %%mm7, %%mm7        \n\t"
            "psrlw             $8, %%mm7        \n\t"
            "1:                                \n\t"
            "movq  -28(%1, %0, 4), %%mm0        \n\t"
            "movq  -20(%1, %0, 4), %%mm1        \n\t"
            "movq  -12(%1, %0, 4), %%mm2        \n\t"
            "movq   -4(%1, %0, 4), %%mm3        \n\t"
            PAVGB" -28(%2, %0, 4), %%mm0        \n\t"
            PAVGB" -20(%2, %0, 4), %%mm1        \n\t"
            PAVGB" -12(%2, %0, 4), %%mm2        \n\t"
            PAVGB" - 4(%2, %0, 4), %%mm3        \n\t"
            "psrlw             $8, %%mm0        \n\t"
            "psrlw             $8, %%mm1        \n\t"
            "psrlw             $8, %%mm2        \n\t"
            "psrlw             $8, %%mm3        \n\t"
            "packuswb       %%mm1, %%mm0        \n\t"
            "packuswb       %%mm3, %%mm2        \n\t"
            "movq           %%mm0, %%mm1        \n\t"
            "movq           %%mm2, %%mm3        \n\t"
            "psrlw             $8, %%mm0        \n\t"
            "psrlw             $8, %%mm2        \n\t"
            "pand           %%mm7, %%mm1        \n\t"
            "pand           %%mm7, %%mm3        \n\t"
            "packuswb       %%mm2, %%mm0        \n\t"
            "packuswb       %%mm3, %%mm1        \n\t"
            MOVNTQ"         %%mm0,- 7(%4, %0)   \n\t"
            MOVNTQ"         %%mm1,- 7(%3, %0)   \n\t"
            "add               $8, %0           \n\t"
            " js 1b                            \n\t"
            : "+r"(count)
            : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
        );
        count -= 7;
    }
#endif
    src0++;
    src1++;
    while(count<0) {
        dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
        dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
        count++;
    }
}

static void RENAME(yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
2394 2395
                                 int width, int height,
                                 int lumStride, int chromStride, int srcStride)
2396
{
2397
    int y;
2398
    const int chromWidth = AV_CEIL_RSHIFT(width, 1);
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

    for (y=0; y<height; y++) {
        RENAME(extract_even)(src, ydst, width);
        if(y&1) {
            RENAME(extract_odd2avg)(src-srcStride, src, udst, vdst, chromWidth);
            udst+= chromStride;
            vdst+= chromStride;
        }

        src += srcStride;
        ydst+= lumStride;
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}

2418
#if !COMPILE_TEMPLATE_AMD3DNOW
2419
static void RENAME(yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
2420 2421
                                 int width, int height,
                                 int lumStride, int chromStride, int srcStride)
2422
{
2423
    int y;
2424
    const int chromWidth = AV_CEIL_RSHIFT(width, 1);
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440

    for (y=0; y<height; y++) {
        RENAME(extract_even)(src, ydst, width);
        RENAME(extract_odd2)(src, udst, vdst, chromWidth);

        src += srcStride;
        ydst+= lumStride;
        udst+= chromStride;
        vdst+= chromStride;
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}
2441
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
2442 2443

static void RENAME(uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
2444 2445
                                 int width, int height,
                                 int lumStride, int chromStride, int srcStride)
2446
{
2447
    int y;
2448
    const int chromWidth = AV_CEIL_RSHIFT(width, 1);
2449 2450

    for (y=0; y<height; y++) {
2451
        RENAME(extract_odd)(src, ydst, width);
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
        if(y&1) {
            RENAME(extract_even2avg)(src-srcStride, src, udst, vdst, chromWidth);
            udst+= chromStride;
            vdst+= chromStride;
        }

        src += srcStride;
        ydst+= lumStride;
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}

2468
#if !COMPILE_TEMPLATE_AMD3DNOW
2469
static void RENAME(uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
2470 2471
                                 int width, int height,
                                 int lumStride, int chromStride, int srcStride)
2472
{
2473
    int y;
2474
    const int chromWidth = AV_CEIL_RSHIFT(width, 1);
2475 2476

    for (y=0; y<height; y++) {
2477
        RENAME(extract_odd)(src, ydst, width);
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
        RENAME(extract_even2)(src, udst, vdst, chromWidth);

        src += srcStride;
        ydst+= lumStride;
        udst+= chromStride;
        vdst+= chromStride;
    }
    __asm__(
            EMMS"       \n\t"
            SFENCE"     \n\t"
            ::: "memory"
        );
}
2491 2492
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
#endif /* !COMPILE_TEMPLATE_SSE2 */
2493

2494
static av_cold void RENAME(rgb2rgb_init)(void)
2495
{
2496 2497
#if !COMPILE_TEMPLATE_SSE2
#if !COMPILE_TEMPLATE_AMD3DNOW
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
    rgb15to16          = RENAME(rgb15to16);
    rgb15tobgr24       = RENAME(rgb15tobgr24);
    rgb15to32          = RENAME(rgb15to32);
    rgb16tobgr24       = RENAME(rgb16tobgr24);
    rgb16to32          = RENAME(rgb16to32);
    rgb16to15          = RENAME(rgb16to15);
    rgb24tobgr16       = RENAME(rgb24tobgr16);
    rgb24tobgr15       = RENAME(rgb24tobgr15);
    rgb24tobgr32       = RENAME(rgb24tobgr32);
    rgb32to16          = RENAME(rgb32to16);
    rgb32to15          = RENAME(rgb32to15);
    rgb32tobgr24       = RENAME(rgb32tobgr24);
    rgb24to15          = RENAME(rgb24to15);
    rgb24to16          = RENAME(rgb24to16);
    rgb24tobgr24       = RENAME(rgb24tobgr24);
    rgb32tobgr16       = RENAME(rgb32tobgr16);
    rgb32tobgr15       = RENAME(rgb32tobgr15);
    yv12toyuy2         = RENAME(yv12toyuy2);
    yv12touyvy         = RENAME(yv12touyvy);
    yuv422ptoyuy2      = RENAME(yuv422ptoyuy2);
    yuv422ptouyvy      = RENAME(yuv422ptouyvy);
    yuy2toyv12         = RENAME(yuy2toyv12);
    vu9_to_vu12        = RENAME(vu9_to_vu12);
    yvu9_to_yuy2       = RENAME(yvu9_to_yuy2);
    uyvytoyuv422       = RENAME(uyvytoyuv422);
    yuyvtoyuv422       = RENAME(yuyvtoyuv422);
2524
#endif /* !COMPILE_TEMPLATE_AMD3DNOW */
2525

2526
#if COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW
2527
    planar2x           = RENAME(planar2x);
2528
#endif /* COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW */
2529
#if HAVE_7REGS
2530
    ff_rgb24toyv12     = RENAME(rgb24toyv12);
2531
#endif /* HAVE_7REGS */
2532 2533 2534

    yuyvtoyuv420       = RENAME(yuyvtoyuv420);
    uyvytoyuv420       = RENAME(uyvytoyuv420);
2535
#endif /* !COMPILE_TEMPLATE_SSE2 */
2536

2537
#if !COMPILE_TEMPLATE_AMD3DNOW && !COMPILE_TEMPLATE_AVX
2538
    interleaveBytes    = RENAME(interleaveBytes);
2539
#endif /* !COMPILE_TEMPLATE_AMD3DNOW && !COMPILE_TEMPLATE_AVX */
2540
#if !COMPILE_TEMPLATE_AVX || HAVE_AVX_EXTERNAL
2541
#if !COMPILE_TEMPLATE_AMD3DNOW && (ARCH_X86_32 || COMPILE_TEMPLATE_SSE2) && COMPILE_TEMPLATE_MMXEXT == COMPILE_TEMPLATE_SSE2 && HAVE_X86ASM
2542 2543
    deinterleaveBytes  = RENAME(deinterleaveBytes);
#endif
2544
#endif
2545
}