| Index: source/libvpx/vp9/encoder/vp9_mcomp.c
 | 
| ===================================================================
 | 
| --- source/libvpx/vp9/encoder/vp9_mcomp.c	(revision 263011)
 | 
| +++ source/libvpx/vp9/encoder/vp9_mcomp.c	(working copy)
 | 
| @@ -23,6 +23,11 @@
 | 
|  
 | 
|  // #define NEW_DIAMOND_SEARCH
 | 
|  
 | 
| +static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
 | 
| +                                             const MV *mv) {
 | 
| +  return &buf->buf[mv->row * buf->stride + mv->col];
 | 
| +}
 | 
| +
 | 
|  void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
 | 
|    int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
 | 
|    int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
 | 
| @@ -98,42 +103,23 @@
 | 
|  }
 | 
|  
 | 
|  void vp9_init_dsmotion_compensation(MACROBLOCK *x, int stride) {
 | 
| -  int len;
 | 
| -  int search_site_count = 0;
 | 
| +  int len, ss_count = 1;
 | 
|  
 | 
| -  // Generate offsets for 4 search sites per step.
 | 
| -  x->ss[search_site_count].mv.col = 0;
 | 
| -  x->ss[search_site_count].mv.row = 0;
 | 
| -  x->ss[search_site_count].offset = 0;
 | 
| -  search_site_count++;
 | 
| +  x->ss[0].mv.col = x->ss[0].mv.row = 0;
 | 
| +  x->ss[0].offset = 0;
 | 
|  
 | 
|    for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
 | 
| -    // Compute offsets for search sites.
 | 
| -    x->ss[search_site_count].mv.col = 0;
 | 
| -    x->ss[search_site_count].mv.row = -len;
 | 
| -    x->ss[search_site_count].offset = -len * stride;
 | 
| -    search_site_count++;
 | 
| -
 | 
| -    // Compute offsets for search sites.
 | 
| -    x->ss[search_site_count].mv.col = 0;
 | 
| -    x->ss[search_site_count].mv.row = len;
 | 
| -    x->ss[search_site_count].offset = len * stride;
 | 
| -    search_site_count++;
 | 
| -
 | 
| -    // Compute offsets for search sites.
 | 
| -    x->ss[search_site_count].mv.col = -len;
 | 
| -    x->ss[search_site_count].mv.row = 0;
 | 
| -    x->ss[search_site_count].offset = -len;
 | 
| -    search_site_count++;
 | 
| -
 | 
| -    // Compute offsets for search sites.
 | 
| -    x->ss[search_site_count].mv.col = len;
 | 
| -    x->ss[search_site_count].mv.row = 0;
 | 
| -    x->ss[search_site_count].offset = len;
 | 
| -    search_site_count++;
 | 
| +    // Generate offsets for 4 search sites per step.
 | 
| +    const MV ss_mvs[] = {{-len, 0}, {len, 0}, {0, -len}, {0, len}};
 | 
| +    int i;
 | 
| +    for (i = 0; i < 4; ++i) {
 | 
| +      search_site *const ss = &x->ss[ss_count++];
 | 
| +      ss->mv = ss_mvs[i];
 | 
| +      ss->offset = ss->mv.row * stride + ss->mv.col;
 | 
| +    }
 | 
|    }
 | 
|  
 | 
| -  x->ss_count = search_site_count;
 | 
| +  x->ss_count = ss_count;
 | 
|    x->searches_per_step = 4;
 | 
|  }
 | 
|  
 | 
| @@ -389,9 +375,9 @@
 | 
|    unsigned int sse;
 | 
|    unsigned int whichdir;
 | 
|    int thismse;
 | 
| -  unsigned int halfiters = iters_per_step;
 | 
| -  unsigned int quarteriters = iters_per_step;
 | 
| -  unsigned int eighthiters = iters_per_step;
 | 
| +  const unsigned int halfiters = iters_per_step;
 | 
| +  const unsigned int quarteriters = iters_per_step;
 | 
| +  const unsigned int eighthiters = iters_per_step;
 | 
|  
 | 
|    DECLARE_ALIGNED_ARRAY(16, uint8_t, comp_pred, 64 * 64);
 | 
|    const int y_stride = xd->plane[0].pre[0].stride;
 | 
| @@ -418,7 +404,7 @@
 | 
|    // calculate central point error
 | 
|    // TODO(yunqingwang): central pointer error was already calculated in full-
 | 
|    // pixel search, and can be passed in this function.
 | 
| -  comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
 | 
| +  vp9_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
 | 
|    besterr = vfp->vf(comp_pred, w, z, src_stride, sse1);
 | 
|    *distortion = besterr;
 | 
|    besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
 | 
| @@ -514,8 +500,7 @@
 | 
|                                MV *ref_mv,
 | 
|                                int search_param,
 | 
|                                int sad_per_bit,
 | 
| -                              int do_init_search,
 | 
| -                              int do_refine,
 | 
| +                              int do_init_search, int do_refine,
 | 
|                                const vp9_variance_fn_ptr_t *vfp,
 | 
|                                int use_mvcost,
 | 
|                                const MV *center_mv, MV *best_mv,
 | 
| @@ -527,20 +512,15 @@
 | 
|      10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
 | 
|    };
 | 
|    int i, j, s, t;
 | 
| -  const uint8_t *what = x->plane[0].src.buf;
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    int br, bc;
 | 
| -  MV this_mv;
 | 
|    int bestsad = INT_MAX;
 | 
|    int thissad;
 | 
| -  const uint8_t *base_offset;
 | 
| -  const uint8_t *this_offset;
 | 
|    int k = -1;
 | 
| -  int best_site = -1;
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
|    int best_init_s = search_param_to_steps[search_param];
 | 
| -  const int *mvjsadcost = x->nmvjointsadcost;
 | 
| +  const int *const mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
|  
 | 
|    // adjust ref_mv to make sure it is within MV range
 | 
| @@ -549,13 +529,10 @@
 | 
|    bc = ref_mv->col;
 | 
|  
 | 
|    // Work out the start point for the search
 | 
| -  base_offset = xd->plane[0].pre[0].buf;
 | 
| -  this_offset = base_offset + (br * in_what_stride) + bc;
 | 
| -  this_mv.row = br;
 | 
| -  this_mv.col = bc;
 | 
| -  bestsad = vfp->sdf(what, what_stride, this_offset, in_what_stride, 0x7fffffff)
 | 
| -                + mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                 mvjsadcost, mvsadcost, sad_per_bit);
 | 
| +  bestsad = vfp->sdf(what->buf, what->stride,
 | 
| +                     get_buf_from_mv(in_what, ref_mv), in_what->stride,
 | 
| +                     0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv,
 | 
| +                         mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
|    // Search all possible scales upto the search param around the center point
 | 
|    // pick the scale of the point that is best as the starting scale of
 | 
| @@ -564,27 +541,25 @@
 | 
|      s = best_init_s;
 | 
|      best_init_s = -1;
 | 
|      for (t = 0; t <= s; ++t) {
 | 
| -      best_site = -1;
 | 
| +      int best_site = -1;
 | 
|        if (check_bounds(x, br, bc, 1 << t)) {
 | 
|          for (i = 0; i < num_candidates[t]; i++) {
 | 
| -          this_mv.row = br + candidates[t][i].row;
 | 
| -          this_mv.col = bc + candidates[t][i].col;
 | 
| -          this_offset = base_offset + (this_mv.row * in_what_stride) +
 | 
| -                                       this_mv.col;
 | 
| -          thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                             bestsad);
 | 
| +          const MV this_mv = {br + candidates[t][i].row,
 | 
| +                              bc + candidates[t][i].col};
 | 
| +          thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                             get_buf_from_mv(in_what, &this_mv),
 | 
| +                             in_what->stride, bestsad);
 | 
|            CHECK_BETTER
 | 
|          }
 | 
|        } else {
 | 
|          for (i = 0; i < num_candidates[t]; i++) {
 | 
| -          this_mv.row = br + candidates[t][i].row;
 | 
| -          this_mv.col = bc + candidates[t][i].col;
 | 
| +          const MV this_mv = {br + candidates[t][i].row,
 | 
| +                              bc + candidates[t][i].col};
 | 
|            if (!is_mv_in(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,
 | 
| -                             bestsad);
 | 
| +          thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                             get_buf_from_mv(in_what, &this_mv),
 | 
| +                             in_what->stride, bestsad);
 | 
|            CHECK_BETTER
 | 
|          }
 | 
|        }
 | 
