Feature: histogram constructed with decimated frame (2x2 pixels centered in each 4x4)
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index 6fded67..4b31248 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -392,8 +392,6 @@
   uint16_t *mask;
   /** buffer with block pixel values */
   uint16_t *block_values;
-  /** buffer with # of occurrens of pixel values in a block */
-  uint16_t *block_hist;
   /** stride from frame, mask and c_values buffers */
   int stride;
   /** frame height for 'frame', mask and c_values buffers */
diff --git a/av1/common/debanding.c b/av1/common/debanding.c
index cb758c0..2c01cb7 100644
--- a/av1/common/debanding.c
+++ b/av1/common/debanding.c
@@ -229,8 +229,7 @@
     int mask_size = (frame_width>>CAMDA_LOG2_BLOCK_SIZE) *
                     (frame_height>>CAMDA_LOG2_BLOCK_SIZE) * sizeof(uint16_t);
     dbi->mask = aom_malloc(mask_size);
-    dbi->block_values = aom_malloc(mask_size * (CAMDA_BLOCK_MAX_COUNT+1));
-    dbi->block_hist = aom_malloc(mask_size * CAMDA_BLOCK_MAX_COUNT);
+    dbi->block_values = aom_malloc(mask_size * CAMDA_BLOCK_MAX_COUNT);
   }
 
   return use_deband;
@@ -274,32 +273,26 @@
 }
 
 
