]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale_unscaled.c
swscale: split swscale.c in unscaled and generic conversion routines.
[ffmpeg] / libswscale / swscale_unscaled.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <inttypes.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include <assert.h>
27 #include "swscale.h"
28 #include "swscale_internal.h"
29 #include "rgb2rgb.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/avutil.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/bswap.h"
35 #include "libavutil/pixdesc.h"
36
37 #define RGB2YUV_SHIFT 15
38 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
39 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
40 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
41 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
42 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
43 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
44 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
45 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
46 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
47
48 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
49 {
50     int i;
51     uint8_t *ptr = plane + stride*y;
52     for (i=0; i<height; i++) {
53         memset(ptr, val, width);
54         ptr += stride;
55     }
56 }
57
58 static void copyPlane(const uint8_t *src, int srcStride,
59                       int srcSliceY, int srcSliceH, int width,
60                       uint8_t *dst, int dstStride)
61 {
62     dst += dstStride * srcSliceY;
63     if (dstStride == srcStride && srcStride > 0) {
64         memcpy(dst, src, srcSliceH * dstStride);
65     } else {
66         int i;
67         for (i=0; i<srcSliceH; i++) {
68             memcpy(dst, src, width);
69             src += srcStride;
70             dst += dstStride;
71         }
72     }
73 }
74
75 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
76                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
77 {
78     uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
79
80     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
81               dstParam[0], dstStride[0]);
82
83     if (c->dstFormat == PIX_FMT_NV12)
84         interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
85     else
86         interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
87
88     return srcSliceH;
89 }
90
91 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
92                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
93 {
94     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
95
96     yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
97
98     return srcSliceH;
99 }
100
101 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
102                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
103 {
104     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
105
106     yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
107
108     return srcSliceH;
109 }
110
111 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
112                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
113 {
114     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
115
116     yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
117
118     return srcSliceH;
119 }
120
121 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
122                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
123 {
124     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
125
126     yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
127
128     return srcSliceH;
129 }
130
131 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
132                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
133 {
134     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
135     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
136     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
137
138     yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
139
140     if (dstParam[3])
141         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
142
143     return srcSliceH;
144 }
145
146 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
147                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
148 {
149     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
150     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
151     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
152
153     yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
154
155     return srcSliceH;
156 }
157
158 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
159                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
160 {
161     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
162     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
163     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
164
165     uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
166
167     if (dstParam[3])
168         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
169
170     return srcSliceH;
171 }
172
173 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
174                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
175 {
176     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
177     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
178     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
179
180     uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
181
182     return srcSliceH;
183 }
184
185 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
186 {
187     int i;
188     for (i=0; i<num_pixels; i++)
189         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
190 }
191
192 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
193 {
194     int i;
195
196     for (i=0; i<num_pixels; i++)
197         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
198 }
199
200 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
201 {
202     int i;
203
204     for (i=0; i<num_pixels; i++) {
205         //FIXME slow?
206         dst[0]= palette[src[i<<1]*4+0];
207         dst[1]= palette[src[i<<1]*4+1];
208         dst[2]= palette[src[i<<1]*4+2];
209         dst+= 3;
210     }
211 }
212
213 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
214                            int srcSliceH, uint8_t* dst[], int dstStride[])
215 {
216     const enum PixelFormat srcFormat= c->srcFormat;
217     const enum PixelFormat dstFormat= c->dstFormat;
218     void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
219                  const uint8_t *palette)=NULL;
220     int i;
221     uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
222     const uint8_t *srcPtr= src[0];
223
224     if (srcFormat == PIX_FMT_Y400A) {
225         switch (dstFormat) {
226         case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
227         case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
228         case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
229         case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
230         case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
231         case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
232         }
233     } else if (usePal(srcFormat)) {
234         switch (dstFormat) {
235         case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
236         case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
237         case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
238         case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
239         case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
240         case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
241         }
242     }
243
244     if (!conv)
245         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
246                sws_format_name(srcFormat), sws_format_name(dstFormat));
247     else {
248         for (i=0; i<srcSliceH; i++) {
249             conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
250             srcPtr+= srcStride[0];
251             dstPtr+= dstStride[0];
252         }
253     }
254
255     return srcSliceH;
256 }
257
258 #define isRGBA32(x) (            \
259            (x) == PIX_FMT_ARGB   \
260         || (x) == PIX_FMT_RGBA   \
261         || (x) == PIX_FMT_BGRA   \
262         || (x) == PIX_FMT_ABGR   \
263         )
264
265 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
266 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
267                            int srcSliceH, uint8_t* dst[], int dstStride[])
268 {
269     const enum PixelFormat srcFormat= c->srcFormat;
270     const enum PixelFormat dstFormat= c->dstFormat;
271     const int srcBpp= (c->srcFormatBpp + 7) >> 3;
272     const int dstBpp= (c->dstFormatBpp + 7) >> 3;
273     const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
274     const int dstId= c->dstFormatBpp >> 2;
275     void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
276
277 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
278
279     if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
280         if (     CONV_IS(ABGR, RGBA)
281               || CONV_IS(ARGB, BGRA)
282               || CONV_IS(BGRA, ARGB)
283               || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
284         else if (CONV_IS(ABGR, ARGB)
285               || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
286         else if (CONV_IS(ABGR, BGRA)
287               || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
288         else if (CONV_IS(BGRA, RGBA)
289               || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
290         else if (CONV_IS(BGRA, ABGR)
291               || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
292     } else
293     /* BGR -> BGR */
294     if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
295        || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
296         switch(srcId | (dstId<<4)) {
297         case 0x34: conv= rgb16to15; break;
298         case 0x36: conv= rgb24to15; break;
299         case 0x38: conv= rgb32to15; break;
300         case 0x43: conv= rgb15to16; break;
301         case 0x46: conv= rgb24to16; break;
302         case 0x48: conv= rgb32to16; break;
303         case 0x63: conv= rgb15to24; break;
304         case 0x64: conv= rgb16to24; break;
305         case 0x68: conv= rgb32to24; break;
306         case 0x83: conv= rgb15to32; break;
307         case 0x84: conv= rgb16to32; break;
308         case 0x86: conv= rgb24to32; break;
309         }
310     } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
311              || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
312         switch(srcId | (dstId<<4)) {
313         case 0x33: conv= rgb15tobgr15; break;
314         case 0x34: conv= rgb16tobgr15; break;
315         case 0x36: conv= rgb24tobgr15; break;
316         case 0x38: conv= rgb32tobgr15; break;
317         case 0x43: conv= rgb15tobgr16; break;
318         case 0x44: conv= rgb16tobgr16; break;
319         case 0x46: conv= rgb24tobgr16; break;
320         case 0x48: conv= rgb32tobgr16; break;
321         case 0x63: conv= rgb15tobgr24; break;
322         case 0x64: conv= rgb16tobgr24; break;
323         case 0x66: conv= rgb24tobgr24; break;
324         case 0x68: conv= rgb32tobgr24; break;
325         case 0x83: conv= rgb15tobgr32; break;
326         case 0x84: conv= rgb16tobgr32; break;
327         case 0x86: conv= rgb24tobgr32; break;
328         }
329     }
330
331     if (!conv) {
332         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
333                sws_format_name(srcFormat), sws_format_name(dstFormat));
334     } else {
335         const uint8_t *srcPtr= src[0];
336               uint8_t *dstPtr= dst[0];
337         if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
338             srcPtr += ALT32_CORR;
339
340         if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
341             dstPtr += ALT32_CORR;
342
343         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
344             conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
345         else {
346             int i;
347             dstPtr += dstStride[0]*srcSliceY;
348
349             for (i=0; i<srcSliceH; i++) {
350                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
351                 srcPtr+= srcStride[0];
352                 dstPtr+= dstStride[0];
353             }
354         }
355     }
356     return srcSliceH;
357 }
358
359 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
360                               int srcSliceH, uint8_t* dst[], int dstStride[])
361 {
362     rgb24toyv12(
363         src[0],
364         dst[0]+ srcSliceY    *dstStride[0],
365         dst[1]+(srcSliceY>>1)*dstStride[1],
366         dst[2]+(srcSliceY>>1)*dstStride[2],
367         c->srcW, srcSliceH,
368         dstStride[0], dstStride[1], srcStride[0]);
369     if (dst[3])
370         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
371     return srcSliceH;
372 }
373
374 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
375                              int srcSliceH, uint8_t* dst[], int dstStride[])
376 {
377     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
378               dst[0], dstStride[0]);
379
380     planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
381              srcSliceH >> 2, srcStride[1], dstStride[1]);
382     planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
383              srcSliceH >> 2, srcStride[2], dstStride[2]);
384     if (dst[3])
385         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
386     return srcSliceH;
387 }
388
389 /* unscaled copy like stuff (assumes nearly identical formats) */
390 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
391                              int srcSliceH, uint8_t* dst[], int dstStride[])
392 {
393     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
394         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
395     else {
396         int i;
397         const uint8_t *srcPtr= src[0];
398         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
399         int length=0;
400
401         /* universal length finder */
402         while(length+c->srcW <= FFABS(dstStride[0])
403            && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
404         assert(length!=0);
405
406         for (i=0; i<srcSliceH; i++) {
407             memcpy(dstPtr, srcPtr, length);
408             srcPtr+= srcStride[0];
409             dstPtr+= dstStride[0];
410         }
411     }
412     return srcSliceH;
413 }
414
415 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
416                              int srcSliceH, uint8_t* dst[], int dstStride[])
417 {
418     int plane, i, j;
419     for (plane=0; plane<4; plane++) {
420         int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
421         int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
422         int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
423         const uint8_t *srcPtr= src[plane];
424         uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
425
426         if (!dst[plane]) continue;
427         // ignore palette for GRAY8
428         if (plane == 1 && !dst[2]) continue;
429         if (!src[plane] || (plane == 1 && !src[2])) {
430             if(is16BPS(c->dstFormat))
431                 length*=2;
432             fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
433         } else {
434             if(is9_OR_10BPS(c->srcFormat)) {
435                 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
436                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
437                 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
438
439                 if (is16BPS(c->dstFormat)) {
440                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
441 #define COPY9_OR_10TO16(rfunc, wfunc) \
442                     for (i = 0; i < height; i++) { \
443                         for (j = 0; j < length; j++) { \
444                             int srcpx = rfunc(&srcPtr2[j]); \
445                             wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
446                         } \
447                         dstPtr2 += dstStride[plane]/2; \
448                         srcPtr2 += srcStride[plane]/2; \
449                     }
450                     if (isBE(c->dstFormat)) {
451                         if (isBE(c->srcFormat)) {
452                             COPY9_OR_10TO16(AV_RB16, AV_WB16);
453                         } else {
454                             COPY9_OR_10TO16(AV_RL16, AV_WB16);
455                         }
456                     } else {
457                         if (isBE(c->srcFormat)) {
458                             COPY9_OR_10TO16(AV_RB16, AV_WL16);
459                         } else {
460                             COPY9_OR_10TO16(AV_RL16, AV_WL16);
461                         }
462                     }
463                 } else if (is9_OR_10BPS(c->dstFormat)) {
464                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
465 #define COPY9_OR_10TO9_OR_10(loop) \
466                     for (i = 0; i < height; i++) { \
467                         for (j = 0; j < length; j++) { \
468                             loop; \
469                         } \
470                         dstPtr2 += dstStride[plane]/2; \
471                         srcPtr2 += srcStride[plane]/2; \
472                     }
473 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
474                     if (dst_depth > src_depth) { \
475                         COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
476                             wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
477                     } else if (dst_depth < src_depth) { \
478                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
479                     } else { \
480                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
481                     }
482                     if (isBE(c->dstFormat)) {
483                         if (isBE(c->srcFormat)) {
484                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
485                         } else {
486                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
487                         }
488                     } else {
489                         if (isBE(c->srcFormat)) {
490                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
491                         } else {
492                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
493                         }
494                     }
495                 } else {
496                     // FIXME Maybe dither instead.
497 #define COPY9_OR_10TO8(rfunc) \
498                     for (i = 0; i < height; i++) { \
499                         for (j = 0; j < length; j++) { \
500                             dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
501                         } \
502                         dstPtr  += dstStride[plane]; \
503                         srcPtr2 += srcStride[plane]/2; \
504                     }
505                     if (isBE(c->srcFormat)) {
506                         COPY9_OR_10TO8(AV_RB16);
507                     } else {
508                         COPY9_OR_10TO8(AV_RL16);
509                     }
510                 }
511             } else if(is9_OR_10BPS(c->dstFormat)) {
512                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
513                 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
514
515                 if (is16BPS(c->srcFormat)) {
516                     const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
517 #define COPY16TO9_OR_10(rfunc, wfunc) \
518                     for (i = 0; i < height; i++) { \
519                         for (j = 0; j < length; j++) { \
520                             wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
521                         } \
522                         dstPtr2 += dstStride[plane]/2; \
523                         srcPtr2 += srcStride[plane]/2; \
524                     }
525                     if (isBE(c->dstFormat)) {
526                         if (isBE(c->srcFormat)) {
527                             COPY16TO9_OR_10(AV_RB16, AV_WB16);
528                         } else {
529                             COPY16TO9_OR_10(AV_RL16, AV_WB16);
530                         }
531                     } else {
532                         if (isBE(c->srcFormat)) {
533                             COPY16TO9_OR_10(AV_RB16, AV_WL16);
534                         } else {
535                             COPY16TO9_OR_10(AV_RL16, AV_WL16);
536                         }
537                     }
538                 } else /* 8bit */ {
539 #define COPY8TO9_OR_10(wfunc) \
540                     for (i = 0; i < height; i++) { \
541                         for (j = 0; j < length; j++) { \
542                             const int srcpx = srcPtr[j]; \
543                             wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
544                         } \
545                         dstPtr2 += dstStride[plane]/2; \
546                         srcPtr  += srcStride[plane]; \
547                     }
548                     if (isBE(c->dstFormat)) {
549                         COPY8TO9_OR_10(AV_WB16);
550                     } else {
551                         COPY8TO9_OR_10(AV_WL16);
552                     }
553                 }
554             } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
555                 if (!isBE(c->srcFormat)) srcPtr++;
556                 for (i=0; i<height; i++) {
557                     for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
558                     srcPtr+= srcStride[plane];
559                     dstPtr+= dstStride[plane];
560                 }
561             } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
562                 for (i=0; i<height; i++) {
563                     for (j=0; j<length; j++) {
564                         dstPtr[ j<<1   ] = srcPtr[j];
565                         dstPtr[(j<<1)+1] = srcPtr[j];
566                     }
567                     srcPtr+= srcStride[plane];
568                     dstPtr+= dstStride[plane];
569                 }
570             } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
571                   && isBE(c->srcFormat) != isBE(c->dstFormat)) {
572
573                 for (i=0; i<height; i++) {
574                     for (j=0; j<length; j++)
575                         ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
576                     srcPtr+= srcStride[plane];
577                     dstPtr+= dstStride[plane];
578                 }
579             } else if (dstStride[plane] == srcStride[plane] &&
580                        srcStride[plane] > 0 && srcStride[plane] == length) {
581                 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
582                        height*dstStride[plane]);
583             } else {
584                 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
585                     length*=2;
586                 for (i=0; i<height; i++) {
587                     memcpy(dstPtr, srcPtr, length);
588                     srcPtr+= srcStride[plane];
589                     dstPtr+= dstStride[plane];
590                 }
591             }
592         }
593     }
594     return srcSliceH;
595 }
596
597 void ff_get_unscaled_swscale(SwsContext *c)
598 {
599     const enum PixelFormat srcFormat = c->srcFormat;
600     const enum PixelFormat dstFormat = c->dstFormat;
601     const int flags = c->flags;
602     const int dstH = c->dstH;
603     int needsDither;
604
605     needsDither= isAnyRGB(dstFormat)
606         &&  c->dstFormatBpp < 24
607         && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
608
609     /* yv12_to_nv12 */
610     if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
611         c->swScale= planarToNv12Wrapper;
612     }
613     /* yuv2bgr */
614     if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
615         && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
616         c->swScale= ff_yuv2rgb_get_func_ptr(c);
617     }
618
619     if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
620         c->swScale= yvu9ToYv12Wrapper;
621     }
622
623     /* bgr24toYV12 */
624     if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
625         c->swScale= bgr24ToYv12Wrapper;
626
627     /* RGB/BGR -> RGB/BGR (no dither needed forms) */
628     if (   isAnyRGB(srcFormat)
629         && isAnyRGB(dstFormat)
630         && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
631         && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
632         && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
633         && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
634         && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
635         && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
636         && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
637         && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
638         && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
639         && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
640         && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
641         && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
642         && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
643         c->swScale= rgbToRgbWrapper;
644
645     if ((usePal(srcFormat) && (
646         dstFormat == PIX_FMT_RGB32   ||
647         dstFormat == PIX_FMT_RGB32_1 ||
648         dstFormat == PIX_FMT_RGB24   ||
649         dstFormat == PIX_FMT_BGR32   ||
650         dstFormat == PIX_FMT_BGR32_1 ||
651         dstFormat == PIX_FMT_BGR24)))
652         c->swScale= palToRgbWrapper;
653
654     if (srcFormat == PIX_FMT_YUV422P) {
655         if (dstFormat == PIX_FMT_YUYV422)
656             c->swScale= yuv422pToYuy2Wrapper;
657         else if (dstFormat == PIX_FMT_UYVY422)
658             c->swScale= yuv422pToUyvyWrapper;
659     }
660
661     /* LQ converters if -sws 0 or -sws 4*/
662     if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
663         /* yv12_to_yuy2 */
664         if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
665             if (dstFormat == PIX_FMT_YUYV422)
666                 c->swScale= planarToYuy2Wrapper;
667             else if (dstFormat == PIX_FMT_UYVY422)
668                 c->swScale= planarToUyvyWrapper;
669         }
670     }
671     if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
672         c->swScale= yuyvToYuv420Wrapper;
673     if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
674         c->swScale= uyvyToYuv420Wrapper;
675     if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
676         c->swScale= yuyvToYuv422Wrapper;
677     if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
678         c->swScale= uyvyToYuv422Wrapper;
679
680     /* simple copy */
681     if (  srcFormat == dstFormat
682         || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
683         || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
684         || (isPlanarYUV(srcFormat) && isGray(dstFormat))
685         || (isPlanarYUV(dstFormat) && isGray(srcFormat))
686         || (isGray(dstFormat) && isGray(srcFormat))
687         || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
688             && c->chrDstHSubSample == c->chrSrcHSubSample
689             && c->chrDstVSubSample == c->chrSrcVSubSample
690             && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
691             && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
692     {
693         if (isPacked(c->srcFormat))
694             c->swScale= packedCopyWrapper;
695         else /* Planar YUV or gray */
696             c->swScale= planarCopyWrapper;
697     }
698
699     if (ARCH_BFIN)
700         ff_bfin_get_unscaled_swscale(c);
701     if (HAVE_ALTIVEC)
702         ff_swscale_get_unscaled_altivec(c);
703 }
704
705 static void reset_ptr(const uint8_t* src[], int format)
706 {
707     if(!isALPHA(format))
708         src[3]=NULL;
709     if(!isPlanarYUV(format)) {
710         src[3]=src[2]=NULL;
711
712         if (!usePal(format))
713             src[1]= NULL;
714     }
715 }
716
717 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
718                                 const int linesizes[4])
719 {
720     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
721     int i;
722
723     for (i = 0; i < 4; i++) {
724         int plane = desc->comp[i].plane;
725         if (!data[plane] || !linesizes[plane])
726             return 0;
727     }
728
729     return 1;
730 }
731
732 /**
733  * swscale wrapper, so we don't need to export the SwsContext.
734  * Assumes planar YUV to be in YUV order instead of YVU.
735  */
736 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
737               int srcSliceH, uint8_t* const dst[], const int dstStride[])
738 {
739     int i;
740     const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
741     uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
742
743     // do not mess up sliceDir if we have a "trailing" 0-size slice
744     if (srcSliceH == 0)
745         return 0;
746
747     if (!check_image_pointers(src, c->srcFormat, srcStride)) {
748         av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
749         return 0;
750     }
751     if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
752         av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
753         return 0;
754     }
755
756     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
757         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
758         return 0;
759     }
760     if (c->sliceDir == 0) {
761         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
762     }
763
764     if (usePal(c->srcFormat)) {
765         for (i=0; i<256; i++) {
766             int p, r, g, b,y,u,v;
767             if(c->srcFormat == PIX_FMT_PAL8) {
768                 p=((const uint32_t*)(src[1]))[i];
769                 r= (p>>16)&0xFF;
770                 g= (p>> 8)&0xFF;
771                 b=  p     &0xFF;
772             } else if(c->srcFormat == PIX_FMT_RGB8) {
773                 r= (i>>5    )*36;
774                 g= ((i>>2)&7)*36;
775                 b= (i&3     )*85;
776             } else if(c->srcFormat == PIX_FMT_BGR8) {
777                 b= (i>>6    )*85;
778                 g= ((i>>3)&7)*36;
779                 r= (i&7     )*36;
780             } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
781                 r= (i>>3    )*255;
782                 g= ((i>>1)&3)*85;
783                 b= (i&1     )*255;
784             } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
785                 r = g = b = i;
786             } else {
787                 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
788                 b= (i>>3    )*255;
789                 g= ((i>>1)&3)*85;
790                 r= (i&1     )*255;
791             }
792             y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
793             u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
794             v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
795             c->pal_yuv[i]= y + (u<<8) + (v<<16);
796
797             switch(c->dstFormat) {
798             case PIX_FMT_BGR32:
799 #if !HAVE_BIGENDIAN
800             case PIX_FMT_RGB24:
801 #endif
802                 c->pal_rgb[i]=  r + (g<<8) + (b<<16);
803                 break;
804             case PIX_FMT_BGR32_1:
805 #if HAVE_BIGENDIAN
806             case PIX_FMT_BGR24:
807 #endif
808                 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
809                 break;
810             case PIX_FMT_RGB32_1:
811 #if HAVE_BIGENDIAN
812             case PIX_FMT_RGB24:
813 #endif
814                 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
815                 break;
816             case PIX_FMT_RGB32:
817 #if !HAVE_BIGENDIAN
818             case PIX_FMT_BGR24:
819 #endif
820             default:
821                 c->pal_rgb[i]=  b + (g<<8) + (r<<16);
822             }
823         }
824     }
825
826     // copy strides, so they can safely be modified
827     if (c->sliceDir == 1) {
828         // slices go from top to bottom
829         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
830         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
831
832         reset_ptr(src2, c->srcFormat);
833         reset_ptr((const uint8_t**)dst2, c->dstFormat);
834
835         /* reset slice direction at end of frame */
836         if (srcSliceY + srcSliceH == c->srcH)
837             c->sliceDir = 0;
838
839         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
840     } else {
841         // slices go from bottom to top => we flip the image internally
842         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
843         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
844
845         src2[0] += (srcSliceH-1)*srcStride[0];
846         if (!usePal(c->srcFormat))
847             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
848         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
849         src2[3] += (srcSliceH-1)*srcStride[3];
850         dst2[0] += ( c->dstH                      -1)*dstStride[0];
851         dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
852         dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
853         dst2[3] += ( c->dstH                      -1)*dstStride[3];
854
855         reset_ptr(src2, c->srcFormat);
856         reset_ptr((const uint8_t**)dst2, c->dstFormat);
857
858         /* reset slice direction at end of frame */
859         if (!srcSliceY)
860             c->sliceDir = 0;
861
862         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
863     }
864 }
865
866 /* Convert the palette to the same packed 32-bit format as the palette */
867 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
868 {
869     int i;
870
871     for (i=0; i<num_pixels; i++)
872         ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
873 }
874
875 /* Palette format: ABCD -> dst format: ABC */
876 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
877 {
878     int i;
879
880     for (i=0; i<num_pixels; i++) {
881         //FIXME slow?
882         dst[0]= palette[src[i]*4+0];
883         dst[1]= palette[src[i]*4+1];
884         dst[2]= palette[src[i]*4+2];
885         dst+= 3;
886     }
887 }