Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/encoder/aq_cyclicrefresh.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
#include <math.h>
14
15
#include "av1/common/pred_common.h"
16
#include "av1/common/seg_common.h"
17
#include "av1/encoder/aq_cyclicrefresh.h"
18
#include "av1/encoder/encoder_utils.h"
19
#include "av1/encoder/ratectrl.h"
20
#include "av1/encoder/segmentation.h"
21
#include "av1/encoder/tokenize.h"
22
#include "aom_dsp/aom_dsp_common.h"
23
24
0
CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
25
0
  CYCLIC_REFRESH *const cr = aom_calloc(1, sizeof(*cr));
26
0
  if (cr == NULL) return NULL;
27
28
0
  cr->map = aom_calloc(mi_rows * mi_cols, sizeof(*cr->map));
29
0
  cr->counter_encode_maxq_scene_change = 0;
30
0
  cr->percent_refresh_adjustment = 5;
31
0
  cr->rate_ratio_qdelta_adjustment = 0.25;
32
0
  if (cr->map == NULL) {
33
0
    av1_cyclic_refresh_free(cr);
34
0
    return NULL;
35
0
  }
36
0
  return cr;
37
0
}
38
39
0
void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
40
0
  if (cr != NULL) {
41
0
    aom_free(cr->map);
42
0
    aom_free(cr);
43
0
  }
44
0
}
45
46
// Check if this coding block, of size bsize, should be considered for refresh
47
// (lower-qp coding). Decision can be based on various factors, such as
48
// size of the coding block (i.e., below min_block size rejected), coding
49
// mode, and rate/distortion.
50
static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
51
                                const MB_MODE_INFO *mbmi, int64_t rate,
52
                                int64_t dist, BLOCK_SIZE bsize,
53
0
                                int noise_level) {
54
0
  MV mv = mbmi->mv[0].as_mv;
55
0
  int is_compound = has_second_ref(mbmi);
56
  // Reject the block for lower-qp coding for non-compound mode if
57
  // projected distortion is above the threshold, and any of the following
58
  // is true:
59
  // 1) mode uses large mv
60
  // 2) mode is an intra-mode
61
  // Otherwise accept for refresh.
62
0
  if (!is_compound && dist > cr->thresh_dist_sb &&
63
0
      (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
64
0
       mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
65
0
       !is_inter_block(mbmi)))
66
0
    return CR_SEGMENT_ID_BASE;
67
0
  else if ((is_compound && noise_level < kMedium) ||
68
0
           (bsize >= BLOCK_16X16 && rate < cr->thresh_rate_sb &&
69
0
            is_inter_block(mbmi) && mbmi->mv[0].as_int == 0 &&
70
0
            cr->rate_boost_fac > 10))
71
    // More aggressive delta-q for bigger blocks with zero motion.
72
0
    return CR_SEGMENT_ID_BOOST2;
73
0
  else
74
0
    return CR_SEGMENT_ID_BOOST1;
75
0
}
76
77
// Compute delta-q for the segment.
78
0
static int compute_deltaq(const AV1_COMP *cpi, int q, double rate_factor) {
79
0
  const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
80
0
  int deltaq = av1_compute_qdelta_by_rate(
81
0
      cpi, cpi->common.current_frame.frame_type, q, rate_factor);
82
0
  if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
83
0
    deltaq = -cr->max_qdelta_perc * q / 100;
84
0
  }
85
0
  return deltaq;
