OSDN Git Service

ac3enc: fix LOCAL_ALIGNED usage in count_mantissa_bits()
[coroid/ffmpeg_saccubus.git] / libswscale / swscale.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /*
22   supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
23   supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
24   {BGR,RGB}{1,4,8,15,16} support dithering
25
26   unscaled special converters (YV12=I420=IYUV, Y800=Y8)
27   YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
28   x -> x
29   YUV9 -> YV12
30   YUV9/YV12 -> Y800
31   Y800 -> YUV9/YV12
32   BGR24 -> BGR32 & RGB24 -> RGB32
33   BGR32 -> BGR24 & RGB32 -> RGB24
34   BGR15 -> BGR16
35 */
36
37 /*
38 tested special converters (most are tested actually, but I did not write it down ...)
39  YV12 -> BGR12/BGR16
40  YV12 -> YV12
41  BGR15 -> BGR16
42  BGR16 -> BGR16
43  YVU9 -> YV12
44
45 untested special converters
46   YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
47   YV12/I420 -> YV12/I420
48   YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
49   BGR24 -> BGR32 & RGB24 -> RGB32
50   BGR32 -> BGR24 & RGB32 -> RGB24
51   BGR24 -> YV12
52 */
53
54 #include <inttypes.h>
55 #include <string.h>
56 #include <math.h>
57 #include <stdio.h>
58 #include "config.h"
59 #include <assert.h>
60 #include "swscale.h"
61 #include "swscale_internal.h"
62 #include "rgb2rgb.h"
63 #include "libavutil/intreadwrite.h"
64 #include "libavutil/x86_cpu.h"
65 #include "libavutil/cpu.h"
66 #include "libavutil/avutil.h"
67 #include "libavutil/mathematics.h"
68 #include "libavutil/bswap.h"
69 #include "libavutil/pixdesc.h"
70
71 #undef MOVNTQ
72 #undef PAVGB
73
74 #define DITHER1XBPP
75
76 #define isPacked(x)         (       \
77            (x)==PIX_FMT_PAL8        \
78         || (x)==PIX_FMT_YUYV422     \
79         || (x)==PIX_FMT_UYVY422     \
80         || (x)==PIX_FMT_Y400A       \
81         || isAnyRGB(x)              \
82     )
83
84 #define RGB2YUV_SHIFT 15
85 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
86 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
87 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
88 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
89 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
90 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
91 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
92 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
93 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
94
95 static const double rgb2yuv_table[8][9]={
96     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
97     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
98     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
99     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
100     {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
101     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
102     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
103     {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
104 };
105
106 /*
107 NOTES
108 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
109
110 TODO
111 more intelligent misalignment avoidance for the horizontal scaler
112 write special vertical cubic upscale version
113 optimize C code (YV12 / minmax)
114 add support for packed pixel YUV input & output
115 add support for Y8 output
116 optimize BGR24 & BGR32
117 add BGR4 output support
118 write special BGR->BGR scaler
119 */
120
121 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
122 {  1,   3,   1,   3,   1,   3,   1,   3, },
123 {  2,   0,   2,   0,   2,   0,   2,   0, },
124 };
125
126 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
127 {  6,   2,   6,   2,   6,   2,   6,   2, },
128 {  0,   4,   0,   4,   0,   4,   0,   4, },
129 };
130
131 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
132 {  8,   4,  11,   7,   8,   4,  11,   7, },
133 {  2,  14,   1,  13,   2,  14,   1,  13, },
134 { 10,   6,   9,   5,  10,   6,   9,   5, },
135 {  0,  12,   3,  15,   0,  12,   3,  15, },
136 };
137
138 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
139 { 17,   9,  23,  15,  16,   8,  22,  14, },
140 {  5,  29,   3,  27,   4,  28,   2,  26, },
141 { 21,  13,  19,  11,  20,  12,  18,  10, },
142 {  0,  24,   6,  30,   1,  25,   7,  31, },
143 { 16,   8,  22,  14,  17,   9,  23,  15, },
144 {  4,  28,   2,  26,   5,  29,   3,  27, },
145 { 20,  12,  18,  10,  21,  13,  19,  11, },
146 {  1,  25,   7,  31,   0,  24,   6,  30, },
147 };
148
149 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
150 {  0,  55,  14,  68,   3,  58,  17,  72, },
151 { 37,  18,  50,  32,  40,  22,  54,  35, },
152 {  9,  64,   5,  59,  13,  67,   8,  63, },
153 { 46,  27,  41,  23,  49,  31,  44,  26, },
154 {  2,  57,  16,  71,   1,  56,  15,  70, },
155 { 39,  21,  52,  34,  38,  19,  51,  33, },
156 { 11,  66,   7,  62,  10,  65,   6,  60, },
157 { 48,  30,  43,  25,  47,  29,  42,  24, },
158 };
159
160 #if 1
161 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
162 {117,  62, 158, 103, 113,  58, 155, 100, },
163 { 34, 199,  21, 186,  31, 196,  17, 182, },
164 {144,  89, 131,  76, 141,  86, 127,  72, },
165 {  0, 165,  41, 206,  10, 175,  52, 217, },
166 {110,  55, 151,  96, 120,  65, 162, 107, },
167 { 28, 193,  14, 179,  38, 203,  24, 189, },
168 {138,  83, 124,  69, 148,  93, 134,  79, },
169 {  7, 172,  48, 213,   3, 168,  45, 210, },
170 };
171 #elif 1
172 // tries to correct a gamma of 1.5
173 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
174 {  0, 143,  18, 200,   2, 156,  25, 215, },
175 { 78,  28, 125,  64,  89,  36, 138,  74, },
176 { 10, 180,   3, 161,  16, 195,   8, 175, },
177 {109,  51,  93,  38, 121,  60, 105,  47, },
178 {  1, 152,  23, 210,   0, 147,  20, 205, },
179 { 85,  33, 134,  71,  81,  30, 130,  67, },
180 { 14, 190,   6, 171,  12, 185,   5, 166, },
181 {117,  57, 101,  44, 113,  54,  97,  41, },
182 };
183 #elif 1
184 // tries to correct a gamma of 2.0
185 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
186 {  0, 124,   8, 193,   0, 140,  12, 213, },
187 { 55,  14, 104,  42,  66,  19, 119,  52, },
188 {  3, 168,   1, 145,   6, 187,   3, 162, },
189 { 86,  31,  70,  21,  99,  39,  82,  28, },
190 {  0, 134,  11, 206,   0, 129,   9, 200, },
191 { 62,  17, 114,  48,  58,  16, 109,  45, },
192 {  5, 181,   2, 157,   4, 175,   1, 151, },
193 { 95,  36,  78,  26,  90,  34,  74,  24, },
194 };
195 #else
196 // tries to correct a gamma of 2.5
197 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
198 {  0, 107,   3, 187,   0, 125,   6, 212, },
199 { 39,   7,  86,  28,  49,  11, 102,  36, },
200 {  1, 158,   0, 131,   3, 180,   1, 151, },
201 { 68,  19,  52,  12,  81,  25,  64,  17, },
202 {  0, 119,   5, 203,   0, 113,   4, 195, },
203 { 45,   9,  96,  33,  42,   8,  91,  30, },
204 {  2, 172,   1, 144,   2, 165,   0, 137, },
205 { 77,  23,  60,  15,  72,  21,  56,  14, },
206 };
207 #endif
208
209 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
210                                                     const int16_t *chrFilter, const int16_t **chrUSrc,
211                                                     const int16_t **chrVSrc, int chrFilterSize,
212                                                     const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
213                                                     int dstW, int chrDstW, int big_endian, int output_bits)
214 {
215     //FIXME Optimize (just quickly written not optimized..)
216     int i;
217     int shift = 11 + 16 - output_bits;
218
219 #define output_pixel(pos, val) \
220     if (big_endian) { \
221         if (output_bits == 16) { \
222             AV_WB16(pos, av_clip_uint16(val >> shift)); \
223         } else { \
224             AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
225         } \
226     } else { \
227         if (output_bits == 16) { \
228             AV_WL16(pos, av_clip_uint16(val >> shift)); \
229         } else { \
230             AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
231         } \
232     }
233     for (i = 0; i < dstW; i++) {
234         int val = 1 << (26-output_bits);
235         int j;
236
237         for (j = 0; j < lumFilterSize; j++)
238             val += lumSrc[j][i] * lumFilter[j];
239
240         output_pixel(&dest[i], val);
241     }
242
243     if (uDest) {
244         for (i = 0; i < chrDstW; i++) {
245             int u = 1 << (26-output_bits);
246             int v = 1 << (26-output_bits);
247             int j;
248
249             for (j = 0; j < chrFilterSize; j++) {
250                 u += chrUSrc[j][i] * chrFilter[j];
251                 v += chrVSrc[j][i] * chrFilter[j];
252             }
253
254             output_pixel(&uDest[i], u);
255             output_pixel(&vDest[i], v);
256         }
257     }
258
259     if (CONFIG_SWSCALE_ALPHA && aDest) {
260         for (i = 0; i < dstW; i++) {
261             int val = 1 << (26-output_bits);
262             int j;
263
264             for (j = 0; j < lumFilterSize; j++)
265                 val += alpSrc[j][i] * lumFilter[j];
266
267             output_pixel(&aDest[i], val);
268         }
269     }
270 }
271
272 #define yuv2NBPS(bits, BE_LE, is_be) \
273 static void yuv2yuvX ## bits ## BE_LE ## _c(const int16_t *lumFilter, \
274                               const int16_t **lumSrc, int lumFilterSize, \
275                               const int16_t *chrFilter, const int16_t **chrUSrc, \
276                               const int16_t **chrVSrc, \
277                               int chrFilterSize, const int16_t **alpSrc, \
278                               uint16_t *dest, uint16_t *uDest, uint16_t *vDest, \
279                               uint16_t *aDest, int dstW, int chrDstW) \
280 { \
281     yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
282                            chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
283                            alpSrc, \
284                            dest, uDest, vDest, aDest, \
285                            dstW, chrDstW, is_be, bits); \
286 }
287 yuv2NBPS( 9, BE, 1);
288 yuv2NBPS( 9, LE, 0);
289 yuv2NBPS(10, BE, 1);
290 yuv2NBPS(10, LE, 0);
291 yuv2NBPS(16, BE, 1);
292 yuv2NBPS(16, LE, 0);
293
294 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
295                                  const int16_t *chrFilter, const int16_t **chrUSrc, const int16_t **chrVSrc, int chrFilterSize,
296                                  const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
297                                  enum PixelFormat dstFormat)
298 {
299 #define conv16(bits) \
300     if (isBE(dstFormat)) { \
301         yuv2yuvX ## bits ## BE_c(lumFilter, lumSrc, lumFilterSize, \
302                                chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
303                                alpSrc, \
304                                dest, uDest, vDest, aDest, \
305                                dstW, chrDstW); \
306     } else { \
307         yuv2yuvX ## bits ## LE_c(lumFilter, lumSrc, lumFilterSize, \
308                                chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
309                                alpSrc, \
310                                dest, uDest, vDest, aDest, \
311                                dstW, chrDstW); \
312     }
313     if (is16BPS(dstFormat)) {
314         conv16(16);
315     } else if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
316         conv16(9);
317     } else {
318         conv16(10);
319     }
320 #undef conv16
321 }
322
323 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
324                                const int16_t *chrFilter, const int16_t **chrUSrc,
325                                const int16_t **chrVSrc, int chrFilterSize,
326                                const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
327 {
328     //FIXME Optimize (just quickly written not optimized..)
329     int i;
330     for (i=0; i<dstW; i++) {
331         int val=1<<18;
332         int j;
333         for (j=0; j<lumFilterSize; j++)
334             val += lumSrc[j][i] * lumFilter[j];
335
336         dest[i]= av_clip_uint8(val>>19);
337     }
338
339     if (uDest)
340         for (i=0; i<chrDstW; i++) {
341             int u=1<<18;
342             int v=1<<18;
343             int j;
344             for (j=0; j<chrFilterSize; j++) {
345                 u += chrUSrc[j][i] * chrFilter[j];
346                 v += chrVSrc[j][i] * chrFilter[j];
347             }
348
349             uDest[i]= av_clip_uint8(u>>19);
350             vDest[i]= av_clip_uint8(v>>19);
351         }
352
353     if (CONFIG_SWSCALE_ALPHA && aDest)
354         for (i=0; i<dstW; i++) {
355             int val=1<<18;
356             int j;
357             for (j=0; j<lumFilterSize; j++)
358                 val += alpSrc[j][i] * lumFilter[j];
359
360             aDest[i]= av_clip_uint8(val>>19);
361         }
362
363 }
364
365 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
366                                 const int16_t *chrFilter, const int16_t **chrUSrc,
367                                 const int16_t **chrVSrc, int chrFilterSize,
368                                 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
369 {
370     //FIXME Optimize (just quickly written not optimized..)
371     int i;
372     for (i=0; i<dstW; i++) {
373         int val=1<<18;
374         int j;
375         for (j=0; j<lumFilterSize; j++)
376             val += lumSrc[j][i] * lumFilter[j];
377
378         dest[i]= av_clip_uint8(val>>19);
379     }
380
381     if (!uDest)
382         return;
383
384     if (dstFormat == PIX_FMT_NV12)
385         for (i=0; i<chrDstW; i++) {
386             int u=1<<18;
387             int v=1<<18;
388             int j;
389             for (j=0; j<chrFilterSize; j++) {
390                 u += chrUSrc[j][i] * chrFilter[j];
391                 v += chrVSrc[j][i] * chrFilter[j];
392             }
393
394             uDest[2*i]= av_clip_uint8(u>>19);
395             uDest[2*i+1]= av_clip_uint8(v>>19);
396         }
397     else
398         for (i=0; i<chrDstW; i++) {
399             int u=1<<18;
400             int v=1<<18;
401             int j;
402             for (j=0; j<chrFilterSize; j++) {
403                 u += chrUSrc[j][i] * chrFilter[j];
404                 v += chrVSrc[j][i] * chrFilter[j];
405             }
406
407             uDest[2*i]= av_clip_uint8(v>>19);
408             uDest[2*i+1]= av_clip_uint8(u>>19);
409         }
410 }
411
412 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
413     for (i=0; i<(dstW>>1); i++) {\
414         int j;\
415         int Y1 = 1<<18;\
416         int Y2 = 1<<18;\
417         int U  = 1<<18;\
418         int V  = 1<<18;\
419         int av_unused A1, A2;\
420         type av_unused *r, *b, *g;\
421         const int i2= 2*i;\
422         \
423         for (j=0; j<lumFilterSize; j++) {\
424             Y1 += lumSrc[j][i2] * lumFilter[j];\
425             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
426         }\
427         for (j=0; j<chrFilterSize; j++) {\
428             U += chrUSrc[j][i] * chrFilter[j];\
429             V += chrVSrc[j][i] * chrFilter[j];\
430         }\
431         Y1>>=19;\
432         Y2>>=19;\
433         U >>=19;\
434         V >>=19;\
435         if (alpha) {\
436             A1 = 1<<18;\
437             A2 = 1<<18;\
438             for (j=0; j<lumFilterSize; j++) {\
439                 A1 += alpSrc[j][i2  ] * lumFilter[j];\
440                 A2 += alpSrc[j][i2+1] * lumFilter[j];\
441             }\
442             A1>>=19;\
443             A2>>=19;\
444         }
445
446 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
447         YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
448         if ((Y1|Y2|U|V)&256) {\
449             if (Y1>255)   Y1=255; \
450             else if (Y1<0)Y1=0;   \
451             if (Y2>255)   Y2=255; \
452             else if (Y2<0)Y2=0;   \
453             if (U>255)    U=255;  \
454             else if (U<0) U=0;    \
455             if (V>255)    V=255;  \
456             else if (V<0) V=0;    \
457         }\
458         if (alpha && ((A1|A2)&256)) {\
459             A1=av_clip_uint8(A1);\
460             A2=av_clip_uint8(A2);\
461         }
462
463 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
464     for (i=0; i<dstW; i++) {\
465         int j;\
466         int Y = 0;\
467         int U = -128<<19;\
468         int V = -128<<19;\
469         int av_unused A;\
470         int R,G,B;\
471         \
472         for (j=0; j<lumFilterSize; j++) {\
473             Y += lumSrc[j][i     ] * lumFilter[j];\
474         }\
475         for (j=0; j<chrFilterSize; j++) {\
476             U += chrUSrc[j][i] * chrFilter[j];\
477             V += chrVSrc[j][i] * chrFilter[j];\
478         }\
479         Y >>=10;\
480         U >>=10;\
481         V >>=10;\
482         if (alpha) {\
483             A = rnd;\
484             for (j=0; j<lumFilterSize; j++)\
485                 A += alpSrc[j][i     ] * lumFilter[j];\
486             A >>=19;\
487             if (A&256)\
488                 A = av_clip_uint8(A);\
489         }
490
491 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
492     YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
493         Y-= c->yuv2rgb_y_offset;\
494         Y*= c->yuv2rgb_y_coeff;\
495         Y+= rnd;\
496         R= Y + V*c->yuv2rgb_v2r_coeff;\
497         G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
498         B= Y +                          U*c->yuv2rgb_u2b_coeff;\
499         if ((R|G|B)&(0xC0000000)) {\
500             if (R>=(256<<22))   R=(256<<22)-1; \
501             else if (R<0)R=0;   \
502             if (G>=(256<<22))   G=(256<<22)-1; \
503             else if (G<0)G=0;   \
504             if (B>=(256<<22))   B=(256<<22)-1; \
505             else if (B<0)B=0;   \
506         }
507
508 #define YSCALE_YUV_2_GRAY16_C \
509     for (i=0; i<(dstW>>1); i++) {\
510         int j;\
511         int Y1 = 1<<18;\
512         int Y2 = 1<<18;\
513         int U  = 1<<18;\
514         int V  = 1<<18;\
515         \
516         const int i2= 2*i;\
517         \
518         for (j=0; j<lumFilterSize; j++) {\
519             Y1 += lumSrc[j][i2] * lumFilter[j];\
520             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
521         }\
522         Y1>>=11;\
523         Y2>>=11;\
524         if ((Y1|Y2|U|V)&65536) {\
525             if (Y1>65535)   Y1=65535; \
526             else if (Y1<0)Y1=0;   \
527             if (Y2>65535)   Y2=65535; \
528             else if (Y2<0)Y2=0;   \
529         }
530
531 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
532     YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
533     r = (type *)c->table_rV[V];   \
534     g = (type *)(c->table_gU[U] + c->table_gV[V]); \
535     b = (type *)c->table_bU[U];
536
537 #define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
538     for (i=0; i<(dstW>>1); i++) { \
539         const int i2= 2*i;       \
540         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
541         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
542         int U= (ubuf0[i]*uvalpha1+ubuf1[i]*uvalpha)>>19;              \
543         int V= (vbuf0[i]*uvalpha1+vbuf1[i]*uvalpha)>>19;              \
544         type av_unused *r, *b, *g;                                    \
545         int av_unused A1, A2;                                         \
546         if (alpha) {\
547             A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
548             A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
549         }
550
551 #define YSCALE_YUV_2_GRAY16_2_C   \
552     for (i=0; i<(dstW>>1); i++) { \
553         const int i2= 2*i;       \
554         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
555         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
556
557 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
558     YSCALE_YUV_2_PACKED2_C(type,alpha)\
559     r = (type *)c->table_rV[V];\
560     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
561     b = (type *)c->table_bU[U];
562
563 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
564     for (i=0; i<(dstW>>1); i++) {\
565         const int i2= 2*i;\
566         int Y1= buf0[i2  ]>>7;\
567         int Y2= buf0[i2+1]>>7;\
568         int U= (ubuf1[i])>>7;\
569         int V= (vbuf1[i])>>7;\
570         type av_unused *r, *b, *g;\
571         int av_unused A1, A2;\
572         if (alpha) {\
573             A1= abuf0[i2  ]>>7;\
574             A2= abuf0[i2+1]>>7;\
575         }
576
577 #define YSCALE_YUV_2_GRAY16_1_C \
578     for (i=0; i<(dstW>>1); i++) {\
579         const int i2= 2*i;\
580         int Y1= buf0[i2  ]<<1;\
581         int Y2= buf0[i2+1]<<1;
582
583 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
584     YSCALE_YUV_2_PACKED1_C(type,alpha)\
585     r = (type *)c->table_rV[V];\
586     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
587     b = (type *)c->table_bU[U];
588
589 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
590     for (i=0; i<(dstW>>1); i++) {\
591         const int i2= 2*i;\
592         int Y1= buf0[i2  ]>>7;\
593         int Y2= buf0[i2+1]>>7;\
594         int U= (ubuf0[i] + ubuf1[i])>>8;\
595         int V= (vbuf0[i] + vbuf1[i])>>8;\
596         type av_unused *r, *b, *g;\
597         int av_unused A1, A2;\
598         if (alpha) {\
599             A1= abuf0[i2  ]>>7;\
600             A2= abuf0[i2+1]>>7;\
601         }
602
603 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
604     YSCALE_YUV_2_PACKED1B_C(type,alpha)\
605     r = (type *)c->table_rV[V];\
606     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
607     b = (type *)c->table_bU[U];
608
609 #define YSCALE_YUV_2_MONO2_C \
610     const uint8_t * const d128=dither_8x8_220[y&7];\
611     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
612     for (i=0; i<dstW-7; i+=8) {\
613         int acc;\
614         acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
615         acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
616         acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
617         acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
618         acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
619         acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
620         acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
621         acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
622         ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
623         dest++;\
624     }
625
626 #define YSCALE_YUV_2_MONOX_C \
627     const uint8_t * const d128=dither_8x8_220[y&7];\
628     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
629     int acc=0;\
630     for (i=0; i<dstW-1; i+=2) {\
631         int j;\
632         int Y1=1<<18;\
633         int Y2=1<<18;\
634 \
635         for (j=0; j<lumFilterSize; j++) {\
636             Y1 += lumSrc[j][i] * lumFilter[j];\
637             Y2 += lumSrc[j][i+1] * lumFilter[j];\
638         }\
639         Y1>>=19;\
640         Y2>>=19;\
641         if ((Y1|Y2)&256) {\
642             if (Y1>255)   Y1=255;\
643             else if (Y1<0)Y1=0;\
644             if (Y2>255)   Y2=255;\
645             else if (Y2<0)Y2=0;\
646         }\
647         acc+= acc + g[Y1+d128[(i+0)&7]];\
648         acc+= acc + g[Y2+d128[(i+1)&7]];\
649         if ((i&7)==6) {\
650             ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
651             dest++;\
652         }\
653     }
654
655 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
656     switch(c->dstFormat) {\
657     case PIX_FMT_RGB48BE:\
658     case PIX_FMT_RGB48LE:\
659         func(uint8_t,0)\
660             ((uint8_t*)dest)[ 0]= r[Y1];\
661             ((uint8_t*)dest)[ 1]= r[Y1];\
662             ((uint8_t*)dest)[ 2]= g[Y1];\
663             ((uint8_t*)dest)[ 3]= g[Y1];\
664             ((uint8_t*)dest)[ 4]= b[Y1];\
665             ((uint8_t*)dest)[ 5]= b[Y1];\
666             ((uint8_t*)dest)[ 6]= r[Y2];\
667             ((uint8_t*)dest)[ 7]= r[Y2];\
668             ((uint8_t*)dest)[ 8]= g[Y2];\
669             ((uint8_t*)dest)[ 9]= g[Y2];\
670             ((uint8_t*)dest)[10]= b[Y2];\
671             ((uint8_t*)dest)[11]= b[Y2];\
672             dest+=12;\
673         }\
674         break;\
675     case PIX_FMT_BGR48BE:\
676     case PIX_FMT_BGR48LE:\
677         func(uint8_t,0)\
678             ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
679             ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
680             ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
681             ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
682             ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
683             ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
684             dest+=12;\
685         }\
686         break;\
687     case PIX_FMT_RGBA:\
688     case PIX_FMT_BGRA:\
689         if (CONFIG_SMALL) {\
690             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
691             func(uint32_t,needAlpha)\
692                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
693                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
694             }\
695         } else {\
696             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
697                 func(uint32_t,1)\
698                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
699                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
700                 }\
701             } else {\
702                 func(uint32_t,0)\
703                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
704                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
705                 }\
706             }\
707         }\
708         break;\
709     case PIX_FMT_ARGB:\
710     case PIX_FMT_ABGR:\
711         if (CONFIG_SMALL) {\
712             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
713             func(uint32_t,needAlpha)\
714                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
715                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
716             }\
717         } else {\
718             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
719                 func(uint32_t,1)\
720                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
721                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
722                 }\
723             } else {\
724                 func(uint32_t,0)\
725                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
726                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
727                 }\
728             }\
729         }                \
730         break;\
731     case PIX_FMT_RGB24:\
732         func(uint8_t,0)\
733             ((uint8_t*)dest)[0]= r[Y1];\
734             ((uint8_t*)dest)[1]= g[Y1];\
735             ((uint8_t*)dest)[2]= b[Y1];\
736             ((uint8_t*)dest)[3]= r[Y2];\
737             ((uint8_t*)dest)[4]= g[Y2];\
738             ((uint8_t*)dest)[5]= b[Y2];\
739             dest+=6;\
740         }\
741         break;\
742     case PIX_FMT_BGR24:\
743         func(uint8_t,0)\
744             ((uint8_t*)dest)[0]= b[Y1];\
745             ((uint8_t*)dest)[1]= g[Y1];\
746             ((uint8_t*)dest)[2]= r[Y1];\
747             ((uint8_t*)dest)[3]= b[Y2];\
748             ((uint8_t*)dest)[4]= g[Y2];\
749             ((uint8_t*)dest)[5]= r[Y2];\
750             dest+=6;\
751         }\
752         break;\
753     case PIX_FMT_RGB565BE:\
754     case PIX_FMT_RGB565LE:\
755     case PIX_FMT_BGR565BE:\
756     case PIX_FMT_BGR565LE:\
757         {\
758             const int dr1= dither_2x2_8[y&1    ][0];\
759             const int dg1= dither_2x2_4[y&1    ][0];\
760             const int db1= dither_2x2_8[(y&1)^1][0];\
761             const int dr2= dither_2x2_8[y&1    ][1];\
762             const int dg2= dither_2x2_4[y&1    ][1];\
763             const int db2= dither_2x2_8[(y&1)^1][1];\
764             func(uint16_t,0)\
765                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
766                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
767             }\
768         }\
769         break;\
770     case PIX_FMT_RGB555BE:\
771     case PIX_FMT_RGB555LE:\
772     case PIX_FMT_BGR555BE:\
773     case PIX_FMT_BGR555LE:\
774         {\
775             const int dr1= dither_2x2_8[y&1    ][0];\
776             const int dg1= dither_2x2_8[y&1    ][1];\
777             const int db1= dither_2x2_8[(y&1)^1][0];\
778             const int dr2= dither_2x2_8[y&1    ][1];\
779             const int dg2= dither_2x2_8[y&1    ][0];\
780             const int db2= dither_2x2_8[(y&1)^1][1];\
781             func(uint16_t,0)\
782                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
783                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
784             }\
785         }\
786         break;\
787     case PIX_FMT_RGB444BE:\
788     case PIX_FMT_RGB444LE:\
789     case PIX_FMT_BGR444BE:\
790     case PIX_FMT_BGR444LE:\
791         {\
792             const int dr1= dither_4x4_16[y&3    ][0];\
793             const int dg1= dither_4x4_16[y&3    ][1];\
794             const int db1= dither_4x4_16[(y&3)^3][0];\
795             const int dr2= dither_4x4_16[y&3    ][1];\
796             const int dg2= dither_4x4_16[y&3    ][0];\
797             const int db2= dither_4x4_16[(y&3)^3][1];\
798             func(uint16_t,0)\
799                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
800                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
801             }\
802         }\
803         break;\
804     case PIX_FMT_RGB8:\
805     case PIX_FMT_BGR8:\
806         {\
807             const uint8_t * const d64= dither_8x8_73[y&7];\
808             const uint8_t * const d32= dither_8x8_32[y&7];\
809             func(uint8_t,0)\
810                 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
811                 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
812             }\
813         }\
814         break;\
815     case PIX_FMT_RGB4:\
816     case PIX_FMT_BGR4:\
817         {\
818             const uint8_t * const d64= dither_8x8_73 [y&7];\
819             const uint8_t * const d128=dither_8x8_220[y&7];\
820             func(uint8_t,0)\
821                 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
822                                  + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
823             }\
824         }\
825         break;\
826     case PIX_FMT_RGB4_BYTE:\
827     case PIX_FMT_BGR4_BYTE:\
828         {\
829             const uint8_t * const d64= dither_8x8_73 [y&7];\
830             const uint8_t * const d128=dither_8x8_220[y&7];\
831             func(uint8_t,0)\
832                 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
833                 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
834             }\
835         }\
836         break;\
837     case PIX_FMT_MONOBLACK:\
838     case PIX_FMT_MONOWHITE:\
839         {\
840             func_monoblack\
841         }\
842         break;\
843     case PIX_FMT_YUYV422:\
844         func2\
845             ((uint8_t*)dest)[2*i2+0]= Y1;\
846             ((uint8_t*)dest)[2*i2+1]= U;\
847             ((uint8_t*)dest)[2*i2+2]= Y2;\
848             ((uint8_t*)dest)[2*i2+3]= V;\
849         }                \
850         break;\
851     case PIX_FMT_UYVY422:\
852         func2\
853             ((uint8_t*)dest)[2*i2+0]= U;\
854             ((uint8_t*)dest)[2*i2+1]= Y1;\
855             ((uint8_t*)dest)[2*i2+2]= V;\
856             ((uint8_t*)dest)[2*i2+3]= Y2;\
857         }                \
858         break;\
859     case PIX_FMT_GRAY16BE:\
860         func_g16\
861             ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
862             ((uint8_t*)dest)[2*i2+1]= Y1;\
863             ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
864             ((uint8_t*)dest)[2*i2+3]= Y2;\
865         }                \
866         break;\
867     case PIX_FMT_GRAY16LE:\
868         func_g16\
869             ((uint8_t*)dest)[2*i2+0]= Y1;\
870             ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
871             ((uint8_t*)dest)[2*i2+2]= Y2;\
872             ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
873         }                \
874         break;\
875     }
876
877 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter,
878                                   const int16_t **lumSrc, int lumFilterSize,
879                                   const int16_t *chrFilter, const int16_t **chrUSrc,
880                                   const int16_t **chrVSrc, int chrFilterSize,
881                                   const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
882 {
883     int i;
884     YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
885 }
886
887 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter,
888                                     const int16_t **lumSrc, int lumFilterSize,
889                                     const int16_t *chrFilter, const int16_t **chrUSrc,
890                                     const int16_t **chrVSrc, int chrFilterSize,
891                                     const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
892 {
893     int i;
894     int step= c->dstFormatBpp/8;
895     int aidx= 3;
896
897     switch(c->dstFormat) {
898     case PIX_FMT_ARGB:
899         dest++;
900         aidx= 0;
901     case PIX_FMT_RGB24:
902         aidx--;
903     case PIX_FMT_RGBA:
904         if (CONFIG_SMALL) {
905             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
906             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
907                 dest[aidx]= needAlpha ? A : 255;
908                 dest[0]= R>>22;
909                 dest[1]= G>>22;
910                 dest[2]= B>>22;
911                 dest+= step;
912             }
913         } else {
914             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
915                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
916                     dest[aidx]= A;
917                     dest[0]= R>>22;
918                     dest[1]= G>>22;
919                     dest[2]= B>>22;
920                     dest+= step;
921                 }
922             } else {
923                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
924                     dest[aidx]= 255;
925                     dest[0]= R>>22;
926                     dest[1]= G>>22;
927                     dest[2]= B>>22;
928                     dest+= step;
929                 }
930             }
931         }
932         break;
933     case PIX_FMT_ABGR:
934         dest++;
935         aidx= 0;
936     case PIX_FMT_BGR24:
937         aidx--;
938     case PIX_FMT_BGRA:
939         if (CONFIG_SMALL) {
940             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
941             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
942                 dest[aidx]= needAlpha ? A : 255;
943                 dest[0]= B>>22;
944                 dest[1]= G>>22;
945                 dest[2]= R>>22;
946                 dest+= step;
947             }
948         } else {
949             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
950                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
951                     dest[aidx]= A;
952                     dest[0]= B>>22;
953                     dest[1]= G>>22;
954                     dest[2]= R>>22;
955                     dest+= step;
956                 }
957             } else {
958                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
959                     dest[aidx]= 255;
960                     dest[0]= B>>22;
961                     dest[1]= G>>22;
962                     dest[2]= R>>22;
963                     dest+= step;
964                 }
965             }
966         }
967         break;
968     default:
969         assert(0);
970     }
971 }
972
973 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
974 {
975     int i;
976     uint8_t *ptr = plane + stride*y;
977     for (i=0; i<height; i++) {
978         memset(ptr, val, width);
979         ptr += stride;
980     }
981 }
982
983 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
984                             uint32_t *unused)
985 {
986     int i;
987     for (i = 0; i < width; i++) {
988         int r = src[i*6+0];
989         int g = src[i*6+2];
990         int b = src[i*6+4];
991
992         dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
993     }
994 }
995
996 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
997                              const uint8_t *src1, const uint8_t *src2,
998                              int width, uint32_t *unused)
999 {
1000     int i;
1001     assert(src1==src2);
1002     for (i = 0; i < width; i++) {
1003         int r = src1[6*i + 0];
1004         int g = src1[6*i + 2];
1005         int b = src1[6*i + 4];
1006
1007         dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1008         dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1009     }
1010 }
1011
1012 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1013                                   const uint8_t *src1, const uint8_t *src2,
1014                                   int width, uint32_t *unused)
1015 {
1016     int i;
1017     assert(src1==src2);
1018     for (i = 0; i < width; i++) {
1019         int r= src1[12*i + 0] + src1[12*i + 6];
1020         int g= src1[12*i + 2] + src1[12*i + 8];
1021         int b= src1[12*i + 4] + src1[12*i + 10];
1022
1023         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1024         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1025     }
1026 }
1027
1028 static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, int width,
1029                             uint32_t *unused)
1030 {
1031     int i;
1032     for (i = 0; i < width; i++) {
1033         int b = src[i*6+0];
1034         int g = src[i*6+2];
1035         int r = src[i*6+4];
1036
1037         dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1038     }
1039 }
1040
1041 static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1042                              const uint8_t *src1, const uint8_t *src2,
1043                              int width, uint32_t *unused)
1044 {
1045     int i;
1046     for (i = 0; i < width; i++) {
1047         int b = src1[6*i + 0];
1048         int g = src1[6*i + 2];
1049         int r = src1[6*i + 4];
1050
1051         dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1052         dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1053     }
1054 }
1055
1056 static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1057                                   const uint8_t *src1, const uint8_t *src2,
1058                                   int width, uint32_t *unused)
1059 {
1060     int i;
1061     for (i = 0; i < width; i++) {
1062         int b= src1[12*i + 0] + src1[12*i + 6];
1063         int g= src1[12*i + 2] + src1[12*i + 8];
1064         int r= src1[12*i + 4] + src1[12*i + 10];
1065
1066         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1067         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1068     }
1069 }
1070
1071 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1072 static inline void name(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)\
1073 {\
1074     int i;\
1075     for (i=0; i<width; i++) {\
1076         int b= (((const type*)src)[i]>>shb)&maskb;\
1077         int g= (((const type*)src)[i]>>shg)&maskg;\
1078         int r= (((const type*)src)[i]>>shr)&maskr;\
1079 \
1080         dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1081     }\
1082 }
1083
1084 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1085 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1086 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1087 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1088 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1089 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1090 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1091 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1092
1093 static inline void abgrToA(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1094 {
1095     int i;
1096     for (i=0; i<width; i++) {
1097         dst[i]= src[4*i];
1098     }
1099 }
1100
1101 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1102 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1103 {\
1104     int i;\
1105     for (i=0; i<width; i++) {\
1106         int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1107         int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1108         int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1109 \
1110         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1111         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1112     }\
1113 }\
1114 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1115 {\
1116     int i;\
1117     for (i=0; i<width; i++) {\
1118         int pix0= ((const type*)src)[2*i+0]>>shp;\
1119         int pix1= ((const type*)src)[2*i+1]>>shp;\
1120         int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1121         int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1122         int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1123         g&= maskg|(2*maskg);\
1124 \
1125         g>>=shg;\
1126 \
1127         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1128         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1129     }\
1130 }
1131
1132 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1133 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1134 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1135 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1136 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1137 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1138 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1139 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1140
1141 static inline void palToY(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
1142 {
1143     int i;
1144     for (i=0; i<width; i++) {
1145         int d= src[i];
1146
1147         dst[i]= pal[d] & 0xFF;
1148     }
1149 }
1150
1151 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1152                            const uint8_t *src1, const uint8_t *src2,
1153                            int width, uint32_t *pal)
1154 {
1155     int i;
1156     assert(src1 == src2);
1157     for (i=0; i<width; i++) {
1158         int p= pal[src1[i]];
1159
1160         dstU[i]= p>>8;
1161         dstV[i]= p>>16;
1162     }
1163 }
1164
1165 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1166 {
1167     int i, j;
1168     for (i=0; i<width/8; i++) {
1169         int d= ~src[i];
1170         for(j=0; j<8; j++)
1171             dst[8*i+j]= ((d>>(7-j))&1)*255;
1172     }
1173 }
1174
1175 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1176 {
1177     int i, j;
1178     for (i=0; i<width/8; i++) {
1179         int d= src[i];
1180         for(j=0; j<8; j++)
1181             dst[8*i+j]= ((d>>(7-j))&1)*255;
1182     }
1183 }
1184
1185 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1186 //Plain C versions
1187
1188 #define COMPILE_TEMPLATE_MMX2 0
1189 #define COMPILE_TEMPLATE_ALTIVEC 0
1190
1191 #include "swscale_template.c"
1192
1193 #if HAVE_ALTIVEC
1194 #undef RENAME
1195 #undef COMPILE_TEMPLATE_ALTIVEC
1196 #define COMPILE_TEMPLATE_ALTIVEC 1
1197 #define RENAME(a) a ## _altivec
1198 #include "ppc/swscale_template.c"
1199 #endif
1200
1201 //MMX versions
1202 #if HAVE_MMX
1203 #undef RENAME
1204 #undef COMPILE_TEMPLATE_MMX2
1205 #define COMPILE_TEMPLATE_MMX2 0
1206 #define RENAME(a) a ## _MMX
1207 #include "x86/swscale_template.c"
1208 #endif
1209
1210 //MMX2 versions
1211 #if HAVE_MMX2
1212 #undef RENAME
1213 #undef COMPILE_TEMPLATE_MMX2
1214 #define COMPILE_TEMPLATE_MMX2 1
1215 #define RENAME(a) a ## _MMX2
1216 #include "x86/swscale_template.c"
1217 #endif
1218
1219 SwsFunc ff_getSwsFunc(SwsContext *c)
1220 {
1221     int cpu_flags = av_get_cpu_flags();
1222
1223     sws_init_swScale_c(c);
1224
1225 #if HAVE_MMX
1226     if (cpu_flags & AV_CPU_FLAG_MMX)
1227         sws_init_swScale_MMX(c);
1228 #endif
1229 #if HAVE_MMX2
1230     if (cpu_flags & AV_CPU_FLAG_MMX2)
1231         sws_init_swScale_MMX2(c);
1232 #endif
1233 #if HAVE_ALTIVEC
1234     if (cpu_flags & AV_CPU_FLAG_ALTIVEC)
1235         sws_init_swScale_altivec(c);
1236 #endif
1237
1238     return swScale_c;
1239 }
1240
1241 static void copyPlane(const uint8_t *src, int srcStride,
1242                       int srcSliceY, int srcSliceH, int width,
1243                       uint8_t *dst, int dstStride)
1244 {
1245     dst += dstStride * srcSliceY;
1246     if (dstStride == srcStride && srcStride > 0) {
1247         memcpy(dst, src, srcSliceH * dstStride);
1248     } else {
1249         int i;
1250         for (i=0; i<srcSliceH; i++) {
1251             memcpy(dst, src, width);
1252             src += srcStride;
1253             dst += dstStride;
1254         }
1255     }
1256 }
1257
1258 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1259                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1260 {
1261     uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1262
1263     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1264               dstParam[0], dstStride[0]);
1265
1266     if (c->dstFormat == PIX_FMT_NV12)
1267         interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1268     else
1269         interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1270
1271     return srcSliceH;
1272 }
1273
1274 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1275                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1276 {
1277     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1278
1279     yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1280
1281     return srcSliceH;
1282 }
1283
1284 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1285                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1286 {
1287     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1288
1289     yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1290
1291     return srcSliceH;
1292 }
1293
1294 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1295                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1296 {
1297     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1298
1299     yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1300
1301     return srcSliceH;
1302 }
1303
1304 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1305                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1306 {
1307     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1308
1309     yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1310
1311     return srcSliceH;
1312 }
1313
1314 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1315                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1316 {
1317     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1318     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1319     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1320
1321     yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1322
1323     if (dstParam[3])
1324         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1325
1326     return srcSliceH;
1327 }
1328
1329 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1330                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1331 {
1332     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1333     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1334     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1335
1336     yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1337
1338     return srcSliceH;
1339 }
1340
1341 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1342                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1343 {
1344     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1345     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1346     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1347
1348     uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1349
1350     if (dstParam[3])
1351         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1352
1353     return srcSliceH;
1354 }
1355
1356 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1357                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1358 {
1359     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1360     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1361     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1362
1363     uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1364
1365     return srcSliceH;
1366 }
1367
1368 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1369 {
1370     int i;
1371     for (i=0; i<num_pixels; i++)
1372         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1373 }
1374
1375 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1376 {
1377     int i;
1378
1379     for (i=0; i<num_pixels; i++)
1380         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1381 }
1382
1383 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1384 {
1385     int i;
1386
1387     for (i=0; i<num_pixels; i++) {
1388         //FIXME slow?
1389         dst[0]= palette[src[i<<1]*4+0];
1390         dst[1]= palette[src[i<<1]*4+1];
1391         dst[2]= palette[src[i<<1]*4+2];
1392         dst+= 3;
1393     }
1394 }
1395
1396 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1397                            int srcSliceH, uint8_t* dst[], int dstStride[])
1398 {
1399     const enum PixelFormat srcFormat= c->srcFormat;
1400     const enum PixelFormat dstFormat= c->dstFormat;
1401     void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
1402                  const uint8_t *palette)=NULL;
1403     int i;
1404     uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1405     const uint8_t *srcPtr= src[0];
1406
1407     if (srcFormat == PIX_FMT_Y400A) {
1408         switch (dstFormat) {
1409         case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1410         case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1411         case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1412         case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1413         case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1414         case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1415         }
1416     } else if (usePal(srcFormat)) {
1417         switch (dstFormat) {
1418         case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1419         case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1420         case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1421         case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1422         case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1423         case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1424         }
1425     }
1426
1427     if (!conv)
1428         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1429                sws_format_name(srcFormat), sws_format_name(dstFormat));
1430     else {
1431         for (i=0; i<srcSliceH; i++) {
1432             conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1433             srcPtr+= srcStride[0];
1434             dstPtr+= dstStride[0];
1435         }
1436     }
1437
1438     return srcSliceH;
1439 }
1440
1441 #define isRGBA32(x) (            \
1442            (x) == PIX_FMT_ARGB   \
1443         || (x) == PIX_FMT_RGBA   \
1444         || (x) == PIX_FMT_BGRA   \
1445         || (x) == PIX_FMT_ABGR   \
1446         )
1447
1448 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1449 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1450                            int srcSliceH, uint8_t* dst[], int dstStride[])
1451 {
1452     const enum PixelFormat srcFormat= c->srcFormat;
1453     const enum PixelFormat dstFormat= c->dstFormat;
1454     const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1455     const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1456     const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1457     const int dstId= c->dstFormatBpp >> 2;
1458     void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
1459
1460 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1461
1462     if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1463         if (     CONV_IS(ABGR, RGBA)
1464               || CONV_IS(ARGB, BGRA)
1465               || CONV_IS(BGRA, ARGB)
1466               || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1467         else if (CONV_IS(ABGR, ARGB)
1468               || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1469         else if (CONV_IS(ABGR, BGRA)
1470               || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1471         else if (CONV_IS(BGRA, RGBA)
1472               || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1473         else if (CONV_IS(BGRA, ABGR)
1474               || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1475     } else
1476     /* BGR -> BGR */
1477     if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1478        || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1479         switch(srcId | (dstId<<4)) {
1480         case 0x34: conv= rgb16to15; break;
1481         case 0x36: conv= rgb24to15; break;
1482         case 0x38: conv= rgb32to15; break;
1483         case 0x43: conv= rgb15to16; break;
1484         case 0x46: conv= rgb24to16; break;
1485         case 0x48: conv= rgb32to16; break;
1486         case 0x63: conv= rgb15to24; break;
1487         case 0x64: conv= rgb16to24; break;
1488         case 0x68: conv= rgb32to24; break;
1489         case 0x83: conv= rgb15to32; break;
1490         case 0x84: conv= rgb16to32; break;
1491         case 0x86: conv= rgb24to32; break;
1492         }
1493     } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1494              || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1495         switch(srcId | (dstId<<4)) {
1496         case 0x33: conv= rgb15tobgr15; break;
1497         case 0x34: conv= rgb16tobgr15; break;
1498         case 0x36: conv= rgb24tobgr15; break;
1499         case 0x38: conv= rgb32tobgr15; break;
1500         case 0x43: conv= rgb15tobgr16; break;
1501         case 0x44: conv= rgb16tobgr16; break;
1502         case 0x46: conv= rgb24tobgr16; break;
1503         case 0x48: conv= rgb32tobgr16; break;
1504         case 0x63: conv= rgb15tobgr24; break;
1505         case 0x64: conv= rgb16tobgr24; break;
1506         case 0x66: conv= rgb24tobgr24; break;
1507         case 0x68: conv= rgb32tobgr24; break;
1508         case 0x83: conv= rgb15tobgr32; break;
1509         case 0x84: conv= rgb16tobgr32; break;
1510         case 0x86: conv= rgb24tobgr32; break;
1511         }
1512     }
1513
1514     if (!conv) {
1515         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1516                sws_format_name(srcFormat), sws_format_name(dstFormat));
1517     } else {
1518         const uint8_t *srcPtr= src[0];
1519               uint8_t *dstPtr= dst[0];
1520         if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1521             srcPtr += ALT32_CORR;
1522
1523         if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1524             dstPtr += ALT32_CORR;
1525
1526         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1527             conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1528         else {
1529             int i;
1530             dstPtr += dstStride[0]*srcSliceY;
1531
1532             for (i=0; i<srcSliceH; i++) {
1533                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1534                 srcPtr+= srcStride[0];
1535                 dstPtr+= dstStride[0];
1536             }
1537         }
1538     }
1539     return srcSliceH;
1540 }
1541
1542 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1543                               int srcSliceH, uint8_t* dst[], int dstStride[])
1544 {
1545     rgb24toyv12(
1546         src[0],
1547         dst[0]+ srcSliceY    *dstStride[0],
1548         dst[1]+(srcSliceY>>1)*dstStride[1],
1549         dst[2]+(srcSliceY>>1)*dstStride[2],
1550         c->srcW, srcSliceH,
1551         dstStride[0], dstStride[1], srcStride[0]);
1552     if (dst[3])
1553         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1554     return srcSliceH;
1555 }
1556
1557 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1558                              int srcSliceH, uint8_t* dst[], int dstStride[])
1559 {
1560     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1561               dst[0], dstStride[0]);
1562
1563     planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1564              srcSliceH >> 2, srcStride[1], dstStride[1]);
1565     planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1566              srcSliceH >> 2, srcStride[2], dstStride[2]);
1567     if (dst[3])
1568         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1569     return srcSliceH;
1570 }
1571
1572 /* unscaled copy like stuff (assumes nearly identical formats) */
1573 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1574                              int srcSliceH, uint8_t* dst[], int dstStride[])
1575 {
1576     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1577         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1578     else {
1579         int i;
1580         const uint8_t *srcPtr= src[0];
1581         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1582         int length=0;
1583
1584         /* universal length finder */
1585         while(length+c->srcW <= FFABS(dstStride[0])
1586            && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1587         assert(length!=0);
1588
1589         for (i=0; i<srcSliceH; i++) {
1590             memcpy(dstPtr, srcPtr, length);
1591             srcPtr+= srcStride[0];
1592             dstPtr+= dstStride[0];
1593         }
1594     }
1595     return srcSliceH;
1596 }
1597
1598 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1599                              int srcSliceH, uint8_t* dst[], int dstStride[])
1600 {
1601     int plane, i, j;
1602     for (plane=0; plane<4; plane++) {
1603         int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1604         int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1605         int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1606         const uint8_t *srcPtr= src[plane];
1607         uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1608
1609         if (!dst[plane]) continue;
1610         // ignore palette for GRAY8
1611         if (plane == 1 && !dst[2]) continue;
1612         if (!src[plane] || (plane == 1 && !src[2])) {
1613             if(is16BPS(c->dstFormat))
1614                 length*=2;
1615             fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1616         } else {
1617             if(is9_OR_10BPS(c->srcFormat)) {
1618                 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1619                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1620                 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1621
1622                 if (is16BPS(c->dstFormat)) {
1623                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1624 #define COPY9_OR_10TO16(rfunc, wfunc) \
1625                     for (i = 0; i < height; i++) { \
1626                         for (j = 0; j < length; j++) { \
1627                             int srcpx = rfunc(&srcPtr2[j]); \
1628                             wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
1629                         } \
1630                         dstPtr2 += dstStride[plane]/2; \
1631                         srcPtr2 += srcStride[plane]/2; \
1632                     }
1633                     if (isBE(c->dstFormat)) {
1634                         if (isBE(c->srcFormat)) {
1635                             COPY9_OR_10TO16(AV_RB16, AV_WB16);
1636                         } else {
1637                             COPY9_OR_10TO16(AV_RL16, AV_WB16);
1638                         }
1639                     } else {
1640                         if (isBE(c->srcFormat)) {
1641                             COPY9_OR_10TO16(AV_RB16, AV_WL16);
1642                         } else {
1643                             COPY9_OR_10TO16(AV_RL16, AV_WL16);
1644                         }
1645                     }
1646                 } else if (is9_OR_10BPS(c->dstFormat)) {
1647                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1648 #define COPY9_OR_10TO9_OR_10(loop) \
1649                     for (i = 0; i < height; i++) { \
1650                         for (j = 0; j < length; j++) { \
1651                             loop; \
1652                         } \
1653                         dstPtr2 += dstStride[plane]/2; \
1654                         srcPtr2 += srcStride[plane]/2; \
1655                     }
1656 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
1657                     if (dst_depth > src_depth) { \
1658                         COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
1659                             wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
1660                     } else if (dst_depth < src_depth) { \
1661                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
1662                     } else { \
1663                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
1664                     }
1665                     if (isBE(c->dstFormat)) {
1666                         if (isBE(c->srcFormat)) {
1667                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
1668                         } else {
1669                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
1670                         }
1671                     } else {
1672                         if (isBE(c->srcFormat)) {
1673                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
1674                         } else {
1675                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
1676                         }
1677                     }
1678                 } else {
1679                     // FIXME Maybe dither instead.
1680 #define COPY9_OR_10TO8(rfunc) \
1681                     for (i = 0; i < height; i++) { \
1682                         for (j = 0; j < length; j++) { \
1683                             dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
1684                         } \
1685                         dstPtr  += dstStride[plane]; \
1686                         srcPtr2 += srcStride[plane]/2; \
1687                     }
1688                     if (isBE(c->srcFormat)) {
1689                         COPY9_OR_10TO8(AV_RB16);
1690                     } else {
1691                         COPY9_OR_10TO8(AV_RL16);
1692                     }
1693                 }
1694             } else if(is9_OR_10BPS(c->dstFormat)) {
1695                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1696                 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1697
1698                 if (is16BPS(c->srcFormat)) {
1699                     const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1700 #define COPY16TO9_OR_10(rfunc, wfunc) \
1701                     for (i = 0; i < height; i++) { \
1702                         for (j = 0; j < length; j++) { \
1703                             wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
1704                         } \
1705                         dstPtr2 += dstStride[plane]/2; \
1706                         srcPtr2 += srcStride[plane]/2; \
1707                     }
1708                     if (isBE(c->dstFormat)) {
1709                         if (isBE(c->srcFormat)) {
1710                             COPY16TO9_OR_10(AV_RB16, AV_WB16);
1711                         } else {
1712                             COPY16TO9_OR_10(AV_RL16, AV_WB16);
1713                         }
1714                     } else {
1715                         if (isBE(c->srcFormat)) {
1716                             COPY16TO9_OR_10(AV_RB16, AV_WL16);
1717                         } else {
1718                             COPY16TO9_OR_10(AV_RL16, AV_WL16);
1719                         }
1720                     }
1721                 } else /* 8bit */ {
1722 #define COPY8TO9_OR_10(wfunc) \
1723                     for (i = 0; i < height; i++) { \
1724                         for (j = 0; j < length; j++) { \
1725                             const int srcpx = srcPtr[j]; \
1726                             wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
1727                         } \
1728                         dstPtr2 += dstStride[plane]/2; \
1729                         srcPtr  += srcStride[plane]; \
1730                     }
1731                     if (isBE(c->dstFormat)) {
1732                         COPY8TO9_OR_10(AV_WB16);
1733                     } else {
1734                         COPY8TO9_OR_10(AV_WL16);
1735                     }
1736                 }
1737             } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1738                 if (!isBE(c->srcFormat)) srcPtr++;
1739                 for (i=0; i<height; i++) {
1740                     for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1741                     srcPtr+= srcStride[plane];
1742                     dstPtr+= dstStride[plane];
1743                 }
1744             } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1745                 for (i=0; i<height; i++) {
1746                     for (j=0; j<length; j++) {
1747                         dstPtr[ j<<1   ] = srcPtr[j];
1748                         dstPtr[(j<<1)+1] = srcPtr[j];
1749                     }
1750                     srcPtr+= srcStride[plane];
1751                     dstPtr+= dstStride[plane];
1752                 }
1753             } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1754                   && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1755
1756                 for (i=0; i<height; i++) {
1757                     for (j=0; j<length; j++)
1758                         ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1759                     srcPtr+= srcStride[plane];
1760                     dstPtr+= dstStride[plane];
1761                 }
1762             } else if (dstStride[plane] == srcStride[plane] &&
1763                        srcStride[plane] > 0 && srcStride[plane] == length) {
1764                 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1765                        height*dstStride[plane]);
1766             } else {
1767                 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1768                     length*=2;
1769                 for (i=0; i<height; i++) {
1770                     memcpy(dstPtr, srcPtr, length);
1771                     srcPtr+= srcStride[plane];
1772                     dstPtr+= dstStride[plane];
1773                 }
1774             }
1775         }
1776     }
1777     return srcSliceH;
1778 }
1779
1780 void ff_get_unscaled_swscale(SwsContext *c)
1781 {
1782     const enum PixelFormat srcFormat = c->srcFormat;
1783     const enum PixelFormat dstFormat = c->dstFormat;
1784     const int flags = c->flags;
1785     const int dstH = c->dstH;
1786     int needsDither;
1787
1788     needsDither= isAnyRGB(dstFormat)
1789         &&  c->dstFormatBpp < 24
1790         && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1791
1792     /* yv12_to_nv12 */
1793     if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1794         c->swScale= planarToNv12Wrapper;
1795     }
1796     /* yuv2bgr */
1797     if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1798         && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1799         c->swScale= ff_yuv2rgb_get_func_ptr(c);
1800     }
1801
1802     if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1803         c->swScale= yvu9ToYv12Wrapper;
1804     }
1805
1806     /* bgr24toYV12 */
1807     if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1808         c->swScale= bgr24ToYv12Wrapper;
1809
1810     /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1811     if (   isAnyRGB(srcFormat)
1812         && isAnyRGB(dstFormat)
1813         && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1814         && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1815         && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1816         && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1817         && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1818         && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1819         && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1820         && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1821         && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1822         && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1823         && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1824         && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1825         && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1826         c->swScale= rgbToRgbWrapper;
1827
1828     if ((usePal(srcFormat) && (
1829         dstFormat == PIX_FMT_RGB32   ||
1830         dstFormat == PIX_FMT_RGB32_1 ||
1831         dstFormat == PIX_FMT_RGB24   ||
1832         dstFormat == PIX_FMT_BGR32   ||
1833         dstFormat == PIX_FMT_BGR32_1 ||
1834         dstFormat == PIX_FMT_BGR24)))
1835         c->swScale= palToRgbWrapper;
1836
1837     if (srcFormat == PIX_FMT_YUV422P) {
1838         if (dstFormat == PIX_FMT_YUYV422)
1839             c->swScale= yuv422pToYuy2Wrapper;
1840         else if (dstFormat == PIX_FMT_UYVY422)
1841             c->swScale= yuv422pToUyvyWrapper;
1842     }
1843
1844     /* LQ converters if -sws 0 or -sws 4*/
1845     if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1846         /* yv12_to_yuy2 */
1847         if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1848             if (dstFormat == PIX_FMT_YUYV422)
1849                 c->swScale= planarToYuy2Wrapper;
1850             else if (dstFormat == PIX_FMT_UYVY422)
1851                 c->swScale= planarToUyvyWrapper;
1852         }
1853     }
1854     if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1855         c->swScale= yuyvToYuv420Wrapper;
1856     if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1857         c->swScale= uyvyToYuv420Wrapper;
1858     if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1859         c->swScale= yuyvToYuv422Wrapper;
1860     if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1861         c->swScale= uyvyToYuv422Wrapper;
1862
1863 #if HAVE_ALTIVEC
1864     if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) &&
1865         !(c->flags & SWS_BITEXACT) &&
1866         srcFormat == PIX_FMT_YUV420P) {
1867         // unscaled YV12 -> packed YUV, we want speed
1868         if (dstFormat == PIX_FMT_YUYV422)
1869             c->swScale= yv12toyuy2_unscaled_altivec;
1870         else if (dstFormat == PIX_FMT_UYVY422)
1871             c->swScale= yv12touyvy_unscaled_altivec;
1872     }
1873 #endif
1874
1875     /* simple copy */
1876     if (  srcFormat == dstFormat
1877         || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1878         || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1879         || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1880         || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1881         || (isGray(dstFormat) && isGray(srcFormat))
1882         || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1883             && c->chrDstHSubSample == c->chrSrcHSubSample
1884             && c->chrDstVSubSample == c->chrSrcVSubSample
1885             && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1886             && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1887     {
1888         if (isPacked(c->srcFormat))
1889             c->swScale= packedCopyWrapper;
1890         else /* Planar YUV or gray */
1891             c->swScale= planarCopyWrapper;
1892     }
1893 #if ARCH_BFIN
1894     ff_bfin_get_unscaled_swscale (c);
1895 #endif
1896 }
1897
1898 static void reset_ptr(const uint8_t* src[], int format)
1899 {
1900     if(!isALPHA(format))
1901         src[3]=NULL;
1902     if(!isPlanarYUV(format)) {
1903         src[3]=src[2]=NULL;
1904
1905         if (!usePal(format))
1906             src[1]= NULL;
1907     }
1908 }
1909
1910 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1911                                 const int linesizes[4])
1912 {
1913     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1914     int i;
1915
1916     for (i = 0; i < 4; i++) {
1917         int plane = desc->comp[i].plane;
1918         if (!data[plane] || !linesizes[plane])
1919             return 0;
1920     }
1921
1922     return 1;
1923 }
1924
1925 /**
1926  * swscale wrapper, so we don't need to export the SwsContext.
1927  * Assumes planar YUV to be in YUV order instead of YVU.
1928  */
1929 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1930               int srcSliceH, uint8_t* const dst[], const int dstStride[])
1931 {
1932     int i;
1933     const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1934     uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1935
1936     // do not mess up sliceDir if we have a "trailing" 0-size slice
1937     if (srcSliceH == 0)
1938         return 0;
1939
1940     if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1941         av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1942         return 0;
1943     }
1944     if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1945         av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1946         return 0;
1947     }
1948
1949     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1950         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1951         return 0;
1952     }
1953     if (c->sliceDir == 0) {
1954         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1955     }
1956
1957     if (usePal(c->srcFormat)) {
1958         for (i=0; i<256; i++) {
1959             int p, r, g, b,y,u,v;
1960             if(c->srcFormat == PIX_FMT_PAL8) {
1961                 p=((const uint32_t*)(src[1]))[i];
1962                 r= (p>>16)&0xFF;
1963                 g= (p>> 8)&0xFF;
1964                 b=  p     &0xFF;
1965             } else if(c->srcFormat == PIX_FMT_RGB8) {
1966                 r= (i>>5    )*36;
1967                 g= ((i>>2)&7)*36;
1968                 b= (i&3     )*85;
1969             } else if(c->srcFormat == PIX_FMT_BGR8) {
1970                 b= (i>>6    )*85;
1971                 g= ((i>>3)&7)*36;
1972                 r= (i&7     )*36;
1973             } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1974                 r= (i>>3    )*255;
1975                 g= ((i>>1)&3)*85;
1976                 b= (i&1     )*255;
1977             } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1978                 r = g = b = i;
1979             } else {
1980                 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1981                 b= (i>>3    )*255;
1982                 g= ((i>>1)&3)*85;
1983                 r= (i&1     )*255;
1984             }
1985             y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1986             u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1987             v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1988             c->pal_yuv[i]= y + (u<<8) + (v<<16);
1989
1990             switch(c->dstFormat) {
1991             case PIX_FMT_BGR32:
1992 #if !HAVE_BIGENDIAN
1993             case PIX_FMT_RGB24:
1994 #endif
1995                 c->pal_rgb[i]=  r + (g<<8) + (b<<16);
1996                 break;
1997             case PIX_FMT_BGR32_1:
1998 #if HAVE_BIGENDIAN
1999             case PIX_FMT_BGR24:
2000 #endif
2001                 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2002                 break;
2003             case PIX_FMT_RGB32_1:
2004 #if HAVE_BIGENDIAN
2005             case PIX_FMT_RGB24:
2006 #endif
2007                 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2008                 break;
2009             case PIX_FMT_RGB32:
2010 #if !HAVE_BIGENDIAN
2011             case PIX_FMT_BGR24:
2012 #endif
2013             default:
2014                 c->pal_rgb[i]=  b + (g<<8) + (r<<16);
2015             }
2016         }
2017     }
2018
2019     // copy strides, so they can safely be modified
2020     if (c->sliceDir == 1) {
2021         // slices go from top to bottom
2022         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2023         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2024
2025         reset_ptr(src2, c->srcFormat);
2026         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2027
2028         /* reset slice direction at end of frame */
2029         if (srcSliceY + srcSliceH == c->srcH)
2030             c->sliceDir = 0;
2031
2032         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2033     } else {
2034         // slices go from bottom to top => we flip the image internally
2035         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2036         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2037
2038         src2[0] += (srcSliceH-1)*srcStride[0];
2039         if (!usePal(c->srcFormat))
2040             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2041         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2042         src2[3] += (srcSliceH-1)*srcStride[3];
2043         dst2[0] += ( c->dstH                      -1)*dstStride[0];
2044         dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2045         dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2046         dst2[3] += ( c->dstH                      -1)*dstStride[3];
2047
2048         reset_ptr(src2, c->srcFormat);
2049         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2050
2051         /* reset slice direction at end of frame */
2052         if (!srcSliceY)
2053             c->sliceDir = 0;
2054
2055         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2056     }
2057 }
2058
2059 /* Convert the palette to the same packed 32-bit format as the palette */
2060 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2061 {
2062     int i;
2063
2064     for (i=0; i<num_pixels; i++)
2065         ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2066 }
2067
2068 /* Palette format: ABCD -> dst format: ABC */
2069 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2070 {
2071     int i;
2072
2073     for (i=0; i<num_pixels; i++) {
2074         //FIXME slow?
2075         dst[0]= palette[src[i]*4+0];
2076         dst[1]= palette[src[i]*4+1];
2077         dst[2]= palette[src[i]*4+2];
2078         dst+= 3;
2079     }
2080 }