vp9_segmentation.c 10.2 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


Dmitry Kovalev's avatar
Dmitry Kovalev committed
12
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
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"
John Koleszar's avatar
John Koleszar committed
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
23
24
  seg->enabled = 1;
  seg->update_map = 1;
  seg->update_data = 1;
25
26
}

27
void vp9_disable_segmentation(VP9_PTR ptr) {
28
  VP9_COMP *cpi = (VP9_COMP *)ptr;
29
30
  struct segmentation *const seg =  &cpi->common.seg;
  seg->enabled = 0;
31
32
}

33
void vp9_set_segmentation_map(VP9_PTR ptr,
John Koleszar's avatar
John Koleszar committed
34
                              unsigned char *segmentation_map) {
35
36
  VP9_COMP *cpi = (VP9_COMP *)ptr;
  struct segmentation *const seg = &cpi->common.seg;
37

John Koleszar's avatar
John Koleszar committed
38
39
  // Copy in the new segmentation map
  vpx_memcpy(cpi->segmentation_map, segmentation_map,
40
             (cpi->common.mi_rows * cpi->common.mi_cols));
41

John Koleszar's avatar
John Koleszar committed
42
  // Signal that the map should be updated.
43
44
  seg->update_map = 1;
  seg->update_data = 1;
45
46
}

47
void vp9_set_segment_data(VP9_PTR ptr,
48
                          signed char *feature_data,
John Koleszar's avatar
John Koleszar committed
49
                          unsigned char abs_delta) {
50
51
  VP9_COMP *cpi = (VP9_COMP *)ptr;
  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

John Koleszar's avatar
John Koleszar committed
57
58
59
  // TBD ?? Set the feature mask
  // vpx_memcpy(cpi->mb.e_mbd.segment_feature_mask, 0,
  //            sizeof(cpi->mb.e_mbd.segment_feature_mask));
60
61
62
}