86
0
}
87
88
int av1_cyclic_refresh_estimate_bits_at_q(const AV1_COMP *cpi,
89
0
                                          double correction_factor) {
90
0
  const AV1_COMMON *const cm = &cpi->common;
91
0
  const int base_qindex = cm->quant_params.base_qindex;
92
0
  const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
93
0
  const int mbs = cm->mi_params.MBs;
94
0
  const int num4x4bl = mbs << 4;
95
  // Weight for non-base segments: use actual number of blocks refreshed in
96
  // previous/just encoded frame. Note number of blocks here is in 4x4 units.
97
0
  double weight_segment1 = (double)cr->actual_num_seg1_blocks / num4x4bl;
98
0
  double weight_segment2 = (double)cr->actual_num_seg2_blocks / num4x4bl;
99
0
  if (cpi->rc.rtc_external_ratectrl) {
100
0
    weight_segment1 = (double)(cr->percent_refresh * cm->mi_params.mi_rows *
101
0
                               cm->mi_params.mi_cols / 100) /
102
0
                      num4x4bl;
103
0
    weight_segment2 = 0;
104
0
  }
105
  // Take segment weighted average for estimated bits.
106
0
  const int estimated_bits = (int)round(
107
0
      (1.0 - weight_segment1 - weight_segment2) *
108
0
          av1_estimate_bits_at_q(cpi, base_qindex, correction_factor) +
109
0
      weight_segment1 *
110
0
          av1_estimate_bits_at_q(cpi, base_qindex + cr->qindex_delta[1],
111
0
                                 correction_factor) +
112
0
      weight_segment2 *
113
0
          av1_estimate_bits_at_q(cpi, base_qindex + cr->qindex_delta[2],
114
0
                                 correction_factor));
115
0
  return estimated_bits;
116
0
}
117
118
int av1_cyclic_refresh_rc_bits_per_mb(const AV1_COMP *cpi, int i,
119
0
                                      double correction_factor) {
120
0
  const AV1_COMMON *const cm = &cpi->common;
121
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
122
0
  int bits_per_mb;
123
0
  int num4x4bl = cm->mi_params.MBs << 4;
124
  // Weight for segment prior to encoding: take the average of the target
125
  // number for the frame to be encoded and the actual from the previous frame.
126
0
  double weight_segment =
127
0
      (double)((cr->target_num_seg_blocks + cr->actual_num_seg1_blocks +
128
0
                cr->actual_num_seg2_blocks) >>
129
0
               1) /
130
0
      num4x4bl;
131
0
  if (cpi->rc.rtc_external_ratectrl) {
132
0
    weight_segment = (double)((cr->target_num_seg_blocks +
133
0
                               cr->percent_refresh * cm->mi_params.mi_rows *
134
0
                                   cm->mi_params.mi_cols / 100) >>
135
0
                              1) /
136
0
                     num4x4bl;
137
0
  }
138
  // Compute delta-q corresponding to qindex i.
139
0
  int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
140
0
  const int accurate_estimate = cpi->sf.hl_sf.accurate_bit_estimate;
141
  // Take segment weighted average for bits per mb.
142
0
  bits_per_mb = (int)round(
143
0
      (1.0 - weight_segment) *
144
0
          av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type, i,
145
0
                             correction_factor, accurate_estimate) +
146
0
      weight_segment * av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type,
147
0
                                          i + deltaq, correction_factor,
148
0
                                          accurate_estimate));
149
0
  return bits_per_mb;
150
0
}
151
152
void av1_cyclic_reset_segment_skip(const AV1_COMP *cpi, MACROBLOCK *const x,
153
                                   int mi_row, int mi_col, BLOCK_SIZE bsize,
154
0
                                   RUN_TYPE dry_run) {
155
0
  int cdf_num;
156
0
  const AV1_COMMON *const cm = &cpi->common;
157
0
  MACROBLOCKD *const xd = &x->e_mbd;
158
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
159
0
  const int prev_segment_id = mbmi->segment_id;
160
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
161
0
  const int bw = mi_size_wide[bsize];
162
0
  const int bh = mi_size_high[bsize];
163
0
  const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
164
0
  const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
165
166
0
  assert(cm->seg.enabled);
167
168
0
  if (!cr->skip_over4x4) {
169
0
    mbmi->segment_id =
170
0
        av1_get_spatial_seg_pred(cm, xd, &cdf_num, cr->skip_over4x4);
171
0
    if (prev_segment_id != mbmi->segment_id) {
172
0
      const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
173
0
      const int mi_stride = cm->mi_params.mi_cols;
174
0
      const uint8_t segment_id = mbmi->segment_id;
175
0
      for (int mi_y = 0; mi_y < ymis; mi_y++) {
176
0
        const int map_offset = block_index + mi_y * mi_stride;
177
0
        memset(&cr->map[map_offset], 0, xmis);
178
0
        memset(&cpi->enc_seg.map[map_offset], segment_id, xmis);
179
0
        memset(&cm->cur_frame->seg_map[map_offset], segment_id, xmis);
180
0
      }
181
0
    }
182
0
  }
183
0
  if (!dry_run) {
184
0
    if (cyclic_refresh_segment_id(prev_segment_id) == CR_SEGMENT_ID_BOOST1)
185
0
      x->actual_num_seg1_blocks -= xmis * ymis;
186
0
    else if (cyclic_refresh_segment_id(prev_segment_id) == CR_SEGMENT_ID_BOOST2)
187
0
      x->actual_num_seg2_blocks -= xmis * ymis;
188
0
  }
189
0
}
190
191
void av1_cyclic_refresh_update_segment(const AV1_COMP *cpi, MACROBLOCK *const x,
192
                                       int mi_row, int mi_col, BLOCK_SIZE bsize,
193
                                       int64_t rate, int64_t dist, int skip,
194
0
                                       RUN_TYPE dry_run) {
195
0
  const AV1_COMMON *const cm = &cpi->common;
196
0
  MACROBLOCKD *const xd = &x->e_mbd;
197
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
198
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
199
0
  const int bw = mi_size_wide[bsize];
200
0
  const int bh = mi_size_high[bsize];
201
0
  const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
202
0
  const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
203
0
  const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
204
0
  int noise_level = 0;
205
0
  if (cpi->noise_estimate.enabled) noise_level = cpi->noise_estimate.level;
206
0
  const int refresh_this_block =
207
0
      candidate_refresh_aq(cr, mbmi, rate, dist, bsize, noise_level);
208
0
  int sh = cpi->cyclic_refresh->skip_over4x4 ? 2 : 1;
209
  // Default is to not update the refresh map.
210
0
  int new_map_value = cr->map[block_index];
211
212
  // If this block is labeled for refresh, check if we should reset the
213
  // segment_id.
214
0
  if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
215
0
    mbmi->segment_id = refresh_this_block;
216
    // Reset segment_id if will be skipped.
217
0
    if (skip) mbmi->segment_id = CR_SEGMENT_ID_BASE;
218
0
  }
