Merge "configure: add --enable-static option"
diff --git a/vp8/common/onyx.h b/vp8/common/onyx.h
index 545798a..015b4c4 100644
--- a/vp8/common/onyx.h
+++ b/vp8/common/onyx.h
@@ -211,8 +211,8 @@
 
 // receive a frames worth of data caller can assume that a copy of this frame is made
 // and not just a copy of the pointer..
-    int vp8_receive_raw_frame(VP8_PTR comp, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time_stamp);
-    int vp8_get_compressed_data(VP8_PTR comp, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, INT64 *time_stamp, INT64 *time_end, int flush);
+    int vp8_receive_raw_frame(VP8_PTR comp, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time_stamp);
+    int vp8_get_compressed_data(VP8_PTR comp, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, int64_t *time_stamp, int64_t *time_end, int flush);
     int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags);
 
     int vp8_use_as_reference(VP8_PTR comp, int ref_frame_flags);
diff --git a/vp8/common/onyxd.h b/vp8/common/onyxd.h
index 2dfdc66..d3e5c2f 100644
--- a/vp8/common/onyxd.h
+++ b/vp8/common/onyxd.h
@@ -54,8 +54,8 @@
 
     int vp8dx_get_setting(VP8D_PTR comp, VP8D_SETTING oxst);
 
-    int vp8dx_receive_compressed_data(VP8D_PTR comp, unsigned long size, const unsigned char *dest, INT64 time_stamp);
-    int vp8dx_get_raw_frame(VP8D_PTR comp, YV12_BUFFER_CONFIG *sd, INT64 *time_stamp, INT64 *time_end_stamp, vp8_ppflags_t *flags);
+    int vp8dx_receive_compressed_data(VP8D_PTR comp, unsigned long size, const unsigned char *dest, int64_t time_stamp);
+    int vp8dx_get_raw_frame(VP8D_PTR comp, YV12_BUFFER_CONFIG *sd, int64_t *time_stamp, int64_t *time_end_stamp, vp8_ppflags_t *flags);
 
     vpx_codec_err_t vp8dx_get_reference(VP8D_PTR comp, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd);
     vpx_codec_err_t vp8dx_set_reference(VP8D_PTR comp, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd);
diff --git a/vp8/decoder/asm_dec_offsets.c b/vp8/decoder/asm_dec_offsets.c
index dd2aad2..1a6090b 100644
--- a/vp8/decoder/asm_dec_offsets.c
+++ b/vp8/decoder/asm_dec_offsets.c
@@ -17,7 +17,6 @@
 DEFINE(detok_scan,                              offsetof(DETOK, scan));
 DEFINE(detok_ptr_block2leftabove,               offsetof(DETOK, ptr_block2leftabove));
 DEFINE(detok_coef_tree_ptr,                     offsetof(DETOK, vp8_coef_tree_ptr));
-DEFINE(detok_teb_base_ptr,                      offsetof(DETOK, teb_base_ptr));
 DEFINE(detok_norm_ptr,                          offsetof(DETOK, norm_ptr));
 DEFINE(detok_ptr_coef_bands_x,                  offsetof(DETOK, ptr_coef_bands_x));
 
@@ -35,9 +34,6 @@
 DEFINE(bool_decoder_count,                      offsetof(BOOL_DECODER, count));
 DEFINE(bool_decoder_range,                      offsetof(BOOL_DECODER, range));
 
-DEFINE(tokenextrabits_min_val,                  offsetof(TOKENEXTRABITS, min_val));
-DEFINE(tokenextrabits_length,                   offsetof(TOKENEXTRABITS, Length));
-
 END
 
 /* add asserts for any offset that is not supported by assembly code */
diff --git a/vp8/decoder/detokenize.c b/vp8/decoder/detokenize.c
index 166be9e..1a71948 100644
--- a/vp8/decoder/detokenize.c
+++ b/vp8/decoder/detokenize.c
@@ -38,37 +38,41 @@
 #define CAT_THREE_CONTEXT_NODE      9
 #define CAT_FIVE_CONTEXT_NODE       10
 
-/*
-//the definition is put in "onyxd_int.h"
-typedef struct
-{
-    INT16         min_val;
-    INT16         Length;
-    UINT8 Probs[12];
-} TOKENEXTRABITS;
-*/
+#define CAT1_MIN_VAL    5
+#define CAT2_MIN_VAL    7
+#define CAT3_MIN_VAL   11
+#define CAT4_MIN_VAL   19
+#define CAT5_MIN_VAL   35
+#define CAT6_MIN_VAL   67
 
