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, static 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 static 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
[1]);
173 interleaveBytes(src
[2], src
[1], dst
, c
->srcW
/ 2, srcSliceH
/ 2,
174 srcStride
[2], srcStride
[1], dstStride
[1]);
179 static int nv12ToPlanarWrapper(SwsContext
*c
, const uint8_t *src
[],
180 int srcStride
[], int srcSliceY
,
181 int srcSliceH
, uint8_t *dstParam
[],
184 uint8_t *dst1
= dstParam
[1] + dstStride
[1] * srcSliceY
/ 2;
185 uint8_t *dst2
= dstParam
[2] + dstStride
[2] * srcSliceY
/ 2;
187 copyPlane(src
[0], srcStride
[0], srcSliceY
, srcSliceH
, c
->srcW
,
188 dstParam
[0], dstStride
[0]);
190 if (c
->srcFormat
== AV_PIX_FMT_NV12
)
191 deinterleaveBytes(src
[1], dst1
, dst2
,c
->srcW
/ 2, srcSliceH
/ 2,
192 srcStride
[1], dstStride
[1], dstStride
[2]);
194 deinterleaveBytes(src
[1], dst2
, dst1
, c
->srcW
/ 2, srcSliceH
/ 2,
195 srcStride
[1], dstStride
[2], dstStride
[1]);
200 static int planarToYuy2Wrapper(SwsContext
*c
, const uint8_t *src
[],
201 int srcStride
[], int srcSliceY
, int srcSliceH
,
202 uint8_t *dstParam
[], int dstStride
[])
204 uint8_t *dst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
206 yv12toyuy2(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0],
207 srcStride
[1], dstStride
[0]);
212 static int planarToUyvyWrapper(SwsContext
*c
, const uint8_t *src
[],
213 int srcStride
[], int srcSliceY
, int srcSliceH
,
214 uint8_t *dstParam
[], int dstStride
[])
216 uint8_t *dst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
218 yv12touyvy(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0],
219 srcStride
[1], dstStride
[0]);
224 static int yuv422pToYuy2Wrapper(SwsContext
*c
, const uint8_t *src
[],
225 int srcStride
[], int srcSliceY
, int srcSliceH
,
226 uint8_t *dstParam
[], int dstStride
[])
228 uint8_t *dst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
230 yuv422ptoyuy2(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0],
231 srcStride
[1], dstStride
[0]);
236 static int yuv422pToUyvyWrapper(SwsContext
*c
, const uint8_t *src
[],
237 int srcStride
[], int srcSliceY
, int srcSliceH
,
238 uint8_t *dstParam
[], int dstStride
[])
240 uint8_t *dst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
242 yuv422ptouyvy(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0],
243 srcStride
[1], dstStride
[0]);
248 static int yuyvToYuv420Wrapper(SwsContext
*c
, const uint8_t *src
[],
249 int srcStride
[], int srcSliceY
, int srcSliceH
,
250 uint8_t *dstParam
[], int dstStride
[])
252 uint8_t *ydst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
253 uint8_t *udst
= dstParam
[1] + dstStride
[1] * srcSliceY
/ 2;
254 uint8_t *vdst
= dstParam
[2] + dstStride
[2] * srcSliceY
/ 2;
256 yuyvtoyuv420(ydst
, udst
, vdst
, src
[0], c
->srcW
, srcSliceH
, dstStride
[0],
257 dstStride
[1], srcStride
[0]);
260 fillPlane(dstParam
[3], dstStride
[3], c
->srcW
, srcSliceH
, srcSliceY
, 255);
265 static int yuyvToYuv422Wrapper(SwsContext
*c
, const uint8_t *src
[],
266 int srcStride
[], int srcSliceY
, int srcSliceH
,
267 uint8_t *dstParam
[], int dstStride
[])
269 uint8_t *ydst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
270 uint8_t *udst
= dstParam
[1] + dstStride
[1] * srcSliceY
;
271 uint8_t *vdst
= dstParam
[2] + dstStride
[2] * srcSliceY
;
273 yuyvtoyuv422(ydst
, udst
, vdst
, src
[0], c
->srcW
, srcSliceH
, dstStride
[0],
274 dstStride
[1], srcStride
[0]);
279 static int uyvyToYuv420Wrapper(SwsContext
*c
, const uint8_t *src
[],
280 int srcStride
[], int srcSliceY
, int srcSliceH
,
281 uint8_t *dstParam
[], int dstStride
[])
283 uint8_t *ydst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
284 uint8_t *udst
= dstParam
[1] + dstStride
[1] * srcSliceY
/ 2;
285 uint8_t *vdst
= dstParam
[2] + dstStride
[2] * srcSliceY
/ 2;
287 uyvytoyuv420(ydst
, udst
, vdst
, src
[0], c
->srcW
, srcSliceH
, dstStride
[0],
288 dstStride
[1], srcStride
[0]);
291 fillPlane(dstParam
[3], dstStride
[3], c
->srcW
, srcSliceH
, srcSliceY
, 255);
296 static int uyvyToYuv422Wrapper(SwsContext
*c
, const uint8_t *src
[],
297 int srcStride
[], int srcSliceY
, int srcSliceH
,
298 uint8_t *dstParam
[], int dstStride
[])
300 uint8_t *ydst
= dstParam
[0] + dstStride
[0] * srcSliceY
;
301 uint8_t *udst
= dstParam
[1] + dstStride
[1] * srcSliceY
;
302 uint8_t *vdst
= dstParam
[2] + dstStride
[2] * srcSliceY
;
304 uyvytoyuv422(ydst
, udst
, vdst
, src
[0], c
->srcW
, srcSliceH
, dstStride
[0],
305 dstStride
[1], srcStride
[0]);
310 static void gray8aToPacked32(const uint8_t *src
, uint8_t *dst
, int num_pixels
,
311 const uint8_t *palette
)
314 for (i
= 0; i
< num_pixels
; i
++)
315 ((uint32_t *) dst
)[i
] = ((const uint32_t *) palette
)[src
[i
<< 1]] | (src
[(i
<< 1) + 1] << 24);
318 static void gray8aToPacked32_1(const uint8_t *src
, uint8_t *dst
, int num_pixels
,
319 const uint8_t *palette
)
323 for (i
= 0; i
< num_pixels
; i
++)
324 ((uint32_t *) dst
)[i
] = ((const uint32_t *) palette
)[src
[i
<< 1]] | src
[(i
<< 1) + 1];
327 static void gray8aToPacked24(const uint8_t *src
, uint8_t *dst
, int num_pixels
,
328 const uint8_t *palette
)
332 for (i
= 0; i
< num_pixels
; i
++) {
334 dst
[0] = palette
[src
[i
<< 1] * 4 + 0];
335 dst
[1] = palette
[src
[i
<< 1] * 4 + 1];
336 dst
[2] = palette
[src
[i
<< 1] * 4 + 2];
341 static int packed_16bpc_bswap(SwsContext
*c
, const uint8_t *src
[],
342 int srcStride
[], int srcSliceY
, int srcSliceH
,
343 uint8_t *dst
[], int dstStride
[])
347 for (p
= 0; p
< 4; p
++) {
348 int srcstr
= srcStride
[p
] / 2;
349 int dststr
= dstStride
[p
] / 2;
350 uint16_t *dstPtr
= (uint16_t *) dst
[p
];
351 const uint16_t *srcPtr
= (const uint16_t *) src
[p
];
352 int min_stride
= FFMIN(FFABS(srcstr
), FFABS(dststr
));
353 if(!dstPtr
|| !srcPtr
)
355 for (i
= 0; i
< (srcSliceH
>> c
->chrDstVSubSample
); i
++) {
356 for (j
= 0; j
< min_stride
; j
++) {
357 dstPtr
[j
] = av_bswap16(srcPtr
[j
]);
367 static int palToRgbWrapper(SwsContext
*c
, const uint8_t *src
[], int srcStride
[],
368 int srcSliceY
, int srcSliceH
, uint8_t *dst
[],
371 const enum AVPixelFormat srcFormat
= c
->srcFormat
;
372 const enum AVPixelFormat dstFormat
= c
->dstFormat
;
373 void (*conv
)(const uint8_t *src
, uint8_t *dst
, int num_pixels
,
374 const uint8_t *palette
) = NULL
;
376 uint8_t *dstPtr
= dst
[0] + dstStride
[0] * srcSliceY
;
377 const uint8_t *srcPtr
= src
[0];
379 if (srcFormat
== AV_PIX_FMT_YA8
) {
381 case AV_PIX_FMT_RGB32
: conv
= gray8aToPacked32
; break;
382 case AV_PIX_FMT_BGR32
: conv
= gray8aToPacked32
; break;
383 case AV_PIX_FMT_BGR32_1
: conv
= gray8aToPacked32_1
; break;
384 case AV_PIX_FMT_RGB32_1
: conv
= gray8aToPacked32_1
; break;
385 case AV_PIX_FMT_RGB24
: conv
= gray8aToPacked24
; break;
386 case AV_PIX_FMT_BGR24
: conv
= gray8aToPacked24
; break;
388 } else if (usePal(srcFormat
)) {
390 case AV_PIX_FMT_RGB32
: conv
= sws_convertPalette8ToPacked32
; break;
391 case AV_PIX_FMT_BGR32
: conv
= sws_convertPalette8ToPacked32
; break;
392 case AV_PIX_FMT_BGR32_1
: conv
= sws_convertPalette8ToPacked32
; break;
393 case AV_PIX_FMT_RGB32_1
: conv
= sws_convertPalette8ToPacked32
; break;
394 case AV_PIX_FMT_RGB24
: conv
= sws_convertPalette8ToPacked24
; break;
395 case AV_PIX_FMT_BGR24
: conv
= sws_convertPalette8ToPacked24
; break;
400 av_log(c
, AV_LOG_ERROR
, "internal error %s -> %s converter\n",
401 av_get_pix_fmt_name(srcFormat
), av_get_pix_fmt_name(dstFormat
));
403 for (i
= 0; i
< srcSliceH
; i
++) {
404 conv(srcPtr
, dstPtr
, c
->srcW
, (uint8_t *) c
->pal_rgb
);
405 srcPtr
+= srcStride
[0];
406 dstPtr
+= dstStride
[0];
413 static void packed16togbra16(const uint8_t *src
, int srcStride
,
414 uint16_t *dst
[], int dstStride
[], int srcSliceH
,
415 int src_alpha
, int swap
, int shift
, int width
)
418 int dst_alpha
= dst
[3] != NULL
;
419 for (h
= 0; h
< srcSliceH
; h
++) {
420 uint16_t *src_line
= (uint16_t *)(src
+ srcStride
* h
);
423 if (src_alpha
&& dst_alpha
) {
424 for (x
= 0; x
< width
; x
++) {
425 dst
[0][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
426 dst
[1][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
427 dst
[2][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
428 dst
[3][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
430 } else if (dst_alpha
) {
431 for (x
= 0; x
< width
; x
++) {
432 dst
[0][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
433 dst
[1][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
434 dst
[2][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
437 } else if (src_alpha
) {
438 for (x
= 0; x
< width
; x
++) {
439 dst
[0][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
440 dst
[1][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
441 dst
[2][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
445 for (x
= 0; x
< width
; x
++) {
446 dst
[0][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
447 dst
[1][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
448 dst
[2][x
] = av_bswap16(av_bswap16(*src_line
++) >> shift
);
453 if (src_alpha
&& dst_alpha
) {
454 for (x
= 0; x
< width
; x
++) {
455 dst
[0][x
] = av_bswap16(*src_line
++ >> shift
);
456 dst
[1][x
] = av_bswap16(*src_line
++ >> shift
);
457 dst
[2][x
] = av_bswap16(*src_line
++ >> shift
);
458 dst
[3][x
] = av_bswap16(*src_line
++ >> shift
);
460 } else if (dst_alpha
) {
461 for (x
= 0; x
< width
; x
++) {
462 dst
[0][x
] = av_bswap16(*src_line
++ >> shift
);
463 dst
[1][x
] = av_bswap16(*src_line
++ >> shift
);
464 dst
[2][x
] = av_bswap16(*src_line
++ >> shift
);
467 } else if (src_alpha
) {
468 for (x
= 0; x
< width
; x
++) {
469 dst
[0][x
] = av_bswap16(*src_line
++ >> shift
);
470 dst
[1][x
] = av_bswap16(*src_line
++ >> shift
);
471 dst
[2][x
] = av_bswap16(*src_line
++ >> shift
);
475 for (x
= 0; x
< width
; x
++) {
476 dst
[0][x
] = av_bswap16(*src_line
++ >> shift
);
477 dst
[1][x
] = av_bswap16(*src_line
++ >> shift
);
478 dst
[2][x
] = av_bswap16(*src_line
++ >> shift
);
483 if (src_alpha
&& dst_alpha
) {
484 for (x
= 0; x
< width
; x
++) {
485 dst
[0][x
] = av_bswap16(*src_line
++) >> shift
;
486 dst
[1][x
] = av_bswap16(*src_line
++) >> shift
;
487 dst
[2][x
] = av_bswap16(*src_line
++) >> shift
;
488 dst
[3][x
] = av_bswap16(*src_line
++) >> shift
;
490 } else if (dst_alpha
) {
491 for (x
= 0; x
< width
; x
++) {
492 dst
[0][x
] = av_bswap16(*src_line
++) >> shift
;
493 dst
[1][x
] = av_bswap16(*src_line
++) >> shift
;
494 dst
[2][x
] = av_bswap16(*src_line
++) >> shift
;
497 } else if (src_alpha
) {
498 for (x
= 0; x
< width
; x
++) {
499 dst
[0][x
] = av_bswap16(*src_line
++) >> shift
;
500 dst
[1][x
] = av_bswap16(*src_line
++) >> shift
;
501 dst
[2][x
] = av_bswap16(*src_line
++) >> shift
;
505 for (x
= 0; x
< width
; x
++) {
506 dst
[0][x
] = av_bswap16(*src_line
++) >> shift
;
507 dst
[1][x
] = av_bswap16(*src_line
++) >> shift
;
508 dst
[2][x
] = av_bswap16(*src_line
++) >> shift
;
513 if (src_alpha
&& dst_alpha
) {
514 for (x
= 0; x
< width
; x
++) {
515 dst
[0][x
] = *src_line
++ >> shift
;
516 dst
[1][x
] = *src_line
++ >> shift
;
517 dst
[2][x
] = *src_line
++ >> shift
;
518 dst
[3][x
] = *src_line
++ >> shift
;
520 } else if (dst_alpha
) {
521 for (x
= 0; x
< width
; x
++) {
522 dst
[0][x
] = *src_line
++ >> shift
;
523 dst
[1][x
] = *src_line
++ >> shift
;
524 dst
[2][x
] = *src_line
++ >> shift
;
527 } else if (src_alpha
) {
528 for (x
= 0; x
< width
; x
++) {
529 dst
[0][x
] = *src_line
++ >> shift
;
530 dst
[1][x
] = *src_line
++ >> shift
;
531 dst
[2][x
] = *src_line
++ >> shift
;
535 for (x
= 0; x
< width
; x
++) {
536 dst
[0][x
] = *src_line
++ >> shift
;
537 dst
[1][x
] = *src_line
++ >> shift
;
538 dst
[2][x
] = *src_line
++ >> shift
;
542 for (i
= 0; i
< 4; i
++)
543 dst
[i
] += dstStride
[i
] >> 1;
547 static int Rgb16ToPlanarRgb16Wrapper(SwsContext
*c
, const uint8_t *src
[],
548 int srcStride
[], int srcSliceY
, int srcSliceH
,
549 uint8_t *dst
[], int dstStride
[])
551 uint16_t *dst2013
[] = { (uint16_t *)dst
[2], (uint16_t *)dst
[0], (uint16_t *)dst
[1], (uint16_t *)dst
[3] };
552 uint16_t *dst1023
[] = { (uint16_t *)dst
[1], (uint16_t *)dst
[0], (uint16_t *)dst
[2], (uint16_t *)dst
[3] };
553 int stride2013
[] = { dstStride
[2], dstStride
[0], dstStride
[1], dstStride
[3] };
554 int stride1023
[] = { dstStride
[1], dstStride
[0], dstStride
[2], dstStride
[3] };
555 const AVPixFmtDescriptor
*src_format
= av_pix_fmt_desc_get(c
->srcFormat
);
556 const AVPixFmtDescriptor
*dst_format
= av_pix_fmt_desc_get(c
->dstFormat
);
557 int bpc
= dst_format
->comp
[0].depth_minus1
+ 1;
558 int alpha
= src_format
->flags
& AV_PIX_FMT_FLAG_ALPHA
;
560 if ( HAVE_BIGENDIAN
&& !(src_format
->flags
& AV_PIX_FMT_FLAG_BE
) ||
561 !HAVE_BIGENDIAN
&& src_format
->flags
& AV_PIX_FMT_FLAG_BE
)
563 if ( HAVE_BIGENDIAN
&& !(dst_format
->flags
& AV_PIX_FMT_FLAG_BE
) ||
564 !HAVE_BIGENDIAN
&& dst_format
->flags
& AV_PIX_FMT_FLAG_BE
)
567 if ((dst_format
->flags
& (AV_PIX_FMT_FLAG_PLANAR
| AV_PIX_FMT_FLAG_RGB
)) !=
568 (AV_PIX_FMT_FLAG_PLANAR
| AV_PIX_FMT_FLAG_RGB
) || bpc
< 9) {
569 av_log(c
, AV_LOG_ERROR
, "unsupported conversion to planar RGB %s -> %s\n",
570 src_format
->name
, dst_format
->name
);
573 switch (c
->srcFormat
) {
574 case AV_PIX_FMT_RGB48LE
:
575 case AV_PIX_FMT_RGB48BE
:
576 case AV_PIX_FMT_RGBA64LE
:
577 case AV_PIX_FMT_RGBA64BE
:
578 packed16togbra16(src
[0] + srcSliceY
* srcStride
[0], srcStride
[0],
579 dst2013
, stride2013
, srcSliceH
, alpha
, swap
,
582 case AV_PIX_FMT_BGR48LE
:
583 case AV_PIX_FMT_BGR48BE
:
584 case AV_PIX_FMT_BGRA64LE
:
585 case AV_PIX_FMT_BGRA64BE
:
586 packed16togbra16(src
[0] + srcSliceY
* srcStride
[0], srcStride
[0],
587 dst1023
, stride1023
, srcSliceH
, alpha
, swap
,
591 av_log(c
, AV_LOG_ERROR
,
592 "unsupported conversion to planar RGB %s -> %s\n",
593 src_format
->name
, dst_format
->name
);
599 static void gbr16ptopacked16(const uint16_t *src
[], int srcStride
[],
600 uint8_t *dst
, int dstStride
, int srcSliceH
,
601 int alpha
, int swap
, int bpp
, int width
)
604 int src_alpha
= src
[3] != NULL
;
605 int scale_high
= 16 - bpp
, scale_low
= (bpp
- 8) * 2;
606 for (h
= 0; h
< srcSliceH
; h
++) {
607 uint16_t *dest
= (uint16_t *)(dst
+ dstStride
* h
);
612 if (alpha
&& !src_alpha
) {
613 for (x
= 0; x
< width
; x
++) {
614 component
= av_bswap16(src
[0][x
]);
615 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
616 component
= av_bswap16(src
[1][x
]);
617 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
618 component
= av_bswap16(src
[2][x
]);
619 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
622 } else if (alpha
&& src_alpha
) {
623 for (x
= 0; x
< width
; x
++) {
624 component
= av_bswap16(src
[0][x
]);
625 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
626 component
= av_bswap16(src
[1][x
]);
627 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
628 component
= av_bswap16(src
[2][x
]);
629 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
630 component
= av_bswap16(src
[3][x
]);
631 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
634 for (x
= 0; x
< width
; x
++) {
635 component
= av_bswap16(src
[0][x
]);
636 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
637 component
= av_bswap16(src
[1][x
]);
638 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
639 component
= av_bswap16(src
[2][x
]);
640 *dest
++ = av_bswap16(component
<< scale_high
| component
>> scale_low
);
645 if (alpha
&& !src_alpha
) {
646 for (x
= 0; x
< width
; x
++) {
647 *dest
++ = av_bswap16(src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
);
648 *dest
++ = av_bswap16(src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
);
649 *dest
++ = av_bswap16(src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
);
652 } else if (alpha
&& src_alpha
) {
653 for (x
= 0; x
< width
; x
++) {
654 *dest
++ = av_bswap16(src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
);
655 *dest
++ = av_bswap16(src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
);
656 *dest
++ = av_bswap16(src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
);
657 *dest
++ = av_bswap16(src
[3][x
] << scale_high
| src
[3][x
] >> scale_low
);
660 for (x
= 0; x
< width
; x
++) {
661 *dest
++ = av_bswap16(src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
);
662 *dest
++ = av_bswap16(src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
);
663 *dest
++ = av_bswap16(src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
);
668 if (alpha
&& !src_alpha
) {
669 for (x
= 0; x
< width
; x
++) {
670 *dest
++ = av_bswap16(src
[0][x
]) << scale_high
| av_bswap16(src
[0][x
]) >> scale_low
;
671 *dest
++ = av_bswap16(src
[1][x
]) << scale_high
| av_bswap16(src
[1][x
]) >> scale_low
;
672 *dest
++ = av_bswap16(src
[2][x
]) << scale_high
| av_bswap16(src
[2][x
]) >> scale_low
;
675 } else if (alpha
&& src_alpha
) {
676 for (x
= 0; x
< width
; x
++) {
677 *dest
++ = av_bswap16(src
[0][x
]) << scale_high
| av_bswap16(src
[0][x
]) >> scale_low
;
678 *dest
++ = av_bswap16(src
[1][x
]) << scale_high
| av_bswap16(src
[1][x
]) >> scale_low
;
679 *dest
++ = av_bswap16(src
[2][x
]) << scale_high
| av_bswap16(src
[2][x
]) >> scale_low
;
680 *dest
++ = av_bswap16(src
[3][x
]) << scale_high
| av_bswap16(src
[3][x
]) >> scale_low
;
683 for (x
= 0; x
< width
; x
++) {
684 *dest
++ = av_bswap16(src
[0][x
]) << scale_high
| av_bswap16(src
[0][x
]) >> scale_low
;
685 *dest
++ = av_bswap16(src
[1][x
]) << scale_high
| av_bswap16(src
[1][x
]) >> scale_low
;
686 *dest
++ = av_bswap16(src
[2][x
]) << scale_high
| av_bswap16(src
[2][x
]) >> scale_low
;
691 if (alpha
&& !src_alpha
) {
692 for (x
= 0; x
< width
; x
++) {
693 *dest
++ = src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
;
694 *dest
++ = src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
;
695 *dest
++ = src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
;
698 } else if (alpha
&& src_alpha
) {
699 for (x
= 0; x
< width
; x
++) {
700 *dest
++ = src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
;
701 *dest
++ = src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
;
702 *dest
++ = src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
;
703 *dest
++ = src
[3][x
] << scale_high
| src
[3][x
] >> scale_low
;
706 for (x
= 0; x
< width
; x
++) {
707 *dest
++ = src
[0][x
] << scale_high
| src
[0][x
] >> scale_low
;
708 *dest
++ = src
[1][x
] << scale_high
| src
[1][x
] >> scale_low
;
709 *dest
++ = src
[2][x
] << scale_high
| src
[2][x
] >> scale_low
;
713 for (i
= 0; i
< 3 + src_alpha
; i
++)
714 src
[i
] += srcStride
[i
] >> 1;
718 static int planarRgb16ToRgb16Wrapper(SwsContext
*c
, const uint8_t *src
[],
719 int srcStride
[], int srcSliceY
, int srcSliceH
,
720 uint8_t *dst
[], int dstStride
[])
722 const uint16_t *src102
[] = { (uint16_t *)src
[1], (uint16_t *)src
[0], (uint16_t *)src
[2], (uint16_t *)src
[3] };
723 const uint16_t *src201
[] = { (uint16_t *)src
[2], (uint16_t *)src
[0], (uint16_t *)src
[1], (uint16_t *)src
[3] };
724 int stride102
[] = { srcStride
[1], srcStride
[0], srcStride
[2], srcStride
[3] };
725 int stride201
[] = { srcStride
[2], srcStride
[0], srcStride
[1], srcStride
[3] };
726 const AVPixFmtDescriptor
*src_format
= av_pix_fmt_desc_get(c
->srcFormat
);
727 const AVPixFmtDescriptor
*dst_format
= av_pix_fmt_desc_get(c
->dstFormat
);
728 int bits_per_sample
= src_format
->comp
[0].depth_minus1
+ 1;
730 if ( HAVE_BIGENDIAN
&& !(src_format
->flags
& AV_PIX_FMT_FLAG_BE
) ||
731 !HAVE_BIGENDIAN
&& src_format
->flags
& AV_PIX_FMT_FLAG_BE
)
733 if ( HAVE_BIGENDIAN
&& !(dst_format
->flags
& AV_PIX_FMT_FLAG_BE
) ||
734 !HAVE_BIGENDIAN
&& dst_format
->flags
& AV_PIX_FMT_FLAG_BE
)
737 if ((src_format
->flags
& (AV_PIX_FMT_FLAG_PLANAR
| AV_PIX_FMT_FLAG_RGB
)) !=
738 (AV_PIX_FMT_FLAG_PLANAR
| AV_PIX_FMT_FLAG_RGB
) ||
739 bits_per_sample
<= 8) {
740 av_log(c
, AV_LOG_ERROR
, "unsupported planar RGB conversion %s -> %s\n",
741 src_format
->name
, dst_format
->name
);
744 switch (c
->dstFormat
) {
745 case AV_PIX_FMT_BGR48LE
:
746 case AV_PIX_FMT_BGR48BE
:
747 gbr16ptopacked16(src102
, stride102
,
748 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
749 srcSliceH
, 0, swap
, bits_per_sample
, c
->srcW
);
751 case AV_PIX_FMT_RGB48LE
:
752 case AV_PIX_FMT_RGB48BE
:
753 gbr16ptopacked16(src201
, stride201
,
754 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
755 srcSliceH
, 0, swap
, bits_per_sample
, c
->srcW
);
757 case AV_PIX_FMT_RGBA64LE
:
758 case AV_PIX_FMT_RGBA64BE
:
759 gbr16ptopacked16(src201
, stride201
,
760 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
761 srcSliceH
, 1, swap
, bits_per_sample
, c
->srcW
);
763 case AV_PIX_FMT_BGRA64LE
:
764 case AV_PIX_FMT_BGRA64BE
:
765 gbr16ptopacked16(src102
, stride102
,
766 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
767 srcSliceH
, 1, swap
, bits_per_sample
, c
->srcW
);
770 av_log(c
, AV_LOG_ERROR
,
771 "unsupported planar RGB conversion %s -> %s\n",
772 src_format
->name
, dst_format
->name
);
778 static void gbr24ptopacked24(const uint8_t *src
[], int srcStride
[],
779 uint8_t *dst
, int dstStride
, int srcSliceH
,
783 for (h
= 0; h
< srcSliceH
; h
++) {
784 uint8_t *dest
= dst
+ dstStride
* h
;
785 for (x
= 0; x
< width
; x
++) {
791 for (i
= 0; i
< 3; i
++)
792 src
[i
] += srcStride
[i
];
796 static void gbr24ptopacked32(const uint8_t *src
[], int srcStride
[],
797 uint8_t *dst
, int dstStride
, int srcSliceH
,
798 int alpha_first
, int width
)
801 for (h
= 0; h
< srcSliceH
; h
++) {
802 uint8_t *dest
= dst
+ dstStride
* h
;
805 for (x
= 0; x
< width
; x
++) {
812 for (x
= 0; x
< width
; x
++) {
820 for (i
= 0; i
< 3; i
++)
821 src
[i
] += srcStride
[i
];
825 static int planarRgbToRgbWrapper(SwsContext
*c
, const uint8_t *src
[],
826 int srcStride
[], int srcSliceY
, int srcSliceH
,
827 uint8_t *dst
[], int dstStride
[])
830 const uint8_t *src102
[] = { src
[1], src
[0], src
[2] };
831 const uint8_t *src201
[] = { src
[2], src
[0], src
[1] };
832 int stride102
[] = { srcStride
[1], srcStride
[0], srcStride
[2] };
833 int stride201
[] = { srcStride
[2], srcStride
[0], srcStride
[1] };
835 if (c
->srcFormat
!= AV_PIX_FMT_GBRP
) {
836 av_log(c
, AV_LOG_ERROR
, "unsupported planar RGB conversion %s -> %s\n",
837 av_get_pix_fmt_name(c
->srcFormat
),
838 av_get_pix_fmt_name(c
->dstFormat
));
842 switch (c
->dstFormat
) {
843 case AV_PIX_FMT_BGR24
:
844 gbr24ptopacked24(src102
, stride102
,
845 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
849 case AV_PIX_FMT_RGB24
:
850 gbr24ptopacked24(src201
, stride201
,
851 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
855 case AV_PIX_FMT_ARGB
:
857 case AV_PIX_FMT_RGBA
:
858 gbr24ptopacked32(src201
, stride201
,
859 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
860 srcSliceH
, alpha_first
, c
->srcW
);
863 case AV_PIX_FMT_ABGR
:
865 case AV_PIX_FMT_BGRA
:
866 gbr24ptopacked32(src102
, stride102
,
867 dst
[0] + srcSliceY
* dstStride
[0], dstStride
[0],
868 srcSliceH
, alpha_first
, c
->srcW
);
872 av_log(c
, AV_LOG_ERROR
,
873 "unsupported planar RGB conversion %s -> %s\n",
874 av_get_pix_fmt_name(c
->srcFormat
),
875 av_get_pix_fmt_name(c
->dstFormat
));
881 static int planarRgbToplanarRgbWrapper(SwsContext
*c
, const uint8_t *src
[],
882 int srcStride
[], int srcSliceY
, int srcSliceH
,
883 uint8_t *dst
[], int dstStride
[])
885 copyPlane(src
[0], srcStride
[0], srcSliceY
, srcSliceH
, c
->srcW
,
886 dst
[0], dstStride
[0]);
887 copyPlane(src
[1], srcStride
[1], srcSliceY
, srcSliceH
, c
->srcW
,
888 dst
[1], dstStride
[1]);
889 copyPlane(src
[2], srcStride
[2], srcSliceY
, srcSliceH
, c
->srcW
,
890 dst
[2], dstStride
[2]);
892 fillPlane(dst
[3], dstStride
[3], c
->srcW
, srcSliceH
, srcSliceY
, 255);
897 static void packedtogbr24p(const uint8_t *src
, int srcStride
,
898 uint8_t *dst
[], int dstStride
[], int srcSliceH
,
899 int alpha_first
, int inc_size
, int width
)
911 for (h
= 0; h
< srcSliceH
; h
++) {
912 for (x
= 0; x
< width
; x
++) {
919 src
+= srcStride
- width
* inc_size
;
920 dest
[0] += dstStride
[0];
921 dest
[1] += dstStride
[1];
922 dest
[2] += dstStride
[2];
926 static int rgbToPlanarRgbWrapper(SwsContext
*c
, const uint8_t *src
[],
927 int srcStride
[], int srcSliceY
, int srcSliceH
,
928 uint8_t *dst
[], int dstStride
[])
931 int stride102
[] = { dstStride
[1], dstStride
[0], dstStride
[2] };
932 int stride201
[] = { dstStride
[2], dstStride
[0], dstStride
[1] };
933 uint8_t *dst102
[] = { dst
[1] + srcSliceY
* dstStride
[1],
934 dst
[0] + srcSliceY
* dstStride
[0],
935 dst
[2] + srcSliceY
* dstStride
[2] };
936 uint8_t *dst201
[] = { dst
[2] + srcSliceY
* dstStride
[2],
937 dst
[0] + srcSliceY
* dstStride
[0],
938 dst
[1] + srcSliceY
* dstStride
[1] };
940 switch (c
->srcFormat
) {
941 case AV_PIX_FMT_RGB24
:
942 packedtogbr24p((const uint8_t *) src
[0], srcStride
[0], dst201
,
943 stride201
, srcSliceH
, alpha_first
, 3, c
->srcW
);
945 case AV_PIX_FMT_BGR24
:
946 packedtogbr24p((const uint8_t *) src
[0], srcStride
[0], dst102
,
947 stride102
, srcSliceH
, alpha_first
, 3, c
->srcW
);
949 case AV_PIX_FMT_ARGB
:
951 case AV_PIX_FMT_RGBA
:
952 packedtogbr24p((const uint8_t *) src
[0], srcStride
[0], dst201
,
953 stride201
, srcSliceH
, alpha_first
, 4, c
->srcW
);
955 case AV_PIX_FMT_ABGR
:
957 case AV_PIX_FMT_BGRA
:
958 packedtogbr24p((const uint8_t *) src
[0], srcStride
[0], dst102
,
959 stride102
, srcSliceH
, alpha_first
, 4, c
->srcW
);
962 av_log(c
, AV_LOG_ERROR
,
963 "unsupported planar RGB conversion %s -> %s\n",
964 av_get_pix_fmt_name(c
->srcFormat
),
965 av_get_pix_fmt_name(c
->dstFormat
));
973 #define BAYER_RENAME(x) bayer_gbrg8_to_##x
974 #include "bayer_template.c"
978 #define BAYER_RENAME(x) bayer_gbrg16le_to_##x
979 #include "bayer_template.c"
983 #define BAYER_RENAME(x) bayer_gbrg16be_to_##x
984 #include "bayer_template.c"
988 #define BAYER_RENAME(x) bayer_grbg8_to_##x
989 #include "bayer_template.c"
993 #define BAYER_RENAME(x) bayer_grbg16le_to_##x
994 #include "bayer_template.c"
998 #define BAYER_RENAME(x) bayer_grbg16be_to_##x
999 #include "bayer_template.c"
1003 #define BAYER_RENAME(x) bayer_bggr8_to_##x
1004 #include "bayer_template.c"
1008 #define BAYER_RENAME(x) bayer_bggr16le_to_##x
1009 #include "bayer_template.c"
1013 #define BAYER_RENAME(x) bayer_bggr16be_to_##x
1014 #include "bayer_template.c"
1018 #define BAYER_RENAME(x) bayer_rggb8_to_##x
1019 #include "bayer_template.c"
1023 #define BAYER_RENAME(x) bayer_rggb16le_to_##x
1024 #include "bayer_template.c"
1028 #define BAYER_RENAME(x) bayer_rggb16be_to_##x
1029 #include "bayer_template.c"
1031 static int bayer_to_rgb24_wrapper(SwsContext
*c
, const uint8_t* src
[], int srcStride
[], int srcSliceY
,
1032 int srcSliceH
, uint8_t* dst
[], int dstStride
[])
1034 uint8_t *dstPtr
= dst
[0];
1035 const uint8_t *srcPtr
= src
[0];
1037 void (*copy
) (const uint8_t *src
, int src_stride
, uint8_t *dst
, int dst_stride
, int width
);
1038 void (*interpolate
)(const uint8_t *src
, int src_stride
, uint8_t *dst
, int dst_stride
, int width
);
1040 switch(c
->srcFormat
) {
1041 #define CASE(pixfmt, prefix) \
1042 case pixfmt: copy = bayer_##prefix##_to_rgb24_copy; \
1043 interpolate = bayer_##prefix##_to_rgb24_interpolate; \
1045 CASE(AV_PIX_FMT_BAYER_BGGR8
, bggr8
)
1046 CASE(AV_PIX_FMT_BAYER_BGGR16LE
, bggr16le
)
1047 CASE(AV_PIX_FMT_BAYER_BGGR16BE
, bggr16be
)
1048 CASE(AV_PIX_FMT_BAYER_RGGB8
, rggb8
)
1049 CASE(AV_PIX_FMT_BAYER_RGGB16LE
, rggb16le
)
1050 CASE(AV_PIX_FMT_BAYER_RGGB16BE
, rggb16be
)
1051 CASE(AV_PIX_FMT_BAYER_GBRG8
, gbrg8
)
1052 CASE(AV_PIX_FMT_BAYER_GBRG16LE
, gbrg16le
)
1053 CASE(AV_PIX_FMT_BAYER_GBRG16BE
, gbrg16be
)
1054 CASE(AV_PIX_FMT_BAYER_GRBG8
, grbg8
)
1055 CASE(AV_PIX_FMT_BAYER_GRBG16LE
, grbg16le
)
1056 CASE(AV_PIX_FMT_BAYER_GRBG16BE
, grbg16be
)
1061 copy(srcPtr
, srcStride
[0], dstPtr
, dstStride
[0], c
->srcW
);
1062 srcPtr
+= 2 * srcStride
[0];
1063 dstPtr
+= 2 * dstStride
[0];
1065 for (i
= 2; i
< srcSliceH
- 2; i
+= 2) {
1066 interpolate(srcPtr
, srcStride
[0], dstPtr
, dstStride
[0], c
->srcW
);
1067 srcPtr
+= 2 * srcStride
[0];
1068 dstPtr
+= 2 * dstStride
[0];
1071 copy(srcPtr
, srcStride
[0], dstPtr
, dstStride
[0], c
->srcW
);
1075 static int bayer_to_yv12_wrapper(SwsContext
*c
, const uint8_t* src
[], int srcStride
[], int srcSliceY
,
1076 int srcSliceH
, uint8_t* dst
[], int dstStride
[])
1078 const uint8_t *srcPtr
= src
[0];
1079 uint8_t *dstY
= dst
[0];
1080 uint8_t *dstU
= dst
[1];
1081 uint8_t *dstV
= dst
[2];
1083 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
);
1084 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
);
1086 switch(c
->srcFormat
) {
1087 #define CASE(pixfmt, prefix) \
1088 case pixfmt: copy = bayer_##prefix##_to_yv12_copy; \
1089 interpolate = bayer_##prefix##_to_yv12_interpolate; \
1091 CASE(AV_PIX_FMT_BAYER_BGGR8
, bggr8
)
1092 CASE(AV_PIX_FMT_BAYER_BGGR16LE
, bggr16le
)
1093 CASE(AV_PIX_FMT_BAYER_BGGR16BE
, bggr16be
)
1094 CASE(AV_PIX_FMT_BAYER_RGGB8
, rggb8
)
1095 CASE(AV_PIX_FMT_BAYER_RGGB16LE
, rggb16le
)
1096 CASE(AV_PIX_FMT_BAYER_RGGB16BE
, rggb16be
)
1097 CASE(AV_PIX_FMT_BAYER_GBRG8
, gbrg8
)
1098 CASE(AV_PIX_FMT_BAYER_GBRG16LE
, gbrg16le
)
1099 CASE(AV_PIX_FMT_BAYER_GBRG16BE
, gbrg16be
)
1100 CASE(AV_PIX_FMT_BAYER_GRBG8
, grbg8
)
1101 CASE(AV_PIX_FMT_BAYER_GRBG16LE
, grbg16le
)
1102 CASE(AV_PIX_FMT_BAYER_GRBG16BE
, grbg16be
)
1107 copy(srcPtr
, srcStride
[0], dstY
, dstU
, dstV
, dstStride
[0], c
->srcW
, c
->input_rgb2yuv_table
);
1108 srcPtr
+= 2 * srcStride
[0];
1109 dstY
+= 2 * dstStride
[0];
1110 dstU
+= dstStride
[1];
1111 dstV
+= dstStride
[1];
1113 for (i
= 2; i
< srcSliceH
- 2; i
+= 2) {
1114 interpolate(srcPtr
, srcStride
[0], dstY
, dstU
, dstV
, dstStride
[0], c
->srcW
, c
->input_rgb2yuv_table
);
1115 srcPtr
+= 2 * srcStride
[0];
1116 dstY
+= 2 * dstStride
[0];
1117 dstU
+= dstStride
[1];
1118 dstV
+= dstStride
[1];
1121 copy(srcPtr
, srcStride
[0], dstY
, dstU
, dstV
, dstStride
[0], c
->srcW
, c
->input_rgb2yuv_table
);
1125 #define isRGBA32(x) ( \
1126 (x) == AV_PIX_FMT_ARGB \
1127 || (x) == AV_PIX_FMT_RGBA \
1128 || (x) == AV_PIX_FMT_BGRA \
1129 || (x) == AV_PIX_FMT_ABGR \
1132 #define isRGBA64(x) ( \
1133 (x) == AV_PIX_FMT_RGBA64LE \
1134 || (x) == AV_PIX_FMT_RGBA64BE \
1135 || (x) == AV_PIX_FMT_BGRA64LE \
1136 || (x) == AV_PIX_FMT_BGRA64BE \
1139 #define isRGB48(x) ( \
1140 (x) == AV_PIX_FMT_RGB48LE \
1141 || (x) == AV_PIX_FMT_RGB48BE \
1142 || (x) == AV_PIX_FMT_BGR48LE \
1143 || (x) == AV_PIX_FMT_BGR48BE \
1146 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1147 typedef void (* rgbConvFn
) (const uint8_t *, uint8_t *, int);
1148 static rgbConvFn
findRgbConvFn(SwsContext
*c
)
1150 const enum AVPixelFormat srcFormat
= c
->srcFormat
;
1151 const enum AVPixelFormat dstFormat
= c
->dstFormat
;
1152 const int srcId
= c
->srcFormatBpp
;
1153 const int dstId
= c
->dstFormatBpp
;
1154 rgbConvFn conv
= NULL
;
1156 #define IS_NOT_NE(bpp, desc) \
1157 (((bpp + 7) >> 3) == 2 && \
1158 (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
1160 #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
1162 if (isRGBA32(srcFormat
) && isRGBA32(dstFormat
)) {
1163 if ( CONV_IS(ABGR
, RGBA
)
1164 || CONV_IS(ARGB
, BGRA
)
1165 || CONV_IS(BGRA
, ARGB
)
1166 || CONV_IS(RGBA
, ABGR
)) conv
= shuffle_bytes_3210
;
1167 else if (CONV_IS(ABGR
, ARGB
)
1168 || CONV_IS(ARGB
, ABGR
)) conv
= shuffle_bytes_0321
;
1169 else if (CONV_IS(ABGR
, BGRA
)
1170 || CONV_IS(ARGB
, RGBA
)) conv
= shuffle_bytes_1230
;
1171 else if (CONV_IS(BGRA
, RGBA
)
1172 || CONV_IS(RGBA
, BGRA
)) conv
= shuffle_bytes_2103
;
1173 else if (CONV_IS(BGRA
, ABGR
)
1174 || CONV_IS(RGBA
, ARGB
)) conv
= shuffle_bytes_3012
;
1175 } else if (isRGB48(srcFormat
) && isRGB48(dstFormat
)) {
1176 if (CONV_IS(RGB48LE
, BGR48LE
)
1177 || CONV_IS(BGR48LE
, RGB48LE
)
1178 || CONV_IS(RGB48BE
, BGR48BE
)
1179 || CONV_IS(BGR48BE
, RGB48BE
)) conv
= rgb48tobgr48_nobswap
;
1180 else if (CONV_IS(RGB48LE
, BGR48BE
)
1181 || CONV_IS(BGR48LE
, RGB48BE
)
1182 || CONV_IS(RGB48BE
, BGR48LE
)
1183 || CONV_IS(BGR48BE
, RGB48LE
)) conv
= rgb48tobgr48_bswap
;
1184 } else if (isRGBA64(srcFormat
) && isRGB48(dstFormat
)) {
1185 if (CONV_IS(RGBA64LE
, BGR48LE
)
1186 || CONV_IS(BGRA64LE
, RGB48LE
)
1187 || CONV_IS(RGBA64BE
, BGR48BE
)
1188 || CONV_IS(BGRA64BE
, RGB48BE
)) conv
= rgb64tobgr48_nobswap
;
1189 else if (CONV_IS(RGBA64LE
, BGR48BE
)
1190 || CONV_IS(BGRA64LE
, RGB48BE
)
1191 || CONV_IS(RGBA64BE
, BGR48LE
)
1192 || CONV_IS(BGRA64BE
, RGB48LE
)) conv
= rgb64tobgr48_bswap
;
1193 else if (CONV_IS(RGBA64LE
, RGB48LE
)
1194 || CONV_IS(BGRA64LE
, BGR48LE
)
1195 || CONV_IS(RGBA64BE
, RGB48BE
)
1196 || CONV_IS(BGRA64BE
, BGR48BE
)) conv
= rgb64to48_nobswap
;
1197 else if (CONV_IS(RGBA64LE
, RGB48BE
)
1198 || CONV_IS(BGRA64LE
, BGR48BE
)
1199 || CONV_IS(RGBA64BE
, RGB48LE
)
1200 || CONV_IS(BGRA64BE
, BGR48LE
)) conv
= rgb64to48_bswap
;
1203 if ((isBGRinInt(srcFormat
) && isBGRinInt(dstFormat
)) ||
1204 (isRGBinInt(srcFormat
) && isRGBinInt(dstFormat
))) {
1205 switch (srcId
| (dstId
<< 16)) {
1206 case 0x000F000C: conv
= rgb12to15
; break;
1207 case 0x000F0010: conv
= rgb16to15
; break;
1208 case 0x000F0018: conv
= rgb24to15
; break;
1209 case 0x000F0020: conv
= rgb32to15
; break;
1210 case 0x0010000F: conv
= rgb15to16
; break;
1211 case 0x00100018: conv
= rgb24to16
; break;
1212 case 0x00100020: conv
= rgb32to16
; break;
1213 case 0x0018000F: conv
= rgb15to24
; break;
1214 case 0x00180010: conv
= rgb16to24
; break;
1215 case 0x00180020: conv
= rgb32to24
; break;
1216 case 0x0020000F: conv
= rgb15to32
; break;
1217 case 0x00200010: conv
= rgb16to32
; break;
1218 case 0x00200018: conv
= rgb24to32
; break;
1220 } else if ((isBGRinInt(srcFormat
) && isRGBinInt(dstFormat
)) ||
1221 (isRGBinInt(srcFormat
) && isBGRinInt(dstFormat
))) {
1222 switch (srcId
| (dstId
<< 16)) {
1223 case 0x000C000C: conv
= rgb12tobgr12
; break;
1224 case 0x000F000F: conv
= rgb15tobgr15
; break;
1225 case 0x000F0010: conv
= rgb16tobgr15
; break;
1226 case 0x000F0018: conv
= rgb24tobgr15
; break;
1227 case 0x000F0020: conv
= rgb32tobgr15
; break;
1228 case 0x0010000F: conv
= rgb15tobgr16
; break;
1229 case 0x00100010: conv
= rgb16tobgr16
; break;
1230 case 0x00100018: conv
= rgb24tobgr16
; break;
1231 case 0x00100020: conv
= rgb32tobgr16
; break;
1232 case 0x0018000F: conv
= rgb15tobgr24
; break;
1233 case 0x00180010: conv
= rgb16tobgr24
; break;
1234 case 0x00180018: conv
= rgb24tobgr24
; break;
1235 case 0x00180020: conv
= rgb32tobgr24
; break;
1236 case 0x0020000F: conv
= rgb15tobgr32
; break;
1237 case 0x00200010: conv
= rgb16tobgr32
; break;
1238 case 0x00200018: conv
= rgb24tobgr32
; break;
1242 if ((dstFormat
== AV_PIX_FMT_RGB32_1
|| dstFormat
== AV_PIX_FMT_BGR32_1
) && !isRGBA32(srcFormat
) && ALT32_CORR
<0)
1248 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1249 static int rgbToRgbWrapper(SwsContext
*c
, const uint8_t *src
[], int srcStride
[],
1250 int srcSliceY
, int srcSliceH
, uint8_t *dst
[],
1254 const enum AVPixelFormat srcFormat
= c
->srcFormat
;
1255 const enum AVPixelFormat dstFormat
= c
->dstFormat
;
1256 const AVPixFmtDescriptor
*desc_src
= av_pix_fmt_desc_get(c
->srcFormat
);
1257 const AVPixFmtDescriptor
*desc_dst
= av_pix_fmt_desc_get(c
->dstFormat
);
1258 const int srcBpp
= (c
->srcFormatBpp
+ 7) >> 3;
1259 const int dstBpp
= (c
->dstFormatBpp
+ 7) >> 3;
1260 rgbConvFn conv
= findRgbConvFn(c
);
1263 av_log(c
, AV_LOG_ERROR
, "internal error %s -> %s converter\n",
1264 av_get_pix_fmt_name(srcFormat
), av_get_pix_fmt_name(dstFormat
));
1266 const uint8_t *srcPtr
= src
[0];
1267 uint8_t *dstPtr
= dst
[0];
1268 int src_bswap
= IS_NOT_NE(c
->srcFormatBpp
, desc_src
);
1269 int dst_bswap
= IS_NOT_NE(c
->dstFormatBpp
, desc_dst
);
1271 if ((srcFormat
== AV_PIX_FMT_RGB32_1
|| srcFormat
== AV_PIX_FMT_BGR32_1
) &&
1272 !isRGBA32(dstFormat
))
1273 srcPtr
+= ALT32_CORR
;
1275 if ((dstFormat
== AV_PIX_FMT_RGB32_1
|| dstFormat
== AV_PIX_FMT_BGR32_1
) &&
1276 !isRGBA32(srcFormat
)) {
1278 av_assert0(ALT32_CORR
== 1);
1279 for (i
= 0; i
< srcSliceH
; i
++)
1280 dstPtr
[dstStride
[0] * (srcSliceY
+ i
)] = 255;
1281 dstPtr
+= ALT32_CORR
;
1284 if (dstStride
[0] * srcBpp
== srcStride
[0] * dstBpp
&& srcStride
[0] > 0 &&
1285 !(srcStride
[0] % srcBpp
) && !dst_bswap
&& !src_bswap
)
1286 conv(srcPtr
, dstPtr
+ dstStride
[0] * srcSliceY
,
1287 (srcSliceH
- 1) * srcStride
[0] + c
->srcW
* srcBpp
);
1290 dstPtr
+= dstStride
[0] * srcSliceY
;
1292 for (i
= 0; i
< srcSliceH
; i
++) {
1294 for(j
=0; j
<c
->srcW
; j
++)
1295 ((uint16_t*)c
->formatConvBuffer
)[j
] = av_bswap16(((uint16_t*)srcPtr
)[j
]);
1296 conv(c
->formatConvBuffer
, dstPtr
, c
->srcW
* srcBpp
);
1298 conv(srcPtr
, dstPtr
, c
->srcW
* srcBpp
);
1300 for(j
=0; j
<c
->srcW
; j
++)
1301 ((uint16_t*)dstPtr
)[j
] = av_bswap16(((uint16_t*)dstPtr
)[j
]);
1302 srcPtr
+= srcStride
[0];
1303 dstPtr
+= dstStride
[0];
1310 static int bgr24ToYv12Wrapper(SwsContext
*c
, const uint8_t *src
[],
1311 int srcStride
[], int srcSliceY
, int srcSliceH
,
1312 uint8_t *dst
[], int dstStride
[])
1316 dst
[0] + srcSliceY
* dstStride
[0],
1317 dst
[1] + (srcSliceY
>> 1) * dstStride
[1],
1318 dst
[2] + (srcSliceY
>> 1) * dstStride
[2],
1320 dstStride
[0], dstStride
[1], srcStride
[0],
1321 c
->input_rgb2yuv_table
);
1323 fillPlane(dst
[3], dstStride
[3], c
->srcW
, srcSliceH
, srcSliceY
, 255);
1327 static int yvu9ToYv12Wrapper(SwsContext
*c
, const uint8_t *src
[],
1328 int srcStride
[], int srcSliceY
, int srcSliceH
,
1329 uint8_t *dst
[], int dstStride
[])
1331 copyPlane(src
[0], srcStride
[0], srcSliceY
, srcSliceH
, c
->srcW
,
1332 dst
[0], dstStride
[0]);
1334 planar2x(src
[1], dst
[1] + dstStride
[1] * (srcSliceY
>> 1), c
->chrSrcW
,
1335 srcSliceH
>> 2, srcStride
[1], dstStride
[1]);
1336 planar2x(src
[2], dst
[2] + dstStride
[2] * (srcSliceY
>> 1), c
->chrSrcW
,
1337 srcSliceH
>> 2, srcStride
[2], dstStride
[2]);
1339 fillPlane(dst
[3], dstStride
[3], c
->srcW
, srcSliceH
, srcSliceY
, 255);
1343 /* unscaled copy like stuff (assumes nearly identical formats) */
1344 static int packedCopyWrapper(SwsContext
*c
, const uint8_t *src
[],
1345 int srcStride
[], int srcSliceY
, int srcSliceH
,
1346 uint8_t *dst
[], int dstStride
[])
1348 if (dstStride
[0] == srcStride
[0] && srcStride
[0] > 0)
1349 memcpy(dst
[0] + dstStride
[0] * srcSliceY
, src
[0], srcSliceH
* dstStride
[0]);
1352 const uint8_t *srcPtr
= src
[0];
1353 uint8_t *dstPtr
= dst
[0] + dstStride
[0] * srcSliceY
;
1356 /* universal length finder */
1357 while (length
+ c
->srcW
<= FFABS(dstStride
[0]) &&
1358 length
+ c
->srcW
<= FFABS(srcStride
[0]))
1360 av_assert1(length
!= 0);
1362 for (i
= 0; i
< srcSliceH
; i
++) {
1363 memcpy(dstPtr
, srcPtr
, length
);
1364 srcPtr
+= srcStride
[0];
1365 dstPtr
+= dstStride
[0];
1371 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
1372 uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
1373 int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
1374 for (i = 0; i < height; i++) {\
1375 const uint8_t *dither= dithers[src_depth-9][i&7];\
1376 for (j = 0; j < length-7; j+=8){\
1377 dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
1378 dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
1379 dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
1380 dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
1381 dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
1382 dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
1383 dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
1384 dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
1386 for (; j < length; j++)\
1387 dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
1392 static int planarCopyWrapper(SwsContext
*c
, const uint8_t *src
[],
1393 int srcStride
[], int srcSliceY
, int srcSliceH
,
1394 uint8_t *dst
[], int dstStride
[])
1396 const AVPixFmtDescriptor
*desc_src
= av_pix_fmt_desc_get(c
->srcFormat
);
1397 const AVPixFmtDescriptor
*desc_dst
= av_pix_fmt_desc_get(c
->dstFormat
);
1399 for (plane
= 0; plane
< 4; plane
++) {
1400 int length
= (plane
== 0 || plane
== 3) ? c
->srcW
: FF_CEIL_RSHIFT(c
->srcW
, c
->chrDstHSubSample
);
1401 int y
= (plane
== 0 || plane
== 3) ? srcSliceY
: FF_CEIL_RSHIFT(srcSliceY
, c
->chrDstVSubSample
);
1402 int height
= (plane
== 0 || plane
== 3) ? srcSliceH
: FF_CEIL_RSHIFT(srcSliceH
, c
->chrDstVSubSample
);
1403 const uint8_t *srcPtr
= src
[plane
];
1404 uint8_t *dstPtr
= dst
[plane
] + dstStride
[plane
] * y
;
1405 int shiftonly
= plane
==1 || plane
==2 || (!c
->srcRange
&& plane
==0);
1409 // ignore palette for GRAY8
1410 if (plane
== 1 && !dst
[2]) continue;
1411 if (!src
[plane
] || (plane
== 1 && !src
[2])) {
1412 if (is16BPS(c
->dstFormat
) || isNBPS(c
->dstFormat
)) {
1413 fillPlane16(dst
[plane
], dstStride
[plane
], length
, height
, y
,
1414 plane
== 3, desc_dst
->comp
[plane
].depth_minus1
,
1415 isBE(c
->dstFormat
));
1417 fillPlane(dst
[plane
], dstStride
[plane
], length
, height
, y
,
1418 (plane
== 3) ? 255 : 128);
1421 if(isNBPS(c
->srcFormat
) || isNBPS(c
->dstFormat
)
1422 || (is16BPS(c
->srcFormat
) != is16BPS(c
->dstFormat
))
1424 const int src_depth
= desc_src
->comp
[plane
].depth_minus1
+ 1;
1425 const int dst_depth
= desc_dst
->comp
[plane
].depth_minus1
+ 1;
1426 const uint16_t *srcPtr2
= (const uint16_t *) srcPtr
;
1427 uint16_t *dstPtr2
= (uint16_t*)dstPtr
;
1429 if (dst_depth
== 8) {
1430 if(isBE(c
->srcFormat
) == HAVE_BIGENDIAN
){
1431 DITHER_COPY(dstPtr
, dstStride
[plane
], srcPtr2
, srcStride
[plane
]/2, , )
1433 DITHER_COPY(dstPtr
, dstStride
[plane
], srcPtr2
, srcStride
[plane
]/2, av_bswap16
, )
1435 } else if (src_depth
== 8) {
1436 for (i
= 0; i
< height
; i
++) {
1439 for (j = 0; j < length; j++)\
1440 w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
1442 for (j = 0; j < length; j++)\
1443 w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
1444 (srcPtr[j]>>(2*8-dst_depth)));\
1446 if(isBE(c
->dstFormat
)){
1451 dstPtr2
+= dstStride
[plane
]/2;
1452 srcPtr
+= srcStride
[plane
];
1454 } else if (src_depth
<= dst_depth
) {
1455 for (i
= 0; i
< height
; i
++) {
1457 if(isBE(c
->srcFormat
) == HAVE_BIGENDIAN
&&
1458 isBE(c
->dstFormat
) == HAVE_BIGENDIAN
&&
1460 unsigned shift
= dst_depth
- src_depth
;
1462 #define FAST_COPY_UP(shift) \
1463 for (; j < length - 3; j += 4) { \
1464 uint64_t v = AV_RN64A(srcPtr2 + j); \
1465 AV_WN64A(dstPtr2 + j, v << shift); \
1468 #define FAST_COPY_UP(shift) \
1469 for (; j < length - 1; j += 2) { \
1470 uint32_t v = AV_RN32A(srcPtr2 + j); \
1471 AV_WN32A(dstPtr2 + j, v << shift); \
1476 case 6: FAST_COPY_UP(6); break;
1477 case 7: FAST_COPY_UP(7); break;
1480 #define COPY_UP(r,w) \
1482 for (; j < length; j++){ \
1483 unsigned int v= r(&srcPtr2[j]);\
1484 w(&dstPtr2[j], v<<(dst_depth-src_depth));\
1487 for (; j < length; j++){ \
1488 unsigned int v= r(&srcPtr2[j]);\
1489 w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
1490 (v>>(2*src_depth-dst_depth)));\
1493 if(isBE(c
->srcFormat
)){
1494 if(isBE(c
->dstFormat
)){
1495 COPY_UP(AV_RB16
, AV_WB16
)
1497 COPY_UP(AV_RB16
, AV_WL16
)
1500 if(isBE(c
->dstFormat
)){
1501 COPY_UP(AV_RL16
, AV_WB16
)
1503 COPY_UP(AV_RL16
, AV_WL16
)
1506 dstPtr2
+= dstStride
[plane
]/2;
1507 srcPtr2
+= srcStride
[plane
]/2;
1510 if(isBE(c
->srcFormat
) == HAVE_BIGENDIAN
){
1511 if(isBE(c
->dstFormat
) == HAVE_BIGENDIAN
){
1512 DITHER_COPY(dstPtr2
, dstStride
[plane
]/2, srcPtr2
, srcStride
[plane
]/2, , )
1514 DITHER_COPY(dstPtr2
, dstStride
[plane
]/2, srcPtr2
, srcStride
[plane
]/2, , av_bswap16
)
1517 if(isBE(c
->dstFormat
) == HAVE_BIGENDIAN
){
1518 DITHER_COPY(dstPtr2
, dstStride
[plane
]/2, srcPtr2
, srcStride
[plane
]/2, av_bswap16
, )
1520 DITHER_COPY(dstPtr2
, dstStride
[plane
]/2, srcPtr2
, srcStride
[plane
]/2, av_bswap16
, av_bswap16
)
1524 } else if (is16BPS(c
->srcFormat
) && is16BPS(c
->dstFormat
) &&
1525 isBE(c
->srcFormat
) != isBE(c
->dstFormat
)) {
1527 for (i
= 0; i
< height
; i
++) {
1528 for (j
= 0; j
< length
; j
++)
1529 ((uint16_t *) dstPtr
)[j
] = av_bswap16(((const uint16_t *) srcPtr
)[j
]);
1530 srcPtr
+= srcStride
[plane
];
1531 dstPtr
+= dstStride
[plane
];
1533 } else if (dstStride
[plane
] == srcStride
[plane
] &&
1534 srcStride
[plane
] > 0 && srcStride
[plane
] == length
) {
1535 memcpy(dst
[plane
] + dstStride
[plane
] * y
, src
[plane
],
1536 height
* dstStride
[plane
]);
1538 if (is16BPS(c
->srcFormat
) && is16BPS(c
->dstFormat
))
1540 else if (!desc_src
->comp
[0].depth_minus1
)
1541 length
>>= 3; // monowhite/black
1542 for (i
= 0; i
< height
; i
++) {
1543 memcpy(dstPtr
, srcPtr
, length
);
1544 srcPtr
+= srcStride
[plane
];
1545 dstPtr
+= dstStride
[plane
];
1554 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
1555 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
1556 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
1559 void ff_get_unscaled_swscale(SwsContext
*c
)
1561 const enum AVPixelFormat srcFormat
= c
->srcFormat
;
1562 const enum AVPixelFormat dstFormat
= c
->dstFormat
;
1563 const int flags
= c
->flags
;
1564 const int dstH
= c
->dstH
;
1567 needsDither
= isAnyRGB(dstFormat
) &&
1568 c
->dstFormatBpp
< 24 &&
1569 (c
->dstFormatBpp
< c
->srcFormatBpp
|| (!isAnyRGB(srcFormat
)));
1572 if ((srcFormat
== AV_PIX_FMT_YUV420P
|| srcFormat
== AV_PIX_FMT_YUVA420P
) &&
1573 (dstFormat
== AV_PIX_FMT_NV12
|| dstFormat
== AV_PIX_FMT_NV21
)) {
1574 c
->swscale
= planarToNv12Wrapper
;
1577 if (dstFormat
== AV_PIX_FMT_YUV420P
&&
1578 (srcFormat
== AV_PIX_FMT_NV12
|| srcFormat
== AV_PIX_FMT_NV21
)) {
1579 c
->swscale
= nv12ToPlanarWrapper
;
1582 if ((srcFormat
== AV_PIX_FMT_YUV420P
|| srcFormat
== AV_PIX_FMT_YUV422P
||
1583 srcFormat
== AV_PIX_FMT_YUVA420P
) && isAnyRGB(dstFormat
) &&
1584 !(flags
& SWS_ACCURATE_RND
) && (c
->dither
== SWS_DITHER_BAYER
|| c
->dither
== SWS_DITHER_AUTO
) && !(dstH
& 1)) {
1585 c
->swscale
= ff_yuv2rgb_get_func_ptr(c
);
1588 if (srcFormat
== AV_PIX_FMT_YUV410P
&& !(dstH
& 3) &&
1589 (dstFormat
== AV_PIX_FMT_YUV420P
|| dstFormat
== AV_PIX_FMT_YUVA420P
) &&
1590 !(flags
& SWS_BITEXACT
)) {
1591 c
->swscale
= yvu9ToYv12Wrapper
;
1595 if (srcFormat
== AV_PIX_FMT_BGR24
&&
1596 (dstFormat
== AV_PIX_FMT_YUV420P
|| dstFormat
== AV_PIX_FMT_YUVA420P
) &&
1597 !(flags
& SWS_ACCURATE_RND
))
1598 c
->swscale
= bgr24ToYv12Wrapper
;
1600 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1601 if (isAnyRGB(srcFormat
) && isAnyRGB(dstFormat
) && findRgbConvFn(c
)
1602 && (!needsDither
|| (c
->flags
&(SWS_FAST_BILINEAR
|SWS_POINT
))))
1603 c
->swscale
= rgbToRgbWrapper
;
1605 if ((srcFormat
== AV_PIX_FMT_GBRP
&& dstFormat
== AV_PIX_FMT_GBRAP
) ||
1606 (srcFormat
== AV_PIX_FMT_GBRAP
&& dstFormat
== AV_PIX_FMT_GBRP
))
1607 c
->swscale
= planarRgbToplanarRgbWrapper
;
1609 #define isByteRGB(f) ( \
1610 f == AV_PIX_FMT_RGB32 || \
1611 f == AV_PIX_FMT_RGB32_1 || \
1612 f == AV_PIX_FMT_RGB24 || \
1613 f == AV_PIX_FMT_BGR32 || \
1614 f == AV_PIX_FMT_BGR32_1 || \
1615 f == AV_PIX_FMT_BGR24)
1617 if (srcFormat
== AV_PIX_FMT_GBRP
&& isPlanar(srcFormat
) && isByteRGB(dstFormat
))
1618 c
->swscale
= planarRgbToRgbWrapper
;
1620 if ((srcFormat
== AV_PIX_FMT_RGB48LE
|| srcFormat
== AV_PIX_FMT_RGB48BE
||
1621 srcFormat
== AV_PIX_FMT_BGR48LE
|| srcFormat
== AV_PIX_FMT_BGR48BE
||
1622 srcFormat
== AV_PIX_FMT_RGBA64LE
|| srcFormat
== AV_PIX_FMT_RGBA64BE
||
1623 srcFormat
== AV_PIX_FMT_BGRA64LE
|| srcFormat
== AV_PIX_FMT_BGRA64BE
) &&
1624 (dstFormat
== AV_PIX_FMT_GBRP9LE
|| dstFormat
== AV_PIX_FMT_GBRP9BE
||
1625 dstFormat
== AV_PIX_FMT_GBRP10LE
|| dstFormat
== AV_PIX_FMT_GBRP10BE
||
1626 dstFormat
== AV_PIX_FMT_GBRP12LE
|| dstFormat
== AV_PIX_FMT_GBRP12BE
||
1627 dstFormat
== AV_PIX_FMT_GBRP14LE
|| dstFormat
== AV_PIX_FMT_GBRP14BE
||
1628 dstFormat
== AV_PIX_FMT_GBRP16LE
|| dstFormat
== AV_PIX_FMT_GBRP16BE
||
1629 dstFormat
== AV_PIX_FMT_GBRAP16LE
|| dstFormat
== AV_PIX_FMT_GBRAP16BE
))
1630 c
->swscale
= Rgb16ToPlanarRgb16Wrapper
;
1632 if ((srcFormat
== AV_PIX_FMT_GBRP9LE
|| srcFormat
== AV_PIX_FMT_GBRP9BE
||
1633 srcFormat
== AV_PIX_FMT_GBRP16LE
|| srcFormat
== AV_PIX_FMT_GBRP16BE
||
1634 srcFormat
== AV_PIX_FMT_GBRP10LE
|| srcFormat
== AV_PIX_FMT_GBRP10BE
||
1635 srcFormat
== AV_PIX_FMT_GBRP12LE
|| srcFormat
== AV_PIX_FMT_GBRP12BE
||
1636 srcFormat
== AV_PIX_FMT_GBRP14LE
|| srcFormat
== AV_PIX_FMT_GBRP14BE
||
1637 srcFormat
== AV_PIX_FMT_GBRAP16LE
|| srcFormat
== AV_PIX_FMT_GBRAP16BE
) &&
1638 (dstFormat
== AV_PIX_FMT_RGB48LE
|| dstFormat
== AV_PIX_FMT_RGB48BE
||
1639 dstFormat
== AV_PIX_FMT_BGR48LE
|| dstFormat
== AV_PIX_FMT_BGR48BE
||
1640 dstFormat
== AV_PIX_FMT_RGBA64LE
|| dstFormat
== AV_PIX_FMT_RGBA64BE
||
1641 dstFormat
== AV_PIX_FMT_BGRA64LE
|| dstFormat
== AV_PIX_FMT_BGRA64BE
))
1642 c
->swscale
= planarRgb16ToRgb16Wrapper
;
1644 if (av_pix_fmt_desc_get(srcFormat
)->comp
[0].depth_minus1
== 7 &&
1645 isPackedRGB(srcFormat
) && dstFormat
== AV_PIX_FMT_GBRP
)
1646 c
->swscale
= rgbToPlanarRgbWrapper
;
1648 if (isBayer(srcFormat
)) {
1649 if (dstFormat
== AV_PIX_FMT_RGB24
)
1650 c
->swscale
= bayer_to_rgb24_wrapper
;
1651 else if (dstFormat
== AV_PIX_FMT_YUV420P
)
1652 c
->swscale
= bayer_to_yv12_wrapper
;
1653 else if (!isBayer(dstFormat
)) {
1654 av_log(c
, AV_LOG_ERROR
, "unsupported bayer conversion\n");
1659 /* bswap 16 bits per pixel/component packed formats */
1660 if (IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BAYER_BGGR16
) ||
1661 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BAYER_RGGB16
) ||
1662 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BAYER_GBRG16
) ||
1663 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BAYER_GRBG16
) ||
1664 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGR444
) ||
1665 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGR48
) ||
1666 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGRA64
) ||
1667 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGR555
) ||
1668 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGR565
) ||
1669 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_BGRA64
) ||
1670 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GRAY16
) ||
1671 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YA16
) ||
1672 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRP9
) ||
1673 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRP10
) ||
1674 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRP12
) ||
1675 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRP14
) ||
1676 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRP16
) ||
1677 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_GBRAP16
) ||
1678 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGB444
) ||
1679 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGB48
) ||
1680 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGBA64
) ||
1681 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGB555
) ||
1682 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGB565
) ||
1683 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_RGBA64
) ||
1684 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_XYZ12
) ||
1685 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV420P9
) ||
1686 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV420P10
) ||
1687 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV420P12
) ||
1688 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV420P14
) ||
1689 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV420P16
) ||
1690 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV422P9
) ||
1691 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV422P10
) ||
1692 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV422P12
) ||
1693 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV422P14
) ||
1694 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV422P16
) ||
1695 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV444P9
) ||
1696 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV444P10
) ||
1697 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV444P12
) ||
1698 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV444P14
) ||
1699 IS_DIFFERENT_ENDIANESS(srcFormat
, dstFormat
, AV_PIX_FMT_YUV444P16
))
1700 c
->swscale
= packed_16bpc_bswap
;
1702 if (usePal(srcFormat
) && isByteRGB(dstFormat
))
1703 c
->swscale
= palToRgbWrapper
;
1705 if (srcFormat
== AV_PIX_FMT_YUV422P
) {
1706 if (dstFormat
== AV_PIX_FMT_YUYV422
)
1707 c
->swscale
= yuv422pToYuy2Wrapper
;
1708 else if (dstFormat
== AV_PIX_FMT_UYVY422
)
1709 c
->swscale
= yuv422pToUyvyWrapper
;
1712 /* LQ converters if -sws 0 or -sws 4*/
1713 if (c
->flags
&(SWS_FAST_BILINEAR
|SWS_POINT
)) {
1715 if (srcFormat
== AV_PIX_FMT_YUV420P
|| srcFormat
== AV_PIX_FMT_YUVA420P
) {
1716 if (dstFormat
== AV_PIX_FMT_YUYV422
)
1717 c
->swscale
= planarToYuy2Wrapper
;
1718 else if (dstFormat
== AV_PIX_FMT_UYVY422
)
1719 c
->swscale
= planarToUyvyWrapper
;
1722 if (srcFormat
== AV_PIX_FMT_YUYV422
&&
1723 (dstFormat
== AV_PIX_FMT_YUV420P
|| dstFormat
== AV_PIX_FMT_YUVA420P
))
1724 c
->swscale
= yuyvToYuv420Wrapper
;
1725 if (srcFormat
== AV_PIX_FMT_UYVY422
&&
1726 (dstFormat
== AV_PIX_FMT_YUV420P
|| dstFormat
== AV_PIX_FMT_YUVA420P
))
1727 c
->swscale
= uyvyToYuv420Wrapper
;
1728 if (srcFormat
== AV_PIX_FMT_YUYV422
&& dstFormat
== AV_PIX_FMT_YUV422P
)
1729 c
->swscale
= yuyvToYuv422Wrapper
;
1730 if (srcFormat
== AV_PIX_FMT_UYVY422
&& dstFormat
== AV_PIX_FMT_YUV422P
)
1731 c
->swscale
= uyvyToYuv422Wrapper
;
1733 #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_YA8 && (x) != AV_PIX_FMT_YA16LE && (x) != AV_PIX_FMT_YA16BE)
1735 if ( srcFormat
== dstFormat
||
1736 (srcFormat
== AV_PIX_FMT_YUVA420P
&& dstFormat
== AV_PIX_FMT_YUV420P
) ||
1737 (srcFormat
== AV_PIX_FMT_YUV420P
&& dstFormat
== AV_PIX_FMT_YUVA420P
) ||
1738 (isPlanarYUV(srcFormat
) && isPlanarGray(dstFormat
)) ||
1739 (isPlanarYUV(dstFormat
) && isPlanarGray(srcFormat
)) ||
1740 (isPlanarGray(dstFormat
) && isPlanarGray(srcFormat
)) ||
1741 (isPlanarYUV(srcFormat
) && isPlanarYUV(dstFormat
) &&
1742 c
->chrDstHSubSample
== c
->chrSrcHSubSample
&&
1743 c
->chrDstVSubSample
== c
->chrSrcVSubSample
&&
1744 dstFormat
!= AV_PIX_FMT_NV12
&& dstFormat
!= AV_PIX_FMT_NV21
&&
1745 srcFormat
!= AV_PIX_FMT_NV12
&& srcFormat
!= AV_PIX_FMT_NV21
))
1747 if (isPacked(c
->srcFormat
))
1748 c
->swscale
= packedCopyWrapper
;
1749 else /* Planar YUV or gray */
1750 c
->swscale
= planarCopyWrapper
;
1754 ff_get_unscaled_swscale_ppc(c
);
1756 // ff_get_unscaled_swscale_arm(c);
1759 /* Convert the palette to the same packed 32-bit format as the palette */
1760 void sws_convertPalette8ToPacked32(const uint8_t *src
, uint8_t *dst
,
1761 int num_pixels
, const uint8_t *palette
)
1765 for (i
= 0; i
< num_pixels
; i
++)
1766 ((uint32_t *) dst
)[i
] = ((const uint32_t *) palette
)[src
[i
]];
1769 /* Palette format: ABCD -> dst format: ABC */
1770 void sws_convertPalette8ToPacked24(const uint8_t *src
, uint8_t *dst
,
1771 int num_pixels
, const uint8_t *palette
)
1775 for (i
= 0; i
< num_pixels
; i
++) {
1777 dst
[0] = palette
[src
[i
] * 4 + 0];
1778 dst
[1] = palette
[src
[i
] * 4 + 1];
1779 dst
[2] = palette
[src
[i
] * 4 + 2];