]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_aq_cyclicrefresh.c
Add dynamic resize logic for 1 pass CBR.
[libvpx] / vp9 / encoder / vp9_aq_cyclicrefresh.c
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13
14 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
15
16 #include "vp9/common/vp9_seg_common.h"
17
18 #include "vp9/encoder/vp9_ratectrl.h"
19 #include "vp9/encoder/vp9_segmentation.h"
20
21 struct CYCLIC_REFRESH {
22   // Percentage of blocks per frame that are targeted as candidates
23   // for cyclic refresh.
24   int percent_refresh;
25   // Maximum q-delta as percentage of base q.
26   int max_qdelta_perc;
27   // Superblock starting index for cycling through the frame.
28   int sb_index;
29   // Controls how long block will need to wait to be refreshed again, in
30   // excess of the cycle time, i.e., in the case of all zero motion, block
31   // will be refreshed every (100/percent_refresh + time_for_refresh) frames.
32   int time_for_refresh;
33   // Target number of (8x8) blocks that are set for delta-q.
34   int target_num_seg_blocks;
35   // Actual number of (8x8) blocks that were applied delta-q.
36   int actual_num_seg1_blocks;
37   int actual_num_seg2_blocks;
38   // RD mult. parameters for segment 1.
39   int rdmult;
40   // Cyclic refresh map.
41   signed char *map;
42   // Thresholds applied to the projected rate/distortion of the coding block,
43   // when deciding whether block should be refreshed.
44   int64_t thresh_rate_sb;
45   int64_t thresh_dist_sb;
46   // Threshold applied to the motion vector (in units of 1/8 pel) of the
47   // coding block, when deciding whether block should be refreshed.
48   int16_t motion_thresh;
49   // Rate target ratio to set q delta.
50   double rate_ratio_qdelta;
51   // Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
52   double rate_boost_fac;
53   double low_content_avg;
54   int qindex_delta_seg1;
55   int qindex_delta_seg2;
56 };
57
58 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
59   CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
60   if (cr == NULL)
61     return NULL;
62
63   cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
64   if (cr->map == NULL) {
65     vpx_free(cr);
66     return NULL;
67   }
68
69   return cr;
70 }
71
72 void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
73   vpx_free(cr->map);
74   vpx_free(cr);
75 }
76
77 // Check if we should turn off cyclic refresh based on bitrate condition.
78 static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
79                                         const RATE_CONTROL *rc) {
80   // Turn off cyclic refresh if bits available per frame is not sufficiently
81   // larger than bit cost of segmentation. Segment map bit cost should scale
82   // with number of seg blocks, so compare available bits to number of blocks.
83   // Average bits available per frame = avg_frame_bandwidth
84   // Number of (8x8) blocks in frame = mi_rows * mi_cols;
85   const float factor = 0.25;
86   const int number_blocks = cm->mi_rows  * cm->mi_cols;
87   // The condition below corresponds to turning off at target bitrates:
88   // (at 30fps), ~12kbps for CIF, 36kbps for VGA, 100kps for HD/720p.
89   // Also turn off at very small frame sizes, to avoid too large fraction of
90   // superblocks to be refreshed per frame. Threshold below is less than QCIF.
91   if (rc->avg_frame_bandwidth < factor * number_blocks ||
92       number_blocks / 64 < 5)
93     return 0;
94   else
95     return 1;
96 }
97
98 // Check if this coding block, of size bsize, should be considered for refresh
99 // (lower-qp coding). Decision can be based on various factors, such as
100 // size of the coding block (i.e., below min_block size rejected), coding
101 // mode, and rate/distortion.
102 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
103                                 const MB_MODE_INFO *mbmi,
104                                 int64_t rate,
105                                 int64_t dist,
106                                 int bsize) {
107   MV mv = mbmi->mv[0].as_mv;
108   // Reject the block for lower-qp coding if projected distortion
109   // is above the threshold, and any of the following is true:
110   // 1) mode uses large mv
111   // 2) mode is an intra-mode
112   // Otherwise accept for refresh.
113   if (dist > cr->thresh_dist_sb &&
114       (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
115        mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
116        !is_inter_block(mbmi)))
117     return CR_SEGMENT_ID_BASE;
118   else  if (bsize >= BLOCK_16X16 &&
119             rate < cr->thresh_rate_sb &&
120             is_inter_block(mbmi) &&
121             mbmi->mv[0].as_int == 0)
122     // More aggressive delta-q for bigger blocks with zero motion.
123     return CR_SEGMENT_ID_BOOST2;
124   else
125     return CR_SEGMENT_ID_BOOST1;
126 }
127
128 // Compute delta-q for the segment.
129 static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) {
130   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
131   const RATE_CONTROL *const rc = &cpi->rc;
132   int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type,
133                                           q, rate_factor,
134                                           cpi->common.bit_depth);
135   if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
136     deltaq = -cr->max_qdelta_perc * q / 100;
137   }
138   return deltaq;
139 }
140
141 // For the just encoded frame, estimate the bits, incorporating the delta-q
142 // from non-base segment. For now ignore effect of multiple segments
143 // (with different delta-q). Note this function is called in the postencode
144 // (called from rc_update_rate_correction_factors()).
145 int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi,
146                                           double correction_factor) {
147   const VP9_COMMON *const cm = &cpi->common;
148   const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
149   int estimated_bits;
150   int mbs = cm->MBs;
151   int num8x8bl = mbs << 2;
152   // Weight for non-base segments: use actual number of blocks refreshed in
153   // previous/just encoded frame. Note number of blocks here is in 8x8 units.
154   double weight_segment1 = (double)cr->actual_num_seg1_blocks / num8x8bl;
155   double weight_segment2 = (double)cr->actual_num_seg2_blocks / num8x8bl;
156   // Take segment weighted average for estimated bits.
157   estimated_bits = (int)((1.0 - weight_segment1 - weight_segment2) *
158       vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs,
159                              correction_factor, cm->bit_depth) +
160                              weight_segment1 *
161       vp9_estimate_bits_at_q(cm->frame_type,
162                              cm->base_qindex + cr->qindex_delta_seg1, mbs,
163                              correction_factor, cm->bit_depth) +
164                              weight_segment2 *
165       vp9_estimate_bits_at_q(cm->frame_type,
166                              cm->base_qindex + cr->qindex_delta_seg2, mbs,
167                              correction_factor, cm->bit_depth));
168   return estimated_bits;
169 }
170
171 // Prior to encoding the frame, estimate the bits per mb, for a given q = i and
172 // a corresponding delta-q (for segment 1). This function is called in the
173 // rc_regulate_q() to set the base qp index.
174 // Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or
175 // to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding.
176 int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i,
177                                       double correction_factor) {
178   const VP9_COMMON *const cm = &cpi->common;
179   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
180   int bits_per_mb;
181   int num8x8bl = cm->MBs << 2;
182   // Weight for segment prior to encoding: take the average of the target
183   // number for the frame to be encoded and the actual from the previous frame.
184   double weight_segment = (double)((cr->target_num_seg_blocks +
185       cr->actual_num_seg1_blocks + cr->actual_num_seg2_blocks) >> 1) /
186       num8x8bl;
187   // Compute delta-q corresponding to qindex i.
188   int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
189   // Take segment weighted average for bits per mb.
190   bits_per_mb = (int)((1.0 - weight_segment) *
191       vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) +
192       weight_segment *
193       vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor,
194                          cm->bit_depth));
195   return bits_per_mb;
196 }
197
198 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
199 // check if we should reset the segment_id, and update the cyclic_refresh map
200 // and segmentation map.
201 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi,
202                                        MB_MODE_INFO *const mbmi,
203                                        int mi_row, int mi_col,
204                                        BLOCK_SIZE bsize,
205                                        int64_t rate,
206                                        int64_t dist,
207                                        int skip) {
208   const VP9_COMMON *const cm = &cpi->common;
209   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
210   const int bw = num_8x8_blocks_wide_lookup[bsize];
211   const int bh = num_8x8_blocks_high_lookup[bsize];
212   const int xmis = MIN(cm->mi_cols - mi_col, bw);
213   const int ymis = MIN(cm->mi_rows - mi_row, bh);
214   const int block_index = mi_row * cm->mi_cols + mi_col;
215   const int refresh_this_block = candidate_refresh_aq(cr, mbmi, rate, dist,
216                                                       bsize);
217   // Default is to not update the refresh map.
218   int new_map_value = cr->map[block_index];
219   int x = 0; int y = 0;
220
221   // If this block is labeled for refresh, check if we should reset the
222   // segment_id.
223   if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
224     mbmi->segment_id = refresh_this_block;
225     // Reset segment_id if will be skipped.
226     if (skip)
227       mbmi->segment_id = CR_SEGMENT_ID_BASE;
228   }
229
230   // Update the cyclic refresh map, to be used for setting segmentation map
231   // for the next frame. If the block  will be refreshed this frame, mark it
232   // as clean. The magnitude of the -ve influences how long before we consider
233   // it for refresh again.
234   if (cyclic_refresh_segment_id_boosted(mbmi->segment_id)) {
235     new_map_value = -cr->time_for_refresh;
236   } else if (refresh_this_block) {
237     // Else if it is accepted as candidate for refresh, and has not already
238     // been refreshed (marked as 1) then mark it as a candidate for cleanup
239     // for future time (marked as 0), otherwise don't update it.
240     if (cr->map[block_index] == 1)
241       new_map_value = 0;
242   } else {
243     // Leave it marked as block that is not candidate for refresh.
244     new_map_value = 1;
245   }
246
247   // Update entries in the cyclic refresh map with new_map_value, and
248   // copy mbmi->segment_id into global segmentation map.
249   for (y = 0; y < ymis; y++)
250     for (x = 0; x < xmis; x++) {
251       cr->map[block_index + y * cm->mi_cols + x] = new_map_value;
252       cpi->segmentation_map[block_index + y * cm->mi_cols + x] =
253           mbmi->segment_id;
254     }
255 }
256
257 // Update the actual number of blocks that were applied the segment delta q.
258 void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) {
259   VP9_COMMON *const cm = &cpi->common;
260   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
261   unsigned char *const seg_map = cpi->segmentation_map;
262   int mi_row, mi_col;
263   cr->actual_num_seg1_blocks = 0;
264   cr->actual_num_seg2_blocks = 0;
265   for (mi_row = 0; mi_row < cm->mi_rows; mi_row++)
266     for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
267       if (cyclic_refresh_segment_id(
268           seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST1)
269         cr->actual_num_seg1_blocks++;
270       else if (cyclic_refresh_segment_id(
271           seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST2)
272         cr->actual_num_seg2_blocks++;
273     }
274 }
275
276 // Set golden frame update interval, for non-svc 1 pass CBR mode.
277 void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) {
278   RATE_CONTROL *const rc = &cpi->rc;
279   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
280   // Set minimum gf_interval for GF update to a multiple (== 2) of refresh
281   // period. Depending on past encoding stats, GF flag may be reset and update
282   // may not occur until next baseline_gf_interval.
283   if (cr->percent_refresh > 0)
284     rc->baseline_gf_interval = 4 * (100 / cr->percent_refresh);
285   else
286     rc->baseline_gf_interval = 40;
287 }
288
289 // Update some encoding stats (from the just encoded frame). If this frame's
290 // background has high motion, refresh the golden frame. Otherwise, if the
291 // golden reference is to be updated check if we should NOT update the golden
292 // ref.
293 void vp9_cyclic_refresh_check_golden_update(VP9_COMP *const cpi) {
294   VP9_COMMON *const cm = &cpi->common;
295   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
296   int mi_row, mi_col;
297   double fraction_low = 0.0;
298   int low_content_frame = 0;
299
300   MODE_INFO **mi = cm->mi_grid_visible;
301   RATE_CONTROL *const rc = &cpi->rc;
302   const int rows = cm->mi_rows, cols = cm->mi_cols;
303   int cnt1 = 0, cnt2 = 0;
304   int force_gf_refresh = 0;
305
306   for (mi_row = 0; mi_row < rows; mi_row++) {
307     for (mi_col = 0; mi_col < cols; mi_col++) {
308       int16_t abs_mvr = mi[0]->mbmi.mv[0].as_mv.row >= 0 ?
309           mi[0]->mbmi.mv[0].as_mv.row : -1 * mi[0]->mbmi.mv[0].as_mv.row;
310       int16_t abs_mvc = mi[0]->mbmi.mv[0].as_mv.col >= 0 ?
311           mi[0]->mbmi.mv[0].as_mv.col : -1 * mi[0]->mbmi.mv[0].as_mv.col;
312
313       // Calculate the motion of the background.
314       if (abs_mvr <= 16 && abs_mvc <= 16) {
315         cnt1++;
316         if (abs_mvr == 0 && abs_mvc == 0)
317           cnt2++;
318       }
319       mi++;
320
321       // Accumulate low_content_frame.
322       if (cr->map[mi_row * cols + mi_col] < 1)
323         low_content_frame++;
324     }
325     mi += 8;
326   }
327
328   // For video conference clips, if the background has high motion in current
329   // frame because of the camera movement, set this frame as the golden frame.
330   // Use 70% and 5% as the thresholds for golden frame refreshing.
331   if (cnt1 * 10 > (70 * rows * cols) && cnt2 * 20 < cnt1) {
332     vp9_cyclic_refresh_set_golden_update(cpi);
333     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
334
335     if (rc->frames_till_gf_update_due > rc->frames_to_key)
336       rc->frames_till_gf_update_due = rc->frames_to_key;
337     cpi->refresh_golden_frame = 1;
338     force_gf_refresh = 1;
339   }
340
341   fraction_low =
342       (double)low_content_frame / (rows * cols);
343   // Update average.
344   cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4;
345   if (!force_gf_refresh && cpi->refresh_golden_frame == 1) {
346     // Don't update golden reference if the amount of low_content for the
347     // current encoded frame is small, or if the recursive average of the
348     // low_content over the update interval window falls below threshold.
349     if (fraction_low < 0.8 || cr->low_content_avg < 0.7)
350       cpi->refresh_golden_frame = 0;
351     // Reset for next internal.
352     cr->low_content_avg = fraction_low;
353   }
354 }
355
356 // Update the segmentation map, and related quantities: cyclic refresh map,
357 // refresh sb_index, and target number of blocks to be refreshed.
358 // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
359 // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
360 // Blocks labeled as BOOST1 may later get set to BOOST2 (during the
361 // encoding of the superblock).
362 static void cyclic_refresh_update_map(VP9_COMP *const cpi) {
363   VP9_COMMON *const cm = &cpi->common;
364   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
365   unsigned char *const seg_map = cpi->segmentation_map;
366   int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
367   int xmis, ymis, x, y;
368   memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols);
369   sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
370   sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
371   sbs_in_frame = sb_cols * sb_rows;
372   // Number of target blocks to get the q delta (segment 1).
373   block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
374   // Set the segmentation map: cycle through the superblocks, starting at
375   // cr->mb_index, and stopping when either block_count blocks have been found
376   // to be refreshed, or we have passed through whole frame.
377   assert(cr->sb_index < sbs_in_frame);
378   i = cr->sb_index;
379   cr->target_num_seg_blocks = 0;
380   do {
381     int sum_map = 0;
382     // Get the mi_row/mi_col corresponding to superblock index i.
383     int sb_row_index = (i / sb_cols);
384     int sb_col_index = i - sb_row_index * sb_cols;
385     int mi_row = sb_row_index * MI_BLOCK_SIZE;
386     int mi_col = sb_col_index * MI_BLOCK_SIZE;
387     assert(mi_row >= 0 && mi_row < cm->mi_rows);
388     assert(mi_col >= 0 && mi_col < cm->mi_cols);
389     bl_index = mi_row * cm->mi_cols + mi_col;
390     // Loop through all 8x8 blocks in superblock and update map.
391     xmis = MIN(cm->mi_cols - mi_col,
392                num_8x8_blocks_wide_lookup[BLOCK_64X64]);
393     ymis = MIN(cm->mi_rows - mi_row,
394                num_8x8_blocks_high_lookup[BLOCK_64X64]);
395     for (y = 0; y < ymis; y++) {
396       for (x = 0; x < xmis; x++) {
397         const int bl_index2 = bl_index + y * cm->mi_cols + x;
398         // If the block is as a candidate for clean up then mark it
399         // for possible boost/refresh (segment 1). The segment id may get
400         // reset to 0 later if block gets coded anything other than ZEROMV.
401         if (cr->map[bl_index2] == 0) {
402           sum_map++;
403         } else if (cr->map[bl_index2] < 0) {
404           cr->map[bl_index2]++;
405         }
406       }
407     }
408     // Enforce constant segment over superblock.
409     // If segment is at least half of superblock, set to 1.
410     if (sum_map >= xmis * ymis / 2) {
411       for (y = 0; y < ymis; y++)
412         for (x = 0; x < xmis; x++) {
413           seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
414         }
415       cr->target_num_seg_blocks += xmis * ymis;
416     }
417     i++;
418     if (i == sbs_in_frame) {
419       i = 0;
420     }
421   } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
422   cr->sb_index = i;
423 }
424
425 // Set cyclic refresh parameters.
426 void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) {
427   const RATE_CONTROL *const rc = &cpi->rc;
428   const VP9_COMMON *const cm = &cpi->common;
429   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
430   cr->percent_refresh = 10;
431   cr->max_qdelta_perc = 50;
432   cr->time_for_refresh = 0;
433   // Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
434   // periods of the refresh cycle, after a key frame.
435   if (rc->frames_since_key <  4 * cr->percent_refresh)
436     cr->rate_ratio_qdelta = 3.0;
437   else
438     cr->rate_ratio_qdelta = 2.0;
439   // Adjust some parameters for low resolutions at low bitrates.
440   if (cm->width <= 352 &&
441       cm->height <= 288 &&
442       rc->avg_frame_bandwidth < 3400) {
443     cr->motion_thresh = 4;
444     cr->rate_boost_fac = 1.25;
445   } else {
446     cr->motion_thresh = 32;
447     cr->rate_boost_fac = 1.7;
448   }
449 }
450
451 // Setup cyclic background refresh: set delta q and segmentation map.
452 void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
453   VP9_COMMON *const cm = &cpi->common;
454   const RATE_CONTROL *const rc = &cpi->rc;
455   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
456   struct segmentation *const seg = &cm->seg;
457   const int apply_cyclic_refresh  = apply_cyclic_refresh_bitrate(cm, rc);
458   if (cm->current_video_frame == 0)
459     cr->low_content_avg = 0.0;
460   // Don't apply refresh on key frame or enhancement layer frames.
461   if (!apply_cyclic_refresh ||
462       (cm->frame_type == KEY_FRAME) ||
463       (cpi->svc.temporal_layer_id > 0) ||
464       (cpi->svc.spatial_layer_id > 0)) {
465     // Set segmentation map to 0 and disable.
466     unsigned char *const seg_map = cpi->segmentation_map;
467     memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
468     vp9_disable_segmentation(&cm->seg);
469     if (cm->frame_type == KEY_FRAME)
470       cr->sb_index = 0;
471     return;
472   } else {
473     int qindex_delta = 0;
474     int qindex2;
475     const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
476     vp9_clear_system_state();
477     // Set rate threshold to some multiple (set to 2 for now) of the target
478     // rate (target is given by sb64_target_rate and scaled by 256).
479     cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
480     // Distortion threshold, quadratic in Q, scale factor to be adjusted.
481     // q will not exceed 457, so (q * q) is within 32bit; see:
482     // vp9_convert_qindex_to_q(), vp9_ac_quant(), ac_qlookup*[].
483     cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
484
485     // Set up segmentation.
486     // Clear down the segment map.
487     vp9_enable_segmentation(&cm->seg);
488     vp9_clearall_segfeatures(seg);
489     // Select delta coding method.
490     seg->abs_delta = SEGMENT_DELTADATA;
491
492     // Note: setting temporal_update has no effect, as the seg-map coding method
493     // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
494     // based on the coding cost of each method. For error_resilient mode on the
495     // last_frame_seg_map is set to 0, so if temporal coding is used, it is
496     // relative to 0 previous map.
497     // seg->temporal_update = 0;
498
499     // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
500     vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
501     // Use segment BOOST1 for in-frame Q adjustment.
502     vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
503     // Use segment BOOST2 for more aggressive in-frame Q adjustment.
504     vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
505
506     // Set the q delta for segment BOOST1.
507     qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
508     cr->qindex_delta_seg1 = qindex_delta;
509
510     // Compute rd-mult for segment BOOST1.
511     qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
512
513     cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);
514
515     vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
516
517     // Set a more aggressive (higher) q delta for segment BOOST2.
518     qindex_delta = compute_deltaq(cpi, cm->base_qindex,
519                                   MIN(CR_MAX_RATE_TARGET_RATIO,
520                                   cr->rate_boost_fac * cr->rate_ratio_qdelta));
521     cr->qindex_delta_seg2 = qindex_delta;
522     vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
523
524     // Update the segmentation and refresh map.
525     cyclic_refresh_update_map(cpi);
526   }
527 }
528
529 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
530   return cr->rdmult;
531 }
532
533 void vp9_cyclic_refresh_reset_resize(VP9_COMP *const cpi) {
534   const VP9_COMMON *const cm = &cpi->common;
535   CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
536   memset(cr->map, 0, cm->mi_rows * cm->mi_cols);
537   cr->sb_index = 0;
538 }