Merge "Experimental change to two pass prediction decay calculation." into experimental
diff --git a/configure b/configure
index 6717174..8c3716e 100755
--- a/configure
+++ b/configure
@@ -228,6 +228,8 @@
     newupdate
     superblocks
     expanded_coef_context
+    int_8x8fdct
+    newintramodes
 "
 CONFIG_LIST="
     external_build
diff --git a/vp8/common/blockd.h b/vp8/common/blockd.h
index 7915714..19511f3 100644
--- a/vp8/common/blockd.h
+++ b/vp8/common/blockd.h
@@ -86,8 +86,16 @@
     DC_PRED,            /* average of above and left pixels */
     V_PRED,             /* vertical prediction */
     H_PRED,             /* horizontal prediction */
+#if CONFIG_NEWINTRAMODES
+    D45_PRED,           /* Directional 45 deg prediction  [anti-clockwise from 0 deg hor] */
+    D135_PRED,          /* Directional 135 deg prediction [anti-clockwise from 0 deg hor] */
+    D117_PRED,          /* Directional 112 deg prediction [anti-clockwise from 0 deg hor] */
+    D153_PRED,          /* Directional 157 deg prediction [anti-clockwise from 0 deg hor] */
+    D27_PRED,           /* Directional 22 deg prediction  [anti-clockwise from 0 deg hor] */
+    D63_PRED,           /* Directional 67 deg prediction  [anti-clockwise from 0 deg hor] */
+#endif
     TM_PRED,            /* Truemotion prediction */
-    I8X8_PRED,           /* 8x8 based prediction, each 8x8 has its own prediction mode */
+    I8X8_PRED,          /* 8x8 based prediction, each 8x8 has its own prediction mode */
     B_PRED,             /* block based prediction, each block has its own prediction mode */
 
     NEARESTMV,
diff --git a/vp8/common/default_coef_probs.h b/vp8/common/default_coef_probs.h
index 3a9262e..3bd46f1 100755
--- a/vp8/common/default_coef_probs.h
+++ b/vp8/common/default_coef_probs.h
@@ -346,3 +346,137 @@
         }
     }
 };
+
+static const vp8_prob
+vp8_default_coef_probs_8x8[BLOCK_TYPES_8X8]
+                          [COEF_BANDS]
+                          [PREV_COEF_CONTEXTS]
+                          [ENTROPY_NODES] =
+{
+  { /* block Type 0 */
+    { /* Coeff Band 0 */
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 1 */
+      { 60, 140, 195, 255, 212, 214, 128, 128, 128, 128, 128},
+      { 75, 221, 231, 255, 203, 255, 128, 128, 128, 128, 128},
+      { 9, 212, 196, 251, 197, 207, 255, 185, 128, 128, 128}
+    },
+    { /* Coeff Band 2 */
+      { 1, 227, 226, 255, 215, 215, 128, 128, 128, 128, 128},
+      { 5, 163, 209, 255, 212, 212, 255, 255, 128, 128, 128},
+      { 1, 133, 203, 255, 210, 220, 255, 255, 128, 128, 128}
+    },
+    { /* Coeff Band 3 */
+      { 1, 226, 225, 255, 228, 236, 128, 128, 128, 128, 128},
+      { 6, 163, 208, 255, 224, 234, 255, 255, 128, 128, 128},
+      { 1, 122, 196, 253, 212, 248, 255, 255, 128, 128, 128}
+    },
+    { /* Coeff Band 4 */
+      { 1, 222, 197, 254, 193, 216, 255, 236, 128, 128, 128},
+      { 7, 140, 163, 251, 195, 211, 255, 238, 128, 128, 128},
+      { 1, 91, 152, 249, 181, 197, 255, 239, 128, 128, 128}
+    },
+    { /* Coeff Band 5 */
+      { 1, 226, 218, 255, 216, 241, 255, 255, 128, 128, 128},
+      { 6, 154, 191, 255, 218, 240, 255, 255, 128, 128, 128},
+      { 1, 111, 184, 255, 210, 225, 255, 255, 128, 128, 128}
+    },
+    { /* Coeff Band 6 */
+      { 1, 221, 217, 255, 208, 217, 255, 232, 128, 128, 128},
+      { 11, 155, 189, 254, 203, 211, 255, 249, 128, 128, 128},
+      { 1, 110, 171, 252, 191, 204, 255, 236, 128, 128, 128}
+    },
+    { /* Coeff Band 7 */
+      { 1, 207, 235, 255, 232, 240, 128, 128, 128, 128, 128},
+      { 58, 161, 216, 255, 229, 235, 255, 255, 128, 128, 128},
+      { 8, 133, 204, 255, 219, 231, 255, 255, 128, 128, 128}
+    }
+  },
+  { /* block Type 1 */
+    { /* Coeff Band 0 */
+      { 134, 152, 233, 224, 234, 52, 255, 166, 128, 128, 128},
+      { 97, 132, 185, 234, 186, 189, 197, 171, 255, 212, 128},
+      { 84, 110, 185, 237, 182, 182, 145, 145, 255, 255, 128}
+    },
+    { /* Coeff Band 1 */
+      { 1, 124, 213, 247, 192, 212, 255, 255, 128, 128, 128},
+      { 88, 111, 178, 254, 189, 211, 255, 255, 128, 128, 128},
+      { 12, 59, 129, 236, 150, 179, 239, 195, 255, 255, 128}
+    },
+    { /* Coeff Band 2 */
+      { 1, 102, 225, 255, 210, 240, 128, 128, 128, 128, 128},
+      { 110, 78, 195, 254, 200, 191, 255, 255, 128, 128, 128},
+      { 37, 63, 177, 255, 194, 195, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 3 */
+      { 1, 1, 229, 255, 202, 224, 128, 128, 128, 128, 128},
+      { 150, 1, 192, 255, 206, 226, 128, 128, 128, 128, 128},
+      { 75, 1, 138, 255, 172, 228, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 4 */
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 5 */
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 6 */
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}
+    },
+    { /* Coeff Band 7 */
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
+      { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}
+    }
+  },
+  { /* block Type 2 */
+    { /* Coeff Band 0 */
+      { 11, 181, 226, 199, 183, 255, 255, 255, 128, 128, 128},
+      { 2, 147, 185, 248, 163, 180, 255, 236, 128, 128, 128},
+      { 1, 123, 157, 238, 154, 176, 255, 226, 255, 255, 128}
+    },
+    { /* Coeff Band 1 */
+      { 1, 150, 191, 246, 174, 188, 255, 235, 128, 128, 128},
+      { 1, 125, 166, 245, 165, 185, 255, 234, 128, 128, 128},
+      { 1, 79, 125, 240, 148, 179, 255, 234, 255, 255, 128}
+    },
+    { /* Coeff Band 2 */
+      { 1, 146, 184, 242, 167, 183, 255, 230, 255, 255, 128},
+      { 1, 119, 160, 239, 156, 178, 255, 231, 255, 255, 128},
+      { 1, 75, 115, 234, 142, 173, 255, 225, 255, 255, 128}
+    },
+    { /* Coeff Band 3 */
+      { 1, 150, 188, 244, 169, 183, 255, 233, 255, 255, 128},
+      { 1, 123, 162, 243, 161, 180, 255, 233, 128, 128, 128},
+      { 1, 76, 120, 238, 148, 178, 255, 230, 255, 255, 128}
+    },
+    { /* Coeff Band 4 */
+      { 1, 163, 202, 252, 188, 204, 255, 248, 128, 128, 128},
+      { 1, 136, 180, 251, 181, 201, 255, 246, 128, 128, 128},
+      { 1, 92, 146, 249, 170, 197, 255, 245, 128, 128, 128}
+    },
+    { /* Coeff Band 5 */
+      { 1, 156, 195, 249, 179, 193, 255, 241, 255, 255, 128},
+      { 1, 128, 169, 248, 171, 192, 255, 242, 255, 255, 128},
+      { 1, 84, 132, 245, 158, 187, 255, 240, 255, 255, 128}
+    },
+    { /* Coeff Band 6 */
+      { 1, 36, 71, 251, 192, 201, 255, 243, 255, 255, 128},
+      { 1, 49, 185, 250, 184, 199, 255, 242, 128, 128, 128},
+      { 1, 95, 147, 247, 168, 190, 255, 239, 255, 255, 128}
+    },
+    { /* Coeff Band 7 */
+      { 1, 19, 98, 255, 218, 222, 255, 255, 128, 128, 128},
+      { 36, 50, 210, 255, 212, 221, 255, 255, 128, 128, 128},
+      { 6, 117, 180, 254, 199, 216, 255, 251, 128, 128, 128}
+    }
+  }
+};
diff --git a/vp8/common/defaultcoefcounts.h b/vp8/common/defaultcoefcounts.h
deleted file mode 100644
index c13855d..0000000
--- a/vp8/common/defaultcoefcounts.h
+++ /dev/null
@@ -1,217 +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.
- */
-
-/* Generated file, included by entropy.c */
-
-static const unsigned int vp8_default_coef_counts_8x8[BLOCK_TYPES_8X8]
-                                              [COEF_BANDS]
-                                              [PREV_COEF_CONTEXTS]
-                                              [MAX_ENTROPY_TOKENS] =
-{
-
-    { /* block Type 0 */
-      { /* Coeff Band 0 */
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#endif
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
-      },
-      { /* Coeff Band 1 */
-        { 21041, 13314, 3420, 592, 117, 0, 0, 0, 0, 0, 0, 11783},
-        { 48236, 6918, 586, 153, 0, 0, 0, 0, 0, 0, 0, 23137},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 676112, 106685, 24701, 6003, 1426, 429, 165, 0, 0, 0, 0, 28910},
-#endif
-        { 676112, 106685, 24701, 6003, 1426, 429, 165, 0, 0, 0, 0, 28910},
-      },
-      { /* Coeff Band 2 */
-        { 660107, 75227, 8451, 1345, 259, 0, 0, 0, 0, 0, 0, 0},
-        { 79164, 36835, 6865, 1185, 246, 47, 0, 0, 0, 0, 0, 2575},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 19469, 14330, 3070, 579, 94, 6, 0, 0, 0, 0, 0, 44},
-#endif
-        { 19469, 14330, 3070, 579, 94, 6, 0, 0, 0, 0, 0, 44},
-      },
-      { /* Coeff Band 3 */
-        { 1978004, 235343, 28485, 3242, 271, 0, 0, 0, 0, 0, 0, 0},
-        { 228684, 106736, 21431, 2842, 272, 46, 0, 0, 0, 0, 0, 9266},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 32470, 27496, 6852, 1386, 45, 93, 0, 0, 0, 0, 0, 0},
-#endif
-        { 32470, 27496, 6852, 1386, 45, 93, 0, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 4 */
-        { 1911212, 224613, 49653, 13748, 2541, 568, 48, 0, 0, 0, 0, 0},
-        { 196670, 103472, 44473, 11490, 2432, 977, 72, 0, 0, 0, 0, 9447},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 37876, 40417, 19142, 6069, 1799, 727, 51, 0, 0, 0, 0, 0},
-#endif
-        { 37876, 40417, 19142, 6069, 1799, 727, 51, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 5 */
-        { 3813399, 437714, 64387, 11312, 695, 219, 0, 0, 0, 0, 0, 0},
-        { 438288, 215917, 61905, 10194, 674, 107, 0, 0, 0, 0, 0, 17808},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 99139, 93643, 30054, 5758, 802, 171, 0, 0, 0, 0, 0, 0},
-#endif
-        { 99139, 93643, 30054, 5758, 802, 171, 0, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 6 */
-        { 12259383, 1625505, 234927, 46306, 8417, 1456, 151, 0, 0, 0, 0, 0},
-        { 1518161, 734287, 204240, 44228, 9462, 2240, 65, 0, 0, 0, 0, 107630},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 292470, 258894, 94925, 25864, 6662, 2055, 170, 0, 0, 0, 0, 0},
-#endif
-        { 292470, 258894, 94925, 25864, 6662, 2055, 170, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 7 */
-        { 9791308, 2118949, 169439, 16735, 1122, 0, 0, 0, 0, 0, 0, 0},
-        { 1500281, 752410, 123259, 13065, 1168, 47, 0, 0, 0, 0, 0, 707182},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 193067, 142638, 31018, 4719, 516, 138, 0, 0, 0, 0, 0, 12439},
-#endif
-        { 193067, 142638, 31018, 4719, 516, 138, 0, 0, 0, 0, 0, 12439},
-      }
-    },
-    { /* block Type 1 */
-      { /* Coeff Band 0 */
-        { 16925, 10553, 852, 16, 63, 87, 47, 0, 0, 0, 0, 31232},
-        { 39777, 26839, 6822, 1908, 678, 456, 227, 168, 35, 0, 0, 46825},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 17300, 16666, 4168, 1209, 492, 154, 118, 207, 0, 0, 0, 19608},
-#endif
-        { 17300, 16666, 4168, 1209, 492, 154, 118, 207, 0, 0, 0, 19608},
-      },
-      { /* Coeff Band 1 */
-        { 35882, 31722, 4625, 1270, 266, 237, 0, 0, 0, 0, 0, 0},
-        { 15426, 13894, 4482, 1305, 281, 43, 0, 0, 0, 0, 0, 18627},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 3900, 6552, 3472, 1723, 746, 366, 115, 35, 0, 0, 0, 798},
-#endif
-        { 3900, 6552, 3472, 1723, 746, 366, 115, 35, 0, 0, 0, 798},
-      },
-      { /* Coeff Band 2 */
-        { 21998, 29132, 3353, 679, 46, 0, 0, 0, 0, 0, 0, 0},
-        { 9098, 15767, 3794, 792, 268, 47, 0, 0, 0, 0, 0, 22402},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 4007, 8472, 2844, 687, 217, 0, 0, 0, 0, 0, 0, 2739},
-#endif
-        { 4007, 8472, 2844, 687, 217, 0, 0, 0, 0, 0, 0, 2739},
-      },
-      { /* Coeff Band 3 */
-        { 0, 31414, 2911, 682, 96, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 16515, 4425, 938, 124, 0, 0, 0, 0, 0, 0, 31369},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 4833, 2787, 1213, 150, 0, 0, 0, 0, 0, 0, 3744},
-#endif
-        { 0, 4833, 2787, 1213, 150, 0, 0, 0, 0, 0, 0, 3744},
-      },
-      { /* Coeff Band 4 */
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#endif
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 5 */
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#endif
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 6 */
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52762},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13326},
-#endif
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13326},
-      },
-      { /* Coeff Band 7 */
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-#endif
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
-      }
-    },
-    { /* block Type 2 */
-      { /* Coeff Band 0 */
-        { 4444, 1614, 120, 48, 0, 48, 0, 0, 0, 0, 0, 278},
-        { 192436, 103730, 24494, 9845, 4122, 1193, 102, 0, 0, 0, 0, 2577},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 3473446, 2308716, 815510, 370374, 167797, 92152, 12073, 86, 0, 0, 0, 6801},
-#endif
-        { 3473446, 2308716, 815510, 370374, 167797, 92152, 12073, 86, 0, 0, 0, 6801},
-      },
-      { /* Coeff Band 1 */
-        { 2150616, 1136388, 250011, 86888, 31434, 13746, 1243, 0, 0, 0, 0, 0},
-        { 1179945, 799802, 266012, 106787, 40809, 16486, 1546, 0, 0, 0, 0, 2673},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 465128, 504130, 286989, 146259, 62380, 30192, 2866, 20, 0, 0, 0, 0},
-#endif
-        { 465128, 504130, 286989, 146259, 62380, 30192, 2866, 20, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 2 */
-        { 2157762, 1177519, 282665, 108499, 43389, 23224, 2597, 34, 0, 0, 0, 0},
-        { 1135685, 813705, 278079, 123255, 53935, 29492, 3152, 39, 0, 0, 0, 2978},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 391894, 428037, 264216, 144306, 69326, 40281, 5541, 29, 0, 0, 0, 38},
-#endif
-        { 391894, 428037, 264216, 144306, 69326, 40281, 5541, 29, 0, 0, 0, 38},
-      },
-      { /* Coeff Band 3 */
-        { 6669109, 3468471, 782161, 288484, 115500, 51083, 4943, 41, 0, 0, 0, 0},
-        { 3454493, 2361636, 809524, 337663, 141343, 65036, 6361, 0, 0, 0, 0, 8730},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 1231825, 1359522, 824686, 420784, 185517, 98731, 10973, 72, 0, 0, 0, 20},
-#endif
-        { 1231825, 1359522, 824686, 420784, 185517, 98731, 10973, 72, 0, 0, 0, 20},
-      },
-      { /* Coeff Band 4 */
-        { 7606203, 3452846, 659856, 191703, 49335, 14336, 450, 0, 0, 0, 0, 0},
-        { 3806506, 2379332, 691697, 224938, 61966, 18324, 766, 0, 0, 0, 0, 8193},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 1270110, 1283728, 628775, 243378, 72617, 24897, 1087, 0, 0, 0, 0, 0},
-#endif
-        { 1270110, 1283728, 628775, 243378, 72617, 24897, 1087, 0, 0, 0, 0, 0},
-      },
-      { /* Coeff Band 5 */
-        { 15314169, 7436809, 1579928, 515790, 167453, 58305, 3502, 19, 0, 0, 0, 0},
-        { 7021286, 4667922, 1545706, 574463, 191793, 68748, 4048, 1, 0, 0, 0, 17222},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 2011989, 2145878, 1185336, 534879, 195719, 79103, 5343, 4, 0, 0, 0, 37},
-#endif
-        { 2011989, 2145878, 1185336, 534879, 195719, 79103, 5343, 4, 0, 0, 0, 37},
-      },
-      { /* Coeff Band 6 */
-        { 63458382, 25384462, 4208045, 1091050, 299011, 95242, 5238, 33, 0, 0, 0, 0},
-        { 25638401, 14694085, 3945978, 1195420, 344813, 117355, 6703, 0, 0, 0, 0, 216811},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 5988177, 5824044, 2754413, 1077350, 370739, 139710, 9693, 38, 0, 0, 0, 1835},
-#endif
-        { 5988177, 5824044, 2754413, 1077350, 370739, 139710, 9693, 38, 0, 0, 0, 1835},
-      },
-      { /* Coeff Band 7 */
-        { 74998348, 29342158, 2955001, 452912, 69631, 9516, 37, 0, 0, 0, 0, 0},
-        { 24762356, 13281085, 2409883, 436787, 68948, 10658, 36, 0, 0, 0, 0, 6614989},
-#if CONFIG_EXPANDED_COEF_CONTEXT
-        { 3882867, 3224489, 1052289, 252890, 46967, 8548, 154, 0, 0, 0, 0, 194354},
-#endif
-        { 3882867, 3224489, 1052289, 252890, 46967, 8548, 154, 0, 0, 0, 0, 194354},
-      }
-    },
-  };
diff --git a/vp8/common/entropy.c b/vp8/common/entropy.c
index 170d176..9849fd4 100644
--- a/vp8/common/entropy.c
+++ b/vp8/common/entropy.c
@@ -175,36 +175,15 @@
 #endif
 
 #include "default_coef_probs.h"
