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