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