Rework base range entropy coding in level map system

Replace the truncated geometric distribution model with the grouped
leaves structure for more efficient probability modeling.
Each group has its own Geometric distribution

This give us 0.2% gain on lowres

Change-Id: If5c73dd429bd5183a8aa81042f8f56937b1d8a6a
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index 447ada6..37ae276 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -78,7 +78,8 @@
 #define LEVEL_CONTEXTS (BR_TMP_OFFSET * BR_REF_CAT)
 
 #define NUM_BASE_LEVELS 2
-#define COEFF_BASE_RANGE (15 - NUM_BASE_LEVELS)
+#define COEFF_BASE_RANGE (16 - NUM_BASE_LEVELS)
+#define BASE_RANGE_SETS 3
 
 #define COEFF_CONTEXT_BITS 6
 #define COEFF_CONTEXT_MASK ((1 << COEFF_CONTEXT_BITS) - 1)
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 259f10d..707b7d0 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -355,58 +355,175 @@
 
 const aom_prob default_coeff_lps[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS] = {
 #if CONFIG_CHROMA_2X2
-  {
-      { 164, 128, 134, 165, 128, 137, 168, 128, 97,  136, 167, 128,
-        182, 205, 143, 172, 200, 145, 173, 193, 103, 137, 170, 191,
-        198, 214, 162, 187, 209, 162, 187, 207, 128, 156, 183, 201,
-        219, 230, 204, 210, 225, 201, 209, 225, 187, 190, 203, 214 },
-      { 106, 128, 98,  126, 128, 87,  122, 128, 54,  89,  131, 128,
-        142, 180, 123, 154, 189, 115, 149, 175, 79,  115, 157, 182,
-        175, 197, 147, 174, 199, 145, 174, 201, 89,  135, 173, 194,
-        212, 222, 206, 203, 223, 188, 201, 220, 128, 144, 202, 206 },
-  },
+  { { 96,  128, 86,  122, 128, 84,  125, 128, 88,  99,  126, 128,
+      135, 159, 99,  130, 134, 100, 128, 144, 70,  97,  128, 139,
+      157, 168, 127, 148, 162, 121, 149, 157, 118, 127, 143, 157,
+      178, 186, 168, 171, 183, 165, 169, 180, 180, 169, 166, 177 },
+    { 81,  128, 72,  95,  128, 64,  98,  128, 42,  66,  101, 128,
+      129, 163, 97,  122, 130, 91,  119, 141, 70,  94,  118, 166,
+      157, 168, 117, 143, 151, 111, 144, 154, 76,  113, 128, 158,
+      177, 185, 165, 167, 179, 155, 166, 179, 110, 137, 115, 165 } },
 #endif
-  {
-      { 164, 128, 134, 165, 128, 137, 168, 128, 97,  136, 167, 128,
-        182, 205, 143, 172, 200, 145, 173, 193, 103, 137, 170, 191,
-        198, 214, 162, 187, 209, 162, 187, 207, 128, 156, 183, 201,
-        219, 230, 204, 210, 225, 201, 209, 225, 187, 190, 203, 214 },
-      { 106, 128, 98,  126, 128, 87,  122, 128, 54,  89,  131, 128,
-        142, 180, 123, 154, 189, 115, 149, 175, 79,  115, 157, 182,
-        175, 197, 147, 174, 199, 145, 174, 201, 89,  135, 173, 194,
-        212, 222, 206, 203, 223, 188, 201, 220, 128, 144, 202, 206 },
-  },
-  {
-      { 171, 128, 123, 169, 128, 121, 165, 128, 82,  125, 168, 128,
-        191, 213, 143, 177, 199, 136, 170, 194, 95,  135, 171, 195,
-        206, 222, 166, 191, 212, 154, 184, 207, 115, 149, 180, 204,
-        223, 237, 196, 215, 231, 186, 209, 228, 158, 178, 201, 222 },
-      { 115, 128, 115, 146, 128, 91,  147, 128, 55,  93,  139, 128,
-        147, 190, 141, 176, 201, 123, 156, 173, 68,  114, 156, 195,
-        186, 205, 153, 191, 214, 141, 179, 205, 107, 132, 166, 184,
-        215, 225, 200, 212, 230, 102, 207, 222, 128, 119, 200, 212 },
-  },
-  {
-      { 185, 128, 134, 198, 128, 128, 195, 128, 58,  110, 162, 128,
-        208, 227, 154, 196, 206, 144, 188, 209, 83,  130, 168, 198,
-        219, 232, 167, 205, 222, 158, 196, 216, 107, 143, 178, 204,
-        233, 244, 202, 226, 238, 191, 217, 234, 153, 178, 200, 223 },
-      { 160, 128, 154, 197, 128, 129, 178, 128, 53,  112, 157, 128,
-        185, 214, 169, 196, 221, 134, 179, 186, 82,  131, 168, 194,
-        204, 220, 176, 209, 221, 173, 194, 209, 107, 154, 181, 203,
-        230, 241, 202, 226, 237, 185, 223, 234, 162, 187, 203, 222 },
-  },
-  {
-      { 177, 128, 165, 226, 128, 152, 219, 128, 45,  129, 188, 128,
-        198, 218, 179, 220, 228, 163, 214, 220, 72,  134, 181, 206,
-        216, 225, 177, 218, 231, 158, 213, 223, 112, 150, 185, 210,
-        245, 251, 204, 234, 247, 195, 231, 243, 163, 186, 213, 235 },
-      { 161, 128, 174, 205, 128, 146, 182, 128, 59,  125, 179, 128,
-        183, 208, 199, 220, 239, 184, 213, 217, 71,  141, 196, 217,
-        213, 219, 215, 230, 237, 171, 224, 238, 112, 173, 193, 221,
-        239, 246, 168, 243, 249, 93,  241, 247, 128, 195, 216, 233 },
-  },
+  { { 96,  128, 86,  122, 128, 84,  125, 128, 88,  99,  126, 128,
+      135, 159, 99,  130, 134, 100, 128, 144, 70,  97,  128, 139,
+      157, 168, 127, 148, 162, 121, 149, 157, 118, 127, 143, 157,
+      178, 186, 168, 171, 183, 165, 169, 180, 180, 169, 166, 177 },
+    { 81,  128, 72,  95,  128, 64,  98,  128, 42,  66,  101, 128,
+      129, 163, 97,  122, 130, 91,  119, 141, 70,  94,  118, 166,
+      157, 168, 117, 143, 151, 111, 144, 154, 76,  113, 128, 158,
+      177, 185, 165, 167, 179, 155, 166, 179, 110, 137, 115, 165 } },
+  { { 102, 128, 79,  125, 128, 74,  121, 128, 61,  98,  128, 128,
+      141, 164, 96,  132, 150, 90,  128, 153, 62,  100, 128, 153,
+      162, 172, 120, 146, 162, 113, 142, 154, 96,  113, 138, 155,
+      181, 188, 151, 170, 179, 147, 167, 181, 158, 157, 163, 176 },
+    { 103, 128, 80,  116, 128, 66,  94,  128, 35,  65,  109, 128,
+      134, 163, 104, 137, 154, 92,  128, 104, 58,  94,  129, 132,
+      156, 173, 137, 149, 165, 104, 143, 143, 112, 101, 133, 159,
+      176, 186, 134, 172, 175, 155, 169, 177, 255, 107, 137, 168 } },
+  { { 125, 128, 85,  157, 128, 82,  155, 128, 42,  83,  116, 128,
+      155, 174, 101, 144, 155, 93,  140, 155, 57,  92,  124, 149,
+      173, 178, 114, 148, 161, 111, 145, 161, 77,  101, 131, 153,
+      190, 191, 140, 169, 183, 140, 169, 179, 108, 122, 150, 171 },
+    { 136, 128, 108, 163, 128, 96,  140, 128, 48,  90,  85,  128,
+      144, 164, 113, 158, 179, 107, 159, 128, 43,  75,  133, 160,
+      157, 184, 144, 160, 189, 154, 152, 184, 128, 124, 137, 140,
+      188, 196, 148, 170, 178, 128, 177, 159, 128, 179, 135, 135 } },
+  { { 133, 128, 110, 153, 128, 101, 157, 128, 49,  91,  134, 128,
+      151, 168, 129, 158, 162, 112, 154, 168, 63,  99,  130, 158,
+      171, 178, 128, 160, 173, 111, 155, 171, 86,  108, 143, 159,
+      194, 196, 162, 177, 185, 123, 172, 181, 101, 132, 156, 178 },
+    { 133, 128, 129, 144, 128, 116, 135, 128, 43,  101, 100, 128,
+      140, 163, 158, 173, 205, 128, 165, 171, 128, 128, 210, 163,
+      172, 184, 192, 176, 201, 183, 177, 190, 128, 192, 199, 144,
+      192, 192, 1,   196, 192, 255, 171, 178, 255, 128, 171, 179 } }
 };
