avfilter: Log an error if avfilter fails to configure a link.
[libav.git] / libswscale / input.c
CommitLineData
2dd7a1c0
RB
1/*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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.
10 *
11 * Libav 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <assert.h>
22#include <math.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <string.h>
26
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"
33#include "config.h"
34#include "rgb2rgb.h"
35#include "swscale.h"
36#include "swscale_internal.h"
37
38#define RGB2YUV_SHIFT 15
9b75ae05
DB
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))
2dd7a1c0
RB
48
49#define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
50
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)
53
9b75ae05
DB
54static av_always_inline void rgb48ToY_c_template(uint16_t *dst,
55 const uint16_t *src, int width,
56 enum PixelFormat origin)
2dd7a1c0
RB
57{
58 int i;
59 for (i = 0; i < width; i++) {
9b75ae05
DB
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]);
2dd7a1c0 63
9b75ae05 64 dst[i] = (RY * r + GY * g + BY * b + (0x2001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
2dd7a1c0
RB
65 }
66}
67
9b75ae05
DB
68static av_always_inline void rgb48ToUV_c_template(uint16_t *dstU,
69 uint16_t *dstV,
70 const uint16_t *src1,
71 const uint16_t *src2,
72 int width,
73 enum PixelFormat origin)
2dd7a1c0
RB
74{
75 int i;
9b75ae05 76 assert(src1 == src2);
2dd7a1c0 77 for (i = 0; i < width; i++) {
9b75ae05
DB
78 int r_b = input_pixel(&src1[i * 3 + 0]);
79 int g = input_pixel(&src1[i * 3 + 1]);
80 int b_r = input_pixel(&src1[i * 3 + 2]);
2dd7a1c0 81
9b75ae05
DB
82 dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
83 dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
2dd7a1c0
RB
84 }
85}
86
9b75ae05
DB
87static av_always_inline void rgb48ToUV_half_c_template(uint16_t *dstU,
88 uint16_t *dstV,
89 const uint16_t *src1,
90 const uint16_t *src2,
91 int width,
92 enum PixelFormat origin)
2dd7a1c0
RB
93{
94 int i;
9b75ae05 95 assert(src1 == src2);
2dd7a1c0 96 for (i = 0; i < width; i++) {
9b75ae05
DB
97 int r_b = (input_pixel(&src1[6 * i + 0]) +
98 input_pixel(&src1[6 * i + 3]) + 1) >> 1;
99 int g = (input_pixel(&src1[6 * i + 1]) +
100 input_pixel(&src1[6 * i + 4]) + 1) >> 1;
101 int b_r = (input_pixel(&src1[6 * i + 2]) +
102 input_pixel(&src1[6 * i + 5]) + 1) >> 1;
2dd7a1c0 103
9b75ae05
DB
104 dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
105 dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
2dd7a1c0
RB
106 }
107}
108
109#undef r
110#undef b
111#undef input_pixel
112
9b75ae05
DB
113#define rgb48funcs(pattern, BE_LE, origin) \
114static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, \
115 const uint8_t *_src, \
116 int width, \
117 uint32_t *unused) \
118{ \
119 const uint16_t *src = (const uint16_t *)_src; \
120 uint16_t *dst = (uint16_t *)_dst; \
121 rgb48ToY_c_template(dst, src, width, origin); \
122} \
123 \
124static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, \
125 uint8_t *_dstV, \
126 const uint8_t *_src1, \
127 const uint8_t *_src2, \
128 int width, \
129 uint32_t *unused) \
130{ \
131 const uint16_t *src1 = (const uint16_t *)_src1, \
132 *src2 = (const uint16_t *)_src2; \
133 uint16_t *dstU = (uint16_t *)_dstU, \
134 *dstV = (uint16_t *)_dstV; \
135 rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
136} \
137 \
138static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, \
139 uint8_t *_dstV, \
140 const uint8_t *_src1, \
141 const uint8_t *_src2, \
142 int width, \
143 uint32_t *unused) \
144{ \
145 const uint16_t *src1 = (const uint16_t *)_src1, \
146 *src2 = (const uint16_t *)_src2; \
147 uint16_t *dstU = (uint16_t *)_dstU, \
148 *dstV = (uint16_t *)_dstV; \
149 rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
2dd7a1c0
RB
150}
151
152rgb48funcs(rgb, LE, PIX_FMT_RGB48LE)
153rgb48funcs(rgb, BE, PIX_FMT_RGB48BE)
154rgb48funcs(bgr, LE, PIX_FMT_BGR48LE)
155rgb48funcs(bgr, BE, PIX_FMT_BGR48BE)
156
9b75ae05
DB
157#define input_pixel(i) ((origin == PIX_FMT_RGBA || \
158 origin == PIX_FMT_BGRA || \
159 origin == PIX_FMT_ARGB || \
160 origin == PIX_FMT_ABGR) \
161 ? AV_RN32A(&src[(i) * 4]) \
162 : (isBE(origin) ? AV_RB16(&src[(i) * 2]) \
163 : AV_RL16(&src[(i) * 2])))
164
165static av_always_inline void rgb16_32ToY_c_template(uint8_t *dst,
166 const uint8_t *src,
167 int width,
168 enum PixelFormat origin,
169 int shr, int shg,
170 int shb, int shp,
171 int maskr, int maskg,
172 int maskb, int rsh,
173 int gsh, int bsh, int S)
174{
175 const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh;
2dd7a1c0
RB
176 const unsigned rnd = 33u << (S - 1);
177 int i;
178
179 for (i = 0; i < width; i++) {
180 int px = input_pixel(i) >> shp;
9b75ae05
DB
181 int b = (px & maskb) >> shb;
182 int g = (px & maskg) >> shg;
183 int r = (px & maskr) >> shr;
2dd7a1c0
RB
184
185 dst[i] = (ry * r + gy * g + by * b + rnd) >> S;
186 }
187}
188
9b75ae05
DB
189static av_always_inline void rgb16_32ToUV_c_template(uint8_t *dstU,
190 uint8_t *dstV,
191 const uint8_t *src,
192 int width,
193 enum PixelFormat origin,
194 int shr, int shg,
195 int shb, int shp,
196 int maskr, int maskg,
197 int maskb, int rsh,
198 int gsh, int bsh, int S)
2dd7a1c0 199{
9b75ae05
DB
200 const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
201 rv = RV << rsh, gv = GV << gsh, bv = BV << bsh;
2dd7a1c0
RB
202 const unsigned rnd = 257u << (S - 1);
203 int i;
204
205 for (i = 0; i < width; i++) {
206 int px = input_pixel(i) >> shp;
9b75ae05
DB
207 int b = (px & maskb) >> shb;
208 int g = (px & maskg) >> shg;
209 int r = (px & maskr) >> shr;
2dd7a1c0
RB
210
211 dstU[i] = (ru * r + gu * g + bu * b + rnd) >> S;
212 dstV[i] = (rv * r + gv * g + bv * b + rnd) >> S;
213 }
214}
215
9b75ae05
DB
216static av_always_inline void rgb16_32ToUV_half_c_template(uint8_t *dstU,
217 uint8_t *dstV,
218 const uint8_t *src,
219 int width,
220 enum PixelFormat origin,
221 int shr, int shg,
222 int shb, int shp,
223 int maskr, int maskg,
224 int maskb, int rsh,
225 int gsh, int bsh, int S)
2dd7a1c0 226{
9b75ae05
DB
227 const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
228 rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
229 maskgx = ~(maskr | maskb);
2dd7a1c0
RB
230 const unsigned rnd = 257u << S;
231 int i;
232
9b75ae05
DB
233 maskr |= maskr << 1;
234 maskb |= maskb << 1;
235 maskg |= maskg << 1;
2dd7a1c0
RB
236 for (i = 0; i < width; i++) {
237 int px0 = input_pixel(2 * i + 0) >> shp;
238 int px1 = input_pixel(2 * i + 1) >> shp;
239 int b, r, g = (px0 & maskgx) + (px1 & maskgx);
240 int rb = px0 + px1 - g;
241
242 b = (rb & maskb) >> shb;
9b75ae05
DB
243 if (shp ||
244 origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
2dd7a1c0
RB
245 origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
246 g >>= shg;
247 } else {
9b75ae05 248 g = (g & maskg) >> shg;
2dd7a1c0
RB
249 }
250 r = (rb & maskr) >> shr;
251
252 dstU[i] = (ru * r + gu * g + bu * b + rnd) >> (S + 1);
253 dstV[i] = (rv * r + gv * g + bv * b + rnd) >> (S + 1);
254 }
255}
256
257#undef input_pixel
258
9b75ae05
DB
259#define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
260 maskg, maskb, rsh, gsh, bsh, S) \
261static void name ## ToY_c(uint8_t *dst, const uint8_t *src, \
262 int width, uint32_t *unused) \
263{ \
264 rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
265 maskr, maskg, maskb, rsh, gsh, bsh, S); \
266} \
267 \
268static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
269 const uint8_t *src, const uint8_t *dummy, \
270 int width, uint32_t *unused) \
271{ \
272 rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, \
273 shr, shg, shb, shp, \
274 maskr, maskg, maskb, rsh, gsh, bsh, S); \
275} \
276 \
277static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
278 const uint8_t *src, \
279 const uint8_t *dummy, \
280 int width, uint32_t *unused) \
281{ \
282 rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, \
283 shr, shg, shb, shp, \
284 maskr, maskg, maskb, \
285 rsh, gsh, bsh, S); \
286}
287
288rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT + 8)
289rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT + 8)
290rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT + 8)
291rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT + 8)
292rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT + 8)
293rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT + 7)
294rgb16_32_wrapper(PIX_FMT_BGR444LE, bgr12le, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT + 4)
295rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT + 8)
296rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT + 7)
297rgb16_32_wrapper(PIX_FMT_RGB444LE, rgb12le, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT + 4)
298rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT + 8)
299rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT + 7)
300rgb16_32_wrapper(PIX_FMT_BGR444BE, bgr12be, 0, 0, 0, 0, 0x000F, 0x00F0, 0x0F00, 8, 4, 0, RGB2YUV_SHIFT + 4)
301rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT + 8)
302rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT + 7)
303rgb16_32_wrapper(PIX_FMT_RGB444BE, rgb12be, 0, 0, 0, 0, 0x0F00, 0x00F0, 0x000F, 0, 4, 8, RGB2YUV_SHIFT + 4)
304
305static void abgrToA_c(uint8_t *dst, const uint8_t *src, int width,
306 uint32_t *unused)
2dd7a1c0
RB
307{
308 int i;
9b75ae05
DB
309 for (i = 0; i < width; i++)
310 dst[i] = src[4 * i];
2dd7a1c0
RB
311}
312
9b75ae05
DB
313static void rgbaToA_c(uint8_t *dst, const uint8_t *src, int width,
314 uint32_t *unused)
2dd7a1c0
RB
315{
316 int i;
9b75ae05
DB
317 for (i = 0; i < width; i++)
318 dst[i] = src[4 * i + 3];
2dd7a1c0
RB
319}
320
321static void palToY_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
322{
323 int i;
9b75ae05
DB
324 for (i = 0; i < width; i++) {
325 int d = src[i];
2dd7a1c0 326
9b75ae05 327 dst[i] = pal[d] & 0xFF;
2dd7a1c0
RB
328 }
329}
330
331static void palToUV_c(uint8_t *dstU, uint8_t *dstV,
332 const uint8_t *src1, const uint8_t *src2,
333 int width, uint32_t *pal)
334{
335 int i;
336 assert(src1 == src2);
9b75ae05
DB
337 for (i = 0; i < width; i++) {
338 int p = pal[src1[i]];
2dd7a1c0 339
9b75ae05
DB
340 dstU[i] = p >> 8;
341 dstV[i] = p >> 16;
2dd7a1c0
RB
342 }
343}
344
345static void monowhite2Y_c(uint8_t *dst, const uint8_t *src,
346 int width, uint32_t *unused)
347{
348 int i, j;
bc0bdda7
RB
349 width = (width + 7) >> 3;
350 for (i = 0; i < width; i++) {
9b75ae05
DB
351 int d = ~src[i];
352 for (j = 0; j < 8; j++)
353 dst[8 * i + j] = ((d >> (7 - j)) & 1) * 255;
2dd7a1c0
RB
354 }
355}
356
357static void monoblack2Y_c(uint8_t *dst, const uint8_t *src,
358 int width, uint32_t *unused)
359{
360 int i, j;
bc0bdda7
RB
361 width = (width + 7) >> 3;
362 for (i = 0; i < width; i++) {
9b75ae05
DB
363 int d = src[i];
364 for (j = 0; j < 8; j++)
365 dst[8 * i + j] = ((d >> (7 - j)) & 1) * 255;
2dd7a1c0
RB
366 }
367}
368
369static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
370 uint32_t *unused)
371{
372 int i;
9b75ae05
DB
373 for (i = 0; i < width; i++)
374 dst[i] = src[2 * i];
2dd7a1c0
RB
375}
376
377static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
378 const uint8_t *src2, int width, uint32_t *unused)
379{
380 int i;
9b75ae05
DB
381 for (i = 0; i < width; i++) {
382 dstU[i] = src1[4 * i + 1];
383 dstV[i] = src1[4 * i + 3];
2dd7a1c0
RB
384 }
385 assert(src1 == src2);
386}
387
9b75ae05
DB
388static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, int width,
389 uint32_t *unused)
2dd7a1c0
RB
390{
391 int i;
9b75ae05
DB
392 const uint16_t *src = (const uint16_t *)_src;
393 uint16_t *dst = (uint16_t *)_dst;
394 for (i = 0; i < width; i++)
2dd7a1c0 395 dst[i] = av_bswap16(src[i]);
2dd7a1c0
RB
396}
397
398static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src1,
399 const uint8_t *_src2, int width, uint32_t *unused)
400{
401 int i;
9b75ae05
DB
402 const uint16_t *src1 = (const uint16_t *)_src1,
403 *src2 = (const uint16_t *)_src2;
404 uint16_t *dstU = (uint16_t *)_dstU, *dstV = (uint16_t *)_dstV;
405 for (i = 0; i < width; i++) {
2dd7a1c0
RB
406 dstU[i] = av_bswap16(src1[i]);
407 dstV[i] = av_bswap16(src2[i]);
408 }
409}
410
411/* This is almost identical to the previous, end exists only because
9b75ae05 412 * yuy2ToY/UV)(dst, src + 1, ...) would have 100% unaligned accesses. */
2dd7a1c0
RB
413static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
414 uint32_t *unused)
415{
416 int i;
9b75ae05
DB
417 for (i = 0; i < width; i++)
418 dst[i] = src[2 * i + 1];
2dd7a1c0
RB
419}
420
421static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
422 const uint8_t *src2, int width, uint32_t *unused)
423{
424 int i;
9b75ae05
DB
425 for (i = 0; i < width; i++) {
426 dstU[i] = src1[4 * i + 0];
427 dstV[i] = src1[4 * i + 2];
2dd7a1c0
RB
428 }
429 assert(src1 == src2);
430}
431
432static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
433 const uint8_t *src, int width)
434{
435 int i;
436 for (i = 0; i < width; i++) {
9b75ae05
DB
437 dst1[i] = src[2 * i + 0];
438 dst2[i] = src[2 * i + 1];
2dd7a1c0
RB
439 }
440}
441
442static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
443 const uint8_t *src1, const uint8_t *src2,
444 int width, uint32_t *unused)
445{
446 nvXXtoUV_c(dstU, dstV, src1, width);
447}
448
449static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
450 const uint8_t *src1, const uint8_t *src2,
451 int width, uint32_t *unused)
452{
453 nvXXtoUV_c(dstV, dstU, src1, width);
454}
455
456#define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
457
458static void bgr24ToY_c(uint8_t *dst, const uint8_t *src,
459 int width, uint32_t *unused)
460{
461 int i;
9b75ae05
DB
462 for (i = 0; i < width; i++) {
463 int b = src[i * 3 + 0];
464 int g = src[i * 3 + 1];
465 int r = src[i * 3 + 2];
2dd7a1c0 466
9b75ae05 467 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
2dd7a1c0
RB
468 }
469}
470
471static void bgr24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
472 const uint8_t *src2, int width, uint32_t *unused)
473{
474 int i;
9b75ae05
DB
475 for (i = 0; i < width; i++) {
476 int b = src1[3 * i + 0];
477 int g = src1[3 * i + 1];
478 int r = src1[3 * i + 2];
2dd7a1c0 479
9b75ae05
DB
480 dstU[i] = (RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
481 dstV[i] = (RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
2dd7a1c0
RB
482 }
483 assert(src1 == src2);
484}
485
486static void bgr24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
487 const uint8_t *src2, int width, uint32_t *unused)
488{
489 int i;
9b75ae05
DB
490 for (i = 0; i < width; i++) {
491 int b = src1[6 * i + 0] + src1[6 * i + 3];
492 int g = src1[6 * i + 1] + src1[6 * i + 4];
493 int r = src1[6 * i + 2] + src1[6 * i + 5];
2dd7a1c0 494
9b75ae05
DB
495 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
496 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
2dd7a1c0
RB
497 }
498 assert(src1 == src2);
499}
500
501static void rgb24ToY_c(uint8_t *dst, const uint8_t *src, int width,
502 uint32_t *unused)
503{
504 int i;
9b75ae05
DB
505 for (i = 0; i < width; i++) {
506 int r = src[i * 3 + 0];
507 int g = src[i * 3 + 1];
508 int b = src[i * 3 + 2];
2dd7a1c0 509
9b75ae05 510 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
2dd7a1c0
RB
511 }
512}
513
514static void rgb24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
515 const uint8_t *src2, int width, uint32_t *unused)
516{
517 int i;
9b75ae05
DB
518 assert(src1 == src2);
519 for (i = 0; i < width; i++) {
520 int r = src1[3 * i + 0];
521 int g = src1[3 * i + 1];
522 int b = src1[3 * i + 2];
523
524 dstU[i] = (RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
525 dstV[i] = (RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
2dd7a1c0
RB
526 }
527}
528
529static void rgb24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
530 const uint8_t *src2, int width, uint32_t *unused)
531{
532 int i;
9b75ae05
DB
533 assert(src1 == src2);
534 for (i = 0; i < width; i++) {
535 int r = src1[6 * i + 0] + src1[6 * i + 3];
536 int g = src1[6 * i + 1] + src1[6 * i + 4];
537 int b = src1[6 * i + 2] + src1[6 * i + 5];
538
539 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
540 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
2dd7a1c0
RB
541 }
542}
543
544static void planar_rgb_to_y(uint8_t *dst, const uint8_t *src[4], int width)
545{
546 int i;
547 for (i = 0; i < width; i++) {
548 int g = src[0][i];
549 int b = src[1][i];
550 int r = src[2][i];
551
552 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
553 }
554}
555
556static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
557{
558 int i;
9b75ae05
DB
559 const uint16_t **src = (const uint16_t **)_src;
560 uint16_t *dst = (uint16_t *)_dst;
2dd7a1c0
RB
561 for (i = 0; i < width; i++) {
562 int g = AV_RL16(src[0] + i);
563 int b = AV_RL16(src[1] + i);
564 int r = AV_RL16(src[2] + i);
565
566 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
567 }
568}
569
570static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
571{
572 int i;
9b75ae05
DB
573 const uint16_t **src = (const uint16_t **)_src;
574 uint16_t *dst = (uint16_t *)_dst;
2dd7a1c0
RB
575 for (i = 0; i < width; i++) {
576 int g = AV_RB16(src[0] + i);
577 int b = AV_RB16(src[1] + i);
578 int r = AV_RB16(src[2] + i);
579
580 dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
581 }
582}
583
584static void planar_rgb_to_uv(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4], int width)
585{
586 int i;
587 for (i = 0; i < width; i++) {
588 int g = src[0][i];
589 int b = src[1][i];
590 int r = src[2][i];
591
592 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
593 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
594 }
595}
596
9b75ae05
DB
597static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV,
598 const uint8_t *_src[4], int width)
2dd7a1c0
RB
599{
600 int i;
9b75ae05
DB
601 const uint16_t **src = (const uint16_t **)_src;
602 uint16_t *dstU = (uint16_t *)_dstU;
603 uint16_t *dstV = (uint16_t *)_dstV;
2dd7a1c0
RB
604 for (i = 0; i < width; i++) {
605 int g = AV_RL16(src[0] + i);
606 int b = AV_RL16(src[1] + i);
607 int r = AV_RL16(src[2] + i);
608
609 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
610 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
611 }
612}
613
9b75ae05
DB
614static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV,
615 const uint8_t *_src[4], int width)
2dd7a1c0
RB
616{
617 int i;
9b75ae05
DB
618 const uint16_t **src = (const uint16_t **)_src;
619 uint16_t *dstU = (uint16_t *)_dstU;
620 uint16_t *dstV = (uint16_t *)_dstV;
2dd7a1c0
RB
621 for (i = 0; i < width; i++) {
622 int g = AV_RB16(src[0] + i);
623 int b = AV_RB16(src[1] + i);
624 int r = AV_RB16(src[2] + i);
625
626 dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
627 dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
628 }
629}
630
631av_cold void ff_sws_init_input_funcs(SwsContext *c)
632{
633 enum PixelFormat srcFormat = c->srcFormat;
634
635 c->chrToYV12 = NULL;
9b75ae05
DB
636 switch (srcFormat) {
637 case PIX_FMT_YUYV422:
638 c->chrToYV12 = yuy2ToUV_c;
639 break;
640 case PIX_FMT_UYVY422:
641 c->chrToYV12 = uyvyToUV_c;
642 break;
643 case PIX_FMT_NV12:
644 c->chrToYV12 = nv12ToUV_c;
645 break;
646 case PIX_FMT_NV21:
647 c->chrToYV12 = nv21ToUV_c;
648 break;
649 case PIX_FMT_RGB8:
650 case PIX_FMT_BGR8:
651 case PIX_FMT_PAL8:
652 case PIX_FMT_BGR4_BYTE:
653 case PIX_FMT_RGB4_BYTE:
654 c->chrToYV12 = palToUV_c;
655 break;
656 case PIX_FMT_GBRP9LE:
657 case PIX_FMT_GBRP10LE:
658 case PIX_FMT_GBRP16LE:
659 c->readChrPlanar = planar_rgb16le_to_uv;
660 break;
661 case PIX_FMT_GBRP9BE:
662 case PIX_FMT_GBRP10BE:
663 case PIX_FMT_GBRP16BE:
664 c->readChrPlanar = planar_rgb16be_to_uv;
665 break;
666 case PIX_FMT_GBRP:
667 c->readChrPlanar = planar_rgb_to_uv;
668 break;
2dd7a1c0 669#if HAVE_BIGENDIAN
9b75ae05
DB
670 case PIX_FMT_YUV444P9LE:
671 case PIX_FMT_YUV422P9LE:
672 case PIX_FMT_YUV420P9LE:
673 case PIX_FMT_YUV422P10LE:
674 case PIX_FMT_YUV444P10LE:
675 case PIX_FMT_YUV420P10LE:
676 case PIX_FMT_YUV420P16LE:
677 case PIX_FMT_YUV422P16LE:
678 case PIX_FMT_YUV444P16LE:
679 c->chrToYV12 = bswap16UV_c;
680 break;
2dd7a1c0 681#else
9b75ae05
DB
682 case PIX_FMT_YUV444P9BE:
683 case PIX_FMT_YUV422P9BE:
684 case PIX_FMT_YUV420P9BE:
685 case PIX_FMT_YUV444P10BE:
686 case PIX_FMT_YUV422P10BE:
687 case PIX_FMT_YUV420P10BE:
688 case PIX_FMT_YUV420P16BE:
689 case PIX_FMT_YUV422P16BE:
690 case PIX_FMT_YUV444P16BE:
691 c->chrToYV12 = bswap16UV_c;
692 break;
2dd7a1c0
RB
693#endif
694 }
695 if (c->chrSrcHSubSample) {
9b75ae05
DB
696 switch (srcFormat) {
697 case PIX_FMT_RGB48BE:
698 c->chrToYV12 = rgb48BEToUV_half_c;
699 break;
700 case PIX_FMT_RGB48LE:
701 c->chrToYV12 = rgb48LEToUV_half_c;
702 break;
703 case PIX_FMT_BGR48BE:
704 c->chrToYV12 = bgr48BEToUV_half_c;
705 break;
706 case PIX_FMT_BGR48LE:
707 c->chrToYV12 = bgr48LEToUV_half_c;
708 break;
709 case PIX_FMT_RGB32:
710 c->chrToYV12 = bgr32ToUV_half_c;
711 break;
712 case PIX_FMT_RGB32_1:
713 c->chrToYV12 = bgr321ToUV_half_c;
714 break;
715 case PIX_FMT_BGR24:
716 c->chrToYV12 = bgr24ToUV_half_c;
717 break;
718 case PIX_FMT_BGR565LE:
719 c->chrToYV12 = bgr16leToUV_half_c;
720 break;
721 case PIX_FMT_BGR565BE:
722 c->chrToYV12 = bgr16beToUV_half_c;
723 break;
724 case PIX_FMT_BGR555LE:
725 c->chrToYV12 = bgr15leToUV_half_c;
726 break;
727 case PIX_FMT_BGR555BE:
728 c->chrToYV12 = bgr15beToUV_half_c;
729 break;
730 case PIX_FMT_BGR444LE:
731 c->chrToYV12 = bgr12leToUV_half_c;
732 break;
733 case PIX_FMT_BGR444BE:
734 c->chrToYV12 = bgr12beToUV_half_c;
735 break;
736 case PIX_FMT_BGR32:
737 c->chrToYV12 = rgb32ToUV_half_c;
738 break;
739 case PIX_FMT_BGR32_1:
740 c->chrToYV12 = rgb321ToUV_half_c;
741 break;
742 case PIX_FMT_RGB24:
743 c->chrToYV12 = rgb24ToUV_half_c;
744 break;
745 case PIX_FMT_RGB565LE:
746 c->chrToYV12 = rgb16leToUV_half_c;
747 break;
748 case PIX_FMT_RGB565BE:
749 c->chrToYV12 = rgb16beToUV_half_c;
750 break;
751 case PIX_FMT_RGB555LE:
752 c->chrToYV12 = rgb15leToUV_half_c;
753 break;
754 case PIX_FMT_RGB555BE:
755 c->chrToYV12 = rgb15beToUV_half_c;
756 break;
757 case PIX_FMT_RGB444LE:
758 c->chrToYV12 = rgb12leToUV_half_c;
759 break;
760 case PIX_FMT_RGB444BE:
761 c->chrToYV12 = rgb12beToUV_half_c;
762 break;
2dd7a1c0
RB
763 }
764 } else {
9b75ae05
DB
765 switch (srcFormat) {
766 case PIX_FMT_RGB48BE:
767 c->chrToYV12 = rgb48BEToUV_c;
768 break;
769 case PIX_FMT_RGB48LE:
770 c->chrToYV12 = rgb48LEToUV_c;
771 break;
772 case PIX_FMT_BGR48BE:
773 c->chrToYV12 = bgr48BEToUV_c;
774 break;
775 case PIX_FMT_BGR48LE:
776 c->chrToYV12 = bgr48LEToUV_c;
777 break;
778 case PIX_FMT_RGB32:
779 c->chrToYV12 = bgr32ToUV_c;
780 break;
781 case PIX_FMT_RGB32_1:
782 c->chrToYV12 = bgr321ToUV_c;
783 break;
784 case PIX_FMT_BGR24:
785 c->chrToYV12 = bgr24ToUV_c;
786 break;
787 case PIX_FMT_BGR565LE:
788 c->chrToYV12 = bgr16leToUV_c;
789 break;
790 case PIX_FMT_BGR565BE:
791 c->chrToYV12 = bgr16beToUV_c;
792 break;
793 case PIX_FMT_BGR555LE:
794 c->chrToYV12 = bgr15leToUV_c;
795 break;
796 case PIX_FMT_BGR555BE:
797 c->chrToYV12 = bgr15beToUV_c;
798 break;
799 case PIX_FMT_BGR444LE:
800 c->chrToYV12 = bgr12leToUV_c;
801 break;
802 case PIX_FMT_BGR444BE:
803 c->chrToYV12 = bgr12beToUV_c;
804 break;
805 case PIX_FMT_BGR32:
806 c->chrToYV12 = rgb32ToUV_c;
807 break;
808 case PIX_FMT_BGR32_1:
809 c->chrToYV12 = rgb321ToUV_c;
810 break;
811 case PIX_FMT_RGB24:
812 c->chrToYV12 = rgb24ToUV_c;
813 break;
814 case PIX_FMT_RGB565LE:
815 c->chrToYV12 = rgb16leToUV_c;
816 break;
817 case PIX_FMT_RGB565BE:
818 c->chrToYV12 = rgb16beToUV_c;
819 break;
820 case PIX_FMT_RGB555LE:
821 c->chrToYV12 = rgb15leToUV_c;
822 break;
823 case PIX_FMT_RGB555BE:
824 c->chrToYV12 = rgb15beToUV_c;
825 break;
826 case PIX_FMT_RGB444LE:
827 c->chrToYV12 = rgb12leToUV_c;
828 break;
829 case PIX_FMT_RGB444BE:
830 c->chrToYV12 = rgb12beToUV_c;
831 break;
2dd7a1c0
RB
832 }
833 }
834
835 c->lumToYV12 = NULL;
836 c->alpToYV12 = NULL;
837 switch (srcFormat) {
838 case PIX_FMT_GBRP9LE:
839 case PIX_FMT_GBRP10LE:
9b75ae05
DB
840 case PIX_FMT_GBRP16LE:
841 c->readLumPlanar = planar_rgb16le_to_y;
842 break;
2dd7a1c0
RB
843 case PIX_FMT_GBRP9BE:
844 case PIX_FMT_GBRP10BE:
9b75ae05
DB
845 case PIX_FMT_GBRP16BE:
846 c->readLumPlanar = planar_rgb16be_to_y;
847 break;
848 case PIX_FMT_GBRP:
849 c->readLumPlanar = planar_rgb_to_y;
850 break;
2dd7a1c0
RB
851#if HAVE_BIGENDIAN
852 case PIX_FMT_YUV444P9LE:
853 case PIX_FMT_YUV422P9LE:
854 case PIX_FMT_YUV420P9LE:
855 case PIX_FMT_YUV444P10LE:
856 case PIX_FMT_YUV422P10LE:
857 case PIX_FMT_YUV420P10LE:
858 case PIX_FMT_YUV420P16LE:
859 case PIX_FMT_YUV422P16LE:
860 case PIX_FMT_YUV444P16LE:
9b75ae05
DB
861 case PIX_FMT_GRAY16LE:
862 c->lumToYV12 = bswap16Y_c;
863 break;
2dd7a1c0
RB
864#else
865 case PIX_FMT_YUV444P9BE:
866 case PIX_FMT_YUV422P9BE:
867 case PIX_FMT_YUV420P9BE:
868 case PIX_FMT_YUV444P10BE:
869 case PIX_FMT_YUV422P10BE:
870 case PIX_FMT_YUV420P10BE:
871 case PIX_FMT_YUV420P16BE:
872 case PIX_FMT_YUV422P16BE:
873 case PIX_FMT_YUV444P16BE:
9b75ae05
DB
874 case PIX_FMT_GRAY16BE:
875 c->lumToYV12 = bswap16Y_c;
876 break;
2dd7a1c0 877#endif
9b75ae05
DB
878 case PIX_FMT_YUYV422:
879 case PIX_FMT_Y400A:
880 c->lumToYV12 = yuy2ToY_c;
881 break;
882 case PIX_FMT_UYVY422:
883 c->lumToYV12 = uyvyToY_c;
884 break;
885 case PIX_FMT_BGR24:
886 c->lumToYV12 = bgr24ToY_c;
887 break;
888 case PIX_FMT_BGR565LE:
889 c->lumToYV12 = bgr16leToY_c;
890 break;
891 case PIX_FMT_BGR565BE:
892 c->lumToYV12 = bgr16beToY_c;
893 break;
894 case PIX_FMT_BGR555LE:
895 c->lumToYV12 = bgr15leToY_c;
896 break;
897 case PIX_FMT_BGR555BE:
898 c->lumToYV12 = bgr15beToY_c;
899 break;
900 case PIX_FMT_BGR444LE:
901 c->lumToYV12 = bgr12leToY_c;
902 break;
903 case PIX_FMT_BGR444BE:
904 c->lumToYV12 = bgr12beToY_c;
905 break;
906 case PIX_FMT_RGB24:
907 c->lumToYV12 = rgb24ToY_c;
908 break;
909 case PIX_FMT_RGB565LE:
910 c->lumToYV12 = rgb16leToY_c;
911 break;
912 case PIX_FMT_RGB565BE:
913 c->lumToYV12 = rgb16beToY_c;
914 break;
915 case PIX_FMT_RGB555LE:
916 c->lumToYV12 = rgb15leToY_c;
917 break;
918 case PIX_FMT_RGB555BE:
919 c->lumToYV12 = rgb15beToY_c;
920 break;
921 case PIX_FMT_RGB444LE:
922 c->lumToYV12 = rgb12leToY_c;
923 break;
924 case PIX_FMT_RGB444BE:
925 c->lumToYV12 = rgb12beToY_c;
926 break;
927 case PIX_FMT_RGB8:
928 case PIX_FMT_BGR8:
929 case PIX_FMT_PAL8:
2dd7a1c0 930 case PIX_FMT_BGR4_BYTE:
9b75ae05
DB
931 case PIX_FMT_RGB4_BYTE:
932 c->lumToYV12 = palToY_c;
933 break;
934 case PIX_FMT_MONOBLACK:
935 c->lumToYV12 = monoblack2Y_c;
936 break;
937 case PIX_FMT_MONOWHITE:
938 c->lumToYV12 = monowhite2Y_c;
939 break;
940 case PIX_FMT_RGB32:
941 c->lumToYV12 = bgr32ToY_c;
942 break;
943 case PIX_FMT_RGB32_1:
944 c->lumToYV12 = bgr321ToY_c;
945 break;
946 case PIX_FMT_BGR32:
947 c->lumToYV12 = rgb32ToY_c;
948 break;
949 case PIX_FMT_BGR32_1:
950 c->lumToYV12 = rgb321ToY_c;
951 break;
952 case PIX_FMT_RGB48BE:
953 c->lumToYV12 = rgb48BEToY_c;
954 break;
955 case PIX_FMT_RGB48LE:
956 c->lumToYV12 = rgb48LEToY_c;
957 break;
958 case PIX_FMT_BGR48BE:
959 c->lumToYV12 = bgr48BEToY_c;
960 break;
961 case PIX_FMT_BGR48LE:
962 c->lumToYV12 = bgr48LEToY_c;
963 break;
2dd7a1c0
RB
964 }
965 if (c->alpPixBuf) {
966 switch (srcFormat) {
967 case PIX_FMT_BGRA:
9b75ae05
DB
968 case PIX_FMT_RGBA:
969 c->alpToYV12 = rgbaToA_c;
970 break;
2dd7a1c0 971 case PIX_FMT_ABGR:
9b75ae05
DB
972 case PIX_FMT_ARGB:
973 c->alpToYV12 = abgrToA_c;
974 break;
975 case PIX_FMT_Y400A:
976 c->alpToYV12 = uyvyToY_c;
977 break;
2dd7a1c0
RB
978 }
979 }
980}