Merge "Remove redundant bsize mapping in motion search"
diff --git a/vp9/common/vp9_default_coef_probs.h b/vp9/common/vp9_default_coef_probs.h
deleted file mode 100644
index 29909ec..0000000
--- a/vp9/common/vp9_default_coef_probs.h
+++ /dev/null
@@ -1,347 +0,0 @@
-/*
- *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
-*/
-#ifndef VP9_COMMON_DEFAULT_COEF_PROBS_H_
-#define VP9_COMMON_DEFAULT_COEF_PROBS_H_
-
-/*Generated file, included by vp9_entropy.c*/
-static const vp9_coeff_probs_model default_coef_probs_4x4[BLOCK_TYPES] = {
-  {  // Y plane
-    {  // Intra
-      {  // Band 0
-        { 195,  29, 183 }, {  84,  49, 136 }, {   8,  42,  71 }
-      }, {  // Band 1
-        {  31, 107, 169 }, {  35,  99, 159 }, {  17,  82, 140 },
-        {   8,  66, 114 }, {   2,  44,  76 }, {   1,  19,  32 }
-      }, {  // Band 2
-        {  40, 132, 201 }, {  29, 114, 187 }, {  13,  91, 157 },
-        {   7,  75, 127 }, {   3,  58,  95 }, {   1,  28,  47 }
-      }, {  // Band 3
-        {  69, 142, 221 }, {  42, 122, 201 }, {  15,  91, 159 },
-        {   6,  67, 121 }, {   1,  42,  77 }, {   1,  17,  31 }
-      }, {  // Band 4
-        { 102, 148, 228 }, {  67, 117, 204 }, {  17,  82, 154 },
-        {   6,  59, 114 }, {   2,  39,  75 }, {   1,  15,  29 }
-      }, {  // Band 5
-        { 156,  57, 233 }, { 119,  57, 212 }, {  58,  48, 163 },
-        {  29,  40, 124 }, {  12,  30,  81 }, {   3,  12,  31 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 191, 107, 226 }, { 124, 117, 204 }, {  25,  99, 155 }
-      }, {  // Band 1
-        {  29, 148, 210 }, {  37, 126, 194 }, {   8,  93, 157 },
-        {   2,  68, 118 }, {   1,  39,  69 }, {   1,  17,  33 }
-      }, {  // Band 2
-        {  41, 151, 213 }, {  27, 123, 193 }, {   3,  82, 144 },
-        {   1,  58, 105 }, {   1,  32,  60 }, {   1,  13,  26 }
-      }, {  // Band 3
-        {  59, 159, 220 }, {  23, 126, 198 }, {   4,  88, 151 },
-        {   1,  66, 114 }, {   1,  38,  71 }, {   1,  18,  34 }
-      }, {  // Band 4
-        { 114, 136, 232 }, {  51, 114, 207 }, {  11,  83, 155 },
-        {   3,  56, 105 }, {   1,  33,  65 }, {   1,  17,  34 }
-      }, {  // Band 5
-        { 149,  65, 234 }, { 121,  57, 215 }, {  61,  49, 166 },
-        {  28,  36, 114 }, {  12,  25,  76 }, {   3,  16,  42 }
-      }
-    }
-  }, {  // UV plane
-    {  // Intra
-      {  // Band 0
-        { 214,  49, 220 }, { 132,  63, 188 }, {  42,  65, 137 }
-      }, {  // Band 1
-        {  85, 137, 221 }, { 104, 131, 216 }, {  49, 111, 192 },
-        {  21,  87, 155 }, {   2,  49,  87 }, {   1,  16,  28 }
-      }, {  // Band 2
-        {  89, 163, 230 }, {  90, 137, 220 }, {  29, 100, 183 },
-        {  10,  70, 135 }, {   2,  42,  81 }, {   1,  17,  33 }
-      }, {  // Band 3
-        { 108, 167, 237 }, {  55, 133, 222 }, {  15,  97, 179 },
-        {   4,  72, 135 }, {   1,  45,  85 }, {   1,  19,  38 }
-      }, {  // Band 4
-        { 124, 146, 240 }, {  66, 124, 224 }, {  17,  88, 175 },
-        {   4,  58, 122 }, {   1,  36,  75 }, {   1,  18,  37 }
-      }, {  //  Band 5
-        { 141,  79, 241 }, { 126,  70, 227 }, {  66,  58, 182 },
-        {  30,  44, 136 }, {  12,  34,  96 }, {   2,  20,  47 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 229,  99, 249 }, { 143, 111, 235 }, {  46, 109, 192 }
-      }, {  // Band 1
-        {  82, 158, 236 }, {  94, 146, 224 }, {  25, 117, 191 },
-        {   9,  87, 149 }, {   3,  56,  99 }, {   1,  33,  57 }
-      }, {  // Band 2
-        {  83, 167, 237 }, {  68, 145, 222 }, {  10, 103, 177 },
-        {   2,  72, 131 }, {   1,  41,  79 }, {   1,  20,  39 }
-      }, {  // Band 3
-        {  99, 167, 239 }, {  47, 141, 224 }, {  10, 104, 178 },
-        {   2,  73, 133 }, {   1,  44,  85 }, {   1,  22,  47 }
-      }, {  // Band 4
-        { 127, 145, 243 }, {  71, 129, 228 }, {  17,  93, 177 },
-        {   3,  61, 124 }, {   1,  41,  84 }, {   1,  21,  52 }
-      }, {  // Band 5
-        { 157,  78, 244 }, { 140,  72, 231 }, {  69,  58, 184 },
-        {  31,  44, 137 }, {  14,  38, 105 }, {   8,  23,  61 }
-      }
-    }
-  }
-};
-static const vp9_coeff_probs_model default_coef_probs_8x8[BLOCK_TYPES] = {
-  {  // Y plane
-    {  // Intra
-      {  // Band 0
-        { 125,  34, 187 }, {  52,  41, 133 }, {   6,  31,  56 }
-      }, {  // Band 1
-        {  37, 109, 153 }, {  51, 102, 147 }, {  23,  87, 128 },
-        {   8,  67, 101 }, {   1,  41,  63 }, {   1,  19,  29 }
-      }, {  // Band 2
-        {  31, 154, 185 }, {  17, 127, 175 }, {   6,  96, 145 },
-        {   2,  73, 114 }, {   1,  51,  82 }, {   1,  28,  45 }
-      }, {  // Band 3
-        {  23, 163, 200 }, {  10, 131, 185 }, {   2,  93, 148 },
-        {   1,  67, 111 }, {   1,  41,  69 }, {   1,  14,  24 }
-      }, {  // Band 4
-        {  29, 176, 217 }, {  12, 145, 201 }, {   3, 101, 156 },
-        {   1,  69, 111 }, {   1,  39,  63 }, {   1,  14,  23 }
-      }, {  // Band 5
-        {  57, 192, 233 }, {  25, 154, 215 }, {   6, 109, 167 },
-        {   3,  78, 118 }, {   1,  48,  69 }, {   1,  21,  29 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 202, 105, 245 }, { 108, 106, 216 }, {  18,  90, 144 }
-      }, {  // Band 1
-        {  33, 172, 219 }, {  64, 149, 206 }, {  14, 117, 177 },
-        {   5,  90, 141 }, {   2,  61,  95 }, {   1,  37,  57 }
-      }, {  // Band 2
-        {  33, 179, 220 }, {  11, 140, 198 }, {   1,  89, 148 },
-        {   1,  60, 104 }, {   1,  33,  57 }, {   1,  12,  21 }
-      }, {  // Band 3
-        {  30, 181, 221 }, {   8, 141, 198 }, {   1,  87, 145 },
-        {   1,  58, 100 }, {   1,  31,  55 }, {   1,  12,  20 }
-      }, {  // Band 4
-        {  32, 186, 224 }, {   7, 142, 198 }, {   1,  86, 143 },
-        {   1,  58, 100 }, {   1,  31,  55 }, {   1,  12,  22 }
-      }, {  // Band 5
-        {  57, 192, 227 }, {  20, 143, 204 }, {   3,  96, 154 },
-        {   1,  68, 112 }, {   1,  42,  69 }, {   1,  19,  32 }
-      }
-    }
-  }, {  // UV plane
-    {  // Intra
-      {  // Band 0
-        { 212,  35, 215 }, { 113,  47, 169 }, {  29,  48, 105 }
-      }, {  // Band 1
-        {  74, 129, 203 }, { 106, 120, 203 }, {  49, 107, 178 },
-        {  19,  84, 144 }, {   4,  50,  84 }, {   1,  15,  25 }
-      }, {  // Band 2
-        {  71, 172, 217 }, {  44, 141, 209 }, {  15, 102, 173 },
-        {   6,  76, 133 }, {   2,  51,  89 }, {   1,  24,  42 }
-      }, {  // Band 3
-        {  64, 185, 231 }, {  31, 148, 216 }, {   8, 103, 175 },
-        {   3,  74, 131 }, {   1,  46,  81 }, {   1,  18,  30 }
-      }, {  // Band 4
-        {  65, 196, 235 }, {  25, 157, 221 }, {   5, 105, 174 },
-        {   1,  67, 120 }, {   1,  38,  69 }, {   1,  15,  30 }
-      }, {  // Band 5
-        {  65, 204, 238 }, {  30, 156, 224 }, {   7, 107, 177 },
-        {   2,  70, 124 }, {   1,  42,  73 }, {   1,  18,  34 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 225,  86, 251 }, { 144, 104, 235 }, {  42,  99, 181 }
-      }, {  // Band 1
-        {  85, 175, 239 }, { 112, 165, 229 }, {  29, 136, 200 },
-        {  12, 103, 162 }, {   6,  77, 123 }, {   2,  53,  84 }
-      }, {  // Band 2
-        {  75, 183, 239 }, {  30, 155, 221 }, {   3, 106, 171 },
-        {   1,  74, 128 }, {   1,  44,  76 }, {   1,  17,  28 }
-      }, {  // Band 3
-        {  73, 185, 240 }, {  27, 159, 222 }, {   2, 107, 172 },
-        {   1,  75, 127 }, {   1,  42,  73 }, {   1,  17,  29 }
-      }, {  // Band 4
-        {  62, 190, 238 }, {  21, 159, 222 }, {   2, 107, 172 },
-        {   1,  72, 122 }, {   1,  40,  71 }, {   1,  18,  32 }
-      }, {  // Band 5
-        {  61, 199, 240 }, {  27, 161, 226 }, {   4, 113, 180 },
-        {   1,  76, 129 }, {   1,  46,  80 }, {   1,  23,  41 }
-      }
-    }
-  }
-};
-static const vp9_coeff_probs_model default_coef_probs_16x16[BLOCK_TYPES] = {
-  {  // Y plane
-    {  // Intra
-      {  // Band 0
-        {   7,  27, 153 }, {   5,  30,  95 }, {   1,  16,  30 }
-      }, {  // Band 1
-        {  50,  75, 127 }, {  57,  75, 124 }, {  27,  67, 108 },
-        {  10,  54,  86 }, {   1,  33,  52 }, {   1,  12,  18 }
-      }, {  // Band 2
-        {  43, 125, 151 }, {  26, 108, 148 }, {   7,  83, 122 },
-        {   2,  59,  89 }, {   1,  38,  60 }, {   1,  17,  27 }
-      }, {  // Band 3
-        {  23, 144, 163 }, {  13, 112, 154 }, {   2,  75, 117 },
-        {   1,  50,  81 }, {   1,  31,  51 }, {   1,  14,  23 }
-      }, {  // Band 4
-        {  18, 162, 185 }, {   6, 123, 171 }, {   1,  78, 125 },
-        {   1,  51,  86 }, {   1,  31,  54 }, {   1,  14,  23 }
-      }, {  // Band 5
-        {  15, 199, 227 }, {   3, 150, 204 }, {   1,  91, 146 },
-        {   1,  55,  95 }, {   1,  30,  53 }, {   1,  11,  20 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        {  19,  55, 240 }, {  19,  59, 196 }, {   3,  52, 105 }
-      }, {  // Band 1
-        {  41, 166, 207 }, { 104, 153, 199 }, {  31, 123, 181 },
-        {  14, 101, 152 }, {   5,  72, 106 }, {   1,  36,  52 }
-      }, {  // Band 2
-        {  35, 176, 211 }, {  12, 131, 190 }, {   2,  88, 144 },
-        {   1,  60, 101 }, {   1,  36,  60 }, {   1,  16,  28 }
-      }, {  // Band 3
-        {  28, 183, 213 }, {   8, 134, 191 }, {   1,  86, 142 },
-        {   1,  56,  96 }, {   1,  30,  53 }, {   1,  12,  20 }
-      }, {  // Band 4
-        {  20, 190, 215 }, {   4, 135, 192 }, {   1,  84, 139 },
-        {   1,  53,  91 }, {   1,  28,  49 }, {   1,  11,  20 }
-      }, {  // Band 5
-        {  13, 196, 216 }, {   2, 137, 192 }, {   1,  86, 143 },
-        {   1,  57,  99 }, {   1,  32,  56 }, {   1,  13,  24 }
-      }
-    }
-  }, {  // UV plane
-    {  // Intra
-      {  // Band 0
-        { 211,  29, 217 }, {  96,  47, 156 }, {  22,  43,  87 }
-      }, {  // Band 1
-        {  78, 120, 193 }, { 111, 116, 186 }, {  46, 102, 164 },
-        {  15,  80, 128 }, {   2,  49,  76 }, {   1,  18,  28 }
-      }, {  // Band 2
-        {  71, 161, 203 }, {  42, 132, 192 }, {  10,  98, 150 },
-        {   3,  69, 109 }, {   1,  44,  70 }, {   1,  18,  29 }
-      }, {  // Band 3
-        {  57, 186, 211 }, {  30, 140, 196 }, {   4,  93, 146 },
-        {   1,  62, 102 }, {   1,  38,  65 }, {   1,  16,  27 }
-      }, {  // Band 4
-        {  47, 199, 217 }, {  14, 145, 196 }, {   1,  88, 142 },
-        {   1,  57,  98 }, {   1,  36,  62 }, {   1,  15,  26 }
-      }, {  // Band 5
-        {  26, 219, 229 }, {   5, 155, 207 }, {   1,  94, 151 },
-        {   1,  60, 104 }, {   1,  36,  62 }, {   1,  16,  28 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 233,  29, 248 }, { 146,  47, 220 }, {  43,  52, 140 }
-      }, {  // Band 1
-        { 100, 163, 232 }, { 179, 161, 222 }, {  63, 142, 204 },
-        {  37, 113, 174 }, {  26,  89, 137 }, {  18,  68,  97 }
-      }, {  // Band 2
-        {  85, 181, 230 }, {  32, 146, 209 }, {   7, 100, 164 },
-        {   3,  71, 121 }, {   1,  45,  77 }, {   1,  18,  30 }
-      }, {  // Band 3
-        {  65, 187, 230 }, {  20, 148, 207 }, {   2,  97, 159 },
-        {   1,  68, 116 }, {   1,  40,  70 }, {   1,  14,  29 }
-      }, {  // Band 4
-        {  40, 194, 227 }, {   8, 147, 204 }, {   1,  94, 155 },
-        {   1,  65, 112 }, {   1,  39,  66 }, {   1,  14,  26 }
-      }, {  // Band 5
-        {  16, 208, 228 }, {   3, 151, 207 }, {   1,  98, 160 },
-        {   1,  67, 117 }, {   1,  41,  74 }, {   1,  17,  31 }
-      }
-    }
-  }
-};
-static const vp9_coeff_probs_model default_coef_probs_32x32[BLOCK_TYPES] = {
-  {  // Y plane
-    {  // Intra
-      {  // Band 0
-        {  17,  38, 140 }, {   7,  34,  80 }, {   1,  17,  29 }
-      }, {  // Band 1
-        {  37,  75, 128 }, {  41,  76, 128 }, {  26,  66, 116 },
-        {  12,  52,  94 }, {   2,  32,  55 }, {   1,  10,  16 }
-      }, {  // Band 2
-        {  50, 127, 154 }, {  37, 109, 152 }, {  16,  82, 121 },
-        {   5,  59,  85 }, {   1,  35,  54 }, {   1,  13,  20 }
-      }, {  // Band 3
-        {  40, 142, 167 }, {  17, 110, 157 }, {   2,  71, 112 },
-        {   1,  44,  72 }, {   1,  27,  45 }, {   1,  11,  17 }
-      }, {  // Band 4
-        {  30, 175, 188 }, {   9, 124, 169 }, {   1,  74, 116 },
-        {   1,  48,  78 }, {   1,  30,  49 }, {   1,  11,  18 }
-      }, {  // Band 5
-        {  10, 222, 223 }, {   2, 150, 194 }, {   1,  83, 128 },
-        {   1,  48,  79 }, {   1,  27,  45 }, {   1,  11,  17 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        {  36,  41, 235 }, {  29,  36, 193 }, {  10,  27, 111 }
-      }, {  // Band 1
-        {  85, 165, 222 }, { 177, 162, 215 }, { 110, 135, 195 },
-        {  57, 113, 168 }, {  23,  83, 120 }, {  10,  49,  61 }
-      }, {  // Band 2
-        {  85, 190, 223 }, {  36, 139, 200 }, {   5,  90, 146 },
-        {   1,  60, 103 }, {   1,  38,  65 }, {   1,  18,  30 }
-      }, {  // Band 3
-        {  72, 202, 223 }, {  23, 141, 199 }, {   2,  86, 140 },
-        {   1,  56,  97 }, {   1,  36,  61 }, {   1,  16,  27 }
-      }, {  // Band 4
-        {  55, 218, 225 }, {  13, 145, 200 }, {   1,  86, 141 },
-        {   1,  57,  99 }, {   1,  35,  61 }, {   1,  13,  22 }
-      }, {  // Band 5
-        {  15, 235, 212 }, {   1, 132, 184 }, {   1,  84, 139 },
-        {   1,  57,  97 }, {   1,  34,  56 }, {   1,  14,  23 }
-      }
-    }
-  }, {  // UV plane
-    {  // Intra
-      {  // Band 0
-        { 181,  21, 201 }, {  61,  37, 123 }, {  10,  38,  71 }
-      }, {  // Band 1
-        {  47, 106, 172 }, {  95, 104, 173 }, {  42,  93, 159 },
-        {  18,  77, 131 }, {   4,  50,  81 }, {   1,  17,  23 }
-      }, {  // Band 2
-        {  62, 147, 199 }, {  44, 130, 189 }, {  28, 102, 154 },
-        {  18,  75, 115 }, {   2,  44,  65 }, {   1,  12,  19 }
-      }, {  // Band 3
-        {  55, 153, 210 }, {  24, 130, 194 }, {   3,  93, 146 },
-        {   1,  61,  97 }, {   1,  31,  50 }, {   1,  10,  16 }
-      }, {  // Band 4
-        {  49, 186, 223 }, {  17, 148, 204 }, {   1,  96, 142 },
-        {   1,  53,  83 }, {   1,  26,  44 }, {   1,  11,  17 }
-      }, {  // Band 5
-        {  13, 217, 212 }, {   2, 136, 180 }, {   1,  78, 124 },
-        {   1,  50,  83 }, {   1,  29,  49 }, {   1,  14,  23 }
-      }
-    }, {  // Inter
-      {  // Band 0
-        { 197,  13, 247 }, {  82,  17, 222 }, {  25,  17, 162 }
-      }, {  // Band 1
-        { 126, 186, 247 }, { 234, 191, 243 }, { 176, 177, 234 },
-        { 104, 158, 220 }, {  66, 128, 186 }, {  55,  90, 137 }
-      }, {  // Band 2
-        { 111, 197, 242 }, {  46, 158, 219 }, {   9, 104, 171 },
-        {   2,  65, 125 }, {   1,  44,  80 }, {   1,  17,  91 }
-      }, {  // Band 3
-        { 104, 208, 245 }, {  39, 168, 224 }, {   3, 109, 162 },
-        {   1,  79, 124 }, {   1,  50, 102 }, {   1,  43, 102 }
-      }, {  // Band 4
-        {  84, 220, 246 }, {  31, 177, 231 }, {   2, 115, 180 },
-        {   1,  79, 134 }, {   1,  55,  77 }, {   1,  60,  79 }
-      }, {  // Band 5
-        {  43, 243, 240 }, {   8, 180, 217 }, {   1, 115, 166 },
-        {   1,  84, 121 }, {   1,  51,  67 }, {   1,  16,   6 }
-      }
-    }
-  }
-};
-
-#endif  // VP9_COMMON_DEFAULT_COEF_PROBS_H_
diff --git a/vp9/common/vp9_entropy.c b/vp9/common/vp9_entropy.c
index 8d33f33..cce05d0 100644
--- a/vp9/common/vp9_entropy.c
+++ b/vp9/common/vp9_entropy.c
@@ -35,8 +35,7 @@
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
-DECLARE_ALIGNED(16, const uint8_t,
-                vp9_coefband_trans_8x8plus[1024]) = {
+DECLARE_ALIGNED(16, const uint8_t, vp9_coefband_trans_8x8plus[1024]) = {
   0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
   4, 4, 4, 4, 4, 5,
   // beyond MAXBAND_INDEX+1 all values are filled as 5
@@ -131,8 +130,8 @@
 
 // Every odd line in this table can be generated from the even lines
 // by averaging :
-// vp9_pareto8_full[l][node] = ( vp9_pareto8_full[l-1][node] +
-//                               vp9_pareto8_full[l+1][node] ) >> 1;
+// vp9_pareto8_full[l][node] = (vp9_pareto8_full[l-1][node] +
+//                              vp9_pareto8_full[l+1][node] ) >> 1;
 const vp9_prob vp9_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES] = {
   {  3,  86, 128,   6,  86,  23,  88,  29},
   {  6,  86, 128,  11,  87,  42,  91,  52},
@@ -392,6 +391,342 @@
   {255, 246, 247, 255, 239, 255, 253, 255},
 };
 
+static const vp9_coeff_probs_model default_coef_probs_4x4[BLOCK_TYPES] = {
+  {  // Y plane
+    {  // Intra
+      {  // Band 0
+        { 195,  29, 183 }, {  84,  49, 136 }, {   8,  42,  71 }
+      }, {  // Band 1
+        {  31, 107, 169 }, {  35,  99, 159 }, {  17,  82, 140 },
+        {   8,  66, 114 }, {   2,  44,  76 }, {   1,  19,  32 }
+      }, {  // Band 2
+        {  40, 132, 201 }, {  29, 114, 187 }, {  13,  91, 157 },
+        {   7,  75, 127 }, {   3,  58,  95 }, {   1,  28,  47 }
+      }, {  // Band 3
+        {  69, 142, 221 }, {  42, 122, 201 }, {  15,  91, 159 },
+        {   6,  67, 121 }, {   1,  42,  77 }, {   1,  17,  31 }
+      }, {  // Band 4
+        { 102, 148, 228 }, {  67, 117, 204 }, {  17,  82, 154 },
+        {   6,  59, 114 }, {   2,  39,  75 }, {   1,  15,  29 }
+      }, {  // Band 5
+        { 156,  57, 233 }, { 119,  57, 212 }, {  58,  48, 163 },
+        {  29,  40, 124 }, {  12,  30,  81 }, {   3,  12,  31 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 191, 107, 226 }, { 124, 117, 204 }, {  25,  99, 155 }
+      }, {  // Band 1
+        {  29, 148, 210 }, {  37, 126, 194 }, {   8,  93, 157 },
+        {   2,  68, 118 }, {   1,  39,  69 }, {   1,  17,  33 }
+      }, {  // Band 2
+        {  41, 151, 213 }, {  27, 123, 193 }, {   3,  82, 144 },
+        {   1,  58, 105 }, {   1,  32,  60 }, {   1,  13,  26 }
+      }, {  // Band 3
+        {  59, 159, 220 }, {  23, 126, 198 }, {   4,  88, 151 },
+        {   1,  66, 114 }, {   1,  38,  71 }, {   1,  18,  34 }
+      }, {  // Band 4
+        { 114, 136, 232 }, {  51, 114, 207 }, {  11,  83, 155 },
+        {   3,  56, 105 }, {   1,  33,  65 }, {   1,  17,  34 }
+      }, {  // Band 5
+        { 149,  65, 234 }, { 121,  57, 215 }, {  61,  49, 166 },
+        {  28,  36, 114 }, {  12,  25,  76 }, {   3,  16,  42 }
+      }
+    }
+  }, {  // UV plane
+    {  // Intra
+      {  // Band 0
+        { 214,  49, 220 }, { 132,  63, 188 }, {  42,  65, 137 }
+      }, {  // Band 1
+        {  85, 137, 221 }, { 104, 131, 216 }, {  49, 111, 192 },
+        {  21,  87, 155 }, {   2,  49,  87 }, {   1,  16,  28 }
+      }, {  // Band 2
+        {  89, 163, 230 }, {  90, 137, 220 }, {  29, 100, 183 },
+        {  10,  70, 135 }, {   2,  42,  81 }, {   1,  17,  33 }
+      }, {  // Band 3
+        { 108, 167, 237 }, {  55, 133, 222 }, {  15,  97, 179 },
+        {   4,  72, 135 }, {   1,  45,  85 }, {   1,  19,  38 }
+      }, {  // Band 4
+        { 124, 146, 240 }, {  66, 124, 224 }, {  17,  88, 175 },
+        {   4,  58, 122 }, {   1,  36,  75 }, {   1,  18,  37 }
+      }, {  //  Band 5
+        { 141,  79, 241 }, { 126,  70, 227 }, {  66,  58, 182 },
+        {  30,  44, 136 }, {  12,  34,  96 }, {   2,  20,  47 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 229,  99, 249 }, { 143, 111, 235 }, {  46, 109, 192 }
+      }, {  // Band 1
+        {  82, 158, 236 }, {  94, 146, 224 }, {  25, 117, 191 },
+        {   9,  87, 149 }, {   3,  56,  99 }, {   1,  33,  57 }
+      }, {  // Band 2
+        {  83, 167, 237 }, {  68, 145, 222 }, {  10, 103, 177 },
+        {   2,  72, 131 }, {   1,  41,  79 }, {   1,  20,  39 }
+      }, {  // Band 3
+        {  99, 167, 239 }, {  47, 141, 224 }, {  10, 104, 178 },
+        {   2,  73, 133 }, {   1,  44,  85 }, {   1,  22,  47 }
+      }, {  // Band 4
+        { 127, 145, 243 }, {  71, 129, 228 }, {  17,  93, 177 },
+        {   3,  61, 124 }, {   1,  41,  84 }, {   1,  21,  52 }
+      }, {  // Band 5
+        { 157,  78, 244 }, { 140,  72, 231 }, {  69,  58, 184 },
+        {  31,  44, 137 }, {  14,  38, 105 }, {   8,  23,  61 }
+      }
+    }
+  }
+};
+
+static const vp9_coeff_probs_model default_coef_probs_8x8[BLOCK_TYPES] = {
+  {  // Y plane
+    {  // Intra
+      {  // Band 0
+        { 125,  34, 187 }, {  52,  41, 133 }, {   6,  31,  56 }
+      }, {  // Band 1
+        {  37, 109, 153 }, {  51, 102, 147 }, {  23,  87, 128 },
+        {   8,  67, 101 }, {   1,  41,  63 }, {   1,  19,  29 }
+      }, {  // Band 2
+        {  31, 154, 185 }, {  17, 127, 175 }, {   6,  96, 145 },
+        {   2,  73, 114 }, {   1,  51,  82 }, {   1,  28,  45 }
+      }, {  // Band 3
+        {  23, 163, 200 }, {  10, 131, 185 }, {   2,  93, 148 },
+        {   1,  67, 111 }, {   1,  41,  69 }, {   1,  14,  24 }
+      }, {  // Band 4
+        {  29, 176, 217 }, {  12, 145, 201 }, {   3, 101, 156 },
+        {   1,  69, 111 }, {   1,  39,  63 }, {   1,  14,  23 }
+      }, {  // Band 5
+        {  57, 192, 233 }, {  25, 154, 215 }, {   6, 109, 167 },
+        {   3,  78, 118 }, {   1,  48,  69 }, {   1,  21,  29 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 202, 105, 245 }, { 108, 106, 216 }, {  18,  90, 144 }
+      }, {  // Band 1
+        {  33, 172, 219 }, {  64, 149, 206 }, {  14, 117, 177 },
+        {   5,  90, 141 }, {   2,  61,  95 }, {   1,  37,  57 }
+      }, {  // Band 2
+        {  33, 179, 220 }, {  11, 140, 198 }, {   1,  89, 148 },
+        {   1,  60, 104 }, {   1,  33,  57 }, {   1,  12,  21 }
+      }, {  // Band 3
+        {  30, 181, 221 }, {   8, 141, 198 }, {   1,  87, 145 },
+        {   1,  58, 100 }, {   1,  31,  55 }, {   1,  12,  20 }
+      }, {  // Band 4
+        {  32, 186, 224 }, {   7, 142, 198 }, {   1,  86, 143 },
+        {   1,  58, 100 }, {   1,  31,  55 }, {   1,  12,  22 }
+      }, {  // Band 5
+        {  57, 192, 227 }, {  20, 143, 204 }, {   3,  96, 154 },
+        {   1,  68, 112 }, {   1,  42,  69 }, {   1,  19,  32 }
+      }
+    }
+  }, {  // UV plane
+    {  // Intra
+      {  // Band 0
+        { 212,  35, 215 }, { 113,  47, 169 }, {  29,  48, 105 }
+      }, {  // Band 1
+        {  74, 129, 203 }, { 106, 120, 203 }, {  49, 107, 178 },
+        {  19,  84, 144 }, {   4,  50,  84 }, {   1,  15,  25 }
+      }, {  // Band 2
+        {  71, 172, 217 }, {  44, 141, 209 }, {  15, 102, 173 },
+        {   6,  76, 133 }, {   2,  51,  89 }, {   1,  24,  42 }
+      }, {  // Band 3
+        {  64, 185, 231 }, {  31, 148, 216 }, {   8, 103, 175 },
+        {   3,  74, 131 }, {   1,  46,  81 }, {   1,  18,  30 }
+      }, {  // Band 4
+        {  65, 196, 235 }, {  25, 157, 221 }, {   5, 105, 174 },
+        {   1,  67, 120 }, {   1,  38,  69 }, {   1,  15,  30 }
+      }, {  // Band 5
+        {  65, 204, 238 }, {  30, 156, 224 }, {   7, 107, 177 },
+        {   2,  70, 124 }, {   1,  42,  73 }, {   1,  18,  34 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 225,  86, 251 }, { 144, 104, 235 }, {  42,  99, 181 }
+      }, {  // Band 1
+        {  85, 175, 239 }, { 112, 165, 229 }, {  29, 136, 200 },
+        {  12, 103, 162 }, {   6,  77, 123 }, {   2,  53,  84 }
+      }, {  // Band 2
+        {  75, 183, 239 }, {  30, 155, 221 }, {   3, 106, 171 },
+        {   1,  74, 128 }, {   1,  44,  76 }, {   1,  17,  28 }
+      }, {  // Band 3
+        {  73, 185, 240 }, {  27, 159, 222 }, {   2, 107, 172 },
+        {   1,  75, 127 }, {   1,  42,  73 }, {   1,  17,  29 }
+      }, {  // Band 4
+        {  62, 190, 238 }, {  21, 159, 222 }, {   2, 107, 172 },
+        {   1,  72, 122 }, {   1,  40,  71 }, {   1,  18,  32 }
+      }, {  // Band 5
+        {  61, 199, 240 }, {  27, 161, 226 }, {   4, 113, 180 },
+        {   1,  76, 129 }, {   1,  46,  80 }, {   1,  23,  41 }
+      }
+    }
+  }
+};
+
+static const vp9_coeff_probs_model default_coef_probs_16x16[BLOCK_TYPES] = {
+  {  // Y plane
+    {  // Intra
+      {  // Band 0
+        {   7,  27, 153 }, {   5,  30,  95 }, {   1,  16,  30 }
+      }, {  // Band 1
+        {  50,  75, 127 }, {  57,  75, 124 }, {  27,  67, 108 },
+        {  10,  54,  86 }, {   1,  33,  52 }, {   1,  12,  18 }
+      }, {  // Band 2
+        {  43, 125, 151 }, {  26, 108, 148 }, {   7,  83, 122 },
+        {   2,  59,  89 }, {   1,  38,  60 }, {   1,  17,  27 }
+      }, {  // Band 3
+        {  23, 144, 163 }, {  13, 112, 154 }, {   2,  75, 117 },
+        {   1,  50,  81 }, {   1,  31,  51 }, {   1,  14,  23 }
+      }, {  // Band 4
+        {  18, 162, 185 }, {   6, 123, 171 }, {   1,  78, 125 },
+        {   1,  51,  86 }, {   1,  31,  54 }, {   1,  14,  23 }
+      }, {  // Band 5
+        {  15, 199, 227 }, {   3, 150, 204 }, {   1,  91, 146 },
+        {   1,  55,  95 }, {   1,  30,  53 }, {   1,  11,  20 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        {  19,  55, 240 }, {  19,  59, 196 }, {   3,  52, 105 }
+      }, {  // Band 1
+        {  41, 166, 207 }, { 104, 153, 199 }, {  31, 123, 181 },
+        {  14, 101, 152 }, {   5,  72, 106 }, {   1,  36,  52 }
+      }, {  // Band 2
+        {  35, 176, 211 }, {  12, 131, 190 }, {   2,  88, 144 },
+        {   1,  60, 101 }, {   1,  36,  60 }, {   1,  16,  28 }
+      }, {  // Band 3
+        {  28, 183, 213 }, {   8, 134, 191 }, {   1,  86, 142 },
+        {   1,  56,  96 }, {   1,  30,  53 }, {   1,  12,  20 }
+      }, {  // Band 4
+        {  20, 190, 215 }, {   4, 135, 192 }, {   1,  84, 139 },
+        {   1,  53,  91 }, {   1,  28,  49 }, {   1,  11,  20 }
+      }, {  // Band 5
+        {  13, 196, 216 }, {   2, 137, 192 }, {   1,  86, 143 },
+        {   1,  57,  99 }, {   1,  32,  56 }, {   1,  13,  24 }
+      }
+    }
+  }, {  // UV plane
+    {  // Intra
+      {  // Band 0
+        { 211,  29, 217 }, {  96,  47, 156 }, {  22,  43,  87 }
+      }, {  // Band 1
+        {  78, 120, 193 }, { 111, 116, 186 }, {  46, 102, 164 },
+        {  15,  80, 128 }, {   2,  49,  76 }, {   1,  18,  28 }
+      }, {  // Band 2
+        {  71, 161, 203 }, {  42, 132, 192 }, {  10,  98, 150 },
+        {   3,  69, 109 }, {   1,  44,  70 }, {   1,  18,  29 }
+      }, {  // Band 3
+        {  57, 186, 211 }, {  30, 140, 196 }, {   4,  93, 146 },
+        {   1,  62, 102 }, {   1,  38,  65 }, {   1,  16,  27 }
+      }, {  // Band 4
+        {  47, 199, 217 }, {  14, 145, 196 }, {   1,  88, 142 },
+        {   1,  57,  98 }, {   1,  36,  62 }, {   1,  15,  26 }
+      }, {  // Band 5
+        {  26, 219, 229 }, {   5, 155, 207 }, {   1,  94, 151 },
+        {   1,  60, 104 }, {   1,  36,  62 }, {   1,  16,  28 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 233,  29, 248 }, { 146,  47, 220 }, {  43,  52, 140 }
+      }, {  // Band 1
+        { 100, 163, 232 }, { 179, 161, 222 }, {  63, 142, 204 },
+        {  37, 113, 174 }, {  26,  89, 137 }, {  18,  68,  97 }
+      }, {  // Band 2
+        {  85, 181, 230 }, {  32, 146, 209 }, {   7, 100, 164 },
+        {   3,  71, 121 }, {   1,  45,  77 }, {   1,  18,  30 }
+      }, {  // Band 3
+        {  65, 187, 230 }, {  20, 148, 207 }, {   2,  97, 159 },
+        {   1,  68, 116 }, {   1,  40,  70 }, {   1,  14,  29 }
+      }, {  // Band 4
+        {  40, 194, 227 }, {   8, 147, 204 }, {   1,  94, 155 },
+        {   1,  65, 112 }, {   1,  39,  66 }, {   1,  14,  26 }
+      }, {  // Band 5
+        {  16, 208, 228 }, {   3, 151, 207 }, {   1,  98, 160 },
+        {   1,  67, 117 }, {   1,  41,  74 }, {   1,  17,  31 }
+      }
+    }
+  }
+};
+
+static const vp9_coeff_probs_model default_coef_probs_32x32[BLOCK_TYPES] = {
+  {  // Y plane
+    {  // Intra
+      {  // Band 0
+        {  17,  38, 140 }, {   7,  34,  80 }, {   1,  17,  29 }
+      }, {  // Band 1
+        {  37,  75, 128 }, {  41,  76, 128 }, {  26,  66, 116 },
+        {  12,  52,  94 }, {   2,  32,  55 }, {   1,  10,  16 }
+      }, {  // Band 2
+        {  50, 127, 154 }, {  37, 109, 152 }, {  16,  82, 121 },
+        {   5,  59,  85 }, {   1,  35,  54 }, {   1,  13,  20 }
+      }, {  // Band 3
+        {  40, 142, 167 }, {  17, 110, 157 }, {   2,  71, 112 },
+        {   1,  44,  72 }, {   1,  27,  45 }, {   1,  11,  17 }
+      }, {  // Band 4
+        {  30, 175, 188 }, {   9, 124, 169 }, {   1,  74, 116 },
+        {   1,  48,  78 }, {   1,  30,  49 }, {   1,  11,  18 }
+      }, {  // Band 5
+        {  10, 222, 223 }, {   2, 150, 194 }, {   1,  83, 128 },
+        {   1,  48,  79 }, {   1,  27,  45 }, {   1,  11,  17 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        {  36,  41, 235 }, {  29,  36, 193 }, {  10,  27, 111 }
+      }, {  // Band 1
+        {  85, 165, 222 }, { 177, 162, 215 }, { 110, 135, 195 },
+        {  57, 113, 168 }, {  23,  83, 120 }, {  10,  49,  61 }
+      }, {  // Band 2
+        {  85, 190, 223 }, {  36, 139, 200 }, {   5,  90, 146 },
+        {   1,  60, 103 }, {   1,  38,  65 }, {   1,  18,  30 }
+      }, {  // Band 3
+        {  72, 202, 223 }, {  23, 141, 199 }, {   2,  86, 140 },
+        {   1,  56,  97 }, {   1,  36,  61 }, {   1,  16,  27 }
+      }, {  // Band 4
+        {  55, 218, 225 }, {  13, 145, 200 }, {   1,  86, 141 },
+        {   1,  57,  99 }, {   1,  35,  61 }, {   1,  13,  22 }
+      }, {  // Band 5
+        {  15, 235, 212 }, {   1, 132, 184 }, {   1,  84, 139 },
+        {   1,  57,  97 }, {   1,  34,  56 }, {   1,  14,  23 }
+      }
+    }
+  }, {  // UV plane
+    {  // Intra
+      {  // Band 0
+        { 181,  21, 201 }, {  61,  37, 123 }, {  10,  38,  71 }
+      }, {  // Band 1
+        {  47, 106, 172 }, {  95, 104, 173 }, {  42,  93, 159 },
+        {  18,  77, 131 }, {   4,  50,  81 }, {   1,  17,  23 }
+      }, {  // Band 2
+        {  62, 147, 199 }, {  44, 130, 189 }, {  28, 102, 154 },
+        {  18,  75, 115 }, {   2,  44,  65 }, {   1,  12,  19 }
+      }, {  // Band 3
+        {  55, 153, 210 }, {  24, 130, 194 }, {   3,  93, 146 },
+        {   1,  61,  97 }, {   1,  31,  50 }, {   1,  10,  16 }
+      }, {  // Band 4
+        {  49, 186, 223 }, {  17, 148, 204 }, {   1,  96, 142 },
+        {   1,  53,  83 }, {   1,  26,  44 }, {   1,  11,  17 }
+      }, {  // Band 5
+        {  13, 217, 212 }, {   2, 136, 180 }, {   1,  78, 124 },
+        {   1,  50,  83 }, {   1,  29,  49 }, {   1,  14,  23 }
+      }
+    }, {  // Inter
+      {  // Band 0
+        { 197,  13, 247 }, {  82,  17, 222 }, {  25,  17, 162 }
+      }, {  // Band 1
+        { 126, 186, 247 }, { 234, 191, 243 }, { 176, 177, 234 },
+        { 104, 158, 220 }, {  66, 128, 186 }, {  55,  90, 137 }
+      }, {  // Band 2
+        { 111, 197, 242 }, {  46, 158, 219 }, {   9, 104, 171 },
+        {   2,  65, 125 }, {   1,  44,  80 }, {   1,  17,  91 }
+      }, {  // Band 3
+        { 104, 208, 245 }, {  39, 168, 224 }, {   3, 109, 162 },
+        {   1,  79, 124 }, {   1,  50, 102 }, {   1,  43, 102 }
+      }, {  // Band 4
+        {  84, 220, 246 }, {  31, 177, 231 }, {   2, 115, 180 },
+        {   1,  79, 134 }, {   1,  55,  77 }, {   1,  60,  79 }
+      }, {  // Band 5
+        {  43, 243, 240 }, {   8, 180, 217 }, {   1, 115, 166 },
+        {   1,  84, 121 }, {   1,  51,  67 }, {   1,  16,   6 }
+      }
+    }
+  }
+};
+
 static void extend_to_full_distribution(vp9_prob *probs, vp9_prob p) {
   vpx_memcpy(probs, vp9_pareto8_full[p = 0 ? 0 : p - 1],
              MODEL_NODES * sizeof(vp9_prob));
@@ -403,8 +738,6 @@
   extend_to_full_distribution(&full[UNCONSTRAINED_NODES], model[PIVOT_NODE]);
 }
 
-#include "vp9/common/vp9_default_coef_probs.h"
-
 void vp9_default_coef_probs(VP9_COMMON *cm) {
   vp9_copy(cm->fc.coef_probs[TX_4X4], default_coef_probs_4x4);
   vp9_copy(cm->fc.coef_probs[TX_8X8], default_coef_probs_8x8);
@@ -427,7 +760,7 @@
   vp9_coeff_probs_model *dst_coef_probs = cm->fc.coef_probs[tx_size];
   const vp9_coeff_probs_model *pre_coef_probs = pre_fc->coef_probs[tx_size];
   vp9_coeff_count_model *coef_counts = cm->counts.coef[tx_size];
-  unsigned int (*eob_branch_count)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
+  unsigned int (*eob_branch_count)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] =
       cm->counts.eob_branch[tx_size];
   int i, j, k, l, m;
   unsigned int branch_ct[UNCONSTRAINED_NODES][2];
@@ -435,9 +768,7 @@
   for (i = 0; i < BLOCK_TYPES; ++i)
     for (j = 0; j < REF_TYPES; ++j)
       for (k = 0; k < COEF_BANDS; ++k)
-        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
-          if (l >= 3 && k == 0)
-            continue;
+        for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
           vp9_tree_probs_from_distribution(vp9_coefmodel_tree, branch_ct,
                                            coef_counts[i][j][k][l]);
           branch_ct[0][1] = eob_branch_count[i][j][k][l] - branch_ct[0][0];
diff --git a/vp9/common/vp9_entropy.h b/vp9/common/vp9_entropy.h
index bd037d7..4560bf8 100644
--- a/vp9/common/vp9_entropy.h
+++ b/vp9/common/vp9_entropy.h
@@ -82,13 +82,14 @@
    coefficient band (and since zigzag positions 0, 1, and 2 are in
    distinct bands). */
 
-#define PREV_COEF_CONTEXTS          6
+#define COEFF_CONTEXTS 6
+#define BAND_COEFF_CONTEXTS(band) ((band) == 0 ? 3 : COEFF_CONTEXTS)
 
 // #define ENTROPY_STATS
 
-typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
+typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
                                     [ENTROPY_TOKENS];
-typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS]
+typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
                                     [ENTROPY_NODES][2];
 
 #define SUBEXP_PARAM                4   /* Subexponential code parameter */
@@ -137,11 +138,10 @@
 extern const vp9_prob vp9_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
 
 typedef vp9_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS]
-                                      [PREV_COEF_CONTEXTS]
-                                      [UNCONSTRAINED_NODES];
+                                      [COEFF_CONTEXTS][UNCONSTRAINED_NODES];
 
 typedef unsigned int vp9_coeff_count_model[REF_TYPES][COEF_BANDS]
-                                          [PREV_COEF_CONTEXTS]
+                                          [COEFF_CONTEXTS]
                                           [UNCONSTRAINED_NODES + 1];
 
 void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full);
diff --git a/vp9/common/vp9_onyxc_int.h b/vp9/common/vp9_onyxc_int.h
index 751accf..dc6d1b9 100644
--- a/vp9/common/vp9_onyxc_int.h
+++ b/vp9/common/vp9_onyxc_int.h
@@ -66,7 +66,7 @@
   unsigned int partition[PARTITION_CONTEXTS][PARTITION_TYPES];
   vp9_coeff_count_model coef[TX_SIZES][BLOCK_TYPES];
   unsigned int eob_branch[TX_SIZES][BLOCK_TYPES][REF_TYPES]
-                         [COEF_BANDS][PREV_COEF_CONTEXTS];
+                         [COEF_BANDS][COEFF_CONTEXTS];
   unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
                                 [SWITCHABLE_FILTERS];
   unsigned int inter_mode[INTER_MODE_CONTEXTS][INTER_MODES];
@@ -238,8 +238,10 @@
 }
 
 static void ref_cnt_fb(int *buf, int *idx, int new_idx) {
-  if (buf[*idx] > 0)
-    buf[*idx]--;
+  const int ref_index = *idx;
+
+  if (ref_index >= 0 && buf[ref_index] > 0)
+    buf[ref_index]--;
 
   *idx = new_idx;
 
diff --git a/vp9/common/vp9_pred_common.c b/vp9/common/vp9_pred_common.c
index 22b66b5..a367f0b 100644
--- a/vp9/common/vp9_pred_common.c
+++ b/vp9/common/vp9_pred_common.c
@@ -369,31 +369,22 @@
 // left of the entries corresponding to real blocks.
 // The prediction flags in these dummy entries are initialized to 0.
 unsigned char vp9_get_pred_context_tx_size(const MACROBLOCKD *xd) {
-  const MODE_INFO *const above_mi = get_above_mi(xd);
-  const MODE_INFO *const left_mi = get_left_mi(xd);
-  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
-  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
-  const int above_in_image = above_mi != NULL;
-  const int left_in_image = left_mi != NULL;
   const int max_tx_size = max_txsize_lookup[xd->mi_8x8[0]->mbmi.sb_type];
-  int above_context = max_tx_size;
-  int left_context = max_tx_size;
+  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(get_above_mi(xd));
+  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(get_left_mi(xd));
+  const int has_above = above_mbmi != NULL;
+  const int has_left = left_mbmi != NULL;
+  int above_ctx = (has_above && !above_mbmi->skip_coeff) ? above_mbmi->tx_size
+                                                         : max_tx_size;
+  int left_ctx = (has_left && !left_mbmi->skip_coeff) ? left_mbmi->tx_size
+                                                      : max_tx_size;
+  if (!has_left)
+    left_ctx = above_ctx;
 
-  if (above_in_image)
-    above_context = above_mbmi->skip_coeff ? max_tx_size
-                                           : above_mbmi->tx_size;
+  if (!has_above)
+    above_ctx = left_ctx;
 
-  if (left_in_image)
-    left_context = left_mbmi->skip_coeff ? max_tx_size
-                                         : left_mbmi->tx_size;
-
-  if (!left_in_image)
-    left_context = above_context;
-
-  if (!above_in_image)
-    above_context = left_context;
-
-  return above_context + left_context > max_tx_size;
+  return (above_ctx + left_ctx) > max_tx_size;
 }
 
 int vp9_get_segment_id(VP9_COMMON *cm, const uint8_t *segment_ids,
diff --git a/vp9/common/vp9_treecoder.c b/vp9/common/vp9_treecoder.c
index e2a5b9f..dca3076 100644
--- a/vp9/common/vp9_treecoder.c
+++ b/vp9/common/vp9_treecoder.c
@@ -14,27 +14,6 @@
 #include "./vpx_config.h"
 #include "vp9/common/vp9_treecoder.h"
 
-static void tree2tok(struct vp9_token *const p, vp9_tree t,
-                    int i, int v, int l) {
-  v += v;
-  ++l;
-
-  do {
-    const vp9_tree_index j = t[i++];
-
-    if (j <= 0) {
-      p[-j].value = v;
-      p[-j].len = l;
-    } else {
-      tree2tok(p, t, j, v, l);
-    }
-  } while (++v & 1);
-}
-
-void vp9_tokens_from_tree(struct vp9_token *p, vp9_tree t) {
-  tree2tok(p, t, 0, 0, 0);
-}
-
 static unsigned int convert_distribution(unsigned int i, vp9_tree tree,
                                          unsigned int branch_ct[][2],
                                          const unsigned int num_events[]) {
diff --git a/vp9/common/vp9_treecoder.h b/vp9/common/vp9_treecoder.h
index a79b156..bbe4e8f 100644
--- a/vp9/common/vp9_treecoder.h
+++ b/vp9/common/vp9_treecoder.h
@@ -34,15 +34,6 @@
 
 typedef const vp9_tree_index vp9_tree[];
 
-struct vp9_token {
-  int value;
-  int len;
-};
-
-/* Construct encoding array from tree. */
-
-void vp9_tokens_from_tree(struct vp9_token*, vp9_tree);
-
 /* Convert array of token occurrence counts into a table of probabilities
    for the associated binary encoding tree.  Also writes count of branches
    taken for each node on the tree; this facilitiates decisions as to
diff --git a/vp9/decoder/vp9_decodeframe.c b/vp9/decoder/vp9_decodeframe.c
index 82bace0..c5ed6c8 100644
--- a/vp9/decoder/vp9_decodeframe.c
+++ b/vp9/decoder/vp9_decodeframe.c
@@ -537,13 +537,12 @@
   int i, j, k, l, m;
 
   if (vp9_read_bit(r))
-    for (i = 0; i < BLOCK_TYPES; i++)
-      for (j = 0; j < REF_TYPES; j++)
-        for (k = 0; k < COEF_BANDS; k++)
-          for (l = 0; l < PREV_COEF_CONTEXTS; l++)
-            if (k > 0 || l < 3)
-              for (m = 0; m < UNCONSTRAINED_NODES; m++)
-                vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
+    for (i = 0; i < BLOCK_TYPES; ++i)
+      for (j = 0; j < REF_TYPES; ++j)
+        for (k = 0; k < COEF_BANDS; ++k)
+          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
+            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
+              vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
 }
 
 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
@@ -687,12 +686,6 @@
 
   if (cm->width != width || cm->height != height) {
     // Change in frame size.
-    if (cm->width == 0 || cm->height == 0) {
-      // Assign new frame buffer on first call.
-      cm->new_fb_idx = NUM_YV12_BUFFERS - 1;
-      cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1;
-    }
-
     // TODO(agrange) Don't test width/height, check overall size.
     if (width > cm->width || height > cm->height) {
       // Rescale frame buffers only if they're not big enough already.
diff --git a/vp9/decoder/vp9_detokenize.c b/vp9/decoder/vp9_detokenize.c
index c9d73d4..63f1731 100644
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -13,11 +13,8 @@
 
 #include "vp9/common/vp9_blockd.h"
 #include "vp9/common/vp9_common.h"
-#include "vp9/common/vp9_seg_common.h"
 
-#include "vp9/decoder/vp9_dboolhuff.h"
 #include "vp9/decoder/vp9_detokenize.h"
-#include "vp9/decoder/vp9_onyxd_int.h"
 
 #define EOB_CONTEXT_NODE            0
 #define ZERO_CONTEXT_NODE           1
@@ -63,47 +60,45 @@
 #define INCREMENT_COUNT(token)                              \
   do {                                                      \
      if (!cm->frame_parallel_decoding_mode)                 \
-       ++coef_counts[band][pt][token];                      \
+       ++coef_counts[band][ctx][token];                     \
   } while (0)
 
-
 #define WRITE_COEF_CONTINUE(val, token)                  \
   {                                                      \
-    v = (val * dqv) >> dq_shift; \
-    dqcoeff_ptr[scan[c]] = (vp9_read_bit(r) ? -v : v); \
+    v = (val * dqv) >> dq_shift;                         \
+    dqcoeff[scan[c]] = vp9_read_bit(r) ? -v : v;         \
     token_cache[scan[c]] = vp9_pt_energy_class[token];   \
     ++c;                                                 \
-    pt = get_coef_context(nb, token_cache, c);           \
-    dqv = dq[1];                                          \
+    ctx = get_coef_context(nb, token_cache, c);          \
+    dqv = dq[1];                                         \
     continue;                                            \
   }
 
-
 #define ADJUST_COEF(prob, bits_count)                   \
   do {                                                  \
     val += (vp9_read(r, prob) << bits_count);           \
   } while (0)
 
-static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd,
-                        vp9_reader *r, int block_idx,
-                        PLANE_TYPE type, int max_eob, int16_t *dqcoeff_ptr,
-                        TX_SIZE tx_size, const int16_t *dq, int pt) {
+static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd, int block,
+                        PLANE_TYPE type, int16_t *dqcoeff, TX_SIZE tx_size,
+                        const int16_t *dq, int ctx, vp9_reader *r) {
+  const int max_eob = 16 << (tx_size << 1);
   const FRAME_CONTEXT *const fc = &cm->fc;
   FRAME_COUNTS *const counts = &cm->counts;
   const int ref = is_inter_block(&xd->mi_8x8[0]->mbmi);
   int band, c = 0;
-  const vp9_prob (*coef_probs)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES] =
+  const vp9_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
       fc->coef_probs[tx_size][type][ref];
   const vp9_prob *prob;
-  unsigned int (*coef_counts)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
+  unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
       counts->coef[tx_size][type][ref];
-  unsigned int (*eob_branch_count)[PREV_COEF_CONTEXTS] =
+  unsigned int (*eob_branch_count)[COEFF_CONTEXTS] =
       counts->eob_branch[tx_size][type][ref];
   uint8_t token_cache[32 * 32];
   const uint8_t *cat6;
   const uint8_t *band_translate = get_band_translate(tx_size);
   const int dq_shift = (tx_size == TX_32X32);
-  const scan_order *so = get_scan(xd, tx_size, type, block_idx);
+  const scan_order *so = get_scan(xd, tx_size, type, block);
   const int16_t *scan = so->scan;
   const int16_t *nb = so->neighbors;
   int v;
@@ -112,9 +107,9 @@
   while (c < max_eob) {
     int val;
     band = *band_translate++;
-    prob = coef_probs[band][pt];
+    prob = coef_probs[band][ctx];
     if (!cm->frame_parallel_decoding_mode)
-      ++eob_branch_count[band][pt];
+      ++eob_branch_count[band][ctx];
     if (!vp9_read(r, prob[EOB_CONTEXT_NODE])) {
       INCREMENT_COUNT(EOB_MODEL_TOKEN);
       break;
@@ -127,9 +122,9 @@
       ++c;
       if (c >= max_eob)
         return c;  // zero tokens at the end (no eob token)
-      pt = get_coef_context(nb, token_cache, c);
+      ctx = get_coef_context(nb, token_cache, c);
       band = *band_translate++;
-      prob = coef_probs[band][pt];
+      prob = coef_probs[band][ctx];
     }
 
     // ONE_CONTEXT_NODE_0_
@@ -205,13 +200,11 @@
                             int plane, int block, BLOCK_SIZE plane_bsize,
                             int x, int y, TX_SIZE tx_size, vp9_reader *r) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
-  const int seg_eob = get_tx_eob(&cm->seg, xd->mi_8x8[0]->mbmi.segment_id,
-                                 tx_size);
-  const int pt = get_entropy_context(tx_size, pd->above_context + x,
-                                              pd->left_context + y);
-  const int eob = decode_coefs(cm, xd, r, block, pd->plane_type, seg_eob,
+  const int ctx = get_entropy_context(tx_size, pd->above_context + x,
+                                               pd->left_context + y);
+  const int eob = decode_coefs(cm, xd, block, pd->plane_type,
                                BLOCK_OFFSET(pd->dqcoeff, block), tx_size,
-                               pd->dequant, pt);
+                               pd->dequant, ctx, r);
   set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y);
   return eob;
 }
diff --git a/vp9/decoder/vp9_onyxd_if.c b/vp9/decoder/vp9_onyxd_if.c
index 25fb3d6..4c0cd45 100644
--- a/vp9/decoder/vp9_onyxd_if.c
+++ b/vp9/decoder/vp9_onyxd_if.c
@@ -125,6 +125,9 @@
 
   vp9_zero(*pbi);
 
+  // Initialize the references to not point to any frame buffers.
+  memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
+
   if (setjmp(cm->error.jmp)) {
     cm->error.setjmp = 0;
     vp9_remove_decompressor(pbi);
diff --git a/vp9/encoder/vp9_bitstream.c b/vp9/encoder/vp9_bitstream.c
index 97717fb..276bb13 100644
--- a/vp9/encoder/vp9_bitstream.c
+++ b/vp9/encoder/vp9_bitstream.c
@@ -553,7 +553,7 @@
 static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size) {
   vp9_coeff_probs_model *coef_probs = cpi->frame_coef_probs[tx_size];
   vp9_coeff_count *coef_counts = cpi->coef_counts[tx_size];
-  unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
+  unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] =
       cpi->common.counts.eob_branch[tx_size];
   vp9_coeff_stats *coef_branch_ct = cpi->frame_branch_ct[tx_size];
   int i, j, k, l, m;
@@ -561,9 +561,7 @@
   for (i = 0; i < BLOCK_TYPES; ++i) {
     for (j = 0; j < REF_TYPES; ++j) {
       for (k = 0; k < COEF_BANDS; ++k) {
-        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
-          if (l >= 3 && k == 0)
-            continue;
+        for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
           vp9_tree_probs_from_distribution(vp9_coef_tree,
                                            coef_branch_ct[i][j][k][l],
                                            coef_counts[i][j][k][l]);
@@ -606,15 +604,12 @@
       for (i = 0; i < BLOCK_TYPES; ++i) {
         for (j = 0; j < REF_TYPES; ++j) {
           for (k = 0; k < COEF_BANDS; ++k) {
-            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
+            for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
               for (t = 0; t < entropy_nodes_update; ++t) {
                 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
                 const vp9_prob oldp = old_frame_coef_probs[i][j][k][l][t];
                 int s;
                 int u = 0;
-
-                if (l >= 3 && k == 0)
-                  continue;
                 if (t == PIVOT_NODE)
                   s = vp9_prob_diff_update_savings_search_model(
                       frame_branch_ct[i][j][k][l][0],
@@ -645,7 +640,7 @@
       for (i = 0; i < BLOCK_TYPES; ++i) {
         for (j = 0; j < REF_TYPES; ++j) {
           for (k = 0; k < COEF_BANDS; ++k) {
-            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
+            for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
               // calc probs and branch cts for this frame only
               for (t = 0; t < entropy_nodes_update; ++t) {
                 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
@@ -653,8 +648,6 @@
                 const vp9_prob upd = DIFF_UPDATE_PROB;
                 int s;
                 int u = 0;
-                if (l >= 3 && k == 0)
-                  continue;
                 if (t == PIVOT_NODE)
                   s = vp9_prob_diff_update_savings_search_model(
                       frame_branch_ct[i][j][k][l][0],
@@ -686,25 +679,23 @@
     case 1:
     case 2: {
       const int prev_coef_contexts_to_update =
-          (cpi->sf.use_fast_coef_updates == 2 ?
-           PREV_COEF_CONTEXTS >> 1 : PREV_COEF_CONTEXTS);
+          cpi->sf.use_fast_coef_updates == 2 ? COEFF_CONTEXTS >> 1
+                                             : COEFF_CONTEXTS;
       const int coef_band_to_update =
-          (cpi->sf.use_fast_coef_updates == 2 ?
-           COEF_BANDS >> 1 : COEF_BANDS);
+          cpi->sf.use_fast_coef_updates == 2 ? COEF_BANDS >> 1
+                                             : COEF_BANDS;
       int updates = 0;
       int noupdates_before_first = 0;
       for (i = 0; i < BLOCK_TYPES; ++i) {
         for (j = 0; j < REF_TYPES; ++j) {
           for (k = 0; k < COEF_BANDS; ++k) {
-            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
+            for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
               // calc probs and branch cts for this frame only
               for (t = 0; t < entropy_nodes_update; ++t) {
                 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
                 vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t;
                 int s;
                 int u = 0;
-                if (l >= 3 && k == 0)
-                  continue;
                 if (l >= prev_coef_contexts_to_update ||
                     k >= coef_band_to_update) {
                   u = 0;
diff --git a/vp9/encoder/vp9_block.h b/vp9/encoder/vp9_block.h
index a34a929..f9a0226 100644
--- a/vp9/encoder/vp9_block.h
+++ b/vp9/encoder/vp9_block.h
@@ -87,7 +87,7 @@
 /* The [2] dimension is for whether we skip the EOB node (i.e. if previous
  * coefficient in this block was zero) or not. */
 typedef unsigned int vp9_coeff_cost[BLOCK_TYPES][REF_TYPES][COEF_BANDS][2]
-                                   [PREV_COEF_CONTEXTS][ENTROPY_TOKENS];
+                                   [COEFF_CONTEXTS][ENTROPY_TOKENS];
 
 typedef struct macroblock MACROBLOCK;
 struct macroblock {
diff --git a/vp9/encoder/vp9_encodeframe.c b/vp9/encoder/vp9_encodeframe.c
index f53c3c9..bc71d02 100644
--- a/vp9/encoder/vp9_encodeframe.c
+++ b/vp9/encoder/vp9_encodeframe.c
@@ -2592,27 +2592,19 @@
                       context, &cm->counts.tx)[mbmi->tx_size];
     } else {
       int x, y;
-      TX_SIZE sz = tx_mode_to_biggest_tx_size[cm->tx_mode];
-      assert(sizeof(tx_mode_to_biggest_tx_size) /
-             sizeof(tx_mode_to_biggest_tx_size[0]) == TX_MODES);
+      TX_SIZE tx_size;
       // The new intra coding scheme requires no change of transform size
       if (is_inter_block(&mi->mbmi)) {
-        if (sz == TX_32X32 && bsize < BLOCK_32X32)
-          sz = TX_16X16;
-        if (sz == TX_16X16 && bsize < BLOCK_16X16)
-          sz = TX_8X8;
-        if (sz == TX_8X8 && bsize < BLOCK_8X8)
-          sz = TX_4X4;
-      } else if (bsize >= BLOCK_8X8) {
-        sz = mbmi->tx_size;
+        tx_size = MIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
+                      max_txsize_lookup[bsize]);
       } else {
-        sz = TX_4X4;
+        tx_size = (bsize >= BLOCK_8X8) ? mbmi->tx_size : TX_4X4;
       }
 
       for (y = 0; y < mi_height; y++)
         for (x = 0; x < mi_width; x++)
           if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
-            mi_8x8[mis * y + x]->mbmi.tx_size = sz;
+            mi_8x8[mis * y + x]->mbmi.tx_size = tx_size;
     }
   }
 }
