]> git.sesse.net Git - ffmpeg/blob - libswscale/input.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswscale / input.c
1 /*
2  * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
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
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))
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
54 static av_always_inline void
55 rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
56                     enum PixelFormat origin)
57 {
58     int i;
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]);
63
64         dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
65     }
66 }
67
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)
72 {
73     int i;
74     assert(src1==src2);
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]);
79
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;
82     }
83 }
84
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)
89 {
90     int i;
91     assert(src1==src2);
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;
96
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;
99     }
100 }
101
102 #undef r
103 #undef b
104 #undef input_pixel
105
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) \
109 { \
110     const uint16_t *src = (const uint16_t *) _src; \
111     uint16_t *dst = (uint16_t *) _dst; \
112     rgb48ToY_c_template(dst, src, width, origin); \
113 } \
114  \
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) \
118 { \
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); \
123 } \
124  \
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) \
128 { \
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); \
133 }
134
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)
139
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])))
143
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)
150 {
151     const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh;
152     const unsigned rnd = (32<<((S)-1)) + (1<<(S-7));
153     int i;
154
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;
160
161         dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
162     }
163 }
164
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)
172 {
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));
176     int i;
177
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;
183
184         dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
185         dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
186     }
187 }
188
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)
196 {
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));
201     int i;
202
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;
209
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) {
213             g >>= shg;
214         } else {
215             g = (g  & maskg) >> shg;
216         }
217         r = (rb & maskr) >> shr;
218
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);
221     }
222 }
223
224 #undef input_pixel
225
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) \
230 { \
231     rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, \
232                            shr, shg, shb, shp, \
233                            maskr, maskg, maskb, rsh, gsh, bsh, S); \
234 } \
235  \
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) \
239 { \
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); \
243 } \
244  \
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) \
248 { \
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); \
252 }
253
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)
270
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)
274 {
275     int i;
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];
280
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);
283     }
284 }
285
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)
287 {
288     int i;
289     for (i=0; i<width; i++) {
290         dst[i]= src[4*i]<<6;
291     }
292 }
293
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)
295 {
296     int i;
297     for (i=0; i<width; i++) {
298         dst[i]= src[4*i+3]<<6;
299     }
300 }
301
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)
303 {
304     int i;
305     for (i=0; i<width; i++) {
306         int d= src[i];
307
308         dst[i]= (pal[d] >> 24)<<6;
309     }
310 }
311
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)
313 {
314     int i;
315     for (i=0; i<width; i++) {
316         int d= src[i];
317
318         dst[i]= (pal[d] & 0xFF)<<6;
319     }
320 }
321
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)
325 {
326     int i;
327     assert(src1 == src2);
328     for (i=0; i<width; i++) {
329         int p= pal[src1[i]];
330
331         dstU[i]= (uint8_t)(p>> 8)<<6;
332         dstV[i]= (uint8_t)(p>>16)<<6;
333     }
334 }
335
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)
337 {
338     int i, j;
339     for (i=0; i<width/8; i++) {
340         int d= ~src[i];
341         for(j=0; j<8; j++)
342             dst[8*i+j]= ((d>>(7-j))&1)*16383;
343     }
344     if(width&7){
345         int d= ~src[i];
346         for(j=0; j<(width&7); j++)
347             dst[8*i+j]= ((d>>(7-j))&1)*16383;
348     }
349 }
350
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)
352 {
353     int i, j;
354     for (i=0; i<width/8; i++) {
355         int d= src[i];
356         for(j=0; j<8; j++)
357             dst[8*i+j]= ((d>>(7-j))&1)*16383;
358     }
359     if(width&7){
360         int d= src[i];
361         for(j=0; j<(width&7); j++)
362             dst[8*i+j]= ((d>>(7-j))&1)*16383;
363     }
364 }
365
366 static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,  int width,
367                       uint32_t *unused)
368 {
369     int i;
370     for (i=0; i<width; i++)
371         dst[i]= src[2*i];
372 }
373
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)
376 {
377     int i;
378     for (i=0; i<width; i++) {
379         dstU[i]= src1[4*i + 1];
380         dstV[i]= src1[4*i + 3];
381     }
382     assert(src1 == src2);
383 }
384
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)
386 {
387     int i;
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]);
392     }
393 }
394
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)
397 {
398     int i;
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]);
405     }
406 }
407
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,
411                       uint32_t *unused)
412 {
413     int i;
414     for (i=0; i<width; i++)
415         dst[i]= src[2*i+1];
416 }
417
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)
420 {
421     int i;
422     for (i=0; i<width; i++) {
423         dstU[i]= src1[4*i + 0];
424         dstV[i]= src1[4*i + 2];
425     }
426     assert(src1 == src2);
427 }
428
429 static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
430                                         const uint8_t *src, int width)
431 {
432     int i;
433     for (i = 0; i < width; i++) {
434         dst1[i] = src[2*i+0];
435         dst2[i] = src[2*i+1];
436     }
437 }
438
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)
442 {
443     nvXXtoUV_c(dstU, dstV, src1, width);
444 }
445
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)
449 {
450     nvXXtoUV_c(dstV, dstU, src1, width);
451 }
452
453 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
454
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)
457 {
458     int i;
459     for (i=0; i<width; i++) {
460         int b= src[i*3+0];
461         int g= src[i*3+1];
462         int r= src[i*3+2];
463
464         dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
465     }
466 }
467
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)
470 {
471     int i;
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];
476
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);
479     }
480     assert(src1 == src2);
481 }
482
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)
485 {
486     int i;
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];
491
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);
494     }
495     assert(src1 == src2);
496 }
497
498 static void rgb24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
499                        uint32_t *unused)
500 {
501     int i;
502     for (i=0; i<width; i++) {
503         int r= src[i*3+0];
504         int g= src[i*3+1];
505         int b= src[i*3+2];
506
507         dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
508     }
509 }
510
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)
513 {
514     int i;
515     assert(src1==src2);
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];
520
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);
523     }
524 }
525
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)
528 {
529     int i;
530     assert(src1==src2);
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];
535
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);
538     }
539 }
540
541 static void planar_rgb_to_y(uint16_t *dst, const uint8_t *src[4], int width)
542 {
543     int i;
544     for (i = 0; i < width; i++) {
545         int g = src[0][i];
546         int b = src[1][i];
547         int r = src[2][i];
548
549         dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
550     }
551 }
552
553 static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
554 {
555     int i;
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);
562
563         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
564     }
565 }
566
567 static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
568 {
569     int i;
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);
576
577         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
578     }
579 }
580
581 static void planar_rgb_to_uv(uint16_t *dstU, uint16_t *dstV, const uint8_t *src[4], int width)
582 {
583     int i;
584     for (i = 0; i < width; i++) {
585         int g = src[0][i];
586         int b = src[1][i];
587         int r = src[2][i];
588
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);
591     }
592 }
593
594 static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
595 {
596     int i;
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);
604
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);
607     }
608 }
609
610 static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
611 {
612     int i;
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);
620
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);
623     }
624 }
625
626 av_cold void ff_sws_init_input_funcs(SwsContext *c)
627 {
628     enum PixelFormat srcFormat = c->srcFormat;
629
630     c->chrToYV12 = NULL;
631     switch(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;
636         case PIX_FMT_RGB8     :
637         case PIX_FMT_BGR8     :
638         case PIX_FMT_PAL8     :
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;
648 #if HAVE_BIGENDIAN
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;
658 #else
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;
668 #endif
669     }
670     if (c->chrSrcHSubSample) {
671         switch(srcFormat) {
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;
695         }
696     } else {
697         switch(srcFormat) {
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;
720         }
721     }
722
723     c->lumToYV12 = NULL;
724     c->alpToYV12 = NULL;
725     switch (srcFormat) {
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;
733 #if HAVE_BIGENDIAN
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;
744 #else
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;
755 #endif
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;
773     case PIX_FMT_RGB8     :
774     case PIX_FMT_BGR8     :
775     case PIX_FMT_PAL8     :
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;
788     }
789     if (c->alpPixBuf) {
790         switch (srcFormat) {
791         case PIX_FMT_BGRA:
792         case PIX_FMT_RGBA:  c->alpToYV12 = rgbaToA_c; break;
793         case PIX_FMT_ABGR:
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;
797         }
798     }
799 }