| @@ -604,31 +579,30 @@
 | 
|    // If the center point is still the best, just skip this and move to
 | 
|    // the refinement step.
 | 
|    if (best_init_s != -1) {
 | 
| +    int best_site = -1;
 | 
|      s = best_init_s;
 | 
| -    best_site = -1;
 | 
| +
 | 
|      do {
 | 
|        // No need to search all 6 points the 1st time if initial search was used
 | 
|        if (!do_init_search || s != best_init_s) {
 | 
|          if (check_bounds(x, br, bc, 1 << s)) {
 | 
|            for (i = 0; i < num_candidates[s]; i++) {
 | 
| -            this_mv.row = br + candidates[s][i].row;
 | 
| -            this_mv.col = bc + candidates[s][i].col;
 | 
| -            this_offset = base_offset + (this_mv.row * in_what_stride) +
 | 
| -                                         this_mv.col;
 | 
| -            thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                               bestsad);
 | 
| +            const MV this_mv = {br + candidates[s][i].row,
 | 
| +                                bc + candidates[s][i].col};
 | 
| +            thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                               get_buf_from_mv(in_what, &this_mv),
 | 
| +                               in_what->stride, bestsad);
 | 
|              CHECK_BETTER
 | 
|            }
 | 
|          } else {
 | 
|            for (i = 0; i < num_candidates[s]; i++) {
 | 
| -            this_mv.row = br + candidates[s][i].row;
 | 
| -            this_mv.col = bc + candidates[s][i].col;
 | 
| +            const MV this_mv = {br + candidates[s][i].row,
 | 
| +                                bc + candidates[s][i].col};
 | 
|              if (!is_mv_in(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,
 | 
| -                               bestsad);
 | 
| +            thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                               get_buf_from_mv(in_what, &this_mv),
 | 
| +                               in_what->stride, bestsad);
 | 
|              CHECK_BETTER
 | 
|            }
 | 
|          }
 | 
| @@ -651,24 +625,22 @@
 | 
|  
 | 
|          if (check_bounds(x, br, bc, 1 << s)) {
 | 
|            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;
 | 
| -            this_offset = base_offset + (this_mv.row * (in_what_stride)) +
 | 
| -                                         this_mv.col;
 | 
| -            thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                               bestsad);
 | 
| +            const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
 | 
| +                                bc + candidates[s][next_chkpts_indices[i]].col};
 | 
| +            thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                               get_buf_from_mv(in_what, &this_mv),
 | 
| +                               in_what->stride, bestsad);
 | 
|              CHECK_BETTER
 | 
|            }
 | 
|          } else {
 | 
|            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;
 | 
| +            const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
 | 
| +                                bc + candidates[s][next_chkpts_indices[i]].col};
 | 
|              if (!is_mv_in(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,
 | 
| -                               bestsad);
 | 
| +            thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                               get_buf_from_mv(in_what, &this_mv),
 | 
| +                               in_what->stride, bestsad);
 | 
|              CHECK_BETTER
 | 
|            }
 | 
|          }
 | 
| @@ -685,29 +657,28 @@
 | 
|    // Check 4 1-away neighbors if do_refine is true.
 | 
|    // For most well-designed schemes do_refine will not be necessary.
 | 
|    if (do_refine) {
 | 
| -    static const MV neighbors[4] = { {0, -1}, { -1, 0}, {1, 0}, {0, 1} };
 | 
| +    static const MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}};
 | 
| +
 | 
|      for (j = 0; j < 16; j++) {
 | 
| -      best_site = -1;
 | 
| +      int best_site = -1;
 | 
|        if (check_bounds(x, br, bc, 1)) {
 | 
|          for (i = 0; i < 4; i++) {
 | 
| -          this_mv.row = br + neighbors[i].row;
 | 
| -          this_mv.col = bc + neighbors[i].col;
 | 
| -          this_offset = base_offset + this_mv.row * in_what_stride +
 | 
| -                            this_mv.col;
 | 
| -          thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                             bestsad);
 | 
| +          const MV this_mv = {br + neighbors[i].row,
 | 
| +                              bc + neighbors[i].col};
 | 
| +          thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                             get_buf_from_mv(in_what, &this_mv),
 | 
| +                             in_what->stride, bestsad);
 | 
|            CHECK_BETTER
 | 
|          }
 | 
|        } else {
 | 
|          for (i = 0; i < 4; i++) {
 | 
| -          this_mv.row = br + neighbors[i].row;
 | 
| -          this_mv.col = bc + neighbors[i].col;
 | 
| +          const MV this_mv = {br + neighbors[i].row,
 | 
| +                              bc + neighbors[i].col};
 | 
|            if (!is_mv_in(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,
 | 
| -                             bestsad);
 | 
| +          thissad = vfp->sdf(what->buf, what->stride,
 | 
| +                             get_buf_from_mv(in_what, &this_mv),
 | 
| +                             in_what->stride, bestsad);
 | 
|            CHECK_BETTER
 | 
|          }
 | 
|        }
 | 
| @@ -724,21 +695,42 @@
 | 
|    best_mv->row = br;
 | 
|    best_mv->col = bc;
 | 
|  
 | 
| -  this_offset = base_offset + (best_mv->row * in_what_stride) +
 | 
| -                               best_mv->col;
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -  if (bestsad == INT_MAX)
 | 
| -    return INT_MAX;
 | 
| +  return bestsad;
 | 
| +}
 | 
|  
 | 
| -  return vfp->vf(what, what_stride, this_offset, in_what_stride,
 | 
| -                 (unsigned int *)&bestsad) +
 | 
| -         use_mvcost ? mv_err_cost(&this_mv, center_mv,
 | 
| -                                  x->nmvjointcost, x->mvcost, x->errorperbit)
 | 
| -                    : 0;
 | 
| +int vp9_get_mvpred_var(const MACROBLOCK *x,
 | 
| +                       const MV *best_mv, const MV *center_mv,
 | 
| +                       const vp9_variance_fn_ptr_t *vfp,
 | 
| +                       int use_mvcost) {
 | 
| +  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
| +  const MV mv = {best_mv->row * 8, best_mv->col * 8};
 | 
| +  unsigned int unused;
 | 
| +
 | 
| +  return vfp->vf(what->buf, what->stride,
 | 
| +                 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
 | 
| +      (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
 | 
| +                                 x->mvcost, x->errorperbit) : 0);
 | 
|  }
 | 
|  
 | 
| +int vp9_get_mvpred_av_var(const MACROBLOCK *x,
 | 
| +                          const MV *best_mv, const MV *center_mv,
 | 
| +                          const uint8_t *second_pred,
 | 
| +                          const vp9_variance_fn_ptr_t *vfp,
 | 
| +                          int use_mvcost) {
 | 
| +  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
| +  const MV mv = {best_mv->row * 8, best_mv->col * 8};
 | 
| +  unsigned int unused;
 | 
|  
 | 
| +  return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
 | 
| +                   what->buf, what->stride, &unused, second_pred) +
 | 
| +      (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
 | 
| +                                 x->mvcost, x->errorperbit) : 0);
 | 
| +}
 | 
| +
 | 
