]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_mcomp.c
Merge "Move mc_buf to cut down size of MACROBLOCKD."
[libvpx] / vp9 / encoder / vp9_mcomp.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vpx_config.h"
16
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/mem.h"
19
20 #include "vp9/common/vp9_common.h"
21
22 #include "vp9/encoder/vp9_encoder.h"
23 #include "vp9/encoder/vp9_mcomp.h"
24
25 // #define NEW_DIAMOND_SEARCH
26
27 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
28                                              const MV *mv) {
29   return &buf->buf[mv->row * buf->stride + mv->col];
30 }
31
32 void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
33   int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
34   int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
35   int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
36   int row_max = (mv->row >> 3) + MAX_FULL_PEL_VAL;
37
38   col_min = MAX(col_min, (MV_LOW >> 3) + 1);
39   row_min = MAX(row_min, (MV_LOW >> 3) + 1);
40   col_max = MIN(col_max, (MV_UPP >> 3) - 1);
41   row_max = MIN(row_max, (MV_UPP >> 3) - 1);
42
43   // Get intersection of UMV window and valid MV window to reduce # of checks
44   // in diamond search.
45   if (x->mv_col_min < col_min)
46     x->mv_col_min = col_min;
47   if (x->mv_col_max > col_max)
48     x->mv_col_max = col_max;
49   if (x->mv_row_min < row_min)
50     x->mv_row_min = row_min;
51   if (x->mv_row_max > row_max)
52     x->mv_row_max = row_max;
53 }
54
55 int vp9_init_search_range(int size) {
56   int sr = 0;
57   // Minimum search size no matter what the passed in value.
58   size = MAX(16, size);
59
60   while ((size << sr) < MAX_FULL_PEL_VAL)
61     sr++;
62
63   sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
64   return sr;
65 }
66
67 static INLINE int mv_cost(const MV *mv,
68                           const int *joint_cost, int *const comp_cost[2]) {
69   return joint_cost[vp9_get_mv_joint(mv)] +
70              comp_cost[0][mv->row] + comp_cost[1][mv->col];
71 }
72
73 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
74                     const int *mvjcost, int *mvcost[2], int weight) {
75   const MV diff = { mv->row - ref->row,
76                     mv->col - ref->col };
77   return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
78 }
79
80 static int mv_err_cost(const MV *mv, const MV *ref,
81                        const int *mvjcost, int *mvcost[2],
82                        int error_per_bit) {
83   if (mvcost) {
84     const MV diff = { mv->row - ref->row,
85                       mv->col - ref->col };
86     return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
87                                   error_per_bit, 13);
88   }
89   return 0;
90 }
91
92 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
93                           int error_per_bit) {
94   const MV diff = { mv->row - ref->row,
95                     mv->col - ref->col };
96   return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
97                                     x->nmvsadcost) * error_per_bit, 8);
98 }
99
100 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride) {
101   int len, ss_count = 1;
102
103   cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
104   cfg->ss[0].offset = 0;
105
106   for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
107     // Generate offsets for 4 search sites per step.
108     const MV ss_mvs[] = {{-len, 0}, {len, 0}, {0, -len}, {0, len}};
109     int i;
110     for (i = 0; i < 4; ++i) {
111       search_site *const ss = &cfg->ss[ss_count++];
112       ss->mv = ss_mvs[i];
113       ss->offset = ss->mv.row * stride + ss->mv.col;
114     }
115   }
116
117   cfg->ss_count = ss_count;
118   cfg->searches_per_step = 4;
119 }
120
121 void vp9_init3smotion_compensation(search_site_config *cfg, int stride) {
122   int len, ss_count = 1;
123
124   cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
125   cfg->ss[0].offset = 0;
126
127   for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
128     // Generate offsets for 8 search sites per step.
129     const MV ss_mvs[8] = {
130       {-len,  0  }, {len,  0  }, { 0,   -len}, {0,    len},
131       {-len, -len}, {-len, len}, {len,  -len}, {len,  len}
132     };
133     int i;
134     for (i = 0; i < 8; ++i) {
135       search_site *const ss = &cfg->ss[ss_count++];
136       ss->mv = ss_mvs[i];
137       ss->offset = ss->mv.row * stride + ss->mv.col;
138     }
139   }
140
141   cfg->ss_count = ss_count;
142   cfg->searches_per_step = 8;
143 }
144
145 /*
146  * To avoid the penalty for crossing cache-line read, preload the reference
147  * area in a small buffer, which is aligned to make sure there won't be crossing
148  * cache-line read while reading from this buffer. This reduced the cpu
149  * cycles spent on reading ref data in sub-pixel filter functions.
150  * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
151  * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
152  * could reduce the area.
153  */
154
155 /* estimated cost of a motion vector (r,c) */
156 #define MVC(r, c)                                       \
157     (mvcost ?                                           \
158      ((mvjcost[((r) != rr) * 2 + ((c) != rc)] +         \
159        mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
160       error_per_bit + 4096) >> 13 : 0)
161
162
163 // convert motion vector component to offset for svf calc
164 static INLINE int sp(int x) {
165   return (x & 7) << 1;
166 }
167
168 static INLINE const uint8_t *pre(const uint8_t *buf, int stride, int r, int c) {
169   return &buf[(r >> 3) * stride + (c >> 3)];
170 }
171
172 /* checks if (r, c) has better score than previous best */
173 #define CHECK_BETTER(v, r, c) \
174   if (c >= minc && c <= maxc && r >= minr && r <= maxr) {              \
175     if (second_pred == NULL)                                           \
176       thismse = vfp->svf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), z, \
177                              src_stride, &sse);                        \
178     else                                                               \
179       thismse = vfp->svaf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), \
180                               z, src_stride, &sse, second_pred);       \
181     if ((v = MVC(r, c) + thismse) < besterr) {                         \
182       besterr = v;                                                     \
183       br = r;                                                          \
184       bc = c;                                                          \
185       *distortion = thismse;                                           \
186       *sse1 = sse;                                                     \
187     }                                                                  \
188   } else {                                                             \
189     v = INT_MAX;                                                       \
190   }
191
192 #define FIRST_LEVEL_CHECKS                              \
193   {                                                     \
194     unsigned int left, right, up, down, diag;           \
195     CHECK_BETTER(left, tr, tc - hstep);                 \
196     CHECK_BETTER(right, tr, tc + hstep);                \
197     CHECK_BETTER(up, tr - hstep, tc);                   \
198     CHECK_BETTER(down, tr + hstep, tc);                 \
199     whichdir = (left < right ? 0 : 1) +                 \
200                (up < down ? 0 : 2);                     \
201     switch (whichdir) {                                 \
202       case 0:                                           \
203         CHECK_BETTER(diag, tr - hstep, tc - hstep);     \
204         break;                                          \
205       case 1:                                           \
206         CHECK_BETTER(diag, tr - hstep, tc + hstep);     \
207         break;                                          \
208       case 2:                                           \
209         CHECK_BETTER(diag, tr + hstep, tc - hstep);     \
210         break;                                          \
211       case 3:                                           \
212         CHECK_BETTER(diag, tr + hstep, tc + hstep);     \
213         break;                                          \
214     }                                                   \
215   }
216
217 #define SECOND_LEVEL_CHECKS                             \
218   {                                                     \
219     int kr, kc;                                         \
220     unsigned int second;                                \
221     if (tr != br && tc != bc) {                         \
222       kr = br - tr;                                     \
223       kc = bc - tc;                                     \
224       CHECK_BETTER(second, tr + kr, tc + 2 * kc);       \
225       CHECK_BETTER(second, tr + 2 * kr, tc + kc);       \
226     } else if (tr == br && tc != bc) {                  \
227       kc = bc - tc;                                     \
228       CHECK_BETTER(second, tr + hstep, tc + 2 * kc);    \
229       CHECK_BETTER(second, tr - hstep, tc + 2 * kc);    \
230       switch (whichdir) {                               \
231         case 0:                                         \
232         case 1:                                         \
233           CHECK_BETTER(second, tr + hstep, tc + kc);    \
234           break;                                        \
235         case 2:                                         \
236         case 3:                                         \
237           CHECK_BETTER(second, tr - hstep, tc + kc);    \
238           break;                                        \
239       }                                                 \
240     } else if (tr != br && tc == bc) {                  \
241       kr = br - tr;                                     \
242       CHECK_BETTER(second, tr + 2 * kr, tc + hstep);    \
243       CHECK_BETTER(second, tr + 2 * kr, tc - hstep);    \
244       switch (whichdir) {                               \
245         case 0:                                         \
246         case 2:                                         \
247           CHECK_BETTER(second, tr + kr, tc + hstep);    \
248           break;                                        \
249         case 1:                                         \
250         case 3:                                         \
251           CHECK_BETTER(second, tr + kr, tc - hstep);    \
252           break;                                        \
253       }                                                 \
254     }                                                   \
255   }
256
257 #define SETUP_SUBPEL_SEARCH                                                \
258   const uint8_t *const z = x->plane[0].src.buf;                            \
259   const int src_stride = x->plane[0].src.stride;                           \
260   const MACROBLOCKD *xd = &x->e_mbd;                                       \
261   unsigned int besterr = INT_MAX;                                          \
262   unsigned int sse;                                                        \
263   unsigned int whichdir;                                                   \
264   int thismse;                                                             \
265   const unsigned int halfiters = iters_per_step;                           \
266   const unsigned int quarteriters = iters_per_step;                        \
267   const unsigned int eighthiters = iters_per_step;                         \
268   const int y_stride = xd->plane[0].pre[0].stride;                         \
269   const int offset = bestmv->row * y_stride + bestmv->col;                 \
270   const uint8_t *const y = xd->plane[0].pre[0].buf;                        \
271                                                                            \
272   int rr = ref_mv->row;                                                    \
273   int rc = ref_mv->col;                                                    \
274   int br = bestmv->row * 8;                                                \
275   int bc = bestmv->col * 8;                                                \
276   int hstep = 4;                                                           \
277   const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX);           \
278   const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX);           \
279   const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX);           \
280   const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX);           \
281   int tr = br;                                                             \
282   int tc = bc;                                                             \
283                                                                            \
284   bestmv->row *= 8;                                                        \
285   bestmv->col *= 8;
286
287 static INLINE unsigned int setup_center_error(const MACROBLOCKD *xd,
288                                               const MV *bestmv,
289                                               const MV *ref_mv,
290                                               int error_per_bit,
291                                               const vp9_variance_fn_ptr_t *vfp,
292                                               const uint8_t *const src,
293                                               const int src_stride,
294                                               const uint8_t *const y,
295                                               int y_stride,
296                                               const uint8_t *second_pred,
297                                               int w, int h, int offset,
298                                               int *mvjcost, int *mvcost[2],
299                                               unsigned int *sse1,
300                                               int *distortion) {
301   unsigned int besterr;
302 #if CONFIG_VP9_HIGHBITDEPTH
303   if (second_pred != NULL) {
304     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
305       DECLARE_ALIGNED(16, uint16_t, comp_pred16[64 * 64]);
306       vp9_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset,
307                                y_stride);
308       besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, src, src_stride,
309                         sse1);
310     } else {
311       DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
312       vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
313       besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
314     }
315   } else {
316     besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
317   }
318   *distortion = besterr;
319   besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
320 #else
321   (void) xd;
322   if (second_pred != NULL) {
323     DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
324     vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
325     besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
326   } else {
327     besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
328   }
329   *distortion = besterr;
330   besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
331 #endif  // CONFIG_VP9_HIGHBITDEPTH
332   return besterr;
333 }
334
335 static INLINE int divide_and_round(const int n, const int d) {
336   return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
337 }
338
339 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
340   return cost_list[0] < cost_list[1] &&
341          cost_list[0] < cost_list[2] &&
342          cost_list[0] < cost_list[3] &&
343          cost_list[0] < cost_list[4];
344 }
345
346 // Returns surface minima estimate at given precision in 1/2^n bits.
347 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
348 // For a given set of costs S0, S1, S2, S3, S4 at points
349 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
350 // the solution for the location of the minima (x0, y0) is given by:
351 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
352 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
353 // The code below is an integerized version of that.
354 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
355                               int bits) {
356   *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
357                          (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
358   *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
359                          (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
360 }
361
362 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
363     const MACROBLOCK *x,
364     MV *bestmv, const MV *ref_mv,
365     int allow_hp,
366     int error_per_bit,
367     const vp9_variance_fn_ptr_t *vfp,
368     int forced_stop,
369     int iters_per_step,
370     int *cost_list,
371     int *mvjcost, int *mvcost[2],
372     int *distortion,
373     unsigned int *sse1,
374     const uint8_t *second_pred,
375     int w, int h) {
376   SETUP_SUBPEL_SEARCH;
377   besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
378                                z, src_stride, y, y_stride, second_pred,
379                                w, h, offset, mvjcost, mvcost,
380                                sse1, distortion);
381   (void) halfiters;
382   (void) quarteriters;
383   (void) eighthiters;
384   (void) whichdir;
385   (void) allow_hp;
386   (void) forced_stop;
387   (void) hstep;
388
389   if (cost_list &&
390       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
391       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
392       cost_list[4] != INT_MAX &&
393       is_cost_list_wellbehaved(cost_list)) {
394     int ir, ic;
395     unsigned int minpt;
396     get_cost_surf_min(cost_list, &ir, &ic, 2);
397     if (ir != 0 || ic != 0) {
398       CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
399     }
400   } else {
401     FIRST_LEVEL_CHECKS;
402     if (halfiters > 1) {
403       SECOND_LEVEL_CHECKS;
404     }
405
406     tr = br;
407     tc = bc;
408
409     // Each subsequent iteration checks at least one point in common with
410     // the last iteration could be 2 ( if diag selected) 1/4 pel
411     // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
412     if (forced_stop != 2) {
413       hstep >>= 1;
414       FIRST_LEVEL_CHECKS;
415       if (quarteriters > 1) {
416         SECOND_LEVEL_CHECKS;
417       }
418     }
419   }
420
421   tr = br;
422   tc = bc;
423
424   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
425     hstep >>= 1;
426     FIRST_LEVEL_CHECKS;
427     if (eighthiters > 1) {
428       SECOND_LEVEL_CHECKS;
429     }
430   }
431
432   bestmv->row = br;
433   bestmv->col = bc;
434
435   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
436       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
437     return INT_MAX;
438
439   return besterr;
440 }
441
442 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
443                                              MV *bestmv, const MV *ref_mv,
444                                              int allow_hp,
445                                              int error_per_bit,
446                                              const vp9_variance_fn_ptr_t *vfp,
447                                              int forced_stop,
448                                              int iters_per_step,
449                                              int *cost_list,
450                                              int *mvjcost, int *mvcost[2],
451                                              int *distortion,
452                                              unsigned int *sse1,
453                                              const uint8_t *second_pred,
454                                              int w, int h) {
455   SETUP_SUBPEL_SEARCH;
456   besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
457                                z, src_stride, y, y_stride, second_pred,
458                                w, h, offset, mvjcost, mvcost,
459                                sse1, distortion);
460   if (cost_list &&
461       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
462       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
463       cost_list[4] != INT_MAX &&
464       is_cost_list_wellbehaved(cost_list)) {
465     unsigned int minpt;
466     int ir, ic;
467     get_cost_surf_min(cost_list, &ir, &ic, 1);
468     if (ir != 0 || ic != 0) {
469       CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
470     }
471   } else {
472     FIRST_LEVEL_CHECKS;
473     if (halfiters > 1) {
474       SECOND_LEVEL_CHECKS;
475     }
476   }
477
478   // Each subsequent iteration checks at least one point in common with
479   // the last iteration could be 2 ( if diag selected) 1/4 pel
480
481   // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
482   if (forced_stop != 2) {
483     tr = br;
484     tc = bc;
485     hstep >>= 1;
486     FIRST_LEVEL_CHECKS;
487     if (quarteriters > 1) {
488       SECOND_LEVEL_CHECKS;
489     }
490   }
491
492   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
493     tr = br;
494     tc = bc;
495     hstep >>= 1;
496     FIRST_LEVEL_CHECKS;
497     if (eighthiters > 1) {
498       SECOND_LEVEL_CHECKS;
499     }
500   }
501   // These lines insure static analysis doesn't warn that
502   // tr and tc aren't used after the above point.
503   (void) tr;
504   (void) tc;
505
506   bestmv->row = br;
507   bestmv->col = bc;
508
509   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
510       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
511     return INT_MAX;
512
513   return besterr;
514 }
515
516 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
517                                         MV *bestmv, const MV *ref_mv,
518                                         int allow_hp,
519                                         int error_per_bit,
520                                         const vp9_variance_fn_ptr_t *vfp,
521                                         int forced_stop,
522                                         int iters_per_step,
523                                         int *cost_list,
524                                         int *mvjcost, int *mvcost[2],
525                                         int *distortion,
526                                         unsigned int *sse1,
527                                         const uint8_t *second_pred,
528                                         int w, int h) {
529   SETUP_SUBPEL_SEARCH;
530   besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
531                                z, src_stride, y, y_stride, second_pred,
532                                w, h, offset, mvjcost, mvcost,
533                                sse1, distortion);
534   if (cost_list &&
535       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
536       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
537       cost_list[4] != INT_MAX) {
538     unsigned int left, right, up, down, diag;
539     whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
540                (cost_list[2] < cost_list[4] ? 0 : 2);
541     switch (whichdir) {
542       case 0:
543         CHECK_BETTER(left, tr, tc - hstep);
544         CHECK_BETTER(down, tr + hstep, tc);
545         CHECK_BETTER(diag, tr + hstep, tc - hstep);
546         break;
547       case 1:
548         CHECK_BETTER(right, tr, tc + hstep);
549         CHECK_BETTER(down, tr + hstep, tc);
550         CHECK_BETTER(diag, tr + hstep, tc + hstep);
551         break;
552       case 2:
553         CHECK_BETTER(left, tr, tc - hstep);
554         CHECK_BETTER(up, tr - hstep, tc);
555         CHECK_BETTER(diag, tr - hstep, tc - hstep);
556         break;
557       case 3:
558         CHECK_BETTER(right, tr, tc + hstep);
559         CHECK_BETTER(up, tr - hstep, tc);
560         CHECK_BETTER(diag, tr - hstep, tc + hstep);
561         break;
562     }
563   } else {
564     FIRST_LEVEL_CHECKS;
565     if (halfiters > 1) {
566       SECOND_LEVEL_CHECKS;
567     }
568   }
569
570   tr = br;
571   tc = bc;
572
573   // Each subsequent iteration checks at least one point in common with
574   // the last iteration could be 2 ( if diag selected) 1/4 pel
575
576   // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
577   if (forced_stop != 2) {
578     hstep >>= 1;
579     FIRST_LEVEL_CHECKS;
580     if (quarteriters > 1) {
581       SECOND_LEVEL_CHECKS;
582     }
583     tr = br;
584     tc = bc;
585   }
586
587   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
588     hstep >>= 1;
589     FIRST_LEVEL_CHECKS;
590     if (eighthiters > 1) {
591       SECOND_LEVEL_CHECKS;
592     }
593     tr = br;
594     tc = bc;
595   }
596   // These lines insure static analysis doesn't warn that
597   // tr and tc aren't used after the above point.
598   (void) tr;
599   (void) tc;
600
601   bestmv->row = br;
602   bestmv->col = bc;
603
604   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
605       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
606     return INT_MAX;
607
608   return besterr;
609 }
610
611 const MV search_step_table[12] = {
612     // left, right, up, down
613     {0, -4}, {0, 4}, {-4, 0}, {4, 0},
614     {0, -2}, {0, 2}, {-2, 0}, {2, 0},
615     {0, -1}, {0, 1}, {-1, 0}, {1, 0}
616 };
617
618 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
619                                  MV *bestmv, const MV *ref_mv,
620                                  int allow_hp,
621                                  int error_per_bit,
622                                  const vp9_variance_fn_ptr_t *vfp,
623                                  int forced_stop,
624                                  int iters_per_step,
625                                  int *cost_list,
626                                  int *mvjcost, int *mvcost[2],
627                                  int *distortion,
628                                  unsigned int *sse1,
629                                  const uint8_t *second_pred,
630                                  int w, int h) {
631   const uint8_t *const z = x->plane[0].src.buf;
632   const uint8_t *const src_address = z;
633   const int src_stride = x->plane[0].src.stride;
634   const MACROBLOCKD *xd = &x->e_mbd;
635   unsigned int besterr = INT_MAX;
636   unsigned int sse;
637   unsigned int whichdir = 0;
638   int thismse;
639   const int y_stride = xd->plane[0].pre[0].stride;
640   const int offset = bestmv->row * y_stride + bestmv->col;
641   const uint8_t *const y = xd->plane[0].pre[0].buf;
642
643   int rr = ref_mv->row;
644   int rc = ref_mv->col;
645   int br = bestmv->row * 8;
646   int bc = bestmv->col * 8;
647   int hstep = 4;
648   int iter, round = 3 - forced_stop;
649   const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX);
650   const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX);
651   const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX);
652   const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX);
653   int tr = br;
654   int tc = bc;
655   const MV *search_step = search_step_table;
656   int idx, best_idx = -1;
657   unsigned int cost_array[5];
658
659   if (!(allow_hp && vp9_use_mv_hp(ref_mv)))
660     if (round == 3)
661       round = 2;
662
663   bestmv->row *= 8;
664   bestmv->col *= 8;
665
666   besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
667                                z, src_stride, y, y_stride, second_pred,
668                                w, h, offset, mvjcost, mvcost,
669                                sse1, distortion);
670
671   (void) cost_list;  // to silence compiler warning
672
673   for (iter = 0; iter < round; ++iter) {
674     // Check vertical and horizontal sub-pixel positions.
675     for (idx = 0; idx < 4; ++idx) {
676       tr = br + search_step[idx].row;
677       tc = bc + search_step[idx].col;
678       if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
679         const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
680         int row_offset = (tr & 0x07) << 1;
681         int col_offset = (tc & 0x07) << 1;
682         MV this_mv;
683         this_mv.row = tr;
684         this_mv.col = tc;
685         if (second_pred == NULL)
686           thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
687                              src_address, src_stride, &sse);
688         else
689           thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
690                               src_address, src_stride, &sse, second_pred);
691         cost_array[idx] = thismse +
692             mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
693
694         if (cost_array[idx] < besterr) {
695           best_idx = idx;
696           besterr = cost_array[idx];
697           *distortion = thismse;
698           *sse1 = sse;
699         }
700       } else {
701         cost_array[idx] = INT_MAX;
702       }
703     }
704
705     // Check diagonal sub-pixel position
706     tc = bc + (cost_array[0] < cost_array[1] ? -hstep : hstep);
707     tr = br + (cost_array[2] < cost_array[3] ? -hstep : hstep);
708     if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
709       const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
710       int row_offset = (tr & 0x07) << 1;
711       int col_offset = (tc & 0x07) << 1;
712       MV this_mv = {tr, tc};
713       if (second_pred == NULL)
714         thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
715                            src_address, src_stride, &sse);
716       else
717         thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
718                             src_address, src_stride, &sse, second_pred);
719       cost_array[4] = thismse +
720           mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
721
722       if (cost_array[4] < besterr) {
723         best_idx = 4;
724         besterr = cost_array[4];
725         *distortion = thismse;
726         *sse1 = sse;
727       }
728     } else {
729       cost_array[idx] = INT_MAX;
730     }
731
732     if (best_idx < 4 && best_idx >= 0) {
733       br += search_step[best_idx].row;
734       bc += search_step[best_idx].col;
735     } else if (best_idx == 4) {
736       br = tr;
737       bc = tc;
738     }
739
740     if (iters_per_step > 1)
741       SECOND_LEVEL_CHECKS;
742
743     tr = br;
744     tc = bc;
745
746     search_step += 4;
747     hstep >>= 1;
748     best_idx = -1;
749   }
750
751   // Each subsequent iteration checks at least one point in common with
752   // the last iteration could be 2 ( if diag selected) 1/4 pel
753
754   // These lines insure static analysis doesn't warn that
755   // tr and tc aren't used after the above point.
756   (void) tr;
757   (void) tc;
758
759   bestmv->row = br;
760   bestmv->col = bc;
761
762   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
763       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
764     return INT_MAX;
765
766   return besterr;
767 }
768
769 #undef MVC
770 #undef PRE
771 #undef CHECK_BETTER
772
773 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
774                                int range) {
775   return ((row - range) >= x->mv_row_min) &
776          ((row + range) <= x->mv_row_max) &
777          ((col - range) >= x->mv_col_min) &
778          ((col + range) <= x->mv_col_max);
779 }
780
781 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
782   return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
783          (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
784 }
785
786 #define CHECK_BETTER \
787   {\
788     if (thissad < bestsad) {\
789       if (use_mvcost) \
790         thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
791       if (thissad < bestsad) {\
792         bestsad = thissad;\
793         best_site = i;\
794       }\
795     }\
796   }
797
798 #define MAX_PATTERN_SCALES         11
799 #define MAX_PATTERN_CANDIDATES      8  // max number of canddiates per scale
800 #define PATTERN_CANDIDATES_REF      3  // number of refinement candidates
801
802 // Calculate and return a sad+mvcost list around an integer best pel.
803 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
804                                       const MV *ref_mv,
805                                       int sadpb,
806                                       const vp9_variance_fn_ptr_t *fn_ptr,
807                                       const MV *best_mv,
808                                       int *cost_list) {
809   static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
810   const struct buf_2d *const what = &x->plane[0].src;
811   const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
812   const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
813   int br = best_mv->row;
814   int bc = best_mv->col;
815   MV this_mv;
816   int i;
817   unsigned int sse;
818
819   this_mv.row = br;
820   this_mv.col = bc;
821   cost_list[0] = fn_ptr->vf(what->buf, what->stride,
822                             get_buf_from_mv(in_what, &this_mv),
823                             in_what->stride, &sse) +
824       mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
825   if (check_bounds(x, br, bc, 1)) {
826     for (i = 0; i < 4; i++) {
827       const MV this_mv = {br + neighbors[i].row,
828         bc + neighbors[i].col};
829       cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
830                                     get_buf_from_mv(in_what, &this_mv),
831                                     in_what->stride, &sse) +
832           // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
833           mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
834                       x->errorperbit);
835     }
836   } else {
837     for (i = 0; i < 4; i++) {
838       const MV this_mv = {br + neighbors[i].row,
839         bc + neighbors[i].col};
840       if (!is_mv_in(x, &this_mv))
841         cost_list[i + 1] = INT_MAX;
842       else
843         cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
844                                       get_buf_from_mv(in_what, &this_mv),
845                                       in_what->stride, &sse) +
846             // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
847             mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
848                         x->errorperbit);
849     }
850   }
851 }
852
853 // Generic pattern search function that searches over multiple scales.
854 // Each scale can have a different number of candidates and shape of
855 // candidates as indicated in the num_candidates and candidates arrays
856 // passed into this function
857 //
858 static int vp9_pattern_search(const MACROBLOCK *x,
859                               MV *ref_mv,
860                               int search_param,
861                               int sad_per_bit,
862                               int do_init_search,
863                               int *cost_list,
864                               const vp9_variance_fn_ptr_t *vfp,
865                               int use_mvcost,
866                               const MV *center_mv,
867                               MV *best_mv,
868                               const int num_candidates[MAX_PATTERN_SCALES],
869                               const MV candidates[MAX_PATTERN_SCALES]
870                                                  [MAX_PATTERN_CANDIDATES]) {
871   const MACROBLOCKD *const xd = &x->e_mbd;
872   static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
873     10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
874   };
875   int i, s, t;
876   const struct buf_2d *const what = &x->plane[0].src;
877   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
878   int br, bc;
879   int bestsad = INT_MAX;
880   int thissad;
881   int k = -1;
882   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
883   int best_init_s = search_param_to_steps[search_param];
884   // adjust ref_mv to make sure it is within MV range
885   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
886   br = ref_mv->row;
887   bc = ref_mv->col;
888
889   // Work out the start point for the search
890   bestsad = vfp->sdf(what->buf, what->stride,
891                      get_buf_from_mv(in_what, ref_mv), in_what->stride) +
892       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
893
894   // Search all possible scales upto the search param around the center point
895   // pick the scale of the point that is best as the starting scale of
896   // further steps around it.
897   if (do_init_search) {
898     s = best_init_s;
899     best_init_s = -1;
900     for (t = 0; t <= s; ++t) {
901       int best_site = -1;
902       if (check_bounds(x, br, bc, 1 << t)) {
903         for (i = 0; i < num_candidates[t]; i++) {
904           const MV this_mv = {br + candidates[t][i].row,
905                               bc + candidates[t][i].col};
906           thissad = vfp->sdf(what->buf, what->stride,
907                              get_buf_from_mv(in_what, &this_mv),
908                              in_what->stride);
909           CHECK_BETTER
910         }
911       } else {
912         for (i = 0; i < num_candidates[t]; i++) {
913           const MV this_mv = {br + candidates[t][i].row,
914                               bc + candidates[t][i].col};
915           if (!is_mv_in(x, &this_mv))
916             continue;
917           thissad = vfp->sdf(what->buf, what->stride,
918                              get_buf_from_mv(in_what, &this_mv),
919                              in_what->stride);
920           CHECK_BETTER
921         }
922       }
923       if (best_site == -1) {
924         continue;
925       } else {
926         best_init_s = t;
927         k = best_site;
928       }
929     }
930     if (best_init_s != -1) {
931       br += candidates[best_init_s][k].row;
932       bc += candidates[best_init_s][k].col;
933     }
934   }
935
936   // If the center point is still the best, just skip this and move to
937   // the refinement step.
938   if (best_init_s != -1) {
939     int best_site = -1;
940     s = best_init_s;
941
942     do {
943       // No need to search all 6 points the 1st time if initial search was used
944       if (!do_init_search || s != best_init_s) {
945         if (check_bounds(x, br, bc, 1 << s)) {
946           for (i = 0; i < num_candidates[s]; i++) {
947             const MV this_mv = {br + candidates[s][i].row,
948                                 bc + candidates[s][i].col};
949             thissad = vfp->sdf(what->buf, what->stride,
950                                get_buf_from_mv(in_what, &this_mv),
951                                in_what->stride);
952             CHECK_BETTER
953           }
954         } else {
955           for (i = 0; i < num_candidates[s]; i++) {
956             const MV this_mv = {br + candidates[s][i].row,
957                                 bc + candidates[s][i].col};
958             if (!is_mv_in(x, &this_mv))
959               continue;
960             thissad = vfp->sdf(what->buf, what->stride,
961                                get_buf_from_mv(in_what, &this_mv),
962                                in_what->stride);
963             CHECK_BETTER
964           }
965         }
966
967         if (best_site == -1) {
968           continue;
969         } else {
970           br += candidates[s][best_site].row;
971           bc += candidates[s][best_site].col;
972           k = best_site;
973         }
974       }
975
976       do {
977         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
978         best_site = -1;
979         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
980         next_chkpts_indices[1] = k;
981         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
982
983         if (check_bounds(x, br, bc, 1 << s)) {
984           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
985             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
986                                 bc + candidates[s][next_chkpts_indices[i]].col};
987             thissad = vfp->sdf(what->buf, what->stride,
988                                get_buf_from_mv(in_what, &this_mv),
989                                in_what->stride);
990             CHECK_BETTER
991           }
992         } else {
993           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
994             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
995                                 bc + candidates[s][next_chkpts_indices[i]].col};
996             if (!is_mv_in(x, &this_mv))
997               continue;
998             thissad = vfp->sdf(what->buf, what->stride,
999                                get_buf_from_mv(in_what, &this_mv),
1000                                in_what->stride);
1001             CHECK_BETTER
1002           }
1003         }
1004
1005         if (best_site != -1) {
1006           k = next_chkpts_indices[best_site];
1007           br += candidates[s][k].row;
1008           bc += candidates[s][k].col;
1009         }
1010       } while (best_site != -1);
1011     } while (s--);
1012   }
1013
1014   // Returns the one-away integer pel sad values around the best as follows:
1015   // cost_list[0]: cost at the best integer pel
1016   // cost_list[1]: cost at delta {0, -1} (left)   from the best integer pel
1017   // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
1018   // cost_list[3]: cost at delta { 0, 1} (right)  from the best integer pel
1019   // cost_list[4]: cost at delta {-1, 0} (top)    from the best integer pel
1020   if (cost_list) {
1021     const MV best_mv = { br, bc };
1022     calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1023   }
1024   best_mv->row = br;
1025   best_mv->col = bc;
1026   return bestsad;
1027 }
1028
1029 // A specialized function where the smallest scale search candidates
1030 // are 4 1-away neighbors, and cost_list is non-null
1031 // TODO(debargha): Merge this function with the one above. Also remove
1032 // use_mvcost option since it is always 1, to save unnecessary branches.
1033 static int vp9_pattern_search_sad(const MACROBLOCK *x,
1034                                   MV *ref_mv,
1035                                   int search_param,
1036                                   int sad_per_bit,
1037                                   int do_init_search,
1038                                   int *cost_list,
1039                                   const vp9_variance_fn_ptr_t *vfp,
1040                                   int use_mvcost,
1041                                   const MV *center_mv,
1042                                   MV *best_mv,
1043                                   const int num_candidates[MAX_PATTERN_SCALES],
1044                                   const MV candidates[MAX_PATTERN_SCALES]
1045                                                      [MAX_PATTERN_CANDIDATES]) {
1046   const MACROBLOCKD *const xd = &x->e_mbd;
1047   static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
1048     10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1049   };
1050   int i, s, t;
1051   const struct buf_2d *const what = &x->plane[0].src;
1052   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1053   int br, bc;
1054   int bestsad = INT_MAX;
1055   int thissad;
1056   int k = -1;
1057   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1058   int best_init_s = search_param_to_steps[search_param];
1059   // adjust ref_mv to make sure it is within MV range
1060   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1061   br = ref_mv->row;
1062   bc = ref_mv->col;
1063   if (cost_list != NULL) {
1064     cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1065         INT_MAX;
1066   }
1067
1068   // Work out the start point for the search
1069   bestsad = vfp->sdf(what->buf, what->stride,
1070                      get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1071       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1072
1073   // Search all possible scales upto the search param around the center point
1074   // pick the scale of the point that is best as the starting scale of
1075   // further steps around it.
1076   if (do_init_search) {
1077     s = best_init_s;
1078     best_init_s = -1;
1079     for (t = 0; t <= s; ++t) {
1080       int best_site = -1;
1081       if (check_bounds(x, br, bc, 1 << t)) {
1082         for (i = 0; i < num_candidates[t]; i++) {
1083           const MV this_mv = {br + candidates[t][i].row,
1084                               bc + candidates[t][i].col};
1085           thissad = vfp->sdf(what->buf, what->stride,
1086                              get_buf_from_mv(in_what, &this_mv),
1087                              in_what->stride);
1088           CHECK_BETTER
1089         }
1090       } else {
1091         for (i = 0; i < num_candidates[t]; i++) {
1092           const MV this_mv = {br + candidates[t][i].row,
1093                               bc + candidates[t][i].col};
1094           if (!is_mv_in(x, &this_mv))
1095             continue;
1096           thissad = vfp->sdf(what->buf, what->stride,
1097                              get_buf_from_mv(in_what, &this_mv),
1098                              in_what->stride);
1099           CHECK_BETTER
1100         }
1101       }
1102       if (best_site == -1) {
1103         continue;
1104       } else {
1105         best_init_s = t;
1106         k = best_site;
1107       }
1108     }
1109     if (best_init_s != -1) {
1110       br += candidates[best_init_s][k].row;
1111       bc += candidates[best_init_s][k].col;
1112     }
1113   }
1114
1115   // If the center point is still the best, just skip this and move to
1116   // the refinement step.
1117   if (best_init_s != -1) {
1118     int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1119     int best_site = -1;
1120     s = best_init_s;
1121
1122     for (; s >= do_sad; s--) {
1123       if (!do_init_search || s != best_init_s) {
1124         if (check_bounds(x, br, bc, 1 << s)) {
1125           for (i = 0; i < num_candidates[s]; i++) {
1126             const MV this_mv = {br + candidates[s][i].row,
1127                                 bc + candidates[s][i].col};
1128             thissad = vfp->sdf(what->buf, what->stride,
1129                                get_buf_from_mv(in_what, &this_mv),
1130                                in_what->stride);
1131             CHECK_BETTER
1132           }
1133         } else {
1134           for (i = 0; i < num_candidates[s]; i++) {
1135             const MV this_mv = {br + candidates[s][i].row,
1136                                 bc + candidates[s][i].col};
1137             if (!is_mv_in(x, &this_mv))
1138               continue;
1139             thissad = vfp->sdf(what->buf, what->stride,
1140                                get_buf_from_mv(in_what, &this_mv),
1141                                in_what->stride);
1142             CHECK_BETTER
1143           }
1144         }
1145
1146         if (best_site == -1) {
1147           continue;
1148         } else {
1149           br += candidates[s][best_site].row;
1150           bc += candidates[s][best_site].col;
1151           k = best_site;
1152         }
1153       }
1154
1155       do {
1156         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1157         best_site = -1;
1158         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1159         next_chkpts_indices[1] = k;
1160         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1161
1162         if (check_bounds(x, br, bc, 1 << s)) {
1163           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1164             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1165                                 bc + candidates[s][next_chkpts_indices[i]].col};
1166             thissad = vfp->sdf(what->buf, what->stride,
1167                                get_buf_from_mv(in_what, &this_mv),
1168                                in_what->stride);
1169             CHECK_BETTER
1170           }
1171         } else {
1172           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1173             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1174                                 bc + candidates[s][next_chkpts_indices[i]].col};
1175             if (!is_mv_in(x, &this_mv))
1176               continue;
1177             thissad = vfp->sdf(what->buf, what->stride,
1178                                get_buf_from_mv(in_what, &this_mv),
1179                                in_what->stride);
1180             CHECK_BETTER
1181           }
1182         }
1183
1184         if (best_site != -1) {
1185           k = next_chkpts_indices[best_site];
1186           br += candidates[s][k].row;
1187           bc += candidates[s][k].col;
1188         }
1189       } while (best_site != -1);
1190     }
1191
1192     // Note: If we enter the if below, then cost_list must be non-NULL.
1193     if (s == 0) {
1194       cost_list[0] = bestsad;
1195       if (!do_init_search || s != best_init_s) {
1196         if (check_bounds(x, br, bc, 1 << s)) {
1197           for (i = 0; i < num_candidates[s]; i++) {
1198             const MV this_mv = {br + candidates[s][i].row,
1199                                 bc + candidates[s][i].col};
1200             cost_list[i + 1] =
1201             thissad = vfp->sdf(what->buf, what->stride,
1202                                get_buf_from_mv(in_what, &this_mv),
1203                                in_what->stride);
1204             CHECK_BETTER
1205           }
1206         } else {
1207           for (i = 0; i < num_candidates[s]; i++) {
1208             const MV this_mv = {br + candidates[s][i].row,
1209                                 bc + candidates[s][i].col};
1210             if (!is_mv_in(x, &this_mv))
1211               continue;
1212             cost_list[i + 1] =
1213             thissad = vfp->sdf(what->buf, what->stride,
1214                                get_buf_from_mv(in_what, &this_mv),
1215                                in_what->stride);
1216             CHECK_BETTER
1217           }
1218         }
1219
1220         if (best_site != -1) {
1221           br += candidates[s][best_site].row;
1222           bc += candidates[s][best_site].col;
1223           k = best_site;
1224         }
1225       }
1226       while (best_site != -1) {
1227         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1228         best_site = -1;
1229         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1230         next_chkpts_indices[1] = k;
1231         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1232         cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1233         cost_list[((k + 2) % 4) + 1] = cost_list[0];
1234         cost_list[0] = bestsad;
1235
1236         if (check_bounds(x, br, bc, 1 << s)) {
1237           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1238             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1239                                 bc + candidates[s][next_chkpts_indices[i]].col};
1240             cost_list[next_chkpts_indices[i] + 1] =
1241             thissad = vfp->sdf(what->buf, what->stride,
1242                                get_buf_from_mv(in_what, &this_mv),
1243                                in_what->stride);
1244             CHECK_BETTER
1245           }
1246         } else {
1247           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1248             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1249                                 bc + candidates[s][next_chkpts_indices[i]].col};
1250             if (!is_mv_in(x, &this_mv)) {
1251               cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1252               continue;
1253             }
1254             cost_list[next_chkpts_indices[i] + 1] =
1255             thissad = vfp->sdf(what->buf, what->stride,
1256                                get_buf_from_mv(in_what, &this_mv),
1257                                in_what->stride);
1258             CHECK_BETTER
1259           }
1260         }
1261
1262         if (best_site != -1) {
1263           k = next_chkpts_indices[best_site];
1264           br += candidates[s][k].row;
1265           bc += candidates[s][k].col;
1266         }
1267       }
1268     }
1269   }
1270
1271   // Returns the one-away integer pel sad values around the best as follows:
1272   // cost_list[0]: sad at the best integer pel
1273   // cost_list[1]: sad at delta {0, -1} (left)   from the best integer pel
1274   // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1275   // cost_list[3]: sad at delta { 0, 1} (right)  from the best integer pel
1276   // cost_list[4]: sad at delta {-1, 0} (top)    from the best integer pel
1277   if (cost_list) {
1278     static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1279     if (cost_list[0] == INT_MAX) {
1280       cost_list[0] = bestsad;
1281       if (check_bounds(x, br, bc, 1)) {
1282         for (i = 0; i < 4; i++) {
1283           const MV this_mv = { br + neighbors[i].row,
1284                                bc + neighbors[i].col };
1285           cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1286                                      get_buf_from_mv(in_what, &this_mv),
1287                                      in_what->stride);
1288         }
1289       } else {
1290         for (i = 0; i < 4; i++) {
1291           const MV this_mv = {br + neighbors[i].row,
1292             bc + neighbors[i].col};
1293           if (!is_mv_in(x, &this_mv))
1294             cost_list[i + 1] = INT_MAX;
1295           else
1296             cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1297                                        get_buf_from_mv(in_what, &this_mv),
1298                                        in_what->stride);
1299         }
1300       }
1301     } else {
1302       if (use_mvcost) {
1303         for (i = 0; i < 4; i++) {
1304           const MV this_mv = {br + neighbors[i].row,
1305             bc + neighbors[i].col};
1306           if (cost_list[i + 1] != INT_MAX) {
1307             cost_list[i + 1] +=
1308                 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1309           }
1310         }
1311       }
1312     }
1313   }
1314   best_mv->row = br;
1315   best_mv->col = bc;
1316   return bestsad;
1317 }
1318
1319 int vp9_get_mvpred_var(const MACROBLOCK *x,
1320                        const MV *best_mv, const MV *center_mv,
1321                        const vp9_variance_fn_ptr_t *vfp,
1322                        int use_mvcost) {
1323   const MACROBLOCKD *const xd = &x->e_mbd;
1324   const struct buf_2d *const what = &x->plane[0].src;
1325   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1326   const MV mv = {best_mv->row * 8, best_mv->col * 8};
1327   unsigned int unused;
1328
1329   return vfp->vf(what->buf, what->stride,
1330                  get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1331       (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
1332                                  x->mvcost, x->errorperbit) : 0);
1333 }
1334
1335 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1336                           const MV *best_mv, const MV *center_mv,
1337                           const uint8_t *second_pred,
1338                           const vp9_variance_fn_ptr_t *vfp,
1339                           int use_mvcost) {
1340   const MACROBLOCKD *const xd = &x->e_mbd;
1341   const struct buf_2d *const what = &x->plane[0].src;
1342   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1343   const MV mv = {best_mv->row * 8, best_mv->col * 8};
1344   unsigned int unused;
1345
1346   return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1347                    what->buf, what->stride, &unused, second_pred) +
1348       (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
1349                                  x->mvcost, x->errorperbit) : 0);
1350 }
1351
1352 int vp9_hex_search(const MACROBLOCK *x,
1353                    MV *ref_mv,
1354                    int search_param,
1355                    int sad_per_bit,
1356                    int do_init_search,
1357                    int *cost_list,
1358                    const vp9_variance_fn_ptr_t *vfp,
1359                    int use_mvcost,
1360                    const MV *center_mv, MV *best_mv) {
1361   // First scale has 8-closest points, the rest have 6 points in hex shape
1362   // at increasing scales
1363   static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1364     8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1365   };
1366   // Note that the largest candidate step at each scale is 2^scale
1367   static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1368     {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1369     {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1370     {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1371     {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1372     {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1373     {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1374     {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1375     {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1376     {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1377     {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1378     {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1379       { -1024, 0}},
1380   };
1381   return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1382                             do_init_search, cost_list, vfp, use_mvcost,
1383                             center_mv, best_mv,
1384                             hex_num_candidates, hex_candidates);
1385 }
1386
1387 int vp9_bigdia_search(const MACROBLOCK *x,
1388                       MV *ref_mv,
1389                       int search_param,
1390                       int sad_per_bit,
1391                       int do_init_search,
1392                       int *cost_list,
1393                       const vp9_variance_fn_ptr_t *vfp,
1394                       int use_mvcost,
1395                       const MV *center_mv,
1396                       MV *best_mv) {
1397   // First scale has 4-closest points, the rest have 8 points in diamond
1398   // shape at increasing scales
1399   static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1400     4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1401   };
1402   // Note that the largest candidate step at each scale is 2^scale
1403   static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1404                                    [MAX_PATTERN_CANDIDATES] = {
1405     {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1406     {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1407     {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1408     {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1409     {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1410     {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1411       {-16, 16}, {-32, 0}},
1412     {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1413       {-32, 32}, {-64, 0}},
1414     {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1415       {-64, 64}, {-128, 0}},
1416     {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1417       {-128, 128}, {-256, 0}},
1418     {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1419       {-256, 256}, {-512, 0}},
1420     {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1421       {-512, 512}, {-1024, 0}},
1422   };
1423   return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1424                                 do_init_search, cost_list, vfp, use_mvcost,
1425                                 center_mv, best_mv,
1426                                 bigdia_num_candidates, bigdia_candidates);
1427 }
1428
1429 int vp9_square_search(const MACROBLOCK *x,
1430                       MV *ref_mv,
1431                       int search_param,
1432                       int sad_per_bit,
1433                       int do_init_search,
1434                       int *cost_list,
1435                       const vp9_variance_fn_ptr_t *vfp,
1436                       int use_mvcost,
1437                       const MV *center_mv,
1438                       MV *best_mv) {
1439   // All scales have 8 closest points in square shape
1440   static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1441     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1442   };
1443   // Note that the largest candidate step at each scale is 2^scale
1444   static const MV square_candidates[MAX_PATTERN_SCALES]
1445                                    [MAX_PATTERN_CANDIDATES] = {
1446     {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1447     {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1448     {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1449     {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1450     {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1451       {-16, 16}, {-16, 0}},
1452     {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1453       {-32, 32}, {-32, 0}},
1454     {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1455       {-64, 64}, {-64, 0}},
1456     {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1457       {-128, 128}, {-128, 0}},
1458     {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1459       {-256, 256}, {-256, 0}},
1460     {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1461       {-512, 512}, {-512, 0}},
1462     {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1463       {0, 1024}, {-1024, 1024}, {-1024, 0}},
1464   };
1465   return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1466                             do_init_search, cost_list, vfp, use_mvcost,
1467                             center_mv, best_mv,
1468                             square_num_candidates, square_candidates);
1469 }
1470
1471 int vp9_fast_hex_search(const MACROBLOCK *x,
1472                         MV *ref_mv,
1473                         int search_param,
1474                         int sad_per_bit,
1475                         int do_init_search,  // must be zero for fast_hex
1476                         int *cost_list,
1477                         const vp9_variance_fn_ptr_t *vfp,
1478                         int use_mvcost,
1479                         const MV *center_mv,
1480                         MV *best_mv) {
1481   return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1482                         sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1483                         center_mv, best_mv);
1484 }
1485
1486 int vp9_fast_dia_search(const MACROBLOCK *x,
1487                         MV *ref_mv,
1488                         int search_param,
1489                         int sad_per_bit,
1490                         int do_init_search,
1491                         int *cost_list,
1492                         const vp9_variance_fn_ptr_t *vfp,
1493                         int use_mvcost,
1494                         const MV *center_mv,
1495                         MV *best_mv) {
1496   return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1497                            sad_per_bit, do_init_search, cost_list, vfp,
1498                            use_mvcost, center_mv, best_mv);
1499 }
1500
1501 #undef CHECK_BETTER
1502
1503 int vp9_full_range_search_c(const MACROBLOCK *x,
1504                             const search_site_config *cfg,
1505                             MV *ref_mv, MV *best_mv,
1506                             int search_param, int sad_per_bit, int *num00,
1507                             const vp9_variance_fn_ptr_t *fn_ptr,
1508                             const MV *center_mv) {
1509   const MACROBLOCKD *const xd = &x->e_mbd;
1510   const struct buf_2d *const what = &x->plane[0].src;
1511   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1512   const int range = 64;
1513   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1514   unsigned int best_sad = INT_MAX;
1515   int r, c, i;
1516   int start_col, end_col, start_row, end_row;
1517
1518   // The cfg and search_param parameters are not used in this search variant
1519   (void)cfg;
1520   (void)search_param;
1521
1522   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1523   *best_mv = *ref_mv;
1524   *num00 = 11;
1525   best_sad = fn_ptr->sdf(what->buf, what->stride,
1526                          get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1527                  mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1528   start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1529   start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1530   end_row = MIN(range, x->mv_row_max - ref_mv->row);
1531   end_col = MIN(range, x->mv_col_max - ref_mv->col);
1532
1533   for (r = start_row; r <= end_row; ++r) {
1534     for (c = start_col; c <= end_col; c += 4) {
1535       if (c + 3 <= end_col) {
1536         unsigned int sads[4];
1537         const uint8_t *addrs[4];
1538         for (i = 0; i < 4; ++i) {
1539           const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1540           addrs[i] = get_buf_from_mv(in_what, &mv);
1541         }
1542
1543         fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1544
1545         for (i = 0; i < 4; ++i) {
1546           if (sads[i] < best_sad) {
1547             const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1548             const unsigned int sad = sads[i] +
1549                 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1550             if (sad < best_sad) {
1551               best_sad = sad;
1552               *best_mv = mv;
1553             }
1554           }
1555         }
1556       } else {
1557         for (i = 0; i < end_col - c; ++i) {
1558           const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1559           unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1560               get_buf_from_mv(in_what, &mv), in_what->stride);
1561           if (sad < best_sad) {
1562             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1563             if (sad < best_sad) {
1564               best_sad = sad;
1565               *best_mv = mv;
1566             }
1567           }
1568         }
1569       }
1570     }
1571   }
1572
1573   return best_sad;
1574 }
1575
1576 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1577                              const search_site_config *cfg,
1578                              MV *ref_mv, MV *best_mv, int search_param,
1579                              int sad_per_bit, int *num00,
1580                              const vp9_variance_fn_ptr_t *fn_ptr,
1581                              const MV *center_mv) {
1582   int i, j, step;
1583
1584   const MACROBLOCKD *const xd = &x->e_mbd;
1585   uint8_t *what = x->plane[0].src.buf;
1586   const int what_stride = x->plane[0].src.stride;
1587   const uint8_t *in_what;
1588   const int in_what_stride = xd->plane[0].pre[0].stride;
1589   const uint8_t *best_address;
1590
1591   unsigned int bestsad = INT_MAX;
1592   int best_site = 0;
1593   int last_site = 0;
1594
1595   int ref_row;
1596   int ref_col;
1597
1598   // search_param determines the length of the initial step and hence the number
1599   // of iterations.
1600   // 0 = initial step (MAX_FIRST_STEP) pel
1601   // 1 = (MAX_FIRST_STEP/2) pel,
1602   // 2 = (MAX_FIRST_STEP/4) pel...
1603   const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1604   const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1605
1606   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1607   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1608   ref_row = ref_mv->row;
1609   ref_col = ref_mv->col;
1610   *num00 = 0;
1611   best_mv->row = ref_row;
1612   best_mv->col = ref_col;
1613
1614   // Work out the start point for the search
1615   in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1616   best_address = in_what;
1617
1618   // Check the starting position
1619   bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1620                 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1621
1622   i = 1;
1623
1624   for (step = 0; step < tot_steps; step++) {
1625     int all_in = 1, t;
1626
1627     // All_in is true if every one of the points we are checking are within
1628     // the bounds of the image.
1629     all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1630     all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1631     all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1632     all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1633
1634     // If all the pixels are within the bounds we don't check whether the
1635     // search point is valid in this loop,  otherwise we check each point
1636     // for validity..
1637     if (all_in) {
1638       unsigned int sad_array[4];
1639
1640       for (j = 0; j < cfg->searches_per_step; j += 4) {
1641         unsigned char const *block_offset[4];
1642
1643         for (t = 0; t < 4; t++)
1644           block_offset[t] = ss[i + t].offset + best_address;
1645
1646         fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1647                        sad_array);
1648
1649         for (t = 0; t < 4; t++, i++) {
1650           if (sad_array[t] < bestsad) {
1651             const MV this_mv = {best_mv->row + ss[i].mv.row,
1652                                 best_mv->col + ss[i].mv.col};
1653             sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1654                                            sad_per_bit);
1655             if (sad_array[t] < bestsad) {
1656               bestsad = sad_array[t];
1657               best_site = i;
1658             }
1659           }
1660         }
1661       }
1662     } else {
1663       for (j = 0; j < cfg->searches_per_step; j++) {
1664         // Trap illegal vectors
1665         const MV this_mv = {best_mv->row + ss[i].mv.row,
1666                             best_mv->col + ss[i].mv.col};
1667
1668         if (is_mv_in(x, &this_mv)) {
1669           const uint8_t *const check_here = ss[i].offset + best_address;
1670           unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1671                                              in_what_stride);
1672
1673           if (thissad < bestsad) {
1674             thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1675             if (thissad < bestsad) {
1676               bestsad = thissad;
1677               best_site = i;
1678             }
1679           }
1680         }
1681         i++;
1682       }
1683     }
1684     if (best_site != last_site) {
1685       best_mv->row += ss[best_site].mv.row;
1686       best_mv->col += ss[best_site].mv.col;
1687       best_address += ss[best_site].offset;
1688       last_site = best_site;
1689 #if defined(NEW_DIAMOND_SEARCH)
1690       while (1) {
1691         const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1692                             best_mv->col + ss[best_site].mv.col};
1693         if (is_mv_in(x, &this_mv)) {
1694           const uint8_t *const check_here = ss[best_site].offset + best_address;
1695           unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1696                                              in_what_stride);
1697           if (thissad < bestsad) {
1698             thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1699             if (thissad < bestsad) {
1700               bestsad = thissad;
1701               best_mv->row += ss[best_site].mv.row;
1702               best_mv->col += ss[best_site].mv.col;
1703               best_address += ss[best_site].offset;
1704               continue;
1705             }
1706           }
1707         }
1708         break;
1709       };
1710 #endif
1711     } else if (best_address == in_what) {
1712       (*num00)++;
1713     }
1714   }
1715   return bestsad;
1716 }
1717
1718 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1719   int best_sad = INT_MAX;
1720   int this_sad;
1721   int d;
1722   int center, offset = 0;
1723   int bw = 4 << bwl;  // redundant variable, to be changed in the experiments.
1724   for (d = 0; d <= bw; d += 16) {
1725     this_sad = vp9_vector_var(&ref[d], src, bwl);
1726     if (this_sad < best_sad) {
1727       best_sad = this_sad;
1728       offset = d;
1729     }
1730   }
1731   center = offset;
1732
1733   for (d = -8; d <= 8; d += 16) {
1734     int this_pos = offset + d;
1735     // check limit
1736     if (this_pos < 0 || this_pos > bw)
1737       continue;
1738     this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1739     if (this_sad < best_sad) {
1740       best_sad = this_sad;
1741       center = this_pos;
1742     }
1743   }
1744   offset = center;
1745
1746   for (d = -4; d <= 4; d += 8) {
1747     int this_pos = offset + d;
1748     // check limit
1749     if (this_pos < 0 || this_pos > bw)
1750       continue;
1751     this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1752     if (this_sad < best_sad) {
1753       best_sad = this_sad;
1754       center = this_pos;
1755     }
1756   }
1757   offset = center;
1758
1759   for (d = -2; d <= 2; d += 4) {
1760     int this_pos = offset + d;
1761     // check limit
1762     if (this_pos < 0 || this_pos > bw)
1763       continue;
1764     this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1765     if (this_sad < best_sad) {
1766       best_sad = this_sad;
1767       center = this_pos;
1768     }
1769   }
1770   offset = center;
1771
1772   for (d = -1; d <= 1; d += 2) {
1773     int this_pos = offset + d;
1774     // check limit
1775     if (this_pos < 0 || this_pos > bw)
1776       continue;
1777     this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1778     if (this_sad < best_sad) {
1779       best_sad = this_sad;
1780       center = this_pos;
1781     }
1782   }
1783
1784   return (center - (bw >> 1));
1785 }
1786
1787 static const MV search_pos[4] = {
1788     {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1789 };
1790
1791 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1792                                            BLOCK_SIZE bsize) {
1793   MACROBLOCKD *xd = &x->e_mbd;
1794   DECLARE_ALIGNED(16, int16_t, hbuf[128]);
1795   DECLARE_ALIGNED(16, int16_t, vbuf[128]);
1796   DECLARE_ALIGNED(16, int16_t, src_hbuf[64]);
1797   DECLARE_ALIGNED(16, int16_t, src_vbuf[64]);
1798   int idx;
1799   const int bw = 4 << b_width_log2_lookup[bsize];
1800   const int bh = 4 << b_height_log2_lookup[bsize];
1801   const int search_width = bw << 1;
1802   const int search_height = bh << 1;
1803   const int src_stride = x->plane[0].src.stride;
1804   const int ref_stride = xd->plane[0].pre[0].stride;
1805   uint8_t const *ref_buf, *src_buf;
1806   MV *tmp_mv = &xd->mi[0]->mbmi.mv[0].as_mv;
1807   unsigned int best_sad, tmp_sad, this_sad[4];
1808   MV this_mv;
1809   const int norm_factor = 3 + (bw >> 5);
1810
1811 #if CONFIG_VP9_HIGHBITDEPTH
1812   tmp_mv->row = 0;
1813   tmp_mv->col = 0;
1814   return cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1815                                 xd->plane[0].pre[0].buf, ref_stride);
1816 #endif
1817
1818   // Set up prediction 1-D reference set
1819   ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1820   for (idx = 0; idx < search_width; idx += 16) {
1821     vp9_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1822     ref_buf += 16;
1823   }
1824
1825   ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1826   for (idx = 0; idx < search_height; ++idx) {
1827     vbuf[idx] = vp9_int_pro_col(ref_buf, bw) >> norm_factor;
1828     ref_buf += ref_stride;
1829   }
1830
1831   // Set up src 1-D reference set
1832   for (idx = 0; idx < bw; idx += 16) {
1833     src_buf = x->plane[0].src.buf + idx;
1834     vp9_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1835   }
1836
1837   src_buf = x->plane[0].src.buf;
1838   for (idx = 0; idx < bh; ++idx) {
1839     src_vbuf[idx] = vp9_int_pro_col(src_buf, bw) >> norm_factor;
1840     src_buf += src_stride;
1841   }
1842
1843   // Find the best match per 1-D search
1844   tmp_mv->col = vector_match(hbuf, src_hbuf, b_width_log2_lookup[bsize]);
1845   tmp_mv->row = vector_match(vbuf, src_vbuf, b_height_log2_lookup[bsize]);
1846
1847   this_mv = *tmp_mv;
1848   src_buf = x->plane[0].src.buf;
1849   ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1850   best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1851
1852   {
1853     const uint8_t * const pos[4] = {
1854         ref_buf - ref_stride,
1855         ref_buf - 1,
1856         ref_buf + 1,
1857         ref_buf + ref_stride,
1858     };
1859
1860     cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1861   }
1862
1863   for (idx = 0; idx < 4; ++idx) {
1864     if (this_sad[idx] < best_sad) {
1865       best_sad = this_sad[idx];
1866       tmp_mv->row = search_pos[idx].row + this_mv.row;
1867       tmp_mv->col = search_pos[idx].col + this_mv.col;
1868     }
1869   }
1870
1871   if (this_sad[0] < this_sad[3])
1872     this_mv.row -= 1;
1873   else
1874     this_mv.row += 1;
1875
1876   if (this_sad[1] < this_sad[2])
1877     this_mv.col -= 1;
1878   else
1879     this_mv.col += 1;
1880
1881   ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1882
1883   tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1884                                    ref_buf, ref_stride);
1885   if (best_sad > tmp_sad) {
1886     *tmp_mv = this_mv;
1887     best_sad = tmp_sad;
1888   }
1889
1890   tmp_mv->row *= 8;
1891   tmp_mv->col *= 8;
1892
1893   return best_sad;
1894 }
1895
1896 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1897               point as the best match, we will do a final 1-away diamond
1898               refining search  */
1899 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1900                            MV *mvp_full, int step_param,
1901                            int sadpb, int further_steps, int do_refine,
1902                            int *cost_list,
1903                            const vp9_variance_fn_ptr_t *fn_ptr,
1904                            const MV *ref_mv, MV *dst_mv) {
1905   MV temp_mv;
1906   int thissme, n, num00 = 0;
1907   int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1908                                         step_param, sadpb, &n,
1909                                         fn_ptr, ref_mv);
1910   if (bestsme < INT_MAX)
1911     bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1912   *dst_mv = temp_mv;
1913
1914   // If there won't be more n-step search, check to see if refining search is
1915   // needed.
1916   if (n > further_steps)
1917     do_refine = 0;
1918
1919   while (n < further_steps) {
1920     ++n;
1921
1922     if (num00) {
1923       num00--;
1924     } else {
1925       thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1926                                         step_param + n, sadpb, &num00,
1927                                         fn_ptr, ref_mv);
1928       if (thissme < INT_MAX)
1929         thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1930
1931       // check to see if refining search is needed.
1932       if (num00 > further_steps - n)
1933         do_refine = 0;
1934
1935       if (thissme < bestsme) {
1936         bestsme = thissme;
1937         *dst_mv = temp_mv;
1938       }
1939     }
1940   }
1941
1942   // final 1-away diamond refining search
1943   if (do_refine) {
1944     const int search_range = 8;
1945     MV best_mv = *dst_mv;
1946     thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1947                                        fn_ptr, ref_mv);
1948     if (thissme < INT_MAX)
1949       thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1950     if (thissme < bestsme) {
1951       bestsme = thissme;
1952       *dst_mv = best_mv;
1953     }
1954   }
1955
1956   // Return cost list.
1957   if (cost_list) {
1958     calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1959   }
1960   return bestsme;
1961 }
1962
1963 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1964                           int sad_per_bit, int distance,
1965                           const vp9_variance_fn_ptr_t *fn_ptr,
1966                           const MV *center_mv, MV *best_mv) {
1967   int r, c;
1968   const MACROBLOCKD *const xd = &x->e_mbd;
1969   const struct buf_2d *const what = &x->plane[0].src;
1970   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1971   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1972   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1973   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1974   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1975   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1976   int best_sad = fn_ptr->sdf(what->buf, what->stride,
1977       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1978       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1979   *best_mv = *ref_mv;
1980
1981   for (r = row_min; r < row_max; ++r) {
1982     for (c = col_min; c < col_max; ++c) {
1983       const MV mv = {r, c};
1984       const int sad = fn_ptr->sdf(what->buf, what->stride,
1985           get_buf_from_mv(in_what, &mv), in_what->stride) +
1986               mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1987       if (sad < best_sad) {
1988         best_sad = sad;
1989         *best_mv = mv;
1990       }
1991     }
1992   }
1993   return best_sad;
1994 }
1995
1996 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
1997                           int sad_per_bit, int distance,
1998                           const vp9_variance_fn_ptr_t *fn_ptr,
1999                           const MV *center_mv, MV *best_mv) {
2000   int r;
2001   const MACROBLOCKD *const xd = &x->e_mbd;
2002   const struct buf_2d *const what = &x->plane[0].src;
2003   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2004   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2005   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2006   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2007   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2008   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2009   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2010       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2011       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2012   *best_mv = *ref_mv;
2013
2014   for (r = row_min; r < row_max; ++r) {
2015     int c = col_min;
2016     const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2017
2018     if (fn_ptr->sdx3f != NULL) {
2019       while ((c + 2) < col_max) {
2020         int i;
2021         DECLARE_ALIGNED(16, uint32_t, sads[3]);
2022
2023         fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2024                       sads);
2025
2026         for (i = 0; i < 3; ++i) {
2027           unsigned int sad = sads[i];
2028           if (sad < best_sad) {
2029             const MV mv = {r, c};
2030             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2031             if (sad < best_sad) {
2032               best_sad = sad;
2033               *best_mv = mv;
2034             }
2035           }
2036           ++check_here;
2037           ++c;
2038         }
2039       }
2040     }
2041
2042     while (c < col_max) {
2043       unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2044                                      check_here, in_what->stride);
2045       if (sad < best_sad) {
2046         const MV mv = {r, c};
2047         sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2048         if (sad < best_sad) {
2049           best_sad = sad;
2050           *best_mv = mv;
2051         }
2052       }
2053       ++check_here;
2054       ++c;
2055     }
2056   }
2057
2058   return best_sad;
2059 }
2060
2061 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
2062                           int sad_per_bit, int distance,
2063                           const vp9_variance_fn_ptr_t *fn_ptr,
2064                           const MV *center_mv, MV *best_mv) {
2065   int r;
2066   const MACROBLOCKD *const xd = &x->e_mbd;
2067   const struct buf_2d *const what = &x->plane[0].src;
2068   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2069   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2070   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2071   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2072   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2073   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2074   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2075       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2076       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2077   *best_mv = *ref_mv;
2078
2079   for (r = row_min; r < row_max; ++r) {
2080     int c = col_min;
2081     const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2082
2083     if (fn_ptr->sdx8f != NULL) {
2084       while ((c + 7) < col_max) {
2085         int i;
2086         DECLARE_ALIGNED(16, uint32_t, sads[8]);
2087
2088         fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2089                       sads);
2090
2091         for (i = 0; i < 8; ++i) {
2092           unsigned int sad = sads[i];
2093           if (sad < best_sad) {
2094             const MV mv = {r, c};
2095             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2096             if (sad < best_sad) {
2097               best_sad = sad;
2098               *best_mv = mv;
2099             }
2100           }
2101           ++check_here;
2102           ++c;
2103         }
2104       }
2105     }
2106
2107     if (fn_ptr->sdx3f != NULL) {
2108       while ((c + 2) < col_max) {
2109         int i;
2110         DECLARE_ALIGNED(16, uint32_t, sads[3]);
2111
2112         fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2113                       sads);
2114
2115         for (i = 0; i < 3; ++i) {
2116           unsigned int sad = sads[i];
2117           if (sad < best_sad) {
2118             const MV mv = {r, c};
2119             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2120             if (sad < best_sad) {
2121               best_sad = sad;
2122               *best_mv = mv;
2123             }
2124           }
2125           ++check_here;
2126           ++c;
2127         }
2128       }
2129     }
2130
2131     while (c < col_max) {
2132       unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2133                                      check_here, in_what->stride);
2134       if (sad < best_sad) {
2135         const MV mv = {r, c};
2136         sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2137         if (sad < best_sad) {
2138           best_sad = sad;
2139           *best_mv = mv;
2140         }
2141       }
2142       ++check_here;
2143       ++c;
2144     }
2145   }
2146
2147   return best_sad;
2148 }
2149
2150 int vp9_refining_search_sad(const MACROBLOCK *x,
2151                             MV *ref_mv, int error_per_bit,
2152                             int search_range,
2153                             const vp9_variance_fn_ptr_t *fn_ptr,
2154                             const MV *center_mv) {
2155   const MACROBLOCKD *const xd = &x->e_mbd;
2156   const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
2157   const struct buf_2d *const what = &x->plane[0].src;
2158   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2159   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2160   const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
2161   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
2162                                     in_what->stride) +
2163       mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2164   int i, j;
2165
2166   for (i = 0; i < search_range; i++) {
2167     int best_site = -1;
2168     const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
2169                        ((ref_mv->row + 1) < x->mv_row_max) &
2170                        ((ref_mv->col - 1) > x->mv_col_min) &
2171                        ((ref_mv->col + 1) < x->mv_col_max);
2172
2173     if (all_in) {
2174       unsigned int sads[4];
2175       const uint8_t *const positions[4] = {
2176         best_address - in_what->stride,
2177         best_address - 1,
2178         best_address + 1,
2179         best_address + in_what->stride
2180       };
2181
2182       fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2183
2184       for (j = 0; j < 4; ++j) {
2185         if (sads[j] < best_sad) {
2186           const MV mv = {ref_mv->row + neighbors[j].row,
2187                          ref_mv->col + neighbors[j].col};
2188           sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2189           if (sads[j] < best_sad) {
2190             best_sad = sads[j];
2191             best_site = j;
2192           }
2193         }
2194       }
2195     } else {
2196       for (j = 0; j < 4; ++j) {
2197         const MV mv = {ref_mv->row + neighbors[j].row,
2198                        ref_mv->col + neighbors[j].col};
2199
2200         if (is_mv_in(x, &mv)) {
2201           unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2202                                          get_buf_from_mv(in_what, &mv),
2203                                          in_what->stride);
2204           if (sad < best_sad) {
2205             sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2206             if (sad < best_sad) {
2207               best_sad = sad;
2208               best_site = j;
2209             }
2210           }
2211         }
2212       }
2213     }
2214
2215     if (best_site == -1) {
2216       break;
2217     } else {
2218       ref_mv->row += neighbors[best_site].row;
2219       ref_mv->col += neighbors[best_site].col;
2220       best_address = get_buf_from_mv(in_what, ref_mv);
2221     }
2222   }
2223
2224   return best_sad;
2225 }
2226
2227 // This function is called when we do joint motion search in comp_inter_inter
2228 // mode.
2229 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2230                              MV *ref_mv, int error_per_bit,
2231                              int search_range,
2232                              const vp9_variance_fn_ptr_t *fn_ptr,
2233                              const MV *center_mv,
2234                              const uint8_t *second_pred) {
2235   const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
2236                            {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
2237   const MACROBLOCKD *const xd = &x->e_mbd;
2238   const struct buf_2d *const what = &x->plane[0].src;
2239   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2240   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2241   unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
2242       get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
2243       mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2244   int i, j;
2245
2246   for (i = 0; i < search_range; ++i) {
2247     int best_site = -1;
2248
2249     for (j = 0; j < 8; ++j) {
2250       const MV mv = {ref_mv->row + neighbors[j].row,
2251                      ref_mv->col + neighbors[j].col};
2252
2253       if (is_mv_in(x, &mv)) {
2254         unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
2255             get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
2256         if (sad < best_sad) {
2257           sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2258           if (sad < best_sad) {
2259             best_sad = sad;
2260             best_site = j;
2261           }
2262         }
2263       }
2264     }
2265
2266     if (best_site == -1) {
2267       break;
2268     } else {
2269       ref_mv->row += neighbors[best_site].row;
2270       ref_mv->col += neighbors[best_site].col;
2271     }
2272   }
2273   return best_sad;
2274 }
2275
2276 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
2277                           BLOCK_SIZE bsize, MV *mvp_full,
2278                           int step_param, int error_per_bit,
2279                           int *cost_list,
2280                           const MV *ref_mv, MV *tmp_mv,
2281                           int var_max, int rd) {
2282   const SPEED_FEATURES *const sf = &cpi->sf;
2283   const SEARCH_METHODS method = sf->mv.search_method;
2284   vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
2285   int var = 0;
2286   if (cost_list) {
2287     cost_list[0] = INT_MAX;
2288     cost_list[1] = INT_MAX;
2289     cost_list[2] = INT_MAX;
2290     cost_list[3] = INT_MAX;
2291     cost_list[4] = INT_MAX;
2292   }
2293
2294   switch (method) {
2295     case FAST_DIAMOND:
2296       var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2297                                 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2298       break;
2299     case FAST_HEX:
2300       var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2301                                 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2302       break;
2303     case HEX:
2304       var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2305                            cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2306       break;
2307     case SQUARE:
2308       var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2309                               cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2310       break;
2311     case BIGDIA:
2312       var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2313                               cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2314       break;
2315     case NSTEP:
2316       var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2317                                    MAX_MVSEARCH_STEPS - 1 - step_param,
2318                                    1, cost_list, fn_ptr, ref_mv, tmp_mv);
2319       break;
2320     default:
2321       assert(0 && "Invalid search method.");
2322   }
2323
2324   if (method != NSTEP && rd && var < var_max)
2325     var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);
2326
2327   return var;
2328 }