summaryrefslogtreecommitdiff
path: root/libavfilter
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2020-08-25 15:31:04 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2020-09-09 13:47:14 +0200
commitf5e1d38b87fb82c199ea293863789c8bfb7fe1bf (patch)
tree9b48bbbe0a74d8cf0f9b4bb41e24309bd5bbb96e /libavfilter
parenta513b306b3bb058f0501a72dd5169dfc39bf73ef (diff)
avfilter/af_headphone: Avoid intermediate buffers I
The headphone filter has two modes; in one of them (say A), it needs certain buffers to store data. But it allocated them in both modes. Furthermore when in mode A it also allocated intermediate buffers of the same size, initialized them, copied their contents into the permanent buffers and freed them. This commit changes this: The permanent buffer is only allocated when needed; the temporary buffer has been completely avoided. Reviewed-by: Paul B Mahol <onemda@gmail.com> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavfilter')
-rw-r--r--libavfilter/af_headphone.c38
1 files changed, 15 insertions, 23 deletions
diff --git a/libavfilter/af_headphone.c b/libavfilter/af_headphone.c
index f862181cbb..359174d4b4 100644
--- a/libavfilter/af_headphone.c
+++ b/libavfilter/af_headphone.c
@@ -375,8 +375,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
FFTComplex *data_hrtf_r = NULL;
FFTComplex *fft_in_l = NULL;
FFTComplex *fft_in_r = NULL;
- float *data_ir_l = NULL;
- float *data_ir_r = NULL;
int offset = 0, ret = 0;
int n_fft;
int i, j, k;
@@ -408,9 +406,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
}
}
- s->data_ir[0] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
- s->data_ir[1] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
-
if (s->type == TIME_DOMAIN) {
s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
s->ringbuffer[1] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
@@ -428,8 +423,7 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
}
}
- if (!s->data_ir[0] || !s->data_ir[1] ||
- !s->ringbuffer[0] || !s->ringbuffer[1]) {
+ if (!s->ringbuffer[0] || !s->ringbuffer[1]) {
ret = AVERROR(ENOMEM);
goto fail;
}
@@ -438,9 +432,9 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
s->temp_src[0] = av_calloc(s->air_len, sizeof(float));
s->temp_src[1] = av_calloc(s->air_len, sizeof(float));
- data_ir_l = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_l));
- data_ir_r = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_r));
- if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
+ s->data_ir[0] = av_calloc(nb_irs * s->air_len, sizeof(*s->data_ir[0]));
+ s->data_ir[1] = av_calloc(nb_irs * s->air_len, sizeof(*s->data_ir[1]));
+ if (!s->data_ir[0] || !s->data_ir[1] || !s->temp_src[0] || !s->temp_src[1]) {
ret = AVERROR(ENOMEM);
goto fail;
}
@@ -475,10 +469,12 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
if (idx == -1)
continue;
if (s->type == TIME_DOMAIN) {
- offset = idx * s->air_len;
+ float *data_ir_l = s->data_ir[0] + idx * s->air_len;
+ float *data_ir_r = s->data_ir[1] + idx * s->air_len;
+
for (j = 0; j < len; j++) {
- data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
- data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
+ data_ir_l[j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
+ data_ir_r[j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
}
} else {
memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
@@ -514,10 +510,12 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
I = idx * 2;
if (s->type == TIME_DOMAIN) {
- offset = idx * s->air_len;
+ float *data_ir_l = s->data_ir[0] + idx * s->air_len;
+ float *data_ir_r = s->data_ir[1] + idx * s->air_len;
+
for (j = 0; j < len; j++) {
- data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
- data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
+ data_ir_l[j] = ptr[len * N - j * N - N + I ] * gain_lin;
+ data_ir_r[j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
}
} else {
memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
@@ -542,10 +540,7 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink)
av_frame_free(&s->in[i + 1].frame);
}
- if (s->type == TIME_DOMAIN) {
- memcpy(s->data_ir[0], data_ir_l, sizeof(float) * nb_irs * s->air_len);
- memcpy(s->data_ir[1], data_ir_r, sizeof(float) * nb_irs * s->air_len);
- } else {
+ if (s->type == FREQUENCY_DOMAIN) {
s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
@@ -566,9 +561,6 @@ fail:
for (i = 0; i < s->nb_inputs - 1; i++)
av_frame_free(&s->in[i + 1].frame);
- av_freep(&data_ir_l);
- av_freep(&data_ir_r);
-
av_freep(&data_hrtf_l);
av_freep(&data_hrtf_r);