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