media/libvpx/vp8/encoder/pickinter.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/encoder/pickinter.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1296 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +
    1.15 +#include <limits.h>
    1.16 +#include "vpx_config.h"
    1.17 +#include "onyx_int.h"
    1.18 +#include "modecosts.h"
    1.19 +#include "encodeintra.h"
    1.20 +#include "vp8/common/entropymode.h"
    1.21 +#include "pickinter.h"
    1.22 +#include "vp8/common/findnearmv.h"
    1.23 +#include "encodemb.h"
    1.24 +#include "vp8/common/reconinter.h"
    1.25 +#include "vp8/common/reconintra4x4.h"
    1.26 +#include "vp8/common/variance.h"
    1.27 +#include "mcomp.h"
    1.28 +#include "rdopt.h"
    1.29 +#include "vpx_mem/vpx_mem.h"
    1.30 +#if CONFIG_TEMPORAL_DENOISING
    1.31 +#include "denoising.h"
    1.32 +#endif
    1.33 +
    1.34 +extern int VP8_UVSSE(MACROBLOCK *x);
    1.35 +
    1.36 +#ifdef SPEEDSTATS
    1.37 +extern unsigned int cnt_pm;
    1.38 +#endif
    1.39 +
    1.40 +extern const int vp8_ref_frame_order[MAX_MODES];
    1.41 +extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
    1.42 +
    1.43 +extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
    1.44 +
    1.45 +
    1.46 +int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
    1.47 +                                int_mv *bestmv, int_mv *ref_mv,
    1.48 +                                int error_per_bit,
    1.49 +                                const vp8_variance_fn_ptr_t *vfp,
    1.50 +                                int *mvcost[2], int *distortion,
    1.51 +                                unsigned int *sse)
    1.52 +{
    1.53 +    (void) b;
    1.54 +    (void) d;
    1.55 +    (void) ref_mv;
    1.56 +    (void) error_per_bit;
    1.57 +    (void) vfp;
    1.58 +    (void) mvcost;
    1.59 +    (void) distortion;
    1.60 +    (void) sse;
    1.61 +    bestmv->as_mv.row <<= 3;
    1.62 +    bestmv->as_mv.col <<= 3;
    1.63 +    return 0;
    1.64 +}
    1.65 +
    1.66 +
    1.67 +int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
    1.68 +                                  const vp8_variance_fn_ptr_t *vfp,
    1.69 +                                  unsigned int *sse,
    1.70 +                                  int_mv this_mv)
    1.71 +{
    1.72 +
    1.73 +    BLOCK *b = &mb->block[0];
    1.74 +    BLOCKD *d = &mb->e_mbd.block[0];
    1.75 +    unsigned char *what = (*(b->base_src) + b->src);
    1.76 +    int what_stride = b->src_stride;
    1.77 +    int pre_stride = mb->e_mbd.pre.y_stride;
    1.78 +    unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
    1.79 +    int in_what_stride = pre_stride;
    1.80 +    int xoffset = this_mv.as_mv.col & 7;
    1.81 +    int yoffset = this_mv.as_mv.row & 7;
    1.82 +
    1.83 +    in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
    1.84 +
    1.85 +    if (xoffset | yoffset)
    1.86 +    {
    1.87 +        return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
    1.88 +    }
    1.89 +    else
    1.90 +    {
    1.91 +        return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
    1.92 +    }
    1.93 +
    1.94 +}
    1.95 +
    1.96 +
    1.97 +unsigned int vp8_get4x4sse_cs_c
    1.98 +(
    1.99 +    const unsigned char *src_ptr,
   1.100 +    int  source_stride,
   1.101 +    const unsigned char *ref_ptr,
   1.102 +    int  recon_stride
   1.103 +)
   1.104 +{
   1.105 +    int distortion = 0;
   1.106 +    int r, c;
   1.107 +
   1.108 +    for (r = 0; r < 4; r++)
   1.109 +    {
   1.110 +        for (c = 0; c < 4; c++)
   1.111 +        {
   1.112 +            int diff = src_ptr[c] - ref_ptr[c];
   1.113 +            distortion += diff * diff;
   1.114 +        }
   1.115 +
   1.116 +        src_ptr += source_stride;
   1.117 +        ref_ptr += recon_stride;
   1.118 +    }
   1.119 +
   1.120 +    return distortion;
   1.121 +}
   1.122 +
   1.123 +static int get_prediction_error(BLOCK *be, BLOCKD *b)
   1.124 +{
   1.125 +    unsigned char *sptr;
   1.126 +    unsigned char *dptr;
   1.127 +    sptr = (*(be->base_src) + be->src);
   1.128 +    dptr = b->predictor;
   1.129 +
   1.130 +    return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
   1.131 +
   1.132 +}
   1.133 +
   1.134 +static int pick_intra4x4block(
   1.135 +    MACROBLOCK *x,
   1.136 +    int ib,
   1.137 +    B_PREDICTION_MODE *best_mode,
   1.138 +    const int *mode_costs,
   1.139 +
   1.140 +    int *bestrate,
   1.141 +    int *bestdistortion)
   1.142 +{
   1.143 +
   1.144 +    BLOCKD *b = &x->e_mbd.block[ib];
   1.145 +    BLOCK *be = &x->block[ib];
   1.146 +    int dst_stride = x->e_mbd.dst.y_stride;
   1.147 +    unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
   1.148 +    B_PREDICTION_MODE mode;
   1.149 +    int best_rd = INT_MAX;
   1.150 +    int rate;
   1.151 +    int distortion;
   1.152 +
   1.153 +    unsigned char *Above = dst - dst_stride;
   1.154 +    unsigned char *yleft = dst - 1;
   1.155 +    unsigned char top_left = Above[-1];
   1.156 +
   1.157 +    for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
   1.158 +    {
   1.159 +        int this_rd;
   1.160 +
   1.161 +        rate = mode_costs[mode];
   1.162 +
   1.163 +        vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
   1.164 +                             b->predictor, 16, top_left);
   1.165 +        distortion = get_prediction_error(be, b);
   1.166 +        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
   1.167 +
   1.168 +        if (this_rd < best_rd)
   1.169 +        {
   1.170 +            *bestrate = rate;
   1.171 +            *bestdistortion = distortion;
   1.172 +            best_rd = this_rd;
   1.173 +            *best_mode = mode;
   1.174 +        }
   1.175 +    }
   1.176 +
   1.177 +    b->bmi.as_mode = *best_mode;
   1.178 +    vp8_encode_intra4x4block(x, ib);
   1.179 +    return best_rd;
   1.180 +}
   1.181 +
   1.182 +
   1.183 +static int pick_intra4x4mby_modes
   1.184 +(
   1.185 +    MACROBLOCK *mb,
   1.186 +    int *Rate,
   1.187 +    int *best_dist
   1.188 +)
   1.189 +{
   1.190 +    MACROBLOCKD *const xd = &mb->e_mbd;
   1.191 +    int i;
   1.192 +    int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
   1.193 +    int error;
   1.194 +    int distortion = 0;
   1.195 +    const int *bmode_costs;
   1.196 +
   1.197 +    intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
   1.198 +
   1.199 +    bmode_costs = mb->inter_bmode_costs;
   1.200 +
   1.201 +    for (i = 0; i < 16; i++)
   1.202 +    {
   1.203 +        MODE_INFO *const mic = xd->mode_info_context;
   1.204 +        const int mis = xd->mode_info_stride;
   1.205 +
   1.206 +        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
   1.207 +        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
   1.208 +
   1.209 +        if (mb->e_mbd.frame_type == KEY_FRAME)
   1.210 +        {
   1.211 +            const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
   1.212 +            const B_PREDICTION_MODE L = left_block_mode(mic, i);
   1.213 +
   1.214 +            bmode_costs  = mb->bmode_costs[A][L];
   1.215 +        }
   1.216 +
   1.217 +
   1.218 +        pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
   1.219 +
   1.220 +        cost += r;
   1.221 +        distortion += d;
   1.222 +        mic->bmi[i].as_mode = best_mode;
   1.223 +
   1.224 +        /* Break out case where we have already exceeded best so far value
   1.225 +         * that was passed in
   1.226 +         */
   1.227 +        if (distortion > *best_dist)
   1.228 +            break;
   1.229 +    }
   1.230 +
   1.231 +    *Rate = cost;
   1.232 +
   1.233 +    if (i == 16)
   1.234 +    {
   1.235 +        *best_dist = distortion;
   1.236 +        error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
   1.237 +    }
   1.238 +    else
   1.239 +    {
   1.240 +        *best_dist = INT_MAX;
   1.241 +        error = INT_MAX;
   1.242 +    }
   1.243 +
   1.244 +    return error;
   1.245 +}
   1.246 +
   1.247 +static void pick_intra_mbuv_mode(MACROBLOCK *mb)
   1.248 +{
   1.249 +
   1.250 +    MACROBLOCKD *x = &mb->e_mbd;
   1.251 +    unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
   1.252 +    unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
   1.253 +    unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
   1.254 +    unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
   1.255 +    int uvsrc_stride = mb->block[16].src_stride;
   1.256 +    unsigned char uleft_col[8];
   1.257 +    unsigned char vleft_col[8];
   1.258 +    unsigned char utop_left = uabove_row[-1];
   1.259 +    unsigned char vtop_left = vabove_row[-1];
   1.260 +    int i, j;
   1.261 +    int expected_udc;
   1.262 +    int expected_vdc;
   1.263 +    int shift;
   1.264 +    int Uaverage = 0;
   1.265 +    int Vaverage = 0;
   1.266 +    int diff;
   1.267 +    int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
   1.268 +    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
   1.269 +
   1.270 +
   1.271 +    for (i = 0; i < 8; i++)
   1.272 +    {
   1.273 +        uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
   1.274 +        vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
   1.275 +    }
   1.276 +
   1.277 +    if (!x->up_available && !x->left_available)
   1.278 +    {
   1.279 +        expected_udc = 128;
   1.280 +        expected_vdc = 128;
   1.281 +    }
   1.282 +    else
   1.283 +    {
   1.284 +        shift = 2;
   1.285 +
   1.286 +        if (x->up_available)
   1.287 +        {
   1.288 +
   1.289 +            for (i = 0; i < 8; i++)
   1.290 +            {
   1.291 +                Uaverage += uabove_row[i];
   1.292 +                Vaverage += vabove_row[i];
   1.293 +            }
   1.294 +
   1.295 +            shift ++;
   1.296 +
   1.297 +        }
   1.298 +
   1.299 +        if (x->left_available)
   1.300 +        {
   1.301 +            for (i = 0; i < 8; i++)
   1.302 +            {
   1.303 +                Uaverage += uleft_col[i];
   1.304 +                Vaverage += vleft_col[i];
   1.305 +            }
   1.306 +
   1.307 +            shift ++;
   1.308 +
   1.309 +        }
   1.310 +
   1.311 +        expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
   1.312 +        expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
   1.313 +    }
   1.314 +
   1.315 +
   1.316 +    for (i = 0; i < 8; i++)
   1.317 +    {
   1.318 +        for (j = 0; j < 8; j++)
   1.319 +        {
   1.320 +
   1.321 +            int predu = uleft_col[i] + uabove_row[j] - utop_left;
   1.322 +            int predv = vleft_col[i] + vabove_row[j] - vtop_left;
   1.323 +            int u_p, v_p;
   1.324 +
   1.325 +            u_p = usrc_ptr[j];
   1.326 +            v_p = vsrc_ptr[j];
   1.327 +
   1.328 +            if (predu < 0)
   1.329 +                predu = 0;
   1.330 +
   1.331 +            if (predu > 255)
   1.332 +                predu = 255;
   1.333 +
   1.334 +            if (predv < 0)
   1.335 +                predv = 0;
   1.336 +
   1.337 +            if (predv > 255)
   1.338 +                predv = 255;
   1.339 +
   1.340 +
   1.341 +            diff = u_p - expected_udc;
   1.342 +            pred_error[DC_PRED] += diff * diff;
   1.343 +            diff = v_p - expected_vdc;
   1.344 +            pred_error[DC_PRED] += diff * diff;
   1.345 +
   1.346 +
   1.347 +            diff = u_p - uabove_row[j];
   1.348 +            pred_error[V_PRED] += diff * diff;
   1.349 +            diff = v_p - vabove_row[j];
   1.350 +            pred_error[V_PRED] += diff * diff;
   1.351 +
   1.352 +
   1.353 +            diff = u_p - uleft_col[i];
   1.354 +            pred_error[H_PRED] += diff * diff;
   1.355 +            diff = v_p - vleft_col[i];
   1.356 +            pred_error[H_PRED] += diff * diff;
   1.357 +
   1.358 +
   1.359 +            diff = u_p - predu;
   1.360 +            pred_error[TM_PRED] += diff * diff;
   1.361 +            diff = v_p - predv;
   1.362 +            pred_error[TM_PRED] += diff * diff;
   1.363 +
   1.364 +
   1.365 +        }
   1.366 +
   1.367 +        usrc_ptr += uvsrc_stride;
   1.368 +        vsrc_ptr += uvsrc_stride;
   1.369 +
   1.370 +        if (i == 3)
   1.371 +        {
   1.372 +            usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
   1.373 +            vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
   1.374 +        }
   1.375 +
   1.376 +
   1.377 +
   1.378 +    }
   1.379 +
   1.380 +
   1.381 +    for (i = DC_PRED; i <= TM_PRED; i++)
   1.382 +    {
   1.383 +        if (best_error > pred_error[i])
   1.384 +        {
   1.385 +            best_error = pred_error[i];
   1.386 +            best_mode = (MB_PREDICTION_MODE)i;
   1.387 +        }
   1.388 +    }
   1.389 +
   1.390 +
   1.391 +    mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
   1.392 +
   1.393 +}
   1.394 +
   1.395 +static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
   1.396 +{
   1.397 +    MACROBLOCKD *xd = &x->e_mbd;
   1.398 +    /* Split MV modes currently not supported when RD is nopt enabled,
   1.399 +     * therefore, only need to modify MVcount in NEWMV mode. */
   1.400 +    if (xd->mode_info_context->mbmi.mode == NEWMV)
   1.401 +    {
   1.402 +        x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
   1.403 +                                      best_ref_mv->as_mv.row) >> 1)]++;
   1.404 +        x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
   1.405 +                                      best_ref_mv->as_mv.col) >> 1)]++;
   1.406 +    }
   1.407 +}
   1.408 +
   1.409 +
   1.410 +#if CONFIG_MULTI_RES_ENCODING
   1.411 +static
   1.412 +void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
   1.413 +                               int *parent_ref_frame,
   1.414 +                               MB_PREDICTION_MODE *parent_mode,
   1.415 +                               int_mv *parent_ref_mv, int mb_row, int mb_col)
   1.416 +{
   1.417 +    LOWER_RES_MB_INFO* store_mode_info
   1.418 +                          = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
   1.419 +    unsigned int parent_mb_index;
   1.420 +
   1.421 +    /* Consider different down_sampling_factor.  */
   1.422 +    {
   1.423 +        /* TODO: Removed the loop that supports special down_sampling_factor
   1.424 +         * such as 2, 4, 8. Will revisit it if needed.
   1.425 +         * Should also try using a look-up table to see if it helps
   1.426 +         * performance. */
   1.427 +        int parent_mb_row, parent_mb_col;
   1.428 +
   1.429 +        parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
   1.430 +                    /cpi->oxcf.mr_down_sampling_factor.num;
   1.431 +        parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
   1.432 +                    /cpi->oxcf.mr_down_sampling_factor.num;
   1.433 +        parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
   1.434 +    }
   1.435 +
   1.436 +    /* Read lower-resolution mode & motion result from memory.*/
   1.437 +    *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
   1.438 +    *parent_mode =  store_mode_info[parent_mb_index].mode;
   1.439 +    *dissim = store_mode_info[parent_mb_index].dissim;
   1.440 +
   1.441 +    /* For highest-resolution encoder, adjust dissim value. Lower its quality
   1.442 +     * for good performance. */
   1.443 +    if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
   1.444 +        *dissim>>=1;
   1.445 +
   1.446 +    if(*parent_ref_frame != INTRA_FRAME)
   1.447 +    {
   1.448 +        /* Consider different down_sampling_factor.
   1.449 +         * The result can be rounded to be more precise, but it takes more time.
   1.450 +         */
   1.451 +        (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
   1.452 +                                  *cpi->oxcf.mr_down_sampling_factor.num
   1.453 +                                  /cpi->oxcf.mr_down_sampling_factor.den;
   1.454 +        (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
   1.455 +                                  *cpi->oxcf.mr_down_sampling_factor.num
   1.456 +                                  /cpi->oxcf.mr_down_sampling_factor.den;
   1.457 +
   1.458 +        vp8_clamp_mv2(parent_ref_mv, xd);
   1.459 +    }
   1.460 +}
   1.461 +#endif
   1.462 +
   1.463 +static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
   1.464 +{
   1.465 +    MACROBLOCKD *xd = &x->e_mbd;
   1.466 +
   1.467 +    unsigned int threshold = (xd->block[0].dequant[1]
   1.468 +        * xd->block[0].dequant[1] >>4);
   1.469 +
   1.470 +    if(threshold < x->encode_breakout)
   1.471 +        threshold = x->encode_breakout;
   1.472 +
   1.473 +    if (sse < threshold )
   1.474 +    {
   1.475 +        /* Check u and v to make sure skip is ok */
   1.476 +        unsigned int sse2 = 0;
   1.477 +
   1.478 +        sse2 = VP8_UVSSE(x);
   1.479 +
   1.480 +        if (sse2 * 2 < x->encode_breakout)
   1.481 +            x->skip = 1;
   1.482 +        else
   1.483 +            x->skip = 0;
   1.484 +    }
   1.485 +}
   1.486 +
   1.487 +static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
   1.488 +                               VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
   1.489 +{
   1.490 +    MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
   1.491 +    int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
   1.492 +    int this_rd;
   1.493 +    /* Exit early and don't compute the distortion if this macroblock
   1.494 +     * is marked inactive. */
   1.495 +    if (cpi->active_map_enabled && x->active_ptr[0] == 0)
   1.496 +    {
   1.497 +        *sse = 0;
   1.498 +        *distortion2 = 0;
   1.499 +        x->skip = 1;
   1.500 +        return INT_MAX;
   1.501 +    }
   1.502 +
   1.503 +    if((this_mode != NEWMV) ||
   1.504 +        !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
   1.505 +        *distortion2 = vp8_get_inter_mbpred_error(x,
   1.506 +                                              &cpi->fn_ptr[BLOCK_16X16],
   1.507 +                                              sse, mv);
   1.508 +
   1.509 +    this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
   1.510 +
   1.511 +    /* Adjust rd to bias to ZEROMV */
   1.512 +    if(this_mode == ZEROMV)
   1.513 +    {
   1.514 +        /* Bias to ZEROMV on LAST_FRAME reference when it is available. */
   1.515 +        if ((cpi->ref_frame_flags & VP8_LAST_FRAME &
   1.516 +            cpi->common.refresh_last_frame)
   1.517 +            && x->e_mbd.mode_info_context->mbmi.ref_frame != LAST_FRAME)
   1.518 +            rd_adj = 100;
   1.519 +
   1.520 +        // rd_adj <= 100
   1.521 +        this_rd = ((int64_t)this_rd) * rd_adj / 100;
   1.522 +    }
   1.523 +
   1.524 +    check_for_encode_breakout(*sse, x);
   1.525 +    return this_rd;
   1.526 +}
   1.527 +
   1.528 +static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
   1.529 +                                    int *rd_adjustment)
   1.530 +{
   1.531 +    MODE_INFO *mic = x->e_mbd.mode_info_context;
   1.532 +    int_mv mv_l, mv_a, mv_al;
   1.533 +    int local_motion_check = 0;
   1.534 +
   1.535 +    if (cpi->lf_zeromv_pct > 40)
   1.536 +    {
   1.537 +        /* left mb */
   1.538 +        mic -= 1;
   1.539 +        mv_l = mic->mbmi.mv;
   1.540 +
   1.541 +        if (mic->mbmi.ref_frame != INTRA_FRAME)
   1.542 +            if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
   1.543 +                local_motion_check++;
   1.544 +
   1.545 +        /* above-left mb */
   1.546 +        mic -= x->e_mbd.mode_info_stride;
   1.547 +        mv_al = mic->mbmi.mv;
   1.548 +
   1.549 +        if (mic->mbmi.ref_frame != INTRA_FRAME)
   1.550 +            if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
   1.551 +                local_motion_check++;
   1.552 +
   1.553 +        /* above mb */
   1.554 +        mic += 1;
   1.555 +        mv_a = mic->mbmi.mv;
   1.556 +
   1.557 +        if (mic->mbmi.ref_frame != INTRA_FRAME)
   1.558 +            if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
   1.559 +                local_motion_check++;
   1.560 +
   1.561 +        if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
   1.562 +            && local_motion_check >0) ||  local_motion_check >2 )
   1.563 +            *rd_adjustment = 80;
   1.564 +        else if (local_motion_check > 0)
   1.565 +            *rd_adjustment = 90;
   1.566 +    }
   1.567 +}
   1.568 +
   1.569 +void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
   1.570 +                         int recon_uvoffset, int *returnrate,
   1.571 +                         int *returndistortion, int *returnintra, int mb_row,
   1.572 +                         int mb_col)
   1.573 +{
   1.574 +    BLOCK *b = &x->block[0];
   1.575 +    BLOCKD *d = &x->e_mbd.block[0];
   1.576 +    MACROBLOCKD *xd = &x->e_mbd;
   1.577 +    MB_MODE_INFO best_mbmode;
   1.578 +
   1.579 +    int_mv best_ref_mv_sb[2];
   1.580 +    int_mv mode_mv_sb[2][MB_MODE_COUNT];
   1.581 +    int_mv best_ref_mv;
   1.582 +    int_mv *mode_mv;
   1.583 +    MB_PREDICTION_MODE this_mode;
   1.584 +    int num00;
   1.585 +    int mdcounts[4];
   1.586 +    int best_rd = INT_MAX;
   1.587 +    int rd_adjustment = 100;
   1.588 +    int best_intra_rd = INT_MAX;
   1.589 +    int mode_index;
   1.590 +    int rate;
   1.591 +    int rate2;
   1.592 +    int distortion2;
   1.593 +    int bestsme = INT_MAX;
   1.594 +    int best_mode_index = 0;
   1.595 +    unsigned int sse = INT_MAX, best_rd_sse = INT_MAX;
   1.596 +#if CONFIG_TEMPORAL_DENOISING
   1.597 +    unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX;
   1.598 +#endif
   1.599 +
   1.600 +    int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
   1.601 +    int_mv mvp;
   1.602 +
   1.603 +    int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
   1.604 +    int saddone=0;
   1.605 +    /* search range got from mv_pred(). It uses step_param levels. (0-7) */
   1.606 +    int sr=0;
   1.607 +
   1.608 +    unsigned char *plane[4][3];
   1.609 +    int ref_frame_map[4];
   1.610 +    int sign_bias = 0;
   1.611 +
   1.612 +#if CONFIG_MULTI_RES_ENCODING
   1.613 +    int dissim = INT_MAX;
   1.614 +    int parent_ref_frame = 0;
   1.615 +    int parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
   1.616 +    int_mv parent_ref_mv;
   1.617 +    MB_PREDICTION_MODE parent_mode = 0;
   1.618 +
   1.619 +    if (parent_ref_valid)
   1.620 +    {
   1.621 +        int parent_ref_flag;
   1.622 +
   1.623 +        get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
   1.624 +                                  &parent_mode, &parent_ref_mv, mb_row, mb_col);
   1.625 +
   1.626 +        /* TODO(jkoleszar): The references available (ref_frame_flags) to the
   1.627 +         * lower res encoder should match those available to this encoder, but
   1.628 +         * there seems to be a situation where this mismatch can happen in the
   1.629 +         * case of frame dropping and temporal layers. For example,
   1.630 +         * GOLD being disallowed in ref_frame_flags, but being returned as
   1.631 +         * parent_ref_frame.
   1.632 +         *
   1.633 +         * In this event, take the conservative approach of disabling the
   1.634 +         * lower res info for this MB.
   1.635 +         */
   1.636 +        parent_ref_flag = 0;
   1.637 +        if (parent_ref_frame == LAST_FRAME)
   1.638 +            parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
   1.639 +        else if (parent_ref_frame == GOLDEN_FRAME)
   1.640 +            parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
   1.641 +        else if (parent_ref_frame == ALTREF_FRAME)
   1.642 +            parent_ref_flag = (cpi->ref_frame_flags & VP8_ALTR_FRAME);
   1.643 +
   1.644 +        //assert(!parent_ref_frame || parent_ref_flag);
   1.645 +        if (parent_ref_frame && !parent_ref_flag)
   1.646 +            parent_ref_valid = 0;
   1.647 +    }
   1.648 +#endif
   1.649 +
   1.650 +    mode_mv = mode_mv_sb[sign_bias];
   1.651 +    best_ref_mv.as_int = 0;
   1.652 +    vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
   1.653 +    vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
   1.654 +
   1.655 +    /* Setup search priorities */
   1.656 +#if CONFIG_MULTI_RES_ENCODING
   1.657 +    if (parent_ref_valid && parent_ref_frame && dissim < 8)
   1.658 +    {
   1.659 +        ref_frame_map[0] = -1;
   1.660 +        ref_frame_map[1] = parent_ref_frame;
   1.661 +        ref_frame_map[2] = -1;
   1.662 +        ref_frame_map[3] = -1;
   1.663 +    } else
   1.664 +#endif
   1.665 +    get_reference_search_order(cpi, ref_frame_map);
   1.666 +
   1.667 +    /* Check to see if there is at least 1 valid reference frame that we need
   1.668 +     * to calculate near_mvs.
   1.669 +     */
   1.670 +    if (ref_frame_map[1] > 0)
   1.671 +    {
   1.672 +        sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
   1.673 +                                           x->e_mbd.mode_info_context,
   1.674 +                                           mode_mv_sb,
   1.675 +                                           best_ref_mv_sb,
   1.676 +                                           mdcounts,
   1.677 +                                           ref_frame_map[1],
   1.678 +                                           cpi->common.ref_frame_sign_bias);
   1.679 +
   1.680 +        mode_mv = mode_mv_sb[sign_bias];
   1.681 +        best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
   1.682 +    }
   1.683 +
   1.684 +    get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
   1.685 +
   1.686 +    /* Count of the number of MBs tested so far this frame */
   1.687 +    x->mbs_tested_so_far++;
   1.688 +
   1.689 +    *returnintra = INT_MAX;
   1.690 +    x->skip = 0;
   1.691 +
   1.692 +    x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
   1.693 +
   1.694 +    /* If the frame has big static background and current MB is in low
   1.695 +     * motion area, its mode decision is biased to ZEROMV mode.
   1.696 +     */
   1.697 +    calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
   1.698 +
   1.699 +    /* if we encode a new mv this is important
   1.700 +     * find the best new motion vector
   1.701 +     */
   1.702 +    for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
   1.703 +    {
   1.704 +        int frame_cost;
   1.705 +        int this_rd = INT_MAX;
   1.706 +        int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
   1.707 +
   1.708 +        if (best_rd <= x->rd_threshes[mode_index])
   1.709 +            continue;
   1.710 +
   1.711 +        if (this_ref_frame < 0)
   1.712 +            continue;
   1.713 +
   1.714 +        x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
   1.715 +
   1.716 +        /* everything but intra */
   1.717 +        if (x->e_mbd.mode_info_context->mbmi.ref_frame)
   1.718 +        {
   1.719 +            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
   1.720 +            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
   1.721 +            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
   1.722 +
   1.723 +            if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
   1.724 +            {
   1.725 +                sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
   1.726 +                mode_mv = mode_mv_sb[sign_bias];
   1.727 +                best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
   1.728 +            }
   1.729 +
   1.730 +#if CONFIG_MULTI_RES_ENCODING
   1.731 +            if (parent_ref_valid)
   1.732 +            {
   1.733 +                if (vp8_mode_order[mode_index] == NEARESTMV &&
   1.734 +                    mode_mv[NEARESTMV].as_int ==0)
   1.735 +                    continue;
   1.736 +                if (vp8_mode_order[mode_index] == NEARMV &&
   1.737 +                    mode_mv[NEARMV].as_int ==0)
   1.738 +                    continue;
   1.739 +
   1.740 +                if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
   1.741 +                    && best_ref_mv.as_int==0)
   1.742 +                    continue;
   1.743 +                else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
   1.744 +                    && best_ref_mv.as_int==parent_ref_mv.as_int)
   1.745 +                    continue;
   1.746 +            }
   1.747 +#endif
   1.748 +        }
   1.749 +
   1.750 +        /* Check to see if the testing frequency for this mode is at its max
   1.751 +         * If so then prevent it from being tested and increase the threshold
   1.752 +         * for its testing */
   1.753 +        if (x->mode_test_hit_counts[mode_index] &&
   1.754 +                                         (cpi->mode_check_freq[mode_index] > 1))
   1.755 +        {
   1.756 +            if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
   1.757 +                                         x->mode_test_hit_counts[mode_index]))
   1.758 +            {
   1.759 +                /* Increase the threshold for coding this mode to make it less
   1.760 +                 * likely to be chosen */
   1.761 +                x->rd_thresh_mult[mode_index] += 4;
   1.762 +
   1.763 +                if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
   1.764 +                    x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
   1.765 +
   1.766 +                x->rd_threshes[mode_index] =
   1.767 +                                 (cpi->rd_baseline_thresh[mode_index] >> 7) *
   1.768 +                                 x->rd_thresh_mult[mode_index];
   1.769 +                continue;
   1.770 +            }
   1.771 +        }
   1.772 +
   1.773 +        /* We have now reached the point where we are going to test the current
   1.774 +         * mode so increment the counter for the number of times it has been
   1.775 +         * tested */
   1.776 +        x->mode_test_hit_counts[mode_index] ++;
   1.777 +
   1.778 +        rate2 = 0;
   1.779 +        distortion2 = 0;
   1.780 +
   1.781 +        this_mode = vp8_mode_order[mode_index];
   1.782 +
   1.783 +        x->e_mbd.mode_info_context->mbmi.mode = this_mode;
   1.784 +        x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1.785 +
   1.786 +        /* Work out the cost assosciated with selecting the reference frame */
   1.787 +        frame_cost =
   1.788 +            x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
   1.789 +        rate2 += frame_cost;
   1.790 +
   1.791 +        /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
   1.792 +         * unless ARNR filtering is enabled in which case we want
   1.793 +         * an unfiltered alternative */
   1.794 +        if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
   1.795 +        {
   1.796 +            if (this_mode != ZEROMV ||
   1.797 +                x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
   1.798 +                continue;
   1.799 +        }
   1.800 +
   1.801 +        switch (this_mode)
   1.802 +        {
   1.803 +        case B_PRED:
   1.804 +            /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
   1.805 +            distortion2 = best_rd_sse;
   1.806 +            pick_intra4x4mby_modes(x, &rate, &distortion2);
   1.807 +
   1.808 +            if (distortion2 == INT_MAX)
   1.809 +            {
   1.810 +                this_rd = INT_MAX;
   1.811 +            }
   1.812 +            else
   1.813 +            {
   1.814 +                rate2 += rate;
   1.815 +                distortion2 = vp8_variance16x16(
   1.816 +                                    *(b->base_src), b->src_stride,
   1.817 +                                    x->e_mbd.predictor, 16, &sse);
   1.818 +                this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
   1.819 +
   1.820 +                if (this_rd < best_intra_rd)
   1.821 +                {
   1.822 +                    best_intra_rd = this_rd;
   1.823 +                    *returnintra = distortion2;
   1.824 +                }
   1.825 +            }
   1.826 +
   1.827 +            break;
   1.828 +
   1.829 +        case SPLITMV:
   1.830 +
   1.831 +            /* Split MV modes currently not supported when RD is not enabled. */
   1.832 +            break;
   1.833 +
   1.834 +        case DC_PRED:
   1.835 +        case V_PRED:
   1.836 +        case H_PRED:
   1.837 +        case TM_PRED:
   1.838 +            vp8_build_intra_predictors_mby_s(xd,
   1.839 +                                             xd->dst.y_buffer - xd->dst.y_stride,
   1.840 +                                             xd->dst.y_buffer - 1,
   1.841 +                                             xd->dst.y_stride,
   1.842 +                                             xd->predictor,
   1.843 +                                             16);
   1.844 +            distortion2 = vp8_variance16x16
   1.845 +                                          (*(b->base_src), b->src_stride,
   1.846 +                                          x->e_mbd.predictor, 16, &sse);
   1.847 +            rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
   1.848 +            this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
   1.849 +
   1.850 +            if (this_rd < best_intra_rd)
   1.851 +            {
   1.852 +                best_intra_rd = this_rd;
   1.853 +                *returnintra = distortion2;
   1.854 +            }
   1.855 +            break;
   1.856 +
   1.857 +        case NEWMV:
   1.858 +        {
   1.859 +            int thissme;
   1.860 +            int step_param;
   1.861 +            int further_steps;
   1.862 +            int n = 0;
   1.863 +            int sadpb = x->sadperbit16;
   1.864 +            int_mv mvp_full;
   1.865 +
   1.866 +            int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
   1.867 +            int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
   1.868 +            int col_max = (best_ref_mv.as_mv.col>>3)
   1.869 +                         + MAX_FULL_PEL_VAL;
   1.870 +            int row_max = (best_ref_mv.as_mv.row>>3)
   1.871 +                         + MAX_FULL_PEL_VAL;
   1.872 +
   1.873 +            int tmp_col_min = x->mv_col_min;
   1.874 +            int tmp_col_max = x->mv_col_max;
   1.875 +            int tmp_row_min = x->mv_row_min;
   1.876 +            int tmp_row_max = x->mv_row_max;
   1.877 +
   1.878 +            int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
   1.879 +
   1.880 +            /* Further step/diamond searches as necessary */
   1.881 +            step_param = cpi->sf.first_step + speed_adjust;
   1.882 +
   1.883 +#if CONFIG_MULTI_RES_ENCODING
   1.884 +            /* If lower-res drops this frame, then higher-res encoder does
   1.885 +               motion search without any previous knowledge. Also, since
   1.886 +               last frame motion info is not stored, then we can not
   1.887 +               use improved_mv_pred. */
   1.888 +            if (cpi->oxcf.mr_encoder_id && !parent_ref_valid)
   1.889 +                sf_improved_mv_pred = 0;
   1.890 +
   1.891 +            if (parent_ref_valid && parent_ref_frame)
   1.892 +            {
   1.893 +                /* Use parent MV as predictor. Adjust search range
   1.894 +                 * accordingly.
   1.895 +                 */
   1.896 +                mvp.as_int = parent_ref_mv.as_int;
   1.897 +                mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
   1.898 +                mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
   1.899 +
   1.900 +                if(dissim <=32) step_param += 3;
   1.901 +                else if(dissim <=128) step_param += 2;
   1.902 +                else step_param += 1;
   1.903 +            }else
   1.904 +#endif
   1.905 +            {
   1.906 +                if(sf_improved_mv_pred)
   1.907 +                {
   1.908 +                    if(!saddone)
   1.909 +                    {
   1.910 +                        vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
   1.911 +                        saddone = 1;
   1.912 +                    }
   1.913 +
   1.914 +                    vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
   1.915 +                                &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
   1.916 +                                cpi->common.ref_frame_sign_bias, &sr,
   1.917 +                                &near_sadidx[0]);
   1.918 +
   1.919 +                    sr += speed_adjust;
   1.920 +                    /* adjust search range according to sr from mv prediction */
   1.921 +                    if(sr > step_param)
   1.922 +                        step_param = sr;
   1.923 +
   1.924 +                    mvp_full.as_mv.col = mvp.as_mv.col>>3;
   1.925 +                    mvp_full.as_mv.row = mvp.as_mv.row>>3;
   1.926 +                }else
   1.927 +                {
   1.928 +                    mvp.as_int = best_ref_mv.as_int;
   1.929 +                    mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
   1.930 +                    mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
   1.931 +                }
   1.932 +            }
   1.933 +
   1.934 +#if CONFIG_MULTI_RES_ENCODING
   1.935 +            if (parent_ref_valid && parent_ref_frame && dissim <= 2 &&
   1.936 +                MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
   1.937 +                    abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
   1.938 +            {
   1.939 +                d->bmi.mv.as_int = mvp_full.as_int;
   1.940 +                mode_mv[NEWMV].as_int = mvp_full.as_int;
   1.941 +
   1.942 +                cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
   1.943 +                                             x->errorperbit,
   1.944 +                                             &cpi->fn_ptr[BLOCK_16X16],
   1.945 +                                             cpi->mb.mvcost,
   1.946 +                                             &distortion2,&sse);
   1.947 +            }else
   1.948 +#endif
   1.949 +            {
   1.950 +                /* Get intersection of UMV window and valid MV window to
   1.951 +                 * reduce # of checks in diamond search. */
   1.952 +                if (x->mv_col_min < col_min )
   1.953 +                    x->mv_col_min = col_min;
   1.954 +                if (x->mv_col_max > col_max )
   1.955 +                    x->mv_col_max = col_max;
   1.956 +                if (x->mv_row_min < row_min )
   1.957 +                    x->mv_row_min = row_min;
   1.958 +                if (x->mv_row_max > row_max )
   1.959 +                    x->mv_row_max = row_max;
   1.960 +
   1.961 +                further_steps = (cpi->Speed >= 8)?
   1.962 +                           0: (cpi->sf.max_step_search_steps - 1 - step_param);
   1.963 +
   1.964 +                if (cpi->sf.search_method == HEX)
   1.965 +                {
   1.966 +#if CONFIG_MULTI_RES_ENCODING
   1.967 +                /* TODO: In higher-res pick_inter_mode, step_param is used to
   1.968 +                 * modify hex search range. Here, set step_param to 0 not to
   1.969 +                 * change the behavior in lowest-resolution encoder.
   1.970 +                 * Will improve it later.
   1.971 +                 */
   1.972 +                 /* Set step_param to 0 to ensure large-range motion search
   1.973 +                    when encoder drops this frame at lower-resolution.
   1.974 +                  */
   1.975 +                if (!parent_ref_valid)
   1.976 +                    step_param = 0;
   1.977 +#endif
   1.978 +                    bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
   1.979 +                                          step_param, sadpb,
   1.980 +                                          &cpi->fn_ptr[BLOCK_16X16],
   1.981 +                                          x->mvsadcost, x->mvcost, &best_ref_mv);
   1.982 +                    mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1.983 +                }
   1.984 +                else
   1.985 +                {
   1.986 +                    bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
   1.987 +                                          &d->bmi.mv, step_param, sadpb, &num00,
   1.988 +                                          &cpi->fn_ptr[BLOCK_16X16],
   1.989 +                                          x->mvcost, &best_ref_mv);
   1.990 +                    mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1.991 +
   1.992 +                    /* Further step/diamond searches as necessary */
   1.993 +                    n = num00;
   1.994 +                    num00 = 0;
   1.995 +
   1.996 +                    while (n < further_steps)
   1.997 +                    {
   1.998 +                        n++;
   1.999 +
  1.1000 +                        if (num00)
  1.1001 +                            num00--;
  1.1002 +                        else
  1.1003 +                        {
  1.1004 +                            thissme =
  1.1005 +                            cpi->diamond_search_sad(x, b, d, &mvp_full,
  1.1006 +                                                    &d->bmi.mv,
  1.1007 +                                                    step_param + n,
  1.1008 +                                                    sadpb, &num00,
  1.1009 +                                                    &cpi->fn_ptr[BLOCK_16X16],
  1.1010 +                                                    x->mvcost, &best_ref_mv);
  1.1011 +                            if (thissme < bestsme)
  1.1012 +                            {
  1.1013 +                                bestsme = thissme;
  1.1014 +                                mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1.1015 +                            }
  1.1016 +                            else
  1.1017 +                            {
  1.1018 +                                d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
  1.1019 +                            }
  1.1020 +                        }
  1.1021 +                    }
  1.1022 +                }
  1.1023 +
  1.1024 +                x->mv_col_min = tmp_col_min;
  1.1025 +                x->mv_col_max = tmp_col_max;
  1.1026 +                x->mv_row_min = tmp_row_min;
  1.1027 +                x->mv_row_max = tmp_row_max;
  1.1028 +
  1.1029 +                if (bestsme < INT_MAX)
  1.1030 +                    cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
  1.1031 +                                             &best_ref_mv, x->errorperbit,
  1.1032 +                                             &cpi->fn_ptr[BLOCK_16X16],
  1.1033 +                                             cpi->mb.mvcost,
  1.1034 +                                             &distortion2,&sse);
  1.1035 +            }
  1.1036 +
  1.1037 +            mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  1.1038 +
  1.1039 +            /* mv cost; */
  1.1040 +            rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
  1.1041 +                                     cpi->mb.mvcost, 128);
  1.1042 +        }
  1.1043 +
  1.1044 +        case NEARESTMV:
  1.1045 +        case NEARMV:
  1.1046 +
  1.1047 +            if (mode_mv[this_mode].as_int == 0)
  1.1048 +                continue;
  1.1049 +
  1.1050 +        case ZEROMV:
  1.1051 +
  1.1052 +            /* Trap vectors that reach beyond the UMV borders
  1.1053 +             * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
  1.1054 +             * through to this point because of the lack of break statements
  1.1055 +             * in the previous two cases.
  1.1056 +             */
  1.1057 +            if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
  1.1058 +                ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
  1.1059 +                ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
  1.1060 +                ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
  1.1061 +                continue;
  1.1062 +
  1.1063 +            rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
  1.1064 +            x->e_mbd.mode_info_context->mbmi.mv.as_int =
  1.1065 +                                                    mode_mv[this_mode].as_int;
  1.1066 +            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
  1.1067 +                                          rd_adjustment);
  1.1068 +
  1.1069 +            break;
  1.1070 +        default:
  1.1071 +            break;
  1.1072 +        }
  1.1073 +
  1.1074 +#if CONFIG_TEMPORAL_DENOISING
  1.1075 +        if (cpi->oxcf.noise_sensitivity)
  1.1076 +        {
  1.1077 +
  1.1078 +            /* Store for later use by denoiser. */
  1.1079 +            if (this_mode == ZEROMV && sse < zero_mv_sse )
  1.1080 +            {
  1.1081 +                zero_mv_sse = sse;
  1.1082 +                x->best_zeromv_reference_frame =
  1.1083 +                        x->e_mbd.mode_info_context->mbmi.ref_frame;
  1.1084 +            }
  1.1085 +
  1.1086 +            /* Store the best NEWMV in x for later use in the denoiser. */
  1.1087 +            if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
  1.1088 +                    sse < best_sse)
  1.1089 +            {
  1.1090 +                best_sse = sse;
  1.1091 +                x->best_sse_inter_mode = NEWMV;
  1.1092 +                x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
  1.1093 +                x->need_to_clamp_best_mvs =
  1.1094 +                    x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
  1.1095 +                x->best_reference_frame =
  1.1096 +                    x->e_mbd.mode_info_context->mbmi.ref_frame;
  1.1097 +            }
  1.1098 +        }
  1.1099 +#endif
  1.1100 +
  1.1101 +        if (this_rd < best_rd || x->skip)
  1.1102 +        {
  1.1103 +            /* Note index of best mode */
  1.1104 +            best_mode_index = mode_index;
  1.1105 +
  1.1106 +            *returnrate = rate2;
  1.1107 +            *returndistortion = distortion2;
  1.1108 +            best_rd_sse = sse;
  1.1109 +            best_rd = this_rd;
  1.1110 +            vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
  1.1111 +                       sizeof(MB_MODE_INFO));
  1.1112 +
  1.1113 +            /* Testing this mode gave rise to an improvement in best error
  1.1114 +             * score. Lower threshold a bit for next time
  1.1115 +             */
  1.1116 +            x->rd_thresh_mult[mode_index] =
  1.1117 +                     (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
  1.1118 +                     x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
  1.1119 +            x->rd_threshes[mode_index] =
  1.1120 +                                   (cpi->rd_baseline_thresh[mode_index] >> 7) *
  1.1121 +                                   x->rd_thresh_mult[mode_index];
  1.1122 +        }
  1.1123 +
  1.1124 +        /* If the mode did not help improve the best error case then raise the
  1.1125 +         * threshold for testing that mode next time around.
  1.1126 +         */
  1.1127 +        else
  1.1128 +        {
  1.1129 +            x->rd_thresh_mult[mode_index] += 4;
  1.1130 +
  1.1131 +            if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
  1.1132 +                x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
  1.1133 +
  1.1134 +            x->rd_threshes[mode_index] =
  1.1135 +                         (cpi->rd_baseline_thresh[mode_index] >> 7) *
  1.1136 +                         x->rd_thresh_mult[mode_index];
  1.1137 +        }
  1.1138 +
  1.1139 +        if (x->skip)
  1.1140 +            break;
  1.1141 +    }
  1.1142 +
  1.1143 +    /* Reduce the activation RD thresholds for the best choice mode */
  1.1144 +    if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
  1.1145 +    {
  1.1146 +        int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
  1.1147 +
  1.1148 +        x->rd_thresh_mult[best_mode_index] =
  1.1149 +                        (x->rd_thresh_mult[best_mode_index]
  1.1150 +                        >= (MIN_THRESHMULT + best_adjustment)) ?
  1.1151 +                        x->rd_thresh_mult[best_mode_index] - best_adjustment :
  1.1152 +                        MIN_THRESHMULT;
  1.1153 +        x->rd_threshes[best_mode_index] =
  1.1154 +                        (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
  1.1155 +                        x->rd_thresh_mult[best_mode_index];
  1.1156 +    }
  1.1157 +
  1.1158 +
  1.1159 +    {
  1.1160 +        int this_rdbin = (*returndistortion >> 7);
  1.1161 +
  1.1162 +        if (this_rdbin >= 1024)
  1.1163 +        {
  1.1164 +            this_rdbin = 1023;
  1.1165 +        }
  1.1166 +
  1.1167 +        x->error_bins[this_rdbin] ++;
  1.1168 +    }
  1.1169 +
  1.1170 +#if CONFIG_TEMPORAL_DENOISING
  1.1171 +    if (cpi->oxcf.noise_sensitivity)
  1.1172 +    {
  1.1173 +        if (x->best_sse_inter_mode == DC_PRED)
  1.1174 +        {
  1.1175 +            /* No best MV found. */
  1.1176 +            x->best_sse_inter_mode = best_mbmode.mode;
  1.1177 +            x->best_sse_mv = best_mbmode.mv;
  1.1178 +            x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
  1.1179 +            x->best_reference_frame = best_mbmode.ref_frame;
  1.1180 +            best_sse = best_rd_sse;
  1.1181 +        }
  1.1182 +        vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
  1.1183 +                                recon_yoffset, recon_uvoffset);
  1.1184 +
  1.1185 +
  1.1186 +        /* Reevaluate ZEROMV after denoising. */
  1.1187 +        if (best_mbmode.ref_frame == INTRA_FRAME &&
  1.1188 +            x->best_zeromv_reference_frame != INTRA_FRAME)
  1.1189 +        {
  1.1190 +            int this_rd = 0;
  1.1191 +            int this_ref_frame = x->best_zeromv_reference_frame;
  1.1192 +            rate2 = x->ref_frame_cost[this_ref_frame] +
  1.1193 +                    vp8_cost_mv_ref(ZEROMV, mdcounts);
  1.1194 +            distortion2 = 0;
  1.1195 +
  1.1196 +            /* set up the proper prediction buffers for the frame */
  1.1197 +            x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
  1.1198 +            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
  1.1199 +            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
  1.1200 +            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
  1.1201 +
  1.1202 +            x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  1.1203 +            x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  1.1204 +            x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  1.1205 +            this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
  1.1206 +                                          rd_adjustment);
  1.1207 +
  1.1208 +            if (this_rd < best_rd)
  1.1209 +            {
  1.1210 +                vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
  1.1211 +                           sizeof(MB_MODE_INFO));
  1.1212 +            }
  1.1213 +        }
  1.1214 +
  1.1215 +    }
  1.1216 +#endif
  1.1217 +
  1.1218 +    if (cpi->is_src_frame_alt_ref &&
  1.1219 +        (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
  1.1220 +    {
  1.1221 +        x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  1.1222 +        x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
  1.1223 +        x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  1.1224 +        x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  1.1225 +        x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
  1.1226 +                                        (cpi->common.mb_no_coeff_skip);
  1.1227 +        x->e_mbd.mode_info_context->mbmi.partitioning = 0;
  1.1228 +
  1.1229 +        return;
  1.1230 +    }
  1.1231 +
  1.1232 +    /* set to the best mb mode, this copy can be skip if x->skip since it
  1.1233 +     * already has the right content */
  1.1234 +    if (!x->skip)
  1.1235 +        vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
  1.1236 +                   sizeof(MB_MODE_INFO));
  1.1237 +
  1.1238 +    if (best_mbmode.mode <= B_PRED)
  1.1239 +    {
  1.1240 +        /* set mode_info_context->mbmi.uv_mode */
  1.1241 +        pick_intra_mbuv_mode(x);
  1.1242 +    }
  1.1243 +
  1.1244 +    if (sign_bias
  1.1245 +      != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
  1.1246 +        best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
  1.1247 +
  1.1248 +    update_mvcount(x, &best_ref_mv);
  1.1249 +}
  1.1250 +
  1.1251 +
  1.1252 +void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
  1.1253 +{
  1.1254 +    int error4x4, error16x16 = INT_MAX;
  1.1255 +    int rate, best_rate = 0, distortion, best_sse;
  1.1256 +    MB_PREDICTION_MODE mode, best_mode = DC_PRED;
  1.1257 +    int this_rd;
  1.1258 +    unsigned int sse;
  1.1259 +    BLOCK *b = &x->block[0];
  1.1260 +    MACROBLOCKD *xd = &x->e_mbd;
  1.1261 +
  1.1262 +    xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
  1.1263 +
  1.1264 +    pick_intra_mbuv_mode(x);
  1.1265 +
  1.1266 +    for (mode = DC_PRED; mode <= TM_PRED; mode ++)
  1.1267 +    {
  1.1268 +        xd->mode_info_context->mbmi.mode = mode;
  1.1269 +        vp8_build_intra_predictors_mby_s(xd,
  1.1270 +                                         xd->dst.y_buffer - xd->dst.y_stride,
  1.1271 +                                         xd->dst.y_buffer - 1,
  1.1272 +                                         xd->dst.y_stride,
  1.1273 +                                         xd->predictor,
  1.1274 +                                         16);
  1.1275 +        distortion = vp8_variance16x16
  1.1276 +            (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
  1.1277 +        rate = x->mbmode_cost[xd->frame_type][mode];
  1.1278 +        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  1.1279 +
  1.1280 +        if (error16x16 > this_rd)
  1.1281 +        {
  1.1282 +            error16x16 = this_rd;
  1.1283 +            best_mode = mode;
  1.1284 +            best_sse = sse;
  1.1285 +            best_rate = rate;
  1.1286 +        }
  1.1287 +    }
  1.1288 +    xd->mode_info_context->mbmi.mode = best_mode;
  1.1289 +
  1.1290 +    error4x4 = pick_intra4x4mby_modes(x, &rate,
  1.1291 +                                      &best_sse);
  1.1292 +    if (error4x4 < error16x16)
  1.1293 +    {
  1.1294 +        xd->mode_info_context->mbmi.mode = B_PRED;
  1.1295 +        best_rate = rate;
  1.1296 +    }
  1.1297 +
  1.1298 +    *rate_ = best_rate;
  1.1299 +}

mercurial