From patchwork Sun Oct 22 21:51:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 44323 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1b28:b0:15d:8365:d4b8 with SMTP id ch40csp1069893pzb; Sun, 22 Oct 2023 14:51:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHW/W0GnsW9jSzV3k1bMuHlS70krfN73IstLhFdSXeE9kvGio9SmUI6m8BR4nsPPwASuo75 X-Received: by 2002:a17:907:720f:b0:9bd:804a:5601 with SMTP id dr15-20020a170907720f00b009bd804a5601mr5394440ejc.54.1698011513838; Sun, 22 Oct 2023 14:51:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698011513; cv=none; d=google.com; s=arc-20160816; b=ZhmphS4pUljkm9Pv/8mz0TL8lgOqsBnXZ01qjIKj4/iLFgIu4Wzvnqke+oqbyy6rNa 3rWOAkQjTuyVr7Bd5o7UVfttX7yvaMrH1m8EfIthnaX1Yo8uGF6KfwIT/KzDVb6hKbJE eN6aarKPFRUlNJOifKkTcX3YrGkEdLI6XMWcvqzJNL1B1XLsZoGDSonopdQHOQiTXpfM Hw2Byz+9GvcVWEkZV/PHcw48yZOk2JVMlcB196Kas7l9xj6U59O0+fQC82jypD8wr1ur psY3MzInbamJTBJyttl/I4og50D55MBAdA13ySHIZQWohuT51zgg0SfzAOrmRZ/9jZKp vxOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=ysVV2V7vT2JRDKaLcpOxHhBKZOdBdzTEHeaGsBhsh/0=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=atZaW7yfM75psVkMJcfOyV/VhzwtMxFHJGlF7NrNt+FlSw8oh+BT6VL6lldqKPRcBL O5FFUDhf83Wopp3Eiotbr0eNb+QuiC9QYIs13cRVauT4VuFGCkJDneBJO48RgYNeGk9b XQG5gZXtLJARki2hB6hczriC49q5LBmD90KRYbLI9LuzsHesoFPDzALnU6W+4V0468Qe 25Dn4OnAiMOr9FkHRKmQun+1VQk7ZxnktwHHpHK4GMx5wixFK8G0OPuM9bXhkuEqDfbw lyNsr9GzEbQnAMsjx0YudaIVSZcRKxOBkrArtFtxtjhtTlsofB4QyihGEigwb6mfvMiH wVOg== 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 g12-20020a1709061c8c00b00992bdcb72f0si2663407ejh.339.2023.10.22.14.51.52; Sun, 22 Oct 2023 14:51:53 -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 BB41B68CA29; Mon, 23 Oct 2023 00:51:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7129068C65F for ; Mon, 23 Oct 2023 00:51:17 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id C8D8320003 for ; Sun, 22 Oct 2023 21:51:16 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Sun, 22 Oct 2023 23:51:08 +0200 Message-Id: <20231022215113.3469-4-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231022215113.3469-1-michael@niedermayer.cc> References: <20231022215113.3469-1-michael@niedermayer.cc> X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 4/9] avcodec/vlc: Replace mysterious max computation code in multi vlc 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 76isq/JE7NGk Signed-off-by: Michael Niedermayer --- libavcodec/vlc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libavcodec/vlc.c b/libavcodec/vlc.c index 65883a506ff..9b7a42f79a3 100644 --- a/libavcodec/vlc.c +++ b/libavcodec/vlc.c @@ -359,7 +359,7 @@ static void add_level(VLC_MULTI_ELEM *table, const int is16bit, unsigned* levelcnt, VLC_MULTI_ELEM *info) { int max_symbols = VLC_MULTI_MAX_SYMBOLS >> is16bit; - for (int i = num-1; i > max; i--) { + for (int i = num-1; i >= max; i--) { for (int j = 0; j < 2; j++) { int newlimit, sym; int t = j ? i-1 : i; @@ -398,7 +398,7 @@ static int vlc_multi_gen(VLC_MULTI_ELEM *table, const VLC *single, const int is16bit, const int nb_codes, const int numbits, VLCcode *buf, void *logctx) { - int minbits, maxbits, max = nb_codes-1; + int minbits, maxbits, max; unsigned count[VLC_MULTI_MAX_SYMBOLS-1] = { 0, }; VLC_MULTI_ELEM info = { { 0, }, 0, 0, }; int count0 = 0; @@ -419,10 +419,13 @@ static int vlc_multi_gen(VLC_MULTI_ELEM *table, const VLC *single, } av_assert0(maxbits <= numbits); - while (max >= nb_codes/2) { - if (buf[max].bits+minbits > maxbits) + for (max = nb_codes; max > nb_codes - count0; max--) { + // We can only add a code that fits with the shortest other code into the table + // We assume the table is sorted by bits and we skip subtables which from our + // point of view are basically random corrupted entries + // If we have not a single useable vlc we end with max = nb_codes + if (buf[max - 1].bits+minbits > numbits) break; - max--; } for (int j = 0; j < 1<