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
28 #include "swscale_internal.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"
37 #define RGB2YUV_SHIFT 15
38 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
39 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
40 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
41 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
42 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
43 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
44 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
45 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
46 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
48 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
51 uint8_t *ptr = plane + stride*y;
52 for (i=0; i<height; i++) {
53 memset(ptr, val, width);
58 static void copyPlane(const uint8_t *src, int srcStride,
59 int srcSliceY, int srcSliceH, int width,
60 uint8_t *dst, int dstStride)
62 dst += dstStride * srcSliceY;
63 if (dstStride == srcStride && srcStride > 0) {
64 memcpy(dst, src, srcSliceH * dstStride);
67 for (i=0; i<srcSliceH; i++) {
68 memcpy(dst, src, width);
75 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
76 int srcSliceH, uint8_t* dstParam[], int dstStride[])
78 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
80 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
81 dstParam[0], dstStride[0]);
83 if (c->dstFormat == PIX_FMT_NV12)
84 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
86 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
91 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
92 int srcSliceH, uint8_t* dstParam[], int dstStride[])
94 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
96 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
101 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
102 int srcSliceH, uint8_t* dstParam[], int dstStride[])
104 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
106 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
111 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
112 int srcSliceH, uint8_t* dstParam[], int dstStride[])
114 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
116 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
121 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
122 int srcSliceH, uint8_t* dstParam[], int dstStride[])
124 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
126 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
131 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
132 int srcSliceH, uint8_t* dstParam[], int dstStride[])
134 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
135 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
136 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
138 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
141 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
146 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
147 int srcSliceH, uint8_t* dstParam[], int dstStride[])
149 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
150 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
151 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
153 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
158 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
159 int srcSliceH, uint8_t* dstParam[], int dstStride[])
161 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
162 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
163 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
165 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
168 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
173 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
174 int srcSliceH, uint8_t* dstParam[], int dstStride[])
176 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
177 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
178 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
180 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
185 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
188 for (i=0; i<num_pixels; i++)
189 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
192 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
196 for (i=0; i<num_pixels; i++)
197 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
200 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
204 for (i=0; i<num_pixels; i++) {
206 dst[0]= palette[src[i<<1]*4+0];
207 dst[1]= palette[src[i<<1]*4+1];
208 dst[2]= palette[src[i<<1]*4+2];
213 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
214 int srcSliceH, uint8_t* dst[], int dstStride[])
216 const enum PixelFormat srcFormat= c->srcFormat;
217 const enum PixelFormat dstFormat= c->dstFormat;
218 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
219 const uint8_t *palette)=NULL;
221 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
222 const uint8_t *srcPtr= src[0];
224 if (srcFormat == PIX_FMT_GRAY8A) {
226 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
227 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
228 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
229 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
230 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
231 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
233 } else if (usePal(srcFormat)) {
235 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
236 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
237 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
238 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
239 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
240 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
245 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
246 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
248 for (i=0; i<srcSliceH; i++) {
249 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
250 srcPtr+= srcStride[0];
251 dstPtr+= dstStride[0];
258 #define isRGBA32(x) ( \
259 (x) == PIX_FMT_ARGB \
260 || (x) == PIX_FMT_RGBA \
261 || (x) == PIX_FMT_BGRA \
262 || (x) == PIX_FMT_ABGR \
265 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
266 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
267 int srcSliceH, uint8_t* dst[], int dstStride[])
269 const enum PixelFormat srcFormat= c->srcFormat;
270 const enum PixelFormat dstFormat= c->dstFormat;
271 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
272 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
273 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
274 const int dstId= c->dstFormatBpp >> 2;
275 void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
277 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
279 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
280 if ( CONV_IS(ABGR, RGBA)
281 || CONV_IS(ARGB, BGRA)
282 || CONV_IS(BGRA, ARGB)
283 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
284 else if (CONV_IS(ABGR, ARGB)
285 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
286 else if (CONV_IS(ABGR, BGRA)
287 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
288 else if (CONV_IS(BGRA, RGBA)
289 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
290 else if (CONV_IS(BGRA, ABGR)
291 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
294 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
295 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
296 switch(srcId | (dstId<<4)) {
297 case 0x34: conv= rgb16to15; break;
298 case 0x36: conv= rgb24to15; break;
299 case 0x38: conv= rgb32to15; break;
300 case 0x43: conv= rgb15to16; break;
301 case 0x46: conv= rgb24to16; break;
302 case 0x48: conv= rgb32to16; break;
303 case 0x63: conv= rgb15to24; break;
304 case 0x64: conv= rgb16to24; break;
305 case 0x68: conv= rgb32to24; break;
306 case 0x83: conv= rgb15to32; break;
307 case 0x84: conv= rgb16to32; break;
308 case 0x86: conv= rgb24to32; break;
310 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
311 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
312 switch(srcId | (dstId<<4)) {
313 case 0x33: conv= rgb15tobgr15; break;
314 case 0x34: conv= rgb16tobgr15; break;
315 case 0x36: conv= rgb24tobgr15; break;
316 case 0x38: conv= rgb32tobgr15; break;
317 case 0x43: conv= rgb15tobgr16; break;
318 case 0x44: conv= rgb16tobgr16; break;
319 case 0x46: conv= rgb24tobgr16; break;
320 case 0x48: conv= rgb32tobgr16; break;
321 case 0x63: conv= rgb15tobgr24; break;
322 case 0x64: conv= rgb16tobgr24; break;
323 case 0x66: conv= rgb24tobgr24; break;
324 case 0x68: conv= rgb32tobgr24; break;
325 case 0x83: conv= rgb15tobgr32; break;
326 case 0x84: conv= rgb16tobgr32; break;
327 case 0x86: conv= rgb24tobgr32; break;
332 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
333 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
335 const uint8_t *srcPtr= src[0];
336 uint8_t *dstPtr= dst[0];
337 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
338 srcPtr += ALT32_CORR;
340 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
341 dstPtr += ALT32_CORR;
343 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
344 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
347 dstPtr += dstStride[0]*srcSliceY;
349 for (i=0; i<srcSliceH; i++) {
350 conv(srcPtr, dstPtr, c->srcW*srcBpp);
351 srcPtr+= srcStride[0];
352 dstPtr+= dstStride[0];
359 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
360 int srcSliceH, uint8_t* dst[], int dstStride[])
364 dst[0]+ srcSliceY *dstStride[0],
365 dst[1]+(srcSliceY>>1)*dstStride[1],
366 dst[2]+(srcSliceY>>1)*dstStride[2],
368 dstStride[0], dstStride[1], srcStride[0]);
370 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
374 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
375 int srcSliceH, uint8_t* dst[], int dstStride[])
377 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
378 dst[0], dstStride[0]);
380 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
381 srcSliceH >> 2, srcStride[1], dstStride[1]);
382 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
383 srcSliceH >> 2, srcStride[2], dstStride[2]);
385 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
389 /* unscaled copy like stuff (assumes nearly identical formats) */
390 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
391 int srcSliceH, uint8_t* dst[], int dstStride[])
393 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
394 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
397 const uint8_t *srcPtr= src[0];
398 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
401 /* universal length finder */
402 while(length+c->srcW <= FFABS(dstStride[0])
403 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
406 for (i=0; i<srcSliceH; i++) {
407 memcpy(dstPtr, srcPtr, length);
408 srcPtr+= srcStride[0];
409 dstPtr+= dstStride[0];
415 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
416 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
417 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
418 for (i = 0; i < height; i++) {\
419 const uint8_t *dither= dithers[src_depth-9][i&7];\
420 for (j = 0; j < length-7; j+=8){\
421 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
422 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
423 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
424 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
425 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
426 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
427 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
428 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
430 for (; j < length; j++)\
431 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
437 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
438 int srcSliceH, uint8_t* dst[], int dstStride[])
441 for (plane=0; plane<4; plane++) {
442 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
443 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
444 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
445 const uint8_t *srcPtr= src[plane];
446 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
448 if (!dst[plane]) continue;
449 // ignore palette for GRAY8
450 if (plane == 1 && !dst[2]) continue;
451 if (!src[plane] || (plane == 1 && !src[2])) {
452 if(is16BPS(c->dstFormat))
454 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
456 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
457 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
459 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
460 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
461 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
462 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
464 if (dst_depth == 8) {
465 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
466 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
468 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
470 } else if (src_depth == 8) {
471 for (i = 0; i < height; i++) {
472 if(isBE(c->dstFormat)){
473 for (j = 0; j < length; j++)
474 AV_WB16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
475 (srcPtr[j]>>(2*8-dst_depth)));
477 for (j = 0; j < length; j++)
478 AV_WL16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
479 (srcPtr[j]>>(2*8-dst_depth)));
481 dstPtr2 += dstStride[plane]/2;
482 srcPtr += srcStride[plane];
484 } else if (src_depth <= dst_depth) {
485 for (i = 0; i < height; i++) {
486 #define COPY_UP(r,w) \
487 for (j = 0; j < length; j++){ \
488 unsigned int v= r(&srcPtr2[j]);\
489 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
490 (v>>(2*src_depth-dst_depth)));\
492 if(isBE(c->srcFormat)){
493 if(isBE(c->dstFormat)){
494 COPY_UP(AV_RB16, AV_WB16)
496 COPY_UP(AV_RB16, AV_WL16)
499 if(isBE(c->dstFormat)){
500 COPY_UP(AV_RL16, AV_WB16)
502 COPY_UP(AV_RL16, AV_WL16)
505 dstPtr2 += dstStride[plane]/2;
506 srcPtr2 += srcStride[plane]/2;
509 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
510 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
511 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
513 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
516 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
517 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
519 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
523 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
524 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
526 for (i=0; i<height; i++) {
527 for (j=0; j<length; j++)
528 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
529 srcPtr+= srcStride[plane];
530 dstPtr+= dstStride[plane];
532 } else if (dstStride[plane] == srcStride[plane] &&
533 srcStride[plane] > 0 && srcStride[plane] == length) {
534 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
535 height*dstStride[plane]);
537 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
539 for (i=0; i<height; i++) {
540 memcpy(dstPtr, srcPtr, length);
541 srcPtr+= srcStride[plane];
542 dstPtr+= dstStride[plane];
550 void ff_get_unscaled_swscale(SwsContext *c)
552 const enum PixelFormat srcFormat = c->srcFormat;
553 const enum PixelFormat dstFormat = c->dstFormat;
554 const int flags = c->flags;
555 const int dstH = c->dstH;
558 needsDither= isAnyRGB(dstFormat)
559 && c->dstFormatBpp < 24
560 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
563 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
564 c->swScale= planarToNv12Wrapper;
567 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
568 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
569 c->swScale= ff_yuv2rgb_get_func_ptr(c);
572 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
573 c->swScale= yvu9ToYv12Wrapper;
577 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
578 c->swScale= bgr24ToYv12Wrapper;
580 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
581 if ( isAnyRGB(srcFormat)
582 && isAnyRGB(dstFormat)
583 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
584 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
585 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
586 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
587 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
588 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
589 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
590 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
591 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
592 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
593 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
594 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
595 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
596 c->swScale= rgbToRgbWrapper;
598 if ((usePal(srcFormat) && (
599 dstFormat == PIX_FMT_RGB32 ||
600 dstFormat == PIX_FMT_RGB32_1 ||
601 dstFormat == PIX_FMT_RGB24 ||
602 dstFormat == PIX_FMT_BGR32 ||
603 dstFormat == PIX_FMT_BGR32_1 ||
604 dstFormat == PIX_FMT_BGR24)))
605 c->swScale= palToRgbWrapper;
607 if (srcFormat == PIX_FMT_YUV422P) {
608 if (dstFormat == PIX_FMT_YUYV422)
609 c->swScale= yuv422pToYuy2Wrapper;
610 else if (dstFormat == PIX_FMT_UYVY422)
611 c->swScale= yuv422pToUyvyWrapper;
614 /* LQ converters if -sws 0 or -sws 4*/
615 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
617 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
618 if (dstFormat == PIX_FMT_YUYV422)
619 c->swScale= planarToYuy2Wrapper;
620 else if (dstFormat == PIX_FMT_UYVY422)
621 c->swScale= planarToUyvyWrapper;
624 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
625 c->swScale= yuyvToYuv420Wrapper;
626 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
627 c->swScale= uyvyToYuv420Wrapper;
628 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
629 c->swScale= yuyvToYuv422Wrapper;
630 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
631 c->swScale= uyvyToYuv422Wrapper;
634 if ( srcFormat == dstFormat
635 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
636 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
637 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
638 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
639 || (isGray(dstFormat) && isGray(srcFormat))
640 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
641 && c->chrDstHSubSample == c->chrSrcHSubSample
642 && c->chrDstVSubSample == c->chrSrcVSubSample
643 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
644 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
646 if (isPacked(c->srcFormat))
647 c->swScale= packedCopyWrapper;
648 else /* Planar YUV or gray */
649 c->swScale= planarCopyWrapper;
653 ff_bfin_get_unscaled_swscale(c);
655 ff_swscale_get_unscaled_altivec(c);
658 static void reset_ptr(const uint8_t* src[], int format)
662 if(!isPlanarYUV(format)) {
670 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
671 const int linesizes[4])
673 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
676 for (i = 0; i < 4; i++) {
677 int plane = desc->comp[i].plane;
678 if (!data[plane] || !linesizes[plane])
686 * swscale wrapper, so we don't need to export the SwsContext.
687 * Assumes planar YUV to be in YUV order instead of YVU.
689 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
690 int srcSliceH, uint8_t* const dst[], const int dstStride[])
693 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
694 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
696 // do not mess up sliceDir if we have a "trailing" 0-size slice
700 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
701 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
704 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
705 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
709 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
710 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
713 if (c->sliceDir == 0) {
714 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
717 if (usePal(c->srcFormat)) {
718 for (i=0; i<256; i++) {
719 int p, r, g, b, y, u, v, a = 0xff;
720 if(c->srcFormat == PIX_FMT_PAL8) {
721 p=((const uint32_t*)(src[1]))[i];
726 } else if(c->srcFormat == PIX_FMT_RGB8) {
730 } else if(c->srcFormat == PIX_FMT_BGR8) {
734 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
738 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
741 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
746 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
747 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
748 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
749 c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
751 switch(c->dstFormat) {
756 c->pal_rgb[i]= r + (g<<8) + (b<<16) + (a<<24);
758 case PIX_FMT_BGR32_1:
762 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
764 case PIX_FMT_RGB32_1:
768 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
775 c->pal_rgb[i]= b + (g<<8) + (r<<16) + (a<<24);
780 // copy strides, so they can safely be modified
781 if (c->sliceDir == 1) {
782 // slices go from top to bottom
783 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
784 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
786 reset_ptr(src2, c->srcFormat);
787 reset_ptr((const uint8_t**)dst2, c->dstFormat);
789 /* reset slice direction at end of frame */
790 if (srcSliceY + srcSliceH == c->srcH)
793 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
795 // slices go from bottom to top => we flip the image internally
796 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
797 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
799 src2[0] += (srcSliceH-1)*srcStride[0];
800 if (!usePal(c->srcFormat))
801 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
802 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
803 src2[3] += (srcSliceH-1)*srcStride[3];
804 dst2[0] += ( c->dstH -1)*dstStride[0];
805 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
806 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
807 dst2[3] += ( c->dstH -1)*dstStride[3];
809 reset_ptr(src2, c->srcFormat);
810 reset_ptr((const uint8_t**)dst2, c->dstFormat);
812 /* reset slice direction at end of frame */
816 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
820 #if LIBSWSCALE_VERSION_MAJOR < 1
821 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
822 int srcSliceH, uint8_t* dst[], int dstStride[])
824 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
828 /* Convert the palette to the same packed 32-bit format as the palette */
829 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
833 for (i=0; i<num_pixels; i++)
834 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
837 /* Palette format: ABCD -> dst format: ABC */
838 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
842 for (i=0; i<num_pixels; i++) {
844 dst[0]= palette[src[i]*4+0];
845 dst[1]= palette[src[i]*4+1];
846 dst[2]= palette[src[i]*4+2];