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 <stdio.h>
20 
21 #include "libavutil/internal.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/pixelutils.c"
24 
25 #define W1 320
26 #define H1 240
27 #define W2 640
28 #define H2 480
29 
run_single_test(const char *test, const uint8_t *block1, ptrdiff_t stride1, const uint8_t *block2, ptrdiff_t stride2, int align, int n)30 static int run_single_test(const char *test,
31                            const uint8_t *block1, ptrdiff_t stride1,
32                            const uint8_t *block2, ptrdiff_t stride2,
33                            int align, int n)
34 {
35     int out, ref;
36     av_pixelutils_sad_fn f_ref = sad_c[n - 1];
37     av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
38 
39     switch (align) {
40     case 0: block1++; block2++; break;
41     case 1:           block2++; break;
42     case 2:                     break;
43     }
44 
45     out = f_out(block1, stride1, block2, stride2);
46     ref = f_ref(block1, stride1, block2, stride2);
47     printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
48            out == ref ? "OK" : "FAIL",
49            align ? 'A' : 'U', align == 2 ? 'A' : 'U',
50            test, 1<<n, 1<<n, out, ref);
51     return out != ref;
52 }
53 
run_test(const char *test, const uint8_t *b1, const uint8_t *b2)54 static int run_test(const char *test,
55                     const uint8_t *b1, const uint8_t *b2)
56 {
57     int i, a, ret = 0;
58 
59     for (a = 0; a < 3; a++) {
60         const uint8_t *block1 = b1;
61         const uint8_t *block2 = b2;
62 
63         switch (a) {
64         case 0: block1++; block2++; break;
65         case 1:           block2++; break;
66         case 2:                     break;
67         }
68         for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
69             int r = run_single_test(test, b1, W1, b2, W2, a, i);
70             if (r)
71                 ret = r;
72         }
73     }
74     return ret;
75 }
76 
main(void)77 int main(void)
78 {
79     int i, align, ret;
80     uint8_t *buf1 = av_malloc(W1*H1);
81     uint8_t *buf2 = av_malloc(W2*H2);
82     uint32_t state = 0;
83 
84     if (!buf1 || !buf2) {
85         fprintf(stderr, "malloc failure\n");
86         ret = 1;
87         goto end;
88     }
89 
90     ff_check_pixfmt_descriptors();
91 
92 #define RANDOM_INIT(buf, size) do {             \
93     int k;                                      \
94     for (k = 0; k < size; k++) {                \
95         state = state * 1664525 + 1013904223;   \
96         buf[k] = state>>24;                     \
97     }                                           \
98 } while (0)
99 
100     /* Normal test with different strides */
101     RANDOM_INIT(buf1, W1*H1);
102     RANDOM_INIT(buf2, W2*H2);
103     ret = run_test("random", buf1, buf2);
104     if (ret < 0)
105         goto end;
106 
107     /* Check for maximum SAD */
108     memset(buf1, 0xff, W1*H1);
109     memset(buf2, 0x00, W2*H2);
110     ret = run_test("max", buf1, buf2);
111     if (ret < 0)
112         goto end;
113 
114     /* Check for minimum SAD */
115     memset(buf1, 0x90, W1*H1);
116     memset(buf2, 0x90, W2*H2);
117     ret = run_test("min", buf1, buf2);
118     if (ret < 0)
119         goto end;
120 
121     /* Exact buffer sizes, to check for overreads */
122     for (i = 1; i <= 5; i++) {
123         for (align = 0; align < 3; align++) {
124             int size1, size2;
125 
126             av_freep(&buf1);
127             av_freep(&buf2);
128 
129             size1 = size2 = 1 << (i << 1);
130 
131             switch (align) {
132             case 0: size1++; size2++; break;
133             case 1:          size2++; break;
134             case 2:                   break;
135             }
136 
137             buf1 = av_malloc(size1);
138             buf2 = av_malloc(size2);
139             if (!buf1 || !buf2) {
140                 fprintf(stderr, "malloc failure\n");
141                 ret = 1;
142                 goto end;
143             }
144             RANDOM_INIT(buf1, size1);
145             RANDOM_INIT(buf2, size2);
146             ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
147             if (ret < 0)
148                 goto end;
149         }
150     }
151 
152 end:
153     av_free(buf1);
154     av_free(buf2);
155     return ret;
156 }
157