mixer: Move floatmix to the audio mixer plugin

Move floatmix to the audiomixer plugin and change the name to
AUDIO_MIXER_DSP.
Add runtime selectable sse and sse2 optimizations.
Load the port mixer plugin dynamically based on the factory_name.
Add some more debug
This commit is contained in:
Wim Taymans 2019-10-03 16:20:12 +02:00
parent 27eabede35
commit 0ecbe4844e
13 changed files with 482 additions and 404 deletions

View file

@ -40,9 +40,14 @@ extern "C" {
/* audio mixer */
#define SPA_NAME_AUDIO_MIXER "audio.mix" /**< mixes the raw audio on N input
#define SPA_NAME_AUDIO_MIXER "audio.mixer" /**< mixes the raw audio on N input
* ports together on the output
* port */
#define SPA_NAME_AUDIO_MIXER_DSP "audio.mixer.dsp" /**< mixes mono audio with fixed input
* and output buffer sizes. supported
* formats must include f32 and
* optionally f64 and s24_32 */
/** audio processing */
#define SPA_NAME_AUDIO_PROCESS_FORMAT "audio.process.format" /**< processes raw audio from one format
* to another */

View file

@ -27,6 +27,7 @@
#include <stdio.h>
#include <spa/support/log.h>
#include <spa/support/cpu.h>
#include <spa/utils/list.h>
#include <spa/utils/names.h>
#include <spa/node/node.h>
@ -96,8 +97,10 @@ struct impl {
struct spa_node node;
struct spa_log *log;
struct spa_cpu *cpu;
uint32_t cpu_flags;
struct spa_audiomixer_ops ops;
struct mix_ops ops;
uint64_t info_all;
struct spa_node_info info;
@ -115,12 +118,6 @@ struct impl {
struct spa_audio_info format;
uint32_t bpf;
mix_clear_func_t clear;
mix_func_t copy;
mix_func_t add;
mix_scale_func_t copy_scale;
mix_scale_func_t add_scale;
bool started;
};
@ -481,24 +478,12 @@ static int port_set_format(void *object,
if (memcmp(&info, &this->format, sizeof(struct spa_audio_info)))
return -EINVAL;
} else {
if (info.info.raw.format == SPA_AUDIO_FORMAT_S16) {
this->clear = this->ops.clear[FMT_S16];
this->copy = this->ops.copy[FMT_S16];
this->add = this->ops.add[FMT_S16];
this->copy_scale = this->ops.copy_scale[FMT_S16];
this->add_scale = this->ops.add_scale[FMT_S16];
this->bpf = sizeof(int16_t) * info.info.raw.channels;
}
else if (info.info.raw.format == SPA_AUDIO_FORMAT_F32) {
this->clear = this->ops.clear[FMT_F32];
this->copy = this->ops.copy[FMT_F32];
this->add = this->ops.add[FMT_F32];
this->copy_scale = this->ops.copy_scale[FMT_F32];
this->add_scale = this->ops.add_scale[FMT_F32];
this->bpf = sizeof(float) * info.info.raw.channels;
}
else
return -EINVAL;
this->ops.fmt = info.info.raw.format;
this->ops.n_channels = info.info.raw.channels;
this->ops.cpu_flags = this->cpu_flags;
if ((res = mix_ops_init(&this->ops)) < 0)
return res;
this->have_format = true;
this->format = info;
@ -651,6 +636,8 @@ add_port_data(struct impl *this, void *out, size_t outsize, struct port *port, i
void *data;
double volume = *port->io_volume;
bool mute = *port->io_mute;
const void *s0[2], *s1[2];
uint32_t n_src;
b = spa_list_first(&port->queue, struct buffer, link);
@ -668,29 +655,24 @@ add_port_data(struct impl *this, void *out, size_t outsize, struct port *port, i
len1 = SPA_MIN(outsize, maxsize - offset);
len2 = outsize - len1;
if (volume < 0.001 || mute) {
/* silence, for the first layer clear, otherwise do nothing */
if (layer == 0) {
this->clear(out, len1);
if (len2 > 0)
this->clear(SPA_MEMBER(out, len1, void), len2);
}
n_src = 0;
if (layer > 0) {
s0[n_src] = out;
s1[n_src] = SPA_MEMBER(out, len1, void);
n_src++;
}
else if (volume < 0.999 || volume > 1.001) {
mix_scale_func_t mix = layer == 0 ? this->copy_scale : this->add_scale;
s0[n_src] = SPA_MEMBER(data, offset, void);
s1[n_src] = data;
n_src++;
mix(out, SPA_MEMBER(data, offset, void), volume, len1);
if (len2 > 0)
mix(SPA_MEMBER(out, len1, void), data, volume, len2);
if (volume < 0.001 || mute) {
/* silence, do nothing */
}
else {
mix_func_t mix = layer == 0 ? this->copy : this->add;
mix(out, SPA_MEMBER(data, offset, void), len1);
mix_ops_process(&this->ops, out, s0, n_src, len1);
if (len2 > 0)
mix(SPA_MEMBER(out, len1, void), data, len2);
mix_ops_process(&this->ops, SPA_MEMBER(out, len1, void), s1, n_src, len2);
}
port->queued_bytes -= outsize;
if (port->queued_bytes == 0) {
@ -863,6 +845,13 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
static int impl_clear(struct spa_handle *handle)
{
struct impl *this;
spa_return_val_if_fail(handle != NULL, -EINVAL);
this = (struct impl *) handle;
mix_ops_free(&this->ops);
return 0;
}
@ -893,9 +882,17 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
this->log = support[i].data;
break;
case SPA_TYPE_INTERFACE_CPU:
this->cpu = support[i].data;
break;
}
}
if (this->cpu)
this->cpu_flags = spa_cpu_get_flags(this->cpu);
spa_hook_list_init(&this->hooks);
@ -929,8 +926,6 @@ impl_init(const struct spa_handle_factory *factory,
spa_list_init(&port->queue);
spa_audiomixer_get_ops(&this->ops);
return 0;
}

View file

@ -1,7 +1,46 @@
audiomixer_sources = ['audiomixer.c', 'mix-ops.c', 'plugin.c']
audiomixer_sources = [
'audiomixer.c',
'mix-ops.c',
'mixer-dsp.c',
'plugin.c']
simd_cargs = []
simd_dependencies = []
audiomixer_c = static_library('audiomixer_c',
['mix-ops-c.c' ],
c_args : ['-O3'],
include_directories : [spa_inc],
install : false
)
simd_dependencies += audiomixer_c
if have_sse
audiomixer_sse = static_library('audiomixer_sse',
['mix-ops-sse.c' ],
c_args : [sse_args, '-O3', '-DHAVE_SSE'],
include_directories : [spa_inc],
install : false
)
simd_cargs += ['-DHAVE_SSE']
simd_dependencies += audiomixer_sse
endif
if have_sse2
audiomixer_sse2 = static_library('audiomixer_sse2',
['mix-ops-sse2.c' ],
c_args : [sse2_args, '-O3', '-DHAVE_SSE2'],
include_directories : [spa_inc],
install : false
)
simd_cargs += ['-DHAVE_SSE2']
simd_dependencies += audiomixer_sse2
endif
audiomixerlib = shared_library('spa-audiomixer',
audiomixer_sources,
c_args : simd_cargs,
link_with : simd_dependencies,
include_directories : [spa_inc],
dependencies : [ mathlib ],
install : true,
install_dir : '@0@/spa/audiomixer/'.format(get_option('libdir')))

View file

@ -0,0 +1,69 @@
/* Spa
*
* Copyright © 2019 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 <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/utils/defs.h>
#include "mix-ops.h"
void
mix_f32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[],
uint32_t n_src, uint32_t n_samples)
{
uint32_t i, n;
float *d = dst;
if (n_src == 0)
memset(dst, 0, n_samples * sizeof(float));
else if (dst != src[0])
memcpy(dst, src[0], n_samples * sizeof(float));
for (i = 1; i < n_src; i++) {
const float *s = src[i];
for (n = 0; n < n_samples; n++)
d[n] += s[n];
}
}
void
mix_f64_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[],
uint32_t n_src, uint32_t n_samples)
{
uint32_t i, n;
double *d = dst;
if (n_src == 0)
memset(dst, 0, n_samples * sizeof(double));
else if (dst != src[0])
memcpy(dst, src[0], n_samples * sizeof(double));
for (i = 1; i < n_src; i++) {
const double *s = src[i];
for (n = 0; n < n_samples; n++)
d[n] += s[n];
}
}

View file

@ -0,0 +1,89 @@
/* Spa
*
* Copyright © 2019 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 <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/utils/defs.h>
#include "mix-ops.h"
#include <xmmintrin.h>
static inline void mix_2(float * dst, const float * SPA_RESTRICT src, uint32_t n_samples)
{
uint32_t n, unrolled;
__m128 in1[4], in2[4];
if (SPA_IS_ALIGNED(src, 16) &&
SPA_IS_ALIGNED(dst, 16))
unrolled = n_samples & ~15;
else
unrolled = 0;
for (n = 0; n < unrolled; n += 16) {
in1[0] = _mm_load_ps(&dst[n+ 0]);
in1[1] = _mm_load_ps(&dst[n+ 4]);
in1[2] = _mm_load_ps(&dst[n+ 8]);
in1[3] = _mm_load_ps(&dst[n+12]);
in2[0] = _mm_load_ps(&src[n+ 0]);
in2[1] = _mm_load_ps(&src[n+ 4]);
in2[2] = _mm_load_ps(&src[n+ 8]);
in2[3] = _mm_load_ps(&src[n+12]);
in1[0] = _mm_add_ps(in1[0], in2[0]);
in1[1] = _mm_add_ps(in1[1], in2[1]);
in1[2] = _mm_add_ps(in1[2], in2[2]);
in1[3] = _mm_add_ps(in1[3], in2[3]);
_mm_store_ps(&dst[n+ 0], in1[0]);
_mm_store_ps(&dst[n+ 4], in1[1]);
_mm_store_ps(&dst[n+ 8], in1[2]);
_mm_store_ps(&dst[n+12], in1[3]);
}
for (; n < n_samples; n++) {
in1[0] = _mm_load_ss(&dst[n]),
in2[0] = _mm_load_ss(&src[n]),
in1[0] = _mm_add_ss(in1[0], in2[0]);
_mm_store_ss(&dst[n], in1[0]);
}
}
void
mix_f32_sse(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[],
uint32_t n_src, uint32_t n_samples)
{
uint32_t i;
if (n_src == 0)
memset(dst, 0, n_samples * sizeof(float));
else if (dst != src[0])
memcpy(dst, src[0], n_samples * sizeof(float));
for (i = 1; i < n_src; i++) {
mix_2(dst, src[i], n_samples);
}
}

View file

@ -0,0 +1,89 @@
/* Spa
*
* Copyright © 2019 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 <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/utils/defs.h>
#include "mix-ops.h"
#include <emmintrin.h>
static inline void mix_2(double * dst, const double * SPA_RESTRICT src, uint32_t n_samples)
{
uint32_t n, unrolled;
__m128d in1[4], in2[4];
if (SPA_IS_ALIGNED(src, 16) &&
SPA_IS_ALIGNED(dst, 16))
unrolled = n_samples & ~7;
else
unrolled = 0;
for (n = 0; n < unrolled; n += 8) {
in1[0] = _mm_load_pd(&dst[n+ 0]);
in1[1] = _mm_load_pd(&dst[n+ 2]);
in1[2] = _mm_load_pd(&dst[n+ 4]);
in1[3] = _mm_load_pd(&dst[n+ 6]);
in2[0] = _mm_load_pd(&src[n+ 0]);
in2[1] = _mm_load_pd(&src[n+ 2]);
in2[2] = _mm_load_pd(&src[n+ 4]);
in2[3] = _mm_load_pd(&src[n+ 6]);
in1[0] = _mm_add_pd(in1[0], in2[0]);
in1[1] = _mm_add_pd(in1[1], in2[1]);
in1[2] = _mm_add_pd(in1[2], in2[2]);
in1[3] = _mm_add_pd(in1[3], in2[3]);
_mm_store_pd(&dst[n+ 0], in1[0]);
_mm_store_pd(&dst[n+ 2], in1[1]);
_mm_store_pd(&dst[n+ 4], in1[2]);
_mm_store_pd(&dst[n+ 6], in1[3]);
}
for (; n < n_samples; n++) {
in1[0] = _mm_load_sd(&dst[n]),
in2[0] = _mm_load_sd(&src[n]),
in1[0] = _mm_add_sd(in1[0], in2[0]);
_mm_store_sd(&dst[n], in1[0]);
}
}
void
mix_f64_sse2(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[],
uint32_t n_src, uint32_t n_samples)
{
uint32_t i;
if (n_src == 0)
memset(dst, 0, n_samples * sizeof(double));
else if (dst != src[0])
memcpy(dst, src[0], n_samples * sizeof(double));
for (i = 1; i < n_src; i++) {
mix_2(dst, src[i], n_samples);
}
}

View file

@ -1,6 +1,6 @@
/* Spa
*
* Copyright © 2018 Wim Taymans
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -22,262 +22,86 @@
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/support/cpu.h>
#include <spa/utils/defs.h>
#include <spa/param/audio/format-utils.h>
#include "mix-ops.h"
static void
clear_s16(void *dst, int n_bytes)
{
memset(dst, 0, n_bytes);
}
typedef void (*mix_func_t) (struct mix_ops *ops, void * SPA_RESTRICT dst,
const void * SPA_RESTRICT src[], uint32_t n_src, uint32_t n_samples);
static void
clear_f32(void *dst, int n_bytes)
{
memset(dst, 0, n_bytes);
}
struct mix_info {
uint32_t fmt;
uint32_t n_channels;
uint32_t cpu_flags;
uint32_t stride;
mix_func_t process;
};
static void
copy_s16(void *dst, const void *src, int n_bytes)
static struct mix_info mix_table[] =
{
memcpy(dst, src, n_bytes);
}
/* f32 */
#if defined (HAVE_SSE)
{ SPA_AUDIO_FORMAT_F32, 1, SPA_CPU_FLAG_SSE, 4, mix_f32_sse },
{ SPA_AUDIO_FORMAT_F32P, 1, SPA_CPU_FLAG_SSE, 4, mix_f32_sse },
#endif
{ SPA_AUDIO_FORMAT_F32, 1, 0, 4, mix_f32_c },
{ SPA_AUDIO_FORMAT_F32P, 1, 0, 4, mix_f32_c },
static void
copy_f32(void *dst, const void *src, int n_bytes)
#if defined (HAVE_SSE2)
{ SPA_AUDIO_FORMAT_F64, 1, SPA_CPU_FLAG_SSE2, 8, mix_f64_sse2 },
{ SPA_AUDIO_FORMAT_F64P, 1, SPA_CPU_FLAG_SSE2, 8, mix_f64_sse2 },
#endif
{ SPA_AUDIO_FORMAT_F64, 1, 0, 8, mix_f64_c },
{ SPA_AUDIO_FORMAT_F64P, 1, 0, 8, mix_f64_c },
};
#define MATCH_CHAN(a,b) ((a) == 0 || (a) == (b))
#define MATCH_CPU_FLAGS(a,b) ((a) == 0 || ((a) & (b)) == a)
static const struct mix_info *find_mix_info(uint32_t fmt,
uint32_t n_channels, uint32_t cpu_flags)
{
memcpy(dst, src, n_bytes);
}
size_t i;
static void
add_s16(void *dst, const void *src, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
int32_t t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = *d + *s;
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d++;
s++;
for (i = 0; i < SPA_N_ELEMENTS(mix_table); i++) {
if (mix_table[i].fmt == fmt &&
MATCH_CHAN(mix_table[i].n_channels, n_channels) &&
MATCH_CPU_FLAGS(mix_table[i].cpu_flags, cpu_flags))
return &mix_table[i];
}
return NULL;
}
static void
add_f32(void *dst, const void *src, int n_bytes)
static void impl_mix_ops_clear(struct mix_ops *ops, void * SPA_RESTRICT dst, uint32_t n_samples)
{
const float *s = src;
float *d = dst;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d += *s;
d++;
s++;
}
const struct mix_info *info = ops->priv;
memset(dst, 0, n_samples * info->stride);
}
static void
copy_scale_s16(void *dst, const void *src, const double scale, int n_bytes)
static void impl_mix_ops_free(struct mix_ops *ops)
{
const int16_t *s = src;
int16_t *d = dst;;
int32_t v = scale * (1 << 11), t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = (*s * v) >> 11;
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d++;
s++;
}
spa_zero(*ops);
}
static void
copy_scale_f32(void *dst, const void *src, const double scale, int n_bytes)
int mix_ops_init(struct mix_ops *ops)
{
const float *s = src;
float *d = dst;
float v = scale;
const struct mix_info *info;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d = *s * v;
d++;
s++;
}
}
static void
add_scale_s16(void *dst, const void *src, const double scale, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
int32_t v = scale * (1 << 11), t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = *d + ((*s * v) >> 11);
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d++;
s++;
}
}
static void
add_scale_f32(void *dst, const void *src, const double scale, int n_bytes)
{
const float *s = src;
float *d = dst;
float v = scale;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d += *s * v;
d++;
s++;
}
}
static void
copy_s16_i(void *dst, int dst_stride, const void *src, int src_stride, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
*d = *s;
d += dst_stride;
s += src_stride;
}
}
static void
copy_f32_i(void *dst, int dst_stride, const void *src, int src_stride, int n_bytes)
{
const float *s = src;
float *d = dst;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d = *s;
d += dst_stride;
s += src_stride;
}
}
static void
add_s16_i(void *dst, int dst_stride, const void *src, int src_stride, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
int32_t t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = *d + *s;
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d += dst_stride;
s += src_stride;
}
}
static void
add_f32_i(void *dst, int dst_stride, const void *src, int src_stride, int n_bytes)
{
const float *s = src;
float *d = dst;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d += *s;
d += dst_stride;
s += src_stride;
}
}
static void
copy_scale_s16_i(void *dst, int dst_stride, const void *src, int src_stride, const double scale, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
int32_t v = scale * (1 << 11), t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = (*s * v) >> 11;
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d += dst_stride;
s += src_stride;
}
}
static void
copy_scale_f32_i(void *dst, int dst_stride, const void *src, int src_stride, const double scale, int n_bytes)
{
const float *s = src;
float *d = dst;
float v = scale;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d = *s * v;
d += dst_stride;
s += src_stride;
}
}
static void
add_scale_s16_i(void *dst, int dst_stride, const void *src, int src_stride, const double scale, int n_bytes)
{
const int16_t *s = src;
int16_t *d = dst;
int32_t v = scale * (1 << 11), t;
n_bytes /= sizeof(int16_t);
while (n_bytes--) {
t = *d + ((*s * v) >> 11);
*d = SPA_CLAMP(t, INT16_MIN, INT16_MAX);
d += dst_stride;
s += src_stride;
}
}
static void
add_scale_f32_i(void *dst, int dst_stride, const void *src, int src_stride, const double scale, int n_bytes)
{
const float *s = src;
float *d = dst;
float v = scale;
n_bytes /= sizeof(float);
while (n_bytes--) {
*d += *s * v;
d += dst_stride;
s += src_stride;
}
}
void spa_audiomixer_get_ops(struct spa_audiomixer_ops *ops)
{
ops->clear[FMT_S16] = clear_s16;
ops->clear[FMT_F32] = clear_f32;
ops->copy[FMT_S16] = copy_s16;
ops->copy[FMT_F32] = copy_f32;
ops->add[FMT_S16] = add_s16;
ops->add[FMT_F32] = add_f32;
ops->copy_scale[FMT_S16] = copy_scale_s16;
ops->copy_scale[FMT_F32] = copy_scale_f32;
ops->add_scale[FMT_S16] = add_scale_s16;
ops->add_scale[FMT_F32] = add_scale_f32;
ops->copy_i[FMT_S16] = copy_s16_i;
ops->copy_i[FMT_F32] = copy_f32_i;
ops->add_i[FMT_S16] = add_s16_i;
ops->add_i[FMT_F32] = add_f32_i;
ops->copy_scale_i[FMT_S16] = copy_scale_s16_i;
ops->copy_scale_i[FMT_F32] = copy_scale_f32_i;
ops->add_scale_i[FMT_S16] = add_scale_s16_i;
ops->add_scale_i[FMT_F32] = add_scale_f32_i;
info = find_mix_info(ops->fmt, ops->n_channels, ops->cpu_flags);
if (info == NULL)
return -ENOTSUP;
ops->priv = info;
ops->cpu_flags = info->cpu_flags;
ops->clear = impl_mix_ops_clear;
ops->process = info->process;
ops->free = impl_mix_ops_free;
return 0;
}

