vp9_encodeframe.c 95 KB
Newer Older
Jim Bankoski's avatar
Jim Bankoski committed
} TREE_LEVEL;

static void tree_to_node(void *data, BLOCK_SIZE bsize, vt_node *node) {
Jim Bankoski's avatar
Jim Bankoski committed
  int i;
Jim Bankoski's avatar
Jim Bankoski committed
      v64x64 *vt = (v64x64 *) data;
      node->vt = &vt->vt;
      for (i = 0; i < 4; i++)
        node->split[i] = &vt->split[i].vt.none;
      break;
    }
Jim Bankoski's avatar
Jim Bankoski committed
      v32x32 *vt = (v32x32 *) data;
      node->vt = &vt->vt;
      for (i = 0; i < 4; i++)
        node->split[i] = &vt->split[i].vt.none;
      break;
    }
Jim Bankoski's avatar
Jim Bankoski committed
      v16x16 *vt = (v16x16 *) data;
      node->vt = &vt->vt;
      for (i = 0; i < 4; i++)
        node->split[i] = &vt->split[i].vt.none;
      break;
    }
Jim Bankoski's avatar
Jim Bankoski committed
      v8x8 *vt = (v8x8 *) data;
      node->vt = &vt->vt;
      for (i = 0; i < 4; i++)
        node->split[i] = &vt->split[i];
      break;
    }
    default:
      node->vt = 0;
      for (i = 0; i < 4; i++)
        node->split[i] = 0;
      assert(-1);
  }
}

Jim Bankoski's avatar
Jim Bankoski committed
// Set variance values given sum square error, sum error, count.
static void fill_variance(var *v, int64_t s2, int64_t s, int c) {
  v->sum_square_error = s2;
  v->sum_error = s;
  v->count = c;
Jim Bankoski's avatar
Jim Bankoski committed
  if (c > 0)
    v->variance = (int)(256
Jim Bankoski's avatar
Jim Bankoski committed
        * (v->sum_square_error - v->sum_error * v->sum_error / v->count)
        / v->count);
Jim Bankoski's avatar
Jim Bankoski committed
  else
    v->variance = 0;
Jim Bankoski's avatar
Jim Bankoski committed
}

// Combine 2 variance structures by summing the sum_error, sum_square_error,
// and counts and then calculating the new variance.
void sum_2_variances(var *r, var *a, var*b) {
  fill_variance(r, a->sum_square_error + b->sum_square_error,
                a->sum_error + b->sum_error, a->count + b->count);
}
static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
Jim Bankoski's avatar
Jim Bankoski committed
  vt_node node;
  tree_to_node(data, bsize, &node);
Jim Bankoski's avatar
Jim Bankoski committed
  sum_2_variances(&node.vt->horz[0], node.split[0], node.split[1]);
  sum_2_variances(&node.vt->horz[1], node.split[2], node.split[3]);
  sum_2_variances(&node.vt->vert[0], node.split[0], node.split[2]);
  sum_2_variances(&node.vt->vert[1], node.split[1], node.split[3]);
  sum_2_variances(&node.vt->none, &node.vt->vert[0], &node.vt->vert[1]);
}

#if PERFORM_RANDOM_PARTITIONING
static int set_vt_partitioning(VP9_COMP *cpi, void *data, MODE_INFO *m,
    BLOCK_SIZE block_size, int mi_row,
Jim Bankoski's avatar
Jim Bankoski committed
    int mi_col, int mi_size) {
  VP9_COMMON * const cm = &cpi->common;
  vt_node vt;
  const int mis = cm->mode_info_stride;
  int64_t threshold = 4 * cpi->common.base_qindex * cpi->common.base_qindex;

  tree_to_node(data, block_size, &vt);

  // split none is available only if we have more than half a block size
  // in width and height inside the visible image
  if (mi_col + mi_size < cm->mi_cols && mi_row + mi_size < cm->mi_rows &&
      (rand() & 3) < 1) {
    set_block_size(cm, m, block_size, mis, mi_row, mi_col);
    return 1;
  }

  // vertical split is available on all but the bottom border
  if (mi_row + mi_size < cm->mi_rows && vt.vt->vert[0].variance < threshold
      && (rand() & 3) < 1) {
    set_block_size(cm, m, get_subsize(block_size, PARTITION_VERT), mis, mi_row,
        mi_col);
    return 1;
  }

  // horizontal split is available on all but the right border
  if (mi_col + mi_size < cm->mi_cols && vt.vt->horz[0].variance < threshold
      && (rand() & 3) < 1) {
    set_block_size(cm, m, get_subsize(block_size, PARTITION_HORZ), mis, mi_row,
        mi_col);
    return 1;
  }

  return 0;
}

