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