1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vpx_config.h"
16 #include "./vpx_dsp_rtcd.h"
17
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h"
20
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_reconinter.h"
23
invoked_unary_negate_function(const Predicate & pred,const Expr & expr)24 #include "vp9/encoder/vp9_encoder.h"
25 #include "vp9/encoder/vp9_mcomp.h"
26
27 // #define NEW_DIAMOND_SEARCH
28
29 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
30 const MV *mv) {
31 return &buf->buf[mv->row * buf->stride + mv->col];
32 }
33
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);
expr() const36 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;
39
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);
44
45 // Get intersection of UMV window and valid MV window to reduce # of checks
46 // in diamond search.
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)
invoked_binary_negate_function(const Predicate & pred,const Expr1 & expr1,const Expr2 & expr2)52 x->mv_row_min = row_min;
53 if (x->mv_row_max > row_max)
54 x->mv_row_max = row_max;
55 }
56
57 int vp9_init_search_range(int size) {
58 int sr = 0;
59 // Minimum search size no matter what the passed in value.
60 size = MAX(16, size);
pred() const61
62 while ((size << sr) < MAX_FULL_PEL_VAL)
63 sr++;
64
65 sr = MIN(sr, MAX_MVSEARCH_STEPS - 2);
expr1() const66 return sr;
67 }
68
69 static INLINE int mv_cost(const MV *mv,
70 const int *joint_cost, int *const comp_cost[2]) {
expr2() const71 return joint_cost[vp9_get_mv_joint(mv)] +
72 comp_cost[0][mv->row] + comp_cost[1][mv->col];
73 }
74
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,
78 mv->col - ref->col };
79 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) * weight, 7);
80 }
81
82 static int mv_err_cost(const MV *mv, const MV *ref,
83 const int *mvjcost, int *mvcost[2],
84 int error_per_bit) {
85 if (mvcost) {
86 const MV diff = { mv->row - ref->row,
87 mv->col - ref->col };
88 return ROUND_POWER_OF_TWO(mv_cost(&diff, mvjcost, mvcost) *
89 error_per_bit, 13);
90 }
91 return 0;
92 }
93
94 static int mvsad_err_cost(const MACROBLOCK *x, const MV *mv, const MV *ref,
95 int error_per_bit) {
96 const MV diff = { mv->row - ref->row,
97 mv->col - ref->col };
98 return ROUND_POWER_OF_TWO(mv_cost(&diff, x->nmvjointsadcost,
99 x->nmvsadcost) * error_per_bit, 8);
100 }
101
102 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride) {
103 int len, ss_count = 1;
104
105 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
106 cfg->ss[0].offset = 0;
107
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}};
111 int i;
112 for (i = 0; i < 4; ++i) {
113 search_site *const ss = &cfg->ss[ss_count++];
114 ss->mv = ss_mvs[i];
115 ss->offset = ss->mv.row * stride + ss->mv.col;
116 }
117 }
unary_negate(Predicate pred)118
119 cfg->ss_count = ss_count;
120 cfg->searches_per_step = 4;
121 }
122
123 void vp9_init3smotion_compensation(search_site_config *cfg, int stride) {
124 int len, ss_count = 1;
125
operator ()(const Arg & arg) const126 cfg->ss[0].mv.col = cfg->ss[0].mv.row = 0;
127 cfg->ss[0].offset = 0;
128
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}
134 };
135 int i;
136 for (i = 0; i < 8; ++i) {
137 search_site *const ss = &cfg->ss[ss_count++];
138 ss->mv = ss_mvs[i];
139 ss->offset = ss->mv.row * stride + ss->mv.col;
140 }
141 }
142
143 cfg->ss_count = ss_count;
144 cfg->searches_per_step = 8;
binary_negate(Predicate pred)145 }
146
147 /*
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.
155 */
156
157 /* estimated cost of a motion vector (r,c) */
158 #define MVC(r, c) \
159 (mvcost ? \
160 ((mvjcost[((r) != rr) * 2 + ((c) != rc)] + \
161 mvcost[0][((r) - rr)] + mvcost[1][((c) - rc)]) * \
162 error_per_bit + 4096) >> 13 : 0)
163
164
165 // convert motion vector component to offset for sv[a]f calc
166 static INLINE int sp(int x) {
167 return x & 7;
168 }
169
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)];
not1(const Predicate & predicate)172 }
173
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, \
179 src_stride, &sse); \
180 else \
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) { \
184 besterr = v; \
185 br = r; \
186 bc = c; \
187 *distortion = thismse; \
188 *sse1 = sse; \
189 } \
190 } else { \
191 v = INT_MAX; \
192 }
193
194 #define FIRST_LEVEL_CHECKS \
195 { \
196 unsigned int left, right, up, down, diag; \
operator ()boost::compute::logical_not197 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) { \
204 case 0: \
205 CHECK_BETTER(diag, tr - hstep, tc - hstep); \
206 break; \
207 case 1: \
208 CHECK_BETTER(diag, tr - hstep, tc + hstep); \
209 break; \
210 case 2: \
211 CHECK_BETTER(diag, tr + hstep, tc - hstep); \
212 break; \
213 case 3: \
214 CHECK_BETTER(diag, tr + hstep, tc + hstep); \
215 break; \
216 } \
217 }
218
219 #define SECOND_LEVEL_CHECKS \
220 { \
221 int kr, kc; \
222 unsigned int second; \
223 if (tr != br && tc != bc) { \
224 kr = br - tr; \
225 kc = bc - tc; \
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) { \
229 kc = bc - tc; \
230 CHECK_BETTER(second, tr + hstep, tc + 2 * kc); \
231 CHECK_BETTER(second, tr - hstep, tc + 2 * kc); \
232 switch (whichdir) { \
233 case 0: \
234 case 1: \
235 CHECK_BETTER(second, tr + hstep, tc + kc); \
236 break; \
237 case 2: \
238 case 3: \
239 CHECK_BETTER(second, tr - hstep, tc + kc); \
240 break; \
241 } \
242 } else if (tr != br && tc == bc) { \
243 kr = br - tr; \
244 CHECK_BETTER(second, tr + 2 * kr, tc + hstep); \
245 CHECK_BETTER(second, tr + 2 * kr, tc - hstep); \
246 switch (whichdir) { \
247 case 0: \
248 case 2: \
249 CHECK_BETTER(second, tr + kr, tc + hstep); \
250 break; \
251 case 1: \
252 case 3: \
253 CHECK_BETTER(second, tr + kr, tc - hstep); \
254 break; \
255 } \
256 } \
257 }
258
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; \
264 unsigned int sse; \
265 unsigned int whichdir; \
266 int thismse; \
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; \
273 \
274 int rr = ref_mv->row; \
275 int rc = ref_mv->col; \
276 int br = bestmv->row * 8; \
277 int bc = bestmv->col * 8; \
278 int hstep = 4; \
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); \
283 int tr = br; \
284 int tc = bc; \
285 \
286 bestmv->row *= 8; \
287 bestmv->col *= 8;
288
289 static INLINE unsigned int setup_center_error(const MACROBLOCKD *xd,
290 const MV *bestmv,
291 const MV *ref_mv,
292 int error_per_bit,
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,
297 int y_stride,
298 const uint8_t *second_pred,
299 int w, int h, int offset,
300 int *mvjcost, int *mvcost[2],
301 unsigned int *sse1,
302 int *distortion) {
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,
309 y_stride);
310 besterr = vfp->vf(CONVERT_TO_BYTEPTR(comp_pred16), w, src, src_stride,
311 sse1);
312 } else {
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);
316 }
317 } else {
318 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
319 }
320 *distortion = besterr;
321 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
322 #else
323 (void) xd;
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);
328 } else {
329 besterr = vfp->vf(y + offset, y_stride, src, src_stride, sse1);
330 }
331 *distortion = besterr;
332 besterr += mv_err_cost(bestmv, ref_mv, mvjcost, mvcost, error_per_bit);
333 #endif // CONFIG_VP9_HIGHBITDEPTH
334 return besterr;
335 }
336
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);
339 }
340
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];
346 }
347
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,
357 int bits) {
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]));
362 }
363
364 int vp9_find_best_sub_pixel_tree_pruned_evenmore(
365 const MACROBLOCK *x,
366 MV *bestmv, const MV *ref_mv,
367 int allow_hp,
368 int error_per_bit,
369 const vp9_variance_fn_ptr_t *vfp,
370 int forced_stop,
371 int iters_per_step,
372 int *cost_list,
373 int *mvjcost, int *mvcost[2],
374 int *distortion,
375 unsigned int *sse1,
376 const uint8_t *second_pred,
377 int w, int h) {
378 SETUP_SUBPEL_SEARCH;
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,
382 sse1, distortion);
383 (void) halfiters;
384 (void) quarteriters;
385 (void) eighthiters;
386 (void) whichdir;
387 (void) allow_hp;
388 (void) forced_stop;
389 (void) hstep;
390
391 if (cost_list &&
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)) {
396 int ir, ic;
397 unsigned int minpt;
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);
401 }
402 } else {
403 FIRST_LEVEL_CHECKS;
404 if (halfiters > 1) {
405 SECOND_LEVEL_CHECKS;
406 }
407
408 tr = br;
409 tc = bc;
410
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) {
415 hstep >>= 1;
416 FIRST_LEVEL_CHECKS;
417 if (quarteriters > 1) {
418 SECOND_LEVEL_CHECKS;
419 }
420 }
421 }
422
423 tr = br;
424 tc = bc;
425
426 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
427 hstep >>= 1;
428 FIRST_LEVEL_CHECKS;
429 if (eighthiters > 1) {
430 SECOND_LEVEL_CHECKS;
431 }
432 }
433
434 bestmv->row = br;
435 bestmv->col = bc;
436
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)))
439 return INT_MAX;
440
441 return besterr;
442 }
443
444 int vp9_find_best_sub_pixel_tree_pruned_more(const MACROBLOCK *x,
445 MV *bestmv, const MV *ref_mv,
446 int allow_hp,
447 int error_per_bit,
448 const vp9_variance_fn_ptr_t *vfp,
449 int forced_stop,
450 int iters_per_step,
451 int *cost_list,
452 int *mvjcost, int *mvcost[2],
453 int *distortion,
454 unsigned int *sse1,
455 const uint8_t *second_pred,
456 int w, int h) {
457 SETUP_SUBPEL_SEARCH;
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,
461 sse1, distortion);
462 if (cost_list &&
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)) {
467 unsigned int minpt;
468 int ir, ic;
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);
472 }
473 } else {
474 FIRST_LEVEL_CHECKS;
475 if (halfiters > 1) {
476 SECOND_LEVEL_CHECKS;
477 }
478 }
479
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
482
483 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
484 if (forced_stop != 2) {
485 tr = br;
486 tc = bc;
487 hstep >>= 1;
488 FIRST_LEVEL_CHECKS;
489 if (quarteriters > 1) {
490 SECOND_LEVEL_CHECKS;
491 }
492 }
493
494 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
495 tr = br;
496 tc = bc;
497 hstep >>= 1;
498 FIRST_LEVEL_CHECKS;
499 if (eighthiters > 1) {
500 SECOND_LEVEL_CHECKS;
501 }
502 }
503 // These lines insure static analysis doesn't warn that
504 // tr and tc aren't used after the above point.
505 (void) tr;
506 (void) tc;
507
508 bestmv->row = br;
509 bestmv->col = bc;
510
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)))
513 return INT_MAX;
514
515 return besterr;
516 }
517
518 int vp9_find_best_sub_pixel_tree_pruned(const MACROBLOCK *x,
519 MV *bestmv, const MV *ref_mv,
520 int allow_hp,
521 int error_per_bit,
522 const vp9_variance_fn_ptr_t *vfp,
523 int forced_stop,
524 int iters_per_step,
525 int *cost_list,
526 int *mvjcost, int *mvcost[2],
527 int *distortion,
528 unsigned int *sse1,
529 const uint8_t *second_pred,
530 int w, int h) {
531 SETUP_SUBPEL_SEARCH;
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,
535 sse1, distortion);
536 if (cost_list &&
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);
543 switch (whichdir) {
544 case 0:
545 CHECK_BETTER(left, tr, tc - hstep);
546 CHECK_BETTER(down, tr + hstep, tc);
547 CHECK_BETTER(diag, tr + hstep, tc - hstep);
548 break;
549 case 1:
550 CHECK_BETTER(right, tr, tc + hstep);
551 CHECK_BETTER(down, tr + hstep, tc);
552 CHECK_BETTER(diag, tr + hstep, tc + hstep);
553 break;
554 case 2:
555 CHECK_BETTER(left, tr, tc - hstep);
556 CHECK_BETTER(up, tr - hstep, tc);
557 CHECK_BETTER(diag, tr - hstep, tc - hstep);
558 break;
559 case 3:
560 CHECK_BETTER(right, tr, tc + hstep);
561 CHECK_BETTER(up, tr - hstep, tc);
562 CHECK_BETTER(diag, tr - hstep, tc + hstep);
563 break;
564 }
565 } else {
566 FIRST_LEVEL_CHECKS;
567 if (halfiters > 1) {
568 SECOND_LEVEL_CHECKS;
569 }
570 }
571
572 tr = br;
573 tc = bc;
574
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
577
578 // Note forced_stop: 0 - full, 1 - qtr only, 2 - half only
579 if (forced_stop != 2) {
580 hstep >>= 1;
581 FIRST_LEVEL_CHECKS;
582 if (quarteriters > 1) {
583 SECOND_LEVEL_CHECKS;
584 }
585 tr = br;
586 tc = bc;
587 }
588
589 if (allow_hp && vp9_use_mv_hp(ref_mv) && forced_stop == 0) {
590 hstep >>= 1;
591 FIRST_LEVEL_CHECKS;
592 if (eighthiters > 1) {
593 SECOND_LEVEL_CHECKS;
594 }
595 tr = br;
596 tc = bc;
597 }
598 // These lines insure static analysis doesn't warn that
599 // tr and tc aren't used after the above point.
600 (void) tr;
601 (void) tc;
602
603 bestmv->row = br;
604 bestmv->col = bc;
605
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)))
608 return INT_MAX;
609
610 return besterr;
611 }
612
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}
618 };
619
620 int vp9_find_best_sub_pixel_tree(const MACROBLOCK *x,
621 MV *bestmv, const MV *ref_mv,
622 int allow_hp,
623 int error_per_bit,
624 const vp9_variance_fn_ptr_t *vfp,
625 int forced_stop,
626 int iters_per_step,
627 int *cost_list,
628 int *mvjcost, int *mvcost[2],
629 int *distortion,
630 unsigned int *sse1,
631 const uint8_t *second_pred,
632 int w, int h) {
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;
638 unsigned int sse;
639 unsigned int whichdir = 0;
640 int thismse;
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;
644
645 int rr = ref_mv->row;
646 int rc = ref_mv->col;
647 int br = bestmv->row * 8;
648 int bc = bestmv->col * 8;
649 int hstep = 4;
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);
655 int tr = br;
656 int tc = bc;
657 const MV *search_step = search_step_table;
658 int idx, best_idx = -1;
659 unsigned int cost_array[5];
660
661 if (!(allow_hp && vp9_use_mv_hp(ref_mv)))
662 if (round == 3)
663 round = 2;
664
665 bestmv->row *= 8;
666 bestmv->col *= 8;
667
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,
671 sse1, distortion);
672
673 (void) cost_list; // to silence compiler warning
674
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 MV this_mv;
683 this_mv.row = tr;
684 this_mv.col = tc;
685 if (second_pred == NULL)
686 thismse = vfp->svf(pre_address, y_stride, sp(tc), sp(tr),
687 src_address, src_stride, &sse);
688 else
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);
693
694 if (cost_array[idx] < besterr) {
695 best_idx = idx;
696 besterr = cost_array[idx];
697 *distortion = thismse;
698 *sse1 = sse;
699 }
700 } else {
701 cost_array[idx] = INT_MAX;
702 }
703 }
704
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);
714 else
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);
719
720 if (cost_array[4] < besterr) {
721 best_idx = 4;
722 besterr = cost_array[4];
723 *distortion = thismse;
724 *sse1 = sse;
725 }
726 } else {
727 cost_array[idx] = INT_MAX;
728 }
729
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) {
734 br = tr;
735 bc = tc;
736 }
737
738 if (iters_per_step > 1)
739 SECOND_LEVEL_CHECKS;
740
741 tr = br;
742 tc = bc;
743
744 search_step += 4;
745 hstep >>= 1;
746 best_idx = -1;
747 }
748
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
751
752 // These lines insure static analysis doesn't warn that
753 // tr and tc aren't used after the above point.
754 (void) tr;
755 (void) tc;
756
757 bestmv->row = br;
758 bestmv->col = bc;
759
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)))
762 return INT_MAX;
763
764 return besterr;
765 }
766
767 #undef MVC
768 #undef PRE
769 #undef CHECK_BETTER
770
771 static INLINE int check_bounds(const MACROBLOCK *x, int row, int col,
772 int range) {
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);
777 }
778
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);
782 }
783
784 #define CHECK_BETTER \
785 {\
786 if (thissad < bestsad) {\
787 if (use_mvcost) \
788 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);\
789 if (thissad < bestsad) {\
790 bestsad = thissad;\
791 best_site = i;\
792 }\
793 }\
794 }
795
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
799
800 // Calculate and return a sad+mvcost list around an integer best pel.
801 static INLINE void calc_int_cost_list(const MACROBLOCK *x,
802 const MV *ref_mv,
803 int sadpb,
804 const vp9_variance_fn_ptr_t *fn_ptr,
805 const MV *best_mv,
806 int *cost_list) {
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;
813 MV this_mv;
814 int i;
815 unsigned int sse;
816
817 this_mv.row = br;
818 this_mv.col = bc;
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,
832 x->errorperbit);
833 }
834 } else {
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;
840 else
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,
846 x->errorperbit);
847 }
848 }
849 }
850
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
855 //
856 static int vp9_pattern_search(const MACROBLOCK *x,
857 MV *ref_mv,
858 int search_param,
859 int sad_per_bit,
860 int do_init_search,
861 int *cost_list,
862 const vp9_variance_fn_ptr_t *vfp,
863 int use_mvcost,
864 const MV *center_mv,
865 MV *best_mv,
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,
872 };
873 int i, s, t;
874 const struct buf_2d *const what = &x->plane[0].src;
875 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
876 int br, bc;
877 int bestsad = INT_MAX;
878 int thissad;
879 int k = -1;
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);
884 br = ref_mv->row;
885 bc = ref_mv->col;
886
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);
891
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) {
896 s = best_init_s;
897 best_init_s = -1;
898 for (t = 0; t <= s; ++t) {
899 int best_site = -1;
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),
906 in_what->stride);
907 CHECK_BETTER
908 }
909 } else {
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))
914 continue;
915 thissad = vfp->sdf(what->buf, what->stride,
916 get_buf_from_mv(in_what, &this_mv),
917 in_what->stride);
918 CHECK_BETTER
919 }
920 }
921 if (best_site == -1) {
922 continue;
923 } else {
924 best_init_s = t;
925 k = best_site;
926 }
927 }
928 if (best_init_s != -1) {
929 br += candidates[best_init_s][k].row;
930 bc += candidates[best_init_s][k].col;
931 }
932 }
933
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) {
937 int best_site = -1;
938 s = best_init_s;
939
940 do {
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),
949 in_what->stride);
950 CHECK_BETTER
951 }
952 } else {
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))
957 continue;
958 thissad = vfp->sdf(what->buf, what->stride,
959 get_buf_from_mv(in_what, &this_mv),
960 in_what->stride);
961 CHECK_BETTER
962 }
963 }
964
965 if (best_site == -1) {
966 continue;
967 } else {
968 br += candidates[s][best_site].row;
969 bc += candidates[s][best_site].col;
970 k = best_site;
971 }
972 }
973
974 do {
975 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
976 best_site = -1;
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;
980
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),
987 in_what->stride);
988 CHECK_BETTER
989 }
990 } else {
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))
995 continue;
996 thissad = vfp->sdf(what->buf, what->stride,
997 get_buf_from_mv(in_what, &this_mv),
998 in_what->stride);
999 CHECK_BETTER
1000 }
1001 }
1002
1003 if (best_site != -1) {
1004 k = next_chkpts_indices[best_site];
1005 br += candidates[s][k].row;
1006 bc += candidates[s][k].col;
1007 }
1008 } while (best_site != -1);
1009 } while (s--);
1010 }
1011
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
1018 if (cost_list) {
1019 const MV best_mv = { br, bc };
1020 calc_int_cost_list(x, &fcenter_mv, sad_per_bit, vfp, &best_mv, cost_list);
1021 }
1022 best_mv->row = br;
1023 best_mv->col = bc;
1024 return bestsad;
1025 }
1026
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,
1032 MV *ref_mv,
1033 int search_param,
1034 int sad_per_bit,
1035 int do_init_search,
1036 int *cost_list,
1037 const vp9_variance_fn_ptr_t *vfp,
1038 int use_mvcost,
1039 const MV *center_mv,
1040 MV *best_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,
1047 };
1048 int i, s, t;
1049 const struct buf_2d *const what = &x->plane[0].src;
1050 const struct buf_2d *const in_what = &xd->plane[0].pre[0];
1051 int br, bc;
1052 int bestsad = INT_MAX;
1053 int thissad;
1054 int k = -1;
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);
1059 br = ref_mv->row;
1060 bc = ref_mv->col;
1061 if (cost_list != NULL) {
1062 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
1063 INT_MAX;
1064 }
1065
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);
1070
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) {
1075 s = best_init_s;
1076 best_init_s = -1;
1077 for (t = 0; t <= s; ++t) {
1078 int best_site = -1;
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),
1085 in_what->stride);
1086 CHECK_BETTER
1087 }
1088 } else {
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))
1093 continue;
1094 thissad = vfp->sdf(what->buf, what->stride,
1095 get_buf_from_mv(in_what, &this_mv),
1096 in_what->stride);
1097 CHECK_BETTER
1098 }
1099 }
1100 if (best_site == -1) {
1101 continue;
1102 } else {
1103 best_init_s = t;
1104 k = best_site;
1105 }
1106 }
1107 if (best_init_s != -1) {
1108 br += candidates[best_init_s][k].row;
1109 bc += candidates[best_init_s][k].col;
1110 }
1111 }
1112
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);
1117 int best_site = -1;
1118 s = best_init_s;
1119
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),
1128 in_what->stride);
1129 CHECK_BETTER
1130 }
1131 } else {
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))
1136 continue;
1137 thissad = vfp->sdf(what->buf, what->stride,
1138 get_buf_from_mv(in_what, &this_mv),
1139 in_what->stride);
1140 CHECK_BETTER
1141 }
1142 }
1143
1144 if (best_site == -1) {
1145 continue;
1146 } else {
1147 br += candidates[s][best_site].row;
1148 bc += candidates[s][best_site].col;
1149 k = best_site;
1150 }
1151 }
1152
1153 do {
1154 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1155 best_site = -1;
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;
1159
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),
1166 in_what->stride);
1167 CHECK_BETTER
1168 }
1169 } else {
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))
1174 continue;
1175 thissad = vfp->sdf(what->buf, what->stride,
1176 get_buf_from_mv(in_what, &this_mv),
1177 in_what->stride);
1178 CHECK_BETTER
1179 }
1180 }
1181
1182 if (best_site != -1) {
1183 k = next_chkpts_indices[best_site];
1184 br += candidates[s][k].row;
1185 bc += candidates[s][k].col;
1186 }
1187 } while (best_site != -1);
1188 }
1189
1190 // Note: If we enter the if below, then cost_list must be non-NULL.
1191 if (s == 0) {
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};
1198 cost_list[i + 1] =
1199 thissad = vfp->sdf(what->buf, what->stride,
1200 get_buf_from_mv(in_what, &this_mv),
1201 in_what->stride);
1202 CHECK_BETTER
1203 }
1204 } else {
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))
1209 continue;
1210 cost_list[i + 1] =
1211 thissad = vfp->sdf(what->buf, what->stride,
1212 get_buf_from_mv(in_what, &this_mv),
1213 in_what->stride);
1214 CHECK_BETTER
1215 }
1216 }
1217
1218 if (best_site != -1) {
1219 br += candidates[s][best_site].row;
1220 bc += candidates[s][best_site].col;
1221 k = best_site;
1222 }
1223 }
1224 while (best_site != -1) {
1225 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1226 best_site = -1;
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;
1233
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),
1241 in_what->stride);
1242 CHECK_BETTER
1243 }
1244 } else {
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;
1250 continue;
1251 }
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),
1255 in_what->stride);
1256 CHECK_BETTER
1257 }
1258 }
1259
1260 if (best_site != -1) {
1261 k = next_chkpts_indices[best_site];
1262 br += candidates[s][k].row;
1263 bc += candidates[s][k].col;
1264 }
1265 }
1266 }
1267 }
1268
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
1275 if (cost_list) {
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),
1285 in_what->stride);
1286 }
1287 } else {
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;
1293 else
1294 cost_list[i + 1] = vfp->sdf(what->buf, what->stride,
1295 get_buf_from_mv(in_what, &this_mv),
1296 in_what->stride);
1297 }
1298 }
1299 } else {
1300 if (use_mvcost) {
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) {
1305 cost_list[i + 1] +=
1306 mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1307 }
1308 }
1309 }
1310 }
1311 }
1312 best_mv->row = br;
1313 best_mv->col = bc;
1314 return bestsad;
1315 }
1316
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,
1320 int use_mvcost) {
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;
1326
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);
1331 }
1332
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,
1337 int use_mvcost) {
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;
1343
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);
1348 }
1349
1350 int vp9_hex_search(const MACROBLOCK *x,
1351 MV *ref_mv,
1352 int search_param,
1353 int sad_per_bit,
1354 int do_init_search,
1355 int *cost_list,
1356 const vp9_variance_fn_ptr_t *vfp,
1357 int use_mvcost,
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
1363 };
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},
1377 { -1024, 0}},
1378 };
1379 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1380 do_init_search, cost_list, vfp, use_mvcost,
1381 center_mv, best_mv,
1382 hex_num_candidates, hex_candidates);
1383 }
1384
1385 int vp9_bigdia_search(const MACROBLOCK *x,
1386 MV *ref_mv,
1387 int search_param,
1388 int sad_per_bit,
1389 int do_init_search,
1390 int *cost_list,
1391 const vp9_variance_fn_ptr_t *vfp,
1392 int use_mvcost,
1393 const MV *center_mv,
1394 MV *best_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,
1399 };
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}},
1420 };
1421 return vp9_pattern_search_sad(x, ref_mv, search_param, sad_per_bit,
1422 do_init_search, cost_list, vfp, use_mvcost,
1423 center_mv, best_mv,
1424 bigdia_num_candidates, bigdia_candidates);
1425 }
1426
1427 int vp9_square_search(const MACROBLOCK *x,
1428 MV *ref_mv,
1429 int search_param,
1430 int sad_per_bit,
1431 int do_init_search,
1432 int *cost_list,
1433 const vp9_variance_fn_ptr_t *vfp,
1434 int use_mvcost,
1435 const MV *center_mv,
1436 MV *best_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,
1440 };
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}},
1462 };
1463 return vp9_pattern_search(x, ref_mv, search_param, sad_per_bit,
1464 do_init_search, cost_list, vfp, use_mvcost,
1465 center_mv, best_mv,
1466 square_num_candidates, square_candidates);
1467 }
1468
1469 int vp9_fast_hex_search(const MACROBLOCK *x,
1470 MV *ref_mv,
1471 int search_param,
1472 int sad_per_bit,
1473 int do_init_search, // must be zero for fast_hex
1474 int *cost_list,
1475 const vp9_variance_fn_ptr_t *vfp,
1476 int use_mvcost,
1477 const MV *center_mv,
1478 MV *best_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);
1482 }
1483
1484 int vp9_fast_dia_search(const MACROBLOCK *x,
1485 MV *ref_mv,
1486 int search_param,
1487 int sad_per_bit,
1488 int do_init_search,
1489 int *cost_list,
1490 const vp9_variance_fn_ptr_t *vfp,
1491 int use_mvcost,
1492 const MV *center_mv,
1493 MV *best_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);
1497 }
1498
1499 #undef CHECK_BETTER
1500
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;
1513 int r, c, i;
1514 int start_col, end_col, start_row, end_row;
1515
1516 // The cfg and search_param parameters are not used in this search variant
1517 (void)cfg;
1518 (void)search_param;
1519
1520 clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1521 *best_mv = *ref_mv;
1522 *num00 = 11;
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);
1530
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);
1539 }
1540
1541 fn_ptr->sdx4df(what->buf, what->stride, addrs, in_what->stride, sads);
1542
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) {
1549 best_sad = sad;
1550 *best_mv = mv;
1551 }
1552 }
1553 }
1554 } else {
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) {
1562 best_sad = sad;
1563 *best_mv = mv;
1564 }
1565 }
1566 }
1567 }
1568 }
1569 }
1570
1571 return best_sad;
1572 }
1573
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) {
1580 int i, j, step;
1581
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;
1588
1589 unsigned int bestsad = INT_MAX;
1590 int best_site = 0;
1591 int last_site = 0;
1592
1593 int ref_row;
1594 int ref_col;
1595
1596 // search_param determines the length of the initial step and hence the number
1597 // of iterations.
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;
1603
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;
1608 *num00 = 0;
1609 best_mv->row = ref_row;
1610 best_mv->col = ref_col;
1611
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;
1615
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);
1619
1620 i = 1;
1621
1622 for (step = 0; step < tot_steps; step++) {
1623 int all_in = 1, t;
1624
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);
1631
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
1634 // for validity..
1635 if (all_in) {
1636 unsigned int sad_array[4];
1637
1638 for (j = 0; j < cfg->searches_per_step; j += 4) {
1639 unsigned char const *block_offset[4];
1640
1641 for (t = 0; t < 4; t++)
1642 block_offset[t] = ss[i + t].offset + best_address;
1643
1644 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride,
1645 sad_array);
1646
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,
1652 sad_per_bit);
1653 if (sad_array[t] < bestsad) {
1654 bestsad = sad_array[t];
1655 best_site = i;
1656 }
1657 }
1658 }
1659 }
1660 } else {
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};
1665
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,
1669 in_what_stride);
1670
1671 if (thissad < bestsad) {
1672 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1673 if (thissad < bestsad) {
1674 bestsad = thissad;
1675 best_site = i;
1676 }
1677 }
1678 }
1679 i++;
1680 }
1681 }
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)
1688 while (1) {
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,
1694 in_what_stride);
1695 if (thissad < bestsad) {
1696 thissad += mvsad_err_cost(x, &this_mv, &fcenter_mv, sad_per_bit);
1697 if (thissad < bestsad) {
1698 bestsad = thissad;
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;
1702 continue;
1703 }
1704 }
1705 }
1706 break;
1707 };
1708 #endif
1709 } else if (best_address == in_what) {
1710 (*num00)++;
1711 }
1712 }
1713 return bestsad;
1714 }
1715
1716 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1717 int best_sad = INT_MAX;
1718 int this_sad;
1719 int d;
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;
1726 offset = d;
1727 }
1728 }
1729 center = offset;
1730
1731 for (d = -8; d <= 8; d += 16) {
1732 int this_pos = offset + d;
1733 // check limit
1734 if (this_pos < 0 || this_pos > bw)
1735 continue;
1736 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1737 if (this_sad < best_sad) {
1738 best_sad = this_sad;
1739 center = this_pos;
1740 }
1741 }
1742 offset = center;
1743
1744 for (d = -4; d <= 4; d += 8) {
1745 int this_pos = offset + d;
1746 // check limit
1747 if (this_pos < 0 || this_pos > bw)
1748 continue;
1749 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1750 if (this_sad < best_sad) {
1751 best_sad = this_sad;
1752 center = this_pos;
1753 }
1754 }
1755 offset = center;
1756
1757 for (d = -2; d <= 2; d += 4) {
1758 int this_pos = offset + d;
1759 // check limit
1760 if (this_pos < 0 || this_pos > bw)
1761 continue;
1762 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1763 if (this_sad < best_sad) {
1764 best_sad = this_sad;
1765 center = this_pos;
1766 }
1767 }
1768 offset = center;
1769
1770 for (d = -1; d <= 1; d += 2) {
1771 int this_pos = offset + d;
1772 // check limit
1773 if (this_pos < 0 || this_pos > bw)
1774 continue;
1775 this_sad = vp9_vector_var(&ref[this_pos], src, bwl);
1776 if (this_sad < best_sad) {
1777 best_sad = this_sad;
1778 center = this_pos;
1779 }
1780 }
1781
1782 return (center - (bw >> 1));
1783 }
1784
1785 static const MV search_pos[4] = {
1786 {-1, 0}, {0, -1}, {0, 1}, {1, 0},
1787 };
1788
1789 unsigned int vp9_int_pro_motion_estimation(const VP9_COMP *cpi, MACROBLOCK *x,
1790 BLOCK_SIZE bsize,
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]);
1799 int idx;
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];
1809 MV this_mv;
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]);
1813
1814 if (scaled_ref_frame) {
1815 int i;
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);
1822 }
1823
1824 #if CONFIG_VP9_HIGHBITDEPTH
1825 {
1826 unsigned int this_sad;
1827 tmp_mv->row = 0;
1828 tmp_mv->col = 0;
1829 this_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1830 xd->plane[0].pre[0].buf, ref_stride);
1831
1832 if (scaled_ref_frame) {
1833 int i;
1834 for (i = 0; i < MAX_MB_PLANE; i++)
1835 xd->plane[i].pre[0] = backup_yv12[i];
1836 }
1837 return this_sad;
1838 }
1839 #endif
1840
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);
1845 ref_buf += 16;
1846 }
1847
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;
1852 }
1853
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);
1858 }
1859
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;
1864 }
1865
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]);
1869
1870 this_mv = *tmp_mv;
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);
1874
1875 {
1876 const uint8_t * const pos[4] = {
1877 ref_buf - ref_stride,
1878 ref_buf - 1,
1879 ref_buf + 1,
1880 ref_buf + ref_stride,
1881 };
1882
1883 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1884 }
1885
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;
1891 }
1892 }
1893
1894 if (this_sad[0] < this_sad[3])
1895 this_mv.row -= 1;
1896 else
1897 this_mv.row += 1;
1898
1899 if (this_sad[1] < this_sad[2])
1900 this_mv.col -= 1;
1901 else
1902 this_mv.col += 1;
1903
1904 ref_buf = xd->plane[0].pre[0].buf + this_mv.row * ref_stride + this_mv.col;
1905
1906 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride,
1907 ref_buf, ref_stride);
1908 if (best_sad > tmp_sad) {
1909 *tmp_mv = this_mv;
1910 best_sad = tmp_sad;
1911 }
1912
1913 tmp_mv->row *= 8;
1914 tmp_mv->col *= 8;
1915
1916 if (scaled_ref_frame) {
1917 int i;
1918 for (i = 0; i < MAX_MB_PLANE; i++)
1919 xd->plane[i].pre[0] = backup_yv12[i];
1920 }
1921
1922 return best_sad;
1923 }
1924
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
1927 refining search */
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,
1931 int *cost_list,
1932 const vp9_variance_fn_ptr_t *fn_ptr,
1933 const MV *ref_mv, MV *dst_mv) {
1934 MV temp_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,
1938 fn_ptr, ref_mv);
1939 if (bestsme < INT_MAX)
1940 bestsme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1941 *dst_mv = temp_mv;
1942
1943 // If there won't be more n-step search, check to see if refining search is
1944 // needed.
1945 if (n > further_steps)
1946 do_refine = 0;
1947
1948 while (n < further_steps) {
1949 ++n;
1950
1951 if (num00) {
1952 num00--;
1953 } else {
1954 thissme = cpi->diamond_search_sad(x, &cpi->ss_cfg, mvp_full, &temp_mv,
1955 step_param + n, sadpb, &num00,
1956 fn_ptr, ref_mv);
1957 if (thissme < INT_MAX)
1958 thissme = vp9_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
1959
1960 // check to see if refining search is needed.
1961 if (num00 > further_steps - n)
1962 do_refine = 0;
1963
1964 if (thissme < bestsme) {
1965 bestsme = thissme;
1966 *dst_mv = temp_mv;
1967 }
1968 }
1969 }
1970
1971 // final 1-away diamond refining search
1972 if (do_refine) {
1973 const int search_range = 8;
1974 MV best_mv = *dst_mv;
1975 thissme = vp9_refining_search_sad(x, &best_mv, sadpb, search_range,
1976 fn_ptr, ref_mv);
1977 if (thissme < INT_MAX)
1978 thissme = vp9_get_mvpred_var(x, &best_mv, ref_mv, fn_ptr, 1);
1979 if (thissme < bestsme) {
1980 bestsme = thissme;
1981 *dst_mv = best_mv;
1982 }
1983 }
1984
1985 // Return cost list.
1986 if (cost_list) {
1987 calc_int_cost_list(x, ref_mv, sadpb, fn_ptr, dst_mv, cost_list);
1988 }
1989 return bestsme;
1990 }
1991
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) {
1996 int r, c;
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);
2008 *best_mv = *ref_mv;
2009
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) {
2017 best_sad = sad;
2018 *best_mv = mv;
2019 }
2020 }
2021 }
2022 return best_sad;
2023 }
2024
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) {
2029 int r;
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);
2041 *best_mv = *ref_mv;
2042
2043 for (r = row_min; r < row_max; ++r) {
2044 int c = col_min;
2045 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2046
2047 if (fn_ptr->sdx3f != NULL) {
2048 while ((c + 2) < col_max) {
2049 int i;
2050 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2051
2052 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2053 sads);
2054
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) {
2061 best_sad = sad;
2062 *best_mv = mv;
2063 }
2064 }
2065 ++check_here;
2066 ++c;
2067 }
2068 }
2069 }
2070
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) {
2078 best_sad = sad;
2079 *best_mv = mv;
2080 }
2081 }
2082 ++check_here;
2083 ++c;
2084 }
2085 }
2086
2087 return best_sad;
2088 }
2089
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) {
2094 int r;
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);
2106 *best_mv = *ref_mv;
2107
2108 for (r = row_min; r < row_max; ++r) {
2109 int c = col_min;
2110 const uint8_t *check_here = &in_what->buf[r * in_what->stride + c];
2111
2112 if (fn_ptr->sdx8f != NULL) {
2113 while ((c + 7) < col_max) {
2114 int i;
2115 DECLARE_ALIGNED(16, uint32_t, sads[8]);
2116
2117 fn_ptr->sdx8f(what->buf, what->stride, check_here, in_what->stride,
2118 sads);
2119
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) {
2126 best_sad = sad;
2127 *best_mv = mv;
2128 }
2129 }
2130 ++check_here;
2131 ++c;
2132 }
2133 }
2134 }
2135
2136 if (fn_ptr->sdx3f != NULL) {
2137 while ((c + 2) < col_max) {
2138 int i;
2139 DECLARE_ALIGNED(16, uint32_t, sads[3]);
2140
2141 fn_ptr->sdx3f(what->buf, what->stride, check_here, in_what->stride,
2142 sads);
2143
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) {
2150 best_sad = sad;
2151 *best_mv = mv;
2152 }
2153 }
2154 ++check_here;
2155 ++c;
2156 }
2157 }
2158 }
2159
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) {
2167 best_sad = sad;
2168 *best_mv = mv;
2169 }
2170 }
2171 ++check_here;
2172 ++c;
2173 }
2174 }
2175
2176 return best_sad;
2177 }
2178
2179 int vp9_refining_search_sad(const MACROBLOCK *x,
2180 MV *ref_mv, int error_per_bit,
2181 int search_range,
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,
2191 in_what->stride) +
2192 mvsad_err_cost(x, ref_mv, &fcenter_mv, error_per_bit);
2193 int i, j;
2194
2195 for (i = 0; i < search_range; i++) {
2196 int best_site = -1;
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);
2201
2202 if (all_in) {
2203 unsigned int sads[4];
2204 const uint8_t *const positions[4] = {
2205 best_address - in_what->stride,
2206 best_address - 1,
2207 best_address + 1,
2208 best_address + in_what->stride
2209 };
2210
2211 fn_ptr->sdx4df(what->buf, what->stride, positions, in_what->stride, sads);
2212
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) {
2219 best_sad = sads[j];
2220 best_site = j;
2221 }
2222 }
2223 }
2224 } else {
2225 for (j = 0; j < 4; ++j) {
2226 const MV mv = {ref_mv->row + neighbors[j].row,
2227 ref_mv->col + neighbors[j].col};
2228
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),
2232 in_what->stride);
2233 if (sad < best_sad) {
2234 sad += mvsad_err_cost(x, &mv, &fcenter_mv, error_per_bit);
2235 if (sad < best_sad) {
2236 best_sad = sad;
2237 best_site = j;
2238 }
2239 }
2240 }
2241 }
2242 }
2243
2244 if (best_site == -1) {
2245 break;
2246 } else {
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);
2250 }
2251 }
2252
2253 return best_sad;
2254 }
2255
2256 // This function is called when we do joint motion search in comp_inter_inter
2257 // mode.
2258 int vp9_refining_search_8p_c(const MACROBLOCK *x,
2259 MV *ref_mv, int error_per_bit,
2260 int search_range,
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);
2273 int i, j;
2274
2275 for (i = 0; i < search_range; ++i) {
2276 int best_site = -1;
2277
2278 for (j = 0; j < 8; ++j) {
2279 const MV mv = {ref_mv->row + neighbors[j].row,
2280 ref_mv->col + neighbors[j].col};
2281
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) {
2288 best_sad = sad;
2289 best_site = j;
2290 }
2291 }
2292 }
2293 }
2294
2295 if (best_site == -1) {
2296 break;
2297 } else {
2298 ref_mv->row += neighbors[best_site].row;
2299 ref_mv->col += neighbors[best_site].col;
2300 }
2301 }
2302 return best_sad;
2303 }
2304
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,
2308 int *cost_list,
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];
2314 int var = 0;
2315 if (cost_list) {
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;
2321 }
2322
2323 switch (method) {
2324 case FAST_DIAMOND:
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);
2327 break;
2328 case FAST_HEX:
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);
2331 break;
2332 case HEX:
2333 var = vp9_hex_search(x, mvp_full, step_param, error_per_bit, 1,
2334 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2335 break;
2336 case SQUARE:
2337 var = vp9_square_search(x, mvp_full, step_param, error_per_bit, 1,
2338 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2339 break;
2340 case BIGDIA:
2341 var = vp9_bigdia_search(x, mvp_full, step_param, error_per_bit, 1,
2342 cost_list, fn_ptr, 1, ref_mv, tmp_mv);
2343 break;
2344 case NSTEP:
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);
2348 break;
2349 default:
2350 assert(0 && "Invalid search method.");
2351 }
2352
2353 if (method != NSTEP && rd && var < var_max)
2354 var = vp9_get_mvpred_var(x, tmp_mv, ref_mv, fn_ptr, 1);
2355
2356 return var;
2357 }
2358