History

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.

Andreas Rheinhardt committed 6 years ago

Commit f5e1d381 parent a513b301 file · +15 −23Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_headphone.c+15−23
@@ -375,8 +375,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 374 unmodified lines
375375 FFTComplex *data_hrtf_r = NULL;
376376 FFTComplex *fft_in_l = NULL;
377377 FFTComplex *fft_in_r = NULL;
378− float *data_ir_l = NULL;
379− float *data_ir_r = NULL;
380378 int offset = 0, ret = 0;
381379 int n_fft;
382380 int i, j, k;
@@ -408,9 +406,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 25 unmodified lines
408406 }
409407 }
410408
411− s->data_ir[0] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
412− s->data_ir[1] = av_calloc(s->air_len, sizeof(float) * s->nb_irs);
413−
414409 if (s->type == TIME_DOMAIN) {
415410 s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels);
416411 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) · 11 unmodified lines
428423 }
429424 }
430425
431− if (!s->data_ir[0] || !s->data_ir[1] ||
432− !s->ringbuffer[0] || !s->ringbuffer[1]) {
426+ if (!s->ringbuffer[0] || !s->ringbuffer[1]) {
433427 ret = AVERROR(ENOMEM);
434428 goto fail;
435429 }
@@ -438,9 +432,9 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 2 unmodified lines
438432 s->temp_src[0] = av_calloc(s->air_len, sizeof(float));
439433 s->temp_src[1] = av_calloc(s->air_len, sizeof(float));
440434
441− data_ir_l = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_l));
442− data_ir_r = av_calloc(nb_irs * s->air_len, sizeof(*data_ir_r));
443− if (!data_ir_r || !data_ir_l || !s->temp_src[0] || !s->temp_src[1]) {
435+ s->data_ir[0] = av_calloc(nb_irs * s->air_len, sizeof(*s->data_ir[0]));
436+ s->data_ir[1] = av_calloc(nb_irs * s->air_len, sizeof(*s->data_ir[1]));
437+ if (!s->data_ir[0] || !s->data_ir[1] || !s->temp_src[0] || !s->temp_src[1]) {
444438 ret = AVERROR(ENOMEM);
445439 goto fail;
446440 }
@@ -475,10 +469,12 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 28 unmodified lines
475469 if (idx == -1)
476470 continue;
477471 if (s->type == TIME_DOMAIN) {
478− offset = idx * s->air_len;
472+ float *data_ir_l = s->data_ir[0] + idx * s->air_len;
473+ float *data_ir_r = s->data_ir[1] + idx * s->air_len;
474+
479475 for (j = 0; j < len; j++) {
480− data_ir_l[offset + j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
481− data_ir_r[offset + j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
476+ data_ir_l[j] = ptr[len * 2 - j * 2 - 2] * gain_lin;
477+ data_ir_r[j] = ptr[len * 2 - j * 2 - 1] * gain_lin;
482478 }
483479 } else {
484480 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
@@ -514,10 +510,12 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 29 unmodified lines
514510
515511 I = idx * 2;
516512 if (s->type == TIME_DOMAIN) {
517− offset = idx * s->air_len;
513+ float *data_ir_l = s->data_ir[0] + idx * s->air_len;
514+ float *data_ir_r = s->data_ir[1] + idx * s->air_len;
515+
518516 for (j = 0; j < len; j++) {
519− data_ir_l[offset + j] = ptr[len * N - j * N - N + I ] * gain_lin;
520− data_ir_r[offset + j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
517+ data_ir_l[j] = ptr[len * N - j * N - N + I ] * gain_lin;
518+ data_ir_r[j] = ptr[len * N - j * N - N + I + 1] * gain_lin;
521519 }
522520 } else {
523521 memset(fft_in_l, 0, n_fft * sizeof(*fft_in_l));
@@ -542,10 +540,7 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 18 unmodified lines
542540 av_frame_free(&s->in[i + 1].frame);
543541 }
544542
545− if (s->type == TIME_DOMAIN) {
546− memcpy(s->data_ir[0], data_ir_l, sizeof(float) * nb_irs * s->air_len);
547− memcpy(s->data_ir[1], data_ir_r, sizeof(float) * nb_irs * s->air_len);
548− } else {
543+ if (s->type == FREQUENCY_DOMAIN) {
549544 s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
550545 s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex));
551546 if (!s->data_hrtf[0] || !s->data_hrtf[1]) {
@@ -566,9 +561,6 @@ fail: · 14 unmodified lines
566561 for (i = 0; i < s->nb_inputs - 1; i++)
567562 av_frame_free(&s->in[i + 1].frame);
568563
569− av_freep(&data_ir_l);
570− av_freep(&data_ir_r);
571−
572564 av_freep(&data_hrtf_l);
573565 av_freep(&data_hrtf_r);
574566

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t