]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
Merge commit 'ba5393a609c723ec8ab7f9727c10fef734c09278'
[ffmpeg] / libswscale / swscale.c
1 /*
2  * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 <math.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "config.h"
34 #include "rgb2rgb.h"
35 #include "swscale_internal.h"
36 #include "swscale.h"
37
38 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128)[8][8] = {
39     {  36, 68,  60, 92,  34, 66,  58, 90, },
40     { 100,  4, 124, 28,  98,  2, 122, 26, },
41     {  52, 84,  44, 76,  50, 82,  42, 74, },
42     { 116, 20, 108, 12, 114, 18, 106, 10, },
43     {  32, 64,  56, 88,  38, 70,  62, 94, },
44     {  96,  0, 120, 24, 102,  6, 126, 30, },
45     {  48, 80,  40, 72,  54, 86,  46, 78, },
46     { 112, 16, 104,  8, 118, 22, 110, 14, },
47 };
48
49 DECLARE_ALIGNED(8, const uint8_t, ff_sws_pb_64)[8] = {
50     64, 64, 64, 64, 64, 64, 64, 64
51 };
52
53 static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
54                                        int height, int y, uint8_t val)
55 {
56     int i;
57     uint8_t *ptr = plane + stride * y;
58     for (i = 0; i < height; i++) {
59         memset(ptr, val, width);
60         ptr += stride;
61     }
62 }
63
64 static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
65                            const uint8_t *_src, const int16_t *filter,
66                            const int32_t *filterPos, int filterSize)
67 {
68     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
69     int i;
70     int32_t *dst        = (int32_t *) _dst;
71     const uint16_t *src = (const uint16_t *) _src;
72     int bits            = desc->comp[0].depth_minus1;
73     int sh              = bits - 4;
74
75     if((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth_minus1<15)
76         sh= 9;
77
78     for (i = 0; i < dstW; i++) {
79         int j;
80         int srcPos = filterPos[i];
81         int val    = 0;
82
83         for (j = 0; j < filterSize; j++) {
84             val += src[srcPos + j] * filter[filterSize * i + j];
85         }
86         // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
87         dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
88     }
89 }
90
91 static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
92                            const uint8_t *_src, const int16_t *filter,
93                            const int32_t *filterPos, int filterSize)
94 {
95     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
96     int i;
97     const uint16_t *src = (const uint16_t *) _src;
98     int sh              = desc->comp[0].depth_minus1;
99
100     if(sh<15)
101         sh= isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : desc->comp[0].depth_minus1;
102
103     for (i = 0; i < dstW; i++) {
104         int j;
105         int srcPos = filterPos[i];
106         int val    = 0;
107
108         for (j = 0; j < filterSize; j++) {
109             val += src[srcPos + j] * filter[filterSize * i + j];
110         }
111         // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
112         dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
113     }
114 }
115
116 // bilinear / bicubic scaling
117 static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
118                           const uint8_t *src, const int16_t *filter,
119                           const int32_t *filterPos, int filterSize)
120 {
121     int i;
122     for (i = 0; i < dstW; i++) {
123         int j;
124         int srcPos = filterPos[i];
125         int val    = 0;
126         for (j = 0; j < filterSize; j++) {
127             val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
128         }
129         dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
130     }
131 }
132
133 static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
134                           const uint8_t *src, const int16_t *filter,
135                           const int32_t *filterPos, int filterSize)
136 {
137     int i;
138     int32_t *dst = (int32_t *) _dst;
139     for (i = 0; i < dstW; i++) {
140         int j;
141         int srcPos = filterPos[i];
142         int val    = 0;
143         for (j = 0; j < filterSize; j++) {
144             val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
145         }
146         dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
147     }
148 }
149
150 // FIXME all pal and rgb srcFormats could do this conversion as well
151 // FIXME all scalers more complex than bilinear could do half of this transform
152 static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
153 {
154     int i;
155     for (i = 0; i < width; i++) {
156         dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
157         dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
158     }
159 }
160
161 static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
162 {
163     int i;
164     for (i = 0; i < width; i++) {
165         dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
166         dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
167     }
168 }
169
170 static void lumRangeToJpeg_c(int16_t *dst, int width)
171 {
172     int i;
173     for (i = 0; i < width; i++)
174         dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
175 }
176
177 static void lumRangeFromJpeg_c(int16_t *dst, int width)
178 {
179     int i;
180     for (i = 0; i < width; i++)
181         dst[i] = (dst[i] * 14071 + 33561947) >> 14;
182 }
183
184 static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
185 {
186     int i;
187     int32_t *dstU = (int32_t *) _dstU;
188     int32_t *dstV = (int32_t *) _dstV;
189     for (i = 0; i < width; i++) {
190         dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
191         dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
192     }
193 }
194
195 static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
196 {
197     int i;
198     int32_t *dstU = (int32_t *) _dstU;
199     int32_t *dstV = (int32_t *) _dstV;
200     for (i = 0; i < width; i++) {
201         dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
202         dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
203     }
204 }
205
206 static void lumRangeToJpeg16_c(int16_t *_dst, int width)
207 {
208     int i;
209     int32_t *dst = (int32_t *) _dst;
210     for (i = 0; i < width; i++)
211         dst[i] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
212 }
213
214 static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
215 {
216     int i;
217     int32_t *dst = (int32_t *) _dst;
218     for (i = 0; i < width; i++)
219         dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
220 }
221
222 static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
223                            const uint8_t *src, int srcW, int xInc)
224 {
225     int i;
226     unsigned int xpos = 0;
227     for (i = 0; i < dstWidth; i++) {
228         register unsigned int xx     = xpos >> 16;
229         register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
230         dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
231         xpos  += xInc;
232     }
233     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
234         dst[i] = src[srcW-1]*128;
235 }
236
237 // *** horizontal scale Y line to temp buffer
238 static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
239                                      const uint8_t *src_in[4],
240                                      int srcW, int xInc,
241                                      const int16_t *hLumFilter,
242                                      const int32_t *hLumFilterPos,
243                                      int hLumFilterSize,
244                                      uint8_t *formatConvBuffer,
245                                      uint32_t *pal, int isAlpha)
246 {
247     void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
248         isAlpha ? c->alpToYV12 : c->lumToYV12;
249     void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
250     const uint8_t *src = src_in[isAlpha ? 3 : 0];
251
252     if (toYV12) {
253         toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
254         src = formatConvBuffer;
255     } else if (c->readLumPlanar && !isAlpha) {
256         c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
257         src = formatConvBuffer;
258     } else if (c->readAlpPlanar && isAlpha) {
259         c->readAlpPlanar(formatConvBuffer, src_in, srcW, NULL);
260         src = formatConvBuffer;
261     }
262
263     if (!c->hyscale_fast) {
264         c->hyScale(c, dst, dstWidth, src, hLumFilter,
265                    hLumFilterPos, hLumFilterSize);
266     } else { // fast bilinear upscale / crap downscale
267         c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
268     }
269
270     if (convertRange)
271         convertRange(dst, dstWidth);
272 }
273
274 static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
275                            int dstWidth, const uint8_t *src1,
276                            const uint8_t *src2, int srcW, int xInc)
277 {
278     int i;
279     unsigned int xpos = 0;
280     for (i = 0; i < dstWidth; i++) {
281         register unsigned int xx     = xpos >> 16;
282         register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
283         dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
284         dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
285         xpos   += xInc;
286     }
287     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
288         dst1[i] = src1[srcW-1]*128;
289         dst2[i] = src2[srcW-1]*128;
290     }
291 }
292
293 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
294                                      int16_t *dst2, int dstWidth,
295                                      const uint8_t *src_in[4],
296                                      int srcW, int xInc,
297                                      const int16_t *hChrFilter,
298                                      const int32_t *hChrFilterPos,
299                                      int hChrFilterSize,
300                                      uint8_t *formatConvBuffer, uint32_t *pal)
301 {
302     const uint8_t *src1 = src_in[1], *src2 = src_in[2];
303     if (c->chrToYV12) {
304         uint8_t *buf2 = formatConvBuffer +
305                         FFALIGN(srcW*2+78, 16);
306         c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
307         src1= formatConvBuffer;
308         src2= buf2;
309     } else if (c->readChrPlanar) {
310         uint8_t *buf2 = formatConvBuffer +
311                         FFALIGN(srcW*2+78, 16);
312         c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
313         src1 = formatConvBuffer;
314         src2 = buf2;
315     }
316
317     if (!c->hcscale_fast) {
318         c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
319         c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
320     } else { // fast bilinear upscale / crap downscale
321         c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
322     }
323
324     if (c->chrConvertRange)
325         c->chrConvertRange(dst1, dst2, dstWidth);
326 }
327
328 #define DEBUG_SWSCALE_BUFFERS 0
329 #define DEBUG_BUFFERS(...)                      \
330     if (DEBUG_SWSCALE_BUFFERS)                  \
331         av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
332
333 static int swScale(SwsContext *c, const uint8_t *src[],
334                    int srcStride[], int srcSliceY,
335                    int srcSliceH, uint8_t *dst[], int dstStride[])
336 {
337     /* load a few things into local vars to make the code more readable?
338      * and faster */
339     const int srcW                   = c->srcW;
340     const int dstW                   = c->dstW;
341     const int dstH                   = c->dstH;
342     const int chrDstW                = c->chrDstW;
343     const int chrSrcW                = c->chrSrcW;
344     const int lumXInc                = c->lumXInc;
345     const int chrXInc                = c->chrXInc;
346     const enum AVPixelFormat dstFormat = c->dstFormat;
347     const int flags                  = c->flags;
348     int32_t *vLumFilterPos           = c->vLumFilterPos;
349     int32_t *vChrFilterPos           = c->vChrFilterPos;
350     int32_t *hLumFilterPos           = c->hLumFilterPos;
351     int32_t *hChrFilterPos           = c->hChrFilterPos;
352     int16_t *hLumFilter              = c->hLumFilter;
353     int16_t *hChrFilter              = c->hChrFilter;
354     int32_t *lumMmxFilter            = c->lumMmxFilter;
355     int32_t *chrMmxFilter            = c->chrMmxFilter;
356     const int vLumFilterSize         = c->vLumFilterSize;
357     const int vChrFilterSize         = c->vChrFilterSize;
358     const int hLumFilterSize         = c->hLumFilterSize;
359     const int hChrFilterSize         = c->hChrFilterSize;
360     int16_t **lumPixBuf              = c->lumPixBuf;
361     int16_t **chrUPixBuf             = c->chrUPixBuf;
362     int16_t **chrVPixBuf             = c->chrVPixBuf;
363     int16_t **alpPixBuf              = c->alpPixBuf;
364     const int vLumBufSize            = c->vLumBufSize;
365     const int vChrBufSize            = c->vChrBufSize;
366     uint8_t *formatConvBuffer        = c->formatConvBuffer;
367     uint32_t *pal                    = c->pal_yuv;
368     yuv2planar1_fn yuv2plane1        = c->yuv2plane1;
369     yuv2planarX_fn yuv2planeX        = c->yuv2planeX;
370     yuv2interleavedX_fn yuv2nv12cX   = c->yuv2nv12cX;
371     yuv2packed1_fn yuv2packed1       = c->yuv2packed1;
372     yuv2packed2_fn yuv2packed2       = c->yuv2packed2;
373     yuv2packedX_fn yuv2packedX       = c->yuv2packedX;
374     yuv2anyX_fn yuv2anyX             = c->yuv2anyX;
375     const int chrSrcSliceY           =                srcSliceY >> c->chrSrcVSubSample;
376     const int chrSrcSliceH           = FF_CEIL_RSHIFT(srcSliceH,   c->chrSrcVSubSample);
377     int should_dither                = is9_OR_10BPS(c->srcFormat) ||
378                                        is16BPS(c->srcFormat);
379     int lastDstY;
380
381     /* vars which will change and which we need to store back in the context */
382     int dstY         = c->dstY;
383     int lumBufIndex  = c->lumBufIndex;
384     int chrBufIndex  = c->chrBufIndex;
385     int lastInLumBuf = c->lastInLumBuf;
386     int lastInChrBuf = c->lastInChrBuf;
387
388     if (!usePal(c->srcFormat)) {
389         pal = c->input_rgb2yuv_table;
390     }
391
392     if (isPacked(c->srcFormat)) {
393         src[0] =
394         src[1] =
395         src[2] =
396         src[3] = src[0];
397         srcStride[0] =
398         srcStride[1] =
399         srcStride[2] =
400         srcStride[3] = srcStride[0];
401     }
402     srcStride[1] <<= c->vChrDrop;
403     srcStride[2] <<= c->vChrDrop;
404
405     DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
406                   src[0], srcStride[0], src[1], srcStride[1],
407                   src[2], srcStride[2], src[3], srcStride[3],
408                   dst[0], dstStride[0], dst[1], dstStride[1],
409                   dst[2], dstStride[2], dst[3], dstStride[3]);
410     DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
411                   srcSliceY, srcSliceH, dstY, dstH);
412     DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
413                   vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
414
415     if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
416         dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
417         static int warnedAlready = 0; // FIXME maybe move this into the context
418         if (flags & SWS_PRINT_INFO && !warnedAlready) {
419             av_log(c, AV_LOG_WARNING,
420                    "Warning: dstStride is not aligned!\n"
421                    "         ->cannot do aligned memory accesses anymore\n");
422             warnedAlready = 1;
423         }
424     }
425
426     if (   (uintptr_t)dst[0]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
427         || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
428         || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
429         || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
430     ) {
431         static int warnedAlready=0;
432         int cpu_flags = av_get_cpu_flags();
433         if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
434             av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
435             warnedAlready=1;
436         }
437     }
438
439     /* Note the user might start scaling the picture in the middle so this
440      * will not get executed. This is not really intended but works
441      * currently, so people might do it. */
442     if (srcSliceY == 0) {
443         lumBufIndex  = -1;
444         chrBufIndex  = -1;
445         dstY         = 0;
446         lastInLumBuf = -1;
447         lastInChrBuf = -1;
448     }
449
450     if (!should_dither) {
451         c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
452     }
453     lastDstY = dstY;
454
455     for (; dstY < dstH; dstY++) {
456         const int chrDstY = dstY >> c->chrDstVSubSample;
457         uint8_t *dest[4]  = {
458             dst[0] + dstStride[0] * dstY,
459             dst[1] + dstStride[1] * chrDstY,
460             dst[2] + dstStride[2] * chrDstY,
461             (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
462         };
463         int use_mmx_vfilter= c->use_mmx_vfilter;
464
465         // First line needed as input
466         const int firstLumSrcY  = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
467         const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
468         // First line needed as input
469         const int firstChrSrcY  = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
470
471         // Last line needed as input
472         int lastLumSrcY  = FFMIN(c->srcH,    firstLumSrcY  + vLumFilterSize) - 1;
473         int lastLumSrcY2 = FFMIN(c->srcH,    firstLumSrcY2 + vLumFilterSize) - 1;
474         int lastChrSrcY  = FFMIN(c->chrSrcH, firstChrSrcY  + vChrFilterSize) - 1;
475         int enough_lines;
476
477         // handle holes (FAST_BILINEAR & weird filters)
478         if (firstLumSrcY > lastInLumBuf)
479             lastInLumBuf = firstLumSrcY - 1;
480         if (firstChrSrcY > lastInChrBuf)
481             lastInChrBuf = firstChrSrcY - 1;
482         av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
483         av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
484
485         DEBUG_BUFFERS("dstY: %d\n", dstY);
486         DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
487                       firstLumSrcY, lastLumSrcY, lastInLumBuf);
488         DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
489                       firstChrSrcY, lastChrSrcY, lastInChrBuf);
490
491         // Do we have enough lines in this slice to output the dstY line
492         enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
493                        lastChrSrcY < FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
494
495         if (!enough_lines) {
496             lastLumSrcY = srcSliceY + srcSliceH - 1;
497             lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
498             DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
499                           lastLumSrcY, lastChrSrcY);
500         }
501
502         // Do horizontal scaling
503         while (lastInLumBuf < lastLumSrcY) {
504             const uint8_t *src1[4] = {
505                 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
506                 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
507                 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
508                 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
509             };
510             lumBufIndex++;
511             av_assert0(lumBufIndex < 2 * vLumBufSize);
512             av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
513             av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
514             hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
515                     hLumFilter, hLumFilterPos, hLumFilterSize,
516                     formatConvBuffer, pal, 0);
517             if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
518                 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
519                         lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
520                         formatConvBuffer, pal, 1);
521             lastInLumBuf++;
522             DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
523                           lumBufIndex, lastInLumBuf);
524         }
525         while (lastInChrBuf < lastChrSrcY) {
526             const uint8_t *src1[4] = {
527                 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
528                 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
529                 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
530                 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
531             };
532             chrBufIndex++;
533             av_assert0(chrBufIndex < 2 * vChrBufSize);
534             av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
535             av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
536             // FIXME replace parameters through context struct (some at least)
537
538             if (c->needs_hcscale)
539                 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
540                         chrDstW, src1, chrSrcW, chrXInc,
541                         hChrFilter, hChrFilterPos, hChrFilterSize,
542                         formatConvBuffer, pal);
543             lastInChrBuf++;
544             DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
545                           chrBufIndex, lastInChrBuf);
546         }
547         // wrap buf index around to stay inside the ring buffer
548         if (lumBufIndex >= vLumBufSize)
549             lumBufIndex -= vLumBufSize;
550         if (chrBufIndex >= vChrBufSize)
551             chrBufIndex -= vChrBufSize;
552         if (!enough_lines)
553             break;  // we can't output a dstY line so let's try with the next slice
554
555 #if HAVE_MMX_INLINE
556         updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
557                               lastInLumBuf, lastInChrBuf);
558 #endif
559         if (should_dither) {
560             c->chrDither8 = dither_8x8_128[chrDstY & 7];
561             c->lumDither8 = dither_8x8_128[dstY    & 7];
562         }
563         if (dstY >= dstH - 2) {
564             /* hmm looks like we can't use MMX here without overwriting
565              * this array's tail */
566             ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
567                                      &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
568             use_mmx_vfilter= 0;
569         }
570
571         {
572             const int16_t **lumSrcPtr  = (const int16_t **)(void*) lumPixBuf  + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
573             const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
574             const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
575             const int16_t **alpSrcPtr  = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
576                                          (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
577             int16_t *vLumFilter = c->vLumFilter;
578             int16_t *vChrFilter = c->vChrFilter;
579
580             if (isPlanarYUV(dstFormat) ||
581                 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
582                 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
583
584                 vLumFilter +=    dstY * vLumFilterSize;
585                 vChrFilter += chrDstY * vChrFilterSize;
586
587 //                 av_assert0(use_mmx_vfilter != (
588 //                                yuv2planeX == yuv2planeX_10BE_c
589 //                             || yuv2planeX == yuv2planeX_10LE_c
590 //                             || yuv2planeX == yuv2planeX_9BE_c
591 //                             || yuv2planeX == yuv2planeX_9LE_c
592 //                             || yuv2planeX == yuv2planeX_16BE_c
593 //                             || yuv2planeX == yuv2planeX_16LE_c
594 //                             || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
595
596                 if(use_mmx_vfilter){
597                     vLumFilter= (int16_t *)c->lumMmxFilter;
598                     vChrFilter= (int16_t *)c->chrMmxFilter;
599                 }
600
601                 if (vLumFilterSize == 1) {
602                     yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
603                 } else {
604                     yuv2planeX(vLumFilter, vLumFilterSize,
605                                lumSrcPtr, dest[0],
606                                dstW, c->lumDither8, 0);
607                 }
608
609                 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
610                     if (yuv2nv12cX) {
611                         yuv2nv12cX(c, vChrFilter,
612                                    vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
613                                    dest[1], chrDstW);
614                     } else if (vChrFilterSize == 1) {
615                         yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
616                         yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
617                     } else {
618                         yuv2planeX(vChrFilter,
619                                    vChrFilterSize, chrUSrcPtr, dest[1],
620                                    chrDstW, c->chrDither8, 0);
621                         yuv2planeX(vChrFilter,
622                                    vChrFilterSize, chrVSrcPtr, dest[2],
623                                    chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
624                     }
625                 }
626
627                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
628                     if(use_mmx_vfilter){
629                         vLumFilter= (int16_t *)c->alpMmxFilter;
630                     }
631                     if (vLumFilterSize == 1) {
632                         yuv2plane1(alpSrcPtr[0], dest[3], dstW,
633                                    c->lumDither8, 0);
634                     } else {
635                         yuv2planeX(vLumFilter,
636                                    vLumFilterSize, alpSrcPtr, dest[3],
637                                    dstW, c->lumDither8, 0);
638                     }
639                 }
640             } else if (yuv2packedX) {
641                 av_assert1(lumSrcPtr  + vLumFilterSize - 1 < (const int16_t **)lumPixBuf  + vLumBufSize * 2);
642                 av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
643                 if (c->yuv2packed1 && vLumFilterSize == 1 &&
644                     vChrFilterSize <= 2) { // unscaled RGB
645                     int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
646                     yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
647                                 alpPixBuf ? *alpSrcPtr : NULL,
648                                 dest[0], dstW, chrAlpha, dstY);
649                 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
650                            vChrFilterSize == 2) { // bilinear upscale RGB
651                     int lumAlpha = vLumFilter[2 * dstY + 1];
652                     int chrAlpha = vChrFilter[2 * dstY + 1];
653                     lumMmxFilter[2] =
654                     lumMmxFilter[3] = vLumFilter[2 * dstY]    * 0x10001;
655                     chrMmxFilter[2] =
656                     chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
657                     yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
658                                 alpPixBuf ? alpSrcPtr : NULL,
659                                 dest[0], dstW, lumAlpha, chrAlpha, dstY);
660                 } else { // general RGB
661                     yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
662                                 lumSrcPtr, vLumFilterSize,
663                                 vChrFilter + dstY * vChrFilterSize,
664                                 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
665                                 alpSrcPtr, dest[0], dstW, dstY);
666                 }
667             } else {
668                 av_assert1(!yuv2packed1 && !yuv2packed2);
669                 yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
670                          lumSrcPtr, vLumFilterSize,
671                          vChrFilter + dstY * vChrFilterSize,
672                          chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
673                          alpSrcPtr, dest, dstW, dstY);
674             }
675         }
676     }
677     if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
678         int length = dstW;
679         int height = dstY - lastDstY;
680
681         if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
682             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
683             fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
684                     1, desc->comp[3].depth_minus1,
685                     isBE(dstFormat));
686         } else
687             fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
688     }
689
690 #if HAVE_MMXEXT_INLINE
691     if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
692         __asm__ volatile ("sfence" ::: "memory");
693 #endif
694     emms_c();
695
696     /* store changed local vars back in the context */
697     c->dstY         = dstY;
698     c->lumBufIndex  = lumBufIndex;
699     c->chrBufIndex  = chrBufIndex;
700     c->lastInLumBuf = lastInLumBuf;
701     c->lastInChrBuf = lastInChrBuf;
702
703     return dstY - lastDstY;
704 }
705
706 static av_cold void sws_init_swScale_c(SwsContext *c)
707 {
708     enum AVPixelFormat srcFormat = c->srcFormat;
709
710     ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
711                              &c->yuv2nv12cX, &c->yuv2packed1,
712                              &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
713
714     ff_sws_init_input_funcs(c);
715
716
717     if (c->srcBpc == 8) {
718         if (c->dstBpc <= 14) {
719             c->hyScale = c->hcScale = hScale8To15_c;
720             if (c->flags & SWS_FAST_BILINEAR) {
721                 c->hyscale_fast = hyscale_fast_c;
722                 c->hcscale_fast = hcscale_fast_c;
723             }
724         } else {
725             c->hyScale = c->hcScale = hScale8To19_c;
726         }
727     } else {
728         c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
729                                                  : hScale16To15_c;
730     }
731
732     if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
733         if (c->dstBpc <= 14) {
734             if (c->srcRange) {
735                 c->lumConvertRange = lumRangeFromJpeg_c;
736                 c->chrConvertRange = chrRangeFromJpeg_c;
737             } else {
738                 c->lumConvertRange = lumRangeToJpeg_c;
739                 c->chrConvertRange = chrRangeToJpeg_c;
740             }
741         } else {
742             if (c->srcRange) {
743                 c->lumConvertRange = lumRangeFromJpeg16_c;
744                 c->chrConvertRange = chrRangeFromJpeg16_c;
745             } else {
746                 c->lumConvertRange = lumRangeToJpeg16_c;
747                 c->chrConvertRange = chrRangeToJpeg16_c;
748             }
749         }
750     }
751
752     if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
753           srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
754         c->needs_hcscale = 1;
755 }
756
757 SwsFunc ff_getSwsFunc(SwsContext *c)
758 {
759     sws_init_swScale_c(c);
760
761     if (HAVE_MMX)
762         ff_sws_init_swScale_mmx(c);
763     if (HAVE_ALTIVEC)
764         ff_sws_init_swScale_altivec(c);
765
766     return swScale;
767 }
768
769 static void reset_ptr(const uint8_t *src[], int format)
770 {
771     if (!isALPHA(format))
772         src[3] = NULL;
773     if (!isPlanar(format)) {
774         src[3] = src[2] = NULL;
775
776         if (!usePal(format))
777             src[1] = NULL;
778     }
779 }
780
781 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
782                                 const int linesizes[4])
783 {
784     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
785     int i;
786
787     for (i = 0; i < 4; i++) {
788         int plane = desc->comp[i].plane;
789         if (!data[plane] || !linesizes[plane])
790             return 0;
791     }
792
793     return 1;
794 }
795
796 static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
797                          const uint16_t *src, int stride, int h)
798 {
799     int xp,yp;
800     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
801
802     for (yp=0; yp<h; yp++) {
803         for (xp=0; xp+2<stride; xp+=3) {
804             int x, y, z, r, g, b;
805
806             if (desc->flags & AV_PIX_FMT_FLAG_BE) {
807                 x = AV_RB16(src + xp + 0);
808                 y = AV_RB16(src + xp + 1);
809                 z = AV_RB16(src + xp + 2);
810             } else {
811                 x = AV_RL16(src + xp + 0);
812                 y = AV_RL16(src + xp + 1);
813                 z = AV_RL16(src + xp + 2);
814             }
815
816             x = c->xyzgamma[x>>4];
817             y = c->xyzgamma[y>>4];
818             z = c->xyzgamma[z>>4];
819
820             // convert from XYZlinear to sRGBlinear
821             r = c->xyz2rgb_matrix[0][0] * x +
822                 c->xyz2rgb_matrix[0][1] * y +
823                 c->xyz2rgb_matrix[0][2] * z >> 12;
824             g = c->xyz2rgb_matrix[1][0] * x +
825                 c->xyz2rgb_matrix[1][1] * y +
826                 c->xyz2rgb_matrix[1][2] * z >> 12;
827             b = c->xyz2rgb_matrix[2][0] * x +
828                 c->xyz2rgb_matrix[2][1] * y +
829                 c->xyz2rgb_matrix[2][2] * z >> 12;
830
831             // limit values to 12-bit depth
832             r = av_clip_c(r,0,4095);
833             g = av_clip_c(g,0,4095);
834             b = av_clip_c(b,0,4095);
835
836             // convert from sRGBlinear to RGB and scale from 12bit to 16bit
837             if (desc->flags & AV_PIX_FMT_FLAG_BE) {
838                 AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
839                 AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
840                 AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
841             } else {
842                 AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
843                 AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
844                 AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
845             }
846         }
847         src += stride;
848         dst += stride;
849     }
850 }
851
852 static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
853                          const uint16_t *src, int stride, int h)
854 {
855     int xp,yp;
856     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
857
858     for (yp=0; yp<h; yp++) {
859         for (xp=0; xp+2<stride; xp+=3) {
860             int x, y, z, r, g, b;
861
862             if (desc->flags & AV_PIX_FMT_FLAG_BE) {
863                 r = AV_RB16(src + xp + 0);
864                 g = AV_RB16(src + xp + 1);
865                 b = AV_RB16(src + xp + 2);
866             } else {
867                 r = AV_RL16(src + xp + 0);
868                 g = AV_RL16(src + xp + 1);
869                 b = AV_RL16(src + xp + 2);
870             }
871
872             r = c->rgbgammainv[r>>4];
873             g = c->rgbgammainv[g>>4];
874             b = c->rgbgammainv[b>>4];
875
876             // convert from sRGBlinear to XYZlinear
877             x = c->rgb2xyz_matrix[0][0] * r +
878                 c->rgb2xyz_matrix[0][1] * g +
879                 c->rgb2xyz_matrix[0][2] * b >> 12;
880             y = c->rgb2xyz_matrix[1][0] * r +
881                 c->rgb2xyz_matrix[1][1] * g +
882                 c->rgb2xyz_matrix[1][2] * b >> 12;
883             z = c->rgb2xyz_matrix[2][0] * r +
884                 c->rgb2xyz_matrix[2][1] * g +
885                 c->rgb2xyz_matrix[2][2] * b >> 12;
886
887             // limit values to 12-bit depth
888             x = av_clip_c(x,0,4095);
889             y = av_clip_c(y,0,4095);
890             z = av_clip_c(z,0,4095);
891
892             // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
893             if (desc->flags & AV_PIX_FMT_FLAG_BE) {
894                 AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
895                 AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
896                 AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
897             } else {
898                 AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
899                 AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
900                 AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
901             }
902         }
903         src += stride;
904         dst += stride;
905     }
906 }
907
908 /**
909  * swscale wrapper, so we don't need to export the SwsContext.
910  * Assumes planar YUV to be in YUV order instead of YVU.
911  */
912 int attribute_align_arg sws_scale(struct SwsContext *c,
913                                   const uint8_t * const srcSlice[],
914                                   const int srcStride[], int srcSliceY,
915                                   int srcSliceH, uint8_t *const dst[],
916                                   const int dstStride[])
917 {
918     int i, ret;
919     const uint8_t *src2[4];
920     uint8_t *dst2[4];
921     uint8_t *rgb0_tmp = NULL;
922
923     if (!srcSlice || !dstStride || !dst || !srcSlice) {
924         av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
925         return 0;
926     }
927     memcpy(src2, srcSlice, sizeof(src2));
928     memcpy(dst2, dst, sizeof(dst2));
929
930     // do not mess up sliceDir if we have a "trailing" 0-size slice
931     if (srcSliceH == 0)
932         return 0;
933
934     if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
935         av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
936         return 0;
937     }
938     if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
939         av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
940         return 0;
941     }
942
943     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
944         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
945         return 0;
946     }
947     if (c->sliceDir == 0) {
948         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
949     }
950
951     if (usePal(c->srcFormat)) {
952         for (i = 0; i < 256; i++) {
953             int p, r, g, b, y, u, v, a = 0xff;
954             if (c->srcFormat == AV_PIX_FMT_PAL8) {
955                 p = ((const uint32_t *)(srcSlice[1]))[i];
956                 a = (p >> 24) & 0xFF;
957                 r = (p >> 16) & 0xFF;
958                 g = (p >>  8) & 0xFF;
959                 b =  p        & 0xFF;
960             } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
961                 r = ( i >> 5     ) * 36;
962                 g = ((i >> 2) & 7) * 36;
963                 b = ( i       & 3) * 85;
964             } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
965                 b = ( i >> 6     ) * 85;
966                 g = ((i >> 3) & 7) * 36;
967                 r = ( i       & 7) * 36;
968             } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
969                 r = ( i >> 3     ) * 255;
970                 g = ((i >> 1) & 3) * 85;
971                 b = ( i       & 1) * 255;
972             } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
973                 r = g = b = i;
974             } else {
975                 av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
976                 b = ( i >> 3     ) * 255;
977                 g = ((i >> 1) & 3) * 85;
978                 r = ( i       & 1) * 255;
979             }
980 #define RGB2YUV_SHIFT 15
981 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
982 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
983 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
984 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
985 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
986 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
987 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
988 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
989 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
990
991             y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
992             u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
993             v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
994             c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
995
996             switch (c->dstFormat) {
997             case AV_PIX_FMT_BGR32:
998 #if !HAVE_BIGENDIAN
999             case AV_PIX_FMT_RGB24:
1000 #endif
1001                 c->pal_rgb[i]=  r + (g<<8) + (b<<16) + ((unsigned)a<<24);
1002                 break;
1003             case AV_PIX_FMT_BGR32_1:
1004 #if HAVE_BIGENDIAN
1005             case AV_PIX_FMT_BGR24:
1006 #endif
1007                 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
1008                 break;
1009             case AV_PIX_FMT_RGB32_1:
1010 #if HAVE_BIGENDIAN
1011             case AV_PIX_FMT_RGB24:
1012 #endif
1013                 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
1014                 break;
1015             case AV_PIX_FMT_RGB32:
1016 #if !HAVE_BIGENDIAN
1017             case AV_PIX_FMT_BGR24:
1018 #endif
1019             default:
1020                 c->pal_rgb[i]=  b + (g<<8) + (r<<16) + ((unsigned)a<<24);
1021             }
1022         }
1023     }
1024
1025     if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
1026         uint8_t *base;
1027         int x,y;
1028         rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1029         if (!rgb0_tmp)
1030             return AVERROR(ENOMEM);
1031
1032         base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1033         for (y=0; y<srcSliceH; y++){
1034             memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
1035             for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
1036                 base[ srcStride[0]*y + x] = 0xFF;
1037             }
1038         }
1039         src2[0] = base;
1040     }
1041
1042     if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1043         uint8_t *base;
1044         rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1045         if (!rgb0_tmp)
1046             return AVERROR(ENOMEM);
1047
1048         base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1049
1050         xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
1051         src2[0] = base;
1052     }
1053
1054     if (!srcSliceY && (c->flags & SWS_BITEXACT) && (c->flags & SWS_ERROR_DIFFUSION) && c->dither_error[0])
1055         for (i = 0; i < 4; i++)
1056             memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
1057
1058
1059     // copy strides, so they can safely be modified
1060     if (c->sliceDir == 1) {
1061         // slices go from top to bottom
1062         int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1063                               srcStride[3] };
1064         int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1065                               dstStride[3] };
1066
1067         reset_ptr(src2, c->srcFormat);
1068         reset_ptr((void*)dst2, c->dstFormat);
1069
1070         /* reset slice direction at end of frame */
1071         if (srcSliceY + srcSliceH == c->srcH)
1072             c->sliceDir = 0;
1073
1074         ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1075                           dstStride2);
1076     } else {
1077         // slices go from bottom to top => we flip the image internally
1078         int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1079                               -srcStride[3] };
1080         int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1081                               -dstStride[3] };
1082
1083         src2[0] += (srcSliceH - 1) * srcStride[0];
1084         if (!usePal(c->srcFormat))
1085             src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1086         src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1087         src2[3] += (srcSliceH - 1) * srcStride[3];
1088         dst2[0] += ( c->dstH                         - 1) * dstStride[0];
1089         dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1090         dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1091         dst2[3] += ( c->dstH                         - 1) * dstStride[3];
1092
1093         reset_ptr(src2, c->srcFormat);
1094         reset_ptr((void*)dst2, c->dstFormat);
1095
1096         /* reset slice direction at end of frame */
1097         if (!srcSliceY)
1098             c->sliceDir = 0;
1099
1100         ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1101                           srcSliceH, dst2, dstStride2);
1102     }
1103
1104
1105     if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1106         /* replace on the same data */
1107         rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);
1108     }
1109
1110     av_free(rgb0_tmp);
1111     return ret;
1112 }
1113