OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // TBD ?? Set the feature mask | 100 // TBD ?? Set the feature mask |
101 // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0, | 101 // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0, |
102 // sizeof(cpi->mb.e_mbd.segment_feature_mask)); | 102 // sizeof(cpi->mb.e_mbd.segment_feature_mask)); |
103 } | 103 } |
104 | 104 |
105 // Based on set of segment counts calculate a probability tree | 105 // Based on set of segment counts calculate a probability tree |
106 static void calc_segtree_probs(MACROBLOCKD *xd, | 106 static void calc_segtree_probs(MACROBLOCKD *xd, |
107 int *segcounts, | 107 int *segcounts, |
108 vp9_prob *segment_tree_probs) { | 108 vp9_prob *segment_tree_probs) { |
109 int count1, count2; | 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 | 110 |
117 // Total count for all segments | 111 // Total count for all segments |
118 count1 = segcounts[0] + segcounts[1]; | 112 count1 = segcounts[0] + segcounts[1]; |
119 count2 = segcounts[2] + segcounts[3]; | 113 count2 = segcounts[2] + segcounts[3]; |
120 tot_count = count1 + count2; | |
121 | 114 |
122 // Work out probabilities of each segment | 115 // Work out probabilities of each segment |
123 if (tot_count) | 116 segment_tree_probs[0] = get_binary_prob(count1, count2); |
124 segment_tree_probs[0] = (count1 * 255) / tot_count; | 117 segment_tree_probs[1] = get_prob(segcounts[0], count1); |
125 if (count1 > 0) | 118 segment_tree_probs[2] = get_prob(segcounts[2], count2); |
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 } | 119 } |
136 | 120 |
137 // Based on set of segment counts and probabilities calculate a cost estimate | 121 // Based on set of segment counts and probabilities calculate a cost estimate |
138 static int cost_segmap(MACROBLOCKD *xd, | 122 static int cost_segmap(MACROBLOCKD *xd, |
139 int *segcounts, | 123 int *segcounts, |
140 vp9_prob *probs) { | 124 vp9_prob *probs) { |
141 int cost; | 125 int cost; |
142 int count1, count2; | 126 int count1, count2; |
143 | 127 |
144 // Cost the top node of the tree | 128 // Cost the top node of the tree |
145 count1 = segcounts[0] + segcounts[1]; | 129 count1 = segcounts[0] + segcounts[1]; |
146 count2 = segcounts[2] + segcounts[3]; | 130 count2 = segcounts[2] + segcounts[3]; |
147 cost = count1 * vp9_cost_zero(probs[0]) + | 131 cost = count1 * vp9_cost_zero(probs[0]) + |
148 count2 * vp9_cost_one(probs[0]); | 132 count2 * vp9_cost_one(probs[0]); |
149 | 133 |
150 // Now add the cost of each individual segment branch | 134 // Now add the cost of each individual segment branch |
151 if (count1 > 0) | 135 if (count1 > 0) |
152 cost += segcounts[0] * vp9_cost_zero(probs[1]) + | 136 cost += segcounts[0] * vp9_cost_zero(probs[1]) + |
153 segcounts[1] * vp9_cost_one(probs[1]); | 137 segcounts[1] * vp9_cost_one(probs[1]); |
154 | 138 |
155 if (count2 > 0) | 139 if (count2 > 0) |
156 cost += segcounts[2] * vp9_cost_zero(probs[2]) + | 140 cost += segcounts[2] * vp9_cost_zero(probs[2]) + |
157 segcounts[3] * vp9_cost_one(probs[2]); | 141 segcounts[3] * vp9_cost_one(probs[2]); |
158 | 142 |
159 return cost; | 143 return cost; |
| 144 } |
160 | 145 |
| 146 static void count_segs(VP9_COMP *cpi, |
| 147 MODE_INFO *mi, |
| 148 int *no_pred_segcounts, |
| 149 int (*temporal_predictor_count)[2], |
| 150 int *t_unpred_seg_counts, |
| 151 int mb_size, int mb_row, int mb_col) { |
| 152 VP9_COMMON *const cm = &cpi->common; |
| 153 MACROBLOCKD *const xd = &cpi->mb.e_mbd; |
| 154 const int segmap_index = mb_row * cm->mb_cols + mb_col; |
| 155 const int segment_id = mi->mbmi.segment_id; |
| 156 |
| 157 xd->mode_info_context = mi; |
| 158 xd->mb_to_top_edge = -((mb_row * 16) << 3); |
| 159 xd->mb_to_left_edge = -((mb_col * 16) << 3); |
| 160 xd->mb_to_bottom_edge = ((cm->mb_rows - mb_size - mb_row) * 16) << 3; |
| 161 xd->mb_to_right_edge = ((cm->mb_cols - mb_size - mb_col) * 16) << 3; |
| 162 |
| 163 // Count the number of hits on each segment with no prediction |
| 164 no_pred_segcounts[segment_id]++; |
| 165 |
| 166 // Temporal prediction not allowed on key frames |
| 167 if (cm->frame_type != KEY_FRAME) { |
| 168 // Test to see if the segment id matches the predicted value. |
| 169 const int seg_predicted = |
| 170 (segment_id == vp9_get_pred_mb_segid(cm, xd, segmap_index)); |
| 171 |
| 172 // Get the segment id prediction context |
| 173 const int pred_context = vp9_get_pred_context(cm, xd, PRED_SEG_ID); |
| 174 |
| 175 // Store the prediction status for this mb and update counts |
| 176 // as appropriate |
| 177 vp9_set_pred_flag(xd, PRED_SEG_ID, seg_predicted); |
| 178 temporal_predictor_count[pred_context][seg_predicted]++; |
| 179 |
| 180 if (!seg_predicted) |
| 181 // Update the "unpredicted" segment count |
| 182 t_unpred_seg_counts[segment_id]++; |
| 183 } |
161 } | 184 } |
162 | 185 |
163 void vp9_choose_segmap_coding_method(VP9_COMP *cpi) { | 186 void vp9_choose_segmap_coding_method(VP9_COMP *cpi) { |
164 VP9_COMMON *const cm = &cpi->common; | 187 VP9_COMMON *const cm = &cpi->common; |
165 MACROBLOCKD *const xd = &cpi->mb.e_mbd; | 188 MACROBLOCKD *const xd = &cpi->mb.e_mbd; |
166 | 189 |
167 int i; | |
168 int tot_count; | |
169 int no_pred_cost; | 190 int no_pred_cost; |
170 int t_pred_cost = INT_MAX; | 191 int t_pred_cost = INT_MAX; |
171 int pred_context; | |
172 | 192 |
| 193 int i; |
173 int mb_row, mb_col; | 194 int mb_row, mb_col; |
174 int segmap_index = 0; | |
175 unsigned char segment_id; | |
176 | 195 |
177 int temporal_predictor_count[PREDICTION_PROBS][2]; | 196 int temporal_predictor_count[PREDICTION_PROBS][2]; |
178 int no_pred_segcounts[MAX_MB_SEGMENTS]; | 197 int no_pred_segcounts[MAX_MB_SEGMENTS]; |
179 int t_unpred_seg_counts[MAX_MB_SEGMENTS]; | 198 int t_unpred_seg_counts[MAX_MB_SEGMENTS]; |
180 | 199 |
181 vp9_prob no_pred_tree[MB_FEATURE_TREE_PROBS]; | 200 vp9_prob no_pred_tree[MB_FEATURE_TREE_PROBS]; |
182 vp9_prob t_pred_tree[MB_FEATURE_TREE_PROBS]; | 201 vp9_prob t_pred_tree[MB_FEATURE_TREE_PROBS]; |
183 vp9_prob t_nopred_prob[PREDICTION_PROBS]; | 202 vp9_prob t_nopred_prob[PREDICTION_PROBS]; |
184 | 203 |
185 #if CONFIG_SUPERBLOCKS | |
186 const int mis = cm->mode_info_stride; | 204 const int mis = cm->mode_info_stride; |
187 #endif | 205 MODE_INFO *mi_ptr = cm->mi, *mi; |
188 | 206 |
189 // Set default state for the segment tree probabilities and the | 207 // Set default state for the segment tree probabilities and the |
190 // temporal coding probabilities | 208 // temporal coding probabilities |
191 vpx_memset(xd->mb_segment_tree_probs, 255, | 209 vpx_memset(xd->mb_segment_tree_probs, 255, |
192 sizeof(xd->mb_segment_tree_probs)); | 210 sizeof(xd->mb_segment_tree_probs)); |
193 vpx_memset(cm->segment_pred_probs, 255, | 211 vpx_memset(cm->segment_pred_probs, 255, |
194 sizeof(cm->segment_pred_probs)); | 212 sizeof(cm->segment_pred_probs)); |
195 | 213 |
196 vpx_memset(no_pred_segcounts, 0, sizeof(no_pred_segcounts)); | 214 vpx_memset(no_pred_segcounts, 0, sizeof(no_pred_segcounts)); |
197 vpx_memset(t_unpred_seg_counts, 0, sizeof(t_unpred_seg_counts)); | 215 vpx_memset(t_unpred_seg_counts, 0, sizeof(t_unpred_seg_counts)); |
198 vpx_memset(temporal_predictor_count, 0, sizeof(temporal_predictor_count)); | 216 vpx_memset(temporal_predictor_count, 0, sizeof(temporal_predictor_count)); |
199 | 217 |
200 // First of all generate stats regarding how well the last segment map | 218 // First of all generate stats regarding how well the last segment map |
201 // predicts this one | 219 // predicts this one |
202 | 220 |
203 // Initialize macroblock decoder mode info context for the first mb | 221 for (mb_row = 0; mb_row < cm->mb_rows; mb_row += 4, mi_ptr += 4 * mis) { |
204 // in the frame | 222 mi = mi_ptr; |
205 xd->mode_info_context = cm->mi; | 223 for (mb_col = 0; mb_col < cm->mb_cols; mb_col += 4, mi += 4) { |
| 224 if (mi->mbmi.sb_type == BLOCK_SIZE_SB64X64) { |
| 225 count_segs(cpi, mi, no_pred_segcounts, temporal_predictor_count, |
| 226 t_unpred_seg_counts, 4, mb_row, mb_col); |
| 227 } else { |
| 228 for (i = 0; i < 4; i++) { |
| 229 int x_idx = (i & 1) << 1, y_idx = i & 2; |
| 230 MODE_INFO *sb_mi = mi + y_idx * mis + x_idx; |
206 | 231 |
207 for (mb_row = 0; mb_row < cm->mb_rows; mb_row += 2) { | 232 if (mb_col + x_idx >= cm->mb_cols || |
208 for (mb_col = 0; mb_col < cm->mb_cols; mb_col += 2) { | 233 mb_row + y_idx >= cm->mb_rows) { |
209 for (i = 0; i < 4; i++) { | 234 continue; |
210 static const int dx[4] = { +1, -1, +1, +1 }; | 235 } |
211 static const int dy[4] = { 0, +1, 0, -1 }; | |
212 int x_idx = i & 1, y_idx = i >> 1; | |
213 | 236 |
214 if (mb_col + x_idx >= cm->mb_cols || | 237 if (sb_mi->mbmi.sb_type) { |
215 mb_row + y_idx >= cm->mb_rows) { | 238 assert(sb_mi->mbmi.sb_type == BLOCK_SIZE_SB32X32); |
216 goto end; | 239 count_segs(cpi, sb_mi, no_pred_segcounts, temporal_predictor_count, |
| 240 t_unpred_seg_counts, 2, mb_row + y_idx, mb_col + x_idx); |
| 241 } else { |
| 242 int j; |
| 243 |
| 244 for (j = 0; j < 4; j++) { |
| 245 const int x_idx_mb = x_idx + (j & 1), y_idx_mb = y_idx + (j >> 1); |
| 246 MODE_INFO *mb_mi = mi + x_idx_mb + y_idx_mb * mis; |
| 247 |
| 248 if (mb_col + x_idx_mb >= cm->mb_cols || |
| 249 mb_row + y_idx_mb >= cm->mb_rows) { |
| 250 continue; |
| 251 } |
| 252 |
| 253 assert(mb_mi->mbmi.sb_type == BLOCK_SIZE_MB16X16); |
| 254 count_segs(cpi, mb_mi, no_pred_segcounts, |
| 255 temporal_predictor_count, t_unpred_seg_counts, |
| 256 1, mb_row + y_idx_mb, mb_col + x_idx_mb); |
| 257 } |
| 258 } |
217 } | 259 } |
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 } | 260 } |
279 } | 261 } |
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 } | 262 } |
285 | 263 |
286 // Work out probability tree for coding segments without prediction | 264 // Work out probability tree for coding segments without prediction |
287 // and the cost. | 265 // and the cost. |
288 calc_segtree_probs(xd, no_pred_segcounts, no_pred_tree); | 266 calc_segtree_probs(xd, no_pred_segcounts, no_pred_tree); |
289 no_pred_cost = cost_segmap(xd, no_pred_segcounts, no_pred_tree); | 267 no_pred_cost = cost_segmap(xd, no_pred_segcounts, no_pred_tree); |
290 | 268 |
291 // Key frames cannot use temporal prediction | 269 // Key frames cannot use temporal prediction |
292 if (cm->frame_type != KEY_FRAME) { | 270 if (cm->frame_type != KEY_FRAME) { |
293 // Work out probability tree for coding those segments not | 271 // Work out probability tree for coding those segments not |
294 // predicted using the temporal method and the cost. | 272 // predicted using the temporal method and the cost. |
295 calc_segtree_probs(xd, t_unpred_seg_counts, t_pred_tree); | 273 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); | 274 t_pred_cost = cost_segmap(xd, t_unpred_seg_counts, t_pred_tree); |
297 | 275 |
298 // Add in the cost of the signalling for each prediction context | 276 // Add in the cost of the signalling for each prediction context |
299 for (i = 0; i < PREDICTION_PROBS; i++) { | 277 for (i = 0; i < PREDICTION_PROBS; i++) { |
300 tot_count = temporal_predictor_count[i][0] + | 278 t_nopred_prob[i] = get_binary_prob(temporal_predictor_count[i][0], |
301 temporal_predictor_count[i][1]; | 279 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 | 280 |
315 // Add in the predictor signaling cost | 281 // Add in the predictor signaling cost |
316 t_pred_cost += (temporal_predictor_count[i][0] * | 282 t_pred_cost += (temporal_predictor_count[i][0] * |
317 vp9_cost_zero(t_nopred_prob[i])) + | 283 vp9_cost_zero(t_nopred_prob[i])) + |
318 (temporal_predictor_count[i][1] * | 284 (temporal_predictor_count[i][1] * |
319 vp9_cost_one(t_nopred_prob[i])); | 285 vp9_cost_one(t_nopred_prob[i])); |
320 } | 286 } |
321 } | 287 } |
322 | 288 |
323 // Now choose which coding method to use. | 289 // Now choose which coding method to use. |
324 if (t_pred_cost < no_pred_cost) { | 290 if (t_pred_cost < no_pred_cost) { |
325 cm->temporal_update = 1; | 291 cm->temporal_update = 1; |
326 vpx_memcpy(xd->mb_segment_tree_probs, | 292 vpx_memcpy(xd->mb_segment_tree_probs, |
327 t_pred_tree, sizeof(t_pred_tree)); | 293 t_pred_tree, sizeof(t_pred_tree)); |
328 vpx_memcpy(&cm->segment_pred_probs, | 294 vpx_memcpy(&cm->segment_pred_probs, |
329 t_nopred_prob, sizeof(t_nopred_prob)); | 295 t_nopred_prob, sizeof(t_nopred_prob)); |
330 } else { | 296 } else { |
331 cm->temporal_update = 0; | 297 cm->temporal_update = 0; |
332 vpx_memcpy(xd->mb_segment_tree_probs, | 298 vpx_memcpy(xd->mb_segment_tree_probs, |
333 no_pred_tree, sizeof(no_pred_tree)); | 299 no_pred_tree, sizeof(no_pred_tree)); |
334 } | 300 } |
335 } | 301 } |
OLD | NEW |