mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2026-05-16 21:37:49 -04:00
audioconvert: allocate memory dynamically
Instead of working with fixed buffer size, allocate everything dynamically. This preserves a lot of memory in the normal case.
This commit is contained in:
parent
cd654efa6a
commit
f1b1f2d97b
5 changed files with 72 additions and 28 deletions
|
|
@ -15,6 +15,8 @@
|
|||
#include "channelmix-ops.h"
|
||||
#include "hilbert.h"
|
||||
|
||||
#define MAX_BUFFER_SIZE 4096
|
||||
|
||||
#define ANY ((uint32_t)-1)
|
||||
#define EQ ((uint32_t)-2)
|
||||
|
||||
|
|
@ -192,7 +194,7 @@ static bool match_mix(struct channelmix *mix,
|
|||
return matched;
|
||||
}
|
||||
|
||||
static int make_matrix(struct channelmix *mix)
|
||||
static void impl_channelmix_reconfigure(struct channelmix *mix)
|
||||
{
|
||||
float matrix[MAX_CHANNELS][MAX_CHANNELS] = {{ 0.0f }};
|
||||
uint64_t src_mask = mix->src_mask, src_paired;
|
||||
|
|
@ -782,7 +784,6 @@ done:
|
|||
for (j = 0; j < src_chan; j++)
|
||||
mix->matrix_orig[i][j] /= maxsum;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void impl_channelmix_set_volume(struct channelmix *mix, float volume, bool mute,
|
||||
|
|
@ -875,6 +876,8 @@ static void impl_channelmix_set_volume(struct channelmix *mix, float volume, boo
|
|||
static void impl_channelmix_free(struct channelmix *mix)
|
||||
{
|
||||
mix->process = NULL;
|
||||
free(mix->data);
|
||||
mix->data = NULL;
|
||||
}
|
||||
|
||||
void channelmix_reset(struct channelmix *mix)
|
||||
|
|
@ -895,6 +898,9 @@ void channelmix_reset(struct channelmix *mix)
|
|||
int channelmix_init(struct channelmix *mix)
|
||||
{
|
||||
const struct channelmix_info *info;
|
||||
void *d, *b[2];
|
||||
size_t taps_size, buffer_size, alloc_size, dstptr_size, matrow_size, matrix_size, lr4_size;
|
||||
uint32_t i;
|
||||
|
||||
if (mix->src_chan > MAX_CHANNELS ||
|
||||
mix->dst_chan > MAX_CHANNELS)
|
||||
|
|
@ -908,29 +914,62 @@ int channelmix_init(struct channelmix *mix)
|
|||
mix->free = impl_channelmix_free;
|
||||
mix->process = info->process;
|
||||
mix->set_volume = impl_channelmix_set_volume;
|
||||
mix->reconfigure = impl_channelmix_reconfigure;
|
||||
mix->delay = (uint32_t)(mix->rear_delay * mix->freq / 1000.0f);
|
||||
mix->func_cpu_flags = info->cpu_flags;
|
||||
mix->func_name = info->name;
|
||||
|
||||
spa_zero(mix->taps_mem);
|
||||
mix->taps = SPA_PTR_ALIGN(mix->taps_mem, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->buffer[0] = SPA_PTR_ALIGN(&mix->buffer_mem[0], CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->buffer[1] = SPA_PTR_ALIGN(&mix->buffer_mem[2*BUFFER_SIZE], CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
if (mix->hilbert_taps > 0)
|
||||
mix->n_taps = SPA_CLAMP(mix->hilbert_taps, 15u, MAX_TAPS) | 1;
|
||||
else
|
||||
mix->n_taps = 1;
|
||||
|
||||
mix->buffer_size = mix->delay + mix->n_taps;
|
||||
if (mix->buffer_size > MAX_BUFFER_SIZE) {
|
||||
mix->buffer_size = MAX_BUFFER_SIZE;
|
||||
mix->delay = MAX_BUFFER_SIZE - mix->n_taps;
|
||||
}
|
||||
|
||||
taps_size = SPA_ROUND_UP(mix->n_taps * sizeof(float), CHANNELMIX_OPS_MAX_ALIGN);
|
||||
buffer_size = SPA_ROUND_UP(mix->buffer_size*2 * sizeof(float), CHANNELMIX_OPS_MAX_ALIGN);
|
||||
dstptr_size = SPA_ROUND_UP(mix->dst_chan * sizeof(void*), CHANNELMIX_OPS_MAX_ALIGN);
|
||||
matrow_size = SPA_ROUND_UP(mix->src_chan * sizeof(float), CHANNELMIX_OPS_MAX_ALIGN);
|
||||
matrix_size = SPA_ROUND_UP(mix->dst_chan * matrow_size, CHANNELMIX_OPS_MAX_ALIGN);
|
||||
lr4_size = SPA_ROUND_UP(mix->dst_chan * sizeof(struct lr4), CHANNELMIX_OPS_MAX_ALIGN);
|
||||
|
||||
alloc_size = taps_size + buffer_size*2 + dstptr_size*2 + lr4_size + matrix_size*2 + CHANNELMIX_OPS_MAX_ALIGN;
|
||||
d = calloc(1, alloc_size);
|
||||
if (d == NULL)
|
||||
return -errno;
|
||||
|
||||
mix->data = d;
|
||||
mix->taps = SPA_PTR_ALIGN(d, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->buffer[0] = SPA_PTROFF_ALIGN(mix->taps, taps_size, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->buffer[1] = SPA_PTROFF_ALIGN(mix->buffer[0], buffer_size, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->matrix = SPA_PTROFF_ALIGN(mix->buffer[1], buffer_size, CHANNELMIX_OPS_MAX_ALIGN, float*);
|
||||
mix->matrix_orig = SPA_PTROFF_ALIGN(mix->matrix, dstptr_size, CHANNELMIX_OPS_MAX_ALIGN, float*);
|
||||
mix->lr4 = SPA_PTROFF_ALIGN(mix->matrix_orig, dstptr_size, CHANNELMIX_OPS_MAX_ALIGN, struct lr4);
|
||||
|
||||
b[0] = SPA_PTROFF_ALIGN(mix->lr4, lr4_size, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
b[1] = SPA_PTROFF_ALIGN(b[0], matrix_size, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
|
||||
for (i = 0; i < mix->dst_chan; i++) {
|
||||
mix->matrix[i] = SPA_PTROFF_ALIGN(b[0], matrow_size * i, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
mix->matrix_orig[i] = SPA_PTROFF_ALIGN(b[1], matrow_size * i, CHANNELMIX_OPS_MAX_ALIGN, float);
|
||||
}
|
||||
|
||||
if (mix->hilbert_taps > 0) {
|
||||
mix->n_taps = SPA_CLAMP(mix->hilbert_taps, 15u, MAX_TAPS) | 1;
|
||||
blackman_window(mix->taps, mix->n_taps);
|
||||
hilbert_generate(mix->taps, mix->n_taps);
|
||||
reverse_taps(mix->taps, mix->n_taps);
|
||||
} else {
|
||||
mix->n_taps = 1;
|
||||
mix->taps[0] = 1.0f;
|
||||
}
|
||||
if (mix->delay + mix->n_taps > BUFFER_SIZE)
|
||||
mix->delay = BUFFER_SIZE - mix->n_taps;
|
||||
|
||||
spa_log_debug(mix->log, "selected %s delay:%d options:%08x", info->name, mix->delay,
|
||||
mix->options);
|
||||
|
||||
return make_matrix(mix);
|
||||
channelmix_reconfigure(mix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue