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