-#include "defaultcoefcounts.h"
 
 void vp8_default_coef_probs(VP8_COMMON *pc)
 {
     int h;
     vpx_memcpy(pc->fc.coef_probs, default_coef_probs,
                    sizeof(default_coef_probs));
-    h = 0;
-    do
-    {
-        int i = 0;
 
-        do
-        {
-            int k = 0;
-
-            do
-            {
-                unsigned int branch_ct [ENTROPY_NODES] [2];
-                vp8_tree_probs_from_distribution(
-                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
-                    pc->fc.coef_probs_8x8 [h][i][k], branch_ct, vp8_default_coef_counts_8x8 [h][i][k],
-                    256, 1);
-
-            }
-            while (++k < PREV_COEF_CONTEXTS);
-        }
-        while (++i < COEF_BANDS);
-    }
-    while (++h < BLOCK_TYPES_8X8);
+    vpx_memcpy(pc->fc.coef_probs_8x8, vp8_default_coef_probs_8x8,
+                   sizeof(vp8_default_coef_probs_8x8));
 
 }
 
diff --git a/vp8/common/entropy.h b/vp8/common/entropy.h
index 42de3ea..4c42d45 100644
--- a/vp8/common/entropy.h
+++ b/vp8/common/entropy.h
@@ -91,8 +91,9 @@
 #endif
 
 #if CONFIG_NEWUPDATE
-#define SUBEXP_PARAM             1  /* Subexponential code parameter */
-#define COEFUPDATETYPE           2  /* coef update type to use (1/2/3) */
+#define SUBEXP_PARAM                2   /* Subexponential code parameter */
+#define MODULUS_PARAM               21  /* Modulus parameter */
+#define COEFUPDATETYPE              2   /* coef update type to use (1/2/3) */
 #endif
 
 
diff --git a/vp8/common/entropymode.c b/vp8/common/entropymode.c
index aa8d037..0ae6a93 100644
--- a/vp8/common/entropymode.c
+++ b/vp8/common/entropymode.c
@@ -17,6 +17,16 @@
 
 const unsigned int kf_y_mode_cts[8][VP8_YMODES] =
 {
+#if CONFIG_NEWINTRAMODES
+    {12,  6,  5,  5,  5,  5,  5,  5,  5,  2, 22, 200},
+    {25, 13, 13,  7,  7,  7,  7,  7,  7,  6, 27, 160},
+    {31, 17, 18,  8,  8,  8,  8,  8,  8,  9, 26, 139},
+    {40, 22, 23,  8,  8,  8,  8,  8,  8, 12, 27, 116},
+    {53, 26, 28,  8,  8,  8,  8,  8,  8, 13, 26,  94},
+    {68, 33, 35,  8,  8,  8,  8,  8,  8, 17, 20,  68},
+    {78, 38, 38,  8,  8,  8,  8,  8,  8, 19, 16,  52},
+    {89, 42, 42,  8,  8,  8,  8,  8,  8, 21, 12,  34},
+#else
     {17,  6,  5,  2, 22, 203},
     {27, 13, 13,  6, 27, 170},
     {35, 17, 18,  9, 26, 152},
@@ -25,29 +35,69 @@
     {73, 33, 36, 17, 20,  78},
     {88, 38, 39, 19, 16,  57},
     {99, 42, 43, 21, 12,  39},
+#endif
 };
 
-static const unsigned int y_mode_cts  [VP8_YMODES] = {
-    106,  25, 21, 13, 16, 74};
+static const unsigned int y_mode_cts  [VP8_YMODES] =
+#if CONFIG_NEWINTRAMODES
+    {98, 19, 15, 14, 14, 14, 14, 12, 12, 13, 16, 70};
+#else
+    {106, 25, 21, 13, 16, 74};
+#endif
 
 static const unsigned int uv_mode_cts [VP8_YMODES] [VP8_UV_MODES] ={
+#if CONFIG_NEWINTRAMODES
+    { 200, 15, 15, 10, 10, 10, 10, 10, 10,  6},
+    { 130, 75, 10, 10, 10, 10, 10, 10, 10,  6},
+    { 130, 10, 75, 10, 10, 10, 10, 10, 10,  6},
+    { 130, 15, 10, 75, 10, 10, 10, 10, 10,  6},
+    { 150, 15, 10, 10, 75, 10, 10, 10, 10,  6},
+    { 150, 15, 10, 10, 10, 75, 10, 10, 10,  6},
+    { 150, 15, 10, 10, 10, 10, 75, 10, 10,  6},
+    { 150, 15, 10, 10, 10, 10, 10, 75, 10,  6},
+    { 150, 15, 10, 10, 10, 10, 10, 10, 75,  6},
+    { 160, 30, 30, 10, 10, 10, 10, 10, 10, 16},
+    { 132, 46, 40, 10, 10, 10, 10, 10, 10, 18}, /* never used */
+    { 150, 35, 41, 10, 10, 10, 10, 10, 10, 10},
+#else
     { 210, 20, 20,  6},
     { 180, 60, 10,  6},
     { 150, 20, 80,  6},
     { 170, 35, 35, 16},
     { 142, 51, 45, 18}, /* never used */
     { 160, 40, 46, 10},
+#endif
 };
 
-static const unsigned int i8x8_mode_cts  [VP8_UV_MODES] = {93, 69, 81, 13};
+static const unsigned int i8x8_mode_cts  [VP8_I8X8_MODES] =
+#if CONFIG_NEWINTRAMODES
+    {73, 49, 61, 30, 30, 30, 30, 30, 30, 13};
+#else
+    {93, 69, 81, 13};
+#endif
 
 static const unsigned int kf_uv_mode_cts [VP8_YMODES] [VP8_UV_MODES] ={
+#if CONFIG_NEWINTRAMODES
+    { 160, 24, 24, 20, 20, 20, 20, 20, 20,  8},
+    { 102, 64, 30, 20, 20, 20, 20, 20, 20, 10},
+    { 102, 30, 64, 20, 20, 20, 20, 20, 20, 10},
+    { 102, 33, 20, 64, 20, 20, 20, 20, 20, 14},
+    { 102, 33, 20, 20, 64, 20, 20, 20, 20, 14},
+    { 122, 33, 20, 20, 20, 64, 20, 20, 20, 14},
+    { 102, 33, 20, 20, 20, 20, 64, 20, 20, 14},
+    { 102, 33, 20, 20, 20, 20, 20, 64, 20, 14},
+    { 102, 33, 20, 20, 20, 20, 20, 20, 64, 14},
+    { 132, 36, 30, 20, 20, 20, 20, 20, 20, 18},
+    { 122, 41, 35, 20, 20, 20, 20, 20, 20, 18}, /* never used */
+    { 122, 41, 35, 20, 20, 20, 20, 20, 20, 18},
+#else
     { 180, 34, 34,  8},
     { 132, 74, 40, 10},
     { 132, 40, 74, 10},
     { 152, 46, 40, 18},
     { 142, 51, 45, 18}, /* never used */
     { 142, 51, 45, 18},
+#endif
 };
 
 static const unsigned int bmode_cts[VP8_BINTRAMODES] =
@@ -133,7 +183,7 @@
 
 /* Array indices are identical to previously-existing INTRAMODECONTEXTNODES. */
 
-const vp8_tree_index vp8_bmode_tree[18] =     /* INTRAMODECONTEXTNODE value */
+const vp8_tree_index vp8_bmode_tree[VP8_BINTRAMODES*2-2] =     /* INTRAMODECONTEXTNODE value */
 {
     -B_DC_PRED, 2,                             /* 0 = DC_NODE */
     -B_TM_PRED, 4,                            /* 1 = TM_NODE */
@@ -146,6 +196,67 @@
     -B_HD_PRED, -B_HU_PRED             /* 8 = HD_NODE */
 };
 
+#if CONFIG_NEWINTRAMODES
+/* Again, these trees use the same probability indices as their
+   explicitly-programmed predecessors. */
+const vp8_tree_index vp8_ymode_tree[VP8_YMODES*2-2] =
+{
+    2, 14,
+    -DC_PRED, 4,
+    6, 8,
+    -D45_PRED, -D135_PRED,
+    10, 12,
+    -D117_PRED, -D153_PRED,
+    -D27_PRED, -D63_PRED,
+    16, 18,
+    -V_PRED, -H_PRED,
+    -TM_PRED, 20,
+    -B_PRED, -I8X8_PRED
+};
+
+const vp8_tree_index vp8_kf_ymode_tree[VP8_YMODES*2-2] =
+{
+    2, 14,
+    -DC_PRED, 4,
+    6, 8,
+    -D45_PRED, -D135_PRED,
+    10, 12,
+    -D117_PRED, -D153_PRED,
+    -D27_PRED, -D63_PRED,
+    16, 18,
+    -V_PRED, -H_PRED,
+    -TM_PRED, 20,
+    -B_PRED, -I8X8_PRED
+};
+
+const vp8_tree_index vp8_i8x8_mode_tree[VP8_I8X8_MODES*2-2] =
+{
+    2, 14,
+    -DC_PRED, 4,
+    6, 8,
+    -D45_PRED, -D135_PRED,
+    10, 12,
+    -D117_PRED, -D153_PRED,
+    -D27_PRED, -D63_PRED,
+    -V_PRED, 16,
+    -H_PRED, -TM_PRED
+};
+
+const vp8_tree_index vp8_uv_mode_tree[VP8_UV_MODES*2-2] =
+{
+    2, 14,
+    -DC_PRED, 4,
+    6, 8,
+    -D45_PRED, -D135_PRED,
+    10, 12,
+    -D117_PRED, -D153_PRED,
+    -D27_PRED, -D63_PRED,
+    -V_PRED, 16,
+    -H_PRED, -TM_PRED
+};
+
+#else  /* CONFIG_NEWINTRAMODES */
+
 /* Again, these trees use the same probability indices as their
    explicitly-programmed predecessors. */
 const vp8_tree_index vp8_ymode_tree[10] =
