rgb2rgb_template.c 67.7 KB
Newer Older
1
/*
2 3
 *
 *  rgb2rgb.c, Software RGB to RGB convertor
4 5 6
 *  pluralize by Software PAL8 to RGB convertor
 *               Software YUV to YUV convertor
 *               Software YUV to RGB convertor
7
 *  Written by Nick Kurshev.
8
 *  palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
9
 *  lot of big-endian byteorder fixes by Alex Beregszaszi
10
 *
11 12 13
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or modify
14 15 16 17
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
18
 * FFmpeg is distributed in the hope that it will be useful,
19 20 21 22 23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
24
 * along with FFmpeg; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 27
 */

Arpi's avatar
Arpi committed
28 29 30 31
#include <stddef.h>
#include <inttypes.h> /* for __WORDSIZE */

#ifndef __WORDSIZE
32 33
// #warning You have misconfigured system and probably will lose performance!
#define __WORDSIZE MP_WORDSIZE
Arpi's avatar
Arpi committed
34 35
#endif

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#undef PREFETCH
#undef MOVNTQ
#undef EMMS
#undef SFENCE
#undef MMREG_SIZE
#undef PREFETCHW
#undef PAVGB

#ifdef HAVE_SSE2
#define MMREG_SIZE 16
#else
#define MMREG_SIZE 8
#endif

#ifdef HAVE_3DNOW
#define PREFETCH  "prefetch"
#define PREFETCHW "prefetchw"
#define PAVGB	  "pavgusb"
#elif defined ( HAVE_MMX2 )
#define PREFETCH "prefetchnta"
#define PREFETCHW "prefetcht0"
#define PAVGB	  "pavgb"
#else
59 60 61
#ifdef __APPLE__
#define PREFETCH "#"
#define PREFETCHW "#"
62
#else
63 64
#define PREFETCH "/nop"
#define PREFETCHW "/nop"
65
#endif
66
#endif
67 68 69 70 71 72

#ifdef HAVE_3DNOW
/* On K6 femms is faster of emms. On K7 femms is directly mapped on emms. */
#define EMMS     "femms"
#else
#define EMMS     "emms"
73
#endif
Nick Kurshev's avatar
Nick Kurshev committed
74

75 76 77 78 79
#ifdef HAVE_MMX2
#define MOVNTQ "movntq"
#define SFENCE "sfence"
#else
#define MOVNTQ "movq"
80 81
#ifdef __APPLE__
#define SFENCE "#"
82
#else
83 84
#define SFENCE "/nop"
#endif
85
#endif
86

87
static inline void RENAME(rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size)
Nick Kurshev's avatar
Nick Kurshev committed
88
{
89
  uint8_t *dest = dst;
Nick Kurshev's avatar
Nick Kurshev committed
90 91
  const uint8_t *s = src;
  const uint8_t *end;
Nick Kurshev's avatar
Nick Kurshev committed
92
#ifdef HAVE_MMX
93
  const uint8_t *mm_end;
Nick Kurshev's avatar
Nick Kurshev committed
94
#endif
Nick Kurshev's avatar
Nick Kurshev committed
95
  end = s + src_size;
Nick Kurshev's avatar
Nick Kurshev committed
96
#ifdef HAVE_MMX
97
  __asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
98
  mm_end = end - 23;
99
  __asm __volatile("movq	%0, %%mm7"::"m"(mask32):"memory");
Nick Kurshev's avatar
Nick Kurshev committed
100 101 102
  while(s < mm_end)
  {
    __asm __volatile(
103
	PREFETCH"	32%1\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
104
	"movd	%1, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
105 106 107 108 109 110 111
	"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"
Nick Kurshev's avatar
Nick Kurshev committed
112
	"pand	%%mm7, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
113
	"pand	%%mm7, %%mm1\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
114
	"pand	%%mm7, %%mm2\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
115
	"pand	%%mm7, %%mm3\n\t"
116
	MOVNTQ"	%%mm0, %0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
117 118 119
	MOVNTQ"	%%mm1, 8%0\n\t"
	MOVNTQ"	%%mm2, 16%0\n\t"
	MOVNTQ"	%%mm3, 24%0"
Nick Kurshev's avatar
Nick Kurshev committed
120 121 122
	:"=m"(*dest)
	:"m"(*s)
	:"memory");
Nick Kurshev's avatar
Nick Kurshev committed
123 124
    dest += 32;
    s += 24;
Nick Kurshev's avatar
Nick Kurshev committed
125
  }
Nick Kurshev's avatar
Nick Kurshev committed
126
  __asm __volatile(SFENCE:::"memory");
127
  __asm __volatile(EMMS:::"memory");
Nick Kurshev's avatar
Nick Kurshev committed
128
#endif
Nick Kurshev's avatar
Nick Kurshev committed
129 130
  while(s < end)
  {
131
#ifdef WORDS_BIGENDIAN
132
    /* RGB24 (= R,G,B) -> RGB32 (= A,B,G,R) */
133
    *dest++ = 0;
134 135 136 137
    *dest++ = s[2];
    *dest++ = s[1];
    *dest++ = s[0];
    s+=3;
138
#else
139 140 141 142
    *dest++ = *s++;
    *dest++ = *s++;
    *dest++ = *s++;
    *dest++ = 0;
143
#endif
Nick Kurshev's avatar
Nick Kurshev committed
144 145
  }
}
146

147
static inline void RENAME(rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size)
148 149
{
  uint8_t *dest = dst;
Nick Kurshev's avatar
Nick Kurshev committed
150 151
  const uint8_t *s = src;
  const uint8_t *end;
152
#ifdef HAVE_MMX
153
  const uint8_t *mm_end;
154
#endif
155
  end = s + src_size;
156
#ifdef HAVE_MMX
157
  __asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
158
  mm_end = end - 31;
159 160 161
  while(s < mm_end)
  {
    __asm __volatile(
162
	PREFETCH"	32%1\n\t"
163 164
	"movq	%1, %%mm0\n\t"
	"movq	8%1, %%mm1\n\t"
165 166
	"movq	16%1, %%mm4\n\t"
	"movq	24%1, %%mm5\n\t"
167 168
	"movq	%%mm0, %%mm2\n\t"
	"movq	%%mm1, %%mm3\n\t"
169 170
	"movq	%%mm4, %%mm6\n\t"
	"movq	%%mm5, %%mm7\n\t"
171 172
	"psrlq	$8, %%mm2\n\t"
	"psrlq	$8, %%mm3\n\t"
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	"psrlq	$8, %%mm6\n\t"
	"psrlq	$8, %%mm7\n\t"
	"pand	%2, %%mm0\n\t"
	"pand	%2, %%mm1\n\t"
	"pand	%2, %%mm4\n\t"
	"pand	%2, %%mm5\n\t"
	"pand	%3, %%mm2\n\t"
	"pand	%3, %%mm3\n\t"
	"pand	%3, %%mm6\n\t"
	"pand	%3, %%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"
	"pand	%4, %%mm2\n\t"
	"pand	%5, %%mm3\n\t"
194
	"por	%%mm2, %%mm0\n\t"
195 196 197
	"psrlq	$16, %%mm1\n\t"
	"psrlq	$32, %%mm4\n\t"
	"psllq	$16, %%mm5\n\t"
198
	"por	%%mm3, %%mm1\n\t"
199 200
	"pand	%6, %%mm5\n\t"
	"por	%%mm5, %%mm4\n\t"
201

202
	MOVNTQ"	%%mm0, %0\n\t"
203 204
	MOVNTQ"	%%mm1, 8%0\n\t"
	MOVNTQ"	%%mm4, 16%0"
205
	:"=m"(*dest)
206 207
	:"m"(*s),"m"(mask24l),
	 "m"(mask24h),"m"(mask24hh),"m"(mask24hhh),"m"(mask24hhhh)
208
	:"memory");
209 210
    dest += 24;
    s += 32;
211 212 213 214
  }
  __asm __volatile(SFENCE:::"memory");
  __asm __volatile(EMMS:::"memory");
#endif
215 216
  while(s < end)
  {
217
#ifdef WORDS_BIGENDIAN
218
    /* RGB32 (= A,B,G,R) -> RGB24 (= R,G,B) */
219
    s++;
220 221 222 223
    dest[2] = *s++;
    dest[1] = *s++;
    dest[0] = *s++;
    dest += 3;
224
#else
225 226 227 228
    *dest++ = *s++;
    *dest++ = *s++;
    *dest++ = *s++;
    s++;
229
#endif
230 231
  }
}
232

233 234 235
/*
 Original by Strepto/Astral
 ported to gcc & bugfixed : A'rpi
Nick Kurshev's avatar
Nick Kurshev committed
236
 MMX2, 3DNOW optimization by Nick Kurshev
237
 32bit c version, and and&add trick by Michael Niedermayer
238
*/
239
static inline void RENAME(rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size)
240
{
Arpi's avatar
Arpi committed
241 242 243
  register const uint8_t* s=src;
  register uint8_t* d=dst;
  register const uint8_t *end;
244
  const uint8_t *mm_end;
Arpi's avatar
Arpi committed
245
  end = s + src_size;
246
#ifdef HAVE_MMX
Arpi's avatar
Arpi committed
247 248
  __asm __volatile(PREFETCH"	%0"::"m"(*s));
  __asm __volatile("movq	%0, %%mm4"::"m"(mask15s));
249
  mm_end = end - 15;
Arpi's avatar
Arpi committed
250
  while(s<mm_end)
251 252 253 254 255 256 257 258 259
  {
	__asm __volatile(
		PREFETCH"	32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	8%1, %%mm2\n\t"
		"movq	%%mm0, %%mm1\n\t"
		"movq	%%mm2, %%mm3\n\t"
		"pand	%%mm4, %%mm0\n\t"
		"pand	%%mm4, %%mm2\n\t"
260 261
		"paddw	%%mm1, %%mm0\n\t"
		"paddw	%%mm3, %%mm2\n\t"
262 263
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm2, 8%0"
Arpi's avatar
Arpi committed
264 265
		:"=m"(*d)
		:"m"(*s)
266
		);
Arpi's avatar
Arpi committed
267 268
	d+=16;
	s+=16;
269
  }
270 271
  __asm __volatile(SFENCE:::"memory");
  __asm __volatile(EMMS:::"memory");
272
#endif
273
    mm_end = end - 3;
Arpi's avatar
Arpi committed
274 275 276 277 278 279 280 281 282 283 284 285
    while(s < mm_end)
    {
	register unsigned x= *((uint32_t *)s);
	*((uint32_t *)d) = (x&0x7FFF7FFF) + (x&0x7FE07FE0);
	d+=4;
	s+=4;
    }
    if(s < end)
    {
	register unsigned short x= *((uint16_t *)s);
	*((uint16_t *)d) = (x&0x7FFF) + (x&0x7FE0);
    }
286
}
287

