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