#else  // !PERFORM_RANDOM_PARTITIONING
static int set_vt_partitioning(VP9_COMP *cpi, void *data, MODE_INFO **m,
                               BLOCK_SIZE bsize, int mi_row,
Jim Bankoski's avatar
Jim Bankoski committed
                               int mi_col, int mi_size) {
  VP9_COMMON * const cm = &cpi->common;
  vt_node vt;
  const int mis = cm->mode_info_stride;
  int64_t threshold = 50 * cpi->common.base_qindex;

  tree_to_node(data, bsize, &vt);
Jim Bankoski's avatar
Jim Bankoski committed

  // split none is available only if we have more than half a block size
  // in width and height inside the visible image
  if (mi_col + mi_size < cm->mi_cols && mi_row + mi_size < cm->mi_rows
      && vt.vt->none.variance < threshold) {
    set_block_size(cm, m, bsize, mis, mi_row, mi_col);
Jim Bankoski's avatar
Jim Bankoski committed
    return 1;
  }

  // vertical split is available on all but the bottom border
  if (mi_row + mi_size < cm->mi_rows && vt.vt->vert[0].variance < threshold
      && vt.vt->vert[1].variance < threshold) {
    set_block_size(cm, m, get_subsize(bsize, PARTITION_VERT), mis, mi_row,
Jim Bankoski's avatar
Jim Bankoski committed
                   mi_col);
    return 1;
Jim Bankoski's avatar
Jim Bankoski committed
  // horizontal split is available on all but the right border
  if (mi_col + mi_size < cm->mi_cols && vt.vt->horz[0].variance < threshold
      && vt.vt->horz[1].variance < threshold) {
    set_block_size(cm, m, get_subsize(bsize, PARTITION_HORZ), mis, mi_row,
Jim Bankoski's avatar
Jim Bankoski committed
                   mi_col);
    return 1;
  }

  return 0;
}
#endif  // PERFORM_RANDOM_PARTITIONING
static void choose_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8,
                                int mi_row, int mi_col) {
Jim Bankoski's avatar
Jim Bankoski committed
  VP9_COMMON * const cm = &cpi->common;
  MACROBLOCK *x = &cpi->mb;
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  const int mis = cm->mode_info_stride;
  // TODO(JBB): More experimentation or testing of this threshold;
  int64_t threshold = 4;
  int i, j, k;
  v64x64 vt;
  unsigned char * s;
  int sp;
Jim Bankoski's avatar
Jim Bankoski committed
  const unsigned char * d;
  int dp;
  int pixels_wide = 64, pixels_high = 64;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
  vp9_zero(vt);
  set_offsets(cpi, mi_row, mi_col, BLOCK_64X64);

  if (xd->mb_to_right_edge < 0)
    pixels_wide += (xd->mb_to_right_edge >> 3);

  if (xd->mb_to_bottom_edge < 0)
    pixels_high += (xd->mb_to_bottom_edge >> 3);

Jim Bankoski's avatar
Jim Bankoski committed
  s = x->plane[0].src.buf;
  sp = x->plane[0].src.stride;

  // TODO(JBB): Clearly the higher the quantizer the fewer partitions we want
  // but this needs more experimentation.
  threshold = threshold * cpi->common.base_qindex * cpi->common.base_qindex;

  d = vp9_64x64_zeros;
  dp = 64;
Jim Bankoski's avatar
Jim Bankoski committed
  if (cm->frame_type != KEY_FRAME) {
    int_mv nearest_mv, near_mv;
    const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, LAST_FRAME)];
    YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx];
Jim Bankoski's avatar
Jim Bankoski committed
    YV12_BUFFER_CONFIG *second_ref_fb = NULL;

    setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col,
                     &xd->scale_factor[0]);
    setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col,
                     &xd->scale_factor[1]);

    xd->this_mi->mbmi.ref_frame[0] = LAST_FRAME;
    xd->this_mi->mbmi.sb_type = BLOCK_64X64;
    vp9_find_best_ref_mvs(xd,
                          mi_8x8[0]->mbmi.ref_mvs[mi_8x8[0]->mbmi.ref_frame[0]],
                          &nearest_mv, &near_mv);
    xd->this_mi->mbmi.mv[0] = nearest_mv;
    vp9_build_inter_predictors_sby(xd, mi_row, mi_col, BLOCK_64X64);
Jim Bankoski's avatar
Jim Bankoski committed
    d = xd->plane[0].dst.buf;
    dp = xd->plane[0].dst.stride;
  }
