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"
14 #include "vpx_ports/config.h"
18 #include "vp8/common/findnearmv.h"
21 static int mv_ref_ct [31] [4] [2];
22 static int mv_mode_cts [4] [2];
25 int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
27 // MV costing is based on the distribution of vectors in the previous frame and as such will tend to
28 // over state the cost of vectors. In addition coding a new vector can have a knock on effect on the
29 // cost of subsequent vectors and the quality of prediction from NEAR and NEAREST for subsequent blocks.
30 // The "Weight" parameter allows, to a limited extent, for some account to be taken of these factors.
31 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;
34 static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit)
36 return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] +
37 mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1])
38 * error_per_bit + 128) >> 8;
41 static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
43 /* Calculate sad error cost on full pixel basis. */
44 return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
45 mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)])
46 * error_per_bit + 128) >> 8;
49 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
52 int search_site_count = 0;
55 // Generate offsets for 4 search sites per step.
57 x->ss[search_site_count].mv.col = 0;
58 x->ss[search_site_count].mv.row = 0;
59 x->ss[search_site_count].offset = 0;
65 // Compute offsets for search sites.
66 x->ss[search_site_count].mv.col = 0;
67 x->ss[search_site_count].mv.row = -Len;
68 x->ss[search_site_count].offset = -Len * stride;
71 // Compute offsets for search sites.
72 x->ss[search_site_count].mv.col = 0;
73 x->ss[search_site_count].mv.row = Len;
74 x->ss[search_site_count].offset = Len * stride;
77 // Compute offsets for search sites.
78 x->ss[search_site_count].mv.col = -Len;
79 x->ss[search_site_count].mv.row = 0;
80 x->ss[search_site_count].offset = -Len;
83 // Compute offsets for search sites.
84 x->ss[search_site_count].mv.col = Len;
85 x->ss[search_site_count].mv.row = 0;
86 x->ss[search_site_count].offset = Len;
93 x->ss_count = search_site_count;
94 x->searches_per_step = 4;
97 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
100 int search_site_count = 0;
102 // Generate offsets for 8 search sites per step.
103 Len = MAX_FIRST_STEP;
104 x->ss[search_site_count].mv.col = 0;
105 x->ss[search_site_count].mv.row = 0;
106 x->ss[search_site_count].offset = 0;
112 // Compute offsets for search sites.
113 x->ss[search_site_count].mv.col = 0;
114 x->ss[search_site_count].mv.row = -Len;
115 x->ss[search_site_count].offset = -Len * stride;
118 // Compute offsets for search sites.
119 x->ss[search_site_count].mv.col = 0;
120 x->ss[search_site_count].mv.row = Len;
121 x->ss[search_site_count].offset = Len * stride;
124 // Compute offsets for search sites.
125 x->ss[search_site_count].mv.col = -Len;
126 x->ss[search_site_count].mv.row = 0;
127 x->ss[search_site_count].offset = -Len;
130 // Compute offsets for search sites.
131 x->ss[search_site_count].mv.col = Len;
132 x->ss[search_site_count].mv.row = 0;
133 x->ss[search_site_count].offset = Len;
136 // Compute offsets for search sites.
137 x->ss[search_site_count].mv.col = -Len;
138 x->ss[search_site_count].mv.row = -Len;
139 x->ss[search_site_count].offset = -Len * stride - Len;
142 // Compute offsets for search sites.
143 x->ss[search_site_count].mv.col = Len;
144 x->ss[search_site_count].mv.row = -Len;
145 x->ss[search_site_count].offset = -Len * stride + Len;
148 // Compute offsets for search sites.
149 x->ss[search_site_count].mv.col = -Len;
150 x->ss[search_site_count].mv.row = Len;
151 x->ss[search_site_count].offset = Len * stride - Len;
154 // Compute offsets for search sites.
155 x->ss[search_site_count].mv.col = Len;
156 x->ss[search_site_count].mv.row = Len;
157 x->ss[search_site_count].offset = Len * stride + Len;
165 x->ss_count = search_site_count;
166 x->searches_per_step = 8;
170 * To avoid the penalty for crossing cache-line read, preload the reference
171 * area in a small buffer, which is aligned to make sure there won't be crossing
172 * cache-line read while reading from this buffer. This reduced the cpu
173 * cycles spent on reading ref data in sub-pixel filter functions.
174 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
175 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
176 * could reduce the area.
178 #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)
179 #define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) // pointer to predictor base of a motionvector
180 #define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
181 #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.
182 #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
183 #define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
184 #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
185 #define MIN(x,y) (((x)<(y))?(x):(y))
186 #define MAX(x,y) (((x)>(y))?(x):(y))
188 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
189 int_mv *bestmv, int_mv *ref_mv,
191 const vp8_variance_fn_ptr_t *vfp,
192 int *mvcost[2], int *distortion,
195 unsigned char *z = (*(b->base_src) + b->src);
197 int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1;
198 int br = bestmv->as_mv.row << 2, bc = bestmv->as_mv.col << 2;
199 int tr = br, tc = bc;
200 unsigned int besterr = INT_MAX;
201 unsigned int left, right, up, down, diag;
203 unsigned int whichdir;
204 unsigned int halfiters = 4;
205 unsigned int quarteriters = 4;
208 int minc = MAX(x->mv_col_min << 2, (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
209 int maxc = MIN(x->mv_col_max << 2, (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
210 int minr = MAX(x->mv_row_min << 2, (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
211 int maxr = MIN(x->mv_row_max << 2, (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
216 #if ARCH_X86 || ARCH_X86_64
217 MACROBLOCKD *xd = &x->e_mbd;
218 unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
220 int buf_r1, buf_r2, buf_c1, buf_c2;
222 // Clamping to avoid out-of-range data access
223 buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)?(bestmv->as_mv.row - x->mv_row_min):3;
224 buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)?(x->mv_row_max - bestmv->as_mv.row):3;
225 buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)?(bestmv->as_mv.col - x->mv_col_min):3;
226 buf_c2 = ((bestmv->as_mv.col + 3) > x->mv_col_max)?(x->mv_col_max - bestmv->as_mv.col):3;
229 /* Copy to intermediate buffer before searching. */
230 vfp->copymem(y0 - buf_c1 - d->pre_stride*buf_r1, d->pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2);
231 y = xd->y_buf + y_stride*buf_r1 +buf_c1;
233 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
234 y_stride = d->pre_stride;
237 offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;
240 bestmv->as_mv.row <<= 3;
241 bestmv->as_mv.col <<= 3;
243 // calculate central point error
244 besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
245 *distortion = besterr;
246 besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
248 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
252 CHECK_BETTER(left, tr, tc - 2);
253 CHECK_BETTER(right, tr, tc + 2);
254 CHECK_BETTER(up, tr - 2, tc);
255 CHECK_BETTER(down, tr + 2, tc);
257 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
262 CHECK_BETTER(diag, tr - 2, tc - 2);
265 CHECK_BETTER(diag, tr - 2, tc + 2);
268 CHECK_BETTER(diag, tr + 2, tc - 2);
271 CHECK_BETTER(diag, tr + 2, tc + 2);
275 // no reason to check the same one again.
276 if (tr == br && tc == bc)
283 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
285 while (--quarteriters)
287 CHECK_BETTER(left, tr, tc - 1);
288 CHECK_BETTER(right, tr, tc + 1);
289 CHECK_BETTER(up, tr - 1, tc);
290 CHECK_BETTER(down, tr + 1, tc);
292 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
297 CHECK_BETTER(diag, tr - 1, tc - 1);
300 CHECK_BETTER(diag, tr - 1, tc + 1);
303 CHECK_BETTER(diag, tr + 1, tc - 1);
306 CHECK_BETTER(diag, tr + 1, tc + 1);
310 // no reason to check the same one again.
311 if (tr == br && tc == bc)
318 bestmv->as_mv.row = br << 1;
319 bestmv->as_mv.col = bc << 1;
321 if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) ||
322 (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3)))
336 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
337 int_mv *bestmv, int_mv *ref_mv,
339 const vp8_variance_fn_ptr_t *vfp,
340 int *mvcost[2], int *distortion,
343 int bestmse = INT_MAX;
346 unsigned char *z = (*(b->base_src) + b->src);
347 int left, right, up, down, diag;
353 #if ARCH_X86 || ARCH_X86_64
354 MACROBLOCKD *xd = &x->e_mbd;
355 unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
359 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
360 vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
361 y = xd->y_buf + y_stride + 1;
363 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
364 y_stride = d->pre_stride;
368 bestmv->as_mv.row <<= 3;
369 bestmv->as_mv.col <<= 3;
372 // calculate central point error
373 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
374 *distortion = bestmse;
375 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
377 // go left then right and check error
378 this_mv.as_mv.row = startmv.as_mv.row;
379 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
380 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
381 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
387 *distortion = thismse;
391 this_mv.as_mv.col += 8;
392 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
393 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
399 *distortion = thismse;
403 // go up then down and check error
404 this_mv.as_mv.col = startmv.as_mv.col;
405 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
406 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
407 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
413 *distortion = thismse;
417 this_mv.as_mv.row += 8;
418 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
419 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
425 *distortion = thismse;
430 // now check 1 more diagonal
431 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
432 //for(whichdir =0;whichdir<4;whichdir++)
439 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
440 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
441 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
444 this_mv.as_mv.col += 4;
445 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
446 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
449 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
450 this_mv.as_mv.row += 4;
451 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
455 this_mv.as_mv.col += 4;
456 this_mv.as_mv.row += 4;
457 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
461 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
467 *distortion = thismse;
474 // time to check quarter pels.
475 if (bestmv->as_mv.row < startmv.as_mv.row)
478 if (bestmv->as_mv.col < startmv.as_mv.col)
485 // go left then right and check error
486 this_mv.as_mv.row = startmv.as_mv.row;
488 if (startmv.as_mv.col & 7)
490 this_mv.as_mv.col = startmv.as_mv.col - 2;
491 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
495 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
496 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
499 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
505 *distortion = thismse;
509 this_mv.as_mv.col += 4;
510 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
511 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
517 *distortion = thismse;
521 // go up then down and check error
522 this_mv.as_mv.col = startmv.as_mv.col;
524 if (startmv.as_mv.row & 7)
526 this_mv.as_mv.row = startmv.as_mv.row - 2;
527 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
531 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
532 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
535 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
541 *distortion = thismse;
545 this_mv.as_mv.row += 4;
546 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
547 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
553 *distortion = thismse;
558 // now check 1 more diagonal
559 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
561 // for(whichdir=0;whichdir<4;whichdir++)
569 if (startmv.as_mv.row & 7)
571 this_mv.as_mv.row -= 2;
573 if (startmv.as_mv.col & 7)
575 this_mv.as_mv.col -= 2;
576 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
580 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
581 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
586 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
588 if (startmv.as_mv.col & 7)
590 this_mv.as_mv.col -= 2;
591 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
595 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
596 thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
602 this_mv.as_mv.col += 2;
604 if (startmv.as_mv.row & 7)
606 this_mv.as_mv.row -= 2;
607 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
611 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
612 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
617 this_mv.as_mv.row += 2;
619 if (startmv.as_mv.col & 7)
621 this_mv.as_mv.col -= 2;
622 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
626 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
627 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
632 this_mv.as_mv.col += 2;
633 this_mv.as_mv.row += 2;
634 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
638 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
644 *distortion = thismse;
651 int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
652 int_mv *bestmv, int_mv *ref_mv,
654 const vp8_variance_fn_ptr_t *vfp,
655 int *mvcost[2], int *distortion,
658 int bestmse = INT_MAX;
661 unsigned char *z = (*(b->base_src) + b->src);
662 int left, right, up, down, diag;
668 #if ARCH_X86 || ARCH_X86_64
669 MACROBLOCKD *xd = &x->e_mbd;
670 unsigned char *y0 = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
674 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
675 vfp->copymem(y0 - 1 - d->pre_stride, d->pre_stride, xd->y_buf, y_stride, 18);
676 y = xd->y_buf + y_stride + 1;
678 unsigned char *y = *(d->base_pre) + d->pre + (bestmv->as_mv.row) * d->pre_stride + bestmv->as_mv.col;
679 y_stride = d->pre_stride;
683 bestmv->as_mv.row <<= 3;
684 bestmv->as_mv.col <<= 3;
687 // calculate central point error
688 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
689 *distortion = bestmse;
690 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
692 // go left then right and check error
693 this_mv.as_mv.row = startmv.as_mv.row;
694 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
695 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
696 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
702 *distortion = thismse;
706 this_mv.as_mv.col += 8;
707 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
708 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
714 *distortion = thismse;
718 // go up then down and check error
719 this_mv.as_mv.col = startmv.as_mv.col;
720 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
721 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
722 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
728 *distortion = thismse;
732 this_mv.as_mv.row += 8;
733 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
734 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
740 *distortion = thismse;
744 // now check 1 more diagonal -
745 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
751 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
752 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
753 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
756 this_mv.as_mv.col += 4;
757 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
758 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
761 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
762 this_mv.as_mv.row += 4;
763 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
767 this_mv.as_mv.col += 4;
768 this_mv.as_mv.row += 4;
769 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
773 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
779 *distortion = thismse;
786 #define CHECK_BOUNDS(range) \
789 all_in &= ((br-range) >= x->mv_row_min);\
790 all_in &= ((br+range) <= x->mv_row_max);\
791 all_in &= ((bc-range) >= x->mv_col_min);\
792 all_in &= ((bc+range) <= x->mv_col_max);\
795 #define CHECK_POINT \
797 if (this_mv.as_mv.col < x->mv_col_min) continue;\
798 if (this_mv.as_mv.col > x->mv_col_max) continue;\
799 if (this_mv.as_mv.row < x->mv_row_min) continue;\
800 if (this_mv.as_mv.row > x->mv_row_max) continue;\
803 #define CHECK_BETTER \
805 if (thissad < bestsad)\
807 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
808 if (thissad < bestsad)\
816 static const MV next_chkpts[6][3] =
818 {{ -2, 0}, { -1, -2}, {1, -2}},
819 {{ -1, -2}, {1, -2}, {2, 0}},
820 {{1, -2}, {2, 0}, {1, 2}},
821 {{2, 0}, {1, 2}, { -1, 2}},
822 {{1, 2}, { -1, 2}, { -2, 0}},
823 {{ -1, 2}, { -2, 0}, { -1, -2}}
835 const vp8_variance_fn_ptr_t *vfp,
841 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
842 MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
845 unsigned char *what = (*(b->base_src) + b->src);
846 int what_stride = b->src_stride;
847 int in_what_stride = d->pre_stride;
850 unsigned int bestsad = 0x7fffffff;
851 unsigned int thissad;
852 unsigned char *base_offset;
853 unsigned char *this_offset;
859 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
860 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
862 // adjust ref_mv to make sure it is within MV range
863 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
864 br = ref_mv->as_mv.row;
865 bc = ref_mv->as_mv.col;
867 // Work out the start point for the search
868 base_offset = (unsigned char *)(*(d->base_pre) + d->pre);
869 this_offset = base_offset + (br * (d->pre_stride)) + bc;
870 this_mv.as_mv.row = br;
871 this_mv.as_mv.col = bc;
872 bestsad = vfp->sdf( what, what_stride, this_offset,
873 in_what_stride, 0x7fffffff)
874 + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
882 for (i = 0; i < 6; i++)
884 this_mv.as_mv.row = br + hex[i].row;
885 this_mv.as_mv.col = bc + hex[i].col;
886 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
887 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
892 for (i = 0; i < 6; i++)
894 this_mv.as_mv.row = br + hex[i].row;
895 this_mv.as_mv.col = bc + hex[i].col;
897 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
898 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
907 br += hex[best_site].row;
908 bc += hex[best_site].col;
912 for (j = 1; j < 127; j++)
919 for (i = 0; i < 3; i++)
921 this_mv.as_mv.row = br + next_chkpts[k][i].row;
922 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
923 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
924 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
929 for (i = 0; i < 3; i++)
931 this_mv.as_mv.row = br + next_chkpts[k][i].row;
932 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
934 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
935 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
944 br += next_chkpts[k][best_site].row;
945 bc += next_chkpts[k][best_site].col;
947 if (k >= 12) k -= 12;
948 else if (k >= 6) k -= 6;
952 // check 4 1-away neighbors
954 for (j = 0; j < 32; j++)
961 for (i = 0; i < 4; i++)
963 this_mv.as_mv.row = br + neighbors[i].row;
964 this_mv.as_mv.col = bc + neighbors[i].col;
965 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
966 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
971 for (i = 0; i < 4; i++)
973 this_mv.as_mv.row = br + neighbors[i].row;
974 this_mv.as_mv.col = bc + neighbors[i].col;
976 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
977 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
986 br += neighbors[best_site].row;
987 bc += neighbors[best_site].col;
991 best_mv->as_mv.row = br;
992 best_mv->as_mv.col = bc;
1000 int vp8_diamond_search_sad
1010 vp8_variance_fn_ptr_t *fn_ptr,
1017 unsigned char *what = (*(b->base_src) + b->src);
1018 int what_stride = b->src_stride;
1019 unsigned char *in_what;
1020 int in_what_stride = d->pre_stride;
1021 unsigned char *best_address;
1026 int bestsad = INT_MAX;
1032 int this_row_offset;
1033 int this_col_offset;
1036 unsigned char *check_here;
1039 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1041 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1042 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1044 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1045 ref_row = ref_mv->as_mv.row;
1046 ref_col = ref_mv->as_mv.col;
1048 best_mv->as_mv.row = ref_row;
1049 best_mv->as_mv.col = ref_col;
1051 // Work out the start point for the search
1052 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1053 best_address = in_what;
1055 // Check the starting position
1056 bestsad = fn_ptr->sdf(what, what_stride, in_what,
1057 in_what_stride, 0x7fffffff)
1058 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1060 // search_param determines the length of the initial step and hence the number of iterations
1061 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1062 ss = &x->ss[search_param * x->searches_per_step];
1063 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1067 for (step = 0; step < tot_steps ; step++)
1069 for (j = 0 ; j < x->searches_per_step ; j++)
1071 // Trap illegal vectors
1072 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1073 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1075 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1076 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1079 check_here = ss[i].offset + best_address;
1080 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1082 if (thissad < bestsad)
1084 this_mv.as_mv.row = this_row_offset;
1085 this_mv.as_mv.col = this_col_offset;
1086 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1087 mvsadcost, sad_per_bit);
1089 if (thissad < bestsad)
1100 if (best_site != last_site)
1102 best_mv->as_mv.row += ss[best_site].mv.row;
1103 best_mv->as_mv.col += ss[best_site].mv.col;
1104 best_address += ss[best_site].offset;
1105 last_site = best_site;
1107 else if (best_address == in_what)
1111 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1112 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1114 if (bestsad == INT_MAX)
1117 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1118 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1121 int vp8_diamond_search_sadx4
1131 vp8_variance_fn_ptr_t *fn_ptr,
1138 unsigned char *what = (*(b->base_src) + b->src);
1139 int what_stride = b->src_stride;
1140 unsigned char *in_what;
1141 int in_what_stride = d->pre_stride;
1142 unsigned char *best_address;
1147 int bestsad = INT_MAX;
1153 int this_row_offset;
1154 int this_col_offset;
1157 unsigned char *check_here;
1158 unsigned int thissad;
1160 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1162 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1163 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1165 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1166 ref_row = ref_mv->as_mv.row;
1167 ref_col = ref_mv->as_mv.col;
1169 best_mv->as_mv.row = ref_row;
1170 best_mv->as_mv.col = ref_col;
1172 // Work out the start point for the search
1173 in_what = (unsigned char *)(*(d->base_pre) + d->pre + (ref_row * (d->pre_stride)) + ref_col);
1174 best_address = in_what;
1176 // Check the starting position
1177 bestsad = fn_ptr->sdf(what, what_stride,
1178 in_what, in_what_stride, 0x7fffffff)
1179 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1181 // search_param determines the length of the initial step and hence the number of iterations
1182 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1183 ss = &x->ss[search_param * x->searches_per_step];
1184 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1188 for (step = 0; step < tot_steps ; step++)
1192 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1193 // checking 4 bounds for each points.
1194 all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
1195 all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
1196 all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
1197 all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
1201 unsigned int sad_array[4];
1203 for (j = 0 ; j < x->searches_per_step ; j += 4)
1205 unsigned char *block_offset[4];
1207 for (t = 0; t < 4; t++)
1208 block_offset[t] = ss[i+t].offset + best_address;
1210 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1212 for (t = 0; t < 4; t++, i++)
1214 if (sad_array[t] < bestsad)
1216 this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1217 this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1218 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
1219 mvsadcost, sad_per_bit);
1221 if (sad_array[t] < bestsad)
1223 bestsad = sad_array[t];
1232 for (j = 0 ; j < x->searches_per_step ; j++)
1234 // Trap illegal vectors
1235 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1236 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1238 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1239 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1241 check_here = ss[i].offset + best_address;
1242 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1244 if (thissad < bestsad)
1246 this_mv.as_mv.row = this_row_offset;
1247 this_mv.as_mv.col = this_col_offset;
1248 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1249 mvsadcost, sad_per_bit);
1251 if (thissad < bestsad)
1262 if (best_site != last_site)
1264 best_mv->as_mv.row += ss[best_site].mv.row;
1265 best_mv->as_mv.col += ss[best_site].mv.col;
1266 best_address += ss[best_site].offset;
1267 last_site = best_site;
1269 else if (best_address == in_what)
1273 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1274 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1276 if (bestsad == INT_MAX)
1279 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1280 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1283 int vp8_full_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1284 int sad_per_bit, int distance,
1285 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1288 unsigned char *what = (*(b->base_src) + b->src);
1289 int what_stride = b->src_stride;
1290 unsigned char *in_what;
1291 int in_what_stride = d->pre_stride;
1292 int mv_stride = d->pre_stride;
1293 unsigned char *bestaddress;
1294 int_mv *best_mv = &d->bmi.mv;
1296 int bestsad = INT_MAX;
1299 unsigned char *check_here;
1302 int ref_row = ref_mv->as_mv.row;
1303 int ref_col = ref_mv->as_mv.col;
1305 int row_min = ref_row - distance;
1306 int row_max = ref_row + distance;
1307 int col_min = ref_col - distance;
1308 int col_max = ref_col + distance;
1310 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1312 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1313 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1315 // Work out the mid point for the search
1316 in_what = *(d->base_pre) + d->pre;
1317 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1319 best_mv->as_mv.row = ref_row;
1320 best_mv->as_mv.col = ref_col;
1322 // Baseline value at the centre
1323 bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
1324 in_what_stride, 0x7fffffff)
1325 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1327 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1328 if (col_min < x->mv_col_min)
1329 col_min = x->mv_col_min;
1331 if (col_max > x->mv_col_max)
1332 col_max = x->mv_col_max;
1334 if (row_min < x->mv_row_min)
1335 row_min = x->mv_row_min;
1337 if (row_max > x->mv_row_max)
1338 row_max = x->mv_row_max;
1340 for (r = row_min; r < row_max ; r++)
1342 this_mv.as_mv.row = r;
1343 check_here = r * mv_stride + in_what + col_min;
1345 for (c = col_min; c < col_max; c++)
1347 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1349 this_mv.as_mv.col = c;
1350 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1351 mvsadcost, sad_per_bit);
1353 if (thissad < bestsad)
1356 best_mv->as_mv.row = r;
1357 best_mv->as_mv.col = c;
1358 bestaddress = check_here;
1365 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1366 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1368 if (bestsad < INT_MAX)
1369 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1370 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1375 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1376 int sad_per_bit, int distance,
1377 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1380 unsigned char *what = (*(b->base_src) + b->src);
1381 int what_stride = b->src_stride;
1382 unsigned char *in_what;
1383 int in_what_stride = d->pre_stride;
1384 int mv_stride = d->pre_stride;
1385 unsigned char *bestaddress;
1386 int_mv *best_mv = &d->bmi.mv;
1388 int bestsad = INT_MAX;
1391 unsigned char *check_here;
1392 unsigned int thissad;
1394 int ref_row = ref_mv->as_mv.row;
1395 int ref_col = ref_mv->as_mv.col;
1397 int row_min = ref_row - distance;
1398 int row_max = ref_row + distance;
1399 int col_min = ref_col - distance;
1400 int col_max = ref_col + distance;
1402 unsigned int sad_array[3];
1404 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1406 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1407 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1409 // Work out the mid point for the search
1410 in_what = *(d->base_pre) + d->pre;
1411 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1413 best_mv->as_mv.row = ref_row;
1414 best_mv->as_mv.col = ref_col;
1416 // Baseline value at the centre
1417 bestsad = fn_ptr->sdf(what, what_stride,
1418 bestaddress, in_what_stride, 0x7fffffff)
1419 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1421 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1422 if (col_min < x->mv_col_min)
1423 col_min = x->mv_col_min;
1425 if (col_max > x->mv_col_max)
1426 col_max = x->mv_col_max;
1428 if (row_min < x->mv_row_min)
1429 row_min = x->mv_row_min;
1431 if (row_max > x->mv_row_max)
1432 row_max = x->mv_row_max;
1434 for (r = row_min; r < row_max ; r++)
1436 this_mv.as_mv.row = r;
1437 check_here = r * mv_stride + in_what + col_min;
1440 while ((c + 2) < col_max)
1444 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1446 for (i = 0; i < 3; i++)
1448 thissad = sad_array[i];
1450 if (thissad < bestsad)
1452 this_mv.as_mv.col = c;
1453 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1454 mvsadcost, sad_per_bit);
1456 if (thissad < bestsad)
1459 best_mv->as_mv.row = r;
1460 best_mv->as_mv.col = c;
1461 bestaddress = check_here;
1472 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1474 if (thissad < bestsad)
1476 this_mv.as_mv.col = c;
1477 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1478 mvsadcost, sad_per_bit);
1480 if (thissad < bestsad)
1483 best_mv->as_mv.row = r;
1484 best_mv->as_mv.col = c;
1485 bestaddress = check_here;
1495 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1496 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1498 if (bestsad < INT_MAX)
1499 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1500 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1505 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1506 int sad_per_bit, int distance,
1507 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1510 unsigned char *what = (*(b->base_src) + b->src);
1511 int what_stride = b->src_stride;
1512 unsigned char *in_what;
1513 int in_what_stride = d->pre_stride;
1514 int mv_stride = d->pre_stride;
1515 unsigned char *bestaddress;
1516 int_mv *best_mv = &d->bmi.mv;
1518 int bestsad = INT_MAX;
1521 unsigned char *check_here;
1522 unsigned int thissad;
1524 int ref_row = ref_mv->as_mv.row;
1525 int ref_col = ref_mv->as_mv.col;
1527 int row_min = ref_row - distance;
1528 int row_max = ref_row + distance;
1529 int col_min = ref_col - distance;
1530 int col_max = ref_col + distance;
1532 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1533 unsigned int sad_array[3];
1535 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1537 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1538 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1540 // Work out the mid point for the search
1541 in_what = *(d->base_pre) + d->pre;
1542 bestaddress = in_what + (ref_row * d->pre_stride) + ref_col;
1544 best_mv->as_mv.row = ref_row;
1545 best_mv->as_mv.col = ref_col;
1547 // Baseline value at the centre
1548 bestsad = fn_ptr->sdf(what, what_stride,
1549 bestaddress, in_what_stride, 0x7fffffff)
1550 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1552 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1553 if (col_min < x->mv_col_min)
1554 col_min = x->mv_col_min;
1556 if (col_max > x->mv_col_max)
1557 col_max = x->mv_col_max;
1559 if (row_min < x->mv_row_min)
1560 row_min = x->mv_row_min;
1562 if (row_max > x->mv_row_max)
1563 row_max = x->mv_row_max;
1565 for (r = row_min; r < row_max ; r++)
1567 this_mv.as_mv.row = r;
1568 check_here = r * mv_stride + in_what + col_min;
1571 while ((c + 7) < col_max)
1575 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1577 for (i = 0; i < 8; i++)
1579 thissad = (unsigned int)sad_array8[i];
1581 if (thissad < bestsad)
1583 this_mv.as_mv.col = c;
1584 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1585 mvsadcost, sad_per_bit);
1587 if (thissad < bestsad)
1590 best_mv->as_mv.row = r;
1591 best_mv->as_mv.col = c;
1592 bestaddress = check_here;
1601 while ((c + 2) < col_max)
1605 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1607 for (i = 0; i < 3; i++)
1609 thissad = sad_array[i];
1611 if (thissad < bestsad)
1613 this_mv.as_mv.col = c;
1614 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1615 mvsadcost, sad_per_bit);
1617 if (thissad < bestsad)
1620 best_mv->as_mv.row = r;
1621 best_mv->as_mv.col = c;
1622 bestaddress = check_here;
1633 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1635 if (thissad < bestsad)
1637 this_mv.as_mv.col = c;
1638 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1639 mvsadcost, sad_per_bit);
1641 if (thissad < bestsad)
1644 best_mv->as_mv.row = r;
1645 best_mv->as_mv.col = c;
1646 bestaddress = check_here;
1655 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1656 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1658 if (bestsad < INT_MAX)
1659 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1660 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1665 int vp8_refining_search_sad(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1666 int error_per_bit, int search_range,
1667 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1670 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1672 short this_row_offset, this_col_offset;
1674 int what_stride = b->src_stride;
1675 int in_what_stride = d->pre_stride;
1676 unsigned char *what = (*(b->base_src) + b->src);
1677 unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1678 (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1679 unsigned char *check_here;
1680 unsigned int thissad;
1682 unsigned int bestsad = INT_MAX;
1684 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1687 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1688 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1690 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1692 for (i=0; i<search_range; i++)
1696 for (j = 0 ; j < 4 ; j++)
1698 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1699 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1701 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1702 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1704 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1705 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1707 if (thissad < bestsad)
1709 this_mv.as_mv.row = this_row_offset;
1710 this_mv.as_mv.col = this_col_offset;
1711 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1713 if (thissad < bestsad)
1722 if (best_site == -1)
1726 ref_mv->as_mv.row += neighbors[best_site].row;
1727 ref_mv->as_mv.col += neighbors[best_site].col;
1728 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1732 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1733 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1735 if (bestsad < INT_MAX)
1736 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1737 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1742 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1743 int_mv *ref_mv, int error_per_bit,
1744 int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1745 int *mvcost[2], int_mv *center_mv)
1747 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1749 short this_row_offset, this_col_offset;
1751 int what_stride = b->src_stride;
1752 int in_what_stride = d->pre_stride;
1753 unsigned char *what = (*(b->base_src) + b->src);
1754 unsigned char *best_address = (unsigned char *)(*(d->base_pre) + d->pre +
1755 (ref_mv->as_mv.row * (d->pre_stride)) + ref_mv->as_mv.col);
1756 unsigned char *check_here;
1757 unsigned int thissad;
1759 unsigned int bestsad = INT_MAX;
1761 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1764 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1765 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1767 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1769 for (i=0; i<search_range; i++)
1774 all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1775 all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1776 all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1777 all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1781 unsigned int sad_array[4];
1782 unsigned char *block_offset[4];
1783 block_offset[0] = best_address - in_what_stride;
1784 block_offset[1] = best_address - 1;
1785 block_offset[2] = best_address + 1;
1786 block_offset[3] = best_address + in_what_stride;
1788 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1790 for (j = 0; j < 4; j++)
1792 if (sad_array[j] < bestsad)
1794 this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1795 this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1796 sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1798 if (sad_array[j] < bestsad)
1800 bestsad = sad_array[j];
1808 for (j = 0 ; j < 4 ; j++)
1810 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1811 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1813 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1814 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1816 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1817 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1819 if (thissad < bestsad)
1821 this_mv.as_mv.row = this_row_offset;
1822 this_mv.as_mv.col = this_col_offset;
1823 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1825 if (thissad < bestsad)
1835 if (best_site == -1)
1839 ref_mv->as_mv.row += neighbors[best_site].row;
1840 ref_mv->as_mv.col += neighbors[best_site].col;
1841 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1845 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1846 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1848 if (bestsad < INT_MAX)
1849 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1850 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1855 #ifdef ENTROPY_STATS
1856 void print_mode_context(void)
1858 FILE *f = fopen("modecont.c", "w");
1861 fprintf(f, "#include \"entropy.h\"\n");
1862 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1865 for (j = 0; j < 6; j++)
1867 fprintf(f, " { // %d \n", j);
1870 for (i = 0; i < 4; i++)
1874 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1877 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1880 overal_prob = 256 * mv_mode_cts[i][0] / count;
1884 if (overal_prob == 0)
1888 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1891 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1898 fprintf(f, "%5d, ", this_prob);
1899 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1900 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1903 fprintf(f, " },\n");
1910 /* MV ref count ENTROPY_STATS stats code */
1911 #ifdef ENTROPY_STATS
1912 void init_mv_ref_counts()
1914 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1915 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1918 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1922 ++mv_ref_ct [ct[0]] [0] [0];
1923 ++mv_mode_cts[0][0];
1927 ++mv_ref_ct [ct[0]] [0] [1];
1928 ++mv_mode_cts[0][1];
1932 ++mv_ref_ct [ct[1]] [1] [0];
1933 ++mv_mode_cts[1][0];
1937 ++mv_ref_ct [ct[1]] [1] [1];
1938 ++mv_mode_cts[1][1];
1942 ++mv_ref_ct [ct[2]] [2] [0];
1943 ++mv_mode_cts[2][0];
1947 ++mv_ref_ct [ct[2]] [2] [1];
1948 ++mv_mode_cts[2][1];
1952 ++mv_ref_ct [ct[3]] [3] [0];
1953 ++mv_mode_cts[3][0];
1957 ++mv_ref_ct [ct[3]] [3] [1];
1958 ++mv_mode_cts[3][1];
1965 #endif/* END MV ref count ENTROPY_STATS stats code */