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"
25 #include "x86/pixelutils.h"
27 static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
28 const uint8_t *src2, ptrdiff_t stride2,
33 for (y = 0; y < h; y++) {
34 for (x = 0; x < w; x++)
35 sum += abs(src1[x] - src2[x]);
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) \
46 return sad_wxh(src1, stride1, src2, stride2, size, size); \
49 DECLARE_BLOCK_FUNCTIONS(2)
50 DECLARE_BLOCK_FUNCTIONS(4)
51 DECLARE_BLOCK_FUNCTIONS(8)
52 DECLARE_BLOCK_FUNCTIONS(16)
54 static const av_pixelutils_sad_fn sad_c[] = {
61 #endif /* CONFIG_PIXELUTILS */
63 av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
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");
70 av_pixelutils_sad_fn sad[FF_ARRAY_ELEMS(sad_c)];
72 memcpy(sad, sad_c, sizeof(sad));
74 if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
75 h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
77 if (w_bits != h_bits) // only squared sad for now
81 ff_pixelutils_sad_init_x86(sad, aligned);
84 return sad[w_bits - 1];
94 static int run_test(const char *test,
95 const uint8_t *b1, const uint8_t *b2)
99 for (a = 0; a < 3; a++) {
100 const uint8_t *block1 = b1;
101 const uint8_t *block2 = b2;
104 case 0: block1++; block2++; break;
105 case 1: block2++; break;
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);
127 uint8_t *buf1 = av_malloc(W1*H1);
128 uint8_t *buf2 = av_malloc(W2*H2);
131 if (!buf1 || !buf2) {
132 fprintf(stderr, "malloc failure\n");
137 for (i = 0; i < W1*H1; i++) {
138 state = state * 1664525 + 1013904223;
141 for (i = 0; i < W2*H2; i++) {
142 state = state * 1664525 + 1013904223;
145 ret = run_test("random", buf1, buf2);
149 memset(buf1, 0xff, W1*H1);
150 memset(buf2, 0x00, W2*H2);
151 ret = run_test("max", buf1, buf2);
155 memset(buf1, 0x90, W1*H1);
156 memset(buf2, 0x90, W2*H2);
157 ret = run_test("min", buf1, buf2);