Jim Bankoski's avatar
Jim Bankoski committed
  // Fill in the entire tree of 8x8 variances for splits.
  for (i = 0; i < 4; i++) {
    const int x32_idx = ((i & 1) << 5);
    const int y32_idx = ((i >> 1) << 5);
    for (j = 0; j < 4; j++) {
Jim Bankoski's avatar
Jim Bankoski committed
      const int x16_idx = x32_idx + ((j & 1) << 4);
      const int y16_idx = y32_idx + ((j >> 1) << 4);
      v16x16 *vst = &vt.split[i].split[j];
Jim Bankoski's avatar
Jim Bankoski committed
      for (k = 0; k < 4; k++) {
        int x_idx = x16_idx + ((k & 1) << 3);
        int y_idx = y16_idx + ((k >> 1) << 3);
        unsigned int sse = 0;
        int sum = 0;
        if (x_idx < pixels_wide && y_idx < pixels_high)
          vp9_get_sse_sum_8x8(s + y_idx * sp + x_idx, sp,
                              d + y_idx * dp + x_idx, dp, &sse, &sum);
        fill_variance(&vst->split[k].vt.none, sse, sum, 64);
      }
Jim Bankoski's avatar
Jim Bankoski committed
    }
  }
  // Fill the rest of the variance tree by summing the split partition
  // values.
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
Jim Bankoski's avatar
Jim Bankoski committed
    }
    fill_variance_tree(&vt.split[i], BLOCK_32X32);
Jim Bankoski's avatar
Jim Bankoski committed
  }
  fill_variance_tree(&vt, BLOCK_64X64);
Jim Bankoski's avatar
Jim Bankoski committed
  // Now go through the entire structure,  splitting every block size until
