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