-DECLARE_ALIGNED(16, static const TOKENEXTRABITS, vp8d_token_extra_bits2[MAX_ENTROPY_TOKENS]) =
-{
-    {  0, -1, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },  /* ZERO_TOKEN */
-    {  1, 0, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },   /* ONE_TOKEN */
-    {  2, 0, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },   /* TWO_TOKEN */
-    {  3, 0, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },   /* THREE_TOKEN */
-    {  4, 0, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },   /* FOUR_TOKEN */
-    {  5, 0, { 159, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },  /* DCT_VAL_CATEGORY1 */
-    {  7, 1, { 145, 165, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } }, /* DCT_VAL_CATEGORY2 */
-    { 11, 2, { 140, 148, 173, 0,  0,  0,  0,  0,  0,  0,  0,  0   } }, /* DCT_VAL_CATEGORY3 */
-    { 19, 3, { 135, 140, 155, 176, 0,  0,  0,  0,  0,  0,  0,  0   } }, /* DCT_VAL_CATEGORY4 */
-    { 35, 4, { 130, 134, 141, 157, 180, 0,  0,  0,  0,  0,  0,  0   } }, /* DCT_VAL_CATEGORY5 */
-    { 67, 10, { 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254, 0   } }, /* DCT_VAL_CATEGORY6 */
-    {  0, -1, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0   } },  /*  EOB TOKEN */
-};
+#define CAT1_PROB0    159
+#define CAT2_PROB0    145
+#define CAT2_PROB1    165
+
+#define CAT3_PROB0 140
+#define CAT3_PROB1 148
+#define CAT3_PROB2 173
+
+#define CAT4_PROB0 135
+#define CAT4_PROB1 140
+#define CAT4_PROB2 155
+#define CAT4_PROB3 176
+
+#define CAT5_PROB0 130
+#define CAT5_PROB1 134
+#define CAT5_PROB2 141
+#define CAT5_PROB3 157
+#define CAT5_PROB4 180
+
+static const unsigned char cat6_prob[12] =
+{ 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254, 0 };
 
 
 void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
 {
     /* Clear entropy contexts for Y2 blocks */
-    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
+    if (x->mode_info_context->mbmi.mode != B_PRED &&
+        x->mode_info_context->mbmi.mode != SPLITMV)
     {
         vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
         vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
@@ -157,12 +161,12 @@
         qcoeff_ptr [ scan[c] ] = (INT16) v; \
         ++c; \
         goto DO_WHILE; }\
-    qcoeff_ptr [ scan[15] ] = (INT16) v; \
+    qcoeff_ptr [ 15 ] = (INT16) v; \
     goto BLOCK_FINISHED;
 
 
-#define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\
-    split = 1 +  (((range-1) * vp8d_token_extra_bits2[t].Probs[bits_count]) >> 8); \
+#define DECODE_EXTRABIT_AND_ADJUST_VAL(prob, bits_count)\
+    split = 1 +  (((range-1) * prob) >> 8); \
     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
     FILL \
     if(value >= bigsplit)\
@@ -220,7 +224,8 @@
     scan = vp8_default_zig_zag1d;
     qcoeff_ptr = &x->qcoeff[0];
 
-    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
+    if (x->mode_info_context->mbmi.mode != B_PRED &&
+        x->mode_info_context->mbmi.mode != SPLITMV)
     {
         i = 24;
         stop = 24;
@@ -256,16 +261,21 @@
 CHECK_0_:
     DECODE_AND_LOOP_IF_ZERO(Prob[ZERO_CONTEXT_NODE], CHECK_0_);
     DECODE_AND_BRANCH_IF_ZERO(Prob[ONE_CONTEXT_NODE], ONE_CONTEXT_NODE_0_);
-    DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE], LOW_VAL_CONTEXT_NODE_0_);
-    DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE], HIGH_LOW_CONTEXT_NODE_0_);
-    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE], CAT_THREEFOUR_CONTEXT_NODE_0_);
-    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE], CAT_FIVE_CONTEXT_NODE_0_);
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY6].min_val;
-    bits_count = vp8d_token_extra_bits2[DCT_VAL_CATEGORY6].Length;
+    DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE],
+                              LOW_VAL_CONTEXT_NODE_0_);
+    DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE],
+                              HIGH_LOW_CONTEXT_NODE_0_);
+    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE],
+                              CAT_THREEFOUR_CONTEXT_NODE_0_);
+    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE],
+                              CAT_FIVE_CONTEXT_NODE_0_);
+
+    val = CAT6_MIN_VAL;
+    bits_count = 10;
 
     do
     {
-        DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count);
+        DECODE_EXTRABIT_AND_ADJUST_VAL(cat6_prob[bits_count], bits_count);
         bits_count -- ;
     }
     while (bits_count >= 0);
@@ -273,41 +283,43 @@
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 CAT_FIVE_CONTEXT_NODE_0_:
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY5].min_val;
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0);
+    val = CAT5_MIN_VAL;
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB4, 4);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB3, 3);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB2, 2);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB1, 1);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT5_PROB0, 0);
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 CAT_THREEFOUR_CONTEXT_NODE_0_:
-    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE], CAT_THREE_CONTEXT_NODE_0_);
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY4].min_val;
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0);
+    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE],
+                              CAT_THREE_CONTEXT_NODE_0_);
+    val = CAT4_MIN_VAL;
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB3, 3);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB2, 2);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB1, 1);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT4_PROB0, 0);
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 CAT_THREE_CONTEXT_NODE_0_:
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY3].min_val;
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0);
+    val = CAT3_MIN_VAL;
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB2, 2);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB1, 1);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT3_PROB0, 0);
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 HIGH_LOW_CONTEXT_NODE_0_:
-    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE], CAT_ONE_CONTEXT_NODE_0_);
+    DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE],
+                              CAT_ONE_CONTEXT_NODE_0_);
 
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY2].min_val;
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1);
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0);
+    val = CAT2_MIN_VAL;
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB1, 1);
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT2_PROB0, 0);
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 CAT_ONE_CONTEXT_NODE_0_:
-    val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY1].min_val;
-    DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0);
+    val = CAT1_MIN_VAL;
+    DECODE_EXTRABIT_AND_ADJUST_VAL(CAT1_PROB0, 0);
     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
 
 LOW_VAL_CONTEXT_NODE_0_:
@@ -332,7 +344,7 @@
         goto DO_WHILE;
     }
 
-    qcoeff_ptr [ scan[15] ] = (INT16) v;
+    qcoeff_ptr [ 15 ] = (INT16) v;
 BLOCK_FINISHED:
     *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
     eobtotal += c;
diff --git a/vp8/decoder/onyxd_if.c b/vp8/decoder/onyxd_if.c
index 829a675..2246194 100644
--- a/vp8/decoder/onyxd_if.c
+++ b/vp8/decoder/onyxd_if.c
@@ -204,8 +204,8 @@
 
 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
 #if HAVE_ARMV7
-extern void vp8_push_neon(INT64 *store);
-extern void vp8_pop_neon(INT64 *store);
+extern void vp8_push_neon(int64_t *store);
+extern void vp8_pop_neon(int64_t *store);
 #endif
 
 static int get_free_fb (VP8_COMMON *cm)
@@ -288,10 +288,10 @@
     return err;
 }
 