Jim Bankoski's avatar
Jim Bankoski committed
  // we get to one that's got a variance lower than our threshold,  or we
  // hit 8x8.
  if (!set_vt_partitioning(cpi, &vt, mi_8x8, BLOCK_64X64, mi_row, mi_col,
Jim Bankoski's avatar
Jim Bankoski committed
                           4)) {
    for (i = 0; i < 4; ++i) {
      const int x32_idx = ((i & 1) << 2);
      const int y32_idx = ((i >> 1) << 2);
      if (!set_vt_partitioning(cpi, &vt.split[i], mi_8x8, BLOCK_32X32,
Jim Bankoski's avatar
Jim Bankoski committed
                               (mi_row + y32_idx), (mi_col + x32_idx), 2)) {
        for (j = 0; j < 4; ++j) {
          const int x16_idx = ((j & 1) << 1);
          const int y16_idx = ((j >> 1) << 1);
          if (!set_vt_partitioning(cpi, &vt.split[i].split[j], mi_8x8,
Jim Bankoski's avatar
Jim Bankoski committed
                                   (mi_row + y32_idx + y16_idx),
                                   (mi_col + x32_idx + x16_idx), 1)) {
            for (k = 0; k < 4; ++k) {
              const int x8_idx = (k & 1);
              const int y8_idx = (k >> 1);
              set_block_size(cm, mi_8x8, BLOCK_8X8, mis,
Jim Bankoski's avatar
Jim Bankoski committed
                             (mi_row + y32_idx + y16_idx + y8_idx),
                             (mi_col + x32_idx + x16_idx + x8_idx));
            }
          }
        }
static void rd_use_partition(VP9_COMP *cpi, MODE_INFO **mi_8x8,
                             TOKENEXTRA **tp, int mi_row, int mi_col,
                             BLOCK_SIZE bsize, int *rate, int64_t *dist,
                             int do_recon) {
Jim Bankoski's avatar
Jim Bankoski committed
  VP9_COMMON * const cm = &cpi->common;
  MACROBLOCK * const x = &cpi->mb;
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  const int mis = cm->mode_info_stride;
  int bsl = b_width_log2(bsize);
Jim Bankoski's avatar
Jim Bankoski committed
  const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
  const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
Jim Bankoski's avatar
Jim Bankoski committed
  int ms = num_4x4_blocks_wide / 2;
  int mh = num_4x4_blocks_high / 2;
Jim Bankoski's avatar
Jim Bankoski committed
  int bss = (1 << bsl) / 4;
Jim Bankoski's avatar
Jim Bankoski committed
  int i, pl;
  PARTITION_TYPE partition = PARTITION_NONE;
Jim Bankoski's avatar
Jim Bankoski committed
  ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  PARTITION_CONTEXT sl[8], sa[8];
  int last_part_rate = INT_MAX;
  int64_t last_part_dist = INT_MAX;
  int split_rate = INT_MAX;
  int64_t split_dist = INT_MAX;
  int none_rate = INT_MAX;
  int64_t none_dist = INT_MAX;
  int chosen_rate = INT_MAX;
  int64_t chosen_dist = INT_MAX;
  BLOCK_SIZE sub_subsize = BLOCK_4X4;
  int splits_below = 0;
  BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
Jim Bankoski's avatar
Jim Bankoski committed

  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
    return;

Jim Bankoski's avatar
Jim Bankoski committed
  partition = partition_lookup[bsl][bs_type];
Jim Bankoski's avatar
Jim Bankoski committed

  subsize = get_subsize(bsize, partition);

  if (bsize < BLOCK_8X8) {
    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
    // there is nothing to be done.
    if (xd->ab_index != 0) {
      *rate = 0;
      *dist = 0;
      return;
    }
  } else {
Jim Bankoski's avatar
Jim Bankoski committed
    *(get_sb_partitioning(x, bsize)) = subsize;
  }
  save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  x->fast_ms = 0;
  x->subblock_ref = 0;

  if (cpi->sf.adjust_partitioning_from_last_frame) {
    // Check if any of the sub blocks are further split.
    if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
      sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
      splits_below = 1;
      for (i = 0; i < 4; i++) {
        int jj = i >> 1, ii = i & 0x01;
        MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
        if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
          splits_below = 0;
        }
      }
    }

    // If partition is not none try none unless each of the 4 splits are split
    // even further..
    if (partition != PARTITION_NONE && !splits_below &&
        mi_row + (ms >> 1) < cm->mi_rows &&
        mi_col + (ms >> 1) < cm->mi_cols) {
      *(get_sb_partitioning(x, bsize)) = bsize;
      pick_sb_modes(cpi, mi_row, mi_col, &none_rate, &none_dist, bsize,
                    get_block_context(x, bsize), INT64_MAX);

      set_partition_seg_context(cm, xd, mi_row, mi_col);
      pl = partition_plane_context(xd, bsize);
      none_rate += x->partition_cost[pl][PARTITION_NONE];

      restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
      mi_8x8[0]->mbmi.sb_type = bs_type;
      *(get_sb_partitioning(x, bsize)) = subsize;
    }
  }

Jim Bankoski's avatar
Jim Bankoski committed
  switch (partition) {
    case PARTITION_NONE:
      pick_sb_modes(cpi, mi_row, mi_col, &last_part_rate, &last_part_dist,
                    bsize, get_block_context(x, bsize), INT64_MAX);
Jim Bankoski's avatar
Jim Bankoski committed
      break;
    case PARTITION_HORZ:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, subsize) = 0;
      pick_sb_modes(cpi, mi_row, mi_col, &last_part_rate, &last_part_dist,
                    subsize, get_block_context(x, subsize), INT64_MAX);
Jim Bankoski's avatar
Jim Bankoski committed
      if (last_part_rate != INT_MAX &&
          bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) {
Yaowu Xu's avatar
Yaowu Xu committed
        int rt = 0;
        int64_t dt = 0;
Jim Bankoski's avatar
Jim Bankoski committed
        update_state(cpi, get_block_context(x, subsize), subsize, 0);
        encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
        *get_sb_index(xd, subsize) = 1;
        pick_sb_modes(cpi, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize,
                      get_block_context(x, subsize), INT64_MAX);
Jim Bankoski's avatar
Jim Bankoski committed
        if (rt == INT_MAX || dt == INT_MAX) {
          last_part_rate = INT_MAX;
          last_part_dist = INT_MAX;
          break;
        }

        last_part_rate += rt;
        last_part_dist += dt;
Jim Bankoski's avatar
Jim Bankoski committed
      }
      break;
    case PARTITION_VERT:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, subsize) = 0;
      pick_sb_modes(cpi, mi_row, mi_col, &last_part_rate, &last_part_dist,
                    subsize, get_block_context(x, subsize), INT64_MAX);
Jim Bankoski's avatar
Jim Bankoski committed
      if (last_part_rate != INT_MAX &&
          bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) {
Yaowu Xu's avatar
Yaowu Xu committed
        int rt = 0;
        int64_t dt = 0;
Jim Bankoski's avatar
Jim Bankoski committed
        update_state(cpi, get_block_context(x, subsize), subsize, 0);
        encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
        *get_sb_index(xd, subsize) = 1;
        pick_sb_modes(cpi, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize,
                      get_block_context(x, subsize), INT64_MAX);
Jim Bankoski's avatar
Jim Bankoski committed
        if (rt == INT_MAX || dt == INT_MAX) {
          last_part_rate = INT_MAX;
          last_part_dist = INT_MAX;
          break;
        }
        last_part_rate += rt;
        last_part_dist += dt;
Jim Bankoski's avatar
Jim Bankoski committed
      }
      break;
    case PARTITION_SPLIT:
      // Split partition.
      last_part_rate = 0;
      last_part_dist = 0;
Jim Bankoski's avatar
Jim Bankoski committed
      for (i = 0; i < 4; i++) {
        int x_idx = (i & 1) * (ms >> 1);
        int y_idx = (i >> 1) * (ms >> 1);
Jim Bankoski's avatar
Jim Bankoski committed
        int jj = i >> 1, ii = i & 0x01;
        int rt;
        int64_t dt;
Jim Bankoski's avatar
Jim Bankoski committed

        if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
          continue;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
        *get_sb_index(xd, subsize) = i;
        rd_use_partition(cpi, mi_8x8 + jj * bss * mis + ii * bss, tp,
                         mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
                         i != 3);
        if (rt == INT_MAX || dt == INT_MAX) {
          last_part_rate = INT_MAX;
          last_part_dist = INT_MAX;
          break;
        }
        last_part_rate += rt;
        last_part_dist += dt;
Jim Bankoski's avatar
Jim Bankoski committed
      }
      break;
    default:
      assert(0);
  }
  set_partition_seg_context(cm, xd, mi_row, mi_col);
  pl = partition_plane_context(xd, bsize);
  if (last_part_rate < INT_MAX)
    last_part_rate += x->partition_cost[pl][partition];

  if (cpi->sf.adjust_partitioning_from_last_frame
      && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
      && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows)
      && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) {
    BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
    split_rate = 0;
    split_dist = 0;
    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);

    // Split partition.
    for (i = 0; i < 4; i++) {
Jim Bankoski's avatar
Jim Bankoski committed
      int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2);
      int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2);
      int rt = 0;
      int64_t dt = 0;
      ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
      PARTITION_CONTEXT sl[8], sa[8];

      if ((mi_row + y_idx >= cm->mi_rows)
          || (mi_col + x_idx >= cm->mi_cols))
        continue;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, split_subsize) = i;
      *get_sb_partitioning(x, bsize) = split_subsize;
      *get_sb_partitioning(x, split_subsize) = split_subsize;

      save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);

      pick_sb_modes(cpi, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
                    split_subsize, get_block_context(x, split_subsize),
                    INT64_MAX);

      restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);

