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 FFmpeg.
11  *
12  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
rgb24tobgr32_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb32tobgr24_c(const uint8_t * src,uint8_t * dst,int src_size)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  */
rgb15to16_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb16to15_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb32to16_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb32tobgr16_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb32to15_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb32tobgr15_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb24tobgr16_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb24to16_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb24tobgr15_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb24to15_c(const uint8_t * src,uint8_t * dst,int src_size)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 
rgb15tobgr24_c(const uint8_t * src,uint8_t * dst,int src_size)243 static inline void rgb15tobgr24_c(const uint8_t *src, uint8_t *dst,
244                                   int src_size)
245 {
246     uint8_t *d          = dst;
247     const uint16_t *s   = (const uint16_t *)src;
248     const uint16_t *end = s + src_size / 2;
249 
250     while (s < end) {
251         register uint16_t bgr = *s++;
252         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
253         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
254         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
255     }
256 }
257 
rgb16tobgr24_c(const uint8_t * src,uint8_t * dst,int src_size)258 static inline void rgb16tobgr24_c(const uint8_t *src, uint8_t *dst,
259                                   int src_size)
260 {
261     uint8_t *d          = (uint8_t *)dst;
262     const uint16_t *s   = (const uint16_t *)src;
263     const uint16_t *end = s + src_size / 2;
264 
265     while (s < end) {
266         register uint16_t bgr = *s++;
267         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
268         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
269         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
270     }
271 }
272 
rgb15to32_c(const uint8_t * src,uint8_t * dst,int src_size)273 static inline void rgb15to32_c(const uint8_t *src, uint8_t *dst, int src_size)
274 {
275     uint8_t *d          = dst;
276     const uint16_t *s   = (const uint16_t *)src;
277     const uint16_t *end = s + src_size / 2;
278 
279     while (s < end) {
280         register uint16_t bgr = *s++;
281 #if HAVE_BIGENDIAN
282         *d++ = 255;
283         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
284         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
285         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
286 #else
287         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
288         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
289         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
290         *d++ = 255;
291 #endif
292     }
293 }
294 
rgb16to32_c(const uint8_t * src,uint8_t * dst,int src_size)295 static inline void rgb16to32_c(const uint8_t *src, uint8_t *dst, int src_size)
296 {
297     uint8_t *d          = dst;
298     const uint16_t *s   = (const uint16_t *)src;
299     const uint16_t *end = s + src_size / 2;
300 
301     while (s < end) {
302         register uint16_t bgr = *s++;
303 #if HAVE_BIGENDIAN
304         *d++ = 255;
305         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
306         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
307         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
308 #else
309         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
310         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
311         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
312         *d++ = 255;
313 #endif
314     }
315 }
316 
shuffle_bytes_2103_c(const uint8_t * src,uint8_t * dst,int src_size)317 static inline void shuffle_bytes_2103_c(const uint8_t *src, uint8_t *dst,
318                                         int src_size)
319 {
320     int idx          = 15  - src_size;
321     const uint8_t *s = src - idx;
322     uint8_t *d       = dst - idx;
323 
324     for (; idx < 15; idx += 4) {
325         register unsigned v   = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
326         v                    &= 0xff00ff;
327         *(uint32_t *)&d[idx]  = (v >> 16) + g + (v << 16);
328     }
329 }
330 
shuffle_bytes_0321_c(const uint8_t * src,uint8_t * dst,int src_size)331 static inline void shuffle_bytes_0321_c(const uint8_t *src, uint8_t *dst,
332                                         int src_size)
333 {
334     int idx          = 15  - src_size;
335     const uint8_t *s = src - idx;
336     uint8_t *d       = dst - idx;
337 
338     for (; idx < 15; idx += 4) {
339         register unsigned v   = *(const uint32_t *)&s[idx], g = v & 0x00ff00ff;
340         v                    &= 0xff00ff00;
341         *(uint32_t *)&d[idx]  = (v >> 16) + g + (v << 16);
342     }
343 }
344 
345 #if !HAVE_BIGENDIAN
346 #define DEFINE_SHUFFLE_BYTES(name, a, b, c, d)                          \
347 static void shuffle_bytes_##name (const uint8_t *src,                   \
348                                         uint8_t *dst, int src_size)     \
349 {                                                                       \
350     int i;                                                              \
351                                                                         \
352     for (i = 0; i < src_size; i += 4) {                                 \
353         dst[i + 0] = src[i + a];                                        \
354         dst[i + 1] = src[i + b];                                        \
355         dst[i + 2] = src[i + c];                                        \
356         dst[i + 3] = src[i + d];                                        \
357     }                                                                   \
358 }
359 
360 DEFINE_SHUFFLE_BYTES(1230_c, 1, 2, 3, 0)
361 DEFINE_SHUFFLE_BYTES(3012_c, 3, 0, 1, 2)
362 DEFINE_SHUFFLE_BYTES(3210_c, 3, 2, 1, 0)
363 #endif
364 
rgb24tobgr24_c(const uint8_t * src,uint8_t * dst,int src_size)365 static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
366 {
367     unsigned i;
368 
369     for (i = 0; i < src_size; i += 3) {
370         register uint8_t x = src[i + 2];
371         dst[i + 1]         = src[i + 1];
372         dst[i + 2]         = src[i + 0];
373         dst[i + 0]         = x;
374     }
375 }
376 
yuvPlanartoyuy2_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride,int vertLumPerChroma)377 static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
378                                      const uint8_t *vsrc, uint8_t *dst,
379                                      int width, int height,
380                                      int lumStride, int chromStride,
381                                      int dstStride, int vertLumPerChroma)
382 {
383     int y, i;
384     const int chromWidth = width >> 1;
385 
386     for (y = 0; y < height; y++) {
387 #if HAVE_FAST_64BIT
388         uint64_t *ldst = (uint64_t *)dst;
389         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
390         for (i = 0; i < chromWidth; i += 2) {
391             uint64_t k = yc[0] + (uc[0] << 8) +
392                          (yc[1] << 16) + ((unsigned) vc[0] << 24);
393             uint64_t l = yc[2] + (uc[1] << 8) +
394                          (yc[3] << 16) + ((unsigned) vc[1] << 24);
395             *ldst++ = k + (l << 32);
396             yc     += 4;
397             uc     += 2;
398             vc     += 2;
399         }
400 
401 #else
402         int *idst = (int32_t *)dst;
403         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
404 
405         for (i = 0; i < chromWidth; i++) {
406 #if HAVE_BIGENDIAN
407             *idst++ = (yc[0] << 24) + (uc[0] << 16) +
408                       (yc[1] <<  8) + (vc[0] <<  0);
409 #else
410             *idst++ = yc[0] + (uc[0] << 8) +
411                       (yc[1] << 16) + (vc[0] << 24);
412 #endif
413             yc += 2;
414             uc++;
415             vc++;
416         }
417 #endif
418         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
419             usrc += chromStride;
420             vsrc += chromStride;
421         }
422         ysrc += lumStride;
423         dst  += dstStride;
424     }
425 }
426 
427 /**
428  * Height should be a multiple of 2 and width should be a multiple of 16.
429  * (If this is a problem for anyone then tell me, and I will fix it.)
430  */
yv12toyuy2_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride)431 static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
432                                 const uint8_t *vsrc, uint8_t *dst,
433                                 int width, int height, int lumStride,
434                                 int chromStride, int dstStride)
435 {
436     //FIXME interpolate chroma
437     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
438                       chromStride, dstStride, 2);
439 }
440 
yuvPlanartouyvy_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride,int vertLumPerChroma)441 static inline void yuvPlanartouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
442                                      const uint8_t *vsrc, uint8_t *dst,
443                                      int width, int height,
444                                      int lumStride, int chromStride,
445                                      int dstStride, int vertLumPerChroma)
446 {
447     int y, i;
448     const int chromWidth = width >> 1;
449 
450     for (y = 0; y < height; y++) {
451 #if HAVE_FAST_64BIT
452         uint64_t *ldst = (uint64_t *)dst;
453         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
454         for (i = 0; i < chromWidth; i += 2) {
455             uint64_t k = uc[0] + (yc[0] << 8) +
456                          (vc[0] << 16) + ((unsigned) yc[1] << 24);
457             uint64_t l = uc[1] + (yc[2] << 8) +
458                          (vc[1] << 16) + ((unsigned) yc[3] << 24);
459             *ldst++ = k + (l << 32);
460             yc     += 4;
461             uc     += 2;
462             vc     += 2;
463         }
464 
465 #else
466         int *idst = (int32_t *)dst;
467         const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
468 
469         for (i = 0; i < chromWidth; i++) {
470 #if HAVE_BIGENDIAN
471             *idst++ = (uc[0] << 24) + (yc[0] << 16) +
472                       (vc[0] <<  8) + (yc[1] <<  0);
473 #else
474             *idst++ = uc[0] + (yc[0] << 8) +
475                       (vc[0] << 16) + (yc[1] << 24);
476 #endif
477             yc += 2;
478             uc++;
479             vc++;
480         }
481 #endif
482         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
483             usrc += chromStride;
484             vsrc += chromStride;
485         }
486         ysrc += lumStride;
487         dst  += dstStride;
488     }
489 }
490 
491 /**
492  * Height should be a multiple of 2 and width should be a multiple of 16
493  * (If this is a problem for anyone then tell me, and I will fix it.)
494  */
yv12touyvy_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride)495 static inline void yv12touyvy_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     //FIXME interpolate chroma
501     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
502                       chromStride, dstStride, 2);
503 }
504 
505 /**
506  * Width should be a multiple of 16.
507  */
yuv422ptouyvy_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride)508 static inline void yuv422ptouyvy_c(const uint8_t *ysrc, const uint8_t *usrc,
509                                    const uint8_t *vsrc, uint8_t *dst,
510                                    int width, int height, int lumStride,
511                                    int chromStride, int dstStride)
512 {
513     yuvPlanartouyvy_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
514                       chromStride, dstStride, 1);
515 }
516 
517 /**
518  * Width should be a multiple of 16.
519  */
yuv422ptoyuy2_c(const uint8_t * ysrc,const uint8_t * usrc,const uint8_t * vsrc,uint8_t * dst,int width,int height,int lumStride,int chromStride,int dstStride)520 static inline void yuv422ptoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
521                                    const uint8_t *vsrc, uint8_t *dst,
522                                    int width, int height, int lumStride,
523                                    int chromStride, int dstStride)
524 {
525     yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
526                       chromStride, dstStride, 1);
527 }
528 
529 /**
530  * Height should be a multiple of 2 and width should be a multiple of 16.
531  * (If this is a problem for anyone then tell me, and I will fix it.)
532  */
yuy2toyv12_c(const uint8_t * src,uint8_t * ydst,uint8_t * udst,uint8_t * vdst,int width,int height,int lumStride,int chromStride,int srcStride)533 static inline void yuy2toyv12_c(const uint8_t *src, uint8_t *ydst,
534                                 uint8_t *udst, uint8_t *vdst,
535                                 int width, int height, int lumStride,
536                                 int chromStride, int srcStride)
537 {
538     int y;
539     const int chromWidth = width >> 1;
540 
541     for (y = 0; y < height; y += 2) {
542         int i;
543         for (i = 0; i < chromWidth; i++) {
544             ydst[2 * i + 0] = src[4 * i + 0];
545             udst[i]         = src[4 * i + 1];
546             ydst[2 * i + 1] = src[4 * i + 2];
547             vdst[i]         = src[4 * i + 3];
548         }
549         ydst += lumStride;
550         src  += srcStride;
551 
552         for (i = 0; i < chromWidth; i++) {
553             ydst[2 * i + 0] = src[4 * i + 0];
554             ydst[2 * i + 1] = src[4 * i + 2];
555         }
556         udst += chromStride;
557         vdst += chromStride;
558         ydst += lumStride;
559         src  += srcStride;
560     }
561 }
562 
planar2x_c(const uint8_t * src,uint8_t * dst,int srcWidth,int srcHeight,int srcStride,int dstStride)563 static inline void planar2x_c(const uint8_t *src, uint8_t *dst, int srcWidth,
564                               int srcHeight, int srcStride, int dstStride)
565 {
566     int x, y;
567 
568     dst[0] = src[0];
569 
570     // first line
571     for (x = 0; x < srcWidth - 1; x++) {
572         dst[2 * x + 1] = (3 * src[x] + src[x + 1]) >> 2;
573         dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
574     }
575     dst[2 * srcWidth - 1] = src[srcWidth - 1];
576 
577     dst += dstStride;
578 
579     for (y = 1; y < srcHeight; y++) {
580         const int mmxSize = 1;
581 
582         dst[0]         = (src[0] * 3 + src[srcStride]) >> 2;
583         dst[dstStride] = (src[0] + 3 * src[srcStride]) >> 2;
584 
585         for (x = mmxSize - 1; x < srcWidth - 1; x++) {
586             dst[2 * x + 1]             = (src[x + 0] * 3 + src[x + srcStride + 1]) >> 2;
587             dst[2 * x + dstStride + 2] = (src[x + 0] + 3 * src[x + srcStride + 1]) >> 2;
588             dst[2 * x + dstStride + 1] = (src[x + 1] + 3 * src[x + srcStride])     >> 2;
589             dst[2 * x + 2]             = (src[x + 1] * 3 + src[x + srcStride])     >> 2;
590         }
591         dst[srcWidth * 2 - 1]             = (src[srcWidth - 1] * 3 + src[srcWidth - 1 + srcStride]) >> 2;
592         dst[srcWidth * 2 - 1 + dstStride] = (src[srcWidth - 1] + 3 * src[srcWidth - 1 + srcStride]) >> 2;
593 
594         dst += dstStride * 2;
595         src += srcStride;
596     }
597 
598     // last line
599     dst[0] = src[0];
600 
601     for (x = 0; x < srcWidth - 1; x++) {
602         dst[2 * x + 1] = (src[x] * 3 + src[x + 1]) >> 2;
603         dst[2 * x + 2] = (src[x] + 3 * src[x + 1]) >> 2;
604     }
605     dst[2 * srcWidth - 1] = src[srcWidth - 1];
606 }
607 
608 /**
609  * Height should be a multiple of 2 and width should be a multiple of 16.
610  * (If this is a problem for anyone then tell me, and I will fix it.)
611  * Chrominance data is only taken from every second line, others are ignored.
612  * FIXME: Write HQ version.
613  */
uyvytoyv12_c(const uint8_t * src,uint8_t * ydst,uint8_t * udst,uint8_t * vdst,int width,int height,int lumStride,int chromStride,int srcStride)614 static inline void uyvytoyv12_c(const uint8_t *src, uint8_t *ydst,
615                                 uint8_t *udst, uint8_t *vdst,
616                                 int width, int height, int lumStride,
617                                 int chromStride, int srcStride)
618 {
619     int y;
620     const int chromWidth = width >> 1;
621 
622     for (y = 0; y < height; y += 2) {
623         int i;
624         for (i = 0; i < chromWidth; i++) {
625             udst[i]         = src[4 * i + 0];
626             ydst[2 * i + 0] = src[4 * i + 1];
627             vdst[i]         = src[4 * i + 2];
628             ydst[2 * i + 1] = src[4 * i + 3];
629         }
630         ydst += lumStride;
631         src  += srcStride;
632 
633         for (i = 0; i < chromWidth; i++) {
634             ydst[2 * i + 0] = src[4 * i + 1];
635             ydst[2 * i + 1] = src[4 * i + 3];
636         }
637         udst += chromStride;
638         vdst += chromStride;
639         ydst += lumStride;
640         src  += srcStride;
641     }
642 }
643 
644 /**
645  * Height should be a multiple of 2 and width should be a multiple of 2.
646  * (If this is a problem for anyone then tell me, and I will fix it.)
647  * Chrominance data is only taken from every second line,
648  * others are ignored in the C version.
649  * FIXME: Write HQ version.
650  */
ff_rgb24toyv12_c(const uint8_t * src,uint8_t * ydst,uint8_t * udst,uint8_t * vdst,int width,int height,int lumStride,int chromStride,int srcStride,int32_t * rgb2yuv)651 void ff_rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
652                    uint8_t *vdst, int width, int height, int lumStride,
653                    int chromStride, int srcStride, int32_t *rgb2yuv)
654 {
655     int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
656     int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
657     int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
658     int y;
659     const int chromWidth = width >> 1;
660 
661     for (y = 0; y < height; y += 2) {
662         int i;
663         for (i = 0; i < chromWidth; i++) {
664             unsigned int b = src[6 * i + 0];
665             unsigned int g = src[6 * i + 1];
666             unsigned int r = src[6 * i + 2];
667 
668             unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) +  16;
669             unsigned int V = ((rv * r + gv * g + bv * b) >> RGB2YUV_SHIFT) + 128;
670             unsigned int U = ((ru * r + gu * g + bu * b) >> RGB2YUV_SHIFT) + 128;
671 
672             udst[i]     = U;
673             vdst[i]     = V;
674             ydst[2 * i] = Y;
675 
676             b = src[6 * i + 3];
677             g = src[6 * i + 4];
678             r = src[6 * i + 5];
679 
680             Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
681             ydst[2 * i + 1] = Y;
682         }
683         ydst += lumStride;
684         src  += srcStride;
685 
686         if (y+1 == height)
687             break;
688 
689         for (i = 0; i < chromWidth; i++) {
690             unsigned int b = src[6 * i + 0];
691             unsigned int g = src[6 * i + 1];
692             unsigned int r = src[6 * i + 2];
693 
694             unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
695 
696             ydst[2 * i] = Y;
697 
698             b = src[6 * i + 3];
699             g = src[6 * i + 4];
700             r = src[6 * i + 5];
701 
702             Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
703             ydst[2 * i + 1] = Y;
704         }
705         udst += chromStride;
706         vdst += chromStride;
707         ydst += lumStride;
708         src  += srcStride;
709     }
710 }
711 
interleaveBytes_c(const uint8_t * src1,const uint8_t * src2,uint8_t * dest,int width,int height,int src1Stride,int src2Stride,int dstStride)712 static void interleaveBytes_c(const uint8_t *src1, const uint8_t *src2,
713                               uint8_t *dest, int width, int height,
714                               int src1Stride, int src2Stride, int dstStride)
715 {
716     int h;
717 
718     for (h = 0; h < height; h++) {
719         int w;
720         for (w = 0; w < width; w++) {
721             dest[2 * w + 0] = src1[w];
722             dest[2 * w + 1] = src2[w];
723         }
724         dest += dstStride;
725         src1 += src1Stride;
726         src2 += src2Stride;
727     }
728 }
729 
deinterleaveBytes_c(const uint8_t * src,uint8_t * dst1,uint8_t * dst2,int width,int height,int srcStride,int dst1Stride,int dst2Stride)730 static void deinterleaveBytes_c(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
731                                 int width, int height, int srcStride,
732                                 int dst1Stride, int dst2Stride)
733 {
734     int h;
735 
736     for (h = 0; h < height; h++) {
737         int w;
738         for (w = 0; w < width; w++) {
739             dst1[w] = src[2 * w + 0];
740             dst2[w] = src[2 * w + 1];
741         }
742         src  += srcStride;
743         dst1 += dst1Stride;
744         dst2 += dst2Stride;
745     }
746 }
747 
vu9_to_vu12_c(const uint8_t * src1,const uint8_t * src2,uint8_t * dst1,uint8_t * dst2,int width,int height,int srcStride1,int srcStride2,int dstStride1,int dstStride2)748 static inline void vu9_to_vu12_c(const uint8_t *src1, const uint8_t *src2,
749                                  uint8_t *dst1, uint8_t *dst2,
750                                  int width, int height,
751                                  int srcStride1, int srcStride2,
752                                  int dstStride1, int dstStride2)
753 {
754     int x, y;
755     int w = width  / 2;
756     int h = height / 2;
757 
758     for (y = 0; y < h; y++) {
759         const uint8_t *s1 = src1 + srcStride1 * (y >> 1);
760         uint8_t *d        = dst1 + dstStride1 *  y;
761         for (x = 0; x < w; x++)
762             d[2 * x] = d[2 * x + 1] = s1[x];
763     }
764     for (y = 0; y < h; y++) {
765         const uint8_t *s2 = src2 + srcStride2 * (y >> 1);
766         uint8_t *d        = dst2 + dstStride2 *  y;
767         for (x = 0; x < w; x++)
768             d[2 * x] = d[2 * x + 1] = s2[x];
769     }
770 }
771 
yvu9_to_yuy2_c(const uint8_t * src1,const uint8_t * src2,const uint8_t * src3,uint8_t * dst,int width,int height,int srcStride1,int srcStride2,int srcStride3,int dstStride)772 static inline void yvu9_to_yuy2_c(const uint8_t *src1, const uint8_t *src2,
773                                   const uint8_t *src3, uint8_t *dst,
774                                   int width, int height,
775                                   int srcStride1, int srcStride2,
776                                   int srcStride3, int dstStride)
777 {
778     int x, y;
779     int w = width / 2;
780     int h = height;
781 
782     for (y = 0; y < h; y++) {
783         const uint8_t *yp = src1 + srcStride1 *  y;
784         const uint8_t *up = src2 + srcStride2 * (y >> 2);
785         const uint8_t *vp = src3 + srcStride3 * (y >> 2);
786         uint8_t *d        = dst  + dstStride  *  y;
787         for (x = 0; x < w; x++) {
788             const int x2 = x << 2;
789             d[8 * x + 0] = yp[x2];
790             d[8 * x + 1] = up[x];
791             d[8 * x + 2] = yp[x2 + 1];
792             d[8 * x + 3] = vp[x];
793             d[8 * x + 4] = yp[x2 + 2];
794             d[8 * x + 5] = up[x];
795             d[8 * x + 6] = yp[x2 + 3];
796             d[8 * x + 7] = vp[x];
797         }
798     }
799 }
800 
extract_even_c(const uint8_t * src,uint8_t * dst,int count)801 static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
802 {
803     dst   +=  count;
804     src   +=  count * 2;
805     count  = -count;
806     while (count < 0) {
807         dst[count] = src[2 * count];
808         count++;
809     }
810 }
811 
extract_even2_c(const uint8_t * src,uint8_t * dst0,uint8_t * dst1,int count)812 static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
813                             int count)
814 {
815     dst0  +=  count;
816     dst1  +=  count;
817     src   +=  count * 4;
818     count  = -count;
819     while (count < 0) {
820         dst0[count] = src[4 * count + 0];
821         dst1[count] = src[4 * count + 2];
822         count++;
823     }
824 }
825 
extract_even2avg_c(const uint8_t * src0,const uint8_t * src1,uint8_t * dst0,uint8_t * dst1,int count)826 static void extract_even2avg_c(const uint8_t *src0, const uint8_t *src1,
827                                uint8_t *dst0, uint8_t *dst1, int count)
828 {
829     dst0  +=  count;
830     dst1  +=  count;
831     src0  +=  count * 4;
832     src1  +=  count * 4;
833     count  = -count;
834     while (count < 0) {
835         dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
836         dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
837         count++;
838     }
839 }
840 
extract_odd2_c(const uint8_t * src,uint8_t * dst0,uint8_t * dst1,int count)841 static void extract_odd2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
842                            int count)
843 {
844     dst0  +=  count;
845     dst1  +=  count;
846     src   +=  count * 4;
847     count  = -count;
848     src++;
849     while (count < 0) {
850         dst0[count] = src[4 * count + 0];
851         dst1[count] = src[4 * count + 2];
852         count++;
853     }
854 }
855 
extract_odd2avg_c(const uint8_t * src0,const uint8_t * src1,uint8_t * dst0,uint8_t * dst1,int count)856 static void extract_odd2avg_c(const uint8_t *src0, const uint8_t *src1,
857                               uint8_t *dst0, uint8_t *dst1, int count)
858 {
859     dst0  +=  count;
860     dst1  +=  count;
861     src0  +=  count * 4;
862     src1  +=  count * 4;
863     count  = -count;
864     src0++;
865     src1++;
866     while (count < 0) {
867         dst0[count] = (src0[4 * count + 0] + src1[4 * count + 0]) >> 1;
868         dst1[count] = (src0[4 * count + 2] + src1[4 * count + 2]) >> 1;
869         count++;
870     }
871 }
872 
yuyvtoyuv420_c(uint8_t * ydst,uint8_t * udst,uint8_t * vdst,const uint8_t * src,int width,int height,int lumStride,int chromStride,int srcStride)873 static void yuyvtoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
874                            const uint8_t *src, int width, int height,
875                            int lumStride, int chromStride, int srcStride)
876 {
877     int y;
878     const int chromWidth = AV_CEIL_RSHIFT(width, 1);
879 
880     for (y = 0; y < height; y++) {
881         extract_even_c(src, ydst, width);
882         if (y & 1) {
883             extract_odd2avg_c(src - srcStride, src, udst, vdst, chromWidth);
884             udst += chromStride;
885             vdst += chromStride;
886         }
887 
888         src  += srcStride;
889         ydst += lumStride;
890     }
891 }
892 
yuyvtoyuv422_c(uint8_t * ydst,uint8_t * udst,uint8_t * vdst,const uint8_t * src,int width,int height,int lumStride,int chromStride,int srcStride)893 static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
894                            const uint8_t *src, int width, int height,
895                            int lumStride, int chromStride, int srcStride)
896 {
897     int y;
898     const int chromWidth = AV_CEIL_RSHIFT(width, 1);
899 
900     for (y = 0; y < height; y++) {
901         extract_even_c(src, ydst, width);
902         extract_odd2_c(src, udst, vdst, chromWidth);
903 
904         src  += srcStride;
905         ydst += lumStride;
906         udst += chromStride;
907         vdst += chromStride;
908     }
909 }
910 
uyvytoyuv420_c(uint8_t * ydst,uint8_t * udst,uint8_t * vdst,const uint8_t * src,int width,int height,int lumStride,int chromStride,int srcStride)911 static void uyvytoyuv420_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
912                            const uint8_t *src, int width, int height,
913                            int lumStride, int chromStride, int srcStride)
914 {
915     int y;
916     const int chromWidth = AV_CEIL_RSHIFT(width, 1);
917 
918     for (y = 0; y < height; y++) {
919         extract_even_c(src + 1, ydst, width);
920         if (y & 1) {
921             extract_even2avg_c(src - srcStride, src, udst, vdst, chromWidth);
922             udst += chromStride;
923             vdst += chromStride;
924         }
925 
926         src  += srcStride;
927         ydst += lumStride;
928     }
929 }
930 
uyvytoyuv422_c(uint8_t * ydst,uint8_t * udst,uint8_t * vdst,const uint8_t * src,int width,int height,int lumStride,int chromStride,int srcStride)931 static void uyvytoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
932                            const uint8_t *src, int width, int height,
933                            int lumStride, int chromStride, int srcStride)
934 {
935     int y;
936     const int chromWidth = AV_CEIL_RSHIFT(width, 1);
937 
938     for (y = 0; y < height; y++) {
939         extract_even_c(src + 1, ydst, width);
940         extract_even2_c(src, udst, vdst, chromWidth);
941 
942         src  += srcStride;
943         ydst += lumStride;
944         udst += chromStride;
945         vdst += chromStride;
946     }
947 }
948 
rgb2rgb_init_c(void)949 static av_cold void rgb2rgb_init_c(void)
950 {
951     rgb15to16          = rgb15to16_c;
952     rgb15tobgr24       = rgb15tobgr24_c;
953     rgb15to32          = rgb15to32_c;
954     rgb16tobgr24       = rgb16tobgr24_c;
955     rgb16to32          = rgb16to32_c;
956     rgb16to15          = rgb16to15_c;
957     rgb24tobgr16       = rgb24tobgr16_c;
958     rgb24tobgr15       = rgb24tobgr15_c;
959     rgb24tobgr32       = rgb24tobgr32_c;
960     rgb32to16          = rgb32to16_c;
961     rgb32to15          = rgb32to15_c;
962     rgb32tobgr24       = rgb32tobgr24_c;
963     rgb24to15          = rgb24to15_c;
964     rgb24to16          = rgb24to16_c;
965     rgb24tobgr24       = rgb24tobgr24_c;
966 #if HAVE_BIGENDIAN
967     shuffle_bytes_0321 = shuffle_bytes_2103_c;
968     shuffle_bytes_2103 = shuffle_bytes_0321_c;
969 #else
970     shuffle_bytes_0321 = shuffle_bytes_0321_c;
971     shuffle_bytes_2103 = shuffle_bytes_2103_c;
972     shuffle_bytes_1230 = shuffle_bytes_1230_c;
973     shuffle_bytes_3012 = shuffle_bytes_3012_c;
974     shuffle_bytes_3210 = shuffle_bytes_3210_c;
975 #endif
976     rgb32tobgr16       = rgb32tobgr16_c;
977     rgb32tobgr15       = rgb32tobgr15_c;
978     yv12toyuy2         = yv12toyuy2_c;
979     yv12touyvy         = yv12touyvy_c;
980     yuv422ptoyuy2      = yuv422ptoyuy2_c;
981     yuv422ptouyvy      = yuv422ptouyvy_c;
982     yuy2toyv12         = yuy2toyv12_c;
983     planar2x           = planar2x_c;
984     ff_rgb24toyv12     = ff_rgb24toyv12_c;
985     interleaveBytes    = interleaveBytes_c;
986     deinterleaveBytes  = deinterleaveBytes_c;
987     vu9_to_vu12        = vu9_to_vu12_c;
988     yvu9_to_yuy2       = yvu9_to_yuy2_c;
989 
990     uyvytoyuv420       = uyvytoyuv420_c;
991     uyvytoyuv422       = uyvytoyuv422_c;
992     yuyvtoyuv420       = yuyvtoyuv420_c;
993     yuyvtoyuv422       = yuyvtoyuv422_c;
994 }
995