Line data Source code
1 : /*
2 : * Copyright(c) 2019 Intel Corporation
3 : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 : */
5 :
6 : #include "EbUtility.h"
7 :
8 : #include "EbPictureControlSet.h"
9 : #include "RateControlModel.h"
10 : #include "RateControlGopInfo.h"
11 :
12 : /*
13 : * @private
14 : * @function get_inter_qp_for_size. Helper to extract the suggested QP from the
15 : * prediction model for a desired size
16 : * @param {EbRateControlModel*} model_ptr.
17 : * @param {uint32_t} desired_size.
18 : * @return {uint32_t}.
19 : */
20 : static uint32_t get_inter_qp_for_size(EbRateControlModel *model_ptr, uint32_t desired_size);
21 :
22 : /*
23 : * @private
24 : * @function record_new_gop. Take into account a new group of picture in the
25 : * model
26 : * @param {EbRateControlModel*} model_ptr.
27 : * @param {PictureParentControlSet*} picture_ptr. Picture holding the intra frame.
28 : * @return {void}.
29 : */
30 : static void record_new_gop(EbRateControlModel *model_ptr, PictureParentControlSet *picture_ptr);
31 :
32 : /*
33 : * Average size in bits for and intra frame per QP for a 1920x1080 reference video clip
34 : */
35 : static const size_t DEFAULT_REF_INTRA_PICTURE_COMPRESSION_RATIO[64] = {
36 : 14834056, 14589237, 14344418, 14099599, 13854780, 13609961, 13365142,
37 : 13120323, 12875503, 12492037, 12108571, 11725105, 11341639, 10958173,
38 : 10574707, 10191241, 9942206, 9693170, 9444135, 9195099, 8946064, 8697028,
39 : 8447993, 8198957, 7949922, 7700886, 7440985, 7181083, 6921182, 6661280,
40 : 6401379, 6141478, 5881576, 5621675, 5361773, 5101872, 4841971, 4582069,
41 : 4322168, 4062266, 3802365, 3542464, 3282562, 3022661, 2762759, 2502858,
42 : 2398196, 2293535, 2188873, 2084211, 1979550, 1874888, 1770226, 1665565,
43 : 1560903, 1456241, 1351580, 1246918, 1142256, 1037595, 932933, 828271,
44 : 723610, 618948
45 : };
46 :
47 : /*
48 : * Average size in bits for and inter frame per QP for a 1920x1080 reference video clip
49 : */
50 : static const size_t DEFAULT_REF_INTER_PICTURE_COMPRESSION_RATIO[64] = {
51 : 12459127, 10817116, 9175105, 7533094, 5891083, 4249072, 3819718, 3390366,
52 : 2961014, 2531662, 2102309, 1931469, 1760631, 1589793, 1418955, 1248117,
53 : 1165879, 1083646, 1001413, 919180, 836947, 783885, 730823, 677761,
54 : 624699, 571634, 531742, 491850, 451958, 412066, 372174, 344015,
55 : 315858, 287701, 259544, 231387, 213604, 195823, 178042, 160261, 142480,
56 : 131278, 120077, 108876, 97675, 86474, 79681, 72892, 66103, 59314,
57 : 52525, 48203, 43882, 39561, 35240, 30919, 28284, 25651, 23018,
58 : 20385, 17752, 15465, 13178, 10891
59 : };
60 :
61 2 : static void rate_control_model_dctor(EbPtr p) {
62 2 : EbRateControlModel *obj = (EbRateControlModel*)p;
63 2 : EB_FREE(obj->gop_infos);
64 2 : }
65 :
66 2 : EbErrorType rate_control_model_ctor(EbRateControlModel *model_ptr) {
67 :
68 2 : model_ptr->dctor = rate_control_model_dctor;
69 2 : EB_MEMCPY(model_ptr->intra_size_predictions, (void*)DEFAULT_REF_INTRA_PICTURE_COMPRESSION_RATIO, sizeof(DEFAULT_REF_INTRA_PICTURE_COMPRESSION_RATIO));
70 2 : EB_MEMCPY(model_ptr->inter_size_predictions, (void*)DEFAULT_REF_INTER_PICTURE_COMPRESSION_RATIO, sizeof(DEFAULT_REF_INTER_PICTURE_COMPRESSION_RATIO));
71 :
72 2 : return EB_ErrorNone;
73 : }
74 :
75 2 : EbErrorType rate_control_model_init(EbRateControlModel *model_ptr, SequenceControlSet *sequenceControlSetPtr) {
76 2 : uint32_t number_of_frame = (uint32_t)sequenceControlSetPtr->static_config.frames_to_be_encoded;
77 :
78 2 : model_ptr->desired_bitrate = sequenceControlSetPtr->static_config.target_bit_rate;
79 2 : model_ptr->frame_rate = sequenceControlSetPtr->static_config.frame_rate >> 16;
80 2 : model_ptr->width = sequenceControlSetPtr->seq_header.max_frame_width;
81 2 : model_ptr->height = sequenceControlSetPtr->seq_header.max_frame_height;
82 2 : model_ptr->pixels = model_ptr->width * model_ptr->height;
83 : //free old
84 2 : EB_FREE(model_ptr->gop_infos);
85 :
86 2 : EB_CALLOC(model_ptr->gop_infos, number_of_frame, sizeof(EbRateControlGopInfo));
87 2 : model_ptr->intra_period = sequenceControlSetPtr->static_config.intra_period_length;
88 :
89 2 : return EB_ErrorNone;
90 : }
91 :
92 0 : EbErrorType rate_control_update_model(EbRateControlModel *model_ptr, PictureParentControlSet *picture_ptr) {
93 0 : uint64_t size = picture_ptr->total_num_bits;
94 0 : EbRateControlGopInfo *gop = get_gop_infos(model_ptr->gop_infos, picture_ptr->picture_number);
95 :
96 0 : model_ptr->total_bytes += size;
97 0 : model_ptr->reported_frames++;
98 0 : gop->actual_size += size;
99 0 : gop->reported_frames++;
100 :
101 0 : if (gop->reported_frames == gop->length) {
102 0 : float variation = 1;
103 :
104 0 : model_ptr->model_variation_reported++;
105 0 : if (gop->actual_size > gop->desired_size) {
106 0 : variation = (float)(-((int64_t)gop->actual_size / (int64_t)gop->desired_size));
107 0 : } else if (gop->desired_size > gop->actual_size)
108 0 : variation = (float)(((int64_t)(gop->desired_size / (int64_t)gop->actual_size)));
109 0 : variation = CLIP3(-100, 100, variation);
110 0 : variation -= gop->model_variation;
111 :
112 0 : model_ptr->model_variation = model_ptr->model_variation * (model_ptr->model_variation_reported - 1) / model_ptr->model_variation_reported + variation / model_ptr->model_variation_reported;
113 : }
114 :
115 0 : return EB_ErrorNone;
116 : }
117 :
118 0 : uint8_t rate_control_get_quantizer(EbRateControlModel *model_ptr, PictureParentControlSet *picture_ptr) {
119 0 : FrameType type = picture_ptr->frm_hdr.frame_type;
120 :
121 0 : if (type == INTRA_ONLY_FRAME || type == KEY_FRAME)
122 0 : record_new_gop(model_ptr, picture_ptr);
123 0 : EbRateControlGopInfo *gop = get_gop_infos(model_ptr->gop_infos, picture_ptr->picture_number);
124 :
125 0 : return gop->qp;
126 : }
127 :
128 0 : uint32_t get_inter_qp_for_size(EbRateControlModel *model_ptr, uint32_t desired_size) {
129 : uint8_t qp;
130 :
131 0 : for (qp = 0; qp < MAX_QP_VALUE; qp++) {
132 0 : float size = (float)model_ptr->inter_size_predictions[qp];
133 :
134 0 : size = (size / (1920 * 1080)) * model_ptr->pixels;
135 : // Scale size for current resolution
136 0 : if (desired_size > size)
137 0 : break;
138 : }
139 :
140 0 : return qp;
141 : }
142 :
143 0 : static void record_new_gop(EbRateControlModel *model_ptr, PictureParentControlSet *picture_ptr) {
144 0 : uint64_t pictureNumber = picture_ptr->picture_number;
145 0 : EbRateControlGopInfo *gop = &model_ptr->gop_infos[pictureNumber];
146 :
147 0 : gop->index = pictureNumber;
148 0 : gop->exists = EB_TRUE;
149 0 : gop->desired_size = get_gop_size_in_bytes(model_ptr);
150 0 : gop->model_variation = (int32_t)model_ptr->model_variation;
151 :
152 0 : uint32_t size = (int32_t)(gop->desired_size / model_ptr->intra_period);
153 :
154 0 : gop->qp = get_inter_qp_for_size(model_ptr, size);
155 :
156 : // Update length in gopinfos
157 0 : if (pictureNumber != 0) {
158 0 : EbRateControlGopInfo *previousGop = get_gop_infos(model_ptr->gop_infos, pictureNumber - 1);
159 :
160 0 : previousGop->length = gop->index - previousGop->index;
161 : }
162 0 : }
163 :
164 0 : uint32_t get_gop_size_in_bytes(EbRateControlModel *model_ptr) {
165 0 : uint32_t gop_per_second = (model_ptr->frame_rate << 8) / model_ptr->intra_period;
166 0 : uint32_t gop_size = ((model_ptr->desired_bitrate << 8) / gop_per_second);
167 0 : uint32_t desired_total_bytes = (uint32_t)((model_ptr->desired_bitrate / model_ptr->frame_rate) * model_ptr->reported_frames);
168 0 : int64_t delta_bytes = desired_total_bytes - model_ptr->total_bytes;
169 0 : float extra = 1;
170 :
171 0 : if (delta_bytes < 0) {
172 0 : extra = (float)((float)delta_bytes / gop_size) + 1;
173 0 : } else if (delta_bytes > 0)
174 0 : extra = (float)((float)delta_bytes / gop_size) + 1;
175 0 : if (model_ptr->model_variation < 0) {
176 0 : gop_size = (uint32_t)(gop_size / -(model_ptr->model_variation));
177 0 : } else if (model_ptr->model_variation > 0)
178 0 : gop_size = (uint32_t)(gop_size * model_ptr->model_variation);
179 0 : if (model_ptr->model_variation_reported > 5) {
180 0 : if (extra < 0) {
181 0 : gop_size = (uint32_t)(gop_size / -extra);
182 0 : } else if (extra > 0)
183 0 : gop_size = (uint32_t)(gop_size * extra);
184 : }
185 :
186 0 : return gop_size + 1;
187 : }
|