Jim Bankoski's avatar
Jim Bankoski committed
      if (rt == INT_MAX || dt == INT_MAX) {
        split_rate = INT_MAX;
        split_dist = INT_MAX;
        break;
      }

      if (i != 3)
        encode_sb(cpi, tp,  mi_row + y_idx, mi_col + x_idx, 0,
                  split_subsize);

      split_rate += rt;
      split_dist += dt;
      set_partition_seg_context(cm, xd, mi_row + y_idx, mi_col + x_idx);
      pl = partition_plane_context(xd, bsize);
      split_rate += x->partition_cost[pl][PARTITION_NONE];
    }
    set_partition_seg_context(cm, xd, mi_row, mi_col);
    pl = partition_plane_context(xd, bsize);
Jim Bankoski's avatar
Jim Bankoski committed
    if (split_rate < INT_MAX) {
      split_rate += x->partition_cost[pl][PARTITION_SPLIT];
Jim Bankoski's avatar
Jim Bankoski committed
      chosen_rate = split_rate;
      chosen_dist = split_dist;
    }
  }

  // If last_part is better set the partitioning to that...
  if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist)
      < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) {
    mi_8x8[0]->mbmi.sb_type = bsize;
    if (bsize >= BLOCK_8X8)
      *(get_sb_partitioning(x, bsize)) = subsize;
    chosen_rate = last_part_rate;
    chosen_dist = last_part_dist;
  }
  // If none was better set the partitioning to that...
  if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)
      > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) {
    if (bsize >= BLOCK_8X8)
      *(get_sb_partitioning(x, bsize)) = bsize;
    chosen_rate = none_rate;
    chosen_dist = none_dist;
  }
Jim Bankoski's avatar
Jim Bankoski committed

  restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);

  // We must have chosen a partitioning and encoding or we'll fail later on.
  // No other opportunities for success.
  if ( bsize == BLOCK_64X64)
    assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX);
    encode_sb(cpi, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
  *rate = chosen_rate;
  *dist = chosen_dist;
static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
  BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
  BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
  BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
};