-int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, INT64 time_stamp)
+int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, int64_t time_stamp)
 {
 #if HAVE_ARMV7
-    INT64 dx_store_reg[8];
+    int64_t dx_store_reg[8];
 #endif
     VP8D_COMP *pbi = (VP8D_COMP *) ptr;
     VP8_COMMON *cm = &pbi->common;
@@ -495,9 +495,9 @@
 #if 0
     {
         int i;
-        INT64 earliest_time = pbi->dr[0].time_stamp;
-        INT64 latest_time = pbi->dr[0].time_stamp;
-        INT64 time_diff = 0;
+        int64_t earliest_time = pbi->dr[0].time_stamp;
+        int64_t latest_time = pbi->dr[0].time_stamp;
+        int64_t time_diff = 0;
         int bytes = 0;
 
         pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
@@ -537,7 +537,7 @@
     pbi->common.error.setjmp = 0;
     return retcode;
 }
-int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, INT64 *time_stamp, INT64 *time_end_stamp, vp8_ppflags_t *flags)
+int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, int64_t *time_stamp, int64_t *time_end_stamp, vp8_ppflags_t *flags)
 {
     int ret = -1;
     VP8D_COMP *pbi = (VP8D_COMP *) ptr;
diff --git a/vp8/decoder/onyxd_int.h b/vp8/decoder/onyxd_int.h
index be6ffe6..eac57ab 100644
--- a/vp8/decoder/onyxd_int.h
+++ b/vp8/decoder/onyxd_int.h
@@ -38,23 +38,15 @@
 
 typedef struct
 {
-    INT64 time_stamp;
+    int64_t time_stamp;
     int size;
 } DATARATE;
 
 typedef struct
 {
-    INT16         min_val;
-    INT16         Length;
-    UINT8 Probs[12];
-} TOKENEXTRABITS;
-
-typedef struct
-{
     int const *scan;
     UINT8 const *ptr_block2leftabove;
     vp8_tree_index const *vp8_coef_tree_ptr;
-    TOKENEXTRABITS const *teb_base_ptr;
     unsigned char *norm_ptr;
     UINT8 *ptr_coef_bands_x;
 
@@ -117,7 +109,7 @@
 #endif
 
     vp8_reader *mbc;
-    INT64 last_time_stamp;
+    int64_t last_time_stamp;
     int   ready_for_new_data;
 
     DATARATE dr[16];
diff --git a/vp8/encoder/encodeframe.c b/vp8/encoder/encodeframe.c
index 0efb9f6..5d4e7e7 100644
--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -140,7 +140,7 @@
 
 // Calculate an "average" mb activity value for the frame
 #define ACT_MEDIAN 0
-static void calc_av_activity( VP8_COMP *cpi, INT64 activity_sum )
+static void calc_av_activity( VP8_COMP *cpi, int64_t activity_sum )
 {
 #if ACT_MEDIAN
     // Find median: Simple n^2 algorithm for experimentation
@@ -208,9 +208,9 @@
     VP8_COMMON *const cm = & cpi->common;
     int mb_row, mb_col;
 
-    INT64 act;
-    INT64 a;
-    INT64 b;
+    int64_t act;
+    int64_t a;
+    int64_t b;
 
 #if OUTPUT_NORM_ACT_STATS
     FILE *f = fopen("norm_act.stt", "a");
@@ -274,7 +274,7 @@
 
     int mb_row, mb_col;
     unsigned int mb_activity;
-    INT64 activity_sum = 0;
+    int64_t activity_sum = 0;
 
     // for each macroblock row in image
     for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
@@ -341,15 +341,15 @@
     x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
     x->errorperbit += (x->errorperbit==0);
 #else
-    INT64 a;
-    INT64 b;
-    INT64 act = *(x->mb_activity_ptr);
+    int64_t a;
+    int64_t b;
+    int64_t act = *(x->mb_activity_ptr);
 
     // Apply the masking to the RD multiplier.
     a = act + (2*cpi->activity_avg);
     b = (2*act) + cpi->activity_avg;
 
-    x->rdmult = (unsigned int)(((INT64)x->rdmult*b + (a>>1))/a);
+    x->rdmult = (unsigned int)(((int64_t)x->rdmult*b + (a>>1))/a);
     x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
     x->errorperbit += (x->errorperbit==0);
 #endif
@@ -1102,18 +1102,18 @@
 #if USE_ACT_INDEX
     x->act_zbin_adj = *(x->mb_activity_ptr);
 #else
-    INT64 a;
-    INT64 b;
-    INT64 act = *(x->mb_activity_ptr);
+    int64_t a;
+    int64_t b;
+    int64_t act = *(x->mb_activity_ptr);
 
     // Apply the masking to the RD multiplier.
     a = act + 4*cpi->activity_avg;
     b = 4*act + cpi->activity_avg;
 
     if ( act > cpi->activity_avg )
-        x->act_zbin_adj = (int)(((INT64)b + (a>>1))/a) - 1;
+        x->act_zbin_adj = (int)(((int64_t)b + (a>>1))/a) - 1;
     else
-        x->act_zbin_adj = 1 - (int)(((INT64)a + (b>>1))/b);
+        x->act_zbin_adj = 1 - (int)(((int64_t)a + (b>>1))/b);
 #endif
 }
 
diff --git a/vp8/encoder/firstpass.c b/vp8/encoder/firstpass.c
index 795388d..a4730a7 100644
--- a/vp8/encoder/firstpass.c
+++ b/vp8/encoder/firstpass.c
@@ -530,8 +530,8 @@
     YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
     int recon_y_stride = lst_yv12->y_stride;
     int recon_uv_stride = lst_yv12->uv_stride;
-    long long intra_error = 0;
-    long long coded_error = 0;
+    int64_t intra_error = 0;
+    int64_t coded_error = 0;
 
     int sum_mvr = 0, sum_mvc = 0;
     int sum_mvr_abs = 0, sum_mvc_abs = 0;
@@ -620,7 +620,7 @@
             this_error += intrapenalty;
 
             // Cumulative intra error total
-            intra_error += (long long)this_error;
+            intra_error += (int64_t)this_error;
 
             // Set up limit values for motion vectors to prevent them extending outside the UMV borders
             x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
@@ -757,7 +757,7 @@
                 }
             }
 
-            coded_error += (long long)this_error;
+            coded_error += (int64_t)this_error;
 
             // adjust to the next column of macroblocks
             x->src.y_buffer += 16;
