From 25724cdd40283a00e6edd9449d0f3cf16823b41b Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 14 Aug 2009 19:45:39 +0200 Subject: Get rid of liboil Get rid of the liboil dependency and reimplement the liboil functions with an equivalent C implementation. Note that most of these functions are deprecated in liboil and that none of them had any optimisations. We can further specialize our handrolled versions for some extra speedups. --- src/pulsecore/resampler.c | 69 +++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 32 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 59e0a0c1..a3c17f8c 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -31,9 +31,6 @@ #include -#include -#include - #include #include #include @@ -1045,33 +1042,46 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) return &r->buf1; } -static void vectoradd_s16_with_fraction( - int16_t *d, int dstr, - const int16_t *s1, int sstr1, - const int16_t *s2, int sstr2, - int n, - float s3, float s4) { +static void vectoradd_f32( + float *d, int dstr, + const float *s, int sstr, + int n, float s4) { - int32_t i3, i4; + for (; n > 0; n--) { + *d = (float) (*d + (s4 * *s)); - i3 = (int32_t) (s3 * 0x10000); - i4 = (int32_t) (s4 * 0x10000); + s = (const float*) ((const uint8_t*) s + sstr); + d = (float*) ((uint8_t*) d + dstr); + } +} + +static void vectoradd_s16( + int16_t *d, int dstr, + const int16_t *s, int sstr, + int n) { for (; n > 0; n--) { - int32_t a, b; + *d = (int16_t) (*d + *s); - a = *s1; - b = *s2; + s = (const int16_t*) ((const uint8_t*) s + sstr); + d = (int16_t*) ((uint8_t*) d + dstr); + } +} - a = (a * i3) / 0x10000; - b = (b * i4) / 0x10000; +static void vectoradd_s16_with_fraction( + int16_t *d, int dstr, + const int16_t *s, int sstr, + int n, float s4) { - *d = (int16_t) (a + b); + int32_t i4; - s1 = (const int16_t*) ((const uint8_t*) s1 + sstr1); - s2 = (const int16_t*) ((const uint8_t*) s2 + sstr2); - d = (int16_t*) ((uint8_t*) d + dstr); + i4 = (int32_t) (s4 * 0x10000); + + for (; n > 0; n--) { + *d = (int16_t) (*d + (((int32_t)*s * i4) >> 16)); + s = (const int16_t*) ((const uint8_t*) s + sstr); + d = (int16_t*) ((uint8_t*) d + dstr); } } @@ -1125,12 +1135,11 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { if (r->map_table[oc][ic] <= 0.0) continue; - oil_vectoradd_f32( - (float*) dst + oc, o_skip, + vectoradd_f32( (float*) dst + oc, o_skip, (float*) src + ic, i_skip, (int) n_frames, - &one, &r->map_table[oc][ic]); + r->map_table[oc][ic]); } } @@ -1147,23 +1156,19 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { continue; if (r->map_table[oc][ic] >= 1.0) { - static const int16_t one = 1; - oil_vectoradd_s16( - (int16_t*) dst + oc, o_skip, + vectoradd_s16( (int16_t*) dst + oc, o_skip, (int16_t*) src + ic, i_skip, - (int) n_frames, - &one, &one); + (int) n_frames); } else vectoradd_s16_with_fraction( - (int16_t*) dst + oc, o_skip, (int16_t*) dst + oc, o_skip, (int16_t*) src + ic, i_skip, (int) n_frames, - 1.0f, r->map_table[oc][ic]); + r->map_table[oc][ic]); } } @@ -1469,7 +1474,7 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned pa_assert(o_index * fz < pa_memblock_get_length(output->memblock)); - oil_memcpy((uint8_t*) dst + fz * o_index, + memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * j, (int) fz); } -- cgit From 601e5f1867065912e1740e2408a948ca818f6c59 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 17 Aug 2009 11:35:47 +0200 Subject: resampler: cache integer channel_map Calculate and cache an integer version of the channel map so that we don't have to regenerate it when dealing with s16 samples. --- src/pulsecore/resampler.c | 66 +++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index a3c17f8c..e3473ac5 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -61,7 +61,8 @@ struct pa_resampler { pa_convert_func_t to_work_format_func; pa_convert_func_t from_work_format_func; - float map_table[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; + float map_table_f[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; + int32_t map_table_i[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; pa_bool_t map_required; void (*impl_free)(pa_resampler *r); @@ -587,7 +588,8 @@ static void calc_map_table(pa_resampler *r) { 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))))) return; - memset(r->map_table, 0, sizeof(r->map_table)); + memset(r->map_table_f, 0, sizeof(r->map_table_f)); + memset(r->map_table_i, 0, sizeof(r->map_table_i)); memset(ic_connected, 0, sizeof(ic_connected)); remix = (r->flags & (PA_RESAMPLER_NO_REMAP|PA_RESAMPLER_NO_REMIX)) == 0; @@ -602,7 +604,7 @@ static void calc_map_table(pa_resampler *r) { /* We shall not do any remapping. Hence, just check by index */ if (ic == oc) - r->map_table[oc][ic] = 1.0; + r->map_table_f[oc][ic] = 1.0; continue; } @@ -611,7 +613,7 @@ static void calc_map_table(pa_resampler *r) { /* We shall not do any remixing. Hence, just check by name */ if (a == b) - r->map_table[oc][ic] = 1.0; + r->map_table_f[oc][ic] = 1.0; continue; } @@ -686,7 +688,7 @@ static void calc_map_table(pa_resampler *r) { */ if (a == b || a == PA_CHANNEL_POSITION_MONO || b == PA_CHANNEL_POSITION_MONO) { - r->map_table[oc][ic] = 1.0; + r->map_table_f[oc][ic] = 1.0; oc_connected = TRUE; ic_connected[ic] = TRUE; @@ -711,7 +713,7 @@ static void calc_map_table(pa_resampler *r) { if (n > 0) for (ic = 0; ic < r->i_ss.channels; ic++) if (on_left(r->i_cm.map[ic])) { - r->map_table[oc][ic] = 1.0f / (float) n; + r->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -732,7 +734,7 @@ static void calc_map_table(pa_resampler *r) { if (n > 0) for (ic = 0; ic < r->i_ss.channels; ic++) if (on_right(r->i_cm.map[ic])) { - r->map_table[oc][ic] = 1.0f / (float) n; + r->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -753,7 +755,7 @@ static void calc_map_table(pa_resampler *r) { if (n > 0) { for (ic = 0; ic < r->i_ss.channels; ic++) if (on_center(r->i_cm.map[ic])) { - r->map_table[oc][ic] = 1.0f / (float) n; + r->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } } else { @@ -770,7 +772,7 @@ static void calc_map_table(pa_resampler *r) { if (n > 0) for (ic = 0; ic < r->i_ss.channels; ic++) if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) { - r->map_table[oc][ic] = 1.0f / (float) n; + r->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -787,9 +789,9 @@ static void calc_map_table(pa_resampler *r) { for (ic = 0; ic < r->i_ss.channels; ic++) { if (!(r->flags & PA_RESAMPLER_NO_LFE)) - r->map_table[oc][ic] = 1.0f / (float) r->i_ss.channels; + r->map_table_f[oc][ic] = 1.0f / (float) r->i_ss.channels; else - r->map_table[oc][ic] = 0; + r->map_table_f[oc][ic] = 0; /* Please note that a channel connected to LFE * doesn't really count as connected. */ @@ -836,12 +838,12 @@ static void calc_map_table(pa_resampler *r) { for (ic = 0; ic < r->i_ss.channels; ic++) { if (ic_connected[ic]) { - r->map_table[oc][ic] *= .9f; + r->map_table_f[oc][ic] *= .9f; continue; } if (on_left(r->i_cm.map[ic])) - r->map_table[oc][ic] = .1f / (float) ic_unconnected_left; + r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left; } } } @@ -861,12 +863,12 @@ static void calc_map_table(pa_resampler *r) { for (ic = 0; ic < r->i_ss.channels; ic++) { if (ic_connected[ic]) { - r->map_table[oc][ic] *= .9f; + r->map_table_f[oc][ic] *= .9f; continue; } if (on_right(r->i_cm.map[ic])) - r->map_table[oc][ic] = .1f / (float) ic_unconnected_right; + r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right; } } } @@ -887,12 +889,12 @@ static void calc_map_table(pa_resampler *r) { for (ic = 0; ic < r->i_ss.channels; ic++) { if (ic_connected[ic]) { - r->map_table[oc][ic] *= .9f; + r->map_table_f[oc][ic] *= .9f; continue; } if (on_center(r->i_cm.map[ic])) { - r->map_table[oc][ic] = .1f / (float) ic_unconnected_center; + r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center; mixed_in = TRUE; } } @@ -950,7 +952,7 @@ static void calc_map_table(pa_resampler *r) { for (ic = 0; ic < r->i_ss.channels; ic++) { if (ic_connected[ic]) { - r->map_table[oc][ic] *= .75f; + r->map_table_f[oc][ic] *= .75f; continue; } @@ -958,7 +960,7 @@ static void calc_map_table(pa_resampler *r) { continue; if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) - r->map_table[oc][ic] = .375f / (float) ncenter[oc]; + r->map_table_f[oc][ic] = .375f / (float) ncenter[oc]; } } } @@ -975,11 +977,14 @@ static void calc_map_table(pa_resampler *r) { continue; for (oc = 0; oc < r->o_ss.channels; oc++) - r->map_table[oc][ic] = 0.375f / (float) ic_unconnected_lfe; + r->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe; } } } - + /* make an 16:16 int version of the matrix */ + for (oc = 0; oc < r->o_ss.channels; oc++) + for (ic = 0; ic < r->i_ss.channels; ic++) + r->map_table_i[oc][ic] = (int32_t) (r->map_table_f[oc][ic] * 0x10000); s = pa_strbuf_new(); @@ -996,7 +1001,7 @@ static void calc_map_table(pa_resampler *r) { pa_strbuf_printf(s, "O%02u |", oc); for (ic = 0; ic < r->i_ss.channels; ic++) - pa_strbuf_printf(s, " %1.3f", r->map_table[oc][ic]); + pa_strbuf_printf(s, " %1.3f", r->map_table_f[oc][ic]); pa_strbuf_puts(s, "\n"); } @@ -1071,11 +1076,7 @@ static void vectoradd_s16( static void vectoradd_s16_with_fraction( int16_t *d, int dstr, const int16_t *s, int sstr, - int n, float s4) { - - int32_t i4; - - i4 = (int32_t) (s4 * 0x10000); + int n, int32_t i4) { for (; n > 0; n--) { *d = (int16_t) (*d + (((int32_t)*s * i4) >> 16)); @@ -1128,18 +1129,17 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { for (oc = 0; oc < r->o_ss.channels; oc++) { unsigned ic; - static const float one = 1.0; for (ic = 0; ic < r->i_ss.channels; ic++) { - if (r->map_table[oc][ic] <= 0.0) + if (r->map_table_f[oc][ic] <= 0.0) continue; vectoradd_f32( (float*) dst + oc, o_skip, (float*) src + ic, i_skip, (int) n_frames, - r->map_table[oc][ic]); + r->map_table_f[oc][ic]); } } @@ -1152,10 +1152,10 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { for (ic = 0; ic < r->i_ss.channels; ic++) { - if (r->map_table[oc][ic] <= 0.0) + if (r->map_table_f[oc][ic] <= 0.0) continue; - if (r->map_table[oc][ic] >= 1.0) { + if (r->map_table_f[oc][ic] >= 1.0) { vectoradd_s16( (int16_t*) dst + oc, o_skip, @@ -1168,7 +1168,7 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { (int16_t*) dst + oc, o_skip, (int16_t*) src + ic, i_skip, (int) n_frames, - r->map_table[oc][ic]); + r->map_table_i[oc][ic]); } } -- cgit From b4e9942c2f3929b4baf4b53b0561102af7845269 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 19 Aug 2009 16:15:18 +0200 Subject: resample: refactor the channel remapping a little Factor out the channel remap matrix code into a separate function. Keep a pointer to the channel remapping function so we can install custom functions. Catch the common mono->stereo remapping case and install a custom, more optimized function. --- src/pulsecore/resampler.c | 197 +++++++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 88 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index e3473ac5..4fb03ce7 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -44,6 +44,11 @@ /* Number of samples of extra space we allow the resamplers to return */ #define EXTRA_FRAMES 128 +typedef void (*pa_do_remap_func_t) (pa_resampler *r, void *d, const void *s, unsigned n); + +static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, unsigned n); +static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, unsigned n); + struct pa_resampler { pa_resample_method_t method; pa_resample_flags_t flags; @@ -64,6 +69,7 @@ struct pa_resampler { float map_table_f[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; int32_t map_table_i[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; pa_bool_t map_required; + pa_do_remap_func_t do_remap; void (*impl_free)(pa_resampler *r); void (*impl_update_rates)(pa_resampler *r); @@ -1008,6 +1014,17 @@ static void calc_map_table(pa_resampler *r) { pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s)); pa_xfree(t); + + /* find some common channel remappings, fall back to full matrix operation. */ + if (r->i_ss.channels == 1 && r->o_ss.channels == 2 && + r->map_table_i[0][0] == 1.0 && r->map_table_i[1][0] == 1.0) { + r->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo;; + pa_log_debug("Using mono to stereo remapping"); + } else { + r->do_remap = (pa_do_remap_func_t) remap_channels_matrix; + pa_log_debug("Using generic matrix remapping"); + } + } static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) { @@ -1047,49 +1064,111 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) return &r->buf1; } -static void vectoradd_f32( - float *d, int dstr, - const float *s, int sstr, - int n, float s4) { +static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, unsigned n) { + + switch (r->work_format) { + case PA_SAMPLE_FLOAT32NE: + { + float *d, *s; + + d = (float *) dst; + s = (float *) src; + + for (; n > 0; n--) { + *d++ = *s; + *d++ = *s++; + } + break; + } + case PA_SAMPLE_S16NE: + { + int16_t *d, *s; - for (; n > 0; n--) { - *d = (float) (*d + (s4 * *s)); + d = (int16_t *) dst; + s = (int16_t *) src; - s = (const float*) ((const uint8_t*) s + sstr); - d = (float*) ((uint8_t*) d + dstr); + for (; n > 0; n--) { + *d++ = *s; + *d++ = *s++; + } + break; + } + default: + pa_assert_not_reached(); } } -static void vectoradd_s16( - int16_t *d, int dstr, - const int16_t *s, int sstr, - int n) { +static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, unsigned n) { + unsigned oc; + unsigned n_ic, n_oc; - for (; n > 0; n--) { - *d = (int16_t) (*d + *s); + n_ic = r->i_ss.channels; + n_oc = r->o_ss.channels; - s = (const int16_t*) ((const uint8_t*) s + sstr); - d = (int16_t*) ((uint8_t*) d + dstr); - } -} + memset(dst, 0, r->buf2.length); -static void vectoradd_s16_with_fraction( - int16_t *d, int dstr, - const int16_t *s, int sstr, - int n, int32_t i4) { + switch (r->work_format) { + case PA_SAMPLE_FLOAT32NE: + { + float *d, *s; - for (; n > 0; n--) { - *d = (int16_t) (*d + (((int32_t)*s * i4) >> 16)); + for (oc = 0; oc < n_oc; oc++) { + unsigned ic; - s = (const int16_t*) ((const uint8_t*) s + sstr); - d = (int16_t*) ((uint8_t*) d + dstr); + for (ic = 0; ic < n_ic; ic++) { + float vol; + + vol = r->map_table_f[oc][ic]; + + if (vol <= 0.0) + continue; + + d = (float *)dst + oc; + s = (float *)src + ic; + + for (; n > 0; n--, s += n_ic, d += n_oc) + *d += *s * vol; + } + } + + break; + } + case PA_SAMPLE_S16NE: + { + int16_t *d, *s; + + for (oc = 0; oc < n_oc; oc++) { + unsigned ic; + + for (ic = 0; ic < n_ic; ic++) { + int32_t vol; + + vol = r->map_table_i[oc][ic]; + + if (vol <= 0) + continue; + + d = (int16_t *)dst + oc; + s = (int16_t *)src + ic; + + if (vol >= 0x10000) { + for (; n > 0; n--, s += n_ic, d += n_oc) + *d += *s; + } else { + for (; n > 0; n--, s += n_ic, d += n_oc) + *d = (int16_t) (*d + (((int32_t)*s * vol) >> 16)); + } + } + } + break; + } + default: + pa_assert_not_reached(); } } static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { unsigned in_n_samples, out_n_samples, n_frames; - int i_skip, o_skip; - unsigned oc; void *src, *dst; pa_assert(r); @@ -1119,70 +1198,12 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { src = ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index); dst = pa_memblock_acquire(r->buf2.memblock); - memset(dst, 0, r->buf2.length); - - o_skip = (int) (r->w_sz * r->o_ss.channels); - i_skip = (int) (r->w_sz * r->i_ss.channels); - - switch (r->work_format) { - case PA_SAMPLE_FLOAT32NE: - - for (oc = 0; oc < r->o_ss.channels; oc++) { - unsigned ic; - - for (ic = 0; ic < r->i_ss.channels; ic++) { - - if (r->map_table_f[oc][ic] <= 0.0) - continue; - - vectoradd_f32( - (float*) dst + oc, o_skip, - (float*) src + ic, i_skip, - (int) n_frames, - r->map_table_f[oc][ic]); - } - } - - break; - - case PA_SAMPLE_S16NE: - - for (oc = 0; oc < r->o_ss.channels; oc++) { - unsigned ic; - - for (ic = 0; ic < r->i_ss.channels; ic++) { - - if (r->map_table_f[oc][ic] <= 0.0) - continue; - - if (r->map_table_f[oc][ic] >= 1.0) { - - vectoradd_s16( - (int16_t*) dst + oc, o_skip, - (int16_t*) src + ic, i_skip, - (int) n_frames); - - } else - - vectoradd_s16_with_fraction( - (int16_t*) dst + oc, o_skip, - (int16_t*) src + ic, i_skip, - (int) n_frames, - r->map_table_i[oc][ic]); - } - } - - break; - - default: - pa_assert_not_reached(); - } + pa_assert (r->do_remap); + r->do_remap (r, dst, src, n_frames); pa_memblock_release(input->memblock); pa_memblock_release(r->buf2.memblock); - r->buf2.length = out_n_samples * r->w_sz; - return &r->buf2; } -- cgit From d04a6e935f8352a4ffd93cb1aeddac8f605a099a Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 19 Aug 2009 16:23:55 +0200 Subject: resample: fix counters --- src/pulsecore/resampler.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 4fb03ce7..cc57b54e 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -1099,7 +1099,7 @@ static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, un } static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, unsigned n) { - unsigned oc; + unsigned oc, i; unsigned n_ic, n_oc; n_ic = r->i_ss.channels; @@ -1126,7 +1126,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, d = (float *)dst + oc; s = (float *)src + ic; - for (; n > 0; n--, s += n_ic, d += n_oc) + for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += *s * vol; } } @@ -1152,10 +1152,10 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, s = (int16_t *)src + ic; if (vol >= 0x10000) { - for (; n > 0; n--, s += n_ic, d += n_oc) + for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += *s; } else { - for (; n > 0; n--, s += n_ic, d += n_oc) + for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d = (int16_t) (*d + (((int32_t)*s * vol) >> 16)); } } -- cgit From 548b735ccf8474ebe60137375cdda4e58582efc3 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 19 Aug 2009 17:24:23 +0200 Subject: resampler: fix identity check Fix the identity matrix check for mono to stereo. Help the compiler generate better code for the C implementation of the channel remapping code. --- src/pulsecore/resampler.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index cc57b54e..2256516e 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -1017,7 +1017,7 @@ static void calc_map_table(pa_resampler *r) { /* find some common channel remappings, fall back to full matrix operation. */ if (r->i_ss.channels == 1 && r->o_ss.channels == 2 && - r->map_table_i[0][0] == 1.0 && r->map_table_i[1][0] == 1.0) { + r->map_table_f[0][0] >= 1.0 && r->map_table_f[1][0] >= 1.0) { r->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo;; pa_log_debug("Using mono to stereo remapping"); } else { @@ -1074,10 +1074,8 @@ static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, un d = (float *) dst; s = (float *) src; - for (; n > 0; n--) { - *d++ = *s; - *d++ = *s++; - } + for (; n > 0; n--, s++, d += 2) + d[0] = d[1] = *s; break; } case PA_SAMPLE_S16NE: @@ -1087,10 +1085,8 @@ static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, un d = (int16_t *) dst; s = (int16_t *) src; - for (; n > 0; n--) { - *d++ = *s; - *d++ = *s++; - } + for (; n > 0; n--, s++, d += 2) + d[0] = d[1] = *s; break; } default: @@ -1156,7 +1152,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, *d += *s; } else { for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d = (int16_t) (*d + (((int32_t)*s * vol) >> 16)); + *d += (int16_t) (((int32_t)*s * vol) >> 16); } } } -- cgit From 3cc1278dcf44c9fb93bfd2725a2f75de1958cf23 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 19 Aug 2009 20:47:48 +0200 Subject: resampler: avoid some multiplies when we can --- src/pulsecore/resampler.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 2256516e..43771dc8 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -1122,8 +1122,13 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, d = (float *)dst + oc; s = (float *)src + ic; - for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d += *s * vol; + if (vol >= 1.0) { + for (i = n; i > 0; i--, s += n_ic, d += n_oc) + *d += *s; + } else { + for (i = n; i > 0; i--, s += n_ic, d += n_oc) + *d += *s * vol; + } } } -- cgit From f09b51198f43d79b22cb92b5223d01a7ab339d9f Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 20 Aug 2009 10:56:20 +0200 Subject: whitespace fixes --- src/pulsecore/resampler.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 21 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 43771dc8..5a6c398e 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -1065,30 +1065,53 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) } static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, unsigned n) { - + unsigned i; + switch (r->work_format) { case PA_SAMPLE_FLOAT32NE: { float *d, *s; - d = (float *) dst; - s = (float *) src; + d = (float *) dst; + s = (float *) src; - for (; n > 0; n--, s++, d += 2) - d[0] = d[1] = *s; - break; - } + for (i = n >> 2; i; i--) { + d[0] = d[1] = s[0]; + d[2] = d[3] = s[1]; + d[4] = d[5] = s[2]; + d[6] = d[7] = s[3]; + s += 4; + d += 8; + } + for (i = n & 3; i; i--) { + d[0] = d[1] = s[0]; + s++; + d += 2; + } + break; + } case PA_SAMPLE_S16NE: { int16_t *d, *s; - d = (int16_t *) dst; - s = (int16_t *) src; + d = (int16_t *) dst; + s = (int16_t *) src; - for (; n > 0; n--, s++, d += 2) - d[0] = d[1] = *s; - break; - } + for (i = n >> 2; i; i--) { + d[0] = d[1] = s[0]; + d[2] = d[3] = s[1]; + d[4] = d[5] = s[2]; + d[6] = d[7] = s[3]; + s += 4; + d += 8; + } + for (i = n & 3; i; i--) { + d[0] = d[1] = s[0]; + s++; + d += 2; + } + break; + } default: pa_assert_not_reached(); } @@ -1114,7 +1137,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, for (ic = 0; ic < n_ic; ic++) { float vol; - vol = r->map_table_f[oc][ic]; + vol = r->map_table_f[oc][ic]; if (vol <= 0.0) continue; @@ -1122,18 +1145,18 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, d = (float *)dst + oc; s = (float *)src + ic; - if (vol >= 1.0) { + if (vol >= 1.0) { for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += *s; - } else { + } else { for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += *s * vol; - } + } } } break; - } + } case PA_SAMPLE_S16NE: { int16_t *d, *s; @@ -1144,7 +1167,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, for (ic = 0; ic < n_ic; ic++) { int32_t vol; - vol = r->map_table_i[oc][ic]; + vol = r->map_table_i[oc][ic]; if (vol <= 0) continue; @@ -1158,11 +1181,11 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, } else { for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += (int16_t) (((int32_t)*s * vol) >> 16); - } + } } } break; - } + } default: pa_assert_not_reached(); } -- cgit From a3f4a4f6ba741a996442d7a80cc3e267fab705fb Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 20 Aug 2009 17:54:45 +0200 Subject: resamples; refactor the channel remapping bits Move the channel remapping bits into a separate structure. We'll make this structure global so that optimized versions can use it to perform the channel remapping. --- src/pulsecore/resampler.c | 186 ++++++++++++++++++++++++++-------------------- 1 file changed, 105 insertions(+), 81 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 5a6c398e..0d8ca010 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -44,10 +44,20 @@ /* Number of samples of extra space we allow the resamplers to return */ #define EXTRA_FRAMES 128 -typedef void (*pa_do_remap_func_t) (pa_resampler *r, void *d, const void *s, unsigned n); +typedef struct pa_remap pa_remap_t; -static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, unsigned n); -static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, unsigned n); +typedef void (*pa_do_remap_func_t) (pa_remap_t *m, void *d, const void *s, unsigned n); + +struct pa_remap { + pa_sample_format_t *format; + pa_sample_spec *i_ss, *o_ss; + float map_table_f[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; + int32_t map_table_i[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; + pa_do_remap_func_t do_remap; +}; + +static void remap_channels_matrix (pa_remap_t *m, void *dst, const void *src, unsigned n); +static void remap_mono_to_stereo(pa_remap_t *m, void *dst, const void *src, unsigned n); struct pa_resampler { pa_resample_method_t method; @@ -66,10 +76,8 @@ struct pa_resampler { pa_convert_func_t to_work_format_func; pa_convert_func_t from_work_format_func; - float map_table_f[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; - int32_t map_table_i[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; + pa_remap_t remap; pa_bool_t map_required; - pa_do_remap_func_t do_remap; void (*impl_free)(pa_resampler *r); void (*impl_update_rates)(pa_resampler *r); @@ -218,6 +226,11 @@ pa_resampler* pa_resampler_new( r->i_ss = *a; r->o_ss = *b; + /* set up the remap structure */ + r->remap.i_ss = &r->i_ss; + r->remap.o_ss = &r->o_ss; + r->remap.format = &r->work_format; + if (am) r->i_cm = *am; else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT)) @@ -584,33 +597,41 @@ static int front_rear_side(pa_channel_position_t p) { static void calc_map_table(pa_resampler *r) { unsigned oc, ic; + unsigned n_oc, n_ic; pa_bool_t ic_connected[PA_CHANNELS_MAX]; pa_bool_t remix; pa_strbuf *s; char *t; + pa_remap_t *m; pa_assert(r); 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))))) return; - memset(r->map_table_f, 0, sizeof(r->map_table_f)); - memset(r->map_table_i, 0, sizeof(r->map_table_i)); + m = &r->remap; + + n_oc = r->o_ss.channels; + n_ic = r->i_ss.channels; + + memset(m->map_table_f, 0, sizeof(m->map_table_f)); + memset(m->map_table_i, 0, sizeof(m->map_table_i)); + memset(ic_connected, 0, sizeof(ic_connected)); remix = (r->flags & (PA_RESAMPLER_NO_REMAP|PA_RESAMPLER_NO_REMIX)) == 0; - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { pa_bool_t oc_connected = FALSE; pa_channel_position_t b = r->o_cm.map[oc]; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { pa_channel_position_t a = r->i_cm.map[ic]; if (r->flags & PA_RESAMPLER_NO_REMAP) { /* We shall not do any remapping. Hence, just check by index */ if (ic == oc) - r->map_table_f[oc][ic] = 1.0; + m->map_table_f[oc][ic] = 1.0; continue; } @@ -619,7 +640,7 @@ static void calc_map_table(pa_resampler *r) { /* We shall not do any remixing. Hence, just check by name */ if (a == b) - r->map_table_f[oc][ic] = 1.0; + m->map_table_f[oc][ic] = 1.0; continue; } @@ -694,7 +715,7 @@ static void calc_map_table(pa_resampler *r) { */ if (a == b || a == PA_CHANNEL_POSITION_MONO || b == PA_CHANNEL_POSITION_MONO) { - r->map_table_f[oc][ic] = 1.0; + m->map_table_f[oc][ic] = 1.0; oc_connected = TRUE; ic_connected[ic] = TRUE; @@ -712,14 +733,14 @@ static void calc_map_table(pa_resampler *r) { /* We are not connected and on the left side, let's * average all left side input channels. */ - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_left(r->i_cm.map[ic])) n++; if (n > 0) - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_left(r->i_cm.map[ic])) { - r->map_table_f[oc][ic] = 1.0f / (float) n; + m->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -733,14 +754,14 @@ static void calc_map_table(pa_resampler *r) { /* We are not connected and on the right side, let's * average all right side input channels. */ - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_right(r->i_cm.map[ic])) n++; if (n > 0) - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_right(r->i_cm.map[ic])) { - r->map_table_f[oc][ic] = 1.0f / (float) n; + m->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -754,14 +775,14 @@ static void calc_map_table(pa_resampler *r) { /* We are not connected and at the center. Let's * average all center input channels. */ - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_center(r->i_cm.map[ic])) n++; if (n > 0) { - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_center(r->i_cm.map[ic])) { - r->map_table_f[oc][ic] = 1.0f / (float) n; + m->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } } else { @@ -771,14 +792,14 @@ static void calc_map_table(pa_resampler *r) { n = 0; - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) n++; if (n > 0) - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) { - r->map_table_f[oc][ic] = 1.0f / (float) n; + m->map_table_f[oc][ic] = 1.0f / (float) n; ic_connected[ic] = TRUE; } @@ -792,12 +813,12 @@ static void calc_map_table(pa_resampler *r) { /* We are not connected and an LFE. Let's average all * channels for LFE. */ - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (!(r->flags & PA_RESAMPLER_NO_LFE)) - r->map_table_f[oc][ic] = 1.0f / (float) r->i_ss.channels; + m->map_table_f[oc][ic] = 1.0f / (float) n_ic; else - r->map_table_f[oc][ic] = 0; + m->map_table_f[oc][ic] = 0; /* Please note that a channel connected to LFE * doesn't really count as connected. */ @@ -813,7 +834,7 @@ static void calc_map_table(pa_resampler *r) { ic_unconnected_center = 0, ic_unconnected_lfe = 0; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { pa_channel_position_t a = r->i_cm.map[ic]; if (ic_connected[ic]) @@ -836,20 +857,20 @@ static void calc_map_table(pa_resampler *r) { * the left side by .9 and add in our averaged unconnected * channels multplied by .1 */ - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_left(r->o_cm.map[oc])) continue; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (ic_connected[ic]) { - r->map_table_f[oc][ic] *= .9f; + m->map_table_f[oc][ic] *= .9f; continue; } if (on_left(r->i_cm.map[ic])) - r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left; + m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left; } } } @@ -861,20 +882,20 @@ static void calc_map_table(pa_resampler *r) { * the right side by .9 and add in our averaged unconnected * channels multplied by .1 */ - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_right(r->o_cm.map[oc])) continue; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (ic_connected[ic]) { - r->map_table_f[oc][ic] *= .9f; + m->map_table_f[oc][ic] *= .9f; continue; } if (on_right(r->i_cm.map[ic])) - r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right; + m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right; } } } @@ -887,20 +908,20 @@ static void calc_map_table(pa_resampler *r) { * the center side by .9 and add in our averaged unconnected * channels multplied by .1 */ - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_center(r->o_cm.map[oc])) continue; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (ic_connected[ic]) { - r->map_table_f[oc][ic] *= .9f; + m->map_table_f[oc][ic] *= .9f; continue; } if (on_center(r->i_cm.map[ic])) { - r->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center; + m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center; mixed_in = TRUE; } } @@ -918,7 +939,7 @@ static void calc_map_table(pa_resampler *r) { it into left and right. Using .375 and 0.75 as factors. */ - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (ic_connected[ic]) continue; @@ -926,7 +947,7 @@ static void calc_map_table(pa_resampler *r) { if (!on_center(r->i_cm.map[ic])) continue; - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc])) continue; @@ -937,7 +958,7 @@ static void calc_map_table(pa_resampler *r) { } } - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc])) continue; @@ -947,7 +968,7 @@ static void calc_map_table(pa_resampler *r) { } } - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc])) continue; @@ -955,10 +976,10 @@ static void calc_map_table(pa_resampler *r) { if (ncenter[oc] <= 0) continue; - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (ic_connected[ic]) { - r->map_table_f[oc][ic] *= .75f; + m->map_table_f[oc][ic] *= .75f; continue; } @@ -966,7 +987,7 @@ static void calc_map_table(pa_resampler *r) { continue; if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) - r->map_table_f[oc][ic] = .375f / (float) ncenter[oc]; + m->map_table_f[oc][ic] = .375f / (float) ncenter[oc]; } } } @@ -977,37 +998,37 @@ static void calc_map_table(pa_resampler *r) { /* OK, so there is an unconnected LFE channel. Let's mix * it into all channels, with factor 0.375 */ - for (ic = 0; ic < r->i_ss.channels; ic++) { + for (ic = 0; ic < n_ic; ic++) { if (!on_lfe(r->i_cm.map[ic])) continue; - for (oc = 0; oc < r->o_ss.channels; oc++) - r->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe; + for (oc = 0; oc < n_oc; oc++) + m->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe; } } } /* make an 16:16 int version of the matrix */ - for (oc = 0; oc < r->o_ss.channels; oc++) - for (ic = 0; ic < r->i_ss.channels; ic++) - r->map_table_i[oc][ic] = (int32_t) (r->map_table_f[oc][ic] * 0x10000); + for (oc = 0; oc < n_oc; oc++) + for (ic = 0; ic < n_ic; ic++) + m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000); s = pa_strbuf_new(); pa_strbuf_printf(s, " "); - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) pa_strbuf_printf(s, " I%02u ", ic); pa_strbuf_puts(s, "\n +"); - for (ic = 0; ic < r->i_ss.channels; ic++) + for (ic = 0; ic < n_ic; ic++) pa_strbuf_printf(s, "------"); pa_strbuf_puts(s, "\n"); - for (oc = 0; oc < r->o_ss.channels; oc++) { + for (oc = 0; oc < n_oc; oc++) { pa_strbuf_printf(s, "O%02u |", oc); - for (ic = 0; ic < r->i_ss.channels; ic++) - pa_strbuf_printf(s, " %1.3f", r->map_table_f[oc][ic]); + for (ic = 0; ic < n_ic; ic++) + pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]); pa_strbuf_puts(s, "\n"); } @@ -1016,13 +1037,13 @@ static void calc_map_table(pa_resampler *r) { pa_xfree(t); /* find some common channel remappings, fall back to full matrix operation. */ - if (r->i_ss.channels == 1 && r->o_ss.channels == 2 && - r->map_table_f[0][0] >= 1.0 && r->map_table_f[1][0] >= 1.0) { - r->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo;; - pa_log_debug("Using mono to stereo remapping"); + if (n_ic == 1 && n_oc == 2 && + m->map_table_f[0][0] >= 1.0 && m->map_table_f[1][0] >= 1.0) { + m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo;; + pa_log_info("Using mono to stereo remapping"); } else { - r->do_remap = (pa_do_remap_func_t) remap_channels_matrix; - pa_log_debug("Using generic matrix remapping"); + m->do_remap = (pa_do_remap_func_t) remap_channels_matrix; + pa_log_info("Using generic matrix remapping"); } } @@ -1064,10 +1085,10 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) return &r->buf1; } -static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, unsigned n) { +static void remap_mono_to_stereo(pa_remap_t *m, void *dst, const void *src, unsigned n) { unsigned i; - switch (r->work_format) { + switch (*m->format) { case PA_SAMPLE_FLOAT32NE: { float *d, *s; @@ -1117,27 +1138,26 @@ static void remap_mono_to_stereo(pa_resampler *r, void *dst, const void *src, un } } -static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, unsigned n) { - unsigned oc, i; +static void remap_channels_matrix (pa_remap_t *m, void *dst, const void *src, unsigned n) { + unsigned oc, ic, i; unsigned n_ic, n_oc; - n_ic = r->i_ss.channels; - n_oc = r->o_ss.channels; + n_ic = m->i_ss->channels; + n_oc = m->o_ss->channels; - memset(dst, 0, r->buf2.length); - - switch (r->work_format) { + switch (*m->format) { case PA_SAMPLE_FLOAT32NE: { float *d, *s; + memset(dst, 0, n * sizeof (float) * n_oc); + for (oc = 0; oc < n_oc; oc++) { - unsigned ic; for (ic = 0; ic < n_ic; ic++) { float vol; - vol = r->map_table_f[oc][ic]; + vol = m->map_table_f[oc][ic]; if (vol <= 0.0) continue; @@ -1161,13 +1181,14 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, { int16_t *d, *s; + memset(dst, 0, n * sizeof (int16_t) * n_oc); + for (oc = 0; oc < n_oc; oc++) { - unsigned ic; for (ic = 0; ic < n_ic; ic++) { int32_t vol; - vol = r->map_table_i[oc][ic]; + vol = m->map_table_i[oc][ic]; if (vol <= 0) continue; @@ -1181,7 +1202,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, } else { for (i = n; i > 0; i--, s += n_ic, d += n_oc) *d += (int16_t) (((int32_t)*s * vol) >> 16); - } + } } } break; @@ -1194,6 +1215,7 @@ static void remap_channels_matrix (pa_resampler *r, void *dst, const void *src, static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { unsigned in_n_samples, out_n_samples, n_frames; void *src, *dst; + pa_remap_t *remap; pa_assert(r); pa_assert(input); @@ -1222,8 +1244,10 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { src = ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index); dst = pa_memblock_acquire(r->buf2.memblock); - pa_assert (r->do_remap); - r->do_remap (r, dst, src, n_frames); + remap = &r->remap; + + pa_assert (remap->do_remap); + remap->do_remap (remap, dst, src, n_frames); pa_memblock_release(input->memblock); pa_memblock_release(r->buf2.memblock); -- cgit From ac1f2e0a2e0707636aabd48baa57c124a877f834 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 20 Aug 2009 18:23:42 +0200 Subject: remap: move remapping code in separate file Move the remapping code into a separate file. Have functions to install custom init functions that can install optimized versions, when they want. --- src/pulsecore/resampler.c | 155 +--------------------------------------------- 1 file changed, 3 insertions(+), 152 deletions(-) (limited to 'src/pulsecore/resampler.c') diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index 0d8ca010..f1bfa156 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -40,25 +40,11 @@ #include "ffmpeg/avcodec.h" #include "resampler.h" +#include "remap.h" /* Number of samples of extra space we allow the resamplers to return */ #define EXTRA_FRAMES 128 -typedef struct pa_remap pa_remap_t; - -typedef void (*pa_do_remap_func_t) (pa_remap_t *m, void *d, const void *s, unsigned n); - -struct pa_remap { - pa_sample_format_t *format; - pa_sample_spec *i_ss, *o_ss; - float map_table_f[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; - int32_t map_table_i[PA_CHANNELS_MAX][PA_CHANNELS_MAX]; - pa_do_remap_func_t do_remap; -}; - -static void remap_channels_matrix (pa_remap_t *m, void *dst, const void *src, unsigned n); -static void remap_mono_to_stereo(pa_remap_t *m, void *dst, const void *src, unsigned n); - struct pa_resampler { pa_resample_method_t method; pa_resample_flags_t flags; @@ -1036,16 +1022,8 @@ static void calc_map_table(pa_resampler *r) { pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s)); pa_xfree(t); - /* find some common channel remappings, fall back to full matrix operation. */ - if (n_ic == 1 && n_oc == 2 && - m->map_table_f[0][0] >= 1.0 && m->map_table_f[1][0] >= 1.0) { - m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo;; - pa_log_info("Using mono to stereo remapping"); - } else { - m->do_remap = (pa_do_remap_func_t) remap_channels_matrix; - pa_log_info("Using generic matrix remapping"); - } - + /* initialize the remapping function */ + pa_init_remap (m); } static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) { @@ -1085,133 +1063,6 @@ static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) return &r->buf1; } -static void remap_mono_to_stereo(pa_remap_t *m, void *dst, const void *src, unsigned n) { - unsigned i; - - switch (*m->format) { - case PA_SAMPLE_FLOAT32NE: - { - float *d, *s; - - d = (float *) dst; - s = (float *) src; - - for (i = n >> 2; i; i--) { - d[0] = d[1] = s[0]; - d[2] = d[3] = s[1]; - d[4] = d[5] = s[2]; - d[6] = d[7] = s[3]; - s += 4; - d += 8; - } - for (i = n & 3; i; i--) { - d[0] = d[1] = s[0]; - s++; - d += 2; - } - break; - } - case PA_SAMPLE_S16NE: - { - int16_t *d, *s; - - d = (int16_t *) dst; - s = (int16_t *) src; - - for (i = n >> 2; i; i--) { - d[0] = d[1] = s[0]; - d[2] = d[3] = s[1]; - d[4] = d[5] = s[2]; - d[6] = d[7] = s[3]; - s += 4; - d += 8; - } - for (i = n & 3; i; i--) { - d[0] = d[1] = s[0]; - s++; - d += 2; - } - break; - } - default: - pa_assert_not_reached(); - } -} - -static void remap_channels_matrix (pa_remap_t *m, void *dst, const void *src, unsigned n) { - unsigned oc, ic, i; - unsigned n_ic, n_oc; - - n_ic = m->i_ss->channels; - n_oc = m->o_ss->channels; - - switch (*m->format) { - case PA_SAMPLE_FLOAT32NE: - { - float *d, *s; - - memset(dst, 0, n * sizeof (float) * n_oc); - - for (oc = 0; oc < n_oc; oc++) { - - for (ic = 0; ic < n_ic; ic++) { - float vol; - - vol = m->map_table_f[oc][ic]; - - if (vol <= 0.0) - continue; - - d = (float *)dst + oc; - s = (float *)src + ic; - - if (vol >= 1.0) { - for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d += *s; - } else { - for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d += *s * vol; - } - } - } - - break; - } - case PA_SAMPLE_S16NE: - { - int16_t *d, *s; - - memset(dst, 0, n * sizeof (int16_t) * n_oc); - - for (oc = 0; oc < n_oc; oc++) { - - for (ic = 0; ic < n_ic; ic++) { - int32_t vol; - - vol = m->map_table_i[oc][ic]; - - if (vol <= 0) - continue; - - d = (int16_t *)dst + oc; - s = (int16_t *)src + ic; - - if (vol >= 0x10000) { - for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d += *s; - } else { - for (i = n; i > 0; i--, s += n_ic, d += n_oc) - *d += (int16_t) (((int32_t)*s * vol) >> 16); - } - } - } - break; - } - default: - pa_assert_not_reached(); - } -} - static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) { unsigned in_n_samples, out_n_samples, n_frames; void *src, *dst; -- cgit