From 54cf1d9ad3fe7ee3d0ce843b2a640f3fcf361887 Mon Sep 17 00:00:00 2001
From: Yaowu Xu <yaowu@google.com>
Date: Wed, 2 May 2012 13:53:15 -0700
Subject: [PATCH] a number of fixes to entropy stats collection

1. block types
There are only three types of blocks for 8x8 transformed MBs, i.e. Y
block with DC does not exist for 8x8 transformed MBs as all MB using
8x8 transform have 2nd order haar transform. This commit introduced
a new macro BLOCK_TYPES_8X8 to reflect such fact.

2. context counters
This commit also fixed the mixed of context_counters between 4x4 and
8x8 transformed MBs. The mixed use of the counters leads me to think
the existing the context probabilities were not properly generated
from 8x8 transformed MBs.

3. redundant collecting in recoding
The commit also corrected the code that accumulates entropy stats by
making sure stats only collected for final packing, not during the
recode loop

Change-Id: I029f09f8f60bd0c3240cc392ff5c6d05435e322c
---
 vp8/common/coefupdateprobs.h   |  68 +------------
 vp8/common/defaultcoefcounts.h |  68 +------------
 vp8/common/entropy.c           |   2 +-
 vp8/common/entropy.h           |   4 +-
 vp8/common/onyxc_int.h         |   2 +-
 vp8/decoder/decodframe.c       |   6 +-
 vp8/encoder/bitstream.c        | 180 ++++++++-------------------------
 vp8/encoder/block.h            |   2 +-
 vp8/encoder/onyx_if.c          |   2 +
 vp8/encoder/onyx_int.h         |  10 +-
 vp8/encoder/rdopt.c            |  29 +++---
 vp8/encoder/tokenize.c         |  32 ++----
 vp8/encoder/tokenize.h         |   2 +-
 13 files changed, 85 insertions(+), 322 deletions(-)

diff --git a/vp8/common/coefupdateprobs.h b/vp8/common/coefupdateprobs.h
index 714dd464ae..ec63546167 100644
--- a/vp8/common/coefupdateprobs.h
+++ b/vp8/common/coefupdateprobs.h
@@ -279,7 +279,7 @@ const vp8_prob vp8_coef_update_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTE
         },
     },
 };
-const vp8_prob vp8_coef_update_probs_8x8 [BLOCK_TYPES]
+const vp8_prob vp8_coef_update_probs_8x8 [BLOCK_TYPES_8X8]
                                          [COEF_BANDS]
                                          [PREV_COEF_CONTEXTS]
                                          [ENTROPY_NODES] =
@@ -482,71 +482,5 @@ const vp8_prob vp8_coef_update_probs_8x8 [BLOCK_TYPES]
         {249, 244, 249, 255, 255, 255, 255, 255, 255, 255, 255, },
       },
     },
-    {
-      {
-        {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, },
-        {249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 239, 234, 244, 239, 244, 249, 255, 255, 255, 255, },
-#endif
-        {255, 239, 234, 244, 239, 244, 249, 255, 255, 255, 255, },
-      },
-      {
-        {255, 249, 239, 239, 244, 255, 255, 255, 255, 255, 255, },
-        {255, 249, 244, 255, 249, 255, 255, 255, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 255, 239, 255, 255, 249, 255, 255, 255, 255, 255, },
-#endif
-        {255, 255, 239, 255, 255, 249, 255, 255, 255, 255, 255, },
-      },
-      {
-        {255, 244, 239, 239, 244, 255, 255, 255, 255, 255, 255, },
-        {255, 234, 239, 234, 249, 255, 255, 255, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 255, 229, 239, 234, 249, 244, 255, 255, 255, 255, },
-#endif
-        {255, 255, 229, 239, 234, 249, 244, 255, 255, 255, 255, },
-      },
-      {
-        {255, 239, 229, 239, 234, 234, 255, 255, 255, 255, 255, },
-        {255, 239, 234, 229, 244, 239, 255, 234, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 229, 209, 229, 239, 234, 244, 229, 255, 249, 255, },
-#endif
-        {255, 229, 209, 229, 239, 234, 244, 229, 255, 249, 255, },
-      },
-      {
-        {255, 239, 234, 229, 244, 249, 255, 249, 255, 255, 255, },
-        {255, 234, 229, 244, 234, 249, 255, 249, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 229, 239, 229, 249, 255, 255, 244, 255, 255, 255, },
-#endif
-        {255, 229, 239, 229, 249, 255, 255, 244, 255, 255, 255, },
-      },
-      {
-        {255, 239, 234, 239, 234, 239, 255, 249, 255, 255, 255, },
-        {255, 229, 234, 239, 239, 239, 255, 244, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 229, 234, 239, 239, 244, 255, 255, 255, 255, 255, },
-#endif
-        {255, 229, 234, 239, 239, 244, 255, 255, 255, 255, 255, },
-      },
-      {
-        {255, 219, 224, 229, 229, 234, 239, 224, 255, 255, 255, },
-        {255, 229, 229, 224, 234, 229, 239, 239, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {255, 229, 224, 239, 234, 239, 224, 224, 255, 249, 255, },
-#endif
-        {255, 229, 224, 239, 234, 239, 224, 224, 255, 249, 255, },
-      },
-      {
-        {255, 234, 229, 244, 229, 229, 255, 214, 255, 255, 255, },
-        {255, 239, 234, 239, 214, 239, 255, 209, 255, 255, 255, },
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        {249, 239, 219, 209, 219, 224, 239, 204, 255, 255, 255, },
-#endif
-        {249, 239, 219, 209, 219, 224, 239, 204, 255, 255, 255, },
-      },
-    },
 
 };
