Merge "Use restore_dst_buf in handle_inter_mode"
diff --git a/examples.mk b/examples.mk
index 3324fd9..36d20df 100644
--- a/examples.mk
+++ b/examples.mk
@@ -25,6 +25,7 @@
 vpxdec.SRCS                 += args.c args.h
 vpxdec.SRCS                 += ivfdec.c ivfdec.h
 vpxdec.SRCS                 += tools_common.c tools_common.h
+vpxdec.SRCS                 += webmdec.c webmdec.h
 vpxdec.SRCS                 += nestegg/halloc/halloc.h
 vpxdec.SRCS                 += nestegg/halloc/src/align.h
 vpxdec.SRCS                 += nestegg/halloc/src/halloc.c
diff --git a/tools_common.h b/tools_common.h
index 353f90a..7500523 100644
--- a/tools_common.h
+++ b/tools_common.h
@@ -92,11 +92,11 @@
   off_t length;
   struct FileTypeDetectionBuffer detect;
   enum VideoFileType file_type;
-  unsigned int width;
-  unsigned int height;
+  uint32_t width;
+  uint32_t height;
   int use_i420;
   int only_i420;
-  unsigned int fourcc;
+  uint32_t fourcc;
   struct VpxRational framerate;
 #if CONFIG_ENCODERS
   y4m_input y4m;
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index 4b60cfd..1c773b7 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3808,11 +3808,22 @@
 
     /* Setup background Q adjustment for error resilient mode.
      * For multi-layer encodes only enable this for the base layer.
-     */
+     * Reduce loop filter to reduce "dot" artifacts that can occur for repeated
+     * loop filtering on ZEROMV_LASTREF blocks.
+     * For now reducing it to -32, only for resolutions above CIF and
+     * #temporal_layers < 3 (artifact is hard to see at low spatial resolution
+     * and/or high #temporal layers).
+    */
     if (cpi->cyclic_refresh_mode_enabled)
     {
+      int delta_loop_filter = 0;
+      if (cm->Width > 352 && cm->Height > 288 &&
+          cpi->oxcf.number_of_layers < 3)
+      {
+        delta_loop_filter = -32;
+      }
       if (cpi->current_layer==0)
-        cyclic_background_refresh(cpi, Q, 0);
+        cyclic_background_refresh(cpi, Q, delta_loop_filter);
       else
         disable_segmentation(cpi);
     }
diff --git a/vp9/common/vp9_common_data.c b/vp9/common/vp9_common_data.c
index 466074b..388f38d 100644
--- a/vp9/common/vp9_common_data.c
+++ b/vp9/common/vp9_common_data.c
@@ -123,8 +123,6 @@
   TX_32X32,  // TX_MODE_SELECT
 };
 
-
-
 const BLOCK_SIZE ss_size_lookup[BLOCK_SIZES][2][2] = {
 //  ss_x == 0    ss_x == 0        ss_x == 1      ss_x == 1
 //  ss_y == 0    ss_y == 1        ss_y == 0      ss_y == 1
@@ -144,23 +142,23 @@
 };
 
 // Generates 4 bit field in which each bit set to 1 represents
-// a blocksize partition  1111 means we split 8x8, 16x16, 32x32
-// and 64x64.  0001 means we just split the 64x64...
+// a blocksize partition  1111 means we split 64x64, 32x32, 16x16
+// and 8x8.  1000 means we just split the 64x64 to 32x32
 const struct {
   PARTITION_CONTEXT above;
   PARTITION_CONTEXT left;
 } partition_context_lookup[BLOCK_SIZES]= {
-  {15, 15},  // 4X4
-  {15,  7},  // 4X8
-  {7,  15},  // 8X4
-  {7,   7},  // 8X8
-  {7,   3},  // 8X16
-  {3,   7},  // 16X8
-  {3,   3},  // 16X16
-  {3,   1},  // 16X32
-  {1,   3},  // 32X16
-  {1,   1},  // 32X32
-  {1,   0},  // 32X64
-  {0,   1},  // 64X32
-  {0,   0},  // 64X64
+  {15, 15},  // 4X4   - {0b1111, 0b1111}
+  {15, 14},  // 4X8   - {0b1111, 0b1110}
+  {14, 15},  // 8X4   - {0b1110, 0b1111}
+  {14, 14},  // 8X8   - {0b1110, 0b1110}
+  {14, 12},  // 8X16  - {0b1110, 0b1100}
+  {12, 14},  // 16X8  - {0b1100, 0b1110}
+  {12, 12},  // 16X16 - {0b1100, 0b1100}
+  {12, 8 },  // 16X32 - {0b1100, 0b1000}
+  {8,  12},  // 32X16 - {0b1000, 0b1100}
+  {8,  8 },  // 32X32 - {0b1000, 0b1000}
+  {8,  0 },  // 32X64 - {0b1000, 0b0000}
+  {0,  8 },  // 64X32 - {0b0000, 0b1000}
+  {0,  0 },  // 64X64 - {0b0000, 0b0000}
 };
diff --git a/vp9/common/vp9_entropy.c b/vp9/common/vp9_entropy.c
index 1cf9dbc..82aa77e 100644
--- a/vp9/common/vp9_entropy.c
+++ b/vp9/common/vp9_entropy.c
@@ -15,7 +15,6 @@
 #include "vpx_mem/vpx_mem.h"
 #include "vpx/vpx_integer.h"
 
