Libav
swscale.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <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(uint8_t *src1, 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];
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  av_image_fill_linesizes(srcStride, srcFormat, srcW);
111  for (p = 0; p < 4; p++) {
112  if (srcStride[p])
113  src[p] = av_mallocz(srcStride[p] * srcH + 16);
114  if (srcStride[p] && !src[p]) {
115  perror("Malloc");
116  res = -1;
117  goto end;
118  }
119  }
120  srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
121  srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
122  if (!srcContext) {
123  fprintf(stderr, "Failed to get %s ---> %s\n",
124  desc_yuva420p->name,
125  desc_src->name);
126  res = -1;
127  goto end;
128  }
129  sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
130  sws_freeContext(srcContext);
131 
132  cur_srcFormat = srcFormat;
133  cur_srcW = srcW;
134  cur_srcH = srcH;
135  }
136 
137  av_image_fill_linesizes(dstStride, dstFormat, dstW);
138  for (i = 0; i < 4; i++) {
139  /* Image buffers passed into libswscale can be allocated any way you
140  * prefer, as long as they're aligned enough for the architecture, and
141  * they're freed appropriately (such as using av_free for buffers
142  * allocated with av_malloc). */
143  /* An extra 16 bytes is being allocated because some scalers may write
144  * out of bounds. */
145  if (dstStride[i])
146  dst[i] = av_mallocz(dstStride[i] * dstH + 16);
147  if (dstStride[i] && !dst[i]) {
148  perror("Malloc");
149  res = -1;
150 
151  goto end;
152  }
153  }
154 
155  dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
156  flags, NULL, NULL, NULL);
157  if (!dstContext) {
158  fprintf(stderr, "Failed to get %s ---> %s\n",
159  desc_src->name, desc_dst->name);
160  res = -1;
161  goto end;
162  }
163 
164  printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
165  desc_src->name, srcW, srcH,
166  desc_dst->name, dstW, dstH,
167  flags);
168  fflush(stdout);
169 
170  sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
171 
172  for (i = 0; i < 4 && dstStride[i]; i++)
173  crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
174  dstStride[i] * dstH);
175 
176  if (r && crc == r->crc) {
177  ssdY = r->ssdY;
178  ssdU = r->ssdU;
179  ssdV = r->ssdV;
180  ssdA = r->ssdA;
181  } else {
182  for (i = 0; i < 4; i++) {
183  if (refStride[i])
184  out[i] = av_mallocz(refStride[i] * h);
185  if (refStride[i] && !out[i]) {
186  perror("Malloc");
187  res = -1;
188  goto end;
189  }
190  }
191  outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
193  NULL, NULL, NULL);
194  if (!outContext) {
195  fprintf(stderr, "Failed to get %s ---> %s\n",
196  desc_dst->name,
197  desc_yuva420p->name);
198  res = -1;
199  goto end;
200  }
201  sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
202 
203  ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
204  if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
205  //FIXME check that output is really gray
206  ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
207  (w + 1) >> 1, (h + 1) >> 1);
208  ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
209  (w + 1) >> 1, (h + 1) >> 1);
210  }
211  if (isALPHA(srcFormat) && isALPHA(dstFormat))
212  ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
213 
214  ssdY /= w * h;
215  ssdU /= w * h / 4;
216  ssdV /= w * h / 4;
217  ssdA /= w * h;
218 
219  sws_freeContext(outContext);
220 
221  for (i = 0; i < 4; i++)
222  if (refStride[i])
223  av_free(out[i]);
224  }
225 
226  printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
227  crc, ssdY, ssdU, ssdV, ssdA);
228 
229 end:
230  sws_freeContext(dstContext);
231 
232  for (i = 0; i < 4; i++)
233  if (dstStride[i])
234  av_free(dst[i]);
235 
236  return res;
237 }
238 
239 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
240  enum AVPixelFormat srcFormat_in,
241  enum AVPixelFormat dstFormat_in)
242 {
244  SWS_X, SWS_POINT, SWS_AREA, 0 };
245  const int srcW = w;
246  const int srcH = h;
247  const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
248  const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
250  const AVPixFmtDescriptor *desc_src, *desc_dst;
251 
252  for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
253  srcFormat < AV_PIX_FMT_NB; srcFormat++) {
254  if (!sws_isSupportedInput(srcFormat) ||
255  !sws_isSupportedOutput(srcFormat))
256  continue;
257 
258  desc_src = av_pix_fmt_desc_get(srcFormat);
259 
260  for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
261  dstFormat < AV_PIX_FMT_NB; dstFormat++) {
262  int i, j, k;
263  int res = 0;
264 
265  if (!sws_isSupportedInput(dstFormat) ||
266  !sws_isSupportedOutput(dstFormat))
267  continue;
268 
269  desc_dst = av_pix_fmt_desc_get(dstFormat);
270 
271  printf("%s -> %s\n", desc_src->name, desc_dst->name);
272  fflush(stdout);
273 
274  for (k = 0; flags[k] && !res; k++)
275  for (i = 0; dstW[i] && !res; i++)
276  for (j = 0; dstH[j] && !res; j++)
277  res = doTest(ref, refStride, w, h,
278  srcFormat, dstFormat,
279  srcW, srcH, dstW[i], dstH[j], flags[k],
280  NULL);
281  if (dstFormat_in != AV_PIX_FMT_NONE)
282  break;
283  }
284  if (srcFormat_in != AV_PIX_FMT_NONE)
285  break;
286  }
287 }
288 
289 static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
290  enum AVPixelFormat srcFormat_in,
291  enum AVPixelFormat dstFormat_in)
292 {
293  char buf[256];
294 
295  while (fgets(buf, sizeof(buf), fp)) {
296  struct Results r;
297  enum AVPixelFormat srcFormat;
298  char srcStr[12];
299  int srcW, srcH;
300  enum AVPixelFormat dstFormat;
301  char dstStr[12];
302  int dstW, dstH;
303  int flags;
304  int ret;
305 
306  ret = sscanf(buf,
307  " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
308  " SSD=%"PRId64 ", %"PRId64 ", %"PRId64 ", %"PRId64 "\n",
309  srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
310  &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
311  if (ret != 12) {
312  srcStr[0] = dstStr[0] = 0;
313  ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
314  }
315 
316  srcFormat = av_get_pix_fmt(srcStr);
317  dstFormat = av_get_pix_fmt(dstStr);
318 
319  if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE) {
320  fprintf(stderr, "malformed input file\n");
321  return -1;
322  }
323  if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
324  (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
325  continue;
326  if (ret != 12) {
327  printf("%s", buf);
328  continue;
329  }
330 
331  doTest(ref, refStride, w, h,
332  srcFormat, dstFormat,
333  srcW, srcH, dstW, dstH, flags,
334  &r);
335  }
336 
337  return 0;
338 }
339 
340 #define W 96
341 #define H 96
342 
343 int main(int argc, char **argv)
344 {
345  enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
346  enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
347  uint8_t *rgb_data = av_malloc(W * H * 4);
348  uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
349  int rgb_stride[4] = { 4 * W, 0, 0, 0 };
350  uint8_t *data = av_malloc(4 * W * H);
351  uint8_t *src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
352  int stride[4] = { W, W, W, W };
353  int x, y;
354  struct SwsContext *sws;
355  AVLFG rand;
356  int res = -1;
357  int i;
358 
359  if (!rgb_data || !data)
360  return -1;
361 
362  sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
363  AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
364 
365  av_lfg_init(&rand, 1);
366 
367  for (y = 0; y < H; y++)
368  for (x = 0; x < W * 4; x++)
369  rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
370  sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
371  sws_freeContext(sws);
372  av_free(rgb_data);
373 
374  for (i = 1; i < argc; i += 2) {
375  if (argv[i][0] != '-' || i + 1 == argc)
376  goto bad_option;
377  if (!strcmp(argv[i], "-ref")) {
378  FILE *fp = fopen(argv[i + 1], "r");
379  if (!fp) {
380  fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
381  goto error;
382  }
383  res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
384  fclose(fp);
385  goto end;
386  } else if (!strcmp(argv[i], "-src")) {
387  srcFormat = av_get_pix_fmt(argv[i + 1]);
388  if (srcFormat == AV_PIX_FMT_NONE) {
389  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
390  return -1;
391  }
392  } else if (!strcmp(argv[i], "-dst")) {
393  dstFormat = av_get_pix_fmt(argv[i + 1]);
394  if (dstFormat == AV_PIX_FMT_NONE) {
395  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
396  return -1;
397  }
398  } else {
399 bad_option:
400  fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
401  goto error;
402  }
403  }
404 
405  selfTest(src, stride, W, H, srcFormat, dstFormat);
406 end:
407  res = 0;
408 error:
409  av_free(data);
410 
411  return res;
412 }
Definition: lfg.h:25
int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
Return a positive value if pix_fmt is a supported output format, 0 otherwise.
Definition: utils.c:198
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1768
#define SWS_X
Definition: swscale.h:60
#define SWS_BICUBIC
Definition: swscale.h:59
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:312
misc image utilities
memory handling functions
#define hasChroma(x)
Definition: swscale.c:46
external API header
uint64_t ssdY
Definition: swscale.c:73
int stride
Definition: mace.c:144
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:343
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int srcH
Height of source luma/alpha planes.
#define SWS_BILINEAR
Definition: swscale.h:58
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:98
uint8_t
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:298
#define H
Definition: swscale.c:341
#define SWS_FAST_BILINEAR
Definition: swscale.h:57
const char data[16]
Definition: mxf.c:70
static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
Definition: swscale.c:57
static int flags
Definition: log.c:50
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:1319
external api for the swscale stuff
enum AVPixelFormat dstFormat
Destination pixel format.
#define isALPHA(x)
Definition: swscale.c:50
const char * name
Definition: pixdesc.h:81
#define r
Definition: input.c:51
int dstH
Height of destination luma/alpha planes.
#define src
Definition: vp8dsp.c:254
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
uint64_t ssdA
Definition: swscale.c:76
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:1679
int dstW
Width of destination luma/alpha planes.
NULL
Definition: eval.c:55
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:80
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:242
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:289
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst...
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
uint64_t ssdU
Definition: swscale.c:74
#define SWS_AREA
Definition: swscale.h:62
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:69
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:61
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:239
#define W
Definition: swscale.c:340
enum AVPixelFormat srcFormat
Source pixel format.
int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
Return a positive value if pix_fmt is a supported input format, 0 otherwise.
Definition: utils.c:192
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:233
FILE * out
Definition: movenc.c:54
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1716
int srcW
Width of source luma/alpha planes.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
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:211