2 * This file is part of FFmpeg.
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.
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.
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
21 #include "pixelutils.h"
26 #include "x86/pixelutils.h"
28 static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
29 const uint8_t *src2, ptrdiff_t stride2,
34 for (y = 0; y < h; y++) {
35 for (x = 0; x < w; x++)
36 sum += abs(src1[x] - src2[x]);
43 #define DECLARE_BLOCK_FUNCTIONS(size) \
44 static int block_sad_##size##x##size##_c(const uint8_t *src1, ptrdiff_t stride1, \
45 const uint8_t *src2, ptrdiff_t stride2) \
47 return sad_wxh(src1, stride1, src2, stride2, size, size); \
50 DECLARE_BLOCK_FUNCTIONS(2)
51 DECLARE_BLOCK_FUNCTIONS(4)
52 DECLARE_BLOCK_FUNCTIONS(8)
53 DECLARE_BLOCK_FUNCTIONS(16)
55 static const av_pixelutils_sad_fn sad_c[] = {
62 #endif /* CONFIG_PIXELUTILS */
64 av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
66 #if !CONFIG_PIXELUTILS
67 av_log(log_ctx, AV_LOG_ERROR, "pixelutils support is required "
68 "but libavutil is not compiled with it\n");
71 av_pixelutils_sad_fn sad[FF_ARRAY_ELEMS(sad_c)];
73 memcpy(sad, sad_c, sizeof(sad));
75 if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
76 h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
78 if (w_bits != h_bits) // only squared sad for now
82 ff_pixelutils_sad_init_x86(sad, aligned);
85 return sad[w_bits - 1];
95 static int run_single_test(const char *test,
96 const uint8_t *block1, ptrdiff_t stride1,
97 const uint8_t *block2, ptrdiff_t stride2,
101 av_pixelutils_sad_fn f_ref = sad_c[n - 1];
102 av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
105 case 0: block1++; block2++; break;
106 case 1: block2++; break;
110 out = f_out(block1, stride1, block2, stride2);
111 ref = f_ref(block1, stride1, block2, stride2);
112 printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
113 out == ref ? "OK" : "FAIL",
114 align ? 'A' : 'U', align == 2 ? 'A' : 'U',
115 test, 1<<n, 1<<n, out, ref);
119 static int run_test(const char *test,
120 const uint8_t *b1, const uint8_t *b2)
124 for (a = 0; a < 3; a++) {
125 const uint8_t *block1 = b1;
126 const uint8_t *block2 = b2;
129 case 0: block1++; block2++; break;
130 case 1: block2++; break;
133 for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
134 int r = run_single_test(test, b1, W1, b2, W2, a, i);
145 uint8_t *buf1 = av_malloc(W1*H1);
146 uint8_t *buf2 = av_malloc(W2*H2);
149 if (!buf1 || !buf2) {
150 fprintf(stderr, "malloc failure\n");
155 ff_check_pixfmt_descriptors();
157 #define RANDOM_INIT(buf, size) do { \
159 for (k = 0; k < size; k++) { \
160 state = state * 1664525 + 1013904223; \
161 buf[k] = state>>24; \
165 /* Normal test with different strides */
166 RANDOM_INIT(buf1, W1*H1);
167 RANDOM_INIT(buf2, W2*H2);
168 ret = run_test("random", buf1, buf2);
172 /* Check for maximum SAD */
173 memset(buf1, 0xff, W1*H1);
174 memset(buf2, 0x00, W2*H2);
175 ret = run_test("max", buf1, buf2);
179 /* Check for minimum SAD */
180 memset(buf1, 0x90, W1*H1);
181 memset(buf2, 0x90, W2*H2);
182 ret = run_test("min", buf1, buf2);
186 /* Exact buffer sizes, to check for overreads */
187 for (i = 1; i <= 4; i++) {
188 for (align = 0; align < 3; align++) {
194 size1 = size2 = 1 << (i << 1);
197 case 0: size1++; size2++; break;
198 case 1: size2++; break;
202 buf1 = av_malloc(size1);
203 buf2 = av_malloc(size2);
204 if (!buf1 || !buf2) {
205 fprintf(stderr, "malloc failure\n");
209 RANDOM_INIT(buf1, size1);
210 RANDOM_INIT(buf2, size2);
211 ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);