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