[FFmpeg-cvslog] Deobfuscate ff_huff_build_tree.

Reimar Döffinger git at videolan.org
Mon Jan 30 18:39:31 CET 2012


ffmpeg | branch: master | Reimar Döffinger <Reimar.Doeffinger at gmx.de> | Sun Jan 29 21:23:59 2012 +0100| [6166bf3cbe99d687b15fc5a84bd6d399d6bab3fa] | committer: Reimar Döffinger

Deobfuscate ff_huff_build_tree.

I have no idea what the idea was behind the original code,
but the new code is equivalent to it.
In that loop that places the new node nodes[j] contains
always the data of the new node (since the steps are always
in order: FFSWAP copies node[j] to node[j-1], j is decremented).
Thus nodes[j].no == i and nodes[j].sym == HNODE.
make fate still passes and contains VP6 samples which use
FF_HUFFMAN_FLAG_HNODE_FIRST.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=6166bf3cbe99d687b15fc5a84bd6d399d6bab3fa
---

 libavcodec/huffman.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/libavcodec/huffman.c b/libavcodec/huffman.c
index 7b33bdd..42f2885 100644
--- a/libavcodec/huffman.c
+++ b/libavcodec/huffman.c
@@ -90,15 +90,14 @@ int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes,
     cur_node = nb_codes;
     nodes[nb_codes*2-1].count = 0;
     for(i = 0; i < nb_codes*2-1; i += 2){
+        uint32_t cur_count = nodes[i].count + nodes[i+1].count;
         nodes[cur_node].sym = HNODE;
-        nodes[cur_node].count = nodes[i].count + nodes[i+1].count;
+        nodes[cur_node].count = cur_count;
         nodes[cur_node].n0 = i;
-        for(j = cur_node; j > 0; j--){
-            if(nodes[j].count > nodes[j-1].count ||
-               (nodes[j].count == nodes[j-1].count &&
-                (!(flags & FF_HUFFMAN_FLAG_HNODE_FIRST) ||
-                 nodes[j].n0==j-1 || nodes[j].n0==j-2 ||
-                 (nodes[j].sym!=HNODE && nodes[j-1].sym!=HNODE))))
+        for(j = cur_node; j > i + 2; j--){
+            if(cur_count > nodes[j-1].count ||
+               (cur_count == nodes[j-1].count &&
+                !(flags & FF_HUFFMAN_FLAG_HNODE_FIRST)))
                 break;
             FFSWAP(Node, nodes[j], nodes[j-1]);
         }



More information about the ffmpeg-cvslog mailing list