2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "vpx_mem/vpx_mem.h"
20 static int mv_ref_ct [31] [4] [2];
21 static int mv_mode_cts [4] [2];
24 int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
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;
33 static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit)
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;
40 static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
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;
48 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
51 int search_site_count = 0;
54 // Generate offsets for 4 search sites per 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;
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;
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;
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;
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;
92 x->ss_count = search_site_count;
93 x->searches_per_step = 4;
96 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
99 int search_site_count = 0;
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;
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;
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;
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;
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;
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;
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;
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;
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;
164 x->ss_count = search_site_count;
165 x->searches_per_step = 8;
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))
179 //#define CHECK_BETTER(v,r,c) if((v = ERR(r,c)) < besterr) { besterr = v; br=r; bc=c; }
181 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
182 int_mv *bestmv, int_mv *ref_mv,
184 const vp8_variance_fn_ptr_t *vfp,
185 int *mvcost[2], int *distortion,
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);
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;
197 unsigned int whichdir;
198 unsigned int halfiters = 4;
199 unsigned int quarteriters = 4;
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));
208 bestmv->as_mv.row <<= 3;
209 bestmv->as_mv.col <<= 3;
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);
216 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
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);
225 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
230 CHECK_BETTER(diag, tr - 2, tc - 2);
233 CHECK_BETTER(diag, tr - 2, tc + 2);
236 CHECK_BETTER(diag, tr + 2, tc - 2);
239 CHECK_BETTER(diag, tr + 2, tc + 2);
243 // no reason to check the same one again.
244 if (tr == br && tc == bc)
251 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
253 while (--quarteriters)
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);
260 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
265 CHECK_BETTER(diag, tr - 1, tc - 1);
268 CHECK_BETTER(diag, tr - 1, tc + 1);
271 CHECK_BETTER(diag, tr + 1, tc - 1);
274 CHECK_BETTER(diag, tr + 1, tc + 1);
278 // no reason to check the same one again.
279 if (tr == br && tc == bc)
286 bestmv->as_mv.row = br << 1;
287 bestmv->as_mv.col = bc << 1;
289 if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL) ||
290 (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
303 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
304 int_mv *bestmv, int_mv *ref_mv,
306 const vp8_variance_fn_ptr_t *vfp,
307 int *mvcost[2], int *distortion,
310 int bestmse = INT_MAX;
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;
321 // Trap uncodable vectors
322 if ((abs((bestmv->as_mv.col << 3) - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL)
323 || (abs((bestmv->as_mv.row << 3) - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
325 bestmv->as_mv.row <<= 3;
326 bestmv->as_mv.col <<= 3;
327 *distortion = INT_MAX;
332 bestmv->as_mv.row <<= 3;
333 bestmv->as_mv.col <<= 3;
336 // calculate central point error
337 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
338 *distortion = bestmse;
339 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
341 // go left then right and check error
342 this_mv.as_mv.row = startmv.as_mv.row;
343 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
344 thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
345 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
351 *distortion = thismse;
355 this_mv.as_mv.col += 8;
356 thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
357 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
363 *distortion = thismse;
367 // go up then down and check error
368 this_mv.as_mv.col = startmv.as_mv.col;
369 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
370 thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
371 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
377 *distortion = thismse;
381 this_mv.as_mv.row += 8;
382 thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
383 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
389 *distortion = thismse;
394 // now check 1 more diagonal
395 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
396 //for(whichdir =0;whichdir<4;whichdir++)
403 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
404 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
405 thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
408 this_mv.as_mv.col += 4;
409 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
410 thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
413 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
414 this_mv.as_mv.row += 4;
415 thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
419 this_mv.as_mv.col += 4;
420 this_mv.as_mv.row += 4;
421 thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
425 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
431 *distortion = thismse;
438 // time to check quarter pels.
439 if (bestmv->as_mv.row < startmv.as_mv.row)
442 if (bestmv->as_mv.col < startmv.as_mv.col)
449 // go left then right and check error
450 this_mv.as_mv.row = startmv.as_mv.row;
452 if (startmv.as_mv.col & 7)
454 this_mv.as_mv.col = startmv.as_mv.col - 2;
455 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
459 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
460 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
463 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
469 *distortion = thismse;
473 this_mv.as_mv.col += 4;
474 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
475 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
481 *distortion = thismse;
485 // go up then down and check error
486 this_mv.as_mv.col = startmv.as_mv.col;
488 if (startmv.as_mv.row & 7)
490 this_mv.as_mv.row = startmv.as_mv.row - 2;
491 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
495 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
496 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
499 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
505 *distortion = thismse;
509 this_mv.as_mv.row += 4;
510 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
511 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
517 *distortion = thismse;
522 // now check 1 more diagonal
523 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
525 // for(whichdir=0;whichdir<4;whichdir++)
533 if (startmv.as_mv.row & 7)
535 this_mv.as_mv.row -= 2;
537 if (startmv.as_mv.col & 7)
539 this_mv.as_mv.col -= 2;
540 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
544 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
545 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
550 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
552 if (startmv.as_mv.col & 7)
554 this_mv.as_mv.col -= 2;
555 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
559 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
560 thismse = vfp->svf(y - d->pre_stride - 1, d->pre_stride, 6, 6, z, b->src_stride, &sse);
566 this_mv.as_mv.col += 2;
568 if (startmv.as_mv.row & 7)
570 this_mv.as_mv.row -= 2;
571 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
575 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
576 thismse = vfp->svf(y - d->pre_stride, d->pre_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
581 this_mv.as_mv.row += 2;
583 if (startmv.as_mv.col & 7)
585 this_mv.as_mv.col -= 2;
586 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
590 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
591 thismse = vfp->svf(y - 1, d->pre_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
596 this_mv.as_mv.col += 2;
597 this_mv.as_mv.row += 2;
598 thismse = vfp->svf(y, d->pre_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
602 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
608 *distortion = thismse;
615 int vp8_find_best_half_pixel_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
616 int_mv *bestmv, int_mv *ref_mv,
618 const vp8_variance_fn_ptr_t *vfp,
619 int *mvcost[2], int *distortion,
622 int bestmse = INT_MAX;
625 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
626 unsigned char *z = (*(b->base_src) + b->src);
627 int left, right, up, down, diag;
631 // Trap uncodable vectors
632 if ((abs((bestmv->as_mv.col << 3) - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL)
633 || (abs((bestmv->as_mv.row << 3) - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL))
635 bestmv->as_mv.row <<= 3;
636 bestmv->as_mv.col <<= 3;
637 *distortion = INT_MAX;
642 bestmv->as_mv.row <<= 3;
643 bestmv->as_mv.col <<= 3;
646 // calculate central point error
647 bestmse = vfp->vf(y, d->pre_stride, z, b->src_stride, sse1);
648 *distortion = bestmse;
649 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
651 // go left then right and check error
652 this_mv.as_mv.row = startmv.as_mv.row;
653 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
654 thismse = vfp->svf_halfpix_h(y - 1, d->pre_stride, z, b->src_stride, &sse);
655 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
661 *distortion = thismse;
665 this_mv.as_mv.col += 8;
666 thismse = vfp->svf_halfpix_h(y, d->pre_stride, z, b->src_stride, &sse);
667 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
673 *distortion = thismse;
677 // go up then down and check error
678 this_mv.as_mv.col = startmv.as_mv.col;
679 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
680 thismse = vfp->svf_halfpix_v(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
681 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
687 *distortion = thismse;
691 this_mv.as_mv.row += 8;
692 thismse = vfp->svf_halfpix_v(y, d->pre_stride, z, b->src_stride, &sse);
693 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
699 *distortion = thismse;
703 // somewhat strangely not doing all the diagonals for half pel is slower than doing them.
705 // now check 1 more diagonal -
706 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
712 this_mv.col = (this_mv.col - 8) | 4;
713 this_mv.row = (this_mv.row - 8) | 4;
714 diag = vfp->svf(y - 1 - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
718 this_mv.row = (this_mv.row - 8) | 4;
719 diag = vfp->svf(y - d->pre_stride, d->pre_stride, 4, 4, z, b->src_stride, &sse);
722 this_mv.col = (this_mv.col - 8) | 4;
724 diag = vfp->svf(y - 1, d->pre_stride, 4, 4, z, b->src_stride, &sse);
729 diag = vfp->svf(y, d->pre_stride, 4, 4, z, b->src_stride, &sse);
733 diag += mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
742 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
743 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
744 thismse = vfp->svf_halfpix_hv(y - 1 - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
745 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
751 *distortion = thismse;
755 this_mv.as_mv.col += 8;
756 thismse = vfp->svf_halfpix_hv(y - d->pre_stride, d->pre_stride, z, b->src_stride, &sse);
757 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
763 *distortion = thismse;
767 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
768 this_mv.as_mv.row = startmv.as_mv.row + 4;
769 thismse = vfp->svf_halfpix_hv(y - 1, d->pre_stride, z, b->src_stride, &sse);
770 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
776 *distortion = thismse;
780 this_mv.as_mv.col += 8;
781 thismse = vfp->svf_halfpix_hv(y, d->pre_stride, z, b->src_stride, &sse);
782 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
788 *distortion = thismse;
796 #define CHECK_BOUNDS(range) \
799 all_in &= ((br-range) >= x->mv_row_min);\
800 all_in &= ((br+range) <= x->mv_row_max);\
801 all_in &= ((bc-range) >= x->mv_col_min);\
802 all_in &= ((bc+range) <= x->mv_col_max);\
805 #define CHECK_POINT \
807 if (this_mv.as_mv.col < x->mv_col_min) continue;\
808 if (this_mv.as_mv.col > x->mv_col_max) continue;\
809 if (this_mv.as_mv.row < x->mv_row_min) continue;\
810 if (this_mv.as_mv.row > x->mv_row_max) continue;\
813 #define CHECK_BETTER \
815 if (thissad < bestsad)\
817 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
818 if (thissad < bestsad)\
826 static const MV next_chkpts[6][3] =
828 {{ -2, 0}, { -1, -2}, {1, -2}},
829 {{ -1, -2}, {1, -2}, {2, 0}},
830 {{1, -2}, {2, 0}, {1, 2}},
831 {{2, 0}, {1, 2}, { -1, 2}},
832 {{1, 2}, { -1, 2}, { -2, 0}},
833 {{ -1, 2}, { -2, 0}, { -1, -2}}
845 const vp8_variance_fn_ptr_t *vfp,
851 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
852 MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
855 unsigned char *what = (*(b->base_src) + b->src);
856 int what_stride = b->src_stride;
857 int in_what_stride = d->pre_stride;
858 int br = ref_mv->as_mv.row >> 3, bc = ref_mv->as_mv.col >> 3;
860 unsigned int bestsad = 0x7fffffff;
861 unsigned int thissad;
862 unsigned char *base_offset;
863 unsigned char *this_offset;
869 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
870 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
872 // Work out the start point for the search
873 base_offset = (unsigned char *)(*(d->base_pre) + d->pre);
874 this_offset = base_offset + (br * (d->pre_stride)) + bc;
875 this_mv.as_mv.row = br;
876 this_mv.as_mv.col = bc;
877 bestsad = vfp->sdf( what, what_stride, this_offset,
878 in_what_stride, 0x7fffffff)
879 + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
887 for (i = 0; i < 6; i++)
889 this_mv.as_mv.row = br + hex[i].row;
890 this_mv.as_mv.col = bc + hex[i].col;
891 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
892 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
897 for (i = 0; i < 6; i++)
899 this_mv.as_mv.row = br + hex[i].row;
900 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);
912 br += hex[best_site].row;
913 bc += hex[best_site].col;
917 for (j = 1; j < 127; j++)
924 for (i = 0; i < 3; i++)
926 this_mv.as_mv.row = br + next_chkpts[k][i].row;
927 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
928 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
929 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
934 for (i = 0; i < 3; i++)
936 this_mv.as_mv.row = br + next_chkpts[k][i].row;
937 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);
949 br += next_chkpts[k][best_site].row;
950 bc += next_chkpts[k][best_site].col;
952 if (k >= 12) k -= 12;
953 else if (k >= 6) k -= 6;
957 // check 4 1-away neighbors
959 for (j = 0; j < 32; j++)
966 for (i = 0; i < 4; i++)
968 this_mv.as_mv.row = br + neighbors[i].row;
969 this_mv.as_mv.col = bc + neighbors[i].col;
970 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
971 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
976 for (i = 0; i < 4; i++)
978 this_mv.as_mv.row = br + neighbors[i].row;
979 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);
991 br += neighbors[best_site].row;
992 bc += neighbors[best_site].col;
996 best_mv->as_mv.row = br;
997 best_mv->as_mv.col = bc;
1005 int vp8_diamond_search_sad
1015 vp8_variance_fn_ptr_t *fn_ptr,
1022 unsigned char *what = (*(b->base_src) + b->src);
1023 int what_stride = b->src_stride;
1024 unsigned char *in_what;
1025 int in_what_stride = d->pre_stride;
1026 unsigned char *best_address;
1031 int bestsad = INT_MAX;
1035 int ref_row = ref_mv->as_mv.row >> 3;
1036 int ref_col = ref_mv->as_mv.col >> 3;
1037 int this_row_offset;
1038 int this_col_offset;
1041 unsigned char *check_here;
1044 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1046 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1047 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1051 best_mv->as_mv.row = ref_row;
1052 best_mv->as_mv.col = ref_col;
1054 // Work out the start point for the search
1055 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1056 best_address = in_what;
1058 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1059 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1060 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1062 // Check the starting position
1063 bestsad = fn_ptr->sdf(what, what_stride, in_what,
1064 in_what_stride, 0x7fffffff)
1065 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1068 // search_param determines the length of the initial step and hence the number of iterations
1069 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1070 ss = &x->ss[search_param * x->searches_per_step];
1071 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1075 for (step = 0; step < tot_steps ; step++)
1077 for (j = 0 ; j < x->searches_per_step ; j++)
1079 // Trap illegal vectors
1080 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1081 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1083 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1084 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1087 check_here = ss[i].offset + best_address;
1088 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1090 if (thissad < bestsad)
1092 this_mv.as_mv.row = this_row_offset;
1093 this_mv.as_mv.col = this_col_offset;
1094 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1095 mvsadcost, sad_per_bit);
1097 if (thissad < bestsad)
1108 if (best_site != last_site)
1110 best_mv->as_mv.row += ss[best_site].mv.row;
1111 best_mv->as_mv.col += ss[best_site].mv.col;
1112 best_address += ss[best_site].offset;
1113 last_site = best_site;
1115 else if (best_address == in_what)
1119 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1120 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1122 if (bestsad == INT_MAX)
1125 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1126 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1129 int vp8_diamond_search_sadx4
1139 vp8_variance_fn_ptr_t *fn_ptr,
1146 unsigned char *what = (*(b->base_src) + b->src);
1147 int what_stride = b->src_stride;
1148 unsigned char *in_what;
1149 int in_what_stride = d->pre_stride;
1150 unsigned char *best_address;
1155 int bestsad = INT_MAX;
1159 int ref_row = ref_mv->as_mv.row >> 3;
1160 int ref_col = ref_mv->as_mv.col >> 3;
1161 int this_row_offset;
1162 int this_col_offset;
1165 unsigned char *check_here;
1166 unsigned int thissad;
1168 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1170 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1171 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1174 best_mv->as_mv.row = ref_row;
1175 best_mv->as_mv.col = ref_col;
1177 // Work out the start point for the search
1178 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1179 best_address = in_what;
1181 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1182 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1183 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1185 // Check the starting position
1186 bestsad = fn_ptr->sdf(what, what_stride,
1187 in_what, in_what_stride, 0x7fffffff)
1188 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1191 // search_param determines the length of the initial step and hence the number of iterations
1192 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1193 ss = &x->ss[search_param * x->searches_per_step];
1194 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1198 for (step = 0; step < tot_steps ; step++)
1202 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1203 // checking 4 bounds for each points.
1204 all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
1205 all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
1206 all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
1207 all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
1211 unsigned int sad_array[4];
1213 for (j = 0 ; j < x->searches_per_step ; j += 4)
1215 unsigned char *block_offset[4];
1217 for (t = 0; t < 4; t++)
1218 block_offset[t] = ss[i+t].offset + best_address;
1220 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1222 for (t = 0; t < 4; t++, i++)
1224 if (sad_array[t] < bestsad)
1226 this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1227 this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1228 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
1229 mvsadcost, sad_per_bit);
1231 if (sad_array[t] < bestsad)
1233 bestsad = sad_array[t];
1242 for (j = 0 ; j < x->searches_per_step ; j++)
1244 // Trap illegal vectors
1245 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1246 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1248 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1249 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1251 check_here = ss[i].offset + best_address;
1252 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1254 if (thissad < bestsad)
1256 this_mv.as_mv.row = this_row_offset;
1257 this_mv.as_mv.col = this_col_offset;
1258 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1259 mvsadcost, sad_per_bit);
1261 if (thissad < bestsad)
1272 if (best_site != last_site)
1274 best_mv->as_mv.row += ss[best_site].mv.row;
1275 best_mv->as_mv.col += ss[best_site].mv.col;
1276 best_address += ss[best_site].offset;
1277 last_site = best_site;
1279 else if (best_address == in_what)
1283 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1284 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1286 if (bestsad == INT_MAX)
1289 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1290 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1293 int vp8_full_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1294 int sad_per_bit, int distance,
1295 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1298 unsigned char *what = (*(b->base_src) + b->src);
1299 int what_stride = b->src_stride;
1300 unsigned char *in_what;
1301 int in_what_stride = d->pre_stride;
1302 int mv_stride = d->pre_stride;
1303 unsigned char *bestaddress;
1304 int_mv *best_mv = &d->bmi.mv;
1306 int bestsad = INT_MAX;
1309 unsigned char *check_here;
1312 int ref_row = ref_mv->as_mv.row;
1313 int ref_col = ref_mv->as_mv.col;
1315 int row_min = ref_row - distance;
1316 int row_max = ref_row + distance;
1317 int col_min = ref_col - distance;
1318 int col_max = ref_col + distance;
1320 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1322 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1323 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1325 // Work out the mid point for the search
1326 in_what = *(d->base_pre) + d->pre;
1327 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1329 best_mv->as_mv.row = ref_row;
1330 best_mv->as_mv.col = ref_col;
1332 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1333 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1334 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1336 // Baseline value at the centre
1338 //bestsad = fn_ptr->sf( what,what_stride,bestaddress,in_what_stride) + (int)sqrt(mv_err_cost(ref_mv,ref_mv, mvcost,error_per_bit*14));
1339 bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
1340 in_what_stride, 0x7fffffff)
1341 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1344 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1345 if (col_min < x->mv_col_min)
1346 col_min = x->mv_col_min;
1348 if (col_max > x->mv_col_max)
1349 col_max = x->mv_col_max;
1351 if (row_min < x->mv_row_min)
1352 row_min = x->mv_row_min;
1354 if (row_max > x->mv_row_max)
1355 row_max = x->mv_row_max;
1357 for (r = row_min; r < row_max ; r++)
1359 this_mv.as_mv.row = r;
1360 check_here = r * mv_stride + in_what + col_min;
1362 for (c = col_min; c < col_max; c++)
1364 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1366 this_mv.as_mv.col = c;
1367 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1368 mvsadcost, sad_per_bit);
1370 if (thissad < bestsad)
1373 best_mv->as_mv.row = r;
1374 best_mv->as_mv.col = c;
1375 bestaddress = check_here;
1382 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1383 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1385 if (bestsad < INT_MAX)
1386 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1387 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1392 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1393 int sad_per_bit, int distance,
1394 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1397 unsigned char *what = (*(b->base_src) + b->src);
1398 int what_stride = b->src_stride;
1399 unsigned char *in_what;
1400 int in_what_stride = d->pre_stride;
1401 int mv_stride = d->pre_stride;
1402 unsigned char *bestaddress;
1403 int_mv *best_mv = &d->bmi.mv;
1405 int bestsad = INT_MAX;
1408 unsigned char *check_here;
1409 unsigned int thissad;
1411 int ref_row = ref_mv->as_mv.row;
1412 int ref_col = ref_mv->as_mv.col;
1414 int row_min = ref_row - distance;
1415 int row_max = ref_row + distance;
1416 int col_min = ref_col - distance;
1417 int col_max = ref_col + distance;
1419 unsigned int sad_array[3];
1421 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1423 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1424 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1426 // Work out the mid point for the search
1427 in_what = *(d->base_pre) + d->pre;
1428 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1430 best_mv->as_mv.row = ref_row;
1431 best_mv->as_mv.col = ref_col;
1433 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1434 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1435 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1437 // Baseline value at the centre
1438 bestsad = fn_ptr->sdf(what, what_stride,
1439 bestaddress, in_what_stride, 0x7fffffff)
1440 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1443 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1444 if (col_min < x->mv_col_min)
1445 col_min = x->mv_col_min;
1447 if (col_max > x->mv_col_max)
1448 col_max = x->mv_col_max;
1450 if (row_min < x->mv_row_min)
1451 row_min = x->mv_row_min;
1453 if (row_max > x->mv_row_max)
1454 row_max = x->mv_row_max;
1456 for (r = row_min; r < row_max ; r++)
1458 this_mv.as_mv.row = r;
1459 check_here = r * mv_stride + in_what + col_min;
1462 while ((c + 2) < col_max)
1466 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1468 for (i = 0; i < 3; i++)
1470 thissad = sad_array[i];
1472 if (thissad < bestsad)
1474 this_mv.as_mv.col = c;
1475 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1476 mvsadcost, sad_per_bit);
1478 if (thissad < bestsad)
1481 best_mv->as_mv.row = r;
1482 best_mv->as_mv.col = c;
1483 bestaddress = check_here;
1494 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1496 if (thissad < bestsad)
1498 this_mv.as_mv.col = c;
1499 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1500 mvsadcost, sad_per_bit);
1502 if (thissad < bestsad)
1505 best_mv->as_mv.row = r;
1506 best_mv->as_mv.col = c;
1507 bestaddress = check_here;
1517 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1518 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1520 if (bestsad < INT_MAX)
1521 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1522 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1527 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1528 int sad_per_bit, int distance,
1529 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1532 unsigned char *what = (*(b->base_src) + b->src);
1533 int what_stride = b->src_stride;
1534 unsigned char *in_what;
1535 int in_what_stride = d->pre_stride;
1536 int mv_stride = d->pre_stride;
1537 unsigned char *bestaddress;
1538 int_mv *best_mv = &d->bmi.mv;
1540 int bestsad = INT_MAX;
1543 unsigned char *check_here;
1544 unsigned int thissad;
1546 int ref_row = ref_mv->as_mv.row;
1547 int ref_col = ref_mv->as_mv.col;
1549 int row_min = ref_row - distance;
1550 int row_max = ref_row + distance;
1551 int col_min = ref_col - distance;
1552 int col_max = ref_col + distance;
1554 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1555 unsigned int sad_array[3];
1557 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1559 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1560 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1562 // Work out the mid point for the search
1563 in_what = *(d->base_pre) + d->pre;
1564 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1566 best_mv->as_mv.row = ref_row;
1567 best_mv->as_mv.col = ref_col;
1569 // We need to check that the starting point for the search (as indicated by ref_mv) is within the buffer limits
1570 if ((ref_col > x->mv_col_min) && (ref_col < x->mv_col_max) &&
1571 (ref_row > x->mv_row_min) && (ref_row < x->mv_row_max))
1573 // Baseline value at the centre
1574 bestsad = fn_ptr->sdf(what, what_stride,
1575 bestaddress, in_what_stride, 0x7fffffff)
1576 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1579 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1580 if (col_min < x->mv_col_min)
1581 col_min = x->mv_col_min;
1583 if (col_max > x->mv_col_max)
1584 col_max = x->mv_col_max;
1586 if (row_min < x->mv_row_min)
1587 row_min = x->mv_row_min;
1589 if (row_max > x->mv_row_max)
1590 row_max = x->mv_row_max;
1592 for (r = row_min; r < row_max ; r++)
1594 this_mv.as_mv.row = r;
1595 check_here = r * mv_stride + in_what + col_min;
1598 while ((c + 7) < col_max)
1602 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1604 for (i = 0; i < 8; i++)
1606 thissad = (unsigned int)sad_array8[i];
1608 if (thissad < bestsad)
1610 this_mv.as_mv.col = c;
1611 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1612 mvsadcost, sad_per_bit);
1614 if (thissad < bestsad)
1617 best_mv->as_mv.row = r;
1618 best_mv->as_mv.col = c;
1619 bestaddress = check_here;
1628 while ((c + 2) < col_max)
1632 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1634 for (i = 0; i < 3; i++)
1636 thissad = sad_array[i];
1638 if (thissad < bestsad)
1640 this_mv.as_mv.col = c;
1641 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1642 mvsadcost, sad_per_bit);
1644 if (thissad < bestsad)
1647 best_mv->as_mv.row = r;
1648 best_mv->as_mv.col = c;
1649 bestaddress = check_here;
1660 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1662 if (thissad < bestsad)
1664 this_mv.as_mv.col = c;
1665 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1666 mvsadcost, sad_per_bit);
1668 if (thissad < bestsad)
1671 best_mv->as_mv.row = r;
1672 best_mv->as_mv.col = c;
1673 bestaddress = check_here;
1682 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1683 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1685 if (bestsad < INT_MAX)
1686 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1687 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1692 int vp8_refining_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1693 int error_per_bit, int search_range,
1694 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1697 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1699 short this_row_offset, this_col_offset;
1701 int what_stride = b->src_stride;
1702 int in_what_stride = d->pre_stride;
1703 unsigned char *what = (*(b->base_src) + b->src);
1704 unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1705 (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1706 unsigned char *check_here;
1707 unsigned int thissad;
1709 unsigned int bestsad = INT_MAX;
1711 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1714 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1715 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1717 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1719 for (i=0; i<search_range; i++)
1723 for (j = 0 ; j < 4 ; j++)
1725 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1726 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1728 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1729 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1731 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1732 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1734 if (thissad < bestsad)
1736 this_mv.as_mv.row = this_row_offset;
1737 this_mv.as_mv.col = this_col_offset;
1738 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1740 if (thissad < bestsad)
1749 if (best_site == -1)
1753 ref_mv->as_mv.row += neighbors[best_site].row;
1754 ref_mv->as_mv.col += neighbors[best_site].col;
1755 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1759 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1760 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1762 if (bestsad < INT_MAX)
1763 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1764 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1769 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1770 int_mv *ref_mv, int error_per_bit,
1771 int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1772 int *mvcost[2], int_mv *center_mv)
1774 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1776 short this_row_offset, this_col_offset;
1778 int what_stride = b->src_stride;
1779 int in_what_stride = d->pre_stride;
1780 unsigned char *what = (*(b->base_src) + b->src);
1781 unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1782 (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1783 unsigned char *check_here;
1784 unsigned int thissad;
1786 unsigned int bestsad = INT_MAX;
1788 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1791 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1792 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1794 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1796 for (i=0; i<search_range; i++)
1801 all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1802 all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1803 all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1804 all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1808 unsigned int sad_array[4];
1809 unsigned char *block_offset[4];
1810 block_offset[0] = best_address - in_what_stride;
1811 block_offset[1] = best_address - 1;
1812 block_offset[2] = best_address + 1;
1813 block_offset[3] = best_address + in_what_stride;
1815 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1817 for (j = 0; j < 4; j++)
1819 if (sad_array[j] < bestsad)
1821 this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1822 this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1823 sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1825 if (sad_array[j] < bestsad)
1827 bestsad = sad_array[j];
1835 for (j = 0 ; j < 4 ; j++)
1837 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1838 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1840 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1841 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1843 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1844 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1846 if (thissad < bestsad)
1848 this_mv.as_mv.row = this_row_offset;
1849 this_mv.as_mv.col = this_col_offset;
1850 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1852 if (thissad < bestsad)
1862 if (best_site == -1)
1866 ref_mv->as_mv.row += neighbors[best_site].row;
1867 ref_mv->as_mv.col += neighbors[best_site].col;
1868 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1872 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1873 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1875 if (bestsad < INT_MAX)
1876 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1877 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1882 #ifdef ENTROPY_STATS
1883 void print_mode_context(void)
1885 FILE *f = fopen("modecont.c", "w");
1888 fprintf(f, "#include \"entropy.h\"\n");
1889 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1892 for (j = 0; j < 6; j++)
1894 fprintf(f, " { // %d \n", j);
1897 for (i = 0; i < 4; i++)
1901 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1904 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1907 overal_prob = 256 * mv_mode_cts[i][0] / count;
1911 if (overal_prob == 0)
1915 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1918 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1925 fprintf(f, "%5d, ", this_prob);
1926 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1927 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1930 fprintf(f, " },\n");
1937 /* MV ref count ENTROPY_STATS stats code */
1938 #ifdef ENTROPY_STATS
1939 void init_mv_ref_counts()
1941 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1942 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1945 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1949 ++mv_ref_ct [ct[0]] [0] [0];
1950 ++mv_mode_cts[0][0];
1954 ++mv_ref_ct [ct[0]] [0] [1];
1955 ++mv_mode_cts[0][1];
1959 ++mv_ref_ct [ct[1]] [1] [0];
1960 ++mv_mode_cts[1][0];
1964 ++mv_ref_ct [ct[1]] [1] [1];
1965 ++mv_mode_cts[1][1];
1969 ++mv_ref_ct [ct[2]] [2] [0];
1970 ++mv_mode_cts[2][0];
1974 ++mv_ref_ct [ct[2]] [2] [1];
1975 ++mv_mode_cts[2][1];
1979 ++mv_ref_ct [ct[3]] [3] [0];
1980 ++mv_mode_cts[3][0];
1984 ++mv_ref_ct [ct[3]] [3] [1];
1985 ++mv_mode_cts[3][1];
1992 #endif/* END MV ref count ENTROPY_STATS stats code */