@@ -179,6 +290,8 @@
     -H_PRED, -TM_PRED
 };
 
+#endif  /* CONFIG_NEWINTRAMODES */
+
 const vp8_tree_index vp8_mbsplit_tree[6] =
 {
     -3, 2,
@@ -282,10 +395,10 @@
     }
 
     vp8_tree_probs_from_distribution(
-        VP8_UV_MODES, vp8_i8x8_mode_encodings, vp8_i8x8_mode_tree,
+        VP8_I8X8_MODES, vp8_i8x8_mode_encodings, vp8_i8x8_mode_tree,
         x->i8x8_mode_prob, bct, i8x8_mode_cts,
-        256, 1
-        );
+        256, 1);
+
     vpx_memcpy(x->fc.sub_mv_ref_prob, sub_mv_ref_prob, sizeof(sub_mv_ref_prob));
 
 }
@@ -448,7 +561,7 @@
 void print_mode_contexts(VP8_COMMON *pc)
 {
     int j, i;
-    printf("====================\n");
+    printf("\n====================\n");
     for(j=0; j<6; j++)
     {
         for (i = 0; i < 4; i++)
diff --git a/vp8/common/idctllm.c b/vp8/common/idctllm.c
index f0536d5..b2523ba 100644
--- a/vp8/common/idctllm.c
+++ b/vp8/common/idctllm.c
@@ -138,65 +138,60 @@
 {
     int i;
     int a1, b1, c1, d1;
-    int a2, b2, c2, d2;
     short *ip = input;
     short *op = output;
 
     for (i = 0; i < 4; i++)
     {
+        a1 = ((ip[0] + ip[3]));
+        b1 = ((ip[1] + ip[2]));
+        c1 = ((ip[1] - ip[2]));
+        d1 = ((ip[0] - ip[3]));
+
+        op[0] = (a1 + b1 + 1)>>1;
+        op[1] = (c1 + d1)>>1;
+        op[2] = (a1 - b1)>>1;
+        op[3] = (d1 - c1)>>1;
+
+        ip += 4;
+        op += 4;
+    }
+
+    ip = output;
+    op = output;
+    for (i = 0; i < 4; i++)
+    {
         a1 = ip[0] + ip[12];
         b1 = ip[4] + ip[8];
         c1 = ip[4] - ip[8];
         d1 = ip[0] - ip[12];
-
-        op[0] = a1 + b1;
-        op[4] = c1 + d1;
-        op[8] = a1 - b1;
-        op[12] = d1 - c1;
+        op[0] = (a1 + b1 + 1)>>1;
+        op[4] = (c1 + d1)>>1;
+        op[8] = (a1 - b1)>>1;
+        op[12]= (d1 - c1)>>1;
         ip++;
         op++;
     }
-
-    ip = output;
-    op = output;
-
-    for (i = 0; i < 4; i++)
-    {
-        a1 = ip[0] + ip[3];
-        b1 = ip[1] + ip[2];
-        c1 = ip[1] - ip[2];
-        d1 = ip[0] - ip[3];
-
-        a2 = a1 + b1;
-        b2 = c1 + d1;
-        c2 = a1 - b1;
-        d2 = d1 - c1;
-
-        op[0] = (a2 + 1) >> 2;
-        op[1] = (b2 + 1) >> 2;
-        op[2] = (c2 + 1) >> 2;
-        op[3] = (d2 + 1) >> 2;
-
-        ip += 4;
-        op += 4;
-    }
 }
 
-void vp8_short_inv_walsh4x4_1_c(short *input, short *output)
+void vp8_short_inv_walsh4x4_1_c(short *in, short *out)
 {
     int i;
-    int a1;
-    short *op = output;
+    short tmp[4];
+    short *ip = in;
+    short *op = tmp;
 
-    a1 = (input[0] + 1 )>> 2;
+    op[0] =(ip[0]+ 1)>>1;
+    op[1] = op[2] = op[3] = (ip[0]>>1);
 
-    for (i = 0; i < 4; i++)
+    ip = tmp;
+    op = out;
+    for(i = 0; i<4; i++)
     {
-        op[0] = a1;
-        op[1] = a1;
-        op[2] = a1;
-        op[3] = a1;
-        op += 4;
+        op[0] =(ip[0]+ 1)>>1;
+        op[4] = op[8] = op[12] = (ip[0]>>1);
+        ip ++;
+        op ++;
     }
 }
 
diff --git a/vp8/common/loopfilter.c b/vp8/common/loopfilter.c
index 1cac063..6f57d49 100644
--- a/vp8/common/loopfilter.c
+++ b/vp8/common/loopfilter.c
@@ -149,6 +149,14 @@
     }
 
     lfi->mode_lf_lut[DC_PRED] = 1;
+#if CONFIG_NEWINTRAMODES
+    lfi->mode_lf_lut[D45_PRED] = 1;
+    lfi->mode_lf_lut[D135_PRED] = 1;
+    lfi->mode_lf_lut[D117_PRED] = 1;
+    lfi->mode_lf_lut[D153_PRED] = 1;
+    lfi->mode_lf_lut[D27_PRED] = 1;
+    lfi->mode_lf_lut[D63_PRED] = 1;
+#endif
     lfi->mode_lf_lut[V_PRED] = 1;
     lfi->mode_lf_lut[H_PRED] = 1;
     lfi->mode_lf_lut[TM_PRED] = 1;
diff --git a/vp8/common/postproc.c b/vp8/common/postproc.c
index d88b419..7e7ed3c 100644
--- a/vp8/common/postproc.c
+++ b/vp8/common/postproc.c
@@ -32,6 +32,14 @@
     { RGB_TO_YUV(0x98FB98) },   /* PaleGreen */
     { RGB_TO_YUV(0x00FF00) },   /* Green */
     { RGB_TO_YUV(0xADFF2F) },   /* GreenYellow */
+#if CONFIG_NEWINTRAMODES
+    { RGB_TO_YUV(0x8F0000) },   /* Dark Red */
+    { RGB_TO_YUV(0x008F8F) },   /* Dark Cyan */
+    { RGB_TO_YUV(0x008F8F) },   /* Dark Cyan */
+    { RGB_TO_YUV(0x008F8F) },   /* Dark Cyan */
+    { RGB_TO_YUV(0x8F0000) },   /* Dark Red */
+    { RGB_TO_YUV(0x8F0000) },   /* Dark Red */
+#endif
     { RGB_TO_YUV(0x228B22) },   /* ForestGreen */
     { RGB_TO_YUV(0x006400) },   /* DarkGreen */
     { RGB_TO_YUV(0x98F5FF) },   /* Cadet Blue */
@@ -39,6 +47,7 @@
     { RGB_TO_YUV(0x00008B) },   /* Dark blue */
     { RGB_TO_YUV(0x551A8B) },   /* Purple */
     { RGB_TO_YUV(0xFF0000) }    /* Red */
+    { RGB_TO_YUV(0xCC33FF) },   /* Magenta */
 };
 
 static const unsigned char B_PREDICTION_MODE_colors[B_MODE_COUNT][3] =
diff --git a/vp8/common/pred_common.c b/vp8/common/pred_common.c
index 0e01e6c..f7222a7 100644
--- a/vp8/common/pred_common.c
+++ b/vp8/common/pred_common.c
@@ -277,7 +277,7 @@
     tot_count = count[0] + count[1] + count[2] + count[3];
     if ( tot_count )
     {
-        probs[0] = (vp8_prob)((count[0] * 255) / tot_count);
+        probs[0] = (vp8_prob)((count[0] * 255 + (tot_count >> 1)) / tot_count);
         probs[0] += !probs[0];
     }
     else
@@ -286,7 +286,7 @@
     tot_count -= count[0];
     if ( tot_count )
     {
-        probs[1] = (vp8_prob)((count[1] * 255) / tot_count);
+        probs[1] = (vp8_prob)((count[1] * 255 + (tot_count >> 1)) / tot_count);
         probs[1] += !probs[1];
     }
     else
@@ -295,7 +295,7 @@
     tot_count -= count[1];
     if ( tot_count )
     {
-        probs[2] = (vp8_prob)((count[2] * 255) / tot_count);
+        probs[2] = (vp8_prob)((count[2] * 255 + (tot_count >> 1)) / tot_count);
         probs[2] += !probs[2];
     }
     else
diff --git a/vp8/common/reconintra.c b/vp8/common/reconintra.c
index 501eaca..27b4792 100644
--- a/vp8/common/reconintra.c
+++ b/vp8/common/reconintra.c
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-
+#include <stdio.h>
 #include "vpx_ports/config.h"
 #include "recon.h"
 #include "reconintra.h"
@@ -17,6 +17,217 @@
 /* For skip_recon_mb(), add vp8_build_intra_predictors_mby_s(MACROBLOCKD *x) and
  * vp8_build_intra_predictors_mbuv_s(MACROBLOCKD *x).
  */
