From patchwork Thu Apr 4 19:33:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47804 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:24a8:b0:1a3:b6bb:3029 with SMTP id m40csp541163pzd; Thu, 4 Apr 2024 12:33:28 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUSqXK/ku4A5OxnqQfmpuN2+gehRrErwPiXP708ROcxspDEb7R8r9b26DHjtZScoiAJXw2LP8GMIGzQ6wDNBzMOvizuzjKQZorLxA== X-Google-Smtp-Source: AGHT+IF8ZVIy7kyLpXF38QNFOZwZq8ZJsqnRcCfqbqZkUEGR0u0Vli661hAJ08RlNBXf22Zi1noJ X-Received: by 2002:a17:906:3bd8:b0:a4a:850f:28fd with SMTP id v24-20020a1709063bd800b00a4a850f28fdmr500639ejf.29.1712259208496; Thu, 04 Apr 2024 12:33:28 -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 kh6-20020a170906f80600b00a4e90f52da9si2825530ejb.598.2024.04.04.12.33.28; Thu, 04 Apr 2024 12:33:28 -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=X2jmKtNv; 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 579C068D146; Thu, 4 Apr 2024 22:33:25 +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-he1eur04olkn2059.outbound.protection.outlook.com [40.92.73.59]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D3F9468CF79 for ; Thu, 4 Apr 2024 22:33:18 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KH9uxmY+fVPigF5gz51eg4Qj7Ko7Yf5Sv0AzchGxfAHRuSsiRrakXDMX87O0lejL0kP2PBKwP3vbZZyc2IkHS6ctyuQONbnyc0Sp1AqIjaeEmHoD8tq66gyP8ZYJeRw7XTizdVHReiD6ctMPHpAVNZoi+Jv5cJqgeYTLLbsw8xZzFhG5w+4TJAApWDVlb59GA7YO8dZRg+byv80QN97NbLxN1eZgDxveYT4xYd9/xHh6DfB4srFQWwnALQC+x/q7UqKGTN24HwLGwZTqG1JTf3w8BdlsWz1SjhBjIrtee/OW+5pToS6tdSMOa0LeKHJ9BDK356zPANf/jaDBS4xAqg== 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=Xl6BgfWwDpwbasSYVGbSP/sSQ0wayN1IG9w+H1YiWyY=; b=XOighCNrtDwhm6mCm6WPs2u79enVlsuxxrB1NpDtZNjXz8YFUrbU7ymzTbpjpN/4qoC9JhGWZNNcgSuu+xZX0jpWuDS12VwgE307xz9IoQzdWKr3aiECn7/lqj9orzvfkAL39znMC6xSKbCnasDHobShl4kn/JTPvOERNJ07fwhUZndXiN+3d8IQqHvQsnAadTh7zu+wPdqWO+Hqvr09xv4XE1v/5UGhXjndhW66cddoQox40erLnwZPiGyfNI/dSu6UQCvUaHMcWVZffc5/SpLkKnt2SsML+Up4QXdCEq3GwUafpPBtx7Fwczx09h+0/Kx1TkJ464iBpV2qpuCutQ== 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=Xl6BgfWwDpwbasSYVGbSP/sSQ0wayN1IG9w+H1YiWyY=; b=X2jmKtNv6aFnVgvYegDbAoyIHeVBqMnDd8HfvK8NdcYnRYUR4RslZCQmxebtVBARnDA/Chfg+0n87zQMHsQS5Xqdg+VRuOf2AlgIvithZ/pLr8gS7VHSnd7wFKML+vHzXY81OvDvnDgWgjDWcftQ3SAt9zaDh6uPWMdKdBR5KRUkg7wChlFSCF3VLVrdypZWuCstXn33qlUj/OXitTymwQ7uxbq7ewBQkixeOQ5zevh2QXPDObuRBKs4M6e85YY9BwKVKIWESAN/cL8xbxCBmLDoWQqlfeqWeM2EhXhNa9lcEms0xGT0gcsIRjJ5dywUnSEpB/YGSxaFmsXfLLsZ0w== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by DU2P250MB0148.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:275::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Thu, 4 Apr 2024 19:33:16 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d%4]) with mapi id 15.20.7409.042; Thu, 4 Apr 2024 19:33:16 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Apr 2024 21:33:08 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [wjJULVmJDbP4/FydtKpn/tPSBE+vt94AvcSqQw9pLyQ=] X-ClientProxiedBy: FR0P281CA0181.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:ab::8) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240404193309.3969296-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|DU2P250MB0148:EE_ X-MS-Office365-Filtering-Correlation-Id: 8e46b5fd-eddf-4e57-82f8-08dc54de136a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JO+af2lMIYU1WFGiTN42uSZNuEqdlrO/oK554FVQ/ZKF5AMYp/sC2F5VXrLK2OpNEI1YZBMmUjUUfDWw7WcDEBJJj0WCjdnSEw7ZcNNPIOqH+p0XeeQzgPfGSaja/fMgaBy9FQSv/6Whzf810DF01GoF+AX27d5QM7AYNsiwcTMEdKzvrfNo98O/XFc+h211mGsAMJTlVE2nLnrhNAk7TIVEA7essiBr6xHv0J2zlnO0j4csTFGXaGiJ5SrLRc/NEhxSkTgxkvPnWAW7d+Ex/+kBKuuHInsFquY7HUvDyrdCQdLs+DxtSvKJ7BL6pu1xGR0k608/b9X/SDISJDc2gaeK6JIOSmYTApdf+Z6GNTODFapTJZP8WWt94Jr9GeMU9eL5zna9lZ6hzA38yFKnEdMqFeoxzJbDEKhbT6b3zKv7mmEuTaHWlJ5/RuuhErsgf8AfUK77dbB9T+9XWnDDZ29zbPp4HBbMfZ/8uunTR8Gpc2bhxVlciNucoEL9/0QN6exj8jZtxposmjhgJg12a9XdrZJgv8x6eM9u1zaQFEOKvGCIp33goNohQTukZ4nkc++w2N/PumkNru2YdLLMxn3Jg55vZH/XM9XluSPOXTBR+QhPWJd1zKbRe8ZXaZun X-MS-Exchange-AntiSpam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m60Ivdbak0l5t5Y2rUgXEkIqJUMx7nAFFQ8i+HDWgFrwaXIiAh8xl66Wy5qyoUxKN12MdS8mqrgn/LM0Bc+RFznDePQNlSfaSiGzBYwCPo90IaioIBtpJ/AQplU7kZ8TliNxza7hpf23ApUTvvuzZ4tKmXvUgjmQ2aMYOPC83+eRJqaMJIsVLbn8hC4Oqgyh8/1LalNr0RyW1zUrrLrcbI6yhjCwt9uFPkcAREVPjV83l8lr7LTo+wEDXTHoCyBL8vZqCX6GJgy8/2Te99m+dqg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8e46b5fd-eddf-4e57-82f8-08dc54de136a X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2024 19:33:16.6787 (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: DU2P250MB0148 Subject: [FFmpeg-devel] [PATCH v2 8/9] avcodec/huffyuvdec: Use bytestream API for byte-aligned reads 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: Pqlfj5umrRSO This also allows to remove the padding from these buffers. Signed-off-by: Andreas Rheinhardt --- libavcodec/huffyuvdec.c | 53 ++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/libavcodec/huffyuvdec.c b/libavcodec/huffyuvdec.c index 12ecfcb933..e35d55c8ad 100644 --- a/libavcodec/huffyuvdec.c +++ b/libavcodec/huffyuvdec.c @@ -36,6 +36,7 @@ #include "avcodec.h" #include "bswapdsp.h" +#include "bytestream.h" #include "codec_internal.h" #include "get_bits.h" #include "huffyuv.h" @@ -86,21 +87,17 @@ typedef struct HYuvDecContext { } HYuvDecContext; -#define classic_shift_luma_table_size 42 -static const unsigned char classic_shift_luma[classic_shift_luma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = { +static const uint8_t classic_shift_luma[] = { 34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10, 14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8, - 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0, - 0,0,0,0,0,0,0,0, + 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, }; -#define classic_shift_chroma_table_size 59 -static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = { +static const uint8_t classic_shift_chroma[] = { 66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83, 84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141, 57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79, - 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0, - 0,0,0,0,0,0,0,0, + 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, }; static const unsigned char classic_add_luma[256] = { @@ -141,23 +138,30 @@ static const unsigned char classic_add_chroma[256] = { 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2, }; -static int read_len_table(uint8_t *dst, GetBitContext *gb, int n) +static int read_len_table(uint8_t *dst, GetByteContext *gb, int n) { int i, val, repeat; for (i = 0; i < n;) { - repeat = get_bits(gb, 3); - val = get_bits(gb, 5); - if (repeat == 0) - repeat = get_bits(gb, 8); - if (i + repeat > n || get_bits_left(gb) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n"); - return AVERROR_INVALIDDATA; + if (bytestream2_get_bytes_left(gb) <= 0) + goto error; + repeat = bytestream2_peek_byteu(gb) >> 5; + val = bytestream2_get_byteu(gb) & 0x1F; + if (repeat == 0) { + if (bytestream2_get_bytes_left(gb) <= 0) + goto error; + repeat = bytestream2_get_byteu(gb); } + if (i + repeat > n) + goto error; while (repeat--) dst[i++] = val; } return 0; + +error: + av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n"); + return AVERROR_INVALIDDATA; } static int generate_joint_tables(HYuvDecContext *s) @@ -253,12 +257,11 @@ out: static int read_huffman_tables(HYuvDecContext *s, const uint8_t *src, int length) { - GetBitContext gb; + GetByteContext gb; int i, ret; int count = 3; - if ((ret = init_get_bits(&gb, src, length * 8)) < 0) - return ret; + bytestream2_init(&gb, src, length); if (s->version > 2) count = 1 + s->alpha + 2*s->chroma; @@ -277,21 +280,21 @@ static int read_huffman_tables(HYuvDecContext *s, const uint8_t *src, int length if ((ret = generate_joint_tables(s)) < 0) return ret; - return (get_bits_count(&gb) + 7) / 8; + return bytestream2_tell(&gb); } static int read_old_huffman_tables(HYuvDecContext *s) { - GetBitContext gb; + GetByteContext gb; int i, ret; - init_get_bits(&gb, classic_shift_luma, - classic_shift_luma_table_size * 8); + bytestream2_init(&gb, classic_shift_luma, + sizeof(classic_shift_luma)); if ((ret = read_len_table(s->len[0], &gb, 256)) < 0) return ret; - init_get_bits(&gb, classic_shift_chroma, - classic_shift_chroma_table_size * 8); + bytestream2_init(&gb, classic_shift_chroma, + sizeof(classic_shift_chroma)); if ((ret = read_len_table(s->len[1], &gb, 256)) < 0) return ret;