media/libvpx/vp9/encoder/vp9_encodeframe.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp9/encoder/vp9_encodeframe.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2537 @@
     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 +#include <limits.h>
    1.15 +#include <math.h>
    1.16 +#include <stdio.h>
    1.17 +
    1.18 +#include "./vp9_rtcd.h"
    1.19 +#include "./vpx_config.h"
    1.20 +
    1.21 +#include "vpx_ports/vpx_timer.h"
    1.22 +
    1.23 +#include "vp9/common/vp9_common.h"
    1.24 +#include "vp9/common/vp9_entropy.h"
    1.25 +#include "vp9/common/vp9_entropymode.h"
    1.26 +#include "vp9/common/vp9_extend.h"
    1.27 +#include "vp9/common/vp9_findnearmv.h"
    1.28 +#include "vp9/common/vp9_idct.h"
    1.29 +#include "vp9/common/vp9_mvref_common.h"
    1.30 +#include "vp9/common/vp9_pred_common.h"
    1.31 +#include "vp9/common/vp9_quant_common.h"
    1.32 +#include "vp9/common/vp9_reconintra.h"
    1.33 +#include "vp9/common/vp9_reconinter.h"
    1.34 +#include "vp9/common/vp9_seg_common.h"
    1.35 +#include "vp9/common/vp9_tile_common.h"
    1.36 +#include "vp9/encoder/vp9_encodeframe.h"
    1.37 +#include "vp9/encoder/vp9_encodeintra.h"
    1.38 +#include "vp9/encoder/vp9_encodemb.h"
    1.39 +#include "vp9/encoder/vp9_encodemv.h"
    1.40 +#include "vp9/encoder/vp9_onyx_int.h"
    1.41 +#include "vp9/encoder/vp9_rdopt.h"
    1.42 +#include "vp9/encoder/vp9_segmentation.h"
    1.43 +#include "vp9/common/vp9_systemdependent.h"
    1.44 +#include "vp9/encoder/vp9_tokenize.h"
    1.45 +#include "vp9/encoder/vp9_vaq.h"
    1.46 +
    1.47 +
    1.48 +#define DBG_PRNT_SEGMAP 0
    1.49 +
    1.50 +
    1.51 +// #define ENC_DEBUG
    1.52 +#ifdef ENC_DEBUG
    1.53 +int enc_debug = 0;
    1.54 +#endif
    1.55 +
    1.56 +static INLINE uint8_t *get_sb_index(MACROBLOCK *x, BLOCK_SIZE subsize) {
    1.57 +  switch (subsize) {
    1.58 +    case BLOCK_64X64:
    1.59 +    case BLOCK_64X32:
    1.60 +    case BLOCK_32X64:
    1.61 +    case BLOCK_32X32:
    1.62 +      return &x->sb_index;
    1.63 +    case BLOCK_32X16:
    1.64 +    case BLOCK_16X32:
    1.65 +    case BLOCK_16X16:
    1.66 +      return &x->mb_index;
    1.67 +    case BLOCK_16X8:
    1.68 +    case BLOCK_8X16:
    1.69 +    case BLOCK_8X8:
    1.70 +      return &x->b_index;
    1.71 +    case BLOCK_8X4:
    1.72 +    case BLOCK_4X8:
    1.73 +    case BLOCK_4X4:
    1.74 +      return &x->ab_index;
    1.75 +    default:
    1.76 +      assert(0);
    1.77 +      return NULL;
    1.78 +  }
    1.79 +}
    1.80 +
    1.81 +static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
    1.82 +                              int mi_row, int mi_col, BLOCK_SIZE bsize);
    1.83 +
    1.84 +static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x);
    1.85 +
    1.86 +/* activity_avg must be positive, or flat regions could get a zero weight
    1.87 + *  (infinite lambda), which confounds analysis.
    1.88 + * This also avoids the need for divide by zero checks in
    1.89 + *  vp9_activity_masking().
    1.90 + */
    1.91 +#define ACTIVITY_AVG_MIN (64)
    1.92 +
    1.93 +/* Motion vector component magnitude threshold for defining fast motion. */
    1.94 +#define FAST_MOTION_MV_THRESH (24)
    1.95 +
    1.96 +/* This is used as a reference when computing the source variance for the
    1.97 + *  purposes of activity masking.
    1.98 + * Eventually this should be replaced by custom no-reference routines,
    1.99 + *  which will be faster.
   1.100 + */
   1.101 +static const uint8_t VP9_VAR_OFFS[64] = {
   1.102 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.103 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.104 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.105 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.106 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.107 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.108 +  128, 128, 128, 128, 128, 128, 128, 128,
   1.109 +  128, 128, 128, 128, 128, 128, 128, 128
   1.110 +};
   1.111 +
   1.112 +static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi, MACROBLOCK *x,
   1.113 +                                              BLOCK_SIZE bs) {
   1.114 +  unsigned int var, sse;
   1.115 +  var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
   1.116 +                           x->plane[0].src.stride,
   1.117 +                           VP9_VAR_OFFS, 0, &sse);
   1.118 +  return (var + (1 << (num_pels_log2_lookup[bs] - 1))) >>
   1.119 +      num_pels_log2_lookup[bs];
   1.120 +}
   1.121 +
   1.122 +// Original activity measure from Tim T's code.
   1.123 +static unsigned int tt_activity_measure(MACROBLOCK *x) {
   1.124 +  unsigned int act;
   1.125 +  unsigned int sse;
   1.126 +  /* TODO: This could also be done over smaller areas (8x8), but that would
   1.127 +   *  require extensive changes elsewhere, as lambda is assumed to be fixed
   1.128 +   *  over an entire MB in most of the code.
   1.129 +   * Another option is to compute four 8x8 variances, and pick a single
   1.130 +   *  lambda using a non-linear combination (e.g., the smallest, or second
   1.131 +   *  smallest, etc.).
   1.132 +   */
   1.133 +  act = vp9_variance16x16(x->plane[0].src.buf, x->plane[0].src.stride,
   1.134 +                          VP9_VAR_OFFS, 0, &sse);
   1.135 +  act <<= 4;
   1.136 +
   1.137 +  /* If the region is flat, lower the activity some more. */
   1.138 +  if (act < 8 << 12)
   1.139 +    act = act < 5 << 12 ? act : 5 << 12;
   1.140 +
   1.141 +  return act;
   1.142 +}
   1.143 +
   1.144 +// Stub for alternative experimental activity measures.
   1.145 +static unsigned int alt_activity_measure(MACROBLOCK *x, int use_dc_pred) {
   1.146 +  return vp9_encode_intra(x, use_dc_pred);
   1.147 +}
   1.148 +
   1.149 +// Measure the activity of the current macroblock
   1.150 +// What we measure here is TBD so abstracted to this function
   1.151 +#define ALT_ACT_MEASURE 1
   1.152 +static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) {
   1.153 +  unsigned int mb_activity;
   1.154 +
   1.155 +  if (ALT_ACT_MEASURE) {
   1.156 +    int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
   1.157 +
   1.158 +    // Or use and alternative.
   1.159 +    mb_activity = alt_activity_measure(x, use_dc_pred);
   1.160 +  } else {
   1.161 +    // Original activity measure from Tim T's code.
   1.162 +    mb_activity = tt_activity_measure(x);
   1.163 +  }
   1.164 +
   1.165 +  if (mb_activity < ACTIVITY_AVG_MIN)
   1.166 +    mb_activity = ACTIVITY_AVG_MIN;
   1.167 +
   1.168 +  return mb_activity;
   1.169 +}
   1.170 +
   1.171 +// Calculate an "average" mb activity value for the frame
   1.172 +#define ACT_MEDIAN 0
   1.173 +static void calc_av_activity(VP9_COMP *cpi, int64_t activity_sum) {
   1.174 +#if ACT_MEDIAN
   1.175 +  // Find median: Simple n^2 algorithm for experimentation
   1.176 +  {
   1.177 +    unsigned int median;
   1.178 +    unsigned int i, j;
   1.179 +    unsigned int *sortlist;
   1.180 +    unsigned int tmp;
   1.181 +
   1.182 +    // Create a list to sort to
   1.183 +    CHECK_MEM_ERROR(&cpi->common, sortlist, vpx_calloc(sizeof(unsigned int),
   1.184 +                    cpi->common.MBs));
   1.185 +
   1.186 +    // Copy map to sort list
   1.187 +    vpx_memcpy(sortlist, cpi->mb_activity_map,
   1.188 +        sizeof(unsigned int) * cpi->common.MBs);
   1.189 +
   1.190 +    // Ripple each value down to its correct position
   1.191 +    for (i = 1; i < cpi->common.MBs; i ++) {
   1.192 +      for (j = i; j > 0; j --) {
   1.193 +        if (sortlist[j] < sortlist[j - 1]) {
   1.194 +          // Swap values
   1.195 +          tmp = sortlist[j - 1];
   1.196 +          sortlist[j - 1] = sortlist[j];
   1.197 +          sortlist[j] = tmp;
   1.198 +        } else {
   1.199 +          break;
   1.200 +        }
   1.201 +      }
   1.202 +    }
   1.203 +
   1.204 +    // Even number MBs so estimate median as mean of two either side.
   1.205 +    median = (1 + sortlist[cpi->common.MBs >> 1] +
   1.206 +        sortlist[(cpi->common.MBs >> 1) + 1]) >> 1;
   1.207 +
   1.208 +    cpi->activity_avg = median;
   1.209 +
   1.210 +    vpx_free(sortlist);
   1.211 +  }
   1.212 +#else
   1.213 +  // Simple mean for now
   1.214 +  cpi->activity_avg = (unsigned int) (activity_sum / cpi->common.MBs);
   1.215 +#endif  // ACT_MEDIAN
   1.216 +
   1.217 +  if (cpi->activity_avg < ACTIVITY_AVG_MIN)
   1.218 +    cpi->activity_avg = ACTIVITY_AVG_MIN;
   1.219 +
   1.220 +  // Experimental code: return fixed value normalized for several clips
   1.221 +  if (ALT_ACT_MEASURE)
   1.222 +    cpi->activity_avg = 100000;
   1.223 +}
   1.224 +
   1.225 +#define USE_ACT_INDEX   0
   1.226 +#define OUTPUT_NORM_ACT_STATS   0
   1.227 +
   1.228 +#if USE_ACT_INDEX
   1.229 +// Calculate an activity index for each mb
   1.230 +static void calc_activity_index(VP9_COMP *cpi, MACROBLOCK *x) {
   1.231 +  VP9_COMMON *const cm = &cpi->common;
   1.232 +  int mb_row, mb_col;
   1.233 +
   1.234 +  int64_t act;
   1.235 +  int64_t a;
   1.236 +  int64_t b;
   1.237 +
   1.238 +#if OUTPUT_NORM_ACT_STATS
   1.239 +  FILE *f = fopen("norm_act.stt", "a");
   1.240 +  fprintf(f, "\n%12d\n", cpi->activity_avg);
   1.241 +#endif
   1.242 +
   1.243 +  // Reset pointers to start of activity map
   1.244 +  x->mb_activity_ptr = cpi->mb_activity_map;
   1.245 +
   1.246 +  // Calculate normalized mb activity number.
   1.247 +  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
   1.248 +    // for each macroblock col in image
   1.249 +    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
   1.250 +      // Read activity from the map
   1.251 +      act = *(x->mb_activity_ptr);
   1.252 +
   1.253 +      // Calculate a normalized activity number
   1.254 +      a = act + 4 * cpi->activity_avg;
   1.255 +      b = 4 * act + cpi->activity_avg;
   1.256 +
   1.257 +      if (b >= a)
   1.258 +      *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1;
   1.259 +      else
   1.260 +      *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b);
   1.261 +
   1.262 +#if OUTPUT_NORM_ACT_STATS
   1.263 +      fprintf(f, " %6d", *(x->mb_activity_ptr));
   1.264 +#endif
   1.265 +      // Increment activity map pointers
   1.266 +      x->mb_activity_ptr++;
   1.267 +    }
   1.268 +
   1.269 +#if OUTPUT_NORM_ACT_STATS
   1.270 +    fprintf(f, "\n");
   1.271 +#endif
   1.272 +  }
   1.273 +
   1.274 +#if OUTPUT_NORM_ACT_STATS
   1.275 +  fclose(f);
   1.276 +#endif
   1.277 +}
   1.278 +#endif  // USE_ACT_INDEX
   1.279 +
   1.280 +// Loop through all MBs. Note activity of each, average activity and
   1.281 +// calculate a normalized activity for each
   1.282 +static void build_activity_map(VP9_COMP *cpi) {
   1.283 +  MACROBLOCK * const x = &cpi->mb;
   1.284 +  MACROBLOCKD *xd = &x->e_mbd;
   1.285 +  VP9_COMMON * const cm = &cpi->common;
   1.286 +
   1.287 +#if ALT_ACT_MEASURE
   1.288 +  YV12_BUFFER_CONFIG *new_yv12 = get_frame_new_buffer(cm);
   1.289 +  int recon_yoffset;
   1.290 +  int recon_y_stride = new_yv12->y_stride;
   1.291 +#endif
   1.292 +
   1.293 +  int mb_row, mb_col;
   1.294 +  unsigned int mb_activity;
   1.295 +  int64_t activity_sum = 0;
   1.296 +
   1.297 +  x->mb_activity_ptr = cpi->mb_activity_map;
   1.298 +
   1.299 +  // for each macroblock row in image
   1.300 +  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
   1.301 +#if ALT_ACT_MEASURE
   1.302 +    // reset above block coeffs
   1.303 +    xd->up_available = (mb_row != 0);
   1.304 +    recon_yoffset = (mb_row * recon_y_stride * 16);
   1.305 +#endif
   1.306 +    // for each macroblock col in image
   1.307 +    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
   1.308 +#if ALT_ACT_MEASURE
   1.309 +      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
   1.310 +      xd->left_available = (mb_col != 0);
   1.311 +      recon_yoffset += 16;
   1.312 +#endif
   1.313 +
   1.314 +      // measure activity
   1.315 +      mb_activity = mb_activity_measure(x, mb_row, mb_col);
   1.316 +
   1.317 +      // Keep frame sum
   1.318 +      activity_sum += mb_activity;
   1.319 +
   1.320 +      // Store MB level activity details.
   1.321 +      *x->mb_activity_ptr = mb_activity;
   1.322 +
   1.323 +      // Increment activity map pointer
   1.324 +      x->mb_activity_ptr++;
   1.325 +
   1.326 +      // adjust to the next column of source macroblocks
   1.327 +      x->plane[0].src.buf += 16;
   1.328 +    }
   1.329 +
   1.330 +    // adjust to the next row of mbs
   1.331 +    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
   1.332 +  }
   1.333 +
   1.334 +  // Calculate an "average" MB activity
   1.335 +  calc_av_activity(cpi, activity_sum);
   1.336 +
   1.337 +#if USE_ACT_INDEX
   1.338 +  // Calculate an activity index number of each mb
   1.339 +  calc_activity_index(cpi, x);
   1.340 +#endif
   1.341 +}
   1.342 +
   1.343 +// Macroblock activity masking
   1.344 +void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x) {
   1.345 +#if USE_ACT_INDEX
   1.346 +  x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
   1.347 +  x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
   1.348 +  x->errorperbit += (x->errorperbit == 0);
   1.349 +#else
   1.350 +  int64_t a;
   1.351 +  int64_t b;
   1.352 +  int64_t act = *(x->mb_activity_ptr);
   1.353 +
   1.354 +  // Apply the masking to the RD multiplier.
   1.355 +  a = act + (2 * cpi->activity_avg);
   1.356 +  b = (2 * act) + cpi->activity_avg;
   1.357 +
   1.358 +  x->rdmult = (unsigned int) (((int64_t) x->rdmult * b + (a >> 1)) / a);
   1.359 +  x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
   1.360 +  x->errorperbit += (x->errorperbit == 0);
   1.361 +#endif
   1.362 +
   1.363 +  // Activity based Zbin adjustment
   1.364 +  adjust_act_zbin(cpi, x);
   1.365 +}
   1.366 +
   1.367 +static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
   1.368 +                         BLOCK_SIZE bsize, int output_enabled) {
   1.369 +  int i, x_idx, y;
   1.370 +  VP9_COMMON *const cm = &cpi->common;
   1.371 +  MACROBLOCK *const x = &cpi->mb;
   1.372 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.373 +  struct macroblock_plane *const p = x->plane;
   1.374 +  struct macroblockd_plane *const pd = xd->plane;
   1.375 +  MODE_INFO *mi = &ctx->mic;
   1.376 +  MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
   1.377 +  MODE_INFO *mi_addr = xd->mi_8x8[0];
   1.378 +
   1.379 +  int mb_mode_index = ctx->best_mode_index;
   1.380 +  const int mis = cm->mode_info_stride;
   1.381 +  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
   1.382 +  const int mi_height = num_8x8_blocks_high_lookup[bsize];
   1.383 +  int max_plane;
   1.384 +
   1.385 +  assert(mi->mbmi.mode < MB_MODE_COUNT);
   1.386 +  assert(mi->mbmi.ref_frame[0] < MAX_REF_FRAMES);
   1.387 +  assert(mi->mbmi.ref_frame[1] < MAX_REF_FRAMES);
   1.388 +  assert(mi->mbmi.sb_type == bsize);
   1.389 +
   1.390 +  *mi_addr = *mi;
   1.391 +
   1.392 +  max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
   1.393 +  for (i = 0; i < max_plane; ++i) {
   1.394 +    p[i].coeff = ctx->coeff_pbuf[i][1];
   1.395 +    pd[i].qcoeff = ctx->qcoeff_pbuf[i][1];
   1.396 +    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
   1.397 +    pd[i].eobs = ctx->eobs_pbuf[i][1];
   1.398 +  }
   1.399 +
   1.400 +  for (i = max_plane; i < MAX_MB_PLANE; ++i) {
   1.401 +    p[i].coeff = ctx->coeff_pbuf[i][2];
   1.402 +    pd[i].qcoeff = ctx->qcoeff_pbuf[i][2];
   1.403 +    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
   1.404 +    pd[i].eobs = ctx->eobs_pbuf[i][2];
   1.405 +  }
   1.406 +
   1.407 +  // Restore the coding context of the MB to that that was in place
   1.408 +  // when the mode was picked for it
   1.409 +  for (y = 0; y < mi_height; y++)
   1.410 +    for (x_idx = 0; x_idx < mi_width; x_idx++)
   1.411 +      if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
   1.412 +          && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y)
   1.413 +        xd->mi_8x8[x_idx + y * mis] = mi_addr;
   1.414 +
   1.415 +  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.416 +    vp9_mb_init_quantizer(cpi, x);
   1.417 +  }
   1.418 +
   1.419 +  // FIXME(rbultje) I'm pretty sure this should go to the end of this block
   1.420 +  // (i.e. after the output_enabled)
   1.421 +  if (bsize < BLOCK_32X32) {
   1.422 +    if (bsize < BLOCK_16X16)
   1.423 +      ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
   1.424 +    ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
   1.425 +  }
   1.426 +
   1.427 +  if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
   1.428 +    mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
   1.429 +    mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
   1.430 +  }
   1.431 +
   1.432 +  x->skip = ctx->skip;
   1.433 +  vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
   1.434 +             sizeof(uint8_t) * ctx->num_4x4_blk);
   1.435 +
   1.436 +  if (!output_enabled)
   1.437 +    return;
   1.438 +
   1.439 +  if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
   1.440 +    for (i = 0; i < TX_MODES; i++)
   1.441 +      cpi->rd_tx_select_diff[i] += ctx->tx_rd_diff[i];
   1.442 +  }
   1.443 +
   1.444 +  if (frame_is_intra_only(cm)) {
   1.445 +#if CONFIG_INTERNAL_STATS
   1.446 +    static const int kf_mode_index[] = {
   1.447 +      THR_DC /*DC_PRED*/,
   1.448 +      THR_V_PRED /*V_PRED*/,
   1.449 +      THR_H_PRED /*H_PRED*/,
   1.450 +      THR_D45_PRED /*D45_PRED*/,
   1.451 +      THR_D135_PRED /*D135_PRED*/,
   1.452 +      THR_D117_PRED /*D117_PRED*/,
   1.453 +      THR_D153_PRED /*D153_PRED*/,
   1.454 +      THR_D207_PRED /*D207_PRED*/,
   1.455 +      THR_D63_PRED /*D63_PRED*/,
   1.456 +      THR_TM /*TM_PRED*/,
   1.457 +    };
   1.458 +    cpi->mode_chosen_counts[kf_mode_index[mi->mbmi.mode]]++;
   1.459 +#endif
   1.460 +  } else {
   1.461 +    // Note how often each mode chosen as best
   1.462 +    cpi->mode_chosen_counts[mb_mode_index]++;
   1.463 +    if (is_inter_block(mbmi)
   1.464 +        && (mbmi->sb_type < BLOCK_8X8 || mbmi->mode == NEWMV)) {
   1.465 +      int_mv best_mv[2];
   1.466 +      const MV_REFERENCE_FRAME rf1 = mbmi->ref_frame[0];
   1.467 +      const MV_REFERENCE_FRAME rf2 = mbmi->ref_frame[1];
   1.468 +      best_mv[0].as_int = ctx->best_ref_mv.as_int;
   1.469 +      best_mv[1].as_int = ctx->second_best_ref_mv.as_int;
   1.470 +      if (mbmi->mode == NEWMV) {
   1.471 +        best_mv[0].as_int = mbmi->ref_mvs[rf1][0].as_int;
   1.472 +        if (rf2 > 0)
   1.473 +          best_mv[1].as_int = mbmi->ref_mvs[rf2][0].as_int;
   1.474 +      }
   1.475 +      mbmi->best_mv[0].as_int = best_mv[0].as_int;
   1.476 +      mbmi->best_mv[1].as_int = best_mv[1].as_int;
   1.477 +      vp9_update_mv_count(cpi, x, best_mv);
   1.478 +    }
   1.479 +
   1.480 +    if (cm->mcomp_filter_type == SWITCHABLE && is_inter_mode(mbmi->mode)) {
   1.481 +      const int ctx = vp9_get_pred_context_switchable_interp(xd);
   1.482 +      ++cm->counts.switchable_interp[ctx][mbmi->interp_filter];
   1.483 +    }
   1.484 +
   1.485 +    cpi->rd_comp_pred_diff[SINGLE_PREDICTION_ONLY] += ctx->single_pred_diff;
   1.486 +    cpi->rd_comp_pred_diff[COMP_PREDICTION_ONLY] += ctx->comp_pred_diff;
   1.487 +    cpi->rd_comp_pred_diff[HYBRID_PREDICTION] += ctx->hybrid_pred_diff;
   1.488 +
   1.489 +    for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
   1.490 +      cpi->rd_filter_diff[i] += ctx->best_filter_diff[i];
   1.491 +  }
   1.492 +}
   1.493 +
   1.494 +void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
   1.495 +                          int mi_row, int mi_col) {
   1.496 +  uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
   1.497 +                               src->alpha_buffer};
   1.498 +  const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
   1.499 +                          src->alpha_stride};
   1.500 +  int i;
   1.501 +
   1.502 +  for (i = 0; i < MAX_MB_PLANE; i++)
   1.503 +    setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
   1.504 +                     NULL, x->e_mbd.plane[i].subsampling_x,
   1.505 +                     x->e_mbd.plane[i].subsampling_y);
   1.506 +}
   1.507 +
   1.508 +static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
   1.509 +                        int mi_row, int mi_col, BLOCK_SIZE bsize) {
   1.510 +  MACROBLOCK *const x = &cpi->mb;
   1.511 +  VP9_COMMON *const cm = &cpi->common;
   1.512 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.513 +  MB_MODE_INFO *mbmi;
   1.514 +  const int dst_fb_idx = cm->new_fb_idx;
   1.515 +  const int idx_str = xd->mode_info_stride * mi_row + mi_col;
   1.516 +  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
   1.517 +  const int mi_height = num_8x8_blocks_high_lookup[bsize];
   1.518 +  const int mb_row = mi_row >> 1;
   1.519 +  const int mb_col = mi_col >> 1;
   1.520 +  const int idx_map = mb_row * cm->mb_cols + mb_col;
   1.521 +  const struct segmentation *const seg = &cm->seg;
   1.522 +
   1.523 +  set_skip_context(xd, cpi->above_context, cpi->left_context, mi_row, mi_col);
   1.524 +
   1.525 +  // Activity map pointer
   1.526 +  x->mb_activity_ptr = &cpi->mb_activity_map[idx_map];
   1.527 +  x->active_ptr = cpi->active_map + idx_map;
   1.528 +
   1.529 +  xd->mi_8x8 = cm->mi_grid_visible + idx_str;
   1.530 +  xd->prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
   1.531 +
   1.532 +  // Special case: if prev_mi is NULL, the previous mode info context
   1.533 +  // cannot be used.
   1.534 +  xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL;
   1.535 +
   1.536 +  xd->mi_8x8[0] = cm->mi + idx_str;
   1.537 +
   1.538 +  mbmi = &xd->mi_8x8[0]->mbmi;
   1.539 +
   1.540 +  // Set up destination pointers
   1.541 +  setup_dst_planes(xd, &cm->yv12_fb[dst_fb_idx], mi_row, mi_col);
   1.542 +
   1.543 +  // Set up limit values for MV components
   1.544 +  // mv beyond the range do not produce new/different prediction block
   1.545 +  x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
   1.546 +  x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
   1.547 +  x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
   1.548 +  x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
   1.549 +
   1.550 +  // Set up distance of MB to edge of frame in 1/8th pel units
   1.551 +  assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
   1.552 +  set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
   1.553 +                 cm->mi_rows, cm->mi_cols);
   1.554 +
   1.555 +  /* set up source buffers */
   1.556 +  vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
   1.557 +
   1.558 +  /* R/D setup */
   1.559 +  x->rddiv = cpi->RDDIV;
   1.560 +  x->rdmult = cpi->RDMULT;
   1.561 +
   1.562 +  /* segment ID */
   1.563 +  if (seg->enabled) {
   1.564 +    if (!cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.565 +      uint8_t *map = seg->update_map ? cpi->segmentation_map
   1.566 +          : cm->last_frame_seg_map;
   1.567 +      mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
   1.568 +    }
   1.569 +    vp9_mb_init_quantizer(cpi, x);
   1.570 +
   1.571 +    if (seg->enabled && cpi->seg0_cnt > 0
   1.572 +        && !vp9_segfeature_active(seg, 0, SEG_LVL_REF_FRAME)
   1.573 +        && vp9_segfeature_active(seg, 1, SEG_LVL_REF_FRAME)) {
   1.574 +      cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt;
   1.575 +    } else {
   1.576 +      const int y = mb_row & ~3;
   1.577 +      const int x = mb_col & ~3;
   1.578 +      const int p16 = ((mb_row & 1) << 1) + (mb_col & 1);
   1.579 +      const int p32 = ((mb_row & 2) << 2) + ((mb_col & 2) << 1);
   1.580 +      const int tile_progress = tile->mi_col_start * cm->mb_rows >> 1;
   1.581 +      const int mb_cols = (tile->mi_col_end - tile->mi_col_start) >> 1;
   1.582 +
   1.583 +      cpi->seg0_progress = ((y * mb_cols + x * 4 + p32 + p16 + tile_progress)
   1.584 +          << 16) / cm->MBs;
   1.585 +    }
   1.586 +
   1.587 +    x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
   1.588 +  } else {
   1.589 +    mbmi->segment_id = 0;
   1.590 +    x->encode_breakout = cpi->oxcf.encode_breakout;
   1.591 +  }
   1.592 +}
   1.593 +
   1.594 +static void pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
   1.595 +                          int mi_row, int mi_col,
   1.596 +                          int *totalrate, int64_t *totaldist,
   1.597 +                          BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
   1.598 +                          int64_t best_rd) {
   1.599 +  VP9_COMMON *const cm = &cpi->common;
   1.600 +  MACROBLOCK *const x = &cpi->mb;
   1.601 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.602 +  struct macroblock_plane *const p = x->plane;
   1.603 +  struct macroblockd_plane *const pd = xd->plane;
   1.604 +  int i;
   1.605 +  int orig_rdmult = x->rdmult;
   1.606 +  double rdmult_ratio;
   1.607 +
   1.608 +  vp9_clear_system_state();  // __asm emms;
   1.609 +  rdmult_ratio = 1.0;  // avoid uninitialized warnings
   1.610 +
   1.611 +  // Use the lower precision, but faster, 32x32 fdct for mode selection.
   1.612 +  x->use_lp32x32fdct = 1;
   1.613 +
   1.614 +  if (bsize < BLOCK_8X8) {
   1.615 +    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
   1.616 +    // there is nothing to be done.
   1.617 +    if (x->ab_index != 0) {
   1.618 +      *totalrate = 0;
   1.619 +      *totaldist = 0;
   1.620 +      return;
   1.621 +    }
   1.622 +  }
   1.623 +
   1.624 +  set_offsets(cpi, tile, mi_row, mi_col, bsize);
   1.625 +  xd->mi_8x8[0]->mbmi.sb_type = bsize;
   1.626 +
   1.627 +  for (i = 0; i < MAX_MB_PLANE; ++i) {
   1.628 +    p[i].coeff = ctx->coeff_pbuf[i][0];
   1.629 +    pd[i].qcoeff = ctx->qcoeff_pbuf[i][0];
   1.630 +    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
   1.631 +    pd[i].eobs = ctx->eobs_pbuf[i][0];
   1.632 +  }
   1.633 +  ctx->is_coded = 0;
   1.634 +  x->skip_recode = 0;
   1.635 +
   1.636 +  // Set to zero to make sure we do not use the previous encoded frame stats
   1.637 +  xd->mi_8x8[0]->mbmi.skip_coeff = 0;
   1.638 +
   1.639 +  x->source_variance = get_sby_perpixel_variance(cpi, x, bsize);
   1.640 +
   1.641 +  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.642 +    int energy;
   1.643 +    if (bsize <= BLOCK_16X16) {
   1.644 +      energy = x->mb_energy;
   1.645 +    } else {
   1.646 +      energy = vp9_block_energy(cpi, x, bsize);
   1.647 +    }
   1.648 +
   1.649 +    xd->mi_8x8[0]->mbmi.segment_id = vp9_vaq_segment_id(energy);
   1.650 +    rdmult_ratio = vp9_vaq_rdmult_ratio(energy);
   1.651 +    vp9_mb_init_quantizer(cpi, x);
   1.652 +  }
   1.653 +
   1.654 +  if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
   1.655 +    vp9_activity_masking(cpi, x);
   1.656 +
   1.657 +  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.658 +    vp9_clear_system_state();  // __asm emms;
   1.659 +    x->rdmult = round(x->rdmult * rdmult_ratio);
   1.660 +  }
   1.661 +
   1.662 +  // Find best coding mode & reconstruct the MB so it is available
   1.663 +  // as a predictor for MBs that follow in the SB
   1.664 +  if (frame_is_intra_only(cm)) {
   1.665 +    vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx,
   1.666 +                              best_rd);
   1.667 +  } else {
   1.668 +    if (bsize >= BLOCK_8X8)
   1.669 +      vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col,
   1.670 +                                totalrate, totaldist, bsize, ctx, best_rd);
   1.671 +    else
   1.672 +      vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate,
   1.673 +                                    totaldist, bsize, ctx, best_rd);
   1.674 +  }
   1.675 +
   1.676 +  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.677 +    x->rdmult = orig_rdmult;
   1.678 +    if (*totalrate != INT_MAX) {
   1.679 +      vp9_clear_system_state();  // __asm emms;
   1.680 +      *totalrate = round(*totalrate * rdmult_ratio);
   1.681 +    }
   1.682 +  }
   1.683 +}
   1.684 +
   1.685 +static void update_stats(VP9_COMP *cpi) {
   1.686 +  VP9_COMMON *const cm = &cpi->common;
   1.687 +  MACROBLOCK *const x = &cpi->mb;
   1.688 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.689 +  MODE_INFO *mi = xd->mi_8x8[0];
   1.690 +  MB_MODE_INFO *const mbmi = &mi->mbmi;
   1.691 +
   1.692 +  if (!frame_is_intra_only(cm)) {
   1.693 +    const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
   1.694 +                                                     SEG_LVL_REF_FRAME);
   1.695 +
   1.696 +    if (!seg_ref_active)
   1.697 +      cpi->intra_inter_count[vp9_get_pred_context_intra_inter(xd)]
   1.698 +                            [is_inter_block(mbmi)]++;
   1.699 +
   1.700 +    // If the segment reference feature is enabled we have only a single
   1.701 +    // reference frame allowed for the segment so exclude it from
   1.702 +    // the reference frame counts used to work out probabilities.
   1.703 +    if (is_inter_block(mbmi) && !seg_ref_active) {
   1.704 +      if (cm->comp_pred_mode == HYBRID_PREDICTION)
   1.705 +        cpi->comp_inter_count[vp9_get_pred_context_comp_inter_inter(cm, xd)]
   1.706 +                             [has_second_ref(mbmi)]++;
   1.707 +
   1.708 +      if (has_second_ref(mbmi)) {
   1.709 +        cpi->comp_ref_count[vp9_get_pred_context_comp_ref_p(cm, xd)]
   1.710 +                           [mbmi->ref_frame[0] == GOLDEN_FRAME]++;
   1.711 +      } else {
   1.712 +        cpi->single_ref_count[vp9_get_pred_context_single_ref_p1(xd)][0]
   1.713 +                             [mbmi->ref_frame[0] != LAST_FRAME]++;
   1.714 +        if (mbmi->ref_frame[0] != LAST_FRAME)
   1.715 +          cpi->single_ref_count[vp9_get_pred_context_single_ref_p2(xd)][1]
   1.716 +                               [mbmi->ref_frame[0] != GOLDEN_FRAME]++;
   1.717 +      }
   1.718 +    }
   1.719 +  }
   1.720 +}
   1.721 +
   1.722 +static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) {
   1.723 +  switch (bsize) {
   1.724 +    case BLOCK_64X64:
   1.725 +      return &x->sb64_partitioning;
   1.726 +    case BLOCK_32X32:
   1.727 +      return &x->sb_partitioning[x->sb_index];
   1.728 +    case BLOCK_16X16:
   1.729 +      return &x->mb_partitioning[x->sb_index][x->mb_index];
   1.730 +    case BLOCK_8X8:
   1.731 +      return &x->b_partitioning[x->sb_index][x->mb_index][x->b_index];
   1.732 +    default:
   1.733 +      assert(0);
   1.734 +      return NULL;
   1.735 +  }
   1.736 +}
   1.737 +
   1.738 +static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col,
   1.739 +                            ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
   1.740 +                            ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
   1.741 +                            PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
   1.742 +                            BLOCK_SIZE bsize) {
   1.743 +  MACROBLOCK *const x = &cpi->mb;
   1.744 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.745 +  int p;
   1.746 +  const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
   1.747 +  const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
   1.748 +  int mi_width = num_8x8_blocks_wide_lookup[bsize];
   1.749 +  int mi_height = num_8x8_blocks_high_lookup[bsize];
   1.750 +  for (p = 0; p < MAX_MB_PLANE; p++) {
   1.751 +    vpx_memcpy(
   1.752 +        cpi->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
   1.753 +        a + num_4x4_blocks_wide * p,
   1.754 +        (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
   1.755 +        xd->plane[p].subsampling_x);
   1.756 +    vpx_memcpy(
   1.757 +        cpi->left_context[p]
   1.758 +            + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
   1.759 +        l + num_4x4_blocks_high * p,
   1.760 +        (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
   1.761 +        xd->plane[p].subsampling_y);
   1.762 +  }
   1.763 +  vpx_memcpy(cpi->above_seg_context + mi_col, sa,
   1.764 +             sizeof(*cpi->above_seg_context) * mi_width);
   1.765 +  vpx_memcpy(cpi->left_seg_context + (mi_row & MI_MASK), sl,
   1.766 +             sizeof(cpi->left_seg_context[0]) * mi_height);
   1.767 +}
   1.768 +static void save_context(VP9_COMP *cpi, int mi_row, int mi_col,
   1.769 +                         ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
   1.770 +                         ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
   1.771 +                         PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
   1.772 +                         BLOCK_SIZE bsize) {
   1.773 +  const MACROBLOCK *const x = &cpi->mb;
   1.774 +  const MACROBLOCKD *const xd = &x->e_mbd;
   1.775 +  int p;
   1.776 +  const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
   1.777 +  const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
   1.778 +  int mi_width = num_8x8_blocks_wide_lookup[bsize];
   1.779 +  int mi_height = num_8x8_blocks_high_lookup[bsize];
   1.780 +
   1.781 +  // buffer the above/left context information of the block in search.
   1.782 +  for (p = 0; p < MAX_MB_PLANE; ++p) {
   1.783 +    vpx_memcpy(
   1.784 +        a + num_4x4_blocks_wide * p,
   1.785 +        cpi->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
   1.786 +        (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
   1.787 +        xd->plane[p].subsampling_x);
   1.788 +    vpx_memcpy(
   1.789 +        l + num_4x4_blocks_high * p,
   1.790 +        cpi->left_context[p]
   1.791 +            + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
   1.792 +        (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
   1.793 +        xd->plane[p].subsampling_y);
   1.794 +  }
   1.795 +  vpx_memcpy(sa, cpi->above_seg_context + mi_col,
   1.796 +             sizeof(*cpi->above_seg_context) * mi_width);
   1.797 +  vpx_memcpy(sl, cpi->left_seg_context + (mi_row & MI_MASK),
   1.798 +             sizeof(cpi->left_seg_context[0]) * mi_height);
   1.799 +}
   1.800 +
   1.801 +static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
   1.802 +                     TOKENEXTRA **tp, int mi_row, int mi_col,
   1.803 +                     int output_enabled, BLOCK_SIZE bsize, int sub_index) {
   1.804 +  VP9_COMMON *const cm = &cpi->common;
   1.805 +  MACROBLOCK *const x = &cpi->mb;
   1.806 +
   1.807 +  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
   1.808 +    return;
   1.809 +
   1.810 +  if (sub_index != -1)
   1.811 +    *get_sb_index(x, bsize) = sub_index;
   1.812 +
   1.813 +  if (bsize < BLOCK_8X8) {
   1.814 +    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
   1.815 +    // there is nothing to be done.
   1.816 +    if (x->ab_index > 0)
   1.817 +      return;
   1.818 +  }
   1.819 +  set_offsets(cpi, tile, mi_row, mi_col, bsize);
   1.820 +  update_state(cpi, get_block_context(x, bsize), bsize, output_enabled);
   1.821 +  encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize);
   1.822 +
   1.823 +  if (output_enabled) {
   1.824 +    update_stats(cpi);
   1.825 +
   1.826 +    (*tp)->token = EOSB_TOKEN;
   1.827 +    (*tp)++;
   1.828 +  }
   1.829 +}
   1.830 +
   1.831 +static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile,
   1.832 +                      TOKENEXTRA **tp, int mi_row, int mi_col,
   1.833 +                      int output_enabled, BLOCK_SIZE bsize) {
   1.834 +  VP9_COMMON *const cm = &cpi->common;
   1.835 +  MACROBLOCK *const x = &cpi->mb;
   1.836 +  BLOCK_SIZE c1 = BLOCK_8X8;
   1.837 +  const int bsl = b_width_log2(bsize), bs = (1 << bsl) / 4;
   1.838 +  int pl = 0;
   1.839 +  PARTITION_TYPE partition;
   1.840 +  BLOCK_SIZE subsize;
   1.841 +  int i;
   1.842 +
   1.843 +  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
   1.844 +    return;
   1.845 +
   1.846 +  c1 = BLOCK_4X4;
   1.847 +  if (bsize >= BLOCK_8X8) {
   1.848 +    pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
   1.849 +                                 mi_row, mi_col, bsize);
   1.850 +    c1 = *(get_sb_partitioning(x, bsize));
   1.851 +  }
   1.852 +  partition = partition_lookup[bsl][c1];
   1.853 +
   1.854 +  switch (partition) {
   1.855 +    case PARTITION_NONE:
   1.856 +      if (output_enabled && bsize >= BLOCK_8X8)
   1.857 +        cpi->partition_count[pl][PARTITION_NONE]++;
   1.858 +      encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, -1);
   1.859 +      break;
   1.860 +    case PARTITION_VERT:
   1.861 +      if (output_enabled)
   1.862 +        cpi->partition_count[pl][PARTITION_VERT]++;
   1.863 +      encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
   1.864 +      encode_b(cpi, tile, tp, mi_row, mi_col + bs, output_enabled, c1, 1);
   1.865 +      break;
   1.866 +    case PARTITION_HORZ:
   1.867 +      if (output_enabled)
   1.868 +        cpi->partition_count[pl][PARTITION_HORZ]++;
   1.869 +      encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
   1.870 +      encode_b(cpi, tile, tp, mi_row + bs, mi_col, output_enabled, c1, 1);
   1.871 +      break;
   1.872 +    case PARTITION_SPLIT:
   1.873 +      subsize = get_subsize(bsize, PARTITION_SPLIT);
   1.874 +
   1.875 +      if (output_enabled)
   1.876 +        cpi->partition_count[pl][PARTITION_SPLIT]++;
   1.877 +
   1.878 +      for (i = 0; i < 4; i++) {
   1.879 +        const int x_idx = i & 1, y_idx = i >> 1;
   1.880 +
   1.881 +        *get_sb_index(x, subsize) = i;
   1.882 +        encode_sb(cpi, tile, tp, mi_row + y_idx * bs, mi_col + x_idx * bs,
   1.883 +                  output_enabled, subsize);
   1.884 +      }
   1.885 +      break;
   1.886 +    default:
   1.887 +      assert(0);
   1.888 +      break;
   1.889 +  }
   1.890 +
   1.891 +  if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
   1.892 +    update_partition_context(cpi->above_seg_context, cpi->left_seg_context,
   1.893 +                             mi_row, mi_col, c1, bsize);
   1.894 +}
   1.895 +
   1.896 +// Check to see if the given partition size is allowed for a specified number
   1.897 +// of 8x8 block rows and columns remaining in the image.
   1.898 +// If not then return the largest allowed partition size
   1.899 +static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
   1.900 +                                      int rows_left, int cols_left,
   1.901 +                                      int *bh, int *bw) {
   1.902 +  if ((rows_left <= 0) || (cols_left <= 0)) {
   1.903 +    return MIN(bsize, BLOCK_8X8);
   1.904 +  } else {
   1.905 +    for (; bsize > 0; --bsize) {
   1.906 +      *bh = num_8x8_blocks_high_lookup[bsize];
   1.907 +      *bw = num_8x8_blocks_wide_lookup[bsize];
   1.908 +      if ((*bh <= rows_left) && (*bw <= cols_left)) {
   1.909 +        break;
   1.910 +      }
   1.911 +    }
   1.912 +  }
   1.913 +  return bsize;
   1.914 +}
   1.915 +
   1.916 +// This function attempts to set all mode info entries in a given SB64
   1.917 +// to the same block partition size.
   1.918 +// However, at the bottom and right borders of the image the requested size
   1.919 +// may not be allowed in which case this code attempts to choose the largest
   1.920 +// allowable partition.
   1.921 +static void set_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
   1.922 +                             MODE_INFO **mi_8x8, int mi_row, int mi_col) {
   1.923 +  VP9_COMMON *const cm = &cpi->common;
   1.924 +  BLOCK_SIZE bsize = cpi->sf.always_this_block_size;
   1.925 +  const int mis = cm->mode_info_stride;
   1.926 +  int row8x8_remaining = tile->mi_row_end - mi_row;
   1.927 +  int col8x8_remaining = tile->mi_col_end - mi_col;
   1.928 +  int block_row, block_col;
   1.929 +  MODE_INFO * mi_upper_left = cm->mi + mi_row * mis + mi_col;
   1.930 +  int bh = num_8x8_blocks_high_lookup[bsize];
   1.931 +  int bw = num_8x8_blocks_wide_lookup[bsize];
   1.932 +
   1.933 +  assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
   1.934 +
   1.935 +  // Apply the requested partition size to the SB64 if it is all "in image"
   1.936 +  if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
   1.937 +      (row8x8_remaining >= MI_BLOCK_SIZE)) {
   1.938 +    for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
   1.939 +      for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
   1.940 +        int index = block_row * mis + block_col;
   1.941 +        mi_8x8[index] = mi_upper_left + index;
   1.942 +        mi_8x8[index]->mbmi.sb_type = bsize;
   1.943 +      }
   1.944 +    }
   1.945 +  } else {
   1.946 +    // Else this is a partial SB64.
   1.947 +    for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
   1.948 +      for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
   1.949 +        int index = block_row * mis + block_col;
   1.950 +        // Find a partition size that fits
   1.951 +        bsize = find_partition_size(cpi->sf.always_this_block_size,
   1.952 +                                    (row8x8_remaining - block_row),
   1.953 +                                    (col8x8_remaining - block_col), &bh, &bw);
   1.954 +        mi_8x8[index] = mi_upper_left + index;
   1.955 +        mi_8x8[index]->mbmi.sb_type = bsize;
   1.956 +      }
   1.957 +    }
   1.958 +  }
   1.959 +}
   1.960 +
   1.961 +static void copy_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8,
   1.962 +                              MODE_INFO **prev_mi_8x8) {
   1.963 +  VP9_COMMON *const cm = &cpi->common;
   1.964 +  const int mis = cm->mode_info_stride;
   1.965 +  int block_row, block_col;
   1.966 +
   1.967 +  for (block_row = 0; block_row < 8; ++block_row) {
   1.968 +    for (block_col = 0; block_col < 8; ++block_col) {
   1.969 +      MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
   1.970 +      BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
   1.971 +      ptrdiff_t offset;
   1.972 +
   1.973 +      if (prev_mi) {
   1.974 +        offset = prev_mi - cm->prev_mi;
   1.975 +        mi_8x8[block_row * mis + block_col] = cm->mi + offset;
   1.976 +        mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type;
   1.977 +      }
   1.978 +    }
   1.979 +  }
   1.980 +}
   1.981 +
   1.982 +static int sb_has_motion(VP9_COMP *cpi, MODE_INFO **prev_mi_8x8) {
   1.983 +  VP9_COMMON *const cm = &cpi->common;
   1.984 +  const int mis = cm->mode_info_stride;
   1.985 +  int block_row, block_col;
   1.986 +
   1.987 +  if (cm->prev_mi) {
   1.988 +    for (block_row = 0; block_row < 8; ++block_row) {
   1.989 +      for (block_col = 0; block_col < 8; ++block_col) {
   1.990 +        MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
   1.991 +        if (prev_mi) {
   1.992 +          if (abs(prev_mi->mbmi.mv[0].as_mv.row) >= 8 ||
   1.993 +              abs(prev_mi->mbmi.mv[0].as_mv.col) >= 8)
   1.994 +            return 1;
   1.995 +        }
   1.996 +      }
   1.997 +    }
   1.998 +  }
   1.999 +  return 0;
  1.1000 +}
  1.1001 +
  1.1002 +static void rd_use_partition(VP9_COMP *cpi,
  1.1003 +                             const TileInfo *const tile,
  1.1004 +                             MODE_INFO **mi_8x8,
  1.1005 +                             TOKENEXTRA **tp, int mi_row, int mi_col,
  1.1006 +                             BLOCK_SIZE bsize, int *rate, int64_t *dist,
  1.1007 +                             int do_recon) {
  1.1008 +  VP9_COMMON *const cm = &cpi->common;
  1.1009 +  MACROBLOCK *const x = &cpi->mb;
  1.1010 +  const int mis = cm->mode_info_stride;
  1.1011 +  int bsl = b_width_log2(bsize);
  1.1012 +  const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1.1013 +  const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1.1014 +  int ms = num_4x4_blocks_wide / 2;
  1.1015 +  int mh = num_4x4_blocks_high / 2;
  1.1016 +  int bss = (1 << bsl) / 4;
  1.1017 +  int i, pl;
  1.1018 +  PARTITION_TYPE partition = PARTITION_NONE;
  1.1019 +  BLOCK_SIZE subsize;
  1.1020 +  ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1.1021 +  PARTITION_CONTEXT sl[8], sa[8];
  1.1022 +  int last_part_rate = INT_MAX;
  1.1023 +  int64_t last_part_dist = INT_MAX;
  1.1024 +  int split_rate = INT_MAX;
  1.1025 +  int64_t split_dist = INT_MAX;
  1.1026 +  int none_rate = INT_MAX;
  1.1027 +  int64_t none_dist = INT_MAX;
  1.1028 +  int chosen_rate = INT_MAX;
  1.1029 +  int64_t chosen_dist = INT_MAX;
  1.1030 +  BLOCK_SIZE sub_subsize = BLOCK_4X4;
  1.1031 +  int splits_below = 0;
  1.1032 +  BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
  1.1033 +
  1.1034 +  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
  1.1035 +    return;
  1.1036 +
  1.1037 +  partition = partition_lookup[bsl][bs_type];
  1.1038 +
  1.1039 +  subsize = get_subsize(bsize, partition);
  1.1040 +
  1.1041 +  if (bsize < BLOCK_8X8) {
  1.1042 +    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
  1.1043 +    // there is nothing to be done.
  1.1044 +    if (x->ab_index != 0) {
  1.1045 +      *rate = 0;
  1.1046 +      *dist = 0;
  1.1047 +      return;
  1.1048 +    }
  1.1049 +  } else {
  1.1050 +    *(get_sb_partitioning(x, bsize)) = subsize;
  1.1051 +  }
  1.1052 +  save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1053 +
  1.1054 +  if (bsize == BLOCK_16X16) {
  1.1055 +    set_offsets(cpi, tile, mi_row, mi_col, bsize);
  1.1056 +    x->mb_energy = vp9_block_energy(cpi, x, bsize);
  1.1057 +  }
  1.1058 +
  1.1059 +  x->fast_ms = 0;
  1.1060 +  x->subblock_ref = 0;
  1.1061 +
  1.1062 +  if (cpi->sf.adjust_partitioning_from_last_frame) {
  1.1063 +    // Check if any of the sub blocks are further split.
  1.1064 +    if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
  1.1065 +      sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
  1.1066 +      splits_below = 1;
  1.1067 +      for (i = 0; i < 4; i++) {
  1.1068 +        int jj = i >> 1, ii = i & 0x01;
  1.1069 +        MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
  1.1070 +        if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
  1.1071 +          splits_below = 0;
  1.1072 +        }
  1.1073 +      }
  1.1074 +    }
  1.1075 +
  1.1076 +    // If partition is not none try none unless each of the 4 splits are split
  1.1077 +    // even further..
  1.1078 +    if (partition != PARTITION_NONE && !splits_below &&
  1.1079 +        mi_row + (ms >> 1) < cm->mi_rows &&
  1.1080 +        mi_col + (ms >> 1) < cm->mi_cols) {
  1.1081 +      *(get_sb_partitioning(x, bsize)) = bsize;
  1.1082 +      pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize,
  1.1083 +                    get_block_context(x, bsize), INT64_MAX);
  1.1084 +
  1.1085 +      pl = partition_plane_context(cpi->above_seg_context,
  1.1086 +                                   cpi->left_seg_context,
  1.1087 +                                   mi_row, mi_col, bsize);
  1.1088 +      none_rate += x->partition_cost[pl][PARTITION_NONE];
  1.1089 +
  1.1090 +      restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1091 +      mi_8x8[0]->mbmi.sb_type = bs_type;
  1.1092 +      *(get_sb_partitioning(x, bsize)) = subsize;
  1.1093 +    }
  1.1094 +  }
  1.1095 +
  1.1096 +  switch (partition) {
  1.1097 +    case PARTITION_NONE:
  1.1098 +      pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
  1.1099 +                    bsize, get_block_context(x, bsize), INT64_MAX);
  1.1100 +      break;
  1.1101 +    case PARTITION_HORZ:
  1.1102 +      *get_sb_index(x, subsize) = 0;
  1.1103 +      pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
  1.1104 +                    subsize, get_block_context(x, subsize), INT64_MAX);
  1.1105 +      if (last_part_rate != INT_MAX &&
  1.1106 +          bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) {
  1.1107 +        int rt = 0;
  1.1108 +        int64_t dt = 0;
  1.1109 +        update_state(cpi, get_block_context(x, subsize), subsize, 0);
  1.1110 +        encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
  1.1111 +        *get_sb_index(x, subsize) = 1;
  1.1112 +        pick_sb_modes(cpi, tile, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize,
  1.1113 +                      get_block_context(x, subsize), INT64_MAX);
  1.1114 +        if (rt == INT_MAX || dt == INT_MAX) {
  1.1115 +          last_part_rate = INT_MAX;
  1.1116 +          last_part_dist = INT_MAX;
  1.1117 +          break;
  1.1118 +        }
  1.1119 +
  1.1120 +        last_part_rate += rt;
  1.1121 +        last_part_dist += dt;
  1.1122 +      }
  1.1123 +      break;
  1.1124 +    case PARTITION_VERT:
  1.1125 +      *get_sb_index(x, subsize) = 0;
  1.1126 +      pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
  1.1127 +                    subsize, get_block_context(x, subsize), INT64_MAX);
  1.1128 +      if (last_part_rate != INT_MAX &&
  1.1129 +          bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) {
  1.1130 +        int rt = 0;
  1.1131 +        int64_t dt = 0;
  1.1132 +        update_state(cpi, get_block_context(x, subsize), subsize, 0);
  1.1133 +        encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
  1.1134 +        *get_sb_index(x, subsize) = 1;
  1.1135 +        pick_sb_modes(cpi, tile, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize,
  1.1136 +                      get_block_context(x, subsize), INT64_MAX);
  1.1137 +        if (rt == INT_MAX || dt == INT_MAX) {
  1.1138 +          last_part_rate = INT_MAX;
  1.1139 +          last_part_dist = INT_MAX;
  1.1140 +          break;
  1.1141 +        }
  1.1142 +        last_part_rate += rt;
  1.1143 +        last_part_dist += dt;
  1.1144 +      }
  1.1145 +      break;
  1.1146 +    case PARTITION_SPLIT:
  1.1147 +      // Split partition.
  1.1148 +      last_part_rate = 0;
  1.1149 +      last_part_dist = 0;
  1.1150 +      for (i = 0; i < 4; i++) {
  1.1151 +        int x_idx = (i & 1) * (ms >> 1);
  1.1152 +        int y_idx = (i >> 1) * (ms >> 1);
  1.1153 +        int jj = i >> 1, ii = i & 0x01;
  1.1154 +        int rt;
  1.1155 +        int64_t dt;
  1.1156 +
  1.1157 +        if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
  1.1158 +          continue;
  1.1159 +
  1.1160 +        *get_sb_index(x, subsize) = i;
  1.1161 +
  1.1162 +        rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp,
  1.1163 +                         mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
  1.1164 +                         i != 3);
  1.1165 +        if (rt == INT_MAX || dt == INT_MAX) {
  1.1166 +          last_part_rate = INT_MAX;
  1.1167 +          last_part_dist = INT_MAX;
  1.1168 +          break;
  1.1169 +        }
  1.1170 +        last_part_rate += rt;
  1.1171 +        last_part_dist += dt;
  1.1172 +      }
  1.1173 +      break;
  1.1174 +    default:
  1.1175 +      assert(0);
  1.1176 +  }
  1.1177 +
  1.1178 +  pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
  1.1179 +                               mi_row, mi_col, bsize);
  1.1180 +  if (last_part_rate < INT_MAX)
  1.1181 +    last_part_rate += x->partition_cost[pl][partition];
  1.1182 +
  1.1183 +  if (cpi->sf.adjust_partitioning_from_last_frame
  1.1184 +      && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
  1.1185 +      && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows)
  1.1186 +      && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) {
  1.1187 +    BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
  1.1188 +    split_rate = 0;
  1.1189 +    split_dist = 0;
  1.1190 +    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1191 +
  1.1192 +    // Split partition.
  1.1193 +    for (i = 0; i < 4; i++) {
  1.1194 +      int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2);
  1.1195 +      int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2);
  1.1196 +      int rt = 0;
  1.1197 +      int64_t dt = 0;
  1.1198 +      ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1.1199 +      PARTITION_CONTEXT sl[8], sa[8];
  1.1200 +
  1.1201 +      if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
  1.1202 +        continue;
  1.1203 +
  1.1204 +      *get_sb_index(x, split_subsize) = i;
  1.1205 +      *get_sb_partitioning(x, bsize) = split_subsize;
  1.1206 +      *get_sb_partitioning(x, split_subsize) = split_subsize;
  1.1207 +
  1.1208 +      save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1209 +
  1.1210 +      pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
  1.1211 +                    split_subsize, get_block_context(x, split_subsize),
  1.1212 +                    INT64_MAX);
  1.1213 +
  1.1214 +      restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1215 +
  1.1216 +      if (rt == INT_MAX || dt == INT_MAX) {
  1.1217 +        split_rate = INT_MAX;
  1.1218 +        split_dist = INT_MAX;
  1.1219 +        break;
  1.1220 +      }
  1.1221 +
  1.1222 +      if (i != 3)
  1.1223 +        encode_sb(cpi, tile, tp,  mi_row + y_idx, mi_col + x_idx, 0,
  1.1224 +                  split_subsize);
  1.1225 +
  1.1226 +      split_rate += rt;
  1.1227 +      split_dist += dt;
  1.1228 +      pl = partition_plane_context(cpi->above_seg_context,
  1.1229 +                                   cpi->left_seg_context,
  1.1230 +                                   mi_row + y_idx, mi_col + x_idx, bsize);
  1.1231 +      split_rate += x->partition_cost[pl][PARTITION_NONE];
  1.1232 +    }
  1.1233 +    pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
  1.1234 +                                 mi_row, mi_col, bsize);
  1.1235 +    if (split_rate < INT_MAX) {
  1.1236 +      split_rate += x->partition_cost[pl][PARTITION_SPLIT];
  1.1237 +
  1.1238 +      chosen_rate = split_rate;
  1.1239 +      chosen_dist = split_dist;
  1.1240 +    }
  1.1241 +  }
  1.1242 +
  1.1243 +  // If last_part is better set the partitioning to that...
  1.1244 +  if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist)
  1.1245 +      < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) {
  1.1246 +    mi_8x8[0]->mbmi.sb_type = bsize;
  1.1247 +    if (bsize >= BLOCK_8X8)
  1.1248 +      *(get_sb_partitioning(x, bsize)) = subsize;
  1.1249 +    chosen_rate = last_part_rate;
  1.1250 +    chosen_dist = last_part_dist;
  1.1251 +  }
  1.1252 +  // If none was better set the partitioning to that...
  1.1253 +  if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)
  1.1254 +      > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) {
  1.1255 +    if (bsize >= BLOCK_8X8)
  1.1256 +      *(get_sb_partitioning(x, bsize)) = bsize;
  1.1257 +    chosen_rate = none_rate;
  1.1258 +    chosen_dist = none_dist;
  1.1259 +  }
  1.1260 +
  1.1261 +  restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1262 +
  1.1263 +  // We must have chosen a partitioning and encoding or we'll fail later on.
  1.1264 +  // No other opportunities for success.
  1.1265 +  if ( bsize == BLOCK_64X64)
  1.1266 +    assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX);
  1.1267 +
  1.1268 +  if (do_recon)
  1.1269 +    encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
  1.1270 +
  1.1271 +  *rate = chosen_rate;
  1.1272 +  *dist = chosen_dist;
  1.1273 +}
  1.1274 +
  1.1275 +static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
  1.1276 +  BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
  1.1277 +  BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
  1.1278 +  BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
  1.1279 +};
  1.1280 +
  1.1281 +static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
  1.1282 +  BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
  1.1283 +  BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64,
  1.1284 +  BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
  1.1285 +};
  1.1286 +
  1.1287 +// Look at all the mode_info entries for blocks that are part of this
  1.1288 +// partition and find the min and max values for sb_type.
  1.1289 +// At the moment this is designed to work on a 64x64 SB but could be
  1.1290 +// adjusted to use a size parameter.
  1.1291 +//
  1.1292 +// The min and max are assumed to have been initialized prior to calling this
  1.1293 +// function so repeat calls can accumulate a min and max of more than one sb64.
  1.1294 +static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8,
  1.1295 +                                        BLOCK_SIZE * min_block_size,
  1.1296 +                                        BLOCK_SIZE * max_block_size ) {
  1.1297 +  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  1.1298 +  int sb_width_in_blocks = MI_BLOCK_SIZE;
  1.1299 +  int sb_height_in_blocks  = MI_BLOCK_SIZE;
  1.1300 +  int i, j;
  1.1301 +  int index = 0;
  1.1302 +
  1.1303 +  // Check the sb_type for each block that belongs to this region.
  1.1304 +  for (i = 0; i < sb_height_in_blocks; ++i) {
  1.1305 +    for (j = 0; j < sb_width_in_blocks; ++j) {
  1.1306 +      MODE_INFO * mi = mi_8x8[index+j];
  1.1307 +      BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
  1.1308 +      *min_block_size = MIN(*min_block_size, sb_type);
  1.1309 +      *max_block_size = MAX(*max_block_size, sb_type);
  1.1310 +    }
  1.1311 +    index += xd->mode_info_stride;
  1.1312 +  }
  1.1313 +}
  1.1314 +
  1.1315 +// Look at neighboring blocks and set a min and max partition size based on
  1.1316 +// what they chose.
  1.1317 +static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
  1.1318 +                                    int row, int col,
  1.1319 +                                    BLOCK_SIZE *min_block_size,
  1.1320 +                                    BLOCK_SIZE *max_block_size) {
  1.1321 +  VP9_COMMON * const cm = &cpi->common;
  1.1322 +  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  1.1323 +  MODE_INFO ** mi_8x8 = xd->mi_8x8;
  1.1324 +  MODE_INFO ** prev_mi_8x8 = xd->prev_mi_8x8;
  1.1325 +
  1.1326 +  const int left_in_image = xd->left_available && mi_8x8[-1];
  1.1327 +  const int above_in_image = xd->up_available &&
  1.1328 +                             mi_8x8[-xd->mode_info_stride];
  1.1329 +  MODE_INFO ** above_sb64_mi_8x8;
  1.1330 +  MODE_INFO ** left_sb64_mi_8x8;
  1.1331 +
  1.1332 +  int row8x8_remaining = tile->mi_row_end - row;
  1.1333 +  int col8x8_remaining = tile->mi_col_end - col;
  1.1334 +  int bh, bw;
  1.1335 +
  1.1336 +  // Trap case where we do not have a prediction.
  1.1337 +  if (!left_in_image && !above_in_image &&
  1.1338 +      ((cm->frame_type == KEY_FRAME) || !cm->prev_mi)) {
  1.1339 +    *min_block_size = BLOCK_4X4;
  1.1340 +    *max_block_size = BLOCK_64X64;
  1.1341 +  } else {
  1.1342 +    // Default "min to max" and "max to min"
  1.1343 +    *min_block_size = BLOCK_64X64;
  1.1344 +    *max_block_size = BLOCK_4X4;
  1.1345 +
  1.1346 +    // NOTE: each call to get_sb_partition_size_range() uses the previous
  1.1347 +    // passed in values for min and max as a starting point.
  1.1348 +    //
  1.1349 +    // Find the min and max partition used in previous frame at this location
  1.1350 +    if (cm->prev_mi && (cm->frame_type != KEY_FRAME)) {
  1.1351 +      get_sb_partition_size_range(cpi, prev_mi_8x8,
  1.1352 +                                  min_block_size, max_block_size);
  1.1353 +    }
  1.1354 +
  1.1355 +    // Find the min and max partition sizes used in the left SB64
  1.1356 +    if (left_in_image) {
  1.1357 +      left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE];
  1.1358 +      get_sb_partition_size_range(cpi, left_sb64_mi_8x8,
  1.1359 +                                  min_block_size, max_block_size);
  1.1360 +    }
  1.1361 +
  1.1362 +    // Find the min and max partition sizes used in the above SB64.
  1.1363 +    if (above_in_image) {
  1.1364 +      above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE];
  1.1365 +      get_sb_partition_size_range(cpi, above_sb64_mi_8x8,
  1.1366 +                                  min_block_size, max_block_size);
  1.1367 +    }
  1.1368 +  }
  1.1369 +
  1.1370 +  // Give a bit of leaway either side of the observed min and max
  1.1371 +  *min_block_size = min_partition_size[*min_block_size];
  1.1372 +  *max_block_size = max_partition_size[*max_block_size];
  1.1373 +
  1.1374 +  // Check border cases where max and min from neighbours may not be legal.
  1.1375 +  *max_block_size = find_partition_size(*max_block_size,
  1.1376 +                                        row8x8_remaining, col8x8_remaining,
  1.1377 +                                        &bh, &bw);
  1.1378 +  *min_block_size = MIN(*min_block_size, *max_block_size);
  1.1379 +}
  1.1380 +
  1.1381 +static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) {
  1.1382 +  VP9_COMMON *const cm = &cpi->common;
  1.1383 +  MACROBLOCK *const x = &cpi->mb;
  1.1384 +
  1.1385 +  // Only use 8x8 result for non HD videos.
  1.1386 +  // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0;
  1.1387 +  int use_8x8 = 1;
  1.1388 +
  1.1389 +  if (cm->frame_type && !cpi->is_src_frame_alt_ref &&
  1.1390 +      ((use_8x8 && bsize == BLOCK_16X16) ||
  1.1391 +      bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) {
  1.1392 +    int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0;
  1.1393 +    PICK_MODE_CONTEXT *block_context = NULL;
  1.1394 +
  1.1395 +    if (bsize == BLOCK_16X16) {
  1.1396 +      block_context = x->sb8x8_context[x->sb_index][x->mb_index];
  1.1397 +    } else if (bsize == BLOCK_32X32) {
  1.1398 +      block_context = x->mb_context[x->sb_index];
  1.1399 +    } else if (bsize == BLOCK_64X64) {
  1.1400 +      block_context = x->sb32_context;
  1.1401 +    }
  1.1402 +
  1.1403 +    if (block_context) {
  1.1404 +      ref0 = block_context[0].mic.mbmi.ref_frame[0];
  1.1405 +      ref1 = block_context[1].mic.mbmi.ref_frame[0];
  1.1406 +      ref2 = block_context[2].mic.mbmi.ref_frame[0];
  1.1407 +      ref3 = block_context[3].mic.mbmi.ref_frame[0];
  1.1408 +    }
  1.1409 +
  1.1410 +    // Currently, only consider 4 inter reference frames.
  1.1411 +    if (ref0 && ref1 && ref2 && ref3) {
  1.1412 +      int d01, d23, d02, d13;
  1.1413 +
  1.1414 +      // Motion vectors for the four subblocks.
  1.1415 +      int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row;
  1.1416 +      int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col;
  1.1417 +      int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row;
  1.1418 +      int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col;
  1.1419 +      int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row;
  1.1420 +      int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col;
  1.1421 +      int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row;
  1.1422 +      int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col;
  1.1423 +
  1.1424 +      // Adjust sign if ref is alt_ref.
  1.1425 +      if (cm->ref_frame_sign_bias[ref0]) {
  1.1426 +        mvr0 *= -1;
  1.1427 +        mvc0 *= -1;
  1.1428 +      }
  1.1429 +
  1.1430 +      if (cm->ref_frame_sign_bias[ref1]) {
  1.1431 +        mvr1 *= -1;
  1.1432 +        mvc1 *= -1;
  1.1433 +      }
  1.1434 +
  1.1435 +      if (cm->ref_frame_sign_bias[ref2]) {
  1.1436 +        mvr2 *= -1;
  1.1437 +        mvc2 *= -1;
  1.1438 +      }
  1.1439 +
  1.1440 +      if (cm->ref_frame_sign_bias[ref3]) {
  1.1441 +        mvr3 *= -1;
  1.1442 +        mvc3 *= -1;
  1.1443 +      }
  1.1444 +
  1.1445 +      // Calculate mv distances.
  1.1446 +      d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1));
  1.1447 +      d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3));
  1.1448 +      d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2));
  1.1449 +      d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3));
  1.1450 +
  1.1451 +      if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH &&
  1.1452 +          d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) {
  1.1453 +        // Set fast motion search level.
  1.1454 +        x->fast_ms = 1;
  1.1455 +
  1.1456 +        if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 &&
  1.1457 +            d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) {
  1.1458 +          // Set fast motion search level.
  1.1459 +          x->fast_ms = 2;
  1.1460 +
  1.1461 +          if (!d01 && !d23 && !d02 && !d13) {
  1.1462 +            x->fast_ms = 3;
  1.1463 +            x->subblock_ref = ref0;
  1.1464 +          }
  1.1465 +        }
  1.1466 +      }
  1.1467 +    }
  1.1468 +  }
  1.1469 +}
  1.1470 +
  1.1471 +static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  1.1472 +  vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
  1.1473 +}
  1.1474 +
  1.1475 +static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  1.1476 +  vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
  1.1477 +}
  1.1478 +
  1.1479 +// TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
  1.1480 +// unlikely to be selected depending on previous rate-distortion optimization
  1.1481 +// results, for encoding speed-up.
  1.1482 +static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
  1.1483 +                              TOKENEXTRA **tp, int mi_row,
  1.1484 +                              int mi_col, BLOCK_SIZE bsize, int *rate,
  1.1485 +                              int64_t *dist, int do_recon, int64_t best_rd) {
  1.1486 +  VP9_COMMON *const cm = &cpi->common;
  1.1487 +  MACROBLOCK *const x = &cpi->mb;
  1.1488 +  const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
  1.1489 +  ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1.1490 +  PARTITION_CONTEXT sl[8], sa[8];
  1.1491 +  TOKENEXTRA *tp_orig = *tp;
  1.1492 +  int i, pl;
  1.1493 +  BLOCK_SIZE subsize;
  1.1494 +  int this_rate, sum_rate = 0, best_rate = INT_MAX;
  1.1495 +  int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
  1.1496 +  int64_t sum_rd = 0;
  1.1497 +  int do_split = bsize >= BLOCK_8X8;
  1.1498 +  int do_rect = 1;
  1.1499 +  // Override skipping rectangular partition operations for edge blocks
  1.1500 +  const int force_horz_split = (mi_row + ms >= cm->mi_rows);
  1.1501 +  const int force_vert_split = (mi_col + ms >= cm->mi_cols);
  1.1502 +
  1.1503 +  int partition_none_allowed = !force_horz_split && !force_vert_split;
  1.1504 +  int partition_horz_allowed = !force_vert_split && bsize >= BLOCK_8X8;
  1.1505 +  int partition_vert_allowed = !force_horz_split && bsize >= BLOCK_8X8;
  1.1506 +
  1.1507 +  int partition_split_done = 0;
  1.1508 +  (void) *tp_orig;
  1.1509 +
  1.1510 +  if (bsize < BLOCK_8X8) {
  1.1511 +    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
  1.1512 +    // there is nothing to be done.
  1.1513 +    if (x->ab_index != 0) {
  1.1514 +      *rate = 0;
  1.1515 +      *dist = 0;
  1.1516 +      return;
  1.1517 +    }
  1.1518 +  }
  1.1519 +  assert(num_8x8_blocks_wide_lookup[bsize] ==
  1.1520 +             num_8x8_blocks_high_lookup[bsize]);
  1.1521 +
  1.1522 +  if (bsize == BLOCK_16X16) {
  1.1523 +    set_offsets(cpi, tile, mi_row, mi_col, bsize);
  1.1524 +    x->mb_energy = vp9_block_energy(cpi, x, bsize);
  1.1525 +  }
  1.1526 +
  1.1527 +  // Determine partition types in search according to the speed features.
  1.1528 +  // The threshold set here has to be of square block size.
  1.1529 +  if (cpi->sf.auto_min_max_partition_size) {
  1.1530 +    partition_none_allowed &= (bsize <= cpi->sf.max_partition_size &&
  1.1531 +                               bsize >= cpi->sf.min_partition_size);
  1.1532 +    partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size &&
  1.1533 +                                bsize >  cpi->sf.min_partition_size) ||
  1.1534 +                                force_horz_split);
  1.1535 +    partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size &&
  1.1536 +                                bsize >  cpi->sf.min_partition_size) ||
  1.1537 +                                force_vert_split);
  1.1538 +    do_split &= bsize > cpi->sf.min_partition_size;
  1.1539 +  }
  1.1540 +  if (cpi->sf.use_square_partition_only) {
  1.1541 +    partition_horz_allowed &= force_horz_split;
  1.1542 +    partition_vert_allowed &= force_vert_split;
  1.1543 +  }
  1.1544 +
  1.1545 +  save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1546 +
  1.1547 +  if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
  1.1548 +    unsigned int source_variancey;
  1.1549 +    vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
  1.1550 +    source_variancey = get_sby_perpixel_variance(cpi, x, bsize);
  1.1551 +    if (source_variancey < cpi->sf.disable_split_var_thresh) {
  1.1552 +      do_split = 0;
  1.1553 +      if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
  1.1554 +        do_rect = 0;
  1.1555 +    }
  1.1556 +  }
  1.1557 +
  1.1558 +  // PARTITION_NONE
  1.1559 +  if (partition_none_allowed) {
  1.1560 +    pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize,
  1.1561 +                  get_block_context(x, bsize), best_rd);
  1.1562 +    if (this_rate != INT_MAX) {
  1.1563 +      if (bsize >= BLOCK_8X8) {
  1.1564 +        pl = partition_plane_context(cpi->above_seg_context,
  1.1565 +                                     cpi->left_seg_context,
  1.1566 +                                     mi_row, mi_col, bsize);
  1.1567 +        this_rate += x->partition_cost[pl][PARTITION_NONE];
  1.1568 +      }
  1.1569 +      sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
  1.1570 +      if (sum_rd < best_rd) {
  1.1571 +        int64_t stop_thresh = 2048;
  1.1572 +
  1.1573 +        best_rate = this_rate;
  1.1574 +        best_dist = this_dist;
  1.1575 +        best_rd = sum_rd;
  1.1576 +        if (bsize >= BLOCK_8X8)
  1.1577 +          *(get_sb_partitioning(x, bsize)) = bsize;
  1.1578 +
  1.1579 +        // Adjust threshold according to partition size.
  1.1580 +        stop_thresh >>= 8 - (b_width_log2_lookup[bsize] +
  1.1581 +            b_height_log2_lookup[bsize]);
  1.1582 +
  1.1583 +        // If obtained distortion is very small, choose current partition
  1.1584 +        // and stop splitting.
  1.1585 +        if (this_dist < stop_thresh) {
  1.1586 +          do_split = 0;
  1.1587 +          do_rect = 0;
  1.1588 +        }
  1.1589 +      }
  1.1590 +    }
  1.1591 +    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1592 +  }
  1.1593 +
  1.1594 +  // store estimated motion vector
  1.1595 +  if (cpi->sf.adaptive_motion_search)
  1.1596 +    store_pred_mv(x, get_block_context(x, bsize));
  1.1597 +
  1.1598 +  // PARTITION_SPLIT
  1.1599 +  sum_rd = 0;
  1.1600 +  // TODO(jingning): use the motion vectors given by the above search as
  1.1601 +  // the starting point of motion search in the following partition type check.
  1.1602 +  if (do_split) {
  1.1603 +    subsize = get_subsize(bsize, PARTITION_SPLIT);
  1.1604 +    for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
  1.1605 +      const int x_idx = (i & 1) * ms;
  1.1606 +      const int y_idx = (i >> 1) * ms;
  1.1607 +
  1.1608 +      if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
  1.1609 +        continue;
  1.1610 +
  1.1611 +      *get_sb_index(x, subsize) = i;
  1.1612 +      if (cpi->sf.adaptive_motion_search)
  1.1613 +        load_pred_mv(x, get_block_context(x, bsize));
  1.1614 +      rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, subsize,
  1.1615 +                        &this_rate, &this_dist, i != 3, best_rd - sum_rd);
  1.1616 +
  1.1617 +      if (this_rate == INT_MAX) {
  1.1618 +        sum_rd = INT64_MAX;
  1.1619 +      } else {
  1.1620 +        sum_rate += this_rate;
  1.1621 +        sum_dist += this_dist;
  1.1622 +        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1623 +      }
  1.1624 +    }
  1.1625 +    if (sum_rd < best_rd && i == 4) {
  1.1626 +      pl = partition_plane_context(cpi->above_seg_context,
  1.1627 +                                   cpi->left_seg_context,
  1.1628 +                                   mi_row, mi_col, bsize);
  1.1629 +      sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
  1.1630 +      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1631 +      if (sum_rd < best_rd) {
  1.1632 +        best_rate = sum_rate;
  1.1633 +        best_dist = sum_dist;
  1.1634 +        best_rd = sum_rd;
  1.1635 +        *(get_sb_partitioning(x, bsize)) = subsize;
  1.1636 +      }
  1.1637 +    } else {
  1.1638 +      // skip rectangular partition test when larger block size
  1.1639 +      // gives better rd cost
  1.1640 +      if (cpi->sf.less_rectangular_check)
  1.1641 +        do_rect &= !partition_none_allowed;
  1.1642 +    }
  1.1643 +    partition_split_done = 1;
  1.1644 +    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1645 +  }
  1.1646 +
  1.1647 +  x->fast_ms = 0;
  1.1648 +  x->subblock_ref = 0;
  1.1649 +
  1.1650 +  if (partition_split_done &&
  1.1651 +      cpi->sf.using_small_partition_info) {
  1.1652 +    compute_fast_motion_search_level(cpi, bsize);
  1.1653 +  }
  1.1654 +
  1.1655 +  // PARTITION_HORZ
  1.1656 +  if (partition_horz_allowed && do_rect) {
  1.1657 +    subsize = get_subsize(bsize, PARTITION_HORZ);
  1.1658 +    *get_sb_index(x, subsize) = 0;
  1.1659 +    if (cpi->sf.adaptive_motion_search)
  1.1660 +      load_pred_mv(x, get_block_context(x, bsize));
  1.1661 +    pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
  1.1662 +                  get_block_context(x, subsize), best_rd);
  1.1663 +    sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1664 +
  1.1665 +    if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
  1.1666 +      update_state(cpi, get_block_context(x, subsize), subsize, 0);
  1.1667 +      encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
  1.1668 +
  1.1669 +      *get_sb_index(x, subsize) = 1;
  1.1670 +      if (cpi->sf.adaptive_motion_search)
  1.1671 +        load_pred_mv(x, get_block_context(x, bsize));
  1.1672 +      pick_sb_modes(cpi, tile, mi_row + ms, mi_col, &this_rate,
  1.1673 +                    &this_dist, subsize, get_block_context(x, subsize),
  1.1674 +                    best_rd - sum_rd);
  1.1675 +      if (this_rate == INT_MAX) {
  1.1676 +        sum_rd = INT64_MAX;
  1.1677 +      } else {
  1.1678 +        sum_rate += this_rate;
  1.1679 +        sum_dist += this_dist;
  1.1680 +        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1681 +      }
  1.1682 +    }
  1.1683 +    if (sum_rd < best_rd) {
  1.1684 +      pl = partition_plane_context(cpi->above_seg_context,
  1.1685 +                                   cpi->left_seg_context,
  1.1686 +                                   mi_row, mi_col, bsize);
  1.1687 +      sum_rate += x->partition_cost[pl][PARTITION_HORZ];
  1.1688 +      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1689 +      if (sum_rd < best_rd) {
  1.1690 +        best_rd = sum_rd;
  1.1691 +        best_rate = sum_rate;
  1.1692 +        best_dist = sum_dist;
  1.1693 +        *(get_sb_partitioning(x, bsize)) = subsize;
  1.1694 +      }
  1.1695 +    }
  1.1696 +    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1697 +  }
  1.1698 +
  1.1699 +  // PARTITION_VERT
  1.1700 +  if (partition_vert_allowed && do_rect) {
  1.1701 +    subsize = get_subsize(bsize, PARTITION_VERT);
  1.1702 +
  1.1703 +    *get_sb_index(x, subsize) = 0;
  1.1704 +    if (cpi->sf.adaptive_motion_search)
  1.1705 +      load_pred_mv(x, get_block_context(x, bsize));
  1.1706 +    pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
  1.1707 +                  get_block_context(x, subsize), best_rd);
  1.1708 +    sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1709 +    if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
  1.1710 +      update_state(cpi, get_block_context(x, subsize), subsize, 0);
  1.1711 +      encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
  1.1712 +
  1.1713 +      *get_sb_index(x, subsize) = 1;
  1.1714 +      if (cpi->sf.adaptive_motion_search)
  1.1715 +        load_pred_mv(x, get_block_context(x, bsize));
  1.1716 +      pick_sb_modes(cpi, tile, mi_row, mi_col + ms, &this_rate,
  1.1717 +                    &this_dist, subsize, get_block_context(x, subsize),
  1.1718 +                    best_rd - sum_rd);
  1.1719 +      if (this_rate == INT_MAX) {
  1.1720 +        sum_rd = INT64_MAX;
  1.1721 +      } else {
  1.1722 +        sum_rate += this_rate;
  1.1723 +        sum_dist += this_dist;
  1.1724 +        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1725 +      }
  1.1726 +    }
  1.1727 +    if (sum_rd < best_rd) {
  1.1728 +      pl = partition_plane_context(cpi->above_seg_context,
  1.1729 +                                   cpi->left_seg_context,
  1.1730 +                                   mi_row, mi_col, bsize);
  1.1731 +      sum_rate += x->partition_cost[pl][PARTITION_VERT];
  1.1732 +      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
  1.1733 +      if (sum_rd < best_rd) {
  1.1734 +        best_rate = sum_rate;
  1.1735 +        best_dist = sum_dist;
  1.1736 +        best_rd = sum_rd;
  1.1737 +        *(get_sb_partitioning(x, bsize)) = subsize;
  1.1738 +      }
  1.1739 +    }
  1.1740 +    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  1.1741 +  }
  1.1742 +
  1.1743 +
  1.1744 +  *rate = best_rate;
  1.1745 +  *dist = best_dist;
  1.1746 +
  1.1747 +  if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon)
  1.1748 +    encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
  1.1749 +  if (bsize == BLOCK_64X64) {
  1.1750 +    assert(tp_orig < *tp);
  1.1751 +    assert(best_rate < INT_MAX);
  1.1752 +    assert(best_dist < INT_MAX);
  1.1753 +  } else {
  1.1754 +    assert(tp_orig == *tp);
  1.1755 +  }
  1.1756 +}
  1.1757 +
  1.1758 +// Examines 64x64 block and chooses a best reference frame
  1.1759 +static void rd_pick_reference_frame(VP9_COMP *cpi, const TileInfo *const tile,
  1.1760 +                                    int mi_row, int mi_col) {
  1.1761 +  VP9_COMMON * const cm = &cpi->common;
  1.1762 +  MACROBLOCK * const x = &cpi->mb;
  1.1763 +  int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl;
  1.1764 +  int ms = bs / 2;
  1.1765 +  ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  1.1766 +  PARTITION_CONTEXT sl[8], sa[8];
  1.1767 +  int pl;
  1.1768 +  int r;
  1.1769 +  int64_t d;
  1.1770 +
  1.1771 +  save_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
  1.1772 +
  1.1773 +  // Default is non mask (all reference frames allowed.
  1.1774 +  cpi->ref_frame_mask = 0;
  1.1775 +
  1.1776 +  // Do RD search for 64x64.
  1.1777 +  if ((mi_row + (ms >> 1) < cm->mi_rows) &&
  1.1778 +      (mi_col + (ms >> 1) < cm->mi_cols)) {
  1.1779 +    cpi->set_ref_frame_mask = 1;
  1.1780 +    pick_sb_modes(cpi, tile, mi_row, mi_col, &r, &d, BLOCK_64X64,
  1.1781 +                  get_block_context(x, BLOCK_64X64), INT64_MAX);
  1.1782 +    pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
  1.1783 +                                 mi_row, mi_col, BLOCK_64X64);
  1.1784 +    r += x->partition_cost[pl][PARTITION_NONE];
  1.1785 +
  1.1786 +    *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64;
  1.1787 +    cpi->set_ref_frame_mask = 0;
  1.1788 +  }
  1.1789 +
  1.1790 +  restore_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
  1.1791 +}
  1.1792 +
  1.1793 +static void encode_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
  1.1794 +                          int mi_row, TOKENEXTRA **tp) {
  1.1795 +  VP9_COMMON * const cm = &cpi->common;
  1.1796 +  int mi_col;
  1.1797 +
  1.1798 +  // Initialize the left context for the new SB row
  1.1799 +  vpx_memset(&cpi->left_context, 0, sizeof(cpi->left_context));
  1.1800 +  vpx_memset(cpi->left_seg_context, 0, sizeof(cpi->left_seg_context));
  1.1801 +
  1.1802 +  // Code each SB in the row
  1.1803 +  for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  1.1804 +       mi_col += MI_BLOCK_SIZE) {
  1.1805 +    int dummy_rate;
  1.1806 +    int64_t dummy_dist;
  1.1807 +
  1.1808 +    vp9_zero(cpi->mb.pred_mv);
  1.1809 +
  1.1810 +    if (cpi->sf.reference_masking)
  1.1811 +      rd_pick_reference_frame(cpi, tile, mi_row, mi_col);
  1.1812 +
  1.1813 +    if (cpi->sf.use_lastframe_partitioning ||
  1.1814 +        cpi->sf.use_one_partition_size_always ) {
  1.1815 +      const int idx_str = cm->mode_info_stride * mi_row + mi_col;
  1.1816 +      MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
  1.1817 +      MODE_INFO **prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
  1.1818 +
  1.1819 +      cpi->mb.source_variance = UINT_MAX;
  1.1820 +      if (cpi->sf.use_one_partition_size_always) {
  1.1821 +        set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
  1.1822 +        set_partitioning(cpi, tile, mi_8x8, mi_row, mi_col);
  1.1823 +        rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
  1.1824 +                         &dummy_rate, &dummy_dist, 1);
  1.1825 +      } else {
  1.1826 +        if ((cpi->common.current_video_frame
  1.1827 +            % cpi->sf.last_partitioning_redo_frequency) == 0
  1.1828 +            || cm->prev_mi == 0
  1.1829 +            || cpi->common.show_frame == 0
  1.1830 +            || cpi->common.frame_type == KEY_FRAME
  1.1831 +            || cpi->is_src_frame_alt_ref
  1.1832 +            || ((cpi->sf.use_lastframe_partitioning ==
  1.1833 +                 LAST_FRAME_PARTITION_LOW_MOTION) &&
  1.1834 +                 sb_has_motion(cpi, prev_mi_8x8))) {
  1.1835 +          // If required set upper and lower partition size limits
  1.1836 +          if (cpi->sf.auto_min_max_partition_size) {
  1.1837 +            set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
  1.1838 +            rd_auto_partition_range(cpi, tile, mi_row, mi_col,
  1.1839 +                                    &cpi->sf.min_partition_size,
  1.1840 +                                    &cpi->sf.max_partition_size);
  1.1841 +          }
  1.1842 +          rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
  1.1843 +                            &dummy_rate, &dummy_dist, 1, INT64_MAX);
  1.1844 +        } else {
  1.1845 +          copy_partitioning(cpi, mi_8x8, prev_mi_8x8);
  1.1846 +          rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
  1.1847 +                           &dummy_rate, &dummy_dist, 1);
  1.1848 +        }
  1.1849 +      }
  1.1850 +    } else {
  1.1851 +      // If required set upper and lower partition size limits
  1.1852 +      if (cpi->sf.auto_min_max_partition_size) {
  1.1853 +        set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
  1.1854 +        rd_auto_partition_range(cpi, tile, mi_row, mi_col,
  1.1855 +                                &cpi->sf.min_partition_size,
  1.1856 +                                &cpi->sf.max_partition_size);
  1.1857 +      }
  1.1858 +      rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
  1.1859 +                        &dummy_rate, &dummy_dist, 1, INT64_MAX);
  1.1860 +    }
  1.1861 +  }
  1.1862 +}
  1.1863 +
  1.1864 +static void init_encode_frame_mb_context(VP9_COMP *cpi) {
  1.1865 +  MACROBLOCK *const x = &cpi->mb;
  1.1866 +  VP9_COMMON *const cm = &cpi->common;
  1.1867 +  MACROBLOCKD *const xd = &x->e_mbd;
  1.1868 +  const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1.1869 +
  1.1870 +  x->act_zbin_adj = 0;
  1.1871 +  cpi->seg0_idx = 0;
  1.1872 +
  1.1873 +  xd->mode_info_stride = cm->mode_info_stride;
  1.1874 +
  1.1875 +  // reset intra mode contexts
  1.1876 +  if (frame_is_intra_only(cm))
  1.1877 +    vp9_init_mbmode_probs(cm);
  1.1878 +
  1.1879 +  // Copy data over into macro block data structures.
  1.1880 +  vp9_setup_src_planes(x, cpi->Source, 0, 0);
  1.1881 +
  1.1882 +  // TODO(jkoleszar): are these initializations required?
  1.1883 +  setup_pre_planes(xd, 0, &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]],
  1.1884 +                   0, 0, NULL);
  1.1885 +  setup_dst_planes(xd, get_frame_new_buffer(cm), 0, 0);
  1.1886 +
  1.1887 +  setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
  1.1888 +
  1.1889 +  xd->mi_8x8[0]->mbmi.mode = DC_PRED;
  1.1890 +  xd->mi_8x8[0]->mbmi.uv_mode = DC_PRED;
  1.1891 +
  1.1892 +  vp9_zero(cpi->y_mode_count);
  1.1893 +  vp9_zero(cpi->y_uv_mode_count);
  1.1894 +  vp9_zero(cm->counts.inter_mode);
  1.1895 +  vp9_zero(cpi->partition_count);
  1.1896 +  vp9_zero(cpi->intra_inter_count);
  1.1897 +  vp9_zero(cpi->comp_inter_count);
  1.1898 +  vp9_zero(cpi->single_ref_count);
  1.1899 +  vp9_zero(cpi->comp_ref_count);
  1.1900 +  vp9_zero(cm->counts.tx);
  1.1901 +  vp9_zero(cm->counts.mbskip);
  1.1902 +
  1.1903 +  // Note: this memset assumes above_context[0], [1] and [2]
  1.1904 +  // are allocated as part of the same buffer.
  1.1905 +  vpx_memset(cpi->above_context[0], 0,
  1.1906 +             sizeof(*cpi->above_context[0]) *
  1.1907 +             2 * aligned_mi_cols * MAX_MB_PLANE);
  1.1908 +  vpx_memset(cpi->above_seg_context, 0,
  1.1909 +             sizeof(*cpi->above_seg_context) * aligned_mi_cols);
  1.1910 +}
  1.1911 +
  1.1912 +static void switch_lossless_mode(VP9_COMP *cpi, int lossless) {
  1.1913 +  if (lossless) {
  1.1914 +    // printf("Switching to lossless\n");
  1.1915 +    cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
  1.1916 +    cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
  1.1917 +    cpi->mb.optimize = 0;
  1.1918 +    cpi->common.lf.filter_level = 0;
  1.1919 +    cpi->zbin_mode_boost_enabled = 0;
  1.1920 +    cpi->common.tx_mode = ONLY_4X4;
  1.1921 +  } else {
  1.1922 +    // printf("Not lossless\n");
  1.1923 +    cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
  1.1924 +    cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
  1.1925 +  }
  1.1926 +}
  1.1927 +
  1.1928 +static void switch_tx_mode(VP9_COMP *cpi) {
  1.1929 +  if (cpi->sf.tx_size_search_method == USE_LARGESTALL &&
  1.1930 +      cpi->common.tx_mode >= ALLOW_32X32)
  1.1931 +    cpi->common.tx_mode = ALLOW_32X32;
  1.1932 +}
  1.1933 +
  1.1934 +static void encode_frame_internal(VP9_COMP *cpi) {
  1.1935 +  int mi_row;
  1.1936 +  MACROBLOCK * const x = &cpi->mb;
  1.1937 +  VP9_COMMON * const cm = &cpi->common;
  1.1938 +  MACROBLOCKD * const xd = &x->e_mbd;
  1.1939 +
  1.1940 +//  fprintf(stderr, "encode_frame_internal frame %d (%d) type %d\n",
  1.1941 +//           cpi->common.current_video_frame, cpi->common.show_frame,
  1.1942 +//           cm->frame_type);
  1.1943 +
  1.1944 +// debug output
  1.1945 +#if DBG_PRNT_SEGMAP
  1.1946 +  {
  1.1947 +    FILE *statsfile;
  1.1948 +    statsfile = fopen("segmap2.stt", "a");
  1.1949 +    fprintf(statsfile, "\n");
  1.1950 +    fclose(statsfile);
  1.1951 +  }
  1.1952 +#endif
  1.1953 +
  1.1954 +  vp9_zero(cm->counts.switchable_interp);
  1.1955 +  vp9_zero(cpi->tx_stepdown_count);
  1.1956 +
  1.1957 +  xd->mi_8x8 = cm->mi_grid_visible;
  1.1958 +  // required for vp9_frame_init_quantizer
  1.1959 +  xd->mi_8x8[0] = cm->mi;
  1.1960 +
  1.1961 +  xd->last_mi = cm->prev_mi;
  1.1962 +
  1.1963 +  vp9_zero(cpi->NMVcount);
  1.1964 +  vp9_zero(cpi->coef_counts);
  1.1965 +  vp9_zero(cm->counts.eob_branch);
  1.1966 +
  1.1967 +  cpi->mb.e_mbd.lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0
  1.1968 +      && cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
  1.1969 +  switch_lossless_mode(cpi, cpi->mb.e_mbd.lossless);
  1.1970 +
  1.1971 +  vp9_frame_init_quantizer(cpi);
  1.1972 +
  1.1973 +  vp9_initialize_rd_consts(cpi);
  1.1974 +  vp9_initialize_me_consts(cpi, cm->base_qindex);
  1.1975 +  switch_tx_mode(cpi);
  1.1976 +
  1.1977 +  if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
  1.1978 +    // Initialize encode frame context.
  1.1979 +    init_encode_frame_mb_context(cpi);
  1.1980 +
  1.1981 +    // Build a frame level activity map
  1.1982 +    build_activity_map(cpi);
  1.1983 +  }
  1.1984 +
  1.1985 +  // Re-initialize encode frame context.
  1.1986 +  init_encode_frame_mb_context(cpi);
  1.1987 +
  1.1988 +  vp9_zero(cpi->rd_comp_pred_diff);
  1.1989 +  vp9_zero(cpi->rd_filter_diff);
  1.1990 +  vp9_zero(cpi->rd_tx_select_diff);
  1.1991 +  vp9_zero(cpi->rd_tx_select_threshes);
  1.1992 +
  1.1993 +  set_prev_mi(cm);
  1.1994 +
  1.1995 +  {
  1.1996 +    struct vpx_usec_timer emr_timer;
  1.1997 +    vpx_usec_timer_start(&emr_timer);
  1.1998 +
  1.1999 +    {
  1.2000 +      // Take tiles into account and give start/end MB
  1.2001 +      int tile_col, tile_row;
  1.2002 +      TOKENEXTRA *tp = cpi->tok;
  1.2003 +      const int tile_cols = 1 << cm->log2_tile_cols;
  1.2004 +      const int tile_rows = 1 << cm->log2_tile_rows;
  1.2005 +
  1.2006 +      for (tile_row = 0; tile_row < tile_rows; tile_row++) {
  1.2007 +        for (tile_col = 0; tile_col < tile_cols; tile_col++) {
  1.2008 +          TileInfo tile;
  1.2009 +          TOKENEXTRA *tp_old = tp;
  1.2010 +
  1.2011 +          // For each row of SBs in the frame
  1.2012 +          vp9_tile_init(&tile, cm, tile_row, tile_col);
  1.2013 +          for (mi_row = tile.mi_row_start;
  1.2014 +               mi_row < tile.mi_row_end; mi_row += 8)
  1.2015 +            encode_sb_row(cpi, &tile, mi_row, &tp);
  1.2016 +
  1.2017 +          cpi->tok_count[tile_row][tile_col] = (unsigned int)(tp - tp_old);
  1.2018 +          assert(tp - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols));
  1.2019 +        }
  1.2020 +      }
  1.2021 +    }
  1.2022 +
  1.2023 +    vpx_usec_timer_mark(&emr_timer);
  1.2024 +    cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
  1.2025 +  }
  1.2026 +
  1.2027 +  if (cpi->sf.skip_encode_sb) {
  1.2028 +    int j;
  1.2029 +    unsigned int intra_count = 0, inter_count = 0;
  1.2030 +    for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
  1.2031 +      intra_count += cpi->intra_inter_count[j][0];
  1.2032 +      inter_count += cpi->intra_inter_count[j][1];
  1.2033 +    }
  1.2034 +    cpi->sf.skip_encode_frame = ((intra_count << 2) < inter_count);
  1.2035 +    cpi->sf.skip_encode_frame &= (cm->frame_type != KEY_FRAME);
  1.2036 +    cpi->sf.skip_encode_frame &= cm->show_frame;
  1.2037 +  } else {
  1.2038 +    cpi->sf.skip_encode_frame = 0;
  1.2039 +  }
  1.2040 +
  1.2041 +#if 0
  1.2042 +  // Keep record of the total distortion this time around for future use
  1.2043 +  cpi->last_frame_distortion = cpi->frame_distortion;
  1.2044 +#endif
  1.2045 +}
  1.2046 +
  1.2047 +static int check_dual_ref_flags(VP9_COMP *cpi) {
  1.2048 +  const int ref_flags = cpi->ref_frame_flags;
  1.2049 +
  1.2050 +  if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
  1.2051 +    return 0;
  1.2052 +  } else {
  1.2053 +    return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
  1.2054 +        + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
  1.2055 +  }
  1.2056 +}
  1.2057 +
  1.2058 +static int get_skip_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs) {
  1.2059 +  int x, y;
  1.2060 +
  1.2061 +  for (y = 0; y < ymbs; y++) {
  1.2062 +    for (x = 0; x < xmbs; x++) {
  1.2063 +      if (!mi_8x8[y * mis + x]->mbmi.skip_coeff)
  1.2064 +        return 0;
  1.2065 +    }
  1.2066 +  }
  1.2067 +
  1.2068 +  return 1;
  1.2069 +}
  1.2070 +
  1.2071 +static void set_txfm_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs,
  1.2072 +                          TX_SIZE tx_size) {
  1.2073 +  int x, y;
  1.2074 +
  1.2075 +  for (y = 0; y < ymbs; y++) {
  1.2076 +    for (x = 0; x < xmbs; x++)
  1.2077 +      mi_8x8[y * mis + x]->mbmi.tx_size = tx_size;
  1.2078 +  }
  1.2079 +}
  1.2080 +
  1.2081 +static void reset_skip_txfm_size_b(VP9_COMP *cpi, MODE_INFO **mi_8x8,
  1.2082 +                                   int mis, TX_SIZE max_tx_size, int bw, int bh,
  1.2083 +                                   int mi_row, int mi_col, BLOCK_SIZE bsize) {
  1.2084 +  VP9_COMMON * const cm = &cpi->common;
  1.2085 +
  1.2086 +  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) {
  1.2087 +    return;
  1.2088 +  } else {
  1.2089 +    MB_MODE_INFO * const mbmi = &mi_8x8[0]->mbmi;
  1.2090 +    if (mbmi->tx_size > max_tx_size) {
  1.2091 +      const int ymbs = MIN(bh, cm->mi_rows - mi_row);
  1.2092 +      const int xmbs = MIN(bw, cm->mi_cols - mi_col);
  1.2093 +
  1.2094 +      assert(vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
  1.2095 +             get_skip_flag(mi_8x8, mis, ymbs, xmbs));
  1.2096 +      set_txfm_flag(mi_8x8, mis, ymbs, xmbs, max_tx_size);
  1.2097 +    }
  1.2098 +  }
  1.2099 +}
  1.2100 +
  1.2101 +static void reset_skip_txfm_size_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8,
  1.2102 +                                    TX_SIZE max_tx_size, int mi_row, int mi_col,
  1.2103 +                                    BLOCK_SIZE bsize) {
  1.2104 +  VP9_COMMON * const cm = &cpi->common;
  1.2105 +  const int mis = cm->mode_info_stride;
  1.2106 +  int bw, bh;
  1.2107 +  const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
  1.2108 +
  1.2109 +  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
  1.2110 +    return;
  1.2111 +
  1.2112 +  bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type];
  1.2113 +  bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type];
  1.2114 +
  1.2115 +  if (bw == bs && bh == bs) {
  1.2116 +    reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, bs, mi_row,
  1.2117 +                           mi_col, bsize);
  1.2118 +  } else if (bw == bs && bh < bs) {
  1.2119 +    reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, hbs, mi_row,
  1.2120 +                           mi_col, bsize);
  1.2121 +    reset_skip_txfm_size_b(cpi, mi_8x8 + hbs * mis, mis, max_tx_size, bs, hbs,
  1.2122 +                           mi_row + hbs, mi_col, bsize);
  1.2123 +  } else if (bw < bs && bh == bs) {
  1.2124 +    reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, hbs, bs, mi_row,
  1.2125 +                           mi_col, bsize);
  1.2126 +    reset_skip_txfm_size_b(cpi, mi_8x8 + hbs, mis, max_tx_size, hbs, bs, mi_row,
  1.2127 +                           mi_col + hbs, bsize);
  1.2128 +
  1.2129 +  } else {
  1.2130 +    const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
  1.2131 +    int n;
  1.2132 +
  1.2133 +    assert(bw < bs && bh < bs);
  1.2134 +
  1.2135 +    for (n = 0; n < 4; n++) {
  1.2136 +      const int mi_dc = hbs * (n & 1);
  1.2137 +      const int mi_dr = hbs * (n >> 1);
  1.2138 +
  1.2139 +      reset_skip_txfm_size_sb(cpi, &mi_8x8[mi_dr * mis + mi_dc], max_tx_size,
  1.2140 +                              mi_row + mi_dr, mi_col + mi_dc, subsize);
  1.2141 +    }
  1.2142 +  }
  1.2143 +}
  1.2144 +
  1.2145 +static void reset_skip_txfm_size(VP9_COMP *cpi, TX_SIZE txfm_max) {
  1.2146 +  VP9_COMMON * const cm = &cpi->common;
  1.2147 +  int mi_row, mi_col;
  1.2148 +  const int mis = cm->mode_info_stride;
  1.2149 +//  MODE_INFO *mi, *mi_ptr = cm->mi;
  1.2150 +  MODE_INFO **mi_8x8, **mi_ptr = cm->mi_grid_visible;
  1.2151 +
  1.2152 +  for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) {
  1.2153 +    mi_8x8 = mi_ptr;
  1.2154 +    for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8, mi_8x8 += 8) {
  1.2155 +      reset_skip_txfm_size_sb(cpi, mi_8x8, txfm_max, mi_row, mi_col,
  1.2156 +                              BLOCK_64X64);
  1.2157 +    }
  1.2158 +  }
  1.2159 +}
  1.2160 +
  1.2161 +static int get_frame_type(VP9_COMP *cpi) {
  1.2162 +  int frame_type;
  1.2163 +  if (frame_is_intra_only(&cpi->common))
  1.2164 +    frame_type = 0;
  1.2165 +  else if (cpi->is_src_frame_alt_ref && cpi->refresh_golden_frame)
  1.2166 +    frame_type = 3;
  1.2167 +  else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
  1.2168 +    frame_type = 1;
  1.2169 +  else
  1.2170 +    frame_type = 2;
  1.2171 +  return frame_type;
  1.2172 +}
  1.2173 +
  1.2174 +static void select_tx_mode(VP9_COMP *cpi) {
  1.2175 +  if (cpi->oxcf.lossless) {
  1.2176 +    cpi->common.tx_mode = ONLY_4X4;
  1.2177 +  } else if (cpi->common.current_video_frame == 0) {
  1.2178 +    cpi->common.tx_mode = TX_MODE_SELECT;
  1.2179 +  } else {
  1.2180 +    if (cpi->sf.tx_size_search_method == USE_LARGESTALL) {
  1.2181 +      cpi->common.tx_mode = ALLOW_32X32;
  1.2182 +    } else if (cpi->sf.tx_size_search_method == USE_FULL_RD) {
  1.2183 +      int frame_type = get_frame_type(cpi);
  1.2184 +      cpi->common.tx_mode =
  1.2185 +          cpi->rd_tx_select_threshes[frame_type][ALLOW_32X32]
  1.2186 +          > cpi->rd_tx_select_threshes[frame_type][TX_MODE_SELECT] ?
  1.2187 +          ALLOW_32X32 : TX_MODE_SELECT;
  1.2188 +    } else {
  1.2189 +      unsigned int total = 0;
  1.2190 +      int i;
  1.2191 +      for (i = 0; i < TX_SIZES; ++i)
  1.2192 +        total += cpi->tx_stepdown_count[i];
  1.2193 +      if (total) {
  1.2194 +        double fraction = (double)cpi->tx_stepdown_count[0] / total;
  1.2195 +        cpi->common.tx_mode = fraction > 0.90 ? ALLOW_32X32 : TX_MODE_SELECT;
  1.2196 +        // printf("fraction = %f\n", fraction);
  1.2197 +      }  // else keep unchanged
  1.2198 +    }
  1.2199 +  }
  1.2200 +}
  1.2201 +
  1.2202 +void vp9_encode_frame(VP9_COMP *cpi) {
  1.2203 +  VP9_COMMON * const cm = &cpi->common;
  1.2204 +
  1.2205 +  // In the longer term the encoder should be generalized to match the
  1.2206 +  // decoder such that we allow compound where one of the 3 buffers has a
  1.2207 +  // different sign bias and that buffer is then the fixed ref. However, this
  1.2208 +  // requires further work in the rd loop. For now the only supported encoder
  1.2209 +  // side behavior is where the ALT ref buffer has opposite sign bias to
  1.2210 +  // the other two.
  1.2211 +  if (!frame_is_intra_only(cm)) {
  1.2212 +    if ((cm->ref_frame_sign_bias[ALTREF_FRAME]
  1.2213 +         == cm->ref_frame_sign_bias[GOLDEN_FRAME])
  1.2214 +        || (cm->ref_frame_sign_bias[ALTREF_FRAME]
  1.2215 +            == cm->ref_frame_sign_bias[LAST_FRAME])) {
  1.2216 +      cm->allow_comp_inter_inter = 0;
  1.2217 +    } else {
  1.2218 +      cm->allow_comp_inter_inter = 1;
  1.2219 +      cm->comp_fixed_ref = ALTREF_FRAME;
  1.2220 +      cm->comp_var_ref[0] = LAST_FRAME;
  1.2221 +      cm->comp_var_ref[1] = GOLDEN_FRAME;
  1.2222 +    }
  1.2223 +  }
  1.2224 +
  1.2225 +  if (cpi->sf.RD) {
  1.2226 +    int i, pred_type;
  1.2227 +    INTERPOLATION_TYPE filter_type;
  1.2228 +    /*
  1.2229 +     * This code does a single RD pass over the whole frame assuming
  1.2230 +     * either compound, single or hybrid prediction as per whatever has
  1.2231 +     * worked best for that type of frame in the past.
  1.2232 +     * It also predicts whether another coding mode would have worked
  1.2233 +     * better that this coding mode. If that is the case, it remembers
  1.2234 +     * that for subsequent frames.
  1.2235 +     * It does the same analysis for transform size selection also.
  1.2236 +     */
  1.2237 +    int frame_type = get_frame_type(cpi);
  1.2238 +
  1.2239 +    /* prediction (compound, single or hybrid) mode selection */
  1.2240 +    if (frame_type == 3 || !cm->allow_comp_inter_inter)
  1.2241 +      pred_type = SINGLE_PREDICTION_ONLY;
  1.2242 +    else if (cpi->rd_prediction_type_threshes[frame_type][1]
  1.2243 +             > cpi->rd_prediction_type_threshes[frame_type][0]
  1.2244 +             && cpi->rd_prediction_type_threshes[frame_type][1]
  1.2245 +             > cpi->rd_prediction_type_threshes[frame_type][2]
  1.2246 +             && check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
  1.2247 +      pred_type = COMP_PREDICTION_ONLY;
  1.2248 +    else if (cpi->rd_prediction_type_threshes[frame_type][0]
  1.2249 +             > cpi->rd_prediction_type_threshes[frame_type][2])
  1.2250 +      pred_type = SINGLE_PREDICTION_ONLY;
  1.2251 +    else
  1.2252 +      pred_type = HYBRID_PREDICTION;
  1.2253 +
  1.2254 +    /* filter type selection */
  1.2255 +    // FIXME(rbultje) for some odd reason, we often select smooth_filter
  1.2256 +    // as default filter for ARF overlay frames. This is a REALLY BAD
  1.2257 +    // IDEA so we explicitly disable it here.
  1.2258 +    if (frame_type != 3 &&
  1.2259 +        cpi->rd_filter_threshes[frame_type][1] >
  1.2260 +            cpi->rd_filter_threshes[frame_type][0] &&
  1.2261 +        cpi->rd_filter_threshes[frame_type][1] >
  1.2262 +            cpi->rd_filter_threshes[frame_type][2] &&
  1.2263 +        cpi->rd_filter_threshes[frame_type][1] >
  1.2264 +            cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
  1.2265 +      filter_type = EIGHTTAP_SMOOTH;
  1.2266 +    } else if (cpi->rd_filter_threshes[frame_type][2] >
  1.2267 +            cpi->rd_filter_threshes[frame_type][0] &&
  1.2268 +        cpi->rd_filter_threshes[frame_type][2] >
  1.2269 +            cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
  1.2270 +      filter_type = EIGHTTAP_SHARP;
  1.2271 +    } else if (cpi->rd_filter_threshes[frame_type][0] >
  1.2272 +                  cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
  1.2273 +      filter_type = EIGHTTAP;
  1.2274 +    } else {
  1.2275 +      filter_type = SWITCHABLE;
  1.2276 +    }
  1.2277 +
  1.2278 +    cpi->mb.e_mbd.lossless = 0;
  1.2279 +    if (cpi->oxcf.lossless) {
  1.2280 +      cpi->mb.e_mbd.lossless = 1;
  1.2281 +    }
  1.2282 +
  1.2283 +    /* transform size selection (4x4, 8x8, 16x16 or select-per-mb) */
  1.2284 +    select_tx_mode(cpi);
  1.2285 +    cpi->common.comp_pred_mode = pred_type;
  1.2286 +    cpi->common.mcomp_filter_type = filter_type;
  1.2287 +    encode_frame_internal(cpi);
  1.2288 +
  1.2289 +    for (i = 0; i < NB_PREDICTION_TYPES; ++i) {
  1.2290 +      const int diff = (int) (cpi->rd_comp_pred_diff[i] / cpi->common.MBs);
  1.2291 +      cpi->rd_prediction_type_threshes[frame_type][i] += diff;
  1.2292 +      cpi->rd_prediction_type_threshes[frame_type][i] >>= 1;
  1.2293 +    }
  1.2294 +
  1.2295 +    for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
  1.2296 +      const int64_t diff = cpi->rd_filter_diff[i] / cpi->common.MBs;
  1.2297 +      cpi->rd_filter_threshes[frame_type][i] =
  1.2298 +          (cpi->rd_filter_threshes[frame_type][i] + diff) / 2;
  1.2299 +    }
  1.2300 +
  1.2301 +    for (i = 0; i < TX_MODES; ++i) {
  1.2302 +      int64_t pd = cpi->rd_tx_select_diff[i];
  1.2303 +      int diff;
  1.2304 +      if (i == TX_MODE_SELECT)
  1.2305 +        pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv,
  1.2306 +                     2048 * (TX_SIZES - 1), 0);
  1.2307 +      diff = (int) (pd / cpi->common.MBs);
  1.2308 +      cpi->rd_tx_select_threshes[frame_type][i] += diff;
  1.2309 +      cpi->rd_tx_select_threshes[frame_type][i] /= 2;
  1.2310 +    }
  1.2311 +
  1.2312 +    if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
  1.2313 +      int single_count_zero = 0;
  1.2314 +      int comp_count_zero = 0;
  1.2315 +
  1.2316 +      for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
  1.2317 +        single_count_zero += cpi->comp_inter_count[i][0];
  1.2318 +        comp_count_zero += cpi->comp_inter_count[i][1];
  1.2319 +      }
  1.2320 +
  1.2321 +      if (comp_count_zero == 0) {
  1.2322 +        cpi->common.comp_pred_mode = SINGLE_PREDICTION_ONLY;
  1.2323 +        vp9_zero(cpi->comp_inter_count);
  1.2324 +      } else if (single_count_zero == 0) {
  1.2325 +        cpi->common.comp_pred_mode = COMP_PREDICTION_ONLY;
  1.2326 +        vp9_zero(cpi->comp_inter_count);
  1.2327 +      }
  1.2328 +    }
  1.2329 +
  1.2330 +    if (cpi->common.tx_mode == TX_MODE_SELECT) {
  1.2331 +      int count4x4 = 0;
  1.2332 +      int count8x8_lp = 0, count8x8_8x8p = 0;
  1.2333 +      int count16x16_16x16p = 0, count16x16_lp = 0;
  1.2334 +      int count32x32 = 0;
  1.2335 +
  1.2336 +      for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
  1.2337 +        count4x4 += cm->counts.tx.p32x32[i][TX_4X4];
  1.2338 +        count4x4 += cm->counts.tx.p16x16[i][TX_4X4];
  1.2339 +        count4x4 += cm->counts.tx.p8x8[i][TX_4X4];
  1.2340 +
  1.2341 +        count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8];
  1.2342 +        count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8];
  1.2343 +        count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8];
  1.2344 +
  1.2345 +        count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16];
  1.2346 +        count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16];
  1.2347 +        count32x32 += cm->counts.tx.p32x32[i][TX_32X32];
  1.2348 +      }
  1.2349 +
  1.2350 +      if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0
  1.2351 +          && count32x32 == 0) {
  1.2352 +        cpi->common.tx_mode = ALLOW_8X8;
  1.2353 +        reset_skip_txfm_size(cpi, TX_8X8);
  1.2354 +      } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0
  1.2355 +                 && count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
  1.2356 +        cpi->common.tx_mode = ONLY_4X4;
  1.2357 +        reset_skip_txfm_size(cpi, TX_4X4);
  1.2358 +      } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
  1.2359 +        cpi->common.tx_mode = ALLOW_32X32;
  1.2360 +      } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
  1.2361 +        cpi->common.tx_mode = ALLOW_16X16;
  1.2362 +        reset_skip_txfm_size(cpi, TX_16X16);
  1.2363 +      }
  1.2364 +    }
  1.2365 +  } else {
  1.2366 +    encode_frame_internal(cpi);
  1.2367 +  }
  1.2368 +}
  1.2369 +
  1.2370 +static void sum_intra_stats(VP9_COMP *cpi, const MODE_INFO *mi) {
  1.2371 +  const MB_PREDICTION_MODE y_mode = mi->mbmi.mode;
  1.2372 +  const MB_PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
  1.2373 +  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
  1.2374 +
  1.2375 +  ++cpi->y_uv_mode_count[y_mode][uv_mode];
  1.2376 +
  1.2377 +  if (bsize < BLOCK_8X8) {
  1.2378 +    int idx, idy;
  1.2379 +    const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  1.2380 +    const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
  1.2381 +    for (idy = 0; idy < 2; idy += num_4x4_blocks_high)
  1.2382 +      for (idx = 0; idx < 2; idx += num_4x4_blocks_wide)
  1.2383 +        ++cpi->y_mode_count[0][mi->bmi[idy * 2 + idx].as_mode];
  1.2384 +  } else {
  1.2385 +    ++cpi->y_mode_count[size_group_lookup[bsize]][y_mode];
  1.2386 +  }
  1.2387 +}
  1.2388 +
  1.2389 +// Experimental stub function to create a per MB zbin adjustment based on
  1.2390 +// some previously calculated measure of MB activity.
  1.2391 +static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x) {
  1.2392 +#if USE_ACT_INDEX
  1.2393 +  x->act_zbin_adj = *(x->mb_activity_ptr);
  1.2394 +#else
  1.2395 +  int64_t a;
  1.2396 +  int64_t b;
  1.2397 +  int64_t act = *(x->mb_activity_ptr);
  1.2398 +
  1.2399 +  // Apply the masking to the RD multiplier.
  1.2400 +  a = act + 4 * cpi->activity_avg;
  1.2401 +  b = 4 * act + cpi->activity_avg;
  1.2402 +
  1.2403 +  if (act > cpi->activity_avg)
  1.2404 +    x->act_zbin_adj = (int) (((int64_t) b + (a >> 1)) / a) - 1;
  1.2405 +  else
  1.2406 +    x->act_zbin_adj = 1 - (int) (((int64_t) a + (b >> 1)) / b);
  1.2407 +#endif
  1.2408 +}
  1.2409 +static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
  1.2410 +                              int mi_row, int mi_col, BLOCK_SIZE bsize) {
  1.2411 +  VP9_COMMON * const cm = &cpi->common;
  1.2412 +  MACROBLOCK * const x = &cpi->mb;
  1.2413 +  MACROBLOCKD * const xd = &x->e_mbd;
  1.2414 +  MODE_INFO **mi_8x8 = xd->mi_8x8;
  1.2415 +  MODE_INFO *mi = mi_8x8[0];
  1.2416 +  MB_MODE_INFO *mbmi = &mi->mbmi;
  1.2417 +  PICK_MODE_CONTEXT *ctx = get_block_context(x, bsize);
  1.2418 +  unsigned int segment_id = mbmi->segment_id;
  1.2419 +  const int mis = cm->mode_info_stride;
  1.2420 +  const int mi_width = num_8x8_blocks_wide_lookup[bsize];
  1.2421 +  const int mi_height = num_8x8_blocks_high_lookup[bsize];
  1.2422 +  x->skip_recode = !x->select_txfm_size && mbmi->sb_type >= BLOCK_8X8;
  1.2423 +  x->skip_optimize = ctx->is_coded;
  1.2424 +  ctx->is_coded = 1;
  1.2425 +  x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
  1.2426 +  x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
  1.2427 +                    x->q_index < QIDX_SKIP_THRESH);
  1.2428 +  if (x->skip_encode)
  1.2429 +    return;
  1.2430 +
  1.2431 +  if (cm->frame_type == KEY_FRAME) {
  1.2432 +    if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
  1.2433 +      adjust_act_zbin(cpi, x);
  1.2434 +      vp9_update_zbin_extra(cpi, x);
  1.2435 +    }
  1.2436 +  } else {
  1.2437 +    vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
  1.2438 +
  1.2439 +    if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
  1.2440 +      // Adjust the zbin based on this MB rate.
  1.2441 +      adjust_act_zbin(cpi, x);
  1.2442 +    }
  1.2443 +
  1.2444 +    // Experimental code. Special case for gf and arf zeromv modes.
  1.2445 +    // Increase zbin size to suppress noise
  1.2446 +    cpi->zbin_mode_boost = 0;
  1.2447 +    if (cpi->zbin_mode_boost_enabled) {
  1.2448 +      if (is_inter_block(mbmi)) {
  1.2449 +        if (mbmi->mode == ZEROMV) {
  1.2450 +          if (mbmi->ref_frame[0] != LAST_FRAME)
  1.2451 +            cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
  1.2452 +          else
  1.2453 +            cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
  1.2454 +        } else if (mbmi->sb_type < BLOCK_8X8) {
  1.2455 +          cpi->zbin_mode_boost = SPLIT_MV_ZBIN_BOOST;
  1.2456 +        } else {
  1.2457 +          cpi->zbin_mode_boost = MV_ZBIN_BOOST;
  1.2458 +        }
  1.2459 +      } else {
  1.2460 +        cpi->zbin_mode_boost = INTRA_ZBIN_BOOST;
  1.2461 +      }
  1.2462 +    }
  1.2463 +
  1.2464 +    vp9_update_zbin_extra(cpi, x);
  1.2465 +  }
  1.2466 +
  1.2467 +  if (!is_inter_block(mbmi)) {
  1.2468 +    vp9_encode_intra_block_y(x, MAX(bsize, BLOCK_8X8));
  1.2469 +    vp9_encode_intra_block_uv(x, MAX(bsize, BLOCK_8X8));
  1.2470 +    if (output_enabled)
  1.2471 +      sum_intra_stats(cpi, mi);
  1.2472 +  } else {
  1.2473 +    int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[0])];
  1.2474 +    YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx];
  1.2475 +    YV12_BUFFER_CONFIG *second_ref_fb = NULL;
  1.2476 +    if (has_second_ref(mbmi)) {
  1.2477 +      idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[1])];
  1.2478 +      second_ref_fb = &cm->yv12_fb[idx];
  1.2479 +    }
  1.2480 +
  1.2481 +    assert(cm->frame_type != KEY_FRAME);
  1.2482 +
  1.2483 +    setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col,
  1.2484 +                     &xd->scale_factor[0]);
  1.2485 +    setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col,
  1.2486 +                     &xd->scale_factor[1]);
  1.2487 +
  1.2488 +    vp9_build_inter_predictors_sb(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
  1.2489 +  }
  1.2490 +
  1.2491 +  if (!is_inter_block(mbmi)) {
  1.2492 +    vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
  1.2493 +  } else if (!x->skip) {
  1.2494 +    vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
  1.2495 +    vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
  1.2496 +  } else {
  1.2497 +    int mb_skip_context = xd->left_available ? mi_8x8[-1]->mbmi.skip_coeff : 0;
  1.2498 +    mb_skip_context += mi_8x8[-mis] ? mi_8x8[-mis]->mbmi.skip_coeff : 0;
  1.2499 +
  1.2500 +    mbmi->skip_coeff = 1;
  1.2501 +    if (output_enabled)
  1.2502 +      cm->counts.mbskip[mb_skip_context][1]++;
  1.2503 +    reset_skip_context(xd, MAX(bsize, BLOCK_8X8));
  1.2504 +  }
  1.2505 +
  1.2506 +  if (output_enabled) {
  1.2507 +    if (cm->tx_mode == TX_MODE_SELECT &&
  1.2508 +        mbmi->sb_type >= BLOCK_8X8  &&
  1.2509 +        !(is_inter_block(mbmi) &&
  1.2510 +            (mbmi->skip_coeff ||
  1.2511 +             vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)))) {
  1.2512 +      const uint8_t context = vp9_get_pred_context_tx_size(xd);
  1.2513 +      ++get_tx_counts(max_txsize_lookup[bsize],
  1.2514 +                      context, &cm->counts.tx)[mbmi->tx_size];
  1.2515 +    } else {
  1.2516 +      int x, y;
  1.2517 +      TX_SIZE sz = tx_mode_to_biggest_tx_size[cm->tx_mode];
  1.2518 +      assert(sizeof(tx_mode_to_biggest_tx_size) /
  1.2519 +             sizeof(tx_mode_to_biggest_tx_size[0]) == TX_MODES);
  1.2520 +      // The new intra coding scheme requires no change of transform size
  1.2521 +      if (is_inter_block(&mi->mbmi)) {
  1.2522 +        if (sz == TX_32X32 && bsize < BLOCK_32X32)
  1.2523 +          sz = TX_16X16;
  1.2524 +        if (sz == TX_16X16 && bsize < BLOCK_16X16)
  1.2525 +          sz = TX_8X8;
  1.2526 +        if (sz == TX_8X8 && bsize < BLOCK_8X8)
  1.2527 +          sz = TX_4X4;
  1.2528 +      } else if (bsize >= BLOCK_8X8) {
  1.2529 +        sz = mbmi->tx_size;
  1.2530 +      } else {
  1.2531 +        sz = TX_4X4;
  1.2532 +      }
  1.2533 +
  1.2534 +      for (y = 0; y < mi_height; y++)
  1.2535 +        for (x = 0; x < mi_width; x++)
  1.2536 +          if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
  1.2537 +            mi_8x8[mis * y + x]->mbmi.tx_size = sz;
  1.2538 +    }
  1.2539 +  }
  1.2540 +}

mercurial