219
0
  const uint8_t segment_id = mbmi->segment_id;
220
221
  // Update the cyclic refresh map, to be used for setting segmentation map
222
  // for the next frame. If the block  will be refreshed this frame, mark it
223
  // as clean. The magnitude of the -ve influences how long before we consider
224
  // it for refresh again.
225
0
  if (cyclic_refresh_segment_id_boosted(segment_id)) {
226
0
    new_map_value = -cr->time_for_refresh;
227
0
  } else if (refresh_this_block) {
228
    // Else if it is accepted as candidate for refresh, and has not already
229
    // been refreshed (marked as 1) then mark it as a candidate for cleanup
230
    // for future time (marked as 0), otherwise don't update it.
231
0
    if (cr->map[block_index] == 1) new_map_value = 0;
232
0
  } else {
233
    // Leave it marked as block that is not candidate for refresh.
234
0
    new_map_value = 1;
235
0
  }
236
237
  // Update entries in the cyclic refresh map with new_map_value, and
238
  // copy mbmi->segment_id into global segmentation map.
239
0
  const int mi_stride = cm->mi_params.mi_cols;
240
0
  for (int mi_y = 0; mi_y < ymis; mi_y += sh) {
241
0
    const int map_offset = block_index + mi_y * mi_stride;
242
0
    memset(&cr->map[map_offset], new_map_value, xmis);
243
0
    memset(&cpi->enc_seg.map[map_offset], segment_id, xmis);
244
0
    memset(&cm->cur_frame->seg_map[map_offset], segment_id, xmis);
245
0
  }
246
247
  // Accumulate cyclic refresh update counters.
248
0
  if (!dry_run) {
249
0
    if (cyclic_refresh_segment_id(segment_id) == CR_SEGMENT_ID_BOOST1)
250
0
      x->actual_num_seg1_blocks += xmis * ymis;
251
0
    else if (cyclic_refresh_segment_id(segment_id) == CR_SEGMENT_ID_BOOST2)
252
0
      x->actual_num_seg2_blocks += xmis * ymis;
253
0
  }