+#if BR_NODE
+const aom_prob
+    default_coeff_br[TX_SIZES][PLANE_TYPES][BASE_RANGE_SETS][LEVEL_CONTEXTS] = {
+#if CONFIG_CHROMA_2X2
+      { { { 62,  128, 54,  116, 128, 51,  97,  128, 59,  68,  107, 128,
+            119, 158, 68,  115, 131, 65,  112, 138, 34,  71,  118, 137,
+            171, 184, 110, 152, 178, 105, 146, 172, 89,  111, 145, 173,
+            214, 226, 201, 198, 214, 196, 193, 210, 239, 196, 186, 202 },
+          { 41,  128, 58,  52,  128, 51,  61,  128, 92,  54,  48,  128,
+            67,  113, 36,  55,  75,  30,  56,  72,  12,  25,  50,  79,
+            94,  131, 37,  75,  108, 42,  78,  103, 5,   31,  67,  103,
+            172, 192, 131, 135, 167, 129, 136, 165, 149, 144, 120, 149 },
+          { 35, 128, 74, 50, 128, 63, 59, 128, 87,  74,  38, 128,
+            32, 53,  23, 34, 50,  18, 30, 41,  15,  13,  18, 18,
+            52, 74,  18, 29, 36,  18, 31, 47,  51,  9,   15, 27,
+            96, 134, 85, 70, 93,  96, 79, 100, 108, 100, 55, 65 } },
+        { { 52,  128, 35,  79,  128, 29,  66,  128, 12,  30, 57,  128,
+            113, 156, 64,  107, 172, 54,  103, 145, 23,  57, 96,  110,
+            165, 184, 95,  138, 166, 95,  141, 184, 55,  80, 133, 165,
+            212, 222, 134, 175, 206, 158, 177, 197, 102, 61, 154, 190 },
+          { 36,  128, 18, 26,  128, 15, 29,  128, 4, 6,  30, 128,
+            63,  113, 25, 44,  66,  22, 40,  67,  9, 14, 34, 55,
+            90,  125, 26, 66,  82,  29, 73,  88,  1, 26, 34, 67,
+            158, 179, 70, 121, 134, 69, 111, 129, 1, 85, 54, 105 },
+          { 24, 128, 8,  31, 128, 15, 16, 128, 1,   1, 1,  128,
+            32, 39,  16, 18, 43,  5,  17, 13,  1,   1, 22, 1,
+            37, 65,  26, 20, 28,  16, 15, 24,  128, 1, 1,  1,
+            83, 107, 57, 56, 74,  34, 29, 73,  128, 1, 37, 47 } } },
+#endif
+      { { { 62,  128, 54,  116, 128, 51,  97,  128, 59,  68,  107, 128,
+            119, 158, 68,  115, 131, 65,  112, 138, 34,  71,  118, 137,
+            171, 184, 110, 152, 178, 105, 146, 172, 89,  111, 145, 173,
+            214, 226, 201, 198, 214, 196, 193, 210, 239, 196, 186, 202 },
+          { 41,  128, 58,  52,  128, 51,  61,  128, 92,  54,  48,  128,
+            67,  113, 36,  55,  75,  30,  56,  72,  12,  25,  50,  79,
+            94,  131, 37,  75,  108, 42,  78,  103, 5,   31,  67,  103,
+            172, 192, 131, 135, 167, 129, 136, 165, 149, 144, 120, 149 },
+          { 35, 128, 74, 50, 128, 63, 59, 128, 87,  74,  38, 128,
+            32, 53,  23, 34, 50,  18, 30, 41,  15,  13,  18, 18,
+            52, 74,  18, 29, 36,  18, 31, 47,  51,  9,   15, 27,
+            96, 134, 85, 70, 93,  96, 79, 100, 108, 100, 55, 65 } },
+        { { 52,  128, 35,  79,  128, 29,  66,  128, 12,  30, 57,  128,
+            113, 156, 64,  107, 172, 54,  103, 145, 23,  57, 96,  110,
+            165, 184, 95,  138, 166, 95,  141, 184, 55,  80, 133, 165,
+            212, 222, 134, 175, 206, 158, 177, 197, 102, 61, 154, 190 },
+          { 36,  128, 18, 26,  128, 15, 29,  128, 4, 6,  30, 128,
+            63,  113, 25, 44,  66,  22, 40,  67,  9, 14, 34, 55,
+            90,  125, 26, 66,  82,  29, 73,  88,  1, 26, 34, 67,
+            158, 179, 70, 121, 134, 69, 111, 129, 1, 85, 54, 105 },
+          { 24, 128, 8,  31, 128, 15, 16, 128, 1,   1, 1,  128,
+            32, 39,  16, 18, 43,  5,  17, 13,  1,   1, 22, 1,
+            37, 65,  26, 20, 28,  16, 15, 24,  128, 1, 1,  1,
+            83, 107, 57, 56, 74,  34, 29, 73,  128, 1, 37, 47 } } },
+      { { { 72,  128, 45,  113, 128, 38,  100, 128, 26,  63,  112, 128,
+            134, 177, 65,  121, 148, 57,  111, 143, 27,  68,  116, 152,
+            181, 198, 98,  148, 173, 84,  136, 168, 53,  89,  134, 170,
+            218, 230, 173, 194, 216, 160, 188, 213, 199, 177, 183, 204 },
+          { 54,  128, 34,  55,  128, 32,  53,  128, 66,  45,  54,  128,
+            81,  128, 33,  59,  102, 26,  55,  80,  7,   23,  49,  91,
+            116, 145, 36,  79,  107, 35,  73,  102, 12,  28,  57,  95,
+            170, 201, 102, 133, 173, 105, 127, 173, 166, 132, 114, 149 },
+          { 40,  128, 25, 30, 128, 21, 31, 128, 24, 17, 24, 128,
+            51,  67,  19, 28, 40,  17, 25, 42,  15, 13, 19, 19,
+            61,  77,  19, 30, 48,  13, 33, 50,  11, 15, 21, 30,
+            103, 147, 37, 69, 111, 37, 66, 105, 18, 18, 36, 76 } },
+        { { 74,  128, 42,  99,  128, 32,  57,  128, 9,  28, 76,  128,
+            115, 187, 70,  118, 120, 52,  109, 128, 19, 60, 93,  100,
+            178, 197, 119, 147, 179, 92,  137, 178, 37, 87, 110, 158,
+            216, 227, 169, 186, 201, 128, 178, 204, 1,  96, 155, 217 },
+          { 59,  128, 26, 34,  128, 11, 20,  128, 7,   8, 24, 128,
+            73,  125, 38, 74,  96,  23, 61,  79,  15,  9, 23, 110,
+            96,  151, 49, 79,  164, 22, 70,  65,  1,   1, 9,  69,
+            156, 196, 73, 105, 181, 17, 126, 155, 128, 1, 90, 111 },
+          { 42, 128, 10, 11, 128, 13, 1,  128, 1,   1,   1, 128,
+            55, 63,  13, 17, 85,  1,  16, 64,  1,   1,   1, 1,
+            62, 58,  32, 21, 53,  1,  37, 91,  128, 128, 1, 1,
+            81, 133, 51, 48, 79,  1,  25, 81,  128, 128, 1, 54 } } },
+      { { { 103, 128, 52,  163, 128, 46,  155, 128, 12, 45,  97,  128,
+            162, 196, 69,  140, 170, 60,  130, 158, 21, 58,  109, 150,
+            205, 214, 93,  149, 178, 79,  143, 179, 38, 71,  120, 159,
+            231, 240, 150, 192, 218, 140, 188, 220, 84, 112, 159, 196 },
+          { 93,  128, 42, 143, 128, 41, 132, 128, 6,  15, 40, 128,
+            113, 172, 39, 99,  113, 33, 91,  94,  5,  15, 42, 83,
+            148, 172, 37, 91,  130, 28, 81,  121, 9,  20, 47, 87,
+            201, 223, 75, 139, 183, 77, 132, 176, 23, 41, 82, 147 },
+          { 92,  128, 45, 123, 128, 28, 88, 128, 1,  8,  20, 128,
+            85,  94,  39, 95,  83,  33, 81, 61,  4,  5,  17, 25,
+            84,  109, 17, 59,  76,  11, 46, 62,  1,  4,  13, 35,
+            139, 184, 25, 86,  129, 25, 71, 123, 26, 13, 31, 84 } },
+        { { 123, 128, 82,  169, 128, 62,  139, 128, 1,   28,  77,  128,
+            139, 167, 92,  170, 146, 76,  149, 255, 19,  68,  160, 73,
+            190, 209, 171, 165, 218, 57,  152, 209, 128, 61,  122, 164,
+            237, 240, 146, 210, 227, 128, 224, 220, 128, 128, 196, 199 },
+          { 130, 128, 52,  141, 128, 32,  101, 128, 128, 1,  85,  128,
+            94,  155, 71,  121, 255, 30,  116, 85,  1,   8,  58,  255,
+            105, 169, 110, 101, 132, 1,   77,  142, 128, 1,  54,  96,
+            166, 214, 224, 154, 198, 255, 153, 230, 128, 85, 100, 146 },
+          { 103, 128, 26, 83, 128, 20,  47,  128, 128, 128, 1,  128,
+            91,  90,  19, 76, 128, 1,   42,  1,   128, 255, 64, 128,
+            74,  77,  1,  72, 68,  128, 13,  77,  128, 128, 64, 1,
+            71,  147, 37, 99, 171, 1,   104, 151, 128, 1,   1,  96 } } },
+      { { { 113, 128, 79,  165, 128, 69,  149, 128, 14, 55,  116, 128,
+            163, 202, 104, 169, 205, 82,  159, 180, 22, 64,  121, 165,
+            207, 216, 113, 177, 215, 95,  166, 195, 35, 77,  132, 179,
+            241, 244, 173, 207, 233, 128, 202, 227, 92, 121, 169, 209 },
+          { 114, 128, 67, 136, 128, 54, 132, 128, 6,  26, 62,  128,
+            85,  129, 85, 146, 173, 64, 129, 140, 7,  19, 65,  92,
+            139, 169, 42, 147, 186, 40, 129, 170, 18, 18, 65,  117,
+            213, 230, 74, 172, 213, 69, 165, 196, 1,  40, 103, 170 },
+          { 101, 128, 61, 134, 128, 52, 97,  128, 1,   14, 26, 128,
+            79,  72,  71, 135, 152, 56, 114, 117, 1,   10, 24, 58,
+            64,  66,  60, 133, 148, 16, 126, 123, 1,   32, 26, 56,
+            143, 197, 51, 141, 176, 59, 132, 162, 128, 17, 47, 106 } },
+        { { 115, 128, 112, 135, 128, 89,  130, 128, 15,  49,  89,  128,
+            143, 238, 154, 203, 255, 138, 172, 255, 1,   98,  196, 255,
+            185, 203, 255, 211, 255, 192, 217, 235, 128, 128, 171, 255,
+            233, 233, 255, 247, 255, 1,   239, 245, 1,   128, 255, 255 },
+          { 75,  128, 76,  118, 128, 35,  74,  128, 1,   13,  23,  128,
+            63,  138, 114, 164, 140, 91,  128, 128, 128, 1,   138, 64,
+            96,  128, 255, 175, 236, 85,  166, 209, 128, 1,   128, 146,
+            196, 217, 1,   204, 206, 128, 212, 221, 128, 128, 128, 219 },
+          { 49,  128, 36,  62,  128, 37,  56, 128, 128, 1,   1,   128,
+            45,  37,  68,  102, 128, 90,  56, 1,   128, 128, 37,  1,
+            26,  27,  128, 126, 128, 255, 63, 142, 128, 128, 1,   1,
+            125, 159, 128, 173, 212, 128, 85, 189, 128, 128, 255, 171 } } }
+    };
+#endif  // BR_NODE
 #endif  // CONFIG_LV_MAP
 
 #if CONFIG_EXT_PARTITION_TYPES
