Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: "Tomas Härdin" <git@haerdin.se>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 06/11] lavc/jpeg2000: Switch Jpeg2000TgtNode to int32_t parent
Date: Wed, 28 Sep 2022 12:07:00 +0200
Message-ID: <c7d8ac509d481d20814443530f5d3c8f96bc8019.camel@haerdin.se> (raw)
In-Reply-To: <65e79fe701374868bb2f4b70ce8fd220938e2e86.camel@haerdin.se>

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: 0006-lavc-jpeg2000-Switch-Jpeg2000TgtNode-to-int32_t-pare.patch --]
[-- Type: text/x-patch, Size: 9160 bytes --]

From 807d7d315269126e7eccd0c36d7c29615cb98676 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
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


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2022-09-28 10:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-28 10:04 [FFmpeg-devel] [PATCH 01/11] lavc/jpeg2000dec: Finer granularity threading Tomas Härdin
2022-09-28 10:05 ` [FFmpeg-devel] [PATCH 02/11] lavc/jpeg2000dec: Reindent Tomas Härdin
2022-09-28 10:05 ` [FFmpeg-devel] [PATCH 03/11] lavc/jpeg2000dwt: Implement sliced transforms Tomas Härdin
2022-09-28 10:06 ` [FFmpeg-devel] [PATCH 04/11] lavc/jpeg2000dec: Thread init_tile() Tomas Härdin
2022-09-28 14:14   ` Tomas Härdin
2022-09-28 10:06 ` [FFmpeg-devel] [PATCH 05/11] lavc/jpeg2000*: Use av_realloc_array_reuse() and av_reallocz_array_reuse() to eliminate lots of allocations Tomas Härdin
2022-09-28 10:07 ` Tomas Härdin [this message]
2022-09-28 10:07 ` [FFmpeg-devel] [PATCH 07/11] lavc/jpeg2000: Speed up ff_jpeg2000_tag_tree_init() using stereotypes for sizes <= 4x4 Tomas Härdin
2022-09-28 10:07 ` [FFmpeg-devel] [PATCH 08/11] lavc/jpeg2000: Reindent Tomas Härdin
2022-09-28 10:08 ` [FFmpeg-devel] [PATCH 09/11] lavc/jpeg2000: Minimize calls to av_codec_is_encoder() Tomas Härdin
2022-09-28 10:09 ` [FFmpeg-devel] [PATCH 10/11] lavc/jpeg2000dec: Use coarser slicing for initial reslevels Tomas Härdin
2022-09-28 10:10 ` [FFmpeg-devel] [PATCH 11/11] lavc/jpeg2000dec: Component-level threading of write_frame() Tomas Härdin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c7d8ac509d481d20814443530f5d3c8f96bc8019.camel@haerdin.se \
    --to=git@haerdin.se \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git