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,
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);
102 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride) {
103 int len, ss_count = 1;
105 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
106 cfg->ss[0].offset = 0;
108 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
109 // Generate offsets for 4 search sites per step.
110 const MV ss_mvs[] = {{-len, 0}, {len, 0}, {0, -len}, {0, len}};
112 for (i = 0; i < 4; ++i) {
113 search_site *const ss = &cfg->ss[ss_count++];
115 ss->offset = ss->mv.row * stride + ss->mv.col;
119 cfg->ss_count = ss_count;
120 cfg->searches_per_step = 4;
123 void vp9_init3smotion_compensation(search_site_config *cfg, int stride) {
124 int len, ss_count = 1;
126 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
127 cfg->ss[0].offset = 0;
129 for (len = MAX_FIRST_STEP; len > 0; len /= 2) {
130 // Generate offsets for 8 search sites per step.
131 const MV ss_mvs[8] = {
132 {-len, 0 }, {len, 0 }, { 0, -len}, {0, len},
133 {-len, -len}, {-len, len}, {len, -len}, {len, len}
136 for (i = 0; i < 8; ++i) {
137 search_site *const ss = &cfg->ss[ss_count++];
139 ss->offset = ss->mv.row * stride + ss->mv.col;
143 cfg->ss_count = ss_count;
144 cfg->searches_per_step = 8;
148 * To avoid the penalty for crossing cache-line read, preload the reference
149 * area in a small buffer, which is aligned to make sure there won't be crossing
150 * cache-line read while reading from this buffer. This reduced the cpu
151 * cycles spent on reading ref data in sub-pixel filter functions.
152 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
153 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
154 * could reduce the area.
157 /* estimated cost of a motion vector (r,c) */
160 ((mvjcost[((r) != rr) * 2 + ((c) != rc)] + \
161 mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
162 error_per_bit + 4096) >> 13 : 0)
165 // convert motion vector component to offset for svf calc
166 static INLINE int sp(int x) {
170 static INLINE const uint8_t *pre(const uint8_t *buf, int stride, int r, int c) {
171 return &buf[(r >> 3) * stride + (c >> 3)];
174 /* checks if (r, c) has better score than previous best */
175 #define CHECK_BETTER(v, r, c) \
176 if (c >= minc && c <= maxc && r >= minr && r <= maxr) { \
177 if (second_pred == NULL) \
178 thismse = vfp->svf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), z, \
181 thismse = vfp->svaf(pre(y, y_stride, r, c), y_stride, sp(c), sp(r), \
182 z, src_stride, &sse, second_pred); \
183 if ((v = MVC(r, c) + thismse) < besterr) { \
187 *distortion = thismse; \
194 #define FIRST_LEVEL_CHECKS \
196 unsigned int left, right, up, down, diag; \
197 CHECK_BETTER(left, tr, tc - hstep); \
198 CHECK_BETTER(right, tr, tc + hstep); \
199 CHECK_BETTER(up, tr - hstep, tc); \
200 CHECK_BETTER(down, tr + hstep, tc); \
201 whichdir = (left < right ? 0 : 1) + \
202 (up < down ? 0 : 2); \
203 switch (whichdir) { \
205 CHECK_BETTER(diag, tr - hstep, tc - hstep); \
208 CHECK_BETTER(diag, tr - hstep, tc + hstep); \
211 CHECK_BETTER(diag, tr + hstep, tc - hstep); \
214 CHECK_BETTER(diag, tr + hstep, tc + hstep); \
219 #define SECOND_LEVEL_CHECKS \
222 unsigned int second; \
223 if (tr != br && tc != bc) { \
226 CHECK_BETTER(second, tr + kr, tc + 2 * kc); \
227 CHECK_BETTER(second, tr + 2 * kr, tc + kc); \
228 } else if (tr == br && tc != bc) { \
230 CHECK_BETTER(second, tr + hstep, tc + 2 * kc); \
231 CHECK_BETTER(second, tr - hstep, tc + 2 * kc); \
232 switch (whichdir) { \
235 CHECK_BETTER(second, tr + hstep, tc + kc); \
239 CHECK_BETTER(second, tr - hstep, tc + kc); \
242 } else if (tr != br && tc == bc) { \
244 CHECK_BETTER(second, tr + 2 * kr, tc + hstep); \
245 CHECK_BETTER(second, tr + 2 * kr, tc - hstep); \
246 switch (whichdir) { \
249 CHECK_BETTER(second, tr + kr, tc + hstep); \
253 CHECK_BETTER(second, tr + kr, tc - hstep); \
259 #define SETUP_SUBPEL_SEARCH \
260 const uint8_t *const z = x->plane[0].src.buf; \
261 const int src_stride = x->plane[0].src.stride; \
262 const MACROBLOCKD *xd = &x->e_mbd; \
263 unsigned int besterr = INT_MAX; \
265 unsigned int whichdir; \
267 const unsigned int halfiters = iters_per_step; \
268 const unsigned int quarteriters = iters_per_step; \
269 const unsigned int eighthiters = iters_per_step; \
270 const int y_stride = xd->plane[0].pre[0].stride; \
271 const int offset = bestmv->row * y_stride + bestmv->col; \
272 const uint8_t *const y = xd->plane[0].pre[0].buf; \
274 int rr = ref_mv->row; \
275 int rc = ref_mv->col; \
276 int br = bestmv->row * 8; \
277 int bc = bestmv->col * 8; \
279 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX); \
280 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX); \
281 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX); \
282 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX); \
289 #if CONFIG_VP9_HIGHBITDEPTH
290 #define SETUP_CENTER_ERROR \
291 if (second_pred != NULL) { \
292 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { \
293 DECLARE_ALIGNED_ARRAY(16, uint16_t, comp_pred16, 64 * 64); \
294 vp9_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset, \
296 besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, z, src_stride, \
299 DECLARE_ALIGNED_ARRAY(16, uint8_t, comp_pred, 64 * 64); \
300 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride); \
301 besterr = vfp->vf(comp_pred, w, z, src_stride, sse1); \
304 besterr = vfp->vf(y + offset, y_stride, z, src_stride, sse1); \
306 *distortion = besterr; \
307 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
311 #define SETUP_CENTER_ERROR \
312 if (second_pred != NULL) { \
313 DECLARE_ALIGNED_ARRAY(16, uint8_t, comp_pred, 64 * 64); \
314 vp9_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride); \
315 besterr = vfp->vf(comp_pred, w, z, src_stride, sse1); \
317 besterr = vfp->vf(y + offset, y_stride, z, src_stride, sse1); \
319 *distortion = besterr; \
320 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
321 #endif // CONFIG_VP9_HIGHBITDEPTH
326 static INLINE int divide_and_round(const int n, const int d) {
327 return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
330 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
331 return cost_list[0] < cost_list[1] &&
332 cost_list[0] < cost_list[2] &&
333 cost_list[0] < cost_list[3] &&
334 cost_list[0] < cost_list[4];
337 // Returns surface minima estimate at given precision in 1/2^n bits.
338 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
339 // For a given set of costs S0, S1, S2, S3, S4 at points
340 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
341 // the solution for the location of the minima (x0, y0) is given by:
342 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
343 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
344 // The code below is an integerized version of that.
345 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
347 *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
348 (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
349 *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
350 (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
353 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
355 MV *bestmv, const MV *ref_mv,
358 const vp9_variance_fn_ptr_t *vfp,
362 int *mvjcost, int *mvcost[2],
365 const uint8_t *second_pred,
378 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
379 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
380 cost_list[4] != INT_MAX &&
381 is_cost_list_wellbehaved(cost_list)) {
384 get_cost_surf_min(cost_list, &ir, &ic, 2);
385 if (ir != 0 || ic != 0) {
386 CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
397 // Each subsequent iteration checks at least one point in common with
398 // the last iteration could be 2 ( if diag selected) 1/4 pel
399 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
400 if (forced_stop != 2) {
403 if (quarteriters > 1) {
412 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
415 if (eighthiters > 1) {
423 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
424 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
430 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
431 MV *bestmv, const MV *ref_mv,
434 const vp9_variance_fn_ptr_t *vfp,
438 int *mvjcost, int *mvcost[2],
441 const uint8_t *second_pred,
446 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
447 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
448 cost_list[4] != INT_MAX &&
449 is_cost_list_wellbehaved(cost_list)) {
452 get_cost_surf_min(cost_list, &ir, &ic, 1);
453 if (ir != 0 || ic != 0) {
454 CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
463 // Each subsequent iteration checks at least one point in common with
464 // the last iteration could be 2 ( if diag selected) 1/4 pel
466 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
467 if (forced_stop != 2) {
472 if (quarteriters > 1) {
477 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
482 if (eighthiters > 1) {
486 // These lines insure static analysis doesn't warn that
487 // tr and tc aren't used after the above point.
494 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
495 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
501 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
502 MV *bestmv, const MV *ref_mv,
505 const vp9_variance_fn_ptr_t *vfp,
509 int *mvjcost, int *mvcost[2],
512 const uint8_t *second_pred,
517 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
518 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
519 cost_list[4] != INT_MAX) {
520 unsigned int left, right, up, down, diag;
521 whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
522 (cost_list[2] < cost_list[4] ? 0 : 2);
525 CHECK_BETTER(left, tr, tc - hstep);
526 CHECK_BETTER(down, tr + hstep, tc);
527 CHECK_BETTER(diag, tr + hstep, tc - hstep);
530 CHECK_BETTER(right, tr, tc + hstep);
531 CHECK_BETTER(down, tr + hstep, tc);
532 CHECK_BETTER(diag, tr + hstep, tc + hstep);
535 CHECK_BETTER(left, tr, tc - hstep);
536 CHECK_BETTER(up, tr - hstep, tc);
537 CHECK_BETTER(diag, tr - hstep, tc - hstep);
540 CHECK_BETTER(right, tr, tc + hstep);
541 CHECK_BETTER(up, tr - hstep, tc);
542 CHECK_BETTER(diag, tr - hstep, tc + hstep);
555 // Each subsequent iteration checks at least one point in common with
556 // the last iteration could be 2 ( if diag selected) 1/4 pel
558 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
559 if (forced_stop != 2) {
562 if (quarteriters > 1) {
569 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
572 if (eighthiters > 1) {
578 // These lines insure static analysis doesn't warn that
579 // tr and tc aren't used after the above point.
586 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
587 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
593 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
594 MV *bestmv, const MV *ref_mv,
597 const vp9_variance_fn_ptr_t *vfp,
601 int *mvjcost, int *mvcost[2],
604 const uint8_t *second_pred,
608 (void) cost_list; // to silence compiler warning
610 // Each subsequent iteration checks at least one point in
611 // common with the last iteration could be 2 ( if diag selected)
620 // Each subsequent iteration checks at least one point in common with
621 // the last iteration could be 2 ( if diag selected) 1/4 pel
623 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
624 if (forced_stop != 2) {
627 if (quarteriters > 1) {
634 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
637 if (eighthiters > 1) {
643 // These lines insure static analysis doesn't warn that
644 // tr and tc aren't used after the above point.
651 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
652 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
662 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
664 return ((row - range) >= x->mv_row_min) &
665 ((row + range) <= x->mv_row_max) &
666 ((col - range) >= x->mv_col_min) &
667 ((col + range) <= x->mv_col_max);
670 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
671 return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
672 (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
675 #define CHECK_BETTER \
677 if (thissad < bestsad) {\
679 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
680 if (thissad < bestsad) {\
687 #define MAX_PATTERN_SCALES 11
688 #define MAX_PATTERN_CANDIDATES 8 // max number of canddiates per scale
689 #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
691 // Calculate and return a sad+mvcost list around an integer best pel.
692 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
695 const vp9_variance_fn_ptr_t *fn_ptr,
698 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
699 const struct buf_2d *const what = &x->plane[0].src;
700 const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
701 const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
702 int br = best_mv->row;
703 int bc = best_mv->col;
710 cost_list[0] = fn_ptr->vf(what->buf, what->stride,
711 get_buf_from_mv(in_what, &this_mv),
712 in_what->stride, &sse) +
713 mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
714 if (check_bounds(x, br, bc, 1)) {
715 for (i = 0; i < 4; i++) {
716 const MV this_mv = {br + neighbors[i].row,
717 bc + neighbors[i].col};
718 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
719 get_buf_from_mv(in_what, &this_mv),
720 in_what->stride, &sse) +
721 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
722 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
726 for (i = 0; i < 4; i++) {
727 const MV this_mv = {br + neighbors[i].row,
728 bc + neighbors[i].col};
729 if (!is_mv_in(x, &this_mv))
730 cost_list[i + 1] = INT_MAX;
732 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
733 get_buf_from_mv(in_what, &this_mv),
734 in_what->stride, &sse) +
735 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
736 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
742 // Generic pattern search function that searches over multiple scales.
743 // Each scale can have a different number of candidates and shape of
744 // candidates as indicated in the num_candidates and candidates arrays
745 // passed into this function
747 static int vp9_pattern_search(const MACROBLOCK *x,
753 const vp9_variance_fn_ptr_t *vfp,
757 const int num_candidates[MAX_PATTERN_SCALES],
758 const MV candidates[MAX_PATTERN_SCALES]
759 [MAX_PATTERN_CANDIDATES]) {
760 const MACROBLOCKD *const xd = &x->e_mbd;
761 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
762 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
765 const struct buf_2d *const what = &x->plane[0].src;
766 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
768 int bestsad = INT_MAX;
771 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
772 int best_init_s = search_param_to_steps[search_param];
773 // adjust ref_mv to make sure it is within MV range
774 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
778 // Work out the start point for the search
779 bestsad = vfp->sdf(what->buf, what->stride,
780 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
781 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
783 // Search all possible scales upto the search param around the center point
784 // pick the scale of the point that is best as the starting scale of
785 // further steps around it.
786 if (do_init_search) {
789 for (t = 0; t <= s; ++t) {
791 if (check_bounds(x, br, bc, 1 << t)) {
792 for (i = 0; i < num_candidates[t]; i++) {
793 const MV this_mv = {br + candidates[t][i].row,
794 bc + candidates[t][i].col};
795 thissad = vfp->sdf(what->buf, what->stride,
796 get_buf_from_mv(in_what, &this_mv),
801 for (i = 0; i < num_candidates[t]; i++) {
802 const MV this_mv = {br + candidates[t][i].row,
803 bc + candidates[t][i].col};
804 if (!is_mv_in(x, &this_mv))
806 thissad = vfp->sdf(what->buf, what->stride,
807 get_buf_from_mv(in_what, &this_mv),
812 if (best_site == -1) {
819 if (best_init_s != -1) {
820 br += candidates[best_init_s][k].row;
821 bc += candidates[best_init_s][k].col;
825 // If the center point is still the best, just skip this and move to
826 // the refinement step.
827 if (best_init_s != -1) {
832 // No need to search all 6 points the 1st time if initial search was used
833 if (!do_init_search || s != best_init_s) {
834 if (check_bounds(x, br, bc, 1 << s)) {
835 for (i = 0; i < num_candidates[s]; i++) {
836 const MV this_mv = {br + candidates[s][i].row,
837 bc + candidates[s][i].col};
838 thissad = vfp->sdf(what->buf, what->stride,
839 get_buf_from_mv(in_what, &this_mv),
844 for (i = 0; i < num_candidates[s]; i++) {
845 const MV this_mv = {br + candidates[s][i].row,
846 bc + candidates[s][i].col};
847 if (!is_mv_in(x, &this_mv))
849 thissad = vfp->sdf(what->buf, what->stride,
850 get_buf_from_mv(in_what, &this_mv),
856 if (best_site == -1) {
859 br += candidates[s][best_site].row;
860 bc += candidates[s][best_site].col;
866 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
868 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
869 next_chkpts_indices[1] = k;
870 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
872 if (check_bounds(x, br, bc, 1 << s)) {
873 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
874 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
875 bc + candidates[s][next_chkpts_indices[i]].col};
876 thissad = vfp->sdf(what->buf, what->stride,
877 get_buf_from_mv(in_what, &this_mv),
882 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
883 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
884 bc + candidates[s][next_chkpts_indices[i]].col};
885 if (!is_mv_in(x, &this_mv))
887 thissad = vfp->sdf(what->buf, what->stride,
888 get_buf_from_mv(in_what, &this_mv),
894 if (best_site != -1) {
895 k = next_chkpts_indices[best_site];
896 br += candidates[s][k].row;
897 bc += candidates[s][k].col;
899 } while (best_site != -1);
903 // Returns the one-away integer pel sad values around the best as follows:
904 // cost_list[0]: cost at the best integer pel
905 // cost_list[1]: cost at delta {0, -1} (left) from the best integer pel
906 // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
907 // cost_list[3]: cost at delta { 0, 1} (right) from the best integer pel
908 // cost_list[4]: cost at delta {-1, 0} (top) from the best integer pel
910 const MV best_mv = { br, bc };
911 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
918 // A specialized function where the smallest scale search candidates
919 // are 4 1-away neighbors, and cost_list is non-null
920 // TODO(debargha): Merge this function with the one above. Also remove
921 // use_mvcost option since it is always 1, to save unnecessary branches.
922 static int vp9_pattern_search_sad(const MACROBLOCK *x,
928 const vp9_variance_fn_ptr_t *vfp,
932 const int num_candidates[MAX_PATTERN_SCALES],
933 const MV candidates[MAX_PATTERN_SCALES]
934 [MAX_PATTERN_CANDIDATES]) {
935 const MACROBLOCKD *const xd = &x->e_mbd;
936 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
937 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
940 const struct buf_2d *const what = &x->plane[0].src;
941 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
943 int bestsad = INT_MAX;
946 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
947 int best_init_s = search_param_to_steps[search_param];
948 // adjust ref_mv to make sure it is within MV range
949 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
952 if (cost_list != NULL) {
953 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
957 // Work out the start point for the search
958 bestsad = vfp->sdf(what->buf, what->stride,
959 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
960 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
962 // Search all possible scales upto the search param around the center point
963 // pick the scale of the point that is best as the starting scale of
964 // further steps around it.
965 if (do_init_search) {
968 for (t = 0; t <= s; ++t) {
970 if (check_bounds(x, br, bc, 1 << t)) {
971 for (i = 0; i < num_candidates[t]; i++) {
972 const MV this_mv = {br + candidates[t][i].row,
973 bc + candidates[t][i].col};
974 thissad = vfp->sdf(what->buf, what->stride,
975 get_buf_from_mv(in_what, &this_mv),
980 for (i = 0; i < num_candidates[t]; i++) {
981 const MV this_mv = {br + candidates[t][i].row,
982 bc + candidates[t][i].col};
983 if (!is_mv_in(x, &this_mv))
985 thissad = vfp->sdf(what->buf, what->stride,
986 get_buf_from_mv(in_what, &this_mv),
991 if (best_site == -1) {
998 if (best_init_s != -1) {
999 br += candidates[best_init_s][k].row;
1000 bc += candidates[best_init_s][k].col;
1004 // If the center point is still the best, just skip this and move to
1005 // the refinement step.
1006 if (best_init_s != -1) {
1007 int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1011 for (; s >= do_sad; s--) {
1012 if (!do_init_search || s != best_init_s) {
1013 if (check_bounds(x, br, bc, 1 << s)) {
1014 for (i = 0; i < num_candidates[s]; i++) {
1015 const MV this_mv = {br + candidates[s][i].row,
1016 bc + candidates[s][i].col};
1017 thissad = vfp->sdf(what->buf, what->stride,
1018 get_buf_from_mv(in_what, &this_mv),
1023 for (i = 0; i < num_candidates[s]; i++) {
1024 const MV this_mv = {br + candidates[s][i].row,
1025 bc + candidates[s][i].col};
1026 if (!is_mv_in(x, &this_mv))
1028 thissad = vfp->sdf(what->buf, what->stride,
1029 get_buf_from_mv(in_what, &this_mv),
1035 if (best_site == -1) {
1038 br += candidates[s][best_site].row;
1039 bc += candidates[s][best_site].col;
1045 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1047 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1048 next_chkpts_indices[1] = k;
1049 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1051 if (check_bounds(x, br, bc, 1 << s)) {
1052 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1053 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1054 bc + candidates[s][next_chkpts_indices[i]].col};
1055 thissad = vfp->sdf(what->buf, what->stride,
1056 get_buf_from_mv(in_what, &this_mv),
1061 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1062 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1063 bc + candidates[s][next_chkpts_indices[i]].col};
1064 if (!is_mv_in(x, &this_mv))
1066 thissad = vfp->sdf(what->buf, what->stride,
1067 get_buf_from_mv(in_what, &this_mv),
1073 if (best_site != -1) {
1074 k = next_chkpts_indices[best_site];
1075 br += candidates[s][k].row;
1076 bc += candidates[s][k].col;
1078 } while (best_site != -1);
1081 // Note: If we enter the if below, then cost_list must be non-NULL.
1083 cost_list[0] = bestsad;
1084 if (!do_init_search || s != best_init_s) {
1085 if (check_bounds(x, br, bc, 1 << s)) {
1086 for (i = 0; i < num_candidates[s]; i++) {
1087 const MV this_mv = {br + candidates[s][i].row,
1088 bc + candidates[s][i].col};
1090 thissad = vfp->sdf(what->buf, what->stride,
1091 get_buf_from_mv(in_what, &this_mv),
1096 for (i = 0; i < num_candidates[s]; i++) {
1097 const MV this_mv = {br + candidates[s][i].row,
1098 bc + candidates[s][i].col};
1099 if (!is_mv_in(x, &this_mv))
1102 thissad = vfp->sdf(what->buf, what->stride,
1103 get_buf_from_mv(in_what, &this_mv),
1109 if (best_site != -1) {
1110 br += candidates[s][best_site].row;
1111 bc += candidates[s][best_site].col;
1115 while (best_site != -1) {
1116 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1118 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1119 next_chkpts_indices[1] = k;
1120 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1121 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1122 cost_list[((k + 2) % 4) + 1] = cost_list[0];
1123 cost_list[0] = bestsad;
1125 if (check_bounds(x, br, bc, 1 << s)) {
1126 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1127 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1128 bc + candidates[s][next_chkpts_indices[i]].col};
1129 cost_list[next_chkpts_indices[i] + 1] =
1130 thissad = vfp->sdf(what->buf, what->stride,
1131 get_buf_from_mv(in_what, &this_mv),
1136 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1137 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1138 bc + candidates[s][next_chkpts_indices[i]].col};
1139 if (!is_mv_in(x, &this_mv)) {
1140 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1143 cost_list[next_chkpts_indices[i] + 1] =
1144 thissad = vfp->sdf(what->buf, what->stride,
1145 get_buf_from_mv(in_what, &this_mv),
1151 if (best_site != -1) {
1152 k = next_chkpts_indices[best_site];
1153 br += candidates[s][k].row;
1154 bc += candidates[s][k].col;
1160 // Returns the one-away integer pel sad values around the best as follows:
1161 // cost_list[0]: sad at the best integer pel
1162 // cost_list[1]: sad at delta {0, -1} (left) from the best integer pel
1163 // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1164 // cost_list[3]: sad at delta { 0, 1} (right) from the best integer pel
1165 // cost_list[4]: sad at delta {-1, 0} (top) from the best integer pel
1167 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1168 if (cost_list[0] == INT_MAX) {
1169 cost_list[0] = bestsad;
1170 if (check_bounds(x, br, bc, 1)) {
1171 for (i = 0; i < 4; i++) {
1172 const MV this_mv = { br + neighbors[i].row,
1173 bc + neighbors[i].col };
1174 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1175 get_buf_from_mv(in_what, &this_mv),
1179 for (i = 0; i < 4; i++) {
1180 const MV this_mv = {br + neighbors[i].row,
1181 bc + neighbors[i].col};
1182 if (!is_mv_in(x, &this_mv))
1183 cost_list[i + 1] = INT_MAX;
1185 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1186 get_buf_from_mv(in_what, &this_mv),
1192 for (i = 0; i < 4; i++) {
1193 const MV this_mv = {br + neighbors[i].row,
1194 bc + neighbors[i].col};
1195 if (cost_list[i + 1] != INT_MAX) {
1197 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1208 int vp9_get_mvpred_var(const MACROBLOCK *x,
1209 const MV *best_mv, const MV *center_mv,
1210 const vp9_variance_fn_ptr_t *vfp,
1212 const MACROBLOCKD *const xd = &x->e_mbd;
1213 const struct buf_2d *const what = &x->plane[0].src;
1214 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1215 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1216 unsigned int unused;
1218 return vfp->vf(what->buf, what->stride,
1219 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1220 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1221 x->mvcost, x->errorperbit) : 0);
1224 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1225 const MV *best_mv, const MV *center_mv,
1226 const uint8_t *second_pred,
1227 const vp9_variance_fn_ptr_t *vfp,
1229 const MACROBLOCKD *const xd = &x->e_mbd;
1230 const struct buf_2d *const what = &x->plane[0].src;
1231 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1232 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1233 unsigned int unused;
1235 return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1236 what->buf, what->stride, &unused, second_pred) +
1237 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1238 x->mvcost, x->errorperbit) : 0);
1241 int vp9_hex_search(const MACROBLOCK *x,
1247 const vp9_variance_fn_ptr_t *vfp,
1249 const MV *center_mv, MV *best_mv) {
1250 // First scale has 8-closest points, the rest have 6 points in hex shape
1251 // at increasing scales
1252 static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1253 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1255 // Note that the largest candidate step at each scale is 2^scale
1256 static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1257 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1258 {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1259 {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1260 {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1261 {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1262 {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1263 {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1264 {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1265 {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1266 {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1267 {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1270 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1271 do_init_search, cost_list, vfp, use_mvcost,
1273 hex_num_candidates, hex_candidates);
1276 int vp9_bigdia_search(const MACROBLOCK *x,
1282 const vp9_variance_fn_ptr_t *vfp,
1284 const MV *center_mv,
1286 // First scale has 4-closest points, the rest have 8 points in diamond
1287 // shape at increasing scales
1288 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1289 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1291 // Note that the largest candidate step at each scale is 2^scale
1292 static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1293 [MAX_PATTERN_CANDIDATES] = {
1294 {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1295 {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1296 {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1297 {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1298 {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1299 {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1300 {-16, 16}, {-32, 0}},
1301 {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1302 {-32, 32}, {-64, 0}},
1303 {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1304 {-64, 64}, {-128, 0}},
1305 {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1306 {-128, 128}, {-256, 0}},
1307 {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1308 {-256, 256}, {-512, 0}},
1309 {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1310 {-512, 512}, {-1024, 0}},
1312 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1313 do_init_search, cost_list, vfp, use_mvcost,
1315 bigdia_num_candidates, bigdia_candidates);
1318 int vp9_square_search(const MACROBLOCK *x,
1324 const vp9_variance_fn_ptr_t *vfp,
1326 const MV *center_mv,
1328 // All scales have 8 closest points in square shape
1329 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1330 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1332 // Note that the largest candidate step at each scale is 2^scale
1333 static const MV square_candidates[MAX_PATTERN_SCALES]
1334 [MAX_PATTERN_CANDIDATES] = {
1335 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1336 {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1337 {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1338 {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1339 {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1340 {-16, 16}, {-16, 0}},
1341 {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1342 {-32, 32}, {-32, 0}},
1343 {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1344 {-64, 64}, {-64, 0}},
1345 {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1346 {-128, 128}, {-128, 0}},
1347 {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1348 {-256, 256}, {-256, 0}},
1349 {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1350 {-512, 512}, {-512, 0}},
1351 {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1352 {0, 1024}, {-1024, 1024}, {-1024, 0}},
1354 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1355 do_init_search, cost_list, vfp, use_mvcost,
1357 square_num_candidates, square_candidates);
1360 int vp9_fast_hex_search(const MACROBLOCK *x,
1364 int do_init_search, // must be zero for fast_hex
1366 const vp9_variance_fn_ptr_t *vfp,
1368 const MV *center_mv,
1370 return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1371 sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1372 center_mv, best_mv);
1375 int vp9_fast_dia_search(const MACROBLOCK *x,
1381 const vp9_variance_fn_ptr_t *vfp,
1383 const MV *center_mv,
1385 return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1386 sad_per_bit, do_init_search, cost_list, vfp,
1387 use_mvcost, center_mv, best_mv);
1392 int vp9_full_range_search_c(const MACROBLOCK *x,
1393 const search_site_config *cfg,
1394 MV *ref_mv, MV *best_mv,
1395 int search_param, int sad_per_bit, int *num00,
1396 const vp9_variance_fn_ptr_t *fn_ptr,
1397 const MV *center_mv) {
1398 const MACROBLOCKD *const xd = &x->e_mbd;
1399 const struct buf_2d *const what = &x->plane[0].src;
1400 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1401 const int range = 64;
1402 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1403 unsigned int best_sad = INT_MAX;
1405 int start_col, end_col, start_row, end_row;
1407 // The cfg and search_param parameters are not used in this search variant
1411 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1414 best_sad = fn_ptr->sdf(what->buf, what->stride,
1415 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1416 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1417 start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1418 start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1419 end_row = MIN(range, x->mv_row_max - ref_mv->row);
1420 end_col = MIN(range, x->mv_col_max - ref_mv->col);
1422 for (r = start_row; r <= end_row; ++r) {
1423 for (c = start_col; c <= end_col; c += 4) {
1424 if (c + 3 <= end_col) {
1425 unsigned int sads[4];
1426 const uint8_t *addrs[4];
1427 for (i = 0; i < 4; ++i) {
1428 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1429 addrs[i] = get_buf_from_mv(in_what, &mv);
1432 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1434 for (i = 0; i < 4; ++i) {
1435 if (sads[i] < best_sad) {
1436 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1437 const unsigned int sad = sads[i] +
1438 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1439 if (sad < best_sad) {
1446 for (i = 0; i < end_col - c; ++i) {
1447 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1448 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1449 get_buf_from_mv(in_what, &mv), in_what->stride);
1450 if (sad < best_sad) {
1451 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1452 if (sad < best_sad) {
1465 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1466 const search_site_config *cfg,
1467 MV *ref_mv, MV *best_mv, int search_param,
1468 int sad_per_bit, int *num00,
1469 const vp9_variance_fn_ptr_t *fn_ptr,
1470 const MV *center_mv) {
1473 const MACROBLOCKD *const xd = &x->e_mbd;
1474 uint8_t *what = x->plane[0].src.buf;
1475 const int what_stride = x->plane[0].src.stride;
1476 const uint8_t *in_what;
1477 const int in_what_stride = xd->plane[0].pre[0].stride;
1478 const uint8_t *best_address;
1480 unsigned int bestsad = INT_MAX;
1487 // search_param determines the length of the initial step and hence the number
1489 // 0 = initial step (MAX_FIRST_STEP) pel
1490 // 1 = (MAX_FIRST_STEP/2) pel,
1491 // 2 = (MAX_FIRST_STEP/4) pel...
1492 const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1493 const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1495 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1496 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1497 ref_row = ref_mv->row;
1498 ref_col = ref_mv->col;
1500 best_mv->row = ref_row;
1501 best_mv->col = ref_col;
1503 // Work out the start point for the search
1504 in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1505 best_address = in_what;
1507 // Check the starting position
1508 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1509 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1513 for (step = 0; step < tot_steps; step++) {
1516 // All_in is true if every one of the points we are checking are within
1517 // the bounds of the image.
1518 all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1519 all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1520 all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1521 all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1523 // If all the pixels are within the bounds we don't check whether the
1524 // search point is valid in this loop, otherwise we check each point
1527 unsigned int sad_array[4];
1529 for (j = 0; j < cfg->searches_per_step; j += 4) {
1530 unsigned char const *block_offset[4];
1532 for (t = 0; t < 4; t++)
1533 block_offset[t] = ss[i + t].offset + best_address;
1535 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1538 for (t = 0; t < 4; t++, i++) {
1539 if (sad_array[t] < bestsad) {
1540 const MV this_mv = {best_mv->row + ss[i].mv.row,
1541 best_mv->col + ss[i].mv.col};
1542 sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1544 if (sad_array[t] < bestsad) {
1545 bestsad = sad_array[t];
1552 for (j = 0; j < cfg->searches_per_step; j++) {
1553 // Trap illegal vectors
1554 const MV this_mv = {best_mv->row + ss[i].mv.row,
1555 best_mv->col + ss[i].mv.col};
1557 if (is_mv_in(x, &this_mv)) {
1558 const uint8_t *const check_here = ss[i].offset + best_address;
1559 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1562 if (thissad < bestsad) {
1563 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1564 if (thissad < bestsad) {
1573 if (best_site != last_site) {
1574 best_mv->row += ss[best_site].mv.row;
1575 best_mv->col += ss[best_site].mv.col;
1576 best_address += ss[best_site].offset;
1577 last_site = best_site;
1578 #if defined(NEW_DIAMOND_SEARCH)
1580 const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1581 best_mv->col + ss[best_site].mv.col};
1582 if (is_mv_in(x, &this_mv)) {
1583 const uint8_t *const check_here = ss[best_site].offset + best_address;
1584 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1586 if (thissad < bestsad) {
1587 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1588 if (thissad < bestsad) {
1590 best_mv->row += ss[best_site].mv.row;
1591 best_mv->col += ss[best_site].mv.col;
1592 best_address += ss[best_site].offset;
1600 } else if (best_address == in_what) {
1607 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1608 point as the best match, we will do a final 1-away diamond
1610 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1611 MV *mvp_full, int step_param,
1612 int sadpb, int further_steps, int do_refine,
1614 const vp9_variance_fn_ptr_t *fn_ptr,
1615 const MV *ref_mv, MV *dst_mv) {
1617 int thissme, n, num00 = 0;
1618 int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1619 step_param, sadpb, &n,
1621 if (bestsme < INT_MAX)
1622 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1625 // If there won't be more n-step search, check to see if refining search is
1627 if (n > further_steps)
1630 while (n < further_steps) {
1636 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1637 step_param + n, sadpb, &num00,
1639 if (thissme < INT_MAX)
1640 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1642 // check to see if refining search is needed.
1643 if (num00 > further_steps - n)
1646 if (thissme < bestsme) {
1653 // final 1-away diamond refining search
1655 const int search_range = 8;
1656 MV best_mv = *dst_mv;
1657 thissme = cpi->refining_search_sad(x, &best_mv, sadpb, search_range,
1659 if (thissme < INT_MAX)
1660 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1661 if (thissme < bestsme) {
1667 // Return cost list.
1669 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1674 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1675 int sad_per_bit, int distance,
1676 const vp9_variance_fn_ptr_t *fn_ptr,
1677 const MV *center_mv, MV *best_mv) {
1679 const MACROBLOCKD *const xd = &x->e_mbd;
1680 const struct buf_2d *const what = &x->plane[0].src;
1681 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1682 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1683 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1684 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1685 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1686 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1687 int best_sad = fn_ptr->sdf(what->buf, what->stride,
1688 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1689 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1692 for (r = row_min; r < row_max; ++r) {
1693 for (c = col_min; c < col_max; ++c) {
1694 const MV mv = {r, c};
1695 const int sad = fn_ptr->sdf(what->buf, what->stride,
1696 get_buf_from_mv(in_what, &mv), in_what->stride) +
1697 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1698 if (sad < best_sad) {
1707 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
1708 int sad_per_bit, int distance,
1709 const vp9_variance_fn_ptr_t *fn_ptr,
1710 const MV *center_mv, MV *best_mv) {
1712 const MACROBLOCKD *const xd = &x->e_mbd;
1713 const struct buf_2d *const what = &x->plane[0].src;
1714 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1715 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1716 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1717 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1718 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1719 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1720 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
1721 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1722 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1725 for (r = row_min; r < row_max; ++r) {
1727 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
1729 if (fn_ptr->sdx3f != NULL) {
1730 while ((c + 2) < col_max) {
1732 unsigned int sads[3];
1734 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
1737 for (i = 0; i < 3; ++i) {
1738 unsigned int sad = sads[i];
1739 if (sad < best_sad) {
1740 const MV mv = {r, c};
1741 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1742 if (sad < best_sad) {
1753 while (c < col_max) {
1754 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1755 check_here, in_what->stride);
1756 if (sad < best_sad) {
1757 const MV mv = {r, c};
1758 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1759 if (sad < best_sad) {
1772 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
1773 int sad_per_bit, int distance,
1774 const vp9_variance_fn_ptr_t *fn_ptr,
1775 const MV *center_mv, MV *best_mv) {
1777 const MACROBLOCKD *const xd = &x->e_mbd;
1778 const struct buf_2d *const what = &x->plane[0].src;
1779 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1780 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
1781 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
1782 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
1783 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
1784 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1785 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
1786 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1787 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1790 for (r = row_min; r < row_max; ++r) {
1792 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
1794 if (fn_ptr->sdx8f != NULL) {
1795 while ((c + 7) < col_max) {
1797 unsigned int sads[8];
1799 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
1802 for (i = 0; i < 8; ++i) {
1803 unsigned int sad = sads[i];
1804 if (sad < best_sad) {
1805 const MV mv = {r, c};
1806 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1807 if (sad < best_sad) {
1818 if (fn_ptr->sdx3f != NULL) {
1819 while ((c + 2) < col_max) {
1821 unsigned int sads[3];
1823 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
1826 for (i = 0; i < 3; ++i) {
1827 unsigned int sad = sads[i];
1828 if (sad < best_sad) {
1829 const MV mv = {r, c};
1830 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1831 if (sad < best_sad) {
1842 while (c < col_max) {
1843 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1844 check_here, in_what->stride);
1845 if (sad < best_sad) {
1846 const MV mv = {r, c};
1847 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1848 if (sad < best_sad) {
1861 int vp9_refining_search_sad_c(const MACROBLOCK *x,
1862 MV *ref_mv, int error_per_bit,
1864 const vp9_variance_fn_ptr_t *fn_ptr,
1865 const MV *center_mv) {
1866 const MACROBLOCKD *const xd = &x->e_mbd;
1867 const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
1868 const struct buf_2d *const what = &x->plane[0].src;
1869 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1870 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1871 const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
1872 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
1874 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
1877 for (i = 0; i < search_range; i++) {
1879 const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
1880 ((ref_mv->row + 1) < x->mv_row_max) &
1881 ((ref_mv->col - 1) > x->mv_col_min) &
1882 ((ref_mv->col + 1) < x->mv_col_max);
1885 unsigned int sads[4];
1886 const uint8_t *const positions[4] = {
1887 best_address - in_what->stride,
1890 best_address + in_what->stride
1893 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
1895 for (j = 0; j < 4; ++j) {
1896 if (sads[j] < best_sad) {
1897 const MV mv = {ref_mv->row + neighbors[j].row,
1898 ref_mv->col + neighbors[j].col};
1899 sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1900 if (sads[j] < best_sad) {
1907 for (j = 0; j < 4; ++j) {
1908 const MV mv = {ref_mv->row + neighbors[j].row,
1909 ref_mv->col + neighbors[j].col};
1911 if (is_mv_in(x, &mv)) {
1912 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1913 get_buf_from_mv(in_what, &mv),
1915 if (sad < best_sad) {
1916 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1917 if (sad < best_sad) {
1926 if (best_site == -1) {
1929 ref_mv->row += neighbors[best_site].row;
1930 ref_mv->col += neighbors[best_site].col;
1931 best_address = get_buf_from_mv(in_what, ref_mv);
1938 // This function is called when we do joint motion search in comp_inter_inter
1940 int vp9_refining_search_8p_c(const MACROBLOCK *x,
1941 MV *ref_mv, int error_per_bit,
1943 const vp9_variance_fn_ptr_t *fn_ptr,
1944 const MV *center_mv,
1945 const uint8_t *second_pred) {
1946 const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
1947 {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1948 const MACROBLOCKD *const xd = &x->e_mbd;
1949 const struct buf_2d *const what = &x->plane[0].src;
1950 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1951 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1952 unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
1953 get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
1954 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
1957 for (i = 0; i < search_range; ++i) {
1960 for (j = 0; j < 8; ++j) {
1961 const MV mv = {ref_mv->row + neighbors[j].row,
1962 ref_mv->col + neighbors[j].col};
1964 if (is_mv_in(x, &mv)) {
1965 unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
1966 get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
1967 if (sad < best_sad) {
1968 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
1969 if (sad < best_sad) {
1977 if (best_site == -1) {
1980 ref_mv->row += neighbors[best_site].row;
1981 ref_mv->col += neighbors[best_site].col;
1987 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
1988 BLOCK_SIZE bsize, MV *mvp_full,
1989 int step_param, int error_per_bit,
1991 const MV *ref_mv, MV *tmp_mv,
1992 int var_max, int rd) {
1993 const SPEED_FEATURES *const sf = &cpi->sf;
1994 const SEARCH_METHODS method = sf->mv.search_method;
1995 vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
1998 cost_list[0] = INT_MAX;
1999 cost_list[1] = INT_MAX;
2000 cost_list[2] = INT_MAX;
2001 cost_list[3] = INT_MAX;
2002 cost_list[4] = INT_MAX;
2007 var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2008 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2011 var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2012 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2015 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2016 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2019 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2020 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2023 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2024 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2027 var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2028 MAX_MVSEARCH_STEPS - 1 - step_param,
2029 1, cost_list, fn_ptr, ref_mv, tmp_mv);
2032 assert(!"Invalid search method.");
2035 if (method != NSTEP && rd && var < var_max)
2036 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);