michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "onyx_int.h" michael@0: #include "mcomp.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vpx_config.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include "vp8/common/findnearmv.h" michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: static int mv_ref_ct [31] [4] [2]; michael@0: static int mv_mode_cts [4] [2]; michael@0: #endif michael@0: michael@0: int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight) michael@0: { michael@0: /* MV costing is based on the distribution of vectors in the previous michael@0: * frame and as such will tend to over state the cost of vectors. In michael@0: * addition coding a new vector can have a knock on effect on the cost michael@0: * of subsequent vectors and the quality of prediction from NEAR and michael@0: * NEAREST for subsequent blocks. The "Weight" parameter allows, to a michael@0: * limited extent, for some account to be taken of these factors. michael@0: */ michael@0: 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; michael@0: } michael@0: michael@0: static int mv_err_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int error_per_bit) michael@0: { michael@0: /* Ignore mv costing if mvcost is NULL */ michael@0: if (mvcost) michael@0: return ((mvcost[0][(mv->as_mv.row - ref->as_mv.row) >> 1] + michael@0: mvcost[1][(mv->as_mv.col - ref->as_mv.col) >> 1]) michael@0: * error_per_bit + 128) >> 8; michael@0: return 0; michael@0: } michael@0: michael@0: static int mvsad_err_cost(int_mv *mv, int_mv *ref, int *mvsadcost[2], int error_per_bit) michael@0: { michael@0: /* Calculate sad error cost on full pixel basis. */ michael@0: /* Ignore mv costing if mvsadcost is NULL */ michael@0: if (mvsadcost) michael@0: return ((mvsadcost[0][(mv->as_mv.row - ref->as_mv.row)] + michael@0: mvsadcost[1][(mv->as_mv.col - ref->as_mv.col)]) michael@0: * error_per_bit + 128) >> 8; michael@0: return 0; michael@0: } michael@0: michael@0: void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride) michael@0: { michael@0: int Len; michael@0: int search_site_count = 0; michael@0: michael@0: michael@0: /* Generate offsets for 4 search sites per step. */ michael@0: Len = MAX_FIRST_STEP; michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = 0; michael@0: search_site_count++; michael@0: michael@0: while (Len > 0) michael@0: { michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = -Len; michael@0: x->ss[search_site_count].offset = -Len * stride; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = Len; michael@0: x->ss[search_site_count].offset = Len * stride; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = -Len; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = -Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = Len; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = Len; michael@0: search_site_count++; michael@0: michael@0: /* Contract. */ michael@0: Len /= 2; michael@0: } michael@0: michael@0: x->ss_count = search_site_count; michael@0: x->searches_per_step = 4; michael@0: } michael@0: michael@0: void vp8_init3smotion_compensation(MACROBLOCK *x, int stride) michael@0: { michael@0: int Len; michael@0: int search_site_count = 0; michael@0: michael@0: /* Generate offsets for 8 search sites per step. */ michael@0: Len = MAX_FIRST_STEP; michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = 0; michael@0: search_site_count++; michael@0: michael@0: while (Len > 0) michael@0: { michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = -Len; michael@0: x->ss[search_site_count].offset = -Len * stride; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = 0; michael@0: x->ss[search_site_count].mv.row = Len; michael@0: x->ss[search_site_count].offset = Len * stride; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = -Len; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = -Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = Len; michael@0: x->ss[search_site_count].mv.row = 0; michael@0: x->ss[search_site_count].offset = Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = -Len; michael@0: x->ss[search_site_count].mv.row = -Len; michael@0: x->ss[search_site_count].offset = -Len * stride - Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = Len; michael@0: x->ss[search_site_count].mv.row = -Len; michael@0: x->ss[search_site_count].offset = -Len * stride + Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = -Len; michael@0: x->ss[search_site_count].mv.row = Len; michael@0: x->ss[search_site_count].offset = Len * stride - Len; michael@0: search_site_count++; michael@0: michael@0: /* Compute offsets for search sites. */ michael@0: x->ss[search_site_count].mv.col = Len; michael@0: x->ss[search_site_count].mv.row = Len; michael@0: x->ss[search_site_count].offset = Len * stride + Len; michael@0: search_site_count++; michael@0: michael@0: michael@0: /* Contract. */ michael@0: Len /= 2; michael@0: } michael@0: michael@0: x->ss_count = search_site_count; michael@0: x->searches_per_step = 8; michael@0: } michael@0: michael@0: /* michael@0: * To avoid the penalty for crossing cache-line read, preload the reference michael@0: * area in a small buffer, which is aligned to make sure there won't be crossing michael@0: * cache-line read while reading from this buffer. This reduced the cpu michael@0: * cycles spent on reading ref data in sub-pixel filter functions. michael@0: * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x michael@0: * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we michael@0: * could reduce the area. michael@0: */ michael@0: michael@0: /* estimated cost of a motion vector (r,c) */ michael@0: #define MVC(r,c) (mvcost ? ((mvcost[0][(r)-rr] + mvcost[1][(c) - rc]) * error_per_bit + 128 )>>8 : 0) michael@0: /* pointer to predictor base of a motionvector */ michael@0: #define PRE(r,c) (y + (((r)>>2) * y_stride + ((c)>>2) -(offset))) michael@0: /* convert motion vector component to offset for svf calc */ michael@0: #define SP(x) (((x)&3)<<1) michael@0: /* returns subpixel variance error function. */ michael@0: #define DIST(r,c) vfp->svf( PRE(r,c), y_stride, SP(c),SP(r), z,b->src_stride,&sse) michael@0: #define IFMVCV(r,c,s,e) if ( c >= minc && c <= maxc && r >= minr && r <= maxr) s else e; michael@0: /* returns distortion + motion vector cost */ michael@0: #define ERR(r,c) (MVC(r,c)+DIST(r,c)) michael@0: /* checks if (r,c) has better score than previous best */ michael@0: #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=UINT_MAX;) michael@0: michael@0: int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d, michael@0: int_mv *bestmv, int_mv *ref_mv, michael@0: int error_per_bit, michael@0: const vp8_variance_fn_ptr_t *vfp, michael@0: int *mvcost[2], int *distortion, michael@0: unsigned int *sse1) michael@0: { michael@0: unsigned char *z = (*(b->base_src) + b->src); michael@0: michael@0: int rr = ref_mv->as_mv.row >> 1, rc = ref_mv->as_mv.col >> 1; michael@0: int br = bestmv->as_mv.row * 4, bc = bestmv->as_mv.col * 4; michael@0: int tr = br, tc = bc; michael@0: unsigned int besterr; michael@0: unsigned int left, right, up, down, diag; michael@0: unsigned int sse; michael@0: unsigned int whichdir; michael@0: unsigned int halfiters = 4; michael@0: unsigned int quarteriters = 4; michael@0: int thismse; michael@0: michael@0: int minc = MAX(x->mv_col_min * 4, michael@0: (ref_mv->as_mv.col >> 1) - ((1 << mvlong_width) - 1)); michael@0: int maxc = MIN(x->mv_col_max * 4, michael@0: (ref_mv->as_mv.col >> 1) + ((1 << mvlong_width) - 1)); michael@0: int minr = MAX(x->mv_row_min * 4, michael@0: (ref_mv->as_mv.row >> 1) - ((1 << mvlong_width) - 1)); michael@0: int maxr = MIN(x->mv_row_max * 4, michael@0: (ref_mv->as_mv.row >> 1) + ((1 << mvlong_width) - 1)); michael@0: michael@0: int y_stride; michael@0: int offset; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: michael@0: michael@0: #if ARCH_X86 || ARCH_X86_64 michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: unsigned char *y; michael@0: int buf_r1, buf_r2, buf_c1; michael@0: michael@0: /* Clamping to avoid out-of-range data access */ michael@0: buf_r1 = ((bestmv->as_mv.row - 3) < x->mv_row_min)?(bestmv->as_mv.row - x->mv_row_min):3; michael@0: buf_r2 = ((bestmv->as_mv.row + 3) > x->mv_row_max)?(x->mv_row_max - bestmv->as_mv.row):3; michael@0: buf_c1 = ((bestmv->as_mv.col - 3) < x->mv_col_min)?(bestmv->as_mv.col - x->mv_col_min):3; michael@0: y_stride = 32; michael@0: michael@0: /* Copy to intermediate buffer before searching. */ michael@0: vfp->copymem(y_0 - buf_c1 - pre_stride*buf_r1, pre_stride, xd->y_buf, y_stride, 16+buf_r1+buf_r2); michael@0: y = xd->y_buf + y_stride*buf_r1 +buf_c1; michael@0: #else michael@0: unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: y_stride = pre_stride; michael@0: #endif michael@0: michael@0: offset = (bestmv->as_mv.row) * y_stride + bestmv->as_mv.col; michael@0: michael@0: /* central mv */ michael@0: bestmv->as_mv.row *= 8; michael@0: bestmv->as_mv.col *= 8; michael@0: michael@0: /* calculate central point error */ michael@0: besterr = vfp->vf(y, y_stride, z, b->src_stride, sse1); michael@0: *distortion = besterr; michael@0: besterr += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: /* TODO: Each subsequent iteration checks at least one point in common michael@0: * with the last iteration could be 2 ( if diag selected) michael@0: */ michael@0: while (--halfiters) michael@0: { michael@0: /* 1/2 pel */ michael@0: CHECK_BETTER(left, tr, tc - 2); michael@0: CHECK_BETTER(right, tr, tc + 2); michael@0: CHECK_BETTER(up, tr - 2, tc); michael@0: CHECK_BETTER(down, tr + 2, tc); michael@0: michael@0: whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2); michael@0: michael@0: switch (whichdir) michael@0: { michael@0: case 0: michael@0: CHECK_BETTER(diag, tr - 2, tc - 2); michael@0: break; michael@0: case 1: michael@0: CHECK_BETTER(diag, tr - 2, tc + 2); michael@0: break; michael@0: case 2: michael@0: CHECK_BETTER(diag, tr + 2, tc - 2); michael@0: break; michael@0: case 3: michael@0: CHECK_BETTER(diag, tr + 2, tc + 2); michael@0: break; michael@0: } michael@0: michael@0: /* no reason to check the same one again. */ michael@0: if (tr == br && tc == bc) michael@0: break; michael@0: michael@0: tr = br; michael@0: tc = bc; michael@0: } michael@0: michael@0: /* TODO: Each subsequent iteration checks at least one point in common michael@0: * with the last iteration could be 2 ( if diag selected) michael@0: */ michael@0: michael@0: /* 1/4 pel */ michael@0: while (--quarteriters) michael@0: { michael@0: CHECK_BETTER(left, tr, tc - 1); michael@0: CHECK_BETTER(right, tr, tc + 1); michael@0: CHECK_BETTER(up, tr - 1, tc); michael@0: CHECK_BETTER(down, tr + 1, tc); michael@0: michael@0: whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2); michael@0: michael@0: switch (whichdir) michael@0: { michael@0: case 0: michael@0: CHECK_BETTER(diag, tr - 1, tc - 1); michael@0: break; michael@0: case 1: michael@0: CHECK_BETTER(diag, tr - 1, tc + 1); michael@0: break; michael@0: case 2: michael@0: CHECK_BETTER(diag, tr + 1, tc - 1); michael@0: break; michael@0: case 3: michael@0: CHECK_BETTER(diag, tr + 1, tc + 1); michael@0: break; michael@0: } michael@0: michael@0: /* no reason to check the same one again. */ michael@0: if (tr == br && tc == bc) michael@0: break; michael@0: michael@0: tr = br; michael@0: tc = bc; michael@0: } michael@0: michael@0: bestmv->as_mv.row = br * 2; michael@0: bestmv->as_mv.col = bc * 2; michael@0: michael@0: if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) || michael@0: (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3))) michael@0: return INT_MAX; michael@0: michael@0: return besterr; michael@0: } michael@0: #undef MVC michael@0: #undef PRE michael@0: #undef SP michael@0: #undef DIST michael@0: #undef IFMVCV michael@0: #undef ERR michael@0: #undef CHECK_BETTER michael@0: michael@0: int vp8_find_best_sub_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d, michael@0: int_mv *bestmv, int_mv *ref_mv, michael@0: int error_per_bit, michael@0: const vp8_variance_fn_ptr_t *vfp, michael@0: int *mvcost[2], int *distortion, michael@0: unsigned int *sse1) michael@0: { michael@0: int bestmse = INT_MAX; michael@0: int_mv startmv; michael@0: int_mv this_mv; michael@0: unsigned char *z = (*(b->base_src) + b->src); michael@0: int left, right, up, down, diag; michael@0: unsigned int sse; michael@0: int whichdir ; michael@0: int thismse; michael@0: int y_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: michael@0: #if ARCH_X86 || ARCH_X86_64 michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: unsigned char *y; michael@0: michael@0: y_stride = 32; michael@0: /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */ michael@0: vfp->copymem(y_0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18); michael@0: y = xd->y_buf + y_stride + 1; michael@0: #else michael@0: unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: y_stride = pre_stride; michael@0: #endif michael@0: michael@0: /* central mv */ michael@0: bestmv->as_mv.row <<= 3; michael@0: bestmv->as_mv.col <<= 3; michael@0: startmv = *bestmv; michael@0: michael@0: /* calculate central point error */ michael@0: bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1); michael@0: *distortion = bestmse; michael@0: bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: /* go left then right and check error */ michael@0: this_mv.as_mv.row = startmv.as_mv.row; michael@0: this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4); michael@0: thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse); michael@0: left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (left < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = left; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.col += 8; michael@0: thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse); michael@0: right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (right < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = right; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: /* go up then down and check error */ michael@0: this_mv.as_mv.col = startmv.as_mv.col; michael@0: this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4); michael@0: thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse); michael@0: up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (up < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = up; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.row += 8; michael@0: thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse); michael@0: down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (down < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = down; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: michael@0: /* now check 1 more diagonal */ michael@0: whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2); michael@0: this_mv = startmv; michael@0: michael@0: switch (whichdir) michael@0: { michael@0: case 0: michael@0: this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4; michael@0: this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4; michael@0: thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 1: michael@0: this_mv.as_mv.col += 4; michael@0: this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4; michael@0: thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 2: michael@0: this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4; michael@0: this_mv.as_mv.row += 4; michael@0: thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 3: michael@0: default: michael@0: this_mv.as_mv.col += 4; michael@0: this_mv.as_mv.row += 4; michael@0: thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: } michael@0: michael@0: diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (diag < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = diag; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: michael@0: /* time to check quarter pels. */ michael@0: if (bestmv->as_mv.row < startmv.as_mv.row) michael@0: y -= y_stride; michael@0: michael@0: if (bestmv->as_mv.col < startmv.as_mv.col) michael@0: y--; michael@0: michael@0: startmv = *bestmv; michael@0: michael@0: michael@0: michael@0: /* go left then right and check error */ michael@0: this_mv.as_mv.row = startmv.as_mv.row; michael@0: michael@0: if (startmv.as_mv.col & 7) michael@0: { michael@0: this_mv.as_mv.col = startmv.as_mv.col - 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6; michael@0: thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: michael@0: left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (left < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = left; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.col += 4; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (right < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = right; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: /* go up then down and check error */ michael@0: this_mv.as_mv.col = startmv.as_mv.col; michael@0: michael@0: if (startmv.as_mv.row & 7) michael@0: { michael@0: this_mv.as_mv.row = startmv.as_mv.row - 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6; michael@0: thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse); michael@0: } michael@0: michael@0: up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (up < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = up; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.row += 4; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (down < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = down; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: michael@0: /* now check 1 more diagonal */ michael@0: whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2); michael@0: michael@0: this_mv = startmv; michael@0: michael@0: switch (whichdir) michael@0: { michael@0: case 0: michael@0: michael@0: if (startmv.as_mv.row & 7) michael@0: { michael@0: this_mv.as_mv.row -= 2; michael@0: michael@0: if (startmv.as_mv.col & 7) michael@0: { michael@0: this_mv.as_mv.col -= 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6; michael@0: thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse);; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6; michael@0: michael@0: if (startmv.as_mv.col & 7) michael@0: { michael@0: this_mv.as_mv.col -= 2; michael@0: thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6; michael@0: thismse = vfp->svf(y - y_stride - 1, y_stride, 6, 6, z, b->src_stride, &sse); michael@0: } michael@0: } michael@0: michael@0: break; michael@0: case 1: michael@0: this_mv.as_mv.col += 2; michael@0: michael@0: if (startmv.as_mv.row & 7) michael@0: { michael@0: this_mv.as_mv.row -= 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.row = (startmv.as_mv.row - 8) | 6; michael@0: thismse = vfp->svf(y - y_stride, y_stride, this_mv.as_mv.col & 7, 6, z, b->src_stride, &sse); michael@0: } michael@0: michael@0: break; michael@0: case 2: michael@0: this_mv.as_mv.row += 2; michael@0: michael@0: if (startmv.as_mv.col & 7) michael@0: { michael@0: this_mv.as_mv.col -= 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: else michael@0: { michael@0: this_mv.as_mv.col = (startmv.as_mv.col - 8) | 6; michael@0: thismse = vfp->svf(y - 1, y_stride, 6, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: } michael@0: michael@0: break; michael@0: case 3: michael@0: this_mv.as_mv.col += 2; michael@0: this_mv.as_mv.row += 2; michael@0: thismse = vfp->svf(y, y_stride, this_mv.as_mv.col & 7, this_mv.as_mv.row & 7, z, b->src_stride, &sse); michael@0: break; michael@0: } michael@0: michael@0: diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (diag < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = diag; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: return bestmse; michael@0: } michael@0: michael@0: int vp8_find_best_half_pixel_step(MACROBLOCK *x, BLOCK *b, BLOCKD *d, michael@0: int_mv *bestmv, int_mv *ref_mv, michael@0: int error_per_bit, michael@0: const vp8_variance_fn_ptr_t *vfp, michael@0: int *mvcost[2], int *distortion, michael@0: unsigned int *sse1) michael@0: { michael@0: int bestmse = INT_MAX; michael@0: int_mv startmv; michael@0: int_mv this_mv; michael@0: unsigned char *z = (*(b->base_src) + b->src); michael@0: int left, right, up, down, diag; michael@0: unsigned int sse; michael@0: int whichdir ; michael@0: int thismse; michael@0: int y_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: michael@0: #if ARCH_X86 || ARCH_X86_64 michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: unsigned char *y_0 = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: unsigned char *y; michael@0: michael@0: y_stride = 32; michael@0: /* Copy 18 rows x 32 cols area to intermediate buffer before searching. */ michael@0: vfp->copymem(y_0 - 1 - pre_stride, pre_stride, xd->y_buf, y_stride, 18); michael@0: y = xd->y_buf + y_stride + 1; michael@0: #else michael@0: unsigned char *y = base_pre + d->offset + (bestmv->as_mv.row) * pre_stride + bestmv->as_mv.col; michael@0: y_stride = pre_stride; michael@0: #endif michael@0: michael@0: /* central mv */ michael@0: bestmv->as_mv.row *= 8; michael@0: bestmv->as_mv.col *= 8; michael@0: startmv = *bestmv; michael@0: michael@0: /* calculate central point error */ michael@0: bestmse = vfp->vf(y, y_stride, z, b->src_stride, sse1); michael@0: *distortion = bestmse; michael@0: bestmse += mv_err_cost(bestmv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: /* go left then right and check error */ michael@0: this_mv.as_mv.row = startmv.as_mv.row; michael@0: this_mv.as_mv.col = ((startmv.as_mv.col - 8) | 4); michael@0: thismse = vfp->svf_halfpix_h(y - 1, y_stride, z, b->src_stride, &sse); michael@0: left = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (left < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = left; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.col += 8; michael@0: thismse = vfp->svf_halfpix_h(y, y_stride, z, b->src_stride, &sse); michael@0: right = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (right < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = right; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: /* go up then down and check error */ michael@0: this_mv.as_mv.col = startmv.as_mv.col; michael@0: this_mv.as_mv.row = ((startmv.as_mv.row - 8) | 4); michael@0: thismse = vfp->svf_halfpix_v(y - y_stride, y_stride, z, b->src_stride, &sse); michael@0: up = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (up < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = up; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: this_mv.as_mv.row += 8; michael@0: thismse = vfp->svf_halfpix_v(y, y_stride, z, b->src_stride, &sse); michael@0: down = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (down < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = down; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: /* now check 1 more diagonal - */ michael@0: whichdir = (left < right ? 0 : 1) + (up < down ? 0 : 2); michael@0: this_mv = startmv; michael@0: michael@0: switch (whichdir) michael@0: { michael@0: case 0: michael@0: this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4; michael@0: this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4; michael@0: thismse = vfp->svf_halfpix_hv(y - 1 - y_stride, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 1: michael@0: this_mv.as_mv.col += 4; michael@0: this_mv.as_mv.row = (this_mv.as_mv.row - 8) | 4; michael@0: thismse = vfp->svf_halfpix_hv(y - y_stride, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 2: michael@0: this_mv.as_mv.col = (this_mv.as_mv.col - 8) | 4; michael@0: this_mv.as_mv.row += 4; michael@0: thismse = vfp->svf_halfpix_hv(y - 1, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: case 3: michael@0: default: michael@0: this_mv.as_mv.col += 4; michael@0: this_mv.as_mv.row += 4; michael@0: thismse = vfp->svf_halfpix_hv(y, y_stride, z, b->src_stride, &sse); michael@0: break; michael@0: } michael@0: michael@0: diag = thismse + mv_err_cost(&this_mv, ref_mv, mvcost, error_per_bit); michael@0: michael@0: if (diag < bestmse) michael@0: { michael@0: *bestmv = this_mv; michael@0: bestmse = diag; michael@0: *distortion = thismse; michael@0: *sse1 = sse; michael@0: } michael@0: michael@0: return bestmse; michael@0: } michael@0: michael@0: #define CHECK_BOUNDS(range) \ michael@0: {\ michael@0: all_in = 1;\ michael@0: all_in &= ((br-range) >= x->mv_row_min);\ michael@0: all_in &= ((br+range) <= x->mv_row_max);\ michael@0: all_in &= ((bc-range) >= x->mv_col_min);\ michael@0: all_in &= ((bc+range) <= x->mv_col_max);\ michael@0: } michael@0: michael@0: #define CHECK_POINT \ michael@0: {\ michael@0: if (this_mv.as_mv.col < x->mv_col_min) continue;\ michael@0: if (this_mv.as_mv.col > x->mv_col_max) continue;\ michael@0: if (this_mv.as_mv.row < x->mv_row_min) continue;\ michael@0: if (this_mv.as_mv.row > x->mv_row_max) continue;\ michael@0: } michael@0: michael@0: #define CHECK_BETTER \ michael@0: {\ michael@0: if (thissad < bestsad)\ michael@0: {\ michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit);\ michael@0: if (thissad < bestsad)\ michael@0: {\ michael@0: bestsad = thissad;\ michael@0: best_site = i;\ michael@0: }\ michael@0: }\ michael@0: } michael@0: michael@0: static const MV next_chkpts[6][3] = michael@0: { michael@0: {{ -2, 0}, { -1, -2}, {1, -2}}, michael@0: {{ -1, -2}, {1, -2}, {2, 0}}, michael@0: {{1, -2}, {2, 0}, {1, 2}}, michael@0: {{2, 0}, {1, 2}, { -1, 2}}, michael@0: {{1, 2}, { -1, 2}, { -2, 0}}, michael@0: {{ -1, 2}, { -2, 0}, { -1, -2}} michael@0: }; michael@0: michael@0: int vp8_hex_search michael@0: ( michael@0: MACROBLOCK *x, michael@0: BLOCK *b, michael@0: BLOCKD *d, michael@0: int_mv *ref_mv, michael@0: int_mv *best_mv, michael@0: int search_param, michael@0: int sad_per_bit, michael@0: const vp8_variance_fn_ptr_t *vfp, michael@0: int *mvsadcost[2], michael@0: int *mvcost[2], michael@0: int_mv *center_mv michael@0: ) michael@0: { michael@0: MV hex[6] = { { -1, -2}, {1, -2}, {2, 0}, {1, 2}, { -1, 2}, { -2, 0} } ; michael@0: MV neighbors[4] = {{0, -1}, { -1, 0}, {1, 0}, {0, 1}} ; michael@0: int i, j; michael@0: michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: michael@0: int in_what_stride = pre_stride; michael@0: int br, bc; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: unsigned char *base_offset; michael@0: unsigned char *this_offset; michael@0: int k = -1; michael@0: int all_in; michael@0: int best_site = -1; michael@0: int hex_range = 127; michael@0: int dia_range = 8; michael@0: michael@0: int_mv fcenter_mv; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: /* adjust ref_mv to make sure it is within MV range */ michael@0: vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max); michael@0: br = ref_mv->as_mv.row; michael@0: bc = ref_mv->as_mv.col; michael@0: michael@0: /* Work out the start point for the search */ michael@0: base_offset = (unsigned char *)(base_pre + d->offset); michael@0: this_offset = base_offset + (br * (pre_stride)) + bc; michael@0: this_mv.as_mv.row = br; michael@0: this_mv.as_mv.col = bc; michael@0: bestsad = vfp->sdf(what, what_stride, this_offset, in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: #if CONFIG_MULTI_RES_ENCODING michael@0: /* Lower search range based on prediction info */ michael@0: if (search_param >= 6) goto cal_neighbors; michael@0: else if (search_param >= 5) hex_range = 4; michael@0: else if (search_param >= 4) hex_range = 6; michael@0: else if (search_param >= 3) hex_range = 15; michael@0: else if (search_param >= 2) hex_range = 31; michael@0: else if (search_param >= 1) hex_range = 63; michael@0: michael@0: dia_range = 8; michael@0: #endif michael@0: michael@0: /* hex search */ michael@0: CHECK_BOUNDS(2) michael@0: michael@0: if(all_in) michael@0: { michael@0: for (i = 0; i < 6; i++) michael@0: { michael@0: this_mv.as_mv.row = br + hex[i].row; michael@0: this_mv.as_mv.col = bc + hex[i].col; michael@0: this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: }else michael@0: { michael@0: for (i = 0; i < 6; i++) michael@0: { michael@0: this_mv.as_mv.row = br + hex[i].row; michael@0: this_mv.as_mv.col = bc + hex[i].col; michael@0: CHECK_POINT michael@0: this_offset = base_offset + (this_mv.as_mv.row * in_what_stride) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: } michael@0: michael@0: if (best_site == -1) michael@0: goto cal_neighbors; michael@0: else michael@0: { michael@0: br += hex[best_site].row; michael@0: bc += hex[best_site].col; michael@0: k = best_site; michael@0: } michael@0: michael@0: for (j = 1; j < hex_range; j++) michael@0: { michael@0: best_site = -1; michael@0: CHECK_BOUNDS(2) michael@0: michael@0: if(all_in) michael@0: { michael@0: for (i = 0; i < 3; i++) michael@0: { michael@0: this_mv.as_mv.row = br + next_chkpts[k][i].row; michael@0: this_mv.as_mv.col = bc + next_chkpts[k][i].col; michael@0: this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: }else michael@0: { michael@0: for (i = 0; i < 3; i++) michael@0: { michael@0: this_mv.as_mv.row = br + next_chkpts[k][i].row; michael@0: this_mv.as_mv.col = bc + next_chkpts[k][i].col; michael@0: CHECK_POINT michael@0: this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: } michael@0: michael@0: if (best_site == -1) michael@0: break; michael@0: else michael@0: { michael@0: br += next_chkpts[k][best_site].row; michael@0: bc += next_chkpts[k][best_site].col; michael@0: k += 5 + best_site; michael@0: if (k >= 12) k -= 12; michael@0: else if (k >= 6) k -= 6; michael@0: } michael@0: } michael@0: michael@0: /* check 4 1-away neighbors */ michael@0: cal_neighbors: michael@0: for (j = 0; j < dia_range; j++) michael@0: { michael@0: best_site = -1; michael@0: CHECK_BOUNDS(1) michael@0: michael@0: if(all_in) michael@0: { michael@0: for (i = 0; i < 4; i++) michael@0: { michael@0: this_mv.as_mv.row = br + neighbors[i].row; michael@0: this_mv.as_mv.col = bc + neighbors[i].col; michael@0: this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: }else michael@0: { michael@0: for (i = 0; i < 4; i++) michael@0: { michael@0: this_mv.as_mv.row = br + neighbors[i].row; michael@0: this_mv.as_mv.col = bc + neighbors[i].col; michael@0: CHECK_POINT michael@0: this_offset = base_offset + (this_mv.as_mv.row * (in_what_stride)) + this_mv.as_mv.col; michael@0: thissad = vfp->sdf(what, what_stride, this_offset, in_what_stride, bestsad); michael@0: CHECK_BETTER michael@0: } michael@0: } michael@0: michael@0: if (best_site == -1) michael@0: break; michael@0: else michael@0: { michael@0: br += neighbors[best_site].row; michael@0: bc += neighbors[best_site].col; michael@0: } michael@0: } michael@0: michael@0: best_mv->as_mv.row = br; michael@0: best_mv->as_mv.col = bc; michael@0: michael@0: return bestsad; michael@0: } michael@0: #undef CHECK_BOUNDS michael@0: #undef CHECK_POINT michael@0: #undef CHECK_BETTER michael@0: michael@0: int vp8_diamond_search_sad_c michael@0: ( michael@0: MACROBLOCK *x, michael@0: BLOCK *b, michael@0: BLOCKD *d, michael@0: int_mv *ref_mv, michael@0: int_mv *best_mv, michael@0: int search_param, michael@0: int sad_per_bit, michael@0: int *num00, michael@0: vp8_variance_fn_ptr_t *fn_ptr, michael@0: int *mvcost[2], michael@0: int_mv *center_mv michael@0: ) michael@0: { michael@0: int i, j, step; michael@0: michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: unsigned char *in_what; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: unsigned char *best_address; michael@0: michael@0: int tot_steps; michael@0: int_mv this_mv; michael@0: michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: int best_site = 0; michael@0: int last_site = 0; michael@0: michael@0: int ref_row; michael@0: int ref_col; michael@0: int this_row_offset; michael@0: int this_col_offset; michael@0: search_site *ss; michael@0: michael@0: unsigned char *check_here; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max); michael@0: ref_row = ref_mv->as_mv.row; michael@0: ref_col = ref_mv->as_mv.col; michael@0: *num00 = 0; michael@0: best_mv->as_mv.row = ref_row; michael@0: best_mv->as_mv.col = ref_col; michael@0: michael@0: /* Work out the start point for the search */ michael@0: in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col); michael@0: best_address = in_what; michael@0: michael@0: /* Check the starting position */ michael@0: bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: /* search_param determines the length of the initial step and hence michael@0: * the number of iterations 0 = initial step (MAX_FIRST_STEP) pel : michael@0: * 1 = (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc. michael@0: */ michael@0: ss = &x->ss[search_param * x->searches_per_step]; michael@0: tot_steps = (x->ss_count / x->searches_per_step) - search_param; michael@0: michael@0: i = 1; michael@0: michael@0: for (step = 0; step < tot_steps ; step++) michael@0: { michael@0: for (j = 0 ; j < x->searches_per_step ; j++) michael@0: { michael@0: /* Trap illegal vectors */ michael@0: this_row_offset = best_mv->as_mv.row + ss[i].mv.row; michael@0: this_col_offset = best_mv->as_mv.col + ss[i].mv.col; michael@0: michael@0: if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) && michael@0: (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max)) michael@0: michael@0: { michael@0: check_here = ss[i].offset + best_address; michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.row = this_row_offset; michael@0: this_mv.as_mv.col = this_col_offset; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_site = i; michael@0: } michael@0: } michael@0: } michael@0: michael@0: i++; michael@0: } michael@0: michael@0: if (best_site != last_site) michael@0: { michael@0: best_mv->as_mv.row += ss[best_site].mv.row; michael@0: best_mv->as_mv.col += ss[best_site].mv.col; michael@0: best_address += ss[best_site].offset; michael@0: last_site = best_site; michael@0: } michael@0: else if (best_address == in_what) michael@0: (*num00)++; michael@0: } michael@0: michael@0: this_mv.as_mv.row = best_mv->as_mv.row << 3; michael@0: this_mv.as_mv.col = best_mv->as_mv.col << 3; michael@0: michael@0: return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_diamond_search_sadx4 michael@0: ( michael@0: MACROBLOCK *x, michael@0: BLOCK *b, michael@0: BLOCKD *d, michael@0: int_mv *ref_mv, michael@0: int_mv *best_mv, michael@0: int search_param, michael@0: int sad_per_bit, michael@0: int *num00, michael@0: vp8_variance_fn_ptr_t *fn_ptr, michael@0: int *mvcost[2], michael@0: int_mv *center_mv michael@0: ) michael@0: { michael@0: int i, j, step; michael@0: michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: unsigned char *in_what; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: unsigned char *best_address; michael@0: michael@0: int tot_steps; michael@0: int_mv this_mv; michael@0: michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: int best_site = 0; michael@0: int last_site = 0; michael@0: michael@0: int ref_row; michael@0: int ref_col; michael@0: int this_row_offset; michael@0: int this_col_offset; michael@0: search_site *ss; michael@0: michael@0: unsigned char *check_here; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: vp8_clamp_mv(ref_mv, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max); michael@0: ref_row = ref_mv->as_mv.row; michael@0: ref_col = ref_mv->as_mv.col; michael@0: *num00 = 0; michael@0: best_mv->as_mv.row = ref_row; michael@0: best_mv->as_mv.col = ref_col; michael@0: michael@0: /* Work out the start point for the search */ michael@0: in_what = (unsigned char *)(base_pre + d->offset + (ref_row * pre_stride) + ref_col); michael@0: best_address = in_what; michael@0: michael@0: /* Check the starting position */ michael@0: bestsad = fn_ptr->sdf(what, what_stride, in_what, in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: /* search_param determines the length of the initial step and hence the michael@0: * number of iterations 0 = initial step (MAX_FIRST_STEP) pel : 1 = michael@0: * (MAX_FIRST_STEP/2) pel, 2 = (MAX_FIRST_STEP/4) pel... etc. michael@0: */ michael@0: ss = &x->ss[search_param * x->searches_per_step]; michael@0: tot_steps = (x->ss_count / x->searches_per_step) - search_param; michael@0: michael@0: i = 1; michael@0: michael@0: for (step = 0; step < tot_steps ; step++) michael@0: { michael@0: int all_in = 1, t; michael@0: michael@0: /* To know if all neighbor points are within the bounds, 4 bounds michael@0: * checking are enough instead of checking 4 bounds for each michael@0: * points. michael@0: */ michael@0: all_in &= ((best_mv->as_mv.row + ss[i].mv.row)> x->mv_row_min); michael@0: all_in &= ((best_mv->as_mv.row + ss[i+1].mv.row) < x->mv_row_max); michael@0: all_in &= ((best_mv->as_mv.col + ss[i+2].mv.col) > x->mv_col_min); michael@0: all_in &= ((best_mv->as_mv.col + ss[i+3].mv.col) < x->mv_col_max); michael@0: michael@0: if (all_in) michael@0: { michael@0: unsigned int sad_array[4]; michael@0: michael@0: for (j = 0 ; j < x->searches_per_step ; j += 4) michael@0: { michael@0: const unsigned char *block_offset[4]; michael@0: michael@0: for (t = 0; t < 4; t++) michael@0: block_offset[t] = ss[i+t].offset + best_address; michael@0: michael@0: fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array); michael@0: michael@0: for (t = 0; t < 4; t++, i++) michael@0: { michael@0: if (sad_array[t] < bestsad) michael@0: { michael@0: this_mv.as_mv.row = best_mv->as_mv.row + ss[i].mv.row; michael@0: this_mv.as_mv.col = best_mv->as_mv.col + ss[i].mv.col; michael@0: sad_array[t] += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (sad_array[t] < bestsad) michael@0: { michael@0: bestsad = sad_array[t]; michael@0: best_site = i; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: for (j = 0 ; j < x->searches_per_step ; j++) michael@0: { michael@0: /* Trap illegal vectors */ michael@0: this_row_offset = best_mv->as_mv.row + ss[i].mv.row; michael@0: this_col_offset = best_mv->as_mv.col + ss[i].mv.col; michael@0: michael@0: if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) && michael@0: (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max)) michael@0: { michael@0: check_here = ss[i].offset + best_address; michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.row = this_row_offset; michael@0: this_mv.as_mv.col = this_col_offset; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_site = i; michael@0: } michael@0: } michael@0: } michael@0: i++; michael@0: } michael@0: } michael@0: michael@0: if (best_site != last_site) michael@0: { michael@0: best_mv->as_mv.row += ss[best_site].mv.row; michael@0: best_mv->as_mv.col += ss[best_site].mv.col; michael@0: best_address += ss[best_site].offset; michael@0: last_site = best_site; michael@0: } michael@0: else if (best_address == in_what) michael@0: (*num00)++; michael@0: } michael@0: michael@0: this_mv.as_mv.row = best_mv->as_mv.row * 8; michael@0: this_mv.as_mv.col = best_mv->as_mv.col * 8; michael@0: michael@0: return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_full_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv, michael@0: int sad_per_bit, int distance, michael@0: vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], michael@0: int_mv *center_mv) michael@0: { michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: unsigned char *in_what; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: int mv_stride = pre_stride; michael@0: unsigned char *bestaddress; michael@0: int_mv *best_mv = &d->bmi.mv; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: int r, c; michael@0: michael@0: unsigned char *check_here; michael@0: michael@0: int ref_row = ref_mv->as_mv.row; michael@0: int ref_col = ref_mv->as_mv.col; michael@0: michael@0: int row_min = ref_row - distance; michael@0: int row_max = ref_row + distance; michael@0: int col_min = ref_col - distance; michael@0: int col_max = ref_col + distance; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: /* Work out the mid point for the search */ michael@0: in_what = base_pre + d->offset; michael@0: bestaddress = in_what + (ref_row * pre_stride) + ref_col; michael@0: michael@0: best_mv->as_mv.row = ref_row; michael@0: best_mv->as_mv.col = ref_col; michael@0: michael@0: /* Baseline value at the centre */ michael@0: bestsad = fn_ptr->sdf(what, what_stride, bestaddress, michael@0: in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: /* Apply further limits to prevent us looking using vectors that michael@0: * stretch beyiond the UMV border michael@0: */ michael@0: if (col_min < x->mv_col_min) michael@0: col_min = x->mv_col_min; michael@0: michael@0: if (col_max > x->mv_col_max) michael@0: col_max = x->mv_col_max; michael@0: michael@0: if (row_min < x->mv_row_min) michael@0: row_min = x->mv_row_min; michael@0: michael@0: if (row_max > x->mv_row_max) michael@0: row_max = x->mv_row_max; michael@0: michael@0: for (r = row_min; r < row_max ; r++) michael@0: { michael@0: this_mv.as_mv.row = r; michael@0: check_here = r * mv_stride + in_what + col_min; michael@0: michael@0: for (c = col_min; c < col_max; c++) michael@0: { michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride, bestsad); michael@0: michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: michael@0: check_here++; michael@0: } michael@0: } michael@0: michael@0: this_mv.as_mv.row = best_mv->as_mv.row << 3; michael@0: this_mv.as_mv.col = best_mv->as_mv.col << 3; michael@0: michael@0: return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_full_search_sadx3(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv, michael@0: int sad_per_bit, int distance, michael@0: vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], michael@0: int_mv *center_mv) michael@0: { michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: unsigned char *in_what; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: int mv_stride = pre_stride; michael@0: unsigned char *bestaddress; michael@0: int_mv *best_mv = &d->bmi.mv; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: int r, c; michael@0: michael@0: unsigned char *check_here; michael@0: michael@0: int ref_row = ref_mv->as_mv.row; michael@0: int ref_col = ref_mv->as_mv.col; michael@0: michael@0: int row_min = ref_row - distance; michael@0: int row_max = ref_row + distance; michael@0: int col_min = ref_col - distance; michael@0: int col_max = ref_col + distance; michael@0: michael@0: unsigned int sad_array[3]; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: /* Work out the mid point for the search */ michael@0: in_what = base_pre + d->offset; michael@0: bestaddress = in_what + (ref_row * pre_stride) + ref_col; michael@0: michael@0: best_mv->as_mv.row = ref_row; michael@0: best_mv->as_mv.col = ref_col; michael@0: michael@0: /* Baseline value at the centre */ michael@0: bestsad = fn_ptr->sdf(what, what_stride, bestaddress, michael@0: in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: /* Apply further limits to prevent us looking using vectors that stretch michael@0: * beyond the UMV border michael@0: */ michael@0: if (col_min < x->mv_col_min) michael@0: col_min = x->mv_col_min; michael@0: michael@0: if (col_max > x->mv_col_max) michael@0: col_max = x->mv_col_max; michael@0: michael@0: if (row_min < x->mv_row_min) michael@0: row_min = x->mv_row_min; michael@0: michael@0: if (row_max > x->mv_row_max) michael@0: row_max = x->mv_row_max; michael@0: michael@0: for (r = row_min; r < row_max ; r++) michael@0: { michael@0: this_mv.as_mv.row = r; michael@0: check_here = r * mv_stride + in_what + col_min; michael@0: c = col_min; michael@0: michael@0: while ((c + 2) < col_max) michael@0: { michael@0: int i; michael@0: michael@0: fn_ptr->sdx3f(what, what_stride, check_here, in_what_stride, sad_array); michael@0: michael@0: for (i = 0; i < 3; i++) michael@0: { michael@0: thissad = sad_array[i]; michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: } michael@0: michael@0: check_here++; michael@0: c++; michael@0: } michael@0: } michael@0: michael@0: while (c < col_max) michael@0: { michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here, in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: } michael@0: michael@0: check_here ++; michael@0: c ++; michael@0: } michael@0: michael@0: } michael@0: michael@0: this_mv.as_mv.row = best_mv->as_mv.row << 3; michael@0: this_mv.as_mv.col = best_mv->as_mv.col << 3; michael@0: michael@0: return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_full_search_sadx8(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv, michael@0: int sad_per_bit, int distance, michael@0: vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], michael@0: int_mv *center_mv) michael@0: { michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: int what_stride = b->src_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: unsigned char *in_what; michael@0: int in_what_stride = pre_stride; michael@0: int mv_stride = pre_stride; michael@0: unsigned char *bestaddress; michael@0: int_mv *best_mv = &d->bmi.mv; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: int r, c; michael@0: michael@0: unsigned char *check_here; michael@0: michael@0: int ref_row = ref_mv->as_mv.row; michael@0: int ref_col = ref_mv->as_mv.col; michael@0: michael@0: int row_min = ref_row - distance; michael@0: int row_max = ref_row + distance; michael@0: int col_min = ref_col - distance; michael@0: int col_max = ref_col + distance; michael@0: michael@0: DECLARE_ALIGNED_ARRAY(16, unsigned short, sad_array8, 8); michael@0: unsigned int sad_array[3]; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: /* Work out the mid point for the search */ michael@0: in_what = base_pre + d->offset; michael@0: bestaddress = in_what + (ref_row * pre_stride) + ref_col; michael@0: michael@0: best_mv->as_mv.row = ref_row; michael@0: best_mv->as_mv.col = ref_col; michael@0: michael@0: /* Baseline value at the centre */ michael@0: bestsad = fn_ptr->sdf(what, what_stride, michael@0: bestaddress, in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(best_mv, &fcenter_mv, mvsadcost, sad_per_bit); michael@0: michael@0: /* Apply further limits to prevent us looking using vectors that stretch michael@0: * beyond the UMV border michael@0: */ michael@0: if (col_min < x->mv_col_min) michael@0: col_min = x->mv_col_min; michael@0: michael@0: if (col_max > x->mv_col_max) michael@0: col_max = x->mv_col_max; michael@0: michael@0: if (row_min < x->mv_row_min) michael@0: row_min = x->mv_row_min; michael@0: michael@0: if (row_max > x->mv_row_max) michael@0: row_max = x->mv_row_max; michael@0: michael@0: for (r = row_min; r < row_max ; r++) michael@0: { michael@0: this_mv.as_mv.row = r; michael@0: check_here = r * mv_stride + in_what + col_min; michael@0: c = col_min; michael@0: michael@0: while ((c + 7) < col_max) michael@0: { michael@0: int i; michael@0: michael@0: fn_ptr->sdx8f(what, what_stride, check_here, in_what_stride, sad_array8); michael@0: michael@0: for (i = 0; i < 8; i++) michael@0: { michael@0: thissad = sad_array8[i]; michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: } michael@0: michael@0: check_here++; michael@0: c++; michael@0: } michael@0: } michael@0: michael@0: while ((c + 2) < col_max) michael@0: { michael@0: int i; michael@0: michael@0: fn_ptr->sdx3f(what, what_stride, check_here , in_what_stride, sad_array); michael@0: michael@0: for (i = 0; i < 3; i++) michael@0: { michael@0: thissad = sad_array[i]; michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: } michael@0: michael@0: check_here++; michael@0: c++; michael@0: } michael@0: } michael@0: michael@0: while (c < col_max) michael@0: { michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.col = c; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, michael@0: mvsadcost, sad_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_mv->as_mv.row = r; michael@0: best_mv->as_mv.col = c; michael@0: bestaddress = check_here; michael@0: } michael@0: } michael@0: michael@0: check_here ++; michael@0: c ++; michael@0: } michael@0: } michael@0: michael@0: this_mv.as_mv.row = best_mv->as_mv.row * 8; michael@0: this_mv.as_mv.col = best_mv->as_mv.col * 8; michael@0: michael@0: return fn_ptr->vf(what, what_stride, bestaddress, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_refining_search_sad_c(MACROBLOCK *x, BLOCK *b, BLOCKD *d, int_mv *ref_mv, michael@0: int error_per_bit, int search_range, michael@0: vp8_variance_fn_ptr_t *fn_ptr, int *mvcost[2], michael@0: int_mv *center_mv) michael@0: { michael@0: MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; michael@0: int i, j; michael@0: short this_row_offset, this_col_offset; michael@0: michael@0: int what_stride = b->src_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: unsigned char *best_address = (unsigned char *)(base_pre + d->offset + michael@0: (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col); michael@0: unsigned char *check_here; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: bestsad = fn_ptr->sdf(what, what_stride, best_address, michael@0: in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit); michael@0: michael@0: for (i=0; ias_mv.row + neighbors[j].row; michael@0: this_col_offset = ref_mv->as_mv.col + neighbors[j].col; michael@0: michael@0: if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) && michael@0: (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max)) michael@0: { michael@0: check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address; michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.row = this_row_offset; michael@0: this_mv.as_mv.col = this_col_offset; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_site = j; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (best_site == -1) michael@0: break; michael@0: else michael@0: { michael@0: ref_mv->as_mv.row += neighbors[best_site].row; michael@0: ref_mv->as_mv.col += neighbors[best_site].col; michael@0: best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col; michael@0: } michael@0: } michael@0: michael@0: this_mv.as_mv.row = ref_mv->as_mv.row << 3; michael@0: this_mv.as_mv.col = ref_mv->as_mv.col << 3; michael@0: michael@0: return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: int vp8_refining_search_sadx4(MACROBLOCK *x, BLOCK *b, BLOCKD *d, michael@0: int_mv *ref_mv, int error_per_bit, michael@0: int search_range, vp8_variance_fn_ptr_t *fn_ptr, michael@0: int *mvcost[2], int_mv *center_mv) michael@0: { michael@0: MV neighbors[4] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; michael@0: int i, j; michael@0: short this_row_offset, this_col_offset; michael@0: michael@0: int what_stride = b->src_stride; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: int in_what_stride = pre_stride; michael@0: unsigned char *what = (*(b->base_src) + b->src); michael@0: unsigned char *best_address = (unsigned char *)(base_pre + d->offset + michael@0: (ref_mv->as_mv.row * pre_stride) + ref_mv->as_mv.col); michael@0: unsigned char *check_here; michael@0: int_mv this_mv; michael@0: unsigned int bestsad; michael@0: unsigned int thissad; michael@0: michael@0: int *mvsadcost[2]; michael@0: int_mv fcenter_mv; michael@0: michael@0: mvsadcost[0] = x->mvsadcost[0]; michael@0: mvsadcost[1] = x->mvsadcost[1]; michael@0: fcenter_mv.as_mv.row = center_mv->as_mv.row >> 3; michael@0: fcenter_mv.as_mv.col = center_mv->as_mv.col >> 3; michael@0: michael@0: bestsad = fn_ptr->sdf(what, what_stride, best_address, michael@0: in_what_stride, UINT_MAX) michael@0: + mvsad_err_cost(ref_mv, &fcenter_mv, mvsadcost, error_per_bit); michael@0: michael@0: for (i=0; ias_mv.row - 1) > x->mv_row_min); michael@0: all_in &= ((ref_mv->as_mv.row + 1) < x->mv_row_max); michael@0: all_in &= ((ref_mv->as_mv.col - 1) > x->mv_col_min); michael@0: all_in &= ((ref_mv->as_mv.col + 1) < x->mv_col_max); michael@0: michael@0: if(all_in) michael@0: { michael@0: unsigned int sad_array[4]; michael@0: const unsigned char *block_offset[4]; michael@0: block_offset[0] = best_address - in_what_stride; michael@0: block_offset[1] = best_address - 1; michael@0: block_offset[2] = best_address + 1; michael@0: block_offset[3] = best_address + in_what_stride; michael@0: michael@0: fn_ptr->sdx4df(what, what_stride, block_offset, in_what_stride, sad_array); michael@0: michael@0: for (j = 0; j < 4; j++) michael@0: { michael@0: if (sad_array[j] < bestsad) michael@0: { michael@0: this_mv.as_mv.row = ref_mv->as_mv.row + neighbors[j].row; michael@0: this_mv.as_mv.col = ref_mv->as_mv.col + neighbors[j].col; michael@0: sad_array[j] += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit); michael@0: michael@0: if (sad_array[j] < bestsad) michael@0: { michael@0: bestsad = sad_array[j]; michael@0: best_site = j; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: for (j = 0 ; j < 4 ; j++) michael@0: { michael@0: this_row_offset = ref_mv->as_mv.row + neighbors[j].row; michael@0: this_col_offset = ref_mv->as_mv.col + neighbors[j].col; michael@0: michael@0: if ((this_col_offset > x->mv_col_min) && (this_col_offset < x->mv_col_max) && michael@0: (this_row_offset > x->mv_row_min) && (this_row_offset < x->mv_row_max)) michael@0: { michael@0: check_here = (neighbors[j].row)*in_what_stride + neighbors[j].col + best_address; michael@0: thissad = fn_ptr->sdf(what, what_stride, check_here , in_what_stride, bestsad); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: this_mv.as_mv.row = this_row_offset; michael@0: this_mv.as_mv.col = this_col_offset; michael@0: thissad += mvsad_err_cost(&this_mv, &fcenter_mv, mvsadcost, error_per_bit); michael@0: michael@0: if (thissad < bestsad) michael@0: { michael@0: bestsad = thissad; michael@0: best_site = j; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (best_site == -1) michael@0: break; michael@0: else michael@0: { michael@0: ref_mv->as_mv.row += neighbors[best_site].row; michael@0: ref_mv->as_mv.col += neighbors[best_site].col; michael@0: best_address += (neighbors[best_site].row)*in_what_stride + neighbors[best_site].col; michael@0: } michael@0: } michael@0: michael@0: this_mv.as_mv.row = ref_mv->as_mv.row * 8; michael@0: this_mv.as_mv.col = ref_mv->as_mv.col * 8; michael@0: michael@0: return fn_ptr->vf(what, what_stride, best_address, in_what_stride, &thissad) michael@0: + mv_err_cost(&this_mv, center_mv, mvcost, x->errorperbit); michael@0: } michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: void print_mode_context(void) michael@0: { michael@0: FILE *f = fopen("modecont.c", "w"); michael@0: int i, j; michael@0: michael@0: fprintf(f, "#include \"entropy.h\"\n"); michael@0: fprintf(f, "const int vp8_mode_contexts[6][4] =\n"); michael@0: fprintf(f, "{\n"); michael@0: michael@0: for (j = 0; j < 6; j++) michael@0: { michael@0: fprintf(f, " { /* %d */\n", j); michael@0: fprintf(f, " "); michael@0: michael@0: for (i = 0; i < 4; i++) michael@0: { michael@0: int overal_prob; michael@0: int this_prob; michael@0: int count; michael@0: michael@0: /* Overall probs */ michael@0: count = mv_mode_cts[i][0] + mv_mode_cts[i][1]; michael@0: michael@0: if (count) michael@0: overal_prob = 256 * mv_mode_cts[i][0] / count; michael@0: else michael@0: overal_prob = 128; michael@0: michael@0: if (overal_prob == 0) michael@0: overal_prob = 1; michael@0: michael@0: /* context probs */ michael@0: count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1]; michael@0: michael@0: if (count) michael@0: this_prob = 256 * mv_ref_ct[j][i][0] / count; michael@0: else michael@0: this_prob = 128; michael@0: michael@0: if (this_prob == 0) michael@0: this_prob = 1; michael@0: michael@0: fprintf(f, "%5d, ", this_prob); michael@0: } michael@0: michael@0: fprintf(f, " },\n"); michael@0: } michael@0: michael@0: fprintf(f, "};\n"); michael@0: fclose(f); michael@0: } michael@0: michael@0: /* MV ref count VP8_ENTROPY_STATS stats code */ michael@0: #ifdef VP8_ENTROPY_STATS michael@0: void init_mv_ref_counts() michael@0: { michael@0: vpx_memset(mv_ref_ct, 0, sizeof(mv_ref_ct)); michael@0: vpx_memset(mv_mode_cts, 0, sizeof(mv_mode_cts)); michael@0: } michael@0: michael@0: void accum_mv_refs(MB_PREDICTION_MODE m, const int ct[4]) michael@0: { michael@0: if (m == ZEROMV) michael@0: { michael@0: ++mv_ref_ct [ct[0]] [0] [0]; michael@0: ++mv_mode_cts[0][0]; michael@0: } michael@0: else michael@0: { michael@0: ++mv_ref_ct [ct[0]] [0] [1]; michael@0: ++mv_mode_cts[0][1]; michael@0: michael@0: if (m == NEARESTMV) michael@0: { michael@0: ++mv_ref_ct [ct[1]] [1] [0]; michael@0: ++mv_mode_cts[1][0]; michael@0: } michael@0: else michael@0: { michael@0: ++mv_ref_ct [ct[1]] [1] [1]; michael@0: ++mv_mode_cts[1][1]; michael@0: michael@0: if (m == NEARMV) michael@0: { michael@0: ++mv_ref_ct [ct[2]] [2] [0]; michael@0: ++mv_mode_cts[2][0]; michael@0: } michael@0: else michael@0: { michael@0: ++mv_ref_ct [ct[2]] [2] [1]; michael@0: ++mv_mode_cts[2][1]; michael@0: michael@0: if (m == NEWMV) michael@0: { michael@0: ++mv_ref_ct [ct[3]] [3] [0]; michael@0: ++mv_mode_cts[3][0]; michael@0: } michael@0: else michael@0: { michael@0: ++mv_ref_ct [ct[3]] [3] [1]; michael@0: ++mv_mode_cts[3][1]; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #endif/* END MV ref count VP8_ENTROPY_STATS stats code */ michael@0: michael@0: #endif