288
static inline void RENAME(rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size)
289 290 291 292
{
  register const uint8_t* s=src;
  register uint8_t* d=dst;
  register const uint8_t *end;
293
  const uint8_t *mm_end;
294 295 296 297 298
  end = s + src_size;
#ifdef HAVE_MMX
  __asm __volatile(PREFETCH"	%0"::"m"(*s));
  __asm __volatile("movq	%0, %%mm7"::"m"(mask15rg));
  __asm __volatile("movq	%0, %%mm6"::"m"(mask15b));
299
  mm_end = end - 15;
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
  while(s<mm_end)
  {
	__asm __volatile(
		PREFETCH"	32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	8%1, %%mm2\n\t"
		"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"
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm2, 8%0"
		:"=m"(*d)
		:"m"(*s)
		);
	d+=16;
	s+=16;
  }
  __asm __volatile(SFENCE:::"memory");
  __asm __volatile(EMMS:::"memory");
#endif
327 328
    mm_end = end - 3;
    while(s < mm_end)
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    {
	register uint32_t x= *((uint32_t *)s);
	*((uint32_t *)d) = ((x>>1)&0x7FE07FE0) | (x&0x001F001F);
	s+=4;
	d+=4;
    }
    if(s < end)
    {
	register uint16_t x= *((uint16_t *)s);
	*((uint16_t *)d) = ((x>>1)&0x7FE0) | (x&0x001F);
	s+=2;
	d+=2;
    }
}

344
static inline void RENAME(rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size)
345
{
346
	const uint8_t *s = src;
Arpi's avatar
Arpi committed
347 348 349 350
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
351 352
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
Arpi's avatar
Arpi committed
353
#ifdef HAVE_MMX
Michael Niedermayer's avatar
Michael Niedermayer committed
354 355 356 357 358 359
	mm_end = end - 15;
#if 1 //is faster only if multiplies are reasonable fast (FIXME figure out on which cpus this is faster, on Athlon its slightly faster)
	asm volatile(
		"movq %3, %%mm5			\n\t"
		"movq %4, %%mm6			\n\t"
		"movq %5, %%mm7			\n\t"
360
		ASMALIGN(4)
Michael Niedermayer's avatar
Michael Niedermayer committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		"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"
381 382 383
		"add $16, %1			\n\t"
		"add $8, %0			\n\t"
		"cmp %2, %1			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
384 385 386 387 388
		" jb 1b				\n\t"
		: "+r" (d), "+r"(s)
		: "r" (mm_end), "m" (mask3216g), "m" (mask3216br), "m" (mul3216)
	);
#else
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
	__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));
	while(s < mm_end)
	{
	    __asm __volatile(
		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	%%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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
		d += 4;
		s += 16;
	}
Michael Niedermayer's avatar
Michael Niedermayer committed
429
#endif
Arpi's avatar
Arpi committed
430 431 432
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
433 434
	while(s < end)
	{
435 436
		register int rgb = *(uint32_t*)s; s += 4;
		*d++ = ((rgb&0xFF)>>3) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>8);
437
	}
438 439
}

440
static inline void RENAME(rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size)
441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
	const uint8_t *s = src;
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
#ifdef HAVE_MMX
	__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));
455
	mm_end = end - 15;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	while(s < mm_end)
	{
	    __asm __volatile(
		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	%%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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
		d += 4;
		s += 16;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
	{
496 497
		register int rgb = *(uint32_t*)s; s += 4;
		*d++ = ((rgb&0xF8)<<8) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>19);
498 499 500
	}
}

501
static inline void RENAME(rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size)
502
{
503
	const uint8_t *s = src;
Arpi's avatar
Arpi committed
504 505 506 507
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
508 509
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
Arpi's avatar
Arpi committed
510
#ifdef HAVE_MMX
Michael Niedermayer's avatar
Michael Niedermayer committed
511 512 513 514 515 516
	mm_end = end - 15;
#if 1 //is faster only if multiplies are reasonable fast (FIXME figure out on which cpus this is faster, on Athlon its slightly faster)
	asm volatile(
		"movq %3, %%mm5			\n\t"
		"movq %4, %%mm6			\n\t"
		"movq %5, %%mm7			\n\t"
517
		ASMALIGN(4)
Michael Niedermayer's avatar
Michael Niedermayer committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
		"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"
538 539 540
		"add $16, %1			\n\t"
		"add $8, %0			\n\t"
		"cmp %2, %1			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
541 542 543 544 545
		" jb 1b				\n\t"
		: "+r" (d), "+r"(s)
		: "r" (mm_end), "m" (mask3215g), "m" (mask3216br), "m" (mul3215)
	);
#else
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	__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));
	while(s < mm_end)
	{
	    __asm __volatile(
		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	%%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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
		d += 4;
		s += 16;
	}
Michael Niedermayer's avatar
Michael Niedermayer committed
586
#endif
Arpi's avatar
Arpi committed
587 588 589
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
590 591
	while(s < end)
	{
592 593
		register int rgb = *(uint32_t*)s; s += 4;
		*d++ = ((rgb&0xFF)>>3) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>9);
594
	}
595 596
}

597
static inline void RENAME(rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size)
598 599 600 601 602 603 604 605 606 607 608 609 610 611
{
	const uint8_t *s = src;
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
#ifdef HAVE_MMX
	__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));
612
	mm_end = end - 15;
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	while(s < mm_end)
	{
	    __asm __volatile(
		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	%%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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
		d += 4;
		s += 16;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
	{
653 654
		register int rgb = *(uint32_t*)s; s += 4;
		*d++ = ((rgb&0xF8)<<7) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>19);
655 656 657
	}
}

658
static inline void RENAME(rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size)
659
{
Nick Kurshev's avatar
Nick Kurshev committed
660
	const uint8_t *s = src;
Arpi's avatar
Arpi committed
661 662 663 664
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
665
	uint16_t *d = (uint16_t *)dst;
Nick Kurshev's avatar
Nick Kurshev committed
666
	end = s + src_size;
Arpi's avatar
Arpi committed
667
#ifdef HAVE_MMX
Nick Kurshev's avatar
Nick Kurshev committed
668 669 670 671
	__asm __volatile(PREFETCH"	%0"::"m"(*src):"memory");
	__asm __volatile(
	    "movq	%0, %%mm7\n\t"
	    "movq	%1, %%mm6\n\t"
672
	    ::"m"(red_16mask),"m"(green_16mask));
673
	mm_end = end - 11;
Nick Kurshev's avatar
Nick Kurshev committed
674
	while(s < mm_end)
Nick Kurshev's avatar
Nick Kurshev committed
675 676 677 678
	{
	    __asm __volatile(
		PREFETCH" 32%1\n\t"
		"movd	%1, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
679 680
		"movd	3%1, %%mm3\n\t"
		"punpckldq 6%1, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
681 682 683 684 685 686 687
		"punpckldq 9%1, %%mm3\n\t"
		"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"
Nick Kurshev's avatar
Nick Kurshev committed
688 689 690 691 692 693 694 695 696 697
		"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"
Nick Kurshev's avatar
Nick Kurshev committed
698 699
		"por	%%mm1, %%mm0\n\t"
		"por	%%mm4, %%mm3\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
700
		"por	%%mm2, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
701
		"por	%%mm5, %%mm3\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
702 703
		"psllq	$16, %%mm3\n\t"
		"por	%%mm3, %%mm0\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
704
		MOVNTQ"	%%mm0, %0\n\t"
705
		:"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
Nick Kurshev's avatar
Nick Kurshev committed
706 707
		d += 4;
		s += 12;
Nick Kurshev's avatar
Nick Kurshev committed
708
	}
Arpi's avatar
Arpi committed
709 710 711
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
Nick Kurshev's avatar
Nick Kurshev committed
712 713 714 715 716 717 718
	while(s < end)
	{
		const int b= *s++;
		const int g= *s++;
		const int r= *s++;
		*d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
	}
719 720
}

721
static inline void RENAME(rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size)
722 723 724 725 726 727 728 729 730 731 732 733 734 735
{
	const uint8_t *s = src;
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
#ifdef HAVE_MMX
	__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));
736
	mm_end = end - 15;
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	while(s < mm_end)
	{
	    __asm __volatile(
		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"
		"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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
		d += 4;
		s += 12;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
	{
		const int r= *s++;
		const int g= *s++;
		const int b= *s++;
		*d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
	}
}

784
static inline void RENAME(rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size)
785
{
786
	const uint8_t *s = src;
Arpi's avatar
Arpi committed
787 788 789 790
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
791 792
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
Arpi's avatar
Arpi committed
793
#ifdef HAVE_MMX
794 795 796 797 798
	__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));
799
	mm_end = end - 11;
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	while(s < mm_end)
	{
	    __asm __volatile(
		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"
		"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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
		d += 4;
		s += 12;
	}
Arpi's avatar
Arpi committed
835 836 837
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
838 839 840 841 842 843 844
	while(s < end)
	{
		const int b= *s++;
		const int g= *s++;
		const int r= *s++;
		*d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
	}
Arpi's avatar
Arpi committed
845 846
}

847
static inline void RENAME(rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size)
848 849 850 851 852 853 854 855 856 857 858 859 860 861
{
	const uint8_t *s = src;
	const uint8_t *end;
#ifdef HAVE_MMX
	const uint8_t *mm_end;
#endif
	uint16_t *d = (uint16_t *)dst;
	end = s + src_size;
#ifdef HAVE_MMX
	__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));