+
+#if CONFIG_NEWINTRAMODES
+void d27_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c, h, w, v;
+    int a, b;
+    r = 0;
+    for (c = 0; c < n-2; c++)
+    {
+        if (c&1)
+            a = yleft_col[r + 1];
+        else
+            a = (yleft_col[r] + yleft_col[r + 1] + 1) >> 1;
+        b = yabove_row[c + 2];
+        ypred_ptr[c] = (2 * a + (c + 1) * b + (c + 3)/2) / (c + 3);
+    }
+    for (r = 1; r < n/2 - 1; r++)
+    {
+        for (c = 0; c < n - 2 - 2 * r; c++)
+        {
+            if (c&1)
+                a = yleft_col[r + 1];
+            else
+                a = (yleft_col[r] + yleft_col[r + 1] + 1) >> 1;
+            b = ypred_ptr[(r - 1) * y_stride + c + 2];
+            ypred_ptr[r * y_stride + c] = (2 * a + (c + 1) * b + (c + 3)/2) / (c + 3);
+        }
+    }
+    for (; r < n - 1; ++r)
+    {
+        for (c = 0; c < n; c++)
+        {
+            v = (c & 1 ? yleft_col[r + 1] : (yleft_col[r] + yleft_col[r + 1] + 1) >> 1);
+            h = r - c/2;
+            ypred_ptr[h * y_stride + c] = v;
+        }
+    }
+    c = 0;
+    r = n - 1;
+    ypred_ptr[r * y_stride] = (ypred_ptr[(r - 1) * y_stride] +
+                               yleft_col[r] + 1) >> 1;
+    for (r = n - 2; r >= n/2; --r)
+    {
+        w = c + (n - 1 - r) * 2;
+        ypred_ptr[r * y_stride + w] = (ypred_ptr[(r - 1) * y_stride + w] +
+                                       ypred_ptr[r * y_stride + w - 1] + 1) >> 1;
+    }
+    for (c = 1; c < n; c++)
+    {
+        for (r = n - 1; r >= n/2 + c/2; --r)
+        {
+            w = c + (n - 1 - r) * 2;
+            ypred_ptr[r * y_stride + w] = (ypred_ptr[(r - 1) * y_stride + w] +
+                                           ypred_ptr[r * y_stride + w - 1] + 1) >> 1;
+        }
+    }
+}
+
+void d63_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c, h, w, v;
+    int a, b;
+    c = 0;
+    for (r = 0; r < n-2; r++)
+    {
+        if (r&1)
+            a = yabove_row[c + 1];
+        else
+            a = (yabove_row[c] + yabove_row[c + 1] + 1) >> 1;
+        b = yleft_col[r + 2];
+        ypred_ptr[r * y_stride] = (2 * a + (r + 1) * b + (r + 3)/2) / (r + 3);
+    }
+    for (c = 1; c < n/2 - 1; c++)
+    {
+        for (r = 0; r < n - 2 - 2 * c; r++)
+        {
+            if (r&1)
+                a = yabove_row[c + 1];
+            else
+                a = (yabove_row[c] + yabove_row[c + 1] + 1) >> 1;
+            b = ypred_ptr[(r + 2) * y_stride + c - 1];
+            ypred_ptr[r * y_stride + c] = (2 * a + (c + 1) * b + (c + 3)/2) / (c + 3);
+        }
+    }
+    for (; c < n - 1; ++c)
+    {
+        for (r = 0; r < n; r++)
+        {
+            v = (r & 1 ? yabove_row[c + 1] : (yabove_row[c] + yabove_row[c + 1] + 1) >> 1);
+            w = c - r/2;
+            ypred_ptr[r * y_stride + w] = v;
+        }
+    }
+    r = 0;
+    c = n - 1;
+    ypred_ptr[c] = (ypred_ptr[(c - 1)] + yabove_row[c] + 1) >> 1;
+    for (c = n - 2; c >= n/2; --c)
+    {
+        h = r + (n - 1 - c) * 2;
+        ypred_ptr[h * y_stride + c] = (ypred_ptr[h * y_stride + c - 1] +
+                                       ypred_ptr[(h - 1) * y_stride + c] + 1) >> 1;
+    }
+    for (r = 1; r < n; r++)
+    {
+        for (c = n - 1; c >= n/2 + r/2; --c)
+        {
+            h = r + (n - 1 - c) * 2;
+            ypred_ptr[h * y_stride + c] = (ypred_ptr[h * y_stride + c - 1] +
+                                           ypred_ptr[(h - 1) * y_stride + c] + 1) >> 1;
+        }
+    }
+}
+
+void d45_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c;
+    for (r = 0; r < n - 1; ++r)
+    {
+        for (c = 0; c <= r; ++c)
+        {
+            ypred_ptr[(r - c) * y_stride + c] =
+                (yabove_row[r+1] * (c + 1) +
+                 yleft_col[r+1] * (r - c + 1) + r/2 + 1) / (r + 2);
+        }
+    }
+    for (c = 0; c <= r; ++c)
+    {
+        int yabove_ext = yabove_row[r]; //2*yabove_row[r] - yabove_row[r-1];
+        int yleft_ext = yleft_col[r]; //2*yleft_col[r] - yleft_col[r-1];
+        yabove_ext = (yabove_ext > 255 ? 255 : (yabove_ext < 0 ? 0 : yabove_ext));
+        yleft_ext = (yleft_ext > 255 ? 255 : (yleft_ext < 0 ? 0 : yleft_ext));
+        ypred_ptr[(r - c) * y_stride + c] =
+            (yabove_ext * (c + 1) +
+             yleft_ext * (r - c + 1) + r/2 + 1) / (r + 2);
+    }
+    for (r = 1; r < n; ++r)
+    {
+        for (c = n - r; c < n; ++c)
+            ypred_ptr[r * y_stride + c] = (ypred_ptr[(r - 1) * y_stride + c] +
+                                           ypred_ptr[r * y_stride + c - 1] + 1) >> 1;
+    }
+}
+
+void d117_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c;
+    for (c = 0; c < n; c++)
+        ypred_ptr[c] = (yabove_row[c-1] + yabove_row[c] + 1) >> 1;
+    ypred_ptr += y_stride;
+    for (c = 0; c < n; c++)
+        ypred_ptr[c] = yabove_row[c-1];
+    ypred_ptr += y_stride;
+    for (r = 2; r < n; ++r)
+    {
+        ypred_ptr[0] = yleft_col[r - 2];
+        for (c = 1; c < n; c++)
+            ypred_ptr[c] = ypred_ptr[-2*y_stride+c-1];
+        ypred_ptr += y_stride;
+    }
+}
+
+void d135_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c;
+    ypred_ptr[0] = yabove_row[-1];
+    for (c = 1; c < n; c++)
+        ypred_ptr[c] = yabove_row[c - 1];
+    for (r = 1; r < n; ++r)
+        ypred_ptr[r * y_stride] = yleft_col[r - 1];
+
+    ypred_ptr += y_stride;
+    for (r = 1; r < n; ++r)
+    {
+        for (c = 1; c < n; c++)
+        {
+            ypred_ptr[c] = ypred_ptr[-y_stride + c - 1];
+        }
+        ypred_ptr += y_stride;
+    }
+}
+
+void d153_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col)
+{
+    int r, c;
+    ypred_ptr[0] = (yabove_row[-1] + yleft_col[0] + 1) >> 1;
+    for (r = 1; r < n; r++)
+        ypred_ptr[r * y_stride] = (yleft_col[r-1] + yleft_col[r] + 1) >> 1;
+    ypred_ptr++;
+    ypred_ptr[0] = yabove_row[-1];
+    for (r = 1; r < n; r++)
+        ypred_ptr[r * y_stride] = yleft_col[r-1];
+    ypred_ptr++;
+
+    for (c = 0; c < n - 2; c++)
+        ypred_ptr[c] = yabove_row[c];
+    ypred_ptr += y_stride;
+    for (r = 1; r < n; ++r)
+    {
+        for (c = 0; c < n - 2; c++)
+            ypred_ptr[c] = ypred_ptr[-y_stride+c-2];
+        ypred_ptr += y_stride;
+    }
+}
+#endif  /* CONFIG_NEWINTRAMODES */
+
 void vp8_recon_intra_mbuv(const vp8_recon_rtcd_vtable_t *rtcd, MACROBLOCKD *x)
 {
     int i;
@@ -64,7 +275,6 @@
 
             if (x->left_available)
             {
-
                 for (i = 0; i < 16; i++)
                 {
                     average += yleft_col[i];
@@ -134,6 +344,38 @@
 
     }
     break;
+#if CONFIG_NEWINTRAMODES
+    case D45_PRED:
+    {
+        d45_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+    case D135_PRED:
+    {
+        d135_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+    case D117_PRED:
+    {
+        d117_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+    case D153_PRED:
+    {
+        d153_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+    case D27_PRED:
+    {
+        d27_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+    case D63_PRED:
+    {
+        d63_predictor(ypred_ptr, y_stride, 16,  yabove_row, yleft_col);
+    }
+    break;
+#endif
 #if CONIFG_I8X8
     case I8X8_PRED:
 #endif
@@ -313,6 +555,44 @@
 
     }
     break;
+#if CONFIG_NEWINTRAMODES
+    case D45_PRED:
+    {
+        d45_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d45_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+    case D135_PRED:
+    {
+        d135_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d135_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+    case D117_PRED:
+    {
+        d117_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d117_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+    case D153_PRED:
+    {
+        d153_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d153_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+    case D27_PRED:
+    {
+        d27_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d27_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+    case D63_PRED:
+    {
+        d63_predictor(upred_ptr, uv_stride, 8,  uabove_row, uleft_col);
+        d63_predictor(vpred_ptr, uv_stride, 8,  vabove_row, vleft_col);
+    }
+    break;
+#endif
     case B_PRED:
     case NEARESTMV:
     case NEARMV:
@@ -403,7 +683,6 @@
             {
                 for (c = 0; c < 8; c++)
                 {
-
                     predictor[c] = yabove_row[c];
                 }
                 predictor += 16;
@@ -444,6 +723,38 @@
             }
         }
         break;
+#if CONFIG_NEWINTRAMODES
+    case D45_PRED:
+        {
+            d45_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+    case D135_PRED:
+        {
+            d135_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+    case D117_PRED:
+        {
+            d117_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+    case D153_PRED:
+        {
+            d153_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+    case D27_PRED:
+        {
+            d27_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+    case D63_PRED:
+        {
+            d63_predictor(predictor, 16, 8,  yabove_row, yleft_col);
+        }
+        break;
+#endif
     }
 }
 
@@ -452,7 +763,6 @@
                                int mode, int second_mode,
                                unsigned char *out_predictor)
 {
-    
     unsigned char predictor[2][8*16];
     int i, j;
 
@@ -553,6 +863,38 @@
             }
         }
         break;
+#if CONFIG_NEWINTRAMODES
+    case D45_PRED:
+        {
+            d45_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+    case D135_PRED:
+        {
+            d135_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+    case D117_PRED:
+        {
+            d117_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+    case D153_PRED:
+        {
+            d153_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+    case D27_PRED:
+        {
+            d27_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+    case D63_PRED:
+        {
+            d63_predictor(predictor, 8, 4,  above_row, left_col);
+        }
+        break;
+#endif
     }
 }
 
diff --git a/vp8/common/reconintra.h b/vp8/common/reconintra.h
index 47e4792..03b50f2 100644
--- a/vp8/common/reconintra.h
+++ b/vp8/common/reconintra.h
@@ -12,6 +12,22 @@
 #ifndef __INC_RECONINTRA_H
 #define __INC_RECONINTRA_H
 
+#include "blockd.h"
+#if CONFIG_NEWINTRAMODES
+void d45_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col);
+void d135_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col);
+void d116_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col);
+void d153_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                    unsigned char *yabove_row, unsigned char *yleft_col);
+void d27_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col);
+void d64_predictor(unsigned char *ypred_ptr, int y_stride, int n,
+                   unsigned char *yabove_row, unsigned char *yleft_col);
+#endif  /* CONFIG_NEWINTRAMODES */
+
 extern void init_intra_left_above_pixels(MACROBLOCKD *x);
 
 #endif
diff --git a/vp8/common/x86/x86_systemdependent.c b/vp8/common/x86/x86_systemdependent.c
index 5300950..b88eb3d 100644
--- a/vp8/common/x86/x86_systemdependent.c
+++ b/vp8/common/x86/x86_systemdependent.c
@@ -84,10 +84,12 @@
         rtcd->recon.recon2      = vp8_recon2b_sse2;
         rtcd->recon.recon4      = vp8_recon4b_sse2;
         rtcd->recon.copy16x16   = vp8_copy_mem16x16_sse2;
+#if CONFIG_NEWINTRAMODES == 0
         rtcd->recon.build_intra_predictors_mbuv =
             vp8_build_intra_predictors_mbuv_sse2;
         rtcd->recon.build_intra_predictors_mbuv_s =
             vp8_build_intra_predictors_mbuv_s_sse2;
+#endif
 
         rtcd->idct.iwalsh16     = vp8_short_inv_walsh4x4_sse2;
 
@@ -122,7 +124,7 @@
 
     if (flags & HAS_SSSE3)
     {
-#if CONFIG_ENHANCED_INTERP == 0 && CONFIG_HIGH_PRECISION_MV == 0
+#if CONFIG_ENHANCED_INTERP == 0 && CONFIG_HIGH_PRECISION_MV == 0 && CONFIG_SIXTEENTH_SUBPEL_UV == 0
         rtcd->subpix.sixtap16x16   = vp8_sixtap_predict16x16_ssse3;
         rtcd->subpix.sixtap8x8     = vp8_sixtap_predict8x8_ssse3;
         rtcd->subpix.sixtap8x4     = vp8_sixtap_predict8x4_ssse3;
@@ -131,10 +133,12 @@
         rtcd->subpix.bilinear8x8   = vp8_bilinear_predict8x8_ssse3;
 #endif
 
+#if CONFIG_NEWINTRAMODES == 0
         rtcd->recon.build_intra_predictors_mbuv =
             vp8_build_intra_predictors_mbuv_ssse3;
         rtcd->recon.build_intra_predictors_mbuv_s =
             vp8_build_intra_predictors_mbuv_s_ssse3;
+#endif
     }
 #endif
 
diff --git a/vp8/decoder/decodframe.c b/vp8/decoder/decodframe.c
index 8776532..d375cdd 100644
--- a/vp8/decoder/decodframe.c
+++ b/vp8/decoder/decodframe.c
@@ -45,13 +45,29 @@
 #endif
 
 #if CONFIG_NEWUPDATE
+
+static int merge_index(int v, int n, int modulus)
+{
+    int max1 = (n-1 - modulus/2)/modulus + 1;
+    if (v < max1) v = v * modulus + modulus/2;
+    else
+    {
+        int w;
+        v -= max1;
+        w = v;
+        v += (v + modulus-modulus/2)/modulus;
+        while (v%modulus == modulus/2 ||
+               w != v - (v + modulus-modulus/2)/modulus) v++;
+    }
+    return v;
+}
+
 static int inv_remap_prob(int v, int m)
 {
     const int n = 256;
-    int i;
-    //if (v <= n - 2 - s) v += s; else v =  n - 2 - v;
-    //v = ((v&240)>>4) | ((v&15)<<4);
-    v = (v%15)*17 + (v/15);
+    const int modulus = MODULUS_PARAM;
+    int i, w;
+    v = merge_index(v, n-1, modulus);
     if ((m<<1)<=n) {
         i = inv_recenter_nonneg(v+1, m);
     } else {
@@ -59,6 +75,12 @@
     }
     return i;
 }
+
+static vp8_prob read_prob_diff_update(vp8_reader *const bc, int oldp)
+{
+    int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
+    return (vp8_prob)inv_remap_prob(delp, oldp);
+}
 #endif
 
 void vp8cx_init_de_quantizer(VP8D_COMP *pbi)
@@ -811,11 +833,7 @@
                         {
                         vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
                         int u = vp8_read(bc, vp8_coef_update_probs [i][j][k][l]);
-                        if (u)
-                        {
-                            int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                            *p = (vp8_prob)inv_remap_prob(delp, *p);
-                        }
+                        if (u) *p = read_prob_diff_update(bc, *p);
                         }
                     }
             }
@@ -839,11 +857,7 @@
                             {
                             vp8_prob *const p = pc->fc.coef_probs_8x8 [i][j][k] + l;
                             int u = vp8_read(bc, vp8_coef_update_probs_8x8 [i][j][k][l]);
-                            if (u)
-                            {
-                                int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                                *p = (vp8_prob)inv_remap_prob(delp, *p);
-                            }
+                            if (u) *p = read_prob_diff_update(bc, *p);
                             }
                         }
                 }
@@ -874,11 +888,7 @@
                         {
                         vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
                         int u = vp8_read(bc, vp8_coef_update_probs [i][j][k][l]);
-                        if (u)
-                        {
-                            int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                            *p = (vp8_prob)inv_remap_prob(delp, *p);
-                        }
+                        if (u) *p = read_prob_diff_update(bc, *p);
                         }
                     }
         }
@@ -902,11 +912,7 @@
                             vp8_prob *const p = pc->fc.coef_probs_8x8 [i][j][k] + l;
 
                             int u = vp8_read(bc, vp8_coef_update_probs_8x8 [i][j][k][l]);
-                            if (u)
-                            {
-                                int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                                *p = (vp8_prob)inv_remap_prob(delp, *p);
-                            }
+                            if (u) *p = read_prob_diff_update(bc, *p);
                             }
                         }
             }
