]> git.sesse.net Git - x264/blob - testing/checkasm.c
Run ranlib after make install (OS X needs that)
[x264] / testing / checkasm.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #
6 #include "common/common.h"
7 #ifdef HAVE_MMXEXT
8 #include "common/i386/pixel.h"
9 #include "common/i386/dct.h"
10 #include "common/i386/mc.h"
11 #endif
12 #ifdef ARCH_PPC
13 #include "common/ppc/pixel.h"
14 #include "common/ppc/mc.h"
15 #endif
16
17 /* buf1, buf2: initialised to randome data and shouldn't write into them */
18 uint8_t * buf1, * buf2;
19 /* buf3, buf4: used to store output */
20 uint8_t * buf3, * buf4;
21
22 static int check_pixel()
23 {
24     x264_pixel_function_t pixel_c = {{0},{0},{0}};
25     x264_pixel_function_t pixel_asm = {{0}, {0},{0}};
26     int ret = 0, ok;
27     int i;
28
29     memset( &pixel_asm, 0, sizeof( x264_pixel_function_t ) );
30     x264_pixel_init( 0, &pixel_c );
31 #ifdef HAVE_MMXEXT
32     x264_pixel_init( X264_CPU_MMX|X264_CPU_MMXEXT, &pixel_asm );
33 #endif
34 #ifdef ARCH_PPC
35     x264_pixel_altivec_init( &pixel_asm );
36 #endif
37
38     for( i = 0, ok = 1; i < 7; i++ )
39     {
40         int res_c, res_asm;
41         if( pixel_asm.sad[i] )
42         {
43             res_c   = pixel_c.sad[i]( buf1, 32, buf2, 32 );
44             res_asm =  pixel_asm.sad[i]( buf1, 32, buf2, 32 );
45             if( res_c != res_asm )
46             {
47                 ok = 0;
48                 fprintf( stderr, "sad[%d]: %d != %d [FAILED]\n", i, res_c, res_asm );
49             }
50         }
51     }
52     if( ok )
53         fprintf( stderr, " - pixel sad :           [OK]\n" );
54     else {
55         ret = -1;
56         fprintf( stderr, " - pixel sat :           [FAILED]\n" );
57     }
58
59     for( i = 0, ok = 1; i < 7; i++ )
60     {
61         int res_c, res_asm;
62         if( pixel_asm.satd[i] )
63         {
64             res_c   = pixel_c.satd[i]( buf1, 32, buf2, 32 );
65             res_asm = pixel_asm.satd[i]( buf1, 32, buf2, 32 );
66             if( res_c != res_asm )
67             {
68                 ok = 0;
69                 fprintf( stderr, "satd[%d]: %d != %d [FAILED]\n", i, res_c, res_asm );
70             }
71         }
72     }
73
74     if( ok )
75         fprintf( stderr, " - pixel satd :          [OK]\n" );
76     else {
77         ret = -1;
78         fprintf( stderr, " - pixel satd :          [FAILED]\n" );
79     }
80
81     for( i = 0, ok = 1; i < 7; i++ )
82     {
83         if( pixel_asm.avg[i] )
84         {
85             memcpy( buf3, buf1, 32*32 );
86             memcpy( buf4, buf1, 32*32 );
87             pixel_c.satd[i]( buf3, 32, buf2, 32 );
88             pixel_asm.satd[i]( buf4, 32, buf2, 32 );
89             if( memcmp( buf3, buf4, 32*32 ) )
90             {
91                 ok = 0;
92                 fprintf( stderr, "avg[%d]: [FAILED]\n", i );
93             }
94         }
95     }
96
97     if( ok )
98         fprintf( stderr, " - pixel avg :           [OK]\n" );
99     else {
100         ret = -1;
101         fprintf( stderr, " - pixel avg :           [FAILED]\n" );
102     }
103
104     return ret;
105 }
106
107 static int check_dct()
108 {
109     x264_dct_function_t dct_c;
110     x264_dct_function_t dct_asm;
111     int ret = 0, ok;
112     int16_t dct1[16][4][4] __attribute((aligned(16)));
113     int16_t dct2[16][4][4] __attribute((aligned(16)));
114
115     memset( &dct_asm, 0, sizeof( dct_asm ) );
116     x264_dct_init( 0, &dct_c );
117 #ifdef HAVE_MMXEXT
118     x264_dct_init( X264_CPU_MMX|X264_CPU_MMXEXT, &dct_asm );
119 #endif
120 #define TEST_DCT( name, t1, t2, size ) \
121     if( dct_asm.name ) \
122     { \
123         dct_c.name( t1, buf1, 32, buf2, 32 ); \
124         dct_asm.name( t2, buf1, 32, buf2, 32 ); \
125         if( memcmp( t1, t2, size ) ) \
126         { \
127             ok = 0; \
128             fprintf( stderr, #name " [FAILED]\n" ); \
129         } \
130     }
131     ok = 1;
132     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
133     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
134     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
135     if( ok )
136         fprintf( stderr, " - sub_dctXxX :          [OK]\n" );
137     else {
138         ret = -1;
139         fprintf( stderr, " - sub_dctXxX :          [FAILED]\n" );
140     }
141 #undef TEST_DCT
142
143 #define TEST_IDCT( name, t ) \
144     if( dct_asm.name ) \
145     { \
146         memcpy( buf3, buf1, 32*32 ); \
147         memcpy( buf4, buf1, 32*32 ); \
148         dct_c.name( buf3, 32, t ); \
149         dct_asm.name( buf4, 32, t ); \
150         if( memcmp( buf3, buf4, 32*32 ) ) \
151         { \
152             ok = 0; \
153             fprintf( stderr, #name " [FAILED]\n" ); \
154         } \
155     }
156     ok = 1;
157     TEST_IDCT( add4x4_idct, dct1[0] );
158     TEST_IDCT( add8x8_idct, dct1 );
159     TEST_IDCT( add16x16_idct, dct1 );
160     if( ok )
161         fprintf( stderr, " - add_idctXxX :         [OK]\n" );
162     else {
163         ret = -1;
164         fprintf( stderr, " - add_idctXxX :         [FAILED]\n" );
165     }
166 #undef TEST_IDCT
167
168     ok = 1;
169     if( dct_asm.dct4x4dc )
170     {
171         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
172         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
173
174         dct_c.dct4x4dc( dct1 );
175         dct_asm.dct4x4dc( dct2 );
176         if( memcmp( dct1, dct2, 32 ) )
177         {
178             ok = 0;
179             fprintf( stderr, " - dct4x4dc :        [FAILED]\n" );
180         }
181     }
182     if( dct_asm.idct4x4dc )
183     {
184         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
185         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
186
187         dct_c.idct4x4dc( dct1 );
188         dct_asm.idct4x4dc( dct2 );
189         if( memcmp( dct1, dct2, 32 ) )
190         {
191             ok = 0;
192             fprintf( stderr, " - idct4x4dc :        [FAILED]\n" );
193         }
194     }
195     if( ok )
196         fprintf( stderr, " - (i)dct4x4dc :         [OK]\n" );
197     else {
198         ret = -1;
199         fprintf( stderr, " - (i)dct4x4dc :         [FAILED]\n" );
200     }
201
202     ok = 1;
203     if( dct_asm.dct2x2dc )
204     {
205         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
206         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
207
208         dct_c.dct2x2dc( dct1 );
209         dct_asm.dct2x2dc( dct2 );
210         if( memcmp( dct1, dct2, 4*2 ) )
211         {
212             ok = 0;
213             fprintf( stderr, " - dct2x2dc :        [FAILED]\n" );
214         }
215     }
216     if( dct_asm.idct2x2dc )
217     {
218         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
219         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
220
221         dct_c.idct2x2dc( dct1 );
222         dct_asm.idct2x2dc( dct2 );
223         if( memcmp( dct1, dct2, 4*2 ) )
224         {
225             ok = 0;
226             fprintf( stderr, " - idct2x2dc :       [FAILED]\n" );
227         }
228     }
229
230     if( ok )
231         fprintf( stderr, " - (i)dct2x2dc :         [OK]\n" );
232     else {
233         ret = -1;
234         fprintf( stderr, " - (i)dct2x2dc :         [FAILED]\n" );
235     }
236
237
238     return ret;
239 }
240
241 static int check_mc()
242 {
243     x264_mc_functions_t mc_c = {0,0,0};
244     x264_mc_functions_t mc_a = {0,0,0};
245
246     uint8_t *src     = &buf1[2*32+2];
247     uint8_t *src2[4] = { &buf1[2*32+2],  &buf1[7*32+2],
248                          &buf1[12*32+2], &buf1[17*32+2] };
249     uint8_t *dst1    = &buf3[2*32+2];
250     uint8_t *dst2    = &buf4[2*32+2];
251
252     int dx, dy;
253     int ret = 0, ok[2] = { 1, 1 };
254
255     x264_mc_init( 0, &mc_c );
256 #ifdef HAVE_MMXEXT
257     x264_mc_mmxext_init( &mc_a );
258 #endif
259 #ifdef ARCH_PPC
260     x264_mc_altivec_init( &mc_a );
261 #endif
262
263 #define MC_TEST_LUMA( w, h ) \
264         if( mc_a.mc_luma ) \
265         { \
266             memset(buf3, 0xCD, 1024); \
267             memset(buf4, 0xCD, 1024); \
268             mc_c.mc_luma( src2, 32, dst1, 16, dx, dy, w, h );     \
269             mc_a.mc_luma( src2, 32, dst2, 16, dx, dy, w, h );   \
270             if( memcmp( buf3, buf4, 1024 ) )               \
271             { \
272                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h );   \
273                 ok[0] = 0; \
274             } \
275         }
276
277 #define MC_TEST_CHROMA( w, h ) \
278         if( mc_a.mc_chroma ) \
279         { \
280             memset(buf3, 0xCD, 1024); \
281             memset(buf4, 0xCD, 1024); \
282             mc_c.mc_chroma( src, 32, dst1, 16, dx, dy, w, h );     \
283             mc_a.mc_chroma( src, 32, dst2, 16, dx, dy, w, h );   \
284             if( memcmp( buf3, buf4, 1024 ) )               \
285             { \
286                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h );   \
287                 ok[1] = 0; \
288             } \
289         }
290
291     for( dy = 0; dy < 4; dy++ )
292     {
293         for( dx = 0; dx < 4; dx++ )
294         {
295             MC_TEST_LUMA( 16, 16 );
296             MC_TEST_LUMA( 16, 8 );
297             MC_TEST_LUMA( 8, 16 );
298             MC_TEST_LUMA( 8, 8 );
299             MC_TEST_LUMA( 8, 4 );
300             MC_TEST_LUMA( 4, 8 );
301             MC_TEST_LUMA( 4, 4 );
302
303             MC_TEST_CHROMA( 8, 8 );
304             MC_TEST_CHROMA( 8, 4 );
305             MC_TEST_CHROMA( 4, 8 );
306             MC_TEST_CHROMA( 4, 4 );
307             MC_TEST_CHROMA( 4, 2 );
308             MC_TEST_CHROMA( 2, 4 );
309             MC_TEST_CHROMA( 2, 2 );
310         }
311     }
312 #undef MC_TEST_LUMA
313 #undef MC_TEST_CHROMA
314     if( ok[0] )
315         fprintf( stderr, " - mc luma :             [OK]\n" );
316     else {
317         ret = -1;
318         fprintf( stderr, " - mc luma :             [FAILED]\n" );
319     }
320     if( ok[1] )
321         fprintf( stderr, " - mc chroma :           [OK]\n" );
322     else {
323         ret = -1;
324         fprintf( stderr, " - mc chroma :           [FAILED]\n" );
325     }
326     return ret;
327 }
328
329 int main()
330 {
331     int ret;
332     int i;
333
334 #ifdef HAVE_MMXEXT
335     fprintf( stderr, "x264: MMXEXT against C\n" );
336 #elif ARCH_PPC
337     fprintf( stderr, "x264: ALTIVEC against C\n" );
338 #endif
339
340     buf1 = x264_malloc( 1024 ); /* 32 x 32 */
341     buf2 = x264_malloc( 1024 );
342     buf3 = x264_malloc( 1024 );
343     buf4 = x264_malloc( 1024 );
344
345     srand( x264_mdate() );
346
347     for( i = 0; i < 1024; i++ )
348     {
349         buf1[i] = rand() % 0xFF;
350         buf2[i] = rand() % 0xFF;
351         buf3[i] = buf4[i] = 0;
352     }
353
354     ret = check_pixel() +
355           check_dct() +
356           check_mc();
357
358     if( ret == 0 )
359     {
360         fprintf( stderr, "x264: All tests passed Yeah :)\n" );
361         return 0;
362     }
363     fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
364     return -1;
365 }
366