Merge "Moving get_scan() call out of decode_coeffs() function."
diff --git a/vp9/common/x86/vp9_idct_intrin_sse2.c b/vp9/common/x86/vp9_idct_intrin_sse2.c
index 06df850..501bed5 100644
--- a/vp9/common/x86/vp9_idct_intrin_sse2.c
+++ b/vp9/common/x86/vp9_idct_intrin_sse2.c
@@ -459,7 +459,9 @@
       res1 = _mm_packs_epi32(tmp2, tmp3); \
   }
 
-#define IDCT8_1D  \
+#define IDCT8_1D(in0, in1, in2, in3, in4, in5, in6, in7, \
+                 out0, out1, out2, out3, out4, out5, out6, out7)  \
+  { \
   /* Stage1 */      \
   { \
     const __m128i lo_17 = _mm_unpacklo_epi16(in1, in7); \
@@ -519,14 +521,15 @@
   } \
   \
   /* Stage4  */ \
-  in0 = _mm_adds_epi16(stp1_0, stp2_7); \
-  in1 = _mm_adds_epi16(stp1_1, stp1_6); \
-  in2 = _mm_adds_epi16(stp1_2, stp1_5); \
-  in3 = _mm_adds_epi16(stp1_3, stp2_4); \
-  in4 = _mm_subs_epi16(stp1_3, stp2_4); \
-  in5 = _mm_subs_epi16(stp1_2, stp1_5); \
-  in6 = _mm_subs_epi16(stp1_1, stp1_6); \
-  in7 = _mm_subs_epi16(stp1_0, stp2_7);
+  out0 = _mm_adds_epi16(stp1_0, stp2_7); \
+  out1 = _mm_adds_epi16(stp1_1, stp1_6); \
+  out2 = _mm_adds_epi16(stp1_2, stp1_5); \
+  out3 = _mm_adds_epi16(stp1_3, stp2_4); \
+  out4 = _mm_subs_epi16(stp1_3, stp2_4); \
+  out5 = _mm_subs_epi16(stp1_2, stp1_5); \
+  out6 = _mm_subs_epi16(stp1_1, stp1_6); \
+  out7 = _mm_subs_epi16(stp1_0, stp2_7); \
+  }
 
 #define RECON_AND_STORE(dest, in_x) \
   {                                                     \
@@ -574,7 +577,8 @@
                   in0, in1, in2, in3, in4, in5, in6, in7);
 
     // 4-stage 1D idct8x8
-    IDCT8_1D
+    IDCT8_1D(in0, in1, in2, in3, in4, in5, in6, in7,
+             in0, in1, in2, in3, in4, in5, in6, in7);
   }
 
   // Final rounding and shift
@@ -697,15 +701,8 @@
                 in0, in1, in2, in3, in4, in5, in6, in7);
 
   // 4-stage 1D idct8x8
-  IDCT8_1D
-  in[0] = in0;
-  in[1] = in1;
-  in[2] = in2;
-  in[3] = in3;
-  in[4] = in4;
-  in[5] = in5;
-  in[6] = in6;
-  in[7] = in7;
+  IDCT8_1D(in0, in1, in2, in3, in4, in5, in6, in7,
+           in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
 }
 
 static void iadst8_1d_sse2(__m128i *in) {
@@ -1112,9 +1109,9 @@
   tmp3 = _mm_subs_epi16(stp1_2, stp1_5);
 
   TRANSPOSE_4X8_10(tmp0, tmp1, tmp2, tmp3, in0, in1, in2, in3)
-  in4 = in5 = in6 = in7 = zero;
 
-  IDCT8_1D
+  IDCT8_1D(in0, in1, in2, in3, zero, zero, zero, zero,
+           in0, in1, in2, in3, in4, in5, in6, in7);
   // Final rounding and shift
   in0 = _mm_adds_epi16(in0, final_rounding);
   in1 = _mm_adds_epi16(in1, final_rounding);
diff --git a/vp9/encoder/vp9_firstpass.c b/vp9/encoder/vp9_firstpass.c
index 3df9a1f..ba0fea2 100644
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -271,8 +271,17 @@
   const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats;
   const double av_err = stats->ssim_weighted_pred_err / stats->count;
   const double this_err = this_frame->ssim_weighted_pred_err;
-  return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err),
-                      this_err > av_err ? POW1 : POW2);
+  double modified_error;
+
+  modified_error =  av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err),
+                                 this_err > av_err ? POW1 : POW2);
+
+  if (modified_error < cpi->twopass.modified_error_min)
+    modified_error = cpi->twopass.modified_error_min;
+  else if (modified_error > cpi->twopass.modified_error_max)
+    modified_error = cpi->twopass.modified_error_max;
+
+  return modified_error;
 }
 
 static const double weight_table[256] = {
@@ -1077,13 +1086,6 @@
   FIRSTPASS_STATS this_frame;
   FIRSTPASS_STATS *start_pos;
 
-  double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
-  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
-                                      cpi->oxcf.two_pass_vbrmin_section / 100);
-
-  if (two_pass_min_rate < lower_bounds_min_rate)
-    two_pass_min_rate = lower_bounds_min_rate;
-
   zero_stats(&cpi->twopass.total_stats);
   zero_stats(&cpi->twopass.total_left_stats);
 
