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