• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include <inttypes.h>
27 
28 #include "libavutil/attributes.h"
29 #include "libavutil/bswap.h"
30 #include "config.h"
31 #include "rgb2rgb.h"
32 #include "swscale.h"
33 #include "swscale_internal.h"
34 
35 void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
36 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
37 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
38 void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int src_size);
39 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
40 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
41 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
42 void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
43 void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
44 
45 void (*rgb32to16)(const uint8_t *src, uint8_t *dst, int src_size);
46 void (*rgb32to15)(const uint8_t *src, uint8_t *dst, int src_size);
47 void (*rgb24to16)(const uint8_t *src, uint8_t *dst, int src_size);
48 void (*rgb24to15)(const uint8_t *src, uint8_t *dst, int src_size);
49 void (*rgb16to32)(const uint8_t *src, uint8_t *dst, int src_size);
50 void (*rgb16to15)(const uint8_t *src, uint8_t *dst, int src_size);
51 void (*rgb15to16)(const uint8_t *src, uint8_t *dst, int src_size);
52 void (*rgb15to32)(const uint8_t *src, uint8_t *dst, int src_size);
53 
54 void (*shuffle_bytes_0321)(const uint8_t *src, uint8_t *dst, int src_size);
55 void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size);
56 void (*shuffle_bytes_1230)(const uint8_t *src, uint8_t *dst, int src_size);
57 void (*shuffle_bytes_3012)(const uint8_t *src, uint8_t *dst, int src_size);
58 void (*shuffle_bytes_3210)(const uint8_t *src, uint8_t *dst, int src_size);
59 
60 
61 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
62                    const uint8_t *vsrc, uint8_t *dst,
63                    int width, int height,
64                    int lumStride, int chromStride, int dstStride);
65 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc,
66                    const uint8_t *vsrc, uint8_t *dst,
67                    int width, int height,
68                    int lumStride, int chromStride, int dstStride);
69 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
70                       const uint8_t *vsrc, uint8_t *dst,
71                       int width, int height,
72                       int lumStride, int chromStride, int dstStride);
73 void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc,
74                       const uint8_t *vsrc, uint8_t *dst,
75                       int width, int height,
76                       int lumStride, int chromStride, int dstStride);
77 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst,
78                    uint8_t *udst, uint8_t *vdst,
79                    int width, int height,
80                    int lumStride, int chromStride, int srcStride);
81 void (*ff_rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
82                        uint8_t *udst, uint8_t *vdst,
83                        int width, int height,
84                        int lumStride, int chromStride, int srcStride,
85                        int32_t *rgb2yuv);
86 void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
87                  int srcStride, int dstStride);
88 void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
89                         int width, int height, int src1Stride,
90                         int src2Stride, int dstStride);
91 void (*deinterleaveBytes)(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
92                           int width, int height, int srcStride,
93                           int dst1Stride, int dst2Stride);
94 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
95                     uint8_t *dst1, uint8_t *dst2,
96                     int width, int height,
97                     int srcStride1, int srcStride2,
98                     int dstStride1, int dstStride2);
99 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2,
100                      const uint8_t *src3, uint8_t *dst,
101                      int width, int height,
102                      int srcStride1, int srcStride2,
103                      int srcStride3, int dstStride);
104 void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
105                      const uint8_t *src, int width, int height,
106                      int lumStride, int chromStride, int srcStride);
107 void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
108                      const uint8_t *src, int width, int height,
109                      int lumStride, int chromStride, int srcStride);
110 void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
111                      const uint8_t *src, int width, int height,
112                      int lumStride, int chromStride, int srcStride);
113 void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
114                      const uint8_t *src, int width, int height,
115                      int lumStride, int chromStride, int srcStride);
116 
117 #define BY ((int)( 0.098 * (1 << RGB2YUV_SHIFT) + 0.5))
118 #define BV ((int)(-0.071 * (1 << RGB2YUV_SHIFT) + 0.5))
119 #define BU ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
120 #define GY ((int)( 0.504 * (1 << RGB2YUV_SHIFT) + 0.5))
121 #define GV ((int)(-0.368 * (1 << RGB2YUV_SHIFT) + 0.5))
122 #define GU ((int)(-0.291 * (1 << RGB2YUV_SHIFT) + 0.5))
123 #define RY ((int)( 0.257 * (1 << RGB2YUV_SHIFT) + 0.5))
124 #define RV ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
125 #define RU ((int)(-0.148 * (1 << RGB2YUV_SHIFT) + 0.5))
126 
127 //plain C versions
128 #include "rgb2rgb_template.c"
129 
130 /*
131  * RGB15->RGB16 original by Strepto/Astral
132  * ported to gcc & bugfixed : A'rpi
133  * MMXEXT, 3DNOW optimization by Nick Kurshev
134  * 32-bit C version, and and&add trick by Michael Niedermayer
135  */
136 
ff_sws_rgb2rgb_init(void)137 av_cold void ff_sws_rgb2rgb_init(void)
138 {
139     rgb2rgb_init_c();
140     if (ARCH_AARCH64)
141         rgb2rgb_init_aarch64();
142     if (ARCH_X86)
143         rgb2rgb_init_x86();
144 }
145 
rgb32to24(const uint8_t * src,uint8_t * dst,int src_size)146 void rgb32to24(const uint8_t *src, uint8_t *dst, int src_size)
147 {
148     int i, num_pixels = src_size >> 2;
149 
150     for (i = 0; i < num_pixels; i++) {
151 #if HAVE_BIGENDIAN
152         /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
153         dst[3 * i + 0] = src[4 * i + 1];
154         dst[3 * i + 1] = src[4 * i + 2];
155         dst[3 * i + 2] = src[4 * i + 3];
156 #else
157         dst[3 * i + 0] = src[4 * i + 2];
158         dst[3 * i + 1] = src[4 * i + 1];
159         dst[3 * i + 2] = src[4 * i + 0];
160 #endif
161     }
162 }
163 
rgb24to32(const uint8_t * src,uint8_t * dst,int src_size)164 void rgb24to32(const uint8_t *src, uint8_t *dst, int src_size)
165 {
166     int i;
167 
168     for (i = 0; 3 * i < src_size; i++) {
169 #if HAVE_BIGENDIAN
170         /* RGB24 (= R, G, B) -> BGR32 (= A, R, G, B) */
171         dst[4 * i + 0] = 255;
172         dst[4 * i + 1] = src[3 * i + 0];
173         dst[4 * i + 2] = src[3 * i + 1];
174         dst[4 * i + 3] = src[3 * i + 2];
175 #else
176         dst[4 * i + 0] = src[3 * i + 2];
177         dst[4 * i + 1] = src[3 * i + 1];
178         dst[4 * i + 2] = src[3 * i + 0];
179         dst[4 * i + 3] = 255;
180 #endif
181     }
182 }
183 
rgb16tobgr32(const uint8_t * src,uint8_t * dst,int src_size)184 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
185 {
186     uint8_t *d          = dst;
187     const uint16_t *s   = (const uint16_t *)src;
188     const uint16_t *end = s + src_size / 2;
189 
190     while (s < end) {
191         register uint16_t bgr = *s++;
192 #if HAVE_BIGENDIAN
193         *d++ = 255;
194         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
195         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
196         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
197 #else
198         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
199         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
200         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
201         *d++ = 255;
202 #endif
203     }
204 }
205 
rgb12to15(const uint8_t * src,uint8_t * dst,int src_size)206 void rgb12to15(const uint8_t *src, uint8_t *dst, int src_size)
207 {
208     uint16_t rgb, r, g, b;
209     uint16_t *d         = (uint16_t *)dst;
210     const uint16_t *s   = (const uint16_t *)src;
211     const uint16_t *end = s + src_size / 2;
212 
213     while (s < end) {
214         rgb  = *s++;
215         r    = rgb & 0xF00;
216         g    = rgb & 0x0F0;
217         b    = rgb & 0x00F;
218         r    = (r << 3) | ((r & 0x800) >> 1);
219         g    = (g << 2) | ((g & 0x080) >> 2);
220         b    = (b << 1) | ( b          >> 3);
221         *d++ = r | g | b;
222     }
223 }
224 
rgb16to24(const uint8_t * src,uint8_t * dst,int src_size)225 void rgb16to24(const uint8_t *src, uint8_t *dst, int src_size)
226 {
227     uint8_t *d          = dst;
228     const uint16_t *s   = (const uint16_t *)src;
229     const uint16_t *end = s + src_size / 2;
230 
231     while (s < end) {
232         register uint16_t bgr = *s++;
233         *d++ = ((bgr&0xF800)>>8) | ((bgr&0xF800)>>13);
234         *d++ = ((bgr&0x07E0)>>3) | ((bgr&0x07E0)>> 9);
235         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
236     }
237 }
238 
rgb16tobgr16(const uint8_t * src,uint8_t * dst,int src_size)239 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
240 {
241     int i, num_pixels = src_size >> 1;
242 
243     for (i = 0; i < num_pixels; i++) {
244         unsigned rgb         = ((const uint16_t *)src)[i];
245         ((uint16_t *)dst)[i] = (rgb >> 11) | (rgb & 0x7E0) | (rgb << 11);
246     }
247 }
248 
rgb16tobgr15(const uint8_t * src,uint8_t * dst,int src_size)249 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
250 {
251     int i, num_pixels = src_size >> 1;
252 
253     for (i = 0; i < num_pixels; i++) {
254         unsigned rgb         = ((const uint16_t *)src)[i];
255         ((uint16_t *)dst)[i] = (rgb >> 11) | ((rgb & 0x7C0) >> 1) | ((rgb & 0x1F) << 10);
256     }
257 }
258 
rgb15tobgr32(const uint8_t * src,uint8_t * dst,int src_size)259 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
260 {
261     uint8_t *d          = 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 #if HAVE_BIGENDIAN
268         *d++ = 255;
269         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
270         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
271         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
272 #else
273         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
274         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
275         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
276         *d++ = 255;
277 #endif
278     }
279 }
280 
rgb15to24(const uint8_t * src,uint8_t * dst,int src_size)281 void rgb15to24(const uint8_t *src, uint8_t *dst, int src_size)
282 {
283     uint8_t *d          = dst;
284     const uint16_t *s   = (const uint16_t *)src;
285     const uint16_t *end = s + src_size / 2;
286 
287     while (s < end) {
288         register uint16_t bgr = *s++;
289         *d++ = ((bgr&0x7C00)>>7) | ((bgr&0x7C00)>>12);
290         *d++ = ((bgr&0x03E0)>>2) | ((bgr&0x03E0)>> 7);
291         *d++ = ((bgr&0x001F)<<3) | ((bgr&0x001F)>> 2);
292     }
293 }
294 
rgb15tobgr16(const uint8_t * src,uint8_t * dst,int src_size)295 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
296 {
297     int i, num_pixels = src_size >> 1;
298 
299     for (i = 0; i < num_pixels; i++) {
300         unsigned rgb         = ((const uint16_t *)src)[i];
301         ((uint16_t *)dst)[i] = ((rgb & 0x7C00) >> 10) | ((rgb & 0x3E0) << 1) | (rgb << 11);
302     }
303 }
304 
rgb15tobgr15(const uint8_t * src,uint8_t * dst,int src_size)305 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
306 {
307     int i, num_pixels = src_size >> 1;
308 
309     for (i = 0; i < num_pixels; i++) {
310         unsigned rgb         = ((const uint16_t *)src)[i];
311         unsigned br          = rgb & 0x7C1F;
312         ((uint16_t *)dst)[i] = (br >> 10) | (rgb & 0x3E0) | (br << 10);
313     }
314 }
315 
rgb12tobgr12(const uint8_t * src,uint8_t * dst,int src_size)316 void rgb12tobgr12(const uint8_t *src, uint8_t *dst, int src_size)
317 {
318     uint16_t *d = (uint16_t *)dst;
319     uint16_t *s = (uint16_t *)src;
320     int i, num_pixels = src_size >> 1;
321 
322     for (i = 0; i < num_pixels; i++) {
323         unsigned rgb = s[i];
324         d[i]         = (rgb << 8 | rgb & 0xF0 | rgb >> 8) & 0xFFF;
325     }
326 }
327 
328 #define DEFINE_RGB48TOBGR48(need_bswap, swap)                           \
329 void rgb48tobgr48_ ## need_bswap(const uint8_t *src,                    \
330                                  uint8_t *dst, int src_size)            \
331 {                                                                       \
332     uint16_t *d = (uint16_t *)dst;                                      \
333     uint16_t *s = (uint16_t *)src;                                      \
334     int i, num_pixels = src_size >> 1;                                  \
335                                                                         \
336     for (i = 0; i < num_pixels; i += 3) {                               \
337         d[i    ] = swap ? av_bswap16(s[i + 2]) : s[i + 2];              \
338         d[i + 1] = swap ? av_bswap16(s[i + 1]) : s[i + 1];              \
339         d[i + 2] = swap ? av_bswap16(s[i    ]) : s[i    ];              \
340     }                                                                   \
341 }
342 
343 DEFINE_RGB48TOBGR48(nobswap, 0)
344 DEFINE_RGB48TOBGR48(bswap, 1)
345 
346 #define DEFINE_RGB64TOBGR48(need_bswap, swap)                           \
347 void rgb64tobgr48_ ## need_bswap(const uint8_t *src,                    \
348                                  uint8_t *dst, int src_size)            \
349 {                                                                       \
350     uint16_t *d = (uint16_t *)dst;                                      \
351     uint16_t *s = (uint16_t *)src;                                      \
352     int i, num_pixels = src_size >> 3;                                  \
353                                                                         \
354     for (i = 0; i < num_pixels; i++) {                                  \
355         d[3 * i    ] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2];  \
356         d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1];  \
357         d[3 * i + 2] = swap ? av_bswap16(s[4 * i    ]) : s[4 * i    ];  \
358     }                                                                   \
359 }
360 
361 DEFINE_RGB64TOBGR48(nobswap, 0)
362 DEFINE_RGB64TOBGR48(bswap, 1)
363 
364 #define DEFINE_RGB64TO48(need_bswap, swap)                              \
365 void rgb64to48_ ## need_bswap(const uint8_t *src,                       \
366                               uint8_t *dst, int src_size)               \
367 {                                                                       \
368     uint16_t *d = (uint16_t *)dst;                                      \
369     uint16_t *s = (uint16_t *)src;                                      \
370     int i, num_pixels = src_size >> 3;                                  \
371                                                                         \
372     for (i = 0; i < num_pixels; i++) {                                  \
373         d[3 * i    ] = swap ? av_bswap16(s[4 * i    ]) : s[4 * i    ];  \
374         d[3 * i + 1] = swap ? av_bswap16(s[4 * i + 1]) : s[4 * i + 1];  \
375         d[3 * i + 2] = swap ? av_bswap16(s[4 * i + 2]) : s[4 * i + 2];  \
376     }                                                                   \
377 }
378 
379 DEFINE_RGB64TO48(nobswap, 0)
380 DEFINE_RGB64TO48(bswap, 1)
381 
382 #define DEFINE_RGB48TOBGR64(need_bswap, swap)                           \
383 void rgb48tobgr64_ ## need_bswap(const uint8_t *src,                    \
384                                  uint8_t *dst, int src_size)            \
385 {                                                                       \
386     uint16_t *d = (uint16_t *)dst;                                      \
387     uint16_t *s = (uint16_t *)src;                                      \
388     int i, num_pixels = src_size / 6;                                   \
389                                                                         \
390     for (i = 0; i < num_pixels; i++) {                                  \
391         d[4 * i    ] = swap ? av_bswap16(s[3 * i + 2]) : s[3 * i + 2];  \
392         d[4 * i + 1] = swap ? av_bswap16(s[3 * i + 1]) : s[3 * i + 1];  \
393         d[4 * i + 2] = swap ? av_bswap16(s[3 * i    ]) : s[3 * i    ];  \
394         d[4 * i + 3] = 0xFFFF;                                          \
395     }                                                                   \
396 }
397 
398 DEFINE_RGB48TOBGR64(nobswap, 0)
399 DEFINE_RGB48TOBGR64(bswap, 1)
400 
401 #define DEFINE_RGB48TO64(need_bswap, swap)                              \
402 void rgb48to64_ ## need_bswap(const uint8_t *src,                       \
403                               uint8_t *dst, int src_size)               \
404 {                                                                       \
405     uint16_t *d = (uint16_t *)dst;                                      \
406     uint16_t *s = (uint16_t *)src;                                      \
407     int i, num_pixels = src_size / 6;                                   \
408                                                                         \
409     for (i = 0; i < num_pixels; i++) {                                  \
410         d[4 * i    ] = swap ? av_bswap16(s[3 * i    ]) : s[3 * i    ];  \
411         d[4 * i + 1] = swap ? av_bswap16(s[3 * i + 1]) : s[3 * i + 1];  \
412         d[4 * i + 2] = swap ? av_bswap16(s[3 * i + 2]) : s[3 * i + 2];  \
413         d[4 * i + 3] = 0xFFFF;                                          \
414     }                                                                   \
415 }
416 
417 DEFINE_RGB48TO64(nobswap, 0)
418 DEFINE_RGB48TO64(bswap, 1)
419