/src/aom/av1/encoder/segmentation.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include <limits.h> |
13 | | |
14 | | #include "aom_mem/aom_mem.h" |
15 | | |
16 | | #include "av1/common/pred_common.h" |
17 | | #include "av1/common/tile_common.h" |
18 | | |
19 | | #include "av1/encoder/cost.h" |
20 | | #include "av1/encoder/segmentation.h" |
21 | | |
22 | 0 | void av1_enable_segmentation(struct segmentation *seg) { |
23 | 0 | seg->enabled = 1; |
24 | 0 | seg->update_map = 1; |
25 | 0 | seg->update_data = 1; |
26 | 0 | seg->temporal_update = 0; |
27 | 0 | } |
28 | | |
29 | 0 | void av1_disable_segmentation(struct segmentation *seg) { |
30 | 0 | seg->enabled = 0; |
31 | 0 | seg->update_map = 0; |
32 | 0 | seg->update_data = 0; |
33 | 0 | seg->temporal_update = 0; |
34 | 0 | } |
35 | | |
36 | | void av1_disable_segfeature(struct segmentation *seg, int segment_id, |
37 | 0 | SEG_LVL_FEATURES feature_id) { |
38 | 0 | seg->feature_mask[segment_id] &= ~(1 << feature_id); |
39 | 0 | } |
40 | | |
41 | | void av1_clear_segdata(struct segmentation *seg, int segment_id, |
42 | 0 | SEG_LVL_FEATURES feature_id) { |
43 | 0 | seg->feature_data[segment_id][feature_id] = 0; |
44 | 0 | } |
45 | | |
46 | | static void count_segs(const AV1_COMMON *cm, MACROBLOCKD *xd, |
47 | | const TileInfo *tile, MB_MODE_INFO **mi, |
48 | | unsigned *no_pred_segcounts, |
49 | | unsigned (*temporal_predictor_count)[2], |
50 | | unsigned *t_unpred_seg_counts, int bw, int bh, |
51 | 0 | int mi_row, int mi_col) { |
52 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
53 | 0 | if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return; |
54 | | |
55 | 0 | xd->mi = mi; |
56 | 0 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows, |
57 | 0 | mi_params->mi_cols); |
58 | | |
59 | | // Count the number of hits on each segment with no prediction |
60 | 0 | const int segment_id = xd->mi[0]->segment_id; |
61 | 0 | no_pred_segcounts[segment_id]++; |
62 | | |
63 | | // Temporal prediction not allowed on key frames |
64 | 0 | if (cm->current_frame.frame_type != KEY_FRAME) { |
65 | 0 | const BLOCK_SIZE bsize = xd->mi[0]->bsize; |
66 | | // Test to see if the segment id matches the predicted value. |
67 | 0 | const int pred_segment_id = |
68 | 0 | cm->last_frame_seg_map |
69 | 0 | ? get_segment_id(mi_params, cm->last_frame_seg_map, bsize, mi_row, |
70 | 0 | mi_col) |
71 | 0 | : 0; |
72 | 0 | const int pred_flag = pred_segment_id == segment_id; |
73 | 0 | const int pred_context = av1_get_pred_context_seg_id(xd); |
74 | | |
75 | | // Store the prediction status for this mb and update counts |
76 | | // as appropriate |
77 | 0 | xd->mi[0]->seg_id_predicted = pred_flag; |
78 | 0 | temporal_predictor_count[pred_context][pred_flag]++; |
79 | | |
80 | | // Update the "unpredicted" segment count |
81 | 0 | if (!pred_flag) t_unpred_seg_counts[segment_id]++; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | static void count_segs_sb(const AV1_COMMON *cm, MACROBLOCKD *xd, |
86 | | const TileInfo *tile, MB_MODE_INFO **mi, |
87 | | unsigned *no_pred_segcounts, |
88 | | unsigned (*temporal_predictor_count)[2], |
89 | | unsigned *t_unpred_seg_counts, int mi_row, int mi_col, |
90 | 0 | BLOCK_SIZE bsize) { |
91 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
92 | 0 | const int mis = mi_params->mi_stride; |
93 | 0 | const int bs = mi_size_wide[bsize], hbs = bs / 2; |
94 | 0 | PARTITION_TYPE partition; |
95 | 0 | const int qbs = bs / 4; |
96 | |
|
97 | 0 | if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return; |
98 | | |
99 | 0 | #define CSEGS(cs_bw, cs_bh, cs_rowoff, cs_coloff) \ |
100 | 0 | count_segs(cm, xd, tile, mi + mis * (cs_rowoff) + (cs_coloff), \ |
101 | 0 | no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, \ |
102 | 0 | (cs_bw), (cs_bh), mi_row + (cs_rowoff), mi_col + (cs_coloff)); |
103 | | |
104 | 0 | if (bsize == BLOCK_8X8) |
105 | 0 | partition = PARTITION_NONE; |
106 | 0 | else |
107 | 0 | partition = get_partition(cm, mi_row, mi_col, bsize); |
108 | 0 | switch (partition) { |
109 | 0 | case PARTITION_NONE: CSEGS(bs, bs, 0, 0); break; |
110 | 0 | case PARTITION_HORZ: |
111 | 0 | CSEGS(bs, hbs, 0, 0); |
112 | 0 | CSEGS(bs, hbs, hbs, 0); |
113 | 0 | break; |
114 | 0 | case PARTITION_VERT: |
115 | 0 | CSEGS(hbs, bs, 0, 0); |
116 | 0 | CSEGS(hbs, bs, 0, hbs); |
117 | 0 | break; |
118 | 0 | case PARTITION_HORZ_A: |
119 | 0 | CSEGS(hbs, hbs, 0, 0); |
120 | 0 | CSEGS(hbs, hbs, 0, hbs); |
121 | 0 | CSEGS(bs, hbs, hbs, 0); |
122 | 0 | break; |
123 | 0 | case PARTITION_HORZ_B: |
124 | 0 | CSEGS(bs, hbs, 0, 0); |
125 | 0 | CSEGS(hbs, hbs, hbs, 0); |
126 | 0 | CSEGS(hbs, hbs, hbs, hbs); |
127 | 0 | break; |
128 | 0 | case PARTITION_VERT_A: |
129 | 0 | CSEGS(hbs, hbs, 0, 0); |
130 | 0 | CSEGS(hbs, hbs, hbs, 0); |
131 | 0 | CSEGS(hbs, bs, 0, hbs); |
132 | 0 | break; |
133 | 0 | case PARTITION_VERT_B: |
134 | 0 | CSEGS(hbs, bs, 0, 0); |
135 | 0 | CSEGS(hbs, hbs, 0, hbs); |
136 | 0 | CSEGS(hbs, hbs, hbs, hbs); |
137 | 0 | break; |
138 | 0 | case PARTITION_HORZ_4: |
139 | 0 | CSEGS(bs, qbs, 0, 0); |
140 | 0 | CSEGS(bs, qbs, qbs, 0); |
141 | 0 | CSEGS(bs, qbs, 2 * qbs, 0); |
142 | 0 | if (mi_row + 3 * qbs < mi_params->mi_rows) CSEGS(bs, qbs, 3 * qbs, 0); |
143 | 0 | break; |
144 | | |
145 | 0 | case PARTITION_VERT_4: |
146 | 0 | CSEGS(qbs, bs, 0, 0); |
147 | 0 | CSEGS(qbs, bs, 0, qbs); |
148 | 0 | CSEGS(qbs, bs, 0, 2 * qbs); |
149 | 0 | if (mi_col + 3 * qbs < mi_params->mi_cols) CSEGS(qbs, bs, 0, 3 * qbs); |
150 | 0 | break; |
151 | | |
152 | 0 | case PARTITION_SPLIT: { |
153 | 0 | const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT); |
154 | 0 | int n; |
155 | 0 | assert(subsize < BLOCK_SIZES_ALL); |
156 | |
|
157 | 0 | for (n = 0; n < 4; n++) { |
158 | 0 | const int mi_dc = hbs * (n & 1); |
159 | 0 | const int mi_dr = hbs * (n >> 1); |
160 | |
|
161 | 0 | count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts, |
162 | 0 | temporal_predictor_count, t_unpred_seg_counts, |
163 | 0 | mi_row + mi_dr, mi_col + mi_dc, subsize); |
164 | 0 | } |
165 | 0 | } break; |
166 | 0 | default: assert(0); |
167 | 0 | } |
168 | |
|
169 | 0 | #undef CSEGS |
170 | 0 | } |
171 | | |
172 | 0 | void av1_choose_segmap_coding_method(AV1_COMMON *cm, MACROBLOCKD *xd) { |
173 | 0 | struct segmentation *seg = &cm->seg; |
174 | 0 | struct segmentation_probs *segp = &cm->fc->seg; |
175 | 0 | int no_pred_cost; |
176 | 0 | int t_pred_cost = INT_MAX; |
177 | 0 | int tile_col, tile_row, mi_row, mi_col; |
178 | |
|
179 | 0 | if (!seg->update_map) return; |
180 | 0 | if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) { |
181 | 0 | seg->temporal_update = 0; |
182 | 0 | assert(seg->update_data == 1); |
183 | 0 | return; |
184 | 0 | } |
185 | | |
186 | 0 | unsigned temporal_predictor_count[SEG_TEMPORAL_PRED_CTXS][2] = { { 0 } }; |
187 | 0 | unsigned no_pred_segcounts[MAX_SEGMENTS] = { 0 }; |
188 | 0 | unsigned t_unpred_seg_counts[MAX_SEGMENTS] = { 0 }; |
189 | 0 | (void)xd; |
190 | 0 | int scale_up = cm->prev_frame && (cm->width > cm->prev_frame->width || |
191 | 0 | cm->height > cm->prev_frame->height); |
192 | | // First of all generate stats regarding how well the last segment map |
193 | | // predicts this one |
194 | 0 | if (!scale_up) { |
195 | 0 | for (tile_row = 0; tile_row < cm->tiles.rows; tile_row++) { |
196 | 0 | TileInfo tile_info; |
197 | 0 | av1_tile_set_row(&tile_info, cm, tile_row); |
198 | 0 | for (tile_col = 0; tile_col < cm->tiles.cols; tile_col++) { |
199 | 0 | MB_MODE_INFO **mi_ptr; |
200 | 0 | av1_tile_set_col(&tile_info, cm, tile_col); |
201 | 0 | mi_ptr = cm->mi_params.mi_grid_base + |
202 | 0 | tile_info.mi_row_start * cm->mi_params.mi_stride + |
203 | 0 | tile_info.mi_col_start; |
204 | 0 | for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end; |
205 | 0 | mi_row += cm->seq_params->mib_size, |
206 | 0 | mi_ptr += cm->seq_params->mib_size * cm->mi_params.mi_stride) { |
207 | 0 | MB_MODE_INFO **mi = mi_ptr; |
208 | 0 | for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end; |
209 | 0 | mi_col += cm->seq_params->mib_size, |
210 | 0 | mi += cm->seq_params->mib_size) { |
211 | 0 | count_segs_sb(cm, xd, &tile_info, mi, no_pred_segcounts, |
212 | 0 | temporal_predictor_count, t_unpred_seg_counts, mi_row, |
213 | 0 | mi_col, cm->seq_params->sb_size); |
214 | 0 | } |
215 | 0 | } |
216 | 0 | } |
217 | 0 | } |
218 | 0 | } |
219 | |
|
220 | 0 | int seg_id_cost[MAX_SEGMENTS]; |
221 | 0 | av1_cost_tokens_from_cdf(seg_id_cost, segp->tree_cdf, NULL); |
222 | 0 | no_pred_cost = 0; |
223 | 0 | for (int i = 0; i < MAX_SEGMENTS; ++i) |
224 | 0 | no_pred_cost += no_pred_segcounts[i] * seg_id_cost[i]; |
225 | | |
226 | | // Frames without past dependency cannot use temporal prediction |
227 | 0 | if (cm->features.primary_ref_frame != PRIMARY_REF_NONE) { |
228 | 0 | int pred_flag_cost[SEG_TEMPORAL_PRED_CTXS][2]; |
229 | 0 | for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) |
230 | 0 | av1_cost_tokens_from_cdf(pred_flag_cost[i], segp->pred_cdf[i], NULL); |
231 | 0 | t_pred_cost = 0; |
232 | | // Cost for signaling the prediction flag. |
233 | 0 | for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) { |
234 | 0 | for (int j = 0; j < 2; ++j) |
235 | 0 | t_pred_cost += temporal_predictor_count[i][j] * pred_flag_cost[i][j]; |
236 | 0 | } |
237 | | // Cost for signaling the unpredicted segment id. |
238 | 0 | for (int i = 0; i < MAX_SEGMENTS; ++i) |
239 | 0 | t_pred_cost += t_unpred_seg_counts[i] * seg_id_cost[i]; |
240 | 0 | } |
241 | | |
242 | | // Now choose which coding method to use. |
243 | 0 | if (t_pred_cost < no_pred_cost) { |
244 | 0 | assert(!cm->features.error_resilient_mode); |
245 | 0 | seg->temporal_update = 1; |
246 | 0 | } else { |
247 | 0 | seg->temporal_update = 0; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | 0 | void av1_reset_segment_features(AV1_COMMON *cm) { |
252 | 0 | struct segmentation *seg = &cm->seg; |
253 | | |
254 | | // Set up default state for MB feature flags |
255 | 0 | seg->enabled = 0; |
256 | 0 | seg->update_map = 0; |
257 | 0 | seg->update_data = 0; |
258 | 0 | av1_clearall_segfeatures(seg); |
259 | 0 | } |