2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
12 #ifndef VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_
13 #define VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_
15 #include "vpx/vpx_integer.h"
16 #include "vp9/common/vp9_blockd.h"
22 // The segment ids used in cyclic refresh: from base (no boost) to increasing
23 // boost (higher delta-qp).
24 #define CR_SEGMENT_ID_BASE 0
25 #define CR_SEGMENT_ID_BOOST1 1
26 #define CR_SEGMENT_ID_BOOST2 2
28 // Maximum rate target ratio for setting segment delta-qp.
29 #define CR_MAX_RATE_TARGET_RATIO 4.0
31 struct CYCLIC_REFRESH {
32 // Percentage of blocks per frame that are targeted as candidates
33 // for cyclic refresh.
35 // Maximum q-delta as percentage of base q.
37 // Superblock starting index for cycling through the frame.
39 // Controls how long block will need to wait to be refreshed again, in
40 // excess of the cycle time, i.e., in the case of all zero motion, block
41 // will be refreshed every (100/percent_refresh + time_for_refresh) frames.
43 // Target number of (8x8) blocks that are set for delta-q.
44 int target_num_seg_blocks;
45 // Actual number of (8x8) blocks that were applied delta-q.
46 int actual_num_seg1_blocks;
47 int actual_num_seg2_blocks;
48 // RD mult. parameters for segment 1.
50 // Cyclic refresh map.
52 // Map of the last q a block was coded at.
53 uint8_t *last_coded_q_map;
54 // Thresholds applied to the projected rate/distortion of the coding block,
55 // when deciding whether block should be refreshed.
56 int64_t thresh_rate_sb;
57 int64_t thresh_dist_sb;
58 // Threshold applied to the motion vector (in units of 1/8 pel) of the
59 // coding block, when deciding whether block should be refreshed.
60 int16_t motion_thresh;
61 // Rate target ratio to set q delta.
62 double rate_ratio_qdelta;
63 // Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
65 double low_content_avg;
71 typedef struct CYCLIC_REFRESH CYCLIC_REFRESH;
73 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols);
75 void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr);
77 // Estimate the bits, incorporating the delta-q from segment 1, after encoding
79 int vp9_cyclic_refresh_estimate_bits_at_q(const struct VP9_COMP *cpi,
80 double correction_factor);
82 // Estimate the bits per mb, for a given q = i and a corresponding delta-q
83 // (for segment 1), prior to encoding the frame.
84 int vp9_cyclic_refresh_rc_bits_per_mb(const struct VP9_COMP *cpi, int i,
85 double correction_factor);
87 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
88 // check if we should reset the segment_id, and update the cyclic_refresh map
89 // and segmentation map.
90 void vp9_cyclic_refresh_update_segment(struct VP9_COMP *const cpi,
91 MB_MODE_INFO *const mbmi,
92 int mi_row, int mi_col, BLOCK_SIZE bsize,
93 int64_t rate, int64_t dist, int skip);
95 // Update the segmentation map, and related quantities: cyclic refresh map,
96 // refresh sb_index, and target number of blocks to be refreshed.
97 void vp9_cyclic_refresh_update__map(struct VP9_COMP *const cpi);
99 // Update the actual number of blocks that were applied the segment delta q.
100 void vp9_cyclic_refresh_postencode(struct VP9_COMP *const cpi);
102 // Set golden frame update interval, for non-svc 1 pass CBR mode.
103 void vp9_cyclic_refresh_set_golden_update(struct VP9_COMP *const cpi);
105 // Check if we should not update golden reference, based on past refresh stats.
106 void vp9_cyclic_refresh_check_golden_update(struct VP9_COMP *const cpi);
108 // Set/update global/frame level refresh parameters.
109 void vp9_cyclic_refresh_update_parameters(struct VP9_COMP *const cpi);
111 // Setup cyclic background refresh: set delta q and segmentation map.
112 void vp9_cyclic_refresh_setup(struct VP9_COMP *const cpi);
114 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr);
116 void vp9_cyclic_refresh_reset_resize(struct VP9_COMP *const cpi);
118 static INLINE int cyclic_refresh_segment_id_boosted(int segment_id) {
119 return segment_id == CR_SEGMENT_ID_BOOST1 ||
120 segment_id == CR_SEGMENT_ID_BOOST2;
123 static INLINE int cyclic_refresh_segment_id(int segment_id) {
124 if (segment_id == CR_SEGMENT_ID_BOOST1)
125 return CR_SEGMENT_ID_BOOST1;
126 else if (segment_id == CR_SEGMENT_ID_BOOST2)
127 return CR_SEGMENT_ID_BOOST2;
129 return CR_SEGMENT_ID_BASE;
136 #endif // VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_