From patchwork Tue Jun 14 14:43:13 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: 36227 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1115002pzb; Tue, 14 Jun 2022 07:43:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmfmnVXz2TcpMf4IoozDZD+yiYBNOmwbTfijD/TJhr+KCTsz+9+rTqBaEdpaOYP1mVzrDe X-Received: by 2002:a17:906:73d2:b0:716:14a4:43d with SMTP id n18-20020a17090673d200b0071614a4043dmr4538094ejl.434.1655217802319; Tue, 14 Jun 2022 07:43:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655217802; cv=none; d=google.com; s=arc-20160816; b=PcMLJCMe3HT7Mhmc4YtZfgKBra9minqveLfgTKJv7V3yktwnKfut4lyH2uBi3vz/wa 8f9RLOq0USk+MGaHa9CEe1TC0Z1A3X554ga2g3pq2GYQK3rww25W8ovui+cChOO/Bidt vgr8/ScDM7HEinlsQswf6q89JZT6y+FA4gZCCsQo6wQRz/qx9W/I8RO9ir2OLMlopAJ3 SRzsNO7mZJrl2ZqdSKdXsq4UwadNGFxlxMSLH8AstUvUdljXE452kdwNAaW5h6WYMWcx mrgZYSwnRTfbOr9jM5Hgn3h7ARb6VsooJ2AU/puJwDFf+MteYeDmwAlxdp/y1icOlUws bklg== 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:dkim-signature:dkim-signature:delivered-to; bh=WI8iyXbfQjTiS7jN0Rq2Dr/xXKdy6u0sWSucH63UbU4=; b=LJMlb1ZRpiNIthNqInisA6Ul7sD7Ync3unHK2+hejzYquDqhqU0VHJydv51medaek3 8HqHNz9Rh84nYzFQjSj4eqBfbiacdNmrg6arh+kPdVxfBfaqNsEm5FLvtRr1ueeykNGn yw6kSrFXpDlZS2Z8xPiY6Vq4cYLtg6+dV+81ieL1Skv6SPRQjya+x940jub/TtaY5xT2 6BbEy/O1WCa7WZt8fWcKMQ4dGMdgB3Fz/Dg+z7xtqfvHcvqTzJV1SRjZVJm5Rk0DSuRi jR3LzYhQcAKan5IO0TN3WkwmREK8lp+jlk08i5YGnWtYfjUBW2hgplC/7tQQx3AF4eOS yXEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=AMgLLKuZ; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=AMgLLKuZ; 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=NONE dis=NONE) header.from=acc.umu.se Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l23-20020aa7c3d7000000b0043348e6ef79si10814717edr.296.2022.06.14.07.43.20; Tue, 14 Jun 2022 07:43:22 -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=@acc.umu.se header.s=mail1 header.b=AMgLLKuZ; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=AMgLLKuZ; 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=NONE dis=NONE) header.from=acc.umu.se Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 814E168B646; Tue, 14 Jun 2022 17:43:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.acc.umu.se (mail.acc.umu.se [130.239.18.156]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 616DB68B67B for ; Tue, 14 Jun 2022 17:43:15 +0300 (EEST) Received: from localhost (localhost.localdomain [127.0.0.1]) by amavisd-new (Postfix) with ESMTP id DB82644DCB for ; Tue, 14 Jun 2022 16:43:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1655217794; bh=wSUFQnLVZUmuIsT4PlghYZ1GjWFlw9BZXyHXkgMhva4=; h=Subject:From:To:Date:In-Reply-To:References:From; b=AMgLLKuZDZadZ1M/CBrmdhkzUqu7Pv79z4oQHoy6YeuedSmCz75CZpwta1JAxfBR1 zCVgSa+Dp583p3VsBMnvdM6YM2GilwQNGpyO1h2Uus457wkThC+M9a5vIubLslOcvp NGIPVokAxeNdFQppsigJnibD0X8iLHwyUCYYE4Md4jqgt/WawNCMDCrGkq4yI1nt6v sdOnKGlgg8S9uM8WOXOI7r5udbddGbihd+DBpfkg3h9Ff5Y2P9sh0M0Q6ozOzNyXtW Ax8Wa69u2qF3hbT0LgFunpiRXGGUSTFDG/h3mn/sUu70Dgtb2pcD07Zfk8LVf4VAL9 WWauyEp2g/Q4g== Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: tjoppen) by mail.acc.umu.se (Postfix) with ESMTPSA id 36FF844DC6 for ; Tue, 14 Jun 2022 16:43:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1655217794; bh=wSUFQnLVZUmuIsT4PlghYZ1GjWFlw9BZXyHXkgMhva4=; h=Subject:From:To:Date:In-Reply-To:References:From; b=AMgLLKuZDZadZ1M/CBrmdhkzUqu7Pv79z4oQHoy6YeuedSmCz75CZpwta1JAxfBR1 zCVgSa+Dp583p3VsBMnvdM6YM2GilwQNGpyO1h2Uus457wkThC+M9a5vIubLslOcvp NGIPVokAxeNdFQppsigJnibD0X8iLHwyUCYYE4Md4jqgt/WawNCMDCrGkq4yI1nt6v sdOnKGlgg8S9uM8WOXOI7r5udbddGbihd+DBpfkg3h9Ff5Y2P9sh0M0Q6ozOzNyXtW Ax8Wa69u2qF3hbT0LgFunpiRXGGUSTFDG/h3mn/sUu70Dgtb2pcD07Zfk8LVf4VAL9 WWauyEp2g/Q4g== Message-ID: <521787bdce69bf8faf7cf1cd8c7470c4f69307ac.camel@acc.umu.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Tue, 14 Jun 2022 16:43:13 +0200 In-Reply-To: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se> References: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se> User-Agent: Evolution 3.38.3-1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/13] 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: W3tR2V54Ih3r From c0e00cf03f5a1fcffc90395d4b26607e1681690c 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 08/13] 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 4de596ffa9..a3d8144acc 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,14 +286,14 @@ 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) { int lev = 0; - 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; lev++; } } @@ -814,7 +814,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; @@ -823,7 +823,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); } } } @@ -877,7 +877,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); } @@ -887,7 +887,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 7ec5986875..0bec2e187d 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 (ff_jpeg2000_tag_tree_init(&prec->cblkincl, @@ -332,10 +336,6 @@ static int init_prec(AVCodecContext *avctx, prec->nb_codeblocks_height)) return AVERROR(ENOMEM); - 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 (ff_fast_recalloc(&prec->cblk, &prec->cblk_size, nb_codeblocks, sizeof(*prec->cblk))) return AVERROR(ENOMEM); diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 3bf85a6669..1fd9d193e7 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -126,10 +126,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 a3fc05ea97..a2b9f0166b 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; @@ -1161,7 +1161,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) @@ -1169,7 +1169,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