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