[pulseaudio-discuss] [PATCH RFCv3 38/51] resampler: Precompute maximum block size in frames

Peter Meerwald pmeerw at pmeerw.net
Tue Nov 4 15:26:33 PST 2014


From: Peter Meerwald <p.meerwald at bct-electronic.com>

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/pulsecore/resampler.c | 34 ++++++++++++++++++++--------------
 src/pulsecore/resampler.h |  3 ++-
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 79b463e..a543610 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -42,6 +42,7 @@ struct ffmpeg_data { /* data specific to ffmpeg */
 };
 
 static int copy_init(pa_resampler *r);
+static void setup_max_frames(pa_resampler *r);
 
 static void setup_remap(const pa_resampler *r, pa_remap_t *m);
 static void free_remap(pa_remap_t *m);
@@ -391,6 +392,8 @@ pa_resampler* pa_resampler_new(
                  pa_sample_format_to_string(b->format), pa_sample_format_to_string(r->work_format));
     pa_log_debug("  channels %d -> %d (resampling %d)", a->channels, b->channels, r->work_channels);
 
+    setup_max_frames(r);
+
     /* set up the remap structure */
     if (r->map_required)
         setup_remap(r, &r->remap);
@@ -485,11 +488,8 @@ size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
     return (((uint64_t) frames * 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) {
+static void setup_max_frames(pa_resampler *r) {
     size_t block_size_max;
-    pa_sample_spec max_ss;
-    size_t max_fs;
-    size_t frames;
 
     pa_assert(r);
 
@@ -497,26 +497,32 @@ size_t pa_resampler_max_block_size(pa_resampler *r) {
 
     /* We deduce the "largest" sample spec we're using during the
      * conversion */
-    max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
+    r->max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
 
     /* We silently assume that the format enum is ordered by size */
-    max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
-    max_ss.format = PA_MAX(max_ss.format, r->work_format);
+    r->max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
+    r->max_ss.format = PA_MAX(r->max_ss.format, r->work_format);
 
-    max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
-    pa_assert(pa_sample_spec_valid(&max_ss));
+    r->max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
+    pa_assert(pa_sample_spec_valid(&r->max_ss));
+
+    r->max_frames = block_size_max / PA_FRAME_SIZE(&r->max_ss) - EXTRA_FRAMES;
+}
 
-    max_fs = PA_FRAME_SIZE(&max_ss);
-    frames = block_size_max / max_fs - EXTRA_FRAMES;
+size_t pa_resampler_max_block_size(pa_resampler *r) {
+    size_t frames;
+
+    pa_assert(r);
 
+    frames = r->max_frames;
     pa_assert(frames >= (r->leftover_buf->length / r->w_fz));
     if (*r->have_leftover)
         frames -= r->leftover_buf->length / r->w_fz;
 
-    block_size_max = ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
+    frames = ((uint64_t) frames * r->i_ss.rate / r->max_ss.rate) * r->i_fz;
 
-    if (block_size_max > 0)
-        return block_size_max;
+    if (frames > 0)
+        return frames;
     else
         /* A single input frame may result in so much output that it doesn't
          * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
diff --git a/src/pulsecore/resampler.h b/src/pulsecore/resampler.h
index 5a84cf0..6f45774 100644
--- a/src/pulsecore/resampler.h
+++ b/src/pulsecore/resampler.h
@@ -73,7 +73,7 @@ struct pa_resampler {
     pa_resample_method_t method;
     pa_resample_flags_t flags;
 
-    pa_sample_spec i_ss, o_ss;
+    pa_sample_spec i_ss, o_ss, max_ss;
     pa_channel_map i_cm, o_cm;
     size_t i_fz, o_fz, w_fz, w_sz;
     pa_mempool *mempool;
@@ -86,6 +86,7 @@ struct pa_resampler {
     size_t remap_buf_size;
     size_t resample_buf_size;
     size_t from_work_format_buf_size;
+    size_t max_frames;
 
     /* points to buffer before resampling stage, remap or to_work */
     pa_memchunk *leftover_buf;
-- 
1.9.1



More information about the pulseaudio-discuss mailing list