2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
14 #include "vpx_dsp/vpx_dsp_common.h"
15 #include "vpx_ports/system_state.h"
17 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
19 #include "vp9/common/vp9_seg_common.h"
21 #include "vp9/encoder/vp9_ratectrl.h"
22 #include "vp9/encoder/vp9_segmentation.h"
24 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
25 size_t last_coded_q_map_size;
26 size_t consec_zero_mv_size;
27 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
31 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
32 if (cr->map == NULL) {
36 last_coded_q_map_size = mi_rows * mi_cols * sizeof(*cr->last_coded_q_map);
37 cr->last_coded_q_map = vpx_malloc(last_coded_q_map_size);
38 if (cr->last_coded_q_map == NULL) {
43 memset(cr->last_coded_q_map, MAXQ, last_coded_q_map_size);
45 consec_zero_mv_size = mi_rows * mi_cols * sizeof(*cr->consec_zero_mv);
46 cr->consec_zero_mv = vpx_malloc(consec_zero_mv_size);
47 if (cr->consec_zero_mv == NULL) {
51 memset(cr->consec_zero_mv, 0, consec_zero_mv_size);
55 void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
57 vpx_free(cr->last_coded_q_map);
58 vpx_free(cr->consec_zero_mv);
62 // Check if we should turn off cyclic refresh based on bitrate condition.
63 // TODO(marpan): May be better in some cases to just reduce the amount/delta-qp
64 // instead of completely shutting off.
65 static int apply_cyclic_refresh_bitrate(const VP9_COMMON *cm,
66 const RATE_CONTROL *rc) {
67 // Turn off cyclic refresh if bits available per frame is not sufficiently
68 // larger than bit cost of segmentation. Segment map bit cost should scale
69 // with number of seg blocks, so compare available bits to number of blocks.
70 // Average bits available per frame = avg_frame_bandwidth
71 // Number of (8x8) blocks in frame = mi_rows * mi_cols;
72 const float factor = 0.15f;
73 const int number_blocks = cm->mi_rows * cm->mi_cols;
74 // The condition below corresponds to turning off at target bitrates:
75 // (at 30fps), ~8kbps for CIF, 20kbps for VGA, 60kps for HD/720p.
76 // Also turn off at very small frame sizes, to avoid too large fraction of
77 // superblocks to be refreshed per frame. Threshold below is less than QCIF.
78 if (rc->avg_frame_bandwidth < factor * number_blocks ||
79 number_blocks / 64 < 5)
85 // Check if this coding block, of size bsize, should be considered for refresh
86 // (lower-qp coding). Decision can be based on various factors, such as
87 // size of the coding block (i.e., below min_block size rejected), coding
88 // mode, and rate/distortion.
89 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr,
94 MV mv = mi->mv[0].as_mv;
95 // Reject the block for lower-qp coding if projected distortion
96 // is above the threshold, and any of the following is true:
97 // 1) mode uses large mv
98 // 2) mode is an intra-mode
99 // Otherwise accept for refresh.
100 if (dist > cr->thresh_dist_sb &&
101 (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
102 mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
103 !is_inter_block(mi)))
104 return CR_SEGMENT_ID_BASE;
105 else if (bsize >= BLOCK_16X16 &&
106 rate < cr->thresh_rate_sb &&
107 is_inter_block(mi) &&
108 mi->mv[0].as_int == 0 &&
109 cr->rate_boost_fac > 10)
110 // More aggressive delta-q for bigger blocks with zero motion.
111 return CR_SEGMENT_ID_BOOST2;
113 return CR_SEGMENT_ID_BOOST1;
116 // Compute delta-q for the segment.
117 static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) {
118 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
119 const RATE_CONTROL *const rc = &cpi->rc;
120 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type,
122 cpi->common.bit_depth);
123 if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
124 deltaq = -cr->max_qdelta_perc * q / 100;
129 // For the just encoded frame, estimate the bits, incorporating the delta-q
130 // from non-base segment. For now ignore effect of multiple segments
131 // (with different delta-q). Note this function is called in the postencode
132 // (called from rc_update_rate_correction_factors()).
133 int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi,
134 double correction_factor) {
135 const VP9_COMMON *const cm = &cpi->common;
136 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
139 int num8x8bl = mbs << 2;
140 // Weight for non-base segments: use actual number of blocks refreshed in
141 // previous/just encoded frame. Note number of blocks here is in 8x8 units.
142 double weight_segment1 = (double)cr->actual_num_seg1_blocks / num8x8bl;
143 double weight_segment2 = (double)cr->actual_num_seg2_blocks / num8x8bl;
144 // Take segment weighted average for estimated bits.
145 estimated_bits = (int)((1.0 - weight_segment1 - weight_segment2) *
146 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs,
147 correction_factor, cm->bit_depth) +
149 vp9_estimate_bits_at_q(cm->frame_type,
150 cm->base_qindex + cr->qindex_delta[1], mbs,
151 correction_factor, cm->bit_depth) +
153 vp9_estimate_bits_at_q(cm->frame_type,
154 cm->base_qindex + cr->qindex_delta[2], mbs,
155 correction_factor, cm->bit_depth));
156 return estimated_bits;
159 // Prior to encoding the frame, estimate the bits per mb, for a given q = i and
160 // a corresponding delta-q (for segment 1). This function is called in the
161 // rc_regulate_q() to set the base qp index.
162 // Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or
163 // to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding.
164 int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i,
165 double correction_factor) {
166 const VP9_COMMON *const cm = &cpi->common;
167 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
169 int num8x8bl = cm->MBs << 2;
170 // Weight for segment prior to encoding: take the average of the target
171 // number for the frame to be encoded and the actual from the previous frame.
172 int target_refresh = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
173 double weight_segment = (double)((target_refresh +
174 cr->actual_num_seg1_blocks + cr->actual_num_seg2_blocks) >> 1) /
176 // Compute delta-q corresponding to qindex i.
177 int deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
178 // Take segment weighted average for bits per mb.
179 bits_per_mb = (int)((1.0 - weight_segment) *
180 vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, cm->bit_depth) +
182 vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, correction_factor,
187 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
188 // check if we should reset the segment_id, and update the cyclic_refresh map
189 // and segmentation map.
190 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi,
192 int mi_row, int mi_col,
197 struct macroblock_plane *const p) {
198 const VP9_COMMON *const cm = &cpi->common;
199 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
200 const int bw = num_8x8_blocks_wide_lookup[bsize];
201 const int bh = num_8x8_blocks_high_lookup[bsize];
202 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
203 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
204 const int block_index = mi_row * cm->mi_cols + mi_col;
205 int refresh_this_block = candidate_refresh_aq(cr, mi, rate, dist, bsize);
206 // Default is to not update the refresh map.
207 int new_map_value = cr->map[block_index];
208 int x = 0; int y = 0;
211 if (refresh_this_block == 0 &&
212 bsize <= BLOCK_16X16 &&
213 cpi->use_skin_detection) {
214 is_skin = vp9_compute_skin_block(p[0].src.buf,
221 refresh_this_block = 1;
224 // If this block is labeled for refresh, check if we should reset the
226 if (cyclic_refresh_segment_id_boosted(mi->segment_id)) {
227 mi->segment_id = refresh_this_block;
228 // Reset segment_id if it will be skipped.
230 mi->segment_id = CR_SEGMENT_ID_BASE;
233 // Update the cyclic refresh map, to be used for setting segmentation map
234 // for the next frame. If the block will be refreshed this frame, mark it
235 // as clean. The magnitude of the -ve influences how long before we consider
236 // it for refresh again.
237 if (cyclic_refresh_segment_id_boosted(mi->segment_id)) {
238 new_map_value = -cr->time_for_refresh;
239 } else if (refresh_this_block) {
240 // Else if it is accepted as candidate for refresh, and has not already
241 // been refreshed (marked as 1) then mark it as a candidate for cleanup
242 // for future time (marked as 0), otherwise don't update it.
243 if (cr->map[block_index] == 1)
246 // Leave it marked as block that is not candidate for refresh.
250 // Update entries in the cyclic refresh map with new_map_value, and
251 // copy mbmi->segment_id into global segmentation map.
252 for (y = 0; y < ymis; y++)
253 for (x = 0; x < xmis; x++) {
254 int map_offset = block_index + y * cm->mi_cols + x;
255 cr->map[map_offset] = new_map_value;
256 cpi->segmentation_map[map_offset] = mi->segment_id;
260 void vp9_cyclic_refresh_update_sb_postencode(VP9_COMP *const cpi,
261 const MODE_INFO *const mi,
262 int mi_row, int mi_col,
264 const VP9_COMMON *const cm = &cpi->common;
265 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
266 MV mv = mi->mv[0].as_mv;
267 const int bw = num_8x8_blocks_wide_lookup[bsize];
268 const int bh = num_8x8_blocks_high_lookup[bsize];
269 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
270 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
271 const int block_index = mi_row * cm->mi_cols + mi_col;
273 for (y = 0; y < ymis; y++)
274 for (x = 0; x < xmis; x++) {
275 int map_offset = block_index + y * cm->mi_cols + x;
276 // Inter skip blocks were clearly not coded at the current qindex, so
277 // don't update the map for them. For cases where motion is non-zero or
278 // the reference frame isn't the previous frame, the previous value in
279 // the map for this spatial location is not entirely correct.
280 if ((!is_inter_block(mi) || !mi->skip) &&
281 mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
282 cr->last_coded_q_map[map_offset] = clamp(
283 cm->base_qindex + cr->qindex_delta[mi->segment_id], 0, MAXQ);
284 } else if (is_inter_block(mi) && mi->skip &&
285 mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
286 cr->last_coded_q_map[map_offset] = VPXMIN(
287 clamp(cm->base_qindex + cr->qindex_delta[mi->segment_id],
289 cr->last_coded_q_map[map_offset]);
290 // Update the consecutive zero/low_mv count.
291 if (is_inter_block(mi) && (abs(mv.row) < 8 && abs(mv.col) < 8)) {
292 if (cr->consec_zero_mv[map_offset] < 255)
293 cr->consec_zero_mv[map_offset]++;
295 cr->consec_zero_mv[map_offset] = 0;
301 // Update the actual number of blocks that were applied the segment delta q.
302 void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) {
303 VP9_COMMON *const cm = &cpi->common;
304 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
305 unsigned char *const seg_map = cpi->segmentation_map;
307 cr->actual_num_seg1_blocks = 0;
308 cr->actual_num_seg2_blocks = 0;
309 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++)
310 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
311 if (cyclic_refresh_segment_id(
312 seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST1)
313 cr->actual_num_seg1_blocks++;
314 else if (cyclic_refresh_segment_id(
315 seg_map[mi_row * cm->mi_cols + mi_col]) == CR_SEGMENT_ID_BOOST2)
316 cr->actual_num_seg2_blocks++;
320 // Set golden frame update interval, for non-svc 1 pass CBR mode.
321 void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) {
322 RATE_CONTROL *const rc = &cpi->rc;
323 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
324 // Set minimum gf_interval for GF update to a multiple of the refresh period,
325 // with some max limit. Depending on past encoding stats, GF flag may be
326 // reset and update may not occur until next baseline_gf_interval.
327 if (cr->percent_refresh > 0)
328 rc->baseline_gf_interval = VPXMIN(4 * (100 / cr->percent_refresh), 40);
330 rc->baseline_gf_interval = 40;
333 // Update some encoding stats (from the just encoded frame). If this frame's
334 // background has high motion, refresh the golden frame. Otherwise, if the
335 // golden reference is to be updated check if we should NOT update the golden
337 void vp9_cyclic_refresh_check_golden_update(VP9_COMP *const cpi) {
338 VP9_COMMON *const cm = &cpi->common;
339 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
341 double fraction_low = 0.0;
342 int low_content_frame = 0;
344 MODE_INFO **mi = cm->mi_grid_visible;
345 RATE_CONTROL *const rc = &cpi->rc;
346 const int rows = cm->mi_rows, cols = cm->mi_cols;
347 int cnt1 = 0, cnt2 = 0;
348 int force_gf_refresh = 0;
350 for (mi_row = 0; mi_row < rows; mi_row++) {
351 for (mi_col = 0; mi_col < cols; mi_col++) {
352 int16_t abs_mvr = mi[0]->mv[0].as_mv.row >= 0 ?
353 mi[0]->mv[0].as_mv.row : -1 * mi[0]->mv[0].as_mv.row;
354 int16_t abs_mvc = mi[0]->mv[0].as_mv.col >= 0 ?
355 mi[0]->mv[0].as_mv.col : -1 * mi[0]->mv[0].as_mv.col;
357 // Calculate the motion of the background.
358 if (abs_mvr <= 16 && abs_mvc <= 16) {
360 if (abs_mvr == 0 && abs_mvc == 0)
365 // Accumulate low_content_frame.
366 if (cr->map[mi_row * cols + mi_col] < 1)
372 // For video conference clips, if the background has high motion in current
373 // frame because of the camera movement, set this frame as the golden frame.
374 // Use 70% and 5% as the thresholds for golden frame refreshing.
375 // Also, force this frame as a golden update frame if this frame will change
376 // the resolution (resize_pending != 0).
377 if (cpi->resize_pending != 0 ||
378 (cnt1 * 10 > (70 * rows * cols) && cnt2 * 20 < cnt1)) {
379 vp9_cyclic_refresh_set_golden_update(cpi);
380 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
382 if (rc->frames_till_gf_update_due > rc->frames_to_key)
383 rc->frames_till_gf_update_due = rc->frames_to_key;
384 cpi->refresh_golden_frame = 1;
385 force_gf_refresh = 1;
389 (double)low_content_frame / (rows * cols);
391 cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4;
392 if (!force_gf_refresh && cpi->refresh_golden_frame == 1) {
393 // Don't update golden reference if the amount of low_content for the
394 // current encoded frame is small, or if the recursive average of the
395 // low_content over the update interval window falls below threshold.
396 if (fraction_low < 0.8 || cr->low_content_avg < 0.7)
397 cpi->refresh_golden_frame = 0;
398 // Reset for next internal.
399 cr->low_content_avg = fraction_low;
403 // Update the segmentation map, and related quantities: cyclic refresh map,
404 // refresh sb_index, and target number of blocks to be refreshed.
405 // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
406 // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
407 // Blocks labeled as BOOST1 may later get set to BOOST2 (during the
408 // encoding of the superblock).
409 static void cyclic_refresh_update_map(VP9_COMP *const cpi) {
410 VP9_COMMON *const cm = &cpi->common;
411 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
412 unsigned char *const seg_map = cpi->segmentation_map;
413 int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
414 int xmis, ymis, x, y;
415 int consec_zero_mv_thresh = 0;
416 int qindex_thresh = 0;
419 memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols);
420 sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
421 sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
422 sbs_in_frame = sb_cols * sb_rows;
423 // Number of target blocks to get the q delta (segment 1).
424 block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
425 // Set the segmentation map: cycle through the superblocks, starting at
426 // cr->mb_index, and stopping when either block_count blocks have been found
427 // to be refreshed, or we have passed through whole frame.
428 assert(cr->sb_index < sbs_in_frame);
430 cr->target_num_seg_blocks = 0;
431 if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
432 consec_zero_mv_thresh = 100;
433 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium)
434 consec_zero_mv_thresh = 80;
437 cpi->oxcf.content == VP9E_CONTENT_SCREEN
438 ? vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2, cm->base_qindex)
439 : vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST1, cm->base_qindex);
442 // Get the mi_row/mi_col corresponding to superblock index i.
443 int sb_row_index = (i / sb_cols);
444 int sb_col_index = i - sb_row_index * sb_cols;
445 int mi_row = sb_row_index * MI_BLOCK_SIZE;
446 int mi_col = sb_col_index * MI_BLOCK_SIZE;
447 assert(mi_row >= 0 && mi_row < cm->mi_rows);
448 assert(mi_col >= 0 && mi_col < cm->mi_cols);
449 bl_index = mi_row * cm->mi_cols + mi_col;
450 // Loop through all 8x8 blocks in superblock and update map.
452 VPXMIN(cm->mi_cols - mi_col, num_8x8_blocks_wide_lookup[BLOCK_64X64]);
454 VPXMIN(cm->mi_rows - mi_row, num_8x8_blocks_high_lookup[BLOCK_64X64]);
455 for (y = 0; y < ymis; y++) {
456 for (x = 0; x < xmis; x++) {
457 const int bl_index2 = bl_index + y * cm->mi_cols + x;
458 // If the block is as a candidate for clean up then mark it
459 // for possible boost/refresh (segment 1). The segment id may get
460 // reset to 0 later if block gets coded anything other than ZEROMV.
461 if (cr->map[bl_index2] == 0) {
463 if (cr->last_coded_q_map[bl_index2] > qindex_thresh ||
464 cr->consec_zero_mv[bl_index2] < consec_zero_mv_thresh) {
468 } else if (cr->map[bl_index2] < 0) {
469 cr->map[bl_index2]++;
473 // Enforce constant segment over superblock.
474 // If segment is at least half of superblock, set to 1.
475 if (sum_map >= xmis * ymis / 2) {
476 for (y = 0; y < ymis; y++)
477 for (x = 0; x < xmis; x++) {
478 seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
480 cr->target_num_seg_blocks += xmis * ymis;
483 if (i == sbs_in_frame) {
486 } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
488 cr->reduce_refresh = 0;
489 if (count_sel < (3 * count_tot) >> 2)
490 cr->reduce_refresh = 1;
493 // Set cyclic refresh parameters.
494 void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) {
495 const RATE_CONTROL *const rc = &cpi->rc;
496 const VP9_COMMON *const cm = &cpi->common;
497 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
498 cr->percent_refresh = 10;
499 if (cr->reduce_refresh)
500 cr->percent_refresh = 5;
501 cr->max_qdelta_perc = 50;
502 cr->time_for_refresh = 0;
503 // Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
504 // periods of the refresh cycle, after a key frame.
505 // Account for larger interval on base layer for temporal layers.
506 if (cr->percent_refresh > 0 &&
507 rc->frames_since_key < (4 * cpi->svc.number_temporal_layers) *
508 (100 / cr->percent_refresh)) {
509 cr->rate_ratio_qdelta = 3.0;
511 cr->rate_ratio_qdelta = 2.0;
512 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium)
513 // Reduce the delta-qp if the estimated source noise is above threshold.
514 cr->rate_ratio_qdelta = 1.5;
516 // Adjust some parameters for low resolutions at low bitrates.
517 if (cm->width <= 352 &&
519 rc->avg_frame_bandwidth < 3400) {
520 cr->motion_thresh = 4;
521 cr->rate_boost_fac = 10;
523 cr->motion_thresh = 32;
524 cr->rate_boost_fac = 15;
526 if (cpi->svc.spatial_layer_id > 0) {
527 cr->motion_thresh = 4;
528 cr->rate_boost_fac = 12;
532 // Setup cyclic background refresh: set delta q and segmentation map.
533 void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
534 VP9_COMMON *const cm = &cpi->common;
535 const RATE_CONTROL *const rc = &cpi->rc;
536 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
537 struct segmentation *const seg = &cm->seg;
538 const int apply_cyclic_refresh = apply_cyclic_refresh_bitrate(cm, rc);
539 if (cm->current_video_frame == 0)
540 cr->low_content_avg = 0.0;
541 // Don't apply refresh on key frame or temporal enhancement layer frames.
542 if (!apply_cyclic_refresh ||
543 (cm->frame_type == KEY_FRAME) ||
544 (cpi->svc.temporal_layer_id > 0)) {
545 // Set segmentation map to 0 and disable.
546 unsigned char *const seg_map = cpi->segmentation_map;
547 memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
548 vp9_disable_segmentation(&cm->seg);
549 if (cm->frame_type == KEY_FRAME) {
550 memset(cr->last_coded_q_map, MAXQ,
551 cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
552 memset(cr->consec_zero_mv, 0,
553 cm->mi_rows * cm->mi_cols * sizeof(*cr->consec_zero_mv));
558 int qindex_delta = 0;
560 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
561 vpx_clear_system_state();
562 // Set rate threshold to some multiple (set to 2 for now) of the target
563 // rate (target is given by sb64_target_rate and scaled by 256).
564 cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
565 // Distortion threshold, quadratic in Q, scale factor to be adjusted.
566 // q will not exceed 457, so (q * q) is within 32bit; see:
567 // vp9_convert_qindex_to_q(), vp9_ac_quant(), ac_qlookup*[].
568 cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
570 // Set up segmentation.
571 // Clear down the segment map.
572 vp9_enable_segmentation(&cm->seg);
573 vp9_clearall_segfeatures(seg);
574 // Select delta coding method.
575 seg->abs_delta = SEGMENT_DELTADATA;
577 // Note: setting temporal_update has no effect, as the seg-map coding method
578 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
579 // based on the coding cost of each method. For error_resilient mode on the
580 // last_frame_seg_map is set to 0, so if temporal coding is used, it is
581 // relative to 0 previous map.
582 // seg->temporal_update = 0;
584 // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
585 vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
586 // Use segment BOOST1 for in-frame Q adjustment.
587 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
588 // Use segment BOOST2 for more aggressive in-frame Q adjustment.
589 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
591 // Set the q delta for segment BOOST1.
592 qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
593 cr->qindex_delta[1] = qindex_delta;
595 // Compute rd-mult for segment BOOST1.
596 qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
598 cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);
600 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
602 // Set a more aggressive (higher) q delta for segment BOOST2.
603 qindex_delta = compute_deltaq(
604 cpi, cm->base_qindex,
605 VPXMIN(CR_MAX_RATE_TARGET_RATIO,
606 0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta));
607 cr->qindex_delta[2] = qindex_delta;
608 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
610 // Reset if resoluton change has occurred.
611 if (cpi->resize_pending != 0)
612 vp9_cyclic_refresh_reset_resize(cpi);
614 // Update the segmentation and refresh map.
615 cyclic_refresh_update_map(cpi);
619 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
623 void vp9_cyclic_refresh_reset_resize(VP9_COMP *const cpi) {
624 const VP9_COMMON *const cm = &cpi->common;
625 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
626 memset(cr->map, 0, cm->mi_rows * cm->mi_cols);
627 memset(cr->last_coded_q_map, MAXQ, cm->mi_rows * cm->mi_cols);
628 memset(cr->consec_zero_mv, 0, cm->mi_rows * cm->mi_cols);
630 cpi->refresh_golden_frame = 1;