From patchwork Sat Feb 4 00:41:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 2420 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp881244vsb; Fri, 3 Feb 2017 16:41:31 -0800 (PST) X-Received: by 10.28.174.208 with SMTP id x199mr40157wme.107.1486168891429; Fri, 03 Feb 2017 16:41:31 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q73si34016254wrb.323.2017.02.03.16.41.30; Fri, 03 Feb 2017 16:41:31 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6908F680C77; Sat, 4 Feb 2017 02:41:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A62468090C for ; Sat, 4 Feb 2017 02:41:20 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 86CF6100BE9; Sat, 4 Feb 2017 01:41:20 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MDEYvuRb40Aa; Sat, 4 Feb 2017 01:41:19 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 0EE91100B4E; Sat, 4 Feb 2017 01:41:19 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 4 Feb 2017 01:41:05 +0100 Message-Id: <20170204004105.25327-1-cus@passwd.hu> X-Mailer: git-send-email 2.10.2 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCHv2 2/2] avfilter/ebur128: fix relative threshold calculation for multiple contexts 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: k@ylo.ph, Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This reworks the code a bit and also disallows NULL contexts. Fixes Coverity CID 1396273, 1396279. Signed-off-by: Marton Balint --- libavfilter/ebur128.c | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/libavfilter/ebur128.c b/libavfilter/ebur128.c index 012df54..e110080 100644 --- a/libavfilter/ebur128.c +++ b/libavfilter/ebur128.c @@ -519,26 +519,27 @@ FF_EBUR128_ADD_FRAMES(int) FF_EBUR128_ADD_FRAMES(float) FF_EBUR128_ADD_FRAMES(double) -static int ebur128_calc_relative_threshold(FFEBUR128State * st, - size_t * above_thresh_counter, +static int ebur128_calc_relative_threshold(FFEBUR128State **sts, size_t size, double *relative_threshold) { - size_t i; + size_t i, j; + int above_thresh_counter = 0; *relative_threshold = 0.0; - *above_thresh_counter = 0; - for (i = 0; i < 1000; ++i) { - *relative_threshold += st->d->block_energy_histogram[i] * - histogram_energies[i]; - *above_thresh_counter += st->d->block_energy_histogram[i]; + for (i = 0; i < size; i++) { + unsigned long *block_energy_histogram = sts[i]->d->block_energy_histogram; + for (j = 0; j < 1000; ++j) { + *relative_threshold += block_energy_histogram[j] * histogram_energies[j]; + above_thresh_counter += block_energy_histogram[j]; + } } - if (*above_thresh_counter != 0) { - *relative_threshold /= (double) *above_thresh_counter; + if (above_thresh_counter != 0) { + *relative_threshold /= (double)above_thresh_counter; *relative_threshold *= RELATIVE_GATE_FACTOR; } - return 0; + return above_thresh_counter; } static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size, @@ -549,20 +550,11 @@ static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size, size_t above_thresh_counter; size_t i, j, start_index; - for (i = 0; i < size; i++) { - if (sts[i] - && (sts[i]->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I) { + for (i = 0; i < size; i++) + if ((sts[i]->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I) return AVERROR(EINVAL); - } - } - for (i = 0; i < size; i++) { - if (!sts[i]) - continue; - ebur128_calc_relative_threshold(sts[i], &above_thresh_counter, - &relative_threshold); - } - if (!above_thresh_counter) { + if (!ebur128_calc_relative_threshold(sts, size, &relative_threshold)) { *out = -HUGE_VAL; return 0; } @@ -577,8 +569,6 @@ static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size, } } for (i = 0; i < size; i++) { - if (!sts[i]) - continue; for (j = start_index; j < 1000; ++j) { gated_loudness += sts[i]->d->block_energy_histogram[j] * histogram_energies[j]; @@ -597,15 +587,11 @@ static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size, int ff_ebur128_relative_threshold(FFEBUR128State * st, double *out) { double relative_threshold; - size_t above_thresh_counter; if ((st->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I) return AVERROR(EINVAL); - ebur128_calc_relative_threshold(st, &above_thresh_counter, - &relative_threshold); - - if (!above_thresh_counter) { + if (!ebur128_calc_relative_threshold(&st, 1, &relative_threshold)) { *out = -70.0; return 0; }