Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_mcomp.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vpx_config.h"
16
17 #include "vpx_mem/vpx_mem.h"
18
19 #include "vp9/common/vp9_common.h"
20
21 #include "vp9/encoder/vp9_encoder.h"
22 #include "vp9/encoder/vp9_mcomp.h"
23
24 // #define NEW_DIAMOND_SEARCH
25
26 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
27                                              const MV *mv) {
28   return &buf->buf[mv->row * buf->stride + mv->col];
29 }
30
31 void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
32   int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
33   int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
34   int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
35   int row_max = (mv->row >> 3) + MAX_FULL_PEL_VAL;
36
37   col_min = MAX(col_min, (MV_LOW >> 3) + 1);
38   row_min = MAX(row_min, (MV_LOW >> 3) + 1);
39   col_max = MIN(col_max, (MV_UPP >> 3) - 1);
40   row_max = MIN(row_max, (MV_UPP >> 3) - 1);
41
42   // Get intersection of UMV window and valid MV window to reduce # of checks
43   // in diamond search.
44   if (x->mv_col_min < col_min)
45     x->mv_col_min = col_min;
46   if (x->mv_col_max > col_max)
47     x->mv_col_max = col_max;
48   if (x->mv_row_min < row_min)
49     x->mv_row_min = row_min;
50   if (x->mv_row_max > row_max)
51     x->mv_row_max = row_max;
52 }
53
54 int vp9_init_search_range(int size) {
55   int sr = 0;
56   // Minimum search size no matter what the passed in value.
57   size = MAX(16, size);
58
59   while ((size << sr) < MAX_FULL_PEL_VAL)
60     sr++;
61
62   sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
63   return sr;
64 }
65
66 static INLINE int mv_cost(const MV *mv,
67                           const int *joint_cost, int *const comp_cost[2]) {
68   return joint_cost[vp9_get_mv_joint(mv)] +
69              comp_cost[0][mv->row] + comp_cost[1][mv->col];
70 }
71
72 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
73                     const int *mvjcost, int *mvcost[2], int weight) {
74   const MV diff = { mv->row - ref->row,
75                     mv->col - ref->col };
76   return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
77 }
78
79 static int mv_err_cost(const MV *mv, const MV *ref,
80                        const int *mvjcost, int *mvcost[2],
81                        int error_per_bit) {
82   if (mvcost) {
83     const MV diff = { mv->row - ref->row,
84                       mv->col - ref->col };
85     return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
86                                   error_per_bit, 13);
87   }
88   return 0;
89 }
90
91 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
92                           int error_per_bit) {
93   if (x->nmvsadcost) {
94     const MV diff = { mv->row - ref->row,
95                       mv->col - ref->col };
96     return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
97                                       x->nmvsadcost) * error_per_bit, 8);
98   }
99   return 0;
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 #if CONFIG_VP9_HIGHBITDEPTH
290 #define SETUP_CENTER_ERROR                                                   \
291   if (second_pred != NULL) {                                                 \
292     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {                       \
293       DECLARE_ALIGNED_ARRAY(16, uint16_t, comp_pred16, 64 * 64);             \
294       vp9_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset,   \
295                                y_stride);                                    \
296       besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, z, src_stride,   \
297                         sse1);                                               \
298     } else {                                                                 \
299       DECLARE_ALIGNED_ARRAY(16, uint8_t, comp_pred, 64 * 64);                \
300       vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride); \
301       besterr = vfp->vf(comp_pred, w, z, src_stride, sse1);                  \
302     }                                                                        \
303   } else {                                                                   \
304     besterr = vfp->vf(y + offset, y_stride, z, src_stride, sse1);            \
305   }                                                                          \
306   *distortion = besterr;                                                     \
307   besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
308
309 #else
310
311 #define SETUP_CENTER_ERROR                                                   \
312   if (second_pred != NULL) {                                                 \
313     DECLARE_ALIGNED_ARRAY(16, uint8_t, comp_pred, 64 * 64);                  \
314     vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);   \
315     besterr = vfp->vf(comp_pred, w, z, src_stride, sse1);                    \
316   } else {                                                                   \
317     besterr = vfp->vf(y + offset, y_stride, z, src_stride, sse1);            \
318   }                                                                          \
319   *distortion = besterr;                                                     \
320   besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
321 #endif  // CONFIG_VP9_HIGHBITDEPTH
322
323
324
325
326 static INLINE int divide_and_round(const int n, const int d) {
327   return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
328 }
329
330 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
331   return cost_list[0] < cost_list[1] &&
332          cost_list[0] < cost_list[2] &&
333          cost_list[0] < cost_list[3] &&
334          cost_list[0] < cost_list[4];
335 }
336
337 // Returns surface minima estimate at given precision in 1/2^n bits.
338 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
339 // For a given set of costs S0, S1, S2, S3, S4 at points
340 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
341 // the solution for the location of the minima (x0, y0) is given by:
342 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
343 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
344 // The code below is an integerized version of that.
345 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
346                               int bits) {
347   *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
348                          (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
349   *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
350                          (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
351 }
352
353 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
354     const MACROBLOCK *x,
355     MV *bestmv, const MV *ref_mv,
356     int allow_hp,
357     int error_per_bit,
358     const vp9_variance_fn_ptr_t *vfp,
359     int forced_stop,
360     int iters_per_step,
361     int *cost_list,
362     int *mvjcost, int *mvcost[2],
363     int *distortion,
364     unsigned int *sse1,
365     const uint8_t *second_pred,
366     int w, int h) {
367   SETUP_SUBPEL_SEARCH;
368   SETUP_CENTER_ERROR;
369   (void) halfiters;
370   (void) quarteriters;
371   (void) eighthiters;
372   (void) whichdir;
373   (void) allow_hp;
374   (void) forced_stop;
375   (void) hstep;
376
377   if (cost_list &&
378       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
379       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
380       cost_list[4] != INT_MAX &&
381       is_cost_list_wellbehaved(cost_list)) {
382     int ir, ic;
383     unsigned int minpt;
384     get_cost_surf_min(cost_list, &ir, &ic, 2);
385     if (ir != 0 || ic != 0) {
386       CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
387     }
388   } else {
389     FIRST_LEVEL_CHECKS;
390     if (halfiters > 1) {
391       SECOND_LEVEL_CHECKS;
392     }
393
394     tr = br;
395     tc = bc;
396
397     // Each subsequent iteration checks at least one point in common with
398     // the last iteration could be 2 ( if diag selected) 1/4 pel
399     // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
400     if (forced_stop != 2) {
401       hstep >>= 1;
402       FIRST_LEVEL_CHECKS;
403       if (quarteriters > 1) {
404         SECOND_LEVEL_CHECKS;
405       }
406     }
407   }
408
409   tr = br;
410   tc = bc;
411
412   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
413     hstep >>= 1;
414     FIRST_LEVEL_CHECKS;
415     if (eighthiters > 1) {
416       SECOND_LEVEL_CHECKS;
417     }
418   }
419
420   bestmv->row = br;
421   bestmv->col = bc;
422
423   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
424       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
425     return INT_MAX;
426
427   return besterr;
428 }
429
430 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
431                                              MV *bestmv, const MV *ref_mv,
432                                              int allow_hp,
433                                              int error_per_bit,
434                                              const vp9_variance_fn_ptr_t *vfp,
435                                              int forced_stop,
436                                              int iters_per_step,
437                                              int *cost_list,
438                                              int *mvjcost, int *mvcost[2],
439                                              int *distortion,
440                                              unsigned int *sse1,
441                                              const uint8_t *second_pred,
442                                              int w, int h) {
443   SETUP_SUBPEL_SEARCH;
444   SETUP_CENTER_ERROR;
445   if (cost_list &&
446       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
447       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
448       cost_list[4] != INT_MAX &&
449       is_cost_list_wellbehaved(cost_list)) {
450     unsigned int minpt;
451     int ir, ic;
452     get_cost_surf_min(cost_list, &ir, &ic, 1);
453     if (ir != 0 || ic != 0) {
454       CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
455     }
456   } else {
457     FIRST_LEVEL_CHECKS;
458     if (halfiters > 1) {
459       SECOND_LEVEL_CHECKS;
460     }
461   }
462
463   // Each subsequent iteration checks at least one point in common with
464   // the last iteration could be 2 ( if diag selected) 1/4 pel
465
466   // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
467   if (forced_stop != 2) {
468     tr = br;
469     tc = bc;
470     hstep >>= 1;
471     FIRST_LEVEL_CHECKS;
472     if (quarteriters > 1) {
473       SECOND_LEVEL_CHECKS;
474     }
475   }
476
477   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
478     tr = br;
479     tc = bc;
480     hstep >>= 1;
481     FIRST_LEVEL_CHECKS;
482     if (eighthiters > 1) {
483       SECOND_LEVEL_CHECKS;
484     }
485   }
486   // These lines insure static analysis doesn't warn that
487   // tr and tc aren't used after the above point.
488   (void) tr;
489   (void) tc;
490
491   bestmv->row = br;
492   bestmv->col = bc;
493
494   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
495       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
496     return INT_MAX;
497
498   return besterr;
499 }
500
501 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
502                                         MV *bestmv, const MV *ref_mv,
503                                         int allow_hp,
504                                         int error_per_bit,
505                                         const vp9_variance_fn_ptr_t *vfp,
506                                         int forced_stop,
507                                         int iters_per_step,
508                                         int *cost_list,
509                                         int *mvjcost, int *mvcost[2],
510                                         int *distortion,
511                                         unsigned int *sse1,
512                                         const uint8_t *second_pred,
513                                         int w, int h) {
514   SETUP_SUBPEL_SEARCH;
515   SETUP_CENTER_ERROR;
516   if (cost_list &&
517       cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
518       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
519       cost_list[4] != INT_MAX) {
520     unsigned int left, right, up, down, diag;
521     whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
522                (cost_list[2] < cost_list[4] ? 0 : 2);
523     switch (whichdir) {
524       case 0:
525         CHECK_BETTER(left, tr, tc - hstep);
526         CHECK_BETTER(down, tr + hstep, tc);
527         CHECK_BETTER(diag, tr + hstep, tc - hstep);
528         break;
529       case 1:
530         CHECK_BETTER(right, tr, tc + hstep);
531         CHECK_BETTER(down, tr + hstep, tc);
532         CHECK_BETTER(diag, tr + hstep, tc + hstep);
533         break;
534       case 2:
535         CHECK_BETTER(left, tr, tc - hstep);
536         CHECK_BETTER(up, tr - hstep, tc);
537         CHECK_BETTER(diag, tr - hstep, tc - hstep);
538         break;
539       case 3:
540         CHECK_BETTER(right, tr, tc + hstep);
541         CHECK_BETTER(up, tr - hstep, tc);
542         CHECK_BETTER(diag, tr - hstep, tc + hstep);
543         break;
544     }
545   } else {
546     FIRST_LEVEL_CHECKS;
547     if (halfiters > 1) {
548       SECOND_LEVEL_CHECKS;
549     }
550   }
551
552   tr = br;
553   tc = bc;
554
555   // Each subsequent iteration checks at least one point in common with
556   // the last iteration could be 2 ( if diag selected) 1/4 pel
557
558   // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
559   if (forced_stop != 2) {
560     hstep >>= 1;
561     FIRST_LEVEL_CHECKS;
562     if (quarteriters > 1) {
563       SECOND_LEVEL_CHECKS;
564     }
565     tr = br;
566     tc = bc;
567   }
568
569   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
570     hstep >>= 1;
571     FIRST_LEVEL_CHECKS;
572     if (eighthiters > 1) {
573       SECOND_LEVEL_CHECKS;
574     }
575     tr = br;
576     tc = bc;
577   }
578   // These lines insure static analysis doesn't warn that
579   // tr and tc aren't used after the above point.
580   (void) tr;
581   (void) tc;
582
583   bestmv->row = br;
584   bestmv->col = bc;
585
586   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
587       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
588     return INT_MAX;
589
590   return besterr;
591 }
592
593 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
594                                  MV *bestmv, const MV *ref_mv,
595                                  int allow_hp,
596                                  int error_per_bit,
597                                  const vp9_variance_fn_ptr_t *vfp,
598                                  int forced_stop,
599                                  int iters_per_step,
600                                  int *cost_list,
601                                  int *mvjcost, int *mvcost[2],
602                                  int *distortion,
603                                  unsigned int *sse1,
604                                  const uint8_t *second_pred,
605                                  int w, int h) {
606   SETUP_SUBPEL_SEARCH;
607   SETUP_CENTER_ERROR;
608   (void) cost_list;  // to silence compiler warning
609
610   // Each subsequent iteration checks at least one point in
611   // common with the last iteration could be 2 ( if diag selected)
612   // 1/2 pel
613   FIRST_LEVEL_CHECKS;
614   if (halfiters > 1) {
615     SECOND_LEVEL_CHECKS;
616   }
617   tr = br;
618   tc = bc;
619
620   // Each subsequent iteration checks at least one point in common with
621   // the last iteration could be 2 ( if diag selected) 1/4 pel
622
623   // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
624   if (forced_stop != 2) {
625     hstep >>= 1;
626     FIRST_LEVEL_CHECKS;
627     if (quarteriters > 1) {
628       SECOND_LEVEL_CHECKS;
629     }
630     tr = br;
631     tc = bc;
632   }
633
634   if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
635     hstep >>= 1;
636     FIRST_LEVEL_CHECKS;
637     if (eighthiters > 1) {
638       SECOND_LEVEL_CHECKS;
639     }
640     tr = br;
641     tc = bc;
642   }
643   // These lines insure static analysis doesn't warn that
644   // tr and tc aren't used after the above point.
645   (void) tr;
646   (void) tc;
647
648   bestmv->row = br;
649   bestmv->col = bc;
650
651   if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
652       (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
653     return INT_MAX;
654
655   return besterr;
656 }
657
658 #undef MVC
659 #undef PRE
660 #undef CHECK_BETTER
661
662 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
663                                int range) {
664   return ((row - range) >= x->mv_row_min) &
665          ((row + range) <= x->mv_row_max) &
666          ((col - range) >= x->mv_col_min) &
667          ((col + range) <= x->mv_col_max);
668 }
669
670 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
671   return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
672          (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
673 }
674
675 #define CHECK_BETTER \
676   {\
677     if (thissad < bestsad) {\
678       if (use_mvcost) \
679         thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
680       if (thissad < bestsad) {\
681         bestsad = thissad;\
682         best_site = i;\
683       }\
684     }\
685   }
686
687 #define MAX_PATTERN_SCALES         11
688 #define MAX_PATTERN_CANDIDATES      8  // max number of canddiates per scale
689 #define PATTERN_CANDIDATES_REF      3  // number of refinement candidates
690
691 // Calculate and return a sad+mvcost list around an integer best pel.
692 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
693                                       const MV *ref_mv,
694                                       int sadpb,
695                                       const vp9_variance_fn_ptr_t *fn_ptr,
696                                       const MV *best_mv,
697                                       int *cost_list) {
698   static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
699   const struct buf_2d *const what = &x->plane[0].src;
700   const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
701   const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
702   int br = best_mv->row;
703   int bc = best_mv->col;
704   MV this_mv;
705   int i;
706   unsigned int sse;
707
708   this_mv.row = br;
709   this_mv.col = bc;
710   cost_list[0] = fn_ptr->vf(what->buf, what->stride,
711                             get_buf_from_mv(in_what, &this_mv),
712                             in_what->stride, &sse) +
713       mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
714   if (check_bounds(x, br, bc, 1)) {
715     for (i = 0; i < 4; i++) {
716       const MV this_mv = {br + neighbors[i].row,
717         bc + neighbors[i].col};
718       cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
719                                     get_buf_from_mv(in_what, &this_mv),
720                                     in_what->stride, &sse) +
721           // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
722           mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
723                       x->errorperbit);
724     }
725   } else {
726     for (i = 0; i < 4; i++) {
727       const MV this_mv = {br + neighbors[i].row,
728         bc + neighbors[i].col};
729       if (!is_mv_in(x, &this_mv))
730         cost_list[i + 1] = INT_MAX;
731       else
732         cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
733                                       get_buf_from_mv(in_what, &this_mv),
734                                       in_what->stride, &sse) +
735             // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
736             mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
737                         x->errorperbit);
738     }
739   }
740 }
741
742 // Generic pattern search function that searches over multiple scales.
743 // Each scale can have a different number of candidates and shape of
744 // candidates as indicated in the num_candidates and candidates arrays
745 // passed into this function
746 //
747 static int vp9_pattern_search(const MACROBLOCK *x,
748                               MV *ref_mv,
749                               int search_param,
750                               int sad_per_bit,
751                               int do_init_search,
752                               int *cost_list,
753                               const vp9_variance_fn_ptr_t *vfp,
754                               int use_mvcost,
755                               const MV *center_mv,
756                               MV *best_mv,
757                               const int num_candidates[MAX_PATTERN_SCALES],
758                               const MV candidates[MAX_PATTERN_SCALES]
759                                                  [MAX_PATTERN_CANDIDATES]) {
760   const MACROBLOCKD *const xd = &x->e_mbd;
761   static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
762     10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
763   };
764   int i, s, t;
765   const struct buf_2d *const what = &x->plane[0].src;
766   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
767   int br, bc;
768   int bestsad = INT_MAX;
769   int thissad;
770   int k = -1;
771   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
772   int best_init_s = search_param_to_steps[search_param];
773   // adjust ref_mv to make sure it is within MV range
774   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
775   br = ref_mv->row;
776   bc = ref_mv->col;
777
778   // Work out the start point for the search
779   bestsad = vfp->sdf(what->buf, what->stride,
780                      get_buf_from_mv(in_what, ref_mv), in_what->stride) +
781       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
782
783   // Search all possible scales upto the search param around the center point
784   // pick the scale of the point that is best as the starting scale of
785   // further steps around it.
786   if (do_init_search) {
787     s = best_init_s;
788     best_init_s = -1;
789     for (t = 0; t <= s; ++t) {
790       int best_site = -1;
791       if (check_bounds(x, br, bc, 1 << t)) {
792         for (i = 0; i < num_candidates[t]; i++) {
793           const MV this_mv = {br + candidates[t][i].row,
794                               bc + candidates[t][i].col};
795           thissad = vfp->sdf(what->buf, what->stride,
796                              get_buf_from_mv(in_what, &this_mv),
797                              in_what->stride);
798           CHECK_BETTER
799         }
800       } else {
801         for (i = 0; i < num_candidates[t]; i++) {
802           const MV this_mv = {br + candidates[t][i].row,
803                               bc + candidates[t][i].col};
804           if (!is_mv_in(x, &this_mv))
805             continue;
806           thissad = vfp->sdf(what->buf, what->stride,
807                              get_buf_from_mv(in_what, &this_mv),
808                              in_what->stride);
809           CHECK_BETTER
810         }
811       }
812       if (best_site == -1) {
813         continue;
814       } else {
815         best_init_s = t;
816         k = best_site;
817       }
818     }
819     if (best_init_s != -1) {
820       br += candidates[best_init_s][k].row;
821       bc += candidates[best_init_s][k].col;
822     }
823   }
824
825   // If the center point is still the best, just skip this and move to
826   // the refinement step.
827   if (best_init_s != -1) {
828     int best_site = -1;
829     s = best_init_s;
830
831     do {
832       // No need to search all 6 points the 1st time if initial search was used
833       if (!do_init_search || s != best_init_s) {
834         if (check_bounds(x, br, bc, 1 << s)) {
835           for (i = 0; i < num_candidates[s]; i++) {
836             const MV this_mv = {br + candidates[s][i].row,
837                                 bc + candidates[s][i].col};
838             thissad = vfp->sdf(what->buf, what->stride,
839                                get_buf_from_mv(in_what, &this_mv),
840                                in_what->stride);
841             CHECK_BETTER
842           }
843         } else {
844           for (i = 0; i < num_candidates[s]; i++) {
845             const MV this_mv = {br + candidates[s][i].row,
846                                 bc + candidates[s][i].col};
847             if (!is_mv_in(x, &this_mv))
848               continue;
849             thissad = vfp->sdf(what->buf, what->stride,
850                                get_buf_from_mv(in_what, &this_mv),
851                                in_what->stride);
852             CHECK_BETTER
853           }
854         }
855
856         if (best_site == -1) {
857           continue;
858         } else {
859           br += candidates[s][best_site].row;
860           bc += candidates[s][best_site].col;
861           k = best_site;
862         }
863       }
864
865       do {
866         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
867         best_site = -1;
868         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
869         next_chkpts_indices[1] = k;
870         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
871
872         if (check_bounds(x, br, bc, 1 << s)) {
873           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
874             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
875                                 bc + candidates[s][next_chkpts_indices[i]].col};
876             thissad = vfp->sdf(what->buf, what->stride,
877                                get_buf_from_mv(in_what, &this_mv),
878                                in_what->stride);
879             CHECK_BETTER
880           }
881         } else {
882           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
883             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
884                                 bc + candidates[s][next_chkpts_indices[i]].col};
885             if (!is_mv_in(x, &this_mv))
886               continue;
887             thissad = vfp->sdf(what->buf, what->stride,
888                                get_buf_from_mv(in_what, &this_mv),
889                                in_what->stride);
890             CHECK_BETTER
891           }
892         }
893
894         if (best_site != -1) {
895           k = next_chkpts_indices[best_site];
896           br += candidates[s][k].row;
897           bc += candidates[s][k].col;
898         }
899       } while (best_site != -1);
900     } while (s--);
901   }
902
903   // Returns the one-away integer pel sad values around the best as follows:
904   // cost_list[0]: cost at the best integer pel
905   // cost_list[1]: cost at delta {0, -1} (left)   from the best integer pel
906   // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
907   // cost_list[3]: cost at delta { 0, 1} (right)  from the best integer pel
908   // cost_list[4]: cost at delta {-1, 0} (top)    from the best integer pel
909   if (cost_list) {
910     const MV best_mv = { br, bc };
911     calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
912   }
913   best_mv->row = br;
914   best_mv->col = bc;
915   return bestsad;
916 }
917
918 // A specialized function where the smallest scale search candidates
919 // are 4 1-away neighbors, and cost_list is non-null
920 // TODO(debargha): Merge this function with the one above. Also remove
921 // use_mvcost option since it is always 1, to save unnecessary branches.
922 static int vp9_pattern_search_sad(const MACROBLOCK *x,
923                                   MV *ref_mv,
924                                   int search_param,
925                                   int sad_per_bit,
926                                   int do_init_search,
927                                   int *cost_list,
928                                   const vp9_variance_fn_ptr_t *vfp,
929                                   int use_mvcost,
930                                   const MV *center_mv,
931                                   MV *best_mv,
932                                   const int num_candidates[MAX_PATTERN_SCALES],
933                                   const MV candidates[MAX_PATTERN_SCALES]
934                                                      [MAX_PATTERN_CANDIDATES]) {
935   const MACROBLOCKD *const xd = &x->e_mbd;
936   static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
937     10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
938   };
939   int i, s, t;
940   const struct buf_2d *const what = &x->plane[0].src;
941   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
942   int br, bc;
943   int bestsad = INT_MAX;
944   int thissad;
945   int k = -1;
946   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
947   int best_init_s = search_param_to_steps[search_param];
948   // adjust ref_mv to make sure it is within MV range
949   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
950   br = ref_mv->row;
951   bc = ref_mv->col;
952   if (cost_list != NULL) {
953     cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
954         INT_MAX;
955   }
956
957   // Work out the start point for the search
958   bestsad = vfp->sdf(what->buf, what->stride,
959                      get_buf_from_mv(in_what, ref_mv), in_what->stride) +
960       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
961
962   // Search all possible scales upto the search param around the center point
963   // pick the scale of the point that is best as the starting scale of
964   // further steps around it.
965   if (do_init_search) {
966     s = best_init_s;
967     best_init_s = -1;
968     for (t = 0; t <= s; ++t) {
969       int best_site = -1;
970       if (check_bounds(x, br, bc, 1 << t)) {
971         for (i = 0; i < num_candidates[t]; i++) {
972           const MV this_mv = {br + candidates[t][i].row,
973                               bc + candidates[t][i].col};
974           thissad = vfp->sdf(what->buf, what->stride,
975                              get_buf_from_mv(in_what, &this_mv),
976                              in_what->stride);
977           CHECK_BETTER
978         }
979       } else {
980         for (i = 0; i < num_candidates[t]; i++) {
981           const MV this_mv = {br + candidates[t][i].row,
982                               bc + candidates[t][i].col};
983           if (!is_mv_in(x, &this_mv))
984             continue;
985           thissad = vfp->sdf(what->buf, what->stride,
986                              get_buf_from_mv(in_what, &this_mv),
987                              in_what->stride);
988           CHECK_BETTER
989         }
990       }
991       if (best_site == -1) {
992         continue;
993       } else {
994         best_init_s = t;
995         k = best_site;
996       }
997     }
998     if (best_init_s != -1) {
999       br += candidates[best_init_s][k].row;
1000       bc += candidates[best_init_s][k].col;
1001     }
1002   }
1003
1004   // If the center point is still the best, just skip this and move to
1005   // the refinement step.
1006   if (best_init_s != -1) {
1007     int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1008     int best_site = -1;
1009     s = best_init_s;
1010
1011     for (; s >= do_sad; s--) {
1012       if (!do_init_search || s != best_init_s) {
1013         if (check_bounds(x, br, bc, 1 << s)) {
1014           for (i = 0; i < num_candidates[s]; i++) {
1015             const MV this_mv = {br + candidates[s][i].row,
1016                                 bc + candidates[s][i].col};
1017             thissad = vfp->sdf(what->buf, what->stride,
1018                                get_buf_from_mv(in_what, &this_mv),
1019                                in_what->stride);
1020             CHECK_BETTER
1021           }
1022         } else {
1023           for (i = 0; i < num_candidates[s]; i++) {
1024             const MV this_mv = {br + candidates[s][i].row,
1025                                 bc + candidates[s][i].col};
1026             if (!is_mv_in(x, &this_mv))
1027               continue;
1028             thissad = vfp->sdf(what->buf, what->stride,
1029                                get_buf_from_mv(in_what, &this_mv),
1030                                in_what->stride);
1031             CHECK_BETTER
1032           }
1033         }
1034
1035         if (best_site == -1) {
1036           continue;
1037         } else {
1038           br += candidates[s][best_site].row;
1039           bc += candidates[s][best_site].col;
1040           k = best_site;
1041         }
1042       }
1043
1044       do {
1045         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1046         best_site = -1;
1047         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1048         next_chkpts_indices[1] = k;
1049         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1050
1051         if (check_bounds(x, br, bc, 1 << s)) {
1052           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1053             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1054                                 bc + candidates[s][next_chkpts_indices[i]].col};
1055             thissad = vfp->sdf(what->buf, what->stride,
1056                                get_buf_from_mv(in_what, &this_mv),
1057                                in_what->stride);
1058             CHECK_BETTER
1059           }
1060         } else {
1061           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1062             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1063                                 bc + candidates[s][next_chkpts_indices[i]].col};
1064             if (!is_mv_in(x, &this_mv))
1065               continue;
1066             thissad = vfp->sdf(what->buf, what->stride,
1067                                get_buf_from_mv(in_what, &this_mv),
1068                                in_what->stride);
1069             CHECK_BETTER
1070           }
1071         }
1072
1073         if (best_site != -1) {
1074           k = next_chkpts_indices[best_site];
1075           br += candidates[s][k].row;
1076           bc += candidates[s][k].col;
1077         }
1078       } while (best_site != -1);
1079     }
1080
1081     // Note: If we enter the if below, then cost_list must be non-NULL.
1082     if (s == 0) {
1083       cost_list[0] = bestsad;
1084       if (!do_init_search || s != best_init_s) {
1085         if (check_bounds(x, br, bc, 1 << s)) {
1086           for (i = 0; i < num_candidates[s]; i++) {
1087             const MV this_mv = {br + candidates[s][i].row,
1088                                 bc + candidates[s][i].col};
1089             cost_list[i + 1] =
1090             thissad = vfp->sdf(what->buf, what->stride,
1091                                get_buf_from_mv(in_what, &this_mv),
1092                                in_what->stride);
1093             CHECK_BETTER
1094           }
1095         } else {
1096           for (i = 0; i < num_candidates[s]; i++) {
1097             const MV this_mv = {br + candidates[s][i].row,
1098                                 bc + candidates[s][i].col};
1099             if (!is_mv_in(x, &this_mv))
1100               continue;
1101             cost_list[i + 1] =
1102             thissad = vfp->sdf(what->buf, what->stride,
1103                                get_buf_from_mv(in_what, &this_mv),
1104                                in_what->stride);
1105             CHECK_BETTER
1106           }
1107         }
1108
1109         if (best_site != -1) {
1110           br += candidates[s][best_site].row;
1111           bc += candidates[s][best_site].col;
1112           k = best_site;
1113         }
1114       }
1115       while (best_site != -1) {
1116         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1117         best_site = -1;
1118         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1119         next_chkpts_indices[1] = k;
1120         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1121         cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1122         cost_list[((k + 2) % 4) + 1] = cost_list[0];
1123         cost_list[0] = bestsad;
1124
1125         if (check_bounds(x, br, bc, 1 << s)) {
1126           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1127             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1128                                 bc + candidates[s][next_chkpts_indices[i]].col};
1129             cost_list[next_chkpts_indices[i] + 1] =
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 < PATTERN_CANDIDATES_REF; i++) {
1137             const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1138                                 bc + candidates[s][next_chkpts_indices[i]].col};
1139             if (!is_mv_in(x, &this_mv)) {
1140               cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1141               continue;
1142             }
1143             cost_list[next_chkpts_indices[i] + 1] =
1144             thissad = vfp->sdf(what->buf, what->stride,
1145                                get_buf_from_mv(in_what, &this_mv),
1146                                in_what->stride);
1147             CHECK_BETTER
1148           }
1149         }
1150
1151         if (best_site != -1) {
1152           k = next_chkpts_indices[best_site];
1153           br += candidates[s][k].row;
1154           bc += candidates[s][k].col;
1155         }
1156       }
1157     }
1158   }
1159
1160   // Returns the one-away integer pel sad values around the best as follows:
1161   // cost_list[0]: sad at the best integer pel
1162   // cost_list[1]: sad at delta {0, -1} (left)   from the best integer pel
1163   // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1164   // cost_list[3]: sad at delta { 0, 1} (right)  from the best integer pel
1165   // cost_list[4]: sad at delta {-1, 0} (top)    from the best integer pel
1166   if (cost_list) {
1167     static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1168     if (cost_list[0] == INT_MAX) {
1169       cost_list[0] = bestsad;
1170       if (check_bounds(x, br, bc, 1)) {
1171         for (i = 0; i < 4; i++) {
1172           const MV this_mv = { br + neighbors[i].row,
1173                                bc + neighbors[i].col };
1174           cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1175                                      get_buf_from_mv(in_what, &this_mv),
1176                                      in_what->stride);
1177         }
1178       } else {
1179         for (i = 0; i < 4; i++) {
1180           const MV this_mv = {br + neighbors[i].row,
1181             bc + neighbors[i].col};
1182           if (!is_mv_in(x, &this_mv))
1183             cost_list[i + 1] = INT_MAX;
1184           else
1185             cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1186                                        get_buf_from_mv(in_what, &this_mv),
1187                                        in_what->stride);
1188         }
1189       }
1190     } else {
1191       if (use_mvcost) {
1192         for (i = 0; i < 4; i++) {
1193           const MV this_mv = {br + neighbors[i].row,
1194             bc + neighbors[i].col};
1195           if (cost_list[i + 1] != INT_MAX) {
1196             cost_list[i + 1] +=
1197                 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1198           }
1199         }
1200       }
1201     }
1202   }
1203   best_mv->row = br;
1204   best_mv->col = bc;
1205   return bestsad;
1206 }
1207
1208 int vp9_get_mvpred_var(const MACROBLOCK *x,
1209                        const MV *best_mv, const MV *center_mv,
1210                        const vp9_variance_fn_ptr_t *vfp,
1211                        int use_mvcost) {
1212   const MACROBLOCKD *const xd = &x->e_mbd;
1213   const struct buf_2d *const what = &x->plane[0].src;
1214   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1215   const MV mv = {best_mv->row * 8, best_mv->col * 8};
1216   unsigned int unused;
1217
1218   return vfp->vf(what->buf, what->stride,
1219                  get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1220       (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
1221                                  x->mvcost, x->errorperbit) : 0);
1222 }
1223
1224 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1225                           const MV *best_mv, const MV *center_mv,
1226                           const uint8_t *second_pred,
1227                           const vp9_variance_fn_ptr_t *vfp,
1228                           int use_mvcost) {
1229   const MACROBLOCKD *const xd = &x->e_mbd;
1230   const struct buf_2d *const what = &x->plane[0].src;
1231   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1232   const MV mv = {best_mv->row * 8, best_mv->col * 8};
1233   unsigned int unused;
1234
1235   return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1236                    what->buf, what->stride, &unused, second_pred) +
1237       (use_mvcost ?  mv_err_cost(&mv, center_mv, x->nmvjointcost,
1238                                  x->mvcost, x->errorperbit) : 0);
1239 }
1240
1241 int vp9_hex_search(const MACROBLOCK *x,
1242                    MV *ref_mv,
1243                    int search_param,
1244                    int sad_per_bit,
1245                    int do_init_search,
1246                    int *cost_list,
1247                    const vp9_variance_fn_ptr_t *vfp,
1248                    int use_mvcost,
1249                    const MV *center_mv, MV *best_mv) {
1250   // First scale has 8-closest points, the rest have 6 points in hex shape
1251   // at increasing scales
1252   static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1253     8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1254   };
1255   // Note that the largest candidate step at each scale is 2^scale
1256   static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1257     {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1258     {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1259     {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1260     {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1261     {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1262     {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1263     {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1264     {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1265     {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1266     {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1267     {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1268       { -1024, 0}},
1269   };
1270   return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1271                             do_init_search, cost_list, vfp, use_mvcost,
1272                             center_mv, best_mv,
1273                             hex_num_candidates, hex_candidates);
1274 }
1275
1276 int vp9_bigdia_search(const MACROBLOCK *x,
1277                       MV *ref_mv,
1278                       int search_param,
1279                       int sad_per_bit,
1280                       int do_init_search,
1281                       int *cost_list,
1282                       const vp9_variance_fn_ptr_t *vfp,
1283                       int use_mvcost,
1284                       const MV *center_mv,
1285                       MV *best_mv) {
1286   // First scale has 4-closest points, the rest have 8 points in diamond
1287   // shape at increasing scales
1288   static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1289     4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1290   };
1291   // Note that the largest candidate step at each scale is 2^scale
1292   static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1293                                    [MAX_PATTERN_CANDIDATES] = {
1294     {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1295     {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1296     {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1297     {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1298     {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1299     {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1300       {-16, 16}, {-32, 0}},
1301     {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1302       {-32, 32}, {-64, 0}},
1303     {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1304       {-64, 64}, {-128, 0}},
1305     {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1306       {-128, 128}, {-256, 0}},
1307     {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1308       {-256, 256}, {-512, 0}},
1309     {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1310       {-512, 512}, {-1024, 0}},
1311   };
1312   return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1313                                 do_init_search, cost_list, vfp, use_mvcost,
1314                                 center_mv, best_mv,
1315                                 bigdia_num_candidates, bigdia_candidates);
1316 }
1317
1318 int vp9_square_search(const MACROBLOCK *x,
1319                       MV *ref_mv,
1320                       int search_param,
1321                       int sad_per_bit,
1322                       int do_init_search,
1323                       int *cost_list,
1324                       const vp9_variance_fn_ptr_t *vfp,
1325                       int use_mvcost,
1326                       const MV *center_mv,
1327                       MV *best_mv) {
1328   // All scales have 8 closest points in square shape
1329   static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1330     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1331   };
1332   // Note that the largest candidate step at each scale is 2^scale
1333   static const MV square_candidates[MAX_PATTERN_SCALES]
1334                                    [MAX_PATTERN_CANDIDATES] = {
1335     {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1336     {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1337     {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1338     {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1339     {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1340       {-16, 16}, {-16, 0}},
1341     {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1342       {-32, 32}, {-32, 0}},
1343     {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1344       {-64, 64}, {-64, 0}},
1345     {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1346       {-128, 128}, {-128, 0}},
1347     {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1348       {-256, 256}, {-256, 0}},
1349     {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1350       {-512, 512}, {-512, 0}},
1351     {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1352       {0, 1024}, {-1024, 1024}, {-1024, 0}},
1353   };
1354   return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1355                             do_init_search, cost_list, vfp, use_mvcost,
1356                             center_mv, best_mv,
1357                             square_num_candidates, square_candidates);
1358 }
1359
1360 int vp9_fast_hex_search(const MACROBLOCK *x,
1361                         MV *ref_mv,
1362                         int search_param,
1363                         int sad_per_bit,
1364                         int do_init_search,  // must be zero for fast_hex
1365                         int *cost_list,
1366                         const vp9_variance_fn_ptr_t *vfp,
1367                         int use_mvcost,
1368                         const MV *center_mv,
1369                         MV *best_mv) {
1370   return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1371                         sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1372                         center_mv, best_mv);
1373 }
1374
1375 int vp9_fast_dia_search(const MACROBLOCK *x,
1376                         MV *ref_mv,
1377                         int search_param,
1378                         int sad_per_bit,
1379                         int do_init_search,
1380                         int *cost_list,
1381                         const vp9_variance_fn_ptr_t *vfp,
1382                         int use_mvcost,
1383                         const MV *center_mv,
1384                         MV *best_mv) {
1385   return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1386                            sad_per_bit, do_init_search, cost_list, vfp,
1387                            use_mvcost, center_mv, best_mv);
1388 }
1389
1390 #undef CHECK_BETTER
1391
1392 int vp9_full_range_search_c(const MACROBLOCK *x,
1393                             const search_site_config *cfg,
1394                             MV *ref_mv, MV *best_mv,
1395                             int search_param, int sad_per_bit, int *num00,
1396                             const vp9_variance_fn_ptr_t *fn_ptr,
1397                             const MV *center_mv) {
1398   const MACROBLOCKD *const xd = &x->e_mbd;
1399   const struct buf_2d *const what = &x->plane[0].src;
1400   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1401   const int range = 64;
1402   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1403   unsigned int best_sad = INT_MAX;
1404   int r, c, i;
1405   int start_col, end_col, start_row, end_row;
1406
1407   // The cfg and search_param parameters are not used in this search variant
1408   (void)cfg;
1409   (void)search_param;
1410
1411   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1412   *best_mv = *ref_mv;
1413   *num00 = 11;
1414   best_sad = fn_ptr->sdf(what->buf, what->stride,
1415                          get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1416                  mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1417   start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1418   start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1419   end_row = MIN(range, x->mv_row_max - ref_mv->row);
1420   end_col = MIN(range, x->mv_col_max - ref_mv->col);
1421
1422   for (r = start_row; r <= end_row; ++r) {
1423     for (c = start_col; c <= end_col; c += 4) {
1424       if (c + 3 <= end_col) {
1425         unsigned int sads[4];
1426         const uint8_t *addrs[4];
1427         for (i = 0; i < 4; ++i) {
1428           const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1429           addrs[i] = get_buf_from_mv(in_what, &mv);
1430         }
1431
1432         fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1433
1434         for (i = 0; i < 4; ++i) {
1435           if (sads[i] < best_sad) {
1436             const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1437             const unsigned int sad = sads[i] +
1438                 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1439             if (sad < best_sad) {
1440               best_sad = sad;
1441               *best_mv = mv;
1442             }
1443           }
1444         }
1445       } else {
1446         for (i = 0; i < end_col - c; ++i) {
1447           const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1448           unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1449               get_buf_from_mv(in_what, &mv), in_what->stride);
1450           if (sad < best_sad) {
1451             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1452             if (sad < best_sad) {
1453               best_sad = sad;
1454               *best_mv = mv;
1455             }
1456           }
1457         }
1458       }
1459     }
1460   }
1461
1462   return best_sad;
1463 }
1464
1465 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1466                              const search_site_config *cfg,
1467                              MV *ref_mv, MV *best_mv, int search_param,
1468                              int sad_per_bit, int *num00,
1469                              const vp9_variance_fn_ptr_t *fn_ptr,
1470                              const MV *center_mv) {
1471   int i, j, step;
1472
1473   const MACROBLOCKD *const xd = &x->e_mbd;
1474   uint8_t *what = x->plane[0].src.buf;
1475   const int what_stride = x->plane[0].src.stride;
1476   const uint8_t *in_what;
1477   const int in_what_stride = xd->plane[0].pre[0].stride;
1478   const uint8_t *best_address;
1479
1480   unsigned int bestsad = INT_MAX;
1481   int best_site = 0;
1482   int last_site = 0;
1483
1484   int ref_row;
1485   int ref_col;
1486
1487   // search_param determines the length of the initial step and hence the number
1488   // of iterations.
1489   // 0 = initial step (MAX_FIRST_STEP) pel
1490   // 1 = (MAX_FIRST_STEP/2) pel,
1491   // 2 = (MAX_FIRST_STEP/4) pel...
1492   const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1493   const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1494
1495   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1496   clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1497   ref_row = ref_mv->row;
1498   ref_col = ref_mv->col;
1499   *num00 = 0;
1500   best_mv->row = ref_row;
1501   best_mv->col = ref_col;
1502
1503   // Work out the start point for the search
1504   in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1505   best_address = in_what;
1506
1507   // Check the starting position
1508   bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1509                 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1510
1511   i = 1;
1512
1513   for (step = 0; step < tot_steps; step++) {
1514     int all_in = 1, t;
1515
1516     // All_in is true if every one of the points we are checking are within
1517     // the bounds of the image.
1518     all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1519     all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1520     all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1521     all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1522
1523     // If all the pixels are within the bounds we don't check whether the
1524     // search point is valid in this loop,  otherwise we check each point
1525     // for validity..
1526     if (all_in) {
1527       unsigned int sad_array[4];
1528
1529       for (j = 0; j < cfg->searches_per_step; j += 4) {
1530         unsigned char const *block_offset[4];
1531
1532         for (t = 0; t < 4; t++)
1533           block_offset[t] = ss[i + t].offset + best_address;
1534
1535         fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1536                        sad_array);
1537
1538         for (t = 0; t < 4; t++, i++) {
1539           if (sad_array[t] < bestsad) {
1540             const MV this_mv = {best_mv->row + ss[i].mv.row,
1541                                 best_mv->col + ss[i].mv.col};
1542             sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1543                                            sad_per_bit);
1544             if (sad_array[t] < bestsad) {
1545               bestsad = sad_array[t];
1546               best_site = i;
1547             }
1548           }
1549         }
1550       }
1551     } else {
1552       for (j = 0; j < cfg->searches_per_step; j++) {
1553         // Trap illegal vectors
1554         const MV this_mv = {best_mv->row + ss[i].mv.row,
1555                             best_mv->col + ss[i].mv.col};
1556
1557         if (is_mv_in(x, &this_mv)) {
1558           const uint8_t *const check_here = ss[i].offset + best_address;
1559           unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1560                                              in_what_stride);
1561
1562           if (thissad < bestsad) {
1563             thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1564             if (thissad < bestsad) {
1565               bestsad = thissad;
1566               best_site = i;
1567             }
1568           }
1569         }
1570         i++;
1571       }
1572     }
1573     if (best_site != last_site) {
1574       best_mv->row += ss[best_site].mv.row;
1575       best_mv->col += ss[best_site].mv.col;
1576       best_address += ss[best_site].offset;
1577       last_site = best_site;
1578 #if defined(NEW_DIAMOND_SEARCH)
1579       while (1) {
1580         const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1581                             best_mv->col + ss[best_site].mv.col};
1582         if (is_mv_in(x, &this_mv)) {
1583           const uint8_t *const check_here = ss[best_site].offset + best_address;
1584           unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1585                                              in_what_stride);
1586           if (thissad < bestsad) {
1587             thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1588             if (thissad < bestsad) {
1589               bestsad = thissad;
1590               best_mv->row += ss[best_site].mv.row;
1591               best_mv->col += ss[best_site].mv.col;
1592               best_address += ss[best_site].offset;
1593               continue;
1594             }
1595           }
1596         }
1597         break;
1598       };
1599 #endif
1600     } else if (best_address == in_what) {
1601       (*num00)++;
1602     }
1603   }
1604   return bestsad;
1605 }
1606
1607 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1608               point as the best match, we will do a final 1-away diamond
1609               refining search  */
1610 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1611                            MV *mvp_full, int step_param,
1612                            int sadpb, int further_steps, int do_refine,
1613                            int *cost_list,
1614                            const vp9_variance_fn_ptr_t *fn_ptr,
1615                            const MV *ref_mv, MV *dst_mv) {
1616   MV temp_mv;
1617   int thissme, n, num00 = 0;
1618   int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1619                                         step_param, sadpb, &n,
1620                                         fn_ptr, ref_mv);
1621   if (bestsme < INT_MAX)
1622     bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1623   *dst_mv = temp_mv;
1624
1625   // If there won't be more n-step search, check to see if refining search is
1626   // needed.
1627   if (n > further_steps)
1628     do_refine = 0;
1629
1630   while (n < further_steps) {
1631     ++n;
1632
1633     if (num00) {
1634       num00--;
1635     } else {
1636       thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1637                                         step_param + n, sadpb, &num00,
1638                                         fn_ptr, ref_mv);
1639       if (thissme < INT_MAX)
1640         thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1641
1642       // check to see if refining search is needed.
1643       if (num00 > further_steps - n)
1644         do_refine = 0;
1645
1646       if (thissme < bestsme) {
1647         bestsme = thissme;
1648         *dst_mv = temp_mv;
1649       }
1650     }
1651   }
1652
1653   // final 1-away diamond refining search
1654   if (do_refine) {
1655     const int search_range = 8;
1656     MV best_mv = *dst_mv;
1657     thissme = cpi->refining_search_sad(x, &best_mv, sadpb, search_range,
1658                                        fn_ptr, ref_mv);
1659     if (thissme < INT_MAX)
1660       thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1661     if (thissme < bestsme) {
1662       bestsme = thissme;
1663       *dst_mv = best_mv;
1664     }
1665   }
1666
1667   // Return cost list.
1668   if (cost_list) {
1669     calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1670   }
1671   return bestsme;
1672 }
1673
1674 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1675                           int sad_per_bit, int distance,
1676                           const vp9_variance_fn_ptr_t *fn_ptr,
1677                           const MV *center_mv, MV *best_mv) {
1678   int r, c;
1679   const MACROBLOCKD *const xd = &x->e_mbd;
1680   const struct buf_2d *const what = &x->plane[0].src;
1681   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1682   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1683   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1684   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1685   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1686   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1687   int best_sad = fn_ptr->sdf(what->buf, what->stride,
1688       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1689       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1690   *best_mv = *ref_mv;
1691
1692   for (r = row_min; r < row_max; ++r) {
1693     for (c = col_min; c < col_max; ++c) {
1694       const MV mv = {r, c};
1695       const int sad = fn_ptr->sdf(what->buf, what->stride,
1696           get_buf_from_mv(in_what, &mv), in_what->stride) +
1697               mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1698       if (sad < best_sad) {
1699         best_sad = sad;
1700         *best_mv = mv;
1701       }
1702     }
1703   }
1704   return best_sad;
1705 }
1706
1707 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
1708                           int sad_per_bit, int distance,
1709                           const vp9_variance_fn_ptr_t *fn_ptr,
1710                           const MV *center_mv, MV *best_mv) {
1711   int r;
1712   const MACROBLOCKD *const xd = &x->e_mbd;
1713   const struct buf_2d *const what = &x->plane[0].src;
1714   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1715   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1716   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1717   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1718   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1719   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1720   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
1721       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1722       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1723   *best_mv = *ref_mv;
1724
1725   for (r = row_min; r < row_max; ++r) {
1726     int c = col_min;
1727     const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
1728
1729     if (fn_ptr->sdx3f != NULL) {
1730       while ((c + 2) < col_max) {
1731         int i;
1732         unsigned int sads[3];
1733
1734         fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
1735                       sads);
1736
1737         for (i = 0; i < 3; ++i) {
1738           unsigned int sad = sads[i];
1739           if (sad < best_sad) {
1740             const MV mv = {r, c};
1741             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1742             if (sad < best_sad) {
1743               best_sad = sad;
1744               *best_mv = mv;
1745             }
1746           }
1747           ++check_here;
1748           ++c;
1749         }
1750       }
1751     }
1752
1753     while (c < col_max) {
1754       unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1755                                      check_here, in_what->stride);
1756       if (sad < best_sad) {
1757         const MV mv = {r, c};
1758         sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1759         if (sad < best_sad) {
1760           best_sad = sad;
1761           *best_mv = mv;
1762         }
1763       }
1764       ++check_here;
1765       ++c;
1766     }
1767   }
1768
1769   return best_sad;
1770 }
1771
1772 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
1773                           int sad_per_bit, int distance,
1774                           const vp9_variance_fn_ptr_t *fn_ptr,
1775                           const MV *center_mv, MV *best_mv) {
1776   int r;
1777   const MACROBLOCKD *const xd = &x->e_mbd;
1778   const struct buf_2d *const what = &x->plane[0].src;
1779   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1780   const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1781   const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1782   const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1783   const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1784   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1785   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
1786       get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1787       mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1788   *best_mv = *ref_mv;
1789
1790   for (r = row_min; r < row_max; ++r) {
1791     int c = col_min;
1792     const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
1793
1794     if (fn_ptr->sdx8f != NULL) {
1795       while ((c + 7) < col_max) {
1796         int i;
1797         unsigned int sads[8];
1798
1799         fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
1800                       sads);
1801
1802         for (i = 0; i < 8; ++i) {
1803           unsigned int sad = sads[i];
1804           if (sad < best_sad) {
1805             const MV mv = {r, c};
1806             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1807             if (sad < best_sad) {
1808               best_sad = sad;
1809               *best_mv = mv;
1810             }
1811           }
1812           ++check_here;
1813           ++c;
1814         }
1815       }
1816     }
1817
1818     if (fn_ptr->sdx3f != NULL) {
1819       while ((c + 2) < col_max) {
1820         int i;
1821         unsigned int sads[3];
1822
1823         fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
1824                       sads);
1825
1826         for (i = 0; i < 3; ++i) {
1827           unsigned int sad = sads[i];
1828           if (sad < best_sad) {
1829             const MV mv = {r, c};
1830             sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1831             if (sad < best_sad) {
1832               best_sad = sad;
1833               *best_mv = mv;
1834             }
1835           }
1836           ++check_here;
1837           ++c;
1838         }
1839       }
1840     }
1841
1842     while (c < col_max) {
1843       unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1844                                      check_here, in_what->stride);
1845       if (sad < best_sad) {
1846         const MV mv = {r, c};
1847         sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1848         if (sad < best_sad) {
1849           best_sad = sad;
1850           *best_mv = mv;
1851         }
1852       }
1853       ++check_here;
1854       ++c;
1855     }
1856   }
1857
1858   return best_sad;
1859 }
1860
1861 int vp9_refining_search_sad_c(const MACROBLOCK *x,
1862                               MV *ref_mv, int error_per_bit,
1863                               int search_range,
1864                               const vp9_variance_fn_ptr_t *fn_ptr,
1865                               const MV *center_mv) {
1866   const MACROBLOCKD *const xd = &x->e_mbd;
1867   const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
1868   const struct buf_2d *const what = &x->plane[0].src;
1869   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1870   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1871   const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
1872   unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
1873                                     in_what->stride) +
1874       mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
1875   int i, j;
1876
1877   for (i = 0; i < search_range; i++) {
1878     int best_site = -1;
1879     const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
1880                        ((ref_mv->row + 1) < x->mv_row_max) &
1881                        ((ref_mv->col - 1) > x->mv_col_min) &
1882                        ((ref_mv->col + 1) < x->mv_col_max);
1883
1884     if (all_in) {
1885       unsigned int sads[4];
1886       const uint8_t *const positions[4] = {
1887         best_address - in_what->stride,
1888         best_address - 1,
1889         best_address + 1,
1890         best_address + in_what->stride
1891       };
1892
1893       fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
1894
1895       for (j = 0; j < 4; ++j) {
1896         if (sads[j] < best_sad) {
1897           const MV mv = {ref_mv->row + neighbors[j].row,
1898                          ref_mv->col + neighbors[j].col};
1899           sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1900           if (sads[j] < best_sad) {
1901             best_sad = sads[j];
1902             best_site = j;
1903           }
1904         }
1905       }
1906     } else {
1907       for (j = 0; j < 4; ++j) {
1908         const MV mv = {ref_mv->row + neighbors[j].row,
1909                        ref_mv->col + neighbors[j].col};
1910
1911         if (is_mv_in(x, &mv)) {
1912           unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1913                                          get_buf_from_mv(in_what, &mv),
1914                                          in_what->stride);
1915           if (sad < best_sad) {
1916             sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1917             if (sad < best_sad) {
1918               best_sad = sad;
1919               best_site = j;
1920             }
1921           }
1922         }
1923       }
1924     }
1925
1926     if (best_site == -1) {
1927       break;
1928     } else {
1929       ref_mv->row += neighbors[best_site].row;
1930       ref_mv->col += neighbors[best_site].col;
1931       best_address = get_buf_from_mv(in_what, ref_mv);
1932     }
1933   }
1934
1935   return best_sad;
1936 }
1937
1938 // This function is called when we do joint motion search in comp_inter_inter
1939 // mode.
1940 int vp9_refining_search_8p_c(const MACROBLOCK *x,
1941                              MV *ref_mv, int error_per_bit,
1942                              int search_range,
1943                              const vp9_variance_fn_ptr_t *fn_ptr,
1944                              const MV *center_mv,
1945                              const uint8_t *second_pred) {
1946   const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
1947                            {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1948   const MACROBLOCKD *const xd = &x->e_mbd;
1949   const struct buf_2d *const what = &x->plane[0].src;
1950   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1951   const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1952   unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
1953       get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
1954       mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
1955   int i, j;
1956
1957   for (i = 0; i < search_range; ++i) {
1958     int best_site = -1;
1959
1960     for (j = 0; j < 8; ++j) {
1961       const MV mv = {ref_mv->row + neighbors[j].row,
1962                      ref_mv->col + neighbors[j].col};
1963
1964       if (is_mv_in(x, &mv)) {
1965         unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
1966             get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
1967         if (sad < best_sad) {
1968           sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1969           if (sad < best_sad) {
1970             best_sad = sad;
1971             best_site = j;
1972           }
1973         }
1974       }
1975     }
1976
1977     if (best_site == -1) {
1978       break;
1979     } else {
1980       ref_mv->row += neighbors[best_site].row;
1981       ref_mv->col += neighbors[best_site].col;
1982     }
1983   }
1984   return best_sad;
1985 }
1986
1987 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
1988                           BLOCK_SIZE bsize, MV *mvp_full,
1989                           int step_param, int error_per_bit,
1990                           int *cost_list,
1991                           const MV *ref_mv, MV *tmp_mv,
1992                           int var_max, int rd) {
1993   const SPEED_FEATURES *const sf = &cpi->sf;
1994   const SEARCH_METHODS method = sf->mv.search_method;
1995   vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
1996   int var = 0;
1997   if (cost_list) {
1998     cost_list[0] = INT_MAX;
1999     cost_list[1] = INT_MAX;
2000     cost_list[2] = INT_MAX;
2001     cost_list[3] = INT_MAX;
2002     cost_list[4] = INT_MAX;
2003   }
2004
2005   switch (method) {
2006     case FAST_DIAMOND:
2007       var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2008                                 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2009       break;
2010     case FAST_HEX:
2011       var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2012                                 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2013       break;
2014     case HEX:
2015       var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2016                            cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2017       break;
2018     case SQUARE:
2019       var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2020                               cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2021       break;
2022     case BIGDIA:
2023       var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2024                               cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2025       break;
2026     case NSTEP:
2027       var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2028                                    MAX_MVSEARCH_STEPS - 1 - step_param,
2029                                    1, cost_list, fn_ptr, ref_mv, tmp_mv);
2030       break;
2031     default:
2032       assert(!"Invalid search method.");
2033   }
2034
2035   if (method != NSTEP && rd && var < var_max)
2036     var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);
2037
2038   return var;
2039 }