From patchwork Mon Aug 31 21:09:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22033 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 B4D3A44B06F for ; Tue, 1 Sep 2020 00:09:48 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9817668AC1E; Tue, 1 Sep 2020 00:09:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 91457687F68 for ; Tue, 1 Sep 2020 00:09:42 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id b79so741307wmb.4 for ; Mon, 31 Aug 2020 14:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Ktdf7cUINraBfUxhXsRW13pRJjAzDPWvwDU8RDnsaQA=; b=IbmG0SNGL9H1Zl8IZhD4NYsiW4h9RQ6wo/lcaeMjKSFU+jUB4l23941PYEJy8Ywh3E e0X+0IVSbZwVzgET2eTUjdoSwrzwczR9loHZK7UGfDtxK0FcrX/dkOPV2/cxdd/N0eC7 xgLaDfRMysEgC27ofa5yZpWj4NSp0KpGCpZs1YPzH1S+7gRu1mBRegQ+fnXuoijRcm2q KVwpjLszFHOIQZVDPA9soRqc1nq5F4Oe2EtrgZKQoc75ZlkzH6lGRqU2+T3XOPIc7XOf chX6kEMgwKV2QM2afPg1sTjRvwejjrHBNYhcpDEykeFAyhsyQg5bNq0HUCo/uqz9Ww+R hoDw== 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:mime-version :content-transfer-encoding; bh=Ktdf7cUINraBfUxhXsRW13pRJjAzDPWvwDU8RDnsaQA=; b=hQw6SGFq8nK9G00zcXfc4hCXYATlC8AUU2GnxcPN8E3Pzz35NXoKJVqUCu67iEjKzO iZS7aO3jHpgCZZkl4MHRWsxF1YKCJSh1KZ4XnBbu1+qNWmUwjmJlvQ1uhH0CeTN2Q9z+ y4qZHvVLaYHXnGkMDo9HKVADBOsLY0y+PG9gzfdih2CvJzfLdZj8d9sH02N1XwnpF7Um DDXs7Tjrox+oE6xvF6EajVR4LGy/ChIxxA2NJKjvJVOsppXbz8DG/pPSfM/OnkCGUs/X nZ7SaU4yNjwavayAfHCUvzLo7usKaVfTDyMB3M4VvADugFKgAfsT2QwGmi6rgmJkMdcX wPRw== X-Gm-Message-State: AOAM5314xsT1uz4bIGO20X5Jq0Pv2EkePlcuwaSSeOX8dkezziF5wVWa uAB2qKxB6urhfCr/1d1vqQJogySPK2M= X-Google-Smtp-Source: ABdhPJycQuUSeXYPSVtupR7ZGGjoLEePgz4lDJUz7mpsvQa//hAkJWJPwHNvgRF3TqZTdNlW1jzQsw== X-Received: by 2002:a1c:6385:: with SMTP id x127mr1089091wmb.95.1598908181571; Mon, 31 Aug 2020 14:09:41 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.09.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:09:40 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:23 +0200 Message-Id: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/8] avcodec/magicyuv: Don't invert Huffman table symbols twice 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" When the MagicYUV decoder builds Huffman tables from an array of code lengths, it proceeds as follows: First it copies the entries of the array of lengths into an array of HuffEntries (a struct which contains a length and a symbol field); it also sets the symbol field in descending order from nb_elem - 1 to 0, where nb_elem is the common number of elements of the length and HuffEntry arrays. Then the HuffEntry array is sorted lexicographically: a > b iff a.len > b.len or a.len == b.len and a.sym > b.sym. Afterwards the symbols of the so sorted array are inverted again (i.e. each symbol sym is replaced by nb_elem - sym). Yet inverting can easily be avoided altogether: Just modify the order so that smaller symbols correspond to bigger HuffEntries. This leads to the same permutation as the current code does and given that the two inversions just cancel each other out, the result is the same. Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index a255f82d9c..32d744add2 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -79,19 +79,19 @@ typedef struct MagicYUVContext { static int huff_cmp_len(const void *a, const void *b) { const HuffEntry *aa = a, *bb = b; - return (aa->len - bb->len) * 256 + aa->sym - bb->sym; + return (aa->len - bb->len) * 256 + bb->sym - aa->sym; } static int huff_cmp_len10(const void *a, const void *b) { const HuffEntry *aa = a, *bb = b; - return (aa->len - bb->len) * 1024 + aa->sym - bb->sym; + return (aa->len - bb->len) * 1024 + bb->sym - aa->sym; } static int huff_cmp_len12(const void *a, const void *b) { const HuffEntry *aa = a, *bb = b; - return (aa->len - bb->len) * 4096 + aa->sym - bb->sym; + return (aa->len - bb->len) * 4096 + bb->sym - aa->sym; } static int huff_build10(VLC *vlc, uint8_t *len) @@ -104,7 +104,7 @@ static int huff_build10(VLC *vlc, uint8_t *len) int i; for (i = 0; i < 1024; i++) { - he[i].sym = 1023 - i; + he[i].sym = i; he[i].len = len[i]; if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; @@ -115,7 +115,7 @@ static int huff_build10(VLC *vlc, uint8_t *len) for (i = 1023; i >= 0; i--) { codes[i] = code >> (32 - he[i].len); bits[i] = he[i].len; - syms[i] = 1023 - he[i].sym; + syms[i] = he[i].sym; code += 0x80000000u >> (he[i].len - 1); } @@ -136,7 +136,7 @@ static int huff_build12(VLC *vlc, uint8_t *len) int i; for (i = 0; i < 4096; i++) { - he[i].sym = 4095 - i; + he[i].sym = i; he[i].len = len[i]; if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; @@ -147,7 +147,7 @@ static int huff_build12(VLC *vlc, uint8_t *len) for (i = 4095; i >= 0; i--) { codes[i] = code >> (32 - he[i].len); bits[i] = he[i].len; - syms[i] = 4095 - he[i].sym; + syms[i] = he[i].sym; code += 0x80000000u >> (he[i].len - 1); } @@ -168,7 +168,7 @@ static int huff_build(VLC *vlc, uint8_t *len) int i; for (i = 0; i < 256; i++) { - he[i].sym = 255 - i; + he[i].sym = i; he[i].len = len[i]; if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; @@ -179,7 +179,7 @@ static int huff_build(VLC *vlc, uint8_t *len) for (i = 255; i >= 0; i--) { codes[i] = code >> (32 - he[i].len); bits[i] = he[i].len; - syms[i] = 255 - he[i].sym; + syms[i] = he[i].sym; code += 0x80000000u >> (he[i].len - 1); } From patchwork Mon Aug 31 21:09:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22034 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 D2E5B44B3A0 for ; Tue, 1 Sep 2020 00:10:08 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B6C4968ACD0; Tue, 1 Sep 2020 00:10:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 919C1687F68 for ; Tue, 1 Sep 2020 00:10:02 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id a65so774881wme.5 for ; Mon, 31 Aug 2020 14:10:02 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=ti7bqAmwrUCT7lci8xUEtMJ7f3TZdYjWaIjnCDGhbNI=; b=UloVFGlNk4QCsyKJe3MBWR1fBPhmwPKnI2Mw+PvFjvZUE6BYpNZLDXLOsF1ullZkJK xOvcU6KaPQHNuBC8MjEBWqo2VUKpx+pYEY4gl+VZvnOdbMwFlH/v3Few8717J+vp/Pe3 RE+oyuPBfJ3rWw+SL60GFmR+oFRtskcIwnX3BrT+6YlYEm6lYOypCkAw8Ql/KKfb4Suv q9LUucL5rXkUaSLH27b6c+4Mi7Z0MAKcJ7gJu1k6emI1BCF+Z61+x2YBJOeNFo0oKeL1 KGJJFjukDa1Ghbl3bwjSrTDXYhAlTu3+5zyV9mXDGFEKXX3disX7aP84UxFS/jlx8i5p Sjqw== 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:mime-version:content-transfer-encoding; bh=ti7bqAmwrUCT7lci8xUEtMJ7f3TZdYjWaIjnCDGhbNI=; b=OC/TkkJ+d3Yx3m1TTaggzBE3MUyf74pEUdrbcdI2Q3NypkvNq5j50S9MPgISFQF1Lb Ac/muxydRDi5BibTqfOTCAs2j08lQurF/ir7Mwa4yWaQxoAP/3IMatIY2FtT1oyu8Dmj BIvZ2neejx5GCYAIk8WcbBw8LoOsnCJl9Wfsrbph4uBht0bStfobfrTf4WF21xdBiCcD 2EjPV4Btfd4n4UqWFmMr2W92CAJsA3IbVu3HO85kjhITE7n+bFkKyEVVUvMQgmcLdtQV u9qveE/0tclbb+7byQAwkOd2RP0HViLV7z/HK0ov0P81FRXE7wjbwi3GsDZIhU1lIxpr p5ug== X-Gm-Message-State: AOAM532Mgc1At4k25FPTTaUFiBOBUgVkdXcl6LtNQSs5R6Pg4vPg2zyW N20IXBfcqU3in400y1q0nUMndmJZveE= X-Google-Smtp-Source: ABdhPJyOwkrAO0PnR5msfJDCCv8IdZKdth44MbVVSbRODOGOXhhEKZ07TN9myZQEwkUUueGyX0cLrg== X-Received: by 2002:a1c:7912:: with SMTP id l18mr1044234wme.124.1598908201609; Mon, 31 Aug 2020 14:10:01 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:01 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:24 +0200 Message-Id: <20200831210930.18900-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/8] avcodec/magicyuv: Avoid copying values around pointlessly 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" When parsing Huffman tables, an array of HuffEntries (a struct containing a code's bitlength, its bits and its symbol) is used as intermediate tables in order to sort the entries (the order depends on both the length of the entries as well on their symbols). After ordering them, the symbol and len components are copied into other arrays (the HuffEntries' code has never been set or used, despite using quite a lot of stack space) and the codes are generated. Afterwards, the VLC is created. Yet ff_init_vlc_sparse() can handle non-continuous arrays as input; there is no need to copy the entries at all. This commit implements this. Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index 32d744add2..f630464162 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -97,9 +97,6 @@ static int huff_cmp_len12(const void *a, const void *b) static int huff_build10(VLC *vlc, uint8_t *len) { HuffEntry he[1024]; - uint32_t codes[1024]; - uint8_t bits[1024]; - uint16_t syms[1024]; uint32_t code; int i; @@ -113,25 +110,20 @@ static int huff_build10(VLC *vlc, uint8_t *len) code = 1; for (i = 1023; i >= 0; i--) { - codes[i] = code >> (32 - he[i].len); - bits[i] = he[i].len; - syms[i] = he[i].sym; + he[i].code = code >> (32 - he[i].len); code += 0x80000000u >> (he[i].len - 1); } ff_free_vlc(vlc); return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024, - bits, sizeof(*bits), sizeof(*bits), - codes, sizeof(*codes), sizeof(*codes), - syms, sizeof(*syms), sizeof(*syms), 0); + &he[0].len, sizeof(he[0]), sizeof(he[0].len), + &he[0].code, sizeof(he[0]), sizeof(he[0].code), + &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); } static int huff_build12(VLC *vlc, uint8_t *len) { HuffEntry he[4096]; - uint32_t codes[4096]; - uint8_t bits[4096]; - uint16_t syms[4096]; uint32_t code; int i; @@ -145,25 +137,20 @@ static int huff_build12(VLC *vlc, uint8_t *len) code = 1; for (i = 4095; i >= 0; i--) { - codes[i] = code >> (32 - he[i].len); - bits[i] = he[i].len; - syms[i] = he[i].sym; + he[i].code = code >> (32 - he[i].len); code += 0x80000000u >> (he[i].len - 1); } ff_free_vlc(vlc); return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 12), 4096, - bits, sizeof(*bits), sizeof(*bits), - codes, sizeof(*codes), sizeof(*codes), - syms, sizeof(*syms), sizeof(*syms), 0); + &he[0].len, sizeof(he[0]), sizeof(he[0].len), + &he[0].code, sizeof(he[0]), sizeof(he[0].code), + &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); } static int huff_build(VLC *vlc, uint8_t *len) { HuffEntry he[256]; - uint32_t codes[256]; - uint8_t bits[256]; - uint8_t syms[256]; uint32_t code; int i; @@ -177,17 +164,15 @@ static int huff_build(VLC *vlc, uint8_t *len) code = 1; for (i = 255; i >= 0; i--) { - codes[i] = code >> (32 - he[i].len); - bits[i] = he[i].len; - syms[i] = he[i].sym; + he[i].code = code >> (32 - he[i].len); code += 0x80000000u >> (he[i].len - 1); } ff_free_vlc(vlc); return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256, - bits, sizeof(*bits), sizeof(*bits), - codes, sizeof(*codes), sizeof(*codes), - syms, sizeof(*syms), sizeof(*syms), 0); + &he[0].len, sizeof(he[0]), sizeof(he[0].len), + &he[0].code, sizeof(he[0]), sizeof(he[0].code), + &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); } static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1, From patchwork Mon Aug 31 21:09:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22035 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 101C544B3A0 for ; Tue, 1 Sep 2020 00:10:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EABD368AD19; Tue, 1 Sep 2020 00:10:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 79E1D68034C for ; Tue, 1 Sep 2020 00:10:03 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id b79so741965wmb.4 for ; Mon, 31 Aug 2020 14:10:03 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=PlIOZJpY8qrBFgTMR49Ofv1PtEzDM/leDqkZ3qRMjHs=; b=tacQhbl7B9PXNmd5FeGvhIGLlzxYsKgpkwC6BLPV3vE91TG/SByzi4VH9OHiS5yR5R 16j2hh5ZXxcReWJP9TvoI5z34xOMYAO6IpXoViF/vZWDKIyqcSvECdVWhdrhbabuqU/K S3AqBvkKB4rfQ+HDBwbQaGyR7mVyceVRuZHWvqFn2N1kdbhWsEO2YQy2DlWEsuoBIOfD yFbt73WU726k/ROSUlsmjjiRnqDCUnuflbIAs4hRxP03Ar+lY4jNzQuHXf+oM7OHnsiV XNElGGgykbF83md8ISuo5T3i8kKwZL5BOZRivYmwnh6H3kN1pt0sYI3I0VX+tImf1XnL 1AXw== 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:mime-version:content-transfer-encoding; bh=PlIOZJpY8qrBFgTMR49Ofv1PtEzDM/leDqkZ3qRMjHs=; b=cNbnbqE8QLuG0TvnA+e8sybEXnFEKbWCuVzwMYmvMXPjRxPjBQ4Q5AEmamM+DjYwNJ EzS7VMFFAvSIrKM9NJ42dW5Zgg28zPZW+yyUrE/wt8Kc1yOj886SNqq7z6n1YDpabikW ZTWy46Qq2WkIj/ykqCa1dyUsrfZPSOqa+FoYdT0o6UU8aNDBQRT5wUPkRCjFvBcJD57V ZYevkRWyjEHYPHsqMQUsj2eFyw3tonA0DQx/ibD2FUwHIVG3I4u+aHULZ6HKS3l6iP31 gJfZGWY2eCEmSFz2xVQ9KjDagItuZFoU5H+sgsB3TxbK8tBGm7t+mNT22NOUYCozsBxU mvcA== X-Gm-Message-State: AOAM530h5K/DXGEtdUGAzo46hrye+85WohD/LxljOHzQtY/Glpuyl0Ch C7rWuRclM33rkda8BOTT0tfOMdz+pj4= X-Google-Smtp-Source: ABdhPJw1iSwIwrIFDGaSDIqw8VaT0Q7sX5mPuY0rqysggNojkPT7iMSsyURK/eA7HGE0xdAuYCDLuQ== X-Received: by 2002:a1c:9a48:: with SMTP id c69mr999793wme.43.1598908202530; Mon, 31 Aug 2020 14:10:02 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:02 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:25 +0200 Message-Id: <20200831210930.18900-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/8] avcodec/magicyuv: Unify qsort comparison functions 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" Up until now, there were three comparison functions depending upon bitness. But they all are actually the same, namely a lexical ordering: entry a > entry b iff a.len > b.len or a.len == b.len and a.sym < b.sym. So they can be easily unified. Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index f630464162..5578374429 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -77,18 +77,6 @@ typedef struct MagicYUVContext { } MagicYUVContext; static int huff_cmp_len(const void *a, const void *b) -{ - const HuffEntry *aa = a, *bb = b; - return (aa->len - bb->len) * 256 + bb->sym - aa->sym; -} - -static int huff_cmp_len10(const void *a, const void *b) -{ - const HuffEntry *aa = a, *bb = b; - return (aa->len - bb->len) * 1024 + bb->sym - aa->sym; -} - -static int huff_cmp_len12(const void *a, const void *b) { const HuffEntry *aa = a, *bb = b; return (aa->len - bb->len) * 4096 + bb->sym - aa->sym; @@ -106,7 +94,7 @@ static int huff_build10(VLC *vlc, uint8_t *len) if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; } - AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10); + AV_QSORT(he, 1024, HuffEntry, huff_cmp_len); code = 1; for (i = 1023; i >= 0; i--) { @@ -133,7 +121,7 @@ static int huff_build12(VLC *vlc, uint8_t *len) if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; } - AV_QSORT(he, 4096, HuffEntry, huff_cmp_len12); + AV_QSORT(he, 4096, HuffEntry, huff_cmp_len); code = 1; for (i = 4095; i >= 0; i--) { From patchwork Mon Aug 31 21:09:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22037 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 1D40C44B3A0 for ; Tue, 1 Sep 2020 00:10:13 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0EB1B68AE6F; Tue, 1 Sep 2020 00:10:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6F16368AD4C for ; Tue, 1 Sep 2020 00:10:04 +0300 (EEST) Received: by mail-wr1-f49.google.com with SMTP id w5so7292822wrp.8 for ; Mon, 31 Aug 2020 14:10:04 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=+mOxBEN+0LNRS8VesF9Xg2J9SPZ/24rtXfw3o3NYxkI=; b=EP0ndiPE/VGm2NIuA5ew0xv3V+PvJkNo5cCl3Mgij/1gj7hqfKGWZV1ccO/B/53wsP +Guf3o3xadqJDWoAV94ROPZ9D07ABJm11/Kkesm/m45eGrDr2Ag0h4M1XOFqX8CjNbov 29T+vqxGYNFyzlw3WUHL2XGRF3grd0SBc+/Ry1iswzx4FbRm33A5Mg9v7ftAiRf8Sj1W ViBYpGgmCac1o+DXeIlOqVppae75RHQixlll9yqvqssuDPu+YDFeYOJdZxwuLB5KDeN+ h/LRa19dTGCGogWfTtBph1zig2vBgGzW8PD/NUDT7VB8N3RO/1f36Q67O8nAq6i0XaI2 n61w== 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:mime-version:content-transfer-encoding; bh=+mOxBEN+0LNRS8VesF9Xg2J9SPZ/24rtXfw3o3NYxkI=; b=HuAy4377dKfr29TcA4Wrn4w+VvOype/AhoM/ANeIH2vdSHm6JKKZuvKLUrqybky9vd 4GR/8EGsMqX7s1CxLSPzIdvR04uuKxCbxG813lqmOqvHSdULI97kBxYunu0cZCIKsn8L sMC341bfmaPnZyTSMlxnCi6QnP0cqT4UjWkfjhmC3hHARj7byMQOr8aAV9eqbEG7euwG V6Xa6130HXrdRUZjJhcrSnRUKyXjB8aBzbXR6pVw2puFGTgTSUc+DsGpnWqsbJ91RIWG 9Cq3beKfKyS8vsvSpQJg0y8g9sG6zZYNOtVIIyC9TZ9ChIj5obUSQjFVG18W++w+6ztS Eq/Q== X-Gm-Message-State: AOAM5323FYfzohN9ZgCnOz8zZZQSLk3uMRTya7uEK7gXe7oWs7k7Op+8 iSJ7XDxrcr0GDDgwpygcSFg78zmwNkw= X-Google-Smtp-Source: ABdhPJyIrB8BEtZpi6DYQs0kn9DUp9XtNXAK6rKhxnKBi1VIn/BOG2KBTuVTRkehTrH0U+GmtITePw== X-Received: by 2002:adf:f149:: with SMTP id y9mr3143620wro.93.1598908203448; Mon, 31 Aug 2020 14:10:03 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:02 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:26 +0200 Message-Id: <20200831210930.18900-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/8] avcodec/magicyuv: Simplify check for invalid Huffman codes 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" Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index 5578374429..f2204f3401 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -459,11 +459,15 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) int x = get_bits(gbit, 7); int l = get_bitsz(gbit, b * 8) + 1; - for (k = 0; k < l; k++) - if (j + k < max) - s->len[i][j + k] = x; + k = j + l; + if (k > max) { + av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n"); + return AVERROR_INVALIDDATA; + } + + for (; j < k; j++) + s->len[i][j] = x; - j += l; if (j == max) { j = 0; if (s->huff_build(&s->vlc[i], s->len[i])) { @@ -474,9 +478,6 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) if (i == s->planes) { break; } - } else if (j > max) { - av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n"); - return AVERROR_INVALIDDATA; } } From patchwork Mon Aug 31 21:09:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22038 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 1044B44B3A0 for ; Tue, 1 Sep 2020 00:10:14 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F141A68AF49; Tue, 1 Sep 2020 00:10:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6E5F568AD4C for ; Tue, 1 Sep 2020 00:10:05 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id a65so774966wme.5 for ; Mon, 31 Aug 2020 14:10:05 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=wyzBPhbFDHTLYqbpKfBKP6ub2kKwfSA0jIaKrZaVTr8=; b=I8HhWdIvQb0JT9Bc1NDOMPgOql3r5ULwfn/++L0ac+cCqHXLb/kjKwuFQBMGWLtZox PixJNK6NqhoXkeDwKkBOlabB7flGCGOtiQTWHzS7HeGRwcWSbbp8O7dybeS9TWAFMxmX QmaVv+PR1R79T9go0mEI6CQ9hG8VlTylvrhaxynXxZXaCxI3+0vWAFHkcOVjldGiee4U r0KtOFtw1afVfKq9c/9KRvoHZZ88NHBph9/pX8w5VbYl3u+LLHED/eWG8FP8LwTFXmN9 hhDcOlBS/LGyb75YQbhAmQO8E9QXc366uTSQoMaGlwEFkDoYIDoZLFUhmhzCQSSCAtW/ PLbQ== 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:mime-version:content-transfer-encoding; bh=wyzBPhbFDHTLYqbpKfBKP6ub2kKwfSA0jIaKrZaVTr8=; b=KHgnnBDs3DgMPtOhYJsyiyZzG2YcTCSWbkVAHPXF2aG1j00GgbEpwnwjoMMD6ZBR1h 1d4v5DWAjhO3zuNYXOW4aloJ92Qu9O6qlZTTufPdD34ZvJ1poU5aGPIYSrvPhfd5CDM5 FCkhPR3RZl4mZtCSVgtAoWViJWA8Uu5yYV5JQ6/gde0xpQuW/qY/QLVRP9PuRkYyPcKI cIG+DiuB8A75E2pc4Goxh52wL/0fguVkR1LLkJ+Zue3Iv1cObpBsZ3MSv9NRVAmSR5Mb sE9BM9NeWLNb+Bq5vrqJkBxTfZdwhE0lDTwsDtUtOZMUKF2LERuNukjsVzkLsQJp0ZT3 v5nA== X-Gm-Message-State: AOAM532DaqLFwr0UVG8qQff5tjzuYS+jyoWnzXeGp8AXibjl+m0e8fKB WLRNUhglBoS/5Uz2KKqAKr7CAO7Rl9g= X-Google-Smtp-Source: ABdhPJxhgBfv7wzhs7tIwsnDKnmA/3rFMTMSfwmmFibBKLJvv9qkw0qRdHMHddyZ65o7tvVW+Ti37g== X-Received: by 2002:a1c:ab06:: with SMTP id u6mr1067652wme.172.1598908204583; Mon, 31 Aug 2020 14:10:04 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:04 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:27 +0200 Message-Id: <20200831210930.18900-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/8] avcodec/magicyuv: Don't zero unnecessarily 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" The code already checks that exactly the expected amount of entries are read and set. Ergo it is unnecessary to zero them at the beginning. Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index f2204f3401..9282b39723 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -453,7 +453,6 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) MagicYUVContext *s = avctx->priv_data; int i = 0, j = 0, k; - memset(s->len, 0, sizeof(s->len)); while (get_bits_left(gbit) >= 8) { int b = get_bits(gbit, 1); int x = get_bits(gbit, 7); From patchwork Mon Aug 31 21:09:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22039 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 D3DF844B3A0 for ; Tue, 1 Sep 2020 00:10:14 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BDFF368AF78; Tue, 1 Sep 2020 00:10:14 +0300 (EEST) 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 79DF068AE45 for ; Tue, 1 Sep 2020 00:10:06 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id w5so7292928wrp.8 for ; Mon, 31 Aug 2020 14:10:06 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=IIIWalMoL+5IGhjDFbhPZLjRFJgISlkz01wPzyfVqPE=; b=scDKj9EZ8+wvWy2/SCHEA/Vy6+Nbb+WuFQjaslCH3u4QscVerj1Z2hr7aodLE/EJuH z1MjUc2l+eVoR5ZgN5YQOLbo2EI7J2Ae6S0K6gL9/3J4fTayznpScBkYkkmeqOHLp5QZ jJXcGQYwuZygYDijzeW9/Iw72P+M6Tn9jyfEaFM1+rOdVjapdMwfXkaQwJPU7DYTc80h WANBVtxKcluiZIE+DR3DOIlKpc/PXBMuH7L1o1fThcCPsGidQ5pAvHNtPo3wq5zQYRE9 zlAy4VQh8pr0w50uYyz7XD36nsGoofW1+EmGNyY+Wa3l2uImIH5SQE4ct+lAA5u0kCko NWdQ== 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:mime-version:content-transfer-encoding; bh=IIIWalMoL+5IGhjDFbhPZLjRFJgISlkz01wPzyfVqPE=; b=H9hzRSzaeipuxFsYy8FdJipEhyVqsC8KfDY9Jt/rgsR4MvzaNcJZFWTFovIyjscpbL QAfpgEpwTHv+uCNiB0SiiZBMTpeh3Q6TPzH8jyu84O6mJ5K2xX3xL5uYTzkxqASJS9Yi 87XHDAUG/qkV32bxtYEx/BVQwiucaH9Mvqxoy4FtF6acLCF8vMiYI/w/sajIjM+9xyDl G4hZHvcDDlRgpbAnzqkaMxuU5y7CgvENKfwZxlBe0q/GeoThv1lbx0Qo6P3JrvsYJinp rSJz3ZFgM2XDRSgKKgSkb0SXlqijXeP4pWTD3YHezjhvcPGRTyzXN5DsX2XfYHUz9ESk Uiiw== X-Gm-Message-State: AOAM532mjaf3BKMj4XU2jdkidMzGZg1fk+4JsaYNNM1fxx9gDtXUdaQo MDUw2jlNlJBzEGcn33pVuFZEUsn1hpg= X-Google-Smtp-Source: ABdhPJwIheJ0gd7JzXVzf6wi9A8g7nnj2TB1Wk46Z5LL/d3Bvt49LaC5dAmWuuOMztfE2MLmv/wYlA== X-Received: by 2002:adf:ba05:: with SMTP id o5mr3268057wrg.7.1598908205649; Mon, 31 Aug 2020 14:10:05 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:05 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:28 +0200 Message-Id: <20200831210930.18900-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/8] avcodec/magicyuv: Reuse array instead of using multiple arrays 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" The lengths of the VLC codes are implicitly contained in the VLC tables itself; apart from that they are not used lateron. So it is unnecessary to store them and the very same array can be reused to parse the Huffman table for the next plane. Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index 9282b39723..a16135e683 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -68,7 +68,7 @@ typedef struct MagicYUVContext { int vshift[4]; Slice *slices[4]; // slice bitstream positions for each plane unsigned int slices_size[4]; // slice sizes for each plane - uint8_t len[4][4096]; // table of code lengths for each plane + uint8_t len[4096]; // scratch table of code lengths VLC vlc[4]; // VLC for each plane int (*huff_build)(VLC *vlc, uint8_t *len); int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata, @@ -465,11 +465,11 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) } for (; j < k; j++) - s->len[i][j] = x; + s->len[j] = x; if (j == max) { j = 0; - if (s->huff_build(&s->vlc[i], s->len[i])) { + if (s->huff_build(&s->vlc[i], s->len)) { av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n"); return AVERROR_INVALIDDATA; } From patchwork Mon Aug 31 21:09:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22040 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 AA7C444B3A0 for ; Tue, 1 Sep 2020 00:10:15 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 953EC68AE44; Tue, 1 Sep 2020 00:10:15 +0300 (EEST) 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 9F03468034C for ; Tue, 1 Sep 2020 00:10:07 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id w13so7303492wrk.5 for ; Mon, 31 Aug 2020 14:10:07 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=CeUPH12+PiCwGKpIY2dS2OczgIZzyo85bJHuKZX2vcA=; b=m3oSgxMvVv6Zw2pFZE6DCzgFihKP8Q3VAvc8cKvEoGPdLUXWhgh330YO2pRyWydWiM G05DCydq8suwEps5z/pUkl1PZUs3E+8aP5oXYnw1OVIOKmqrEflG3jPozIbmPr9u7fmn jIAg0EmroIiwfZXCrTKelxZcUxzH6cNywjr57ik8SiaBFtWa8+R3iS5AtemdlEjDmaUV SQ7qprWu4OndOCJ9wJBX7pcwB64NGMsrypwvuBWRCe3SEkDKW9G4OI6IU8/6TL3tuVMv +kneeT2B4rvFoTtYmsZISV0zrsMpW50MvknnWZII0M2M4XQ98r/14y4imw7lOG7zrvl5 B1ZQ== 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:mime-version:content-transfer-encoding; bh=CeUPH12+PiCwGKpIY2dS2OczgIZzyo85bJHuKZX2vcA=; b=IX7MudOw8DD/xrOzY8x4Cfwfu+t0T1mRMNniUGuaS06u5oiGxJrzGzzFa0DM8N88wn XPbv6uKmZjRkQUgg33anCBSboAnlAkFfNVsAr5lppqDEFx9D7RZRg/1YS+iJ4RI8PTl6 XVK1zfEnmiR9mE5kjepsrrMcDIYtvZrp9psYo7MXWWq66kpfm3ObDqa3gJt5JhsbOVep gi7MWV0H9IjYeX2F5BnNcnyuwBJDBxPmidwZItisZoWh68FuzbadMUH89Kx4HwNJcE60 YWDxAhCbbcBeEYUBK6Fnhfn+JltmM2NbTYHjQMXHgWHdVDc2aP3X4GYtR/oSg6M239MP zRPg== X-Gm-Message-State: AOAM531JE+62Vg+cfXk6NEnSBkcXlL3GhZiBtLZEiohAdMRKa7jhVknn zD5LSXukSmcSh8i3KQRuf6JmqGJqoJk= X-Google-Smtp-Source: ABdhPJzUkE5w935vyFL9QszyCz4fbnqzPzZdP7SF4uXXfVO2UxphfafmhDGzqOBZJHLpcQ/RSJkGMQ== X-Received: by 2002:adf:f043:: with SMTP id t3mr3294050wro.263.1598908206716; Mon, 31 Aug 2020 14:10:06 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:06 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:29 +0200 Message-Id: <20200831210930.18900-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 7/8] avcodec/magicyuv: Unify creating Huffman tables 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" Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 71 ++++--------------------------------------- 1 file changed, 6 insertions(+), 65 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index a16135e683..d2656427b9 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -70,7 +70,6 @@ typedef struct MagicYUVContext { unsigned int slices_size[4]; // slice sizes for each plane uint8_t len[4096]; // scratch table of code lengths VLC vlc[4]; // VLC for each plane - int (*huff_build)(VLC *vlc, uint8_t *len); int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata, int j, int threadnr); LLVidDSPContext llviddsp; @@ -82,82 +81,28 @@ static int huff_cmp_len(const void *a, const void *b) return (aa->len - bb->len) * 4096 + bb->sym - aa->sym; } -static int huff_build10(VLC *vlc, uint8_t *len) -{ - HuffEntry he[1024]; - uint32_t code; - int i; - - for (i = 0; i < 1024; i++) { - he[i].sym = i; - he[i].len = len[i]; - if (len[i] == 0 || len[i] > 32) - return AVERROR_INVALIDDATA; - } - AV_QSORT(he, 1024, HuffEntry, huff_cmp_len); - - code = 1; - for (i = 1023; i >= 0; i--) { - he[i].code = code >> (32 - he[i].len); - code += 0x80000000u >> (he[i].len - 1); - } - - ff_free_vlc(vlc); - return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024, - &he[0].len, sizeof(he[0]), sizeof(he[0].len), - &he[0].code, sizeof(he[0]), sizeof(he[0].code), - &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); -} - -static int huff_build12(VLC *vlc, uint8_t *len) +static int huff_build(VLC *vlc, uint8_t *len, int nb_elems) { HuffEntry he[4096]; uint32_t code; int i; - for (i = 0; i < 4096; i++) { - he[i].sym = i; - he[i].len = len[i]; - if (len[i] == 0 || len[i] > 32) - return AVERROR_INVALIDDATA; - } - AV_QSORT(he, 4096, HuffEntry, huff_cmp_len); - - code = 1; - for (i = 4095; i >= 0; i--) { - he[i].code = code >> (32 - he[i].len); - code += 0x80000000u >> (he[i].len - 1); - } - - ff_free_vlc(vlc); - return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 12), 4096, - &he[0].len, sizeof(he[0]), sizeof(he[0].len), - &he[0].code, sizeof(he[0]), sizeof(he[0].code), - &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); -} - -static int huff_build(VLC *vlc, uint8_t *len) -{ - HuffEntry he[256]; - uint32_t code; - int i; - - for (i = 0; i < 256; i++) { + for (i = 0; i < nb_elems; i++) { he[i].sym = i; he[i].len = len[i]; if (len[i] == 0 || len[i] > 32) return AVERROR_INVALIDDATA; } - AV_QSORT(he, 256, HuffEntry, huff_cmp_len); + AV_QSORT(he, nb_elems, HuffEntry, huff_cmp_len); code = 1; - for (i = 255; i >= 0; i--) { + for (i = nb_elems - 1; i >= 0; i--) { he[i].code = code >> (32 - he[i].len); code += 0x80000000u >> (he[i].len - 1); } ff_free_vlc(vlc); - return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256, + return ff_init_vlc_sparse(vlc, FFMIN(he[nb_elems - 1].len, 12), nb_elems, &he[0].len, sizeof(he[0]), sizeof(he[0].len), &he[0].code, sizeof(he[0]), sizeof(he[0].code), &he[0].sym, sizeof(he[0]), sizeof(he[0].sym), 0); @@ -469,7 +414,7 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) if (j == max) { j = 0; - if (s->huff_build(&s->vlc[i], s->len)) { + if (huff_build(&s->vlc[i], s->len, max)) { av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n"); return AVERROR_INVALIDDATA; } @@ -595,10 +540,6 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data, } s->max = 1 << s->bps; s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10; - if ( s->bps == 8) - s->huff_build = huff_build; - else - s->huff_build = s->bps == 10 ? huff_build10 : huff_build12; s->planes = av_pix_fmt_count_planes(avctx->pix_fmt); bytestream2_skip(&gbyte, 1); From patchwork Mon Aug 31 21:09:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22036 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 05C2F44B3A0 for ; Tue, 1 Sep 2020 00:10:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E3C5A68AE45; Tue, 1 Sep 2020 00:10:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2ED5F68AD4C for ; Tue, 1 Sep 2020 00:10:09 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id o5so3407373wrn.13 for ; Mon, 31 Aug 2020 14:10:08 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=1cGAbG+JjXZSFjXFjji+jHBJONkzBL+F3oBrKTzYYq8=; b=GhUdJUaqySiv/mj1aBdMuRXDjdMbmHlNI30zlrlX58RdLyo4Hw8PfnxxqF8eYogoSI BGD52Sj/QujlaIIesOVfB20cZn4mCHK/VOTi9cLAvAvM0mVmrpvlz4yzz47NTVBtmltt Eftpm3vJMkbtx/811dB5ag4h2mbm2rLv86K/JUgG6YUevjxULmGcJo+WJTNH/7AAogvg leyKlefSorrn0AW3ielCHRj+3kXtctKSorHAHJVJmwj4rSwmqlDeeFMq5ob1s4nf+S4y F9IGuNDWhQ6BM3oRbgjgHzl1PALOYpdIHeBtv4qskPJS2QWsjfDTR/GLOoI54zZCP9cH vD4w== 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:mime-version:content-transfer-encoding; bh=1cGAbG+JjXZSFjXFjji+jHBJONkzBL+F3oBrKTzYYq8=; b=PoqfjgfmF0Df3djVAJFKUG5wtJNv7NZnQ6hST1gtSBSlr7bLiysXBWu/vQh3qfjh7g Y2Qo5ciYDiNCJ40ZplIOg6DT7mt0VupI2oFXT6yHT6g08zhoAN/88YlIH4/Nqaq5gn50 s8FPWzDKTOvE/ZRhzl55bjp/y/8FDSNPzBFoMBKeePe0N/SRRaEjf9x9lOLWUdhFUnak G4Re/29A9EhNvlzo5Ye5FQA3e+wMrgx8x3qO6wWO3cQxkXYHZVPLsNatEVs/3rQxzaw+ jAJC9QE4jm0SPLctKIxEFLOJmq2blXBgbaWLdVuYsetlpTGCHwaV4ZQgUbaRXIeLlx4g Qjzg== X-Gm-Message-State: AOAM530ihI0+JGFY2xqwJMEC/Is0spEjvPpSQPCsxGJ6LbwVJS/Fs9Jf w7GuRZeUF2DVRB2MS7w8L2Rb7x6oNao= X-Google-Smtp-Source: ABdhPJzVgUfcZE0h6eyBY9mhEEY1aPh8rVqPWW2Tl+s/rpOIuZvgODLnNQ6S+rT/03q4eatNUkhvEA== X-Received: by 2002:adf:9ed4:: with SMTP id b20mr3283301wrf.206.1598908207591; Mon, 31 Aug 2020 14:10:07 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id s124sm1035798wme.29.2020.08.31.14.10.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 14:10:07 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 31 Aug 2020 23:09:30 +0200 Message-Id: <20200831210930.18900-8-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> References: <20200831210930.18900-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 8/8] avcodec/magicyuv: Avoid intermediate array when parsing Huffman tables 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" Signed-off-by: Andreas Rheinhardt --- libavcodec/magicyuv.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c index d2656427b9..eed5ad1b40 100644 --- a/libavcodec/magicyuv.c +++ b/libavcodec/magicyuv.c @@ -68,7 +68,6 @@ typedef struct MagicYUVContext { int vshift[4]; Slice *slices[4]; // slice bitstream positions for each plane unsigned int slices_size[4]; // slice sizes for each plane - uint8_t len[4096]; // scratch table of code lengths VLC vlc[4]; // VLC for each plane int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata, int j, int threadnr); @@ -81,18 +80,11 @@ static int huff_cmp_len(const void *a, const void *b) return (aa->len - bb->len) * 4096 + bb->sym - aa->sym; } -static int huff_build(VLC *vlc, uint8_t *len, int nb_elems) +static int huff_build(HuffEntry he[], VLC *vlc, int nb_elems) { - HuffEntry he[4096]; uint32_t code; int i; - for (i = 0; i < nb_elems; i++) { - he[i].sym = i; - he[i].len = len[i]; - if (len[i] == 0 || len[i] > 32) - return AVERROR_INVALIDDATA; - } AV_QSORT(he, nb_elems, HuffEntry, huff_cmp_len); code = 1; @@ -396,6 +388,7 @@ static int magy_decode_slice(AVCodecContext *avctx, void *tdata, static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) { MagicYUVContext *s = avctx->priv_data; + HuffEntry he[4096]; int i = 0, j = 0, k; while (get_bits_left(gbit) >= 8) { @@ -404,17 +397,19 @@ static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max) int l = get_bitsz(gbit, b * 8) + 1; k = j + l; - if (k > max) { + if (k > max || x == 0 || x > 32) { av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n"); return AVERROR_INVALIDDATA; } - for (; j < k; j++) - s->len[j] = x; + for (; j < k; j++) { + he[j].sym = j; + he[j].len = x; + } if (j == max) { j = 0; - if (huff_build(&s->vlc[i], s->len, max)) { + if (huff_build(he, &s->vlc[i], max)) { av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n"); return AVERROR_INVALIDDATA; }