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