]> git.sesse.net Git - ffmpeg/blob - libavcodec/motion-test.c
Add some explanatory comments to #endif directives.
[ffmpeg] / libavcodec / motion-test.c
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 motion_test.c
23  * motion test.
24  */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31
32 #include "dsputil.h"
33
34 #include "i386/mmx.h"
35
36 #undef exit
37 #undef printf
38
39 #define WIDTH 64
40 #define HEIGHT 64
41
42 uint8_t img1[WIDTH * HEIGHT];
43 uint8_t img2[WIDTH * HEIGHT];
44
45 void fill_random(uint8_t *tab, int size)
46 {
47     int i;
48     for(i=0;i<size;i++) {
49 #if 1
50         tab[i] = random() % 256;
51 #else
52         tab[i] = i;
53 #endif
54     }
55 }
56
57 void help(void)
58 {
59     printf("motion-test [-h]\n"
60            "test motion implementations\n");
61     exit(1);
62 }
63
64 int64_t gettime(void)
65 {
66     struct timeval tv;
67     gettimeofday(&tv,NULL);
68     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
69 }
70
71 #define NB_ITS 500
72
73 int dummy;
74
75 void test_motion(const char *name,
76                  me_cmp_func test_func, me_cmp_func ref_func)
77 {
78     int x, y, d1, d2, it;
79     uint8_t *ptr;
80     int64_t ti;
81     printf("testing '%s'\n", name);
82
83     /* test correctness */
84     for(it=0;it<20;it++) {
85
86         fill_random(img1, WIDTH * HEIGHT);
87         fill_random(img2, WIDTH * HEIGHT);
88
89         for(y=0;y<HEIGHT-17;y++) {
90             for(x=0;x<WIDTH-17;x++) {
91                 ptr = img2 + y * WIDTH + x;
92                 d1 = test_func(NULL, img1, ptr, WIDTH, 1);
93                 d2 = ref_func(NULL, img1, ptr, WIDTH, 1);
94                 if (d1 != d2) {
95                     printf("error: mmx=%d c=%d\n", d1, d2);
96                 }
97             }
98         }
99     }
100     emms_c();
101
102     /* speed test */
103     ti = gettime();
104     d1 = 0;
105     for(it=0;it<NB_ITS;it++) {
106         for(y=0;y<HEIGHT-17;y++) {
107             for(x=0;x<WIDTH-17;x++) {
108                 ptr = img2 + y * WIDTH + x;
109                 d1 += test_func(NULL, img1, ptr, WIDTH, 1);
110             }
111         }
112     }
113     emms_c();
114     dummy = d1; /* avoid optimisation */
115     ti = gettime() - ti;
116
117     printf("  %0.0f kop/s\n",
118            (double)NB_ITS * (WIDTH - 16) * (HEIGHT - 16) /
119            (double)(ti / 1000.0));
120 }
121
122
123 int main(int argc, char **argv)
124 {
125     AVCodecContext *ctx;
126     int c;
127     DSPContext cctx, mmxctx;
128     int flags[2] = { FF_MM_MMX, FF_MM_MMXEXT };
129
130     for(;;) {
131         c = getopt(argc, argv, "h");
132         if (c == -1)
133             break;
134         switch(c) {
135         case 'h':
136             help();
137             break;
138         }
139     }
140
141     printf("ffmpeg motion test\n");
142
143     ctx = avcodec_alloc_context();
144     ctx->dsp_mask = FF_MM_FORCE;
145     dsputil_init(&cctx, ctx);
146     for (c = 0; c < 2; c++) {
147         int x;
148         ctx->dsp_mask = FF_MM_FORCE | flags[c];
149         dsputil_init(&mmxctx, ctx);
150
151         for (x = 0; x < 2; x++) {
152             printf("%s for %dx%d pixels\n", c ? "mmx2" : "mmx",
153                    x ? 8 : 16, x ? 8 : 16);
154             test_motion("mmx",     mmxctx.pix_abs[x][0], cctx.pix_abs[x][0]);
155             test_motion("mmx_x2",  mmxctx.pix_abs[x][1], cctx.pix_abs[x][1]);
156             test_motion("mmx_y2",  mmxctx.pix_abs[x][2], cctx.pix_abs[x][2]);
157             test_motion("mmx_xy2", mmxctx.pix_abs[x][3], cctx.pix_abs[x][3]);
158         }
159     }
160     av_free(ctx);
161
162     return 0;
163 }