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