From 6366fb7b52a88403007edaa24e2a0fa9d082a560 Mon Sep 17 00:00:00 2001 From: Tanu Kaskinen Date: Sun, 22 Apr 2012 20:31:11 +0300 Subject: [PATCH 3/3] resampler: Add support for resamplers that consume less data than asked. libsamplerate_resample() assumed that src_process() would always consume the whole input buffer. That was an invalid assumption leading to crashes. This patch adds a leftover memchunk for holding a reference to the input memblock to keep it around until the next pa_resampler_run() call, in case the resampler doesn't consume all of the input. The leftover data is concatenated to the new input in the add_leftover() function that is called before resampling, after format conversion and channel remapping. BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=47156 --- src/pulsecore/resampler.c | 85 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 5f791da..ca77f88 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -56,8 +56,9 @@ struct pa_resampler { size_t i_fz, o_fz, w_sz; pa_mempool *mempool; - pa_memchunk buf1, buf2, buf4, buf5; - unsigned buf1_samples, buf2_samples, buf4_samples, buf5_samples; + pa_memchunk buf1, buf2, buf3, buf4, buf5; + unsigned buf1_samples, buf2_samples, buf3_samples, buf4_samples, buf5_samples; + pa_memchunk leftover; pa_sample_format_t work_format; @@ -338,10 +339,14 @@ void pa_resampler_free(pa_resampler *r) { pa_memblock_unref(r->buf1.memblock); if (r->buf2.memblock) pa_memblock_unref(r->buf2.memblock); + if (r->buf3.memblock) + pa_memblock_unref(r->buf3.memblock); if (r->buf4.memblock) pa_memblock_unref(r->buf4.memblock); if (r->buf5.memblock) pa_memblock_unref(r->buf5.memblock); + if (r->leftover.memblock) + pa_memblock_unref(r->leftover.memblock); pa_xfree(r); } @@ -373,17 +378,24 @@ void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) { size_t pa_resampler_request(pa_resampler *r, size_t out_length) { pa_assert(r); - /* Let's round up here */ - + /* Let's round up here to make it more likely that the caller will get at + * least out_length amount of data from pa_resampler_run(). + * + * We don't take the leftover into account here. If we did, then it might + * be in theory possible that this function would return 0 and + * pa_resampler_run() would also return 0. That could lead to infinite + * loops. When the leftover is ignored here, such loops would eventually + * terminate, because the leftover would grow each round, finally + * surpassing the minimum input threshold of the resampler. */ return (((((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; } size_t pa_resampler_result(pa_resampler *r, size_t in_length) { pa_assert(r); - /* Let's round up here */ - - return (((((in_length + r->i_fz-1) / r->i_fz) * r->o_ss.rate) + r->i_ss.rate-1) / r->i_ss.rate) * r->o_fz; + /* Let's round up here to ensure that the caller will always allocate big + * enough output buffer. */ + return (((((in_length + r->i_fz-1) / r->i_fz + r->leftover.length / r->o_fz) * r->o_ss.rate) + r->i_ss.rate-1) / r->i_ss.rate) * r->o_fz; } size_t pa_resampler_max_block_size(pa_resampler *r) { @@ -407,7 +419,7 @@ size_t pa_resampler_max_block_size(pa_resampler *r) { fs = pa_frame_size(&ss); - return (((block_size_max/fs - EXTRA_FRAMES)*r->i_ss.rate)/ss.rate)*r->i_fz; + return (((block_size_max / fs - EXTRA_FRAMES - r->leftover.length / r->o_fz) * r->i_ss.rate) / ss.rate) * r->i_fz; } void pa_resampler_reset(pa_resampler *r) { @@ -415,6 +427,11 @@ void pa_resampler_reset(pa_resampler *r) { if (r->impl_reset) r->impl_reset(r); + + if (r->leftover.memblock) { + pa_memblock_unref(r->leftover.memblock); + pa_memchunk_reset(&r->leftover); + } } pa_resample_method_t pa_resampler_get_method(pa_resampler *r) { @@ -1136,6 +1153,50 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { return &r->buf2; } +static pa_memchunk *add_leftover(pa_resampler *r, pa_memchunk *input) { + unsigned out_n_samples; + void *src; + void *dst; + + pa_assert(r); + pa_assert(input); + + /* Concatenate leftover and input and place the result in buf3. */ + + if (r->leftover.length <= 0) + return input; + + r->buf3.index = 0; + r->buf3.length = r->leftover.length + input->length; + + out_n_samples = (unsigned) (r->buf3.length / r->w_sz); + + if (!r->buf3.memblock || r->buf3_samples < out_n_samples) { + if (r->buf3.memblock) + pa_memblock_unref(r->buf3.memblock); + + r->buf3_samples = out_n_samples; + r->buf3.memblock = pa_memblock_new(r->mempool, r->buf3.length); + } + + src = (uint8_t *) pa_memblock_acquire(r->leftover.memblock) + r->leftover.index; + dst = pa_memblock_acquire(r->buf3.memblock); + memcpy(dst, src, r->leftover.length); + + src = (uint8_t *) pa_memblock_acquire(input->memblock) + input->index; + dst = (uint8_t *) dst + r->leftover.length; + memcpy(dst, src, input->length); + + pa_memblock_release(r->leftover.memblock); + pa_memblock_release(input->memblock); + pa_memblock_release(r->buf3.memblock); + + pa_memblock_unref(r->leftover.memblock); + pa_memchunk_reset(&r->leftover); + + return &r->buf3; +} + static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) { unsigned in_n_frames, in_n_samples; unsigned out_n_frames, out_n_samples; @@ -1221,6 +1282,7 @@ void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) buf = (pa_memchunk*) in; buf = convert_to_work_format(r, buf); buf = remap_channels(r, buf); + buf = add_leftover(r, buf); buf = resample(r, buf); if (buf->length) { @@ -1258,12 +1320,17 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un data.end_of_input = 0; pa_assert_se(src_process(r->src.state, &data) == 0); - pa_assert((unsigned) data.input_frames_used == in_n_frames); pa_memblock_release(input->memblock); pa_memblock_release(output->memblock); *out_n_frames = (unsigned) data.output_frames_gen; + + if (data.input_frames_used < in_n_frames) { + r->leftover.index = input->index + data.input_frames_used * r->o_fz; + r->leftover.length = (in_n_frames - data.input_frames_used) * r->o_fz; + r->leftover.memblock = pa_memblock_ref(input->memblock); + } } static void libsamplerate_update_rates(pa_resampler *r) { -- 1.7.10