View file

@ -1,6 +1,6 @@
/* Spa
*
* Copyright © 2018 Wim Taymans
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@ -22,35 +22,40 @@
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <spa/utils/defs.h>
typedef void (*mix_clear_func_t) (void *dst, int n_bytes);
typedef void (*mix_func_t) (void *dst, const void *src, int n_bytes);
typedef void (*mix_scale_func_t) (void *dst, const void *src, const double scale, int n_bytes);
typedef void (*mix_i_func_t) (void *dst, int dst_stride,
const void *src, int src_stride, int n_bytes);
typedef void (*mix_scale_i_func_t) (void *dst, int dst_stride,
const void *src, int src_stride, const double scale, int n_bytes);
struct mix_ops {
uint32_t fmt;
uint32_t n_channels;
uint32_t cpu_flags;
enum {
FMT_S16,
FMT_F32,
FMT_MAX,
void (*clear) (struct mix_ops *ops, void * SPA_RESTRICT dst, uint32_t n_samples);
void (*process) (struct mix_ops *ops,
void * SPA_RESTRICT dst,
const void * SPA_RESTRICT src[], uint32_t n_src,
uint32_t n_samples);
void (*free) (struct mix_ops *ops);
const void *priv;
};
struct spa_audiomixer_ops {
mix_clear_func_t clear[FMT_MAX];
mix_func_t copy[FMT_MAX];
mix_func_t add[FMT_MAX];
mix_scale_func_t copy_scale[FMT_MAX];
mix_scale_func_t add_scale[FMT_MAX];
mix_i_func_t copy_i[FMT_MAX];
mix_i_func_t add_i[FMT_MAX];
mix_scale_i_func_t copy_scale_i[FMT_MAX];
mix_scale_i_func_t add_scale_i[FMT_MAX];
};
int mix_ops_init(struct mix_ops *ops);
void spa_audiomixer_get_ops(struct spa_audiomixer_ops *ops);
#define mix_ops_clear(ops,...) (ops)->clear(ops, __VA_ARGS__)
#define mix_ops_process(ops,...) (ops)->process(ops, __VA_ARGS__)
#define mix_ops_free(ops) (ops)->free(ops)
#define DEFINE_FUNCTION(name,arch) \
void mix_##name##_##arch(struct mix_ops *ops, void * SPA_RESTRICT dst, \
const void * SPA_RESTRICT src[], uint32_t n_src, \
uint32_t n_samples) \
DEFINE_FUNCTION(f32, c);
DEFINE_FUNCTION(f64, c);
#if defined(HAVE_SSE)
DEFINE_FUNCTION(f32, sse);
#endif
#if defined(HAVE_SSE2)
DEFINE_FUNCTION(f64, sse2);
#endif

View file

@ -27,6 +27,7 @@
#include <stdio.h>
#include <spa/support/log.h>
#include <spa/support/cpu.h>
#include <spa/utils/list.h>
#include <spa/utils/names.h>
#include <spa/node/node.h>
@ -36,7 +37,9 @@
#include <spa/param/param.h>
#include <spa/pod/filter.h>
#define NAME "floatmix"
#include "mix-ops.h"
#define NAME "mixer-dsp"
#define MAX_BUFFERS 64
#define MAX_PORTS 128
@ -96,6 +99,10 @@ struct impl {
struct spa_node node;
struct spa_log *log;
struct spa_cpu *cpu;
uint32_t cpu_flags;
struct mix_ops ops;
uint64_t info_all;
struct spa_node_info info;
@ -500,6 +507,13 @@ static int port_set_format(void *object,
if (info.info.raw.rate != this->format.info.raw.rate)
return -EINVAL;
} else {
this->ops.fmt = info.info.raw.format;
this->ops.n_channels = info.info.raw.channels;
this->ops.cpu_flags = this->cpu_flags;
if ((res = mix_ops_init(&this->ops)) < 0)
return res;
this->stride = sizeof(float);
this->have_format = true;
this->format = info;
@ -635,60 +649,6 @@ static int impl_node_port_reuse_buffer(void *object, uint32_t port_id, uint32_t
return queue_buffer(this, port, &port->buffers[buffer_id]);
}
#if defined (__SSE__)
#include <xmmintrin.h>
static void mix_2(float * dst, const float * SPA_RESTRICT src1,
const float * SPA_RESTRICT src2, uint32_t n_samples)
{
uint32_t n, unrolled;
__m128 in1[4], in2[4];
if (SPA_IS_ALIGNED(src1, 16) &&
SPA_IS_ALIGNED(src2, 16) &&
SPA_IS_ALIGNED(dst, 16))
unrolled = n_samples & ~15;
else
unrolled = 0;
for (n = 0; n < unrolled; n += 16) {
in1[0] = _mm_load_ps(&src1[n+ 0]);
in1[1] = _mm_load_ps(&src1[n+ 4]);
in1[2] = _mm_load_ps(&src1[n+ 8]);
in1[3] = _mm_load_ps(&src1[n+12]);
in2[0] = _mm_load_ps(&src2[n+ 0]);
in2[1] = _mm_load_ps(&src2[n+ 4]);
in2[2] = _mm_load_ps(&src2[n+ 8]);
in2[3] = _mm_load_ps(&src2[n+12]);
in1[0] = _mm_add_ps(in1[0], in2[0]);
in1[1] = _mm_add_ps(in1[1], in2[1]);
in1[2] = _mm_add_ps(in1[2], in2[2]);
in1[3] = _mm_add_ps(in1[3], in2[3]);
_mm_store_ps(&dst[n+ 0], in1[0]);
_mm_store_ps(&dst[n+ 4], in1[1]);
_mm_store_ps(&dst[n+ 8], in1[2]);
_mm_store_ps(&dst[n+12], in1[3]);
}
for (; n < n_samples; n++) {
in1[0] = _mm_load_ss(&src1[n]),
in2[0] = _mm_load_ss(&src2[n]),
in1[0] = _mm_add_ss(in1[0], in2[0]);
_mm_store_ss(&dst[n], in1[0]);
}
}
#else
static void mix_2(float * dst, const float * SPA_RESTRICT src1,
const float * SPA_RESTRICT src2, uint32_t n_samples)
{
uint32_t i;
for (i = 0; i < n_samples; i++)
dst[i] = src1[i] + src2[i];
}
#endif
static int impl_node_process(void *object)
{
struct impl *this = object;
@ -697,6 +657,7 @@ static int impl_node_process(void *object)
uint32_t n_samples, n_buffers, i, maxsize;
struct buffer **buffers;
struct buffer *outb;
const void **datas;
spa_return_val_if_fail(this != NULL, -EINVAL);
@ -717,6 +678,7 @@ static int impl_node_process(void *object)
}
buffers = alloca(MAX_PORTS * sizeof(struct buffer *));
datas = alloca(MAX_PORTS * sizeof(void *));
n_buffers = 0;
maxsize = MAX_SAMPLES * sizeof(float);
@ -746,6 +708,7 @@ static int impl_node_process(void *object)
maxsize = SPA_MIN(inb->buffer->datas[0].chunk->size, maxsize);
datas[n_buffers] = inb->buffer->datas[0].data;
buffers[n_buffers++] = inb;
inio->status = SPA_STATUS_NEED_DATA;
}
@ -762,8 +725,6 @@ static int impl_node_process(void *object)
*outb->buffer = *buffers[0]->buffer;
}
else {
float *dst;
outb->buffer->n_datas = 1;
outb->buffer->datas = outb->datas;
outb->datas[0].data = SPA_PTR_ALIGN(this->empty, 16, void);
@ -772,18 +733,7 @@ static int impl_node_process(void *object)
outb->datas[0].chunk->size = n_samples * sizeof(float);
outb->datas[0].chunk->stride = sizeof(float);
dst = outb->datas[0].data;
if (n_buffers == 0) {
memset(dst, 0, n_samples * sizeof(float));
}
else {
/* first 2 buffers, add and store */
mix_2(dst, buffers[0]->buffer->datas[0].data,
buffers[1]->buffer->datas[0].data, n_samples);
/* next buffers */
for (i = 2; i < n_buffers; i++)
mix_2(dst, dst, buffers[i]->buffer->datas[0].data, n_samples);
}
mix_ops_process(&this->ops, outb->datas[0].data, datas, n_buffers, n_samples);
}
outio->buffer_id = outb->id;
@ -859,9 +809,17 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
switch (support[i].type) {
case SPA_TYPE_INTERFACE_Log:
this->log = support[i].data;
break;
case SPA_TYPE_INTERFACE_CPU:
this->cpu = support[i].data;
break;
}
}
if (this->cpu)
this->cpu_flags = spa_cpu_get_flags(this->cpu);
spa_hook_list_init(&this->hooks);
@ -920,9 +878,9 @@ impl_enum_interface_info(const struct spa_handle_factory *factory,
return 1;
}
const struct spa_handle_factory spa_floatmix_factory = {
const struct spa_handle_factory spa_mixer_dsp_factory = {
SPA_VERSION_HANDLE_FACTORY,
SPA_NAME_AUDIO_MIXER,
SPA_NAME_AUDIO_MIXER_DSP,
NULL,
impl_get_size,
impl_init,

View file

@ -27,6 +27,7 @@
#include <spa/support/plugin.h>
extern const struct spa_handle_factory spa_audiomixer_factory;
extern const struct spa_handle_factory spa_mixer_dsp_factory;
SPA_EXPORT
int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t *index)
@ -38,6 +39,9 @@ int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t
case 0:
*factory = &spa_audiomixer_factory;
break;
case 1:
*factory = &spa_mixer_dsp_factory;
break;
default:
return 0;
}