@@ -1104,8 +1106,6 @@
   cpi->output_framerate = cpi->oxcf.framerate;
   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);
 
   // Calculate a minimum intra value to be used in determining the IIratio
   // scores used in the second pass. We have this minimum to make sure
@@ -1142,9 +1142,16 @@
   // Scan the first pass file and calculate a modified total error based upon
   // the bias/power function used to allocate bits.
   {
+    double av_error = cpi->twopass.total_stats.ssim_weighted_pred_err /
+                      DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.count);
+
     start_pos = cpi->twopass.stats_in;  // Note starting "file" position
 
     cpi->twopass.modified_error_total = 0.0;
+    cpi->twopass.modified_error_min =
+      (av_error * cpi->oxcf.two_pass_vbrmin_section) / 100;
+    cpi->twopass.modified_error_max =
+      (av_error * cpi->oxcf.two_pass_vbrmax_section) / 100;
 
     while (input_stats(cpi, &this_frame) != EOF) {
       cpi->twopass.modified_error_total +=
@@ -2618,14 +2625,4 @@
 #else
   cpi->twopass.bits_left -= 8 * bytes_used;
 #endif
-  if (!cpi->refresh_alt_ref_frame) {
-    double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
-    double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
-                                        cpi->oxcf.two_pass_vbrmin_section
-                                        / 100);
-    if (two_pass_min_rate < lower_bounds_min_rate)
-      two_pass_min_rate = lower_bounds_min_rate;
-    cpi->twopass.bits_left += (int64_t)(two_pass_min_rate /
-                                        cpi->oxcf.framerate);
-  }
 }
diff --git a/vp9/encoder/vp9_mbgraph.c b/vp9/encoder/vp9_mbgraph.c
index 32ab3fc..a9e0dfd 100644
--- a/vp9/encoder/vp9_mbgraph.c
+++ b/vp9/encoder/vp9_mbgraph.c
@@ -22,7 +22,7 @@
 
 
 static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi,
-                                              int_mv *ref_mv,
+                                              const MV *ref_mv,
                                               int_mv *dst_mv,
                                               int mb_row,
                                               int mb_col) {
@@ -42,15 +42,14 @@
       (cpi->speed < 8 ? (cpi->speed > 5 ? 1 : 0) : 2);
   step_param = MIN(step_param, (cpi->sf.max_step_search_steps - 2));
 
-  vp9_set_mv_search_range(x, &ref_mv->as_mv);
+  vp9_set_mv_search_range(x, ref_mv);
 
-  ref_full.as_mv.col = ref_mv->as_mv.col >> 3;
-  ref_full.as_mv.row = ref_mv->as_mv.row >> 3;
+  ref_full.as_mv.col = ref_mv->col >> 3;
+  ref_full.as_mv.row = ref_mv->row >> 3;
 
   /*cpi->sf.search_method == HEX*/
   best_err = vp9_hex_search(x, &ref_full.as_mv, step_param, x->errorperbit,
-                            0, &v_fn_ptr,
-                            0, &ref_mv->as_mv, &dst_mv->as_mv);
+                            0, &v_fn_ptr, 0, ref_mv, &dst_mv->as_mv);
 
   // Try sub-pixel MC
   // if (bestsme > error_thresh && bestsme < INT_MAX)