@@ -1219,8 +1219,8 @@
     cpi->twopass.total_coded_error_left = cpi->twopass.total_stats->coded_error;
     cpi->twopass.start_tot_err_left = cpi->twopass.total_error_left;
 
-    //cpi->twopass.bits_left = (long long)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
-    //cpi->twopass.bits_left -= (long long)(cpi->twopass.total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
+    //cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
+    //cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
 
     // each frame can have a different duration, as the frame rate in the source
     // isn't guaranteed to be constant.   The frame rate prior to the first frame
@@ -1230,8 +1230,8 @@
     vp8_new_frame_rate(cpi, 10000000.0 * cpi->twopass.total_stats->count / cpi->twopass.total_stats->duration);
 
     cpi->output_frame_rate = cpi->oxcf.frame_rate;
-    cpi->twopass.bits_left = (long long)(cpi->twopass.total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
-    cpi->twopass.bits_left -= (long long)(cpi->twopass.total_stats->duration * two_pass_min_rate / 10000000.0);
+    cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
+    cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->duration * two_pass_min_rate / 10000000.0);
     cpi->twopass.clip_bits_total = cpi->twopass.bits_left;
 
     // Calculate a minimum intra value to be used in determining the IIratio
@@ -2671,16 +2671,16 @@
         int max_bits = frame_max_bits(cpi);
 
         // Maximum bits for the kf group
-        long long max_grp_bits;
+        int64_t max_grp_bits;
 
         // Default allocation based on bits left and relative
         // complexity of the section
-        cpi->twopass.kf_group_bits = (long long)( cpi->twopass.bits_left *
+        cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
                                           ( kf_group_err /
                                             cpi->twopass.modified_error_left ));
 
         // Clip based on maximum per frame rate defined by the user.
-        max_grp_bits = (long long)max_bits * (long long)cpi->twopass.frames_to_key;
+        max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
         if (cpi->twopass.kf_group_bits > max_grp_bits)
             cpi->twopass.kf_group_bits = max_grp_bits;
 
@@ -2697,19 +2697,19 @@
                 int high_water_mark = (opt_buffer_lvl +
                                        cpi->oxcf.maximum_buffer_size) >> 1;
 
-                long long av_group_bits;
+                int64_t av_group_bits;
 
                 // Av bits per frame * number of frames
-                av_group_bits = (long long)cpi->av_per_frame_bandwidth *
-                                (long long)cpi->twopass.frames_to_key;
+                av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
+                                (int64_t)cpi->twopass.frames_to_key;
 
                 // We are at or above the maximum.
                 if (cpi->buffer_level >= high_water_mark)
                 {
-                    long long min_group_bits;
+                    int64_t min_group_bits;
 
                     min_group_bits = av_group_bits +
-                                     (long long)(buffer_lvl -
+                                     (int64_t)(buffer_lvl -
                                                  high_water_mark);
 
                     if (cpi->twopass.kf_group_bits < min_group_bits)
@@ -2718,11 +2718,11 @@
                 // We are above optimal but below the maximum
                 else if (cpi->twopass.kf_group_bits < av_group_bits)
                 {
-                    long long bits_below_av = av_group_bits -
+                    int64_t bits_below_av = av_group_bits -
                                               cpi->twopass.kf_group_bits;
 
                     cpi->twopass.kf_group_bits +=
-                       (long long)((double)bits_below_av *
+                       (int64_t)((double)bits_below_av *
                                    (double)(buffer_lvl - opt_buffer_lvl) /
                                    (double)(high_water_mark - opt_buffer_lvl));
                 }
@@ -3043,8 +3043,8 @@
         }
         else
         {
-            long long clip_bits = (long long)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
-            long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
+            int64_t clip_bits = (int64_t)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
+            int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
 
             if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||                                               // If triggered last time the threshold for triggering again is reduced
                 ((kf_q > cpi->worst_quality) &&                                                                  // Projected Q higher than allowed and ...
diff --git a/vp8/encoder/mcomp.c b/vp8/encoder/mcomp.c
index 58b524f..ff84306 100644
--- a/vp8/encoder/mcomp.c
+++ b/vp8/encoder/mcomp.c
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <limits.h>
 #include <math.h>
+#include "vp8/common/findnearmv.h"
 
 #ifdef ENTROPY_STATS
 static int mv_ref_ct [31] [4] [2];
@@ -342,12 +343,26 @@
     int bestmse = INT_MAX;
     int_mv startmv;
     int_mv this_mv;
-    unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
     unsigned char *z = (*(b->base_src) + b->src);
     int left, right, up, down, diag;
     unsigned int sse;
     int whichdir ;
     int thismse;
+    int y_stride;
+
+#if ARCH_X86 || ARCH_X86_64
+    MACROBLOCKD *xd = &x->e_mbd;
+    unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
+    unsigned char *y;
+
+    y_stride = 32;
+    /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
+     vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
+     y = xd->y_buf + y_stride + 1;
+#else
+     unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
+     y_stride = d->pre_stride;
+#endif
 
     // central mv
     bestmv->as_mv.row <<= 3;
@@ -355,14 +370,14 @@
     startmv = *bestmv;
 
     // calculate central point error
-    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
+    bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
     *distortion = bestmse;
     bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
 
     // go left then right and check error
     this_mv.as_mv.row = startmv.as_mv.row;
     this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
-    thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
     left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (left < bestmse)
@@ -374,7 +389,7 @@
     }
 
     this_mv.as_mv.col += 8;
-    thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
     right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (right < bestmse)
@@ -388,7 +403,7 @@
     // go up then down and check error
     this_mv.as_mv.col = startmv.as_mv.col;
     this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
-    thismse =  vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+    thismse =  vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
     up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (up < bestmse)
@@ -400,7 +415,7 @@
     }
 
     this_mv.as_mv.row += 8;
-    thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
     down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (down < bestmse)
@@ -423,23 +438,23 @@
     case 0:
         this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
         this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
-        thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+        thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
         break;
     case 1:
         this_mv.as_mv.col += 4;
         this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
-        thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+        thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
         break;
     case 2:
         this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
         this_mv.as_mv.row += 4;
-        thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
+        thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
         break;
     case 3:
     default:
         this_mv.as_mv.col += 4;
         this_mv.as_mv.row += 4;
-        thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
+        thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
         break;
     }
 
@@ -458,7 +473,7 @@
 
     // time to check quarter pels.
     if (bestmv->as_mv.row < startmv.as_mv.row)
-        y -= d->pre_stride;
+        y -= y_stride;
 
     if (bestmv->as_mv.col < startmv.as_mv.col)
         y--;
@@ -473,12 +488,12 @@
     if (startmv.as_mv.col & 7)
     {
         this_mv.as_mv.col = startmv.as_mv.col - 2;
-        thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+        thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
     }
     else
     {
         this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
-        thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+        thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
     }
 
     left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
@@ -492,7 +507,7 @@
     }
 
     this_mv.as_mv.col += 4;
