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