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, int output_bits)
217 //FIXME Optimize (just quickly written not optimized..)
219 int shift = 11 + 16 - output_bits;
221 for (i = 0; i < dstW; i++) {
225 for (j = 0; j < lumFilterSize; j++)
226 val += lumSrc[j][i] * lumFilter[j];
229 AV_WB16(&dest[i], av_clip_uint16(val >> shift));
231 AV_WL16(&dest[i], av_clip_uint16(val >> shift));
236 for (i = 0; i < chrDstW; i++) {
241 for (j = 0; j < chrFilterSize; j++) {
242 u += chrSrc[j][i ] * chrFilter[j];
243 v += chrSrc[j][i + VOFW] * chrFilter[j];
247 AV_WB16(&uDest[i], av_clip_uint16(u >> shift));
248 AV_WB16(&vDest[i], av_clip_uint16(v >> shift));
250 AV_WL16(&uDest[i], av_clip_uint16(u >> shift));
251 AV_WL16(&vDest[i], av_clip_uint16(v >> shift));
256 if (CONFIG_SWSCALE_ALPHA && aDest) {
257 for (i = 0; i < dstW; i++) {
261 for (j = 0; j < lumFilterSize; j++)
262 val += alpSrc[j][i] * lumFilter[j];
265 AV_WB16(&aDest[i], av_clip_uint16(val >> shift));
267 AV_WL16(&aDest[i], av_clip_uint16(val >> shift));
273 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
274 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
275 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
276 enum PixelFormat dstFormat)
278 #define conv16(bits) \
279 if (isBE(dstFormat)) { \
280 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
281 chrFilter, chrSrc, chrFilterSize, \
283 dest, uDest, vDest, aDest, \
284 dstW, chrDstW, 1, bits); \
286 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
287 chrFilter, chrSrc, chrFilterSize, \
289 dest, uDest, vDest, aDest, \
290 dstW, chrDstW, 0, bits); \
292 if (is16BPS(dstFormat)) {
294 } else if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
302 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
303 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
304 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
306 //FIXME Optimize (just quickly written not optimized..)
308 for (i=0; i<dstW; i++) {
311 for (j=0; j<lumFilterSize; j++)
312 val += lumSrc[j][i] * lumFilter[j];
314 dest[i]= av_clip_uint8(val>>19);
318 for (i=0; i<chrDstW; i++) {
322 for (j=0; j<chrFilterSize; j++) {
323 u += chrSrc[j][i] * chrFilter[j];
324 v += chrSrc[j][i + VOFW] * chrFilter[j];
327 uDest[i]= av_clip_uint8(u>>19);
328 vDest[i]= av_clip_uint8(v>>19);
331 if (CONFIG_SWSCALE_ALPHA && aDest)
332 for (i=0; i<dstW; i++) {
335 for (j=0; j<lumFilterSize; j++)
336 val += alpSrc[j][i] * lumFilter[j];
338 aDest[i]= av_clip_uint8(val>>19);
343 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
344 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
345 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
347 //FIXME Optimize (just quickly written not optimized..)
349 for (i=0; i<dstW; i++) {
352 for (j=0; j<lumFilterSize; j++)
353 val += lumSrc[j][i] * lumFilter[j];
355 dest[i]= av_clip_uint8(val>>19);
361 if (dstFormat == PIX_FMT_NV12)
362 for (i=0; i<chrDstW; i++) {
366 for (j=0; j<chrFilterSize; j++) {
367 u += chrSrc[j][i] * chrFilter[j];
368 v += chrSrc[j][i + VOFW] * chrFilter[j];
371 uDest[2*i]= av_clip_uint8(u>>19);
372 uDest[2*i+1]= av_clip_uint8(v>>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[2*i]= av_clip_uint8(v>>19);
385 uDest[2*i+1]= av_clip_uint8(u>>19);
389 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
390 for (i=0; i<(dstW>>1); i++) {\
396 int av_unused A1, A2;\
397 type av_unused *r, *b, *g;\
400 for (j=0; j<lumFilterSize; j++) {\
401 Y1 += lumSrc[j][i2] * lumFilter[j];\
402 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
404 for (j=0; j<chrFilterSize; j++) {\
405 U += chrSrc[j][i] * chrFilter[j];\
406 V += chrSrc[j][i+VOFW] * chrFilter[j];\
415 for (j=0; j<lumFilterSize; j++) {\
416 A1 += alpSrc[j][i2 ] * lumFilter[j];\
417 A2 += alpSrc[j][i2+1] * lumFilter[j];\
423 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
424 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
425 if ((Y1|Y2|U|V)&256) {\
426 if (Y1>255) Y1=255; \
427 else if (Y1<0)Y1=0; \
428 if (Y2>255) Y2=255; \
429 else if (Y2<0)Y2=0; \
435 if (alpha && ((A1|A2)&256)) {\
436 A1=av_clip_uint8(A1);\
437 A2=av_clip_uint8(A2);\
440 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
441 for (i=0; i<dstW; i++) {\
449 for (j=0; j<lumFilterSize; j++) {\
450 Y += lumSrc[j][i ] * lumFilter[j];\
452 for (j=0; j<chrFilterSize; j++) {\
453 U += chrSrc[j][i ] * chrFilter[j];\
454 V += chrSrc[j][i+VOFW] * chrFilter[j];\
461 for (j=0; j<lumFilterSize; j++)\
462 A += alpSrc[j][i ] * lumFilter[j];\
465 A = av_clip_uint8(A);\
468 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
469 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
470 Y-= c->yuv2rgb_y_offset;\
471 Y*= c->yuv2rgb_y_coeff;\
473 R= Y + V*c->yuv2rgb_v2r_coeff;\
474 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
475 B= Y + U*c->yuv2rgb_u2b_coeff;\
476 if ((R|G|B)&(0xC0000000)) {\
477 if (R>=(256<<22)) R=(256<<22)-1; \
479 if (G>=(256<<22)) G=(256<<22)-1; \
481 if (B>=(256<<22)) B=(256<<22)-1; \
485 #define YSCALE_YUV_2_GRAY16_C \
486 for (i=0; i<(dstW>>1); i++) {\
495 for (j=0; j<lumFilterSize; j++) {\
496 Y1 += lumSrc[j][i2] * lumFilter[j];\
497 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
501 if ((Y1|Y2|U|V)&65536) {\
502 if (Y1>65535) Y1=65535; \
503 else if (Y1<0)Y1=0; \
504 if (Y2>65535) Y2=65535; \
505 else if (Y2<0)Y2=0; \
508 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
509 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
510 r = (type *)c->table_rV[V]; \
511 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
512 b = (type *)c->table_bU[U];
514 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
515 for (i=0; i<(dstW>>1); i++) { \
517 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
518 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
519 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
520 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
521 type av_unused *r, *b, *g; \
522 int av_unused A1, A2; \
524 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
525 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
528 #define YSCALE_YUV_2_GRAY16_2_C \
529 for (i=0; i<(dstW>>1); i++) { \
531 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
532 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
534 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
535 YSCALE_YUV_2_PACKED2_C(type,alpha)\
536 r = (type *)c->table_rV[V];\
537 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
538 b = (type *)c->table_bU[U];
540 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
541 for (i=0; i<(dstW>>1); i++) {\
543 int Y1= buf0[i2 ]>>7;\
544 int Y2= buf0[i2+1]>>7;\
545 int U= (uvbuf1[i ])>>7;\
546 int V= (uvbuf1[i+VOFW])>>7;\
547 type av_unused *r, *b, *g;\
548 int av_unused A1, A2;\
554 #define YSCALE_YUV_2_GRAY16_1_C \
555 for (i=0; i<(dstW>>1); i++) {\
557 int Y1= buf0[i2 ]<<1;\
558 int Y2= buf0[i2+1]<<1;
560 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
561 YSCALE_YUV_2_PACKED1_C(type,alpha)\
562 r = (type *)c->table_rV[V];\
563 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
564 b = (type *)c->table_bU[U];
566 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
567 for (i=0; i<(dstW>>1); i++) {\
569 int Y1= buf0[i2 ]>>7;\
570 int Y2= buf0[i2+1]>>7;\
571 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
572 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
573 type av_unused *r, *b, *g;\
574 int av_unused A1, A2;\
580 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
581 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
582 r = (type *)c->table_rV[V];\
583 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
584 b = (type *)c->table_bU[U];
586 #define YSCALE_YUV_2_MONO2_C \
587 const uint8_t * const d128=dither_8x8_220[y&7];\
588 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
589 for (i=0; i<dstW-7; i+=8) {\
591 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
592 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
593 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
594 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
595 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
596 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
597 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
598 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
599 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
603 #define YSCALE_YUV_2_MONOX_C \
604 const uint8_t * const d128=dither_8x8_220[y&7];\
605 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
607 for (i=0; i<dstW-1; i+=2) {\
612 for (j=0; j<lumFilterSize; j++) {\
613 Y1 += lumSrc[j][i] * lumFilter[j];\
614 Y2 += lumSrc[j][i+1] * lumFilter[j];\
624 acc+= acc + g[Y1+d128[(i+0)&7]];\
625 acc+= acc + g[Y2+d128[(i+1)&7]];\
627 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
632 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
633 switch(c->dstFormat) {\
634 case PIX_FMT_RGB48BE:\
635 case PIX_FMT_RGB48LE:\
637 ((uint8_t*)dest)[ 0]= r[Y1];\
638 ((uint8_t*)dest)[ 1]= r[Y1];\
639 ((uint8_t*)dest)[ 2]= g[Y1];\
640 ((uint8_t*)dest)[ 3]= g[Y1];\
641 ((uint8_t*)dest)[ 4]= b[Y1];\
642 ((uint8_t*)dest)[ 5]= b[Y1];\
643 ((uint8_t*)dest)[ 6]= r[Y2];\
644 ((uint8_t*)dest)[ 7]= r[Y2];\
645 ((uint8_t*)dest)[ 8]= g[Y2];\
646 ((uint8_t*)dest)[ 9]= g[Y2];\
647 ((uint8_t*)dest)[10]= b[Y2];\
648 ((uint8_t*)dest)[11]= b[Y2];\
652 case PIX_FMT_BGR48BE:\
653 case PIX_FMT_BGR48LE:\
655 ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
656 ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
657 ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
658 ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
659 ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
660 ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[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<<24) : 0);\
670 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
673 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
675 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
676 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
680 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
681 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
689 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
690 func(uint32_t,needAlpha)\
691 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
692 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
695 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
697 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
698 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
702 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
703 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
710 ((uint8_t*)dest)[0]= r[Y1];\
711 ((uint8_t*)dest)[1]= g[Y1];\
712 ((uint8_t*)dest)[2]= b[Y1];\
713 ((uint8_t*)dest)[3]= r[Y2];\
714 ((uint8_t*)dest)[4]= g[Y2];\
715 ((uint8_t*)dest)[5]= b[Y2];\
721 ((uint8_t*)dest)[0]= b[Y1];\
722 ((uint8_t*)dest)[1]= g[Y1];\
723 ((uint8_t*)dest)[2]= r[Y1];\
724 ((uint8_t*)dest)[3]= b[Y2];\
725 ((uint8_t*)dest)[4]= g[Y2];\
726 ((uint8_t*)dest)[5]= r[Y2];\
730 case PIX_FMT_RGB565BE:\
731 case PIX_FMT_RGB565LE:\
732 case PIX_FMT_BGR565BE:\
733 case PIX_FMT_BGR565LE:\
735 const int dr1= dither_2x2_8[y&1 ][0];\
736 const int dg1= dither_2x2_4[y&1 ][0];\
737 const int db1= dither_2x2_8[(y&1)^1][0];\
738 const int dr2= dither_2x2_8[y&1 ][1];\
739 const int dg2= dither_2x2_4[y&1 ][1];\
740 const int db2= dither_2x2_8[(y&1)^1][1];\
742 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
743 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
747 case PIX_FMT_RGB555BE:\
748 case PIX_FMT_RGB555LE:\
749 case PIX_FMT_BGR555BE:\
750 case PIX_FMT_BGR555LE:\
752 const int dr1= dither_2x2_8[y&1 ][0];\
753 const int dg1= dither_2x2_8[y&1 ][1];\
754 const int db1= dither_2x2_8[(y&1)^1][0];\
755 const int dr2= dither_2x2_8[y&1 ][1];\
756 const int dg2= dither_2x2_8[y&1 ][0];\
757 const int db2= dither_2x2_8[(y&1)^1][1];\
759 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
760 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
764 case PIX_FMT_RGB444BE:\
765 case PIX_FMT_RGB444LE:\
766 case PIX_FMT_BGR444BE:\
767 case PIX_FMT_BGR444LE:\
769 const int dr1= dither_4x4_16[y&3 ][0];\
770 const int dg1= dither_4x4_16[y&3 ][1];\
771 const int db1= dither_4x4_16[(y&3)^3][0];\
772 const int dr2= dither_4x4_16[y&3 ][1];\
773 const int dg2= dither_4x4_16[y&3 ][0];\
774 const int db2= dither_4x4_16[(y&3)^3][1];\
776 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
777 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
784 const uint8_t * const d64= dither_8x8_73[y&7];\
785 const uint8_t * const d32= dither_8x8_32[y&7];\
787 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
788 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
795 const uint8_t * const d64= dither_8x8_73 [y&7];\
796 const uint8_t * const d128=dither_8x8_220[y&7];\
798 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
799 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
803 case PIX_FMT_RGB4_BYTE:\
804 case PIX_FMT_BGR4_BYTE:\
806 const uint8_t * const d64= dither_8x8_73 [y&7];\
807 const uint8_t * const d128=dither_8x8_220[y&7];\
809 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
810 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
814 case PIX_FMT_MONOBLACK:\
815 case PIX_FMT_MONOWHITE:\
820 case PIX_FMT_YUYV422:\
822 ((uint8_t*)dest)[2*i2+0]= Y1;\
823 ((uint8_t*)dest)[2*i2+1]= U;\
824 ((uint8_t*)dest)[2*i2+2]= Y2;\
825 ((uint8_t*)dest)[2*i2+3]= V;\
828 case PIX_FMT_UYVY422:\
830 ((uint8_t*)dest)[2*i2+0]= U;\
831 ((uint8_t*)dest)[2*i2+1]= Y1;\
832 ((uint8_t*)dest)[2*i2+2]= V;\
833 ((uint8_t*)dest)[2*i2+3]= Y2;\
836 case PIX_FMT_GRAY16BE:\
838 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
839 ((uint8_t*)dest)[2*i2+1]= Y1;\
840 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
841 ((uint8_t*)dest)[2*i2+3]= Y2;\
844 case PIX_FMT_GRAY16LE:\
846 ((uint8_t*)dest)[2*i2+0]= Y1;\
847 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
848 ((uint8_t*)dest)[2*i2+2]= Y2;\
849 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
854 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
855 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
856 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
859 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)
862 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
863 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
864 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
867 int step= c->dstFormatBpp/8;
870 switch(c->dstFormat) {
878 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
879 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
880 dest[aidx]= needAlpha ? A : 255;
887 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
888 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
896 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
913 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
914 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
915 dest[aidx]= needAlpha ? A : 255;
922 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
923 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
931 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
946 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
949 uint8_t *ptr = plane + stride*y;
950 for (i=0; i<height; i++) {
951 memset(ptr, val, width);
956 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
960 for (i = 0; i < width; i++) {
965 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
969 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
970 const uint8_t *src1, const uint8_t *src2,
971 long width, uint32_t *unused)
975 for (i = 0; i < width; i++) {
976 int r = src1[6*i + 0];
977 int g = src1[6*i + 2];
978 int b = src1[6*i + 4];
980 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
981 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
985 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
986 const uint8_t *src1, const uint8_t *src2,
987 long width, uint32_t *unused)
991 for (i = 0; i < width; i++) {
992 int r= src1[12*i + 0] + src1[12*i + 6];
993 int g= src1[12*i + 2] + src1[12*i + 8];
994 int b= src1[12*i + 4] + src1[12*i + 10];
996 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
997 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1001 static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
1005 for (i = 0; i < width; i++) {
1010 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1014 static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1015 const uint8_t *src1, const uint8_t *src2,
1016 long width, uint32_t *unused)
1019 for (i = 0; i < width; i++) {
1020 int b = src1[6*i + 0];
1021 int g = src1[6*i + 2];
1022 int r = src1[6*i + 4];
1024 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1025 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1029 static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1030 const uint8_t *src1, const uint8_t *src2,
1031 long width, uint32_t *unused)
1034 for (i = 0; i < width; i++) {
1035 int b= src1[12*i + 0] + src1[12*i + 6];
1036 int g= src1[12*i + 2] + src1[12*i + 8];
1037 int r= src1[12*i + 4] + src1[12*i + 10];
1039 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1040 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1044 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1045 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1048 for (i=0; i<width; i++) {\
1049 int b= (((const type*)src)[i]>>shb)&maskb;\
1050 int g= (((const type*)src)[i]>>shg)&maskg;\
1051 int r= (((const type*)src)[i]>>shr)&maskr;\
1053 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1057 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1058 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1059 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1060 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1061 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1062 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1063 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1064 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1066 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1069 for (i=0; i<width; i++) {
1074 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1075 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1078 for (i=0; i<width; i++) {\
1079 int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1080 int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1081 int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1083 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1084 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1087 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1090 for (i=0; i<width; i++) {\
1091 int pix0= ((const type*)src)[2*i+0]>>shp;\
1092 int pix1= ((const type*)src)[2*i+1]>>shp;\
1093 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1094 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1095 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1096 g&= maskg|(2*maskg);\
1100 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1101 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1105 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1106 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1107 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1108 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1109 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1110 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1111 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1112 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1114 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1117 for (i=0; i<width; i++) {
1120 dst[i]= pal[d] & 0xFF;
1124 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1125 const uint8_t *src1, const uint8_t *src2,
1126 long width, uint32_t *pal)
1129 assert(src1 == src2);
1130 for (i=0; i<width; i++) {
1131 int p= pal[src1[i]];
1138 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1141 for (i=0; i<width/8; i++) {
1144 dst[8*i+j]= ((d>>(7-j))&1)*255;
1148 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1151 for (i=0; i<width/8; i++) {
1154 dst[8*i+j]= ((d>>(7-j))&1)*255;
1158 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1160 #if CONFIG_RUNTIME_CPUDETECT
1161 # define COMPILE_C 1
1163 # define COMPILE_MMX HAVE_MMX
1164 # define COMPILE_MMX2 HAVE_MMX2
1165 # define COMPILE_3DNOW HAVE_AMD3DNOW
1167 # define COMPILE_ALTIVEC HAVE_ALTIVEC
1169 #else /* CONFIG_RUNTIME_CPUDETECT */
1172 # define COMPILE_MMX2 1
1173 # elif HAVE_AMD3DNOW
1174 # define COMPILE_3DNOW 1
1176 # define COMPILE_MMX 1
1178 # define COMPILE_C 1
1180 # elif ARCH_PPC && HAVE_ALTIVEC
1181 # define COMPILE_ALTIVEC 1
1183 # define COMPILE_C 1
1188 # define COMPILE_C 0
1191 # define COMPILE_MMX 0
1193 #ifndef COMPILE_MMX2
1194 # define COMPILE_MMX2 0
1196 #ifndef COMPILE_3DNOW
1197 # define COMPILE_3DNOW 0
1199 #ifndef COMPILE_ALTIVEC
1200 # define COMPILE_ALTIVEC 0
1203 #define COMPILE_TEMPLATE_MMX 0
1204 #define COMPILE_TEMPLATE_MMX2 0
1205 #define COMPILE_TEMPLATE_AMD3DNOW 0
1206 #define COMPILE_TEMPLATE_ALTIVEC 0
1208 #include "swscale_template.c"
1212 #undef COMPILE_TEMPLATE_ALTIVEC
1213 #define COMPILE_TEMPLATE_ALTIVEC 1
1214 #define RENAME(a) a ## _altivec
1215 #include "ppc/swscale_template.c"
1223 #undef COMPILE_TEMPLATE_MMX
1224 #undef COMPILE_TEMPLATE_MMX2
1225 #undef COMPILE_TEMPLATE_AMD3DNOW
1226 #define COMPILE_TEMPLATE_MMX 1
1227 #define COMPILE_TEMPLATE_MMX2 0
1228 #define COMPILE_TEMPLATE_AMD3DNOW 0
1229 #define RENAME(a) a ## _MMX
1230 #include "x86/swscale_template.c"
1236 #undef COMPILE_TEMPLATE_MMX
1237 #undef COMPILE_TEMPLATE_MMX2
1238 #undef COMPILE_TEMPLATE_AMD3DNOW
1239 #define COMPILE_TEMPLATE_MMX 1
1240 #define COMPILE_TEMPLATE_MMX2 1
1241 #define COMPILE_TEMPLATE_AMD3DNOW 0
1242 #define RENAME(a) a ## _MMX2
1243 #include "x86/swscale_template.c"
1249 #undef COMPILE_TEMPLATE_MMX
1250 #undef COMPILE_TEMPLATE_MMX2
1251 #undef COMPILE_TEMPLATE_AMD3DNOW
1252 #define COMPILE_TEMPLATE_MMX 1
1253 #define COMPILE_TEMPLATE_MMX2 0
1254 #define COMPILE_TEMPLATE_AMD3DNOW 1
1255 #define RENAME(a) a ## _3DNow
1256 #include "x86/swscale_template.c"
1261 SwsFunc ff_getSwsFunc(SwsContext *c)
1263 sws_init_swScale_c(c);
1265 #if CONFIG_RUNTIME_CPUDETECT
1267 // ordered per speed fastest first
1268 if (c->flags & SWS_CPU_CAPS_MMX2) {
1269 sws_init_swScale_MMX2(c);
1270 return swScale_MMX2;
1271 } else if (c->flags & SWS_CPU_CAPS_3DNOW) {
1272 sws_init_swScale_3DNow(c);
1273 return swScale_3DNow;
1274 } else if (c->flags & SWS_CPU_CAPS_MMX) {
1275 sws_init_swScale_MMX(c);
1281 if (c->flags & SWS_CPU_CAPS_ALTIVEC) {
1282 sws_init_swScale_altivec(c);
1283 return swScale_altivec;
1286 #endif /* ARCH_X86 */
1287 #else //CONFIG_RUNTIME_CPUDETECT
1288 #if COMPILE_TEMPLATE_MMX2
1289 sws_init_swScale_MMX2(c);
1290 return swScale_MMX2;
1291 #elif COMPILE_TEMPLATE_AMD3DNOW
1292 sws_init_swScale_3DNow(c);
1293 return swScale_3DNow;
1294 #elif COMPILE_TEMPLATE_MMX
1295 sws_init_swScale_MMX(c);
1297 #elif COMPILE_TEMPLATE_ALTIVEC
1298 sws_init_swScale_altivec(c);
1299 return swScale_altivec;
1301 #endif //!CONFIG_RUNTIME_CPUDETECT
1306 static void copyPlane(const uint8_t *src, int srcStride,
1307 int srcSliceY, int srcSliceH, int width,
1308 uint8_t *dst, int dstStride)
1310 dst += dstStride * srcSliceY;
1311 if (dstStride == srcStride && srcStride > 0) {
1312 memcpy(dst, src, srcSliceH * dstStride);
1315 for (i=0; i<srcSliceH; i++) {
1316 memcpy(dst, src, width);
1323 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1324 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1326 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1328 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1329 dstParam[0], dstStride[0]);
1331 if (c->dstFormat == PIX_FMT_NV12)
1332 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1334 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1339 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1340 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1342 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1344 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1349 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1350 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1352 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1354 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1359 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1360 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1362 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1364 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1369 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1370 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1372 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1374 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1379 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1380 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1382 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1383 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1384 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1386 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1389 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1394 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1395 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1397 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1398 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1399 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1401 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1406 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1407 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1409 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1410 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1411 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1413 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1416 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1421 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1422 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1424 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1425 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1426 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1428 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1433 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1436 for (i=0; i<num_pixels; i++)
1437 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1440 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1444 for (i=0; i<num_pixels; i++)
1445 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1448 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1452 for (i=0; i<num_pixels; i++) {
1454 dst[0]= palette[src[i<<1]*4+0];
1455 dst[1]= palette[src[i<<1]*4+1];
1456 dst[2]= palette[src[i<<1]*4+2];
1461 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1462 int srcSliceH, uint8_t* dst[], int dstStride[])
1464 const enum PixelFormat srcFormat= c->srcFormat;
1465 const enum PixelFormat dstFormat= c->dstFormat;
1466 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1467 const uint8_t *palette)=NULL;
1469 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1470 const uint8_t *srcPtr= src[0];
1472 if (srcFormat == PIX_FMT_Y400A) {
1473 switch (dstFormat) {
1474 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
1475 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
1476 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1477 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1478 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
1479 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
1481 } else if (usePal(srcFormat)) {
1482 switch (dstFormat) {
1483 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
1484 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
1485 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1486 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1487 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
1488 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
1493 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1494 sws_format_name(srcFormat), sws_format_name(dstFormat));
1496 for (i=0; i<srcSliceH; i++) {
1497 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1498 srcPtr+= srcStride[0];
1499 dstPtr+= dstStride[0];
1506 #define isRGBA32(x) ( \
1507 (x) == PIX_FMT_ARGB \
1508 || (x) == PIX_FMT_RGBA \
1509 || (x) == PIX_FMT_BGRA \
1510 || (x) == PIX_FMT_ABGR \
1513 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1514 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1515 int srcSliceH, uint8_t* dst[], int dstStride[])
1517 const enum PixelFormat srcFormat= c->srcFormat;
1518 const enum PixelFormat dstFormat= c->dstFormat;
1519 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1520 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1521 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1522 const int dstId= c->dstFormatBpp >> 2;
1523 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1525 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1527 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1528 if ( CONV_IS(ABGR, RGBA)
1529 || CONV_IS(ARGB, BGRA)
1530 || CONV_IS(BGRA, ARGB)
1531 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1532 else if (CONV_IS(ABGR, ARGB)
1533 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1534 else if (CONV_IS(ABGR, BGRA)
1535 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1536 else if (CONV_IS(BGRA, RGBA)
1537 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1538 else if (CONV_IS(BGRA, ABGR)
1539 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1542 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1543 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1544 switch(srcId | (dstId<<4)) {
1545 case 0x34: conv= rgb16to15; break;
1546 case 0x36: conv= rgb24to15; break;
1547 case 0x38: conv= rgb32to15; break;
1548 case 0x43: conv= rgb15to16; break;
1549 case 0x46: conv= rgb24to16; break;
1550 case 0x48: conv= rgb32to16; break;
1551 case 0x63: conv= rgb15to24; break;
1552 case 0x64: conv= rgb16to24; break;
1553 case 0x68: conv= rgb32to24; break;
1554 case 0x83: conv= rgb15to32; break;
1555 case 0x84: conv= rgb16to32; break;
1556 case 0x86: conv= rgb24to32; break;
1558 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1559 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1560 switch(srcId | (dstId<<4)) {
1561 case 0x33: conv= rgb15tobgr15; break;
1562 case 0x34: conv= rgb16tobgr15; break;
1563 case 0x36: conv= rgb24tobgr15; break;
1564 case 0x38: conv= rgb32tobgr15; break;
1565 case 0x43: conv= rgb15tobgr16; break;
1566 case 0x44: conv= rgb16tobgr16; break;
1567 case 0x46: conv= rgb24tobgr16; break;
1568 case 0x48: conv= rgb32tobgr16; break;
1569 case 0x63: conv= rgb15tobgr24; break;
1570 case 0x64: conv= rgb16tobgr24; break;
1571 case 0x66: conv= rgb24tobgr24; break;
1572 case 0x68: conv= rgb32tobgr24; break;
1573 case 0x83: conv= rgb15tobgr32; break;
1574 case 0x84: conv= rgb16tobgr32; break;
1575 case 0x86: conv= rgb24tobgr32; break;
1580 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1581 sws_format_name(srcFormat), sws_format_name(dstFormat));
1583 const uint8_t *srcPtr= src[0];
1584 uint8_t *dstPtr= dst[0];
1585 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1586 srcPtr += ALT32_CORR;
1588 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1589 dstPtr += ALT32_CORR;
1591 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1592 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1595 dstPtr += dstStride[0]*srcSliceY;
1597 for (i=0; i<srcSliceH; i++) {
1598 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1599 srcPtr+= srcStride[0];
1600 dstPtr+= dstStride[0];
1607 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1608 int srcSliceH, uint8_t* dst[], int dstStride[])
1612 dst[0]+ srcSliceY *dstStride[0],
1613 dst[1]+(srcSliceY>>1)*dstStride[1],
1614 dst[2]+(srcSliceY>>1)*dstStride[2],
1616 dstStride[0], dstStride[1], srcStride[0]);
1618 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1622 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1623 int srcSliceH, uint8_t* dst[], int dstStride[])
1625 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1626 dst[0], dstStride[0]);
1628 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1629 srcSliceH >> 2, srcStride[1], dstStride[1]);
1630 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1631 srcSliceH >> 2, srcStride[2], dstStride[2]);
1633 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1637 /* unscaled copy like stuff (assumes nearly identical formats) */
1638 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1639 int srcSliceH, uint8_t* dst[], int dstStride[])
1641 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1642 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1645 const uint8_t *srcPtr= src[0];
1646 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1649 /* universal length finder */
1650 while(length+c->srcW <= FFABS(dstStride[0])
1651 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1654 for (i=0; i<srcSliceH; i++) {
1655 memcpy(dstPtr, srcPtr, length);
1656 srcPtr+= srcStride[0];
1657 dstPtr+= dstStride[0];
1663 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1664 int srcSliceH, uint8_t* dst[], int dstStride[])
1667 for (plane=0; plane<4; plane++) {
1668 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1669 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1670 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1671 const uint8_t *srcPtr= src[plane];
1672 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1674 if (!dst[plane]) continue;
1675 // ignore palette for GRAY8
1676 if (plane == 1 && !dst[2]) continue;
1677 if (!src[plane] || (plane == 1 && !src[2])) {
1678 if(is16BPS(c->dstFormat))
1680 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1682 if(is9_OR_10BPS(c->srcFormat)) {
1683 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1684 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1685 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1687 if (is16BPS(c->dstFormat)) {
1688 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1689 #define COPY9_OR_10TO16(rfunc, wfunc) \
1690 for (i = 0; i < height; i++) { \
1691 for (j = 0; j < length; j++) { \
1692 int srcpx = rfunc(&srcPtr2[j]); \
1693 wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
1695 dstPtr2 += dstStride[plane]/2; \
1696 srcPtr2 += srcStride[plane]/2; \
1698 if (isBE(c->dstFormat)) {
1699 if (isBE(c->srcFormat)) {
1700 COPY9_OR_10TO16(AV_RB16, AV_WB16);
1702 COPY9_OR_10TO16(AV_RL16, AV_WB16);
1705 if (isBE(c->srcFormat)) {
1706 COPY9_OR_10TO16(AV_RB16, AV_WL16);
1708 COPY9_OR_10TO16(AV_RL16, AV_WL16);
1711 } else if (is9_OR_10BPS(c->dstFormat)) {
1712 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1713 #define COPY9_OR_10TO9_OR_10(loop) \
1714 for (i = 0; i < height; i++) { \
1715 for (j = 0; j < length; j++) { \
1718 dstPtr2 += dstStride[plane]/2; \
1719 srcPtr2 += srcStride[plane]/2; \
1721 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
1722 if (dst_depth > src_depth) { \
1723 COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
1724 wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
1725 } else if (dst_depth < src_depth) { \
1726 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
1728 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
1730 if (isBE(c->dstFormat)) {
1731 if (isBE(c->srcFormat)) {
1732 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
1734 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
1737 if (isBE(c->srcFormat)) {
1738 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
1740 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
1744 // FIXME Maybe dither instead.
1745 #define COPY9_OR_10TO8(rfunc) \
1746 for (i = 0; i < height; i++) { \
1747 for (j = 0; j < length; j++) { \
1748 dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
1750 dstPtr += dstStride[plane]; \
1751 srcPtr2 += srcStride[plane]/2; \
1753 if (isBE(c->srcFormat)) {
1754 COPY9_OR_10TO8(AV_RB16);
1756 COPY9_OR_10TO8(AV_RL16);
1759 } else if(is9_OR_10BPS(c->dstFormat)) {
1760 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1761 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1763 if (is16BPS(c->srcFormat)) {
1764 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1765 #define COPY16TO9_OR_10(rfunc, wfunc) \
1766 for (i = 0; i < height; i++) { \
1767 for (j = 0; j < length; j++) { \
1768 wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
1770 dstPtr2 += dstStride[plane]/2; \
1771 srcPtr2 += srcStride[plane]/2; \
1773 if (isBE(c->dstFormat)) {
1774 if (isBE(c->srcFormat)) {
1775 COPY16TO9_OR_10(AV_RB16, AV_WB16);
1777 COPY16TO9_OR_10(AV_RL16, AV_WB16);
1780 if (isBE(c->srcFormat)) {
1781 COPY16TO9_OR_10(AV_RB16, AV_WL16);
1783 COPY16TO9_OR_10(AV_RL16, AV_WL16);
1787 #define COPY8TO9_OR_10(wfunc) \
1788 for (i = 0; i < height; i++) { \
1789 for (j = 0; j < length; j++) { \
1790 const int srcpx = srcPtr[j]; \
1791 wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
1793 dstPtr2 += dstStride[plane]/2; \
1794 srcPtr += srcStride[plane]; \
1796 if (isBE(c->dstFormat)) {
1797 COPY8TO9_OR_10(AV_WB16);
1799 COPY8TO9_OR_10(AV_WL16);
1802 } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1803 if (!isBE(c->srcFormat)) srcPtr++;
1804 for (i=0; i<height; i++) {
1805 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1806 srcPtr+= srcStride[plane];
1807 dstPtr+= dstStride[plane];
1809 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1810 for (i=0; i<height; i++) {
1811 for (j=0; j<length; j++) {
1812 dstPtr[ j<<1 ] = srcPtr[j];
1813 dstPtr[(j<<1)+1] = srcPtr[j];
1815 srcPtr+= srcStride[plane];
1816 dstPtr+= dstStride[plane];
1818 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1819 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1821 for (i=0; i<height; i++) {
1822 for (j=0; j<length; j++)
1823 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1824 srcPtr+= srcStride[plane];
1825 dstPtr+= dstStride[plane];
1827 } else if (dstStride[plane] == srcStride[plane] &&
1828 srcStride[plane] > 0 && srcStride[plane] == length) {
1829 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1830 height*dstStride[plane]);
1832 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1834 for (i=0; i<height; i++) {
1835 memcpy(dstPtr, srcPtr, length);
1836 srcPtr+= srcStride[plane];
1837 dstPtr+= dstStride[plane];
1845 int ff_hardcodedcpuflags(void)
1848 #if COMPILE_TEMPLATE_MMX2
1849 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1850 #elif COMPILE_TEMPLATE_AMD3DNOW
1851 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1852 #elif COMPILE_TEMPLATE_MMX
1853 flags |= SWS_CPU_CAPS_MMX;
1854 #elif COMPILE_TEMPLATE_ALTIVEC
1855 flags |= SWS_CPU_CAPS_ALTIVEC;
1857 flags |= SWS_CPU_CAPS_BFIN;
1862 void ff_get_unscaled_swscale(SwsContext *c)
1864 const enum PixelFormat srcFormat = c->srcFormat;
1865 const enum PixelFormat dstFormat = c->dstFormat;
1866 const int flags = c->flags;
1867 const int dstH = c->dstH;
1870 needsDither= isAnyRGB(dstFormat)
1871 && c->dstFormatBpp < 24
1872 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1875 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1876 c->swScale= planarToNv12Wrapper;
1879 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1880 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1881 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1884 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1885 c->swScale= yvu9ToYv12Wrapper;
1889 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1890 c->swScale= bgr24ToYv12Wrapper;
1892 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1893 if ( isAnyRGB(srcFormat)
1894 && isAnyRGB(dstFormat)
1895 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1896 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1897 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1898 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1899 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1900 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1901 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1902 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1903 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1904 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1905 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
1906 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
1907 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1908 c->swScale= rgbToRgbWrapper;
1910 if ((usePal(srcFormat) && (
1911 dstFormat == PIX_FMT_RGB32 ||
1912 dstFormat == PIX_FMT_RGB32_1 ||
1913 dstFormat == PIX_FMT_RGB24 ||
1914 dstFormat == PIX_FMT_BGR32 ||
1915 dstFormat == PIX_FMT_BGR32_1 ||
1916 dstFormat == PIX_FMT_BGR24)))
1917 c->swScale= palToRgbWrapper;
1919 if (srcFormat == PIX_FMT_YUV422P) {
1920 if (dstFormat == PIX_FMT_YUYV422)
1921 c->swScale= yuv422pToYuy2Wrapper;
1922 else if (dstFormat == PIX_FMT_UYVY422)
1923 c->swScale= yuv422pToUyvyWrapper;
1926 /* LQ converters if -sws 0 or -sws 4*/
1927 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1929 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1930 if (dstFormat == PIX_FMT_YUYV422)
1931 c->swScale= planarToYuy2Wrapper;
1932 else if (dstFormat == PIX_FMT_UYVY422)
1933 c->swScale= planarToUyvyWrapper;
1936 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1937 c->swScale= yuyvToYuv420Wrapper;
1938 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1939 c->swScale= uyvyToYuv420Wrapper;
1940 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1941 c->swScale= yuyvToYuv422Wrapper;
1942 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1943 c->swScale= uyvyToYuv422Wrapper;
1946 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1947 !(c->flags & SWS_BITEXACT) &&
1948 srcFormat == PIX_FMT_YUV420P) {
1949 // unscaled YV12 -> packed YUV, we want speed
1950 if (dstFormat == PIX_FMT_YUYV422)
1951 c->swScale= yv12toyuy2_unscaled_altivec;
1952 else if (dstFormat == PIX_FMT_UYVY422)
1953 c->swScale= yv12touyvy_unscaled_altivec;
1958 if ( srcFormat == dstFormat
1959 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1960 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1961 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1962 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1963 || (isGray(dstFormat) && isGray(srcFormat))
1964 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1965 && c->chrDstHSubSample == c->chrSrcHSubSample
1966 && c->chrDstVSubSample == c->chrSrcVSubSample
1967 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1968 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1970 if (isPacked(c->srcFormat))
1971 c->swScale= packedCopyWrapper;
1972 else /* Planar YUV or gray */
1973 c->swScale= planarCopyWrapper;
1976 if (flags & SWS_CPU_CAPS_BFIN)
1977 ff_bfin_get_unscaled_swscale (c);
1981 static void reset_ptr(const uint8_t* src[], int format)
1983 if(!isALPHA(format))
1985 if(!isPlanarYUV(format)) {
1988 if (!usePal(format))
1993 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1994 const int linesizes[4])
1996 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1999 for (i = 0; i < 4; i++) {
2000 int plane = desc->comp[i].plane;
2001 if (!data[plane] || !linesizes[plane])
2009 * swscale wrapper, so we don't need to export the SwsContext.
2010 * Assumes planar YUV to be in YUV order instead of YVU.
2012 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
2013 int srcSliceH, uint8_t* const dst[], const int dstStride[])
2016 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2017 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2019 // do not mess up sliceDir if we have a "trailing" 0-size slice
2023 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
2024 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
2027 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
2028 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
2032 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2033 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2036 if (c->sliceDir == 0) {
2037 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2040 if (usePal(c->srcFormat)) {
2041 for (i=0; i<256; i++) {
2042 int p, r, g, b,y,u,v;
2043 if(c->srcFormat == PIX_FMT_PAL8) {
2044 p=((const uint32_t*)(src[1]))[i];
2048 } else if(c->srcFormat == PIX_FMT_RGB8) {
2052 } else if(c->srcFormat == PIX_FMT_BGR8) {
2056 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
2060 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
2063 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2068 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2069 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2070 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2071 c->pal_yuv[i]= y + (u<<8) + (v<<16);
2073 switch(c->dstFormat) {
2078 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2080 case PIX_FMT_BGR32_1:
2084 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2086 case PIX_FMT_RGB32_1:
2090 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2097 c->pal_rgb[i]= b + (g<<8) + (r<<16);
2102 // copy strides, so they can safely be modified
2103 if (c->sliceDir == 1) {
2104 // slices go from top to bottom
2105 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2106 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2108 reset_ptr(src2, c->srcFormat);
2109 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2111 /* reset slice direction at end of frame */
2112 if (srcSliceY + srcSliceH == c->srcH)
2115 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2117 // slices go from bottom to top => we flip the image internally
2118 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2119 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2121 src2[0] += (srcSliceH-1)*srcStride[0];
2122 if (!usePal(c->srcFormat))
2123 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2124 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2125 src2[3] += (srcSliceH-1)*srcStride[3];
2126 dst2[0] += ( c->dstH -1)*dstStride[0];
2127 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2128 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2129 dst2[3] += ( c->dstH -1)*dstStride[3];
2131 reset_ptr(src2, c->srcFormat);
2132 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2134 /* reset slice direction at end of frame */
2138 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2142 /* Convert the palette to the same packed 32-bit format as the palette */
2143 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2147 for (i=0; i<num_pixels; i++)
2148 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2151 /* Palette format: ABCD -> dst format: ABC */
2152 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2156 for (i=0; i<num_pixels; i++) {
2158 dst[0]= palette[src[i]*4+0];
2159 dst[1]= palette[src[i]*4+1];
2160 dst[2]= palette[src[i]*4+2];