@@ -945,10 +951,7 @@
                         if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
                         {
 #if CONFIG_NEWUPDATE
-                            int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                            //printf("delp = %d/%d", *p, delp);
-                            *p = (vp8_prob)inv_remap_prob(delp, *p);
-                            //printf("/%d\n", *p);
+                            *p = read_prob_diff_update(bc, *p);
 #else
                             *p = (vp8_prob)vp8_read_literal(bc, 8);
 #endif
@@ -982,8 +985,7 @@
                         if (vp8_read(bc, vp8_coef_update_probs_8x8 [i][j][k][l]))
                         {
 #if CONFIG_NEWUPDATE
-                            int delp = vp8_decode_term_subexp(bc, SUBEXP_PARAM, 255);
-                            *p = (vp8_prob)inv_remap_prob(delp, *p);
+                            *p = read_prob_diff_update(bc, *p);
 #else
                             *p = (vp8_prob)vp8_read_literal(bc, 8);
 #endif
diff --git a/vp8/decoder/onyxd_int.h b/vp8/decoder/onyxd_int.h
index 289808e..c7ee834 100644
--- a/vp8/decoder/onyxd_int.h
+++ b/vp8/decoder/onyxd_int.h
@@ -56,8 +56,8 @@
     INT16 *qcoeff_start_ptr;
     BOOL_DECODER *current_bc;
 
-    vp8_prob const *coef_probs[4];
-    vp8_prob const *coef_probs_8x8[4];
+    vp8_prob const *coef_probs[BLOCK_TYPES];
+    vp8_prob const *coef_probs_8x8[BLOCK_TYPES_8X8];
 
     UINT8 eob[25];
 
diff --git a/vp8/encoder/bitstream.c b/vp8/encoder/bitstream.c
index a96a75e..cf22dd4 100644
--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -23,8 +23,6 @@
 #include "vpx_mem/vpx_mem.h"
 #include "bitstream.h"
 
-#include "vp8/common/defaultcoefcounts.h"
-
 #include "vp8/common/seg_common.h"
 #include "vp8/common/pred_common.h"
 #include "vp8/common/entropy.h"
@@ -34,15 +32,18 @@
 #endif
 
 #ifdef ENTROPY_STATS
-int intra_mode_stats[10][10][10];
+int intra_mode_stats[VP8_BINTRAMODES]
+                    [VP8_BINTRAMODES]
+                    [VP8_BINTRAMODES];
 static unsigned int tree_update_hist [BLOCK_TYPES]
                                      [COEF_BANDS]
                                      [PREV_COEF_CONTEXTS]
-                                     [ENTROPY_NODES][2]={0};
+                                     [ENTROPY_NODES] [2]={0};
 static unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                          [COEF_BANDS]
                                          [PREV_COEF_CONTEXTS]
                                          [ENTROPY_NODES] [2]={0};
+
 extern unsigned int active_section;
 #endif
 
@@ -64,19 +65,41 @@
         update_bits[i] = vp8_count_term_subexp(i, SUBEXP_PARAM, 255);
 }
 
+static int split_index(int i, int n, int modulus)
+{
+    int max1 = (n-1 - modulus/2)/modulus + 1;
+    if (i%modulus == modulus/2) i = i/modulus;
+    else i = max1 + i - (i + modulus-modulus/2)/modulus;
+    return i;
+}
+
 static int remap_prob(int v, int m)
 {
     const int n = 256;
+    const int modulus = MODULUS_PARAM;
+    const int max1 = (n-2-modulus/2+modulus-1)/modulus;
     int i;
     if ((m<<1)<=n)
         i = recenter_nonneg(v, m) - 1;
     else
         i = recenter_nonneg(n-1-v, n-1-m) - 1;
-    //if (i >= s) i -= s; else i = n - 2 - i;
-    //i = ((i>>4)&15) | ((i((i>>4)&15) | ((i&15)<<4);&15)<<4);
-    i = (i%17)*15 + (i/17);
+
+    i = split_index(i, n-1, modulus);
     return i;
 }
+
+static void write_prob_diff_update(vp8_writer *const w,
+                                   vp8_prob newp, vp8_prob oldp)
+{
+    int delp = remap_prob(newp, oldp);
+    vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
+}
+
+static int prob_diff_update_cost(vp8_prob newp, vp8_prob oldp)
+{
+    int delp = remap_prob(newp, oldp);
+    return update_bits[delp]*256;
+}
 #endif
 
 static void update_mode(
@@ -194,21 +217,11 @@
 static void update_refpred_stats( VP8_COMP *cpi )
 {
     VP8_COMMON *const cm = & cpi->common;
-    MACROBLOCKD *const xd = & cpi->mb.e_mbd;
-
-    int mb_row, mb_col;
     int i;
     int tot_count;
-    int ref_pred_count[PREDICTION_PROBS][2];
     vp8_prob new_pred_probs[PREDICTION_PROBS];
-    unsigned char pred_context;
-    unsigned char pred_flag;
-
     int old_cost, new_cost;
 
-    // Clear the prediction hit counters
-    vpx_memset(ref_pred_count, 0, sizeof(ref_pred_count));
-
     // Set the prediction probability structures to defaults
     if ( cm->frame_type == KEY_FRAME )
     {
@@ -222,47 +235,17 @@
     }
     else
     {
-        // For non-key frames.......
-
-        // Scan through the macroblocks and collate prediction counts.
-        xd->mode_info_context = cm->mi;
-        for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
-        {
-            for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
-            {
-                // Get the prediction context and status
-                pred_flag = get_pred_flag( xd, PRED_REF );
-                pred_context = get_pred_context( cm, xd, PRED_REF );
-
-                // Count prediction success
-                ref_pred_count[pred_context][pred_flag]++;
-
-                // Step on to the next mb
-                xd->mode_info_context++;
-            }
-
-            // this is to account for the border in mode_info_context
-            xd->mode_info_context++;
-        }
-
         // From the prediction counts set the probabilities for each context
         for ( i = 0; i < PREDICTION_PROBS; i++ )
         {
-            // MB reference frame not relevent to key frame encoding
-            if ( cm->frame_type != KEY_FRAME )
+            tot_count = cpi->ref_pred_count[i][0] + cpi->ref_pred_count[i][1];
+            if ( tot_count )
             {
-                // Work out the probabilities for the reference frame predictor
-                tot_count = ref_pred_count[i][0] + ref_pred_count[i][1];
-                if ( tot_count )
-                {
-                    new_pred_probs[i] =
-                        ( ref_pred_count[i][0] * 255 ) / tot_count;
+                new_pred_probs[i] =
+                    ( cpi->ref_pred_count[i][0] * 255 + (tot_count >> 1)) / tot_count;
 
-                    // Clamp to minimum allowed value
-                    new_pred_probs[i] += !new_pred_probs[i];
-                }
-                else
-                    new_pred_probs[i] = 128;
+                // Clamp to minimum allowed value
+                new_pred_probs[i] += !new_pred_probs[i];
             }
             else
                 new_pred_probs[i] = 128;
@@ -270,12 +253,12 @@
             // Decide whether or not to update the reference frame probs.
             // Returned costs are in 1/256 bit units.
             old_cost =
-                (ref_pred_count[i][0] * vp8_cost_zero(cm->ref_pred_probs[i])) +
-                (ref_pred_count[i][1] * vp8_cost_one(cm->ref_pred_probs[i]));
+                (cpi->ref_pred_count[i][0] * vp8_cost_zero(cm->ref_pred_probs[i])) +
+                (cpi->ref_pred_count[i][1] * vp8_cost_one(cm->ref_pred_probs[i]));
 
             new_cost =
-                (ref_pred_count[i][0] * vp8_cost_zero(new_pred_probs[i])) +
-                (ref_pred_count[i][1] * vp8_cost_one(new_pred_probs[i]));
+                (cpi->ref_pred_count[i][0] * vp8_cost_zero(new_pred_probs[i])) +
+                (cpi->ref_pred_count[i][1] * vp8_cost_one(new_pred_probs[i]));
 
             // Cost saving must be >= 8 bits (2048 in these units)
             if ( (old_cost - new_cost) >= 2048 )
@@ -329,22 +312,28 @@
 {
     const int old_b = vp8_cost_branch256(ct, oldp);
     const int new_b = vp8_cost_branch256(ct, newp);
-#if CONFIG_NEWUPDATE
-    const int delp = remap_prob(newp, oldp);
-    const int update_b = update_bits[delp]*256 + vp8_cost_upd256;
-#else
     const int update_b = 2048 + vp8_cost_upd256;
-#endif
     return (old_b - new_b - update_b);
 }
 
 #if CONFIG_NEWUPDATE
-static int prob_update_savings_search(const unsigned int *ct,
+static int prob_diff_update_savings(const unsigned int *ct,
+                               const vp8_prob oldp, const vp8_prob newp,
+                               const vp8_prob upd)
+{
+    const int old_b = vp8_cost_branch256(ct, oldp);
+    const int new_b = vp8_cost_branch256(ct, newp);
+    const int update_b = (newp == oldp ? 0 :
+        prob_diff_update_cost(newp, oldp) + vp8_cost_upd256);
+    return (old_b - new_b - update_b);
+}
+
+static int prob_diff_update_savings_search(const unsigned int *ct,
                                       const vp8_prob oldp, vp8_prob *bestp,
                                       const vp8_prob upd)
 {
     const int old_b = vp8_cost_branch256(ct, oldp);
-    int new_b, delp, update_b, savings, bestsavings, step;
+    int new_b, update_b, savings, bestsavings, step;
     vp8_prob newp, bestnewp;
 
     bestsavings = 0;
@@ -354,8 +343,7 @@
     for (newp = *bestp; newp != oldp; newp+=step)
     {
         new_b = vp8_cost_branch256(ct, newp);
-        delp = remap_prob(newp, oldp);
-        update_b = update_bits[delp]*256 + vp8_cost_upd256;
+        update_b = prob_diff_update_cost(newp, oldp) + vp8_cost_upd256;
         savings = old_b - new_b - update_b;
         if (savings > bestsavings)
         {
@@ -1197,6 +1185,7 @@
     int row_delta[4] = { 0, +1,  0, -1};
     int col_delta[4] = {+1, -1, +1, +1};
 
+    //printf("write_kfmodes\n");
     if (c->mb_no_coeff_skip)
     {
         // Divide by 0 check. 0 case possible with segment features
@@ -1322,6 +1311,7 @@
 #endif
 
                         write_bmode(bc, bm, c->kf_bmode_prob [A] [L]);
+                        //printf("    mode: %d\n", bm);
 #if CONFIG_COMP_INTRA_PRED
                         if (uses_second)
                         {
@@ -1335,12 +1325,16 @@
                 {
                     write_i8x8_mode(bc, m->bmi[0].as_mode.first,
                                     c->i8x8_mode_prob);
+                    //printf("    mode: %d\n", m->bmi[0].as_mode.first); fflush(stdout);
                     write_i8x8_mode(bc, m->bmi[2].as_mode.first,
                                     c->i8x8_mode_prob);
+                    //printf("    mode: %d\n", m->bmi[2].as_mode.first); fflush(stdout);
                     write_i8x8_mode(bc, m->bmi[8].as_mode.first,
                                     c->i8x8_mode_prob);
+                    //printf("    mode: %d\n", m->bmi[8].as_mode.first); fflush(stdout);
                     write_i8x8_mode(bc, m->bmi[10].as_mode.first,
                                     c->i8x8_mode_prob);
+                    //printf("    mode: %d\n", m->bmi[10].as_mode.first); fflush(stdout);
                 }
                 else
                     write_uv_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
@@ -1519,7 +1513,7 @@
 #endif
 
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold) u = 1;
                     if (u)
@@ -1558,7 +1552,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                         cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold) u = 1;
 #else
@@ -1574,8 +1568,7 @@
 #endif
                     if (u)
                     { /* send/use new probability */
-                        int delp = remap_prob(newp, *Pold);
-                        vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
+                        write_prob_diff_update(w, newp, *Pold);
                         *Pold = newp;
                     }
 
@@ -1608,7 +1601,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct_8x8 [i][j][k][t],
                             *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold)
@@ -1650,7 +1643,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                         cpi->frame_branch_ct_8x8 [i][j][k][t],
                         *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold)
@@ -1670,8 +1663,7 @@
                     if (u)
                     {
                         /* send/use new probability */
-                        int delp = remap_prob(newp, *Pold);
-                        vp8_encode_term_subexp( w, delp, SUBEXP_PARAM, 255);
+                        write_prob_diff_update(w, newp, *Pold);
                         *Pold = newp;
                     }
                 }
@@ -1704,18 +1696,18 @@
             {
                 for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
                 {
-#if CONFIG_EXPANDED_COEF_CONTEXT
-                    if (k >=3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
-                        continue;
-#endif
                     vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
                     vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
                     const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
                     int s;
                     int u = 0;
+#if CONFIG_EXPANDED_COEF_CONTEXT
+                    if (k >=3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
+                        continue;
+#endif
 
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold) u = 1;
                     if (u)
@@ -1756,7 +1748,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                         cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold) u = 1;
 #else
@@ -1771,8 +1763,7 @@
 #endif
                     if (u)
                     { /* send/use new probability */
-                        int delp = remap_prob(newp, *Pold);
-                        vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
+                        write_prob_diff_update(w, newp, *Pold);
                         *Pold = newp;
                     }
                 }
@@ -1803,7 +1794,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct_8x8 [i][j][k][t],
                             *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold)
@@ -1847,7 +1838,7 @@
                         continue;
 #endif
 #if defined(SEARCH_NEWP)
-                        s = prob_update_savings_search(
+                        s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct_8x8 [i][j][k][t],
                             *Pold, &newp, upd);
                         if (s > 0 && newp != *Pold)
@@ -1867,8 +1858,7 @@
                         if (u)
                         {
                             /* send/use new probability */
-                            int delp = remap_prob(newp, *Pold);
-                            vp8_encode_term_subexp( w, delp, SUBEXP_PARAM, 255);
+                            write_prob_diff_update(w, newp, *Pold);
                             *Pold = newp;
                         }
                 }
@@ -1920,7 +1910,7 @@
                         continue;
 #endif
 #if CONFIG_NEWUPDATE && defined(SEARCH_NEWP)
-                    s = prob_update_savings_search(
+                    s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct [i][j][k][t],
                             *Pold, &newp, upd);
                     if (s > 0 && newp != *Pold)
@@ -1992,7 +1982,7 @@
 #endif
 
 #if CONFIG_NEWUPDATE && defined(SEARCH_NEWP)
-                        s = prob_update_savings_search(
+                        s = prob_diff_update_savings_search(
                             cpi->frame_branch_ct [i][j][k][t],
                             *Pold, &newp, upd);
                         if (s > 0 && newp != *Pold)
@@ -2015,9 +2005,7 @@
                         {
                             /* send/use new probability */
 #if CONFIG_NEWUPDATE
-                            vp8_encode_term_subexp(
-                                w, remap_prob(newp, *Pold), SUBEXP_PARAM, 255);
-                            //printf("delp = %d/%d/%d\n", *Pold, remap_prob(newp, *Pold, 256), newp);
+                            write_prob_diff_update(w, newp, *Pold);
 #else
                             vp8_write_literal(w, newp, 8);
 #endif
@@ -2068,7 +2056,7 @@
                             continue;
 #endif
 #if CONFIG_NEWUPDATE && defined(SEARCH_NEWP)
-                        const int s = prob_update_savings_search(ct, oldp, &newp, upd);
+                        const int s = prob_diff_update_savings_search(ct, oldp, &newp, upd);
                         const int u = s > 0 && newp != oldp ? 1 : 0;
                         if (u)
                             savings += s - (int)(vp8_cost_zero(upd));
@@ -2124,7 +2112,7 @@
                             const vp8_prob oldp = *Pold;
                             const vp8_prob upd = vp8_coef_update_probs_8x8 [i][j][k][t];
 #if CONFIG_NEWUPDATE && defined(SEARCH_NEWP)
-                            const int s = prob_update_savings_search(ct, oldp, &newp, upd);
+                            const int s = prob_diff_update_savings_search(ct, oldp, &newp, upd);
                             const int u = s > 0 && newp != oldp ? 1 : 0;
 #else
                             const int s = prob_update_savings(ct, oldp, newp, upd);
@@ -2144,8 +2132,7 @@
                             {
                                 /* send/use new probability */
 #if CONFIG_NEWUPDATE
-                                vp8_encode_term_subexp(
-                                    w, remap_prob(newp, oldp), SUBEXP_PARAM, 255);
+                                write_prob_diff_update(w, newp, oldp);
 #else
                                 vp8_write_literal(w, newp, 8);
 #endif
diff --git a/vp8/encoder/block.h b/vp8/encoder/block.h
index 2b21fa2..98bc132 100644
--- a/vp8/encoder/block.h
+++ b/vp8/encoder/block.h
@@ -124,7 +124,7 @@
 #endif
     int mbmode_cost[2][MB_MODE_COUNT];
     int intra_uv_mode_cost[2][MB_MODE_COUNT];
-    unsigned int bmode_costs[10][10][10];
+    unsigned int bmode_costs[VP8_BINTRAMODES][VP8_BINTRAMODES][VP8_BINTRAMODES];
     unsigned int i8x8_mode_costs[MB_MODE_COUNT];
     unsigned int inter_bmode_costs[B_MODE_COUNT];
 
diff --git a/vp8/encoder/dct.c b/vp8/encoder/dct.c
index c2f2d11..6f9c68e 100644
--- a/vp8/encoder/dct.c
+++ b/vp8/encoder/dct.c
@@ -12,10 +12,249 @@
 #include <math.h>
 #include "vpx_ports/config.h"
 
+#if CONFIG_INT_8X8FDCT
+
+static const int xC1S7 = 16069;
+static const int xC2S6 = 15137;
+static const int xC3S5 = 13623;
+static const int xC4S4 = 11585;
+static const int xC5S3 =  9102;
+static const int xC6S2 =  6270;
+static const int xC7S1 =  3196;
+
+#define SHIFT_BITS 14
+#define DOROUND(X) X += (1<<(SHIFT_BITS-1));
+
+#define FINAL_SHIFT 3
+#define FINAL_ROUNDING (1<<(FINAL_SHIFT -1))
+#define IN_SHIFT (FINAL_SHIFT+1)
 
 
+void vp8_short_fdct8x8_c ( short * InputData, short * OutputData, int pitch)
+{
+    int loop;
+    int short_pitch = pitch>>1;
+    int is07, is12, is34, is56;
+    int is0734, is1256;
+    int id07, id12, id34, id56;
+    int irot_input_x, irot_input_y;
+    int icommon_product1;      // Re-used product  (c4s4 * (s12 - s56))
+    int icommon_product2;      // Re-used product  (c4s4 * (d12 + d56))
+    int temp1, temp2;          // intermediate variable for computation
+
+    int  InterData[64];
+    int  *ip = InterData;
+    short *op = OutputData;
+
+    for (loop = 0; loop < 8; loop++)
+    {
+        // Pre calculate some common sums and differences.
+        is07 = (InputData[0] + InputData[7])<<IN_SHIFT;
+        is12 = (InputData[1] + InputData[2])<<IN_SHIFT;
+        is34 = (InputData[3] + InputData[4])<<IN_SHIFT;
+        is56 = (InputData[5] + InputData[6])<<IN_SHIFT;
+        id07 = (InputData[0] - InputData[7])<<IN_SHIFT;
+        id12 = (InputData[1] - InputData[2])<<IN_SHIFT;
+        id34 = (InputData[3] - InputData[4])<<IN_SHIFT;
+        id56 = (InputData[5] - InputData[6])<<IN_SHIFT;
+
+        is0734 = is07 + is34;
+        is1256 = is12 + is56;
+
+        // Pre-Calculate some common product terms.
+        icommon_product1 = xC4S4*(is12 - is56);
+        DOROUND(icommon_product1)
+        icommon_product1>>=SHIFT_BITS;
+
+        icommon_product2 = xC4S4*(id12 + id56);
+        DOROUND(icommon_product2)
+        icommon_product2>>=SHIFT_BITS;
 
 
+        ip[0] = (xC4S4*(is0734 + is1256));
+        DOROUND(ip[0]);
+        ip[0] >>= SHIFT_BITS;
+
+        ip[4] = (xC4S4*(is0734 - is1256));
+        DOROUND(ip[4]);
+        ip[4] >>= SHIFT_BITS;
+
+        // Define inputs to rotation for outputs 2 and 6
+        irot_input_x = id12 - id56;
+        irot_input_y = is07 - is34;
+
+        // Apply rotation for outputs 2 and 6.
+        temp1=xC6S2*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC2S6*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[2] = temp1 + temp2;
+
+        temp1=xC6S2*irot_input_y;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC2S6*irot_input_x ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[6] = temp1 -temp2 ;
+
+        // Define inputs to rotation for outputs 1 and 7
+        irot_input_x = icommon_product1 + id07;
+        irot_input_y = -( id34 + icommon_product2 );
+
+        // Apply rotation for outputs 1 and 7.
+        temp1=xC1S7*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC7S1*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[1] = temp1 - temp2;
+
+        temp1=xC7S1*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC1S7*irot_input_y ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[7] = temp1 + temp2 ;
+
+        // Define inputs to rotation for outputs 3 and 5
+        irot_input_x = id07 - icommon_product1;
+        irot_input_y = id34 - icommon_product2;
+
+        // Apply rotation for outputs 3 and 5.
+        temp1=xC3S5*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC5S3*irot_input_y ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[3] = temp1 - temp2 ;
+
+
+        temp1=xC5S3*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC3S5*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        ip[5] = temp1 + temp2;
+
+        // Increment data pointer for next row
+        InputData += short_pitch ;
+        ip += 8;
+    }
+
+    // Performed DCT on rows, now transform the columns
+    ip = InterData;
+    for (loop = 0; loop < 8; loop++)
+    {
+        // Pre calculate some common sums and differences.
+        is07 = ip[0 * 8] + ip[7 * 8];
+        is12 = ip[1 * 8] + ip[2 * 8];
+        is34 = ip[3 * 8] + ip[4 * 8];
+        is56 = ip[5 * 8] + ip[6 * 8];
+
+        id07 = ip[0 * 8] - ip[7 * 8];
+        id12 = ip[1 * 8] - ip[2 * 8];
+        id34 = ip[3 * 8] - ip[4 * 8];
+        id56 = ip[5 * 8] - ip[6 * 8];
+
+        is0734 = is07 + is34;
+        is1256 = is12 + is56;
+
+        // Pre-Calculate some common product terms
+        icommon_product1 = xC4S4*(is12 - is56) ;
+        icommon_product2 = xC4S4*(id12 + id56) ;
+        DOROUND(icommon_product1)
+        DOROUND(icommon_product2)
+        icommon_product1>>=SHIFT_BITS;
+        icommon_product2>>=SHIFT_BITS;
+
+
+        temp1 = xC4S4*(is0734 + is1256) ;
+        temp2 = xC4S4*(is0734 - is1256) ;
+        DOROUND(temp1);
+        DOROUND(temp2);
+        temp1>>=SHIFT_BITS;
+
+        temp2>>=SHIFT_BITS;
+        op[0*8] = (temp1 + FINAL_ROUNDING)>>FINAL_SHIFT;
+        op[4*8] = (temp2 + FINAL_ROUNDING)>>FINAL_SHIFT;
+
+        // Define inputs to rotation for outputs 2 and 6
+        irot_input_x = id12 - id56;
+        irot_input_y = is07 - is34;
+
+        // Apply rotation for outputs 2 and 6.
+        temp1=xC6S2*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC2S6*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[2*8] = (temp1 + temp2 + FINAL_ROUNDING)>>FINAL_SHIFT;
+
+        temp1=xC6S2*irot_input_y;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC2S6*irot_input_x ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[6*8] = (temp1 -temp2 + FINAL_ROUNDING)>>FINAL_SHIFT ;
+
+        // Define inputs to rotation for outputs 1 and 7
+        irot_input_x = icommon_product1 + id07;
+        irot_input_y = -( id34 + icommon_product2 );
+
+        // Apply rotation for outputs 1 and 7.
+        temp1=xC1S7*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC7S1*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[1*8] = (temp1 - temp2 + FINAL_ROUNDING)>>FINAL_SHIFT;
+
+        temp1=xC7S1*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC1S7*irot_input_y ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[7*8] = (temp1 + temp2 + FINAL_ROUNDING)>>FINAL_SHIFT;
+
+        // Define inputs to rotation for outputs 3 and 5
+        irot_input_x = id07 - icommon_product1;
+        irot_input_y = id34 - icommon_product2;
+
+        // Apply rotation for outputs 3 and 5.
+        temp1=xC3S5*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC5S3*irot_input_y ;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[3*8] =  (temp1 - temp2 + FINAL_ROUNDING)>>FINAL_SHIFT ;
+
+
+        temp1=xC5S3*irot_input_x;
+        DOROUND(temp1);
+        temp1>>=SHIFT_BITS;
+        temp2=xC3S5*irot_input_y;
+        DOROUND(temp2);
+        temp2>>=SHIFT_BITS;
+        op[5*8] =  (temp1 + temp2 + FINAL_ROUNDING)>>FINAL_SHIFT;
+
+        // Increment data pointer for next column.
+        ip ++;
+        op ++;
+    }
+}
+#else
 
 void vp8_short_fdct8x8_c(short *block, short *coefs, int pitch)
 {
@@ -106,7 +345,7 @@
   return;
 }
 
-
+#endif
 
 void vp8_short_fhaar2x2_c(short *input, short *output, int pitch) //pitch = 8
 {
@@ -181,52 +420,41 @@
 {
     int i;
     int a1, b1, c1, d1;
-    int a2, b2, c2, d2;
     short *ip = input;
     short *op = output;
-
+    int pitch_short = pitch >>1;
 
     for (i = 0; i < 4; i++)
     {
-        a1 = ((ip[0] + ip[2]));
-        d1 = ((ip[1] + ip[3]));
-        c1 = ((ip[1] - ip[3]));
-        b1 = ((ip[0] - ip[2]));
+        a1 = ip[0 * pitch_short] + ip[3 * pitch_short];
+        b1 = ip[1 * pitch_short] + ip[2 * pitch_short];
+        c1 = ip[1 * pitch_short] - ip[2 * pitch_short];
+        d1 = ip[0 * pitch_short] - ip[3 * pitch_short];
 
-        op[0] = a1 + d1;
-        op[1] = b1 + c1;
-        op[2] = b1 - c1;
-        op[3] = a1 - d1;
-        ip += pitch / 2;
-        op += 4;
+        op[0] = (a1 + b1 + 1)>>1;
+        op[4] = (c1 + d1)>>1;
+        op[8] = (a1 - b1)>>1;
+        op[12]= (d1 - c1)>>1;
+
+        ip++;
+        op++;
     }
-
     ip = output;
     op = output;
 
     for (i = 0; i < 4; i++)
     {
-        a1 = ip[0] + ip[8];
-        d1 = ip[4] + ip[12];
-        c1 = ip[4] - ip[12];
-        b1 = ip[0] - ip[8];
+        a1 = ip[0] + ip[3];
+        b1 = ip[1] + ip[2];
+        c1 = ip[1] - ip[2];
+        d1 = ip[0] - ip[3];
 
-        a2 = a1 + d1;
-        b2 = b1 + c1;
-        c2 = b1 - c1;
-        d2 = a1 - d1;
+        op[0] = (a1 + b1 + 1)>>1;
+        op[1] = (c1 + d1)>>1;
+        op[2] = (a1 - b1)>>1;
+        op[3] = (d1 - c1)>>1;
 
-        a2 += a2<0;
-        b2 += b2<0;
-        c2 += c2<0;
-        d2 += d2<0;
-
-        op[0] = (a2+1) >> 2;
-        op[4] = (b2+1) >> 2;
-        op[8] = (c2+1) >> 2;
-        op[12]= (d2+1) >> 2;
-
-        ip++;
-        op++;
+        ip += 4;
+        op += 4;
     }
-}
+}
\ No newline at end of file
diff --git a/vp8/encoder/encodeframe.c b/vp8/encoder/encodeframe.c
index a044f8c..364d6ed 100644
--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -71,22 +71,14 @@
 
 
 #ifdef MODE_STATS
-unsigned int inter_y_modes[MB_MODE_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-unsigned int inter_uv_modes[VP8_UV_MODES] = {0, 0, 0, 0};
-unsigned int inter_b_modes[B_MODE_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-unsigned int y_modes[VP8_YMODES] = {0, 0, 0, 0, 0, 0};
-unsigned int i8x8_modes[VP8_I8X8_MODES]={0  };
-unsigned int uv_modes[VP8_UV_MODES] = {0, 0, 0, 0};
-unsigned int uv_modes_y[VP8_YMODES][VP8_UV_MODES]=
-{
-{0, 0, 0, 0},
-{0, 0, 0, 0},
-{0, 0, 0, 0},
-{0, 0, 0, 0},
-{0, 0, 0, 0},
-{0, 0, 0, 0}
-};
-unsigned int b_modes[B_MODE_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+unsigned int inter_y_modes[MB_MODE_COUNT];
+unsigned int inter_uv_modes[VP8_UV_MODES];
+unsigned int inter_b_modes[B_MODE_COUNT];
+unsigned int y_modes[VP8_YMODES];
+unsigned int i8x8_modes[VP8_I8X8_MODES];
+unsigned int uv_modes[VP8_UV_MODES];
+unsigned int uv_modes_y[VP8_YMODES][VP8_UV_MODES];
+unsigned int b_modes[B_MODE_COUNT];
 #endif
 
 
@@ -628,6 +620,23 @@
         }
         else
         {
+            int seg_id;
+
+            if (xd->segmentation_enabled && cpi->seg0_cnt > 0 &&
+                !segfeature_active( xd, 0, SEG_LVL_REF_FRAME ) &&
+                segfeature_active( xd, 1, SEG_LVL_REF_FRAME ) &&
+                check_segref(xd, 1, INTRA_FRAME)  +
+                check_segref(xd, 1, LAST_FRAME)   +
+                check_segref(xd, 1, GOLDEN_FRAME) +
+                check_segref(xd, 1, ALTREF_FRAME) == 1)
+            {
+                cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt;
+            }
+            else
+            {
+                cpi->seg0_progress = (((mb_col & ~1) * 2 + (mb_row & ~1) * cm->mb_cols + i) << 16) / cm->MBs;
+            }
+
             *totalrate += vp8cx_pick_mode_inter_macroblock(cpi, x,
                                                            recon_yoffset,
                                                            recon_uvoffset);
@@ -635,6 +644,26 @@
             // Dummy encode, do not do the tokenization
             vp8cx_encode_inter_macroblock(cpi, x, tp,
                                          recon_yoffset, recon_uvoffset, 0);
+
+            seg_id = xd->mode_info_context->mbmi.segment_id;
+            if (cpi->mb.e_mbd.segmentation_enabled && seg_id == 0)
+            {
+                cpi->seg0_idx++;
+            }
+            if (!xd->segmentation_enabled ||
+                !segfeature_active( xd, seg_id, SEG_LVL_REF_FRAME ) ||
+                check_segref(xd, seg_id, INTRA_FRAME)  +
+                check_segref(xd, seg_id, LAST_FRAME)   +
+                check_segref(xd, seg_id, GOLDEN_FRAME) +
+                check_segref(xd, seg_id, ALTREF_FRAME) > 1)
+            {
+                // Get the prediction context and status
+                int pred_flag = get_pred_flag( xd, PRED_REF );
+                int pred_context = get_pred_context( cm, xd, PRED_REF );
+
+                // Count prediction success
+                cpi->ref_pred_count[pred_context][pred_flag]++;
+            }
         }
 
         // Keep a copy of the updated left context
@@ -997,6 +1026,8 @@
     x->mb_activity_ptr = cpi->mb_activity_map;
 
     x->act_zbin_adj = 0;
+    cpi->seg0_idx = 0;
+    vpx_memset(cpi->ref_pred_count, 0, sizeof(cpi->ref_pred_count));
 
     x->partition_info = x->pi;
 
@@ -1288,11 +1319,13 @@
         else
             frame_type = 2;
 
-        if (cpi->rd_prediction_type_threshes[frame_type][1] >
-                cpi->rd_prediction_type_threshes[frame_type][0] &&
-            cpi->rd_prediction_type_threshes[frame_type][1] >
-                cpi->rd_prediction_type_threshes[frame_type][2] &&
-            check_dual_ref_flags(cpi))
+        if (frame_type == 3)
+            pred_type = SINGLE_PREDICTION_ONLY;
+        else if (cpi->rd_prediction_type_threshes[frame_type][1] >
+                    cpi->rd_prediction_type_threshes[frame_type][0] &&
+                 cpi->rd_prediction_type_threshes[frame_type][1] >
+                    cpi->rd_prediction_type_threshes[frame_type][2] &&
+                 check_dual_ref_flags(cpi))
             pred_type = COMP_PREDICTION_ONLY;
         else if (cpi->rd_prediction_type_threshes[frame_type][0] >
                     cpi->rd_prediction_type_threshes[frame_type][1] &&
diff --git a/vp8/encoder/encodemb.c b/vp8/encoder/encodemb.c
index ae8ab88..81c69d9 100644
--- a/vp8/encoder/encodemb.c
+++ b/vp8/encoder/encodemb.c
@@ -239,7 +239,9 @@
 {
     int i;
 
+#if !CONFIG_INT_8X8FDCT
     vp8_clear_system_state();
+#endif
 
     for (i = 16; i < 24; i += 4)
     {
@@ -252,9 +254,9 @@
 void vp8_transform_intra_mby_8x8(MACROBLOCK *x)//changed
 {
     int i;
-
+#if !CONFIG_INT_8X8FDCT
     vp8_clear_system_state();
-
+#endif
     for (i = 0; i < 9; i += 8)
     {
         x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
@@ -279,9 +281,9 @@
 void vp8_transform_mb_8x8(MACROBLOCK *x)
 {
     int i;
-
+#if !CONFIG_INT_8X8FDCT
     vp8_clear_system_state();
-
+#endif
     for (i = 0; i < 9; i += 8)
     {
         x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
@@ -312,9 +314,9 @@
 void vp8_transform_mby_8x8(MACROBLOCK *x)
 {
     int i;
-
+#if !CONFIG_INT_8X8FDCT
     vp8_clear_system_state();
-
+#endif
     for (i = 0; i < 9; i += 8)
     {
         x->vp8_short_fdct8x8(&x->block[i].src_diff[0],
diff --git a/vp8/encoder/mbgraph.c b/vp8/encoder/mbgraph.c
index ebdb9b5..d2d3b6b 100644
--- a/vp8/encoder/mbgraph.c
+++ b/vp8/encoder/mbgraph.c
@@ -502,6 +502,7 @@
         else
             cpi->static_mb_pct = 0;
 
+        cpi->seg0_cnt = ncnt[0];
         vp8_enable_segmentation((VP8_PTR) cpi);
     }
     else
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index 1a174c1..a66e71a 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -140,7 +140,7 @@
 
 
 #ifdef ENTROPY_STATS
-extern int intra_mode_stats[10][10][10];
+extern int intra_mode_stats[VP8_BINTRAMODES][VP8_BINTRAMODES][VP8_BINTRAMODES];
 #endif
 
 #ifdef SPEEDSTATS
@@ -156,13 +156,13 @@
 #endif
 #ifdef MODE_STATS
 extern INT64 Sectionbits[500];
-extern int y_modes[VP8_YMODES]  ;
-extern int i8x8_modes[VP8_I8X8_MODES];
-extern int uv_modes[VP8_UV_MODES] ;
-extern int uv_modes_y[VP8_YMODES][VP8_UV_MODES];
-extern int b_modes[B_MODE_COUNT];
-extern int inter_y_modes[MB_MODE_COUNT] ;
-extern int inter_uv_modes[VP8_UV_MODES] ;
+extern unsigned int y_modes[VP8_YMODES]  ;
+extern unsigned int i8x8_modes[VP8_I8X8_MODES];
+extern unsigned int uv_modes[VP8_UV_MODES] ;
+extern unsigned int uv_modes_y[VP8_YMODES][VP8_UV_MODES];
+extern unsigned int b_modes[B_MODE_COUNT];
+extern unsigned int inter_y_modes[MB_MODE_COUNT] ;
+extern unsigned int inter_uv_modes[VP8_UV_MODES] ;
 extern unsigned int inter_b_modes[B_MODE_COUNT];
 #endif
 
@@ -760,6 +760,14 @@
 
         sf->thresh_mult[THR_V_PRED   ] = 1000;
         sf->thresh_mult[THR_H_PRED   ] = 1000;
+#if CONFIG_NEWINTRAMODES
+        sf->thresh_mult[THR_D45_PRED ] = 1000;
+        sf->thresh_mult[THR_D135_PRED] = 1000;
+        sf->thresh_mult[THR_D117_PRED] = 1000;
+        sf->thresh_mult[THR_D153_PRED] = 1000;
+        sf->thresh_mult[THR_D27_PRED ] = 1000;
+        sf->thresh_mult[THR_D63_PRED ] = 1000;
+#endif
         sf->thresh_mult[THR_B_PRED   ] = 2000;
         sf->thresh_mult[THR_I8X8_PRED] = 2000;
         sf->thresh_mult[THR_TM       ] = 1000;
@@ -803,6 +811,14 @@
         sf->thresh_mult[THR_NEARMV   ] = 0;
         sf->thresh_mult[THR_V_PRED   ] = 1000;
         sf->thresh_mult[THR_H_PRED   ] = 1000;
+#if CONFIG_NEWINTRAMODES
+        sf->thresh_mult[THR_D45_PRED ] = 1000;
+        sf->thresh_mult[THR_D135_PRED] = 1000;
+        sf->thresh_mult[THR_D117_PRED] = 1000;
+        sf->thresh_mult[THR_D153_PRED] = 1000;
+        sf->thresh_mult[THR_D27_PRED ] = 1000;
+        sf->thresh_mult[THR_D63_PRED ] = 1000;
+#endif
         sf->thresh_mult[THR_B_PRED   ] = 2500;
         sf->thresh_mult[THR_I8X8_PRED] = 2500;
         sf->thresh_mult[THR_TM       ] = 1000;
@@ -881,6 +897,14 @@
             sf->thresh_mult[THR_TM       ] = 1500;
             sf->thresh_mult[THR_V_PRED   ] = 1500;
             sf->thresh_mult[THR_H_PRED   ] = 1500;
+#if CONFIG_NEWINTRAMODES
+            sf->thresh_mult[THR_D45_PRED ] = 1500;
+            sf->thresh_mult[THR_D135_PRED] = 1500;
+            sf->thresh_mult[THR_D117_PRED] = 1500;
+            sf->thresh_mult[THR_D153_PRED] = 1500;
+            sf->thresh_mult[THR_D27_PRED ] = 1500;
+            sf->thresh_mult[THR_D63_PRED ] = 1500;
+#endif
             sf->thresh_mult[THR_B_PRED   ] = 5000;
             sf->thresh_mult[THR_I8X8_PRED] = 5000;
 
@@ -939,6 +963,14 @@
             sf->thresh_mult[THR_TM       ] = 2000;
             sf->thresh_mult[THR_V_PRED   ] = 2000;
             sf->thresh_mult[THR_H_PRED   ] = 2000;
+#if CONFIG_NEWINTRAMODES
+            sf->thresh_mult[THR_D45_PRED ] = 2000;
+            sf->thresh_mult[THR_D135_PRED] = 2000;
+            sf->thresh_mult[THR_D117_PRED] = 2000;
+            sf->thresh_mult[THR_D153_PRED] = 2000;
+            sf->thresh_mult[THR_D27_PRED ] = 2000;
+            sf->thresh_mult[THR_D63_PRED ] = 2000;
+#endif
             sf->thresh_mult[THR_B_PRED   ] = 7500;
             sf->thresh_mult[THR_I8X8_PRED] = 7500;
 
@@ -1719,6 +1751,16 @@
 #ifdef ENTROPY_STATS
     init_context_counters();
 #endif
+#ifdef MODE_STATS
+    vp8_zero(y_modes);
+    vp8_zero(i8x8_modes);
+    vp8_zero(uv_modes);
+    vp8_zero(uv_modes_y);
+    vp8_zero(b_modes);
+    vp8_zero(inter_y_modes);
+    vp8_zero(inter_uv_modes);
+    vp8_zero(inter_b_modes);
+#endif
 
     /*Initialize the feed-forward activity masking.*/
     cpi->activity_avg = 90<<12;
@@ -2006,32 +2048,49 @@
             sprintf(modes_stats_file, "modes_q%03d.stt",cpi->common.base_qindex);
             f = fopen(modes_stats_file, "w");
             fprintf(f, "intra_mode in Intra Frames:\n");
-            fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4], y_modes[5]);
-            fprintf(f, "I8:%8d, %8d, %8d, %8d\n", i8x8_modes[0], i8x8_modes[1], i8x8_modes[2], i8x8_modes[3]);
-            fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
-            fprintf(f, "KeyFrame Y-UV:\n");
             {
                 int i;
-                for(i=0;i<VP8_YMODES;i++)
-                {
-                    fprintf(f, "%2d:%8d, %8d, %8d, %8d\n",i,uv_modes_y[i][0],
-                        uv_modes_y[i][1], uv_modes_y[i][2], uv_modes_y[i][3]);
-                }
+                fprintf(f, "Y: ");
+                for (i=0;i<VP8_YMODES;i++) fprintf(f, " %8d,", y_modes[i]);
+                fprintf(f, "\n");
             }
-            fprintf(f, "Inter Y-UV:\n");
             {
                 int i;
+                fprintf(f, "I8: ");
+                for (i=0;i<VP8_I8X8_MODES;i++) fprintf(f, " %8d,", i8x8_modes[i]);
+                fprintf(f, "\n");
+            }
+            {
+                int i;
+                fprintf(f, "UV: ");
+                for (i=0;i<VP8_UV_MODES;i++) fprintf(f, " %8d,", uv_modes[i]);
+                fprintf(f, "\n");
+            }
+            {
+                int i, j;
+                fprintf(f, "KeyFrame Y-UV:\n");
                 for(i=0;i<VP8_YMODES;i++)
                 {
-                    fprintf(f, "%2d:%8d, %8d, %8d, %8d\n",i,cpi->y_uv_mode_count[i][0],
-                        cpi->y_uv_mode_count[i][1], cpi->y_uv_mode_count[i][2], cpi->y_uv_mode_count[i][3]);
+                    fprintf(f, "%2d:", i);
+                    for (j=0; j<VP8_UV_MODES;j++) fprintf(f, "%8d, ",uv_modes_y[i][j]);
+                    fprintf(f, "\n");
                 }
             }
-            fprintf(f, "B: ");
+            {
+                int i, j;
+                fprintf(f, "Inter Y-UV:\n");
+                for(i=0;i<VP8_YMODES;i++)
+                {
+                    fprintf(f, "%2d:", i);
+                    for (j=0; j<VP8_UV_MODES;j++) fprintf(f, "%8d, ",cpi->y_uv_mode_count[i][j]);
+                    fprintf(f, "\n");
+                }
+            }
             {
                 int i;
 
-                for (i = 0; i < 10; i++)
+                fprintf(f, "B: ");
+                for (i = 0; i < VP8_BINTRAMODES; i++)
                     fprintf(f, "%8d, ", b_modes[i]);
 
                 fprintf(f, "\n");
@@ -2039,23 +2098,23 @@
             }
 
             fprintf(f, "Modes in Inter Frames:\n");
-            fprintf(f,
-                "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
-                    inter_y_modes[0], inter_y_modes[1], inter_y_modes[2],
-                    inter_y_modes[3], inter_y_modes[4], inter_y_modes[5],
-                    inter_y_modes[6], inter_y_modes[7], inter_y_modes[8],
-                    inter_y_modes[9], inter_y_modes[10]);
-            fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0],
-                    inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
-            fprintf(f, "B: ");
             {
                 int i;
-
-                for (i = 0; i < 15; i++)
-                    fprintf(f, "%8d, ", inter_b_modes[i]);
-
+                fprintf(f, "Y: ");
+                for (i=0;i<MB_MODE_COUNT;i++) fprintf(f, " %8d,", inter_y_modes[i]);
                 fprintf(f, "\n");
-
+            }
+            {
+                int i;
+                fprintf(f, "UV: ");
+                for (i=0;i<VP8_UV_MODES;i++) fprintf(f, " %8d,", inter_uv_modes[i]);
+                fprintf(f, "\n");
+            }
+            {
+                int i;
+                fprintf(f, "B: ");
+                for (i = 0; i < B_MODE_COUNT; i++) fprintf(f, "%8d, ", inter_b_modes[i]);
+                fprintf(f, "\n");
             }
             fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
             fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
@@ -2082,7 +2141,7 @@
 
                     fprintf(fmode, "        {");
 
-                    for (k = 0; k < 10; k++)
+                    for (k = 0; k < VP8_BINTRAMODES; k++)
                     {
                         if (!intra_mode_stats[i][j][k])
                             fprintf(fmode, " %5d, ", 1);
diff --git a/vp8/encoder/onyx_int.h b/vp8/encoder/onyx_int.h
index 93a445c..feefc24 100644
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -41,7 +41,12 @@
 #define AF_THRESH   25
 #define AF_THRESH2  100
 #define ARF_DECAY_THRESH 12
+
+#if CONFIG_NEWINTRAMODES
+#define MAX_MODES 42
+#else
 #define MAX_MODES 36
+#endif
 
 #define MIN_THRESHMULT  32
 #define MAX_THRESHMULT  512
@@ -173,38 +178,46 @@
 
     THR_V_PRED         = 10,
     THR_H_PRED         = 11,
-    THR_TM             = 12,
+#if CONFIG_NEWINTRAMODES
+    THR_D45_PRED,
+    THR_D135_PRED,
+    THR_D117_PRED,
+    THR_D153_PRED,
+    THR_D27_PRED,
+    THR_D63_PRED,
+#endif
+    THR_TM,
 
-    THR_NEWMV          = 13,
-    THR_NEWG           = 14,
-    THR_NEWA           = 15,
+    THR_NEWMV,
+    THR_NEWG,
+    THR_NEWA,
 
-    THR_SPLITMV        = 16,
-    THR_SPLITG         = 17,
-    THR_SPLITA         = 18,
+    THR_SPLITMV,
+    THR_SPLITG,
+    THR_SPLITA,
 
-    THR_B_PRED         = 19,
-    THR_I8X8_PRED      = 20,
+    THR_B_PRED,
+    THR_I8X8_PRED,
 
-    THR_COMP_ZEROLG    = 21,
-    THR_COMP_NEARESTLG = 22,
-    THR_COMP_NEARLG    = 23,
+    THR_COMP_ZEROLG,
+    THR_COMP_NEARESTLG,
+    THR_COMP_NEARLG,
 
-    THR_COMP_ZEROLA    = 24,
-    THR_COMP_NEARESTLA = 25,
-    THR_COMP_NEARLA    = 26,
+    THR_COMP_ZEROLA,
+    THR_COMP_NEARESTLA,
+    THR_COMP_NEARLA,
 
-    THR_COMP_ZEROGA    = 27,
-    THR_COMP_NEARESTGA = 28,
-    THR_COMP_NEARGA    = 29,
+    THR_COMP_ZEROGA,
+    THR_COMP_NEARESTGA,
+    THR_COMP_NEARGA,
 
-    THR_COMP_NEWLG     = 30,
-    THR_COMP_NEWLA     = 31,
-    THR_COMP_NEWGA     = 32,
+    THR_COMP_NEWLG,
+    THR_COMP_NEWLA,
+    THR_COMP_NEWGA,
 
-    THR_COMP_SPLITLG   = 33,
-    THR_COMP_SPLITLA   = 34,
-    THR_COMP_SPLITGA   = 35,
+    THR_COMP_SPLITLG,
+    THR_COMP_SPLITLA,
+    THR_COMP_SPLITGA,
 }
 THR_MODES;
 
@@ -472,6 +485,8 @@
     MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
     int mbgraph_n_frames;             // number of frames filled in the above
     int static_mb_pct;                // % forced skip mbs by segmentation
+    int seg0_progress, seg0_idx, seg0_cnt;
+    int ref_pred_count[3][2];
 
     int decimation_factor;
     int decimation_count;
diff --git a/vp8/encoder/ratectrl.c b/vp8/encoder/ratectrl.c
index f51fe6f..864dd65 100644
--- a/vp8/encoder/ratectrl.c
+++ b/vp8/encoder/ratectrl.c
@@ -33,13 +33,13 @@
 
 
 #ifdef MODE_STATS
-extern int y_modes[VP8_YMODES];
-extern int uv_modes[VP8_UV_MODES];
-extern int b_modes[B_MODE_COUNT];
+extern unsigned int y_modes[VP8_YMODES];
+extern unsigned int uv_modes[VP8_UV_MODES];
+extern unsigned int b_modes[B_MODE_COUNT];
 
-extern int inter_y_modes[MB_MODE_COUNT];
-extern int inter_uv_modes[VP8_UV_MODES];
-extern int inter_b_modes[B_MODE_COUNT];
+extern unsigned int inter_y_modes[MB_MODE_COUNT];
+extern unsigned int inter_uv_modes[VP8_UV_MODES];
+extern unsigned int inter_b_modes[B_MODE_COUNT];
 #endif
 
 // Bits Per MB at different Q (Multiplied by 512)
diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c
index cceeb76..0caa4c7 100644
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -100,6 +100,14 @@
 
     V_PRED,
     H_PRED,
+#if CONFIG_NEWINTRAMODES
+    D45_PRED,
+    D135_PRED,
+    D117_PRED,
+    D153_PRED,
+    D27_PRED,
+    D63_PRED,
+#endif
     TM_PRED,
 
     NEWMV,
@@ -154,6 +162,14 @@
 
     INTRA_FRAME,
     INTRA_FRAME,
+#if CONFIG_NEWINTRAMODES
+    INTRA_FRAME,
+    INTRA_FRAME,
+    INTRA_FRAME,
+    INTRA_FRAME,
+    INTRA_FRAME,
+    INTRA_FRAME,
+#endif
     INTRA_FRAME,
 
     LAST_FRAME,
@@ -191,9 +207,13 @@
 
 const MV_REFERENCE_FRAME vp8_second_ref_frame_order[MAX_MODES] =
 {
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-    0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+#if CONFIG_NEWINTRAMODES
+    0, 0,
+    0, 0,
+    0, 0,
+#endif
+    0, 0, 0, 0, 0, 0, 0, 0, 0,
 
     /* compound prediction modes */
     GOLDEN_FRAME,
@@ -898,7 +918,6 @@
         // Do we need to do this for mode2 also?
         if (mode==B_LD_PRED || mode==B_VL_PRED)
             continue;
-
         rate = bmode_costs[mode];
 
 #if CONFIG_COMP_INTRA_PRED
@@ -2562,7 +2581,66 @@
     }
 }
 
-void vp8_estimate_ref_frame_costs(VP8_COMP *cpi, unsigned int * ref_costs )
+extern void calc_ref_probs( int * count, vp8_prob * probs );
+static void estimate_curframe_refprobs(VP8_COMP *cpi, vp8_prob mod_refprobs[3], int pred_ref)
+{
+    int norm_cnt[MAX_REF_FRAMES];
+    const int *const rfct = cpi->count_mb_ref_frame_usage;
+    int intra_count = rfct[INTRA_FRAME];
+    int last_count  = rfct[LAST_FRAME];
+    int gf_count    = rfct[GOLDEN_FRAME];
+    int arf_count   = rfct[ALTREF_FRAME];
+
+    // Work out modified reference frame probabilities to use where prediction
+    // of the reference frame fails
+    if (pred_ref == INTRA_FRAME)
+    {
+        norm_cnt[0] = 0;
+        norm_cnt[1] = last_count;
+        norm_cnt[2] = gf_count;
+        norm_cnt[3] = arf_count;
+        calc_ref_probs( norm_cnt, mod_refprobs );
+        mod_refprobs[0] = 0;    // This branch implicit
+    }
+    else if (pred_ref == LAST_FRAME)
+    {
+        norm_cnt[0] = intra_count;
+        norm_cnt[1] = 0;
+        norm_cnt[2] = gf_count;
+        norm_cnt[3] = arf_count;
+        calc_ref_probs( norm_cnt, mod_refprobs);
+        mod_refprobs[1] = 0;    // This branch implicit
+    }
+    else if (pred_ref == GOLDEN_FRAME)
+    {
+        norm_cnt[0] = intra_count;
+        norm_cnt[1] = last_count;
+        norm_cnt[2] = 0;
+        norm_cnt[3] = arf_count;
+        calc_ref_probs( norm_cnt, mod_refprobs );
+        mod_refprobs[2] = 0;  // This branch implicit
+    }
+    else
+    {
+        norm_cnt[0] = intra_count;
+        norm_cnt[1] = last_count;
+        norm_cnt[2] = gf_count;
+        norm_cnt[3] = 0;
+        calc_ref_probs( norm_cnt, mod_refprobs );
+        mod_refprobs[2] = 0;  // This branch implicit
+    }
+}
+
+static __inline unsigned weighted_cost(vp8_prob *tab0, vp8_prob *tab1, int idx, int val, int weight)
+{
+    unsigned cost0 = tab0[idx] ? vp8_cost_bit(tab0[idx], val) : 0;
+    unsigned cost1 = tab1[idx] ? vp8_cost_bit(tab1[idx], val) : 0;
+    // weight is 16-bit fixed point, so this basically calculates:
+    // 0.5 + weight * cost1 + (1.0 - weight) * cost0
+    return (0x8000 + weight * cost1 + (0x10000 - weight) * cost0) >> 16;
+}
+
+static void vp8_estimate_ref_frame_costs(VP8_COMP *cpi, int segment_id, unsigned int * ref_costs )
 {
     VP8_COMMON *cm = &cpi->common;
     MACROBLOCKD *xd = &cpi->mb.e_mbd;
@@ -2571,47 +2649,87 @@
     unsigned int cost;
     int pred_ref ;
     int pred_flag;
+    int pred_ctx ;
     int i;
+    int tot_count;
 
-    vp8_prob pred_prob;
+    vp8_prob pred_prob, new_pred_prob;
+    int seg_ref_active;
+    int seg_ref_count = 0;
+    seg_ref_active = segfeature_active( xd,
+                                       segment_id,
+                                       SEG_LVL_REF_FRAME );
+
+    if ( seg_ref_active )
+    {
+        seg_ref_count = check_segref( xd, segment_id, INTRA_FRAME )  +
+                        check_segref( xd, segment_id, LAST_FRAME )   +
+                        check_segref( xd, segment_id, GOLDEN_FRAME ) +
+                        check_segref( xd, segment_id, ALTREF_FRAME );
+    }
 
     // Get the predicted reference for this mb
     pred_ref = get_pred_ref( cm, xd );
 
-    // Get the context probability for the prediction flag
+    // Get the context probability for the prediction flag (based on last frame)
     pred_prob = get_pred_prob( cm, xd, PRED_REF );
 
+    // Predict probability for current frame based on stats so far
+    pred_ctx = get_pred_context(cm, xd, PRED_REF);
+    tot_count = cpi->ref_pred_count[pred_ctx][0] + cpi->ref_pred_count[pred_ctx][1];
+    if ( tot_count )
+    {
+        new_pred_prob =
+            ( cpi->ref_pred_count[pred_ctx][0] * 255 + (tot_count >> 1)) / tot_count;
+        new_pred_prob += !new_pred_prob;
+    }
+    else
+        new_pred_prob = 128;
+
     // Get the set of probabilities to use if prediction fails
     mod_refprobs = cm->mod_refprobs[pred_ref];
 
     // For each possible selected reference frame work out a cost.
-    // TODO: correct handling of costs if segment indicates only a subset of
-    // reference frames are allowed... though mostly this should come out
-    // in the wash.
     for ( i = 0; i < MAX_REF_FRAMES; i++ )
     {
-        pred_flag = (i == pred_ref);
-
-        // Get the prediction for the current mb
-        cost = vp8_cost_bit( pred_prob, pred_flag );
-
-        // for incorrectly predicted cases
-        if ( ! pred_flag )
+        if (seg_ref_active && seg_ref_count == 1)
         {
-            if ( mod_refprobs[0] )
-                cost += vp8_cost_bit( mod_refprobs[0], (i != INTRA_FRAME) );
+            cost = 0;
+        }
+        else
+        {
+            pred_flag = (i == pred_ref);
 
-            // Inter coded
-            if (i != INTRA_FRAME)
+            // Get the prediction for the current mb
+            cost = weighted_cost(&pred_prob, &new_pred_prob, 0,
+                                 pred_flag, cpi->seg0_progress);
+            if (cost > 1024) cost = 768; // i.e. account for 4 bits max.
+
+            // for incorrectly predicted cases
+            if ( ! pred_flag )
             {
-                if ( mod_refprobs[1] )
-                    cost += vp8_cost_bit( mod_refprobs[1], (i != LAST_FRAME) );
+                vp8_prob curframe_mod_refprobs[3];
 
-                if (i != LAST_FRAME)
+                if (cpi->seg0_progress)
                 {
-                    if ( mod_refprobs[2] )
-                        cost += vp8_cost_bit( mod_refprobs[2],
-                                             (i != GOLDEN_FRAME));
+                    estimate_curframe_refprobs(cpi, curframe_mod_refprobs, pred_ref);
+                }
+                else
+                {
+                    vpx_memset(curframe_mod_refprobs, 0, sizeof(curframe_mod_refprobs));
+                }
+
+                cost += weighted_cost(mod_refprobs, curframe_mod_refprobs, 0,
+                                      (i != INTRA_FRAME), cpi->seg0_progress);
+                if (i != INTRA_FRAME)
+                {
+                    cost += weighted_cost(mod_refprobs, curframe_mod_refprobs, 1,
+                                          (i != LAST_FRAME), cpi->seg0_progress);
+                    if (i != LAST_FRAME)
+                    {
+                        cost += weighted_cost(mod_refprobs, curframe_mod_refprobs, 2,
+                                              (i != GOLDEN_FRAME), cpi->seg0_progress);
+                    }
                 }
             }
         }
@@ -2800,7 +2918,7 @@
 
     // Get estimates of reference frame costs for each reference frame
     // that depend on the current prediction etc.
-    vp8_estimate_ref_frame_costs( cpi, ref_costs );
+    vp8_estimate_ref_frame_costs( cpi, segment_id, ref_costs );
 
     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
     {
@@ -3017,6 +3135,14 @@
                 vp8_cost_bit( get_pred_prob( cm, xd, PRED_COMP ), 0 );
         }
         break;
+#if CONFIG_NEWINTRAMODES
+        case D45_PRED:
+        case D135_PRED:
+        case D117_PRED:
+        case D153_PRED:
+        case D27_PRED:
+        case D63_PRED:
+#endif
         case DC_PRED:
         case V_PRED:
         case H_PRED:
diff --git a/vp8/vp8_common.mk b/vp8/vp8_common.mk
index de645eb..c4bd283 100644
--- a/vp8/vp8_common.mk
+++ b/vp8/vp8_common.mk
@@ -32,7 +32,6 @@
 VP8_COMMON_SRCS-yes += common/blockd.h
 VP8_COMMON_SRCS-yes += common/common.h
 VP8_COMMON_SRCS-yes += common/common_types.h
-VP8_COMMON_SRCS-yes += common/defaultcoefcounts.h
 VP8_COMMON_SRCS-yes += common/entropy.h
 VP8_COMMON_SRCS-yes += common/entropymode.h
 VP8_COMMON_SRCS-yes += common/entropymv.h