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