diff --git a/spa/plugins/audioconvert/fmt-ops.h b/spa/plugins/audioconvert/fmt-ops.h index 9ad6c7eb9..cfc694967 100644 --- a/spa/plugins/audioconvert/fmt-ops.h +++ b/spa/plugins/audioconvert/fmt-ops.h @@ -218,3 +218,5 @@ DEFINE_FUNCTION(f32d_to_s16_4, avx2); DEFINE_FUNCTION(f32d_to_s16_2, avx2); DEFINE_FUNCTION(f32d_to_s16, avx2); #endif + +#undef DEFINE_FUNCTION diff --git a/spa/plugins/audioconvert/merger.c b/spa/plugins/audioconvert/merger.c index 243e646d2..25304519c 100644 --- a/spa/plugins/audioconvert/merger.c +++ b/spa/plugins/audioconvert/merger.c @@ -43,6 +43,7 @@ #include #include +#include "volume-ops.h" #include "fmt-ops.h" #define NAME "merger" @@ -107,10 +108,12 @@ struct impl { uint32_t monitor_count; struct port in_ports[MAX_PORTS]; struct port out_ports[MAX_PORTS + 1]; + float monitor_volume[MAX_PORTS]; struct spa_audio_info format; unsigned int have_profile:1; + struct volume volume; struct convert conv; uint32_t cpu_flags; unsigned int is_passthrough:1; @@ -959,7 +962,7 @@ static inline int get_out_buffer(struct impl *this, struct port *port, struct bu return 0; } -static inline int handle_monitor(struct impl *this, const void *data, int n_samples, struct port *outport) +static inline int handle_monitor(struct impl *this, const void *data, float volume, int n_samples, struct port *outport) { struct buffer *dbuf; struct spa_data *dd; @@ -975,10 +978,10 @@ static inline int handle_monitor(struct impl *this, const void *data, int n_samp spa_log_trace(this->log, "%p: io %p %08x", this, outport->io, dd->flags); - if (SPA_FLAG_IS_SET(dd->flags, SPA_DATA_FLAG_DYNAMIC)) + if (SPA_FLAG_IS_SET(dd->flags, SPA_DATA_FLAG_DYNAMIC) && volume == VOLUME_NORM) dd->data = (void*)data; else - spa_memcpy(dd->data, data, size); + volume_process(&this->volume, dd->data, data, volume, size / outport->stride); return res; } @@ -1045,7 +1048,9 @@ static int impl_node_process(void *object) } for (i = 0; i < this->monitor_count; i++) - handle_monitor(this, src_datas[i], n_samples, GET_OUT_PORT(this, i + 1)); + handle_monitor(this, src_datas[i], + this->monitor_volume[i], n_samples, + GET_OUT_PORT(this, i + 1)); for (i = 0; i < n_dst_datas; i++) { uint32_t dst_remap = this->dst_remap[i]; @@ -1126,6 +1131,7 @@ impl_init(const struct spa_handle_factory *factory, { struct impl *this; struct port *port; + uint32_t i; spa_return_val_if_fail(factory != NULL, -EINVAL); spa_return_val_if_fail(handle != NULL, -EINVAL); @@ -1174,6 +1180,11 @@ impl_init(const struct spa_handle_factory *factory, port->info.n_params = 5; spa_list_init(&port->queue); + this->volume.cpu_flags = this->cpu_flags; + volume_init(&this->volume); + for (i = 0; i < MAX_PORTS; i++) + this->monitor_volume[i] = VOLUME_NORM; + return 0; } diff --git a/spa/plugins/audioconvert/meson.build b/spa/plugins/audioconvert/meson.build index a9df5be59..261f11cde 100644 --- a/spa/plugins/audioconvert/meson.build +++ b/spa/plugins/audioconvert/meson.build @@ -14,6 +14,7 @@ if have_sse audioconvert_sse = static_library('audioconvert_sse', ['resample-native-sse.c', 'resample-peaks-sse.c', + 'volume-ops-sse.c', 'channelmix-ops-sse.c' ], c_args : [sse_args, '-O3', '-DHAVE_SSE'], include_directories : [spa_inc], @@ -92,7 +93,9 @@ audioconvert = static_library('audioconvert', 'channelmix-ops-c.c', 'resample-native.c', 'resample-peaks.c', - 'fmt-ops-c.c' ], + 'fmt-ops-c.c', + 'volume-ops.c', + 'volume-ops-c.c' ], c_args : [ simd_cargs, '-O3'], link_with : simd_dependencies, include_directories : [spa_inc], diff --git a/spa/plugins/audioconvert/volume-ops-c.c b/spa/plugins/audioconvert/volume-ops-c.c new file mode 100644 index 000000000..0cc6f5f9a --- /dev/null +++ b/spa/plugins/audioconvert/volume-ops-c.c @@ -0,0 +1,45 @@ +/* Spa + * + * Copyright © 2021 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "volume-ops.h" + +void +volume_f32_c(struct volume *vol, void * SPA_RESTRICT dst, + const void * SPA_RESTRICT src, float volume, uint32_t n_samples) +{ + uint32_t n; + float *d = (float*)dst; + const float *s = (const float*)src; + + if (volume == VOLUME_MIN) { + memset(d, 0, n_samples * sizeof(float)); + } + else if (volume == VOLUME_NORM) { + spa_memcpy(d, s, n_samples * sizeof(float)); + } + else { + for (n = 0; n < n_samples; n++) + d[n] = s[n] * volume; + } +} diff --git a/spa/plugins/audioconvert/volume-ops-sse.c b/spa/plugins/audioconvert/volume-ops-sse.c new file mode 100644 index 000000000..cd1f3cc6a --- /dev/null +++ b/spa/plugins/audioconvert/volume-ops-sse.c @@ -0,0 +1,66 @@ +/* Spa + * + * Copyright © 2021 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "volume-ops.h" + +#include + +void +volume_f32_sse(struct volume *vol, void * SPA_RESTRICT dst, + const void * SPA_RESTRICT src, float volume, uint32_t n_samples) +{ + uint32_t n, unrolled; + float *d = (float*)dst; + const float *s = (const float*)src; + + if (volume == VOLUME_MIN) { + memset(d, 0, n_samples * sizeof(float)); + } + else if (volume == VOLUME_NORM) { + spa_memcpy(d, s, n_samples * sizeof(float)); + } + else { + __m128 t[4]; + const __m128 vol = _mm_set1_ps(volume); + + if (SPA_IS_ALIGNED(d, 16) && + SPA_IS_ALIGNED(s, 16)) + unrolled = n_samples & ~15; + else + unrolled = 0; + + for(n = 0; n < unrolled; n += 16) { + t[0] = _mm_load_ps(&s[n]); + t[1] = _mm_load_ps(&s[n+4]); + t[2] = _mm_load_ps(&s[n+8]); + t[3] = _mm_load_ps(&s[n+12]); + _mm_store_ps(&d[n], _mm_mul_ps(t[0], vol)); + _mm_store_ps(&d[n+4], _mm_mul_ps(t[1], vol)); + _mm_store_ps(&d[n+8], _mm_mul_ps(t[2], vol)); + _mm_store_ps(&d[n+12], _mm_mul_ps(t[3], vol)); + } + for(; n < n_samples; n++) + _mm_store_ss(&d[n], _mm_mul_ss(_mm_load_ss(&s[n]), vol)); + } +} diff --git a/spa/plugins/audioconvert/volume-ops.c b/spa/plugins/audioconvert/volume-ops.c new file mode 100644 index 000000000..32a0d22cb --- /dev/null +++ b/spa/plugins/audioconvert/volume-ops.c @@ -0,0 +1,79 @@ +/* Spa + * + * Copyright © 2021 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "volume-ops.h" + +typedef void (*volume_func_t) (struct volume *vol, void * SPA_RESTRICT dst, + const void * SPA_RESTRICT src, float volume, uint32_t n_samples); + +static const struct volume_info { + volume_func_t process; + uint32_t cpu_flags; +} volume_table[] = +{ +#if defined (HAVE_SSE) + { volume_f32_sse, SPA_CPU_FLAG_SSE }, +#endif + { volume_f32_c, 0 }, +}; + +#define MATCH_CPU_FLAGS(a,b) ((a) == 0 || ((a) & (b)) == a) + +static const struct volume_info *find_volume_info(uint32_t cpu_flags) +{ + size_t i; + for (i = 0; i < SPA_N_ELEMENTS(volume_table); i++) { + if (!MATCH_CPU_FLAGS(volume_table[i].cpu_flags, cpu_flags)) + continue; + return &volume_table[i]; + } + return NULL; +} + +static void impl_volume_free(struct volume *vol) +{ + vol->process = NULL; +} + +int volume_init(struct volume *vol) +{ + const struct volume_info *info; + + info = find_volume_info(vol->cpu_flags); + if (info == NULL) + return -ENOTSUP; + + vol->free = impl_volume_free; + vol->process = info->process; + return 0; +}