16fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org/*
26fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
36fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *
46fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  Use of this source code is governed by a BSD-style license
56fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  that can be found in the LICENSE file in the root of the source
66fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  tree. An additional intellectual property rights grant can be found
76fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  in the file PATENTS.  All contributing project authors may
86fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  be found in the AUTHORS file in the root of the source tree.
96fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org */
106fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
116fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1290c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org#include <limits.h>
13411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.org
146fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org#include "vpx_mem/vpx_mem.h"
15411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.org
166fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org#include "vp9/common/vp9_pred_common.h"
1790c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org#include "vp9/common/vp9_tile_common.h"
186fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1993a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org#include "vp9/encoder/vp9_cost.h"
20411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.org#include "vp9/encoder/vp9_segmentation.h"
216fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
22411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.orgvoid vp9_enable_segmentation(struct segmentation *seg) {
2353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  seg->enabled = 1;
2453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  seg->update_map = 1;
2553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  seg->update_data = 1;
266fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
276fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
28411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.orgvoid vp9_disable_segmentation(struct segmentation *seg) {
2953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  seg->enabled = 0;
30e2064011d36b2008099446503f28e64d445060ecjohannkoenig@chromium.org  seg->update_map = 0;
31e2064011d36b2008099446503f28e64d445060ecjohannkoenig@chromium.org  seg->update_data = 0;
326fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
336fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
34411971f94253c85e1866c281860d6344f6aa0c78fgalligan@chromium.orgvoid vp9_set_segment_data(struct segmentation *seg,
356fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org                          signed char *feature_data,
366fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org                          unsigned char abs_delta) {
3753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  seg->abs_delta = abs_delta;
386fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
3953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  vpx_memcpy(seg->feature_data, feature_data, sizeof(seg->feature_data));
406fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
416fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // TBD ?? Set the feature mask
426fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0,
436fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  //            sizeof(cpi->mb.e_mbd.segment_feature_mask));
446fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
458b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.orgvoid vp9_disable_segfeature(struct segmentation *seg, int segment_id,
468b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org                            SEG_LVL_FEATURES feature_id) {
478b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org  seg->feature_mask[segment_id] &= ~(1 << feature_id);
488b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org}
498b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org
508b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.orgvoid vp9_clear_segdata(struct segmentation *seg, int segment_id,
518b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org                       SEG_LVL_FEATURES feature_id) {
528b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org  seg->feature_data[segment_id][feature_id] = 0;
538b26fe55f3e4daa2311dbd2d95e8ac2b4e080685johannkoenig@chromium.org}
546fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
556fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org// Based on set of segment counts calculate a probability tree
5653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgstatic void calc_segtree_probs(int *segcounts, vp9_prob *segment_tree_probs) {
576fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Work out probabilities of each segment
5810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c01 = segcounts[0] + segcounts[1];
5910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c23 = segcounts[2] + segcounts[3];
6010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c45 = segcounts[4] + segcounts[5];
6110a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c67 = segcounts[6] + segcounts[7];
6210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
6310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[0] = get_binary_prob(c01 + c23, c45 + c67);
6410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[1] = get_binary_prob(c01, c23);
6510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[2] = get_binary_prob(c45, c67);
6610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]);
6710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]);
6810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]);
6910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  segment_tree_probs[6] = get_binary_prob(segcounts[6], segcounts[7]);
706fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
716fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
726fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org// Based on set of segment counts and probabilities calculate a cost estimate
7353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgstatic int cost_segmap(int *segcounts, vp9_prob *probs) {
7410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c01 = segcounts[0] + segcounts[1];
7510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c23 = segcounts[2] + segcounts[3];
7610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c45 = segcounts[4] + segcounts[5];
7710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c67 = segcounts[6] + segcounts[7];
7810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c0123 = c01 + c23;
7910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  const int c4567 = c45 + c67;
806fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
816fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Cost the top node of the tree
8210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  int cost = c0123 * vp9_cost_zero(probs[0]) +
8310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org             c4567 * vp9_cost_one(probs[0]);
8410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
8510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  // Cost subsequent levels
8610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  if (c0123 > 0) {
8710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    cost += c01 * vp9_cost_zero(probs[1]) +
8810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org            c23 * vp9_cost_one(probs[1]);
8910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
9010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    if (c01 > 0)
9110a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      cost += segcounts[0] * vp9_cost_zero(probs[3]) +
9210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org              segcounts[1] * vp9_cost_one(probs[3]);
9310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    if (c23 > 0)
9410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      cost += segcounts[2] * vp9_cost_zero(probs[4]) +
9510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org              segcounts[3] * vp9_cost_one(probs[4]);
9610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  }
9790c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org
9810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  if (c4567 > 0) {
9910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    cost += c45 * vp9_cost_zero(probs[2]) +
10010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org            c67 * vp9_cost_one(probs[2]);
10190c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org
10210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    if (c45 > 0)
10310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      cost += segcounts[4] * vp9_cost_zero(probs[5]) +
10410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org              segcounts[5] * vp9_cost_one(probs[5]);
10510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    if (c67 > 0)
10610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      cost += segcounts[6] * vp9_cost_zero(probs[6]) +
10710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org              segcounts[7] * vp9_cost_one(probs[6]);
10890c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org  }
10990c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org
11090c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org  return cost;
11190c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org}
11290c5310de3dfbd1b1624502616cbc8aaf9ad25f0johannkoenig@chromium.org
11341294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.orgstatic void count_segs(const VP9_COMMON *cm, MACROBLOCKD *xd,
11487997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org                       const TileInfo *tile, MODE_INFO *mi,
115d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org                       int *no_pred_segcounts,
116d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org                       int (*temporal_predictor_count)[2],
11710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                       int *t_unpred_seg_counts,
11810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                       int bw, int bh, int mi_row, int mi_col) {
11910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  int segment_id;
12010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
12110a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
12210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    return;
123d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org
1247765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org  xd->mi = mi;
12587997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org  segment_id = xd->mi[0].src_mi->mbmi.segment_id;
126f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org
127ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
128d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org
129d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org  // Count the number of hits on each segment with no prediction
130d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org  no_pred_segcounts[segment_id]++;
131d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org
132d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org  // Temporal prediction not allowed on key frames
133d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org  if (cm->frame_type != KEY_FRAME) {
13487997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org    const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
135d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org    // Test to see if the segment id matches the predicted value.
13647265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    const int pred_segment_id = vp9_get_segment_id(cm, cm->last_frame_seg_map,
13747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org                                                   bsize, mi_row, mi_col);
13847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    const int pred_flag = pred_segment_id == segment_id;
13947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    const int pred_context = vp9_get_pred_context_seg_id(xd);
140d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org
141d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org    // Store the prediction status for this mb and update counts
142d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org    // as appropriate
14387997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org    xd->mi[0].src_mi->mbmi.seg_id_predicted = pred_flag;
14447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    temporal_predictor_count[pred_context][pred_flag]++;
1456fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1467765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org    // Update the "unpredicted" segment count
14747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    if (!pred_flag)
14810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      t_unpred_seg_counts[segment_id]++;
14910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  }
15010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org}
15110a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
15241294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.orgstatic void count_segs_sb(const VP9_COMMON *cm, MACROBLOCKD *xd,
15387997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org                          const TileInfo *tile, MODE_INFO *mi,
15410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                          int *no_pred_segcounts,
15510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                          int (*temporal_predictor_count)[2],
15610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                          int *t_unpred_seg_counts,
15710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                          int mi_row, int mi_col,
15853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org                          BLOCK_SIZE bsize) {
15993a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org  const int mis = cm->mi_stride;
16053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  int bw, bh;
16153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
16210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
16310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
16410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    return;
16510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
16687997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org  bw = num_8x8_blocks_wide_lookup[mi[0].src_mi->mbmi.sb_type];
16787997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org  bh = num_8x8_blocks_high_lookup[mi[0].src_mi->mbmi.sb_type];
16810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
16953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  if (bw == bs && bh == bs) {
17041294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org    count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
17153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org               t_unpred_seg_counts, bs, bs, mi_row, mi_col);
17253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  } else if (bw == bs && bh < bs) {
17341294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org    count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
17453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org               t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
17541294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org    count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
176f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org               temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
177f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org               mi_row + hbs, mi_col);
17853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  } else if (bw < bs && bh == bs) {
17941294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org    count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
18053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org               t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
18141294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org    count_segs(cm, xd, tile, mi + hbs,
182ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org               no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts,
183ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org               hbs, bs, mi_row, mi_col + hbs);
18410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org  } else {
18553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
18610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    int n;
18710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
18853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    assert(bw < bs && bh < bs);
18910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
19010a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    for (n = 0; n < 4; n++) {
19153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      const int mi_dc = hbs * (n & 1);
19253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org      const int mi_dr = hbs * (n >> 1);
19310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
19441294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org      count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc],
19510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                    no_pred_segcounts, temporal_predictor_count,
19610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                    t_unpred_seg_counts,
19753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org                    mi_row + mi_dr, mi_col + mi_dc, subsize);
19810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    }
199d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org  }
2006fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
2016fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
20241294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.orgvoid vp9_choose_segmap_coding_method(VP9_COMMON *cm, MACROBLOCKD *xd) {
20353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  struct segmentation *seg = &cm->seg;
2046fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2056fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  int no_pred_cost;
2066fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  int t_pred_cost = INT_MAX;
2076fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
20847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  int i, tile_col, mi_row, mi_col;
2096fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
21053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } };
21153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  int no_pred_segcounts[MAX_SEGMENTS] = { 0 };
21253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 };
2136fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
21447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  vp9_prob no_pred_tree[SEG_TREE_PROBS];
21547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  vp9_prob t_pred_tree[SEG_TREE_PROBS];
2166fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  vp9_prob t_nopred_prob[PREDICTION_PROBS];
2176fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2186fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Set default state for the segment tree probabilities and the
2196fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // temporal coding probabilities
22053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
22153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  vpx_memset(seg->pred_probs, 255, sizeof(seg->pred_probs));
2226fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2236fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // First of all generate stats regarding how well the last segment map
2246fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // predicts this one
22547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) {
226ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    TileInfo tile;
22787997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org    MODE_INFO *mi_ptr;
228ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    vp9_tile_init(&tile, cm, 0, tile_col);
2297765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org
23087997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org    mi_ptr = cm->mi + tile.mi_col_start;
23110a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    for (mi_row = 0; mi_row < cm->mi_rows;
2327765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org         mi_row += 8, mi_ptr += 8 * cm->mi_stride) {
23387997d490ae52aa962a985c95b3cddf7f8832641johannkoenig@chromium.org      MODE_INFO *mi = mi_ptr;
234ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org      for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
23547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org           mi_col += 8, mi += 8)
23641294d96d7dbf9bc215b09832a8336c5fb158f0bjohannkoenig@chromium.org        count_segs_sb(cm, xd, &tile, mi, no_pred_segcounts,
237ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org                      temporal_predictor_count, t_unpred_seg_counts,
238ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org                      mi_row, mi_col, BLOCK_64X64);
2396fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    }
2406fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  }
2416fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2426fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Work out probability tree for coding segments without prediction
2436fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // and the cost.
24453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  calc_segtree_probs(no_pred_segcounts, no_pred_tree);
24553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org  no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree);
2466fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2476fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Key frames cannot use temporal prediction
248ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  if (!frame_is_intra_only(cm)) {
2496fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    // Work out probability tree for coding those segments not
2506fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    // predicted using the temporal method and the cost.
25153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    calc_segtree_probs(t_unpred_seg_counts, t_pred_tree);
25253a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree);
2536fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
254ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    // Add in the cost of the signaling for each prediction context.
2556fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    for (i = 0; i < PREDICTION_PROBS; i++) {
25610a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      const int count0 = temporal_predictor_count[i][0];
25710a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      const int count1 = temporal_predictor_count[i][1];
25810a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
25910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      t_nopred_prob[i] = get_binary_prob(count0, count1);
2606fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2616fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org      // Add in the predictor signaling cost
26210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org      t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) +
26310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org                     count1 * vp9_cost_one(t_nopred_prob[i]);
2646fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    }
2656fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  }
2666fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2676fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  // Now choose which coding method to use.
2686fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  if (t_pred_cost < no_pred_cost) {
26953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    seg->temporal_update = 1;
27053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    vpx_memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree));
27153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    vpx_memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob));
2726fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  } else {
27353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    seg->temporal_update = 0;
27453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    vpx_memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree));
2756fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  }
2766fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
277dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org
278dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.orgvoid vp9_reset_segment_features(struct segmentation *seg) {
279dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  // Set up default state for MB feature flags
280dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  seg->enabled = 0;
281dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  seg->update_map = 0;
282dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  seg->update_data = 0;
283dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
284dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org  vp9_clearall_segfeatures(seg);
285dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org}
286