862
	mm_end = end - 15;
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	while(s < mm_end)
	{
	    __asm __volatile(
		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"
		"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"
		MOVNTQ"	%%mm0, %0\n\t"
		:"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
		d += 4;
		s += 12;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
	{
		const int r= *s++;
		const int g= *s++;
		const int b= *s++;
		*d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
	}
}

Arpi's avatar
Arpi committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
/*
  I use here less accurate approximation by simply
 left-shifting the input
  value and filling the low order bits with
 zeroes. This method improves png's
  compression but this scheme cannot reproduce white exactly, since it does not
  generate an all-ones maximum value; the net effect is to darken the
  image slightly.

  The better method should be "left bit replication":

   4 3 2 1 0
   ---------
   1 1 0 1 1

   7 6 5 4 3  2 1 0
   ----------------
   1 1 0 1 1  1 1 0
   |=======|  |===|
       |      Leftmost Bits Repeated to Fill Open Bits
       |
   Original Bits
*/
933
static inline void RENAME(rgb15to24)(const uint8_t *src, uint8_t *dst, long src_size)
Arpi's avatar
Arpi committed
934 935 936 937 938 939 940 941 942 943
{
	const uint16_t *end;
#ifdef HAVE_MMX
	const uint16_t *mm_end;
#endif
	uint8_t *d = (uint8_t *)dst;
	const uint16_t *s = (uint16_t *)src;
	end = s + src_size/2;
#ifdef HAVE_MMX
	__asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
944
	mm_end = end - 7;
Arpi's avatar
Arpi committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	while(s < mm_end)
	{
	    __asm __volatile(
		PREFETCH" 32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	%1, %%mm1\n\t"
		"movq	%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$2, %%mm1\n\t"
		"psrlq	$7, %%mm2\n\t"
		"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"
		
		"movq	8%1, %%mm0\n\t"
		"movq	8%1, %%mm1\n\t"
		"movq	8%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$2, %%mm1\n\t"
		"psrlq	$7, %%mm2\n\t"
		"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)
		:"m"(*s),"m"(mask15b),"m"(mask15g),"m"(mask15r), "m"(mmx_null)
		:"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"

		"psrlq	$8, %%mm2\n\t"
		"psrlq	$8, %%mm3\n\t"
		"psrlq	$8, %%mm6\n\t"
		"psrlq	$8, %%mm7\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%2, %%mm1\n\t"
		"pand	%2, %%mm4\n\t"
		"pand	%2, %%mm5\n\t"
		"pand	%3, %%mm2\n\t"
		"pand	%3, %%mm3\n\t"
		"pand	%3, %%mm6\n\t"
		"pand	%3, %%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"
		"pand	%4, %%mm2\n\t"
		"pand	%5, %%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"
		"pand	%6, %%mm5\n\t"
		"por	%%mm5, %%mm4\n\t"

		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm1, 8%0\n\t"
		MOVNTQ"	%%mm4, 16%0"

		:"=m"(*d)
		:"m"(*s),"m"(mask24l),"m"(mask24h),"m"(mask24hh),"m"(mask24hhh),"m"(mask24hhhh)
		:"memory");
		d += 24;
		s += 8;
	}
1062 1063
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
Arpi's avatar
Arpi committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
#endif
	while(s < end)
	{
		register uint16_t bgr;
		bgr = *s++;
		*d++ = (bgr&0x1F)<<3;
		*d++ = (bgr&0x3E0)>>2;
		*d++ = (bgr&0x7C00)>>7;
	}
}

1075
static inline void RENAME(rgb16to24)(const uint8_t *src, uint8_t *dst, long src_size)
Arpi's avatar
Arpi committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
{
	const uint16_t *end;
#ifdef HAVE_MMX
	const uint16_t *mm_end;
#endif
	uint8_t *d = (uint8_t *)dst;
	const uint16_t *s = (const uint16_t *)src;
	end = s + src_size/2;
#ifdef HAVE_MMX
	__asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
1086
	mm_end = end - 7;
Arpi's avatar
Arpi committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	while(s < mm_end)
	{
	    __asm __volatile(
		PREFETCH" 32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	%1, %%mm1\n\t"
		"movq	%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$3, %%mm1\n\t"
		"psrlq	$8, %%mm2\n\t"
		"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"

		"movq	8%1, %%mm0\n\t"
		"movq	8%1, %%mm1\n\t"
		"movq	8%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$3, %%mm1\n\t"
		"psrlq	$8, %%mm2\n\t"
		"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)
		:"m"(*s),"m"(mask16b),"m"(mask16g),"m"(mask16r),"m"(mmx_null)		
		:"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"

		"psrlq	$8, %%mm2\n\t"
		"psrlq	$8, %%mm3\n\t"
		"psrlq	$8, %%mm6\n\t"
		"psrlq	$8, %%mm7\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%2, %%mm1\n\t"
		"pand	%2, %%mm4\n\t"
		"pand	%2, %%mm5\n\t"
		"pand	%3, %%mm2\n\t"
		"pand	%3, %%mm3\n\t"
		"pand	%3, %%mm6\n\t"
		"pand	%3, %%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"
		"pand	%4, %%mm2\n\t"
		"pand	%5, %%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"
		"pand	%6, %%mm5\n\t"
		"por	%%mm5, %%mm4\n\t"

		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm1, 8%0\n\t"
		MOVNTQ"	%%mm4, 16%0"

		:"=m"(*d)
		:"m"(*s),"m"(mask24l),"m"(mask24h),"m"(mask24hh),"m"(mask24hhh),"m"(mask24hhhh)
		:"memory");
		d += 24;
		s += 8;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
	{
		register uint16_t bgr;
		bgr = *s++;
		*d++ = (bgr&0x1F)<<3;
		*d++ = (bgr&0x7E0)>>3;
		*d++ = (bgr&0xF800)>>8;
	}
}

1216
static inline void RENAME(rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size)
Arpi's avatar
Arpi committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
{
	const uint16_t *end;
#ifdef HAVE_MMX
	const uint16_t *mm_end;
#endif
	uint8_t *d = (uint8_t *)dst;
	const uint16_t *s = (const uint16_t *)src;
	end = s + src_size/2;
#ifdef HAVE_MMX
	__asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
	__asm __volatile("pxor	%%mm7,%%mm7\n\t":::"memory");
1228
	mm_end = end - 3;
Arpi's avatar
Arpi committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	while(s < mm_end)
	{
	    __asm __volatile(
		PREFETCH" 32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	%1, %%mm1\n\t"
		"movq	%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$2, %%mm1\n\t"
		"psrlq	$7, %%mm2\n\t"
		"movq	%%mm0, %%mm3\n\t"
		"movq	%%mm1, %%mm4\n\t"
		"movq	%%mm2, %%mm5\n\t"
		"punpcklwd %%mm7, %%mm0\n\t"
		"punpcklwd %%mm7, %%mm1\n\t"
		"punpcklwd %%mm7, %%mm2\n\t"
		"punpckhwd %%mm7, %%mm3\n\t"
		"punpckhwd %%mm7, %%mm4\n\t"
		"punpckhwd %%mm7, %%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"
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm3, 8%0\n\t"
		:"=m"(*d)
		:"m"(*s),"m"(mask15b),"m"(mask15g),"m"(mask15r)
		:"memory");
		d += 16;
		s += 4;
	}
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
#endif
	while(s < end)
1271
	{
Michael Niedermayer's avatar
Michael Niedermayer committed
1272 1273 1274 1275
#if 0 //slightly slower on athlon
		int bgr= *s++;
		*((uint32_t*)d)++ = ((bgr&0x1F)<<3) + ((bgr&0x3E0)<<6) + ((bgr&0x7C00)<<9);
#else
Arpi's avatar
Arpi committed
1276 1277
		register uint16_t bgr;
		bgr = *s++;
1278 1279 1280
#ifdef WORDS_BIGENDIAN
		*d++ = 0;
		*d++ = (bgr&0x7C00)>>7;
1281 1282
		*d++ = (bgr&0x3E0)>>2;
		*d++ = (bgr&0x1F)<<3;
1283
#else
Arpi's avatar
Arpi committed
1284 1285 1286 1287
		*d++ = (bgr&0x1F)<<3;
		*d++ = (bgr&0x3E0)>>2;
		*d++ = (bgr&0x7C00)>>7;
		*d++ = 0;
1288 1289
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
1290
#endif
Arpi's avatar
Arpi committed
1291 1292
	}
}
1293

1294
static inline void RENAME(rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size)
Arpi's avatar
Arpi committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
{
	const uint16_t *end;
#ifdef HAVE_MMX
	const uint16_t *mm_end;
#endif
	uint8_t *d = (uint8_t *)dst;
	const uint16_t *s = (uint16_t *)src;
	end = s + src_size/2;
#ifdef HAVE_MMX
	__asm __volatile(PREFETCH"	%0"::"m"(*s):"memory");
	__asm __volatile("pxor	%%mm7,%%mm7\n\t":::"memory");
1306
	mm_end = end - 3;
Arpi's avatar
Arpi committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	while(s < mm_end)
	{
	    __asm __volatile(
		PREFETCH" 32%1\n\t"
		"movq	%1, %%mm0\n\t"
		"movq	%1, %%mm1\n\t"
		"movq	%1, %%mm2\n\t"
		"pand	%2, %%mm0\n\t"
		"pand	%3, %%mm1\n\t"
		"pand	%4, %%mm2\n\t"
		"psllq	$3, %%mm0\n\t"
		"psrlq	$3, %%mm1\n\t"
		"psrlq	$8, %%mm2\n\t"
		"movq	%%mm0, %%mm3\n\t"
		"movq	%%mm1, %%mm4\n\t"
		"movq	%%mm2, %%mm5\n\t"
		"punpcklwd %%mm7, %%mm0\n\t"
		"punpcklwd %%mm7, %%mm1\n\t"
		"punpcklwd %%mm7, %%mm2\n\t"
		"punpckhwd %%mm7, %%mm3\n\t"
		"punpckhwd %%mm7, %%mm4\n\t"
		"punpckhwd %%mm7, %%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"
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm3, 8%0\n\t"
		:"=m"(*d)
		:"m"(*s),"m"(mask16b),"m"(mask16g),"m"(mask16r)
		:"memory");
		d += 16;
		s += 4;
1344
	}
Arpi's avatar
Arpi committed
1345 1346
	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
1347
#endif
Arpi's avatar
Arpi committed
1348 1349 1350 1351
	while(s < end)
	{
		register uint16_t bgr;
		bgr = *s++;
1352 1353 1354
#ifdef WORDS_BIGENDIAN
		*d++ = 0;
		*d++ = (bgr&0xF800)>>8;
1355 1356
		*d++ = (bgr&0x7E0)>>3;
		*d++ = (bgr&0x1F)<<3;
1357
#else
Arpi's avatar
Arpi committed
1358 1359 1360 1361
		*d++ = (bgr&0x1F)<<3;
		*d++ = (bgr&0x7E0)>>3;
		*d++ = (bgr&0xF800)>>8;
		*d++ = 0;
1362
#endif
Arpi's avatar
Arpi committed
1363
	}
1364
}
1365

1366
static inline void RENAME(rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size)
1367 1368
{
#ifdef HAVE_MMX
Arpi's avatar
Arpi committed
1369
/* TODO: unroll this loop */
1370
	asm volatile (
1371
		"xor %%"REG_a", %%"REG_a"	\n\t"
1372
		ASMALIGN(4)
1373
		"1:				\n\t"
1374 1375
		PREFETCH" 32(%0, %%"REG_a")	\n\t"
		"movq (%0, %%"REG_a"), %%mm0	\n\t"
1376 1377 1378 1379
		"movq %%mm0, %%mm1		\n\t"
		"movq %%mm0, %%mm2		\n\t"
		"pslld $16, %%mm0		\n\t"
		"psrld $16, %%mm1		\n\t"
Arpi's avatar
Arpi committed
1380 1381 1382
		"pand "MANGLE(mask32r)", %%mm0	\n\t"
		"pand "MANGLE(mask32g)", %%mm2	\n\t"
		"pand "MANGLE(mask32b)", %%mm1	\n\t"
1383 1384
		"por %%mm0, %%mm2		\n\t"
		"por %%mm1, %%mm2		\n\t"
1385 1386 1387
		MOVNTQ" %%mm2, (%1, %%"REG_a")	\n\t"
		"add $8, %%"REG_a"		\n\t"
		"cmp %2, %%"REG_a"		\n\t"
1388
		" jb 1b				\n\t"
1389
		:: "r" (src), "r"(dst), "r" (src_size-7)
1390
		: "%"REG_a
1391
	);
Michael Niedermayer's avatar
Michael Niedermayer committed
1392 1393 1394

	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");
1395
#else
Arpi's avatar
Arpi committed
1396 1397
	unsigned i;
	unsigned num_pixels = src_size >> 2;
1398 1399
	for(i=0; i<num_pixels; i++)
	{
1400 1401 1402 1403 1404 1405 1406 1407 1408
#ifdef WORDS_BIGENDIAN  
	  dst[4*i + 1] = src[4*i + 3];
	  dst[4*i + 2] = src[4*i + 2];
	  dst[4*i + 3] = src[4*i + 1];
#else
	  dst[4*i + 0] = src[4*i + 2];
	  dst[4*i + 1] = src[4*i + 1];
	  dst[4*i + 2] = src[4*i + 0];
#endif
1409 1410 1411 1412
	}
#endif
}

1413
static inline void RENAME(rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size)
Michael Niedermayer's avatar
Michael Niedermayer committed
1414
{
Arpi's avatar
Arpi committed
1415
	unsigned i;
Michael Niedermayer's avatar
Michael Niedermayer committed
1416
#ifdef HAVE_MMX
1417
	long mmx_size= 23 - src_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
1418 1419 1420 1421
	asm volatile (
		"movq "MANGLE(mask24r)", %%mm5	\n\t"
		"movq "MANGLE(mask24g)", %%mm6	\n\t"
		"movq "MANGLE(mask24b)", %%mm7	\n\t"
1422
		ASMALIGN(4)
Michael Niedermayer's avatar
Michael Niedermayer committed
1423
		"1:				\n\t"
1424 1425 1426 1427
		PREFETCH" 32(%1, %%"REG_a")	\n\t"
		"movq   (%1, %%"REG_a"), %%mm0	\n\t" // BGR BGR BG
		"movq   (%1, %%"REG_a"), %%mm1	\n\t" // BGR BGR BG
		"movq  2(%1, %%"REG_a"), %%mm2	\n\t" // R BGR BGR B
Michael Niedermayer's avatar
Michael Niedermayer committed
1428 1429 1430 1431 1432 1433
		"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"                
1434 1435 1436 1437
		"movq  6(%1, %%"REG_a"), %%mm0	\n\t" // BGR BGR BG
		MOVNTQ" %%mm1,   (%2, %%"REG_a")\n\t" // RGB RGB RG
		"movq  8(%1, %%"REG_a"), %%mm1	\n\t" // R BGR BGR B
		"movq 10(%1, %%"REG_a"), %%mm2	\n\t" // GR BGR BGR
Michael Niedermayer's avatar
Michael Niedermayer committed
1438 1439 1440 1441 1442
		"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"                
1443 1444 1445 1446
		"movq 14(%1, %%"REG_a"), %%mm0	\n\t" // R BGR BGR B
		MOVNTQ" %%mm1,  8(%2, %%"REG_a")\n\t" // B RGB RGB R
		"movq 16(%1, %%"REG_a"), %%mm1	\n\t" // GR BGR BGR
		"movq 18(%1, %%"REG_a"), %%mm2	\n\t" // BGR BGR BG
Michael Niedermayer's avatar
Michael Niedermayer committed
1447 1448 1449 1450 1451
		"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"                
1452 1453
		MOVNTQ" %%mm1, 16(%2, %%"REG_a")\n\t"
		"add $24, %%"REG_a"		\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1454 1455 1456 1457 1458 1459 1460 1461
		" js 1b				\n\t"
		: "+a" (mmx_size)
		: "r" (src-mmx_size), "r"(dst-mmx_size)
	);

	__asm __volatile(SFENCE:::"memory");
	__asm __volatile(EMMS:::"memory");

1462
	if(mmx_size==23) return; //finihsed, was multiple of 8
Arpi's avatar
Arpi committed
1463

Michael Niedermayer's avatar
Michael Niedermayer committed
1464 1465
	src+= src_size;
	dst+= src_size;
Arpi's avatar
Arpi committed
1466
	src_size= 23-mmx_size;
Michael Niedermayer's avatar
Michael Niedermayer committed
1467 1468 1469 1470 1471
	src-= src_size;
	dst-= src_size;
#endif
	for(i=0; i<src_size; i+=3)
	{
Arpi's avatar
Arpi committed
1472
		register uint8_t x;
Michael Niedermayer's avatar
Michael Niedermayer committed
1473 1474 1475 1476 1477 1478 1479
		x          = src[i + 2];
		dst[i + 1] = src[i + 1];
		dst[i + 2] = src[i + 0];
		dst[i + 0] = x;
	}
}

1480
static inline void RENAME(yuvPlanartoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1481 1482
	long width, long height,
	long lumStride, long chromStride, long dstStride, long vertLumPerChroma)
Michael Niedermayer's avatar
Michael Niedermayer committed
1483
{
1484 1485
	long y;
	const long chromWidth= width>>1;
Michael Niedermayer's avatar
Michael Niedermayer committed
1486 1487
	for(y=0; y<height; y++)
	{
Michael Niedermayer's avatar
Michael Niedermayer committed
1488
#ifdef HAVE_MMX
Michael Niedermayer's avatar
Michael Niedermayer committed
1489 1490
//FIXME handle 2 lines a once (fewer prefetch, reuse some chrom, but very likely limited by mem anyway)
		asm volatile(
1491
			"xor %%"REG_a", %%"REG_a"	\n\t"
1492
			ASMALIGN(4)
Michael Niedermayer's avatar
Michael Niedermayer committed
1493
			"1:				\n\t"
1494 1495 1496 1497
			PREFETCH" 32(%1, %%"REG_a", 2)	\n\t"
			PREFETCH" 32(%2, %%"REG_a")	\n\t"
			PREFETCH" 32(%3, %%"REG_a")	\n\t"
			"movq (%2, %%"REG_a"), %%mm0	\n\t" // U(0)
Michael Niedermayer's avatar
Michael Niedermayer committed
1498
			"movq %%mm0, %%mm2		\n\t" // U(0)
1499
			"movq (%3, %%"REG_a"), %%mm1	\n\t" // V(0)
Michael Niedermayer's avatar
Michael Niedermayer committed
1500 1501
			"punpcklbw %%mm1, %%mm0		\n\t" // UVUV UVUV(0)
			"punpckhbw %%mm1, %%mm2		\n\t" // UVUV UVUV(8)
Michael Niedermayer's avatar
Michael Niedermayer committed
1502

1503 1504
			"movq (%1, %%"REG_a",2), %%mm3	\n\t" // Y(0)
			"movq 8(%1, %%"REG_a",2), %%mm5	\n\t" // Y(8)
Michael Niedermayer's avatar
Michael Niedermayer committed
1505 1506 1507 1508 1509 1510
			"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)
Michael Niedermayer's avatar
Michael Niedermayer committed
1511

1512 1513 1514 1515
			MOVNTQ" %%mm3, (%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm4, 8(%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm5, 16(%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm6, 24(%0, %%"REG_a", 4)\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1516

1517 1518
			"add $8, %%"REG_a"		\n\t"
			"cmp %4, %%"REG_a"		\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1519
			" jb 1b				\n\t"
1520
			::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
1521
			: "%"REG_a
Michael Niedermayer's avatar
Michael Niedermayer committed
1522
		);
Michael Niedermayer's avatar
Michael Niedermayer committed
1523
#else
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

#if defined ARCH_ALPHA && defined HAVE_MVI
#define pl2yuy2(n)					\
	y1 = yc[n];					\
	y2 = yc2[n];					\
	u = uc[n];					\
	v = vc[n];					\
	asm("unpkbw %1, %0" : "=r"(y1) : "r"(y1));	\
	asm("unpkbw %1, %0" : "=r"(y2) : "r"(y2));	\
	asm("unpkbl %1, %0" : "=r"(u) : "r"(u));	\
	asm("unpkbl %1, %0" : "=r"(v) : "r"(v));	\
	yuv1 = (u << 8) + (v << 24);			\
	yuv2 = yuv1 + y2;				\
	yuv1 += y1;					\
	qdst[n] = yuv1;					\
	qdst2[n] = yuv2;

		int i;
		uint64_t *qdst = (uint64_t *) dst;
		uint64_t *qdst2 = (uint64_t *) (dst + dstStride);
		const uint32_t *yc = (uint32_t *) ysrc;
		const uint32_t *yc2 = (uint32_t *) (ysrc + lumStride);
		const uint16_t *uc = (uint16_t*) usrc, *vc = (uint16_t*) vsrc;
		for(i = 0; i < chromWidth; i += 8){
			uint64_t y1, y2, yuv1, yuv2;
			uint64_t u, v;
			/* Prefetch */
			asm("ldq $31,64(%0)" :: "r"(yc));
			asm("ldq $31,64(%0)" :: "r"(yc2));
			asm("ldq $31,64(%0)" :: "r"(uc));
			asm("ldq $31,64(%0)" :: "r"(vc));

			pl2yuy2(0);
			pl2yuy2(1);
			pl2yuy2(2);
			pl2yuy2(3);

			yc += 4;
			yc2 += 4;
			uc += 4;
			vc += 4;
			qdst += 4;
			qdst2 += 4;
		}
		y++;
		ysrc += lumStride;
		dst += dstStride;

#elif __WORDSIZE >= 64
Michael Niedermayer's avatar
Michael Niedermayer committed
1573
		int i;
Arpi's avatar
Arpi committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
		uint64_t *ldst = (uint64_t *) dst;
		const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
		for(i = 0; i < chromWidth; i += 2){
			uint64_t k, l;
			k = yc[0] + (uc[0] << 8) +
			    (yc[1] << 16) + (vc[0] << 24);
			l = yc[2] + (uc[1] << 8) +
			    (yc[3] << 16) + (vc[1] << 24);
			*ldst++ = k + (l << 32);
			yc += 4;
			uc += 2;
			vc += 2;
Michael Niedermayer's avatar
Michael Niedermayer committed
1586
		}
Arpi's avatar
Arpi committed
1587 1588 1589 1590 1591

#else
		int i, *idst = (int32_t *) dst;
		const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
		for(i = 0; i < chromWidth; i++){
1592 1593 1594 1595
#ifdef WORDS_BIGENDIAN
			*idst++ = (yc[0] << 24)+ (uc[0] << 16) +
			    (yc[1] << 8) + (vc[0] << 0);
#else
Arpi's avatar
Arpi committed
1596 1597
			*idst++ = yc[0] + (uc[0] << 8) +
			    (yc[1] << 16) + (vc[0] << 24);
1598
#endif
Arpi's avatar
Arpi committed
1599 1600 1601 1602 1603
			yc += 2;
			uc++;
			vc++;
		}
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1604
#endif
1605
		if((y&(vertLumPerChroma-1))==(vertLumPerChroma-1) )
Michael Niedermayer's avatar
Michael Niedermayer committed
1606 1607 1608 1609 1610 1611
		{
			usrc += chromStride;
			vsrc += chromStride;
		}
		ysrc += lumStride;
		dst += dstStride;
Michael Niedermayer's avatar
Michael Niedermayer committed
1612
	}
Michael Niedermayer's avatar
Michael Niedermayer committed
1613 1614 1615 1616
#ifdef HAVE_MMX
asm(    EMMS" \n\t"
        SFENCE" \n\t"
        :::"memory");
Michael Niedermayer's avatar
Michael Niedermayer committed
1617
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1618 1619
}

1620 1621 1622 1623 1624 1625
/**
 *
 * 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 ill fix it)
 */
static inline void RENAME(yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1626 1627
	long width, long height,
	long lumStride, long chromStride, long dstStride)
1628 1629 1630
{
	//FIXME interpolate chroma
	RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
1631 1632 1633
}

static inline void RENAME(yuvPlanartouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1634 1635
	long width, long height,
	long lumStride, long chromStride, long dstStride, long vertLumPerChroma)
1636
{
1637 1638
	long y;
	const long chromWidth= width>>1;
1639 1640
	for(y=0; y<height; y++)
	{
Michael Niedermayer's avatar
Michael Niedermayer committed
1641 1642 1643
#ifdef HAVE_MMX
//FIXME handle 2 lines a once (fewer prefetch, reuse some chrom, but very likely limited by mem anyway)
		asm volatile(
1644
			"xor %%"REG_a", %%"REG_a"	\n\t"
1645
			ASMALIGN(4)
Michael Niedermayer's avatar
Michael Niedermayer committed
1646
			"1:				\n\t"
1647 1648 1649 1650
			PREFETCH" 32(%1, %%"REG_a", 2)	\n\t"
			PREFETCH" 32(%2, %%"REG_a")	\n\t"
			PREFETCH" 32(%3, %%"REG_a")	\n\t"
			"movq (%2, %%"REG_a"), %%mm0	\n\t" // U(0)
Michael Niedermayer's avatar
Michael Niedermayer committed
1651
			"movq %%mm0, %%mm2		\n\t" // U(0)
1652
			"movq (%3, %%"REG_a"), %%mm1	\n\t" // V(0)
Michael Niedermayer's avatar
Michael Niedermayer committed
1653 1654 1655
			"punpcklbw %%mm1, %%mm0		\n\t" // UVUV UVUV(0)
			"punpckhbw %%mm1, %%mm2		\n\t" // UVUV UVUV(8)

1656 1657
			"movq (%1, %%"REG_a",2), %%mm3	\n\t" // Y(0)
			"movq 8(%1, %%"REG_a",2), %%mm5	\n\t" // Y(8)
Michael Niedermayer's avatar
Michael Niedermayer committed
1658 1659 1660 1661 1662 1663 1664
			"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)

1665 1666 1667 1668
			MOVNTQ" %%mm0, (%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm4, 8(%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm2, 16(%0, %%"REG_a", 4)\n\t"
			MOVNTQ" %%mm6, 24(%0, %%"REG_a", 4)\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1669

1670 1671
			"add $8, %%"REG_a"		\n\t"
			"cmp %4, %%"REG_a"		\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1672
			" jb 1b				\n\t"
1673
			::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
1674
			: "%"REG_a
Michael Niedermayer's avatar
Michael Niedermayer committed
1675 1676 1677 1678
		);
#else
//FIXME adapt the alpha asm code from yv12->yuy2

1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
#if __WORDSIZE >= 64
		int i;
		uint64_t *ldst = (uint64_t *) dst;
		const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
		for(i = 0; i < chromWidth; i += 2){
			uint64_t k, l;
			k = uc[0] + (yc[0] << 8) +
			    (vc[0] << 16) + (yc[1] << 24);
			l = uc[1] + (yc[2] << 8) +
			    (vc[1] << 16) + (yc[3] << 24);
			*ldst++ = k + (l << 32);
			yc += 4;
			uc += 2;
			vc += 2;
		}

#else
		int i, *idst = (int32_t *) dst;
		const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
		for(i = 0; i < chromWidth; i++){
1699 1700 1701 1702
#ifdef WORDS_BIGENDIAN
			*idst++ = (uc[0] << 24)+ (yc[0] << 16) +
			    (vc[0] << 8) + (yc[1] << 0);
#else
1703 1704
			*idst++ = uc[0] + (yc[0] << 8) +
			    (vc[0] << 16) + (yc[1] << 24);
1705
#endif
1706 1707 1708 1709
			yc += 2;
			uc++;
			vc++;
		}
Michael Niedermayer's avatar
Michael Niedermayer committed
1710
#endif
1711 1712 1713 1714 1715 1716 1717 1718 1719
#endif
		if((y&(vertLumPerChroma-1))==(vertLumPerChroma-1) )
		{
			usrc += chromStride;
			vsrc += chromStride;
		}
		ysrc += lumStride;
		dst += dstStride;
	}
Michael Niedermayer's avatar
Michael Niedermayer committed
1720 1721 1722 1723 1724
#ifdef HAVE_MMX
asm(    EMMS" \n\t"
        SFENCE" \n\t"
        :::"memory");
#endif
1725 1726 1727 1728 1729 1730 1731 1732
}

/**
 *
 * 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 ill fix it)
 */
static inline void RENAME(yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
1733 1734
	long width, long height,
	long lumStride, long chromStride, long dstStride)
1735 1736 1737
{
	//FIXME interpolate chroma
	RENAME(yuvPlanartouyvy)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
1738 1739 1740 1741 1742 1743 1744
}

/**
 *
 * 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,
1745 1746
	long width, long height,
	long lumStride, long chromStride, long dstStride)
1747 1748 1749 1750
{
	RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 1);
}

1751 1752 1753 1754 1755
/**
 *
 * 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 ill fix it)
 */
1756
static inline void RENAME(yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1757 1758
	long width, long height,
	long lumStride, long chromStride, long srcStride)
Michael Niedermayer's avatar
Michael Niedermayer committed
1759
{
1760 1761
	long y;
	const long chromWidth= width>>1;
1762 1763
	for(y=0; y<height; y+=2)
	{
Michael Niedermayer's avatar
Michael Niedermayer committed
1764
#ifdef HAVE_MMX
1765
		asm volatile(
1766
			"xor %%"REG_a", %%"REG_a"	\n\t"
1767 1768
			"pcmpeqw %%mm7, %%mm7		\n\t"
			"psrlw $8, %%mm7		\n\t" // FF,00,FF,00...
1769
			ASMALIGN(4)
1770
			"1:				\n\t"
1771 1772 1773
			PREFETCH" 64(%0, %%"REG_a", 4)	\n\t"
			"movq (%0, %%"REG_a", 4), %%mm0	\n\t" // YUYV YUYV(0)
			"movq 8(%0, %%"REG_a", 4), %%mm1\n\t" // YUYV YUYV(4)
1774 1775 1776 1777 1778 1779 1780 1781 1782
			"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)

1783
			MOVNTQ" %%mm2, (%1, %%"REG_a", 2)\n\t"
1784

1785 1786
			"movq 16(%0, %%"REG_a", 4), %%mm1\n\t" // YUYV YUYV(8)
			"movq 24(%0, %%"REG_a", 4), %%mm2\n\t" // YUYV YUYV(12)
1787 1788 1789 1790 1791 1792 1793 1794 1795
			"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)

1796
			MOVNTQ" %%mm3, 8(%1, %%"REG_a", 2)\n\t"
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806

			"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)

1807 1808
			MOVNTQ" %%mm0, (%3, %%"REG_a")	\n\t"
			MOVNTQ" %%mm2, (%2, %%"REG_a")	\n\t"
1809

1810 1811
			"add $8, %%"REG_a"		\n\t"
			"cmp %4, %%"REG_a"		\n\t"
1812
			" jb 1b				\n\t"
1813
			::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1814
			: "memory", "%"REG_a
Michael Niedermayer's avatar
Michael Niedermayer committed
1815
		);
1816

Michael Niedermayer's avatar
Michael Niedermayer committed
1817 1818 1819
		ydst += lumStride;
		src  += srcStride;

Michael Niedermayer's avatar
Michael Niedermayer committed
1820
		asm volatile(
1821
			"xor %%"REG_a", %%"REG_a"	\n\t"
1822
			ASMALIGN(4)
1823
			"1:				\n\t"
1824 1825 1826 1827 1828
			PREFETCH" 64(%0, %%"REG_a", 4)	\n\t"
			"movq (%0, %%"REG_a", 4), %%mm0	\n\t" // YUYV YUYV(0)
			"movq 8(%0, %%"REG_a", 4), %%mm1\n\t" // YUYV YUYV(4)
			"movq 16(%0, %%"REG_a", 4), %%mm2\n\t" // YUYV YUYV(8)
			"movq 24(%0, %%"REG_a", 4), %%mm3\n\t" // YUYV YUYV(12)
1829 1830 1831 1832 1833 1834 1835
			"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)

1836 1837
			MOVNTQ" %%mm0, (%1, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm2, 8(%1, %%"REG_a", 2)\n\t"
1838

1839 1840
			"add $8, %%"REG_a"		\n\t"
			"cmp %4, %%"REG_a"		\n\t"
1841 1842
			" jb 1b				\n\t"

1843
			::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
1844
			: "memory", "%"REG_a
1845
		);
Michael Niedermayer's avatar
Michael Niedermayer committed
1846
#else
1847
		long i;
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
		for(i=0; i<chromWidth; i++)
		{
			ydst[2*i+0] 	= src[4*i+0];
			udst[i] 	= src[4*i+1];
			ydst[2*i+1] 	= src[4*i+2];
			vdst[i] 	= src[4*i+3];
		}
		ydst += lumStride;
		src  += srcStride;

		for(i=0; i<chromWidth; i++)
		{
			ydst[2*i+0] 	= src[4*i+0];
			ydst[2*i+1] 	= src[4*i+2];
		}
#endif
		udst += chromStride;
		vdst += chromStride;
		ydst += lumStride;
		src  += srcStride;
Michael Niedermayer's avatar
Michael Niedermayer committed
1868
	}
1869
#ifdef HAVE_MMX
1870 1871 1872
asm volatile(   EMMS" \n\t"
        	SFENCE" \n\t"
        	:::"memory");
Michael Niedermayer's avatar
Michael Niedermayer committed
1873
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1874
}
Arpi's avatar
Arpi committed
1875

1876 1877
static inline void RENAME(yvu9toyv12)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc,
	uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1878
	long width, long height, long lumStride, long chromStride)
1879 1880 1881 1882 1883 1884 1885
{
	/* Y Plane */
	memcpy(ydst, ysrc, width*height);

	/* XXX: implement upscaling for U,V */
}

1886
static inline void RENAME(planar2x)(const uint8_t *src, uint8_t *dst, long srcWidth, long srcHeight, long srcStride, long dstStride)
1887
{
1888
	long x,y;
1889
	
1890 1891
	dst[0]= src[0];
        
1892
	// first line
1893 1894 1895
	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;
1896
	}
1897 1898 1899
	dst[2*srcWidth-1]= src[srcWidth-1];
	
        dst+= dstStride;
1900 1901 1902

	for(y=1; y<srcHeight; y++){
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1903
		const long mmxSize= srcWidth&~15;
1904
		asm volatile(
1905
			"mov %4, %%"REG_a"		\n\t"
1906
			"1:				\n\t"
1907 1908 1909 1910 1911 1912
			"movq (%0, %%"REG_a"), %%mm0	\n\t"
			"movq (%1, %%"REG_a"), %%mm1	\n\t"
			"movq 1(%0, %%"REG_a"), %%mm2	\n\t"
			"movq 1(%1, %%"REG_a"), %%mm3	\n\t"
			"movq -1(%0, %%"REG_a"), %%mm4	\n\t"
			"movq -1(%1, %%"REG_a"), %%mm5	\n\t"
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
			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"
1927
#if 1
1928 1929 1930 1931
			MOVNTQ" %%mm5, (%2, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm7, 8(%2, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm4, (%3, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm6, 8(%3, %%"REG_a", 2)\n\t"
1932
#else
1933 1934 1935 1936
			"movq %%mm5, (%2, %%"REG_a", 2)	\n\t"
			"movq %%mm7, 8(%2, %%"REG_a", 2)\n\t"
			"movq %%mm4, (%3, %%"REG_a", 2)	\n\t"
			"movq %%mm6, 8(%3, %%"REG_a", 2)\n\t"
1937
#endif
1938
			"add $8, %%"REG_a"		\n\t"
1939
			" js 1b				\n\t"
1940
			:: "r" (src + mmxSize  ), "r" (src + srcStride + mmxSize  ),
1941 1942
			   "r" (dst + mmxSize*2), "r" (dst + dstStride + mmxSize*2),
			   "g" (-mmxSize)
1943
			: "%"REG_a
1944 1945 1946

		);
#else
1947
		const long mmxSize=1;
1948 1949 1950
#endif
		dst[0        ]= (3*src[0] +   src[srcStride])>>2;
		dst[dstStride]= (  src[0] + 3*src[srcStride])>>2;
1951

1952
		for(x=mmxSize-1; x<srcWidth-1; x++){
1953 1954 1955 1956 1957
			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;
		}
1958 1959
		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;
1960 1961 1962 1963 1964 1965

		dst+=dstStride*2;
		src+=srcStride;
	}
	
	// last line
1966 1967 1968 1969 1970 1971 1972 1973 1974
#if 1
	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];
