From patchwork Tue Sep 8 21:18:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22188 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2E32C44BD83 for ; Wed, 9 Sep 2020 00:19:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1243768B753; Wed, 9 Sep 2020 00:19:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 42CD368B6B4 for ; Wed, 9 Sep 2020 00:19:35 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id m6so719036wrn.0 for ; Tue, 08 Sep 2020 14:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pQSVcDRd3DlbvDZobuGlhZipA6D4JydkknPfG/ADZdc=; b=lv5bfTyRbsUoPmpwQIcZnzAf7SguSEtNiy7M8kORKpH6a8GZB1fg8UxvFCyQv7SufU UyOLrH2QGGFrnSoIiJdNSEK9SQwdHwDFdXhhwU7oW+wU9I/mFCsaSjvBn1SSY028uqFp 9rwjEiLvdkksvpqVmims788dj1hw1J/UEJ4AAovQAp77yvbSe4q92PyN3U/C8nfGl64w wZ5Bx02CLE9Hv3R8nSZvpB0Lcyv0fJ7JLx9IEDcGY/iEZFqWEugUaDFH7f5BV8oEGhvr vgo/VuPvB1rmHz2qNUDYxaK2D3D2bWw/jD20xqV89jQEmxy+XOYHOqs0eIgdqwGBaiSG Z1lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pQSVcDRd3DlbvDZobuGlhZipA6D4JydkknPfG/ADZdc=; b=oM7S9tbxGTdPu2V9eVVq+fOSKsgwSAUhiK3WEOGww6RWwgv3AqNuThpZVZ1noqV8z9 Fj3UR47IA3cXob4pYhn8/wDIEQTK7sujyD/sxXbjU4dAEYt3Eo/4v8+Y3ashgASQ7Xlm X7V0Yjd8WpcGIvAEJu0z81mxbmLT/Kf2nkGkH+V1B6fHdKE+Xto1jWFlO+lXAtSysy7y hZryzQMfVZUTskM5LgQss1HyNXa6nVJrQqWtqW382E+E3AmlC/of6lHu5G8SpKwdBZTW vUMzu27XJqi9j4/Z4DvwvXM0172OFaR/ESjW+ab5SihyybiU6bjNJms5JldnRzRmhmTb a3Tw== X-Gm-Message-State: AOAM532PB+WDE5vHiludojkIoqMerDqIgyTiylRMWebeXINqM2o4UCGm 30gMBolMdgprzdYvb8ZfOJ84NPyip9Q= X-Google-Smtp-Source: ABdhPJzlwztPkcKuS5lvo8kkIsjWrsIPMtk3RvVDq8BAmv1GQqmdu1IKkhYgjzYlE79ja/cvupdV1A== X-Received: by 2002:a5d:4682:: with SMTP id u2mr600897wrq.254.1599599974414; Tue, 08 Sep 2020 14:19:34 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id a15sm1074304wrn.3.2020.09.08.14.19.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2020 14:19:33 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Sep 2020 23:18:45 +0200 Message-Id: <20200908211856.16290-14-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200908211856.16290-1-andreas.rheinhardt@gmail.com> References: <20200908211856.16290-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/25] avfilter/af_headphone: Avoid intermediate buffers I X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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. Signed-off-by: Andreas Rheinhardt --- libavfilter/af_headphone.c | 38 +++++++++++++++----------------------- 1 file 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);