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