From patchwork Tue Nov 3 17:57:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23359 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 D92C044B2DE for ; Tue, 3 Nov 2020 19:57:50 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B62F068B5C5; Tue, 3 Nov 2020 19:57:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F348D68A7F4 for ; Tue, 3 Nov 2020 19:57:43 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id n18so19457838wrs.5 for ; Tue, 03 Nov 2020 09:57:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Sy5OMDDdK/EC70QdMREV6AxxGIstDJm1Pz0cT7ak6jY=; b=gA73MtnKajTpKdKx5MdwVzsNQX/vpYV2Rb/SZhQJmIEJpum+27U2awCEp7FQYxg5Ad E9+oCoKPToNPkuai7xjzJecaMCjrUqMH6jiB54hfwdBKEAX4bCzvlDY1+JnICwZ7z1v2 hswMyWjWbFBdH/Io0aNVPVaUFtI9U2feK8GY9IVG1oUbUpXnl7BQjmCogiGs2f5XpAd+ XEJkWTMBEF3SFS+TAGeU3agwY79VB0eF03YgxO+9jqyN4W29OIiTzeUpLdhFfzaqZJ6I t/QxQSypriipWV6AbOKzne+Kdj5Q2c1geyMEavSWznwl/4cOHFQ38lRnR3WmX2vfKrlJ CaAA== 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:mime-version :content-transfer-encoding; bh=Sy5OMDDdK/EC70QdMREV6AxxGIstDJm1Pz0cT7ak6jY=; b=TfNU1kP/jLr3bgxKuHHtvJVSu6DPG3onUyYWapWwLXQZxqM2wQ6DIeLpzL+KNBmJb9 PhvB22/ywvGYMM69E9KfqLDHxwxcjVrurqNmWGxDDONNYgmUihSVr8E45dL4sidOSMRh qXE5LEyIpXMt6mQkcplE5+GfLCzFFos+TCJ6dhCjYuhUgMMDfFW6CW1jQFPaAy9mi4rt PhmoonRB4WOQ1bOz49VSUzbq4ndGp0C/ezK/fUNz13oY+/lv1suluDm73agO4QDdPKSl cTz51Ww/iOjIdFNUSoteGYzI4L0ksrE3NQWjD1OVkglXTqfHP8Aod5QsKCoAxV93Tb2+ u9gg== X-Gm-Message-State: AOAM530z1PQKr0Byq/S/OFltBu1ToXOWmmtbNby1cas8TULEv6Zhy8jX n3eQeO64Y0WkzQvtuudlrbu4E4CEkD8= X-Google-Smtp-Source: ABdhPJwf62b+qVgIKioJ4feqr1Daa0/5HTEOeUNp0gD64MGDRL3daAD4n1ITOKYotBgVNb7qpHeu0Q== X-Received: by 2002:a5d:4148:: with SMTP id c8mr27246013wrq.261.1604426263165; Tue, 03 Nov 2020 09:57:43 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id n8sm3586453wmc.11.2020.11.03.09.57.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Nov 2020 09:57:42 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 3 Nov 2020 18:57:29 +0100 Message-Id: <20201103175735.170080-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/7] avcodec/sonic: Allocate several buffers together 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" It simplifies freeing them and reduces the amount of allocations. Signed-off-by: Andreas Rheinhardt --- libavcodec/sonic.c | 48 ++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c index 0d4891ebf4..bca1eca27a 100644 --- a/libavcodec/sonic.c +++ b/libavcodec/sonic.c @@ -596,6 +596,7 @@ static inline int code_samplerate(int samplerate) static av_cold int sonic_encode_init(AVCodecContext *avctx) { SonicContext *s = avctx->priv_data; + int *coded_samples; PutBitContext pb; int i; @@ -655,12 +656,11 @@ static av_cold int sonic_encode_init(AVCodecContext *avctx) if (!s->predictor_k) return AVERROR(ENOMEM); - for (i = 0; i < s->channels; i++) - { - s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples)); - if (!s->coded_samples[i]) - return AVERROR(ENOMEM); - } + coded_samples = av_calloc(s->block_align, s->channels * sizeof(**s->coded_samples)); + if (!coded_samples) + return AVERROR(ENOMEM); + for (i = 0; i < s->channels; i++, coded_samples += s->block_align) + s->coded_samples[i] = coded_samples; s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples)); @@ -706,11 +706,8 @@ static av_cold int sonic_encode_init(AVCodecContext *avctx) static av_cold int sonic_encode_close(AVCodecContext *avctx) { SonicContext *s = avctx->priv_data; - int i; - - for (i = 0; i < s->channels; i++) - av_freep(&s->coded_samples[i]); + av_freep(&s->coded_samples[0]); av_freep(&s->predictor_k); av_freep(&s->tail); av_freep(&s->tap_quant); @@ -859,6 +856,7 @@ static const int samplerate_table[] = static av_cold int sonic_decode_init(AVCodecContext *avctx) { SonicContext *s = avctx->priv_data; + int *tmp; GetBitContext gb; int i; int ret; @@ -951,19 +949,18 @@ static av_cold int sonic_decode_init(AVCodecContext *avctx) s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k)); - for (i = 0; i < s->channels; i++) - { - s->predictor_state[i] = av_calloc(s->num_taps, sizeof(**s->predictor_state)); - if (!s->predictor_state[i]) - return AVERROR(ENOMEM); - } + tmp = av_calloc(s->num_taps, s->channels * sizeof(**s->predictor_state)); + if (!tmp) + return AVERROR(ENOMEM); + for (i = 0; i < s->channels; i++, tmp += s->num_taps) + s->predictor_state[i] = tmp; + + tmp = av_calloc(s->block_align, s->channels * sizeof(**s->coded_samples)); + if (!tmp) + return AVERROR(ENOMEM); + for (i = 0; i < s->channels; i++, tmp += s->block_align) + s->coded_samples[i] = tmp; - for (i = 0; i < s->channels; i++) - { - s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples)); - if (!s->coded_samples[i]) - return AVERROR(ENOMEM); - } s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples)); if (!s->int_samples) return AVERROR(ENOMEM); @@ -975,15 +972,12 @@ static av_cold int sonic_decode_init(AVCodecContext *avctx) static av_cold int sonic_decode_close(AVCodecContext *avctx) { SonicContext *s = avctx->priv_data; - int i; av_freep(&s->int_samples); av_freep(&s->tap_quant); av_freep(&s->predictor_k); - for (i = 0; i < MAX_CHANNELS; i++) { - av_freep(&s->predictor_state[i]); - av_freep(&s->coded_samples[i]); - } + av_freep(&s->predictor_state[0]); + av_freep(&s->coded_samples[0]); return 0; }