xref: /aosp_15_r20/external/libaom/av1/encoder/aq_cyclicrefresh.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 
av1_cyclic_refresh_alloc(int mi_rows,int mi_cols)24 CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
25   CYCLIC_REFRESH *const cr = aom_calloc(1, sizeof(*cr));
26   if (cr == NULL) return NULL;
27 
28   cr->map = aom_calloc(mi_rows * mi_cols, sizeof(*cr->map));
29   cr->counter_encode_maxq_scene_change = 0;
30   cr->percent_refresh_adjustment = 5;
31   cr->rate_ratio_qdelta_adjustment = 0.25;
32   if (cr->map == NULL) {
33     av1_cyclic_refresh_free(cr);
34     return NULL;
35   }
36   return cr;
37 }
38 
av1_cyclic_refresh_free(CYCLIC_REFRESH * cr)39 void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
40   if (cr != NULL) {
41     aom_free(cr->map);
42     aom_free(cr);
43   }
44 }
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.
candidate_refresh_aq(const CYCLIC_REFRESH * cr,const MB_MODE_INFO * mbmi,int64_t rate,int64_t dist,BLOCK_SIZE bsize,int noise_level)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                                 int noise_level) {
54   MV mv = mbmi->mv[0].as_mv;
55   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   if (!is_compound && dist > cr->thresh_dist_sb &&
63       (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
64        mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
65        !is_inter_block(mbmi)))
66     return CR_SEGMENT_ID_BASE;
67   else if ((is_compound && noise_level < kMedium) ||
68            (bsize >= BLOCK_16X16 && rate < cr->thresh_rate_sb &&
69             is_inter_block(mbmi) && mbmi->mv[0].as_int == 0 &&
70             cr->rate_boost_fac > 10))
71     // More aggressive delta-q for bigger blocks with zero motion.
72     return CR_SEGMENT_ID_BOOST2;
73   else
74     return CR_SEGMENT_ID_BOOST1;
75 }
76 
77 // Compute delta-q for the segment.
compute_deltaq(const AV1_COMP * cpi,int q,double rate_factor)78 static int compute_deltaq(const AV1_COMP *cpi, int q, double rate_factor) {
79   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
80   int deltaq = av1_compute_qdelta_by_rate(
81       cpi, cpi->common.current_frame.frame_type, q, rate_factor);
82   if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
83     deltaq = -cr->max_qdelta_perc * q / 100;
84   }
85   return deltaq;
86 }
87 
av1_cyclic_refresh_estimate_bits_at_q(const AV1_COMP * cpi,double correction_factor)88 int av1_cyclic_refresh_estimate_bits_at_q(const AV1_COMP *cpi,
89                                           double correction_factor) {
90   const AV1_COMMON *const cm = &cpi->common;
91   const int base_qindex = cm->quant_params.base_qindex;
92   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
93   const int mbs = cm->mi_params.MBs;
94   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   double weight_segment1 = (double)cr->actual_num_seg1_blocks / num4x4bl;
98   double weight_segment2 = (double)cr->actual_num_seg2_blocks / num4x4bl;
99   if (cpi->rc.rtc_external_ratectrl) {
100     weight_segment1 = (double)(cr->percent_refresh * cm->mi_params.mi_rows *
101                                cm->mi_params.mi_cols / 100) /
102                       num4x4bl;
103     weight_segment2 = 0;
104   }
105   // Take segment weighted average for estimated bits.
106   const int estimated_bits = (int)round(
107       (1.0 - weight_segment1 - weight_segment2) *
108           av1_estimate_bits_at_q(cpi, base_qindex, correction_factor) +
109       weight_segment1 *
110           av1_estimate_bits_at_q(cpi, base_qindex + cr->qindex_delta[1],
111                                  correction_factor) +
112       weight_segment2 *
113           av1_estimate_bits_at_q(cpi, base_qindex + cr->qindex_delta[2],
114                                  correction_factor));
115   return estimated_bits;
116 }
117 
av1_cyclic_refresh_rc_bits_per_mb(const AV1_COMP * cpi,int i,double correction_factor)118 int av1_cyclic_refresh_rc_bits_per_mb(const AV1_COMP *cpi, int i,
119                                       double correction_factor) {
120   const AV1_COMMON *const cm = &cpi->common;
121   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
122   int bits_per_mb;
123   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   double weight_segment =
127       (double)((cr->target_num_seg_blocks + cr->actual_num_seg1_blocks +
128                 cr->actual_num_seg2_blocks) >>
129                1) /
130       num4x4bl;
131   if (cpi->rc.rtc_external_ratectrl) {
132     weight_segment = (double)((cr->target_num_seg_blocks +
133                                cr->percent_refresh * cm->mi_params.mi_rows *
134                                    cm->mi_params.mi_cols / 100) >>
135                               1) /
136                      num4x4bl;
137   }
138   // Compute delta-q corresponding to qindex i.
139   int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
140   const int accurate_estimate = cpi->sf.hl_sf.accurate_bit_estimate;
141   // Take segment weighted average for bits per mb.
142   bits_per_mb = (int)round(
143       (1.0 - weight_segment) *
144           av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type, i,
145                              correction_factor, accurate_estimate) +
146       weight_segment * av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type,
147                                           i + deltaq, correction_factor,
148                                           accurate_estimate));
149   return bits_per_mb;
150 }
151 
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)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                                    RUN_TYPE dry_run) {
155   int cdf_num;
156   const AV1_COMMON *const cm = &cpi->common;
157   MACROBLOCKD *const xd = &x->e_mbd;
158   MB_MODE_INFO *const mbmi = xd->mi[0];
159   const int prev_segment_id = mbmi->segment_id;
160   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
161   const int bw = mi_size_wide[bsize];
162   const int bh = mi_size_high[bsize];
163   const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
164   const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
165 
166   assert(cm->seg.enabled);
167 
168   if (!cr->skip_over4x4) {
169     mbmi->segment_id =
170         av1_get_spatial_seg_pred(cm, xd, &cdf_num, cr->skip_over4x4);
171     if (prev_segment_id != mbmi->segment_id) {
172       const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
173       const int mi_stride = cm->mi_params.mi_cols;
174       const uint8_t segment_id = mbmi->segment_id;
175       for (int mi_y = 0; mi_y < ymis; mi_y++) {
176         const int map_offset = block_index + mi_y * mi_stride;
177         memset(&cr->map[map_offset], 0, xmis);
178         memset(&cpi->enc_seg.map[map_offset], segment_id, xmis);
179         memset(&cm->cur_frame->seg_map[map_offset], segment_id, xmis);
180       }
181     }
182   }
183   if (!dry_run) {
184     if (cyclic_refresh_segment_id(prev_segment_id) == CR_SEGMENT_ID_BOOST1)
185       x->actual_num_seg1_blocks -= xmis * ymis;
186     else if (cyclic_refresh_segment_id(prev_segment_id) == CR_SEGMENT_ID_BOOST2)
187       x->actual_num_seg2_blocks -= xmis * ymis;
188   }
189 }
190 
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)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                                        RUN_TYPE dry_run) {
195   const AV1_COMMON *const cm = &cpi->common;
196   MACROBLOCKD *const xd = &x->e_mbd;
197   MB_MODE_INFO *const mbmi = xd->mi[0];
198   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
199   const int bw = mi_size_wide[bsize];
200   const int bh = mi_size_high[bsize];
201   const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
202   const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
203   const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
204   int noise_level = 0;
205   if (cpi->noise_estimate.enabled) noise_level = cpi->noise_estimate.level;
206   const int refresh_this_block =
207       candidate_refresh_aq(cr, mbmi, rate, dist, bsize, noise_level);
208   int sh = cpi->cyclic_refresh->skip_over4x4 ? 2 : 1;
209   // Default is to not update the refresh map.
210   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   if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
215     mbmi->segment_id = refresh_this_block;
216     // Reset segment_id if will be skipped.
217     if (skip) mbmi->segment_id = CR_SEGMENT_ID_BASE;
218   }
219   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   if (cyclic_refresh_segment_id_boosted(segment_id)) {
226     new_map_value = -cr->time_for_refresh;
227   } 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     if (cr->map[block_index] == 1) new_map_value = 0;
232   } else {
233     // Leave it marked as block that is not candidate for refresh.
234     new_map_value = 1;
235   }
236 
237   // Update entries in the cyclic refresh map with new_map_value, and
238   // copy mbmi->segment_id into global segmentation map.
239   const int mi_stride = cm->mi_params.mi_cols;
240   for (int mi_y = 0; mi_y < ymis; mi_y += sh) {
241     const int map_offset = block_index + mi_y * mi_stride;
242     memset(&cr->map[map_offset], new_map_value, xmis);
243     memset(&cpi->enc_seg.map[map_offset], segment_id, xmis);
244     memset(&cm->cur_frame->seg_map[map_offset], segment_id, xmis);
245   }
246 
247   // Accumulate cyclic refresh update counters.
248   if (!dry_run) {
249     if (cyclic_refresh_segment_id(segment_id) == CR_SEGMENT_ID_BOOST1)
250       x->actual_num_seg1_blocks += xmis * ymis;
251     else if (cyclic_refresh_segment_id(segment_id) == CR_SEGMENT_ID_BOOST2)
252       x->actual_num_seg2_blocks += xmis * ymis;
253   }
254 }
255 
256 // Initializes counters used for cyclic refresh.
av1_init_cyclic_refresh_counters(MACROBLOCK * const x)257 void av1_init_cyclic_refresh_counters(MACROBLOCK *const x) {
258   x->actual_num_seg1_blocks = 0;
259   x->actual_num_seg2_blocks = 0;
260 }
261 
262 // Accumulate cyclic refresh counters.
av1_accumulate_cyclic_refresh_counters(CYCLIC_REFRESH * const cyclic_refresh,const MACROBLOCK * const x)263 void av1_accumulate_cyclic_refresh_counters(
264     CYCLIC_REFRESH *const cyclic_refresh, const MACROBLOCK *const x) {
265   cyclic_refresh->actual_num_seg1_blocks += x->actual_num_seg1_blocks;
266   cyclic_refresh->actual_num_seg2_blocks += x->actual_num_seg2_blocks;
267 }
268 
av1_cyclic_refresh_set_golden_update(AV1_COMP * const cpi)269 void av1_cyclic_refresh_set_golden_update(AV1_COMP *const cpi) {
270   RATE_CONTROL *const rc = &cpi->rc;
271   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
272   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   const int gf_length_mult[2] = { 8, 4 };
277   if (cr->percent_refresh > 0)
278     p_rc->baseline_gf_interval =
279         AOMMIN(gf_length_mult[cpi->sf.rt_sf.gf_length_lvl] *
280                    (100 / cr->percent_refresh),
281                MAX_GF_INTERVAL_RT);
282   else
283     p_rc->baseline_gf_interval = FIXED_GF_INTERVAL_RT;
284   if (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 40)
285     p_rc->baseline_gf_interval = 16;
286 }
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).
cyclic_refresh_update_map(AV1_COMP * const cpi)294 static void cyclic_refresh_update_map(AV1_COMP *const cpi) {
295   AV1_COMMON *const cm = &cpi->common;
296   const CommonModeInfoParams *const mi_params = &cm->mi_params;
297   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
298   unsigned char *const seg_map = cpi->enc_seg.map;
299   unsigned char *const active_map_4x4 = cpi->active_map.map;
300   int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
301   int xmis, ymis, x, y;
302   uint64_t sb_sad = 0;
303   uint64_t thresh_sad_low = 0;
304   uint64_t thresh_sad = INT64_MAX;
305   const int mi_rows = mi_params->mi_rows, mi_cols = mi_params->mi_cols;
306   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   if (!cpi->active_map.enabled) {
311     memset(seg_map, CR_SEGMENT_ID_BASE, mi_rows * mi_cols);
312   }
313   sb_cols = (mi_cols + cm->seq_params->mib_size - 1) / cm->seq_params->mib_size;
314   sb_rows = (mi_rows + cm->seq_params->mib_size - 1) / cm->seq_params->mib_size;
315   sbs_in_frame = sb_cols * sb_rows;
316   // Number of target blocks to get the q delta (segment 1).
317   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   if (cr->sb_index >= sbs_in_frame) cr->sb_index = 0;
322   assert(cr->sb_index < sbs_in_frame);
323   i = cr->sb_index;
324   cr->last_sb_index = cr->sb_index;
325   cr->target_num_seg_blocks = 0;
326   do {
327     int sum_map = 0;
328     // Get the mi_row/mi_col corresponding to superblock index i.
329     int sb_row_index = (i / sb_cols);
330     int sb_col_index = i - sb_row_index * sb_cols;
331     int mi_row = sb_row_index * cm->seq_params->mib_size;
332     int mi_col = sb_col_index * cm->seq_params->mib_size;
333     assert(mi_row >= 0 && mi_row < mi_rows);
334     assert(mi_col >= 0 && mi_col < mi_cols);
335     bl_index = mi_row * mi_stride + mi_col;
336     // Loop through all MI blocks in superblock and update map.
337     xmis = AOMMIN(mi_cols - mi_col, cm->seq_params->mib_size);
338     ymis = AOMMIN(mi_rows - mi_row, cm->seq_params->mib_size);
339     if (cr->use_block_sad_scene_det && cpi->rc.frames_since_key > 30 &&
340         cr->counter_encode_maxq_scene_change > 30 &&
341         cpi->src_sad_blk_64x64 != NULL &&
342         cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
343       sb_sad = cpi->src_sad_blk_64x64[sb_col_index + sb_cols * sb_row_index];
344       int scale = (cm->width * cm->height < 640 * 360) ? 6 : 8;
345       int scale_low = 2;
346       thresh_sad = (scale * 64 * 64);
347       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       if (cpi->svc.number_temporal_layers > 1 &&
354           cpi->svc.temporal_layer_id == 0) {
355         thresh_sad <<= 4;
356         thresh_sad_low <<= 2;
357       }
358     }
359     // cr_map only needed at 8x8 blocks.
360     for (y = 0; y < ymis; y += 2) {
361       for (x = 0; x < xmis; x += 2) {
362         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         if ((cr->map[bl_index2] == 0 || sb_sad < thresh_sad_low) &&
369             (!cpi->active_map.enabled ||
370              active_map_4x4[bl_index2] == AM_SEGMENT_ID_ACTIVE)) {
371           sum_map += 4;
372         } else if (cr->map[bl_index2] < 0) {
373           cr->map[bl_index2]++;
374         }
375       }
376     }
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     if (sum_map >= (xmis * ymis) >> 1 && sb_sad < thresh_sad) {
381       set_segment_id(seg_map, bl_index, xmis, ymis, mi_stride,
382                      CR_SEGMENT_ID_BOOST1);
383       cr->target_num_seg_blocks += xmis * ymis;
384     }
385     i++;
386     if (i == sbs_in_frame) {
387       i = 0;
388     }
389   } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
390   cr->sb_index = i;
391   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     if (!cpi->active_map.enabled) av1_disable_segmentation(&cm->seg);
395   }
396 }
397 
is_scene_change_detected(AV1_COMP * const cpi)398 static int is_scene_change_detected(AV1_COMP *const cpi) {
399   return cpi->rc.high_source_sad;
400 }
401 
402 // Set cyclic refresh parameters.
av1_cyclic_refresh_update_parameters(AV1_COMP * const cpi)403 void av1_cyclic_refresh_update_parameters(AV1_COMP *const cpi) {
404   // TODO(marpan): Parameters need to be tuned.
405   const RATE_CONTROL *const rc = &cpi->rc;
406   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
407   const AV1_COMMON *const cm = &cpi->common;
408   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
409   SVC *const svc = &cpi->svc;
410   const int qp_thresh = AOMMAX(16, rc->best_quality + 4);
411   const int qp_max_thresh = 118 * MAXQ >> 7;
412   const int scene_change_detected = is_scene_change_detected(cpi);
413   const int is_screen_content =
414       (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   const int frames_since_scene_change =
418       (cpi->ppi->use_svc || !is_screen_content)
419           ? cpi->rc.frames_since_key
420           : AOMMIN(cpi->rc.frames_since_key,
421                    cr->counter_encode_maxq_scene_change);
422 
423   // Cases to reset the cyclic refresh adjustment parameters.
424   if (frame_is_intra_only(cm) || scene_change_detected ||
425       cpi->ppi->rtc_ref.bias_recovery_frame) {
426     // Reset adaptive elements for intra only frames and scene changes.
427     cr->percent_refresh_adjustment = 5;
428     cr->rate_ratio_qdelta_adjustment = 0.25;
429   }
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   cr->skip_over4x4 = (cpi->oxcf.speed > 9) ? 1 : 0;
438 
439   // should we enable cyclic refresh on this frame.
440   cr->apply_cyclic_refresh = 1;
441   if (frame_is_intra_only(cm) || is_lossless_requested(&cpi->oxcf.rc_cfg) ||
442       cpi->rc.high_motion_content_screen_rtc || scene_change_detected ||
443       svc->temporal_layer_id > 0 ||
444       svc->prev_number_spatial_layers != svc->number_spatial_layers ||
445       p_rc->avg_frame_qindex[INTER_FRAME] < qp_thresh ||
446       (svc->number_spatial_layers > 1 &&
447        svc->layer_context[svc->temporal_layer_id].is_key_frame) ||
448       (frames_since_scene_change > 20 &&
449        p_rc->avg_frame_qindex[INTER_FRAME] > qp_max_thresh) ||
450       (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 30 &&
451        frames_since_scene_change > 40) ||
452       cpi->ppi->rtc_ref.bias_recovery_frame) {
453     cr->apply_cyclic_refresh = 0;
454     return;
455   }
456 
457   // Increase the amount of refresh for #temporal_layers > 2
458   if (svc->number_temporal_layers > 2)
459     cr->percent_refresh = 15;
460   else
461     cr->percent_refresh = 10 + cr->percent_refresh_adjustment;
462 
463   if (cpi->active_map.enabled) {
464     // Scale down the percent_refresh to target the active blocks only.
465     cr->percent_refresh =
466         cr->percent_refresh * (100 - cpi->rc.percent_blocks_inactive) / 100;
467     if (cr->percent_refresh == 0) {
468       cr->apply_cyclic_refresh = 0;
469     }
470   }
471 
472   cr->max_qdelta_perc = 60;
473   cr->time_for_refresh = 0;
474   cr->use_block_sad_scene_det =
475       (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
476        cm->seq_params->sb_size == BLOCK_64X64)
477           ? 1
478           : 0;
479   cr->motion_thresh = 32;
480   cr->rate_boost_fac =
481       (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   if (cr->percent_refresh > 0) {
489     if (cpi->ppi->use_svc || !is_screen_content) {
490       if (frames_since_scene_change <
491           ((4 * svc->number_temporal_layers) * (100 / cr->percent_refresh))) {
492         cr->rate_ratio_qdelta = 3.0 + cr->rate_ratio_qdelta_adjustment;
493       } else {
494         cr->rate_ratio_qdelta = 2.25 + cr->rate_ratio_qdelta_adjustment;
495       }
496     } else {
497       double distance_from_sc_factor =
498           AOMMIN(0.75, (int)(frames_since_scene_change / 10) * 0.1);
499       cr->rate_ratio_qdelta =
500           3.0 + cr->rate_ratio_qdelta_adjustment - distance_from_sc_factor;
501       if ((frames_since_scene_change < 10) &&
502           ((cpi->rc.rc_1_frame < 0) || (cpi->rc.rc_2_frame < 0))) {
503         cr->rate_ratio_qdelta -= 0.25;
504       }
505     }
506   } else {
507     cr->rate_ratio_qdelta = 2.25 + cr->rate_ratio_qdelta_adjustment;
508   }
509   // Adjust some parameters for low resolutions.
510   if (cm->width * cm->height <= 352 * 288) {
511     if (cpi->svc.number_temporal_layers > 1) {
512       cr->motion_thresh = 32;
513       cr->rate_boost_fac = 13;
514     } else {
515       if (rc->avg_frame_bandwidth < 3000) {
516         cr->motion_thresh = 16;
517         cr->rate_boost_fac = 13;
518       } else {
519         cr->max_qdelta_perc = 50;
520         cr->rate_ratio_qdelta = AOMMAX(cr->rate_ratio_qdelta, 2.0);
521       }
522     }
523   }
524   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     cr->percent_refresh = 10;
529     cr->rate_ratio_qdelta = 1.5;
530     cr->rate_boost_fac = 10;
531     if (cpi->refresh_frame.golden_frame) {
532       cr->percent_refresh = 0;
533       cr->rate_ratio_qdelta = 1.0;
534     }
535   }
536   if (rc->rtc_external_ratectrl) {
537     cr->actual_num_seg1_blocks = cr->percent_refresh * cm->mi_params.mi_rows *
538                                  cm->mi_params.mi_cols / 100;
539     cr->actual_num_seg2_blocks = 0;
540   }
541 }
542 
cyclic_refresh_reset_resize(AV1_COMP * const cpi)543 static void cyclic_refresh_reset_resize(AV1_COMP *const cpi) {
544   const AV1_COMMON *const cm = &cpi->common;
545   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
546   memset(cr->map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
547   cr->sb_index = 0;
548   cr->last_sb_index = 0;
549   cpi->refresh_frame.golden_frame = true;
550   cr->apply_cyclic_refresh = 0;
551   cr->counter_encode_maxq_scene_change = 0;
552   cr->percent_refresh_adjustment = 5;
553   cr->rate_ratio_qdelta_adjustment = 0.25;
554 }
555 
556 // Setup cyclic background refresh: set delta q and segmentation map.
av1_cyclic_refresh_setup(AV1_COMP * const cpi)557 void av1_cyclic_refresh_setup(AV1_COMP *const cpi) {
558   AV1_COMMON *const cm = &cpi->common;
559   const RATE_CONTROL *const rc = &cpi->rc;
560   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
561   struct segmentation *const seg = &cm->seg;
562   const int scene_change_detected = is_scene_change_detected(cpi);
563   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
564   const int boost_index = AOMMIN(15, (cpi->ppi->p_rc.gfu_boost / 100));
565   const int layer_depth = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
566   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   const int resolution_change =
571       cm->prev_frame &&
572       (cm->width != cm->prev_frame->width ||
573        cm->height != cm->prev_frame->height) &&
574       cpi->svc.prev_number_spatial_layers == cpi->svc.number_spatial_layers;
575 
576   if (resolution_change) cyclic_refresh_reset_resize(cpi);
577   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     if (!cpi->active_map.enabled || cpi->rc.percent_blocks_inactive == 100) {
582       unsigned char *const seg_map = cpi->enc_seg.map;
583       memset(seg_map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
584       av1_disable_segmentation(&cm->seg);
585     }
586     if (frame_is_intra_only(cm) || scene_change_detected ||
587         cpi->ppi->rtc_ref.bias_recovery_frame) {
588       cr->sb_index = 0;
589       cr->last_sb_index = 0;
590       cr->counter_encode_maxq_scene_change = 0;
591       cr->actual_num_seg1_blocks = 0;
592       cr->actual_num_seg2_blocks = 0;
593     }
594     return;
595   } else {
596     cr->counter_encode_maxq_scene_change++;
597     const double q = av1_convert_qindex_to_q(cm->quant_params.base_qindex,
598                                              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     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     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     if (cpi->oxcf.speed <= 7 || (cm->width * cm->height < 640 * 360)) {
609       cr->thresh_dist_sb = 0;
610       cr->thresh_rate_sb = INT64_MAX;
611     }
612     // Set up segmentation.
613     av1_enable_segmentation(&cm->seg);
614     if (!cpi->active_map.enabled) {
615       // Clear down the segment map, only if active_maps is not enabled.
616       av1_clearall_segfeatures(seg);
617     }
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     av1_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
629     // Use segment BOOST1 for in-frame Q adjustment.
630     av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
631     // Use segment BOOST2 for more aggressive in-frame Q adjustment.
632     av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
633 
634     // Set the q delta for segment BOOST1.
635     const CommonQuantParams *const quant_params = &cm->quant_params;
636     int qindex_delta =
637         compute_deltaq(cpi, quant_params->base_qindex, cr->rate_ratio_qdelta);
638     cr->qindex_delta[1] = qindex_delta;
639 
640     // Compute rd-mult for segment BOOST1.
641     const int qindex2 = clamp(
642         quant_params->base_qindex + quant_params->y_dc_delta_q + qindex_delta,
643         0, MAXQ);
644     cr->rdmult = av1_compute_rd_mult(
645         qindex2, cm->seq_params->bit_depth,
646         cpi->ppi->gf_group.update_type[cpi->gf_frame_index], layer_depth,
647         boost_index, frame_type, cpi->oxcf.q_cfg.use_fixed_qp_offsets,
648         is_stat_consumption_stage(cpi));
649 
650     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     qindex_delta = compute_deltaq(
654         cpi, quant_params->base_qindex,
655         AOMMIN(CR_MAX_RATE_TARGET_RATIO,
656                0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta));
657     cr->qindex_delta[2] = qindex_delta;
658     av1_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
659 
660     // Update the segmentation and refresh map.
661     cyclic_refresh_update_map(cpi);
662   }
663 }
664 
av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH * cr)665 int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
666   return cr->rdmult;
667 }
668 
av1_cyclic_refresh_disable_lf_cdef(AV1_COMP * const cpi)669 int av1_cyclic_refresh_disable_lf_cdef(AV1_COMP *const cpi) {
670   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
671   const int qindex = cpi->common.quant_params.base_qindex;
672   if (cpi->active_map.enabled &&
673       cpi->rc.percent_blocks_inactive >
674           cpi->sf.rt_sf.thresh_active_maps_skip_lf_cdef)
675     return 1;
676   if (cpi->rc.frames_since_key > 30 && cr->percent_refresh > 0 &&
677       cr->counter_encode_maxq_scene_change > 300 / cr->percent_refresh &&
678       cpi->rc.frame_source_sad < 1000 &&
679       qindex < 7 * (cpi->rc.worst_quality >> 3))
680     return 1;
681   // More aggressive skip.
682   else if (cpi->sf.rt_sf.skip_lf_screen > 1 && !cpi->rc.high_source_sad &&
683            cpi->rc.frame_source_sad < 50000 && qindex < cpi->rc.worst_quality)
684     return 1;
685   return 0;
686 }
687