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