2 * Copyright (C) 2001-2011 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
27 #include "swscale_internal.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/bswap.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/avassert.h"
37 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
39 { 0, 1, 0, 1, 0, 1, 0, 1,},
40 { 1, 0, 1, 0, 1, 0, 1, 0,},
41 { 0, 1, 0, 1, 0, 1, 0, 1,},
42 { 1, 0, 1, 0, 1, 0, 1, 0,},
43 { 0, 1, 0, 1, 0, 1, 0, 1,},
44 { 1, 0, 1, 0, 1, 0, 1, 0,},
45 { 0, 1, 0, 1, 0, 1, 0, 1,},
46 { 1, 0, 1, 0, 1, 0, 1, 0,},
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,},
57 { 2, 4, 3, 5, 2, 4, 3, 5,},
58 { 6, 0, 7, 1, 6, 0, 7, 1,},
59 { 3, 5, 2, 4, 3, 5, 2, 4,},
60 { 7, 1, 6, 0, 7, 1, 6, 0,},
61 { 2, 4, 3, 5, 2, 4, 3, 5,},
62 { 6, 0, 7, 1, 6, 0, 7, 1,},
63 { 3, 5, 2, 4, 3, 5, 2, 4,},
64 { 7, 1, 6, 0, 7, 1, 6, 0,},
66 { 4, 8, 7, 11, 4, 8, 7, 11,},
67 { 12, 0, 15, 3, 12, 0, 15, 3,},
68 { 6, 10, 5, 9, 6, 10, 5, 9,},
69 { 14, 2, 13, 1, 14, 2, 13, 1,},
70 { 4, 8, 7, 11, 4, 8, 7, 11,},
71 { 12, 0, 15, 3, 12, 0, 15, 3,},
72 { 6, 10, 5, 9, 6, 10, 5, 9,},
73 { 14, 2, 13, 1, 14, 2, 13, 1,},
75 { 9, 17, 15, 23, 8, 16, 14, 22,},
76 { 25, 1, 31, 7, 24, 0, 30, 6,},
77 { 13, 21, 11, 19, 12, 20, 10, 18,},
78 { 29, 5, 27, 3, 28, 4, 26, 2,},
79 { 8, 16, 14, 22, 9, 17, 15, 23,},
80 { 24, 0, 30, 6, 25, 1, 31, 7,},
81 { 12, 20, 10, 18, 13, 21, 11, 19,},
82 { 28, 4, 26, 2, 29, 5, 27, 3,},
84 { 18, 34, 30, 46, 17, 33, 29, 45,},
85 { 50, 2, 62, 14, 49, 1, 61, 13,},
86 { 26, 42, 22, 38, 25, 41, 21, 37,},
87 { 58, 10, 54, 6, 57, 9, 53, 5,},
88 { 16, 32, 28, 44, 19, 35, 31, 47,},
89 { 48, 0, 60, 12, 51, 3, 63, 15,},
90 { 24, 40, 20, 36, 27, 43, 23, 39,},
91 { 56, 8, 52, 4, 59, 11, 55, 7,},
93 { 18, 34, 30, 46, 17, 33, 29, 45,},
94 { 50, 2, 62, 14, 49, 1, 61, 13,},
95 { 26, 42, 22, 38, 25, 41, 21, 37,},
96 { 58, 10, 54, 6, 57, 9, 53, 5,},
97 { 16, 32, 28, 44, 19, 35, 31, 47,},
98 { 48, 0, 60, 12, 51, 3, 63, 15,},
99 { 24, 40, 20, 36, 27, 43, 23, 39,},
100 { 56, 8, 52, 4, 59, 11, 55, 7,},
102 { 36, 68, 60, 92, 34, 66, 58, 90,},
103 { 100, 4,124, 28, 98, 2,122, 26,},
104 { 52, 84, 44, 76, 50, 82, 42, 74,},
105 { 116, 20,108, 12,114, 18,106, 10,},
106 { 32, 64, 56, 88, 38, 70, 62, 94,},
107 { 96, 0,120, 24,102, 6,126, 30,},
108 { 48, 80, 40, 72, 54, 86, 46, 78,},
109 { 112, 16,104, 8,118, 22,110, 14,},
112 const uint16_t dither_scale[15][16]={
113 { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
114 { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
115 { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
116 { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
117 { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
118 { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
119 { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
120 { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
121 { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
122 { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
123 { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
124 { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
125 { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
126 { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
127 { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
131 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
135 uint8_t *ptr = plane + stride * y;
136 for (i = 0; i < height; i++) {
137 memset(ptr, val, width);
142 static void copyPlane(const uint8_t *src, int srcStride,
143 int srcSliceY, int srcSliceH, int width,
144 uint8_t *dst, int dstStride)
146 dst += dstStride * srcSliceY;
147 if (dstStride == srcStride && srcStride > 0) {
148 memcpy(dst, src, srcSliceH * dstStride);
151 for (i = 0; i < srcSliceH; i++) {
152 memcpy(dst, src, width);
159 static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
160 int srcStride[], int srcSliceY,
161 int srcSliceH, uint8_t *dstParam[],
164 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
166 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
167 dstParam[0], dstStride[0]);
169 if (c->dstFormat == AV_PIX_FMT_NV12)
170 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
171 srcStride[1], srcStride[2], dstStride[0]);
173 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
174 srcStride[2], srcStride[1], dstStride[0]);
179 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
180 int srcStride[], int srcSliceY, int srcSliceH,
181 uint8_t *dstParam[], int dstStride[])
183 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
185 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
186 srcStride[1], dstStride[0]);
191 static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
192 int srcStride[], int srcSliceY, int srcSliceH,
193 uint8_t *dstParam[], int dstStride[])
195 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
197 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
198 srcStride[1], dstStride[0]);
203 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
204 int srcStride[], int srcSliceY, int srcSliceH,
205 uint8_t *dstParam[], int dstStride[])
207 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
209 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
210 srcStride[1], dstStride[0]);
215 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
216 int srcStride[], int srcSliceY, int srcSliceH,
217 uint8_t *dstParam[], int dstStride[])
219 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
221 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
222 srcStride[1], dstStride[0]);
227 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
228 int srcStride[], int srcSliceY, int srcSliceH,
229 uint8_t *dstParam[], int dstStride[])
231 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
232 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
233 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
235 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
236 dstStride[1], srcStride[0]);
239 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
244 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
245 int srcStride[], int srcSliceY, int srcSliceH,
246 uint8_t *dstParam[], int dstStride[])
248 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
249 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
250 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
252 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
253 dstStride[1], srcStride[0]);
258 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
259 int srcStride[], int srcSliceY, int srcSliceH,
260 uint8_t *dstParam[], int dstStride[])
262 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
263 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
264 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
266 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
267 dstStride[1], srcStride[0]);
270 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
275 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
276 int srcStride[], int srcSliceY, int srcSliceH,
277 uint8_t *dstParam[], int dstStride[])
279 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
280 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
281 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
283 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
284 dstStride[1], srcStride[0]);
289 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
290 const uint8_t *palette)
293 for (i = 0; i < num_pixels; i++)
294 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
297 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
298 const uint8_t *palette)
302 for (i = 0; i < num_pixels; i++)
303 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
306 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
307 const uint8_t *palette)
311 for (i = 0; i < num_pixels; i++) {
313 dst[0] = palette[src[i << 1] * 4 + 0];
314 dst[1] = palette[src[i << 1] * 4 + 1];
315 dst[2] = palette[src[i << 1] * 4 + 2];
320 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
321 int srcStride[], int srcSliceY, int srcSliceH,
322 uint8_t *dst[], int dstStride[])
325 int srcstr = srcStride[0] >> 1;
326 int dststr = dstStride[0] >> 1;
327 uint16_t *dstPtr = (uint16_t *) dst[0];
328 const uint16_t *srcPtr = (const uint16_t *) src[0];
329 int min_stride = FFMIN(srcstr, dststr);
331 for (i = 0; i < srcSliceH; i++) {
332 for (j = 0; j < min_stride; j++) {
333 dstPtr[j] = av_bswap16(srcPtr[j]);
342 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
343 int srcSliceY, int srcSliceH, uint8_t *dst[],
346 const enum AVPixelFormat srcFormat = c->srcFormat;
347 const enum AVPixelFormat dstFormat = c->dstFormat;
348 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
349 const uint8_t *palette) = NULL;
351 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
352 const uint8_t *srcPtr = src[0];
354 if (srcFormat == AV_PIX_FMT_GRAY8A) {
356 case AV_PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
357 case AV_PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
358 case AV_PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
359 case AV_PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
360 case AV_PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
361 case AV_PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
363 } else if (usePal(srcFormat)) {
365 case AV_PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
366 case AV_PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
367 case AV_PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
368 case AV_PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
369 case AV_PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
370 case AV_PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
375 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
376 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
378 for (i = 0; i < srcSliceH; i++) {
379 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
380 srcPtr += srcStride[0];
381 dstPtr += dstStride[0];
388 static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
389 uint8_t *dst, int dstStride, int srcSliceH,
393 for (h = 0; h < srcSliceH; h++) {
394 uint8_t *dest = dst + dstStride * h;
395 for (x = 0; x < width; x++) {
401 for (i = 0; i < 3; i++)
402 src[i] += srcStride[i];
406 static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
407 uint8_t *dst, int dstStride, int srcSliceH,
408 int alpha_first, int width)
411 for (h = 0; h < srcSliceH; h++) {
412 uint8_t *dest = dst + dstStride * h;
415 for (x = 0; x < width; x++) {
422 for (x = 0; x < width; x++) {
430 for (i = 0; i < 3; i++)
431 src[i] += srcStride[i];
435 static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
436 int srcStride[], int srcSliceY, int srcSliceH,
437 uint8_t *dst[], int dstStride[])
440 const uint8_t *src102[] = { src[1], src[0], src[2] };
441 const uint8_t *src201[] = { src[2], src[0], src[1] };
442 int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
443 int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
445 if (c->srcFormat != AV_PIX_FMT_GBRP) {
446 av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
447 av_get_pix_fmt_name(c->srcFormat),
448 av_get_pix_fmt_name(c->dstFormat));
452 switch (c->dstFormat) {
453 case AV_PIX_FMT_BGR24:
454 gbr24ptopacked24(src102, stride102,
455 dst[0] + srcSliceY * dstStride[0], dstStride[0],
459 case AV_PIX_FMT_RGB24:
460 gbr24ptopacked24(src201, stride201,
461 dst[0] + srcSliceY * dstStride[0], dstStride[0],
465 case AV_PIX_FMT_ARGB:
467 case AV_PIX_FMT_RGBA:
468 gbr24ptopacked32(src201, stride201,
469 dst[0] + srcSliceY * dstStride[0], dstStride[0],
470 srcSliceH, alpha_first, c->srcW);
473 case AV_PIX_FMT_ABGR:
475 case AV_PIX_FMT_BGRA:
476 gbr24ptopacked32(src102, stride102,
477 dst[0] + srcSliceY * dstStride[0], dstStride[0],
478 srcSliceH, alpha_first, c->srcW);
482 av_log(c, AV_LOG_ERROR,
483 "unsupported planar RGB conversion %s -> %s\n",
484 av_get_pix_fmt_name(c->srcFormat),
485 av_get_pix_fmt_name(c->dstFormat));
491 #define isRGBA32(x) ( \
492 (x) == AV_PIX_FMT_ARGB \
493 || (x) == AV_PIX_FMT_RGBA \
494 || (x) == AV_PIX_FMT_BGRA \
495 || (x) == AV_PIX_FMT_ABGR \
498 #define isRGBA64(x) ( \
499 (x) == AV_PIX_FMT_RGBA64LE \
500 || (x) == AV_PIX_FMT_RGBA64BE \
501 || (x) == AV_PIX_FMT_BGRA64LE \
502 || (x) == AV_PIX_FMT_BGRA64BE \
505 #define isRGB48(x) ( \
506 (x) == AV_PIX_FMT_RGB48LE \
507 || (x) == AV_PIX_FMT_RGB48BE \
508 || (x) == AV_PIX_FMT_BGR48LE \
509 || (x) == AV_PIX_FMT_BGR48BE \
512 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
513 typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
514 static rgbConvFn findRgbConvFn(SwsContext *c)
516 const enum AVPixelFormat srcFormat = c->srcFormat;
517 const enum AVPixelFormat dstFormat = c->dstFormat;
518 const int srcId = c->srcFormatBpp;
519 const int dstId = c->dstFormatBpp;
520 rgbConvFn conv = NULL;
522 #define IS_NOT_NE(bpp, desc) \
523 (((bpp + 7) >> 3) == 2 && \
524 (!(desc->flags & PIX_FMT_BE) != !HAVE_BIGENDIAN))
526 #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
528 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
529 if ( CONV_IS(ABGR, RGBA)
530 || CONV_IS(ARGB, BGRA)
531 || CONV_IS(BGRA, ARGB)
532 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
533 else if (CONV_IS(ABGR, ARGB)
534 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
535 else if (CONV_IS(ABGR, BGRA)
536 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
537 else if (CONV_IS(BGRA, RGBA)
538 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
539 else if (CONV_IS(BGRA, ABGR)
540 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
541 } else if (isRGB48(srcFormat) && isRGB48(dstFormat)) {
542 if (CONV_IS(RGB48LE, BGR48LE)
543 || CONV_IS(BGR48LE, RGB48LE)
544 || CONV_IS(RGB48BE, BGR48BE)
545 || CONV_IS(BGR48BE, RGB48BE)) conv = rgb48tobgr48_nobswap;
546 else if (CONV_IS(RGB48LE, BGR48BE)
547 || CONV_IS(BGR48LE, RGB48BE)
548 || CONV_IS(RGB48BE, BGR48LE)
549 || CONV_IS(BGR48BE, RGB48LE)) conv = rgb48tobgr48_bswap;
550 } else if (isRGBA64(srcFormat) && isRGB48(dstFormat)) {
551 if (CONV_IS(RGBA64LE, BGR48LE)
552 || CONV_IS(BGRA64LE, RGB48LE)
553 || CONV_IS(RGBA64BE, BGR48BE)
554 || CONV_IS(BGRA64BE, RGB48BE)) conv = rgb64tobgr48_nobswap;
555 else if (CONV_IS(RGBA64LE, BGR48BE)
556 || CONV_IS(BGRA64LE, RGB48BE)
557 || CONV_IS(RGBA64BE, BGR48LE)
558 || CONV_IS(BGRA64BE, RGB48LE)) conv = rgb64tobgr48_bswap;
559 else if (CONV_IS(RGBA64LE, RGB48LE)
560 || CONV_IS(BGRA64LE, BGR48LE)
561 || CONV_IS(RGBA64BE, RGB48BE)
562 || CONV_IS(BGRA64BE, BGR48BE)) conv = rgb64to48_nobswap;
563 else if (CONV_IS(RGBA64LE, RGB48BE)
564 || CONV_IS(BGRA64LE, BGR48BE)
565 || CONV_IS(RGBA64BE, RGB48LE)
566 || CONV_IS(BGRA64BE, BGR48LE)) conv = rgb64to48_bswap;
569 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
570 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
571 switch (srcId | (dstId << 16)) {
572 case 0x000F000C: conv = rgb12to15; break;
573 case 0x000F0010: conv = rgb16to15; break;
574 case 0x000F0018: conv = rgb24to15; break;
575 case 0x000F0020: conv = rgb32to15; break;
576 case 0x0010000F: conv = rgb15to16; break;
577 case 0x00100018: conv = rgb24to16; break;
578 case 0x00100020: conv = rgb32to16; break;
579 case 0x0018000F: conv = rgb15to24; break;
580 case 0x00180010: conv = rgb16to24; break;
581 case 0x00180020: conv = rgb32to24; break;
582 case 0x0020000F: conv = rgb15to32; break;
583 case 0x00200010: conv = rgb16to32; break;
584 case 0x00200018: conv = rgb24to32; break;
586 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
587 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
588 switch (srcId | (dstId << 16)) {
589 case 0x000C000C: conv = rgb12tobgr12; break;
590 case 0x000F000F: conv = rgb15tobgr15; break;
591 case 0x000F0010: conv = rgb16tobgr15; break;
592 case 0x000F0018: conv = rgb24tobgr15; break;
593 case 0x000F0020: conv = rgb32tobgr15; break;
594 case 0x0010000F: conv = rgb15tobgr16; break;
595 case 0x00100010: conv = rgb16tobgr16; break;
596 case 0x00100018: conv = rgb24tobgr16; break;
597 case 0x00100020: conv = rgb32tobgr16; break;
598 case 0x0018000F: conv = rgb15tobgr24; break;
599 case 0x00180010: conv = rgb16tobgr24; break;
600 case 0x00180018: conv = rgb24tobgr24; break;
601 case 0x00180020: conv = rgb32tobgr24; break;
602 case 0x0020000F: conv = rgb15tobgr32; break;
603 case 0x00200010: conv = rgb16tobgr32; break;
604 case 0x00200018: conv = rgb24tobgr32; break;
608 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) && !isRGBA32(srcFormat) && ALT32_CORR<0)
614 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
615 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
616 int srcSliceY, int srcSliceH, uint8_t *dst[],
620 const enum AVPixelFormat srcFormat = c->srcFormat;
621 const enum AVPixelFormat dstFormat = c->dstFormat;
622 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
623 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
624 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
625 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
626 rgbConvFn conv = findRgbConvFn(c);
629 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
630 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
632 const uint8_t *srcPtr = src[0];
633 uint8_t *dstPtr = dst[0];
634 int src_bswap = IS_NOT_NE(c->srcFormatBpp, desc_src);
635 int dst_bswap = IS_NOT_NE(c->dstFormatBpp, desc_dst);
637 if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
638 !isRGBA32(dstFormat))
639 srcPtr += ALT32_CORR;
641 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
642 !isRGBA32(srcFormat))
643 dstPtr += ALT32_CORR;
645 if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
646 !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap)
647 conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
648 srcSliceH * srcStride[0]);
651 dstPtr += dstStride[0] * srcSliceY;
653 for (i = 0; i < srcSliceH; i++) {
655 for(j=0; j<c->srcW; j++)
656 ((uint16_t*)c->formatConvBuffer)[j] = av_bswap16(((uint16_t*)srcPtr)[j]);
657 conv(c->formatConvBuffer, dstPtr, c->srcW * srcBpp);
659 conv(srcPtr, dstPtr, c->srcW * srcBpp);
661 for(j=0; j<c->srcW; j++)
662 ((uint16_t*)dstPtr)[j] = av_bswap16(((uint16_t*)dstPtr)[j]);
663 srcPtr += srcStride[0];
664 dstPtr += dstStride[0];
671 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
672 int srcStride[], int srcSliceY, int srcSliceH,
673 uint8_t *dst[], int dstStride[])
677 dst[0] + srcSliceY * dstStride[0],
678 dst[1] + (srcSliceY >> 1) * dstStride[1],
679 dst[2] + (srcSliceY >> 1) * dstStride[2],
681 dstStride[0], dstStride[1], srcStride[0]);
683 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
687 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
688 int srcStride[], int srcSliceY, int srcSliceH,
689 uint8_t *dst[], int dstStride[])
691 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
692 dst[0], dstStride[0]);
694 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
695 srcSliceH >> 2, srcStride[1], dstStride[1]);
696 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
697 srcSliceH >> 2, srcStride[2], dstStride[2]);
699 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
703 /* unscaled copy like stuff (assumes nearly identical formats) */
704 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
705 int srcStride[], int srcSliceY, int srcSliceH,
706 uint8_t *dst[], int dstStride[])
708 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
709 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
712 const uint8_t *srcPtr = src[0];
713 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
716 /* universal length finder */
717 while (length + c->srcW <= FFABS(dstStride[0]) &&
718 length + c->srcW <= FFABS(srcStride[0]))
720 av_assert1(length != 0);
722 for (i = 0; i < srcSliceH; i++) {
723 memcpy(dstPtr, srcPtr, length);
724 srcPtr += srcStride[0];
725 dstPtr += dstStride[0];
731 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
732 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
733 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
734 for (i = 0; i < height; i++) {\
735 const uint8_t *dither= dithers[src_depth-9][i&7];\
736 for (j = 0; j < length-7; j+=8){\
737 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
738 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
739 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
740 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
741 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
742 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
743 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
744 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
746 for (; j < length; j++)\
747 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
752 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
753 int srcStride[], int srcSliceY, int srcSliceH,
754 uint8_t *dst[], int dstStride[])
756 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
757 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
759 for (plane = 0; plane < 4; plane++) {
760 int length = (plane == 0 || plane == 3) ? c->srcW : -((-c->srcW ) >> c->chrDstHSubSample);
761 int y = (plane == 0 || plane == 3) ? srcSliceY: -((-srcSliceY) >> c->chrDstVSubSample);
762 int height = (plane == 0 || plane == 3) ? srcSliceH: -((-srcSliceH) >> c->chrDstVSubSample);
763 const uint8_t *srcPtr = src[plane];
764 uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
765 int shiftonly= plane==1 || plane==2 || (!c->srcRange && plane==0);
769 // ignore palette for GRAY8
770 if (plane == 1 && !dst[2]) continue;
771 if (!src[plane] || (plane == 1 && !src[2])) {
772 if (is16BPS(c->dstFormat) || isNBPS(c->dstFormat)) {
773 fillPlane16(dst[plane], dstStride[plane], length, height, y,
774 plane == 3, desc_dst->comp[plane].depth_minus1,
777 fillPlane(dst[plane], dstStride[plane], length, height, y,
778 (plane == 3) ? 255 : 128);
781 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
782 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
784 const int src_depth = desc_src->comp[plane].depth_minus1 + 1;
785 const int dst_depth = desc_dst->comp[plane].depth_minus1 + 1;
786 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
787 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
789 if (dst_depth == 8) {
790 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
791 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
793 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
795 } else if (src_depth == 8) {
796 for (i = 0; i < height; i++) {
799 for (j = 0; j < length; j++)\
800 w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
802 for (j = 0; j < length; j++)\
803 w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
804 (srcPtr[j]>>(2*8-dst_depth)));\
806 if(isBE(c->dstFormat)){
811 dstPtr2 += dstStride[plane]/2;
812 srcPtr += srcStride[plane];
814 } else if (src_depth <= dst_depth) {
815 int orig_length = length;
816 for (i = 0; i < height; i++) {
817 if(isBE(c->srcFormat) == HAVE_BIGENDIAN &&
818 isBE(c->dstFormat) == HAVE_BIGENDIAN &&
820 unsigned shift = dst_depth - src_depth;
821 length = orig_length;
823 #define FAST_COPY_UP(shift) \
824 for (j = 0; j < length - 3; j += 4) { \
825 uint64_t v = AV_RN64A(srcPtr2 + j); \
826 AV_WN64A(dstPtr2 + j, v << shift); \
830 #define FAST_COPY_UP(shift) \
831 for (j = 0; j < length - 1; j += 2) { \
832 uint32_t v = AV_RN32A(srcPtr2 + j); \
833 AV_WN32A(dstPtr2 + j, v << shift); \
839 case 6: FAST_COPY_UP(6); break;
840 case 7: FAST_COPY_UP(7); break;
843 #define COPY_UP(r,w) \
845 for (j = 0; j < length; j++){ \
846 unsigned int v= r(&srcPtr2[j]);\
847 w(&dstPtr2[j], v<<(dst_depth-src_depth));\
850 for (j = 0; j < length; j++){ \
851 unsigned int v= r(&srcPtr2[j]);\
852 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
853 (v>>(2*src_depth-dst_depth)));\
856 if(isBE(c->srcFormat)){
857 if(isBE(c->dstFormat)){
858 COPY_UP(AV_RB16, AV_WB16)
860 COPY_UP(AV_RB16, AV_WL16)
863 if(isBE(c->dstFormat)){
864 COPY_UP(AV_RL16, AV_WB16)
866 COPY_UP(AV_RL16, AV_WL16)
869 dstPtr2 += dstStride[plane]/2;
870 srcPtr2 += srcStride[plane]/2;
873 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
874 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
875 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
877 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
880 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
881 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
883 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
887 } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
888 isBE(c->srcFormat) != isBE(c->dstFormat)) {
890 for (i = 0; i < height; i++) {
891 for (j = 0; j < length; j++)
892 ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
893 srcPtr += srcStride[plane];
894 dstPtr += dstStride[plane];
896 } else if (dstStride[plane] == srcStride[plane] &&
897 srcStride[plane] > 0 && srcStride[plane] == length) {
898 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
899 height * dstStride[plane]);
901 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
903 else if (!desc_src->comp[0].depth_minus1)
904 length >>= 3; // monowhite/black
905 for (i = 0; i < height; i++) {
906 memcpy(dstPtr, srcPtr, length);
907 srcPtr += srcStride[plane];
908 dstPtr += dstStride[plane];
917 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
918 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
919 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
922 void ff_get_unscaled_swscale(SwsContext *c)
924 const enum AVPixelFormat srcFormat = c->srcFormat;
925 const enum AVPixelFormat dstFormat = c->dstFormat;
926 const int flags = c->flags;
927 const int dstH = c->dstH;
930 needsDither = isAnyRGB(dstFormat) &&
931 c->dstFormatBpp < 24 &&
932 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
935 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
936 (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
937 c->swScale = planarToNv12Wrapper;
940 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
941 srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
942 !(flags & (SWS_ACCURATE_RND|SWS_ERROR_DIFFUSION)) && !(dstH & 1)) {
943 c->swScale = ff_yuv2rgb_get_func_ptr(c);
946 if (srcFormat == AV_PIX_FMT_YUV410P &&
947 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
948 !(flags & SWS_BITEXACT)) {
949 c->swScale = yvu9ToYv12Wrapper;
953 if (srcFormat == AV_PIX_FMT_BGR24 &&
954 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
955 !(flags & SWS_ACCURATE_RND))
956 c->swScale = bgr24ToYv12Wrapper;
958 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
959 if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
960 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
961 c->swScale= rgbToRgbWrapper;
963 #define isByteRGB(f) (\
964 f == AV_PIX_FMT_RGB32 ||\
965 f == AV_PIX_FMT_RGB32_1 ||\
966 f == AV_PIX_FMT_RGB24 ||\
967 f == AV_PIX_FMT_BGR32 ||\
968 f == AV_PIX_FMT_BGR32_1 ||\
969 f == AV_PIX_FMT_BGR24)
971 if (srcFormat == AV_PIX_FMT_GBR24P && isPlanar(srcFormat) && isByteRGB(dstFormat))
972 c->swScale = planarRgbToRgbWrapper;
974 /* bswap 16 bits per pixel/component packed formats */
975 if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
976 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48) ||
977 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
978 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
979 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
980 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
981 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
982 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48) ||
983 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
984 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
985 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565))
986 c->swScale = packed_16bpc_bswap;
988 if (usePal(srcFormat) && isByteRGB(dstFormat))
989 c->swScale = palToRgbWrapper;
991 if (srcFormat == AV_PIX_FMT_YUV422P) {
992 if (dstFormat == AV_PIX_FMT_YUYV422)
993 c->swScale = yuv422pToYuy2Wrapper;
994 else if (dstFormat == AV_PIX_FMT_UYVY422)
995 c->swScale = yuv422pToUyvyWrapper;
998 /* LQ converters if -sws 0 or -sws 4*/
999 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1001 if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
1002 if (dstFormat == AV_PIX_FMT_YUYV422)
1003 c->swScale = planarToYuy2Wrapper;
1004 else if (dstFormat == AV_PIX_FMT_UYVY422)
1005 c->swScale = planarToUyvyWrapper;
1008 if (srcFormat == AV_PIX_FMT_YUYV422 &&
1009 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1010 c->swScale = yuyvToYuv420Wrapper;
1011 if (srcFormat == AV_PIX_FMT_UYVY422 &&
1012 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1013 c->swScale = uyvyToYuv420Wrapper;
1014 if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
1015 c->swScale = yuyvToYuv422Wrapper;
1016 if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
1017 c->swScale = uyvyToYuv422Wrapper;
1019 #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_GRAY8A)
1021 if ( srcFormat == dstFormat ||
1022 (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
1023 (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
1024 (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
1025 (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
1026 (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
1027 (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
1028 c->chrDstHSubSample == c->chrSrcHSubSample &&
1029 c->chrDstVSubSample == c->chrSrcVSubSample &&
1030 dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
1031 srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21))
1033 if (isPacked(c->srcFormat))
1034 c->swScale = packedCopyWrapper;
1035 else /* Planar YUV or gray */
1036 c->swScale = planarCopyWrapper;
1040 ff_bfin_get_unscaled_swscale(c);
1042 ff_swscale_get_unscaled_altivec(c);
1045 /* Convert the palette to the same packed 32-bit format as the palette */
1046 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1047 int num_pixels, const uint8_t *palette)
1051 for (i = 0; i < num_pixels; i++)
1052 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1055 /* Palette format: ABCD -> dst format: ABC */
1056 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1057 int num_pixels, const uint8_t *palette)
1061 for (i = 0; i < num_pixels; i++) {
1063 dst[0] = palette[src[i] * 4 + 0];
1064 dst[1] = palette[src[i] * 4 + 1];
1065 dst[2] = palette[src[i] * 4 + 2];