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