Use 7 neighbors for nz_map ctx

This will let coding performance drop slightly
lowres 0.093%

Increase encoder speed by 24%

Reduce nz_map's context size by 20%

Change-Id: I871c18a7e0341e066afc334556b9998194b3f8c9
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index ad81dfd..447ada6 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -61,13 +61,13 @@
 #if CONFIG_LV_MAP
 #define TXB_SKIP_CONTEXTS 13
 #if CONFIG_EXT_TX
-#define SIG_COEF_CONTEXTS_2D 20
-#define SIG_COEF_CONTEXTS_1D 20
+#define SIG_COEF_CONTEXTS_2D 16
+#define SIG_COEF_CONTEXTS_1D 16
 #define SIG_COEF_CONTEXTS \
   (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D + SIG_COEF_CONTEXTS_1D)
 #else
-#define SIG_COEF_CONTEXTS_2D 20
-#define SIG_COEF_CONTEXTS 20
+#define SIG_COEF_CONTEXTS_2D 16
+#define SIG_COEF_CONTEXTS 16
 #endif
 #define EOB_COEF_CONTEXTS 25
 #define COEFF_BASE_CONTEXTS 42
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 4a695bc..ad5f7ac 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -175,133 +175,178 @@
 #if CONFIG_EXT_TX
 #if CONFIG_CHROMA_2X2
   {
-      { 34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37,
+      {
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
 
-        34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37,
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
 
-        34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37 },
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
+      },
 
-      { 52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50,
+      {
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
 
-        52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50,
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
 
-        52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50 },
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
+      },
   },
 #endif
   {
-      { 34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37,
+      {
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
 
-        34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37,
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
 
-        34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37 },
+          34,  103, 61,  106, 62,  160, 112, 54,
+          173, 121, 157, 92,  157, 129, 94,  65,
+      },
 
-      { 52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50,
+      {
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
 
-        52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50,
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
 
-        52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50 },
+          52,  124, 84,  136, 107, 197, 161, 82,
+          183, 151, 153, 140, 152, 134, 109, 81,
+      },
   },
   {
-      { 34, 127, 74,  124, 74,  204, 153, 76,  226, 162,
-        92, 207, 126, 91,  227, 192, 149, 108, 85,  55,
+      {
+          34,  127, 74,  124, 74,  204, 153, 76,
+          226, 162, 207, 126, 227, 192, 149, 108,
 
-        34, 127, 74,  124, 74,  204, 153, 76,  226, 162,
-        92, 207, 126, 91,  227, 192, 149, 108, 85,  55,
+          34,  127, 74,  124, 74,  204, 153, 76,
+          226, 162, 207, 126, 227, 192, 149, 108,
 
-        34, 127, 74,  124, 74,  204, 153, 76,  226, 162,
-        92, 207, 126, 91,  227, 192, 149, 108, 85,  55 },
+          34,  127, 74,  124, 74,  204, 153, 76,
+          226, 162, 207, 126, 227, 192, 149, 108,
+      },
 
-      { 43,  136, 115, 158, 130, 212, 187, 112, 231, 180,
-        130, 202, 164, 130, 236, 204, 168, 139, 112, 114,
+      {
+          43,  136, 115, 158, 130, 212, 187, 112,
+          231, 180, 202, 164, 236, 204, 168, 139,
 
-        43,  136, 115, 158, 130, 212, 187, 112, 231, 180,
-        130, 202, 164, 130, 236, 204, 168, 139, 112, 114,
+          43,  136, 115, 158, 130, 212, 187, 112,
+          231, 180, 202, 164, 236, 204, 168, 139,
 
-        43,  136, 115, 158, 130, 212, 187, 112, 231, 180,
-        130, 202, 164, 130, 236, 204, 168, 139, 112, 114 },
+          43,  136, 115, 158, 130, 212, 187, 112,
+          231, 180, 202, 164, 236, 204, 168, 139,
+      },
   },
   {
-      { 25,  117, 70,  120, 77,  215, 171, 102, 234, 156,
-        105, 235, 155, 109, 247, 220, 176, 127, 92,  72,
+      {
+          25,  117, 70,  120, 77,  215, 171, 102,
+          234, 156, 235, 155, 247, 220, 176, 127,
 
-        25,  117, 70,  120, 77,  215, 171, 102, 234, 156,
-        105, 235, 155, 109, 247, 220, 176, 127, 92,  72,
+          25,  117, 70,  120, 77,  215, 171, 102,
+          234, 156, 235, 155, 247, 220, 176, 127,
 
-        25,  117, 70,  120, 77,  215, 171, 102, 234, 156,
-        105, 235, 155, 109, 247, 220, 176, 127, 92,  72 },
+          25,  117, 70,  120, 77,  215, 171, 102,
+          234, 156, 235, 155, 247, 220, 176, 127,
+      },
 
-      { 24,  88,  49,  100, 62,  202, 148, 62,  237, 178,
-        102, 233, 168, 105, 244, 198, 162, 127, 103, 71,
+      {
+          24,  88,  49,  100, 62,  202, 148, 62,
+          237, 178, 233, 168, 244, 198, 162, 127,
 
-        24,  88,  49,  100, 62,  202, 148, 62,  237, 178,
-        102, 233, 168, 105, 244, 198, 162, 127, 103, 71,
+          24,  88,  49,  100, 62,  202, 148, 62,
+          237, 178, 233, 168, 244, 198, 162, 127,
 
-        24,  88,  49,  100, 62,  202, 148, 62,  237, 178,
-        102, 233, 168, 105, 244, 198, 162, 127, 103, 71 },
+          24,  88,  49,  100, 62,  202, 148, 62,
+          237, 178, 233, 168, 244, 198, 162, 127,
+      },
   },
   {
-      { 11, 54,  17,  69, 26,  128, 125, 56,  232, 130,
-        60, 237, 121, 66, 250, 168, 134, 114, 93,  53,
+      {
+          11,  54,  17,  69,  26,  128, 125, 56,
+          232, 130, 237, 121, 250, 168, 134, 114,
 
-        11, 54,  17,  69, 26,  128, 125, 56,  232, 130,
-        60, 237, 121, 66, 250, 168, 134, 114, 93,  53,
+          11,  54,  17,  69,  26,  128, 125, 56,
+          232, 130, 237, 121, 250, 168, 134, 114,
 
-        11, 54,  17,  69, 26,  128, 125, 56,  232, 130,
-        60, 237, 121, 66, 250, 168, 134, 114, 93,  53 },
+          11,  54,  17,  69,  26,  128, 125, 56,
+          232, 130, 237, 121, 250, 168, 134, 114,
+      },
 
-      { 21, 52,  32,  95,  64,  171, 152, 70,  247, 159,
-        81, 252, 177, 100, 252, 221, 192, 143, 195, 146,
+      {
+          21,  52,  32,  95,  64,  171, 152, 70,
+          247, 159, 252, 177, 252, 221, 192, 143,
 
-        21, 52,  32,  95,  64,  171, 152, 70,  247, 159,
-        81, 252, 177, 100, 252, 221, 192, 143, 195, 146,
+          21,  52,  32,  95,  64,  171, 152, 70,
+          247, 159, 252, 177, 252, 221, 192, 143,
 
-        21, 52,  32,  95,  64,  171, 152, 70,  247, 159,
-        81, 252, 177, 100, 252, 221, 192, 143, 195, 146 },
+          21,  52,  32,  95,  64,  171, 152, 70,
+          247, 159, 252, 177, 252, 221, 192, 143,
+      },
   },
 #else  // CONFIG_EXT_TX
 #if CONFIG_CHROMA_2X2
   {
-      { 34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37 },
-      { 52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50 },
+      {
+          34, 103, 61, 106, 62, 160, 112, 54, 173, 121, 157, 92, 157, 129, 94,
+          65,
+      },
+
+      {
+          52, 124, 84, 136, 107, 197, 161, 82, 183, 151, 153, 140, 152, 134,
+          109, 81,
+      },
   },
 #endif
   {
-      { 34, 103, 61, 106, 62,  160, 112, 54, 173, 121,
-        75, 157, 92, 75,  157, 129, 94,  65, 52,  37 },
-      { 52,  124, 84,  136, 107, 197, 161, 82, 183, 151,
-        109, 153, 140, 103, 152, 134, 109, 81, 69,  50 },
+      {
+          34, 103, 61, 106, 62, 160, 112, 54, 173, 121, 157, 92, 157, 129, 94,
+          65,
+      },
+
+      {
+          52, 124, 84, 136, 107, 197, 161, 82, 183, 151, 153, 140, 152, 134,
+          109, 81,
+      },
   },
   {
-      { 34, 127, 74,  124, 74,  204, 153, 76,  226, 162,
-        92, 207, 126, 91,  227, 192, 149, 108, 85,  55 },
-      { 43,  136, 115, 158, 130, 212, 187, 112, 231, 180,
-        130, 202, 164, 130, 236, 204, 168, 139, 112, 114 },
+      {
+          34, 127, 74, 124, 74, 204, 153, 76, 226, 162, 207, 126, 227, 192, 149,
+          108,
+      },
+
+      {
+          43, 136, 115, 158, 130, 212, 187, 112, 231, 180, 202, 164, 236, 204,
+          168, 139,
+      },
   },
   {
-      { 25,  117, 70,  120, 77,  215, 171, 102, 234, 156,
-        105, 235, 155, 109, 247, 220, 176, 127, 92,  72 },
-      { 24,  88,  49,  100, 62,  202, 148, 62,  237, 178,
-        102, 233, 168, 105, 244, 198, 162, 127, 103, 71 },
+      {
+          25, 117, 70, 120, 77, 215, 171, 102, 234, 156, 235, 155, 247, 220,
+          176, 127,
+      },
+
+      {
+          24, 88, 49, 100, 62, 202, 148, 62, 237, 178, 233, 168, 244, 198, 162,
+          127,
+      },
   },
   {
-      { 11, 54,  17,  69, 26,  128, 125, 56,  232, 130,
-        60, 237, 121, 66, 250, 168, 134, 114, 93,  53 },
-      { 21, 52,  32,  95,  64,  171, 152, 70,  247, 159,
-        81, 252, 177, 100, 252, 221, 192, 143, 195, 146 },
+      {
+          11, 54, 17, 69, 26, 128, 125, 56, 232, 130, 237, 121, 250, 168, 134,
+          114,
+      },
+
+      {
+          21, 52, 32, 95, 64, 171, 152, 70, 247, 159, 252, 177, 252, 221, 192,
+          143,
+      },
   },
 #endif  // CONFIG_EXT_TX
 };
diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index d2a0525..21dc176 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -249,16 +249,15 @@
   return ctx;
 }
 
-#define SIG_REF_OFFSET_NUM 11
+#define SIG_REF_OFFSET_NUM 7
 static int sig_ref_offset[SIG_REF_OFFSET_NUM][2] = {
-  { -2, -1 }, { -2, 0 }, { -2, 1 }, { -1, -2 }, { -1, -1 }, { -1, 0 },
-  { -1, 1 },  { 0, -2 }, { 0, -1 }, { 1, -2 },  { 1, -1 },
+  { -2, -1 }, { -2, 0 }, { -1, -2 }, { -1, -1 },
+  { -1, 0 },  { 0, -2 }, { 0, -1 },
 };
 
 static INLINE int get_nz_count(const tran_low_t *tcoeffs, int bwl, int height,
-                               int row, int col, const int16_t *iscan) {
+                               int row, int col) {
   int count = 0;
-  const int pos = (row << bwl) + col;
   for (int idx = 0; idx < SIG_REF_OFFSET_NUM; ++idx) {
     const int ref_row = row + sig_ref_offset[idx][0];
     const int ref_col = col + sig_ref_offset[idx][1];
@@ -266,7 +265,7 @@
         ref_col >= (1 << bwl))
       continue;
     const int nb_pos = (ref_row << bwl) + ref_col;
-    if (iscan[nb_pos] < iscan[pos]) count += (tcoeffs[nb_pos] != 0);
+    count += (tcoeffs[nb_pos] != 0);
   }
   return count;
 }
