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