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