diff --git a/vp9/encoder/vp9_firstpass.c b/vp9/encoder/vp9_firstpass.c
index 30e18a3..60830d0 100644
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -2170,21 +2170,6 @@
           (int)(cpi->twopass.bits_left / frames_left);
       cpi->twopass.est_max_qcorrection_factor = 1.0;
 
-      // Set a cq_level in constrained quality mode.
-      // Commenting this code out for now since it does not seem to be
-      // working well.
-      /*
-      if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
-        int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
-           section_target_bandwidth);
-
-        if (est_cq > cpi->cq_target_quality)
-          cpi->cq_target_quality = est_cq;
-        else
-          cpi->cq_target_quality = cpi->oxcf.cq_level;
-      }
-      */
-
       // guess at maxq needed in 2nd pass
       cpi->twopass.maxq_max_limit = cpi->rc.worst_quality;
       cpi->twopass.maxq_min_limit = cpi->rc.best_quality;
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index 7c36637..d40fc66 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -143,8 +143,9 @@
   }
 }
 
-static void set_mvcost(VP9_COMP *cpi) {
+static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
   MACROBLOCK *const mb = &cpi->mb;
+  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
   if (cpi->common.allow_high_precision_mv) {
     mb->mvcost = mb->nmvcost_hp;
     mb->mvsadcost = mb->nmvsadcost_hp;
@@ -234,7 +235,7 @@
 // Computes a q delta (in "q index" terms) to get from a starting q value
 // to a target value
 // target q value
-int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
+int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
   int i;
   int start_index = cpi->rc.worst_quality;
   int target_index = cpi->rc.worst_quality;
@@ -1115,8 +1116,8 @@
   vp9_change_config(ptr, oxcf);
 
   // Initialize active best and worst q and average q values.
-  cpi->rc.active_worst_quality         = cpi->oxcf.worst_allowed_q;
-  cpi->rc.active_best_quality          = cpi->oxcf.best_allowed_q;
+  cpi->rc.active_worst_quality      = cpi->oxcf.worst_allowed_q;
+
   cpi->rc.avg_frame_qindex          = cpi->oxcf.worst_allowed_q;
 
   // Initialise the starting buffer levels
@@ -1205,8 +1206,7 @@
   cm->reset_frame_context = 0;
 
   setup_features(cm);
-  cpi->common.allow_high_precision_mv = 0;  // Default mv precision
-  set_mvcost(cpi);
+  set_high_precision_mv(cpi, 0);
 
   {
     int i;
@@ -1253,12 +1253,8 @@
 
   // active values should only be modified if out of new range
   cpi->rc.active_worst_quality = clamp(cpi->rc.active_worst_quality,
-                                    cpi->oxcf.best_allowed_q,
-                                    cpi->oxcf.worst_allowed_q);
-
-  cpi->rc.active_best_quality = clamp(cpi->rc.active_best_quality,
-                                   cpi->oxcf.best_allowed_q,
-                                   cpi->oxcf.worst_allowed_q);
+                                       cpi->rc.best_quality,
+                                       cpi->rc.worst_quality);
 
   cpi->cq_target_quality = cpi->oxcf.cq_level;
 
@@ -2369,16 +2365,6 @@
   return i;
 }
 
-static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest,
-                        unsigned int *frame_flags) {
-  (void) size;
-  (void) dest;
-  (void) frame_flags;
-
-  vp9_set_quantizer(cpi, find_fp_qindex());
-  vp9_first_pass(cpi);
-}
-
 #define WRITE_RECON_BUFFER 0
 #if WRITE_RECON_BUFFER
 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