|  int vp9_hex_search(const MACROBLOCK *x,
 | 
|                     MV *ref_mv,
 | 
|                     int search_param,
 | 
| @@ -853,184 +845,34 @@
 | 
|                              do_init_search, 0, vfp, use_mvcost,
 | 
|                              center_mv, best_mv,
 | 
|                              square_num_candidates, square_candidates);
 | 
| -};
 | 
| +}
 | 
|  
 | 
| -// Number of candidates in first hex search
 | 
| -#define FIRST_HEX_CANDIDATES 6
 | 
| -// Index of previous hex search's best match
 | 
| -#define PRE_BEST_CANDIDATE 6
 | 
| -// Number of candidates in following hex search
 | 
| -#define NEXT_HEX_CANDIDATES 3
 | 
| -// Number of candidates in refining search
 | 
| -#define REFINE_CANDIDATES 4
 | 
| -
 | 
|  int vp9_fast_hex_search(const MACROBLOCK *x,
 | 
|                          MV *ref_mv,
 | 
|                          int search_param,
 | 
|                          int sad_per_bit,
 | 
| +                        int do_init_search,  // must be zero for fast_hex
 | 
|                          const vp9_variance_fn_ptr_t *vfp,
 | 
|                          int use_mvcost,
 | 
|                          const MV *center_mv,
 | 
|                          MV *best_mv) {
 | 
| -  const MACROBLOCKD* const xd = &x->e_mbd;
 | 
| -  static const MV hex[FIRST_HEX_CANDIDATES] = {
 | 
| -    { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}
 | 
| -  };
 | 
| -  static const MV next_chkpts[PRE_BEST_CANDIDATE][NEXT_HEX_CANDIDATES] = {
 | 
| -    {{ -2, 0}, { -1, -2}, {1, -2}},
 | 
| -    {{ -1, -2}, {1, -2}, {2, 0}},
 | 
| -    {{1, -2}, {2, 0}, {1, 2}},
 | 
| -    {{2, 0}, {1, 2}, { -1, 2}},
 | 
| -    {{1, 2}, { -1, 2}, { -2, 0}},
 | 
| -    {{ -1, 2}, { -2, 0}, { -1, -2}}
 | 
| -  };
 | 
| -  static const MV neighbors[REFINE_CANDIDATES] = {
 | 
| -      {0, -1}, { -1, 0}, {1, 0}, {0, 1}
 | 
| -  };
 | 
| -  int i, j;
 | 
| +  return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
 | 
| +                        sad_per_bit, do_init_search, vfp, use_mvcost,
 | 
| +                        center_mv, best_mv);
 | 
| +}
 | 
|  
 | 
| -  const uint8_t *what = x->plane[0].src.buf;
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  int br, bc;
 | 
| -  MV this_mv;
 | 
| -  unsigned int bestsad = 0x7fffffff;
 | 
| -  unsigned int thissad;
 | 
| -  const uint8_t *base_offset;
 | 
| -  const uint8_t *this_offset;
 | 
| -  int k = -1;
 | 
| -  int best_site = -1;
 | 
| -  const int max_hex_search = 512;
 | 
| -  const int max_dia_search = 32;
 | 
| -
 | 
| -  const int *mvjsadcost = x->nmvjointsadcost;
 | 
| -  int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
| -
 | 
| -  const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
| -
 | 
| -  // Adjust ref_mv to make sure it is within MV range
 | 
| -  clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
 | 
| -  br = ref_mv->row;
 | 
| -  bc = ref_mv->col;
 | 
| -
 | 
| -  // Check the start point
 | 
| -  base_offset = xd->plane[0].pre[0].buf;
 | 
| -  this_offset = base_offset + (br * in_what_stride) + bc;
 | 
| -  this_mv.row = br;
 | 
| -  this_mv.col = bc;
 | 
| -  bestsad = vfp->sdf(what, what_stride, this_offset, in_what_stride, 0x7fffffff)
 | 
| -            + mvsad_err_cost(&this_mv, &fcenter_mv, mvjsadcost, mvsadcost,
 | 
| -                             sad_per_bit);
 | 
| -
 | 
| -  // Initial 6-point hex search
 | 
| -  if (check_bounds(x, br, bc, 2)) {
 | 
| -    for (i = 0; i < FIRST_HEX_CANDIDATES; i++) {
 | 
| -      this_mv.row = br + hex[i].row;
 | 
| -      this_mv.col = bc + hex[i].col;
 | 
| -      this_offset = base_offset + (this_mv.row * in_what_stride) + this_mv.col;
 | 
| -      thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                         bestsad);
 | 
| -      CHECK_BETTER
 | 
| -    }
 | 
| -  } else {
 | 
| -    for (i = 0; i < FIRST_HEX_CANDIDATES; i++) {
 | 
| -      this_mv.row = br + hex[i].row;
 | 
| -      this_mv.col = bc + hex[i].col;
 | 
| -      if (!is_mv_in(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,
 | 
| -                         bestsad);
 | 
| -      CHECK_BETTER
 | 
| -    }
 | 
| -  }
 | 
| -
 | 
| -  // Continue hex search if we find a better match in first round
 | 
| -  if (best_site != -1) {
 | 
| -    br += hex[best_site].row;
 | 
| -    bc += hex[best_site].col;
 | 
| -    k = best_site;
 | 
| -
 | 
| -    // Allow search covering maximum MV range
 | 
| -    for (j = 1; j < max_hex_search; j++) {
 | 
| -      best_site = -1;
 | 
| -
 | 
| -      if (check_bounds(x, br, bc, 2)) {
 | 
| -        for (i = 0; i < 3; i++) {
 | 
| -          this_mv.row = br + next_chkpts[k][i].row;
 | 
| -          this_mv.col = bc + next_chkpts[k][i].col;
 | 
| -          this_offset = base_offset + (this_mv.row * in_what_stride) +
 | 
| -              this_mv.col;
 | 
| -          thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                             bestsad);
 | 
| -          CHECK_BETTER
 | 
| -        }
 | 
| -      } else {
 | 
| -        for (i = 0; i < 3; i++) {
 | 
| -          this_mv.row = br + next_chkpts[k][i].row;
 | 
| -          this_mv.col = bc + next_chkpts[k][i].col;
 | 
| -          if (!is_mv_in(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,
 | 
| -                             bestsad);
 | 
| -          CHECK_BETTER
 | 
| -        }
 | 
| -      }
 | 
| -
 | 
| -      if (best_site == -1) {
 | 
| -        break;
 | 
| -      } else {
 | 
| -        br += next_chkpts[k][best_site].row;
 | 
| -        bc += next_chkpts[k][best_site].col;
 | 
| -        k += 5 + best_site;
 | 
| -        if (k >= 12) k -= 12;
 | 
| -        else if (k >= 6) k -= 6;
 | 
| -      }
 | 
| -    }
 | 
| -  }
 | 
| -
 | 
| -  // Check 4 1-away neighbors
 | 
| -  for (j = 0; j < max_dia_search; j++) {
 | 
| -    best_site = -1;
 | 
| -
 | 
| -    if (check_bounds(x, br, bc, 1)) {
 | 
| -      for (i = 0; i < REFINE_CANDIDATES; i++) {
 | 
| -        this_mv.row = br + neighbors[i].row;
 | 
| -        this_mv.col = bc + neighbors[i].col;
 | 
| -        this_offset = base_offset + (this_mv.row * in_what_stride) +
 | 
| -            this_mv.col;
 | 
| -        thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride,
 | 
| -                           bestsad);
 | 
| -        CHECK_BETTER
 | 
| -      }
 | 
| -    } else {
 | 
| -      for (i = 0; i < REFINE_CANDIDATES; i++) {
 | 
| -        this_mv.row = br + neighbors[i].row;
 | 
| -        this_mv.col = bc + neighbors[i].col;
 | 
| -        if (!is_mv_in(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,
 | 
| -                           bestsad);
 | 
| -        CHECK_BETTER
 | 
| -      }
 | 
| -    }
 | 
| -
 | 
| -    if (best_site == -1) {
 | 
| -      break;
 | 
| -    } else {
 | 
| -      br += neighbors[best_site].row;
 | 
| -      bc += neighbors[best_site].col;
 | 
| -    }
 | 
| -  }
 | 
| -
 | 
| -  best_mv->row = br;
 | 
| -  best_mv->col = bc;
 | 
| -
 | 
| -  return bestsad;
 | 
| +int vp9_fast_dia_search(const MACROBLOCK *x,
 | 
| +                        MV *ref_mv,
 | 
| +                        int search_param,
 | 
| +                        int sad_per_bit,
 | 
| +                        int do_init_search,
 | 
| +                        const vp9_variance_fn_ptr_t *vfp,
 | 
| +                        int use_mvcost,
 | 
| +                        const MV *center_mv,
 | 
| +                        MV *best_mv) {
 | 
| +  return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
 | 
| +                           sad_per_bit, do_init_search, vfp, use_mvcost,
 | 
| +                           center_mv, best_mv);
 | 
|  }
 | 