-    thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+    thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
     right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (right < bestmse)
@@ -509,12 +524,12 @@
     if (startmv.as_mv.row & 7)
     {
         this_mv.as_mv.row = startmv.as_mv.row - 2;
-        thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+        thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
     }
     else
     {
         this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
-        thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
+        thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
     }
 
     up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
@@ -528,7 +543,7 @@
     }
 
     this_mv.as_mv.row += 4;
-    thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+    thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
     down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (down < bestmse)
@@ -558,12 +573,12 @@
             if (startmv.as_mv.col & 7)
             {
                 this_mv.as_mv.col -= 2;
-                thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+                thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
             }
             else
             {
                 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
-                thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
+                thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
             }
         }
         else
@@ -573,12 +588,12 @@
             if (startmv.as_mv.col & 7)
             {
                 this_mv.as_mv.col -= 2;
-                thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
+                thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
             }
             else
             {
                 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
-                thismse = vfp->svf(y - d->pre_stride - 1, d->pre_stride, 6, 6, z, b->src_stride, &sse);
+                thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
             }
         }
 
@@ -589,12 +604,12 @@
         if (startmv.as_mv.row & 7)
         {
             this_mv.as_mv.row -= 2;
-            thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+            thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
         }
         else
         {
             this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
-            thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
+            thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
         }
 
         break;
@@ -604,19 +619,19 @@
         if (startmv.as_mv.col & 7)
         {
             this_mv.as_mv.col -= 2;
-            thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+            thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
         }
         else
         {
             this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
-            thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+            thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
         }
 
         break;
     case 3:
         this_mv.as_mv.col += 2;
         this_mv.as_mv.row += 2;
-        thismse = vfp->svf(y, d->pre_stride,  this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
+        thismse = vfp->svf(y, y_stride,  this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
         break;
     }
 
@@ -633,7 +648,7 @@
     return bestmse;
 }
 
-int vp8_find_best_half_pixel_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
+int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
                                   int_mv *bestmv, int_mv *ref_mv,
                                   int error_per_bit,
                                   const vp8_variance_fn_ptr_t *vfp,
@@ -643,11 +658,25 @@
     int bestmse = INT_MAX;
     int_mv startmv;
     int_mv this_mv;
-    unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
     unsigned char *z = (*(b->base_src) + b->src);
     int left, right, up, down, diag;
     unsigned int sse;
     int thismse;
+    int y_stride;
+
+#if ARCH_X86 || ARCH_X86_64
+    MACROBLOCKD *xd = &x->e_mbd;
+    unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
+    unsigned char *y;
+
+    y_stride = 32;
+    /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
+    vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
+    y = xd->y_buf + y_stride + 1;
+#else
+    unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
+    y_stride = d->pre_stride;
+#endif
 
     // central mv
     bestmv->as_mv.row <<= 3;
@@ -655,14 +684,14 @@
     startmv = *bestmv;
 
     // calculate central point error
-    bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
+    bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
     *distortion = bestmse;
     bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
 
     // go left then right and check error
     this_mv.as_mv.row = startmv.as_mv.row;
     this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
-    thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
     left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (left < bestmse)
@@ -674,7 +703,7 @@
     }
 
     this_mv.as_mv.col += 8;
-    thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
     right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (right < bestmse)
@@ -688,7 +717,7 @@
     // go up then down and check error
     this_mv.as_mv.col = startmv.as_mv.col;
     this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
-    thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
     up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (up < bestmse)
@@ -700,7 +729,7 @@
     }
 
     this_mv.as_mv.row += 8;
-    thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
     down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (down < bestmse)
@@ -722,22 +751,22 @@
     case 0:
         this_mv.col = (this_mv.col - 8) | 4;
         this_mv.row = (this_mv.row - 8) | 4;
-        diag = vfp->svf(y - 1 - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
+        diag = vfp->svf(y - 1 - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
         break;
     case 1:
         this_mv.col += 4;
         this_mv.row = (this_mv.row - 8) | 4;
-        diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
+        diag = vfp->svf(y - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
         break;
     case 2:
         this_mv.col = (this_mv.col - 8) | 4;
         this_mv.row += 4;
-        diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
+        diag = vfp->svf(y - 1, y_stride, 4, 4, z, b->src_stride, &sse);
         break;
     case 3:
         this_mv.col += 4;
         this_mv.row += 4;
-        diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
+        diag = vfp->svf(y, y_stride, 4, 4, z, b->src_stride, &sse);
         break;
     }
 
@@ -752,7 +781,7 @@
 #else
     this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
     this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
-    thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
     diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (diag < bestmse)
@@ -764,7 +793,7 @@
     }
 
     this_mv.as_mv.col += 8;
-    thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
     diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (diag < bestmse)
@@ -777,7 +806,7 @@
 
     this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
     this_mv.as_mv.row = startmv.as_mv.row + 4;
-    thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
     diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (diag < bestmse)
@@ -789,7 +818,7 @@
     }
 
     this_mv.as_mv.col += 8;
-    thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
+    thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
     diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
 
     if (diag < bestmse)
@@ -866,7 +895,7 @@
     unsigned char *what = (*(b->base_src) + b->src);
     int what_stride = b->src_stride;
     int in_what_stride = d->pre_stride;
