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