From patchwork Wed Sep 28 10:07:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 38424 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp98987pzh; Wed, 28 Sep 2022 03:07:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7SjiJ5XPvoBKllBZLVz0Ulh6i7Jj5+wrsm66Mr6Pr9wDcx0Ql1Y0UT8Xn49em9oWB/WoF7 X-Received: by 2002:a17:907:270b:b0:77b:17b3:f446 with SMTP id w11-20020a170907270b00b0077b17b3f446mr26099240ejk.415.1664359625605; Wed, 28 Sep 2022 03:07:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664359625; cv=none; d=google.com; s=arc-20160816; b=JseaGsh6ggwKhXhOePSBWaUI7npl7lDP0HGb1ipWryLvQ6ef3CcpaKPMunt+hUIfGJ NQ+qI1u0mFvsstbpaQywN6tNW9ZJjreNdcGmenhGPkqKIQB85jRxnIssop6ws639jQ2e IwAoxkTyQXsxKIyz32rLl0LLdTkYgEroFYLeBZ2KeM7J0QssdJeCCB5bNfwHLStp8Giz IxFxcjEbaoEXKpQxG/a/CTOVsD7M4uK+bToFpq/1SL3NF/4Ndv2gogWRzx2WOg76H/wS nLaZ/O8pM8YGP+K7Zp+VM3DmipF8uYcMIMYDIPqW1WnFwwkN/rS0z6hzuCGsCybhoIh1 XVyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:references:in-reply-to:date:to:from :message-id:delivered-to; bh=CECb1k53ACqrSCUHGusj6MO+3IyYCbIk9i2oOra+fgg=; b=gyRZXCTpxUwPU++q8iTn4FE5I0kVPt/MyWutzmp2YOnVnIosrqzTflIDc++qoZiF6x 0Ifh+52LLLuyvixpvxYGhxEvtxO4cq4f9qp2rr8lpV2yk86nwRX8hjgUAt6z7iByM+73 N9hN8ZjnPczL7vJkzXjvF/YX7+HPBOx5c57Uq9+jfGsMHRsuRWje7IpVa4sWSiwoQ5UE 6/ZXam8m0jzX7D1Yof70QSoEWC+CYVXNJxRC4g4EVsKj/4+Z07dT4xEvmVY0CDYTy5zw +acfGSgz92Ta69ER6mOnRCES1rUVwjzfy1tCyqeYo3EgMo8LZhgA4bTKcNCvf0wM6rrK BlNA== 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 xe1-20020a170907318100b0077fadd74307si3979516ejb.128.2022.09.28.03.07.05; Wed, 28 Sep 2022 03:07:05 -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 2555568BC3D; Wed, 28 Sep 2022 13:07:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.frobbit.se (mail.frobbit.se [85.30.129.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DBB6C68BB75 for ; Wed, 28 Sep 2022 13:07:01 +0300 (EEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) by mail.frobbit.se (Postfix) with ESMTPSA id 51227213E0 for ; Wed, 28 Sep 2022 12:07:01 +0200 (CEST) Message-ID: From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Wed, 28 Sep 2022 12:07:00 +0200 In-Reply-To: <65e79fe701374868bb2f4b70ce8fd220938e2e86.camel@haerdin.se> References: <65e79fe701374868bb2f4b70ce8fd220938e2e86.camel@haerdin.se> User-Agent: Evolution 3.38.3-1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/11] lavc/jpeg2000: Switch Jpeg2000TgtNode to int32_t parent 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: i755pnapcnga From 807d7d315269126e7eccd0c36d7c29615cb98676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 7 Jun 2022 16:43:40 +0200 Subject: [PATCH 06/11] lavc/jpeg2000: Switch Jpeg2000TgtNode to int32_t parent --- libavcodec/j2kenc.c | 44 ++++++++++++++++++++-------------------- libavcodec/jpeg2000.c | 20 +++++++++--------- libavcodec/jpeg2000.h | 2 +- libavcodec/jpeg2000dec.c | 18 ++++++++-------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c index cd325e94e0..8f23ddbcf6 100644 --- a/libavcodec/j2kenc.c +++ b/libavcodec/j2kenc.c @@ -249,36 +249,36 @@ static void j2k_flush(Jpeg2000EncoderContext *s) /* tag tree routines */ /** code the value stored in node */ -static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold) +static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *nodes, int32_t node, int threshold) { - Jpeg2000TgtNode *stack[30]; + int32_t stack[30]; int sp = -1, curval = 0; - while(node->parent){ + while(nodes[node].parent >= 0){ stack[++sp] = node; - node = node->parent; + node = nodes[node].parent; } while (1) { - if (curval > node->temp_val) - node->temp_val = curval; + if (curval > nodes[node].temp_val) + nodes[node].temp_val = curval; else { - curval = node->temp_val; + curval = nodes[node].temp_val; } - if (node->val >= threshold) { + if (nodes[node].val >= threshold) { put_bits(s, 0, threshold - curval); curval = threshold; } else { - put_bits(s, 0, node->val - curval); - curval = node->val; - if (!node->vis) { + put_bits(s, 0, nodes[node].val - curval); + curval = nodes[node].val; + if (!nodes[node].vis) { put_bits(s, 1, 1); - node->vis = 1; + nodes[node].vis = 1; } } - node->temp_val = curval; + nodes[node].temp_val = curval; if (sp < 0) break; node = stack[sp--]; @@ -286,13 +286,13 @@ static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int } /** update the value in node */ -static void tag_tree_update(Jpeg2000TgtNode *node) +static void tag_tree_update(Jpeg2000TgtNode *nodes, int node) { - while (node->parent){ - if (node->parent->val <= node->val) + while (nodes[node].parent >= 0){ + if (nodes[nodes[node].parent].val <= nodes[node].val) break; - node->parent->val = node->val; - node = node->parent; + nodes[nodes[node].parent].val = nodes[node].val; + node = nodes[node].parent; } } @@ -812,7 +812,7 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits; cblk->incl = 0; cblk->lblock = 3; - tag_tree_update(prec->zerobits + pos); + tag_tree_update(prec->zerobits, pos); for (i = 0; i < nlayers; i++) { if (cblk->layers[i].npasses > 0) { prec->cblkincl[pos].val = i; @@ -821,7 +821,7 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in } if (i == nlayers) prec->cblkincl[pos].val = i; - tag_tree_update(prec->cblkincl + pos); + tag_tree_update(prec->cblkincl, pos); } } } @@ -875,7 +875,7 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in // inclusion information if (!cblk->incl) - tag_tree_code(s, prec->cblkincl + pos, layno + 1); + tag_tree_code(s, prec->cblkincl, pos, layno + 1); else { put_bits(s, cblk->layers[layno].npasses > 0, 1); } @@ -885,7 +885,7 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in // zerobits information if (!cblk->incl) { - tag_tree_code(s, prec->zerobits + pos, 100); + tag_tree_code(s, prec->zerobits, pos, 100); cblk->incl = 1; } diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index afff9809e4..4ddb45bf33 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -55,8 +55,8 @@ static int32_t tag_tree_size(int w, int h) static int ff_jpeg2000_tag_tree_init(Jpeg2000TgtNode **old, unsigned int *size, int w, int h) { int pw = w, ph = h; - Jpeg2000TgtNode *t, *t2; - int32_t tt_size; + Jpeg2000TgtNode *t; + int32_t tt_size, ofs = 0; size_t prod; tt_size = tag_tree_size(w, h); @@ -77,15 +77,15 @@ static int ff_jpeg2000_tag_tree_init(Jpeg2000TgtNode **old, unsigned int *size, w = (w + 1) >> 1; h = (h + 1) >> 1; - t2 = t + pw * ph; + ofs += pw * ph; for (i = 0; i < ph; i++) for (j = 0; j < pw; j++) - t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)]; + t[i * pw + j].parent = (i >> 1) * w + (j >> 1) + ofs; - t = t2; + t += pw * ph; } - t[0].parent = NULL; + t[0].parent = -1; return 0; } @@ -320,6 +320,10 @@ static int init_prec(AVCodecContext *avctx, band->log2_cblk_height) - (prec->coord[1][0] >> band->log2_cblk_height); + /* \sum_{i=0}^\inf 4^-i = 4/3 */ + if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT32_MAX / 4 * 3) { + return AVERROR(ENOMEM); + } /* Tag trees initialization */ if ((ret = ff_jpeg2000_tag_tree_init(&prec->cblkincl, @@ -332,10 +336,6 @@ static int init_prec(AVCodecContext *avctx, prec->nb_codeblocks_height)) < 0) return ret; - if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) { - prec->cblk = NULL; - return AVERROR(ENOMEM); - } nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height; if (av_reallocz_array_reuse(&prec->cblk, &prec->cblk_allocated, nb_codeblocks, INT_MAX, sizeof(*prec->cblk))) return AVERROR(ENOMEM); diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 6594d8e5cb..c9a2e55efa 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -128,10 +128,10 @@ typedef struct Jpeg2000T1Context { } Jpeg2000T1Context; typedef struct Jpeg2000TgtNode { + int32_t parent; uint8_t val; uint8_t temp_val; uint8_t vis; - struct Jpeg2000TgtNode *parent; } Jpeg2000TgtNode; typedef struct Jpeg2000CodingStyle { diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 49a815a9b0..46f7d841b5 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -185,24 +185,24 @@ static void jpeg2000_flush(Jpeg2000DecoderContext *s) } /* decode the value stored in node */ -static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, +static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *nodes, int32_t node, int threshold) { Jpeg2000TgtNode *stack[30]; int sp = -1, curval = 0; - if (!node) { + if (node < 0) { av_log(s->avctx, AV_LOG_ERROR, "missing node\n"); return AVERROR_INVALIDDATA; } - while (node && !node->vis) { - stack[++sp] = node; - node = node->parent; + while (node >= 0 && !nodes[node].vis) { + stack[++sp] = &nodes[node]; + node = nodes[node].parent; } - if (node) - curval = node->val; + if (node >= 0) + curval = nodes[node].val; else curval = stack[sp]->val; @@ -1164,7 +1164,7 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, if (cblk->npasses) incl = get_bits(s, 1); else - incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno; + incl = tag_tree_decode(s, prec->cblkincl, cblkno, layno + 1) == layno; if (!incl) continue; else if (incl < 0) @@ -1172,7 +1172,7 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, if (!cblk->npasses) { int v = expn[bandno] + numgbits - 1 - - tag_tree_decode(s, prec->zerobits + cblkno, 100); + tag_tree_decode(s, prec->zerobits, cblkno, 100); if (v < 0 || v > 30) { av_log(s->avctx, AV_LOG_ERROR, "nonzerobits %d invalid or unsupported\n", v); -- 2.30.2