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.
Unified · Split
| @@ -375,8 +375,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 374 unmodified lines | ||
| 375 | 375 | FFTComplex *data_hrtf_r = NULL; |
| 376 | 376 | FFTComplex *fft_in_l = NULL; |
| 377 | 377 | FFTComplex *fft_in_r = NULL; |
| 378 | − float *data_ir_l = NULL; | |
| 379 | − float *data_ir_r = NULL; | |
| 380 | 378 | int offset = 0, ret = 0; |
| 381 | 379 | int n_fft; |
| 382 | 380 | int i, j, k; |
| @@ -408,9 +406,6 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 25 unmodified lines | ||
| 408 | 406 | } |
| 409 | 407 | } |
| 410 | 408 | |
| 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 | − | |
| 414 | 409 | if (s->type == TIME_DOMAIN) { |
| 415 | 410 | s->ringbuffer[0] = av_calloc(s->buffer_length, sizeof(float) * nb_input_channels); |
| 416 | 411 | 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 | ||
| 428 | 423 | } |
| 429 | 424 | } |
| 430 | 425 | |
| 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]) { | |
| 433 | 427 | ret = AVERROR(ENOMEM); |
| 434 | 428 | goto fail; |
| 435 | 429 | } |
| @@ -438,9 +432,9 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 2 unmodified lines | ||
| 438 | 432 | s->temp_src[0] = av_calloc(s->air_len, sizeof(float)); |
| 439 | 433 | s->temp_src[1] = av_calloc(s->air_len, sizeof(float)); |
| 440 | 434 | |
| 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]) { | |
| 444 | 438 | ret = AVERROR(ENOMEM); |
| 445 | 439 | goto fail; |
| 446 | 440 | } |
| @@ -475,10 +469,12 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 28 unmodified lines | ||
| 475 | 469 | if (idx == -1) |
| 476 | 470 | continue; |
| 477 | 471 | 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 | + | |
| 479 | 475 | 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; | |
| 482 | 478 | } |
| 483 | 479 | } else { |
| 484 | 480 | 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 | ||
| 514 | 510 | |
| 515 | 511 | I = idx * 2; |
| 516 | 512 | 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 | + | |
| 518 | 516 | 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; | |
| 521 | 519 | } |
| 522 | 520 | } else { |
| 523 | 521 | 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 | ||
| 542 | 540 | av_frame_free(&s->in[i + 1].frame); |
| 543 | 541 | } |
| 544 | 542 | |
| 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) { | |
| 549 | 544 | s->data_hrtf[0] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex)); |
| 550 | 545 | s->data_hrtf[1] = av_calloc(n_fft * s->nb_irs, sizeof(FFTComplex)); |
| 551 | 546 | if (!s->data_hrtf[0] || !s->data_hrtf[1]) { |
| @@ -566,9 +561,6 @@ fail: · 14 unmodified lines | ||
| 566 | 561 | for (i = 0; i < s->nb_inputs - 1; i++) |
| 567 | 562 | av_frame_free(&s->in[i + 1].frame); |
| 568 | 563 | |
| 569 | − av_freep(&data_ir_l); | |
| 570 | − av_freep(&data_ir_r); | |
| 571 | − | |
| 572 | 564 | av_freep(&data_hrtf_l); |
| 573 | 565 | av_freep(&data_hrtf_r); |
| 574 | 566 | |