Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libswscale / rgb2rgb_template.c
blob3785ef9f07dc57f7ed91b4f6c0f34dc30b968a28
1 /*
2 * software RGB to RGB converter
3 * pluralize by software PAL8 to RGB converter
4 * software YUV to YUV converter
5 * software YUV to RGB converter
6 * Written by Nick Kurshev.
7 * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
8 * lot of big-endian byte order fixes by Alex Beregszaszi
10 * This file is part of Libav.
12 * Libav is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * Libav is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with Libav; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <stddef.h>
29 static inline void rgb24tobgr32_c(const uint8_t *src, uint8_t *dst,
30 int src_size)
32 uint8_t *dest = dst;
33 const uint8_t *s = src;
34 const uint8_t *end = s + src_size;
36 while (s < end) {
37 #if HAVE_BIGENDIAN
38 /* RGB24 (= R, G, B) -> RGB32 (= A, B, G, R) */
39 *dest++ = 255;
40 *dest++ = s[2];
41 *dest++ = s[1];
42 *dest++ = s[0];
43 s += 3;
44 #else
45 *dest++ = *s++;
46 *dest++ = *s++;
47 *dest++ = *s++;
48 *dest++ = 255;
49 #endif
53 static inline void rgb32tobgr24_c(const uint8_t *src, uint8_t *dst,
54 int src_size)
56 uint8_t *dest = dst;
57 const uint8_t *s = src;
58 const uint8_t *end = s + src_size;
60 while (s < end) {
61 #if HAVE_BIGENDIAN
62 /* RGB32 (= A, B, G, R) -> RGB24 (= R, G, B) */
63 s++;
64 dest[2] = *s++;
65 dest[1] = *s++;
66 dest[0] = *s++;
67 dest += 3;
68 #else
69 *dest++ = *s++;
70 *dest++ = *s++;
71 *dest++ = *s++;
72 s++;
73 #endif
78 * original by Strepto/Astral
79 * ported to gcc & bugfixed: A'rpi
80 * MMXEXT, 3DNOW optimization by Nick Kurshev
81 * 32-bit C version, and and&add trick by Michael Niedermayer
83 static inline void rgb15to16_c(const uint8_t *src, uint8_t *dst, int src_size)
85 register uint8_t *d = dst;
86 register const uint8_t *s = src;
87 register const uint8_t *end = s + src_size;
88 const uint8_t *mm_end = end - 3;
90 while (s < mm_end) {
91 register unsigned x = *((const uint32_t *)s);
92 *((uint32_t *)d) = (x & 0x7FFF7FFF) + (x & 0x7FE07FE0);
93 d += 4;
94 s += 4;
96 if (s < end) {
97 register unsigned short x = *((const uint16_t *)s);
98 *((uint16_t *)d) = (x & 0x7FFF) + (x & 0x7FE0);
102 static inline void rgb16to15_c(const uint8_t *src, uint8_t *dst, int src_size)
104 register uint8_t *d = dst;
105 register const uint8_t *s = src;
106 register const uint8_t *end = s + src_size;
107 const uint8_t *mm_end = end - 3;
109 while (s < mm_end) {
110 register uint32_t x = *((const uint32_t *)s);
111 *((uint32_t *)d) = ((x >> 1) & 0x7FE07FE0) | (x & 0x001F001F);
112 s += 4;
113 d += 4;
115 if (s < end) {
116 register uint16_t x = *((const uint16_t *)s);
117 *((uint16_t *)d) = ((x >> 1) & 0x7FE0) | (x & 0x001F);
121 static inline void rgb32to16_c(const uint8_t *src, uint8_t *dst, int src_size)
123 uint16_t *d = (uint16_t *)dst;
124 const uint8_t *s = src;
125 const uint8_t *end = s + src_size;
127 while (s < end) {
128 register int rgb = *(const uint32_t *)s;
129 s += 4;
130 *d++ = ((rgb & 0xFF) >> 3) +
131 ((rgb & 0xFC00) >> 5) +
132 ((rgb & 0xF80000) >> 8);
136 static inline void rgb32tobgr16_c(const uint8_t *src, uint8_t *dst,
137 int src_size)
139 uint16_t *d = (uint16_t *)dst;
140 const uint8_t *s = src;
141 const uint8_t *end = s + src_size;
143 while (s < end) {
144 register int rgb = *(const uint32_t *)s;
145 s += 4;
146 *d++ = ((rgb & 0xF8) << 8) +
147 ((rgb & 0xFC00) >> 5) +
148 ((rgb & 0xF80000) >> 19);
152 static inline void rgb32to15_c(const uint8_t *src, uint8_t *dst, int src_size)
154 uint16_t *d = (uint16_t *)dst;
155 const uint8_t *s = src;
156 const uint8_t *end = s + src_size;
158 while (s < end) {
159 register int rgb = *(const uint32_t *)s;
160 s += 4;
161 *d++ = ((rgb & 0xFF) >> 3) +
162 ((rgb & 0xF800) >> 6) +
163 ((rgb & 0xF80000) >> 9);
167 static inline void rgb32tobgr15_c(const uint8_t *src, uint8_t *dst,
168 int src_size)
170 uint16_t *d = (uint16_t *)dst;
171 const uint8_t *s = src;
172 const uint8_t *end = s + src_size;
174 while (s < end) {
175 register int rgb = *(const uint32_t *)s;
176 s += 4;
177 *d++ = ((rgb & 0xF8) << 7) +
178 ((rgb & 0xF800) >> 6) +
179 ((rgb & 0xF80000) >> 19);
183 static inline void rgb24tobgr16_c(const uint8_t *src, uint8_t *dst,
184 int src_size)
186 uint16_t *d = (uint16_t *)dst;
187 const uint8_t *s = src;
188 const uint8_t *end = s + src_size;
190 while (s < end) {
191 const int b = *s++;
192 const int g = *s++;
193 const int r = *s++;
194 *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
198 static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
200 uint16_t *d = (uint16_t *)dst;
201 const uint8_t *s = src;
202 const uint8_t *end = s + src_size;
204 while (s < end) {
205 const int r = *s++;
206 const int g = *s++;
207 const int b = *s++;
208 *d++ = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
212 static inline void rgb24tobgr15_c(const uint8_t *src, uint8_t *dst,
213 int src_size)
215 uint16_t *d = (uint16_t *)dst;
216 const uint8_t *s = src;
217 const uint8_t *end = s + src_size;
219 while (s < end) {
220 const int b = *s++;
221 const int g = *s++;
222 const int r = *s++;
223 *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
227 static inline void rgb24to15_c(const uint8_t *src, uint8_t *dst, int src_size)
229 uint16_t *d = (uint16_t *)dst;
230 const uint8_t *s = src;
231 const uint8_t *end = s + src_size;
233 while (s < end) {
234 const int r = *s++;
235 const int g = *s++;
236 const int b = *s++;
237 *d++ = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
242 * I use less accurate approximation here by simply left-shifting the input
243 * value and filling the low order bits with zeroes. This method improves PNG
244 * compression but this scheme cannot reproduce white exactly, since it does
245 * not generate an all-ones maximum value; the net effect is to darken the
246 * image slightly.
248 * The better method should be "left bit replication":
250 * 4 3 2 1 0
251 * ---------
252 * 1 1 0 1 1
254 * 7 6 5 4 3 2 1 0
255 * ----------------
256 * 1 1 0 1 1 1 1 0
257 * |=======| |===|
258 * | leftmost bits repeated to fill open bits
260 * original bits
262 static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
263 int src_size)
265 uint8_t *d = dst;
266 const uint16_t *s = (const uint16_t *)src;
267 const uint16_t *end = s + src_size / 2;
269 while (s < end) {
270 register uint16_t bgr = *s++;
271 *d++ = (bgr & 0x1F) << 3;
272 *d++ = (bgr & 0x3E0) >> 2;
273 *d++ = (bgr & 0x7C00) >> 7;
277 static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
278 int src_size)
280 uint8_t *d = (uint8_t *)dst;
281 const uint16_t *s = (const uint16_t *)src;
282 const uint16_t *end = s + src_size / 2;
284 while (s < end) {
285 register uint16_t bgr = *s++;
286 *d++ = (bgr & 0x1F) << 3;
287 *d++ = (bgr & 0x7E0) >> 3;
288 *d++ = (bgr & 0xF800) >> 8;
292 static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
294 uint8_t *d = dst;
295 const uint16_t *s = (const uint16_t *)src;
296 const uint16_t *end = s + src_size / 2;
298 while (s < end) {
299 register uint16_t bgr = *s++;
300 #if HAVE_BIGENDIAN
301 *d++ = 255;
302 *d++ = (bgr & 0x7C00) >> 7;
303 *d++ = (bgr & 0x3E0) >> 2;
304 *d++ = (bgr & 0x1F) << 3;
305 #else
306 *d++ = (bgr & 0x1F) << 3;
307 *d++ = (bgr & 0x3E0) >> 2;
308 *d++ = (bgr & 0x7C00) >> 7;
309 *d++ = 255;
310 #endif
314 static inline void rgb16to32_c(const uint8_t *src, uint8_t *dst, int src_size)
316 uint8_t *d = dst;
317 const uint16_t *s = (const uint16_t *)src;
318 const uint16_t *end = s + src_size / 2;
320 while (s < end) {
321 register uint16_t bgr = *s++;
322 #if HAVE_BIGENDIAN
323 *d++ = 255;
324 *d++ = (bgr & 0xF800) >> 8;
325 *d++ = (bgr & 0x7E0) >> 3;
326 *d++ = (bgr & 0x1F) << 3;
327 #else
328 *d++ = (bgr & 0x1F) << 3;
329 *d++ = (bgr & 0x7E0) >> 3;
330 *d++ = (bgr & 0xF800) >> 8;
331 *d++ = 255;
332 #endif
336 static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
337 int src_size)
339 int idx = 15 - src_size;
340 const uint8_t *s = src - idx;
341 uint8_t *d = dst - idx;
343 for (; idx < 15; idx += 4) {
344 register int v = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
345 v &= 0xff00ff;
346 *(uint32_t *)&d[idx] = (v >> 16) + g + (v << 16);
350 static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
352 unsigned i;
354 for (i = 0; i < src_size; i += 3) {
355 register uint8_t x = src[i + 2];
356 dst[i + 1] = src[i + 1];
357 dst[i + 2] = src[i + 0];
358 dst[i + 0] = x;
362 static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
363 const uint8_t *vsrc, uint8_t *dst,
364 int width, int height,
365 int lumStride, int chromStride,
366 int dstStride, int vertLumPerChroma)
368 int y, i;
369 const int chromWidth = width >> 1;
371 for (y = 0; y < height; y++) {
372 #if HAVE_FAST_64BIT
373 uint64_t *ldst = (uint64_t *)dst;
374 const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
375 for (i = 0; i < chromWidth; i += 2) {
376 uint64_t k = yc[0] + (uc[0] << 8) +
377 (yc[1] << 16) + (vc[0] << 24);
378 uint64_t l = yc[2] + (uc[1] << 8) +
379 (yc[3] << 16) + (vc[1] << 24);
380 *ldst++ = k + (l << 32);
381 yc += 4;
382 uc += 2;
383 vc += 2;
386 #else
387 int *idst = (int32_t *)dst;
388 const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
390 for (i = 0; i < chromWidth; i++) {
391 #if HAVE_BIGENDIAN
392 *idst++ = (yc[0] << 24) + (uc[0] << 16) +
393 (yc[1] << 8) + (vc[0] << 0);
394 #else
395 *idst++ = yc[0] + (uc[0] << 8) +
396 (yc[1] << 16) + (vc[0] << 24);
397 #endif
398 yc += 2;
399 uc++;
400 vc++;
402 #endif
403 if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
404 usrc += chromStride;
405 vsrc += chromStride;
407 ysrc += lumStride;
408 dst += dstStride;
413 * Height should be a multiple of 2 and width should be a multiple of 16.
414 * (If this is a problem for anyone then tell me, and I will fix it.)
416 static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
417 const uint8_t *vsrc, uint8_t *dst,
418 int width, int height, int lumStride,
419 int chromStride, int dstStride)
421 //FIXME interpolate chroma
422 yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
423 chromStride, dstStride, 2);
426 static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
427 const uint8_t *vsrc, uint8_t *dst,
428 int width, int height,
429 int lumStride, int chromStride,
430 int dstStride, int vertLumPerChroma)
432 int y, i;
433 const int chromWidth = width >> 1;
435 for (y = 0; y < height; y++) {
436 #if HAVE_FAST_64BIT
437 uint64_t *ldst = (uint64_t *)dst;
438 const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
439 for (i = 0; i < chromWidth; i += 2) {
440 uint64_t k = uc[0] + (yc[0] << 8) +
441 (vc[0] << 16) + (yc[1] << 24);
442 uint64_t l = uc[1] + (yc[2] << 8) +
443 (vc[1] << 16) + (yc[3] << 24);
444 *ldst++ = k + (l << 32);
445 yc += 4;
446 uc += 2;
447 vc += 2;
450 #else
451 int *idst = (int32_t *)dst;
452 const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
454 for (i = 0; i < chromWidth; i++) {
455 #if HAVE_BIGENDIAN
456 *idst++ = (uc[0] << 24) + (yc[0] << 16) +
457 (vc[0] << 8) + (yc[1] << 0);
458 #else
459 *idst++ = uc[0] + (yc[0] << 8) +
460 (vc[0] << 16) + (yc[1] << 24);
461 #endif
462 yc += 2;
463 uc++;
464 vc++;
466 #endif
467 if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
468 usrc += chromStride;
469 vsrc += chromStride;
471 ysrc += lumStride;
472 dst += dstStride;
477 * Height should be a multiple of 2 and width should be a multiple of 16
478 * (If this is a problem for anyone then tell me, and I will fix it.)
480 static inline void yv12touyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
481 const uint8_t *vsrc, uint8_t *dst,
482 int width, int height, int lumStride,
483 int chromStride, int dstStride)
485 //FIXME interpolate chroma
486 yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
487 chromStride, dstStride, 2);
491 * Width should be a multiple of 16.
493 static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
494 const uint8_t *vsrc, uint8_t *dst,
495 int width, int height, int lumStride,
496 int chromStride, int dstStride)
498 yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
499 chromStride, dstStride, 1);
503 * Width should be a multiple of 16.
505 static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
506 const uint8_t *vsrc, uint8_t *dst,
507 int width, int height, int lumStride,
508 int chromStride, int dstStride)
510 yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
511 chromStride, dstStride, 1);
515 * Height should be a multiple of 2 and width should be a multiple of 16.
516 * (If this is a problem for anyone then tell me, and I will fix it.)
518 static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
519 uint8_t *udst, uint8_t *vdst,
520 int width, int height, int lumStride,
521 int chromStride, int srcStride)
523 int y;
524 const int chromWidth = width >> 1;
526 for (y = 0; y < height; y += 2) {
527 int i;
528 for (i = 0; i < chromWidth; i++) {
529 ydst[2 * i + 0] = src[4 * i + 0];
530 udst[i] = src[4 * i + 1];
531 ydst[2 * i + 1] = src[4 * i + 2];
532 vdst[i] = src[4 * i + 3];
534 ydst += lumStride;
535 src += srcStride;
537 for (i = 0; i < chromWidth; i++) {
538 ydst[2 * i + 0] = src[4 * i + 0];
539 ydst[2 * i + 1] = src[4 * i + 2];
541 udst += chromStride;
542 vdst += chromStride;
543 ydst += lumStride;
544 src += srcStride;
548 static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
549 int srcHeight, int srcStride, int dstStride)
551 int x, y;
553 dst[0] = src[0];
555 // first line
556 for (x = 0; x < srcWidth - 1; x++) {
557 dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
558 dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
560 dst[2 * srcWidth - 1] = src[srcWidth - 1];
562 dst += dstStride;
564 for (y = 1; y < srcHeight; y++) {
565 const int mmxSize = 1;
567 dst[0] = (src[0] * 3 + src[srcStride]) >> 2;
568 dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
570 for (x = mmxSize - 1; x < srcWidth - 1; x++) {
571 dst[2 * x + 1] = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
572 dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
573 dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride]) >> 2;
574 dst[2 * x + 2] = (src[x + 1] * 3 + src[x + srcStride]) >> 2;
576 dst[srcWidth * 2 - 1] = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
577 dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
579 dst += dstStride * 2;
580 src += srcStride;
583 // last line
584 dst[0] = src[0];
586 for (x = 0; x < srcWidth - 1; x++) {
587 dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
588 dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
590 dst[2 * srcWidth - 1] = src[srcWidth - 1];
594 * Height should be a multiple of 2 and width should be a multiple of 16.
595 * (If this is a problem for anyone then tell me, and I will fix it.)
596 * Chrominance data is only taken from every second line, others are ignored.
597 * FIXME: Write HQ version.
599 static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
600 uint8_t *udst, uint8_t *vdst,
601 int width, int height, int lumStride,
602 int chromStride, int srcStride)
604 int y;
605 const int chromWidth = width >> 1;
607 for (y = 0; y < height; y += 2) {
608 int i;
609 for (i = 0; i < chromWidth; i++) {
610 udst[i] = src[4 * i + 0];
611 ydst[2 * i + 0] = src[4 * i + 1];
612 vdst[i] = src[4 * i + 2];
613 ydst[2 * i + 1] = src[4 * i + 3];
615 ydst += lumStride;
616 src += srcStride;
618 for (i = 0; i < chromWidth; i++) {
619 ydst[2 * i + 0] = src[4 * i + 1];
620 ydst[2 * i + 1] = src[4 * i + 3];
622 udst += chromStride;
623 vdst += chromStride;
624 ydst += lumStride;
625 src += srcStride;
630 * Height should be a multiple of 2 and width should be a multiple of 2.
631 * (If this is a problem for anyone then tell me, and I will fix it.)
632 * Chrominance data is only taken from every second line,
633 * others are ignored in the C version.
634 * FIXME: Write HQ version.
636 void rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
637 uint8_t *vdst, int width, int height, int lumStride,
638 int chromStride, int srcStride)
640 int y;
641 const int chromWidth = width >> 1;
643 for (y = 0; y < height; y += 2) {
644 int i;
645 for (i = 0; i < chromWidth; i++) {
646 unsigned int b = src[6 * i + 0];
647 unsigned int g = src[6 * i + 1];
648 unsigned int r = src[6 * i + 2];
650 unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
651 unsigned int V = ((RV * r + GV * g + BV * b) >> RGB2YUV_SHIFT) + 128;
652 unsigned int U = ((RU * r + GU * g + BU * b) >> RGB2YUV_SHIFT) + 128;
654 udst[i] = U;
655 vdst[i] = V;
656 ydst[2 * i] = Y;
658 b = src[6 * i + 3];
659 g = src[6 * i + 4];
660 r = src[6 * i + 5];
662 Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
663 ydst[2 * i + 1] = Y;
665 ydst += lumStride;
666 src += srcStride;
668 for (i = 0; i < chromWidth; i++) {
669 unsigned int b = src[6 * i + 0];
670 unsigned int g = src[6 * i + 1];
671 unsigned int r = src[6 * i + 2];
673 unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
675 ydst[2 * i] = Y;
677 b = src[6 * i + 3];
678 g = src[6 * i + 4];
679 r = src[6 * i + 5];
681 Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
682 ydst[2 * i + 1] = Y;
684 udst += chromStride;
685 vdst += chromStride;
686 ydst += lumStride;
687 src += srcStride;
691 static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
692 uint8_t *dest, int width, int height,
693 int src1Stride, int src2Stride, int dstStride)
695 int h;
697 for (h = 0; h < height; h++) {
698 int w;
699 for (w = 0; w < width; w++) {
700 dest[2 * w + 0] = src1[w];
701 dest[2 * w + 1] = src2[w];
703 dest += dstStride;
704 src1 += src1Stride;
705 src2 += src2Stride;
709 static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
710 uint8_t *dst1, uint8_t *dst2,
711 int width, int height,
712 int srcStride1, int srcStride2,
713 int dstStride1, int dstStride2)
715 int x, y;
716 int w = width / 2;
717 int h = height / 2;
719 for (y = 0; y < h; y++) {
720 const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
721 uint8_t *d = dst1 + dstStride1 * y;
722 for (x = 0; x < w; x++)
723 d[2 * x] = d[2 * x + 1] = s1[x];
725 for (y = 0; y < h; y++) {
726 const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
727 uint8_t *d = dst2 + dstStride2 * y;
728 for (x = 0; x < w; x++)
729 d[2 * x] = d[2 * x + 1] = s2[x];
733 static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
734 const uint8_t *src3, uint8_t *dst,
735 int width, int height,
736 int srcStride1, int srcStride2,
737 int srcStride3, int dstStride)
739 int x, y;
740 int w = width / 2;
741 int h = height;
743 for (y = 0; y < h; y++) {
744 const uint8_t *yp = src1 + srcStride1 * y;
745 const uint8_t *up = src2 + srcStride2 * (y >> 2);
746 const uint8_t *vp = src3 + srcStride3 * (y >> 2);
747 uint8_t *d = dst + dstStride * y;
748 for (x = 0; x < w; x++) {
749 const int x2 = x << 2;
750 d[8 * x + 0] = yp[x2];
751 d[8 * x + 1] = up[x];
752 d[8 * x + 2] = yp[x2 + 1];
753 d[8 * x + 3] = vp[x];
754 d[8 * x + 4] = yp[x2 + 2];
755 d[8 * x + 5] = up[x];
756 d[8 * x + 6] = yp[x2 + 3];
757 d[8 * x + 7] = vp[x];
762 static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
764 dst += count;
765 src += count * 2;
766 count = -count;
767 while (count < 0) {
768 dst[count] = src[2 * count];
769 count++;
773 static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
774 int count)
776 dst0 += count;
777 dst1 += count;
778 src += count * 4;
779 count = -count;
780 while (count < 0) {
781 dst0[count] = src[4 * count + 0];
782 dst1[count] = src[4 * count + 2];
783 count++;
787 static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
788 uint8_t *dst0, uint8_t *dst1, int count)
790 dst0 += count;
791 dst1 += count;
792 src0 += count * 4;
793 src1 += count * 4;
794 count = -count;
795 while (count < 0) {
796 dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
797 dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
798 count++;
802 static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
803 int count)
805 dst0 += count;
806 dst1 += count;
807 src += count * 4;
808 count = -count;
809 src++;
810 while (count < 0) {
811 dst0[count] = src[4 * count + 0];
812 dst1[count] = src[4 * count + 2];
813 count++;
817 static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
818 uint8_t *dst0, uint8_t *dst1, int count)
820 dst0 += count;
821 dst1 += count;
822 src0 += count * 4;
823 src1 += count * 4;
824 count = -count;
825 src0++;
826 src1++;
827 while (count < 0) {
828 dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
829 dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
830 count++;
834 static void yuyvtoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
835 const uint8_t *src, int width, int height,
836 int lumStride, int chromStride, int srcStride)
838 int y;
839 const int chromWidth = -((-width) >> 1);
841 for (y = 0; y < height; y++) {
842 extract_even_c(src, ydst, width);
843 if (y & 1) {
844 extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
845 udst += chromStride;
846 vdst += chromStride;
849 src += srcStride;
850 ydst += lumStride;
854 static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
855 const uint8_t *src, int width, int height,
856 int lumStride, int chromStride, int srcStride)
858 int y;
859 const int chromWidth = -((-width) >> 1);
861 for (y = 0; y < height; y++) {
862 extract_even_c(src, ydst, width);
863 extract_odd2_c(src, udst, vdst, chromWidth);
865 src += srcStride;
866 ydst += lumStride;
867 udst += chromStride;
868 vdst += chromStride;
872 static void uyvytoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
873 const uint8_t *src, int width, int height,
874 int lumStride, int chromStride, int srcStride)
876 int y;
877 const int chromWidth = -((-width) >> 1);
879 for (y = 0; y < height; y++) {
880 extract_even_c(src + 1, ydst, width);
881 if (y & 1) {
882 extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
883 udst += chromStride;
884 vdst += chromStride;
887 src += srcStride;
888 ydst += lumStride;
892 static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
893 const uint8_t *src, int width, int height,
894 int lumStride, int chromStride, int srcStride)
896 int y;
897 const int chromWidth = -((-width) >> 1);
899 for (y = 0; y < height; y++) {
900 extract_even_c(src + 1, ydst, width);
901 extract_even2_c(src, udst, vdst, chromWidth);
903 src += srcStride;
904 ydst += lumStride;
905 udst += chromStride;
906 vdst += chromStride;
910 static inline void rgb2rgb_init_c(void)
912 rgb15to16 = rgb15to16_c;
913 rgb15tobgr24 = rgb15tobgr24_c;
914 rgb15to32 = rgb15to32_c;
915 rgb16tobgr24 = rgb16tobgr24_c;
916 rgb16to32 = rgb16to32_c;
917 rgb16to15 = rgb16to15_c;
918 rgb24tobgr16 = rgb24tobgr16_c;
919 rgb24tobgr15 = rgb24tobgr15_c;
920 rgb24tobgr32 = rgb24tobgr32_c;
921 rgb32to16 = rgb32to16_c;
922 rgb32to15 = rgb32to15_c;
923 rgb32tobgr24 = rgb32tobgr24_c;
924 rgb24to15 = rgb24to15_c;
925 rgb24to16 = rgb24to16_c;
926 rgb24tobgr24 = rgb24tobgr24_c;
927 shuffle_bytes_2103 = shuffle_bytes_2103_c;
928 rgb32tobgr16 = rgb32tobgr16_c;
929 rgb32tobgr15 = rgb32tobgr15_c;
930 yv12toyuy2 = yv12toyuy2_c;
931 yv12touyvy = yv12touyvy_c;
932 yuv422ptoyuy2 = yuv422ptoyuy2_c;
933 yuv422ptouyvy = yuv422ptouyvy_c;
934 yuy2toyv12 = yuy2toyv12_c;
935 planar2x = planar2x_c;
936 rgb24toyv12 = rgb24toyv12_c;
937 interleaveBytes = interleaveBytes_c;
938 vu9_to_vu12 = vu9_to_vu12_c;
939 yvu9_to_yuy2 = yvu9_to_yuy2_c;
941 uyvytoyuv420 = uyvytoyuv420_c;
942 uyvytoyuv422 = uyvytoyuv422_c;
943 yuyvtoyuv420 = yuyvtoyuv420_c;
944 yuyvtoyuv422 = yuyvtoyuv422_c;