@@ -2470,16 +2456,9 @@
           cpi->rc.projected_frame_size <
           ((cpi->rc.this_frame_target * 7) >> 3)) {
         force_recode = 1;
-      } else if (q > cpi->oxcf.cq_level &&
-                 cpi->rc.projected_frame_size < cpi->rc.min_frame_bandwidth &&
-                 cpi->rc.active_best_quality > cpi->oxcf.cq_level) {
-        // Severe undershoot and between auto and user cq level
-        force_recode = 1;
-        cpi->rc.active_best_quality = cpi->oxcf.cq_level;
       }
     }
   }
-
   return force_recode;
 }
 
@@ -2614,17 +2593,15 @@
   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
 }
 
-static void full_to_model_counts(
-    vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) {
+static void full_to_model_counts(vp9_coeff_count_model *model_count,
+                                 vp9_coeff_count *full_count) {
   int i, j, k, l;
+
   for (i = 0; i < BLOCK_TYPES; ++i)
     for (j = 0; j < REF_TYPES; ++j)
       for (k = 0; k < COEF_BANDS; ++k)
-        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
-          if (l >= 3 && k == 0)
-            continue;
+        for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
-        }
 }
 
 #if 0 && CONFIG_INTERNAL_STATS
@@ -2639,7 +2616,7 @@
 
   if (cpi->twopass.total_left_stats.coded_error != 0.0)
     fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d"
