swscale: consistent names for arch-specific acceleration functions
[libav.git] / libswscale / swscale_unscaled.c
CommitLineData
6af28010
RB
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
d2585315 37DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_1)[8][8] = {
1ce724ee
RB
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};
d2585315 47DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_3)[8][8] = {
1ce724ee
RB
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};
d2585315 57DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_64)[8][8] = {
1ce724ee
RB
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};
d2585315 67DECLARE_ALIGNED(8, static const uint8_t, dither_8x8_256)[8][8] = {
1ce724ee
RB
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
6af28010 78#define RGB2YUV_SHIFT 15
7b3894be
NAM
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
89static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
90 uint8_t val)
6af28010
RB
91{
92 int i;
7b3894be
NAM
93 uint8_t *ptr = plane + stride * y;
94 for (i = 0; i < height; i++) {
6af28010
RB
95 memset(ptr, val, width);
96 ptr += stride;
97 }
98}
99
26b5ad25
LB
100static 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
6af28010
RB
121static 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;
7b3894be 130 for (i = 0; i < srcSliceH; i++) {
6af28010
RB
131 memcpy(dst, src, width);
132 src += srcStride;
133 dst += dstStride;
134 }
135 }
136}
137
7b3894be
NAM
138static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
139 int srcStride[], int srcSliceY,
140 int srcSliceH, uint8_t *dstParam[],
141 int dstStride[])
6af28010 142{
7b3894be 143 uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
6af28010
RB
144
145 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
146 dstParam[0], dstStride[0]);
147
716d413c 148 if (c->dstFormat == AV_PIX_FMT_NV12)
7b3894be
NAM
149 interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
150 srcStride[1], srcStride[2], dstStride[0]);
6af28010 151 else
7b3894be
NAM
152 interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
153 srcStride[2], srcStride[1], dstStride[0]);
6af28010
RB
154
155 return srcSliceH;
156}
157
7b3894be
NAM
158static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
159 int srcStride[], int srcSliceY, int srcSliceH,
160 uint8_t *dstParam[], int dstStride[])
6af28010 161{
7b3894be 162 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
6af28010 163
7b3894be
NAM
164 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
165 srcStride[1], dstStride[0]);
6af28010
RB
166
167 return srcSliceH;
168}
169
7b3894be
NAM
170static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
171 int srcStride[], int srcSliceY, int srcSliceH,
172 uint8_t *dstParam[], int dstStride[])
6af28010 173{
7b3894be 174 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
6af28010 175
7b3894be
NAM
176 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
177 srcStride[1], dstStride[0]);
6af28010
RB
178
179 return srcSliceH;
180}
181
7b3894be
NAM
182static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
183 int srcStride[], int srcSliceY, int srcSliceH,
184 uint8_t *dstParam[], int dstStride[])
6af28010 185{
7b3894be 186 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
6af28010 187
7b3894be
NAM
188 yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
189 srcStride[1], dstStride[0]);
6af28010
RB
190
191 return srcSliceH;
192}
193
7b3894be
NAM
194static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
195 int srcStride[], int srcSliceY, int srcSliceH,
196 uint8_t *dstParam[], int dstStride[])
6af28010 197{
7b3894be 198 uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
6af28010 199
7b3894be
NAM
200 yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
201 srcStride[1], dstStride[0]);
6af28010
RB
202
203 return srcSliceH;
204}
205
7b3894be
NAM
206static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
207 int srcStride[], int srcSliceY, int srcSliceH,
208 uint8_t *dstParam[], int dstStride[])
6af28010 209{
7b3894be
NAM
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;
6af28010 213
7b3894be
NAM
214 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
215 dstStride[1], srcStride[0]);
6af28010
RB
216
217 if (dstParam[3])
218 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
219
220 return srcSliceH;
221}
222
7b3894be
NAM
223static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
224 int srcStride[], int srcSliceY, int srcSliceH,
225 uint8_t *dstParam[], int dstStride[])
6af28010 226{
7b3894be
NAM
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;
6af28010 230
7b3894be
NAM
231 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
232 dstStride[1], srcStride[0]);
6af28010
RB
233
234 return srcSliceH;
235}
236
7b3894be
NAM
237static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
238 int srcStride[], int srcSliceY, int srcSliceH,
239 uint8_t *dstParam[], int dstStride[])
6af28010 240{
7b3894be
NAM
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;
6af28010 244
7b3894be
NAM
245 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
246 dstStride[1], srcStride[0]);
6af28010
RB
247
248 if (dstParam[3])
249 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
250
251 return srcSliceH;
252}
253
7b3894be
NAM
254static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
255 int srcStride[], int srcSliceY, int srcSliceH,
256 uint8_t *dstParam[], int dstStride[])
6af28010 257{
7b3894be
NAM
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;
6af28010 261
7b3894be
NAM
262 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
263 dstStride[1], srcStride[0]);
6af28010
RB
264
265 return srcSliceH;
266}
267
7b3894be
NAM
268static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
269 const uint8_t *palette)
6af28010
RB
270{
271 int i;
7b3894be
NAM
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);
6af28010
RB
274}
275
7b3894be
NAM
276static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
277 const uint8_t *palette)
6af28010
RB
278{
279 int i;
280
7b3894be
NAM
281 for (i = 0; i < num_pixels; i++)
282 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
6af28010
RB
283}
284
7b3894be
NAM
285static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
286 const uint8_t *palette)
6af28010
RB
287{
288 int i;
289
7b3894be 290 for (i = 0; i < num_pixels; i++) {
6af28010 291 //FIXME slow?
7b3894be
NAM
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;
6af28010
RB
296 }
297}
298
7b3894be 299static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
72cb9044 300 int srcStride[], int srcSliceY, int srcSliceH,
7b3894be 301 uint8_t *dst[], int dstStride[])
72cb9044
JG
302{
303 int i, j;
304 int srcstr = srcStride[0] >> 1;
305 int dststr = dstStride[0] >> 1;
7b3894be
NAM
306 uint16_t *dstPtr = (uint16_t *) dst[0];
307 const uint16_t *srcPtr = (const uint16_t *) src[0];
6e9bb5aa 308 int min_stride = FFMIN(srcstr, dststr);
72cb9044
JG
309
310 for (i = 0; i < srcSliceH; i++) {
6e9bb5aa 311 for (j = 0; j < min_stride; j++) {
72cb9044
JG
312 dstPtr[j] = av_bswap16(srcPtr[j]);
313 }
314 srcPtr += srcstr;
315 dstPtr += dststr;
316 }
317
318 return srcSliceH;
319}
320
7b3894be
NAM
321static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
322 int srcSliceY, int srcSliceH, uint8_t *dst[],
323 int dstStride[])
6af28010 324{
716d413c
AK
325 const enum AVPixelFormat srcFormat = c->srcFormat;
326 const enum AVPixelFormat dstFormat = c->dstFormat;
6af28010 327 void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
7b3894be 328 const uint8_t *palette) = NULL;
6af28010 329 int i;
7b3894be
NAM
330 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
331 const uint8_t *srcPtr = src[0];
6af28010 332
716d413c 333 if (srcFormat == AV_PIX_FMT_Y400A) {
6af28010 334 switch (dstFormat) {
716d413c
AK
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;
6af28010
RB
341 }
342 } else if (usePal(srcFormat)) {
343 switch (dstFormat) {
716d413c
AK
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;
6af28010
RB
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 {
7b3894be 357 for (i = 0; i < srcSliceH; i++) {
6af28010 358 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
7b3894be
NAM
359 srcPtr += srcStride[0];
360 dstPtr += dstStride[0];
6af28010
RB
361 }
362 }
363
364 return srcSliceH;
365}
366
b991b300
HKA
367static 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
385static 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
414static 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;
a805cefd
MR
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
716d413c 424 if (c->srcFormat != AV_PIX_FMT_GBRP) {
b991b300
HKA
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) {
716d413c 432 case AV_PIX_FMT_BGR24:
a805cefd 433 gbr24ptopacked24(src102, stride102,
b991b300
HKA
434 dst[0] + srcSliceY * dstStride[0], dstStride[0],
435 srcSliceH, c->srcW);
436 break;
437
716d413c 438 case AV_PIX_FMT_RGB24:
a805cefd 439 gbr24ptopacked24(src201, stride201,
b991b300
HKA
440 dst[0] + srcSliceY * dstStride[0], dstStride[0],
441 srcSliceH, c->srcW);
442 break;
443
716d413c 444 case AV_PIX_FMT_ARGB:
b991b300 445 alpha_first = 1;
716d413c 446 case AV_PIX_FMT_RGBA:
a805cefd 447 gbr24ptopacked32(src201, stride201,
b991b300
HKA
448 dst[0] + srcSliceY * dstStride[0], dstStride[0],
449 srcSliceH, alpha_first, c->srcW);
450 break;
451
716d413c 452 case AV_PIX_FMT_ABGR:
b991b300 453 alpha_first = 1;
716d413c 454 case AV_PIX_FMT_BGRA:
a805cefd 455 gbr24ptopacked32(src102, stride102,
b991b300
HKA
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
57c4c255
DB
470static 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
499static 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) {
542b83fc 514 case AV_PIX_FMT_RGB24:
57c4c255
DB
515 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
516 stride201, srcSliceH, alpha_first, 3, c->srcW);
517 break;
542b83fc 518 case AV_PIX_FMT_BGR24:
57c4c255
DB
519 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
520 stride102, srcSliceH, alpha_first, 3, c->srcW);
521 break;
542b83fc 522 case AV_PIX_FMT_ARGB:
57c4c255 523 alpha_first = 1;
542b83fc 524 case AV_PIX_FMT_RGBA:
57c4c255
DB
525 packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
526 stride201, srcSliceH, alpha_first, 4, c->srcW);
527 break;
542b83fc 528 case AV_PIX_FMT_ABGR:
57c4c255 529 alpha_first = 1;
542b83fc 530 case AV_PIX_FMT_BGRA:
57c4c255
DB
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
6af28010 544#define isRGBA32(x) ( \
716d413c
AK
545 (x) == AV_PIX_FMT_ARGB \
546 || (x) == AV_PIX_FMT_RGBA \
547 || (x) == AV_PIX_FMT_BGRA \
548 || (x) == AV_PIX_FMT_ABGR \
6af28010
RB
549 )
550
551/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
e7843db3
RB
552typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
553static rgbConvFn findRgbConvFn(SwsContext *c)
6af28010 554{
716d413c
AK
555 const enum AVPixelFormat srcFormat = c->srcFormat;
556 const enum AVPixelFormat dstFormat = c->dstFormat;
0b8b3387
PM
557 const int srcId = c->srcFormatBpp;
558 const int dstId = c->dstFormatBpp;
e7843db3 559 rgbConvFn conv = NULL;
0a7068fa
AK
560 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
561 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
e7843db3 562
0a7068fa 563#define IS_NOT_NE(bpp, desc) \
e7843db3 564 (((bpp + 7) >> 3) == 2 && \
e6c4ac7b 565 (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
e7843db3
RB
566
567 /* if this is non-native rgb444/555/565, don't handle it here. */
0a7068fa 568 if (IS_NOT_NE(srcId, desc_src) || IS_NOT_NE(dstId, desc_dst))
e7843db3 569 return NULL;
6af28010 570
716d413c 571#define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
6af28010
RB
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 */
7b3894be
NAM
588 if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
589 (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
0b8b3387 590 switch (srcId | (dstId << 16)) {
0cc1a86d 591 case 0x000F000C: conv = rgb12to15; break;
0b8b3387
PM
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;
6af28010 604 }
7b3894be
NAM
605 } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
606 (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
0b8b3387 607 switch (srcId | (dstId << 16)) {
f7f35632 608 case 0x000C000C: conv = rgb12tobgr12; break;
0b8b3387
PM
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;
6af28010
RB
624 }
625 }
626
e7843db3
RB
627 return conv;
628}
629
630/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
631static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
632 int srcSliceY, int srcSliceH, uint8_t *dst[],
633 int dstStride[])
634
635{
716d413c
AK
636 const enum AVPixelFormat srcFormat = c->srcFormat;
637 const enum AVPixelFormat dstFormat = c->dstFormat;
e7843db3
RB
638 const int srcBpp = (c->srcFormatBpp + 7) >> 3;
639 const int dstBpp = (c->dstFormatBpp + 7) >> 3;
640 rgbConvFn conv = findRgbConvFn(c);
641
6af28010
RB
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 {
7b3894be
NAM
646 const uint8_t *srcPtr = src[0];
647 uint8_t *dstPtr = dst[0];
716d413c 648 if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
7b3894be 649 !isRGBA32(dstFormat))
6af28010
RB
650 srcPtr += ALT32_CORR;
651
716d413c 652 if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
7b3894be 653 !isRGBA32(srcFormat))
6af28010
RB
654 dstPtr += ALT32_CORR;
655
7b3894be
NAM
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]);
6af28010
RB
660 else {
661 int i;
7b3894be 662 dstPtr += dstStride[0] * srcSliceY;
6af28010 663
7b3894be
NAM
664 for (i = 0; i < srcSliceH; i++) {
665 conv(srcPtr, dstPtr, c->srcW * srcBpp);
666 srcPtr += srcStride[0];
667 dstPtr += dstStride[0];
6af28010
RB
668 }
669 }
670 }
671 return srcSliceH;
672}
673
7b3894be
NAM
674static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
675 int srcStride[], int srcSliceY, int srcSliceH,
676 uint8_t *dst[], int dstStride[])
6af28010
RB
677{
678 rgb24toyv12(
679 src[0],
7b3894be
NAM
680 dst[0] + srcSliceY * dstStride[0],
681 dst[1] + (srcSliceY >> 1) * dstStride[1],
682 dst[2] + (srcSliceY >> 1) * dstStride[2],
6af28010
RB
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
7b3894be
NAM
690static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
691 int srcStride[], int srcSliceY, int srcSliceH,
692 uint8_t *dst[], int dstStride[])
6af28010
RB
693{
694 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
695 dst[0], dstStride[0]);
696
7b3894be 697 planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
6af28010 698 srcSliceH >> 2, srcStride[1], dstStride[1]);
7b3894be 699 planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
6af28010
RB
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) */
7b3894be
NAM
707static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
708 int srcStride[], int srcSliceY, int srcSliceH,
709 uint8_t *dst[], int dstStride[])
6af28010 710{
7b3894be
NAM
711 if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
712 memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
6af28010
RB
713 else {
714 int i;
7b3894be
NAM
715 const uint8_t *srcPtr = src[0];
716 uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
717 int length = 0;
6af28010
RB
718
719 /* universal length finder */
7b3894be
NAM
720 while (length + c->srcW <= FFABS(dstStride[0]) &&
721 length + c->srcW <= FFABS(srcStride[0]))
722 length += c->srcW;
723 assert(length != 0);
6af28010 724
7b3894be 725 for (i = 0; i < srcSliceH; i++) {
6af28010 726 memcpy(dstPtr, srcPtr, length);
7b3894be
NAM
727 srcPtr += srcStride[0];
728 dstPtr += dstStride[0];
6af28010
RB
729 }
730 }
731 return srcSliceH;
732}
733
55eda370
RB
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) \
1ce724ee
RB
737 for (i = 0; i < height; i++) { \
738 const uint8_t *dither = dithers[i & 7]; \
739 for (j = 0; j < length - 7; j += 8) { \
55eda370
RB
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)); \
1ce724ee
RB
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
7b3894be
NAM
755static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
756 int srcStride[], int srcSliceY, int srcSliceH,
757 uint8_t *dst[], int dstStride[])
6af28010 758{
0a7068fa
AK
759 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
760 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
6af28010 761 int plane, i, j;
7b3894be
NAM
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;
6af28010
RB
771 // ignore palette for GRAY8
772 if (plane == 1 && !dst[2]) continue;
773 if (!src[plane] || (plane == 1 && !src[2])) {
26b5ad25 774 int val = (plane == 3) ? 255 : 128;
7b3894be
NAM
775 if (is16BPS(c->dstFormat))
776 length *= 2;
26b5ad25
LB
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);
6af28010 785 } else {
7b3894be 786 if (is9_OR_10BPS(c->srcFormat)) {
0a7068fa
AK
787 const int src_depth = desc_src->comp[plane].depth_minus1 + 1;
788 const int dst_depth = desc_dst->comp[plane].depth_minus1 + 1;
7b3894be 789 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
6af28010
RB
790
791 if (is16BPS(c->dstFormat)) {
7b3894be 792 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
6af28010
RB
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]); \
7b3894be 797 wfunc(&dstPtr2[j], (srcpx << (16 - src_depth)) | (srcpx >> (2 * src_depth - 16))); \
6af28010 798 } \
7b3894be
NAM
799 dstPtr2 += dstStride[plane] / 2; \
800 srcPtr2 += srcStride[plane] / 2; \
6af28010
RB
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)) {
7b3894be 816 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
6af28010
RB
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 } \
7b3894be
NAM
822 dstPtr2 += dstStride[plane] / 2; \
823 srcPtr2 += srcStride[plane] / 2; \
6af28010
RB
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) { \
7b3894be
NAM
830 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
831 srcPtr2, srcStride[plane] / 2, rfunc, \
55eda370 832 dither_8x8_1, 1, clip9); \
6af28010
RB
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 {
1ce724ee 850#define W8(a, b) { *(a) = (b); }
6af28010 851#define COPY9_OR_10TO8(rfunc) \
1ce724ee
RB
852 if (src_depth == 9) { \
853 DITHER_COPY(dstPtr, dstStride[plane], W8, \
7b3894be 854 srcPtr2, srcStride[plane] / 2, rfunc, \
55eda370 855 dither_8x8_1, 1, av_clip_uint8); \
1ce724ee
RB
856 } else { \
857 DITHER_COPY(dstPtr, dstStride[plane], W8, \
7b3894be 858 srcPtr2, srcStride[plane] / 2, rfunc, \
55eda370 859 dither_8x8_3, 2, av_clip_uint8); \
6af28010
RB
860 }
861 if (isBE(c->srcFormat)) {
862 COPY9_OR_10TO8(AV_RB16);
863 } else {
864 COPY9_OR_10TO8(AV_RL16);
865 }
866 }
7b3894be 867 } else if (is9_OR_10BPS(c->dstFormat)) {
0a7068fa 868 const int dst_depth = desc_dst->comp[plane].depth_minus1 + 1;
7b3894be 869 uint16_t *dstPtr2 = (uint16_t *) dstPtr;
6af28010
RB
870
871 if (is16BPS(c->srcFormat)) {
7b3894be 872 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
6af28010 873#define COPY16TO9_OR_10(rfunc, wfunc) \
1ce724ee 874 if (dst_depth == 9) { \
7b3894be
NAM
875 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
876 srcPtr2, srcStride[plane] / 2, rfunc, \
aa2ba8c9 877 ff_dither_8x8_128, 7, clip9); \
1ce724ee 878 } else { \
7b3894be
NAM
879 DITHER_COPY(dstPtr2, dstStride[plane] / 2, wfunc, \
880 srcPtr2, srcStride[plane] / 2, rfunc, \
55eda370 881 dither_8x8_64, 6, clip10); \
6af28010
RB
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]; \
7b3894be 901 wfunc(&dstPtr2[j], (srcpx << (dst_depth - 8)) | (srcpx >> (16 - dst_depth))); \
6af28010 902 } \
7b3894be 903 dstPtr2 += dstStride[plane] / 2; \
6af28010
RB
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 }
7b3894be
NAM
912 } else if (is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
913 const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
1ce724ee
RB
914#define COPY16TO8(rfunc) \
915 DITHER_COPY(dstPtr, dstStride[plane], W8, \
7b3894be 916 srcPtr2, srcStride[plane] / 2, rfunc, \
55eda370 917 dither_8x8_256, 8, av_clip_uint8);
1ce724ee
RB
918 if (isBE(c->srcFormat)) {
919 COPY16TO8(AV_RB16);
920 } else {
921 COPY16TO8(AV_RL16);
6af28010 922 }
7b3894be
NAM
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];
6af28010 928 }
7b3894be
NAM
929 srcPtr += srcStride[plane];
930 dstPtr += dstStride[plane];
6af28010 931 }
7b3894be
NAM
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];
6af28010
RB
940 }
941 } else if (dstStride[plane] == srcStride[plane] &&
942 srcStride[plane] > 0 && srcStride[plane] == length) {
7b3894be
NAM
943 memcpy(dst[plane] + dstStride[plane] * y, src[plane],
944 height * dstStride[plane]);
6af28010 945 } else {
7b3894be
NAM
946 if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
947 length *= 2;
0a7068fa 948 else if (!desc_src->comp[0].depth_minus1)
e7843db3 949 length >>= 3; // monowhite/black
7b3894be 950 for (i = 0; i < height; i++) {
6af28010 951 memcpy(dstPtr, srcPtr, length);
7b3894be
NAM
952 srcPtr += srcStride[plane];
953 dstPtr += dstStride[plane];
6af28010
RB
954 }
955 }
956 }
957 }
958 return srcSliceH;
959}
960
b4dc6880
JG
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
6af28010
RB
967void ff_get_unscaled_swscale(SwsContext *c)
968{
716d413c
AK
969 const enum AVPixelFormat srcFormat = c->srcFormat;
970 const enum AVPixelFormat dstFormat = c->dstFormat;
6af28010
RB
971 const int flags = c->flags;
972 const int dstH = c->dstH;
973 int needsDither;
974
7b3894be
NAM
975 needsDither = isAnyRGB(dstFormat) &&
976 c->dstFormatBpp < 24 &&
977 (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
6af28010
RB
978
979 /* yv12_to_nv12 */
716d413c
AK
980 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
981 (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
7b3894be 982 c->swScale = planarToNv12Wrapper;
6af28010
RB
983 }
984 /* yuv2bgr */
716d413c
AK
985 if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
986 srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
7b3894be
NAM
987 !(flags & SWS_ACCURATE_RND) && !(dstH & 1)) {
988 c->swScale = ff_yuv2rgb_get_func_ptr(c);
6af28010
RB
989 }
990
716d413c
AK
991 if (srcFormat == AV_PIX_FMT_YUV410P &&
992 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
7b3894be
NAM
993 !(flags & SWS_BITEXACT)) {
994 c->swScale = yvu9ToYv12Wrapper;
6af28010
RB
995 }
996
997 /* bgr24toYV12 */
716d413c
AK
998 if (srcFormat == AV_PIX_FMT_BGR24 &&
999 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
7b3894be
NAM
1000 !(flags & SWS_ACCURATE_RND))
1001 c->swScale = bgr24ToYv12Wrapper;
6af28010
RB
1002
1003 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
e7843db3 1004 if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
6af28010
RB
1005 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1006 c->swScale= rgbToRgbWrapper;
1007
e58013dd
MN
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))
b991b300
HKA
1017 c->swScale = planarRgbToRgbWrapper;
1018
57c4c255
DB
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
ed46a3d8 1023 /* bswap 16 bits per pixel/component packed formats */
716d413c
AK
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) ||
9cfa21c8
JG
1032 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
1033 IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12))
ed46a3d8
JG
1034 c->swScale = packed_16bpc_bswap;
1035
6af28010 1036 if ((usePal(srcFormat) && (
716d413c
AK
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)))
7b3894be 1043 c->swScale = palToRgbWrapper;
6af28010 1044
716d413c
AK
1045 if (srcFormat == AV_PIX_FMT_YUV422P) {
1046 if (dstFormat == AV_PIX_FMT_YUYV422)
7b3894be 1047 c->swScale = yuv422pToYuy2Wrapper;
716d413c 1048 else if (dstFormat == AV_PIX_FMT_UYVY422)
7b3894be 1049 c->swScale = yuv422pToUyvyWrapper;
6af28010
RB
1050 }
1051
1052 /* LQ converters if -sws 0 or -sws 4*/
1053 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1054 /* yv12_to_yuy2 */
716d413c
AK
1055 if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
1056 if (dstFormat == AV_PIX_FMT_YUYV422)
7b3894be 1057 c->swScale = planarToYuy2Wrapper;
716d413c 1058 else if (dstFormat == AV_PIX_FMT_UYVY422)
7b3894be 1059 c->swScale = planarToUyvyWrapper;
6af28010
RB
1060 }
1061 }
716d413c
AK
1062 if (srcFormat == AV_PIX_FMT_YUYV422 &&
1063 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
7b3894be 1064 c->swScale = yuyvToYuv420Wrapper;
716d413c
AK
1065 if (srcFormat == AV_PIX_FMT_UYVY422 &&
1066 (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
7b3894be 1067 c->swScale = uyvyToYuv420Wrapper;
716d413c 1068 if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
7b3894be 1069 c->swScale = yuyvToYuv422Wrapper;
716d413c 1070 if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
7b3894be 1071 c->swScale = uyvyToYuv422Wrapper;
6af28010
RB
1072
1073 /* simple copy */
7b3894be 1074 if ( srcFormat == dstFormat ||
716d413c
AK
1075 (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
1076 (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
7b3894be
NAM
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 &&
716d413c
AK
1083 dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
1084 srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21))
6af28010
RB
1085 {
1086 if (isPacked(c->srcFormat))
7b3894be 1087 c->swScale = packedCopyWrapper;
6af28010 1088 else /* Planar YUV or gray */
7b3894be 1089 c->swScale = planarCopyWrapper;
6af28010
RB
1090 }
1091
1092 if (ARCH_BFIN)
3aa682f2 1093 ff_get_unscaled_swscale_bfin(c);
c2503d9c 1094 if (ARCH_PPC)
3aa682f2 1095 ff_get_unscaled_swscale_ppc(c);
6af28010
RB
1096}
1097
7b3894be 1098static void reset_ptr(const uint8_t *src[], int format)
6af28010 1099{
7b3894be
NAM
1100 if (!isALPHA(format))
1101 src[3] = NULL;
185655c6 1102 if (!isPlanar(format)) {
7b3894be 1103 src[3] = src[2] = NULL;
6af28010
RB
1104
1105 if (!usePal(format))
7b3894be 1106 src[1] = NULL;
6af28010
RB
1107 }
1108}
1109
716d413c 1110static int check_image_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt,
6af28010
RB
1111 const int linesizes[4])
1112{
0a7068fa 1113 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
6af28010
RB
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 */
7b3894be
NAM
1129int 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[])
6af28010
RB
1134{
1135 int i;
7b3894be
NAM
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] };
6af28010
RB
1138
1139 // do not mess up sliceDir if we have a "trailing" 0-size slice
1140 if (srcSliceH == 0)
1141 return 0;
1142
96c1e6d4 1143 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
6af28010
RB
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)) {
7b3894be
NAM
1161 for (i = 0; i < 256; i++) {
1162 int p, r, g, b, y, u, v;
716d413c 1163 if (c->srcFormat == AV_PIX_FMT_PAL8) {
7b3894be
NAM
1164 p = ((const uint32_t *)(srcSlice[1]))[i];
1165 r = (p >> 16) & 0xFF;
1166 g = (p >> 8) & 0xFF;
1167 b = p & 0xFF;
716d413c 1168 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
7b3894be
NAM
1169 r = ( i >> 5 ) * 36;
1170 g = ((i >> 2) & 7) * 36;
1171 b = ( i & 3) * 85;
716d413c 1172 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
7b3894be
NAM
1173 b = ( i >> 6 ) * 85;
1174 g = ((i >> 3) & 7) * 36;
1175 r = ( i & 7) * 36;
716d413c 1176 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
7b3894be
NAM
1177 r = ( i >> 3 ) * 255;
1178 g = ((i >> 1) & 3) * 85;
1179 b = ( i & 1) * 255;
716d413c
AK
1180 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 ||
1181 c->srcFormat == AV_PIX_FMT_Y400A) {
6af28010
RB
1182 r = g = b = i;
1183 } else {
716d413c 1184 assert(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
7b3894be
NAM
1185 b = ( i >> 3 ) * 255;
1186 g = ((i >> 1) & 3) * 85;
1187 r = ( i & 1) * 255;
6af28010 1188 }
7b3894be
NAM
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);
6af28010 1193
7b3894be 1194 switch (c->dstFormat) {
716d413c 1195 case AV_PIX_FMT_BGR32:
6af28010 1196#if !HAVE_BIGENDIAN
716d413c 1197 case AV_PIX_FMT_RGB24:
6af28010 1198#endif
7b3894be 1199 c->pal_rgb[i] = r + (g << 8) + (b << 16);
6af28010 1200 break;
716d413c 1201 case AV_PIX_FMT_BGR32_1:
6af28010 1202#if HAVE_BIGENDIAN
716d413c 1203 case AV_PIX_FMT_BGR24:
6af28010 1204#endif
7b3894be 1205 c->pal_rgb[i] = (r + (g << 8) + (b << 16)) << 8;
6af28010 1206 break;
716d413c 1207 case AV_PIX_FMT_RGB32_1:
6af28010 1208#if HAVE_BIGENDIAN
716d413c 1209 case AV_PIX_FMT_RGB24:
6af28010 1210#endif
7b3894be 1211 c->pal_rgb[i] = (b + (g << 8) + (r << 16)) << 8;
6af28010 1212 break;
716d413c 1213 case AV_PIX_FMT_RGB32:
6af28010 1214#if !HAVE_BIGENDIAN
716d413c 1215 case AV_PIX_FMT_BGR24:
6af28010
RB
1216#endif
1217 default:
7b3894be 1218 c->pal_rgb[i] = b + (g << 8) + (r << 16);
6af28010
RB
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
7b3894be
NAM
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] };
6af28010
RB
1230
1231 reset_ptr(src2, c->srcFormat);
7b3894be 1232 reset_ptr((const uint8_t **) dst2, c->dstFormat);
6af28010
RB
1233
1234 /* reset slice direction at end of frame */
1235 if (srcSliceY + srcSliceH == c->srcH)
1236 c->sliceDir = 0;
1237
7b3894be
NAM
1238 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1239 dstStride2);
6af28010
RB
1240 } else {
1241 // slices go from bottom to top => we flip the image internally
7b3894be
NAM
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] };
6af28010 1246
7b3894be 1247 src2[0] += (srcSliceH - 1) * srcStride[0];
6af28010 1248 if (!usePal(c->srcFormat))
7b3894be
NAM
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];
6af28010
RB
1256
1257 reset_ptr(src2, c->srcFormat);
7b3894be 1258 reset_ptr((const uint8_t **) dst2, c->dstFormat);
6af28010
RB
1259
1260 /* reset slice direction at end of frame */
1261 if (!srcSliceY)
1262 c->sliceDir = 0;
1263
7b3894be
NAM
1264 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1265 srcSliceH, dst2, dstStride2);
6af28010
RB
1266 }
1267}
1268
1269/* Convert the palette to the same packed 32-bit format as the palette */
7b3894be
NAM
1270void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1271 int num_pixels, const uint8_t *palette)
6af28010
RB
1272{
1273 int i;
1274
7b3894be 1275 for (i = 0; i < num_pixels; i++)
6af28010
RB
1276 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1277}
1278
1279/* Palette format: ABCD -> dst format: ABC */
7b3894be
NAM
1280void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1281 int num_pixels, const uint8_t *palette)
6af28010
RB
1282{
1283 int i;
1284
7b3894be 1285 for (i = 0; i < num_pixels; i++) {
6af28010 1286 //FIXME slow?
7b3894be
NAM
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;
6af28010
RB
1291 }
1292}