1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbRateControl_h
7 #define EbRateControl_h
8 
9 #include "EbDefinitions.h"
10 #include "EbSystemResourceManager.h"
11 #include "EbSvtVp9Enc.h"
12 #include "EbPictureControlSet.h"
13 
14 #define CCOEFF_INIT_FACT              2
15 #define SAD_CLIP_COEFF                5
16 // 88 + 3*16*8
17 #define SLICE_HEADER_BITS_NUM       104
18 #define RC_PRECISION                16
19 #define RC_PRECISION_OFFSET         (1 << (RC_PRECISION - 1))
20 
21 #define OVERSHOOT_STAT_PRINT             0 // Do not remove.
22                                            // For printing overshooting percentages for both RC and fixed QP.
23                                            // Target rate and and max buffer size should be set properly even for fixed QP.
24                                            // Disabled by default.
25 #if OVERSHOOT_STAT_PRINT
26 #define CODED_FRAMES_STAT_QUEUE_MAX_DEPTH   10000
27 #endif
28 
29 #define ADAPTIVE_PERCENTAGE   1
30 #define RC_UPDATE_TARGET_RATE 1
31 
32 static const uint32_t  rate_percentage_layer_array[EB_MAX_TEMPORAL_LAYERS][EB_MAX_TEMPORAL_LAYERS] = {
33     {100,  0,  0,  0,  0,  0 },
34     { 70, 30,  0,  0,  0,  0 },
35     { 70, 15, 15,  0,  0,  0 },
36     { 55, 15, 15, 15,  0,  0 },
37     { 40, 15, 15, 15, 15,  0 },
38     { 30, 10, 15, 15, 15, 15 }
39 };
40 
41 // range from 0 to 51
42 // precision is 16 bits
43 static const uint64_t two_to_power_qp_over_three[] = {
44          0x10000,      0x1428A,     0x19660,     0x20000,
45          0x28514,      0x32CC0,     0x40000,     0x50A29,
46          0x65980,      0x80000,     0xA1451,     0xCB2FF,
47         0x100000,     0x1428A3,    0x1965FF,    0x200000,
48         0x285146,     0x32CBFD,    0x400000,    0x50A28C,
49         0x6597FB,     0x800000,    0xA14518,    0xCB2FF5,
50        0x1000000,    0x1428A30,   0x1965FEA,   0x2000000,
51        0x285145F,    0x32CBFD5,   0x4000000,   0x50A28BE,
52        0x6597FA9,    0x8000000,   0xA14517D,   0xCB2FF53,
53       0x10000000,   0x1428A2FA,  0x1965FEA5,  0x20000000,
54       0x285145F3,   0x32CBFD4A,  0x40000000,  0x50A28BE6,
55       0x6597FA95,   0x80000000,  0xA14517CC,  0xCB2FF52A,
56      0x100000000,  0x1428A2F99, 0x1965FEA54, 0x200000000
57 };
58 /**************************************
59  * Input Port Types
60  **************************************/
61 typedef enum RateControlInputPortTypes
62 {
63     RATE_CONTROL_INPUT_PORT_PICTURE_MANAGER = 0,
64     RATE_CONTROL_INPUT_PORT_PACKETIZATION   = 1,
65     RATE_CONTROL_INPUT_PORT_ENTROPY_CODING  = 2,
66     RATE_CONTROL_INPUT_PORT_TOTAL_COUNT     = 3,
67     RATE_CONTROL_INPUT_PORT_INVALID         = ~0,
68 } RateControlInputPortTypes;
69 
70 /**************************************
71  * Input Port Config
72  **************************************/
73 typedef struct RateControlPorts
74 {
75     RateControlInputPortTypes type;
76     uint32_t                  count;
77 } RateControlPorts;
78 
79 /**************************************
80  * Coded Frames Stats
81  **************************************/
82 typedef struct CodedFramesStatsEntry
83 {
84     uint64_t picture_number;
85     int64_t  frame_total_bit_actual;
86     EB_BOOL  end_of_sequence_flag;
87 } CodedFramesStatsEntry;
88 /**************************************
89  * Context
90  **************************************/
91 typedef struct RateControlLayerContext
92 {
93     uint64_t   previous_frame_distortion_me;
94     uint64_t   previous_frame_bit_actual;
95     uint64_t   previous_framequantized_coeff_bit_actual;
96     EB_BOOL    feedback_arrived;
97 
98     uint64_t   target_bit_rate;
99     uint64_t   frame_rate;
100     uint64_t   channel_bit_rate;
101 
102     uint64_t   previous_bit_constraint;
103     uint64_t   bit_constraint;
104     uint64_t   ec_bit_constraint;
105     uint64_t   previous_ec_bits;
106     int64_t    dif_total_and_ec_bits;
107 
108     int64_t    bit_diff;
109     uint32_t   coeff_averaging_weight1;
110     uint32_t   coeff_averaging_weight2; // coeff_averaging_weight2 = 16- coeff_averaging_weight1
111     //Ccoeffs have 2*RC_PRECISION precision
112     int64_t    c_coeff;
113     int64_t    previous_c_coeff;
114     //Kcoeffs have RC_PRECISION precision
115     uint64_t   k_coeff;
116     uint64_t   previous_k_coeff;
117 
118     //delta_qp_fraction has RC_PRECISION precision
119     int64_t    delta_qp_fraction;
120     uint32_t   previous_frame_qp;
121     uint32_t   calculated_frame_qp;
122     uint32_t   previous_calculated_frame_qp;
123     uint32_t   area_in_pixel;
124     uint32_t   previous_frame_average_qp;
125 
126     //total_mad has RC_PRECISION precision
127     uint64_t   total_mad;
128 
129     uint32_t   first_frame;
130     uint32_t   first_non_intra_frame;
131     uint32_t   same_distortion_count;
132     uint32_t   frame_same_distortion_min_qp_count;
133     uint32_t   critical_states;
134 
135     uint32_t   max_qp;
136     uint32_t   temporal_index;
137 
138     uint64_t   alpha;
139 
140 } RateControlLayerContext;
141 
142 typedef struct RateControlIntervalParamContext
143 {
144     uint64_t                     first_poc;
145     uint64_t                     last_poc;
146     EB_BOOL                      in_use;
147     EB_BOOL                      was_used;
148     uint64_t                     processed_frames_number;
149     EB_BOOL                      last_gop;
150     RateControlLayerContext    **rate_control_layer_array;
151 
152     int64_t                      virtual_buffer_level;
153     int64_t                      previous_virtual_buffer_level;
154     uint32_t                     intra_frames_qp;
155     uint8_t                      intra_frames_qp_bef_scal;
156 
157     uint32_t                     next_gop_intra_frame_qp;
158     uint64_t                     first_pic_pred_bits;
159     uint64_t                     first_pic_actual_bits;
160     uint16_t                     first_pic_pred_qp;
161     uint16_t                     first_pic_actual_qp;
162     EB_BOOL                      first_pic_actual_qp_assigned;
163     EB_BOOL                      scene_change_in_gop;
164     EB_BOOL                      min_target_rate_assigned;
165     int64_t                      extra_ap_bit_ratio_i;
166 
167 } RateControlIntervalParamContext;
168 
169 typedef struct HighLevelRateControlContext
170 {
171     uint64_t target_bit_rate;
172     uint64_t frame_rate;
173     uint64_t channel_bit_rate_per_frame;
174     uint64_t channel_bit_rate_per_sw;
175     uint64_t bit_constraint_per_sw;
176     uint64_t pred_bits_ref_qpPerSw[MAX_REF_QP_NUM];
177 #if RC_UPDATE_TARGET_RATE
178     uint32_t prev_intra_selected_ref_qp;
179     uint32_t prev_intra_org_selected_ref_qp;
180     uint64_t previous_updated_bit_constraint_per_sw;
181 #endif
182 } HighLevelRateControlContext;
183 
184 typedef struct RateControlContext
185 {
186     EbFifo                            *rate_control_input_tasks_fifo_ptr;
187     EbFifo                            *rate_control_output_results_fifo_ptr;
188 
189     HighLevelRateControlContext       *high_level_rate_control_ptr;
190 
191     RateControlIntervalParamContext  **rate_control_param_queue;
192     uint64_t                           rate_control_param_queue_head_index;
193 
194     uint64_t                           frame_rate;
195 
196     uint64_t                           virtual_buffer_size;
197 
198     int64_t                            virtual_buffer_level_initial_value;
199     int64_t                            previous_virtual_buffer_level;
200 
201     int64_t                            virtual_buffer_level;
202 
203    //Virtual Buffer Thresholds
204     int64_t                            vb_fill_threshold1;
205     int64_t                            vb_fill_threshold2;
206 
207     // Rate Control Previous Bits Queue
208 #if OVERSHOOT_STAT_PRINT
209     CodedFramesStatsEntry            **coded_frames_stat_queue;
210     uint32_t                           coded_frames_stat_queue_head_index;
211     uint32_t                           coded_frames_stat_queue_tail_index;
212 
213     uint64_t                           total_bit_actual_per_sw;
214     uint64_t                           max_bit_actual_per_sw;
215     uint64_t                           max_bit_actual_per_gop;
216     uint64_t                           min_bit_actual_per_gop;
217     uint64_t                           avg_bit_actual_per_gop;
218 
219 #endif
220 
221     uint64_t                           rate_average_periodin_frames;
222     uint32_t                           base_layer_frames_avg_qp;
223     uint32_t                           base_layer_intra_frames_avg_qp;
224 
225     EB_BOOL                            end_of_sequence_region;
226 
227     uint32_t                           intra_coef_rate;
228 
229     uint64_t                           frames_in_interval [EB_MAX_TEMPORAL_LAYERS];
230     int64_t                            extra_bits;
231     int64_t                            extra_bits_gen;
232     int16_t                            max_rate_adjust_delta_qp;
233 
234     uint32_t                           qp_scaling_map[EB_MAX_TEMPORAL_LAYERS][MAX_REF_QP_NUM];
235     uint32_t                           qp_scaling_map_I_SLICE[MAX_REF_QP_NUM];
236 
237 } RateControlContext;
238 
239 /**************************************
240  * Extern Function Declarations
241  **************************************/
242 extern EbErrorType eb_vp9_rate_control_layer_context_ctor(
243     RateControlLayerContext **entry_dbl_ptr);
244 
245 extern EbErrorType eb_vp9_rate_control_interval_param_context_ctor(
246     RateControlIntervalParamContext **entry_dbl_ptr);
247 
248 extern EbErrorType eb_vp9_rate_control_coded_frames_stats_context_ctor(
249     CodedFramesStatsEntry **entry_dbl_ptr,
250     uint64_t                picture_number);
251 
252 extern EbErrorType eb_vp9_rate_control_context_ctor(
253     RateControlContext **context_dbl_ptr,
254     EbFifo              *rate_control_input_tasks_fifo_ptr,
255     EbFifo              *rate_control_output_results_fifo_ptr,
256     int32_t              intra_period);
257 
258 extern void* eb_vp9_rate_control_kernel(void *input_ptr);
259 
260 #endif // EbRateControl_h
261