tests: Move all test programs to a subdirectory
[libav.git] / libswscale / tests / swscale.c
CommitLineData
c9b99ea6 1/*
d026b45e
DB
2 * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
3 *
2912e87a 4 * This file is part of Libav.
d026b45e 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
54e3ae2e
DB
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.
d026b45e 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
d026b45e 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54e3ae2e
DB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
d026b45e 15 *
54e3ae2e 16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
b19bcbaa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d026b45e 19 */
c9b99ea6
MN
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <inttypes.h>
25#include <stdarg.h>
26
e9e12f0e 27#undef HAVE_AV_CONFIG_H
737eb597 28#include "libavutil/imgutils.h"
51d83986 29#include "libavutil/mem.h"
83da2c6f 30#include "libavutil/avutil.h"
8d884020 31#include "libavutil/crc.h"
92bfd746 32#include "libavutil/pixdesc.h"
9a034dde 33#include "libavutil/lfg.h"
a6a750c7
DB
34
35#include "libswscale/swscale.h"
c9b99ea6 36
71ed6c8f
RP
37/* HACK Duplicated from swscale_internal.h.
38 * Should be removed when a cleaner pixel format system exists. */
999484c9 39#define isGray(x) \
716d413c 40 ((x) == AV_PIX_FMT_GRAY8 || \
e96c3b81 41 (x) == AV_PIX_FMT_YA8 || \
716d413c 42 (x) == AV_PIX_FMT_GRAY16BE || \
f84a1b59
VG
43 (x) == AV_PIX_FMT_GRAY16LE || \
44 (x) == AV_PIX_FMT_YA16BE || \
45 (x) == AV_PIX_FMT_YA16LE)
999484c9
DB
46#define hasChroma(x) \
47 (!(isGray(x) || \
716d413c
AK
48 (x) == AV_PIX_FMT_MONOBLACK || \
49 (x) == AV_PIX_FMT_MONOWHITE))
999484c9 50#define isALPHA(x) \
716d413c
AK
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)
999484c9
DB
56
57static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1,
58 int stride2, int w, int h)
dd68318c 59{
999484c9
DB
60 int x, y;
61 uint64_t ssd = 0;
c9b99ea6 62
999484c9
DB
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;
221b804f 67 }
221b804f
DB
68 }
69 return ssd;
c9b99ea6
MN
70}
71
4bf44785
RP
72struct Results {
73 uint64_t ssdY;
74 uint64_t ssdU;
75 uint64_t ssdV;
76 uint64_t ssdA;
77 uint32_t crc;
78};
79
c9b99ea6
MN
80// test by ref -> src -> dst -> out & compare out against ref
81// ref & out are YV12
2d8d885b 82static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
716d413c 83 enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
4bf44785
RP
84 int srcW, int srcH, int dstW, int dstH, int flags,
85 struct Results *r)
dd68318c 86{
0a7068fa
AK
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);
716d413c 90 static enum AVPixelFormat cur_srcFormat;
6bbae4c8 91 static int cur_srcW, cur_srcH;
3e1e7800
RP
92 static uint8_t *src[4];
93 static int srcStride[4];
999484c9
DB
94 uint8_t *dst[4] = { 0 };
95 uint8_t *out[4] = { 0 };
73016d29 96 int dstStride[4];
221b804f 97 int i;
999484c9 98 uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
73016d29 99 struct SwsContext *dstContext = NULL, *outContext = NULL;
8d884020 100 uint32_t crc = 0;
999484c9 101 int res = 0;
221b804f 102
6bbae4c8 103 if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
3e1e7800
RP
104 struct SwsContext *srcContext = NULL;
105 int p;
106
107 for (p = 0; p < 4; p++)
523d9407 108 av_freep(&src[p]);
3e1e7800
RP
109
110 av_image_fill_linesizes(srcStride, srcFormat, srcW);
111 for (p = 0; p < 4; p++) {
112 if (srcStride[p])
999484c9 113 src[p] = av_mallocz(srcStride[p] * srcH + 16);
3e1e7800
RP
114 if (srcStride[p] && !src[p]) {
115 perror("Malloc");
116 res = -1;
3e1e7800
RP
117 goto end;
118 }
119 }
716d413c 120 srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
6bbae4c8 121 srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
3e1e7800
RP
122 if (!srcContext) {
123 fprintf(stderr, "Failed to get %s ---> %s\n",
0a7068fa
AK
124 desc_yuva420p->name,
125 desc_src->name);
3e1e7800 126 res = -1;
3e1e7800
RP
127 goto end;
128 }
129 sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
130 sws_freeContext(srcContext);
131
132 cur_srcFormat = srcFormat;
999484c9
DB
133 cur_srcW = srcW;
134 cur_srcH = srcH;
3e1e7800
RP
135 }
136
4389d606 137 av_image_fill_linesizes(dstStride, dstFormat, dstW);
999484c9 138 for (i = 0; i < 4; i++) {
9e5f164d
RP
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). */
67e1d527
RP
143 /* An extra 16 bytes is being allocated because some scalers may write
144 * out of bounds. */
06c93cb7 145 if (dstStride[i])
999484c9 146 dst[i] = av_mallocz(dstStride[i] * dstH + 16);
4bf44785 147 if (dstStride[i] && !dst[i]) {
221b804f
DB
148 perror("Malloc");
149 res = -1;
150
151 goto end;
152 }
153 }
154
999484c9
DB
155 dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
156 flags, NULL, NULL, NULL);
1b0a4572 157 if (!dstContext) {
221b804f 158 fprintf(stderr, "Failed to get %s ---> %s\n",
0a7068fa 159 desc_src->name, desc_dst->name);
221b804f 160 res = -1;
221b804f
DB
161 goto end;
162 }
221b804f 163
9bb9875e 164 printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
0a7068fa
AK
165 desc_src->name, srcW, srcH,
166 desc_dst->name, dstW, dstH,
b6f1e7ec
RP
167 flags);
168 fflush(stdout);
169
221b804f 170 sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
27161c06 171
999484c9
DB
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);
8d884020 175
4bf44785
RP
176 if (r && crc == r->crc) {
177 ssdY = r->ssdY;
178 ssdU = r->ssdU;
179 ssdV = r->ssdV;
180 ssdA = r->ssdA;
181 } else {
999484c9 182 for (i = 0; i < 4; i++) {
4bf44785 183 if (refStride[i])
999484c9 184 out[i] = av_mallocz(refStride[i] * h);
4bf44785
RP
185 if (refStride[i] && !out[i]) {
186 perror("Malloc");
187 res = -1;
4bf44785
RP
188 goto end;
189 }
190 }
999484c9 191 outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
716d413c 192 AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
999484c9 193 NULL, NULL, NULL);
4bf44785
RP
194 if (!outContext) {
195 fprintf(stderr, "Failed to get %s ---> %s\n",
0a7068fa
AK
196 desc_dst->name,
197 desc_yuva420p->name);
4bf44785 198 res = -1;
4bf44785
RP
199 goto end;
200 }
201 sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
202
999484c9 203 ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
9075bc7c
RP
204 if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
205 //FIXME check that output is really gray
999484c9
DB
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);
9075bc7c
RP
210 }
211 if (isALPHA(srcFormat) && isALPHA(dstFormat))
999484c9 212 ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
6a4970ab 213
999484c9
DB
214 ssdY /= w * h;
215 ssdU /= w * h / 4;
216 ssdV /= w * h / 4;
217 ssdA /= w * h;
6a4970ab 218
4bf44785
RP
219 sws_freeContext(outContext);
220
999484c9 221 for (i = 0; i < 4; i++)
4bf44785
RP
222 if (refStride[i])
223 av_free(out[i]);
4bf44785
RP
224 }
225
999484c9 226 printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
8d884020 227 crc, ssdY, ssdU, ssdV, ssdA);
c9b99ea6 228
a48ce2c3 229end:
221b804f 230 sws_freeContext(dstContext);
c9b99ea6 231
999484c9 232 for (i = 0; i < 4; i++)
06c93cb7 233 if (dstStride[i])
a4fc3bd5 234 av_free(dst[i]);
c9b99ea6 235
221b804f 236 return res;
c9b99ea6
MN
237}
238
9b4bf9c2 239static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
716d413c
AK
240 enum AVPixelFormat srcFormat_in,
241 enum AVPixelFormat dstFormat_in)
dd68318c 242{
999484c9
DB
243 const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
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 };
716d413c 249 enum AVPixelFormat srcFormat, dstFormat;
0a7068fa 250 const AVPixFmtDescriptor *desc_src, *desc_dst;
91cfeac6 251
716d413c
AK
252 for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
253 srcFormat < AV_PIX_FMT_NB; srcFormat++) {
999484c9
DB
254 if (!sws_isSupportedInput(srcFormat) ||
255 !sws_isSupportedOutput(srcFormat))
91cfeac6
SS
256 continue;
257
0a7068fa
AK
258 desc_src = av_pix_fmt_desc_get(srcFormat);
259
716d413c
AK
260 for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
261 dstFormat < AV_PIX_FMT_NB; dstFormat++) {
91cfeac6
SS
262 int i, j, k;
263 int res = 0;
264
999484c9
DB
265 if (!sws_isSupportedInput(dstFormat) ||
266 !sws_isSupportedOutput(dstFormat))
91cfeac6 267 continue;
731c04ad 268
0a7068fa
AK
269 desc_dst = av_pix_fmt_desc_get(dstFormat);
270
271 printf("%s -> %s\n", desc_src->name, desc_dst->name);
ae9e0e83 272 fflush(stdout);
221b804f 273
999484c9 274 for (k = 0; flags[k] && !res; k++)
be1c5d35
RP
275 for (i = 0; dstW[i] && !res; i++)
276 for (j = 0; dstH[j] && !res; j++)
3e1e7800 277 res = doTest(ref, refStride, w, h,
73016d29 278 srcFormat, dstFormat,
4bf44785
RP
279 srcW, srcH, dstW[i], dstH[j], flags[k],
280 NULL);
716d413c 281 if (dstFormat_in != AV_PIX_FMT_NONE)
9b4bf9c2 282 break;
91cfeac6 283 }
716d413c 284 if (srcFormat_in != AV_PIX_FMT_NONE)
9b4bf9c2 285 break;
91cfeac6 286 }
c9b99ea6
MN
287}
288
9b4bf9c2 289static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
716d413c
AK
290 enum AVPixelFormat srcFormat_in,
291 enum AVPixelFormat dstFormat_in)
4bf44785
RP
292{
293 char buf[256];
294
295 while (fgets(buf, sizeof(buf), fp)) {
296 struct Results r;
716d413c 297 enum AVPixelFormat srcFormat;
4bf44785
RP
298 char srcStr[12];
299 int srcW, srcH;
716d413c 300 enum AVPixelFormat dstFormat;
4bf44785
RP
301 char dstStr[12];
302 int dstW, dstH;
303 int flags;
304 int ret;
305
999484c9
DB
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);
4bf44785
RP
311 if (ret != 12) {
312 srcStr[0] = dstStr[0] = 0;
999484c9 313 ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
4bf44785
RP
314 }
315
316 srcFormat = av_get_pix_fmt(srcStr);
317 dstFormat = av_get_pix_fmt(dstStr);
318
716d413c 319 if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE) {
4bf44785
RP
320 fprintf(stderr, "malformed input file\n");
321 return -1;
322 }
716d413c
AK
323 if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
324 (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
9b4bf9c2 325 continue;
4bf44785
RP
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
c9b99ea6
MN
340#define W 96
341#define H 96
342
dd68318c
RP
343int main(int argc, char **argv)
344{
716d413c
AK
345 enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
346 enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
999484c9 347 uint8_t *rgb_data = av_malloc(W * H * 4);
06b0246d
RB
348 uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
349 int rgb_stride[4] = { 4 * W, 0, 0, 0 };
999484c9
DB
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 };
221b804f
DB
353 int x, y;
354 struct SwsContext *sws;
9a034dde 355 AVLFG rand;
4bf44785
RP
356 int res = -1;
357 int i;
221b804f 358
7248797c
RP
359 if (!rgb_data || !data)
360 return -1;
361
716d413c
AK
362 sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
363 AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
221b804f 364
9a034dde
RP
365 av_lfg_init(&rand, 1);
366
999484c9
DB
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);
e5091488 370 sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
64d39b7c 371 sws_freeContext(sws);
51d83986 372 av_free(rgb_data);
27161c06 373
4bf44785 374 for (i = 1; i < argc; i += 2) {
999484c9 375 if (argv[i][0] != '-' || i + 1 == argc)
4bf44785
RP
376 goto bad_option;
377 if (!strcmp(argv[i], "-ref")) {
999484c9 378 FILE *fp = fopen(argv[i + 1], "r");
4bf44785 379 if (!fp) {
999484c9 380 fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
4bf44785
RP
381 goto error;
382 }
9b4bf9c2 383 res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
4bf44785
RP
384 fclose(fp);
385 goto end;
9b4bf9c2 386 } else if (!strcmp(argv[i], "-src")) {
999484c9 387 srcFormat = av_get_pix_fmt(argv[i + 1]);
716d413c 388 if (srcFormat == AV_PIX_FMT_NONE) {
999484c9 389 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
9b4bf9c2
RP
390 return -1;
391 }
392 } else if (!strcmp(argv[i], "-dst")) {
999484c9 393 dstFormat = av_get_pix_fmt(argv[i + 1]);
716d413c 394 if (dstFormat == AV_PIX_FMT_NONE) {
999484c9 395 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
9b4bf9c2
RP
396 return -1;
397 }
4bf44785
RP
398 } else {
399bad_option:
400 fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
401 goto error;
402 }
403 }
404
9b4bf9c2 405 selfTest(src, stride, W, H, srcFormat, dstFormat);
4bf44785
RP
406end:
407 res = 0;
408error:
51d83986 409 av_free(data);
516b1f82 410
4bf44785 411 return res;
c9b99ea6 412}