From patchwork Tue Nov 10 10:48:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23547 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 672C444B3FF for ; Tue, 10 Nov 2020 13:05:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D3B2968BF52; Tue, 10 Nov 2020 12:50:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 08F5E68BEF3 for ; Tue, 10 Nov 2020 12:50:27 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id b6so810998wrt.4 for ; Tue, 10 Nov 2020 02:50:27 -0800 (PST) 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:reply-to :mime-version:content-transfer-encoding; bh=kOprLMhvsNtqRZO8Mjaf7uMzPJvrH0vxCOMJWTAQEQA=; b=YlN9+5r+aXRS7pkZpQe+vJeXyskSO+IoncyAz/jxSPlJa8MN9YOCQB5MdCu8Vrao/w zwtAsIIMjeOOXIvYVFMRRY43/RDmgO15hHhoNQ29RK2KKgQcLEkqyszwBnSLXAvcB3aQ LO3l3vlYeR4+6vKzaNCm77pXvWKTjSzXZOfTCPI9figQf7Ak6cnEP7TS0So8BQW7Vztb S0v09pb7V0zHSIhx4savpIgcdj2mVRA2EwljQYRij43yIvQFXIMTyv8x3pq/nbOUzSXT 23Wwx7PrQJcOexlIHP5UOOCKbI8dRlw9tsxjKicTxm9HiI07/cbZ9DTm2bOQz0CZtrwn sDvA== 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:reply-to:mime-version:content-transfer-encoding; bh=kOprLMhvsNtqRZO8Mjaf7uMzPJvrH0vxCOMJWTAQEQA=; b=gQK6cJ+jxNKzLGYdBDrQY0idTsRnoYsaozwLVxzzGpHiZYsYULvCn5M/xaKr9unmjL Tgn4X7Xw6ez79SGcrtW9GVzTMthj4ELIRSvtfnvotq2tVXuWTkMeaVi7Hco1dRRhxXCX NlMs2FpRNXfs7E8t2KhBVcwl/Pp1lI2wGQNVE8uJeSYAT5kbsXJAgKye+D3CqJNCpkLt a2B/xhDNA8mlRjtdBhfrcVsIJ21s0O1Lx8nIasMLcysydk//AOn3KH57t5vspJ+zK/hf Oa4SsKfxrgAVko5hIhIH++6yWoJ5tVosZYkN7MUgi1+w7N7AIS1Jw8f/Rw8xZlUuImSr ZjDw== X-Gm-Message-State: AOAM5335N3o7mIw4BLsO8Qo+eBBl8U5USwx6DQ9ElLDZirSEctPK3mFa AUr+32O9dEQXEZbmaj097H+OA//2q5g= X-Google-Smtp-Source: ABdhPJzqG2+g2uhMmITPkwNh/nI4EXui+1wuhxFLydpT0BvTCOT03c34UzTtpGWw/bWnnATi3xG0ag== X-Received: by 2002:a05:6000:345:: with SMTP id e5mr22198403wre.333.1605005427207; Tue, 10 Nov 2020 02:50:27 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id l24sm2572543wmi.7.2020.11.10.02.50.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2020 02:50:26 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Nov 2020 11:48:06 +0100 Message-Id: <20201110104851.321029-70-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201110104851.321029-1-andreas.rheinhardt@gmail.com> References: <20201110104851.321029-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 069/114] avcodec/truemotion2: Simplify creating VLC table 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" ff_init_vlc_from_lengths() can be used to offload the computation of the codes; it also allows to omit the check whether the codes are already properly ordered (they are). In this case, this also allows to avoid the allocation of the buffer for the codes. This improves performance: The amount of decicycles for one call to tm2_build_huff_tables() when decoding tm20.avi from the FATE-suite decreased from 46239 to 40035. This test consisted of looping 50 times over the file and iterating the test ten times. Signed-off-by: Andreas Rheinhardt --- libavcodec/truemotion2.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c index 0f4f345a6c..8c476a2285 100644 --- a/libavcodec/truemotion2.c +++ b/libavcodec/truemotion2.c @@ -108,15 +108,14 @@ typedef struct TM2Huff { int num; ///< current number filled int max_num; ///< total number of codes int *nums; ///< literals - uint32_t *bits; ///< codes - int *lens; ///< codelengths + uint8_t *lens; ///< codelengths } TM2Huff; /** * * @returns the length of the longest code or an AVERROR code */ -static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff) +static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff) { int ret, ret2; if (length > huff->max_bits) { @@ -134,14 +133,13 @@ static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff * return AVERROR_INVALIDDATA; } huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits); - huff->bits[huff->num] = prefix; huff->lens[huff->num] = length; huff->num++; return length; } else { /* non-terminal node */ - if ((ret2 = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0) + if ((ret2 = tm2_read_tree(ctx, length + 1, huff)) < 0) return ret2; - if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0) + if ((ret = tm2_read_tree(ctx, length + 1, huff)) < 0) return ret; } return FFMAX(ret, ret2); @@ -177,15 +175,14 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) /* allocate space for codes - it is exactly ceil(nodes / 2) entries */ huff.max_num = (huff.nodes + 1) >> 1; huff.nums = av_calloc(huff.max_num, sizeof(int)); - huff.bits = av_calloc(huff.max_num, sizeof(uint32_t)); - huff.lens = av_calloc(huff.max_num, sizeof(int)); + huff.lens = av_mallocz(huff.max_num); - if (!huff.nums || !huff.bits || !huff.lens) { + if (!huff.nums || !huff.lens) { res = AVERROR(ENOMEM); goto out; } - res = tm2_read_tree(ctx, 0, 0, &huff); + res = tm2_read_tree(ctx, 0, &huff); if (res >= 0 && res != huff.max_bits) { av_log(ctx->avctx, AV_LOG_ERROR, "Got less bits than expected: %i of %i\n", @@ -200,9 +197,9 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) /* convert codes to vlc_table */ if (res >= 0) { - res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, - huff.lens, sizeof(int), sizeof(int), - huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); + res = ff_init_vlc_from_lengths(&code->vlc, huff.max_bits, huff.max_num, + huff.lens, sizeof(huff.lens[0]), + NULL, 0, 0, 0, 0); if (res < 0) av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); else { @@ -216,7 +213,6 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) out: /* free allocated memory */ av_free(huff.nums); - av_free(huff.bits); av_free(huff.lens); return res;