static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
  BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
  BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64,
  BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
};
// Look at all the mode_info entries for blocks that are part of this
// partition and find the min and max values for sb_type.
// At the moment this is designed to work on a 64x64 SB but could be
// adjusted to use a size parameter.
//
// The min and max are assumed to have been initialized prior to calling this
// function so repeat calls can accumulate a min and max of more than one sb64.
static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8,
                                        BLOCK_SIZE * min_block_size,
                                        BLOCK_SIZE * max_block_size ) {
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  int sb_width_in_blocks = MI_BLOCK_SIZE;
  int sb_height_in_blocks  = MI_BLOCK_SIZE;
  int i, j;
  int index = 0;

  // Check the sb_type for each block that belongs to this region.
  for (i = 0; i < sb_height_in_blocks; ++i) {
    for (j = 0; j < sb_width_in_blocks; ++j) {
      MODE_INFO * mi = mi_8x8[index+j];
      BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
      *min_block_size = MIN(*min_block_size, sb_type);
      *max_block_size = MAX(*max_block_size, sb_type);
    }
    index += xd->mode_info_stride;
  }
}
// Look at neighboring blocks and set a min and max partition size based on
// what they chose.
static void rd_auto_partition_range(VP9_COMP *cpi, int row, int col,
                                    BLOCK_SIZE *min_block_size,
                                    BLOCK_SIZE *max_block_size) {
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  MODE_INFO ** mi_8x8 = xd->mi_8x8;
  const int left_in_image = xd->left_available && mi_8x8[-1];
  const int above_in_image = xd->up_available &&
                             mi_8x8[-xd->mode_info_stride];
  MODE_INFO ** above_sb64_mi_8x8;
  MODE_INFO ** left_sb64_mi_8x8;

  // Frequency check
  if (cpi->sf.auto_min_max_partition_count <= 0) {
    cpi->sf.auto_min_max_partition_count =
      cpi->sf.auto_min_max_partition_interval;
    *min_block_size = BLOCK_4X4;
    *max_block_size = BLOCK_64X64;
  } else {
    --cpi->sf.auto_min_max_partition_count;

    // Set default values if no left or above neighbour
    if (!left_in_image && !above_in_image) {
      *min_block_size = BLOCK_4X4;
      *max_block_size = BLOCK_64X64;
    } else {
      VP9_COMMON *const cm = &cpi->common;
      int row8x8_remaining = cm->cur_tile_mi_row_end - row;
      int col8x8_remaining = cm->cur_tile_mi_col_end - col;
      int bh, bw;

      // Default "min to max" and "max to min"
      *min_block_size = BLOCK_64X64;
      *max_block_size = BLOCK_4X4;

      // Find the min and max partition sizes used in the left SB64
      if (left_in_image) {
        left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE];
        get_sb_partition_size_range(cpi, left_sb64_mi_8x8,
                                    min_block_size, max_block_size);
      }
      // Find the min and max partition sizes used in the above SB64 taking
      // the values found for left as a starting point.
      if (above_in_image) {
        above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE];
        get_sb_partition_size_range(cpi, above_sb64_mi_8x8,
                                    min_block_size, max_block_size);
      }

      // Give a bit of leaway either side of the observed min and max
      *min_block_size = min_partition_size[*min_block_size];
      *max_block_size = max_partition_size[*max_block_size];
      // Check border cases where max and min from neighbours may not be legal.
      *max_block_size = find_partition_size(*max_block_size,
                                            row8x8_remaining, col8x8_remaining,
                                            &bh, &bw);
      *min_block_size = MIN(*min_block_size, *max_block_size);
    }
static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) {
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;

  // Only use 8x8 result for non HD videos.
  // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0;
  int use_8x8 = 1;

  if (cm->frame_type && !cpi->is_src_frame_alt_ref &&
      ((use_8x8 && bsize == BLOCK_16X16) ||
      bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) {
    int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0;
    PICK_MODE_CONTEXT *block_context = NULL;

    if (bsize == BLOCK_16X16) {
      block_context = x->sb8x8_context[xd->sb_index][xd->mb_index];
    } else if (bsize == BLOCK_32X32) {
      block_context = x->mb_context[xd->sb_index];
    } else if (bsize == BLOCK_64X64) {
      block_context = x->sb32_context;
    }

    if (block_context) {
      ref0 = block_context[0].mic.mbmi.ref_frame[0];
      ref1 = block_context[1].mic.mbmi.ref_frame[0];
      ref2 = block_context[2].mic.mbmi.ref_frame[0];
      ref3 = block_context[3].mic.mbmi.ref_frame[0];
    }

    // Currently, only consider 4 inter reference frames.
    if (ref0 && ref1 && ref2 && ref3) {
      int d01, d23, d02, d13;

      // Motion vectors for the four subblocks.
      int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row;
      int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col;
      int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row;
      int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col;
      int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row;
      int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col;
      int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row;
      int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col;

      // Adjust sign if ref is alt_ref.
      if (cm->ref_frame_sign_bias[ref0]) {
        mvr0 *= -1;
        mvc0 *= -1;
      }

      if (cm->ref_frame_sign_bias[ref1]) {
        mvr1 *= -1;
        mvc1 *= -1;
      }

      if (cm->ref_frame_sign_bias[ref2]) {
        mvr2 *= -1;
        mvc2 *= -1;
      }

      if (cm->ref_frame_sign_bias[ref3]) {
        mvr3 *= -1;
        mvc3 *= -1;
      }

      // Calculate mv distances.
      d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1));
      d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3));
      d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2));
      d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3));

      if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH &&
          d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) {
        // Set fast motion search level.
        x->fast_ms = 1;

        if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 &&
            d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) {
          // Set fast motion search level.
          x->fast_ms = 2;

          if (!d01 && !d23 && !d02 && !d13) {
            x->fast_ms = 3;
            x->subblock_ref = ref0;
          }
        }
      }
    }
  }
}

