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