~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libswscale/swscale-test.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
/* HACK Duplicated from swscale_internal.h.
37
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
 
    )
 
38
#define isGray(x)                      \
 
39
    ((x) == PIX_FMT_GRAY8       ||     \
 
40
     (x) == PIX_FMT_Y400A       ||     \
 
41
     (x) == PIX_FMT_GRAY16BE    ||     \
 
42
     (x) == PIX_FMT_GRAY16LE)
 
43
#define hasChroma(x)                   \
 
44
    (!(isGray(x)                ||     \
 
45
       (x) == PIX_FMT_MONOBLACK ||     \
 
46
       (x) == PIX_FMT_MONOWHITE))
 
47
#define isALPHA(x)                     \
 
48
    ((x) == PIX_FMT_BGR32   ||         \
 
49
     (x) == PIX_FMT_BGR32_1 ||         \
 
50
     (x) == PIX_FMT_RGB32   ||         \
 
51
     (x) == PIX_FMT_RGB32_1 ||         \
 
52
     (x) == PIX_FMT_YUVA420P)
55
53
 
56
 
static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
 
54
static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1,
 
55
                       int stride2, int w, int h)
57
56
{
58
 
    int x,y;
59
 
    uint64_t ssd=0;
 
57
    int x, y;
 
58
    uint64_t ssd = 0;
60
59
 
61
 
    for (y=0; y<h; y++) {
62
 
        for (x=0; x<w; x++) {
63
 
            int d= src1[x + y*stride1] - src2[x + y*stride2];
64
 
            ssd+= d*d;
 
60
    for (y = 0; y < h; y++) {
 
61
        for (x = 0; x < w; x++) {
 
62
            int d = src1[x + y * stride1] - src2[x + y * stride2];
 
63
            ssd += d * d;
65
64
        }
66
65
    }
67
66
    return ssd;
86
85
    static int cur_srcW, cur_srcH;
87
86
    static uint8_t *src[4];
88
87
    static int srcStride[4];
89
 
    uint8_t *dst[4] = {0};
90
 
    uint8_t *out[4] = {0};
 
88
    uint8_t *dst[4] = { 0 };
 
89
    uint8_t *out[4] = { 0 };
91
90
    int dstStride[4];
92
91
    int i;
93
 
    uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
 
92
    uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
94
93
    struct SwsContext *dstContext = NULL, *outContext = NULL;
95
94
    uint32_t crc = 0;
96
 
    int res = 0;
 
95
    int res      = 0;
97
96
 
98
97
    if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
99
98
        struct SwsContext *srcContext = NULL;
105
104
        av_image_fill_linesizes(srcStride, srcFormat, srcW);
106
105
        for (p = 0; p < 4; p++) {
107
106
            if (srcStride[p])
108
 
                src[p] = av_mallocz(srcStride[p]*srcH+16);
 
107
                src[p] = av_mallocz(srcStride[p] * srcH + 16);
109
108
            if (srcStride[p] && !src[p]) {
110
109
                perror("Malloc");
111
110
                res = -1;
112
 
 
113
111
                goto end;
114
112
            }
115
113
        }
120
118
                    av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name,
121
119
                    av_pix_fmt_descriptors[srcFormat].name);
122
120
            res = -1;
123
 
 
124
121
            goto end;
125
122
        }
126
123
        sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
127
124
        sws_freeContext(srcContext);
128
125
 
129
126
        cur_srcFormat = srcFormat;
130
 
        cur_srcW = srcW;
131
 
        cur_srcH = srcH;
 
127
        cur_srcW      = srcW;
 
128
        cur_srcH      = srcH;
132
129
    }
133
130
 
134
131
    av_image_fill_linesizes(dstStride, dstFormat, dstW);
135
 
    for (i=0; i<4; i++) {
 
132
    for (i = 0; i < 4; i++) {
136
133
        /* Image buffers passed into libswscale can be allocated any way you
137
134
         * prefer, as long as they're aligned enough for the architecture, and
138
135
         * they're freed appropriately (such as using av_free for buffers
140
137
        /* An extra 16 bytes is being allocated because some scalers may write
141
138
         * out of bounds. */
142
139
        if (dstStride[i])
143
 
            dst[i]= av_mallocz(dstStride[i]*dstH+16);
 
140
            dst[i] = av_mallocz(dstStride[i] * dstH + 16);
144
141
        if (dstStride[i] && !dst[i]) {
145
142
            perror("Malloc");
146
143
            res = -1;
149
146
        }
150
147
    }
151
148
 
152
 
    dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
 
149
    dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
 
150
                                flags, NULL, NULL, NULL);
153
151
    if (!dstContext) {
154
152
        fprintf(stderr, "Failed to get %s ---> %s\n",
155
153
                av_pix_fmt_descriptors[srcFormat].name,
156
154
                av_pix_fmt_descriptors[dstFormat].name);
157
155
        res = -1;
158
 
 
159
156
        goto end;
160
157
    }