@@ -288,10 +287,8 @@
 // TODO(angiebird): optimize this function by generate a table that maps from
 // count to ctx
 static INLINE int get_nz_map_ctx_from_count(int count,
-                                            const tran_low_t *tcoeffs,
                                             int coeff_idx,  // raster order
-                                            int bwl, const int16_t *iscan,
-                                            TX_TYPE tx_type) {
+                                            int bwl, TX_TYPE tx_type) {
   (void)tx_type;
   const int row = coeff_idx >> bwl;
   const int col = coeff_idx - (row << bwl);
@@ -311,19 +308,12 @@
 
   if (row == 0 && col == 0) return offset + 0;
 
-  if (row == 0 && col == 1) return offset + 1 + (tcoeffs[0] != 0);
+  if (row == 0 && col == 1) return offset + 1 + count;
 
-  if (row == 1 && col == 0) return offset + 3 + (tcoeffs[0] != 0);
+  if (row == 1 && col == 0) return offset + 3 + count;
 
   if (row == 1 && col == 1) {
-    int pos;
-    ctx = (tcoeffs[0] != 0);
-
-    if (iscan[1] < iscan[coeff_idx]) ctx += (tcoeffs[1] != 0);
-    pos = 1 << bwl;
-    if (iscan[pos] < iscan[coeff_idx]) ctx += (tcoeffs[pos] != 0);
-
-    ctx = (ctx + 1) >> 1;
+    ctx = (count + 1) >> 1;
 
     assert(5 + ctx <= 7);
 
@@ -333,33 +323,32 @@
   if (row == 0) {
     ctx = (count + 1) >> 1;
 
-    assert(ctx < 3);
+    assert(ctx < 2);
     return offset + 8 + ctx;
   }
 
   if (col == 0) {
     ctx = (count + 1) >> 1;
 
-    assert(ctx < 3);
-    return offset + 11 + ctx;
+    assert(ctx < 2);
+    return offset + 10 + ctx;
   }
 
   ctx = count >> 1;
 
-  assert(14 + ctx < 20);
+  assert(12 + ctx < 16);
 
-  return offset + 14 + ctx;
+  return offset + 12 + ctx;
 }
 
 static INLINE int get_nz_map_ctx(const tran_low_t *tcoeffs,
                                  const int coeff_idx,  // raster order
                                  const int bwl, const int height,
-                                 const int16_t *iscan, TX_TYPE tx_type) {
+                                 TX_TYPE tx_type) {
   const int row = coeff_idx >> bwl;
   const int col = coeff_idx - (row << bwl);
-  int count = get_nz_count(tcoeffs, bwl, height, row, col, iscan);
-  return get_nz_map_ctx_from_count(count, tcoeffs, coeff_idx, bwl, iscan,
-                                   tx_type);
+  int count = get_nz_count(tcoeffs, bwl, height, row, col);
+  return get_nz_map_ctx_from_count(count, coeff_idx, bwl, tx_type);
 }
 
 static INLINE int get_eob_ctx(const tran_low_t *tcoeffs,