libavcodec/motion-test.c
04d7f601
 /*
  * (c) 2001 Fabrice Bellard
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
04d7f601
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
04d7f601
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
04d7f601
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
04d7f601
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
983e3246
 
 /**
ba87f080
  * @file
983e3246
  * motion test.
  */
 
d0a0b248
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
 #include <unistd.h>
 
e4732ba9
 #include "config.h"
d0a0b248
 #include "dsputil.h"
294eaa26
 #include "libavutil/lfg.h"
d0a0b248
 
8823419f
 #undef printf
 
d0a0b248
 #define WIDTH 64
 #define HEIGHT 64
 
4dccfff9
 static uint8_t img1[WIDTH * HEIGHT];
 static uint8_t img2[WIDTH * HEIGHT];
d0a0b248
 
504ffed1
 static void fill_random(uint8_t *tab, int size)
d0a0b248
 {
     int i;
64bde197
     AVLFG prng;
294eaa26
 
64bde197
     av_lfg_init(&prng, 1);
d0a0b248
     for(i=0;i<size;i++) {
 #if 1
64bde197
         tab[i] = av_lfg_get(&prng) % 256;
d0a0b248
 #else
         tab[i] = i;
 #endif
     }
 }
 
504ffed1
 static void help(void)
d0a0b248
 {
     printf("motion-test [-h]\n"
            "test motion implementations\n");
 }
 
504ffed1
 static int64_t gettime(void)
d0a0b248
 {
     struct timeval tv;
     gettimeofday(&tv,NULL);
0c1a9eda
     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
d0a0b248
 }
 
 #define NB_ITS 500
 
 int dummy;
 
504ffed1
 static void test_motion(const char *name,
097e1da4
                  me_cmp_func test_func, me_cmp_func ref_func)
d0a0b248
 {
     int x, y, d1, d2, it;
0c1a9eda
     uint8_t *ptr;
     int64_t ti;
d0a0b248
     printf("testing '%s'\n", name);
 
     /* test correctness */
     for(it=0;it<20;it++) {
 
         fill_random(img1, WIDTH * HEIGHT);
         fill_random(img2, WIDTH * HEIGHT);
115329f1
 
d0a0b248
         for(y=0;y<HEIGHT-17;y++) {
             for(x=0;x<WIDTH-17;x++) {
115329f1
                 ptr = img2 + y * WIDTH + x;
097e1da4
                 d1 = test_func(NULL, img1, ptr, WIDTH, 1);
                 d2 = ref_func(NULL, img1, ptr, WIDTH, 1);
d0a0b248
                 if (d1 != d2) {
                     printf("error: mmx=%d c=%d\n", d1, d2);
                 }
             }
         }
     }
bcbd6603
     emms_c();
115329f1
 
d0a0b248
     /* speed test */
     ti = gettime();
     d1 = 0;
     for(it=0;it<NB_ITS;it++) {
         for(y=0;y<HEIGHT-17;y++) {
             for(x=0;x<WIDTH-17;x++) {
115329f1
                 ptr = img2 + y * WIDTH + x;
097e1da4
                 d1 += test_func(NULL, img1, ptr, WIDTH, 1);
d0a0b248
             }
         }
     }
bcbd6603
     emms_c();
52b541ad
     dummy = d1; /* avoid optimization */
d0a0b248
     ti = gettime() - ti;
115329f1
 
     printf("  %0.0f kop/s\n",
            (double)NB_ITS * (WIDTH - 16) * (HEIGHT - 16) /
d0a0b248
            (double)(ti / 1000.0));
 }
 
 
 int main(int argc, char **argv)
 {
097e1da4
     AVCodecContext *ctx;
d0a0b248
     int c;
097e1da4
     DSPContext cctx, mmxctx;
7160bb71
     int flags[2] = { AV_CPU_FLAG_MMX, AV_CPU_FLAG_MMX2 };
e4732ba9
     int flags_size = HAVE_MMX2 ? 2 : 1;
115329f1
 
d0a0b248
     for(;;) {
         c = getopt(argc, argv, "h");
         if (c == -1)
             break;
         switch(c) {
         case 'h':
             help();
9a5d6c23
             return 1;
d0a0b248
         }
     }
115329f1
 
d0a0b248
     printf("ffmpeg motion test\n");
 
71a861cf
     ctx = avcodec_alloc_context3(NULL);
7160bb71
     ctx->dsp_mask = AV_CPU_FLAG_FORCE;
097e1da4
     dsputil_init(&cctx, ctx);
e4732ba9
     for (c = 0; c < flags_size; c++) {
097e1da4
         int x;
7160bb71
         ctx->dsp_mask = AV_CPU_FLAG_FORCE | flags[c];
097e1da4
         dsputil_init(&mmxctx, ctx);
 
         for (x = 0; x < 2; x++) {
             printf("%s for %dx%d pixels\n", c ? "mmx2" : "mmx",
                    x ? 8 : 16, x ? 8 : 16);
             test_motion("mmx",     mmxctx.pix_abs[x][0], cctx.pix_abs[x][0]);
             test_motion("mmx_x2",  mmxctx.pix_abs[x][1], cctx.pix_abs[x][1]);
             test_motion("mmx_y2",  mmxctx.pix_abs[x][2], cctx.pix_abs[x][2]);
             test_motion("mmx_xy2", mmxctx.pix_abs[x][3], cctx.pix_abs[x][3]);
         }
     }
     av_free(ctx);
 
d0a0b248
     return 0;
 }