diff --git a/vp8/common/defaultcoefcounts.h b/vp8/common/defaultcoefcounts.h
index da41e40e00..c13855d28c 100644
--- a/vp8/common/defaultcoefcounts.h
+++ b/vp8/common/defaultcoefcounts.h
@@ -10,7 +10,7 @@
 
 /* Generated file, included by entropy.c */
 
-static const unsigned int vp8_default_coef_counts_8x8[BLOCK_TYPES]
+static const unsigned int vp8_default_coef_counts_8x8[BLOCK_TYPES_8X8]
                                               [COEF_BANDS]
                                               [PREV_COEF_CONTEXTS]
                                               [MAX_ENTROPY_TOKENS] =
@@ -214,70 +214,4 @@ static const unsigned int vp8_default_coef_counts_8x8[BLOCK_TYPES]
         { 3882867, 3224489, 1052289, 252890, 46967, 8548, 154, 0, 0, 0, 0, 194354},
       }
     },
-    { /* block Type 3 */
-      { /* Coeff Band 0 */
-        { 10583, 12059, 3155, 1041, 248, 175, 24, 2, 0, 0, 0, 5717},
-        { 42461, 41782, 13553, 4966, 1352, 855, 89, 0, 0, 0, 0, 15000},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 4691125, 5045589, 2673566, 1089317, 378161, 160268, 18252, 813, 69, 13, 0, 49},
-#endif
-        { 4691125, 5045589, 2673566, 1089317, 378161, 160268, 18252, 813, 69, 13, 0, 49},
-      },
-      { /* Coeff Band 1 */
-        { 1535203, 1685686, 924565, 390329, 141709, 60523, 5983, 171, 0, 0, 0, 0},
-        { 1594021, 1793276, 1016078, 441332, 164159, 70843, 8098, 311, 0, 0, 0, 11312},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 1225223, 1430184, 888492, 460713, 203286, 115149, 22061, 804, 7, 0, 0, 0},
-#endif
-        { 1225223, 1430184, 888492, 460713, 203286, 115149, 22061, 804, 7, 0, 0, 0},
-      },
-      { /* Coeff Band 2 */
-        { 1522386, 1590366, 799910, 303691, 96625, 37608, 3637, 180, 33, 11, 0, 0},
-        { 1682184, 1793869, 913649, 353520, 113674, 46309, 4736, 221, 18, 3, 0, 963},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 1574580, 1740474, 954392, 417994, 151400, 67091, 8000, 536, 73, 10, 0, 63},
-#endif
-        { 1574580, 1740474, 954392, 417994, 151400, 67091, 8000, 536, 73, 10, 0, 63},
-      },
-      { /* Coeff Band 3 */
-        { 4963672, 5197790, 2585383, 982161, 313333, 118498, 16014, 536, 62, 0, 0, 0},
-        { 5223913, 5569803, 2845858, 1107384, 364949, 147841, 18296, 658, 11, 11, 0, 1866},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 4042207, 4548894, 2608767, 1154993, 446290, 221295, 41054, 2438, 124, 20, 0, 0},
-#endif
-        { 4042207, 4548894, 2608767, 1154993, 446290, 221295, 41054, 2438, 124, 20, 0, 0},
-      },
-      { /* Coeff Band 4 */
-        { 3857216, 4431325, 2670447, 1330169, 553301, 286825, 46763, 1917, 0, 0, 0, 0},
-        { 4226215, 4963701, 3046198, 1523923, 644670, 355519, 58792, 2525, 0, 0, 0, 1298},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 3831873, 4580350, 3018580, 1660048, 797298, 502983, 123906, 7172, 16, 0, 0, 0},
-#endif
-        { 3831873, 4580350, 3018580, 1660048, 797298, 502983, 123906, 7172, 16, 0, 0, 0},
-      },
-      { /* Coeff Band 5 */
-        { 8524543, 9285149, 4979435, 2039330, 683458, 266032, 22628, 270, 0, 0, 0, 0},
-        { 9432163, 10428088, 5715661, 2385738, 838389, 326264, 29981, 361, 0, 0, 0, 884},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 9039066, 10368964, 6136765, 2862030, 1098269, 511668, 63105, 945, 14, 0, 0, 0},
-#endif
-        { 9039066, 10368964, 6136765, 2862030, 1098269, 511668, 63105, 945, 14, 0, 0, 0},
-      },
-      { /* Coeff Band 6 */
-        { 33222872, 34748297, 17701695, 7214933, 2602336, 1191859, 187873, 12667, 390, 3, 0, 0},
-        { 34765051, 37140719, 19525578, 8268934, 3085012, 1473864, 246743, 15258, 736, 3, 0, 8403},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 28591289, 32252393, 19037068, 9213729, 4020653, 2372354, 586420, 67428, 3920, 92, 7, 3},
-#endif
-        { 28591289, 32252393, 19037068, 9213729, 4020653, 2372354, 586420, 67428, 3920, 92, 7, 3},
-      },
-      { /* Coeff Band 7 */
-        { 68604786, 60777665, 19712887, 5656955, 1520443, 507166, 51829, 2466, 10, 0, 0, 0},
-        { 55447403, 51682540, 19008774, 5928582, 1706884, 595531, 65998, 3661, 101, 0, 0, 8468343},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 28321970, 29149398, 13565882, 5258675, 1868588, 898041, 192023, 21497, 672, 17, 0, 1884921},
-#endif
-        { 28321970, 29149398, 13565882, 5258675, 1868588, 898041, 192023, 21497, 672, 17, 0, 1884921},
-      }
-    }
   };