// Based on set of segment counts calculate a probability tree
63
static void calc_segtree_probs(int *segcounts, vp9_prob *segment_tree_probs) {
John Koleszar's avatar
John Koleszar committed
64
  // Work out probabilities of each segment
65
66
67
68
69
70
71
72
73
74
  const int c01 = segcounts[0] + segcounts[1];
  const int c23 = segcounts[2] + segcounts[3];
  const int c45 = segcounts[4] + segcounts[5];
  const int c67 = segcounts[6] + segcounts[7];

  segment_tree_probs[0] = get_binary_prob(c01 + c23, c45 + c67);
  segment_tree_probs[1] = get_binary_prob(c01, c23);
  segment_tree_probs[2] = get_binary_prob(c45, c67);
  segment_tree_probs[3] = get_binary_prob(segcounts[0], segcounts[1]);
  segment_tree_probs[4] = get_binary_prob(segcounts[2], segcounts[3]);
75
76
  segment_tree_probs[5] = get_binary_prob(segcounts[4], segcounts[5]);
  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
80
static int cost_segmap(int *segcounts, vp9_prob *probs) {
81
82
83
84
85
86
  const int c01 = segcounts[0] + segcounts[1];
  const int c23 = segcounts[2] + segcounts[3];
  const int c45 = segcounts[4] + segcounts[5];
  const int c67 = segcounts[6] + segcounts[7];
  const int c0123 = c01 + c23;
  const int c4567 = c45 + c67;
87

John Koleszar's avatar
John Koleszar committed
88
  // Cost the top node of the tree
89
90
  int cost = c0123 * vp9_cost_zero(probs[0]) +
             c4567 * vp9_cost_one(probs[0]);
91

92
  // Cost subsequent levels
93
94
95
96
97
98
99
100
101
102
  if (c0123 > 0) {
    cost += c01 * vp9_cost_zero(probs[1]) +
            c23 * vp9_cost_one(probs[1]);

    if (c01 > 0)
      cost += segcounts[0] * vp9_cost_zero(probs[3]) +
              segcounts[1] * vp9_cost_one(probs[3]);
    if (c23 > 0)
      cost += segcounts[2] * vp9_cost_zero(probs[4]) +
              segcounts[3] * vp9_cost_one(probs[4]);
103
  }
104

105
106
107
  if (c4567 > 0) {
    cost += c45 * vp9_cost_zero(probs[2]) +
            c67 * vp9_cost_one(probs[2]);
108

109
    if (c45 > 0)
110
111
      cost += segcounts[4] * vp9_cost_zero(probs[5]) +
              segcounts[5] * vp9_cost_one(probs[5]);
112
    if (c67 > 0)
113
114
      cost += segcounts[6] * vp9_cost_zero(probs[6]) +
              segcounts[7] * vp9_cost_one(probs[6]);
115
116
117
118
119
  }

  return cost;
}

James Zern's avatar
James Zern committed
120
121
static void count_segs(VP9_COMP *cpi, const TileInfo *const tile,
                       MODE_INFO **mi_8x8,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
122
123
                       int *no_pred_segcounts,
                       int (*temporal_predictor_count)[2],
124
                       int *t_unpred_seg_counts,
125
                       int bw, int bh, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
126
127
  VP9_COMMON *const cm = &cpi->common;
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
128
  int segment_id;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
129

130
131
132
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
    return;

133
  xd->mi_8x8 = mi_8x8;
134
  segment_id = xd->mi_8x8[0]->mbmi.segment_id;
135

James Zern's avatar
James Zern committed
136
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
137
138
139
140
141
142

  // Count the number of hits on each segment with no prediction
  no_pred_segcounts[segment_id]++;

  // Temporal prediction not allowed on key frames
  if (cm->frame_type != KEY_FRAME) {
143
    const BLOCK_SIZE bsize = mi_8x8[0]->mbmi.sb_type;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
144
    // Test to see if the segment id matches the predicted value.
145
    const int pred_segment_id = vp9_get_segment_id(cm, cm->last_frame_seg_map,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
146
                                                   bsize, mi_row, mi_col);
147
    const int pred_flag = pred_segment_id == segment_id;
148
    const int pred_context = vp9_get_pred_context_seg_id(xd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
149
150
151

    // Store the prediction status for this mb and update counts
    // as appropriate
152
    xd->mi_8x8[0]->mbmi.seg_id_predicted = pred_flag;
153
    temporal_predictor_count[pred_context][pred_flag]++;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
154

155
    if (!pred_flag)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
156
      // Update the "unpredicted" segment count
157
      t_unpred_seg_counts[segment_id]++;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
158
  }
John Koleszar's avatar
John Koleszar committed
159
160
}

James Zern's avatar
James Zern committed
161
162
static void count_segs_sb(VP9_COMP *cpi, const TileInfo *const tile,
                          MODE_INFO **mi_8x8,
163
164
165
166
                          int *no_pred_segcounts,
                          int (*temporal_predictor_count)[2],
                          int *t_unpred_seg_counts,
                          int mi_row, int mi_col,
167
                          BLOCK_SIZE bsize) {
168
  const VP9_COMMON *const cm = &cpi->common;
169
  const int mis = cm->mode_info_stride;
170
171
  int bw, bh;
  const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
172
173
174
175

  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
    return;

176
177
  bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type];
  bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type];
178

179
  if (bw == bs && bh == bs) {
James Zern's avatar
James Zern committed
180
    count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count,
181
182
               t_unpred_seg_counts, bs, bs, mi_row, mi_col);
  } else if (bw == bs && bh < bs) {
James Zern's avatar
James Zern committed
183
    count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count,
184
               t_unpred_seg_counts, bs, hbs, mi_row, mi_col);
James Zern's avatar
James Zern committed
185
    count_segs(cpi, tile, mi_8x8 + hbs * mis, no_pred_segcounts,
186
187
               temporal_predictor_count, t_unpred_seg_counts, bs, hbs,
               mi_row + hbs, mi_col);
188
  } else if (bw < bs && bh == bs) {
James Zern's avatar
James Zern committed
189
    count_segs(cpi, tile, mi_8x8, no_pred_segcounts, temporal_predictor_count,
190
               t_unpred_seg_counts, hbs, bs, mi_row, mi_col);
James Zern's avatar
James Zern committed
191
192
193
    count_segs(cpi, tile, mi_8x8 + hbs,
               no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts,
               hbs, bs, mi_row, mi_col + hbs);
194
  } else {
195
    const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
196
197
    int n;

198
    assert(bw < bs && bh < bs);
199
200

    for (n = 0; n < 4; n++) {
201
202
      const int mi_dc = hbs * (n & 1);
      const int mi_dr = hbs * (n >> 1);
203

James Zern's avatar
James Zern committed
204
      count_segs_sb(cpi, tile, &mi_8x8[mi_dr * mis + mi_dc],
205
206
                    no_pred_segcounts, temporal_predictor_count,
                    t_unpred_seg_counts,
207
                    mi_row + mi_dr, mi_col + mi_dc, subsize);
208
209
210
211
    }
  }
}

