[pulseaudio-discuss] [PATCHv2 1/3] resampler: Extend fit_buf() helper to copy leftover data to new buffer
Peter Meerwald
pmeerw at pmeerw.net
Mon Dec 2 08:26:59 PST 2013
From: Peter Meerwald <p.meerwald at bct-electronic.com>
the patch changes the interface of the (internal) fit_buf() function:
fit_buf() manages the memblock of the buf chunk, it reallocates the memblock
if the requested number of bytes ('len') if larger than the memblock's size ('size')
and optionally preserves 'copy' bytes
the code should be in line with the comment now
Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
src/pulsecore/resampler.c | 63 ++++++++++++++++++++-------------------------
1 file changed, 28 insertions(+), 35 deletions(-)
diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 3ac8f5e..58b250e 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -1141,15 +1141,32 @@ static void calc_map_table(pa_resampler *r) {
pa_init_remap(m);
}
-static size_t fit_buf(pa_resampler *r, pa_memchunk *buf, size_t size) {
- if (!buf->memblock || size < buf->length) {
- size = buf->length;
- if (buf->memblock)
+/* check if buf's memblock is large enough to hold 'len' bytes; create a
+ * new memblock if necessary and optionally preserve 'copy' data bytes */
+static void fit_buf(pa_resampler *r, pa_memchunk *buf, size_t len, size_t *size, size_t copy) {
+ pa_assert(size);
+
+ if (!buf->memblock || len > *size) {
+ pa_memblock *new_block = pa_memblock_new(r->mempool, len);
+
+ if (buf->memblock) {
+ if (copy > 0) {
+ void *src = pa_memblock_acquire(buf->memblock);
+ void *dst = pa_memblock_acquire(new_block);
+ pa_assert(copy <= len);
+ memcpy(dst, src, copy);
+ pa_memblock_release(new_block);
+ pa_memblock_release(buf->memblock);
+ }
+
pa_memblock_unref(buf->memblock);
+ }
- buf->memblock = pa_memblock_new(r->mempool, size);
+ buf->memblock = new_block;
+ *size = len;
}
- return size;
+
+ buf->length = len;
}
static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
@@ -1167,9 +1184,7 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input)
return input;
n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
-
- r->to_work_format_buf.length = r->w_sz * n_samples;
- r->to_work_format_buf_size = fit_buf(r, &r->to_work_format_buf, r->to_work_format_buf_size);
+ fit_buf(r, &r->to_work_format_buf, r->w_sz * n_samples, &r->to_work_format_buf_size, 0);
src = pa_memblock_acquire_chunk(input);
dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
@@ -1213,24 +1228,7 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
}
out_n_samples = out_n_frames * r->o_ss.channels;
- r->remap_buf.length = out_n_samples * r->w_sz;
-
- if (have_leftover) {
- if (r->remap_buf_size < r->remap_buf.length) {
- pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
-
- src = pa_memblock_acquire(r->remap_buf.memblock);
- dst = pa_memblock_acquire(new_block);
- memcpy(dst, src, leftover_length);
- pa_memblock_release(r->remap_buf.memblock);
- pa_memblock_release(new_block);
-
- pa_memblock_unref(r->remap_buf.memblock);
- r->remap_buf.memblock = new_block;
- r->remap_buf_size = r->remap_buf.length;
- }
- } else
- r->remap_buf_size = fit_buf(r, &r->remap_buf, r->remap_buf_size);
+ fit_buf(r, &r->remap_buf, out_n_samples * r->w_sz, &r->remap_buf_size, leftover_length);
src = pa_memblock_acquire_chunk(input);
dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
@@ -1258,8 +1256,7 @@ static void save_leftover(pa_resampler *r, void *buf, size_t len) {
pa_assert(len > 0);
/* Store the leftover data. */
- r->leftover_buf->length = len;
- *r->leftover_buf_size = fit_buf(r, r->leftover_buf, *r->leftover_buf_size);
+ fit_buf(r, r->leftover_buf, len, r->leftover_buf_size, 0);
*r->have_leftover = true;
dst = pa_memblock_acquire(r->leftover_buf->memblock);
@@ -1281,9 +1278,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
in_n_frames = (unsigned) (input->length / r->w_fz);
out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
-
- r->resample_buf.length = r->w_fz * out_n_frames;
- r->resample_buf_size = fit_buf(r, &r->resample_buf, r->resample_buf_size);
+ fit_buf(r, &r->resample_buf, r->w_fz * out_n_frames, &r->resample_buf_size, 0);
leftover_n_frames = r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
@@ -1313,9 +1308,7 @@ static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input
n_samples = (unsigned) (input->length / r->w_sz);
n_frames = n_samples / r->o_ss.channels;
-
- r->from_work_format_buf.length = r->o_fz * n_frames;
- r->from_work_format_buf_size = fit_buf(r, &r->from_work_format_buf, r->from_work_format_buf_size);
+ fit_buf(r, &r->from_work_format_buf, r->o_fz * n_frames, &r->from_work_format_buf_size, 0);
src = pa_memblock_acquire_chunk(input);
dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
--
1.7.9.5
More information about the pulseaudio-discuss
mailing list