[lv_map_multi] Base level alphabet adjustment

At eob-1, coefficient must be non-zero. As such, this CL changes the
alphabet for base levels at eob-1 from size 4 to size 3. Minor
performance improvement is observed. In addition, changes in 33462 made
by Ola Hugosson were also incorporated.

Now with trained initial probability distributions.

Change-Id: Id6b5d0908b5ff186ed88ab0733ce7cc0c4a468d5
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index bc568cf..806bf2b 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -1623,6 +1623,11 @@
   AVERAGE_TILE_CDFS(eob_extra_cdf)
   AVERAGE_TILE_CDFS(dc_sign_cdf)
   AVERAGE_TILE_CDFS(coeff_base_cdf)
+#if CONFIG_LV_MAP_MULTI
+#if USE_BASE_EOB_ALPHABET
+  AVERAGE_TILE_CDFS(coeff_base_eob_cdf)
+#endif
+#endif
 #if !CONFIG_LV_MAP_MULTI
   AVERAGE_TILE_CDFS(coeff_lps_cdf)
 #endif
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index bd24ba7..e11cf47 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -75,12 +75,18 @@
 #define SIG_COEF_CONTEXTS_2D 26
 #define SIG_COEF_CONTEXTS_1D 16
 #if CONFIG_LV_MAP_MULTI
+#define USE_BASE_EOB_ALPHABET 1
 #define SIG_COEF_CONTEXTS_EOB 4
 #define SIG_COEF_CONTEXTS \
   (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D + SIG_COEF_CONTEXTS_EOB)
 #else
 #if USE_CAUSAL_BASE_CTX
+#define TRAIN_INIT_PROB 1
+#if TRAIN_INIT_PROB
+#define SIG_COEF_CONTEXTS (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D + 4)
+#else
 #define SIG_COEF_CONTEXTS (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D)
+#endif
 #else  // USE_CAUSAL_BASE_CTX
 #define SIG_COEF_CONTEXTS \
   (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D + SIG_COEF_CONTEXTS_1D)
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 932e1ec..7ba6198 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -31,107 +31,107 @@
   { 125, 102, 147 }, { 119, 101, 135 },
 };
 
