]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
5a28ce1d5873101f006e3bf10acb4eb37f802052
[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 <assert.h>
22 #include <inttypes.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/bswap.h"
30 #include "libavutil/cpu.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, dither_8x8_128)[8][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 };
49
50 DECLARE_ALIGNED(8, const uint8_t, ff_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     int i;
70     int32_t *dst        = (int32_t *) _dst;
71     const uint16_t *src = (const uint16_t *) _src;
72     int bits            = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
73     int sh              = bits - 4;
74
75     if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
76         sh= 9;
77
78     for (i = 0; i < dstW; i++) {
79         int j;
80         int srcPos = filterPos[i];
81         int val    = 0;
82
83         for (j = 0; j < filterSize; j++) {
84             val += src[srcPos + j] * filter[filterSize * i + j];
85         }
86         // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
87         dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
88     }
89 }
90
91 static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
92                            const uint8_t *_src, const int16_t *filter,
93                            const int32_t *filterPos, int filterSize)
94 {
95     int i;
96     const uint16_t *src = (const uint16_t *) _src;
97     int sh              = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
98
99     if(sh<15)
100         sh= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
101
102     for (i = 0; i < dstW; i++) {
103         int j;
104         int srcPos = filterPos[i];
105         int val    = 0;
106
107         for (j = 0; j < filterSize; j++) {
108             val += src[srcPos + j] * filter[filterSize * i + j];
109         }
110         // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
111         dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
112     }
113 }
114
115 // bilinear / bicubic scaling
116 static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
117                           const uint8_t *src, const int16_t *filter,
118                           const int32_t *filterPos, int filterSize)
119 {
120     int i;
121     for (i = 0; i < dstW; i++) {
122         int j;
123         int srcPos = filterPos[i];
124         int val    = 0;
125         for (j = 0; j < filterSize; j++) {
126             val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
127         }
128         dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
129     }
130 }
131
132 static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
133                           const uint8_t *src, const int16_t *filter,
134                           const int32_t *filterPos, int filterSize)
135 {
136     int i;
137     int32_t *dst = (int32_t *) _dst;
138     for (i = 0; i < dstW; i++) {
139         int j;
140         int srcPos = filterPos[i];
141         int val    = 0;
142         for (j = 0; j < filterSize; j++) {
143             val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
144         }
145         dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
146     }
147 }
148
149 // FIXME all pal and rgb srcFormats could do this convertion as well
150 // FIXME all scalers more complex than bilinear could do half of this transform
151 static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
152 {
153     int i;
154     for (i = 0; i < width; i++) {
155         dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
156         dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
157     }
158 }
159
160 static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
161 {
162     int i;
163     for (i = 0; i < width; i++) {
164         dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
165         dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
166     }
167 }
168
169 static void lumRangeToJpeg_c(int16_t *dst, int width)
170 {
171     int i;
172     for (i = 0; i < width; i++)
173         dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
174 }
175
176 static void lumRangeFromJpeg_c(int16_t *dst, int width)
177 {
178     int i;
179     for (i = 0; i < width; i++)
180         dst[i] = (dst[i] * 14071 + 33561947) >> 14;
181 }
182
183 static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
184 {
185     int i;
186     int32_t *dstU = (int32_t *) _dstU;
187     int32_t *dstV = (int32_t *) _dstV;
188     for (i = 0; i < width; i++) {
189         dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
190         dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
191     }
192 }
193
194 static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
195 {
196     int i;
197     int32_t *dstU = (int32_t *) _dstU;
198     int32_t *dstV = (int32_t *) _dstV;
199     for (i = 0; i < width; i++) {
200         dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
201         dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
202     }
203 }
204
205 static void lumRangeToJpeg16_c(int16_t *_dst, int width)
206 {
207     int i;
208     int32_t *dst = (int32_t *) _dst;
209     for (i = 0; i < width; i++)
210         dst[i] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
211 }
212
213 static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
214 {
215     int i;
216     int32_t *dst = (int32_t *) _dst;
217     for (i = 0; i < width; i++)
218         dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
219 }
220
221 static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
222                            const uint8_t *src, int srcW, int xInc)
223 {
224     int i;
225     unsigned int xpos = 0;
226     for (i = 0; i < dstWidth; i++) {
227         register unsigned int xx     = xpos >> 16;
228         register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
229         dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
230         xpos  += xInc;
231     }
232     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
233         dst[i] = src[srcW-1]*128;
234 }
235
236 // *** horizontal scale Y line to temp buffer
237 static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
238                                      const uint8_t *src_in[4],
239                                      int srcW, int xInc,
240                                      const int16_t *hLumFilter,
241                                      const int32_t *hLumFilterPos,
242                                      int hLumFilterSize,
243                                      uint8_t *formatConvBuffer,
244                                      uint32_t *pal, int isAlpha)
245 {
246     void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
247         isAlpha ? c->alpToYV12 : c->lumToYV12;
248     void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
249     const uint8_t *src = src_in[isAlpha ? 3 : 0];
250
251     if (toYV12) {
252         toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
253         src = formatConvBuffer;
254     } else if (c->readLumPlanar && !isAlpha) {
255         c->readLumPlanar(formatConvBuffer, src_in, srcW);
256         src = formatConvBuffer;
257     }
258
259     if (!c->hyscale_fast) {
260         c->hyScale(c, dst, dstWidth, src, hLumFilter,
261                    hLumFilterPos, hLumFilterSize);
262     } else { // fast bilinear upscale / crap downscale
263         c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
264     }
265
266     if (convertRange)
267         convertRange(dst, dstWidth);
268 }
269
270 static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
271                            int dstWidth, const uint8_t *src1,
272                            const uint8_t *src2, int srcW, int xInc)
273 {
274     int i;
275     unsigned int xpos = 0;
276     for (i = 0; i < dstWidth; i++) {
277         register unsigned int xx     = xpos >> 16;
278         register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
279         dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
280         dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
281         xpos   += xInc;
282     }
283     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
284         dst1[i] = src1[srcW-1]*128;
285         dst2[i] = src2[srcW-1]*128;
286     }
287 }
288
289 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
290                                      int16_t *dst2, int dstWidth,
291                                      const uint8_t *src_in[4],
292                                      int srcW, int xInc,
293                                      const int16_t *hChrFilter,
294                                      const int32_t *hChrFilterPos,
295                                      int hChrFilterSize,
296                                      uint8_t *formatConvBuffer, uint32_t *pal)
297 {
298     const uint8_t *src1 = src_in[1], *src2 = src_in[2];
299     if (c->chrToYV12) {
300         uint8_t *buf2 = formatConvBuffer +
301                         FFALIGN(srcW*2+78, 16);
302         c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
303         src1= formatConvBuffer;
304         src2= buf2;
305     } else if (c->readChrPlanar) {
306         uint8_t *buf2 = formatConvBuffer +
307                         FFALIGN(srcW*2+78, 16);
308         c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
309         src1 = formatConvBuffer;
310         src2 = buf2;
311     }
312
313     if (!c->hcscale_fast) {
314         c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
315         c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
316     } else { // fast bilinear upscale / crap downscale
317         c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
318     }
319
320     if (c->chrConvertRange)
321         c->chrConvertRange(dst1, dst2, dstWidth);
322 }
323
324 #define DEBUG_SWSCALE_BUFFERS 0
325 #define DEBUG_BUFFERS(...)                      \
326     if (DEBUG_SWSCALE_BUFFERS)                  \
327         av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
328
329 static int swScale(SwsContext *c, const uint8_t *src[],
330                    int srcStride[], int srcSliceY,
331                    int srcSliceH, uint8_t *dst[], int dstStride[])
332 {
333     /* load a few things into local vars to make the code more readable?
334      * and faster */
335     const int srcW                   = c->srcW;
336     const int dstW                   = c->dstW;
337     const int dstH                   = c->dstH;
338     const int chrDstW                = c->chrDstW;
339     const int chrSrcW                = c->chrSrcW;
340     const int lumXInc                = c->lumXInc;
341     const int chrXInc                = c->chrXInc;
342     const enum PixelFormat dstFormat = c->dstFormat;
343     const int flags                  = c->flags;
344     int32_t *vLumFilterPos           = c->vLumFilterPos;
345     int32_t *vChrFilterPos           = c->vChrFilterPos;
346     int32_t *hLumFilterPos           = c->hLumFilterPos;
347     int32_t *hChrFilterPos           = c->hChrFilterPos;
348     int16_t *vLumFilter              = c->vLumFilter;
349     int16_t *vChrFilter              = c->vChrFilter;
350     int16_t *hLumFilter              = c->hLumFilter;
351     int16_t *hChrFilter              = c->hChrFilter;
352     int32_t *lumMmxFilter            = c->lumMmxFilter;
353     int32_t *chrMmxFilter            = c->chrMmxFilter;
354     const int vLumFilterSize         = c->vLumFilterSize;
355     const int vChrFilterSize         = c->vChrFilterSize;
356     const int hLumFilterSize         = c->hLumFilterSize;
357     const int hChrFilterSize         = c->hChrFilterSize;
358     int16_t **lumPixBuf              = c->lumPixBuf;
359     int16_t **chrUPixBuf             = c->chrUPixBuf;
360     int16_t **chrVPixBuf             = c->chrVPixBuf;
361     int16_t **alpPixBuf              = c->alpPixBuf;
362     const int vLumBufSize            = c->vLumBufSize;
363     const int vChrBufSize            = c->vChrBufSize;
364     uint8_t *formatConvBuffer        = c->formatConvBuffer;
365     uint32_t *pal                    = c->pal_yuv;
366     yuv2planar1_fn yuv2plane1        = c->yuv2plane1;
367     yuv2planarX_fn yuv2planeX        = c->yuv2planeX;
368     yuv2interleavedX_fn yuv2nv12cX   = c->yuv2nv12cX;
369     yuv2packed1_fn yuv2packed1       = c->yuv2packed1;
370     yuv2packed2_fn yuv2packed2       = c->yuv2packed2;
371     yuv2packedX_fn yuv2packedX       = c->yuv2packedX;
372     const int chrSrcSliceY           =     srcSliceY  >> c->chrSrcVSubSample;
373     const int chrSrcSliceH           = -((-srcSliceH) >> c->chrSrcVSubSample);
374     int should_dither                = is9_OR_10BPS(c->srcFormat) ||
375                                        is16BPS(c->srcFormat);
376     int lastDstY;
377
378     /* vars which will change and which we need to store back in the context */
379     int dstY         = c->dstY;
380     int lumBufIndex  = c->lumBufIndex;
381     int chrBufIndex  = c->chrBufIndex;
382     int lastInLumBuf = c->lastInLumBuf;
383     int lastInChrBuf = c->lastInChrBuf;
384
385     if (isPacked(c->srcFormat)) {
386         src[0] =
387         src[1] =
388         src[2] =
389         src[3] = src[0];
390         srcStride[0] =
391         srcStride[1] =
392         srcStride[2] =
393         srcStride[3] = srcStride[0];
394     }
395     srcStride[1] <<= c->vChrDrop;
396     srcStride[2] <<= c->vChrDrop;
397
398     DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
399                   src[0], srcStride[0], src[1], srcStride[1],
400                   src[2], srcStride[2], src[3], srcStride[3],
401                   dst[0], dstStride[0], dst[1], dstStride[1],
402                   dst[2], dstStride[2], dst[3], dstStride[3]);
403     DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
404                   srcSliceY, srcSliceH, dstY, dstH);
405     DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
406                   vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
407
408     if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
409         dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
410         static int warnedAlready = 0; // FIXME maybe move this into the context
411         if (flags & SWS_PRINT_INFO && !warnedAlready) {
412             av_log(c, AV_LOG_WARNING,
413                    "Warning: dstStride is not aligned!\n"
414                    "         ->cannot do aligned memory accesses anymore\n");
415             warnedAlready = 1;
416         }
417     }
418
419     if ((int)dst[0]%16 || (int)dst[1]%16 || (int)dst[2]%16 || (int)src[0]%16 || (int)src[1]%16 || (int)src[2]%16
420         || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
421         || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
422     ) {
423         static int warnedAlready=0;
424         int cpu_flags = av_get_cpu_flags();
425         if (HAVE_MMX2 && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
426             av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
427             warnedAlready=1;
428         }
429     }
430
431     /* Note the user might start scaling the picture in the middle so this
432      * will not get executed. This is not really intended but works
433      * currently, so people might do it. */
434     if (srcSliceY == 0) {
435         lumBufIndex  = -1;
436         chrBufIndex  = -1;
437         dstY         = 0;
438         lastInLumBuf = -1;
439         lastInChrBuf = -1;
440     }
441
442     if (!should_dither) {
443         c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
444     }
445     lastDstY = dstY;
446
447     for (; dstY < dstH; dstY++) {
448         const int chrDstY = dstY >> c->chrDstVSubSample;
449         uint8_t *dest[4]  = {
450             dst[0] + dstStride[0] * dstY,
451             dst[1] + dstStride[1] * chrDstY,
452             dst[2] + dstStride[2] * chrDstY,
453             (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
454         };
455         int use_mmx_vfilter= c->use_mmx_vfilter;
456
457         // First line needed as input
458         const int firstLumSrcY  = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
459         const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
460         // First line needed as input
461         const int firstChrSrcY  = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
462
463         // Last line needed as input
464         int lastLumSrcY  = FFMIN(c->srcH,    firstLumSrcY  + vLumFilterSize) - 1;
465         int lastLumSrcY2 = FFMIN(c->srcH,    firstLumSrcY2 + vLumFilterSize) - 1;
466         int lastChrSrcY  = FFMIN(c->chrSrcH, firstChrSrcY  + vChrFilterSize) - 1;
467         int enough_lines;
468
469         // handle holes (FAST_BILINEAR & weird filters)
470         if (firstLumSrcY > lastInLumBuf)
471             lastInLumBuf = firstLumSrcY - 1;
472         if (firstChrSrcY > lastInChrBuf)
473             lastInChrBuf = firstChrSrcY - 1;
474         assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
475         assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
476
477         DEBUG_BUFFERS("dstY: %d\n", dstY);
478         DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
479                       firstLumSrcY, lastLumSrcY, lastInLumBuf);
480         DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
481                       firstChrSrcY, lastChrSrcY, lastInChrBuf);
482
483         // Do we have enough lines in this slice to output the dstY line
484         enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
485                        lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
486
487         if (!enough_lines) {
488             lastLumSrcY = srcSliceY + srcSliceH - 1;
489             lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
490             DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
491                           lastLumSrcY, lastChrSrcY);
492         }
493
494         // Do horizontal scaling
495         while (lastInLumBuf < lastLumSrcY) {
496             const uint8_t *src1[4] = {
497                 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
498                 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
499                 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
500                 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
501             };
502             lumBufIndex++;
503             assert(lumBufIndex < 2 * vLumBufSize);
504             assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
505             assert(lastInLumBuf + 1 - srcSliceY >= 0);
506             hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
507                     hLumFilter, hLumFilterPos, hLumFilterSize,
508                     formatConvBuffer, pal, 0);
509             if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
510                 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
511                         lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
512                         formatConvBuffer, pal, 1);
513             lastInLumBuf++;
514             DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
515                           lumBufIndex, lastInLumBuf);
516         }
517         while (lastInChrBuf < lastChrSrcY) {
518             const uint8_t *src1[4] = {
519                 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
520                 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
521                 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
522                 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
523             };
524             chrBufIndex++;
525             assert(chrBufIndex < 2 * vChrBufSize);
526             assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
527             assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
528             // FIXME replace parameters through context struct (some at least)
529
530             if (c->needs_hcscale)
531                 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
532                         chrDstW, src1, chrSrcW, chrXInc,
533                         hChrFilter, hChrFilterPos, hChrFilterSize,
534                         formatConvBuffer, pal);
535             lastInChrBuf++;
536             DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
537                           chrBufIndex, lastInChrBuf);
538         }
539         // wrap buf index around to stay inside the ring buffer
540         if (lumBufIndex >= vLumBufSize)
541             lumBufIndex -= vLumBufSize;
542         if (chrBufIndex >= vChrBufSize)
543             chrBufIndex -= vChrBufSize;
544         if (!enough_lines)
545             break;  // we can't output a dstY line so let's try with the next slice
546
547 #if HAVE_MMX && HAVE_INLINE_ASM
548         updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
549                               lastInLumBuf, lastInChrBuf);
550 #endif
551         if (should_dither) {
552             c->chrDither8 = dither_8x8_128[chrDstY & 7];
553             c->lumDither8 = dither_8x8_128[dstY    & 7];
554         }
555         if (dstY >= dstH - 2) {
556             /* hmm looks like we can't use MMX here without overwriting
557              * this array's tail */
558             ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
559                                      &yuv2packed1, &yuv2packed2, &yuv2packedX);
560             use_mmx_vfilter= 0;
561         }
562
563         {
564             const int16_t **lumSrcPtr  = (const int16_t **)(void*) lumPixBuf  + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
565             const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
566             const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
567             const int16_t **alpSrcPtr  = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
568                                          (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
569             int16_t *vLumFilter = c->vLumFilter;
570             int16_t *vChrFilter = c->vChrFilter;
571
572             if (isPlanarYUV(dstFormat) ||
573                 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
574                 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
575
576                 vLumFilter +=    dstY * vLumFilterSize;
577                 vChrFilter += chrDstY * vChrFilterSize;
578
579 //                 av_assert0(use_mmx_vfilter != (
580 //                                yuv2planeX == yuv2planeX_10BE_c
581 //                             || yuv2planeX == yuv2planeX_10LE_c
582 //                             || yuv2planeX == yuv2planeX_9BE_c
583 //                             || yuv2planeX == yuv2planeX_9LE_c
584 //                             || yuv2planeX == yuv2planeX_16BE_c
585 //                             || yuv2planeX == yuv2planeX_16LE_c
586 //                             || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
587
588                 if(use_mmx_vfilter){
589                     vLumFilter= c->lumMmxFilter;
590                     vChrFilter= c->chrMmxFilter;
591                 }
592
593                 if (vLumFilterSize == 1) {
594                     yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
595                 } else {
596                     yuv2planeX(vLumFilter, vLumFilterSize,
597                                lumSrcPtr, dest[0],
598                                dstW, c->lumDither8, 0);
599                 }
600
601                 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
602                     if (yuv2nv12cX) {
603                         yuv2nv12cX(c, vChrFilter,
604                                    vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
605                                    dest[1], chrDstW);
606                     } else if (vChrFilterSize == 1) {
607                         yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
608                         yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
609                     } else {
610                         yuv2planeX(vChrFilter,
611                                    vChrFilterSize, chrUSrcPtr, dest[1],
612                                    chrDstW, c->chrDither8, 0);
613                         yuv2planeX(vChrFilter,
614                                    vChrFilterSize, chrVSrcPtr, dest[2],
615                                    chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
616                     }
617                 }
618
619                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
620                     if(use_mmx_vfilter){
621                         vLumFilter= c->alpMmxFilter;
622                     }
623                     if (vLumFilterSize == 1) {
624                         yuv2plane1(alpSrcPtr[0], dest[3], dstW,
625                                    c->lumDither8, 0);
626                     } else {
627                         yuv2planeX(vLumFilter,
628                                    vLumFilterSize, alpSrcPtr, dest[3],
629                                    dstW, c->lumDither8, 0);
630                     }
631                 }
632             } else {
633                 assert(lumSrcPtr  + vLumFilterSize - 1 < lumPixBuf  + vLumBufSize * 2);
634                 assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize * 2);
635                 if (c->yuv2packed1 && vLumFilterSize == 1 &&
636                     vChrFilterSize <= 2) { // unscaled RGB
637                     int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
638                     yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
639                                 alpPixBuf ? *alpSrcPtr : NULL,
640                                 dest[0], dstW, chrAlpha, dstY);
641                 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
642                            vChrFilterSize == 2) { // bilinear upscale RGB
643                     int lumAlpha = vLumFilter[2 * dstY + 1];
644                     int chrAlpha = vChrFilter[2 * dstY + 1];
645                     lumMmxFilter[2] =
646                     lumMmxFilter[3] = vLumFilter[2 * dstY]    * 0x10001;
647                     chrMmxFilter[2] =
648                     chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
649                     yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
650                                 alpPixBuf ? alpSrcPtr : NULL,
651                                 dest[0], dstW, lumAlpha, chrAlpha, dstY);
652                 } else { // general RGB
653                     yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
654                                 lumSrcPtr, vLumFilterSize,
655                                 vChrFilter + dstY * vChrFilterSize,
656                                 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
657                                 alpSrcPtr, dest[0], dstW, dstY);
658                 }
659             }
660         }
661     }
662
663     if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf)
664         fillPlane(dst[3], dstStride[3], dstW, dstY - lastDstY, lastDstY, 255);
665
666 #if HAVE_MMX2 && HAVE_INLINE_ASM
667     if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
668         __asm__ volatile ("sfence" ::: "memory");
669 #endif
670     emms_c();
671
672     /* store changed local vars back in the context */
673     c->dstY         = dstY;
674     c->lumBufIndex  = lumBufIndex;
675     c->chrBufIndex  = chrBufIndex;
676     c->lastInLumBuf = lastInLumBuf;
677     c->lastInChrBuf = lastInChrBuf;
678
679     return dstY - lastDstY;
680 }
681
682 static av_cold void sws_init_swScale_c(SwsContext *c)
683 {
684     enum PixelFormat srcFormat = c->srcFormat;
685
686     ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
687                              &c->yuv2nv12cX, &c->yuv2packed1,
688                              &c->yuv2packed2, &c->yuv2packedX);
689
690     ff_sws_init_input_funcs(c);
691
692
693     if (c->srcBpc == 8) {
694         if (c->dstBpc <= 14) {
695             c->hyScale = c->hcScale = hScale8To15_c;
696             if (c->flags & SWS_FAST_BILINEAR) {
697                 c->hyscale_fast = hyscale_fast_c;
698                 c->hcscale_fast = hcscale_fast_c;
699             }
700         } else {
701             c->hyScale = c->hcScale = hScale8To19_c;
702         }
703     } else {
704         c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
705                                                  : hScale16To15_c;
706     }
707
708     if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
709         if (c->dstBpc <= 14) {
710             if (c->srcRange) {
711                 c->lumConvertRange = lumRangeFromJpeg_c;
712                 c->chrConvertRange = chrRangeFromJpeg_c;
713             } else {
714                 c->lumConvertRange = lumRangeToJpeg_c;
715                 c->chrConvertRange = chrRangeToJpeg_c;
716             }
717         } else {
718             if (c->srcRange) {
719                 c->lumConvertRange = lumRangeFromJpeg16_c;
720                 c->chrConvertRange = chrRangeFromJpeg16_c;
721             } else {
722                 c->lumConvertRange = lumRangeToJpeg16_c;
723                 c->chrConvertRange = chrRangeToJpeg16_c;
724             }
725         }
726     }
727
728     if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
729           srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
730         c->needs_hcscale = 1;
731 }
732
733 SwsFunc ff_getSwsFunc(SwsContext *c)
734 {
735     sws_init_swScale_c(c);
736
737     if (HAVE_MMX)
738         ff_sws_init_swScale_mmx(c);
739     if (HAVE_ALTIVEC)
740         ff_sws_init_swScale_altivec(c);
741
742     return swScale;
743 }