212
213
void vp9_choose_segmap_coding_method(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
214
  struct segmentation *seg = &cm->seg;
John Koleszar's avatar
John Koleszar committed
215
216
217
218

  int no_pred_cost;
  int t_pred_cost = INT_MAX;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
219
  int i, tile_col, mi_row, mi_col;
John Koleszar's avatar
John Koleszar committed
220

221
222
223
  int temporal_predictor_count[PREDICTION_PROBS][2] = { { 0 } };
  int no_pred_segcounts[MAX_SEGMENTS] = { 0 };
  int t_unpred_seg_counts[MAX_SEGMENTS] = { 0 };
John Koleszar's avatar
John Koleszar committed
224

Paul Wilkins's avatar
Paul Wilkins committed
225
226
  vp9_prob no_pred_tree[SEG_TREE_PROBS];
  vp9_prob t_pred_tree[SEG_TREE_PROBS];
227
  vp9_prob t_nopred_prob[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
228

229
  const int mis = cm->mode_info_stride;
230
  MODE_INFO **mi_ptr, **mi;
231

John Koleszar's avatar
John Koleszar committed
232
233
  // Set default state for the segment tree probabilities and the
  // temporal coding probabilities
234
235
  vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
  vpx_memset(seg->pred_probs, 255, sizeof(seg->pred_probs));
John Koleszar's avatar
John Koleszar committed
236
237
238

  // First of all generate stats regarding how well the last segment map
  // predicts this one
Dmitry Kovalev's avatar
Dmitry Kovalev committed
239
  for (tile_col = 0; tile_col < 1 << cm->log2_tile_cols; tile_col++) {
James Zern's avatar
James Zern committed
240
241
242
243
    TileInfo tile;

    vp9_tile_init(&tile, cm, 0, tile_col);
    mi_ptr = cm->mi_grid_visible + tile.mi_col_start;
244
    for (mi_row = 0; mi_row < cm->mi_rows;
Jingning Han's avatar
Jingning Han committed
245
         mi_row += 8, mi_ptr += 8 * mis) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
246
      mi = mi_ptr;
James Zern's avatar
James Zern committed
247
      for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
248
           mi_col += 8, mi += 8)
James Zern's avatar
James Zern committed
249
250
251
        count_segs_sb(cpi, &tile, mi, no_pred_segcounts,
                      temporal_predictor_count, t_unpred_seg_counts,
                      mi_row, mi_col, BLOCK_64X64);
252
    }
253
254
  }

John Koleszar's avatar
John Koleszar committed
255
256
  // Work out probability tree for coding segments without prediction
  // and the cost.
257
258
  calc_segtree_probs(no_pred_segcounts, no_pred_tree);
  no_pred_cost = cost_segmap(no_pred_segcounts, no_pred_tree);
John Koleszar's avatar
John Koleszar committed
259
260

  // Key frames cannot use temporal prediction
261
  if (!frame_is_intra_only(cm)) {
John Koleszar's avatar
John Koleszar committed
262
263
    // Work out probability tree for coding those segments not
    // predicted using the temporal method and the cost.
264
265
    calc_segtree_probs(t_unpred_seg_counts, t_pred_tree);
    t_pred_cost = cost_segmap(t_unpred_seg_counts, t_pred_tree);
John Koleszar's avatar
John Koleszar committed
266

267
    // Add in the cost of the signaling for each prediction context.
John Koleszar's avatar
John Koleszar committed
268
    for (i = 0; i < PREDICTION_PROBS; i++) {
269
270
271
272
      const int count0 = temporal_predictor_count[i][0];
      const int count1 = temporal_predictor_count[i][1];

      t_nopred_prob[i] = get_binary_prob(count0, count1);
John Koleszar's avatar
John Koleszar committed
273
274

      // Add in the predictor signaling cost
275
276
      t_pred_cost += count0 * vp9_cost_zero(t_nopred_prob[i]) +
                     count1 * vp9_cost_one(t_nopred_prob[i]);
277
    }
John Koleszar's avatar
John Koleszar committed
278
279
280
281
  }

  // Now choose which coding method to use.
  if (t_pred_cost < no_pred_cost) {
282
283
284
    seg->temporal_update = 1;
    vpx_memcpy(seg->tree_probs, t_pred_tree, sizeof(t_pred_tree));
    vpx_memcpy(seg->pred_probs, t_nopred_prob, sizeof(t_nopred_prob));
John Koleszar's avatar
John Koleszar committed
285
  } else {
286
287
    seg->temporal_update = 0;
    vpx_memcpy(seg->tree_probs, no_pred_tree, sizeof(no_pred_tree));
John Koleszar's avatar
John Koleszar committed
288
  }
289
}