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