|  
 | 
|  #undef CHECK_BETTER
 | 
| @@ -1045,10 +887,7 @@
 | 
|    const int what_stride = x->plane[0].src.stride;
 | 
|    const uint8_t *in_what;
 | 
|    const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  const uint8_t *best_address;
 | 
|  
 | 
| -  MV this_mv;
 | 
| -
 | 
|    unsigned int bestsad = INT_MAX;
 | 
|    int ref_row, ref_col;
 | 
|  
 | 
| @@ -1076,7 +915,6 @@
 | 
|  
 | 
|    // Work out the start point for the search
 | 
|    in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
 | 
| -  best_address = in_what;
 | 
|  
 | 
|    // Check the starting position
 | 
|    bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff)
 | 
| @@ -1100,8 +938,7 @@
 | 
|  
 | 
|          for (i = 0; i < 4; ++i) {
 | 
|            if (sad_array[i] < bestsad) {
 | 
| -            this_mv.row = ref_row + tr;
 | 
| -            this_mv.col = ref_col + tc + i;
 | 
| +            const MV this_mv = {ref_row + tr, ref_col + tc + i};
 | 
|              thissad = sad_array[i] +
 | 
|                        mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
|                                        mvjsadcost, mvsadcost, sad_per_bit);
 | 
| @@ -1119,8 +956,7 @@
 | 
|                                  bestsad);
 | 
|  
 | 
|            if (thissad < bestsad) {
 | 
| -            this_mv.row = ref_row + tr;
 | 
| -            this_mv.col = ref_col + tc + i;
 | 
| +            const MV this_mv = {ref_row + tr, ref_col + tc + i};
 | 
|              thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
|                                        mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
| @@ -1134,20 +970,9 @@
 | 
|        }
 | 
|      }
 | 
|    }
 | 
| -
 | 
|    best_mv->row += best_tr;
 | 
|    best_mv->col += best_tc;
 | 
| -
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad == INT_MAX)
 | 
| -    return INT_MAX;
 | 
| -
 | 
| -  return fn_ptr->vf(what, what_stride, best_address, in_what_stride,
 | 
| -                    (unsigned int *)(&thissad)) +
 | 
| -                       mv_err_cost(&this_mv, center_mv,
 | 
| -                                   mvjcost, mvcost, x->errorperbit);
 | 
| +  return bestsad;
 | 
|  }
 | 
|  
 | 
|  int vp9_diamond_search_sad_c(const MACROBLOCK *x,
 | 
| @@ -1156,77 +981,49 @@
 | 
|                               const vp9_variance_fn_ptr_t *fn_ptr,
 | 
|                               int *mvjcost, int *mvcost[2],
 | 
|                               const MV *center_mv) {
 | 
| -  int i, j, step;
 | 
| -
 | 
|    const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| -  const uint8_t *what = x->plane[0].src.buf;
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const uint8_t *in_what;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  const uint8_t *best_address;
 | 
| -
 | 
| -  MV this_mv;
 | 
| -
 | 
| -  int bestsad = INT_MAX;
 | 
| -  int best_site = 0;
 | 
| -  int last_site = 0;
 | 
| -
 | 
| -  int ref_row, ref_col;
 | 
| -  int this_row_offset, this_col_offset;
 | 
| -
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    // search_param determines the length of the initial step and hence the number
 | 
|    // of iterations
 | 
|    // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 =
 | 
|    // (MAX_FIRST_STEP/4) pel... etc.
 | 
|    const search_site *const ss = &x->ss[search_param * x->searches_per_step];
 | 
|    const int tot_steps = (x->ss_count / x->searches_per_step) - search_param;
 | 
| -
 | 
| -  int thissad;
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
| -
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
| +  const uint8_t *best_address;
 | 
| +  int best_sad = INT_MAX;
 | 
| +  int best_site = 0;
 | 
| +  int last_site = 0;
 | 
| +  int i, j, step;
 | 
|  
 | 
|    clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
 | 
| -  ref_row = ref_mv->row;
 | 
| -  ref_col = ref_mv->col;
 | 
| +  best_address = get_buf_from_mv(in_what, ref_mv);
 | 
|    *num00 = 0;
 | 
| -  best_mv->row = ref_row;
 | 
| -  best_mv->col = ref_col;
 | 
| +  *best_mv = *ref_mv;
 | 
|  
 | 
| -  // Work out the start point for the search
 | 
| -  in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
 | 
| -  best_address = in_what;
 | 
| -
 | 
|    // Check the starting position
 | 
| -  bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, 0x7fffffff)
 | 
| -                + mvsad_err_cost(best_mv, &fcenter_mv,
 | 
| -                                 mvjsadcost, mvsadcost, sad_per_bit);
 | 
| +  best_sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +                        in_what->buf, in_what->stride, 0x7fffffff) +
 | 