@@ -5060,6 +5177,9 @@
   av1_copy(fc->dc_sign, default_dc_sign);
   av1_copy(fc->coeff_base, default_coeff_base);
   av1_copy(fc->coeff_lps, default_coeff_lps);
+#if BR_NODE
+  av1_copy(fc->coeff_br, default_coeff_br);
+#endif
 
 #if LV_MAP_PROB
   av1_init_txb_probs(fc);
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 8a8b5ba..97275bd 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -175,6 +175,9 @@
   aom_prob coeff_base[TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS]
                      [COEFF_BASE_CONTEXTS];
   aom_prob coeff_lps[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS];
+#if BR_NODE
+  aom_prob coeff_br[TX_SIZES][PLANE_TYPES][BASE_RANGE_SETS][LEVEL_CONTEXTS];
+#endif
 
 #if LV_MAP_PROB
   aom_cdf_prob txb_skip_cdf[TX_SIZES][TXB_SKIP_CONTEXTS][CDF_SIZE(2)];
@@ -187,6 +190,10 @@
                              [COEFF_BASE_CONTEXTS][CDF_SIZE(2)];
   aom_cdf_prob coeff_lps_cdf[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS]
                             [CDF_SIZE(2)];
+#if BR_NODE
+  aom_cdf_prob coeff_br_cdf[TX_SIZES][PLANE_TYPES][BASE_RANGE_SETS]
+                           [LEVEL_CONTEXTS][CDF_SIZE(2)];
+#endif
 #endif  // LV_MAP_PROB
 #endif
 
