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.
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_config.h"
19 #include "vp8/common/findnearmv.h"
22 static int mv_ref_ct [31] [4] [2];
23 static int mv_mode_cts [4] [2];
26 int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight)
28 // MV costing is based on the distribution of vectors in the previous frame and as such will tend to
29 // over state the cost of vectors. In addition coding a new vector can have a knock on effect on the
30 // cost of subsequent vectors and the quality of prediction from NEAR and NEAREST for subsequent blocks.
31 // The "Weight" parameter allows, to a limited extent, for some account to be taken of these factors.
32 return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] + mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1]) * Weight) >> 7;
35 static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit)
37 // Ignore mv costing if mvcost is NULL
39 return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] +
40 mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1])
41 * error_per_bit + 128) >> 8;
45 static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit)
47 // Calculate sad error cost on full pixel basis.
48 // Ignore mv costing if mvsadcost is NULL
50 return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] +
51 mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)])
52 * error_per_bit + 128) >> 8;
56 void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride)
59 int search_site_count = 0;
62 // Generate offsets for 4 search sites per step.
64 x->ss[search_site_count].mv.col = 0;
65 x->ss[search_site_count].mv.row = 0;
66 x->ss[search_site_count].offset = 0;
72 // Compute offsets for search sites.
73 x->ss[search_site_count].mv.col = 0;
74 x->ss[search_site_count].mv.row = -Len;
75 x->ss[search_site_count].offset = -Len * stride;
78 // Compute offsets for search sites.
79 x->ss[search_site_count].mv.col = 0;
80 x->ss[search_site_count].mv.row = Len;
81 x->ss[search_site_count].offset = Len * stride;
84 // Compute offsets for search sites.
85 x->ss[search_site_count].mv.col = -Len;
86 x->ss[search_site_count].mv.row = 0;
87 x->ss[search_site_count].offset = -Len;
90 // Compute offsets for search sites.
91 x->ss[search_site_count].mv.col = Len;
92 x->ss[search_site_count].mv.row = 0;
93 x->ss[search_site_count].offset = Len;
100 x->ss_count = search_site_count;
101 x->searches_per_step = 4;
104 void vp8_init3smotion_compensation(MACROBLOCK *x, int stride)
107 int search_site_count = 0;
109 // Generate offsets for 8 search sites per step.
110 Len = MAX_FIRST_STEP;
111 x->ss[search_site_count].mv.col = 0;
112 x->ss[search_site_count].mv.row = 0;
113 x->ss[search_site_count].offset = 0;
119 // Compute offsets for search sites.
120 x->ss[search_site_count].mv.col = 0;
121 x->ss[search_site_count].mv.row = -Len;
122 x->ss[search_site_count].offset = -Len * stride;
125 // Compute offsets for search sites.
126 x->ss[search_site_count].mv.col = 0;
127 x->ss[search_site_count].mv.row = Len;
128 x->ss[search_site_count].offset = Len * stride;
131 // Compute offsets for search sites.
132 x->ss[search_site_count].mv.col = -Len;
133 x->ss[search_site_count].mv.row = 0;
134 x->ss[search_site_count].offset = -Len;
137 // Compute offsets for search sites.
138 x->ss[search_site_count].mv.col = Len;
139 x->ss[search_site_count].mv.row = 0;
140 x->ss[search_site_count].offset = Len;
143 // Compute offsets for search sites.
144 x->ss[search_site_count].mv.col = -Len;
145 x->ss[search_site_count].mv.row = -Len;
146 x->ss[search_site_count].offset = -Len * stride - Len;
149 // Compute offsets for search sites.
150 x->ss[search_site_count].mv.col = Len;
151 x->ss[search_site_count].mv.row = -Len;
152 x->ss[search_site_count].offset = -Len * stride + Len;
155 // Compute offsets for search sites.
156 x->ss[search_site_count].mv.col = -Len;
157 x->ss[search_site_count].mv.row = Len;
158 x->ss[search_site_count].offset = Len * stride - Len;
161 // Compute offsets for search sites.
162 x->ss[search_site_count].mv.col = Len;
163 x->ss[search_site_count].mv.row = Len;
164 x->ss[search_site_count].offset = Len * stride + Len;
172 x->ss_count = search_site_count;
173 x->searches_per_step = 8;
177 * To avoid the penalty for crossing cache-line read, preload the reference
178 * area in a small buffer, which is aligned to make sure there won't be crossing
179 * cache-line read while reading from this buffer. This reduced the cpu
180 * cycles spent on reading ref data in sub-pixel filter functions.
181 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
182 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
183 * could reduce the area.
185 #define MVC(r,c) (mvcost ? ((mvcost[0][(r)-rr] + mvcost[1][(c) - rc]) * error_per_bit + 128 )>>8 : 0) // estimated cost of a motion vector (r,c)
186 #define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) // pointer to predictor base of a motionvector
187 #define SP(x) (((x)&3)<<1) // convert motion vector component to offset for svf calc
188 #define DIST(r,c) vfp->svf( PRE(r,c), y_stride, SP(c),SP(r), z,b->src_stride,&sse) // returns subpixel variance error function.
189 #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e;
190 #define ERR(r,c) (MVC(r,c)+DIST(r,c)) // returns distortion + motion vector cost
191 #define CHECK_BETTER(v,r,c) IFMVCV(r,c,{thismse = DIST(r,c); if((v = (MVC(r,c)+thismse)) < besterr) { besterr = v; br=r; bc=c; *distortion = thismse; *sse1 = sse; }}, v=INT_MAX;)// checks if (r,c) has better score than previous best
193 int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
194 int_mv *bestmv, int_mv *ref_mv,
196 const vp8_variance_fn_ptr_t *vfp,
197 int *mvcost[2], int *distortion,
200 unsigned char *z = (*(b->base_src) + b->src);
202 int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1;
203 int br = bestmv->as_mv.row << 2, bc = bestmv->as_mv.col << 2;
204 int tr = br, tc = bc;
205 unsigned int besterr = INT_MAX;
206 unsigned int left, right, up, down, diag;
208 unsigned int whichdir;
209 unsigned int halfiters = 4;
210 unsigned int quarteriters = 4;
213 int minc = MAX(x->mv_col_min << 2, (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1));
214 int maxc = MIN(x->mv_col_max << 2, (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1));
215 int minr = MAX(x->mv_row_min << 2, (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1));
216 int maxr = MIN(x->mv_row_max << 2, (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1));
220 int pre_stride = x->e_mbd.pre.y_stride;
221 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
224 #if ARCH_X86 || ARCH_X86_64
225 MACROBLOCKD *xd = &x->e_mbd;
226 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
228 int buf_r1, buf_r2, buf_c1, buf_c2;
230 // Clamping to avoid out-of-range data access
231 buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)?(bestmv->as_mv.row - x->mv_row_min):3;
232 buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)?(x->mv_row_max - bestmv->as_mv.row):3;
233 buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)?(bestmv->as_mv.col - x->mv_col_min):3;
234 buf_c2 = ((bestmv->as_mv.col + 3) > x->mv_col_max)?(x->mv_col_max - bestmv->as_mv.col):3;
237 /* Copy to intermediate buffer before searching. */
238 vfp->copymem(y0 - buf_c1 - pre_stride*buf_r1, pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2);
239 y = xd->y_buf + y_stride*buf_r1 +buf_c1;
241 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
242 y_stride = pre_stride;
245 offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col;
248 bestmv->as_mv.row <<= 3;
249 bestmv->as_mv.col <<= 3;
251 // calculate central point error
252 besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1);
253 *distortion = besterr;
254 besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
256 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
260 CHECK_BETTER(left, tr, tc - 2);
261 CHECK_BETTER(right, tr, tc + 2);
262 CHECK_BETTER(up, tr - 2, tc);
263 CHECK_BETTER(down, tr + 2, tc);
265 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
270 CHECK_BETTER(diag, tr - 2, tc - 2);
273 CHECK_BETTER(diag, tr - 2, tc + 2);
276 CHECK_BETTER(diag, tr + 2, tc - 2);
279 CHECK_BETTER(diag, tr + 2, tc + 2);
283 // no reason to check the same one again.
284 if (tr == br && tc == bc)
291 // TODO: Each subsequent iteration checks at least one point in common with the last iteration could be 2 ( if diag selected)
293 while (--quarteriters)
295 CHECK_BETTER(left, tr, tc - 1);
296 CHECK_BETTER(right, tr, tc + 1);
297 CHECK_BETTER(up, tr - 1, tc);
298 CHECK_BETTER(down, tr + 1, tc);
300 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
305 CHECK_BETTER(diag, tr - 1, tc - 1);
308 CHECK_BETTER(diag, tr - 1, tc + 1);
311 CHECK_BETTER(diag, tr + 1, tc - 1);
314 CHECK_BETTER(diag, tr + 1, tc + 1);
318 // no reason to check the same one again.
319 if (tr == br && tc == bc)
326 bestmv->as_mv.row = br << 1;
327 bestmv->as_mv.col = bc << 1;
329 if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) ||
330 (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3)))
343 int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
344 int_mv *bestmv, int_mv *ref_mv,
346 const vp8_variance_fn_ptr_t *vfp,
347 int *mvcost[2], int *distortion,
350 int bestmse = INT_MAX;
353 unsigned char *z = (*(b->base_src) + b->src);
354 int left, right, up, down, diag;
359 int pre_stride = x->e_mbd.pre.y_stride;
360 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
362 #if ARCH_X86 || ARCH_X86_64
363 MACROBLOCKD *xd = &x->e_mbd;
364 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
368 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
369 vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
370 y = xd->y_buf + y_stride + 1;
372 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
373 y_stride = pre_stride;
377 bestmv->as_mv.row <<= 3;
378 bestmv->as_mv.col <<= 3;
381 // calculate central point error
382 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
383 *distortion = bestmse;
384 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
386 // go left then right and check error
387 this_mv.as_mv.row = startmv.as_mv.row;
388 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
389 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
390 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
396 *distortion = thismse;
400 this_mv.as_mv.col += 8;
401 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
402 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
408 *distortion = thismse;
412 // go up then down and check error
413 this_mv.as_mv.col = startmv.as_mv.col;
414 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
415 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
416 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
422 *distortion = thismse;
426 this_mv.as_mv.row += 8;
427 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
428 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
434 *distortion = thismse;
439 // now check 1 more diagonal
440 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
441 //for(whichdir =0;whichdir<4;whichdir++)
448 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
449 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
450 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
453 this_mv.as_mv.col += 4;
454 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
455 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
458 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
459 this_mv.as_mv.row += 4;
460 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
464 this_mv.as_mv.col += 4;
465 this_mv.as_mv.row += 4;
466 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
470 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
476 *distortion = thismse;
483 // time to check quarter pels.
484 if (bestmv->as_mv.row < startmv.as_mv.row)
487 if (bestmv->as_mv.col < startmv.as_mv.col)
494 // go left then right and check error
495 this_mv.as_mv.row = startmv.as_mv.row;
497 if (startmv.as_mv.col & 7)
499 this_mv.as_mv.col = startmv.as_mv.col - 2;
500 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
504 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
505 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
508 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
514 *distortion = thismse;
518 this_mv.as_mv.col += 4;
519 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
520 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
526 *distortion = thismse;
530 // go up then down and check error
531 this_mv.as_mv.col = startmv.as_mv.col;
533 if (startmv.as_mv.row & 7)
535 this_mv.as_mv.row = startmv.as_mv.row - 2;
536 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
540 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
541 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
544 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
550 *distortion = thismse;
554 this_mv.as_mv.row += 4;
555 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
556 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
562 *distortion = thismse;
567 // now check 1 more diagonal
568 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
570 // for(whichdir=0;whichdir<4;whichdir++)
578 if (startmv.as_mv.row & 7)
580 this_mv.as_mv.row -= 2;
582 if (startmv.as_mv.col & 7)
584 this_mv.as_mv.col -= 2;
585 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
589 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
590 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);;
595 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
597 if (startmv.as_mv.col & 7)
599 this_mv.as_mv.col -= 2;
600 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
604 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
605 thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse);
611 this_mv.as_mv.col += 2;
613 if (startmv.as_mv.row & 7)
615 this_mv.as_mv.row -= 2;
616 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
620 this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6;
621 thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse);
626 this_mv.as_mv.row += 2;
628 if (startmv.as_mv.col & 7)
630 this_mv.as_mv.col -= 2;
631 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
635 this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6;
636 thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
641 this_mv.as_mv.col += 2;
642 this_mv.as_mv.row += 2;
643 thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse);
647 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
653 *distortion = thismse;
660 int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
661 int_mv *bestmv, int_mv *ref_mv,
663 const vp8_variance_fn_ptr_t *vfp,
664 int *mvcost[2], int *distortion,
667 int bestmse = INT_MAX;
670 unsigned char *z = (*(b->base_src) + b->src);
671 int left, right, up, down, diag;
676 int pre_stride = x->e_mbd.pre.y_stride;
677 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
679 #if ARCH_X86 || ARCH_X86_64
680 MACROBLOCKD *xd = &x->e_mbd;
681 unsigned char *y0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
685 /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */
686 vfp->copymem(y0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18);
687 y = xd->y_buf + y_stride + 1;
689 unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col;
690 y_stride = pre_stride;
694 bestmv->as_mv.row <<= 3;
695 bestmv->as_mv.col <<= 3;
698 // calculate central point error
699 bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1);
700 *distortion = bestmse;
701 bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit);
703 // go left then right and check error
704 this_mv.as_mv.row = startmv.as_mv.row;
705 this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4);
706 thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse);
707 left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
713 *distortion = thismse;
717 this_mv.as_mv.col += 8;
718 thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse);
719 right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
725 *distortion = thismse;
729 // go up then down and check error
730 this_mv.as_mv.col = startmv.as_mv.col;
731 this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4);
732 thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse);
733 up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
739 *distortion = thismse;
743 this_mv.as_mv.row += 8;
744 thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse);
745 down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
751 *distortion = thismse;
755 // now check 1 more diagonal -
756 whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2);
762 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
763 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
764 thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse);
767 this_mv.as_mv.col += 4;
768 this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4;
769 thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse);
772 this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4;
773 this_mv.as_mv.row += 4;
774 thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse);
778 this_mv.as_mv.col += 4;
779 this_mv.as_mv.row += 4;
780 thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse);
784 diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit);
790 *distortion = thismse;
797 #define CHECK_BOUNDS(range) \
800 all_in &= ((br-range) >= x->mv_row_min);\
801 all_in &= ((br+range) <= x->mv_row_max);\
802 all_in &= ((bc-range) >= x->mv_col_min);\
803 all_in &= ((bc+range) <= x->mv_col_max);\
806 #define CHECK_POINT \
808 if (this_mv.as_mv.col < x->mv_col_min) continue;\
809 if (this_mv.as_mv.col > x->mv_col_max) continue;\
810 if (this_mv.as_mv.row < x->mv_row_min) continue;\
811 if (this_mv.as_mv.row > x->mv_row_max) continue;\
814 #define CHECK_BETTER \
816 if (thissad < bestsad)\
818 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\
819 if (thissad < bestsad)\
827 static const MV next_chkpts[6][3] =
829 {{ -2, 0}, { -1, -2}, {1, -2}},
830 {{ -1, -2}, {1, -2}, {2, 0}},
831 {{1, -2}, {2, 0}, {1, 2}},
832 {{2, 0}, {1, 2}, { -1, 2}},
833 {{1, 2}, { -1, 2}, { -2, 0}},
834 {{ -1, 2}, { -2, 0}, { -1, -2}}
846 const vp8_variance_fn_ptr_t *vfp,
852 MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ;
853 MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ;
856 unsigned char *what = (*(b->base_src) + b->src);
857 int what_stride = b->src_stride;
858 int pre_stride = x->e_mbd.pre.y_stride;
859 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
861 int in_what_stride = pre_stride;
864 unsigned int bestsad = 0x7fffffff;
865 unsigned int thissad;
866 unsigned char *base_offset;
867 unsigned char *this_offset;
875 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
876 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
878 // adjust ref_mv to make sure it is within MV range
879 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
880 br = ref_mv->as_mv.row;
881 bc = ref_mv->as_mv.col;
883 // Work out the start point for the search
884 base_offset = (unsigned char *)(base_pre + d->offset);
885 this_offset = base_offset + (br * (pre_stride)) + bc;
886 this_mv.as_mv.row = br;
887 this_mv.as_mv.col = bc;
888 bestsad = vfp->sdf( what, what_stride, this_offset,
889 in_what_stride, 0x7fffffff)
890 + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);
892 #if CONFIG_MULTI_RES_ENCODING
893 /* Lower search range based on prediction info */
894 if (search_param >= 6) goto cal_neighbors;
895 else if (search_param >= 5) hex_range = 4;
896 else if (search_param >= 4) hex_range = 6;
897 else if (search_param >= 3) hex_range = 15;
898 else if (search_param >= 2) hex_range = 31;
899 else if (search_param >= 1) hex_range = 63;
910 for (i = 0; i < 6; i++)
912 this_mv.as_mv.row = br + hex[i].row;
913 this_mv.as_mv.col = bc + hex[i].col;
914 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
915 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
920 for (i = 0; i < 6; i++)
922 this_mv.as_mv.row = br + hex[i].row;
923 this_mv.as_mv.col = bc + hex[i].col;
925 this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col;
926 thissad=vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
935 br += hex[best_site].row;
936 bc += hex[best_site].col;
940 for (j = 1; j < hex_range; j++)
947 for (i = 0; i < 3; i++)
949 this_mv.as_mv.row = br + next_chkpts[k][i].row;
950 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
951 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
952 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
957 for (i = 0; i < 3; i++)
959 this_mv.as_mv.row = br + next_chkpts[k][i].row;
960 this_mv.as_mv.col = bc + next_chkpts[k][i].col;
962 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
963 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
972 br += next_chkpts[k][best_site].row;
973 bc += next_chkpts[k][best_site].col;
975 if (k >= 12) k -= 12;
976 else if (k >= 6) k -= 6;
980 // check 4 1-away neighbors
982 for (j = 0; j < dia_range; j++)
989 for (i = 0; i < 4; i++)
991 this_mv.as_mv.row = br + neighbors[i].row;
992 this_mv.as_mv.col = bc + neighbors[i].col;
993 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
994 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
999 for (i = 0; i < 4; i++)
1001 this_mv.as_mv.row = br + neighbors[i].row;
1002 this_mv.as_mv.col = bc + neighbors[i].col;
1004 this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col;
1005 thissad = vfp->sdf( what, what_stride, this_offset, in_what_stride, bestsad);
1010 if (best_site == -1)
1014 br += neighbors[best_site].row;
1015 bc += neighbors[best_site].col;
1019 best_mv->as_mv.row = br;
1020 best_mv->as_mv.col = bc;
1028 int vp8_diamond_search_sad_c
1038 vp8_variance_fn_ptr_t *fn_ptr,
1045 unsigned char *what = (*(b->base_src) + b->src);
1046 int what_stride = b->src_stride;
1047 unsigned char *in_what;
1048 int pre_stride = x->e_mbd.pre.y_stride;
1049 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1050 int in_what_stride = pre_stride;
1051 unsigned char *best_address;
1056 int bestsad = INT_MAX;
1062 int this_row_offset;
1063 int this_col_offset;
1066 unsigned char *check_here;
1069 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1071 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1072 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1074 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1075 ref_row = ref_mv->as_mv.row;
1076 ref_col = ref_mv->as_mv.col;
1078 best_mv->as_mv.row = ref_row;
1079 best_mv->as_mv.col = ref_col;
1081 // Work out the start point for the search
1082 in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col);
1083 best_address = in_what;
1085 // Check the starting position
1086 bestsad = fn_ptr->sdf(what, what_stride, in_what,
1087 in_what_stride, 0x7fffffff)
1088 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1090 // search_param determines the length of the initial step and hence the number of iterations
1091 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1092 ss = &x->ss[search_param * x->searches_per_step];
1093 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1097 for (step = 0; step < tot_steps ; step++)
1099 for (j = 0 ; j < x->searches_per_step ; j++)
1101 // Trap illegal vectors
1102 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1103 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1105 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1106 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1109 check_here = ss[i].offset + best_address;
1110 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1112 if (thissad < bestsad)
1114 this_mv.as_mv.row = this_row_offset;
1115 this_mv.as_mv.col = this_col_offset;
1116 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1117 mvsadcost, sad_per_bit);
1119 if (thissad < bestsad)
1130 if (best_site != last_site)
1132 best_mv->as_mv.row += ss[best_site].mv.row;
1133 best_mv->as_mv.col += ss[best_site].mv.col;
1134 best_address += ss[best_site].offset;
1135 last_site = best_site;
1137 else if (best_address == in_what)
1141 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1142 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1144 if (bestsad == INT_MAX)
1147 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1148 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1151 int vp8_diamond_search_sadx4
1161 vp8_variance_fn_ptr_t *fn_ptr,
1168 unsigned char *what = (*(b->base_src) + b->src);
1169 int what_stride = b->src_stride;
1170 unsigned char *in_what;
1171 int pre_stride = x->e_mbd.pre.y_stride;
1172 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1173 int in_what_stride = pre_stride;
1174 unsigned char *best_address;
1179 unsigned int bestsad = UINT_MAX;
1185 int this_row_offset;
1186 int this_col_offset;
1189 unsigned char *check_here;
1190 unsigned int thissad;
1192 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1194 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1195 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1197 vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1198 ref_row = ref_mv->as_mv.row;
1199 ref_col = ref_mv->as_mv.col;
1201 best_mv->as_mv.row = ref_row;
1202 best_mv->as_mv.col = ref_col;
1204 // Work out the start point for the search
1205 in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col);
1206 best_address = in_what;
1208 // Check the starting position
1209 bestsad = fn_ptr->sdf(what, what_stride,
1210 in_what, in_what_stride, 0x7fffffff)
1211 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1213 // search_param determines the length of the initial step and hence the number of iterations
1214 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc.
1215 ss = &x->ss[search_param * x->searches_per_step];
1216 tot_steps = (x->ss_count / x->searches_per_step) - search_param;
1220 for (step = 0; step < tot_steps ; step++)
1224 // To know if all neighbor points are within the bounds, 4 bounds checking are enough instead of
1225 // checking 4 bounds for each points.
1226 all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min);
1227 all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max);
1228 all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min);
1229 all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max);
1233 unsigned int sad_array[4];
1235 for (j = 0 ; j < x->searches_per_step ; j += 4)
1237 unsigned char *block_offset[4];
1239 for (t = 0; t < 4; t++)
1240 block_offset[t] = ss[i+t].offset + best_address;
1242 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1244 for (t = 0; t < 4; t++, i++)
1246 if (sad_array[t] < bestsad)
1248 this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row;
1249 this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col;
1250 sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv,
1251 mvsadcost, sad_per_bit);
1253 if (sad_array[t] < bestsad)
1255 bestsad = sad_array[t];
1264 for (j = 0 ; j < x->searches_per_step ; j++)
1266 // Trap illegal vectors
1267 this_row_offset = best_mv->as_mv.row + ss[i].mv.row;
1268 this_col_offset = best_mv->as_mv.col + ss[i].mv.col;
1270 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1271 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1273 check_here = ss[i].offset + best_address;
1274 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1276 if (thissad < bestsad)
1278 this_mv.as_mv.row = this_row_offset;
1279 this_mv.as_mv.col = this_col_offset;
1280 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1281 mvsadcost, sad_per_bit);
1283 if (thissad < bestsad)
1294 if (best_site != last_site)
1296 best_mv->as_mv.row += ss[best_site].mv.row;
1297 best_mv->as_mv.col += ss[best_site].mv.col;
1298 best_address += ss[best_site].offset;
1299 last_site = best_site;
1301 else if (best_address == in_what)
1305 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1306 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1308 if (bestsad == INT_MAX)
1311 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1312 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1315 int vp8_full_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1316 int sad_per_bit, int distance,
1317 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1320 unsigned char *what = (*(b->base_src) + b->src);
1321 int what_stride = b->src_stride;
1322 unsigned char *in_what;
1323 int pre_stride = x->e_mbd.pre.y_stride;
1324 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1325 int in_what_stride = pre_stride;
1326 int mv_stride = pre_stride;
1327 unsigned char *bestaddress;
1328 int_mv *best_mv = &d->bmi.mv;
1330 int bestsad = INT_MAX;
1333 unsigned char *check_here;
1336 int ref_row = ref_mv->as_mv.row;
1337 int ref_col = ref_mv->as_mv.col;
1339 int row_min = ref_row - distance;
1340 int row_max = ref_row + distance;
1341 int col_min = ref_col - distance;
1342 int col_max = ref_col + distance;
1344 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1346 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1347 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1349 // Work out the mid point for the search
1350 in_what = base_pre + d->offset;
1351 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1353 best_mv->as_mv.row = ref_row;
1354 best_mv->as_mv.col = ref_col;
1356 // Baseline value at the centre
1357 bestsad = fn_ptr->sdf(what, what_stride, bestaddress,
1358 in_what_stride, 0x7fffffff)
1359 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1361 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1362 if (col_min < x->mv_col_min)
1363 col_min = x->mv_col_min;
1365 if (col_max > x->mv_col_max)
1366 col_max = x->mv_col_max;
1368 if (row_min < x->mv_row_min)
1369 row_min = x->mv_row_min;
1371 if (row_max > x->mv_row_max)
1372 row_max = x->mv_row_max;
1374 for (r = row_min; r < row_max ; r++)
1376 this_mv.as_mv.row = r;
1377 check_here = r * mv_stride + in_what + col_min;
1379 for (c = col_min; c < col_max; c++)
1381 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1383 this_mv.as_mv.col = c;
1384 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1385 mvsadcost, sad_per_bit);
1387 if (thissad < bestsad)
1390 best_mv->as_mv.row = r;
1391 best_mv->as_mv.col = c;
1392 bestaddress = check_here;
1399 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1400 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1402 if (bestsad < INT_MAX)
1403 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1404 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1409 int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1410 int sad_per_bit, int distance,
1411 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1414 unsigned char *what = (*(b->base_src) + b->src);
1415 int what_stride = b->src_stride;
1416 unsigned char *in_what;
1417 int pre_stride = x->e_mbd.pre.y_stride;
1418 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1419 int in_what_stride = pre_stride;
1420 int mv_stride = pre_stride;
1421 unsigned char *bestaddress;
1422 int_mv *best_mv = &d->bmi.mv;
1424 unsigned int bestsad = UINT_MAX;
1427 unsigned char *check_here;
1428 unsigned int thissad;
1430 int ref_row = ref_mv->as_mv.row;
1431 int ref_col = ref_mv->as_mv.col;
1433 int row_min = ref_row - distance;
1434 int row_max = ref_row + distance;
1435 int col_min = ref_col - distance;
1436 int col_max = ref_col + distance;
1438 unsigned int sad_array[3];
1440 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1442 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1443 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1445 // Work out the mid point for the search
1446 in_what = base_pre + d->offset;
1447 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1449 best_mv->as_mv.row = ref_row;
1450 best_mv->as_mv.col = ref_col;
1452 // Baseline value at the centre
1453 bestsad = fn_ptr->sdf(what, what_stride,
1454 bestaddress, in_what_stride, 0x7fffffff)
1455 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1457 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1458 if (col_min < x->mv_col_min)
1459 col_min = x->mv_col_min;
1461 if (col_max > x->mv_col_max)
1462 col_max = x->mv_col_max;
1464 if (row_min < x->mv_row_min)
1465 row_min = x->mv_row_min;
1467 if (row_max > x->mv_row_max)
1468 row_max = x->mv_row_max;
1470 for (r = row_min; r < row_max ; r++)
1472 this_mv.as_mv.row = r;
1473 check_here = r * mv_stride + in_what + col_min;
1476 while ((c + 2) < col_max)
1480 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1482 for (i = 0; i < 3; i++)
1484 thissad = sad_array[i];
1486 if (thissad < bestsad)
1488 this_mv.as_mv.col = c;
1489 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1490 mvsadcost, sad_per_bit);
1492 if (thissad < bestsad)
1495 best_mv->as_mv.row = r;
1496 best_mv->as_mv.col = c;
1497 bestaddress = check_here;
1508 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1510 if (thissad < bestsad)
1512 this_mv.as_mv.col = c;
1513 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1514 mvsadcost, sad_per_bit);
1516 if (thissad < bestsad)
1519 best_mv->as_mv.row = r;
1520 best_mv->as_mv.col = c;
1521 bestaddress = check_here;
1531 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1532 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1534 if (bestsad < INT_MAX)
1535 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1536 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1541 int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1542 int sad_per_bit, int distance,
1543 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1546 unsigned char *what = (*(b->base_src) + b->src);
1547 int what_stride = b->src_stride;
1548 int pre_stride = x->e_mbd.pre.y_stride;
1549 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1550 unsigned char *in_what;
1551 int in_what_stride = pre_stride;
1552 int mv_stride = pre_stride;
1553 unsigned char *bestaddress;
1554 int_mv *best_mv = &d->bmi.mv;
1556 unsigned int bestsad = UINT_MAX;
1559 unsigned char *check_here;
1560 unsigned int thissad;
1562 int ref_row = ref_mv->as_mv.row;
1563 int ref_col = ref_mv->as_mv.col;
1565 int row_min = ref_row - distance;
1566 int row_max = ref_row + distance;
1567 int col_min = ref_col - distance;
1568 int col_max = ref_col + distance;
1570 DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8);
1571 unsigned int sad_array[3];
1573 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1575 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1576 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1578 // Work out the mid point for the search
1579 in_what = base_pre + d->offset;
1580 bestaddress = in_what + (ref_row * pre_stride) + ref_col;
1582 best_mv->as_mv.row = ref_row;
1583 best_mv->as_mv.col = ref_col;
1585 // Baseline value at the centre
1586 bestsad = fn_ptr->sdf(what, what_stride,
1587 bestaddress, in_what_stride, 0x7fffffff)
1588 + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit);
1590 // Apply further limits to prevent us looking using vectors that stretch beyiond the UMV border
1591 if (col_min < x->mv_col_min)
1592 col_min = x->mv_col_min;
1594 if (col_max > x->mv_col_max)
1595 col_max = x->mv_col_max;
1597 if (row_min < x->mv_row_min)
1598 row_min = x->mv_row_min;
1600 if (row_max > x->mv_row_max)
1601 row_max = x->mv_row_max;
1603 for (r = row_min; r < row_max ; r++)
1605 this_mv.as_mv.row = r;
1606 check_here = r * mv_stride + in_what + col_min;
1609 while ((c + 7) < col_max)
1613 fn_ptr->sdx8f(what, what_stride, check_here , in_what_stride, sad_array8);
1615 for (i = 0; i < 8; i++)
1617 thissad = (unsigned int)sad_array8[i];
1619 if (thissad < bestsad)
1621 this_mv.as_mv.col = c;
1622 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1623 mvsadcost, sad_per_bit);
1625 if (thissad < bestsad)
1628 best_mv->as_mv.row = r;
1629 best_mv->as_mv.col = c;
1630 bestaddress = check_here;
1639 while ((c + 2) < col_max)
1643 fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array);
1645 for (i = 0; i < 3; i++)
1647 thissad = sad_array[i];
1649 if (thissad < bestsad)
1651 this_mv.as_mv.col = c;
1652 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1653 mvsadcost, sad_per_bit);
1655 if (thissad < bestsad)
1658 best_mv->as_mv.row = r;
1659 best_mv->as_mv.col = c;
1660 bestaddress = check_here;
1671 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1673 if (thissad < bestsad)
1675 this_mv.as_mv.col = c;
1676 thissad += mvsad_err_cost(&this_mv, &fcenter_mv,
1677 mvsadcost, sad_per_bit);
1679 if (thissad < bestsad)
1682 best_mv->as_mv.row = r;
1683 best_mv->as_mv.col = c;
1684 bestaddress = check_here;
1693 this_mv.as_mv.row = best_mv->as_mv.row << 3;
1694 this_mv.as_mv.col = best_mv->as_mv.col << 3;
1696 if (bestsad < INT_MAX)
1697 return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, (unsigned int *)(&thissad))
1698 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1703 int vp8_refining_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv,
1704 int error_per_bit, int search_range,
1705 vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2],
1708 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1710 short this_row_offset, this_col_offset;
1712 int what_stride = b->src_stride;
1713 int pre_stride = x->e_mbd.pre.y_stride;
1714 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1715 int in_what_stride = pre_stride;
1716 unsigned char *what = (*(b->base_src) + b->src);
1717 unsigned char *best_address = (unsigned char *)(base_pre + d->offset +
1718 (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1719 unsigned char *check_here;
1720 unsigned int thissad;
1722 unsigned int bestsad = INT_MAX;
1724 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1727 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1728 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1730 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1732 for (i=0; i<search_range; i++)
1736 for (j = 0 ; j < 4 ; j++)
1738 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1739 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1741 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1742 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1744 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1745 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1747 if (thissad < bestsad)
1749 this_mv.as_mv.row = this_row_offset;
1750 this_mv.as_mv.col = this_col_offset;
1751 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1753 if (thissad < bestsad)
1762 if (best_site == -1)
1766 ref_mv->as_mv.row += neighbors[best_site].row;
1767 ref_mv->as_mv.col += neighbors[best_site].col;
1768 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1772 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1773 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1775 if (bestsad < INT_MAX)
1776 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1777 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1782 int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d,
1783 int_mv *ref_mv, int error_per_bit,
1784 int search_range, vp8_variance_fn_ptr_t *fn_ptr,
1785 int *mvcost[2], int_mv *center_mv)
1787 MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
1789 short this_row_offset, this_col_offset;
1791 int what_stride = b->src_stride;
1792 int pre_stride = x->e_mbd.pre.y_stride;
1793 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1794 int in_what_stride = pre_stride;
1795 unsigned char *what = (*(b->base_src) + b->src);
1796 unsigned char *best_address = (unsigned char *)(base_pre + d->offset +
1797 (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col);
1798 unsigned char *check_here;
1799 unsigned int thissad;
1801 unsigned int bestsad = INT_MAX;
1803 int *mvsadcost[2] = {x->mvsadcost[0], x->mvsadcost[1]};
1806 fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3;
1807 fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3;
1809 bestsad = fn_ptr->sdf(what, what_stride, best_address, in_what_stride, 0x7fffffff) + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit);
1811 for (i=0; i<search_range; i++)
1816 all_in &= ((ref_mv->as_mv.row - 1) > x->mv_row_min);
1817 all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max);
1818 all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min);
1819 all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max);
1823 unsigned int sad_array[4];
1824 unsigned char *block_offset[4];
1825 block_offset[0] = best_address - in_what_stride;
1826 block_offset[1] = best_address - 1;
1827 block_offset[2] = best_address + 1;
1828 block_offset[3] = best_address + in_what_stride;
1830 fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array);
1832 for (j = 0; j < 4; j++)
1834 if (sad_array[j] < bestsad)
1836 this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row;
1837 this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col;
1838 sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1840 if (sad_array[j] < bestsad)
1842 bestsad = sad_array[j];
1850 for (j = 0 ; j < 4 ; j++)
1852 this_row_offset = ref_mv->as_mv.row + neighbors[j].row;
1853 this_col_offset = ref_mv->as_mv.col + neighbors[j].col;
1855 if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) &&
1856 (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max))
1858 check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address;
1859 thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad);
1861 if (thissad < bestsad)
1863 this_mv.as_mv.row = this_row_offset;
1864 this_mv.as_mv.col = this_col_offset;
1865 thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit);
1867 if (thissad < bestsad)
1877 if (best_site == -1)
1881 ref_mv->as_mv.row += neighbors[best_site].row;
1882 ref_mv->as_mv.col += neighbors[best_site].col;
1883 best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col;
1887 this_mv.as_mv.row = ref_mv->as_mv.row << 3;
1888 this_mv.as_mv.col = ref_mv->as_mv.col << 3;
1890 if (bestsad < INT_MAX)
1891 return fn_ptr->vf(what, what_stride, best_address, in_what_stride, (unsigned int *)(&thissad))
1892 + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit);
1897 #ifdef ENTROPY_STATS
1898 void print_mode_context(void)
1900 FILE *f = fopen("modecont.c", "w");
1903 fprintf(f, "#include \"entropy.h\"\n");
1904 fprintf(f, "const int vp8_mode_contexts[6][4] =\n");
1907 for (j = 0; j < 6; j++)
1909 fprintf(f, " { // %d \n", j);
1912 for (i = 0; i < 4; i++)
1916 int count; // = mv_ref_ct[j][i][0]+mv_ref_ct[j][i][1];
1919 count = mv_mode_cts[i][0] + mv_mode_cts[i][1];
1922 overal_prob = 256 * mv_mode_cts[i][0] / count;
1926 if (overal_prob == 0)
1930 count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
1933 this_prob = 256 * mv_ref_ct[j][i][0] / count;
1940 fprintf(f, "%5d, ", this_prob);
1941 //fprintf(f,"%5d, %5d, %8d,", this_prob, overal_prob, (this_prob << 10)/overal_prob);
1942 //fprintf(f,"%8d, ", (this_prob << 10)/overal_prob);
1945 fprintf(f, " },\n");
1952 /* MV ref count ENTROPY_STATS stats code */
1953 #ifdef ENTROPY_STATS
1954 void init_mv_ref_counts()
1956 vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct));
1957 vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts));
1960 void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4])
1964 ++mv_ref_ct [ct[0]] [0] [0];
1965 ++mv_mode_cts[0][0];
1969 ++mv_ref_ct [ct[0]] [0] [1];
1970 ++mv_mode_cts[0][1];
1974 ++mv_ref_ct [ct[1]] [1] [0];
1975 ++mv_mode_cts[1][0];
1979 ++mv_ref_ct [ct[1]] [1] [1];
1980 ++mv_mode_cts[1][1];
1984 ++mv_ref_ct [ct[2]] [2] [0];
1985 ++mv_mode_cts[2][0];
1989 ++mv_ref_ct [ct[2]] [2] [1];
1990 ++mv_mode_cts[2][1];
1994 ++mv_ref_ct [ct[3]] [3] [0];
1995 ++mv_mode_cts[3][0];
1999 ++mv_ref_ct [ct[3]] [3] [1];
2000 ++mv_mode_cts[3][1];
2007 #endif/* END MV ref count ENTROPY_STATS stats code */