]> code.delx.au - pulseaudio/blob - src/pulsecore/resampler.c
resampler: Drop redundant assignment in convert_from_work_format()
[pulseaudio] / src / pulsecore / resampler.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
30 #endif
31
32 #ifdef HAVE_SPEEX
33 #include <speex/speex_resampler.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42 #include <pulsecore/core-util.h>
43 #include "ffmpeg/avcodec.h"
44
45 #include "resampler.h"
46
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
49
50 struct pa_resampler {
51 pa_resample_method_t method;
52 pa_resample_flags_t flags;
53
54 pa_sample_spec i_ss, o_ss;
55 pa_channel_map i_cm, o_cm;
56 size_t i_fz, o_fz, w_sz;
57 pa_mempool *mempool;
58
59 pa_memchunk to_work_format_buf;
60 pa_memchunk remap_buf;
61 pa_memchunk resample_buf;
62 pa_memchunk from_work_format_buf;
63 unsigned to_work_format_buf_samples;
64 size_t remap_buf_size;
65 unsigned resample_buf_samples;
66 unsigned from_work_format_buf_samples;
67 pa_bool_t remap_buf_contains_leftover_data;
68
69 pa_sample_format_t work_format;
70
71 pa_convert_func_t to_work_format_func;
72 pa_convert_func_t from_work_format_func;
73
74 pa_remap_t remap;
75 pa_bool_t map_required;
76
77 void (*impl_free)(pa_resampler *r);
78 void (*impl_update_rates)(pa_resampler *r);
79 void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
80 void (*impl_reset)(pa_resampler *r);
81
82 struct { /* data specific to the trivial resampler */
83 unsigned o_counter;
84 unsigned i_counter;
85 } trivial;
86
87 struct { /* data specific to the peak finder pseudo resampler */
88 unsigned o_counter;
89 unsigned i_counter;
90
91 float max_f[PA_CHANNELS_MAX];
92 int16_t max_i[PA_CHANNELS_MAX];
93
94 } peaks;
95
96 #ifdef HAVE_LIBSAMPLERATE
97 struct { /* data specific to libsamplerate */
98 SRC_STATE *state;
99 } src;
100 #endif
101
102 #ifdef HAVE_SPEEX
103 struct { /* data specific to speex */
104 SpeexResamplerState* state;
105 } speex;
106 #endif
107
108 struct { /* data specific to ffmpeg */
109 struct AVResampleContext *state;
110 pa_memchunk buf[PA_CHANNELS_MAX];
111 } ffmpeg;
112 };
113
114 static int copy_init(pa_resampler *r);
115 static int trivial_init(pa_resampler*r);
116 #ifdef HAVE_SPEEX
117 static int speex_init(pa_resampler*r);
118 #endif
119 static int ffmpeg_init(pa_resampler*r);
120 static int peaks_init(pa_resampler*r);
121 #ifdef HAVE_LIBSAMPLERATE
122 static int libsamplerate_init(pa_resampler*r);
123 #endif
124
125 static void calc_map_table(pa_resampler *r);
126
127 static int (* const init_table[])(pa_resampler*r) = {
128 #ifdef HAVE_LIBSAMPLERATE
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
131 [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
133 [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
134 #else
135 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
136 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
137 [PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
138 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
139 [PA_RESAMPLER_SRC_LINEAR] = NULL,
140 #endif
141 [PA_RESAMPLER_TRIVIAL] = trivial_init,
142 #ifdef HAVE_SPEEX
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
165 #else
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
188 #endif
189 [PA_RESAMPLER_FFMPEG] = ffmpeg_init,
190 [PA_RESAMPLER_AUTO] = NULL,
191 [PA_RESAMPLER_COPY] = copy_init,
192 [PA_RESAMPLER_PEAKS] = peaks_init,
193 };
194
195 pa_resampler* pa_resampler_new(
196 pa_mempool *pool,
197 const pa_sample_spec *a,
198 const pa_channel_map *am,
199 const pa_sample_spec *b,
200 const pa_channel_map *bm,
201 pa_resample_method_t method,
202 pa_resample_flags_t flags) {
203
204 pa_resampler *r = NULL;
205
206 pa_assert(pool);
207 pa_assert(a);
208 pa_assert(b);
209 pa_assert(pa_sample_spec_valid(a));
210 pa_assert(pa_sample_spec_valid(b));
211 pa_assert(method >= 0);
212 pa_assert(method < PA_RESAMPLER_MAX);
213
214 /* Fix method */
215
216 if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && a->rate == b->rate) {
217 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
218 method = PA_RESAMPLER_COPY;
219 }
220
221 if (!pa_resample_method_supported(method)) {
222 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
223 method = PA_RESAMPLER_AUTO;
224 }
225
226 if (method == PA_RESAMPLER_FFMPEG && (flags & PA_RESAMPLER_VARIABLE_RATE)) {
227 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
228 method = PA_RESAMPLER_AUTO;
229 }
230
231 if (method == PA_RESAMPLER_COPY && ((flags & PA_RESAMPLER_VARIABLE_RATE) || a->rate != b->rate)) {
232 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
233 method = PA_RESAMPLER_AUTO;
234 }
235
236 if (method == PA_RESAMPLER_AUTO) {
237 #ifdef HAVE_SPEEX
238 method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
239 #else
240 if (flags & PA_RESAMPLER_VARIABLE_RATE)
241 method = PA_RESAMPLER_TRIVIAL;
242 else
243 method = PA_RESAMPLER_FFMPEG;
244 #endif
245 }
246
247 r = pa_xnew0(pa_resampler, 1);
248 r->mempool = pool;
249 r->method = method;
250 r->flags = flags;
251
252 /* Fill sample specs */
253 r->i_ss = *a;
254 r->o_ss = *b;
255
256 /* set up the remap structure */
257 r->remap.i_ss = &r->i_ss;
258 r->remap.o_ss = &r->o_ss;
259 r->remap.format = &r->work_format;
260
261 if (am)
262 r->i_cm = *am;
263 else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
264 goto fail;
265
266 if (bm)
267 r->o_cm = *bm;
268 else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
269 goto fail;
270
271 r->i_fz = pa_frame_size(a);
272 r->o_fz = pa_frame_size(b);
273
274 calc_map_table(r);
275
276 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
277
278 if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
279 (method == PA_RESAMPLER_FFMPEG))
280 r->work_format = PA_SAMPLE_S16NE;
281 else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
282
283 if (r->map_required || a->format != b->format || method == PA_RESAMPLER_PEAKS) {
284
285 if (a->format == PA_SAMPLE_S32NE || a->format == PA_SAMPLE_S32RE ||
286 a->format == PA_SAMPLE_FLOAT32NE || a->format == PA_SAMPLE_FLOAT32RE ||
287 a->format == PA_SAMPLE_S24NE || a->format == PA_SAMPLE_S24RE ||
288 a->format == PA_SAMPLE_S24_32NE || a->format == PA_SAMPLE_S24_32RE ||
289 b->format == PA_SAMPLE_S32NE || b->format == PA_SAMPLE_S32RE ||
290 b->format == PA_SAMPLE_FLOAT32NE || b->format == PA_SAMPLE_FLOAT32RE ||
291 b->format == PA_SAMPLE_S24NE || b->format == PA_SAMPLE_S24RE ||
292 b->format == PA_SAMPLE_S24_32NE || b->format == PA_SAMPLE_S24_32RE)
293 r->work_format = PA_SAMPLE_FLOAT32NE;
294 else
295 r->work_format = PA_SAMPLE_S16NE;
296
297 } else
298 r->work_format = a->format;
299
300 } else
301 r->work_format = PA_SAMPLE_FLOAT32NE;
302
303 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
304
305 r->w_sz = pa_sample_size_of_format(r->work_format);
306
307 if (r->i_ss.format != r->work_format) {
308 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
309 if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
310 goto fail;
311 } else {
312 pa_assert(r->work_format == PA_SAMPLE_S16NE);
313 if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
314 goto fail;
315 }
316 }
317
318 if (r->o_ss.format != r->work_format) {
319 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
320 if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
321 goto fail;
322 } else {
323 pa_assert(r->work_format == PA_SAMPLE_S16NE);
324 if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
325 goto fail;
326 }
327 }
328
329 /* initialize implementation */
330 if (init_table[method](r) < 0)
331 goto fail;
332
333 return r;
334
335 fail:
336 pa_xfree(r);
337
338 return NULL;
339 }
340
341 void pa_resampler_free(pa_resampler *r) {
342 pa_assert(r);
343
344 if (r->impl_free)
345 r->impl_free(r);
346
347 if (r->to_work_format_buf.memblock)
348 pa_memblock_unref(r->to_work_format_buf.memblock);
349 if (r->remap_buf.memblock)
350 pa_memblock_unref(r->remap_buf.memblock);
351 if (r->resample_buf.memblock)
352 pa_memblock_unref(r->resample_buf.memblock);
353 if (r->from_work_format_buf.memblock)
354 pa_memblock_unref(r->from_work_format_buf.memblock);
355
356 pa_xfree(r);
357 }
358
359 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
360 pa_assert(r);
361 pa_assert(rate > 0);
362
363 if (r->i_ss.rate == rate)
364 return;
365
366 r->i_ss.rate = rate;
367
368 r->impl_update_rates(r);
369 }
370
371 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
372 pa_assert(r);
373 pa_assert(rate > 0);
374
375 if (r->o_ss.rate == rate)
376 return;
377
378 r->o_ss.rate = rate;
379
380 r->impl_update_rates(r);
381 }
382
383 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
384 pa_assert(r);
385
386 /* Let's round up here to make it more likely that the caller will get at
387 * least out_length amount of data from pa_resampler_run().
388 *
389 * We don't take the leftover into account here. If we did, then it might
390 * be in theory possible that this function would return 0 and
391 * pa_resampler_run() would also return 0. That could lead to infinite
392 * loops. When the leftover is ignored here, such loops would eventually
393 * terminate, because the leftover would grow each round, finally
394 * surpassing the minimum input threshold of the resampler. */
395 return ((((uint64_t) ((out_length + r->o_fz-1) / r->o_fz) * r->i_ss.rate) + r->o_ss.rate-1) / r->o_ss.rate) * r->i_fz;
396 }
397
398 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
399 size_t frames;
400
401 pa_assert(r);
402
403 /* Let's round up here to ensure that the caller will always allocate big
404 * enough output buffer. */
405
406 frames = (in_length + r->i_fz - 1) / r->i_fz;
407
408 if (r->remap_buf_contains_leftover_data)
409 frames += r->remap_buf.length / (r->w_sz * r->o_ss.channels);
410
411 return (((uint64_t) frames * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * r->o_fz;
412 }
413
414 size_t pa_resampler_max_block_size(pa_resampler *r) {
415 size_t block_size_max;
416 pa_sample_spec max_ss;
417 size_t max_fs;
418 size_t frames;
419
420 pa_assert(r);
421
422 block_size_max = pa_mempool_block_size_max(r->mempool);
423
424 /* We deduce the "largest" sample spec we're using during the
425 * conversion */
426 max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
427
428 /* We silently assume that the format enum is ordered by size */
429 max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
430 max_ss.format = PA_MAX(max_ss.format, r->work_format);
431
432 max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
433
434 max_fs = pa_frame_size(&max_ss);
435 frames = block_size_max / max_fs - EXTRA_FRAMES;
436
437 if (r->remap_buf_contains_leftover_data)
438 frames -= r->remap_buf.length / (r->w_sz * r->o_ss.channels);
439
440 return ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
441 }
442
443 void pa_resampler_reset(pa_resampler *r) {
444 pa_assert(r);
445
446 if (r->impl_reset)
447 r->impl_reset(r);
448
449 r->remap_buf_contains_leftover_data = FALSE;
450 }
451
452 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
453 pa_assert(r);
454
455 return r->method;
456 }
457
458 const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
459 pa_assert(r);
460
461 return &r->i_cm;
462 }
463
464 const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
465 pa_assert(r);
466
467 return &r->i_ss;
468 }
469
470 const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
471 pa_assert(r);
472
473 return &r->o_cm;
474 }
475
476 const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
477 pa_assert(r);
478
479 return &r->o_ss;
480 }
481
482 static const char * const resample_methods[] = {
483 "src-sinc-best-quality",
484 "src-sinc-medium-quality",
485 "src-sinc-fastest",
486 "src-zero-order-hold",
487 "src-linear",
488 "trivial",
489 "speex-float-0",
490 "speex-float-1",
491 "speex-float-2",
492 "speex-float-3",
493 "speex-float-4",
494 "speex-float-5",
495 "speex-float-6",
496 "speex-float-7",
497 "speex-float-8",
498 "speex-float-9",
499 "speex-float-10",
500 "speex-fixed-0",
501 "speex-fixed-1",
502 "speex-fixed-2",
503 "speex-fixed-3",
504 "speex-fixed-4",
505 "speex-fixed-5",
506 "speex-fixed-6",
507 "speex-fixed-7",
508 "speex-fixed-8",
509 "speex-fixed-9",
510 "speex-fixed-10",
511 "ffmpeg",
512 "auto",
513 "copy",
514 "peaks"
515 };
516
517 const char *pa_resample_method_to_string(pa_resample_method_t m) {
518
519 if (m < 0 || m >= PA_RESAMPLER_MAX)
520 return NULL;
521
522 return resample_methods[m];
523 }
524
525 int pa_resample_method_supported(pa_resample_method_t m) {
526
527 if (m < 0 || m >= PA_RESAMPLER_MAX)
528 return 0;
529
530 #ifndef HAVE_LIBSAMPLERATE
531 if (m <= PA_RESAMPLER_SRC_LINEAR)
532 return 0;
533 #endif
534
535 #ifndef HAVE_SPEEX
536 if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
537 return 0;
538 if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
539 return 0;
540 #endif
541
542 return 1;
543 }
544
545 pa_resample_method_t pa_parse_resample_method(const char *string) {
546 pa_resample_method_t m;
547
548 pa_assert(string);
549
550 for (m = 0; m < PA_RESAMPLER_MAX; m++)
551 if (pa_streq(string, resample_methods[m]))
552 return m;
553
554 if (pa_streq(string, "speex-fixed"))
555 return PA_RESAMPLER_SPEEX_FIXED_BASE + 3;
556
557 if (pa_streq(string, "speex-float"))
558 return PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
559
560 return PA_RESAMPLER_INVALID;
561 }
562
563 static pa_bool_t on_left(pa_channel_position_t p) {
564
565 return
566 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
567 p == PA_CHANNEL_POSITION_REAR_LEFT ||
568 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
569 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
570 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
571 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
572 }
573
574 static pa_bool_t on_right(pa_channel_position_t p) {
575
576 return
577 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
578 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
579 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
580 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
581 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
582 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
583 }
584
585 static pa_bool_t on_center(pa_channel_position_t p) {
586
587 return
588 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
589 p == PA_CHANNEL_POSITION_REAR_CENTER ||
590 p == PA_CHANNEL_POSITION_TOP_CENTER ||
591 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
592 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
593 }
594
595 static pa_bool_t on_lfe(pa_channel_position_t p) {
596 return
597 p == PA_CHANNEL_POSITION_LFE;
598 }
599
600 static pa_bool_t on_front(pa_channel_position_t p) {
601 return
602 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
603 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
604 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
605 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
606 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
607 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
608 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
609 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
610 }
611
612 static pa_bool_t on_rear(pa_channel_position_t p) {
613 return
614 p == PA_CHANNEL_POSITION_REAR_LEFT ||
615 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
616 p == PA_CHANNEL_POSITION_REAR_CENTER ||
617 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
618 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
619 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
620 }
621
622 static pa_bool_t on_side(pa_channel_position_t p) {
623 return
624 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
625 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
626 p == PA_CHANNEL_POSITION_TOP_CENTER;
627 }
628
629 enum {
630 ON_FRONT,
631 ON_REAR,
632 ON_SIDE,
633 ON_OTHER
634 };
635
636 static int front_rear_side(pa_channel_position_t p) {
637 if (on_front(p))
638 return ON_FRONT;
639 if (on_rear(p))
640 return ON_REAR;
641 if (on_side(p))
642 return ON_SIDE;
643 return ON_OTHER;
644 }
645
646 static void calc_map_table(pa_resampler *r) {
647 unsigned oc, ic;
648 unsigned n_oc, n_ic;
649 pa_bool_t ic_connected[PA_CHANNELS_MAX];
650 pa_bool_t remix;
651 pa_strbuf *s;
652 char *t;
653 pa_remap_t *m;
654
655 pa_assert(r);
656
657 if (!(r->map_required = (r->i_ss.channels != r->o_ss.channels || (!(r->flags & PA_RESAMPLER_NO_REMAP) && !pa_channel_map_equal(&r->i_cm, &r->o_cm)))))
658 return;
659
660 m = &r->remap;
661
662 n_oc = r->o_ss.channels;
663 n_ic = r->i_ss.channels;
664
665 memset(m->map_table_f, 0, sizeof(m->map_table_f));
666 memset(m->map_table_i, 0, sizeof(m->map_table_i));
667
668 memset(ic_connected, 0, sizeof(ic_connected));
669 remix = (r->flags & (PA_RESAMPLER_NO_REMAP|PA_RESAMPLER_NO_REMIX)) == 0;
670
671 for (oc = 0; oc < n_oc; oc++) {
672 pa_bool_t oc_connected = FALSE;
673 pa_channel_position_t b = r->o_cm.map[oc];
674
675 for (ic = 0; ic < n_ic; ic++) {
676 pa_channel_position_t a = r->i_cm.map[ic];
677
678 if (r->flags & PA_RESAMPLER_NO_REMAP) {
679 /* We shall not do any remapping. Hence, just check by index */
680
681 if (ic == oc)
682 m->map_table_f[oc][ic] = 1.0;
683
684 continue;
685 }
686
687 if (r->flags & PA_RESAMPLER_NO_REMIX) {
688 /* We shall not do any remixing. Hence, just check by name */
689
690 if (a == b)
691 m->map_table_f[oc][ic] = 1.0;
692
693 continue;
694 }
695
696 pa_assert(remix);
697
698 /* OK, we shall do the full monty: upmixing and
699 * downmixing. Our algorithm is relatively simple, does
700 * not do spacialization, delay elements or apply lowpass
701 * filters for LFE. Patches are always welcome,
702 * though. Oh, and it doesn't do any matrix
703 * decoding. (Which probably wouldn't make any sense
704 * anyway.)
705 *
706 * This code is not idempotent: downmixing an upmixed
707 * stereo stream is not identical to the original. The
708 * volume will not match, and the two channels will be a
709 * linear combination of both.
710 *
711 * This is loosely based on random suggestions found on the
712 * Internet, such as this:
713 * http://www.halfgaar.net/surround-sound-in-linux and the
714 * alsa upmix plugin.
715 *
716 * The algorithm works basically like this:
717 *
718 * 1) Connect all channels with matching names.
719 *
720 * 2) Mono Handling:
721 * S:Mono: Copy into all D:channels
722 * D:Mono: Avg all S:channels
723 *
724 * 3) Mix D:Left, D:Right:
725 * D:Left: If not connected, avg all S:Left
726 * D:Right: If not connected, avg all S:Right
727 *
728 * 4) Mix D:Center
729 * If not connected, avg all S:Center
730 * If still not connected, avg all S:Left, S:Right
731 *
732 * 5) Mix D:LFE
733 * If not connected, avg all S:*
734 *
735 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If
736 * not connected, mix into all D:left and all D:right
737 * channels. Gain is 0.1, the current left and right
738 * should be multiplied by 0.9.
739 *
740 * 7) Make sure S:Center, S:LFE is used:
741 *
742 * S:Center, S:LFE: If not connected, mix into all
743 * D:left, all D:right, all D:center channels, gain is
744 * 0.375. The current (as result of 1..6) factors
745 * should be multiplied by 0.75. (Alt. suggestion: 0.25
746 * vs. 0.5) If C-front is only mixed into
747 * L-front/R-front if available, otherwise into all L/R
748 * channels. Similarly for C-rear.
749 *
750 * S: and D: shall relate to the source resp. destination channels.
751 *
752 * Rationale: 1, 2 are probably obvious. For 3: this
753 * copies front to rear if needed. For 4: we try to find
754 * some suitable C source for C, if we don't find any, we
755 * avg L and R. For 5: LFE is mixed from all channels. For
756 * 6: the rear channels should not be dropped entirely,
757 * however have only minimal impact. For 7: movies usually
758 * encode speech on the center channel. Thus we have to
759 * make sure this channel is distributed to L and R if not
760 * available in the output. Also, LFE is used to achieve a
761 * greater dynamic range, and thus we should try to do our
762 * best to pass it to L+R.
763 */
764
765 if (a == b || a == PA_CHANNEL_POSITION_MONO) {
766 m->map_table_f[oc][ic] = 1.0;
767
768 oc_connected = TRUE;
769 ic_connected[ic] = TRUE;
770 }
771 else if (b == PA_CHANNEL_POSITION_MONO) {
772 if (n_ic)
773 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
774
775 oc_connected = TRUE;
776 ic_connected[ic] = TRUE;
777 }
778 }
779
780 if (!oc_connected && remix) {
781 /* OK, we shall remix */
782
783 /* Try to find matching input ports for this output port */
784
785 if (on_left(b)) {
786 unsigned n = 0;
787
788 /* We are not connected and on the left side, let's
789 * average all left side input channels. */
790
791 for (ic = 0; ic < n_ic; ic++)
792 if (on_left(r->i_cm.map[ic]))
793 n++;
794
795 if (n > 0)
796 for (ic = 0; ic < n_ic; ic++)
797 if (on_left(r->i_cm.map[ic])) {
798 m->map_table_f[oc][ic] = 1.0f / (float) n;
799 ic_connected[ic] = TRUE;
800 }
801
802 /* We ignore the case where there is no left input
803 * channel. Something is really wrong in this case
804 * anyway. */
805
806 } else if (on_right(b)) {
807 unsigned n = 0;
808
809 /* We are not connected and on the right side, let's
810 * average all right side input channels. */
811
812 for (ic = 0; ic < n_ic; ic++)
813 if (on_right(r->i_cm.map[ic]))
814 n++;
815
816 if (n > 0)
817 for (ic = 0; ic < n_ic; ic++)
818 if (on_right(r->i_cm.map[ic])) {
819 m->map_table_f[oc][ic] = 1.0f / (float) n;
820 ic_connected[ic] = TRUE;
821 }
822
823 /* We ignore the case where there is no right input
824 * channel. Something is really wrong in this case
825 * anyway. */
826
827 } else if (on_center(b)) {
828 unsigned n = 0;
829
830 /* We are not connected and at the center. Let's
831 * average all center input channels. */
832
833 for (ic = 0; ic < n_ic; ic++)
834 if (on_center(r->i_cm.map[ic]))
835 n++;
836
837 if (n > 0) {
838 for (ic = 0; ic < n_ic; ic++)
839 if (on_center(r->i_cm.map[ic])) {
840 m->map_table_f[oc][ic] = 1.0f / (float) n;
841 ic_connected[ic] = TRUE;
842 }
843 } else {
844
845 /* Hmm, no center channel around, let's synthesize
846 * it by mixing L and R.*/
847
848 n = 0;
849
850 for (ic = 0; ic < n_ic; ic++)
851 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic]))
852 n++;
853
854 if (n > 0)
855 for (ic = 0; ic < n_ic; ic++)
856 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
857 m->map_table_f[oc][ic] = 1.0f / (float) n;
858 ic_connected[ic] = TRUE;
859 }
860
861 /* We ignore the case where there is not even a
862 * left or right input channel. Something is
863 * really wrong in this case anyway. */
864 }
865
866 } else if (on_lfe(b)) {
867
868 /* We are not connected and an LFE. Let's average all
869 * channels for LFE. */
870
871 for (ic = 0; ic < n_ic; ic++) {
872
873 if (!(r->flags & PA_RESAMPLER_NO_LFE))
874 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
875 else
876 m->map_table_f[oc][ic] = 0;
877
878 /* Please note that a channel connected to LFE
879 * doesn't really count as connected. */
880 }
881 }
882 }
883 }
884
885 if (remix) {
886 unsigned
887 ic_unconnected_left = 0,
888 ic_unconnected_right = 0,
889 ic_unconnected_center = 0,
890 ic_unconnected_lfe = 0;
891
892 for (ic = 0; ic < n_ic; ic++) {
893 pa_channel_position_t a = r->i_cm.map[ic];
894
895 if (ic_connected[ic])
896 continue;
897
898 if (on_left(a))
899 ic_unconnected_left++;
900 else if (on_right(a))
901 ic_unconnected_right++;
902 else if (on_center(a))
903 ic_unconnected_center++;
904 else if (on_lfe(a))
905 ic_unconnected_lfe++;
906 }
907
908 if (ic_unconnected_left > 0) {
909
910 /* OK, so there are unconnected input channels on the
911 * left. Let's multiply all already connected channels on
912 * the left side by .9 and add in our averaged unconnected
913 * channels multiplied by .1 */
914
915 for (oc = 0; oc < n_oc; oc++) {
916
917 if (!on_left(r->o_cm.map[oc]))
918 continue;
919
920 for (ic = 0; ic < n_ic; ic++) {
921
922 if (ic_connected[ic]) {
923 m->map_table_f[oc][ic] *= .9f;
924 continue;
925 }
926
927 if (on_left(r->i_cm.map[ic]))
928 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left;
929 }
930 }
931 }
932
933 if (ic_unconnected_right > 0) {
934
935 /* OK, so there are unconnected input channels on the
936 * right. Let's multiply all already connected channels on
937 * the right side by .9 and add in our averaged unconnected
938 * channels multiplied by .1 */
939
940 for (oc = 0; oc < n_oc; oc++) {
941
942 if (!on_right(r->o_cm.map[oc]))
943 continue;
944
945 for (ic = 0; ic < n_ic; ic++) {
946
947 if (ic_connected[ic]) {
948 m->map_table_f[oc][ic] *= .9f;
949 continue;
950 }
951
952 if (on_right(r->i_cm.map[ic]))
953 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right;
954 }
955 }
956 }
957
958 if (ic_unconnected_center > 0) {
959 pa_bool_t mixed_in = FALSE;
960
961 /* OK, so there are unconnected input channels on the
962 * center. Let's multiply all already connected channels on
963 * the center side by .9 and add in our averaged unconnected
964 * channels multiplied by .1 */
965
966 for (oc = 0; oc < n_oc; oc++) {
967
968 if (!on_center(r->o_cm.map[oc]))
969 continue;
970
971 for (ic = 0; ic < n_ic; ic++) {
972
973 if (ic_connected[ic]) {
974 m->map_table_f[oc][ic] *= .9f;
975 continue;
976 }
977
978 if (on_center(r->i_cm.map[ic])) {
979 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center;
980 mixed_in = TRUE;
981 }
982 }
983 }
984
985 if (!mixed_in) {
986 unsigned ncenter[PA_CHANNELS_MAX];
987 pa_bool_t found_frs[PA_CHANNELS_MAX];
988
989 memset(ncenter, 0, sizeof(ncenter));
990 memset(found_frs, 0, sizeof(found_frs));
991
992 /* Hmm, as it appears there was no center channel we
993 could mix our center channel in. In this case, mix
994 it into left and right. Using .375 and 0.75 as
995 factors. */
996
997 for (ic = 0; ic < n_ic; ic++) {
998
999 if (ic_connected[ic])
1000 continue;
1001
1002 if (!on_center(r->i_cm.map[ic]))
1003 continue;
1004
1005 for (oc = 0; oc < n_oc; oc++) {
1006
1007 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1008 continue;
1009
1010 if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
1011 found_frs[ic] = TRUE;
1012 break;
1013 }
1014 }
1015
1016 for (oc = 0; oc < n_oc; oc++) {
1017
1018 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1019 continue;
1020
1021 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1022 ncenter[oc]++;
1023 }
1024 }
1025
1026 for (oc = 0; oc < n_oc; oc++) {
1027
1028 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1029 continue;
1030
1031 if (ncenter[oc] <= 0)
1032 continue;
1033
1034 for (ic = 0; ic < n_ic; ic++) {
1035
1036 if (ic_connected[ic]) {
1037 m->map_table_f[oc][ic] *= .75f;
1038 continue;
1039 }
1040
1041 if (!on_center(r->i_cm.map[ic]))
1042 continue;
1043
1044 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1045 m->map_table_f[oc][ic] = .375f / (float) ncenter[oc];
1046 }
1047 }
1048 }
1049 }
1050
1051 if (ic_unconnected_lfe > 0 && !(r->flags & PA_RESAMPLER_NO_LFE)) {
1052
1053 /* OK, so there is an unconnected LFE channel. Let's mix
1054 * it into all channels, with factor 0.375 */
1055
1056 for (ic = 0; ic < n_ic; ic++) {
1057
1058 if (!on_lfe(r->i_cm.map[ic]))
1059 continue;
1060
1061 for (oc = 0; oc < n_oc; oc++)
1062 m->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe;
1063 }
1064 }
1065 }
1066 /* make an 16:16 int version of the matrix */
1067 for (oc = 0; oc < n_oc; oc++)
1068 for (ic = 0; ic < n_ic; ic++)
1069 m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1070
1071 s = pa_strbuf_new();
1072
1073 pa_strbuf_printf(s, " ");
1074 for (ic = 0; ic < n_ic; ic++)
1075 pa_strbuf_printf(s, " I%02u ", ic);
1076 pa_strbuf_puts(s, "\n +");
1077
1078 for (ic = 0; ic < n_ic; ic++)
1079 pa_strbuf_printf(s, "------");
1080 pa_strbuf_puts(s, "\n");
1081
1082 for (oc = 0; oc < n_oc; oc++) {
1083 pa_strbuf_printf(s, "O%02u |", oc);
1084
1085 for (ic = 0; ic < n_ic; ic++)
1086 pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1087
1088 pa_strbuf_puts(s, "\n");
1089 }
1090
1091 pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
1092 pa_xfree(t);
1093
1094 /* initialize the remapping function */
1095 pa_init_remap(m);
1096 }
1097
1098 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1099 unsigned n_samples;
1100 void *src, *dst;
1101
1102 pa_assert(r);
1103 pa_assert(input);
1104 pa_assert(input->memblock);
1105
1106 /* Convert the incoming sample into the work sample format and place them
1107 * in to_work_format_buf. */
1108
1109 if (!r->to_work_format_func || !input->length)
1110 return input;
1111
1112 n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1113
1114 r->to_work_format_buf.index = 0;
1115 r->to_work_format_buf.length = r->w_sz * n_samples;
1116
1117 if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
1118 if (r->to_work_format_buf.memblock)
1119 pa_memblock_unref(r->to_work_format_buf.memblock);
1120
1121 r->to_work_format_buf_samples = n_samples;
1122 r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
1123 }
1124
1125 src = pa_memblock_acquire_chunk(input);
1126 dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
1127
1128 r->to_work_format_func(n_samples, src, dst);
1129
1130 pa_memblock_release(input->memblock);
1131 pa_memblock_release(r->to_work_format_buf.memblock);
1132
1133 return &r->to_work_format_buf;
1134 }
1135
1136 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1137 unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
1138 void *src, *dst;
1139 size_t leftover_length = 0;
1140 pa_bool_t have_leftover;
1141
1142 pa_assert(r);
1143 pa_assert(input);
1144 pa_assert(input->memblock);
1145
1146 /* Remap channels and place the result in remap_buf. There may be leftover
1147 * data in the beginning of remap_buf. The leftover data is already
1148 * remapped, so it's not part of the input, it's part of the output. */
1149
1150 have_leftover = r->remap_buf_contains_leftover_data;
1151 r->remap_buf_contains_leftover_data = FALSE;
1152
1153 if (!have_leftover && (!r->map_required || input->length <= 0))
1154 return input;
1155 else if (input->length <= 0)
1156 return &r->remap_buf;
1157
1158 in_n_samples = (unsigned) (input->length / r->w_sz);
1159 in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
1160
1161 if (have_leftover) {
1162 leftover_length = r->remap_buf.length;
1163 out_n_frames += leftover_length / (r->w_sz * r->o_ss.channels);
1164 }
1165
1166 out_n_samples = out_n_frames * r->o_ss.channels;
1167 r->remap_buf.length = out_n_samples * r->w_sz;
1168
1169 if (have_leftover) {
1170 if (r->remap_buf_size < r->remap_buf.length) {
1171 pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
1172
1173 src = pa_memblock_acquire(r->remap_buf.memblock);
1174 dst = pa_memblock_acquire(new_block);
1175 memcpy(dst, src, leftover_length);
1176 pa_memblock_release(r->remap_buf.memblock);
1177 pa_memblock_release(new_block);
1178
1179 pa_memblock_unref(r->remap_buf.memblock);
1180 r->remap_buf.memblock = new_block;
1181 r->remap_buf_size = r->remap_buf.length;
1182 }
1183
1184 } else {
1185 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1186 if (r->remap_buf.memblock)
1187 pa_memblock_unref(r->remap_buf.memblock);
1188
1189 r->remap_buf_size = r->remap_buf.length;
1190 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1191 }
1192 }
1193
1194 src = pa_memblock_acquire_chunk(input);
1195 dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
1196
1197 if (r->map_required) {
1198 pa_remap_t *remap = &r->remap;
1199
1200 pa_assert(remap->do_remap);
1201 remap->do_remap(remap, dst, src, in_n_frames);
1202
1203 } else
1204 memcpy(dst, src, input->length);
1205
1206 pa_memblock_release(input->memblock);
1207 pa_memblock_release(r->remap_buf.memblock);
1208
1209 return &r->remap_buf;
1210 }
1211
1212 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1213 unsigned in_n_frames, in_n_samples;
1214 unsigned out_n_frames, out_n_samples;
1215
1216 pa_assert(r);
1217 pa_assert(input);
1218
1219 /* Resample the data and place the result in resample_buf. */
1220
1221 if (!r->impl_resample || !input->length)
1222 return input;
1223
1224 in_n_samples = (unsigned) (input->length / r->w_sz);
1225 in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
1226
1227 out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1228 out_n_samples = out_n_frames * r->o_ss.channels;
1229
1230 r->resample_buf.index = 0;
1231 r->resample_buf.length = r->w_sz * out_n_samples;
1232
1233 if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
1234 if (r->resample_buf.memblock)
1235 pa_memblock_unref(r->resample_buf.memblock);
1236
1237 r->resample_buf_samples = out_n_samples;
1238 r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
1239 }
1240
1241 r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1242 r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
1243
1244 return &r->resample_buf;
1245 }
1246
1247 static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1248 unsigned n_samples, n_frames;
1249 void *src, *dst;
1250
1251 pa_assert(r);
1252 pa_assert(input);
1253
1254 /* Convert the data into the correct sample type and place the result in
1255 * from_work_format_buf. */
1256
1257 if (!r->from_work_format_func || !input->length)
1258 return input;
1259
1260 n_samples = (unsigned) (input->length / r->w_sz);
1261 n_frames = n_samples / r->o_ss.channels;
1262
1263 r->from_work_format_buf.index = 0;
1264 r->from_work_format_buf.length = r->o_fz * n_frames;
1265
1266 if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
1267 if (r->from_work_format_buf.memblock)
1268 pa_memblock_unref(r->from_work_format_buf.memblock);
1269
1270 r->from_work_format_buf_samples = n_samples;
1271 r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
1272 }
1273
1274 src = pa_memblock_acquire_chunk(input);
1275 dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1276 r->from_work_format_func(n_samples, src, dst);
1277 pa_memblock_release(input->memblock);
1278 pa_memblock_release(r->from_work_format_buf.memblock);
1279
1280 return &r->from_work_format_buf;
1281 }
1282
1283 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1284 pa_memchunk *buf;
1285
1286 pa_assert(r);
1287 pa_assert(in);
1288 pa_assert(out);
1289 pa_assert(in->length);
1290 pa_assert(in->memblock);
1291 pa_assert(in->length % r->i_fz == 0);
1292
1293 buf = (pa_memchunk*) in;
1294 buf = convert_to_work_format(r, buf);
1295 buf = remap_channels(r, buf);
1296 buf = resample(r, buf);
1297
1298 if (buf->length) {
1299 buf = convert_from_work_format(r, buf);
1300 *out = *buf;
1301
1302 if (buf == in)
1303 pa_memblock_ref(buf->memblock);
1304 else
1305 pa_memchunk_reset(buf);
1306 } else
1307 pa_memchunk_reset(out);
1308 }
1309
1310 static void save_leftover(pa_resampler *r, void *buf, size_t len) {
1311 void *dst;
1312
1313 pa_assert(r);
1314 pa_assert(buf);
1315 pa_assert(len > 0);
1316
1317 /* Store the leftover to remap_buf. */
1318
1319 r->remap_buf.length = len;
1320
1321 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1322 if (r->remap_buf.memblock)
1323 pa_memblock_unref(r->remap_buf.memblock);
1324
1325 r->remap_buf_size = r->remap_buf.length;
1326 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1327 }
1328
1329 dst = pa_memblock_acquire(r->remap_buf.memblock);
1330 memcpy(dst, buf, r->remap_buf.length);
1331 pa_memblock_release(r->remap_buf.memblock);
1332
1333 r->remap_buf_contains_leftover_data = TRUE;
1334 }
1335
1336 /*** libsamplerate based implementation ***/
1337
1338 #ifdef HAVE_LIBSAMPLERATE
1339 static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1340 SRC_DATA data;
1341
1342 pa_assert(r);
1343 pa_assert(input);
1344 pa_assert(output);
1345 pa_assert(out_n_frames);
1346
1347 memset(&data, 0, sizeof(data));
1348
1349 data.data_in = pa_memblock_acquire_chunk(input);
1350 data.input_frames = (long int) in_n_frames;
1351
1352 data.data_out = pa_memblock_acquire_chunk(output);
1353 data.output_frames = (long int) *out_n_frames;
1354
1355 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
1356 data.end_of_input = 0;
1357
1358 pa_assert_se(src_process(r->src.state, &data) == 0);
1359
1360 if (data.input_frames_used < in_n_frames) {
1361 void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
1362 size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->o_ss.channels;
1363
1364 save_leftover(r, leftover_data, leftover_length);
1365 }
1366
1367 pa_memblock_release(input->memblock);
1368 pa_memblock_release(output->memblock);
1369
1370 *out_n_frames = (unsigned) data.output_frames_gen;
1371 }
1372
1373 static void libsamplerate_update_rates(pa_resampler *r) {
1374 pa_assert(r);
1375
1376 pa_assert_se(src_set_ratio(r->src.state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
1377 }
1378
1379 static void libsamplerate_reset(pa_resampler *r) {
1380 pa_assert(r);
1381
1382 pa_assert_se(src_reset(r->src.state) == 0);
1383 }
1384
1385 static void libsamplerate_free(pa_resampler *r) {
1386 pa_assert(r);
1387
1388 if (r->src.state)
1389 src_delete(r->src.state);
1390 }
1391
1392 static int libsamplerate_init(pa_resampler *r) {
1393 int err;
1394
1395 pa_assert(r);
1396
1397 if (!(r->src.state = src_new(r->method, r->o_ss.channels, &err)))
1398 return -1;
1399
1400 r->impl_free = libsamplerate_free;
1401 r->impl_update_rates = libsamplerate_update_rates;
1402 r->impl_resample = libsamplerate_resample;
1403 r->impl_reset = libsamplerate_reset;
1404
1405 return 0;
1406 }
1407 #endif
1408
1409 #ifdef HAVE_SPEEX
1410 /*** speex based implementation ***/
1411
1412 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1413 float *in, *out;
1414 uint32_t inf = in_n_frames, outf = *out_n_frames;
1415
1416 pa_assert(r);
1417 pa_assert(input);
1418 pa_assert(output);
1419 pa_assert(out_n_frames);
1420
1421 in = pa_memblock_acquire_chunk(input);
1422 out = pa_memblock_acquire_chunk(output);
1423
1424 pa_assert_se(speex_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);
1425
1426 pa_memblock_release(input->memblock);
1427 pa_memblock_release(output->memblock);
1428
1429 pa_assert(inf == in_n_frames);
1430 *out_n_frames = outf;
1431 }
1432
1433 static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1434 int16_t *in, *out;
1435 uint32_t inf = in_n_frames, outf = *out_n_frames;
1436
1437 pa_assert(r);
1438 pa_assert(input);
1439 pa_assert(output);
1440 pa_assert(out_n_frames);
1441
1442 in = pa_memblock_acquire_chunk(input);
1443 out = pa_memblock_acquire_chunk(output);
1444
1445 pa_assert_se(speex_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);
1446
1447 pa_memblock_release(input->memblock);
1448 pa_memblock_release(output->memblock);
1449
1450 pa_assert(inf == in_n_frames);
1451 *out_n_frames = outf;
1452 }
1453
1454 static void speex_update_rates(pa_resampler *r) {
1455 pa_assert(r);
1456
1457 pa_assert_se(speex_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
1458 }
1459
1460 static void speex_reset(pa_resampler *r) {
1461 pa_assert(r);
1462
1463 pa_assert_se(speex_resampler_reset_mem(r->speex.state) == 0);
1464 }
1465
1466 static void speex_free(pa_resampler *r) {
1467 pa_assert(r);
1468
1469 if (!r->speex.state)
1470 return;
1471
1472 speex_resampler_destroy(r->speex.state);
1473 }
1474
1475 static int speex_init(pa_resampler *r) {
1476 int q, err;
1477
1478 pa_assert(r);
1479
1480 r->impl_free = speex_free;
1481 r->impl_update_rates = speex_update_rates;
1482 r->impl_reset = speex_reset;
1483
1484 if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
1485
1486 q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
1487 r->impl_resample = speex_resample_int;
1488
1489 } else {
1490 pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
1491
1492 q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
1493 r->impl_resample = speex_resample_float;
1494 }
1495
1496 pa_log_info("Choosing speex quality setting %i.", q);
1497
1498 if (!(r->speex.state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
1499 return -1;
1500
1501 return 0;
1502 }
1503 #endif
1504
1505 /* Trivial implementation */
1506
1507 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1508 size_t fz;
1509 unsigned i_index, o_index;
1510 void *src, *dst;
1511
1512 pa_assert(r);
1513 pa_assert(input);
1514 pa_assert(output);
1515 pa_assert(out_n_frames);
1516
1517 fz = r->w_sz * r->o_ss.channels;
1518
1519 src = pa_memblock_acquire_chunk(input);
1520 dst = pa_memblock_acquire_chunk(output);
1521
1522 for (o_index = 0;; o_index++, r->trivial.o_counter++) {
1523 i_index = ((uint64_t) r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate;
1524 i_index = i_index > r->trivial.i_counter ? i_index - r->trivial.i_counter : 0;
1525
1526 if (i_index >= in_n_frames)
1527 break;
1528
1529 pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
1530
1531 memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * i_index, (int) fz);
1532 }
1533
1534 pa_memblock_release(input->memblock);
1535 pa_memblock_release(output->memblock);
1536
1537 *out_n_frames = o_index;
1538
1539 r->trivial.i_counter += in_n_frames;
1540
1541 /* Normalize counters */
1542 while (r->trivial.i_counter >= r->i_ss.rate) {
1543 pa_assert(r->trivial.o_counter >= r->o_ss.rate);
1544
1545 r->trivial.i_counter -= r->i_ss.rate;
1546 r->trivial.o_counter -= r->o_ss.rate;
1547 }
1548 }
1549
1550 static void trivial_update_rates_or_reset(pa_resampler *r) {
1551 pa_assert(r);
1552
1553 r->trivial.i_counter = 0;
1554 r->trivial.o_counter = 0;
1555 }
1556
1557 static int trivial_init(pa_resampler*r) {
1558 pa_assert(r);
1559
1560 r->trivial.o_counter = r->trivial.i_counter = 0;
1561
1562 r->impl_resample = trivial_resample;
1563 r->impl_update_rates = trivial_update_rates_or_reset;
1564 r->impl_reset = trivial_update_rates_or_reset;
1565
1566 return 0;
1567 }
1568
1569 /* Peak finder implementation */
1570
1571 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1572 unsigned c, o_index = 0;
1573 unsigned i, i_end = 0;
1574 void *src, *dst;
1575
1576 pa_assert(r);
1577 pa_assert(input);
1578 pa_assert(output);
1579 pa_assert(out_n_frames);
1580
1581 src = pa_memblock_acquire_chunk(input);
1582 dst = pa_memblock_acquire_chunk(output);
1583
1584 i = ((uint64_t) r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
1585 i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
1586
1587 while (i_end < in_n_frames) {
1588 i_end = ((uint64_t) (r->peaks.o_counter + 1) * r->i_ss.rate) / r->o_ss.rate;
1589 i_end = i_end > r->peaks.i_counter ? i_end - r->peaks.i_counter : 0;
1590
1591 pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
1592
1593 /* 1ch float is treated separately, because that is the common case */
1594 if (r->o_ss.channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
1595 float *s = (float*) src + i;
1596 float *d = (float*) dst + o_index;
1597
1598 for (; i < i_end && i < in_n_frames; i++) {
1599 float n = fabsf(*s++);
1600
1601 if (n > r->peaks.max_f[0])
1602 r->peaks.max_f[0] = n;
1603 }
1604
1605 if (i == i_end) {
1606 *d = r->peaks.max_f[0];
1607 r->peaks.max_f[0] = 0;
1608 o_index++, r->peaks.o_counter++;
1609 }
1610 } else if (r->work_format == PA_SAMPLE_S16NE) {
1611 int16_t *s = (int16_t*) src + r->i_ss.channels * i;
1612 int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
1613
1614 for (; i < i_end && i < in_n_frames; i++)
1615 for (c = 0; c < r->o_ss.channels; c++) {
1616 int16_t n = abs(*s++);
1617
1618 if (n > r->peaks.max_i[c])
1619 r->peaks.max_i[c] = n;
1620 }
1621
1622 if (i == i_end) {
1623 for (c = 0; c < r->o_ss.channels; c++, d++) {
1624 *d = r->peaks.max_i[c];
1625 r->peaks.max_i[c] = 0;
1626 }
1627 o_index++, r->peaks.o_counter++;
1628 }
1629 } else {
1630 float *s = (float*) src + r->i_ss.channels * i;
1631 float *d = (float*) dst + r->o_ss.channels * o_index;
1632
1633 for (; i < i_end && i < in_n_frames; i++)
1634 for (c = 0; c < r->o_ss.channels; c++) {
1635 float n = fabsf(*s++);
1636
1637 if (n > r->peaks.max_f[c])
1638 r->peaks.max_f[c] = n;
1639 }
1640
1641 if (i == i_end) {
1642 for (c = 0; c < r->o_ss.channels; c++, d++) {
1643 *d = r->peaks.max_f[c];
1644 r->peaks.max_f[c] = 0;
1645 }
1646 o_index++, r->peaks.o_counter++;
1647 }
1648 }
1649 }
1650
1651 pa_memblock_release(input->memblock);
1652 pa_memblock_release(output->memblock);
1653
1654 *out_n_frames = o_index;
1655
1656 r->peaks.i_counter += in_n_frames;
1657
1658 /* Normalize counters */
1659 while (r->peaks.i_counter >= r->i_ss.rate) {
1660 pa_assert(r->peaks.o_counter >= r->o_ss.rate);
1661
1662 r->peaks.i_counter -= r->i_ss.rate;
1663 r->peaks.o_counter -= r->o_ss.rate;
1664 }
1665 }
1666
1667 static void peaks_update_rates_or_reset(pa_resampler *r) {
1668 pa_assert(r);
1669
1670 r->peaks.i_counter = 0;
1671 r->peaks.o_counter = 0;
1672 }
1673
1674 static int peaks_init(pa_resampler*r) {
1675 pa_assert(r);
1676 pa_assert(r->i_ss.rate >= r->o_ss.rate);
1677 pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
1678
1679 r->peaks.o_counter = r->peaks.i_counter = 0;
1680 memset(r->peaks.max_i, 0, sizeof(r->peaks.max_i));
1681 memset(r->peaks.max_f, 0, sizeof(r->peaks.max_f));
1682
1683 r->impl_resample = peaks_resample;
1684 r->impl_update_rates = peaks_update_rates_or_reset;
1685 r->impl_reset = peaks_update_rates_or_reset;
1686
1687 return 0;
1688 }
1689
1690 /*** ffmpeg based implementation ***/
1691
1692 static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1693 unsigned used_frames = 0, c;
1694 int previous_consumed_frames = -1;
1695
1696 pa_assert(r);
1697 pa_assert(input);
1698 pa_assert(output);
1699 pa_assert(out_n_frames);
1700
1701 for (c = 0; c < r->o_ss.channels; c++) {
1702 unsigned u;
1703 pa_memblock *b, *w;
1704 int16_t *p, *t, *k, *q, *s;
1705 int consumed_frames;
1706
1707 /* Allocate a new block */
1708 b = pa_memblock_new(r->mempool, r->ffmpeg.buf[c].length + in_n_frames * sizeof(int16_t));
1709 p = pa_memblock_acquire(b);
1710
1711 /* Now copy the input data, splitting up channels */
1712 t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
1713 k = p;
1714 for (u = 0; u < in_n_frames; u++) {
1715 *k = *t;
1716 t += r->o_ss.channels;
1717 k ++;
1718 }
1719 pa_memblock_release(input->memblock);
1720
1721 /* Allocate buffer for the result */
1722 w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
1723 q = pa_memblock_acquire(w);
1724
1725 /* Now, resample */
1726 used_frames = (unsigned) av_resample(r->ffmpeg.state,
1727 q, p,
1728 &consumed_frames,
1729 (int) in_n_frames, (int) *out_n_frames,
1730 c >= (unsigned) (r->o_ss.channels-1));
1731
1732 pa_memblock_release(b);
1733 pa_memblock_unref(b);
1734
1735 pa_assert(consumed_frames <= (int) in_n_frames);
1736 pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
1737 previous_consumed_frames = consumed_frames;
1738
1739 /* And place the results in the output buffer */
1740 s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
1741 for (u = 0; u < used_frames; u++) {
1742 *s = *q;
1743 q++;
1744 s += r->o_ss.channels;
1745 }
1746 pa_memblock_release(output->memblock);
1747 pa_memblock_release(w);
1748 pa_memblock_unref(w);
1749 }
1750
1751 if (previous_consumed_frames < (int) in_n_frames) {
1752 void *leftover_data = (int16_t *) pa_memblock_acquire_chunk(input) + previous_consumed_frames * r->o_ss.channels;
1753 size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->o_ss.channels * sizeof(int16_t);
1754
1755 save_leftover(r, leftover_data, leftover_length);
1756 pa_memblock_release(input->memblock);
1757 }
1758
1759 *out_n_frames = used_frames;
1760 }
1761
1762 static void ffmpeg_free(pa_resampler *r) {
1763 unsigned c;
1764
1765 pa_assert(r);
1766
1767 if (r->ffmpeg.state)
1768 av_resample_close(r->ffmpeg.state);
1769
1770 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1771 if (r->ffmpeg.buf[c].memblock)
1772 pa_memblock_unref(r->ffmpeg.buf[c].memblock);
1773 }
1774
1775 static int ffmpeg_init(pa_resampler *r) {
1776 unsigned c;
1777
1778 pa_assert(r);
1779
1780 /* We could probably implement different quality levels by
1781 * adjusting the filter parameters here. However, ffmpeg
1782 * internally only uses these hardcoded values, so let's use them
1783 * here for now as well until ffmpeg makes this configurable. */
1784
1785 if (!(r->ffmpeg.state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
1786 return -1;
1787
1788 r->impl_free = ffmpeg_free;
1789 r->impl_resample = ffmpeg_resample;
1790
1791 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1792 pa_memchunk_reset(&r->ffmpeg.buf[c]);
1793
1794 return 0;
1795 }
1796
1797 /*** copy (noop) implementation ***/
1798
1799 static int copy_init(pa_resampler *r) {
1800 pa_assert(r);
1801
1802 pa_assert(r->o_ss.rate == r->i_ss.rate);
1803
1804 return 0;
1805 }