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