2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27 {BGR,RGB}{1,4,8,15,16} support dithering
29 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30 YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
41 tested special converters (most are tested actually, but I did not write it down ...)
48 untested special converters
49 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
50 YV12/I420 -> YV12/I420
51 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
52 BGR24 -> BGR32 & RGB24 -> RGB32
53 BGR32 -> BGR24 & RGB32 -> RGB24
64 #include "swscale_internal.h"
66 #include "libavutil/intreadwrite.h"
67 #include "libavutil/x86_cpu.h"
68 #include "libavutil/avutil.h"
69 #include "libavutil/bswap.h"
70 #include "libavutil/pixdesc.h"
76 //#define HAVE_AMD3DNOW
81 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
86 #define PI 3.14159265358979323846
89 #define isPacked(x) ( \
91 || (x)==PIX_FMT_YUYV422 \
92 || (x)==PIX_FMT_UYVY422 \
95 #define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
97 #define RGB2YUV_SHIFT 15
98 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
99 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
100 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
101 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
102 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
103 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
104 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
105 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
106 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
108 static const double rgb2yuv_table[8][9]={
109 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
110 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
111 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
112 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
113 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
114 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
115 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
116 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
121 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
124 more intelligent misalignment avoidance for the horizontal scaler
125 write special vertical cubic upscale version
126 optimize C code (YV12 / minmax)
127 add support for packed pixel YUV input & output
128 add support for Y8 output
129 optimize BGR24 & BGR32
130 add BGR4 output support
131 write special BGR->BGR scaler
134 #if ARCH_X86 && CONFIG_GPL
135 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
136 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
137 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
138 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
139 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
140 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
141 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
142 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
144 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
145 0x0103010301030103LL,
146 0x0200020002000200LL,};
148 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
149 0x0602060206020602LL,
150 0x0004000400040004LL,};
152 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
153 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
154 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
155 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
156 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
157 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
159 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
160 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
161 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
164 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
165 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
166 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
168 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
169 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
170 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
171 #endif /* FAST_BGR2YV12 */
172 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
173 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
174 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
176 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
177 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
178 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
179 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
180 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
182 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
183 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
184 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
187 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
189 #endif /* ARCH_X86 && CONFIG_GPL */
191 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
192 { 1, 3, 1, 3, 1, 3, 1, 3, },
193 { 2, 0, 2, 0, 2, 0, 2, 0, },
196 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
197 { 6, 2, 6, 2, 6, 2, 6, 2, },
198 { 0, 4, 0, 4, 0, 4, 0, 4, },
201 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
202 { 8, 4, 11, 7, 8, 4, 11, 7, },
203 { 2, 14, 1, 13, 2, 14, 1, 13, },
204 { 10, 6, 9, 5, 10, 6, 9, 5, },
205 { 0, 12, 3, 15, 0, 12, 3, 15, },
208 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
209 { 17, 9, 23, 15, 16, 8, 22, 14, },
210 { 5, 29, 3, 27, 4, 28, 2, 26, },
211 { 21, 13, 19, 11, 20, 12, 18, 10, },
212 { 0, 24, 6, 30, 1, 25, 7, 31, },
213 { 16, 8, 22, 14, 17, 9, 23, 15, },
214 { 4, 28, 2, 26, 5, 29, 3, 27, },
215 { 20, 12, 18, 10, 21, 13, 19, 11, },
216 { 1, 25, 7, 31, 0, 24, 6, 30, },
219 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
220 { 0, 55, 14, 68, 3, 58, 17, 72, },
221 { 37, 18, 50, 32, 40, 22, 54, 35, },
222 { 9, 64, 5, 59, 13, 67, 8, 63, },
223 { 46, 27, 41, 23, 49, 31, 44, 26, },
224 { 2, 57, 16, 71, 1, 56, 15, 70, },
225 { 39, 21, 52, 34, 38, 19, 51, 33, },
226 { 11, 66, 7, 62, 10, 65, 6, 60, },
227 { 48, 30, 43, 25, 47, 29, 42, 24, },
231 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
232 {117, 62, 158, 103, 113, 58, 155, 100, },
233 { 34, 199, 21, 186, 31, 196, 17, 182, },
234 {144, 89, 131, 76, 141, 86, 127, 72, },
235 { 0, 165, 41, 206, 10, 175, 52, 217, },
236 {110, 55, 151, 96, 120, 65, 162, 107, },
237 { 28, 193, 14, 179, 38, 203, 24, 189, },
238 {138, 83, 124, 69, 148, 93, 134, 79, },
239 { 7, 172, 48, 213, 3, 168, 45, 210, },
242 // tries to correct a gamma of 1.5
243 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
244 { 0, 143, 18, 200, 2, 156, 25, 215, },
245 { 78, 28, 125, 64, 89, 36, 138, 74, },
246 { 10, 180, 3, 161, 16, 195, 8, 175, },
247 {109, 51, 93, 38, 121, 60, 105, 47, },
248 { 1, 152, 23, 210, 0, 147, 20, 205, },
249 { 85, 33, 134, 71, 81, 30, 130, 67, },
250 { 14, 190, 6, 171, 12, 185, 5, 166, },
251 {117, 57, 101, 44, 113, 54, 97, 41, },
254 // tries to correct a gamma of 2.0
255 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
256 { 0, 124, 8, 193, 0, 140, 12, 213, },
257 { 55, 14, 104, 42, 66, 19, 119, 52, },
258 { 3, 168, 1, 145, 6, 187, 3, 162, },
259 { 86, 31, 70, 21, 99, 39, 82, 28, },
260 { 0, 134, 11, 206, 0, 129, 9, 200, },
261 { 62, 17, 114, 48, 58, 16, 109, 45, },
262 { 5, 181, 2, 157, 4, 175, 1, 151, },
263 { 95, 36, 78, 26, 90, 34, 74, 24, },
266 // tries to correct a gamma of 2.5
267 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
268 { 0, 107, 3, 187, 0, 125, 6, 212, },
269 { 39, 7, 86, 28, 49, 11, 102, 36, },
270 { 1, 158, 0, 131, 3, 180, 1, 151, },
271 { 68, 19, 52, 12, 81, 25, 64, 17, },
272 { 0, 119, 5, 203, 0, 113, 4, 195, },
273 { 45, 9, 96, 33, 42, 8, 91, 30, },
274 { 2, 172, 1, 144, 2, 165, 0, 137, },
275 { 77, 23, 60, 15, 72, 21, 56, 14, },
279 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
280 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
281 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
282 int dstW, int chrDstW, int big_endian)
284 //FIXME Optimize (just quickly written not optimized..)
287 for (i = 0; i < dstW; i++) {
291 for (j = 0; j < lumFilterSize; j++)
292 val += lumSrc[j][i] * lumFilter[j];
295 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
297 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
302 for (i = 0; i < chrDstW; i++) {
307 for (j = 0; j < chrFilterSize; j++) {
308 u += chrSrc[j][i ] * chrFilter[j];
309 v += chrSrc[j][i + VOFW] * chrFilter[j];
313 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
314 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
316 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
317 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
322 if (CONFIG_SWSCALE_ALPHA && aDest) {
323 for (i = 0; i < dstW; i++) {
327 for (j = 0; j < lumFilterSize; j++)
328 val += alpSrc[j][i] * lumFilter[j];
331 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
333 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
339 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
340 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
341 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
342 enum PixelFormat dstFormat)
344 if (isBE(dstFormat)) {
345 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
346 chrFilter, chrSrc, chrFilterSize,
348 dest, uDest, vDest, aDest,
351 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
352 chrFilter, chrSrc, chrFilterSize,
354 dest, uDest, vDest, aDest,
359 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
360 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
361 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
363 //FIXME Optimize (just quickly written not optimized..)
365 for (i=0; i<dstW; i++) {
368 for (j=0; j<lumFilterSize; j++)
369 val += lumSrc[j][i] * lumFilter[j];
371 dest[i]= av_clip_uint8(val>>19);
375 for (i=0; i<chrDstW; i++) {
379 for (j=0; j<chrFilterSize; j++) {
380 u += chrSrc[j][i] * chrFilter[j];
381 v += chrSrc[j][i + VOFW] * chrFilter[j];
384 uDest[i]= av_clip_uint8(u>>19);
385 vDest[i]= av_clip_uint8(v>>19);
388 if (CONFIG_SWSCALE_ALPHA && aDest)
389 for (i=0; i<dstW; i++) {
392 for (j=0; j<lumFilterSize; j++)
393 val += alpSrc[j][i] * lumFilter[j];
395 aDest[i]= av_clip_uint8(val>>19);
400 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
401 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
402 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
404 //FIXME Optimize (just quickly written not optimized..)
406 for (i=0; i<dstW; i++) {
409 for (j=0; j<lumFilterSize; j++)
410 val += lumSrc[j][i] * lumFilter[j];
412 dest[i]= av_clip_uint8(val>>19);
418 if (dstFormat == PIX_FMT_NV12)
419 for (i=0; i<chrDstW; i++) {
423 for (j=0; j<chrFilterSize; j++) {
424 u += chrSrc[j][i] * chrFilter[j];
425 v += chrSrc[j][i + VOFW] * chrFilter[j];
428 uDest[2*i]= av_clip_uint8(u>>19);
429 uDest[2*i+1]= av_clip_uint8(v>>19);
432 for (i=0; i<chrDstW; i++) {
436 for (j=0; j<chrFilterSize; j++) {
437 u += chrSrc[j][i] * chrFilter[j];
438 v += chrSrc[j][i + VOFW] * chrFilter[j];
441 uDest[2*i]= av_clip_uint8(v>>19);
442 uDest[2*i+1]= av_clip_uint8(u>>19);
446 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
447 for (i=0; i<(dstW>>1); i++) {\
453 int av_unused A1, A2;\
454 type av_unused *r, *b, *g;\
457 for (j=0; j<lumFilterSize; j++) {\
458 Y1 += lumSrc[j][i2] * lumFilter[j];\
459 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
461 for (j=0; j<chrFilterSize; j++) {\
462 U += chrSrc[j][i] * chrFilter[j];\
463 V += chrSrc[j][i+VOFW] * chrFilter[j];\
472 for (j=0; j<lumFilterSize; j++) {\
473 A1 += alpSrc[j][i2 ] * lumFilter[j];\
474 A2 += alpSrc[j][i2+1] * lumFilter[j];\
480 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
481 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
482 if ((Y1|Y2|U|V)&256) {\
483 if (Y1>255) Y1=255; \
484 else if (Y1<0)Y1=0; \
485 if (Y2>255) Y2=255; \
486 else if (Y2<0)Y2=0; \
492 if (alpha && ((A1|A2)&256)) {\
493 A1=av_clip_uint8(A1);\
494 A2=av_clip_uint8(A2);\
497 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
498 for (i=0; i<dstW; i++) {\
506 for (j=0; j<lumFilterSize; j++) {\
507 Y += lumSrc[j][i ] * lumFilter[j];\
509 for (j=0; j<chrFilterSize; j++) {\
510 U += chrSrc[j][i ] * chrFilter[j];\
511 V += chrSrc[j][i+VOFW] * chrFilter[j];\
518 for (j=0; j<lumFilterSize; j++)\
519 A += alpSrc[j][i ] * lumFilter[j];\
522 A = av_clip_uint8(A);\
525 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
526 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
527 Y-= c->yuv2rgb_y_offset;\
528 Y*= c->yuv2rgb_y_coeff;\
530 R= Y + V*c->yuv2rgb_v2r_coeff;\
531 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
532 B= Y + U*c->yuv2rgb_u2b_coeff;\
533 if ((R|G|B)&(0xC0000000)) {\
534 if (R>=(256<<22)) R=(256<<22)-1; \
536 if (G>=(256<<22)) G=(256<<22)-1; \
538 if (B>=(256<<22)) B=(256<<22)-1; \
542 #define YSCALE_YUV_2_GRAY16_C \
543 for (i=0; i<(dstW>>1); i++) {\
552 for (j=0; j<lumFilterSize; j++) {\
553 Y1 += lumSrc[j][i2] * lumFilter[j];\
554 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
558 if ((Y1|Y2|U|V)&65536) {\
559 if (Y1>65535) Y1=65535; \
560 else if (Y1<0)Y1=0; \
561 if (Y2>65535) Y2=65535; \
562 else if (Y2<0)Y2=0; \
565 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
566 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
567 r = (type *)c->table_rV[V]; \
568 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
569 b = (type *)c->table_bU[U];
571 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
572 for (i=0; i<(dstW>>1); i++) { \
574 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
575 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
576 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
577 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
578 type av_unused *r, *b, *g; \
579 int av_unused A1, A2; \
581 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
582 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
585 #define YSCALE_YUV_2_GRAY16_2_C \
586 for (i=0; i<(dstW>>1); i++) { \
588 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
589 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
591 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
592 YSCALE_YUV_2_PACKED2_C(type,alpha)\
593 r = (type *)c->table_rV[V];\
594 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
595 b = (type *)c->table_bU[U];
597 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
598 for (i=0; i<(dstW>>1); i++) {\
600 int Y1= buf0[i2 ]>>7;\
601 int Y2= buf0[i2+1]>>7;\
602 int U= (uvbuf1[i ])>>7;\
603 int V= (uvbuf1[i+VOFW])>>7;\
604 type av_unused *r, *b, *g;\
605 int av_unused A1, A2;\
611 #define YSCALE_YUV_2_GRAY16_1_C \
612 for (i=0; i<(dstW>>1); i++) {\
614 int Y1= buf0[i2 ]<<1;\
615 int Y2= buf0[i2+1]<<1;
617 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
618 YSCALE_YUV_2_PACKED1_C(type,alpha)\
619 r = (type *)c->table_rV[V];\
620 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
621 b = (type *)c->table_bU[U];
623 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
624 for (i=0; i<(dstW>>1); i++) {\
626 int Y1= buf0[i2 ]>>7;\
627 int Y2= buf0[i2+1]>>7;\
628 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
629 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
630 type av_unused *r, *b, *g;\
631 int av_unused A1, A2;\
637 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
638 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
639 r = (type *)c->table_rV[V];\
640 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
641 b = (type *)c->table_bU[U];
643 #define YSCALE_YUV_2_MONO2_C \
644 const uint8_t * const d128=dither_8x8_220[y&7];\
645 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
646 for (i=0; i<dstW-7; i+=8) {\
648 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
649 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
650 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
651 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
652 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
653 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
654 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
655 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
656 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
660 #define YSCALE_YUV_2_MONOX_C \
661 const uint8_t * const d128=dither_8x8_220[y&7];\
662 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
664 for (i=0; i<dstW-1; i+=2) {\
669 for (j=0; j<lumFilterSize; j++) {\
670 Y1 += lumSrc[j][i] * lumFilter[j];\
671 Y2 += lumSrc[j][i+1] * lumFilter[j];\
681 acc+= acc + g[Y1+d128[(i+0)&7]];\
682 acc+= acc + g[Y2+d128[(i+1)&7]];\
684 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
689 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
690 switch(c->dstFormat) {\
691 case PIX_FMT_RGB48BE:\
692 case PIX_FMT_RGB48LE:\
694 ((uint8_t*)dest)[ 0]= r[Y1];\
695 ((uint8_t*)dest)[ 1]= r[Y1];\
696 ((uint8_t*)dest)[ 2]= g[Y1];\
697 ((uint8_t*)dest)[ 3]= g[Y1];\
698 ((uint8_t*)dest)[ 4]= b[Y1];\
699 ((uint8_t*)dest)[ 5]= b[Y1];\
700 ((uint8_t*)dest)[ 6]= r[Y2];\
701 ((uint8_t*)dest)[ 7]= r[Y2];\
702 ((uint8_t*)dest)[ 8]= g[Y2];\
703 ((uint8_t*)dest)[ 9]= g[Y2];\
704 ((uint8_t*)dest)[10]= b[Y2];\
705 ((uint8_t*)dest)[11]= b[Y2];\
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<<24) : 0);\
715 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
718 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
720 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
721 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
725 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
726 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
734 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
735 func(uint32_t,needAlpha)\
736 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
737 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
740 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
742 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
743 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
747 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
748 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
755 ((uint8_t*)dest)[0]= r[Y1];\
756 ((uint8_t*)dest)[1]= g[Y1];\
757 ((uint8_t*)dest)[2]= b[Y1];\
758 ((uint8_t*)dest)[3]= r[Y2];\
759 ((uint8_t*)dest)[4]= g[Y2];\
760 ((uint8_t*)dest)[5]= b[Y2];\
766 ((uint8_t*)dest)[0]= b[Y1];\
767 ((uint8_t*)dest)[1]= g[Y1];\
768 ((uint8_t*)dest)[2]= r[Y1];\
769 ((uint8_t*)dest)[3]= b[Y2];\
770 ((uint8_t*)dest)[4]= g[Y2];\
771 ((uint8_t*)dest)[5]= r[Y2];\
775 case PIX_FMT_RGB565BE:\
776 case PIX_FMT_RGB565LE:\
777 case PIX_FMT_BGR565BE:\
778 case PIX_FMT_BGR565LE:\
780 const int dr1= dither_2x2_8[y&1 ][0];\
781 const int dg1= dither_2x2_4[y&1 ][0];\
782 const int db1= dither_2x2_8[(y&1)^1][0];\
783 const int dr2= dither_2x2_8[y&1 ][1];\
784 const int dg2= dither_2x2_4[y&1 ][1];\
785 const int db2= dither_2x2_8[(y&1)^1][1];\
787 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
788 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
792 case PIX_FMT_RGB555BE:\
793 case PIX_FMT_RGB555LE:\
794 case PIX_FMT_BGR555BE:\
795 case PIX_FMT_BGR555LE:\
797 const int dr1= dither_2x2_8[y&1 ][0];\
798 const int dg1= dither_2x2_8[y&1 ][1];\
799 const int db1= dither_2x2_8[(y&1)^1][0];\
800 const int dr2= dither_2x2_8[y&1 ][1];\
801 const int dg2= dither_2x2_8[y&1 ][0];\
802 const int db2= dither_2x2_8[(y&1)^1][1];\
804 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
805 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
812 const uint8_t * const d64= dither_8x8_73[y&7];\
813 const uint8_t * const d32= dither_8x8_32[y&7];\
815 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
816 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
823 const uint8_t * const d64= dither_8x8_73 [y&7];\
824 const uint8_t * const d128=dither_8x8_220[y&7];\
826 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
827 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
831 case PIX_FMT_RGB4_BYTE:\
832 case PIX_FMT_BGR4_BYTE:\
834 const uint8_t * const d64= dither_8x8_73 [y&7];\
835 const uint8_t * const d128=dither_8x8_220[y&7];\
837 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
838 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
842 case PIX_FMT_MONOBLACK:\
843 case PIX_FMT_MONOWHITE:\
848 case PIX_FMT_YUYV422:\
850 ((uint8_t*)dest)[2*i2+0]= Y1;\
851 ((uint8_t*)dest)[2*i2+1]= U;\
852 ((uint8_t*)dest)[2*i2+2]= Y2;\
853 ((uint8_t*)dest)[2*i2+3]= V;\
856 case PIX_FMT_UYVY422:\
858 ((uint8_t*)dest)[2*i2+0]= U;\
859 ((uint8_t*)dest)[2*i2+1]= Y1;\
860 ((uint8_t*)dest)[2*i2+2]= V;\
861 ((uint8_t*)dest)[2*i2+3]= Y2;\
864 case PIX_FMT_GRAY16BE:\
866 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
867 ((uint8_t*)dest)[2*i2+1]= Y1;\
868 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
869 ((uint8_t*)dest)[2*i2+3]= Y2;\
872 case PIX_FMT_GRAY16LE:\
874 ((uint8_t*)dest)[2*i2+0]= Y1;\
875 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
876 ((uint8_t*)dest)[2*i2+2]= Y2;\
877 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
882 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
883 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
884 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
887 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)
890 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
891 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
892 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
895 int step= c->dstFormatBpp/8;
898 switch(c->dstFormat) {
906 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
907 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
908 dest[aidx]= needAlpha ? A : 255;
915 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
916 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
924 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
941 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
942 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
943 dest[aidx]= needAlpha ? A : 255;
950 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
951 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
959 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
974 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
977 uint8_t *ptr = plane + stride*y;
978 for (i=0; i<height; i++) {
979 memset(ptr, val, width);
984 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
988 for (i = 0; i < width; i++) {
993 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
997 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
998 const uint8_t *src1, const uint8_t *src2,
999 int width, uint32_t *unused)
1003 for (i = 0; i < width; i++) {
1004 int r = src1[6*i + 0];
1005 int g = src1[6*i + 2];
1006 int b = src1[6*i + 4];
1008 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1009 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1013 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1014 const uint8_t *src1, const uint8_t *src2,
1015 int width, uint32_t *unused)
1019 for (i = 0; i < width; i++) {
1020 int r= src1[12*i + 0] + src1[12*i + 6];
1021 int g= src1[12*i + 2] + src1[12*i + 8];
1022 int b= src1[12*i + 4] + src1[12*i + 10];
1024 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1025 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1029 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1030 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1033 for (i=0; i<width; i++) {\
1034 int b= (((const type*)src)[i]>>shb)&maskb;\
1035 int g= (((const type*)src)[i]>>shg)&maskg;\
1036 int r= (((const type*)src)[i]>>shr)&maskr;\
1038 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1042 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1043 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1044 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1045 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1046 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1047 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1049 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1052 for (i=0; i<width; i++) {
1057 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1058 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1061 for (i=0; i<width; i++) {\
1062 int b= (((const type*)src)[i]&maskb)>>shb;\
1063 int g= (((const type*)src)[i]&maskg)>>shg;\
1064 int r= (((const type*)src)[i]&maskr)>>shr;\
1066 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1067 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1070 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1073 for (i=0; i<width; i++) {\
1074 int pix0= ((const type*)src)[2*i+0];\
1075 int pix1= ((const type*)src)[2*i+1];\
1076 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1077 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1078 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1079 g&= maskg|(2*maskg);\
1083 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1084 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1088 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1089 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1090 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1091 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1092 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1093 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1095 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1098 for (i=0; i<width; i++) {
1101 dst[i]= pal[d] & 0xFF;
1105 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1106 const uint8_t *src1, const uint8_t *src2,
1107 long width, uint32_t *pal)
1110 assert(src1 == src2);
1111 for (i=0; i<width; i++) {
1112 int p= pal[src1[i]];
1119 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1122 for (i=0; i<width/8; i++) {
1125 dst[8*i+j]= ((d>>(7-j))&1)*255;
1129 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1132 for (i=0; i<width/8; i++) {
1135 dst[8*i+j]= ((d>>(7-j))&1)*255;
1139 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1141 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1146 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1147 #define COMPILE_ALTIVEC
1153 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1157 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1158 #define COMPILE_MMX2
1161 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1162 #define COMPILE_3DNOW
1166 #define COMPILE_TEMPLATE_MMX 0
1167 #define COMPILE_TEMPLATE_MMX2 0
1168 #define COMPILE_TEMPLATE_AMD3DNOW 0
1169 #define COMPILE_TEMPLATE_ALTIVEC 0
1172 #define RENAME(a) a ## _C
1173 #include "swscale_template.c"
1176 #ifdef COMPILE_ALTIVEC
1178 #undef COMPILE_TEMPLATE_ALTIVEC
1179 #define COMPILE_TEMPLATE_ALTIVEC 1
1180 #define RENAME(a) a ## _altivec
1181 #include "swscale_template.c"
1189 #undef COMPILE_TEMPLATE_MMX
1190 #undef COMPILE_TEMPLATE_MMX2
1191 #undef COMPILE_TEMPLATE_AMD3DNOW
1192 #define COMPILE_TEMPLATE_MMX 1
1193 #define COMPILE_TEMPLATE_MMX2 0
1194 #define COMPILE_TEMPLATE_AMD3DNOW 0
1195 #define RENAME(a) a ## _MMX
1196 #include "swscale_template.c"
1202 #undef COMPILE_TEMPLATE_MMX
1203 #undef COMPILE_TEMPLATE_MMX2
1204 #undef COMPILE_TEMPLATE_AMD3DNOW
1205 #define COMPILE_TEMPLATE_MMX 1
1206 #define COMPILE_TEMPLATE_MMX2 1
1207 #define COMPILE_TEMPLATE_AMD3DNOW 0
1208 #define RENAME(a) a ## _MMX2
1209 #include "swscale_template.c"
1213 #ifdef COMPILE_3DNOW
1215 #undef COMPILE_TEMPLATE_MMX
1216 #undef COMPILE_TEMPLATE_MMX2
1217 #undef COMPILE_TEMPLATE_AMD3DNOW
1218 #define COMPILE_TEMPLATE_MMX 1
1219 #define COMPILE_TEMPLATE_MMX2 0
1220 #define COMPILE_TEMPLATE_AMD3DNOW 1
1221 #define RENAME(a) a ## _3DNow
1222 #include "swscale_template.c"
1227 SwsFunc ff_getSwsFunc(SwsContext *c)
1229 #if CONFIG_RUNTIME_CPUDETECT
1230 int flags = c->flags;
1232 #if ARCH_X86 && CONFIG_GPL
1233 // ordered per speed fastest first
1234 if (flags & SWS_CPU_CAPS_MMX2) {
1235 sws_init_swScale_MMX2(c);
1236 return swScale_MMX2;
1237 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1238 sws_init_swScale_3DNow(c);
1239 return swScale_3DNow;
1240 } else if (flags & SWS_CPU_CAPS_MMX) {
1241 sws_init_swScale_MMX(c);
1244 sws_init_swScale_C(c);
1250 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1251 sws_init_swScale_altivec(c);
1252 return swScale_altivec;
1254 sws_init_swScale_C(c);
1258 sws_init_swScale_C(c);
1260 #endif /* ARCH_X86 && CONFIG_GPL */
1261 #else //CONFIG_RUNTIME_CPUDETECT
1262 #if COMPILE_TEMPLATE_MMX2
1263 sws_init_swScale_MMX2(c);
1264 return swScale_MMX2;
1265 #elif COMPILE_TEMPLATE_AMD3DNOW
1266 sws_init_swScale_3DNow(c);
1267 return swScale_3DNow;
1268 #elif COMPILE_TEMPLATE_MMX
1269 sws_init_swScale_MMX(c);
1271 #elif COMPILE_TEMPLATE_ALTIVEC
1272 sws_init_swScale_altivec(c);
1273 return swScale_altivec;
1275 sws_init_swScale_C(c);
1278 #endif //!CONFIG_RUNTIME_CPUDETECT
1281 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1282 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1284 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1286 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1287 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1290 const uint8_t *srcPtr= src[0];
1291 uint8_t *dstPtr= dst;
1292 for (i=0; i<srcSliceH; i++) {
1293 memcpy(dstPtr, srcPtr, c->srcW);
1294 srcPtr+= srcStride[0];
1295 dstPtr+= dstStride[0];
1298 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1299 if (c->dstFormat == PIX_FMT_NV12)
1300 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1302 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1307 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1308 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1310 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1312 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1317 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1318 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1320 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1322 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1327 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1328 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1330 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1332 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1337 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1338 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1340 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1342 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1347 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1348 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1350 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1351 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1352 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1354 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1357 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1362 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1363 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1365 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1366 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1367 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1369 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1374 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1375 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1377 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1378 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1379 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1381 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1384 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1389 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1390 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1392 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1393 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1394 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1396 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1401 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1402 int srcSliceH, uint8_t* dst[], int dstStride[])
1404 const enum PixelFormat srcFormat= c->srcFormat;
1405 const enum PixelFormat dstFormat= c->dstFormat;
1406 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1407 const uint8_t *palette)=NULL;
1409 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1410 const uint8_t *srcPtr= src[0];
1412 if (usePal(srcFormat)) {
1413 switch (dstFormat) {
1414 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1415 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1416 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1417 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1418 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1419 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
1424 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1425 sws_format_name(srcFormat), sws_format_name(dstFormat));
1427 for (i=0; i<srcSliceH; i++) {
1428 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1429 srcPtr+= srcStride[0];
1430 dstPtr+= dstStride[0];
1437 #define isRGBA32(x) ( \
1438 (x) == PIX_FMT_ARGB \
1439 || (x) == PIX_FMT_RGBA \
1440 || (x) == PIX_FMT_BGRA \
1441 || (x) == PIX_FMT_ABGR \
1444 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1445 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1446 int srcSliceH, uint8_t* dst[], int dstStride[])
1448 const enum PixelFormat srcFormat= c->srcFormat;
1449 const enum PixelFormat dstFormat= c->dstFormat;
1450 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1451 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1452 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1453 const int dstId= c->dstFormatBpp >> 2;
1454 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1456 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1458 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1459 if ( CONV_IS(ABGR, RGBA)
1460 || CONV_IS(ARGB, BGRA)
1461 || CONV_IS(BGRA, ARGB)
1462 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1463 else if (CONV_IS(ABGR, ARGB)
1464 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1465 else if (CONV_IS(ABGR, BGRA)
1466 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1467 else if (CONV_IS(BGRA, RGBA)
1468 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1469 else if (CONV_IS(BGRA, ABGR)
1470 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1473 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1474 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1475 switch(srcId | (dstId<<4)) {
1476 case 0x34: conv= rgb16to15; break;
1477 case 0x36: conv= rgb24to15; break;
1478 case 0x38: conv= rgb32to15; break;
1479 case 0x43: conv= rgb15to16; break;
1480 case 0x46: conv= rgb24to16; break;
1481 case 0x48: conv= rgb32to16; break;
1482 case 0x63: conv= rgb15to24; break;
1483 case 0x64: conv= rgb16to24; break;
1484 case 0x68: conv= rgb32to24; break;
1485 case 0x83: conv= rgb15to32; break;
1486 case 0x84: conv= rgb16to32; break;
1487 case 0x86: conv= rgb24to32; break;
1489 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1490 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1491 switch(srcId | (dstId<<4)) {
1492 case 0x33: conv= rgb15tobgr15; break;
1493 case 0x34: conv= rgb16tobgr15; break;
1494 case 0x36: conv= rgb24tobgr15; break;
1495 case 0x38: conv= rgb32tobgr15; break;
1496 case 0x43: conv= rgb15tobgr16; break;
1497 case 0x44: conv= rgb16tobgr16; break;
1498 case 0x46: conv= rgb24tobgr16; break;
1499 case 0x48: conv= rgb32tobgr16; break;
1500 case 0x63: conv= rgb15tobgr24; break;
1501 case 0x64: conv= rgb16tobgr24; break;
1502 case 0x66: conv= rgb24tobgr24; break;
1503 case 0x68: conv= rgb32tobgr24; break;
1504 case 0x83: conv= rgb15tobgr32; break;
1505 case 0x84: conv= rgb16tobgr32; break;
1506 case 0x86: conv= rgb24tobgr32; break;
1511 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1512 sws_format_name(srcFormat), sws_format_name(dstFormat));
1514 const uint8_t *srcPtr= src[0];
1515 uint8_t *dstPtr= dst[0];
1516 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1517 srcPtr += ALT32_CORR;
1519 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1520 dstPtr += ALT32_CORR;
1522 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1523 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1526 dstPtr += dstStride[0]*srcSliceY;
1528 for (i=0; i<srcSliceH; i++) {
1529 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1530 srcPtr+= srcStride[0];
1531 dstPtr+= dstStride[0];
1538 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1539 int srcSliceH, uint8_t* dst[], int dstStride[])
1543 dst[0]+ srcSliceY *dstStride[0],
1544 dst[1]+(srcSliceY>>1)*dstStride[1],
1545 dst[2]+(srcSliceY>>1)*dstStride[2],
1547 dstStride[0], dstStride[1], srcStride[0]);
1549 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1553 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1554 int srcSliceH, uint8_t* dst[], int dstStride[])
1559 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1560 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1562 const uint8_t *srcPtr= src[0];
1563 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1565 for (i=0; i<srcSliceH; i++) {
1566 memcpy(dstPtr, srcPtr, c->srcW);
1567 srcPtr+= srcStride[0];
1568 dstPtr+= dstStride[0];
1572 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
1573 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1574 srcSliceH >> 2, srcStride[1], dstStride[1]);
1575 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1576 srcSliceH >> 2, srcStride[2], dstStride[2]);
1578 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1579 srcSliceH >> 2, srcStride[1], dstStride[2]);
1580 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1581 srcSliceH >> 2, srcStride[2], dstStride[1]);
1584 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1588 /* unscaled copy like stuff (assumes nearly identical formats) */
1589 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1590 int srcSliceH, uint8_t* dst[], int dstStride[])
1592 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1593 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1596 const uint8_t *srcPtr= src[0];
1597 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1600 /* universal length finder */
1601 while(length+c->srcW <= FFABS(dstStride[0])
1602 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1605 for (i=0; i<srcSliceH; i++) {
1606 memcpy(dstPtr, srcPtr, length);
1607 srcPtr+= srcStride[0];
1608 dstPtr+= dstStride[0];
1614 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1615 int srcSliceH, uint8_t* dst[], int dstStride[])
1618 for (plane=0; plane<4; plane++) {
1619 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1620 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1621 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1622 const uint8_t *srcPtr= src[plane];
1623 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1625 if (!dst[plane]) continue;
1626 // ignore palette for GRAY8
1627 if (plane == 1 && !dst[2]) continue;
1628 if (!src[plane] || (plane == 1 && !src[2])) {
1629 if(is16BPS(c->dstFormat))
1631 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1633 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1634 if (!isBE(c->srcFormat)) srcPtr++;
1635 for (i=0; i<height; i++) {
1636 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1637 srcPtr+= srcStride[plane];
1638 dstPtr+= dstStride[plane];
1640 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1641 for (i=0; i<height; i++) {
1642 for (j=0; j<length; j++) {
1643 dstPtr[ j<<1 ] = srcPtr[j];
1644 dstPtr[(j<<1)+1] = srcPtr[j];
1646 srcPtr+= srcStride[plane];
1647 dstPtr+= dstStride[plane];
1649 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1650 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1652 for (i=0; i<height; i++) {
1653 for (j=0; j<length; j++)
1654 ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
1655 srcPtr+= srcStride[plane];
1656 dstPtr+= dstStride[plane];
1658 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1659 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1661 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1663 for (i=0; i<height; i++) {
1664 memcpy(dstPtr, srcPtr, length);
1665 srcPtr+= srcStride[plane];
1666 dstPtr+= dstStride[plane];
1674 int ff_hardcodedcpuflags(void)
1677 #if COMPILE_TEMPLATE_MMX2
1678 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1679 #elif COMPILE_TEMPLATE_AMD3DNOW
1680 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1681 #elif COMPILE_TEMPLATE_MMX
1682 flags |= SWS_CPU_CAPS_MMX;
1683 #elif COMPILE_TEMPLATE_ALTIVEC
1684 flags |= SWS_CPU_CAPS_ALTIVEC;
1686 flags |= SWS_CPU_CAPS_BFIN;
1691 void ff_get_unscaled_swscale(SwsContext *c)
1693 const enum PixelFormat srcFormat = c->srcFormat;
1694 const enum PixelFormat dstFormat = c->dstFormat;
1695 const int flags = c->flags;
1696 const int dstH = c->dstH;
1699 needsDither= isAnyRGB(dstFormat)
1700 && c->dstFormatBpp < 24
1701 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1704 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1705 c->swScale= planarToNv12Wrapper;
1708 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1709 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1710 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1713 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1714 c->swScale= yvu9ToYv12Wrapper;
1718 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1719 c->swScale= bgr24ToYv12Wrapper;
1721 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1722 if ( isAnyRGB(srcFormat)
1723 && isAnyRGB(dstFormat)
1724 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1725 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1726 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1727 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1728 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1729 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1730 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1731 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1732 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1733 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1734 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1735 c->swScale= rgbToRgbWrapper;
1737 if ((usePal(srcFormat) && (
1738 dstFormat == PIX_FMT_RGB32 ||
1739 dstFormat == PIX_FMT_RGB32_1 ||
1740 dstFormat == PIX_FMT_RGB24 ||
1741 dstFormat == PIX_FMT_BGR32 ||
1742 dstFormat == PIX_FMT_BGR32_1 ||
1743 dstFormat == PIX_FMT_BGR24)))
1744 c->swScale= palToRgbWrapper;
1746 if (srcFormat == PIX_FMT_YUV422P) {
1747 if (dstFormat == PIX_FMT_YUYV422)
1748 c->swScale= yuv422pToYuy2Wrapper;
1749 else if (dstFormat == PIX_FMT_UYVY422)
1750 c->swScale= yuv422pToUyvyWrapper;
1753 /* LQ converters if -sws 0 or -sws 4*/
1754 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1756 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1757 if (dstFormat == PIX_FMT_YUYV422)
1758 c->swScale= planarToYuy2Wrapper;
1759 else if (dstFormat == PIX_FMT_UYVY422)
1760 c->swScale= planarToUyvyWrapper;
1763 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1764 c->swScale= yuyvToYuv420Wrapper;
1765 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1766 c->swScale= uyvyToYuv420Wrapper;
1767 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1768 c->swScale= yuyvToYuv422Wrapper;
1769 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1770 c->swScale= uyvyToYuv422Wrapper;
1772 #ifdef COMPILE_ALTIVEC
1773 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1774 !(c->flags & SWS_BITEXACT) &&
1775 srcFormat == PIX_FMT_YUV420P) {
1776 // unscaled YV12 -> packed YUV, we want speed
1777 if (dstFormat == PIX_FMT_YUYV422)
1778 c->swScale= yv12toyuy2_unscaled_altivec;
1779 else if (dstFormat == PIX_FMT_UYVY422)
1780 c->swScale= yv12touyvy_unscaled_altivec;
1785 if ( srcFormat == dstFormat
1786 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1787 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1788 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1789 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1790 || (isGray(dstFormat) && isGray(srcFormat))
1791 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1792 && c->chrDstHSubSample == c->chrSrcHSubSample
1793 && c->chrDstVSubSample == c->chrSrcVSubSample
1794 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1795 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1797 if (isPacked(c->srcFormat))
1798 c->swScale= packedCopyWrapper;
1799 else /* Planar YUV or gray */
1800 c->swScale= planarCopyWrapper;
1803 if (flags & SWS_CPU_CAPS_BFIN)
1804 ff_bfin_get_unscaled_swscale (c);
1808 static void reset_ptr(const uint8_t* src[], int format)
1810 if(!isALPHA(format))
1812 if(!isPlanarYUV(format)) {
1815 if (!usePal(format))
1821 * swscale wrapper, so we don't need to export the SwsContext.
1822 * Assumes planar YUV to be in YUV order instead of YVU.
1824 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1825 int srcSliceH, uint8_t* const dst[], const int dstStride[])
1828 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1829 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1831 // do not mess up sliceDir if we have a "trailing" 0-size slice
1835 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1836 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1839 if (c->sliceDir == 0) {
1840 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1843 if (usePal(c->srcFormat)) {
1844 for (i=0; i<256; i++) {
1845 int p, r, g, b,y,u,v;
1846 if(c->srcFormat == PIX_FMT_PAL8) {
1847 p=((const uint32_t*)(src[1]))[i];
1851 } else if(c->srcFormat == PIX_FMT_RGB8) {
1855 } else if(c->srcFormat == PIX_FMT_BGR8) {
1859 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1863 } else if(c->srcFormat == PIX_FMT_GRAY8) {
1866 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1871 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1872 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1873 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1874 c->pal_yuv[i]= y + (u<<8) + (v<<16);
1876 switch(c->dstFormat) {
1881 c->pal_rgb[i]= r + (g<<8) + (b<<16);
1883 case PIX_FMT_BGR32_1:
1887 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1889 case PIX_FMT_RGB32_1:
1893 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1900 c->pal_rgb[i]= b + (g<<8) + (r<<16);
1905 // copy strides, so they can safely be modified
1906 if (c->sliceDir == 1) {
1907 // slices go from top to bottom
1908 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1909 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1911 reset_ptr(src2, c->srcFormat);
1912 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1914 /* reset slice direction at end of frame */
1915 if (srcSliceY + srcSliceH == c->srcH)
1918 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
1920 // slices go from bottom to top => we flip the image internally
1921 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
1922 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
1924 src2[0] += (srcSliceH-1)*srcStride[0];
1925 if (!usePal(c->srcFormat))
1926 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
1927 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
1928 src2[3] += (srcSliceH-1)*srcStride[3];
1929 dst2[0] += ( c->dstH -1)*dstStride[0];
1930 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
1931 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
1932 dst2[3] += ( c->dstH -1)*dstStride[3];
1934 reset_ptr(src2, c->srcFormat);
1935 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1937 /* reset slice direction at end of frame */
1941 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
1945 #if LIBSWSCALE_VERSION_MAJOR < 1
1946 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
1947 int srcSliceH, uint8_t* dst[], int dstStride[])
1949 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);