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/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_GRAY8A \
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
125 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
126 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
127 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
128 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
129 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
130 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
131 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
132 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
134 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
135 0x0103010301030103LL,
136 0x0200020002000200LL,};
138 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
139 0x0602060206020602LL,
140 0x0004000400040004LL,};
142 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
143 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
144 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
145 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
146 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
147 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
149 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
150 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
151 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
154 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
155 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
156 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
158 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
159 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
160 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
161 #endif /* FAST_BGR2YV12 */
162 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
163 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
164 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
166 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
167 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
168 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
169 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
170 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
172 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
173 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
174 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
177 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
179 #endif /* ARCH_X86 */
181 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
182 { 1, 3, 1, 3, 1, 3, 1, 3, },
183 { 2, 0, 2, 0, 2, 0, 2, 0, },
186 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
187 { 6, 2, 6, 2, 6, 2, 6, 2, },
188 { 0, 4, 0, 4, 0, 4, 0, 4, },
191 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
192 { 8, 4, 11, 7, 8, 4, 11, 7, },
193 { 2, 14, 1, 13, 2, 14, 1, 13, },
194 { 10, 6, 9, 5, 10, 6, 9, 5, },
195 { 0, 12, 3, 15, 0, 12, 3, 15, },
198 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
199 { 17, 9, 23, 15, 16, 8, 22, 14, },
200 { 5, 29, 3, 27, 4, 28, 2, 26, },
201 { 21, 13, 19, 11, 20, 12, 18, 10, },
202 { 0, 24, 6, 30, 1, 25, 7, 31, },
203 { 16, 8, 22, 14, 17, 9, 23, 15, },
204 { 4, 28, 2, 26, 5, 29, 3, 27, },
205 { 20, 12, 18, 10, 21, 13, 19, 11, },
206 { 1, 25, 7, 31, 0, 24, 6, 30, },
209 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
210 { 0, 55, 14, 68, 3, 58, 17, 72, },
211 { 37, 18, 50, 32, 40, 22, 54, 35, },
212 { 9, 64, 5, 59, 13, 67, 8, 63, },
213 { 46, 27, 41, 23, 49, 31, 44, 26, },
214 { 2, 57, 16, 71, 1, 56, 15, 70, },
215 { 39, 21, 52, 34, 38, 19, 51, 33, },
216 { 11, 66, 7, 62, 10, 65, 6, 60, },
217 { 48, 30, 43, 25, 47, 29, 42, 24, },
221 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
222 {117, 62, 158, 103, 113, 58, 155, 100, },
223 { 34, 199, 21, 186, 31, 196, 17, 182, },
224 {144, 89, 131, 76, 141, 86, 127, 72, },
225 { 0, 165, 41, 206, 10, 175, 52, 217, },
226 {110, 55, 151, 96, 120, 65, 162, 107, },
227 { 28, 193, 14, 179, 38, 203, 24, 189, },
228 {138, 83, 124, 69, 148, 93, 134, 79, },
229 { 7, 172, 48, 213, 3, 168, 45, 210, },
232 // tries to correct a gamma of 1.5
233 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
234 { 0, 143, 18, 200, 2, 156, 25, 215, },
235 { 78, 28, 125, 64, 89, 36, 138, 74, },
236 { 10, 180, 3, 161, 16, 195, 8, 175, },
237 {109, 51, 93, 38, 121, 60, 105, 47, },
238 { 1, 152, 23, 210, 0, 147, 20, 205, },
239 { 85, 33, 134, 71, 81, 30, 130, 67, },
240 { 14, 190, 6, 171, 12, 185, 5, 166, },
241 {117, 57, 101, 44, 113, 54, 97, 41, },
244 // tries to correct a gamma of 2.0
245 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
246 { 0, 124, 8, 193, 0, 140, 12, 213, },
247 { 55, 14, 104, 42, 66, 19, 119, 52, },
248 { 3, 168, 1, 145, 6, 187, 3, 162, },
249 { 86, 31, 70, 21, 99, 39, 82, 28, },
250 { 0, 134, 11, 206, 0, 129, 9, 200, },
251 { 62, 17, 114, 48, 58, 16, 109, 45, },
252 { 5, 181, 2, 157, 4, 175, 1, 151, },
253 { 95, 36, 78, 26, 90, 34, 74, 24, },
256 // tries to correct a gamma of 2.5
257 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
258 { 0, 107, 3, 187, 0, 125, 6, 212, },
259 { 39, 7, 86, 28, 49, 11, 102, 36, },
260 { 1, 158, 0, 131, 3, 180, 1, 151, },
261 { 68, 19, 52, 12, 81, 25, 64, 17, },
262 { 0, 119, 5, 203, 0, 113, 4, 195, },
263 { 45, 9, 96, 33, 42, 8, 91, 30, },
264 { 2, 172, 1, 144, 2, 165, 0, 137, },
265 { 77, 23, 60, 15, 72, 21, 56, 14, },
269 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
271 { 0, 1, 0, 1, 0, 1, 0, 1,},
272 { 1, 0, 1, 0, 1, 0, 1, 0,},
273 { 0, 1, 0, 1, 0, 1, 0, 1,},
274 { 1, 0, 1, 0, 1, 0, 1, 0,},
275 { 0, 1, 0, 1, 0, 1, 0, 1,},
276 { 1, 0, 1, 0, 1, 0, 1, 0,},
277 { 0, 1, 0, 1, 0, 1, 0, 1,},
278 { 1, 0, 1, 0, 1, 0, 1, 0,},
280 { 1, 2, 1, 2, 1, 2, 1, 2,},
281 { 3, 0, 3, 0, 3, 0, 3, 0,},
282 { 1, 2, 1, 2, 1, 2, 1, 2,},
283 { 3, 0, 3, 0, 3, 0, 3, 0,},
284 { 1, 2, 1, 2, 1, 2, 1, 2,},
285 { 3, 0, 3, 0, 3, 0, 3, 0,},
286 { 1, 2, 1, 2, 1, 2, 1, 2,},
287 { 3, 0, 3, 0, 3, 0, 3, 0,},
289 { 2, 4, 3, 5, 2, 4, 3, 5,},
290 { 6, 0, 7, 1, 6, 0, 7, 1,},
291 { 3, 5, 2, 4, 3, 5, 2, 4,},
292 { 7, 1, 6, 0, 7, 1, 6, 0,},
293 { 2, 4, 3, 5, 2, 4, 3, 5,},
294 { 6, 0, 7, 1, 6, 0, 7, 1,},
295 { 3, 5, 2, 4, 3, 5, 2, 4,},
296 { 7, 1, 6, 0, 7, 1, 6, 0,},
298 { 4, 8, 7, 11, 4, 8, 7, 11,},
299 { 12, 0, 15, 3, 12, 0, 15, 3,},
300 { 6, 10, 5, 9, 6, 10, 5, 9,},
301 { 14, 2, 13, 1, 14, 2, 13, 1,},
302 { 4, 8, 7, 11, 4, 8, 7, 11,},
303 { 12, 0, 15, 3, 12, 0, 15, 3,},
304 { 6, 10, 5, 9, 6, 10, 5, 9,},
305 { 14, 2, 13, 1, 14, 2, 13, 1,},
307 { 9, 17, 15, 23, 8, 16, 14, 22,},
308 { 25, 1, 31, 7, 24, 0, 30, 6,},
309 { 13, 21, 11, 19, 12, 20, 10, 18,},
310 { 29, 5, 27, 3, 28, 4, 26, 2,},
311 { 8, 16, 14, 22, 9, 17, 15, 23,},
312 { 24, 0, 30, 6, 25, 1, 31, 7,},
313 { 12, 20, 10, 18, 13, 21, 11, 19,},
314 { 28, 4, 26, 2, 29, 5, 27, 3,},
316 { 18, 34, 30, 46, 17, 33, 29, 45,},
317 { 50, 2, 62, 14, 49, 1, 61, 13,},
318 { 26, 42, 22, 38, 25, 41, 21, 37,},
319 { 58, 10, 54, 6, 57, 9, 53, 5,},
320 { 16, 32, 28, 44, 19, 35, 31, 47,},
321 { 48, 0, 60, 12, 51, 3, 63, 15,},
322 { 24, 40, 20, 36, 27, 43, 23, 39,},
323 { 56, 8, 52, 4, 59, 11, 55, 7,},
325 { 18, 34, 30, 46, 17, 33, 29, 45,},
326 { 50, 2, 62, 14, 49, 1, 61, 13,},
327 { 26, 42, 22, 38, 25, 41, 21, 37,},
328 { 58, 10, 54, 6, 57, 9, 53, 5,},
329 { 16, 32, 28, 44, 19, 35, 31, 47,},
330 { 48, 0, 60, 12, 51, 3, 63, 15,},
331 { 24, 40, 20, 36, 27, 43, 23, 39,},
332 { 56, 8, 52, 4, 59, 11, 55, 7,},
334 { 36, 68, 60, 92, 34, 66, 58, 90,},
335 { 100, 4,124, 28, 98, 2,122, 26,},
336 { 52, 84, 44, 76, 50, 82, 42, 74,},
337 { 116, 20,108, 12,114, 18,106, 10,},
338 { 32, 64, 56, 88, 38, 70, 62, 94,},
339 { 96, 0,120, 24,102, 6,126, 30,},
340 { 48, 80, 40, 72, 54, 86, 46, 78,},
341 { 112, 16,104, 8,118, 22,110, 14,},
344 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
345 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
346 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
347 int dstW, int chrDstW, int big_endian)
349 //FIXME Optimize (just quickly written not optimized..)
352 for (i = 0; i < dstW; i++) {
356 for (j = 0; j < lumFilterSize; j++)
357 val += lumSrc[j][i] * lumFilter[j];
360 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
362 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
367 for (i = 0; i < chrDstW; i++) {
372 for (j = 0; j < chrFilterSize; j++) {
373 u += chrSrc[j][i ] * chrFilter[j];
374 v += chrSrc[j][i + VOFW] * chrFilter[j];
378 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
379 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
381 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
382 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
387 if (CONFIG_SWSCALE_ALPHA && aDest) {
388 for (i = 0; i < dstW; i++) {
392 for (j = 0; j < lumFilterSize; j++)
393 val += alpSrc[j][i] * lumFilter[j];
396 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
398 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
404 static av_always_inline void yuv2yuvXNinC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
405 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
406 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
407 int dstW, int chrDstW, int big_endian, int depth)
409 //FIXME Optimize (just quickly written not optimized..)
412 for (i = 0; i < dstW; i++) {
413 int val = 1 << (26-depth);
416 for (j = 0; j < lumFilterSize; j++)
417 val += lumSrc[j][i] * lumFilter[j];
420 AV_WB16(&dest[i], av_clip(val >> (27-depth), 0, (1<<depth)-1));
422 AV_WL16(&dest[i], av_clip(val >> (27-depth), 0, (1<<depth)-1));
427 for (i = 0; i < chrDstW; i++) {
428 int u = 1 << (26-depth);
429 int v = 1 << (26-depth);
432 for (j = 0; j < chrFilterSize; j++) {
433 u += chrSrc[j][i ] * chrFilter[j];
434 v += chrSrc[j][i + VOFW] * chrFilter[j];
438 AV_WB16(&uDest[i], av_clip(u >> (27-depth), 0, (1<<depth)-1));
439 AV_WB16(&vDest[i], av_clip(v >> (27-depth), 0, (1<<depth)-1));
441 AV_WL16(&uDest[i], av_clip(u >> (27-depth), 0, (1<<depth)-1));
442 AV_WL16(&vDest[i], av_clip(v >> (27-depth), 0, (1<<depth)-1));
448 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
449 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
450 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
451 enum PixelFormat dstFormat)
453 if (isNBPS(dstFormat)) {
454 const int depth = av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1+1;
455 yuv2yuvXNinC_template(lumFilter, lumSrc, lumFilterSize,
456 chrFilter, chrSrc, chrFilterSize,
458 dest, uDest, vDest, aDest,
459 dstW, chrDstW, isBE(dstFormat), depth);
461 if (isBE(dstFormat)) {
462 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
463 chrFilter, chrSrc, chrFilterSize,
465 dest, uDest, vDest, aDest,
468 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
469 chrFilter, chrSrc, chrFilterSize,
471 dest, uDest, vDest, aDest,
477 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
478 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
479 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
481 //FIXME Optimize (just quickly written not optimized..)
483 for (i=0; i<dstW; i++) {
486 for (j=0; j<lumFilterSize; j++)
487 val += lumSrc[j][i] * lumFilter[j];
489 dest[i]= av_clip_uint8(val>>19);
493 for (i=0; i<chrDstW; i++) {
497 for (j=0; j<chrFilterSize; j++) {
498 u += chrSrc[j][i] * chrFilter[j];
499 v += chrSrc[j][i + VOFW] * chrFilter[j];
502 uDest[i]= av_clip_uint8(u>>19);
503 vDest[i]= av_clip_uint8(v>>19);
506 if (CONFIG_SWSCALE_ALPHA && aDest)
507 for (i=0; i<dstW; i++) {
510 for (j=0; j<lumFilterSize; j++)
511 val += alpSrc[j][i] * lumFilter[j];
513 aDest[i]= av_clip_uint8(val>>19);
518 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
519 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
520 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
522 //FIXME Optimize (just quickly written not optimized..)
524 for (i=0; i<dstW; i++) {
527 for (j=0; j<lumFilterSize; j++)
528 val += lumSrc[j][i] * lumFilter[j];
530 dest[i]= av_clip_uint8(val>>19);
536 if (dstFormat == PIX_FMT_NV12)
537 for (i=0; i<chrDstW; i++) {
541 for (j=0; j<chrFilterSize; j++) {
542 u += chrSrc[j][i] * chrFilter[j];
543 v += chrSrc[j][i + VOFW] * chrFilter[j];
546 uDest[2*i]= av_clip_uint8(u>>19);
547 uDest[2*i+1]= av_clip_uint8(v>>19);
550 for (i=0; i<chrDstW; i++) {
554 for (j=0; j<chrFilterSize; j++) {
555 u += chrSrc[j][i] * chrFilter[j];
556 v += chrSrc[j][i + VOFW] * chrFilter[j];
559 uDest[2*i]= av_clip_uint8(v>>19);
560 uDest[2*i+1]= av_clip_uint8(u>>19);
564 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
565 for (i=0; i<(dstW>>1); i++) {\
571 int av_unused A1, A2;\
572 type av_unused *r, *b, *g;\
575 for (j=0; j<lumFilterSize; j++) {\
576 Y1 += lumSrc[j][i2] * lumFilter[j];\
577 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
579 for (j=0; j<chrFilterSize; j++) {\
580 U += chrSrc[j][i] * chrFilter[j];\
581 V += chrSrc[j][i+VOFW] * chrFilter[j];\
590 for (j=0; j<lumFilterSize; j++) {\
591 A1 += alpSrc[j][i2 ] * lumFilter[j];\
592 A2 += alpSrc[j][i2+1] * lumFilter[j];\
598 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
599 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
600 if ((Y1|Y2|U|V)&256) {\
601 if (Y1>255) Y1=255; \
602 else if (Y1<0)Y1=0; \
603 if (Y2>255) Y2=255; \
604 else if (Y2<0)Y2=0; \
610 if (alpha && ((A1|A2)&256)) {\
611 A1=av_clip_uint8(A1);\
612 A2=av_clip_uint8(A2);\
615 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
616 for (i=0; i<dstW; i++) {\
624 for (j=0; j<lumFilterSize; j++) {\
625 Y += lumSrc[j][i ] * lumFilter[j];\
627 for (j=0; j<chrFilterSize; j++) {\
628 U += chrSrc[j][i ] * chrFilter[j];\
629 V += chrSrc[j][i+VOFW] * chrFilter[j];\
636 for (j=0; j<lumFilterSize; j++)\
637 A += alpSrc[j][i ] * lumFilter[j];\
640 A = av_clip_uint8(A);\
643 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
644 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
645 Y-= c->yuv2rgb_y_offset;\
646 Y*= c->yuv2rgb_y_coeff;\
648 R= Y + V*c->yuv2rgb_v2r_coeff;\
649 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
650 B= Y + U*c->yuv2rgb_u2b_coeff;\
651 if ((R|G|B)&(0xC0000000)) {\
652 if (R>=(256<<22)) R=(256<<22)-1; \
654 if (G>=(256<<22)) G=(256<<22)-1; \
656 if (B>=(256<<22)) B=(256<<22)-1; \
660 #define YSCALE_YUV_2_GRAY16_C \
661 for (i=0; i<(dstW>>1); i++) {\
670 for (j=0; j<lumFilterSize; j++) {\
671 Y1 += lumSrc[j][i2] * lumFilter[j];\
672 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
676 if ((Y1|Y2|U|V)&65536) {\
677 if (Y1>65535) Y1=65535; \
678 else if (Y1<0)Y1=0; \
679 if (Y2>65535) Y2=65535; \
680 else if (Y2<0)Y2=0; \
683 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
684 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
685 r = (type *)c->table_rV[V]; \
686 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
687 b = (type *)c->table_bU[U];
689 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
690 for (i=0; i<(dstW>>1); i++) { \
692 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
693 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
694 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
695 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
696 type av_unused *r, *b, *g; \
697 int av_unused A1, A2; \
699 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
700 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
703 #define YSCALE_YUV_2_GRAY16_2_C \
704 for (i=0; i<(dstW>>1); i++) { \
706 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
707 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
709 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
710 YSCALE_YUV_2_PACKED2_C(type,alpha)\
711 r = (type *)c->table_rV[V];\
712 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
713 b = (type *)c->table_bU[U];
715 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
716 for (i=0; i<(dstW>>1); i++) {\
718 int Y1= buf0[i2 ]>>7;\
719 int Y2= buf0[i2+1]>>7;\
720 int U= (uvbuf1[i ])>>7;\
721 int V= (uvbuf1[i+VOFW])>>7;\
722 type av_unused *r, *b, *g;\
723 int av_unused A1, A2;\
729 #define YSCALE_YUV_2_GRAY16_1_C \
730 for (i=0; i<(dstW>>1); i++) {\
732 int Y1= buf0[i2 ]<<1;\
733 int Y2= buf0[i2+1]<<1;
735 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
736 YSCALE_YUV_2_PACKED1_C(type,alpha)\
737 r = (type *)c->table_rV[V];\
738 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
739 b = (type *)c->table_bU[U];
741 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
742 for (i=0; i<(dstW>>1); i++) {\
744 int Y1= buf0[i2 ]>>7;\
745 int Y2= buf0[i2+1]>>7;\
746 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
747 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
748 type av_unused *r, *b, *g;\
749 int av_unused A1, A2;\
755 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
756 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
757 r = (type *)c->table_rV[V];\
758 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
759 b = (type *)c->table_bU[U];
761 #define YSCALE_YUV_2_MONO2_C \
762 const uint8_t * const d128=dither_8x8_220[y&7];\
763 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
764 for (i=0; i<dstW-7; i+=8) {\
766 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
767 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
768 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
769 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
770 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
771 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
772 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
773 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
774 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
778 #define YSCALE_YUV_2_MONOX_C \
779 const uint8_t * const d128=dither_8x8_220[y&7];\
780 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
782 for (i=0; i<dstW-1; i+=2) {\
787 for (j=0; j<lumFilterSize; j++) {\
788 Y1 += lumSrc[j][i] * lumFilter[j];\
789 Y2 += lumSrc[j][i+1] * lumFilter[j];\
799 acc+= acc + g[Y1+d128[(i+0)&7]];\
800 acc+= acc + g[Y2+d128[(i+1)&7]];\
802 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
807 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
808 switch(c->dstFormat) {\
809 case PIX_FMT_RGB48BE:\
810 case PIX_FMT_RGB48LE:\
812 ((uint8_t*)dest)[ 0]= r[Y1];\
813 ((uint8_t*)dest)[ 1]= r[Y1];\
814 ((uint8_t*)dest)[ 2]= g[Y1];\
815 ((uint8_t*)dest)[ 3]= g[Y1];\
816 ((uint8_t*)dest)[ 4]= b[Y1];\
817 ((uint8_t*)dest)[ 5]= b[Y1];\
818 ((uint8_t*)dest)[ 6]= r[Y2];\
819 ((uint8_t*)dest)[ 7]= r[Y2];\
820 ((uint8_t*)dest)[ 8]= g[Y2];\
821 ((uint8_t*)dest)[ 9]= g[Y2];\
822 ((uint8_t*)dest)[10]= b[Y2];\
823 ((uint8_t*)dest)[11]= b[Y2];\
827 case PIX_FMT_BGR48BE:\
828 case PIX_FMT_BGR48LE:\
830 ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
831 ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
832 ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
833 ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
834 ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
835 ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
842 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
843 func(uint32_t,needAlpha)\
844 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
845 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
848 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
850 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
851 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
855 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
856 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
864 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
865 func(uint32_t,needAlpha)\
866 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
867 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
870 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
872 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
873 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
877 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
878 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
885 ((uint8_t*)dest)[0]= r[Y1];\
886 ((uint8_t*)dest)[1]= g[Y1];\
887 ((uint8_t*)dest)[2]= b[Y1];\
888 ((uint8_t*)dest)[3]= r[Y2];\
889 ((uint8_t*)dest)[4]= g[Y2];\
890 ((uint8_t*)dest)[5]= b[Y2];\
896 ((uint8_t*)dest)[0]= b[Y1];\
897 ((uint8_t*)dest)[1]= g[Y1];\
898 ((uint8_t*)dest)[2]= r[Y1];\
899 ((uint8_t*)dest)[3]= b[Y2];\
900 ((uint8_t*)dest)[4]= g[Y2];\
901 ((uint8_t*)dest)[5]= r[Y2];\
905 case PIX_FMT_RGB565BE:\
906 case PIX_FMT_RGB565LE:\
907 case PIX_FMT_BGR565BE:\
908 case PIX_FMT_BGR565LE:\
910 const int dr1= dither_2x2_8[y&1 ][0];\
911 const int dg1= dither_2x2_4[y&1 ][0];\
912 const int db1= dither_2x2_8[(y&1)^1][0];\
913 const int dr2= dither_2x2_8[y&1 ][1];\
914 const int dg2= dither_2x2_4[y&1 ][1];\
915 const int db2= dither_2x2_8[(y&1)^1][1];\
917 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
918 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
922 case PIX_FMT_RGB555BE:\
923 case PIX_FMT_RGB555LE:\
924 case PIX_FMT_BGR555BE:\
925 case PIX_FMT_BGR555LE:\
927 const int dr1= dither_2x2_8[y&1 ][0];\
928 const int dg1= dither_2x2_8[y&1 ][1];\
929 const int db1= dither_2x2_8[(y&1)^1][0];\
930 const int dr2= dither_2x2_8[y&1 ][1];\
931 const int dg2= dither_2x2_8[y&1 ][0];\
932 const int db2= dither_2x2_8[(y&1)^1][1];\
934 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
935 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
939 case PIX_FMT_RGB444BE:\
940 case PIX_FMT_RGB444LE:\
941 case PIX_FMT_BGR444BE:\
942 case PIX_FMT_BGR444LE:\
944 const int dr1= dither_4x4_16[y&3 ][0];\
945 const int dg1= dither_4x4_16[y&3 ][1];\
946 const int db1= dither_4x4_16[(y&3)^3][0];\
947 const int dr2= dither_4x4_16[y&3 ][1];\
948 const int dg2= dither_4x4_16[y&3 ][0];\
949 const int db2= dither_4x4_16[(y&3)^3][1];\
951 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
952 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
959 const uint8_t * const d64= dither_8x8_73[y&7];\
960 const uint8_t * const d32= dither_8x8_32[y&7];\
962 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
963 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
970 const uint8_t * const d64= dither_8x8_73 [y&7];\
971 const uint8_t * const d128=dither_8x8_220[y&7];\
973 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
974 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
978 case PIX_FMT_RGB4_BYTE:\
979 case PIX_FMT_BGR4_BYTE:\
981 const uint8_t * const d64= dither_8x8_73 [y&7];\
982 const uint8_t * const d128=dither_8x8_220[y&7];\
984 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
985 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
989 case PIX_FMT_MONOBLACK:\
990 case PIX_FMT_MONOWHITE:\
995 case PIX_FMT_YUYV422:\
997 ((uint8_t*)dest)[2*i2+0]= Y1;\
998 ((uint8_t*)dest)[2*i2+1]= U;\
999 ((uint8_t*)dest)[2*i2+2]= Y2;\
1000 ((uint8_t*)dest)[2*i2+3]= V;\
1003 case PIX_FMT_UYVY422:\
1005 ((uint8_t*)dest)[2*i2+0]= U;\
1006 ((uint8_t*)dest)[2*i2+1]= Y1;\
1007 ((uint8_t*)dest)[2*i2+2]= V;\
1008 ((uint8_t*)dest)[2*i2+3]= Y2;\
1011 case PIX_FMT_GRAY16BE:\
1013 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1014 ((uint8_t*)dest)[2*i2+1]= Y1;\
1015 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1016 ((uint8_t*)dest)[2*i2+3]= Y2;\
1019 case PIX_FMT_GRAY16LE:\
1021 ((uint8_t*)dest)[2*i2+0]= Y1;\
1022 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1023 ((uint8_t*)dest)[2*i2+2]= Y2;\
1024 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1029 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1030 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1031 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1034 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)
1037 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1038 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1039 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1042 int step= c->dstFormatBpp/8;
1045 switch(c->dstFormat) {
1053 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1054 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1055 dest[aidx]= needAlpha ? A : 255;
1062 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1063 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1071 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1088 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1089 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1090 dest[aidx]= needAlpha ? A : 255;
1097 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1098 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1106 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1121 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1124 uint8_t *ptr = plane + stride*y;
1125 for (i=0; i<height; i++) {
1126 memset(ptr, val, width);
1131 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
1135 for (i = 0; i < width; i++) {
1140 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1144 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1145 const uint8_t *src1, const uint8_t *src2,
1146 long width, uint32_t *unused)
1150 for (i = 0; i < width; i++) {
1151 int r = src1[6*i + 0];
1152 int g = src1[6*i + 2];
1153 int b = src1[6*i + 4];
1155 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1156 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1160 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1161 const uint8_t *src1, const uint8_t *src2,
1162 long width, uint32_t *unused)
1166 for (i = 0; i < width; i++) {
1167 int r= src1[12*i + 0] + src1[12*i + 6];
1168 int g= src1[12*i + 2] + src1[12*i + 8];
1169 int b= src1[12*i + 4] + src1[12*i + 10];
1171 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1172 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1176 static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
1180 for (i = 0; i < width; i++) {
1185 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1189 static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1190 const uint8_t *src1, const uint8_t *src2,
1191 long width, uint32_t *unused)
1194 for (i = 0; i < width; i++) {
1195 int b = src1[6*i + 0];
1196 int g = src1[6*i + 2];
1197 int r = src1[6*i + 4];
1199 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1200 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1204 static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1205 const uint8_t *src1, const uint8_t *src2,
1206 long width, uint32_t *unused)
1209 for (i = 0; i < width; i++) {
1210 int b= src1[12*i + 0] + src1[12*i + 6];
1211 int g= src1[12*i + 2] + src1[12*i + 8];
1212 int r= src1[12*i + 4] + src1[12*i + 10];
1214 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1215 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1219 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1220 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1223 for (i=0; i<width; i++) {\
1224 int b= (((const type*)src)[i]>>shb)&maskb;\
1225 int g= (((const type*)src)[i]>>shg)&maskg;\
1226 int r= (((const type*)src)[i]>>shr)&maskr;\
1228 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1232 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1233 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1234 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1235 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1236 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1237 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1238 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1239 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1241 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1244 for (i=0; i<width; i++) {
1249 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1250 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1253 for (i=0; i<width; i++) {\
1254 int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1255 int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1256 int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1258 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1259 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1262 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1265 for (i=0; i<width; i++) {\
1266 int pix0= ((const type*)src)[2*i+0]>>shp;\
1267 int pix1= ((const type*)src)[2*i+1]>>shp;\
1268 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1269 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1270 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1271 g&= maskg|(2*maskg);\
1275 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1276 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1280 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1281 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1282 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1283 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1284 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1285 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1286 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1287 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1289 static inline void palToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1292 for (i=0; i<width; i++) {
1295 dst[i]= pal[d] >> 24;
1299 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1302 for (i=0; i<width; i++) {
1305 dst[i]= pal[d] & 0xFF;
1309 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1310 const uint8_t *src1, const uint8_t *src2,
1311 long width, uint32_t *pal)
1314 assert(src1 == src2);
1315 for (i=0; i<width; i++) {
1316 int p= pal[src1[i]];
1323 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1326 for (i=0; i<width/8; i++) {
1329 dst[8*i+j]= ((d>>(7-j))&1)*255;
1333 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1336 for (i=0; i<width/8; i++) {
1339 dst[8*i+j]= ((d>>(7-j))&1)*255;
1343 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1345 #if CONFIG_RUNTIME_CPUDETECT
1346 # define COMPILE_C 1
1348 # define COMPILE_MMX 1
1349 # define COMPILE_MMX2 1
1350 # define COMPILE_3DNOW 1
1352 # define COMPILE_ALTIVEC HAVE_ALTIVEC
1354 #else /* CONFIG_RUNTIME_CPUDETECT */
1357 # define COMPILE_MMX2 1
1358 # elif HAVE_AMD3DNOW
1359 # define COMPILE_3DNOW 1
1361 # define COMPILE_MMX 1
1363 # define COMPILE_C 1
1365 # elif ARCH_PPC && HAVE_ALTIVEC
1366 # define COMPILE_ALTIVEC 1
1368 # define COMPILE_C 1
1373 # define COMPILE_C 0
1376 # define COMPILE_MMX 0
1378 #ifndef COMPILE_MMX2
1379 # define COMPILE_MMX2 0
1381 #ifndef COMPILE_3DNOW
1382 # define COMPILE_3DNOW 0
1384 #ifndef COMPILE_ALTIVEC
1385 # define COMPILE_ALTIVEC 0
1388 #define COMPILE_TEMPLATE_MMX 0
1389 #define COMPILE_TEMPLATE_MMX2 0
1390 #define COMPILE_TEMPLATE_AMD3DNOW 0
1391 #define COMPILE_TEMPLATE_ALTIVEC 0
1394 #define RENAME(a) a ## _C
1395 #include "swscale_template.c"
1400 #undef COMPILE_TEMPLATE_ALTIVEC
1401 #define COMPILE_TEMPLATE_ALTIVEC 1
1402 #define RENAME(a) a ## _altivec
1403 #include "swscale_template.c"
1411 #undef COMPILE_TEMPLATE_MMX
1412 #undef COMPILE_TEMPLATE_MMX2
1413 #undef COMPILE_TEMPLATE_AMD3DNOW
1414 #define COMPILE_TEMPLATE_MMX 1
1415 #define COMPILE_TEMPLATE_MMX2 0
1416 #define COMPILE_TEMPLATE_AMD3DNOW 0
1417 #define RENAME(a) a ## _MMX
1418 #include "swscale_template.c"
1424 #undef COMPILE_TEMPLATE_MMX
1425 #undef COMPILE_TEMPLATE_MMX2
1426 #undef COMPILE_TEMPLATE_AMD3DNOW
1427 #define COMPILE_TEMPLATE_MMX 1
1428 #define COMPILE_TEMPLATE_MMX2 1
1429 #define COMPILE_TEMPLATE_AMD3DNOW 0
1430 #define RENAME(a) a ## _MMX2
1431 #include "swscale_template.c"
1437 #undef COMPILE_TEMPLATE_MMX
1438 #undef COMPILE_TEMPLATE_MMX2
1439 #undef COMPILE_TEMPLATE_AMD3DNOW
1440 #define COMPILE_TEMPLATE_MMX 1
1441 #define COMPILE_TEMPLATE_MMX2 0
1442 #define COMPILE_TEMPLATE_AMD3DNOW 1
1443 #define RENAME(a) a ## _3DNow
1444 #include "swscale_template.c"
1449 SwsFunc ff_getSwsFunc(SwsContext *c)
1451 #if CONFIG_RUNTIME_CPUDETECT
1452 int flags = c->flags;
1455 // ordered per speed fastest first
1456 if (flags & SWS_CPU_CAPS_MMX2) {
1457 sws_init_swScale_MMX2(c);
1458 return swScale_MMX2;
1459 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1460 sws_init_swScale_3DNow(c);
1461 return swScale_3DNow;
1462 } else if (flags & SWS_CPU_CAPS_MMX) {
1463 sws_init_swScale_MMX(c);
1466 sws_init_swScale_C(c);
1472 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1473 sws_init_swScale_altivec(c);
1474 return swScale_altivec;
1476 sws_init_swScale_C(c);
1480 sws_init_swScale_C(c);
1482 #endif /* ARCH_X86 */
1483 #else //CONFIG_RUNTIME_CPUDETECT
1484 #if COMPILE_TEMPLATE_MMX2
1485 sws_init_swScale_MMX2(c);
1486 return swScale_MMX2;
1487 #elif COMPILE_TEMPLATE_AMD3DNOW
1488 sws_init_swScale_3DNow(c);
1489 return swScale_3DNow;
1490 #elif COMPILE_TEMPLATE_MMX
1491 sws_init_swScale_MMX(c);
1493 #elif COMPILE_TEMPLATE_ALTIVEC
1494 sws_init_swScale_altivec(c);
1495 return swScale_altivec;
1497 sws_init_swScale_C(c);
1500 #endif //!CONFIG_RUNTIME_CPUDETECT
1503 static void copyPlane(const uint8_t *src, int srcStride,
1504 int srcSliceY, int srcSliceH, int width,
1505 uint8_t *dst, int dstStride)
1507 dst += dstStride * srcSliceY;
1508 if (dstStride == srcStride && srcStride > 0) {
1509 memcpy(dst, src, srcSliceH * dstStride);
1512 for (i=0; i<srcSliceH; i++) {
1513 memcpy(dst, src, width);
1520 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1521 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1523 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1525 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1526 dstParam[0], dstStride[0]);
1528 if (c->dstFormat == PIX_FMT_NV12)
1529 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1531 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1536 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1537 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1539 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1541 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1546 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1547 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1549 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1551 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1556 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1557 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1559 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1561 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1566 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1567 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1569 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1571 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1576 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1577 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1579 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1580 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1581 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1583 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1586 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1591 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1592 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1594 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1595 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1596 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1598 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1603 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1604 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1606 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1607 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1608 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1610 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1613 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1618 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1619 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1621 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1622 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1623 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1625 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1630 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1633 for (i=0; i<num_pixels; i++)
1634 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1637 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1641 for (i=0; i<num_pixels; i++)
1642 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1645 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1649 for (i=0; i<num_pixels; i++) {
1651 dst[0]= palette[src[i<<1]*4+0];
1652 dst[1]= palette[src[i<<1]*4+1];
1653 dst[2]= palette[src[i<<1]*4+2];
1658 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1659 int srcSliceH, uint8_t* dst[], int dstStride[])
1661 const enum PixelFormat srcFormat= c->srcFormat;
1662 const enum PixelFormat dstFormat= c->dstFormat;
1663 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1664 const uint8_t *palette)=NULL;
1666 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1667 const uint8_t *srcPtr= src[0];
1669 if (srcFormat == PIX_FMT_GRAY8A) {
1670 switch (dstFormat) {
1671 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
1672 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
1673 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1674 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1675 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
1676 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
1678 } else if (usePal(srcFormat)) {
1679 switch (dstFormat) {
1680 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
1681 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
1682 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1683 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1684 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
1685 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
1690 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1691 sws_format_name(srcFormat), sws_format_name(dstFormat));
1693 for (i=0; i<srcSliceH; i++) {
1694 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1695 srcPtr+= srcStride[0];
1696 dstPtr+= dstStride[0];
1703 #define isRGBA32(x) ( \
1704 (x) == PIX_FMT_ARGB \
1705 || (x) == PIX_FMT_RGBA \
1706 || (x) == PIX_FMT_BGRA \
1707 || (x) == PIX_FMT_ABGR \
1710 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1711 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1712 int srcSliceH, uint8_t* dst[], int dstStride[])
1714 const enum PixelFormat srcFormat= c->srcFormat;
1715 const enum PixelFormat dstFormat= c->dstFormat;
1716 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1717 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1718 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1719 const int dstId= c->dstFormatBpp >> 2;
1720 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1722 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1724 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1725 if ( CONV_IS(ABGR, RGBA)
1726 || CONV_IS(ARGB, BGRA)
1727 || CONV_IS(BGRA, ARGB)
1728 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1729 else if (CONV_IS(ABGR, ARGB)
1730 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1731 else if (CONV_IS(ABGR, BGRA)
1732 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1733 else if (CONV_IS(BGRA, RGBA)
1734 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1735 else if (CONV_IS(BGRA, ABGR)
1736 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1739 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1740 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1741 switch(srcId | (dstId<<4)) {
1742 case 0x34: conv= rgb16to15; break;
1743 case 0x36: conv= rgb24to15; break;
1744 case 0x38: conv= rgb32to15; break;
1745 case 0x43: conv= rgb15to16; break;
1746 case 0x46: conv= rgb24to16; break;
1747 case 0x48: conv= rgb32to16; break;
1748 case 0x63: conv= rgb15to24; break;
1749 case 0x64: conv= rgb16to24; break;
1750 case 0x68: conv= rgb32to24; break;
1751 case 0x83: conv= rgb15to32; break;
1752 case 0x84: conv= rgb16to32; break;
1753 case 0x86: conv= rgb24to32; break;
1755 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1756 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1757 switch(srcId | (dstId<<4)) {
1758 case 0x33: conv= rgb15tobgr15; break;
1759 case 0x34: conv= rgb16tobgr15; break;
1760 case 0x36: conv= rgb24tobgr15; break;
1761 case 0x38: conv= rgb32tobgr15; break;
1762 case 0x43: conv= rgb15tobgr16; break;
1763 case 0x44: conv= rgb16tobgr16; break;
1764 case 0x46: conv= rgb24tobgr16; break;
1765 case 0x48: conv= rgb32tobgr16; break;
1766 case 0x63: conv= rgb15tobgr24; break;
1767 case 0x64: conv= rgb16tobgr24; break;
1768 case 0x66: conv= rgb24tobgr24; break;
1769 case 0x68: conv= rgb32tobgr24; break;
1770 case 0x83: conv= rgb15tobgr32; break;
1771 case 0x84: conv= rgb16tobgr32; break;
1772 case 0x86: conv= rgb24tobgr32; break;
1777 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1778 sws_format_name(srcFormat), sws_format_name(dstFormat));
1780 const uint8_t *srcPtr= src[0];
1781 uint8_t *dstPtr= dst[0];
1782 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1783 srcPtr += ALT32_CORR;
1785 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1786 dstPtr += ALT32_CORR;
1788 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
1789 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1792 dstPtr += dstStride[0]*srcSliceY;
1794 for (i=0; i<srcSliceH; i++) {
1795 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1796 srcPtr+= srcStride[0];
1797 dstPtr+= dstStride[0];
1804 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1805 int srcSliceH, uint8_t* dst[], int dstStride[])
1809 dst[0]+ srcSliceY *dstStride[0],
1810 dst[1]+(srcSliceY>>1)*dstStride[1],
1811 dst[2]+(srcSliceY>>1)*dstStride[2],
1813 dstStride[0], dstStride[1], srcStride[0]);
1815 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1819 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1820 int srcSliceH, uint8_t* dst[], int dstStride[])
1822 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1823 dst[0], dstStride[0]);
1825 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1826 srcSliceH >> 2, srcStride[1], dstStride[1]);
1827 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1828 srcSliceH >> 2, srcStride[2], dstStride[2]);
1830 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1834 /* unscaled copy like stuff (assumes nearly identical formats) */
1835 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1836 int srcSliceH, uint8_t* dst[], int dstStride[])
1838 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1839 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1842 const uint8_t *srcPtr= src[0];
1843 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1846 /* universal length finder */
1847 while(length+c->srcW <= FFABS(dstStride[0])
1848 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1851 for (i=0; i<srcSliceH; i++) {
1852 memcpy(dstPtr, srcPtr, length);
1853 srcPtr+= srcStride[0];
1854 dstPtr+= dstStride[0];
1860 #define DITHER_COPY(dst, dstStride, src, srcStride)\
1861 for (i = 0; i < height; i++) {\
1862 uint8_t *dither= dithers[src_depth-9][i&7];\
1863 for (j = 0; j < length-7; j+=8){\
1864 dst[j+0] = (src[j+0] + dither[0])>>(src_depth-dst_depth);\
1865 dst[j+1] = (src[j+1] + dither[1])>>(src_depth-dst_depth);\
1866 dst[j+2] = (src[j+2] + dither[2])>>(src_depth-dst_depth);\
1867 dst[j+3] = (src[j+3] + dither[3])>>(src_depth-dst_depth);\
1868 dst[j+4] = (src[j+4] + dither[4])>>(src_depth-dst_depth);\
1869 dst[j+5] = (src[j+5] + dither[5])>>(src_depth-dst_depth);\
1870 dst[j+6] = (src[j+6] + dither[6])>>(src_depth-dst_depth);\
1871 dst[j+7] = (src[j+7] + dither[7])>>(src_depth-dst_depth);\
1873 for (; j < length; j++)\
1874 dst[j] = (src[j] + dither[j&7])>>(src_depth-dst_depth);\
1880 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1881 int srcSliceH, uint8_t* dst[], int dstStride[])
1884 for (plane=0; plane<4; plane++) {
1885 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1886 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1887 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1888 const uint8_t *srcPtr= src[plane];
1889 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1891 if (!dst[plane]) continue;
1892 // ignore palette for GRAY8
1893 if (plane == 1 && !dst[2]) continue;
1894 if (!src[plane] || (plane == 1 && !src[2])) {
1895 if(is16BPS(c->dstFormat))
1897 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1899 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)) {
1900 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1901 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1902 uint16_t *srcPtr2 = (uint16_t*)srcPtr;
1903 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1905 if (dst_depth == 8) {
1906 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2)
1907 } else if (src_depth == 8) {
1908 for (i = 0; i < height; i++) {
1909 for (j = 0; j < length; j++)
1910 dstPtr2[j] = (srcPtr[j]<<(dst_depth-8)) |
1911 (srcPtr[j]>>(2*8-dst_depth));
1912 dstPtr2 += dstStride[plane]/2;
1913 srcPtr += srcStride[plane];
1915 } else if (src_depth < dst_depth) {
1916 for (i = 0; i < height; i++) {
1917 if(isBE(c->dstFormat)){
1918 for (j = 0; j < length; j++)
1919 AV_WB16(&dstPtr2[j], (srcPtr2[j]<<(dst_depth-src_depth)) |
1920 (srcPtr2[j]>>(2*src_depth-dst_depth)));
1922 for (j = 0; j < length; j++)
1923 AV_WL16(&dstPtr2[j], (srcPtr2[j]<<(dst_depth-src_depth)) |
1924 (srcPtr2[j]>>(2*src_depth-dst_depth)));
1926 dstPtr2 += dstStride[plane]/2;
1927 srcPtr2 += srcStride[plane]/2;
1930 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2)
1932 } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1934 if (!isBE(c->srcFormat)) srcPtr++;
1935 for (i=0; i<height; i++) {
1936 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1937 srcPtr+= srcStride[plane];
1938 dstPtr+= dstStride[plane];
1940 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1941 for (i=0; i<height; i++) {
1942 for (j=0; j<length; j++) {
1943 dstPtr[ j<<1 ] = srcPtr[j];
1944 dstPtr[(j<<1)+1] = srcPtr[j];
1946 srcPtr+= srcStride[plane];
1947 dstPtr+= dstStride[plane];
1949 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1950 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1952 for (i=0; i<height; i++) {
1953 for (j=0; j<length; j++)
1954 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1955 srcPtr+= srcStride[plane];
1956 dstPtr+= dstStride[plane];
1958 } else if (dstStride[plane] == srcStride[plane] &&
1959 srcStride[plane] > 0 && srcStride[plane] == length) {
1960 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1961 height*dstStride[plane]);
1963 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1965 for (i=0; i<height; i++) {
1966 memcpy(dstPtr, srcPtr, length);
1967 srcPtr+= srcStride[plane];
1968 dstPtr+= dstStride[plane];
1976 int ff_hardcodedcpuflags(void)
1979 #if COMPILE_TEMPLATE_MMX2
1980 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1981 #elif COMPILE_TEMPLATE_AMD3DNOW
1982 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1983 #elif COMPILE_TEMPLATE_MMX
1984 flags |= SWS_CPU_CAPS_MMX;
1985 #elif COMPILE_TEMPLATE_ALTIVEC
1986 flags |= SWS_CPU_CAPS_ALTIVEC;
1988 flags |= SWS_CPU_CAPS_BFIN;
1993 void ff_get_unscaled_swscale(SwsContext *c)
1995 const enum PixelFormat srcFormat = c->srcFormat;
1996 const enum PixelFormat dstFormat = c->dstFormat;
1997 const int flags = c->flags;
1998 const int dstH = c->dstH;
2001 needsDither= isAnyRGB(dstFormat)
2002 && c->dstFormatBpp < 24
2003 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
2006 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
2007 c->swScale= planarToNv12Wrapper;
2010 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
2011 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
2012 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2015 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2016 c->swScale= yvu9ToYv12Wrapper;
2020 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2021 c->swScale= bgr24ToYv12Wrapper;
2023 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2024 if ( isAnyRGB(srcFormat)
2025 && isAnyRGB(dstFormat)
2026 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2027 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2028 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2029 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2030 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2031 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2032 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2033 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2034 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2035 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2036 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
2037 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
2038 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2039 c->swScale= rgbToRgbWrapper;
2041 if ((usePal(srcFormat) && (
2042 dstFormat == PIX_FMT_RGB32 ||
2043 dstFormat == PIX_FMT_RGB32_1 ||
2044 dstFormat == PIX_FMT_RGB24 ||
2045 dstFormat == PIX_FMT_BGR32 ||
2046 dstFormat == PIX_FMT_BGR32_1 ||
2047 dstFormat == PIX_FMT_BGR24)))
2048 c->swScale= palToRgbWrapper;
2050 if (srcFormat == PIX_FMT_YUV422P) {
2051 if (dstFormat == PIX_FMT_YUYV422)
2052 c->swScale= yuv422pToYuy2Wrapper;
2053 else if (dstFormat == PIX_FMT_UYVY422)
2054 c->swScale= yuv422pToUyvyWrapper;
2057 /* LQ converters if -sws 0 or -sws 4*/
2058 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2060 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2061 if (dstFormat == PIX_FMT_YUYV422)
2062 c->swScale= planarToYuy2Wrapper;
2063 else if (dstFormat == PIX_FMT_UYVY422)
2064 c->swScale= planarToUyvyWrapper;
2067 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2068 c->swScale= yuyvToYuv420Wrapper;
2069 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2070 c->swScale= uyvyToYuv420Wrapper;
2071 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2072 c->swScale= yuyvToYuv422Wrapper;
2073 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2074 c->swScale= uyvyToYuv422Wrapper;
2077 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2078 !(c->flags & SWS_BITEXACT) &&
2079 srcFormat == PIX_FMT_YUV420P) {
2080 // unscaled YV12 -> packed YUV, we want speed
2081 if (dstFormat == PIX_FMT_YUYV422)
2082 c->swScale= yv12toyuy2_unscaled_altivec;
2083 else if (dstFormat == PIX_FMT_UYVY422)
2084 c->swScale= yv12touyvy_unscaled_altivec;
2089 if ( srcFormat == dstFormat
2090 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2091 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2092 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2093 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2094 || (isGray(dstFormat) && isGray(srcFormat))
2095 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2096 && c->chrDstHSubSample == c->chrSrcHSubSample
2097 && c->chrDstVSubSample == c->chrSrcVSubSample
2098 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2099 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2101 if (isPacked(c->srcFormat))
2102 c->swScale= packedCopyWrapper;
2103 else /* Planar YUV or gray */
2104 c->swScale= planarCopyWrapper;
2107 if (flags & SWS_CPU_CAPS_BFIN)
2108 ff_bfin_get_unscaled_swscale (c);
2112 static void reset_ptr(const uint8_t* src[], int format)
2114 if(!isALPHA(format))
2116 if(!isPlanarYUV(format)) {
2119 if (!usePal(format))
2124 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
2125 const int linesizes[4])
2127 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
2130 for (i = 0; i < 4; i++) {
2131 int plane = desc->comp[i].plane;
2132 if (!data[plane] || !linesizes[plane])
2140 * swscale wrapper, so we don't need to export the SwsContext.
2141 * Assumes planar YUV to be in YUV order instead of YVU.
2143 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
2144 int srcSliceH, uint8_t* const dst[], const int dstStride[])
2147 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2148 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2150 // do not mess up sliceDir if we have a "trailing" 0-size slice
2154 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
2155 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
2158 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
2159 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
2163 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2164 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2167 if (c->sliceDir == 0) {
2168 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2171 if (usePal(c->srcFormat)) {
2172 for (i=0; i<256; i++) {
2173 int p, r, g, b, y, u, v, a = 0xff;
2174 if(c->srcFormat == PIX_FMT_PAL8) {
2175 p=((const uint32_t*)(src[1]))[i];
2180 } else if(c->srcFormat == PIX_FMT_RGB8) {
2184 } else if(c->srcFormat == PIX_FMT_BGR8) {
2188 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
2192 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
2195 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2200 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2201 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2202 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2203 c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
2205 switch(c->dstFormat) {
2210 c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
2212 case PIX_FMT_BGR32_1:
2216 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
2218 case PIX_FMT_RGB32_1:
2222 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
2229 c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
2234 // copy strides, so they can safely be modified
2235 if (c->sliceDir == 1) {
2236 // slices go from top to bottom
2237 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2238 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2240 reset_ptr(src2, c->srcFormat);
2241 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2243 /* reset slice direction at end of frame */
2244 if (srcSliceY + srcSliceH == c->srcH)
2247 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2249 // slices go from bottom to top => we flip the image internally
2250 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2251 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2253 src2[0] += (srcSliceH-1)*srcStride[0];
2254 if (!usePal(c->srcFormat))
2255 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2256 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2257 src2[3] += (srcSliceH-1)*srcStride[3];
2258 dst2[0] += ( c->dstH -1)*dstStride[0];
2259 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2260 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2261 dst2[3] += ( c->dstH -1)*dstStride[3];
2263 reset_ptr(src2, c->srcFormat);
2264 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2266 /* reset slice direction at end of frame */
2270 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2274 #if LIBSWSCALE_VERSION_MAJOR < 1
2275 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2276 int srcSliceH, uint8_t* dst[], int dstStride[])
2278 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2282 /* Convert the palette to the same packed 32-bit format as the palette */
2283 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2287 for (i=0; i<num_pixels; i++)
2288 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2291 /* Palette format: ABCD -> dst format: ABC */
2292 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2296 for (i=0; i<num_pixels; i++) {
2298 dst[0]= palette[src[i]*4+0];
2299 dst[1]= palette[src[i]*4+1];
2300 dst[2]= palette[src[i]*4+2];