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