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