-#define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
 
 DECLARE_ALIGNED(16, const uint8_t, vp9_norm[256]) = {
   0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
@@ -158,6 +157,8 @@
 // the probabilities for the rest of the nodes.
 
 // beta = 8
+
+
 static const vp9_prob pareto8_probs[COEFPROB_MODELS][MODEL_NODES] = {
   {  3,  86, 128,   6,  86,  23,  88,  29},
   {  9,  86, 129,  17,  88,  61,  94,  76},
@@ -289,6 +290,268 @@
   {255, 246, 247, 255, 239, 255, 253, 255}
 };
 
+// This table is an expansion of the table : modelcoefprobs_pareto8
+// to all 255 probabilities using the code as follows to do the expansion:
+// tree_probs[i] = (model[l][i - UNCONSTRAINED_NODES] +
+//                  model[l + 1][i - UNCONSTRAINED_NODES]) >> 1;
+const vp9_prob vp9_pareto8_full[255][MODEL_NODES] = {
+    {  3, 86, 128,  6, 86, 23, 88, 29},
+    {  6, 86, 128, 11, 87, 42, 91, 52},
+    {  9, 86, 129, 17, 88, 61, 94, 76},
+    { 12, 86, 129, 22, 88, 77, 97, 93},
+    { 15, 87, 129, 28, 89, 93, 100, 110},
+    { 17, 87, 129, 33, 90, 105, 103, 123},
+    { 20, 88, 130, 38, 91, 118, 106, 136},
+    { 23, 88, 130, 43, 91, 128, 108, 146},
+    { 26, 89, 131, 48, 92, 139, 111, 156},
+    { 28, 89, 131, 53, 93, 147, 114, 163},
+    { 31, 90, 131, 58, 94, 156, 117, 171},
+    { 34, 90, 131, 62, 94, 163, 119, 177},
+    { 37, 90, 132, 66, 95, 171, 122, 184},
+    { 39, 90, 132, 70, 96, 177, 124, 189},
+    { 42, 91, 132, 75, 97, 183, 127, 194},
+    { 44, 91, 132, 79, 97, 188, 129, 198},
+    { 47, 92, 133, 83, 98, 193, 132, 202},
+    { 49, 92, 133, 86, 99, 197, 134, 205},
+    { 52, 93, 133, 90, 100, 201, 137, 208},
+    { 54, 93, 133, 94, 100, 204, 139, 211},
+    { 57, 94, 134, 98, 101, 208, 142, 214},
+    { 59, 94, 134, 101, 102, 211, 144, 216},
+    { 62, 94, 135, 105, 103, 214, 146, 218},
+    { 64, 94, 135, 108, 103, 216, 148, 220},
+    { 66, 95, 135, 111, 104, 219, 151, 222},
+    { 68, 95, 135, 114, 105, 221, 153, 223},
+    { 71, 96, 136, 117, 106, 224, 155, 225},
+    { 73, 96, 136, 120, 106, 225, 157, 226},
+    { 76, 97, 136, 123, 107, 227, 159, 228},
+    { 78, 97, 136, 126, 108, 229, 160, 229},
+    { 80, 98, 137, 129, 109, 231, 162, 231},
+    { 82, 98, 137, 131, 109, 232, 164, 232},
+    { 84, 98, 138, 134, 110, 234, 166, 233},
+    { 86, 98, 138, 137, 111, 235, 168, 234},
+    { 89, 99, 138, 140, 112, 236, 170, 235},
+    { 91, 99, 138, 142, 112, 237, 171, 235},
+    { 93, 100, 139, 145, 113, 238, 173, 236},
+    { 95, 100, 139, 147, 114, 239, 174, 237},
+    { 97, 101, 140, 149, 115, 240, 176, 238},
+    { 99, 101, 140, 151, 115, 241, 177, 238},
+    {101, 102, 140, 154, 116, 242, 179, 239},
+    {103, 102, 140, 156, 117, 242, 180, 239},
+    {105, 103, 141, 158, 118, 243, 182, 240},
+    {107, 103, 141, 160, 118, 243, 183, 240},
+    {109, 104, 141, 162, 119, 244, 185, 241},
+    {111, 104, 141, 164, 119, 244, 186, 241},
+    {113, 104, 142, 166, 120, 245, 187, 242},
+    {114, 104, 142, 168, 121, 245, 188, 242},
+    {116, 105, 143, 170, 122, 246, 190, 243},
+    {118, 105, 143, 171, 122, 246, 191, 243},
+    {120, 106, 143, 173, 123, 247, 192, 244},
+    {121, 106, 143, 175, 124, 247, 193, 244},
+    {123, 107, 144, 177, 125, 248, 195, 244},
+    {125, 107, 144, 178, 125, 248, 196, 244},
+    {127, 108, 145, 180, 126, 249, 197, 245},
+    {128, 108, 145, 181, 127, 249, 198, 245},
+    {130, 109, 145, 183, 128, 249, 199, 245},
+    {132, 109, 145, 184, 128, 249, 200, 245},
+    {134, 110, 146, 186, 129, 250, 201, 246},
+    {135, 110, 146, 187, 130, 250, 202, 246},
+    {137, 111, 147, 189, 131, 251, 203, 246},
+    {138, 111, 147, 190, 131, 251, 204, 246},
+    {140, 112, 147, 192, 132, 251, 205, 247},
+    {141, 112, 147, 193, 132, 251, 206, 247},
+    {143, 113, 148, 194, 133, 251, 207, 247},
+    {144, 113, 148, 195, 134, 251, 207, 247},
+    {146, 114, 149, 197, 135, 252, 208, 248},
+    {147, 114, 149, 198, 135, 252, 209, 248},
+    {149, 115, 149, 199, 136, 252, 210, 248},
+    {150, 115, 149, 200, 137, 252, 210, 248},
+    {152, 115, 150, 201, 138, 252, 211, 248},
+    {153, 115, 150, 202, 138, 252, 212, 248},
+    {155, 116, 151, 204, 139, 253, 213, 249},
+    {156, 116, 151, 205, 139, 253, 213, 249},
+    {158, 117, 151, 206, 140, 253, 214, 249},
+    {159, 117, 151, 207, 141, 253, 215, 249},
+    {161, 118, 152, 208, 142, 253, 216, 249},
+    {162, 118, 152, 209, 142, 253, 216, 249},
+    {163, 119, 153, 210, 143, 253, 217, 249},
+    {164, 119, 153, 211, 143, 253, 217, 249},
+    {166, 120, 153, 212, 144, 254, 218, 250},
+    {167, 120, 153, 212, 145, 254, 219, 250},
+    {168, 121, 154, 213, 146, 254, 220, 250},
+    {169, 121, 154, 214, 146, 254, 220, 250},
+    {171, 122, 155, 215, 147, 254, 221, 250},
+    {172, 122, 155, 216, 147, 254, 221, 250},
+    {173, 123, 155, 217, 148, 254, 222, 250},
+    {174, 123, 155, 217, 149, 254, 222, 250},
+    {176, 124, 156, 218, 150, 254, 223, 250},
+    {177, 124, 156, 219, 150, 254, 223, 250},
+    {178, 125, 157, 220, 151, 254, 224, 251},
+    {179, 125, 157, 220, 151, 254, 224, 251},
+    {180, 126, 157, 221, 152, 254, 225, 251},
+    {181, 126, 157, 221, 152, 254, 225, 251},
+    {183, 127, 158, 222, 153, 254, 226, 251},
+    {184, 127, 158, 223, 154, 254, 226, 251},
+    {185, 128, 159, 224, 155, 255, 227, 251},
+    {186, 128, 159, 224, 155, 255, 227, 251},
+    {187, 129, 160, 225, 156, 255, 228, 251},
+    {188, 130, 160, 225, 156, 255, 228, 251},
+    {189, 131, 160, 226, 157, 255, 228, 251},
+    {190, 131, 160, 226, 158, 255, 228, 251},
+    {191, 132, 161, 227, 159, 255, 229, 251},
+    {192, 132, 161, 227, 159, 255, 229, 251},
+    {193, 133, 162, 228, 160, 255, 230, 252},
+    {194, 133, 162, 229, 160, 255, 230, 252},
+    {195, 134, 163, 230, 161, 255, 231, 252},
+    {196, 134, 163, 230, 161, 255, 231, 252},
+    {197, 135, 163, 231, 162, 255, 231, 252},
+    {198, 135, 163, 231, 162, 255, 231, 252},
+    {199, 136, 164, 232, 163, 255, 232, 252},
+    {200, 136, 164, 232, 164, 255, 232, 252},
+    {201, 137, 165, 233, 165, 255, 233, 252},
+    {201, 137, 165, 233, 165, 255, 233, 252},
+    {202, 138, 166, 233, 166, 255, 233, 252},
+    {203, 138, 166, 233, 166, 255, 233, 252},
+    {204, 139, 166, 234, 167, 255, 234, 252},
+    {205, 139, 166, 234, 167, 255, 234, 252},
+    {206, 140, 167, 235, 168, 255, 235, 252},
+    {206, 140, 167, 235, 168, 255, 235, 252},
+    {207, 141, 168, 236, 169, 255, 235, 252},
+    {208, 141, 168, 236, 170, 255, 235, 252},
+    {209, 142, 169, 237, 171, 255, 236, 252},
+    {209, 143, 169, 237, 171, 255, 236, 252},
+    {210, 144, 169, 237, 172, 255, 236, 252},
+    {211, 144, 169, 237, 172, 255, 236, 252},
+    {212, 145, 170, 238, 173, 255, 237, 252},
+    {213, 145, 170, 238, 173, 255, 237, 252},
+    {214, 146, 171, 239, 174, 255, 237, 253},
+    {214, 146, 171, 239, 174, 255, 237, 253},
+    {215, 147, 172, 240, 175, 255, 238, 253},
+    {215, 147, 172, 240, 175, 255, 238, 253},
+    {216, 148, 173, 240, 176, 255, 238, 253},
+    {217, 148, 173, 240, 176, 255, 238, 253},
+    {218, 149, 173, 241, 177, 255, 239, 253},
+    {218, 149, 173, 241, 178, 255, 239, 253},
+    {219, 150, 174, 241, 179, 255, 239, 253},
+    {219, 151, 174, 241, 179, 255, 239, 253},
+    {220, 152, 175, 242, 180, 255, 240, 253},
+    {221, 152, 175, 242, 180, 255, 240, 253},
+    {222, 153, 176, 242, 181, 255, 240, 253},
+    {222, 153, 176, 242, 181, 255, 240, 253},
+    {223, 154, 177, 243, 182, 255, 240, 253},
+    {223, 154, 177, 243, 182, 255, 240, 253},
+    {224, 155, 178, 244, 183, 255, 241, 253},
+    {224, 155, 178, 244, 183, 255, 241, 253},
+    {225, 156, 178, 244, 184, 255, 241, 253},
+    {225, 157, 178, 244, 184, 255, 241, 253},
+    {226, 158, 179, 244, 185, 255, 242, 253},
+    {227, 158, 179, 244, 185, 255, 242, 253},
+    {228, 159, 180, 245, 186, 255, 242, 253},
+    {228, 159, 180, 245, 186, 255, 242, 253},
+    {229, 160, 181, 245, 187, 255, 242, 253},
+    {229, 160, 181, 245, 187, 255, 242, 253},
+    {230, 161, 182, 246, 188, 255, 243, 253},
+    {230, 162, 182, 246, 188, 255, 243, 253},
+    {231, 163, 183, 246, 189, 255, 243, 253},
+    {231, 163, 183, 246, 189, 255, 243, 253},
+    {232, 164, 184, 247, 190, 255, 243, 253},
+    {232, 164, 184, 247, 190, 255, 243, 253},
+    {233, 165, 185, 247, 191, 255, 244, 253},
+    {233, 165, 185, 247, 191, 255, 244, 253},
+    {234, 166, 185, 247, 192, 255, 244, 253},
+    {234, 167, 185, 247, 192, 255, 244, 253},
+    {235, 168, 186, 248, 193, 255, 244, 253},
+    {235, 168, 186, 248, 193, 255, 244, 253},
+    {236, 169, 187, 248, 194, 255, 244, 253},
+    {236, 169, 187, 248, 194, 255, 244, 253},
+    {236, 170, 188, 248, 195, 255, 245, 253},
+    {236, 170, 188, 248, 195, 255, 245, 253},
+    {237, 171, 189, 249, 196, 255, 245, 254},
+    {237, 172, 189, 249, 196, 255, 245, 254},
+    {238, 173, 190, 249, 197, 255, 245, 254},
+    {238, 173, 190, 249, 197, 255, 245, 254},
+    {239, 174, 191, 249, 198, 255, 245, 254},
+    {239, 174, 191, 249, 198, 255, 245, 254},
+    {240, 175, 192, 249, 199, 255, 246, 254},
+    {240, 176, 192, 249, 199, 255, 246, 254},
+    {240, 177, 193, 250, 200, 255, 246, 254},
+    {240, 177, 193, 250, 200, 255, 246, 254},
+    {241, 178, 194, 250, 201, 255, 246, 254},
+    {241, 178, 194, 250, 201, 255, 246, 254},
+    {242, 179, 195, 250, 202, 255, 246, 254},
+    {242, 180, 195, 250, 202, 255, 246, 254},
+    {242, 181, 196, 250, 203, 255, 247, 254},
+    {242, 181, 196, 250, 203, 255, 247, 254},
+    {243, 182, 197, 251, 204, 255, 247, 254},
+    {243, 183, 197, 251, 204, 255, 247, 254},
+    {244, 184, 198, 251, 205, 255, 247, 254},
+    {244, 184, 198, 251, 205, 255, 247, 254},
+    {244, 185, 199, 251, 206, 255, 247, 254},
+    {244, 185, 199, 251, 206, 255, 247, 254},
+    {245, 186, 200, 251, 207, 255, 247, 254},
+    {245, 187, 200, 251, 207, 255, 247, 254},
+    {246, 188, 201, 252, 207, 255, 248, 254},
+    {246, 188, 201, 252, 207, 255, 248, 254},
+    {246, 189, 202, 252, 208, 255, 248, 254},
+    {246, 190, 202, 252, 208, 255, 248, 254},
+    {247, 191, 203, 252, 209, 255, 248, 254},
+    {247, 191, 203, 252, 209, 255, 248, 254},
+    {247, 192, 204, 252, 210, 255, 248, 254},
+    {247, 193, 204, 252, 210, 255, 248, 254},
+    {248, 194, 205, 252, 211, 255, 248, 254},
+    {248, 194, 205, 252, 211, 255, 248, 254},
+    {248, 195, 206, 252, 212, 255, 249, 254},
+    {248, 196, 206, 252, 212, 255, 249, 254},
+    {249, 197, 207, 253, 213, 255, 249, 254},
+    {249, 197, 207, 253, 213, 255, 249, 254},
+    {249, 198, 208, 253, 214, 255, 249, 254},
+    {249, 199, 209, 253, 214, 255, 249, 254},
+    {250, 200, 210, 253, 215, 255, 249, 254},
+    {250, 200, 210, 253, 215, 255, 249, 254},
+    {250, 201, 211, 253, 215, 255, 249, 254},
+    {250, 202, 211, 253, 215, 255, 249, 254},
+    {250, 203, 212, 253, 216, 255, 249, 254},
+    {250, 203, 212, 253, 216, 255, 249, 254},
+    {251, 204, 213, 253, 217, 255, 250, 254},
+    {251, 205, 213, 253, 217, 255, 250, 254},
+    {251, 206, 214, 254, 218, 255, 250, 254},
+    {251, 206, 215, 254, 218, 255, 250, 254},
+    {252, 207, 216, 254, 219, 255, 250, 254},
+    {252, 208, 216, 254, 219, 255, 250, 254},
+    {252, 209, 217, 254, 220, 255, 250, 254},
+    {252, 210, 217, 254, 220, 255, 250, 254},
+    {252, 211, 218, 254, 221, 255, 250, 254},
+    {252, 212, 218, 254, 221, 255, 250, 254},
+    {253, 213, 219, 254, 222, 255, 250, 254},
+    {253, 213, 220, 254, 222, 255, 250, 254},
+    {253, 214, 221, 254, 223, 255, 250, 254},
+    {253, 215, 221, 254, 223, 255, 250, 254},
+    {253, 216, 222, 254, 224, 255, 251, 254},
+    {253, 217, 223, 254, 224, 255, 251, 254},
+    {253, 218, 224, 254, 225, 255, 251, 254},
+    {253, 219, 224, 254, 225, 255, 251, 254},
+    {254, 220, 225, 254, 225, 255, 251, 254},
+    {254, 221, 226, 254, 225, 255, 251, 254},
+    {254, 222, 227, 255, 226, 255, 251, 254},
+    {254, 223, 227, 255, 226, 255, 251, 254},
+    {254, 224, 228, 255, 227, 255, 251, 254},
+    {254, 225, 229, 255, 227, 255, 251, 254},
+    {254, 226, 230, 255, 228, 255, 251, 254},
+    {254, 227, 230, 255, 229, 255, 251, 254},
+    {255, 228, 231, 255, 230, 255, 251, 254},
+    {255, 229, 232, 255, 230, 255, 251, 254},
+    {255, 230, 233, 255, 231, 255, 252, 254},
+    {255, 231, 234, 255, 231, 255, 252, 254},
+    {255, 232, 235, 255, 232, 255, 252, 254},
+    {255, 233, 236, 255, 232, 255, 252, 254},
+    {255, 235, 237, 255, 233, 255, 252, 254},
+    {255, 236, 238, 255, 234, 255, 252, 254},
+    {255, 238, 240, 255, 235, 255, 252, 255},
+    {255, 239, 241, 255, 235, 255, 252, 254},
+    {255, 241, 243, 255, 236, 255, 252, 254},
+    {255, 243, 245, 255, 237, 255, 252, 254},
+    {255, 246, 247, 255, 239, 255, 253, 255},
+};
+
 static void extend_to_full_distribution(vp9_prob *probs, vp9_prob p) {
   const int l = (p - 1) / 2;
   if (p & 1) {
diff --git a/vp9/common/vp9_entropy.h b/vp9/common/vp9_entropy.h
index e133d65..9ab918f 100644
--- a/vp9/common/vp9_entropy.h
+++ b/vp9/common/vp9_entropy.h
@@ -17,6 +17,7 @@
 #include "vp9/common/vp9_common.h"
 #include "vp9/common/vp9_scan.h"
 #include "vp9/common/vp9_treecoder.h"
+#include "vp9/common/vp9_entropymode.h"
 
 #define DIFF_UPDATE_PROB 252
 
@@ -141,6 +142,9 @@
 
 #define PIVOT_NODE                  2   // which node is pivot
 
+#define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
+extern const vp9_prob vp9_pareto8_full[255][MODEL_NODES];
+
 typedef vp9_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS]
                                       [PREV_COEF_CONTEXTS]
                                       [UNCONSTRAINED_NODES];
diff --git a/vp9/common/vp9_onyxc_int.h b/vp9/common/vp9_onyxc_int.h
index 943fcdc..fb959cb 100644
--- a/vp9/common/vp9_onyxc_int.h
+++ b/vp9/common/vp9_onyxc_int.h
@@ -303,43 +303,40 @@
 static INLINE void update_partition_context(
     PARTITION_CONTEXT *above_seg_context,
     PARTITION_CONTEXT left_seg_context[8],
-    int mi_row, int mi_col,
-    BLOCK_SIZE sb_type,
-    BLOCK_SIZE sb_size) {
-  PARTITION_CONTEXT *above_ctx = above_seg_context + mi_col;
-  PARTITION_CONTEXT *left_ctx = left_seg_context + (mi_row & MI_MASK);
+    int mi_row, int mi_col, BLOCK_SIZE subsize, BLOCK_SIZE bsize) {
+  PARTITION_CONTEXT *const above_ctx = above_seg_context + mi_col;
+  PARTITION_CONTEXT *const left_ctx = left_seg_context + (mi_row & MI_MASK);
 
-  const int bsl = b_width_log2(sb_size), bs = (1 << bsl) / 2;
+  // num_4x4_blocks_wide_lookup[bsize] / 2
+  const int bs = num_8x8_blocks_wide_lookup[bsize];
 
   // update the partition context at the end notes. set partition bits
   // of block sizes larger than the current one to be one, and partition
   // bits of smaller block sizes to be zero.
-  vpx_memset(above_ctx, partition_context_lookup[sb_type].above, bs);
-  vpx_memset(left_ctx, partition_context_lookup[sb_type].left, bs);
+  vpx_memset(above_ctx, partition_context_lookup[subsize].above, bs);
+  vpx_memset(left_ctx, partition_context_lookup[subsize].left, bs);
 }
 
 static INLINE int partition_plane_context(
     const PARTITION_CONTEXT *above_seg_context,
     const PARTITION_CONTEXT left_seg_context[8],
-    int mi_row, int mi_col,
-    BLOCK_SIZE sb_type) {
+    int mi_row, int mi_col, BLOCK_SIZE bsize) {
   const PARTITION_CONTEXT *above_ctx = above_seg_context + mi_col;
   const PARTITION_CONTEXT *left_ctx = left_seg_context + (mi_row & MI_MASK);
 
-  int bsl = mi_width_log2(sb_type), bs = 1 << bsl;
+  const int bsl = mi_width_log2(bsize);
+  const int bs = 1 << bsl;
   int above = 0, left = 0, i;
-  int boffset = mi_width_log2(BLOCK_64X64) - bsl;
 
-  assert(mi_width_log2(sb_type) == mi_height_log2(sb_type));
+  assert(mi_width_log2(bsize) == mi_height_log2(bsize));
   assert(bsl >= 0);
-  assert(boffset >= 0);
 
   for (i = 0; i < bs; i++) {
     above |= above_ctx[i];
     left |= left_ctx[i];
   }
-  above = (above & (1 << boffset)) > 0;
-  left  = (left & (1 << boffset)) > 0;
+  above = (above & bs) > 0;
+  left  = (left & bs) > 0;
 
   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
 }
diff --git a/vp9/common/vp9_reconinter.c b/vp9/common/vp9_reconinter.c
index 7cc66c8..3add81b 100644
--- a/vp9/common/vp9_reconinter.c
+++ b/vp9/common/vp9_reconinter.c
@@ -20,25 +20,6 @@
 #include "vp9/common/vp9_reconinter.h"
 #include "vp9/common/vp9_reconintra.h"
 
-void vp9_setup_interp_filters(MACROBLOCKD *xd,
-                              INTERPOLATION_TYPE mcomp_filter_type,
-                              VP9_COMMON *cm) {
-  if (xd->mi_8x8 && xd->mi_8x8[0]) {
-    MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
-
-    set_scale_factors(xd, mbmi->ref_frame[0] - LAST_FRAME,
-                          mbmi->ref_frame[1] - LAST_FRAME,
-                          cm->active_ref_scale);
-  } else {
-    set_scale_factors(xd, -1, -1, cm->active_ref_scale);
-  }
-
-  xd->subpix.filter_x = xd->subpix.filter_y =
-      vp9_get_filter_kernel(mcomp_filter_type == SWITCHABLE ?
-                               EIGHTTAP : mcomp_filter_type);
-
-  assert(((intptr_t)xd->subpix.filter_x & 0xff) == 0);
-}
 
 static void inter_predictor(const uint8_t *src, int src_stride,
                             uint8_t *dst, int dst_stride,
diff --git a/vp9/common/vp9_reconinter.h b/vp9/common/vp9_reconinter.h
index 2c8a6e4..b328754 100644
--- a/vp9/common/vp9_reconinter.h
+++ b/vp9/common/vp9_reconinter.h
@@ -24,10 +24,6 @@
 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
                                    BLOCK_SIZE bsize);
 
-void vp9_setup_interp_filters(MACROBLOCKD *xd,
-                              INTERPOLATION_TYPE filter,
-                              VP9_COMMON *cm);
-
 void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
                                uint8_t *dst, int dst_stride,
                                const MV *mv_q3,
diff --git a/vp9/common/vp9_scan.c b/vp9/common/vp9_scan.c
index f17da91..f62150f 100644
--- a/vp9/common/vp9_scan.c
+++ b/vp9/common/vp9_scan.c
@@ -266,6 +266,62 @@
 DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_16x16[256]);
 DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_32x32[1024]);
 
