]> git.sesse.net Git - ffmpeg/blob - libavutil/pixelutils.c
Merge commit '424b929b5cb9ca4094099f25179829260d4b0fa3'
[ffmpeg] / libavutil / pixelutils.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20 #include "common.h"
21 #include "pixelutils.h"
22
23 #if CONFIG_PIXELUTILS
24
25 #include "x86/pixelutils.h"
26
27 static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
28                                     const uint8_t *src2, ptrdiff_t stride2,
29                                     int w, int h)
30 {
31     int x, y, sum = 0;
32
33     for (y = 0; y < h; y++) {
34         for (x = 0; x < w; x++)
35             sum += abs(src1[x] - src2[x]);
36         src1 += stride1;
37         src2 += stride2;
38     }
39     return sum;
40 }
41
42 #define DECLARE_BLOCK_FUNCTIONS(size)                                               \
43 static int block_sad_##size##x##size##_c(const uint8_t *src1, ptrdiff_t stride1,    \
44                                          const uint8_t *src2, ptrdiff_t stride2)    \
45 {                                                                                   \
46     return sad_wxh(src1, stride1, src2, stride2, size, size);                       \
47 }
48
49 DECLARE_BLOCK_FUNCTIONS(2)
50 DECLARE_BLOCK_FUNCTIONS(4)
51 DECLARE_BLOCK_FUNCTIONS(8)
52 DECLARE_BLOCK_FUNCTIONS(16)
53
54 static const av_pixelutils_sad_fn sad_c[] = {
55     block_sad_2x2_c,
56     block_sad_4x4_c,
57     block_sad_8x8_c,
58     block_sad_16x16_c,
59 };
60
61 #endif /* CONFIG_PIXELUTILS */
62
63 av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
64 {
65 #if !CONFIG_PIXELUTILS
66     av_log(log_ctx, AV_LOG_ERROR, "pixelutils support is required "
67            "but libavutil is not compiled with it\n");
68     return NULL;
69 #else
70     av_pixelutils_sad_fn sad[FF_ARRAY_ELEMS(sad_c)];
71
72     memcpy(sad, sad_c, sizeof(sad));
73
74     if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
75         h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
76         return NULL;
77     if (w_bits != h_bits) // only squared sad for now
78         return NULL;
79
80 #if ARCH_X86
81     ff_pixelutils_sad_init_x86(sad, aligned);
82 #endif
83
84     return sad[w_bits - 1];
85 #endif
86 }
87
88 #ifdef TEST
89 #define W1 320
90 #define H1 240
91 #define W2 640
92 #define H2 480
93
94 static int run_test(const char *test,
95                     const uint8_t *b1, const uint8_t *b2)
96 {
97     int i, a, ret = 0;
98
99     for (a = 0; a < 3; a++) {
100         const uint8_t *block1 = b1;
101         const uint8_t *block2 = b2;
102
103         switch (a) {
104         case 0: block1++; block2++; break;
105         case 1:           block2++; break;
106         case 2:                     break;
107         }
108         for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
109             av_pixelutils_sad_fn f_ref = sad_c[i - 1];
110             av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(i, i, a, NULL);
111             const int out = f_out(block1, W1, block2, W2);
112             const int ref = f_ref(block1, W1, block2, W2);
113             printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
114                    out == ref ? "OK" : "FAIL",
115                    a ? 'A' : 'U', a == 2 ? 'A' : 'U',
116                    test, 1<<i, 1<<i, out, ref);
117             if (out != ref)
118                 ret = 1;
119         }
120     }
121     return ret;
122 }
123
124 int main(void)
125 {
126     int i, ret;
127     uint8_t *buf1 = av_malloc(W1*H1);
128     uint8_t *buf2 = av_malloc(W2*H2);
129     uint32_t state = 0;
130
131     if (!buf1 || !buf2) {
132         fprintf(stderr, "malloc failure\n");
133         ret = 1;
134         goto end;
135     }
136
137     for (i = 0; i < W1*H1; i++) {
138         state = state * 1664525 + 1013904223;
139         buf1[i] = state>>24;
140     }
141     for (i = 0; i < W2*H2; i++) {
142         state = state * 1664525 + 1013904223;
143         buf2[i] = state>>24;
144     }
145     ret = run_test("random", buf1, buf2);
146     if (ret < 0)
147         goto end;
148
149     memset(buf1, 0xff, W1*H1);
150     memset(buf2, 0x00, W2*H2);
151     ret = run_test("max", buf1, buf2);
152     if (ret < 0)
153         goto end;
154
155     memset(buf1, 0x90, W1*H1);
156     memset(buf2, 0x90, W2*H2);
157     ret = run_test("min", buf1, buf2);
158 end:
159     av_free(buf1);
160     av_free(buf2);
161     return ret;
162 }
163 #endif /* TEST */