161
158
 
167
164
 
168
165
    sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
169
166
 
170
 
    for (i = 0; i < 4 && dstStride[i]; i++) {
171
 
        crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i], dstStride[i] * dstH);
172
 
    }
 
167
    for (i = 0; i < 4 && dstStride[i]; i++)
 
168
        crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
 
169
                     dstStride[i] * dstH);
173
170
 
174
171
    if (r && crc == r->crc) {
175
172
        ssdY = r->ssdY;
177
174
        ssdV = r->ssdV;
178
175
        ssdA = r->ssdA;
179
176
    } else {
180
 
        for (i=0; i<4; i++) {
 
177
        for (i = 0; i < 4; i++) {
181
178
            if (refStride[i])
182
 
                out[i]= av_mallocz(refStride[i]*h);
 
179
                out[i] = av_mallocz(refStride[i] * h);
183
180
            if (refStride[i] && !out[i]) {
184
181
                perror("Malloc");
185
182
                res = -1;
186
 
 
187
183
                goto end;
188
184
            }
189
185
        }
190
 
        outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
 
186
        outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
 
187
                                    PIX_FMT_YUVA420P, SWS_BILINEAR,
 
188
                                    NULL, NULL, NULL);
191
189
        if (!outContext) {
192
190
            fprintf(stderr, "Failed to get %s ---> %s\n",
193
191
                    av_pix_fmt_descriptors[dstFormat].name,
194
192
                    av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name);
195
193
            res = -1;
196
 
 
197
194
            goto end;
198
195
        }
199
196
        sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
200
197
 
201
 
        ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
 
198
        ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
202
199
        if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
203
200
            //FIXME check that output is really gray
204
 
            ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
205
 
            ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
 
201
            ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
 
202
                          (w + 1) >> 1, (h + 1) >> 1);
 
203
            ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
 
204
                          (w + 1) >> 1, (h + 1) >> 1);
206
205
        }
207
206
        if (isALPHA(srcFormat) && isALPHA(dstFormat))
208
 
            ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
 
207
            ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
209
208
 
210
 
        ssdY/= w*h;
211
 
        ssdU/= w*h/4;
212
 
        ssdV/= w*h/4;
213
 
        ssdA/= w*h;
 
209
        ssdY /= w * h;
 
210
        ssdU /= w * h / 4;
 
211
        ssdV /= w * h / 4;
 
212
        ssdA /= w * h;
214
213
 
215
214
        sws_freeContext(outContext);
216
215
 
217
 
        for (i=0; i<4; i++) {
 
216
        for (i = 0; i < 4; i++)
218
217
            if (refStride[i])
219
218
                av_free(out[i]);
220
 
        }
221
219
    }
222
220
 
223
 
    printf(" CRC=%08x SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
 
221
    printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
224
222
           crc, ssdY, ssdU, ssdV, ssdA);
225
223
 
226
224
end:
227
 
 
228
225
    sws_freeContext(dstContext);
229
226
 
230
 
    for (i=0; i<4; i++) {
 
227
    for (i = 0; i < 4; i++)
231
228
        if (dstStride[i])
232
229
            av_free(dst[i]);
233
 
    }
234
230
 
235
231
    return res;
236
232
}
239
235
                     enum PixelFormat srcFormat_in,
240
236
                     enum PixelFormat dstFormat_in)
241
237
{
242
 
    const int flags[] = { SWS_FAST_BILINEAR,
243
 
                          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 };
 
238
    const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
 
239
                          SWS_X, SWS_POINT, SWS_AREA, 0 };
 
240
    const int srcW   = w;
 
241
    const int srcH   = h;
 
242
    const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
 
243
    const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
249
244
    enum PixelFormat srcFormat, dstFormat;
250
245
 
