FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
swscale.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <stdarg.h>
26 
27 #undef HAVE_AV_CONFIG_H
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/avutil.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/lfg.h"
34 
35 #include "libswscale/swscale.h"
36 
37 /* HACK Duplicated from swscale_internal.h.
38  * Should be removed when a cleaner pixel format system exists. */
39 #define isGray(x) \
40  ((x) == AV_PIX_FMT_GRAY8 || \
41  (x) == AV_PIX_FMT_YA8 || \
42  (x) == AV_PIX_FMT_GRAY16BE || \
43  (x) == AV_PIX_FMT_GRAY16LE || \
44  (x) == AV_PIX_FMT_YA16BE || \
45  (x) == AV_PIX_FMT_YA16LE)
46 #define hasChroma(x) \
47  (!(isGray(x) || \
48  (x) == AV_PIX_FMT_MONOBLACK || \
49  (x) == AV_PIX_FMT_MONOWHITE))
50 #define isALPHA(x) \
51  ((x) == AV_PIX_FMT_BGR32 || \
52  (x) == AV_PIX_FMT_BGR32_1 || \
53  (x) == AV_PIX_FMT_RGB32 || \
54  (x) == AV_PIX_FMT_RGB32_1 || \
55  (x) == AV_PIX_FMT_YUVA420P)
56 
57 static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2, int stride1,
58  int stride2, int w, int h)
59 {
60  int x, y;
61  uint64_t ssd = 0;
62 
63  for (y = 0; y < h; y++) {
64  for (x = 0; x < w; x++) {
65  int d = src1[x + y * stride1] - src2[x + y * stride2];
66  ssd += d * d;
67  }
68  }
69  return ssd;
70 }
71 
72 struct Results {
73  uint64_t ssdY;
74  uint64_t ssdU;
75  uint64_t ssdV;
76  uint64_t ssdA;
77  uint32_t crc;
78 };
79 
80 // test by ref -> src -> dst -> out & compare out against ref
81 // ref & out are YV12
82 static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
83  enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
84  int srcW, int srcH, int dstW, int dstH, int flags,
85  struct Results *r)
86 {
88  const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
89  const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
90  static enum AVPixelFormat cur_srcFormat;
91  static int cur_srcW, cur_srcH;
92  static uint8_t *src[4];
93  static int srcStride[4];
94  uint8_t *dst[4] = { 0 };
95  uint8_t *out[4] = { 0 };
96  int dstStride[4] = {0};
97  int i;
98  uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
99  struct SwsContext *dstContext = NULL, *outContext = NULL;
100  uint32_t crc = 0;
101  int res = 0;
102 
103  if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
104  struct SwsContext *srcContext = NULL;
105  int p;
106 
107  for (p = 0; p < 4; p++)
108  av_freep(&src[p]);
109 
110  res = av_image_fill_linesizes(srcStride, srcFormat, srcW);
111  if (res < 0) {
112  fprintf(stderr, "av_image_fill_linesizes failed\n");
113  goto end;
114  }
115  for (p = 0; p < 4; p++) {
116  srcStride[p] = FFALIGN(srcStride[p], 16);
117  if (srcStride[p])
118  src[p] = av_mallocz(srcStride[p] * srcH + 16);
119  if (srcStride[p] && !src[p]) {
120  perror("Malloc");
121  res = -1;
122  goto end;
123  }
124  }
125  srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
126  srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
127  if (!srcContext) {
128  fprintf(stderr, "Failed to get %s ---> %s\n",
129  desc_yuva420p->name,
130  desc_src->name);
131  res = -1;
132  goto end;
133  }
134  sws_scale(srcContext, (const uint8_t * const*)ref, refStride, 0, h, src, srcStride);
135  sws_freeContext(srcContext);
136 
137  cur_srcFormat = srcFormat;
138  cur_srcW = srcW;
139  cur_srcH = srcH;
140  }
141 
142  res = av_image_fill_linesizes(dstStride, dstFormat, dstW);
143  if (res < 0) {
144  fprintf(stderr, "av_image_fill_linesizes failed\n");
145  goto end;
146  }
147 
148  for (i = 0; i < 4; i++) {
149  /* Image buffers passed into libswscale can be allocated any way you
150  * prefer, as long as they're aligned enough for the architecture, and
151  * they're freed appropriately (such as using av_free for buffers
152  * allocated with av_malloc). */
153  /* An extra 16 bytes is being allocated because some scalers may write
154  * out of bounds. */
155  dstStride[i] = FFALIGN(dstStride[i], 16);
156  if (dstStride[i])
157  dst[i] = av_mallocz(dstStride[i] * dstH + 16);
158  if (dstStride[i] && !dst[i]) {
159  perror("Malloc");
160  res = -1;
161 
162  goto end;
163  }
164  }
165 
166  dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
167  flags, NULL, NULL, NULL);
168  if (!dstContext) {
169  fprintf(stderr, "Failed to get %s ---> %s\n",
170  desc_src->name, desc_dst->name);
171  res = -1;
172  goto end;
173  }
174 
175  printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
176  desc_src->name, srcW, srcH,
177  desc_dst->name, dstW, dstH,
178  flags);
179  fflush(stdout);
180 
181  sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
182 
183  for (i = 0; i < 4 && dstStride[i]; i++)
184  crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
185  dstStride[i] * dstH);
186 
187  if (r && crc == r->crc) {
188  ssdY = r->ssdY;
189  ssdU = r->ssdU;
190  ssdV = r->ssdV;
191  ssdA = r->ssdA;
192  } else {
193  for (i = 0; i < 4; i++) {
194  refStride[i] = FFALIGN(refStride[i], 16);
195  if (refStride[i])
196  out[i] = av_mallocz(refStride[i] * h);
197  if (refStride[i] && !out[i]) {
198  perror("Malloc");
199  res = -1;
200  goto end;
201  }
202  }
203  outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
205  NULL, NULL, NULL);
206  if (!outContext) {
207  fprintf(stderr, "Failed to get %s ---> %s\n",
208  desc_dst->name,
209  desc_yuva420p->name);
210  res = -1;
211  goto end;
212  }
213  sws_scale(outContext, (const uint8_t * const*)dst, dstStride, 0, dstH, out, refStride);
214 
215  ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
216  if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
217  //FIXME check that output is really gray
218  ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
219  (w + 1) >> 1, (h + 1) >> 1);
220  ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
221  (w + 1) >> 1, (h + 1) >> 1);
222  }
223  if (isALPHA(srcFormat) && isALPHA(dstFormat))
224  ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
225 
226  ssdY /= w * h;
227  ssdU /= w * h / 4;
228  ssdV /= w * h / 4;
229  ssdA /= w * h;
230 
231  sws_freeContext(outContext);
232 
233  for (i = 0; i < 4; i++)
234  if (refStride[i])
235  av_free(out[i]);
236  }
237 
238  printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
239  crc, ssdY, ssdU, ssdV, ssdA);
240 
241 end:
242  sws_freeContext(dstContext);
243 
244  for (i = 0; i < 4; i++)
245  if (dstStride[i])
246  av_free(dst[i]);
247 
248  return res;
249 }
250 
251 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
252  enum AVPixelFormat srcFormat_in,
253  enum AVPixelFormat dstFormat_in)
254 {
256  SWS_X, SWS_POINT, SWS_AREA, 0 };
257  const int srcW = w;
258  const int srcH = h;
259  const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
260  const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
262  const AVPixFmtDescriptor *desc_src, *desc_dst;
263 
264  for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
265  srcFormat < AV_PIX_FMT_NB; srcFormat++) {
266  if (!sws_isSupportedInput(srcFormat) ||
267  !sws_isSupportedOutput(srcFormat))
268  continue;
269 
270  desc_src = av_pix_fmt_desc_get(srcFormat);
271 
272  for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
273  dstFormat < AV_PIX_FMT_NB; dstFormat++) {
274  int i, j, k;
275  int res = 0;
276 
277  if (!sws_isSupportedInput(dstFormat) ||
278  !sws_isSupportedOutput(dstFormat))
279  continue;
280 
281  desc_dst = av_pix_fmt_desc_get(dstFormat);
282 
283  printf("%s -> %s\n", desc_src->name, desc_dst->name);
284  fflush(stdout);
285 
286  for (k = 0; flags[k] && !res; k++)
287  for (i = 0; dstW[i] && !res; i++)
288  for (j = 0; dstH[j] && !res; j++)
289  res = doTest(ref, refStride, w, h,
290  srcFormat, dstFormat,
291  srcW, srcH, dstW[i], dstH[j], flags[k],
292  NULL);
293  if (dstFormat_in != AV_PIX_FMT_NONE)
294  break;
295  }
296  if (srcFormat_in != AV_PIX_FMT_NONE)
297  break;
298  }
299 }
300 
301 static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
302  enum AVPixelFormat srcFormat_in,
303  enum AVPixelFormat dstFormat_in)
304 {
305  char buf[256];
306 
307  while (fgets(buf, sizeof(buf), fp)) {
308  struct Results r;
309  enum AVPixelFormat srcFormat;
310  char srcStr[12];
311  int srcW, srcH;
312  enum AVPixelFormat dstFormat;
313  char dstStr[12];
314  int dstW, dstH;
315  int flags;
316  int ret;
317 
318  ret = sscanf(buf,
319  " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
320  " SSD=%"SCNd64 ", %"SCNd64 ", %"SCNd64 ", %"SCNd64 "\n",
321  srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
322  &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
323  if (ret != 12) {
324  srcStr[0] = dstStr[0] = 0;
325  ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
326  }
327 
328  srcFormat = av_get_pix_fmt(srcStr);
329  dstFormat = av_get_pix_fmt(dstStr);
330 
331  if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE ||
332  srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) {
333  fprintf(stderr, "malformed input file\n");
334  return -1;
335  }
336  if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
337  (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
338  continue;
339  if (ret != 12) {
340  printf("%s", buf);
341  continue;
342  }
343 
344  doTest(ref, refStride, w, h,
345  srcFormat, dstFormat,
346  srcW, srcH, dstW, dstH, flags,
347  &r);
348  }
349 
350  return 0;
351 }
352 
353 #define W 96
354 #define H 96
355 
356 int main(int argc, char **argv)
357 {
358  enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
359  enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
360  uint8_t *rgb_data = av_malloc(W * H * 4);
361  const uint8_t * const rgb_src[4] = { rgb_data, NULL, NULL, NULL };
362  int rgb_stride[4] = { 4 * W, 0, 0, 0 };
363  uint8_t *data = av_malloc(4 * W * H);
364  uint8_t *src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
365  int stride[4] = { W, W, W, W };
366  int x, y;
367  struct SwsContext *sws;
368  AVLFG rand;
369  int res = -1;
370  int i;
371  FILE *fp = NULL;
372 
373  if (!rgb_data || !data)
374  return -1;
375 
376  for (i = 1; i < argc; i += 2) {
377  if (argv[i][0] != '-' || i + 1 == argc)
378  goto bad_option;
379  if (!strcmp(argv[i], "-ref")) {
380  fp = fopen(argv[i + 1], "r");
381  if (!fp) {
382  fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
383  goto error;
384  }
385  } else if (!strcmp(argv[i], "-src")) {
386  srcFormat = av_get_pix_fmt(argv[i + 1]);
387  if (srcFormat == AV_PIX_FMT_NONE) {
388  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
389  return -1;
390  }
391  } else if (!strcmp(argv[i], "-dst")) {
392  dstFormat = av_get_pix_fmt(argv[i + 1]);
393  if (dstFormat == AV_PIX_FMT_NONE) {
394  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
395  return -1;
396  }
397  } else {
398 bad_option:
399  fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
400  goto error;
401  }
402  }
403 
404  sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
406 
407  av_lfg_init(&rand, 1);
408 
409  for (y = 0; y < H; y++)
410  for (x = 0; x < W * 4; x++)
411  rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
412  sws_scale(sws, rgb_src, rgb_stride, 0, H / 12, src, stride);
413  sws_freeContext(sws);
414  av_free(rgb_data);
415 
416  if(fp) {
417  res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
418  fclose(fp);
419  } else {
420  selfTest(src, stride, W, H, srcFormat, dstFormat);
421  res = 0;
422  }
423 error:
424  av_free(data);
425 
426  return res;
427 }
Definition: lfg.h:25
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
#define SWS_X
Definition: swscale.h:59
#define SWS_BICUBIC
Definition: swscale.h:58
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
memory handling functions
#define hasChroma(x)
Definition: swscale.c:46
external API header
uint64_t ssdY
Definition: swscale.c:73
static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2, int stride1, int stride2, int w, int h)
Definition: swscale.c:57
static int doTest(uint8_t *ref[4], int refStride[4], int w, int h, enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat, int srcW, int srcH, int dstW, int dstH, int flags, struct Results *r)
Definition: swscale.c:82
int main(int argc, char **argv)
Definition: swscale.c:356
int srcH
Height of source luma/alpha planes.
#define SWS_BILINEAR
Definition: swscale.h:57
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define H
Definition: swscale.c:354
#define SWS_FAST_BILINEAR
Definition: swscale.h:56
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:1854
external API header
enum AVPixelFormat dstFormat
Destination pixel format.
#define isALPHA(x)
Definition: swscale.c:50
#define FFALIGN(x, a)
Definition: macros.h:48
const char * name
Definition: pixdesc.h:82
int dstH
Height of destination luma/alpha planes.
#define U(x)
Definition: vp56_arith.h:37
#define sws_isSupportedOutput(x)
const char * r
Definition: vf_curves.c:107
uint64_t ssdA
Definition: swscale.c:76
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2274
#define sws_isSupportedInput(x)
#define src
Definition: vp9dsp.c:530
int dstW
Width of destination luma/alpha planes.
uint32_t crc
Definition: swscale.c:77
#define src1
Definition: h264pred.c:139
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:318
#define fp
Definition: regdef.h:44
static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp, enum AVPixelFormat srcFormat_in, enum AVPixelFormat dstFormat_in)
Definition: swscale.c:301
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition: swscale.c:753
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
void * buf
Definition: avisynth_c.h:553
uint64_t ssdU
Definition: swscale.c:74
#define SWS_AREA
Definition: swscale.h:61
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:88
uint64_t ssdV
Definition: swscale.c:75
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
#define SWS_POINT
Definition: swscale.h:60
static int flags
Definition: cpu.c:47
static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h, enum AVPixelFormat srcFormat_in, enum AVPixelFormat dstFormat_in)
Definition: swscale.c:251
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
#define W
Definition: swscale.c:353
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
enum AVPixelFormat srcFormat
Source pixel format.
#define av_free(p)
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:306
FILE * out
Definition: movenc.c:54
#define av_freep(p)
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2150
int srcW
Width of source luma/alpha planes.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252