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