// Based on set of segment counts and probabilities calculate a cost estimate static int cost_segmap(MACROBLOCKD *xd, int *segcounts, vp9_prob *probs) { int cost; int count1, count2; // Cost the top node of the tree count1 = segcounts[0] + segcounts[1]; count2 = segcounts[2] + segcounts[3]; cost = count1 * vp9_cost_zero(probs[0]) + count2 * vp9_cost_one(probs[0]); // Now add the cost of each individual segment branch if (count1 > 0) cost += segcounts[0] * vp9_cost_zero(probs[1]) + segcounts[1] * vp9_cost_one(probs[1]); if (count2 > 0) cost += segcounts[2] * vp9_cost_zero(probs[2]) + segcounts[3] * vp9_cost_one(probs[2]); return cost; }
// Based on set of segment counts and probabilities calculate a cost estimate static int cost_segmap(MACROBLOCKD *xd, int *segcounts, vp9_prob *probs) { const int c01 = segcounts[0] + segcounts[1]; const int c23 = segcounts[2] + segcounts[3]; const int c45 = segcounts[4] + segcounts[5]; const int c67 = segcounts[6] + segcounts[7]; const int c0123 = c01 + c23; const int c4567 = c45 + c67; // Cost the top node of the tree int cost = c0123 * vp9_cost_zero(probs[0]) + c4567 * vp9_cost_one(probs[0]); // Cost subsequent levels if (c0123 > 0) { cost += c01 * vp9_cost_zero(probs[1]) + c23 * vp9_cost_one(probs[1]); if (c01 > 0) cost += segcounts[0] * vp9_cost_zero(probs[3]) + segcounts[1] * vp9_cost_one(probs[3]); if (c23 > 0) cost += segcounts[2] * vp9_cost_zero(probs[4]) + segcounts[3] * vp9_cost_one(probs[4]); } if (c4567 > 0) { cost += c45 * vp9_cost_zero(probs[2]) + c67 * vp9_cost_one(probs[2]); if (c45 > 0) cost += segcounts[4] * vp9_cost_zero(probs[5]) + segcounts[5] * vp9_cost_one(probs[5]); if (c67 > 0) cost += segcounts[6] * vp9_cost_zero(probs[6]) + segcounts[7] * vp9_cost_one(probs[6]); } return cost; }
void vp9_choose_segmap_coding_method(VP9_COMP *cpi) { VP9_COMMON *const cm = &cpi->common; MACROBLOCKD *const xd = &cpi->mb.e_mbd; int no_pred_cost; int t_pred_cost = INT_MAX; int i; int tile_col, mi_row, mi_col; int temporal_predictor_count[PREDICTION_PROBS][2]; int no_pred_segcounts[MAX_MB_SEGMENTS]; int t_unpred_seg_counts[MAX_MB_SEGMENTS]; vp9_prob no_pred_tree[MB_SEG_TREE_PROBS]; vp9_prob t_pred_tree[MB_SEG_TREE_PROBS]; vp9_prob t_nopred_prob[PREDICTION_PROBS]; const int mis = cm->mode_info_stride; MODE_INFO *mi_ptr, *mi; // Set default state for the segment tree probabilities and the // temporal coding probabilities vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs)); vpx_memset(cm->segment_pred_probs, 255, sizeof(cm->segment_pred_probs)); vpx_memset(no_pred_segcounts, 0, sizeof(no_pred_segcounts)); vpx_memset(t_unpred_seg_counts, 0, sizeof(t_unpred_seg_counts)); vpx_memset(temporal_predictor_count, 0, sizeof(temporal_predictor_count)); // First of all generate stats regarding how well the last segment map // predicts this one for (tile_col = 0; tile_col < cm->tile_columns; tile_col++) { vp9_get_tile_col_offsets(cm, tile_col); mi_ptr = cm->mi + cm->cur_tile_mi_col_start; for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) { mi = mi_ptr; for (mi_col = cm->cur_tile_mi_col_start; mi_col < cm->cur_tile_mi_col_end; mi_col += 8, mi += 8) { count_segs_sb(cpi, mi, no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, mi_row, mi_col, BLOCK_SIZE_SB64X64); } } } // Work out probability tree for coding segments without prediction // and the cost. calc_segtree_probs(xd, no_pred_segcounts, no_pred_tree); no_pred_cost = cost_segmap(xd, no_pred_segcounts, no_pred_tree); // Key frames cannot use temporal prediction if (cm->frame_type != KEY_FRAME) { // Work out probability tree for coding those segments not // predicted using the temporal method and the cost. calc_segtree_probs(xd, t_unpred_seg_counts, t_pred_tree); t_pred_cost = cost_segmap(xd, t_unpred_seg_counts, t_pred_tree); // Add in the cost of the signalling for each prediction context for (i = 0; i < PREDICTION_PROBS; i++) { const int count0 = temporal_predictor_count[i][0]; const int count1 = temporal_predictor_count[i][1]; t_nopred_prob[i] = get_binary_prob(count0, count1); // Add in the predictor signaling cost t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) + count1 * vp9_cost_one(t_nopred_prob[i]); } } // Now choose which coding method to use. if (t_pred_cost < no_pred_cost) { cm->temporal_update = 1; vpx_memcpy(xd->mb_segment_tree_probs, t_pred_tree, sizeof(t_pred_tree)); vpx_memcpy(cm->segment_pred_probs, t_nopred_prob, sizeof(t_nopred_prob)); } else { cm->temporal_update = 0; vpx_memcpy(xd->mb_segment_tree_probs, no_pred_tree, sizeof(no_pred_tree)); } }
void vp9_choose_segmap_coding_method(VP9_COMMON *cm, MACROBLOCKD *xd) { struct segmentation *seg = &cm->seg; int no_pred_cost; int t_pred_cost = INT_MAX; int i, tile_col, mi_row, mi_col; int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } }; int no_pred_segcounts[MAX_SEGMENTS] = { 0 }; int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 }; vp9_prob no_pred_tree[SEG_TREE_PROBS]; vp9_prob t_pred_tree[SEG_TREE_PROBS]; vp9_prob t_nopred_prob[PREDICTION_PROBS]; // Set default state for the segment tree probabilities and the // temporal coding probabilities memset(seg->tree_probs, 255, sizeof(seg->tree_probs)); memset(seg->pred_probs, 255, sizeof(seg->pred_probs)); // First of all generate stats regarding how well the last segment map // predicts this one for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) { TileInfo tile; MODE_INFO **mi_ptr; vp9_tile_init(&tile, cm, 0, tile_col); mi_ptr = cm->mi_grid_visible + tile.mi_col_start; for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * cm->mi_stride) { MODE_INFO **mi = mi_ptr; for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end; mi_col += 8, mi += 8) count_segs_sb(cm, xd, &tile, mi, no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, mi_row, mi_col, BLOCK_64X64); } } // Work out probability tree for coding segments without prediction // and the cost. calc_segtree_probs(no_pred_segcounts, no_pred_tree); no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree); // Key frames cannot use temporal prediction if (!frame_is_intra_only(cm)) { // Work out probability tree for coding those segments not // predicted using the temporal method and the cost. calc_segtree_probs(t_unpred_seg_counts, t_pred_tree); t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree); // Add in the cost of the signaling for each prediction context. for (i = 0; i < PREDICTION_PROBS; i++) { const int count0 = temporal_predictor_count[i][0]; const int count1 = temporal_predictor_count[i][1]; t_nopred_prob[i] = get_binary_prob(count0, count1); // Add in the predictor signaling cost t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) + count1 * vp9_cost_one(t_nopred_prob[i]); } } // Now choose which coding method to use. if (t_pred_cost < no_pred_cost) { seg->temporal_update = 1; memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree)); memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob)); } else { seg->temporal_update = 0; memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree)); } }
void vp9_choose_segmap_coding_method(VP9_COMP *cpi) { VP9_COMMON *const cm = &cpi->common; MACROBLOCKD *const xd = &cpi->mb.e_mbd; int i; int tot_count; int no_pred_cost; int t_pred_cost = INT_MAX; int pred_context; int mb_row, mb_col; int segmap_index = 0; unsigned char segment_id; int temporal_predictor_count[PREDICTION_PROBS][2]; int no_pred_segcounts[MAX_MB_SEGMENTS]; int t_unpred_seg_counts[MAX_MB_SEGMENTS]; vp9_prob no_pred_tree[MB_FEATURE_TREE_PROBS]; vp9_prob t_pred_tree[MB_FEATURE_TREE_PROBS]; vp9_prob t_nopred_prob[PREDICTION_PROBS]; #if CONFIG_SUPERBLOCKS const int mis = cm->mode_info_stride; #endif // Set default state for the segment tree probabilities and the // temporal coding probabilities vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs)); vpx_memset(cm->segment_pred_probs, 255, sizeof(cm->segment_pred_probs)); vpx_memset(no_pred_segcounts, 0, sizeof(no_pred_segcounts)); vpx_memset(t_unpred_seg_counts, 0, sizeof(t_unpred_seg_counts)); vpx_memset(temporal_predictor_count, 0, sizeof(temporal_predictor_count)); // First of all generate stats regarding how well the last segment map // predicts this one // Initialize macroblock decoder mode info context for the first mb // in the frame xd->mode_info_context = cm->mi; for (mb_row = 0; mb_row < cm->mb_rows; mb_row += 2) { for (mb_col = 0; mb_col < cm->mb_cols; mb_col += 2) { for (i = 0; i < 4; i++) { static const int dx[4] = { +1, -1, +1, +1 }; static const int dy[4] = { 0, +1, 0, -1 }; int x_idx = i & 1, y_idx = i >> 1; if (mb_col + x_idx >= cm->mb_cols || mb_row + y_idx >= cm->mb_rows) { goto end; } xd->mb_to_top_edge = -((mb_row * 16) << 3); xd->mb_to_left_edge = -((mb_col * 16) << 3); segmap_index = (mb_row + y_idx) * cm->mb_cols + mb_col + x_idx; segment_id = xd->mode_info_context->mbmi.segment_id; #if CONFIG_SUPERBLOCKS if (xd->mode_info_context->mbmi.encoded_as_sb) { if (mb_col + 1 < cm->mb_cols) segment_id = segment_id && xd->mode_info_context[1].mbmi.segment_id; if (mb_row + 1 < cm->mb_rows) { segment_id = segment_id && xd->mode_info_context[mis].mbmi.segment_id; if (mb_col + 1 < cm->mb_cols) segment_id = segment_id && xd->mode_info_context[mis + 1].mbmi.segment_id; } xd->mb_to_bottom_edge = ((cm->mb_rows - 2 - mb_row) * 16) << 3; xd->mb_to_right_edge = ((cm->mb_cols - 2 - mb_col) * 16) << 3; } else { #endif xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3; xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3; #if CONFIG_SUPERBLOCKS } #endif // Count the number of hits on each segment with no prediction no_pred_segcounts[segment_id]++; // Temporal prediction not allowed on key frames if (cm->frame_type != KEY_FRAME) { // Test to see if the segment id matches the predicted value. int seg_predicted = (segment_id == vp9_get_pred_mb_segid(cm, xd, segmap_index)); // Get the segment id prediction context pred_context = vp9_get_pred_context(cm, xd, PRED_SEG_ID); // Store the prediction status for this mb and update counts // as appropriate vp9_set_pred_flag(xd, PRED_SEG_ID, seg_predicted); temporal_predictor_count[pred_context][seg_predicted]++; if (!seg_predicted) // Update the "unpredicted" segment count t_unpred_seg_counts[segment_id]++; } #if CONFIG_SUPERBLOCKS if (xd->mode_info_context->mbmi.encoded_as_sb) { assert(!i); xd->mode_info_context += 2; break; } #endif end: xd->mode_info_context += dx[i] + dy[i] * cm->mode_info_stride; } } // this is to account for the border in mode_info_context xd->mode_info_context -= mb_col; xd->mode_info_context += cm->mode_info_stride * 2; } // Work out probability tree for coding segments without prediction // and the cost. calc_segtree_probs(xd, no_pred_segcounts, no_pred_tree); no_pred_cost = cost_segmap(xd, no_pred_segcounts, no_pred_tree); // Key frames cannot use temporal prediction if (cm->frame_type != KEY_FRAME) { // Work out probability tree for coding those segments not // predicted using the temporal method and the cost. calc_segtree_probs(xd, t_unpred_seg_counts, t_pred_tree); t_pred_cost = cost_segmap(xd, t_unpred_seg_counts, t_pred_tree); // Add in the cost of the signalling for each prediction context for (i = 0; i < PREDICTION_PROBS; i++) { tot_count = temporal_predictor_count[i][0] + temporal_predictor_count[i][1]; // Work out the context probabilities for the segment // prediction flag if (tot_count) { t_nopred_prob[i] = (temporal_predictor_count[i][0] * 255) / tot_count; // Clamp to minimum allowed value if (t_nopred_prob[i] < 1) t_nopred_prob[i] = 1; } else t_nopred_prob[i] = 1; // Add in the predictor signaling cost t_pred_cost += (temporal_predictor_count[i][0] * vp9_cost_zero(t_nopred_prob[i])) + (temporal_predictor_count[i][1] * vp9_cost_one(t_nopred_prob[i])); } } // Now choose which coding method to use. if (t_pred_cost < no_pred_cost) { cm->temporal_update = 1; vpx_memcpy(xd->mb_segment_tree_probs, t_pred_tree, sizeof(t_pred_tree)); vpx_memcpy(&cm->segment_pred_probs, t_nopred_prob, sizeof(t_nopred_prob)); } else { cm->temporal_update = 0; vpx_memcpy(xd->mb_segment_tree_probs, no_pred_tree, sizeof(no_pred_tree)); } }