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