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"
36 #include "libavutil/avconfig.h"
38 DECLARE_ALIGNED(8, static const uint8_t, dithers)[8][8][8]={
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 { 0, 1, 0, 1, 0, 1, 0, 1,},
47 { 1, 0, 1, 0, 1, 0, 1, 0,},
49 { 1, 2, 1, 2, 1, 2, 1, 2,},
50 { 3, 0, 3, 0, 3, 0, 3, 0,},
51 { 1, 2, 1, 2, 1, 2, 1, 2,},
52 { 3, 0, 3, 0, 3, 0, 3, 0,},
53 { 1, 2, 1, 2, 1, 2, 1, 2,},
54 { 3, 0, 3, 0, 3, 0, 3, 0,},
55 { 1, 2, 1, 2, 1, 2, 1, 2,},
56 { 3, 0, 3, 0, 3, 0, 3, 0,},
58 { 2, 4, 3, 5, 2, 4, 3, 5,},
59 { 6, 0, 7, 1, 6, 0, 7, 1,},
60 { 3, 5, 2, 4, 3, 5, 2, 4,},
61 { 7, 1, 6, 0, 7, 1, 6, 0,},
62 { 2, 4, 3, 5, 2, 4, 3, 5,},
63 { 6, 0, 7, 1, 6, 0, 7, 1,},
64 { 3, 5, 2, 4, 3, 5, 2, 4,},
65 { 7, 1, 6, 0, 7, 1, 6, 0,},
67 { 4, 8, 7, 11, 4, 8, 7, 11,},
68 { 12, 0, 15, 3, 12, 0, 15, 3,},
69 { 6, 10, 5, 9, 6, 10, 5, 9,},
70 { 14, 2, 13, 1, 14, 2, 13, 1,},
71 { 4, 8, 7, 11, 4, 8, 7, 11,},
72 { 12, 0, 15, 3, 12, 0, 15, 3,},
73 { 6, 10, 5, 9, 6, 10, 5, 9,},
74 { 14, 2, 13, 1, 14, 2, 13, 1,},
76 { 9, 17, 15, 23, 8, 16, 14, 22,},
77 { 25, 1, 31, 7, 24, 0, 30, 6,},
78 { 13, 21, 11, 19, 12, 20, 10, 18,},
79 { 29, 5, 27, 3, 28, 4, 26, 2,},
80 { 8, 16, 14, 22, 9, 17, 15, 23,},
81 { 24, 0, 30, 6, 25, 1, 31, 7,},
82 { 12, 20, 10, 18, 13, 21, 11, 19,},
83 { 28, 4, 26, 2, 29, 5, 27, 3,},
85 { 18, 34, 30, 46, 17, 33, 29, 45,},
86 { 50, 2, 62, 14, 49, 1, 61, 13,},
87 { 26, 42, 22, 38, 25, 41, 21, 37,},
88 { 58, 10, 54, 6, 57, 9, 53, 5,},
89 { 16, 32, 28, 44, 19, 35, 31, 47,},
90 { 48, 0, 60, 12, 51, 3, 63, 15,},
91 { 24, 40, 20, 36, 27, 43, 23, 39,},
92 { 56, 8, 52, 4, 59, 11, 55, 7,},
94 { 18, 34, 30, 46, 17, 33, 29, 45,},
95 { 50, 2, 62, 14, 49, 1, 61, 13,},
96 { 26, 42, 22, 38, 25, 41, 21, 37,},
97 { 58, 10, 54, 6, 57, 9, 53, 5,},
98 { 16, 32, 28, 44, 19, 35, 31, 47,},
99 { 48, 0, 60, 12, 51, 3, 63, 15,},
100 { 24, 40, 20, 36, 27, 43, 23, 39,},
101 { 56, 8, 52, 4, 59, 11, 55, 7,},
103 { 36, 68, 60, 92, 34, 66, 58, 90,},
104 { 100, 4,124, 28, 98, 2,122, 26,},
105 { 52, 84, 44, 76, 50, 82, 42, 74,},
106 { 116, 20,108, 12,114, 18,106, 10,},
107 { 32, 64, 56, 88, 38, 70, 62, 94,},
108 { 96, 0,120, 24,102, 6,126, 30,},
109 { 48, 80, 40, 72, 54, 86, 46, 78,},
110 { 112, 16,104, 8,118, 22,110, 14,},
114 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
118 uint8_t *ptr = plane + stride * y;
119 for (i = 0; i < height; i++) {
120 memset(ptr, val, width);
125 static void copyPlane(const uint8_t *src, int srcStride,
126 int srcSliceY, int srcSliceH, int width,
127 uint8_t *dst, int dstStride)
129 dst += dstStride * srcSliceY;
130 if (dstStride == srcStride && srcStride > 0) {
131 memcpy(dst, src, srcSliceH * dstStride);
134 for (i = 0; i < srcSliceH; i++) {
135 memcpy(dst, src, width);
142 static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
143 int srcStride[], int srcSliceY,
144 int srcSliceH, uint8_t *dstParam[],
147 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
149 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
150 dstParam[0], dstStride[0]);
152 if (c->dstFormat == AV_PIX_FMT_NV12)
153 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
154 srcStride[1], srcStride[2], dstStride[1]);
156 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
157 srcStride[2], srcStride[1], dstStride[1]);
162 static int nv12ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
163 int srcStride[], int srcSliceY,
164 int srcSliceH, uint8_t *dstParam[],
167 uint8_t *dst1 = dstParam[1] + dstStride[1] * srcSliceY / 2;
168 uint8_t *dst2 = dstParam[2] + dstStride[2] * srcSliceY / 2;
170 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
171 dstParam[0], dstStride[0]);
173 if (c->srcFormat == AV_PIX_FMT_NV12)
174 deinterleaveBytes(src[1], dst1, dst2,c->srcW / 2, srcSliceH / 2,
175 srcStride[1], dstStride[1], dstStride[2]);
177 deinterleaveBytes(src[1], dst2, dst1, c->srcW / 2, srcSliceH / 2,
178 srcStride[1], dstStride[2], dstStride[1]);
183 static int planarToP010Wrapper(SwsContext *c, const uint8_t *src8[],
184 int srcStride[], int srcSliceY,
185 int srcSliceH, uint8_t *dstParam8[],
188 const uint16_t **src = (const uint16_t**)src8;
189 uint16_t *dstY = (uint16_t*)(dstParam8[0] + dstStride[0] * srcSliceY);
190 uint16_t *dstUV = (uint16_t*)(dstParam8[1] + dstStride[1] * srcSliceY / 2);
193 av_assert0(!(srcStride[0] % 2 || srcStride[1] % 2 || srcStride[2] % 2 ||
194 dstStride[0] % 2 || dstStride[1] % 2));
196 for (y = 0; y < srcSliceH; y++) {
197 uint16_t *tdstY = dstY;
198 const uint16_t *tsrc0 = src[0];
199 for (x = c->srcW; x > 0; x--) {
200 *tdstY++ = *tsrc0++ << 6;
202 src[0] += srcStride[0] / 2;
203 dstY += dstStride[0] / 2;
206 uint16_t *tdstUV = dstUV;
207 const uint16_t *tsrc1 = src[1];
208 const uint16_t *tsrc2 = src[2];
209 for (x = c->srcW / 2; x > 0; x--) {
210 *tdstUV++ = *tsrc1++ << 6;
211 *tdstUV++ = *tsrc2++ << 6;
213 src[1] += srcStride[1] / 2;
214 src[2] += srcStride[2] / 2;
215 dstUV += dstStride[1] / 2;
222 #if AV_HAVE_BIGENDIAN
223 #define output_pixel(p, v) do { \
224 uint16_t *pp = (p); \
228 #define output_pixel(p, v) (*p) = (v)
231 static int planar8ToP01xleWrapper(SwsContext *c, const uint8_t *src[],
232 int srcStride[], int srcSliceY,
233 int srcSliceH, uint8_t *dstParam8[],
236 uint16_t *dstY = (uint16_t*)(dstParam8[0] + dstStride[0] * srcSliceY);
237 uint16_t *dstUV = (uint16_t*)(dstParam8[1] + dstStride[1] * srcSliceY / 2);
240 av_assert0(!(dstStride[0] % 2 || dstStride[1] % 2));
242 for (y = 0; y < srcSliceH; y++) {
243 uint16_t *tdstY = dstY;
244 const uint8_t *tsrc0 = src[0];
245 for (x = c->srcW; x > 0; x--) {
247 output_pixel(tdstY++, t | (t << 8));
249 src[0] += srcStride[0];
250 dstY += dstStride[0] / 2;
253 uint16_t *tdstUV = dstUV;
254 const uint8_t *tsrc1 = src[1];
255 const uint8_t *tsrc2 = src[2];
256 for (x = c->srcW / 2; x > 0; x--) {
258 output_pixel(tdstUV++, t | (t << 8));
260 output_pixel(tdstUV++, t | (t << 8));
262 src[1] += srcStride[1];
263 src[2] += srcStride[2];
264 dstUV += dstStride[1] / 2;
273 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
274 int srcStride[], int srcSliceY, int srcSliceH,
275 uint8_t *dstParam[], int dstStride[])
277 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
279 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
280 srcStride[1], dstStride[0]);
285 static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
286 int srcStride[], int srcSliceY, int srcSliceH,
287 uint8_t *dstParam[], int dstStride[])
289 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
291 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
292 srcStride[1], dstStride[0]);
297 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
298 int srcStride[], int srcSliceY, int srcSliceH,
299 uint8_t *dstParam[], int dstStride[])
301 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
303 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
304 srcStride[1], dstStride[0]);
309 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
310 int srcStride[], int srcSliceY, int srcSliceH,
311 uint8_t *dstParam[], int dstStride[])
313 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
315 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
316 srcStride[1], dstStride[0]);
321 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
322 int srcStride[], int srcSliceY, int srcSliceH,
323 uint8_t *dstParam[], int dstStride[])
325 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
326 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
327 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
329 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
330 dstStride[1], srcStride[0]);
333 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
338 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
339 int srcStride[], int srcSliceY, int srcSliceH,
340 uint8_t *dstParam[], int dstStride[])
342 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
343 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
344 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
346 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
347 dstStride[1], srcStride[0]);
352 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
353 int srcStride[], int srcSliceY, int srcSliceH,
354 uint8_t *dstParam[], int dstStride[])
356 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
357 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
358 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
360 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
361 dstStride[1], srcStride[0]);
364 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
369 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
370 int srcStride[], int srcSliceY, int srcSliceH,
371 uint8_t *dstParam[], int dstStride[])
373 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
374 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
375 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
377 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
378 dstStride[1], srcStride[0]);
383 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
384 const uint8_t *palette)
387 for (i = 0; i < num_pixels; i++)
388 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
391 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
392 const uint8_t *palette)
396 for (i = 0; i < num_pixels; i++)
397 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
400 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
401 const uint8_t *palette)
405 for (i = 0; i < num_pixels; i++) {
407 dst[0] = palette[src[i << 1] * 4 + 0];
408 dst[1] = palette[src[i << 1] * 4 + 1];
409 dst[2] = palette[src[i << 1] * 4 + 2];
414 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
415 int srcStride[], int srcSliceY, int srcSliceH,
416 uint8_t *dst[], int dstStride[])
420 for (p = 0; p < 4; p++) {
421 int srcstr = srcStride[p] / 2;
422 int dststr = dstStride[p] / 2;
423 uint16_t *dstPtr = (uint16_t *) dst[p];
424 const uint16_t *srcPtr = (const uint16_t *) src[p];
425 int min_stride = FFMIN(FFABS(srcstr), FFABS(dststr));
426 if(!dstPtr || !srcPtr)
428 dstPtr += (srcSliceY >> c->chrDstVSubSample) * dststr;
429 for (i = 0; i < (srcSliceH >> c->chrDstVSubSample); i++) {
430 for (j = 0; j < min_stride; j++) {
431 dstPtr[j] = av_bswap16(srcPtr[j]);
441 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
442 int srcSliceY, int srcSliceH, uint8_t *dst[],
445 const enum AVPixelFormat srcFormat = c->srcFormat;
446 const enum AVPixelFormat dstFormat = c->dstFormat;
447 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
448 const uint8_t *palette) = NULL;
450 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
451 const uint8_t *srcPtr = src[0];
453 if (srcFormat == AV_PIX_FMT_YA8) {
455 case AV_PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
456 case AV_PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
457 case AV_PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
458 case AV_PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
459 case AV_PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
460 case AV_PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
462 } else if (usePal(srcFormat)) {
464 case AV_PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
465 case AV_PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
466 case AV_PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
467 case AV_PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
468 case AV_PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
469 case AV_PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
474 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
475 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
477 for (i = 0; i < srcSliceH; i++) {
478 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
479 srcPtr += srcStride[0];
480 dstPtr += dstStride[0];
487 static void packed16togbra16(const uint8_t *src, int srcStride,
488 uint16_t *dst[], int dstStride[], int srcSliceH,
489 int src_alpha, int swap, int shift, int width)
492 int dst_alpha = dst[3] != NULL;
493 for (h = 0; h < srcSliceH; h++) {
494 uint16_t *src_line = (uint16_t *)(src + srcStride * h);
497 if (src_alpha && dst_alpha) {
498 for (x = 0; x < width; x++) {
499 dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
500 dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
501 dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
502 dst[3][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
504 } else if (dst_alpha) {
505 for (x = 0; x < width; x++) {
506 dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
507 dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
508 dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
511 } else if (src_alpha) {
512 for (x = 0; x < width; x++) {
513 dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
514 dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
515 dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
519 for (x = 0; x < width; x++) {
520 dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
521 dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
522 dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
527 if (src_alpha && dst_alpha) {
528 for (x = 0; x < width; x++) {
529 dst[0][x] = av_bswap16(*src_line++ >> shift);
530 dst[1][x] = av_bswap16(*src_line++ >> shift);
531 dst[2][x] = av_bswap16(*src_line++ >> shift);
532 dst[3][x] = av_bswap16(*src_line++ >> shift);
534 } else if (dst_alpha) {
535 for (x = 0; x < width; x++) {
536 dst[0][x] = av_bswap16(*src_line++ >> shift);
537 dst[1][x] = av_bswap16(*src_line++ >> shift);
538 dst[2][x] = av_bswap16(*src_line++ >> shift);
541 } else if (src_alpha) {
542 for (x = 0; x < width; x++) {
543 dst[0][x] = av_bswap16(*src_line++ >> shift);
544 dst[1][x] = av_bswap16(*src_line++ >> shift);
545 dst[2][x] = av_bswap16(*src_line++ >> shift);
549 for (x = 0; x < width; x++) {
550 dst[0][x] = av_bswap16(*src_line++ >> shift);
551 dst[1][x] = av_bswap16(*src_line++ >> shift);
552 dst[2][x] = av_bswap16(*src_line++ >> shift);
557 if (src_alpha && dst_alpha) {
558 for (x = 0; x < width; x++) {
559 dst[0][x] = av_bswap16(*src_line++) >> shift;
560 dst[1][x] = av_bswap16(*src_line++) >> shift;
561 dst[2][x] = av_bswap16(*src_line++) >> shift;
562 dst[3][x] = av_bswap16(*src_line++) >> shift;
564 } else if (dst_alpha) {
565 for (x = 0; x < width; x++) {
566 dst[0][x] = av_bswap16(*src_line++) >> shift;
567 dst[1][x] = av_bswap16(*src_line++) >> shift;
568 dst[2][x] = av_bswap16(*src_line++) >> shift;
571 } else if (src_alpha) {
572 for (x = 0; x < width; x++) {
573 dst[0][x] = av_bswap16(*src_line++) >> shift;
574 dst[1][x] = av_bswap16(*src_line++) >> shift;
575 dst[2][x] = av_bswap16(*src_line++) >> shift;
579 for (x = 0; x < width; x++) {
580 dst[0][x] = av_bswap16(*src_line++) >> shift;
581 dst[1][x] = av_bswap16(*src_line++) >> shift;
582 dst[2][x] = av_bswap16(*src_line++) >> shift;
587 if (src_alpha && dst_alpha) {
588 for (x = 0; x < width; x++) {
589 dst[0][x] = *src_line++ >> shift;
590 dst[1][x] = *src_line++ >> shift;
591 dst[2][x] = *src_line++ >> shift;
592 dst[3][x] = *src_line++ >> shift;
594 } else if (dst_alpha) {
595 for (x = 0; x < width; x++) {
596 dst[0][x] = *src_line++ >> shift;
597 dst[1][x] = *src_line++ >> shift;
598 dst[2][x] = *src_line++ >> shift;
601 } else if (src_alpha) {
602 for (x = 0; x < width; x++) {
603 dst[0][x] = *src_line++ >> shift;
604 dst[1][x] = *src_line++ >> shift;
605 dst[2][x] = *src_line++ >> shift;
609 for (x = 0; x < width; x++) {
610 dst[0][x] = *src_line++ >> shift;
611 dst[1][x] = *src_line++ >> shift;
612 dst[2][x] = *src_line++ >> shift;
616 for (i = 0; i < 4; i++)
617 dst[i] += dstStride[i] >> 1;
621 static int Rgb16ToPlanarRgb16Wrapper(SwsContext *c, const uint8_t *src[],
622 int srcStride[], int srcSliceY, int srcSliceH,
623 uint8_t *dst[], int dstStride[])
625 uint16_t *dst2013[] = { (uint16_t *)dst[2], (uint16_t *)dst[0], (uint16_t *)dst[1], (uint16_t *)dst[3] };
626 uint16_t *dst1023[] = { (uint16_t *)dst[1], (uint16_t *)dst[0], (uint16_t *)dst[2], (uint16_t *)dst[3] };
627 int stride2013[] = { dstStride[2], dstStride[0], dstStride[1], dstStride[3] };
628 int stride1023[] = { dstStride[1], dstStride[0], dstStride[2], dstStride[3] };
629 const AVPixFmtDescriptor *src_format = av_pix_fmt_desc_get(c->srcFormat);
630 const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
631 int bpc = dst_format->comp[0].depth;
632 int alpha = src_format->flags & AV_PIX_FMT_FLAG_ALPHA;
636 if ( HAVE_BIGENDIAN && !(src_format->flags & AV_PIX_FMT_FLAG_BE) ||
637 !HAVE_BIGENDIAN && src_format->flags & AV_PIX_FMT_FLAG_BE)
639 if ( HAVE_BIGENDIAN && !(dst_format->flags & AV_PIX_FMT_FLAG_BE) ||
640 !HAVE_BIGENDIAN && dst_format->flags & AV_PIX_FMT_FLAG_BE)
643 if ((dst_format->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) !=
644 (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB) || bpc < 9) {
645 av_log(c, AV_LOG_ERROR, "unsupported conversion to planar RGB %s -> %s\n",
646 src_format->name, dst_format->name);
651 dst2013[i] += stride2013[i] * srcSliceY / 2;
652 dst1023[i] += stride1023[i] * srcSliceY / 2;
655 switch (c->srcFormat) {
656 case AV_PIX_FMT_RGB48LE:
657 case AV_PIX_FMT_RGB48BE:
658 case AV_PIX_FMT_RGBA64LE:
659 case AV_PIX_FMT_RGBA64BE:
660 packed16togbra16(src[0], srcStride[0],
661 dst2013, stride2013, srcSliceH, alpha, swap,
664 case AV_PIX_FMT_BGR48LE:
665 case AV_PIX_FMT_BGR48BE:
666 case AV_PIX_FMT_BGRA64LE:
667 case AV_PIX_FMT_BGRA64BE:
668 packed16togbra16(src[0], srcStride[0],
669 dst1023, stride1023, srcSliceH, alpha, swap,
673 av_log(c, AV_LOG_ERROR,
674 "unsupported conversion to planar RGB %s -> %s\n",
675 src_format->name, dst_format->name);
681 static void gbr16ptopacked16(const uint16_t *src[], int srcStride[],
682 uint8_t *dst, int dstStride, int srcSliceH,
683 int alpha, int swap, int bpp, int width)
686 int src_alpha = src[3] != NULL;
687 int scale_high = 16 - bpp, scale_low = (bpp - 8) * 2;
688 for (h = 0; h < srcSliceH; h++) {
689 uint16_t *dest = (uint16_t *)(dst + dstStride * h);
694 if (alpha && !src_alpha) {
695 for (x = 0; x < width; x++) {
696 component = av_bswap16(src[0][x]);
697 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
698 component = av_bswap16(src[1][x]);
699 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
700 component = av_bswap16(src[2][x]);
701 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
704 } else if (alpha && src_alpha) {
705 for (x = 0; x < width; x++) {
706 component = av_bswap16(src[0][x]);
707 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
708 component = av_bswap16(src[1][x]);
709 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
710 component = av_bswap16(src[2][x]);
711 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
712 component = av_bswap16(src[3][x]);
713 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
716 for (x = 0; x < width; x++) {
717 component = av_bswap16(src[0][x]);
718 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
719 component = av_bswap16(src[1][x]);
720 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
721 component = av_bswap16(src[2][x]);
722 *dest++ = av_bswap16(component << scale_high | component >> scale_low);
727 if (alpha && !src_alpha) {
728 for (x = 0; x < width; x++) {
729 *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
730 *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
731 *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
734 } else if (alpha && src_alpha) {
735 for (x = 0; x < width; x++) {
736 *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
737 *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
738 *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
739 *dest++ = av_bswap16(src[3][x] << scale_high | src[3][x] >> scale_low);
742 for (x = 0; x < width; x++) {
743 *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
744 *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
745 *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
750 if (alpha && !src_alpha) {
751 for (x = 0; x < width; x++) {
752 *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
753 *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
754 *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
757 } else if (alpha && src_alpha) {
758 for (x = 0; x < width; x++) {
759 *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
760 *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
761 *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
762 *dest++ = av_bswap16(src[3][x]) << scale_high | av_bswap16(src[3][x]) >> scale_low;
765 for (x = 0; x < width; x++) {
766 *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
767 *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
768 *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
773 if (alpha && !src_alpha) {
774 for (x = 0; x < width; x++) {
775 *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
776 *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
777 *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
780 } else if (alpha && src_alpha) {
781 for (x = 0; x < width; x++) {
782 *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
783 *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
784 *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
785 *dest++ = src[3][x] << scale_high | src[3][x] >> scale_low;
788 for (x = 0; x < width; x++) {
789 *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
790 *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
791 *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
795 for (i = 0; i < 3 + src_alpha; i++)
796 src[i] += srcStride[i] >> 1;
800 static int planarRgb16ToRgb16Wrapper(SwsContext *c, const uint8_t *src[],
801 int srcStride[], int srcSliceY, int srcSliceH,
802 uint8_t *dst[], int dstStride[])
804 const uint16_t *src102[] = { (uint16_t *)src[1], (uint16_t *)src[0], (uint16_t *)src[2], (uint16_t *)src[3] };
805 const uint16_t *src201[] = { (uint16_t *)src[2], (uint16_t *)src[0], (uint16_t *)src[1], (uint16_t *)src[3] };
806 int stride102[] = { srcStride[1], srcStride[0], srcStride[2], srcStride[3] };
807 int stride201[] = { srcStride[2], srcStride[0], srcStride[1], srcStride[3] };
808 const AVPixFmtDescriptor *src_format = av_pix_fmt_desc_get(c->srcFormat);
809 const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
810 int bits_per_sample = src_format->comp[0].depth;
812 if ( HAVE_BIGENDIAN && !(src_format->flags & AV_PIX_FMT_FLAG_BE) ||
813 !HAVE_BIGENDIAN && src_format->flags & AV_PIX_FMT_FLAG_BE)
815 if ( HAVE_BIGENDIAN && !(dst_format->flags & AV_PIX_FMT_FLAG_BE) ||
816 !HAVE_BIGENDIAN && dst_format->flags & AV_PIX_FMT_FLAG_BE)
819 if ((src_format->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) !=
820 (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB) ||
821 bits_per_sample <= 8) {
822 av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
823 src_format->name, dst_format->name);
826 switch (c->dstFormat) {
827 case AV_PIX_FMT_BGR48LE:
828 case AV_PIX_FMT_BGR48BE:
829 gbr16ptopacked16(src102, stride102,
830 dst[0] + srcSliceY * dstStride[0], dstStride[0],
831 srcSliceH, 0, swap, bits_per_sample, c->srcW);
833 case AV_PIX_FMT_RGB48LE:
834 case AV_PIX_FMT_RGB48BE:
835 gbr16ptopacked16(src201, stride201,
836 dst[0] + srcSliceY * dstStride[0], dstStride[0],
837 srcSliceH, 0, swap, bits_per_sample, c->srcW);
839 case AV_PIX_FMT_RGBA64LE:
840 case AV_PIX_FMT_RGBA64BE:
841 gbr16ptopacked16(src201, stride201,
842 dst[0] + srcSliceY * dstStride[0], dstStride[0],
843 srcSliceH, 1, swap, bits_per_sample, c->srcW);
845 case AV_PIX_FMT_BGRA64LE:
846 case AV_PIX_FMT_BGRA64BE:
847 gbr16ptopacked16(src102, stride102,
848 dst[0] + srcSliceY * dstStride[0], dstStride[0],
849 srcSliceH, 1, swap, bits_per_sample, c->srcW);
852 av_log(c, AV_LOG_ERROR,
853 "unsupported planar RGB conversion %s -> %s\n",
854 src_format->name, dst_format->name);
860 static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
861 uint8_t *dst, int dstStride, int srcSliceH,
865 for (h = 0; h < srcSliceH; h++) {
866 uint8_t *dest = dst + dstStride * h;
867 for (x = 0; x < width; x++) {
873 for (i = 0; i < 3; i++)
874 src[i] += srcStride[i];
878 static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
879 uint8_t *dst, int dstStride, int srcSliceH,
880 int alpha_first, int width)
883 for (h = 0; h < srcSliceH; h++) {
884 uint8_t *dest = dst + dstStride * h;
887 for (x = 0; x < width; x++) {
894 for (x = 0; x < width; x++) {
902 for (i = 0; i < 3; i++)
903 src[i] += srcStride[i];
907 static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
908 int srcStride[], int srcSliceY, int srcSliceH,
909 uint8_t *dst[], int dstStride[])
912 const uint8_t *src102[] = { src[1], src[0], src[2] };
913 const uint8_t *src201[] = { src[2], src[0], src[1] };
914 int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
915 int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
917 if (c->srcFormat != AV_PIX_FMT_GBRP) {
918 av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
919 av_get_pix_fmt_name(c->srcFormat),
920 av_get_pix_fmt_name(c->dstFormat));
924 switch (c->dstFormat) {
925 case AV_PIX_FMT_BGR24:
926 gbr24ptopacked24(src102, stride102,
927 dst[0] + srcSliceY * dstStride[0], dstStride[0],
931 case AV_PIX_FMT_RGB24:
932 gbr24ptopacked24(src201, stride201,
933 dst[0] + srcSliceY * dstStride[0], dstStride[0],
937 case AV_PIX_FMT_ARGB:
939 case AV_PIX_FMT_RGBA:
940 gbr24ptopacked32(src201, stride201,
941 dst[0] + srcSliceY * dstStride[0], dstStride[0],
942 srcSliceH, alpha_first, c->srcW);
945 case AV_PIX_FMT_ABGR:
947 case AV_PIX_FMT_BGRA:
948 gbr24ptopacked32(src102, stride102,
949 dst[0] + srcSliceY * dstStride[0], dstStride[0],
950 srcSliceH, alpha_first, c->srcW);
954 av_log(c, AV_LOG_ERROR,
955 "unsupported planar RGB conversion %s -> %s\n",
956 av_get_pix_fmt_name(c->srcFormat),
957 av_get_pix_fmt_name(c->dstFormat));
963 static int planarRgbToplanarRgbWrapper(SwsContext *c,
964 const uint8_t *src[], int srcStride[],
965 int srcSliceY, int srcSliceH,
966 uint8_t *dst[], int dstStride[])
968 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
969 dst[0], dstStride[0]);
970 copyPlane(src[1], srcStride[1], srcSliceY, srcSliceH, c->srcW,
971 dst[1], dstStride[1]);
972 copyPlane(src[2], srcStride[2], srcSliceY, srcSliceH, c->srcW,
973 dst[2], dstStride[2]);
975 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
980 static void packedtogbr24p(const uint8_t *src, int srcStride,
981 uint8_t *dst[], int dstStride[], int srcSliceH,
982 int alpha_first, int inc_size, int width)
994 for (h = 0; h < srcSliceH; h++) {
995 for (x = 0; x < width; x++) {
1002 src += srcStride - width * inc_size;
1003 dest[0] += dstStride[0];
1004 dest[1] += dstStride[1];
1005 dest[2] += dstStride[2];
1009 static int rgbToPlanarRgbWrapper(SwsContext *c, const uint8_t *src[],
1010 int srcStride[], int srcSliceY, int srcSliceH,
1011 uint8_t *dst[], int dstStride[])
1013 int alpha_first = 0;
1014 int stride102[] = { dstStride[1], dstStride[0], dstStride[2] };
1015 int stride201[] = { dstStride[2], dstStride[0], dstStride[1] };
1016 uint8_t *dst102[] = { dst[1] + srcSliceY * dstStride[1],
1017 dst[0] + srcSliceY * dstStride[0],
1018 dst[2] + srcSliceY * dstStride[2] };
1019 uint8_t *dst201[] = { dst[2] + srcSliceY * dstStride[2],
1020 dst[0] + srcSliceY * dstStride[0],
1021 dst[1] + srcSliceY * dstStride[1] };
1023 switch (c->srcFormat) {
1024 case AV_PIX_FMT_RGB24:
1025 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
1026 stride201, srcSliceH, alpha_first, 3, c->srcW);
1028 case AV_PIX_FMT_BGR24:
1029 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
1030 stride102, srcSliceH, alpha_first, 3, c->srcW);
1032 case AV_PIX_FMT_ARGB:
1034 case AV_PIX_FMT_RGBA:
1035 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
1036 stride201, srcSliceH, alpha_first, 4, c->srcW);
1038 case AV_PIX_FMT_ABGR:
1040 case AV_PIX_FMT_BGRA:
1041 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
1042 stride102, srcSliceH, alpha_first, 4, c->srcW);
1045 av_log(c, AV_LOG_ERROR,
1046 "unsupported planar RGB conversion %s -> %s\n",
1047 av_get_pix_fmt_name(c->srcFormat),
1048 av_get_pix_fmt_name(c->dstFormat));
1056 #define BAYER_RENAME(x) bayer_gbrg8_to_##x
1057 #include "bayer_template.c"
1061 #define BAYER_RENAME(x) bayer_gbrg16le_to_##x
1062 #include "bayer_template.c"
1066 #define BAYER_RENAME(x) bayer_gbrg16be_to_##x
1067 #include "bayer_template.c"
1071 #define BAYER_RENAME(x) bayer_grbg8_to_##x
1072 #include "bayer_template.c"
1076 #define BAYER_RENAME(x) bayer_grbg16le_to_##x
1077 #include "bayer_template.c"
1081 #define BAYER_RENAME(x) bayer_grbg16be_to_##x
1082 #include "bayer_template.c"
1086 #define BAYER_RENAME(x) bayer_bggr8_to_##x
1087 #include "bayer_template.c"
1091 #define BAYER_RENAME(x) bayer_bggr16le_to_##x
1092 #include "bayer_template.c"
1096 #define BAYER_RENAME(x) bayer_bggr16be_to_##x
1097 #include "bayer_template.c"
1101 #define BAYER_RENAME(x) bayer_rggb8_to_##x
1102 #include "bayer_template.c"
1106 #define BAYER_RENAME(x) bayer_rggb16le_to_##x
1107 #include "bayer_template.c"
1111 #define BAYER_RENAME(x) bayer_rggb16be_to_##x
1112 #include "bayer_template.c"
1114 static int bayer_to_rgb24_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1115 int srcSliceH, uint8_t* dst[], int dstStride[])
1117 uint8_t *dstPtr= dst[0] + srcSliceY * dstStride[0];
1118 const uint8_t *srcPtr= src[0];
1120 void (*copy) (const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width);
1121 void (*interpolate)(const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width);
1123 switch(c->srcFormat) {
1124 #define CASE(pixfmt, prefix) \
1125 case pixfmt: copy = bayer_##prefix##_to_rgb24_copy; \
1126 interpolate = bayer_##prefix##_to_rgb24_interpolate; \
1128 CASE(AV_PIX_FMT_BAYER_BGGR8, bggr8)
1129 CASE(AV_PIX_FMT_BAYER_BGGR16LE, bggr16le)
1130 CASE(AV_PIX_FMT_BAYER_BGGR16BE, bggr16be)
1131 CASE(AV_PIX_FMT_BAYER_RGGB8, rggb8)
1132 CASE(AV_PIX_FMT_BAYER_RGGB16LE, rggb16le)
1133 CASE(AV_PIX_FMT_BAYER_RGGB16BE, rggb16be)
1134 CASE(AV_PIX_FMT_BAYER_GBRG8, gbrg8)
1135 CASE(AV_PIX_FMT_BAYER_GBRG16LE, gbrg16le)
1136 CASE(AV_PIX_FMT_BAYER_GBRG16BE, gbrg16be)
1137 CASE(AV_PIX_FMT_BAYER_GRBG8, grbg8)
1138 CASE(AV_PIX_FMT_BAYER_GRBG16LE, grbg16le)
1139 CASE(AV_PIX_FMT_BAYER_GRBG16BE, grbg16be)
1144 av_assert0(srcSliceH > 1);
1146 copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
1147 srcPtr += 2 * srcStride[0];
1148 dstPtr += 2 * dstStride[0];
1150 for (i = 2; i < srcSliceH - 2; i += 2) {
1151 interpolate(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
1152 srcPtr += 2 * srcStride[0];
1153 dstPtr += 2 * dstStride[0];
1156 if (i + 1 == srcSliceH) {
1157 copy(srcPtr, -srcStride[0], dstPtr, -dstStride[0], c->srcW);
1158 } else if (i < srcSliceH)
1159 copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
1163 static int bayer_to_yv12_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1164 int srcSliceH, uint8_t* dst[], int dstStride[])
1166 const uint8_t *srcPtr= src[0];
1167 uint8_t *dstY= dst[0] + srcSliceY * dstStride[0];
1168 uint8_t *dstU= dst[1] + srcSliceY * dstStride[1] / 2;
1169 uint8_t *dstV= dst[2] + srcSliceY * dstStride[2] / 2;
1171 void (*copy) (const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv);
1172 void (*interpolate)(const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv);
1174 switch(c->srcFormat) {
1175 #define CASE(pixfmt, prefix) \
1176 case pixfmt: copy = bayer_##prefix##_to_yv12_copy; \
1177 interpolate = bayer_##prefix##_to_yv12_interpolate; \
1179 CASE(AV_PIX_FMT_BAYER_BGGR8, bggr8)
1180 CASE(AV_PIX_FMT_BAYER_BGGR16LE, bggr16le)
1181 CASE(AV_PIX_FMT_BAYER_BGGR16BE, bggr16be)
1182 CASE(AV_PIX_FMT_BAYER_RGGB8, rggb8)
1183 CASE(AV_PIX_FMT_BAYER_RGGB16LE, rggb16le)
1184 CASE(AV_PIX_FMT_BAYER_RGGB16BE, rggb16be)
1185 CASE(AV_PIX_FMT_BAYER_GBRG8, gbrg8)
1186 CASE(AV_PIX_FMT_BAYER_GBRG16LE, gbrg16le)
1187 CASE(AV_PIX_FMT_BAYER_GBRG16BE, gbrg16be)
1188 CASE(AV_PIX_FMT_BAYER_GRBG8, grbg8)
1189 CASE(AV_PIX_FMT_BAYER_GRBG16LE, grbg16le)
1190 CASE(AV_PIX_FMT_BAYER_GRBG16BE, grbg16be)
1195 av_assert0(srcSliceH > 1);
1197 copy(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
1198 srcPtr += 2 * srcStride[0];
1199 dstY += 2 * dstStride[0];
1200 dstU += dstStride[1];
1201 dstV += dstStride[1];
1203 for (i = 2; i < srcSliceH - 2; i += 2) {
1204 interpolate(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
1205 srcPtr += 2 * srcStride[0];
1206 dstY += 2 * dstStride[0];
1207 dstU += dstStride[1];
1208 dstV += dstStride[1];
1211 if (i + 1 == srcSliceH) {
1212 copy(srcPtr, -srcStride[0], dstY, dstU, dstV, -dstStride[0], c->srcW, c->input_rgb2yuv_table);
1213 } else if (i < srcSliceH)
1214 copy(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
1218 #define isRGBA32(x) ( \
1219 (x) == AV_PIX_FMT_ARGB \
1220 || (x) == AV_PIX_FMT_RGBA \
1221 || (x) == AV_PIX_FMT_BGRA \
1222 || (x) == AV_PIX_FMT_ABGR \
1225 #define isRGBA64(x) ( \
1226 (x) == AV_PIX_FMT_RGBA64LE \
1227 || (x) == AV_PIX_FMT_RGBA64BE \
1228 || (x) == AV_PIX_FMT_BGRA64LE \
1229 || (x) == AV_PIX_FMT_BGRA64BE \
1232 #define isRGB48(x) ( \
1233 (x) == AV_PIX_FMT_RGB48LE \
1234 || (x) == AV_PIX_FMT_RGB48BE \
1235 || (x) == AV_PIX_FMT_BGR48LE \
1236 || (x) == AV_PIX_FMT_BGR48BE \
1239 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1240 typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
1241 static rgbConvFn findRgbConvFn(SwsContext *c)
1243 const enum AVPixelFormat srcFormat = c->srcFormat;
1244 const enum AVPixelFormat dstFormat = c->dstFormat;
1245 const int srcId = c->srcFormatBpp;
1246 const int dstId = c->dstFormatBpp;
1247 rgbConvFn conv = NULL;
1249 #define IS_NOT_NE(bpp, desc) \
1250 (((bpp + 7) >> 3) == 2 && \
1251 (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
1253 #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
1255 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1256 if ( CONV_IS(ABGR, RGBA)
1257 || CONV_IS(ARGB, BGRA)
1258 || CONV_IS(BGRA, ARGB)
1259 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1260 else if (CONV_IS(ABGR, ARGB)
1261 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1262 else if (CONV_IS(ABGR, BGRA)
1263 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1264 else if (CONV_IS(BGRA, RGBA)
1265 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1266 else if (CONV_IS(BGRA, ABGR)
1267 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1268 } else if (isRGB48(srcFormat) && isRGB48(dstFormat)) {
1269 if (CONV_IS(RGB48LE, BGR48LE)
1270 || CONV_IS(BGR48LE, RGB48LE)
1271 || CONV_IS(RGB48BE, BGR48BE)
1272 || CONV_IS(BGR48BE, RGB48BE)) conv = rgb48tobgr48_nobswap;
1273 else if (CONV_IS(RGB48LE, BGR48BE)
1274 || CONV_IS(BGR48LE, RGB48BE)
1275 || CONV_IS(RGB48BE, BGR48LE)
1276 || CONV_IS(BGR48BE, RGB48LE)) conv = rgb48tobgr48_bswap;
1277 } else if (isRGB48(srcFormat) && isRGBA64(dstFormat)) {
1278 if (CONV_IS(RGB48LE, BGRA64LE)
1279 || CONV_IS(BGR48LE, RGBA64LE)
1280 || CONV_IS(RGB48BE, BGRA64BE)
1281 || CONV_IS(BGR48BE, RGBA64BE)) conv = rgb48tobgr64_nobswap;
1282 else if (CONV_IS(RGB48LE, BGRA64BE)
1283 || CONV_IS(BGR48LE, RGBA64BE)
1284 || CONV_IS(RGB48BE, BGRA64LE)
1285 || CONV_IS(BGR48BE, RGBA64LE)) conv = rgb48tobgr64_bswap;
1286 if (CONV_IS(RGB48LE, RGBA64LE)
1287 || CONV_IS(BGR48LE, BGRA64LE)
1288 || CONV_IS(RGB48BE, RGBA64BE)
1289 || CONV_IS(BGR48BE, BGRA64BE)) conv = rgb48to64_nobswap;
1290 else if (CONV_IS(RGB48LE, RGBA64BE)
1291 || CONV_IS(BGR48LE, BGRA64BE)
1292 || CONV_IS(RGB48BE, RGBA64LE)
1293 || CONV_IS(BGR48BE, BGRA64LE)) conv = rgb48to64_bswap;
1294 } else if (isRGBA64(srcFormat) && isRGB48(dstFormat)) {
1295 if (CONV_IS(RGBA64LE, BGR48LE)
1296 || CONV_IS(BGRA64LE, RGB48LE)
1297 || CONV_IS(RGBA64BE, BGR48BE)
1298 || CONV_IS(BGRA64BE, RGB48BE)) conv = rgb64tobgr48_nobswap;
1299 else if (CONV_IS(RGBA64LE, BGR48BE)
1300 || CONV_IS(BGRA64LE, RGB48BE)
1301 || CONV_IS(RGBA64BE, BGR48LE)
1302 || CONV_IS(BGRA64BE, RGB48LE)) conv = rgb64tobgr48_bswap;
1303 else if (CONV_IS(RGBA64LE, RGB48LE)
1304 || CONV_IS(BGRA64LE, BGR48LE)
1305 || CONV_IS(RGBA64BE, RGB48BE)
1306 || CONV_IS(BGRA64BE, BGR48BE)) conv = rgb64to48_nobswap;
1307 else if (CONV_IS(RGBA64LE, RGB48BE)
1308 || CONV_IS(BGRA64LE, BGR48BE)
1309 || CONV_IS(RGBA64BE, RGB48LE)
1310 || CONV_IS(BGRA64BE, BGR48LE)) conv = rgb64to48_bswap;
1313 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
1314 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1315 switch (srcId | (dstId << 16)) {
1316 case 0x000F000C: conv = rgb12to15; break;
1317 case 0x000F0010: conv = rgb16to15; break;
1318 case 0x000F0018: conv = rgb24to15; break;
1319 case 0x000F0020: conv = rgb32to15; break;
1320 case 0x0010000F: conv = rgb15to16; break;
1321 case 0x00100018: conv = rgb24to16; break;
1322 case 0x00100020: conv = rgb32to16; break;
1323 case 0x0018000F: conv = rgb15to24; break;
1324 case 0x00180010: conv = rgb16to24; break;
1325 case 0x00180020: conv = rgb32to24; break;
1326 case 0x0020000F: conv = rgb15to32; break;
1327 case 0x00200010: conv = rgb16to32; break;
1328 case 0x00200018: conv = rgb24to32; break;
1330 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
1331 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1332 switch (srcId | (dstId << 16)) {
1333 case 0x000C000C: conv = rgb12tobgr12; break;
1334 case 0x000F000F: conv = rgb15tobgr15; break;
1335 case 0x000F0010: conv = rgb16tobgr15; break;
1336 case 0x000F0018: conv = rgb24tobgr15; break;
1337 case 0x000F0020: conv = rgb32tobgr15; break;
1338 case 0x0010000F: conv = rgb15tobgr16; break;
1339 case 0x00100010: conv = rgb16tobgr16; break;
1340 case 0x00100018: conv = rgb24tobgr16; break;
1341 case 0x00100020: conv = rgb32tobgr16; break;
1342 case 0x0018000F: conv = rgb15tobgr24; break;
1343 case 0x00180010: conv = rgb16tobgr24; break;
1344 case 0x00180018: conv = rgb24tobgr24; break;
1345 case 0x00180020: conv = rgb32tobgr24; break;
1346 case 0x0020000F: conv = rgb15tobgr32; break;
1347 case 0x00200010: conv = rgb16tobgr32; break;
1348 case 0x00200018: conv = rgb24tobgr32; break;
1352 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) && !isRGBA32(srcFormat) && ALT32_CORR<0)
1355 // Maintain symmetry between endianness
1356 if (c->flags & SWS_BITEXACT)
1357 if ((dstFormat == AV_PIX_FMT_RGB32 || dstFormat == AV_PIX_FMT_BGR32 ) && !isRGBA32(srcFormat) && ALT32_CORR>0)
1363 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1364 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
1365 int srcSliceY, int srcSliceH, uint8_t *dst[],
1369 const enum AVPixelFormat srcFormat = c->srcFormat;
1370 const enum AVPixelFormat dstFormat = c->dstFormat;
1371 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
1372 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
1373 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
1374 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
1375 rgbConvFn conv = findRgbConvFn(c);
1378 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1379 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1381 const uint8_t *srcPtr = src[0];
1382 uint8_t *dstPtr = dst[0];
1383 int src_bswap = IS_NOT_NE(c->srcFormatBpp, desc_src);
1384 int dst_bswap = IS_NOT_NE(c->dstFormatBpp, desc_dst);
1386 if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
1387 !isRGBA32(dstFormat))
1388 srcPtr += ALT32_CORR;
1390 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
1391 !isRGBA32(srcFormat)) {
1393 av_assert0(ALT32_CORR == 1);
1394 for (i = 0; i < srcSliceH; i++)
1395 dstPtr[dstStride[0] * (srcSliceY + i)] = 255;
1396 dstPtr += ALT32_CORR;
1399 if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
1400 !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap)
1401 conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
1402 (srcSliceH - 1) * srcStride[0] + c->srcW * srcBpp);
1405 dstPtr += dstStride[0] * srcSliceY;
1407 for (i = 0; i < srcSliceH; i++) {
1409 for(j=0; j<c->srcW; j++)
1410 ((uint16_t*)c->formatConvBuffer)[j] = av_bswap16(((uint16_t*)srcPtr)[j]);
1411 conv(c->formatConvBuffer, dstPtr, c->srcW * srcBpp);
1413 conv(srcPtr, dstPtr, c->srcW * srcBpp);
1415 for(j=0; j<c->srcW; j++)
1416 ((uint16_t*)dstPtr)[j] = av_bswap16(((uint16_t*)dstPtr)[j]);
1417 srcPtr += srcStride[0];
1418 dstPtr += dstStride[0];
1425 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
1426 int srcStride[], int srcSliceY, int srcSliceH,
1427 uint8_t *dst[], int dstStride[])
1431 dst[0] + srcSliceY * dstStride[0],
1432 dst[1] + (srcSliceY >> 1) * dstStride[1],
1433 dst[2] + (srcSliceY >> 1) * dstStride[2],
1435 dstStride[0], dstStride[1], srcStride[0],
1436 c->input_rgb2yuv_table);
1438 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1442 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
1443 int srcStride[], int srcSliceY, int srcSliceH,
1444 uint8_t *dst[], int dstStride[])
1446 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1447 dst[0], dstStride[0]);
1449 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
1450 srcSliceH >> 2, srcStride[1], dstStride[1]);
1451 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
1452 srcSliceH >> 2, srcStride[2], dstStride[2]);
1454 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1458 /* unscaled copy like stuff (assumes nearly identical formats) */
1459 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
1460 int srcStride[], int srcSliceY, int srcSliceH,
1461 uint8_t *dst[], int dstStride[])
1463 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
1464 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
1467 const uint8_t *srcPtr = src[0];
1468 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
1471 /* universal length finder */
1472 while (length + c->srcW <= FFABS(dstStride[0]) &&
1473 length + c->srcW <= FFABS(srcStride[0]))
1475 av_assert1(length != 0);
1477 for (i = 0; i < srcSliceH; i++) {
1478 memcpy(dstPtr, srcPtr, length);
1479 srcPtr += srcStride[0];
1480 dstPtr += dstStride[0];
1486 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
1487 unsigned shift= src_depth-dst_depth, tmp;\
1488 if (c->dither == SWS_DITHER_NONE) {\
1489 for (i = 0; i < height; i++) {\
1490 for (j = 0; j < length-7; j+=8) {\
1491 dst[j+0] = dbswap(bswap(src[j+0])>>shift);\
1492 dst[j+1] = dbswap(bswap(src[j+1])>>shift);\
1493 dst[j+2] = dbswap(bswap(src[j+2])>>shift);\
1494 dst[j+3] = dbswap(bswap(src[j+3])>>shift);\
1495 dst[j+4] = dbswap(bswap(src[j+4])>>shift);\
1496 dst[j+5] = dbswap(bswap(src[j+5])>>shift);\
1497 dst[j+6] = dbswap(bswap(src[j+6])>>shift);\
1498 dst[j+7] = dbswap(bswap(src[j+7])>>shift);\
1500 for (; j < length; j++) {\
1501 dst[j] = dbswap(bswap(src[j])>>shift);\
1506 } else if (shiftonly) {\
1507 for (i = 0; i < height; i++) {\
1508 const uint8_t *dither= dithers[shift-1][i&7];\
1509 for (j = 0; j < length-7; j+=8) {\
1510 tmp = (bswap(src[j+0]) + dither[0])>>shift; dst[j+0] = dbswap(tmp - (tmp>>dst_depth));\
1511 tmp = (bswap(src[j+1]) + dither[1])>>shift; dst[j+1] = dbswap(tmp - (tmp>>dst_depth));\
1512 tmp = (bswap(src[j+2]) + dither[2])>>shift; dst[j+2] = dbswap(tmp - (tmp>>dst_depth));\
1513 tmp = (bswap(src[j+3]) + dither[3])>>shift; dst[j+3] = dbswap(tmp - (tmp>>dst_depth));\
1514 tmp = (bswap(src[j+4]) + dither[4])>>shift; dst[j+4] = dbswap(tmp - (tmp>>dst_depth));\
1515 tmp = (bswap(src[j+5]) + dither[5])>>shift; dst[j+5] = dbswap(tmp - (tmp>>dst_depth));\
1516 tmp = (bswap(src[j+6]) + dither[6])>>shift; dst[j+6] = dbswap(tmp - (tmp>>dst_depth));\
1517 tmp = (bswap(src[j+7]) + dither[7])>>shift; dst[j+7] = dbswap(tmp - (tmp>>dst_depth));\
1519 for (; j < length; j++) {\
1520 tmp = (bswap(src[j]) + dither[j&7])>>shift; dst[j] = dbswap(tmp - (tmp>>dst_depth));\
1526 for (i = 0; i < height; i++) {\
1527 const uint8_t *dither= dithers[shift-1][i&7];\
1528 for (j = 0; j < length-7; j+=8) {\
1529 tmp = bswap(src[j+0]); dst[j+0] = dbswap((tmp - (tmp>>dst_depth) + dither[0])>>shift);\
1530 tmp = bswap(src[j+1]); dst[j+1] = dbswap((tmp - (tmp>>dst_depth) + dither[1])>>shift);\
1531 tmp = bswap(src[j+2]); dst[j+2] = dbswap((tmp - (tmp>>dst_depth) + dither[2])>>shift);\
1532 tmp = bswap(src[j+3]); dst[j+3] = dbswap((tmp - (tmp>>dst_depth) + dither[3])>>shift);\
1533 tmp = bswap(src[j+4]); dst[j+4] = dbswap((tmp - (tmp>>dst_depth) + dither[4])>>shift);\
1534 tmp = bswap(src[j+5]); dst[j+5] = dbswap((tmp - (tmp>>dst_depth) + dither[5])>>shift);\
1535 tmp = bswap(src[j+6]); dst[j+6] = dbswap((tmp - (tmp>>dst_depth) + dither[6])>>shift);\
1536 tmp = bswap(src[j+7]); dst[j+7] = dbswap((tmp - (tmp>>dst_depth) + dither[7])>>shift);\
1538 for (; j < length; j++) {\
1539 tmp = bswap(src[j]); dst[j] = dbswap((tmp - (tmp>>dst_depth) + dither[j&7])>>shift);\
1546 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
1547 int srcStride[], int srcSliceY, int srcSliceH,
1548 uint8_t *dst[], int dstStride[])
1550 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
1551 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
1553 for (plane = 0; plane < 4; plane++) {
1554 int length = (plane == 0 || plane == 3) ? c->srcW : AV_CEIL_RSHIFT(c->srcW, c->chrDstHSubSample);
1555 int y = (plane == 0 || plane == 3) ? srcSliceY: AV_CEIL_RSHIFT(srcSliceY, c->chrDstVSubSample);
1556 int height = (plane == 0 || plane == 3) ? srcSliceH: AV_CEIL_RSHIFT(srcSliceH, c->chrDstVSubSample);
1557 const uint8_t *srcPtr = src[plane];
1558 uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
1559 int shiftonly = plane == 1 || plane == 2 || (!c->srcRange && plane == 0);
1563 // ignore palette for GRAY8
1564 if (plane == 1 && !dst[2]) continue;
1565 if (!src[plane] || (plane == 1 && !src[2])) {
1566 if (is16BPS(c->dstFormat) || isNBPS(c->dstFormat)) {
1567 fillPlane16(dst[plane], dstStride[plane], length, height, y,
1568 plane == 3, desc_dst->comp[plane].depth,
1569 isBE(c->dstFormat));
1571 fillPlane(dst[plane], dstStride[plane], length, height, y,
1572 (plane == 3) ? 255 : 128);
1575 if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
1576 || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
1578 const int src_depth = desc_src->comp[plane].depth;
1579 const int dst_depth = desc_dst->comp[plane].depth;
1580 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
1581 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1583 if (dst_depth == 8) {
1584 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1585 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
1587 DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
1589 } else if (src_depth == 8) {
1590 for (i = 0; i < height; i++) {
1593 for (j = 0; j < length; j++)\
1594 w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
1596 for (j = 0; j < length; j++)\
1597 w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
1598 (srcPtr[j]>>(2*8-dst_depth)));\
1600 if(isBE(c->dstFormat)){
1605 dstPtr2 += dstStride[plane]/2;
1606 srcPtr += srcStride[plane];
1608 } else if (src_depth <= dst_depth) {
1609 for (i = 0; i < height; i++) {
1611 if(isBE(c->srcFormat) == HAVE_BIGENDIAN &&
1612 isBE(c->dstFormat) == HAVE_BIGENDIAN &&
1614 unsigned shift = dst_depth - src_depth;
1616 #define FAST_COPY_UP(shift) \
1617 for (; j < length - 3; j += 4) { \
1618 uint64_t v = AV_RN64A(srcPtr2 + j); \
1619 AV_WN64A(dstPtr2 + j, v << shift); \
1622 #define FAST_COPY_UP(shift) \
1623 for (; j < length - 1; j += 2) { \
1624 uint32_t v = AV_RN32A(srcPtr2 + j); \
1625 AV_WN32A(dstPtr2 + j, v << shift); \
1630 case 6: FAST_COPY_UP(6); break;
1631 case 7: FAST_COPY_UP(7); break;
1634 #define COPY_UP(r,w) \
1636 for (; j < length; j++){ \
1637 unsigned int v= r(&srcPtr2[j]);\
1638 w(&dstPtr2[j], v<<(dst_depth-src_depth));\
1641 for (; j < length; j++){ \
1642 unsigned int v= r(&srcPtr2[j]);\
1643 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
1644 (v>>(2*src_depth-dst_depth)));\
1647 if(isBE(c->srcFormat)){
1648 if(isBE(c->dstFormat)){
1649 COPY_UP(AV_RB16, AV_WB16)
1651 COPY_UP(AV_RB16, AV_WL16)
1654 if(isBE(c->dstFormat)){
1655 COPY_UP(AV_RL16, AV_WB16)
1657 COPY_UP(AV_RL16, AV_WL16)
1660 dstPtr2 += dstStride[plane]/2;
1661 srcPtr2 += srcStride[plane]/2;
1664 if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1665 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1666 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
1668 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
1671 if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1672 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
1674 DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
1678 } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
1679 isBE(c->srcFormat) != isBE(c->dstFormat)) {
1681 for (i = 0; i < height; i++) {
1682 for (j = 0; j < length; j++)
1683 ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
1684 srcPtr += srcStride[plane];
1685 dstPtr += dstStride[plane];
1687 } else if (dstStride[plane] == srcStride[plane] &&
1688 srcStride[plane] > 0 && srcStride[plane] == length) {
1689 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
1690 height * dstStride[plane]);
1692 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1694 else if (desc_src->comp[0].depth == 1)
1695 length >>= 3; // monowhite/black
1696 for (i = 0; i < height; i++) {
1697 memcpy(dstPtr, srcPtr, length);
1698 srcPtr += srcStride[plane];
1699 dstPtr += dstStride[plane];
1708 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
1709 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
1710 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
1713 void ff_get_unscaled_swscale(SwsContext *c)
1715 const enum AVPixelFormat srcFormat = c->srcFormat;
1716 const enum AVPixelFormat dstFormat = c->dstFormat;
1717 const int flags = c->flags;
1718 const int dstH = c->dstH;
1721 needsDither = isAnyRGB(dstFormat) &&
1722 c->dstFormatBpp < 24 &&
1723 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1726 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
1727 (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
1728 c->swscale = planarToNv12Wrapper;
1731 if (dstFormat == AV_PIX_FMT_YUV420P &&
1732 (srcFormat == AV_PIX_FMT_NV12 || srcFormat == AV_PIX_FMT_NV21)) {
1733 c->swscale = nv12ToPlanarWrapper;
1736 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
1737 srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
1738 !(flags & SWS_ACCURATE_RND) && (c->dither == SWS_DITHER_BAYER || c->dither == SWS_DITHER_AUTO) && !(dstH & 1)) {
1739 c->swscale = ff_yuv2rgb_get_func_ptr(c);
1741 /* yuv420p10_to_p010 */
1742 if ((srcFormat == AV_PIX_FMT_YUV420P10 || srcFormat == AV_PIX_FMT_YUVA420P10) &&
1743 dstFormat == AV_PIX_FMT_P010) {
1744 c->swscale = planarToP010Wrapper;
1746 /* yuv420p_to_p010le */
1747 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
1748 dstFormat == AV_PIX_FMT_P010LE) {
1749 c->swscale = planar8ToP01xleWrapper;
1752 if (srcFormat == AV_PIX_FMT_YUV410P && !(dstH & 3) &&
1753 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1754 !(flags & SWS_BITEXACT)) {
1755 c->swscale = yvu9ToYv12Wrapper;
1759 if (srcFormat == AV_PIX_FMT_BGR24 &&
1760 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1761 !(flags & SWS_ACCURATE_RND))
1762 c->swscale = bgr24ToYv12Wrapper;
1764 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1765 if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
1766 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1767 c->swscale = rgbToRgbWrapper;
1769 /* RGB to planar RGB */
1770 if ((srcFormat == AV_PIX_FMT_GBRP && dstFormat == AV_PIX_FMT_GBRAP) ||
1771 (srcFormat == AV_PIX_FMT_GBRAP && dstFormat == AV_PIX_FMT_GBRP))
1772 c->swscale = planarRgbToplanarRgbWrapper;
1774 #define isByteRGB(f) ( \
1775 f == AV_PIX_FMT_RGB32 || \
1776 f == AV_PIX_FMT_RGB32_1 || \
1777 f == AV_PIX_FMT_RGB24 || \
1778 f == AV_PIX_FMT_BGR32 || \
1779 f == AV_PIX_FMT_BGR32_1 || \
1780 f == AV_PIX_FMT_BGR24)
1782 if (srcFormat == AV_PIX_FMT_GBRP && isPlanar(srcFormat) && isByteRGB(dstFormat))
1783 c->swscale = planarRgbToRgbWrapper;
1785 if ((srcFormat == AV_PIX_FMT_RGB48LE || srcFormat == AV_PIX_FMT_RGB48BE ||
1786 srcFormat == AV_PIX_FMT_BGR48LE || srcFormat == AV_PIX_FMT_BGR48BE ||
1787 srcFormat == AV_PIX_FMT_RGBA64LE || srcFormat == AV_PIX_FMT_RGBA64BE ||
1788 srcFormat == AV_PIX_FMT_BGRA64LE || srcFormat == AV_PIX_FMT_BGRA64BE) &&
1789 (dstFormat == AV_PIX_FMT_GBRP9LE || dstFormat == AV_PIX_FMT_GBRP9BE ||
1790 dstFormat == AV_PIX_FMT_GBRP10LE || dstFormat == AV_PIX_FMT_GBRP10BE ||
1791 dstFormat == AV_PIX_FMT_GBRP12LE || dstFormat == AV_PIX_FMT_GBRP12BE ||
1792 dstFormat == AV_PIX_FMT_GBRP14LE || dstFormat == AV_PIX_FMT_GBRP14BE ||
1793 dstFormat == AV_PIX_FMT_GBRP16LE || dstFormat == AV_PIX_FMT_GBRP16BE ||
1794 dstFormat == AV_PIX_FMT_GBRAP12LE || dstFormat == AV_PIX_FMT_GBRAP12BE ||
1795 dstFormat == AV_PIX_FMT_GBRAP16LE || dstFormat == AV_PIX_FMT_GBRAP16BE ))
1796 c->swscale = Rgb16ToPlanarRgb16Wrapper;
1798 if ((srcFormat == AV_PIX_FMT_GBRP9LE || srcFormat == AV_PIX_FMT_GBRP9BE ||
1799 srcFormat == AV_PIX_FMT_GBRP16LE || srcFormat == AV_PIX_FMT_GBRP16BE ||
1800 srcFormat == AV_PIX_FMT_GBRP10LE || srcFormat == AV_PIX_FMT_GBRP10BE ||
1801 srcFormat == AV_PIX_FMT_GBRP12LE || srcFormat == AV_PIX_FMT_GBRP12BE ||
1802 srcFormat == AV_PIX_FMT_GBRP14LE || srcFormat == AV_PIX_FMT_GBRP14BE ||
1803 srcFormat == AV_PIX_FMT_GBRAP12LE || srcFormat == AV_PIX_FMT_GBRAP12BE ||
1804 srcFormat == AV_PIX_FMT_GBRAP16LE || srcFormat == AV_PIX_FMT_GBRAP16BE) &&
1805 (dstFormat == AV_PIX_FMT_RGB48LE || dstFormat == AV_PIX_FMT_RGB48BE ||
1806 dstFormat == AV_PIX_FMT_BGR48LE || dstFormat == AV_PIX_FMT_BGR48BE ||
1807 dstFormat == AV_PIX_FMT_RGBA64LE || dstFormat == AV_PIX_FMT_RGBA64BE ||
1808 dstFormat == AV_PIX_FMT_BGRA64LE || dstFormat == AV_PIX_FMT_BGRA64BE))
1809 c->swscale = planarRgb16ToRgb16Wrapper;
1811 if (av_pix_fmt_desc_get(srcFormat)->comp[0].depth == 8 &&
1812 isPackedRGB(srcFormat) && dstFormat == AV_PIX_FMT_GBRP)
1813 c->swscale = rgbToPlanarRgbWrapper;
1815 if (isBayer(srcFormat)) {
1816 if (dstFormat == AV_PIX_FMT_RGB24)
1817 c->swscale = bayer_to_rgb24_wrapper;
1818 else if (dstFormat == AV_PIX_FMT_YUV420P)
1819 c->swscale = bayer_to_yv12_wrapper;
1820 else if (!isBayer(dstFormat)) {
1821 av_log(c, AV_LOG_ERROR, "unsupported bayer conversion\n");
1826 /* bswap 16 bits per pixel/component packed formats */
1827 if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_BGGR16) ||
1828 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_RGGB16) ||
1829 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GBRG16) ||
1830 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GRBG16) ||
1831 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
1832 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48) ||
1833 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
1834 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
1835 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
1836 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
1837 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY9) ||
1838 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY10) ||
1839 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY12) ||
1840 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
1841 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YA16) ||
1842 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_AYUV64) ||
1843 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP9) ||
1844 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP10) ||
1845 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP12) ||
1846 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP14) ||
1847 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP16) ||
1848 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP12) ||
1849 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP16) ||
1850 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
1851 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48) ||
1852 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
1853 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
1854 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
1855 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
1856 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12) ||
1857 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P9) ||
1858 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P10) ||
1859 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P12) ||
1860 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P14) ||
1861 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P16) ||
1862 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P9) ||
1863 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P10) ||
1864 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P12) ||
1865 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P14) ||
1866 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P16) ||
1867 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV440P10) ||
1868 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV440P12) ||
1869 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P9) ||
1870 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P10) ||
1871 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P12) ||
1872 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P14) ||
1873 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P16))
1874 c->swscale = packed_16bpc_bswap;
1876 if (usePal(srcFormat) && isByteRGB(dstFormat))
1877 c->swscale = palToRgbWrapper;
1879 if (srcFormat == AV_PIX_FMT_YUV422P) {
1880 if (dstFormat == AV_PIX_FMT_YUYV422)
1881 c->swscale = yuv422pToYuy2Wrapper;
1882 else if (dstFormat == AV_PIX_FMT_UYVY422)
1883 c->swscale = yuv422pToUyvyWrapper;
1886 /* LQ converters if -sws 0 or -sws 4*/
1887 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1889 if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
1890 if (dstFormat == AV_PIX_FMT_YUYV422)
1891 c->swscale = planarToYuy2Wrapper;
1892 else if (dstFormat == AV_PIX_FMT_UYVY422)
1893 c->swscale = planarToUyvyWrapper;
1896 if (srcFormat == AV_PIX_FMT_YUYV422 &&
1897 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1898 c->swscale = yuyvToYuv420Wrapper;
1899 if (srcFormat == AV_PIX_FMT_UYVY422 &&
1900 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1901 c->swscale = uyvyToYuv420Wrapper;
1902 if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
1903 c->swscale = yuyvToYuv422Wrapper;
1904 if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
1905 c->swscale = uyvyToYuv422Wrapper;
1907 #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_YA8 && (x) != AV_PIX_FMT_YA16LE && (x) != AV_PIX_FMT_YA16BE)
1909 if ( srcFormat == dstFormat ||
1910 (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
1911 (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
1912 (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
1913 (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
1914 (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
1915 (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
1916 c->chrDstHSubSample == c->chrSrcHSubSample &&
1917 c->chrDstVSubSample == c->chrSrcVSubSample &&
1918 dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
1919 dstFormat != AV_PIX_FMT_P010LE && dstFormat != AV_PIX_FMT_P010BE &&
1920 dstFormat != AV_PIX_FMT_P016LE && dstFormat != AV_PIX_FMT_P016BE &&
1921 srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21 &&
1922 srcFormat != AV_PIX_FMT_P010LE && srcFormat != AV_PIX_FMT_P010BE &&
1923 srcFormat != AV_PIX_FMT_P016LE && srcFormat != AV_PIX_FMT_P016BE))
1925 if (isPacked(c->srcFormat))
1926 c->swscale = packedCopyWrapper;
1927 else /* Planar YUV or gray */
1928 c->swscale = planarCopyWrapper;
1932 ff_get_unscaled_swscale_ppc(c);
1934 ff_get_unscaled_swscale_arm(c);
1936 ff_get_unscaled_swscale_aarch64(c);
1939 /* Convert the palette to the same packed 32-bit format as the palette */
1940 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1941 int num_pixels, const uint8_t *palette)
1945 for (i = 0; i < num_pixels; i++)
1946 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1949 /* Palette format: ABCD -> dst format: ABC */
1950 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1951 int num_pixels, const uint8_t *palette)
1955 for (i = 0; i < num_pixels; i++) {
1957 dst[0] = palette[src[i] * 4 + 0];
1958 dst[1] = palette[src[i] * 4 + 1];
1959 dst[2] = palette[src[i] * 4 + 2];