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