-        "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
+        "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
         "%6d %6d %5d %5d %5d %8.2f %10d %10.3f"
         "%10.3f %8d %10d %10d %10d\n",
         cpi->common.current_video_frame, cpi->rc.this_frame_target,
@@ -2650,7 +2627,6 @@
         (int)cpi->rc.total_actual_bits, cm->base_qindex,
         vp9_convert_qindex_to_q(cm->base_qindex),
         (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
-        vp9_convert_qindex_to_q(cpi->rc.active_best_quality),
         vp9_convert_qindex_to_q(cpi->rc.active_worst_quality), cpi->rc.avg_q,
         vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
         vp9_convert_qindex_to_q(cpi->cq_target_quality),
@@ -2699,6 +2675,7 @@
   int overshoot_seen = 0;
   int undershoot_seen = 0;
   int q_low = bottom_index, q_high = top_index;
+
   do {
     vp9_clear_system_state();  // __asm emms;
 
@@ -2816,11 +2793,13 @@
             // Update rate_correction_factor unless
             vp9_rc_update_rate_correction_factors(cpi, 0);
 
-            *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target);
+            *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
+                                   bottom_index, top_index);
 
             while (*q < q_low && retries < 10) {
               vp9_rc_update_rate_correction_factors(cpi, 0);
-              *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target);
+              *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
+                                     bottom_index, top_index);
               retries++;
             }
           }
