2 * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/avutil.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
36 #include "swscale_internal.h"
38 #define RGB2YUV_SHIFT 15
39 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
40 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
41 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
42 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
43 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
44 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
45 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
46 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
47 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
49 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
51 #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
52 #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
54 static av_always_inline void
55 rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
56 enum PixelFormat origin)
59 for (i = 0; i < width; i++) {
60 unsigned int r_b = input_pixel(&src[i*3+0]);
61 unsigned int g = input_pixel(&src[i*3+1]);
62 unsigned int b_r = input_pixel(&src[i*3+2]);
64 dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
68 static av_always_inline void
69 rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
70 const uint16_t *src1, const uint16_t *src2,
71 int width, enum PixelFormat origin)
75 for (i = 0; i < width; i++) {
76 int r_b = input_pixel(&src1[i*3+0]);
77 int g = input_pixel(&src1[i*3+1]);
78 int b_r = input_pixel(&src1[i*3+2]);
80 dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
81 dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
85 static av_always_inline void
86 rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
87 const uint16_t *src1, const uint16_t *src2,
88 int width, enum PixelFormat origin)
92 for (i = 0; i < width; i++) {
93 int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
94 int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
95 int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
97 dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
98 dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
106 #define rgb48funcs(pattern, BE_LE, origin) \
107 static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused0, const uint8_t *unused1,\
108 int width, uint32_t *unused) \
110 const uint16_t *src = (const uint16_t *) _src; \
111 uint16_t *dst = (uint16_t *) _dst; \
112 rgb48ToY_c_template(dst, src, width, origin); \
115 static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
116 const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
117 int width, uint32_t *unused) \
119 const uint16_t *src1 = (const uint16_t *) _src1, \
120 *src2 = (const uint16_t *) _src2; \
121 uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
122 rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
125 static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
126 const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
127 int width, uint32_t *unused) \
129 const uint16_t *src1 = (const uint16_t *) _src1, \
130 *src2 = (const uint16_t *) _src2; \
131 uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
132 rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
135 rgb48funcs(rgb, LE, PIX_FMT_RGB48LE)
136 rgb48funcs(rgb, BE, PIX_FMT_RGB48BE)
137 rgb48funcs(bgr, LE, PIX_FMT_BGR48LE)
138 rgb48funcs(bgr, BE, PIX_FMT_BGR48BE)
140 #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
141 origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
142 (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
144 static av_always_inline void
145 rgb16_32ToY_c_template(int16_t *dst, const uint8_t *src,
146 int width, enum PixelFormat origin,
147 int shr, int shg, int shb, int shp,
148 int maskr, int maskg, int maskb,
149 int rsh, int gsh, int bsh, int S)
151 const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh;
152 const unsigned rnd = (32<<((S)-1)) + (1<<(S-7));
155 for (i = 0; i < width; i++) {
156 int px = input_pixel(i) >> shp;
157 int b = (px & maskb) >> shb;
158 int g = (px & maskg) >> shg;
159 int r = (px & maskr) >> shr;
161 dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
165 static av_always_inline void
166 rgb16_32ToUV_c_template(int16_t *dstU, int16_t *dstV,
167 const uint8_t *src, int width,
168 enum PixelFormat origin,
169 int shr, int shg, int shb, int shp,
170 int maskr, int maskg, int maskb,
171 int rsh, int gsh, int bsh, int S)
173 const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
174 rv = RV << rsh, gv = GV << gsh, bv = BV << bsh;
175 const unsigned rnd = (256u<<((S)-1)) + (1<<(S-7));
178 for (i = 0; i < width; i++) {
179 int px = input_pixel(i) >> shp;
180 int b = (px & maskb) >> shb;
181 int g = (px & maskg) >> shg;
182 int r = (px & maskr) >> shr;
184 dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
185 dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
189 static av_always_inline void
190 rgb16_32ToUV_half_c_template(int16_t *dstU, int16_t *dstV,
191 const uint8_t *src, int width,
192 enum PixelFormat origin,
193 int shr, int shg, int shb, int shp,
194 int maskr, int maskg, int maskb,
195 int rsh, int gsh, int bsh, int S)
197 const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
198 rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
199 maskgx = ~(maskr | maskb);
200 const unsigned rnd = (256U<<(S)) + (1<<(S-6));
203 maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
204 for (i = 0; i < width; i++) {
205 int px0 = input_pixel(2 * i + 0) >> shp;
206 int px1 = input_pixel(2 * i + 1) >> shp;
207 int b, r, g = (px0 & maskgx) + (px1 & maskgx);
208 int rb = px0 + px1 - g;
210 b = (rb & maskb) >> shb;
211 if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
212 origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
215 g = (g & maskg) >> shg;
217 r = (rb & maskr) >> shr;
219 dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
220 dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
226 #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
227 maskg, maskb, rsh, gsh, bsh, S) \
228 static void name ## ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, \
229 int width, uint32_t *unused) \
231 rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, \
232 shr, shg, shb, shp, \
233 maskr, maskg, maskb, rsh, gsh, bsh, S); \
236 static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
237 const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
238 int width, uint32_t *unused) \
240 rgb16_32ToUV_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
241 shr, shg, shb, shp, \
242 maskr, maskg, maskb, rsh, gsh, bsh, S); \
245 static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
246 const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
247 int width, uint32_t *unused) \
249 rgb16_32ToUV_half_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
250 shr, shg, shb, shp, \
251 maskr, maskg, maskb, rsh, gsh, bsh, S); \
254 rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8)
255 rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8)
256 rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8)
257 rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8)
258 rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8)
259 rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7)
260 rgb16_32_wrapper(PIX_FMT_BGR444LE, bgr12le, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT+4)
261 rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8)
262 rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7)
263 rgb16_32_wrapper(PIX_FMT_RGB444LE, rgb12le, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT+4)
264 rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8)
265 rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7)
266 rgb16_32_wrapper(PIX_FMT_BGR444BE, bgr12be, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT+4)
267 rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8)
268 rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7)
269 rgb16_32_wrapper(PIX_FMT_RGB444BE, rgb12be, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT+4)
271 static void gbr24pToUV_half_c(uint16_t *dstU, uint16_t *dstV,
272 const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
273 int width, enum PixelFormat origin)
276 for (i=0; i<width; i++) {
277 unsigned int g = gsrc[2*i] + gsrc[2*i+1];
278 unsigned int b = bsrc[2*i] + bsrc[2*i+1];
279 unsigned int r = rsrc[2*i] + rsrc[2*i+1];
281 dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
282 dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
286 static void abgrToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
289 for (i=0; i<width; i++) {
294 static void rgbaToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
297 for (i=0; i<width; i++) {
298 dst[i]= src[4*i+3]<<6;
302 static void palToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *pal)
305 for (i=0; i<width; i++) {
308 dst[i]= (pal[d] >> 24)<<6;
312 static void palToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, long width, uint32_t *pal)
315 for (i=0; i<width; i++) {
318 dst[i]= (pal[d] & 0xFF)<<6;
322 static void palToUV_c(uint16_t *dstU, int16_t *dstV,
323 const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
324 int width, uint32_t *pal)
327 assert(src1 == src2);
328 for (i=0; i<width; i++) {
331 dstU[i]= (uint8_t)(p>> 8)<<6;
332 dstV[i]= (uint8_t)(p>>16)<<6;
336 static void monowhite2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
339 for (i=0; i<width/8; i++) {
342 dst[8*i+j]= ((d>>(7-j))&1)*16383;
346 for(j=0; j<(width&7); j++)
347 dst[8*i+j]= ((d>>(7-j))&1)*16383;
351 static void monoblack2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
354 for (i=0; i<width/8; i++) {
357 dst[8*i+j]= ((d>>(7-j))&1)*16383;
361 for(j=0; j<(width&7); j++)
362 dst[8*i+j]= ((d>>(7-j))&1)*16383;
366 static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
370 for (i=0; i<width; i++)
374 static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
375 const uint8_t *src2, int width, uint32_t *unused)
378 for (i=0; i<width; i++) {
379 dstU[i]= src1[4*i + 1];
380 dstV[i]= src1[4*i + 3];
382 assert(src1 == src2);
385 static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
388 const uint16_t *src = (const uint16_t *) _src;
389 uint16_t *dst = (uint16_t *) _dst;
390 for (i=0; i<width; i++) {
391 dst[i] = av_bswap16(src[i]);
395 static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *_src1,
396 const uint8_t *_src2, int width, uint32_t *unused)
399 const uint16_t *src1 = (const uint16_t *) _src1,
400 *src2 = (const uint16_t *) _src2;
401 uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
402 for (i=0; i<width; i++) {
403 dstU[i] = av_bswap16(src1[i]);
404 dstV[i] = av_bswap16(src2[i]);
408 /* This is almost identical to the previous, end exists only because
409 * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
410 static void uyvyToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
414 for (i=0; i<width; i++)
418 static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
419 const uint8_t *src2, int width, uint32_t *unused)
422 for (i=0; i<width; i++) {
423 dstU[i]= src1[4*i + 0];
424 dstV[i]= src1[4*i + 2];
426 assert(src1 == src2);
429 static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
430 const uint8_t *src, int width)
433 for (i = 0; i < width; i++) {
434 dst1[i] = src[2*i+0];
435 dst2[i] = src[2*i+1];
439 static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
440 const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
441 int width, uint32_t *unused)
443 nvXXtoUV_c(dstU, dstV, src1, width);
446 static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
447 const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
448 int width, uint32_t *unused)
450 nvXXtoUV_c(dstV, dstU, src1, width);
453 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
455 static void bgr24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,
456 int width, uint32_t *unused)
459 for (i=0; i<width; i++) {
464 dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
468 static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
469 const uint8_t *src2, int width, uint32_t *unused)
472 for (i=0; i<width; i++) {
473 int b= src1[3*i + 0];
474 int g= src1[3*i + 1];
475 int r= src1[3*i + 2];
477 dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
478 dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
480 assert(src1 == src2);
483 static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
484 const uint8_t *src2, int width, uint32_t *unused)
487 for (i=0; i<width; i++) {
488 int b= src1[6*i + 0] + src1[6*i + 3];
489 int g= src1[6*i + 1] + src1[6*i + 4];
490 int r= src1[6*i + 2] + src1[6*i + 5];
492 dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
493 dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
495 assert(src1 == src2);
498 static void rgb24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
502 for (i=0; i<width; i++) {
507 dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
511 static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
512 const uint8_t *src2, int width, uint32_t *unused)
516 for (i=0; i<width; i++) {
517 int r= src1[3*i + 0];
518 int g= src1[3*i + 1];
519 int b= src1[3*i + 2];
521 dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
522 dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
526 static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
527 const uint8_t *src2, int width, uint32_t *unused)
531 for (i=0; i<width; i++) {
532 int r= src1[6*i + 0] + src1[6*i + 3];
533 int g= src1[6*i + 1] + src1[6*i + 4];
534 int b= src1[6*i + 2] + src1[6*i + 5];
536 dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
537 dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
541 static void planar_rgb_to_y(uint16_t *dst, const uint8_t *src[4], int width)
544 for (i = 0; i < width; i++) {
549 dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
553 static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
556 const uint16_t **src = (const uint16_t **) _src;
557 uint16_t *dst = (uint16_t *) _dst;
558 for (i = 0; i < width; i++) {
559 int g = AV_RL16(src[0] + i);
560 int b = AV_RL16(src[1] + i);
561 int r = AV_RL16(src[2] + i);
563 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
567 static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
570 const uint16_t **src = (const uint16_t **) _src;
571 uint16_t *dst = (uint16_t *) _dst;
572 for (i = 0; i < width; i++) {
573 int g = AV_RB16(src[0] + i);
574 int b = AV_RB16(src[1] + i);
575 int r = AV_RB16(src[2] + i);
577 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
581 static void planar_rgb_to_uv(uint16_t *dstU, uint16_t *dstV, const uint8_t *src[4], int width)
584 for (i = 0; i < width; i++) {
589 dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
590 dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
594 static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
597 const uint16_t **src = (const uint16_t **) _src;
598 uint16_t *dstU = (uint16_t *) _dstU;
599 uint16_t *dstV = (uint16_t *) _dstV;
600 for (i = 0; i < width; i++) {
601 int g = AV_RL16(src[0] + i);
602 int b = AV_RL16(src[1] + i);
603 int r = AV_RL16(src[2] + i);
605 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
606 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
610 static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
613 const uint16_t **src = (const uint16_t **) _src;
614 uint16_t *dstU = (uint16_t *) _dstU;
615 uint16_t *dstV = (uint16_t *) _dstV;
616 for (i = 0; i < width; i++) {
617 int g = AV_RB16(src[0] + i);
618 int b = AV_RB16(src[1] + i);
619 int r = AV_RB16(src[2] + i);
621 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
622 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
626 av_cold void ff_sws_init_input_funcs(SwsContext *c)
628 enum PixelFormat srcFormat = c->srcFormat;
632 case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
633 case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
634 case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
635 case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
639 case PIX_FMT_BGR4_BYTE:
640 case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
641 case PIX_FMT_GBRP9LE:
642 case PIX_FMT_GBRP10LE:
643 case PIX_FMT_GBRP16LE: c->readChrPlanar = planar_rgb16le_to_uv; break;
644 case PIX_FMT_GBRP9BE:
645 case PIX_FMT_GBRP10BE:
646 case PIX_FMT_GBRP16BE: c->readChrPlanar = planar_rgb16be_to_uv; break;
647 case PIX_FMT_GBRP: c->readChrPlanar = planar_rgb_to_uv; break;
649 case PIX_FMT_YUV444P9LE:
650 case PIX_FMT_YUV422P9LE:
651 case PIX_FMT_YUV420P9LE:
652 case PIX_FMT_YUV422P10LE:
653 case PIX_FMT_YUV444P10LE:
654 case PIX_FMT_YUV420P10LE:
655 case PIX_FMT_YUV420P16LE:
656 case PIX_FMT_YUV422P16LE:
657 case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
659 case PIX_FMT_YUV444P9BE:
660 case PIX_FMT_YUV422P9BE:
661 case PIX_FMT_YUV420P9BE:
662 case PIX_FMT_YUV444P10BE:
663 case PIX_FMT_YUV422P10BE:
664 case PIX_FMT_YUV420P10BE:
665 case PIX_FMT_YUV420P16BE:
666 case PIX_FMT_YUV422P16BE:
667 case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
670 if (c->chrSrcHSubSample) {
672 case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
673 case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
674 case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
675 case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
676 case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
677 case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
678 case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
679 case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
680 case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
681 case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
682 case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
683 case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_half_c; break;
684 case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_half_c; break;
685 case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
686 case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
687 case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
688 case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
689 case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
690 case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
691 case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
692 case PIX_FMT_GBR24P : c->chrToYV12 = gbr24pToUV_half_c; break;
693 case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_half_c; break;
694 case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_half_c; break;
698 case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
699 case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
700 case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
701 case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
702 case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
703 case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
704 case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
705 case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
706 case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
707 case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
708 case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
709 case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_c; break;
710 case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_c; break;
711 case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
712 case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
713 case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
714 case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
715 case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
716 case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
717 case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
718 case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_c; break;
719 case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_c; break;
726 case PIX_FMT_GBRP9LE:
727 case PIX_FMT_GBRP10LE:
728 case PIX_FMT_GBRP16LE: c->readLumPlanar = planar_rgb16le_to_y; break;
729 case PIX_FMT_GBRP9BE:
730 case PIX_FMT_GBRP10BE:
731 case PIX_FMT_GBRP16BE: c->readLumPlanar = planar_rgb16be_to_y; break;
732 case PIX_FMT_GBRP: c->readLumPlanar = planar_rgb_to_y; break;
734 case PIX_FMT_YUV444P9LE:
735 case PIX_FMT_YUV422P9LE:
736 case PIX_FMT_YUV420P9LE:
737 case PIX_FMT_YUV444P10LE:
738 case PIX_FMT_YUV422P10LE:
739 case PIX_FMT_YUV420P10LE:
740 case PIX_FMT_YUV420P16LE:
741 case PIX_FMT_YUV422P16LE:
742 case PIX_FMT_YUV444P16LE:
743 case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
745 case PIX_FMT_YUV444P9BE:
746 case PIX_FMT_YUV422P9BE:
747 case PIX_FMT_YUV420P9BE:
748 case PIX_FMT_YUV444P10BE:
749 case PIX_FMT_YUV422P10BE:
750 case PIX_FMT_YUV420P10BE:
751 case PIX_FMT_YUV420P16BE:
752 case PIX_FMT_YUV422P16BE:
753 case PIX_FMT_YUV444P16BE:
754 case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
756 case PIX_FMT_YUYV422 :
757 case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
758 case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
759 case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
760 case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
761 case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
762 case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
763 case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
764 case PIX_FMT_BGR444LE : c->lumToYV12 = bgr12leToY_c; break;
765 case PIX_FMT_BGR444BE : c->lumToYV12 = bgr12beToY_c; break;
766 case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
767 case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
768 case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
769 case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
770 case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
771 case PIX_FMT_RGB444LE : c->lumToYV12 = rgb12leToY_c; break;
772 case PIX_FMT_RGB444BE : c->lumToYV12 = rgb12beToY_c; break;
776 case PIX_FMT_BGR4_BYTE:
777 case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
778 case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
779 case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
780 case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
781 case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
782 case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
783 case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
784 case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
785 case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
786 case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
787 case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
792 case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
794 case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
795 case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
796 case PIX_FMT_PAL8 : c->alpToYV12 = palToA_c; break;