| +      mvsad_err_cost(best_mv, &fcenter_mv, mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
|    i = 1;
 | 
|  
 | 
|    for (step = 0; step < tot_steps; step++) {
 | 
|      for (j = 0; j < x->searches_per_step; j++) {
 | 
| -      // Trap illegal vectors
 | 
| -      this_row_offset = best_mv->row + ss[i].mv.row;
 | 
| -      this_col_offset = best_mv->col + ss[i].mv.col;
 | 
| -
 | 
| -      if ((this_col_offset > x->mv_col_min) &&
 | 
| -          (this_col_offset < x->mv_col_max) &&
 | 
| -          (this_row_offset > x->mv_row_min) &&
 | 
| -          (this_row_offset < x->mv_row_max)) {
 | 
| -        const uint8_t *const check_here = ss[i].offset + best_address;
 | 
| -        thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                              bestsad);
 | 
| -
 | 
| -        if (thissad < bestsad) {
 | 
| -          this_mv.row = this_row_offset;
 | 
| -          this_mv.col = this_col_offset;
 | 
| -          thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                    mvjsadcost, mvsadcost, sad_per_bit);
 | 
| -
 | 
| -          if (thissad < bestsad) {
 | 
| -            bestsad = thissad;
 | 
| +      const MV mv = {best_mv->row + ss[i].mv.row,
 | 
| +                     best_mv->col + ss[i].mv.col};
 | 
| +      if (is_mv_in(x, &mv)) {
 | 
| +       int sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +                             best_address + ss[i].offset, in_what->stride,
 | 
| +                             best_sad);
 | 
| +        if (sad < best_sad) {
 | 
| +          sad += mvsad_err_cost(&mv, &fcenter_mv, mvjsadcost, mvsadcost,
 | 
| +                                sad_per_bit);
 | 
| +          if (sad < best_sad) {
 | 
| +            best_sad = sad;
 | 
|              best_site = i;
 | 
|            }
 | 
|          }
 | 
| @@ -1242,22 +1039,17 @@
 | 
|        last_site = best_site;
 | 
|  #if defined(NEW_DIAMOND_SEARCH)
 | 
|        while (1) {
 | 
| -        this_row_offset = best_mv->row + ss[best_site].mv.row;
 | 
| -        this_col_offset = best_mv->col + ss[best_site].mv.col;
 | 
| -        if ((this_col_offset > x->mv_col_min) &&
 | 
| -            (this_col_offset < x->mv_col_max) &&
 | 
| -            (this_row_offset > x->mv_row_min) &&
 | 
| -            (this_row_offset < x->mv_row_max)) {
 | 
| -          check_here = ss[best_site].offset + best_address;
 | 
| -          thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                                bestsad);
 | 
| -          if (thissad < bestsad) {
 | 
| -            this_mv.row = this_row_offset;
 | 
| -            this_mv.col = this_col_offset;
 | 
| -            thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                      mvjsadcost, mvsadcost, sad_per_bit);
 | 
| -            if (thissad < bestsad) {
 | 
| -              bestsad = thissad;
 | 
| +        const MV this_mv = {best_mv->row + ss[best_site].mv.row,
 | 
| +                            best_mv->col + ss[best_site].mv.col};
 | 
| +        if (is_mv_in(x, &this_mv)) {
 | 
| +          int sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +                                best_address + ss[best_site].offset,
 | 
| +                                in_what->stride, best_sad);
 | 
| +          if (sad < best_sad) {
 | 
| +            sad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| +                                  mvjsadcost, mvsadcost, sad_per_bit);
 | 
| +            if (sad < best_sad) {
 | 
| +              best_sad = sad;
 | 
|                best_mv->row += ss[best_site].mv.row;
 | 
|                best_mv->col += ss[best_site].mv.col;
 | 
|                best_address += ss[best_site].offset;
 | 
| @@ -1268,21 +1060,11 @@
 | 
|          break;
 | 
|        };
 | 
|  #endif
 | 
| -    } else if (best_address == in_what) {
 | 
| +    } else if (best_address == in_what->buf) {
 | 
|        (*num00)++;
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad == INT_MAX)
 | 
| -    return INT_MAX;
 | 
| -
 | 
| -  return fn_ptr->vf(what, what_stride, best_address, in_what_stride,
 | 
| -                    (unsigned int *)(&thissad)) +
 | 
| -                       mv_err_cost(&this_mv, center_mv,
 | 
| -                                   mvjcost, mvcost, x->errorperbit);
 | 
| +  return best_sad;
 | 
|  }
 | 
|  
 | 
|  int vp9_diamond_search_sadx4(const MACROBLOCK *x,
 | 
| @@ -1300,16 +1082,12 @@
 | 
|    const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
|    const uint8_t *best_address;
 | 
|  
 | 
| -  MV this_mv;
 | 
| -
 | 
|    unsigned int bestsad = INT_MAX;
 | 
|    int best_site = 0;
 | 
|    int last_site = 0;
 | 
|  
 | 
|    int ref_row;
 | 
|    int ref_col;
 | 
| -  int this_row_offset;
 | 
| -  int this_col_offset;
 | 
|  
 | 
|    // search_param determines the length of the initial step and hence the number
 | 
|    // of iterations.
 | 
| @@ -1319,7 +1097,6 @@
 | 
|    const search_site *ss = &x->ss[search_param * x->searches_per_step];
 | 
|    const int tot_steps = (x->ss_count / x->searches_per_step) - search_param;
 | 
|  
 | 
| -  unsigned int thissad;
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
|  
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
| @@ -1370,8 +1147,8 @@
 | 
|  
 | 
|          for (t = 0; t < 4; t++, i++) {
 | 
|            if (sad_array[t] < bestsad) {
 | 
| -            this_mv.row = best_mv->row + ss[i].mv.row;
 | 
| -            this_mv.col = best_mv->col + ss[i].mv.col;
 | 
| +            const MV this_mv = {best_mv->row + ss[i].mv.row,
 | 
| +                                best_mv->col + ss[i].mv.col};
 | 
|              sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
|                                             mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
| @@ -1385,20 +1162,15 @@
 | 
|      } else {
 | 
|        for (j = 0; j < x->searches_per_step; j++) {
 | 
|          // Trap illegal vectors
 | 
| -        this_row_offset = best_mv->row + ss[i].mv.row;
 | 
| -        this_col_offset = best_mv->col + ss[i].mv.col;
 | 
| +        const MV this_mv = {best_mv->row + ss[i].mv.row,
 | 
| +                            best_mv->col + ss[i].mv.col};
 | 
|  
 | 
| -        if ((this_col_offset > x->mv_col_min) &&
 | 
| -            (this_col_offset < x->mv_col_max) &&
 | 
| -            (this_row_offset > x->mv_row_min) &&
 | 
| -            (this_row_offset < x->mv_row_max)) {
 | 
| +        if (is_mv_in(x, &this_mv)) {
 | 
|            const uint8_t *const check_here = ss[i].offset + best_address;
 | 
| -          thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                                bestsad);
 | 
| +          unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
 | 
| +                                             in_what_stride, bestsad);
 | 
|  
 | 
|            if (thissad < bestsad) {
 | 
| -            this_mv.row = this_row_offset;
 | 
| -            this_mv.col = this_col_offset;
 | 
|              thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
|                                        mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
| @@ -1418,18 +1190,13 @@
 | 
|        last_site = best_site;
 | 
|  #if defined(NEW_DIAMOND_SEARCH)
 | 
|        while (1) {
 | 
| -        this_row_offset = best_mv->row + ss[best_site].mv.row;
 | 
| -        this_col_offset = best_mv->col + ss[best_site].mv.col;
 | 
| -        if ((this_col_offset > x->mv_col_min) &&
 | 
| -            (this_col_offset < x->mv_col_max) &&
 | 
| -            (this_row_offset > x->mv_row_min) &&
 | 
| -            (this_row_offset < x->mv_row_max)) {
 | 
| -          check_here = ss[best_site].offset + best_address;
 | 
| -          thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                                bestsad);
 | 
| +        const MV this_mv = {best_mv->row + ss[best_site].mv.row,
 | 
| +                            best_mv->col + ss[best_site].mv.col};
 | 
| +        if (is_mv_in(x, &this_mv)) {
 | 
| +          const uint8_t *const check_here = ss[best_site].offset + best_address;
 | 
| +          unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
 | 
| +                                             in_what_stride, bestsad);
 | 
|            if (thissad < bestsad) {
 | 
| -            this_mv.row = this_row_offset;
 | 
| -            this_mv.col = this_col_offset;
 | 
|              thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
|                                        mvjsadcost, mvsadcost, sad_per_bit);
 | 
|              if (thissad < bestsad) {
 | 
| @@ -1448,24 +1215,14 @@
 | 
|        (*num00)++;
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad == INT_MAX)
 | 
| -    return INT_MAX;
 | 
| -
 | 
| -  return fn_ptr->vf(what, what_stride, best_address, in_what_stride,
 | 
| -                    (unsigned int *)(&thissad)) +
 | 
| -                    mv_err_cost(&this_mv, center_mv,
 | 
| -                                mvjcost, mvcost, x->errorperbit);
 | 
| +  return bestsad;
 | 
|  }
 | 
|  
 | 
|  /* do_refine: If last step (1-away) of n-step search doesn't pick the center
 | 
|                point as the best match, we will do a final 1-away diamond
 | 
|                refining search  */
 | 
|  
 | 
| -int vp9_full_pixel_diamond(VP9_COMP *cpi, MACROBLOCK *x,
 | 
| +int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
 | 
|                             MV *mvp_full, int step_param,
 | 
|                             int sadpb, int further_steps, int do_refine,
 | 
|                             const vp9_variance_fn_ptr_t *fn_ptr,
 | 
| @@ -1476,6 +1233,8 @@
 | 
|                                          step_param, sadpb, &n,
 | 
|                                          fn_ptr, x->nmvjointcost,
 | 
|                                          x->mvcost, ref_mv);
 | 
| +  if (bestsme < INT_MAX)
 | 
| +    bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
 | 
|    *dst_mv = temp_mv;
 | 
|  
 | 
|    // If there won't be more n-step search, check to see if refining search is
 | 
| @@ -1493,6 +1252,8 @@
 | 
|                                          step_param + n, sadpb, &num00,
 | 
|                                          fn_ptr, x->nmvjointcost, x->mvcost,
 | 
|                                          ref_mv);
 | 
| +      if (thissme < INT_MAX)
 | 
| +        thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
 | 
|  
 | 
|        // check to see if refining search is needed.
 | 
|        if (num00 > further_steps - n)
 | 
| @@ -1512,12 +1273,13 @@
 | 
|      thissme = cpi->refining_search_sad(x, &best_mv, sadpb, search_range,
 | 
|                                         fn_ptr, x->nmvjointcost, x->mvcost,
 | 
|                                         ref_mv);
 | 
| +    if (thissme < INT_MAX)
 | 
| +      thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
 | 
|      if (thissme < bestsme) {
 | 
|        bestsme = thissme;
 | 
|        *dst_mv = best_mv;
 | 
|      }
 | 
|    }
 | 
| -
 | 
|    return bestsme;
 | 
|  }
 | 
|  
 | 
| @@ -1528,10 +1290,8 @@
 | 
|                            const MV *center_mv, MV *best_mv) {
 | 
|    int r, c;
 | 
|    const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| -  const uint8_t *const what = x->plane[0].src.buf;
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const uint8_t *const in_what = xd->plane[0].pre[0].buf;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
 | 
|    const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
 | 
|    const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
 | 
| @@ -1539,38 +1299,26 @@
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
| -  const uint8_t *best_address = &in_what[ref_mv->row * in_what_stride +
 | 
| -                                         ref_mv->col];
 | 
| -  int best_sad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride,
 | 
| -                             0x7fffffff) +
 | 
| +  int best_sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +      get_buf_from_mv(in_what, ref_mv), in_what->stride, 0x7fffffff) +
 | 
|        mvsad_err_cost(ref_mv, &fcenter_mv, mvjsadcost, mvsadcost, sad_per_bit);
 | 
|    *best_mv = *ref_mv;
 | 
|  
 | 
|    for (r = row_min; r < row_max; ++r) {
 | 
|      for (c = col_min; c < col_max; ++c) {
 | 
| -      const MV this_mv = {r, c};
 | 
| -      const uint8_t *check_here = &in_what[r * in_what_stride + c];
 | 
| -      const int sad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                                  best_sad) +
 | 
| -          mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                         mvjsadcost, mvsadcost, sad_per_bit);
 | 
| +      const MV mv = {r, c};
 | 
| +      const int sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +          get_buf_from_mv(in_what, &mv), in_what->stride, best_sad) +
 | 
| +          mvsad_err_cost(&mv, &fcenter_mv, mvjsadcost, mvsadcost,
 | 
| +                         sad_per_bit);
 | 
|  
 | 
|        if (sad < best_sad) {
 | 
|          best_sad = sad;
 | 
| -        *best_mv = this_mv;
 | 
| -        best_address = check_here;
 | 
| +        *best_mv = mv;
 | 
|        }
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  if (best_sad < INT_MAX) {
 | 
| -    unsigned int unused;
 | 
| -    const MV mv = {best_mv->row * 8, best_mv->col * 8};
 | 
| -    return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &unused)
 | 
| -                + mv_err_cost(&mv, center_mv, mvjcost, mvcost, x->errorperbit);
 | 
| -  } else {
 | 
| -    return INT_MAX;
 | 
| -  }
 | 
| +  return best_sad;
 | 
|  }
 | 
|  
 | 
|  int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
 | 
| @@ -1635,10 +1383,8 @@
 | 
|              bestsad = thissad;
 | 
|              best_mv->row = r;
 | 
|              best_mv->col = c;
 | 
| -            bestaddress = check_here;
 | 
|            }
 | 