+const scan_order inter_scan_orders[TX_SIZES] = {
+  {vp9_default_scan_4x4,   vp9_default_scan_4x4_neighbors},    // NEWMV
+  {vp9_default_scan_8x8,   vp9_default_scan_8x8_neighbors},    // NEWMV
+  {vp9_default_scan_16x16, vp9_default_scan_16x16_neighbors},  // NEWMV
+  {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // NEWMV
+};
+
+const scan_order intra_scan_orders[TX_SIZES][INTRA_MODES] = {
+  {  // 4X4
+    {vp9_default_scan_4x4,   vp9_default_scan_4x4_neighbors},    // DC
+    {vp9_row_scan_4x4,       vp9_row_scan_4x4_neighbors},        // V
+    {vp9_col_scan_4x4,       vp9_col_scan_4x4_neighbors},        // H
+    {vp9_default_scan_4x4,   vp9_default_scan_4x4_neighbors},    // D45
+    {vp9_default_scan_4x4,   vp9_default_scan_4x4_neighbors},    // D135
+    {vp9_row_scan_4x4,       vp9_row_scan_4x4_neighbors},        // D117
+    {vp9_col_scan_4x4,       vp9_col_scan_4x4_neighbors},        // D153
+    {vp9_col_scan_4x4,       vp9_col_scan_4x4_neighbors},        // D207
+    {vp9_row_scan_4x4,       vp9_row_scan_4x4_neighbors},        // D63
+    {vp9_default_scan_4x4,   vp9_default_scan_4x4_neighbors},    // TM
+  }, {  // 8x8
+    {vp9_default_scan_8x8,   vp9_default_scan_8x8_neighbors},    // DC
+    {vp9_row_scan_8x8,       vp9_row_scan_8x8_neighbors},        // V
+    {vp9_col_scan_8x8,       vp9_col_scan_8x8_neighbors},        // H
+    {vp9_default_scan_8x8,   vp9_default_scan_8x8_neighbors},    // D45
+    {vp9_default_scan_8x8,   vp9_default_scan_8x8_neighbors},    // D135
+    {vp9_row_scan_8x8,       vp9_row_scan_8x8_neighbors},        // D117
+    {vp9_col_scan_8x8,       vp9_col_scan_8x8_neighbors},        // D153
+    {vp9_col_scan_8x8,       vp9_col_scan_8x8_neighbors},        // D207
+    {vp9_row_scan_8x8,       vp9_row_scan_8x8_neighbors},        // D63
+    {vp9_default_scan_8x8,   vp9_default_scan_8x8_neighbors},    // TM
+  }, {  // 16x16
+    {vp9_default_scan_16x16, vp9_default_scan_16x16_neighbors},  // DC
+    {vp9_row_scan_16x16,     vp9_row_scan_16x16_neighbors},      // V
+    {vp9_col_scan_16x16,     vp9_col_scan_16x16_neighbors},      // H
+    {vp9_default_scan_16x16, vp9_default_scan_16x16_neighbors},  // D45
+    {vp9_default_scan_16x16, vp9_default_scan_16x16_neighbors},  // D135
+    {vp9_row_scan_16x16,     vp9_row_scan_16x16_neighbors},      // D117
+    {vp9_col_scan_16x16,     vp9_col_scan_16x16_neighbors},      // D153
+    {vp9_col_scan_16x16,     vp9_col_scan_16x16_neighbors},      // D207
+    {vp9_row_scan_16x16,     vp9_row_scan_16x16_neighbors},      // D63
+    {vp9_default_scan_16x16, vp9_default_scan_16x16_neighbors},  // TM
+  }, {  // 32x32
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // DC
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // V
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // H
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D45
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D135
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D117
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D153
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D207
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // D63
+    {vp9_default_scan_32x32, vp9_default_scan_32x32_neighbors},  // TM
+  }
+};
+
+
 static int find_in_scan(const int16_t *scan, int l, int idx) {
   int n, l2 = l * l;
   for (n = 0; n < l2; n++) {
diff --git a/vp9/common/vp9_scan.h b/vp9/common/vp9_scan.h
index 14a1a7e..98fc607 100644
--- a/vp9/common/vp9_scan.h
+++ b/vp9/common/vp9_scan.h
@@ -15,6 +15,7 @@
 #include "vpx_ports/mem.h"
 
 #include "vp9/common/vp9_enums.h"
+#include "vp9/common/vp9_blockd.h"
 
 #define MAX_NEIGHBORS 2
 
@@ -67,9 +68,16 @@
 extern DECLARE_ALIGNED(16, int16_t,
                        vp9_default_scan_32x32_neighbors[1025 * MAX_NEIGHBORS]);
 
-
 void vp9_init_neighbors();
 
+typedef struct {
+  const int16_t *scan;
+  const int16_t *neighbors;
+} scan_order;
+
+extern const scan_order intra_scan_orders[TX_SIZES][INTRA_MODES];
+extern const scan_order inter_scan_orders[TX_SIZES];
+
 static INLINE const int16_t* get_scan_4x4(TX_TYPE tx_type) {
   switch (tx_type) {
     case ADST_DCT:
diff --git a/vp9/decoder/vp9_decodeframe.c b/vp9/decoder/vp9_decodeframe.c
index 1f4fb66..dbcae76 100644
--- a/vp9/decoder/vp9_decodeframe.c
+++ b/vp9/decoder/vp9_decodeframe.c
@@ -241,16 +241,13 @@
 }
 
 static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block,
-                                    TX_SIZE tx_size, int x, int y) {
+                                    TX_SIZE tx_size, uint8_t *dst, int stride) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
   const int eob = pd->eobs[block];
   if (eob > 0) {
     TX_TYPE tx_type;
     const int plane_type = pd->plane_type;
-    const int stride = pd->dst.stride;
     int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
-    uint8_t *const dst = &pd->dst.buf[4 * y * stride + 4 * x];
-
     switch (tx_size) {
       case TX_4X4:
         tx_type = get_tx_type_4x4(plane_type, xd, block);
@@ -322,7 +319,7 @@
   if (!mi->mbmi.skip_coeff) {
     vp9_decode_block_tokens(cm, xd, plane, block, plane_bsize, x, y, tx_size,
                             args->r, args->token_cache);
-    inverse_transform_block(xd, plane, block, tx_size, x, y);
+    inverse_transform_block(xd, plane, block, tx_size, dst, pd->dst.stride);
   }
 }
 
@@ -340,13 +337,15 @@
   struct inter_args *args = arg;
   VP9_COMMON *const cm = args->cm;
   MACROBLOCKD *const xd = args->xd;
+  struct macroblockd_plane *const pd = &xd->plane[plane];
   int x, y;
   txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
-
   *args->eobtotal += vp9_decode_block_tokens(cm, xd, plane, block,
                                              plane_bsize, x, y, tx_size,
                                              args->r, args->token_cache);
-  inverse_transform_block(xd, plane, block, tx_size, x, y);
+  inverse_transform_block(xd, plane, block, tx_size,
+                          &pd->dst.buf[4 * y * pd->dst.stride + 4 * x],
+                          pd->dst.stride);
 }
 
 static void set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
diff --git a/vp9/decoder/vp9_detokenize.c b/vp9/decoder/vp9_detokenize.c
index 05a2b42..522d666 100644
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -23,14 +23,14 @@
 #define EOB_CONTEXT_NODE            0
 #define ZERO_CONTEXT_NODE           1
 #define ONE_CONTEXT_NODE            2
-#define LOW_VAL_CONTEXT_NODE        3
-#define TWO_CONTEXT_NODE            4
-#define THREE_CONTEXT_NODE          5
-#define HIGH_LOW_CONTEXT_NODE       6
-#define CAT_ONE_CONTEXT_NODE        7
-#define CAT_THREEFOUR_CONTEXT_NODE  8
-#define CAT_THREE_CONTEXT_NODE      9
-#define CAT_FIVE_CONTEXT_NODE       10
+#define LOW_VAL_CONTEXT_NODE        0
+#define TWO_CONTEXT_NODE            1
+#define THREE_CONTEXT_NODE          2
+#define HIGH_LOW_CONTEXT_NODE       3
+#define CAT_ONE_CONTEXT_NODE        4
+#define CAT_THREEFOUR_CONTEXT_NODE  5
+#define CAT_THREE_CONTEXT_NODE      6
+#define CAT_FIVE_CONTEXT_NODE       7
 
 #define CAT1_MIN_VAL    5
 #define CAT2_MIN_VAL    7
@@ -76,14 +76,16 @@
 
 #define WRITE_COEF_CONTINUE(val, token)                  \
   {                                                      \
-    dqcoeff_ptr[scan[c]] = (vp9_read_bit(r) ? -val : val) * \
-                            dq[c > 0] / (1 + (tx_size == TX_32X32)); \
+    v = (val * dqv) >> dq_shift; \
+    dqcoeff_ptr[scan[c]] = (vp9_read_bit(r) ? -v : v); \
     INCREMENT_COUNT(token);                              \
     token_cache[scan[c]] = vp9_pt_energy_class[token];   \
     ++c;                                                 \
+    dqv = dq[1];                                          \
     continue;                                            \
   }
 
+
 #define ADJUST_COEF(prob, bits_count)                   \
   do {                                                  \
     val += (vp9_read(r, prob) << bits_count);           \
@@ -100,8 +102,6 @@
   int band, c = 0;
   const vp9_prob (*coef_probs)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES] =
       fc->coef_probs[tx_size][type][ref];
-  vp9_prob coef_probs_full[COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
-  uint8_t load_map[COEF_BANDS][PREV_COEF_CONTEXTS] = { { 0 } };
   const vp9_prob *prob;
   unsigned int (*coef_counts)[PREV_COEF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
       counts->coef[tx_size][type][ref];
@@ -110,8 +110,23 @@
   const int16_t *scan, *nb;
   const uint8_t *cat6;
   const uint8_t *band_translate = get_band_translate(tx_size);
-  get_scan(xd, tx_size, type, block_idx, &scan, &nb);
+  const int dq_shift = (tx_size == TX_32X32);
+  const MODE_INFO *const mi = xd->mi_8x8[0];
+  const MB_MODE_INFO *const mbmi = &mi->mbmi;
+  scan_order const *so;
+  int v;
+  int16_t dqv = dq[0];
 
+  if (mbmi->ref_frame[0] > 0 || type != PLANE_TYPE_Y_WITH_DC || xd->lossless) {
+    so = &inter_scan_orders[tx_size];
+  } else {
+    MB_PREDICTION_MODE mode = mbmi->mode;
+    if (mbmi->sb_type < BLOCK_8X8)
+      mode = mi->bmi[block_idx].as_mode;
+    so = &intra_scan_orders[tx_size][mode];
+  }
+  scan = so->scan;
+  nb = so->neighbors;
   while (c < seg_eob) {
     int val;
     if (c)
@@ -136,6 +151,7 @@
     if (!vp9_read(r, prob[ZERO_CONTEXT_NODE])) {
       INCREMENT_COUNT(ZERO_TOKEN);
       token_cache[scan[c]] = vp9_pt_energy_class[ZERO_TOKEN];
+      dqv = dq[1];                                          \
       ++c;
       goto SKIP_START;
     }
@@ -144,13 +160,9 @@
     if (!vp9_read(r, prob[ONE_CONTEXT_NODE])) {
       WRITE_COEF_CONTINUE(1, ONE_TOKEN);
     }
-    // Load full probabilities if not already loaded
-    if (!load_map[band][pt]) {
-      vp9_model_to_full_probs(coef_probs[band][pt],
-                              coef_probs_full[band][pt]);
-      load_map[band][pt] = 1;
-    }
-    prob = coef_probs_full[band][pt];
+
+    prob = vp9_pareto8_full[coef_probs[band][pt][PIVOT_NODE]-1];
+
     // LOW_VAL_CONTEXT_NODE_0_
     if (!vp9_read(r, prob[LOW_VAL_CONTEXT_NODE])) {
       if (!vp9_read(r, prob[TWO_CONTEXT_NODE])) {
diff --git a/vp9/encoder/vp9_encodemb.c b/vp9/encoder/vp9_encodemb.c
index 0d089ce..0e1523b 100644
--- a/vp9/encoder/vp9_encodemb.c
+++ b/vp9/encoder/vp9_encodemb.c
@@ -25,6 +25,26 @@
 #include "vp9/encoder/vp9_rdopt.h"
 #include "vp9/encoder/vp9_tokenize.h"
 
+void vp9_setup_interp_filters(MACROBLOCKD *xd,
+                              INTERPOLATION_TYPE mcomp_filter_type,
+                              VP9_COMMON *cm) {
+  if (xd->mi_8x8 && xd->mi_8x8[0]) {
+    MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
+
+    set_scale_factors(xd, mbmi->ref_frame[0] - LAST_FRAME,
+                          mbmi->ref_frame[1] - LAST_FRAME,
+                          cm->active_ref_scale);
+  } else {
+    set_scale_factors(xd, -1, -1, cm->active_ref_scale);
+  }
+
+  xd->subpix.filter_x = xd->subpix.filter_y =
+      vp9_get_filter_kernel(mcomp_filter_type == SWITCHABLE ?
+                               EIGHTTAP : mcomp_filter_type);
+
+  assert(((intptr_t)xd->subpix.filter_x & 0xff) == 0);
+}
+
 void vp9_subtract_block_c(int rows, int cols,
                           int16_t *diff_ptr, ptrdiff_t diff_stride,
                           const uint8_t *src_ptr, ptrdiff_t src_stride,
diff --git a/vp9/encoder/vp9_encodemb.h b/vp9/encoder/vp9_encodemb.h
index 8fbc4a0..7be6621 100644
--- a/vp9/encoder/vp9_encodemb.h
+++ b/vp9/encoder/vp9_encodemb.h
@@ -54,5 +54,7 @@
 void vp9_encode_intra_block_uv(MACROBLOCK *x, BLOCK_SIZE bsize);
 
 int vp9_encode_intra(MACROBLOCK *x, int use_16x16_pred);
-
+void vp9_setup_interp_filters(MACROBLOCKD *xd,
+                              INTERPOLATION_TYPE mcomp_filter_type,
+                              VP9_COMMON *cm);
 #endif  // VP9_ENCODER_VP9_ENCODEMB_H_
diff --git a/vp9/encoder/vp9_encodemv.c b/vp9/encoder/vp9_encodemv.c
index 030ca64..7e838c9 100644
--- a/vp9/encoder/vp9_encodemv.c
+++ b/vp9/encoder/vp9_encodemv.c
@@ -126,20 +126,15 @@
 
 static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p,
                      vp9_prob upd_p) {
-  const vp9_prob new_p = get_binary_prob(ct[0], ct[1]);
-  vp9_prob mod_p = new_p | 1;
-  const int cur_b = cost_branch256(ct, *cur_p);
-  const int mod_b = cost_branch256(ct, mod_p);
-  const int cost = 7 * 256 + (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
-  if (cur_b - mod_b > cost) {
-    *cur_p = mod_p;
-    vp9_write(w, 1, upd_p);
-    vp9_write_literal(w, mod_p >> 1, 7);
-    return 1;
-  } else {
-    vp9_write(w, 0, upd_p);
-    return 0;
+  const vp9_prob new_p = get_binary_prob(ct[0], ct[1]) | 1;
+  const int update = cost_branch256(ct, *cur_p) + vp9_cost_zero(upd_p) >
+                     cost_branch256(ct, new_p) + vp9_cost_one(upd_p) + 7 * 256;
+  vp9_write(w, update, upd_p);
+  if (update) {
+    *cur_p = new_p;
+    vp9_write_literal(w, new_p >> 1, 7);
   }
+  return update;
 }
 
 static void counts_to_nmv_context(
diff --git a/vp9/encoder/vp9_sad_c.c b/vp9/encoder/vp9_sad_c.c
index 42ddb21..55d595b 100644
--- a/vp9/encoder/vp9_sad_c.c
+++ b/vp9/encoder/vp9_sad_c.c
@@ -10,11 +10,11 @@
 
 
 #include <stdlib.h>
-#include "vp9/common/vp9_sadmxn.h"
-#include "vp9/encoder/vp9_variance.h"
-#include "./vpx_config.h"
-#include "vpx/vpx_integer.h"
 #include "./vp9_rtcd.h"
+#include "./vpx_config.h"
+#include "vp9/encoder/vp9_sadmxn.h"
+#include "vp9/encoder/vp9_variance.h"
+#include "vpx/vpx_integer.h"
 
 #define sad_mxn_func(m, n) \
 unsigned int vp9_sad##m##x##n##_c(const uint8_t *src_ptr, \
diff --git a/vp9/common/vp9_sadmxn.h b/vp9/encoder/vp9_sadmxn.h
similarity index 100%
rename from vp9/common/vp9_sadmxn.h
rename to vp9/encoder/vp9_sadmxn.h
diff --git a/vp9/encoder/vp9_subexp.c b/vp9/encoder/vp9_subexp.c
index 387fc90..f31e568 100644
--- a/vp9/encoder/vp9_subexp.c
+++ b/vp9/encoder/vp9_subexp.c
@@ -14,7 +14,6 @@
 #include "vp9/encoder/vp9_boolhuff.h"
 #include "vp9/encoder/vp9_treewriter.h"
 
-#define vp9_cost_upd  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8)
 #define vp9_cost_upd256  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
 
 static int update_bits[255];
diff --git a/vp9/encoder/vp9_treewriter.h b/vp9/encoder/vp9_treewriter.h
index eeda5cd..41d1bfb 100644
--- a/vp9/encoder/vp9_treewriter.h
+++ b/vp9/encoder/vp9_treewriter.h
@@ -19,31 +19,20 @@
 
 #include "vp9/encoder/vp9_boolhuff.h"       /* for now */
 
-
 #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
 
-/* Approximate length of an encoded bool in 256ths of a bit at given prob */
+#define vp9_cost_zero(prob) (vp9_prob_cost[prob])
 
-#define vp9_cost_zero(x) (vp9_prob_cost[x])
-#define vp9_cost_one(x) vp9_cost_zero(vp9_complement(x))
+#define vp9_cost_one(prob) vp9_cost_zero(vp9_complement(prob))
 
-#define vp9_cost_bit(x, b) vp9_cost_zero((b) ? vp9_complement(x) : (x))
+#define vp9_cost_bit(prob, bit) vp9_cost_zero((bit) ? vp9_complement(prob) \
+                                                    : (prob))
 
-/* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */
-
-
-/* Both of these return bits, not scaled bits. */
 static INLINE unsigned int cost_branch256(const unsigned int ct[2],
                                           vp9_prob p) {
   return ct[0] * vp9_cost_zero(p) + ct[1] * vp9_cost_one(p);
 }
 
-static INLINE unsigned int cost_branch(const unsigned int ct[2],
-                                       vp9_prob p) {
-  return cost_branch256(ct, p) >> 8;
-}
-
-
 static INLINE void treed_write(vp9_writer *w,
                                vp9_tree tree, const vp9_prob *probs,
                                int bits, int len) {
diff --git a/vp9/vp9_common.mk b/vp9/vp9_common.mk
index 57ede0f..c566765 100644
--- a/vp9/vp9_common.mk
+++ b/vp9/vp9_common.mk
@@ -45,7 +45,6 @@
 VP9_COMMON_SRCS-yes += common/vp9_reconintra.h
 VP9_COMMON_SRCS-yes += common/vp9_rtcd.c
 VP9_COMMON_SRCS-yes += common/vp9_rtcd_defs.sh
-VP9_COMMON_SRCS-yes += common/vp9_sadmxn.h
 VP9_COMMON_SRCS-yes += common/vp9_scale.h
 VP9_COMMON_SRCS-yes += common/vp9_scale.c
 VP9_COMMON_SRCS-yes += common/vp9_seg_common.h
diff --git a/vp9/vp9cx.mk b/vp9/vp9cx.mk
index b594779..bd13518 100644
--- a/vp9/vp9cx.mk
+++ b/vp9/vp9cx.mk
@@ -44,6 +44,7 @@
 VP9_CX_SRCS-yes += encoder/vp9_quantize.h
 VP9_CX_SRCS-yes += encoder/vp9_ratectrl.h
 VP9_CX_SRCS-yes += encoder/vp9_rdopt.h
+VP9_CX_SRCS-yes += encoder/vp9_sadmxn.h
 VP9_CX_SRCS-yes += encoder/vp9_tokenize.h
 VP9_CX_SRCS-yes += encoder/vp9_treewriter.h
 VP9_CX_SRCS-yes += encoder/vp9_variance.h
diff --git a/vpxdec.c b/vpxdec.c
index 6508992..dc2eec8 100644
--- a/vpxdec.c
+++ b/vpxdec.c
@@ -15,6 +15,9 @@
 #include <string.h>
 #include <limits.h>
 
+#include "third_party/libyuv/include/libyuv/scale.h"
+
+#include "./args.h"
 #include "./ivfdec.h"
 
 #define VPX_CODEC_DISABLE_COMPAT 1
@@ -27,20 +30,19 @@
 #endif
 
 #if CONFIG_MD5
-#include "md5_utils.h"
+#include "./md5_utils.h"
 #endif
 
 #include "./tools_common.h"
-#include "nestegg/include/nestegg/nestegg.h"
-#include "third_party/libyuv/include/libyuv/scale.h"
+#include "./webmdec.h"
 
 static const char *exec_name;
 
 static const struct {
   char const *name;
   const vpx_codec_iface_t *(*iface)(void);
-  unsigned int             fourcc;
-  unsigned int             fourcc_mask;
+  uint32_t fourcc;
+  uint32_t fourcc_mask;
 } ifaces[] = {
 #if CONFIG_VP8_DECODER
   {"vp8",  vpx_codec_vp8_dx,   VP8_FOURCC_MASK, 0x00FFFFFF},
@@ -50,7 +52,11 @@
 #endif
 };
 
-#include "args.h"
+struct VpxDecInputContext {
+  struct VpxInputContext *vpx_input_ctx;
+  struct WebmInputContext *webm_ctx;
+};
+
 static const arg_def_t looparg = ARG_DEF(NULL, "loops", 1,
                                           "Number of times to decode the file");
 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
@@ -162,15 +168,6 @@
   exit(EXIT_FAILURE);
 }
 
-struct VpxDecInputContext {
-  nestegg *nestegg_ctx;
-  nestegg_packet *pkt;
-  unsigned int chunk;
-  unsigned int chunks;
-  unsigned int video_track;
-  struct VpxInputContext *vpx_input_ctx;
-};
-
 static int read_frame(struct VpxDecInputContext *input,
                       uint8_t **buf,
                       size_t *bytes_in_buffer,
@@ -180,30 +177,8 @@
   FILE *infile = input->vpx_input_ctx->file;
   enum VideoFileType kind = input->vpx_input_ctx->file_type;
   if (kind == FILE_TYPE_WEBM) {
-    if (input->chunk >= input->chunks) {
-      unsigned int track;
-
-      do {
-        /* End of this packet, get another. */
-        if (input->pkt)
-          nestegg_free_packet(input->pkt);
-
-        if (nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0
-            || nestegg_packet_track(input->pkt, &track))
-          return 1;
-
-      } while (track != input->video_track);
-
-      if (nestegg_packet_count(input->pkt, &input->chunks))
-        return 1;
-      input->chunk = 0;
-    }
-
-    if (nestegg_packet_data(input->pkt, input->chunk, buf, bytes_in_buffer))
-      return 1;
-    input->chunk++;
-
-    return 0;
+    return webm_read_frame(input->webm_ctx,
+                           buf, bytes_in_buffer, buffer_size);
   } else if (kind == FILE_TYPE_RAW) {
     if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
       if (!feof(infile))
@@ -333,161 +308,12 @@
   return is_raw;
 }
 
-
-static int
-nestegg_read_cb(void *buffer, size_t length, void *userdata) {
-  FILE *f = userdata;
-
-  if (fread(buffer, 1, length, f) < length) {
-    if (ferror(f))
-      return -1;
-    if (feof(f))
-      return 0;
-  }
-  return 1;
-}
-
-
-static int
-nestegg_seek_cb(int64_t offset, int whence, void *userdata) {
-  switch (whence) {
-    case NESTEGG_SEEK_SET:
-      whence = SEEK_SET;
-      break;
-    case NESTEGG_SEEK_CUR:
-      whence = SEEK_CUR;
-      break;
-    case NESTEGG_SEEK_END:
-      whence = SEEK_END;
-      break;
-  };
-  return fseek(userdata, (long)offset, whence) ? -1 : 0;
-}
-
-
-static int64_t
-nestegg_tell_cb(void *userdata) {
-  return ftell(userdata);
-}
-
-
-static void
-nestegg_log_cb(nestegg *context, unsigned int severity, char const *format,
-               ...) {
-  va_list ap;
-
-  va_start(ap, format);
-  vfprintf(stderr, format, ap);
-  fprintf(stderr, "\n");
-  va_end(ap);
-}
-
-
-static int
-webm_guess_framerate(struct VpxDecInputContext *input) {
-  unsigned int i;
-  uint64_t     tstamp = 0;
-
-  /* Check to see if we can seek before we parse any data. */
-  if (nestegg_track_seek(input->nestegg_ctx, input->video_track, 0)) {
-    warn("WARNING: Failed to guess framerate (no Cues), set to 30fps.\n");
-    input->vpx_input_ctx->framerate.numerator = 30;
-    input->vpx_input_ctx->framerate.denominator  = 1;
-    return 0;
-  }
-
-  /* Guess the framerate. Read up to 1 second, or 50 video packets,
-   * whichever comes first.
-   */
-  for (i = 0; tstamp < 1000000000 && i < 50;) {
-    nestegg_packet *pkt;
-    unsigned int track;
-
-    if (nestegg_read_packet(input->nestegg_ctx, &pkt) <= 0)
-      break;
-
-    nestegg_packet_track(pkt, &track);
-    if (track == input->video_track) {
-      nestegg_packet_tstamp(pkt, &tstamp);
-      i++;
-    }
-
-    nestegg_free_packet(pkt);
-  }
-
-  if (nestegg_track_seek(input->nestegg_ctx, input->video_track, 0))
-    goto fail;
-
-  input->vpx_input_ctx->framerate.numerator = (i - 1) * 1000000;
-  input->vpx_input_ctx->framerate.denominator = (int)(tstamp / 1000);
-  return 0;
-fail:
-  nestegg_destroy(input->nestegg_ctx);
-  input->nestegg_ctx = NULL;
-  rewind(input->vpx_input_ctx->file);
-  return 1;
-}
-
-
-static int
-file_is_webm(struct VpxDecInputContext *input) {
-  unsigned int i, n;
-  int track_type = -1;
-  int codec_id;
-
-  nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb, 0};
-  nestegg_video_params params;
-
-  io.userdata = input->vpx_input_ctx->file;
-  if (nestegg_init(&input->nestegg_ctx, io, NULL))
-    goto fail;
-
-  if (nestegg_track_count(input->nestegg_ctx, &n))
-    goto fail;
-
-  for (i = 0; i < n; i++) {
-    track_type = nestegg_track_type(input->nestegg_ctx, i);
-
-    if (track_type == NESTEGG_TRACK_VIDEO)
-      break;
-    else if (track_type < 0)
-      goto fail;
-  }
-
-  codec_id = nestegg_track_codec_id(input->nestegg_ctx, i);
-  if (codec_id == NESTEGG_CODEC_VP8) {
-    input->vpx_input_ctx->fourcc = VP8_FOURCC_MASK;
-  } else if (codec_id == NESTEGG_CODEC_VP9) {
-    input->vpx_input_ctx->fourcc = VP9_FOURCC_MASK;
-  } else {
-    fprintf(stderr, "Not VPx video, quitting.\n");
-    exit(1);
-  }
-
-  input->video_track = i;
-
-  if (nestegg_track_video_params(input->nestegg_ctx, i, &params))
-    goto fail;
-
-  input->vpx_input_ctx->framerate.denominator = 0;
-  input->vpx_input_ctx->framerate.numerator = 0;
-  input->vpx_input_ctx->width = params.width;
-  input->vpx_input_ctx->height = params.height;
-  return 1;
-fail:
-  input->nestegg_ctx = NULL;
-  rewind(input->vpx_input_ctx->file);
-  return 0;
-}
-
-
 void show_progress(int frame_in, int frame_out, unsigned long dx_time) {
   fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r",
           frame_in, frame_out, dx_time,
           (float)frame_out * 1000000.0 / (float)dx_time);
 }
 
-
 void generate_filename(const char *pattern, char *out, size_t q_len,
                        unsigned int d_w, unsigned int d_h,
                        unsigned int frame_in) {
@@ -606,7 +432,9 @@
 
   struct VpxDecInputContext input = {0};
   struct VpxInputContext vpx_input_ctx = {0};
+  struct WebmInputContext webm_ctx = {0};
   input.vpx_input_ctx = &vpx_input_ctx;
+  input.webm_ctx = &webm_ctx;
 
   /* Parse command line */
   exec_name = argv_[0];
@@ -748,7 +576,7 @@
   input.vpx_input_ctx->file = infile;
   if (file_is_ivf(input.vpx_input_ctx))
     input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
-  else if (file_is_webm(&input))
+  else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
     input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
   else if (file_is_raw(input.vpx_input_ctx))
     input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
@@ -792,7 +620,7 @@
     }
 
     if (vpx_input_ctx.file_type == FILE_TYPE_WEBM)
-      if (webm_guess_framerate(&input)) {
+      if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
         fprintf(stderr, "Failed to guess framerate -- error parsing "
                 "webm file?\n");
         return EXIT_FAILURE;
@@ -1036,10 +864,11 @@
   if (single_file && !noblit)
     out_close(out, outfile, do_md5);
 
-  if (input.nestegg_ctx)
-    nestegg_destroy(input.nestegg_ctx);
-  if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM)
+  if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
+    webm_free(input.webm_ctx);
+  else
     free(buf);
+
   fclose(infile);
   free(argv);
 
diff --git a/webmdec.c b/webmdec.c
new file mode 100644
index 0000000..4bf7c7e
--- /dev/null
+++ b/webmdec.c
@@ -0,0 +1,193 @@
+/*
+ *  Copyright (c) 2013 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.
+ */
+
+#include "./webmdec.h"
+
+#include <stdarg.h>
+
+#include "nestegg/include/nestegg/nestegg.h"
+
+static int nestegg_read_cb(void *buffer, size_t length, void *userdata) {
+  FILE *f = userdata;
+
+  if (fread(buffer, 1, length, f) < length) {
+    if (ferror(f))
+      return -1;
+    if (feof(f))
+      return 0;
+  }
+  return 1;
+}
+
+static int nestegg_seek_cb(int64_t offset, int whence, void *userdata) {
+  switch (whence) {
+    case NESTEGG_SEEK_SET:
+      whence = SEEK_SET;
+      break;
+    case NESTEGG_SEEK_CUR:
+      whence = SEEK_CUR;
+      break;
+    case NESTEGG_SEEK_END:
+      whence = SEEK_END;
+      break;
+  };
+  return fseek(userdata, (int32_t)offset, whence) ? -1 : 0;
+}
+
+static int64_t nestegg_tell_cb(void *userdata) {
+  return ftell(userdata);
+}
+
+static void nestegg_log_cb(nestegg *context,
+                           unsigned int severity,
+                           char const *format, ...) {
+  va_list ap;
+  va_start(ap, format);
+  vfprintf(stderr, format, ap);
+  fprintf(stderr, "\n");
+  va_end(ap);
+}
+
+int file_is_webm(struct WebmInputContext *webm_ctx,
+                 struct VpxInputContext *vpx_ctx) {
+  uint32_t i, n;
+  int track_type = -1;
+  int codec_id;
+
+  nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb, 0};
+  nestegg_video_params params;
+
+  io.userdata = vpx_ctx->file;
+  if (nestegg_init(&webm_ctx->nestegg_ctx, io, NULL))
+    goto fail;
+
+  if (nestegg_track_count(webm_ctx->nestegg_ctx, &n))
+    goto fail;
+
+  for (i = 0; i < n; i++) {
+    track_type = nestegg_track_type(webm_ctx->nestegg_ctx, i);
+
+    if (track_type == NESTEGG_TRACK_VIDEO)
+      break;
+    else if (track_type < 0)
+      goto fail;
+  }
+
+  codec_id = nestegg_track_codec_id(webm_ctx->nestegg_ctx, i);
+  if (codec_id == NESTEGG_CODEC_VP8) {
+    vpx_ctx->fourcc = VP8_FOURCC_MASK;
+  } else if (codec_id == NESTEGG_CODEC_VP9) {
+    vpx_ctx->fourcc = VP9_FOURCC_MASK;
+  } else {
+    fatal("Not VPx video, quitting.\n");
+  }
+
+  webm_ctx->video_track = i;
+
+  if (nestegg_track_video_params(webm_ctx->nestegg_ctx, i, &params))
+    goto fail;
+
+  vpx_ctx->framerate.denominator = 0;
+  vpx_ctx->framerate.numerator = 0;
+  vpx_ctx->width = params.width;
+  vpx_ctx->height = params.height;
+
+  return 1;
+
+ fail:
+  webm_ctx->nestegg_ctx = NULL;
+  rewind(vpx_ctx->file);
+
+  return 0;
+}
+
+int webm_read_frame(struct WebmInputContext *webm_ctx,
+                    uint8_t **buffer,
+                    size_t *bytes_in_buffer,
+                    size_t *buffer_size) {
+  if (webm_ctx->chunk >= webm_ctx->chunks) {
+    uint32_t track;
+
+    do {
+      /* End of this packet, get another. */
+      if (webm_ctx->pkt)
+        nestegg_free_packet(webm_ctx->pkt);
+
+      if (nestegg_read_packet(webm_ctx->nestegg_ctx, &webm_ctx->pkt) <= 0 ||
+          nestegg_packet_track(webm_ctx->pkt, &track)) {
+        return 1;
+      }
+    } while (track != webm_ctx->video_track);
+
+    if (nestegg_packet_count(webm_ctx->pkt, &webm_ctx->chunks))
+      return 1;
+
+    webm_ctx->chunk = 0;
+  }
+
+  if (nestegg_packet_data(webm_ctx->pkt, webm_ctx->chunk,
+                          buffer, bytes_in_buffer)) {
+    return 1;
+  }
+
+  webm_ctx->chunk++;
+  return 0;
+}
+
+int webm_guess_framerate(struct WebmInputContext *webm_ctx,
+                         struct VpxInputContext *vpx_ctx) {
+  uint32_t i;
+  uint64_t tstamp = 0;
+
+  /* Check to see if we can seek before we parse any data. */
+  if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0)) {
+    warn("Failed to guess framerate (no Cues), set to 30fps.\n");
+    vpx_ctx->framerate.numerator = 30;
+    vpx_ctx->framerate.denominator  = 1;
+    return 0;
+  }
+
+  /* Guess the framerate. Read up to 1 second, or 50 video packets,
+   * whichever comes first.
+   */
+  for (i = 0; tstamp < 1000000000 && i < 50;) {
+    nestegg_packet *pkt;
+    uint32_t track;
+
+    if (nestegg_read_packet(webm_ctx->nestegg_ctx, &pkt) <= 0)
+      break;
+
+    nestegg_packet_track(pkt, &track);
+    if (track == webm_ctx->video_track) {
+      nestegg_packet_tstamp(pkt, &tstamp);
+      ++i;
+    }
+
+    nestegg_free_packet(pkt);
+  }
+
+  if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0))
+    goto fail;
+
+  vpx_ctx->framerate.numerator = (i - 1) * 1000000;
+  vpx_ctx->framerate.denominator = (int)(tstamp / 1000);
+  return 0;
+
+ fail:
+  nestegg_destroy(webm_ctx->nestegg_ctx);
+  webm_ctx->nestegg_ctx = NULL;
+  rewind(vpx_ctx->file);
+  return 1;
+}
+
+void webm_free(struct WebmInputContext *webm_ctx) {
+  if (webm_ctx && webm_ctx->nestegg_ctx)
+    nestegg_destroy(webm_ctx->nestegg_ctx);
+}
diff --git a/webmdec.h b/webmdec.h
new file mode 100644
index 0000000..002fbe6
--- /dev/null
+++ b/webmdec.h
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+#ifndef WEBMDEC_H_
+#define WEBMDEC_H_
+
+#include "./tools_common.h"
+
+struct nestegg;
+struct nestegg_packet;
+struct VpxInputContext;
+
+struct WebmInputContext {
+  uint32_t chunk;
+  uint32_t chunks;
+  uint32_t video_track;
+  struct nestegg *nestegg_ctx;
+  struct nestegg_packet *pkt;
+};
+
+int file_is_webm(struct WebmInputContext *webm_ctx,
+                 struct VpxInputContext *vpx_ctx);
+
+int webm_read_frame(struct WebmInputContext *webm_ctx,
+                    uint8_t **buffer,
+                    size_t *bytes_in_buffer,
+                    size_t *buffer_size);
+
+int webm_guess_framerate(struct WebmInputContext *webm_ctx,
+                         struct VpxInputContext *vpx_ctx);
+
+void webm_free(struct WebmInputContext *webm_ctx);
+
+#endif  // WEBMDEC_H_