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 static const uint8_t VP9_VAR_OFFS[64] = {
25 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
26 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
27 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
28 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
29 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
30 };
31
vp9_cyclic_refresh_alloc(int mi_rows,int mi_cols)32 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) {
33 size_t last_coded_q_map_size;
34 CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr));
35 if (cr == NULL) return NULL;
36
37 cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map));
38 if (cr->map == NULL) {
39 vp9_cyclic_refresh_free(cr);
40 return NULL;
41 }
42 last_coded_q_map_size = mi_rows * mi_cols * sizeof(*cr->last_coded_q_map);
43 cr->last_coded_q_map = vpx_malloc(last_coded_q_map_size);
44 if (cr->last_coded_q_map == NULL) {
45 vp9_cyclic_refresh_free(cr);
46 return NULL;
47 }
48 assert(MAXQ <= 255);
49 memset(cr->last_coded_q_map, MAXQ, last_coded_q_map_size);
50 cr->counter_encode_maxq_scene_change = 0;
51 cr->content_mode = 1;
52 return cr;
53 }
54
vp9_cyclic_refresh_free(CYCLIC_REFRESH * cr)55 void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) {
56 if (cr != NULL) {
57 vpx_free(cr->map);
58 vpx_free(cr->last_coded_q_map);
59 vpx_free(cr);
60 }
61 }
62
63 // Check if this coding block, of size bsize, should be considered for refresh
64 // (lower-qp coding). Decision can be based on various factors, such as
65 // size of the coding block (i.e., below min_block size rejected), coding
66 // mode, and rate/distortion.
candidate_refresh_aq(const CYCLIC_REFRESH * cr,const MODE_INFO * mi,int64_t rate,int64_t dist,int bsize)67 static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, const MODE_INFO *mi,
68 int64_t rate, int64_t dist, int bsize) {
69 MV mv = mi->mv[0].as_mv;
70 // Reject the block for lower-qp coding if projected distortion
71 // is above the threshold, and any of the following is true:
72 // 1) mode uses large mv
73 // 2) mode is an intra-mode
74 // Otherwise accept for refresh.
75 if (dist > cr->thresh_dist_sb &&
76 (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh ||
77 mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh ||
78 !is_inter_block(mi)))
79 return CR_SEGMENT_ID_BASE;
80 else if (bsize >= BLOCK_16X16 && rate < cr->thresh_rate_sb &&
81 is_inter_block(mi) && mi->mv[0].as_int == 0 &&
82 cr->rate_boost_fac > 10)
83 // More aggressive delta-q for bigger blocks with zero motion.
84 return CR_SEGMENT_ID_BOOST2;
85 else
86 return CR_SEGMENT_ID_BOOST1;
87 }
88
89 // Compute delta-q for the segment.
compute_deltaq(const VP9_COMP * cpi,int q,double rate_factor)90 static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) {
91 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
92 const RATE_CONTROL *const rc = &cpi->rc;
93 int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, q,
94 rate_factor, cpi->common.bit_depth);
95 if ((-deltaq) > cr->max_qdelta_perc * q / 100) {
96 deltaq = -cr->max_qdelta_perc * q / 100;
97 }
98 return deltaq;
99 }
100
101 // For the just encoded frame, estimate the bits, incorporating the delta-q
102 // from non-base segment. For now ignore effect of multiple segments
103 // (with different delta-q). Note this function is called in the postencode
104 // (called from rc_update_rate_correction_factors()).
vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP * cpi,double correction_factor)105 int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi,
106 double correction_factor) {
107 const VP9_COMMON *const cm = &cpi->common;
108 const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
109 int estimated_bits;
110 int mbs = cm->MBs;
111 int num8x8bl = mbs << 2;
112 // Weight for non-base segments: use actual number of blocks refreshed in
113 // previous/just encoded frame. Note number of blocks here is in 8x8 units.
114 double weight_segment1 = (double)cr->actual_num_seg1_blocks / num8x8bl;
115 double weight_segment2 = (double)cr->actual_num_seg2_blocks / num8x8bl;
116 // Take segment weighted average for estimated bits.
117 estimated_bits = (int)round(
118 (1.0 - weight_segment1 - weight_segment2) *
119 vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs,
120 correction_factor, cm->bit_depth) +
121 weight_segment1 *
122 vp9_estimate_bits_at_q(cm->frame_type,
123 cm->base_qindex + cr->qindex_delta[1], mbs,
124 correction_factor, cm->bit_depth) +
125 weight_segment2 *
126 vp9_estimate_bits_at_q(cm->frame_type,
127 cm->base_qindex + cr->qindex_delta[2], mbs,
128 correction_factor, cm->bit_depth));
129 return estimated_bits;
130 }
131
132 // Prior to encoding the frame, estimate the bits per mb, for a given q = i and
133 // a corresponding delta-q (for segment 1). This function is called in the
134 // rc_regulate_q() to set the base qp index.
135 // Note: the segment map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or
136 // to 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock, prior to encoding.
vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP * cpi,int i,double correction_factor)137 int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i,
138 double correction_factor) {
139 const VP9_COMMON *const cm = &cpi->common;
140 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
141 int bits_per_mb;
142 int deltaq = 0;
143 if (cpi->oxcf.speed < 8)
144 deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta);
145 else
146 deltaq = -(cr->max_qdelta_perc * i) / 200;
147 // Take segment weighted average for bits per mb.
148 bits_per_mb =
149 (int)round((1.0 - cr->weight_segment) *
150 vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor,
151 cm->bit_depth) +
152 cr->weight_segment *
153 vp9_rc_bits_per_mb(cm->frame_type, i + deltaq,
154 correction_factor, cm->bit_depth));
155 return bits_per_mb;
156 }
157
158 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
159 // check if we should reset the segment_id, and update the cyclic_refresh map
160 // and segmentation map.
vp9_cyclic_refresh_update_segment(VP9_COMP * const cpi,MODE_INFO * const mi,int mi_row,int mi_col,BLOCK_SIZE bsize,int64_t rate,int64_t dist,int skip,struct macroblock_plane * const p)161 void vp9_cyclic_refresh_update_segment(VP9_COMP *const cpi, MODE_INFO *const mi,
162 int mi_row, int mi_col, BLOCK_SIZE bsize,
163 int64_t rate, int64_t dist, int skip,
164 struct macroblock_plane *const p) {
165 const VP9_COMMON *const cm = &cpi->common;
166 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
167 const int bw = num_8x8_blocks_wide_lookup[bsize];
168 const int bh = num_8x8_blocks_high_lookup[bsize];
169 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
170 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
171 const int block_index = mi_row * cm->mi_cols + mi_col;
172 int refresh_this_block = candidate_refresh_aq(cr, mi, rate, dist, bsize);
173 // Default is to not update the refresh map.
174 int new_map_value = cr->map[block_index];
175 int x = 0;
176 int y = 0;
177
178 int is_skin = 0;
179 if (refresh_this_block == 0 && bsize <= BLOCK_16X16 &&
180 cpi->use_skin_detection) {
181 is_skin =
182 vp9_compute_skin_block(p[0].src.buf, p[1].src.buf, p[2].src.buf,
183 p[0].src.stride, p[1].src.stride, bsize, 0, 0);
184 if (is_skin) refresh_this_block = 1;
185 }
186
187 if (cpi->oxcf.rc_mode == VPX_VBR && mi->ref_frame[0] == GOLDEN_FRAME)
188 refresh_this_block = 0;
189
190 // If this block is labeled for refresh, check if we should reset the
191 // segment_id.
192 if (cpi->sf.use_nonrd_pick_mode &&
193 cyclic_refresh_segment_id_boosted(mi->segment_id)) {
194 mi->segment_id = refresh_this_block;
195 // Reset segment_id if it will be skipped.
196 if (skip) mi->segment_id = CR_SEGMENT_ID_BASE;
197 }
198
199 // Update the cyclic refresh map, to be used for setting segmentation map
200 // for the next frame. If the block will be refreshed this frame, mark it
201 // as clean. The magnitude of the -ve influences how long before we consider
202 // it for refresh again.
203 if (cyclic_refresh_segment_id_boosted(mi->segment_id)) {
204 new_map_value = -cr->time_for_refresh;
205 } else if (refresh_this_block) {
206 // Else if it is accepted as candidate for refresh, and has not already
207 // been refreshed (marked as 1) then mark it as a candidate for cleanup
208 // for future time (marked as 0), otherwise don't update it.
209 if (cr->map[block_index] == 1) new_map_value = 0;
210 } else {
211 // Leave it marked as block that is not candidate for refresh.
212 new_map_value = 1;
213 }
214
215 // Update entries in the cyclic refresh map with new_map_value, and
216 // copy mbmi->segment_id into global segmentation map.
217 for (y = 0; y < ymis; y++)
218 for (x = 0; x < xmis; x++) {
219 int map_offset = block_index + y * cm->mi_cols + x;
220 cr->map[map_offset] = new_map_value;
221 cpi->segmentation_map[map_offset] = mi->segment_id;
222 }
223 }
224
vp9_cyclic_refresh_update_sb_postencode(VP9_COMP * const cpi,const MODE_INFO * const mi,int mi_row,int mi_col,BLOCK_SIZE bsize)225 void vp9_cyclic_refresh_update_sb_postencode(VP9_COMP *const cpi,
226 const MODE_INFO *const mi,
227 int mi_row, int mi_col,
228 BLOCK_SIZE bsize) {
229 const VP9_COMMON *const cm = &cpi->common;
230 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
231 const int bw = num_8x8_blocks_wide_lookup[bsize];
232 const int bh = num_8x8_blocks_high_lookup[bsize];
233 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
234 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
235 const int block_index = mi_row * cm->mi_cols + mi_col;
236 int x, y;
237 for (y = 0; y < ymis; y++)
238 for (x = 0; x < xmis; x++) {
239 int map_offset = block_index + y * cm->mi_cols + x;
240 // Inter skip blocks were clearly not coded at the current qindex, so
241 // don't update the map for them. For cases where motion is non-zero or
242 // the reference frame isn't the previous frame, the previous value in
243 // the map for this spatial location is not entirely correct.
244 if ((!is_inter_block(mi) || !mi->skip) &&
245 mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
246 cr->last_coded_q_map[map_offset] =
247 clamp(cm->base_qindex + cr->qindex_delta[mi->segment_id], 0, MAXQ);
248 } else if (is_inter_block(mi) && mi->skip &&
249 mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
250 cr->last_coded_q_map[map_offset] = VPXMIN(
251 clamp(cm->base_qindex + cr->qindex_delta[mi->segment_id], 0, MAXQ),
252 cr->last_coded_q_map[map_offset]);
253 }
254 }
255 }
256
257 // From the just encoded frame: update the actual number of blocks that were
258 // applied the segment delta q, and the amount of low motion in the frame.
259 // Also check conditions for forcing golden update, or preventing golden
260 // update if the period is up.
vp9_cyclic_refresh_postencode(VP9_COMP * const cpi)261 void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) {
262 VP9_COMMON *const cm = &cpi->common;
263 MODE_INFO **mi = cm->mi_grid_visible;
264 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
265 RATE_CONTROL *const rc = &cpi->rc;
266 unsigned char *const seg_map = cpi->segmentation_map;
267 double fraction_low = 0.0;
268 int force_gf_refresh = 0;
269 int low_content_frame = 0;
270 int mi_row, mi_col;
271 cr->actual_num_seg1_blocks = 0;
272 cr->actual_num_seg2_blocks = 0;
273 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
274 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
275 MV mv = mi[0]->mv[0].as_mv;
276 int map_index = mi_row * cm->mi_cols + mi_col;
277 if (cyclic_refresh_segment_id(seg_map[map_index]) == CR_SEGMENT_ID_BOOST1)
278 cr->actual_num_seg1_blocks++;
279 else if (cyclic_refresh_segment_id(seg_map[map_index]) ==
280 CR_SEGMENT_ID_BOOST2)
281 cr->actual_num_seg2_blocks++;
282 // Accumulate low_content_frame.
283 if (is_inter_block(mi[0]) && abs(mv.row) < 16 && abs(mv.col) < 16)
284 low_content_frame++;
285 mi++;
286 }
287 mi += 8;
288 }
289 // Check for golden frame update: only for non-SVC and non-golden boost.
290 if (!cpi->use_svc && cpi->ext_refresh_frame_flags_pending == 0 &&
291 !cpi->oxcf.gf_cbr_boost_pct) {
292 // Force this frame as a golden update frame if this frame changes the
293 // resolution (resize_pending != 0).
294 if (cpi->resize_pending != 0) {
295 vp9_cyclic_refresh_set_golden_update(cpi);
296 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
297 if (rc->frames_till_gf_update_due > rc->frames_to_key)
298 rc->frames_till_gf_update_due = rc->frames_to_key;
299 cpi->refresh_golden_frame = 1;
300 force_gf_refresh = 1;
301 }
302 // Update average of low content/motion in the frame.
303 fraction_low = (double)low_content_frame / (cm->mi_rows * cm->mi_cols);
304 cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4;
305 if (!force_gf_refresh && cpi->refresh_golden_frame == 1 &&
306 rc->frames_since_key > rc->frames_since_golden + 1) {
307 // Don't update golden reference if the amount of low_content for the
308 // current encoded frame is small, or if the recursive average of the
309 // low_content over the update interval window falls below threshold.
310 if (fraction_low < 0.65 || cr->low_content_avg < 0.6) {
311 cpi->refresh_golden_frame = 0;
312 }
313 // Reset for next internal.
314 cr->low_content_avg = fraction_low;
315 }
316 }
317 }
318
319 // Set golden frame update interval, for non-svc 1 pass CBR mode.
vp9_cyclic_refresh_set_golden_update(VP9_COMP * const cpi)320 void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) {
321 RATE_CONTROL *const rc = &cpi->rc;
322 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
323 // Set minimum gf_interval for GF update to a multiple of the refresh period,
324 // with some max limit. Depending on past encoding stats, GF flag may be
325 // reset and update may not occur until next baseline_gf_interval.
326 if (cr->percent_refresh > 0)
327 rc->baseline_gf_interval = VPXMIN(4 * (100 / cr->percent_refresh), 40);
328 else
329 rc->baseline_gf_interval = 40;
330 if (cpi->oxcf.rc_mode == VPX_VBR) rc->baseline_gf_interval = 20;
331 if (rc->avg_frame_low_motion < 50 && rc->frames_since_key > 40 &&
332 cr->content_mode)
333 rc->baseline_gf_interval = 10;
334 }
335
is_superblock_flat_static(VP9_COMP * const cpi,int sb_row_index,int sb_col_index)336 static int is_superblock_flat_static(VP9_COMP *const cpi, int sb_row_index,
337 int sb_col_index) {
338 unsigned int source_variance;
339 const uint8_t *src_y = cpi->Source->y_buffer;
340 const int ystride = cpi->Source->y_stride;
341 unsigned int sse;
342 const BLOCK_SIZE bsize = BLOCK_64X64;
343 src_y += (sb_row_index << 6) * ystride + (sb_col_index << 6);
344 source_variance =
345 cpi->fn_ptr[bsize].vf(src_y, ystride, VP9_VAR_OFFS, 0, &sse);
346 if (source_variance == 0) {
347 uint64_t block_sad;
348 const uint8_t *last_src_y = cpi->Last_Source->y_buffer;
349 const int last_ystride = cpi->Last_Source->y_stride;
350 last_src_y += (sb_row_index << 6) * ystride + (sb_col_index << 6);
351 block_sad =
352 cpi->fn_ptr[bsize].sdf(src_y, ystride, last_src_y, last_ystride);
353 if (block_sad == 0) return 1;
354 }
355 return 0;
356 }
357
358 // Update the segmentation map, and related quantities: cyclic refresh map,
359 // refresh sb_index, and target number of blocks to be refreshed.
360 // The map is set to either 0/CR_SEGMENT_ID_BASE (no refresh) or to
361 // 1/CR_SEGMENT_ID_BOOST1 (refresh) for each superblock.
362 // Blocks labeled as BOOST1 may later get set to BOOST2 (during the
363 // encoding of the superblock).
cyclic_refresh_update_map(VP9_COMP * const cpi)364 static void cyclic_refresh_update_map(VP9_COMP *const cpi) {
365 VP9_COMMON *const cm = &cpi->common;
366 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
367 unsigned char *const seg_map = cpi->segmentation_map;
368 int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame;
369 int xmis, ymis, x, y;
370 int consec_zero_mv_thresh = 0;
371 int qindex_thresh = 0;
372 int count_sel = 0;
373 int count_tot = 0;
374 memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols);
375 sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
376 sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
377 sbs_in_frame = sb_cols * sb_rows;
378 // Number of target blocks to get the q delta (segment 1).
379 block_count = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
380 // Set the segmentation map: cycle through the superblocks, starting at
381 // cr->mb_index, and stopping when either block_count blocks have been found
382 // to be refreshed, or we have passed through whole frame.
383 assert(cr->sb_index < sbs_in_frame);
384 i = cr->sb_index;
385 cr->target_num_seg_blocks = 0;
386 if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
387 consec_zero_mv_thresh = 100;
388 }
389 qindex_thresh =
390 cpi->oxcf.content == VP9E_CONTENT_SCREEN
391 ? vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2, cm->base_qindex)
392 : vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST1, cm->base_qindex);
393 // More aggressive settings for noisy content.
394 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium &&
395 cr->content_mode) {
396 consec_zero_mv_thresh = 60;
397 qindex_thresh =
398 VPXMAX(vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST1, cm->base_qindex),
399 cm->base_qindex);
400 }
401 do {
402 int sum_map = 0;
403 int consec_zero_mv_thresh_block = consec_zero_mv_thresh;
404 // Get the mi_row/mi_col corresponding to superblock index i.
405 int sb_row_index = (i / sb_cols);
406 int sb_col_index = i - sb_row_index * sb_cols;
407 int mi_row = sb_row_index * MI_BLOCK_SIZE;
408 int mi_col = sb_col_index * MI_BLOCK_SIZE;
409 int flat_static_blocks = 0;
410 int compute_content = 1;
411 assert(mi_row >= 0 && mi_row < cm->mi_rows);
412 assert(mi_col >= 0 && mi_col < cm->mi_cols);
413 #if CONFIG_VP9_HIGHBITDEPTH
414 if (cpi->common.use_highbitdepth) compute_content = 0;
415 #endif
416 if (cr->content_mode == 0 || cpi->Last_Source == NULL ||
417 cpi->Last_Source->y_width != cpi->Source->y_width ||
418 cpi->Last_Source->y_height != cpi->Source->y_height)
419 compute_content = 0;
420 bl_index = mi_row * cm->mi_cols + mi_col;
421 // Loop through all 8x8 blocks in superblock and update map.
422 xmis =
423 VPXMIN(cm->mi_cols - mi_col, num_8x8_blocks_wide_lookup[BLOCK_64X64]);
424 ymis =
425 VPXMIN(cm->mi_rows - mi_row, num_8x8_blocks_high_lookup[BLOCK_64X64]);
426 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium &&
427 (xmis <= 2 || ymis <= 2))
428 consec_zero_mv_thresh_block = 4;
429 for (y = 0; y < ymis; y++) {
430 for (x = 0; x < xmis; x++) {
431 const int bl_index2 = bl_index + y * cm->mi_cols + x;
432 // If the block is as a candidate for clean up then mark it
433 // for possible boost/refresh (segment 1). The segment id may get
434 // reset to 0 later depending on the coding mode.
435 if (cr->map[bl_index2] == 0) {
436 count_tot++;
437 if (cr->content_mode == 0 ||
438 cr->last_coded_q_map[bl_index2] > qindex_thresh ||
439 cpi->consec_zero_mv[bl_index2] < consec_zero_mv_thresh_block) {
440 sum_map++;
441 count_sel++;
442 }
443 } else if (cr->map[bl_index2] < 0) {
444 cr->map[bl_index2]++;
445 }
446 }
447 }
448 // Enforce constant segment over superblock.
449 // If segment is at least half of superblock, set to 1.
450 if (sum_map >= xmis * ymis / 2) {
451 // This superblock is a candidate for refresh:
452 // compute spatial variance and exclude blocks that are spatially flat
453 // and stationary. Note: this is currently only done for screne content
454 // mode.
455 if (compute_content && cr->skip_flat_static_blocks)
456 flat_static_blocks =
457 is_superblock_flat_static(cpi, sb_row_index, sb_col_index);
458 if (!flat_static_blocks) {
459 // Label this superblock as segment 1.
460 for (y = 0; y < ymis; y++)
461 for (x = 0; x < xmis; x++) {
462 seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1;
463 }
464 cr->target_num_seg_blocks += xmis * ymis;
465 }
466 }
467 i++;
468 if (i == sbs_in_frame) {
469 i = 0;
470 }
471 } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index);
472 cr->sb_index = i;
473 cr->reduce_refresh = 0;
474 if (cpi->oxcf.content != VP9E_CONTENT_SCREEN)
475 if (count_sel < (3 * count_tot) >> 2) cr->reduce_refresh = 1;
476 }
477
478 // Set cyclic refresh parameters.
vp9_cyclic_refresh_update_parameters(VP9_COMP * const cpi)479 void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) {
480 const RATE_CONTROL *const rc = &cpi->rc;
481 const VP9_COMMON *const cm = &cpi->common;
482 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
483 int num8x8bl = cm->MBs << 2;
484 int target_refresh = 0;
485 double weight_segment_target = 0;
486 double weight_segment = 0;
487 int thresh_low_motion = 20;
488 int qp_thresh = VPXMIN((cpi->oxcf.content == VP9E_CONTENT_SCREEN) ? 35 : 20,
489 rc->best_quality << 1);
490 int qp_max_thresh = 117 * MAXQ >> 7;
491 cr->apply_cyclic_refresh = 1;
492 if (frame_is_intra_only(cm) || cpi->svc.temporal_layer_id > 0 ||
493 is_lossless_requested(&cpi->oxcf) ||
494 rc->avg_frame_qindex[INTER_FRAME] < qp_thresh ||
495 (cpi->use_svc &&
496 cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame) ||
497 (!cpi->use_svc && cr->content_mode &&
498 rc->avg_frame_low_motion < thresh_low_motion &&
499 rc->frames_since_key > 40) ||
500 (!cpi->use_svc && rc->avg_frame_qindex[INTER_FRAME] > qp_max_thresh &&
501 rc->frames_since_key > 20) ||
502 (cpi->roi.enabled && cpi->roi.skip[BACKGROUND_SEG_SKIP_ID] &&
503 rc->frames_since_key > FRAMES_NO_SKIPPING_AFTER_KEY)) {
504 cr->apply_cyclic_refresh = 0;
505 return;
506 }
507 cr->percent_refresh = 10;
508 if (cr->reduce_refresh) cr->percent_refresh = 5;
509 cr->max_qdelta_perc = 60;
510 cr->time_for_refresh = 0;
511 cr->motion_thresh = 32;
512 cr->rate_boost_fac = 15;
513 // Use larger delta-qp (increase rate_ratio_qdelta) for first few (~4)
514 // periods of the refresh cycle, after a key frame.
515 // Account for larger interval on base layer for temporal layers.
516 if (cr->percent_refresh > 0 &&
517 rc->frames_since_key <
518 (4 * cpi->svc.number_temporal_layers) * (100 / cr->percent_refresh)) {
519 cr->rate_ratio_qdelta = 3.0;
520 } else {
521 cr->rate_ratio_qdelta = 2.0;
522 if (cr->content_mode && cpi->noise_estimate.enabled &&
523 cpi->noise_estimate.level >= kMedium) {
524 // Reduce the delta-qp if the estimated source noise is above threshold.
525 cr->rate_ratio_qdelta = 1.7;
526 cr->rate_boost_fac = 13;
527 }
528 }
529 // For screen-content: keep rate_ratio_qdelta to 2.0 (segment#1 boost) and
530 // percent_refresh (refresh rate) to 10. But reduce rate boost for segment#2
531 // (rate_boost_fac = 10 disables segment#2).
532 if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
533 // Only enable feature of skipping flat_static blocks for top layer
534 // under screen content mode.
535 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
536 cr->skip_flat_static_blocks = 1;
537 cr->percent_refresh = (cr->skip_flat_static_blocks) ? 5 : 10;
538 // Increase the amount of refresh on scene change that is encoded at max Q,
539 // increase for a few cycles of the refresh period (~100 / percent_refresh).
540 if (cr->content_mode && cr->counter_encode_maxq_scene_change < 30)
541 cr->percent_refresh = (cr->skip_flat_static_blocks) ? 10 : 15;
542 cr->rate_ratio_qdelta = 2.0;
543 cr->rate_boost_fac = 10;
544 }
545 // Adjust some parameters for low resolutions.
546 if (cm->width * cm->height <= 352 * 288) {
547 if (rc->avg_frame_bandwidth < 3000) {
548 cr->motion_thresh = 64;
549 cr->rate_boost_fac = 13;
550 } else {
551 cr->max_qdelta_perc = 70;
552 cr->rate_ratio_qdelta = VPXMAX(cr->rate_ratio_qdelta, 2.5);
553 }
554 }
555 if (cpi->oxcf.rc_mode == VPX_VBR) {
556 // To be adjusted for VBR mode, e.g., based on gf period and boost.
557 // For now use smaller qp-delta (than CBR), no second boosted seg, and
558 // turn-off (no refresh) on golden refresh (since it's already boosted).
559 cr->percent_refresh = 10;
560 cr->rate_ratio_qdelta = 1.5;
561 cr->rate_boost_fac = 10;
562 if (cpi->refresh_golden_frame == 1 && !cpi->use_svc) {
563 cr->percent_refresh = 0;
564 cr->rate_ratio_qdelta = 1.0;
565 }
566 }
567 // Weight for segment prior to encoding: take the average of the target
568 // number for the frame to be encoded and the actual from the previous frame.
569 // Use the target if its less. To be used for setting the base qp for the
570 // frame in vp9_rc_regulate_q.
571 target_refresh = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
572 weight_segment_target = (double)(target_refresh) / num8x8bl;
573 weight_segment = (double)((target_refresh + cr->actual_num_seg1_blocks +
574 cr->actual_num_seg2_blocks) >>
575 1) /
576 num8x8bl;
577 if (weight_segment_target < 7 * weight_segment / 8)
578 weight_segment = weight_segment_target;
579 // For screen-content: don't include target for the weight segment,
580 // since for all flat areas the segment is reset, so its more accurate
581 // to just use the previous actual number of seg blocks for the weight.
582 if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
583 weight_segment =
584 (double)(cr->actual_num_seg1_blocks + cr->actual_num_seg2_blocks) /
585 num8x8bl;
586 cr->weight_segment = weight_segment;
587 if (cr->content_mode == 0) {
588 cr->actual_num_seg1_blocks =
589 cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100;
590 cr->actual_num_seg2_blocks = 0;
591 cr->weight_segment = (double)(cr->actual_num_seg1_blocks) / num8x8bl;
592 }
593 }
594
595 // Setup cyclic background refresh: set delta q and segmentation map.
vp9_cyclic_refresh_setup(VP9_COMP * const cpi)596 void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) {
597 VP9_COMMON *const cm = &cpi->common;
598 const RATE_CONTROL *const rc = &cpi->rc;
599 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
600 struct segmentation *const seg = &cm->seg;
601 int scene_change_detected =
602 cpi->rc.high_source_sad ||
603 (cpi->use_svc && cpi->svc.high_source_sad_superframe);
604 if (cm->current_video_frame == 0) cr->low_content_avg = 0.0;
605 // Reset if resoluton change has occurred.
606 if (cpi->resize_pending != 0) vp9_cyclic_refresh_reset_resize(cpi);
607 if (!cr->apply_cyclic_refresh || (cpi->force_update_segmentation) ||
608 scene_change_detected) {
609 // Set segmentation map to 0 and disable.
610 unsigned char *const seg_map = cpi->segmentation_map;
611 memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
612 vp9_disable_segmentation(&cm->seg);
613 if (cm->frame_type == KEY_FRAME || scene_change_detected) {
614 memset(cr->last_coded_q_map, MAXQ,
615 cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
616 cr->sb_index = 0;
617 cr->reduce_refresh = 0;
618 cr->counter_encode_maxq_scene_change = 0;
619 }
620 return;
621 } else {
622 int qindex_delta = 0;
623 int qindex2;
624 const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
625 cr->counter_encode_maxq_scene_change++;
626 vpx_clear_system_state();
627 // Set rate threshold to some multiple (set to 2 for now) of the target
628 // rate (target is given by sb64_target_rate and scaled by 256).
629 cr->thresh_rate_sb = ((int64_t)(rc->sb64_target_rate) << 8) << 2;
630 // Distortion threshold, quadratic in Q, scale factor to be adjusted.
631 // q will not exceed 457, so (q * q) is within 32bit; see:
632 // vp9_convert_qindex_to_q(), vp9_ac_quant(), ac_qlookup*[].
633 cr->thresh_dist_sb = ((int64_t)(q * q)) << 2;
634
635 // Set up segmentation.
636 // Clear down the segment map.
637 vp9_enable_segmentation(&cm->seg);
638 vp9_clearall_segfeatures(seg);
639 // Select delta coding method.
640 seg->abs_delta = SEGMENT_DELTADATA;
641
642 // Note: setting temporal_update has no effect, as the seg-map coding method
643 // (temporal or spatial) is determined in vp9_choose_segmap_coding_method(),
644 // based on the coding cost of each method. For error_resilient mode on the
645 // last_frame_seg_map is set to 0, so if temporal coding is used, it is
646 // relative to 0 previous map.
647 // seg->temporal_update = 0;
648
649 // Segment BASE "Q" feature is disabled so it defaults to the baseline Q.
650 vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q);
651 // Use segment BOOST1 for in-frame Q adjustment.
652 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q);
653 // Use segment BOOST2 for more aggressive in-frame Q adjustment.
654 vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q);
655
656 // Set the q delta for segment BOOST1.
657 qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta);
658 cr->qindex_delta[1] = qindex_delta;
659
660 // Compute rd-mult for segment BOOST1.
661 qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ);
662
663 cr->rdmult = vp9_compute_rd_mult(cpi, qindex2);
664
665 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q, qindex_delta);
666
667 // Set a more aggressive (higher) q delta for segment BOOST2.
668 qindex_delta = compute_deltaq(
669 cpi, cm->base_qindex,
670 VPXMIN(CR_MAX_RATE_TARGET_RATIO,
671 0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta));
672 cr->qindex_delta[2] = qindex_delta;
673 vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta);
674
675 // Update the segmentation and refresh map.
676 cyclic_refresh_update_map(cpi);
677 }
678 }
679
vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH * cr)680 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) {
681 return cr->rdmult;
682 }
683
vp9_cyclic_refresh_reset_resize(VP9_COMP * const cpi)684 void vp9_cyclic_refresh_reset_resize(VP9_COMP *const cpi) {
685 const VP9_COMMON *const cm = &cpi->common;
686 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
687 memset(cr->map, 0, cm->mi_rows * cm->mi_cols);
688 memset(cr->last_coded_q_map, MAXQ,
689 cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
690 cr->sb_index = 0;
691 cpi->refresh_golden_frame = 1;
692 cpi->refresh_alt_ref_frame = 1;
693 cr->counter_encode_maxq_scene_change = 0;
694 }
695
vp9_cyclic_refresh_limit_q(const VP9_COMP * cpi,int * q)696 void vp9_cyclic_refresh_limit_q(const VP9_COMP *cpi, int *q) {
697 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
698 // For now apply hard limit to frame-level decrease in q, if the cyclic
699 // refresh is active (percent_refresh > 0).
700 if (cr->percent_refresh > 0 && cpi->rc.q_1_frame - *q > 8) {
701 *q = cpi->rc.q_1_frame - 8;
702 }
703 }
704