diff mbox series

[FFmpeg-devel,06/11] lavc/jpeg2000: Switch Jpeg2000TgtNode to int32_t parent

Message ID c7d8ac509d481d20814443530f5d3c8f96bc8019.camel@haerdin.se
State New
Headers show
Series [FFmpeg-devel,01/11] lavc/jpeg2000dec: Finer granularity threading | expand

Checks

Context Check Description
andriy/configure_x86 warning Failed to apply patch

Commit Message

Tomas Härdin Sept. 28, 2022, 10:07 a.m. UTC

diff mbox series

Patch

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