]> git.sesse.net Git - ffmpeg/blob - libswscale/rgb2rgb_template.c
asrc_anullsrc: do not set samplerate and channel layout explicitly
[ffmpeg] / libswscale / rgb2rgb_template.c
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
9  *
10  * This file is part of Libav.
11  *
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.
16  *
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.
21  *
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
25  */
26
27 #include <stddef.h>
28
29 static inline void rgb24tobgr32_c(const uint8_t *src, uint8_t *dst,
30                                   int src_size)
31 {
32     uint8_t *dest      = dst;
33     const uint8_t *s   = src;
34     const uint8_t *end = s + src_size;
35
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
50     }
51 }
52
53 static inline void rgb32tobgr24_c(const uint8_t *src, uint8_t *dst,
54                                   int src_size)
55 {
56     uint8_t *dest      = dst;
57     const uint8_t *s   = src;
58     const uint8_t *end = s + src_size;
59
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
74     }
75 }
76
77 /*
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
82  */
83 static inline void rgb15to16_c(const uint8_t *src, uint8_t *dst, int src_size)
84 {
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;
89
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;
95     }
96     if (s < end) {
97         register unsigned short x = *((const uint16_t *)s);
98         *((uint16_t *)d)          = (x & 0x7FFF) + (x & 0x7FE0);
99     }
100 }
101
102 static inline void rgb16to15_c(const uint8_t *src, uint8_t *dst, int src_size)
103 {
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;
108
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;
114     }
115     if (s < end) {
116         register uint16_t x = *((const uint16_t *)s);
117         *((uint16_t *)d)    = ((x >> 1) & 0x7FE0) | (x & 0x001F);
118     }
119 }
120
121 static inline void rgb32to16_c(const uint8_t *src, uint8_t *dst, int src_size)
122 {
123     uint16_t *d        = (uint16_t *)dst;
124     const uint8_t *s   = src;
125     const uint8_t *end = s + src_size;
126
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);
133     }
134 }
135
136 static inline void rgb32tobgr16_c(const uint8_t *src, uint8_t *dst,
137                                   int src_size)
138 {
139     uint16_t *d        = (uint16_t *)dst;
140     const uint8_t *s   = src;
141     const uint8_t *end = s + src_size;
142
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);
149     }
150 }
151
152 static inline void rgb32to15_c(const uint8_t *src, uint8_t *dst, int src_size)
153 {
154     uint16_t *d        = (uint16_t *)dst;
155     const uint8_t *s   = src;
156     const uint8_t *end = s + src_size;
157
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);
164     }
165 }
166
167 static inline void rgb32tobgr15_c(const uint8_t *src, uint8_t *dst,
168                                   int src_size)
169 {
170     uint16_t *d        = (uint16_t *)dst;
171     const uint8_t *s   = src;
172     const uint8_t *end = s + src_size;
173
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);
180     }
181 }
182
183 static inline void rgb24tobgr16_c(const uint8_t *src, uint8_t *dst,
184                                   int src_size)
185 {
186     uint16_t *d        = (uint16_t *)dst;
187     const uint8_t *s   = src;
188     const uint8_t *end = s + src_size;
189
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);
195     }
196 }
197
198 static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
199 {
200     uint16_t *d        = (uint16_t *)dst;
201     const uint8_t *s   = src;
202     const uint8_t *end = s + src_size;
203
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);
209     }
210 }
211
212 static inline void rgb24tobgr15_c(const uint8_t *src, uint8_t *dst,
213                                   int src_size)
214 {
215     uint16_t *d        = (uint16_t *)dst;
216     const uint8_t *s   = src;
217     const uint8_t *end = s + src_size;
218
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);
224     }
225 }
226
227 static inline void rgb24to15_c(const uint8_t *src, uint8_t *dst, int src_size)
228 {
229     uint16_t *d        = (uint16_t *)dst;
230     const uint8_t *s   = src;
231     const uint8_t *end = s + src_size;
232
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);
238     }
239 }
240
241 /*
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.
247  *
248  * The better method should be "left bit replication":
249  *
250  *  4 3 2 1 0
251  *  ---------
252  *  1 1 0 1 1
253  *
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
259  *      |
260  *  original bits
261  */
262 static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
263                                   int src_size)
264 {
265     uint8_t *d          = dst;
266     const uint16_t *s   = (const uint16_t *)src;
267     const uint16_t *end = s + src_size / 2;
268
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;
274     }
275 }
276
277 static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
278                                   int src_size)
279 {
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;
283
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;
289     }
290 }
291
292 static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
293 {
294     uint8_t *d          = dst;
295     const uint16_t *s   = (const uint16_t *)src;
296     const uint16_t *end = s + src_size / 2;
297
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
311     }
312 }
313
314 static inline void rgb16to32_c(const uint8_t *src, uint8_t *dst, int src_size)
315 {
316     uint8_t *d          = dst;
317     const uint16_t *s   = (const uint16_t *)src;
318     const uint16_t *end = s + src_size / 2;
319
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
333     }
334 }
335
336 static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
337                                         int src_size)
338 {
339     int idx          = 15  - src_size;
340     const uint8_t *s = src - idx;
341     uint8_t *d       = dst - idx;
342
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);
347     }
348 }
349
350 static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
351 {
352     unsigned i;
353
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;
359     }
360 }
361
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)
367 {
368     int y, i;
369     const int chromWidth = width >> 1;
370
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;
384         }
385
386 #else
387         int *idst = (int32_t *)dst;
388         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
389
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++;
401         }
402 #endif
403         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
404             usrc += chromStride;
405             vsrc += chromStride;
406         }
407         ysrc += lumStride;
408         dst  += dstStride;
409     }
410 }
411
412 /**
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.)
415  */
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)
420 {
421     //FIXME interpolate chroma
422     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
423                       chromStride, dstStride, 2);
424 }
425
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)
431 {
432     int y, i;
433     const int chromWidth = width >> 1;
434
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;
448         }
449
450 #else
451         int *idst = (int32_t *)dst;
452         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
453
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++;
465         }
466 #endif
467         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
468             usrc += chromStride;
469             vsrc += chromStride;
470         }
471         ysrc += lumStride;
472         dst  += dstStride;
473     }
474 }
475
476 /**
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.)
479  */
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)
484 {
485     //FIXME interpolate chroma
486     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
487                       chromStride, dstStride, 2);
488 }
489
490 /**
491  * Width should be a multiple of 16.
492  */
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)
497 {
498     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
499                       chromStride, dstStride, 1);
500 }
501
502 /**
503  * Width should be a multiple of 16.
504  */
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)
509 {
510     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
511                       chromStride, dstStride, 1);
512 }
513
514 /**
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.)
517  */
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)
522 {
523     int y;
524     const int chromWidth = width >> 1;
525
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];
533         }
534         ydst += lumStride;
535         src  += srcStride;
536
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];
540         }
541         udst += chromStride;
542         vdst += chromStride;
543         ydst += lumStride;
544         src  += srcStride;
545     }
546 }
547
548 static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
549                               int srcHeight, int srcStride, int dstStride)
550 {
551     int x, y;
552
553     dst[0] = src[0];
554
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;
559     }
560     dst[2 * srcWidth - 1] = src[srcWidth - 1];
561
562     dst += dstStride;
563
564     for (y = 1; y < srcHeight; y++) {
565         const int mmxSize = 1;
566
567         dst[0]         = (src[0] * 3 + src[srcStride]) >> 2;
568         dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
569
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;
575         }
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;
578
579         dst += dstStride * 2;
580         src += srcStride;
581     }
582
583     // last line
584     dst[0] = src[0];
585
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;
589     }
590     dst[2 * srcWidth - 1] = src[srcWidth - 1];
591 }
592
593 /**
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.
598  */
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)
603 {
604     int y;
605     const int chromWidth = width >> 1;
606
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];
614         }
615         ydst += lumStride;
616         src  += srcStride;
617
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];
621         }
622         udst += chromStride;
623         vdst += chromStride;
624         ydst += lumStride;
625         src  += srcStride;
626     }
627 }
628
629 /**
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.
635  */
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)
639 {
640     int y;
641     const int chromWidth = width >> 1;
642
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];
649
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;
653
654             udst[i]     = U;
655             vdst[i]     = V;
656             ydst[2 * i] = Y;
657
658             b = src[6 * i + 3];
659             g = src[6 * i + 4];
660             r = src[6 * i + 5];
661
662             Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
663             ydst[2 * i + 1] = Y;
664         }
665         ydst += lumStride;
666         src  += srcStride;
667
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];
672
673             unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
674
675             ydst[2 * i] = Y;
676
677             b = src[6 * i + 3];
678             g = src[6 * i + 4];
679             r = src[6 * i + 5];
680
681             Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
682             ydst[2 * i + 1] = Y;
683         }
684         udst += chromStride;
685         vdst += chromStride;
686         ydst += lumStride;
687         src  += srcStride;
688     }
689 }
690
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)
694 {
695     int h;
696
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];
702         }
703         dest += dstStride;
704         src1 += src1Stride;
705         src2 += src2Stride;
706     }
707 }
708
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)
714 {
715     int x, y;
716     int w = width  / 2;
717     int h = height / 2;
718
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];
724     }
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];
730     }
731 }
732
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)
738 {
739     int x, y;
740     int w = width / 2;
741     int h = height;
742
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];
758         }
759     }
760 }
761
762 static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
763 {
764     dst   +=  count;
765     src   +=  count * 2;
766     count  = -count;
767     while (count < 0) {
768         dst[count] = src[2 * count];
769         count++;
770     }
771 }
772
773 static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
774                             int count)
775 {
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++;
784     }
785 }
786
787 static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
788                                uint8_t *dst0, uint8_t *dst1, int count)
789 {
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++;
799     }
800 }
801
802 static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
803                            int count)
804 {
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++;
814     }
815 }
816
817 static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
818                               uint8_t *dst0, uint8_t *dst1, int count)
819 {
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++;
831     }
832 }
833
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)
837 {
838     int y;
839     const int chromWidth = -((-width) >> 1);
840
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;
847         }
848
849         src  += srcStride;
850         ydst += lumStride;
851     }
852 }
853
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)
857 {
858     int y;
859     const int chromWidth = -((-width) >> 1);
860
861     for (y = 0; y < height; y++) {
862         extract_even_c(src, ydst, width);
863         extract_odd2_c(src, udst, vdst, chromWidth);
864
865         src  += srcStride;
866         ydst += lumStride;
867         udst += chromStride;
868         vdst += chromStride;
869     }
870 }
871
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)
875 {
876     int y;
877     const int chromWidth = -((-width) >> 1);
878
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;
885         }
886
887         src  += srcStride;
888         ydst += lumStride;
889     }
890 }
891
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)
895 {
896     int y;
897     const int chromWidth = -((-width) >> 1);
898
899     for (y = 0; y < height; y++) {
900         extract_even_c(src + 1, ydst, width);
901         extract_even2_c(src, udst, vdst, chromWidth);
902
903         src  += srcStride;
904         ydst += lumStride;
905         udst += chromStride;
906         vdst += chromStride;
907     }
908 }
909
910 static inline void rgb2rgb_init_c(void)
911 {
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;
940
941     uyvytoyuv420       = uyvytoyuv420_c;
942     uyvytoyuv422       = uyvytoyuv422_c;
943     yuyvtoyuv420       = yuyvtoyuv420_c;
944     yuyvtoyuv422       = yuyvtoyuv422_c;
945 }