From bb25083d65c4892c0f2fb585588b0c52c1c11b11 Mon Sep 17 00:00:00 2001
From: Yaowu Xu <yaowu@google.com>
Date: Tue, 8 May 2012 12:38:39 -0700
Subject: [PATCH] Added the ability to accumulate coef stats across encodings

This commit added the ability to accumulate the coef stats across
different encodings using an intermediate binary stats files. The
accumulation happens only the binary stats file exists in current
directory. The encoder needs to be built with "ENTROPY_STATS" to
allow the output. The commit also fixed a few formating issues in
output stats file.

Change-Id: Ib1a41180aa554845cf51e4421a230b128a3a82b4
---
 vp8/encoder/bitstream.c | 34 ++++++++++++++++++--------
 vp8/encoder/onyx_if.c   |  6 ++++-
 vp8/encoder/tokenize.c  | 53 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 73 insertions(+), 20 deletions(-)

diff --git a/vp8/encoder/bitstream.c b/vp8/encoder/bitstream.c
index f44705299f..27b585302a 100644
--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -35,14 +35,14 @@ unsigned __int64 Sectionbits[500];
 int intra_mode_stats[VP8_BINTRAMODES]
                     [VP8_BINTRAMODES]
                     [VP8_BINTRAMODES];
-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};
+unsigned int tree_update_hist [BLOCK_TYPES]
+                              [COEF_BANDS]
+                              [PREV_COEF_CONTEXTS]
+                              [ENTROPY_NODES][2];
+unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
+                                  [COEF_BANDS]
+                                  [PREV_COEF_CONTEXTS]
+                                  [ENTROPY_NODES] [2];
 
 extern unsigned int active_section;
 #endif
@@ -2686,7 +2686,11 @@ void print_tree_update_probs()
     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");
+    fprintf(f, "const vp8_prob\n"
+               "vp8_coef_update_probs[BLOCK_TYPES]\n"
+               "                     [COEF_BANDS]\n"
+               "                     [PREV_COEF_CONTEXTS]\n"
+               "                     [ENTROPY_NODES] = {\n");
 
     for (i = 0; i < BLOCK_TYPES; i++)
     {
@@ -2726,7 +2730,12 @@ void print_tree_update_probs()
 
     fprintf(f, "};\n");
 
-    fprintf(f, "const vp8_prob tree_update_probs_8x8[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
+    fprintf(f, "const vp8_prob\n"
+               "vp8_coef_update_probs_8x8[BLOCK_TYPES_8X8]\n"
+               "                         [COEF_BANDS]\n"
+               "                         [PREV_COEF_CONTEXTS]\n"
+               "                         [ENTROPY_NODES] = {\n");
+
 
     for (i = 0; i < BLOCK_TYPES_8X8; i++)
     {
@@ -2764,5 +2773,10 @@ void print_tree_update_probs()
         fprintf(f, "  },\n");
     }
     fclose(f);
+    f = fopen("treeupdate.bin", "wb");
+    fwrite(tree_update_hist, sizeof(tree_update_hist), 1, f);
+    fwrite(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
+    fclose(f);
+
 }
 #endif
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index 3896d977b0..642ff64ebe 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -1742,7 +1742,8 @@ VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
     }
 
 #ifdef ENTROPY_STATS
-    init_context_counters();
+    if(cpi->pass != 1)
+        init_context_counters();
 #endif
 #ifdef MODE_STATS
     vp8_zero(y_modes);
@@ -1978,9 +1979,12 @@ void vp8_remove_compressor(VP8_PTR *ptr)
         }
 
 #ifdef ENTROPY_STATS
+    if(cpi->pass != 1)
+    {
         print_context_counters();
         print_tree_update_probs();
         print_mode_context();
+    }
 #endif
 
 #if CONFIG_INTERNAL_STATS
diff --git a/vp8/encoder/tokenize.c b/vp8/encoder/tokenize.c
index 1d55244140..4bbe99fd08 100644
--- a/vp8/encoder/tokenize.c
+++ b/vp8/encoder/tokenize.c
@@ -26,6 +26,14 @@
 #ifdef ENTROPY_STATS
 INT64 context_counters[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];
+extern unsigned int tree_update_hist [BLOCK_TYPES]
+                                     [COEF_BANDS]
+                                     [PREV_COEF_CONTEXTS]
+                                     [ENTROPY_NODES][2];
+extern unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
+                                         [COEF_BANDS]
+                                         [PREV_COEF_CONTEXTS]
+                                         [ENTROPY_NODES] [2];
 #endif
 void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
 void vp8_stuff_mb_8x8(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t) ;
@@ -614,25 +622,48 @@ void vp8_tokenize_mb(VP8_COMP *cpi, MACROBLOCKD *x, TOKENEXTRA **t)
 
 void init_context_counters(void)
 {
-    vpx_memset(context_counters, 0, sizeof(context_counters));
-    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
+    FILE *f = fopen("context.bin", "rb");
+    if(!f)
+    {
+        vpx_memset(context_counters, 0, sizeof(context_counters));
+        vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
+    }
+    else
+    {
+        fread(context_counters, sizeof(context_counters), 1, f);
+        fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
+        fclose(f);
+    }
+
+    f = fopen("treeupdate.bin", "rb");
+    if(!f)
+    {
+        vpx_memset(tree_update_hist, 0, sizeof(tree_update_hist));
+        vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
+    }
+    else
+    {
+        fread(tree_update_hist, sizeof(tree_update_hist), 1, f);
+        fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
+        fclose(f);
+    }
 }
 
 void print_context_counters()
 {
 
     int type, band, pt, t;
-
-    FILE *const f = fopen("context.c", "w");
+    FILE *f = fopen("context.c", "w");
 
     fprintf(f, "#include \"entropy.h\"\n");
-
     fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
-
-    fprintf(f, "static const unsigned int\nvp8_default_coef_counts[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
+    fprintf(f, "static const unsigned int\n"
+               "vp8_default_coef_counts[BLOCK_TYPES]\n"
+               "                      [COEF_BANDS]\n"
+               "                      [PREV_COEF_CONTEXTS]\n"
+               "                      [MAX_ENTROPY_TOKENS]={\n");
 
 # define Comma( X) (X? ",":"")
-
     type = 0;
     do
     {
@@ -641,7 +672,6 @@ void print_context_counters()
         do
         {
             fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
-
             pt = 0;
             do
             {
@@ -817,6 +847,11 @@ void print_context_counters()
     fprintf(f, "\n};\n");
 
     fclose(f);
+
+    f = fopen("context.bin", "wb");
+    fwrite(context_counters, sizeof(context_counters), 1, f);
+    fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
+    fclose(f);
 }
 
 #endif
-- 
GitLab