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