[FFmpeg-devel] [PATCH 2/4] libavcodec/j2kenc: Fix tag tree coding

gautamramk at gmail.com gautamramk at gmail.com
Tue Aug 18 22:40:35 EEST 2020


From: Gautam Ramakrishnan <gautamramk at gmail.com>

This patch fixes tag tree coding for JPEG2000
encoder.
---
 libavcodec/j2kenc.c   | 43 +++++++++++++++++++++++++------------------
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..1c31e48d61 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -239,30 +239,37 @@ 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 *node, int threshold, int log)
 {
     Jpeg2000TgtNode *stack[30];
-    int sp = 1, curval = 0;
-    stack[0] = node;
+    int sp = -1, curval = 0;
 
-    node = node->parent;
-    while(node){
-        if (node->vis){
-            curval = node->val;
-            break;
-        }
-        node->vis++;
-        stack[sp++] = node;
+    while(node->parent){
+        stack[++sp] = node;
         node = node->parent;
     }
-    while(--sp >= 0){
-        if (stack[sp]->val >= threshold){
-            put_bits(s, 0, threshold - curval);
-            break;
+
+    while (1) {
+        if (curval > node->temp_val)
+            node->temp_val = curval;
+        else {
+            curval = node->temp_val;
         }
-        put_bits(s, 0, stack[sp]->val - curval);
-        put_bits(s, 1, 1);
-        curval = stack[sp]->val;
+        while (curval < threshold) {
+            if (curval >= node->val) {
+                if (!node->vis) {
+                    node->vis = 1;
+                    put_bits(s, 1, 1);
+                }
+                break;
+            }
+            put_bits(s, 0, 1);
+            curval++;
+        }
+        node->temp_val = curval;
+        if (sp < 0)
+            break;
+        node = stack[sp--];
     }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 35e21f54a4..7a34a36121 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
     for (i = 0; i < siz; i++) {
         t[i].val = 0;
+        t[i].temp_val = 0;
         t[i].vis = 0;
     }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 4ea7daa5da..e9f4a51261 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
     uint8_t val;
+    uint8_t temp_val;
     uint8_t vis;
     struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1



More information about the ffmpeg-devel mailing list