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