@@ -2831,18 +2810,12 @@
           q_high = *q > q_low ? *q - 1 : q_low;
 
           if (overshoot_seen || loop_count > 1) {
-            // Update rate_correction_factor unless
-            // cpi->rc.active_worst_quality has changed.
             vp9_rc_update_rate_correction_factors(cpi, 1);
-
             *q = (q_high + q_low) / 2;
           } else {
-            // Update rate_correction_factor unless
-            // cpi->rc.active_worst_quality has changed.
             vp9_rc_update_rate_correction_factors(cpi, 0);
-
-            *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target);
-
+            *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
+                                   bottom_index, top_index);
             // Special case reset for qlow for constrained quality.
             // This should only trigger where there is very substantial
             // undershoot on a frame and the auto cq level is above
@@ -2854,7 +2827,8 @@
 
             while (*q > q_high && retries < 10) {
               vp9_rc_update_rate_correction_factors(cpi, 0);
-              *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target);
+              *q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
+                                     bottom_index, top_index);
               retries++;
             }
           }
@@ -2893,8 +2867,8 @@
   int q;
   int frame_over_shoot_limit;
   int frame_under_shoot_limit;
-
   int top_index;
+  int top_index_prop;
   int bottom_index;
 
   SPEED_FEATURES *const sf = &cpi->sf;