View file

@ -49,7 +49,6 @@ pipewire_sources = [
'main-loop.c',
'mem.c',
'module.c',
'mix/floatmix.c',
'node.c',
'factory.c',
'pipewire.c',

View file

@ -126,6 +126,7 @@ open_plugin(struct registry *registry,
goto error_dlclose;
}
pw_log_debug("loaded plugin:'%s'", filename);
plugin->ref = 1;
plugin->filename = filename;
plugin->hnd = hnd;
@ -150,6 +151,7 @@ unref_plugin(struct plugin *plugin)
{
if (--plugin->ref == 0) {
spa_list_remove(&plugin->link);
pw_log_debug("unloaded plugin:'%s'", plugin->filename);
dlclose(plugin->hnd);
free(plugin->filename);
free(plugin);
@ -235,7 +237,7 @@ struct spa_handle *pw_load_spa_handle(const char *lib,
if (lib == NULL)
lib = sup->support_lib;
pw_log_debug("load \"%s\", \"%s\"", lib, factory_name);
pw_log_debug("load lib:'%s' factory-name:'%s'", lib, factory_name);
if ((plugin = open_plugin(sup->registry, sup->plugin_dir, lib)) == NULL) {
res = -errno;

View file

@ -29,6 +29,7 @@
#include <spa/pod/parser.h>
#include <spa/param/audio/format-utils.h>
#include <spa/node/utils.h>
#include <spa/utils/names.h>
#include <spa/debug/types.h>
#include "pipewire/pipewire.h"
@ -39,8 +40,6 @@
#define NAME "port"
extern const struct spa_handle_factory spa_floatmix_factory;
/** \cond */
struct impl {
struct pw_port this;
@ -467,8 +466,7 @@ int pw_port_set_mix(struct pw_port *port, struct spa_node *node, uint32_t flags)
SPA_IO_Buffers, NULL, 0);
}
if (port->mix_handle != NULL) {
spa_handle_clear(port->mix_handle);
free(port->mix_handle);
pw_unload_spa_handle(port->mix_handle);
port->mix_handle = NULL;
}
@ -491,10 +489,9 @@ static int setup_mixer(struct pw_port *port, const struct spa_pod *param)
{
uint32_t media_type, media_subtype;
int res;
const struct spa_handle_factory *factory = NULL;
const char *fallback_lib, *factory_name;
struct spa_handle *handle;
const struct spa_support *support;
uint32_t n_support;
struct spa_dict_item items[1];
void *iface;
if ((res = spa_format_parse(param, &media_type, &media_subtype)) < 0)
@ -516,7 +513,8 @@ static int setup_mixer(struct pw_port *port, const struct spa_pod *param)
if (info.format != SPA_AUDIO_FORMAT_F32P || info.channels != 1)
return -ENOTSUP;
factory = &spa_floatmix_factory;
fallback_lib = "audiomixer/libspa-audiomixer";
factory_name = SPA_NAME_AUDIO_MIXER_DSP;
break;
}
default:
@ -527,15 +525,17 @@ static int setup_mixer(struct pw_port *port, const struct spa_pod *param)
return -ENOTSUP;
}
if (factory == NULL)
return -EIO;
items[0] = SPA_DICT_ITEM_INIT(SPA_KEY_LIBRARY_NAME, fallback_lib);
handle = pw_core_load_spa_handle(port->node->core, factory_name,
&SPA_DICT_INIT_ARRAY(items));
if (handle == NULL)
return -errno;
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
support = pw_core_get_support(port->node->core, &n_support);
spa_handle_factory_init(factory, handle, NULL, support, n_support);
spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface);
if ((res = spa_handle_get_interface(handle,
SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
pw_unload_spa_handle(handle);
return res;
}
pw_log_debug("mix node %p", iface);
pw_port_set_mix(port, (struct spa_node*)iface,