From patchwork Thu Sep 22 15:00:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 38150 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp372603pzh; Thu, 22 Sep 2022 08:00:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6o6uhPRPSkvCQRjcJfynTKiP4gayQsHKkNdi+XnmyRHczNFAh/+UgBd30qkG0XaBPkxHOg X-Received: by 2002:a05:6402:1761:b0:455:37bb:93c3 with SMTP id da1-20020a056402176100b0045537bb93c3mr1432364edb.403.1663858855248; Thu, 22 Sep 2022 08:00:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663858855; cv=none; d=google.com; s=arc-20160816; b=KyQpjhFrcCIiqjIa4NsSm3sjdxWRZnJJp9kKxtLhK/AfkxMjwD186QBR+E1h0T+X0o OF7wdZryn8X/fSi9XKIsz+J6FWyxnYcSDb+jk9F6KH+eeLS7MHy7JnO0HOVNCwvX8A85 Rg0Wc6zqG0ApLVqea2i9GXnrNW5GCkiDcdK/FdCRMOQaDjg28h6L2tq8P/a4iSJMkoUF AS9L/tvP8gYCEGlPNVnOj9CIjx8E6OudWZXXx551IPc3JZpYCZW+uLRdC6fukRFgnHUj 9Cv3pEmG384ntGnMhQCQIRz9980MvBxVaebVolkaiZ5onQbThzmWZ3ihFummF7OsV4vl TkQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=aMRizTqrkIarNOZ7kmst5CRbaEcKOcNa5Kv8uTVp/Qs=; b=rOupqWlmI1VRw4ugIgAObvorRtFPU5zbWi71gs394h0xT+QTl4sL41HUh+qxF6/jQ+ 25S9TORISvuEfjhUoUqSuHTykDA2vP1DqsKSKfyDONX+qkopV4TsEBF+5sdpqEYz9aPy ye3D5gnReeWl6Dr871v7HWfnQ49atMsd/T9VmaU3Uq40xg3haqH8EanBefySRMRpbbFg Lcc5EL9wbjoig2U7b4G8TGWWPwYrssIEybv/N7QNiGnYA+1QXOvPG7nW3lP3r686NNUQ rzSksrcMgQ1SenCSxJjq3zThLhXqK/bujauGShYcT0w8ZfKLKU+QvZMqNQwdUCIpsbki MEPw== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i2-20020a05640242c200b0044ef29c71e9si6273898edc.185.2022.09.22.08.00.51; Thu, 22 Sep 2022 08:00:55 -0700 (PDT) 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 7427968BBDD; Thu, 22 Sep 2022 18:00:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0BB8068BA31 for ; Thu, 22 Sep 2022 18:00:33 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 5BFA2C0072 for ; Thu, 22 Sep 2022 18:00:32 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Sep 2022 18:00:31 +0300 Message-Id: <20220922150032.21811-1-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [REPOST] [PATCHv2 1/2] lavc: avoid rounding errors in float constants X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OmT/qdpMsEJZ From: Rémi Denis-Courmont INT_MAX is (typically) a value with 31 significant bits but float can only represent 23 significant bits, leading to a rounding error. This substitutes the actual rounded value as an unsigned int, to avoid a clang warning while not overflowing signed int: warning: implicit conversion from 'int' to 'float' changes value from 2147483647 to 2147483648 [-Wimplicit-const-int-float-conversion] --- libavcodec/aaccoder.c | 2 +- libavcodec/imc.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index e3b6b2f02c..4eab81c43f 100644 --- a/libavcodec/aaccoder.c +++ b/libavcodec/aaccoder.c @@ -531,7 +531,7 @@ static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, int nz = 0; bandaddr[idx] = w * 16 + g; - qmin = INT_MAX; + qmin = 1U << 31; qmax = 0.0f; for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; diff --git a/libavcodec/imc.c b/libavcodec/imc.c index 92f9980ded..afdb93636a 100644 --- a/libavcodec/imc.c +++ b/libavcodec/imc.c @@ -917,7 +917,7 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch) chctx->flcoeffs1, chctx->flcoeffs2); for(i=0; iflcoeffs1[i] > INT_MAX) { + if(chctx->flcoeffs1[i] > (1U << 31)) { av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n"); return AVERROR_INVALIDDATA; } From patchwork Thu Sep 22 15:00:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 38149 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp372398pzh; Thu, 22 Sep 2022 08:00:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4vRyWU1T7eBuynUrL4MGOjnsLbhs7VPn/LlrFXAvqN180021sA/cG33xeRghLSoCAlRvV6 X-Received: by 2002:a17:907:6d9f:b0:77a:424b:23d4 with SMTP id sb31-20020a1709076d9f00b0077a424b23d4mr3242988ejc.75.1663858842823; Thu, 22 Sep 2022 08:00:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663858842; cv=none; d=google.com; s=arc-20160816; b=Kk0xKlidnwHFyyuL6OTQS+d0fgAwhjxeajRSQgOr4rdz3JU7ucKZlylKxXjeiRJ29Z otuqt7E+MQZdXeBCewBmGwCvKJeMn4737Iw6lwU7PwvypvuiMNuma3jXjBkSQcbguhWJ tLz/fsQPdVAGkHFa8z9mE4H3l1B3fVWsC6XjbNbiFTm5hgk6H3cMOXwyq2yh+juL+cAi 3xvYcGODuUuQdtZtvrPE/fIxqqz+Gb2+lTVR51EKykgl00LaH7mm1stOECAfNAUkTpzi PAMwVJXEnoizya3JcMp74lSmIZQRM6oQ1aYlFyUKnbXt+N7bq93lULxweOqDB3I+pIwh GDfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=J9jSjEfFXc7EqlHI2OP1cvRpPwI1125z40413Angfsg=; b=ujr7XlYMJU0q5fIyPz+ErgZH8FxIZ9qyMbxP34ci2EO5DLjWnpIsnB1Cs3TNf0aer5 YCETdHeCAt1xFpkt5BNlWkZRbJcfpzgwDz5LjdrNDGvPziyKPlMs4bOcbFX8Knuoo+9E VZ4EY7mJultbJJvH5LNWJ1+W3i7RooinOk3hlYSO/1aw73cKdR/kF7aU9scp3Io44GM6 kRBtvztupBQa7ssl05bh1InrWGzgwTib/hvxKRu3XDmfWkQlS9XEMlEoeDugWPYp94LR KmbqzdXCZPoiaR1tZgSObMVIcSzsEnqoU0VoJC9Ytf0FNW3fRKUfYd0fCwVf3u1Q7sa7 WL7g== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s7-20020a50ab07000000b00446dc8f5e44si5420136edc.243.2022.09.22.08.00.42; Thu, 22 Sep 2022 08:00:42 -0700 (PDT) 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 A7ED968BBD8; Thu, 22 Sep 2022 18:00:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0BD2A68BBCB for ; Thu, 22 Sep 2022 18:00:33 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 8A196C007A for ; Thu, 22 Sep 2022 18:00:32 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Sep 2022 18:00:32 +0300 Message-Id: <20220922150032.21811-2-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [REPOST] [PATCHv2 2/2] lavc/audiodsp: fix aliasing violation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: aub7mOFdiAue From: Rémi Denis-Courmont Even though they have the same size, and typically the same alignment, uint32_t and float are under no circumstances compatible types in C. The casts from float * to uint32_t * are invalid here. Insofar as the resulting pointers are dereferenced, this is undefined behaviour. This patch uses av_float2int() / av_int2float() instead. --- libavcodec/audiodsp.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/libavcodec/audiodsp.c b/libavcodec/audiodsp.c index ff43e87dce..8c0847f53f 100644 --- a/libavcodec/audiodsp.c +++ b/libavcodec/audiodsp.c @@ -22,36 +22,36 @@ #include "libavutil/common.h" #include "audiodsp.h" -static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini, - uint32_t maxi, uint32_t maxisign) +static inline float clipf_c_one(float a, uint32_t mini, + uint32_t maxi, uint32_t maxisign) { - if (a > mini) - return mini; - else if ((a ^ (1U << 31)) > maxisign) - return maxi; + uint32_t ai = av_float2int(a); + + if (ai > mini) + return av_int2float(mini); + else if ((ai ^ (1U << 31)) > maxisign) + return av_int2float(maxi); else return a; } static void vector_clipf_c_opposite_sign(float *dst, const float *src, - float *min, float *max, int len) + float min, float max, int len) { int i; - uint32_t mini = *(uint32_t *) min; - uint32_t maxi = *(uint32_t *) max; + uint32_t mini = av_float2int(min); + uint32_t maxi = av_float2int(max); uint32_t maxisign = maxi ^ (1U << 31); - uint32_t *dsti = (uint32_t *) dst; - const uint32_t *srci = (const uint32_t *) src; for (i = 0; i < len; i += 8) { - dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign); - dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign); - dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign); - dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign); - dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign); - dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign); - dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign); - dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign); + dst[i + 0] = clipf_c_one(src[i + 0], mini, maxi, maxisign); + dst[i + 1] = clipf_c_one(src[i + 1], mini, maxi, maxisign); + dst[i + 2] = clipf_c_one(src[i + 2], mini, maxi, maxisign); + dst[i + 3] = clipf_c_one(src[i + 3], mini, maxi, maxisign); + dst[i + 4] = clipf_c_one(src[i + 4], mini, maxi, maxisign); + dst[i + 5] = clipf_c_one(src[i + 5], mini, maxi, maxisign); + dst[i + 6] = clipf_c_one(src[i + 6], mini, maxi, maxisign); + dst[i + 7] = clipf_c_one(src[i + 7], mini, maxi, maxisign); } } @@ -61,7 +61,7 @@ static void vector_clipf_c(float *dst, const float *src, int len, int i; if (min < 0 && max > 0) { - vector_clipf_c_opposite_sign(dst, src, &min, &max, len); + vector_clipf_c_opposite_sign(dst, src, min, max, len); } else { for (i = 0; i < len; i += 8) { dst[i] = av_clipf(src[i], min, max);