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/seg_common.h"
16 #include "av1/encoder/aq_cyclicrefresh.h"
17 #include "av1/encoder/ratectrl.h"
18 #include "av1/encoder/segmentation.h"
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_ports/system_state.h"
21 
av1_cyclic_refresh_alloc(int mi_rows,int mi_cols)22 CYCLIC_REFRESH *av1_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
23   size_t last_coded_q_map_size;
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   if (cr->map == NULL) {
29     av1_cyclic_refresh_free(cr);
30     return NULL;
31   }
32   last_coded_q_map_size = mi_rows * mi_cols * sizeof(*cr->last_coded_q_map);
33   cr->last_coded_q_map = aom_malloc(last_coded_q_map_size);
34   if (cr->last_coded_q_map == NULL) {
35     av1_cyclic_refresh_free(cr);
36     return NULL;
37   }
38   assert(MAXQ <= 255);
39   memset(cr->last_coded_q_map, MAXQ, last_coded_q_map_size);
40   cr->avg_frame_low_motion = 0.0;
41   return cr;
42 }
43 
av1_cyclic_refresh_free(CYCLIC_REFRESH * cr)44 void av1_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
45   if (cr != NULL) {
46     aom_free(cr->map);
47     aom_free(cr->last_coded_q_map);
48     aom_free(cr);
49   }
50 }
51 
52 // Check if this coding block, of size bsize, should be considered for refresh
53 // (lower-qp coding). Decision can be based on various factors, such as
54 // size of the coding block (i.e., below min_block size rejected), coding
55 // mode, and rate/distortion.
candidate_refresh_aq(const CYCLIC_REFRESH * cr,const MB_MODE_INFO * mbmi,int64_t rate,int64_t dist,int bsize)56 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
57                                 const MB_MODE_INFO *mbmi, int64_t rate,
58                                 int64_t dist, int bsize) {
59   MV mv = mbmi->mv[0].as_mv;
60   // Reject the block for lower-qp coding if projected distortion
61   // is above the threshold, and any of the following is true:
62   // 1) mode uses large mv
63   // 2) mode is an intra-mode
64   // Otherwise accept for refresh.
65   if (dist > cr->thresh_dist_sb &&
66       (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
67        mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
68        !is_inter_block(mbmi)))
69     return CR_SEGMENT_ID_BASE;
70   else if (bsize >= BLOCK_16X16 && rate < cr->thresh_rate_sb &&
71            is_inter_block(mbmi) && mbmi->mv[0].as_int == 0 &&
72            cr->rate_boost_fac > 10)
73     // More aggressive delta-q for bigger blocks with zero motion.
74     return CR_SEGMENT_ID_BOOST2;
75   else
76     return CR_SEGMENT_ID_BOOST1;
77 }
78 
79 // Compute delta-q for the segment.
compute_deltaq(const AV1_COMP * cpi,int q,double rate_factor)80 static int compute_deltaq(const AV1_COMP *cpi, int q, double rate_factor) {
81   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
82   const RATE_CONTROL *const rc = &cpi->rc;
83   int deltaq = av1_compute_qdelta_by_rate(
84       rc, cpi->common.current_frame.frame_type, q, rate_factor,
85       cpi->is_screen_content_type, cpi->common.seq_params.bit_depth);
86   if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
87     deltaq = -cr->max_qdelta_perc * q / 100;
88   }
89   return deltaq;
90 }
91 
av1_cyclic_refresh_estimate_bits_at_q(const AV1_COMP * cpi,double correction_factor)92 int av1_cyclic_refresh_estimate_bits_at_q(const AV1_COMP *cpi,
93                                           double correction_factor) {
94   const AV1_COMMON *const cm = &cpi->common;
95   const FRAME_TYPE frame_type = cm->current_frame.frame_type;
96   const int base_qindex = cm->quant_params.base_qindex;
97   const int bit_depth = cm->seq_params.bit_depth;
98   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
99   const int mbs = cm->mi_params.MBs;
100   const int num4x4bl = mbs << 4;
101   // Weight for non-base segments: use actual number of blocks refreshed in
102   // previous/just encoded frame. Note number of blocks here is in 4x4 units.
103   const double weight_segment1 = (double)cr->actual_num_seg1_blocks / num4x4bl;
104   const double weight_segment2 = (double)cr->actual_num_seg2_blocks / num4x4bl;
105   // Take segment weighted average for estimated bits.
106   const int estimated_bits =
107       (int)((1.0 - weight_segment1 - weight_segment2) *
108                 av1_estimate_bits_at_q(frame_type, base_qindex, mbs,
109                                        correction_factor, bit_depth,
110                                        cpi->is_screen_content_type) +
111             weight_segment1 * av1_estimate_bits_at_q(
112                                   frame_type, base_qindex + cr->qindex_delta[1],
113                                   mbs, correction_factor, bit_depth,
114                                   cpi->is_screen_content_type) +
115             weight_segment2 * av1_estimate_bits_at_q(
116                                   frame_type, base_qindex + cr->qindex_delta[2],
117                                   mbs, correction_factor, bit_depth,
118                                   cpi->is_screen_content_type));
119   return estimated_bits;
120 }
121 
av1_cyclic_refresh_rc_bits_per_mb(const AV1_COMP * cpi,int i,double correction_factor)122 int av1_cyclic_refresh_rc_bits_per_mb(const AV1_COMP *cpi, int i,
123                                       double correction_factor) {
124   const AV1_COMMON *const cm = &cpi->common;
125   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
126   int bits_per_mb;
127   int num4x4bl = cm->mi_params.MBs << 4;
128   // Weight for segment prior to encoding: take the average of the target
129   // number for the frame to be encoded and the actual from the previous frame.
130   double weight_segment =
131       (double)((cr->target_num_seg_blocks + cr->actual_num_seg1_blocks +
132                 cr->actual_num_seg2_blocks) >>
133                1) /
134       num4x4bl;
135   // Compute delta-q corresponding to qindex i.
136   int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
137   // Take segment weighted average for bits per mb.
138   bits_per_mb =
139       (int)((1.0 - weight_segment) *
140                 av1_rc_bits_per_mb(cm->current_frame.frame_type, i,
141                                    correction_factor, cm->seq_params.bit_depth,
142                                    cpi->is_screen_content_type) +
143             weight_segment * av1_rc_bits_per_mb(cm->current_frame.frame_type,
144                                                 i + deltaq, correction_factor,
145                                                 cm->seq_params.bit_depth,
146                                                 cpi->is_screen_content_type));
147   return bits_per_mb;
148 }
149 
av1_cyclic_refresh_update_segment(const AV1_COMP * cpi,MB_MODE_INFO * const mbmi,int mi_row,int mi_col,BLOCK_SIZE bsize,int64_t rate,int64_t dist,int skip)150 void av1_cyclic_refresh_update_segment(const AV1_COMP *cpi,
151                                        MB_MODE_INFO *const mbmi, int mi_row,
152                                        int mi_col, BLOCK_SIZE bsize,
153                                        int64_t rate, int64_t dist, int skip) {
154   const AV1_COMMON *const cm = &cpi->common;
155   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
156   const int bw = mi_size_wide[bsize];
157   const int bh = mi_size_high[bsize];
158   const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
159   const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
160   const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
161   const int refresh_this_block =
162       candidate_refresh_aq(cr, mbmi, rate, dist, bsize);
163   // Default is to not update the refresh map.
164   int new_map_value = cr->map[block_index];
165 
166   // If this block is labeled for refresh, check if we should reset the
167   // segment_id.
168   if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
169     mbmi->segment_id = refresh_this_block;
170     // Reset segment_id if will be skipped.
171     if (skip) mbmi->segment_id = CR_SEGMENT_ID_BASE;
172   }
173 
174   // Update the cyclic refresh map, to be used for setting segmentation map
175   // for the next frame. If the block  will be refreshed this frame, mark it
176   // as clean. The magnitude of the -ve influences how long before we consider
177   // it for refresh again.
178   if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
179     new_map_value = -cr->time_for_refresh;
180   } else if (refresh_this_block) {
181     // Else if it is accepted as candidate for refresh, and has not already
182     // been refreshed (marked as 1) then mark it as a candidate for cleanup
183     // for future time (marked as 0), otherwise don't update it.
184     if (cr->map[block_index] == 1) new_map_value = 0;
185   } else {
186     // Leave it marked as block that is not candidate for refresh.
187     new_map_value = 1;
188   }
189 
190   // Update entries in the cyclic refresh map with new_map_value, and
191   // copy mbmi->segment_id into global segmentation map.
192   for (int y = 0; y < ymis; y++)
193     for (int x = 0; x < xmis; x++) {
194       int map_offset = block_index + y * cm->mi_params.mi_cols + x;
195       cr->map[map_offset] = new_map_value;
196       cpi->enc_seg.map[map_offset] = mbmi->segment_id;
197     }
198 }
199 
av1_cyclic_refresh_postencode(AV1_COMP * const cpi)200 void av1_cyclic_refresh_postencode(AV1_COMP *const cpi) {
201   AV1_COMMON *const cm = &cpi->common;
202   const CommonModeInfoParams *const mi_params = &cm->mi_params;
203   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
204   unsigned char *const seg_map = cpi->enc_seg.map;
205   cr->cnt_zeromv = 0;
206   cr->actual_num_seg1_blocks = 0;
207   cr->actual_num_seg2_blocks = 0;
208   for (int mi_row = 0; mi_row < mi_params->mi_rows; mi_row++) {
209     for (int mi_col = 0; mi_col < mi_params->mi_cols; mi_col++) {
210       MB_MODE_INFO **mi =
211           mi_params->mi_grid_base + mi_row * mi_params->mi_stride + mi_col;
212       MV mv = mi[0]->mv[0].as_mv;
213       if (cm->seg.enabled) {
214         int map_index = mi_row * mi_params->mi_cols + mi_col;
215         if (cyclic_refresh_segment_id(seg_map[map_index]) ==
216             CR_SEGMENT_ID_BOOST1)
217           cr->actual_num_seg1_blocks++;
218         else if (cyclic_refresh_segment_id(seg_map[map_index]) ==
219                  CR_SEGMENT_ID_BOOST2)
220           cr->actual_num_seg2_blocks++;
221       }
222       // Accumulate low_content_frame.
223       if (is_inter_block(mi[0]) && abs(mv.row) < 16 && abs(mv.col) < 16)
224         cr->cnt_zeromv++;
225     }
226   }
227   cr->cnt_zeromv =
228       100 * cr->cnt_zeromv / (mi_params->mi_rows * mi_params->mi_cols);
229   cr->avg_frame_low_motion =
230       (3 * cr->avg_frame_low_motion + (double)cr->cnt_zeromv) / 4;
231 }
232 
av1_cyclic_refresh_set_golden_update(AV1_COMP * const cpi)233 void av1_cyclic_refresh_set_golden_update(AV1_COMP *const cpi) {
234   RATE_CONTROL *const rc = &cpi->rc;
235   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
236   // Set minimum gf_interval for GF update to a multiple of the refresh period,
237   // with some max limit. Depending on past encoding stats, GF flag may be
238   // reset and update may not occur until next baseline_gf_interval.
239   if (cr->percent_refresh > 0)
240     rc->baseline_gf_interval = AOMMIN(2 * (100 / cr->percent_refresh), 40);
241   else
242     rc->baseline_gf_interval = 20;
243   if (cr->avg_frame_low_motion < 40) rc->baseline_gf_interval = 8;
244 }
245 
246 // Update the segmentation map, and related quantities: cyclic refresh map,
247 // refresh sb_index, and target number of blocks to be refreshed.
248 // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
249 // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
250 // Blocks labeled as BOOST1 may later get set to BOOST2 (during the
251 // encoding of the superblock).
cyclic_refresh_update_map(AV1_COMP * const cpi)252 static void cyclic_refresh_update_map(AV1_COMP *const cpi) {
253   AV1_COMMON *const cm = &cpi->common;
254   const CommonModeInfoParams *const mi_params = &cm->mi_params;
255   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
256   unsigned char *const seg_map = cpi->enc_seg.map;
257   int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
258   int xmis, ymis, x, y;
259   memset(seg_map, CR_SEGMENT_ID_BASE, mi_params->mi_rows * mi_params->mi_cols);
260   sb_cols = (mi_params->mi_cols + cm->seq_params.mib_size - 1) /
261             cm->seq_params.mib_size;
262   sb_rows = (mi_params->mi_rows + cm->seq_params.mib_size - 1) /
263             cm->seq_params.mib_size;
264   sbs_in_frame = sb_cols * sb_rows;
265   // Number of target blocks to get the q delta (segment 1).
266   block_count =
267       cr->percent_refresh * mi_params->mi_rows * mi_params->mi_cols / 100;
268   // Set the segmentation map: cycle through the superblocks, starting at
269   // cr->mb_index, and stopping when either block_count blocks have been found
270   // to be refreshed, or we have passed through whole frame.
271   if (cr->sb_index >= sbs_in_frame) cr->sb_index = 0;
272   assert(cr->sb_index < sbs_in_frame);
273   i = cr->sb_index;
274   cr->target_num_seg_blocks = 0;
275   do {
276     int sum_map = 0;
277     // Get the mi_row/mi_col corresponding to superblock index i.
278     int sb_row_index = (i / sb_cols);
279     int sb_col_index = i - sb_row_index * sb_cols;
280     int mi_row = sb_row_index * cm->seq_params.mib_size;
281     int mi_col = sb_col_index * cm->seq_params.mib_size;
282     // TODO(any): Ensure the population of
283     // cpi->common.features.allow_screen_content_tools and use the same instead
284     // of cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN
285     int qindex_thresh = cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN
286                             ? av1_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2,
287                                              cm->quant_params.base_qindex)
288                             : 0;
289     assert(mi_row >= 0 && mi_row < mi_params->mi_rows);
290     assert(mi_col >= 0 && mi_col < mi_params->mi_cols);
291     bl_index = mi_row * mi_params->mi_cols + mi_col;
292     // Loop through all MI blocks in superblock and update map.
293     xmis = AOMMIN(mi_params->mi_cols - mi_col, cm->seq_params.mib_size);
294     ymis = AOMMIN(mi_params->mi_rows - mi_row, cm->seq_params.mib_size);
295     for (y = 0; y < ymis; y++) {
296       for (x = 0; x < xmis; x++) {
297         const int bl_index2 = bl_index + y * mi_params->mi_cols + x;
298         // If the block is as a candidate for clean up then mark it
299         // for possible boost/refresh (segment 1). The segment id may get
300         // reset to 0 later if block gets coded anything other than GLOBALMV.
301         if (cr->map[bl_index2] == 0) {
302           if (cr->last_coded_q_map[bl_index2] > qindex_thresh) sum_map++;
303         } else if (cr->map[bl_index2] < 0) {
304           cr->map[bl_index2]++;
305         }
306       }
307     }
308     // Enforce constant segment over superblock.
309     // If segment is at least half of superblock, set to 1.
310     if (sum_map >= xmis * ymis / 2) {
311       for (y = 0; y < ymis; y++)
312         for (x = 0; x < xmis; x++) {
313           seg_map[bl_index + y * mi_params->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
314         }
315       cr->target_num_seg_blocks += xmis * ymis;
316     }
317     i++;
318     if (i == sbs_in_frame) {
319       i = 0;
320     }
321   } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
322   cr->sb_index = i;
323 }
324 
325 // Set cyclic refresh parameters.
av1_cyclic_refresh_update_parameters(AV1_COMP * const cpi)326 void av1_cyclic_refresh_update_parameters(AV1_COMP *const cpi) {
327   // TODO(marpan): Parameters need to be tuned.
328   const RATE_CONTROL *const rc = &cpi->rc;
329   const AV1_COMMON *const cm = &cpi->common;
330   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
331   int num4x4bl = cm->mi_params.MBs << 4;
332   int target_refresh = 0;
333   double weight_segment_target = 0;
334   double weight_segment = 0;
335   int qp_thresh = AOMMIN(20, rc->best_quality << 1);
336   int qp_max_thresh = 118 * MAXQ >> 7;
337   cr->apply_cyclic_refresh = 1;
338   if (frame_is_intra_only(cm) || is_lossless_requested(&cpi->oxcf.rc_cfg) ||
339       cpi->svc.temporal_layer_id > 0 ||
340       rc->avg_frame_qindex[INTER_FRAME] < qp_thresh ||
341       (rc->frames_since_key > 20 &&
342        rc->avg_frame_qindex[INTER_FRAME] > qp_max_thresh) ||
343       (cr->avg_frame_low_motion < 45 && rc->frames_since_key > 40)) {
344     cr->apply_cyclic_refresh = 0;
345     return;
346   }
347   cr->percent_refresh = 10;
348   cr->max_qdelta_perc = 60;
349   cr->time_for_refresh = 0;
350   cr->motion_thresh = 32;
351   cr->rate_boost_fac = 15;
352   // Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
353   // periods of the refresh cycle, after a key frame.
354   // Account for larger interval on base layer for temporal layers.
355   if (cr->percent_refresh > 0 &&
356       rc->frames_since_key < 400 / cr->percent_refresh) {
357     cr->rate_ratio_qdelta = 3.0;
358   } else {
359     cr->rate_ratio_qdelta = 2.0;
360   }
361   // Adjust some parameters for low resolutions.
362   if (cm->width * cm->height <= 352 * 288) {
363     if (rc->avg_frame_bandwidth < 3000) {
364       cr->motion_thresh = 16;
365       cr->rate_boost_fac = 13;
366     } else {
367       cr->max_qdelta_perc = 70;
368       cr->rate_ratio_qdelta = AOMMAX(cr->rate_ratio_qdelta, 2.5);
369     }
370   }
371   if (cpi->oxcf.rc_cfg.mode == AOM_VBR) {
372     // To be adjusted for VBR mode, e.g., based on gf period and boost.
373     // For now use smaller qp-delta (than CBR), no second boosted seg, and
374     // turn-off (no refresh) on golden refresh (since it's already boosted).
375     cr->percent_refresh = 10;
376     cr->rate_ratio_qdelta = 1.5;
377     cr->rate_boost_fac = 10;
378     if (cpi->refresh_frame.golden_frame) {
379       cr->percent_refresh = 0;
380       cr->rate_ratio_qdelta = 1.0;
381     }
382   }
383   // Weight for segment prior to encoding: take the average of the target
384   // number for the frame to be encoded and the actual from the previous frame.
385   // Use the target if its less. To be used for setting the base qp for the
386   // frame in av1_rc_regulate_q.
387   target_refresh =
388       cr->percent_refresh * cm->mi_params.mi_rows * cm->mi_params.mi_cols / 100;
389   weight_segment_target = (double)(target_refresh) / num4x4bl;
390   weight_segment = (double)((target_refresh + cr->actual_num_seg1_blocks +
391                              cr->actual_num_seg2_blocks) >>
392                             1) /
393                    num4x4bl;
394   if (weight_segment_target < 7 * weight_segment / 8)
395     weight_segment = weight_segment_target;
396   cr->weight_segment = weight_segment;
397 }
398 
399 // Setup cyclic background refresh: set delta q and segmentation map.
av1_cyclic_refresh_setup(AV1_COMP * const cpi)400 void av1_cyclic_refresh_setup(AV1_COMP *const cpi) {
401   AV1_COMMON *const cm = &cpi->common;
402   const RATE_CONTROL *const rc = &cpi->rc;
403   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
404   struct segmentation *const seg = &cm->seg;
405   int resolution_change =
406       cm->prev_frame && (cm->width != cm->prev_frame->width ||
407                          cm->height != cm->prev_frame->height);
408   if (resolution_change) av1_cyclic_refresh_reset_resize(cpi);
409   if (cm->current_frame.frame_number == 0) cr->low_content_avg = 0.0;
410   if (!cr->apply_cyclic_refresh) {
411     // Set segmentation map to 0 and disable.
412     unsigned char *const seg_map = cpi->enc_seg.map;
413     memset(seg_map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
414     av1_disable_segmentation(&cm->seg);
415     if (cm->current_frame.frame_type == KEY_FRAME) {
416       memset(cr->last_coded_q_map, MAXQ,
417              cm->mi_params.mi_rows * cm->mi_params.mi_cols *
418                  sizeof(*cr->last_coded_q_map));
419       cr->sb_index = 0;
420     }
421     return;
422   } else {
423     const double q = av1_convert_qindex_to_q(cm->quant_params.base_qindex,
424                                              cm->seq_params.bit_depth);
425     aom_clear_system_state();
426     // Set rate threshold to some multiple (set to 2 for now) of the target
427     // rate (target is given by sb64_target_rate and scaled by 256).
428     cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
429     // Distortion threshold, quadratic in Q, scale factor to be adjusted.
430     // q will not exceed 457, so (q * q) is within 32bit; see:
431     // av1_convert_qindex_to_q(), av1_ac_quant(), ac_qlookup*[].
432     cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
433 
434     // Set up segmentation.
435     // Clear down the segment map.
436     av1_enable_segmentation(&cm->seg);
437     av1_clearall_segfeatures(seg);
438 
439     // Note: setting temporal_update has no effect, as the seg-map coding method
440     // (temporal or spatial) is determined in
441     // av1_choose_segmap_coding_method(),
442     // based on the coding cost of each method. For error_resilient mode on the
443     // last_frame_seg_map is set to 0, so if temporal coding is used, it is
444     // relative to 0 previous map.
445     // seg->temporal_update = 0;
446 
447     // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
448     av1_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
449     // Use segment BOOST1 for in-frame Q adjustment.
450     av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
451     // Use segment BOOST2 for more aggressive in-frame Q adjustment.
452     av1_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
453 
454     // Set the q delta for segment BOOST1.
455     const CommonQuantParams *const quant_params = &cm->quant_params;
456     int qindex_delta =
457         compute_deltaq(cpi, quant_params->base_qindex, cr->rate_ratio_qdelta);
458     cr->qindex_delta[1] = qindex_delta;
459 
460     // Compute rd-mult for segment BOOST1.
461     const int qindex2 = clamp(
462         quant_params->base_qindex + quant_params->y_dc_delta_q + qindex_delta,
463         0, MAXQ);
464     cr->rdmult = av1_compute_rd_mult(cpi, qindex2);
465 
466     av1_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
467 
468     // Set a more aggressive (higher) q delta for segment BOOST2.
469     qindex_delta = compute_deltaq(
470         cpi, quant_params->base_qindex,
471         AOMMIN(CR_MAX_RATE_TARGET_RATIO,
472                0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta));
473     cr->qindex_delta[2] = qindex_delta;
474     av1_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
475 
476     // Update the segmentation and refresh map.
477     cyclic_refresh_update_map(cpi);
478   }
479 }
480 
av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH * cr)481 int av1_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
482   return cr->rdmult;
483 }
484 
av1_cyclic_refresh_reset_resize(AV1_COMP * const cpi)485 void av1_cyclic_refresh_reset_resize(AV1_COMP *const cpi) {
486   const AV1_COMMON *const cm = &cpi->common;
487   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
488   memset(cr->map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols);
489   cr->sb_index = 0;
490   cpi->refresh_frame.golden_frame = true;
491   cr->apply_cyclic_refresh = 0;
492 }
493