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 sv[a]f 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 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 static 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);
685 if (second_pred == NULL)
686 thismse = vfp->svf(pre_address, y_stride, sp(tc), sp(tr),
687 src_address, src_stride, &sse);
689 thismse = vfp->svaf(pre_address, y_stride, sp(tc), sp(tr),
690 src_address, src_stride, &sse, second_pred);
691 cost_array[idx] = thismse +
692 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
694 if (cost_array[idx] < besterr) {
696 besterr = cost_array[idx];
697 *distortion = thismse;
701 cost_array[idx] = INT_MAX;
705 // Check diagonal sub-pixel position
706 tc = bc + (cost_array[0] < cost_array[1] ? -hstep : hstep);
707 tr = br + (cost_array[2] < cost_array[3] ? -hstep : hstep);
708 if (tc >= minc && tc <= maxc && tr >= minr && tr <= maxr) {
709 const uint8_t *const pre_address = y + (tr >> 3) * y_stride + (tc >> 3);
710 MV this_mv = {tr, tc};
711 if (second_pred == NULL)
712 thismse = vfp->svf(pre_address, y_stride, sp(tc), sp(tr),
713 src_address, src_stride, &sse);
715 thismse = vfp->svaf(pre_address, y_stride, sp(tc), sp(tr),
716 src_address, src_stride, &sse, second_pred);
717 cost_array[4] = thismse +
718 mv_err_cost(&this_mv, ref_mv, mvjcost, mvcost, error_per_bit);
720 if (cost_array[4] < besterr) {
722 besterr = cost_array[4];
723 *distortion = thismse;
727 cost_array[idx] = INT_MAX;
730 if (best_idx < 4 && best_idx >= 0) {
731 br += search_step[best_idx].row;
732 bc += search_step[best_idx].col;
733 } else if (best_idx == 4) {
738 if (iters_per_step > 1 && best_idx != -1)
749 // Each subsequent iteration checks at least one point in common with
750 // the last iteration could be 2 ( if diag selected) 1/4 pel
752 // These lines insure static analysis doesn't warn that
753 // tr and tc aren't used after the above point.
760 if ((abs(bestmv->col - ref_mv->col) > (MAX_FULL_PEL_VAL << 3)) ||
761 (abs(bestmv->row - ref_mv->row) > (MAX_FULL_PEL_VAL << 3)))
771 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
773 return ((row - range) >= x->mv_row_min) &
774 ((row + range) <= x->mv_row_max) &
775 ((col - range) >= x->mv_col_min) &
776 ((col + range) <= x->mv_col_max);
779 static INLINE int is_mv_in(const MACROBLOCK *x, const MV *mv) {
780 return (mv->col >= x->mv_col_min) && (mv->col <= x->mv_col_max) &&
781 (mv->row >= x->mv_row_min) && (mv->row <= x->mv_row_max);
784 #define CHECK_BETTER \
786 if (thissad < bestsad) {\
788 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
789 if (thissad < bestsad) {\
796 #define MAX_PATTERN_SCALES 11
797 #define MAX_PATTERN_CANDIDATES 8 // max number of canddiates per scale
798 #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
800 // Calculate and return a sad+mvcost list around an integer best pel.
801 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
804 const vp9_variance_fn_ptr_t *fn_ptr,
807 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
808 const struct buf_2d *const what = &x->plane[0].src;
809 const struct buf_2d *const in_what = &x->e_mbd.plane[0].pre[0];
810 const MV fcenter_mv = {ref_mv->row >> 3, ref_mv->col >> 3};
811 int br = best_mv->row;
812 int bc = best_mv->col;
819 cost_list[0] = fn_ptr->vf(what->buf, what->stride,
820 get_buf_from_mv(in_what, &this_mv),
821 in_what->stride, &sse) +
822 mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
823 if (check_bounds(x, br, bc, 1)) {
824 for (i = 0; i < 4; i++) {
825 const MV this_mv = {br + neighbors[i].row,
826 bc + neighbors[i].col};
827 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
828 get_buf_from_mv(in_what, &this_mv),
829 in_what->stride, &sse) +
830 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
831 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
835 for (i = 0; i < 4; i++) {
836 const MV this_mv = {br + neighbors[i].row,
837 bc + neighbors[i].col};
838 if (!is_mv_in(x, &this_mv))
839 cost_list[i + 1] = INT_MAX;
841 cost_list[i + 1] = fn_ptr->vf(what->buf, what->stride,
842 get_buf_from_mv(in_what, &this_mv),
843 in_what->stride, &sse) +
844 // mvsad_err_cost(x, &this_mv, &fcenter_mv, sadpb);
845 mv_err_cost(&this_mv, &fcenter_mv, x->nmvjointcost, x->mvcost,
851 // Generic pattern search function that searches over multiple scales.
852 // Each scale can have a different number of candidates and shape of
853 // candidates as indicated in the num_candidates and candidates arrays
854 // passed into this function
856 static int vp9_pattern_search(const MACROBLOCK *x,
862 const vp9_variance_fn_ptr_t *vfp,
866 const int num_candidates[MAX_PATTERN_SCALES],
867 const MV candidates[MAX_PATTERN_SCALES]
868 [MAX_PATTERN_CANDIDATES]) {
869 const MACROBLOCKD *const xd = &x->e_mbd;
870 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
871 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
874 const struct buf_2d *const what = &x->plane[0].src;
875 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
877 int bestsad = INT_MAX;
880 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
881 int best_init_s = search_param_to_steps[search_param];
882 // adjust ref_mv to make sure it is within MV range
883 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
887 // Work out the start point for the search
888 bestsad = vfp->sdf(what->buf, what->stride,
889 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
890 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
892 // Search all possible scales upto the search param around the center point
893 // pick the scale of the point that is best as the starting scale of
894 // further steps around it.
895 if (do_init_search) {
898 for (t = 0; t <= s; ++t) {
900 if (check_bounds(x, br, bc, 1 << t)) {
901 for (i = 0; i < num_candidates[t]; i++) {
902 const MV this_mv = {br + candidates[t][i].row,
903 bc + candidates[t][i].col};
904 thissad = vfp->sdf(what->buf, what->stride,
905 get_buf_from_mv(in_what, &this_mv),
910 for (i = 0; i < num_candidates[t]; i++) {
911 const MV this_mv = {br + candidates[t][i].row,
912 bc + candidates[t][i].col};
913 if (!is_mv_in(x, &this_mv))
915 thissad = vfp->sdf(what->buf, what->stride,
916 get_buf_from_mv(in_what, &this_mv),
921 if (best_site == -1) {
928 if (best_init_s != -1) {
929 br += candidates[best_init_s][k].row;
930 bc += candidates[best_init_s][k].col;
934 // If the center point is still the best, just skip this and move to
935 // the refinement step.
936 if (best_init_s != -1) {
941 // No need to search all 6 points the 1st time if initial search was used
942 if (!do_init_search || s != best_init_s) {
943 if (check_bounds(x, br, bc, 1 << s)) {
944 for (i = 0; i < num_candidates[s]; i++) {
945 const MV this_mv = {br + candidates[s][i].row,
946 bc + candidates[s][i].col};
947 thissad = vfp->sdf(what->buf, what->stride,
948 get_buf_from_mv(in_what, &this_mv),
953 for (i = 0; i < num_candidates[s]; i++) {
954 const MV this_mv = {br + candidates[s][i].row,
955 bc + candidates[s][i].col};
956 if (!is_mv_in(x, &this_mv))
958 thissad = vfp->sdf(what->buf, what->stride,
959 get_buf_from_mv(in_what, &this_mv),
965 if (best_site == -1) {
968 br += candidates[s][best_site].row;
969 bc += candidates[s][best_site].col;
975 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
977 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
978 next_chkpts_indices[1] = k;
979 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
981 if (check_bounds(x, br, bc, 1 << s)) {
982 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
983 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
984 bc + candidates[s][next_chkpts_indices[i]].col};
985 thissad = vfp->sdf(what->buf, what->stride,
986 get_buf_from_mv(in_what, &this_mv),
991 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
992 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
993 bc + candidates[s][next_chkpts_indices[i]].col};
994 if (!is_mv_in(x, &this_mv))
996 thissad = vfp->sdf(what->buf, what->stride,
997 get_buf_from_mv(in_what, &this_mv),
1003 if (best_site != -1) {
1004 k = next_chkpts_indices[best_site];
1005 br += candidates[s][k].row;
1006 bc += candidates[s][k].col;
1008 } while (best_site != -1);
1012 // Returns the one-away integer pel sad values around the best as follows:
1013 // cost_list[0]: cost at the best integer pel
1014 // cost_list[1]: cost at delta {0, -1} (left) from the best integer pel
1015 // cost_list[2]: cost at delta { 1, 0} (bottom) from the best integer pel
1016 // cost_list[3]: cost at delta { 0, 1} (right) from the best integer pel
1017 // cost_list[4]: cost at delta {-1, 0} (top) from the best integer pel
1019 const MV best_mv = { br, bc };
1020 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1027 // A specialized function where the smallest scale search candidates
1028 // are 4 1-away neighbors, and cost_list is non-null
1029 // TODO(debargha): Merge this function with the one above. Also remove
1030 // use_mvcost option since it is always 1, to save unnecessary branches.
1031 static int vp9_pattern_search_sad(const MACROBLOCK *x,
1037 const vp9_variance_fn_ptr_t *vfp,
1039 const MV *center_mv,
1041 const int num_candidates[MAX_PATTERN_SCALES],
1042 const MV candidates[MAX_PATTERN_SCALES]
1043 [MAX_PATTERN_CANDIDATES]) {
1044 const MACROBLOCKD *const xd = &x->e_mbd;
1045 static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
1046 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1049 const struct buf_2d *const what = &x->plane[0].src;
1050 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1052 int bestsad = INT_MAX;
1055 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1056 int best_init_s = search_param_to_steps[search_param];
1057 // adjust ref_mv to make sure it is within MV range
1058 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1061 if (cost_list != NULL) {
1062 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1066 // Work out the start point for the search
1067 bestsad = vfp->sdf(what->buf, what->stride,
1068 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1069 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1071 // Search all possible scales upto the search param around the center point
1072 // pick the scale of the point that is best as the starting scale of
1073 // further steps around it.
1074 if (do_init_search) {
1077 for (t = 0; t <= s; ++t) {
1079 if (check_bounds(x, br, bc, 1 << t)) {
1080 for (i = 0; i < num_candidates[t]; i++) {
1081 const MV this_mv = {br + candidates[t][i].row,
1082 bc + candidates[t][i].col};
1083 thissad = vfp->sdf(what->buf, what->stride,
1084 get_buf_from_mv(in_what, &this_mv),
1089 for (i = 0; i < num_candidates[t]; i++) {
1090 const MV this_mv = {br + candidates[t][i].row,
1091 bc + candidates[t][i].col};
1092 if (!is_mv_in(x, &this_mv))
1094 thissad = vfp->sdf(what->buf, what->stride,
1095 get_buf_from_mv(in_what, &this_mv),
1100 if (best_site == -1) {
1107 if (best_init_s != -1) {
1108 br += candidates[best_init_s][k].row;
1109 bc += candidates[best_init_s][k].col;
1113 // If the center point is still the best, just skip this and move to
1114 // the refinement step.
1115 if (best_init_s != -1) {
1116 int do_sad = (num_candidates[0] == 4 && cost_list != NULL);
1120 for (; s >= do_sad; s--) {
1121 if (!do_init_search || s != best_init_s) {
1122 if (check_bounds(x, br, bc, 1 << s)) {
1123 for (i = 0; i < num_candidates[s]; i++) {
1124 const MV this_mv = {br + candidates[s][i].row,
1125 bc + candidates[s][i].col};
1126 thissad = vfp->sdf(what->buf, what->stride,
1127 get_buf_from_mv(in_what, &this_mv),
1132 for (i = 0; i < num_candidates[s]; i++) {
1133 const MV this_mv = {br + candidates[s][i].row,
1134 bc + candidates[s][i].col};
1135 if (!is_mv_in(x, &this_mv))
1137 thissad = vfp->sdf(what->buf, what->stride,
1138 get_buf_from_mv(in_what, &this_mv),
1144 if (best_site == -1) {
1147 br += candidates[s][best_site].row;
1148 bc += candidates[s][best_site].col;
1154 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1156 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1157 next_chkpts_indices[1] = k;
1158 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1160 if (check_bounds(x, br, bc, 1 << s)) {
1161 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1162 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1163 bc + candidates[s][next_chkpts_indices[i]].col};
1164 thissad = vfp->sdf(what->buf, what->stride,
1165 get_buf_from_mv(in_what, &this_mv),
1170 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1171 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1172 bc + candidates[s][next_chkpts_indices[i]].col};
1173 if (!is_mv_in(x, &this_mv))
1175 thissad = vfp->sdf(what->buf, what->stride,
1176 get_buf_from_mv(in_what, &this_mv),
1182 if (best_site != -1) {
1183 k = next_chkpts_indices[best_site];
1184 br += candidates[s][k].row;
1185 bc += candidates[s][k].col;
1187 } while (best_site != -1);
1190 // Note: If we enter the if below, then cost_list must be non-NULL.
1192 cost_list[0] = bestsad;
1193 if (!do_init_search || s != best_init_s) {
1194 if (check_bounds(x, br, bc, 1 << s)) {
1195 for (i = 0; i < num_candidates[s]; i++) {
1196 const MV this_mv = {br + candidates[s][i].row,
1197 bc + candidates[s][i].col};
1199 thissad = vfp->sdf(what->buf, what->stride,
1200 get_buf_from_mv(in_what, &this_mv),
1205 for (i = 0; i < num_candidates[s]; i++) {
1206 const MV this_mv = {br + candidates[s][i].row,
1207 bc + candidates[s][i].col};
1208 if (!is_mv_in(x, &this_mv))
1211 thissad = vfp->sdf(what->buf, what->stride,
1212 get_buf_from_mv(in_what, &this_mv),
1218 if (best_site != -1) {
1219 br += candidates[s][best_site].row;
1220 bc += candidates[s][best_site].col;
1224 while (best_site != -1) {
1225 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1227 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1228 next_chkpts_indices[1] = k;
1229 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1230 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1231 cost_list[((k + 2) % 4) + 1] = cost_list[0];
1232 cost_list[0] = bestsad;
1234 if (check_bounds(x, br, bc, 1 << s)) {
1235 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1236 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1237 bc + candidates[s][next_chkpts_indices[i]].col};
1238 cost_list[next_chkpts_indices[i] + 1] =
1239 thissad = vfp->sdf(what->buf, what->stride,
1240 get_buf_from_mv(in_what, &this_mv),
1245 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
1246 const MV this_mv = {br + candidates[s][next_chkpts_indices[i]].row,
1247 bc + candidates[s][next_chkpts_indices[i]].col};
1248 if (!is_mv_in(x, &this_mv)) {
1249 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1252 cost_list[next_chkpts_indices[i] + 1] =
1253 thissad = vfp->sdf(what->buf, what->stride,
1254 get_buf_from_mv(in_what, &this_mv),
1260 if (best_site != -1) {
1261 k = next_chkpts_indices[best_site];
1262 br += candidates[s][k].row;
1263 bc += candidates[s][k].col;
1269 // Returns the one-away integer pel sad values around the best as follows:
1270 // cost_list[0]: sad at the best integer pel
1271 // cost_list[1]: sad at delta {0, -1} (left) from the best integer pel
1272 // cost_list[2]: sad at delta { 1, 0} (bottom) from the best integer pel
1273 // cost_list[3]: sad at delta { 0, 1} (right) from the best integer pel
1274 // cost_list[4]: sad at delta {-1, 0} (top) from the best integer pel
1276 static const MV neighbors[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
1277 if (cost_list[0] == INT_MAX) {
1278 cost_list[0] = bestsad;
1279 if (check_bounds(x, br, bc, 1)) {
1280 for (i = 0; i < 4; i++) {
1281 const MV this_mv = { br + neighbors[i].row,
1282 bc + neighbors[i].col };
1283 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1284 get_buf_from_mv(in_what, &this_mv),
1288 for (i = 0; i < 4; i++) {
1289 const MV this_mv = {br + neighbors[i].row,
1290 bc + neighbors[i].col};
1291 if (!is_mv_in(x, &this_mv))
1292 cost_list[i + 1] = INT_MAX;
1294 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1295 get_buf_from_mv(in_what, &this_mv),
1301 for (i = 0; i < 4; i++) {
1302 const MV this_mv = {br + neighbors[i].row,
1303 bc + neighbors[i].col};
1304 if (cost_list[i + 1] != INT_MAX) {
1306 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1317 int vp9_get_mvpred_var(const MACROBLOCK *x,
1318 const MV *best_mv, const MV *center_mv,
1319 const vp9_variance_fn_ptr_t *vfp,
1321 const MACROBLOCKD *const xd = &x->e_mbd;
1322 const struct buf_2d *const what = &x->plane[0].src;
1323 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1324 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1325 unsigned int unused;
1327 return vfp->vf(what->buf, what->stride,
1328 get_buf_from_mv(in_what, best_mv), in_what->stride, &unused) +
1329 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1330 x->mvcost, x->errorperbit) : 0);
1333 int vp9_get_mvpred_av_var(const MACROBLOCK *x,
1334 const MV *best_mv, const MV *center_mv,
1335 const uint8_t *second_pred,
1336 const vp9_variance_fn_ptr_t *vfp,
1338 const MACROBLOCKD *const xd = &x->e_mbd;
1339 const struct buf_2d *const what = &x->plane[0].src;
1340 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1341 const MV mv = {best_mv->row * 8, best_mv->col * 8};
1342 unsigned int unused;
1344 return vfp->svaf(get_buf_from_mv(in_what, best_mv), in_what->stride, 0, 0,
1345 what->buf, what->stride, &unused, second_pred) +
1346 (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmvjointcost,
1347 x->mvcost, x->errorperbit) : 0);
1350 int vp9_hex_search(const MACROBLOCK *x,
1356 const vp9_variance_fn_ptr_t *vfp,
1358 const MV *center_mv, MV *best_mv) {
1359 // First scale has 8-closest points, the rest have 6 points in hex shape
1360 // at increasing scales
1361 static const int hex_num_candidates[MAX_PATTERN_SCALES] = {
1362 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
1364 // Note that the largest candidate step at each scale is 2^scale
1365 static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
1366 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, { 0, 1}, { -1, 1}, {-1, 0}},
1367 {{-1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0}},
1368 {{-2, -4}, {2, -4}, {4, 0}, {2, 4}, { -2, 4}, { -4, 0}},
1369 {{-4, -8}, {4, -8}, {8, 0}, {4, 8}, { -4, 8}, { -8, 0}},
1370 {{-8, -16}, {8, -16}, {16, 0}, {8, 16}, { -8, 16}, { -16, 0}},
1371 {{-16, -32}, {16, -32}, {32, 0}, {16, 32}, { -16, 32}, { -32, 0}},
1372 {{-32, -64}, {32, -64}, {64, 0}, {32, 64}, { -32, 64}, { -64, 0}},
1373 {{-64, -128}, {64, -128}, {128, 0}, {64, 128}, { -64, 128}, { -128, 0}},
1374 {{-128, -256}, {128, -256}, {256, 0}, {128, 256}, { -128, 256}, { -256, 0}},
1375 {{-256, -512}, {256, -512}, {512, 0}, {256, 512}, { -256, 512}, { -512, 0}},
1376 {{-512, -1024}, {512, -1024}, {1024, 0}, {512, 1024}, { -512, 1024},
1379 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1380 do_init_search, cost_list, vfp, use_mvcost,
1382 hex_num_candidates, hex_candidates);
1385 int vp9_bigdia_search(const MACROBLOCK *x,
1391 const vp9_variance_fn_ptr_t *vfp,
1393 const MV *center_mv,
1395 // First scale has 4-closest points, the rest have 8 points in diamond
1396 // shape at increasing scales
1397 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
1398 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1400 // Note that the largest candidate step at each scale is 2^scale
1401 static const MV bigdia_candidates[MAX_PATTERN_SCALES]
1402 [MAX_PATTERN_CANDIDATES] = {
1403 {{0, -1}, {1, 0}, { 0, 1}, {-1, 0}},
1404 {{-1, -1}, {0, -2}, {1, -1}, {2, 0}, {1, 1}, {0, 2}, {-1, 1}, {-2, 0}},
1405 {{-2, -2}, {0, -4}, {2, -2}, {4, 0}, {2, 2}, {0, 4}, {-2, 2}, {-4, 0}},
1406 {{-4, -4}, {0, -8}, {4, -4}, {8, 0}, {4, 4}, {0, 8}, {-4, 4}, {-8, 0}},
1407 {{-8, -8}, {0, -16}, {8, -8}, {16, 0}, {8, 8}, {0, 16}, {-8, 8}, {-16, 0}},
1408 {{-16, -16}, {0, -32}, {16, -16}, {32, 0}, {16, 16}, {0, 32},
1409 {-16, 16}, {-32, 0}},
1410 {{-32, -32}, {0, -64}, {32, -32}, {64, 0}, {32, 32}, {0, 64},
1411 {-32, 32}, {-64, 0}},
1412 {{-64, -64}, {0, -128}, {64, -64}, {128, 0}, {64, 64}, {0, 128},
1413 {-64, 64}, {-128, 0}},
1414 {{-128, -128}, {0, -256}, {128, -128}, {256, 0}, {128, 128}, {0, 256},
1415 {-128, 128}, {-256, 0}},
1416 {{-256, -256}, {0, -512}, {256, -256}, {512, 0}, {256, 256}, {0, 512},
1417 {-256, 256}, {-512, 0}},
1418 {{-512, -512}, {0, -1024}, {512, -512}, {1024, 0}, {512, 512}, {0, 1024},
1419 {-512, 512}, {-1024, 0}},
1421 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1422 do_init_search, cost_list, vfp, use_mvcost,
1424 bigdia_num_candidates, bigdia_candidates);
1427 int vp9_square_search(const MACROBLOCK *x,
1433 const vp9_variance_fn_ptr_t *vfp,
1435 const MV *center_mv,
1437 // All scales have 8 closest points in square shape
1438 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
1439 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1441 // Note that the largest candidate step at each scale is 2^scale
1442 static const MV square_candidates[MAX_PATTERN_SCALES]
1443 [MAX_PATTERN_CANDIDATES] = {
1444 {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}},
1445 {{-2, -2}, {0, -2}, {2, -2}, {2, 0}, {2, 2}, {0, 2}, {-2, 2}, {-2, 0}},
1446 {{-4, -4}, {0, -4}, {4, -4}, {4, 0}, {4, 4}, {0, 4}, {-4, 4}, {-4, 0}},
1447 {{-8, -8}, {0, -8}, {8, -8}, {8, 0}, {8, 8}, {0, 8}, {-8, 8}, {-8, 0}},
1448 {{-16, -16}, {0, -16}, {16, -16}, {16, 0}, {16, 16}, {0, 16},
1449 {-16, 16}, {-16, 0}},
1450 {{-32, -32}, {0, -32}, {32, -32}, {32, 0}, {32, 32}, {0, 32},
1451 {-32, 32}, {-32, 0}},
1452 {{-64, -64}, {0, -64}, {64, -64}, {64, 0}, {64, 64}, {0, 64},
1453 {-64, 64}, {-64, 0}},
1454 {{-128, -128}, {0, -128}, {128, -128}, {128, 0}, {128, 128}, {0, 128},
1455 {-128, 128}, {-128, 0}},
1456 {{-256, -256}, {0, -256}, {256, -256}, {256, 0}, {256, 256}, {0, 256},
1457 {-256, 256}, {-256, 0}},
1458 {{-512, -512}, {0, -512}, {512, -512}, {512, 0}, {512, 512}, {0, 512},
1459 {-512, 512}, {-512, 0}},
1460 {{-1024, -1024}, {0, -1024}, {1024, -1024}, {1024, 0}, {1024, 1024},
1461 {0, 1024}, {-1024, 1024}, {-1024, 0}},
1463 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1464 do_init_search, cost_list, vfp, use_mvcost,
1466 square_num_candidates, square_candidates);
1469 int vp9_fast_hex_search(const MACROBLOCK *x,
1473 int do_init_search, // must be zero for fast_hex
1475 const vp9_variance_fn_ptr_t *vfp,
1477 const MV *center_mv,
1479 return vp9_hex_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1480 sad_per_bit, do_init_search, cost_list, vfp, use_mvcost,
1481 center_mv, best_mv);
1484 int vp9_fast_dia_search(const MACROBLOCK *x,
1490 const vp9_variance_fn_ptr_t *vfp,
1492 const MV *center_mv,
1494 return vp9_bigdia_search(x, ref_mv, MAX(MAX_MVSEARCH_STEPS - 2, search_param),
1495 sad_per_bit, do_init_search, cost_list, vfp,
1496 use_mvcost, center_mv, best_mv);
1501 int vp9_full_range_search_c(const MACROBLOCK *x,
1502 const search_site_config *cfg,
1503 MV *ref_mv, MV *best_mv,
1504 int search_param, int sad_per_bit, int *num00,
1505 const vp9_variance_fn_ptr_t *fn_ptr,
1506 const MV *center_mv) {
1507 const MACROBLOCKD *const xd = &x->e_mbd;
1508 const struct buf_2d *const what = &x->plane[0].src;
1509 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1510 const int range = 64;
1511 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1512 unsigned int best_sad = INT_MAX;
1514 int start_col, end_col, start_row, end_row;
1516 // The cfg and search_param parameters are not used in this search variant
1520 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1523 best_sad = fn_ptr->sdf(what->buf, what->stride,
1524 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
1525 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
1526 start_row = MAX(-range, x->mv_row_min - ref_mv->row);
1527 start_col = MAX(-range, x->mv_col_min - ref_mv->col);
1528 end_row = MIN(range, x->mv_row_max - ref_mv->row);
1529 end_col = MIN(range, x->mv_col_max - ref_mv->col);
1531 for (r = start_row; r <= end_row; ++r) {
1532 for (c = start_col; c <= end_col; c += 4) {
1533 if (c + 3 <= end_col) {
1534 unsigned int sads[4];
1535 const uint8_t *addrs[4];
1536 for (i = 0; i < 4; ++i) {
1537 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1538 addrs[i] = get_buf_from_mv(in_what, &mv);
1541 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1543 for (i = 0; i < 4; ++i) {
1544 if (sads[i] < best_sad) {
1545 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1546 const unsigned int sad = sads[i] +
1547 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1548 if (sad < best_sad) {
1555 for (i = 0; i < end_col - c; ++i) {
1556 const MV mv = {ref_mv->row + r, ref_mv->col + c + i};
1557 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
1558 get_buf_from_mv(in_what, &mv), in_what->stride);
1559 if (sad < best_sad) {
1560 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
1561 if (sad < best_sad) {
1574 int vp9_diamond_search_sad_c(const MACROBLOCK *x,
1575 const search_site_config *cfg,
1576 MV *ref_mv, MV *best_mv, int search_param,
1577 int sad_per_bit, int *num00,
1578 const vp9_variance_fn_ptr_t *fn_ptr,
1579 const MV *center_mv) {
1582 const MACROBLOCKD *const xd = &x->e_mbd;
1583 uint8_t *what = x->plane[0].src.buf;
1584 const int what_stride = x->plane[0].src.stride;
1585 const uint8_t *in_what;
1586 const int in_what_stride = xd->plane[0].pre[0].stride;
1587 const uint8_t *best_address;
1589 unsigned int bestsad = INT_MAX;
1596 // search_param determines the length of the initial step and hence the number
1598 // 0 = initial step (MAX_FIRST_STEP) pel
1599 // 1 = (MAX_FIRST_STEP/2) pel,
1600 // 2 = (MAX_FIRST_STEP/4) pel...
1601 const search_site *ss = &cfg->ss[search_param * cfg->searches_per_step];
1602 const int tot_steps = (cfg->ss_count / cfg->searches_per_step) - search_param;
1604 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
1605 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1606 ref_row = ref_mv->row;
1607 ref_col = ref_mv->col;
1609 best_mv->row = ref_row;
1610 best_mv->col = ref_col;
1612 // Work out the start point for the search
1613 in_what = xd->plane[0].pre[0].buf + ref_row * in_what_stride + ref_col;
1614 best_address = in_what;
1616 // Check the starting position
1617 bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride)
1618 + mvsad_err_cost(x, best_mv, &fcenter_mv, sad_per_bit);
1622 for (step = 0; step < tot_steps; step++) {
1625 // All_in is true if every one of the points we are checking are within
1626 // the bounds of the image.
1627 all_in &= ((best_mv->row + ss[i].mv.row) > x->mv_row_min);
1628 all_in &= ((best_mv->row + ss[i + 1].mv.row) < x->mv_row_max);
1629 all_in &= ((best_mv->col + ss[i + 2].mv.col) > x->mv_col_min);
1630 all_in &= ((best_mv->col + ss[i + 3].mv.col) < x->mv_col_max);
1632 // If all the pixels are within the bounds we don't check whether the
1633 // search point is valid in this loop, otherwise we check each point
1636 unsigned int sad_array[4];
1638 for (j = 0; j < cfg->searches_per_step; j += 4) {
1639 unsigned char const *block_offset[4];
1641 for (t = 0; t < 4; t++)
1642 block_offset[t] = ss[i + t].offset + best_address;
1644 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1647 for (t = 0; t < 4; t++, i++) {
1648 if (sad_array[t] < bestsad) {
1649 const MV this_mv = {best_mv->row + ss[i].mv.row,
1650 best_mv->col + ss[i].mv.col};
1651 sad_array[t] += mvsad_err_cost(x, &this_mv, &fcenter_mv,
1653 if (sad_array[t] < bestsad) {
1654 bestsad = sad_array[t];
1661 for (j = 0; j < cfg->searches_per_step; j++) {
1662 // Trap illegal vectors
1663 const MV this_mv = {best_mv->row + ss[i].mv.row,
1664 best_mv->col + ss[i].mv.col};
1666 if (is_mv_in(x, &this_mv)) {
1667 const uint8_t *const check_here = ss[i].offset + best_address;
1668 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1671 if (thissad < bestsad) {
1672 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1673 if (thissad < bestsad) {
1682 if (best_site != last_site) {
1683 best_mv->row += ss[best_site].mv.row;
1684 best_mv->col += ss[best_site].mv.col;
1685 best_address += ss[best_site].offset;
1686 last_site = best_site;
1687 #if defined(NEW_DIAMOND_SEARCH)
1689 const MV this_mv = {best_mv->row + ss[best_site].mv.row,
1690 best_mv->col + ss[best_site].mv.col};
1691 if (is_mv_in(x, &this_mv)) {
1692 const uint8_t *const check_here = ss[best_site].offset + best_address;
1693 unsigned int thissad = fn_ptr->sdf(what, what_stride, check_here,
1695 if (thissad < bestsad) {
1696 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1697 if (thissad < bestsad) {
1699 best_mv->row += ss[best_site].mv.row;
1700 best_mv->col += ss[best_site].mv.col;
1701 best_address += ss[best_site].offset;
1709 } else if (best_address == in_what) {
1716 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1717 int best_sad = INT_MAX;
1720 int center, offset = 0;
1721 int bw = 4 << bwl; // redundant variable, to be changed in the experiments.
1722 for (d = 0; d <= bw; d += 16) {
1723 this_sad = vp9_vector_var(&ref[d], src, bwl);
1724 if (this_sad < best_sad) {
1725 best_sad = this_sad;
1731 for (d = -8; d <= 8; d += 16) {
1732 int this_pos = offset + d;
1734 if (this_pos < 0 || this_pos > bw)
1736 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1737 if (this_sad < best_sad) {
1738 best_sad = this_sad;
1744 for (d = -4; d <= 4; d += 8) {
1745 int this_pos = offset + d;
1747 if (this_pos < 0 || this_pos > bw)
1749 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1750 if (this_sad < best_sad) {
1751 best_sad = this_sad;
1757 for (d = -2; d <= 2; d += 4) {
1758 int this_pos = offset + d;
1760 if (this_pos < 0 || this_pos > bw)
1762 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1763 if (this_sad < best_sad) {
1764 best_sad = this_sad;
1770 for (d = -1; d <= 1; d += 2) {
1771 int this_pos = offset + d;
1773 if (this_pos < 0 || this_pos > bw)
1775 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1776 if (this_sad < best_sad) {
1777 best_sad = this_sad;
1782 return (center - (bw >> 1));
1785 static const MV search_pos[4] = {
1786 {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1789 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1791 int mi_row, int mi_col) {
1792 MACROBLOCKD *xd = &x->e_mbd;
1793 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1794 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
1795 DECLARE_ALIGNED(16, int16_t, hbuf[128]);
1796 DECLARE_ALIGNED(16, int16_t, vbuf[128]);
1797 DECLARE_ALIGNED(16, int16_t, src_hbuf[64]);
1798 DECLARE_ALIGNED(16, int16_t, src_vbuf[64]);
1800 const int bw = 4 << b_width_log2_lookup[bsize];
1801 const int bh = 4 << b_height_log2_lookup[bsize];
1802 const int search_width = bw << 1;
1803 const int search_height = bh << 1;
1804 const int src_stride = x->plane[0].src.stride;
1805 const int ref_stride = xd->plane[0].pre[0].stride;
1806 uint8_t const *ref_buf, *src_buf;
1807 MV *tmp_mv = &xd->mi[0]->mbmi.mv[0].as_mv;
1808 unsigned int best_sad, tmp_sad, this_sad[4];
1810 const int norm_factor = 3 + (bw >> 5);
1811 const YV12_BUFFER_CONFIG *scaled_ref_frame =
1812 vp9_get_scaled_ref_frame(cpi, mbmi->ref_frame[0]);
1814 if (scaled_ref_frame) {
1816 // Swap out the reference frame for a version that's been scaled to
1817 // match the resolution of the current frame, allowing the existing
1818 // motion search code to be used without additional modifications.
1819 for (i = 0; i < MAX_MB_PLANE; i++)
1820 backup_yv12[i] = xd->plane[i].pre[0];
1821 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
1824 #if CONFIG_VP9_HIGHBITDEPTH
1826 unsigned int this_sad;
1829 this_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1830 xd->plane[0].pre[0].buf, ref_stride);
1832 if (scaled_ref_frame) {
1834 for (i = 0; i < MAX_MB_PLANE; i++)
1835 xd->plane[i].pre[0] = backup_yv12[i];
1841 // Set up prediction 1-D reference set
1842 ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1843 for (idx = 0; idx < search_width; idx += 16) {
1844 vp9_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1848 ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1849 for (idx = 0; idx < search_height; ++idx) {
1850 vbuf[idx] = vp9_int_pro_col(ref_buf, bw) >> norm_factor;
1851 ref_buf += ref_stride;
1854 // Set up src 1-D reference set
1855 for (idx = 0; idx < bw; idx += 16) {
1856 src_buf = x->plane[0].src.buf + idx;
1857 vp9_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1860 src_buf = x->plane[0].src.buf;
1861 for (idx = 0; idx < bh; ++idx) {
1862 src_vbuf[idx] = vp9_int_pro_col(src_buf, bw) >> norm_factor;
1863 src_buf += src_stride;
1866 // Find the best match per 1-D search
1867 tmp_mv->col = vector_match(hbuf, src_hbuf, b_width_log2_lookup[bsize]);
1868 tmp_mv->row = vector_match(vbuf, src_vbuf, b_height_log2_lookup[bsize]);
1871 src_buf = x->plane[0].src.buf;
1872 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1873 best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1876 const uint8_t * const pos[4] = {
1877 ref_buf - ref_stride,
1880 ref_buf + ref_stride,
1883 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1886 for (idx = 0; idx < 4; ++idx) {
1887 if (this_sad[idx] < best_sad) {
1888 best_sad = this_sad[idx];
1889 tmp_mv->row = search_pos[idx].row + this_mv.row;
1890 tmp_mv->col = search_pos[idx].col + this_mv.col;
1894 if (this_sad[0] < this_sad[3])
1899 if (this_sad[1] < this_sad[2])
1904 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1906 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1907 ref_buf, ref_stride);
1908 if (best_sad > tmp_sad) {
1916 if (scaled_ref_frame) {
1918 for (i = 0; i < MAX_MB_PLANE; i++)
1919 xd->plane[i].pre[0] = backup_yv12[i];
1925 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1926 point as the best match, we will do a final 1-away diamond
1928 int vp9_full_pixel_diamond(const VP9_COMP *cpi, MACROBLOCK *x,
1929 MV *mvp_full, int step_param,
1930 int sadpb, int further_steps, int do_refine,
1932 const vp9_variance_fn_ptr_t *fn_ptr,
1933 const MV *ref_mv, MV *dst_mv) {
1935 int thissme, n, num00 = 0;
1936 int bestsme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1937 step_param, sadpb, &n,
1939 if (bestsme < INT_MAX)
1940 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1943 // If there won't be more n-step search, check to see if refining search is
1945 if (n > further_steps)
1948 while (n < further_steps) {
1954 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1955 step_param + n, sadpb, &num00,
1957 if (thissme < INT_MAX)
1958 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1960 // check to see if refining search is needed.
1961 if (num00 > further_steps - n)
1964 if (thissme < bestsme) {
1971 // final 1-away diamond refining search
1973 const int search_range = 8;
1974 MV best_mv = *dst_mv;
1975 thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1977 if (thissme < INT_MAX)
1978 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1979 if (thissme < bestsme) {
1985 // Return cost list.
1987 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1992 int vp9_full_search_sad_c(const MACROBLOCK *x, const MV *ref_mv,
1993 int sad_per_bit, int distance,
1994 const vp9_variance_fn_ptr_t *fn_ptr,
1995 const MV *center_mv, MV *best_mv) {
1997 const MACROBLOCKD *const xd = &x->e_mbd;
1998 const struct buf_2d *const what = &x->plane[0].src;
1999 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2000 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2001 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2002 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2003 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2004 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2005 int best_sad = fn_ptr->sdf(what->buf, what->stride,
2006 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2007 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2010 for (r = row_min; r < row_max; ++r) {
2011 for (c = col_min; c < col_max; ++c) {
2012 const MV mv = {r, c};
2013 const int sad = fn_ptr->sdf(what->buf, what->stride,
2014 get_buf_from_mv(in_what, &mv), in_what->stride) +
2015 mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2016 if (sad < best_sad) {
2025 int vp9_full_search_sadx3(const MACROBLOCK *x, const MV *ref_mv,
2026 int sad_per_bit, int distance,
2027 const vp9_variance_fn_ptr_t *fn_ptr,
2028 const MV *center_mv, MV *best_mv) {
2030 const MACROBLOCKD *const xd = &x->e_mbd;
2031 const struct buf_2d *const what = &x->plane[0].src;
2032 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2033 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2034 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2035 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2036 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2037 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2038 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2039 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2040 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2043 for (r = row_min; r < row_max; ++r) {
2045 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2047 if (fn_ptr->sdx3f != NULL) {
2048 while ((c + 2) < col_max) {
2050 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2052 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2055 for (i = 0; i < 3; ++i) {
2056 unsigned int sad = sads[i];
2057 if (sad < best_sad) {
2058 const MV mv = {r, c};
2059 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2060 if (sad < best_sad) {
2071 while (c < col_max) {
2072 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2073 check_here, in_what->stride);
2074 if (sad < best_sad) {
2075 const MV mv = {r, c};
2076 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2077 if (sad < best_sad) {
2090 int vp9_full_search_sadx8(const MACROBLOCK *x, const MV *ref_mv,
2091 int sad_per_bit, int distance,
2092 const vp9_variance_fn_ptr_t *fn_ptr,
2093 const MV *center_mv, MV *best_mv) {
2095 const MACROBLOCKD *const xd = &x->e_mbd;
2096 const struct buf_2d *const what = &x->plane[0].src;
2097 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2098 const int row_min = MAX(ref_mv->row - distance, x->mv_row_min);
2099 const int row_max = MIN(ref_mv->row + distance, x->mv_row_max);
2100 const int col_min = MAX(ref_mv->col - distance, x->mv_col_min);
2101 const int col_max = MIN(ref_mv->col + distance, x->mv_col_max);
2102 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2103 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride,
2104 get_buf_from_mv(in_what, ref_mv), in_what->stride) +
2105 mvsad_err_cost(x, ref_mv, &fcenter_mv, sad_per_bit);
2108 for (r = row_min; r < row_max; ++r) {
2110 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2112 if (fn_ptr->sdx8f != NULL) {
2113 while ((c + 7) < col_max) {
2115 DECLARE_ALIGNED(16, uint32_t, sads[8]);
2117 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2120 for (i = 0; i < 8; ++i) {
2121 unsigned int sad = sads[i];
2122 if (sad < best_sad) {
2123 const MV mv = {r, c};
2124 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2125 if (sad < best_sad) {
2136 if (fn_ptr->sdx3f != NULL) {
2137 while ((c + 2) < col_max) {
2139 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2141 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2144 for (i = 0; i < 3; ++i) {
2145 unsigned int sad = sads[i];
2146 if (sad < best_sad) {
2147 const MV mv = {r, c};
2148 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2149 if (sad < best_sad) {
2160 while (c < col_max) {
2161 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2162 check_here, in_what->stride);
2163 if (sad < best_sad) {
2164 const MV mv = {r, c};
2165 sad += mvsad_err_cost(x, &mv, &fcenter_mv, sad_per_bit);
2166 if (sad < best_sad) {
2179 int vp9_refining_search_sad(const MACROBLOCK *x,
2180 MV *ref_mv, int error_per_bit,
2182 const vp9_variance_fn_ptr_t *fn_ptr,
2183 const MV *center_mv) {
2184 const MACROBLOCKD *const xd = &x->e_mbd;
2185 const MV neighbors[4] = {{ -1, 0}, {0, -1}, {0, 1}, {1, 0}};
2186 const struct buf_2d *const what = &x->plane[0].src;
2187 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2188 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2189 const uint8_t *best_address = get_buf_from_mv(in_what, ref_mv);
2190 unsigned int best_sad = fn_ptr->sdf(what->buf, what->stride, best_address,
2192 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2195 for (i = 0; i < search_range; i++) {
2197 const int all_in = ((ref_mv->row - 1) > x->mv_row_min) &
2198 ((ref_mv->row + 1) < x->mv_row_max) &
2199 ((ref_mv->col - 1) > x->mv_col_min) &
2200 ((ref_mv->col + 1) < x->mv_col_max);
2203 unsigned int sads[4];
2204 const uint8_t *const positions[4] = {
2205 best_address - in_what->stride,
2208 best_address + in_what->stride
2211 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2213 for (j = 0; j < 4; ++j) {
2214 if (sads[j] < best_sad) {
2215 const MV mv = {ref_mv->row + neighbors[j].row,
2216 ref_mv->col + neighbors[j].col};
2217 sads[j] += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2218 if (sads[j] < best_sad) {
2225 for (j = 0; j < 4; ++j) {
2226 const MV mv = {ref_mv->row + neighbors[j].row,
2227 ref_mv->col + neighbors[j].col};
2229 if (is_mv_in(x, &mv)) {
2230 unsigned int sad = fn_ptr->sdf(what->buf, what->stride,
2231 get_buf_from_mv(in_what, &mv),
2233 if (sad < best_sad) {
2234 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2235 if (sad < best_sad) {
2244 if (best_site == -1) {
2247 ref_mv->row += neighbors[best_site].row;
2248 ref_mv->col += neighbors[best_site].col;
2249 best_address = get_buf_from_mv(in_what, ref_mv);
2256 // This function is called when we do joint motion search in comp_inter_inter
2258 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2259 MV *ref_mv, int error_per_bit,
2261 const vp9_variance_fn_ptr_t *fn_ptr,
2262 const MV *center_mv,
2263 const uint8_t *second_pred) {
2264 const MV neighbors[8] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
2265 {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
2266 const MACROBLOCKD *const xd = &x->e_mbd;
2267 const struct buf_2d *const what = &x->plane[0].src;
2268 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
2269 const MV fcenter_mv = {center_mv->row >> 3, center_mv->col >> 3};
2270 unsigned int best_sad = fn_ptr->sdaf(what->buf, what->stride,
2271 get_buf_from_mv(in_what, ref_mv), in_what->stride, second_pred) +
2272 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2275 for (i = 0; i < search_range; ++i) {
2278 for (j = 0; j < 8; ++j) {
2279 const MV mv = {ref_mv->row + neighbors[j].row,
2280 ref_mv->col + neighbors[j].col};
2282 if (is_mv_in(x, &mv)) {
2283 unsigned int sad = fn_ptr->sdaf(what->buf, what->stride,
2284 get_buf_from_mv(in_what, &mv), in_what->stride, second_pred);
2285 if (sad < best_sad) {
2286 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2287 if (sad < best_sad) {
2295 if (best_site == -1) {
2298 ref_mv->row += neighbors[best_site].row;
2299 ref_mv->col += neighbors[best_site].col;
2305 int vp9_full_pixel_search(VP9_COMP *cpi, MACROBLOCK *x,
2306 BLOCK_SIZE bsize, MV *mvp_full,
2307 int step_param, int error_per_bit,
2309 const MV *ref_mv, MV *tmp_mv,
2310 int var_max, int rd) {
2311 const SPEED_FEATURES *const sf = &cpi->sf;
2312 const SEARCH_METHODS method = sf->mv.search_method;
2313 vp9_variance_fn_ptr_t *fn_ptr = &cpi->fn_ptr[bsize];
2316 cost_list[0] = INT_MAX;
2317 cost_list[1] = INT_MAX;
2318 cost_list[2] = INT_MAX;
2319 cost_list[3] = INT_MAX;
2320 cost_list[4] = INT_MAX;
2325 var = vp9_fast_dia_search(x, mvp_full, step_param, error_per_bit, 0,
2326 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2329 var = vp9_fast_hex_search(x, mvp_full, step_param, error_per_bit, 0,
2330 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2333 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2334 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2337 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2338 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2341 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2342 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2345 var = vp9_full_pixel_diamond(cpi, x, mvp_full, step_param, error_per_bit,
2346 MAX_MVSEARCH_STEPS - 1 - step_param,
2347 1, cost_list, fn_ptr, ref_mv, tmp_mv);
2350 assert(0 && "Invalid search method.");
2353 if (method != NSTEP && rd && var < var_max)
2354 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);