2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
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 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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 FFmpeg; 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/avassert.h"
64 #include "libavutil/intreadwrite.h"
65 #include "libavutil/x86_cpu.h"
66 #include "libavutil/cpu.h"
67 #include "libavutil/avutil.h"
68 #include "libavutil/mathematics.h"
69 #include "libavutil/bswap.h"
70 #include "libavutil/pixdesc.h"
77 #define isPacked(x) ( \
79 || (x)==PIX_FMT_YUYV422 \
80 || (x)==PIX_FMT_UYVY422 \
81 || (x)==PIX_FMT_GRAY8A \
85 #define RGB2YUV_SHIFT 15
86 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
87 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
88 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
89 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
90 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
91 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
92 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
93 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
94 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
96 static const double rgb2yuv_table[8][9]={
97 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
98 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
99 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
100 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
101 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
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.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
104 {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
109 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
112 more intelligent misalignment avoidance for the horizontal scaler
113 write special vertical cubic upscale version
114 optimize C code (YV12 / minmax)
115 add support for packed pixel YUV input & output
116 add support for Y8 output
117 optimize BGR24 & BGR32
118 add BGR4 output support
119 write special BGR->BGR scaler
122 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
123 { 1, 3, 1, 3, 1, 3, 1, 3, },
124 { 2, 0, 2, 0, 2, 0, 2, 0, },
127 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
128 { 6, 2, 6, 2, 6, 2, 6, 2, },
129 { 0, 4, 0, 4, 0, 4, 0, 4, },
132 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
133 { 8, 4, 11, 7, 8, 4, 11, 7, },
134 { 2, 14, 1, 13, 2, 14, 1, 13, },
135 { 10, 6, 9, 5, 10, 6, 9, 5, },
136 { 0, 12, 3, 15, 0, 12, 3, 15, },
139 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
140 { 17, 9, 23, 15, 16, 8, 22, 14, },
141 { 5, 29, 3, 27, 4, 28, 2, 26, },
142 { 21, 13, 19, 11, 20, 12, 18, 10, },
143 { 0, 24, 6, 30, 1, 25, 7, 31, },
144 { 16, 8, 22, 14, 17, 9, 23, 15, },
145 { 4, 28, 2, 26, 5, 29, 3, 27, },
146 { 20, 12, 18, 10, 21, 13, 19, 11, },
147 { 1, 25, 7, 31, 0, 24, 6, 30, },
150 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
151 { 0, 55, 14, 68, 3, 58, 17, 72, },
152 { 37, 18, 50, 32, 40, 22, 54, 35, },
153 { 9, 64, 5, 59, 13, 67, 8, 63, },
154 { 46, 27, 41, 23, 49, 31, 44, 26, },
155 { 2, 57, 16, 71, 1, 56, 15, 70, },
156 { 39, 21, 52, 34, 38, 19, 51, 33, },
157 { 11, 66, 7, 62, 10, 65, 6, 60, },
158 { 48, 30, 43, 25, 47, 29, 42, 24, },
162 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
163 {117, 62, 158, 103, 113, 58, 155, 100, },
164 { 34, 199, 21, 186, 31, 196, 17, 182, },
165 {144, 89, 131, 76, 141, 86, 127, 72, },
166 { 0, 165, 41, 206, 10, 175, 52, 217, },
167 {110, 55, 151, 96, 120, 65, 162, 107, },
168 { 28, 193, 14, 179, 38, 203, 24, 189, },
169 {138, 83, 124, 69, 148, 93, 134, 79, },
170 { 7, 172, 48, 213, 3, 168, 45, 210, },
173 // tries to correct a gamma of 1.5
174 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
175 { 0, 143, 18, 200, 2, 156, 25, 215, },
176 { 78, 28, 125, 64, 89, 36, 138, 74, },
177 { 10, 180, 3, 161, 16, 195, 8, 175, },
178 {109, 51, 93, 38, 121, 60, 105, 47, },
179 { 1, 152, 23, 210, 0, 147, 20, 205, },
180 { 85, 33, 134, 71, 81, 30, 130, 67, },
181 { 14, 190, 6, 171, 12, 185, 5, 166, },
182 {117, 57, 101, 44, 113, 54, 97, 41, },
185 // tries to correct a gamma of 2.0
186 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
187 { 0, 124, 8, 193, 0, 140, 12, 213, },
188 { 55, 14, 104, 42, 66, 19, 119, 52, },
189 { 3, 168, 1, 145, 6, 187, 3, 162, },
190 { 86, 31, 70, 21, 99, 39, 82, 28, },
191 { 0, 134, 11, 206, 0, 129, 9, 200, },
192 { 62, 17, 114, 48, 58, 16, 109, 45, },
193 { 5, 181, 2, 157, 4, 175, 1, 151, },
194 { 95, 36, 78, 26, 90, 34, 74, 24, },
197 // tries to correct a gamma of 2.5
198 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
199 { 0, 107, 3, 187, 0, 125, 6, 212, },
200 { 39, 7, 86, 28, 49, 11, 102, 36, },
201 { 1, 158, 0, 131, 3, 180, 1, 151, },
202 { 68, 19, 52, 12, 81, 25, 64, 17, },
203 { 0, 119, 5, 203, 0, 113, 4, 195, },
204 { 45, 9, 96, 33, 42, 8, 91, 30, },
205 { 2, 172, 1, 144, 2, 165, 0, 137, },
206 { 77, 23, 60, 15, 72, 21, 56, 14, },
210 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
212 { 0, 1, 0, 1, 0, 1, 0, 1,},
213 { 1, 0, 1, 0, 1, 0, 1, 0,},
214 { 0, 1, 0, 1, 0, 1, 0, 1,},
215 { 1, 0, 1, 0, 1, 0, 1, 0,},
216 { 0, 1, 0, 1, 0, 1, 0, 1,},
217 { 1, 0, 1, 0, 1, 0, 1, 0,},
218 { 0, 1, 0, 1, 0, 1, 0, 1,},
219 { 1, 0, 1, 0, 1, 0, 1, 0,},
221 { 1, 2, 1, 2, 1, 2, 1, 2,},
222 { 3, 0, 3, 0, 3, 0, 3, 0,},
223 { 1, 2, 1, 2, 1, 2, 1, 2,},
224 { 3, 0, 3, 0, 3, 0, 3, 0,},
225 { 1, 2, 1, 2, 1, 2, 1, 2,},
226 { 3, 0, 3, 0, 3, 0, 3, 0,},
227 { 1, 2, 1, 2, 1, 2, 1, 2,},
228 { 3, 0, 3, 0, 3, 0, 3, 0,},
230 { 2, 4, 3, 5, 2, 4, 3, 5,},
231 { 6, 0, 7, 1, 6, 0, 7, 1,},
232 { 3, 5, 2, 4, 3, 5, 2, 4,},
233 { 7, 1, 6, 0, 7, 1, 6, 0,},
234 { 2, 4, 3, 5, 2, 4, 3, 5,},
235 { 6, 0, 7, 1, 6, 0, 7, 1,},
236 { 3, 5, 2, 4, 3, 5, 2, 4,},
237 { 7, 1, 6, 0, 7, 1, 6, 0,},
239 { 4, 8, 7, 11, 4, 8, 7, 11,},
240 { 12, 0, 15, 3, 12, 0, 15, 3,},
241 { 6, 10, 5, 9, 6, 10, 5, 9,},
242 { 14, 2, 13, 1, 14, 2, 13, 1,},
243 { 4, 8, 7, 11, 4, 8, 7, 11,},
244 { 12, 0, 15, 3, 12, 0, 15, 3,},
245 { 6, 10, 5, 9, 6, 10, 5, 9,},
246 { 14, 2, 13, 1, 14, 2, 13, 1,},
248 { 9, 17, 15, 23, 8, 16, 14, 22,},
249 { 25, 1, 31, 7, 24, 0, 30, 6,},
250 { 13, 21, 11, 19, 12, 20, 10, 18,},
251 { 29, 5, 27, 3, 28, 4, 26, 2,},
252 { 8, 16, 14, 22, 9, 17, 15, 23,},
253 { 24, 0, 30, 6, 25, 1, 31, 7,},
254 { 12, 20, 10, 18, 13, 21, 11, 19,},
255 { 28, 4, 26, 2, 29, 5, 27, 3,},
257 { 18, 34, 30, 46, 17, 33, 29, 45,},
258 { 50, 2, 62, 14, 49, 1, 61, 13,},
259 { 26, 42, 22, 38, 25, 41, 21, 37,},
260 { 58, 10, 54, 6, 57, 9, 53, 5,},
261 { 16, 32, 28, 44, 19, 35, 31, 47,},
262 { 48, 0, 60, 12, 51, 3, 63, 15,},
263 { 24, 40, 20, 36, 27, 43, 23, 39,},
264 { 56, 8, 52, 4, 59, 11, 55, 7,},
266 { 18, 34, 30, 46, 17, 33, 29, 45,},
267 { 50, 2, 62, 14, 49, 1, 61, 13,},
268 { 26, 42, 22, 38, 25, 41, 21, 37,},
269 { 58, 10, 54, 6, 57, 9, 53, 5,},
270 { 16, 32, 28, 44, 19, 35, 31, 47,},
271 { 48, 0, 60, 12, 51, 3, 63, 15,},
272 { 24, 40, 20, 36, 27, 43, 23, 39,},
273 { 56, 8, 52, 4, 59, 11, 55, 7,},
275 { 36, 68, 60, 92, 34, 66, 58, 90,},
276 { 100, 4,124, 28, 98, 2,122, 26,},
277 { 52, 84, 44, 76, 50, 82, 42, 74,},
278 { 116, 20,108, 12,114, 18,106, 10,},
279 { 32, 64, 56, 88, 38, 70, 62, 94,},
280 { 96, 0,120, 24,102, 6,126, 30,},
281 { 48, 80, 40, 72, 54, 86, 46, 78,},
282 { 112, 16,104, 8,118, 22,110, 14,},
285 uint16_t dither_scale[15][16]={
286 { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
287 { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
288 { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
289 { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
290 { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
291 { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
292 { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
293 { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
294 { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
295 { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
296 { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
297 { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
298 { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
299 { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
300 { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
303 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
304 const int16_t *chrFilter, const int16_t **chrUSrc,
305 const int16_t **chrVSrc, int chrFilterSize,
306 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
307 int dstW, int chrDstW, int big_endian, int output_bits)
309 //FIXME Optimize (just quickly written not optimized..)
311 int shift = 11 + 16 - output_bits;
313 #define output_pixel(pos, val) \
315 if (output_bits == 16) { \
316 AV_WB16(pos, av_clip_uint16(val >> shift)); \
318 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
321 if (output_bits == 16) { \
322 AV_WL16(pos, av_clip_uint16(val >> shift)); \
324 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
327 for (i = 0; i < dstW; i++) {
328 int val = 1 << (26-output_bits);
331 for (j = 0; j < lumFilterSize; j++)
332 val += lumSrc[j][i] * lumFilter[j];
334 output_pixel(&dest[i], val);
338 for (i = 0; i < chrDstW; i++) {
339 int u = 1 << (26-output_bits);
340 int v = 1 << (26-output_bits);
343 for (j = 0; j < chrFilterSize; j++) {
344 u += chrUSrc[j][i] * chrFilter[j];
345 v += chrVSrc[j][i] * chrFilter[j];
348 output_pixel(&uDest[i], u);
349 output_pixel(&vDest[i], v);
353 if (CONFIG_SWSCALE_ALPHA && aDest) {
354 for (i = 0; i < dstW; i++) {
355 int val = 1 << (26-output_bits);
358 for (j = 0; j < lumFilterSize; j++)
359 val += alpSrc[j][i] * lumFilter[j];
361 output_pixel(&aDest[i], val);
366 #define yuv2NBPS(bits, BE_LE, is_be) \
367 static void yuv2yuvX ## bits ## BE_LE ## _c(const int16_t *lumFilter, \
368 const int16_t **lumSrc, int lumFilterSize, \
369 const int16_t *chrFilter, const int16_t **chrUSrc, \
370 const int16_t **chrVSrc, \
371 int chrFilterSize, const int16_t **alpSrc, \
372 uint16_t *dest, uint16_t *uDest, uint16_t *vDest, \
373 uint16_t *aDest, int dstW, int chrDstW) \
375 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
376 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
378 dest, uDest, vDest, aDest, \
379 dstW, chrDstW, is_be, bits); \
388 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
389 const int16_t *chrFilter, const int16_t **chrUSrc, const int16_t **chrVSrc, int chrFilterSize,
390 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
391 enum PixelFormat dstFormat)
393 if (isNBPS(dstFormat)) {
394 const int depth = av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1+1;
395 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
396 chrFilter, chrUSrc, chrVSrc, chrFilterSize,
398 dest, uDest, vDest, aDest,
399 dstW, chrDstW, isBE(dstFormat), depth);
401 if (isBE(dstFormat)) {
402 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
403 chrFilter, chrUSrc, chrVSrc, chrFilterSize,
405 dest, uDest, vDest, aDest,
406 dstW, chrDstW, 1, 16);
408 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
409 chrFilter, chrUSrc, chrVSrc, chrFilterSize,
411 dest, uDest, vDest, aDest,
412 dstW, chrDstW, 0, 16);
417 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
418 const int16_t *chrFilter, const int16_t **chrUSrc,
419 const int16_t **chrVSrc, int chrFilterSize,
420 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
422 //FIXME Optimize (just quickly written not optimized..)
424 for (i=0; i<dstW; i++) {
427 for (j=0; j<lumFilterSize; j++)
428 val += lumSrc[j][i] * lumFilter[j];
430 dest[i]= av_clip_uint8(val>>19);
434 for (i=0; i<chrDstW; i++) {
438 for (j=0; j<chrFilterSize; j++) {
439 u += chrUSrc[j][i] * chrFilter[j];
440 v += chrVSrc[j][i] * chrFilter[j];
443 uDest[i]= av_clip_uint8(u>>19);
444 vDest[i]= av_clip_uint8(v>>19);
447 if (CONFIG_SWSCALE_ALPHA && aDest)
448 for (i=0; i<dstW; i++) {
451 for (j=0; j<lumFilterSize; j++)
452 val += alpSrc[j][i] * lumFilter[j];
454 aDest[i]= av_clip_uint8(val>>19);
459 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
460 const int16_t *chrFilter, const int16_t **chrUSrc,
461 const int16_t **chrVSrc, int chrFilterSize,
462 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
464 //FIXME Optimize (just quickly written not optimized..)
466 for (i=0; i<dstW; i++) {
469 for (j=0; j<lumFilterSize; j++)
470 val += lumSrc[j][i] * lumFilter[j];
472 dest[i]= av_clip_uint8(val>>19);
478 if (dstFormat == PIX_FMT_NV12)
479 for (i=0; i<chrDstW; i++) {
483 for (j=0; j<chrFilterSize; j++) {
484 u += chrUSrc[j][i] * chrFilter[j];
485 v += chrVSrc[j][i] * chrFilter[j];
488 uDest[2*i]= av_clip_uint8(u>>19);
489 uDest[2*i+1]= av_clip_uint8(v>>19);
492 for (i=0; i<chrDstW; i++) {
496 for (j=0; j<chrFilterSize; j++) {
497 u += chrUSrc[j][i] * chrFilter[j];
498 v += chrVSrc[j][i] * chrFilter[j];
501 uDest[2*i]= av_clip_uint8(v>>19);
502 uDest[2*i+1]= av_clip_uint8(u>>19);
506 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
507 for (i=0; i<(dstW>>1); i++) {\
513 int av_unused A1, A2;\
514 type av_unused *r, *b, *g;\
517 for (j=0; j<lumFilterSize; j++) {\
518 Y1 += lumSrc[j][i2] * lumFilter[j];\
519 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
521 for (j=0; j<chrFilterSize; j++) {\
522 U += chrUSrc[j][i] * chrFilter[j];\
523 V += chrVSrc[j][i] * chrFilter[j];\
532 for (j=0; j<lumFilterSize; j++) {\
533 A1 += alpSrc[j][i2 ] * lumFilter[j];\
534 A2 += alpSrc[j][i2+1] * lumFilter[j];\
540 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
541 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
542 if ((Y1|Y2|U|V)&256) {\
543 if (Y1>255) Y1=255; \
544 else if (Y1<0)Y1=0; \
545 if (Y2>255) Y2=255; \
546 else if (Y2<0)Y2=0; \
552 if (alpha && ((A1|A2)&256)) {\
553 A1=av_clip_uint8(A1);\
554 A2=av_clip_uint8(A2);\
557 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
558 for (i=0; i<dstW; i++) {\
566 for (j=0; j<lumFilterSize; j++) {\
567 Y += lumSrc[j][i ] * lumFilter[j];\
569 for (j=0; j<chrFilterSize; j++) {\
570 U += chrUSrc[j][i] * chrFilter[j];\
571 V += chrVSrc[j][i] * chrFilter[j];\
578 for (j=0; j<lumFilterSize; j++)\
579 A += alpSrc[j][i ] * lumFilter[j];\
582 A = av_clip_uint8(A);\
585 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
586 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
587 Y-= c->yuv2rgb_y_offset;\
588 Y*= c->yuv2rgb_y_coeff;\
590 R= Y + V*c->yuv2rgb_v2r_coeff;\
591 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
592 B= Y + U*c->yuv2rgb_u2b_coeff;\
593 if ((R|G|B)&(0xC0000000)) {\
594 if (R>=(256<<22)) R=(256<<22)-1; \
596 if (G>=(256<<22)) G=(256<<22)-1; \
598 if (B>=(256<<22)) B=(256<<22)-1; \
602 #define YSCALE_YUV_2_GRAY16_C \
603 for (i=0; i<(dstW>>1); i++) {\
612 for (j=0; j<lumFilterSize; j++) {\
613 Y1 += lumSrc[j][i2] * lumFilter[j];\
614 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
618 if ((Y1|Y2|U|V)&65536) {\
619 if (Y1>65535) Y1=65535; \
620 else if (Y1<0)Y1=0; \
621 if (Y2>65535) Y2=65535; \
622 else if (Y2<0)Y2=0; \
625 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
626 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
627 r = (type *)c->table_rV[V]; \
628 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
629 b = (type *)c->table_bU[U];
631 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
632 for (i=0; i<(dstW>>1); i++) { \
634 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
635 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
636 int U= (ubuf0[i]*uvalpha1+ubuf1[i]*uvalpha)>>19; \
637 int V= (vbuf0[i]*uvalpha1+vbuf1[i]*uvalpha)>>19; \
638 type av_unused *r, *b, *g; \
639 int av_unused A1, A2; \
641 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
642 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
645 #define YSCALE_YUV_2_GRAY16_2_C \
646 for (i=0; i<(dstW>>1); i++) { \
648 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
649 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
651 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
652 YSCALE_YUV_2_PACKED2_C(type,alpha)\
653 r = (type *)c->table_rV[V];\
654 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
655 b = (type *)c->table_bU[U];
657 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
658 for (i=0; i<(dstW>>1); i++) {\
660 int Y1= buf0[i2 ]>>7;\
661 int Y2= buf0[i2+1]>>7;\
662 int U= (ubuf1[i])>>7;\
663 int V= (vbuf1[i])>>7;\
664 type av_unused *r, *b, *g;\
665 int av_unused A1, A2;\
671 #define YSCALE_YUV_2_GRAY16_1_C \
672 for (i=0; i<(dstW>>1); i++) {\
674 int Y1= buf0[i2 ]<<1;\
675 int Y2= buf0[i2+1]<<1;
677 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
678 YSCALE_YUV_2_PACKED1_C(type,alpha)\
679 r = (type *)c->table_rV[V];\
680 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
681 b = (type *)c->table_bU[U];
683 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
684 for (i=0; i<(dstW>>1); i++) {\
686 int Y1= buf0[i2 ]>>7;\
687 int Y2= buf0[i2+1]>>7;\
688 int U= (ubuf0[i] + ubuf1[i])>>8;\
689 int V= (vbuf0[i] + vbuf1[i])>>8;\
690 type av_unused *r, *b, *g;\
691 int av_unused A1, A2;\
697 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
698 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
699 r = (type *)c->table_rV[V];\
700 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
701 b = (type *)c->table_bU[U];
703 #define YSCALE_YUV_2_MONO2_C \
704 const uint8_t * const d128=dither_8x8_220[y&7];\
705 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
706 for (i=0; i<dstW-7; i+=8) {\
708 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
709 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
710 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
711 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
712 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
713 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
714 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
715 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
716 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
720 #define YSCALE_YUV_2_MONOX_C \
721 const uint8_t * const d128=dither_8x8_220[y&7];\
722 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
724 for (i=0; i<dstW-1; i+=2) {\
729 for (j=0; j<lumFilterSize; j++) {\
730 Y1 += lumSrc[j][i] * lumFilter[j];\
731 Y2 += lumSrc[j][i+1] * lumFilter[j];\
741 acc+= acc + g[Y1+d128[(i+0)&7]];\
742 acc+= acc + g[Y2+d128[(i+1)&7]];\
744 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
749 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
750 switch(c->dstFormat) {\
751 case PIX_FMT_RGB48BE:\
752 case PIX_FMT_RGB48LE:\
754 ((uint8_t*)dest)[ 0]= r[Y1];\
755 ((uint8_t*)dest)[ 1]= r[Y1];\
756 ((uint8_t*)dest)[ 2]= g[Y1];\
757 ((uint8_t*)dest)[ 3]= g[Y1];\
758 ((uint8_t*)dest)[ 4]= b[Y1];\
759 ((uint8_t*)dest)[ 5]= b[Y1];\
760 ((uint8_t*)dest)[ 6]= r[Y2];\
761 ((uint8_t*)dest)[ 7]= r[Y2];\
762 ((uint8_t*)dest)[ 8]= g[Y2];\
763 ((uint8_t*)dest)[ 9]= g[Y2];\
764 ((uint8_t*)dest)[10]= b[Y2];\
765 ((uint8_t*)dest)[11]= b[Y2];\
769 case PIX_FMT_BGR48BE:\
770 case PIX_FMT_BGR48LE:\
772 ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
773 ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
774 ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
775 ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
776 ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
777 ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
784 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
785 func(uint32_t,needAlpha)\
786 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
787 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
790 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
792 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
793 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
797 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
798 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
806 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
807 func(uint32_t,needAlpha)\
808 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
809 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
812 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
814 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
815 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
819 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
820 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
827 ((uint8_t*)dest)[0]= r[Y1];\
828 ((uint8_t*)dest)[1]= g[Y1];\
829 ((uint8_t*)dest)[2]= b[Y1];\
830 ((uint8_t*)dest)[3]= r[Y2];\
831 ((uint8_t*)dest)[4]= g[Y2];\
832 ((uint8_t*)dest)[5]= b[Y2];\
838 ((uint8_t*)dest)[0]= b[Y1];\
839 ((uint8_t*)dest)[1]= g[Y1];\
840 ((uint8_t*)dest)[2]= r[Y1];\
841 ((uint8_t*)dest)[3]= b[Y2];\
842 ((uint8_t*)dest)[4]= g[Y2];\
843 ((uint8_t*)dest)[5]= r[Y2];\
847 case PIX_FMT_RGB565BE:\
848 case PIX_FMT_RGB565LE:\
849 case PIX_FMT_BGR565BE:\
850 case PIX_FMT_BGR565LE:\
852 const int dr1= dither_2x2_8[y&1 ][0];\
853 const int dg1= dither_2x2_4[y&1 ][0];\
854 const int db1= dither_2x2_8[(y&1)^1][0];\
855 const int dr2= dither_2x2_8[y&1 ][1];\
856 const int dg2= dither_2x2_4[y&1 ][1];\
857 const int db2= dither_2x2_8[(y&1)^1][1];\
859 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
860 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
864 case PIX_FMT_RGB555BE:\
865 case PIX_FMT_RGB555LE:\
866 case PIX_FMT_BGR555BE:\
867 case PIX_FMT_BGR555LE:\
869 const int dr1= dither_2x2_8[y&1 ][0];\
870 const int dg1= dither_2x2_8[y&1 ][1];\
871 const int db1= dither_2x2_8[(y&1)^1][0];\
872 const int dr2= dither_2x2_8[y&1 ][1];\
873 const int dg2= dither_2x2_8[y&1 ][0];\
874 const int db2= dither_2x2_8[(y&1)^1][1];\
876 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
877 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
881 case PIX_FMT_RGB444BE:\
882 case PIX_FMT_RGB444LE:\
883 case PIX_FMT_BGR444BE:\
884 case PIX_FMT_BGR444LE:\
886 const int dr1= dither_4x4_16[y&3 ][0];\
887 const int dg1= dither_4x4_16[y&3 ][1];\
888 const int db1= dither_4x4_16[(y&3)^3][0];\
889 const int dr2= dither_4x4_16[y&3 ][1];\
890 const int dg2= dither_4x4_16[y&3 ][0];\
891 const int db2= dither_4x4_16[(y&3)^3][1];\
893 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
894 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
901 const uint8_t * const d64= dither_8x8_73[y&7];\
902 const uint8_t * const d32= dither_8x8_32[y&7];\
904 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
905 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
912 const uint8_t * const d64= dither_8x8_73 [y&7];\
913 const uint8_t * const d128=dither_8x8_220[y&7];\
915 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
916 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
920 case PIX_FMT_RGB4_BYTE:\
921 case PIX_FMT_BGR4_BYTE:\
923 const uint8_t * const d64= dither_8x8_73 [y&7];\
924 const uint8_t * const d128=dither_8x8_220[y&7];\
926 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
927 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
931 case PIX_FMT_MONOBLACK:\
932 case PIX_FMT_MONOWHITE:\
937 case PIX_FMT_YUYV422:\
939 ((uint8_t*)dest)[2*i2+0]= Y1;\
940 ((uint8_t*)dest)[2*i2+1]= U;\
941 ((uint8_t*)dest)[2*i2+2]= Y2;\
942 ((uint8_t*)dest)[2*i2+3]= V;\
945 case PIX_FMT_UYVY422:\
947 ((uint8_t*)dest)[2*i2+0]= U;\
948 ((uint8_t*)dest)[2*i2+1]= Y1;\
949 ((uint8_t*)dest)[2*i2+2]= V;\
950 ((uint8_t*)dest)[2*i2+3]= Y2;\
953 case PIX_FMT_GRAY16BE:\
955 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
956 ((uint8_t*)dest)[2*i2+1]= Y1;\
957 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
958 ((uint8_t*)dest)[2*i2+3]= Y2;\
961 case PIX_FMT_GRAY16LE:\
963 ((uint8_t*)dest)[2*i2+0]= Y1;\
964 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
965 ((uint8_t*)dest)[2*i2+2]= Y2;\
966 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
971 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter,
972 const int16_t **lumSrc, int lumFilterSize,
973 const int16_t *chrFilter, const int16_t **chrUSrc,
974 const int16_t **chrVSrc, int chrFilterSize,
975 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
978 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)
981 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter,
982 const int16_t **lumSrc, int lumFilterSize,
983 const int16_t *chrFilter, const int16_t **chrUSrc,
984 const int16_t **chrVSrc, int chrFilterSize,
985 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
988 int step= c->dstFormatBpp/8;
991 switch(c->dstFormat) {
999 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1000 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1001 dest[aidx]= needAlpha ? A : 255;
1008 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1009 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1017 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1034 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1035 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1036 dest[aidx]= needAlpha ? A : 255;
1043 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1044 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1052 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1067 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1070 uint8_t *ptr = plane + stride*y;
1071 for (i=0; i<height; i++) {
1072 memset(ptr, val, width);
1077 #define RGB48(name, R, B, READ)\
1078 static inline void name ## ToY(int16_t *dst, const uint16_t *src, int width, uint32_t *unused)\
1081 for (i = 0; i < width; i++) {\
1082 int r = READ(&src[i*3+R]);\
1083 int g = READ(&src[i*3+1]);\
1084 int b = READ(&src[i*3+B]);\
1086 dst[i] = (RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1090 static inline void name ## ToUV(int16_t *dstU, int16_t *dstV,\
1091 const uint16_t *src1, const uint16_t *src2,\
1092 int width, uint32_t *unused)\
1095 assert(src1==src2);\
1096 for (i = 0; i < width; i++) {\
1097 int r = READ(&src1[3*i + R]);\
1098 int g = READ(&src1[3*i + 1]);\
1099 int b = READ(&src1[3*i + B]);\
1101 dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1102 dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1106 static inline void name ## ToUV_half(int16_t *dstU, int16_t *dstV,\
1107 const uint16_t *src1, const uint16_t *src2,\
1108 int width, uint32_t *unused)\
1111 assert(src1==src2);\
1112 for (i = 0; i < width; i++) {\
1113 int r= READ(&src1[6*i + R]) + READ(&src1[6*i + 3+R]);\
1114 int g= READ(&src1[6*i + 1]) + READ(&src1[6*i + 4]);\
1115 int b= READ(&src1[6*i + B]) + READ(&src1[6*i + 3+B]);\
1117 dstU[i]= (RU*r + GU*g + BU*b + (256U<<(RGB2YUV_SHIFT+8)) + (1<<(RGB2YUV_SHIFT-6+8))) >> (RGB2YUV_SHIFT-5+8);\
1118 dstV[i]= (RV*r + GV*g + BV*b + (256U<<(RGB2YUV_SHIFT+8)) + (1<<(RGB2YUV_SHIFT-6+8))) >> (RGB2YUV_SHIFT-5+8);\
1122 RGB48(rgb48LE, 0, 2, AV_RL16)
1123 RGB48(rgb48BE, 0, 2, AV_RB16)
1124 RGB48(bgr48LE, 2, 0, AV_RL16)
1125 RGB48(bgr48BE, 2, 0, AV_RB16)
1127 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1128 static inline void name(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)\
1131 for (i=0; i<width; i++) {\
1132 int b= (((const type*)src)[i]>>shb)&maskb;\
1133 int g= (((const type*)src)[i]>>shg)&maskg;\
1134 int r= (((const type*)src)[i]>>shr)&maskr;\
1136 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (32<<((S)-1)) + (1<<(S-7)))>>((S)-6));\
1140 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1141 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1142 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1143 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1144 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1145 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1146 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1147 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1149 static inline void abgrToA(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1152 for (i=0; i<width; i++) {
1153 dst[i]= src[4*i]<<6;
1157 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1158 static inline void name(int16_t *dstU, int16_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1161 for (i=0; i<width; i++) {\
1162 int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1163 int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1164 int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1166 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (256<<((S)-1)) + (1<<(S-7)))>>((S)-6);\
1167 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (256<<((S)-1)) + (1<<(S-7)))>>((S)-6);\
1170 static inline void name ## _half(int16_t *dstU, int16_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1173 for (i=0; i<width; i++) {\
1174 int pix0= ((const type*)src)[2*i+0]>>shp;\
1175 int pix1= ((const type*)src)[2*i+1]>>shp;\
1176 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1177 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1178 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1179 g&= maskg|(2*maskg);\
1183 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (256U<<(S)) + (1<<(S-6)))>>((S)-6+1);\
1184 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (256U<<(S)) + (1<<(S-6)))>>((S)-6+1);\
1188 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1189 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1190 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1191 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1192 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1193 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1194 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1195 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1197 static inline void palToA(int16_t *dst, const uint8_t *src, int width, uint32_t *pal)
1200 for (i=0; i<width; i++) {
1203 dst[i]= (pal[d] >> 24)<<6;
1207 static inline void palToY(int16_t *dst, const uint8_t *src, long width, uint32_t *pal)
1210 for (i=0; i<width; i++) {
1213 dst[i]= (pal[d] & 0xFF)<<6;
1217 static inline void palToUV(uint16_t *dstU, int16_t *dstV,
1218 const uint8_t *src1, const uint8_t *src2,
1219 int width, uint32_t *pal)
1222 assert(src1 == src2);
1223 for (i=0; i<width; i++) {
1224 int p= pal[src1[i]];
1226 dstU[i]= (uint8_t)(p>> 8)<<6;
1227 dstV[i]= (uint8_t)(p>>16)<<6;
1231 static inline void monowhite2Y(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1234 for (i=0; i<width/8; i++) {
1237 dst[8*i+j]= ((d>>(7-j))&1)*16383;
1241 static inline void monoblack2Y(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1244 for (i=0; i<width/8; i++) {
1247 dst[8*i+j]= ((d>>(7-j))&1)*16383;
1251 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1254 #define COMPILE_TEMPLATE_MMX2 0
1255 #define COMPILE_TEMPLATE_ALTIVEC 0
1257 #include "swscale_template.c"
1261 #undef COMPILE_TEMPLATE_ALTIVEC
1262 #define COMPILE_TEMPLATE_ALTIVEC 1
1263 #define RENAME(a) a ## _altivec
1264 #include "ppc/swscale_template.c"
1270 #undef COMPILE_TEMPLATE_MMX2
1271 #define COMPILE_TEMPLATE_MMX2 0
1272 #define RENAME(a) a ## _MMX
1273 #include "x86/swscale_template.c"
1279 #undef COMPILE_TEMPLATE_MMX2
1280 #define COMPILE_TEMPLATE_MMX2 1
1281 #define RENAME(a) a ## _MMX2
1282 #include "x86/swscale_template.c"
1285 SwsFunc ff_getSwsFunc(SwsContext *c)
1287 int cpu_flags = av_get_cpu_flags();
1289 sws_init_swScale_c(c);
1292 if (cpu_flags & AV_CPU_FLAG_MMX)
1293 sws_init_swScale_MMX(c);
1296 if (cpu_flags & AV_CPU_FLAG_MMX2)
1297 sws_init_swScale_MMX2(c);
1300 if (cpu_flags & AV_CPU_FLAG_ALTIVEC)
1301 sws_init_swScale_altivec(c);
1307 static void copyPlane(const uint8_t *src, int srcStride,
1308 int srcSliceY, int srcSliceH, int width,
1309 uint8_t *dst, int dstStride)
1311 dst += dstStride * srcSliceY;
1312 if (dstStride == srcStride && srcStride > 0) {
1313 memcpy(dst, src, srcSliceH * dstStride);
1316 for (i=0; i<srcSliceH; i++) {
1317 memcpy(dst, src, width);
1324 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1325 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1327 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1329 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1330 dstParam[0], dstStride[0]);
1332 if (c->dstFormat == PIX_FMT_NV12)
1333 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1335 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1340 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1341 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1343 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1345 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1350 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1351 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1353 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1355 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1360 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1361 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1363 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1365 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1370 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1371 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1373 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1375 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1380 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1381 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1383 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1384 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1385 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1387 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1390 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1395 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1396 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1398 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1399 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1400 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1402 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1407 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1408 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1410 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1411 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1412 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1414 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1417 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1422 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1423 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1425 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1426 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1427 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1429 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1434 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1437 for (i=0; i<num_pixels; i++)
1438 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1441 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1445 for (i=0; i<num_pixels; i++)
1446 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1449 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1453 for (i=0; i<num_pixels; i++) {
1455 dst[0]= palette[src[i<<1]*4+0];
1456 dst[1]= palette[src[i<<1]*4+1];
1457 dst[2]= palette[src[i<<1]*4+2];
1462 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1463 int srcSliceH, uint8_t* dst[], int dstStride[])
1465 const enum PixelFormat srcFormat= c->srcFormat;
1466 const enum PixelFormat dstFormat= c->dstFormat;
1467 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
1468 const uint8_t *palette)=NULL;
1470 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1471 const uint8_t *srcPtr= src[0];
1473 if (srcFormat == PIX_FMT_GRAY8A) {
1474 switch (dstFormat) {
1475 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
1476 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
1477 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1478 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1479 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
1480 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
1482 } else if (usePal(srcFormat)) {
1483 switch (dstFormat) {
1484 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
1485 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
1486 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1487 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1488 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
1489 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
1494 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1495 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1497 for (i=0; i<srcSliceH; i++) {
1498 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1499 srcPtr+= srcStride[0];
1500 dstPtr+= dstStride[0];
1507 #define isRGBA32(x) ( \
1508 (x) == PIX_FMT_ARGB \
1509 || (x) == PIX_FMT_RGBA \
1510 || (x) == PIX_FMT_BGRA \
1511 || (x) == PIX_FMT_ABGR \
1514 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1515 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1516 int srcSliceH, uint8_t* dst[], int dstStride[])
1518 const enum PixelFormat srcFormat= c->srcFormat;
1519 const enum PixelFormat dstFormat= c->dstFormat;
1520 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1521 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1522 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1523 const int dstId= c->dstFormatBpp >> 2;
1524 void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
1526 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1528 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1529 if ( CONV_IS(ABGR, RGBA)
1530 || CONV_IS(ARGB, BGRA)
1531 || CONV_IS(BGRA, ARGB)
1532 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1533 else if (CONV_IS(ABGR, ARGB)
1534 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1535 else if (CONV_IS(ABGR, BGRA)
1536 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1537 else if (CONV_IS(BGRA, RGBA)
1538 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1539 else if (CONV_IS(BGRA, ABGR)
1540 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1543 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1544 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1545 switch(srcId | (dstId<<4)) {
1546 case 0x34: conv= rgb16to15; break;
1547 case 0x36: conv= rgb24to15; break;
1548 case 0x38: conv= rgb32to15; break;
1549 case 0x43: conv= rgb15to16; break;
1550 case 0x46: conv= rgb24to16; break;
1551 case 0x48: conv= rgb32to16; break;
1552 case 0x63: conv= rgb15to24; break;
1553 case 0x64: conv= rgb16to24; break;
1554 case 0x68: conv= rgb32to24; break;
1555 case 0x83: conv= rgb15to32; break;
1556 case 0x84: conv= rgb16to32; break;
1557 case 0x86: conv= rgb24to32; break;
1559 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1560 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1561 switch(srcId | (dstId<<4)) {
1562 case 0x33: conv= rgb15tobgr15; break;
1563 case 0x34: conv= rgb16tobgr15; break;
1564 case 0x36: conv= rgb24tobgr15; break;
1565 case 0x38: conv= rgb32tobgr15; break;
1566 case 0x43: conv= rgb15tobgr16; break;
1567 case 0x44: conv= rgb16tobgr16; break;
1568 case 0x46: conv= rgb24tobgr16; break;
1569 case 0x48: conv= rgb32tobgr16; break;
1570 case 0x63: conv= rgb15tobgr24; break;
1571 case 0x64: conv= rgb16tobgr24; break;
1572 case 0x66: conv= rgb24tobgr24; break;
1573 case 0x68: conv= rgb32tobgr24; break;
1574 case 0x83: conv= rgb15tobgr32; break;
1575 case 0x84: conv= rgb16tobgr32; break;
1576 case 0x86: conv= rgb24tobgr32; break;
1581 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1582 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1584 const uint8_t *srcPtr= src[0];
1585 uint8_t *dstPtr= dst[0];
1586 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1587 srcPtr += ALT32_CORR;
1589 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1590 dstPtr += ALT32_CORR;
1592 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
1593 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1596 dstPtr += dstStride[0]*srcSliceY;
1598 for (i=0; i<srcSliceH; i++) {
1599 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1600 srcPtr+= srcStride[0];
1601 dstPtr+= dstStride[0];
1608 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1609 int srcSliceH, uint8_t* dst[], int dstStride[])
1613 dst[0]+ srcSliceY *dstStride[0],
1614 dst[1]+(srcSliceY>>1)*dstStride[1],
1615 dst[2]+(srcSliceY>>1)*dstStride[2],
1617 dstStride[0], dstStride[1], srcStride[0]);
1619 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1623 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1624 int srcSliceH, uint8_t* dst[], int dstStride[])
1626 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1627 dst[0], dstStride[0]);
1629 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1630 srcSliceH >> 2, srcStride[1], dstStride[1]);
1631 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1632 srcSliceH >> 2, srcStride[2], dstStride[2]);
1634 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1638 /* unscaled copy like stuff (assumes nearly identical formats) */
1639 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1640 int srcSliceH, uint8_t* dst[], int dstStride[])
1642 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1643 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1646 const uint8_t *srcPtr= src[0];
1647 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1650 /* universal length finder */
1651 while(length+c->srcW <= FFABS(dstStride[0])
1652 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1655 for (i=0; i<srcSliceH; i++) {
1656 memcpy(dstPtr, srcPtr, length);
1657 srcPtr+= srcStride[0];
1658 dstPtr+= dstStride[0];
1664 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
1665 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
1666 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
1667 for (i = 0; i < height; i++) {\
1668 const uint8_t *dither= dithers[src_depth-9][i&7];\
1669 for (j = 0; j < length-7; j+=8){\
1670 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
1671 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
1672 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
1673 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
1674 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
1675 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
1676 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
1677 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
1679 for (; j < length; j++)\
1680 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
1686 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1687 int srcSliceH, uint8_t* dst[], int dstStride[])
1690 for (plane=0; plane<4; plane++) {
1691 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1692 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1693 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1694 const uint8_t *srcPtr= src[plane];
1695 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1697 if (!dst[plane]) continue;
1698 // ignore palette for GRAY8
1699 if (plane == 1 && !dst[2]) continue;
1700 if (!src[plane] || (plane == 1 && !src[2])) {
1701 if(is16BPS(c->dstFormat))
1703 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1705 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
1706 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
1708 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1709 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1710 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1711 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1713 if (dst_depth == 8) {
1714 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1715 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
1717 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
1719 } else if (src_depth == 8) {
1720 for (i = 0; i < height; i++) {
1721 if(isBE(c->dstFormat)){
1722 for (j = 0; j < length; j++)
1723 AV_WB16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
1724 (srcPtr[j]>>(2*8-dst_depth)));
1726 for (j = 0; j < length; j++)
1727 AV_WL16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
1728 (srcPtr[j]>>(2*8-dst_depth)));
1730 dstPtr2 += dstStride[plane]/2;
1731 srcPtr += srcStride[plane];
1733 } else if (src_depth <= dst_depth) {
1734 for (i = 0; i < height; i++) {
1735 #define COPY_UP(r,w) \
1736 for (j = 0; j < length; j++){ \
1737 unsigned int v= r(&srcPtr2[j]);\
1738 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
1739 (v>>(2*src_depth-dst_depth)));\
1741 if(isBE(c->srcFormat)){
1742 if(isBE(c->dstFormat)){
1743 COPY_UP(AV_RB16, AV_WB16)
1745 COPY_UP(AV_RB16, AV_WL16)
1748 if(isBE(c->dstFormat)){
1749 COPY_UP(AV_RL16, AV_WB16)
1751 COPY_UP(AV_RL16, AV_WL16)
1754 dstPtr2 += dstStride[plane]/2;
1755 srcPtr2 += srcStride[plane]/2;
1758 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1759 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1760 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
1762 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
1765 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1766 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
1768 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
1772 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1773 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1775 for (i=0; i<height; i++) {
1776 for (j=0; j<length; j++)
1777 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1778 srcPtr+= srcStride[plane];
1779 dstPtr+= dstStride[plane];
1781 } else if (dstStride[plane] == srcStride[plane] &&
1782 srcStride[plane] > 0 && srcStride[plane] == length) {
1783 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1784 height*dstStride[plane]);
1786 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1788 for (i=0; i<height; i++) {
1789 memcpy(dstPtr, srcPtr, length);
1790 srcPtr+= srcStride[plane];
1791 dstPtr+= dstStride[plane];
1799 void ff_get_unscaled_swscale(SwsContext *c)
1801 const enum PixelFormat srcFormat = c->srcFormat;
1802 const enum PixelFormat dstFormat = c->dstFormat;
1803 const int flags = c->flags;
1804 const int dstH = c->dstH;
1807 needsDither= isAnyRGB(dstFormat)
1808 && c->dstFormatBpp < 24
1809 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1812 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1813 c->swScale= planarToNv12Wrapper;
1816 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1817 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1818 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1821 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1822 c->swScale= yvu9ToYv12Wrapper;
1826 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1827 c->swScale= bgr24ToYv12Wrapper;
1829 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1830 if ( isAnyRGB(srcFormat)
1831 && isAnyRGB(dstFormat)
1832 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1833 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1834 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1835 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1836 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1837 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1838 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1839 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1840 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1841 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1842 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
1843 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
1844 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1845 c->swScale= rgbToRgbWrapper;
1847 if ((usePal(srcFormat) && (
1848 dstFormat == PIX_FMT_RGB32 ||
1849 dstFormat == PIX_FMT_RGB32_1 ||
1850 dstFormat == PIX_FMT_RGB24 ||
1851 dstFormat == PIX_FMT_BGR32 ||
1852 dstFormat == PIX_FMT_BGR32_1 ||
1853 dstFormat == PIX_FMT_BGR24)))
1854 c->swScale= palToRgbWrapper;
1856 if (srcFormat == PIX_FMT_YUV422P) {
1857 if (dstFormat == PIX_FMT_YUYV422)
1858 c->swScale= yuv422pToYuy2Wrapper;
1859 else if (dstFormat == PIX_FMT_UYVY422)
1860 c->swScale= yuv422pToUyvyWrapper;
1863 /* LQ converters if -sws 0 or -sws 4*/
1864 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1866 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1867 if (dstFormat == PIX_FMT_YUYV422)
1868 c->swScale= planarToYuy2Wrapper;
1869 else if (dstFormat == PIX_FMT_UYVY422)
1870 c->swScale= planarToUyvyWrapper;
1873 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1874 c->swScale= yuyvToYuv420Wrapper;
1875 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1876 c->swScale= uyvyToYuv420Wrapper;
1877 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1878 c->swScale= yuyvToYuv422Wrapper;
1879 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1880 c->swScale= uyvyToYuv422Wrapper;
1883 if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) &&
1884 !(c->flags & SWS_BITEXACT) &&
1885 srcFormat == PIX_FMT_YUV420P) {
1886 // unscaled YV12 -> packed YUV, we want speed
1887 if (dstFormat == PIX_FMT_YUYV422)
1888 c->swScale= yv12toyuy2_unscaled_altivec;
1889 else if (dstFormat == PIX_FMT_UYVY422)
1890 c->swScale= yv12touyvy_unscaled_altivec;
1895 if ( srcFormat == dstFormat
1896 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1897 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1898 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1899 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1900 || (isGray(dstFormat) && isGray(srcFormat))
1901 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1902 && c->chrDstHSubSample == c->chrSrcHSubSample
1903 && c->chrDstVSubSample == c->chrSrcVSubSample
1904 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1905 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1907 if (isPacked(c->srcFormat))
1908 c->swScale= packedCopyWrapper;
1909 else /* Planar YUV or gray */
1910 c->swScale= planarCopyWrapper;
1913 ff_bfin_get_unscaled_swscale (c);
1917 static void reset_ptr(const uint8_t* src[], int format)
1919 if(!isALPHA(format))
1921 if(!isPlanarYUV(format)) {
1924 if (!usePal(format))
1929 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1930 const int linesizes[4])
1932 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1935 for (i = 0; i < 4; i++) {
1936 int plane = desc->comp[i].plane;
1937 if (!data[plane] || !linesizes[plane])
1945 * swscale wrapper, so we don't need to export the SwsContext.
1946 * Assumes planar YUV to be in YUV order instead of YVU.
1948 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1949 int srcSliceH, uint8_t* const dst[], const int dstStride[])
1952 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1953 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1955 // do not mess up sliceDir if we have a "trailing" 0-size slice
1959 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1960 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1963 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1964 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1968 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1969 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1972 if (c->sliceDir == 0) {
1973 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1976 if (usePal(c->srcFormat)) {
1977 for (i=0; i<256; i++) {
1978 int p, r, g, b, y, u, v, a = 0xff;
1979 if(c->srcFormat == PIX_FMT_PAL8) {
1980 p=((const uint32_t*)(src[1]))[i];
1985 } else if(c->srcFormat == PIX_FMT_RGB8) {
1989 } else if(c->srcFormat == PIX_FMT_BGR8) {
1993 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1997 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
2000 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2005 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2006 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2007 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2008 c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
2010 switch(c->dstFormat) {
2015 c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
2017 case PIX_FMT_BGR32_1:
2021 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
2023 case PIX_FMT_RGB32_1:
2027 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
2034 c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
2039 // copy strides, so they can safely be modified
2040 if (c->sliceDir == 1) {
2041 // slices go from top to bottom
2042 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2043 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2045 reset_ptr(src2, c->srcFormat);
2046 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2048 /* reset slice direction at end of frame */
2049 if (srcSliceY + srcSliceH == c->srcH)
2052 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2054 // slices go from bottom to top => we flip the image internally
2055 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2056 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2058 src2[0] += (srcSliceH-1)*srcStride[0];
2059 if (!usePal(c->srcFormat))
2060 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2061 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2062 src2[3] += (srcSliceH-1)*srcStride[3];
2063 dst2[0] += ( c->dstH -1)*dstStride[0];
2064 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2065 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2066 dst2[3] += ( c->dstH -1)*dstStride[3];
2068 reset_ptr(src2, c->srcFormat);
2069 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2071 /* reset slice direction at end of frame */
2075 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2079 #if LIBSWSCALE_VERSION_MAJOR < 1
2080 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2081 int srcSliceH, uint8_t* dst[], int dstStride[])
2083 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2087 /* Convert the palette to the same packed 32-bit format as the palette */
2088 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2092 for (i=0; i<num_pixels; i++)
2093 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2096 /* Palette format: ABCD -> dst format: ABC */
2097 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2101 for (i=0; i<num_pixels; i++) {
2103 dst[0]= palette[src[i]*4+0];
2104 dst[1]= palette[src[i]*4+1];
2105 dst[2]= palette[src[i]*4+2];