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.
15 #include "./vpx_config.h"
17 #include "vpx_mem/vpx_mem.h"
19 #include "vp9/common/vp9_common.h"
21 #include "vp9/encoder/vp9_encoder.h"
22 #include "vp9/encoder/vp9_mcomp.h"
24 // #define NEW_DIAMOND_SEARCH
26 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
28 return &buf->buf[mv->row * buf->stride + mv->col];
31 void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
32 int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
33 int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
34 int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
35 int row_max = (mv->row >> 3) + MAX_FULL_PEL_VAL;
37 col_min = MAX(col_min, (MV_LOW >> 3) + 1);
38 row_min = MAX(row_min, (MV_LOW >> 3) + 1);
39 col_max = MIN(col_max, (MV_UPP >> 3) - 1);
40 row_max = MIN(row_max, (MV_UPP >> 3) - 1);
42 // Get intersection of UMV window and valid MV window to reduce # of checks
44 if (x->mv_col_min < col_min)
45 x->mv_col_min = col_min;
46 if (x->mv_col_max > col_max)
47 x->mv_col_max = col_max;
48 if (x->mv_row_min < row_min)
49 x->mv_row_min = row_min;
50 if (x->mv_row_max > row_max)
51 x->mv_row_max = row_max;
54 int vp9_init_search_range(int size) {
56 // Minimum search size no matter what the passed in value.
59 while ((size << sr) < MAX_FULL_PEL_VAL)
62 sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
66 static INLINE int mv_cost(const MV *mv,
67 const int *joint_cost, int *const comp_cost[2]) {
68 return joint_cost[vp9_get_mv_joint(mv)] +
69 comp_cost[0][mv->row] + comp_cost[1][mv->col];
72 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
73 const int *mvjcost, int *mvcost[2], int weight) {
74 const MV diff = { mv->row - ref->row,
76 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
79 static int mv_err_cost(const MV *mv, const MV *ref,
80 const int *mvjcost, int *mvcost[2],
83 const MV diff = { mv->row - ref->row,
85 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
91 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
93 const MV diff = { mv->row - ref->row,
95 return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
96 x->nmvsadcost) * error_per_bit, 8);
99 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride) {
100 int len, ss_count = 1;
102 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
103 cfg->ss[0].offset = 0;
105 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
106 // Generate offsets for 4 search sites per step.
107 const MV ss_mvs[] = {{-len, 0}, {len, 0}, {0, -len}, {0, len}};
109 for (i = 0; i < 4; ++i) {
110 search_site *const ss = &cfg->ss[ss_count++];
112 ss->offset = ss->mv.row * stride + ss->mv.col;
116 cfg->ss_count = ss_count;
117 cfg->searches_per_step = 4;
120 void vp9_init3smotion_compensation(search_site_config *cfg, int stride) {
121 int len, ss_count = 1;
123 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
124 cfg->ss[0].offset = 0;
126 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
127 // Generate offsets for 8 search sites per step.
128 const MV ss_mvs[8] = {
129 {-len, 0 }, {len, 0 }, { 0, -len}, {0, len},
130 {-len, -len}, {-len, len}, {len, -len}, {len, len}
133 for (i = 0; i < 8; ++i) {
134 search_site *const ss = &cfg->ss[ss_count++];
136 ss->offset = ss->mv.row * stride + ss->mv.col;
140 cfg->ss_count = ss_count;
141 cfg->searches_per_step = 8;
145 * To avoid the penalty for crossing cache-line read, preload the reference
146 * area in a small buffer, which is aligned to make sure there won't be crossing
147 * cache-line read while reading from this buffer. This reduced the cpu
148 * cycles spent on reading ref data in sub-pixel filter functions.
149 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
150 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
151 * could reduce the area.
154 /* estimated cost of a motion vector (r,c) */
157 ((mvjcost[((r) != rr) * 2 + ((c) != rc)] + \
158 mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
159 error_per_bit + 4096) >> 13 : 0)
162 // convert motion vector component to offset for svf calc
163 static INLINE int sp(int x) {
167 static INLINE const uint8_t *pre(const uint8_t *buf, int stride, int r, int c) {
168 return &buf[(r >> 3) * stride + (c >> 3)];
171 /* checks if (r, c) has better score than previous best */
172 #define CHECK_BETTER(v, r, c) \
173 if (c >= minc && c <= maxc && r >= minr && r <= maxr) { \
174 if (second_pred == NULL) \
175 thismse = vfp->svf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), z, \
178 thismse = vfp->svaf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), \
179 z, src_stride, &sse, second_pred); \
180 if ((v = MVC(r, c) + thismse) < besterr) { \
184 *distortion = thismse; \
191 #define FIRST_LEVEL_CHECKS \
193 unsigned int left, right, up, down, diag; \
194 CHECK_BETTER(left, tr, tc - hstep); \
195 CHECK_BETTER(right, tr, tc + hstep); \
196 CHECK_BETTER(up, tr - hstep, tc); \
197 CHECK_BETTER(down, tr + hstep, tc); \
198 whichdir = (left < right ? 0 : 1) + \
199 (up < down ? 0 : 2); \
200 switch (whichdir) { \
202 CHECK_BETTER(diag, tr - hstep, tc - hstep); \
205 CHECK_BETTER(diag, tr - hstep, tc + hstep); \
208 CHECK_BETTER(diag, tr + hstep, tc - hstep); \
211 CHECK_BETTER(diag, tr + hstep, tc + hstep); \
216 #define SECOND_LEVEL_CHECKS \
219 unsigned int second; \
220 if (tr != br && tc != bc) { \
223 CHECK_BETTER(second, tr + kr, tc + 2 * kc); \
224 CHECK_BETTER(second, tr + 2 * kr, tc + kc); \
225 } else if (tr == br && tc != bc) { \
227 CHECK_BETTER(second, tr + hstep, tc + 2 * kc); \
228 CHECK_BETTER(second, tr - hstep, tc + 2 * kc); \
229 switch (whichdir) { \
232 CHECK_BETTER(second, tr + hstep, tc + kc); \
236 CHECK_BETTER(second, tr - hstep, tc + kc); \
239 } else if (tr != br && tc == bc) { \
241 CHECK_BETTER(second, tr + 2 * kr, tc + hstep); \
242 CHECK_BETTER(second, tr + 2 * kr, tc - hstep); \
243 switch (whichdir) { \
246 CHECK_BETTER(second, tr + kr, tc + hstep); \
250 CHECK_BETTER(second, tr + kr, tc - hstep); \
256 #define SETUP_SUBPEL_SEARCH \
257 const uint8_t *const z = x->plane[0].src.buf; \
258 const int src_stride = x->plane[0].src.stride; \
259 const MACROBLOCKD *xd = &x->e_mbd; \
260 unsigned int besterr = INT_MAX; \
262 unsigned int whichdir; \
264 const unsigned int halfiters = iters_per_step; \
265 const unsigned int quarteriters = iters_per_step; \
266 const unsigned int eighthiters = iters_per_step; \
267 const int y_stride = xd->plane[0].pre[0].stride; \
268 const int offset = bestmv->row * y_stride + bestmv->col; \
269 const uint8_t *const y = xd->plane[0].pre[0].buf; \
271 int rr = ref_mv->row; \
272 int rc = ref_mv->col; \
273 int br = bestmv->row * 8; \
274 int bc = bestmv->col * 8; \
276 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX); \
277 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX); \
278 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX); \
279 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX); \
286 static INLINE unsigned int setup_center_error(const MACROBLOCKD *xd,
290 const vp9_variance_fn_ptr_t *vfp,
291 const uint8_t *const src,
292 const int src_stride,
293 const uint8_t *const y,
295 const uint8_t *second_pred,
296 int w, int h, int offset,
297 int *mvjcost, int *mvcost[2],
300 unsigned int besterr;
301 #if CONFIG_VP9_HIGHBITDEPTH
302 if (second_pred != NULL) {
303 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
304 DECLARE_ALIGNED(16, uint16_t, comp_pred16[64 * 64]);
305 vp9_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset,
307 besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, src, src_stride,
310 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
311 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
312 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
315 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
317 *distortion = besterr;
318 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
321 if (second_pred != NULL) {
322 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
323 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
324 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
326 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
328 *distortion = besterr;
329 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
330 #endif // CONFIG_VP9_HIGHBITDEPTH
334 static INLINE int divide_and_round(const int n, const int d) {
335 return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
338 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
339 return cost_list[0] < cost_list[1] &&
340 cost_list[0] < cost_list[2] &&
341 cost_list[0] < cost_list[3] &&
342 cost_list[0] < cost_list[4];
345 // Returns surface minima estimate at given precision in 1/2^n bits.
346 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
347 // For a given set of costs S0, S1, S2, S3, S4 at points
348 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
349 // the solution for the location of the minima (x0, y0) is given by:
350 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
351 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
352 // The code below is an integerized version of that.
353 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
355 *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
356 (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
357 *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
358 (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
361 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
363 MV *bestmv, const MV *ref_mv,
366 const vp9_variance_fn_ptr_t *vfp,
370 int *mvjcost, int *mvcost[2],
373 const uint8_t *second_pred,
376 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
377 z, src_stride, y, y_stride, second_pred,
378 w, h, offset, mvjcost, mvcost,
389 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
390 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
391 cost_list[4] != INT_MAX &&
392 is_cost_list_wellbehaved(cost_list)) {
395 get_cost_surf_min(cost_list, &ir, &ic, 2);
396 if (ir != 0 || ic != 0) {
397 CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
408 // Each subsequent iteration checks at least one point in common with
409 // the last iteration could be 2 ( if diag selected) 1/4 pel
410 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
411 if (forced_stop != 2) {
414 if (quarteriters > 1) {
423 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
426 if (eighthiters > 1) {
434 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
435 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
441 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
442 MV *bestmv, const MV *ref_mv,
445 const vp9_variance_fn_ptr_t *vfp,
449 int *mvjcost, int *mvcost[2],
452 const uint8_t *second_pred,
455 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
456 z, src_stride, y, y_stride, second_pred,
457 w, h, offset, mvjcost, mvcost,
460 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
461 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
462 cost_list[4] != INT_MAX &&
463 is_cost_list_wellbehaved(cost_list)) {
466 get_cost_surf_min(cost_list, &ir, &ic, 1);
467 if (ir != 0 || ic != 0) {
468 CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
477 // Each subsequent iteration checks at least one point in common with
478 // the last iteration could be 2 ( if diag selected) 1/4 pel
480 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
481 if (forced_stop != 2) {
486 if (quarteriters > 1) {
491 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
496 if (eighthiters > 1) {
500 // These lines insure static analysis doesn't warn that
501 // tr and tc aren't used after the above point.
508 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
509 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
515 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
516 MV *bestmv, const MV *ref_mv,
519 const vp9_variance_fn_ptr_t *vfp,
523 int *mvjcost, int *mvcost[2],
526 const uint8_t *second_pred,
529 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
530 z, src_stride, y, y_stride, second_pred,
531 w, h, offset, mvjcost, mvcost,
534 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
535 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
536 cost_list[4] != INT_MAX) {
537 unsigned int left, right, up, down, diag;
538 whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
539 (cost_list[2] < cost_list[4] ? 0 : 2);
542 CHECK_BETTER(left, tr, tc - hstep);
543 CHECK_BETTER(down, tr + hstep, tc);
544 CHECK_BETTER(diag, tr + hstep, tc - hstep);
547 CHECK_BETTER(right, tr, tc + hstep);
548 CHECK_BETTER(down, tr + hstep, tc);
549 CHECK_BETTER(diag, tr + hstep, tc + hstep);
552 CHECK_BETTER(left, tr, tc - hstep);
553 CHECK_BETTER(up, tr - hstep, tc);
554 CHECK_BETTER(diag, tr - hstep, tc - hstep);
557 CHECK_BETTER(right, tr, tc + hstep);
558 CHECK_BETTER(up, tr - hstep, tc);
559 CHECK_BETTER(diag, tr - hstep, tc + hstep);
572 // Each subsequent iteration checks at least one point in common with
573 // the last iteration could be 2 ( if diag selected) 1/4 pel
575 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
576 if (forced_stop != 2) {
579 if (quarteriters > 1) {
586 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
589 if (eighthiters > 1) {
595 // These lines insure static analysis doesn't warn that
596 // tr and tc aren't used after the above point.
603 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
604 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
610 const MV search_step_table[12] = {
611 // left, right, up, down
612 {0, -4}, {0, 4}, {-4, 0}, {4, 0},
613 {0, -2}, {0, 2}, {-2, 0}, {2, 0},
614 {0, -1}, {0, 1}, {-1, 0}, {1, 0}
617 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
618 MV *bestmv, const MV *ref_mv,
621 const vp9_variance_fn_ptr_t *vfp,
625 int *mvjcost, int *mvcost[2],
628 const uint8_t *second_pred,
630 const uint8_t *const z = x->plane[0].src.buf;
631 const uint8_t *const src_address = z;
632 const int src_stride = x->plane[0].src.stride;
633 const MACROBLOCKD *xd = &x->e_mbd;
634 unsigned int besterr = INT_MAX;
636 unsigned int whichdir = 0;
638 const int y_stride = xd->plane[0].pre[0].stride;
639 const int offset = bestmv->row * y_stride + bestmv->col;
640 const uint8_t *const y = xd->plane[0].pre[0].buf;
642 int rr = ref_mv->row;
643 int rc = ref_mv->col;
644 int br = bestmv->row * 8;
645 int bc = bestmv->col * 8;
647 int iter, round = 3 - forced_stop;
648 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX);
649 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX);
650 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX);
651 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX);
654 const MV *search_step = search_step_table;
655 int idx, best_idx = -1;
656 unsigned int cost_array[5];
658 if (!(allow_hp && vp9_use_mv_hp(ref_mv)))
665 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
666 z, src_stride, y, y_stride, second_pred,
667 w, h, offset, mvjcost, mvcost,
670 (void) cost_list; // to silence compiler warning
672 for (iter = 0; iter < round; ++iter) {
673 // Check vertical and horizontal sub-pixel positions.
674 for (idx = 0; idx < 4; ++idx) {
675 tr = br + search_step[idx].row;
676 tc = bc + search_step[idx].col;
677 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
678 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
679 int row_offset = (tr & 0x07) << 1;
680 int col_offset = (tc & 0x07) << 1;
684 if (second_pred == NULL)
685 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
686 src_address, src_stride, &sse);
688 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
689 src_address, src_stride, &sse, second_pred);
690 cost_array[idx] = thismse +
691 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
693 if (cost_array[idx] < besterr) {
695 besterr = cost_array[idx];
696 *distortion = thismse;
700 cost_array[idx] = INT_MAX;
704 // Check diagonal sub-pixel position
705 tc = bc + (cost_array[0] < cost_array[1] ? -hstep : hstep);
706 tr = br + (cost_array[2] < cost_array[3] ? -hstep : hstep);
707 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
708 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
709 int row_offset = (tr & 0x07) << 1;
710 int col_offset = (tc & 0x07) << 1;
711 MV this_mv = {tr, tc};
712 if (second_pred == NULL)
713 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
714 src_address, src_stride, &sse);
716 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
717 src_address, src_stride, &sse, second_pred);
718 cost_array[4] = thismse +
719 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
721 if (cost_array[4] < besterr) {
723 besterr = cost_array[4];
724 *distortion = thismse;
728 cost_array[idx] = INT_MAX;
731 if (best_idx < 4 && best_idx >= 0) {
732 br += search_step[best_idx].row;
733 bc += search_step[best_idx].col;
734 } else if (best_idx == 4) {
739 if (iters_per_step > 1)
750 // Each subsequent iteration checks at least one point in common with
751 // the last iteration could be 2 ( if diag selected) 1/4 pel
753 // These lines insure static analysis doesn't warn that
754 // tr and tc aren't used after the above point.
761 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
762 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
772 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
774 return ((row - range) >= x->mv_row_min) &
775 ((row + range) <= x->mv_row_max) &
776 ((col - range) >= x->mv_col_min) &
777 ((col + range) <= x->mv_col_max);
780 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
781 return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
782 (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
785 #define CHECK_BETTER \
787 if (thissad < bestsad) {\
789 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
790 if (thissad < bestsad) {\
797 #define MAX_PATTERN_SCALES 11
798 #define MAX_PATTERN_CANDIDATES 8 // max number of canddiates per scale
799 #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
801 // Calculate and return a sad+mvcost list around an integer best pel.
802 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
805 const vp9_variance_fn_ptr_t *fn_ptr,
808 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
809 const struct buf_2d *const what = &x->plane[0].src;
810 const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
811 const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
812 int br = best_mv->row;
813 int bc = best_mv->col;
820 cost_list[0] = fn_ptr->vf(what->buf, what->stride,
821 get_buf_from_mv(in_what, &this_mv),
822 in_what->stride, &sse) +
823 mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
824 if (check_bounds(x, br, bc, 1)) {
825 for (i = 0; i < 4; i++) {
826 const MV this_mv = {br + neighbors[i].row,
827 bc + neighbors[i].col};
828 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
829 get_buf_from_mv(in_what, &this_mv),
830 in_what->stride, &sse) +
831 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
832 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
836 for (i = 0; i < 4; i++) {
837 const MV this_mv = {br + neighbors[i].row,
838 bc + neighbors[i].col};
839 if (!is_mv_in(x, &this_mv))
840 cost_list[i + 1] = INT_MAX;
842 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
843 get_buf_from_mv(in_what, &this_mv),
844 in_what->stride, &sse) +
845 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
846 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
852 // Generic pattern search function that searches over multiple scales.
853 // Each scale can have a different number of candidates and shape of
854 // candidates as indicated in the num_candidates and candidates arrays
855 // passed into this function
857 static int vp9_pattern_search(const MACROBLOCK *x,
863 const vp9_variance_fn_ptr_t *vfp,
867 const int num_candidates[MAX_PATTERN_SCALES],
868 const MV candidates[MAX_PATTERN_SCALES]
869 [MAX_PATTERN_CANDIDATES]) {
870 const MACROBLOCKD *const xd = &x->e_mbd;
871 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
872 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
875 const struct buf_2d *const what = &x->plane[0].src;
876 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
878 int bestsad = INT_MAX;
881 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
882 int best_init_s = search_param_to_steps[search_param];
883 // adjust ref_mv to make sure it is within MV range
884 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
888 // Work out the start point for the search
889 bestsad = vfp->sdf(what->buf, what->stride,
890 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
891 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
893 // Search all possible scales upto the search param around the center point
894 // pick the scale of the point that is best as the starting scale of
895 // further steps around it.
896 if (do_init_search) {
899 for (t = 0; t <= s; ++t) {
901 if (check_bounds(x, br, bc, 1 << t)) {
902 for (i = 0; i < num_candidates[t]; i++) {
903 const MV this_mv = {br + candidates[t][i].row,
904 bc + candidates[t][i].col};
905 thissad = vfp->sdf(what->buf, what->stride,
906 get_buf_from_mv(in_what, &this_mv),
911 for (i = 0; i < num_candidates[t]; i++) {
912 const MV this_mv = {br + candidates[t][i].row,
913 bc + candidates[t][i].col};
914 if (!is_mv_in(x, &this_mv))
916 thissad = vfp->sdf(what->buf, what->stride,
917 get_buf_from_mv(in_what, &this_mv),
922 if (best_site == -1) {
929 if (best_init_s != -1) {
930 br += candidates[best_init_s][k].row;
931 bc += candidates[best_init_s][k].col;
935 // If the center point is still the best, just skip this and move to
936 // the refinement step.
937 if (best_init_s != -1) {
942 // No need to search all 6 points the 1st time if initial search was used
943 if (!do_init_search || s != best_init_s) {
944 if (check_bounds(x, br, bc, 1 << s)) {
945 for (i = 0; i < num_candidates[s]; i++) {
946 const MV this_mv = {br + candidates[s][i].row,
947 bc + candidates[s][i].col};
948 thissad = vfp->sdf(what->buf, what->stride,
949 get_buf_from_mv(in_what, &this_mv),
954 for (i = 0; i < num_candidates[s]; i++) {
955 const MV this_mv = {br + candidates[s][i].row,
956 bc + candidates[s][i].col};
957 if (!is_mv_in(x, &this_mv))
959 thissad = vfp->sdf(what->buf, what->stride,
960 get_buf_from_mv(in_what, &this_mv),
966 if (best_site == -1) {
969 br += candidates[s][best_site].row;
970 bc += candidates[s][best_site].col;
976 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
978 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
979 next_chkpts_indices[1] = k;
980 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
982 if (check_bounds(x, br, bc, 1 << s)) {
983 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
984 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
985 bc + candidates[s][next_chkpts_indices[i]].col};
986 thissad = vfp->sdf(what->buf, what->stride,
987 get_buf_from_mv(in_what, &this_mv),
992 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
993 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
994 bc + candidates[s][next_chkpts_indices[i]].col};
995 if (!is_mv_in(x, &this_mv))
997 thissad = vfp->sdf(what->buf, what->stride,
998 get_buf_from_mv(in_what, &this_mv),
1004 if (best_site != -1) {
1005 k = next_chkpts_indices[best_site];
1006 br += candidates[s][k].row;
1007 bc += candidates[s][k].col;
1009 } while (best_site != -1);
1013 // Returns the one-away integer pel sad values around the best as follows:
1014 // cost_list[0]: cost at the best integer pel
1015 // cost_list[1]: cost at delta {0, -1} (left) from the best integer pel
1016 // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
1017 // cost_list[3]: cost at delta { 0, 1} (right) from the best integer pel
1018 // cost_list[4]: cost at delta {-1, 0} (top) from the best integer pel
1020 const MV best_mv = { br, bc };
1021 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1028 // A specialized function where the smallest scale search candidates
1029 // are 4 1-away neighbors, and cost_list is non-null
1030 // TODO(debargha): Merge this function with the one above. Also remove
1031 // use_mvcost option since it is always 1, to save unnecessary branches.
1032 static int vp9_pattern_search_sad(const MACROBLOCK *x,
1038 const vp9_variance_fn_ptr_t *vfp,
1040 const MV *center_mv,
1042 const int num_candidates[MAX_PATTERN_SCALES],
1043 const MV candidates[MAX_PATTERN_SCALES]
1044 [MAX_PATTERN_CANDIDATES]) {
1045 const MACROBLOCKD *const xd = &x->e_mbd;
1046 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
1047 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1050 const struct buf_2d *const what = &x->plane[0].src;
1051 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1053 int bestsad = INT_MAX;
1056 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1057 int best_init_s = search_param_to_steps[search_param];
1058 // adjust ref_mv to make sure it is within MV range
1059 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1062 if (cost_list != NULL) {
1063 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1067 // Work out the start point for the search
1068 bestsad = vfp->sdf(what->buf, what->stride,
1069 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1070 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1072 // Search all possible scales upto the search param around the center point
1073 // pick the scale of the point that is best as the starting scale of
1074 // further steps around it.
1075 if (do_init_search) {
1078 for (t = 0; t <= s; ++t) {
1080 if (check_bounds(x, br, bc, 1 << t)) {
1081 for (i = 0; i < num_candidates[t]; i++) {
1082 const MV this_mv = {br + candidates[t][i].row,
1083 bc + candidates[t][i].col};
1084 thissad = vfp->sdf(what->buf, what->stride,
1085 get_buf_from_mv(in_what, &this_mv),
1090 for (i = 0; i < num_candidates[t]; i++) {
1091 const MV this_mv = {br + candidates[t][i].row,
1092 bc + candidates[t][i].col};
1093 if (!is_mv_in(x, &this_mv))
1095 thissad = vfp->sdf(what->buf, what->stride,
1096 get_buf_from_mv(in_what, &this_mv),
1101 if (best_site == -1) {
1108 if (best_init_s != -1) {
1109 br += candidates[best_init_s][k].row;
1110 bc += candidates[best_init_s][k].col;
1114 // If the center point is still the best, just skip this and move to
1115 // the refinement step.
1116 if (best_init_s != -1) {
1117 int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1121 for (; s >= do_sad; s--) {
1122 if (!do_init_search || s != best_init_s) {
1123 if (check_bounds(x, br, bc, 1 << s)) {
1124 for (i = 0; i < num_candidates[s]; i++) {
1125 const MV this_mv = {br + candidates[s][i].row,
1126 bc + candidates[s][i].col};
1127 thissad = vfp->sdf(what->buf, what->stride,
1128 get_buf_from_mv(in_what, &this_mv),
1133 for (i = 0; i < num_candidates[s]; i++) {
1134 const MV this_mv = {br + candidates[s][i].row,
1135 bc + candidates[s][i].col};
1136 if (!is_mv_in(x, &this_mv))
1138 thissad = vfp->sdf(what->buf, what->stride,
1139 get_buf_from_mv(in_what, &this_mv),
1145 if (best_site == -1) {
1148 br += candidates[s][best_site].row;
1149 bc += candidates[s][best_site].col;
1155 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1157 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1158 next_chkpts_indices[1] = k;
1159 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1161 if (check_bounds(x, br, bc, 1 << s)) {
1162 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1163 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1164 bc + candidates[s][next_chkpts_indices[i]].col};
1165 thissad = vfp->sdf(what->buf, what->stride,
1166 get_buf_from_mv(in_what, &this_mv),
1171 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1172 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1173 bc + candidates[s][next_chkpts_indices[i]].col};
1174 if (!is_mv_in(x, &this_mv))
1176 thissad = vfp->sdf(what->buf, what->stride,
1177 get_buf_from_mv(in_what, &this_mv),
1183 if (best_site != -1) {
1184 k = next_chkpts_indices[best_site];
1185 br += candidates[s][k].row;
1186 bc += candidates[s][k].col;
1188 } while (best_site != -1);
1191 // Note: If we enter the if below, then cost_list must be non-NULL.
1193 cost_list[0] = bestsad;
1194 if (!do_init_search || s != best_init_s) {
1195 if (check_bounds(x, br, bc, 1 << s)) {
1196 for (i = 0; i < num_candidates[s]; i++) {
1197 const MV this_mv = {br + candidates[s][i].row,
1198 bc + candidates[s][i].col};
1200 thissad = vfp->sdf(what->buf, what->stride,
1201 get_buf_from_mv(in_what, &this_mv),
1206 for (i = 0; i < num_candidates[s]; i++) {
1207 const MV this_mv = {br + candidates[s][i].row,
1208 bc + candidates[s][i].col};
1209 if (!is_mv_in(x, &this_mv))
1212 thissad = vfp->sdf(what->buf, what->stride,
1213 get_buf_from_mv(in_what, &this_mv),
1219 if (best_site != -1) {
1220 br += candidates[s][best_site].row;
1221 bc += candidates[s][best_site].col;
1225 while (best_site != -1) {
1226 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1228 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1229 next_chkpts_indices[1] = k;
1230 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1231 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1232 cost_list[((k + 2) % 4) + 1] = cost_list[0];
1233 cost_list[0] = bestsad;
1235 if (check_bounds(x, br, bc, 1 << s)) {
1236 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1237 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1238 bc + candidates[s][next_chkpts_indices[i]].col};
1239 cost_list[next_chkpts_indices[i] + 1] =
1240 thissad = vfp->sdf(what->buf, what->stride,
1241 get_buf_from_mv(in_what, &this_mv),
1246 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1247 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1248 bc + candidates[s][next_chkpts_indices[i]].col};
1249 if (!is_mv_in(x, &this_mv)) {
1250 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1253 cost_list[next_chkpts_indices[i] + 1] =
1254 thissad = vfp->sdf(what->buf, what->stride,
1255 get_buf_from_mv(in_what, &this_mv),
1261 if (best_site != -1) {
1262 k = next_chkpts_indices[best_site];
1263 br += candidates[s][k].row;
1264 bc += candidates[s][k].col;
1270 // Returns the one-away integer pel sad values around the best as follows:
1271 // cost_list[0]: sad at the best integer pel
1272 // cost_list[1]: sad at delta {0, -1} (left) from the best integer pel
1273 // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1274 // cost_list[3]: sad at delta { 0, 1} (right) from the best integer pel
1275 // cost_list[4]: sad at delta {-1, 0} (top) from the best integer pel
1277 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1278 if (cost_list[0] == INT_MAX) {
1279 cost_list[0] = bestsad;
1280 if (check_bounds(x, br, bc, 1)) {
1281 for (i = 0; i < 4; i++) {
1282 const MV this_mv = { br + neighbors[i].row,
1283 bc + neighbors[i].col };
1284 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1285 get_buf_from_mv(in_what, &this_mv),
1289 for (i = 0; i < 4; i++) {
1290 const MV this_mv = {br + neighbors[i].row,
1291 bc + neighbors[i].col};
1292 if (!is_mv_in(x, &this_mv))
1293 cost_list[i + 1] = INT_MAX;
1295 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1296 get_buf_from_mv(in_what, &this_mv),
1302 for (i = 0; i < 4; i++) {
1303 const MV this_mv = {br + neighbors[i].row,
1304 bc + neighbors[i].col};
1305 if (cost_list[i + 1] != INT_MAX) {
1307 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1318 int vp9_get_mvpred_var(const MACROBLOCK *x,
1319 const MV *best_mv, const MV *center_mv,
1320 const vp9_variance_fn_ptr_t *vfp,
1322 const MACROBLOCKD *const xd = &x->e_mbd;
1323 const struct buf_2d *const what = &x->plane[0].src;
1324 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1325 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1326 unsigned int unused;
1328 return vfp->vf(what->buf, what->stride,
1329 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1330 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1331 x->mvcost, x->errorperbit) : 0);
1334 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1335 const MV *best_mv, const MV *center_mv,
1336 const uint8_t *second_pred,
1337 const vp9_variance_fn_ptr_t *vfp,
1339 const MACROBLOCKD *const xd = &x->e_mbd;
1340 const struct buf_2d *const what = &x->plane[0].src;
1341 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1342 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1343 unsigned int unused;
1345 return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1346 what->buf, what->stride, &unused, second_pred) +
1347 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1348 x->mvcost, x->errorperbit) : 0);
1351 int vp9_hex_search(const MACROBLOCK *x,
1357 const vp9_variance_fn_ptr_t *vfp,
1359 const MV *center_mv, MV *best_mv) {
1360 // First scale has 8-closest points, the rest have 6 points in hex shape
1361 // at increasing scales
1362 static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1363 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1365 // Note that the largest candidate step at each scale is 2^scale
1366 static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1367 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1368 {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1369 {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1370 {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1371 {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1372 {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1373 {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1374 {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1375 {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1376 {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1377 {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1380 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1381 do_init_search, cost_list, vfp, use_mvcost,
1383 hex_num_candidates, hex_candidates);
1386 int vp9_bigdia_search(const MACROBLOCK *x,
1392 const vp9_variance_fn_ptr_t *vfp,
1394 const MV *center_mv,
1396 // First scale has 4-closest points, the rest have 8 points in diamond
1397 // shape at increasing scales
1398 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1399 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1401 // Note that the largest candidate step at each scale is 2^scale
1402 static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1403 [MAX_PATTERN_CANDIDATES] = {
1404 {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1405 {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1406 {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1407 {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1408 {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1409 {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1410 {-16, 16}, {-32, 0}},
1411 {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1412 {-32, 32}, {-64, 0}},
1413 {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1414 {-64, 64}, {-128, 0}},
1415 {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1416 {-128, 128}, {-256, 0}},
1417 {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1418 {-256, 256}, {-512, 0}},
1419 {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1420 {-512, 512}, {-1024, 0}},
1422 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1423 do_init_search, cost_list, vfp, use_mvcost,
1425 bigdia_num_candidates, bigdia_candidates);
1428 int vp9_square_search(const MACROBLOCK *x,
1434 const vp9_variance_fn_ptr_t *vfp,
1436 const MV *center_mv,
1438 // All scales have 8 closest points in square shape
1439 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1440 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1442 // Note that the largest candidate step at each scale is 2^scale
1443 static const MV square_candidates[MAX_PATTERN_SCALES]
1444 [MAX_PATTERN_CANDIDATES] = {
1445 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1446 {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1447 {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1448 {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1449 {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1450 {-16, 16}, {-16, 0}},
1451 {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1452 {-32, 32}, {-32, 0}},
1453 {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1454 {-64, 64}, {-64, 0}},
1455 {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1456 {-128, 128}, {-128, 0}},
1457 {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1458 {-256, 256}, {-256, 0}},
1459 {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1460 {-512, 512}, {-512, 0}},
1461 {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1462 {0, 1024}, {-1024, 1024}, {-1024, 0}},
1464 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1465 do_init_search, cost_list, vfp, use_mvcost,
1467 square_num_candidates, square_candidates);
1470 int vp9_fast_hex_search(const MACROBLOCK *x,
1474 int do_init_search, // must be zero for fast_hex
1476 const vp9_variance_fn_ptr_t *vfp,
1478 const MV *center_mv,
1480 return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1481 sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1482 center_mv, best_mv);
1485 int vp9_fast_dia_search(const MACROBLOCK *x,
1491 const vp9_variance_fn_ptr_t *vfp,
1493 const MV *center_mv,
1495 return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1496 sad_per_bit, do_init_search, cost_list, vfp,
1497 use_mvcost, center_mv, best_mv);
1502 int vp9_full_range_search_c(const MACROBLOCK *x,
1503 const search_site_config *cfg,
1504 MV *ref_mv, MV *best_mv,
1505 int search_param, int sad_per_bit, int *num00,
1506 const vp9_variance_fn_ptr_t *fn_ptr,
1507 const MV *center_mv) {
1508 const MACROBLOCKD *const xd = &x->e_mbd;
1509 const struct buf_2d *const what = &x->plane[0].src;
1510 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1511 const int range = 64;
1512 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1513 unsigned int best_sad = INT_MAX;
1515 int start_col, end_col, start_row, end_row;
1517 // The cfg and search_param parameters are not used in this search variant
1521 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1524 best_sad = fn_ptr->sdf(what->buf, what->stride,
1525 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1526 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1527 start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1528 start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1529 end_row = MIN(range, x->mv_row_max - ref_mv->row);
1530 end_col = MIN(range, x->mv_col_max - ref_mv->col);
1532 for (r = start_row; r <= end_row; ++r) {
1533 for (c = start_col; c <= end_col; c += 4) {
1534 if (c + 3 <= end_col) {
1535 unsigned int sads[4];
1536 const uint8_t *addrs[4];
1537 for (i = 0; i < 4; ++i) {
1538 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1539 addrs[i] = get_buf_from_mv(in_what, &mv);
1542 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1544 for (i = 0; i < 4; ++i) {
1545 if (sads[i] < best_sad) {
1546 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1547 const unsigned int sad = sads[i] +
1548 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1549 if (sad < best_sad) {
1556 for (i = 0; i < end_col - c; ++i) {
1557 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1558 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1559 get_buf_from_mv(in_what, &mv), in_what->stride);
1560 if (sad < best_sad) {
1561 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1562 if (sad < best_sad) {
1575 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1576 const search_site_config *cfg,
1577 MV *ref_mv, MV *best_mv, int search_param,
1578 int sad_per_bit, int *num00,
1579 const vp9_variance_fn_ptr_t *fn_ptr,
1580 const MV *center_mv) {
1583 const MACROBLOCKD *const xd = &x->e_mbd;
1584 uint8_t *what = x->plane[0].src.buf;
1585 const int what_stride = x->plane[0].src.stride;
1586 const uint8_t *in_what;
1587 const int in_what_stride = xd->plane[0].pre[0].stride;
1588 const uint8_t *best_address;
1590 unsigned int bestsad = INT_MAX;
1597 // search_param determines the length of the initial step and hence the number
1599 // 0 = initial step (MAX_FIRST_STEP) pel
1600 // 1 = (MAX_FIRST_STEP/2) pel,
1601 // 2 = (MAX_FIRST_STEP/4) pel...
1602 const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1603 const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1605 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1606 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1607 ref_row = ref_mv->row;
1608 ref_col = ref_mv->col;
1610 best_mv->row = ref_row;
1611 best_mv->col = ref_col;
1613 // Work out the start point for the search
1614 in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1615 best_address = in_what;
1617 // Check the starting position
1618 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1619 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1623 for (step = 0; step < tot_steps; step++) {
1626 // All_in is true if every one of the points we are checking are within
1627 // the bounds of the image.
1628 all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1629 all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1630 all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1631 all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1633 // If all the pixels are within the bounds we don't check whether the
1634 // search point is valid in this loop, otherwise we check each point
1637 unsigned int sad_array[4];
1639 for (j = 0; j < cfg->searches_per_step; j += 4) {
1640 unsigned char const *block_offset[4];
1642 for (t = 0; t < 4; t++)
1643 block_offset[t] = ss[i + t].offset + best_address;
1645 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1648 for (t = 0; t < 4; t++, i++) {
1649 if (sad_array[t] < bestsad) {
1650 const MV this_mv = {best_mv->row + ss[i].mv.row,
1651 best_mv->col + ss[i].mv.col};
1652 sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1654 if (sad_array[t] < bestsad) {
1655 bestsad = sad_array[t];
1662 for (j = 0; j < cfg->searches_per_step; j++) {
1663 // Trap illegal vectors
1664 const MV this_mv = {best_mv->row + ss[i].mv.row,
1665 best_mv->col + ss[i].mv.col};
1667 if (is_mv_in(x, &this_mv)) {
1668 const uint8_t *const check_here = ss[i].offset + best_address;
1669 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1672 if (thissad < bestsad) {
1673 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1674 if (thissad < bestsad) {
1683 if (best_site != last_site) {
1684 best_mv->row += ss[best_site].mv.row;
1685 best_mv->col += ss[best_site].mv.col;
1686 best_address += ss[best_site].offset;
1687 last_site = best_site;
1688 #if defined(NEW_DIAMOND_SEARCH)
1690 const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1691 best_mv->col + ss[best_site].mv.col};
1692 if (is_mv_in(x, &this_mv)) {
1693 const uint8_t *const check_here = ss[best_site].offset + best_address;
1694 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1696 if (thissad < bestsad) {
1697 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1698 if (thissad < bestsad) {
1700 best_mv->row += ss[best_site].mv.row;
1701 best_mv->col += ss[best_site].mv.col;
1702 best_address += ss[best_site].offset;
1710 } else if (best_address == in_what) {
1717 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1718 int best_sad = INT_MAX;
1721 int center, offset = 0;
1722 int bw = 4 << bwl; // redundant variable, to be changed in the experiments.
1723 for (d = 0; d <= bw; d += 16) {
1724 this_sad = vp9_vector_var(&ref[d], src, bwl);
1725 if (this_sad < best_sad) {
1726 best_sad = this_sad;
1732 for (d = -8; d <= 8; d += 16) {
1733 int this_pos = offset + d;
1735 if (this_pos < 0 || this_pos > bw)
1737 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1738 if (this_sad < best_sad) {
1739 best_sad = this_sad;
1745 for (d = -4; d <= 4; d += 8) {
1746 int this_pos = offset + d;
1748 if (this_pos < 0 || this_pos > bw)
1750 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1751 if (this_sad < best_sad) {
1752 best_sad = this_sad;
1758 for (d = -2; d <= 2; d += 4) {
1759 int this_pos = offset + d;
1761 if (this_pos < 0 || this_pos > bw)
1763 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1764 if (this_sad < best_sad) {
1765 best_sad = this_sad;
1771 for (d = -1; d <= 1; d += 2) {
1772 int this_pos = offset + d;
1774 if (this_pos < 0 || this_pos > bw)
1776 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1777 if (this_sad < best_sad) {
1778 best_sad = this_sad;
1783 return (center - (bw >> 1));
1786 static const MV search_pos[4] = {
1787 {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1790 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1792 MACROBLOCKD *xd = &x->e_mbd;
1793 DECLARE_ALIGNED(16, int16_t, hbuf[128]);
1794 DECLARE_ALIGNED(16, int16_t, vbuf[128]);
1795 DECLARE_ALIGNED(16, int16_t, src_hbuf[64]);
1796 DECLARE_ALIGNED(16, int16_t, src_vbuf[64]);
1798 const int bw = 4 << b_width_log2_lookup[bsize];
1799 const int bh = 4 << b_height_log2_lookup[bsize];
1800 const int search_width = bw << 1;
1801 const int search_height = bh << 1;
1802 const int src_stride = x->plane[0].src.stride;
1803 const int ref_stride = xd->plane[0].pre[0].stride;
1804 uint8_t const *ref_buf, *src_buf;
1805 MV *tmp_mv = &xd->mi[0]->mbmi.mv[0].as_mv;
1806 unsigned int best_sad, tmp_sad, this_sad[4];
1808 const int norm_factor = 3 + (bw >> 5);
1810 #if CONFIG_VP9_HIGHBITDEPTH
1813 return cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1814 xd->plane[0].pre[0].buf, ref_stride);
1817 // Set up prediction 1-D reference set
1818 ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1819 for (idx = 0; idx < search_width; idx += 16) {
1820 vp9_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1824 ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1825 for (idx = 0; idx < search_height; ++idx) {
1826 vbuf[idx] = vp9_int_pro_col(ref_buf, bw) >> norm_factor;
1827 ref_buf += ref_stride;
1830 // Set up src 1-D reference set
1831 for (idx = 0; idx < bw; idx += 16) {
1832 src_buf = x->plane[0].src.buf + idx;
1833 vp9_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1836 src_buf = x->plane[0].src.buf;
1837 for (idx = 0; idx < bh; ++idx) {
1838 src_vbuf[idx] = vp9_int_pro_col(src_buf, bw) >> norm_factor;
1839 src_buf += src_stride;
1842 // Find the best match per 1-D search
1843 tmp_mv->col = vector_match(hbuf, src_hbuf, b_width_log2_lookup[bsize]);
1844 tmp_mv->row = vector_match(vbuf, src_vbuf, b_height_log2_lookup[bsize]);
1847 src_buf = x->plane[0].src.buf;
1848 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1849 best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1852 const uint8_t * const pos[4] = {
1853 ref_buf - ref_stride,
1856 ref_buf + ref_stride,
1859 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1862 for (idx = 0; idx < 4; ++idx) {
1863 if (this_sad[idx] < best_sad) {
1864 best_sad = this_sad[idx];
1865 tmp_mv->row = search_pos[idx].row + this_mv.row;
1866 tmp_mv->col = search_pos[idx].col + this_mv.col;
1870 if (this_sad[0] < this_sad[3])
1875 if (this_sad[1] < this_sad[2])
1880 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1882 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1883 ref_buf, ref_stride);
1884 if (best_sad > tmp_sad) {
1895 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1896 point as the best match, we will do a final 1-away diamond
1898 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1899 MV *mvp_full, int step_param,
1900 int sadpb, int further_steps, int do_refine,
1902 const vp9_variance_fn_ptr_t *fn_ptr,
1903 const MV *ref_mv, MV *dst_mv) {
1905 int thissme, n, num00 = 0;
1906 int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1907 step_param, sadpb, &n,
1909 if (bestsme < INT_MAX)
1910 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1913 // If there won't be more n-step search, check to see if refining search is
1915 if (n > further_steps)
1918 while (n < further_steps) {
1924 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1925 step_param + n, sadpb, &num00,
1927 if (thissme < INT_MAX)
1928 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1930 // check to see if refining search is needed.
1931 if (num00 > further_steps - n)
1934 if (thissme < bestsme) {
1941 // final 1-away diamond refining search
1943 const int search_range = 8;
1944 MV best_mv = *dst_mv;
1945 thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1947 if (thissme < INT_MAX)
1948 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1949 if (thissme < bestsme) {
1955 // Return cost list.
1957 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1962 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1963 int sad_per_bit, int distance,
1964 const vp9_variance_fn_ptr_t *fn_ptr,
1965 const MV *center_mv, MV *best_mv) {
1967 const MACROBLOCKD *const xd = &x->e_mbd;
1968 const struct buf_2d *const what = &x->plane[0].src;
1969 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1970 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1971 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1972 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1973 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1974 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1975 int best_sad = fn_ptr->sdf(what->buf, what->stride,
1976 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1977 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1980 for (r = row_min; r < row_max; ++r) {
1981 for (c = col_min; c < col_max; ++c) {
1982 const MV mv = {r, c};
1983 const int sad = fn_ptr->sdf(what->buf, what->stride,
1984 get_buf_from_mv(in_what, &mv), in_what->stride) +
1985 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1986 if (sad < best_sad) {
1995 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
1996 int sad_per_bit, int distance,
1997 const vp9_variance_fn_ptr_t *fn_ptr,
1998 const MV *center_mv, MV *best_mv) {
2000 const MACROBLOCKD *const xd = &x->e_mbd;
2001 const struct buf_2d *const what = &x->plane[0].src;
2002 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2003 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2004 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2005 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2006 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2007 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2008 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2009 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2010 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2013 for (r = row_min; r < row_max; ++r) {
2015 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2017 if (fn_ptr->sdx3f != NULL) {
2018 while ((c + 2) < col_max) {
2020 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2022 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2025 for (i = 0; i < 3; ++i) {
2026 unsigned int sad = sads[i];
2027 if (sad < best_sad) {
2028 const MV mv = {r, c};
2029 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2030 if (sad < best_sad) {
2041 while (c < col_max) {
2042 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2043 check_here, in_what->stride);
2044 if (sad < best_sad) {
2045 const MV mv = {r, c};
2046 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2047 if (sad < best_sad) {
2060 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
2061 int sad_per_bit, int distance,
2062 const vp9_variance_fn_ptr_t *fn_ptr,
2063 const MV *center_mv, MV *best_mv) {
2065 const MACROBLOCKD *const xd = &x->e_mbd;
2066 const struct buf_2d *const what = &x->plane[0].src;
2067 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2068 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2069 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2070 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2071 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2072 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2073 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2074 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2075 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2078 for (r = row_min; r < row_max; ++r) {
2080 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2082 if (fn_ptr->sdx8f != NULL) {
2083 while ((c + 7) < col_max) {
2085 DECLARE_ALIGNED(16, uint32_t, sads[8]);
2087 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2090 for (i = 0; i < 8; ++i) {
2091 unsigned int sad = sads[i];
2092 if (sad < best_sad) {
2093 const MV mv = {r, c};
2094 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2095 if (sad < best_sad) {
2106 if (fn_ptr->sdx3f != NULL) {
2107 while ((c + 2) < col_max) {
2109 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2111 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2114 for (i = 0; i < 3; ++i) {
2115 unsigned int sad = sads[i];
2116 if (sad < best_sad) {
2117 const MV mv = {r, c};
2118 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2119 if (sad < best_sad) {
2130 while (c < col_max) {
2131 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2132 check_here, in_what->stride);
2133 if (sad < best_sad) {
2134 const MV mv = {r, c};
2135 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2136 if (sad < best_sad) {
2149 int vp9_refining_search_sad(const MACROBLOCK *x,
2150 MV *ref_mv, int error_per_bit,
2152 const vp9_variance_fn_ptr_t *fn_ptr,
2153 const MV *center_mv) {
2154 const MACROBLOCKD *const xd = &x->e_mbd;
2155 const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
2156 const struct buf_2d *const what = &x->plane[0].src;
2157 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2158 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2159 const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
2160 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
2162 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2165 for (i = 0; i < search_range; i++) {
2167 const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
2168 ((ref_mv->row + 1) < x->mv_row_max) &
2169 ((ref_mv->col - 1) > x->mv_col_min) &
2170 ((ref_mv->col + 1) < x->mv_col_max);
2173 unsigned int sads[4];
2174 const uint8_t *const positions[4] = {
2175 best_address - in_what->stride,
2178 best_address + in_what->stride
2181 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2183 for (j = 0; j < 4; ++j) {
2184 if (sads[j] < best_sad) {
2185 const MV mv = {ref_mv->row + neighbors[j].row,
2186 ref_mv->col + neighbors[j].col};
2187 sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2188 if (sads[j] < best_sad) {
2195 for (j = 0; j < 4; ++j) {
2196 const MV mv = {ref_mv->row + neighbors[j].row,
2197 ref_mv->col + neighbors[j].col};
2199 if (is_mv_in(x, &mv)) {
2200 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2201 get_buf_from_mv(in_what, &mv),
2203 if (sad < best_sad) {
2204 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2205 if (sad < best_sad) {
2214 if (best_site == -1) {
2217 ref_mv->row += neighbors[best_site].row;
2218 ref_mv->col += neighbors[best_site].col;
2219 best_address = get_buf_from_mv(in_what, ref_mv);
2226 // This function is called when we do joint motion search in comp_inter_inter
2228 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2229 MV *ref_mv, int error_per_bit,
2231 const vp9_variance_fn_ptr_t *fn_ptr,
2232 const MV *center_mv,
2233 const uint8_t *second_pred) {
2234 const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
2235 {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
2236 const MACROBLOCKD *const xd = &x->e_mbd;
2237 const struct buf_2d *const what = &x->plane[0].src;
2238 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2239 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2240 unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
2241 get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
2242 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2245 for (i = 0; i < search_range; ++i) {
2248 for (j = 0; j < 8; ++j) {
2249 const MV mv = {ref_mv->row + neighbors[j].row,
2250 ref_mv->col + neighbors[j].col};
2252 if (is_mv_in(x, &mv)) {
2253 unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
2254 get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
2255 if (sad < best_sad) {
2256 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2257 if (sad < best_sad) {
2265 if (best_site == -1) {
2268 ref_mv->row += neighbors[best_site].row;
2269 ref_mv->col += neighbors[best_site].col;
2275 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
2276 BLOCK_SIZE bsize, MV *mvp_full,
2277 int step_param, int error_per_bit,
2279 const MV *ref_mv, MV *tmp_mv,
2280 int var_max, int rd) {
2281 const SPEED_FEATURES *const sf = &cpi->sf;
2282 const SEARCH_METHODS method = sf->mv.search_method;
2283 vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
2286 cost_list[0] = INT_MAX;
2287 cost_list[1] = INT_MAX;
2288 cost_list[2] = INT_MAX;
2289 cost_list[3] = INT_MAX;
2290 cost_list[4] = INT_MAX;
2295 var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2296 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2299 var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2300 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2303 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2304 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2307 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2308 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2311 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2312 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2315 var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2316 MAX_MVSEARCH_STEPS - 1 - step_param,
2317 1, cost_list, fn_ptr, ref_mv, tmp_mv);
2320 assert(0 && "Invalid search method.");
2323 if (method != NSTEP && rd && var < var_max)
2324 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);