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 #ifndef VPX_VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_
12 #define VPX_VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_
13
14 #include "vpx/vpx_integer.h"
15 #include "vp9/common/vp9_blockd.h"
16 #include "vp9/encoder/vp9_block.h"
17 #include "vp9/encoder/vp9_skin_detection.h"
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 // The segment ids used in cyclic refresh: from base (no boost) to increasing
24 // boost (higher delta-qp).
25 #define CR_SEGMENT_ID_BASE 0
26 #define CR_SEGMENT_ID_BOOST1 1
27 #define CR_SEGMENT_ID_BOOST2 2
28
29 // Maximum rate target ratio for setting segment delta-qp.
30 #define CR_MAX_RATE_TARGET_RATIO 4.0
31
32 struct CYCLIC_REFRESH {
33 // Percentage of blocks per frame that are targeted as candidates
34 // for cyclic refresh.
35 int percent_refresh;
36 // Maximum q-delta as percentage of base q.
37 int max_qdelta_perc;
38 // Superblock starting index for cycling through the frame.
39 int sb_index;
40 // Controls how long block will need to wait to be refreshed again, in
41 // excess of the cycle time, i.e., in the case of all zero motion, block
42 // will be refreshed every (100/percent_refresh + time_for_refresh) frames.
43 int time_for_refresh;
44 // Target number of (8x8) blocks that are set for delta-q.
45 int target_num_seg_blocks;
46 // Actual number of (8x8) blocks that were applied delta-q.
47 int actual_num_seg1_blocks;
48 int actual_num_seg2_blocks;
49 // RD mult. parameters for segment 1.
50 int rdmult;
51 // Cyclic refresh map.
52 signed char *map;
53 // Map of the last q a block was coded at.
54 uint8_t *last_coded_q_map;
55 // Thresholds applied to the projected rate/distortion of the coding block,
56 // when deciding whether block should be refreshed.
57 int64_t thresh_rate_sb;
58 int64_t thresh_dist_sb;
59 // Threshold applied to the motion vector (in units of 1/8 pel) of the
60 // coding block, when deciding whether block should be refreshed.
61 int16_t motion_thresh;
62 // Rate target ratio to set q delta.
63 double rate_ratio_qdelta;
64 // Boost factor for rate target ratio, for segment CR_SEGMENT_ID_BOOST2.
65 int rate_boost_fac;
66 double low_content_avg;
67 int qindex_delta[3];
68 int reduce_refresh;
69 double weight_segment;
70 int apply_cyclic_refresh;
71 int counter_encode_maxq_scene_change;
72 int skip_flat_static_blocks;
73 int content_mode;
74 };
75
76 struct VP9_COMP;
77
78 typedef struct CYCLIC_REFRESH CYCLIC_REFRESH;
79
80 CYCLIC_REFRESH *vp9_cyclic_refresh_alloc(int mi_rows, int mi_cols);
81
82 void vp9_cyclic_refresh_free(CYCLIC_REFRESH *cr);
83
84 // Estimate the bits, incorporating the delta-q from segment 1, after encoding
85 // the frame.
86 int vp9_cyclic_refresh_estimate_bits_at_q(const struct VP9_COMP *cpi,
87 double correction_factor);
88
89 // Estimate the bits per mb, for a given q = i and a corresponding delta-q
90 // (for segment 1), prior to encoding the frame.
91 int vp9_cyclic_refresh_rc_bits_per_mb(const struct VP9_COMP *cpi, int i,
92 double correction_factor);
93
94 // Prior to coding a given prediction block, of size bsize at (mi_row, mi_col),
95 // check if we should reset the segment_id, and update the cyclic_refresh map
96 // and segmentation map.
97 void vp9_cyclic_refresh_update_segment(struct VP9_COMP *const cpi,
98 MODE_INFO *const mi, int mi_row,
99 int mi_col, BLOCK_SIZE bsize,
100 int64_t rate, int64_t dist, int skip,
101 struct macroblock_plane *const p);
102
103 void vp9_cyclic_refresh_update_sb_postencode(struct VP9_COMP *const cpi,
104 const MODE_INFO *const mi,
105 int mi_row, int mi_col,
106 BLOCK_SIZE bsize);
107
108 // From the just encoded frame: update the actual number of blocks that were
109 // applied the segment delta q, and the amount of low motion in the frame.
110 // Also check conditions for forcing golden update, or preventing golden
111 // update if the period is up.
112 void vp9_cyclic_refresh_postencode(struct VP9_COMP *const cpi);
113
114 // Set golden frame update interval, for non-svc 1 pass CBR mode.
115 void vp9_cyclic_refresh_set_golden_update(struct VP9_COMP *const cpi);
116
117 // Set/update global/frame level refresh parameters.
118 void vp9_cyclic_refresh_update_parameters(struct VP9_COMP *const cpi);
119
120 // Setup cyclic background refresh: set delta q and segmentation map.
121 void vp9_cyclic_refresh_setup(struct VP9_COMP *const cpi);
122
123 int vp9_cyclic_refresh_get_rdmult(const CYCLIC_REFRESH *cr);
124
125 void vp9_cyclic_refresh_reset_resize(struct VP9_COMP *const cpi);
126
cyclic_refresh_segment_id_boosted(int segment_id)127 static INLINE int cyclic_refresh_segment_id_boosted(int segment_id) {
128 return segment_id == CR_SEGMENT_ID_BOOST1 ||
129 segment_id == CR_SEGMENT_ID_BOOST2;
130 }
131
cyclic_refresh_segment_id(int segment_id)132 static INLINE int cyclic_refresh_segment_id(int segment_id) {
133 if (segment_id == CR_SEGMENT_ID_BOOST1)
134 return CR_SEGMENT_ID_BOOST1;
135 else if (segment_id == CR_SEGMENT_ID_BOOST2)
136 return CR_SEGMENT_ID_BOOST2;
137 else
138 return CR_SEGMENT_ID_BASE;
139 }
140
141 void vp9_cyclic_refresh_limit_q(const struct VP9_COMP *cpi, int *q);
142
143 #ifdef __cplusplus
144 } // extern "C"
145 #endif
146
147 #endif // VPX_VP9_ENCODER_VP9_AQ_CYCLICREFRESH_H_
148