#else
1975 1976 1977 1978
	for(x=0; x<srcWidth; x++){
		dst[2*x+0]=
		dst[2*x+1]= src[x];
	}
1979 1980
#endif

1981 1982 1983 1984 1985 1986 1987
#ifdef HAVE_MMX
asm volatile(   EMMS" \n\t"
        	SFENCE" \n\t"
        	:::"memory");
#endif
}

Arpi's avatar
Arpi committed
1988 1989 1990 1991
/**
 *
 * 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 ill fix it)
1992
 * chrominance data is only taken from every secound line others are ignored FIXME write HQ version
Arpi's avatar
Arpi committed
1993
 */
1994
static inline void RENAME(uyvytoyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
1995 1996
	long width, long height,
	long lumStride, long chromStride, long srcStride)
Arpi's avatar
Arpi committed
1997
{
1998 1999
	long y;
	const long chromWidth= width>>1;
Arpi's avatar
Arpi committed
2000 2001
	for(y=0; y<height; y+=2)
	{
2002 2003 2004 2005 2006
#ifdef HAVE_MMX
		asm volatile(
			"xorl %%eax, %%eax		\n\t"
			"pcmpeqw %%mm7, %%mm7		\n\t"
			"psrlw $8, %%mm7		\n\t" // FF,00,FF,00...
2007
			ASMALIGN(4)
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
			"1:				\n\t"
			PREFETCH" 64(%0, %%eax, 4)	\n\t"
			"movq (%0, %%eax, 4), %%mm0	\n\t" // UYVY UYVY(0)
			"movq 8(%0, %%eax, 4), %%mm1	\n\t" // UYVY UYVY(4)
			"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)

			MOVNTQ" %%mm2, (%1, %%eax, 2)	\n\t"

			"movq 16(%0, %%eax, 4), %%mm1	\n\t" // UYVY UYVY(8)
			"movq 24(%0, %%eax, 4), %%mm2	\n\t" // UYVY UYVY(12)
			"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)

			MOVNTQ" %%mm3, 8(%1, %%eax, 2)	\n\t"

			"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)

			MOVNTQ" %%mm0, (%3, %%eax)	\n\t"
			MOVNTQ" %%mm2, (%2, %%eax)	\n\t"

			"addl $8, %%eax			\n\t"
			"cmpl %4, %%eax			\n\t"
			" jb 1b				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2051
			::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
2052 2053 2054 2055 2056 2057 2058 2059
			: "memory", "%eax"
		);

		ydst += lumStride;
		src  += srcStride;

		asm volatile(
			"xorl %%eax, %%eax		\n\t"
2060
			ASMALIGN(4)
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
			"1:				\n\t"
			PREFETCH" 64(%0, %%eax, 4)	\n\t"
			"movq (%0, %%eax, 4), %%mm0	\n\t" // YUYV YUYV(0)
			"movq 8(%0, %%eax, 4), %%mm1	\n\t" // YUYV YUYV(4)
			"movq 16(%0, %%eax, 4), %%mm2	\n\t" // YUYV YUYV(8)
			"movq 24(%0, %%eax, 4), %%mm3	\n\t" // YUYV YUYV(12)
			"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)

			MOVNTQ" %%mm0, (%1, %%eax, 2)	\n\t"
			MOVNTQ" %%mm2, 8(%1, %%eax, 2)	\n\t"

			"addl $8, %%eax			\n\t"
			"cmpl %4, %%eax			\n\t"
			" jb 1b				\n\t"

Michael Niedermayer's avatar
Michael Niedermayer committed
2081
			::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
2082 2083 2084
			: "memory", "%eax"
		);
#else
2085
		long i;
Arpi's avatar
Arpi committed
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
		for(i=0; i<chromWidth; i++)
		{
			udst[i] 	= src[4*i+0];
			ydst[2*i+0] 	= src[4*i+1];
			vdst[i] 	= src[4*i+2];
			ydst[2*i+1] 	= src[4*i+3];
		}
		ydst += lumStride;
		src  += srcStride;

		for(i=0; i<chromWidth; i++)
		{
			ydst[2*i+0] 	= src[4*i+1];
			ydst[2*i+1] 	= src[4*i+3];
		}
2101
#endif
Arpi's avatar
Arpi committed
2102 2103 2104 2105 2106
		udst += chromStride;
		vdst += chromStride;
		ydst += lumStride;
		src  += srcStride;
	}
2107 2108 2109 2110 2111
#ifdef HAVE_MMX
asm volatile(   EMMS" \n\t"
        	SFENCE" \n\t"
        	:::"memory");
#endif
Arpi's avatar
Arpi committed
2112 2113
}

