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