diff --git a/vp8/common/entropy.c b/vp8/common/entropy.c
index 8c0ebf19b3..170d17640f 100644
--- a/vp8/common/entropy.c
+++ b/vp8/common/entropy.c
@@ -204,7 +204,7 @@ void vp8_default_coef_probs(VP8_COMMON *pc)
         }
         while (++i < COEF_BANDS);
     }
-    while (++h < BLOCK_TYPES);
+    while (++h < BLOCK_TYPES_8X8);
 
 }
 
diff --git a/vp8/common/entropy.h b/vp8/common/entropy.h
index 113332fede..42de3ea52e 100644
--- a/vp8/common/entropy.h
+++ b/vp8/common/entropy.h
@@ -58,6 +58,8 @@ extern vp8_extra_bit_struct vp8_extra_bits[12];    /* indexed by token value */
 
 #define BLOCK_TYPES 4
 
+#define BLOCK_TYPES_8X8 3
+
 /* Middle dimension is a coarsening of the coefficient's
    position within the 4x4 DCT. */
 
@@ -97,7 +99,7 @@ extern DECLARE_ALIGNED(64, const unsigned char, vp8_coef_bands_8x8[64]);
 extern DECLARE_ALIGNED(16, const unsigned char, vp8_prev_token_class[MAX_ENTROPY_TOKENS]);
 
 extern const vp8_prob vp8_coef_update_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
