Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1229)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_segmentation.c

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/encoder/vp9_segmentation.h"
15 #include "vp9/common/vp9_pred_common.h"
16
17 void vp9_update_gf_useage_maps(VP9_COMP *cpi, VP9_COMMON *cm, MACROBLOCK *x) {
18 int mb_row, mb_col;
19
20 MODE_INFO *this_mb_mode_info = cm->mi;
21
22 x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
23
24 if ((cm->frame_type == KEY_FRAME) || (cm->refresh_golden_frame)) {
25 // Reset Gf useage monitors
26 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
27 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
28 } else {
29 // for each macroblock row in image
30 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
31 // for each macroblock col in image
32 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
33
34 // If using golden then set GF active flag if not already set.
35 // If using last frame 0,0 mode then leave flag as it is
36 // else if using non 0,0 motion or intra modes then clear
37 // flag if it is currently set
38 if ((this_mb_mode_info->mbmi.ref_frame == GOLDEN_FRAME) ||
39 (this_mb_mode_info->mbmi.ref_frame == ALTREF_FRAME)) {
40 if (*(x->gf_active_ptr) == 0) {
41 *(x->gf_active_ptr) = 1;
42 cpi->gf_active_count++;
43 }
44 } else if ((this_mb_mode_info->mbmi.mode != ZEROMV) &&
45 *(x->gf_active_ptr)) {
46 *(x->gf_active_ptr) = 0;
47 cpi->gf_active_count--;
48 }
49
50 x->gf_active_ptr++; // Step onto next entry
51 this_mb_mode_info++; // skip to next mb
52
53 }
54
55 // this is to account for the border
56 this_mb_mode_info++;
57 }
58 }
59 }
60
61 void vp9_enable_segmentation(VP9_PTR ptr) {
62 VP9_COMP *cpi = (VP9_COMP *)(ptr);
63
64 // Set the appropriate feature bit
65 cpi->mb.e_mbd.segmentation_enabled = 1;
66 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
67 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
68 }
69
70 void vp9_disable_segmentation(VP9_PTR ptr) {
71 VP9_COMP *cpi = (VP9_COMP *)(ptr);
72
73 // Clear the appropriate feature bit
74 cpi->mb.e_mbd.segmentation_enabled = 0;
75 }
76
77 void vp9_set_segmentation_map(VP9_PTR ptr,
78 unsigned char *segmentation_map) {
79 VP9_COMP *cpi = (VP9_COMP *)(ptr);
80
81 // Copy in the new segmentation map
82 vpx_memcpy(cpi->segmentation_map, segmentation_map,
83 (cpi->common.mb_rows * cpi->common.mb_cols));
84
85 // Signal that the map should be updated.
86 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
87 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
88 }
89
90 void vp9_set_segment_data(VP9_PTR ptr,
91 signed char *feature_data,
92 unsigned char abs_delta) {
93 VP9_COMP *cpi = (VP9_COMP *)(ptr);
94
95 cpi->mb.e_mbd.mb_segment_abs_delta = abs_delta;
96
97 vpx_memcpy(cpi->mb.e_mbd.segment_feature_data, feature_data,
98 sizeof(cpi->mb.e_mbd.segment_feature_data));
99
100 // TBD ?? Set the feature mask
101 // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0,
102 // sizeof(cpi->mb.e_mbd.segment_feature_mask));
103 }
104
105 // Based on set of segment counts calculate a probability tree
106 static void calc_segtree_probs(MACROBLOCKD *xd,
107 int *segcounts,
108 vp9_prob *segment_tree_probs) {
109 int count1, count2;
110 int tot_count;
111 int i;
112
113 // Blank the strtucture to start with
114 vpx_memset(segment_tree_probs, 0,
115 MB_FEATURE_TREE_PROBS * sizeof(*segment_tree_probs));
116
117 // Total count for all segments
118 count1 = segcounts[0] + segcounts[1];
119 count2 = segcounts[2] + segcounts[3];
120 tot_count = count1 + count2;
121
122 // Work out probabilities of each segment
123 if (tot_count)
124 segment_tree_probs[0] = (count1 * 255) / tot_count;
125 if (count1 > 0)
126 segment_tree_probs[1] = (segcounts[0] * 255) / count1;
127 if (count2 > 0)
128 segment_tree_probs[2] = (segcounts[2] * 255) / count2;
129
130 // Clamp probabilities to minimum allowed value
131 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) {
132 if (segment_tree_probs[i] == 0)
133 segment_tree_probs[i] = 1;
134 }
135 }
136
137 // Based on set of segment counts and probabilities calculate a cost estimate
138 static int cost_segmap(MACROBLOCKD *xd,
139 int *segcounts,
140 vp9_prob *probs) {
141 int cost;
142 int count1, count2;
143
144 // Cost the top node of the tree
145 count1 = segcounts[0] + segcounts[1];
146 count2 = segcounts[2] + segcounts[3];
147 cost = count1 * vp9_cost_zero(probs[0]) +
148 count2 * vp9_cost_one(probs[0]);
149
150 // Now add the cost of each individual segment branch
151 if (count1 > 0)
152 cost += segcounts[0] * vp9_cost_zero(probs[1]) +
153 segcounts[1] * vp9_cost_one(probs[1]);
154
155 if (count2 > 0)
156 cost += segcounts[2] * vp9_cost_zero(probs[2]) +
157 segcounts[3] * vp9_cost_one(probs[2]);
158
159 return cost;
160
161 }
162
163 void vp9_choose_segmap_coding_method(VP9_COMP *cpi) {
164 VP9_COMMON *const cm = &cpi->common;
165 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
166
167 int i;
168 int tot_count;
169 int no_pred_cost;
170 int t_pred_cost = INT_MAX;
171 int pred_context;
172
173 int mb_row, mb_col;
174 int segmap_index = 0;
175 unsigned char segment_id;
176
177 int temporal_predictor_count[PREDICTION_PROBS][2];
178 int no_pred_segcounts[MAX_MB_SEGMENTS];
179 int t_unpred_seg_counts[MAX_MB_SEGMENTS];
180
181 vp9_prob no_pred_tree[MB_FEATURE_TREE_PROBS];
182 vp9_prob t_pred_tree[MB_FEATURE_TREE_PROBS];
183 vp9_prob t_nopred_prob[PREDICTION_PROBS];
184
185 #if CONFIG_SUPERBLOCKS
186 const int mis = cm->mode_info_stride;
187 #endif
188
189 // Set default state for the segment tree probabilities and the
190 // temporal coding probabilities
191 vpx_memset(xd->mb_segment_tree_probs, 255,
192 sizeof(xd->mb_segment_tree_probs));
193 vpx_memset(cm->segment_pred_probs, 255,
194 sizeof(cm->segment_pred_probs));
195
196 vpx_memset(no_pred_segcounts, 0, sizeof(no_pred_segcounts));
197 vpx_memset(t_unpred_seg_counts, 0, sizeof(t_unpred_seg_counts));
198 vpx_memset(temporal_predictor_count, 0, sizeof(temporal_predictor_count));
199
200 // First of all generate stats regarding how well the last segment map
201 // predicts this one
202
203 // Initialize macroblock decoder mode info context for the first mb
204 // in the frame
205 xd->mode_info_context = cm->mi;
206
207 for (mb_row = 0; mb_row < cm->mb_rows; mb_row += 2) {
208 for (mb_col = 0; mb_col < cm->mb_cols; mb_col += 2) {
209 for (i = 0; i < 4; i++) {
210 static const int dx[4] = { +1, -1, +1, +1 };
211 static const int dy[4] = { 0, +1, 0, -1 };
212 int x_idx = i & 1, y_idx = i >> 1;
213
214 if (mb_col + x_idx >= cm->mb_cols ||
215 mb_row + y_idx >= cm->mb_rows) {
216 goto end;
217 }
218
219 xd->mb_to_top_edge = -((mb_row * 16) << 3);
220 xd->mb_to_left_edge = -((mb_col * 16) << 3);
221
222 segmap_index = (mb_row + y_idx) * cm->mb_cols + mb_col + x_idx;
223 segment_id = xd->mode_info_context->mbmi.segment_id;
224 #if CONFIG_SUPERBLOCKS
225 if (xd->mode_info_context->mbmi.encoded_as_sb) {
226 if (mb_col + 1 < cm->mb_cols)
227 segment_id = segment_id &&
228 xd->mode_info_context[1].mbmi.segment_id;
229 if (mb_row + 1 < cm->mb_rows) {
230 segment_id = segment_id &&
231 xd->mode_info_context[mis].mbmi.segment_id;
232 if (mb_col + 1 < cm->mb_cols)
233 segment_id = segment_id &&
234 xd->mode_info_context[mis + 1].mbmi.segment_id;
235 }
236 xd->mb_to_bottom_edge = ((cm->mb_rows - 2 - mb_row) * 16) << 3;
237 xd->mb_to_right_edge = ((cm->mb_cols - 2 - mb_col) * 16) << 3;
238 } else {
239 #endif
240 xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
241 xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
242 #if CONFIG_SUPERBLOCKS
243 }
244 #endif
245
246 // Count the number of hits on each segment with no prediction
247 no_pred_segcounts[segment_id]++;
248
249 // Temporal prediction not allowed on key frames
250 if (cm->frame_type != KEY_FRAME) {
251 // Test to see if the segment id matches the predicted value.
252 int seg_predicted =
253 (segment_id == vp9_get_pred_mb_segid(cm, xd, segmap_index));
254
255 // Get the segment id prediction context
256 pred_context =
257 vp9_get_pred_context(cm, xd, PRED_SEG_ID);
258
259 // Store the prediction status for this mb and update counts
260 // as appropriate
261 vp9_set_pred_flag(xd, PRED_SEG_ID, seg_predicted);
262 temporal_predictor_count[pred_context][seg_predicted]++;
263
264 if (!seg_predicted)
265 // Update the "unpredicted" segment count
266 t_unpred_seg_counts[segment_id]++;
267 }
268
269 #if CONFIG_SUPERBLOCKS
270 if (xd->mode_info_context->mbmi.encoded_as_sb) {
271 assert(!i);
272 xd->mode_info_context += 2;
273 break;
274 }
275 #endif
276 end:
277 xd->mode_info_context += dx[i] + dy[i] * cm->mode_info_stride;
278 }
279 }
280
281 // this is to account for the border in mode_info_context
282 xd->mode_info_context -= mb_col;
283 xd->mode_info_context += cm->mode_info_stride * 2;
284 }
285
286 // Work out probability tree for coding segments without prediction
287 // and the cost.
288 calc_segtree_probs(xd, no_pred_segcounts, no_pred_tree);
289 no_pred_cost = cost_segmap(xd, no_pred_segcounts, no_pred_tree);
290
291 // Key frames cannot use temporal prediction
292 if (cm->frame_type != KEY_FRAME) {
293 // Work out probability tree for coding those segments not
294 // predicted using the temporal method and the cost.
295 calc_segtree_probs(xd, t_unpred_seg_counts, t_pred_tree);
296 t_pred_cost = cost_segmap(xd, t_unpred_seg_counts, t_pred_tree);
297
298 // Add in the cost of the signalling for each prediction context
299 for (i = 0; i < PREDICTION_PROBS; i++) {
300 tot_count = temporal_predictor_count[i][0] +
301 temporal_predictor_count[i][1];
302
303 // Work out the context probabilities for the segment
304 // prediction flag
305 if (tot_count) {
306 t_nopred_prob[i] = (temporal_predictor_count[i][0] * 255) /
307 tot_count;
308
309 // Clamp to minimum allowed value
310 if (t_nopred_prob[i] < 1)
311 t_nopred_prob[i] = 1;
312 } else
313 t_nopred_prob[i] = 1;
314
315 // Add in the predictor signaling cost
316 t_pred_cost += (temporal_predictor_count[i][0] *
317 vp9_cost_zero(t_nopred_prob[i])) +
318 (temporal_predictor_count[i][1] *
319 vp9_cost_one(t_nopred_prob[i]));
320 }
321 }
322
323 // Now choose which coding method to use.
324 if (t_pred_cost < no_pred_cost) {
325 cm->temporal_update = 1;
326 vpx_memcpy(xd->mb_segment_tree_probs,
327 t_pred_tree, sizeof(t_pred_tree));
328 vpx_memcpy(&cm->segment_pred_probs,
329 t_nopred_prob, sizeof(t_nopred_prob));
330 } else {
331 cm->temporal_update = 0;
332 vpx_memcpy(xd->mb_segment_tree_probs,
333 no_pred_tree, sizeof(no_pred_tree));
334 }
335 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698