Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale-test.c @ 9075bc7c

History | View | Annotate | Download (11.7 KB)

1
/*
2
 * Copyright (C) 2003 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 "libavcore/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
#include "swscale.h"
35

    
36
/* HACK Duplicated from swscale_internal.h.
37
 * Should be removed when a cleaner pixel format system exists. */
38
#define isGray(x)       (           \
39
           (x)==PIX_FMT_GRAY8       \
40
        || (x)==PIX_FMT_GRAY16BE    \
41
        || (x)==PIX_FMT_GRAY16LE    \
42
    )
43
#define hasChroma(x)   (!(          \
44
            isGray(x)               \
45
        || (x)==PIX_FMT_MONOBLACK   \
46
        || (x)==PIX_FMT_MONOWHITE   \
47
    ))
48
#define isALPHA(x)      (           \
49
           (x)==PIX_FMT_BGR32       \
50
        || (x)==PIX_FMT_BGR32_1     \
51
        || (x)==PIX_FMT_RGB32       \
52
        || (x)==PIX_FMT_RGB32_1     \
53
        || (x)==PIX_FMT_YUVA420P    \
54
    )
55

    
56
static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
57
{
58
    int x,y;
59
    uint64_t ssd=0;
60

    
61
//printf("%d %d\n", w, h);
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
//printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
68
        }
69
//printf("\n");
70
    }
71
    return ssd;
72
}
73

    
74
struct Results {
75
    uint64_t ssdY;
76
    uint64_t ssdU;
77
    uint64_t ssdV;
78
    uint64_t ssdA;
79
    uint32_t crc;
80
};
81

    
82
// test by ref -> src -> dst -> out & compare out against ref
83
// ref & out are YV12
84
static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
85
                  enum PixelFormat srcFormat, enum PixelFormat dstFormat,
86
                  int srcW, int srcH, int dstW, int dstH, int flags,
87
                  struct Results *r)
88
{
89
    static enum PixelFormat cur_srcFormat;
90
    static int cur_srcW, cur_srcH;
91
    static uint8_t *src[4];
92
    static int srcStride[4];
93
    uint8_t *dst[4] = {0};
94
    uint8_t *out[4] = {0};
95
    int dstStride[4];
96
    int i;
97
    uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
98
    struct SwsContext *dstContext = NULL, *outContext = NULL;
99
    uint32_t crc = 0;
100
    int res = 0;
101

    
102
    if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
103
        struct SwsContext *srcContext = NULL;
104
        int p;
105

    
106
        for (p = 0; p < 4; p++)
107
            if (src[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

    
118
                goto end;
119
            }
120
        }
121
        srcContext = sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH,
122
                                    srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
123
        if (!srcContext) {
124
            fprintf(stderr, "Failed to get %s ---> %s\n",
125
                    av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name,
126
                    av_pix_fmt_descriptors[srcFormat].name);
127
            res = -1;
128

    
129
            goto end;
130
        }
131
        sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
132
        sws_freeContext(srcContext);
133

    
134
        cur_srcFormat = srcFormat;
135
        cur_srcW = srcW;
136
        cur_srcH = srcH;
137
    }
138

    
139
    av_image_fill_linesizes(dstStride, dstFormat, dstW);
140
    for (i=0; i<4; i++) {
141
        /* Image buffers passed into libswscale can be allocated any way you
142
         * prefer, as long as they're aligned enough for the architecture, and
143
         * they're freed appropriately (such as using av_free for buffers
144
         * allocated with av_malloc). */
145
        /* An extra 16 bytes is being allocated because some scalers may write
146
         * out of bounds. */
147
        if (dstStride[i])
148
            dst[i]= av_mallocz(dstStride[i]*dstH+16);
149
        if (dstStride[i] && !dst[i]) {
150
            perror("Malloc");
151
            res = -1;
152

    
153
            goto end;
154
        }
155
    }
156

    
157
    dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
158
    if (!dstContext) {
159
        fprintf(stderr, "Failed to get %s ---> %s\n",
160
                av_pix_fmt_descriptors[srcFormat].name,
161
                av_pix_fmt_descriptors[dstFormat].name);
162
        res = -1;
163

    
164
        goto end;
165
    }
166
//    printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
167
//        (int)src[0], (int)src[1], (int)src[2]);
168

    
169
    printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
170
           av_pix_fmt_descriptors[srcFormat].name, srcW, srcH,
171
           av_pix_fmt_descriptors[dstFormat].name, dstW, dstH,
172
           flags);
173
    fflush(stdout);
174

    
175
    sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
176

    
177
    for (i = 0; i < 4 && dstStride[i]; i++) {
178
        crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i], dstStride[i] * dstH);
179
    }
180

    
181
    if (r && crc == r->crc) {
182
        ssdY = r->ssdY;
183
        ssdU = r->ssdU;
184
        ssdV = r->ssdV;
185
        ssdA = r->ssdA;
186
    } else {
187
        for (i=0; i<4; i++) {
188
            if (refStride[i])
189
                out[i]= av_mallocz(refStride[i]*h);
190
            if (refStride[i] && !out[i]) {
191
                perror("Malloc");
192
                res = -1;
193

    
194
                goto end;
195
            }
196
        }
197
        outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, flags, NULL, NULL, NULL);
198
        if (!outContext) {
199
            fprintf(stderr, "Failed to get %s ---> %s\n",
200
                    av_pix_fmt_descriptors[dstFormat].name,
201
                    av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name);
202
            res = -1;
203

    
204
            goto end;
205
        }
206
        sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
207

    
208
        ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
209
        if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
210
            //FIXME check that output is really gray