-extern const vp8_prob vp8_coef_update_probs_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
+extern const vp8_prob vp8_coef_update_probs_8x8 [BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
 
 struct VP8Common;
 void vp8_default_coef_probs(struct VP8Common *);
diff --git a/vp8/common/onyxc_int.h b/vp8/common/onyxc_int.h
index 1f46521488..f36915c51c 100644
--- a/vp8/common/onyxc_int.h
+++ b/vp8/common/onyxc_int.h
@@ -49,7 +49,7 @@ typedef struct frame_contexts
     vp8_prob uv_mode_prob [VP8_YMODES][VP8_UV_MODES-1];
     vp8_prob sub_mv_ref_prob [VP8_SUBMVREFS-1];
     vp8_prob coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
-    vp8_prob coef_probs_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
+    vp8_prob coef_probs_8x8 [BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
     MV_CONTEXT mvc[2];
 #if CONFIG_HIGH_PRECISION_MV
     MV_CONTEXT_HP mvc_hp[2];
diff --git a/vp8/decoder/decodframe.c b/vp8/decoder/decodframe.c
index 35353805b8..877653290f 100644
--- a/vp8/decoder/decodframe.c
+++ b/vp8/decoder/decodframe.c
@@ -823,7 +823,7 @@ static void read_coef_probs3(VP8D_COMP *pbi)
 
     if(pbi->common.txfm_mode == ALLOW_8X8)
     {
-        for (i = 0; i < BLOCK_TYPES; i++)
+        for (i = 0; i < BLOCK_TYPES_8X8; i++)
             for (l = 0; l < ENTROPY_NODES; l++)
             {
                 if(vp8_read(bc, grpupd))
@@ -889,7 +889,7 @@ static void read_coef_probs2(VP8D_COMP *pbi)
         {
             if(vp8_read(bc, grpupd))
             {
-                for (i = 0; i < BLOCK_TYPES; i++)
+                for (i = 0; i < BLOCK_TYPES_8X8; i++)
                     for (j = !i; j < COEF_BANDS; j++)
                         for (k = 0; k < PREV_COEF_CONTEXTS; k++)
                         {
@@ -961,7 +961,7 @@ static void read_coef_probs(VP8D_COMP *pbi)
     if(pbi->common.txfm_mode == ALLOW_8X8 && vp8_read_bit(bc))
     {
         // read coef probability tree
-        for (i = 0; i < BLOCK_TYPES; i++)
+        for (i = 0; i < BLOCK_TYPES_8X8; i++)
 #if CONFIG_NEWUPDATE
             for (j = !i; j < COEF_BANDS; j++)
 #else
diff --git a/vp8/encoder/bitstream.c b/vp8/encoder/bitstream.c
index a2bce4e4c0..a96a75e061 100644
--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -35,9 +35,14 @@ unsigned __int64 Sectionbits[500];
 
 #ifdef ENTROPY_STATS
 int intra_mode_stats[10][10][10];
-static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
-static unsigned int tree_update_hist_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
-
+static unsigned int tree_update_hist [BLOCK_TYPES]
+                                     [COEF_BANDS]
+                                     [PREV_COEF_CONTEXTS]
+                                     [ENTROPY_NODES][2]={0};
+static unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
+                                         [COEF_BANDS]
+                                         [PREV_COEF_CONTEXTS]
+                                         [ENTROPY_NODES] [2]={0};
 extern unsigned int active_section;
 #endif
 
@@ -1383,87 +1388,6 @@ static void print_prob_tree(vp8_prob
     fclose(f);
 }
 
-static void sum_probs_over_prev_coef_context(
-        const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
-        unsigned int* out)
-{
-    int i, j;
-    for (i=0; i < MAX_ENTROPY_TOKENS; ++i)
-    {
-        for (j=0; j < PREV_COEF_CONTEXTS; ++j)
-        {
-            const int tmp = out[i];
-            out[i] += probs[j][i];
-            /* check for wrap */
-            if (out[i] < tmp)
-                out[i] = UINT_MAX;
-        }
-    }
-}
-
-static int default_coef_context_savings(VP8_COMP *cpi)
-{
-    int savings = 0;
-    int i = 0;
-    do
-    {
-#if CONFIG_NEWUPDATE
-        int j = !i;
-#else
-        int j = 0;      /* token/prob index */
-#endif
-        do
-        {
-            int k = 0;
-            do
-            {
-                /* at every context */
-
-                /* calc probs and branch cts for this frame only */
-                //vp8_prob new_p           [ENTROPY_NODES];
-                //unsigned int branch_ct   [ENTROPY_NODES] [2];
-
-                int t = 0;      /* token/prob index */
-#if CONFIG_EXPANDED_COEF_CONTEXT
-                if (k >=3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
-                        continue;
-#endif
-
-                vp8_tree_probs_from_distribution(
-                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
-                    cpi->frame_coef_probs [i][j][k],
-                    cpi->frame_branch_ct [i][j][k],
-                    cpi->coef_counts [i][j][k],
-                    256, 1
-                );
-
-                do
-                {
-                    const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
-                    vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
-                    const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
-                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
-
-#if CONFIG_NEWUPDATE && defined(SEARCH_NEWP)
-                    const int s = prob_update_savings_search(ct, oldp, &newp, upd);
-#else
-                    const int s = prob_update_savings(ct, oldp, newp, upd);
-#endif
-
-                    if (s > 0)
-                    {
-                        savings += s;
-                    }
-                }
-                while (++t < ENTROPY_NODES);
-            }
-            while (++k < PREV_COEF_CONTEXTS);
-        }
-        while (++j < COEF_BANDS);
-    }
-    while (++i < BLOCK_TYPES);
-    return savings;
-}
 
 void build_coeff_contexts(VP8_COMP *cpi)
 {
@@ -1489,12 +1413,16 @@ void build_coeff_contexts(VP8_COMP *cpi)
                     256, 1
                 );
 #ifdef ENTROPY_STATS
-                t = 0;
-                do
+                if(!cpi->dummy_packing)
                 {
-                    context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
+                    t = 0;
+                    do
+                    {
+                        context_counters [i][j][k][t] +=
+                            cpi->coef_counts [i][j][k][t];
+                    }
+                    while (++t < MAX_ENTROPY_TOKENS);
                 }
-                while (++t < MAX_ENTROPY_TOKENS);
 #endif
             }
             while (++k < PREV_COEF_CONTEXTS);
@@ -1532,12 +1460,16 @@ void build_coeff_contexts(VP8_COMP *cpi)
                         256, 1
                         );
 #ifdef ENTROPY_STATS
-                    t = 0;
-                    do
+                    if(!cpi->dummy_packing)
                     {
-                        context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
+                        t = 0;
+                        do
+                        {
+                            context_counters_8x8 [i][j][k][t] +=
+                                cpi->coef_counts_8x8 [i][j][k][t];
+                        }
+                        while (++t < MAX_ENTROPY_TOKENS);
                     }
-                    while (++t < MAX_ENTROPY_TOKENS);
 #endif
 
                 }
@@ -1545,7 +1477,7 @@ void build_coeff_contexts(VP8_COMP *cpi)
             }
             while (++j < COEF_BANDS);
         }
-        while (++i < BLOCK_TYPES);
+        while (++i < BLOCK_TYPES_8X8);
     }
 
 }
@@ -1637,7 +1569,8 @@ static void update_coef_probs3(VP8_COMP *cpi)
                     //printf("  %d %d %d: %d (%d)\n", i, j, k, u, upd);
                     vp8_write(w, u, upd);
 #ifdef ENTROPY_STATS
-                    ++ tree_update_hist [i][j][k][t] [u];
+                    if(!cpi->dummy_packing)
+                        ++ tree_update_hist [i][j][k][t] [u];
 #endif
                     if (u)
                     { /* send/use new probability */
@@ -1653,7 +1586,7 @@ static void update_coef_probs3(VP8_COMP *cpi)
 
     if(cpi->common.txfm_mode != ALLOW_8X8) return;
 
-    for (i = 0; i < BLOCK_TYPES; ++i)
+    for (i = 0; i < BLOCK_TYPES_8X8; ++i)
     {
         for (t = 0; t < ENTROPY_NODES; ++t)
         {
@@ -1731,7 +1664,8 @@ static void update_coef_probs3(VP8_COMP *cpi)
 #endif
                     vp8_write(w, u, upd);
 #ifdef ENTROPY_STATS
-                    ++ tree_update_hist_8x8 [i][j][k][t] [u];
+                    if(!cpi->dummy_packing)
+                        ++ tree_update_hist_8x8 [i][j][k][t] [u];
 #endif
                     if (u)
                     {
@@ -1853,7 +1787,7 @@ static void update_coef_probs2(VP8_COMP *cpi)
         /* dry run to see if there is any udpate at all needed */
         savings = 0;
         update[0] = update[1] = 0;
-        for (i = 0; i < BLOCK_TYPES; ++i)
+        for (i = 0; i < BLOCK_TYPES_8X8; ++i)
         {
             for (j = !i; j < COEF_BANDS; ++j)
             {
@@ -1897,7 +1831,7 @@ static void update_coef_probs2(VP8_COMP *cpi)
             continue;
         }
         vp8_write(w, 1, grpupd);
-        for (i = 0; i < BLOCK_TYPES; ++i)
+        for (i = 0; i < BLOCK_TYPES_8X8; ++i)
         {
             for (j = !i; j < COEF_BANDS; ++j)
             {
@@ -1927,7 +1861,8 @@ static void update_coef_probs2(VP8_COMP *cpi)
 #endif
                         vp8_write(w, u, upd);
 #ifdef ENTROPY_STATS
-                        ++ tree_update_hist_8x8 [i][j][k][t] [u];
+                        if(!cpi->dummy_packing)
+                            ++ tree_update_hist_8x8 [i][j][k][t] [u];
 #endif
                         if (u)
                         {
@@ -2073,7 +2008,8 @@ static void update_coef_probs(VP8_COMP *cpi)
 
                         vp8_write(w, u, upd);
 #ifdef ENTROPY_STATS
-                        ++ tree_update_hist [i][j][k][t] [u];
+                        if(!cpi->dummy_packing)
+                            ++ tree_update_hist [i][j][k][t] [u];
 #endif
                         if (u)
                         {
@@ -2090,15 +2026,6 @@ static void update_coef_probs(VP8_COMP *cpi)
                     }
                     while (++t < ENTROPY_NODES);
 
-                    // Accum token counts for generation of default statistics
-#if 0//def ENTROPY_STATS
-                    t = 0;
-                    do
-                    {
-                        context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
-                    }
-                    while (++t < MAX_ENTROPY_TOKENS);
-#endif
                 }
                 while (++k < PREV_COEF_CONTEXTS);
             }
@@ -2154,30 +2081,15 @@ static void update_coef_probs(VP8_COMP *cpi)
                             savings += s;
 #endif
 
-#ifdef ENTROPY_STATS
-                        ++ tree_update_hist_8x8 [i][j][k][t] [u];
-#endif
                         update[u]++;
                     }
                     while (++t < MAX_ENTROPY_TOKENS - 1);
-
-                    // Accum token counts for generation of default statistics
-#if 0//def ENTROPY_STATS
-                    t = 0;
-
-                    do
-                    {
-                        context_counters_8x8 [i][j][k][t] += cpi->coef_counts_8x8 [i][j][k][t];
-                    }
-                    while (++t < MAX_ENTROPY_TOKENS - 1);
-
-#endif
                 }
                 while (++k < PREV_COEF_CONTEXTS);
             }
             while (++j < COEF_BANDS);
         }
-        while (++i < BLOCK_TYPES);
+        while (++i < BLOCK_TYPES_8X8);
 
 #if CONFIG_NEWUPDATE
         if (update[1] == 0 || savings < 0)
@@ -2225,7 +2137,8 @@ static void update_coef_probs(VP8_COMP *cpi)
 #endif
                             vp8_write(w, u, upd);
 #ifdef ENTROPY_STATS
-                            ++ tree_update_hist_8x8 [i][j][k][t] [u];
+                            if(!cpi->dummy_packing)
+                                ++ tree_update_hist_8x8 [i][j][k][t] [u];
 #endif
                             if (u)
                             {
@@ -2240,21 +2153,12 @@ static void update_coef_probs(VP8_COMP *cpi)
                             }
                         }
                         while (++t < MAX_ENTROPY_TOKENS - 1);
-                        // Accum token counts for generation of default statistics
-#if 0//def ENTROPY_STATS
-                        t = 0;
-                        do
-                        {
-                            context_counters_8x8 [i][j][k][t] += cpi->coef_counts_8x8 [i][j][k][t];
-                        }
-                        while (++t < MAX_ENTROPY_TOKENS);
-#endif
                     }
                     while (++k < PREV_COEF_CONTEXTS);
                 }
                 while (++j < COEF_BANDS);
             }
