]> 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 rgb64ToY_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*4+0]);
61         unsigned int   g = input_pixel(&src[i*4+1]);
62         unsigned int b_r = input_pixel(&src[i*4+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 rgb64ToUV_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*4+0]);
77         int   g = input_pixel(&src1[i*4+1]);
78         int b_r = input_pixel(&src1[i*4+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 rgb64ToUV_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[8 * i + 0]) + input_pixel(&src1[8 * i + 4]) + 1) >> 1;
94         int   g = (input_pixel(&src1[8 * i + 1]) + input_pixel(&src1[8 * i + 5]) + 1) >> 1;
95         int b_r = (input_pixel(&src1[8 * i + 2]) + input_pixel(&src1[8 * i + 6]) + 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 #define rgb64funcs(pattern, BE_LE, origin) \
103 static void pattern ## 64 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused0, const uint8_t *unused1,\
104                                     int width, uint32_t *unused) \
105 { \
106     const uint16_t *src = (const uint16_t *) _src; \
107     uint16_t *dst = (uint16_t *) _dst; \
108     rgb64ToY_c_template(dst, src, width, origin); \
109 } \
110  \
111 static void pattern ## 64 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
112                                     const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
113                                     int width, uint32_t *unused) \
114 { \
115     const uint16_t *src1 = (const uint16_t *) _src1, \
116                    *src2 = (const uint16_t *) _src2; \
117     uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
118     rgb64ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
119 } \
120  \
121 static void pattern ## 64 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
122                                     const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
123                                     int width, uint32_t *unused) \
124 { \
125     const uint16_t *src1 = (const uint16_t *) _src1, \
126                    *src2 = (const uint16_t *) _src2; \
127     uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
128     rgb64ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
129 }
130
131 rgb64funcs(rgb, LE, PIX_FMT_RGBA64LE)
132 rgb64funcs(rgb, BE, PIX_FMT_RGBA64BE)
133
134 static av_always_inline void rgb48ToY_c_template(uint16_t *dst,
135                                                  const uint16_t *src, int width,
136                                                  enum PixelFormat origin)
137 {
138     int i;
139     for (i = 0; i < width; i++) {
140         unsigned int r_b = input_pixel(&src[i * 3 + 0]);
141         unsigned int g   = input_pixel(&src[i * 3 + 1]);
142         unsigned int b_r = input_pixel(&src[i * 3 + 2]);
143
144         dst[i] = (RY * r + GY * g + BY * b + (0x2001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
145     }
146 }
147
148 static av_always_inline void rgb48ToUV_c_template(uint16_t *dstU,
149                                                   uint16_t *dstV,
150                                                   const uint16_t *src1,
151                                                   const uint16_t *src2,
152                                                   int width,
153                                                   enum PixelFormat origin)
154 {
155     int i;
156     assert(src1 == src2);
157     for (i = 0; i < width; i++) {
158         int r_b = input_pixel(&src1[i * 3 + 0]);
159         int g   = input_pixel(&src1[i * 3 + 1]);
160         int b_r = input_pixel(&src1[i * 3 + 2]);
161
162         dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
163         dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
164     }
165 }
166
167 static av_always_inline void rgb48ToUV_half_c_template(uint16_t *dstU,
168                                                        uint16_t *dstV,
169                                                        const uint16_t *src1,
170                                                        const uint16_t *src2,
171                                                        int width,
172                                                        enum PixelFormat origin)
173 {
174     int i;
175     assert(src1 == src2);
176     for (i = 0; i < width; i++) {
177         int r_b = (input_pixel(&src1[6 * i + 0]) +
178                    input_pixel(&src1[6 * i + 3]) + 1) >> 1;
179         int g   = (input_pixel(&src1[6 * i + 1]) +
180                    input_pixel(&src1[6 * i + 4]) + 1) >> 1;
181         int b_r = (input_pixel(&src1[6 * i + 2]) +
182                    input_pixel(&src1[6 * i + 5]) + 1) >> 1;
183
184         dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
185         dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
186     }
187 }
188
189 #undef r
190 #undef b
191 #undef input_pixel
192
193 #define rgb48funcs(pattern, BE_LE, origin)                              \
194 static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst,              \
195                                             const uint8_t *_src,        \
196                                             const uint8_t *unused0, const uint8_t *unused1,\
197                                             int width,                  \
198                                             uint32_t *unused)           \
199 {                                                                       \
200     const uint16_t *src = (const uint16_t *)_src;                       \
201     uint16_t *dst       = (uint16_t *)_dst;                             \
202     rgb48ToY_c_template(dst, src, width, origin);                       \
203 }                                                                       \
204                                                                         \
205 static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU,            \
206                                              uint8_t *_dstV,            \
207                                              const uint8_t *unused0,    \
208                                              const uint8_t *_src1,      \
209                                              const uint8_t *_src2,      \
210                                              int width,                 \
211                                              uint32_t *unused)          \
212 {                                                                       \
213     const uint16_t *src1 = (const uint16_t *)_src1,                     \
214                    *src2 = (const uint16_t *)_src2;                     \
215     uint16_t *dstU = (uint16_t *)_dstU,                                 \
216              *dstV = (uint16_t *)_dstV;                                 \
217     rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin);        \
218 }                                                                       \
219                                                                         \
220 static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU,       \
221                                                   uint8_t *_dstV,       \
222                                                   const uint8_t *unused0,    \
223                                                   const uint8_t *_src1, \
224                                                   const uint8_t *_src2, \
225                                                   int width,            \
226                                                   uint32_t *unused)     \
227 {                                                                       \
228     const uint16_t *src1 = (const uint16_t *)_src1,                     \
229                    *src2 = (const uint16_t *)_src2;                     \
230     uint16_t *dstU = (uint16_t *)_dstU,                                 \
231              *dstV = (uint16_t *)_dstV;                                 \
232     rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin);   \
233 }
234
235 rgb48funcs(rgb, LE, PIX_FMT_RGB48LE)
236 rgb48funcs(rgb, BE, PIX_FMT_RGB48BE)
237 rgb48funcs(bgr, LE, PIX_FMT_BGR48LE)
238 rgb48funcs(bgr, BE, PIX_FMT_BGR48BE)
239
240 #define input_pixel(i) ((origin == PIX_FMT_RGBA ||                      \
241                          origin == PIX_FMT_BGRA ||                      \
242                          origin == PIX_FMT_ARGB ||                      \
243                          origin == PIX_FMT_ABGR)                        \
244                         ? AV_RN32A(&src[(i) * 4])                       \
245                         : (isBE(origin) ? AV_RB16(&src[(i) * 2])        \
246                                         : AV_RL16(&src[(i) * 2])))
247
248 static av_always_inline void rgb16_32ToY_c_template(int16_t *dst,
249                                                     const uint8_t *src,
250                                                     int width,
251                                                     enum PixelFormat origin,
252                                                     int shr, int shg,
253                                                     int shb, int shp,
254                                                     int maskr, int maskg,
255                                                     int maskb, int rsh,
256                                                     int gsh, int bsh, int S)
257 {
258     const int ry       = RY << rsh, gy = GY << gsh, by = BY << bsh;
259     const unsigned rnd = (32<<((S)-1)) + (1<<(S-7));
260     int i;
261
262     for (i = 0; i < width; i++) {
263         int px = input_pixel(i) >> shp;
264         int b  = (px & maskb) >> shb;
265         int g  = (px & maskg) >> shg;
266         int r  = (px & maskr) >> shr;
267
268         dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
269     }
270 }
271
272 static av_always_inline void rgb16_32ToUV_c_template(int16_t *dstU,
273                                                      int16_t *dstV,
274                                                      const uint8_t *src,
275                                                      int width,
276                                                      enum PixelFormat origin,
277                                                      int shr, int shg,
278                                                      int shb, int shp,
279                                                      int maskr, int maskg,
280                                                      int maskb, int rsh,
281                                                      int gsh, int bsh, int S)
282 {
283     const int ru       = RU << rsh, gu = GU << gsh, bu = BU << bsh,
284               rv       = RV << rsh, gv = GV << gsh, bv = BV << bsh;
285     const unsigned rnd = (256u<<((S)-1)) + (1<<(S-7));
286     int i;
287
288     for (i = 0; i < width; i++) {
289         int px = input_pixel(i) >> shp;
290         int b  = (px & maskb)   >> shb;
291         int g  = (px & maskg)   >> shg;
292         int r  = (px & maskr)   >> shr;
293
294         dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
295         dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
296     }
297 }
298
299 static av_always_inline void rgb16_32ToUV_half_c_template(int16_t *dstU,
300                                                           int16_t *dstV,
301                                                           const uint8_t *src,
302                                                           int width,
303                                                           enum PixelFormat origin,
304                                                           int shr, int shg,
305                                                           int shb, int shp,
306                                                           int maskr, int maskg,
307                                                           int maskb, int rsh,
308                                                           int gsh, int bsh, int S)
309 {
310     const int ru       = RU << rsh, gu = GU << gsh, bu = BU << bsh,
311               rv       = RV << rsh, gv = GV << gsh, bv = BV << bsh,
312               maskgx   = ~(maskr | maskb);
313     const unsigned rnd = (256U<<(S)) + (1<<(S-6));
314     int i;
315
316     maskr |= maskr << 1;
317     maskb |= maskb << 1;
318     maskg |= maskg << 1;
319     for (i = 0; i < width; i++) {
320         int px0 = input_pixel(2 * i + 0) >> shp;
321         int px1 = input_pixel(2 * i + 1) >> shp;
322         int b, r, g = (px0 & maskgx) + (px1 & maskgx);
323         int rb = px0 + px1 - g;
324
325         b = (rb & maskb) >> shb;
326         if (shp ||
327             origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
328             origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
329             g >>= shg;
330         } else {
331             g = (g & maskg) >> shg;
332         }
333         r = (rb & maskr) >> shr;
334
335         dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
336         dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
337     }
338 }
339
340 #undef input_pixel
341
342 #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr,          \
343                          maskg, maskb, rsh, gsh, bsh, S)                \
344 static void name ## ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,            \
345                           int width, uint32_t *unused)                  \
346 {                                                                       \
347     rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, shr, shg, shb, shp,    \
348                            maskr, maskg, maskb, rsh, gsh, bsh, S);      \
349 }                                                                       \
350                                                                         \
351 static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV,                \
352                            const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy,    \
353                            int width, uint32_t *unused)                 \
354 {                                                                       \
355     rgb16_32ToUV_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt,                \
356                             shr, shg, shb, shp,                         \
357                             maskr, maskg, maskb, rsh, gsh, bsh, S);     \
358 }                                                                       \
359                                                                         \
360 static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV,           \
361                                 const uint8_t *unused0, const uint8_t *src,                     \
362                                 const uint8_t *dummy,                   \
363                                 int width, uint32_t *unused)            \
364 {                                                                       \
365     rgb16_32ToUV_half_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt,           \
366                                  shr, shg, shb, shp,                    \
367                                  maskr, maskg, maskb,                   \
368                                  rsh, gsh, bsh, S);                     \
369 }
370
371 rgb16_32_wrapper(PIX_FMT_BGR32,    bgr32,  16, 0,  0, 0, 0xFF0000, 0xFF00,   0x00FF,  8, 0,  8, RGB2YUV_SHIFT + 8)
372 rgb16_32_wrapper(PIX_FMT_BGR32_1,  bgr321, 16, 0,  0, 8, 0xFF0000, 0xFF00,   0x00FF,  8, 0,  8, RGB2YUV_SHIFT + 8)
373 rgb16_32_wrapper(PIX_FMT_RGB32,    rgb32,   0, 0, 16, 0,   0x00FF, 0xFF00, 0xFF0000,  8, 0,  8, RGB2YUV_SHIFT + 8)
374 rgb16_32_wrapper(PIX_FMT_RGB32_1,  rgb321,  0, 0, 16, 8,   0x00FF, 0xFF00, 0xFF0000,  8, 0,  8, RGB2YUV_SHIFT + 8)
375 rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0,  0, 0,   0x001F, 0x07E0,   0xF800, 11, 5,  0, RGB2YUV_SHIFT + 8)
376 rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0,  0, 0,   0x001F, 0x03E0,   0x7C00, 10, 5,  0, RGB2YUV_SHIFT + 7)
377 rgb16_32_wrapper(PIX_FMT_BGR444LE, bgr12le, 0, 0,  0, 0,   0x000F, 0x00F0,   0x0F00,  8, 4,  0, RGB2YUV_SHIFT + 4)
378 rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0,  0, 0,   0xF800, 0x07E0,   0x001F,  0, 5, 11, RGB2YUV_SHIFT + 8)
379 rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0,  0, 0,   0x7C00, 0x03E0,   0x001F,  0, 5, 10, RGB2YUV_SHIFT + 7)
380 rgb16_32_wrapper(PIX_FMT_RGB444LE, rgb12le, 0, 0,  0, 0,   0x0F00, 0x00F0,   0x000F,  0, 4,  8, RGB2YUV_SHIFT + 4)
381 rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0,  0, 0,   0x001F, 0x07E0,   0xF800, 11, 5,  0, RGB2YUV_SHIFT + 8)
382 rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0,  0, 0,   0x001F, 0x03E0,   0x7C00, 10, 5,  0, RGB2YUV_SHIFT + 7)
383 rgb16_32_wrapper(PIX_FMT_BGR444BE, bgr12be, 0, 0,  0, 0,   0x000F, 0x00F0,   0x0F00,  8, 4,  0, RGB2YUV_SHIFT + 4)
384 rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0,  0, 0,   0xF800, 0x07E0,   0x001F,  0, 5, 11, RGB2YUV_SHIFT + 8)
385 rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0,  0, 0,   0x7C00, 0x03E0,   0x001F,  0, 5, 10, RGB2YUV_SHIFT + 7)
386 rgb16_32_wrapper(PIX_FMT_RGB444BE, rgb12be, 0, 0,  0, 0,   0x0F00, 0x00F0,   0x000F,  0, 4,  8, RGB2YUV_SHIFT + 4)
387
388 static void gbr24pToUV_half_c(uint16_t *dstU, uint16_t *dstV,
389                          const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
390                          int width, enum PixelFormat origin)
391 {
392     int i;
393     for (i = 0; i < width; i++) {
394         unsigned int g   = gsrc[2*i] + gsrc[2*i+1];
395         unsigned int b   = bsrc[2*i] + bsrc[2*i+1];
396         unsigned int r   = rsrc[2*i] + rsrc[2*i+1];
397
398         dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
399         dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
400     }
401 }
402
403 static void rgba64ToA_c(int16_t *dst, const uint16_t *src, const uint8_t *unused1,
404                         const uint8_t *unused2, int width, uint32_t *unused)
405 {
406     int i;
407     for (i = 0; i < width; i++)
408         dst[i] = src[4 * i + 3];
409 }
410
411 static void abgrToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
412 {
413     int i;
414     for (i=0; i<width; i++) {
415         dst[i]= src[4*i]<<6;
416     }
417 }
418
419 static void rgbaToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
420 {
421     int i;
422     for (i=0; i<width; i++) {
423         dst[i]= src[4*i+3]<<6;
424     }
425 }
426
427 static void palToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *pal)
428 {
429     int i;
430     for (i=0; i<width; i++) {
431         int d= src[i];
432
433         dst[i]= (pal[d] >> 24)<<6;
434     }
435 }
436
437 static void palToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, long width, uint32_t *pal)
438 {
439     int i;
440     for (i = 0; i < width; i++) {
441         int d = src[i];
442
443         dst[i] = (pal[d] & 0xFF)<<6;
444     }
445 }
446
447 static void palToUV_c(uint16_t *dstU, int16_t *dstV,
448                            const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
449                       int width, uint32_t *pal)
450 {
451     int i;
452     assert(src1 == src2);
453     for (i = 0; i < width; i++) {
454         int p = pal[src1[i]];
455
456         dstU[i] = (uint8_t)(p>> 8)<<6;
457         dstV[i] = (uint8_t)(p>>16)<<6;
458     }
459 }
460
461 static void monowhite2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,  int width, uint32_t *unused)
462 {
463     int i, j;
464     width = (width + 7) >> 3;
465     for (i = 0; i < width; i++) {
466         int d = ~src[i];
467         for (j = 0; j < 8; j++)
468             dst[8*i+j]= ((d>>(7-j))&1) * 16383;
469     }
470     if(width&7){
471         int d= ~src[i];
472         for (j = 0; j < (width&7); j++)
473             dst[8*i+j]= ((d>>(7-j))&1) * 16383;
474     }
475 }
476
477 static void monoblack2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,  int width, uint32_t *unused)
478 {
479     int i, j;
480     width = (width + 7) >> 3;
481     for (i = 0; i < width; i++) {
482         int d = src[i];
483         for (j = 0; j < 8; j++)
484             dst[8*i+j]= ((d>>(7-j))&1) * 16383;
485     }
486     if(width&7){
487         int d = src[i];
488         for (j = 0; j < (width&7); j++)
489             dst[8*i+j] = ((d>>(7-j))&1) * 16383;
490     }
491 }
492
493 static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,  int width,
494                       uint32_t *unused)
495 {
496     int i;
497     for (i = 0; i < width; i++)
498         dst[i] = src[2 * i];
499 }
500
501 static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
502                        const uint8_t *src2, int width, uint32_t *unused)
503 {
504     int i;
505     for (i = 0; i < width; i++) {
506         dstU[i] = src1[4 * i + 1];
507         dstV[i] = src1[4 * i + 3];
508     }
509     assert(src1 == src2);
510 }
511
512 static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused1, const uint8_t *unused2,  int width,
513                        uint32_t *unused)
514 {
515     int i;
516     const uint16_t *src = (const uint16_t *)_src;
517     uint16_t *dst       = (uint16_t *)_dst;
518     for (i = 0; i < width; i++)
519         dst[i] = av_bswap16(src[i]);
520 }
521
522 static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *_src1,
523                         const uint8_t *_src2, int width, uint32_t *unused)
524 {
525     int i;
526     const uint16_t *src1 = (const uint16_t *)_src1,
527     *src2                = (const uint16_t *)_src2;
528     uint16_t *dstU       = (uint16_t *)_dstU, *dstV = (uint16_t *)_dstV;
529     for (i = 0; i < width; i++) {
530         dstU[i] = av_bswap16(src1[i]);
531         dstV[i] = av_bswap16(src2[i]);
532     }
533 }
534
535 /* This is almost identical to the previous, end exists only because
536  * yuy2ToY/UV)(dst, src + 1, ...) would have 100% unaligned accesses. */
537 static void uyvyToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,  int width,
538                       uint32_t *unused)
539 {
540     int i;
541     for (i = 0; i < width; i++)
542         dst[i] = src[2 * i + 1];
543 }
544
545 static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
546                        const uint8_t *src2, int width, uint32_t *unused)
547 {
548     int i;
549     for (i = 0; i < width; i++) {
550         dstU[i] = src1[4 * i + 0];
551         dstV[i] = src1[4 * i + 2];
552     }
553     assert(src1 == src2);
554 }
555
556 static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
557                                         const uint8_t *src, int width)
558 {
559     int i;
560     for (i = 0; i < width; i++) {
561         dst1[i] = src[2 * i + 0];
562         dst2[i] = src[2 * i + 1];
563     }
564 }
565
566 static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
567                        const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
568                        int width, uint32_t *unused)
569 {
570     nvXXtoUV_c(dstU, dstV, src1, width);
571 }
572
573 static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
574                        const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
575                        int width, uint32_t *unused)
576 {
577     nvXXtoUV_c(dstV, dstU, src1, width);
578 }
579
580 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
581
582 static void bgr24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,
583                        int width, uint32_t *unused)
584 {
585     int i;
586     for (i = 0; i < width; i++) {
587         int b = src[i * 3 + 0];
588         int g = src[i * 3 + 1];
589         int r = src[i * 3 + 2];
590
591         dst[i] = ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
592     }
593 }
594
595 static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
596                         const uint8_t *src2, int width, uint32_t *unused)
597 {
598     int i;
599     for (i = 0; i < width; i++) {
600         int b = src1[3 * i + 0];
601         int g = src1[3 * i + 1];
602         int r = src1[3 * i + 2];
603
604         dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
605         dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
606     }
607     assert(src1 == src2);
608 }
609
610 static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
611                              const uint8_t *src2, int width, uint32_t *unused)
612 {
613     int i;
614     for (i = 0; i < width; i++) {
615         int b = src1[6 * i + 0] + src1[6 * i + 3];
616         int g = src1[6 * i + 1] + src1[6 * i + 4];
617         int r = src1[6 * i + 2] + src1[6 * i + 5];
618
619         dstU[i] = (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
620         dstV[i] = (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
621     }
622     assert(src1 == src2);
623 }
624
625 static void rgb24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
626                        uint32_t *unused)
627 {
628     int i;
629     for (i = 0; i < width; i++) {
630         int r = src[i * 3 + 0];
631         int g = src[i * 3 + 1];
632         int b = src[i * 3 + 2];
633
634         dst[i] = ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
635     }
636 }
637
638 static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
639                         const uint8_t *src2, int width, uint32_t *unused)
640 {
641     int i;
642     assert(src1 == src2);
643     for (i = 0; i < width; i++) {
644         int r = src1[3 * i + 0];
645         int g = src1[3 * i + 1];
646         int b = src1[3 * i + 2];
647
648         dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
649         dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
650     }
651 }
652
653 static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
654                              const uint8_t *src2, int width, uint32_t *unused)
655 {
656     int i;
657     assert(src1 == src2);
658     for (i = 0; i < width; i++) {
659         int r = src1[6 * i + 0] + src1[6 * i + 3];
660         int g = src1[6 * i + 1] + src1[6 * i + 4];
661         int b = src1[6 * i + 2] + src1[6 * i + 5];
662
663         dstU[i] = (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
664         dstV[i] = (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
665     }
666 }
667
668 static void planar_rgb_to_y(uint16_t *dst, const uint8_t *src[4], int width)
669 {
670     int i;
671     for (i = 0; i < width; i++) {
672         int g = src[0][i];
673         int b = src[1][i];
674         int r = src[2][i];
675
676         dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
677     }
678 }
679
680 static void planar_rgb_to_uv(uint16_t *dstU, uint16_t *dstV, const uint8_t *src[4], int width)
681 {
682     int i;
683     for (i = 0; i < width; i++) {
684         int g = src[0][i];
685         int b = src[1][i];
686         int r = src[2][i];
687
688         dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
689         dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
690     }
691 }
692
693 #define rdpx(src) \
694     is_be ? AV_RB16(src) : AV_RL16(src)
695 static av_always_inline void planar_rgb16_to_y(uint8_t *_dst, const uint8_t *_src[4],
696                                                int width, int bpc, int is_be)
697 {
698     int i;
699     const uint16_t **src = (const uint16_t **)_src;
700     uint16_t *dst        = (uint16_t *)_dst;
701     for (i = 0; i < width; i++) {
702         int g = rdpx(src[0] + i);
703         int b = rdpx(src[1] + i);
704         int r = rdpx(src[2] + i);
705
706         dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT + bpc - 9))) >> RGB2YUV_SHIFT);
707     }
708 }
709
710 static void planar_rgb9le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
711 {
712     planar_rgb16_to_y(dst, src, w, 9, 0);
713 }
714
715 static void planar_rgb9be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
716 {
717     planar_rgb16_to_y(dst, src, w, 9, 1);
718 }
719
720 static void planar_rgb10le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
721 {
722     planar_rgb16_to_y(dst, src, w, 10, 0);
723 }
724
725 static void planar_rgb10be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
726 {
727     planar_rgb16_to_y(dst, src, w, 10, 1);
728 }
729
730 static void planar_rgb16le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
731 {
732     planar_rgb16_to_y(dst, src, w, 16, 0);
733 }
734
735 static void planar_rgb16be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
736 {
737     planar_rgb16_to_y(dst, src, w, 16, 1);
738 }
739
740 static av_always_inline void planar_rgb16_to_uv(uint8_t *_dstU, uint8_t *_dstV,
741                                                 const uint8_t *_src[4], int width,
742                                                 int bpc, int is_be)
743 {
744     int i;
745     const uint16_t **src = (const uint16_t **)_src;
746     uint16_t *dstU       = (uint16_t *)_dstU;
747     uint16_t *dstV       = (uint16_t *)_dstV;
748     for (i = 0; i < width; i++) {
749         int g = rdpx(src[0] + i);
750         int b = rdpx(src[1] + i);
751         int r = rdpx(src[2] + i);
752
753         dstU[i] = (RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> RGB2YUV_SHIFT;
754         dstV[i] = (RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> RGB2YUV_SHIFT;
755     }
756 }
757 #undef rdpx
758
759 static void planar_rgb9le_to_uv(uint8_t *dstU, uint8_t *dstV,
760                                 const uint8_t *src[4], int w)
761 {
762     planar_rgb16_to_uv(dstU, dstV, src, w, 9, 0);
763 }
764
765 static void planar_rgb9be_to_uv(uint8_t *dstU, uint8_t *dstV,
766                                 const uint8_t *src[4], int w)
767 {
768     planar_rgb16_to_uv(dstU, dstV, src, w, 9, 1);
769 }
770
771 static void planar_rgb10le_to_uv(uint8_t *dstU, uint8_t *dstV,
772                                  const uint8_t *src[4], int w)
773 {
774     planar_rgb16_to_uv(dstU, dstV, src, w, 10, 0);
775 }
776
777 static void planar_rgb10be_to_uv(uint8_t *dstU, uint8_t *dstV,
778                                  const uint8_t *src[4], int w)
779 {
780     planar_rgb16_to_uv(dstU, dstV, src, w, 10, 1);
781 }
782
783 static void planar_rgb16le_to_uv(uint8_t *dstU, uint8_t *dstV,
784                                  const uint8_t *src[4], int w)
785 {
786     planar_rgb16_to_uv(dstU, dstV, src, w, 16, 0);
787 }
788
789 static void planar_rgb16be_to_uv(uint8_t *dstU, uint8_t *dstV,
790                                  const uint8_t *src[4], int w)
791 {
792     planar_rgb16_to_uv(dstU, dstV, src, w, 16, 1);
793 }
794
795 av_cold void ff_sws_init_input_funcs(SwsContext *c)
796 {
797     enum PixelFormat srcFormat = c->srcFormat;
798
799     c->chrToYV12 = NULL;
800     switch (srcFormat) {
801     case PIX_FMT_YUYV422:
802         c->chrToYV12 = yuy2ToUV_c;
803         break;
804     case PIX_FMT_UYVY422:
805         c->chrToYV12 = uyvyToUV_c;
806         break;
807     case PIX_FMT_NV12:
808         c->chrToYV12 = nv12ToUV_c;
809         break;
810     case PIX_FMT_NV21:
811         c->chrToYV12 = nv21ToUV_c;
812         break;
813     case PIX_FMT_RGB8:
814     case PIX_FMT_BGR8:
815     case PIX_FMT_PAL8:
816     case PIX_FMT_BGR4_BYTE:
817     case PIX_FMT_RGB4_BYTE:
818         c->chrToYV12 = palToUV_c;
819         break;
820     case PIX_FMT_GBRP9LE:
821         c->readChrPlanar = planar_rgb9le_to_uv;
822         break;
823     case PIX_FMT_GBRP10LE:
824         c->readChrPlanar = planar_rgb10le_to_uv;
825         break;
826     case PIX_FMT_GBRP16LE:
827         c->readChrPlanar = planar_rgb16le_to_uv;
828         break;
829     case PIX_FMT_GBRP9BE:
830         c->readChrPlanar = planar_rgb9be_to_uv;
831         break;
832     case PIX_FMT_GBRP10BE:
833         c->readChrPlanar = planar_rgb10be_to_uv;
834         break;
835     case PIX_FMT_GBRP16BE:
836         c->readChrPlanar = planar_rgb16be_to_uv;
837         break;
838     case PIX_FMT_GBRP:
839         c->readChrPlanar = planar_rgb_to_uv;
840         break;
841 #if HAVE_BIGENDIAN
842     case PIX_FMT_YUV444P9LE:
843     case PIX_FMT_YUV422P9LE:
844     case PIX_FMT_YUV420P9LE:
845     case PIX_FMT_YUV422P10LE:
846     case PIX_FMT_YUV444P10LE:
847     case PIX_FMT_YUV420P10LE:
848     case PIX_FMT_YUV420P16LE:
849     case PIX_FMT_YUV422P16LE:
850     case PIX_FMT_YUV444P16LE:
851         c->chrToYV12 = bswap16UV_c;
852         break;
853 #else
854     case PIX_FMT_YUV444P9BE:
855     case PIX_FMT_YUV422P9BE:
856     case PIX_FMT_YUV420P9BE:
857     case PIX_FMT_YUV444P10BE:
858     case PIX_FMT_YUV422P10BE:
859     case PIX_FMT_YUV420P10BE:
860     case PIX_FMT_YUV420P16BE:
861     case PIX_FMT_YUV422P16BE:
862     case PIX_FMT_YUV444P16BE:
863         c->chrToYV12 = bswap16UV_c;
864         break;
865 #endif
866     }
867     if (c->chrSrcHSubSample) {
868         switch (srcFormat) {
869         case PIX_FMT_RGBA64BE:
870             c->chrToYV12 = rgb64BEToUV_half_c;
871             break;
872         case PIX_FMT_RGBA64LE:
873             c->chrToYV12 = rgb64LEToUV_half_c;
874             break;
875         case PIX_FMT_RGB48BE:
876             c->chrToYV12 = rgb48BEToUV_half_c;
877             break;
878         case PIX_FMT_RGB48LE:
879             c->chrToYV12 = rgb48LEToUV_half_c;
880             break;
881         case PIX_FMT_BGR48BE:
882             c->chrToYV12 = bgr48BEToUV_half_c;
883             break;
884         case PIX_FMT_BGR48LE:
885             c->chrToYV12 = bgr48LEToUV_half_c;
886             break;
887         case PIX_FMT_RGB32:
888             c->chrToYV12 = bgr32ToUV_half_c;
889             break;
890         case PIX_FMT_RGB32_1:
891             c->chrToYV12 = bgr321ToUV_half_c;
892             break;
893         case PIX_FMT_BGR24:
894             c->chrToYV12 = bgr24ToUV_half_c;
895             break;
896         case PIX_FMT_BGR565LE:
897             c->chrToYV12 = bgr16leToUV_half_c;
898             break;
899         case PIX_FMT_BGR565BE:
900             c->chrToYV12 = bgr16beToUV_half_c;
901             break;
902         case PIX_FMT_BGR555LE:
903             c->chrToYV12 = bgr15leToUV_half_c;
904             break;
905         case PIX_FMT_BGR555BE:
906             c->chrToYV12 = bgr15beToUV_half_c;
907             break;
908         case PIX_FMT_GBR24P  :
909             c->chrToYV12 = gbr24pToUV_half_c;
910             break;
911         case PIX_FMT_BGR444LE:
912             c->chrToYV12 = bgr12leToUV_half_c;
913             break;
914         case PIX_FMT_BGR444BE:
915             c->chrToYV12 = bgr12beToUV_half_c;
916             break;
917         case PIX_FMT_BGR32:
918             c->chrToYV12 = rgb32ToUV_half_c;
919             break;
920         case PIX_FMT_BGR32_1:
921             c->chrToYV12 = rgb321ToUV_half_c;
922             break;
923         case PIX_FMT_RGB24:
924             c->chrToYV12 = rgb24ToUV_half_c;
925             break;
926         case PIX_FMT_RGB565LE:
927             c->chrToYV12 = rgb16leToUV_half_c;
928             break;
929         case PIX_FMT_RGB565BE:
930             c->chrToYV12 = rgb16beToUV_half_c;
931             break;
932         case PIX_FMT_RGB555LE:
933             c->chrToYV12 = rgb15leToUV_half_c;
934             break;
935         case PIX_FMT_RGB555BE:
936             c->chrToYV12 = rgb15beToUV_half_c;
937             break;
938         case PIX_FMT_RGB444LE:
939             c->chrToYV12 = rgb12leToUV_half_c;
940             break;
941         case PIX_FMT_RGB444BE:
942             c->chrToYV12 = rgb12beToUV_half_c;
943             break;
944         }
945     } else {
946         switch (srcFormat) {
947         case PIX_FMT_RGBA64BE:
948             c->chrToYV12 = rgb64BEToUV_c;
949             break;
950         case PIX_FMT_RGBA64LE:
951             c->chrToYV12 = rgb64LEToUV_c;
952             break;
953         case PIX_FMT_RGB48BE:
954             c->chrToYV12 = rgb48BEToUV_c;
955             break;
956         case PIX_FMT_RGB48LE:
957             c->chrToYV12 = rgb48LEToUV_c;
958             break;
959         case PIX_FMT_BGR48BE:
960             c->chrToYV12 = bgr48BEToUV_c;
961             break;
962         case PIX_FMT_BGR48LE:
963             c->chrToYV12 = bgr48LEToUV_c;
964             break;
965         case PIX_FMT_RGB32:
966             c->chrToYV12 = bgr32ToUV_c;
967             break;
968         case PIX_FMT_RGB32_1:
969             c->chrToYV12 = bgr321ToUV_c;
970             break;
971         case PIX_FMT_BGR24:
972             c->chrToYV12 = bgr24ToUV_c;
973             break;
974         case PIX_FMT_BGR565LE:
975             c->chrToYV12 = bgr16leToUV_c;
976             break;
977         case PIX_FMT_BGR565BE:
978             c->chrToYV12 = bgr16beToUV_c;
979             break;
980         case PIX_FMT_BGR555LE:
981             c->chrToYV12 = bgr15leToUV_c;
982             break;
983         case PIX_FMT_BGR555BE:
984             c->chrToYV12 = bgr15beToUV_c;
985             break;
986         case PIX_FMT_BGR444LE:
987             c->chrToYV12 = bgr12leToUV_c;
988             break;
989         case PIX_FMT_BGR444BE:
990             c->chrToYV12 = bgr12beToUV_c;
991             break;
992         case PIX_FMT_BGR32:
993             c->chrToYV12 = rgb32ToUV_c;
994             break;
995         case PIX_FMT_BGR32_1:
996             c->chrToYV12 = rgb321ToUV_c;
997             break;
998         case PIX_FMT_RGB24:
999             c->chrToYV12 = rgb24ToUV_c;
1000             break;
1001         case PIX_FMT_RGB565LE:
1002             c->chrToYV12 = rgb16leToUV_c;
1003             break;
1004         case PIX_FMT_RGB565BE:
1005             c->chrToYV12 = rgb16beToUV_c;
1006             break;
1007         case PIX_FMT_RGB555LE:
1008             c->chrToYV12 = rgb15leToUV_c;
1009             break;
1010         case PIX_FMT_RGB555BE:
1011             c->chrToYV12 = rgb15beToUV_c;
1012             break;
1013         case PIX_FMT_RGB444LE:
1014             c->chrToYV12 = rgb12leToUV_c;
1015             break;
1016         case PIX_FMT_RGB444BE:
1017             c->chrToYV12 = rgb12beToUV_c;
1018             break;
1019         }
1020     }
1021
1022     c->lumToYV12 = NULL;
1023     c->alpToYV12 = NULL;
1024     switch (srcFormat) {
1025     case PIX_FMT_GBRP9LE:
1026         c->readLumPlanar = planar_rgb9le_to_y;
1027         break;
1028     case PIX_FMT_GBRP10LE:
1029         c->readLumPlanar = planar_rgb10le_to_y;
1030         break;
1031     case PIX_FMT_GBRP16LE:
1032         c->readLumPlanar = planar_rgb16le_to_y;
1033         break;
1034     case PIX_FMT_GBRP9BE:
1035         c->readLumPlanar = planar_rgb9be_to_y;
1036         break;
1037     case PIX_FMT_GBRP10BE:
1038         c->readLumPlanar = planar_rgb10be_to_y;
1039         break;
1040     case PIX_FMT_GBRP16BE:
1041         c->readLumPlanar = planar_rgb16be_to_y;
1042         break;
1043     case PIX_FMT_GBRP:
1044         c->readLumPlanar = planar_rgb_to_y;
1045         break;
1046 #if HAVE_BIGENDIAN
1047     case PIX_FMT_YUV444P9LE:
1048     case PIX_FMT_YUV422P9LE:
1049     case PIX_FMT_YUV420P9LE:
1050     case PIX_FMT_YUV444P10LE:
1051     case PIX_FMT_YUV422P10LE:
1052     case PIX_FMT_YUV420P10LE:
1053     case PIX_FMT_YUV420P16LE:
1054     case PIX_FMT_YUV422P16LE:
1055     case PIX_FMT_YUV444P16LE:
1056     case PIX_FMT_GRAY16LE:
1057         c->lumToYV12 = bswap16Y_c;
1058         break;
1059 #else
1060     case PIX_FMT_YUV444P9BE:
1061     case PIX_FMT_YUV422P9BE:
1062     case PIX_FMT_YUV420P9BE:
1063     case PIX_FMT_YUV444P10BE:
1064     case PIX_FMT_YUV422P10BE:
1065     case PIX_FMT_YUV420P10BE:
1066     case PIX_FMT_YUV420P16BE:
1067     case PIX_FMT_YUV422P16BE:
1068     case PIX_FMT_YUV444P16BE:
1069     case PIX_FMT_GRAY16BE:
1070         c->lumToYV12 = bswap16Y_c;
1071         break;
1072 #endif
1073     case PIX_FMT_YUYV422:
1074     case PIX_FMT_Y400A:
1075         c->lumToYV12 = yuy2ToY_c;
1076         break;
1077     case PIX_FMT_UYVY422:
1078         c->lumToYV12 = uyvyToY_c;
1079         break;
1080     case PIX_FMT_BGR24:
1081         c->lumToYV12 = bgr24ToY_c;
1082         break;
1083     case PIX_FMT_BGR565LE:
1084         c->lumToYV12 = bgr16leToY_c;
1085         break;
1086     case PIX_FMT_BGR565BE:
1087         c->lumToYV12 = bgr16beToY_c;
1088         break;
1089     case PIX_FMT_BGR555LE:
1090         c->lumToYV12 = bgr15leToY_c;
1091         break;
1092     case PIX_FMT_BGR555BE:
1093         c->lumToYV12 = bgr15beToY_c;
1094         break;
1095     case PIX_FMT_BGR444LE:
1096         c->lumToYV12 = bgr12leToY_c;
1097         break;
1098     case PIX_FMT_BGR444BE:
1099         c->lumToYV12 = bgr12beToY_c;
1100         break;
1101     case PIX_FMT_RGB24:
1102         c->lumToYV12 = rgb24ToY_c;
1103         break;
1104     case PIX_FMT_RGB565LE:
1105         c->lumToYV12 = rgb16leToY_c;
1106         break;
1107     case PIX_FMT_RGB565BE:
1108         c->lumToYV12 = rgb16beToY_c;
1109         break;
1110     case PIX_FMT_RGB555LE:
1111         c->lumToYV12 = rgb15leToY_c;
1112         break;
1113     case PIX_FMT_RGB555BE:
1114         c->lumToYV12 = rgb15beToY_c;
1115         break;
1116     case PIX_FMT_RGB444LE:
1117         c->lumToYV12 = rgb12leToY_c;
1118         break;
1119     case PIX_FMT_RGB444BE:
1120         c->lumToYV12 = rgb12beToY_c;
1121         break;
1122     case PIX_FMT_RGB8:
1123     case PIX_FMT_BGR8:
1124     case PIX_FMT_PAL8:
1125     case PIX_FMT_BGR4_BYTE:
1126     case PIX_FMT_RGB4_BYTE:
1127         c->lumToYV12 = palToY_c;
1128         break;
1129     case PIX_FMT_MONOBLACK:
1130         c->lumToYV12 = monoblack2Y_c;
1131         break;
1132     case PIX_FMT_MONOWHITE:
1133         c->lumToYV12 = monowhite2Y_c;
1134         break;
1135     case PIX_FMT_RGB32:
1136         c->lumToYV12 = bgr32ToY_c;
1137         break;
1138     case PIX_FMT_RGB32_1:
1139         c->lumToYV12 = bgr321ToY_c;
1140         break;
1141     case PIX_FMT_BGR32:
1142         c->lumToYV12 = rgb32ToY_c;
1143         break;
1144     case PIX_FMT_BGR32_1:
1145         c->lumToYV12 = rgb321ToY_c;
1146         break;
1147     case PIX_FMT_RGB48BE:
1148         c->lumToYV12 = rgb48BEToY_c;
1149         break;
1150     case PIX_FMT_RGB48LE:
1151         c->lumToYV12 = rgb48LEToY_c;
1152         break;
1153     case PIX_FMT_BGR48BE:
1154         c->lumToYV12 = bgr48BEToY_c;
1155         break;
1156     case PIX_FMT_BGR48LE:
1157         c->lumToYV12 = bgr48LEToY_c;
1158         break;
1159     case PIX_FMT_RGBA64BE:
1160         c->lumToYV12 = rgb64BEToY_c;
1161         break;
1162     case PIX_FMT_RGBA64LE:
1163         c->lumToYV12 = rgb64LEToY_c;
1164         break;
1165     }
1166     if (c->alpPixBuf) {
1167         switch (srcFormat) {
1168         case PIX_FMT_RGBA64LE:
1169         case PIX_FMT_RGBA64BE:  c->alpToYV12 = rgba64ToA_c; break;
1170         case PIX_FMT_BGRA:
1171         case PIX_FMT_RGBA:
1172             c->alpToYV12 = rgbaToA_c;
1173             break;
1174         case PIX_FMT_ABGR:
1175         case PIX_FMT_ARGB:
1176             c->alpToYV12 = abgrToA_c;
1177             break;
1178         case PIX_FMT_Y400A:
1179             c->alpToYV12 = uyvyToY_c;
1180             break;
1181         case PIX_FMT_PAL8 :
1182             c->alpToYV12 = palToA_c;
1183             break;
1184         }
1185     }
1186 }