vp9_aq_cyclicrefresh.c 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <limits.h>
#include <math.h>

Marco Paniconi's avatar
Marco Paniconi committed
14
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
15
16
17
18
19
20
21

#include "vp9/common/vp9_seg_common.h"

#include "vp9/encoder/vp9_ratectrl.h"
#include "vp9/encoder/vp9_rdopt.h"
#include "vp9/encoder/vp9_segmentation.h"

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
struct CYCLIC_REFRESH {
  // Target percentage of blocks per frame that are cyclicly refreshed.
  int max_mbs_perframe;
  // Maximum q-delta as percentage of base q.
  int max_qdelta_perc;
  // Block size below which we don't apply cyclic refresh.
  BLOCK_SIZE min_block_size;
  // Macroblock starting index (unit of 8x8) for cycling through the frame.
  int mb_index;
  // Controls how long a block will need to wait to be refreshed again.
  int time_for_refresh;
  // Actual number of blocks that were applied delta-q (segment 1).
  int num_seg_blocks;
  // Actual encoding bits for segment 1.
  int actual_seg_bits;
  // RD mult. parameters for segment 1.
  int rdmult;
  // Cyclic refresh map.
  signed char *map;
  // Projected rate and distortion for the current superblock.
  int64_t projected_rate_sb;
  int64_t projected_dist_sb;
  // Thresholds applied to projected rate/distortion of the superblock.
  int64_t thresh_rate_sb;
  int64_t thresh_dist_sb;
};

CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
  CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
  if (cr == NULL)
    return NULL;

  cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
  if (cr->map == NULL) {
    vpx_free(cr);
    return NULL;
  }

  return cr;
}

void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
  vpx_free(cr->map);
  vpx_free(cr);
}
67
68

// Check if we should turn off cyclic refresh based on bitrate condition.
69
70
static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
                                        const RATE_CONTROL *rc) {
71
72
73
74
75
  // Turn off cyclic refresh if bits available per frame is not sufficiently
  // larger than bit cost of segmentation. Segment map bit cost should scale
  // with number of seg blocks, so compare available bits to number of blocks.
  // Average bits available per frame = av_per_frame_bandwidth
  // Number of (8x8) blocks in frame = mi_rows * mi_cols;
76
77
  const float factor  = 0.5;
  const int number_blocks = cm->mi_rows  * cm->mi_cols;
78
79
  // The condition below corresponds to turning off at target bitrates:
  // ~24kbps for CIF, 72kbps for VGA (at 30fps).
80
  return rc->av_per_frame_bandwidth >= factor * number_blocks;
81
82
83
84
85
86
}

// Check if this coding block, of size bsize, should be considered for refresh
// (lower-qp coding). Decision can be based on various factors, such as
// size of the coding block (i.e., below min_block size rejected), coding
// mode, and rate/distortion.
87
88
89
static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
                                const MB_MODE_INFO *mbmi,
                                BLOCK_SIZE bsize, int use_rd) {
90
91
92
93
94
95
96
97
98
99
  if (use_rd) {
    // If projected rate is below the thresh_rate (well below target,
    // so undershoot expected), accept it for lower-qp coding.
    if (cr->projected_rate_sb < cr->thresh_rate_sb)
      return 1;
    // Otherwise, reject the block for lower-qp coding if any of the following:
    // 1) prediction block size is below min_block_size
    // 2) mode is non-zero mv and projected distortion is above thresh_dist
    // 3) mode is an intra-mode (we may want to allow some of this under
    // another thresh_dist)
100
101
102
103
    else if (bsize < cr->min_block_size ||
             (mbmi->mv[0].as_int != 0 &&
              cr->projected_dist_sb > cr->thresh_dist_sb) ||
             !is_inter_block(mbmi))
104
105
106
107
108
      return 0;
    else
      return 1;
  } else {
    // Rate/distortion not used for update.
109
110
111
    if (bsize < cr->min_block_size ||
        mbmi->mv[0].as_int != 0 ||
        !is_inter_block(mbmi))
112
113
114
115
116
117
118
119
120
      return 0;
    else
      return 1;
  }
}

// Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
// check if we should reset the segment_id, and update the cyclic_refresh map
// and segmentation map.
121
122
123
124
void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi,
                                       MB_MODE_INFO *const mbmi,
                                       int mi_row, int mi_col,
                                       BLOCK_SIZE bsize, int use_rd) {
125
  const VP9_COMMON *const cm = &cpi->common;
126
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
127
128
129
130
131
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
  const int xmis = MIN(cm->mi_cols - mi_col, bw);
  const int ymis = MIN(cm->mi_rows - mi_row, bh);
  const int block_index = mi_row * cm->mi_cols + mi_col;
132
  const int refresh_this_block = candidate_refresh_aq(cr, mbmi, bsize, use_rd);
133
134
135
  // Default is to not update the refresh map.
  int new_map_value = cr->map[block_index];
  int x = 0; int y = 0;
136

137
  // Check if we should reset the segment_id for this block.
138
139
  if (mbmi->segment_id > 0 && !refresh_this_block)
    mbmi->segment_id = 0;
140
141
142
143
144

  // Update the cyclic refresh map, to be used for setting segmentation map
  // for the next frame. If the block  will be refreshed this frame, mark it
  // as clean. The magnitude of the -ve influences how long before we consider
  // it for refresh again.
145
  if (mbmi->segment_id == 1) {
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    new_map_value = -cr->time_for_refresh;
  } else if (refresh_this_block) {
    // Else if it is accepted as candidate for refresh, and has not already
    // been refreshed (marked as 1) then mark it as a candidate for cleanup
    // for future time (marked as 0), otherwise don't update it.
    if (cr->map[block_index] == 1)
      new_map_value = 0;
  } else {
    // Leave it marked as block that is not candidate for refresh.
    new_map_value = 1;
  }
  // Update entries in the cyclic refresh map with new_map_value, and
  // copy mbmi->segment_id into global segmentation map.
  for (y = 0; y < ymis; y++)
    for (x = 0; x < xmis; x++) {
      cr->map[block_index + y * cm->mi_cols + x] = new_map_value;
      cpi->segmentation_map[block_index + y * cm->mi_cols + x] =
163
          mbmi->segment_id;
164
165
166
    }
  // Keep track of actual number (in units of 8x8) of blocks in segment 1 used
  // for encoding this frame.
167
  if (mbmi->segment_id)
168
169
170
171
    cr->num_seg_blocks += xmis * ymis;
}

// Setup cyclic background refresh: set delta q and segmentation map.
172
void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
173
  VP9_COMMON *const cm = &cpi->common;
174
  const RATE_CONTROL *const rc = &cpi->rc;
175
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
176
  struct segmentation *const seg = &cm->seg;
177
178
  unsigned char *const seg_map = cpi->segmentation_map;
  const int apply_cyclic_refresh  = apply_cyclic_refresh_bitrate(cm, rc);
179
180
  // Don't apply refresh on key frame or enhancement layer frames.
  if (!apply_cyclic_refresh ||
181
      (cm->frame_type == KEY_FRAME) ||
182
183
184
185
      (cpi->svc.temporal_layer_id > 0)) {
    // Set segmentation map to 0 and disable.
    vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
    vp9_disable_segmentation(&cm->seg);
186
    if (cm->frame_type == KEY_FRAME)
187
188
189
      cr->mb_index = 0;
    return;
  } else {
190
    const int mbs_in_frame = cm->mi_rows * cm->mi_cols;
191
    int qindex_delta = 0;
192
193
    int i, x, y, block_count;
    int mi_row, mi_col, qindex2;
194
195

    // Rate target ratio to set q delta.
196
    const float rate_ratio_qdelta = 2.0;
197
198
199
200
201
202
203
    vp9_clear_system_state();
    // Some of these parameters may be set via codec-control function later.
    cr->max_mbs_perframe = 10;
    cr->max_qdelta_perc = 50;
    cr->min_block_size = BLOCK_16X16;
    cr->time_for_refresh = 1;
    // Set rate threshold to some fraction of target (and scaled by 256).
204
    cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 2;
205
206
207
208
209
210
    // Distortion threshold, quadratic in Q, scale factor to be adjusted.
    cr->thresh_dist_sb = 8 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) *
        vp9_convert_qindex_to_q(cm->base_qindex));
    if (cpi->sf.use_nonrd_pick_mode) {
      // May want to be more conservative with thresholds in non-rd mode for now
      // as rate/distortion are derived from model based on prediction residual.
211
      cr->thresh_rate_sb = (rc->sb64_target_rate * 256) >> 3;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
      cr->thresh_dist_sb = 4 * (int)(vp9_convert_qindex_to_q(cm->base_qindex) *
          vp9_convert_qindex_to_q(cm->base_qindex));
    }

    cr->num_seg_blocks = 0;
    // Set up segmentation.
    // Clear down the segment map.
    vpx_memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
    vp9_enable_segmentation(&cm->seg);
    vp9_clearall_segfeatures(seg);
    // Select delta coding method.
    seg->abs_delta = SEGMENT_DELTADATA;

    // Note: setting temporal_update has no effect, as the seg-map coding method
    // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
    // based on the coding cost of each method. For error_resilient mode on the
    // last_frame_seg_map is set to 0, so if temporal coding is used, it is
    // relative to 0 previous map.
    // seg->temporal_update = 0;

    // Segment 0 "Q" feature is disabled so it defaults to the baseline Q.
    vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);
    // Use segment 1 for in-frame Q adjustment.
    vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);

    // Set the q delta for segment 1.
    qindex_delta = vp9_compute_qdelta_by_rate(cpi,
                                              cm->base_qindex,
                                              rate_ratio_qdelta);
    // TODO(marpan): Incorporate the actual-vs-target rate over/undershoot from
    // previous encoded frame.
