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
av1_enable_segmentation(struct segmentation * seg)22 void av1_enable_segmentation(struct segmentation *seg) {
23 seg->enabled = 1;
24 seg->update_map = 1;
25 seg->update_data = 1;
26 seg->temporal_update = 0;
27 }
28
av1_disable_segmentation(struct segmentation * seg)29 void av1_disable_segmentation(struct segmentation *seg) {
30 seg->enabled = 0;
31 seg->update_map = 0;
32 seg->update_data = 0;
33 seg->temporal_update = 0;
34 }
35
av1_disable_segfeature(struct segmentation * seg,int segment_id,SEG_LVL_FEATURES feature_id)36 void av1_disable_segfeature(struct segmentation *seg, int segment_id,
37 SEG_LVL_FEATURES feature_id) {
38 seg->feature_mask[segment_id] &= ~(1 << feature_id);
39 }
40
av1_clear_segdata(struct segmentation * seg,int segment_id,SEG_LVL_FEATURES feature_id)41 void av1_clear_segdata(struct segmentation *seg, int segment_id,
42 SEG_LVL_FEATURES feature_id) {
43 seg->feature_data[segment_id][feature_id] = 0;
44 }
45
count_segs(const AV1_COMMON * cm,MACROBLOCKD * xd,const TileInfo * tile,MB_MODE_INFO ** mi,unsigned * no_pred_segcounts,unsigned (* temporal_predictor_count)[2],unsigned * t_unpred_seg_counts,int bw,int bh,int mi_row,int mi_col)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 int mi_row, int mi_col) {
52 int segment_id;
53
54 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
55
56 xd->mi = mi;
57 segment_id = xd->mi[0]->segment_id;
58
59 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
60
61 // Count the number of hits on each segment with no prediction
62 no_pred_segcounts[segment_id]++;
63
64 // Temporal prediction not allowed on key frames
65 if (cm->current_frame.frame_type != KEY_FRAME) {
66 const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
67 // Test to see if the segment id matches the predicted value.
68 const int pred_segment_id =
69 cm->last_frame_seg_map
70 ? get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col)
71 : 0;
72 const int pred_flag = pred_segment_id == segment_id;
73 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 xd->mi[0]->seg_id_predicted = pred_flag;
78 temporal_predictor_count[pred_context][pred_flag]++;
79
80 // Update the "unpredicted" segment count
81 if (!pred_flag) t_unpred_seg_counts[segment_id]++;
82 }
83 }
84
count_segs_sb(const AV1_COMMON * cm,MACROBLOCKD * xd,const TileInfo * tile,MB_MODE_INFO ** mi,unsigned * no_pred_segcounts,unsigned (* temporal_predictor_count)[2],unsigned * t_unpred_seg_counts,int mi_row,int mi_col,BLOCK_SIZE bsize)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 BLOCK_SIZE bsize) {
91 const int mis = cm->mi_stride;
92 const int bs = mi_size_wide[bsize], hbs = bs / 2;
93 PARTITION_TYPE partition;
94 const int qbs = bs / 4;
95
96 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
97
98 #define CSEGS(cs_bw, cs_bh, cs_rowoff, cs_coloff) \
99 count_segs(cm, xd, tile, mi + mis * (cs_rowoff) + (cs_coloff), \
100 no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, \
101 (cs_bw), (cs_bh), mi_row + (cs_rowoff), mi_col + (cs_coloff));
102
103 if (bsize == BLOCK_8X8)
104 partition = PARTITION_NONE;
105 else
106 partition = get_partition(cm, mi_row, mi_col, bsize);
107 switch (partition) {
108 case PARTITION_NONE: CSEGS(bs, bs, 0, 0); break;
109 case PARTITION_HORZ:
110 CSEGS(bs, hbs, 0, 0);
111 CSEGS(bs, hbs, hbs, 0);
112 break;
113 case PARTITION_VERT:
114 CSEGS(hbs, bs, 0, 0);
115 CSEGS(hbs, bs, 0, hbs);
116 break;
117 case PARTITION_HORZ_A:
118 CSEGS(hbs, hbs, 0, 0);
119 CSEGS(hbs, hbs, 0, hbs);
120 CSEGS(bs, hbs, hbs, 0);
121 break;
122 case PARTITION_HORZ_B:
123 CSEGS(bs, hbs, 0, 0);
124 CSEGS(hbs, hbs, hbs, 0);
125 CSEGS(hbs, hbs, hbs, hbs);
126 break;
127 case PARTITION_VERT_A:
128 CSEGS(hbs, hbs, 0, 0);
129 CSEGS(hbs, hbs, hbs, 0);
130 CSEGS(hbs, bs, 0, hbs);
131 break;
132 case PARTITION_VERT_B:
133 CSEGS(hbs, bs, 0, 0);
134 CSEGS(hbs, hbs, 0, hbs);
135 CSEGS(hbs, hbs, hbs, hbs);
136 break;
137 case PARTITION_HORZ_4:
138 CSEGS(bs, qbs, 0, 0);
139 CSEGS(bs, qbs, qbs, 0);
140 CSEGS(bs, qbs, 2 * qbs, 0);
141 if (mi_row + 3 * qbs < cm->mi_rows) CSEGS(bs, qbs, 3 * qbs, 0);
142 break;
143
144 case PARTITION_VERT_4:
145 CSEGS(qbs, bs, 0, 0);
146 CSEGS(qbs, bs, 0, qbs);
147 CSEGS(qbs, bs, 0, 2 * qbs);
148 if (mi_col + 3 * qbs < cm->mi_cols) CSEGS(qbs, bs, 0, 3 * qbs);
149 break;
150
151 case PARTITION_SPLIT: {
152 const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT);
153 int n;
154
155 for (n = 0; n < 4; n++) {
156 const int mi_dc = hbs * (n & 1);
157 const int mi_dr = hbs * (n >> 1);
158
159 count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts,
160 temporal_predictor_count, t_unpred_seg_counts,
161 mi_row + mi_dr, mi_col + mi_dc, subsize);
162 }
163 } break;
164 default: assert(0);
165 }
166
167 #undef CSEGS
168 }
169
av1_choose_segmap_coding_method(AV1_COMMON * cm,MACROBLOCKD * xd)170 void av1_choose_segmap_coding_method(AV1_COMMON *cm, MACROBLOCKD *xd) {
171 struct segmentation *seg = &cm->seg;
172 struct segmentation_probs *segp = &cm->fc->seg;
173 int no_pred_cost;
174 int t_pred_cost = INT_MAX;
175 int tile_col, tile_row, mi_row, mi_col;
176 unsigned temporal_predictor_count[SEG_TEMPORAL_PRED_CTXS][2] = { { 0 } };
177 unsigned no_pred_segcounts[MAX_SEGMENTS] = { 0 };
178 unsigned t_unpred_seg_counts[MAX_SEGMENTS] = { 0 };
179 (void)xd;
180
181 // First of all generate stats regarding how well the last segment map
182 // predicts this one
183 for (tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
184 TileInfo tile_info;
185 av1_tile_set_row(&tile_info, cm, tile_row);
186 for (tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
187 MB_MODE_INFO **mi_ptr;
188 av1_tile_set_col(&tile_info, cm, tile_col);
189 mi_ptr = cm->mi_grid_visible + tile_info.mi_row_start * cm->mi_stride +
190 tile_info.mi_col_start;
191 for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
192 mi_row += cm->seq_params.mib_size,
193 mi_ptr += cm->seq_params.mib_size * cm->mi_stride) {
194 MB_MODE_INFO **mi = mi_ptr;
195 for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
196 mi_col += cm->seq_params.mib_size, mi += cm->seq_params.mib_size) {
197 count_segs_sb(cm, xd, &tile_info, mi, no_pred_segcounts,
198 temporal_predictor_count, t_unpred_seg_counts, mi_row,
199 mi_col, cm->seq_params.sb_size);
200 }
201 }
202 }
203 }
204
205 int seg_id_cost[MAX_SEGMENTS];
206 av1_cost_tokens_from_cdf(seg_id_cost, segp->tree_cdf, NULL);
207 no_pred_cost = 0;
208 for (int i = 0; i < MAX_SEGMENTS; ++i)
209 no_pred_cost += no_pred_segcounts[i] * seg_id_cost[i];
210
211 // Frames without past dependency cannot use temporal prediction
212 if (cm->primary_ref_frame != PRIMARY_REF_NONE) {
213 int pred_flag_cost[SEG_TEMPORAL_PRED_CTXS][2];
214 for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i)
215 av1_cost_tokens_from_cdf(pred_flag_cost[i], segp->pred_cdf[i], NULL);
216 t_pred_cost = 0;
217 // Cost for signaling the prediction flag.
218 for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) {
219 for (int j = 0; j < 2; ++j)
220 t_pred_cost += temporal_predictor_count[i][j] * pred_flag_cost[i][j];
221 }
222 // Cost for signaling the unpredicted segment id.
223 for (int i = 0; i < MAX_SEGMENTS; ++i)
224 t_pred_cost += t_unpred_seg_counts[i] * seg_id_cost[i];
225 }
226
227 // Now choose which coding method to use.
228 if (t_pred_cost < no_pred_cost) {
229 assert(!cm->error_resilient_mode);
230 seg->temporal_update = 1;
231 } else {
232 seg->temporal_update = 0;
233 }
234 }
235
av1_reset_segment_features(AV1_COMMON * cm)236 void av1_reset_segment_features(AV1_COMMON *cm) {
237 struct segmentation *seg = &cm->seg;
238
239 // Set up default state for MB feature flags
240 seg->enabled = 0;
241 seg->update_map = 0;
242 seg->update_data = 0;
243 av1_clearall_segfeatures(seg);
244 }
245