avutil/hwcontext_videotoolbox: add missing include for AVFrame
[ffmpeg.git] / tests / checkasm / motion.c
1 /*
2  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <string.h>
21
22 #include "libavutil/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem_internal.h"
25
26 #include "libavcodec/me_cmp.h"
27
28 #include "checkasm.h"
29
30 static void fill_random(uint8_t *tab, int size)
31 {
32     int i;
33     for (i = 0; i < size; i++) {
34         tab[i] = rnd() % 256;
35     }
36 }
37
38 static void test_motion(const char *name, me_cmp_func test_func)
39 {
40     /* test configurarion */
41 #define ITERATIONS 16
42 #define WIDTH 64
43 #define HEIGHT 64
44
45     /* motion estimation can look up to 17 bytes ahead */
46     static const int look_ahead = 17;
47
48     int i, x, y, d1, d2;
49     uint8_t *ptr;
50
51     LOCAL_ALIGNED_16(uint8_t, img1, [WIDTH * HEIGHT]);
52     LOCAL_ALIGNED_16(uint8_t, img2, [WIDTH * HEIGHT]);
53
54     declare_func_emms(AV_CPU_FLAG_MMX, int, struct MpegEncContext *c,
55                       uint8_t *blk1 /* align width (8 or 16) */,
56                       uint8_t *blk2 /* align 1 */, ptrdiff_t stride,
57                       int h);
58
59     if (test_func == NULL) {
60         return;
61     }
62
63     /* test correctness */
64     fill_random(img1, WIDTH * HEIGHT);
65     fill_random(img2, WIDTH * HEIGHT);
66
67     if (check_func(test_func, "%s", name)) {
68         for (i = 0; i < ITERATIONS; i++) {
69             x = rnd() % (WIDTH - look_ahead);
70             y = rnd() % (HEIGHT - look_ahead);
71
72             ptr = img2 + y * WIDTH + x;
73             d2 = call_ref(NULL, img1, ptr, WIDTH, 8);
74             d1 = call_new(NULL, img1, ptr, WIDTH, 8);
75
76             if (d1 != d2) {
77                 fail();
78                 printf("func: %s, x=%d y=%d, error: asm=%d c=%d\n", name, x, y, d1, d2);
79                 break;
80             }
81         }
82         // benchmark with the final value of ptr
83         bench_new(NULL, img1, ptr, WIDTH, 8);
84     }
85 }
86
87 #define ME_CMP_1D_ARRAYS(XX)                                                   \
88     XX(sad)                                                                    \
89     XX(sse)                                                                    \
90     XX(hadamard8_diff)                                                         \
91     XX(vsad)                                                                   \
92     XX(vsse)                                                                   \
93     XX(nsse)                                                                   \
94     XX(me_pre_cmp)                                                             \
95     XX(me_cmp)                                                                 \
96     XX(me_sub_cmp)                                                             \
97     XX(mb_cmp)                                                                 \
98     XX(ildct_cmp)                                                              \
99     XX(frame_skip_cmp)                                                         \
100     XX(median_sad)
101
102 // tests for functions not yet implemented
103 #if 0
104     XX(dct_sad)                                                                \
105     XX(quant_psnr)                                                             \
106     XX(bit)                                                                    \
107     XX(rd)                                                                     \
108     XX(w53)                                                                    \
109     XX(w97)                                                                    \
110     XX(dct_max)                                                                \
111     XX(dct264_sad)                                                             \
112
113 #endif
114
115 static void check_motion(void)
116 {
117     char buf[64];
118     AVCodecContext *av_ctx;
119     MECmpContext me_ctx;
120
121     memset(&me_ctx, 0, sizeof(me_ctx));
122
123     /* allocate AVCodecContext */
124     av_ctx = avcodec_alloc_context3(NULL);
125     av_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
126
127     ff_me_cmp_init(&me_ctx, av_ctx);
128
129     for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.pix_abs); i++) {
130         for (int j = 0; j < FF_ARRAY_ELEMS(me_ctx.pix_abs[0]); j++) {
131             snprintf(buf, sizeof(buf), "pix_abs_%d_%d", i, j);
132             test_motion(buf, me_ctx.pix_abs[i][j]);
133         }
134     }
135
136 #define XX(me_cmp_array)                                                        \
137     for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.me_cmp_array); i++) {             \
138         snprintf(buf, sizeof(buf), #me_cmp_array "_%d", i);                     \
139         test_motion(buf, me_ctx.me_cmp_array[i]);                               \
140     }
141     ME_CMP_1D_ARRAYS(XX)
142 #undef XX
143
144     avcodec_free_context(&av_ctx);
145 }
146
147 void checkasm_check_motion(void)
148 {
149     check_motion();
150     report("motion");
151 }