|          }
 | 
| -
 | 
|          check_here++;
 | 
|          c++;
 | 
|        }
 | 
| @@ -1657,7 +1403,6 @@
 | 
|            bestsad = thissad;
 | 
|            best_mv->row = r;
 | 
|            best_mv->col = c;
 | 
| -          bestaddress = check_here;
 | 
|          }
 | 
|        }
 | 
|  
 | 
| @@ -1665,17 +1410,7 @@
 | 
|        c++;
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad < INT_MAX)
 | 
| -    return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride,
 | 
| -                      (unsigned int *)(&thissad)) +
 | 
| -                      mv_err_cost(&this_mv, center_mv,
 | 
| -                                  mvjcost, mvcost, x->errorperbit);
 | 
| -  else
 | 
| -    return INT_MAX;
 | 
| +  return bestsad;
 | 
|  }
 | 
|  
 | 
|  int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
 | 
| @@ -1691,7 +1426,6 @@
 | 
|    MV this_mv;
 | 
|    unsigned int bestsad = INT_MAX;
 | 
|    int r, c;
 | 
| -  unsigned int thissad;
 | 
|    int ref_row = ref_mv->row;
 | 
|    int ref_col = ref_mv->col;
 | 
|  
 | 
| @@ -1731,7 +1465,7 @@
 | 
|        fn_ptr->sdx8f(what, what_stride, check_here, in_what_stride, sad_array8);
 | 
|  
 | 
|        for (i = 0; i < 8; i++) {
 | 
| -        thissad = (unsigned int)sad_array8[i];
 | 
| +        unsigned int thissad = (unsigned int)sad_array8[i];
 | 
|  
 | 
|          if (thissad < bestsad) {
 | 
|            this_mv.col = c;
 | 
| @@ -1742,7 +1476,6 @@
 | 
|              bestsad = thissad;
 | 
|              best_mv->row = r;
 | 
|              best_mv->col = c;
 | 
| -            bestaddress = check_here;
 | 
|            }
 | 
|          }
 | 
|  
 | 
| @@ -1757,18 +1490,17 @@
 | 
|        fn_ptr->sdx3f(what, what_stride, check_here, in_what_stride, sad_array);
 | 
|  
 | 
|        for (i = 0; i < 3; i++) {
 | 
| -        thissad = sad_array[i];
 | 
| +        unsigned int thissad = sad_array[i];
 | 
|  
 | 
|          if (thissad < bestsad) {
 | 
|            this_mv.col = c;
 | 
| -          thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                     mvjsadcost, mvsadcost, sad_per_bit);
 | 
| +          thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| +                                    mvjsadcost, mvsadcost, sad_per_bit);
 | 
|  
 | 
|            if (thissad < bestsad) {
 | 
|              bestsad = thissad;
 | 
|              best_mv->row = r;
 | 
|              best_mv->col = c;
 | 
| -            bestaddress = check_here;
 | 
|            }
 | 
|          }
 | 
|  
 | 
| @@ -1778,8 +1510,8 @@
 | 
|      }
 | 
|  
 | 
|      while (c < col_max) {
 | 
| -      thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                            bestsad);
 | 
| +      unsigned int thissad = fn_ptr->sdf(what, what_stride,
 | 
| +                                         check_here, in_what_stride, bestsad);
 | 
|  
 | 
|        if (thissad < bestsad) {
 | 
|          this_mv.col = c;
 | 
| @@ -1790,7 +1522,6 @@
 | 
|            bestsad = thissad;
 | 
|            best_mv->row = r;
 | 
|            best_mv->col = c;
 | 
| -          bestaddress = check_here;
 | 
|          }
 | 
|        }
 | 
|  
 | 
| @@ -1798,17 +1529,7 @@
 | 