@@ -435,6 +442,8 @@
   unsigned int coeff_base[TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS]
                          [COEFF_BASE_CONTEXTS][2];
   unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS][2];
+  unsigned int coeff_br[TX_SIZES][PLANE_TYPES][BASE_RANGE_SETS][LEVEL_CONTEXTS]
+                       [2];
 #endif  // CONFIG_LV_MAP
 
   av1_blockz_count_model blockz_count[TX_SIZES][PLANE_TYPES];
diff --git a/av1/common/txb_common.c b/av1/common/txb_common.c
index df24320..a95e07c 100644
--- a/av1/common/txb_common.c
+++ b/av1/common/txb_common.c
@@ -160,10 +160,20 @@
         fc->coeff_lps_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
         fc->coeff_lps_cdf[tx_size][plane][ctx][2] = 0;
       }
+#if BR_NODE
+      for (int br = 0; br < BASE_RANGE_SETS; ++br) {
+        for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
+          fc->coeff_br_cdf[tx_size][plane][br][ctx][0] = AOM_ICDF(
+              128 * (aom_cdf_prob)fc->coeff_br[tx_size][plane][br][ctx]);
+          fc->coeff_br_cdf[tx_size][plane][br][ctx][1] = AOM_ICDF(32768);
+          fc->coeff_br_cdf[tx_size][plane][br][ctx][2] = 0;
+        }
+      }
+#endif  // BR_NODE
     }
   }
 }
