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.
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_config.h"
19 #include "vp8/common/findnearmv.h"
22 static int mv_ref_ct [31] [4] [2];
23 static int mv_mode_cts [4] [2];
26 int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
28 // MV costing is based on the distribution of vectors in the previous frame and as such will tend to
29 // over state the cost of vectors. In addition coding a new vector can have a knock on effect on the
30 // cost of subsequent vectors and the quality of prediction from NEAR and NEAREST for subsequent blocks.
31 // The "Weight" parameter allows, to a limited extent, for some account to be taken of these factors.
32 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;
35 static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit)
37 return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] +
38 mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1])
39 * error_per_bit + 128) >> 8;
42 static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
44 /* Calculate sad error cost on full pixel basis. */
45 return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
46 mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)])
47 * error_per_bit + 128) >> 8;
50 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
53 int search_site_count = 0;
56 // Generate offsets for 4 search sites per step.
58 x->ss[search_site_count].mv.col = 0;
59 x->ss[search_site_count].mv.row = 0;
60 x->ss[search_site_count].offset = 0;
66 // Compute offsets for search sites.
67 x->ss[search_site_count].mv.col = 0;
68 x->ss[search_site_count].mv.row = -Len;
69 x->ss[search_site_count].offset = -Len * stride;
72 // Compute offsets for search sites.
73 x->ss[search_site_count].mv.col = 0;
74 x->ss[search_site_count].mv.row = Len;
75 x->ss[search_site_count].offset = Len * stride;
78 // Compute offsets for search sites.
79 x->ss[search_site_count].mv.col = -Len;
80 x->ss[search_site_count].mv.row = 0;
81 x->ss[search_site_count].offset = -Len;
84 // Compute offsets for search sites.
85 x->ss[search_site_count].mv.col = Len;
86 x->ss[search_site_count].mv.row = 0;
87 x->ss[search_site_count].offset = Len;
94 x->ss_count = search_site_count;
95 x->searches_per_step = 4;
98 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
101 int search_site_count = 0;
103 // Generate offsets for 8 search sites per step.
104 Len = MAX_FIRST_STEP;
105 x->ss[search_site_count].mv.col = 0;
106 x->ss[search_site_count].mv.row = 0;
107 x->ss[search_site_count].offset = 0;
113 // Compute offsets for search sites.
114 x->ss[search_site_count].mv.col = 0;
115 x->ss[search_site_count].mv.row = -Len;
116 x->ss[search_site_count].offset = -Len * stride;
119 // Compute offsets for search sites.
120 x->ss[search_site_count].mv.col = 0;
121 x->ss[search_site_count].mv.row = Len;
122 x->ss[search_site_count].offset = Len * stride;
125 // Compute offsets for search sites.
126 x->ss[search_site_count].mv.col = -Len;
127 x->ss[search_site_count].mv.row = 0;
128 x->ss[search_site_count].offset = -Len;
131 // Compute offsets for search sites.
132 x->ss[search_site_count].mv.col = Len;
133 x->ss[search_site_count].mv.row = 0;
134 x->ss[search_site_count].offset = Len;
137 // Compute offsets for search sites.
138 x->ss[search_site_count].mv.col = -Len;
139 x->ss[search_site_count].mv.row = -Len;
140 x->ss[search_site_count].offset = -Len * stride - Len;
143 // Compute offsets for search sites.
144 x->ss[search_site_count].mv.col = Len;
145 x->ss[search_site_count].mv.row = -Len;
146 x->ss[search_site_count].offset = -Len * stride + Len;
149 // Compute offsets for search sites.
150 x->ss[search_site_count].mv.col = -Len;
151 x->ss[search_site_count].mv.row = Len;
152 x->ss[search_site_count].offset = Len * stride - Len;
155 // Compute offsets for search sites.
156 x->ss[search_site_count].mv.col = Len;
157 x->ss[search_site_count].mv.row = Len;
158 x->ss[search_site_count].offset = Len * stride + Len;
166 x->ss_count = search_site_count;
167 x->searches_per_step = 8;
171 * To avoid the penalty for crossing cache-line read, preload the reference
172 * area in a small buffer, which is aligned to make sure there won't be crossing
173 * cache-line read while reading from this buffer. This reduced the cpu
174 * cycles spent on reading ref data in sub-pixel filter functions.
175 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
176 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
177 * could reduce the area.
179 #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)
180 #define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) // pointer to predictor base of a motionvector
181 #define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
182 #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.
183 #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
184 #define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
185 #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
187 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
188 int_mv *bestmv, int_mv *ref_mv,
190 const vp8_variance_fn_ptr_t *vfp,
191 int *mvcost[2], int *distortion,
194 unsigned char *z = (*(b->base_src) + b->src);
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;
202 unsigned int whichdir;
203 unsigned int halfiters = 4;
204 unsigned int quarteriters = 4;
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));
214 int pre_stride = x->e_mbd.pre.y_stride;
215 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
218 #if ARCH_X86 || ARCH_X86_64
219 MACROBLOCKD *xd = &x->e_mbd;
220 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
222 int buf_r1, buf_r2, buf_c1, buf_c2;
224 // Clamping to avoid out-of-range data access
225 buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)?(bestmv->as_mv.row - x->mv_row_min):3;
226 buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)?(x->mv_row_max - bestmv->as_mv.row):3;
227 buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)?(bestmv->as_mv.col - x->mv_col_min):3;
228 buf_c2 = ((bestmv->as_mv.col + 3) > x->mv_col_max)?(x->mv_col_max - bestmv->as_mv.col):3;
231 /* Copy to intermediate buffer before searching. */
232 vfp->copymem(y0 - buf_c1 - pre_stride*buf_r1, pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2);
233 y = xd->y_buf + y_stride*buf_r1 +buf_c1;
235 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
236 y_stride = pre_stride;
239 offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;
242 bestmv->as_mv.row <<= 3;
243 bestmv->as_mv.col <<= 3;
245 // calculate central point error
246 besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
247 *distortion = besterr;
248 besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
250 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
254 CHECK_BETTER(left, tr, tc - 2);
255 CHECK_BETTER(right, tr, tc + 2);
256 CHECK_BETTER(up, tr - 2, tc);
257 CHECK_BETTER(down, tr + 2, tc);
259 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
264 CHECK_BETTER(diag, tr - 2, tc - 2);
267 CHECK_BETTER(diag, tr - 2, tc + 2);
270 CHECK_BETTER(diag, tr + 2, tc - 2);
273 CHECK_BETTER(diag, tr + 2, tc + 2);
277 // no reason to check the same one again.
278 if (tr == br && tc == bc)
285 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
287 while (--quarteriters)
289 CHECK_BETTER(left, tr, tc - 1);
290 CHECK_BETTER(right, tr, tc + 1);
291 CHECK_BETTER(up, tr - 1, tc);
292 CHECK_BETTER(down, tr + 1, tc);
294 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
299 CHECK_BETTER(diag, tr - 1, tc - 1);
302 CHECK_BETTER(diag, tr - 1, tc + 1);
305 CHECK_BETTER(diag, tr + 1, tc - 1);
308 CHECK_BETTER(diag, tr + 1, tc + 1);
312 // no reason to check the same one again.
313 if (tr == br && tc == bc)
320 bestmv->as_mv.row = br << 1;
321 bestmv->as_mv.col = bc << 1;
323 if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) ||
324 (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3)))
337 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
338 int_mv *bestmv, int_mv *ref_mv,
340 const vp8_variance_fn_ptr_t *vfp,
341 int *mvcost[2], int *distortion,
344 int bestmse = INT_MAX;
347 unsigned char *z = (*(b->base_src) + b->src);
348 int left, right, up, down, diag;
353 int pre_stride = x->e_mbd.pre.y_stride;
354 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
356 #if ARCH_X86 || ARCH_X86_64
357 MACROBLOCKD *xd = &x->e_mbd;
358 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
362 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
363 vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
364 y = xd->y_buf + y_stride + 1;
366 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
367 y_stride = pre_stride;
371 bestmv->as_mv.row <<= 3;
372 bestmv->as_mv.col <<= 3;
375 // calculate central point error
376 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
377 *distortion = bestmse;
378 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
380 // go left then right and check error
381 this_mv.as_mv.row = startmv.as_mv.row;
382 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
383 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
384 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
390 *distortion = thismse;
394 this_mv.as_mv.col += 8;
395 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
396 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
402 *distortion = thismse;
406 // go up then down and check error
407 this_mv.as_mv.col = startmv.as_mv.col;
408 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
409 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
410 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
416 *distortion = thismse;
420 this_mv.as_mv.row += 8;
421 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
422 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
428 *distortion = thismse;
433 // now check 1 more diagonal
434 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
435 //for(whichdir =0;whichdir<4;whichdir++)
442 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
443 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
444 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
447 this_mv.as_mv.col += 4;
448 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
449 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
452 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
453 this_mv.as_mv.row += 4;
454 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
458 this_mv.as_mv.col += 4;
459 this_mv.as_mv.row += 4;
460 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
464 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
470 *distortion = thismse;
477 // time to check quarter pels.
478 if (bestmv->as_mv.row < startmv.as_mv.row)
481 if (bestmv->as_mv.col < startmv.as_mv.col)
488 // go left then right and check error
489 this_mv.as_mv.row = startmv.as_mv.row;
491 if (startmv.as_mv.col & 7)
493 this_mv.as_mv.col = startmv.as_mv.col - 2;
494 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
498 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
499 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
502 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
508 *distortion = thismse;
512 this_mv.as_mv.col += 4;
513 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
514 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
520 *distortion = thismse;
524 // go up then down and check error
525 this_mv.as_mv.col = startmv.as_mv.col;
527 if (startmv.as_mv.row & 7)
529 this_mv.as_mv.row = startmv.as_mv.row - 2;
530 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
534 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
535 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
538 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
544 *distortion = thismse;
548 this_mv.as_mv.row += 4;
549 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
550 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
556 *distortion = thismse;
561 // now check 1 more diagonal
562 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
564 // for(whichdir=0;whichdir<4;whichdir++)
572 if (startmv.as_mv.row & 7)
574 this_mv.as_mv.row -= 2;
576 if (startmv.as_mv.col & 7)
578 this_mv.as_mv.col -= 2;
579 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
583 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
584 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
589 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
591 if (startmv.as_mv.col & 7)
593 this_mv.as_mv.col -= 2;
594 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
598 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
599 thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
605 this_mv.as_mv.col += 2;
607 if (startmv.as_mv.row & 7)
609 this_mv.as_mv.row -= 2;
610 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
614 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
615 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
620 this_mv.as_mv.row += 2;
622 if (startmv.as_mv.col & 7)
624 this_mv.as_mv.col -= 2;
625 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
629 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
630 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
635 this_mv.as_mv.col += 2;
636 this_mv.as_mv.row += 2;
637 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
641 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
647 *distortion = thismse;
654 int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
655 int_mv *bestmv, int_mv *ref_mv,
657 const vp8_variance_fn_ptr_t *vfp,
658 int *mvcost[2], int *distortion,
661 int bestmse = INT_MAX;
664 unsigned char *z = (*(b->base_src) + b->src);
665 int left, right, up, down, diag;
670 int pre_stride = x->e_mbd.pre.y_stride;
671 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
673 #if ARCH_X86 || ARCH_X86_64
674 MACROBLOCKD *xd = &x->e_mbd;
675 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
679 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
680 vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
681 y = xd->y_buf + y_stride + 1;
683 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
684 y_stride = pre_stride;
688 bestmv->as_mv.row <<= 3;
689 bestmv->as_mv.col <<= 3;
692 // calculate central point error
693 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
694 *distortion = bestmse;
695 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
697 // go left then right and check error
698 this_mv.as_mv.row = startmv.as_mv.row;
699 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
700 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
701 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
707 *distortion = thismse;
711 this_mv.as_mv.col += 8;
712 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
713 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
719 *distortion = thismse;
723 // go up then down and check error
724 this_mv.as_mv.col = startmv.as_mv.col;
725 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
726 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
727 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
733 *distortion = thismse;
737 this_mv.as_mv.row += 8;
738 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
739 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
745 *distortion = thismse;
749 // now check 1 more diagonal -
750 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
756 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
757 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
758 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
761 this_mv.as_mv.col += 4;
762 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
763 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
766 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
767 this_mv.as_mv.row += 4;
768 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
772 this_mv.as_mv.col += 4;
773 this_mv.as_mv.row += 4;
774 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
778 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
784 *distortion = thismse;
791 #define CHECK_BOUNDS(range) \
794 all_in &= ((br-range) >= x->mv_row_min);\
795 all_in &= ((br+range) <= x->mv_row_max);\
796 all_in &= ((bc-range) >= x->mv_col_min);\
797 all_in &= ((bc+range) <= x->mv_col_max);\
800 #define CHECK_POINT \
802 if (this_mv.as_mv.col < x->mv_col_min) continue;\
803 if (this_mv.as_mv.col > x->mv_col_max) continue;\
804 if (this_mv.as_mv.row < x->mv_row_min) continue;\
805 if (this_mv.as_mv.row > x->mv_row_max) continue;\
808 #define CHECK_BETTER \
810 if (thissad < bestsad)\
812 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
813 if (thissad < bestsad)\
821 static const MV next_chkpts[6][3] =
823 {{ -2, 0}, { -1, -2}, {1, -2}},
824 {{ -1, -2}, {1, -2}, {2, 0}},
825 {{1, -2}, {2, 0}, {1, 2}},
826 {{2, 0}, {1, 2}, { -1, 2}},
827 {{1, 2}, { -1, 2}, { -2, 0}},
828 {{ -1, 2}, { -2, 0}, { -1, -2}}
840 const vp8_variance_fn_ptr_t *vfp,
846 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
847 MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
850 unsigned char *what = (*(b->base_src) + b->src);
851 int what_stride = b->src_stride;
852 int pre_stride = x->e_mbd.pre.y_stride;
853 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
855 int in_what_stride = pre_stride;
858 unsigned int bestsad = 0x7fffffff;
859 unsigned int thissad;
860 unsigned char *base_offset;
861 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 // adjust ref_mv to make sure it is within MV range
873 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
874 br = ref_mv->as_mv.row;
875 bc = ref_mv->as_mv.col;
877 // Work out the start point for the search
878 base_offset = (unsigned char *)(base_pre + d->offset);
879 this_offset = base_offset + (br * (pre_stride)) + bc;
880 this_mv.as_mv.row = br;
881 this_mv.as_mv.col = bc;
882 bestsad = vfp->sdf( what, what_stride, this_offset,
883 in_what_stride, 0x7fffffff)
884 + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
886 #if CONFIG_MULTI_RES_ENCODING
887 /* Lower search range based on prediction info */
888 if (search_param >= 6) goto cal_neighbors;
889 else if (search_param >= 5) hex_range = 4;
890 else if (search_param >= 4) hex_range = 6;
891 else if (search_param >= 3) hex_range = 15;
892 else if (search_param >= 2) hex_range = 31;
893 else if (search_param >= 1) hex_range = 63;
904 for (i = 0; i < 6; i++)
906 this_mv.as_mv.row = br + hex[i].row;
907 this_mv.as_mv.col = bc + hex[i].col;
908 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
909 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
914 for (i = 0; i < 6; i++)
916 this_mv.as_mv.row = br + hex[i].row;
917 this_mv.as_mv.col = bc + hex[i].col;
919 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
920 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
929 br += hex[best_site].row;
930 bc += hex[best_site].col;
934 for (j = 1; j < hex_range; j++)
941 for (i = 0; i < 3; i++)
943 this_mv.as_mv.row = br + next_chkpts[k][i].row;
944 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
945 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
946 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
951 for (i = 0; i < 3; i++)
953 this_mv.as_mv.row = br + next_chkpts[k][i].row;
954 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
956 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
957 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
966 br += next_chkpts[k][best_site].row;
967 bc += next_chkpts[k][best_site].col;
969 if (k >= 12) k -= 12;
970 else if (k >= 6) k -= 6;
974 // check 4 1-away neighbors
976 for (j = 0; j < dia_range; j++)
983 for (i = 0; i < 4; i++)
985 this_mv.as_mv.row = br + neighbors[i].row;
986 this_mv.as_mv.col = bc + neighbors[i].col;
987 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
988 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
993 for (i = 0; i < 4; i++)
995 this_mv.as_mv.row = br + neighbors[i].row;
996 this_mv.as_mv.col = bc + neighbors[i].col;
998 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
999 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
1004 if (best_site == -1)
1008 br += neighbors[best_site].row;
1009 bc += neighbors[best_site].col;
1013 best_mv->as_mv.row = br;
1014 best_mv->as_mv.col = bc;
1022 int vp8_diamond_search_sad_c
1032 vp8_variance_fn_ptr_t *fn_ptr,
1039 unsigned char *what = (*(b->base_src) + b->src);
1040 int what_stride = b->src_stride;
1041 unsigned char *in_what;
1042 int pre_stride = x->e_mbd.pre.y_stride;
1043 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1044 int in_what_stride = pre_stride;
1045 unsigned char *best_address;
1050 int bestsad = INT_MAX;
1056 int this_row_offset;
1057 int this_col_offset;
1060 unsigned char *check_here;
1063 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1065 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1066 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1068 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1069 ref_row = ref_mv->as_mv.row;
1070 ref_col = ref_mv->as_mv.col;
1072 best_mv->as_mv.row = ref_row;
1073 best_mv->as_mv.col = ref_col;
1075 // Work out the start point for the search
1076 in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col);
1077 best_address = in_what;
1079 // Check the starting position
1080 bestsad = fn_ptr->sdf(what, what_stride, in_what,
1081 in_what_stride, 0x7fffffff)
1082 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1084 // search_param determines the length of the initial step and hence the number of iterations
1085 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1086 ss = &x->ss[search_param * x->searches_per_step];
1087 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1091 for (step = 0; step < tot_steps ; step++)
1093 for (j = 0 ; j < x->searches_per_step ; j++)
1095 // Trap illegal vectors
1096 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1097 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1099 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1100 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1103 check_here = ss[i].offset + best_address;
1104 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1106 if (thissad < bestsad)
1108 this_mv.as_mv.row = this_row_offset;
1109 this_mv.as_mv.col = this_col_offset;
1110 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1111 mvsadcost, sad_per_bit);
1113 if (thissad < bestsad)
1124 if (best_site != last_site)
1126 best_mv->as_mv.row += ss[best_site].mv.row;
1127 best_mv->as_mv.col += ss[best_site].mv.col;
1128 best_address += ss[best_site].offset;
1129 last_site = best_site;
1131 else if (best_address == in_what)
1135 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1136 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1138 if (bestsad == INT_MAX)
1141 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1142 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1145 int vp8_diamond_search_sadx4
1155 vp8_variance_fn_ptr_t *fn_ptr,
1162 unsigned char *what = (*(b->base_src) + b->src);
1163 int what_stride = b->src_stride;
1164 unsigned char *in_what;
1165 int pre_stride = x->e_mbd.pre.y_stride;
1166 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1167 int in_what_stride = pre_stride;
1168 unsigned char *best_address;
1173 unsigned int bestsad = UINT_MAX;
1179 int this_row_offset;
1180 int this_col_offset;
1183 unsigned char *check_here;
1184 unsigned int thissad;
1186 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1188 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1189 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1191 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1192 ref_row = ref_mv->as_mv.row;
1193 ref_col = ref_mv->as_mv.col;
1195 best_mv->as_mv.row = ref_row;
1196 best_mv->as_mv.col = ref_col;
1198 // Work out the start point for the search
1199 in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col);
1200 best_address = in_what;
1202 // Check the starting position
1203 bestsad = fn_ptr->sdf(what, what_stride,
1204 in_what, in_what_stride, 0x7fffffff)
1205 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1207 // search_param determines the length of the initial step and hence the number of iterations
1208 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1209 ss = &x->ss[search_param * x->searches_per_step];
1210 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1214 for (step = 0; step < tot_steps ; step++)
1218 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1219 // checking 4 bounds for each points.
1220 all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
1221 all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
1222 all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
1223 all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
1227 unsigned int sad_array[4];
1229 for (j = 0 ; j < x->searches_per_step ; j += 4)
1231 unsigned char *block_offset[4];
1233 for (t = 0; t < 4; t++)
1234 block_offset[t] = ss[i+t].offset + best_address;
1236 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1238 for (t = 0; t < 4; t++, i++)
1240 if (sad_array[t] < bestsad)
1242 this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1243 this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1244 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
1245 mvsadcost, sad_per_bit);
1247 if (sad_array[t] < bestsad)
1249 bestsad = sad_array[t];
1258 for (j = 0 ; j < x->searches_per_step ; j++)
1260 // Trap illegal vectors
1261 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1262 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1264 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1265 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1267 check_here = ss[i].offset + best_address;
1268 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1270 if (thissad < bestsad)
1272 this_mv.as_mv.row = this_row_offset;
1273 this_mv.as_mv.col = this_col_offset;
1274 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1275 mvsadcost, sad_per_bit);
1277 if (thissad < bestsad)
1288 if (best_site != last_site)
1290 best_mv->as_mv.row += ss[best_site].mv.row;
1291 best_mv->as_mv.col += ss[best_site].mv.col;
1292 best_address += ss[best_site].offset;
1293 last_site = best_site;
1295 else if (best_address == in_what)
1299 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1300 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1302 if (bestsad == INT_MAX)
1305 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1306 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1309 int vp8_full_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1310 int sad_per_bit, int distance,
1311 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1314 unsigned char *what = (*(b->base_src) + b->src);
1315 int what_stride = b->src_stride;
1316 unsigned char *in_what;
1317 int pre_stride = x->e_mbd.pre.y_stride;
1318 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1319 int in_what_stride = pre_stride;
1320 int mv_stride = pre_stride;
1321 unsigned char *bestaddress;
1322 int_mv *best_mv = &d->bmi.mv;
1324 int bestsad = INT_MAX;
1327 unsigned char *check_here;
1330 int ref_row = ref_mv->as_mv.row;
1331 int ref_col = ref_mv->as_mv.col;
1333 int row_min = ref_row - distance;
1334 int row_max = ref_row + distance;
1335 int col_min = ref_col - distance;
1336 int col_max = ref_col + distance;
1338 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1340 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1341 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1343 // Work out the mid point for the search
1344 in_what = base_pre + d->offset;
1345 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1347 best_mv->as_mv.row = ref_row;
1348 best_mv->as_mv.col = ref_col;
1350 // Baseline value at the centre
1351 bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
1352 in_what_stride, 0x7fffffff)
1353 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1355 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1356 if (col_min < x->mv_col_min)
1357 col_min = x->mv_col_min;
1359 if (col_max > x->mv_col_max)
1360 col_max = x->mv_col_max;
1362 if (row_min < x->mv_row_min)
1363 row_min = x->mv_row_min;
1365 if (row_max > x->mv_row_max)
1366 row_max = x->mv_row_max;
1368 for (r = row_min; r < row_max ; r++)
1370 this_mv.as_mv.row = r;
1371 check_here = r * mv_stride + in_what + col_min;
1373 for (c = col_min; c < col_max; c++)
1375 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1377 this_mv.as_mv.col = c;
1378 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1379 mvsadcost, sad_per_bit);
1381 if (thissad < bestsad)
1384 best_mv->as_mv.row = r;
1385 best_mv->as_mv.col = c;
1386 bestaddress = check_here;
1393 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1394 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1396 if (bestsad < INT_MAX)
1397 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1398 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1403 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1404 int sad_per_bit, int distance,
1405 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1408 unsigned char *what = (*(b->base_src) + b->src);
1409 int what_stride = b->src_stride;
1410 unsigned char *in_what;
1411 int pre_stride = x->e_mbd.pre.y_stride;
1412 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1413 int in_what_stride = pre_stride;
1414 int mv_stride = pre_stride;
1415 unsigned char *bestaddress;
1416 int_mv *best_mv = &d->bmi.mv;
1418 unsigned int bestsad = UINT_MAX;
1421 unsigned char *check_here;
1422 unsigned int thissad;
1424 int ref_row = ref_mv->as_mv.row;
1425 int ref_col = ref_mv->as_mv.col;
1427 int row_min = ref_row - distance;
1428 int row_max = ref_row + distance;
1429 int col_min = ref_col - distance;
1430 int col_max = ref_col + distance;
1432 unsigned int sad_array[3];
1434 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1436 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1437 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1439 // Work out the mid point for the search
1440 in_what = base_pre + d->offset;
1441 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1443 best_mv->as_mv.row = ref_row;
1444 best_mv->as_mv.col = ref_col;
1446 // Baseline value at the centre
1447 bestsad = fn_ptr->sdf(what, what_stride,
1448 bestaddress, in_what_stride, 0x7fffffff)
1449 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1451 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1452 if (col_min < x->mv_col_min)
1453 col_min = x->mv_col_min;
1455 if (col_max > x->mv_col_max)
1456 col_max = x->mv_col_max;
1458 if (row_min < x->mv_row_min)
1459 row_min = x->mv_row_min;
1461 if (row_max > x->mv_row_max)
1462 row_max = x->mv_row_max;
1464 for (r = row_min; r < row_max ; r++)
1466 this_mv.as_mv.row = r;
1467 check_here = r * mv_stride + in_what + col_min;
1470 while ((c + 2) < col_max)
1474 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1476 for (i = 0; i < 3; i++)
1478 thissad = sad_array[i];
1480 if (thissad < bestsad)
1482 this_mv.as_mv.col = c;
1483 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1484 mvsadcost, sad_per_bit);
1486 if (thissad < bestsad)
1489 best_mv->as_mv.row = r;
1490 best_mv->as_mv.col = c;
1491 bestaddress = check_here;
1502 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1504 if (thissad < bestsad)
1506 this_mv.as_mv.col = c;
1507 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1508 mvsadcost, sad_per_bit);
1510 if (thissad < bestsad)
1513 best_mv->as_mv.row = r;
1514 best_mv->as_mv.col = c;
1515 bestaddress = check_here;
1525 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1526 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1528 if (bestsad < INT_MAX)
1529 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1530 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1535 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1536 int sad_per_bit, int distance,
1537 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1540 unsigned char *what = (*(b->base_src) + b->src);
1541 int what_stride = b->src_stride;
1542 int pre_stride = x->e_mbd.pre.y_stride;
1543 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1544 unsigned char *in_what;
1545 int in_what_stride = pre_stride;
1546 int mv_stride = pre_stride;
1547 unsigned char *bestaddress;
1548 int_mv *best_mv = &d->bmi.mv;
1550 unsigned int bestsad = UINT_MAX;
1553 unsigned char *check_here;
1554 unsigned int thissad;
1556 int ref_row = ref_mv->as_mv.row;
1557 int ref_col = ref_mv->as_mv.col;
1559 int row_min = ref_row - distance;
1560 int row_max = ref_row + distance;
1561 int col_min = ref_col - distance;
1562 int col_max = ref_col + distance;
1564 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1565 unsigned int sad_array[3];
1567 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1569 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1570 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1572 // Work out the mid point for the search
1573 in_what = base_pre + d->offset;
1574 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1576 best_mv->as_mv.row = ref_row;
1577 best_mv->as_mv.col = ref_col;
1579 // Baseline value at the centre
1580 bestsad = fn_ptr->sdf(what, what_stride,
1581 bestaddress, in_what_stride, 0x7fffffff)
1582 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1584 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1585 if (col_min < x->mv_col_min)
1586 col_min = x->mv_col_min;
1588 if (col_max > x->mv_col_max)
1589 col_max = x->mv_col_max;
1591 if (row_min < x->mv_row_min)
1592 row_min = x->mv_row_min;
1594 if (row_max > x->mv_row_max)
1595 row_max = x->mv_row_max;
1597 for (r = row_min; r < row_max ; r++)
1599 this_mv.as_mv.row = r;
1600 check_here = r * mv_stride + in_what + col_min;
1603 while ((c + 7) < col_max)
1607 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1609 for (i = 0; i < 8; i++)
1611 thissad = (unsigned int)sad_array8[i];
1613 if (thissad < bestsad)
1615 this_mv.as_mv.col = c;
1616 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1617 mvsadcost, sad_per_bit);
1619 if (thissad < bestsad)
1622 best_mv->as_mv.row = r;
1623 best_mv->as_mv.col = c;
1624 bestaddress = check_here;
1633 while ((c + 2) < col_max)
1637 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1639 for (i = 0; i < 3; i++)
1641 thissad = sad_array[i];
1643 if (thissad < bestsad)
1645 this_mv.as_mv.col = c;
1646 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1647 mvsadcost, sad_per_bit);
1649 if (thissad < bestsad)
1652 best_mv->as_mv.row = r;
1653 best_mv->as_mv.col = c;
1654 bestaddress = check_here;
1665 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1667 if (thissad < bestsad)
1669 this_mv.as_mv.col = c;
1670 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1671 mvsadcost, sad_per_bit);
1673 if (thissad < bestsad)
1676 best_mv->as_mv.row = r;
1677 best_mv->as_mv.col = c;
1678 bestaddress = check_here;
1687 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1688 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1690 if (bestsad < INT_MAX)
1691 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1692 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1697 int vp8_refining_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1698 int error_per_bit, int search_range,
1699 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1702 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1704 short this_row_offset, this_col_offset;
1706 int what_stride = b->src_stride;
1707 int pre_stride = x->e_mbd.pre.y_stride;
1708 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1709 int in_what_stride = pre_stride;
1710 unsigned char *what = (*(b->base_src) + b->src);
1711 unsigned char *best_address = (unsigned char *)(base_pre + d->offset +
1712 (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1713 unsigned char *check_here;
1714 unsigned int thissad;
1716 unsigned int bestsad = INT_MAX;
1718 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1721 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1722 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1724 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1726 for (i=0; i<search_range; i++)
1730 for (j = 0 ; j < 4 ; j++)
1732 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1733 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1735 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1736 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1738 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1739 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1741 if (thissad < bestsad)
1743 this_mv.as_mv.row = this_row_offset;
1744 this_mv.as_mv.col = this_col_offset;
1745 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1747 if (thissad < bestsad)
1756 if (best_site == -1)
1760 ref_mv->as_mv.row += neighbors[best_site].row;
1761 ref_mv->as_mv.col += neighbors[best_site].col;
1762 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1766 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1767 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1769 if (bestsad < INT_MAX)
1770 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1771 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1776 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1777 int_mv *ref_mv, int error_per_bit,
1778 int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1779 int *mvcost[2], int_mv *center_mv)
1781 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1783 short this_row_offset, this_col_offset;
1785 int what_stride = b->src_stride;
1786 int pre_stride = x->e_mbd.pre.y_stride;
1787 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1788 int in_what_stride = pre_stride;
1789 unsigned char *what = (*(b->base_src) + b->src);
1790 unsigned char *best_address = (unsigned char *)(base_pre + d->offset +
1791 (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1792 unsigned char *check_here;
1793 unsigned int thissad;
1795 unsigned int bestsad = INT_MAX;
1797 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1800 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1801 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1803 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1805 for (i=0; i<search_range; i++)
1810 all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1811 all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1812 all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1813 all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1817 unsigned int sad_array[4];
1818 unsigned char *block_offset[4];
1819 block_offset[0] = best_address - in_what_stride;
1820 block_offset[1] = best_address - 1;
1821 block_offset[2] = best_address + 1;
1822 block_offset[3] = best_address + in_what_stride;
1824 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1826 for (j = 0; j < 4; j++)
1828 if (sad_array[j] < bestsad)
1830 this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1831 this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1832 sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1834 if (sad_array[j] < bestsad)
1836 bestsad = sad_array[j];
1844 for (j = 0 ; j < 4 ; j++)
1846 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1847 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1849 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1850 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1852 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1853 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1855 if (thissad < bestsad)
1857 this_mv.as_mv.row = this_row_offset;
1858 this_mv.as_mv.col = this_col_offset;
1859 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1861 if (thissad < bestsad)
1871 if (best_site == -1)
1875 ref_mv->as_mv.row += neighbors[best_site].row;
1876 ref_mv->as_mv.col += neighbors[best_site].col;
1877 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1881 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1882 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1884 if (bestsad < INT_MAX)
1885 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1886 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1891 #ifdef ENTROPY_STATS
1892 void print_mode_context(void)
1894 FILE *f = fopen("modecont.c", "w");
1897 fprintf(f, "#include \"entropy.h\"\n");
1898 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1901 for (j = 0; j < 6; j++)
1903 fprintf(f, " { // %d \n", j);
1906 for (i = 0; i < 4; i++)
1910 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1913 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1916 overal_prob = 256 * mv_mode_cts[i][0] / count;
1920 if (overal_prob == 0)
1924 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1927 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1934 fprintf(f, "%5d, ", this_prob);
1935 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1936 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1939 fprintf(f, " },\n");
1946 /* MV ref count ENTROPY_STATS stats code */
1947 #ifdef ENTROPY_STATS
1948 void init_mv_ref_counts()
1950 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1951 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1954 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1958 ++mv_ref_ct [ct[0]] [0] [0];
1959 ++mv_mode_cts[0][0];
1963 ++mv_ref_ct [ct[0]] [0] [1];
1964 ++mv_mode_cts[0][1];
1968 ++mv_ref_ct [ct[1]] [1] [0];
1969 ++mv_mode_cts[1][0];
1973 ++mv_ref_ct [ct[1]] [1] [1];
1974 ++mv_mode_cts[1][1];
1978 ++mv_ref_ct [ct[2]] [2] [0];
1979 ++mv_mode_cts[2][0];
1983 ++mv_ref_ct [ct[2]] [2] [1];
1984 ++mv_mode_cts[2][1];
1988 ++mv_ref_ct [ct[3]] [3] [0];
1989 ++mv_mode_cts[3][0];
1993 ++mv_ref_ct [ct[3]] [3] [1];
1994 ++mv_mode_cts[3][1];
2001 #endif/* END MV ref count ENTROPY_STATS stats code */