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