FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
swscale_unscaled.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 <inttypes.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include "swscale.h"
27 #include "swscale_internal.h"
28 #include "rgb2rgb.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/bswap.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/avassert.h"
36 
37 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
38 {
39  { 0, 1, 0, 1, 0, 1, 0, 1,},
40  { 1, 0, 1, 0, 1, 0, 1, 0,},
41  { 0, 1, 0, 1, 0, 1, 0, 1,},
42  { 1, 0, 1, 0, 1, 0, 1, 0,},
43  { 0, 1, 0, 1, 0, 1, 0, 1,},
44  { 1, 0, 1, 0, 1, 0, 1, 0,},
45  { 0, 1, 0, 1, 0, 1, 0, 1,},
46  { 1, 0, 1, 0, 1, 0, 1, 0,},
47 },{
48  { 1, 2, 1, 2, 1, 2, 1, 2,},
49  { 3, 0, 3, 0, 3, 0, 3, 0,},
50  { 1, 2, 1, 2, 1, 2, 1, 2,},
51  { 3, 0, 3, 0, 3, 0, 3, 0,},
52  { 1, 2, 1, 2, 1, 2, 1, 2,},
53  { 3, 0, 3, 0, 3, 0, 3, 0,},
54  { 1, 2, 1, 2, 1, 2, 1, 2,},
55  { 3, 0, 3, 0, 3, 0, 3, 0,},
56 },{
57  { 2, 4, 3, 5, 2, 4, 3, 5,},
58  { 6, 0, 7, 1, 6, 0, 7, 1,},
59  { 3, 5, 2, 4, 3, 5, 2, 4,},
60  { 7, 1, 6, 0, 7, 1, 6, 0,},
61  { 2, 4, 3, 5, 2, 4, 3, 5,},
62  { 6, 0, 7, 1, 6, 0, 7, 1,},
63  { 3, 5, 2, 4, 3, 5, 2, 4,},
64  { 7, 1, 6, 0, 7, 1, 6, 0,},
65 },{
66  { 4, 8, 7, 11, 4, 8, 7, 11,},
67  { 12, 0, 15, 3, 12, 0, 15, 3,},
68  { 6, 10, 5, 9, 6, 10, 5, 9,},
69  { 14, 2, 13, 1, 14, 2, 13, 1,},
70  { 4, 8, 7, 11, 4, 8, 7, 11,},
71  { 12, 0, 15, 3, 12, 0, 15, 3,},
72  { 6, 10, 5, 9, 6, 10, 5, 9,},
73  { 14, 2, 13, 1, 14, 2, 13, 1,},
74 },{
75  { 9, 17, 15, 23, 8, 16, 14, 22,},
76  { 25, 1, 31, 7, 24, 0, 30, 6,},
77  { 13, 21, 11, 19, 12, 20, 10, 18,},
78  { 29, 5, 27, 3, 28, 4, 26, 2,},
79  { 8, 16, 14, 22, 9, 17, 15, 23,},
80  { 24, 0, 30, 6, 25, 1, 31, 7,},
81  { 12, 20, 10, 18, 13, 21, 11, 19,},
82  { 28, 4, 26, 2, 29, 5, 27, 3,},
83 },{
84  { 18, 34, 30, 46, 17, 33, 29, 45,},
85  { 50, 2, 62, 14, 49, 1, 61, 13,},
86  { 26, 42, 22, 38, 25, 41, 21, 37,},
87  { 58, 10, 54, 6, 57, 9, 53, 5,},
88  { 16, 32, 28, 44, 19, 35, 31, 47,},
89  { 48, 0, 60, 12, 51, 3, 63, 15,},
90  { 24, 40, 20, 36, 27, 43, 23, 39,},
91  { 56, 8, 52, 4, 59, 11, 55, 7,},
92 },{
93  { 18, 34, 30, 46, 17, 33, 29, 45,},
94  { 50, 2, 62, 14, 49, 1, 61, 13,},
95  { 26, 42, 22, 38, 25, 41, 21, 37,},
96  { 58, 10, 54, 6, 57, 9, 53, 5,},
97  { 16, 32, 28, 44, 19, 35, 31, 47,},
98  { 48, 0, 60, 12, 51, 3, 63, 15,},
99  { 24, 40, 20, 36, 27, 43, 23, 39,},
100  { 56, 8, 52, 4, 59, 11, 55, 7,},
101 },{
102  { 36, 68, 60, 92, 34, 66, 58, 90,},
103  { 100, 4,124, 28, 98, 2,122, 26,},
104  { 52, 84, 44, 76, 50, 82, 42, 74,},
105  { 116, 20,108, 12,114, 18,106, 10,},
106  { 32, 64, 56, 88, 38, 70, 62, 94,},
107  { 96, 0,120, 24,102, 6,126, 30,},
108  { 48, 80, 40, 72, 54, 86, 46, 78,},
109  { 112, 16,104, 8,118, 22,110, 14,},
110 }};
111 
112 const uint16_t dither_scale[15][16]={
113 { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
114 { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
115 { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
116 { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
117 { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
118 { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
119 { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
120 { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
121 { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
122 { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
123 { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
124 { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
125 { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
126 { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
127 { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
128 };
129 
130 
131 static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
132  uint8_t val)
133 {
134  int i;
135  uint8_t *ptr = plane + stride * y;
136  for (i = 0; i < height; i++) {
137  memset(ptr, val, width);
138  ptr += stride;
139  }
140 }
141 
142 static void copyPlane(const uint8_t *src, int srcStride,
143  int srcSliceY, int srcSliceH, int width,
144  uint8_t *dst, int dstStride)
145 {
146  dst += dstStride * srcSliceY;
147  if (dstStride == srcStride && srcStride > 0) {
148  memcpy(dst, src, srcSliceH * dstStride);
149  } else {
150  int i;
151  for (i = 0; i < srcSliceH; i++) {
152  memcpy(dst, src, width);
153  src += srcStride;
154  dst += dstStride;
155  }
156  }
157 }
158 
160  int srcStride[], int srcSliceY,
161  int srcSliceH, uint8_t *dstParam[],
162  int dstStride[])
163 {
164  uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
165 
166  copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
167  dstParam[0], dstStride[0]);
168 
169  if (c->dstFormat == AV_PIX_FMT_NV12)
170  interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
171  srcStride[1], srcStride[2], dstStride[0]);
172  else
173  interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
174  srcStride[2], srcStride[1], dstStride[0]);
175 
176  return srcSliceH;
177 }
178 
180  int srcStride[], int srcSliceY, int srcSliceH,
181  uint8_t *dstParam[], int dstStride[])
182 {
183  uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
184 
185  yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
186  srcStride[1], dstStride[0]);
187 
188  return srcSliceH;
189 }
190 
192  int srcStride[], int srcSliceY, int srcSliceH,
193  uint8_t *dstParam[], int dstStride[])
194 {
195  uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
196 
197  yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
198  srcStride[1], dstStride[0]);
199 
200  return srcSliceH;
201 }
202 
204  int srcStride[], int srcSliceY, int srcSliceH,
205  uint8_t *dstParam[], int dstStride[])
206 {
207  uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
208 
209  yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
210  srcStride[1], dstStride[0]);
211 
212  return srcSliceH;
213 }
214 
216  int srcStride[], int srcSliceY, int srcSliceH,
217  uint8_t *dstParam[], int dstStride[])
218 {
219  uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
220 
221  yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
222  srcStride[1], dstStride[0]);
223 
224  return srcSliceH;
225 }
226 
228  int srcStride[], int srcSliceY, int srcSliceH,
229  uint8_t *dstParam[], int dstStride[])
230 {
231  uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
232  uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
233  uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
234 
235  yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
236  dstStride[1], srcStride[0]);
237 
238  if (dstParam[3])
239  fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
240 
241  return srcSliceH;
242 }
243 
245  int srcStride[], int srcSliceY, int srcSliceH,
246  uint8_t *dstParam[], int dstStride[])
247 {
248  uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
249  uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
250  uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
251 
252  yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
253  dstStride[1], srcStride[0]);
254 
255  return srcSliceH;
256 }
257 
259  int srcStride[], int srcSliceY, int srcSliceH,
260  uint8_t *dstParam[], int dstStride[])
261 {
262  uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
263  uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
264  uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
265 
266  uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
267  dstStride[1], srcStride[0]);
268 
269  if (dstParam[3])
270  fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
271 
272  return srcSliceH;
273 }
274 
276  int srcStride[], int srcSliceY, int srcSliceH,
277  uint8_t *dstParam[], int dstStride[])
278 {
279  uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
280  uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
281  uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
282 
283  uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
284  dstStride[1], srcStride[0]);
285 
286  return srcSliceH;
287 }
288 
289 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
290  const uint8_t *palette)
291 {
292  int i;
293  for (i = 0; i < num_pixels; i++)
294  ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
295 }
296 
297 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
298  const uint8_t *palette)
299 {
300  int i;
301 
302  for (i = 0; i < num_pixels; i++)
303  ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
304 }
305 
306 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
307  const uint8_t *palette)
308 {
309  int i;
310 
311  for (i = 0; i < num_pixels; i++) {
312  //FIXME slow?
313  dst[0] = palette[src[i << 1] * 4 + 0];
314  dst[1] = palette[src[i << 1] * 4 + 1];
315  dst[2] = palette[src[i << 1] * 4 + 2];
316  dst += 3;
317  }
318 }
319 
320 static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
321  int srcStride[], int srcSliceY, int srcSliceH,
322  uint8_t *dst[], int dstStride[])
323 {
324  int i, j, p;
325 
326  for (p = 0; p < 4; p++) {
327  int srcstr = srcStride[p] / 2;
328  int dststr = dstStride[p] / 2;
329  uint16_t *dstPtr = (uint16_t *) dst[p];
330  const uint16_t *srcPtr = (const uint16_t *) src[p];
331  int min_stride = FFMIN(FFABS(srcstr), FFABS(dststr));
332  if(!dstPtr || !srcPtr)
333  continue;
334  for (i = 0; i < (srcSliceH >> c->chrDstVSubSample); i++) {
335  for (j = 0; j < min_stride; j++) {
336  dstPtr[j] = av_bswap16(srcPtr[j]);
337  }
338  srcPtr += srcstr;
339  dstPtr += dststr;
340  }
341  }
342 
343  return srcSliceH;
344 }
345 
346 static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
347  int srcSliceY, int srcSliceH, uint8_t *dst[],
348  int dstStride[])
349 {
350  const enum AVPixelFormat srcFormat = c->srcFormat;
351  const enum AVPixelFormat dstFormat = c->dstFormat;
352  void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
353  const uint8_t *palette) = NULL;
354  int i;
355  uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
356  const uint8_t *srcPtr = src[0];
357 
358  if (srcFormat == AV_PIX_FMT_GRAY8A) {
359  switch (dstFormat) {
360  case AV_PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
361  case AV_PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
364  case AV_PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
365  case AV_PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
366  }
367  } else if (usePal(srcFormat)) {
368  switch (dstFormat) {
375  }
376  }
377 
378  if (!conv)
379  av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
380  av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
381  else {
382  for (i = 0; i < srcSliceH; i++) {
383  conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
384  srcPtr += srcStride[0];
385  dstPtr += dstStride[0];
386  }
387  }
388 
389  return srcSliceH;
390 }
391 
392 static void gbr16ptopacked16(const uint16_t *src[], int srcStride[],
393  uint8_t *dst, int dstStride, int srcSliceH,
394  int alpha, int swap, int bpp, int width)
395 {
396  int x, h, i;
397  int scale_high = 16 - bpp, scale_low = (bpp - 8) * 2;
398  for (h = 0; h < srcSliceH; h++) {
399  uint16_t *dest = (uint16_t *)(dst + dstStride * h);
400  uint16_t component;
401 
402  switch(swap) {
403  case 3:
404  if (alpha) {
405  for (x = 0; x < width; x++) {
406  component = av_bswap16(src[0][x]);
407  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
408  component = av_bswap16(src[1][x]);
409  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
410  component = av_bswap16(src[2][x]);
411  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
412  *dest++ = 0xffff;
413  }
414  } else {
415  for (x = 0; x < width; x++) {
416  component = av_bswap16(src[0][x]);
417  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
418  component = av_bswap16(src[1][x]);
419  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
420  component = av_bswap16(src[2][x]);
421  *dest++ = av_bswap16(component << scale_high | component >> scale_low);
422  }
423  }
424  break;
425  case 2:
426  if (alpha) {
427  for (x = 0; x < width; x++) {
428  *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
429  *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
430  *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
431  *dest++ = 0xffff;
432  }
433  } else {
434  for (x = 0; x < width; x++) {
435  *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
436  *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
437  *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
438  }
439  }
440  break;
441  case 1:
442  if (alpha) {
443  for (x = 0; x < width; x++) {
444  *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
445  *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
446  *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
447  *dest++ = 0xffff;
448  }
449  } else {
450  for (x = 0; x < width; x++) {
451  *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
452  *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
453  *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
454  }
455  }
456  break;
457  default:
458  if (alpha) {
459  for (x = 0; x < width; x++) {
460  *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
461  *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
462  *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
463  *dest++ = 0xffff;
464  }
465  } else {
466  for (x = 0; x < width; x++) {
467  *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
468  *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
469  *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
470  }
471  }
472  }
473  for (i = 0; i < 3; i++)
474  src[i] += srcStride[i] >> 1;
475  }
476 }
477 
479  int srcStride[], int srcSliceY, int srcSliceH,
480  uint8_t *dst[], int dstStride[])
481 {
482  const uint16_t *src102[] = { (uint16_t *)src[1], (uint16_t *)src[0], (uint16_t *)src[2] };
483  const uint16_t *src201[] = { (uint16_t *)src[2], (uint16_t *)src[0], (uint16_t *)src[1] };
484  int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
485  int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
486  const AVPixFmtDescriptor *src_format = av_pix_fmt_desc_get(c->srcFormat);
487  const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
488  int bits_per_sample = src_format->comp[0].depth_minus1 + 1;
489  int swap = 0;
490  if ( HAVE_BIGENDIAN && !(src_format->flags & AV_PIX_FMT_FLAG_BE) ||
491  !HAVE_BIGENDIAN && src_format->flags & AV_PIX_FMT_FLAG_BE)
492  swap++;
493  if ( HAVE_BIGENDIAN && !(dst_format->flags & AV_PIX_FMT_FLAG_BE) ||
494  !HAVE_BIGENDIAN && dst_format->flags & AV_PIX_FMT_FLAG_BE)
495  swap += 2;
496 
497  if ((src_format->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) !=
499  bits_per_sample <= 8) {
500  av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
501  src_format->name, dst_format->name);
502  return srcSliceH;
503  }
504  switch (c->dstFormat) {
505  case AV_PIX_FMT_BGR48LE:
506  case AV_PIX_FMT_BGR48BE:
507  gbr16ptopacked16(src102, stride102,
508  dst[0] + srcSliceY * dstStride[0], dstStride[0],
509  srcSliceH, 0, swap, bits_per_sample, c->srcW);
510  break;
511  case AV_PIX_FMT_RGB48LE:
512  case AV_PIX_FMT_RGB48BE:
513  gbr16ptopacked16(src201, stride201,
514  dst[0] + srcSliceY * dstStride[0], dstStride[0],
515  srcSliceH, 0, swap, bits_per_sample, c->srcW);
516  break;
517  case AV_PIX_FMT_RGBA64LE:
518  case AV_PIX_FMT_RGBA64BE:
519  gbr16ptopacked16(src201, stride201,
520  dst[0] + srcSliceY * dstStride[0], dstStride[0],
521  srcSliceH, 1, swap, bits_per_sample, c->srcW);
522  break;
523  case AV_PIX_FMT_BGRA64LE:
524  case AV_PIX_FMT_BGRA64BE:
525  gbr16ptopacked16(src102, stride102,
526  dst[0] + srcSliceY * dstStride[0], dstStride[0],
527  srcSliceH, 1, swap, bits_per_sample, c->srcW);
528  break;
529  default:
530  av_log(c, AV_LOG_ERROR,
531  "unsupported planar RGB conversion %s -> %s\n",
532  src_format->name, dst_format->name);
533  }
534 
535  return srcSliceH;
536 }
537 
538 static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
539  uint8_t *dst, int dstStride, int srcSliceH,
540  int width)
541 {
542  int x, h, i;
543  for (h = 0; h < srcSliceH; h++) {
544  uint8_t *dest = dst + dstStride * h;
545  for (x = 0; x < width; x++) {
546  *dest++ = src[0][x];
547  *dest++ = src[1][x];
548  *dest++ = src[2][x];
549  }
550 
551  for (i = 0; i < 3; i++)
552  src[i] += srcStride[i];
553  }
554 }
555 
556 static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
557  uint8_t *dst, int dstStride, int srcSliceH,
558  int alpha_first, int width)
559 {
560  int x, h, i;
561  for (h = 0; h < srcSliceH; h++) {
562  uint8_t *dest = dst + dstStride * h;
563 
564  if (alpha_first) {
565  for (x = 0; x < width; x++) {
566  *dest++ = 0xff;
567  *dest++ = src[0][x];
568  *dest++ = src[1][x];
569  *dest++ = src[2][x];
570  }
571  } else {
572  for (x = 0; x < width; x++) {
573  *dest++ = src[0][x];
574  *dest++ = src[1][x];
575  *dest++ = src[2][x];
576  *dest++ = 0xff;
577  }
578  }
579 
580  for (i = 0; i < 3; i++)
581  src[i] += srcStride[i];
582  }
583 }
584 
585 static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
586  int srcStride[], int srcSliceY, int srcSliceH,
587  uint8_t *dst[], int dstStride[])
588 {
589  int alpha_first = 0;
590  const uint8_t *src102[] = { src[1], src[0], src[2] };
591  const uint8_t *src201[] = { src[2], src[0], src[1] };
592  int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
593  int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
594 
595  if (c->srcFormat != AV_PIX_FMT_GBRP) {
596  av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
599  return srcSliceH;
600  }
601 
602  switch (c->dstFormat) {
603  case AV_PIX_FMT_BGR24:
604  gbr24ptopacked24(src102, stride102,
605  dst[0] + srcSliceY * dstStride[0], dstStride[0],
606  srcSliceH, c->srcW);
607  break;
608 
609  case AV_PIX_FMT_RGB24:
610  gbr24ptopacked24(src201, stride201,
611  dst[0] + srcSliceY * dstStride[0], dstStride[0],
612  srcSliceH, c->srcW);
613  break;
614 
615  case AV_PIX_FMT_ARGB:
616  alpha_first = 1;
617  case AV_PIX_FMT_RGBA:
618  gbr24ptopacked32(src201, stride201,
619  dst[0] + srcSliceY * dstStride[0], dstStride[0],
620  srcSliceH, alpha_first, c->srcW);
621  break;
622 
623  case AV_PIX_FMT_ABGR:
624  alpha_first = 1;
625  case AV_PIX_FMT_BGRA:
626  gbr24ptopacked32(src102, stride102,
627  dst[0] + srcSliceY * dstStride[0], dstStride[0],
628  srcSliceH, alpha_first, c->srcW);
629  break;
630 
631  default:
632  av_log(c, AV_LOG_ERROR,
633  "unsupported planar RGB conversion %s -> %s\n",
636  }
637 
638  return srcSliceH;
639 }
640 
641 static int planarRgbToplanarRgbWrapper(SwsContext *c, const uint8_t *src[],
642  int srcStride[], int srcSliceY, int srcSliceH,
643  uint8_t *dst[], int dstStride[])
644 {
645  copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
646  dst[0], dstStride[0]);
647  copyPlane(src[1], srcStride[1], srcSliceY, srcSliceH, c->srcW,
648  dst[1], dstStride[1]);
649  copyPlane(src[2], srcStride[2], srcSliceY, srcSliceH, c->srcW,
650  dst[2], dstStride[2]);
651  if (dst[3])
652  fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
653 
654  return srcSliceH;
655 }
656 
657 static void packedtogbr24p(const uint8_t *src, int srcStride,
658  uint8_t *dst[], int dstStride[], int srcSliceH,
659  int alpha_first, int inc_size, int width)
660 {
661  uint8_t *dest[3];
662  int x, h;
663 
664  dest[0] = dst[0];
665  dest[1] = dst[1];
666  dest[2] = dst[2];
667 
668  if (alpha_first)
669  src++;
670 
671  for (h = 0; h < srcSliceH; h++) {
672  for (x = 0; x < width; x++) {
673  dest[0][x] = src[0];
674  dest[1][x] = src[1];
675  dest[2][x] = src[2];
676 
677  src += inc_size;
678  }
679  src += srcStride - width * inc_size;
680  dest[0] += dstStride[0];
681  dest[1] += dstStride[1];
682  dest[2] += dstStride[2];
683  }
684 }
685 
686 static int rgbToPlanarRgbWrapper(SwsContext *c, const uint8_t *src[],
687  int srcStride[], int srcSliceY, int srcSliceH,
688  uint8_t *dst[], int dstStride[])
689 {
690  int alpha_first = 0;
691  int stride102[] = { dstStride[1], dstStride[0], dstStride[2] };
692  int stride201[] = { dstStride[2], dstStride[0], dstStride[1] };
693  uint8_t *dst102[] = { dst[1] + srcSliceY * dstStride[1],
694  dst[0] + srcSliceY * dstStride[0],
695  dst[2] + srcSliceY * dstStride[2] };
696  uint8_t *dst201[] = { dst[2] + srcSliceY * dstStride[2],
697  dst[0] + srcSliceY * dstStride[0],
698  dst[1] + srcSliceY * dstStride[1] };
699 
700  switch (c->srcFormat) {
701  case AV_PIX_FMT_RGB24:
702  packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
703  stride201, srcSliceH, alpha_first, 3, c->srcW);
704  break;
705  case AV_PIX_FMT_BGR24:
706  packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
707  stride102, srcSliceH, alpha_first, 3, c->srcW);
708  break;
709  case AV_PIX_FMT_ARGB:
710  alpha_first = 1;
711  case AV_PIX_FMT_RGBA:
712  packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
713  stride201, srcSliceH, alpha_first, 4, c->srcW);
714  break;
715  case AV_PIX_FMT_ABGR:
716  alpha_first = 1;
717  case AV_PIX_FMT_BGRA:
718  packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
719  stride102, srcSliceH, alpha_first, 4, c->srcW);
720  break;
721  default:
722  av_log(c, AV_LOG_ERROR,
723  "unsupported planar RGB conversion %s -> %s\n",
726  }
727 
728  return srcSliceH;
729 }
730 
731 #define isRGBA32(x) ( \
732  (x) == AV_PIX_FMT_ARGB \
733  || (x) == AV_PIX_FMT_RGBA \
734  || (x) == AV_PIX_FMT_BGRA \
735  || (x) == AV_PIX_FMT_ABGR \
736  )
737 
738 #define isRGBA64(x) ( \
739  (x) == AV_PIX_FMT_RGBA64LE \
740  || (x) == AV_PIX_FMT_RGBA64BE \
741  || (x) == AV_PIX_FMT_BGRA64LE \
742  || (x) == AV_PIX_FMT_BGRA64BE \
743  )
744 
745 #define isRGB48(x) ( \
746  (x) == AV_PIX_FMT_RGB48LE \
747  || (x) == AV_PIX_FMT_RGB48BE \
748  || (x) == AV_PIX_FMT_BGR48LE \
749  || (x) == AV_PIX_FMT_BGR48BE \
750  )
751 
752 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
753 typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
755 {
756  const enum AVPixelFormat srcFormat = c->srcFormat;
757  const enum AVPixelFormat dstFormat = c->dstFormat;
758  const int srcId = c->srcFormatBpp;
759  const int dstId = c->dstFormatBpp;
760  rgbConvFn conv = NULL;
761 
762 #define IS_NOT_NE(bpp, desc) \
763  (((bpp + 7) >> 3) == 2 && \
764  (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
765 
766 #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
767 
768  if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
769  if ( CONV_IS(ABGR, RGBA)
770  || CONV_IS(ARGB, BGRA)
771  || CONV_IS(BGRA, ARGB)
772  || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
773  else if (CONV_IS(ABGR, ARGB)
774  || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
775  else if (CONV_IS(ABGR, BGRA)
776  || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
777  else if (CONV_IS(BGRA, RGBA)
778  || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
779  else if (CONV_IS(BGRA, ABGR)
780  || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
781  } else if (isRGB48(srcFormat) && isRGB48(dstFormat)) {
782  if (CONV_IS(RGB48LE, BGR48LE)
783  || CONV_IS(BGR48LE, RGB48LE)
784  || CONV_IS(RGB48BE, BGR48BE)
785  || CONV_IS(BGR48BE, RGB48BE)) conv = rgb48tobgr48_nobswap;
786  else if (CONV_IS(RGB48LE, BGR48BE)
787  || CONV_IS(BGR48LE, RGB48BE)
788  || CONV_IS(RGB48BE, BGR48LE)
789  || CONV_IS(BGR48BE, RGB48LE)) conv = rgb48tobgr48_bswap;
790  } else if (isRGBA64(srcFormat) && isRGB48(dstFormat)) {
791  if (CONV_IS(RGBA64LE, BGR48LE)
792  || CONV_IS(BGRA64LE, RGB48LE)
793  || CONV_IS(RGBA64BE, BGR48BE)
794  || CONV_IS(BGRA64BE, RGB48BE)) conv = rgb64tobgr48_nobswap;
795  else if (CONV_IS(RGBA64LE, BGR48BE)
796  || CONV_IS(BGRA64LE, RGB48BE)
797  || CONV_IS(RGBA64BE, BGR48LE)
798  || CONV_IS(BGRA64BE, RGB48LE)) conv = rgb64tobgr48_bswap;
799  else if (CONV_IS(RGBA64LE, RGB48LE)
800  || CONV_IS(BGRA64LE, BGR48LE)
801  || CONV_IS(RGBA64BE, RGB48BE)
802  || CONV_IS(BGRA64BE, BGR48BE)) conv = rgb64to48_nobswap;
803  else if (CONV_IS(RGBA64LE, RGB48BE)
804  || CONV_IS(BGRA64LE, BGR48BE)
805  || CONV_IS(RGBA64BE, RGB48LE)
806  || CONV_IS(BGRA64BE, BGR48LE)) conv = rgb64to48_bswap;
807  } else
808  /* BGR -> BGR */
809  if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
810  (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
811  switch (srcId | (dstId << 16)) {
812  case 0x000F000C: conv = rgb12to15; break;
813  case 0x000F0010: conv = rgb16to15; break;
814  case 0x000F0018: conv = rgb24to15; break;
815  case 0x000F0020: conv = rgb32to15; break;
816  case 0x0010000F: conv = rgb15to16; break;
817  case 0x00100018: conv = rgb24to16; break;
818  case 0x00100020: conv = rgb32to16; break;
819  case 0x0018000F: conv = rgb15to24; break;
820  case 0x00180010: conv = rgb16to24; break;
821  case 0x00180020: conv = rgb32to24; break;
822  case 0x0020000F: conv = rgb15to32; break;
823  case 0x00200010: conv = rgb16to32; break;
824  case 0x00200018: conv = rgb24to32; break;
825  }
826  } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
827  (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
828  switch (srcId | (dstId << 16)) {
829  case 0x000C000C: conv = rgb12tobgr12; break;
830  case 0x000F000F: conv = rgb15tobgr15; break;
831  case 0x000F0010: conv = rgb16tobgr15; break;
832  case 0x000F0018: conv = rgb24tobgr15; break;
833  case 0x000F0020: conv = rgb32tobgr15; break;
834  case 0x0010000F: conv = rgb15tobgr16; break;
835  case 0x00100010: conv = rgb16tobgr16; break;
836  case 0x00100018: conv = rgb24tobgr16; break;
837  case 0x00100020: conv = rgb32tobgr16; break;
838  case 0x0018000F: conv = rgb15tobgr24; break;
839  case 0x00180010: conv = rgb16tobgr24; break;
840  case 0x00180018: conv = rgb24tobgr24; break;
841  case 0x00180020: conv = rgb32tobgr24; break;
842  case 0x0020000F: conv = rgb15tobgr32; break;
843  case 0x00200010: conv = rgb16tobgr32; break;
844  case 0x00200018: conv = rgb24tobgr32; break;
845  }
846  }
847 
848  if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) && !isRGBA32(srcFormat) && ALT32_CORR<0)
849  return NULL;
850 
851  return conv;
852 }
853 
854 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
855 static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
856  int srcSliceY, int srcSliceH, uint8_t *dst[],
857  int dstStride[])
858 
859 {
860  const enum AVPixelFormat srcFormat = c->srcFormat;
861  const enum AVPixelFormat dstFormat = c->dstFormat;
862  const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
863  const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
864  const int srcBpp = (c->srcFormatBpp + 7) >> 3;
865  const int dstBpp = (c->dstFormatBpp + 7) >> 3;
867 
868  if (!conv) {
869  av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
870  av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
871  } else {
872  const uint8_t *srcPtr = src[0];
873  uint8_t *dstPtr = dst[0];
874  int src_bswap = IS_NOT_NE(c->srcFormatBpp, desc_src);
875  int dst_bswap = IS_NOT_NE(c->dstFormatBpp, desc_dst);
876 
877  if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
878  !isRGBA32(dstFormat))
879  srcPtr += ALT32_CORR;
880 
881  if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
882  !isRGBA32(srcFormat))
883  dstPtr += ALT32_CORR;
884 
885  if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
886  !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap)
887  conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
888  srcSliceH * srcStride[0]);
889  else {
890  int i, j;
891  dstPtr += dstStride[0] * srcSliceY;
892 
893  for (i = 0; i < srcSliceH; i++) {
894  if(src_bswap) {
895  for(j=0; j<c->srcW; j++)
896  ((uint16_t*)c->formatConvBuffer)[j] = av_bswap16(((uint16_t*)srcPtr)[j]);
897  conv(c->formatConvBuffer, dstPtr, c->srcW * srcBpp);
898  }else
899  conv(srcPtr, dstPtr, c->srcW * srcBpp);
900  if(dst_bswap)
901  for(j=0; j<c->srcW; j++)
902  ((uint16_t*)dstPtr)[j] = av_bswap16(((uint16_t*)dstPtr)[j]);
903  srcPtr += srcStride[0];
904  dstPtr += dstStride[0];
905  }
906  }
907  }
908  return srcSliceH;
909 }
910 
911 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
912  int srcStride[], int srcSliceY, int srcSliceH,
913  uint8_t *dst[], int dstStride[])
914 {
916  src[0],
917  dst[0] + srcSliceY * dstStride[0],
918  dst[1] + (srcSliceY >> 1) * dstStride[1],
919  dst[2] + (srcSliceY >> 1) * dstStride[2],
920  c->srcW, srcSliceH,
921  dstStride[0], dstStride[1], srcStride[0],
923  if (dst[3])
924  fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
925  return srcSliceH;
926 }
927 
928 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
929  int srcStride[], int srcSliceY, int srcSliceH,
930  uint8_t *dst[], int dstStride[])
931 {
932  copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
933  dst[0], dstStride[0]);
934 
935  planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
936  srcSliceH >> 2, srcStride[1], dstStride[1]);
937  planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
938  srcSliceH >> 2, srcStride[2], dstStride[2]);
939  if (dst[3])
940  fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
941  return srcSliceH;
942 }
943 
944 /* unscaled copy like stuff (assumes nearly identical formats) */
945 static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
946  int srcStride[], int srcSliceY, int srcSliceH,
947  uint8_t *dst[], int dstStride[])
948 {
949  if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
950  memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
951  else {
952  int i;
953  const uint8_t *srcPtr = src[0];
954  uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
955  int length = 0;
956 
957  /* universal length finder */
958  while (length + c->srcW <= FFABS(dstStride[0]) &&
959  length + c->srcW <= FFABS(srcStride[0]))
960  length += c->srcW;
961  av_assert1(length != 0);
962 
963  for (i = 0; i < srcSliceH; i++) {
964  memcpy(dstPtr, srcPtr, length);
965  srcPtr += srcStride[0];
966  dstPtr += dstStride[0];
967  }
968  }
969  return srcSliceH;
970 }
971 
972 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
973  uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
974  int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
975  for (i = 0; i < height; i++) {\
976  const uint8_t *dither= dithers[src_depth-9][i&7];\
977  for (j = 0; j < length-7; j+=8){\
978  dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
979  dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
980  dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
981  dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
982  dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
983  dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
984  dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
985  dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
986  }\
987  for (; j < length; j++)\
988  dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
989  dst += dstStride;\
990  src += srcStride;\
991  }
992 
993 static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
994  int srcStride[], int srcSliceY, int srcSliceH,
995  uint8_t *dst[], int dstStride[])
996 {
997  const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
998  const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
999  int plane, i, j;
1000  for (plane = 0; plane < 4; plane++) {
1001  int length = (plane == 0 || plane == 3) ? c->srcW : FF_CEIL_RSHIFT(c->srcW, c->chrDstHSubSample);
1002  int y = (plane == 0 || plane == 3) ? srcSliceY: FF_CEIL_RSHIFT(srcSliceY, c->chrDstVSubSample);
1003  int height = (plane == 0 || plane == 3) ? srcSliceH: FF_CEIL_RSHIFT(srcSliceH, c->chrDstVSubSample);
1004  const uint8_t *srcPtr = src[plane];
1005  uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
1006  int shiftonly= plane==1 || plane==2 || (!c->srcRange && plane==0);
1007 
1008  if (!dst[plane])
1009  continue;
1010  // ignore palette for GRAY8
1011  if (plane == 1 && !dst[2]) continue;
1012  if (!src[plane] || (plane == 1 && !src[2])) {
1013  if (is16BPS(c->dstFormat) || isNBPS(c->dstFormat)) {
1014  fillPlane16(dst[plane], dstStride[plane], length, height, y,
1015  plane == 3, desc_dst->comp[plane].depth_minus1,
1016  isBE(c->dstFormat));
1017  } else {
1018  fillPlane(dst[plane], dstStride[plane], length, height, y,
1019  (plane == 3) ? 255 : 128);
1020  }
1021  } else {
1022  if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
1023  || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
1024  ) {
1025  const int src_depth = desc_src->comp[plane].depth_minus1 + 1;
1026  const int dst_depth = desc_dst->comp[plane].depth_minus1 + 1;
1027  const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
1028  uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1029 
1030  if (dst_depth == 8) {
1031  if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1032  DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
1033  } else {
1034  DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
1035  }
1036  } else if (src_depth == 8) {
1037  for (i = 0; i < height; i++) {
1038  #define COPY816(w)\
1039  if(shiftonly){\
1040  for (j = 0; j < length; j++)\
1041  w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
1042  }else{\
1043  for (j = 0; j < length; j++)\
1044  w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
1045  (srcPtr[j]>>(2*8-dst_depth)));\
1046  }
1047  if(isBE(c->dstFormat)){
1048  COPY816(AV_WB16)
1049  } else {
1050  COPY816(AV_WL16)
1051  }
1052  dstPtr2 += dstStride[plane]/2;
1053  srcPtr += srcStride[plane];
1054  }
1055  } else if (src_depth <= dst_depth) {
1056  int orig_length = length;
1057  for (i = 0; i < height; i++) {
1058  if(isBE(c->srcFormat) == HAVE_BIGENDIAN &&
1059  isBE(c->dstFormat) == HAVE_BIGENDIAN &&
1060  shiftonly) {
1061  unsigned shift = dst_depth - src_depth;
1062  length = orig_length;
1063 #if HAVE_FAST_64BIT
1064 #define FAST_COPY_UP(shift) \
1065  for (j = 0; j < length - 3; j += 4) { \
1066  uint64_t v = AV_RN64A(srcPtr2 + j); \
1067  AV_WN64A(dstPtr2 + j, v << shift); \
1068  } \
1069  length &= 3;
1070 #else
1071 #define FAST_COPY_UP(shift) \
1072  for (j = 0; j < length - 1; j += 2) { \
1073  uint32_t v = AV_RN32A(srcPtr2 + j); \
1074  AV_WN32A(dstPtr2 + j, v << shift); \
1075  } \
1076  length &= 1;
1077 #endif
1078  switch (shift)
1079  {
1080  case 6: FAST_COPY_UP(6); break;
1081  case 7: FAST_COPY_UP(7); break;
1082  }
1083  }
1084 #define COPY_UP(r,w) \
1085  if(shiftonly){\
1086  for (j = 0; j < length; j++){ \
1087  unsigned int v= r(&srcPtr2[j]);\
1088  w(&dstPtr2[j], v<<(dst_depth-src_depth));\
1089  }\
1090  }else{\
1091  for (j = 0; j < length; j++){ \
1092  unsigned int v= r(&srcPtr2[j]);\
1093  w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
1094  (v>>(2*src_depth-dst_depth)));\
1095  }\
1096  }
1097  if(isBE(c->srcFormat)){
1098  if(isBE(c->dstFormat)){
1100  } else {
1102  }
1103  } else {
1104  if(isBE(c->dstFormat)){
1106  } else {
1108  }
1109  }
1110  dstPtr2 += dstStride[plane]/2;
1111  srcPtr2 += srcStride[plane]/2;
1112  }
1113  } else {
1114  if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1115  if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1116  DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
1117  } else {
1118  DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
1119  }
1120  }else{
1121  if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1122  DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
1123  } else {
1124  DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
1125  }
1126  }
1127  }
1128  } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
1129  isBE(c->srcFormat) != isBE(c->dstFormat)) {
1130 
1131  for (i = 0; i < height; i++) {
1132  for (j = 0; j < length; j++)
1133  ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
1134  srcPtr += srcStride[plane];
1135  dstPtr += dstStride[plane];
1136  }
1137  } else if (dstStride[plane] == srcStride[plane] &&
1138  srcStride[plane] > 0 && srcStride[plane] == length) {
1139  memcpy(dst[plane] + dstStride[plane] * y, src[plane],
1140  height * dstStride[plane]);
1141  } else {
1142  if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1143  length *= 2;
1144  else if (!desc_src->comp[0].depth_minus1)
1145  length >>= 3; // monowhite/black
1146  for (i = 0; i < height; i++) {
1147  memcpy(dstPtr, srcPtr, length);
1148  srcPtr += srcStride[plane];
1149  dstPtr += dstStride[plane];
1150  }
1151  }
1152  }
1153  }
1154  return srcSliceH;
1155 }
1156 
1157 
1158 #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
1159  ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
1160  (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
1161 
1162 
1164 {
1165  const enum AVPixelFormat srcFormat = c->srcFormat;
1166  const enum AVPixelFormat dstFormat = c->dstFormat;
1167  const int flags = c->flags;
1168  const int dstH = c->dstH;
1169  int needsDither;
1170 
1171  needsDither = isAnyRGB(dstFormat) &&
1172  c->dstFormatBpp < 24 &&
1173  (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1174 
1175  /* yv12_to_nv12 */
1176  if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
1177  (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
1179  }
1180  /* yuv2bgr */
1181  if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
1182  srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
1183  !(flags & (SWS_ACCURATE_RND|SWS_ERROR_DIFFUSION)) && !(dstH & 1)) {
1185  }
1186 
1187  if (srcFormat == AV_PIX_FMT_YUV410P &&
1188  (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1189  !(flags & SWS_BITEXACT)) {
1191  }
1192 
1193  /* bgr24toYV12 */
1194  if (srcFormat == AV_PIX_FMT_BGR24 &&
1195  (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
1196  !(flags & SWS_ACCURATE_RND))
1198 
1199  /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1200  if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
1201  && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1203 
1204  if ((srcFormat == AV_PIX_FMT_GBRP && dstFormat == AV_PIX_FMT_GBRAP) ||
1205  (srcFormat == AV_PIX_FMT_GBRAP && dstFormat == AV_PIX_FMT_GBRP))
1207 
1208 #define isByteRGB(f) ( \
1209  f == AV_PIX_FMT_RGB32 || \
1210  f == AV_PIX_FMT_RGB32_1 || \
1211  f == AV_PIX_FMT_RGB24 || \
1212  f == AV_PIX_FMT_BGR32 || \
1213  f == AV_PIX_FMT_BGR32_1 || \
1214  f == AV_PIX_FMT_BGR24)
1215 
1216  if (srcFormat == AV_PIX_FMT_GBRP && isPlanar(srcFormat) && isByteRGB(dstFormat))
1218 
1219  if ((srcFormat == AV_PIX_FMT_GBRP9LE || srcFormat == AV_PIX_FMT_GBRP9BE ||
1220  srcFormat == AV_PIX_FMT_GBRP16LE || srcFormat == AV_PIX_FMT_GBRP16BE ||
1221  srcFormat == AV_PIX_FMT_GBRP10LE || srcFormat == AV_PIX_FMT_GBRP10BE ||
1222  srcFormat == AV_PIX_FMT_GBRP12LE || srcFormat == AV_PIX_FMT_GBRP12BE ||
1223  srcFormat == AV_PIX_FMT_GBRP14LE || srcFormat == AV_PIX_FMT_GBRP14BE) &&
1224  (dstFormat == AV_PIX_FMT_RGB48LE || dstFormat == AV_PIX_FMT_RGB48BE ||
1225  dstFormat == AV_PIX_FMT_BGR48LE || dstFormat == AV_PIX_FMT_BGR48BE ||
1226  dstFormat == AV_PIX_FMT_RGBA64LE || dstFormat == AV_PIX_FMT_RGBA64BE ||
1227  dstFormat == AV_PIX_FMT_BGRA64LE || dstFormat == AV_PIX_FMT_BGRA64BE))
1229 
1230  if (av_pix_fmt_desc_get(srcFormat)->comp[0].depth_minus1 == 7 &&
1231  isPackedRGB(srcFormat) && dstFormat == AV_PIX_FMT_GBRP)
1233 
1234  /* bswap 16 bits per pixel/component packed formats */
1235  if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
1236  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48) ||
1237  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
1238  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
1239  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
1240  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
1241  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP9) ||
1242  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP10) ||
1243  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP12) ||
1244  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP14) ||
1245  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP16) ||
1246  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP16) ||
1247  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
1248  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48) ||
1249  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
1250  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
1251  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
1252  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12) ||
1253  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P9) ||
1254  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P10) ||
1255  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P12) ||
1256  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P14) ||
1257  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P16) ||
1258  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P9) ||
1259  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P10) ||
1260  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P12) ||
1261  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P14) ||
1262  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P16) ||
1263  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P9) ||
1264  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P10) ||
1265  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P12) ||
1266  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P14) ||
1267  IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P16))
1269 
1270  if (usePal(srcFormat) && isByteRGB(dstFormat))
1271  c->swScale = palToRgbWrapper;
1272 
1273  if (srcFormat == AV_PIX_FMT_YUV422P) {
1274  if (dstFormat == AV_PIX_FMT_YUYV422)
1276  else if (dstFormat == AV_PIX_FMT_UYVY422)
1278  }
1279 
1280  /* LQ converters if -sws 0 or -sws 4*/
1281  if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1282  /* yv12_to_yuy2 */
1283  if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
1284  if (dstFormat == AV_PIX_FMT_YUYV422)
1286  else if (dstFormat == AV_PIX_FMT_UYVY422)
1288  }
1289  }
1290  if (srcFormat == AV_PIX_FMT_YUYV422 &&
1291  (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1293  if (srcFormat == AV_PIX_FMT_UYVY422 &&
1294  (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
1296  if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
1298  if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
1300 
1301 #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_GRAY8A)
1302  /* simple copy */
1303  if ( srcFormat == dstFormat ||
1304  (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
1305  (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
1306  (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
1307  (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
1308  (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
1309  (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
1312  dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
1313  srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21))
1314  {
1315  if (isPacked(c->srcFormat))
1317  else /* Planar YUV or gray */
1319  }
1320 
1321  if (ARCH_BFIN)
1323  if (HAVE_ALTIVEC)
1325 }
1326 
1327 /* Convert the palette to the same packed 32-bit format as the palette */
1328 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
1329  int num_pixels, const uint8_t *palette)
1330 {
1331  int i;
1332 
1333  for (i = 0; i < num_pixels; i++)
1334  ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1335 }
1336 
1337 /* Palette format: ABCD -> dst format: ABC */
1338 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
1339  int num_pixels, const uint8_t *palette)
1340 {
1341  int i;
1342 
1343  for (i = 0; i < num_pixels; i++) {
1344  //FIXME slow?
1345  dst[0] = palette[src[i] * 4 + 0];
1346  dst[1] = palette[src[i] * 4 + 1];
1347  dst[2] = palette[src[i] * 4 + 2];
1348  dst += 3;
1349  }
1350 }