|        c++;
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  this_mv.row = best_mv->row * 8;
 | 
| -  this_mv.col = best_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad < INT_MAX)
 | 
| -    return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride,
 | 
| -                      (unsigned int *)(&thissad)) +
 | 
| -                      mv_err_cost(&this_mv, center_mv,
 | 
| -                                  mvjcost, mvcost, x->errorperbit);
 | 
| -  else
 | 
| -    return INT_MAX;
 | 
| +  return bestsad;
 | 
|  }
 | 
|  
 | 
|  int vp9_refining_search_sad_c(const MACROBLOCK *x,
 | 
| @@ -1817,41 +1538,34 @@
 | 
|                                const vp9_variance_fn_ptr_t *fn_ptr,
 | 
|                                int *mvjcost, int *mvcost[2],
 | 
|                                const MV *center_mv) {
 | 
| -  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
|    const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
 | 
| -  int i, j;
 | 
| -
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const uint8_t *const what = x->plane[0].src.buf;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  const uint8_t *const in_what = xd->plane[0].pre[0].buf;
 | 
| -  const uint8_t *best_address = &in_what[ref_mv->row * in_what_stride +
 | 
| -                                             ref_mv->col];
 | 
| +  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
|  
 | 
| -  unsigned int bestsad = fn_ptr->sdf(what, what_stride, best_address,
 | 
| -                                     in_what_stride, 0x7fffffff) +
 | 
| +  unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +                                     get_buf_from_mv(in_what, ref_mv),
 | 
| +                                     in_what->stride, 0x7fffffff) +
 | 
|        mvsad_err_cost(ref_mv, &fcenter_mv, mvjsadcost, mvsadcost, error_per_bit);
 | 
| +  int i, j;
 | 
|  
 | 