static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
}

static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
  vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
}

// TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
// unlikely to be selected depending on previous rate-distortion optimization
// results, for encoding speed-up.
Jim Bankoski's avatar
Jim Bankoski committed
static void rd_pick_partition(VP9_COMP *cpi, TOKENEXTRA **tp, int mi_row,
                              int mi_col, BLOCK_SIZE bsize, int *rate,
                              int64_t *dist, int do_recon, int64_t best_rd) {
Jim Bankoski's avatar
Jim Bankoski committed
  VP9_COMMON * const cm = &cpi->common;
  MACROBLOCK * const x = &cpi->mb;
  MACROBLOCKD * const xd = &x->e_mbd;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
  const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
Jim Bankoski's avatar
Jim Bankoski committed
  ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
  PARTITION_CONTEXT sl[8], sa[8];
  TOKENEXTRA *tp_orig = *tp;
Jim Bankoski's avatar
Jim Bankoski committed
  int i, pl;
  int this_rate, sum_rate = 0, best_rate = INT_MAX;
  int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
  int do_split = bsize >= BLOCK_8X8;
  int do_rect = 1;
  // Override skipping rectangular partition operations for edge blocks
Dmitry Kovalev's avatar
Dmitry Kovalev committed
  const int force_horz_split = (mi_row + ms >= cm->mi_rows);
  const int force_vert_split = (mi_col + ms >= cm->mi_cols);
  int partition_none_allowed = !force_horz_split && !force_vert_split;
  int partition_horz_allowed = !force_vert_split && bsize >= BLOCK_8X8;
  int partition_vert_allowed = !force_horz_split && bsize >= BLOCK_8X8;
  int partition_split_done = 0;
  (void) *tp_orig;

  if (bsize < BLOCK_8X8) {
    // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
    // there is nothing to be done.
    if (xd->ab_index != 0) {
      *rate = 0;
      *dist = 0;
      return;
    }
  assert(mi_height_log2(bsize) == mi_width_log2(bsize));

  // Determine partition types in search according to the speed features.
  // The threshold set here has to be of square block size.
  if (cpi->sf.auto_min_max_partition_size) {
    partition_none_allowed &= (bsize <= cpi->sf.max_partition_size &&
                               bsize >= cpi->sf.min_partition_size);
    partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size &&
                                bsize >  cpi->sf.min_partition_size) ||
                                force_horz_split);
    partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size &&
                                bsize >  cpi->sf.min_partition_size) ||
                                force_vert_split);
    do_split &= bsize > cpi->sf.min_partition_size;
  }
  if (cpi->sf.use_square_partition_only) {
    partition_horz_allowed &= force_horz_split;
    partition_vert_allowed &= force_vert_split;
  }

