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