|    for (i = 0; i < search_range; i++) {
 | 
|      int best_site = -1;
 | 
|  
 | 
|      for (j = 0; j < 4; j++) {
 | 
| -      const MV this_mv = {ref_mv->row + neighbors[j].row,
 | 
| -                          ref_mv->col + neighbors[j].col};
 | 
| -      if (is_mv_in(x, &this_mv)) {
 | 
| -        const uint8_t *check_here = &in_what[this_mv.row * in_what_stride +
 | 
| -                                                this_mv.col];
 | 
| -        unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
 | 
| -                                           in_what_stride, bestsad);
 | 
| -        if (thissad < bestsad) {
 | 
| -          thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                    mvjsadcost, mvsadcost, error_per_bit);
 | 
| -
 | 
| -          if (thissad < bestsad) {
 | 
| -            bestsad = thissad;
 | 
| +      const MV mv = {ref_mv->row + neighbors[j].row,
 | 
| +                     ref_mv->col + neighbors[j].col};
 | 
| +      if (is_mv_in(x, &mv)) {
 | 
| +        unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +            get_buf_from_mv(in_what, &mv), in_what->stride, best_sad);
 | 
| +        if (sad < best_sad) {
 | 
| +          sad += mvsad_err_cost(&mv, &fcenter_mv, mvjsadcost, mvsadcost,
 | 
| +                                error_per_bit);
 | 
| +          if (sad < best_sad) {
 | 
| +            best_sad = sad;
 | 
|              best_site = j;
 | 
|            }
 | 
|          }
 | 
| @@ -1863,19 +1577,9 @@
 | 
|      } else {
 | 
|        ref_mv->row += neighbors[best_site].row;
 | 
|        ref_mv->col += neighbors[best_site].col;
 | 
| -      best_address = &in_what[ref_mv->row * in_what_stride + ref_mv->col];
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  if (bestsad < INT_MAX) {
 | 
| -    unsigned int unused;
 | 
| -    const MV mv = {ref_mv->row * 8, ref_mv->col * 8};
 | 
| -    return fn_ptr->vf(what, what_stride, best_address, in_what_stride,
 | 
| -                      &unused) +
 | 
| -        mv_err_cost(&mv, center_mv, mvjcost, mvcost, x->errorperbit);
 | 
| -  } else {
 | 
| -    return INT_MAX;
 | 
| -  }
 | 
| +  return best_sad;
 | 
|  }
 | 
|  
 | 
|  int vp9_refining_search_sadx4(const MACROBLOCK *x,
 | 
| @@ -1885,82 +1589,64 @@
 | 
|                                int *mvjcost, int *mvcost[2],
 | 
|                                const MV *center_mv) {
 | 
|    const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| -  MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
 | 
| -  int i, j;
 | 
| -  int this_row_offset, this_col_offset;
 | 
| -
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  const uint8_t *what = x->plane[0].src.buf;
 | 
| -  const uint8_t *best_address = xd->plane[0].pre[0].buf +
 | 
| -                          (ref_mv->row * xd->plane[0].pre[0].stride) +
 | 
| -                          ref_mv->col;
 | 
| -  unsigned int thissad;
 | 
| -  MV this_mv;
 | 
| -
 | 
| +  const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
| -
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
| -
 | 
| -  unsigned int bestsad = fn_ptr->sdf(what, what_stride, best_address,
 | 
| -                                    in_what_stride, 0x7fffffff) +
 | 
| +  const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
 | 
| +  unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
 | 
| +                                    in_what->stride, 0x7fffffff) +
 | 
|        mvsad_err_cost(ref_mv, &fcenter_mv, mvjsadcost, mvsadcost, error_per_bit);
 | 
| +  int i, j;
 | 
|  
 | 
|    for (i = 0; i < search_range; i++) {
 | 
|      int best_site = -1;
 | 
| -    int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
 | 
| -                 ((ref_mv->row + 1) < x->mv_row_max) &
 | 
| -                 ((ref_mv->col - 1) > x->mv_col_min) &
 | 
| -                 ((ref_mv->col + 1) < x->mv_col_max);
 | 
| +    const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
 | 
| +                       ((ref_mv->row + 1) < x->mv_row_max) &
 | 
| +                       ((ref_mv->col - 1) > x->mv_col_min) &
 | 
| +                       ((ref_mv->col + 1) < x->mv_col_max);
 | 
|  
 | 
|      if (all_in) {
 | 
| -      unsigned int sad_array[4];
 | 
| -      uint8_t const *block_offset[4] = {
 | 
| -        best_address - in_what_stride,
 | 
| +      unsigned int sads[4];
 | 
| +      const uint8_t *const positions[4] = {
 | 
| +        best_address - in_what->stride,
 | 
|          best_address - 1,
 | 
|          best_address + 1,
 | 
| -        best_address + in_what_stride
 | 
| +        best_address + in_what->stride
 | 
|        };
 | 
|  
 | 
| -      fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
 | 
| -                     sad_array);
 | 
| +      fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
 | 
|  
 | 
| -      for (j = 0; j < 4; j++) {
 | 
| -        if (sad_array[j] < bestsad) {
 | 
| -          this_mv.row = ref_mv->row + neighbors[j].row;
 | 
| -          this_mv.col = ref_mv->col + neighbors[j].col;
 | 
| -          sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| +      for (j = 0; j < 4; ++j) {
 | 
| +        if (sads[j] < best_sad) {
 | 
| +          const MV mv = {ref_mv->row + neighbors[j].row,
 | 
| +                         ref_mv->col + neighbors[j].col};
 | 
| +          sads[j] += mvsad_err_cost(&mv, &fcenter_mv,
 | 
|                                           mvjsadcost, mvsadcost, error_per_bit);
 | 
|  
 | 
| -          if (sad_array[j] < bestsad) {
 | 
| -            bestsad = sad_array[j];
 | 
| +          if (sads[j] < best_sad) {
 | 
| +            best_sad = sads[j];
 | 
|              best_site = j;
 | 
|            }
 | 
|          }
 | 
|        }
 | 
|      } else {
 | 
| -      for (j = 0; j < 4; j++) {
 | 
| -        this_row_offset = ref_mv->row + neighbors[j].row;
 | 
| -        this_col_offset = ref_mv->col + neighbors[j].col;
 | 
| +      for (j = 0; j < 4; ++j) {
 | 
| +        const MV mv = {ref_mv->row + neighbors[j].row,
 | 
| +                       ref_mv->col + neighbors[j].col};
 | 
|  
 | 
| -        if ((this_col_offset > x->mv_col_min) &&
 | 
| -            (this_col_offset < x->mv_col_max) &&
 | 
| -            (this_row_offset > x->mv_row_min) &&
 | 
| -            (this_row_offset < x->mv_row_max)) {
 | 
| -          const uint8_t *check_here = neighbors[j].row * in_what_stride +
 | 
| -                                      neighbors[j].col + best_address;
 | 
| -          thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride,
 | 
| -                                bestsad);
 | 
| +        if (is_mv_in(x, &mv)) {
 | 
| +          unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
 | 
| +                                         get_buf_from_mv(in_what, &mv),
 | 
| +                                         in_what->stride, best_sad);
 | 
| +          if (sad < best_sad) {
 | 
| +            sad += mvsad_err_cost(&mv, &fcenter_mv,
 | 
| +                                  mvjsadcost, mvsadcost, error_per_bit);
 | 
|  
 | 
| -          if (thissad < bestsad) {
 | 
| -            this_mv.row = this_row_offset;
 | 
| -            this_mv.col = this_col_offset;
 | 
| -            thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| -                                      mvjsadcost, mvsadcost, error_per_bit);
 | 
| -
 | 
| -            if (thissad < bestsad) {
 | 
| -              bestsad = thissad;
 | 
| +            if (sad < best_sad) {
 | 
| +              best_sad = sad;
 | 
|                best_site = j;
 | 
|              }
 | 
|            }
 | 
| @@ -1973,21 +1659,11 @@
 | 
|      } else {
 | 
|        ref_mv->row += neighbors[best_site].row;
 | 
|        ref_mv->col += neighbors[best_site].col;
 | 
| -      best_address += (neighbors[best_site].row) * in_what_stride +
 | 
| -                      neighbors[best_site].col;
 | 
| +      best_address = get_buf_from_mv(in_what, ref_mv);
 | 
|      }
 | 
|    }
 | 
|  
 | 
| -  this_mv.row = ref_mv->row * 8;
 | 
| -  this_mv.col = ref_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad < INT_MAX)
 | 
| -    return fn_ptr->vf(what, what_stride, best_address, in_what_stride,
 | 
| -                      (unsigned int *)(&thissad)) +
 | 
| -                      mv_err_cost(&this_mv, center_mv,
 | 
| -                                  mvjcost, mvcost, x->errorperbit);
 | 
| -  else
 | 
| -    return INT_MAX;
 | 
| +  return best_sad;
 | 
|  }
 | 
|  
 | 
|  // This function is called when we do joint motion search in comp_inter_inter
 | 
| @@ -1999,48 +1675,36 @@
 | 
|                               int *mvjcost, int *mvcost[2],
 | 
|                               const MV *center_mv,
 | 
|                               const uint8_t *second_pred, int w, int h) {
 | 
| -  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
|    const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
 | 
|                             {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
 | 
| -  int i, j;
 | 
| -
 | 
| -  const uint8_t *what = x->plane[0].src.buf;
 | 
| -  const int what_stride = x->plane[0].src.stride;
 | 
| -  const uint8_t *in_what = xd->plane[0].pre[0].buf;
 | 
| -  const int in_what_stride = xd->plane[0].pre[0].stride;
 | 
| -  const uint8_t *best_address = &in_what[ref_mv->row * in_what_stride +
 | 
| -                                             ref_mv->col];
 | 
| -  unsigned int thissad;
 | 
| -  MV this_mv;
 | 
| +  const MACROBLOCKD *const xd = &x->e_mbd;
 | 
| +  const struct buf_2d *const what = &x->plane[0].src;
 | 
| +  const struct buf_2d *const in_what = &xd->plane[0].pre[0];
 | 
|    const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
 | 
| -
 | 
|    const int *mvjsadcost = x->nmvjointsadcost;
 | 
|    int *mvsadcost[2] = {x->nmvsadcost[0], x->nmvsadcost[1]};
 | 
| -
 | 
| -  /* Get compound pred by averaging two pred blocks. */
 | 
| -  unsigned int bestsad = fn_ptr->sdaf(what, what_stride,
 | 
| -                                      best_address, in_what_stride,
 | 
| -                                      second_pred, 0x7fffffff) +
 | 
| +  unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
 | 
| +      get_buf_from_mv(in_what, ref_mv), in_what->stride,
 | 
| +      second_pred, 0x7fffffff) +
 | 
|        mvsad_err_cost(ref_mv, &fcenter_mv, mvjsadcost, mvsadcost, error_per_bit);
 | 
| +  int i, j;
 | 
|  
 | 
|    for (i = 0; i < search_range; ++i) {
 | 
|      int best_site = -1;
 | 
|  
 | 
| -    for (j = 0; j < 8; j++) {
 | 
| -      this_mv.row = ref_mv->row + neighbors[j].row;
 | 
| -      this_mv.col = ref_mv->col + neighbors[j].col;
 | 
| +    for (j = 0; j < 8; ++j) {
 | 
| +      const MV mv = {ref_mv->row + neighbors[j].row,
 | 
| +                     ref_mv->col + neighbors[j].col};
 | 
|  
 | 
| -      if (is_mv_in(x, &this_mv)) {
 | 
| -        const uint8_t *check_here = &in_what[this_mv.row * in_what_stride +
 | 
| -                                                this_mv.col];
 | 
| -
 | 
| -        thissad = fn_ptr->sdaf(what, what_stride, check_here, in_what_stride,
 | 
| -                               second_pred, bestsad);
 | 
| -        if (thissad < bestsad) {
 | 
| -          thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
 | 
| +      if (is_mv_in(x, &mv)) {
 | 
| +        unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
 | 
| +            get_buf_from_mv(in_what, &mv), in_what->stride,
 | 
| +            second_pred, best_sad);
 | 
| +        if (sad < best_sad) {
 | 
| +          sad += mvsad_err_cost(&mv, &fcenter_mv,
 | 
|                                      mvjsadcost, mvsadcost, error_per_bit);
 | 
| -          if (thissad < bestsad) {
 | 
| -            bestsad = thissad;
 | 
| +          if (sad < best_sad) {
 | 
| +            best_sad = sad;
 | 
|              best_site = j;
 | 
|            }
 | 
|          }
 | 
| @@ -2052,21 +1716,7 @@
 | 
|      } else {
 | 
|        ref_mv->row += neighbors[best_site].row;
 | 
|        ref_mv->col += neighbors[best_site].col;
 | 
| -      best_address = &in_what[ref_mv->row * in_what_stride + ref_mv->col];
 | 
|      }
 | 
|    }
 | 
| -
 | 
| -  this_mv.row = ref_mv->row * 8;
 | 
| -  this_mv.col = ref_mv->col * 8;
 | 
| -
 | 
| -  if (bestsad < INT_MAX) {
 | 
| -    // FIXME(rbultje, yunqing): add full-pixel averaging variance functions
 | 
| -    // so we don't have to use the subpixel with xoff=0,yoff=0 here.
 | 
| -    return fn_ptr->svaf(best_address, in_what_stride, 0, 0, what, what_stride,
 | 
| -                        (unsigned int *)(&thissad), second_pred) +
 | 
| -                        mv_err_cost(&this_mv, center_mv,
 | 
| -                                    mvjcost, mvcost, x->errorperbit);
 | 
| -  } else {
 | 
| -    return INT_MAX;
 | 
| -  }
 | 
| +  return best_sad;
 | 
|  }
 | 
| 
 |