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