rgb2rgb: allow conversion for <15 bpp
[libav.git] / libswscale / swscale_unscaled.c
1 /*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <inttypes.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include <assert.h>
27 #include "swscale.h"
28 #include "swscale_internal.h"
29 #include "rgb2rgb.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/avutil.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/bswap.h"
35 #include "libavutil/pixdesc.h"
36
37 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_1)[8][8] = {
38 { 0, 1, 0, 1, 0, 1, 0, 1,},
39 { 1, 0, 1, 0, 1, 0, 1, 0,},
40 { 0, 1, 0, 1, 0, 1, 0, 1,},
41 { 1, 0, 1, 0, 1, 0, 1, 0,},
42 { 0, 1, 0, 1, 0, 1, 0, 1,},
43 { 1, 0, 1, 0, 1, 0, 1, 0,},
44 { 0, 1, 0, 1, 0, 1, 0, 1,},
45 { 1, 0, 1, 0, 1, 0, 1, 0,},
46 };
47 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_3)[8][8] = {
48 { 1, 2, 1, 2, 1, 2, 1, 2,},
49 { 3, 0, 3, 0, 3, 0, 3, 0,},
50 { 1, 2, 1, 2, 1, 2, 1, 2,},
51 { 3, 0, 3, 0, 3, 0, 3, 0,},
52 { 1, 2, 1, 2, 1, 2, 1, 2,},
53 { 3, 0, 3, 0, 3, 0, 3, 0,},
54 { 1, 2, 1, 2, 1, 2, 1, 2,},
55 { 3, 0, 3, 0, 3, 0, 3, 0,},
56 };
57 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_64)[8][8] = {
58 { 18, 34, 30, 46, 17, 33, 29, 45,},
59 { 50, 2, 62, 14, 49, 1, 61, 13,},
60 { 26, 42, 22, 38, 25, 41, 21, 37,},
61 { 58, 10, 54, 6, 57, 9, 53, 5,},
62 { 16, 32, 28, 44, 19, 35, 31, 47,},
63 { 48, 0, 60, 12, 51, 3, 63, 15,},
64 { 24, 40, 20, 36, 27, 43, 23, 39,},
65 { 56, 8, 52, 4, 59, 11, 55, 7,},
66 };
67 extern const uint8_t dither_8x8_128[8][8];
68 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_256)[8][8] = {
69 { 72, 136, 120, 184, 68, 132, 116, 180,},
70 { 200, 8, 248, 56, 196, 4, 244, 52,},
71 { 104, 168, 88, 152, 100, 164, 84, 148,},
72 { 232, 40, 216, 24, 228, 36, 212, 20,},
73 { 64, 128, 102, 176, 76, 140, 124, 188,},
74 { 192, 0, 240, 48, 204, 12, 252, 60,},
75 { 96, 160, 80, 144, 108, 172, 92, 156,},
76 { 224, 32, 208, 16, 236, 44, 220, 28,},
77 };
78
79 #define RGB2YUV_SHIFT 15
80 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
81 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
82 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
83 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
84 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
85 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
86 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
87 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
88 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
89
90 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
91 uint8_t val)
92 {
93 int i;
94 uint8_t *ptr = plane + stride * y;
95 for (i = 0; i < height; i++) {
96 memset(ptr, val, width);
97 ptr += stride;
98 }
99 }
100
101 static void copyPlane(const uint8_t *src, int srcStride,
102 int srcSliceY, int srcSliceH, int width,
103 uint8_t *dst, int dstStride)
104 {
105 dst += dstStride * srcSliceY;
106 if (dstStride == srcStride && srcStride > 0) {
107 memcpy(dst, src, srcSliceH * dstStride);
108 } else {
109 int i;
110 for (i = 0; i < srcSliceH; i++) {
111 memcpy(dst, src, width);
112 src += srcStride;
113 dst += dstStride;
114 }
115 }
116 }
117
118 static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
119 int srcStride[], int srcSliceY,
120 int srcSliceH, uint8_t *dstParam[],
121 int dstStride[])
122 {
123 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
124
125 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
126 dstParam[0], dstStride[0]);
127
128 if (c->dstFormat == PIX_FMT_NV12)
129 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
130 srcStride[1], srcStride[2], dstStride[0]);
131 else
132 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
133 srcStride[2], srcStride[1], dstStride[0]);
134
135 return srcSliceH;
136 }
137
138 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
139 int srcStride[], int srcSliceY, int srcSliceH,
140 uint8_t *dstParam[], int dstStride[])
141 {
142 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
143
144 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
145 srcStride[1], dstStride[0]);
146
147 return srcSliceH;
148 }
149
150 static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
151 int srcStride[], int srcSliceY, int srcSliceH,
152 uint8_t *dstParam[], int dstStride[])
153 {
154 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
155
156 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
157 srcStride[1], dstStride[0]);
158
159 return srcSliceH;
160 }
161
162 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
163 int srcStride[], int srcSliceY, int srcSliceH,
164 uint8_t *dstParam[], int dstStride[])
165 {
166 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
167
168 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
169 srcStride[1], dstStride[0]);
170
171 return srcSliceH;
172 }
173
174 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
175 int srcStride[], int srcSliceY, int srcSliceH,
176 uint8_t *dstParam[], int dstStride[])
177 {
178 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
179
180 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
181 srcStride[1], dstStride[0]);
182
183 return srcSliceH;
184 }
185
186 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
187 int srcStride[], int srcSliceY, int srcSliceH,
188 uint8_t *dstParam[], int dstStride[])
189 {
190 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
191 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
192 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
193
194 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
195 dstStride[1], srcStride[0]);
196
197 if (dstParam[3])
198 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
199
200 return srcSliceH;
201 }
202
203 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
204 int srcStride[], int srcSliceY, int srcSliceH,
205 uint8_t *dstParam[], int dstStride[])
206 {
207 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
208 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
209 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
210
211 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
212 dstStride[1], srcStride[0]);
213
214 return srcSliceH;
215 }
216
217 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
218 int srcStride[], int srcSliceY, int srcSliceH,
219 uint8_t *dstParam[], int dstStride[])
220 {
221 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
222 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
223 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
224
225 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
226 dstStride[1], srcStride[0]);
227
228 if (dstParam[3])
229 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
230
231 return srcSliceH;
232 }
233
234 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
235 int srcStride[], int srcSliceY, int srcSliceH,
236 uint8_t *dstParam[], int dstStride[])
237 {
238 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
239 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
240 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
241
242 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
243 dstStride[1], srcStride[0]);
244
245 return srcSliceH;
246 }
247
248 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
249 const uint8_t *palette)
250 {
251 int i;
252 for (i = 0; i < num_pixels; i++)
253 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
254 }
255
256 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
257 const uint8_t *palette)
258 {
259 int i;
260
261 for (i = 0; i < num_pixels; i++)
262 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
263 }
264
265 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
266 const uint8_t *palette)
267 {
268 int i;
269
270 for (i = 0; i < num_pixels; i++) {
271 //FIXME slow?
272 dst[0] = palette[src[i << 1] * 4 + 0];
273 dst[1] = palette[src[i << 1] * 4 + 1];
274 dst[2] = palette[src[i << 1] * 4 + 2];
275 dst += 3;
276 }
277 }
278
279 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
280 int srcStride[], int srcSliceY, int srcSliceH,
281 uint8_t *dst[], int dstStride[])
282 {
283 int i, j;
284 int srcstr = srcStride[0] >> 1;
285 int dststr = dstStride[0] >> 1;
286 uint16_t *dstPtr = (uint16_t *) dst[0];
287 const uint16_t *srcPtr = (const uint16_t *) src[0];
288 int min_stride = FFMIN(srcstr, dststr);
289
290 for (i = 0; i < srcSliceH; i++) {
291 for (j = 0; j < min_stride; j++) {
292 dstPtr[j] = av_bswap16(srcPtr[j]);
293 }
294 srcPtr += srcstr;
295 dstPtr += dststr;
296 }
297
298 return srcSliceH;
299 }
300
301 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
302 int srcSliceY, int srcSliceH, uint8_t *dst[],
303 int dstStride[])
304 {
305 const enum PixelFormat srcFormat = c->srcFormat;
306 const enum PixelFormat dstFormat = c->dstFormat;
307 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
308 const uint8_t *palette) = NULL;
309 int i;
310 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
311 const uint8_t *srcPtr = src[0];
312
313 if (srcFormat == PIX_FMT_Y400A) {
314 switch (dstFormat) {
315 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
316 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
317 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
318 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
319 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
320 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
321 }
322 } else if (usePal(srcFormat)) {
323 switch (dstFormat) {
324 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
325 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
326 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
327 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
328 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
329 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
330 }
331 }
332
333 if (!conv)
334 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
335 sws_format_name(srcFormat), sws_format_name(dstFormat));
336 else {
337 for (i = 0; i < srcSliceH; i++) {
338 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
339 srcPtr += srcStride[0];
340 dstPtr += dstStride[0];
341 }
342 }
343
344 return srcSliceH;
345 }
346
347 #define isRGBA32(x) ( \
348 (x) == PIX_FMT_ARGB \
349 || (x) == PIX_FMT_RGBA \
350 || (x) == PIX_FMT_BGRA \
351 || (x) == PIX_FMT_ABGR \
352 )
353
354 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
355 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
356 int srcSliceY, int srcSliceH, uint8_t *dst[],
357 int dstStride[])
358 {
359 const enum PixelFormat srcFormat = c->srcFormat;
360 const enum PixelFormat dstFormat = c->dstFormat;
361 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
362 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
363 const int srcId = c->srcFormatBpp;
364 const int dstId = c->dstFormatBpp;
365 void (*conv)(const uint8_t *src, uint8_t *dst, int src_size) = NULL;
366
367 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
368
369 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
370 if ( CONV_IS(ABGR, RGBA)
371 || CONV_IS(ARGB, BGRA)
372 || CONV_IS(BGRA, ARGB)
373 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
374 else if (CONV_IS(ABGR, ARGB)
375 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
376 else if (CONV_IS(ABGR, BGRA)
377 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
378 else if (CONV_IS(BGRA, RGBA)
379 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
380 else if (CONV_IS(BGRA, ABGR)
381 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
382 } else
383 /* BGR -> BGR */
384 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
385 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
386 switch (srcId | (dstId << 16)) {
387 case 0x000F0010: conv = rgb16to15; break;
388 case 0x000F0018: conv = rgb24to15; break;
389 case 0x000F0020: conv = rgb32to15; break;
390 case 0x0010000F: conv = rgb15to16; break;
391 case 0x00100018: conv = rgb24to16; break;
392 case 0x00100020: conv = rgb32to16; break;
393 case 0x0018000F: conv = rgb15to24; break;
394 case 0x00180010: conv = rgb16to24; break;
395 case 0x00180020: conv = rgb32to24; break;
396 case 0x0020000F: conv = rgb15to32; break;
397 case 0x00200010: conv = rgb16to32; break;
398 case 0x00200018: conv = rgb24to32; break;
399 }
400 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
401 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
402 switch (srcId | (dstId << 16)) {
403 case 0x000F000F: conv = rgb15tobgr15; break;
404 case 0x000F0010: conv = rgb16tobgr15; break;
405 case 0x000F0018: conv = rgb24tobgr15; break;
406 case 0x000F0020: conv = rgb32tobgr15; break;
407 case 0x0010000F: conv = rgb15tobgr16; break;
408 case 0x00100010: conv = rgb16tobgr16; break;
409 case 0x00100018: conv = rgb24tobgr16; break;
410 case 0x00100020: conv = rgb32tobgr16; break;
411 case 0x0018000F: conv = rgb15tobgr24; break;
412 case 0x00180010: conv = rgb16tobgr24; break;
413 case 0x00180018: conv = rgb24tobgr24; break;
414 case 0x00180020: conv = rgb32tobgr24; break;
415 case 0x0020000F: conv = rgb15tobgr32; break;
416 case 0x00200010: conv = rgb16tobgr32; break;
417 case 0x00200018: conv = rgb24tobgr32; break;
418 }
419 }
420
421 if (!conv) {
422 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
423 sws_format_name(srcFormat), sws_format_name(dstFormat));
424 } else {
425 const uint8_t *srcPtr = src[0];
426 uint8_t *dstPtr = dst[0];
427 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) &&
428 !isRGBA32(dstFormat))
429 srcPtr += ALT32_CORR;
430
431 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) &&
432 !isRGBA32(srcFormat))
433 dstPtr += ALT32_CORR;
434
435 if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
436 !(srcStride[0] % srcBpp))
437 conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
438 srcSliceH * srcStride[0]);
439 else {
440 int i;
441 dstPtr += dstStride[0] * srcSliceY;
442
443 for (i = 0; i < srcSliceH; i++) {
444 conv(srcPtr, dstPtr, c->srcW * srcBpp);
445 srcPtr += srcStride[0];
446 dstPtr += dstStride[0];
447 }
448 }
449 }
450 return srcSliceH;
451 }
452
453 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
454 int srcStride[], int srcSliceY, int srcSliceH,
455 uint8_t *dst[], int dstStride[])
456 {
457 rgb24toyv12(
458 src[0],
459 dst[0] + srcSliceY * dstStride[0],
460 dst[1] + (srcSliceY >> 1) * dstStride[1],
461 dst[2] + (srcSliceY >> 1) * dstStride[2],
462 c->srcW, srcSliceH,
463 dstStride[0], dstStride[1], srcStride[0]);
464 if (dst[3])
465 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
466 return srcSliceH;
467 }
468
469 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
470 int srcStride[], int srcSliceY, int srcSliceH,
471 uint8_t *dst[], int dstStride[])
472 {
473 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
474 dst[0], dstStride[0]);
475
476 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
477 srcSliceH >> 2, srcStride[1], dstStride[1]);
478 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
479 srcSliceH >> 2, srcStride[2], dstStride[2]);
480 if (dst[3])
481 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
482 return srcSliceH;
483 }
484
485 /* unscaled copy like stuff (assumes nearly identical formats) */
486 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
487 int srcStride[], int srcSliceY, int srcSliceH,
488 uint8_t *dst[], int dstStride[])
489 {
490 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
491 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
492 else {
493 int i;
494 const uint8_t *srcPtr = src[0];
495 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
496 int length = 0;
497
498 /* universal length finder */
499 while (length + c->srcW <= FFABS(dstStride[0]) &&
500 length + c->srcW <= FFABS(srcStride[0]))
501 length += c->srcW;
502 assert(length != 0);
503
504 for (i = 0; i < srcSliceH; i++) {
505 memcpy(dstPtr, srcPtr, length);
506 srcPtr += srcStride[0];
507 dstPtr += dstStride[0];
508 }
509 }
510 return srcSliceH;
511 }
512
513 #define clip9(x) av_clip_uintp2(x, 9)
514 #define clip10(x) av_clip_uintp2(x, 10)
515 #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift, clip) \
516 for (i = 0; i < height; i++) { \
517 const uint8_t *dither = dithers[i & 7]; \
518 for (j = 0; j < length - 7; j += 8) { \
519 wfunc(&dst[j + 0], clip((rfunc(&src[j + 0]) + dither[0]) >> shift)); \
520 wfunc(&dst[j + 1], clip((rfunc(&src[j + 1]) + dither[1]) >> shift)); \
521 wfunc(&dst[j + 2], clip((rfunc(&src[j + 2]) + dither[2]) >> shift)); \
522 wfunc(&dst[j + 3], clip((rfunc(&src[j + 3]) + dither[3]) >> shift)); \
523 wfunc(&dst[j + 4], clip((rfunc(&src[j + 4]) + dither[4]) >> shift)); \
524 wfunc(&dst[j + 5], clip((rfunc(&src[j + 5]) + dither[5]) >> shift)); \
525 wfunc(&dst[j + 6], clip((rfunc(&src[j + 6]) + dither[6]) >> shift)); \
526 wfunc(&dst[j + 7], clip((rfunc(&src[j + 7]) + dither[7]) >> shift)); \
527 } \
528 for (; j < length; j++) \
529 wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
530 dst += dstStride; \
531 src += srcStride; \
532 }
533
534 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
535 int srcStride[], int srcSliceY, int srcSliceH,
536 uint8_t *dst[], int dstStride[])
537 {
538 int plane, i, j;
539 for (plane = 0; plane < 4; plane++) {
540 int length = (plane == 0 || plane == 3) ? c->srcW : -((-c->srcW ) >> c->chrDstHSubSample);
541 int y = (plane == 0 || plane == 3) ? srcSliceY: -((-srcSliceY) >> c->chrDstVSubSample);
542 int height = (plane == 0 || plane == 3) ? srcSliceH: -((-srcSliceH) >> c->chrDstVSubSample);
543 const uint8_t *srcPtr = src[plane];
544 uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
545
546 if (!dst[plane])
547 continue;
548 // ignore palette for GRAY8
549 if (plane == 1 && !dst[2]) continue;
550 if (!src[plane] || (plane == 1 && !src[2])) {
551 if (is16BPS(c->dstFormat))
552 length *= 2;
553 fillPlane(dst[plane], dstStride[plane], length, height, y,
554 (plane == 3) ? 255 : 128);
555 } else {
556 if (is9_OR_10BPS(c->srcFormat)) {
557 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1 + 1;
558 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1 + 1;
559 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
560
561 if (is16BPS(c->dstFormat)) {
562 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
563 #define COPY9_OR_10TO16(rfunc, wfunc) \
564 for (i = 0; i < height; i++) { \
565 for (j = 0; j < length; j++) { \
566 int srcpx = rfunc(&srcPtr2[j]); \
567 wfunc(&dstPtr2[j], (srcpx << (16 - src_depth)) | (srcpx >> (2 * src_depth - 16))); \
568 } \
569 dstPtr2 += dstStride[plane] / 2; \
570 srcPtr2 += srcStride[plane] / 2; \
571 }
572 if (isBE(c->dstFormat)) {
573 if (isBE(c->srcFormat)) {
574 COPY9_OR_10TO16(AV_RB16, AV_WB16);
575 } else {
576 COPY9_OR_10TO16(AV_RL16, AV_WB16);
577 }
578 } else {
579 if (isBE(c->srcFormat)) {
580 COPY9_OR_10TO16(AV_RB16, AV_WL16);
581 } else {
582 COPY9_OR_10TO16(AV_RL16, AV_WL16);
583 }
584 }
585 } else if (is9_OR_10BPS(c->dstFormat)) {
586 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
587 #define COPY9_OR_10TO9_OR_10(loop) \
588 for (i = 0; i < height; i++) { \
589 for (j = 0; j < length; j++) { \
590 loop; \
591 } \
592 dstPtr2 += dstStride[plane] / 2; \
593 srcPtr2 += srcStride[plane] / 2; \
594 }
595 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
596 if (dst_depth > src_depth) { \
597 COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
598 wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
599 } else if (dst_depth < src_depth) { \
600 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
601 srcPtr2, srcStride[plane] / 2, rfunc, \
602 dither_8x8_1, 1, clip9); \
603 } else { \
604 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
605 }
606 if (isBE(c->dstFormat)) {
607 if (isBE(c->srcFormat)) {
608 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
609 } else {
610 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
611 }
612 } else {
613 if (isBE(c->srcFormat)) {
614 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
615 } else {
616 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
617 }
618 }
619 } else {
620 #define W8(a, b) { *(a) = (b); }
621 #define COPY9_OR_10TO8(rfunc) \
622 if (src_depth == 9) { \
623 DITHER_COPY(dstPtr, dstStride[plane], W8, \
624 srcPtr2, srcStride[plane] / 2, rfunc, \
625 dither_8x8_1, 1, av_clip_uint8); \
626 } else { \
627 DITHER_COPY(dstPtr, dstStride[plane], W8, \
628 srcPtr2, srcStride[plane] / 2, rfunc, \
629 dither_8x8_3, 2, av_clip_uint8); \
630 }
631 if (isBE(c->srcFormat)) {
632 COPY9_OR_10TO8(AV_RB16);
633 } else {
634 COPY9_OR_10TO8(AV_RL16);
635 }
636 }
637 } else if (is9_OR_10BPS(c->dstFormat)) {
638 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1 + 1;
639 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
640
641 if (is16BPS(c->srcFormat)) {
642 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
643 #define COPY16TO9_OR_10(rfunc, wfunc) \
644 if (dst_depth == 9) { \
645 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
646 srcPtr2, srcStride[plane] / 2, rfunc, \
647 dither_8x8_128, 7, clip9); \
648 } else { \
649 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
650 srcPtr2, srcStride[plane] / 2, rfunc, \
651 dither_8x8_64, 6, clip10); \
652 }
653 if (isBE(c->dstFormat)) {
654 if (isBE(c->srcFormat)) {
655 COPY16TO9_OR_10(AV_RB16, AV_WB16);
656 } else {
657 COPY16TO9_OR_10(AV_RL16, AV_WB16);
658 }
659 } else {
660 if (isBE(c->srcFormat)) {
661 COPY16TO9_OR_10(AV_RB16, AV_WL16);
662 } else {
663 COPY16TO9_OR_10(AV_RL16, AV_WL16);
664 }
665 }
666 } else /* 8bit */ {
667 #define COPY8TO9_OR_10(wfunc) \
668 for (i = 0; i < height; i++) { \
669 for (j = 0; j < length; j++) { \
670 const int srcpx = srcPtr[j]; \
671 wfunc(&dstPtr2[j], (srcpx << (dst_depth - 8)) | (srcpx >> (16 - dst_depth))); \
672 } \
673 dstPtr2 += dstStride[plane] / 2; \
674 srcPtr += srcStride[plane]; \
675 }
676 if (isBE(c->dstFormat)) {
677 COPY8TO9_OR_10(AV_WB16);
678 } else {
679 COPY8TO9_OR_10(AV_WL16);
680 }
681 }
682 } else if (is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
683 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
684 #define COPY16TO8(rfunc) \
685 DITHER_COPY(dstPtr, dstStride[plane], W8, \
686 srcPtr2, srcStride[plane] / 2, rfunc, \
687 dither_8x8_256, 8, av_clip_uint8);
688 if (isBE(c->srcFormat)) {
689 COPY16TO8(AV_RB16);
690 } else {
691 COPY16TO8(AV_RL16);
692 }
693 } else if (!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
694 for (i = 0; i < height; i++) {
695 for (j = 0; j < length; j++) {
696 dstPtr[ j << 1 ] = srcPtr[j];
697 dstPtr[(j << 1) + 1] = srcPtr[j];
698 }
699 srcPtr += srcStride[plane];
700 dstPtr += dstStride[plane];
701 }
702 } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
703 isBE(c->srcFormat) != isBE(c->dstFormat)) {
704
705 for (i = 0; i < height; i++) {
706 for (j = 0; j < length; j++)
707 ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
708 srcPtr += srcStride[plane];
709 dstPtr += dstStride[plane];
710 }
711 } else if (dstStride[plane] == srcStride[plane] &&
712 srcStride[plane] > 0 && srcStride[plane] == length) {
713 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
714 height * dstStride[plane]);
715 } else {
716 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
717 length *= 2;
718 for (i = 0; i < height; i++) {
719 memcpy(dstPtr, srcPtr, length);
720 srcPtr += srcStride[plane];
721 dstPtr += dstStride[plane];
722 }
723 }
724 }
725 }
726 return srcSliceH;
727 }
728
729
730 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
731 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
732 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
733
734
735 void ff_get_unscaled_swscale(SwsContext *c)
736 {
737 const enum PixelFormat srcFormat = c->srcFormat;
738 const enum PixelFormat dstFormat = c->dstFormat;
739 const int flags = c->flags;
740 const int dstH = c->dstH;
741 int needsDither;
742
743 needsDither = isAnyRGB(dstFormat) &&
744 c->dstFormatBpp < 24 &&
745 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
746
747 /* yv12_to_nv12 */
748 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) &&
749 (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
750 c->swScale = planarToNv12Wrapper;
751 }
752 /* yuv2bgr */
753 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUV422P ||
754 srcFormat == PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
755 !(flags & SWS_ACCURATE_RND) && !(dstH & 1)) {
756 c->swScale = ff_yuv2rgb_get_func_ptr(c);
757 }
758
759 if (srcFormat == PIX_FMT_YUV410P &&
760 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P) &&
761 !(flags & SWS_BITEXACT)) {
762 c->swScale = yvu9ToYv12Wrapper;
763 }
764
765 /* bgr24toYV12 */
766 if (srcFormat == PIX_FMT_BGR24 &&
767 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P) &&
768 !(flags & SWS_ACCURATE_RND))
769 c->swScale = bgr24ToYv12Wrapper;
770
771 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
772 if ( isAnyRGB(srcFormat)
773 && isAnyRGB(dstFormat)
774 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
775 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
776 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
777 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
778 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
779 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
780 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
781 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
782 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
783 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
784 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
785 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
786 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
787 c->swScale= rgbToRgbWrapper;
788
789 /* bswap 16 bits per pixel/component packed formats */
790 if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR444) ||
791 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR48) ||
792 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR555) ||
793 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_BGR565) ||
794 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_GRAY16) ||
795 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB444) ||
796 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB48) ||
797 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB555) ||
798 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, PIX_FMT_RGB565))
799 c->swScale = packed_16bpc_bswap;
800
801 if ((usePal(srcFormat) && (
802 dstFormat == PIX_FMT_RGB32 ||
803 dstFormat == PIX_FMT_RGB32_1 ||
804 dstFormat == PIX_FMT_RGB24 ||
805 dstFormat == PIX_FMT_BGR32 ||
806 dstFormat == PIX_FMT_BGR32_1 ||
807 dstFormat == PIX_FMT_BGR24)))
808 c->swScale = palToRgbWrapper;
809
810 if (srcFormat == PIX_FMT_YUV422P) {
811 if (dstFormat == PIX_FMT_YUYV422)
812 c->swScale = yuv422pToYuy2Wrapper;
813 else if (dstFormat == PIX_FMT_UYVY422)
814 c->swScale = yuv422pToUyvyWrapper;
815 }
816
817 /* LQ converters if -sws 0 or -sws 4*/
818 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
819 /* yv12_to_yuy2 */
820 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
821 if (dstFormat == PIX_FMT_YUYV422)
822 c->swScale = planarToYuy2Wrapper;
823 else if (dstFormat == PIX_FMT_UYVY422)
824 c->swScale = planarToUyvyWrapper;
825 }
826 }
827 if (srcFormat == PIX_FMT_YUYV422 &&
828 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
829 c->swScale = yuyvToYuv420Wrapper;
830 if (srcFormat == PIX_FMT_UYVY422 &&
831 (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
832 c->swScale = uyvyToYuv420Wrapper;
833 if (srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
834 c->swScale = yuyvToYuv422Wrapper;
835 if (srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
836 c->swScale = uyvyToYuv422Wrapper;
837
838 /* simple copy */
839 if ( srcFormat == dstFormat ||
840 (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P) ||
841 (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P) ||
842 (isPlanarYUV(srcFormat) && isGray(dstFormat)) ||
843 (isPlanarYUV(dstFormat) && isGray(srcFormat)) ||
844 (isGray(dstFormat) && isGray(srcFormat)) ||
845 (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
846 c->chrDstHSubSample == c->chrSrcHSubSample &&
847 c->chrDstVSubSample == c->chrSrcVSubSample &&
848 dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21 &&
849 srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
850 {
851 if (isPacked(c->srcFormat))
852 c->swScale = packedCopyWrapper;
853 else /* Planar YUV or gray */
854 c->swScale = planarCopyWrapper;
855 }
856
857 if (ARCH_BFIN)
858 ff_bfin_get_unscaled_swscale(c);
859 if (HAVE_ALTIVEC)
860 ff_swscale_get_unscaled_altivec(c);
861 }
862
863 static void reset_ptr(const uint8_t *src[], int format)
864 {
865 if (!isALPHA(format))
866 src[3] = NULL;
867 if (!isPlanar(format)) {
868 src[3] = src[2] = NULL;
869
870 if (!usePal(format))
871 src[1] = NULL;
872 }
873 }
874
875 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
876 const int linesizes[4])
877 {
878 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
879 int i;
880
881 for (i = 0; i < 4; i++) {
882 int plane = desc->comp[i].plane;
883 if (!data[plane] || !linesizes[plane])
884 return 0;
885 }
886
887 return 1;
888 }
889
890 /**
891 * swscale wrapper, so we don't need to export the SwsContext.
892 * Assumes planar YUV to be in YUV order instead of YVU.
893 */
894 int attribute_align_arg sws_scale(struct SwsContext *c,
895 const uint8_t * const srcSlice[],
896 const int srcStride[], int srcSliceY,
897 int srcSliceH, uint8_t *const dst[],
898 const int dstStride[])
899 {
900 int i;
901 const uint8_t *src2[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
902 uint8_t *dst2[4] = { dst[0], dst[1], dst[2], dst[3] };
903
904 // do not mess up sliceDir if we have a "trailing" 0-size slice
905 if (srcSliceH == 0)
906 return 0;
907
908 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
909 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
910 return 0;
911 }
912 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
913 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
914 return 0;
915 }
916
917 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
918 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
919 return 0;
920 }
921 if (c->sliceDir == 0) {
922 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
923 }
924
925 if (usePal(c->srcFormat)) {
926 for (i = 0; i < 256; i++) {
927 int p, r, g, b, y, u, v;
928 if (c->srcFormat == PIX_FMT_PAL8) {
929 p = ((const uint32_t *)(srcSlice[1]))[i];
930 r = (p >> 16) & 0xFF;
931 g = (p >> 8) & 0xFF;
932 b = p & 0xFF;
933 } else if (c->srcFormat == PIX_FMT_RGB8) {
934 r = ( i >> 5 ) * 36;
935 g = ((i >> 2) & 7) * 36;
936 b = ( i & 3) * 85;
937 } else if (c->srcFormat == PIX_FMT_BGR8) {
938 b = ( i >> 6 ) * 85;
939 g = ((i >> 3) & 7) * 36;
940 r = ( i & 7) * 36;
941 } else if (c->srcFormat == PIX_FMT_RGB4_BYTE) {
942 r = ( i >> 3 ) * 255;
943 g = ((i >> 1) & 3) * 85;
944 b = ( i & 1) * 255;
945 } else if (c->srcFormat == PIX_FMT_GRAY8 ||
946 c->srcFormat == PIX_FMT_Y400A) {
947 r = g = b = i;
948 } else {
949 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
950 b = ( i >> 3 ) * 255;
951 g = ((i >> 1) & 3) * 85;
952 r = ( i & 1) * 255;
953 }
954 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
955 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
956 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
957 c->pal_yuv[i] = y + (u << 8) + (v << 16);
958
959 switch (c->dstFormat) {
960 case PIX_FMT_BGR32:
961 #if !HAVE_BIGENDIAN
962 case PIX_FMT_RGB24:
963 #endif
964 c->pal_rgb[i] = r + (g << 8) + (b << 16);
965 break;
966 case PIX_FMT_BGR32_1:
967 #if HAVE_BIGENDIAN
968 case PIX_FMT_BGR24:
969 #endif
970 c->pal_rgb[i] = (r + (g << 8) + (b << 16)) << 8;
971 break;
972 case PIX_FMT_RGB32_1:
973 #if HAVE_BIGENDIAN
974 case PIX_FMT_RGB24:
975 #endif
976 c->pal_rgb[i] = (b + (g << 8) + (r << 16)) << 8;
977 break;
978 case PIX_FMT_RGB32:
979 #if !HAVE_BIGENDIAN
980 case PIX_FMT_BGR24:
981 #endif
982 default:
983 c->pal_rgb[i] = b + (g << 8) + (r << 16);
984 }
985 }
986 }
987
988 // copy strides, so they can safely be modified
989 if (c->sliceDir == 1) {
990 // slices go from top to bottom
991 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
992 srcStride[3] };
993 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
994 dstStride[3] };
995
996 reset_ptr(src2, c->srcFormat);
997 reset_ptr((const uint8_t **) dst2, c->dstFormat);
998
999 /* reset slice direction at end of frame */
1000 if (srcSliceY + srcSliceH == c->srcH)
1001 c->sliceDir = 0;
1002
1003 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1004 dstStride2);
1005 } else {
1006 // slices go from bottom to top => we flip the image internally
1007 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1008 -srcStride[3] };
1009 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1010 -dstStride[3] };
1011
1012 src2[0] += (srcSliceH - 1) * srcStride[0];
1013 if (!usePal(c->srcFormat))
1014 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1015 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1016 src2[3] += (srcSliceH - 1) * srcStride[3];
1017 dst2[0] += ( c->dstH - 1) * dstStride[0];
1018 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1019 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1020 dst2[3] += ( c->dstH - 1) * dstStride[3];
1021
1022 reset_ptr(src2, c->srcFormat);
1023 reset_ptr((const uint8_t **) dst2, c->dstFormat);
1024
1025 /* reset slice direction at end of frame */
1026 if (!srcSliceY)
1027 c->sliceDir = 0;
1028
1029 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1030 srcSliceH, dst2, dstStride2);
1031 }
1032 }
1033
1034 /* Convert the palette to the same packed 32-bit format as the palette */
1035 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1036 int num_pixels, const uint8_t *palette)
1037 {
1038 int i;
1039
1040 for (i = 0; i < num_pixels; i++)
1041 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1042 }
1043
1044 /* Palette format: ABCD -> dst format: ABC */
1045 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1046 int num_pixels, const uint8_t *palette)
1047 {
1048 int i;
1049
1050 for (i = 0; i < num_pixels; i++) {
1051 //FIXME slow?
1052 dst[0] = palette[src[i] * 4 + 0];
1053 dst[1] = palette[src[i] * 4 + 1];
1054 dst[2] = palette[src[i] * 4 + 2];
1055 dst += 3;
1056 }
1057 }