From 9a03154f4657f62ef21901a9117910eb569626b9 Mon Sep 17 00:00:00 2001
From: Guillaume Martres <gmartres@google.com>
Date: Thu, 10 Oct 2013 11:03:36 -0700
Subject: [PATCH] Make the static_segmentation feature work again

Change-Id: I766c4b74db526efa4ff6dd2d95ef3e0beb45b6e5
---
 vp9/encoder/vp9_mbgraph.c | 22 +++++++++-------------
 vp9/encoder/vp9_onyx_if.c |  7 ++++---
 2 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/vp9/encoder/vp9_mbgraph.c b/vp9/encoder/vp9_mbgraph.c
index f83fcc5315..e0c24a956f 100644
--- a/vp9/encoder/vp9_mbgraph.c
+++ b/vp9/encoder/vp9_mbgraph.c
@@ -312,6 +312,7 @@ static void update_mbgraph_frame_stats(VP9_COMP *cpi,
 static void separate_arf_mbs(VP9_COMP *cpi) {
   VP9_COMMON *const cm = &cpi->common;
   int mb_col, mb_row, offset, i;
+  int mi_row, mi_col;
   int ncnt[4] = { 0 };
   int n_frames = cpi->mbgraph_n_frames;
 
@@ -348,22 +349,17 @@ static void separate_arf_mbs(VP9_COMP *cpi) {
     }
   }
 
-  for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
-       offset += cm->mb_cols, mb_row++) {
-    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
+  // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
+  // of bound access in segmentation_map
+  for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
+    for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
       // If any of the blocks in the sequence failed then the MB
       // goes in segment 0
-      if (arf_not_zz[offset + mb_col]) {
+      if (arf_not_zz[mi_row/2*cm->mb_cols + mi_col/2]) {
         ncnt[0]++;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col] = 0;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + 1] = 0;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + cm->mi_cols] = 0;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + cm->mi_cols + 1] = 0;
+        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
       } else {
-        cpi->segmentation_map[offset * 4 + 2 * mb_col] = 1;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + 1] = 1;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + cm->mi_cols] = 1;
-        cpi->segmentation_map[offset * 4 + 2 * mb_col + cm->mi_cols + 1] = 1;
+        cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
         ncnt[1]++;
       }
     }
@@ -374,7 +370,7 @@ static void separate_arf_mbs(VP9_COMP *cpi) {
   if (1) {
     // Note % of blocks that are marked as static
     if (cm->MBs)
-      cpi->static_mb_pct = (ncnt[1] * 100) / cm->MBs;
+      cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
 
     // This error case should not be reachable as this function should
     // never be called with the common data structure uninitialized.
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index 5ac26c06c3..74388d6c3b 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -445,8 +445,8 @@ static void configure_static_seg_features(VP9_COMP *cpi) {
 
       // Skip all MBs if high Q (0,0 mv and skip coeffs)
       if (high_q) {
-          vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
-          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
+        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
+        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
       }
       // Enable data update
       seg->update_data = 1;
@@ -2912,7 +2912,8 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi,
 
   // Set various flags etc to special state if it is a key frame.
   if (frame_is_intra_only(cm)) {
-    // Reset the loop filter deltas and segmentation map
+    vp9_setup_key_frame(cpi);
+    // Reset the loop filter deltas and segmentation map.
     setup_features(cm);
 
     // If segmentation is enabled force a map update for key frames.
-- 
GitLab