1 /*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <limits.h>
12
13 #include "vpx_mem/vpx_mem.h"
14
15 #include "vp9/common/vp9_pred_common.h"
16 #include "vp9/common/vp9_tile_common.h"
17
18 #include "vp9/encoder/vp9_cost.h"
19 #include "vp9/encoder/vp9_segmentation.h"
20
vp9_enable_segmentation(struct segmentation * seg)21 void vp9_enable_segmentation(struct segmentation *seg) {
22 seg->enabled = 1;
23 seg->update_map = 1;
24 seg->update_data = 1;
25 }
26
vp9_disable_segmentation(struct segmentation * seg)27 void vp9_disable_segmentation(struct segmentation *seg) {
28 seg->enabled = 0;
29 seg->update_map = 0;
30 seg->update_data = 0;
31 }
32
vp9_set_segment_data(struct segmentation * seg,signed char * feature_data,unsigned char abs_delta)33 void vp9_set_segment_data(struct segmentation *seg, signed char *feature_data,
34 unsigned char abs_delta) {
35 seg->abs_delta = abs_delta;
36
37 memcpy(seg->feature_data, feature_data, sizeof(seg->feature_data));
38 }
vp9_disable_segfeature(struct segmentation * seg,int segment_id,SEG_LVL_FEATURES feature_id)39 void vp9_disable_segfeature(struct segmentation *seg, int segment_id,
40 SEG_LVL_FEATURES feature_id) {
41 seg->feature_mask[segment_id] &= ~(1 << feature_id);
42 }
43
vp9_clear_segdata(struct segmentation * seg,int segment_id,SEG_LVL_FEATURES feature_id)44 void vp9_clear_segdata(struct segmentation *seg, int segment_id,
45 SEG_LVL_FEATURES feature_id) {
46 seg->feature_data[segment_id][feature_id] = 0;
47 }
48
49 // Based on set of segment counts calculate a probability tree
calc_segtree_probs(int * segcounts,vpx_prob * segment_tree_probs)50 static void calc_segtree_probs(int *segcounts, vpx_prob *segment_tree_probs) {
51 // Work out probabilities of each segment
52 const int c01 = segcounts[0] + segcounts[1];
53 const int c23 = segcounts[2] + segcounts[3];
54 const int c45 = segcounts[4] + segcounts[5];
55 const int c67 = segcounts[6] + segcounts[7];
56
57 segment_tree_probs[0] = get_binary_prob(c01 + c23, c45 + c67);
58 segment_tree_probs[1] = get_binary_prob(c01, c23);
59 segment_tree_probs[2] = get_binary_prob(c45, c67);
60 segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]);
61 segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]);
62 segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]);
63 segment_tree_probs[6] = get_binary_prob(segcounts[6], segcounts[7]);
64 }
65
66 // Based on set of segment counts and probabilities calculate a cost estimate
cost_segmap(int * segcounts,vpx_prob * probs)67 static int cost_segmap(int *segcounts, vpx_prob *probs) {
68 const int c01 = segcounts[0] + segcounts[1];
69 const int c23 = segcounts[2] + segcounts[3];
70 const int c45 = segcounts[4] + segcounts[5];
71 const int c67 = segcounts[6] + segcounts[7];
72 const int c0123 = c01 + c23;
73 const int c4567 = c45 + c67;
74
75 // Cost the top node of the tree
76 int cost = c0123 * vp9_cost_zero(probs[0]) + c4567 * vp9_cost_one(probs[0]);
77
78 // Cost subsequent levels
79 if (c0123 > 0) {
80 cost += c01 * vp9_cost_zero(probs[1]) + c23 * vp9_cost_one(probs[1]);
81
82 if (c01 > 0)
83 cost += segcounts[0] * vp9_cost_zero(probs[3]) +
84 segcounts[1] * vp9_cost_one(probs[3]);
85 if (c23 > 0)
86 cost += segcounts[2] * vp9_cost_zero(probs[4]) +
87 segcounts[3] * vp9_cost_one(probs[4]);
88 }
89
90 if (c4567 > 0) {
91 cost += c45 * vp9_cost_zero(probs[2]) + c67 * vp9_cost_one(probs[2]);
92
93 if (c45 > 0)
94 cost += segcounts[4] * vp9_cost_zero(probs[5]) +
95 segcounts[5] * vp9_cost_one(probs[5]);
96 if (c67 > 0)
97 cost += segcounts[6] * vp9_cost_zero(probs[6]) +
98 segcounts[7] * vp9_cost_one(probs[6]);
99 }
100
101 return cost;
102 }
103
count_segs(const VP9_COMMON * cm,MACROBLOCKD * xd,const TileInfo * tile,MODE_INFO ** mi,int * no_pred_segcounts,int (* temporal_predictor_count)[2],int * t_unpred_seg_counts,int bw,int bh,int mi_row,int mi_col)104 static void count_segs(const VP9_COMMON *cm, MACROBLOCKD *xd,
105 const TileInfo *tile, MODE_INFO **mi,
106 int *no_pred_segcounts,
107 int (*temporal_predictor_count)[2],
108 int *t_unpred_seg_counts, int bw, int bh, int mi_row,
109 int mi_col) {
110 int segment_id;
111
112 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
113
114 xd->mi = mi;
115 segment_id = xd->mi[0]->segment_id;
116
117 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
118
119 // Count the number of hits on each segment with no prediction
120 no_pred_segcounts[segment_id]++;
121
122 // Temporal prediction not allowed on key frames
123 if (cm->frame_type != KEY_FRAME) {
124 const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
125 // Test to see if the segment id matches the predicted value.
126 const int pred_segment_id =
127 get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col);
128 const int pred_flag = pred_segment_id == segment_id;
129 const int pred_context = vp9_get_pred_context_seg_id(xd);
130
131 // Store the prediction status for this mb and update counts
132 // as appropriate
133 xd->mi[0]->seg_id_predicted = pred_flag;
134 temporal_predictor_count[pred_context][pred_flag]++;
135
136 // Update the "unpredicted" segment count
137 if (!pred_flag) t_unpred_seg_counts[segment_id]++;
138 }
139 }
140
count_segs_sb(const VP9_COMMON * cm,MACROBLOCKD * xd,const TileInfo * tile,MODE_INFO ** mi,int * no_pred_segcounts,int (* temporal_predictor_count)[2],int * t_unpred_seg_counts,int mi_row,int mi_col,BLOCK_SIZE bsize)141 static void count_segs_sb(const VP9_COMMON *cm, MACROBLOCKD *xd,
142 const TileInfo *tile, MODE_INFO **mi,
143 int *no_pred_segcounts,
144 int (*temporal_predictor_count)[2],
145 int *t_unpred_seg_counts, int mi_row, int mi_col,
146 BLOCK_SIZE bsize) {
147 const int mis = cm->mi_stride;
148 int bw, bh;
149 const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
150
151 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
152
153 bw = num_8x8_blocks_wide_lookup[mi[0]->sb_type];
154 bh = num_8x8_blocks_high_lookup[mi[0]->sb_type];
155
156 if (bw == bs && bh == bs) {
157 count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
158 t_unpred_seg_counts, bs, bs, mi_row, mi_col);
159 } else if (bw == bs && bh < bs) {
160 count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
161 t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
162 count_segs(cm, xd, tile, mi + hbs * mis, no_pred_segcounts,
163 temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
164 mi_row + hbs, mi_col);
165 } else if (bw < bs && bh == bs) {
166 count_segs(cm, xd, tile, mi, no_pred_segcounts, temporal_predictor_count,
167 t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
168 count_segs(cm, xd, tile, mi + hbs, no_pred_segcounts,
169 temporal_predictor_count, t_unpred_seg_counts, hbs, bs, mi_row,
170 mi_col + hbs);
171 } else {
172 const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
173 int n;
174
175 assert(bw < bs && bh < bs);
176
177 for (n = 0; n < 4; n++) {
178 const int mi_dc = hbs * (n & 1);
179 const int mi_dr = hbs * (n >> 1);
180
181 count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts,
182 temporal_predictor_count, t_unpred_seg_counts,
183 mi_row + mi_dr, mi_col + mi_dc, subsize);
184 }
185 }
186 }
187
vp9_choose_segmap_coding_method(VP9_COMMON * cm,MACROBLOCKD * xd)188 void vp9_choose_segmap_coding_method(VP9_COMMON *cm, MACROBLOCKD *xd) {
189 struct segmentation *seg = &cm->seg;
190
191 int no_pred_cost;
192 int t_pred_cost = INT_MAX;
193
194 int i, tile_col, mi_row, mi_col;
195
196 int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } };
197 int no_pred_segcounts[MAX_SEGMENTS] = { 0 };
198 int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 };
199
200 vpx_prob no_pred_tree[SEG_TREE_PROBS];
201 vpx_prob t_pred_tree[SEG_TREE_PROBS];
202 vpx_prob t_nopred_prob[PREDICTION_PROBS];
203
204 // Set default state for the segment tree probabilities and the
205 // temporal coding probabilities
206 memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
207 memset(seg->pred_probs, 255, sizeof(seg->pred_probs));
208
209 // First of all generate stats regarding how well the last segment map
210 // predicts this one
211 for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) {
212 TileInfo tile;
213 MODE_INFO **mi_ptr;
214 vp9_tile_init(&tile, cm, 0, tile_col);
215
216 mi_ptr = cm->mi_grid_visible + tile.mi_col_start;
217 for (mi_row = 0; mi_row < cm->mi_rows;
218 mi_row += 8, mi_ptr += 8 * cm->mi_stride) {
219 MODE_INFO **mi = mi_ptr;
220 for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
221 mi_col += 8, mi += 8)
222 count_segs_sb(cm, xd, &tile, mi, no_pred_segcounts,
223 temporal_predictor_count, t_unpred_seg_counts, mi_row,
224 mi_col, BLOCK_64X64);
225 }
226 }
227
228 // Work out probability tree for coding segments without prediction
229 // and the cost.
230 calc_segtree_probs(no_pred_segcounts, no_pred_tree);
231 no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree);
232
233 // Key frames cannot use temporal prediction
234 if (!frame_is_intra_only(cm)) {
235 // Work out probability tree for coding those segments not
236 // predicted using the temporal method and the cost.
237 calc_segtree_probs(t_unpred_seg_counts, t_pred_tree);
238 t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree);
239
240 // Add in the cost of the signaling for each prediction context.
241 for (i = 0; i < PREDICTION_PROBS; i++) {
242 const int count0 = temporal_predictor_count[i][0];
243 const int count1 = temporal_predictor_count[i][1];
244
245 t_nopred_prob[i] = get_binary_prob(count0, count1);
246
247 // Add in the predictor signaling cost
248 t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) +
249 count1 * vp9_cost_one(t_nopred_prob[i]);
250 }
251 }
252
253 // Now choose which coding method to use.
254 if (t_pred_cost < no_pred_cost) {
255 seg->temporal_update = 1;
256 memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree));
257 memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob));
258 } else {
259 seg->temporal_update = 0;
260 memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree));
261 }
262 }
263
vp9_reset_segment_features(struct segmentation * seg)264 void vp9_reset_segment_features(struct segmentation *seg) {
265 // Set up default state for MB feature flags
266 seg->enabled = 0;
267 seg->update_map = 0;
268 seg->update_data = 0;
269 memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
270 vp9_clearall_segfeatures(seg);
271 }
272