@@ -3008,45 +2982,8 @@
 
   vp9_clear_system_state();
 
-  // Decide how big to make the frame.
-  vp9_rc_pick_frame_size_and_bounds(cpi,
-                                    &frame_under_shoot_limit,
-                                    &frame_over_shoot_limit);
-
-  q = vp9_rc_pick_q_and_adjust_q_bounds(cpi,
-                                        &bottom_index,
-                                        &top_index);
-
-#if CONFIG_MULTIPLE_ARF
-  // Force the quantizer determined by the coding order pattern.
-  if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
-      cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) {
-    double new_q;
-    double current_q = vp9_convert_qindex_to_q(cpi->rc.active_worst_quality);
-    int level = cpi->this_frame_weight;
-    assert(level >= 0);
-
-    // Set quantizer steps at 10% increments.
-    new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
-    q = cpi->rc.active_worst_quality +
-        vp9_compute_qdelta(cpi, current_q, new_q);
-
-    bottom_index = q;
-    top_index    = q;
-
-    printf("frame:%d q:%d\n", cm->current_video_frame, q);
-  }
-#endif
-
   vp9_zero(cpi->rd_tx_select_threshes);
 
-  if (!frame_is_intra_only(cm)) {
-    cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
-    /* TODO: Decide this more intelligently */
-    cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH;
-    set_mvcost(cpi);
-  }
-
 #if CONFIG_VP9_POSTPROC
   if (cpi->oxcf.noise_sensitivity > 0) {
     int l = 0;
@@ -3076,6 +3013,26 @@
   vp9_write_yuv_frame(cpi->Source);
 #endif
 
+  // Decide how big to make the frame.
+  vp9_rc_pick_frame_size_target(cpi);
+
+  // Decide frame size bounds
+  vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
+                                   &frame_under_shoot_limit,
+                                   &frame_over_shoot_limit);
+
+  // Decide q and q bounds
+  q = vp9_rc_pick_q_and_adjust_q_bounds(cpi,
+                                        &bottom_index,
+                                        &top_index,
+                                        &top_index_prop);
+
+  if (!frame_is_intra_only(cm)) {
+    cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
+    /* TODO: Decide this more intelligently */
+    set_high_precision_mv(cpi, (q < HIGH_PRECISION_MV_QTHRESH));
+  }
+
   encode_with_recode_loop(cpi,
                           size,
                           dest,
@@ -3161,7 +3118,7 @@
    * needed in motion search besides loopfilter */
   cm->last_frame_type = cm->frame_type;
 
-  vp9_rc_postencode_update(cpi, *size, q);
+  vp9_rc_postencode_update(cpi, *size, top_index_prop);
 
 #if 0
   output_frame_level_debug_stats(cpi);
@@ -3282,6 +3239,21 @@
   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
 }
 
+static void Pass0Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest,
+                        unsigned int *frame_flags) {
+  encode_frame_to_data_rate(cpi, size, dest, frame_flags);
+}
+
+static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest,
+                        unsigned int *frame_flags) {
+  (void) size;
+  (void) dest;
+  (void) frame_flags;
+
+  vp9_set_quantizer(cpi, find_fp_qindex());
+  vp9_first_pass(cpi);
+}
+
 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size,
                         unsigned char *dest, unsigned int *frame_flags) {
   cpi->enable_encode_breakout = 1;
@@ -3293,27 +3265,6 @@
   // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt");
 
   vp9_twopass_postencode_update(cpi, *size);
-
-  /*
-#ifdef DISABLE_RC_LONG_TERM_MEM
-  cpi->twopass.bits_left -=  cpi->rc.this_frame_target;
-#else
-  cpi->twopass.bits_left -= 8 * *size;
-#endif
-
-  if (!cpi->refresh_alt_ref_frame) {
-    double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
-    double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
-                                        * cpi->oxcf.two_pass_vbrmin_section
-                                        / 100);
-
-    if (two_pass_min_rate < lower_bounds_min_rate)
-      two_pass_min_rate = lower_bounds_min_rate;
-
-    cpi->twopass.bits_left += (int64_t)(two_pass_min_rate
-                              / cpi->oxcf.framerate);
-  }
-  */
 }
 
 static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) {
@@ -3385,8 +3336,7 @@
 
   cpi->source = NULL;
 
-  cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
-  set_mvcost(cpi);
+  set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
 
   // Should we code an alternate reference frame.
   if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) {
@@ -3626,7 +3576,8 @@
   } else if (cpi->pass == 2) {
     Pass2Encode(cpi, size, dest, frame_flags);
   } else {
-    encode_frame_to_data_rate(cpi, size, dest, frame_flags);
+    // One pass encode
+    Pass0Encode(cpi, size, dest, frame_flags);
   }
 
   if (cm->refresh_frame_context)