211
            ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
212
            ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
213
        }
214
        if (isALPHA(srcFormat) && isALPHA(dstFormat))
215
            ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
216

    
217
        ssdY/= w*h;
218
        ssdU/= w*h/4;
219
        ssdV/= w*h/4;
220
        ssdA/= w*h;
221

    
222
        sws_freeContext(outContext);
223

    
224
        for (i=0; i<4; i++) {
225
            if (refStride[i])
226
                av_free(out[i]);
227
        }
228
    }
229

    
230
    printf(" CRC=%08x SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
231
           crc, ssdY, ssdU, ssdV, ssdA);
232

    
233
end:
234

    
235
    sws_freeContext(dstContext);
236

    
237
    for (i=0; i<4; i++) {
238
        if (dstStride[i])
239
            av_free(dst[i]);
240
    }
241

    
242
    return res;
243
}
244

    
245
static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h)
246
{
247
    const int flags[] = { SWS_FAST_BILINEAR,
248
                          SWS_BILINEAR, SWS_BICUBIC,
249
                          SWS_X       , SWS_POINT  , SWS_AREA, 0 };
250
    const int srcW = w;
251
    const int srcH = h;
252
    const int dstW[] = { srcW - srcW/3, srcW, srcW + srcW/3, 0 };
253
    const int dstH[] = { srcH - srcH/3, srcH, srcH + srcH/3, 0 };
254
    enum PixelFormat srcFormat, dstFormat;
255

    
256
    for (srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
257
        if (!sws_isSupportedInput(srcFormat) || !sws_isSupportedOutput(srcFormat))
258
            continue;
259

    
260
        for (dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
261
            int i, j, k;
262
            int res = 0;
263

    
264
            if (!sws_isSupportedInput(dstFormat) || !sws_isSupportedOutput(dstFormat))
265
                continue;
266

    
267
            printf("%s -> %s\n",
268
                   av_pix_fmt_descriptors[srcFormat].name,
269
                   av_pix_fmt_descriptors[dstFormat].name);
270
            fflush(stdout);
271

    
272
            for (k = 0; flags[k] && !res; k++) {
273
                for (i = 0; dstW[i] && !res; i++)
274
                    for (j = 0; dstH[j] && !res; j++)
275
                        res = doTest(ref, refStride, w, h,
276
                                     srcFormat, dstFormat,
277
                                     srcW, srcH, dstW[i], dstH[j], flags[k],
278
                                     NULL);
279
            }
280
        }
281
    }
282
}
283

    
284
static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp)
285
{
286
    char buf[256];
287

    
288
    while (fgets(buf, sizeof(buf), fp)) {
289
        struct Results r;
290
        enum PixelFormat srcFormat;
291
        char srcStr[12];
292
        int srcW, srcH;
293
        enum PixelFormat dstFormat;
294
        char dstStr[12];
295
        int dstW, dstH;
296
        int flags;
297
        int ret;
298

    
299
        ret = sscanf(buf, " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
300
                          " SSD=%"PRId64", %"PRId64", %"PRId64", %"PRId64"\n",
301
                          srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
302
                          &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
303
        if (ret != 12) {
304
            srcStr[0] = dstStr[0] = 0;
305
            ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
306
        }
307

    
308
        srcFormat = av_get_pix_fmt(srcStr);
309
        dstFormat = av_get_pix_fmt(dstStr);
310

    
311
        if (srcFormat == PIX_FMT_NONE || dstFormat == PIX_FMT_NONE) {
312
            fprintf(stderr, "malformed input file\n");
313
            return -1;
314
        }
315
        if (ret != 12) {
316
            printf("%s", buf);
317
            continue;
318
        }
319

    
320
        doTest(ref, refStride, w, h,
321
               srcFormat, dstFormat,
322
               srcW, srcH, dstW, dstH, flags,
323
               &r);
324
    }
325

    
326
    return 0;
327
}
328

    
329
#define W 96
330
#define H 96
331

    
332
int main(int argc, char **argv)
333
{
334
    uint8_t *rgb_data = av_malloc (W*H*4);
335
    uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
336
    int rgb_stride[3]={4*W, 0, 0};
337
    uint8_t *data = av_malloc (4*W*H);
338
    uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
339
    int stride[4]={W, W, W, W};
340
    int x, y;
341
    struct SwsContext *sws;
342
    AVLFG rand;
343
    int res = -1;
344
    int i;
345

    
346
    if (!rgb_data || !data)
347
        return -1;
348

    
349
    sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
350

    
351
    av_lfg_init(&rand, 1);
352

    
353
    for (y=0; y<H; y++) {
354
        for (x=0; x<W*4; x++) {
355
            rgb_data[ x + y*4*W]= av_lfg_get(&rand);
356
        }
357
    }
358
    sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
359
    sws_freeContext(sws);
360
    av_free(rgb_data);
361

    
362
    for (i = 1; i < argc; i += 2) {
363
        if (argv[i][0] != '-' || i+1 == argc)
364
            goto bad_option;
365
        if (!strcmp(argv[i], "-ref")) {
366
            FILE *fp = fopen(argv[i+1], "r");
367
            if (!fp) {
368
                fprintf(stderr, "could not open '%s'\n", argv[i+1]);
369
                goto error;
370
            }
371
            res = fileTest(src, stride, W, H, fp);
372
            fclose(fp);
373
            goto end;
374
        } else {
375
bad_option:
376
            fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
377
            goto error;
378
        }
379
    }
380

    
381
    selfTest(src, stride, W, H);
382
end:
383
    res = 0;
384
error:
385
    av_free(data);
386

    
387
    return res;
388
}