From patchwork Tue Sep 26 22:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 43919 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2a18:b0:15d:8365:d4b8 with SMTP id e24csp2207899pzh; Tue, 26 Sep 2023 15:16:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCBXnOUsPv0T6oOnvLrAH6tSOObcC+6JyKOMeXnv6dAp0qT/Na2VWe8l4yz2XOl/kTnwDy X-Received: by 2002:a50:ee8d:0:b0:522:2d1b:5a38 with SMTP id f13-20020a50ee8d000000b005222d1b5a38mr285559edr.10.1695766600048; Tue, 26 Sep 2023 15:16:40 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r8-20020aa7cfc8000000b00530bfea3104si11081992edy.266.2023.09.26.15.16.33; Tue, 26 Sep 2023 15:16:40 -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; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b="d/yvKm3z"; arc=fail (body hash mismatch); 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7763668CA66; Wed, 27 Sep 2023 01:16:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04olkn2085.outbound.protection.outlook.com [40.92.73.85]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2A45F68C8DA for ; Wed, 27 Sep 2023 01:16:25 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PDyLwwAjbRIdcP+qdpePIEFu0hivo0CpIUvsSH1hvYNV28DiAWlbOleEqaF/cWbYrpW00RDPpTFCK7Ku7SFjApw8JA6WYJJhTf8EdGrmGaXMOCS3+Ljv5bJsVZ52KBta0Xh0d33HzyeQR6UZh5M/H32ltE6JYnjbxIKaHaQzifczMsTUZPnU8v9zOOTktjjXAHct8LQkQCXsanWpYN2CR8yJCaImwusGO2H0iJDHSyAH4b7YUg2dGNUIT465ZVmHeG4Di5GFQ1BIBVkEnT+mJeSg78SnEWgbfZBSe7pGkRT/4wD9axWHrl1XtK5OC2k7KjT94Euww0vSEXmhIeuCsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wJLDgjMLTokeHkckqzo4yYPO32diihUdkZYjhmJw6oQ=; b=FnvoBPtcCLo02VkIlJ26qtw4PALmimrlNET1ARlB/BIwhuDcTGCGam1MH1pBm/XiDGN+YQZ3EY8fMTd7m0icxP3wDanj4P2OxQMjzG6IjD0EBYPnPpjr+fJadpKfx+1UdIvNgsWVuckC362e5D0f79es7fO+q4rYyMgkTED2fL5a0ZXr6Jl5cLuFK5Nqxa3cp9mH9a/PDxK7s253DIH20FlihOkVBkulvMRqBWeP1I+C303ZD1g4JRFSxKbO9Qfp6a8sROnj84DzX0F6LjfIgAAXkghuU9xmTY4kXj3toAt2yxlvZpq5NH85iKuexuDBc7Kict8WMU2KIIzlp1Maew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wJLDgjMLTokeHkckqzo4yYPO32diihUdkZYjhmJw6oQ=; b=d/yvKm3z8jZM7RBr3myJGE9KFNHTB1elnD2djgzuCDBPMI8sVCdNGqnbqWus9Qw5RHKVG43BDmPObCYmFyXEiFfHXI5BY5ZwXKPMSqx0+AuZDgzRR4QXGNPaRH3QpaMTHCxOLuI16kRUt366Nx0+ClE00Qo/1pkYBNgqzPVWIkRsrhOaNKLenhgknMXaXp8qyCM3rCfuFD8WdDyj4ppfuHh6qjROmVunze/bshT5Zsu2WRegwypRDj3++EQ+xEzLEh8W9C09SEtQjKOYmvlX/cKOHRJNEc6bb5+K3blVeCrGHPfj1PgzmTyZnFaXadihyefvjHw0FFrv9ALMcVOEdw== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by AS4P250MB0413.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:4c0::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.28; Tue, 26 Sep 2023 22:16:23 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::9d42:7b4f:71ef:c76a]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::9d42:7b4f:71ef:c76a%3]) with mapi id 15.20.6813.027; Tue, 26 Sep 2023 22:16:23 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Sep 2023 00:16:33 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [CI11ZyYy2+SkMlGFODCJdwZ5/KD5SQmZ] X-ClientProxiedBy: FR3P281CA0019.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:1c::6) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20230926221732.3711507-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|AS4P250MB0413:EE_ X-MS-Office365-Filtering-Correlation-Id: 96e1be6e-e90d-4a51-d1b8-08dbbede37d6 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3Gxc65e57i7IQapYTSSochAO3aT9giZfdRWmK/eRf8DT7JwwOYLlh6cXS5lJCBdVJwMPfuX0NDsU76oCKIs/h2YRjGpAeyI6+HBk5kMkjiI3yxJv2PdRf1nPqeXeKS6CXZ2Lp5FbUcqpnPik6XS0HB6Hz2USBhHQPdPnQa+UqoIsQUOuwPDq01sbFiSsJQgeqYhaunnqQDWX4aa4/xxuANAnB2ueqGUPrGVPsaN0U6IvckvDpxe53gTKnrd0+mWGvh9aoduaOymC9XvnMXeF+m0CCp1aPGz2wHmIV+eCtlM1h9e9+tBMd/hpELFn+Zumu+Mf42OFZIWQBivB1+OgwBoiwNIGAtByap0uFFyPVuxr7EUhMyL/eRNczuBFhy6/AIF9vxGP7B9rayW6cLpY5rM+D0mlJKdXEPrtFMM5RYt502U8zAWZnZj3TGA05gHOO0DG6CqM8fbbwaEhBGd+W5if9l8AeqJWgp6xyRgjl750w57INZHN9OADgzF8OQW+XY/9x/nXAs4vYdkxk3ywUiajKYsn2pBtKieenmWKyUnY5FbjzgntOIBuxQEBwUmBjAdwscxMjTuNk4GdtkfBWhSvyzdHZE48wQHIOQuaXaw1UgoJsf4SMG95NYbOJgLy X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Pwwa1d1gtx/3T+wGFIlLKxIvyrHO0qRIqAc++Om4Nc18loEkzw48XVPaBFKM/vHCk11OC47/EZUpIeRiLEm6OpSICS9DxKgnJcUtIbDBFDJwabbnbwB/ARo3ffMOYH64BYSC4l77KKflFoy7UkgAtkg5xeI9ZA6isJeOL46blrXIAq699+MONdqGDNw02j047OK61qofIpTA0osAs4XU3rP81xU/mAfEYd300rN+yMTzMzLLLYnw2iLdKHCe65rQjzBw9OAp2gdllFYiviv91u/9NRpnWaDCcEe+awDhxj3JUJxd/WfibhI5eguj9Sz1/UCjN0TNhl957ArLU6IpyhzpqZf2mnAYVWnn9yhhPgyqNI4uhRN6FAt+/k9NfniMqg086f6NAryuE3Xzvria/VVvY5zVxym752a+gCn09fKhb9eQK8U/X8uLEwj1bLiao4gZlCJReRaS9/Iti+RmvHVkB3WE2f51cisWMYF3QDHH+INeV59uiBFhsFcpo3YHtEd4esW3XAaxfOHxQY6bUcusrFNmQSyD1jBn9S2un0ah+A25+q66JCXdTQyg0HH6AJOEf3ZHTEI2WeT9cE+hOOGfx4GOpRNot4FDNNmQG6klwYhVugV7U7dUqOHQOM4tX8m3qATHCshe7f577FsoTz44PPZV3KAkNnh0dAYZORoD9Rp2ELjroGIA7ZvLN9HIgsJaykL1yVIgRwVD4hrVYYMATDyuWZYFbJpfXc7EiFPhlWjMUwHXm4P0sFfLE8AJ2ZVBdrkGHJcRLgFuZsjAopXzUVwuLZ4wWfXo7armTmMfEyqLNE84+/ZeHvFShN1HtviHEIjGYyrakgWiLNKU1cNxYnQiVfDdHSlRDz5famiXh6nWUovUKXu+e/4JPT8pIFXw7QgQhUd4JGNHWzRy2hPUyejEkTdnnmrJPfWCChkgrkh8RJ5svm/Es/7j+ZiPwq1CNaWqNpCEQbMEPHZtozyCYDOEKsJVMta1Dwiso+66fiDCU0YQgAYpY64NfsiZgjVYsmsNUMeYBc4DozvbsQMHywEw61acmZB8GVRzaWCLx4ePOFRHQ6svWIer8J79drg34RhaCjx2vNGzXHvBHeDyAmgZoZx8P1ou3AdhBghDWoEytiVlkPUQ07SxNbVnw70Cw5Nb7Qk8sqby5vZDf7P+mixPdB9O/d+SuzgFVWnbTOBnTthgaSpoa8NiHPxsGRaUhpB9UWaHNLx/3X2FLokajfutUYFG1clZgIJ0WZTajjp+Pkrzg8LrXPv5wF/1 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 96e1be6e-e90d-4a51-d1b8-08dbbede37d6 X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Sep 2023 22:16:23.4382 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS4P250MB0413 Subject: [FFmpeg-devel] [PATCH 02/61] avcodec/vp3: Make VLC tables static where possible 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: tVY8CAV9AVVk This is especially important for frame-threaded decoders like this one, because up until now each thread had an identical copy of all these VLC tables. Signed-off-by: Andreas Rheinhardt --- libavcodec/vp3.c | 162 +++++++++++++++++++++++------------------------ 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index 98dabfc907..9199f53397 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -38,6 +38,7 @@ #include "libavutil/emms.h" #include "libavutil/imgutils.h" #include "libavutil/mem_internal.h" +#include "libavutil/thread.h" #include "avcodec.h" #include "codec_internal.h" @@ -158,6 +159,18 @@ static const uint8_t vp4_pred_block_type_map[8] = { [MODE_INTER_FOURMV] = VP4_DC_INTER, }; +static VLCElem superblock_run_length_vlc[88]; /* version < 2 */ +static VLCElem fragment_run_length_vlc[56]; /* version < 2 */ +static VLCElem motion_vector_vlc[112]; /* version < 2 */ + +// The VP4 tables reuse this vlc. +static VLCElem mode_code_vlc[18 + 2084 * CONFIG_VP4_DECODER]; + +#if CONFIG_VP4_DECODER +static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */ +static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */ +#endif + typedef struct { int dc; int type; @@ -280,13 +293,6 @@ typedef struct Vp3DecodeContext { the others are four groups of 16 VLCs each for ac coefficients. */ VLC coeff_vlc[5 * 16]; - VLC superblock_run_length_vlc; /* version < 2 */ - VLC fragment_run_length_vlc; /* version < 2 */ - VLC block_pattern_vlc[2]; /* version >= 2*/ - VLC mode_code_vlc; - VLC motion_vector_vlc; /* version < 2 */ - VLC vp4_mv_vlc[2][7]; /* version >=2 */ - /* these arrays need to be on 16-byte boundaries since SSE2 operations * index into them */ DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane] @@ -362,17 +368,6 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx) for (int i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) ff_vlc_free(&s->coeff_vlc[i]); - ff_vlc_free(&s->superblock_run_length_vlc); - ff_vlc_free(&s->fragment_run_length_vlc); - ff_vlc_free(&s->mode_code_vlc); - ff_vlc_free(&s->motion_vector_vlc); - - for (int j = 0; j < 2; j++) - for (int i = 0; i < 7; i++) - ff_vlc_free(&s->vp4_mv_vlc[j][i]); - - for (int i = 0; i < 2; i++) - ff_vlc_free(&s->block_pattern_vlc[i]); return 0; } @@ -493,7 +488,7 @@ static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) else bit ^= 1; - current_run = get_vlc2(gb, s->superblock_run_length_vlc.table, + current_run = get_vlc2(gb, superblock_run_length_vlc, SUPERBLOCK_VLC_BITS, 2); if (current_run == 34) current_run += get_bits(gb, 12); @@ -527,7 +522,7 @@ static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) else bit ^= 1; - current_run = get_vlc2(gb, s->superblock_run_length_vlc.table, + current_run = get_vlc2(gb, superblock_run_length_vlc, SUPERBLOCK_VLC_BITS, 2); if (current_run == 34) current_run += get_bits(gb, 12); @@ -607,7 +602,7 @@ static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) * that cares about the fragment coding runs */ if (current_run-- == 0) { bit ^= 1; - current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2); + current_run = get_vlc2(gb, fragment_run_length_vlc, 5, 2); } coded = bit; } @@ -684,9 +679,9 @@ static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb) return v; } -static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table) +static int vp4_get_block_pattern(GetBitContext *gb, int *next_block_pattern_table) { - int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2); + int v = get_vlc2(gb, block_pattern_vlc[*next_block_pattern_table], 3, 2); *next_block_pattern_table = vp4_block_pattern_table_selector[v]; return v + 1; } @@ -758,7 +753,7 @@ static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb) if (mb_coded == SB_FULLY_CODED) pattern = 0xF; else if (mb_coded == SB_PARTIALLY_CODED) - pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table); + pattern = vp4_get_block_pattern(gb, &next_block_pattern_table); else pattern = 0; @@ -845,7 +840,7 @@ static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) if (scheme == 7) coding_mode = get_bits(gb, 3); else - coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; + coding_mode = alphabet[get_vlc2(gb, mode_code_vlc, 3, 3)]; s->macroblock_coding[current_macroblock] = coding_mode; for (k = 0; k < 4; k++) { @@ -886,11 +881,15 @@ static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) return 0; } -static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion) +static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion) { - int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, +#if CONFIG_VP4_DECODER + int v = get_vlc2(gb, vp4_mv_vlc_table[axis][vp4_mv_table_selector[FFABS(last_motion)]], VP4_MV_VLC_BITS, 2); return last_motion < 0 ? -v : v; +#else + return 0; +#endif } /* @@ -938,23 +937,23 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) switch (s->macroblock_coding[current_macroblock]) { case MODE_GOLDEN_MV: if (coding_mode == 2) { /* VP4 */ - last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x); - last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y); + last_gold_motion_x = motion_x[0] = vp4_get_mv(gb, 0, last_gold_motion_x); + last_gold_motion_y = motion_y[0] = vp4_get_mv(gb, 1, last_gold_motion_y); break; } /* otherwise fall through */ case MODE_INTER_PLUS_MV: /* all 6 fragments use the same motion vector */ if (coding_mode == 0) { - motion_x[0] = get_vlc2(gb, s->motion_vector_vlc.table, + motion_x[0] = get_vlc2(gb, motion_vector_vlc, VP3_MV_VLC_BITS, 2); - motion_y[0] = get_vlc2(gb, s->motion_vector_vlc.table, + motion_y[0] = get_vlc2(gb, motion_vector_vlc, VP3_MV_VLC_BITS, 2); } else if (coding_mode == 1) { motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)]; } else { /* VP4 */ - motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x); - motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y); + motion_x[0] = vp4_get_mv(gb, 0, last_motion_x); + motion_y[0] = vp4_get_mv(gb, 1, last_motion_y); } /* vector maintenance, only on MODE_INTER_PLUS_MV */ @@ -977,16 +976,16 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X; if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { if (coding_mode == 0) { - motion_x[k] = get_vlc2(gb, s->motion_vector_vlc.table, + motion_x[k] = get_vlc2(gb, motion_vector_vlc, VP3_MV_VLC_BITS, 2); - motion_y[k] = get_vlc2(gb, s->motion_vector_vlc.table, + motion_y[k] = get_vlc2(gb, motion_vector_vlc, VP3_MV_VLC_BITS, 2); } else if (coding_mode == 1) { motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; } else { /* VP4 */ - motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x); - motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y); + motion_x[k] = vp4_get_mv(gb, 0, prior_last_motion_x); + motion_y[k] = vp4_get_mv(gb, 1, prior_last_motion_y); } last_motion_x = motion_x[k]; last_motion_y = motion_y[k]; @@ -1111,7 +1110,7 @@ static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) else bit ^= 1; - run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, + run_length = get_vlc2(gb, superblock_run_length_vlc, SUPERBLOCK_VLC_BITS, 2); if (run_length == 34) run_length += get_bits(gb, 12); @@ -2258,6 +2257,48 @@ static void render_slice(Vp3DecodeContext *s, int slice) s->height - 16)); } +static av_cold void init_tables_once(void) +{ + VLCInitState state = VLC_INIT_STATE(mode_code_vlc); + + VLC_INIT_STATIC_TABLE_FROM_LENGTHS(superblock_run_length_vlc, + SUPERBLOCK_VLC_BITS, 34, + superblock_run_length_vlc_lens, 1, + NULL, 0, 0, 1, 0); + + VLC_INIT_STATIC_TABLE_FROM_LENGTHS(fragment_run_length_vlc, 5, 30, + fragment_run_length_vlc_len, 1, + NULL, 0, 0, 0, 0); + + VLC_INIT_STATIC_TABLE_FROM_LENGTHS(motion_vector_vlc, VP3_MV_VLC_BITS, 63, + &motion_vector_vlc_table[0][1], 2, + &motion_vector_vlc_table[0][0], 2, 1, + -31, 0); + + ff_vlc_init_tables_from_lengths(&state, 3, 8, + mode_code_vlc_len, 1, + NULL, 0, 0, 0, 0); + +#if CONFIG_VP4_DECODER + for (int j = 0; j < 2; j++) + for (int i = 0; i < 7; i++) { + vp4_mv_vlc_table[j][i] = + ff_vlc_init_tables_from_lengths(&state, VP4_MV_VLC_BITS, 63, + &vp4_mv_vlc[j][i][0][1], 2, + &vp4_mv_vlc[j][i][0][0], 2, 1, + -31, 0); + } + + /* version >= 2 */ + for (int i = 0; i < 2; i++) { + block_pattern_vlc[i] = + ff_vlc_init_tables(&state, 3, 14, + &vp4_block_pattern_vlc[i][0][1], 2, 1, + &vp4_block_pattern_vlc[i][0][0], 2, 1, 0); + } +#endif +} + /// Allocate tables for per-frame data in Vp3DecodeContext static av_cold int allocate_tables(AVCodecContext *avctx) { @@ -2316,6 +2357,7 @@ static av_cold int init_frames(Vp3DecodeContext *s) static av_cold int vp3_decode_init(AVCodecContext *avctx) { + static AVOnce init_static_once = AV_ONCE_INIT; Vp3DecodeContext *s = avctx->priv_data; int ret; int c_width; @@ -2445,49 +2487,7 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) } } - ret = ff_vlc_init_from_lengths(&s->superblock_run_length_vlc, SUPERBLOCK_VLC_BITS, 34, - superblock_run_length_vlc_lens, 1, - NULL, 0, 0, 1, 0, avctx); - if (ret < 0) - return ret; - - ret = ff_vlc_init_from_lengths(&s->fragment_run_length_vlc, 5, 30, - fragment_run_length_vlc_len, 1, - NULL, 0, 0, 0, 0, avctx); - if (ret < 0) - return ret; - - ret = ff_vlc_init_from_lengths(&s->mode_code_vlc, 3, 8, - mode_code_vlc_len, 1, - NULL, 0, 0, 0, 0, avctx); - if (ret < 0) - return ret; - - ret = ff_vlc_init_from_lengths(&s->motion_vector_vlc, VP3_MV_VLC_BITS, 63, - &motion_vector_vlc_table[0][1], 2, - &motion_vector_vlc_table[0][0], 2, 1, - -31, 0, avctx); - if (ret < 0) - return ret; - -#if CONFIG_VP4_DECODER - for (int j = 0; j < 2; j++) - for (int i = 0; i < 7; i++) { - ret = ff_vlc_init_from_lengths(&s->vp4_mv_vlc[j][i], VP4_MV_VLC_BITS, 63, - &vp4_mv_vlc[j][i][0][1], 2, - &vp4_mv_vlc[j][i][0][0], 2, 1, -31, - 0, avctx); - if (ret < 0) - return ret; - } - - /* version >= 2 */ - for (int i = 0; i < 2; i++) - if ((ret = vlc_init(&s->block_pattern_vlc[i], 3, 14, - &vp4_block_pattern_vlc[i][0][1], 2, 1, - &vp4_block_pattern_vlc[i][0][0], 2, 1, 0)) < 0) - return ret; -#endif + ff_thread_once(&init_static_once, init_tables_once); return allocate_tables(avctx); }