/src/libvpx/vp9/encoder/vp9_aq_cyclicrefresh.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
32 | 2.98k | CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols) { |
33 | 2.98k | size_t last_coded_q_map_size; |
34 | 2.98k | CYCLIC_REFRESH *const cr = vpx_calloc(1, sizeof(*cr)); |
35 | 2.98k | if (cr == NULL) return NULL; |
36 | | |
37 | 2.98k | cr->map = vpx_calloc(mi_rows * mi_cols, sizeof(*cr->map)); |
38 | 2.98k | if (cr->map == NULL) { |
39 | 0 | vp9_cyclic_refresh_free(cr); |
40 | 0 | return NULL; |
41 | 0 | } |
42 | 2.98k | last_coded_q_map_size = mi_rows * mi_cols * sizeof(*cr->last_coded_q_map); |
43 | 2.98k | cr->last_coded_q_map = vpx_malloc(last_coded_q_map_size); |
44 | 2.98k | if (cr->last_coded_q_map == NULL) { |
45 | 0 | vp9_cyclic_refresh_free(cr); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | 2.98k | assert(MAXQ <= 255); |
49 | 2.98k | memset(cr->last_coded_q_map, MAXQ, last_coded_q_map_size); |
50 | 2.98k | cr->counter_encode_maxq_scene_change = 0; |
51 | 2.98k | cr->content_mode = 1; |
52 | 2.98k | return cr; |
53 | 2.98k | } |
54 | | |
55 | 2.98k | void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr) { |
56 | 2.98k | if (cr != NULL) { |
57 | 2.98k | vpx_free(cr->map); |
58 | 2.98k | vpx_free(cr->last_coded_q_map); |
59 | 2.98k | vpx_free(cr); |
60 | 2.98k | } |
61 | 2.98k | } |
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. |
67 | | static int candidate_refresh_aq(const CYCLIC_REFRESH *cr, const MODE_INFO *mi, |
68 | 0 | int64_t rate, int64_t dist, int bsize) { |
69 | 0 | 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 | 0 | if (dist > cr->thresh_dist_sb && |
76 | 0 | (mv.row > cr->motion_thresh || mv.row < -cr->motion_thresh || |
77 | 0 | mv.col > cr->motion_thresh || mv.col < -cr->motion_thresh || |
78 | 0 | !is_inter_block(mi))) |
79 | 0 | return CR_SEGMENT_ID_BASE; |
80 | 0 | else if (bsize >= BLOCK_16X16 && rate < cr->thresh_rate_sb && |
81 | 0 | is_inter_block(mi) && mi->mv[0].as_int == 0 && |
82 | 0 | cr->rate_boost_fac > 10) |
83 | | // More aggressive delta-q for bigger blocks with zero motion. |
84 | 0 | return CR_SEGMENT_ID_BOOST2; |
85 | 0 | else |
86 | 0 | return CR_SEGMENT_ID_BOOST1; |
87 | 0 | } |
88 | | |
89 | | // Compute delta-q for the segment. |
90 | 0 | static int compute_deltaq(const VP9_COMP *cpi, int q, double rate_factor) { |
91 | 0 | const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
92 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
93 | 0 | int deltaq = vp9_compute_qdelta_by_rate(rc, cpi->common.frame_type, q, |
94 | 0 | rate_factor, cpi->common.bit_depth); |
95 | 0 | if ((-deltaq) > cr->max_qdelta_perc * q / 100) { |
96 | 0 | deltaq = -cr->max_qdelta_perc * q / 100; |
97 | 0 | } |
98 | 0 | return deltaq; |
99 | 0 | } |
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()). |
105 | | int vp9_cyclic_refresh_estimate_bits_at_q(const VP9_COMP *cpi, |
106 | 0 | double correction_factor) { |
107 | 0 | const VP9_COMMON *const cm = &cpi->common; |
108 | 0 | const CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
109 | 0 | int estimated_bits; |
110 | 0 | int mbs = cm->MBs; |
111 | 0 | 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 | 0 | double weight_segment1 = (double)cr->actual_num_seg1_blocks / num8x8bl; |
115 | 0 | double weight_segment2 = (double)cr->actual_num_seg2_blocks / num8x8bl; |
116 | | // Take segment weighted average for estimated bits. |
117 | 0 | estimated_bits = (int)round( |
118 | 0 | (1.0 - weight_segment1 - weight_segment2) * |
119 | 0 | vp9_estimate_bits_at_q(cm->frame_type, cm->base_qindex, mbs, |
120 | 0 | correction_factor, cm->bit_depth) + |
121 | 0 | weight_segment1 * |
122 | 0 | vp9_estimate_bits_at_q(cm->frame_type, |
123 | 0 | cm->base_qindex + cr->qindex_delta[1], mbs, |
124 | 0 | correction_factor, cm->bit_depth) + |
125 | 0 | weight_segment2 * |
126 | 0 | vp9_estimate_bits_at_q(cm->frame_type, |
127 | 0 | cm->base_qindex + cr->qindex_delta[2], mbs, |
128 | 0 | correction_factor, cm->bit_depth)); |
129 | 0 | return estimated_bits; |
130 | 0 | } |
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. |
137 | | int vp9_cyclic_refresh_rc_bits_per_mb(const VP9_COMP *cpi, int i, |
138 | 0 | double correction_factor) { |
139 | 0 | const VP9_COMMON *const cm = &cpi->common; |
140 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
141 | 0 | int bits_per_mb; |
142 | 0 | int deltaq = 0; |
143 | 0 | if (cpi->oxcf.speed < 8) |
144 | 0 | deltaq = compute_deltaq(cpi, i, cr->rate_ratio_qdelta); |
145 | 0 | else |
146 | 0 | deltaq = -(cr->max_qdelta_perc * i) / 200; |
147 | | // Take segment weighted average for bits per mb. |
148 | 0 | bits_per_mb = |
149 | 0 | (int)round((1.0 - cr->weight_segment) * |
150 | 0 | vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor, |
151 | 0 | cm->bit_depth) + |
152 | 0 | cr->weight_segment * |
153 | 0 | vp9_rc_bits_per_mb(cm->frame_type, i + deltaq, |
154 | 0 | correction_factor, cm->bit_depth)); |
155 | 0 | return bits_per_mb; |
156 | 0 | } |
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. |
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 | 0 | struct macroblock_plane *const p) { |
165 | 0 | const VP9_COMMON *const cm = &cpi->common; |
166 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
167 | 0 | const int bw = num_8x8_blocks_wide_lookup[bsize]; |
168 | 0 | const int bh = num_8x8_blocks_high_lookup[bsize]; |
169 | 0 | const int xmis = VPXMIN(cm->mi_cols - mi_col, bw); |
170 | 0 | const int ymis = VPXMIN(cm->mi_rows - mi_row, bh); |
171 | 0 | const int block_index = mi_row * cm->mi_cols + mi_col; |
172 | 0 | int refresh_this_block = candidate_refresh_aq(cr, mi, rate, dist, bsize); |
173 | | // Default is to not update the refresh map. |
174 | 0 | int new_map_value = cr->map[block_index]; |
175 | 0 | int x = 0; |
176 | 0 | int y = 0; |
177 | |
|
178 | 0 | int is_skin = 0; |
179 | 0 | if (refresh_this_block == 0 && bsize <= BLOCK_16X16 && |
180 | 0 | cpi->use_skin_detection) { |
181 | 0 | is_skin = |
182 | 0 | vp9_compute_skin_block(p[0].src.buf, p[1].src.buf, p[2].src.buf, |
183 | 0 | p[0].src.stride, p[1].src.stride, bsize, 0, 0); |
184 | 0 | if (is_skin) refresh_this_block = 1; |
185 | 0 | } |
186 | |
|
187 | 0 | if (cpi->oxcf.rc_mode == VPX_VBR && mi->ref_frame[0] == GOLDEN_FRAME) |
188 | 0 | refresh_this_block = 0; |
189 | | |
190 | | // If this block is labeled for refresh, check if we should reset the |
191 | | // segment_id. |
192 | 0 | if (cpi->sf.use_nonrd_pick_mode && |
193 | 0 | cyclic_refresh_segment_id_boosted(mi->segment_id)) { |
194 | 0 | mi->segment_id = refresh_this_block; |
195 | | // Reset segment_id if it will be skipped. |
196 | 0 | if (skip) mi->segment_id = CR_SEGMENT_ID_BASE; |
197 | 0 | } |
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 | 0 | if (cyclic_refresh_segment_id_boosted(mi->segment_id)) { |
204 | 0 | new_map_value = -cr->time_for_refresh; |
205 | 0 | } 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 | 0 | if (cr->map[block_index] == 1) new_map_value = 0; |
210 | 0 | } else { |
211 | | // Leave it marked as block that is not candidate for refresh. |
212 | 0 | new_map_value = 1; |
213 | 0 | } |
214 | | |
215 | | // Update entries in the cyclic refresh map with new_map_value, and |
216 | | // copy mbmi->segment_id into global segmentation map. |
217 | 0 | for (y = 0; y < ymis; y++) |
218 | 0 | for (x = 0; x < xmis; x++) { |
219 | 0 | int map_offset = block_index + y * cm->mi_cols + x; |
220 | 0 | cr->map[map_offset] = new_map_value; |
221 | 0 | cpi->segmentation_map[map_offset] = mi->segment_id; |
222 | 0 | } |
223 | 0 | } |
224 | | |
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 | 0 | BLOCK_SIZE bsize) { |
229 | 0 | const VP9_COMMON *const cm = &cpi->common; |
230 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
231 | 0 | const int bw = num_8x8_blocks_wide_lookup[bsize]; |
232 | 0 | const int bh = num_8x8_blocks_high_lookup[bsize]; |
233 | 0 | const int xmis = VPXMIN(cm->mi_cols - mi_col, bw); |
234 | 0 | const int ymis = VPXMIN(cm->mi_rows - mi_row, bh); |
235 | 0 | const int block_index = mi_row * cm->mi_cols + mi_col; |
236 | 0 | int x, y; |
237 | 0 | for (y = 0; y < ymis; y++) |
238 | 0 | for (x = 0; x < xmis; x++) { |
239 | 0 | 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 | 0 | 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] = |
247 | 0 | clamp(cm->base_qindex + cr->qindex_delta[mi->segment_id], 0, MAXQ); |
248 | 0 | } else if (is_inter_block(mi) && mi->skip && |
249 | 0 | mi->segment_id <= CR_SEGMENT_ID_BOOST2) { |
250 | 0 | cr->last_coded_q_map[map_offset] = VPXMIN( |
251 | 0 | clamp(cm->base_qindex + cr->qindex_delta[mi->segment_id], 0, MAXQ), |
252 | 0 | cr->last_coded_q_map[map_offset]); |
253 | 0 | } |
254 | 0 | } |
255 | 0 | } |
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. |
261 | 0 | void vp9_cyclic_refresh_postencode(VP9_COMP *const cpi) { |
262 | 0 | VP9_COMMON *const cm = &cpi->common; |
263 | 0 | MODE_INFO **mi = cm->mi_grid_visible; |
264 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
265 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
266 | 0 | unsigned char *const seg_map = cpi->segmentation_map; |
267 | 0 | double fraction_low = 0.0; |
268 | 0 | int force_gf_refresh = 0; |
269 | 0 | int low_content_frame = 0; |
270 | 0 | int mi_row, mi_col; |
271 | 0 | cr->actual_num_seg1_blocks = 0; |
272 | 0 | cr->actual_num_seg2_blocks = 0; |
273 | 0 | for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) { |
274 | 0 | for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { |
275 | 0 | MV mv = mi[0]->mv[0].as_mv; |
276 | 0 | int map_index = mi_row * cm->mi_cols + mi_col; |
277 | 0 | if (cyclic_refresh_segment_id(seg_map[map_index]) == CR_SEGMENT_ID_BOOST1) |
278 | 0 | cr->actual_num_seg1_blocks++; |
279 | 0 | else if (cyclic_refresh_segment_id(seg_map[map_index]) == |
280 | 0 | CR_SEGMENT_ID_BOOST2) |
281 | 0 | cr->actual_num_seg2_blocks++; |
282 | | // Accumulate low_content_frame. |
283 | 0 | if (is_inter_block(mi[0]) && abs(mv.row) < 16 && abs(mv.col) < 16) |
284 | 0 | low_content_frame++; |
285 | 0 | mi++; |
286 | 0 | } |
287 | 0 | mi += 8; |
288 | 0 | } |
289 | | // Check for golden frame update: only for non-SVC and non-golden boost. |
290 | 0 | if (!cpi->use_svc && cpi->ext_refresh_frame_flags_pending == 0 && |
291 | 0 | !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 | 0 | if (cpi->resize_pending != 0) { |
295 | 0 | vp9_cyclic_refresh_set_golden_update(cpi); |
296 | 0 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
297 | 0 | if (rc->frames_till_gf_update_due > rc->frames_to_key) |
298 | 0 | rc->frames_till_gf_update_due = rc->frames_to_key; |
299 | 0 | cpi->refresh_golden_frame = 1; |
300 | 0 | force_gf_refresh = 1; |
301 | 0 | } |
302 | | // Update average of low content/motion in the frame. |
303 | 0 | fraction_low = (double)low_content_frame / (cm->mi_rows * cm->mi_cols); |
304 | 0 | cr->low_content_avg = (fraction_low + 3 * cr->low_content_avg) / 4; |
305 | 0 | if (!force_gf_refresh && cpi->refresh_golden_frame == 1 && |
306 | 0 | 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 | 0 | if (fraction_low < 0.65 || cr->low_content_avg < 0.6) { |
311 | 0 | cpi->refresh_golden_frame = 0; |
312 | 0 | } |
313 | | // Reset for next internal. |
314 | 0 | cr->low_content_avg = fraction_low; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | } |
318 | | |
319 | | // Set golden frame update interval, for non-svc 1 pass CBR mode. |
320 | 0 | void vp9_cyclic_refresh_set_golden_update(VP9_COMP *const cpi) { |
321 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
322 | 0 | 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 | 0 | if (cr->percent_refresh > 0) |
327 | 0 | rc->baseline_gf_interval = VPXMIN(4 * (100 / cr->percent_refresh), 40); |
328 | 0 | else |
329 | 0 | rc->baseline_gf_interval = 40; |
330 | 0 | if (cpi->oxcf.rc_mode == VPX_VBR) rc->baseline_gf_interval = 20; |
331 | 0 | if (rc->avg_frame_low_motion < 50 && rc->frames_since_key > 40 && |
332 | 0 | cr->content_mode) |
333 | 0 | rc->baseline_gf_interval = 10; |
334 | 0 | } |
335 | | |
336 | | static int is_superblock_flat_static(VP9_COMP *const cpi, int sb_row_index, |
337 | 0 | int sb_col_index) { |
338 | 0 | unsigned int source_variance; |
339 | 0 | const uint8_t *src_y = cpi->Source->y_buffer; |
340 | 0 | const int ystride = cpi->Source->y_stride; |
341 | 0 | unsigned int sse; |
342 | 0 | const BLOCK_SIZE bsize = BLOCK_64X64; |
343 | 0 | src_y += (sb_row_index << 6) * ystride + (sb_col_index << 6); |
344 | 0 | source_variance = |
345 | 0 | cpi->fn_ptr[bsize].vf(src_y, ystride, VP9_VAR_OFFS, 0, &sse); |
346 | 0 | if (source_variance == 0) { |
347 | 0 | uint64_t block_sad; |
348 | 0 | const uint8_t *last_src_y = cpi->Last_Source->y_buffer; |
349 | 0 | const int last_ystride = cpi->Last_Source->y_stride; |
350 | 0 | last_src_y += (sb_row_index << 6) * ystride + (sb_col_index << 6); |
351 | 0 | block_sad = |
352 | 0 | cpi->fn_ptr[bsize].sdf(src_y, ystride, last_src_y, last_ystride); |
353 | 0 | if (block_sad == 0) return 1; |
354 | 0 | } |
355 | 0 | return 0; |
356 | 0 | } |
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). |
364 | 0 | static void cyclic_refresh_update_map(VP9_COMP *const cpi) { |
365 | 0 | VP9_COMMON *const cm = &cpi->common; |
366 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
367 | 0 | unsigned char *const seg_map = cpi->segmentation_map; |
368 | 0 | int i, block_count, bl_index, sb_rows, sb_cols, sbs_in_frame; |
369 | 0 | int xmis, ymis, x, y; |
370 | 0 | int consec_zero_mv_thresh = 0; |
371 | 0 | int qindex_thresh = 0; |
372 | 0 | int count_sel = 0; |
373 | 0 | int count_tot = 0; |
374 | 0 | memset(seg_map, CR_SEGMENT_ID_BASE, cm->mi_rows * cm->mi_cols); |
375 | 0 | sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
376 | 0 | sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
377 | 0 | sbs_in_frame = sb_cols * sb_rows; |
378 | | // Number of target blocks to get the q delta (segment 1). |
379 | 0 | 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 | 0 | assert(cr->sb_index < sbs_in_frame); |
384 | 0 | i = cr->sb_index; |
385 | 0 | cr->target_num_seg_blocks = 0; |
386 | 0 | if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) { |
387 | 0 | consec_zero_mv_thresh = 100; |
388 | 0 | } |
389 | 0 | qindex_thresh = |
390 | 0 | cpi->oxcf.content == VP9E_CONTENT_SCREEN |
391 | 0 | ? vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2, cm->base_qindex) |
392 | 0 | : vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST1, cm->base_qindex); |
393 | | // More aggressive settings for noisy content. |
394 | 0 | if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium && |
395 | 0 | cr->content_mode) { |
396 | 0 | consec_zero_mv_thresh = 60; |
397 | 0 | qindex_thresh = |
398 | 0 | VPXMAX(vp9_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST1, cm->base_qindex), |
399 | 0 | cm->base_qindex); |
400 | 0 | } |
401 | 0 | do { |
402 | 0 | int sum_map = 0; |
403 | 0 | int consec_zero_mv_thresh_block = consec_zero_mv_thresh; |
404 | | // Get the mi_row/mi_col corresponding to superblock index i. |
405 | 0 | int sb_row_index = (i / sb_cols); |
406 | 0 | int sb_col_index = i - sb_row_index * sb_cols; |
407 | 0 | int mi_row = sb_row_index * MI_BLOCK_SIZE; |
408 | 0 | int mi_col = sb_col_index * MI_BLOCK_SIZE; |
409 | 0 | int flat_static_blocks = 0; |
410 | 0 | int compute_content = 1; |
411 | 0 | assert(mi_row >= 0 && mi_row < cm->mi_rows); |
412 | 0 | assert(mi_col >= 0 && mi_col < cm->mi_cols); |
413 | 0 | #if CONFIG_VP9_HIGHBITDEPTH |
414 | 0 | if (cpi->common.use_highbitdepth) compute_content = 0; |
415 | 0 | #endif |
416 | 0 | if (cr->content_mode == 0 || cpi->Last_Source == NULL || |
417 | 0 | cpi->Last_Source->y_width != cpi->Source->y_width || |
418 | 0 | cpi->Last_Source->y_height != cpi->Source->y_height) |
419 | 0 | compute_content = 0; |
420 | 0 | bl_index = mi_row * cm->mi_cols + mi_col; |
421 | | // Loop through all 8x8 blocks in superblock and update map. |
422 | 0 | xmis = |
423 | 0 | VPXMIN(cm->mi_cols - mi_col, num_8x8_blocks_wide_lookup[BLOCK_64X64]); |
424 | 0 | ymis = |
425 | 0 | VPXMIN(cm->mi_rows - mi_row, num_8x8_blocks_high_lookup[BLOCK_64X64]); |
426 | 0 | if (cpi->noise_estimate.enabled && cpi->noise_estimate.level >= kMedium && |
427 | 0 | (xmis <= 2 || ymis <= 2)) |
428 | 0 | consec_zero_mv_thresh_block = 4; |
429 | 0 | for (y = 0; y < ymis; y++) { |
430 | 0 | for (x = 0; x < xmis; x++) { |
431 | 0 | 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 | 0 | if (cr->map[bl_index2] == 0) { |
436 | 0 | count_tot++; |
437 | 0 | if (cr->content_mode == 0 || |
438 | 0 | cr->last_coded_q_map[bl_index2] > qindex_thresh || |
439 | 0 | cpi->consec_zero_mv[bl_index2] < consec_zero_mv_thresh_block) { |
440 | 0 | sum_map++; |
441 | 0 | count_sel++; |
442 | 0 | } |
443 | 0 | } else if (cr->map[bl_index2] < 0) { |
444 | 0 | cr->map[bl_index2]++; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | } |
448 | | // Enforce constant segment over superblock. |
449 | | // If segment is at least half of superblock, set to 1. |
450 | 0 | 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 | 0 | if (compute_content && cr->skip_flat_static_blocks) |
456 | 0 | flat_static_blocks = |
457 | 0 | is_superblock_flat_static(cpi, sb_row_index, sb_col_index); |
458 | 0 | if (!flat_static_blocks) { |
459 | | // Label this superblock as segment 1. |
460 | 0 | for (y = 0; y < ymis; y++) |
461 | 0 | for (x = 0; x < xmis; x++) { |
462 | 0 | seg_map[bl_index + y * cm->mi_cols + x] = CR_SEGMENT_ID_BOOST1; |
463 | 0 | } |
464 | 0 | cr->target_num_seg_blocks += xmis * ymis; |
465 | 0 | } |
466 | 0 | } |
467 | 0 | i++; |
468 | 0 | if (i == sbs_in_frame) { |
469 | 0 | i = 0; |
470 | 0 | } |
471 | 0 | } while (cr->target_num_seg_blocks < block_count && i != cr->sb_index); |
472 | 0 | cr->sb_index = i; |
473 | 0 | cr->reduce_refresh = 0; |
474 | 0 | if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) |
475 | 0 | if (count_sel < (3 * count_tot) >> 2) cr->reduce_refresh = 1; |
476 | 0 | } |
477 | | |
478 | | // Set cyclic refresh parameters. |
479 | 0 | void vp9_cyclic_refresh_update_parameters(VP9_COMP *const cpi) { |
480 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
481 | 0 | const VP9_COMMON *const cm = &cpi->common; |
482 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
483 | 0 | int num8x8bl = cm->MBs << 2; |
484 | 0 | int target_refresh = 0; |
485 | 0 | double weight_segment_target = 0; |
486 | 0 | double weight_segment = 0; |
487 | 0 | int thresh_low_motion = 20; |
488 | 0 | int qp_thresh = VPXMIN((cpi->oxcf.content == VP9E_CONTENT_SCREEN) ? 35 : 20, |
489 | 0 | rc->best_quality << 1); |
490 | 0 | int qp_max_thresh = 117 * MAXQ >> 7; |
491 | 0 | cr->apply_cyclic_refresh = 1; |
492 | 0 | if (frame_is_intra_only(cm) || cpi->svc.temporal_layer_id > 0 || |
493 | 0 | is_lossless_requested(&cpi->oxcf) || |
494 | 0 | rc->avg_frame_qindex[INTER_FRAME] < qp_thresh || |
495 | 0 | (cpi->use_svc && |
496 | 0 | cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame) || |
497 | 0 | (!cpi->use_svc && cr->content_mode && |
498 | 0 | rc->avg_frame_low_motion < thresh_low_motion && |
499 | 0 | rc->frames_since_key > 40) || |
500 | 0 | (!cpi->use_svc && rc->avg_frame_qindex[INTER_FRAME] > qp_max_thresh && |
501 | 0 | rc->frames_since_key > 20) || |
502 | 0 | (cpi->roi.enabled && cpi->roi.skip[BACKGROUND_SEG_SKIP_ID] && |
503 | 0 | rc->frames_since_key > FRAMES_NO_SKIPPING_AFTER_KEY)) { |
504 | 0 | cr->apply_cyclic_refresh = 0; |
505 | 0 | return; |
506 | 0 | } |
507 | 0 | cr->percent_refresh = 10; |
508 | 0 | if (cr->reduce_refresh) cr->percent_refresh = 5; |
509 | 0 | cr->max_qdelta_perc = 60; |
510 | 0 | cr->time_for_refresh = 0; |
511 | 0 | cr->motion_thresh = 32; |
512 | 0 | 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 | 0 | if (cr->percent_refresh > 0 && |
517 | 0 | rc->frames_since_key < |
518 | 0 | (4 * cpi->svc.number_temporal_layers) * (100 / cr->percent_refresh)) { |
519 | 0 | cr->rate_ratio_qdelta = 3.0; |
520 | 0 | } else { |
521 | 0 | cr->rate_ratio_qdelta = 2.0; |
522 | 0 | if (cr->content_mode && cpi->noise_estimate.enabled && |
523 | 0 | cpi->noise_estimate.level >= kMedium) { |
524 | | // Reduce the delta-qp if the estimated source noise is above threshold. |
525 | 0 | cr->rate_ratio_qdelta = 1.7; |
526 | 0 | cr->rate_boost_fac = 13; |
527 | 0 | } |
528 | 0 | } |
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 | 0 | 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 | 0 | if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) |
536 | 0 | cr->skip_flat_static_blocks = 1; |
537 | 0 | 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 | 0 | if (cr->content_mode && cr->counter_encode_maxq_scene_change < 30) |
541 | 0 | cr->percent_refresh = (cr->skip_flat_static_blocks) ? 10 : 15; |
542 | 0 | cr->rate_ratio_qdelta = 2.0; |
543 | 0 | cr->rate_boost_fac = 10; |
544 | 0 | } |
545 | | // Adjust some parameters for low resolutions. |
546 | 0 | if (cm->width * cm->height <= 352 * 288) { |
547 | 0 | if (rc->avg_frame_bandwidth < 3000) { |
548 | 0 | cr->motion_thresh = 64; |
549 | 0 | cr->rate_boost_fac = 13; |
550 | 0 | } else { |
551 | 0 | cr->max_qdelta_perc = 70; |
552 | 0 | cr->rate_ratio_qdelta = VPXMAX(cr->rate_ratio_qdelta, 2.5); |
553 | 0 | } |
554 | 0 | } |
555 | 0 | 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 | 0 | cr->percent_refresh = 10; |
560 | 0 | cr->rate_ratio_qdelta = 1.5; |
561 | 0 | cr->rate_boost_fac = 10; |
562 | 0 | if (cpi->refresh_golden_frame == 1 && !cpi->use_svc) { |
563 | 0 | cr->percent_refresh = 0; |
564 | 0 | cr->rate_ratio_qdelta = 1.0; |
565 | 0 | } |
566 | 0 | } |
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 | 0 | target_refresh = cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100; |
572 | 0 | weight_segment_target = (double)(target_refresh) / num8x8bl; |
573 | 0 | weight_segment = (double)((target_refresh + cr->actual_num_seg1_blocks + |
574 | 0 | cr->actual_num_seg2_blocks) >> |
575 | 0 | 1) / |
576 | 0 | num8x8bl; |
577 | 0 | if (weight_segment_target < 7 * weight_segment / 8) |
578 | 0 | 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 | 0 | if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) |
583 | 0 | weight_segment = |
584 | 0 | (double)(cr->actual_num_seg1_blocks + cr->actual_num_seg2_blocks) / |
585 | 0 | num8x8bl; |
586 | 0 | cr->weight_segment = weight_segment; |
587 | 0 | if (cr->content_mode == 0) { |
588 | 0 | cr->actual_num_seg1_blocks = |
589 | 0 | cr->percent_refresh * cm->mi_rows * cm->mi_cols / 100; |
590 | 0 | cr->actual_num_seg2_blocks = 0; |
591 | 0 | cr->weight_segment = (double)(cr->actual_num_seg1_blocks) / num8x8bl; |
592 | 0 | } |
593 | 0 | } |
594 | | |
595 | | // Setup cyclic background refresh: set delta q and segmentation map. |
596 | 0 | void vp9_cyclic_refresh_setup(VP9_COMP *const cpi) { |
597 | 0 | VP9_COMMON *const cm = &cpi->common; |
598 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
599 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
600 | 0 | struct segmentation *const seg = &cm->seg; |
601 | 0 | int scene_change_detected = |
602 | 0 | cpi->rc.high_source_sad || |
603 | 0 | (cpi->use_svc && cpi->svc.high_source_sad_superframe); |
604 | 0 | if (cm->current_video_frame == 0) cr->low_content_avg = 0.0; |
605 | | // Reset if resoluton change has occurred. |
606 | 0 | if (cpi->resize_pending != 0) vp9_cyclic_refresh_reset_resize(cpi); |
607 | 0 | if (!cr->apply_cyclic_refresh || (cpi->force_update_segmentation) || |
608 | 0 | scene_change_detected) { |
609 | | // Set segmentation map to 0 and disable. |
610 | 0 | unsigned char *const seg_map = cpi->segmentation_map; |
611 | 0 | memset(seg_map, 0, cm->mi_rows * cm->mi_cols); |
612 | 0 | vp9_disable_segmentation(&cm->seg); |
613 | 0 | if (cm->frame_type == KEY_FRAME || scene_change_detected) { |
614 | 0 | memset(cr->last_coded_q_map, MAXQ, |
615 | 0 | cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map)); |
616 | 0 | cr->sb_index = 0; |
617 | 0 | cr->reduce_refresh = 0; |
618 | 0 | cr->counter_encode_maxq_scene_change = 0; |
619 | 0 | } |
620 | 0 | return; |
621 | 0 | } else { |
622 | 0 | int qindex_delta = 0; |
623 | 0 | int qindex2; |
624 | 0 | const double q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); |
625 | 0 | cr->counter_encode_maxq_scene_change++; |
626 | 0 | 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 | 0 | 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 | 0 | cr->thresh_dist_sb = ((int64_t)(q * q)) << 2; |
634 | | |
635 | | // Set up segmentation. |
636 | | // Clear down the segment map. |
637 | 0 | vp9_enable_segmentation(&cm->seg); |
638 | 0 | vp9_clearall_segfeatures(seg); |
639 | | // Select delta coding method. |
640 | 0 | 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 | 0 | vp9_disable_segfeature(seg, CR_SEGMENT_ID_BASE, SEG_LVL_ALT_Q); |
651 | | // Use segment BOOST1 for in-frame Q adjustment. |
652 | 0 | vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST1, SEG_LVL_ALT_Q); |
653 | | // Use segment BOOST2 for more aggressive in-frame Q adjustment. |
654 | 0 | vp9_enable_segfeature(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q); |
655 | | |
656 | | // Set the q delta for segment BOOST1. |
657 | 0 | qindex_delta = compute_deltaq(cpi, cm->base_qindex, cr->rate_ratio_qdelta); |
658 | 0 | cr->qindex_delta[1] = qindex_delta; |
659 | | |
660 | | // Compute rd-mult for segment BOOST1. |
661 | 0 | qindex2 = clamp(cm->base_qindex + cm->y_dc_delta_q + qindex_delta, 0, MAXQ); |
662 | |
|
663 | 0 | cr->rdmult = vp9_compute_rd_mult(cpi, qindex2); |
664 | |
|
665 | 0 | 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 | 0 | qindex_delta = compute_deltaq( |
669 | 0 | cpi, cm->base_qindex, |
670 | 0 | VPXMIN(CR_MAX_RATE_TARGET_RATIO, |
671 | 0 | 0.1 * cr->rate_boost_fac * cr->rate_ratio_qdelta)); |
672 | 0 | cr->qindex_delta[2] = qindex_delta; |
673 | 0 | vp9_set_segdata(seg, CR_SEGMENT_ID_BOOST2, SEG_LVL_ALT_Q, qindex_delta); |
674 | | |
675 | | // Update the segmentation and refresh map. |
676 | 0 | cyclic_refresh_update_map(cpi); |
677 | 0 | } |
678 | 0 | } |
679 | | |
680 | 0 | int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr) { |
681 | 0 | return cr->rdmult; |
682 | 0 | } |
683 | | |
684 | 0 | void vp9_cyclic_refresh_reset_resize(VP9_COMP *const cpi) { |
685 | 0 | const VP9_COMMON *const cm = &cpi->common; |
686 | 0 | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
687 | 0 | memset(cr->map, 0, cm->mi_rows * cm->mi_cols); |
688 | 0 | memset(cr->last_coded_q_map, MAXQ, |
689 | 0 | cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map)); |
690 | 0 | cr->sb_index = 0; |
691 | 0 | cpi->refresh_golden_frame = 1; |
692 | 0 | cpi->refresh_alt_ref_frame = 1; |
693 | 0 | cr->counter_encode_maxq_scene_change = 0; |
694 | 0 | } |
695 | | |
696 | 0 | void vp9_cyclic_refresh_limit_q(const VP9_COMP *cpi, int *q) { |
697 | 0 | 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 | 0 | if (cr->percent_refresh > 0 && cpi->rc.q_1_frame - *q > 8) { |
701 | 0 | *q = cpi->rc.q_1_frame - 8; |
702 | 0 | } |
703 | 0 | } |