-            while (++i < BLOCK_TYPES);
+            while (++i < BLOCK_TYPES_8X8);
         }
     }
 }
@@ -2786,7 +2690,7 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size)
 void print_tree_update_probs()
 {
     int i, j, k, l;
-    FILE *f = fopen("context.c", "a");
+    FILE *f = fopen("coefupdprob.h", "w");
     int Sum;
     fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
     fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
@@ -2831,7 +2735,7 @@ void print_tree_update_probs()
 
     fprintf(f, "const vp8_prob tree_update_probs_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
 
-    for (i = 0; i < BLOCK_TYPES; i++)
+    for (i = 0; i < BLOCK_TYPES_8X8; i++)
     {
         fprintf(f, "  { \n");
 
diff --git a/vp8/encoder/block.h b/vp8/encoder/block.h
index cca32f31ff..2b21fa2b23 100644
--- a/vp8/encoder/block.h
+++ b/vp8/encoder/block.h
@@ -149,7 +149,7 @@ typedef struct
 
     unsigned int token_costs[BLOCK_TYPES] [COEF_BANDS]
                             [PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
-    unsigned int token_costs_8x8[BLOCK_TYPES] [COEF_BANDS]
+    unsigned int token_costs_8x8[BLOCK_TYPES_8X8] [COEF_BANDS]
                             [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
 
     int optimize;
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index 128cd30199..a570e39736 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3279,6 +3279,7 @@ static void encode_frame_to_data_rate
         // accurate estimate of output frame size to determine if we need
         // to recode.
         vp8_save_coding_context(cpi);
+        cpi->dummy_packing = 1;
         vp8_pack_bitstream(cpi, dest, size);
         cpi->projected_frame_size = (*size) << 3;
         vp8_restore_coding_context(cpi);
@@ -3604,6 +3605,7 @@ static void encode_frame_to_data_rate
     loopfilter_frame(cpi, cm);
 
     // build the bitstream
+    cpi->dummy_packing = 0;
     vp8_pack_bitstream(cpi, dest, size);
 
     update_reference_frames(cm);
diff --git a/vp8/encoder/onyx_int.h b/vp8/encoder/onyx_int.h
index 279f4bad7e..7068d1abce 100644
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -87,7 +87,7 @@ typedef struct
 
     vp8_prob coef_probs[BLOCK_TYPES]
                        [COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
-    vp8_prob coef_probs_8x8[BLOCK_TYPES]
+    vp8_prob coef_probs_8x8[BLOCK_TYPES_8X8]
                            [COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
 
     vp8_prob ymode_prob [VP8_YMODES-1];   /* interframe intra mode probs */
@@ -452,9 +452,9 @@ typedef struct VP8_COMP
     //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation
     vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
     unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
-    unsigned int coef_counts_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];  /* for this frame */
-    vp8_prob frame_coef_probs_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
-    unsigned int frame_branch_ct_8x8 [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
+    unsigned int coef_counts_8x8 [BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];  /* for this frame */
+    vp8_prob frame_coef_probs_8x8 [BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES];
+    unsigned int frame_branch_ct_8x8 [BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES][2];
 
     int gfu_boost;
     int kf_boost;
@@ -651,6 +651,8 @@ typedef struct VP8_COMP
     // TODO Do we still need this??
     int update_context;
 
+    int dummy_packing;    /* flag to indicate if packing is dummy */
+
 } VP8_COMP;
 
 void control_data_rate(VP8_COMP *cpi);
diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c
index 017a9853ec..cceeb76178 100644
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -218,26 +218,28 @@ const MV_REFERENCE_FRAME vp8_second_ref_frame_order[MAX_MODES] =
 };
 
 static void fill_token_costs(
-    unsigned int c      [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS],
-    const vp8_prob p    [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES]
-)
+    unsigned int (*c)[COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS],
+    const vp8_prob (*p)[COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES],
+    int block_type_counts)
 {
     int i, j, k;
 
-
-    for (i = 0; i < BLOCK_TYPES; i++)
+    for (i = 0; i < block_type_counts; i++)
         for (j = 0; j < COEF_BANDS; j++)
             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
             {
                 if(k == 0 && ((j > 0 && i > 0) || (j > 1 && i == 0)))
-                    vp8_cost_tokens_skip((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
+                    vp8_cost_tokens_skip((int *)( c [i][j][k]),
+                                                  p [i][j][k],
+                                                  vp8_coef_tree);
                 else
-
-                    vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
+                    vp8_cost_tokens((int *)(c [i][j][k]),
+                                            p [i][j][k],
+                                            vp8_coef_tree);
             }
-
 }
 
+
 static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
                                       0,   0,   0,   0,   0,   0,   0,   0,
                                       0,   0,   0,   0,   0,   0,   0,   0,
@@ -370,13 +372,14 @@ void vp8_initialize_rd_consts(VP8_COMP *cpi, int QIndex)
 
     fill_token_costs(
         cpi->mb.token_costs,
-        (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs
-    );
+        (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs,
+        BLOCK_TYPES);
 
     fill_token_costs(
         cpi->mb.token_costs_8x8,
-        (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_8x8
-    );
+        (const vp8_prob( *)[8][PREV_COEF_CONTEXTS][11]) cpi->common.fc.coef_probs_8x8,
+        BLOCK_TYPES_8X8);
+
     /*rough estimate for costing*/
     cpi->common.kf_ymode_probs_index = cpi->common.base_qindex>>4;
     vp8_init_mode_costs(cpi);
diff --git a/vp8/encoder/tokenize.c b/vp8/encoder/tokenize.c
index e9935b0da0..1d55244140 100644
--- a/vp8/encoder/tokenize.c
+++ b/vp8/encoder/tokenize.c
@@ -25,7 +25,7 @@
 
 #ifdef ENTROPY_STATS
 INT64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
-INT64 context_counters_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
+INT64 context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
 #endif
 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
 void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
@@ -634,74 +634,56 @@ void print_context_counters()
 # define Comma( X) (X? ",":"")
 
     type = 0;
-
     do
     {
         fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
-
         band = 0;
-
         do
         {
             fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
 
             pt = 0;
-
             do
             {
                 fprintf(f, "%s\n      {", Comma(pt));
 
                 t = 0;
-
                 do
                 {
                     const INT64 x = context_counters [type] [band] [pt] [t];
                     const int y = (int) x;
-
                     assert(x == (INT64) y);  /* no overflow handling yet */
                     fprintf(f, "%s %d", Comma(t), y);
-
                 }
                 while (++t < MAX_ENTROPY_TOKENS);
-
                 fprintf(f, "}");
             }
             while (++pt < PREV_COEF_CONTEXTS);
-
             fprintf(f, "\n    }");
-
         }
         while (++band < COEF_BANDS);
-
         fprintf(f, "\n  }");
     }
     while (++type < BLOCK_TYPES);
     fprintf(f, "\n};\n");
 
     fprintf(f, "static const unsigned int\nvp8_default_coef_counts_8x8"
-            "[BLOCK_TYPES] [COEF_BANDS]"
+            "[BLOCK_TYPES_8X8] [COEF_BANDS]"
             "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
 
     type = 0;
-
     do
     {
         fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
-
         band = 0;
-
         do
         {
             fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
-
             pt = 0;
-
             do
             {
                 fprintf(f, "%s\n      {", Comma(pt));
-
                 t = 0;
-
                 do
                 {
                     const INT64 x = context_counters_8x8 [type] [band] [pt] [t];
@@ -724,7 +706,7 @@ void print_context_counters()
 
         fprintf(f, "\n  }");
     }
-    while (++type < BLOCK_TYPES);
+    while (++type < BLOCK_TYPES_8X8);
 
     fprintf(f, "\n};\n");
 
@@ -747,7 +729,6 @@ void print_context_counters()
 
             do
             {
-                fprintf(f, "%s\n      {", Comma(pt));
 
                 unsigned int branch_ct [ENTROPY_NODES] [2];
                 unsigned int coef_counts[MAX_ENTROPY_TOKENS];
@@ -757,6 +738,7 @@ void print_context_counters()
                 vp8_tree_probs_from_distribution(
                     MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
                     coef_probs, branch_ct, coef_counts, 256, 1);
+                fprintf(f, "%s\n      {", Comma(pt));
 
                 t = 0;
 
@@ -782,7 +764,7 @@ void print_context_counters()
     fprintf(f, "\n};\n");
 
     fprintf(f, "static const vp8_prob\n"
-            "vp8_default_coef_probs_8x8[BLOCK_TYPES] [COEF_BANDS]\n"
+            "vp8_default_coef_probs_8x8[BLOCK_TYPES_8X8] [COEF_BANDS]\n"
             "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {");
     type = 0;
 
@@ -800,7 +782,6 @@ void print_context_counters()
 
             do
             {
-                fprintf(f, "%s\n      {", Comma(pt));
 
                 unsigned int branch_ct [ENTROPY_NODES] [2];
                 unsigned int coef_counts[MAX_ENTROPY_TOKENS];
@@ -811,6 +792,7 @@ void print_context_counters()
                     MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
                     coef_probs, branch_ct, coef_counts, 256, 1);
 
+                fprintf(f, "%s\n      {", Comma(pt));
                 t = 0;
 
                 do
@@ -831,7 +813,7 @@ void print_context_counters()
 
         fprintf(f, "\n  }");
     }
-    while (++type < BLOCK_TYPES);
+    while (++type < BLOCK_TYPES_8X8);
     fprintf(f, "\n};\n");
 
     fclose(f);
diff --git a/vp8/encoder/tokenize.h b/vp8/encoder/tokenize.h
index 5452c6ecfc..bc3a05bd51 100644
--- a/vp8/encoder/tokenize.h
+++ b/vp8/encoder/tokenize.h
@@ -45,7 +45,7 @@ void init_context_counters();
 void print_context_counters();
 
 extern INT64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
-extern INT64 context_counters_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
+extern INT64 context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
 
 #endif
 extern const int *vp8_dct_value_cost_ptr;
-- 
GitLab