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