2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
23 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
24 {BGR,RGB}{1,4,8,15,16} support dithering
26 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
27 YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
32 BGR24 -> BGR32 & RGB24 -> RGB32
33 BGR32 -> BGR24 & RGB32 -> RGB24
38 tested special converters (most are tested actually, but I did not write it down ...)
45 untested special converters
46 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
47 YV12/I420 -> YV12/I420
48 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
49 BGR24 -> BGR32 & RGB24 -> RGB32
50 BGR32 -> BGR24 & RGB32 -> RGB24
61 #include "swscale_internal.h"
63 #include "libavutil/intreadwrite.h"
64 #include "libavutil/x86_cpu.h"
65 #include "libavutil/avutil.h"
66 #include "libavutil/mathematics.h"
67 #include "libavutil/bswap.h"
68 #include "libavutil/pixdesc.h"
74 //#define HAVE_AMD3DNOW
79 #define isPacked(x) ( \
81 || (x)==PIX_FMT_YUYV422 \
82 || (x)==PIX_FMT_UYVY422 \
83 || (x)==PIX_FMT_Y400A \
87 #define RGB2YUV_SHIFT 15
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))
98 static const double rgb2yuv_table[8][9]={
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
103 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
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
106 {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
111 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
114 more intelligent misalignment avoidance for the horizontal scaler
115 write special vertical cubic upscale version
116 optimize C code (YV12 / minmax)
117 add support for packed pixel YUV input & output
118 add support for Y8 output
119 optimize BGR24 & BGR32
120 add BGR4 output support
121 write special BGR->BGR scaler
124 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
125 { 1, 3, 1, 3, 1, 3, 1, 3, },
126 { 2, 0, 2, 0, 2, 0, 2, 0, },
129 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
130 { 6, 2, 6, 2, 6, 2, 6, 2, },
131 { 0, 4, 0, 4, 0, 4, 0, 4, },
134 DECLARE_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, },
141 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
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, },
152 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
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, },
164 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
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, },
175 // tries to correct a gamma of 1.5
176 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
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, },
187 // tries to correct a gamma of 2.0
188 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
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, },
199 // tries to correct a gamma of 2.5
200 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
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, },
212 static 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)
217 //FIXME Optimize (just quickly written not optimized..)
220 for (i = 0; i < dstW; i++) {
224 for (j = 0; j < lumFilterSize; j++)
225 val += lumSrc[j][i] * lumFilter[j];
228 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
230 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
235 for (i = 0; i < chrDstW; i++) {
240 for (j = 0; j < chrFilterSize; j++) {
241 u += chrSrc[j][i ] * chrFilter[j];
242 v += chrSrc[j][i + VOFW] * chrFilter[j];
246 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
247 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
249 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
250 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
255 if (CONFIG_SWSCALE_ALPHA && aDest) {
256 for (i = 0; i < dstW; i++) {
260 for (j = 0; j < lumFilterSize; j++)
261 val += alpSrc[j][i] * lumFilter[j];
264 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
266 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
272 static 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)
277 if (isBE(dstFormat)) {
278 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
279 chrFilter, chrSrc, chrFilterSize,
281 dest, uDest, vDest, aDest,
284 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
285 chrFilter, chrSrc, chrFilterSize,
287 dest, uDest, vDest, aDest,
292 static 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)
296 //FIXME Optimize (just quickly written not optimized..)
298 for (i=0; i<dstW; i++) {
301 for (j=0; j<lumFilterSize; j++)
302 val += lumSrc[j][i] * lumFilter[j];
304 dest[i]= av_clip_uint8(val>>19);
308 for (i=0; i<chrDstW; i++) {
312 for (j=0; j<chrFilterSize; j++) {
313 u += chrSrc[j][i] * chrFilter[j];
314 v += chrSrc[j][i + VOFW] * chrFilter[j];
317 uDest[i]= av_clip_uint8(u>>19);
318 vDest[i]= av_clip_uint8(v>>19);
321 if (CONFIG_SWSCALE_ALPHA && aDest)
322 for (i=0; i<dstW; i++) {
325 for (j=0; j<lumFilterSize; j++)
326 val += alpSrc[j][i] * lumFilter[j];
328 aDest[i]= av_clip_uint8(val>>19);
333 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
334 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
335 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
337 //FIXME Optimize (just quickly written not optimized..)
339 for (i=0; i<dstW; i++) {
342 for (j=0; j<lumFilterSize; j++)
343 val += lumSrc[j][i] * lumFilter[j];
345 dest[i]= av_clip_uint8(val>>19);
351 if (dstFormat == PIX_FMT_NV12)
352 for (i=0; i<chrDstW; i++) {
356 for (j=0; j<chrFilterSize; j++) {
357 u += chrSrc[j][i] * chrFilter[j];
358 v += chrSrc[j][i + VOFW] * chrFilter[j];
361 uDest[2*i]= av_clip_uint8(u>>19);
362 uDest[2*i+1]= av_clip_uint8(v>>19);
365 for (i=0; i<chrDstW; i++) {
369 for (j=0; j<chrFilterSize; j++) {
370 u += chrSrc[j][i] * chrFilter[j];
371 v += chrSrc[j][i + VOFW] * chrFilter[j];
374 uDest[2*i]= av_clip_uint8(v>>19);
375 uDest[2*i+1]= av_clip_uint8(u>>19);
379 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
380 for (i=0; i<(dstW>>1); i++) {\
386 int av_unused A1, A2;\
387 type av_unused *r, *b, *g;\
390 for (j=0; j<lumFilterSize; j++) {\
391 Y1 += lumSrc[j][i2] * lumFilter[j];\
392 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
394 for (j=0; j<chrFilterSize; j++) {\
395 U += chrSrc[j][i] * chrFilter[j];\
396 V += chrSrc[j][i+VOFW] * chrFilter[j];\
405 for (j=0; j<lumFilterSize; j++) {\
406 A1 += alpSrc[j][i2 ] * lumFilter[j];\
407 A2 += alpSrc[j][i2+1] * lumFilter[j];\
413 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
414 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
415 if ((Y1|Y2|U|V)&256) {\
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; \
425 if (alpha && ((A1|A2)&256)) {\
426 A1=av_clip_uint8(A1);\
427 A2=av_clip_uint8(A2);\
430 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
431 for (i=0; i<dstW; i++) {\
439 for (j=0; j<lumFilterSize; j++) {\
440 Y += lumSrc[j][i ] * lumFilter[j];\
442 for (j=0; j<chrFilterSize; j++) {\
443 U += chrSrc[j][i ] * chrFilter[j];\
444 V += chrSrc[j][i+VOFW] * chrFilter[j];\
451 for (j=0; j<lumFilterSize; j++)\
452 A += alpSrc[j][i ] * lumFilter[j];\
455 A = av_clip_uint8(A);\
458 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
459 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
460 Y-= c->yuv2rgb_y_offset;\
461 Y*= c->yuv2rgb_y_coeff;\
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;\
466 if ((R|G|B)&(0xC0000000)) {\
467 if (R>=(256<<22)) R=(256<<22)-1; \
469 if (G>=(256<<22)) G=(256<<22)-1; \
471 if (B>=(256<<22)) B=(256<<22)-1; \
475 #define YSCALE_YUV_2_GRAY16_C \
476 for (i=0; i<(dstW>>1); i++) {\
485 for (j=0; j<lumFilterSize; j++) {\
486 Y1 += lumSrc[j][i2] * lumFilter[j];\
487 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
491 if ((Y1|Y2|U|V)&65536) {\
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; \
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*/\
500 r = (type *)c->table_rV[V]; \
501 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
502 b = (type *)c->table_bU[U];
504 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
505 for (i=0; i<(dstW>>1); 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; \
510 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
511 type av_unused *r, *b, *g; \
512 int av_unused A1, A2; \
514 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
515 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
518 #define YSCALE_YUV_2_GRAY16_2_C \
519 for (i=0; i<(dstW>>1); i++) { \
521 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
522 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
524 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
525 YSCALE_YUV_2_PACKED2_C(type,alpha)\
526 r = (type *)c->table_rV[V];\
527 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
528 b = (type *)c->table_bU[U];
530 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
531 for (i=0; i<(dstW>>1); i++) {\
533 int Y1= buf0[i2 ]>>7;\
534 int Y2= buf0[i2+1]>>7;\
535 int U= (uvbuf1[i ])>>7;\
536 int V= (uvbuf1[i+VOFW])>>7;\
537 type av_unused *r, *b, *g;\
538 int av_unused A1, A2;\
544 #define YSCALE_YUV_2_GRAY16_1_C \
545 for (i=0; i<(dstW>>1); i++) {\
547 int Y1= buf0[i2 ]<<1;\
548 int Y2= buf0[i2+1]<<1;
550 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
551 YSCALE_YUV_2_PACKED1_C(type,alpha)\
552 r = (type *)c->table_rV[V];\
553 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
554 b = (type *)c->table_bU[U];
556 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
557 for (i=0; i<(dstW>>1); i++) {\
559 int Y1= buf0[i2 ]>>7;\
560 int Y2= buf0[i2+1]>>7;\
561 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
562 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
563 type av_unused *r, *b, *g;\
564 int av_unused A1, A2;\
570 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
571 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
572 r = (type *)c->table_rV[V];\
573 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
574 b = (type *)c->table_bU[U];
576 #define YSCALE_YUV_2_MONO2_C \
577 const uint8_t * const d128=dither_8x8_220[y&7];\
578 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
579 for (i=0; i<dstW-7; i+=8) {\
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]];\
589 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
593 #define YSCALE_YUV_2_MONOX_C \
594 const uint8_t * const d128=dither_8x8_220[y&7];\
595 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
597 for (i=0; i<dstW-1; i+=2) {\
602 for (j=0; j<lumFilterSize; j++) {\
603 Y1 += lumSrc[j][i] * lumFilter[j];\
604 Y2 += lumSrc[j][i+1] * lumFilter[j];\
614 acc+= acc + g[Y1+d128[(i+0)&7]];\
615 acc+= acc + g[Y2+d128[(i+1)&7]];\
617 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
622 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
623 switch(c->dstFormat) {\
624 case PIX_FMT_RGB48BE:\
625 case PIX_FMT_RGB48LE:\
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];\
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);\
651 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
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);\
658 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
659 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
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);\
673 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
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;\
680 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
681 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
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];\
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];\
708 case PIX_FMT_RGB565BE:\
709 case PIX_FMT_RGB565LE:\
710 case PIX_FMT_BGR565BE:\
711 case PIX_FMT_BGR565LE:\
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];\
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];\
725 case PIX_FMT_RGB555BE:\
726 case PIX_FMT_RGB555LE:\
727 case PIX_FMT_BGR555BE:\
728 case PIX_FMT_BGR555LE:\
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];\
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];\
742 case PIX_FMT_RGB444BE:\
743 case PIX_FMT_RGB444LE:\
744 case PIX_FMT_BGR444BE:\
745 case PIX_FMT_BGR444LE:\
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];\
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];\
762 const uint8_t * const d64= dither_8x8_73[y&7];\
763 const uint8_t * const d32= dither_8x8_32[y&7];\
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]];\
773 const uint8_t * const d64= dither_8x8_73 [y&7];\
774 const uint8_t * const d128=dither_8x8_220[y&7];\
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);\
781 case PIX_FMT_RGB4_BYTE:\
782 case PIX_FMT_BGR4_BYTE:\
784 const uint8_t * const d64= dither_8x8_73 [y&7];\
785 const uint8_t * const d128=dither_8x8_220[y&7];\
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]];\
792 case PIX_FMT_MONOBLACK:\
793 case PIX_FMT_MONOWHITE:\
798 case PIX_FMT_YUYV422:\
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;\
806 case PIX_FMT_UYVY422:\
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;\
814 case PIX_FMT_GRAY16BE:\
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;\
822 case PIX_FMT_GRAY16LE:\
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;\
832 static 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)
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)
840 static 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)
845 int step= c->dstFormatBpp/8;
848 switch(c->dstFormat) {
856 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
857 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
858 dest[aidx]= needAlpha ? A : 255;
865 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
866 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
874 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
891 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
892 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
893 dest[aidx]= needAlpha ? A : 255;
900 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
901 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
909 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
924 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
927 uint8_t *ptr = plane + stride*y;
928 for (i=0; i<height; i++) {
929 memset(ptr, val, width);
934 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
938 for (i = 0; i < width; i++) {
943 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
947 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
948 const uint8_t *src1, const uint8_t *src2,
949 long width, uint32_t *unused)
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];
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;
963 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
964 const uint8_t *src1, const uint8_t *src2,
965 long width, uint32_t *unused)
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];
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);
979 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
980 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
983 for (i=0; i<width; i++) {\
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;\
988 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
992 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
993 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
994 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
995 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
996 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
997 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
998 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
999 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1001 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1004 for (i=0; i<width; i++) {
1009 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1010 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1013 for (i=0; i<width; i++) {\
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;\
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);\
1022 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1025 for (i=0; i<width; i++) {\
1026 int pix0= ((const type*)src)[2*i+0]>>shp;\
1027 int pix1= ((const type*)src)[2*i+1]>>shp;\
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);\
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);\
1040 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1041 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1042 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1043 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1044 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1045 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1046 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1047 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1049 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1052 for (i=0; i<width; i++) {
1055 dst[i]= pal[d] & 0xFF;
1059 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1060 const uint8_t *src1, const uint8_t *src2,
1061 long width, uint32_t *pal)
1064 assert(src1 == src2);
1065 for (i=0; i<width; i++) {
1066 int p= pal[src1[i]];
1073 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1076 for (i=0; i<width/8; i++) {
1079 dst[8*i+j]= ((d>>(7-j))&1)*255;
1083 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1086 for (i=0; i<width/8; i++) {
1089 dst[8*i+j]= ((d>>(7-j))&1)*255;
1093 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1095 #if CONFIG_RUNTIME_CPUDETECT
1096 # define COMPILE_C 1
1098 # define COMPILE_MMX HAVE_MMX
1099 # define COMPILE_MMX2 HAVE_MMX2
1100 # define COMPILE_3DNOW HAVE_AMD3DNOW
1102 # define COMPILE_ALTIVEC HAVE_ALTIVEC
1104 #else /* CONFIG_RUNTIME_CPUDETECT */
1107 # define COMPILE_MMX2 1
1108 # elif HAVE_AMD3DNOW
1109 # define COMPILE_3DNOW 1
1111 # define COMPILE_MMX 1
1113 # define COMPILE_C 1
1115 # elif ARCH_PPC && HAVE_ALTIVEC
1116 # define COMPILE_ALTIVEC 1
1118 # define COMPILE_C 1
1123 # define COMPILE_C 0
1126 # define COMPILE_MMX 0
1128 #ifndef COMPILE_MMX2
1129 # define COMPILE_MMX2 0
1131 #ifndef COMPILE_3DNOW
1132 # define COMPILE_3DNOW 0
1134 #ifndef COMPILE_ALTIVEC
1135 # define COMPILE_ALTIVEC 0
1138 #define COMPILE_TEMPLATE_MMX 0
1139 #define COMPILE_TEMPLATE_MMX2 0
1140 #define COMPILE_TEMPLATE_AMD3DNOW 0
1141 #define COMPILE_TEMPLATE_ALTIVEC 0
1143 #include "swscale_template.c"
1147 #undef COMPILE_TEMPLATE_ALTIVEC
1148 #define COMPILE_TEMPLATE_ALTIVEC 1
1149 #define RENAME(a) a ## _altivec
1150 #include "ppc/swscale_template.c"
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
1164 #define RENAME(a) a ## _MMX
1165 #include "x86/swscale_template.c"
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
1177 #define RENAME(a) a ## _MMX2
1178 #include "x86/swscale_template.c"
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
1190 #define RENAME(a) a ## _3DNow
1191 #include "x86/swscale_template.c"
1196 SwsFunc ff_getSwsFunc(SwsContext *c)
1198 sws_init_swScale_c(c);
1200 #if CONFIG_RUNTIME_CPUDETECT
1202 // ordered per speed fastest first
1203 if (c->flags & SWS_CPU_CAPS_MMX2) {
1204 sws_init_swScale_MMX2(c);
1205 return swScale_MMX2;
1206 } else if (c->flags & SWS_CPU_CAPS_3DNOW) {
1207 sws_init_swScale_3DNow(c);
1208 return swScale_3DNow;
1209 } else if (c->flags & SWS_CPU_CAPS_MMX) {
1210 sws_init_swScale_MMX(c);
1216 if (c->flags & SWS_CPU_CAPS_ALTIVEC) {
1217 sws_init_swScale_altivec(c);
1218 return swScale_altivec;
1221 #endif /* ARCH_X86 */
1222 #else //CONFIG_RUNTIME_CPUDETECT
1223 #if COMPILE_TEMPLATE_MMX2
1224 sws_init_swScale_MMX2(c);
1225 return swScale_MMX2;
1226 #elif COMPILE_TEMPLATE_AMD3DNOW
1227 sws_init_swScale_3DNow(c);
1228 return swScale_3DNow;
1229 #elif COMPILE_TEMPLATE_MMX
1230 sws_init_swScale_MMX(c);
1232 #elif COMPILE_TEMPLATE_ALTIVEC
1233 sws_init_swScale_altivec(c);
1234 return swScale_altivec;
1236 #endif //!CONFIG_RUNTIME_CPUDETECT
1241 static void copyPlane(const uint8_t *src, int srcStride,
1242 int srcSliceY, int srcSliceH, int width,
1243 uint8_t *dst, int dstStride)
1245 dst += dstStride * srcSliceY;
1246 if (dstStride == srcStride && srcStride > 0) {
1247 memcpy(dst, src, srcSliceH * dstStride);
1250 for (i=0; i<srcSliceH; i++) {
1251 memcpy(dst, src, width);
1258 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1259 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1261 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1263 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1264 dstParam[0], dstStride[0]);
1266 if (c->dstFormat == PIX_FMT_NV12)
1267 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1269 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1274 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1275 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1277 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1279 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1284 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1285 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1287 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1289 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1294 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1295 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1297 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1299 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1304 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1305 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1307 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1309 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1314 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1315 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1317 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1318 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1319 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1321 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1324 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1329 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1330 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1332 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1333 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1334 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1336 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1341 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1342 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1344 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1345 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1346 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1348 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1351 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1356 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1357 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1359 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1360 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1361 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1363 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1368 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1371 for (i=0; i<num_pixels; i++)
1372 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1375 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1379 for (i=0; i<num_pixels; i++)
1380 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1383 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1387 for (i=0; i<num_pixels; i++) {
1389 dst[0]= palette[src[i<<1]*4+0];
1390 dst[1]= palette[src[i<<1]*4+1];
1391 dst[2]= palette[src[i<<1]*4+2];
1396 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1397 int srcSliceH, uint8_t* dst[], int dstStride[])
1399 const enum PixelFormat srcFormat= c->srcFormat;
1400 const enum PixelFormat dstFormat= c->dstFormat;
1401 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1402 const uint8_t *palette)=NULL;
1404 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1405 const uint8_t *srcPtr= src[0];
1407 if (srcFormat == PIX_FMT_Y400A) {
1408 switch (dstFormat) {
1409 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
1410 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
1411 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1412 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1413 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
1414 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
1416 } else if (usePal(srcFormat)) {
1417 switch (dstFormat) {
1418 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
1419 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
1420 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1421 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1422 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
1423 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
1428 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1429 sws_format_name(srcFormat), sws_format_name(dstFormat));
1431 for (i=0; i<srcSliceH; i++) {
1432 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1433 srcPtr+= srcStride[0];
1434 dstPtr+= dstStride[0];
1441 #define isRGBA32(x) ( \
1442 (x) == PIX_FMT_ARGB \
1443 || (x) == PIX_FMT_RGBA \
1444 || (x) == PIX_FMT_BGRA \
1445 || (x) == PIX_FMT_ABGR \
1448 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1449 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1450 int srcSliceH, uint8_t* dst[], int dstStride[])
1452 const enum PixelFormat srcFormat= c->srcFormat;
1453 const enum PixelFormat dstFormat= c->dstFormat;
1454 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1455 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1456 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1457 const int dstId= c->dstFormatBpp >> 2;
1458 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1460 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1462 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1463 if ( CONV_IS(ABGR, RGBA)
1464 || CONV_IS(ARGB, BGRA)
1465 || CONV_IS(BGRA, ARGB)
1466 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1467 else if (CONV_IS(ABGR, ARGB)
1468 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1469 else if (CONV_IS(ABGR, BGRA)
1470 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1471 else if (CONV_IS(BGRA, RGBA)
1472 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1473 else if (CONV_IS(BGRA, ABGR)
1474 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1477 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1478 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1479 switch(srcId | (dstId<<4)) {
1480 case 0x34: conv= rgb16to15; break;
1481 case 0x36: conv= rgb24to15; break;
1482 case 0x38: conv= rgb32to15; break;
1483 case 0x43: conv= rgb15to16; break;
1484 case 0x46: conv= rgb24to16; break;
1485 case 0x48: conv= rgb32to16; break;
1486 case 0x63: conv= rgb15to24; break;
1487 case 0x64: conv= rgb16to24; break;
1488 case 0x68: conv= rgb32to24; break;
1489 case 0x83: conv= rgb15to32; break;
1490 case 0x84: conv= rgb16to32; break;
1491 case 0x86: conv= rgb24to32; break;
1493 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1494 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1495 switch(srcId | (dstId<<4)) {
1496 case 0x33: conv= rgb15tobgr15; break;
1497 case 0x34: conv= rgb16tobgr15; break;
1498 case 0x36: conv= rgb24tobgr15; break;
1499 case 0x38: conv= rgb32tobgr15; break;
1500 case 0x43: conv= rgb15tobgr16; break;
1501 case 0x44: conv= rgb16tobgr16; break;
1502 case 0x46: conv= rgb24tobgr16; break;
1503 case 0x48: conv= rgb32tobgr16; break;
1504 case 0x63: conv= rgb15tobgr24; break;
1505 case 0x64: conv= rgb16tobgr24; break;
1506 case 0x66: conv= rgb24tobgr24; break;
1507 case 0x68: conv= rgb32tobgr24; break;
1508 case 0x83: conv= rgb15tobgr32; break;
1509 case 0x84: conv= rgb16tobgr32; break;
1510 case 0x86: conv= rgb24tobgr32; break;
1515 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1516 sws_format_name(srcFormat), sws_format_name(dstFormat));
1518 const uint8_t *srcPtr= src[0];
1519 uint8_t *dstPtr= dst[0];
1520 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1521 srcPtr += ALT32_CORR;
1523 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1524 dstPtr += ALT32_CORR;
1526 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1527 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1530 dstPtr += dstStride[0]*srcSliceY;
1532 for (i=0; i<srcSliceH; i++) {
1533 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1534 srcPtr+= srcStride[0];
1535 dstPtr+= dstStride[0];
1542 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1543 int srcSliceH, uint8_t* dst[], int dstStride[])
1547 dst[0]+ srcSliceY *dstStride[0],
1548 dst[1]+(srcSliceY>>1)*dstStride[1],
1549 dst[2]+(srcSliceY>>1)*dstStride[2],
1551 dstStride[0], dstStride[1], srcStride[0]);
1553 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1557 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1558 int srcSliceH, uint8_t* dst[], int dstStride[])
1560 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1561 dst[0], dstStride[0]);
1563 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1564 srcSliceH >> 2, srcStride[1], dstStride[1]);
1565 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1566 srcSliceH >> 2, srcStride[2], dstStride[2]);
1568 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1572 /* unscaled copy like stuff (assumes nearly identical formats) */
1573 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1574 int srcSliceH, uint8_t* dst[], int dstStride[])
1576 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1577 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1580 const uint8_t *srcPtr= src[0];
1581 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1584 /* universal length finder */
1585 while(length+c->srcW <= FFABS(dstStride[0])
1586 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1589 for (i=0; i<srcSliceH; i++) {
1590 memcpy(dstPtr, srcPtr, length);
1591 srcPtr+= srcStride[0];
1592 dstPtr+= dstStride[0];
1598 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1599 int srcSliceH, uint8_t* dst[], int dstStride[])
1602 for (plane=0; plane<4; plane++) {
1603 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1604 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1605 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1606 const uint8_t *srcPtr= src[plane];
1607 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1609 if (!dst[plane]) continue;
1610 // ignore palette for GRAY8
1611 if (plane == 1 && !dst[2]) continue;
1612 if (!src[plane] || (plane == 1 && !src[2])) {
1613 if(is16BPS(c->dstFormat))
1615 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1617 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1618 if (!isBE(c->srcFormat)) srcPtr++;
1619 for (i=0; i<height; i++) {
1620 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1621 srcPtr+= srcStride[plane];
1622 dstPtr+= dstStride[plane];
1624 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1625 for (i=0; i<height; i++) {
1626 for (j=0; j<length; j++) {
1627 dstPtr[ j<<1 ] = srcPtr[j];
1628 dstPtr[(j<<1)+1] = srcPtr[j];
1630 srcPtr+= srcStride[plane];
1631 dstPtr+= dstStride[plane];
1633 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1634 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1636 for (i=0; i<height; i++) {
1637 for (j=0; j<length; j++)
1638 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1639 srcPtr+= srcStride[plane];
1640 dstPtr+= dstStride[plane];
1642 } else if (dstStride[plane] == srcStride[plane] &&
1643 srcStride[plane] > 0 && srcStride[plane] == length) {
1644 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1645 height*dstStride[plane]);
1647 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1649 for (i=0; i<height; i++) {
1650 memcpy(dstPtr, srcPtr, length);
1651 srcPtr+= srcStride[plane];
1652 dstPtr+= dstStride[plane];
1660 int ff_hardcodedcpuflags(void)
1663 #if COMPILE_TEMPLATE_MMX2
1664 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1665 #elif COMPILE_TEMPLATE_AMD3DNOW
1666 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1667 #elif COMPILE_TEMPLATE_MMX
1668 flags |= SWS_CPU_CAPS_MMX;
1669 #elif COMPILE_TEMPLATE_ALTIVEC
1670 flags |= SWS_CPU_CAPS_ALTIVEC;
1672 flags |= SWS_CPU_CAPS_BFIN;
1677 void ff_get_unscaled_swscale(SwsContext *c)
1679 const enum PixelFormat srcFormat = c->srcFormat;
1680 const enum PixelFormat dstFormat = c->dstFormat;
1681 const int flags = c->flags;
1682 const int dstH = c->dstH;
1685 needsDither= isAnyRGB(dstFormat)
1686 && c->dstFormatBpp < 24
1687 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1690 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1691 c->swScale= planarToNv12Wrapper;
1694 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1695 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1696 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1699 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1700 c->swScale= yvu9ToYv12Wrapper;
1704 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1705 c->swScale= bgr24ToYv12Wrapper;
1707 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1708 if ( isAnyRGB(srcFormat)
1709 && isAnyRGB(dstFormat)
1710 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1711 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1712 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1713 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1714 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1715 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1716 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1717 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1718 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1719 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1720 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1721 c->swScale= rgbToRgbWrapper;
1723 if ((usePal(srcFormat) && (
1724 dstFormat == PIX_FMT_RGB32 ||
1725 dstFormat == PIX_FMT_RGB32_1 ||
1726 dstFormat == PIX_FMT_RGB24 ||
1727 dstFormat == PIX_FMT_BGR32 ||
1728 dstFormat == PIX_FMT_BGR32_1 ||
1729 dstFormat == PIX_FMT_BGR24)))
1730 c->swScale= palToRgbWrapper;
1732 if (srcFormat == PIX_FMT_YUV422P) {
1733 if (dstFormat == PIX_FMT_YUYV422)
1734 c->swScale= yuv422pToYuy2Wrapper;
1735 else if (dstFormat == PIX_FMT_UYVY422)
1736 c->swScale= yuv422pToUyvyWrapper;
1739 /* LQ converters if -sws 0 or -sws 4*/
1740 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1742 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1743 if (dstFormat == PIX_FMT_YUYV422)
1744 c->swScale= planarToYuy2Wrapper;
1745 else if (dstFormat == PIX_FMT_UYVY422)
1746 c->swScale= planarToUyvyWrapper;
1749 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1750 c->swScale= yuyvToYuv420Wrapper;
1751 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1752 c->swScale= uyvyToYuv420Wrapper;
1753 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1754 c->swScale= yuyvToYuv422Wrapper;
1755 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1756 c->swScale= uyvyToYuv422Wrapper;
1759 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1760 !(c->flags & SWS_BITEXACT) &&
1761 srcFormat == PIX_FMT_YUV420P) {
1762 // unscaled YV12 -> packed YUV, we want speed
1763 if (dstFormat == PIX_FMT_YUYV422)
1764 c->swScale= yv12toyuy2_unscaled_altivec;
1765 else if (dstFormat == PIX_FMT_UYVY422)
1766 c->swScale= yv12touyvy_unscaled_altivec;
1771 if ( srcFormat == dstFormat
1772 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1773 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1774 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1775 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1776 || (isGray(dstFormat) && isGray(srcFormat))
1777 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1778 && c->chrDstHSubSample == c->chrSrcHSubSample
1779 && c->chrDstVSubSample == c->chrSrcVSubSample
1780 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1781 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1783 if (isPacked(c->srcFormat))
1784 c->swScale= packedCopyWrapper;
1785 else /* Planar YUV or gray */
1786 c->swScale= planarCopyWrapper;
1789 if (flags & SWS_CPU_CAPS_BFIN)
1790 ff_bfin_get_unscaled_swscale (c);
1794 static void reset_ptr(const uint8_t* src[], int format)
1796 if(!isALPHA(format))
1798 if(!isPlanarYUV(format)) {
1801 if (!usePal(format))
1806 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1807 const int linesizes[4])
1809 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1812 for (i = 0; i < 4; i++) {
1813 int plane = desc->comp[i].plane;
1814 if (!data[plane] || !linesizes[plane])
1822 * swscale wrapper, so we don't need to export the SwsContext.
1823 * Assumes planar YUV to be in YUV order instead of YVU.
1825 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1826 int srcSliceH, uint8_t* const dst[], const int dstStride[])
1829 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1830 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1832 // do not mess up sliceDir if we have a "trailing" 0-size slice
1836 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1837 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1840 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1841 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1845 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1846 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1849 if (c->sliceDir == 0) {
1850 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1853 if (usePal(c->srcFormat)) {
1854 for (i=0; i<256; i++) {
1855 int p, r, g, b,y,u,v;
1856 if(c->srcFormat == PIX_FMT_PAL8) {
1857 p=((const uint32_t*)(src[1]))[i];
1861 } else if(c->srcFormat == PIX_FMT_RGB8) {
1865 } else if(c->srcFormat == PIX_FMT_BGR8) {
1869 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1873 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1876 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1881 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1882 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1883 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1884 c->pal_yuv[i]= y + (u<<8) + (v<<16);
1886 switch(c->dstFormat) {
1891 c->pal_rgb[i]= r + (g<<8) + (b<<16);
1893 case PIX_FMT_BGR32_1:
1897 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1899 case PIX_FMT_RGB32_1:
1903 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1910 c->pal_rgb[i]= b + (g<<8) + (r<<16);
1915 // copy strides, so they can safely be modified
1916 if (c->sliceDir == 1) {
1917 // slices go from top to bottom
1918 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1919 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1921 reset_ptr(src2, c->srcFormat);
1922 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1924 /* reset slice direction at end of frame */
1925 if (srcSliceY + srcSliceH == c->srcH)
1928 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
1930 // slices go from bottom to top => we flip the image internally
1931 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
1932 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
1934 src2[0] += (srcSliceH-1)*srcStride[0];
1935 if (!usePal(c->srcFormat))
1936 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
1937 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
1938 src2[3] += (srcSliceH-1)*srcStride[3];
1939 dst2[0] += ( c->dstH -1)*dstStride[0];
1940 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
1941 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
1942 dst2[3] += ( c->dstH -1)*dstStride[3];
1944 reset_ptr(src2, c->srcFormat);
1945 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1947 /* reset slice direction at end of frame */
1951 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
1955 /* Convert the palette to the same packed 32-bit format as the palette */
1956 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1960 for (i=0; i<num_pixels; i++)
1961 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1964 /* Palette format: ABCD -> dst format: ABC */
1965 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1969 for (i=0; i<num_pixels; i++) {
1971 dst[0]= palette[src[i]*4+0];
1972 dst[1]= palette[src[i]*4+1];
1973 dst[2]= palette[src[i]*4+2];