diff --git a/vp9/encoder/vp9_onyx_int.h b/vp9/encoder/vp9_onyx_int.h
index b8602e0..c631565 100644
--- a/vp9/encoder/vp9_onyx_int.h
+++ b/vp9/encoder/vp9_onyx_int.h
@@ -338,7 +338,7 @@
   int worst_quality;
   int active_worst_quality;
   int best_quality;
-  int active_best_quality;
+  // int active_best_quality;
 } RATE_CONTROL;
 
 typedef struct VP9_COMP {
@@ -695,7 +695,7 @@
 
 void vp9_alloc_compressor_data(VP9_COMP *cpi);
 
-int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget);
+int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget);
 
 static int get_token_alloc(int mb_rows, int mb_cols) {
   return mb_rows * mb_cols * (48 * 16 + 4);
diff --git a/vp9/encoder/vp9_ratectrl.c b/vp9/encoder/vp9_ratectrl.c
index bf1fc4f..d631b31 100644
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -45,6 +45,10 @@
 static int inter_minq[QINDEX_RANGE];
 static int afq_low_motion_minq[QINDEX_RANGE];
 static int afq_high_motion_minq[QINDEX_RANGE];
+static int gf_high = 2000;
+static int gf_low = 400;
+static int kf_high = 5000;
+static int kf_low = 400;
 
 // Functions to compute the active minq lookup table entries based on a
 // formulaic approach to facilitate easier adjustment of the Q tables.
@@ -84,7 +88,7 @@
     kf_high_motion_minq[i] = calculate_minq_index(maxq,
                                                   0.000002,
                                                   -0.0012,
-                                                  0.5,
+                                                  0.50,
                                                   0.0);
 
     gf_low_motion_minq[i] = calculate_minq_index(maxq,
@@ -97,11 +101,6 @@
                                                   -0.00125,
                                                   0.50,
                                                   0.0);
-    inter_minq[i] = calculate_minq_index(maxq,
-                                         0.00000271,
-                                         -0.00113,
-                                         0.75,
-                                         0.0);
     afq_low_motion_minq[i] = calculate_minq_index(maxq,
                                                   0.0000015,
                                                   -0.0009,
@@ -112,6 +111,11 @@
                                                    -0.00125,
                                                    0.55,
                                                    0.0);
+    inter_minq[i] = calculate_minq_index(maxq,
+                                         0.00000271,
+                                         -0.00113,
+                                         0.75,
+                                         0.0);
   }
 }
 
@@ -285,7 +289,6 @@
   }
 }
 
-
 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
   const int q = cpi->common.base_qindex;
   int correction_factor = 100;
@@ -333,7 +336,6 @@
       break;
   }
 
-  // if ( (correction_factor > 102) && (Q < cpi->rc.active_worst_quality) )
   if (correction_factor > 102) {
     // We are not already at the worst allowable quality
     correction_factor =
@@ -367,8 +369,9 @@
 }
 
 
-int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame) {
-  int q = cpi->rc.active_worst_quality;
+int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
+                      int active_best_quality, int active_worst_quality) {
+  int q = active_worst_quality;
 
   int i;
   int last_error = INT_MAX;
@@ -396,7 +399,7 @@
     target_bits_per_mb =
         (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
 
-  i = cpi->rc.active_best_quality;
+  i = active_best_quality;
 
   do {
     bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cpi->common.frame_type, i,
@@ -412,7 +415,7 @@
     } else {
       last_error = bits_per_mb_at_this_q - target_bits_per_mb;
     }
-  } while (++i <= cpi->rc.active_worst_quality);
+  } while (++i <= active_worst_quality);
 
   return q;
 }
@@ -438,14 +441,17 @@
   return active_best_quality;
 }
 
-int vp9_rc_pick_q_and_adjust_q_bounds(VP9_COMP *cpi,
+int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi,
                                       int *bottom_index,
-                                      int *top_index) {
-  // Set an active best quality and if necessary active worst quality
-  int q = cpi->rc.active_worst_quality;
-  VP9_COMMON *const cm = &cpi->common;
+                                      int *top_index,
+                                      int *top_index_prop) {
+  const VP9_COMMON *const cm = &cpi->common;
+  int active_best_quality;
+  int active_worst_quality = cpi->rc.active_worst_quality;
+  int q;
 
   if (frame_is_intra_only(cm)) {
+    active_best_quality = cpi->rc.best_quality;
 #if !CONFIG_MULTIPLE_ARF
     // Handle the special case for key frames forced when we have75 reached
     // the maximum key frame interval. Here force the Q to a range
@@ -457,25 +463,19 @@
 
       delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q,
                                         (last_boosted_q * 0.75));
-
-      cpi->rc.active_best_quality = MAX(qindex + delta_qindex,
-                                        cpi->rc.best_quality);
-    } else if (cpi->pass == 0 && cpi->common.current_video_frame == 0) {
-      // If this is the first (key) frame in 1-pass, active best/worst is
-      // the user best/worst-allowed, and leave the top_index to active_worst.
-      cpi->rc.active_best_quality = cpi->oxcf.best_allowed_q;
-      cpi->rc.active_worst_quality = cpi->oxcf.worst_allowed_q;
-    } else {
-      int high = 5000;
-      int low = 400;
+      active_best_quality = MAX(qindex + delta_qindex,
+                                cpi->rc.best_quality);
+    } else if (!(cpi->pass == 0 && cpi->common.current_video_frame == 0)) {
+      // not first frame of one pass
       double q_adj_factor = 1.0;
       double q_val;
 
       // Baseline value derived from cpi->active_worst_quality and kf boost
-      cpi->rc.active_best_quality = get_active_quality(q, cpi->rc.kf_boost,
-                                                       low, high,
-                                                       kf_low_motion_minq,
-                                                       kf_high_motion_minq);
+      active_best_quality = get_active_quality(active_worst_quality,
+                                               cpi->rc.kf_boost,
+                                               kf_low, kf_high,
+                                               kf_low_motion_minq,
+                                               kf_high_motion_minq);
 
       // Allow somewhat lower kf minq with small image formats.
       if ((cm->width * cm->height) <= (352 * 288)) {
@@ -487,124 +487,128 @@
 
       // Convert the adjustment factor to a qindex delta
       // on active_best_quality.
-      q_val = vp9_convert_qindex_to_q(cpi->rc.active_best_quality);
-      cpi->rc.active_best_quality +=
+      q_val = vp9_convert_qindex_to_q(active_best_quality);
+      active_best_quality +=
           vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor));
     }
 #else
     double current_q;
     // Force the KF quantizer to be 30% of the active_worst_quality.
-    current_q = vp9_convert_qindex_to_q(cpi->rc.active_worst_quality);
-    cpi->rc.active_best_quality = cpi->rc.active_worst_quality
+    current_q = vp9_convert_qindex_to_q(active_worst_quality);
+    active_best_quality = active_worst_quality
         + vp9_compute_qdelta(cpi, current_q, current_q * 0.3);
 #endif
   } else if (!cpi->is_src_frame_alt_ref &&
              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
-    int high = 2000;
-    int low = 400;
 
-    // Use the lower of cpi->rc.active_worst_quality and recent
+    // Use the lower of active_worst_quality and recent
     // average Q as basis for GF/ARF best Q limit unless last frame was
     // a key frame.
     if (cpi->frames_since_key > 1 &&
-        cpi->rc.avg_frame_qindex < cpi->rc.active_worst_quality) {
+        cpi->rc.avg_frame_qindex < active_worst_quality) {
       q = cpi->rc.avg_frame_qindex;
+    } else {
+      q = active_worst_quality;
     }
     // For constrained quality dont allow Q less than the cq level
     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
       if (q < cpi->cq_target_quality)
         q = cpi->cq_target_quality;
       if (cpi->frames_since_key > 1) {
-        cpi->rc.active_best_quality = get_active_quality(q, cpi->rc.gfu_boost,
-                                                         low, high,
-                                                         afq_low_motion_minq,
-                                                         afq_high_motion_minq);
+        active_best_quality = get_active_quality(q, cpi->rc.gfu_boost,
+                                                 gf_low, gf_high,
+                                                 afq_low_motion_minq,
+                                                 afq_high_motion_minq);
       } else {
-        cpi->rc.active_best_quality = get_active_quality(q, cpi->rc.gfu_boost,
-                                                         low, high,
-                                                         gf_low_motion_minq,
-                                                         gf_high_motion_minq);
+        active_best_quality = get_active_quality(q, cpi->rc.gfu_boost,
+                                                 gf_low, gf_high,
+                                                 gf_low_motion_minq,
+                                                 gf_high_motion_minq);
       }
       // Constrained quality use slightly lower active best.
-      cpi->rc.active_best_quality = cpi->rc.active_best_quality * 15 / 16;
+      active_best_quality = active_best_quality * 15 / 16;
 
     } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
       if (!cpi->refresh_alt_ref_frame) {
-        cpi->rc.active_best_quality = cpi->cq_target_quality;
+        active_best_quality = cpi->cq_target_quality;
       } else {
         if (cpi->frames_since_key > 1) {
-          cpi->rc.active_best_quality = get_active_quality(
-              q, cpi->rc.gfu_boost, low, high,
+          active_best_quality = get_active_quality(
+              q, cpi->rc.gfu_boost, gf_low, gf_high,
               afq_low_motion_minq, afq_high_motion_minq);
         } else {
-          cpi->rc.active_best_quality = get_active_quality(
-              q, cpi->rc.gfu_boost, low, high,
+          active_best_quality = get_active_quality(
+              q, cpi->rc.gfu_boost, gf_low, gf_high,
               gf_low_motion_minq, gf_high_motion_minq);
         }
       }
     } else {
-        cpi->rc.active_best_quality = get_active_quality(
-            q, cpi->rc.gfu_boost, low, high,
-            gf_low_motion_minq, gf_high_motion_minq);
+      active_best_quality = get_active_quality(
+          q, cpi->rc.gfu_boost, gf_low, gf_high,
+          gf_low_motion_minq, gf_high_motion_minq);
     }
   } else {
     if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
-      cpi->rc.active_best_quality = cpi->cq_target_quality;
+      active_best_quality = cpi->cq_target_quality;
     } else {
-      cpi->rc.active_best_quality = inter_minq[q];
-      // 1-pass: for now, use the average Q for the active_best, if its lower
-      // than active_worst.
-      if (cpi->pass == 0 && (cpi->rc.avg_frame_qindex < q))
-        cpi->rc.active_best_quality = inter_minq[cpi->rc.avg_frame_qindex];
+      if (cpi->pass == 0 &&
+          cpi->rc.avg_frame_qindex < active_worst_quality)
+        // 1-pass: for now, use the average Q for the active_best, if its lower
+        // than active_worst.
+        active_best_quality = inter_minq[cpi->rc.avg_frame_qindex];
+      else
+        active_best_quality = inter_minq[active_worst_quality];
 
       // For the constrained quality mode we don't want
       // q to fall below the cq level.
       if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
-          (cpi->rc.active_best_quality < cpi->cq_target_quality)) {
+          (active_best_quality < cpi->cq_target_quality)) {
         // If we are strongly undershooting the target rate in the last
         // frames then use the user passed in cq value not the auto
         // cq value.
         if (cpi->rc.rolling_actual_bits < cpi->rc.min_frame_bandwidth)
-          cpi->rc.active_best_quality = cpi->oxcf.cq_level;
+          active_best_quality = cpi->oxcf.cq_level;
         else
-          cpi->rc.active_best_quality = cpi->cq_target_quality;
+          active_best_quality = cpi->cq_target_quality;
       }
     }
   }
 
   // Clip the active best and worst quality values to limits
-  if (cpi->rc.active_worst_quality > cpi->rc.worst_quality)
-    cpi->rc.active_worst_quality = cpi->rc.worst_quality;
+  if (active_worst_quality > cpi->rc.worst_quality)
+    active_worst_quality = cpi->rc.worst_quality;
 
-  if (cpi->rc.active_best_quality < cpi->rc.best_quality)
-    cpi->rc.active_best_quality = cpi->rc.best_quality;
+  if (active_best_quality < cpi->rc.best_quality)
+    active_best_quality = cpi->rc.best_quality;
 
-  if (cpi->rc.active_best_quality > cpi->rc.worst_quality)
-    cpi->rc.active_best_quality = cpi->rc.worst_quality;
+  if (active_best_quality > cpi->rc.worst_quality)
+    active_best_quality = cpi->rc.worst_quality;
 
