shithub: libvpx

Download patch

ref: 2bb322380d6b4b046e7a9aeba2b1058684d46e30
parent: 9e36d442327c597e87fefda4913981f65aa6ae6c
parent: 318a14c637e3ec91d3ee9541a0ed527106947203
author: John Koleszar <jkoleszar@google.com>
date: Mon Feb 7 09:16:09 EST 2011

Merge remote branch 'internal/upstream-experimental' into HEAD

Conflicts:
	vp8/encoder/encodeframe.c
	vp8/encoder/ethreading.c
	vp8/encoder/onyx_int.h

Change-Id: I1c562d2fe6e42c0d1d86f68c77c0e899066e02bd

--- a/vp8/common/arm/loopfilter_arm.c
+++ b/vp8/common/arm/loopfilter_arm.c
@@ -41,13 +41,13 @@
                                int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_horizontal_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_horizontal_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_horizontal_edge_armv6(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_armv6(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_horizontal_edge_armv6(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_armv6(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_mbhs_armv6(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -57,7 +57,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_horizontal_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_horizontal_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Vertical MB Filtering */
@@ -65,13 +65,13 @@
                                int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_vertical_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_vertical_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_vertical_edge_armv6(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_armv6(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_vertical_edge_armv6(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_armv6(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_mbvs_armv6(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -81,7 +81,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_vertical_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_vertical_edge_armv6(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Horizontal B Filtering */
@@ -94,10 +94,10 @@
     vp8_loop_filter_horizontal_edge_armv6(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_horizontal_edge_armv6(u_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_armv6(u_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_horizontal_edge_armv6(v_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_armv6(v_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_bhs_armv6(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -122,10 +122,10 @@
     vp8_loop_filter_vertical_edge_armv6(y_ptr + 12, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_vertical_edge_armv6(u_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_armv6(u_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_vertical_edge_armv6(v_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_armv6(v_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_bvs_armv6(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -148,10 +148,10 @@
                               int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_horizontal_edge_y_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_horizontal_edge_y_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_horizontal_edge_uv_neon(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, v_ptr);
+        vp8_mbloop_filter_horizontal_edge_uv_neon(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, v_ptr);
 }
 
 void vp8_loop_filter_mbhs_neon(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -161,7 +161,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_horizontal_edge_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_horizontal_edge_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Vertical MB Filtering */
@@ -169,10 +169,10 @@
                               int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_vertical_edge_y_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_vertical_edge_y_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_vertical_edge_uv_neon(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, v_ptr);
+        vp8_mbloop_filter_vertical_edge_uv_neon(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, v_ptr);
 }
 
 void vp8_loop_filter_mbvs_neon(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -182,7 +182,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_vertical_edge_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_vertical_edge_neon(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Horizontal B Filtering */
@@ -195,7 +195,7 @@
     vp8_loop_filter_horizontal_edge_y_neon(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_horizontal_edge_uv_neon(u_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, v_ptr + 4 * uv_stride);
+        vp8_loop_filter_horizontal_edge_uv_neon(u_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, v_ptr + 4 * uv_stride);
 }
 
 void vp8_loop_filter_bhs_neon(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -220,7 +220,7 @@
     vp8_loop_filter_vertical_edge_y_neon(y_ptr + 12, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_vertical_edge_uv_neon(u_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, v_ptr + 4);
+        vp8_loop_filter_vertical_edge_uv_neon(u_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, v_ptr + 4);
 }
 
 void vp8_loop_filter_bvs_neon(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
--- a/vp8/common/findnearmv.c
+++ b/vp8/common/findnearmv.c
@@ -11,8 +11,6 @@
 
 #include "findnearmv.h"
 
-#define FINDNEAR_SEARCH_SITES   3
-
 /* Predict motion vectors using those from already-decoded nearby blocks.
    Note that we only consider one 4x4 subblock from each candidate 16x16
    macroblock.   */
--- a/vp8/common/loopfilter.c
+++ b/vp8/common/loopfilter.c
@@ -28,13 +28,13 @@
                            int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_horizontal_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_horizontal_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_horizontal_edge_c(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_c(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_horizontal_edge_c(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_c(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_mbhs_c(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -44,7 +44,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_horizontal_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_horizontal_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Vertical MB Filtering */
@@ -52,13 +52,13 @@
                            int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_vertical_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_vertical_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_vertical_edge_c(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_c(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_vertical_edge_c(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_c(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_mbvs_c(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -68,7 +68,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_vertical_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_vertical_edge_c(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 /* Horizontal B Filtering */
@@ -81,10 +81,10 @@
     vp8_loop_filter_horizontal_edge_c(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_horizontal_edge_c(u_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_c(u_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_horizontal_edge_c(v_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_c(v_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_bhs_c(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -109,10 +109,10 @@
     vp8_loop_filter_vertical_edge_c(y_ptr + 12, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_vertical_edge_c(u_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_c(u_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_vertical_edge_c(v_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_c(v_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 void vp8_loop_filter_bvs_c(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -137,8 +137,6 @@
 
     int block_inside_limit = 0;
     int HEVThresh;
-    const int yhedge_boost  = 2;
-    const int uvhedge_boost = 2;
 
     /* For each possible value for the loop filter fill out a "loop_filter_info" entry. */
     for (i = 0; i <= MAX_LOOP_FILTER; i++)
@@ -182,15 +180,9 @@
         for (j = 0; j < 16; j++)
         {
             lfi[i].lim[j] = block_inside_limit;
-            lfi[i].mbflim[j] = filt_lvl + yhedge_boost;
-            lfi[i].mbthr[j] = HEVThresh;
+            lfi[i].mbflim[j] = filt_lvl + 2;
             lfi[i].flim[j] = filt_lvl;
             lfi[i].thr[j] = HEVThresh;
-            lfi[i].uvlim[j] = block_inside_limit;
-            lfi[i].uvmbflim[j] = filt_lvl + uvhedge_boost;
-            lfi[i].uvmbthr[j] = HEVThresh;
-            lfi[i].uvflim[j] = filt_lvl;
-            lfi[i].uvthr[j] = HEVThresh;
         }
 
     }
@@ -249,21 +241,15 @@
         for (j = 0; j < 16; j++)
         {
             /*lfi[i].lim[j] = block_inside_limit;
-            lfi[i].mbflim[j] = filt_lvl+yhedge_boost;*/
-            lfi[i].mbthr[j] = HEVThresh;
+            lfi[i].mbflim[j] = filt_lvl+2;*/
             /*lfi[i].flim[j] = filt_lvl;*/
             lfi[i].thr[j] = HEVThresh;
-            /*lfi[i].uvlim[j] = block_inside_limit;
-            lfi[i].uvmbflim[j] = filt_lvl+uvhedge_boost;*/
-            lfi[i].uvmbthr[j] = HEVThresh;
-            /*lfi[i].uvflim[j] = filt_lvl;*/
-            lfi[i].uvthr[j] = HEVThresh;
         }
     }
 }
 
 
-void vp8_adjust_mb_lf_value(MACROBLOCKD *mbd, int *filter_level)
+int vp8_adjust_mb_lf_value(MACROBLOCKD *mbd, int filter_level)
 {
     MB_MODE_INFO *mbmi = &mbd->mode_info_context->mbmi;
 
@@ -270,7 +256,7 @@
     if (mbd->mode_ref_lf_delta_enabled)
     {
         /* Apply delta for reference frame */
-        *filter_level += mbd->ref_lf_deltas[mbmi->ref_frame];
+        filter_level += mbd->ref_lf_deltas[mbmi->ref_frame];
 
         /* Apply delta for mode */
         if (mbmi->ref_frame == INTRA_FRAME)
@@ -277,29 +263,30 @@
         {
             /* Only the split mode BPRED has a further special case */
             if (mbmi->mode == B_PRED)
-                *filter_level +=  mbd->mode_lf_deltas[0];
+                filter_level +=  mbd->mode_lf_deltas[0];
         }
         else
         {
             /* Zero motion mode */
             if (mbmi->mode == ZEROMV)
-                *filter_level +=  mbd->mode_lf_deltas[1];
+                filter_level +=  mbd->mode_lf_deltas[1];
 
             /* Split MB motion mode */
             else if (mbmi->mode == SPLITMV)
-                *filter_level +=  mbd->mode_lf_deltas[3];
+                filter_level +=  mbd->mode_lf_deltas[3];
 
             /* All other inter motion modes (Nearest, Near, New) */
             else
-                *filter_level +=  mbd->mode_lf_deltas[2];
+                filter_level +=  mbd->mode_lf_deltas[2];
         }
 
         /* Range check */
-        if (*filter_level > MAX_LOOP_FILTER)
-            *filter_level = MAX_LOOP_FILTER;
-        else if (*filter_level < 0)
-            *filter_level = 0;
+        if (filter_level > MAX_LOOP_FILTER)
+            filter_level = MAX_LOOP_FILTER;
+        else if (filter_level < 0)
+            filter_level = 0;
     }
+    return filter_level;
 }
 
 
@@ -373,7 +360,7 @@
              * These specified to 8th pel as they are always compared to values that are in 1/8th pel units
              * Apply any context driven MB level adjustment
              */
-            vp8_adjust_mb_lf_value(mbd, &filter_level);
+            filter_level = vp8_adjust_mb_lf_value(mbd, filter_level);
 
             if (filter_level)
             {
@@ -473,7 +460,7 @@
             filter_level = baseline_filter_level[Segment];
 
             /* Apply any context driven MB level adjustment */
-            vp8_adjust_mb_lf_value(mbd, &filter_level);
+            filter_level = vp8_adjust_mb_lf_value(mbd, filter_level);
 
             if (filter_level)
             {
--- a/vp8/common/loopfilter.h
+++ b/vp8/common/loopfilter.h
@@ -32,12 +32,6 @@
     DECLARE_ALIGNED(16, signed char, flim[16]);
     DECLARE_ALIGNED(16, signed char, thr[16]);
     DECLARE_ALIGNED(16, signed char, mbflim[16]);
-    DECLARE_ALIGNED(16, signed char, mbthr[16]);
-    DECLARE_ALIGNED(16, signed char, uvlim[16]);
-    DECLARE_ALIGNED(16, signed char, uvflim[16]);
-    DECLARE_ALIGNED(16, signed char, uvthr[16]);
-    DECLARE_ALIGNED(16, signed char, uvmbflim[16]);
-    DECLARE_ALIGNED(16, signed char, uvmbthr[16]);
 } loop_filter_info;
 
 
--- a/vp8/common/onyxc_int.h
+++ b/vp8/common/onyxc_int.h
@@ -201,7 +201,7 @@
 } VP8_COMMON;
 
 
-void vp8_adjust_mb_lf_value(MACROBLOCKD *mbd, int *filter_level);
+int vp8_adjust_mb_lf_value(MACROBLOCKD *mbd, int filter_level);
 void vp8_init_loop_filter(VP8_COMMON *cm);
 void vp8_frame_init_loop_filter(loop_filter_info *lfi, int frame_type);
 extern void vp8_loop_filter_frame(VP8_COMMON *cm,    MACROBLOCKD *mbd,  int filt_val);
--- a/vp8/common/ppc/loopfilter_altivec.c
+++ b/vp8/common/ppc/loopfilter_altivec.c
@@ -56,10 +56,10 @@
                          int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void)simpler_lpf;
-    mbloop_filter_horizontal_edge_y_ppc(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr);
+    mbloop_filter_horizontal_edge_y_ppc(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr);
 
     if (u_ptr)
-        mbloop_filter_horizontal_edge_uv_ppc(u_ptr, v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr);
+        mbloop_filter_horizontal_edge_uv_ppc(u_ptr, v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr);
 }
 
 void loop_filter_mbhs_ppc(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -77,10 +77,10 @@
                          int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void)simpler_lpf;
-    mbloop_filter_vertical_edge_y_ppc(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr);
+    mbloop_filter_vertical_edge_y_ppc(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr);
 
     if (u_ptr)
-        mbloop_filter_vertical_edge_uv_ppc(u_ptr, v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr);
+        mbloop_filter_vertical_edge_uv_ppc(u_ptr, v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr);
 }
 
 void loop_filter_mbvs_ppc(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -104,7 +104,7 @@
     loop_filter_horizontal_edge_y_ppc(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr);
 
     if (u_ptr)
-        loop_filter_horizontal_edge_uv_ppc(u_ptr + 4 * uv_stride, v_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr);
+        loop_filter_horizontal_edge_uv_ppc(u_ptr + 4 * uv_stride, v_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr);
 }
 
 void loop_filter_bhs_ppc(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
@@ -127,7 +127,7 @@
     loop_filter_vertical_edge_y_ppc(y_ptr, y_stride, lfi->flim, lfi->lim, lfi->thr);
 
     if (u_ptr)
-        loop_filter_vertical_edge_uv_ppc(u_ptr + 4, v_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr);
+        loop_filter_vertical_edge_uv_ppc(u_ptr + 4, v_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr);
 }
 
 void loop_filter_bvs_ppc(unsigned char *y_ptr, unsigned char *u_ptr, unsigned char *v_ptr,
--- a/vp8/common/x86/loopfilter_x86.c
+++ b/vp8/common/x86/loopfilter_x86.c
@@ -45,13 +45,13 @@
                              int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_horizontal_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_horizontal_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_horizontal_edge_mmx(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_mmx(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_horizontal_edge_mmx(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_horizontal_edge_mmx(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 
@@ -62,7 +62,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_horizontal_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_horizontal_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 
@@ -71,13 +71,13 @@
                              int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_vertical_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_vertical_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_vertical_edge_mmx(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_mmx(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_mbloop_filter_vertical_edge_mmx(v_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, 1);
+        vp8_mbloop_filter_vertical_edge_mmx(v_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, 1);
 }
 
 
@@ -88,7 +88,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_vertical_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_vertical_edge_mmx(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 
@@ -102,10 +102,10 @@
     vp8_loop_filter_horizontal_edge_mmx(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_horizontal_edge_mmx(u_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_mmx(u_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_horizontal_edge_mmx(v_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_horizontal_edge_mmx(v_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 
@@ -132,10 +132,10 @@
     vp8_loop_filter_vertical_edge_mmx(y_ptr + 12, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_vertical_edge_mmx(u_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_mmx(u_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 
     if (v_ptr)
-        vp8_loop_filter_vertical_edge_mmx(v_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, 1);
+        vp8_loop_filter_vertical_edge_mmx(v_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, 1);
 }
 
 
@@ -159,10 +159,10 @@
                               int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_horizontal_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_horizontal_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_horizontal_edge_uv_sse2(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, v_ptr);
+        vp8_mbloop_filter_horizontal_edge_uv_sse2(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, v_ptr);
 }
 
 
@@ -173,7 +173,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_horizontal_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_horizontal_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 
@@ -182,10 +182,10 @@
                               int y_stride, int uv_stride, loop_filter_info *lfi, int simpler_lpf)
 {
     (void) simpler_lpf;
-    vp8_mbloop_filter_vertical_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_mbloop_filter_vertical_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_mbloop_filter_vertical_edge_uv_sse2(u_ptr, uv_stride, lfi->uvmbflim, lfi->uvlim, lfi->uvmbthr, v_ptr);
+        vp8_mbloop_filter_vertical_edge_uv_sse2(u_ptr, uv_stride, lfi->mbflim, lfi->lim, lfi->thr, v_ptr);
 }
 
 
@@ -196,7 +196,7 @@
     (void) v_ptr;
     (void) uv_stride;
     (void) simpler_lpf;
-    vp8_loop_filter_simple_vertical_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->mbthr, 2);
+    vp8_loop_filter_simple_vertical_edge_sse2(y_ptr, y_stride, lfi->mbflim, lfi->lim, lfi->thr, 2);
 }
 
 
@@ -210,7 +210,7 @@
     vp8_loop_filter_horizontal_edge_sse2(y_ptr + 12 * y_stride, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_horizontal_edge_uv_sse2(u_ptr + 4 * uv_stride, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, v_ptr + 4 * uv_stride);
+        vp8_loop_filter_horizontal_edge_uv_sse2(u_ptr + 4 * uv_stride, uv_stride, lfi->flim, lfi->lim, lfi->thr, v_ptr + 4 * uv_stride);
 }
 
 
@@ -237,7 +237,7 @@
     vp8_loop_filter_vertical_edge_sse2(y_ptr + 12, y_stride, lfi->flim, lfi->lim, lfi->thr, 2);
 
     if (u_ptr)
-        vp8_loop_filter_vertical_edge_uv_sse2(u_ptr + 4, uv_stride, lfi->uvflim, lfi->uvlim, lfi->uvthr, v_ptr + 4);
+        vp8_loop_filter_vertical_edge_uv_sse2(u_ptr + 4, uv_stride, lfi->flim, lfi->lim, lfi->thr, v_ptr + 4);
 }
 
 
--- a/vp8/decoder/threading.c
+++ b/vp8/decoder/threading.c
@@ -320,7 +320,7 @@
                              * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
                              * Apply any context driven MB level adjustment
                              */
-                            vp8_adjust_mb_lf_value(xd, &filter_level);
+                            filter_level = vp8_adjust_mb_lf_value(xd, filter_level);
                         }
 
                         /* Distance of Mb to the various image edges.
@@ -863,7 +863,7 @@
                      * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
                      * Apply any context driven MB level adjustment
                      */
-                    vp8_adjust_mb_lf_value(xd, &filter_level);
+                    filter_level = vp8_adjust_mb_lf_value(xd, filter_level);
                 }
 
                 /* Distance of Mb to the various image edges.
--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -407,13 +407,6 @@
     // Clear Zbin mode boost for default case
     cpi->zbin_mode_boost = 0;
 
-    // vp8cx_init_quantizer() is first called in vp8_create_compressor(). A check is added here so that vp8cx_init_quantizer() is only called
-    // when these values are not all zero.
-    if (cpi->common.y1dc_delta_q | cpi->common.y2dc_delta_q | cpi->common.uvdc_delta_q | cpi->common.y2ac_delta_q | cpi->common.uvac_delta_q)
-    {
-        vp8cx_init_quantizer(cpi);
-    }
-
     // MB level quantizer setup
     vp8cx_mb_init_quantizer(cpi, &cpi->mb);
 }
@@ -498,6 +491,16 @@
     int left_id, above_id;
     int sum;
 #endif
+#if CONFIG_MULTITHREAD
+    const int nsync = cpi->mt_sync_range;
+    const int rightmost_col = cm->mb_cols - 1;
+    volatile const int *last_row_current_mb_col;
+
+    if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
+        last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
+    else
+        last_row_current_mb_col = &rightmost_col;
+#endif
     // reset above block coeffs
     xd->above_context = cm->above_context;
 
@@ -542,6 +545,21 @@
         x->rddiv = cpi->RDDIV;
         x->rdmult = cpi->RDMULT;
 
+#if CONFIG_MULTITHREAD
+        if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
+        {
+            if ((mb_col & (nsync - 1)) == 0)
+            {
+                while (mb_col > (*last_row_current_mb_col - nsync)
+                        && (*last_row_current_mb_col) != (cm->mb_cols - 1))
+                {
+                    x86_pause_hint();
+                    thread_sleep(0);
+                }
+            }
+        }
+#endif
+
         if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
             activity_sum += vp8_activity_masking(cpi, x);
 
@@ -677,7 +695,12 @@
         x->partition_info++;
 
         xd->above_context++;
-        cpi->current_mb_col_main = mb_col;
+#if CONFIG_MULTITHREAD
+        if (cpi->b_multi_threaded != 0)
+        {
+            cpi->mt_current_mb_col[mb_row] = mb_col;
+        }
+#endif
     }
 
     //extend the recon for intra prediction
@@ -691,8 +714,13 @@
     xd->mode_info_context++;
     x->partition_info++;
     x->activity_sum += activity_sum;
+#if CONFIG_MULTITHREAD
+    if ((cpi->b_multi_threaded != 0) && (mb_row == cm->mb_rows - 1))
+    {
+        sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
+    }
+#endif
 }
-
 void vp8_encode_frame(VP8_COMP *cpi)
 {
     int mb_row;
@@ -851,22 +879,16 @@
 
             vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, 1,  cpi->encoding_thread_count);
 
-            for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
+            for (i = 0; i < cm->mb_rows; i++)
+                cpi->mt_current_mb_col[i] = 0;
+
+            for (i = 0; i < cpi->encoding_thread_count; i++)
             {
-                cpi->current_mb_col_main = -1;
+                sem_post(&cpi->h_event_start_encoding[i]);
+            }
 
-                for (i = 0; i < cpi->encoding_thread_count; i++)
-                {
-                    if ((mb_row + i + 1) >= cm->mb_rows)
-                        break;
-
-                    cpi->mb_row_ei[i].mb_row = mb_row + i + 1;
-                    cpi->mb_row_ei[i].tp  = cpi->tok + (mb_row + i + 1) * (cm->mb_cols * 16 * 24);
-                    cpi->mb_row_ei[i].current_mb_col = -1;
-                    //SetEvent(cpi->h_event_mbrencoding[i]);
-                    sem_post(&cpi->h_event_mbrencoding[i]);
-                }
-
+            for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
+            {
                 vp8_zero(cm->left_context)
 
                 tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24);
@@ -881,26 +903,10 @@
                 xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
                 x->partition_info  += xd->mode_info_stride * cpi->encoding_thread_count;
 
-                if (mb_row < cm->mb_rows - 1)
-                    //WaitForSingleObject(cpi->h_event_main, INFINITE);
-                    sem_wait(&cpi->h_event_main);
             }
 
-            /*
-            for( ;mb_row<cm->mb_rows; mb_row ++)
-            {
-            vp8_zero( cm->left_context)
+            sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */
 
-            tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24);
-
-            encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
-            // adjust to the next row of mbs
-            x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
-            x->src.u_buffer +=  8 * x->src.uv_stride - 8 * cm->mb_cols;
-            x->src.v_buffer +=  8 * x->src.uv_stride - 8 * cm->mb_cols;
-
-            }
-            */
             cpi->tok_count = 0;
 
             for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
@@ -910,7 +916,6 @@
 
             if (xd->segmentation_enabled)
             {
-
                 int i, j;
 
                 if (xd->segmentation_enabled)
@@ -922,7 +927,6 @@
                             segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j];
                     }
                 }
-
             }
 
             for (i = 0; i < cpi->encoding_thread_count; i++)
--- a/vp8/encoder/ethreading.c
+++ b/vp8/encoder/ethreading.c
@@ -8,15 +8,16 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-
 #include "onyx_int.h"
 #include "threading.h"
 #include "common.h"
 #include "extend.h"
 
-
-extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
-extern int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
+extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
+                                         TOKENEXTRA **t, int recon_yoffset,
+                                         int recon_uvoffset);
+extern int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x,
+                                          TOKENEXTRA **t);
 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
 extern void vp8_build_block_offsets(MACROBLOCK *x);
 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
@@ -26,10 +27,11 @@
 {
 #if CONFIG_MULTITHREAD
     int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
-    VP8_COMP *cpi   = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
+    VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
     MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
     ENTROPY_CONTEXT_PLANES mb_row_left_context;
 
+    const int nsync = cpi->mt_sync_range;
     //printf("Started thread %d\n", ithread);
 
     while (1)
@@ -38,210 +40,209 @@
             break;
 
         //if(WaitForSingleObject(cpi->h_event_mbrencoding[ithread], INFINITE) == WAIT_OBJECT_0)
-        if (sem_wait(&cpi->h_event_mbrencoding[ithread]) == 0)
+        if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0)
         {
+            VP8_COMMON *cm = &cpi->common;
+            int mb_row;
+            MACROBLOCK *x = &mbri->mb;
+            MACROBLOCKD *xd = &x->e_mbd;
+            TOKENEXTRA *tp ;
+
+            int *segment_counts = mbri->segment_counts;
+            int *totalrate = &mbri->totalrate;
+
             if (cpi->b_multi_threaded == FALSE) // we're shutting down
                 break;
-            else
+
+            for (mb_row = ithread + 1; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
             {
-                VP8_COMMON *cm      = &cpi->common;
-                int mb_row           = mbri->mb_row;
-                MACROBLOCK  *x      = &mbri->mb;
-                MACROBLOCKD *xd     = &x->e_mbd;
-                TOKENEXTRA **tp     = &mbri->tp;
-                int *segment_counts  = mbri->segment_counts;
-                int *totalrate      = &mbri->totalrate;
 
-                {
-                    int i;
-                    int recon_yoffset, recon_uvoffset;
-                    int mb_col;
-                    int ref_fb_idx = cm->lst_fb_idx;
-                    int dst_fb_idx = cm->new_fb_idx;
-                    int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
-                    int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
-                    volatile int *last_row_current_mb_col;
-                    INT64 activity_sum = 0;
+                int i;
+                int recon_yoffset, recon_uvoffset;
+                int mb_col;
+                int ref_fb_idx = cm->lst_fb_idx;
+                int dst_fb_idx = cm->new_fb_idx;
+                int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
+                int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
+                volatile int *last_row_current_mb_col;
+                INT64 activity_sum = 0;
 
-                    if (ithread > 0)
-                        last_row_current_mb_col = &cpi->mb_row_ei[ithread-1].current_mb_col;
-                    else
-                        last_row_current_mb_col = &cpi->current_mb_col_main;
+                tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
 
-                    // reset above block coeffs
-                    xd->above_context = cm->above_context;
-                    xd->left_context = &mb_row_left_context;
+                last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
 
-                    vp8_zero(mb_row_left_context);
+                // reset above block coeffs
+                xd->above_context = cm->above_context;
+                xd->left_context = &mb_row_left_context;
 
-                    xd->up_available = (mb_row != 0);
-                    recon_yoffset = (mb_row * recon_y_stride * 16);
-                    recon_uvoffset = (mb_row * recon_uv_stride * 8);
+                vp8_zero(mb_row_left_context);
 
+                xd->up_available = (mb_row != 0);
+                recon_yoffset = (mb_row * recon_y_stride * 16);
+                recon_uvoffset = (mb_row * recon_uv_stride * 8);
 
-                    cpi->tplist[mb_row].start = *tp;
+                cpi->tplist[mb_row].start = tp;
 
-                    //printf("Thread mb_row = %d\n", mb_row);
+                //printf("Thread mb_row = %d\n", mb_row);
 
-                    // for each macroblock col in image
-                    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
-                    {
-                        int seg_map_index = (mb_row * cm->mb_cols);
+                // for each macroblock col in image
+                for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
+                {
+                    int seg_map_index = (mb_row * cm->mb_cols);
    
-                        while (mb_col > (*last_row_current_mb_col - 1) && *last_row_current_mb_col != cm->mb_cols - 1)
+                    if ((mb_col & (nsync - 1)) == 0)
+                    {
+                        while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != cm->mb_cols - 1)
                         {
                             x86_pause_hint();
                             thread_sleep(0);
                         }
+                    }
 
-                        // Distance of Mb to the various image edges.
-                        // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
-                        xd->mb_to_left_edge = -((mb_col * 16) << 3);
-                        xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
-                        xd->mb_to_top_edge = -((mb_row * 16) << 3);
-                        xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
+                    // Distance of Mb to the various image edges.
+                    // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
+                    xd->mb_to_left_edge = -((mb_col * 16) << 3);
+                    xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
+                    xd->mb_to_top_edge = -((mb_row * 16) << 3);
+                    xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
 
-                        // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
-                        x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
-                        x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
-                        x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
-                        x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
+                    // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
+                    x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
+                    x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
+                    x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
+                    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
 
-                        xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
-                        xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
-                        xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
-                        xd->left_available = (mb_col != 0);
+                    xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
+                    xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
+                    xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
+                    xd->left_available = (mb_col != 0);
 
-                        x->rddiv = cpi->RDDIV;
-                        x->rdmult = cpi->RDMULT;
+                    x->rddiv = cpi->RDDIV;
+                    x->rdmult = cpi->RDMULT;
 
-                        if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
-                            activity_sum += vp8_activity_masking(cpi, x);
+                    if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
+                        activity_sum += vp8_activity_masking(cpi, x);
 
-                        // Is segmentation enabled
-                        // MB level adjutment to quantizer
-                        if (xd->segmentation_enabled)
-                        {
-                            // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
-                            if (cpi->segmentation_map[seg_map_index+mb_col] <= 3)
-                                xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[seg_map_index+mb_col];
-                            else
-                                xd->mode_info_context->mbmi.segment_id = 0;
-
-                            vp8cx_mb_init_quantizer(cpi, x);
-                        }
+                    // Is segmentation enabled
+                    // MB level adjutment to quantizer
+                    if (xd->segmentation_enabled)
+                    {
+                        // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
+                        if (cpi->segmentation_map[seg_map_index + mb_col] <= 3)
+                            xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[seg_map_index + mb_col];
                         else
-                            xd->mode_info_context->mbmi.segment_id = 0;         // Set to Segment 0 by default
+                            xd->mode_info_context->mbmi.segment_id = 0;
 
-                        x->active_ptr = cpi->active_map + seg_map_index + mb_col;
+                        vp8cx_mb_init_quantizer(cpi, x);
+                    }
+                    else
+                        xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
 
-                        if (cm->frame_type == KEY_FRAME)
-                        {
-                            *totalrate += vp8cx_encode_intra_macro_block(cpi, x, tp);
+                    x->active_ptr = cpi->active_map + seg_map_index + mb_col;
+
+                    if (cm->frame_type == KEY_FRAME)
+                    {
+                        *totalrate += vp8cx_encode_intra_macro_block(cpi, x, &tp);
 #ifdef MODE_STATS
-                            y_modes[xd->mbmi.mode] ++;
+                        y_modes[xd->mbmi.mode] ++;
 #endif
-                        }
-                        else
-                        {
-                            *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset);
+                    }
+                    else
+                    {
+                        *totalrate += vp8cx_encode_inter_macroblock(cpi, x, &tp, recon_yoffset, recon_uvoffset);
 
 #ifdef MODE_STATS
-                            inter_y_modes[xd->mbmi.mode] ++;
+                        inter_y_modes[xd->mbmi.mode] ++;
 
-                            if (xd->mbmi.mode == SPLITMV)
-                            {
-                                int b;
+                        if (xd->mbmi.mode == SPLITMV)
+                        {
+                            int b;
 
-                                for (b = 0; b < xd->mbmi.partition_count; b++)
-                                {
-                                    inter_b_modes[x->partition->bmi[b].mode] ++;
-                                }
+                            for (b = 0; b < xd->mbmi.partition_count; b++)
+                            {
+                                inter_b_modes[x->partition->bmi[b].mode] ++;
                             }
+                        }
 
 #endif
 
-                            // Count of last ref frame 0,0 useage
-                            if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
-                                cpi->inter_zz_count ++;
+                        // Count of last ref frame 0,0 useage
+                        if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
+                            cpi->inter_zz_count++;
 
-                            // Special case code for cyclic refresh
-                            // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
-                            // during vp8cx_encode_inter_macroblock()) back into the global sgmentation map
-                            if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
-                            {
-                                cpi->segmentation_map[seg_map_index+mb_col] = xd->mode_info_context->mbmi.segment_id;
+                        // Special case code for cyclic refresh
+                        // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
+                        // during vp8cx_encode_inter_macroblock()) back into the global sgmentation map
+                        if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
+                        {
+                            const MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
+                            cpi->segmentation_map[seg_map_index + mb_col] = mbmi->segment_id;
 
-                                // If the block has been refreshed mark it as clean (the magnitude of the -ve influences how long it will be before we consider another refresh):
-                                // Else if it was coded (last frame 0,0) and has not already been refreshed then mark it as a candidate for cleanup next time (marked 0)
-                                // else mark it as dirty (1).
-                                if (xd->mode_info_context->mbmi.segment_id)
-                                    cpi->cyclic_refresh_map[seg_map_index+mb_col] = -1;
-                                else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
-                                {
-                                    if (cpi->cyclic_refresh_map[seg_map_index+mb_col] == 1)
-                                        cpi->cyclic_refresh_map[seg_map_index+mb_col] = 0;
-                                }
-                                else
-                                    cpi->cyclic_refresh_map[seg_map_index+mb_col] = 1;
-
+                            // If the block has been refreshed mark it as clean (the magnitude of the -ve influences how long it will be before we consider another refresh):
+                            // Else if it was coded (last frame 0,0) and has not already been refreshed then mark it as a candidate for cleanup next time (marked 0)
+                            // else mark it as dirty (1).
+                            if (mbmi->segment_id)
+                                cpi->cyclic_refresh_map[seg_map_index + mb_col] = -1;
+                            else if ((mbmi->mode == ZEROMV) && (mbmi->ref_frame == LAST_FRAME))
+                            {
+                                if (cpi->cyclic_refresh_map[seg_map_index + mb_col] == 1)
+                                    cpi->cyclic_refresh_map[seg_map_index + mb_col] = 0;
                             }
+                            else
+                                cpi->cyclic_refresh_map[seg_map_index + mb_col] = 1;
+
                         }
-                        cpi->tplist[mb_row].stop = *tp;
+                    }
+                    cpi->tplist[mb_row].stop = tp;
 
-                        x->gf_active_ptr++;      // Increment pointer into gf useage flags structure for next mb
+                    x->gf_active_ptr++; // Increment pointer into gf useage flags structure for next mb
 
-                        for (i = 0; i < 16; i++)
-                            vpx_memcpy(&xd->mode_info_context->bmi[i], &xd->block[i].bmi, sizeof(xd->block[i].bmi));
+                    for (i = 0; i < 16; i++)
+                        vpx_memcpy(&xd->mode_info_context->bmi[i], &xd->block[i].bmi, sizeof(xd->block[i].bmi));
 
-                        // adjust to the next column of macroblocks
-                        x->src.y_buffer += 16;
-                        x->src.u_buffer += 8;
-                        x->src.v_buffer += 8;
+                    // adjust to the next column of macroblocks
+                    x->src.y_buffer += 16;
+                    x->src.u_buffer += 8;
+                    x->src.v_buffer += 8;
 
-                        recon_yoffset += 16;
-                        recon_uvoffset += 8;
+                    recon_yoffset += 16;
+                    recon_uvoffset += 8;
 
-                        // Keep track of segment useage
-                        segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
+                    // Keep track of segment useage
+                    segment_counts[xd->mode_info_context->mbmi.segment_id]++;
 
-                        // skip to next mb
-                        xd->mode_info_context++;
-                        x->partition_info++;
-
-                        xd->above_context++;
-
-                        cpi->mb_row_ei[ithread].current_mb_col = mb_col;
-
-                    }
-
-                    //extend the recon for intra prediction
-                    vp8_extend_mb_row(
-                        &cm->yv12_fb[dst_fb_idx],
-                        xd->dst.y_buffer + 16,
-                        xd->dst.u_buffer + 8,
-                        xd->dst.v_buffer + 8);
-
-                    // this is to account for the border
+                    // skip to next mb
                     xd->mode_info_context++;
                     x->partition_info++;
-                    x->activity_sum += activity_sum;
+                    xd->above_context++;
 
-                    x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
-                    x->src.u_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
-                    x->src.v_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
+                    cpi->mt_current_mb_col[mb_row] = mb_col;
+                }
 
-                    xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
-                    x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
+                //extend the recon for intra prediction
+                vp8_extend_mb_row(
+                    &cm->yv12_fb[dst_fb_idx],
+                    xd->dst.y_buffer + 16,
+                    xd->dst.u_buffer + 8,
+                    xd->dst.v_buffer + 8);
 
-                    if (ithread == (cpi->encoding_thread_count - 1) || mb_row == cm->mb_rows - 1)
-                    {
-                        //SetEvent(cpi->h_event_main);
-                        sem_post(&cpi->h_event_main);
-                    }
+                // this is to account for the border
+                xd->mode_info_context++;
+                x->partition_info++;
+                x->activity_sum += activity_sum;
 
-                }
+                x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
+                x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
+                x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
 
+                xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
+                x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
+
+                if (mb_row == cm->mb_rows - 1)
+                {
+                    //SetEvent(cpi->h_event_main);
+                    sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
+                }
             }
         }
     }
@@ -363,7 +364,6 @@
     }
 }
 
-
 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
                                MACROBLOCK *x,
                                MB_ROW_COMP *mbr_ei,
@@ -414,7 +414,6 @@
         mb->src.u_buffer +=  8 * x->src.uv_stride * (i + 1);
         mb->src.v_buffer +=  8 * x->src.uv_stride * (i + 1);
 
-
         vp8_build_block_offsets(mb);
 
         vp8_setup_block_dptrs(mbd);
@@ -431,7 +430,6 @@
     }
 }
 
-
 void vp8cx_create_encoder_threads(VP8_COMP *cpi)
 {
     cpi->b_multi_threaded = 0;
@@ -451,14 +449,15 @@
         else
             cpi->encoding_thread_count = cpi->oxcf.multi_threaded - 1;
 
-
         CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * cpi->encoding_thread_count));
-        CHECK_MEM_ERROR(cpi->h_event_mbrencoding, vpx_malloc(sizeof(sem_t) * cpi->encoding_thread_count));
+        CHECK_MEM_ERROR(cpi->h_event_start_encoding, vpx_malloc(sizeof(sem_t) * cpi->encoding_thread_count));
         CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count));
         vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count);
         CHECK_MEM_ERROR(cpi->en_thread_data, vpx_malloc(sizeof(ENCODETHREAD_DATA) * cpi->encoding_thread_count));
+        CHECK_MEM_ERROR(cpi->mt_current_mb_col, vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cpi->common.mb_rows));
+
         //cpi->h_event_main = CreateEvent(NULL, FALSE, FALSE, NULL);
-        sem_init(&cpi->h_event_main, 0, 0);
+        sem_init(&cpi->h_event_end_encoding, 0, 0);
 
         cpi->b_multi_threaded = 1;
 
@@ -466,11 +465,13 @@
 
         for (ithread = 0; ithread < cpi->encoding_thread_count; ithread++)
         {
+            ENCODETHREAD_DATA * ethd = &cpi->en_thread_data[ithread];
+
             //cpi->h_event_mbrencoding[ithread] = CreateEvent(NULL, FALSE, FALSE, NULL);
-            sem_init(&cpi->h_event_mbrencoding[ithread], 0, 0);
-            cpi->en_thread_data[ithread].ithread = ithread;
-            cpi->en_thread_data[ithread].ptr1 = (void *)cpi;
-            cpi->en_thread_data[ithread].ptr2 = (void *)&cpi->mb_row_ei[ithread];
+            sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
+            ethd->ithread = ithread;
+            ethd->ptr1 = (void *)cpi;
+            ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
 
             //printf(" call begin thread %d \n", ithread);
 
@@ -482,8 +483,7 @@
             //  0,
             //  NULL);
 
-            pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, (&cpi->en_thread_data[ithread]));
-
+            pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, ethd);
         }
 
     }
@@ -505,18 +505,21 @@
             for (i = 0; i < cpi->encoding_thread_count; i++)
             {
                 //SetEvent(cpi->h_event_mbrencoding[i]);
-                sem_post(&cpi->h_event_mbrencoding[i]);
+                sem_post(&cpi->h_event_start_encoding[i]);
                 pthread_join(cpi->h_encoding_thread[i], 0);
-            }
 
-            for (i = 0; i < cpi->encoding_thread_count; i++)
-                sem_destroy(&cpi->h_event_mbrencoding[i]);
+                sem_destroy(&cpi->h_event_start_encoding[i]);
+            }
         }
+
+        sem_destroy(&cpi->h_event_end_encoding);
+
         //free thread related resources
-        vpx_free(cpi->h_event_mbrencoding);
+        vpx_free(cpi->h_event_start_encoding);
         vpx_free(cpi->h_encoding_thread);
         vpx_free(cpi->mb_row_ei);
         vpx_free(cpi->en_thread_data);
+        vpx_free(cpi->mt_current_mb_col);
     }
 
 #endif
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -1531,6 +1531,17 @@
         vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
                            "Failed to allocate firstpass stats");
 #endif
+
+#if CONFIG_MULTITHREAD
+    if (width < 640)
+        cpi->mt_sync_range = 1;
+    else if (width <= 1280)
+        cpi->mt_sync_range = 4;
+    else if (width <= 2560)
+        cpi->mt_sync_range = 8;
+    else
+        cpi->mt_sync_range = 16;
+#endif
 }
 
 
@@ -3177,11 +3188,14 @@
 
     return 1;
 }
+
 static void set_quantizer(VP8_COMP *cpi, int Q)
 {
     VP8_COMMON *cm = &cpi->common;
     MACROBLOCKD *mbd = &cpi->mb.e_mbd;
+    int update = 0;
 
+    update |= cm->base_qindex != Q;
     cm->base_qindex = Q;
 
     cm->y1dc_delta_q = 0;
@@ -3192,13 +3206,19 @@
 
     if(Q<4)
     {
+        update |= cm->y2dc_delta_q != 4-Q;
         cm->y2dc_delta_q = 4-Q;
     }
+
     // Set Segment specific quatizers
     mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
     mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
     mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
     mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
+
+    if(update)
+        vp8cx_init_quantizer(cpi);
+
 }
 
 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
@@ -3678,6 +3698,17 @@
     cpi->mb.e_mbd.update_mb_segmentation_map = 1;
 #endif
 
+#if CONFIG_REALTIME_ONLY
+    if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
+    {
+        if(cpi->force_next_frame_intra)
+        {
+            cm->frame_type = KEY_FRAME;  /* delayed intra frame */
+        }
+    }
+    cpi->force_next_frame_intra = 0;
+#endif
+
     // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
 #if !(CONFIG_REALTIME_ONLY)
 
@@ -4198,6 +4229,14 @@
         // (assuming that we didn't)!
         if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
         {
+
+#if CONFIG_REALTIME_ONLY
+            {
+                /* we don't do re-encoding in realtime mode
+                 * if key frame is decided than we force it on next frame */
+                cpi->force_next_frame_intra = decide_key_frame(cpi);
+            }
+#else
             if (decide_key_frame(cpi))
             {
                 vp8_calc_auto_iframe_target_size(cpi);
@@ -4236,6 +4275,7 @@
                 resize_key_frame(cpi);
                 continue;
             }
+#endif
         }
 
         vp8_clear_system_state();
@@ -4498,7 +4538,7 @@
     }
 
     // Update the GF useage maps.
-    // Update the GF useage maps.
+    // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
     // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
     vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
 
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -191,8 +191,6 @@
 typedef struct
 {
     MACROBLOCK  mb;
-    int mb_row;
-    TOKENEXTRA *tp;
 #if CONFIG_SEGMENTATION
     int segment_counts[MAX_MB_SEGMENTS + 8];
 #else
@@ -199,7 +197,6 @@
     int segment_counts[MAX_MB_SEGMENTS];
 #endif
     int totalrate;
-    int current_mb_col;
 } MB_ROW_COMP;
 
 typedef struct
@@ -597,7 +594,8 @@
     signed char *cyclic_refresh_map;
 
     // multithread data
-    int current_mb_col_main;
+    int * mt_current_mb_col;
+    int mt_sync_range;
     int processor_core_count;
     int b_multi_threaded;
     int encoding_thread_count;
@@ -610,8 +608,8 @@
 
 #if CONFIG_MULTITHREAD
     //events
-    sem_t *h_event_mbrencoding;
-    sem_t h_event_main;
+    sem_t *h_event_start_encoding;
+    sem_t h_event_end_encoding;
 #endif
 
     TOKENLIST *tplist;
@@ -683,6 +681,9 @@
     int *lf_ref_frame_sign_bias;
     int *lf_ref_frame;
 
+#if CONFIG_REALTIME_ONLY
+    int force_next_frame_intra; /* force next frame to intra when kf_auto says so */
+#endif
 } VP8_COMP;
 
 void control_data_rate(VP8_COMP *cpi);
--- a/vp8/encoder/pickinter.c
+++ b/vp8/encoder/pickinter.c
@@ -24,7 +24,7 @@
 #include "g_common.h"
 #include "variance.h"
 #include "mcomp.h"
-
+#include "rdopt.h"
 #include "vpx_mem/vpx_mem.h"
 
 #if CONFIG_RUNTIME_CPU_DETECT
@@ -412,7 +412,6 @@
 
 }
 
-
 int vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
 {
     BLOCK *b = &x->block[0];
@@ -421,7 +420,7 @@
     B_MODE_INFO best_bmodes[16];
     MB_MODE_INFO best_mbmode;
     PARTITION_INFO best_partition;
-    MV best_ref_mv1;
+    MV best_ref_mv;
     MV mode_mv[MB_MODE_COUNT];
     MB_PREDICTION_MODE this_mode;
     int num00;
@@ -439,9 +438,14 @@
     int best_mode_index = 0;
     int sse = INT_MAX;
 
+    MV mvp;
+    int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
+    int saddone=0;
+    int sr=0;    //search range got from mv_pred(). It uses step_param levels. (0-7)
+
     MV nearest_mv[4];
     MV near_mv[4];
-    MV best_ref_mv[4];
+    MV frame_best_ref_mv[4];
     int MDCounts[4][4];
     unsigned char *y_buffer[4];
     unsigned char *u_buffer[4];
@@ -461,7 +465,7 @@
         YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
 
         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[LAST_FRAME], &near_mv[LAST_FRAME],
-                          &best_ref_mv[LAST_FRAME], MDCounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
+                          &frame_best_ref_mv[LAST_FRAME], MDCounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
 
         y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
         u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
@@ -475,7 +479,7 @@
         YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
 
         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[GOLDEN_FRAME], &near_mv[GOLDEN_FRAME],
-                          &best_ref_mv[GOLDEN_FRAME], MDCounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
+                          &frame_best_ref_mv[GOLDEN_FRAME], MDCounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
 
         y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
         u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
@@ -489,7 +493,7 @@
         YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
 
         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[ALTREF_FRAME], &near_mv[ALTREF_FRAME],
-                          &best_ref_mv[ALTREF_FRAME], MDCounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
+                          &frame_best_ref_mv[ALTREF_FRAME], MDCounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
 
         y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
         u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
@@ -529,10 +533,6 @@
                                         + vp8_cost_one(cpi->prob_gf_coded);
     }
 
-
-
-    best_rd = INT_MAX;
-
     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
 
     // if we encode a new mv this is important
@@ -595,7 +595,7 @@
             x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
             mode_mv[NEARESTMV] = nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
             mode_mv[NEARMV] = near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
-            best_ref_mv1 = best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
+            best_ref_mv = frame_best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
             memcpy(mdcounts, MDCounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
         }
 
@@ -608,6 +608,28 @@
                 continue;
         }
 
+        if(x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
+        {
+            if(!saddone)
+            {
+                vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
+                saddone = 1;
+            }
+
+            vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
+                        x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
+
+            /* adjust mvp to make sure it is within MV range */
+            if(mvp.row > best_ref_mv.row + MAX_FULL_PEL_VAL)
+                mvp.row = best_ref_mv.row + MAX_FULL_PEL_VAL;
+            else if(mvp.row < best_ref_mv.row - MAX_FULL_PEL_VAL)
+                mvp.row = best_ref_mv.row - MAX_FULL_PEL_VAL;
+            if(mvp.col > best_ref_mv.col + MAX_FULL_PEL_VAL)
+                mvp.col = best_ref_mv.col + MAX_FULL_PEL_VAL;
+            else if(mvp.col < best_ref_mv.col - MAX_FULL_PEL_VAL)
+                mvp.col = best_ref_mv.col - MAX_FULL_PEL_VAL;
+        }
+
         switch (this_mode)
         {
         case B_PRED:
@@ -663,61 +685,46 @@
             int n = 0;
             int sadpb = x->sadperbit16;
 
-            // Further step/diamond searches as necessary
-            if (cpi->Speed < 8)
-            {
-                step_param = cpi->sf.first_step + ((cpi->Speed > 5) ? 1 : 0);
-                further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
-            }
-            else
-            {
-                step_param = cpi->sf.first_step + 2;
-                further_steps = 0;
-            }
+            int col_min = (best_ref_mv.col - MAX_FULL_PEL_VAL) >>3;
+            int col_max = (best_ref_mv.col + MAX_FULL_PEL_VAL) >>3;
+            int row_min = (best_ref_mv.row - MAX_FULL_PEL_VAL) >>3;
+            int row_max = (best_ref_mv.row + MAX_FULL_PEL_VAL) >>3;
 
-#if 0
+            int tmp_col_min = x->mv_col_min;
+            int tmp_col_max = x->mv_col_max;
+            int tmp_row_min = x->mv_row_min;
+            int tmp_row_max = x->mv_row_max;
 
-            // Initial step Search
-            bestsme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, cpi->mb.mvcost, &best_ref_mv1);
-            mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
-            mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
+            // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
+            if (x->mv_col_min < col_min )
+                x->mv_col_min = col_min;
+            if (x->mv_col_max > col_max )
+                x->mv_col_max = col_max;
+            if (x->mv_row_min < row_min )
+                x->mv_row_min = row_min;
+            if (x->mv_row_max > row_max )
+                x->mv_row_max = row_max;
 
-            // Further step searches
-            while (n < further_steps)
+            // Further step/diamond searches as necessary
             {
-                n++;
-
-                if (num00)
-                    num00--;
-                else
-                {
-                    thissme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, x->mvcost, &best_ref_mv1);
-
-                    if (thissme < bestsme)
-                    {
-                        bestsme = thissme;
-                        mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
-                        mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
-                    }
-                    else
-                    {
-                        d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
-                        d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
-                    }
-                }
+                int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
+                step_param = cpi->sf.first_step + speed_adjust;
+                sr += speed_adjust;
+                //adjust search range according to sr from mv prediction
+                if(sr > step_param)
+                    step_param = sr;
+                further_steps = (cpi->Speed >= 8)? 0: (cpi->sf.max_step_search_steps - 1 - step_param);
             }
 
-#else
-
             if (cpi->sf.search_method == HEX)
             {
-                bestsme = vp8_hex_search(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost);
+                bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost);
                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
             }
             else
             {
-                bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv1); //sadpb < 9
+                bestsme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv); //sadpb < 9
                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
 
@@ -736,7 +743,7 @@
                         num00--;
                     else
                     {
-                        thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv1); //sadpb = 9
+                        thissme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv); //sadpb = 9
 
                         if (thissme < bestsme)
                         {
@@ -753,18 +760,20 @@
                 }
             }
 
-#endif
-        }
+            x->mv_col_min = tmp_col_min;
+            x->mv_col_max = tmp_col_max;
+            x->mv_row_min = tmp_row_min;
+            x->mv_row_max = tmp_row_max;
 
-        if (bestsme < INT_MAX)
-            cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv1, x->errorperbit, &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost);
+            if (bestsme < INT_MAX)
+                cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv, x->errorperbit, &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost);
 
-        mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
-        mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
+            mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
+            mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
 
-        // mv cost;
-        rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv1, cpi->mb.mvcost, 128);
-
+            // mv cost;
+            rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, cpi->mb.mvcost, 128);
+        }
 
         case NEARESTMV:
         case NEARMV:
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -1716,6 +1716,61 @@
     vp8_clamp_mv(mvp, xd);
 }
 
+void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
+{
+
+    int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
+
+    //calculate sad for current frame 3 nearby MBs.
+    if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
+    {
+        near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
+    }else if(xd->mb_to_top_edge==0)
+    {   //only has left MB for sad calculation.
+        near_sad[0] = near_sad[2] = INT_MAX;
+        near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
+    }else if(xd->mb_to_left_edge ==0)
+    {   //only has left MB for sad calculation.
+        near_sad[1] = near_sad[2] = INT_MAX;
+        near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
+    }else
+    {
+        near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
+        near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
+        near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
+    }
+
+    if(cpi->common.last_frame_type != KEY_FRAME)
+    {
+        //calculate sad for last frame 5 nearby MBs.
+        unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
+        int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
+
+        if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
+        if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
+        if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
+        if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
+
+        if(near_sad[4] != INT_MAX)
+            near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
+        if(near_sad[5] != INT_MAX)
+            near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
+        near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
+        if(near_sad[6] != INT_MAX)
+            near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
+        if(near_sad[7] != INT_MAX)
+            near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
+    }
+
+    if(cpi->common.last_frame_type != KEY_FRAME)
+    {
+        quicksortsad(near_sad, near_sadidx, 0, 7);
+    }else
+    {
+        quicksortsad(near_sad, near_sadidx, 0, 2);
+    }
+}
+
 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
 {
     BLOCK *b = &x->block[0];
@@ -1753,7 +1808,6 @@
     int force_no_skip = 0;
 
     MV mvp;
-    int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
     int saddone=0;
     int sr=0;    //search range got from mv_pred(). It uses step_param levels. (0-7)
@@ -1900,60 +1954,11 @@
             lf_or_gf = frame_lf_or_gf[x->e_mbd.mode_info_context->mbmi.ref_frame];
         }
 
-
         if(x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
         {
             if(!saddone)
             {
-                //calculate sad for current frame 3 nearby MBs.
-                if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
-                {
-                    near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
-                }else if(xd->mb_to_top_edge==0)
-                {   //only has left MB for sad calculation.
-                    near_sad[0] = near_sad[2] = INT_MAX;
-                    near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
-                }else if(xd->mb_to_left_edge ==0)
-                {   //only has left MB for sad calculation.
-                    near_sad[1] = near_sad[2] = INT_MAX;
-                    near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
-                }else
-                {
-                    near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
-                    near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
-                    near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
-                }
-
-                if(cpi->common.last_frame_type != KEY_FRAME)
-                {
-                    //calculate sad for last frame 5 nearby MBs.
-                    unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
-                    int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
-
-                    if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
-                    if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
-                    if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
-                    if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
-
-                    if(near_sad[4] != INT_MAX)
-                        near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
-                    if(near_sad[5] != INT_MAX)
-                        near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
-                    near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
-                    if(near_sad[6] != INT_MAX)
-                        near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
-                    if(near_sad[7] != INT_MAX)
-                        near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
-                }
-
-                if(cpi->common.last_frame_type != KEY_FRAME)
-                {
-                    quicksortsad(near_sad, near_sadidx, 0, 7);
-                }else
-                {
-                    quicksortsad(near_sad, near_sadidx, 0, 2);
-                }
-
+                vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
                 saddone = 1;
             }
 
@@ -2018,15 +2023,9 @@
         switch (this_mode)
         {
         case B_PRED:
-
-            for (i = 0; i < 16; i++)
-            {
-                vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
-            }
             // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
             vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
             rate2 += rate;
-
             distortion2 += distortion;
             rate2 += uv_intra_rate;
             rate_uv = uv_intra_rate_tokenonly;
@@ -2068,22 +2067,16 @@
         case V_PRED:
         case H_PRED:
         case TM_PRED:
-            for (i = 0; i < 16; i++)
-            {
-                vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
-            }
             x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
             vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
-            {
-                macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
-                rate2 += rate_y;
-                distortion2 += distortion;
-                rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
-                rate2 += uv_intra_rate;
-                rate_uv = uv_intra_rate_tokenonly;
-                distortion2 += uv_intra_distortion;
-                distortion_uv = uv_intra_distortion;
-            }
+            macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
+            rate2 += rate_y;
+            distortion2 += distortion;
+            rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
+            rate2 += uv_intra_rate;
+            rate_uv = uv_intra_rate_tokenonly;
+            distortion2 += uv_intra_distortion;
+            distortion_uv = uv_intra_distortion;
             break;
 
         case NEWMV:
@@ -2525,6 +2518,15 @@
         return best_rd;
     }
 
+
+    if(best_mbmode.mode <= B_PRED)
+    {
+        int i;
+        for (i = 0; i < 16; i++)
+        {
+            best_bmodes[i].mv.as_int = 0;
+        }
+    }
 
     // macroblock modes
     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
--- a/vp8/encoder/rdopt.h
+++ b/vp8/encoder/rdopt.h
@@ -17,5 +17,17 @@
 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_to, int *distortion);
 extern int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra);
 
+extern void vp8_mv_pred
+(
+    VP8_COMP *cpi,
+    MACROBLOCKD *xd,
+    const MODE_INFO *here,
+    MV *mvp,
+    int refframe,
+    int *ref_frame_sign_bias,
+    int *sr,
+    int near_sadidx[]
+);
+void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[]);
 
 #endif
--- a/vpx/internal/vpx_codec_internal.h
+++ b/vpx/internal/vpx_codec_internal.h
@@ -321,7 +321,7 @@
     {
         vpx_codec_put_frame_cb_fn_t    put_frame;
         vpx_codec_put_slice_cb_fn_t    put_slice;
-    };
+    } u;
     void                            *user_priv;
 } vpx_codec_priv_cb_pair_t;
 
--- a/vpx/src/vpx_decoder.c
+++ b/vpx/src/vpx_decoder.c
@@ -160,7 +160,7 @@
         res = VPX_CODEC_ERROR;
     else
     {
-        ctx->priv->dec.put_frame_cb.put_frame = cb;
+        ctx->priv->dec.put_frame_cb.u.put_frame = cb;
         ctx->priv->dec.put_frame_cb.user_priv = user_priv;
         res = VPX_CODEC_OK;
     }
@@ -182,7 +182,7 @@
         res = VPX_CODEC_ERROR;
     else
     {
-        ctx->priv->dec.put_slice_cb.put_slice = cb;
+        ctx->priv->dec.put_slice_cb.u.put_slice = cb;
         ctx->priv->dec.put_slice_cb.user_priv = user_priv;
         res = VPX_CODEC_OK;
     }