-#if CONFIG_LV_MAP_MULTI
+#if CONFIG_LV_MAP_MULTI || TRAIN_INIT_PROB
 const aom_prob default_coeff_base
     [TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS] = {
-      { { { 139, 242, 199, 135, 87,  51,  245, 208, 138, 89,  60,  128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 240, 200, 108,
-            70,  56,  230, 199, 126, 66,  32,  242, 204, 128, 74,  45,
-            241, 203, 120, 62,  128, 128, 139, 180, 180, 242 },
-          { 118, 230, 197, 147, 103, 60,  228, 202, 154, 110, 72,  128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 213, 196, 134,
-            94,  58,  199, 184, 130, 82,  40,  217, 186, 134, 86,  57,
-            204, 185, 127, 74,  128, 128, 118, 180, 180, 230 } },
-        { { 203, 248, 223, 170, 105, 58,  252, 235, 195, 125, 72,  128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 253, 244, 209,
-            110, 85,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 203, 180, 180, 248 },
-          { 170, 239, 210, 163, 112, 68,  239, 219, 175, 127, 84,  128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 246, 234, 184,
-            142, 154, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 170, 180, 180, 239 } } },
-      { { { 119, 240, 196, 136, 90,  50,  247, 214, 159, 109, 64,  241,
-            201, 137, 91,  53,  239, 199, 136, 89,  52,  251, 230, 176,
-            121, 76,  231, 202, 142, 84,  39,  246, 221, 169, 111, 61,
-            248, 221, 152, 97,  60,  128, 119, 180, 180, 240 },
-          { 98,  209, 182, 140, 100, 56,  234, 207, 166, 122, 73,  215,
-            189, 142, 103, 59,  216, 188, 140, 99,  59,  235, 216, 179,
-            137, 88,  214, 189, 141, 96,  46,  224, 200, 159, 117, 68,
-            221, 199, 152, 108, 68,  128, 98,  180, 180, 209 } },
-        { { 195, 245, 212, 159, 108, 64,  251, 231, 186, 131, 80,  250,
-            228, 175, 108, 58,  244, 213, 163, 120, 82,  254, 245, 216,
-            159, 91,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 195, 180, 180, 245 },
-          { 150, 207, 199, 164, 121, 76,  239, 221, 185, 142, 89,  231,
-            208, 162, 113, 69,  228, 205, 169, 136, 95,  246, 235, 204,
-            157, 110, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 150, 180, 180, 207 } } },
-      { { { 93,  232, 185, 124, 81,  38,  244, 206, 147, 98,  53,  246,
-            210, 150, 100, 54,  244, 208, 146, 94,  44,  253, 236, 189,
-            135, 82,  228, 195, 130, 76,  35,  242, 220, 170, 114, 59,
-            253, 235, 184, 122, 68,  128, 93,  180, 180, 232 },
-          { 80,  203, 169, 127, 86,  41,  228, 194, 151, 108, 58,  221,
-            195, 152, 110, 60,  220, 194, 149, 103, 48,  238, 221, 188,
-            147, 92,  205, 184, 130, 85,  41,  208, 195, 159, 120, 62,
-            233, 212, 173, 126, 76,  128, 80,  180, 180, 203 } },
-        { { 187, 239, 199, 149, 109, 66,  250, 226, 179, 130, 82,  248,
-            218, 159, 100, 63,  251, 227, 184, 135, 91,  254, 241, 202,
-            150, 99,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 187, 180, 180, 239 },
-          { 130, 202, 169, 146, 118, 73,  236, 217, 186, 146, 91,  215,
-            194, 154, 102, 77,  234, 216, 188, 145, 98,  245, 229, 201,
-            163, 110, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 130, 180, 180, 202 } } },
-      { { { 102, 225, 171, 113, 74,  33,  243, 201, 139, 92,  44,  244,
-            208, 146, 99,  53,  247, 214, 158, 107, 52,  253, 233, 183,
-            131, 73,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 102, 180, 180, 225 },
-          { 80,  190, 152, 116, 79,  34,  222, 187, 142, 101, 47,  211,
-            187, 148, 106, 56,  221, 200, 163, 118, 56,  236, 215, 181,
-            140, 78,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 80,  180, 180, 190 } },
-        { { 166, 237, 184, 144, 106, 62,  245, 211, 167, 129, 93,  128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 254, 240, 205,
-            158, 112, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 166, 180, 180, 237 },
-          { 91,  185, 133, 105, 95,  60,  213, 190, 164, 131, 107, 128,
-            128, 128, 128, 128, 128, 128, 128, 128, 128, 242, 227, 203,
-            160, 127, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
-            128, 128, 128, 128, 128, 128, 91,  180, 180, 185 } } }
+      { { { 109, 232, 199, 135, 87,  52,  239, 210, 142, 92,  63,  128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 233, 205, 112,
+            75,  62,  226, 200, 122, 60,  29,  241, 204, 126, 70,  42,
+            238, 204, 119, 57,  128, 128, 203, 244, 246, 247 },
+          { 107, 223, 197, 147, 102, 62,  227, 205, 156, 112, 74,  128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 212, 200, 138,
+            99,  64,  198, 184, 129, 78,  38,  219, 186, 131, 84,  56,
+            204, 184, 127, 77,  128, 128, 191, 226, 222, 223 } },
+        { { 163, 241, 224, 174, 109, 58,  248, 236, 200, 130, 75,  128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 249, 244, 213,
+            128, 51,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 228, 248, 252, 254 },
+          { 153, 231, 211, 165, 113, 66,  236, 221, 180, 127, 88,  128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 243, 238, 191,
+            111, 154, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 209, 237, 239, 248 } } },
+      { { { 91,  228, 196, 134, 88,  48,  242, 213, 157, 107, 63,  239,
+            202, 139, 92,  54,  236, 201, 138, 90,  53,  248, 229, 176,
+            121, 76,  231, 202, 140, 80,  37,  246, 222, 168, 110, 61,
+            248, 221, 151, 97,  60,  128, 199, 249, 252, 252 },
+          { 85,  196, 181, 138, 98,  55,  230, 206, 164, 121, 73,  219,
+            193, 144, 104, 62,  218, 192, 143, 101, 61,  237, 218, 182,
+            140, 90,  213, 191, 140, 94,  44,  224, 200, 159, 115, 68,
+            220, 199, 151, 107, 69,  128, 183, 229, 226, 230 } },
+        { { 139, 235, 211, 159, 107, 66,  247, 231, 184, 127, 79,  246,
+            226, 170, 103, 61,  236, 212, 161, 119, 76,  253, 245, 215,
+            156, 96,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 227, 249, 253, 255 },
+          { 128, 195, 197, 162, 121, 73,  236, 220, 184, 138, 86,  223,
+            208, 158, 107, 64,  222, 204, 167, 132, 86,  246, 234, 205,
+            161, 108, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 198, 228, 239, 244 } } },
+      { { { 73,  224, 186, 123, 79,  39,  240, 205, 146, 98,  53,  243,
+            209, 147, 98,  52,  241, 207, 145, 93,  44,  252, 236, 188,
+            134, 82,  227, 193, 125, 72,  32,  241, 218, 168, 112, 52,
+            251, 233, 179, 114, 62,  128, 179, 250, 254, 255 },
+          { 69,  200, 171, 127, 86,  41,  224, 193, 150, 107, 58,  218,
+            194, 150, 107, 58,  220, 193, 147, 102, 47,  239, 222, 188,
+            147, 92,  204, 183, 127, 80,  36,  211, 194, 158, 114, 57,
+            231, 209, 167, 120, 70,  128, 171, 226, 235, 232 } },
+        { { 117, 225, 196, 146, 107, 68,  246, 225, 177, 128, 81,  243,
+            217, 163, 105, 60,  247, 225, 180, 131, 84,  253, 241, 202,
+            148, 97,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 219, 251, 255, 255 },
+          { 103, 189, 166, 141, 116, 70,  234, 216, 184, 145, 92,  208,
+            195, 155, 110, 79,  226, 214, 184, 144, 90,  245, 229, 202,
+            164, 113, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 182, 221, 236, 243 } } },
+      { { { 72,  212, 172, 113, 74,  33,  238, 200, 139, 91,  44,  240,
+            208, 147, 98,  51,  244, 214, 157, 107, 51,  253, 233, 184,
+            132, 74,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 201, 250, 254, 255 },
+          { 68,  187, 154, 116, 80,  34,  217, 186, 142, 101, 47,  205,
+            187, 148, 106, 56,  218, 201, 163, 118, 55,  236, 215, 181,
+            141, 79,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 192, 219, 246, 244 } },
+        { { 80,  217, 179, 140, 106, 60,  237, 208, 165, 129, 94,  128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 253, 240, 205,
+            158, 111, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 205, 250, 255, 254 },
+          { 66,  175, 129, 103, 89,  57,  204, 189, 164, 133, 106, 128,
+            128, 128, 128, 128, 128, 128, 128, 128, 128, 238, 228, 202,
+            166, 124, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+            128, 128, 128, 128, 128, 128, 154, 207, 128, 226 } } }
     };
 
 const aom_prob default_nz_map[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS] = {
-  { { 59,  183, 130, 76,  48,  28,  202, 153, 84,  54,  34,  128,
-      128, 128, 128, 128, 128, 128, 128, 128, 128, 200, 163, 84,
-      61,  34,  182, 131, 71,  31,  16,  210, 156, 78,  36,  26,
-      220, 170, 73,  32,  128, 128, 1,   1,   1,   1 },
-    { 96,  209, 171, 109, 59,  28,  223, 187, 128, 71,  35,  128,
-      128, 128, 128, 128, 128, 128, 128, 128, 128, 215, 192, 135,
-      63,  73,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+  { { 59,  183, 130, 76,  47,  29,  203, 154, 85,  55,  35,  128,
+      128, 128, 128, 128, 128, 128, 128, 128, 128, 202, 165, 86,
+      70,  42,  183, 132, 70,  28,  13,  212, 158, 77,  34,  22,
+      222, 172, 74,  30,  128, 128, 1,   1,   1,   1 },
+    { 97,  210, 172, 111, 61,  29,  223, 188, 131, 77,  33,  128,
+      128, 128, 128, 128, 128, 128, 128, 128, 128, 214, 192, 137,
+      63,  51,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
       128, 128, 128, 128, 128, 128, 1,   1,   1,   1 } },
-  { { 49,  190, 129, 74,  46,  25,  206, 150, 90,  55,  32,  205,
-      142, 79,  48,  27,  191, 137, 78,  47,  27,  223, 181, 109,
-      63,  37,  204, 136, 82,  42,  19,  226, 169, 106, 58,  30,
-      231, 180, 96,  49,  29,  128, 1,   1,   1,   1 },
-    { 86,  210, 159, 90,  56,  34,  223, 181, 114, 69,  40,  229,
-      182, 113, 59,  28,  218, 162, 96,  64,  42,  237, 207, 146,
-      92,  50,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+  { { 50,  194, 130, 72,  45,  25,  208, 150, 89,  55,  32,  206,
+      142, 80,  49,  28,  191, 137, 79,  47,  27,  224, 180, 108,
+      63,  37,  206, 137, 81,  40,  18,  229, 170, 106, 58,  30,
+      233, 180, 96,  49,  29,  128, 1,   1,   1,   1 },
+    { 84,  210, 159, 89,  55,  33,  223, 181, 114, 67,  36,  227,
+      180, 112, 58,  31,  218, 161, 95,  63,  40,  237, 208, 146,
+      88,  50,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
       128, 128, 128, 128, 128, 128, 1,   1,   1,   1 } },
-  { { 41,  187, 119, 66,  40,  20,  212, 144, 80,  49,  26,  226,
-      151, 84,  49,  27,  214, 146, 82,  47,  22,  237, 190, 115,
-      68,  40,  206, 126, 71,  37,  17,  227, 168, 103, 61,  29,
-      237, 197, 119, 63,  32,  128, 1,   1,   1,   1 },
-    { 70,  207, 146, 82,  54,  35,  226, 167, 99,  65,  40,  238,
-      176, 96,  57,  29,  229, 171, 108, 69,  47,  244, 201, 124,
-      78,  48,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+  { { 40,  187, 118, 65,  39,  20,  211, 142, 80,  49,  26,  226,
+      150, 83,  49,  26,  213, 146, 82,  47,  22,  237, 191, 116,
+      68,  39,  204, 124, 68,  35,  15,  226, 167, 104, 58,  26,
+      236, 195, 117, 59,  30,  128, 1,   1,   1,   1 },
+    { 69,  207, 145, 80,  53,  36,  226, 167, 98,  64,  38,  238,
+      174, 95,  54,  32,  228, 169, 103, 67,  41,  243, 201, 123,
+      77,  48,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
       128, 128, 128, 128, 128, 128, 1,   1,   1,   1 } },
-  { { 40,  176, 107, 57,  37,  16,  216, 136, 73,  44,  22,  235,
-      145, 79,  49,  26,  227, 149, 84,  52,  26,  247, 195, 107,
-      64,  35,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+  { { 39,  175, 107, 58,  38,  16,  214, 135, 73,  44,  21,  235,
+      145, 80,  48,  26,  227, 149, 84,  52,  25,  247, 195, 108,
+      65,  35,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
       128, 128, 128, 128, 128, 128, 1,   1,   1,   1 },
-    { 49,  210, 153, 95,  58,  31,  217, 151, 93,  64,  41,  128,
-      128, 128, 128, 128, 128, 128, 128, 128, 128, 248, 197, 123,
-      79,  53,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+    { 44,  206, 151, 90,  60,  30,  214, 148, 92,  62,  40,  128,
+      128, 128, 128, 128, 128, 128, 128, 128, 128, 248, 197, 125,
+      80,  52,  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
       128, 128, 128, 128, 128, 128, 1,   1,   1,   1 } }
 };
 #else  // CONFIG_LV_MAP_MULTI
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index c7a7f7f..a8aee78 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -156,6 +156,10 @@
                             [CDF_SIZE(2)];
   aom_cdf_prob dc_sign_cdf[PLANE_TYPES][DC_SIGN_CONTEXTS][CDF_SIZE(2)];
 #if CONFIG_LV_MAP_MULTI
+#if USE_BASE_EOB_ALPHABET
+  aom_cdf_prob coeff_base_eob_cdf[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS_EOB]
+                                 [CDF_SIZE(3)];
+#endif
   aom_cdf_prob coeff_base_cdf[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]
                              [CDF_SIZE(4)];
   aom_cdf_prob coeff_br_cdf[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS]
diff --git a/av1/common/txb_common.c b/av1/common/txb_common.c
index 4607428..c36c5d2 100644
--- a/av1/common/txb_common.c
+++ b/av1/common/txb_common.c
@@ -122,6 +122,21 @@
 #if CONFIG_LV_MAP_MULTI
   for (tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
     for (plane = 0; plane < PLANE_TYPES; ++plane) {
+#if USE_BASE_EOB_ALPHABET
+      for (ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx) {
+        int p = fc->coeff_base[tx_size][plane][0][SIG_COEF_CONTEXTS -
+                                                  SIG_COEF_CONTEXTS_EOB + ctx] *
+                128;
+        fc->coeff_base_eob_cdf[tx_size][plane][ctx][0] = AOM_ICDF(p);
+        p += ((32768 - p) *
+              fc->coeff_base[tx_size][plane][1][SIG_COEF_CONTEXTS -
+                                                SIG_COEF_CONTEXTS_EOB + ctx]) >>
+             8;
+        fc->coeff_base_eob_cdf[tx_size][plane][ctx][1] = AOM_ICDF(p);
+        fc->coeff_base_eob_cdf[tx_size][plane][ctx][2] = AOM_ICDF(32768);
+        fc->coeff_base_eob_cdf[tx_size][plane][ctx][3] = 0;
+      }
+#endif
       for (ctx = 0; ctx < COEFF_BASE_CONTEXTS; ++ctx) {
         int p = fc->nz_map[tx_size][plane][ctx] * 128;
         fc->coeff_base_cdf[tx_size][plane][ctx][0] = AOM_ICDF(p);
diff --git a/av1/decoder/decodetxb.c b/av1/decoder/decodetxb.c
index f7a7035..1826ae5 100644
--- a/av1/decoder/decodetxb.c
+++ b/av1/decoder/decodetxb.c
@@ -170,10 +170,27 @@
 #if CONFIG_LV_MAP_MULTI
     int coeff_ctx =
         get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, c == *eob - 1);
+#if USE_BASE_EOB_ALPHABET
+    int level;
+    if (c == *eob - 1) {
+      level = av1_read_record_symbol(
+                  counts, r,
+                  ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type]
+                                            [coeff_ctx - SIG_COEF_CONTEXTS +
+                                             SIG_COEF_CONTEXTS_EOB],
+                  3, ACCT_STR) +
+              1;
+    } else {
+      level = av1_read_record_symbol(
+          counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], 4,
+          ACCT_STR);
+    }
+#else
     int level = av1_read_record_symbol(
         counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], 4,
         ACCT_STR);
-    levels[get_paded_idx(pos, bwl)] = level;
+#endif
+    levels[get_paded_idx(scan[c], bwl)] = level;
     if (level) *max_scan_line = AOMMAX(*max_scan_line, scan[c]);
     // printf("base_cdf: %d %d %2d\n", txs_ctx, plane_type, coeff_ctx);
     // printf("base_cdf: %d %d %2d : %3d %3d %3d\n", txs_ctx, plane_type,
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 2fd67f1..0b14b48 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -64,6 +64,9 @@
 typedef struct {
   int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
 #if CONFIG_LV_MAP_MULTI
+#if USE_BASE_EOB_ALPHABET
+  int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
+#endif
   int base_cost[SIG_COEF_CONTEXTS][4];
 #else
   int nz_map_cost[SIG_COEF_CONTEXTS][2];
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 2e1e1ed..2f7481a 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -383,8 +383,23 @@
     coeff_ctx =
         get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, c == eob - 1);
     tran_low_t v = tcoeff[scan[c]];
+#if USE_BASE_EOB_ALPHABET
+    if (c == eob - 1) {
+      aom_write_symbol(
+          w, AOMMIN(abs(v), 3) - 1,
+          ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type]
+                                    [coeff_ctx - SIG_COEF_CONTEXTS +
+                                     SIG_COEF_CONTEXTS_EOB],
+          3);
+    } else {
+      aom_write_symbol(w, AOMMIN(abs(v), 3),
+                       ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
+                       4);
+    }
+#else
     aom_write_symbol(w, AOMMIN(abs(v), 3),
                      ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], 4);
+#endif
 #else
     coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
     tran_low_t v = tcoeff[scan[c]];
@@ -675,7 +690,17 @@
 #if CONFIG_LV_MAP_MULTI
     coeff_ctx =
         get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, c == eob - 1);
+#if USE_BASE_EOB_ALPHABET
+    if (c == eob - 1) {
+      cost += coeff_costs
+                  ->base_eob_cost[coeff_ctx - SIG_COEF_CONTEXTS +
+                                  SIG_COEF_CONTEXTS_EOB][AOMMIN(level, 3) - 1];
+    } else {
+      cost += coeff_costs->base_cost[coeff_ctx][AOMMIN(level, 3)];
+    }
+#else
     cost += coeff_costs->base_cost[coeff_ctx][AOMMIN(level, 3)];
+#endif
 #else  // CONFIG_LV_MAP_MULTI
 #if USE_CAUSAL_BASE_CTX
     coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
@@ -1519,7 +1544,18 @@
   int coeff_ctx =
       get_nz_map_ctx(txb_info->levels, scan_idx, scan, txb_info->bwl,
                      txb_info->height, txb_info->tx_type, is_eob);
+#if USE_BASE_EOB_ALPHABET
+  if (is_eob) {
+    cost +=
+        txb_costs->base_eob_cost[coeff_ctx - SIG_COEF_CONTEXTS +
+                                 SIG_COEF_CONTEXTS_EOB][AOMMIN(abs_qc, 3) - 1];
+  } else {
+    cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
+  }
+
+#else
   cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
+#endif
 #else
 #if USE_CAUSAL_BASE_CTX
   int coeff_ctx =
@@ -2199,9 +2235,20 @@
     (void)nz_map_count;
     coeff_ctx =
         get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, c == eob - 1);
+#if USE_BASE_EOB_ALPHABET
+    if (c == eob - 1) {
+      update_cdf(ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type]
+                                           [coeff_ctx - SIG_COEF_CONTEXTS +
+                                            SIG_COEF_CONTEXTS_EOB],
+                 AOMMIN(abs(v), 3) - 1, 3);
+    } else {
+      update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
+                 AOMMIN(abs(v), 3), 4);
+    }
+#else
     update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
                AOMMIN(abs(v), 3), 4);
-
+#endif
 #elif USE_CAUSAL_BASE_CTX
     coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
 
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 5571602..2d7e5aa 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -459,6 +459,12 @@
                                  fc->txb_skip_cdf[tx_size][ctx], NULL);
 
 #if CONFIG_LV_MAP_MULTI
+#if USE_BASE_EOB_ALPHABET
+      for (int ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->base_eob_cost[ctx],
+                                 fc->coeff_base_eob_cdf[tx_size][plane][ctx],
+                                 NULL);
+#endif
       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
         av1_cost_tokens_from_cdf(pcost->base_cost[ctx],
                                  fc->coeff_base_cdf[tx_size][plane][ctx], NULL);