254
0
}
255
256
// Initializes counters used for cyclic refresh.
257
0
void av1_init_cyclic_refresh_counters(MACROBLOCK *const x) {
258
0
  x->actual_num_seg1_blocks = 0;
259
0
  x->actual_num_seg2_blocks = 0;
260
0
}
261
262
// Accumulate cyclic refresh counters.
263
void av1_accumulate_cyclic_refresh_counters(
264
0
    CYCLIC_REFRESH *const cyclic_refresh, const MACROBLOCK *const x) {
265
0
  cyclic_refresh->actual_num_seg1_blocks += x->actual_num_seg1_blocks;
266
0
  cyclic_refresh->actual_num_seg2_blocks += x->actual_num_seg2_blocks;
267
0
}
268
269
0
void av1_cyclic_refresh_set_golden_update(AV1_COMP *const cpi) {
270
0
  RATE_CONTROL *const rc = &cpi->rc;
271
0
  PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
272
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
273
  // Set minimum gf_interval for GF update to a multiple of the refresh period,
274
  // with some max limit. Depending on past encoding stats, GF flag may be
275
  // reset and update may not occur until next baseline_gf_interval.
276
0
  const int gf_length_mult[2] = { 8, 4 };
277
0
  if (cr->percent_refresh > 0)
278
0
    p_rc->baseline_gf_interval =
279
0
        AOMMIN(gf_length_mult[cpi->sf.rt_sf.gf_length_lvl] *
280
0
                   (100 / cr->percent_refresh),
281
0
               MAX_GF_INTERVAL_RT);
282
0
  else
283
0
    p_rc->baseline_gf_interval = FIXED_GF_INTERVAL_RT;
284
0
  if (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 40)
285
0
    p_rc->baseline_gf_interval = 16;
286
0
}
287
288
// Update the segmentation map, and related quantities: cyclic refresh map,
289
// refresh sb_index, and target number of blocks to be refreshed.
290
// The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
291
// 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
292
// Blocks labeled as BOOST1 may later get set to BOOST2 (during the
293
// encoding of the superblock).
294
0
static void cyclic_refresh_update_map(AV1_COMP *const cpi) {
295
0
  AV1_COMMON *const cm = &cpi->common;
296
0
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
297
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
298
0
  unsigned char *const seg_map = cpi->enc_seg.map;
299
0
  unsigned char *const active_map_4x4 = cpi->active_map.map;
300
0
  int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
301
0
  int xmis, ymis, x, y;
302
0
  uint64_t sb_sad = 0;
303
0
  uint64_t thresh_sad_low = 0;
304
0
  uint64_t thresh_sad = INT64_MAX;
305
0
  const int mi_rows = mi_params->mi_rows, mi_cols = mi_params->mi_cols;
306
0
  const int mi_stride = mi_cols;
307
  // Don't set seg_map to 0 if active_maps is enabled. Active_maps will set
308
  // seg_map to either 7 or 0 (AM_SEGMENT_ID_INACTIVE/ACTIVE), and cyclic
309
  // refresh set below (segment 1 or 2) will only be set for ACTIVE blocks.
310
0
  if (!cpi->active_map.enabled) {
311
0
    memset(seg_map, CR_SEGMENT_ID_BASE, mi_rows * mi_cols);
312
0
  }
313
0
  sb_cols = (mi_cols + cm->seq_params->mib_size - 1) / cm->seq_params->mib_size;
314
0
  sb_rows = (mi_rows + cm->seq_params->mib_size - 1) / cm->seq_params->mib_size;
315
0
  sbs_in_frame = sb_cols * sb_rows;
316
  // Number of target blocks to get the q delta (segment 1).
317
0
  block_count = cr->percent_refresh * mi_rows * mi_cols / 100;
318
  // Set the segmentation map: cycle through the superblocks, starting at
319
  // cr->mb_index, and stopping when either block_count blocks have been found
320
  // to be refreshed, or we have passed through whole frame.
321
0
  if (cr->sb_index >= sbs_in_frame) cr->sb_index = 0;
322
0
  assert(cr->sb_index < sbs_in_frame);
323
0
  i = cr->sb_index;
324
0
  cr->last_sb_index = cr->sb_index;
325
0
  cr->target_num_seg_blocks = 0;
326
0
  do {
327
0
    int sum_map = 0;
328
    // Get the mi_row/mi_col corresponding to superblock index i.
329
0
    int sb_row_index = (i / sb_cols);
330
0
    int sb_col_index = i - sb_row_index * sb_cols;
331
0
    int mi_row = sb_row_index * cm->seq_params->mib_size;
332
0
    int mi_col = sb_col_index * cm->seq_params->mib_size;
333
0
    assert(mi_row >= 0 && mi_row < mi_rows);
334
0
    assert(mi_col >= 0 && mi_col < mi_cols);
335
0
    bl_index = mi_row * mi_stride + mi_col;
336
    // Loop through all MI blocks in superblock and update map.
337
0
    xmis = AOMMIN(mi_cols - mi_col, cm->seq_params->mib_size);
338
0
    ymis = AOMMIN(mi_rows - mi_row, cm->seq_params->mib_size);
339
0
    if (cr->use_block_sad_scene_det && cpi->rc.frames_since_key > 30 &&
340
0
        cr->counter_encode_maxq_scene_change > 30 &&
341
0
        cpi->src_sad_blk_64x64 != NULL &&
342
0
        cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
343
0
      sb_sad = cpi->src_sad_blk_64x64[sb_col_index + sb_cols * sb_row_index];
344
0
      int scale = (cm->width * cm->height < 640 * 360) ? 6 : 8;
345
0
      int scale_low = 2;
346
0
      thresh_sad = (scale * 64 * 64);
347
0
      thresh_sad_low = (scale_low * 64 * 64);
348
      // For temporal layers: the base temporal layer (temporal_layer_id = 0)
349
      // has larger frame separation (2 or 4 frames apart), so use larger sad
350
      // thresholds to compensate for larger frame sad. The larger thresholds
351
      // also increase the amount of refresh, which is needed for the base
352
      // temporal layer.
353
0
      if (cpi->svc.number_temporal_layers > 1 &&
354
0
          cpi->svc.temporal_layer_id == 0) {
355
0
        thresh_sad <<= 4;
356
0
        thresh_sad_low <<= 2;
357
0
      }
358
0
    }
359
    // cr_map only needed at 8x8 blocks.
360
0
    for (y = 0; y < ymis; y += 2) {
361
0
      for (x = 0; x < xmis; x += 2) {
362
0
        const int bl_index2 = bl_index + y * mi_stride + x;
363
        // If the block is as a candidate for clean up then mark it
364
        // for possible boost/refresh (segment 1). The segment id may get
365
        // reset to 0 later if block gets coded anything other than low motion.
366
        // If the block_sad (sb_sad) is very low label it for refresh anyway.
367
        // If active_maps is enabled, only allow for setting on ACTIVE blocks.
368
0
        if ((cr->map[bl_index2] == 0 || sb_sad < thresh_sad_low) &&
369
0
            (!cpi->active_map.enabled ||
370
0
             active_map_4x4[bl_index2] == AM_SEGMENT_ID_ACTIVE)) {
371
0
          sum_map += 4;
372
0
        } else if (cr->map[bl_index2] < 0) {
373
0
          cr->map[bl_index2]++;
374
0
        }
375
0
      }
376
0
    }
377
    // Enforce constant segment over superblock.
378
    // If segment is at least half of superblock, set to 1.
379
    // Enforce that block sad (sb_sad) is not too high.
380
0
    if (sum_map >= (xmis * ymis) >> 1 && sb_sad < thresh_sad) {
381
0
      set_segment_id(seg_map, bl_index, xmis, ymis, mi_stride,
382
0
                     CR_SEGMENT_ID_BOOST1);
383
0
      cr->target_num_seg_blocks += xmis * ymis;
384
0
    }
385
0
    i++;
386
0
    if (i == sbs_in_frame) {
387
0
      i = 0;
388
0
    }
389
0
  } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
390
0
  cr->sb_index = i;
391
0
  if (cr->target_num_seg_blocks == 0) {
392
    // Disable segmentation, seg_map is already set to 0 above.
393
    // Don't disable if active_map is being used.
394
0
    if (!cpi->active_map.enabled) av1_disable_segmentation(&cm->seg);
395
0
  }
396
0
}
397
398
0
static int is_scene_change_detected(AV1_COMP *const cpi) {
399
0
  return cpi->rc.high_source_sad;
400
0
}
401
402
// Set cyclic refresh parameters.
403
0
void av1_cyclic_refresh_update_parameters(AV1_COMP *const cpi) {
404
  // TODO(marpan): Parameters need to be tuned.
405
0
  const RATE_CONTROL *const rc = &cpi->rc;
406
0
  const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
407
0
  const AV1_COMMON *const cm = &cpi->common;
408
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
409
0
  SVC *const svc = &cpi->svc;
410
0
  const int qp_thresh = AOMMAX(16, rc->best_quality + 4);
411
0
  const int qp_max_thresh = 118 * MAXQ >> 7;
412
0
  const int scene_change_detected = is_scene_change_detected(cpi);
413
0
  const int is_screen_content =
414
0
      (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN);
415
416
  // A scene change or key frame marks the start of a cyclic refresh cycle.
417
0
  const int frames_since_scene_change =
418
0
      (cpi->ppi->use_svc || !is_screen_content)
419
0
          ? cpi->rc.frames_since_key
420
0
          : AOMMIN(cpi->rc.frames_since_key,
421
0
                   cr->counter_encode_maxq_scene_change);
422
423
  // Cases to reset the cyclic refresh adjustment parameters.
424
0
  if (frame_is_intra_only(cm) || scene_change_detected ||
425
0
      cpi->ppi->rtc_ref.bias_recovery_frame) {
426
    // Reset adaptive elements for intra only frames and scene changes.
427
0
    cr->percent_refresh_adjustment = 5;
428
0
    cr->rate_ratio_qdelta_adjustment = 0.25;
429
0
  }
430
431
  // Although this segment feature for RTC is only used for
432
  // blocks >= 8X8, for more efficient coding of the seg map
433
  // cur_frame->seg_map needs to set at 4x4 along with the
434
  // function av1_cyclic_reset_segment_skip(). Skipping over
435
  // 4x4 will therefore have small bdrate loss (~0.2%), so
436
  // we use it only for speed > 9 for now.
437
0
  cr->skip_over4x4 = (cpi->oxcf.speed > 9) ? 1 : 0;
438
439
  // should we enable cyclic refresh on this frame.
440
0
  cr->apply_cyclic_refresh = 1;
441
0
  if (frame_is_intra_only(cm) || is_lossless_requested(&cpi->oxcf.rc_cfg) ||
442
0
      cpi->rc.high_motion_content_screen_rtc || scene_change_detected ||
443
0
      svc->temporal_layer_id > 0 ||
444
0
      svc->prev_number_spatial_layers != svc->number_spatial_layers ||
445
0
      p_rc->avg_frame_qindex[INTER_FRAME] < qp_thresh ||
446
0
      (svc->number_spatial_layers > 1 &&
447
0
       svc->layer_context[svc->temporal_layer_id].is_key_frame) ||
448
0
      (frames_since_scene_change > 20 &&
449
0
       p_rc->avg_frame_qindex[INTER_FRAME] > qp_max_thresh) ||
450
0
      (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 30 &&
451
0
       frames_since_scene_change > 40) ||
452
0
      cpi->ppi->rtc_ref.bias_recovery_frame) {
453
0
    cr->apply_cyclic_refresh = 0;
454
0
    return;
455
0
  }
456
457
  // Increase the amount of refresh for #temporal_layers > 2
458
0
  if (svc->number_temporal_layers > 2)
459
0
    cr->percent_refresh = 15;
460
0
  else
461
0
    cr->percent_refresh = 10 + cr->percent_refresh_adjustment;
462
463
0
  if (cpi->active_map.enabled) {
464
    // Scale down the percent_refresh to target the active blocks only.
465
0
    cr->percent_refresh =
466
0
        cr->percent_refresh * (100 - cpi->rc.percent_blocks_inactive) / 100;
467
0
    if (cr->percent_refresh == 0) {
468
0
      cr->apply_cyclic_refresh = 0;
469
0
    }
470
0
  }
471
472
0
  cr->max_qdelta_perc = 60;
473
0
  cr->time_for_refresh = 0;
474
0
  cr->use_block_sad_scene_det =
475
0
      (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
476
0
       cm->seq_params->sb_size == BLOCK_64X64)
477
0
          ? 1
478
0
          : 0;
479
0
  cr->motion_thresh = 32;
480
0
  cr->rate_boost_fac =
481
0
      (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) ? 10 : 15;
482
483
  // Use larger delta-qp (increase rate_ratio_qdelta) for first few
484
  // refresh cycles after a key frame (svc) or scene change (non svc).
485
  // For non svc screen content, after a scene change gradually reduce
486
  // this boost and supress it further if either of the previous two
487
  // frames overshot.
488
0
  if (cr->percent_refresh > 0) {
489
0
    if (cpi->ppi->use_svc || !is_screen_content) {
490
0
      if (frames_since_scene_change <
491
0
          ((4 * svc->number_temporal_layers) * (100 / cr->percent_refresh))) {
492
0
        cr->rate_ratio_qdelta = 3.0 + cr->rate_ratio_qdelta_adjustment;
493
0
      } else {
494
0
        cr->rate_ratio_qdelta = 2.25 + cr->rate_ratio_qdelta_adjustment;
495
0
      }
496
0
    } else {
497
0
      double distance_from_sc_factor =
498
0
          AOMMIN(0.75, (int)(frames_since_scene_change / 10) * 0.1);
499
0
      cr->rate_ratio_qdelta =
500
0
          3.0 + cr->rate_ratio_qdelta_adjustment - distance_from_sc_factor;
501
0
      if ((frames_since_scene_change < 10) &&
502
0
          ((cpi->rc.rc_1_frame < 0) || (cpi->rc.rc_2_frame < 0))) {
503
0
        cr->rate_ratio_qdelta -= 0.25;
504
0
      }
505
0
    }
506
0
  } else {
507
0
    cr->rate_ratio_qdelta = 2.25 + cr->rate_ratio_qdelta_adjustment;
508
0
  }
509
  // Adjust some parameters for low resolutions.
510
0
  if (cm->width * cm->height <= 352 * 288) {
511
0
    if (cpi->svc.number_temporal_layers > 1) {
512
0
      cr->motion_thresh = 32;
513
0
      cr->rate_boost_fac = 13;
514
0
    } else {
515
0
      if (rc->avg_frame_bandwidth < 3000) {
516
0
        cr->motion_thresh = 16;
517
0
        cr->rate_boost_fac = 13;
518
0
      } else {
519
0
        cr->max_qdelta_perc = 50;
520
0
        cr->rate_ratio_qdelta = AOMMAX(cr->rate_ratio_qdelta, 2.0);
521
0
      }
522
0
    }
523
0
  }
524
0
  if (cpi->oxcf.rc_cfg.mode == AOM_VBR) {
525
    // To be adjusted for VBR mode, e.g., based on gf period and boost.
526
    // For now use smaller qp-delta (than CBR), no second boosted seg, and
527
    // turn-off (no refresh) on golden refresh (since it's already boosted).
528
0
    cr->percent_refresh = 10;
529
0
    cr->rate_ratio_qdelta = 1.5;
530
0
    cr->rate_boost_fac = 10;
531
0
    if (cpi->refresh_frame.golden_frame) {
532
0
      cr->percent_refresh = 0;
533
0
      cr->rate_ratio_qdelta = 1.0;
534
0
    }
535
0
  }
536
0
  if (rc->rtc_external_ratectrl) {
537
0
    cr->actual_num_seg1_blocks = cr->percent_refresh * cm->mi_params.mi_rows *
538
0
                                 cm->mi_params.mi_cols / 100;
539
0
    cr->actual_num_seg2_blocks = 0;
540
0
  }
541
0
}
542
543
0
static void cyclic_refresh_reset_resize(AV1_COMP *const cpi) {
544
0
  const AV1_COMMON *const cm = &cpi->common;
545
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
546
0
  memset(cr->map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
547
0
  cr->sb_index = 0;
548
0
  cr->last_sb_index = 0;
549
0
  cpi->refresh_frame.golden_frame = true;
550
0
  cr->apply_cyclic_refresh = 0;
551
0
  cr->counter_encode_maxq_scene_change = 0;
552
0
  cr->percent_refresh_adjustment = 5;
553
0
  cr->rate_ratio_qdelta_adjustment = 0.25;
554
0
}
555
556
// Setup cyclic background refresh: set delta q and segmentation map.
557
0
void av1_cyclic_refresh_setup(AV1_COMP *const cpi) {
558
0
  AV1_COMMON *const cm = &cpi->common;
559
0
  const RATE_CONTROL *const rc = &cpi->rc;
560
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
561
0
  struct segmentation *const seg = &cm->seg;
562
0
  const int scene_change_detected = is_scene_change_detected(cpi);
563
0
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
564
0
  const int boost_index = AOMMIN(15, (cpi->ppi->p_rc.gfu_boost / 100));
565
0
  const int layer_depth = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
566
0
  const FRAME_TYPE frame_type = cm->current_frame.frame_type;
567
568
  // Set resolution_change flag: for svc only set it when the
569
  // number of spatial layers has not changed.
570
0
  const int resolution_change =
571
0
      cm->prev_frame &&
572
0
      (cm->width != cm->prev_frame->width ||
573
0
       cm->height != cm->prev_frame->height) &&
574
0
      cpi->svc.prev_number_spatial_layers == cpi->svc.number_spatial_layers;
575
576
0
  if (resolution_change) cyclic_refresh_reset_resize(cpi);
577
0
  if (!cr->apply_cyclic_refresh) {
578
    // Don't disable and set seg_map to 0 if active_maps is enabled, unless
579
    // whole frame is set as inactive (since we only apply cyclic_refresh to
580
    // active blocks).
581
0
    if (!cpi->active_map.enabled || cpi->rc.percent_blocks_inactive == 100) {
582
0
      unsigned char *const seg_map = cpi->enc_seg.map;
583
0
      memset(seg_map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
584
0
      av1_disable_segmentation(&cm->seg);
585
0
    }
586
0
    if (frame_is_intra_only(cm) || scene_change_detected ||
587
0
        cpi->ppi->rtc_ref.bias_recovery_frame) {
588
0
      cr->sb_index = 0;
589
0
      cr->last_sb_index = 0;
590
0
      cr->counter_encode_maxq_scene_change = 0;
591
0
      cr->actual_num_seg1_blocks = 0;
592
0
      cr->actual_num_seg2_blocks = 0;
593
0
    }
594
0
    return;
595
0
  } else {
596
0
    cr->counter_encode_maxq_scene_change++;
597
0
    const double q = av1_convert_qindex_to_q(cm->quant_params.base_qindex,
598
0
                                             cm->seq_params->bit_depth);
599
    // Set rate threshold to some multiple (set to 2 for now) of the target
600
    // rate (target is given by sb64_target_rate and scaled by 256).
601
0
    cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
602
    // Distortion threshold, quadratic in Q, scale factor to be adjusted.
603
    // q will not exceed 457, so (q * q) is within 32bit; see:
604
    // av1_convert_qindex_to_q(), av1_ac_quant(), ac_qlookup*[].
605
0
    cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
606
    // For low-resoln or lower speeds, the rate/dist thresholds need to be
607
    // tuned/updated.
608
0
    if (cpi->oxcf.speed <= 7 || (cm->width * cm->height < 640 * 360)) {
609
0
      cr->thresh_dist_sb = 0;
610
0
      cr->thresh_rate_sb = INT64_MAX;
611
0
    }
612
    // Set up segmentation.
613
0
    av1_enable_segmentation(&cm->seg);
614
0
    if (!cpi->active_map.enabled) {
615
      // Clear down the segment map, only if active_maps is not enabled.
616
0
      av1_clearall_segfeatures(seg);
617
0
    }
618
619
    // Note: setting temporal_update has no effect, as the seg-map coding method
620
    // (temporal or spatial) is determined in
621
    // av1_choose_segmap_coding_method(),
622
    // based on the coding cost of each method. For error_resilient mode on the
623
    // last_frame_seg_map is set to 0, so if temporal coding is used, it is
624
    // relative to 0 previous map.
625
    // seg->temporal_update = 0;
626
627
    // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
628
0
    av1_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
629
    // Use segment BOOST1 for in-frame Q adjustment.
630
0
    av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
631
    // Use segment BOOST2 for more aggressive in-frame Q adjustment.
632
0
    av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
633
634
    // Set the q delta for segment BOOST1.
635
0
    const CommonQuantParams *const quant_params = &cm->quant_params;
636
0
    int qindex_delta =
637
0
        compute_deltaq(cpi, quant_params->base_qindex, cr->rate_ratio_qdelta);
638
0
    cr->qindex_delta[1] = qindex_delta;
639
640
    // Compute rd-mult for segment BOOST1.
641
0
    const int qindex2 = clamp(
642
0
        quant_params->base_qindex + quant_params->y_dc_delta_q + qindex_delta,
643
0
        0, MAXQ);
644
0
    cr->rdmult = av1_compute_rd_mult(
645
0
        qindex2, cm->seq_params->bit_depth,
646
0
        cpi->ppi->gf_group.update_type[cpi->gf_frame_index], layer_depth,
647
0
        boost_index, frame_type, cpi->oxcf.q_cfg.use_fixed_qp_offsets,
648
0
        is_stat_consumption_stage(cpi), cpi->oxcf.tune_cfg.tuning);
649
650
0
    av1_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
651
652
    // Set a more aggressive (higher) q delta for segment BOOST2.
653
0
    qindex_delta = compute_deltaq(
654
0
        cpi, quant_params->base_qindex,
655
0
        AOMMIN(CR_MAX_RATE_TARGET_RATIO,
656
0
               0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta));
657
0
    cr->qindex_delta[2] = qindex_delta;
658
0
    av1_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
659
660
    // Update the segmentation and refresh map.
661
0
    cyclic_refresh_update_map(cpi);
662
0
  }
663
0
}
664
665
0
int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
666
0
  return cr->rdmult;
667
0
}
668
669
0
int av1_cyclic_refresh_disable_lf_cdef(AV1_COMP *const cpi) {
670
0
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
671
0
  const int qindex = cpi->common.quant_params.base_qindex;
672
0
  if (cpi->active_map.enabled &&
673
0
      cpi->rc.percent_blocks_inactive >
674
0
          cpi->sf.rt_sf.thresh_active_maps_skip_lf_cdef)
675
0
    return 1;
676
0
  if (cpi->rc.frames_since_key > 30 && cr->percent_refresh > 0 &&
677
0
      cr->counter_encode_maxq_scene_change > 300 / cr->percent_refresh &&
678
0
      cpi->rc.frame_source_sad < 1000 &&
679
0
      qindex < 7 * (cpi->rc.worst_quality >> 3))
680
0
    return 1;
681
  // More aggressive skip.
682
0
  else if (cpi->sf.rt_sf.skip_lf_screen > 1 && !cpi->rc.high_source_sad &&
683
0
           cpi->rc.frame_source_sad < 50000 && qindex < cpi->rc.worst_quality)
684
0
    return 1;
685
0
  return 0;
686
0
}