vp8: remove VP8_ENTROPY_STATS code
[platform/upstream/libvpx.git] / vp8 / encoder / 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 "./vp8_rtcd.h"
12 #include "./vpx_dsp_rtcd.h"
13 #include "onyx_int.h"
14 #include "mcomp.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_config.h"
17 #include <stdio.h>
18 #include <limits.h>
19 #include <math.h>
20 #include "vp8/common/findnearmv.h"
21 #include "vp8/common/common.h"
22 #include "vpx_dsp/vpx_dsp_common.h"
23
24 int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight) {
25   /* MV costing is based on the distribution of vectors in the previous
26    * frame and as such will tend to over state the cost of vectors. In
27    * addition coding a new vector can have a knock on effect on the cost
28    * of subsequent vectors and the quality of prediction from NEAR and
29    * NEAREST for subsequent blocks. The "Weight" parameter allows, to a
30    * limited extent, for some account to be taken of these factors.
31    */
32   const int mv_idx_row =
33       clamp((mv->as_mv.row - ref->as_mv.row) >> 1, 0, MVvals);
34   const int mv_idx_col =
35       clamp((mv->as_mv.col - ref->as_mv.col) >> 1, 0, MVvals);
36   return ((mvcost[0][mv_idx_row] + mvcost[1][mv_idx_col]) * Weight) >> 7;
37 }
38
39 static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2],
40                        int error_per_bit) {
41   /* Ignore mv costing if mvcost is NULL */
42   if (mvcost) {
43     const int mv_idx_row =
44         clamp((mv->as_mv.row - ref->as_mv.row) >> 1, 0, MVvals);
45     const int mv_idx_col =
46         clamp((mv->as_mv.col - ref->as_mv.col) >> 1, 0, MVvals);
47     return ((mvcost[0][mv_idx_row] + mvcost[1][mv_idx_col]) * error_per_bit +
48             128) >>
49            8;
50   }
51   return 0;
52 }
53
54 static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2],
55                           int error_per_bit) {
56   /* Calculate sad error cost on full pixel basis. */
57   /* Ignore mv costing if mvsadcost is NULL */
58   if (mvsadcost) {
59     return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
60              mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)]) *
61                 error_per_bit +
62             128) >>
63            8;
64   }
65   return 0;
66 }
67
68 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride) {
69   int Len;
70   int search_site_count = 0;
71
72   /* Generate offsets for 4 search sites per step. */
73   Len = MAX_FIRST_STEP;
74   x->ss[search_site_count].mv.col = 0;
75   x->ss[search_site_count].mv.row = 0;
76   x->ss[search_site_count].offset = 0;
77   search_site_count++;
78
79   while (Len > 0) {
80     /* Compute offsets for search sites. */
81     x->ss[search_site_count].mv.col = 0;
82     x->ss[search_site_count].mv.row = -Len;
83     x->ss[search_site_count].offset = -Len * stride;
84     search_site_count++;
85
86     /* Compute offsets for search sites. */
87     x->ss[search_site_count].mv.col = 0;
88     x->ss[search_site_count].mv.row = Len;
89     x->ss[search_site_count].offset = Len * stride;
90     search_site_count++;
91
92     /* Compute offsets for search sites. */
93     x->ss[search_site_count].mv.col = -Len;
94     x->ss[search_site_count].mv.row = 0;
95     x->ss[search_site_count].offset = -Len;
96     search_site_count++;
97
98     /* Compute offsets for search sites. */
99     x->ss[search_site_count].mv.col = Len;
100     x->ss[search_site_count].mv.row = 0;
101     x->ss[search_site_count].offset = Len;
102     search_site_count++;
103
104     /* Contract. */
105     Len /= 2;
106   }
107
108   x->ss_count = search_site_count;
109   x->searches_per_step = 4;
110 }
111
112 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride) {
113   int Len;
114   int search_site_count = 0;
115
116   /* Generate offsets for 8 search sites per step. */
117   Len = MAX_FIRST_STEP;
118   x->ss[search_site_count].mv.col = 0;
119   x->ss[search_site_count].mv.row = 0;
120   x->ss[search_site_count].offset = 0;
121   search_site_count++;
122
123   while (Len > 0) {
124     /* Compute offsets for search sites. */
125     x->ss[search_site_count].mv.col = 0;
126     x->ss[search_site_count].mv.row = -Len;
127     x->ss[search_site_count].offset = -Len * stride;
128     search_site_count++;
129
130     /* Compute offsets for search sites. */
131     x->ss[search_site_count].mv.col = 0;
132     x->ss[search_site_count].mv.row = Len;
133     x->ss[search_site_count].offset = Len * stride;
134     search_site_count++;
135
136     /* Compute offsets for search sites. */
137     x->ss[search_site_count].mv.col = -Len;
138     x->ss[search_site_count].mv.row = 0;
139     x->ss[search_site_count].offset = -Len;
140     search_site_count++;
141
142     /* Compute offsets for search sites. */
143     x->ss[search_site_count].mv.col = Len;
144     x->ss[search_site_count].mv.row = 0;
145     x->ss[search_site_count].offset = Len;
146     search_site_count++;
147
148     /* Compute offsets for search sites. */
149     x->ss[search_site_count].mv.col = -Len;
150     x->ss[search_site_count].mv.row = -Len;
151     x->ss[search_site_count].offset = -Len * stride - Len;
152     search_site_count++;
153
154     /* Compute offsets for search sites. */
155     x->ss[search_site_count].mv.col = Len;
156     x->ss[search_site_count].mv.row = -Len;
157     x->ss[search_site_count].offset = -Len * stride + Len;
158     search_site_count++;
159
160     /* Compute offsets for search sites. */
161     x->ss[search_site_count].mv.col = -Len;
162     x->ss[search_site_count].mv.row = Len;
163     x->ss[search_site_count].offset = Len * stride - Len;
164     search_site_count++;
165
166     /* Compute offsets for search sites. */
167     x->ss[search_site_count].mv.col = Len;
168     x->ss[search_site_count].mv.row = Len;
169     x->ss[search_site_count].offset = Len * stride + Len;
170     search_site_count++;
171
172     /* Contract. */
173     Len /= 2;
174   }
175
176   x->ss_count = search_site_count;
177   x->searches_per_step = 8;
178 }
179
180 /*
181  * To avoid the penalty for crossing cache-line read, preload the reference
182  * area in a small buffer, which is aligned to make sure there won't be crossing
183  * cache-line read while reading from this buffer. This reduced the cpu
184  * cycles spent on reading ref data in sub-pixel filter functions.
185  * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
186  * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
187  * could reduce the area.
188  */
189
190 /* estimated cost of a motion vector (r,c) */
191 #define MVC(r, c)                                                             \
192   (mvcost                                                                     \
193        ? ((mvcost[0][(r)-rr] + mvcost[1][(c)-rc]) * error_per_bit + 128) >> 8 \
194        : 0)
195 /* pointer to predictor base of a motionvector */
196 #define PRE(r, c) (y + (((r) >> 2) * y_stride + ((c) >> 2) - (offset)))
197 /* convert motion vector component to offset for svf calc */
198 #define SP(x) (((x)&3) << 1)
199 /* returns subpixel variance error function. */
200 #define DIST(r, c) \
201   vfp->svf(PRE(r, c), y_stride, SP(c), SP(r), z, b->src_stride, &sse)
202 #define IFMVCV(r, c, s, e) \
203   if (c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
204 /* returns distortion + motion vector cost */
205 #define ERR(r, c) (MVC(r, c) + DIST(r, c))
206 /* checks if (r,c) has better score than previous best */
207 #define CHECK_BETTER(v, r, c)                           \
208   IFMVCV(r, c,                                          \
209          {                                              \
210            thismse = DIST(r, c);                        \
211            if ((v = (MVC(r, c) + thismse)) < besterr) { \
212              besterr = v;                               \
213              br = r;                                    \
214              bc = c;                                    \
215              *distortion = thismse;                     \
216              *sse1 = sse;                               \
217            }                                            \
218          },                                             \
219          v = UINT_MAX;)
220
221 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
222                                              int_mv *bestmv, int_mv *ref_mv,
223                                              int error_per_bit,
224                                              const vp8_variance_fn_ptr_t *vfp,
225                                              int *mvcost[2], int *distortion,
226                                              unsigned int *sse1) {
227   unsigned char *z = (*(b->base_src) + b->src);
228
229   int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1;
230   int br = bestmv->as_mv.row * 4, bc = bestmv->as_mv.col * 4;
231   int tr = br, tc = bc;
232   unsigned int besterr;
233   unsigned int left, right, up, down, diag;
234   unsigned int sse;
235   unsigned int whichdir;
236   unsigned int halfiters = 4;
237   unsigned int quarteriters = 4;
238   int thismse;
239
240   int minc = VPXMAX(x->mv_col_min * 4,
241                     (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
242   int maxc = VPXMIN(x->mv_col_max * 4,
243                     (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
244   int minr = VPXMAX(x->mv_row_min * 4,
245                     (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
246   int maxr = VPXMIN(x->mv_row_max * 4,
247                     (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
248
249   int y_stride;
250   int offset;
251   int pre_stride = x->e_mbd.pre.y_stride;
252   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
253
254 #if ARCH_X86 || ARCH_X86_64
255   MACROBLOCKD *xd = &x->e_mbd;
256   unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
257                        bestmv->as_mv.col;
258   unsigned char *y;
259   int buf_r1, buf_r2, buf_c1;
260
261   /* Clamping to avoid out-of-range data access */
262   buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)
263                ? (bestmv->as_mv.row - x->mv_row_min)
264                : 3;
265   buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)
266                ? (x->mv_row_max - bestmv->as_mv.row)
267                : 3;
268   buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)
269                ? (bestmv->as_mv.col - x->mv_col_min)
270                : 3;
271   y_stride = 32;
272
273   /* Copy to intermediate buffer before searching. */
274   vfp->copymem(y_0 - buf_c1 - pre_stride * buf_r1, pre_stride, xd->y_buf,
275                y_stride, 16 + buf_r1 + buf_r2);
276   y = xd->y_buf + y_stride * buf_r1 + buf_c1;
277 #else
278   unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
279                      bestmv->as_mv.col;
280   y_stride = pre_stride;
281 #endif
282
283   offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;
284
285   /* central mv */
286   bestmv->as_mv.row *= 8;
287   bestmv->as_mv.col *= 8;
288
289   /* calculate central point error */
290   besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
291   *distortion = besterr;
292   besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
293
294   /* TODO: Each subsequent iteration checks at least one point in common
295    * with the last iteration could be 2 ( if diag selected)
296    */
297   while (--halfiters) {
298     /* 1/2 pel */
299     CHECK_BETTER(left, tr, tc - 2);
300     CHECK_BETTER(right, tr, tc + 2);
301     CHECK_BETTER(up, tr - 2, tc);
302     CHECK_BETTER(down, tr + 2, tc);
303
304     whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
305
306     switch (whichdir) {
307       case 0: CHECK_BETTER(diag, tr - 2, tc - 2); break;
308       case 1: CHECK_BETTER(diag, tr - 2, tc + 2); break;
309       case 2: CHECK_BETTER(diag, tr + 2, tc - 2); break;
310       case 3: CHECK_BETTER(diag, tr + 2, tc + 2); break;
311     }
312
313     /* no reason to check the same one again. */
314     if (tr == br && tc == bc) break;
315
316     tr = br;
317     tc = bc;
318   }
319
320   /* TODO: Each subsequent iteration checks at least one point in common
321    * with the last iteration could be 2 ( if diag selected)
322    */
323
324   /* 1/4 pel */
325   while (--quarteriters) {
326     CHECK_BETTER(left, tr, tc - 1);
327     CHECK_BETTER(right, tr, tc + 1);
328     CHECK_BETTER(up, tr - 1, tc);
329     CHECK_BETTER(down, tr + 1, tc);
330
331     whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
332
333     switch (whichdir) {
334       case 0: CHECK_BETTER(diag, tr - 1, tc - 1); break;
335       case 1: CHECK_BETTER(diag, tr - 1, tc + 1); break;
336       case 2: CHECK_BETTER(diag, tr + 1, tc - 1); break;
337       case 3: CHECK_BETTER(diag, tr + 1, tc + 1); break;
338     }
339
340     /* no reason to check the same one again. */
341     if (tr == br && tc == bc) break;
342
343     tr = br;
344     tc = bc;
345   }
346
347   bestmv->as_mv.row = br * 2;
348   bestmv->as_mv.col = bc * 2;
349
350   if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL << 3)) ||
351       (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL << 3))) {
352     return INT_MAX;
353   }
354
355   return besterr;
356 }
357 #undef MVC
358 #undef PRE
359 #undef SP
360 #undef DIST
361 #undef IFMVCV
362 #undef ERR
363 #undef CHECK_BETTER
364
365 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
366                                  int_mv *bestmv, int_mv *ref_mv,
367                                  int error_per_bit,
368                                  const vp8_variance_fn_ptr_t *vfp,
369                                  int *mvcost[2], int *distortion,
370                                  unsigned int *sse1) {
371   int bestmse = INT_MAX;
372   int_mv startmv;
373   int_mv this_mv;
374   unsigned char *z = (*(b->base_src) + b->src);
375   int left, right, up, down, diag;
376   unsigned int sse;
377   int whichdir;
378   int thismse;
379   int y_stride;
380   int pre_stride = x->e_mbd.pre.y_stride;
381   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
382
383 #if ARCH_X86 || ARCH_X86_64
384   MACROBLOCKD *xd = &x->e_mbd;
385   unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
386                        bestmv->as_mv.col;
387   unsigned char *y;
388
389   y_stride = 32;
390   /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
391   vfp->copymem(y_0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
392   y = xd->y_buf + y_stride + 1;
393 #else
394   unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
395                      bestmv->as_mv.col;
396   y_stride = pre_stride;
397 #endif
398
399   /* central mv */
400   bestmv->as_mv.row *= 8;
401   bestmv->as_mv.col *= 8;
402   startmv = *bestmv;
403
404   /* calculate central point error */
405   bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
406   *distortion = bestmse;
407   bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
408
409   /* go left then right and check error */
410   this_mv.as_mv.row = startmv.as_mv.row;
411   this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
412   /* "halfpix" horizontal variance */
413   thismse = vfp->svf(y - 1, y_stride, 4, 0, z, b->src_stride, &sse);
414   left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
415
416   if (left < bestmse) {
417     *bestmv = this_mv;
418     bestmse = left;
419     *distortion = thismse;
420     *sse1 = sse;
421   }
422
423   this_mv.as_mv.col += 8;
424   /* "halfpix" horizontal variance */
425   thismse = vfp->svf(y, y_stride, 4, 0, z, b->src_stride, &sse);
426   right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
427
428   if (right < bestmse) {
429     *bestmv = this_mv;
430     bestmse = right;
431     *distortion = thismse;
432     *sse1 = sse;
433   }
434
435   /* go up then down and check error */
436   this_mv.as_mv.col = startmv.as_mv.col;
437   this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
438   /* "halfpix" vertical variance */
439   thismse = vfp->svf(y - y_stride, y_stride, 0, 4, z, b->src_stride, &sse);
440   up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
441
442   if (up < bestmse) {
443     *bestmv = this_mv;
444     bestmse = up;
445     *distortion = thismse;
446     *sse1 = sse;
447   }
448
449   this_mv.as_mv.row += 8;
450   /* "halfpix" vertical variance */
451   thismse = vfp->svf(y, y_stride, 0, 4, z, b->src_stride, &sse);
452   down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
453
454   if (down < bestmse) {
455     *bestmv = this_mv;
456     bestmse = down;
457     *distortion = thismse;
458     *sse1 = sse;
459   }
460
461   /* now check 1 more diagonal */
462   whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
463   this_mv = startmv;
464
465   switch (whichdir) {
466     case 0:
467       this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
468       this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
469       /* "halfpix" horizontal/vertical variance */
470       thismse =
471           vfp->svf(y - 1 - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
472       break;
473     case 1:
474       this_mv.as_mv.col += 4;
475       this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
476       /* "halfpix" horizontal/vertical variance */
477       thismse = vfp->svf(y - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
478       break;
479     case 2:
480       this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
481       this_mv.as_mv.row += 4;
482       /* "halfpix" horizontal/vertical variance */
483       thismse = vfp->svf(y - 1, y_stride, 4, 4, z, b->src_stride, &sse);
484       break;
485     case 3:
486     default:
487       this_mv.as_mv.col += 4;
488       this_mv.as_mv.row += 4;
489       /* "halfpix" horizontal/vertical variance */
490       thismse = vfp->svf(y, y_stride, 4, 4, z, b->src_stride, &sse);
491       break;
492   }
493
494   diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
495
496   if (diag < bestmse) {
497     *bestmv = this_mv;
498     bestmse = diag;
499     *distortion = thismse;
500     *sse1 = sse;
501   }
502
503   /* time to check quarter pels. */
504   if (bestmv->as_mv.row < startmv.as_mv.row) y -= y_stride;
505
506   if (bestmv->as_mv.col < startmv.as_mv.col) y--;
507
508   startmv = *bestmv;
509
510   /* go left then right and check error */
511   this_mv.as_mv.row = startmv.as_mv.row;
512
513   if (startmv.as_mv.col & 7) {
514     this_mv.as_mv.col = startmv.as_mv.col - 2;
515     thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
516                        this_mv.as_mv.row & 7, z, b->src_stride, &sse);
517   } else {
518     this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
519     thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z,
520                        b->src_stride, &sse);
521   }
522
523   left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
524
525   if (left < bestmse) {
526     *bestmv = this_mv;
527     bestmse = left;
528     *distortion = thismse;
529     *sse1 = sse;
530   }
531
532   this_mv.as_mv.col += 4;
533   thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7,
534                      z, b->src_stride, &sse);
535   right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
536
537   if (right < bestmse) {
538     *bestmv = this_mv;
539     bestmse = right;
540     *distortion = thismse;
541     *sse1 = sse;
542   }
543
544   /* go up then down and check error */
545   this_mv.as_mv.col = startmv.as_mv.col;
546
547   if (startmv.as_mv.row & 7) {
548     this_mv.as_mv.row = startmv.as_mv.row - 2;
549     thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
550                        this_mv.as_mv.row & 7, z, b->src_stride, &sse);
551   } else {
552     this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
553     thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z,
554                        b->src_stride, &sse);
555   }
556
557   up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
558
559   if (up < bestmse) {
560     *bestmv = this_mv;
561     bestmse = up;
562     *distortion = thismse;
563     *sse1 = sse;
564   }
565
566   this_mv.as_mv.row += 4;
567   thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7,
568                      z, b->src_stride, &sse);
569   down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
570
571   if (down < bestmse) {
572     *bestmv = this_mv;
573     bestmse = down;
574     *distortion = thismse;
575     *sse1 = sse;
576   }
577
578   /* now check 1 more diagonal */
579   whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
580
581   this_mv = startmv;
582
583   switch (whichdir) {
584     case 0:
585
586       if (startmv.as_mv.row & 7) {
587         this_mv.as_mv.row -= 2;
588
589         if (startmv.as_mv.col & 7) {
590           this_mv.as_mv.col -= 2;
591           thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
592                              this_mv.as_mv.row & 7, z, b->src_stride, &sse);
593         } else {
594           this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
595           thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z,
596                              b->src_stride, &sse);
597         }
598       } else {
599         this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
600
601         if (startmv.as_mv.col & 7) {
602           this_mv.as_mv.col -= 2;
603           thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6,
604                              z, b->src_stride, &sse);
605         } else {
606           this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
607           thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride,
608                              &sse);
609         }
610       }
611
612       break;
613     case 1:
614       this_mv.as_mv.col += 2;
615
616       if (startmv.as_mv.row & 7) {
617         this_mv.as_mv.row -= 2;
618         thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
619                            this_mv.as_mv.row & 7, z, b->src_stride, &sse);
620       } else {
621         this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
622         thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z,
623                            b->src_stride, &sse);
624       }
625
626       break;
627     case 2:
628       this_mv.as_mv.row += 2;
629
630       if (startmv.as_mv.col & 7) {
631         this_mv.as_mv.col -= 2;
632         thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
633                            this_mv.as_mv.row & 7, z, b->src_stride, &sse);
634       } else {
635         this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
636         thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z,
637                            b->src_stride, &sse);
638       }
639
640       break;
641     case 3:
642       this_mv.as_mv.col += 2;
643       this_mv.as_mv.row += 2;
644       thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7,
645                          this_mv.as_mv.row & 7, z, b->src_stride, &sse);
646       break;
647   }
648
649   diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
650
651   if (diag < bestmse) {
652     *bestmv = this_mv;
653     bestmse = diag;
654     *distortion = thismse;
655     *sse1 = sse;
656   }
657
658   return bestmse;
659 }
660
661 int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
662                                   int_mv *bestmv, int_mv *ref_mv,
663                                   int error_per_bit,
664                                   const vp8_variance_fn_ptr_t *vfp,
665                                   int *mvcost[2], int *distortion,
666                                   unsigned int *sse1) {
667   int bestmse = INT_MAX;
668   int_mv startmv;
669   int_mv this_mv;
670   unsigned char *z = (*(b->base_src) + b->src);
671   int left, right, up, down, diag;
672   unsigned int sse;
673   int whichdir;
674   int thismse;
675   int y_stride;
676   int pre_stride = x->e_mbd.pre.y_stride;
677   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
678
679 #if ARCH_X86 || ARCH_X86_64
680   MACROBLOCKD *xd = &x->e_mbd;
681   unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
682                        bestmv->as_mv.col;
683   unsigned char *y;
684
685   y_stride = 32;
686   /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
687   vfp->copymem(y_0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
688   y = xd->y_buf + y_stride + 1;
689 #else
690   unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride +
691                      bestmv->as_mv.col;
692   y_stride = pre_stride;
693 #endif
694
695   /* central mv */
696   bestmv->as_mv.row *= 8;
697   bestmv->as_mv.col *= 8;
698   startmv = *bestmv;
699
700   /* calculate central point error */
701   bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
702   *distortion = bestmse;
703   bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
704
705   /* go left then right and check error */
706   this_mv.as_mv.row = startmv.as_mv.row;
707   this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
708   /* "halfpix" horizontal variance */
709   thismse = vfp->svf(y - 1, y_stride, 4, 0, z, b->src_stride, &sse);
710   left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
711
712   if (left < bestmse) {
713     *bestmv = this_mv;
714     bestmse = left;
715     *distortion = thismse;
716     *sse1 = sse;
717   }
718
719   this_mv.as_mv.col += 8;
720   /* "halfpix" horizontal variance */
721   thismse = vfp->svf(y, y_stride, 4, 0, z, b->src_stride, &sse);
722   right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
723
724   if (right < bestmse) {
725     *bestmv = this_mv;
726     bestmse = right;
727     *distortion = thismse;
728     *sse1 = sse;
729   }
730
731   /* go up then down and check error */
732   this_mv.as_mv.col = startmv.as_mv.col;
733   this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
734   /* "halfpix" vertical variance */
735   thismse = vfp->svf(y - y_stride, y_stride, 0, 4, z, b->src_stride, &sse);
736   up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
737
738   if (up < bestmse) {
739     *bestmv = this_mv;
740     bestmse = up;
741     *distortion = thismse;
742     *sse1 = sse;
743   }
744
745   this_mv.as_mv.row += 8;
746   /* "halfpix" vertical variance */
747   thismse = vfp->svf(y, y_stride, 0, 4, z, b->src_stride, &sse);
748   down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
749
750   if (down < bestmse) {
751     *bestmv = this_mv;
752     bestmse = down;
753     *distortion = thismse;
754     *sse1 = sse;
755   }
756
757   /* now check 1 more diagonal - */
758   whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
759   this_mv = startmv;
760
761   switch (whichdir) {
762     case 0:
763       this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
764       this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
765       /* "halfpix" horizontal/vertical variance */
766       thismse =
767           vfp->svf(y - 1 - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
768       break;
769     case 1:
770       this_mv.as_mv.col += 4;
771       this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
772       /* "halfpix" horizontal/vertical variance */
773       thismse = vfp->svf(y - y_stride, y_stride, 4, 4, z, b->src_stride, &sse);
774       break;
775     case 2:
776       this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
777       this_mv.as_mv.row += 4;
778       /* "halfpix" horizontal/vertical variance */
779       thismse = vfp->svf(y - 1, y_stride, 4, 4, z, b->src_stride, &sse);
780       break;
781     case 3:
782     default:
783       this_mv.as_mv.col += 4;
784       this_mv.as_mv.row += 4;
785       /* "halfpix" horizontal/vertical variance */
786       thismse = vfp->svf(y, y_stride, 4, 4, z, b->src_stride, &sse);
787       break;
788   }
789
790   diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
791
792   if (diag < bestmse) {
793     *bestmv = this_mv;
794     bestmse = diag;
795     *distortion = thismse;
796     *sse1 = sse;
797   }
798
799   return bestmse;
800 }
801
802 #define CHECK_BOUNDS(range)                    \
803   {                                            \
804     all_in = 1;                                \
805     all_in &= ((br - range) >= x->mv_row_min); \
806     all_in &= ((br + range) <= x->mv_row_max); \
807     all_in &= ((bc - range) >= x->mv_col_min); \
808     all_in &= ((bc + range) <= x->mv_col_max); \
809   }
810
811 #define CHECK_POINT                                  \
812   {                                                  \
813     if (this_mv.as_mv.col < x->mv_col_min) continue; \
814     if (this_mv.as_mv.col > x->mv_col_max) continue; \
815     if (this_mv.as_mv.row < x->mv_row_min) continue; \
816     if (this_mv.as_mv.row > x->mv_row_max) continue; \
817   }
818
819 #define CHECK_BETTER                                                     \
820   {                                                                      \
821     if (thissad < bestsad) {                                             \
822       thissad +=                                                         \
823           mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit); \
824       if (thissad < bestsad) {                                           \
825         bestsad = thissad;                                               \
826         best_site = i;                                                   \
827       }                                                                  \
828     }                                                                    \
829   }
830
831 static const MV next_chkpts[6][3] = {
832   { { -2, 0 }, { -1, -2 }, { 1, -2 } }, { { -1, -2 }, { 1, -2 }, { 2, 0 } },
833   { { 1, -2 }, { 2, 0 }, { 1, 2 } },    { { 2, 0 }, { 1, 2 }, { -1, 2 } },
834   { { 1, 2 }, { -1, 2 }, { -2, 0 } },   { { -1, 2 }, { -2, 0 }, { -1, -2 } }
835 };
836
837 int vp8_hex_search(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
838                    int_mv *best_mv, int search_param, int sad_per_bit,
839                    const vp8_variance_fn_ptr_t *vfp, int *mvsadcost[2],
840                    int *mvcost[2], int_mv *center_mv) {
841   MV hex[6] = {
842     { -1, -2 }, { 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 }
843   };
844   MV neighbors[4] = { { 0, -1 }, { -1, 0 }, { 1, 0 }, { 0, 1 } };
845   int i, j;
846
847   unsigned char *what = (*(b->base_src) + b->src);
848   int what_stride = b->src_stride;
849   int pre_stride = x->e_mbd.pre.y_stride;
850   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
851
852   int in_what_stride = pre_stride;
853   int br, bc;
854   int_mv this_mv;
855   unsigned int bestsad;
856   unsigned int thissad;
857   unsigned char *base_offset;
858   unsigned char *this_offset;
859   int k = -1;
860   int all_in;
861   int best_site = -1;
862   int hex_range = 127;
863   int dia_range = 8;
864
865   int_mv fcenter_mv;
866   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
867   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
868
869   (void)mvcost;
870
871   /* adjust ref_mv to make sure it is within MV range */
872   vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min,
873                x->mv_row_max);
874   br = ref_mv->as_mv.row;
875   bc = ref_mv->as_mv.col;
876
877   /* Work out the start point for the search */
878   base_offset = (unsigned char *)(base_pre + d->offset);
879   this_offset = base_offset + (br * (pre_stride)) + bc;
880   this_mv.as_mv.row = br;
881   this_mv.as_mv.col = bc;
882   bestsad = vfp->sdf(what, what_stride, this_offset, in_what_stride) +
883             mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
884
885 #if CONFIG_MULTI_RES_ENCODING
886   /* Lower search range based on prediction info */
887   if (search_param >= 6)
888     goto cal_neighbors;
889   else if (search_param >= 5)
890     hex_range = 4;
891   else if (search_param >= 4)
892     hex_range = 6;
893   else if (search_param >= 3)
894     hex_range = 15;
895   else if (search_param >= 2)
896     hex_range = 31;
897   else if (search_param >= 1)
898     hex_range = 63;
899
900   dia_range = 8;
901 #else
902   (void)search_param;
903 #endif
904
905   /* hex search */
906   CHECK_BOUNDS(2)
907
908   if (all_in) {
909     for (i = 0; i < 6; ++i) {
910       this_mv.as_mv.row = br + hex[i].row;
911       this_mv.as_mv.col = bc + hex[i].col;
912       this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) +
913                     this_mv.as_mv.col;
914       thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
915       CHECK_BETTER
916     }
917   } else {
918     for (i = 0; i < 6; ++i) {
919       this_mv.as_mv.row = br + hex[i].row;
920       this_mv.as_mv.col = bc + hex[i].col;
921       CHECK_POINT
922       this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) +
923                     this_mv.as_mv.col;
924       thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
925       CHECK_BETTER
926     }
927   }
928
929   if (best_site == -1) {
930     goto cal_neighbors;
931   } else {
932     br += hex[best_site].row;
933     bc += hex[best_site].col;
934     k = best_site;
935   }
936
937   for (j = 1; j < hex_range; ++j) {
938     best_site = -1;
939     CHECK_BOUNDS(2)
940
941     if (all_in) {
942       for (i = 0; i < 3; ++i) {
943         this_mv.as_mv.row = br + next_chkpts[k][i].row;
944         this_mv.as_mv.col = bc + next_chkpts[k][i].col;
945         this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) +
946                       this_mv.as_mv.col;
947         thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
948         CHECK_BETTER
949       }
950     } else {
951       for (i = 0; i < 3; ++i) {
952         this_mv.as_mv.row = br + next_chkpts[k][i].row;
953         this_mv.as_mv.col = bc + next_chkpts[k][i].col;
954         CHECK_POINT
955         this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) +
956                       this_mv.as_mv.col;
957         thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
958         CHECK_BETTER
959       }
960     }
961
962     if (best_site == -1) {
963       break;
964     } else {
965       br += next_chkpts[k][best_site].row;
966       bc += next_chkpts[k][best_site].col;
967       k += 5 + best_site;
968       if (k >= 12) {
969         k -= 12;
970       } else if (k >= 6) {
971         k -= 6;
972       }
973     }
974   }
975
976 /* check 4 1-away neighbors */
977 cal_neighbors:
978   for (j = 0; j < dia_range; ++j) {
979     best_site = -1;
980     CHECK_BOUNDS(1)
981
982     if (all_in) {
983       for (i = 0; i < 4; ++i) {
984         this_mv.as_mv.row = br + neighbors[i].row;
985         this_mv.as_mv.col = bc + neighbors[i].col;
986         this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) +
987                       this_mv.as_mv.col;
988         thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
989         CHECK_BETTER
990       }
991     } else {
992       for (i = 0; i < 4; ++i) {
993         this_mv.as_mv.row = br + neighbors[i].row;
994         this_mv.as_mv.col = bc + neighbors[i].col;
995         CHECK_POINT
996         this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) +
997                       this_mv.as_mv.col;
998         thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride);
999         CHECK_BETTER
1000       }
1001     }
1002
1003     if (best_site == -1) {
1004       break;
1005     } else {
1006       br += neighbors[best_site].row;
1007       bc += neighbors[best_site].col;
1008     }
1009   }
1010
1011   best_mv->as_mv.row = br;
1012   best_mv->as_mv.col = bc;
1013
1014   return bestsad;
1015 }
1016 #undef CHECK_BOUNDS
1017 #undef CHECK_POINT
1018 #undef CHECK_BETTER
1019
1020 int vp8_diamond_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1021                              int_mv *best_mv, int search_param, int sad_per_bit,
1022                              int *num00, vp8_variance_fn_ptr_t *fn_ptr,
1023                              int *mvcost[2], int_mv *center_mv) {
1024   int i, j, step;
1025
1026   unsigned char *what = (*(b->base_src) + b->src);
1027   int what_stride = b->src_stride;
1028   unsigned char *in_what;
1029   int pre_stride = x->e_mbd.pre.y_stride;
1030   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1031   int in_what_stride = pre_stride;
1032   unsigned char *best_address;
1033
1034   int tot_steps;
1035   int_mv this_mv;
1036
1037   unsigned int bestsad;
1038   unsigned int thissad;
1039   int best_site = 0;
1040   int last_site = 0;
1041
1042   int ref_row;
1043   int ref_col;
1044   int this_row_offset;
1045   int this_col_offset;
1046   search_site *ss;
1047
1048   unsigned char *check_here;
1049
1050   int *mvsadcost[2];
1051   int_mv fcenter_mv;
1052
1053   mvsadcost[0] = x->mvsadcost[0];
1054   mvsadcost[1] = x->mvsadcost[1];
1055   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1056   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1057
1058   vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min,
1059                x->mv_row_max);
1060   ref_row = ref_mv->as_mv.row;
1061   ref_col = ref_mv->as_mv.col;
1062   *num00 = 0;
1063   best_mv->as_mv.row = ref_row;
1064   best_mv->as_mv.col = ref_col;
1065
1066   /* Work out the start point for the search */
1067   in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) +
1068                               ref_col);
1069   best_address = in_what;
1070
1071   /* Check the starting position */
1072   bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride) +
1073             mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1074
1075   /* search_param determines the length of the initial step and hence
1076    * the number of iterations 0 = initial step (MAX_FIRST_STEP) pel :
1077    * 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1078    */
1079   ss = &x->ss[search_param * x->searches_per_step];
1080   tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1081
1082   i = 1;
1083
1084   for (step = 0; step < tot_steps; ++step) {
1085     for (j = 0; j < x->searches_per_step; ++j) {
1086       /* Trap illegal vectors */
1087       this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1088       this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1089
1090       if ((this_col_offset > x->mv_col_min) &&
1091           (this_col_offset < x->mv_col_max) &&
1092           (this_row_offset > x->mv_row_min) &&
1093           (this_row_offset < x->mv_row_max))
1094
1095       {
1096         check_here = ss[i].offset + best_address;
1097         thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1098
1099         if (thissad < bestsad) {
1100           this_mv.as_mv.row = this_row_offset;
1101           this_mv.as_mv.col = this_col_offset;
1102           thissad +=
1103               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1104
1105           if (thissad < bestsad) {
1106             bestsad = thissad;
1107             best_site = i;
1108           }
1109         }
1110       }
1111
1112       i++;
1113     }
1114
1115     if (best_site != last_site) {
1116       best_mv->as_mv.row += ss[best_site].mv.row;
1117       best_mv->as_mv.col += ss[best_site].mv.col;
1118       best_address += ss[best_site].offset;
1119       last_site = best_site;
1120     } else if (best_address == in_what) {
1121       (*num00)++;
1122     }
1123   }
1124
1125   this_mv.as_mv.row = best_mv->as_mv.row << 3;
1126   this_mv.as_mv.col = best_mv->as_mv.col << 3;
1127
1128   return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) +
1129          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1130 }
1131
1132 int vp8_diamond_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1133                              int_mv *best_mv, int search_param, int sad_per_bit,
1134                              int *num00, vp8_variance_fn_ptr_t *fn_ptr,
1135                              int *mvcost[2], int_mv *center_mv) {
1136   int i, j, step;
1137
1138   unsigned char *what = (*(b->base_src) + b->src);
1139   int what_stride = b->src_stride;
1140   unsigned char *in_what;
1141   int pre_stride = x->e_mbd.pre.y_stride;
1142   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1143   int in_what_stride = pre_stride;
1144   unsigned char *best_address;
1145
1146   int tot_steps;
1147   int_mv this_mv;
1148
1149   unsigned int bestsad;
1150   unsigned int thissad;
1151   int best_site = 0;
1152   int last_site = 0;
1153
1154   int ref_row;
1155   int ref_col;
1156   int this_row_offset;
1157   int this_col_offset;
1158   search_site *ss;
1159
1160   unsigned char *check_here;
1161
1162   int *mvsadcost[2];
1163   int_mv fcenter_mv;
1164
1165   mvsadcost[0] = x->mvsadcost[0];
1166   mvsadcost[1] = x->mvsadcost[1];
1167   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1168   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1169
1170   vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min,
1171                x->mv_row_max);
1172   ref_row = ref_mv->as_mv.row;
1173   ref_col = ref_mv->as_mv.col;
1174   *num00 = 0;
1175   best_mv->as_mv.row = ref_row;
1176   best_mv->as_mv.col = ref_col;
1177
1178   /* Work out the start point for the search */
1179   in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) +
1180                               ref_col);
1181   best_address = in_what;
1182
1183   /* Check the starting position */
1184   bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride) +
1185             mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1186
1187   /* search_param determines the length of the initial step and hence the
1188    * number of iterations 0 = initial step (MAX_FIRST_STEP) pel : 1 =
1189    * (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1190    */
1191   ss = &x->ss[search_param * x->searches_per_step];
1192   tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1193
1194   i = 1;
1195
1196   for (step = 0; step < tot_steps; ++step) {
1197     int all_in = 1, t;
1198
1199     /* To know if all neighbor points are within the bounds, 4 bounds
1200      * checking are enough instead of checking 4 bounds for each
1201      * points.
1202      */
1203     all_in &= ((best_mv->as_mv.row + ss[i].mv.row) > x->mv_row_min);
1204     all_in &= ((best_mv->as_mv.row + ss[i + 1].mv.row) < x->mv_row_max);
1205     all_in &= ((best_mv->as_mv.col + ss[i + 2].mv.col) > x->mv_col_min);
1206     all_in &= ((best_mv->as_mv.col + ss[i + 3].mv.col) < x->mv_col_max);
1207
1208     if (all_in) {
1209       unsigned int sad_array[4];
1210
1211       for (j = 0; j < x->searches_per_step; j += 4) {
1212         const unsigned char *block_offset[4];
1213
1214         for (t = 0; t < 4; ++t) {
1215           block_offset[t] = ss[i + t].offset + best_address;
1216         }
1217
1218         fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1219                        sad_array);
1220
1221         for (t = 0; t < 4; t++, i++) {
1222           if (sad_array[t] < bestsad) {
1223             this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1224             this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1225             sad_array[t] +=
1226                 mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1227
1228             if (sad_array[t] < bestsad) {
1229               bestsad = sad_array[t];
1230               best_site = i;
1231             }
1232           }
1233         }
1234       }
1235     } else {
1236       for (j = 0; j < x->searches_per_step; ++j) {
1237         /* Trap illegal vectors */
1238         this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1239         this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1240
1241         if ((this_col_offset > x->mv_col_min) &&
1242             (this_col_offset < x->mv_col_max) &&
1243             (this_row_offset > x->mv_row_min) &&
1244             (this_row_offset < x->mv_row_max)) {
1245           check_here = ss[i].offset + best_address;
1246           thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1247
1248           if (thissad < bestsad) {
1249             this_mv.as_mv.row = this_row_offset;
1250             this_mv.as_mv.col = this_col_offset;
1251             thissad +=
1252                 mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1253
1254             if (thissad < bestsad) {
1255               bestsad = thissad;
1256               best_site = i;
1257             }
1258           }
1259         }
1260         i++;
1261       }
1262     }
1263
1264     if (best_site != last_site) {
1265       best_mv->as_mv.row += ss[best_site].mv.row;
1266       best_mv->as_mv.col += ss[best_site].mv.col;
1267       best_address += ss[best_site].offset;
1268       last_site = best_site;
1269     } else if (best_address == in_what) {
1270       (*num00)++;
1271     }
1272   }
1273
1274   this_mv.as_mv.row = best_mv->as_mv.row * 8;
1275   this_mv.as_mv.col = best_mv->as_mv.col * 8;
1276
1277   return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) +
1278          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1279 }
1280
1281 int vp8_full_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1282                           int sad_per_bit, int distance,
1283                           vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1284                           int_mv *center_mv) {
1285   unsigned char *what = (*(b->base_src) + b->src);
1286   int what_stride = b->src_stride;
1287   unsigned char *in_what;
1288   int pre_stride = x->e_mbd.pre.y_stride;
1289   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1290   int in_what_stride = pre_stride;
1291   int mv_stride = pre_stride;
1292   unsigned char *bestaddress;
1293   int_mv *best_mv = &d->bmi.mv;
1294   int_mv this_mv;
1295   unsigned int bestsad;
1296   unsigned int thissad;
1297   int r, c;
1298
1299   unsigned char *check_here;
1300
1301   int ref_row = ref_mv->as_mv.row;
1302   int ref_col = ref_mv->as_mv.col;
1303
1304   int row_min = ref_row - distance;
1305   int row_max = ref_row + distance;
1306   int col_min = ref_col - distance;
1307   int col_max = ref_col + distance;
1308
1309   int *mvsadcost[2];
1310   int_mv fcenter_mv;
1311
1312   mvsadcost[0] = x->mvsadcost[0];
1313   mvsadcost[1] = x->mvsadcost[1];
1314   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1315   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1316
1317   /* Work out the mid point for the search */
1318   in_what = base_pre + d->offset;
1319   bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1320
1321   best_mv->as_mv.row = ref_row;
1322   best_mv->as_mv.col = ref_col;
1323
1324   /* Baseline value at the centre */
1325   bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride) +
1326             mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1327
1328   /* Apply further limits to prevent us looking using vectors that
1329    * stretch beyiond the UMV border
1330    */
1331   if (col_min < x->mv_col_min) col_min = x->mv_col_min;
1332
1333   if (col_max > x->mv_col_max) col_max = x->mv_col_max;
1334
1335   if (row_min < x->mv_row_min) row_min = x->mv_row_min;
1336
1337   if (row_max > x->mv_row_max) row_max = x->mv_row_max;
1338
1339   for (r = row_min; r < row_max; ++r) {
1340     this_mv.as_mv.row = r;
1341     check_here = r * mv_stride + in_what + col_min;
1342
1343     for (c = col_min; c < col_max; ++c) {
1344       thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1345
1346       this_mv.as_mv.col = c;
1347       thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1348
1349       if (thissad < bestsad) {
1350         bestsad = thissad;
1351         best_mv->as_mv.row = r;
1352         best_mv->as_mv.col = c;
1353         bestaddress = check_here;
1354       }
1355
1356       check_here++;
1357     }
1358   }
1359
1360   this_mv.as_mv.row = best_mv->as_mv.row << 3;
1361   this_mv.as_mv.col = best_mv->as_mv.col << 3;
1362
1363   return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) +
1364          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1365 }
1366
1367 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1368                           int sad_per_bit, int distance,
1369                           vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1370                           int_mv *center_mv) {
1371   unsigned char *what = (*(b->base_src) + b->src);
1372   int what_stride = b->src_stride;
1373   unsigned char *in_what;
1374   int pre_stride = x->e_mbd.pre.y_stride;
1375   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1376   int in_what_stride = pre_stride;
1377   int mv_stride = pre_stride;
1378   unsigned char *bestaddress;
1379   int_mv *best_mv = &d->bmi.mv;
1380   int_mv this_mv;
1381   unsigned int bestsad;
1382   unsigned int thissad;
1383   int r, c;
1384
1385   unsigned char *check_here;
1386
1387   int ref_row = ref_mv->as_mv.row;
1388   int ref_col = ref_mv->as_mv.col;
1389
1390   int row_min = ref_row - distance;
1391   int row_max = ref_row + distance;
1392   int col_min = ref_col - distance;
1393   int col_max = ref_col + distance;
1394
1395   unsigned int sad_array[3];
1396
1397   int *mvsadcost[2];
1398   int_mv fcenter_mv;
1399
1400   mvsadcost[0] = x->mvsadcost[0];
1401   mvsadcost[1] = x->mvsadcost[1];
1402   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1403   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1404
1405   /* Work out the mid point for the search */
1406   in_what = base_pre + d->offset;
1407   bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1408
1409   best_mv->as_mv.row = ref_row;
1410   best_mv->as_mv.col = ref_col;
1411
1412   /* Baseline value at the centre */
1413   bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride) +
1414             mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1415
1416   /* Apply further limits to prevent us looking using vectors that stretch
1417    * beyond the UMV border
1418    */
1419   if (col_min < x->mv_col_min) col_min = x->mv_col_min;
1420
1421   if (col_max > x->mv_col_max) col_max = x->mv_col_max;
1422
1423   if (row_min < x->mv_row_min) row_min = x->mv_row_min;
1424
1425   if (row_max > x->mv_row_max) row_max = x->mv_row_max;
1426
1427   for (r = row_min; r < row_max; ++r) {
1428     this_mv.as_mv.row = r;
1429     check_here = r * mv_stride + in_what + col_min;
1430     c = col_min;
1431
1432     while ((c + 2) < col_max) {
1433       int i;
1434
1435       fn_ptr->sdx3f(what, what_stride, check_here, in_what_stride, sad_array);
1436
1437       for (i = 0; i < 3; ++i) {
1438         thissad = sad_array[i];
1439
1440         if (thissad < bestsad) {
1441           this_mv.as_mv.col = c;
1442           thissad +=
1443               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1444
1445           if (thissad < bestsad) {
1446             bestsad = thissad;
1447             best_mv->as_mv.row = r;
1448             best_mv->as_mv.col = c;
1449             bestaddress = check_here;
1450           }
1451         }
1452
1453         check_here++;
1454         c++;
1455       }
1456     }
1457
1458     while (c < col_max) {
1459       thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1460
1461       if (thissad < bestsad) {
1462         this_mv.as_mv.col = c;
1463         thissad +=
1464             mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1465
1466         if (thissad < bestsad) {
1467           bestsad = thissad;
1468           best_mv->as_mv.row = r;
1469           best_mv->as_mv.col = c;
1470           bestaddress = check_here;
1471         }
1472       }
1473
1474       check_here++;
1475       c++;
1476     }
1477   }
1478
1479   this_mv.as_mv.row = best_mv->as_mv.row << 3;
1480   this_mv.as_mv.col = best_mv->as_mv.col << 3;
1481
1482   return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) +
1483          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1484 }
1485
1486 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1487                           int sad_per_bit, int distance,
1488                           vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1489                           int_mv *center_mv) {
1490   unsigned char *what = (*(b->base_src) + b->src);
1491   int what_stride = b->src_stride;
1492   int pre_stride = x->e_mbd.pre.y_stride;
1493   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1494   unsigned char *in_what;
1495   int in_what_stride = pre_stride;
1496   int mv_stride = pre_stride;
1497   unsigned char *bestaddress;
1498   int_mv *best_mv = &d->bmi.mv;
1499   int_mv this_mv;
1500   unsigned int bestsad;
1501   unsigned int thissad;
1502   int r, c;
1503
1504   unsigned char *check_here;
1505
1506   int ref_row = ref_mv->as_mv.row;
1507   int ref_col = ref_mv->as_mv.col;
1508
1509   int row_min = ref_row - distance;
1510   int row_max = ref_row + distance;
1511   int col_min = ref_col - distance;
1512   int col_max = ref_col + distance;
1513
1514   DECLARE_ALIGNED(16, unsigned int, sad_array8[8]);
1515   unsigned int sad_array[3];
1516
1517   int *mvsadcost[2];
1518   int_mv fcenter_mv;
1519
1520   mvsadcost[0] = x->mvsadcost[0];
1521   mvsadcost[1] = x->mvsadcost[1];
1522   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1523   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1524
1525   /* Work out the mid point for the search */
1526   in_what = base_pre + d->offset;
1527   bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1528
1529   best_mv->as_mv.row = ref_row;
1530   best_mv->as_mv.col = ref_col;
1531
1532   /* Baseline value at the centre */
1533   bestsad = fn_ptr->sdf(what, what_stride, bestaddress, in_what_stride) +
1534             mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1535
1536   /* Apply further limits to prevent us looking using vectors that stretch
1537    * beyond the UMV border
1538    */
1539   if (col_min < x->mv_col_min) col_min = x->mv_col_min;
1540
1541   if (col_max > x->mv_col_max) col_max = x->mv_col_max;
1542
1543   if (row_min < x->mv_row_min) row_min = x->mv_row_min;
1544
1545   if (row_max > x->mv_row_max) row_max = x->mv_row_max;
1546
1547   for (r = row_min; r < row_max; ++r) {
1548     this_mv.as_mv.row = r;
1549     check_here = r * mv_stride + in_what + col_min;
1550     c = col_min;
1551
1552     while ((c + 7) < col_max) {
1553       int i;
1554
1555       fn_ptr->sdx8f(what, what_stride, check_here, in_what_stride, sad_array8);
1556
1557       for (i = 0; i < 8; ++i) {
1558         thissad = sad_array8[i];
1559
1560         if (thissad < bestsad) {
1561           this_mv.as_mv.col = c;
1562           thissad +=
1563               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1564
1565           if (thissad < bestsad) {
1566             bestsad = thissad;
1567             best_mv->as_mv.row = r;
1568             best_mv->as_mv.col = c;
1569             bestaddress = check_here;
1570           }
1571         }
1572
1573         check_here++;
1574         c++;
1575       }
1576     }
1577
1578     while ((c + 2) < col_max) {
1579       int i;
1580
1581       fn_ptr->sdx3f(what, what_stride, check_here, in_what_stride, sad_array);
1582
1583       for (i = 0; i < 3; ++i) {
1584         thissad = sad_array[i];
1585
1586         if (thissad < bestsad) {
1587           this_mv.as_mv.col = c;
1588           thissad +=
1589               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1590
1591           if (thissad < bestsad) {
1592             bestsad = thissad;
1593             best_mv->as_mv.row = r;
1594             best_mv->as_mv.col = c;
1595             bestaddress = check_here;
1596           }
1597         }
1598
1599         check_here++;
1600         c++;
1601       }
1602     }
1603
1604     while (c < col_max) {
1605       thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1606
1607       if (thissad < bestsad) {
1608         this_mv.as_mv.col = c;
1609         thissad +=
1610             mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1611
1612         if (thissad < bestsad) {
1613           bestsad = thissad;
1614           best_mv->as_mv.row = r;
1615           best_mv->as_mv.col = c;
1616           bestaddress = check_here;
1617         }
1618       }
1619
1620       check_here++;
1621       c++;
1622     }
1623   }
1624
1625   this_mv.as_mv.row = best_mv->as_mv.row * 8;
1626   this_mv.as_mv.col = best_mv->as_mv.col * 8;
1627
1628   return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) +
1629          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1630 }
1631
1632 int vp8_refining_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1633                               int_mv *ref_mv, int error_per_bit,
1634                               int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1635                               int *mvcost[2], int_mv *center_mv) {
1636   MV neighbors[4] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
1637   int i, j;
1638   short this_row_offset, this_col_offset;
1639
1640   int what_stride = b->src_stride;
1641   int pre_stride = x->e_mbd.pre.y_stride;
1642   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1643   int in_what_stride = pre_stride;
1644   unsigned char *what = (*(b->base_src) + b->src);
1645   unsigned char *best_address =
1646       (unsigned char *)(base_pre + d->offset +
1647                         (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1648   unsigned char *check_here;
1649   int_mv this_mv;
1650   unsigned int bestsad;
1651   unsigned int thissad;
1652
1653   int *mvsadcost[2];
1654   int_mv fcenter_mv;
1655
1656   mvsadcost[0] = x->mvsadcost[0];
1657   mvsadcost[1] = x->mvsadcost[1];
1658   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1659   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1660
1661   bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride) +
1662             mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1663
1664   for (i = 0; i < search_range; ++i) {
1665     int best_site = -1;
1666
1667     for (j = 0; j < 4; ++j) {
1668       this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1669       this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1670
1671       if ((this_col_offset > x->mv_col_min) &&
1672           (this_col_offset < x->mv_col_max) &&
1673           (this_row_offset > x->mv_row_min) &&
1674           (this_row_offset < x->mv_row_max)) {
1675         check_here = (neighbors[j].row) * in_what_stride + neighbors[j].col +
1676                      best_address;
1677         thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1678
1679         if (thissad < bestsad) {
1680           this_mv.as_mv.row = this_row_offset;
1681           this_mv.as_mv.col = this_col_offset;
1682           thissad +=
1683               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1684
1685           if (thissad < bestsad) {
1686             bestsad = thissad;
1687             best_site = j;
1688           }
1689         }
1690       }
1691     }
1692
1693     if (best_site == -1) {
1694       break;
1695     } else {
1696       ref_mv->as_mv.row += neighbors[best_site].row;
1697       ref_mv->as_mv.col += neighbors[best_site].col;
1698       best_address += (neighbors[best_site].row) * in_what_stride +
1699                       neighbors[best_site].col;
1700     }
1701   }
1702
1703   this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1704   this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1705
1706   return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) +
1707          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1708 }
1709
1710 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1711                               int_mv *ref_mv, int error_per_bit,
1712                               int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1713                               int *mvcost[2], int_mv *center_mv) {
1714   MV neighbors[4] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
1715   int i, j;
1716   short this_row_offset, this_col_offset;
1717
1718   int what_stride = b->src_stride;
1719   int pre_stride = x->e_mbd.pre.y_stride;
1720   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1721   int in_what_stride = pre_stride;
1722   unsigned char *what = (*(b->base_src) + b->src);
1723   unsigned char *best_address =
1724       (unsigned char *)(base_pre + d->offset +
1725                         (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1726   unsigned char *check_here;
1727   int_mv this_mv;
1728   unsigned int bestsad;
1729   unsigned int thissad;
1730
1731   int *mvsadcost[2];
1732   int_mv fcenter_mv;
1733
1734   mvsadcost[0] = x->mvsadcost[0];
1735   mvsadcost[1] = x->mvsadcost[1];
1736   fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1737   fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1738
1739   bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride) +
1740             mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1741
1742   for (i = 0; i < search_range; ++i) {
1743     int best_site = -1;
1744     int all_in = 1;
1745
1746     all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1747     all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1748     all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1749     all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1750
1751     if (all_in) {
1752       unsigned int sad_array[4];
1753       const unsigned char *block_offset[4];
1754       block_offset[0] = best_address - in_what_stride;
1755       block_offset[1] = best_address - 1;
1756       block_offset[2] = best_address + 1;
1757       block_offset[3] = best_address + in_what_stride;
1758
1759       fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1760                      sad_array);
1761
1762       for (j = 0; j < 4; ++j) {
1763         if (sad_array[j] < bestsad) {
1764           this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1765           this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1766           sad_array[j] +=
1767               mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1768
1769           if (sad_array[j] < bestsad) {
1770             bestsad = sad_array[j];
1771             best_site = j;
1772           }
1773         }
1774       }
1775     } else {
1776       for (j = 0; j < 4; ++j) {
1777         this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1778         this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1779
1780         if ((this_col_offset > x->mv_col_min) &&
1781             (this_col_offset < x->mv_col_max) &&
1782             (this_row_offset > x->mv_row_min) &&
1783             (this_row_offset < x->mv_row_max)) {
1784           check_here = (neighbors[j].row) * in_what_stride + neighbors[j].col +
1785                        best_address;
1786           thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride);
1787
1788           if (thissad < bestsad) {
1789             this_mv.as_mv.row = this_row_offset;
1790             this_mv.as_mv.col = this_col_offset;
1791             thissad +=
1792                 mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1793
1794             if (thissad < bestsad) {
1795               bestsad = thissad;
1796               best_site = j;
1797             }
1798           }
1799         }
1800       }
1801     }
1802
1803     if (best_site == -1) {
1804       break;
1805     } else {
1806       ref_mv->as_mv.row += neighbors[best_site].row;
1807       ref_mv->as_mv.col += neighbors[best_site].col;
1808       best_address += (neighbors[best_site].row) * in_what_stride +
1809                       neighbors[best_site].col;
1810     }
1811   }
1812
1813   this_mv.as_mv.row = ref_mv->as_mv.row * 8;
1814   this_mv.as_mv.col = ref_mv->as_mv.col * 8;
1815
1816   return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) +
1817          mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1818 }