@@ -59,7 +58,7 @@
     unsigned int sse;
     best_err = cpi->find_fractional_mv_step(
         x,
-        &dst_mv->as_mv, &ref_mv->as_mv,
+        &dst_mv->as_mv, ref_mv,
         cpi->common.allow_high_precision_mv,
         x->errorperbit, &v_fn_ptr,
         0, cpi->sf.subpel_iters_per_step, NULL, NULL,
@@ -81,8 +80,8 @@
   return best_err;
 }
 
-static int do_16x16_motion_search(VP9_COMP *cpi, int_mv *ref_mv, int_mv *dst_mv,
-                                  int mb_row, int mb_col) {
+static int do_16x16_motion_search(VP9_COMP *cpi, const int_mv *ref_mv,
+                                  int_mv *dst_mv, int mb_row, int mb_col) {
   MACROBLOCK *const x = &cpi->mb;
   MACROBLOCKD *const xd = &x->e_mbd;
   unsigned int err, tmp_err;
@@ -97,7 +96,8 @@
 
   // Test last reference frame using the previous best mv as the
   // starting point (best reference) for the search
-  tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
+  tmp_err = do_16x16_motion_iteration(cpi, &ref_mv->as_mv, &tmp_mv,
+                                      mb_row, mb_col);
   if (tmp_err < err) {
     err = tmp_err;
     dst_mv->as_int = tmp_mv.as_int;
@@ -110,7 +110,7 @@
     int_mv zero_ref_mv, tmp_mv;
 
     zero_ref_mv.as_int = 0;
-    tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv,
+    tmp_err = do_16x16_motion_iteration(cpi, &zero_ref_mv.as_mv, &tmp_mv,
                                         mb_row, mb_col);
     if (tmp_err < err) {
       dst_mv->as_int = tmp_mv.as_int;
diff --git a/vp9/encoder/vp9_mcomp.c b/vp9/encoder/vp9_mcomp.c
index 5e74d1a..c199dff 100644
--- a/vp9/encoder/vp9_mcomp.c
+++ b/vp9/encoder/vp9_mcomp.c
@@ -23,7 +23,7 @@
 
 // #define NEW_DIAMOND_SEARCH
 
-void vp9_set_mv_search_range(MACROBLOCK *x, MV *mv) {
+void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
   const int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
   const int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
   const int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
@@ -464,20 +464,20 @@
 #undef CHECK_BETTER
 #undef SP
 
-static INLINE int check_bounds(MACROBLOCK *x, int row, int col, int range) {
+static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
+                               int range) {
   return ((row - range) >= x->mv_row_min) &
          ((row + range) <= x->mv_row_max) &
          ((col - range) >= x->mv_col_min) &
          ((col + range) <= x->mv_col_max);
 }
 
-#define CHECK_POINT \
-  {\
-    if (this_mv.col < x->mv_col_min) continue;\
-    if (this_mv.col > x->mv_col_max) continue;\
-    if (this_mv.row < x->mv_row_min) continue;\
-    if (this_mv.row > x->mv_row_max) continue;\
-  }
+static INLINE int check_point(const MACROBLOCK *x, const MV *mv) {
+  return (mv->col < x->mv_col_min) |
+         (mv->col > x->mv_col_max) |
+         (mv->row < x->mv_row_min) |
+         (mv->row > x->mv_row_max);
+}
 
 #define CHECK_BETTER \
   {\
@@ -580,7 +580,8 @@
         for (i = 0; i < num_candidates[t]; i++) {
           this_mv.row = br + candidates[t][i].row;
           this_mv.col = bc + candidates[t][i].col;
-          CHECK_POINT
+          if (check_point(x, &this_mv))
+            continue;
           this_offset = base_offset + (this_mv.row * in_what_stride) +
                                        this_mv.col;
           thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
@@ -623,7 +624,8 @@
           for (i = 0; i < num_candidates[s]; i++) {
             this_mv.row = br + candidates[s][i].row;
             this_mv.col = bc + candidates[s][i].col;
-            CHECK_POINT
+            if (check_point(x, &this_mv))
+              continue;
             this_offset = base_offset + (this_mv.row * in_what_stride) +
                                          this_mv.col;
             thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
@@ -659,7 +661,8 @@
           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
             this_mv.row = br + candidates[s][next_chkpts_indices[i]].row;
             this_mv.col = bc + candidates[s][next_chkpts_indices[i]].col;
-            CHECK_POINT
+            if (check_point(x, &this_mv))
+              continue;
             this_offset = base_offset + (this_mv.row * (in_what_stride)) +
                                          this_mv.col;
             thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
@@ -699,7 +702,8 @@
         for (i = 0; i < 4; i++) {
           this_mv.row = br + neighbors[i].row;
           this_mv.col = bc + neighbors[i].col;
-          CHECK_POINT
+          if (check_point(x, &this_mv))
+            continue;
           this_offset = base_offset + (this_mv.row * (in_what_stride)) +
                                        this_mv.col;
           thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
@@ -852,7 +856,6 @@
                             square_num_candidates, square_candidates);
 };
 
-#undef CHECK_POINT
 #undef CHECK_BETTER
 
 int vp9_full_range_search_c(MACROBLOCK *x, MV *ref_mv, MV *best_mv,
@@ -1303,16 +1306,16 @@
               refining search  */
 
 int vp9_full_pixel_diamond(VP9_COMP *cpi, MACROBLOCK *x,
-                           int_mv *mvp_full, int step_param,
+                           MV *mvp_full, int step_param,
                            int sadpb, int further_steps,
                            int do_refine, vp9_variance_fn_ptr_t *fn_ptr,
-                           int_mv *ref_mv, int_mv *dst_mv) {
+                           const MV *ref_mv, int_mv *dst_mv) {
   int_mv temp_mv;
   int thissme, n, num00;
-  int bestsme = cpi->diamond_search_sad(x, &mvp_full->as_mv, &temp_mv.as_mv,
+  int bestsme = cpi->diamond_search_sad(x, mvp_full, &temp_mv.as_mv,
                                         step_param, sadpb, &num00,
                                         fn_ptr, x->nmvjointcost,
-                                        x->mvcost, &ref_mv->as_mv);
+                                        x->mvcost, ref_mv);
   dst_mv->as_int = temp_mv.as_int;
 
   n = num00;
@@ -1329,10 +1332,10 @@
     if (num00) {
       num00--;
     } else {
-      thissme = cpi->diamond_search_sad(x, &mvp_full->as_mv, &temp_mv.as_mv,
+      thissme = cpi->diamond_search_sad(x, mvp_full, &temp_mv.as_mv,
                                         step_param + n, sadpb, &num00,
                                         fn_ptr, x->nmvjointcost, x->mvcost,
-                                        &ref_mv->as_mv);
+                                        ref_mv);
 
       /* check to see if refining search is needed. */
       if (num00 > (further_steps - n))
@@ -1352,7 +1355,7 @@
     best_mv.as_int = dst_mv->as_int;
     thissme = cpi->refining_search_sad(x, &best_mv.as_mv, sadpb, search_range,
                                        fn_ptr, x->nmvjointcost, x->mvcost,
-                                       &ref_mv->as_mv);
+                                       ref_mv);
 
     if (thissme < bestsme) {
       bestsme = thissme;
diff --git a/vp9/encoder/vp9_mcomp.h b/vp9/encoder/vp9_mcomp.h
index 1cb2977..b3d8975 100644
--- a/vp9/encoder/vp9_mcomp.h
+++ b/vp9/encoder/vp9_mcomp.h
@@ -28,7 +28,7 @@
 #define BORDER_MV_PIXELS_B16 (16 + VP9_INTERP_EXTEND)
 
 
-void vp9_set_mv_search_range(MACROBLOCK *x, MV *mv);
+void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv);
 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
                     const int *mvjcost, int *mvcost[2], int weight);
 void vp9_init_dsmotion_compensation(MACROBLOCK *x, int stride);
@@ -39,10 +39,10 @@
 
 // Runs sequence of diamond searches in smaller steps for RD
 int vp9_full_pixel_diamond(struct VP9_COMP *cpi, MACROBLOCK *x,
-                           int_mv *mvp_full, int step_param,
+                           MV *mvp_full, int step_param,
                            int sadpb, int further_steps, int do_refine,
                            vp9_variance_fn_ptr_t *fn_ptr,
-                           int_mv *ref_mv, int_mv *dst_mv);
+                           const MV *ref_mv, int_mv *dst_mv);
 
 int vp9_hex_search(MACROBLOCK *x,
                    MV *ref_mv,
diff --git a/vp9/encoder/vp9_onyx_int.h b/vp9/encoder/vp9_onyx_int.h
index f5b6562..d32711c 100644
--- a/vp9/encoder/vp9_onyx_int.h
+++ b/vp9/encoder/vp9_onyx_int.h
@@ -525,6 +525,8 @@
     int64_t bits_left;
     int64_t clip_bits_total;
     double avg_iiratio;
+    double modified_error_min;
+    double modified_error_max;
     double modified_error_total;
     double modified_error_left;
     double kf_intra_err_min;
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index 22e480b..46fe163 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -1742,7 +1742,7 @@
           int further_steps;
           int thissme, bestsme = INT_MAX;
           int sadpb = x->sadperbit4;
-          int_mv mvp_full;
+          MV mvp_full;
           int max_mv;
 
           /* Is the best so far sufficiently good that we cant justify doing
@@ -1773,12 +1773,12 @@
             step_param = cpi->mv_step_param;
           }
 
-          mvp_full.as_mv.row = bsi->mvp.as_mv.row >> 3;
-          mvp_full.as_mv.col = bsi->mvp.as_mv.col >> 3;
+          mvp_full.row = bsi->mvp.as_mv.row >> 3;
+          mvp_full.col = bsi->mvp.as_mv.col >> 3;
 
           if (cpi->sf.adaptive_motion_search && cpi->common.show_frame) {
-            mvp_full.as_mv.row = x->pred_mv[mbmi->ref_frame[0]].as_mv.row >> 3;
-            mvp_full.as_mv.col = x->pred_mv[mbmi->ref_frame[0]].as_mv.col >> 3;
+            mvp_full.row = x->pred_mv[mbmi->ref_frame[0]].as_mv.row >> 3;
+            mvp_full.col = x->pred_mv[mbmi->ref_frame[0]].as_mv.col >> 3;
             step_param = MAX(step_param, 8);
           }
 
@@ -1789,19 +1789,19 @@
           vp9_set_mv_search_range(x, &bsi->ref_mv->as_mv);
 
           if (cpi->sf.search_method == HEX) {
-            bestsme = vp9_hex_search(x, &mvp_full.as_mv,
+            bestsme = vp9_hex_search(x, &mvp_full,
                                      step_param,
                                      sadpb, 1, v_fn_ptr, 1,
                                      &bsi->ref_mv->as_mv,
                                      &mode_mv[NEWMV].as_mv);
           } else if (cpi->sf.search_method == SQUARE) {
-            bestsme = vp9_square_search(x, &mvp_full.as_mv,
+            bestsme = vp9_square_search(x, &mvp_full,
                                         step_param,
                                         sadpb, 1, v_fn_ptr, 1,
                                         &bsi->ref_mv->as_mv,
                                         &mode_mv[NEWMV].as_mv);
           } else if (cpi->sf.search_method == BIGDIA) {
-            bestsme = vp9_bigdia_search(x, &mvp_full.as_mv,
+            bestsme = vp9_bigdia_search(x, &mvp_full,
                                         step_param,
                                         sadpb, 1, v_fn_ptr, 1,
                                         &bsi->ref_mv->as_mv,
@@ -1809,16 +1809,17 @@
           } else {
             bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param,
                                              sadpb, further_steps, 0, v_fn_ptr,
-                                             bsi->ref_mv, &mode_mv[NEWMV]);
+                                             &bsi->ref_mv->as_mv,
+                                             &mode_mv[NEWMV]);
           }
 
           // Should we do a full search (best quality only)
           if (cpi->compressor_speed == 0) {
             /* Check if mvp_full is within the range. */
-            clamp_mv(&mvp_full.as_mv, x->mv_col_min, x->mv_col_max,
+            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, &mvp_full.as_mv,
+            thissme = cpi->full_search_sad(x, &mvp_full,
                                            sadpb, 16, v_fn_ptr,
                                            x->nmvjointcost, x->mvcost,
                                            &bsi->ref_mv->as_mv, i);
@@ -2439,10 +2440,10 @@
                                 &cpi->fn_ptr[bsize], 1,
                                 &ref_mv.as_mv, &tmp_mv->as_mv);
   } else {
-    bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param,
+    bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full.as_mv, step_param,
                                      sadpb, further_steps, 1,
                                      &cpi->fn_ptr[bsize],
-                                     &ref_mv, tmp_mv);
+                                     &ref_mv.as_mv, tmp_mv);
   }
 
   x->mv_col_min = tmp_col_min;