243
    if (-qindex_delta > cr->max_qdelta_perc * cm->base_qindex / 100)
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
      qindex_delta = -cr->max_qdelta_perc * cm->base_qindex / 100;

    // Compute rd-mult for segment 1.
    qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
    cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);

    vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qindex_delta);
    // Number of target macroblocks to get the q delta (segment 1).
    block_count = cr->max_mbs_perframe * mbs_in_frame / 100;
    // Set the segmentation map: cycle through the macroblocks, starting at
    // cr->mb_index, and stopping when either block_count blocks have been found
    // to be refreshed, or we have passed through whole frame.
    // Note the setting of seg_map below is done in two steps (one over 8x8)
    // and then another over SB, in order to keep the value constant over SB.
    // TODO(marpan): Do this in one pass in SB order.
    assert(cr->mb_index < mbs_in_frame);
    i = cr->mb_index;
    do {
      // If the macroblock is as a candidate for clean up then mark it
      // for possible boost/refresh (segment 1). The segment id may get reset to
      // 0 later if the macroblock gets coded anything other than ZEROMV.
      if (cr->map[i] == 0) {
        seg_map[i] = 1;
        block_count--;
      } else if (cr->map[i] < 0) {
        cr->map[i]++;
      }
      i++;
      if (i == mbs_in_frame) {
        i = 0;
      }
    } while (block_count && i != cr->mb_index);
    cr->mb_index = i;
    // Enforce constant segment map over superblock.
    for (mi_row = 0; mi_row < cm->mi_rows; mi_row +=  MI_BLOCK_SIZE)
      for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
280
281
282
283
284
285
        const int bl_index = mi_row * cm->mi_cols + mi_col;
        const int xmis = MIN(cm->mi_cols - mi_col,
                             num_8x8_blocks_wide_lookup[BLOCK_64X64]);
        const int ymis = MIN(cm->mi_rows - mi_row,
                             num_8x8_blocks_high_lookup[BLOCK_64X64]);
        int sum_map = 0;
286
        for (y = 0; y < ymis; y++)
287
288
          for (x = 0; x < xmis; x++)
            sum_map += seg_map[bl_index + y * cm->mi_cols + x];
289
290
        // If segment is partial over superblock, reset.
        if (sum_map > 0 && sum_map < xmis * ymis) {
291
          const int new_value = (sum_map >= xmis * ymis / 2);
292
          for (y = 0; y < ymis; y++)
293
294
            for (x = 0; x < xmis; x++)
              seg_map[bl_index + y * cm->mi_cols + x] = new_value;
295
296
297
298
        }
      }
  }
}
299
300
301
302
303
304
305
306
307
308

void vp9_cyclic_refresh_set_rate_and_dist_sb(CYCLIC_REFRESH *cr,
                                             int64_t rate_sb, int64_t dist_sb) {
  cr->projected_rate_sb = rate_sb;
  cr->projected_dist_sb = dist_sb;
}

int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
  return cr->rdmult;
}