-#endif
+#endif  // LV_MAP_PROB
 
 void av1_adapt_txb_probs(AV1_COMMON *cm, unsigned int count_sat,
                          unsigned int update_factor) {
@@ -211,11 +221,23 @@
   }
 
   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
-    for (plane = 0; plane < PLANE_TYPES; ++plane)
-      for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
+    for (plane = 0; plane < PLANE_TYPES; ++plane) {
+      for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
         fc->coeff_lps[tx_size][plane][ctx] = merge_probs(
             pre_fc->coeff_lps[tx_size][plane][ctx],
             counts->coeff_lps[tx_size][plane][ctx], count_sat, update_factor);
+      }
+#if BR_NODE
+      for (int br = 0; br < BASE_RANGE_SETS; ++br) {
+        for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
+          fc->coeff_br[tx_size][plane][br][ctx] =
+              merge_probs(pre_fc->coeff_br[tx_size][plane][br][ctx],
+                          counts->coeff_br[tx_size][plane][br][ctx], count_sat,
+                          update_factor);
+        }
+      }
+#endif  // BR_NODE
+    }
   }
 }
 
diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index 2d78cf5..c8de720 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -173,10 +173,22 @@
   /* clang-format on*/
 };
 
-static int br_level_map[9] = {
+static const int br_level_map[9] = {
   0, 0, 1, 1, 2, 2, 3, 3, 3,
 };
 
+static const int coeff_to_br_index[COEFF_BASE_RANGE] = {
+  0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
+};
+
+static const int br_index_to_coeff[BASE_RANGE_SETS] = {
+  0, 2, 6,
+};
+
+static const int br_extra_bits[BASE_RANGE_SETS] = {
+  1, 2, 3,
+};
+
 #define BR_MAG_OFFSET 1
 // TODO(angiebird): optimize this function by using a table to map from
 // count/mag to ctx
@@ -214,7 +226,7 @@
     offset = 0;
   else if (mag <= 3)
     offset = 1;
-  else if (mag <= 6)
+  else if (mag <= 5)
     offset = 2;
   else
     offset = 3;
diff --git a/av1/decoder/decodetxb.c b/av1/decoder/decodetxb.c
index abb2987..3fc9341 100644
--- a/av1/decoder/decodetxb.c
+++ b/av1/decoder/decodetxb.c
@@ -216,6 +216,52 @@
 
     if (ec_ctx->coeff_lps[txs_ctx][plane_type][ctx] == 0) exit(0);
 
+#if BR_NODE
+    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
+#if LV_MAP_PROB
+      if (aom_read_symbol(r,
+                          cm->fc->coeff_br_cdf[txs_ctx][plane_type][idx][ctx],
+                          2, ACCT_STR))
+#else   // LV_MAP_PROB
+      if (aom_read(r, cm->fc->coeff_br[txs_ctx][plane_type][idx][ctx],
+                   ACCT_STR))
+#endif  // LV_MAP_PROB
+      {
+        int extra_bits = (1 << br_extra_bits[idx]) - 1;
+        //        int br_offset = aom_read_literal(r, extra_bits, ACCT_STR);
+        int br_offset = 0;
+        int tok;
+        if (counts) ++counts->coeff_br[txs_ctx][plane_type][ctx][idx][1];
+        for (tok = 0; tok < extra_bits; ++tok) {
+#if LV_MAP_PROB
+          if (aom_read_symbol(r,
+                              cm->fc->coeff_lps_cdf[txs_ctx][plane_type][ctx],
+                              2, ACCT_STR))
+#else
+          if (aom_read(r, cm->fc->coeff_lps[txs_ctx][plane_type][ctx],
+                       ACCT_STR))
+#endif
+          {
+            br_offset = tok;
+            if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1];
+            break;
+          }
+          if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
+        }
+        if (tok == extra_bits) br_offset = extra_bits;
+
+        int br_base = br_index_to_coeff[idx];
+
+        *v = NUM_BASE_LEVELS + 1 + br_base + br_offset;
+        cul_level += *v;
+        if (sign) *v = -(*v);
+        break;
+      }
+      if (counts) ++counts->coeff_br[txs_ctx][plane_type][ctx][idx][0];
+    }
+
+    if (idx < BASE_RANGE_SETS) continue;
+#else
     for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
 #if LV_MAP_PROB
       if (aom_read_symbol(r, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2,
@@ -234,6 +280,7 @@
       if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
     }
     if (idx < COEFF_BASE_RANGE) continue;
+#endif
 
     // decode 0-th order Golomb code
     *v = read_golomb(xd, r) + COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS;
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 94c2bda..451f3be 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -75,6 +75,9 @@
   int dc_sign_cost[DC_SIGN_CONTEXTS][2];
   int base_cost[NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS][2];
   int lps_cost[LEVEL_CONTEXTS][2];
+#if BR_NODE
+  int br_cost[BASE_RANGE_SETS][LEVEL_CONTEXTS][2];
+#endif
 } LV_MAP_COEFF_COST;
 
 typedef struct {
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 1012668..df25c7e 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -231,6 +231,41 @@
 
     // level is above 1.
     ctx = get_br_ctx(tcoeff, scan[c], bwl, height);
+
+#if BR_NODE
+    int base_range = level - 1 - NUM_BASE_LEVELS;
+    int br_set_idx = 0;
+    int br_base = 0;
+    int br_offset = 0;
+
+    if (base_range >= COEFF_BASE_RANGE)
+      br_set_idx = BASE_RANGE_SETS;
+    else
+      br_set_idx = coeff_to_br_index[base_range];
+
+    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
+      aom_write_symbol(w, idx == br_set_idx,
+                       cm->fc->coeff_br_cdf[txs_ctx][plane_type][idx][ctx], 2);
+      if (idx == br_set_idx) {
+        br_base = br_index_to_coeff[br_set_idx];
+        br_offset = base_range - br_base;
+        int extra_bits = (1 << br_extra_bits[idx]) - 1;
+        for (int tok = 0; tok < extra_bits; ++tok) {
+          if (tok == br_offset) {
+            aom_write_symbol(
+                w, 1, cm->fc->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2);
+            break;
+          }
+          aom_write_symbol(w, 0,
+                           cm->fc->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2);
+        }
+        //        aom_write_literal(w, br_offset, br_extra_bits[idx]);
+        break;
+      }
+    }
+
+    if (br_set_idx < BASE_RANGE_SETS) continue;
+#else  // BR_NODE
     for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
       if (level == (idx + 1 + NUM_BASE_LEVELS)) {
 #if LV_MAP_PROB
@@ -249,6 +284,7 @@
 #endif
     }
     if (idx < COEFF_BASE_RANGE) continue;
