aboutsummaryrefslogtreecommitdiffstats
path: root/tests/checkasm/motion.c
blob: 631a9ed29f21fd3587ccf81e7d3e75225b75bf0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <string.h>

#include "libavutil/common.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem_internal.h"

#include "libavcodec/me_cmp.h"

#include "checkasm.h"

static void fill_random(uint8_t *tab, int size)
{
    int i;
    for (i = 0; i < size; i++) {
        tab[i] = rnd() % 256;
    }
}

static void test_motion(const char *name, me_cmp_func test_func)
{
    /* test configurarion */
#define ITERATIONS 16
#define WIDTH 64
#define HEIGHT 64

    /* motion estimation can look up to 17 bytes ahead */
    static const int look_ahead = 17;

    int i, x, y, d1, d2;
    uint8_t *ptr;

    LOCAL_ALIGNED_16(uint8_t, img1, [WIDTH * HEIGHT]);
    LOCAL_ALIGNED_16(uint8_t, img2, [WIDTH * HEIGHT]);

    declare_func_emms(AV_CPU_FLAG_MMX, int, struct MpegEncContext *c,
                      uint8_t *blk1 /* align width (8 or 16) */,
                      uint8_t *blk2 /* align 1 */, ptrdiff_t stride,
                      int h);

    if (test_func == NULL) {
        return;
    }

    /* test correctness */
    fill_random(img1, WIDTH * HEIGHT);
    fill_random(img2, WIDTH * HEIGHT);

    if (check_func(test_func, "%s", name)) {
        for (i = 0; i < ITERATIONS; i++) {
            x = rnd() % (WIDTH - look_ahead);
            y = rnd() % (HEIGHT - look_ahead);

            ptr = img2 + y * WIDTH + x;
            d2 = call_ref(NULL, img1, ptr, WIDTH, 8);
            d1 = call_new(NULL, img1, ptr, WIDTH, 8);

            if (d1 != d2) {
                fail();
                printf("func: %s, x=%d y=%d, error: asm=%d c=%d\n", name, x, y, d1, d2);
                break;
            }
        }
        // Test with a fixed offset, for benchmark stability
        ptr = img2 + 3 * WIDTH + 3;
        bench_new(NULL, img1, ptr, WIDTH, 8);
    }
}

#define ME_CMP_1D_ARRAYS(XX)                                                   \
    XX(sad)                                                                    \
    XX(sse)                                                                    \
    XX(hadamard8_diff)                                                         \
    XX(vsad)                                                                   \
    XX(vsse)                                                                   \
    XX(nsse)                                                                   \
    XX(me_pre_cmp)                                                             \
    XX(me_cmp)                                                                 \
    XX(me_sub_cmp)                                                             \
    XX(mb_cmp)                                                                 \
    XX(ildct_cmp)                                                              \
    XX(frame_skip_cmp)                                                         \
    XX(median_sad)

// tests for functions not yet implemented
#if 0
    XX(dct_sad)                                                                \
    XX(quant_psnr)                                                             \
    XX(bit)                                                                    \
    XX(rd)                                                                     \
    XX(w53)                                                                    \
    XX(w97)                                                                    \
    XX(dct_max)                                                                \
    XX(dct264_sad)                                                             \

#endif

static void check_motion(void)
{
    char buf[64];
    AVCodecContext *av_ctx;
    MECmpContext me_ctx;

    memset(&me_ctx, 0, sizeof(me_ctx));

    /* allocate AVCodecContext */
    av_ctx = avcodec_alloc_context3(NULL);
    av_ctx->flags |= AV_CODEC_FLAG_BITEXACT;

    ff_me_cmp_init(&me_ctx, av_ctx);

    for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.pix_abs); i++) {
        for (int j = 0; j < FF_ARRAY_ELEMS(me_ctx.pix_abs[0]); j++) {
            snprintf(buf, sizeof(buf), "pix_abs_%d_%d", i, j);
            test_motion(buf, me_ctx.pix_abs[i][j]);
        }
    }

#define XX(me_cmp_array)                                                        \
    for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.me_cmp_array); i++) {             \
        snprintf(buf, sizeof(buf), #me_cmp_array "_%d", i);                     \
        test_motion(buf, me_ctx.me_cmp_array[i]);                               \
    }
    ME_CMP_1D_ARRAYS(XX)
#undef XX

    avcodec_free_context(&av_ctx);
}

void checkasm_check_motion(void)
{
    check_motion();
    report("motion");
}