1 /***
2     This file is part of PulseAudio.
3 
4     Copyright 2010 Intel Corporation
5     Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
6     Copyright 2012 Niels Ole Salscheider <niels_ole@salscheider-online.de>
7     Contributor: Alexander E. Patrakov <patrakov@gmail.com>
8     Copyright 2020 Christopher Snowhill <kode54@gmail.com>
9 
10     PulseAudio is free software; you can redistribute it and/or modify
11     it under the terms of the GNU Lesser General Public License as published
12     by the Free Software Foundation; either version 2.1 of the License,
13     or (at your option) any later version.
14 
15     PulseAudio is distributed in the hope that it will be useful, but
16     WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18     General Public License for more details.
19 
20     You should have received a copy of the GNU Lesser General Public License
21     along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22 ***/
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <math.h>
29 
30 #include <fftw3.h>
31 
32 #include <pulse/gccmacro.h>
33 #include <pulse/xmalloc.h>
34 
35 #include <pulsecore/i18n.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/rtpoll.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/ltdl-helper.h>
45 #include <pulsecore/sound-file.h>
46 #include <pulsecore/resampler.h>
47 
48 
49 PA_MODULE_AUTHOR("Christopher Snowhill");
50 PA_MODULE_DESCRIPTION(_("Virtual surround sink"));
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(false);
53 PA_MODULE_USAGE(
54         _("sink_name=<name for the sink> "
55           "sink_properties=<properties for the sink> "
56           "master=<name of sink to filter> "
57           "sink_master=<name of sink to filter> "
58           "format=<sample format> "
59           "rate=<sample rate> "
60           "channels=<number of channels> "
61           "channel_map=<channel map> "
62           "use_volume_sharing=<yes or no> "
63           "force_flat_volume=<yes or no> "
64           "hrir=/path/to/left_hrir.wav "
65           "hrir_left=/path/to/left_hrir.wav "
66           "hrir_right=/path/to/optional/right_hrir.wav "
67           "autoloaded=<set if this module is being loaded automatically> "
68         ));
69 
70 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
71 #define DEFAULT_AUTOLOADED false
72 
73 struct userdata {
74     pa_module *module;
75 
76     bool autoloaded;
77 
78     pa_sink *sink;
79     pa_sink_input *sink_input;
80 
81     pa_memblockq *memblockq_sink;
82 
83     bool auto_desc;
84 
85     size_t fftlen;
86     size_t hrir_samples;
87     size_t inputs;
88 
89     fftwf_plan *p_fw, p_bw;
90     fftwf_complex *f_in, *f_out, **f_ir;
91     float *revspace, *outspace[2], **inspace;
92 };
93 
94 #define BLOCK_SIZE (512)
95 
96 static const char* const valid_modargs[] = {
97     "sink_name",
98     "sink_properties",
99     "master",  /* Will be deprecated. */
100     "sink_master",
101     "format",
102     "rate",
103     "channels",
104     "channel_map",
105     "use_volume_sharing",
106     "force_flat_volume",
107     "autoloaded",
108     "hrir",
109     "hrir_left",
110     "hrir_right",
111     NULL
112 };
113 
114 /* Vector size of 4 floats */
115 #define v_size 4
alloc(size_t x, size_t s)116 static void * alloc(size_t x, size_t s) {
117     size_t f;
118     float *t;
119 
120     f = PA_ROUND_UP(x*s, sizeof(float)*v_size);
121     pa_assert_se(t = fftwf_malloc(f));
122     pa_memzero(t, f);
123 
124     return t;
125 }
126 
sink_input_samples(size_t nbytes)127 static size_t sink_input_samples(size_t nbytes)
128 {
129     return nbytes / 8;
130 }
131 
sink_input_bytes(size_t nsamples)132 static size_t sink_input_bytes(size_t nsamples)
133 {
134     return nsamples * 8;
135 }
136 
sink_samples(const struct userdata *u, size_t nbytes)137 static size_t sink_samples(const struct userdata *u, size_t nbytes)
138 {
139     return nbytes / (u->inputs * 4);
140 }
141 
sink_bytes(const struct userdata *u, size_t nsamples)142 static size_t sink_bytes(const struct userdata *u, size_t nsamples)
143 {
144     return nsamples * (u->inputs * 4);
145 }
146 
147 /* Mirror channels for symmetrical impulse */
mirror_channel(pa_channel_position_t channel)148 static pa_channel_position_t mirror_channel(pa_channel_position_t channel) {
149     switch (channel) {
150         case PA_CHANNEL_POSITION_FRONT_LEFT:
151             return PA_CHANNEL_POSITION_FRONT_RIGHT;
152 
153         case PA_CHANNEL_POSITION_FRONT_RIGHT:
154             return PA_CHANNEL_POSITION_FRONT_LEFT;
155 
156         case PA_CHANNEL_POSITION_REAR_LEFT:
157             return PA_CHANNEL_POSITION_REAR_RIGHT;
158 
159         case PA_CHANNEL_POSITION_REAR_RIGHT:
160             return PA_CHANNEL_POSITION_REAR_LEFT;
161 
162         case PA_CHANNEL_POSITION_SIDE_LEFT:
163             return PA_CHANNEL_POSITION_SIDE_RIGHT;
164 
165         case PA_CHANNEL_POSITION_SIDE_RIGHT:
166             return PA_CHANNEL_POSITION_SIDE_LEFT;
167 
168         case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
169             return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
170 
171         case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
172             return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
173 
174         case PA_CHANNEL_POSITION_TOP_FRONT_LEFT:
175             return PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
176 
177         case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT:
178             return PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
179 
180         case PA_CHANNEL_POSITION_TOP_REAR_LEFT:
181             return PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
182 
183         case PA_CHANNEL_POSITION_TOP_REAR_RIGHT:
184             return PA_CHANNEL_POSITION_TOP_REAR_LEFT;
185 
186         default:
187             return channel;
188     }
189 }
190 
191 /* Normalize the hrir */
normalize_hrir(float * hrir_data, unsigned hrir_samples, unsigned hrir_channels)192 static void normalize_hrir(float * hrir_data, unsigned hrir_samples, unsigned hrir_channels) {
193     /* normalize hrir to avoid audible clipping
194      *
195      * The following heuristic tries to avoid audible clipping. It cannot avoid
196      * clipping in the worst case though, because the scaling factor would
197      * become too large resulting in a too quiet signal.
198      * The idea of the heuristic is to avoid clipping when a single click is
199      * played back on all channels. The scaling factor describes the additional
200      * factor that is necessary to avoid clipping for "normal" signals.
201      *
202      * This algorithm doesn't pretend to be perfect, it's just something that
203      * appears to work (not too quiet, no audible clipping) on the material that
204      * it has been tested on. If you find a real-world example where this
205      * algorithm results in audible clipping, please write a patch that adjusts
206      * the scaling factor constants or improves the algorithm (or if you can't
207      * write a patch, at least report the problem to the PulseAudio mailing list
208      * or bug tracker). */
209 
210     const float scaling_factor = 2.5;
211 
212     float hrir_sum, hrir_max;
213     unsigned i, j;
214 
215     hrir_max = 0;
216     for (i = 0; i < hrir_samples; i++) {
217         hrir_sum = 0;
218         for (j = 0; j < hrir_channels; j++)
219             hrir_sum += fabs(hrir_data[i * hrir_channels + j]);
220 
221         if (hrir_sum > hrir_max)
222             hrir_max = hrir_sum;
223     }
224 
225     for (i = 0; i < hrir_samples; i++) {
226         for (j = 0; j < hrir_channels; j++)
227             hrir_data[i * hrir_channels + j] /= hrir_max * scaling_factor;
228     }
229 }
230 
231 /* Normalize a stereo hrir */
normalize_hrir_stereo(float * hrir_data, float * hrir_right_data, unsigned hrir_samples, unsigned hrir_channels)232 static void normalize_hrir_stereo(float * hrir_data, float * hrir_right_data, unsigned hrir_samples, unsigned hrir_channels) {
233     const float scaling_factor = 2.5;
234 
235     float hrir_sum, hrir_max;
236     unsigned i, j;
237 
238     hrir_max = 0;
239     for (i = 0; i < hrir_samples; i++) {
240         hrir_sum = 0;
241         for (j = 0; j < hrir_channels; j++) {
242             hrir_sum += fabs(hrir_data[i * hrir_channels + j]);
243             hrir_sum += fabs(hrir_right_data[i * hrir_channels + j]);
244         }
245 
246         if (hrir_sum > hrir_max)
247             hrir_max = hrir_sum;
248     }
249 
250     for (i = 0; i < hrir_samples; i++) {
251         for (j = 0; j < hrir_channels; j++) {
252             hrir_data[i * hrir_channels + j] /= hrir_max * scaling_factor;
253             hrir_right_data[i * hrir_channels + j] /= hrir_max * scaling_factor;
254         }
255     }
256 }
257 
258 /* Called from I/O thread context */
sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk)259 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
260     struct userdata *u = PA_SINK(o)->userdata;
261 
262     switch (code) {
263 
264         case PA_SINK_MESSAGE_GET_LATENCY:
265 
266             /* The sink is _put() before the sink input is, so let's
267              * make sure we don't access it in that time. Also, the
268              * sink input is first shut down, the sink second. */
269             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
270                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
271                 *((pa_usec_t*) data) = 0;
272                 return 0;
273             }
274 
275             *((pa_usec_t*) data) =
276 
277                 /* Get the latency of the master sink */
278                 pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
279 
280                 /* Add the latency internal to our sink input on top */
281                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
282 
283             /* Add resampler latency */
284             *((int64_t*) data) += pa_resampler_get_delay_usec(u->sink_input->thread_info.resampler);
285 
286             return 0;
287     }
288 
289     return pa_sink_process_msg(o, code, data, offset, chunk);
290 }
291 
292 /* Called from main context */
sink_set_state_in_main_thread_cb(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause)293 static int sink_set_state_in_main_thread_cb(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause) {
294     struct userdata *u;
295 
296     pa_sink_assert_ref(s);
297     pa_assert_se(u = s->userdata);
298 
299     if (!PA_SINK_IS_LINKED(state) ||
300         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
301         return 0;
302 
303     pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
304     return 0;
305 }
306 
307 /* Called from the IO thread. */
sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause)308 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
309     struct userdata *u;
310 
311     pa_assert(s);
312     pa_assert_se(u = s->userdata);
313 
314     /* When set to running or idle for the first time, request a rewind
315      * of the master sink to make sure we are heard immediately */
316     if (PA_SINK_IS_OPENED(new_state) && s->thread_info.state == PA_SINK_INIT) {
317         pa_log_debug("Requesting rewind due to state change.");
318         pa_sink_input_request_rewind(u->sink_input, 0, false, true, true);
319     }
320 
321     return 0;
322 }
323 
324 /* Called from I/O thread context */
sink_request_rewind_cb(pa_sink *s)325 static void sink_request_rewind_cb(pa_sink *s) {
326     struct userdata *u;
327     size_t nbytes_sink, nbytes_input;
328 
329     pa_sink_assert_ref(s);
330     pa_assert_se(u = s->userdata);
331 
332     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
333         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
334         return;
335 
336     nbytes_sink = s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq_sink);
337     nbytes_input = sink_input_bytes(sink_samples(u, nbytes_sink));
338 
339     /* Just hand this one over to the master sink */
340     pa_sink_input_request_rewind(u->sink_input, nbytes_input, true, false, false);
341 }
342 
343 /* Called from I/O thread context */
sink_update_requested_latency_cb(pa_sink *s)344 static void sink_update_requested_latency_cb(pa_sink *s) {
345     struct userdata *u;
346 
347     pa_sink_assert_ref(s);
348     pa_assert_se(u = s->userdata);
349 
350     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
351         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
352         return;
353 
354     /* Just hand this one over to the master sink */
355     pa_sink_input_set_requested_latency_within_thread(
356             u->sink_input,
357             pa_sink_get_requested_latency_within_thread(s));
358 }
359 
360 /* Called from main context */
sink_set_volume_cb(pa_sink *s)361 static void sink_set_volume_cb(pa_sink *s) {
362     struct userdata *u;
363 
364     pa_sink_assert_ref(s);
365     pa_assert_se(u = s->userdata);
366 
367     if (!PA_SINK_IS_LINKED(s->state) ||
368         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
369         return;
370 
371     pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, true);
372 }
373 
374 /* Called from main context */
sink_set_mute_cb(pa_sink *s)375 static void sink_set_mute_cb(pa_sink *s) {
376     struct userdata *u;
377 
378     pa_sink_assert_ref(s);
379     pa_assert_se(u = s->userdata);
380 
381     if (!PA_SINK_IS_LINKED(s->state) ||
382         !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
383         return;
384 
385     pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
386 }
387 
memblockq_missing(pa_memblockq *bq)388 static size_t memblockq_missing(pa_memblockq *bq) {
389     size_t l, tlength;
390     pa_assert(bq);
391 
392     tlength = pa_memblockq_get_tlength(bq);
393     if ((l = pa_memblockq_get_length(bq)) >= tlength)
394         return 0;
395 
396     l = tlength - l;
397     return l >= pa_memblockq_get_minreq(bq) ? l : 0;
398 }
399 
400 /* Called from I/O thread context */
sink_input_pop_cb(pa_sink_input *i, size_t nbytes_input, pa_memchunk *chunk)401 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes_input, pa_memchunk *chunk) {
402     struct userdata *u;
403     float *src, *dst;
404     int c, ear;
405     size_t s, bytes_missing, fftlen;
406     pa_memchunk tchunk;
407     float fftlen_if, *revspace;
408 
409     pa_sink_input_assert_ref(i);
410     pa_assert(chunk);
411     pa_assert_se(u = i->userdata);
412 
413     /* Hmm, process any rewind request that might be queued up */
414     pa_sink_process_rewind(u->sink, 0);
415 
416     while ((bytes_missing = memblockq_missing(u->memblockq_sink)) != 0) {
417         pa_memchunk nchunk;
418 
419         pa_sink_render(u->sink, bytes_missing, &nchunk);
420         pa_memblockq_push(u->memblockq_sink, &nchunk);
421         pa_memblock_unref(nchunk.memblock);
422     }
423 
424     pa_memblockq_rewind(u->memblockq_sink, sink_bytes(u, u->fftlen - BLOCK_SIZE));
425     pa_memblockq_peek_fixed_size(u->memblockq_sink, sink_bytes(u, u->fftlen), &tchunk);
426 
427     pa_memblockq_drop(u->memblockq_sink, tchunk.length);
428 
429     /* Now tchunk contains enough data to perform the FFT
430      * This should be equal to u->fftlen */
431 
432     chunk->index = 0;
433     chunk->length = sink_input_bytes(BLOCK_SIZE);
434     chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
435 
436     src = pa_memblock_acquire_chunk(&tchunk);
437 
438     for (c = 0; c < u->inputs; c++) {
439         for (s = 0, fftlen = u->fftlen; s < fftlen; s++) {
440             u->inspace[c][s] = src[s * u->inputs + c];
441         }
442     }
443 
444     pa_memblock_release(tchunk.memblock);
445     pa_memblock_unref(tchunk.memblock);
446 
447     fftlen_if = 1.0f / (float)u->fftlen;
448     revspace = u->revspace + u->fftlen - BLOCK_SIZE;
449 
450     pa_memzero(u->outspace[0], BLOCK_SIZE * 4);
451     pa_memzero(u->outspace[1], BLOCK_SIZE * 4);
452 
453     for (c = 0; c < u->inputs; c++) {
454         fftwf_complex *f_in = u->f_in;
455         fftwf_complex *f_out = u->f_out;
456 
457         fftwf_execute(u->p_fw[c]);
458 
459         for (ear = 0; ear < 2; ear++) {
460             fftwf_complex *f_ir = u->f_ir[c * 2 + ear];
461             float *outspace = u->outspace[ear];
462 
463             for (s = 0, fftlen = u->fftlen / 2 + 1; s < fftlen; s++) {
464                 float re = f_ir[s][0] * f_in[s][0] - f_ir[s][1] * f_in[s][1];
465                 float im = f_ir[s][1] * f_in[s][0] + f_ir[s][0] * f_in[s][1];
466                 f_out[s][0] = re;
467                 f_out[s][1] = im;
468             }
469 
470             fftwf_execute(u->p_bw);
471 
472             for (s = 0, fftlen = BLOCK_SIZE; s < fftlen; ++s)
473                 outspace[s] += revspace[s] * fftlen_if;
474         }
475     }
476 
477     dst = pa_memblock_acquire_chunk(chunk);
478 
479     for (s = 0, fftlen = BLOCK_SIZE; s < fftlen; s++) {
480         float output;
481         float *outspace = u->outspace[0];
482 
483         output = outspace[s];
484         if (output < -1.0) output = -1.0;
485         if (output > 1.0) output = 1.0;
486         dst[s * 2 + 0] = output;
487 
488         outspace = u->outspace[1];
489 
490         output = outspace[s];
491         if (output < -1.0) output = -1.0;
492         if (output > 1.0) output = 1.0;
493         dst[s * 2 + 1] = output;
494     }
495 
496     pa_memblock_release(chunk->memblock);
497 
498     return 0;
499 }
500 
501 /* Called from I/O thread context */
sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes_input)502 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes_input) {
503     struct userdata *u;
504     size_t amount = 0;
505     size_t nbytes_sink;
506 
507     pa_sink_input_assert_ref(i);
508     pa_assert_se(u = i->userdata);
509 
510     nbytes_sink = sink_bytes(u, sink_input_samples(nbytes_input));
511 
512     if (u->sink->thread_info.rewind_nbytes > 0) {
513         size_t max_rewrite;
514 
515         max_rewrite = nbytes_sink + pa_memblockq_get_length(u->memblockq_sink);
516         amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
517         u->sink->thread_info.rewind_nbytes = 0;
518 
519         if (amount > 0) {
520             pa_memblockq_seek(u->memblockq_sink, - (int64_t) amount, PA_SEEK_RELATIVE, true);
521         }
522     }
523 
524     pa_sink_process_rewind(u->sink, amount);
525 
526     pa_memblockq_rewind(u->memblockq_sink, nbytes_sink);
527 }
528 
529 /* Called from I/O thread context */
sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes_input)530 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes_input) {
531     struct userdata *u;
532     size_t nbytes_sink, nbytes_memblockq;
533 
534     pa_sink_input_assert_ref(i);
535     pa_assert_se(u = i->userdata);
536 
537     nbytes_sink = sink_bytes(u, sink_input_samples(nbytes_input));
538     nbytes_memblockq = sink_bytes(u, sink_input_samples(nbytes_input) + u->fftlen);
539 
540     /* FIXME: Too small max_rewind:
541      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
542     pa_memblockq_set_maxrewind(u->memblockq_sink, nbytes_memblockq);
543     pa_sink_set_max_rewind_within_thread(u->sink, nbytes_sink);
544 }
545 
546 /* Called from I/O thread context */
sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes_input)547 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes_input) {
548     struct userdata *u;
549 
550     size_t nbytes_sink;
551 
552     pa_sink_input_assert_ref(i);
553     pa_assert_se(u = i->userdata);
554 
555     nbytes_sink = sink_bytes(u, sink_input_samples(nbytes_input));
556 
557     nbytes_sink = PA_ROUND_UP(nbytes_sink, sink_bytes(u, BLOCK_SIZE));
558     pa_sink_set_max_request_within_thread(u->sink, nbytes_sink);
559 }
560 
561 /* Called from I/O thread context */
sink_input_update_sink_latency_range_cb(pa_sink_input *i)562 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
563     struct userdata *u;
564 
565     pa_sink_input_assert_ref(i);
566     pa_assert_se(u = i->userdata);
567 
568     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
569 }
570 
571 /* Called from I/O thread context */
sink_input_update_sink_fixed_latency_cb(pa_sink_input *i)572 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
573     struct userdata *u;
574 
575     pa_sink_input_assert_ref(i);
576     pa_assert_se(u = i->userdata);
577 
578     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
579 }
580 
581 /* Called from I/O thread context */
sink_input_detach_cb(pa_sink_input *i)582 static void sink_input_detach_cb(pa_sink_input *i) {
583     struct userdata *u;
584 
585     pa_sink_input_assert_ref(i);
586     pa_assert_se(u = i->userdata);
587 
588     if (PA_SINK_IS_LINKED(u->sink->thread_info.state))
589         pa_sink_detach_within_thread(u->sink);
590 
591     pa_sink_set_rtpoll(u->sink, NULL);
592 }
593 
594 /* Called from I/O thread context */
sink_input_attach_cb(pa_sink_input *i)595 static void sink_input_attach_cb(pa_sink_input *i) {
596     struct userdata *u;
597     size_t max_request;
598 
599     pa_sink_input_assert_ref(i);
600     pa_assert_se(u = i->userdata);
601 
602     pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
603     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
604 
605     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
606 
607     max_request = sink_bytes(u, sink_input_samples(pa_sink_input_get_max_request(i)));
608     max_request = PA_ROUND_UP(max_request, sink_bytes(u, BLOCK_SIZE));
609     pa_sink_set_max_request_within_thread(u->sink, max_request);
610 
611     /* FIXME: Too small max_rewind:
612      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
613     pa_sink_set_max_rewind_within_thread(u->sink, sink_bytes(u, sink_input_samples(pa_sink_input_get_max_rewind(i))));
614 
615     pa_sink_attach_within_thread(u->sink);
616 }
617 
618 /* Called from main context */
sink_input_kill_cb(pa_sink_input *i)619 static void sink_input_kill_cb(pa_sink_input *i) {
620     struct userdata *u;
621 
622     pa_sink_input_assert_ref(i);
623     pa_assert_se(u = i->userdata);
624 
625     /* The order here matters! We first kill the sink input, followed
626      * by the sink. That means the sink callbacks must be protected
627      * against an unconnected sink input! */
628     pa_sink_input_cork(u->sink_input, true);
629     pa_sink_input_unlink(u->sink_input);
630     pa_sink_unlink(u->sink);
631 
632     pa_sink_input_unref(u->sink_input);
633     u->sink_input = NULL;
634 
635     pa_sink_unref(u->sink);
636     u->sink = NULL;
637 
638     pa_module_unload_request(u->module, true);
639 }
640 
641 /* Called from main context */
sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest)642 static bool sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
643     struct userdata *u;
644 
645     pa_sink_input_assert_ref(i);
646     pa_assert_se(u = i->userdata);
647 
648     if (u->autoloaded)
649         return false;
650 
651     return u->sink != dest;
652 }
653 
654 /* Called from main context */
sink_input_moving_cb(pa_sink_input *i, pa_sink *dest)655 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
656     struct userdata *u;
657 
658     pa_sink_input_assert_ref(i);
659     pa_assert_se(u = i->userdata);
660 
661     if (dest) {
662         pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
663         pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
664     } else
665         pa_sink_set_asyncmsgq(u->sink, NULL);
666 
667     if (u->auto_desc && dest) {
668         const char *z;
669         pa_proplist *pl;
670 
671         pl = pa_proplist_new();
672         z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
673         pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s",
674                          pa_proplist_gets(u->sink->proplist, "device.vsurroundsink.name"), z ? z : dest->name);
675 
676         pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
677         pa_proplist_free(pl);
678     }
679 }
680 
681 /* Called from main context */
sink_input_volume_changed_cb(pa_sink_input *i)682 static void sink_input_volume_changed_cb(pa_sink_input *i) {
683     struct userdata *u;
684 
685     pa_sink_input_assert_ref(i);
686     pa_assert_se(u = i->userdata);
687 
688     pa_sink_volume_changed(u->sink, &i->volume);
689 }
690 
691 /* Called from main context */
sink_input_mute_changed_cb(pa_sink_input *i)692 static void sink_input_mute_changed_cb(pa_sink_input *i) {
693     struct userdata *u;
694 
695     pa_sink_input_assert_ref(i);
696     pa_assert_se(u = i->userdata);
697 
698     pa_sink_mute_changed(u->sink, i->muted);
699 }
700 
pa__init(pa_module*m)701 int pa__init(pa_module*m) {
702     struct userdata *u;
703     pa_sample_spec ss_input, ss_output;
704     pa_channel_map map_output;
705     pa_modargs *ma;
706     const char *master_name;
707     const char *hrir_left_file;
708     const char *hrir_right_file;
709     pa_sink *master=NULL;
710     pa_sink_input_new_data sink_input_data;
711     pa_sink_new_data sink_data;
712     bool use_volume_sharing = true;
713     bool force_flat_volume = false;
714     pa_memchunk silence;
715     const char* z;
716     unsigned i, j, ear, found_channel_left, found_channel_right;
717 
718     pa_sample_spec ss;
719     pa_channel_map map;
720 
721     float *hrir_data=NULL, *hrir_right_data=NULL;
722     float *hrir_temp_data;
723     size_t hrir_samples;
724     size_t hrir_copied_length, hrir_total_length;
725     int hrir_channels;
726     int fftlen;
727 
728     float *impulse_temp=NULL;
729 
730     unsigned *mapping_left=NULL;
731     unsigned *mapping_right=NULL;
732 
733     fftwf_plan p;
734 
735     pa_channel_map hrir_map, hrir_right_map;
736 
737     pa_sample_spec hrir_left_temp_ss;
738     pa_memchunk hrir_left_temp_chunk, hrir_left_temp_chunk_resampled;
739     pa_resampler *resampler;
740 
741 
742     pa_sample_spec hrir_right_temp_ss;
743     pa_memchunk hrir_right_temp_chunk, hrir_right_temp_chunk_resampled;
744 
745     pa_assert(m);
746 
747     hrir_left_temp_chunk.memblock = NULL;
748     hrir_left_temp_chunk_resampled.memblock = NULL;
749     hrir_right_temp_chunk.memblock = NULL;
750     hrir_right_temp_chunk_resampled.memblock = NULL;
751 
752     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
753         pa_log("Failed to parse module arguments.");
754         goto fail;
755     }
756 
757     master_name = pa_modargs_get_value(ma, "sink_master", NULL);
758     if (!master_name) {
759         master_name = pa_modargs_get_value(ma, "master", NULL);
760         if (master_name)
761             pa_log_warn("The 'master' module argument is deprecated and may be removed in the future, "
762                         "please use the 'sink_master' argument instead.");
763     }
764 
765     if (!(master = pa_namereg_get(m->core, master_name, PA_NAMEREG_SINK))) {
766         pa_log("Master sink not found");
767         goto fail;
768     }
769 
770     hrir_left_file = pa_modargs_get_value(ma, "hrir_left", NULL);
771     if (!hrir_left_file) {
772         hrir_left_file = pa_modargs_get_value(ma, "hrir", NULL);
773         if (!hrir_left_file) {
774             pa_log("Either the 'hrir' or 'hrir_left' module arguments are required.");
775             goto fail;
776         }
777     }
778 
779     hrir_right_file = pa_modargs_get_value(ma, "hrir_right", NULL);
780 
781     pa_assert(master);
782 
783     if (pa_sound_file_load(master->core->mempool, hrir_left_file, &hrir_left_temp_ss, &hrir_map, &hrir_left_temp_chunk, NULL) < 0) {
784         pa_log("Cannot load hrir file.");
785         goto fail;
786     }
787 
788     if (hrir_right_file) {
789         if (pa_sound_file_load(master->core->mempool, hrir_right_file, &hrir_right_temp_ss, &hrir_right_map, &hrir_right_temp_chunk, NULL) < 0) {
790             pa_log("Cannot load hrir_right file.");
791             goto fail;
792         }
793         if (!pa_sample_spec_equal(&hrir_left_temp_ss, &hrir_right_temp_ss)) {
794             pa_log("Both hrir_left and hrir_right must have the same sample format");
795             goto fail;
796         }
797         if (!pa_channel_map_equal(&hrir_map, &hrir_right_map)) {
798             pa_log("Both hrir_left and hrir_right must have the same channel layout");
799             goto fail;
800         }
801     }
802 
803     ss_input.format = PA_SAMPLE_FLOAT32NE;
804     ss_input.rate = master->sample_spec.rate;
805     ss_input.channels = hrir_left_temp_ss.channels;
806 
807     ss = ss_input;
808     map = hrir_map;
809     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
810         pa_log("Invalid sample format specification or channel map");
811         goto fail;
812     }
813 
814     ss.format = PA_SAMPLE_FLOAT32NE;
815     ss_input.rate = ss.rate;
816     ss_input.channels = ss.channels;
817 
818     ss_output = ss_input;
819     ss_output.channels = 2;
820 
821     if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
822         pa_log("use_volume_sharing= expects a boolean argument");
823         goto fail;
824     }
825 
826     if (pa_modargs_get_value_boolean(ma, "force_flat_volume", &force_flat_volume) < 0) {
827         pa_log("force_flat_volume= expects a boolean argument");
828         goto fail;
829     }
830 
831     if (use_volume_sharing && force_flat_volume) {
832         pa_log("Flat volume can't be forced when using volume sharing.");
833         goto fail;
834     }
835 
836     pa_channel_map_init_stereo(&map_output);
837 
838     u = pa_xnew0(struct userdata, 1);
839     u->module = m;
840     m->userdata = u;
841 
842     /* Create sink */
843     pa_sink_new_data_init(&sink_data);
844     sink_data.driver = __FILE__;
845     sink_data.module = m;
846     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
847         sink_data.name = pa_sprintf_malloc("%s.vsurroundsink", master->name);
848     pa_sink_new_data_set_sample_spec(&sink_data, &ss_input);
849     pa_sink_new_data_set_channel_map(&sink_data, &map);
850     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
851     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
852     pa_proplist_sets(sink_data.proplist, "device.vsurroundsink.name", sink_data.name);
853 
854     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
855         pa_log("Invalid properties");
856         pa_sink_new_data_done(&sink_data);
857         goto fail;
858     }
859 
860     u->autoloaded = DEFAULT_AUTOLOADED;
861     if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
862         pa_log("Failed to parse autoloaded value");
863         goto fail;
864     }
865 
866     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
867         z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
868         pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s", sink_data.name, z ? z : master->name);
869     }
870 
871     u->sink = pa_sink_new(m->core, &sink_data, (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))
872                                                | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
873     pa_sink_new_data_done(&sink_data);
874 
875     if (!u->sink) {
876         pa_log("Failed to create sink.");
877         goto fail;
878     }
879 
880     u->sink->parent.process_msg = sink_process_msg_cb;
881     u->sink->set_state_in_main_thread = sink_set_state_in_main_thread_cb;
882     u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
883     u->sink->update_requested_latency = sink_update_requested_latency_cb;
884     u->sink->request_rewind = sink_request_rewind_cb;
885     pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
886     if (!use_volume_sharing) {
887         pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
888         pa_sink_enable_decibel_volume(u->sink, true);
889     }
890     /* Normally this flag would be enabled automatically but we can force it. */
891     if (force_flat_volume)
892         u->sink->flags |= PA_SINK_FLAT_VOLUME;
893     u->sink->userdata = u;
894 
895     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
896 
897     /* Create sink input */
898     pa_sink_input_new_data_init(&sink_input_data);
899     sink_input_data.driver = __FILE__;
900     sink_input_data.module = m;
901     pa_sink_input_new_data_set_sink(&sink_input_data, master, false, true);
902     sink_input_data.origin_sink = u->sink;
903     pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Surround Sink Stream from %s", pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
904     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
905     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss_output);
906     pa_sink_input_new_data_set_channel_map(&sink_input_data, &map_output);
907 
908     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
909     pa_sink_input_new_data_done(&sink_input_data);
910 
911     if (!u->sink_input)
912         goto fail;
913 
914     u->sink_input->pop = sink_input_pop_cb;
915     u->sink_input->process_rewind = sink_input_process_rewind_cb;
916     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
917     u->sink_input->update_max_request = sink_input_update_max_request_cb;
918     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
919     u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
920     u->sink_input->kill = sink_input_kill_cb;
921     u->sink_input->attach = sink_input_attach_cb;
922     u->sink_input->detach = sink_input_detach_cb;
923     u->sink_input->may_move_to = sink_input_may_move_to_cb;
924     u->sink_input->moving = sink_input_moving_cb;
925     u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
926     u->sink_input->mute_changed = sink_input_mute_changed_cb;
927     u->sink_input->userdata = u;
928 
929     u->sink->input_to_master = u->sink_input;
930 
931     pa_sink_input_get_silence(u->sink_input, &silence);
932 
933     resampler = pa_resampler_new(u->sink->core->mempool, &hrir_left_temp_ss, &hrir_map, &ss_input, &hrir_map, u->sink->core->lfe_crossover_freq,
934                                  PA_RESAMPLER_SRC_SINC_BEST_QUALITY, PA_RESAMPLER_NO_REMAP);
935 
936     hrir_samples = hrir_left_temp_chunk.length / pa_frame_size(&hrir_left_temp_ss) * ss_input.rate / hrir_left_temp_ss.rate;
937 
938     hrir_total_length = hrir_samples * pa_frame_size(&ss_input);
939     hrir_channels = ss_input.channels;
940 
941     hrir_data = (float *) pa_xmalloc(hrir_total_length);
942     hrir_copied_length = 0;
943 
944     u->hrir_samples = hrir_samples;
945     u->inputs = hrir_channels;
946 
947     /* add silence to the hrir until we get enough samples out of the resampler */
948     while (hrir_copied_length < hrir_total_length) {
949         pa_resampler_run(resampler, &hrir_left_temp_chunk, &hrir_left_temp_chunk_resampled);
950         if (hrir_left_temp_chunk.memblock != hrir_left_temp_chunk_resampled.memblock) {
951             /* Silence input block */
952             pa_silence_memblock(hrir_left_temp_chunk.memblock, &hrir_left_temp_ss);
953         }
954 
955         if (hrir_left_temp_chunk_resampled.memblock) {
956             /* Copy hrir data */
957             hrir_temp_data = (float *) pa_memblock_acquire(hrir_left_temp_chunk_resampled.memblock);
958 
959             if (hrir_total_length - hrir_copied_length >= hrir_left_temp_chunk_resampled.length) {
960                 memcpy(hrir_data + hrir_copied_length, hrir_temp_data, hrir_left_temp_chunk_resampled.length);
961                 hrir_copied_length += hrir_left_temp_chunk_resampled.length;
962             } else {
963                 memcpy(hrir_data + hrir_copied_length, hrir_temp_data, hrir_total_length - hrir_copied_length);
964                 hrir_copied_length = hrir_total_length;
965             }
966 
967             pa_memblock_release(hrir_left_temp_chunk_resampled.memblock);
968             pa_memblock_unref(hrir_left_temp_chunk_resampled.memblock);
969             hrir_left_temp_chunk_resampled.memblock = NULL;
970         }
971     }
972 
973     pa_memblock_unref(hrir_left_temp_chunk.memblock);
974     hrir_left_temp_chunk.memblock = NULL;
975 
976     if (hrir_right_file) {
977         pa_resampler_reset(resampler);
978 
979         hrir_right_data = (float *) pa_xmalloc(hrir_total_length);
980         hrir_copied_length = 0;
981 
982         while (hrir_copied_length < hrir_total_length) {
983             pa_resampler_run(resampler, &hrir_right_temp_chunk, &hrir_right_temp_chunk_resampled);
984             if (hrir_right_temp_chunk.memblock != hrir_right_temp_chunk_resampled.memblock) {
985                 /* Silence input block */
986                 pa_silence_memblock(hrir_right_temp_chunk.memblock, &hrir_right_temp_ss);
987             }
988 
989             if (hrir_right_temp_chunk_resampled.memblock) {
990                 /* Copy hrir data */
991                 hrir_temp_data = (float *) pa_memblock_acquire(hrir_right_temp_chunk_resampled.memblock);
992 
993                 if (hrir_total_length - hrir_copied_length >= hrir_right_temp_chunk_resampled.length) {
994                     memcpy(hrir_right_data + hrir_copied_length, hrir_temp_data, hrir_right_temp_chunk_resampled.length);
995                     hrir_copied_length += hrir_right_temp_chunk_resampled.length;
996                 } else {
997                     memcpy(hrir_right_data + hrir_copied_length, hrir_temp_data, hrir_total_length - hrir_copied_length);
998                     hrir_copied_length = hrir_total_length;
999                 }
1000 
1001                 pa_memblock_release(hrir_right_temp_chunk_resampled.memblock);
1002                 pa_memblock_unref(hrir_right_temp_chunk_resampled.memblock);
1003                 hrir_right_temp_chunk_resampled.memblock = NULL;
1004             }
1005         }
1006 
1007         pa_memblock_unref(hrir_right_temp_chunk.memblock);
1008         hrir_right_temp_chunk.memblock = NULL;
1009     }
1010 
1011     pa_resampler_free(resampler);
1012 
1013     if (hrir_right_data)
1014         normalize_hrir_stereo(hrir_data, hrir_right_data, hrir_samples, hrir_channels);
1015     else
1016         normalize_hrir(hrir_data, hrir_samples, hrir_channels);
1017 
1018     /* create mapping between hrir and input */
1019     mapping_left = (unsigned *) pa_xnew0(unsigned, hrir_channels);
1020     mapping_right = (unsigned *) pa_xnew0(unsigned, hrir_channels);
1021     for (i = 0; i < map.channels; i++) {
1022         found_channel_left = 0;
1023         found_channel_right = 0;
1024 
1025         for (j = 0; j < hrir_map.channels; j++) {
1026             if (hrir_map.map[j] == map.map[i]) {
1027                 mapping_left[i] = j;
1028                 found_channel_left = 1;
1029             }
1030 
1031             if (hrir_map.map[j] == mirror_channel(map.map[i])) {
1032                 mapping_right[i] = j;
1033                 found_channel_right = 1;
1034             }
1035         }
1036 
1037         if (!found_channel_left) {
1038             pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(map.map[i]));
1039             goto fail;
1040         }
1041         if (!found_channel_right) {
1042             pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(mirror_channel(map.map[i])));
1043             goto fail;
1044         }
1045     }
1046 
1047     fftlen = (hrir_samples + BLOCK_SIZE + 1); /* Grow a bit for overlap */
1048     {
1049         /* Round up to a power of two */
1050         int pow = 1;
1051         while (fftlen > 2) { pow++; fftlen /= 2; }
1052         fftlen = 2 << pow;
1053     }
1054 
1055     u->fftlen = fftlen;
1056 
1057     u->f_in = (fftwf_complex*) alloc(sizeof(fftwf_complex), (fftlen/2+1));
1058     u->f_out = (fftwf_complex*) alloc(sizeof(fftwf_complex), (fftlen/2+1));
1059 
1060     u->f_ir = (fftwf_complex**) alloc(sizeof(fftwf_complex*), (hrir_channels*2));
1061     for (i = 0, j = hrir_channels*2; i < j; i++)
1062         u->f_ir[i] = (fftwf_complex*) alloc(sizeof(fftwf_complex), (fftlen/2+1));
1063 
1064     u->revspace = (float*) alloc(sizeof(float), fftlen);
1065 
1066     u->outspace[0] = (float*) alloc(sizeof(float), BLOCK_SIZE);
1067     u->outspace[1] = (float*) alloc(sizeof(float), BLOCK_SIZE);
1068 
1069     u->inspace = (float**) alloc(sizeof(float*), hrir_channels);
1070     for (i = 0; i < hrir_channels; i++)
1071         u->inspace[i] = (float*) alloc(sizeof(float), fftlen);
1072 
1073     u->p_fw = (fftwf_plan*) alloc(sizeof(fftwf_plan), hrir_channels);
1074     for (i = 0; i < hrir_channels; i++)
1075         pa_assert_se(u->p_fw[i] = fftwf_plan_dft_r2c_1d(fftlen, u->inspace[i], u->f_in, FFTW_ESTIMATE));
1076 
1077     pa_assert_se(u->p_bw = fftwf_plan_dft_c2r_1d(fftlen, u->f_out, u->revspace, FFTW_ESTIMATE));
1078 
1079     impulse_temp = (float*) alloc(sizeof(float), fftlen);
1080 
1081     if (hrir_right_data) {
1082         for (i = 0; i < hrir_channels; i++) {
1083             for (ear = 0; ear < 2; ear++) {
1084                 size_t index = i * 2 + ear;
1085                 size_t impulse_index = mapping_left[i];
1086                 float *impulse = (ear == 0) ? hrir_data : hrir_right_data;
1087                 for (j = 0; j < hrir_samples; j++) {
1088                     impulse_temp[j] = impulse[j * hrir_channels + impulse_index];
1089                 }
1090 
1091                 p = fftwf_plan_dft_r2c_1d(fftlen, impulse_temp, u->f_ir[index], FFTW_ESTIMATE);
1092                 if (p) {
1093                     fftwf_execute(p);
1094                     fftwf_destroy_plan(p);
1095                 } else {
1096                     pa_log("fftw plan creation failed for %s ear speaker index %d", (ear == 0) ? "left" : "right", i);
1097                     goto fail;
1098                 }
1099             }
1100         }
1101     } else {
1102         for (i = 0; i < hrir_channels; i++) {
1103             for (ear = 0; ear < 2; ear++) {
1104                 size_t index = i * 2 + ear;
1105                 size_t impulse_index = (ear == 0) ? mapping_left[i] : mapping_right[i];
1106                 for (j = 0; j < hrir_samples; j++) {
1107                     impulse_temp[j] = hrir_data[j * hrir_channels + impulse_index];
1108                 }
1109 
1110                 p = fftwf_plan_dft_r2c_1d(fftlen, impulse_temp, u->f_ir[index], FFTW_ESTIMATE);
1111                 if (p) {
1112                     fftwf_execute(p);
1113                     fftwf_destroy_plan(p);
1114                 } else {
1115                     pa_log("fftw plan creation failed for %s ear speaker index %d", (ear == 0) ? "left" : "right", i);
1116                     goto fail;
1117                 }
1118             }
1119         }
1120     }
1121 
1122     pa_xfree(impulse_temp);
1123 
1124     pa_xfree(hrir_data);
1125     if (hrir_right_data)
1126         pa_xfree(hrir_right_data);
1127 
1128     pa_xfree(mapping_left);
1129     pa_xfree(mapping_right);
1130 
1131     u->memblockq_sink = pa_memblockq_new("module-virtual-surround-sink memblockq (input)", 0, MEMBLOCKQ_MAXLENGTH, sink_bytes(u, BLOCK_SIZE), &ss_input, 0, 0, sink_bytes(u, u->fftlen), &silence);
1132     pa_memblock_unref(silence.memblock);
1133 
1134     pa_memblockq_seek(u->memblockq_sink, sink_bytes(u, u->fftlen - BLOCK_SIZE), PA_SEEK_RELATIVE, false);
1135     pa_memblockq_flush_read(u->memblockq_sink);
1136 
1137     pa_sink_put(u->sink);
1138     pa_sink_input_put(u->sink_input);
1139 
1140     pa_modargs_free(ma);
1141 
1142     return 0;
1143 
1144 fail:
1145     if (impulse_temp)
1146         pa_xfree(impulse_temp);
1147 
1148     if (mapping_left)
1149         pa_xfree(mapping_left);
1150 
1151     if (mapping_right)
1152         pa_xfree(mapping_right);
1153 
1154     if (hrir_data)
1155         pa_xfree(hrir_data);
1156 
1157     if (hrir_right_data)
1158         pa_xfree(hrir_right_data);
1159 
1160     if (hrir_left_temp_chunk.memblock)
1161         pa_memblock_unref(hrir_left_temp_chunk.memblock);
1162 
1163     if (hrir_left_temp_chunk_resampled.memblock)
1164         pa_memblock_unref(hrir_left_temp_chunk_resampled.memblock);
1165 
1166     if (hrir_right_temp_chunk.memblock)
1167         pa_memblock_unref(hrir_right_temp_chunk.memblock);
1168 
1169     if (hrir_right_temp_chunk_resampled.memblock)
1170         pa_memblock_unref(hrir_right_temp_chunk_resampled.memblock);
1171 
1172     if (ma)
1173         pa_modargs_free(ma);
1174 
1175     pa__done(m);
1176 
1177     return -1;
1178 }
1179 
pa__get_n_used(pa_module *m)1180 int pa__get_n_used(pa_module *m) {
1181     struct userdata *u;
1182 
1183     pa_assert(m);
1184     pa_assert_se(u = m->userdata);
1185 
1186     return pa_sink_linked_by(u->sink);
1187 }
1188 
pa__done(pa_module*m)1189 void pa__done(pa_module*m) {
1190     size_t i, j;
1191     struct userdata *u;
1192 
1193     pa_assert(m);
1194 
1195     if (!(u = m->userdata))
1196         return;
1197 
1198     /* See comments in sink_input_kill_cb() above regarding
1199      * destruction order! */
1200 
1201     if (u->sink_input)
1202         pa_sink_input_unlink(u->sink_input);
1203 
1204     if (u->sink)
1205         pa_sink_unlink(u->sink);
1206 
1207     if (u->sink_input)
1208         pa_sink_input_unref(u->sink_input);
1209 
1210     if (u->sink)
1211         pa_sink_unref(u->sink);
1212 
1213     if (u->memblockq_sink)
1214         pa_memblockq_free(u->memblockq_sink);
1215 
1216     if (u->p_fw) {
1217         for (i = 0, j = u->inputs; i < j; i++) {
1218             if (u->p_fw[i])
1219                 fftwf_destroy_plan(u->p_fw[i]);
1220         }
1221         fftwf_free(u->p_fw);
1222     }
1223 
1224     if (u->p_bw)
1225         fftwf_destroy_plan(u->p_bw);
1226 
1227     if (u->f_ir) {
1228         for (i = 0, j = u->inputs * 2; i < j; i++) {
1229             if (u->f_ir[i])
1230                 fftwf_free(u->f_ir[i]);
1231         }
1232         fftwf_free(u->f_ir);
1233     }
1234 
1235     if (u->f_out)
1236         fftwf_free(u->f_out);
1237 
1238     if (u->f_in)
1239         fftwf_free(u->f_in);
1240 
1241     if (u->revspace)
1242         fftwf_free(u->revspace);
1243 
1244     if (u->outspace[0])
1245         fftwf_free(u->outspace[0]);
1246     if (u->outspace[1])
1247         fftwf_free(u->outspace[1]);
1248 
1249     if (u->inspace) {
1250         for (i = 0, j = u->inputs; i < j; i++) {
1251             if (u->inspace[i])
1252                 fftwf_free(u->inspace[i]);
1253         }
1254         fftwf_free(u->inspace);
1255     }
1256 
1257     pa_xfree(u);
1258 }
1259