From patchwork Sat Sep 18 10:01:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Ross X-Patchwork-Id: 30356 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp120545iov; Sat, 18 Sep 2021 03:02:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7EICLry2AKTI+tu1fve/meWgQpGkW48wi/+bo4koHMuqq/BZuRT5d4gUKIIyAf2Ec5AIw X-Received: by 2002:a50:9b52:: with SMTP id a18mr17760918edj.165.1631959322154; Sat, 18 Sep 2021 03:02:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631959322; cv=none; d=google.com; s=arc-20160816; b=j7WX5O4+aceFgVqtrBkl5Im+L3baqGkbuG/y5H1dEjJMuRSy9MxPZnuFvTXsM+f4/z kDd05asVB2MK+raNAmwZHuJF5rxZYiTuQpMU9aXWl25k6arXeJiZ4jOApSk/MfWG5IQt 6TRbUI0KFgnH6J/DU7q+rzaOKfrVR3RDdsx53FgsFzzSHgFeDXM8i59YjzcsFbdsbjUm JAxlnudFfEOS+uyoV2htsFBQxipBGa2mB20a4KPcP6uX9bUguU9X07O4F/bnsLHXGMGH 2N5hGPjZB0dulyYfByMMA3J4CfGbgbrCAdHBWOBrPxm+kfWFRaBdOxnuJgEv7q9+MaIQ tk5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:message-id:to:from:date:delivered-to; bh=ImrUE9YvgsAUHrXwtFUAk1vnUuZVoGG5HSdxnOaR6Ts=; b=wMTnPZskoWHsO7nFwmqqVipitC3HGsAuO11DbqL+2P9NmbdNSCGJYa/qfasxYAc+X1 RXj2W5oBoCbZ/gErYCZ6kXrjK2vT/bDBfFAMHy/WbxJFp12f/KKu+nTwX9Dejm8vo4cw 4qEOCM0kiYwa3DeJF+R9BMk2ap1bKq0eDCl46clB9mw67dOy7BRk9+6PCAzCRHerbNXQ ZmzXqFIDoghxynwZCWBgDA+G3hm//K6K7dGYiiz44JnEH9Y04wg0L7//4Fqdj1GvIbB5 qjXOa6uWZOzwZ8HvO98N35rncC+buE4XEFabZsdkTuCTwWTKlmdQRyjTn4y74OhYl8p4 hk8g== 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 v22si10433953edb.257.2021.09.18.03.01.59; Sat, 18 Sep 2021 03:02:02 -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 A5CA868ADE9; Sat, 18 Sep 2021 13:01:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mx.sdf.org (mx.sdf.org [205.166.94.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 07A5468AA0C for ; Sat, 18 Sep 2021 13:01:48 +0300 (EEST) Received: from 01265acabc6aad6a4a464cb17c468ad3 ([1.145.161.12]) (authenticated (128 bits)) by mx.sdf.org (8.15.2/8.14.5) with ESMTPSA id 18IA1geI016387 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for ; Sat, 18 Sep 2021 10:01:46 GMT Date: Sat, 18 Sep 2021 20:01:38 +1000 From: Peter Ross To: ffmpeg-devel@ffmpeg.org Message-ID: <5da14be177af2fd768c1cde4d450b27d659a5472.1631959242.git.pross@xvid.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/siren: prevent getbitcontext overread 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: uYaPg+NP7cn6 --- libavcodec/siren.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libavcodec/siren.c b/libavcodec/siren.c index 2161b29a2c..3b0ad7b642 100644 --- a/libavcodec/siren.c +++ b/libavcodec/siren.c @@ -608,12 +608,16 @@ static int decode_vector(SirenContext *s, int number_of_regions, index >>= 1; - if (error == 0 && get_bits_left(gb) >= 0) { + if (error == 0) { for (j = 0; j < vector_dimension[category]; j++) { decoded_value = mlt_quant[category][index & ((1 << index_table[category]) - 1)]; index >>= index_table[category]; if (decoded_value) { + if (get_bits_left(gb) <= 0) { + error = 1; + break; + } if (!get_bits1(gb)) decoded_value *= -decoder_standard_deviation[region]; else From patchwork Sat Sep 18 10:02:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Ross X-Patchwork-Id: 30355 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp121438iov; Sat, 18 Sep 2021 03:03:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJArhSNOmIxY9nbwGvBNsgQq+eBiSMlUqV32MQuejo69zloBNE9P0qk0WaZasO31CJH3Bd X-Received: by 2002:a17:906:3e81:: with SMTP id a1mr4335041ejj.482.1631959395013; Sat, 18 Sep 2021 03:03:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631959395; cv=none; d=google.com; s=arc-20160816; b=LiEWfW5Ir6XbedpUsNdRbFwkOm7AxUv74FOOjC6HX4GgRKhdo/TTj+5bFbitiixvRJ NIsJZ0J9m+dxB8RZCwfQtN2DuYt5aDP/RfDpMmt7IFe0F0353BLhPVC1iKVQhDbdXhlC KrYeXoH5CWa8ypHjeMh4JxBJtsXpqCdifYV9CUKRrB1vzpiVdYuJSbhNHdFnL3+JRHkO ECtUHpfpIS2wHREKLnpuQl9CKQQlkCFA1OTJjSBpI65EdfAWZVjyHFKiPKlWQh1C6R/W FfonBveQ+xIo/AJP1S9jQvemvkoOnpaO4RnQuMsDLLej/XIgmTa/znOcAFrdpMq1hcdN Epmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :in-reply-to:mime-version:references:message-id:to:from:date :delivered-to; bh=X7cVj1ST5ImJIxLgG1T5AW3g3hDZ1W2Otkj2yjNJnDM=; b=rxqLG71omy24o0Tx6wus3Wsd0p5Q15WzsnpjWHNfkrlUsDh0JdVj0rWVPgWyPHPVPj XvTaevB84g0PcmJ4CzZkP+hu+flLtODFho0vvSDU6O0inQzEWfuAyjPsg0s+Wke30+gv LTdYOA85+PSXew2Tseh4kopMGwyVtllXcfpV2gNTKIoQTl6TkDjpMvokRWhIAQc6uHAY CHlVjRBuYFoEa47mTofS0bHCtsOS5x2nG0aS3hKpYYozN9YmuRPLq0LV11yEWmbte780 fbnUghxCn2hx3IctlTuQsulGQAZrWmmByOB2IT0y3bLiBd91ktihjjeSfewHJcvHj89C wJrg== 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 o29si10042027edz.353.2021.09.18.03.03.14; Sat, 18 Sep 2021 03:03:15 -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 9DA8368B09D; Sat, 18 Sep 2021 13:03:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mx.sdf.org (mx.sdf.org [205.166.94.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 033B168AEE2 for ; Sat, 18 Sep 2021 13:03:05 +0300 (EEST) Received: from 8cca0619ae923910e5991cca6d545b97 ([1.145.161.12]) (authenticated (128 bits)) by mx.sdf.org (8.15.2/8.14.5) with ESMTPSA id 18IA2wAQ012913 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for ; Sat, 18 Sep 2021 10:03:03 GMT Date: Sat, 18 Sep 2021 20:02:54 +1000 From: Peter Ross To: ffmpeg-devel@ffmpeg.org Message-ID: <850ab9581cc2c568e34d013045b6db92a2c46fef.1631959242.git.pross@xvid.org> References: <5da14be177af2fd768c1cde4d450b27d659a5472.1631959242.git.pross@xvid.org> MIME-Version: 1.0 In-Reply-To: <5da14be177af2fd768c1cde4d450b27d659a5472.1631959242.git.pross@xvid.org> Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/siren: don't reduce getbitcontext size by checksum_bits at initialisation 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: go2h8nS3QuuV this allows the checksum calculation routine to also use getbitcontext --- at the expense of having to offset get_bits_left(gb) everywhere libavcodec/siren.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/siren.c b/libavcodec/siren.c index 3b0ad7b642..92fd3632f5 100644 --- a/libavcodec/siren.c +++ b/libavcodec/siren.c @@ -594,7 +594,7 @@ static int decode_vector(SirenContext *s, int number_of_regions, for (i = 0; i < number_of_vectors[category]; i++) { index = 0; do { - if (get_bits_left(gb) <= 0) { + if (get_bits_left(gb) - s->checksum_bits <= 0) { error = 1; break; } @@ -614,7 +614,7 @@ static int decode_vector(SirenContext *s, int number_of_regions, index >>= index_table[category]; if (decoded_value) { - if (get_bits_left(gb) <= 0) { + if (get_bits_left(gb) - s->checksum_bits <= 0) { error = 1; break; } @@ -693,7 +693,7 @@ static int decode_vector(SirenContext *s, int number_of_regions, } } - return error == 1 ? AVERROR_INVALIDDATA : get_bits_left(gb); + return error == 1 ? AVERROR_INVALIDDATA : (get_bits_left(gb) - s->checksum_bits); } static int siren_decode(AVCodecContext *avctx, void *data, @@ -712,7 +712,7 @@ static int siren_decode(AVCodecContext *avctx, void *data, if (avpkt->size < bits_per_frame / 8) return AVERROR_INVALIDDATA; - if ((ret = init_get_bits(gb, avpkt->data, bits_per_frame - s->checksum_bits)) < 0) + if ((ret = init_get_bits(gb, avpkt->data, bits_per_frame)) < 0) return ret; } else if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0) @@ -726,7 +726,7 @@ static int siren_decode(AVCodecContext *avctx, void *data, rate_control = get_bits(gb, 4); - ret = categorize_regions(s->number_of_regions, get_bits_left(gb), + ret = categorize_regions(s->number_of_regions, get_bits_left(gb) - s->checksum_bits, s->absolute_region_power_index, s->power_categories, s->category_balance); if (ret < 0) @@ -741,11 +741,11 @@ static int siren_decode(AVCodecContext *avctx, void *data, if (ret < 0 && !s->microsoft) return ret; - if (get_bits_left(gb) > 0) { + if (get_bits_left(gb) - s->checksum_bits > 0) { do { frame_error |= !get_bits1(gb); - } while (get_bits_left(gb) > 0); - } else if (get_bits_left(gb) < 0 && + } while (get_bits_left(gb) - s->checksum_bits > 0); + } else if (get_bits_left(gb) - s->checksum_bits < 0 && rate_control + 1 < s->rate_control_possibilities) { frame_error = 1; } From patchwork Sat Sep 18 10:03:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Ross X-Patchwork-Id: 30354 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp121804iov; Sat, 18 Sep 2021 03:03:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVpSpRFF5FixGid9LGlY7I1dS+LH475jHTMhnArNkExFnfU4fpD5TDLn2kffG0HvZP+qO5 X-Received: by 2002:a05:6402:5107:: with SMTP id m7mr17813136edd.63.1631959421096; Sat, 18 Sep 2021 03:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631959421; cv=none; d=google.com; s=arc-20160816; b=0qN3eAdY9U5mSt+X2o6jueTYTObXnxf/tbYOnQk+X3+3bCjIBaeNPONQIt775Nc1Ub bvZTv1DQzJnP/35lzKEPPTqvSs2Q7EM66z8VZOCj67YBtTC7c8VNpj/d71+c/U2hzGfD uAKLgT8XfXOpCbXDXjSF7mGy8ev5/A7lRhGQGPgZVts9FAChe+1RcjbmkWRxsDyCbUIu 1IKuH7VbVL4OBZ3C2qKw8qN+GozzSR7ogSbxZzhZ2u1v+6Z5ynymh7z+7dpeV8KLbQdh AEYLQq4OMTT5gjZy4wS+/zmBOllDgwkBhYHclcZ8gPw03MoOzeI+GQtAGMPaip3YalVl /d3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :in-reply-to:mime-version:references:message-id:to:from:date :delivered-to; bh=3++wvG8hvJgWY7WX9Cm03x7UvPFrkDsC6Q15pA6m4Ps=; b=RhjEJ8VB4tkiRdUv6iJfXDtyT/k10qhYqvqJ9vgwx1drCVtdQ8rbfmoNaM5osZxZwc vovWNZz1lN8AUx4iVvVlu4DykK6DCP1FK5aDT1bcamA9DoTGt4KVvvzcRWDonHjniYAn Hl4SXuWK0FpsjovL6c6v6Sayw4PSqgCc5HqLhRlCQEniJ5E6gOTLpgMFNzhD2nu7EwzC oyxI1zksHzkqD7YoDYNns+v+UijWWtm71cPeB4gDfNv+uTkJKNYrBVLm+MdOm06UVa97 1ZFYYEHGfO7j/wqLLm2DLDJ60ZSEW0L5pT8TGyuUaloNlnyrN8cXA4fZHeNCdKKgYKpz CYDw== 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 gb4si5414147ejc.246.2021.09.18.03.03.40; Sat, 18 Sep 2021 03:03:41 -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 8642F68B15A; Sat, 18 Sep 2021 13:03:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mx.sdf.org (mx.sdf.org [205.166.94.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81A8068AC21 for ; Sat, 18 Sep 2021 13:03:36 +0300 (EEST) Received: from 4d660feff0cdf23df522b53dcb6770fa ([1.145.161.12]) (authenticated (128 bits)) by mx.sdf.org (8.15.2/8.14.5) with ESMTPSA id 18IA3T81024615 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for ; Sat, 18 Sep 2021 10:03:34 GMT Date: Sat, 18 Sep 2021 20:03:26 +1000 From: Peter Ross To: ffmpeg-devel@ffmpeg.org Message-ID: References: <5da14be177af2fd768c1cde4d450b27d659a5472.1631959242.git.pross@xvid.org> MIME-Version: 1.0 In-Reply-To: <5da14be177af2fd768c1cde4d450b27d659a5472.1631959242.git.pross@xvid.org> Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/siren: add checksum calculation 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: Fk9Nfl97xPg7 --- libavcodec/siren.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/libavcodec/siren.c b/libavcodec/siren.c index 92fd3632f5..2d3969f24c 100644 --- a/libavcodec/siren.c +++ b/libavcodec/siren.c @@ -756,7 +756,37 @@ static int siren_decode(AVCodecContext *avctx, void *data, frame_error = 1; } - skip_bits(gb, s->checksum_bits); + if ((avctx->err_recognition & AV_EF_CRCCHECK) && s->checksum_bits) { + static const uint16_t ChecksumTable[4] = {0x7F80, 0x7878, 0x6666, 0x5555}; + int wpf, checksum, sum, calculated_checksum, temp1; + + checksum = get_bits(gb, s->checksum_bits); + + wpf = bits_per_frame / 16; + sum = 0; + for (int i = 0; i < wpf - 1; i++) + sum ^= AV_RB16(avpkt->data + i * 2) << (i % 15); + sum ^= (AV_RB16(avpkt->data + (wpf - 1) * 2) & ~checksum) << ((wpf - 1) % 15); + sum = (sum >> 15) ^ (sum & 0x7FFF); + + calculated_checksum = 0; + for (int i = 0; i < 4; i++) { + temp1 = ChecksumTable[i] & sum; + + for (int j = 8; j > 0; j >>= 1) + temp1 ^= temp1 >> j; + + calculated_checksum <<= 1; + calculated_checksum |= temp1 & 1; + } + + if (checksum != calculated_checksum) { + av_log(avctx, AV_LOG_WARNING, "Invalid checksum\n"); + if (avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + frame_error = 1; + } + } if (frame_error) { memcpy(s->imdct_in, s->backup_frame, number_of_valid_coefs * sizeof(float));