+#endif  // BR_NODE
 
     // use 0-th order Golomb code to handle the residual level.
     write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
@@ -428,9 +464,35 @@
       if (level > NUM_BASE_LEVELS) {
         int idx;
         int ctx;
-
         ctx = get_br_ctx(qcoeff, scan[c], bwl, height);
+#if BR_NODE
+        int base_range = level - 1 - NUM_BASE_LEVELS;
+        int br_set_idx = base_range < COEFF_BASE_RANGE
+                             ? coeff_to_br_index[base_range]
+                             : BASE_RANGE_SETS;
 
+        for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
+          if (br_set_idx == idx) {
+            int br_base = br_index_to_coeff[br_set_idx];
+            int br_offset = base_range - br_base;
+            int extra_bits = (1 << br_extra_bits[idx]) - 1;
+            cost += coeff_costs->br_cost[idx][ctx][1];
+            for (int tok = 0; tok < extra_bits; ++tok) {
+              if (tok == br_offset) {
+                cost += coeff_costs->lps_cost[ctx][1];
+                break;
+              }
+              cost += coeff_costs->lps_cost[ctx][0];
+            }
+            //            cost += extra_bits * av1_cost_bit(128, 1);
+            break;
+          }
+          cost += coeff_costs->br_cost[idx][ctx][0];
+        }
+        if (idx < BASE_RANGE_SETS) continue;
+
+        idx = COEFF_BASE_RANGE;
+#else
         for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
           if (level == (idx + 1 + NUM_BASE_LEVELS)) {
             cost += coeff_costs->lps_cost[ctx][1];
@@ -438,7 +500,7 @@
           }
           cost += coeff_costs->lps_cost[ctx][0];
         }