-    int br = ref_mv->as_mv.row, bc = ref_mv->as_mv.col;
+    int br, bc;
     int_mv this_mv;
     unsigned int bestsad = 0x7fffffff;
     unsigned int thissad;
@@ -880,6 +909,11 @@
     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
 
+    // adjust ref_mv to make sure it is within MV range
+    vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
+    br = ref_mv->as_mv.row;
+    bc = ref_mv->as_mv.col;
+
     // Work out the start point for the search
     base_offset = (unsigned char *)(*(d->base_pre) + d->pre);
     this_offset = base_offset + (br * (d->pre_stride)) + bc;
@@ -1043,8 +1077,8 @@
     int best_site = 0;
     int last_site = 0;
 
-    int ref_row = ref_mv->as_mv.row;
-    int ref_col = ref_mv->as_mv.col;
+    int ref_row;
+    int ref_col;
     int this_row_offset;
     int this_col_offset;
     search_site *ss;
@@ -1057,8 +1091,10 @@
     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
 
+    vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
+    ref_row = ref_mv->as_mv.row;
+    ref_col = ref_mv->as_mv.col;
     *num00 = 0;
-
     best_mv->as_mv.row = ref_row;
     best_mv->as_mv.col = ref_col;
 
@@ -1162,8 +1198,8 @@
     int best_site = 0;
     int last_site = 0;
 
-    int ref_row = ref_mv->as_mv.row;
-    int ref_col = ref_mv->as_mv.col;
+    int ref_row;
+    int ref_col;
     int this_row_offset;
     int this_col_offset;
     search_site *ss;
@@ -1176,6 +1212,9 @@
     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
 
