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