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"
16 #include "./vpx_dsp_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h"
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_reconinter.h"
24 #include "vp9/encoder/vp9_encoder.h"
25 #include "vp9/encoder/vp9_mcomp.h"
27 // #define NEW_DIAMOND_SEARCH
29 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
31 return &buf->buf[mv->row * buf->stride + mv->col];
34 void vp9_set_mv_search_range(MACROBLOCK *x, const MV *mv) {
35 int col_min = (mv->col >> 3) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
36 int row_min = (mv->row >> 3) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
37 int col_max = (mv->col >> 3) + MAX_FULL_PEL_VAL;
38 int row_max = (mv->row >> 3) + MAX_FULL_PEL_VAL;
40 col_min = MAX(col_min, (MV_LOW >> 3) + 1);
41 row_min = MAX(row_min, (MV_LOW >> 3) + 1);
42 col_max = MIN(col_max, (MV_UPP >> 3) - 1);
43 row_max = MIN(row_max, (MV_UPP >> 3) - 1);
45 // Get intersection of UMV window and valid MV window to reduce # of checks
47 if (x->mv_col_min < col_min)
48 x->mv_col_min = col_min;
49 if (x->mv_col_max > col_max)
50 x->mv_col_max = col_max;
51 if (x->mv_row_min < row_min)
52 x->mv_row_min = row_min;
53 if (x->mv_row_max > row_max)
54 x->mv_row_max = row_max;
57 int vp9_init_search_range(int size) {
59 // Minimum search size no matter what the passed in value.
62 while ((size << sr) < MAX_FULL_PEL_VAL)
65 sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
69 static INLINE int mv_cost(const MV *mv,
70 const int *joint_cost, int *const comp_cost[2]) {
71 return joint_cost[vp9_get_mv_joint(mv)] +
72 comp_cost[0][mv->row] + comp_cost[1][mv->col];
75 int vp9_mv_bit_cost(const MV *mv, const MV *ref,
76 const int *mvjcost, int *mvcost[2], int weight) {
77 const MV diff = { mv->row - ref->row,
79 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
82 static int mv_err_cost(const MV *mv, const MV *ref,
83 const int *mvjcost, int *mvcost[2],
86 const MV diff = { mv->row - ref->row,
88 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
94 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
96 const MV diff = { mv->row - ref->row,
98 return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
99 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 static INLINE unsigned int setup_center_error(const MACROBLOCKD *xd,
293 const vp9_variance_fn_ptr_t *vfp,
294 const uint8_t *const src,
295 const int src_stride,
296 const uint8_t *const y,
298 const uint8_t *second_pred,
299 int w, int h, int offset,
300 int *mvjcost, int *mvcost[2],
303 unsigned int besterr;
304 #if CONFIG_VP9_HIGHBITDEPTH
305 if (second_pred != NULL) {
306 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
307 DECLARE_ALIGNED(16, uint16_t, comp_pred16[64 * 64]);
308 vpx_highbd_comp_avg_pred(comp_pred16, second_pred, w, h, y + offset,
310 besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, src, src_stride,
313 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
314 vpx_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
315 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
318 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
320 *distortion = besterr;
321 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
324 if (second_pred != NULL) {
325 DECLARE_ALIGNED(16, uint8_t, comp_pred[64 * 64]);
326 vpx_comp_avg_pred(comp_pred, second_pred, w, h, y + offset, y_stride);
327 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
329 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
331 *distortion = besterr;
332 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
333 #endif // CONFIG_VP9_HIGHBITDEPTH
337 static INLINE int divide_and_round(const int n, const int d) {
338 return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
341 static INLINE int is_cost_list_wellbehaved(int *cost_list) {
342 return cost_list[0] < cost_list[1] &&
343 cost_list[0] < cost_list[2] &&
344 cost_list[0] < cost_list[3] &&
345 cost_list[0] < cost_list[4];
348 // Returns surface minima estimate at given precision in 1/2^n bits.
349 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
350 // For a given set of costs S0, S1, S2, S3, S4 at points
351 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
352 // the solution for the location of the minima (x0, y0) is given by:
353 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
354 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
355 // The code below is an integerized version of that.
356 static void get_cost_surf_min(int *cost_list, int *ir, int *ic,
358 *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
359 (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
360 *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
361 (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
364 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
366 MV *bestmv, const MV *ref_mv,
369 const vp9_variance_fn_ptr_t *vfp,
373 int *mvjcost, int *mvcost[2],
376 const uint8_t *second_pred,
379 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
380 z, src_stride, y, y_stride, second_pred,
381 w, h, offset, mvjcost, mvcost,
392 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
393 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
394 cost_list[4] != INT_MAX &&
395 is_cost_list_wellbehaved(cost_list)) {
398 get_cost_surf_min(cost_list, &ir, &ic, 2);
399 if (ir != 0 || ic != 0) {
400 CHECK_BETTER(minpt, tr + 2 * ir, tc + 2 * ic);
411 // Each subsequent iteration checks at least one point in common with
412 // the last iteration could be 2 ( if diag selected) 1/4 pel
413 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
414 if (forced_stop != 2) {
417 if (quarteriters > 1) {
426 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
429 if (eighthiters > 1) {
437 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
438 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
444 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
445 MV *bestmv, const MV *ref_mv,
448 const vp9_variance_fn_ptr_t *vfp,
452 int *mvjcost, int *mvcost[2],
455 const uint8_t *second_pred,
458 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
459 z, src_stride, y, y_stride, second_pred,
460 w, h, offset, mvjcost, mvcost,
463 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
464 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
465 cost_list[4] != INT_MAX &&
466 is_cost_list_wellbehaved(cost_list)) {
469 get_cost_surf_min(cost_list, &ir, &ic, 1);
470 if (ir != 0 || ic != 0) {
471 CHECK_BETTER(minpt, tr + ir * hstep, tc + ic * hstep);
480 // Each subsequent iteration checks at least one point in common with
481 // the last iteration could be 2 ( if diag selected) 1/4 pel
483 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
484 if (forced_stop != 2) {
489 if (quarteriters > 1) {
494 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
499 if (eighthiters > 1) {
503 // These lines insure static analysis doesn't warn that
504 // tr and tc aren't used after the above point.
511 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
512 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
518 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
519 MV *bestmv, const MV *ref_mv,
522 const vp9_variance_fn_ptr_t *vfp,
526 int *mvjcost, int *mvcost[2],
529 const uint8_t *second_pred,
532 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
533 z, src_stride, y, y_stride, second_pred,
534 w, h, offset, mvjcost, mvcost,
537 cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
538 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
539 cost_list[4] != INT_MAX) {
540 unsigned int left, right, up, down, diag;
541 whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
542 (cost_list[2] < cost_list[4] ? 0 : 2);
545 CHECK_BETTER(left, tr, tc - hstep);
546 CHECK_BETTER(down, tr + hstep, tc);
547 CHECK_BETTER(diag, tr + hstep, tc - hstep);
550 CHECK_BETTER(right, tr, tc + hstep);
551 CHECK_BETTER(down, tr + hstep, tc);
552 CHECK_BETTER(diag, tr + hstep, tc + hstep);
555 CHECK_BETTER(left, tr, tc - hstep);
556 CHECK_BETTER(up, tr - hstep, tc);
557 CHECK_BETTER(diag, tr - hstep, tc - hstep);
560 CHECK_BETTER(right, tr, tc + hstep);
561 CHECK_BETTER(up, tr - hstep, tc);
562 CHECK_BETTER(diag, tr - hstep, tc + hstep);
575 // Each subsequent iteration checks at least one point in common with
576 // the last iteration could be 2 ( if diag selected) 1/4 pel
578 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
579 if (forced_stop != 2) {
582 if (quarteriters > 1) {
589 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
592 if (eighthiters > 1) {
598 // These lines insure static analysis doesn't warn that
599 // tr and tc aren't used after the above point.
606 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
607 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
613 const MV search_step_table[12] = {
614 // left, right, up, down
615 {0, -4}, {0, 4}, {-4, 0}, {4, 0},
616 {0, -2}, {0, 2}, {-2, 0}, {2, 0},
617 {0, -1}, {0, 1}, {-1, 0}, {1, 0}
620 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
621 MV *bestmv, const MV *ref_mv,
624 const vp9_variance_fn_ptr_t *vfp,
628 int *mvjcost, int *mvcost[2],
631 const uint8_t *second_pred,
633 const uint8_t *const z = x->plane[0].src.buf;
634 const uint8_t *const src_address = z;
635 const int src_stride = x->plane[0].src.stride;
636 const MACROBLOCKD *xd = &x->e_mbd;
637 unsigned int besterr = INT_MAX;
639 unsigned int whichdir = 0;
641 const int y_stride = xd->plane[0].pre[0].stride;
642 const int offset = bestmv->row * y_stride + bestmv->col;
643 const uint8_t *const y = xd->plane[0].pre[0].buf;
645 int rr = ref_mv->row;
646 int rc = ref_mv->col;
647 int br = bestmv->row * 8;
648 int bc = bestmv->col * 8;
650 int iter, round = 3 - forced_stop;
651 const int minc = MAX(x->mv_col_min * 8, ref_mv->col - MV_MAX);
652 const int maxc = MIN(x->mv_col_max * 8, ref_mv->col + MV_MAX);
653 const int minr = MAX(x->mv_row_min * 8, ref_mv->row - MV_MAX);
654 const int maxr = MIN(x->mv_row_max * 8, ref_mv->row + MV_MAX);
657 const MV *search_step = search_step_table;
658 int idx, best_idx = -1;
659 unsigned int cost_array[5];
661 if (!(allow_hp && vp9_use_mv_hp(ref_mv)))
668 besterr = setup_center_error(xd, bestmv, ref_mv, error_per_bit, vfp,
669 z, src_stride, y, y_stride, second_pred,
670 w, h, offset, mvjcost, mvcost,
673 (void) cost_list; // to silence compiler warning
675 for (iter = 0; iter < round; ++iter) {
676 // Check vertical and horizontal sub-pixel positions.
677 for (idx = 0; idx < 4; ++idx) {
678 tr = br + search_step[idx].row;
679 tc = bc + search_step[idx].col;
680 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
681 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
682 int row_offset = (tr & 0x07) << 1;
683 int col_offset = (tc & 0x07) << 1;
687 if (second_pred == NULL)
688 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
689 src_address, src_stride, &sse);
691 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
692 src_address, src_stride, &sse, second_pred);
693 cost_array[idx] = thismse +
694 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
696 if (cost_array[idx] < besterr) {
698 besterr = cost_array[idx];
699 *distortion = thismse;
703 cost_array[idx] = INT_MAX;
707 // Check diagonal sub-pixel position
708 tc = bc + (cost_array[0] < cost_array[1] ? -hstep : hstep);
709 tr = br + (cost_array[2] < cost_array[3] ? -hstep : hstep);
710 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
711 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
712 int row_offset = (tr & 0x07) << 1;
713 int col_offset = (tc & 0x07) << 1;
714 MV this_mv = {tr, tc};
715 if (second_pred == NULL)
716 thismse = vfp->svf(pre_address, y_stride, col_offset, row_offset,
717 src_address, src_stride, &sse);
719 thismse = vfp->svaf(pre_address, y_stride, col_offset, row_offset,
720 src_address, src_stride, &sse, second_pred);
721 cost_array[4] = thismse +
722 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
724 if (cost_array[4] < besterr) {
726 besterr = cost_array[4];
727 *distortion = thismse;
731 cost_array[idx] = INT_MAX;
734 if (best_idx < 4 && best_idx >= 0) {
735 br += search_step[best_idx].row;
736 bc += search_step[best_idx].col;
737 } else if (best_idx == 4) {
742 if (iters_per_step > 1)
753 // Each subsequent iteration checks at least one point in common with
754 // the last iteration could be 2 ( if diag selected) 1/4 pel
756 // These lines insure static analysis doesn't warn that
757 // tr and tc aren't used after the above point.
764 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
765 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
775 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
777 return ((row - range) >= x->mv_row_min) &
778 ((row + range) <= x->mv_row_max) &
779 ((col - range) >= x->mv_col_min) &
780 ((col + range) <= x->mv_col_max);
783 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
784 return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
785 (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
788 #define CHECK_BETTER \
790 if (thissad < bestsad) {\
792 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
793 if (thissad < bestsad) {\
800 #define MAX_PATTERN_SCALES 11
801 #define MAX_PATTERN_CANDIDATES 8 // max number of canddiates per scale
802 #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
804 // Calculate and return a sad+mvcost list around an integer best pel.
805 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
808 const vp9_variance_fn_ptr_t *fn_ptr,
811 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
812 const struct buf_2d *const what = &x->plane[0].src;
813 const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
814 const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
815 int br = best_mv->row;
816 int bc = best_mv->col;
823 cost_list[0] = fn_ptr->vf(what->buf, what->stride,
824 get_buf_from_mv(in_what, &this_mv),
825 in_what->stride, &sse) +
826 mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
827 if (check_bounds(x, br, bc, 1)) {
828 for (i = 0; i < 4; i++) {
829 const MV this_mv = {br + neighbors[i].row,
830 bc + neighbors[i].col};
831 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
832 get_buf_from_mv(in_what, &this_mv),
833 in_what->stride, &sse) +
834 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
835 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
839 for (i = 0; i < 4; i++) {
840 const MV this_mv = {br + neighbors[i].row,
841 bc + neighbors[i].col};
842 if (!is_mv_in(x, &this_mv))
843 cost_list[i + 1] = INT_MAX;
845 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
846 get_buf_from_mv(in_what, &this_mv),
847 in_what->stride, &sse) +
848 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
849 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
855 // Generic pattern search function that searches over multiple scales.
856 // Each scale can have a different number of candidates and shape of
857 // candidates as indicated in the num_candidates and candidates arrays
858 // passed into this function
860 static int vp9_pattern_search(const MACROBLOCK *x,
866 const vp9_variance_fn_ptr_t *vfp,
870 const int num_candidates[MAX_PATTERN_SCALES],
871 const MV candidates[MAX_PATTERN_SCALES]
872 [MAX_PATTERN_CANDIDATES]) {
873 const MACROBLOCKD *const xd = &x->e_mbd;
874 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
875 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
878 const struct buf_2d *const what = &x->plane[0].src;
879 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
881 int bestsad = INT_MAX;
884 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
885 int best_init_s = search_param_to_steps[search_param];
886 // adjust ref_mv to make sure it is within MV range
887 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
891 // Work out the start point for the search
892 bestsad = vfp->sdf(what->buf, what->stride,
893 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
894 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
896 // Search all possible scales upto the search param around the center point
897 // pick the scale of the point that is best as the starting scale of
898 // further steps around it.
899 if (do_init_search) {
902 for (t = 0; t <= s; ++t) {
904 if (check_bounds(x, br, bc, 1 << t)) {
905 for (i = 0; i < num_candidates[t]; i++) {
906 const MV this_mv = {br + candidates[t][i].row,
907 bc + candidates[t][i].col};
908 thissad = vfp->sdf(what->buf, what->stride,
909 get_buf_from_mv(in_what, &this_mv),
914 for (i = 0; i < num_candidates[t]; i++) {
915 const MV this_mv = {br + candidates[t][i].row,
916 bc + candidates[t][i].col};
917 if (!is_mv_in(x, &this_mv))
919 thissad = vfp->sdf(what->buf, what->stride,
920 get_buf_from_mv(in_what, &this_mv),
925 if (best_site == -1) {
932 if (best_init_s != -1) {
933 br += candidates[best_init_s][k].row;
934 bc += candidates[best_init_s][k].col;
938 // If the center point is still the best, just skip this and move to
939 // the refinement step.
940 if (best_init_s != -1) {
945 // No need to search all 6 points the 1st time if initial search was used
946 if (!do_init_search || s != best_init_s) {
947 if (check_bounds(x, br, bc, 1 << s)) {
948 for (i = 0; i < num_candidates[s]; i++) {
949 const MV this_mv = {br + candidates[s][i].row,
950 bc + candidates[s][i].col};
951 thissad = vfp->sdf(what->buf, what->stride,
952 get_buf_from_mv(in_what, &this_mv),
957 for (i = 0; i < num_candidates[s]; i++) {
958 const MV this_mv = {br + candidates[s][i].row,
959 bc + candidates[s][i].col};
960 if (!is_mv_in(x, &this_mv))
962 thissad = vfp->sdf(what->buf, what->stride,
963 get_buf_from_mv(in_what, &this_mv),
969 if (best_site == -1) {
972 br += candidates[s][best_site].row;
973 bc += candidates[s][best_site].col;
979 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
981 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
982 next_chkpts_indices[1] = k;
983 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
985 if (check_bounds(x, br, bc, 1 << s)) {
986 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
987 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
988 bc + candidates[s][next_chkpts_indices[i]].col};
989 thissad = vfp->sdf(what->buf, what->stride,
990 get_buf_from_mv(in_what, &this_mv),
995 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
996 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
997 bc + candidates[s][next_chkpts_indices[i]].col};
998 if (!is_mv_in(x, &this_mv))
1000 thissad = vfp->sdf(what->buf, what->stride,
1001 get_buf_from_mv(in_what, &this_mv),
1007 if (best_site != -1) {
1008 k = next_chkpts_indices[best_site];
1009 br += candidates[s][k].row;
1010 bc += candidates[s][k].col;
1012 } while (best_site != -1);
1016 // Returns the one-away integer pel sad values around the best as follows:
1017 // cost_list[0]: cost at the best integer pel
1018 // cost_list[1]: cost at delta {0, -1} (left) from the best integer pel
1019 // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
1020 // cost_list[3]: cost at delta { 0, 1} (right) from the best integer pel
1021 // cost_list[4]: cost at delta {-1, 0} (top) from the best integer pel
1023 const MV best_mv = { br, bc };
1024 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1031 // A specialized function where the smallest scale search candidates
1032 // are 4 1-away neighbors, and cost_list is non-null
1033 // TODO(debargha): Merge this function with the one above. Also remove
1034 // use_mvcost option since it is always 1, to save unnecessary branches.
1035 static int vp9_pattern_search_sad(const MACROBLOCK *x,
1041 const vp9_variance_fn_ptr_t *vfp,
1043 const MV *center_mv,
1045 const int num_candidates[MAX_PATTERN_SCALES],
1046 const MV candidates[MAX_PATTERN_SCALES]
1047 [MAX_PATTERN_CANDIDATES]) {
1048 const MACROBLOCKD *const xd = &x->e_mbd;
1049 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
1050 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1053 const struct buf_2d *const what = &x->plane[0].src;
1054 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1056 int bestsad = INT_MAX;
1059 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1060 int best_init_s = search_param_to_steps[search_param];
1061 // adjust ref_mv to make sure it is within MV range
1062 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1065 if (cost_list != NULL) {
1066 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1070 // Work out the start point for the search
1071 bestsad = vfp->sdf(what->buf, what->stride,
1072 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1073 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1075 // Search all possible scales upto the search param around the center point
1076 // pick the scale of the point that is best as the starting scale of
1077 // further steps around it.
1078 if (do_init_search) {
1081 for (t = 0; t <= s; ++t) {
1083 if (check_bounds(x, br, bc, 1 << t)) {
1084 for (i = 0; i < num_candidates[t]; i++) {
1085 const MV this_mv = {br + candidates[t][i].row,
1086 bc + candidates[t][i].col};
1087 thissad = vfp->sdf(what->buf, what->stride,
1088 get_buf_from_mv(in_what, &this_mv),
1093 for (i = 0; i < num_candidates[t]; i++) {
1094 const MV this_mv = {br + candidates[t][i].row,
1095 bc + candidates[t][i].col};
1096 if (!is_mv_in(x, &this_mv))
1098 thissad = vfp->sdf(what->buf, what->stride,
1099 get_buf_from_mv(in_what, &this_mv),
1104 if (best_site == -1) {
1111 if (best_init_s != -1) {
1112 br += candidates[best_init_s][k].row;
1113 bc += candidates[best_init_s][k].col;
1117 // If the center point is still the best, just skip this and move to
1118 // the refinement step.
1119 if (best_init_s != -1) {
1120 int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1124 for (; s >= do_sad; s--) {
1125 if (!do_init_search || s != best_init_s) {
1126 if (check_bounds(x, br, bc, 1 << s)) {
1127 for (i = 0; i < num_candidates[s]; i++) {
1128 const MV this_mv = {br + candidates[s][i].row,
1129 bc + candidates[s][i].col};
1130 thissad = vfp->sdf(what->buf, what->stride,
1131 get_buf_from_mv(in_what, &this_mv),
1136 for (i = 0; i < num_candidates[s]; i++) {
1137 const MV this_mv = {br + candidates[s][i].row,
1138 bc + candidates[s][i].col};
1139 if (!is_mv_in(x, &this_mv))
1141 thissad = vfp->sdf(what->buf, what->stride,
1142 get_buf_from_mv(in_what, &this_mv),
1148 if (best_site == -1) {
1151 br += candidates[s][best_site].row;
1152 bc += candidates[s][best_site].col;
1158 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1160 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1161 next_chkpts_indices[1] = k;
1162 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1164 if (check_bounds(x, br, bc, 1 << s)) {
1165 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1166 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1167 bc + candidates[s][next_chkpts_indices[i]].col};
1168 thissad = vfp->sdf(what->buf, what->stride,
1169 get_buf_from_mv(in_what, &this_mv),
1174 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1175 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1176 bc + candidates[s][next_chkpts_indices[i]].col};
1177 if (!is_mv_in(x, &this_mv))
1179 thissad = vfp->sdf(what->buf, what->stride,
1180 get_buf_from_mv(in_what, &this_mv),
1186 if (best_site != -1) {
1187 k = next_chkpts_indices[best_site];
1188 br += candidates[s][k].row;
1189 bc += candidates[s][k].col;
1191 } while (best_site != -1);
1194 // Note: If we enter the if below, then cost_list must be non-NULL.
1196 cost_list[0] = bestsad;
1197 if (!do_init_search || s != best_init_s) {
1198 if (check_bounds(x, br, bc, 1 << s)) {
1199 for (i = 0; i < num_candidates[s]; i++) {
1200 const MV this_mv = {br + candidates[s][i].row,
1201 bc + candidates[s][i].col};
1203 thissad = vfp->sdf(what->buf, what->stride,
1204 get_buf_from_mv(in_what, &this_mv),
1209 for (i = 0; i < num_candidates[s]; i++) {
1210 const MV this_mv = {br + candidates[s][i].row,
1211 bc + candidates[s][i].col};
1212 if (!is_mv_in(x, &this_mv))
1215 thissad = vfp->sdf(what->buf, what->stride,
1216 get_buf_from_mv(in_what, &this_mv),
1222 if (best_site != -1) {
1223 br += candidates[s][best_site].row;
1224 bc += candidates[s][best_site].col;
1228 while (best_site != -1) {
1229 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1231 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1232 next_chkpts_indices[1] = k;
1233 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1234 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1235 cost_list[((k + 2) % 4) + 1] = cost_list[0];
1236 cost_list[0] = bestsad;
1238 if (check_bounds(x, br, bc, 1 << s)) {
1239 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1240 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1241 bc + candidates[s][next_chkpts_indices[i]].col};
1242 cost_list[next_chkpts_indices[i] + 1] =
1243 thissad = vfp->sdf(what->buf, what->stride,
1244 get_buf_from_mv(in_what, &this_mv),
1249 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1250 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1251 bc + candidates[s][next_chkpts_indices[i]].col};
1252 if (!is_mv_in(x, &this_mv)) {
1253 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1256 cost_list[next_chkpts_indices[i] + 1] =
1257 thissad = vfp->sdf(what->buf, what->stride,
1258 get_buf_from_mv(in_what, &this_mv),
1264 if (best_site != -1) {
1265 k = next_chkpts_indices[best_site];
1266 br += candidates[s][k].row;
1267 bc += candidates[s][k].col;
1273 // Returns the one-away integer pel sad values around the best as follows:
1274 // cost_list[0]: sad at the best integer pel
1275 // cost_list[1]: sad at delta {0, -1} (left) from the best integer pel
1276 // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1277 // cost_list[3]: sad at delta { 0, 1} (right) from the best integer pel
1278 // cost_list[4]: sad at delta {-1, 0} (top) from the best integer pel
1280 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1281 if (cost_list[0] == INT_MAX) {
1282 cost_list[0] = bestsad;
1283 if (check_bounds(x, br, bc, 1)) {
1284 for (i = 0; i < 4; i++) {
1285 const MV this_mv = { br + neighbors[i].row,
1286 bc + neighbors[i].col };
1287 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1288 get_buf_from_mv(in_what, &this_mv),
1292 for (i = 0; i < 4; i++) {
1293 const MV this_mv = {br + neighbors[i].row,
1294 bc + neighbors[i].col};
1295 if (!is_mv_in(x, &this_mv))
1296 cost_list[i + 1] = INT_MAX;
1298 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1299 get_buf_from_mv(in_what, &this_mv),
1305 for (i = 0; i < 4; i++) {
1306 const MV this_mv = {br + neighbors[i].row,
1307 bc + neighbors[i].col};
1308 if (cost_list[i + 1] != INT_MAX) {
1310 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1321 int vp9_get_mvpred_var(const MACROBLOCK *x,
1322 const MV *best_mv, const MV *center_mv,
1323 const vp9_variance_fn_ptr_t *vfp,
1325 const MACROBLOCKD *const xd = &x->e_mbd;
1326 const struct buf_2d *const what = &x->plane[0].src;
1327 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1328 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1329 unsigned int unused;
1331 return vfp->vf(what->buf, what->stride,
1332 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1333 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1334 x->mvcost, x->errorperbit) : 0);
1337 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1338 const MV *best_mv, const MV *center_mv,
1339 const uint8_t *second_pred,
1340 const vp9_variance_fn_ptr_t *vfp,
1342 const MACROBLOCKD *const xd = &x->e_mbd;
1343 const struct buf_2d *const what = &x->plane[0].src;
1344 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1345 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1346 unsigned int unused;
1348 return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1349 what->buf, what->stride, &unused, second_pred) +
1350 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1351 x->mvcost, x->errorperbit) : 0);
1354 int vp9_hex_search(const MACROBLOCK *x,
1360 const vp9_variance_fn_ptr_t *vfp,
1362 const MV *center_mv, MV *best_mv) {
1363 // First scale has 8-closest points, the rest have 6 points in hex shape
1364 // at increasing scales
1365 static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1366 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1368 // Note that the largest candidate step at each scale is 2^scale
1369 static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1370 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1371 {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1372 {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1373 {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1374 {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1375 {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1376 {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1377 {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1378 {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1379 {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1380 {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1383 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1384 do_init_search, cost_list, vfp, use_mvcost,
1386 hex_num_candidates, hex_candidates);
1389 int vp9_bigdia_search(const MACROBLOCK *x,
1395 const vp9_variance_fn_ptr_t *vfp,
1397 const MV *center_mv,
1399 // First scale has 4-closest points, the rest have 8 points in diamond
1400 // shape at increasing scales
1401 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1402 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1404 // Note that the largest candidate step at each scale is 2^scale
1405 static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1406 [MAX_PATTERN_CANDIDATES] = {
1407 {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1408 {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1409 {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1410 {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1411 {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1412 {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1413 {-16, 16}, {-32, 0}},
1414 {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1415 {-32, 32}, {-64, 0}},
1416 {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1417 {-64, 64}, {-128, 0}},
1418 {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1419 {-128, 128}, {-256, 0}},
1420 {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1421 {-256, 256}, {-512, 0}},
1422 {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1423 {-512, 512}, {-1024, 0}},
1425 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1426 do_init_search, cost_list, vfp, use_mvcost,
1428 bigdia_num_candidates, bigdia_candidates);
1431 int vp9_square_search(const MACROBLOCK *x,
1437 const vp9_variance_fn_ptr_t *vfp,
1439 const MV *center_mv,
1441 // All scales have 8 closest points in square shape
1442 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1443 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1445 // Note that the largest candidate step at each scale is 2^scale
1446 static const MV square_candidates[MAX_PATTERN_SCALES]
1447 [MAX_PATTERN_CANDIDATES] = {
1448 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1449 {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1450 {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1451 {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1452 {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1453 {-16, 16}, {-16, 0}},
1454 {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1455 {-32, 32}, {-32, 0}},
1456 {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1457 {-64, 64}, {-64, 0}},
1458 {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1459 {-128, 128}, {-128, 0}},
1460 {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1461 {-256, 256}, {-256, 0}},
1462 {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1463 {-512, 512}, {-512, 0}},
1464 {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1465 {0, 1024}, {-1024, 1024}, {-1024, 0}},
1467 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1468 do_init_search, cost_list, vfp, use_mvcost,
1470 square_num_candidates, square_candidates);
1473 int vp9_fast_hex_search(const MACROBLOCK *x,
1477 int do_init_search, // must be zero for fast_hex
1479 const vp9_variance_fn_ptr_t *vfp,
1481 const MV *center_mv,
1483 return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1484 sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1485 center_mv, best_mv);
1488 int vp9_fast_dia_search(const MACROBLOCK *x,
1494 const vp9_variance_fn_ptr_t *vfp,
1496 const MV *center_mv,
1498 return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1499 sad_per_bit, do_init_search, cost_list, vfp,
1500 use_mvcost, center_mv, best_mv);
1505 int vp9_full_range_search_c(const MACROBLOCK *x,
1506 const search_site_config *cfg,
1507 MV *ref_mv, MV *best_mv,
1508 int search_param, int sad_per_bit, int *num00,
1509 const vp9_variance_fn_ptr_t *fn_ptr,
1510 const MV *center_mv) {
1511 const MACROBLOCKD *const xd = &x->e_mbd;
1512 const struct buf_2d *const what = &x->plane[0].src;
1513 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1514 const int range = 64;
1515 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1516 unsigned int best_sad = INT_MAX;
1518 int start_col, end_col, start_row, end_row;
1520 // The cfg and search_param parameters are not used in this search variant
1524 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1527 best_sad = fn_ptr->sdf(what->buf, what->stride,
1528 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1529 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1530 start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1531 start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1532 end_row = MIN(range, x->mv_row_max - ref_mv->row);
1533 end_col = MIN(range, x->mv_col_max - ref_mv->col);
1535 for (r = start_row; r <= end_row; ++r) {
1536 for (c = start_col; c <= end_col; c += 4) {
1537 if (c + 3 <= end_col) {
1538 unsigned int sads[4];
1539 const uint8_t *addrs[4];
1540 for (i = 0; i < 4; ++i) {
1541 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1542 addrs[i] = get_buf_from_mv(in_what, &mv);
1545 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1547 for (i = 0; i < 4; ++i) {
1548 if (sads[i] < best_sad) {
1549 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1550 const unsigned int sad = sads[i] +
1551 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1552 if (sad < best_sad) {
1559 for (i = 0; i < end_col - c; ++i) {
1560 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1561 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1562 get_buf_from_mv(in_what, &mv), in_what->stride);
1563 if (sad < best_sad) {
1564 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1565 if (sad < best_sad) {
1578 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1579 const search_site_config *cfg,
1580 MV *ref_mv, MV *best_mv, int search_param,
1581 int sad_per_bit, int *num00,
1582 const vp9_variance_fn_ptr_t *fn_ptr,
1583 const MV *center_mv) {
1586 const MACROBLOCKD *const xd = &x->e_mbd;
1587 uint8_t *what = x->plane[0].src.buf;
1588 const int what_stride = x->plane[0].src.stride;
1589 const uint8_t *in_what;
1590 const int in_what_stride = xd->plane[0].pre[0].stride;
1591 const uint8_t *best_address;
1593 unsigned int bestsad = INT_MAX;
1600 // search_param determines the length of the initial step and hence the number
1602 // 0 = initial step (MAX_FIRST_STEP) pel
1603 // 1 = (MAX_FIRST_STEP/2) pel,
1604 // 2 = (MAX_FIRST_STEP/4) pel...
1605 const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1606 const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1608 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1609 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1610 ref_row = ref_mv->row;
1611 ref_col = ref_mv->col;
1613 best_mv->row = ref_row;
1614 best_mv->col = ref_col;
1616 // Work out the start point for the search
1617 in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1618 best_address = in_what;
1620 // Check the starting position
1621 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1622 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1626 for (step = 0; step < tot_steps; step++) {
1629 // All_in is true if every one of the points we are checking are within
1630 // the bounds of the image.
1631 all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1632 all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1633 all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1634 all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1636 // If all the pixels are within the bounds we don't check whether the
1637 // search point is valid in this loop, otherwise we check each point
1640 unsigned int sad_array[4];
1642 for (j = 0; j < cfg->searches_per_step; j += 4) {
1643 unsigned char const *block_offset[4];
1645 for (t = 0; t < 4; t++)
1646 block_offset[t] = ss[i + t].offset + best_address;
1648 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1651 for (t = 0; t < 4; t++, i++) {
1652 if (sad_array[t] < bestsad) {
1653 const MV this_mv = {best_mv->row + ss[i].mv.row,
1654 best_mv->col + ss[i].mv.col};
1655 sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1657 if (sad_array[t] < bestsad) {
1658 bestsad = sad_array[t];
1665 for (j = 0; j < cfg->searches_per_step; j++) {
1666 // Trap illegal vectors
1667 const MV this_mv = {best_mv->row + ss[i].mv.row,
1668 best_mv->col + ss[i].mv.col};
1670 if (is_mv_in(x, &this_mv)) {
1671 const uint8_t *const check_here = ss[i].offset + best_address;
1672 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1675 if (thissad < bestsad) {
1676 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1677 if (thissad < bestsad) {
1686 if (best_site != last_site) {
1687 best_mv->row += ss[best_site].mv.row;
1688 best_mv->col += ss[best_site].mv.col;
1689 best_address += ss[best_site].offset;
1690 last_site = best_site;
1691 #if defined(NEW_DIAMOND_SEARCH)
1693 const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1694 best_mv->col + ss[best_site].mv.col};
1695 if (is_mv_in(x, &this_mv)) {
1696 const uint8_t *const check_here = ss[best_site].offset + best_address;
1697 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1699 if (thissad < bestsad) {
1700 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1701 if (thissad < bestsad) {
1703 best_mv->row += ss[best_site].mv.row;
1704 best_mv->col += ss[best_site].mv.col;
1705 best_address += ss[best_site].offset;
1713 } else if (best_address == in_what) {
1720 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1721 int best_sad = INT_MAX;
1724 int center, offset = 0;
1725 int bw = 4 << bwl; // redundant variable, to be changed in the experiments.
1726 for (d = 0; d <= bw; d += 16) {
1727 this_sad = vp9_vector_var(&ref[d], src, bwl);
1728 if (this_sad < best_sad) {
1729 best_sad = this_sad;
1735 for (d = -8; d <= 8; d += 16) {
1736 int this_pos = offset + d;
1738 if (this_pos < 0 || this_pos > bw)
1740 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1741 if (this_sad < best_sad) {
1742 best_sad = this_sad;
1748 for (d = -4; d <= 4; d += 8) {
1749 int this_pos = offset + d;
1751 if (this_pos < 0 || this_pos > bw)
1753 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1754 if (this_sad < best_sad) {
1755 best_sad = this_sad;
1761 for (d = -2; d <= 2; d += 4) {
1762 int this_pos = offset + d;
1764 if (this_pos < 0 || this_pos > bw)
1766 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1767 if (this_sad < best_sad) {
1768 best_sad = this_sad;
1774 for (d = -1; d <= 1; d += 2) {
1775 int this_pos = offset + d;
1777 if (this_pos < 0 || this_pos > bw)
1779 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1780 if (this_sad < best_sad) {
1781 best_sad = this_sad;
1786 return (center - (bw >> 1));
1789 static const MV search_pos[4] = {
1790 {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1793 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1795 int mi_row, int mi_col) {
1796 MACROBLOCKD *xd = &x->e_mbd;
1797 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1798 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
1799 DECLARE_ALIGNED(16, int16_t, hbuf[128]);
1800 DECLARE_ALIGNED(16, int16_t, vbuf[128]);
1801 DECLARE_ALIGNED(16, int16_t, src_hbuf[64]);
1802 DECLARE_ALIGNED(16, int16_t, src_vbuf[64]);
1804 const int bw = 4 << b_width_log2_lookup[bsize];
1805 const int bh = 4 << b_height_log2_lookup[bsize];
1806 const int search_width = bw << 1;
1807 const int search_height = bh << 1;
1808 const int src_stride = x->plane[0].src.stride;
1809 const int ref_stride = xd->plane[0].pre[0].stride;
1810 uint8_t const *ref_buf, *src_buf;
1811 MV *tmp_mv = &xd->mi[0]->mbmi.mv[0].as_mv;
1812 unsigned int best_sad, tmp_sad, this_sad[4];
1814 const int norm_factor = 3 + (bw >> 5);
1815 const YV12_BUFFER_CONFIG *scaled_ref_frame =
1816 vp9_get_scaled_ref_frame(cpi, mbmi->ref_frame[0]);
1818 if (scaled_ref_frame) {
1820 // Swap out the reference frame for a version that's been scaled to
1821 // match the resolution of the current frame, allowing the existing
1822 // motion search code to be used without additional modifications.
1823 for (i = 0; i < MAX_MB_PLANE; i++)
1824 backup_yv12[i] = xd->plane[i].pre[0];
1825 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
1828 #if CONFIG_VP9_HIGHBITDEPTH
1830 unsigned int this_sad;
1833 this_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1834 xd->plane[0].pre[0].buf, ref_stride);
1836 if (scaled_ref_frame) {
1838 for (i = 0; i < MAX_MB_PLANE; i++)
1839 xd->plane[i].pre[0] = backup_yv12[i];
1845 // Set up prediction 1-D reference set
1846 ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1847 for (idx = 0; idx < search_width; idx += 16) {
1848 vp9_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1852 ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1853 for (idx = 0; idx < search_height; ++idx) {
1854 vbuf[idx] = vp9_int_pro_col(ref_buf, bw) >> norm_factor;
1855 ref_buf += ref_stride;
1858 // Set up src 1-D reference set
1859 for (idx = 0; idx < bw; idx += 16) {
1860 src_buf = x->plane[0].src.buf + idx;
1861 vp9_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1864 src_buf = x->plane[0].src.buf;
1865 for (idx = 0; idx < bh; ++idx) {
1866 src_vbuf[idx] = vp9_int_pro_col(src_buf, bw) >> norm_factor;
1867 src_buf += src_stride;
1870 // Find the best match per 1-D search
1871 tmp_mv->col = vector_match(hbuf, src_hbuf, b_width_log2_lookup[bsize]);
1872 tmp_mv->row = vector_match(vbuf, src_vbuf, b_height_log2_lookup[bsize]);
1875 src_buf = x->plane[0].src.buf;
1876 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1877 best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1880 const uint8_t * const pos[4] = {
1881 ref_buf - ref_stride,
1884 ref_buf + ref_stride,
1887 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1890 for (idx = 0; idx < 4; ++idx) {
1891 if (this_sad[idx] < best_sad) {
1892 best_sad = this_sad[idx];
1893 tmp_mv->row = search_pos[idx].row + this_mv.row;
1894 tmp_mv->col = search_pos[idx].col + this_mv.col;
1898 if (this_sad[0] < this_sad[3])
1903 if (this_sad[1] < this_sad[2])
1908 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1910 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1911 ref_buf, ref_stride);
1912 if (best_sad > tmp_sad) {
1920 if (scaled_ref_frame) {
1922 for (i = 0; i < MAX_MB_PLANE; i++)
1923 xd->plane[i].pre[0] = backup_yv12[i];
1929 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1930 point as the best match, we will do a final 1-away diamond
1932 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1933 MV *mvp_full, int step_param,
1934 int sadpb, int further_steps, int do_refine,
1936 const vp9_variance_fn_ptr_t *fn_ptr,
1937 const MV *ref_mv, MV *dst_mv) {
1939 int thissme, n, num00 = 0;
1940 int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1941 step_param, sadpb, &n,
1943 if (bestsme < INT_MAX)
1944 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1947 // If there won't be more n-step search, check to see if refining search is
1949 if (n > further_steps)
1952 while (n < further_steps) {
1958 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1959 step_param + n, sadpb, &num00,
1961 if (thissme < INT_MAX)
1962 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1964 // check to see if refining search is needed.
1965 if (num00 > further_steps - n)
1968 if (thissme < bestsme) {
1975 // final 1-away diamond refining search
1977 const int search_range = 8;
1978 MV best_mv = *dst_mv;
1979 thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1981 if (thissme < INT_MAX)
1982 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1983 if (thissme < bestsme) {
1989 // Return cost list.
1991 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1996 int vp9_full_search_sad_c(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 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) {
2015 for (c = col_min; c < col_max; ++c) {
2016 const MV mv = {r, c};
2017 const int sad = fn_ptr->sdf(what->buf, what->stride,
2018 get_buf_from_mv(in_what, &mv), in_what->stride) +
2019 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2020 if (sad < best_sad) {
2029 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
2030 int sad_per_bit, int distance,
2031 const vp9_variance_fn_ptr_t *fn_ptr,
2032 const MV *center_mv, MV *best_mv) {
2034 const MACROBLOCKD *const xd = &x->e_mbd;
2035 const struct buf_2d *const what = &x->plane[0].src;
2036 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2037 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2038 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2039 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2040 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2041 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2042 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2043 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2044 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2047 for (r = row_min; r < row_max; ++r) {
2049 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2051 if (fn_ptr->sdx3f != NULL) {
2052 while ((c + 2) < col_max) {
2054 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2056 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2059 for (i = 0; i < 3; ++i) {
2060 unsigned int sad = sads[i];
2061 if (sad < best_sad) {
2062 const MV mv = {r, c};
2063 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2064 if (sad < best_sad) {
2075 while (c < col_max) {
2076 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2077 check_here, in_what->stride);
2078 if (sad < best_sad) {
2079 const MV mv = {r, c};
2080 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2081 if (sad < best_sad) {
2094 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
2095 int sad_per_bit, int distance,
2096 const vp9_variance_fn_ptr_t *fn_ptr,
2097 const MV *center_mv, MV *best_mv) {
2099 const MACROBLOCKD *const xd = &x->e_mbd;
2100 const struct buf_2d *const what = &x->plane[0].src;
2101 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2102 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2103 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2104 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2105 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2106 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2107 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2108 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2109 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2112 for (r = row_min; r < row_max; ++r) {
2114 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2116 if (fn_ptr->sdx8f != NULL) {
2117 while ((c + 7) < col_max) {
2119 DECLARE_ALIGNED(16, uint32_t, sads[8]);
2121 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2124 for (i = 0; i < 8; ++i) {
2125 unsigned int sad = sads[i];
2126 if (sad < best_sad) {
2127 const MV mv = {r, c};
2128 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2129 if (sad < best_sad) {
2140 if (fn_ptr->sdx3f != NULL) {
2141 while ((c + 2) < col_max) {
2143 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2145 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2148 for (i = 0; i < 3; ++i) {
2149 unsigned int sad = sads[i];
2150 if (sad < best_sad) {
2151 const MV mv = {r, c};
2152 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2153 if (sad < best_sad) {
2164 while (c < col_max) {
2165 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2166 check_here, in_what->stride);
2167 if (sad < best_sad) {
2168 const MV mv = {r, c};
2169 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2170 if (sad < best_sad) {
2183 int vp9_refining_search_sad(const MACROBLOCK *x,
2184 MV *ref_mv, int error_per_bit,
2186 const vp9_variance_fn_ptr_t *fn_ptr,
2187 const MV *center_mv) {
2188 const MACROBLOCKD *const xd = &x->e_mbd;
2189 const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
2190 const struct buf_2d *const what = &x->plane[0].src;
2191 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2192 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2193 const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
2194 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
2196 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2199 for (i = 0; i < search_range; i++) {
2201 const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
2202 ((ref_mv->row + 1) < x->mv_row_max) &
2203 ((ref_mv->col - 1) > x->mv_col_min) &
2204 ((ref_mv->col + 1) < x->mv_col_max);
2207 unsigned int sads[4];
2208 const uint8_t *const positions[4] = {
2209 best_address - in_what->stride,
2212 best_address + in_what->stride
2215 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2217 for (j = 0; j < 4; ++j) {
2218 if (sads[j] < best_sad) {
2219 const MV mv = {ref_mv->row + neighbors[j].row,
2220 ref_mv->col + neighbors[j].col};
2221 sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2222 if (sads[j] < best_sad) {
2229 for (j = 0; j < 4; ++j) {
2230 const MV mv = {ref_mv->row + neighbors[j].row,
2231 ref_mv->col + neighbors[j].col};
2233 if (is_mv_in(x, &mv)) {
2234 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2235 get_buf_from_mv(in_what, &mv),
2237 if (sad < best_sad) {
2238 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2239 if (sad < best_sad) {
2248 if (best_site == -1) {
2251 ref_mv->row += neighbors[best_site].row;
2252 ref_mv->col += neighbors[best_site].col;
2253 best_address = get_buf_from_mv(in_what, ref_mv);
2260 // This function is called when we do joint motion search in comp_inter_inter
2262 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2263 MV *ref_mv, int error_per_bit,
2265 const vp9_variance_fn_ptr_t *fn_ptr,
2266 const MV *center_mv,
2267 const uint8_t *second_pred) {
2268 const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
2269 {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
2270 const MACROBLOCKD *const xd = &x->e_mbd;
2271 const struct buf_2d *const what = &x->plane[0].src;
2272 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2273 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2274 unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
2275 get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
2276 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2279 for (i = 0; i < search_range; ++i) {
2282 for (j = 0; j < 8; ++j) {
2283 const MV mv = {ref_mv->row + neighbors[j].row,
2284 ref_mv->col + neighbors[j].col};
2286 if (is_mv_in(x, &mv)) {
2287 unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
2288 get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
2289 if (sad < best_sad) {
2290 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2291 if (sad < best_sad) {
2299 if (best_site == -1) {
2302 ref_mv->row += neighbors[best_site].row;
2303 ref_mv->col += neighbors[best_site].col;
2309 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
2310 BLOCK_SIZE bsize, MV *mvp_full,
2311 int step_param, int error_per_bit,
2313 const MV *ref_mv, MV *tmp_mv,
2314 int var_max, int rd) {
2315 const SPEED_FEATURES *const sf = &cpi->sf;
2316 const SEARCH_METHODS method = sf->mv.search_method;
2317 vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
2320 cost_list[0] = INT_MAX;
2321 cost_list[1] = INT_MAX;
2322 cost_list[2] = INT_MAX;
2323 cost_list[3] = INT_MAX;
2324 cost_list[4] = INT_MAX;
2329 var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2330 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2333 var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2334 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2337 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2338 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2341 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2342 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2345 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2346 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2349 var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2350 MAX_MVSEARCH_STEPS - 1 - step_param,
2351 1, cost_list, fn_ptr, ref_mv, tmp_mv);
2354 assert(0 && "Invalid search method.");
2357 if (method != NSTEP && rd && var < var_max)
2358 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);