2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
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.
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.
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
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_Y400A) {
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 sws_format_name(srcFormat), sws_format_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 sws_format_name(srcFormat), sws_format_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)
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 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
416 int srcSliceH, uint8_t* dst[], int dstStride[])
419 for (plane=0; plane<4; plane++) {
420 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
421 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
422 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
423 const uint8_t *srcPtr= src[plane];
424 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
426 if (!dst[plane]) continue;
427 // ignore palette for GRAY8
428 if (plane == 1 && !dst[2]) continue;
429 if (!src[plane] || (plane == 1 && !src[2])) {
430 if(is16BPS(c->dstFormat))
432 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
434 if(is9_OR_10BPS(c->srcFormat)) {
435 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
436 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
437 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
439 if (is16BPS(c->dstFormat)) {
440 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
441 #define COPY9_OR_10TO16(rfunc, wfunc) \
442 for (i = 0; i < height; i++) { \
443 for (j = 0; j < length; j++) { \
444 int srcpx = rfunc(&srcPtr2[j]); \
445 wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
447 dstPtr2 += dstStride[plane]/2; \
448 srcPtr2 += srcStride[plane]/2; \
450 if (isBE(c->dstFormat)) {
451 if (isBE(c->srcFormat)) {
452 COPY9_OR_10TO16(AV_RB16, AV_WB16);
454 COPY9_OR_10TO16(AV_RL16, AV_WB16);
457 if (isBE(c->srcFormat)) {
458 COPY9_OR_10TO16(AV_RB16, AV_WL16);
460 COPY9_OR_10TO16(AV_RL16, AV_WL16);
463 } else if (is9_OR_10BPS(c->dstFormat)) {
464 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
465 #define COPY9_OR_10TO9_OR_10(loop) \
466 for (i = 0; i < height; i++) { \
467 for (j = 0; j < length; j++) { \
470 dstPtr2 += dstStride[plane]/2; \
471 srcPtr2 += srcStride[plane]/2; \
473 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
474 if (dst_depth > src_depth) { \
475 COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
476 wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
477 } else if (dst_depth < src_depth) { \
478 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
480 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
482 if (isBE(c->dstFormat)) {
483 if (isBE(c->srcFormat)) {
484 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
486 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
489 if (isBE(c->srcFormat)) {
490 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
492 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
496 // FIXME Maybe dither instead.
497 #define COPY9_OR_10TO8(rfunc) \
498 for (i = 0; i < height; i++) { \
499 for (j = 0; j < length; j++) { \
500 dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
502 dstPtr += dstStride[plane]; \
503 srcPtr2 += srcStride[plane]/2; \
505 if (isBE(c->srcFormat)) {
506 COPY9_OR_10TO8(AV_RB16);
508 COPY9_OR_10TO8(AV_RL16);
511 } else if(is9_OR_10BPS(c->dstFormat)) {
512 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
513 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
515 if (is16BPS(c->srcFormat)) {
516 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
517 #define COPY16TO9_OR_10(rfunc, wfunc) \
518 for (i = 0; i < height; i++) { \
519 for (j = 0; j < length; j++) { \
520 wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
522 dstPtr2 += dstStride[plane]/2; \
523 srcPtr2 += srcStride[plane]/2; \
525 if (isBE(c->dstFormat)) {
526 if (isBE(c->srcFormat)) {
527 COPY16TO9_OR_10(AV_RB16, AV_WB16);
529 COPY16TO9_OR_10(AV_RL16, AV_WB16);
532 if (isBE(c->srcFormat)) {
533 COPY16TO9_OR_10(AV_RB16, AV_WL16);
535 COPY16TO9_OR_10(AV_RL16, AV_WL16);
539 #define COPY8TO9_OR_10(wfunc) \
540 for (i = 0; i < height; i++) { \
541 for (j = 0; j < length; j++) { \
542 const int srcpx = srcPtr[j]; \
543 wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
545 dstPtr2 += dstStride[plane]/2; \
546 srcPtr += srcStride[plane]; \
548 if (isBE(c->dstFormat)) {
549 COPY8TO9_OR_10(AV_WB16);
551 COPY8TO9_OR_10(AV_WL16);
554 } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
555 if (!isBE(c->srcFormat)) srcPtr++;
556 for (i=0; i<height; i++) {
557 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
558 srcPtr+= srcStride[plane];
559 dstPtr+= dstStride[plane];
561 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
562 for (i=0; i<height; i++) {
563 for (j=0; j<length; j++) {
564 dstPtr[ j<<1 ] = srcPtr[j];
565 dstPtr[(j<<1)+1] = srcPtr[j];
567 srcPtr+= srcStride[plane];
568 dstPtr+= dstStride[plane];
570 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
571 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
573 for (i=0; i<height; i++) {
574 for (j=0; j<length; j++)
575 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
576 srcPtr+= srcStride[plane];
577 dstPtr+= dstStride[plane];
579 } else if (dstStride[plane] == srcStride[plane] &&
580 srcStride[plane] > 0 && srcStride[plane] == length) {
581 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
582 height*dstStride[plane]);
584 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
586 for (i=0; i<height; i++) {
587 memcpy(dstPtr, srcPtr, length);
588 srcPtr+= srcStride[plane];
589 dstPtr+= dstStride[plane];
597 void ff_get_unscaled_swscale(SwsContext *c)
599 const enum PixelFormat srcFormat = c->srcFormat;
600 const enum PixelFormat dstFormat = c->dstFormat;
601 const int flags = c->flags;
602 const int dstH = c->dstH;
605 needsDither= isAnyRGB(dstFormat)
606 && c->dstFormatBpp < 24
607 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
610 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
611 c->swScale= planarToNv12Wrapper;
614 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
615 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
616 c->swScale= ff_yuv2rgb_get_func_ptr(c);
619 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
620 c->swScale= yvu9ToYv12Wrapper;
624 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
625 c->swScale= bgr24ToYv12Wrapper;
627 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
628 if ( isAnyRGB(srcFormat)
629 && isAnyRGB(dstFormat)
630 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
631 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
632 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
633 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
634 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
635 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
636 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
637 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
638 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
639 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
640 && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
641 && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
642 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
643 c->swScale= rgbToRgbWrapper;
645 if ((usePal(srcFormat) && (
646 dstFormat == PIX_FMT_RGB32 ||
647 dstFormat == PIX_FMT_RGB32_1 ||
648 dstFormat == PIX_FMT_RGB24 ||
649 dstFormat == PIX_FMT_BGR32 ||
650 dstFormat == PIX_FMT_BGR32_1 ||
651 dstFormat == PIX_FMT_BGR24)))
652 c->swScale= palToRgbWrapper;
654 if (srcFormat == PIX_FMT_YUV422P) {
655 if (dstFormat == PIX_FMT_YUYV422)
656 c->swScale= yuv422pToYuy2Wrapper;
657 else if (dstFormat == PIX_FMT_UYVY422)
658 c->swScale= yuv422pToUyvyWrapper;
661 /* LQ converters if -sws 0 or -sws 4*/
662 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
664 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
665 if (dstFormat == PIX_FMT_YUYV422)
666 c->swScale= planarToYuy2Wrapper;
667 else if (dstFormat == PIX_FMT_UYVY422)
668 c->swScale= planarToUyvyWrapper;
671 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
672 c->swScale= yuyvToYuv420Wrapper;
673 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
674 c->swScale= uyvyToYuv420Wrapper;
675 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
676 c->swScale= yuyvToYuv422Wrapper;
677 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
678 c->swScale= uyvyToYuv422Wrapper;
681 if ( srcFormat == dstFormat
682 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
683 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
684 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
685 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
686 || (isGray(dstFormat) && isGray(srcFormat))
687 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
688 && c->chrDstHSubSample == c->chrSrcHSubSample
689 && c->chrDstVSubSample == c->chrSrcVSubSample
690 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
691 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
693 if (isPacked(c->srcFormat))
694 c->swScale= packedCopyWrapper;
695 else /* Planar YUV or gray */
696 c->swScale= planarCopyWrapper;
700 ff_bfin_get_unscaled_swscale(c);
702 ff_swscale_get_unscaled_altivec(c);
705 static void reset_ptr(const uint8_t* src[], int format)
709 if(!isPlanarYUV(format)) {
717 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
718 const int linesizes[4])
720 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
723 for (i = 0; i < 4; i++) {
724 int plane = desc->comp[i].plane;
725 if (!data[plane] || !linesizes[plane])
733 * swscale wrapper, so we don't need to export the SwsContext.
734 * Assumes planar YUV to be in YUV order instead of YVU.
736 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
737 int srcSliceH, uint8_t* const dst[], const int dstStride[])
740 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
741 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
743 // do not mess up sliceDir if we have a "trailing" 0-size slice
747 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
748 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
751 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
752 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
756 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
757 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
760 if (c->sliceDir == 0) {
761 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
764 if (usePal(c->srcFormat)) {
765 for (i=0; i<256; i++) {
766 int p, r, g, b,y,u,v;
767 if(c->srcFormat == PIX_FMT_PAL8) {
768 p=((const uint32_t*)(src[1]))[i];
772 } else if(c->srcFormat == PIX_FMT_RGB8) {
776 } else if(c->srcFormat == PIX_FMT_BGR8) {
780 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
784 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
787 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
792 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
793 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
794 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
795 c->pal_yuv[i]= y + (u<<8) + (v<<16);
797 switch(c->dstFormat) {
802 c->pal_rgb[i]= r + (g<<8) + (b<<16);
804 case PIX_FMT_BGR32_1:
808 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
810 case PIX_FMT_RGB32_1:
814 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
821 c->pal_rgb[i]= b + (g<<8) + (r<<16);
826 // copy strides, so they can safely be modified
827 if (c->sliceDir == 1) {
828 // slices go from top to bottom
829 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
830 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
832 reset_ptr(src2, c->srcFormat);
833 reset_ptr((const uint8_t**)dst2, c->dstFormat);
835 /* reset slice direction at end of frame */
836 if (srcSliceY + srcSliceH == c->srcH)
839 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
841 // slices go from bottom to top => we flip the image internally
842 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
843 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
845 src2[0] += (srcSliceH-1)*srcStride[0];
846 if (!usePal(c->srcFormat))
847 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
848 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
849 src2[3] += (srcSliceH-1)*srcStride[3];
850 dst2[0] += ( c->dstH -1)*dstStride[0];
851 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
852 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
853 dst2[3] += ( c->dstH -1)*dstStride[3];
855 reset_ptr(src2, c->srcFormat);
856 reset_ptr((const uint8_t**)dst2, c->dstFormat);
858 /* reset slice direction at end of frame */
862 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
866 /* Convert the palette to the same packed 32-bit format as the palette */
867 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
871 for (i=0; i<num_pixels; i++)
872 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
875 /* Palette format: ABCD -> dst format: ABC */
876 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
880 for (i=0; i<num_pixels; i++) {
882 dst[0]= palette[src[i]*4+0];
883 dst[1]= palette[src[i]*4+1];
884 dst[2]= palette[src[i]*4+2];