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