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"
18 #include "vpx_ports/mem.h"
20 #include "vp9/common/vp9_common.h"
22 #include "vp9/encoder/vp9_encoder.h"
23 #include "vp9/encoder/vp9_mcomp.h"
25 // #define NEW_DIAMOND_SEARCH
27 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
29 return &buf->buf[mv->row * buf->stride + mv->col];
32 void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
33 int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
34 int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
35 int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
36 int row_max = (mv->row >> 3) + MAX_FULL_PEL_VAL;
38 col_min = MAX(col_min, (MV_LOW >> 3) + 1);
39 row_min = MAX(row_min, (MV_LOW >> 3) + 1);
40 col_max = MIN(col_max, (MV_UPP >> 3) - 1);
41 row_max = MIN(row_max, (MV_UPP >> 3) - 1);
43 // Get intersection of UMV window and valid MV window to reduce # of checks
45 if (x->mv_col_min < col_min)
46 x->mv_col_min = col_min;
47 if (x->mv_col_max > col_max)
48 x->mv_col_max = col_max;
49 if (x->mv_row_min < row_min)
50 x->mv_row_min = row_min;
51 if (x->mv_row_max > row_max)
52 x->mv_row_max = row_max;
55 int vp9_init_search_range(int size) {
57 // Minimum search size no matter what the passed in value.
60 while ((size << sr) < MAX_FULL_PEL_VAL)
63 sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
67 static INLINE int mv_cost(const MV *mv,
68 const int *joint_cost, int *const comp_cost[2]) {
69 return joint_cost[vp9_get_mv_joint(mv)] +
70 comp_cost[0][mv->row] + comp_cost[1][mv->col];
73 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
74 const int *mvjcost, int *mvcost[2], int weight) {
75 const MV diff = { mv->row - ref->row,
77 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
80 static int mv_err_cost(const MV *mv, const MV *ref,
81 const int *mvjcost, int *mvcost[2],
84 const MV diff = { mv->row - ref->row,
86 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
92 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
94 const MV diff = { mv->row - ref->row,
96 return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
97 x->nmvsadcost) * error_per_bit, 8);
100 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride) {
101 int len, ss_count = 1;
103 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
104 cfg->ss[0].offset = 0;
106 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
107 // Generate offsets for 4 search sites per step.
108 const MV ss_mvs[] = {{-len, 0}, {len, 0}, {0, -len}, {0, len}};
110 for (i = 0; i < 4; ++i) {
111 search_site *const ss = &cfg->ss[ss_count++];
113 ss->offset = ss->mv.row * stride + ss->mv.col;
117 cfg->ss_count = ss_count;
118 cfg->searches_per_step = 4;
121 void vp9_init3smotion_compensation(search_site_config *cfg, int stride) {
122 int len, ss_count = 1;
124 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
125 cfg->ss[0].offset = 0;
127 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
128 // Generate offsets for 8 search sites per step.
129 const MV ss_mvs[8] = {
130 {-len, 0 }, {len, 0 }, { 0, -len}, {0, len},
131 {-len, -len}, {-len, len}, {len, -len}, {len, len}
134 for (i = 0; i < 8; ++i) {
135 search_site *const ss = &cfg->ss[ss_count++];
137 ss->offset = ss->mv.row * stride + ss->mv.col;
141 cfg->ss_count = ss_count;
142 cfg->searches_per_step = 8;
146 * To avoid the penalty for crossing cache-line read, preload the reference
147 * area in a small buffer, which is aligned to make sure there won't be crossing
148 * cache-line read while reading from this buffer. This reduced the cpu
149 * cycles spent on reading ref data in sub-pixel filter functions.
150 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
151 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
152 * could reduce the area.
155 /* estimated cost of a motion vector (r,c) */
158 ((mvjcost[((r) != rr) * 2 + ((c) != rc)] + \
159 mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
160 error_per_bit + 4096) >> 13 : 0)
163 // convert motion vector component to offset for svf calc
164 static INLINE int sp(int x) {
168 static INLINE const uint8_t *pre(const uint8_t *buf, int stride, int r, int c) {
169 return &buf[(r >> 3) * stride + (c >> 3)];
172 /* checks if (r, c) has better score than previous best */
173 #define CHECK_BETTER(v, r, c) \
174 if (c >= minc && c <= maxc && r >= minr && r <= maxr) { \
175 if (second_pred == NULL) \
176 thismse = vfp->svf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), z, \
179 thismse = vfp->svaf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), \
180 z, src_stride, &sse, second_pred); \
181 if ((v = MVC(r, c) + thismse) < besterr) { \
185 *distortion = thismse; \
192 #define FIRST_LEVEL_CHECKS \
194 unsigned int left, right, up, down, diag; \
195 CHECK_BETTER(left, tr, tc - hstep); \
196 CHECK_BETTER(right, tr, tc + hstep); \
197 CHECK_BETTER(up, tr - hstep, tc); \
198 CHECK_BETTER(down, tr + hstep, tc); \
199 whichdir = (left < right ? 0 : 1) + \
200 (up < down ? 0 : 2); \
201 switch (whichdir) { \
203 CHECK_BETTER(diag, tr - hstep, tc - hstep); \
206 CHECK_BETTER(diag, tr - hstep, tc + hstep); \
209 CHECK_BETTER(diag, tr + hstep, tc - hstep); \
212 CHECK_BETTER(diag, tr + hstep, tc + hstep); \
217 #define SECOND_LEVEL_CHECKS \
220 unsigned int second; \
221 if (tr != br && tc != bc) { \
224 CHECK_BETTER(second, tr + kr, tc + 2 * kc); \
225 CHECK_BETTER(second, tr + 2 * kr, tc + kc); \
226 } else if (tr == br && tc != bc) { \
228 CHECK_BETTER(second, tr + hstep, tc + 2 * kc); \
229 CHECK_BETTER(second, tr - hstep, tc + 2 * kc); \
230 switch (whichdir) { \
233 CHECK_BETTER(second, tr + hstep, tc + kc); \
237 CHECK_BETTER(second, tr - hstep, tc + kc); \
240 } else if (tr != br && tc == bc) { \
242 CHECK_BETTER(second, tr + 2 * kr, tc + hstep); \
243 CHECK_BETTER(second, tr + 2 * kr, tc - hstep); \
244 switch (whichdir) { \
247 CHECK_BETTER(second, tr + kr, tc + hstep); \
251 CHECK_BETTER(second, tr + kr, tc - hstep); \
257 #define SETUP_SUBPEL_SEARCH \
258 const uint8_t *const z = x->plane[0].src.buf; \
259 const int src_stride = x->plane[0].src.stride; \
260 const MACROBLOCKD *xd = &x->e_mbd; \
261 unsigned int besterr = INT_MAX; \
263 unsigned int whichdir; \
265 const unsigned int halfiters = iters_per_step; \
266 const unsigned int quarteriters = iters_per_step; \
267 const unsigned int eighthiters = iters_per_step; \
268 const int y_stride = xd->plane[0].pre[0].stride; \
269 const int offset = bestmv->row * y_stride + bestmv->col; \
270 const uint8_t *const y = xd->plane[0].pre[0].buf; \
272 int rr = ref_mv->row; \
273 int rc = ref_mv->col; \
274 int br = bestmv->row * 8; \
275 int bc = bestmv->col * 8; \
277 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX); \
278 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX); \
279 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX); \
280 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX); \
287 static INLINE unsigned int setup_center_error(const MACROBLOCKD *xd,
291 const vp9_variance_fn_ptr_t *vfp,
292 const uint8_t *const src,
293 const int src_stride,
294 const uint8_t *const y,
296 const uint8_t *second_pred,
297 int w, int h, int offset,
298 int *mvjcost, int *mvcost[2],
301 unsigned int besterr;
302 #if CONFIG_VP9_HIGHBITDEPTH
303 if (second_pred != NULL) {
304 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
305 DECLARE_ALIGNED(16, uint16_t, comp_pred16[64 * 64]);
306 vp9_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset,
308 besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, src, src_stride,
311 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
312 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
313 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
316 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
318 *distortion = besterr;
319 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
322 if (second_pred != NULL) {
323 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
324 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
325 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
327 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
329 *distortion = besterr;
330 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
331 #endif // CONFIG_VP9_HIGHBITDEPTH
335 static INLINE int divide_and_round(const int n, const int d) {
336 return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
339 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
340 return cost_list[0] < cost_list[1] &&
341 cost_list[0] < cost_list[2] &&
342 cost_list[0] < cost_list[3] &&
343 cost_list[0] < cost_list[4];
346 // Returns surface minima estimate at given precision in 1/2^n bits.
347 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
348 // For a given set of costs S0, S1, S2, S3, S4 at points
349 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
350 // the solution for the location of the minima (x0, y0) is given by:
351 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
352 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
353 // The code below is an integerized version of that.
354 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
356 *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
357 (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
358 *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
359 (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
362 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
364 MV *bestmv, const MV *ref_mv,
367 const vp9_variance_fn_ptr_t *vfp,
371 int *mvjcost, int *mvcost[2],
374 const uint8_t *second_pred,
377 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
378 z, src_stride, y, y_stride, second_pred,
379 w, h, offset, mvjcost, mvcost,
390 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
391 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
392 cost_list[4] != INT_MAX &&
393 is_cost_list_wellbehaved(cost_list)) {
396 get_cost_surf_min(cost_list, &ir, &ic, 2);
397 if (ir != 0 || ic != 0) {
398 CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
409 // Each subsequent iteration checks at least one point in common with
410 // the last iteration could be 2 ( if diag selected) 1/4 pel
411 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
412 if (forced_stop != 2) {
415 if (quarteriters > 1) {
424 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
427 if (eighthiters > 1) {
435 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
436 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
442 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
443 MV *bestmv, const MV *ref_mv,
446 const vp9_variance_fn_ptr_t *vfp,
450 int *mvjcost, int *mvcost[2],
453 const uint8_t *second_pred,
456 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
457 z, src_stride, y, y_stride, second_pred,
458 w, h, offset, mvjcost, mvcost,
461 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
462 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
463 cost_list[4] != INT_MAX &&
464 is_cost_list_wellbehaved(cost_list)) {
467 get_cost_surf_min(cost_list, &ir, &ic, 1);
468 if (ir != 0 || ic != 0) {
469 CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
478 // Each subsequent iteration checks at least one point in common with
479 // the last iteration could be 2 ( if diag selected) 1/4 pel
481 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
482 if (forced_stop != 2) {
487 if (quarteriters > 1) {
492 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
497 if (eighthiters > 1) {
501 // These lines insure static analysis doesn't warn that
502 // tr and tc aren't used after the above point.
509 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
510 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
516 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
517 MV *bestmv, const MV *ref_mv,
520 const vp9_variance_fn_ptr_t *vfp,
524 int *mvjcost, int *mvcost[2],
527 const uint8_t *second_pred,
530 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
531 z, src_stride, y, y_stride, second_pred,
532 w, h, offset, mvjcost, mvcost,
535 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
536 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
537 cost_list[4] != INT_MAX) {
538 unsigned int left, right, up, down, diag;
539 whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
540 (cost_list[2] < cost_list[4] ? 0 : 2);
543 CHECK_BETTER(left, tr, tc - hstep);
544 CHECK_BETTER(down, tr + hstep, tc);
545 CHECK_BETTER(diag, tr + hstep, tc - hstep);
548 CHECK_BETTER(right, tr, tc + hstep);
549 CHECK_BETTER(down, tr + hstep, tc);
550 CHECK_BETTER(diag, tr + hstep, tc + hstep);
553 CHECK_BETTER(left, tr, tc - hstep);
554 CHECK_BETTER(up, tr - hstep, tc);
555 CHECK_BETTER(diag, tr - hstep, tc - hstep);
558 CHECK_BETTER(right, tr, tc + hstep);
559 CHECK_BETTER(up, tr - hstep, tc);
560 CHECK_BETTER(diag, tr - hstep, tc + hstep);
573 // Each subsequent iteration checks at least one point in common with
574 // the last iteration could be 2 ( if diag selected) 1/4 pel
576 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
577 if (forced_stop != 2) {
580 if (quarteriters > 1) {
587 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
590 if (eighthiters > 1) {
596 // These lines insure static analysis doesn't warn that
597 // tr and tc aren't used after the above point.
604 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
605 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
611 const MV search_step_table[12] = {
612 // left, right, up, down
613 {0, -4}, {0, 4}, {-4, 0}, {4, 0},
614 {0, -2}, {0, 2}, {-2, 0}, {2, 0},
615 {0, -1}, {0, 1}, {-1, 0}, {1, 0}
618 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
619 MV *bestmv, const MV *ref_mv,
622 const vp9_variance_fn_ptr_t *vfp,
626 int *mvjcost, int *mvcost[2],
629 const uint8_t *second_pred,
631 const uint8_t *const z = x->plane[0].src.buf;
632 const uint8_t *const src_address = z;
633 const int src_stride = x->plane[0].src.stride;
634 const MACROBLOCKD *xd = &x->e_mbd;
635 unsigned int besterr = INT_MAX;
637 unsigned int whichdir = 0;
639 const int y_stride = xd->plane[0].pre[0].stride;
640 const int offset = bestmv->row * y_stride + bestmv->col;
641 const uint8_t *const y = xd->plane[0].pre[0].buf;
643 int rr = ref_mv->row;
644 int rc = ref_mv->col;
645 int br = bestmv->row * 8;
646 int bc = bestmv->col * 8;
648 int iter, round = 3 - forced_stop;
649 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX);
650 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX);
651 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX);
652 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX);
655 const MV *search_step = search_step_table;
656 int idx, best_idx = -1;
657 unsigned int cost_array[5];
659 if (!(allow_hp && vp9_use_mv_hp(ref_mv)))
666 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
667 z, src_stride, y, y_stride, second_pred,
668 w, h, offset, mvjcost, mvcost,
671 (void) cost_list; // to silence compiler warning
673 for (iter = 0; iter < round; ++iter) {
674 // Check vertical and horizontal sub-pixel positions.
675 for (idx = 0; idx < 4; ++idx) {
676 tr = br + search_step[idx].row;
677 tc = bc + search_step[idx].col;
678 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
679 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
680 int row_offset = (tr & 0x07) << 1;
681 int col_offset = (tc & 0x07) << 1;
685 if (second_pred == NULL)
686 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
687 src_address, src_stride, &sse);
689 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
690 src_address, src_stride, &sse, second_pred);
691 cost_array[idx] = thismse +
692 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
694 if (cost_array[idx] < besterr) {
696 besterr = cost_array[idx];
697 *distortion = thismse;
701 cost_array[idx] = INT_MAX;
705 // Check diagonal sub-pixel position
706 tc = bc + (cost_array[0] < cost_array[1] ? -hstep : hstep);
707 tr = br + (cost_array[2] < cost_array[3] ? -hstep : hstep);
708 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
709 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
710 int row_offset = (tr & 0x07) << 1;
711 int col_offset = (tc & 0x07) << 1;
712 MV this_mv = {tr, tc};
713 if (second_pred == NULL)
714 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
715 src_address, src_stride, &sse);
717 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
718 src_address, src_stride, &sse, second_pred);
719 cost_array[4] = thismse +
720 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
722 if (cost_array[4] < besterr) {
724 besterr = cost_array[4];
725 *distortion = thismse;
729 cost_array[idx] = INT_MAX;
732 if (best_idx < 4 && best_idx >= 0) {
733 br += search_step[best_idx].row;
734 bc += search_step[best_idx].col;
735 } else if (best_idx == 4) {
740 if (iters_per_step > 1)
751 // Each subsequent iteration checks at least one point in common with
752 // the last iteration could be 2 ( if diag selected) 1/4 pel
754 // These lines insure static analysis doesn't warn that
755 // tr and tc aren't used after the above point.
762 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
763 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
773 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
775 return ((row - range) >= x->mv_row_min) &
776 ((row + range) <= x->mv_row_max) &
777 ((col - range) >= x->mv_col_min) &
778 ((col + range) <= x->mv_col_max);
781 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
782 return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
783 (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
786 #define CHECK_BETTER \
788 if (thissad < bestsad) {\
790 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
791 if (thissad < bestsad) {\
798 #define MAX_PATTERN_SCALES 11
799 #define MAX_PATTERN_CANDIDATES 8 // max number of canddiates per scale
800 #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
802 // Calculate and return a sad+mvcost list around an integer best pel.
803 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
806 const vp9_variance_fn_ptr_t *fn_ptr,
809 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
810 const struct buf_2d *const what = &x->plane[0].src;
811 const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
812 const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
813 int br = best_mv->row;
814 int bc = best_mv->col;
821 cost_list[0] = fn_ptr->vf(what->buf, what->stride,
822 get_buf_from_mv(in_what, &this_mv),
823 in_what->stride, &sse) +
824 mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
825 if (check_bounds(x, br, bc, 1)) {
826 for (i = 0; i < 4; i++) {
827 const MV this_mv = {br + neighbors[i].row,
828 bc + neighbors[i].col};
829 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
830 get_buf_from_mv(in_what, &this_mv),
831 in_what->stride, &sse) +
832 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
833 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
837 for (i = 0; i < 4; i++) {
838 const MV this_mv = {br + neighbors[i].row,
839 bc + neighbors[i].col};
840 if (!is_mv_in(x, &this_mv))
841 cost_list[i + 1] = INT_MAX;
843 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
844 get_buf_from_mv(in_what, &this_mv),
845 in_what->stride, &sse) +
846 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
847 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
853 // Generic pattern search function that searches over multiple scales.
854 // Each scale can have a different number of candidates and shape of
855 // candidates as indicated in the num_candidates and candidates arrays
856 // passed into this function
858 static int vp9_pattern_search(const MACROBLOCK *x,
864 const vp9_variance_fn_ptr_t *vfp,
868 const int num_candidates[MAX_PATTERN_SCALES],
869 const MV candidates[MAX_PATTERN_SCALES]
870 [MAX_PATTERN_CANDIDATES]) {
871 const MACROBLOCKD *const xd = &x->e_mbd;
872 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
873 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
876 const struct buf_2d *const what = &x->plane[0].src;
877 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
879 int bestsad = INT_MAX;
882 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
883 int best_init_s = search_param_to_steps[search_param];
884 // adjust ref_mv to make sure it is within MV range
885 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
889 // Work out the start point for the search
890 bestsad = vfp->sdf(what->buf, what->stride,
891 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
892 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
894 // Search all possible scales upto the search param around the center point
895 // pick the scale of the point that is best as the starting scale of
896 // further steps around it.
897 if (do_init_search) {
900 for (t = 0; t <= s; ++t) {
902 if (check_bounds(x, br, bc, 1 << t)) {
903 for (i = 0; i < num_candidates[t]; i++) {
904 const MV this_mv = {br + candidates[t][i].row,
905 bc + candidates[t][i].col};
906 thissad = vfp->sdf(what->buf, what->stride,
907 get_buf_from_mv(in_what, &this_mv),
912 for (i = 0; i < num_candidates[t]; i++) {
913 const MV this_mv = {br + candidates[t][i].row,
914 bc + candidates[t][i].col};
915 if (!is_mv_in(x, &this_mv))
917 thissad = vfp->sdf(what->buf, what->stride,
918 get_buf_from_mv(in_what, &this_mv),
923 if (best_site == -1) {
930 if (best_init_s != -1) {
931 br += candidates[best_init_s][k].row;
932 bc += candidates[best_init_s][k].col;
936 // If the center point is still the best, just skip this and move to
937 // the refinement step.
938 if (best_init_s != -1) {
943 // No need to search all 6 points the 1st time if initial search was used
944 if (!do_init_search || s != best_init_s) {
945 if (check_bounds(x, br, bc, 1 << s)) {
946 for (i = 0; i < num_candidates[s]; i++) {
947 const MV this_mv = {br + candidates[s][i].row,
948 bc + candidates[s][i].col};
949 thissad = vfp->sdf(what->buf, what->stride,
950 get_buf_from_mv(in_what, &this_mv),
955 for (i = 0; i < num_candidates[s]; i++) {
956 const MV this_mv = {br + candidates[s][i].row,
957 bc + candidates[s][i].col};
958 if (!is_mv_in(x, &this_mv))
960 thissad = vfp->sdf(what->buf, what->stride,
961 get_buf_from_mv(in_what, &this_mv),
967 if (best_site == -1) {
970 br += candidates[s][best_site].row;
971 bc += candidates[s][best_site].col;
977 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
979 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
980 next_chkpts_indices[1] = k;
981 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
983 if (check_bounds(x, br, bc, 1 << s)) {
984 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
985 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
986 bc + candidates[s][next_chkpts_indices[i]].col};
987 thissad = vfp->sdf(what->buf, what->stride,
988 get_buf_from_mv(in_what, &this_mv),
993 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
994 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
995 bc + candidates[s][next_chkpts_indices[i]].col};
996 if (!is_mv_in(x, &this_mv))
998 thissad = vfp->sdf(what->buf, what->stride,
999 get_buf_from_mv(in_what, &this_mv),
1005 if (best_site != -1) {
1006 k = next_chkpts_indices[best_site];
1007 br += candidates[s][k].row;
1008 bc += candidates[s][k].col;
1010 } while (best_site != -1);
1014 // Returns the one-away integer pel sad values around the best as follows:
1015 // cost_list[0]: cost at the best integer pel
1016 // cost_list[1]: cost at delta {0, -1} (left) from the best integer pel
1017 // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
1018 // cost_list[3]: cost at delta { 0, 1} (right) from the best integer pel
1019 // cost_list[4]: cost at delta {-1, 0} (top) from the best integer pel
1021 const MV best_mv = { br, bc };
1022 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1029 // A specialized function where the smallest scale search candidates
1030 // are 4 1-away neighbors, and cost_list is non-null
1031 // TODO(debargha): Merge this function with the one above. Also remove
1032 // use_mvcost option since it is always 1, to save unnecessary branches.
1033 static int vp9_pattern_search_sad(const MACROBLOCK *x,
1039 const vp9_variance_fn_ptr_t *vfp,
1041 const MV *center_mv,
1043 const int num_candidates[MAX_PATTERN_SCALES],
1044 const MV candidates[MAX_PATTERN_SCALES]
1045 [MAX_PATTERN_CANDIDATES]) {
1046 const MACROBLOCKD *const xd = &x->e_mbd;
1047 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
1048 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1051 const struct buf_2d *const what = &x->plane[0].src;
1052 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1054 int bestsad = INT_MAX;
1057 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1058 int best_init_s = search_param_to_steps[search_param];
1059 // adjust ref_mv to make sure it is within MV range
1060 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1063 if (cost_list != NULL) {
1064 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1068 // Work out the start point for the search
1069 bestsad = vfp->sdf(what->buf, what->stride,
1070 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1071 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1073 // Search all possible scales upto the search param around the center point
1074 // pick the scale of the point that is best as the starting scale of
1075 // further steps around it.
1076 if (do_init_search) {
1079 for (t = 0; t <= s; ++t) {
1081 if (check_bounds(x, br, bc, 1 << t)) {
1082 for (i = 0; i < num_candidates[t]; i++) {
1083 const MV this_mv = {br + candidates[t][i].row,
1084 bc + candidates[t][i].col};
1085 thissad = vfp->sdf(what->buf, what->stride,
1086 get_buf_from_mv(in_what, &this_mv),
1091 for (i = 0; i < num_candidates[t]; i++) {
1092 const MV this_mv = {br + candidates[t][i].row,
1093 bc + candidates[t][i].col};
1094 if (!is_mv_in(x, &this_mv))
1096 thissad = vfp->sdf(what->buf, what->stride,
1097 get_buf_from_mv(in_what, &this_mv),
1102 if (best_site == -1) {
1109 if (best_init_s != -1) {
1110 br += candidates[best_init_s][k].row;
1111 bc += candidates[best_init_s][k].col;
1115 // If the center point is still the best, just skip this and move to
1116 // the refinement step.
1117 if (best_init_s != -1) {
1118 int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1122 for (; s >= do_sad; s--) {
1123 if (!do_init_search || s != best_init_s) {
1124 if (check_bounds(x, br, bc, 1 << s)) {
1125 for (i = 0; i < num_candidates[s]; i++) {
1126 const MV this_mv = {br + candidates[s][i].row,
1127 bc + candidates[s][i].col};
1128 thissad = vfp->sdf(what->buf, what->stride,
1129 get_buf_from_mv(in_what, &this_mv),
1134 for (i = 0; i < num_candidates[s]; i++) {
1135 const MV this_mv = {br + candidates[s][i].row,
1136 bc + candidates[s][i].col};
1137 if (!is_mv_in(x, &this_mv))
1139 thissad = vfp->sdf(what->buf, what->stride,
1140 get_buf_from_mv(in_what, &this_mv),
1146 if (best_site == -1) {
1149 br += candidates[s][best_site].row;
1150 bc += candidates[s][best_site].col;
1156 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1158 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1159 next_chkpts_indices[1] = k;
1160 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1162 if (check_bounds(x, br, bc, 1 << s)) {
1163 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1164 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1165 bc + candidates[s][next_chkpts_indices[i]].col};
1166 thissad = vfp->sdf(what->buf, what->stride,
1167 get_buf_from_mv(in_what, &this_mv),
1172 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1173 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1174 bc + candidates[s][next_chkpts_indices[i]].col};
1175 if (!is_mv_in(x, &this_mv))
1177 thissad = vfp->sdf(what->buf, what->stride,
1178 get_buf_from_mv(in_what, &this_mv),
1184 if (best_site != -1) {
1185 k = next_chkpts_indices[best_site];
1186 br += candidates[s][k].row;
1187 bc += candidates[s][k].col;
1189 } while (best_site != -1);
1192 // Note: If we enter the if below, then cost_list must be non-NULL.
1194 cost_list[0] = bestsad;
1195 if (!do_init_search || s != best_init_s) {
1196 if (check_bounds(x, br, bc, 1 << s)) {
1197 for (i = 0; i < num_candidates[s]; i++) {
1198 const MV this_mv = {br + candidates[s][i].row,
1199 bc + candidates[s][i].col};
1201 thissad = vfp->sdf(what->buf, what->stride,
1202 get_buf_from_mv(in_what, &this_mv),
1207 for (i = 0; i < num_candidates[s]; i++) {
1208 const MV this_mv = {br + candidates[s][i].row,
1209 bc + candidates[s][i].col};
1210 if (!is_mv_in(x, &this_mv))
1213 thissad = vfp->sdf(what->buf, what->stride,
1214 get_buf_from_mv(in_what, &this_mv),
1220 if (best_site != -1) {
1221 br += candidates[s][best_site].row;
1222 bc += candidates[s][best_site].col;
1226 while (best_site != -1) {
1227 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1229 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1230 next_chkpts_indices[1] = k;
1231 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1232 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1233 cost_list[((k + 2) % 4) + 1] = cost_list[0];
1234 cost_list[0] = bestsad;
1236 if (check_bounds(x, br, bc, 1 << s)) {
1237 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1238 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1239 bc + candidates[s][next_chkpts_indices[i]].col};
1240 cost_list[next_chkpts_indices[i] + 1] =
1241 thissad = vfp->sdf(what->buf, what->stride,
1242 get_buf_from_mv(in_what, &this_mv),
1247 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1248 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1249 bc + candidates[s][next_chkpts_indices[i]].col};
1250 if (!is_mv_in(x, &this_mv)) {
1251 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1254 cost_list[next_chkpts_indices[i] + 1] =
1255 thissad = vfp->sdf(what->buf, what->stride,
1256 get_buf_from_mv(in_what, &this_mv),
1262 if (best_site != -1) {
1263 k = next_chkpts_indices[best_site];
1264 br += candidates[s][k].row;
1265 bc += candidates[s][k].col;
1271 // Returns the one-away integer pel sad values around the best as follows:
1272 // cost_list[0]: sad at the best integer pel
1273 // cost_list[1]: sad at delta {0, -1} (left) from the best integer pel
1274 // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1275 // cost_list[3]: sad at delta { 0, 1} (right) from the best integer pel
1276 // cost_list[4]: sad at delta {-1, 0} (top) from the best integer pel
1278 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1279 if (cost_list[0] == INT_MAX) {
1280 cost_list[0] = bestsad;
1281 if (check_bounds(x, br, bc, 1)) {
1282 for (i = 0; i < 4; i++) {
1283 const MV this_mv = { br + neighbors[i].row,
1284 bc + neighbors[i].col };
1285 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1286 get_buf_from_mv(in_what, &this_mv),
1290 for (i = 0; i < 4; i++) {
1291 const MV this_mv = {br + neighbors[i].row,
1292 bc + neighbors[i].col};
1293 if (!is_mv_in(x, &this_mv))
1294 cost_list[i + 1] = INT_MAX;
1296 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1297 get_buf_from_mv(in_what, &this_mv),
1303 for (i = 0; i < 4; i++) {
1304 const MV this_mv = {br + neighbors[i].row,
1305 bc + neighbors[i].col};
1306 if (cost_list[i + 1] != INT_MAX) {
1308 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1319 int vp9_get_mvpred_var(const MACROBLOCK *x,
1320 const MV *best_mv, const MV *center_mv,
1321 const vp9_variance_fn_ptr_t *vfp,
1323 const MACROBLOCKD *const xd = &x->e_mbd;
1324 const struct buf_2d *const what = &x->plane[0].src;
1325 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1326 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1327 unsigned int unused;
1329 return vfp->vf(what->buf, what->stride,
1330 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1331 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1332 x->mvcost, x->errorperbit) : 0);
1335 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1336 const MV *best_mv, const MV *center_mv,
1337 const uint8_t *second_pred,
1338 const vp9_variance_fn_ptr_t *vfp,
1340 const MACROBLOCKD *const xd = &x->e_mbd;
1341 const struct buf_2d *const what = &x->plane[0].src;
1342 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1343 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1344 unsigned int unused;
1346 return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1347 what->buf, what->stride, &unused, second_pred) +
1348 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1349 x->mvcost, x->errorperbit) : 0);
1352 int vp9_hex_search(const MACROBLOCK *x,
1358 const vp9_variance_fn_ptr_t *vfp,
1360 const MV *center_mv, MV *best_mv) {
1361 // First scale has 8-closest points, the rest have 6 points in hex shape
1362 // at increasing scales
1363 static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1364 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1366 // Note that the largest candidate step at each scale is 2^scale
1367 static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1368 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1369 {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1370 {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1371 {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1372 {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1373 {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1374 {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1375 {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1376 {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1377 {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1378 {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1381 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1382 do_init_search, cost_list, vfp, use_mvcost,
1384 hex_num_candidates, hex_candidates);
1387 int vp9_bigdia_search(const MACROBLOCK *x,
1393 const vp9_variance_fn_ptr_t *vfp,
1395 const MV *center_mv,
1397 // First scale has 4-closest points, the rest have 8 points in diamond
1398 // shape at increasing scales
1399 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1400 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1402 // Note that the largest candidate step at each scale is 2^scale
1403 static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1404 [MAX_PATTERN_CANDIDATES] = {
1405 {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1406 {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1407 {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1408 {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1409 {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1410 {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1411 {-16, 16}, {-32, 0}},
1412 {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1413 {-32, 32}, {-64, 0}},
1414 {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1415 {-64, 64}, {-128, 0}},
1416 {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1417 {-128, 128}, {-256, 0}},
1418 {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1419 {-256, 256}, {-512, 0}},
1420 {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1421 {-512, 512}, {-1024, 0}},
1423 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1424 do_init_search, cost_list, vfp, use_mvcost,
1426 bigdia_num_candidates, bigdia_candidates);
1429 int vp9_square_search(const MACROBLOCK *x,
1435 const vp9_variance_fn_ptr_t *vfp,
1437 const MV *center_mv,
1439 // All scales have 8 closest points in square shape
1440 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1441 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1443 // Note that the largest candidate step at each scale is 2^scale
1444 static const MV square_candidates[MAX_PATTERN_SCALES]
1445 [MAX_PATTERN_CANDIDATES] = {
1446 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1447 {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1448 {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1449 {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1450 {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1451 {-16, 16}, {-16, 0}},
1452 {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1453 {-32, 32}, {-32, 0}},
1454 {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1455 {-64, 64}, {-64, 0}},
1456 {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1457 {-128, 128}, {-128, 0}},
1458 {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1459 {-256, 256}, {-256, 0}},
1460 {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1461 {-512, 512}, {-512, 0}},
1462 {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1463 {0, 1024}, {-1024, 1024}, {-1024, 0}},
1465 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1466 do_init_search, cost_list, vfp, use_mvcost,
1468 square_num_candidates, square_candidates);
1471 int vp9_fast_hex_search(const MACROBLOCK *x,
1475 int do_init_search, // must be zero for fast_hex
1477 const vp9_variance_fn_ptr_t *vfp,
1479 const MV *center_mv,
1481 return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1482 sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1483 center_mv, best_mv);
1486 int vp9_fast_dia_search(const MACROBLOCK *x,
1492 const vp9_variance_fn_ptr_t *vfp,
1494 const MV *center_mv,
1496 return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1497 sad_per_bit, do_init_search, cost_list, vfp,
1498 use_mvcost, center_mv, best_mv);
1503 int vp9_full_range_search_c(const MACROBLOCK *x,
1504 const search_site_config *cfg,
1505 MV *ref_mv, MV *best_mv,
1506 int search_param, int sad_per_bit, int *num00,
1507 const vp9_variance_fn_ptr_t *fn_ptr,
1508 const MV *center_mv) {
1509 const MACROBLOCKD *const xd = &x->e_mbd;
1510 const struct buf_2d *const what = &x->plane[0].src;
1511 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1512 const int range = 64;
1513 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1514 unsigned int best_sad = INT_MAX;
1516 int start_col, end_col, start_row, end_row;
1518 // The cfg and search_param parameters are not used in this search variant
1522 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1525 best_sad = fn_ptr->sdf(what->buf, what->stride,
1526 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1527 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1528 start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1529 start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1530 end_row = MIN(range, x->mv_row_max - ref_mv->row);
1531 end_col = MIN(range, x->mv_col_max - ref_mv->col);
1533 for (r = start_row; r <= end_row; ++r) {
1534 for (c = start_col; c <= end_col; c += 4) {
1535 if (c + 3 <= end_col) {
1536 unsigned int sads[4];
1537 const uint8_t *addrs[4];
1538 for (i = 0; i < 4; ++i) {
1539 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1540 addrs[i] = get_buf_from_mv(in_what, &mv);
1543 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1545 for (i = 0; i < 4; ++i) {
1546 if (sads[i] < best_sad) {
1547 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1548 const unsigned int sad = sads[i] +
1549 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1550 if (sad < best_sad) {
1557 for (i = 0; i < end_col - c; ++i) {
1558 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1559 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1560 get_buf_from_mv(in_what, &mv), in_what->stride);
1561 if (sad < best_sad) {
1562 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1563 if (sad < best_sad) {
1576 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1577 const search_site_config *cfg,
1578 MV *ref_mv, MV *best_mv, int search_param,
1579 int sad_per_bit, int *num00,
1580 const vp9_variance_fn_ptr_t *fn_ptr,
1581 const MV *center_mv) {
1584 const MACROBLOCKD *const xd = &x->e_mbd;
1585 uint8_t *what = x->plane[0].src.buf;
1586 const int what_stride = x->plane[0].src.stride;
1587 const uint8_t *in_what;
1588 const int in_what_stride = xd->plane[0].pre[0].stride;
1589 const uint8_t *best_address;
1591 unsigned int bestsad = INT_MAX;
1598 // search_param determines the length of the initial step and hence the number
1600 // 0 = initial step (MAX_FIRST_STEP) pel
1601 // 1 = (MAX_FIRST_STEP/2) pel,
1602 // 2 = (MAX_FIRST_STEP/4) pel...
1603 const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1604 const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1606 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1607 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1608 ref_row = ref_mv->row;
1609 ref_col = ref_mv->col;
1611 best_mv->row = ref_row;
1612 best_mv->col = ref_col;
1614 // Work out the start point for the search
1615 in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1616 best_address = in_what;
1618 // Check the starting position
1619 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1620 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1624 for (step = 0; step < tot_steps; step++) {
1627 // All_in is true if every one of the points we are checking are within
1628 // the bounds of the image.
1629 all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1630 all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1631 all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1632 all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1634 // If all the pixels are within the bounds we don't check whether the
1635 // search point is valid in this loop, otherwise we check each point
1638 unsigned int sad_array[4];
1640 for (j = 0; j < cfg->searches_per_step; j += 4) {
1641 unsigned char const *block_offset[4];
1643 for (t = 0; t < 4; t++)
1644 block_offset[t] = ss[i + t].offset + best_address;
1646 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1649 for (t = 0; t < 4; t++, i++) {
1650 if (sad_array[t] < bestsad) {
1651 const MV this_mv = {best_mv->row + ss[i].mv.row,
1652 best_mv->col + ss[i].mv.col};
1653 sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1655 if (sad_array[t] < bestsad) {
1656 bestsad = sad_array[t];
1663 for (j = 0; j < cfg->searches_per_step; j++) {
1664 // Trap illegal vectors
1665 const MV this_mv = {best_mv->row + ss[i].mv.row,
1666 best_mv->col + ss[i].mv.col};
1668 if (is_mv_in(x, &this_mv)) {
1669 const uint8_t *const check_here = ss[i].offset + best_address;
1670 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1673 if (thissad < bestsad) {
1674 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1675 if (thissad < bestsad) {
1684 if (best_site != last_site) {
1685 best_mv->row += ss[best_site].mv.row;
1686 best_mv->col += ss[best_site].mv.col;
1687 best_address += ss[best_site].offset;
1688 last_site = best_site;
1689 #if defined(NEW_DIAMOND_SEARCH)
1691 const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1692 best_mv->col + ss[best_site].mv.col};
1693 if (is_mv_in(x, &this_mv)) {
1694 const uint8_t *const check_here = ss[best_site].offset + best_address;
1695 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1697 if (thissad < bestsad) {
1698 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1699 if (thissad < bestsad) {
1701 best_mv->row += ss[best_site].mv.row;
1702 best_mv->col += ss[best_site].mv.col;
1703 best_address += ss[best_site].offset;
1711 } else if (best_address == in_what) {
1718 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1719 int best_sad = INT_MAX;
1722 int center, offset = 0;
1723 int bw = 4 << bwl; // redundant variable, to be changed in the experiments.
1724 for (d = 0; d <= bw; d += 16) {
1725 this_sad = vp9_vector_var(&ref[d], src, bwl);
1726 if (this_sad < best_sad) {
1727 best_sad = this_sad;
1733 for (d = -8; d <= 8; d += 16) {
1734 int this_pos = offset + d;
1736 if (this_pos < 0 || this_pos > bw)
1738 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1739 if (this_sad < best_sad) {
1740 best_sad = this_sad;
1746 for (d = -4; d <= 4; d += 8) {
1747 int this_pos = offset + d;
1749 if (this_pos < 0 || this_pos > bw)
1751 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1752 if (this_sad < best_sad) {
1753 best_sad = this_sad;
1759 for (d = -2; d <= 2; d += 4) {
1760 int this_pos = offset + d;
1762 if (this_pos < 0 || this_pos > bw)
1764 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1765 if (this_sad < best_sad) {
1766 best_sad = this_sad;
1772 for (d = -1; d <= 1; d += 2) {
1773 int this_pos = offset + d;
1775 if (this_pos < 0 || this_pos > bw)
1777 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1778 if (this_sad < best_sad) {
1779 best_sad = this_sad;
1784 return (center - (bw >> 1));
1787 static const MV search_pos[4] = {
1788 {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1791 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1793 MACROBLOCKD *xd = &x->e_mbd;
1794 DECLARE_ALIGNED(16, int16_t, hbuf[128]);
1795 DECLARE_ALIGNED(16, int16_t, vbuf[128]);
1796 DECLARE_ALIGNED(16, int16_t, src_hbuf[64]);
1797 DECLARE_ALIGNED(16, int16_t, src_vbuf[64]);
1799 const int bw = 4 << b_width_log2_lookup[bsize];
1800 const int bh = 4 << b_height_log2_lookup[bsize];
1801 const int search_width = bw << 1;
1802 const int search_height = bh << 1;
1803 const int src_stride = x->plane[0].src.stride;
1804 const int ref_stride = xd->plane[0].pre[0].stride;
1805 uint8_t const *ref_buf, *src_buf;
1806 MV *tmp_mv = &xd->mi[0]->mbmi.mv[0].as_mv;
1807 unsigned int best_sad, tmp_sad, this_sad[4];
1809 const int norm_factor = 3 + (bw >> 5);
1811 #if CONFIG_VP9_HIGHBITDEPTH
1814 return cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1815 xd->plane[0].pre[0].buf, ref_stride);
1818 // Set up prediction 1-D reference set
1819 ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1820 for (idx = 0; idx < search_width; idx += 16) {
1821 vp9_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1825 ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1826 for (idx = 0; idx < search_height; ++idx) {
1827 vbuf[idx] = vp9_int_pro_col(ref_buf, bw) >> norm_factor;
1828 ref_buf += ref_stride;
1831 // Set up src 1-D reference set
1832 for (idx = 0; idx < bw; idx += 16) {
1833 src_buf = x->plane[0].src.buf + idx;
1834 vp9_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1837 src_buf = x->plane[0].src.buf;
1838 for (idx = 0; idx < bh; ++idx) {
1839 src_vbuf[idx] = vp9_int_pro_col(src_buf, bw) >> norm_factor;
1840 src_buf += src_stride;
1843 // Find the best match per 1-D search
1844 tmp_mv->col = vector_match(hbuf, src_hbuf, b_width_log2_lookup[bsize]);
1845 tmp_mv->row = vector_match(vbuf, src_vbuf, b_height_log2_lookup[bsize]);
1848 src_buf = x->plane[0].src.buf;
1849 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1850 best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1853 const uint8_t * const pos[4] = {
1854 ref_buf - ref_stride,
1857 ref_buf + ref_stride,
1860 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1863 for (idx = 0; idx < 4; ++idx) {
1864 if (this_sad[idx] < best_sad) {
1865 best_sad = this_sad[idx];
1866 tmp_mv->row = search_pos[idx].row + this_mv.row;
1867 tmp_mv->col = search_pos[idx].col + this_mv.col;
1871 if (this_sad[0] < this_sad[3])
1876 if (this_sad[1] < this_sad[2])
1881 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1883 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1884 ref_buf, ref_stride);
1885 if (best_sad > tmp_sad) {
1896 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1897 point as the best match, we will do a final 1-away diamond
1899 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1900 MV *mvp_full, int step_param,
1901 int sadpb, int further_steps, int do_refine,
1903 const vp9_variance_fn_ptr_t *fn_ptr,
1904 const MV *ref_mv, MV *dst_mv) {
1906 int thissme, n, num00 = 0;
1907 int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1908 step_param, sadpb, &n,
1910 if (bestsme < INT_MAX)
1911 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1914 // If there won't be more n-step search, check to see if refining search is
1916 if (n > further_steps)
1919 while (n < further_steps) {
1925 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1926 step_param + n, sadpb, &num00,
1928 if (thissme < INT_MAX)
1929 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1931 // check to see if refining search is needed.
1932 if (num00 > further_steps - n)
1935 if (thissme < bestsme) {
1942 // final 1-away diamond refining search
1944 const int search_range = 8;
1945 MV best_mv = *dst_mv;
1946 thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1948 if (thissme < INT_MAX)
1949 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1950 if (thissme < bestsme) {
1956 // Return cost list.
1958 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1963 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1964 int sad_per_bit, int distance,
1965 const vp9_variance_fn_ptr_t *fn_ptr,
1966 const MV *center_mv, MV *best_mv) {
1968 const MACROBLOCKD *const xd = &x->e_mbd;
1969 const struct buf_2d *const what = &x->plane[0].src;
1970 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1971 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1972 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1973 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1974 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1975 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1976 int best_sad = fn_ptr->sdf(what->buf, what->stride,
1977 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1978 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1981 for (r = row_min; r < row_max; ++r) {
1982 for (c = col_min; c < col_max; ++c) {
1983 const MV mv = {r, c};
1984 const int sad = fn_ptr->sdf(what->buf, what->stride,
1985 get_buf_from_mv(in_what, &mv), in_what->stride) +
1986 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1987 if (sad < best_sad) {
1996 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
1997 int sad_per_bit, int distance,
1998 const vp9_variance_fn_ptr_t *fn_ptr,
1999 const MV *center_mv, MV *best_mv) {
2001 const MACROBLOCKD *const xd = &x->e_mbd;
2002 const struct buf_2d *const what = &x->plane[0].src;
2003 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2004 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2005 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2006 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2007 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2008 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2009 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2010 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2011 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2014 for (r = row_min; r < row_max; ++r) {
2016 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2018 if (fn_ptr->sdx3f != NULL) {
2019 while ((c + 2) < col_max) {
2021 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2023 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2026 for (i = 0; i < 3; ++i) {
2027 unsigned int sad = sads[i];
2028 if (sad < best_sad) {
2029 const MV mv = {r, c};
2030 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2031 if (sad < best_sad) {
2042 while (c < col_max) {
2043 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2044 check_here, in_what->stride);
2045 if (sad < best_sad) {
2046 const MV mv = {r, c};
2047 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2048 if (sad < best_sad) {
2061 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
2062 int sad_per_bit, int distance,
2063 const vp9_variance_fn_ptr_t *fn_ptr,
2064 const MV *center_mv, MV *best_mv) {
2066 const MACROBLOCKD *const xd = &x->e_mbd;
2067 const struct buf_2d *const what = &x->plane[0].src;
2068 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2069 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2070 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2071 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2072 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2073 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2074 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2075 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2076 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2079 for (r = row_min; r < row_max; ++r) {
2081 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2083 if (fn_ptr->sdx8f != NULL) {
2084 while ((c + 7) < col_max) {
2086 DECLARE_ALIGNED(16, uint32_t, sads[8]);
2088 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2091 for (i = 0; i < 8; ++i) {
2092 unsigned int sad = sads[i];
2093 if (sad < best_sad) {
2094 const MV mv = {r, c};
2095 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2096 if (sad < best_sad) {
2107 if (fn_ptr->sdx3f != NULL) {
2108 while ((c + 2) < col_max) {
2110 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2112 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2115 for (i = 0; i < 3; ++i) {
2116 unsigned int sad = sads[i];
2117 if (sad < best_sad) {
2118 const MV mv = {r, c};
2119 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2120 if (sad < best_sad) {
2131 while (c < col_max) {
2132 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2133 check_here, in_what->stride);
2134 if (sad < best_sad) {
2135 const MV mv = {r, c};
2136 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2137 if (sad < best_sad) {
2150 int vp9_refining_search_sad(const MACROBLOCK *x,
2151 MV *ref_mv, int error_per_bit,
2153 const vp9_variance_fn_ptr_t *fn_ptr,
2154 const MV *center_mv) {
2155 const MACROBLOCKD *const xd = &x->e_mbd;
2156 const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
2157 const struct buf_2d *const what = &x->plane[0].src;
2158 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2159 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2160 const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
2161 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
2163 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2166 for (i = 0; i < search_range; i++) {
2168 const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
2169 ((ref_mv->row + 1) < x->mv_row_max) &
2170 ((ref_mv->col - 1) > x->mv_col_min) &
2171 ((ref_mv->col + 1) < x->mv_col_max);
2174 unsigned int sads[4];
2175 const uint8_t *const positions[4] = {
2176 best_address - in_what->stride,
2179 best_address + in_what->stride
2182 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2184 for (j = 0; j < 4; ++j) {
2185 if (sads[j] < best_sad) {
2186 const MV mv = {ref_mv->row + neighbors[j].row,
2187 ref_mv->col + neighbors[j].col};
2188 sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2189 if (sads[j] < best_sad) {
2196 for (j = 0; j < 4; ++j) {
2197 const MV mv = {ref_mv->row + neighbors[j].row,
2198 ref_mv->col + neighbors[j].col};
2200 if (is_mv_in(x, &mv)) {
2201 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2202 get_buf_from_mv(in_what, &mv),
2204 if (sad < best_sad) {
2205 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2206 if (sad < best_sad) {
2215 if (best_site == -1) {
2218 ref_mv->row += neighbors[best_site].row;
2219 ref_mv->col += neighbors[best_site].col;
2220 best_address = get_buf_from_mv(in_what, ref_mv);
2227 // This function is called when we do joint motion search in comp_inter_inter
2229 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2230 MV *ref_mv, int error_per_bit,
2232 const vp9_variance_fn_ptr_t *fn_ptr,
2233 const MV *center_mv,
2234 const uint8_t *second_pred) {
2235 const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
2236 {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
2237 const MACROBLOCKD *const xd = &x->e_mbd;
2238 const struct buf_2d *const what = &x->plane[0].src;
2239 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2240 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2241 unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
2242 get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
2243 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2246 for (i = 0; i < search_range; ++i) {
2249 for (j = 0; j < 8; ++j) {
2250 const MV mv = {ref_mv->row + neighbors[j].row,
2251 ref_mv->col + neighbors[j].col};
2253 if (is_mv_in(x, &mv)) {
2254 unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
2255 get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
2256 if (sad < best_sad) {
2257 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2258 if (sad < best_sad) {
2266 if (best_site == -1) {
2269 ref_mv->row += neighbors[best_site].row;
2270 ref_mv->col += neighbors[best_site].col;
2276 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
2277 BLOCK_SIZE bsize, MV *mvp_full,
2278 int step_param, int error_per_bit,
2280 const MV *ref_mv, MV *tmp_mv,
2281 int var_max, int rd) {
2282 const SPEED_FEATURES *const sf = &cpi->sf;
2283 const SEARCH_METHODS method = sf->mv.search_method;
2284 vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
2287 cost_list[0] = INT_MAX;
2288 cost_list[1] = INT_MAX;
2289 cost_list[2] = INT_MAX;
2290 cost_list[3] = INT_MAX;
2291 cost_list[4] = INT_MAX;
2296 var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2297 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2300 var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2301 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2304 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2305 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2308 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2309 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2312 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2313 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2316 var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2317 MAX_MVSEARCH_STEPS - 1 - step_param,
2318 1, cost_list, fn_ptr, ref_mv, tmp_mv);
2321 assert(0 && "Invalid search method.");
2324 if (method != NSTEP && rd && var < var_max)
2325 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);