1/*
2 * (c) 2001 Fabrice Bellard
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/**
22 * @file
23 * motion test.
24 */
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include "config.h"
31#include "libavcodec/me_cmp.h"
32#include "libavutil/cpu.h"
33#include "libavutil/internal.h"
34#include "libavutil/lfg.h"
35#include "libavutil/mem.h"
36#include "libavutil/time.h"
37
38#undef printf
39
40#define WIDTH 64
41#define HEIGHT 64
42
43static uint8_t img1[WIDTH * HEIGHT];
44static uint8_t img2[WIDTH * HEIGHT];
45
46static void fill_random(uint8_t *tab, int size)
47{
48    int i;
49    AVLFG prng;
50
51    av_lfg_init(&prng, 1);
52    for(i=0;i<size;i++) {
53        tab[i] = av_lfg_get(&prng) % 256;
54    }
55}
56
57static void help(void)
58{
59    printf("motion-test [-h]\n"
60           "test motion implementations\n");
61}
62
63#define NB_ITS 500
64
65int dummy;
66
67static void test_motion(const char *name,
68                 me_cmp_func test_func, me_cmp_func ref_func)
69{
70    int x, y, d1, d2, it;
71    uint8_t *ptr;
72    int64_t ti;
73    printf("testing '%s'\n", name);
74
75    /* test correctness */
76    for(it=0;it<20;it++) {
77
78        fill_random(img1, WIDTH * HEIGHT);
79        fill_random(img2, WIDTH * HEIGHT);
80
81        for(y=0;y<HEIGHT-17;y++) {
82            for(x=0;x<WIDTH-17;x++) {
83                ptr = img2 + y * WIDTH + x;
84                d1 = test_func(NULL, img1, ptr, WIDTH, 8);
85                d2 = ref_func(NULL, img1, ptr, WIDTH, 8);
86                if (d1 != d2) {
87                    printf("error: mmx=%d c=%d\n", d1, d2);
88                }
89            }
90        }
91    }
92    emms_c();
93
94    /* speed test */
95    ti = av_gettime_relative();
96    d1 = 0;
97    for(it=0;it<NB_ITS;it++) {
98        for(y=0;y<HEIGHT-17;y++) {
99            for(x=0;x<WIDTH-17;x++) {
100                ptr = img2 + y * WIDTH + x;
101                d1 += test_func(NULL, img1, ptr, WIDTH, 8);
102            }
103        }
104    }
105    emms_c();
106    dummy = d1; /* avoid optimization */
107    ti = av_gettime_relative() - ti;
108
109    printf("  %0.0f kop/s\n",
110           (double)NB_ITS * (WIDTH - 16) * (HEIGHT - 16) /
111           (double)(ti / 1000.0));
112}
113
114
115int main(int argc, char **argv)
116{
117    AVCodecContext *ctx;
118    int c;
119    MECmpContext cctx, mmxctx;
120    int flags[2] = { AV_CPU_FLAG_MMX, AV_CPU_FLAG_MMXEXT };
121    int flags_size = HAVE_MMXEXT ? 2 : 1;
122
123    if (argc > 1) {
124        help();
125        return 1;
126    }
127
128    printf("ffmpeg motion test\n");
129
130    ctx = avcodec_alloc_context3(NULL);
131    ctx->flags |= AV_CODEC_FLAG_BITEXACT;
132    av_force_cpu_flags(0);
133    memset(&cctx, 0, sizeof(cctx));
134    ff_me_cmp_init(&cctx, ctx);
135    for (c = 0; c < flags_size; c++) {
136        int x;
137        av_force_cpu_flags(flags[c]);
138        memset(&mmxctx, 0, sizeof(mmxctx));
139        ff_me_cmp_init(&mmxctx, ctx);
140
141        for (x = 0; x < 2; x++) {
142            printf("%s for %dx%d pixels\n", c ? "mmx2" : "mmx",
143                   x ? 8 : 16, x ? 8 : 16);
144            test_motion("mmx",     mmxctx.pix_abs[x][0], cctx.pix_abs[x][0]);
145            test_motion("mmx_x2",  mmxctx.pix_abs[x][1], cctx.pix_abs[x][1]);
146            test_motion("mmx_y2",  mmxctx.pix_abs[x][2], cctx.pix_abs[x][2]);
147            test_motion("mmx_xy2", mmxctx.pix_abs[x][3], cctx.pix_abs[x][3]);
148        }
149    }
150    av_free(ctx);
151
152    return 0;
153}
154