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