remove some magic weights associated with sad_per_bit
[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,
879                         in_what_stride, 0x7fffffff)
880             + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
881
882     // hex search
883     //j=0
884     CHECK_BOUNDS(2)
885
886     if(all_in)
887     {
888         for (i = 0; i < 6; i++)
889         {
890             this_mv.as_mv.row = br + hex[i].row;
891             this_mv.as_mv.col = bc + hex[i].col;
892             this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
893             thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
894             CHECK_BETTER
895         }
896     }else
897     {
898         for (i = 0; i < 6; i++)
899         {
900             this_mv.as_mv.row = br + hex[i].row;
901             this_mv.as_mv.col = bc + hex[i].col;
902             CHECK_POINT
903             this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
904             thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
905             CHECK_BETTER
906         }
907     }
908
909     if (best_site == -1)
910         goto cal_neighbors;
911     else
912     {
913         br += hex[best_site].row;
914         bc += hex[best_site].col;
915         k = best_site;
916     }
917
918     for (j = 1; j < 127; j++)
919     {
920         best_site = -1;
921         CHECK_BOUNDS(2)
922
923         if(all_in)
924         {
925             for (i = 0; i < 3; i++)
926             {
927                 this_mv.as_mv.row = br + next_chkpts[k][i].row;
928                 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
929                 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
930                 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
931                 CHECK_BETTER
932             }
933         }else
934         {
935             for (i = 0; i < 3; i++)
936             {
937                 this_mv.as_mv.row = br + next_chkpts[k][i].row;
938                 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
939                 CHECK_POINT
940                 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
941                 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
942                 CHECK_BETTER
943             }
944         }
945
946         if (best_site == -1)
947             break;
948         else
949         {
950             br += next_chkpts[k][best_site].row;
951             bc += next_chkpts[k][best_site].col;
952             k += 5 + best_site;
953             if (k >= 12) k -= 12;
954             else if (k >= 6) k -= 6;
955         }
956     }
957
958     // check 4 1-away neighbors
959 cal_neighbors:
960     for (j = 0; j < 32; j++)
961     {
962         best_site = -1;
963         CHECK_BOUNDS(1)
964
965         if(all_in)
966         {
967             for (i = 0; i < 4; i++)
968             {
969                 this_mv.as_mv.row = br + neighbors[i].row;
970                 this_mv.as_mv.col = bc + neighbors[i].col;
971                 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
972                 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
973                 CHECK_BETTER
974             }
975         }else
976         {
977             for (i = 0; i < 4; i++)
978             {
979                 this_mv.as_mv.row = br + neighbors[i].row;
980                 this_mv.as_mv.col = bc + neighbors[i].col;
981                 CHECK_POINT
982                 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
983                 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
984                 CHECK_BETTER
985             }
986         }
987
988         if (best_site == -1)
989             break;
990         else
991         {
992             br += neighbors[best_site].row;
993             bc += neighbors[best_site].col;
994         }
995     }
996
997     best_mv->as_mv.row = br;
998     best_mv->as_mv.col = bc;
999     this_mv.as_mv.row = br<<3;
1000     this_mv.as_mv.col = bc<<3;
1001
1002     this_offset = (unsigned char *)(*(d->base_pre) + d->pre + (br * (in_what_stride)) + bc);
1003     return vfp->vf(what, what_stride, this_offset, in_what_stride, &bestsad) + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit) ;
1004 }
1005 #undef CHECK_BOUNDS
1006 #undef CHECK_POINT
1007 #undef CHECK_BETTER
1008
1009 int vp8_diamond_search_sad
1010 (
1011     MACROBLOCK *x,
1012     BLOCK *b,
1013     BLOCKD *d,
1014     int_mv *ref_mv,
1015     int_mv *best_mv,
1016     int search_param,
1017     int sad_per_bit,
1018     int *num00,
1019     vp8_variance_fn_ptr_t *fn_ptr,
1020     int *mvcost[2],
1021     int_mv *center_mv
1022 )
1023 {
1024     int i, j, step;
1025
1026     unsigned char *what = (*(b->base_src) + b->src);
1027     int what_stride = b->src_stride;
1028     unsigned char *in_what;
1029     int in_what_stride = d->pre_stride;
1030     unsigned char *best_address;
1031
1032     int tot_steps;
1033     int_mv this_mv;
1034
1035     int bestsad = INT_MAX;
1036     int best_site = 0;
1037     int last_site = 0;
1038
1039     int ref_row = ref_mv->as_mv.row >> 3;
1040     int ref_col = ref_mv->as_mv.col >> 3;
1041     int this_row_offset;
1042     int this_col_offset;
1043     search_site *ss;
1044
1045     unsigned char *check_here;
1046     int thissad;
1047
1048     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1049     int_mv fcenter_mv;
1050     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1051     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1052
1053     *num00 = 0;
1054
1055     best_mv->as_mv.row = ref_row;
1056     best_mv->as_mv.col = ref_col;
1057
1058     // Work out the start point for the search
1059     in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1060     best_address = in_what;
1061
1062     // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1063     if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1064     (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1065     {
1066         // Check the starting position
1067         bestsad = fn_ptr->sdf(what, what_stride, in_what,
1068                               in_what_stride, 0x7fffffff)
1069                 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1070     }
1071
1072     // search_param determines the length of the initial step and hence the number of iterations
1073     // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1074     ss = &x->ss[search_param * x->searches_per_step];
1075     tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1076
1077     i = 1;
1078
1079     for (step = 0; step < tot_steps ; step++)
1080     {
1081         for (j = 0 ; j < x->searches_per_step ; j++)
1082         {
1083             // Trap illegal vectors
1084             this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1085             this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1086
1087             if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1088             (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1089
1090             {
1091                 check_here = ss[i].offset + best_address;
1092                 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1093
1094                 if (thissad < bestsad)
1095                 {
1096                     this_mv.as_mv.row = this_row_offset;
1097                     this_mv.as_mv.col = this_col_offset;
1098                     thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1099                                                 mvsadcost, sad_per_bit);
1100
1101                     if (thissad < bestsad)
1102                     {
1103                         bestsad = thissad;
1104                         best_site = i;
1105                     }
1106                 }
1107             }
1108
1109             i++;
1110         }
1111
1112         if (best_site != last_site)
1113         {
1114             best_mv->as_mv.row += ss[best_site].mv.row;
1115             best_mv->as_mv.col += ss[best_site].mv.col;
1116             best_address += ss[best_site].offset;
1117             last_site = best_site;
1118         }
1119         else if (best_address == in_what)
1120             (*num00)++;
1121     }
1122
1123     this_mv.as_mv.row = best_mv->as_mv.row << 3;
1124     this_mv.as_mv.col = best_mv->as_mv.col << 3;
1125
1126     if (bestsad == INT_MAX)
1127         return INT_MAX;
1128
1129     return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1130         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1131 }
1132
1133 int vp8_diamond_search_sadx4
1134 (
1135     MACROBLOCK *x,
1136     BLOCK *b,
1137     BLOCKD *d,
1138     int_mv *ref_mv,
1139     int_mv *best_mv,
1140     int search_param,
1141     int sad_per_bit,
1142     int *num00,
1143     vp8_variance_fn_ptr_t *fn_ptr,
1144     int *mvcost[2],
1145     int_mv *center_mv
1146 )
1147 {
1148     int i, j, step;
1149
1150     unsigned char *what = (*(b->base_src) + b->src);
1151     int what_stride = b->src_stride;
1152     unsigned char *in_what;
1153     int in_what_stride = d->pre_stride;
1154     unsigned char *best_address;
1155
1156     int tot_steps;
1157     int_mv this_mv;
1158
1159     int bestsad = INT_MAX;
1160     int best_site = 0;
1161     int last_site = 0;
1162
1163     int ref_row = ref_mv->as_mv.row >> 3;
1164     int ref_col = ref_mv->as_mv.col >> 3;
1165     int this_row_offset;
1166     int this_col_offset;
1167     search_site *ss;
1168
1169     unsigned char *check_here;
1170     unsigned int thissad;
1171
1172     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1173     int_mv fcenter_mv;
1174     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1175     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1176
1177     *num00 = 0;
1178     best_mv->as_mv.row = ref_row;
1179     best_mv->as_mv.col = ref_col;
1180
1181     // Work out the start point for the search
1182     in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1183     best_address = in_what;
1184
1185     // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1186     if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1187     (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1188     {
1189         // Check the starting position
1190         bestsad = fn_ptr->sdf(what, what_stride,
1191                               in_what, in_what_stride, 0x7fffffff)
1192                 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1193     }
1194
1195     // search_param determines the length of the initial step and hence the number of iterations
1196     // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1197     ss = &x->ss[search_param * x->searches_per_step];
1198     tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1199
1200     i = 1;
1201
1202     for (step = 0; step < tot_steps ; step++)
1203     {
1204         int all_in = 1, t;
1205
1206         // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1207         // checking 4 bounds for each points.
1208         all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
1209         all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
1210         all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
1211         all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
1212
1213         if (all_in)
1214         {
1215             unsigned int sad_array[4];
1216
1217             for (j = 0 ; j < x->searches_per_step ; j += 4)
1218             {
1219                 unsigned char *block_offset[4];
1220
1221                 for (t = 0; t < 4; t++)
1222                     block_offset[t] = ss[i+t].offset + best_address;
1223
1224                 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1225
1226                 for (t = 0; t < 4; t++, i++)
1227                 {
1228                     if (sad_array[t] < bestsad)
1229                     {
1230                         this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1231                         this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1232                         sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
1233                                                        mvsadcost, sad_per_bit);
1234
1235                         if (sad_array[t] < bestsad)
1236                         {
1237                             bestsad = sad_array[t];
1238                             best_site = i;
1239                         }
1240                     }
1241                 }
1242             }
1243         }
1244         else
1245         {
1246             for (j = 0 ; j < x->searches_per_step ; j++)
1247             {
1248                 // Trap illegal vectors
1249                 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1250                 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1251
1252                 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1253                 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1254                 {
1255                     check_here = ss[i].offset + best_address;
1256                     thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1257
1258                     if (thissad < bestsad)
1259                     {
1260                         this_mv.as_mv.row = this_row_offset;
1261                         this_mv.as_mv.col = this_col_offset;
1262                         thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1263                                                    mvsadcost, sad_per_bit);
1264
1265                         if (thissad < bestsad)
1266                         {
1267                             bestsad = thissad;
1268                             best_site = i;
1269                         }
1270                     }
1271                 }
1272                 i++;
1273             }
1274         }
1275
1276         if (best_site != last_site)
1277         {
1278             best_mv->as_mv.row += ss[best_site].mv.row;
1279             best_mv->as_mv.col += ss[best_site].mv.col;
1280             best_address += ss[best_site].offset;
1281             last_site = best_site;
1282         }
1283         else if (best_address == in_what)
1284             (*num00)++;
1285     }
1286
1287     this_mv.as_mv.row = best_mv->as_mv.row << 3;
1288     this_mv.as_mv.col = best_mv->as_mv.col << 3;
1289
1290     if (bestsad == INT_MAX)
1291         return INT_MAX;
1292
1293     return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1294         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1295 }
1296
1297 int vp8_full_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1298                         int sad_per_bit, int distance,
1299                         vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1300                         int_mv *center_mv)
1301 {
1302     unsigned char *what = (*(b->base_src) + b->src);
1303     int what_stride = b->src_stride;
1304     unsigned char *in_what;
1305     int in_what_stride = d->pre_stride;
1306     int mv_stride = d->pre_stride;
1307     unsigned char *bestaddress;
1308     int_mv *best_mv = &d->bmi.mv;
1309     int_mv this_mv;
1310     int bestsad = INT_MAX;
1311     int r, c;
1312
1313     unsigned char *check_here;
1314     int thissad;
1315
1316     int ref_row = ref_mv->as_mv.row;
1317     int ref_col = ref_mv->as_mv.col;
1318
1319     int row_min = ref_row - distance;
1320     int row_max = ref_row + distance;
1321     int col_min = ref_col - distance;
1322     int col_max = ref_col + distance;
1323
1324     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1325     int_mv fcenter_mv;
1326     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1327     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1328
1329     // Work out the mid point for the search
1330     in_what = *(d->base_pre) + d->pre;
1331     bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1332
1333     best_mv->as_mv.row = ref_row;
1334     best_mv->as_mv.col = ref_col;
1335
1336     // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1337     if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1338     (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1339     {
1340         // Baseline value at the centre
1341
1342         //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));
1343         bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
1344                               in_what_stride, 0x7fffffff)
1345                 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1346     }
1347
1348     // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1349     if (col_min < x->mv_col_min)
1350         col_min = x->mv_col_min;
1351
1352     if (col_max > x->mv_col_max)
1353         col_max = x->mv_col_max;
1354
1355     if (row_min < x->mv_row_min)
1356         row_min = x->mv_row_min;
1357
1358     if (row_max > x->mv_row_max)
1359         row_max = x->mv_row_max;
1360
1361     for (r = row_min; r < row_max ; r++)
1362     {
1363         this_mv.as_mv.row = r;
1364         check_here = r * mv_stride + in_what + col_min;
1365
1366         for (c = col_min; c < col_max; c++)
1367         {
1368             thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1369
1370             this_mv.as_mv.col = c;
1371             thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1372                         mvsadcost, sad_per_bit);
1373
1374             if (thissad < bestsad)
1375             {
1376                 bestsad = thissad;
1377                 best_mv->as_mv.row = r;
1378                 best_mv->as_mv.col = c;
1379                 bestaddress = check_here;
1380             }
1381
1382             check_here++;
1383         }
1384     }
1385
1386     this_mv.as_mv.row = best_mv->as_mv.row << 3;
1387     this_mv.as_mv.col = best_mv->as_mv.col << 3;
1388
1389     if (bestsad < INT_MAX)
1390         return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1391         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1392     else
1393         return INT_MAX;
1394 }
1395
1396 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1397                           int sad_per_bit, int distance,
1398                           vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1399                           int_mv *center_mv)
1400 {
1401     unsigned char *what = (*(b->base_src) + b->src);
1402     int what_stride = b->src_stride;
1403     unsigned char *in_what;
1404     int in_what_stride = d->pre_stride;
1405     int mv_stride = d->pre_stride;
1406     unsigned char *bestaddress;
1407     int_mv *best_mv = &d->bmi.mv;
1408     int_mv this_mv;
1409     int bestsad = INT_MAX;
1410     int r, c;
1411
1412     unsigned char *check_here;
1413     unsigned int thissad;
1414
1415     int ref_row = ref_mv->as_mv.row;
1416     int ref_col = ref_mv->as_mv.col;
1417
1418     int row_min = ref_row - distance;
1419     int row_max = ref_row + distance;
1420     int col_min = ref_col - distance;
1421     int col_max = ref_col + distance;
1422
1423     unsigned int sad_array[3];
1424
1425     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1426     int_mv fcenter_mv;
1427     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1428     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1429
1430     // Work out the mid point for the search
1431     in_what = *(d->base_pre) + d->pre;
1432     bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1433
1434     best_mv->as_mv.row = ref_row;
1435     best_mv->as_mv.col = ref_col;
1436
1437     // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1438     if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1439     (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1440     {
1441         // Baseline value at the centre
1442         bestsad = fn_ptr->sdf(what, what_stride,
1443                               bestaddress, in_what_stride, 0x7fffffff)
1444                 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1445     }
1446
1447     // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1448     if (col_min < x->mv_col_min)
1449         col_min = x->mv_col_min;
1450
1451     if (col_max > x->mv_col_max)
1452         col_max = x->mv_col_max;
1453
1454     if (row_min < x->mv_row_min)
1455         row_min = x->mv_row_min;
1456
1457     if (row_max > x->mv_row_max)
1458         row_max = x->mv_row_max;
1459
1460     for (r = row_min; r < row_max ; r++)
1461     {
1462         this_mv.as_mv.row = r;
1463         check_here = r * mv_stride + in_what + col_min;
1464         c = col_min;
1465
1466         while ((c + 2) < col_max)
1467         {
1468             int i;
1469
1470             fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1471
1472             for (i = 0; i < 3; i++)
1473             {
1474                 thissad = sad_array[i];
1475
1476                 if (thissad < bestsad)
1477                 {
1478                     this_mv.as_mv.col = c;
1479                     thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1480                                                 mvsadcost, sad_per_bit);
1481
1482                     if (thissad < bestsad)
1483                     {
1484                         bestsad = thissad;
1485                         best_mv->as_mv.row = r;
1486                         best_mv->as_mv.col = c;
1487                         bestaddress = check_here;
1488                     }
1489                 }
1490
1491                 check_here++;
1492                 c++;
1493             }
1494         }
1495
1496         while (c < col_max)
1497         {
1498             thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1499
1500             if (thissad < bestsad)
1501             {
1502                 this_mv.as_mv.col = c;
1503                 thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1504                                             mvsadcost, sad_per_bit);
1505
1506                 if (thissad < bestsad)
1507                 {
1508                     bestsad = thissad;
1509                     best_mv->as_mv.row = r;
1510                     best_mv->as_mv.col = c;
1511                     bestaddress = check_here;
1512                 }
1513             }
1514
1515             check_here ++;
1516             c ++;
1517         }
1518
1519     }
1520
1521     this_mv.as_mv.row = best_mv->as_mv.row << 3;
1522     this_mv.as_mv.col = best_mv->as_mv.col << 3;
1523
1524     if (bestsad < INT_MAX)
1525         return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1526         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1527     else
1528         return INT_MAX;
1529 }
1530
1531 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1532                           int sad_per_bit, int distance,
1533                           vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1534                           int_mv *center_mv)
1535 {
1536     unsigned char *what = (*(b->base_src) + b->src);
1537     int what_stride = b->src_stride;
1538     unsigned char *in_what;
1539     int in_what_stride = d->pre_stride;
1540     int mv_stride = d->pre_stride;
1541     unsigned char *bestaddress;
1542     int_mv *best_mv = &d->bmi.mv;
1543     int_mv this_mv;
1544     int bestsad = INT_MAX;
1545     int r, c;
1546
1547     unsigned char *check_here;
1548     unsigned int thissad;
1549
1550     int ref_row = ref_mv->as_mv.row;
1551     int ref_col = ref_mv->as_mv.col;
1552
1553     int row_min = ref_row - distance;
1554     int row_max = ref_row + distance;
1555     int col_min = ref_col - distance;
1556     int col_max = ref_col + distance;
1557
1558     DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1559     unsigned int sad_array[3];
1560
1561     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1562     int_mv fcenter_mv;
1563     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1564     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1565
1566     // Work out the mid point for the search
1567     in_what = *(d->base_pre) + d->pre;
1568     bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1569
1570     best_mv->as_mv.row = ref_row;
1571     best_mv->as_mv.col = ref_col;
1572
1573     // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1574     if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1575     (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1576     {
1577         // Baseline value at the centre
1578         bestsad = fn_ptr->sdf(what, what_stride,
1579                               bestaddress, in_what_stride, 0x7fffffff)
1580                 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1581     }
1582
1583     // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1584     if (col_min < x->mv_col_min)
1585         col_min = x->mv_col_min;
1586
1587     if (col_max > x->mv_col_max)
1588         col_max = x->mv_col_max;
1589
1590     if (row_min < x->mv_row_min)
1591         row_min = x->mv_row_min;
1592
1593     if (row_max > x->mv_row_max)
1594         row_max = x->mv_row_max;
1595
1596     for (r = row_min; r < row_max ; r++)
1597     {
1598         this_mv.as_mv.row = r;
1599         check_here = r * mv_stride + in_what + col_min;
1600         c = col_min;
1601
1602         while ((c + 7) < col_max)
1603         {
1604             int i;
1605
1606             fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1607
1608             for (i = 0; i < 8; i++)
1609             {
1610                 thissad = (unsigned int)sad_array8[i];
1611
1612                 if (thissad < bestsad)
1613                 {
1614                     this_mv.as_mv.col = c;
1615                     thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1616                                                 mvsadcost, sad_per_bit);
1617
1618                     if (thissad < bestsad)
1619                     {
1620                         bestsad = thissad;
1621                         best_mv->as_mv.row = r;
1622                         best_mv->as_mv.col = c;
1623                         bestaddress = check_here;
1624                     }
1625                 }
1626
1627                 check_here++;
1628                 c++;
1629             }
1630         }
1631
1632         while ((c + 2) < col_max)
1633         {
1634             int i;
1635
1636             fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1637
1638             for (i = 0; i < 3; i++)
1639             {
1640                 thissad = sad_array[i];
1641
1642                 if (thissad < bestsad)
1643                 {
1644                     this_mv.as_mv.col = c;
1645                     thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1646                         mvsadcost, sad_per_bit);
1647
1648                     if (thissad < bestsad)
1649                     {
1650                         bestsad = thissad;
1651                         best_mv->as_mv.row = r;
1652                         best_mv->as_mv.col = c;
1653                         bestaddress = check_here;
1654                     }
1655                 }
1656
1657                 check_here++;
1658                 c++;
1659             }
1660         }
1661
1662         while (c < col_max)
1663         {
1664             thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1665
1666             if (thissad < bestsad)
1667             {
1668                 this_mv.as_mv.col = c;
1669                 thissad  += mvsad_err_cost(&this_mv, &fcenter_mv,
1670                     mvsadcost, sad_per_bit);
1671
1672                 if (thissad < bestsad)
1673                 {
1674                     bestsad = thissad;
1675                     best_mv->as_mv.row = r;
1676                     best_mv->as_mv.col = c;
1677                     bestaddress = check_here;
1678                 }
1679             }
1680
1681             check_here ++;
1682             c ++;
1683         }
1684     }
1685
1686     this_mv.as_mv.row = best_mv->as_mv.row << 3;
1687     this_mv.as_mv.col = best_mv->as_mv.col << 3;
1688
1689     if (bestsad < INT_MAX)
1690         return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1691         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1692     else
1693         return INT_MAX;
1694 }
1695
1696 int vp8_refining_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1697                             int error_per_bit, int search_range,
1698                             vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1699                             int_mv *center_mv)
1700 {
1701     MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1702     int i, j;
1703     short this_row_offset, this_col_offset;
1704
1705     int what_stride = b->src_stride;
1706     int in_what_stride = d->pre_stride;
1707     unsigned char *what = (*(b->base_src) + b->src);
1708     unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1709         (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1710     unsigned char *check_here;
1711     unsigned int thissad;
1712     int_mv this_mv;
1713     unsigned int bestsad = INT_MAX;
1714
1715     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1716     int_mv fcenter_mv;
1717
1718     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1719     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1720
1721     bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1722
1723     for (i=0; i<search_range; i++)
1724     {
1725         int best_site = -1;
1726
1727         for (j = 0 ; j < 4 ; j++)
1728         {
1729             this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1730             this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1731
1732             if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1733             (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1734             {
1735                 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1736                 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1737
1738                 if (thissad < bestsad)
1739                 {
1740                     this_mv.as_mv.row = this_row_offset;
1741                     this_mv.as_mv.col = this_col_offset;
1742                     thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1743
1744                     if (thissad < bestsad)
1745                     {
1746                         bestsad = thissad;
1747                         best_site = j;
1748                     }
1749                 }
1750             }
1751         }
1752
1753         if (best_site == -1)
1754             break;
1755         else
1756         {
1757             ref_mv->as_mv.row += neighbors[best_site].row;
1758             ref_mv->as_mv.col += neighbors[best_site].col;
1759             best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1760         }
1761     }
1762
1763     this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1764     this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1765
1766     if (bestsad < INT_MAX)
1767         return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1768         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1769     else
1770         return INT_MAX;
1771 }
1772
1773 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1774                               int_mv *ref_mv, int error_per_bit,
1775                               int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1776                               int *mvcost[2], int_mv *center_mv)
1777 {
1778     MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1779     int i, j;
1780     short this_row_offset, this_col_offset;
1781
1782     int what_stride = b->src_stride;
1783     int in_what_stride = d->pre_stride;
1784     unsigned char *what = (*(b->base_src) + b->src);
1785     unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1786         (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1787     unsigned char *check_here;
1788     unsigned int thissad;
1789     int_mv this_mv;
1790     unsigned int bestsad = INT_MAX;
1791
1792     int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1793     int_mv fcenter_mv;
1794
1795     fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1796     fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1797
1798     bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1799
1800     for (i=0; i<search_range; i++)
1801     {
1802         int best_site = -1;
1803         int all_in = 1;
1804
1805         all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1806         all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1807         all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1808         all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1809
1810         if(all_in)
1811         {
1812             unsigned int sad_array[4];
1813             unsigned char *block_offset[4];
1814             block_offset[0] = best_address - in_what_stride;
1815             block_offset[1] = best_address - 1;
1816             block_offset[2] = best_address + 1;
1817             block_offset[3] = best_address + in_what_stride;
1818
1819             fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1820
1821             for (j = 0; j < 4; j++)
1822             {
1823                 if (sad_array[j] < bestsad)
1824                 {
1825                     this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1826                     this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1827                     sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1828
1829                     if (sad_array[j] < bestsad)
1830                     {
1831                         bestsad = sad_array[j];
1832                         best_site = j;
1833                     }
1834                 }
1835             }
1836         }
1837         else
1838         {
1839             for (j = 0 ; j < 4 ; j++)
1840             {
1841                 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1842                 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1843
1844                 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1845                 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1846                 {
1847                     check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1848                     thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1849
1850                     if (thissad < bestsad)
1851                     {
1852                         this_mv.as_mv.row = this_row_offset;
1853                         this_mv.as_mv.col = this_col_offset;
1854                         thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1855
1856                         if (thissad < bestsad)
1857                         {
1858                             bestsad = thissad;
1859                             best_site = j;
1860                         }
1861                     }
1862                 }
1863             }
1864         }
1865
1866         if (best_site == -1)
1867             break;
1868         else
1869         {
1870             ref_mv->as_mv.row += neighbors[best_site].row;
1871             ref_mv->as_mv.col += neighbors[best_site].col;
1872             best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1873         }
1874     }
1875
1876     this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1877     this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1878
1879     if (bestsad < INT_MAX)
1880         return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1881         + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1882     else
1883         return INT_MAX;
1884 }
1885
1886 #ifdef ENTROPY_STATS
1887 void print_mode_context(void)
1888 {
1889     FILE *f = fopen("modecont.c", "w");
1890     int i, j;
1891
1892     fprintf(f, "#include \"entropy.h\"\n");
1893     fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1894     fprintf(f, "{\n");
1895
1896     for (j = 0; j < 6; j++)
1897     {
1898         fprintf(f, "  { // %d \n", j);
1899         fprintf(f, "    ");
1900
1901         for (i = 0; i < 4; i++)
1902         {
1903             int overal_prob;
1904             int this_prob;
1905             int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1906
1907             // Overall probs
1908             count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1909
1910             if (count)
1911                 overal_prob = 256 * mv_mode_cts[i][0] / count;
1912             else
1913                 overal_prob = 128;
1914
1915             if (overal_prob == 0)
1916                 overal_prob = 1;
1917
1918             // context probs
1919             count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1920
1921             if (count)
1922                 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1923             else
1924                 this_prob = 128;
1925
1926             if (this_prob == 0)
1927                 this_prob = 1;
1928
1929             fprintf(f, "%5d, ", this_prob);
1930             //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1931             //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1932         }
1933
1934         fprintf(f, "  },\n");
1935     }
1936
1937     fprintf(f, "};\n");
1938     fclose(f);
1939 }
1940
1941 /* MV ref count ENTROPY_STATS stats code */
1942 #ifdef ENTROPY_STATS
1943 void init_mv_ref_counts()
1944 {
1945     vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1946     vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1947 }
1948
1949 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1950 {
1951     if (m == ZEROMV)
1952     {
1953         ++mv_ref_ct [ct[0]] [0] [0];
1954         ++mv_mode_cts[0][0];
1955     }
1956     else
1957     {
1958         ++mv_ref_ct [ct[0]] [0] [1];
1959         ++mv_mode_cts[0][1];
1960
1961         if (m == NEARESTMV)
1962         {
1963             ++mv_ref_ct [ct[1]] [1] [0];
1964             ++mv_mode_cts[1][0];
1965         }
1966         else
1967         {
1968             ++mv_ref_ct [ct[1]] [1] [1];
1969             ++mv_mode_cts[1][1];
1970
1971             if (m == NEARMV)
1972             {
1973                 ++mv_ref_ct [ct[2]] [2] [0];
1974                 ++mv_mode_cts[2][0];
1975             }
1976             else
1977             {
1978                 ++mv_ref_ct [ct[2]] [2] [1];
1979                 ++mv_mode_cts[2][1];
1980
1981                 if (m == NEWMV)
1982                 {
1983                     ++mv_ref_ct [ct[3]] [3] [0];
1984                     ++mv_mode_cts[3][0];
1985                 }
1986                 else
1987                 {
1988                     ++mv_ref_ct [ct[3]] [3] [1];
1989                     ++mv_mode_cts[3][1];
1990                 }
1991             }
1992         }
1993     }
1994 }
1995
1996 #endif/* END MV ref count ENTROPY_STATS stats code */
1997
1998 #endif