d3863bba1089e9ed17d886a6a83bb867fff51fa1
[libav.git] / libswscale / swscale_unscaled.c
1 /*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <inttypes.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include <assert.h>
27 #include "swscale.h"
28 #include "swscale_internal.h"
29 #include "rgb2rgb.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/avutil.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/bswap.h"
35 #include "libavutil/pixdesc.h"
36
37 DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_1)[8][8] = {
38 { 0, 1, 0, 1, 0, 1, 0, 1,},
39 { 1, 0, 1, 0, 1, 0, 1, 0,},
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 };
47 DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_3)[8][8] = {
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,},
56 };
57 DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_64)[8][8] = {
58 { 18, 34, 30, 46, 17, 33, 29, 45,},
59 { 50, 2, 62, 14, 49, 1, 61, 13,},
60 { 26, 42, 22, 38, 25, 41, 21, 37,},
61 { 58, 10, 54, 6, 57, 9, 53, 5,},
62 { 16, 32, 28, 44, 19, 35, 31, 47,},
63 { 48, 0, 60, 12, 51, 3, 63, 15,},
64 { 24, 40, 20, 36, 27, 43, 23, 39,},
65 { 56, 8, 52, 4, 59, 11, 55, 7,},
66 };
67 DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_256)[8][8] = {
68 { 72, 136, 120, 184, 68, 132, 116, 180,},
69 { 200, 8, 248, 56, 196, 4, 244, 52,},
70 { 104, 168, 88, 152, 100, 164, 84, 148,},
71 { 232, 40, 216, 24, 228, 36, 212, 20,},
72 { 64, 128, 102, 176, 76, 140, 124, 188,},
73 { 192, 0, 240, 48, 204, 12, 252, 60,},
74 { 96, 160, 80, 144, 108, 172, 92, 156,},
75 { 224, 32, 208, 16, 236, 44, 220, 28,},
76 };
77
78 #define RGB2YUV_SHIFT 15
79 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
80 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
81 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
82 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
83 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
84 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
85 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
86 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
87 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
88
89 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
90 uint8_t val)
91 {
92 int i;
93 uint8_t *ptr = plane + stride * y;
94 for (i = 0; i < height; i++) {
95 memset(ptr, val, width);
96 ptr += stride;
97 }
98 }
99
100 static void fill_plane9or10(uint8_t *plane, int stride, int width,
101 int height, int y, uint8_t val,
102 const int dst_depth, const int big_endian)
103 {
104 int i, j;
105 uint16_t *dst = (uint16_t *) (plane + stride * y);
106 #define FILL8TO9_OR_10(wfunc) \
107 for (i = 0; i < height; i++) { \
108 for (j = 0; j < width; j++) { \
109 wfunc(&dst[j], (val << (dst_depth - 8)) | \
110 (val >> (16 - dst_depth))); \
111 } \
112 dst += stride / 2; \
113 }
114 if (big_endian) {
115 FILL8TO9_OR_10(AV_WB16);
116 } else {
117 FILL8TO9_OR_10(AV_WL16);
118 }
119 }
120
121 static void copyPlane(const uint8_t *src, int srcStride,
122 int srcSliceY, int srcSliceH, int width,
123 uint8_t *dst, int dstStride)
124 {
125 dst += dstStride * srcSliceY;
126 if (dstStride == srcStride && srcStride > 0) {
127 memcpy(dst, src, srcSliceH * dstStride);
128 } else {
129 int i;
130 for (i = 0; i < srcSliceH; i++) {
131 memcpy(dst, src, width);
132 src += srcStride;
133 dst += dstStride;
134 }
135 }
136 }
137
138 static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
139 int srcStride[], int srcSliceY,
140 int srcSliceH, uint8_t *dstParam[],
141 int dstStride[])
142 {
143 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
144
145 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
146 dstParam[0], dstStride[0]);
147
148 if (c->dstFormat == AV_PIX_FMT_NV12)
149 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
150 srcStride[1], srcStride[2], dstStride[1]);
151 else
152 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
153 srcStride[2], srcStride[1], dstStride[1]);
154
155 return srcSliceH;
156 }
157
158 static int nv12ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
159 int srcStride[], int srcSliceY,
160 int srcSliceH, uint8_t *dstParam[],
161 int dstStride[])
162 {
163 uint8_t *dst1 = dstParam[1] + dstStride[1] * srcSliceY / 2;
164 uint8_t *dst2 = dstParam[2] + dstStride[2] * srcSliceY / 2;
165
166 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
167 dstParam[0], dstStride[0]);
168
169 if (c->srcFormat == AV_PIX_FMT_NV12)
170 deinterleaveBytes(src[1], dst1, dst2,c->srcW / 2, srcSliceH / 2,
171 srcStride[1], dstStride[1], dstStride[2]);
172 else
173 deinterleaveBytes(src[1], dst2, dst1, c->srcW / 2, srcSliceH / 2,
174 srcStride[1], dstStride[2], dstStride[1]);
175
176 return srcSliceH;
177 }
178
179 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
180 int srcStride[], int srcSliceY, int srcSliceH,
181 uint8_t *dstParam[], int dstStride[])
182 {
183 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
184
185 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
186 srcStride[1], dstStride[0]);
187
188 return srcSliceH;
189 }
190
191 static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
192 int srcStride[], int srcSliceY, int srcSliceH,
193 uint8_t *dstParam[], int dstStride[])
194 {
195 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
196
197 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
198 srcStride[1], dstStride[0]);
199
200 return srcSliceH;
201 }
202
203 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
204 int srcStride[], int srcSliceY, int srcSliceH,
205 uint8_t *dstParam[], int dstStride[])
206 {
207 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
208
209 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
210 srcStride[1], dstStride[0]);
211
212 return srcSliceH;
213 }
214
215 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
216 int srcStride[], int srcSliceY, int srcSliceH,
217 uint8_t *dstParam[], int dstStride[])
218 {
219 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
220
221 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
222 srcStride[1], dstStride[0]);
223
224 return srcSliceH;
225 }
226
227 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
228 int srcStride[], int srcSliceY, int srcSliceH,
229 uint8_t *dstParam[], int dstStride[])
230 {
231 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
232 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
233 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
234
235 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
236 dstStride[1], srcStride[0]);
237
238 if (dstParam[3])
239 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
240
241 return srcSliceH;
242 }
243
244 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
245 int srcStride[], int srcSliceY, int srcSliceH,
246 uint8_t *dstParam[], int dstStride[])
247 {
248 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
249 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
250 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
251
252 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
253 dstStride[1], srcStride[0]);
254
255 return srcSliceH;
256 }
257
258 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
259 int srcStride[], int srcSliceY, int srcSliceH,
260 uint8_t *dstParam[], int dstStride[])
261 {
262 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
263 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
264 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
265
266 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
267 dstStride[1], srcStride[0]);
268
269 if (dstParam[3])
270 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
271
272 return srcSliceH;
273 }
274
275 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
276 int srcStride[], int srcSliceY, int srcSliceH,
277 uint8_t *dstParam[], int dstStride[])
278 {
279 uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
280 uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
281 uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
282
283 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
284 dstStride[1], srcStride[0]);
285
286 return srcSliceH;
287 }
288
289 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
290 const uint8_t *palette)
291 {
292 int i;
293 for (i = 0; i < num_pixels; i++)
294 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
295 }
296
297 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
298 const uint8_t *palette)
299 {
300 int i;
301
302 for (i = 0; i < num_pixels; i++)
303 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
304 }
305
306 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
307 const uint8_t *palette)
308 {
309 int i;
310
311 for (i = 0; i < num_pixels; i++) {
312 //FIXME slow?
313 dst[0] = palette[src[i << 1] * 4 + 0];
314 dst[1] = palette[src[i << 1] * 4 + 1];
315 dst[2] = palette[src[i << 1] * 4 + 2];
316 dst += 3;
317 }
318 }
319
320 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
321 int srcStride[], int srcSliceY, int srcSliceH,
322 uint8_t *dst[], int dstStride[])
323 {
324 int i, j;
325 int srcstr = srcStride[0] >> 1;
326 int dststr = dstStride[0] >> 1;
327 uint16_t *dstPtr = (uint16_t *) dst[0];
328 const uint16_t *srcPtr = (const uint16_t *) src[0];
329 int min_stride = FFMIN(srcstr, dststr);
330
331 for (i = 0; i < srcSliceH; i++) {
332 for (j = 0; j < min_stride; j++) {
333 dstPtr[j] = av_bswap16(srcPtr[j]);
334 }
335 srcPtr += srcstr;
336 dstPtr += dststr;
337 }
338
339 return srcSliceH;
340 }
341
342 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
343 int srcSliceY, int srcSliceH, uint8_t *dst[],
344 int dstStride[])
345 {
346 const enum AVPixelFormat srcFormat = c->srcFormat;
347 const enum AVPixelFormat dstFormat = c->dstFormat;
348 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
349 const uint8_t *palette) = NULL;
350 int i;
351 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
352 const uint8_t *srcPtr = src[0];
353
354 if (srcFormat == AV_PIX_FMT_YA8) {
355 switch (dstFormat) {
356 case AV_PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
357 case AV_PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
358 case AV_PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
359 case AV_PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
360 case AV_PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
361 case AV_PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
362 }
363 } else if (usePal(srcFormat)) {
364 switch (dstFormat) {
365 case AV_PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
366 case AV_PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
367 case AV_PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
368 case AV_PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
369 case AV_PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
370 case AV_PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
371 }
372 }
373
374 if (!conv)
375 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
376 sws_format_name(srcFormat), sws_format_name(dstFormat));
377 else {
378 for (i = 0; i < srcSliceH; i++) {
379 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
380 srcPtr += srcStride[0];
381 dstPtr += dstStride[0];
382 }
383 }
384
385 return srcSliceH;
386 }
387
388 static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
389 uint8_t *dst, int dstStride, int srcSliceH,
390 int width)
391 {
392 int x, h, i;
393 for (h = 0; h < srcSliceH; h++) {
394 uint8_t *dest = dst + dstStride * h;
395 for (x = 0; x < width; x++) {
396 *dest++ = src[0][x];
397 *dest++ = src[1][x];
398 *dest++ = src[2][x];
399 }
400
401 for (i = 0; i < 3; i++)
402 src[i] += srcStride[i];
403 }
404 }
405
406 static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
407 uint8_t *dst, int dstStride, int srcSliceH,
408 int alpha_first, int width)
409 {
410 int x, h, i;
411 for (h = 0; h < srcSliceH; h++) {
412 uint8_t *dest = dst + dstStride * h;
413
414 if (alpha_first) {
415 for (x = 0; x < width; x++) {
416 *dest++ = 0xff;
417 *dest++ = src[0][x];
418 *dest++ = src[1][x];
419 *dest++ = src[2][x];
420 }
421 } else {
422 for (x = 0; x < width; x++) {
423 *dest++ = src[0][x];
424 *dest++ = src[1][x];
425 *dest++ = src[2][x];
426 *dest++ = 0xff;
427 }
428 }
429
430 for (i = 0; i < 3; i++)
431 src[i] += srcStride[i];
432 }
433 }
434
435 static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
436 int srcStride[], int srcSliceY, int srcSliceH,
437 uint8_t *dst[], int dstStride[])
438 {
439 int alpha_first = 0;
440 const uint8_t *src102[] = { src[1], src[0], src[2] };
441 const uint8_t *src201[] = { src[2], src[0], src[1] };
442 int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
443 int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
444
445 if (c->srcFormat != AV_PIX_FMT_GBRP) {
446 av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
447 av_get_pix_fmt_name(c->srcFormat),
448 av_get_pix_fmt_name(c->dstFormat));
449 return srcSliceH;
450 }
451
452 switch (c->dstFormat) {
453 case AV_PIX_FMT_BGR24:
454 gbr24ptopacked24(src102, stride102,
455 dst[0] + srcSliceY * dstStride[0], dstStride[0],
456 srcSliceH, c->srcW);
457 break;
458
459 case AV_PIX_FMT_RGB24:
460 gbr24ptopacked24(src201, stride201,
461 dst[0] + srcSliceY * dstStride[0], dstStride[0],
462 srcSliceH, c->srcW);
463 break;
464
465 case AV_PIX_FMT_ARGB:
466 alpha_first = 1;
467 case AV_PIX_FMT_RGBA:
468 gbr24ptopacked32(src201, stride201,
469 dst[0] + srcSliceY * dstStride[0], dstStride[0],
470 srcSliceH, alpha_first, c->srcW);
471 break;
472
473 case AV_PIX_FMT_ABGR:
474 alpha_first = 1;
475 case AV_PIX_FMT_BGRA:
476 gbr24ptopacked32(src102, stride102,
477 dst[0] + srcSliceY * dstStride[0], dstStride[0],
478 srcSliceH, alpha_first, c->srcW);
479 break;
480
481 default:
482 av_log(c, AV_LOG_ERROR,
483 "unsupported planar RGB conversion %s -> %s\n",
484 av_get_pix_fmt_name(c->srcFormat),
485 av_get_pix_fmt_name(c->dstFormat));
486 }
487
488 return srcSliceH;
489 }
490
491 static int planarRgbToplanarRgbWrapper(SwsContext *c,
492 const uint8_t *src[], int srcStride[],
493 int srcSliceY, int srcSliceH,
494 uint8_t *dst[], int dstStride[])
495 {
496 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
497 dst[0], dstStride[0]);
498 copyPlane(src[1], srcStride[1], srcSliceY, srcSliceH, c->srcW,
499 dst[1], dstStride[1]);
500 copyPlane(src[2], srcStride[2], srcSliceY, srcSliceH, c->srcW,
501 dst[2], dstStride[2]);
502 if (dst[3])
503 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
504
505 return srcSliceH;
506 }
507
508 static void packedtogbr24p(const uint8_t *src, int srcStride,
509 uint8_t *dst[], int dstStride[], int srcSliceH,
510 int alpha_first, int inc_size, int width)
511 {
512 uint8_t *dest[3];
513 int x, h;
514
515 dest[0] = dst[0];
516 dest[1] = dst[1];
517 dest[2] = dst[2];
518
519 if (alpha_first)
520 src++;
521
522 for (h = 0; h < srcSliceH; h++) {
523 for (x = 0; x < width; x++) {
524 dest[0][x] = src[0];
525 dest[1][x] = src[1];
526 dest[2][x] = src[2];
527
528 src += inc_size;
529 }
530 src += srcStride - width * inc_size;
531 dest[0] += dstStride[0];
532 dest[1] += dstStride[1];
533 dest[2] += dstStride[2];
534 }
535 }
536
537 static int rgbToPlanarRgbWrapper(SwsContext *c, const uint8_t *src[],
538 int srcStride[], int srcSliceY, int srcSliceH,
539 uint8_t *dst[], int dstStride[])
540 {
541 int alpha_first = 0;
542 int stride102[] = { dstStride[1], dstStride[0], dstStride[2] };
543 int stride201[] = { dstStride[2], dstStride[0], dstStride[1] };
544 uint8_t *dst102[] = { dst[1] + srcSliceY * dstStride[1],
545 dst[0] + srcSliceY * dstStride[0],
546 dst[2] + srcSliceY * dstStride[2] };
547 uint8_t *dst201[] = { dst[2] + srcSliceY * dstStride[2],
548 dst[0] + srcSliceY * dstStride[0],
549 dst[1] + srcSliceY * dstStride[1] };
550
551 switch (c->srcFormat) {
552 case AV_PIX_FMT_RGB24:
553 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
554 stride201, srcSliceH, alpha_first, 3, c->srcW);
555 break;
556 case AV_PIX_FMT_BGR24:
557 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
558 stride102, srcSliceH, alpha_first, 3, c->srcW);
559 break;
560 case AV_PIX_FMT_ARGB:
561 alpha_first = 1;
562 case AV_PIX_FMT_RGBA:
563 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
564 stride201, srcSliceH, alpha_first, 4, c->srcW);
565 break;
566 case AV_PIX_FMT_ABGR:
567 alpha_first = 1;
568 case AV_PIX_FMT_BGRA:
569 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
570 stride102, srcSliceH, alpha_first, 4, c->srcW);
571 break;
572 default:
573 av_log(c, AV_LOG_ERROR,
574 "unsupported planar RGB conversion %s -> %s\n",
575 av_get_pix_fmt_name(c->srcFormat),
576 av_get_pix_fmt_name(c->dstFormat));
577 }
578
579 return srcSliceH;
580 }
581
582 #define isRGBA32(x) ( \
583 (x) == AV_PIX_FMT_ARGB \
584 || (x) == AV_PIX_FMT_RGBA \
585 || (x) == AV_PIX_FMT_BGRA \
586 || (x) == AV_PIX_FMT_ABGR \
587 )
588
589 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
590 typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
591 static rgbConvFn findRgbConvFn(SwsContext *c)
592 {
593 const enum AVPixelFormat srcFormat = c->srcFormat;
594 const enum AVPixelFormat dstFormat = c->dstFormat;
595 const int srcId = c->srcFormatBpp;
596 const int dstId = c->dstFormatBpp;
597 rgbConvFn conv = NULL;
598 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
599 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
600
601 #define IS_NOT_NE(bpp, desc) \
602 (((bpp + 7) >> 3) == 2 && \
603 (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
604
605 /* if this is non-native rgb444/555/565, don't handle it here. */
606 if (IS_NOT_NE(srcId, desc_src) || IS_NOT_NE(dstId, desc_dst))
607 return NULL;
608
609 #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
610
611 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
612 if ( CONV_IS(ABGR, RGBA)
613 || CONV_IS(ARGB, BGRA)
614 || CONV_IS(BGRA, ARGB)
615 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
616 else if (CONV_IS(ABGR, ARGB)
617 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
618 else if (CONV_IS(ABGR, BGRA)
619 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
620 else if (CONV_IS(BGRA, RGBA)
621 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
622 else if (CONV_IS(BGRA, ABGR)
623 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
624 } else
625 /* BGR -> BGR */
626 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
627 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
628 switch (srcId | (dstId << 16)) {
629 case 0x000F000C: conv = rgb12to15; break;
630 case 0x000F0010: conv = rgb16to15; break;
631 case 0x000F0018: conv = rgb24to15; break;
632 case 0x000F0020: conv = rgb32to15; break;
633 case 0x0010000F: conv = rgb15to16; break;
634 case 0x00100018: conv = rgb24to16; break;
635 case 0x00100020: conv = rgb32to16; break;
636 case 0x0018000F: conv = rgb15to24; break;
637 case 0x00180010: conv = rgb16to24; break;
638 case 0x00180020: conv = rgb32to24; break;
639 case 0x0020000F: conv = rgb15to32; break;
640 case 0x00200010: conv = rgb16to32; break;
641 case 0x00200018: conv = rgb24to32; break;
642 }
643 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
644 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
645 switch (srcId | (dstId << 16)) {
646 case 0x000C000C: conv = rgb12tobgr12; break;
647 case 0x000F000F: conv = rgb15tobgr15; break;
648 case 0x000F0010: conv = rgb16tobgr15; break;
649 case 0x000F0018: conv = rgb24tobgr15; break;
650 case 0x000F0020: conv = rgb32tobgr15; break;
651 case 0x0010000F: conv = rgb15tobgr16; break;
652 case 0x00100010: conv = rgb16tobgr16; break;
653 case 0x00100018: conv = rgb24tobgr16; break;
654 case 0x00100020: conv = rgb32tobgr16; break;
655 case 0x0018000F: conv = rgb15tobgr24; break;
656 case 0x00180010: conv = rgb16tobgr24; break;
657 case 0x00180018: conv = rgb24tobgr24; break;
658 case 0x00180020: conv = rgb32tobgr24; break;
659 case 0x0020000F: conv = rgb15tobgr32; break;
660 case 0x00200010: conv = rgb16tobgr32; break;
661 case 0x00200018: conv = rgb24tobgr32; break;
662 }
663 }
664
665 return conv;
666 }
667
668 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
669 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
670 int srcSliceY, int srcSliceH, uint8_t *dst[],
671 int dstStride[])
672
673 {
674 const enum AVPixelFormat srcFormat = c->srcFormat;
675 const enum AVPixelFormat dstFormat = c->dstFormat;
676 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
677 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
678 rgbConvFn conv = findRgbConvFn(c);
679
680 if (!conv) {
681 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
682 sws_format_name(srcFormat), sws_format_name(dstFormat));
683 } else {
684 const uint8_t *srcPtr = src[0];
685 uint8_t *dstPtr = dst[0];
686 if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
687 !isRGBA32(dstFormat))
688 srcPtr += ALT32_CORR;
689
690 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
691 !isRGBA32(srcFormat))
692 dstPtr += ALT32_CORR;
693
694 if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
695 !(srcStride[0] % srcBpp))
696 conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
697 (srcSliceH - 1) * srcStride[0] + c->srcW * srcBpp);
698 else {
699 int i;
700 dstPtr += dstStride[0] * srcSliceY;
701
702 for (i = 0; i < srcSliceH; i++) {
703 conv(srcPtr, dstPtr, c->srcW * srcBpp);
704 srcPtr += srcStride[0];
705 dstPtr += dstStride[0];
706 }
707 }
708 }
709 return srcSliceH;
710 }
711
712 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
713 int srcStride[], int srcSliceY, int srcSliceH,
714 uint8_t *dst[], int dstStride[])
715 {
716 rgb24toyv12(
717 src[0],
718 dst[0] + srcSliceY * dstStride[0],
719 dst[1] + (srcSliceY >> 1) * dstStride[1],
720 dst[2] + (srcSliceY >> 1) * dstStride[2],
721 c->srcW, srcSliceH,
722 dstStride[0], dstStride[1], srcStride[0]);
723 if (dst[3])
724 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
725 return srcSliceH;
726 }
727
728 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
729 int srcStride[], int srcSliceY, int srcSliceH,
730 uint8_t *dst[], int dstStride[])
731 {
732 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
733 dst[0], dstStride[0]);
734
735 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
736 srcSliceH >> 2, srcStride[1], dstStride[1]);
737 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
738 srcSliceH >> 2, srcStride[2], dstStride[2]);
739 if (dst[3])
740 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
741 return srcSliceH;
742 }
743
744 /* unscaled copy like stuff (assumes nearly identical formats) */
745 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
746 int srcStride[], int srcSliceY, int srcSliceH,
747 uint8_t *dst[], int dstStride[])
748 {
749 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
750 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
751 else {
752 int i;
753 const uint8_t *srcPtr = src[0];
754 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
755 int length = 0;
756
757 /* universal length finder */
758 while (length + c->srcW <= FFABS(dstStride[0]) &&
759 length + c->srcW <= FFABS(srcStride[0]))
760 length += c->srcW;
761 assert(length != 0);
762
763 for (i = 0; i < srcSliceH; i++) {
764 memcpy(dstPtr, srcPtr, length);
765 srcPtr += srcStride[0];
766 dstPtr += dstStride[0];
767 }
768 }
769 return srcSliceH;
770 }
771
772 #define clip9(x) av_clip_uintp2(x, 9)
773 #define clip10(x) av_clip_uintp2(x, 10)
774 #define DITHER_COPY(dst, dstStride, wfunc, src, srcStride, rfunc, dithers, shift, clip) \
775 for (i = 0; i < height; i++) { \
776 const uint8_t *dither = dithers[i & 7]; \
777 for (j = 0; j < length - 7; j += 8) { \
778 wfunc(&dst[j + 0], clip((rfunc(&src[j + 0]) + dither[0]) >> shift)); \
779 wfunc(&dst[j + 1], clip((rfunc(&src[j + 1]) + dither[1]) >> shift)); \
780 wfunc(&dst[j + 2], clip((rfunc(&src[j + 2]) + dither[2]) >> shift)); \
781 wfunc(&dst[j + 3], clip((rfunc(&src[j + 3]) + dither[3]) >> shift)); \
782 wfunc(&dst[j + 4], clip((rfunc(&src[j + 4]) + dither[4]) >> shift)); \
783 wfunc(&dst[j + 5], clip((rfunc(&src[j + 5]) + dither[5]) >> shift)); \
784 wfunc(&dst[j + 6], clip((rfunc(&src[j + 6]) + dither[6]) >> shift)); \
785 wfunc(&dst[j + 7], clip((rfunc(&src[j + 7]) + dither[7]) >> shift)); \
786 } \
787 for (; j < length; j++) \
788 wfunc(&dst[j], (rfunc(&src[j]) + dither[j & 7]) >> shift); \
789 dst += dstStride; \
790 src += srcStride; \
791 }
792
793 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
794 int srcStride[], int srcSliceY, int srcSliceH,
795 uint8_t *dst[], int dstStride[])
796 {
797 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
798 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
799 int plane, i, j;
800 for (plane = 0; plane < 4; plane++) {
801 int length = (plane == 0 || plane == 3) ? c->srcW : AV_CEIL_RSHIFT(c->srcW, c->chrDstHSubSample);
802 int y = (plane == 0 || plane == 3) ? srcSliceY: AV_CEIL_RSHIFT(srcSliceY, c->chrDstVSubSample);
803 int height = (plane == 0 || plane == 3) ? srcSliceH: AV_CEIL_RSHIFT(srcSliceH, c->chrDstVSubSample);
804 const uint8_t *srcPtr = src[plane];
805 uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
806 int shiftonly = plane == 1 || plane == 2 || (!c->srcRange && plane == 0);
807
808 if (!dst[plane])
809 continue;
810 // ignore palette for GRAY8
811 if (plane == 1 && !dst[2]) continue;
812 if (!src[plane] || (plane == 1 && !src[2])) {
813 int val = (plane == 3) ? 255 : 128;
814 if (is16BPS(c->dstFormat))
815 length *= 2;
816 if (is9_15BPS(c->dstFormat)) {
817 fill_plane9or10(dst[plane], dstStride[plane],
818 length, height, y, val,
819 desc_dst->comp[plane].depth,
820 isBE(c->dstFormat));
821 } else
822 fillPlane(dst[plane], dstStride[plane], length, height, y,
823 val);
824 } else {
825 if (is9_15BPS(c->srcFormat)) {
826 const int src_depth = desc_src->comp[plane].depth;
827 const int dst_depth = desc_dst->comp[plane].depth;
828 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
829
830 if (is16BPS(c->dstFormat)) {
831 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
832 #define COPY9_OR_10TO16(rfunc, wfunc) \
833 if (shiftonly) { \
834 for (i = 0; i < height; i++) { \
835 for (j = 0; j < length; j++) { \
836 int srcpx = rfunc(&srcPtr2[j]); \
837 wfunc(&dstPtr2[j], srcpx << (16 - src_depth)); \
838 } \
839 dstPtr2 += dstStride[plane] / 2; \
840 srcPtr2 += srcStride[plane] / 2; \
841 } \
842 } else { \
843 for (i = 0; i < height; i++) { \
844 for (j = 0; j < length; j++) { \
845 int srcpx = rfunc(&srcPtr2[j]); \
846 wfunc(&dstPtr2[j], (srcpx << (16 - src_depth)) | (srcpx >> (2 * src_depth - 16))); \
847 } \
848 dstPtr2 += dstStride[plane] / 2; \
849 srcPtr2 += srcStride[plane] / 2; \
850 } \
851 }
852 if (isBE(c->dstFormat)) {
853 if (isBE(c->srcFormat)) {
854 COPY9_OR_10TO16(AV_RB16, AV_WB16);
855 } else {
856 COPY9_OR_10TO16(AV_RL16, AV_WB16);
857 }
858 } else {
859 if (isBE(c->srcFormat)) {
860 COPY9_OR_10TO16(AV_RB16, AV_WL16);
861 } else {
862 COPY9_OR_10TO16(AV_RL16, AV_WL16);
863 }
864 }
865 } else if (is9_15BPS(c->dstFormat)) {
866 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
867 #define COPY9_OR_10TO9_OR_10(loop) \
868 for (i = 0; i < height; i++) { \
869 for (j = 0; j < length; j++) { \
870 loop; \
871 } \
872 dstPtr2 += dstStride[plane] / 2; \
873 srcPtr2 += srcStride[plane] / 2; \
874 }
875 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
876 if (dst_depth > src_depth) { \
877 COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
878 wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
879 } else if (dst_depth < src_depth) { \
880 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
881 srcPtr2, srcStride[plane] / 2, rfunc, \
882 dither_8x8_1, 1, clip9); \
883 } else { \
884 COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
885 }
886 if (isBE(c->dstFormat)) {
887 if (isBE(c->srcFormat)) {
888 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
889 } else {
890 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
891 }
892 } else {
893 if (isBE(c->srcFormat)) {
894 COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
895 } else {
896 COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
897 }
898 }
899 } else {
900 #define W8(a, b) { *(a) = (b); }
901 #define COPY9_OR_10TO8(rfunc) \
902 if (src_depth == 9) { \
903 DITHER_COPY(dstPtr, dstStride[plane], W8, \
904 srcPtr2, srcStride[plane] / 2, rfunc, \
905 dither_8x8_1, 1, av_clip_uint8); \
906 } else { \
907 DITHER_COPY(dstPtr, dstStride[plane], W8, \
908 srcPtr2, srcStride[plane] / 2, rfunc, \
909 dither_8x8_3, 2, av_clip_uint8); \
910 }
911 if (isBE(c->srcFormat)) {
912 COPY9_OR_10TO8(AV_RB16);
913 } else {
914 COPY9_OR_10TO8(AV_RL16);
915 }
916 }
917 } else if (is9_15BPS(c->dstFormat)) {
918 const int dst_depth = desc_dst->comp[plane].depth;
919 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
920
921 if (is16BPS(c->srcFormat)) {
922 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
923 #define COPY16TO9_OR_10(rfunc, wfunc) \
924 if (dst_depth == 9) { \
925 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
926 srcPtr2, srcStride[plane] / 2, rfunc, \
927 ff_dither_8x8_128, 7, clip9); \
928 } else { \
929 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
930 srcPtr2, srcStride[plane] / 2, rfunc, \
931 dither_8x8_64, 6, clip10); \
932 }
933 if (isBE(c->dstFormat)) {
934 if (isBE(c->srcFormat)) {
935 COPY16TO9_OR_10(AV_RB16, AV_WB16);
936 } else {
937 COPY16TO9_OR_10(AV_RL16, AV_WB16);
938 }
939 } else {
940 if (isBE(c->srcFormat)) {
941 COPY16TO9_OR_10(AV_RB16, AV_WL16);
942 } else {
943 COPY16TO9_OR_10(AV_RL16, AV_WL16);
944 }
945 }
946 } else /* 8 bits */ {
947 #define COPY8TO9_OR_10(wfunc) \
948 if (shiftonly) { \
949 for (i = 0; i < height; i++) { \
950 for (j = 0; j < length; j++) { \
951 const int srcpx = srcPtr[j]; \
952 wfunc(&dstPtr2[j], srcpx << (dst_depth - 8)); \
953 } \
954 dstPtr2 += dstStride[plane] / 2; \
955 srcPtr += srcStride[plane]; \
956 } \
957 } else { \
958 for (i = 0; i < height; i++) { \
959 for (j = 0; j < length; j++) { \
960 const int srcpx = srcPtr[j]; \
961 wfunc(&dstPtr2[j], (srcpx << (dst_depth - 8)) | (srcpx >> (16 - dst_depth))); \
962 } \
963 dstPtr2 += dstStride[plane] / 2; \
964 srcPtr += srcStride[plane]; \
965 } \
966 }
967 if (isBE(c->dstFormat)) {
968 COPY8TO9_OR_10(AV_WB16);
969 } else {
970 COPY8TO9_OR_10(AV_WL16);
971 }
972 }
973 } else if (is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
974 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
975 #define COPY16TO8(rfunc) \
976 DITHER_COPY(dstPtr, dstStride[plane], W8, \
977 srcPtr2, srcStride[plane] / 2, rfunc, \
978 dither_8x8_256, 8, av_clip_uint8);
979 if (isBE(c->srcFormat)) {
980 COPY16TO8(AV_RB16);
981 } else {
982 COPY16TO8(AV_RL16);
983 }
984 } else if (!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
985 for (i = 0; i < height; i++) {
986 for (j = 0; j < length; j++) {
987 dstPtr[ j << 1 ] = srcPtr[j];
988 dstPtr[(j << 1) + 1] = srcPtr[j];
989 }
990 srcPtr += srcStride[plane];
991 dstPtr += dstStride[plane];
992 }
993 } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
994 isBE(c->srcFormat) != isBE(c->dstFormat)) {
995
996 for (i = 0; i < height; i++) {
997 for (j = 0; j < length; j++)
998 ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
999 srcPtr += srcStride[plane];
1000 dstPtr += dstStride[plane];
1001 }
1002 } else if (dstStride[plane] == srcStride[plane] &&
1003 srcStride[plane] > 0 && srcStride[plane] == length) {
1004 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
1005 height * dstStride[plane]);
1006 } else {
1007 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1008 length *= 2;
1009 else if (desc_src->comp[0].depth == 1)
1010 length >>= 3; // monowhite/black
1011 for (i = 0; i < height; i++) {
1012 memcpy(dstPtr, srcPtr, length);
1013 srcPtr += srcStride[plane];
1014 dstPtr += dstStride[plane];
1015 }
1016 }
1017 }
1018 }
1019 return srcSliceH;
1020 }
1021
1022
1023 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
1024 ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
1025 (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
1026
1027
1028 void ff_get_unscaled_swscale(SwsContext *c)
1029 {
1030 const enum AVPixelFormat srcFormat = c->srcFormat;
1031 const enum AVPixelFormat dstFormat = c->dstFormat;
1032 const int flags = c->flags;
1033 const int dstH = c->dstH;
1034 int needsDither;
1035
1036 needsDither = isAnyRGB(dstFormat) &&
1037 c->dstFormatBpp < 24 &&
1038 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1039
1040 /* yv12_to_nv12 */
1041 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
1042 (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
1043 c->swscale = planarToNv12Wrapper;
1044 }
1045 /* nv12_to_yv12 */
1046 if (dstFormat == AV_PIX_FMT_YUV420P &&
1047 (srcFormat == AV_PIX_FMT_NV12 || srcFormat == AV_PIX_FMT_NV21)) {
1048 c->swscale = nv12ToPlanarWrapper;
1049 }
1050 /* yuv2bgr */
1051 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
1052 srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
1053 !(flags & SWS_ACCURATE_RND) && !(dstH & 1)) {
1054 c->swscale = ff_yuv2rgb_get_func_ptr(c);
1055 }
1056
1057 if (srcFormat == AV_PIX_FMT_YUV410P &&
1058 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1059 !(flags & SWS_BITEXACT)) {
1060 c->swscale = yvu9ToYv12Wrapper;
1061 }
1062
1063 /* bgr24toYV12 */
1064 if (srcFormat == AV_PIX_FMT_BGR24 &&
1065 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1066 !(flags & SWS_ACCURATE_RND))
1067 c->swscale = bgr24ToYv12Wrapper;
1068
1069 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1070 if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
1071 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1072 c->swscale = rgbToRgbWrapper;
1073
1074 /* RGB to planar RGB */
1075 if ((srcFormat == AV_PIX_FMT_GBRP && dstFormat == AV_PIX_FMT_GBRAP) ||
1076 (srcFormat == AV_PIX_FMT_GBRAP && dstFormat == AV_PIX_FMT_GBRP))
1077 c->swscale = planarRgbToplanarRgbWrapper;
1078
1079 #define isByteRGB(f) ( \
1080 f == AV_PIX_FMT_RGB32 || \
1081 f == AV_PIX_FMT_RGB32_1 || \
1082 f == AV_PIX_FMT_RGB24 || \
1083 f == AV_PIX_FMT_BGR32 || \
1084 f == AV_PIX_FMT_BGR32_1 || \
1085 f == AV_PIX_FMT_BGR24)
1086
1087 if (srcFormat == AV_PIX_FMT_GBRP && isPlanar(srcFormat) && isByteRGB(dstFormat))
1088 c->swscale = planarRgbToRgbWrapper;
1089
1090 if (av_pix_fmt_desc_get(srcFormat)->comp[0].depth == 8 &&
1091 isPackedRGB(srcFormat) && dstFormat == AV_PIX_FMT_GBRP)
1092 c->swscale = rgbToPlanarRgbWrapper;
1093
1094 /* bswap 16 bits per pixel/component packed formats */
1095 if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
1096 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48) ||
1097 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
1098 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
1099 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
1100 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY12) ||
1101 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
1102 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YA16) ||
1103 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP12)||
1104 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP16)||
1105 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
1106 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48) ||
1107 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
1108 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
1109 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
1110 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12))
1111 c->swscale = packed_16bpc_bswap;
1112
1113 if ((usePal(srcFormat) && (
1114 dstFormat == AV_PIX_FMT_RGB32 ||
1115 dstFormat == AV_PIX_FMT_RGB32_1 ||
1116 dstFormat == AV_PIX_FMT_RGB24 ||
1117 dstFormat == AV_PIX_FMT_BGR32 ||
1118 dstFormat == AV_PIX_FMT_BGR32_1 ||
1119 dstFormat == AV_PIX_FMT_BGR24)))
1120 c->swscale = palToRgbWrapper;
1121
1122 if (srcFormat == AV_PIX_FMT_YUV422P) {
1123 if (dstFormat == AV_PIX_FMT_YUYV422)
1124 c->swscale = yuv422pToYuy2Wrapper;
1125 else if (dstFormat == AV_PIX_FMT_UYVY422)
1126 c->swscale = yuv422pToUyvyWrapper;
1127 }
1128
1129 /* LQ converters if -sws 0 or -sws 4*/
1130 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1131 /* yv12_to_yuy2 */
1132 if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
1133 if (dstFormat == AV_PIX_FMT_YUYV422)
1134 c->swscale = planarToYuy2Wrapper;
1135 else if (dstFormat == AV_PIX_FMT_UYVY422)
1136 c->swscale = planarToUyvyWrapper;
1137 }
1138 }
1139 if (srcFormat == AV_PIX_FMT_YUYV422 &&
1140 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1141 c->swscale = yuyvToYuv420Wrapper;
1142 if (srcFormat == AV_PIX_FMT_UYVY422 &&
1143 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1144 c->swscale = uyvyToYuv420Wrapper;
1145 if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
1146 c->swscale = yuyvToYuv422Wrapper;
1147 if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
1148 c->swscale = uyvyToYuv422Wrapper;
1149
1150 /* simple copy */
1151 if ( srcFormat == dstFormat ||
1152 (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
1153 (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
1154 (isPlanarYUV(srcFormat) && isGray(dstFormat)) ||
1155 (isPlanarYUV(dstFormat) && isGray(srcFormat)) ||
1156 (isGray(dstFormat) && isGray(srcFormat)) ||
1157 (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
1158 c->chrDstHSubSample == c->chrSrcHSubSample &&
1159 c->chrDstVSubSample == c->chrSrcVSubSample &&
1160 dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
1161 dstFormat != AV_PIX_FMT_P010LE && dstFormat != AV_PIX_FMT_P010BE &&
1162 dstFormat != AV_PIX_FMT_YUV420P12LE && dstFormat != AV_PIX_FMT_YUV420P12BE &&
1163 dstFormat != AV_PIX_FMT_YUV422P12LE && dstFormat != AV_PIX_FMT_YUV422P12BE &&
1164 dstFormat != AV_PIX_FMT_YUV444P12LE && dstFormat != AV_PIX_FMT_YUV444P12BE &&
1165 srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21 &&
1166 srcFormat != AV_PIX_FMT_P010LE && srcFormat != AV_PIX_FMT_P010BE &&
1167 srcFormat != AV_PIX_FMT_YUV420P12LE && srcFormat != AV_PIX_FMT_YUV420P12BE &&
1168 srcFormat != AV_PIX_FMT_YUV422P12LE && srcFormat != AV_PIX_FMT_YUV422P12BE &&
1169 srcFormat != AV_PIX_FMT_YUV444P12LE && srcFormat != AV_PIX_FMT_YUV444P12BE))
1170 {
1171 if (isPacked(c->srcFormat))
1172 c->swscale = packedCopyWrapper;
1173 else /* Planar YUV or gray */
1174 c->swscale = planarCopyWrapper;
1175 }
1176
1177 if (ARCH_PPC)
1178 ff_get_unscaled_swscale_ppc(c);
1179 }
1180
1181 static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
1182 {
1183 if (!isALPHA(format))
1184 src[3] = NULL;
1185 if (!isPlanar(format)) {
1186 src[3] = src[2] = NULL;
1187
1188 if (!usePal(format))
1189 src[1] = NULL;
1190 }
1191 }
1192
1193 static int check_image_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt,
1194 const int linesizes[4])
1195 {
1196 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1197 int i;
1198
1199 for (i = 0; i < 4; i++) {
1200 int plane = desc->comp[i].plane;
1201 if (!data[plane] || !linesizes[plane])
1202 return 0;
1203 }
1204
1205 return 1;
1206 }
1207
1208 /**
1209 * swscale wrapper, so we don't need to export the SwsContext.
1210 * Assumes planar YUV to be in YUV order instead of YVU.
1211 */
1212 int attribute_align_arg sws_scale(struct SwsContext *c,
1213 const uint8_t * const srcSlice[],
1214 const int srcStride[], int srcSliceY,
1215 int srcSliceH, uint8_t *const dst[],
1216 const int dstStride[])
1217 {
1218 int i;
1219 const uint8_t *src2[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
1220 uint8_t *dst2[4] = { dst[0], dst[1], dst[2], dst[3] };
1221
1222 // do not mess up sliceDir if we have a "trailing" 0-size slice
1223 if (srcSliceH == 0)
1224 return 0;
1225
1226 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
1227 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1228 return 0;
1229 }
1230 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1231 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1232 return 0;
1233 }
1234
1235 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1236 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1237 return 0;
1238 }
1239 if (c->sliceDir == 0) {
1240 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1241 }
1242
1243 if (usePal(c->srcFormat)) {
1244 for (i = 0; i < 256; i++) {
1245 int r, g, b, y, u, v;
1246 if (c->srcFormat == AV_PIX_FMT_PAL8) {
1247 uint32_t p = ((const uint32_t *)(srcSlice[1]))[i];
1248 r = (p >> 16) & 0xFF;
1249 g = (p >> 8) & 0xFF;
1250 b = p & 0xFF;
1251 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
1252 r = ( i >> 5 ) * 36;
1253 g = ((i >> 2) & 7) * 36;
1254 b = ( i & 3) * 85;
1255 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
1256 b = ( i >> 6 ) * 85;
1257 g = ((i >> 3) & 7) * 36;
1258 r = ( i & 7) * 36;
1259 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
1260 r = ( i >> 3 ) * 255;
1261 g = ((i >> 1) & 3) * 85;
1262 b = ( i & 1) * 255;
1263 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 ||
1264 c->srcFormat == AV_PIX_FMT_YA8) {
1265 r = g = b = i;
1266 } else {
1267 assert(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
1268 b = ( i >> 3 ) * 255;
1269 g = ((i >> 1) & 3) * 85;
1270 r = ( i & 1) * 255;
1271 }
1272 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1273 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1274 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1275 c->pal_yuv[i] = y + (u << 8) + (v << 16) + (0xFFU << 24);
1276
1277 switch (c->dstFormat) {
1278 case AV_PIX_FMT_BGR32:
1279 #if !HAVE_BIGENDIAN
1280 case AV_PIX_FMT_RGB24:
1281 #endif
1282 c->pal_rgb[i] = r + (g << 8) + (b << 16) + (0xFFU << 24);
1283 break;
1284 case AV_PIX_FMT_BGR32_1:
1285 #if HAVE_BIGENDIAN
1286 case AV_PIX_FMT_BGR24:
1287 #endif
1288 c->pal_rgb[i] = 0xFF + (r << 8) + (g << 16) + ((unsigned)b << 24);
1289 break;
1290 case AV_PIX_FMT_RGB32_1:
1291 #if HAVE_BIGENDIAN
1292 case AV_PIX_FMT_RGB24:
1293 #endif
1294 c->pal_rgb[i] = 0xFF + (b << 8) + (g << 16) + ((unsigned)r << 24);
1295 break;
1296 case AV_PIX_FMT_RGB32:
1297 #if !HAVE_BIGENDIAN
1298 case AV_PIX_FMT_BGR24:
1299 #endif
1300 default:
1301 c->pal_rgb[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
1302 }
1303 }
1304 }
1305
1306 // copy strides, so they can safely be modified
1307 if (c->sliceDir == 1) {
1308 // slices go from top to bottom
1309 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1310 srcStride[3] };
1311 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1312 dstStride[3] };
1313
1314 reset_ptr(src2, c->srcFormat);
1315 reset_ptr((const uint8_t **) dst2, c->dstFormat);
1316
1317 /* reset slice direction at end of frame */
1318 if (srcSliceY + srcSliceH == c->srcH)
1319 c->sliceDir = 0;
1320
1321 return c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1322 dstStride2);
1323 } else {
1324 // slices go from bottom to top => we flip the image internally
1325 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1326 -srcStride[3] };
1327 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1328 -dstStride[3] };
1329
1330 src2[0] += (srcSliceH - 1) * srcStride[0];
1331 if (!usePal(c->srcFormat))
1332 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1333 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1334 src2[3] += (srcSliceH - 1) * srcStride[3];
1335 dst2[0] += ( c->dstH - 1) * dstStride[0];
1336 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1337 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1338 dst2[3] += ( c->dstH - 1) * dstStride[3];
1339
1340 reset_ptr(src2, c->srcFormat);
1341 reset_ptr((const uint8_t **) dst2, c->dstFormat);
1342
1343 /* reset slice direction at end of frame */
1344 if (!srcSliceY)
1345 c->sliceDir = 0;
1346
1347 return c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1348 srcSliceH, dst2, dstStride2);
1349 }
1350 }
1351
1352 /* Convert the palette to the same packed 32-bit format as the palette */
1353 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1354 int num_pixels, const uint8_t *palette)
1355 {
1356 int i;
1357
1358 for (i = 0; i < num_pixels; i++)
1359 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1360 }
1361
1362 /* Palette format: ABCD -> dst format: ABC */
1363 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1364 int num_pixels, const uint8_t *palette)
1365 {
1366 int i;
1367
1368 for (i = 0; i < num_pixels; i++) {
1369 //FIXME slow?
1370 dst[0] = palette[src[i] * 4 + 0];
1371 dst[1] = palette[src[i] * 4 + 1];
1372 dst[2] = palette[src[i] * 4 + 2];
1373 dst += 3;
1374 }
1375 }