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