]> git.sesse.net Git - ffmpeg/blob - libswscale/tests/swscale.c
Merge commit '470cd0c5fe6337b6cb5276b3f84400999450fc1b'
[ffmpeg] / libswscale / tests / swscale.c
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 {
87     const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
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,
204                                     AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
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 {
255     const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
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 };
261     enum AVPixelFormat srcFormat, dstFormat;
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,
405                          AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
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 }