-  if (cpi->rc.active_worst_quality < cpi->rc.active_best_quality)
-    cpi->rc.active_worst_quality = cpi->rc.active_best_quality;
+  if (active_worst_quality < active_best_quality)
+    active_worst_quality = active_best_quality;
 
-  *top_index = cpi->rc.active_worst_quality;
-  *bottom_index = cpi->rc.active_best_quality;
+  *top_index_prop = active_worst_quality;
+  *top_index = active_worst_quality;
+  *bottom_index = active_best_quality;
 
 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
   // Limit Q range for the adaptive loop.
   if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) {
     if (!(cpi->pass == 0 && cpi->common.current_video_frame == 0)) {
+      *top_index = active_worst_quality;
       *top_index =
-          (cpi->rc.active_worst_quality + cpi->rc.active_best_quality * 3) / 4;
+          (active_worst_quality + active_best_quality * 3) / 4;
     }
   } else if (!cpi->is_src_frame_alt_ref &&
              (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) &&
              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
     *top_index =
-      (cpi->rc.active_worst_quality + cpi->rc.active_best_quality) / 2;
+      (active_worst_quality + active_best_quality) / 2;
   }
 #endif
 
   if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
-    q = cpi->rc.active_best_quality;
+    q = active_best_quality;
   // Special case code to try and match quality with forced key frames
   } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
     q = cpi->rc.last_boosted_qindex;
@@ -614,17 +618,35 @@
       // 1-pass: for now, use per-frame-bw for target size of frame, scaled
       // by |x| for key frame.
       int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1;
-      q = vp9_rc_regulate_q(cpi, scale * cpi->rc.av_per_frame_bandwidth);
+      q = vp9_rc_regulate_q(cpi, scale * cpi->rc.av_per_frame_bandwidth,
+                            active_best_quality, active_worst_quality);
     } else {
-      q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target);
+      q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
+                            active_best_quality, active_worst_quality);
     }
     if (q > *top_index)
       q = *top_index;
   }
+#if CONFIG_MULTIPLE_ARF
+  // Force the quantizer determined by the coding order pattern.
+  if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
+      cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) {
+    double new_q;
+    double current_q = vp9_convert_qindex_to_q(active_worst_quality);
+    int level = cpi->this_frame_weight;
+    assert(level >= 0);
+    new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
+    q = active_worst_quality +
+        vp9_compute_qdelta(cpi, current_q, new_q);
+
+    *bottom_index = q;
+    *top_index    = q;
+    printf("frame:%d q:%d\n", cm->current_video_frame, q);
+  }
+#endif
   return q;
 }
 
-
 static int estimate_keyframe_frequency(VP9_COMP *cpi) {
   int i;
 
@@ -680,8 +702,7 @@
   cpi->rc.key_frame_count++;
 }
 
-
-static void compute_frame_size_bounds(const VP9_COMP *cpi,
+void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
                                       int this_frame_target,
                                       int *frame_under_shoot_limit,
                                       int *frame_over_shoot_limit) {
@@ -720,9 +741,7 @@
 }
 
 // return of 0 means drop frame
-int vp9_rc_pick_frame_size_and_bounds(VP9_COMP *cpi,
-                                      int *frame_under_shoot_limit,
-                                      int *frame_over_shoot_limit) {
+int vp9_rc_pick_frame_size_target(VP9_COMP *cpi) {
   VP9_COMMON *cm = &cpi->common;
 
   if (cm->frame_type == KEY_FRAME)
@@ -733,13 +752,11 @@
   // Target rate per SB64 (including partial SB64s.
   cpi->rc.sb64_target_rate = ((int64_t)cpi->rc.this_frame_target * 64 * 64) /
                              (cpi->common.width * cpi->common.height);
-  compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
-                            frame_under_shoot_limit, frame_over_shoot_limit);
-
   return 1;
 }
 
-void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used, int q) {
+void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used,
+                              int worst_q) {
   VP9_COMMON *const cm = &cpi->common;
   // Update rate control heuristics
   cpi->rc.projected_frame_size = (bytes_used << 3);
@@ -750,6 +767,7 @@
             cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0);
 
   cpi->rc.last_q[cm->frame_type] = cm->base_qindex;
+  cpi->rc.active_worst_quality = worst_q;
 
   // Keep record of last boosted (KF/KF/ARF) Q value.
   // If the current frame is coded at a lower Q then we also update it.
@@ -777,11 +795,11 @@
   if (cm->frame_type != KEY_FRAME &&
       !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) {
     cpi->rc.ni_frames++;
-    cpi->rc.tot_q += vp9_convert_qindex_to_q(q);
+    cpi->rc.tot_q += vp9_convert_qindex_to_q(cm->base_qindex);
     cpi->rc.avg_q = cpi->rc.tot_q / (double)cpi->rc.ni_frames;
 
     // Calculate the average Q for normal inter frames (not key or GFU frames).
-    cpi->rc.ni_tot_qi += q;
+    cpi->rc.ni_tot_qi += cm->base_qindex;
     cpi->rc.ni_av_qi = cpi->rc.ni_tot_qi / cpi->rc.ni_frames;
   }
 
diff --git a/vp9/encoder/vp9_ratectrl.h b/vp9/encoder/vp9_ratectrl.h
index f01d186..063ac8f 100644
--- a/vp9/encoder/vp9_ratectrl.h
+++ b/vp9/encoder/vp9_ratectrl.h
@@ -31,23 +31,29 @@
 void vp9_rc_init_minq_luts(void);
 
 // return of 0 means drop frame
-// Changes rc.this_frame_target and rc.sb64_rate_target
-int vp9_rc_pick_frame_size_and_bounds(VP9_COMP *cpi,
+// Changes only rc.this_frame_target and rc.sb64_rate_target
+int vp9_rc_pick_frame_size_target(VP9_COMP *cpi);
+
+void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
+                                      int this_frame_target,
                                       int *frame_under_shoot_limit,
                                       int *frame_over_shoot_limit);
+
 // Picks q and q bounds given the target for bits
-int vp9_rc_pick_q_and_adjust_q_bounds(VP9_COMP *cpi,
-                                      int * bottom_index,
-                                      int * top_index);
+int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi,
+                                      int *bottom_index,
+                                      int *top_index,
+                                      int *top_index_prop);
 
 // Estimates q to achieve a target bits per frame
-int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame);
+int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
+                      int active_best_quality, int active_worst_quality);
 
 // Post encode update of the rate control parameters based
 // on bytes used and q used for the frame
 void vp9_rc_postencode_update(VP9_COMP *cpi,
                               uint64_t bytes_used,
-                              int q_used);
+                              int worst_q);
 
 // estimates bits per mb for a given qindex and correction factor
 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index 84b3d1c..2eaf521 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -154,11 +154,11 @@
                              vp9_coeff_probs_model (*p)[BLOCK_TYPES]) {
   int i, j, k, l;
   TX_SIZE t;
-  for (t = TX_4X4; t <= TX_32X32; t++)
-    for (i = 0; i < BLOCK_TYPES; i++)
-      for (j = 0; j < REF_TYPES; j++)
-        for (k = 0; k < COEF_BANDS; k++)
-          for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
+  for (t = TX_4X4; t <= TX_32X32; ++t)
+    for (i = 0; i < BLOCK_TYPES; ++i)
+      for (j = 0; j < REF_TYPES; ++j)
+        for (k = 0; k < COEF_BANDS; ++k)
+          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
             vp9_prob probs[ENTROPY_NODES];
             vp9_model_to_full_probs(p[t][i][j][k][l], probs);
             vp9_cost_tokens((int *)c[t][i][j][k][0][l], probs,
@@ -528,7 +528,7 @@
   const int eob = p->eobs[block];
   const int16_t *const qcoeff_ptr = BLOCK_OFFSET(p->qcoeff, block);
   const int ref = mbmi->ref_frame[0] != INTRA_FRAME;
-  unsigned int (*token_costs)[2][PREV_COEF_CONTEXTS][ENTROPY_TOKENS] =
+  unsigned int (*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
                    x->token_costs[tx_size][type][ref];
   const ENTROPY_CONTEXT above_ec = !!*A, left_ec = !!*L;
   uint8_t *p_tok = x->token_cache;
diff --git a/vp9/encoder/vp9_tokenize.h b/vp9/encoder/vp9_tokenize.h
index 482ea6c..67e6c9d 100644
--- a/vp9/encoder/vp9_tokenize.h
+++ b/vp9/encoder/vp9_tokenize.h
@@ -12,7 +12,9 @@
 #define VP9_ENCODER_VP9_TOKENIZE_H_
 
 #include "vp9/common/vp9_entropy.h"
+
 #include "vp9/encoder/vp9_block.h"
+#include "vp9/encoder/vp9_treewriter.h"
 
 void vp9_tokenize_initialize();
 
diff --git a/vp9/encoder/vp9_treewriter.c b/vp9/encoder/vp9_treewriter.c
index e4aed53..5b0c17f 100644
--- a/vp9/encoder/vp9_treewriter.c
+++ b/vp9/encoder/vp9_treewriter.c
@@ -36,3 +36,24 @@
   costs[-tree[0]] = vp9_cost_bit(probs[0], 0);
   cost(costs, tree, probs, 2, 0);
 }
+
+static void tree2tok(struct vp9_token *tokens, const vp9_tree_index *tree,
+                     int i, int v, int l) {
+  v += v;
+  ++l;
+
+  do {
+    const vp9_tree_index j = tree[i++];
+    if (j <= 0) {
+      tokens[-j].value = v;
+      tokens[-j].len = l;
+    } else {
+      tree2tok(tokens, tree, j, v, l);
+    }
+  } while (++v & 1);
+}
+
+void vp9_tokens_from_tree(struct vp9_token *tokens,
+                          const vp9_tree_index *tree) {
+  tree2tok(tokens, tree, 0, 0, 0);
+}
diff --git a/vp9/encoder/vp9_treewriter.h b/vp9/encoder/vp9_treewriter.h
index 3245960..94f3eb9 100644
--- a/vp9/encoder/vp9_treewriter.h
+++ b/vp9/encoder/vp9_treewriter.h
@@ -44,6 +44,14 @@
   } while (len);
 }
 
+struct vp9_token {
+  int value;
+  int len;
+};
+
+
+void vp9_tokens_from_tree(struct vp9_token*, const vp9_tree_index *);
+
 static INLINE void write_token(vp9_writer *w, vp9_tree tree,
                                const vp9_prob *probs,
                                const struct vp9_token *token) {
diff --git a/vp9/vp9_common.mk b/vp9/vp9_common.mk
index eefbd1a..f6aebff 100644
--- a/vp9/vp9_common.mk
+++ b/vp9/vp9_common.mk
@@ -17,7 +17,6 @@
 VP9_COMMON_SRCS-yes += common/vp9_convolve.c
 VP9_COMMON_SRCS-yes += common/vp9_convolve.h
 VP9_COMMON_SRCS-yes += common/vp9_debugmodes.c
-VP9_COMMON_SRCS-yes += common/vp9_default_coef_probs.h
 VP9_COMMON_SRCS-yes += common/vp9_entropy.c
 VP9_COMMON_SRCS-yes += common/vp9_entropymode.c
 VP9_COMMON_SRCS-yes += common/vp9_entropymv.c
diff --git a/vp9/vp9_cx_iface.c b/vp9/vp9_cx_iface.c
index 9a23ebd..5d53a41 100644
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -198,6 +198,10 @@
   RANGE_CHECK(vp8_cfg, arnr_type,       1, 3);
   RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
 
+  // TODO(yaowu): remove this when ssim tuning is implemented for vp9
+  if (vp8_cfg->tuning == VP8_TUNE_SSIM)
+      ERROR("Option --tune=ssim is not currently supported in VP9.");
+
   if (cfg->g_pass == VPX_RC_LAST_PASS) {
     size_t           packet_sz = sizeof(FIRSTPASS_STATS);
     int              n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
diff --git a/vpx/src/vpx_decoder.c b/vpx/src/vpx_decoder.c
index 1f575e0..a99e48f 100644
--- a/vpx/src/vpx_decoder.c
+++ b/vpx/src/vpx_decoder.c
@@ -172,7 +172,7 @@
   if (!ctx || !cb)
     res = VPX_CODEC_INVALID_PARAM;
   else if (!ctx->iface || !ctx->priv
-           || !(ctx->iface->caps & VPX_CODEC_CAP_PUT_FRAME))
+           || !(ctx->iface->caps & VPX_CODEC_CAP_PUT_SLICE))
     res = VPX_CODEC_ERROR;
   else {
     ctx->priv->dec.put_slice_cb.u.put_slice = cb;