-uint16_t pixel_count_if_below_thr(DebandInfo *dbi, uint16_t *image, ptrdiff_t stride,
-                                  int mask_pos, int b_row, int b_col) {
+static inline void store_pixel_data(DebandInfo *dbi, uint16_t *image, ptrdiff_t stride,
+                                    int mask_pos, int b_row, int b_col) {
+  /* Note: assumes CAMDA_BLOCK_SIZE=4
+     4x4 block, pixels considered in histogram in X:
+            O O O O
+            O X X O
+            O X X O
+            O O O O
+  */
   const int row = b_row << CAMDA_LOG2_BLOCK_SIZE;
   const int col = b_col << CAMDA_LOG2_BLOCK_SIZE;
-  long int index = row * stride + col;
-  uint16_t num_vals = 0;
-  uint16_t *values = (uint16_t*) dbi->block_values + mask_pos * (CAMDA_BLOCK_MAX_COUNT+1);
-  uint16_t *hist = (uint16_t*)  dbi->block_hist + mask_pos * CAMDA_BLOCK_MAX_COUNT;
+  long int index = (row+1) * stride + col;
+  uint16_t *values = (uint16_t*) dbi->block_values + mask_pos * CAMDA_BLOCK_MAX_COUNT;
+  uint16_t count = 0;
 
-  for (int i=0; i<CAMDA_BLOCK_SIZE; i++, index+=stride) {
-    for (int j=0; j<CAMDA_BLOCK_SIZE; j++) {
-      uint16_t count = 0;
-      while (values[count]!=image[index+j] && values[count]!=UINT16_MAX)
-        count++;
-
-      if (count>num_vals) {
-        num_vals = count;
-        if (count==CAMDA_BLOCK_MAX_COUNT)
-          return 0;
-      }
-
-      values[count] = image[index+j];
-      hist[count]++;
+  for (int i=1; i<3; i++, index+=stride) {
+    for (int j=1; j<3; j++) {
+      values[count++] = image[index+j];
     }
   }
-  return num_vals+1;
 }
 
 void camda_get_spatial_mask(DebandInfo *dbi, int width, int height) {
@@ -318,8 +311,7 @@
 
   int mask_size = mask_height * mask_stride * sizeof(uint16_t);
   memset(mask_data, 0, mask_size);
-  memset(dbi->block_values, UINT16_MAX, mask_size * (CAMDA_BLOCK_MAX_COUNT+1));
-  memset(dbi->block_hist, 0, mask_size * CAMDA_BLOCK_MAX_COUNT);
+  memset(dbi->block_values, UINT16_MAX, mask_size * CAMDA_BLOCK_MAX_COUNT);
 
   // Initial computation: fill dp except for the last row
   for (int i = 0; i < pad_size; i++) {
@@ -380,8 +372,9 @@
           int mask_j = left >> CAMDA_LOG2_BLOCK_SIZE;
           if (result > mask_index) {
             int mask_pos = mask_i * mask_stride + mask_j;
-            mask_data[mask_pos] = pixel_count_if_below_thr(dbi, image_data, stride,
-                                                           mask_pos, mask_i, mask_j);
+            mask_data[mask_pos] = 1;
+            store_pixel_data(dbi, image_data, stride,
+                             mask_pos, mask_i, mask_j);
           }
         }
       }
@@ -390,24 +383,20 @@
 }
 
 static inline void add_hist_to_histogram(DebandInfo *dbi, uint16_t *histogram,
-                                         ptrdiff_t mask_pos, const uint16_t num_diffs,
-                                         uint16_t num_values) {
+                                         ptrdiff_t mask_pos, const uint16_t num_diffs) {
   uint16_t *hist_diff = histogram + num_diffs;
-  ptrdiff_t hist_pos = mask_pos * CAMDA_BLOCK_MAX_COUNT;
-  ptrdiff_t values_pos = hist_pos + mask_pos;
-  for (int i=0; i<num_values; i++) {
-    hist_diff[dbi->block_values[values_pos++]] += dbi->block_hist[hist_pos++];
+  ptrdiff_t values_pos = mask_pos * CAMDA_BLOCK_MAX_COUNT;
+  for (int i=0; i<CAMDA_BLOCK_MAX_COUNT; i++) {
+    hist_diff[dbi->block_values[values_pos+i]]++;
   }
 }
 
 static inline void sub_hist_to_histogram(DebandInfo *dbi, uint16_t *histogram,
-                                         ptrdiff_t mask_pos, const uint16_t num_diffs,
-                                         uint16_t num_values) {
+                                         ptrdiff_t mask_pos, const uint16_t num_diffs) {
   uint16_t *hist_diff = histogram + num_diffs;
-  ptrdiff_t hist_pos = mask_pos * CAMDA_BLOCK_MAX_COUNT;
-  ptrdiff_t values_pos = hist_pos + mask_pos;
-  for (int i=0; i<num_values; i++) {
-    hist_diff[dbi->block_values[values_pos++]] -= dbi->block_hist[hist_pos++];
+  ptrdiff_t values_pos = mask_pos * CAMDA_BLOCK_MAX_COUNT;
+  for (int i=0; i<CAMDA_BLOCK_MAX_COUNT; i++) {
+    hist_diff[dbi->block_values[values_pos+i]]--;
   }
 }
 
@@ -454,7 +443,7 @@
 
   int row = b_row << CAMDA_LOG2_BLOCK_SIZE;
   int col = b_col << CAMDA_LOG2_BLOCK_SIZE;
-  int pixels_in_window = dbi->pixels_in_window;
+  int pixels_in_window = (dbi->pixels_in_window) >> 2;
   ptrdiff_t dst_pix_r = row * dbi->dst_stride + col;
   ptrdiff_t img_pix_r = row * stride + col;
 
@@ -503,7 +492,7 @@
         if (b_row+b_i>=0 && b_row+b_i<b_height) {
           ptrdiff_t mask_pos = (b_row+b_i) * mask_stride + b_j;
           if (mask[mask_pos])
-            add_hist_to_histogram(dbi, hist, mask_pos, num_diffs, mask[mask_pos]);
+            add_hist_to_histogram(dbi, hist, mask_pos, num_diffs);
         }
 
     if (mask[b_row * mask_stride])
@@ -519,13 +508,13 @@
           if (b_col_out >= 0) {
             ptrdiff_t mask_pos = b_row_curr * mask_stride + b_col_out;
             if (mask[mask_pos])
-              sub_hist_to_histogram(dbi, hist, mask_pos, num_diffs, mask[mask_pos]);
+              sub_hist_to_histogram(dbi, hist, mask_pos, num_diffs);
           }
 
           if (b_col_in<b_width) {
             ptrdiff_t mask_pos = b_row_curr * mask_stride + b_col_in;
             if (mask[mask_pos])
-              add_hist_to_histogram(dbi, hist, mask_pos, num_diffs, mask[mask_pos]);
+              add_hist_to_histogram(dbi, hist, mask_pos, num_diffs);
           }
         }
       }
@@ -577,6 +566,5 @@
     aom_free(dbi->buffers.c_values_histograms);
   } else {
     aom_free(dbi->block_values);
-    aom_free(dbi->block_hist);
   }
 }