libavcodec/motion-test.c
Go to the documentation of this file.
00001 /*
00002  * (c) 2001 Fabrice Bellard
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030 #include <unistd.h>
00031 
00032 #include "config.h"
00033 #include "dsputil.h"
00034 #include "libavutil/lfg.h"
00035 
00036 #undef printf
00037 
00038 #define WIDTH 64
00039 #define HEIGHT 64
00040 
00041 static uint8_t img1[WIDTH * HEIGHT];
00042 static uint8_t img2[WIDTH * HEIGHT];
00043 
00044 static void fill_random(uint8_t *tab, int size)
00045 {
00046     int i;
00047     AVLFG prng;
00048 
00049     av_lfg_init(&prng, 1);
00050     for(i=0;i<size;i++) {
00051 #if 1
00052         tab[i] = av_lfg_get(&prng) % 256;
00053 #else
00054         tab[i] = i;
00055 #endif
00056     }
00057 }
00058 
00059 static void help(void)
00060 {
00061     printf("motion-test [-h]\n"
00062            "test motion implementations\n");
00063 }
00064 
00065 static int64_t gettime(void)
00066 {
00067     struct timeval tv;
00068     gettimeofday(&tv,NULL);
00069     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
00070 }
00071 
00072 #define NB_ITS 500
00073 
00074 int dummy;
00075 
00076 static void test_motion(const char *name,
00077                  me_cmp_func test_func, me_cmp_func ref_func)
00078 {
00079     int x, y, d1, d2, it;
00080     uint8_t *ptr;
00081     int64_t ti;
00082     printf("testing '%s'\n", name);
00083 
00084     /* test correctness */
00085     for(it=0;it<20;it++) {
00086 
00087         fill_random(img1, WIDTH * HEIGHT);
00088         fill_random(img2, WIDTH * HEIGHT);
00089 
00090         for(y=0;y<HEIGHT-17;y++) {
00091             for(x=0;x<WIDTH-17;x++) {
00092                 ptr = img2 + y * WIDTH + x;
00093                 d1 = test_func(NULL, img1, ptr, WIDTH, 1);
00094                 d2 = ref_func(NULL, img1, ptr, WIDTH, 1);
00095                 if (d1 != d2) {
00096                     printf("error: mmx=%d c=%d\n", d1, d2);
00097                 }
00098             }
00099         }
00100     }
00101     emms_c();
00102 
00103     /* speed test */
00104     ti = gettime();
00105     d1 = 0;
00106     for(it=0;it<NB_ITS;it++) {
00107         for(y=0;y<HEIGHT-17;y++) {
00108             for(x=0;x<WIDTH-17;x++) {
00109                 ptr = img2 + y * WIDTH + x;
00110                 d1 += test_func(NULL, img1, ptr, WIDTH, 1);
00111             }
00112         }
00113     }
00114     emms_c();
00115     dummy = d1; /* avoid optimization */
00116     ti = gettime() - ti;
00117 
00118     printf("  %0.0f kop/s\n",
00119            (double)NB_ITS * (WIDTH - 16) * (HEIGHT - 16) /
00120            (double)(ti / 1000.0));
00121 }
00122 
00123 
00124 int main(int argc, char **argv)
00125 {
00126     AVCodecContext *ctx;
00127     int c;
00128     DSPContext cctx, mmxctx;
00129     int flags[2] = { AV_CPU_FLAG_MMX, AV_CPU_FLAG_MMX2 };
00130     int flags_size = HAVE_MMX2 ? 2 : 1;
00131 
00132     for(;;) {
00133         c = getopt(argc, argv, "h");
00134         if (c == -1)
00135             break;
00136         switch(c) {
00137         case 'h':
00138             help();
00139             return 1;
00140         }
00141     }
00142 
00143     printf("Libav motion test\n");
00144 
00145     ctx = avcodec_alloc_context3(NULL);
00146     ctx->dsp_mask = AV_CPU_FLAG_FORCE;
00147     dsputil_init(&cctx, ctx);
00148     for (c = 0; c < flags_size; c++) {
00149         int x;
00150         ctx->dsp_mask = AV_CPU_FLAG_FORCE | flags[c];
00151         dsputil_init(&mmxctx, ctx);
00152 
00153         for (x = 0; x < 2; x++) {
00154             printf("%s for %dx%d pixels\n", c ? "mmx2" : "mmx",
00155                    x ? 8 : 16, x ? 8 : 16);
00156             test_motion("mmx",     mmxctx.pix_abs[x][0], cctx.pix_abs[x][0]);
00157             test_motion("mmx_x2",  mmxctx.pix_abs[x][1], cctx.pix_abs[x][1]);
00158             test_motion("mmx_y2",  mmxctx.pix_abs[x][2], cctx.pix_abs[x][2]);
00159             test_motion("mmx_xy2", mmxctx.pix_abs[x][3], cctx.pix_abs[x][3]);
00160         }
00161     }
00162     av_free(ctx);
00163 
00164     return 0;
00165 }