-
+#endif
         if (idx >= COEFF_BASE_RANGE) {
           // residual cost
           int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
@@ -1681,6 +1743,45 @@
 
     // level is above 1.
     ctx = get_br_ctx(tcoeff, scan[c], bwl, height);
+
+#if BR_NODE
+    int base_range = level - 1 - NUM_BASE_LEVELS;
+    int br_set_idx = base_range < COEFF_BASE_RANGE
+                         ? coeff_to_br_index[base_range]
+                         : BASE_RANGE_SETS;
+
+    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
+      if (idx == br_set_idx) {
+        int br_base = br_index_to_coeff[br_set_idx];
+        int br_offset = base_range - br_base;
+        ++td->counts->coeff_br[txsize_ctx][plane_type][idx][ctx][1];
+#if LV_MAP_PROB
+        update_cdf(ec_ctx->coeff_br_cdf[txsize_ctx][plane_type][idx][ctx], 1,
+                   2);
+#endif
+        int extra_bits = (1 << br_extra_bits[idx]) - 1;
+        for (int tok = 0; tok < extra_bits; ++tok) {
+          if (br_offset == tok) {
+            ++td->counts->coeff_lps[txsize_ctx][plane_type][ctx][1];
+#if LV_MAP_PROB
+            update_cdf(ec_ctx->coeff_lps_cdf[txsize_ctx][plane_type][ctx], 1,
+                       2);
+#endif
+            break;
+          }
+          ++td->counts->coeff_lps[txsize_ctx][plane_type][ctx][0];
+#if LV_MAP_PROB
+          update_cdf(ec_ctx->coeff_lps_cdf[txsize_ctx][plane_type][ctx], 0, 2);
+#endif
+        }
+        break;
+      }
+      ++td->counts->coeff_br[txsize_ctx][plane_type][idx][ctx][0];
+#if LV_MAP_PROB
+      update_cdf(ec_ctx->coeff_br_cdf[txsize_ctx][plane_type][idx][ctx], 0, 2);
+#endif
+    }
+#else  // BR_NODE
     for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
       if (level == (idx + 1 + NUM_BASE_LEVELS)) {
         ++td->counts->coeff_lps[txsize_ctx][plane_type][ctx][1];
@@ -1695,7 +1796,7 @@
 #endif
     }
     if (idx < COEFF_BASE_RANGE) continue;