251
246
    for (srcFormat = srcFormat_in != PIX_FMT_NONE ? srcFormat_in : 0;
252
247
         srcFormat < PIX_FMT_NB; srcFormat++) {
253
 
        if (!sws_isSupportedInput(srcFormat) || !sws_isSupportedOutput(srcFormat))
 
248
        if (!sws_isSupportedInput(srcFormat) ||
 
249
            !sws_isSupportedOutput(srcFormat))
254
250
            continue;
255
251
 
256
252
        for (dstFormat = dstFormat_in != PIX_FMT_NONE ? dstFormat_in : 0;
258
254
            int i, j, k;
259
255
            int res = 0;
260
256
 
261
 
            if (!sws_isSupportedInput(dstFormat) || !sws_isSupportedOutput(dstFormat))
 
257
            if (!sws_isSupportedInput(dstFormat) ||
 
258
                !sws_isSupportedOutput(dstFormat))
262
259
                continue;
263
260
 
264
261
            printf("%s -> %s\n",
266
263
                   av_pix_fmt_descriptors[dstFormat].name);
267
264
            fflush(stdout);
268
265
 
269
 
            for (k = 0; flags[k] && !res; k++) {
 
266
            for (k = 0; flags[k] && !res; k++)
270
267
                for (i = 0; dstW[i] && !res; i++)
271
268
                    for (j = 0; dstH[j] && !res; j++)
272
269
                        res = doTest(ref, refStride, w, h,
273
270
                                     srcFormat, dstFormat,
274
271
                                     srcW, srcH, dstW[i], dstH[j], flags[k],
275
272
                                     NULL);
276
 
            }
277
273
            if (dstFormat_in != PIX_FMT_NONE)
278
274
                break;
279
275
        }
299
295
        int flags;
300
296
        int ret;
301
297
 
302
 
        ret = sscanf(buf, " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
303
 
                          " SSD=%"PRId64", %"PRId64", %"PRId64", %"PRId64"\n",
304
 
                          srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
305
 
                          &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
 
298
        ret = sscanf(buf,
 
299
                     " %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);
306
303
        if (ret != 12) {
307
304
            srcStr[0] = dstStr[0] = 0;
308
 
            ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
 
305
            ret       = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
309
306
        }
310
307
 
311
308
        srcFormat = av_get_pix_fmt(srcStr);
339
336
{
340
337
    enum PixelFormat srcFormat = PIX_FMT_NONE;
341
338
    enum PixelFormat dstFormat = PIX_FMT_NONE;
342
 
    uint8_t *rgb_data = av_malloc (W*H*4);
343
 
    uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
344
 
    int rgb_stride[3]={4*W, 0, 0};
345
 
    uint8_t *data = av_malloc (4*W*H);
346
 
    uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
347
 
    int stride[4]={W, W, W, W};
 
339
    uint8_t *rgb_data   = av_malloc(W * H * 4);
 
340
    uint8_t *rgb_src[3] = { rgb_data, NULL, NULL };
 
341
    int rgb_stride[3]   = { 4 * W, 0, 0 };
 
342
    uint8_t *data       = av_malloc(4 * W * H);
 
343
    uint8_t *src[4]     = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
 
344
    int stride[4]       = { W, W, W, W };
348
345
    int x, y;
349
346
    struct SwsContext *sws;
350
347
    AVLFG rand;
354
351
    if (!rgb_data || !data)
355
352
        return -1;
356
353
 
357
 
    sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
 
354
    sws = sws_getContext(W / 12, H / 12, PIX_FMT_RGB32, W, H,
 
355
                         PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
358
356
 
359
357
    av_lfg_init(&rand, 1);
360
358
 
361
 
    for (y=0; y<H; y++) {
362
 
        for (x=0; x<W*4; x++) {
363
 
            rgb_data[ x + y*4*W]= av_lfg_get(&rand);
364
 
        }
365
 
    }
 
359
    for (y = 0; y < H; y++)
 
360
        for (x = 0; x < W * 4; x++)
 
361
            rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
366
362
    sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
367
363
    sws_freeContext(sws);
368
364
    av_free(rgb_data);
369
365
 
370
366
    for (i = 1; i < argc; i += 2) {
371
 
        if (argv[i][0] != '-' || i+1 == argc)
 
367
        if (argv[i][0] != '-' || i + 1 == argc)
372
368
            goto bad_option;
373
369
        if (!strcmp(argv[i], "-ref")) {
374
 
            FILE *fp = fopen(argv[i+1], "r");
 
370
            FILE *fp = fopen(argv[i + 1], "r");
375
371
            if (!fp) {
376
 
                fprintf(stderr, "could not open '%s'\n", argv[i+1]);
 
372
                fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
377
373
                goto error;
378
374
            }
379
375
            res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
380
376
            fclose(fp);
381
377
            goto end;
382
378
        } else if (!strcmp(argv[i], "-src")) {
383
 
            srcFormat = av_get_pix_fmt(argv[i+1]);
 
379
            srcFormat = av_get_pix_fmt(argv[i + 1]);
384
380
            if (srcFormat == PIX_FMT_NONE) {
385
 
                fprintf(stderr, "invalid pixel format %s\n", argv[i+1]);
 
381
                fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
386
382
                return -1;
387
383
            }
388
384
        } else if (!strcmp(argv[i], "-dst")) {
389
 
            dstFormat = av_get_pix_fmt(argv[i+1]);
 
385
            dstFormat = av_get_pix_fmt(argv[i + 1]);
390
386
            if (dstFormat == PIX_FMT_NONE) {
391
 
                fprintf(stderr, "invalid pixel format %s\n", argv[i+1]);
 
387
                fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
392
388
                return -1;
393
389
            }
394
390
        } else {