2114 2115 2116 2117
/**
 *
 * 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 ill fix it)
2118
 * chrominance data is only taken from every secound line others are ignored in the C version FIXME write HQ version
2119 2120
 */
static inline void RENAME(rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
2121 2122
	long width, long height,
	long lumStride, long chromStride, long srcStride)
2123
{
2124 2125
	long y;
	const long chromWidth= width>>1;
2126 2127 2128
#ifdef HAVE_MMX
	for(y=0; y<height-2; y+=2)
	{
2129
		long i;
2130 2131 2132
		for(i=0; i<2; i++)
		{
			asm volatile(
2133
				"mov %2, %%"REG_a"		\n\t"
Felix Bünemann's avatar
Felix Bünemann committed
2134 2135
				"movq "MANGLE(bgr2YCoeff)", %%mm6		\n\t"
				"movq "MANGLE(w1111)", %%mm5		\n\t"
2136
				"pxor %%mm7, %%mm7		\n\t"
2137
				"lea (%%"REG_a", %%"REG_a", 2), %%"REG_d"\n\t"
2138
				ASMALIGN(4)
2139
				"1:				\n\t"
2140 2141 2142
				PREFETCH" 64(%0, %%"REG_d")	\n\t"
				"movd (%0, %%"REG_d"), %%mm0	\n\t"
				"movd 3(%0, %%"REG_d"), %%mm1	\n\t"
2143 2144
				"punpcklbw %%mm7, %%mm0		\n\t"
				"punpcklbw %%mm7, %%mm1		\n\t"
2145 2146
				"movd 6(%0, %%"REG_d"), %%mm2	\n\t"
				"movd 9(%0, %%"REG_d"), %%mm3	\n\t"
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
				"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"
#ifndef FAST_BGR2YV12
				"psrad $8, %%mm0		\n\t"
				"psrad $8, %%mm1		\n\t"
				"psrad $8, %%mm2		\n\t"
				"psrad $8, %%mm3		\n\t"
#endif
				"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"

2166 2167
				"movd 12(%0, %%"REG_d"), %%mm4	\n\t"
				"movd 15(%0, %%"REG_d"), %%mm1	\n\t"
2168 2169
				"punpcklbw %%mm7, %%mm4		\n\t"
				"punpcklbw %%mm7, %%mm1		\n\t"
2170 2171
				"movd 18(%0, %%"REG_d"), %%mm2	\n\t"
				"movd 21(%0, %%"REG_d"), %%mm3	\n\t"
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
				"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"
#ifndef FAST_BGR2YV12
				"psrad $8, %%mm4		\n\t"
				"psrad $8, %%mm1		\n\t"
				"psrad $8, %%mm2		\n\t"
				"psrad $8, %%mm3		\n\t"
#endif
				"packssdw %%mm1, %%mm4		\n\t"
				"packssdw %%mm3, %%mm2		\n\t"
				"pmaddwd %%mm5, %%mm4		\n\t"
				"pmaddwd %%mm5, %%mm2		\n\t"
2188
				"add $24, %%"REG_d"		\n\t"
2189 2190 2191 2192
				"packssdw %%mm2, %%mm4		\n\t"
				"psraw $7, %%mm4		\n\t"

				"packuswb %%mm4, %%mm0		\n\t"
Felix Bünemann's avatar
Felix Bünemann committed
2193
				"paddusb "MANGLE(bgr2YOffset)", %%mm0	\n\t"
2194

2195 2196
				MOVNTQ" %%mm0, (%1, %%"REG_a")	\n\t"
				"add $8, %%"REG_a"		\n\t"
2197
				" js 1b				\n\t"
2198
				: : "r" (src+width*3), "r" (ydst+width), "g" (-width)
2199
				: "%"REG_a, "%"REG_d
2200 2201 2202 2203 2204 2205
			);
			ydst += lumStride;
			src  += srcStride;
		}
		src -= srcStride*2;
		asm volatile(
2206
			"mov %4, %%"REG_a"		\n\t"
Felix Bünemann's avatar
Felix Bünemann committed
2207 2208
			"movq "MANGLE(w1111)", %%mm5		\n\t"
			"movq "MANGLE(bgr2UCoeff)", %%mm6		\n\t"
2209
			"pxor %%mm7, %%mm7		\n\t"
2210 2211
			"lea (%%"REG_a", %%"REG_a", 2), %%"REG_d"\n\t"
			"add %%"REG_d", %%"REG_d"	\n\t"
2212
			ASMALIGN(4)
2213
			"1:				\n\t"
2214 2215
			PREFETCH" 64(%0, %%"REG_d")	\n\t"
			PREFETCH" 64(%1, %%"REG_d")	\n\t"
2216
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2217 2218 2219 2220
			"movq (%0, %%"REG_d"), %%mm0	\n\t"
			"movq (%1, %%"REG_d"), %%mm1	\n\t"
			"movq 6(%0, %%"REG_d"), %%mm2	\n\t"
			"movq 6(%1, %%"REG_d"), %%mm3	\n\t"
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
			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
2232 2233 2234 2235
			"movd (%0, %%"REG_d"), %%mm0	\n\t"
			"movd (%1, %%"REG_d"), %%mm1	\n\t"
			"movd 3(%0, %%"REG_d"), %%mm2	\n\t"
			"movd 3(%1, %%"REG_d"), %%mm3	\n\t"
2236 2237 2238 2239 2240 2241 2242
			"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"
2243 2244 2245 2246
			"movd 6(%0, %%"REG_d"), %%mm4	\n\t"
			"movd 6(%1, %%"REG_d"), %%mm1	\n\t"
			"movd 9(%0, %%"REG_d"), %%mm2	\n\t"
			"movd 9(%1, %%"REG_d"), %%mm3	\n\t"
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
			"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
Felix Bünemann's avatar
Felix Bünemann committed
2257 2258
			"movq "MANGLE(bgr2VCoeff)", %%mm1		\n\t"
			"movq "MANGLE(bgr2VCoeff)", %%mm3		\n\t"
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277

			"pmaddwd %%mm0, %%mm1		\n\t"
			"pmaddwd %%mm2, %%mm3		\n\t"
			"pmaddwd %%mm6, %%mm0		\n\t"
			"pmaddwd %%mm6, %%mm2		\n\t"
#ifndef FAST_BGR2YV12
			"psrad $8, %%mm0		\n\t"
			"psrad $8, %%mm1		\n\t"
			"psrad $8, %%mm2		\n\t"
			"psrad $8, %%mm3		\n\t"
#endif
			"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"

#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2278 2279 2280 2281
			"movq 12(%0, %%"REG_d"), %%mm4	\n\t"
			"movq 12(%1, %%"REG_d"), %%mm1	\n\t"
			"movq 18(%0, %%"REG_d"), %%mm2	\n\t"
			"movq 18(%1, %%"REG_d"), %%mm3	\n\t"
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
			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
2293 2294 2295 2296
			"movd 12(%0, %%"REG_d"), %%mm4	\n\t"
			"movd 12(%1, %%"REG_d"), %%mm1	\n\t"
			"movd 15(%0, %%"REG_d"), %%mm2	\n\t"
			"movd 15(%1, %%"REG_d"), %%mm3	\n\t"
2297 2298 2299 2300 2301 2302 2303
			"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"
2304 2305 2306 2307
			"movd 18(%0, %%"REG_d"), %%mm5	\n\t"
			"movd 18(%1, %%"REG_d"), %%mm1	\n\t"
			"movd 21(%0, %%"REG_d"), %%mm2	\n\t"
			"movd 21(%1, %%"REG_d"), %%mm3	\n\t"
2308 2309 2310 2311 2312 2313 2314
			"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"
Felix Bünemann's avatar
Felix Bünemann committed
2315
			"movq "MANGLE(w1111)", %%mm5		\n\t"
2316 2317 2318
			"psrlw $2, %%mm4		\n\t"
			"psrlw $2, %%mm2		\n\t"
#endif
Felix Bünemann's avatar
Felix Bünemann committed
2319 2320
			"movq "MANGLE(bgr2VCoeff)", %%mm1		\n\t"
			"movq "MANGLE(bgr2VCoeff)", %%mm3		\n\t"
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

			"pmaddwd %%mm4, %%mm1		\n\t"
			"pmaddwd %%mm2, %%mm3		\n\t"
			"pmaddwd %%mm6, %%mm4		\n\t"
			"pmaddwd %%mm6, %%mm2		\n\t"
#ifndef FAST_BGR2YV12
			"psrad $8, %%mm4		\n\t"
			"psrad $8, %%mm1		\n\t"
			"psrad $8, %%mm2		\n\t"
			"psrad $8, %%mm3		\n\t"
#endif
			"packssdw %%mm2, %%mm4		\n\t"
			"packssdw %%mm3, %%mm1		\n\t"
			"pmaddwd %%mm5, %%mm4		\n\t"
			"pmaddwd %%mm5, %%mm1		\n\t"
2336
			"add $24, %%"REG_d"		\n\t"
2337 2338 2339 2340 2341 2342 2343
			"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"
Felix Bünemann's avatar
Felix Bünemann committed
2344
			"paddb "MANGLE(bgr2UVOffset)", %%mm0	\n\t"
2345
			"movd %%mm0, (%2, %%"REG_a")	\n\t"
2346
			"punpckhdq %%mm0, %%mm0		\n\t"
2347 2348
			"movd %%mm0, (%3, %%"REG_a")	\n\t"
			"add $4, %%"REG_a"		\n\t"
2349
			" js 1b				\n\t"
2350
			: : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth)
2351
			: "%"REG_a, "%"REG_d
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
		);

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

	asm volatile(   EMMS" \n\t"
			SFENCE" \n\t"
			:::"memory");
#else
	y=0;
#endif
	for(; y<height; y+=2)
2366
	{
2367
		long i;
2368 2369 2370 2371 2372 2373
		for(i=0; i<chromWidth; i++)
		{
			unsigned int b= src[6*i+0];
			unsigned int g= src[6*i+1];
			unsigned int r= src[6*i+2];

Michael Niedermayer's avatar
Michael Niedermayer committed
2374 2375 2376
			unsigned int Y  =  ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
			unsigned int V  =  ((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128;
			unsigned int U  =  ((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128;
Arpi's avatar
Arpi committed
2377

2378 2379 2380 2381 2382 2383 2384 2385
			udst[i] 	= U;
			vdst[i] 	= V;
			ydst[2*i] 	= Y;

			b= src[6*i+3];
			g= src[6*i+4];
			r= src[6*i+5];

Michael Niedermayer's avatar
Michael Niedermayer committed
2386
			Y  =  ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
			ydst[2*i+1] 	= Y;
		}
		ydst += lumStride;
		src  += srcStride;

		for(i=0; i<chromWidth; i++)
		{
			unsigned int b= src[6*i+0];
			unsigned int g= src[6*i+1];
			unsigned int r= src[6*i+2];

Michael Niedermayer's avatar
Michael Niedermayer committed
2398
			unsigned int Y  =  ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
2399 2400 2401 2402 2403 2404 2405

			ydst[2*i] 	= Y;

			b= src[6*i+3];
			g= src[6*i+4];
			r= src[6*i+5];

Michael Niedermayer's avatar
Michael Niedermayer committed
2406
			Y  =  ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
2407 2408 2409 2410 2411 2412 2413 2414
			ydst[2*i+1] 	= Y;
		}
		udst += chromStride;
		vdst += chromStride;
		ydst += lumStride;
		src  += srcStride;
	}
}
2415 2416

void RENAME(interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dest,
2417 2418 2419
			    long width, long height, long src1Stride,
			    long src2Stride, long dstStride){
	long h;
2420 2421 2422

	for(h=0; h < height; h++)
	{
2423
		long w;
2424 2425 2426 2427

#ifdef HAVE_MMX
#ifdef HAVE_SSE2
		asm(
2428
			"xor %%"REG_a", %%"REG_a"	\n\t"
2429
			"1:				\n\t"
2430 2431 2432 2433 2434
			PREFETCH" 64(%1, %%"REG_a")	\n\t"
			PREFETCH" 64(%2, %%"REG_a")	\n\t"
			"movdqa (%1, %%"REG_a"), %%xmm0	\n\t"
			"movdqa (%1, %%"REG_a"), %%xmm1	\n\t"
			"movdqa (%2, %%"REG_a"), %%xmm2	\n\t"
2435 2436
			"punpcklbw %%xmm2, %%xmm0	\n\t"
			"punpckhbw %%xmm2, %%xmm1	\n\t"
2437 2438 2439 2440
			"movntdq %%xmm0, (%0, %%"REG_a", 2)\n\t"
			"movntdq %%xmm1, 16(%0, %%"REG_a", 2)\n\t"
			"add $16, %%"REG_a"		\n\t"
			"cmp %3, %%"REG_a"		\n\t"
2441
			" jb 1b				\n\t"
2442
			::"r"(dest), "r"(src1), "r"(src2), "r" (width-15)
2443
			: "memory", "%"REG_a""
2444 2445 2446
		);
#else
		asm(
2447
			"xor %%"REG_a", %%"REG_a"	\n\t"
2448
			"1:				\n\t"
2449 2450 2451 2452
			PREFETCH" 64(%1, %%"REG_a")	\n\t"
			PREFETCH" 64(%2, %%"REG_a")	\n\t"
			"movq (%1, %%"REG_a"), %%mm0	\n\t"
			"movq 8(%1, %%"REG_a"), %%mm2	\n\t"
2453 2454
			"movq %%mm0, %%mm1		\n\t"
			"movq %%mm2, %%mm3		\n\t"
2455 2456
			"movq (%2, %%"REG_a"), %%mm4	\n\t"
			"movq 8(%2, %%"REG_a"), %%mm5	\n\t"
2457 2458 2459 2460
			"punpcklbw %%mm4, %%mm0		\n\t"
			"punpckhbw %%mm4, %%mm1		\n\t"
			"punpcklbw %%mm5, %%mm2		\n\t"
			"punpckhbw %%mm5, %%mm3		\n\t"
2461 2462 2463 2464 2465 2466
			MOVNTQ" %%mm0, (%0, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm1, 8(%0, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm2, 16(%0, %%"REG_a", 2)\n\t"
			MOVNTQ" %%mm3, 24(%0, %%"REG_a", 2)\n\t"
			"add $16, %%"REG_a"		\n\t"
			"cmp %3, %%"REG_a"		\n\t"
2467
			" jb 1b				\n\t"
2468
			::"r"(dest), "r"(src1), "r"(src2), "r" (width-15)
2469
			: "memory", "%"REG_a
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
		);
#endif
		for(w= (width&(~15)); w < width; w++)
		{
			dest[2*w+0] = src1[w];
			dest[2*w+1] = src2[w];
		}
#else
		for(w=0; w < width; w++)
		{
			dest[2*w+0] = src1[w];
			dest[2*w+1] = src2[w];
		}
#endif
		dest += dstStride;
                src1 += src1Stride;
                src2 += src2Stride;
	}
#ifdef HAVE_MMX
	asm(
		EMMS" \n\t"
		SFENCE" \n\t"
		::: "memory"
		);
#endif
}
2496 2497 2498

static inline void RENAME(vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
			uint8_t *dst1, uint8_t *dst2,
2499 2500 2501
			long width, long height,
			long srcStride1, long srcStride2,
			long dstStride1, long dstStride2)
2502
{
2503
    long y,x,w,h;
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
    w=width/2; h=height/2;
#ifdef HAVE_MMX
    asm volatile(
	PREFETCH" %0\n\t"
	PREFETCH" %1\n\t"
	::"m"(*(src1+srcStride1)),"m"(*(src2+srcStride2)):"memory");
#endif
    for(y=0;y<h;y++){
	const uint8_t* s1=src1+srcStride1*(y>>1);
	uint8_t* d=dst1+dstStride1*y;
	x=0;
#ifdef HAVE_MMX
2516
	for(;x<w-31;x+=32)
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
	{
	    asm volatile(
		PREFETCH" 32%1\n\t"
	        "movq	%1, %%mm0\n\t"
	        "movq	8%1, %%mm2\n\t"
	        "movq	16%1, %%mm4\n\t"
	        "movq	24%1, %%mm6\n\t"
	        "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"
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm1, 8%0\n\t"
		MOVNTQ"	%%mm2, 16%0\n\t"
		MOVNTQ"	%%mm3, 24%0\n\t"
		MOVNTQ"	%%mm4, 32%0\n\t"
		MOVNTQ"	%%mm5, 40%0\n\t"
		MOVNTQ"	%%mm6, 48%0\n\t"
		MOVNTQ"	%%mm7, 56%0"
		:"=m"(d[2*x])
		:"m"(s1[x])
		:"memory");
	}
#endif
	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;
#ifdef HAVE_MMX
2556
	for(;x<w-31;x+=32)
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
	{
	    asm volatile(
		PREFETCH" 32%1\n\t"
	        "movq	%1, %%mm0\n\t"
	        "movq	8%1, %%mm2\n\t"
	        "movq	16%1, %%mm4\n\t"
	        "movq	24%1, %%mm6\n\t"
	        "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"
		MOVNTQ"	%%mm0, %0\n\t"
		MOVNTQ"	%%mm1, 8%0\n\t"
		MOVNTQ"	%%mm2, 16%0\n\t"
		MOVNTQ"	%%mm3, 24%0\n\t"
		MOVNTQ"	%%mm4, 32%0\n\t"
		MOVNTQ"	%%mm5, 40%0\n\t"
		MOVNTQ"	%%mm6, 48%0\n\t"
		MOVNTQ"	%%mm7, 56%0"
		:"=m"(d[2*x])
		:"m"(s2[x])
		:"memory");
	}
#endif
	for(;x<w;x++) d[2*x]=d[2*x+1]=s2[x];
    }
#ifdef HAVE_MMX
	asm(
		EMMS" \n\t"
		SFENCE" \n\t"
		::: "memory"
		);
#endif
}

static inline void RENAME(yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
			uint8_t *dst,
2602 2603 2604
			long width, long height,
			long srcStride1, long srcStride2,
			long srcStride3, long dstStride)
2605
{
2606
    long y,x,w,h;
2607 2608 2609 2610 2611 2612 2613 2614
    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;
#ifdef HAVE_MMX
Michael Niedermayer's avatar
Michael Niedermayer committed
2615
	for(;x<w-7;x+=8)
2616 2617
	{
	    asm volatile(
Michael Niedermayer's avatar
Michael Niedermayer committed
2618 2619 2620 2621 2622 2623
		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 */
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
		"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*/
Michael Niedermayer's avatar
Michael Niedermayer committed
2636 2637
		MOVNTQ"	%%mm0, (%4, %0, 8)\n\t"
		MOVNTQ"	%%mm3, 8(%4, %0, 8)\n\t"
2638 2639
		
		"punpckhbw %%mm2, %%mm6\n\t" /* U2V2 U2V2 U3V3 U3V3*/
Michael Niedermayer's avatar
Michael Niedermayer committed
2640
		"movq	8(%1, %0, 4), %%mm0\n\t"
2641 2642 2643
		"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*/
Michael Niedermayer's avatar
Michael Niedermayer committed
2644 2645
		MOVNTQ"	%%mm0, 16(%4, %0, 8)\n\t"
		MOVNTQ"	%%mm3, 24(%4, %0, 8)\n\t"
2646 2647

		"movq	%%mm4, %%mm6\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2648
		"movq	16(%1, %0, 4), %%mm0\n\t"
2649 2650 2651 2652
		"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*/
Michael Niedermayer's avatar
Michael Niedermayer committed
2653 2654
		MOVNTQ"	%%mm0, 32(%4, %0, 8)\n\t"
		MOVNTQ"	%%mm3, 40(%4, %0, 8)\n\t"
2655 2656
		
		"punpckhbw %%mm5, %%mm6\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2657
		"movq	24(%1, %0, 4), %%mm0\n\t"
2658 2659 2660
		"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*/
Michael Niedermayer's avatar
Michael Niedermayer committed
2661 2662
		MOVNTQ"	%%mm0, 48(%4, %0, 8)\n\t"
		MOVNTQ"	%%mm3, 56(%4, %0, 8)\n\t"
2663

Michael Niedermayer's avatar
Michael Niedermayer committed
2664 2665
		: "+r" (x)
                : "r"(yp), "r" (up), "r"(vp), "r"(d)
2666 2667 2668
		:"memory");
	}
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
2669
	for(; x<w; x++)
2670
	{
2671
	    const long x2= x<<2;
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
	    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];
	}
    }
#ifdef HAVE_MMX
	asm(
		EMMS" \n\t"
		SFENCE" \n\t"
		::: "memory"
		);
#endif
}