+    vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
+    ref_row = ref_mv->as_mv.row;
+    ref_col = ref_mv->as_mv.col;
     *num00 = 0;
     best_mv->as_mv.row = ref_row;
     best_mv->as_mv.col = ref_col;
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index de045b4..228850d 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3196,7 +3196,7 @@
 
 static void update_buffer_level(VP8_COMP *cpi)
 {
-    long long tmp;
+    int64_t tmp;
 
     /* Update the buffered average bitrate.
      *
@@ -3266,7 +3266,7 @@
          */
         if(cpi->total_actual_bits > cpi->oxcf.starting_buffer_level)
         {
-            tmp = (long long)cpi->buffered_av_per_frame_bandwidth
+            tmp = (int64_t)cpi->buffered_av_per_frame_bandwidth
                   * cpi->oxcf.maximum_buffer_size
                   / cpi->av_per_frame_bandwidth;
             cpi->buffer_level = cpi->oxcf.maximum_buffer_size
@@ -3284,7 +3284,7 @@
          */
         if(cpi->total_actual_bits > cpi->oxcf.starting_buffer_level)
         {
-            long long decayed_overshoot;
+            int64_t decayed_overshoot;
 
             decayed_overshoot = cpi->accumulated_overshoot;
             decayed_overshoot *= (cpi->oxcf.maximum_buffer_size
@@ -4638,22 +4638,22 @@
     {
         double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
             *cpi->oxcf.two_pass_vbrmin_section / 100);
-        cpi->twopass.bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
+        cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->oxcf.frame_rate);
     }
 }
 #endif
 
 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
 #if HAVE_ARMV7
-extern void vp8_push_neon(INT64 *store);
-extern void vp8_pop_neon(INT64 *store);
+extern void vp8_push_neon(int64_t *store);
+extern void vp8_pop_neon(int64_t *store);
 #endif
 
 
-int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
+int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
 {
 #if HAVE_ARMV7
-    INT64 store_reg[8];
+    int64_t store_reg[8];
 #endif
     VP8_COMP              *cpi = (VP8_COMP *) ptr;
     VP8_COMMON            *cm = &cpi->common;
@@ -4704,10 +4704,10 @@
 }
 
 
-int vp8_get_compressed_data(VP8_PTR ptr, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, INT64 *time_stamp, INT64 *time_end, int flush)
+int vp8_get_compressed_data(VP8_PTR ptr, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, int64_t *time_stamp, int64_t *time_end, int flush)
 {
 #if HAVE_ARMV7
-    INT64 store_reg[8];
+    int64_t store_reg[8];
 #endif
     VP8_COMP *cpi = (VP8_COMP *) ptr;
     VP8_COMMON *cm = &cpi->common;
@@ -4814,7 +4814,7 @@
     // adjust frame rates based on timestamps given
     if (!cm->refresh_alt_ref_frame)
     {
-        long long this_duration;
+        int64_t this_duration;
         int step = 0;
 
         if (cpi->source->ts_start == cpi->first_time_stamp_ever)
@@ -4824,7 +4824,7 @@
         }
         else
         {
-            long long last_duration;
+            int64_t last_duration;
 
             this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
             last_duration = cpi->last_end_time_stamp_seen
@@ -5000,7 +5000,7 @@
                 int y_samples = orig->y_height * orig->y_width ;
                 int uv_samples = orig->uv_height * orig->uv_width ;
                 int t_samples = y_samples + 2 * uv_samples;
-                long long sq_error;
+                int64_t sq_error;
 
                 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
                   recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height,
diff --git a/vp8/encoder/onyx_int.h b/vp8/encoder/onyx_int.h
index 341ece3..0d347e3 100644
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -317,10 +317,10 @@
     CODING_CONTEXT coding_context;
 
     // Rate targetting variables
-    long long prediction_error;
-    long long last_prediction_error;
-    long long intra_error;
-    long long last_intra_error;
+    int64_t prediction_error;
+    int64_t last_prediction_error;
+    int64_t intra_error;
+    int64_t last_intra_error;
 
     int this_frame_target;
     int projected_frame_size;
@@ -343,7 +343,7 @@
     int baseline_gf_interval;
     int active_arnr_frames;           // <= cpi->oxcf.arnr_max_frames
 
-    INT64 key_frame_count;
+    int64_t key_frame_count;
     int prior_key_frame_distance[KEY_FRAME_CONTEXT];
     int per_frame_bandwidth;          // Current section per frame bandwidth target
     int av_per_frame_bandwidth;        // Average frame size target for clip
@@ -354,9 +354,9 @@
 
     int inter_frame_target;
     double output_frame_rate;
-    long long last_time_stamp_seen;
-    long long last_end_time_stamp_seen;
-    long long first_time_stamp_ever;
+    int64_t last_time_stamp_seen;
+    int64_t last_end_time_stamp_seen;
+    int64_t first_time_stamp_ever;
 
     int ni_av_qi;
     int ni_tot_qi;
@@ -367,7 +367,7 @@
     int zbin_mode_boost;
     int zbin_mode_boost_enabled;
 
-    INT64 total_byte_count;
+    int64_t total_byte_count;
 
     int buffered_mode;
 
@@ -380,7 +380,7 @@
     int long_rolling_target_bits;
     int long_rolling_actual_bits;
 
-    long long total_actual_bits;
+    int64_t total_actual_bits;
     int total_target_vs_actual;        // debug stats
 
     int worst_quality;
@@ -528,8 +528,8 @@
         FIRSTPASS_STATS *this_frame_stats;
         FIRSTPASS_STATS *stats_in, *stats_in_end, *stats_in_start;
         int first_pass_done;
-        long long bits_left;
-        long long clip_bits_total;
+        int64_t bits_left;
+        int64_t clip_bits_total;
         double avg_iiratio;
         double modified_error_total;
         double modified_error_used;
@@ -549,10 +549,10 @@
         int gf_group_error_left;           // Remaining error from uncoded frames in a gf group. Two pass use only
 
         // Projected total bits available for a key frame group of frames
-        long long kf_group_bits;
+        int64_t kf_group_bits;
 
         // Error score of frames still to be coded in kf group
-        long long kf_group_error_left;
+        int64_t kf_group_error_left;
 
         int gf_group_bits;                // Projected Bits available for a group of frames including 1 GF or ARF
         int gf_bits;                     // Bits for the golden frame or ARF - 2 pass only
diff --git a/vp8/encoder/pickinter.c b/vp8/encoder/pickinter.c
index 725e44e..9cdaf7d 100644
--- a/vp8/encoder/pickinter.c
+++ b/vp8/encoder/pickinter.c
@@ -669,8 +669,6 @@
                 mvp_full.as_mv.col = mvp.as_mv.col>>3;
                 mvp_full.as_mv.row = mvp.as_mv.row>>3;
 
-                /* adjust mvp to make sure it is within MV range */
-                vp8_clamp_mv(&mvp_full, col_min, col_max, row_min, row_max);
             }else
             {
                 mvp.as_int = best_ref_mv.as_int;
diff --git a/vp8/encoder/ratectrl.c b/vp8/encoder/ratectrl.c
index 70e133f..769c91a 100644
--- a/vp8/encoder/ratectrl.c
+++ b/vp8/encoder/ratectrl.c
@@ -852,9 +852,9 @@
                         if ( critical_buffer_level >
                              (cpi->oxcf.optimal_buffer_level >> 2) )
                         {
-                            INT64 qadjustment_range =
+                            int64_t qadjustment_range =
                                       cpi->worst_quality - cpi->ni_av_qi;
-                            INT64 above_base =
+                            int64_t above_base =
                                       (critical_buffer_level -
                                        (cpi->oxcf.optimal_buffer_level >> 2));
 
@@ -1118,7 +1118,7 @@
     if (cpi->pass==0
         && cpi->common.refresh_golden_frame
         && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
-        long long adjust;
+        int64_t adjust;
 
         /*
         frames_in_buffer = cpi->oxcf.maximum_buffer_size
diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c
index 8b18541..992df71 100644
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -665,7 +665,7 @@
     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
     int distortion = 0;
     int tot_rate_y = 0;
-    long long total_rd = 0;
+    int64_t total_rd = 0;
     ENTROPY_CONTEXT_PLANES t_above, t_left;
     ENTROPY_CONTEXT *ta;
     ENTROPY_CONTEXT *tl;
@@ -707,11 +707,11 @@
 
         mic->bmi[i].as_mode = best_mode;
 
-        if(total_rd >= (long long)best_rd)
+        if(total_rd >= (int64_t)best_rd)
             break;
     }
 
-    if(total_rd >= (long long)best_rd)
+    if(total_rd >= (int64_t)best_rd)
         return INT_MAX;
 
     *Rate = cost;
@@ -1181,6 +1181,9 @@
                     // Should we do a full search (best quality only)
                     if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
                     {
+                        /* Check if mvp_full is within the range. */
+                        vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
+
                         thissme = cpi->full_search_sad(x, c, e, &mvp_full,
                                                        sadpb, 16, v_fn_ptr,
                                                        x->mvcost, bsi->ref_mv);
@@ -2016,9 +2019,6 @@
             mvp_full.as_mv.col = mvp.as_mv.col>>3;
             mvp_full.as_mv.row = mvp.as_mv.row>>3;
 
-            /* adjust mvp to make sure it is within MV range */
-            vp8_clamp_mv(&mvp_full, col_min, col_max, row_min, row_max);
-
             // 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;
diff --git a/vp8/encoder/ssim.c b/vp8/encoder/ssim.c
index b8701e8..fea756f 100644
--- a/vp8/encoder/ssim.c
+++ b/vp8/encoder/ssim.c
@@ -73,8 +73,8 @@
      }
 }
 
-const static long long cc1 =  26634; // (64^2*(.01*255)^2
-const static long long cc2 = 239708; // (64^2*(.03*255)^2
+const static int64_t cc1 =  26634; // (64^2*(.01*255)^2
+const static int64_t cc2 = 239708; // (64^2*(.03*255)^2
 
 static double similarity
 (
@@ -86,19 +86,19 @@
     int count
 )
 {
-    long long ssim_n, ssim_d;
-    long long c1, c2;
+    int64_t ssim_n, ssim_d;
+    int64_t c1, c2;
 
     //scale the constants by number of pixels
     c1 = (cc1*count*count)>>12;
     c2 = (cc2*count*count)>>12;
 
-    ssim_n = (2*sum_s*sum_r+ c1)*((long long) 2*count*sum_sxr-
-          (long long) 2*sum_s*sum_r+c2);
+    ssim_n = (2*sum_s*sum_r+ c1)*((int64_t) 2*count*sum_sxr-
+          (int64_t) 2*sum_s*sum_r+c2);
 
     ssim_d = (sum_s*sum_s +sum_r*sum_r+c1)*
-        ((long long)count*sum_sq_s-(long long)sum_s*sum_s +
-        (long long)count*sum_sq_r-(long long) sum_r*sum_r +c2) ;
+        ((int64_t)count*sum_sq_s-(int64_t)sum_s*sum_s +
+        (int64_t)count*sum_sq_r-(int64_t) sum_r*sum_r +c2) ;
 
     return ssim_n * 1.0 / ssim_d;
 }
@@ -124,11 +124,11 @@
            const vp8_variance_rtcd_vtable_t *rtcd)
 {
     unsigned long sum_s=0,sum_r=0,sum_sq_s=0,sum_sq_r=0,sum_sxr=0;
-    long long ssim3;
-    long long ssim_n1,ssim_n2;
-    long long ssim_d1,ssim_d2;
-    long long ssim_t1,ssim_t2;
-    long long c1, c2;
+    int64_t ssim3;
+    int64_t ssim_n1,ssim_n2;
+    int64_t ssim_d1,ssim_d2;
+    int64_t ssim_t1,ssim_t2;
+    int64_t c1, c2;
 
     // normalize by 256/64
     c1 = cc1*16;
@@ -137,12 +137,12 @@
     rtcd->ssimpf(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr);
     ssim_n1 = (2*sum_s*sum_r+ c1);
 
-    ssim_n2 =((long long) 2*256*sum_sxr-(long long) 2*sum_s*sum_r+c2);
+    ssim_n2 =((int64_t) 2*256*sum_sxr-(int64_t) 2*sum_s*sum_r+c2);
 
-    ssim_d1 =((long long)sum_s*sum_s +(long long)sum_r*sum_r+c1);
+    ssim_d1 =((int64_t)sum_s*sum_s +(int64_t)sum_r*sum_r+c1);
 
-    ssim_d2 = (256 * (long long) sum_sq_s-(long long) sum_s*sum_s +
-                    (long long) 256*sum_sq_r-(long long) sum_r*sum_r +c2) ;
+    ssim_d2 = (256 * (int64_t) sum_sq_s-(int64_t) sum_s*sum_s +
+                    (int64_t) 256*sum_sq_r-(int64_t) sum_r*sum_r +c2) ;
 
     ssim_t1 = 256 - 256 * ssim_n1 / ssim_d1;
     ssim_t2 = 256 - 256 * ssim_n2 / ssim_d2;
diff --git a/vp8/vp8_cx_iface.c b/vp8/vp8_cx_iface.c
index 8fcee81..d72d4b2 100644
--- a/vp8/vp8_cx_iface.c
+++ b/vp8/vp8_cx_iface.c
@@ -723,7 +723,7 @@
     {
         unsigned int lib_flags;
         YV12_BUFFER_CONFIG sd;
-        INT64 dst_time_stamp, dst_end_time_stamp;
+        int64_t dst_time_stamp, dst_end_time_stamp;
         unsigned long size, cx_data_sz;
         unsigned char *cx_data;
 
diff --git a/vp8/vp8_dx_iface.c b/vp8/vp8_dx_iface.c
index 13a072b..ad8cd5e 100644
--- a/vp8/vp8_dx_iface.c
+++ b/vp8/vp8_dx_iface.c
@@ -427,7 +427,7 @@
     if (!res && ctx->pbi)
     {
         YV12_BUFFER_CONFIG sd;
-        INT64 time_stamp = 0, time_end_stamp = 0;
+        int64_t time_stamp = 0, time_end_stamp = 0;
         vp8_ppflags_t flags = {0};
 
         if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
diff --git a/vpxenc.c b/vpxenc.c
index d82a97f..2de881c 100644
--- a/vpxenc.c
+++ b/vpxenc.c
@@ -538,7 +538,7 @@
 }
 #undef WRITE_BUFFER
 
-/* Need a fixed size serializer for the track ID. libmkv provdes a 64 bit
+/* Need a fixed size serializer for the track ID. libmkv provides a 64 bit
  * one, but not a 32 bit one.
  */
 static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
@@ -555,8 +555,8 @@
                           unsigned long class_id)
 {
     //todo this is always taking 8 bytes, this may need later optimization
-    //this is a key that says lenght unknown
-    unsigned long long unknownLen =  LITERALU64(0x01FFFFFFFFFFFFFF);
+    //this is a key that says length unknown
+    uint64_t unknownLen =  LITERALU64(0x01FFFFFFFFFFFFFF);
 
     Ebml_WriteID(glob, class_id);
     *ebmlLoc = ftello(glob->stream);
@@ -971,7 +971,7 @@
 static const arg_def_t stereo_mode      = ARG_DEF_ENUM(NULL, "stereo-mode", 1,
         "Stereo 3D video format", stereo_mode_enum);
 static const arg_def_t timebase         = ARG_DEF(NULL, "timebase", 1,
-        "Stream timebase (frame duration)");
+        "Stream timebase (see below)");
 static const arg_def_t error_resilient  = ARG_DEF(NULL, "error-resilient", 1,
         "Enable error resiliency features");
 static const arg_def_t lag_in_frames    = ARG_DEF(NULL, "lag-in-frames", 1,
@@ -1127,6 +1127,9 @@
     fprintf(stderr, "\nVP8 Specific Options:\n");
     arg_show_usage(stdout, vp8_args);
 #endif
+    fprintf(stderr, "\nStream timebase (--timebase):\n"
+            "  This is the unit of time used to represent frame timestamps,\n"
+            "  in fractional seconds. Default is 1/1000.\n");
     fprintf(stderr, "\n"
            "Included encoders:\n"
            "\n");
@@ -1165,7 +1168,7 @@
             big_bucket = i;
     }
 
-    /* If we have too many buckets, merge the smallest with an ajacent
+    /* If we have too many buckets, merge the smallest with an adjacent
      * bucket.
      */
     while(buckets > max_buckets)