-
+#endif  // BR_NODE
     // use 0-th order Golomb code to handle the residual level.
   }
 
diff --git a/av1/encoder/encodetxb.h b/av1/encoder/encodetxb.h
index acca0ea..16a1b93 100644
--- a/av1/encoder/encodetxb.h
+++ b/av1/encoder/encodetxb.h
@@ -64,6 +64,9 @@
   const aom_prob *coeff_lps;
   const aom_prob *eob_flag;
   const aom_prob *txb_skip;
+#if BR_NODE
+  const aom_prob *coeff_br;
+#endif
 } TxbProbs;
 
 void av1_alloc_txb_buf(AV1_COMP *cpi);
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 038fe33..a37eb4d 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -500,10 +500,18 @@
               pcost->base_cost[layer][ctx],
               fc->coeff_base_cdf[tx_size][plane][layer][ctx], NULL);
 
+#if BR_NODE
+      for (int br = 0; br < BASE_RANGE_SETS; ++br)
+        for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
+          av1_cost_tokens_from_cdf(pcost->br_cost[br][ctx],
+                                   fc->coeff_br_cdf[tx_size][plane][br][ctx],
+                                   NULL);
+#endif  // BR_NODE
+
       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
         av1_cost_tokens_from_cdf(pcost->lps_cost[ctx],
                                  fc->coeff_lps_cdf[tx_size][plane][ctx], NULL);
-#else
+#else   // LV_MAP_PROB
       for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
         get_rate_cost(fc->txb_skip[tx_size][ctx], pcost->txb_skip_cost[ctx]);
 
@@ -523,11 +531,11 @@
 
       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
         get_rate_cost(fc->coeff_lps[tx_size][plane][ctx], pcost->lps_cost[ctx]);
-#endif
+#endif  // LV_MAP_PROB
     }
   }
 }
-#endif
+#endif  // CONFIG_LV_MAP
 
 void av1_fill_token_costs_from_cdf(av1_coeff_cost *cost,
                                    coeff_cdf_model (*cdf)[PLANE_TYPES]) {