• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   PulseAudio is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published
6   by the Free Software Foundation; either version 2.1 of the License,
7   or (at your option) any later version.
8 
9   PulseAudio is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public License
15   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16 ***/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include <check.h>
23 
24 #include <pulsecore/cpu.h>
25 #include <pulsecore/cpu-arm.h>
26 #include <pulsecore/random.h>
27 #include <pulsecore/macro.h>
28 #include <pulsecore/mix.h>
29 
30 #include "runtime-test-util.h"
31 
32 #define SAMPLES 1028
33 #define TIMES 1000
34 #define TIMES2 100
35 
acquire_mix_streams(pa_mix_info streams[],unsigned nstreams)36 static void acquire_mix_streams(pa_mix_info streams[], unsigned nstreams) {
37     unsigned i;
38 
39     for (i = 0; i < nstreams; i++)
40         streams[i].ptr = pa_memblock_acquire_chunk(&streams[i].chunk);
41 }
42 
release_mix_streams(pa_mix_info streams[],unsigned nstreams)43 static void release_mix_streams(pa_mix_info streams[], unsigned nstreams) {
44     unsigned i;
45 
46     for (i = 0; i < nstreams; i++)
47         pa_memblock_release(streams[i].chunk.memblock);
48 }
49 
run_mix_test(pa_do_mix_func_t func,pa_do_mix_func_t orig_func,int align,int channels,bool correct,bool perf)50 static void run_mix_test(
51         pa_do_mix_func_t func,
52         pa_do_mix_func_t orig_func,
53         int align,
54         int channels,
55         bool correct,
56         bool perf) {
57 
58     PA_DECLARE_ALIGNED(8, int16_t, in0[SAMPLES * 4]) = { 0 };
59     PA_DECLARE_ALIGNED(8, int16_t, in1[SAMPLES * 4]) = { 0 };
60     PA_DECLARE_ALIGNED(8, int16_t, out[SAMPLES * 4]) = { 0 };
61     PA_DECLARE_ALIGNED(8, int16_t, out_ref[SAMPLES * 4]) = { 0 };
62     int16_t *samples0, *samples1;
63     int16_t *samples, *samples_ref;
64     int nsamples;
65     pa_mempool *pool;
66     pa_memchunk c0, c1;
67     pa_mix_info m[2];
68     int i;
69 
70     pa_assert(channels == 1 || channels == 2 || channels == 4);
71 
72     /* Force sample alignment as requested */
73     samples0 = in0 + (8 - align);
74     samples1 = in1 + (8 - align);
75     samples = out + (8 - align);
76     samples_ref = out_ref + (8 - align);
77     nsamples = channels * (SAMPLES - (8 - align));
78 
79     fail_unless((pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true)) != NULL, NULL);
80 
81     pa_random(samples0, nsamples * sizeof(int16_t));
82     c0.memblock = pa_memblock_new_fixed(pool, samples0, nsamples * sizeof(int16_t), false);
83     c0.length = pa_memblock_get_length(c0.memblock);
84     c0.index = 0;
85 
86     pa_random(samples1, nsamples * sizeof(int16_t));
87     c1.memblock = pa_memblock_new_fixed(pool, samples1, nsamples * sizeof(int16_t), false);
88     c1.length = pa_memblock_get_length(c1.memblock);
89     c1.index = 0;
90 
91     m[0].chunk = c0;
92     m[0].volume.channels = channels;
93     for (i = 0; i < channels; i++) {
94         m[0].volume.values[i] = PA_VOLUME_NORM;
95         m[0].linear[i].i = 0x5555;
96     }
97 
98     m[1].chunk = c1;
99     m[1].volume.channels = channels;
100     for (i = 0; i < channels; i++) {
101         m[1].volume.values[i] = PA_VOLUME_NORM;
102         m[1].linear[i].i = 0x6789;
103     }
104 
105     if (correct) {
106         acquire_mix_streams(m, 2);
107         orig_func(m, 2, channels, samples_ref, nsamples * sizeof(int16_t));
108         release_mix_streams(m, 2);
109 
110         acquire_mix_streams(m, 2);
111         func(m, 2, channels, samples, nsamples * sizeof(int16_t));
112         release_mix_streams(m, 2);
113 
114         for (i = 0; i < nsamples; i++) {
115             if (samples[i] != samples_ref[i]) {
116                 pa_log_debug("Correctness test failed: align=%d, channels=%d", align, channels);
117                 pa_log_debug("%d: %hd != %04hd (%hd + %hd)",
118                     i,
119                     samples[i], samples_ref[i],
120                     samples0[i], samples1[i]);
121                 ck_abort();
122             }
123         }
124     }
125 
126     if (perf) {
127         pa_log_debug("Testing %d-channel mixing performance with %d sample alignment", channels, align);
128 
129         PA_RUNTIME_TEST_RUN_START("func", TIMES, TIMES2) {
130             acquire_mix_streams(m, 2);
131             func(m, 2, channels, samples, nsamples * sizeof(int16_t));
132             release_mix_streams(m, 2);
133         } PA_RUNTIME_TEST_RUN_STOP
134 
135         PA_RUNTIME_TEST_RUN_START("orig", TIMES, TIMES2) {
136             acquire_mix_streams(m, 2);
137             orig_func(m, 2, channels, samples_ref, nsamples * sizeof(int16_t));
138             release_mix_streams(m, 2);
139         } PA_RUNTIME_TEST_RUN_STOP
140     }
141 
142     pa_memblock_unref(c0.memblock);
143     pa_memblock_unref(c1.memblock);
144 
145     pa_mempool_unref(pool);
146 }
147 
START_TEST(mix_special_test)148 START_TEST (mix_special_test) {
149     pa_cpu_info cpu_info = { PA_CPU_UNDEFINED, {}, false };
150     pa_do_mix_func_t orig_func, special_func;
151 
152     cpu_info.force_generic_code = true;
153     pa_mix_func_init(&cpu_info);
154     orig_func = pa_get_mix_func(PA_SAMPLE_S16NE);
155 
156     cpu_info.force_generic_code = false;
157     pa_mix_func_init(&cpu_info);
158     special_func = pa_get_mix_func(PA_SAMPLE_S16NE);
159 
160     pa_log_debug("Checking special mix (s16, stereo)");
161     run_mix_test(special_func, orig_func, 7, 2, true, true);
162 
163     pa_log_debug("Checking special mix (s16, 4-channel)");
164     run_mix_test(special_func, orig_func, 7, 4, true, true);
165 
166     pa_log_debug("Checking special mix (s16, mono)");
167     run_mix_test(special_func, orig_func, 7, 1, true, true);
168 }
169 END_TEST
170 
171 #if defined (__arm__) && defined (__linux__) && defined (HAVE_NEON)
START_TEST(mix_neon_test)172 START_TEST (mix_neon_test) {
173     pa_do_mix_func_t orig_func, neon_func;
174     pa_cpu_arm_flag_t flags = 0;
175 
176     pa_cpu_get_arm_flags(&flags);
177 
178     if (!(flags & PA_CPU_ARM_NEON)) {
179         pa_log_info("NEON not supported. Skipping");
180         return;
181     }
182 
183     orig_func = pa_get_mix_func(PA_SAMPLE_S16NE);
184     pa_mix_func_init_neon(flags);
185     neon_func = pa_get_mix_func(PA_SAMPLE_S16NE);
186 
187     pa_log_debug("Checking NEON mix (s16, stereo)");
188     run_mix_test(neon_func, orig_func, 7, 2, true, true);
189 
190     pa_log_debug("Checking NEON mix (s16, 4-channel)");
191     run_mix_test(neon_func, orig_func, 7, 4, true, true);
192 
193     pa_log_debug("Checking NEON mix (s16, mono)");
194     run_mix_test(neon_func, orig_func, 7, 1, true, true);
195 }
196 END_TEST
197 #endif /* defined (__arm__) && defined (__linux__) && defined (HAVE_NEON) */
198 
main(int argc,char * argv[])199 int main(int argc, char *argv[]) {
200     int failed = 0;
201     Suite *s;
202     TCase *tc;
203     SRunner *sr;
204 
205     if (!getenv("MAKE_CHECK"))
206         pa_log_set_level(PA_LOG_DEBUG);
207 
208     s = suite_create("CPU");
209 
210     tc = tcase_create("mix");
211     tcase_add_test(tc, mix_special_test);
212 #if defined (__arm__) && defined (__linux__) && defined (HAVE_NEON)
213     tcase_add_test(tc, mix_neon_test);
214 #endif
215     tcase_set_timeout(tc, 120);
216     suite_add_tcase(s, tc);
217 
218     sr = srunner_create(s);
219     srunner_run_all(sr, CK_NORMAL);
220     failed = srunner_ntests_failed(sr);
221     srunner_free(sr);
222 
223     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
224 }
225