shithub: libvpx

Download patch

ref: 54cf1d9ad3fe7ee3d0ce843b2a640f3fcf361887
parent: 813c6c3925186d3cc0b5fc8221d88ef2ceab878f
author: Yaowu Xu <yaowu@google.com>
date: Wed May 2 09:53:15 EDT 2012

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

--- a/vp8/common/coefupdateprobs.h
+++ b/vp8/common/coefupdateprobs.h
@@ -279,7 +279,7 @@
         },
     },
 };
-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] =
@@ -480,72 +480,6 @@
         {249, 244, 249, 255, 255, 255, 255, 255, 255, 255, 255, },
 #endif
         {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, },
       },
     },
 
--- 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 @@
         { 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},
-      }
-    }
   };
--- a/vp8/common/entropy.c
+++ b/vp8/common/entropy.c
@@ -204,7 +204,7 @@
         }
         while (++i < COEF_BANDS);
     }
-    while (++h < BLOCK_TYPES);
+    while (++h < BLOCK_TYPES_8X8);
 
 }
 
--- a/vp8/common/entropy.h
+++ b/vp8/common/entropy.h
@@ -58,6 +58,8 @@
 
 #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(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 *);
--- a/vp8/common/onyxc_int.h
+++ b/vp8/common/onyxc_int.h
@@ -49,7 +49,7 @@
     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];
--- a/vp8/decoder/decodframe.c
+++ b/vp8/decoder/decodframe.c
@@ -823,7 +823,7 @@
 
     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 @@
         {
             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 @@
     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
--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -35,9 +35,14 @@
 
 #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,88 +1388,7 @@
     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)
 {
     int i = 0;
@@ -1489,12 +1413,16 @@
                     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 @@
                         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 @@
             }
             while (++j < COEF_BANDS);
         }
-        while (++i < BLOCK_TYPES);
+        while (++i < BLOCK_TYPES_8X8);
     }
 
 }
@@ -1637,7 +1569,8 @@
                     //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 @@
 
     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 @@
 #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 @@
         /* 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 @@
             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 @@
 #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 @@
 
                         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 @@
                     }
                     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 @@
                             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 @@
 #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 @@
                             }
                         }
                         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 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 @@
 
     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");
 
--- a/vp8/encoder/block.h
+++ b/vp8/encoder/block.h
@@ -149,7 +149,7 @@
 
     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;
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3279,6 +3279,7 @@
         // 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 @@
     loopfilter_frame(cpi, cm);
 
     // build the bitstream
+    cpi->dummy_packing = 0;
     vp8_pack_bitstream(cpi, dest, size);
 
     update_reference_frames(cm);
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -87,7 +87,7 @@
 
     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 @@
     //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;
@@ -650,6 +650,8 @@
 
     // TODO Do we still need this??
     int update_context;
+
+    int dummy_packing;    /* flag to indicate if packing is dummy */
 
 } VP8_COMP;
 
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -218,26 +218,28 @@
 };
 
 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 @@
 
     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);
--- 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,45 +634,34 @@
 # 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);
@@ -679,29 +668,22 @@
     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 @@
 
         fprintf(f, "\n  }");
     }
-    while (++type < BLOCK_TYPES);
+    while (++type < BLOCK_TYPES_8X8);
 
     fprintf(f, "\n};\n");
 
@@ -747,7 +729,6 @@
 
             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 @@
                 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 @@
     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 @@
 
             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 @@
                     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 @@
 
         fprintf(f, "\n  }");
     }
-    while (++type < BLOCK_TYPES);
+    while (++type < BLOCK_TYPES_8X8);
     fprintf(f, "\n};\n");
 
     fclose(f);
--- a/vp8/encoder/tokenize.h
+++ b/vp8/encoder/tokenize.h
@@ -45,7 +45,7 @@
 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;