From patchwork Sat Sep 28 02:25:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15345 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 15FBD449F5A for ; Sat, 28 Sep 2019 05:34:05 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EF81B68A41B; Sat, 28 Sep 2019 05:34:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9EF8F6899C5 for ; Sat, 28 Sep 2019 05:33:56 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id f22so7296770wmc.2 for ; Fri, 27 Sep 2019 19:33:56 -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=b1Aj2Jr/JMlkr+Jn0dZoVwQrhOpjTbd/tv6PMkOntig=; b=nniDFu49yOMe2jmvyboHRoL2sXB/NBBMTfwv4oztMpDihN/Jpj0vfGusDJi76Dd8ip egWFu4zlZ3y+ThqZeVITM+VSn2yeIqEG8WsTqHH3lNJrIwcOwu0UY77IVqQQbhD8kY3v Mpzy/ATp7AXFUS7dsh9UvXZ2VH3Ir3QvWgKzo+ArAX7BCzn1/GDRVjbnauxTcjv7HpOd 4pTfnAOTyHP3E9aH971b1ua8EMrQEnjAYYdry78uqf8vjGplzThPXKtP13kMjfyb+zDz icL8R4Wsj09RWEWAD1PvziQQ/wlpx7PSUWqvqWQVnXhFGrHAywFwwHHTMwr7iPq8Irqx WhDg== 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=b1Aj2Jr/JMlkr+Jn0dZoVwQrhOpjTbd/tv6PMkOntig=; b=NfHMFCDiYAI7Iue/Gq3MmYuRW+w/MTSmOqi1NcHGODbRAPDf4muB8nVCoJlkorKBsF /Xz1QqUqmatoK5t+XrFOaweGPIv2N0Dh+K1WOUsYxg8e6+qS6Ms+dTOHj2jdYw9hMCPz m1lnjohTkH8FF+Rr7azGTCcamGBDctPKDiC0Ktfr4sDZ7YIZrMnwJhQmjA+8lgnCehxt Ow3c1JMJ3KFugzw4T2InEv/d06job3GF/YqX2foFcgGvcy5ylQDQILit/Qykd1tx2dJm /83b20WZyhTJgMEDkUadiljndsuwCq0WjWQ2Ej8JLurRe/1bsHrUt9MUMNGxk3rhugZU zVWA== X-Gm-Message-State: APjAAAWkrOBtFyNUSqobwBxfLlwCjXdmF+ipXaaUML7SZ4zAp4Wv+USB WwePsPeiiP+pyMQoARI9haKaADcN X-Google-Smtp-Source: APXvYqyoShlWIiS9x2Vrhcr4e52cX1abEjRU4lf/rydXDWhU54GOBsJ8zNAsJG8S5xiDBWehx3sJbg== X-Received: by 2002:a1c:9d52:: with SMTP id g79mr8964105wme.91.1569638035964; Fri, 27 Sep 2019 19:33:55 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc0f857.dynamic.kabel-deutschland.de. [188.192.248.87]) by smtp.gmail.com with ESMTPSA id r7sm4484231wrx.87.2019.09.27.19.33.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2019 19:33:55 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Sep 2019 04:25:59 +0200 Message-Id: <20190928022610.5903-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190928022610.5903-1-andreas.rheinhardt@gmail.com> References: <20190928022610.5903-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 04/15] avcodec/adpcm: Fix undefined left shifts of negative numbers 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" Affected the adpcm-afc, adpcm-dtk, adpcm-ea-1, adpcm-ea-2, adpcm-ima-oki, adpcm-ea-maxis-xa, adpcm-ea-r1, adpcm-ea-r2, adpcm-ea-r3 and adpcm-thp FATE-tests. Signed-off-by: Andreas Rheinhardt --- libavcodec/adpcm.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 7f2ebfc99d..a0c5b6a42a 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -342,7 +342,7 @@ static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nib c->predictor = av_clip_intp2(predictor, 11); c->step_index = step_index; - return c->predictor << 4; + return c->predictor * (1 << 4); } static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble) @@ -1279,8 +1279,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, for (count2 = 0; count2 < 28; count2++) { byte = bytestream2_get_byteu(&gb); - next_left_sample = sign_extend(byte >> 4, 4) << shift_left; - next_right_sample = sign_extend(byte, 4) << shift_right; + next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left); + next_right_sample = sign_extend(byte, 4) * (1 << shift_right); next_left_sample = (next_left_sample + (current_left_sample * coeff1l) + @@ -1319,7 +1319,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, if (st) byte[1] = bytestream2_get_byteu(&gb); for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */ for(channel = 0; channel < avctx->channels; channel++) { - int sample = sign_extend(byte[channel] >> i, 4) << shift[channel]; + int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]); sample = (sample + c->status[channel].sample1 * coeff[channel][0] + c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8; @@ -1380,11 +1380,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, for (count2=0; count2<28; count2++) { if (count2 & 1) - next_sample = sign_extend(byte, 4) << shift; + next_sample = sign_extend(byte, 4); else { byte = bytestream2_get_byte(&gb); - next_sample = sign_extend(byte >> 4, 4) << shift; + next_sample = sign_extend(byte >> 4, 4); } + next_sample *= 1 << shift; next_sample += (current_sample * coeff1) + (previous_sample * coeff2); @@ -1595,8 +1596,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, sampledat = sign_extend(byte >> 4, 4); } - sampledat = ((prev1 * factor1 + prev2 * factor2) + - ((sampledat * scale) << 11)) >> 11; + sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) + + sampledat * scale; *samples = av_clip_int16(sampledat); prev2 = prev1; prev1 = *samples++; @@ -1673,7 +1674,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, } sampledat = ((c->status[ch].sample1 * factor1 - + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp); + + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp); *samples = av_clip_int16(sampledat); c->status[ch].sample2 = c->status[ch].sample1; c->status[ch].sample1 = *samples++; @@ -1720,7 +1721,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, else sampledat = sign_extend(byte >> 4, 4); - sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev; + sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev; *samples++ = av_clip_int16(sampledat >> 6); c->status[channel].sample2 = c->status[channel].sample1; c->status[channel].sample1 = sampledat;