Jim Bankoski's avatar
Jim Bankoski committed
  save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
    unsigned int source_variancey;
    vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
    source_variancey = get_sby_perpixel_variance(cpi, x, bsize);
    if (source_variancey < cpi->sf.disable_split_var_thresh) {
      if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
        do_rect = 0;
    }
  // PARTITION_NONE
  if (partition_none_allowed) {
    pick_sb_modes(cpi, mi_row, mi_col, &this_rate, &this_dist, bsize,
                  get_block_context(x, bsize), best_rd);
    if (this_rate != INT_MAX) {
      if (bsize >= BLOCK_8X8) {
        set_partition_seg_context(cm, xd, mi_row, mi_col);
        pl = partition_plane_context(xd, bsize);
        this_rate += x->partition_cost[pl][PARTITION_NONE];
      }
      sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
      if (sum_rd < best_rd) {
        int64_t stop_thresh = 2048;

        best_rate = this_rate;
        best_dist = this_dist;
        best_rd = sum_rd;
        if (bsize >= BLOCK_8X8)
          *(get_sb_partitioning(x, bsize)) = bsize;

        // Adjust threshold according to partition size.
        stop_thresh >>= 8 - (b_width_log2_lookup[bsize] +
            b_height_log2_lookup[bsize]);

        // If obtained distortion is very small, choose current partition
        // and stop splitting.
        if (this_dist < stop_thresh) {
          do_split = 0;
          do_rect = 0;
        }
      }
    }
    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  }
  // store estimated motion vector
  if (cpi->sf.adaptive_motion_search)
    store_pred_mv(x, get_block_context(x, bsize));

  // PARTITION_SPLIT
  sum_rd = 0;
  // TODO(jingning): use the motion vectors given by the above search as
  // the starting point of motion search in the following partition type check.
  if (do_split) {
    subsize = get_subsize(bsize, PARTITION_SPLIT);
    for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      const int x_idx = (i & 1) * ms;
      const int y_idx = (i >> 1) * ms;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, subsize) = i;
      if (cpi->sf.adaptive_motion_search)
        load_pred_mv(x, get_block_context(x, bsize));
      rd_pick_partition(cpi, tp, mi_row + y_idx, mi_col + x_idx, subsize,
                        &this_rate, &this_dist, i != 3, best_rd - sum_rd);

      if (this_rate == INT_MAX) {
        sum_rd = INT64_MAX;
      } else {
        sum_rate += this_rate;
        sum_dist += this_dist;
        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
    if (sum_rd < best_rd && i == 4) {
      set_partition_seg_context(cm, xd, mi_row, mi_col);
      pl = partition_plane_context(xd, bsize);
      sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
      if (sum_rd < best_rd) {
        best_rate = sum_rate;
        best_dist = sum_dist;
        best_rd = sum_rd;
        *(get_sb_partitioning(x, bsize)) = subsize;
      } else {
        // skip rectangular partition test when larger block size
        // gives better rd cost
        if (cpi->sf.less_rectangular_check)
          do_rect &= !partition_none_allowed;
      }
    }
    partition_split_done = 1;
    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  if (partition_split_done &&
      cpi->sf.using_small_partition_info) {
    compute_fast_motion_search_level(cpi, bsize);
  // PARTITION_HORZ
  if (partition_horz_allowed && do_rect) {
    subsize = get_subsize(bsize, PARTITION_HORZ);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
    *get_sb_index(xd, subsize) = 0;
    if (cpi->sf.adaptive_motion_search)
      load_pred_mv(x, get_block_context(x, bsize));
    pick_sb_modes(cpi, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
                  get_block_context(x, subsize), best_rd);
    sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
    if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
      update_state(cpi, get_block_context(x, subsize), subsize, 0);
      encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, subsize) = 1;
      if (cpi->sf.adaptive_motion_search)
        load_pred_mv(x, get_block_context(x, bsize));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      pick_sb_modes(cpi, mi_row + ms, mi_col, &this_rate,
                    &this_dist, subsize, get_block_context(x, subsize),
                    best_rd - sum_rd);
      if (this_rate == INT_MAX) {
        sum_rd = INT64_MAX;
      } else {
        sum_rate += this_rate;
        sum_dist += this_dist;
        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
    if (sum_rd < best_rd) {
      set_partition_seg_context(cm, xd, mi_row, mi_col);
      pl = partition_plane_context(xd, bsize);
      sum_rate += x->partition_cost[pl][PARTITION_HORZ];
      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
      if (sum_rd < best_rd) {
        best_rd = sum_rd;
        best_rate = sum_rate;
        best_dist = sum_dist;
        *(get_sb_partitioning(x, bsize)) = subsize;
    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  }
  // PARTITION_VERT
  if (partition_vert_allowed && do_rect) {
    subsize = get_subsize(bsize, PARTITION_VERT);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
    *get_sb_index(xd, subsize) = 0;
    if (cpi->sf.adaptive_motion_search)
      load_pred_mv(x, get_block_context(x, bsize));
    pick_sb_modes(cpi, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
                  get_block_context(x, subsize), best_rd);
    sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
    if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
      update_state(cpi, get_block_context(x, subsize), subsize, 0);
      encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
      *get_sb_index(xd, subsize) = 1;
      if (cpi->sf.adaptive_motion_search)
        load_pred_mv(x, get_block_context(x, bsize));
Dmitry Kovalev's avatar
Dmitry Kovalev committed
      pick_sb_modes(cpi, mi_row, mi_col + ms, &this_rate,
                    &this_dist, subsize, get_block_context(x, subsize),
                    best_rd - sum_rd);
      if (this_rate == INT_MAX) {
        sum_rd = INT64_MAX;
      } else {
        sum_rate += this_rate;
        sum_dist += this_dist;
        sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
    }
    if (sum_rd < best_rd) {
      set_partition_seg_context(cm, xd, mi_row, mi_col);
      pl = partition_plane_context(xd, bsize);
      sum_rate += x->partition_cost[pl][PARTITION_VERT];
      sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
      if (sum_rd < best_rd) {
        best_rate = sum_rate;
        best_dist = sum_dist;
        best_rd = sum_rd;
        *(get_sb_partitioning(x, bsize)) = subsize;
    restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
  *rate = best_rate;
  *dist = best_dist;
John Koleszar's avatar
John Koleszar committed

  if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon)
    encode_sb(cpi, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
  if (bsize == BLOCK_64X64) {
    assert(tp_orig < *tp);
    assert(best_rate < INT_MAX);
    assert(best_dist < INT_MAX);