checkasm: add tests for dcadsp
[ffmpeg.git] / tests / checkasm / dcadsp.c
1 /*
2  * Copyright (c) 2015 Janne Grunau
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with Libav; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <math.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "libavutil/internal.h"
26 #include "libavutil/intfloat.h"
27 #include "libavcodec/dca.h"
28 #include "libavcodec/dcadsp.h"
29 #include "libavcodec/dcadata.h"
30
31 #include "checkasm.h"
32
33 #define randomize_lfe_fir(size)                                 \
34     do {                                                        \
35         int i;                                                  \
36         for (i = 0; i < size; i++) {                            \
37             float f = (float)rnd() / (UINT_MAX >> 1) - 1.0f;    \
38             in[i] = f;                                          \
39         }                                                       \
40         for (i = 0; i < 256; i++) {                             \
41             float f = (float)rnd() / (UINT_MAX >> 1) - 1.0f;    \
42             coeffs[i] = f;                                      \
43         }                                                       \
44     } while (0)
45
46 #define check_lfe_fir(decifactor, eps)                                  \
47     do {                                                                \
48         LOCAL_ALIGNED_16(float, in,     [256 / decifactor]);            \
49         LOCAL_ALIGNED_16(float, out0,   [decifactor * 2]);              \
50         LOCAL_ALIGNED_16(float, out1,   [decifactor * 2]);              \
51         LOCAL_ALIGNED_16(float, coeffs, [256]);                         \
52         int i;                                                          \
53         const float * in_ptr = in + (256 / decifactor) - 1;             \
54         declare_func(void, float *out, const float *in, const float *coeffs); \
55         /* repeat the test several times */                             \
56         for (i = 0; i < 32; i++) {                                      \
57             int j;                                                      \
58             memset(out0,    0, sizeof(*out0) * 2 * decifactor);         \
59             memset(out1, 0xFF, sizeof(*out1) * 2 * decifactor);         \
60             randomize_lfe_fir(256 / decifactor);                        \
61             call_ref(out0, in_ptr, coeffs);                             \
62             call_new(out1, in_ptr, coeffs);                             \
63             for (j = 0; j < 2 * decifactor; j++) {                      \
64                 if (!float_near_abs_eps(out0[j], out1[j], eps)) {       \
65                     if (0) {                                            \
66                         union av_intfloat32 x, y; x.f = out0[j]; y.f = out1[j]; \
67                         fprintf(stderr, "%3d: %11g (0x%08x); %11g (0x%08x)\n", \
68                                 j, x.f, x.i, y.f, y.i);                 \
69                     }                                                   \
70                     fail();                                             \
71                     break;                                              \
72                 }                                                       \
73             }                                                           \
74             bench_new(out1, in_ptr, coeffs);                            \
75         }                                                               \
76     } while (0)
77
78 #define randomize_decode_hf()                                   \
79     do {                                                        \
80         int i;                                                  \
81         for (i = 0; i < DCA_SUBBANDS; i++) {                    \
82             vq_num[i]   = rnd() >> 22;                          \
83             scale[i][0] = rnd() >> 26;                          \
84             scale[i][1] = INT32_MIN;                            \
85         }                                                       \
86     } while (0)
87
88 void checkasm_check_dcadsp(void)
89 {
90     DCADSPContext c;
91
92     ff_dcadsp_init(&c);
93
94     /* values are limited to {-8, 8} so absolute epsilon is good enough */
95     if (check_func(c.lfe_fir[0], "dca_lfe_fir0"))
96         check_lfe_fir(32, 1.0e-6f);
97
98     if (check_func(c.lfe_fir[1], "dca_lfe_fir1"))
99         check_lfe_fir(64, 1.0e-6f);
100
101     if (check_func(c.decode_hf,  "dca_decode_hf")) {
102         LOCAL_ALIGNED_16(float,   dst0,   [DCA_SUBBANDS], [8]);
103         LOCAL_ALIGNED_16(float,   dst1,   [DCA_SUBBANDS], [8]);
104         LOCAL_ALIGNED_16(int32_t, scale,  [DCA_SUBBANDS], [2]);
105         LOCAL_ALIGNED_16(int32_t, vq_num, [DCA_SUBBANDS]);
106         intptr_t start, end = 32, offset;
107
108         declare_func(void, float[DCA_SUBBANDS][8], const int32_t[DCA_SUBBANDS],
109                      const int8_t[1024][DCA_SUBBANDS], intptr_t, int32_t[DCA_SUBBANDS][2],
110                      intptr_t, intptr_t);
111
112         for (start = 0; start < 32; start++) {
113             for (offset = 0; offset < 32; offset += 8) {
114                 int j;
115                 for (j = 0; j < DCA_SUBBANDS; j++) {
116                     memset(dst0[j], 0, sizeof(*(dst0[j])) * 8);
117                     memset(dst1[j], 0, sizeof(*(dst1[j])) * 8);
118                 }
119                 randomize_decode_hf();
120
121                 call_ref(dst0, vq_num, ff_dca_high_freq_vq, offset, scale, start, end);
122                 call_new(dst1, vq_num, ff_dca_high_freq_vq, offset, scale, start, end);
123
124                 for (j = 0; j < 8 * DCA_SUBBANDS; j++) {
125                     if (!float_near_ulp(dst0[j>>3][j&7], dst1[j>>3][j&7], 1)) {
126                         fail();
127                         break;
128                     }
129                 }
130
131                 bench_new(dst1, vq_num, ff_dca_high_freq_vq, offset, scale, start, end);
132             }
133         }
134     }
135
136     report("dcadsp");
137 }