michael@0: /* michael@0: * Copyright (c) 2012 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp9/encoder/vp9_segmentation.h" michael@0: #include "vp9/common/vp9_pred_common.h" michael@0: #include "vp9/common/vp9_tile_common.h" michael@0: michael@0: void vp9_enable_segmentation(VP9_PTR ptr) { michael@0: VP9_COMP *cpi = (VP9_COMP *)ptr; michael@0: struct segmentation *const seg = &cpi->common.seg; michael@0: michael@0: seg->enabled = 1; michael@0: seg->update_map = 1; michael@0: seg->update_data = 1; michael@0: } michael@0: michael@0: void vp9_disable_segmentation(VP9_PTR ptr) { michael@0: VP9_COMP *cpi = (VP9_COMP *)ptr; michael@0: struct segmentation *const seg = &cpi->common.seg; michael@0: seg->enabled = 0; michael@0: } michael@0: michael@0: void vp9_set_segmentation_map(VP9_PTR ptr, michael@0: unsigned char *segmentation_map) { michael@0: VP9_COMP *cpi = (VP9_COMP *)ptr; michael@0: struct segmentation *const seg = &cpi->common.seg; michael@0: michael@0: // Copy in the new segmentation map michael@0: vpx_memcpy(cpi->segmentation_map, segmentation_map, michael@0: (cpi->common.mi_rows * cpi->common.mi_cols)); michael@0: michael@0: // Signal that the map should be updated. michael@0: seg->update_map = 1; michael@0: seg->update_data = 1; michael@0: } michael@0: michael@0: void vp9_set_segment_data(VP9_PTR ptr, michael@0: signed char *feature_data, michael@0: unsigned char abs_delta) { michael@0: VP9_COMP *cpi = (VP9_COMP *)ptr; michael@0: struct segmentation *const seg = &cpi->common.seg; michael@0: michael@0: seg->abs_delta = abs_delta; michael@0: michael@0: vpx_memcpy(seg->feature_data, feature_data, sizeof(seg->feature_data)); michael@0: michael@0: // TBD ?? Set the feature mask michael@0: // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0, michael@0: // sizeof(cpi->mb.e_mbd.segment_feature_mask)); michael@0: } michael@0: michael@0: // Based on set of segment counts calculate a probability tree michael@0: static void calc_segtree_probs(int *segcounts, vp9_prob *segment_tree_probs) { michael@0: // Work out probabilities of each segment michael@0: const int c01 = segcounts[0] + segcounts[1]; michael@0: const int c23 = segcounts[2] + segcounts[3]; michael@0: const int c45 = segcounts[4] + segcounts[5]; michael@0: const int c67 = segcounts[6] + segcounts[7]; michael@0: michael@0: segment_tree_probs[0] = get_binary_prob(c01 + c23, c45 + c67); michael@0: segment_tree_probs[1] = get_binary_prob(c01, c23); michael@0: segment_tree_probs[2] = get_binary_prob(c45, c67); michael@0: segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]); michael@0: segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]); michael@0: segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]); michael@0: segment_tree_probs[6] = get_binary_prob(segcounts[6], segcounts[7]); michael@0: } michael@0: michael@0: // Based on set of segment counts and probabilities calculate a cost estimate michael@0: static int cost_segmap(int *segcounts, vp9_prob *probs) { michael@0: const int c01 = segcounts[0] + segcounts[1]; michael@0: const int c23 = segcounts[2] + segcounts[3]; michael@0: const int c45 = segcounts[4] + segcounts[5]; michael@0: const int c67 = segcounts[6] + segcounts[7]; michael@0: const int c0123 = c01 + c23; michael@0: const int c4567 = c45 + c67; michael@0: michael@0: // Cost the top node of the tree michael@0: int cost = c0123 * vp9_cost_zero(probs[0]) + michael@0: c4567 * vp9_cost_one(probs[0]); michael@0: michael@0: // Cost subsequent levels michael@0: if (c0123 > 0) { michael@0: cost += c01 * vp9_cost_zero(probs[1]) + michael@0: c23 * vp9_cost_one(probs[1]); michael@0: michael@0: if (c01 > 0) michael@0: cost += segcounts[0] * vp9_cost_zero(probs[3]) + michael@0: segcounts[1] * vp9_cost_one(probs[3]); michael@0: if (c23 > 0) michael@0: cost += segcounts[2] * vp9_cost_zero(probs[4]) + michael@0: segcounts[3] * vp9_cost_one(probs[4]); michael@0: } michael@0: michael@0: if (c4567 > 0) { michael@0: cost += c45 * vp9_cost_zero(probs[2]) + michael@0: c67 * vp9_cost_one(probs[2]); michael@0: michael@0: if (c45 > 0) michael@0: cost += segcounts[4] * vp9_cost_zero(probs[5]) + michael@0: segcounts[5] * vp9_cost_one(probs[5]); michael@0: if (c67 > 0) michael@0: cost += segcounts[6] * vp9_cost_zero(probs[6]) + michael@0: segcounts[7] * vp9_cost_one(probs[6]); michael@0: } michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: static void count_segs(VP9_COMP *cpi, const TileInfo *const tile, michael@0: MODE_INFO **mi_8x8, michael@0: int *no_pred_segcounts, michael@0: int (*temporal_predictor_count)[2], michael@0: int *t_unpred_seg_counts, michael@0: int bw, int bh, int mi_row, int mi_col) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &cpi->mb.e_mbd; michael@0: int segment_id; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: xd->mi_8x8 = mi_8x8; michael@0: segment_id = xd->mi_8x8[0]->mbmi.segment_id; michael@0: michael@0: set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols); michael@0: michael@0: // Count the number of hits on each segment with no prediction michael@0: no_pred_segcounts[segment_id]++; michael@0: michael@0: // Temporal prediction not allowed on key frames michael@0: if (cm->frame_type != KEY_FRAME) { michael@0: const BLOCK_SIZE bsize = mi_8x8[0]->mbmi.sb_type; michael@0: // Test to see if the segment id matches the predicted value. michael@0: const int pred_segment_id = vp9_get_segment_id(cm, cm->last_frame_seg_map, michael@0: bsize, mi_row, mi_col); michael@0: const int pred_flag = pred_segment_id == segment_id; michael@0: const int pred_context = vp9_get_pred_context_seg_id(xd); michael@0: michael@0: // Store the prediction status for this mb and update counts michael@0: // as appropriate michael@0: vp9_set_pred_flag_seg_id(xd, pred_flag); michael@0: temporal_predictor_count[pred_context][pred_flag]++; michael@0: michael@0: if (!pred_flag) michael@0: // Update the "unpredicted" segment count michael@0: t_unpred_seg_counts[segment_id]++; michael@0: } michael@0: } michael@0: michael@0: static void count_segs_sb(VP9_COMP *cpi, const TileInfo *const tile, michael@0: MODE_INFO **mi_8x8, michael@0: int *no_pred_segcounts, michael@0: int (*temporal_predictor_count)[2], michael@0: int *t_unpred_seg_counts, michael@0: int mi_row, int mi_col, michael@0: BLOCK_SIZE bsize) { michael@0: const VP9_COMMON *const cm = &cpi->common; michael@0: const int mis = cm->mode_info_stride; michael@0: int bw, bh; michael@0: const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type]; michael@0: bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type]; michael@0: michael@0: if (bw == bs && bh == bs) { michael@0: count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count, michael@0: t_unpred_seg_counts, bs, bs, mi_row, mi_col); michael@0: } else if (bw == bs && bh < bs) { michael@0: count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count, michael@0: t_unpred_seg_counts, bs, hbs, mi_row, mi_col); michael@0: count_segs(cpi, tile, mi_8x8 + hbs * mis, no_pred_segcounts, michael@0: temporal_predictor_count, t_unpred_seg_counts, bs, hbs, michael@0: mi_row + hbs, mi_col); michael@0: } else if (bw < bs && bh == bs) { michael@0: count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count, michael@0: t_unpred_seg_counts, hbs, bs, mi_row, mi_col); michael@0: count_segs(cpi, tile, mi_8x8 + hbs, michael@0: no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, michael@0: hbs, bs, mi_row, mi_col + hbs); michael@0: } else { michael@0: const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize]; michael@0: int n; michael@0: michael@0: assert(bw < bs && bh < bs); michael@0: michael@0: for (n = 0; n < 4; n++) { michael@0: const int mi_dc = hbs * (n & 1); michael@0: const int mi_dr = hbs * (n >> 1); michael@0: michael@0: count_segs_sb(cpi, tile, &mi_8x8[mi_dr * mis + mi_dc], michael@0: no_pred_segcounts, temporal_predictor_count, michael@0: t_unpred_seg_counts, michael@0: mi_row + mi_dr, mi_col + mi_dc, subsize); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_choose_segmap_coding_method(VP9_COMP *cpi) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: struct segmentation *seg = &cm->seg; michael@0: michael@0: int no_pred_cost; michael@0: int t_pred_cost = INT_MAX; michael@0: michael@0: int i, tile_col, mi_row, mi_col; michael@0: michael@0: int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } }; michael@0: int no_pred_segcounts[MAX_SEGMENTS] = { 0 }; michael@0: int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 }; michael@0: michael@0: vp9_prob no_pred_tree[SEG_TREE_PROBS]; michael@0: vp9_prob t_pred_tree[SEG_TREE_PROBS]; michael@0: vp9_prob t_nopred_prob[PREDICTION_PROBS]; michael@0: michael@0: const int mis = cm->mode_info_stride; michael@0: MODE_INFO **mi_ptr, **mi; michael@0: michael@0: // Set default state for the segment tree probabilities and the michael@0: // temporal coding probabilities michael@0: vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs)); michael@0: vpx_memset(seg->pred_probs, 255, sizeof(seg->pred_probs)); michael@0: michael@0: // First of all generate stats regarding how well the last segment map michael@0: // predicts this one michael@0: for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) { michael@0: TileInfo tile; michael@0: michael@0: vp9_tile_init(&tile, cm, 0, tile_col); michael@0: mi_ptr = cm->mi_grid_visible + tile.mi_col_start; michael@0: for (mi_row = 0; mi_row < cm->mi_rows; michael@0: mi_row += 8, mi_ptr += 8 * mis) { michael@0: mi = mi_ptr; michael@0: for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end; michael@0: mi_col += 8, mi += 8) michael@0: count_segs_sb(cpi, &tile, mi, no_pred_segcounts, michael@0: temporal_predictor_count, t_unpred_seg_counts, michael@0: mi_row, mi_col, BLOCK_64X64); michael@0: } michael@0: } michael@0: michael@0: // Work out probability tree for coding segments without prediction michael@0: // and the cost. michael@0: calc_segtree_probs(no_pred_segcounts, no_pred_tree); michael@0: no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree); michael@0: michael@0: // Key frames cannot use temporal prediction michael@0: if (!frame_is_intra_only(cm)) { michael@0: // Work out probability tree for coding those segments not michael@0: // predicted using the temporal method and the cost. michael@0: calc_segtree_probs(t_unpred_seg_counts, t_pred_tree); michael@0: t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree); michael@0: michael@0: // Add in the cost of the signaling for each prediction context. michael@0: for (i = 0; i < PREDICTION_PROBS; i++) { michael@0: const int count0 = temporal_predictor_count[i][0]; michael@0: const int count1 = temporal_predictor_count[i][1]; michael@0: michael@0: t_nopred_prob[i] = get_binary_prob(count0, count1); michael@0: michael@0: // Add in the predictor signaling cost michael@0: t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) + michael@0: count1 * vp9_cost_one(t_nopred_prob[i]); michael@0: } michael@0: } michael@0: michael@0: // Now choose which coding method to use. michael@0: if (t_pred_cost < no_pred_cost) { michael@0: seg->temporal_update = 1; michael@0: vpx_memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree)); michael@0: vpx_memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob)); michael@0: } else { michael@0: seg->temporal_update = 0; michael@0: vpx_memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree)); michael@0: } michael@0: }