]> git.sesse.net Git - ffmpeg/blob - libswscale/input.c
ttadec: fix invalid free when an error occurs while decoding 24-bit tta
[ffmpeg] / libswscale / input.c
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
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, \
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 *_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 *_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(uint8_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 = 33u << (S - 1);
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;
162     }
163 }
164
165 static av_always_inline void
166 rgb16_32ToUV_c_template(uint8_t *dstU, uint8_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 = 257u << (S - 1);
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;
185         dstV[i] = (rv * r + gv * g + bv * b + rnd) >> S;
186     }
187 }
188
189 static av_always_inline void
190 rgb16_32ToUV_half_c_template(uint8_t *dstU, uint8_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 = 257u << S;
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 + rnd) >> (S + 1);
220         dstV[i] = (rv * r + gv * g + bv * b + rnd) >> (S + 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, \
229                           int width, uint32_t *unused) \
230 { \
231     rgb16_32ToY_c_template(dst, src, width, fmt, shr, shg, shb, shp, \
232                            maskr, maskg, maskb, rsh, gsh, bsh, S); \
233 } \
234  \
235 static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
236                            const uint8_t *src, const uint8_t *dummy, \
237                            int width, uint32_t *unused) \
238 { \
239     rgb16_32ToUV_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
240                             maskr, maskg, maskb, rsh, gsh, bsh, S); \
241 } \
242  \
243 static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
244                                 const uint8_t *src, const uint8_t *dummy, \
245                                 int width, uint32_t *unused) \
246 { \
247     rgb16_32ToUV_half_c_template(dstU, dstV, src, width, fmt, shr, shg, shb, shp, \
248                                  maskr, maskg, maskb, rsh, gsh, bsh, S); \
249 }
250
251 rgb16_32_wrapper(PIX_FMT_BGR32,    bgr32,  16, 0,  0, 0, 0xFF0000, 0xFF00,   0x00FF,  8, 0,  8, RGB2YUV_SHIFT+8)
252 rgb16_32_wrapper(PIX_FMT_BGR32_1,  bgr321, 16, 0,  0, 8, 0xFF0000, 0xFF00,   0x00FF,  8, 0,  8, RGB2YUV_SHIFT+8)
253 rgb16_32_wrapper(PIX_FMT_RGB32,    rgb32,   0, 0, 16, 0,   0x00FF, 0xFF00, 0xFF0000,  8, 0,  8, RGB2YUV_SHIFT+8)
254 rgb16_32_wrapper(PIX_FMT_RGB32_1,  rgb321,  0, 0, 16, 8,   0x00FF, 0xFF00, 0xFF0000,  8, 0,  8, RGB2YUV_SHIFT+8)
255 rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0,  0, 0,   0x001F, 0x07E0,   0xF800, 11, 5,  0, RGB2YUV_SHIFT+8)
256 rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0,  0, 0,   0x001F, 0x03E0,   0x7C00, 10, 5,  0, RGB2YUV_SHIFT+7)
257 rgb16_32_wrapper(PIX_FMT_BGR444LE, bgr12le, 0, 0,  0, 0,   0x000F, 0x00F0,   0x0F00,  8, 4,  0, RGB2YUV_SHIFT+4)
258 rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0,  0, 0,   0xF800, 0x07E0,   0x001F,  0, 5, 11, RGB2YUV_SHIFT+8)
259 rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0,  0, 0,   0x7C00, 0x03E0,   0x001F,  0, 5, 10, RGB2YUV_SHIFT+7)
260 rgb16_32_wrapper(PIX_FMT_RGB444LE, rgb12le, 0, 0,  0, 0,   0x0F00, 0x00F0,   0x000F,  0, 4,  8, RGB2YUV_SHIFT+4)
261 rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0,  0, 0,   0x001F, 0x07E0,   0xF800, 11, 5,  0, RGB2YUV_SHIFT+8)
262 rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0,  0, 0,   0x001F, 0x03E0,   0x7C00, 10, 5,  0, RGB2YUV_SHIFT+7)
263 rgb16_32_wrapper(PIX_FMT_BGR444BE, bgr12be, 0, 0,  0, 0,   0x000F, 0x00F0,   0x0F00,  8, 4,  0, RGB2YUV_SHIFT+4)
264 rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0,  0, 0,   0xF800, 0x07E0,   0x001F,  0, 5, 11, RGB2YUV_SHIFT+8)
265 rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0,  0, 0,   0x7C00, 0x03E0,   0x001F,  0, 5, 10, RGB2YUV_SHIFT+7)
266 rgb16_32_wrapper(PIX_FMT_RGB444BE, rgb12be, 0, 0,  0, 0,   0x0F00, 0x00F0,   0x000F,  0, 4,  8, RGB2YUV_SHIFT+4)
267
268 static void abgrToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
269 {
270     int i;
271     for (i=0; i<width; i++) {
272         dst[i]= src[4*i];
273     }
274 }
275
276 static void rgbaToA_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
277 {
278     int i;
279     for (i=0; i<width; i++) {
280         dst[i]= src[4*i+3];
281     }
282 }
283
284 static void palToY_c(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
285 {
286     int i;
287     for (i=0; i<width; i++) {
288         int d= src[i];
289
290         dst[i]= pal[d] & 0xFF;
291     }
292 }
293
294 static void palToUV_c(uint8_t *dstU, uint8_t *dstV,
295                       const uint8_t *src1, const uint8_t *src2,
296                       int width, uint32_t *pal)
297 {
298     int i;
299     assert(src1 == src2);
300     for (i=0; i<width; i++) {
301         int p= pal[src1[i]];
302
303         dstU[i]= p>>8;
304         dstV[i]= p>>16;
305     }
306 }
307
308 static void monowhite2Y_c(uint8_t *dst, const uint8_t *src,
309                           int width, uint32_t *unused)
310 {
311     int i, j;
312     for (i=0; i<width/8; i++) {
313         int d= ~src[i];
314         for(j=0; j<8; j++)
315             dst[8*i+j]= ((d>>(7-j))&1)*255;
316     }
317 }
318
319 static void monoblack2Y_c(uint8_t *dst, const uint8_t *src,
320                           int width, uint32_t *unused)
321 {
322     int i, j;
323     for (i=0; i<width/8; i++) {
324         int d= src[i];
325         for(j=0; j<8; j++)
326             dst[8*i+j]= ((d>>(7-j))&1)*255;
327     }
328 }
329
330 static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, int width,
331                       uint32_t *unused)
332 {
333     int i;
334     for (i=0; i<width; i++)
335         dst[i]= src[2*i];
336 }
337
338 static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
339                        const uint8_t *src2, int width, uint32_t *unused)
340 {
341     int i;
342     for (i=0; i<width; i++) {
343         dstU[i]= src1[4*i + 1];
344         dstV[i]= src1[4*i + 3];
345     }
346     assert(src1 == src2);
347 }
348
349 static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, int width, uint32_t *unused)
350 {
351     int i;
352     const uint16_t *src = (const uint16_t *) _src;
353     uint16_t *dst = (uint16_t *) _dst;
354     for (i=0; i<width; i++) {
355         dst[i] = av_bswap16(src[i]);
356     }
357 }
358
359 static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src1,
360                         const uint8_t *_src2, int width, uint32_t *unused)
361 {
362     int i;
363     const uint16_t *src1 = (const uint16_t *) _src1,
364                    *src2 = (const uint16_t *) _src2;
365     uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
366     for (i=0; i<width; i++) {
367         dstU[i] = av_bswap16(src1[i]);
368         dstV[i] = av_bswap16(src2[i]);
369     }
370 }
371
372 /* This is almost identical to the previous, end exists only because
373  * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
374 static void uyvyToY_c(uint8_t *dst, const uint8_t *src, int width,
375                       uint32_t *unused)
376 {
377     int i;
378     for (i=0; i<width; i++)
379         dst[i]= src[2*i+1];
380 }
381
382 static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
383                        const uint8_t *src2, int width, uint32_t *unused)
384 {
385     int i;
386     for (i=0; i<width; i++) {
387         dstU[i]= src1[4*i + 0];
388         dstV[i]= src1[4*i + 2];
389     }
390     assert(src1 == src2);
391 }
392
393 static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
394                                         const uint8_t *src, int width)
395 {
396     int i;
397     for (i = 0; i < width; i++) {
398         dst1[i] = src[2*i+0];
399         dst2[i] = src[2*i+1];
400     }
401 }
402
403 static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
404                        const uint8_t *src1, const uint8_t *src2,
405                        int width, uint32_t *unused)
406 {
407     nvXXtoUV_c(dstU, dstV, src1, width);
408 }
409
410 static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
411                        const uint8_t *src1, const uint8_t *src2,
412                        int width, uint32_t *unused)
413 {
414     nvXXtoUV_c(dstV, dstU, src1, width);
415 }
416
417 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
418
419 static void bgr24ToY_c(uint8_t *dst, const uint8_t *src,
420                        int width, uint32_t *unused)
421 {
422     int i;
423     for (i=0; i<width; i++) {
424         int b= src[i*3+0];
425         int g= src[i*3+1];
426         int r= src[i*3+2];
427
428         dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
429     }
430 }
431
432 static void bgr24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
433                         const uint8_t *src2, int width, uint32_t *unused)
434 {
435     int i;
436     for (i=0; i<width; i++) {
437         int b= src1[3*i + 0];
438         int g= src1[3*i + 1];
439         int r= src1[3*i + 2];
440
441         dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
442         dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
443     }
444     assert(src1 == src2);
445 }
446
447 static void bgr24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
448                              const uint8_t *src2, int width, uint32_t *unused)
449 {
450     int i;
451     for (i=0; i<width; i++) {
452         int b= src1[6*i + 0] + src1[6*i + 3];
453         int g= src1[6*i + 1] + src1[6*i + 4];
454         int r= src1[6*i + 2] + src1[6*i + 5];
455
456         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
457         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
458     }
459     assert(src1 == src2);
460 }
461
462 static void rgb24ToY_c(uint8_t *dst, const uint8_t *src, int width,
463                        uint32_t *unused)
464 {
465     int i;
466     for (i=0; i<width; i++) {
467         int r= src[i*3+0];
468         int g= src[i*3+1];
469         int b= src[i*3+2];
470
471         dst[i]= ((RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
472     }
473 }
474
475 static void rgb24ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
476                         const uint8_t *src2, int width, uint32_t *unused)
477 {
478     int i;
479     assert(src1==src2);
480     for (i=0; i<width; i++) {
481         int r= src1[3*i + 0];
482         int g= src1[3*i + 1];
483         int b= src1[3*i + 2];
484
485         dstU[i]= (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
486         dstV[i]= (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT;
487     }
488 }
489
490 static void rgb24ToUV_half_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1,
491                              const uint8_t *src2, int width, uint32_t *unused)
492 {
493     int i;
494     assert(src1==src2);
495     for (i=0; i<width; i++) {
496         int r= src1[6*i + 0] + src1[6*i + 3];
497         int g= src1[6*i + 1] + src1[6*i + 4];
498         int b= src1[6*i + 2] + src1[6*i + 5];
499
500         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
501         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT))>>(RGB2YUV_SHIFT+1);
502     }
503 }
504
505 static void planar_rgb_to_y(uint8_t *dst, const uint8_t *src[4], int width)
506 {
507     int i;
508     for (i = 0; i < width; i++) {
509         int g = src[0][i];
510         int b = src[1][i];
511         int r = src[2][i];
512
513         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
514     }
515 }
516
517 static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
518 {
519     int i;
520     const uint16_t **src = (const uint16_t **) _src;
521     uint16_t *dst = (uint16_t *) _dst;
522     for (i = 0; i < width; i++) {
523         int g = AV_RL16(src[0] + i);
524         int b = AV_RL16(src[1] + i);
525         int r = AV_RL16(src[2] + i);
526
527         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
528     }
529 }
530
531 static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
532 {
533     int i;
534     const uint16_t **src = (const uint16_t **) _src;
535     uint16_t *dst = (uint16_t *) _dst;
536     for (i = 0; i < width; i++) {
537         int g = AV_RB16(src[0] + i);
538         int b = AV_RB16(src[1] + i);
539         int r = AV_RB16(src[2] + i);
540
541         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
542     }
543 }
544
545 static void planar_rgb_to_uv(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4], int width)
546 {
547     int i;
548     for (i = 0; i < width; i++) {
549         int g = src[0][i];
550         int b = src[1][i];
551         int r = src[2][i];
552
553         dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
554         dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
555     }
556 }
557
558 static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
559 {
560     int i;
561     const uint16_t **src = (const uint16_t **) _src;
562     uint16_t *dstU = (uint16_t *) _dstU;
563     uint16_t *dstV = (uint16_t *) _dstV;
564     for (i = 0; i < width; i++) {
565         int g = AV_RL16(src[0] + i);
566         int b = AV_RL16(src[1] + i);
567         int r = AV_RL16(src[2] + i);
568
569         dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
570         dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
571     }
572 }
573
574 static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
575 {
576     int i;
577     const uint16_t **src = (const uint16_t **) _src;
578     uint16_t *dstU = (uint16_t *) _dstU;
579     uint16_t *dstV = (uint16_t *) _dstV;
580     for (i = 0; i < width; i++) {
581         int g = AV_RB16(src[0] + i);
582         int b = AV_RB16(src[1] + i);
583         int r = AV_RB16(src[2] + i);
584
585         dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
586         dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
587     }
588 }
589
590 av_cold void ff_sws_init_input_funcs(SwsContext *c)
591 {
592     enum PixelFormat srcFormat = c->srcFormat;
593
594     c->chrToYV12 = NULL;
595     switch(srcFormat) {
596         case PIX_FMT_YUYV422  : c->chrToYV12 = yuy2ToUV_c; break;
597         case PIX_FMT_UYVY422  : c->chrToYV12 = uyvyToUV_c; break;
598         case PIX_FMT_NV12     : c->chrToYV12 = nv12ToUV_c; break;
599         case PIX_FMT_NV21     : c->chrToYV12 = nv21ToUV_c; break;
600         case PIX_FMT_RGB8     :
601         case PIX_FMT_BGR8     :
602         case PIX_FMT_PAL8     :
603         case PIX_FMT_BGR4_BYTE:
604         case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
605         case PIX_FMT_GBRP9LE:
606         case PIX_FMT_GBRP10LE:
607         case PIX_FMT_GBRP16LE:  c->readChrPlanar = planar_rgb16le_to_uv; break;
608         case PIX_FMT_GBRP9BE:
609         case PIX_FMT_GBRP10BE:
610         case PIX_FMT_GBRP16BE:  c->readChrPlanar = planar_rgb16be_to_uv; break;
611         case PIX_FMT_GBRP:      c->readChrPlanar = planar_rgb_to_uv; break;
612 #if HAVE_BIGENDIAN
613         case PIX_FMT_YUV444P9LE:
614         case PIX_FMT_YUV422P9LE:
615         case PIX_FMT_YUV420P9LE:
616         case PIX_FMT_YUV422P10LE:
617         case PIX_FMT_YUV444P10LE:
618         case PIX_FMT_YUV420P10LE:
619         case PIX_FMT_YUV420P16LE:
620         case PIX_FMT_YUV422P16LE:
621         case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
622 #else
623         case PIX_FMT_YUV444P9BE:
624         case PIX_FMT_YUV422P9BE:
625         case PIX_FMT_YUV420P9BE:
626         case PIX_FMT_YUV444P10BE:
627         case PIX_FMT_YUV422P10BE:
628         case PIX_FMT_YUV420P10BE:
629         case PIX_FMT_YUV420P16BE:
630         case PIX_FMT_YUV422P16BE:
631         case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
632 #endif
633     }
634     if (c->chrSrcHSubSample) {
635         switch(srcFormat) {
636         case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
637         case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
638         case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
639         case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
640         case PIX_FMT_RGB32   : c->chrToYV12 = bgr32ToUV_half_c;   break;
641         case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c;  break;
642         case PIX_FMT_BGR24   : c->chrToYV12 = bgr24ToUV_half_c;   break;
643         case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
644         case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
645         case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
646         case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
647         case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_half_c; break;
648         case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_half_c; break;
649         case PIX_FMT_BGR32   : c->chrToYV12 = rgb32ToUV_half_c;   break;
650         case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c;  break;
651         case PIX_FMT_RGB24   : c->chrToYV12 = rgb24ToUV_half_c;   break;
652         case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
653         case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
654         case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
655         case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
656         case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_half_c; break;
657         case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_half_c; break;
658         }
659     } else {
660         switch(srcFormat) {
661         case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
662         case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
663         case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
664         case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
665         case PIX_FMT_RGB32   : c->chrToYV12 = bgr32ToUV_c;   break;
666         case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c;  break;
667         case PIX_FMT_BGR24   : c->chrToYV12 = bgr24ToUV_c;   break;
668         case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
669         case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
670         case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
671         case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
672         case PIX_FMT_BGR444LE: c->chrToYV12 = bgr12leToUV_c; break;
673         case PIX_FMT_BGR444BE: c->chrToYV12 = bgr12beToUV_c; break;
674         case PIX_FMT_BGR32   : c->chrToYV12 = rgb32ToUV_c;   break;
675         case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c;  break;
676         case PIX_FMT_RGB24   : c->chrToYV12 = rgb24ToUV_c;   break;
677         case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
678         case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
679         case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
680         case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
681         case PIX_FMT_RGB444LE: c->chrToYV12 = rgb12leToUV_c; break;
682         case PIX_FMT_RGB444BE: c->chrToYV12 = rgb12beToUV_c; break;
683         }
684     }
685
686     c->lumToYV12 = NULL;
687     c->alpToYV12 = NULL;
688     switch (srcFormat) {
689     case PIX_FMT_GBRP9LE:
690     case PIX_FMT_GBRP10LE:
691     case PIX_FMT_GBRP16LE: c->readLumPlanar = planar_rgb16le_to_y; break;
692     case PIX_FMT_GBRP9BE:
693     case PIX_FMT_GBRP10BE:
694     case PIX_FMT_GBRP16BE: c->readLumPlanar = planar_rgb16be_to_y; break;
695     case PIX_FMT_GBRP:     c->readLumPlanar = planar_rgb_to_y; break;
696 #if HAVE_BIGENDIAN
697     case PIX_FMT_YUV444P9LE:
698     case PIX_FMT_YUV422P9LE:
699     case PIX_FMT_YUV420P9LE:
700     case PIX_FMT_YUV444P10LE:
701     case PIX_FMT_YUV422P10LE:
702     case PIX_FMT_YUV420P10LE:
703     case PIX_FMT_YUV420P16LE:
704     case PIX_FMT_YUV422P16LE:
705     case PIX_FMT_YUV444P16LE:
706     case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
707 #else
708     case PIX_FMT_YUV444P9BE:
709     case PIX_FMT_YUV422P9BE:
710     case PIX_FMT_YUV420P9BE:
711     case PIX_FMT_YUV444P10BE:
712     case PIX_FMT_YUV422P10BE:
713     case PIX_FMT_YUV420P10BE:
714     case PIX_FMT_YUV420P16BE:
715     case PIX_FMT_YUV422P16BE:
716     case PIX_FMT_YUV444P16BE:
717     case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
718 #endif
719     case PIX_FMT_YUYV422  :
720     case PIX_FMT_Y400A    : c->lumToYV12 = yuy2ToY_c; break;
721     case PIX_FMT_UYVY422  : c->lumToYV12 = uyvyToY_c;    break;
722     case PIX_FMT_BGR24    : c->lumToYV12 = bgr24ToY_c;   break;
723     case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
724     case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
725     case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
726     case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
727     case PIX_FMT_BGR444LE : c->lumToYV12 = bgr12leToY_c; break;
728     case PIX_FMT_BGR444BE : c->lumToYV12 = bgr12beToY_c; break;
729     case PIX_FMT_RGB24    : c->lumToYV12 = rgb24ToY_c;   break;
730     case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
731     case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
732     case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
733     case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
734     case PIX_FMT_RGB444LE : c->lumToYV12 = rgb12leToY_c; break;
735     case PIX_FMT_RGB444BE : c->lumToYV12 = rgb12beToY_c; break;
736     case PIX_FMT_RGB8     :
737     case PIX_FMT_BGR8     :
738     case PIX_FMT_PAL8     :
739     case PIX_FMT_BGR4_BYTE:
740     case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
741     case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
742     case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
743     case PIX_FMT_RGB32  : c->lumToYV12 = bgr32ToY_c;  break;
744     case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
745     case PIX_FMT_BGR32  : c->lumToYV12 = rgb32ToY_c;  break;
746     case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
747     case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
748     case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
749     case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
750     case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
751     }
752     if (c->alpPixBuf) {
753         switch (srcFormat) {
754         case PIX_FMT_BGRA:
755         case PIX_FMT_RGBA:  c->alpToYV12 = rgbaToA_c; break;
756         case PIX_FMT_ABGR:
757         case PIX_FMT_ARGB:  c->alpToYV12 = abgrToA_c; break;
758         case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
759         }
760     }
761 }