]> git.sesse.net Git - ffmpeg/blob - libswscale/rgb2rgb_template.c
swscale: fix sign extensions in yuv planar conversion
[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 #include "libavutil/attributes.h"
30
31 static inline void rgb24tobgr32_c(const uint8_t *src, uint8_t *dst,
32                                   int src_size)
33 {
34     uint8_t *dest      = dst;
35     const uint8_t *s   = src;
36     const uint8_t *end = s + src_size;
37
38     while (s < end) {
39 #if HAVE_BIGENDIAN
40         /* RGB24 (= R, G, B) -> RGB32 (= A, B, G, R) */
41         *dest++  = 255;
42         *dest++  = s[2];
43         *dest++  = s[1];
44         *dest++  = s[0];
45         s       += 3;
46 #else
47         *dest++  = *s++;
48         *dest++  = *s++;
49         *dest++  = *s++;
50         *dest++  = 255;
51 #endif
52     }
53 }
54
55 static inline void rgb32tobgr24_c(const uint8_t *src, uint8_t *dst,
56                                   int src_size)
57 {
58     uint8_t *dest      = dst;
59     const uint8_t *s   = src;
60     const uint8_t *end = s + src_size;
61
62     while (s < end) {
63 #if HAVE_BIGENDIAN
64         /* RGB32 (= A, B, G, R) -> RGB24 (= R, G, B) */
65         s++;
66         dest[2]  = *s++;
67         dest[1]  = *s++;
68         dest[0]  = *s++;
69         dest    += 3;
70 #else
71         *dest++  = *s++;
72         *dest++  = *s++;
73         *dest++  = *s++;
74         s++;
75 #endif
76     }
77 }
78
79 /*
80  * original by Strepto/Astral
81  * ported to gcc & bugfixed: A'rpi
82  * MMXEXT, 3DNOW optimization by Nick Kurshev
83  * 32-bit C version, and and&add trick by Michael Niedermayer
84  */
85 static inline void rgb15to16_c(const uint8_t *src, uint8_t *dst, int src_size)
86 {
87     register uint8_t *d         = dst;
88     register const uint8_t *s   = src;
89     register const uint8_t *end = s + src_size;
90     const uint8_t *mm_end       = end - 3;
91
92     while (s < mm_end) {
93         register unsigned x = *((const uint32_t *)s);
94         *((uint32_t *)d)    = (x & 0x7FFF7FFF) + (x & 0x7FE07FE0);
95         d += 4;
96         s += 4;
97     }
98     if (s < end) {
99         register unsigned short x = *((const uint16_t *)s);
100         *((uint16_t *)d)          = (x & 0x7FFF) + (x & 0x7FE0);
101     }
102 }
103
104 static inline void rgb16to15_c(const uint8_t *src, uint8_t *dst, int src_size)
105 {
106     register uint8_t *d         = dst;
107     register const uint8_t *s   = src;
108     register const uint8_t *end = s + src_size;
109     const uint8_t *mm_end       = end - 3;
110
111     while (s < mm_end) {
112         register uint32_t x  = *((const uint32_t *)s);
113         *((uint32_t *)d)     = ((x >> 1) & 0x7FE07FE0) | (x & 0x001F001F);
114         s                   += 4;
115         d                   += 4;
116     }
117     if (s < end) {
118         register uint16_t x = *((const uint16_t *)s);
119         *((uint16_t *)d)    = ((x >> 1) & 0x7FE0) | (x & 0x001F);
120     }
121 }
122
123 static inline void rgb32to16_c(const uint8_t *src, uint8_t *dst, int src_size)
124 {
125     uint16_t *d        = (uint16_t *)dst;
126     const uint8_t *s   = src;
127     const uint8_t *end = s + src_size;
128
129     while (s < end) {
130         register int rgb  = *(const uint32_t *)s;
131         s                += 4;
132         *d++              = ((rgb & 0xFF)     >> 3) +
133                             ((rgb & 0xFC00)   >> 5) +
134                             ((rgb & 0xF80000) >> 8);
135     }
136 }
137
138 static inline void rgb32tobgr16_c(const uint8_t *src, uint8_t *dst,
139                                   int src_size)
140 {
141     uint16_t *d        = (uint16_t *)dst;
142     const uint8_t *s   = src;
143     const uint8_t *end = s + src_size;
144
145     while (s < end) {
146         register int rgb  = *(const uint32_t *)s;
147         s                += 4;
148         *d++              = ((rgb & 0xF8)     << 8) +
149                             ((rgb & 0xFC00)   >> 5) +
150                             ((rgb & 0xF80000) >> 19);
151     }
152 }
153
154 static inline void rgb32to15_c(const uint8_t *src, uint8_t *dst, int src_size)
155 {
156     uint16_t *d        = (uint16_t *)dst;
157     const uint8_t *s   = src;
158     const uint8_t *end = s + src_size;
159
160     while (s < end) {
161         register int rgb  = *(const uint32_t *)s;
162         s                += 4;
163         *d++              = ((rgb & 0xFF)     >> 3) +
164                             ((rgb & 0xF800)   >> 6) +
165                             ((rgb & 0xF80000) >> 9);
166     }
167 }
168
169 static inline void rgb32tobgr15_c(const uint8_t *src, uint8_t *dst,
170                                   int src_size)
171 {
172     uint16_t *d        = (uint16_t *)dst;
173     const uint8_t *s   = src;
174     const uint8_t *end = s + src_size;
175
176     while (s < end) {
177         register int rgb  = *(const uint32_t *)s;
178         s                += 4;
179         *d++              = ((rgb & 0xF8)     <<  7) +
180                             ((rgb & 0xF800)   >>  6) +
181                             ((rgb & 0xF80000) >> 19);
182     }
183 }
184
185 static inline void rgb24tobgr16_c(const uint8_t *src, uint8_t *dst,
186                                   int src_size)
187 {
188     uint16_t *d        = (uint16_t *)dst;
189     const uint8_t *s   = src;
190     const uint8_t *end = s + src_size;
191
192     while (s < end) {
193         const int b = *s++;
194         const int g = *s++;
195         const int r = *s++;
196         *d++        = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
197     }
198 }
199
200 static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
201 {
202     uint16_t *d        = (uint16_t *)dst;
203     const uint8_t *s   = src;
204     const uint8_t *end = s + src_size;
205
206     while (s < end) {
207         const int r = *s++;
208         const int g = *s++;
209         const int b = *s++;
210         *d++        = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
211     }
212 }
213
214 static inline void rgb24tobgr15_c(const uint8_t *src, uint8_t *dst,
215                                   int src_size)
216 {
217     uint16_t *d        = (uint16_t *)dst;
218     const uint8_t *s   = src;
219     const uint8_t *end = s + src_size;
220
221     while (s < end) {
222         const int b = *s++;
223         const int g = *s++;
224         const int r = *s++;
225         *d++        = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
226     }
227 }
228
229 static inline void rgb24to15_c(const uint8_t *src, uint8_t *dst, int src_size)
230 {
231     uint16_t *d        = (uint16_t *)dst;
232     const uint8_t *s   = src;
233     const uint8_t *end = s + src_size;
234
235     while (s < end) {
236         const int r = *s++;
237         const int g = *s++;
238         const int b = *s++;
239         *d++        = (b >> 3) | ((g & 0xF8) << 2) | ((r & 0xF8) << 7);
240     }
241 }
242
243 /*
244  * I use less accurate approximation here by simply left-shifting the input
245  * value and filling the low order bits with zeroes. This method improves PNG
246  * compression but this scheme cannot reproduce white exactly, since it does
247  * not generate an all-ones maximum value; the net effect is to darken the
248  * image slightly.
249  *
250  * The better method should be "left bit replication":
251  *
252  *  4 3 2 1 0
253  *  ---------
254  *  1 1 0 1 1
255  *
256  *  7 6 5 4 3  2 1 0
257  *  ----------------
258  *  1 1 0 1 1  1 1 0
259  *  |=======|  |===|
260  *      |      leftmost bits repeated to fill open bits
261  *      |
262  *  original bits
263  */
264 static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
265                                   int src_size)
266 {
267     uint8_t *d          = dst;
268     const uint16_t *s   = (const uint16_t *)src;
269     const uint16_t *end = s + src_size / 2;
270
271     while (s < end) {
272         register uint16_t bgr = *s++;
273         *d++ = (bgr & 0x1F)   << 3;
274         *d++ = (bgr & 0x3E0)  >> 2;
275         *d++ = (bgr & 0x7C00) >> 7;
276     }
277 }
278
279 static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
280                                   int src_size)
281 {
282     uint8_t *d          = (uint8_t *)dst;
283     const uint16_t *s   = (const uint16_t *)src;
284     const uint16_t *end = s + src_size / 2;
285
286     while (s < end) {
287         register uint16_t bgr = *s++;
288         *d++ = (bgr & 0x1F)   << 3;
289         *d++ = (bgr & 0x7E0)  >> 3;
290         *d++ = (bgr & 0xF800) >> 8;
291     }
292 }
293
294 static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
295 {
296     uint8_t *d          = dst;
297     const uint16_t *s   = (const uint16_t *)src;
298     const uint16_t *end = s + src_size / 2;
299
300     while (s < end) {
301         register uint16_t bgr = *s++;
302 #if HAVE_BIGENDIAN
303         *d++ = 255;
304         *d++ = (bgr & 0x7C00) >> 7;
305         *d++ = (bgr & 0x3E0)  >> 2;
306         *d++ = (bgr & 0x1F)   << 3;
307 #else
308         *d++ = (bgr & 0x1F)   << 3;
309         *d++ = (bgr & 0x3E0)  >> 2;
310         *d++ = (bgr & 0x7C00) >> 7;
311         *d++ = 255;
312 #endif
313     }
314 }
315
316 static inline void rgb16to32_c(const uint8_t *src, uint8_t *dst, int src_size)
317 {
318     uint8_t *d          = dst;
319     const uint16_t *s   = (const uint16_t *)src;
320     const uint16_t *end = s + src_size / 2;
321
322     while (s < end) {
323         register uint16_t bgr = *s++;
324 #if HAVE_BIGENDIAN
325         *d++ = 255;
326         *d++ = (bgr & 0xF800) >> 8;
327         *d++ = (bgr & 0x7E0)  >> 3;
328         *d++ = (bgr & 0x1F)   << 3;
329 #else
330         *d++ = (bgr & 0x1F)   << 3;
331         *d++ = (bgr & 0x7E0)  >> 3;
332         *d++ = (bgr & 0xF800) >> 8;
333         *d++ = 255;
334 #endif
335     }
336 }
337
338 static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
339                                         int src_size)
340 {
341     int idx          = 15  - src_size;
342     const uint8_t *s = src - idx;
343     uint8_t *d       = dst - idx;
344
345     for (; idx < 15; idx += 4) {
346         register int v        = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
347         v                    &= 0xff00ff;
348         *(uint32_t *)&d[idx]  = (v >> 16) + g + (v << 16);
349     }
350 }
351
352 static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
353 {
354     unsigned i;
355
356     for (i = 0; i < src_size; i += 3) {
357         register uint8_t x = src[i + 2];
358         dst[i + 1]         = src[i + 1];
359         dst[i + 2]         = src[i + 0];
360         dst[i + 0]         = x;
361     }
362 }
363
364 static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
365                                      const uint8_t *vsrc, uint8_t *dst,
366                                      int width, int height,
367                                      int lumStride, int chromStride,
368                                      int dstStride, int vertLumPerChroma)
369 {
370     int y, i;
371     const int chromWidth = width >> 1;
372
373     for (y = 0; y < height; y++) {
374 #if HAVE_FAST_64BIT
375         uint64_t *ldst = (uint64_t *)dst;
376         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
377         for (i = 0; i < chromWidth; i += 2) {
378             uint64_t k = yc[0] + (uc[0] << 8) +
379                          (yc[1] << 16) + ((unsigned) vc[0] << 24);
380             uint64_t l = yc[2] + (uc[1] << 8) +
381                          (yc[3] << 16) + ((unsigned) vc[1] << 24);
382             *ldst++ = k + (l << 32);
383             yc     += 4;
384             uc     += 2;
385             vc     += 2;
386         }
387
388 #else
389         int *idst = (int32_t *)dst;
390         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
391
392         for (i = 0; i < chromWidth; i++) {
393 #if HAVE_BIGENDIAN
394             *idst++ = (yc[0] << 24) + (uc[0] << 16) +
395                       (yc[1] <<  8) + (vc[0] <<  0);
396 #else
397             *idst++ = yc[0] + (uc[0] << 8) +
398                       (yc[1] << 16) + (vc[0] << 24);
399 #endif
400             yc += 2;
401             uc++;
402             vc++;
403         }
404 #endif
405         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
406             usrc += chromStride;
407             vsrc += chromStride;
408         }
409         ysrc += lumStride;
410         dst  += dstStride;
411     }
412 }
413
414 /**
415  * Height should be a multiple of 2 and width should be a multiple of 16.
416  * (If this is a problem for anyone then tell me, and I will fix it.)
417  */
418 static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
419                                 const uint8_t *vsrc, uint8_t *dst,
420                                 int width, int height, int lumStride,
421                                 int chromStride, int dstStride)
422 {
423     //FIXME interpolate chroma
424     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
425                       chromStride, dstStride, 2);
426 }
427
428 static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
429                                      const uint8_t *vsrc, uint8_t *dst,
430                                      int width, int height,
431                                      int lumStride, int chromStride,
432                                      int dstStride, int vertLumPerChroma)
433 {
434     int y, i;
435     const int chromWidth = width >> 1;
436
437     for (y = 0; y < height; y++) {
438 #if HAVE_FAST_64BIT
439         uint64_t *ldst = (uint64_t *)dst;
440         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
441         for (i = 0; i < chromWidth; i += 2) {
442             uint64_t k = uc[0] + (yc[0] << 8) +
443                          (vc[0] << 16) + ((unsigned) yc[1] << 24);
444             uint64_t l = uc[1] + (yc[2] << 8) +
445                          (vc[1] << 16) + ((unsigned) yc[3] << 24);
446             *ldst++ = k + (l << 32);
447             yc     += 4;
448             uc     += 2;
449             vc     += 2;
450         }
451
452 #else
453         int *idst = (int32_t *)dst;
454         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
455
456         for (i = 0; i < chromWidth; i++) {
457 #if HAVE_BIGENDIAN
458             *idst++ = (uc[0] << 24) + (yc[0] << 16) +
459                       (vc[0] <<  8) + (yc[1] <<  0);
460 #else
461             *idst++ = uc[0] + (yc[0] << 8) +
462                       (vc[0] << 16) + (yc[1] << 24);
463 #endif
464             yc += 2;
465             uc++;
466             vc++;
467         }
468 #endif
469         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
470             usrc += chromStride;
471             vsrc += chromStride;
472         }
473         ysrc += lumStride;
474         dst  += dstStride;
475     }
476 }
477
478 /**
479  * Height should be a multiple of 2 and width should be a multiple of 16
480  * (If this is a problem for anyone then tell me, and I will fix it.)
481  */
482 static inline void yv12touyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
483                                 const uint8_t *vsrc, uint8_t *dst,
484                                 int width, int height, int lumStride,
485                                 int chromStride, int dstStride)
486 {
487     //FIXME interpolate chroma
488     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
489                       chromStride, dstStride, 2);
490 }
491
492 /**
493  * Width should be a multiple of 16.
494  */
495 static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
496                                    const uint8_t *vsrc, uint8_t *dst,
497                                    int width, int height, int lumStride,
498                                    int chromStride, int dstStride)
499 {
500     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
501                       chromStride, dstStride, 1);
502 }
503
504 /**
505  * Width should be a multiple of 16.
506  */
507 static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
508                                    const uint8_t *vsrc, uint8_t *dst,
509                                    int width, int height, int lumStride,
510                                    int chromStride, int dstStride)
511 {
512     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
513                       chromStride, dstStride, 1);
514 }
515
516 /**
517  * Height should be a multiple of 2 and width should be a multiple of 16.
518  * (If this is a problem for anyone then tell me, and I will fix it.)
519  */
520 static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
521                                 uint8_t *udst, uint8_t *vdst,
522                                 int width, int height, int lumStride,
523                                 int chromStride, int srcStride)
524 {
525     int y;
526     const int chromWidth = width >> 1;
527
528     for (y = 0; y < height; y += 2) {
529         int i;
530         for (i = 0; i < chromWidth; i++) {
531             ydst[2 * i + 0] = src[4 * i + 0];
532             udst[i]         = src[4 * i + 1];
533             ydst[2 * i + 1] = src[4 * i + 2];
534             vdst[i]         = src[4 * i + 3];
535         }
536         ydst += lumStride;
537         src  += srcStride;
538
539         for (i = 0; i < chromWidth; i++) {
540             ydst[2 * i + 0] = src[4 * i + 0];
541             ydst[2 * i + 1] = src[4 * i + 2];
542         }
543         udst += chromStride;
544         vdst += chromStride;
545         ydst += lumStride;
546         src  += srcStride;
547     }
548 }
549
550 static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
551                               int srcHeight, int srcStride, int dstStride)
552 {
553     int x, y;
554
555     dst[0] = src[0];
556
557     // first line
558     for (x = 0; x < srcWidth - 1; x++) {
559         dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
560         dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
561     }
562     dst[2 * srcWidth - 1] = src[srcWidth - 1];
563
564     dst += dstStride;
565
566     for (y = 1; y < srcHeight; y++) {
567         const int mmxSize = 1;
568
569         dst[0]         = (src[0] * 3 + src[srcStride]) >> 2;
570         dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
571
572         for (x = mmxSize - 1; x < srcWidth - 1; x++) {
573             dst[2 * x + 1]             = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
574             dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
575             dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride])     >> 2;
576             dst[2 * x + 2]             = (src[x + 1] * 3 + src[x + srcStride])     >> 2;
577         }
578         dst[srcWidth * 2 - 1]             = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
579         dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
580
581         dst += dstStride * 2;
582         src += srcStride;
583     }
584
585     // last line
586     dst[0] = src[0];
587
588     for (x = 0; x < srcWidth - 1; x++) {
589         dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
590         dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
591     }
592     dst[2 * srcWidth - 1] = src[srcWidth - 1];
593 }
594
595 /**
596  * Height should be a multiple of 2 and width should be a multiple of 16.
597  * (If this is a problem for anyone then tell me, and I will fix it.)
598  * Chrominance data is only taken from every second line, others are ignored.
599  * FIXME: Write HQ version.
600  */
601 static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
602                                 uint8_t *udst, uint8_t *vdst,
603                                 int width, int height, int lumStride,
604                                 int chromStride, int srcStride)
605 {
606     int y;
607     const int chromWidth = width >> 1;
608
609     for (y = 0; y < height; y += 2) {
610         int i;
611         for (i = 0; i < chromWidth; i++) {
612             udst[i]         = src[4 * i + 0];
613             ydst[2 * i + 0] = src[4 * i + 1];
614             vdst[i]         = src[4 * i + 2];
615             ydst[2 * i + 1] = src[4 * i + 3];
616         }
617         ydst += lumStride;
618         src  += srcStride;
619
620         for (i = 0; i < chromWidth; i++) {
621             ydst[2 * i + 0] = src[4 * i + 1];
622             ydst[2 * i + 1] = src[4 * i + 3];
623         }
624         udst += chromStride;
625         vdst += chromStride;
626         ydst += lumStride;
627         src  += srcStride;
628     }
629 }
630
631 /**
632  * Height should be a multiple of 2 and width should be a multiple of 2.
633  * (If this is a problem for anyone then tell me, and I will fix it.)
634  * Chrominance data is only taken from every second line,
635  * others are ignored in the C version.
636  * FIXME: Write HQ version.
637  */
638 void rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
639                    uint8_t *vdst, int width, int height, int lumStride,
640                    int chromStride, int srcStride)
641 {
642     int y;
643     const int chromWidth = width >> 1;
644
645     for (y = 0; y < height; y += 2) {
646         int i;
647         for (i = 0; i < chromWidth; i++) {
648             unsigned int b = src[6 * i + 0];
649             unsigned int g = src[6 * i + 1];
650             unsigned int r = src[6 * i + 2];
651
652             unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) +  16;
653             unsigned int V = ((RV * r + GV * g + BV * b) >> RGB2YUV_SHIFT) + 128;
654             unsigned int U = ((RU * r + GU * g + BU * b) >> RGB2YUV_SHIFT) + 128;
655
656             udst[i]     = U;
657             vdst[i]     = V;
658             ydst[2 * i] = Y;
659
660             b = src[6 * i + 3];
661             g = src[6 * i + 4];
662             r = src[6 * i + 5];
663
664             Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
665             ydst[2 * i + 1] = Y;
666         }
667         ydst += lumStride;
668         src  += srcStride;
669
670         for (i = 0; i < chromWidth; i++) {
671             unsigned int b = src[6 * i + 0];
672             unsigned int g = src[6 * i + 1];
673             unsigned int r = src[6 * i + 2];
674
675             unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
676
677             ydst[2 * i] = Y;
678
679             b = src[6 * i + 3];
680             g = src[6 * i + 4];
681             r = src[6 * i + 5];
682
683             Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
684             ydst[2 * i + 1] = Y;
685         }
686         udst += chromStride;
687         vdst += chromStride;
688         ydst += lumStride;
689         src  += srcStride;
690     }
691 }
692
693 static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
694                               uint8_t *dest, int width, int height,
695                               int src1Stride, int src2Stride, int dstStride)
696 {
697     int h;
698
699     for (h = 0; h < height; h++) {
700         int w;
701         for (w = 0; w < width; w++) {
702             dest[2 * w + 0] = src1[w];
703             dest[2 * w + 1] = src2[w];
704         }
705         dest += dstStride;
706         src1 += src1Stride;
707         src2 += src2Stride;
708     }
709 }
710
711 static void deinterleaveBytes_c(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
712                                 int width, int height, int srcStride,
713                                 int dst1Stride, int dst2Stride)
714 {
715     int h;
716
717     for (h = 0; h < height; h++) {
718         int w;
719         for (w = 0; w < width; w++) {
720             dst1[w] = src[2 * w + 0];
721             dst2[w] = src[2 * w + 1];
722         }
723         src  += srcStride;
724         dst1 += dst1Stride;
725         dst2 += dst2Stride;
726     }
727 }
728
729 static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
730                                  uint8_t *dst1, uint8_t *dst2,
731                                  int width, int height,
732                                  int srcStride1, int srcStride2,
733                                  int dstStride1, int dstStride2)
734 {
735     int x, y;
736     int w = width  / 2;
737     int h = height / 2;
738
739     for (y = 0; y < h; y++) {
740         const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
741         uint8_t *d        = dst1 + dstStride1 *  y;
742         for (x = 0; x < w; x++)
743             d[2 * x] = d[2 * x + 1] = s1[x];
744     }
745     for (y = 0; y < h; y++) {
746         const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
747         uint8_t *d        = dst2 + dstStride2 *  y;
748         for (x = 0; x < w; x++)
749             d[2 * x] = d[2 * x + 1] = s2[x];
750     }
751 }
752
753 static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
754                                   const uint8_t *src3, uint8_t *dst,
755                                   int width, int height,
756                                   int srcStride1, int srcStride2,
757                                   int srcStride3, int dstStride)
758 {
759     int x, y;
760     int w = width / 2;
761     int h = height;
762
763     for (y = 0; y < h; y++) {
764         const uint8_t *yp = src1 + srcStride1 *  y;
765         const uint8_t *up = src2 + srcStride2 * (y >> 2);
766         const uint8_t *vp = src3 + srcStride3 * (y >> 2);
767         uint8_t *d        = dst  + dstStride  *  y;
768         for (x = 0; x < w; x++) {
769             const int x2 = x << 2;
770             d[8 * x + 0] = yp[x2];
771             d[8 * x + 1] = up[x];
772             d[8 * x + 2] = yp[x2 + 1];
773             d[8 * x + 3] = vp[x];
774             d[8 * x + 4] = yp[x2 + 2];
775             d[8 * x + 5] = up[x];
776             d[8 * x + 6] = yp[x2 + 3];
777             d[8 * x + 7] = vp[x];
778         }
779     }
780 }
781
782 static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
783 {
784     dst   +=  count;
785     src   +=  count * 2;
786     count  = -count;
787     while (count < 0) {
788         dst[count] = src[2 * count];
789         count++;
790     }
791 }
792
793 static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
794                             int count)
795 {
796     dst0  +=  count;
797     dst1  +=  count;
798     src   +=  count * 4;
799     count  = -count;
800     while (count < 0) {
801         dst0[count] = src[4 * count + 0];
802         dst1[count] = src[4 * count + 2];
803         count++;
804     }
805 }
806
807 static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
808                                uint8_t *dst0, uint8_t *dst1, int count)
809 {
810     dst0  +=  count;
811     dst1  +=  count;
812     src0  +=  count * 4;
813     src1  +=  count * 4;
814     count  = -count;
815     while (count < 0) {
816         dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
817         dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
818         count++;
819     }
820 }
821
822 static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
823                            int count)
824 {
825     dst0  +=  count;
826     dst1  +=  count;
827     src   +=  count * 4;
828     count  = -count;
829     src++;
830     while (count < 0) {
831         dst0[count] = src[4 * count + 0];
832         dst1[count] = src[4 * count + 2];
833         count++;
834     }
835 }
836
837 static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
838                               uint8_t *dst0, uint8_t *dst1, int count)
839 {
840     dst0  +=  count;
841     dst1  +=  count;
842     src0  +=  count * 4;
843     src1  +=  count * 4;
844     count  = -count;
845     src0++;
846     src1++;
847     while (count < 0) {
848         dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
849         dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
850         count++;
851     }
852 }
853
854 static void yuyvtoyuv420_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         if (y & 1) {
864             extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
865             udst += chromStride;
866             vdst += chromStride;
867         }
868
869         src  += srcStride;
870         ydst += lumStride;
871     }
872 }
873
874 static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
875                            const uint8_t *src, int width, int height,
876                            int lumStride, int chromStride, int srcStride)
877 {
878     int y;
879     const int chromWidth = -((-width) >> 1);
880
881     for (y = 0; y < height; y++) {
882         extract_even_c(src, ydst, width);
883         extract_odd2_c(src, udst, vdst, chromWidth);
884
885         src  += srcStride;
886         ydst += lumStride;
887         udst += chromStride;
888         vdst += chromStride;
889     }
890 }
891
892 static void uyvytoyuv420_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         if (y & 1) {
902             extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
903             udst += chromStride;
904             vdst += chromStride;
905         }
906
907         src  += srcStride;
908         ydst += lumStride;
909     }
910 }
911
912 static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
913                            const uint8_t *src, int width, int height,
914                            int lumStride, int chromStride, int srcStride)
915 {
916     int y;
917     const int chromWidth = -((-width) >> 1);
918
919     for (y = 0; y < height; y++) {
920         extract_even_c(src + 1, ydst, width);
921         extract_even2_c(src, udst, vdst, chromWidth);
922
923         src  += srcStride;
924         ydst += lumStride;
925         udst += chromStride;
926         vdst += chromStride;
927     }
928 }
929
930 static av_cold void rgb2rgb_init_c(void)
931 {
932     rgb15to16          = rgb15to16_c;
933     rgb15tobgr24       = rgb15tobgr24_c;
934     rgb15to32          = rgb15to32_c;
935     rgb16tobgr24       = rgb16tobgr24_c;
936     rgb16to32          = rgb16to32_c;
937     rgb16to15          = rgb16to15_c;
938     rgb24tobgr16       = rgb24tobgr16_c;
939     rgb24tobgr15       = rgb24tobgr15_c;
940     rgb24tobgr32       = rgb24tobgr32_c;
941     rgb32to16          = rgb32to16_c;
942     rgb32to15          = rgb32to15_c;
943     rgb32tobgr24       = rgb32tobgr24_c;
944     rgb24to15          = rgb24to15_c;
945     rgb24to16          = rgb24to16_c;
946     rgb24tobgr24       = rgb24tobgr24_c;
947     shuffle_bytes_2103 = shuffle_bytes_2103_c;
948     rgb32tobgr16       = rgb32tobgr16_c;
949     rgb32tobgr15       = rgb32tobgr15_c;
950     yv12toyuy2         = yv12toyuy2_c;
951     yv12touyvy         = yv12touyvy_c;
952     yuv422ptoyuy2      = yuv422ptoyuy2_c;
953     yuv422ptouyvy      = yuv422ptouyvy_c;
954     yuy2toyv12         = yuy2toyv12_c;
955     planar2x           = planar2x_c;
956     rgb24toyv12        = rgb24toyv12_c;
957     interleaveBytes    = interleaveBytes_c;
958     deinterleaveBytes  = deinterleaveBytes_c;
959     vu9_to_vu12        = vu9_to_vu12_c;
960     yvu9_to_yuy2       = yvu9_to_yuy2_c;
961
962     uyvytoyuv420       = uyvytoyuv420_c;
963     uyvytoyuv422       = uyvytoyuv422_c;
964     yuyvtoyuv420       = yuyvtoyuv420_c;
965     yuyvtoyuv422       = yuyvtoyuv422_c;
966 }