/src/libavc/encoder/irc_bit_allocation.c
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2015 The Android Open Source Project |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at: |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | ***************************************************************************** |
18 | | * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore |
19 | | */ |
20 | | |
21 | | /** Includes */ |
22 | | #include <stdio.h> |
23 | | #include <string.h> |
24 | | #include "irc_datatypes.h" |
25 | | #include "irc_mem_req_and_acq.h" |
26 | | #include "irc_common.h" |
27 | | #include "irc_cntrl_param.h" |
28 | | #include "irc_fixed_point_error_bits.h" |
29 | | #include "irc_rd_model.h" |
30 | | #include "irc_est_sad.h" |
31 | | #include "irc_picture_type.h" |
32 | | #include "irc_bit_allocation.h" |
33 | | #include "irc_trace_support.h" |
34 | | |
35 | | /** Macros **/ |
36 | | #define MIN(x,y) ((x) < (y))? (x) : (y) |
37 | | |
38 | | /* State structure for bit allocation */ |
39 | | typedef struct |
40 | | { |
41 | | /* using var_q number as it can cross 31 bits for large intra frameinterval */ |
42 | | number_t vq_rem_bits_in_period; |
43 | | |
44 | | /* Storing inputs */ |
45 | | WORD32 i4_tot_frms_in_gop; |
46 | | |
47 | | WORD32 i4_num_intra_frm_interval; |
48 | | |
49 | | WORD32 i4_bits_per_frm; |
50 | | |
51 | | } rem_bit_in_prd_t; |
52 | | |
53 | | typedef struct bit_allocation_t |
54 | | { |
55 | | rem_bit_in_prd_t s_rbip; |
56 | | |
57 | | /* A universal constant giving the relative complexity between pictures */ |
58 | | WORD32 i2_K[MAX_PIC_TYPE]; |
59 | | |
60 | | /* To get a estimate of the header bits consumed */ |
61 | | WORD32 i4_prev_frm_header_bits[MAX_PIC_TYPE]; |
62 | | |
63 | | WORD32 i4_bits_per_frm; |
64 | | |
65 | | WORD32 i4_num_gops_in_period; |
66 | | |
67 | | /* Num gops as set by rate control module */ |
68 | | WORD32 i4_actual_num_gops_in_period; |
69 | | |
70 | | number_t vq_saved_bits; |
71 | | |
72 | | WORD32 i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES]; |
73 | | |
74 | | WORD32 i4_min_bits_per_frm; |
75 | | |
76 | | /* Error bits module */ |
77 | | error_bits_handle ps_error_bits; |
78 | | |
79 | | /* Storing frame rate */ |
80 | | WORD32 i4_frame_rate; |
81 | | |
82 | | WORD32 i4_bit_rate; |
83 | | |
84 | | WORD32 ai4_peak_bit_rate[MAX_NUM_DRAIN_RATES]; |
85 | | |
86 | | } bit_allocation_t; |
87 | | |
88 | | static WORD32 get_number_of_frms_in_a_gop(pic_handling_handle ps_pic_handling) |
89 | 181k | { |
90 | 181k | WORD32 i4_tot_frms_in_gop = 0, i; |
91 | 181k | WORD32 ai4_frms_in_gop[MAX_PIC_TYPE]; |
92 | | |
93 | | /* Query the pic_handling struct for the rem frames in the period */ |
94 | 181k | irc_pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_gop); |
95 | | |
96 | | /* Get the total frms in the gop */ |
97 | 181k | i4_tot_frms_in_gop = 0; |
98 | 726k | for(i = 0; i < MAX_PIC_TYPE; i++) |
99 | 544k | { |
100 | 544k | i4_tot_frms_in_gop += ai4_frms_in_gop[i]; |
101 | 544k | } |
102 | 181k | return (i4_tot_frms_in_gop); |
103 | 181k | } |
104 | | |
105 | | static void init_rbip(rem_bit_in_prd_t *ps_rbip, |
106 | | pic_handling_handle ps_pic_handling, |
107 | | WORD32 i4_bits_per_frm, |
108 | | WORD32 i4_num_intra_frm_interval) |
109 | 40.4k | { |
110 | 40.4k | WORD32 i4_tot_frms_in_gop = get_number_of_frms_in_a_gop(ps_pic_handling); |
111 | | |
112 | | /* rem_bits_in_period = bits_per_frm * tot_frms_in_gop * num_intra_frm_interval */ |
113 | 40.4k | { |
114 | 40.4k | number_t vq_bits_per_frm, vq_tot_frms_in_gop, vq_num_intra_frm_interval; |
115 | 40.4k | number_t *pvq_rem_bits_in_period = &ps_rbip->vq_rem_bits_in_period; |
116 | | |
117 | 40.4k | SET_VAR_Q(vq_bits_per_frm, i4_bits_per_frm, 0); |
118 | 40.4k | SET_VAR_Q(vq_tot_frms_in_gop, i4_tot_frms_in_gop, 0); |
119 | 40.4k | SET_VAR_Q(vq_num_intra_frm_interval, i4_num_intra_frm_interval, 0); |
120 | | |
121 | | /* rem_bits_in_period = bits_per_frm * tot_frms_in_gop */ |
122 | 40.4k | mult32_var_q(vq_bits_per_frm, vq_tot_frms_in_gop, |
123 | 40.4k | pvq_rem_bits_in_period); |
124 | | |
125 | | /* rem_bits_in_period *= num_intra_frm_interval */ |
126 | 40.4k | mult32_var_q(vq_num_intra_frm_interval, pvq_rem_bits_in_period[0], |
127 | 40.4k | pvq_rem_bits_in_period); |
128 | 40.4k | } |
129 | | |
130 | | /* |
131 | | * Store the total number of frames in GOP value which is |
132 | | * used from module A |
133 | | */ |
134 | 40.4k | ps_rbip->i4_tot_frms_in_gop = i4_tot_frms_in_gop; |
135 | 40.4k | ps_rbip->i4_num_intra_frm_interval = i4_num_intra_frm_interval; |
136 | 40.4k | ps_rbip->i4_bits_per_frm = i4_bits_per_frm; |
137 | 40.4k | } |
138 | | |
139 | | static void check_update_rbip(rem_bit_in_prd_t *ps_rbip, |
140 | | pic_handling_handle ps_pic_handling) |
141 | 123k | { |
142 | | /* |
143 | | * NOTE: Intra frame interval changes after the first I frame that is |
144 | | * encoded in a GOP |
145 | | */ |
146 | 123k | WORD32 i4_new_tot_frms_in_gop = get_number_of_frms_in_a_gop( |
147 | 123k | ps_pic_handling); |
148 | | |
149 | 123k | if(i4_new_tot_frms_in_gop != ps_rbip->i4_tot_frms_in_gop) |
150 | 88 | { |
151 | 88 | WORD32 i4_rem_frames_in_period = |
152 | 88 | ps_rbip->i4_num_intra_frm_interval |
153 | 88 | * (i4_new_tot_frms_in_gop |
154 | 88 | - ps_rbip->i4_tot_frms_in_gop); |
155 | | |
156 | 88 | number_t vq_rem_frms_in_period, s_bits_per_frm, vq_delta_bits_in_period; |
157 | | |
158 | 88 | SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frames_in_period, 0); |
159 | 88 | SET_VAR_Q(s_bits_per_frm, ps_rbip->i4_bits_per_frm, 0); |
160 | | |
161 | | /* delta_bits_in_period = bits_per_frm * rem_frms_in_period */ |
162 | 88 | mult32_var_q(s_bits_per_frm, vq_rem_frms_in_period, |
163 | 88 | &vq_delta_bits_in_period); |
164 | | |
165 | | /* rem_bits_in_period += delta_bits_in_period */ |
166 | 88 | add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, |
167 | 88 | &ps_rbip->vq_rem_bits_in_period); |
168 | 88 | } |
169 | | /* Updated the new values */ |
170 | 123k | ps_rbip->i4_tot_frms_in_gop = i4_new_tot_frms_in_gop; |
171 | 123k | } |
172 | | |
173 | | static void irc_ba_update_rbip(rem_bit_in_prd_t *ps_rbip, |
174 | | pic_handling_handle ps_pic_handling, |
175 | | number_t vq_num_bits) |
176 | 123k | { |
177 | 123k | check_update_rbip(ps_rbip, ps_pic_handling); |
178 | | |
179 | | /* rem_bits_in_period += num_of_bits */ |
180 | 123k | add32_var_q(vq_num_bits, ps_rbip->vq_rem_bits_in_period, |
181 | 123k | &ps_rbip->vq_rem_bits_in_period); |
182 | 123k | } |
183 | | |
184 | | static void irc_ba_change_rbip(rem_bit_in_prd_t *ps_rbip, |
185 | | pic_handling_handle ps_pic_handling, |
186 | | WORD32 i4_new_bits_per_frm, |
187 | | WORD32 i4_new_num_intra_frm_interval) |
188 | 135k | { |
189 | 135k | WORD32 ai4_rem_frms_in_period[MAX_PIC_TYPE], i4_rem_frms_in_gop, i; |
190 | 135k | irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); |
191 | | |
192 | 135k | i4_rem_frms_in_gop = 0; |
193 | 542k | for(i = 0; i < MAX_PIC_TYPE; i++) |
194 | 406k | i4_rem_frms_in_gop += ai4_rem_frms_in_period[i]; |
195 | | |
196 | 135k | if(i4_new_bits_per_frm != ps_rbip->i4_bits_per_frm) |
197 | 19.9k | { |
198 | 19.9k | WORD32 i4_rem_frms_in_period = (ps_rbip->i4_num_intra_frm_interval - 1) |
199 | 19.9k | * ps_rbip->i4_tot_frms_in_gop + i4_rem_frms_in_gop; |
200 | | |
201 | 19.9k | number_t vq_rem_frms_in_period, vq_delta_bits_per_frm, |
202 | 19.9k | vq_delta_bits_in_period; |
203 | | |
204 | | /* delta_bits_per_frm = new_bits_per_frm - old_bits_per_frm */ |
205 | 19.9k | SET_VAR_Q(vq_delta_bits_per_frm, |
206 | 19.9k | (i4_new_bits_per_frm - ps_rbip->i4_bits_per_frm), 0); |
207 | | |
208 | 19.9k | SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frms_in_period, 0); |
209 | | |
210 | | /* delta_bits_in_period = delta_bits_per_frm * rem_frms_in_period */ |
211 | 19.9k | mult32_var_q(vq_delta_bits_per_frm, vq_rem_frms_in_period, |
212 | 19.9k | &vq_delta_bits_in_period); |
213 | | |
214 | | /* ps_rbip->rem_bits_in_period += delta_bits_in_period */ |
215 | 19.9k | add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, |
216 | 19.9k | &ps_rbip->vq_rem_bits_in_period); |
217 | 19.9k | } |
218 | | |
219 | 135k | if(i4_new_num_intra_frm_interval != ps_rbip->i4_num_intra_frm_interval) |
220 | 1.69k | { |
221 | 1.69k | WORD32 i4_rem_frms_in_period = ps_rbip->i4_tot_frms_in_gop |
222 | 1.69k | * (i4_new_num_intra_frm_interval |
223 | 1.69k | - ps_rbip->i4_num_intra_frm_interval); |
224 | | |
225 | 1.69k | number_t vq_rem_frms_in_period, vq_new_bits_per_frm, |
226 | 1.69k | vq_delta_bits_in_period; |
227 | | |
228 | | /* new_bits_per_frm = new_new_bits_per_frm - old_new_bits_per_frm */ |
229 | 1.69k | SET_VAR_Q(vq_new_bits_per_frm, i4_new_bits_per_frm, 0); |
230 | | |
231 | 1.69k | SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frms_in_period, 0); |
232 | | |
233 | | /* delta_bits_in_period = new_bits_per_frm * rem_frms_in_period */ |
234 | 1.69k | mult32_var_q(vq_new_bits_per_frm, vq_rem_frms_in_period, |
235 | 1.69k | &vq_delta_bits_in_period); |
236 | | |
237 | | /* ps_rbip->rem_bits_in_period += delta_bits_in_period */ |
238 | 1.69k | add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, |
239 | 1.69k | &ps_rbip->vq_rem_bits_in_period); |
240 | 1.69k | } |
241 | | /* Update the new value */ |
242 | 135k | ps_rbip->i4_num_intra_frm_interval = i4_new_num_intra_frm_interval; |
243 | 135k | ps_rbip->i4_bits_per_frm = i4_new_bits_per_frm; |
244 | 135k | } |
245 | | |
246 | | WORD32 irc_ba_num_fill_use_free_memtab(bit_allocation_t **pps_bit_allocation, |
247 | | itt_memtab_t *ps_memtab, |
248 | | ITT_FUNC_TYPE_E e_func_type) |
249 | 121k | { |
250 | 121k | WORD32 i4_mem_tab_idx = 0; |
251 | 121k | bit_allocation_t s_bit_allocation_temp; |
252 | | |
253 | | /* |
254 | | * Hack for all alloc, during which we don't have any state memory. |
255 | | * Dereferencing can cause issues |
256 | | */ |
257 | 121k | if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB) |
258 | 101k | (*pps_bit_allocation) = &s_bit_allocation_temp; |
259 | | |
260 | | /*for src rate control state structure*/ |
261 | 121k | if(e_func_type != GET_NUM_MEMTAB) |
262 | 60.6k | { |
263 | 60.6k | fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(bit_allocation_t), |
264 | 60.6k | ALIGN_128_BYTE, PERSISTENT, DDR); |
265 | 60.6k | use_or_fill_base(&ps_memtab[0], (void**)pps_bit_allocation, |
266 | 60.6k | e_func_type); |
267 | 60.6k | } |
268 | 121k | i4_mem_tab_idx++; |
269 | | |
270 | 121k | i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab( |
271 | 121k | &pps_bit_allocation[0]->ps_error_bits, |
272 | 121k | &ps_memtab[i4_mem_tab_idx], e_func_type); |
273 | | |
274 | 121k | return (i4_mem_tab_idx); |
275 | 121k | } |
276 | | |
277 | | /******************************************************************************* |
278 | | Function Name : irc_ba_init_bit_allocation |
279 | | Description : Initialize the bit_allocation structure. |
280 | | ******************************************************************************/ |
281 | | void irc_ba_init_bit_allocation(bit_allocation_t *ps_bit_allocation, |
282 | | pic_handling_handle ps_pic_handling, |
283 | | WORD32 i4_num_intra_frm_interval, |
284 | | WORD32 i4_bit_rate, |
285 | | WORD32 i4_frm_rate, |
286 | | WORD32 *i4_peak_bit_rate, |
287 | | WORD32 i4_min_bitrate) |
288 | 40.4k | { |
289 | 40.4k | WORD32 i; |
290 | 40.4k | WORD32 i4_bits_per_frm, i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES]; |
291 | | |
292 | | /* Calculate the bits per frame */ |
293 | 40.4k | X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frm_rate, i4_bits_per_frm); |
294 | 121k | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
295 | 80.8k | { |
296 | 80.8k | X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frm_rate, |
297 | 80.8k | i4_max_bits_per_frm[i]); |
298 | 80.8k | } |
299 | | /* Initialize the bits_per_frame */ |
300 | 40.4k | ps_bit_allocation->i4_bits_per_frm = i4_bits_per_frm; |
301 | 121k | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
302 | 80.8k | { |
303 | 80.8k | ps_bit_allocation->i4_max_bits_per_frm[i] = i4_max_bits_per_frm[i]; |
304 | 80.8k | } |
305 | 40.4k | X_PROD_Y_DIV_Z(i4_min_bitrate, 1000, i4_frm_rate, |
306 | 40.4k | ps_bit_allocation->i4_min_bits_per_frm); |
307 | | |
308 | | /* |
309 | | * Initialize the rem_bits in period |
310 | | * The first gop in case of an OPEN GOP may have fewer B_PICs, |
311 | | * That condition is not taken care of |
312 | | */ |
313 | 40.4k | init_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_bits_per_frm, |
314 | 40.4k | i4_num_intra_frm_interval); |
315 | | |
316 | | /* Initialize the num_gops_in_period */ |
317 | 40.4k | ps_bit_allocation->i4_num_gops_in_period = i4_num_intra_frm_interval; |
318 | 40.4k | ps_bit_allocation->i4_actual_num_gops_in_period = i4_num_intra_frm_interval; |
319 | | |
320 | | /* Relative complexity between I and P frames */ |
321 | 40.4k | ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q); |
322 | 40.4k | ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO; |
323 | 40.4k | ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q; |
324 | | |
325 | | /* Initialize the saved bits to 0*/ |
326 | 40.4k | SET_VAR_Q(ps_bit_allocation->vq_saved_bits, 0, 0); |
327 | | |
328 | | /* Update the error bits module with average bits */ |
329 | 40.4k | irc_init_error_bits(ps_bit_allocation->ps_error_bits, i4_frm_rate, |
330 | 40.4k | i4_bit_rate); |
331 | | /* Store the input for implementing change in values */ |
332 | 40.4k | ps_bit_allocation->i4_frame_rate = i4_frm_rate; |
333 | 40.4k | ps_bit_allocation->i4_bit_rate = i4_bit_rate; |
334 | | |
335 | 40.4k | memset(ps_bit_allocation->i4_prev_frm_header_bits, 0, sizeof(ps_bit_allocation->i4_prev_frm_header_bits)); |
336 | 121k | for(i=0;i<MAX_NUM_DRAIN_RATES;i++) |
337 | 80.8k | ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i]; |
338 | 40.4k | } |
339 | | |
340 | | /******************************************************************************* |
341 | | Function Name : get_cur_frm_est_bits |
342 | | Description : Based on remaining bits in period and rd_model |
343 | | the number of bits required for the current frame is estimated. |
344 | | ******************************************************************************/ |
345 | | WORD32 irc_ba_get_cur_frm_est_texture_bits(bit_allocation_t *ps_bit_allocation, |
346 | | rc_rd_model_handle *pps_rd_model, |
347 | | est_sad_handle ps_est_sad, |
348 | | pic_handling_handle ps_pic_handling, |
349 | | picture_type_e e_pic_type) |
350 | 41.0k | { |
351 | 41.0k | WORD32 i, j; |
352 | 41.0k | WORD32 i4_est_texture_bits_for_frm; |
353 | 41.0k | number_t vq_rem_texture_bits; |
354 | 41.0k | number_t vq_complexity_estimate[MAX_PIC_TYPE]; |
355 | 41.0k | WORD32 i4_rem_frms_in_period[MAX_PIC_TYPE], i4_frms_in_period[MAX_PIC_TYPE]; |
356 | 41.0k | number_t vq_max_consumable_bits; |
357 | 41.0k | number_t vq_rem_frms_in_period[MAX_PIC_TYPE], vq_est_texture_bits_for_frm; |
358 | 41.0k | number_t vq_prev_hdr_bits[MAX_PIC_TYPE]; |
359 | 41.0k | number_t vq_num_bits; |
360 | 41.0k | WORD32 complexity_est = 0; |
361 | | |
362 | | /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */ |
363 | 41.0k | irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); |
364 | 41.0k | irc_pic_type_get_frms_in_gop(ps_pic_handling, i4_frms_in_period); |
365 | | |
366 | | /* Depending on the number of gops in a period, find the num_frms_in_prd */ |
367 | 164k | for(j = 0; j < MAX_PIC_TYPE; j++) |
368 | 123k | { |
369 | 123k | i4_rem_frms_in_period[j] += (i4_frms_in_period[j] |
370 | 123k | * (ps_bit_allocation->i4_num_gops_in_period - 1)); |
371 | 123k | i4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period; |
372 | 123k | } |
373 | | |
374 | | /* Remove the header bits from the remaining bits to find how many bits you |
375 | | can transfer.*/ |
376 | 41.0k | SET_VAR_Q(vq_num_bits, 0, 0); |
377 | 41.0k | irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, vq_num_bits); |
378 | 164k | for(i = 0; i < MAX_PIC_TYPE; i++) |
379 | 123k | { |
380 | 123k | SET_VAR_Q(vq_rem_frms_in_period[i], i4_rem_frms_in_period[i], 0); |
381 | 123k | SET_VAR_Q(vq_prev_hdr_bits[i], |
382 | 123k | ps_bit_allocation->i4_prev_frm_header_bits[i], 0); |
383 | 123k | } |
384 | 41.0k | { |
385 | | /* |
386 | | *rem_texture_bits = rem_bits_in_period - |
387 | | *(rem_frms_in_period[I_PIC] * prev_frm_header_bits[I_PIC]) - |
388 | | *(rem_frms_in_period[P_PIC] * prev_frm_header_bits[P_PIC]) - |
389 | | *(rem_frms_in_period[B_PIC] * prev_frm_header_bits[B_PIC]); |
390 | | */ |
391 | 41.0k | number_t vq_rem_hdr_bits; |
392 | 41.0k | vq_rem_texture_bits = ps_bit_allocation->s_rbip.vq_rem_bits_in_period; |
393 | | |
394 | 41.0k | mult32_var_q(vq_prev_hdr_bits[I_PIC], vq_rem_frms_in_period[I_PIC], |
395 | 41.0k | &vq_rem_hdr_bits); |
396 | 41.0k | sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); |
397 | | |
398 | 41.0k | mult32_var_q(vq_prev_hdr_bits[P_PIC], vq_rem_frms_in_period[P_PIC], |
399 | 41.0k | &vq_rem_hdr_bits); |
400 | 41.0k | sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); |
401 | | |
402 | 41.0k | mult32_var_q(vq_prev_hdr_bits[B_PIC], vq_rem_frms_in_period[B_PIC], |
403 | 41.0k | &vq_rem_hdr_bits); |
404 | 41.0k | sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); |
405 | 41.0k | } |
406 | 41.0k | { |
407 | | /* max_consumable_bits = |
408 | | *(frms_in_period[I_PIC] * max_bits_per_frm[0] ) + |
409 | | *(frms_in_period[P_PIC] + frms_in_period[B_PIC] ) * max_bits_per_frm[1]; |
410 | | */ |
411 | 41.0k | number_t vq_max_bits, vq_max_bits_per_frm[2]; |
412 | | |
413 | 41.0k | SET_VAR_Q(vq_max_bits_per_frm[0], |
414 | 41.0k | ps_bit_allocation->i4_max_bits_per_frm[0], 0); |
415 | 41.0k | SET_VAR_Q(vq_max_bits_per_frm[1], |
416 | 41.0k | ps_bit_allocation->i4_max_bits_per_frm[1], 0); |
417 | | |
418 | 41.0k | mult32_var_q(vq_rem_frms_in_period[I_PIC], vq_max_bits_per_frm[0], |
419 | 41.0k | &vq_max_bits); |
420 | 41.0k | vq_max_consumable_bits = vq_max_bits; |
421 | | |
422 | 41.0k | mult32_var_q(vq_rem_frms_in_period[P_PIC], vq_max_bits_per_frm[1], |
423 | 41.0k | &vq_max_bits); |
424 | 41.0k | add32_var_q(vq_max_bits, vq_max_consumable_bits, |
425 | 41.0k | &vq_max_consumable_bits); |
426 | | |
427 | 41.0k | mult32_var_q(vq_rem_frms_in_period[B_PIC], vq_max_bits_per_frm[1], |
428 | 41.0k | &vq_max_bits); |
429 | 41.0k | add32_var_q(vq_max_bits, vq_max_consumable_bits, |
430 | 41.0k | &vq_max_consumable_bits); |
431 | 41.0k | } |
432 | | |
433 | | /* rem_texture_bits = MIN(rem_texture_bits, max_consumable_bits) */ |
434 | 41.0k | MIN_VARQ(vq_max_consumable_bits, vq_rem_texture_bits, vq_rem_texture_bits); |
435 | | |
436 | | /* The bits are then allocated based on the relative complexity of the |
437 | | current frame with respect to that of the rest of the frames in period */ |
438 | 164k | for(i = 0; i < MAX_PIC_TYPE; i++) |
439 | 123k | { |
440 | 123k | number_t vq_lin_mod_coeff, vq_est_sad, vq_K; |
441 | | |
442 | | /* Getting the linear model coefficient */ |
443 | 123k | vq_lin_mod_coeff = irc_get_linear_coefficient(pps_rd_model[i]); |
444 | | |
445 | | /* Getting the estimated SAD */ |
446 | 123k | SET_VAR_Q(vq_est_sad, irc_get_est_sad(ps_est_sad,i), 0); |
447 | | |
448 | | /* Making K factor a var Q format */ |
449 | 123k | SET_VAR_Q(vq_K, ps_bit_allocation->i2_K[i], K_Q); |
450 | | |
451 | | /* Complexity_estimate = [ (lin_mod_coeff * estimated_sad) / K factor ] */ |
452 | 123k | mult32_var_q(vq_lin_mod_coeff, vq_est_sad, &vq_lin_mod_coeff); |
453 | 123k | div32_var_q(vq_lin_mod_coeff, vq_K, &vq_complexity_estimate[i]); |
454 | 123k | } |
455 | | |
456 | | /* |
457 | | * For simple cases, one of the complexities go to zero and in those cases |
458 | | * distribute the bits evenly among frames based on I_TO_P_RATIO |
459 | | */ |
460 | | |
461 | | /* Also check the B-pictures complexity only in case they are present*/ |
462 | 41.0k | if(i4_frms_in_period[B_PIC] == 0) |
463 | 29.9k | { |
464 | 29.9k | complexity_est = (vq_complexity_estimate[I_PIC] |
465 | 23.4k | && vq_complexity_estimate[P_PIC]); |
466 | 29.9k | } |
467 | 11.0k | else |
468 | 11.0k | { |
469 | 11.0k | complexity_est = (vq_complexity_estimate[I_PIC] |
470 | 10.9k | && vq_complexity_estimate[P_PIC] |
471 | 10.9k | && vq_complexity_estimate[B_PIC]); |
472 | 11.0k | } |
473 | | |
474 | 41.0k | if(complexity_est) |
475 | 23.6k | { |
476 | | /* |
477 | | * Estimated texture bits = |
478 | | * (remaining bits) * (cur frm complexity) |
479 | | * --------------------------------------- |
480 | | * (num_i_frm*i_frm_complexity) + (num_p_frm*pfrm_complexity) |
481 | | * + (b_frm * b_frm_cm) |
482 | | */ |
483 | 23.6k | mult32_var_q(vq_rem_texture_bits, vq_complexity_estimate[e_pic_type], |
484 | 23.6k | &vq_rem_texture_bits); |
485 | | |
486 | 94.6k | for(i = 0; i < MAX_PIC_TYPE; i++) |
487 | 70.9k | { |
488 | 70.9k | mult32_var_q(vq_rem_frms_in_period[i], vq_complexity_estimate[i], |
489 | 70.9k | &vq_rem_frms_in_period[i]); |
490 | 70.9k | } |
491 | | |
492 | 23.6k | add32_var_q(vq_rem_frms_in_period[I_PIC], vq_rem_frms_in_period[P_PIC], |
493 | 23.6k | &vq_rem_frms_in_period[I_PIC]); |
494 | | |
495 | 23.6k | add32_var_q(vq_rem_frms_in_period[I_PIC], vq_rem_frms_in_period[B_PIC], |
496 | 23.6k | &vq_rem_frms_in_period[I_PIC]); |
497 | | |
498 | 23.6k | div32_var_q(vq_rem_texture_bits, vq_rem_frms_in_period[I_PIC], |
499 | 23.6k | &vq_est_texture_bits_for_frm); |
500 | | |
501 | 23.6k | number_t_to_word32(vq_est_texture_bits_for_frm, |
502 | 23.6k | &i4_est_texture_bits_for_frm); |
503 | 23.6k | } |
504 | 17.4k | else |
505 | 17.4k | { |
506 | 17.4k | number_t vq_i_to_p_bit_ratio, vq_rem_frms; |
507 | | |
508 | 17.4k | SET_VAR_Q(vq_i_to_p_bit_ratio, I_TO_P_BIT_RATIO, 0); |
509 | | |
510 | | /* rem_frms = ((I_TO_P_BIT_RATIO * rem_frms_in_period[I_PIC]) + |
511 | | * rem_frms_in_period[P_PIC] + rem_frms_in_period[B_PIC]); |
512 | | */ |
513 | 17.4k | mult32_var_q(vq_rem_frms_in_period[I_PIC], vq_i_to_p_bit_ratio, |
514 | 17.4k | &vq_rem_frms); |
515 | 17.4k | add32_var_q(vq_rem_frms_in_period[P_PIC], vq_rem_frms, &vq_rem_frms); |
516 | 17.4k | add32_var_q(vq_rem_frms_in_period[B_PIC], vq_rem_frms, &vq_rem_frms); |
517 | | |
518 | | /* est_texture_bits_for_frm = rem_texture_bits / rem_frms */ |
519 | 17.4k | div32_var_q(vq_rem_texture_bits, vq_rem_frms, |
520 | 17.4k | &vq_est_texture_bits_for_frm); |
521 | 17.4k | number_t_to_word32(vq_est_texture_bits_for_frm, |
522 | 17.4k | &i4_est_texture_bits_for_frm); |
523 | | |
524 | 17.4k | i4_est_texture_bits_for_frm = |
525 | 17.4k | (I_PIC == e_pic_type) ? |
526 | 10.9k | (i4_est_texture_bits_for_frm |
527 | 10.9k | * I_TO_P_BIT_RATIO) : |
528 | 17.4k | i4_est_texture_bits_for_frm; |
529 | 17.4k | } |
530 | | |
531 | | /* |
532 | | * If the remaining bits in the period becomes negative then the estimated |
533 | | * texture bits would also become negative. This would send a feedback to |
534 | | * the model which may go for a toss. Thus sending the minimum possible |
535 | | * value = 0 |
536 | | */ |
537 | 41.0k | if(i4_est_texture_bits_for_frm < 0) |
538 | 8.51k | { |
539 | 8.51k | i4_est_texture_bits_for_frm = 0; |
540 | 8.51k | } |
541 | | |
542 | 41.0k | return (i4_est_texture_bits_for_frm); |
543 | 41.0k | } |
544 | | |
545 | | /****************************************************************************** |
546 | | Function Name : irc_ba_get_cur_frm_est_header_bits |
547 | | Description : Based on remaining bits in period and rd_model |
548 | | the number of bits required for the current frame is estimated. |
549 | | ******************************************************************************/ |
550 | | WORD32 irc_ba_get_cur_frm_est_header_bits(bit_allocation_t *ps_bit_allocation, |
551 | | picture_type_e e_pic_type) |
552 | 41.0k | { |
553 | 41.0k | return (ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type]); |
554 | 41.0k | } |
555 | | |
556 | | WORD32 irc_ba_get_rem_bits_in_period(bit_allocation_t *ps_bit_allocation, |
557 | | pic_handling_handle ps_pic_handling) |
558 | 0 | { |
559 | 0 | WORD32 i4_rem_bits_in_gop = 0; |
560 | |
|
561 | 0 | number_t vq_num_bits; |
562 | 0 | SET_VAR_Q(vq_num_bits, 0, 0); |
563 | |
|
564 | 0 | irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, vq_num_bits); |
565 | 0 | number_t_to_word32(ps_bit_allocation->s_rbip.vq_rem_bits_in_period, |
566 | 0 | &i4_rem_bits_in_gop); |
567 | 0 | return (i4_rem_bits_in_gop); |
568 | 0 | } |
569 | | |
570 | | /******************************************************************************* |
571 | | Function Name : irc_ba_update_cur_frm_consumed_bits |
572 | | Description : Based on remaining bits in period and rd_model |
573 | | the number of bits required for the current frame is estimated. |
574 | | ******************************************************************************/ |
575 | | void irc_ba_update_cur_frm_consumed_bits(bit_allocation_t *ps_bit_allocation, |
576 | | pic_handling_handle ps_pic_handling, |
577 | | WORD32 i4_total_frame_bits, |
578 | | WORD32 i4_model_updation_hdr_bits, |
579 | | picture_type_e e_pic_type, |
580 | | UWORD8 u1_is_scd, |
581 | | WORD32 i4_last_frm_in_gop) |
582 | 70.0k | { |
583 | 70.0k | WORD32 i4_error_bits = irc_get_error_bits(ps_bit_allocation->ps_error_bits); |
584 | | |
585 | | /* Update the remaining bits in period */ |
586 | 70.0k | number_t vq_num_bits; |
587 | 70.0k | SET_VAR_Q(vq_num_bits, (-i4_total_frame_bits + i4_error_bits), 0); |
588 | | |
589 | 70.0k | irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, vq_num_bits); |
590 | | |
591 | | /* |
592 | | * Update the header bits so that it can be used as an estimate to the next |
593 | | * frame |
594 | | */ |
595 | 70.0k | if(u1_is_scd) |
596 | 2.78k | { |
597 | | /* |
598 | | * In case of SCD, even though the frame type is P, it is equivalent to |
599 | | * a I frame and so the corresponding header bits is updated |
600 | | */ |
601 | 2.78k | ps_bit_allocation->i4_prev_frm_header_bits[I_PIC] = |
602 | 2.78k | i4_model_updation_hdr_bits; |
603 | | |
604 | 2.78k | #define MAX_NUM_GOPS_IN_PERIOD (3) |
605 | 2.78k | if(ps_bit_allocation->i4_num_gops_in_period < MAX_NUM_GOPS_IN_PERIOD) |
606 | 1.00k | { |
607 | | /* |
608 | | * Whenever there is a scene change increase the number of gops by |
609 | | * 2 so that the number of bits allocated is not very constrained |
610 | | */ |
611 | 1.00k | ps_bit_allocation->i4_num_gops_in_period += 2; |
612 | | /* Add the extra bits in GOP to remaining bits in period */ |
613 | 1.00k | irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, |
614 | 1.00k | ps_bit_allocation->i4_bits_per_frm, |
615 | 1.00k | ps_bit_allocation->i4_num_gops_in_period); |
616 | 1.00k | } |
617 | 2.78k | } |
618 | 67.2k | else |
619 | 67.2k | { |
620 | 67.2k | ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] = |
621 | 67.2k | i4_model_updation_hdr_bits; |
622 | 67.2k | } |
623 | | |
624 | 70.0k | if(i4_last_frm_in_gop) |
625 | 11.6k | { |
626 | 11.6k | WORD32 i4_tot_frms_in_gop = get_number_of_frms_in_a_gop( |
627 | 11.6k | ps_pic_handling); |
628 | 11.6k | number_t vq_total_frms_in_gop, vq_bits_per_frm, vq_num_bits_in_gop; |
629 | | |
630 | 11.6k | SET_VAR_Q(vq_total_frms_in_gop, i4_tot_frms_in_gop, 0); |
631 | 11.6k | SET_VAR_Q(vq_bits_per_frm, ps_bit_allocation->i4_bits_per_frm, 0); |
632 | | |
633 | 11.6k | mult32_var_q(vq_bits_per_frm, vq_total_frms_in_gop, |
634 | 11.6k | &vq_num_bits_in_gop); |
635 | | /* |
636 | | * If the number of gops in period has been increased due to scene |
637 | | * change, slowly bring in down across the gops |
638 | | */ |
639 | 11.6k | if(ps_bit_allocation->i4_num_gops_in_period |
640 | 11.6k | > ps_bit_allocation->i4_actual_num_gops_in_period) |
641 | 692 | { |
642 | 692 | ps_bit_allocation->i4_num_gops_in_period--; |
643 | 692 | irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, |
644 | 692 | ps_bit_allocation->i4_bits_per_frm, |
645 | 692 | ps_bit_allocation->i4_num_gops_in_period); |
646 | 692 | } |
647 | | /* |
648 | | * If rem_bits_in_period < 0 decrease the number of bits allocated for |
649 | | * the next period else increase it |
650 | | */ |
651 | 11.6k | irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, |
652 | 11.6k | vq_num_bits_in_gop); |
653 | 11.6k | } |
654 | | /* Update the lower modules */ |
655 | 70.0k | irc_update_error_bits(ps_bit_allocation->ps_error_bits); |
656 | 70.0k | } |
657 | | |
658 | | void irc_ba_change_remaining_bits_in_period(bit_allocation_t *ps_bit_allocation, |
659 | | pic_handling_handle ps_pic_handling, |
660 | | WORD32 i4_bit_rate, |
661 | | WORD32 i4_frame_rate, |
662 | | WORD32 *i4_peak_bit_rate) |
663 | 133k | { |
664 | 133k | WORD32 i4_new_avg_bits_per_frm; |
665 | 133k | WORD32 i4_new_peak_bits_per_frm[MAX_NUM_DRAIN_RATES]; |
666 | 133k | WORD32 i4_rem_frms_in_period[MAX_PIC_TYPE]; |
667 | 133k | int i; |
668 | | |
669 | | /* Calculate the new per frame bits */ |
670 | 133k | X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frame_rate, i4_new_avg_bits_per_frm); |
671 | 401k | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
672 | 267k | { |
673 | 267k | X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frame_rate, |
674 | 267k | i4_new_peak_bits_per_frm[i]); |
675 | 267k | } |
676 | | |
677 | 401k | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
678 | 267k | { |
679 | 267k | ps_bit_allocation->i4_max_bits_per_frm[i] = i4_new_peak_bits_per_frm[i]; |
680 | 267k | } |
681 | | |
682 | | /* |
683 | | * Get the rem_frms_in_prd & the frms_in_prd from the pic_type state |
684 | | * struct |
685 | | */ |
686 | 133k | irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); |
687 | | |
688 | | /* |
689 | | * If the difference > 0(/ <0), the remaining bits in period needs to be |
690 | | * increased(/decreased) based on the remaining number of frames |
691 | | */ |
692 | 133k | irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, |
693 | 133k | i4_new_avg_bits_per_frm, |
694 | 133k | ps_bit_allocation->i4_num_gops_in_period); |
695 | | |
696 | | /* Update the new average bits per frame */ |
697 | 133k | ps_bit_allocation->i4_bits_per_frm = i4_new_avg_bits_per_frm; |
698 | | /* change the lower modules state */ |
699 | 133k | irc_change_bitrate_in_error_bits(ps_bit_allocation->ps_error_bits, |
700 | 133k | i4_bit_rate); |
701 | 133k | irc_change_frm_rate_in_error_bits(ps_bit_allocation->ps_error_bits, |
702 | 133k | i4_frame_rate); |
703 | | |
704 | | /* Store the modified frame_rate */ |
705 | 133k | ps_bit_allocation->i4_frame_rate = i4_frame_rate; |
706 | 133k | ps_bit_allocation->i4_bit_rate = i4_bit_rate; |
707 | 401k | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
708 | 267k | ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i]; |
709 | 133k | } |
710 | | |
711 | | void irc_ba_change_ba_peak_bit_rate(bit_allocation_t *ps_bit_allocation, |
712 | | WORD32 *ai4_peak_bit_rate) |
713 | 0 | { |
714 | 0 | WORD32 i; |
715 | | |
716 | | /* Calculate the bits per frame */ |
717 | 0 | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
718 | 0 | { |
719 | 0 | X_PROD_Y_DIV_Z(ai4_peak_bit_rate[i], 1000, |
720 | 0 | ps_bit_allocation->i4_frame_rate, |
721 | 0 | ps_bit_allocation->i4_max_bits_per_frm[i]); |
722 | 0 | ps_bit_allocation->ai4_peak_bit_rate[i] = ai4_peak_bit_rate[i]; |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | | /****************************************************************************** |
727 | | * @brief Modifies the remaining bit in period for the gop which has fif. |
728 | | * since fif would cause a new gop to be created, we need to add the number |
729 | | * of encoded frames in the fif GOP worth of bits to remaining bits in |
730 | | * period |
731 | | ******************************************************************************/ |
732 | | void irc_ba_change_rem_bits_in_prd_at_force_I_frame(bit_allocation_t *ps_bit_allocation, |
733 | | pic_handling_handle ps_pic_handling) |
734 | 820 | { |
735 | 820 | WORD32 i4_frms_in_period; |
736 | 820 | number_t vq_frms_in_period, vq_bits_per_frm, vq_num_bits_in_period; |
737 | | |
738 | 820 | i4_frms_in_period = irc_pic_type_get_frms_in_gop_force_I_frm( |
739 | 820 | ps_pic_handling); |
740 | 820 | SET_VAR_Q(vq_frms_in_period, i4_frms_in_period, 0); |
741 | 820 | SET_VAR_Q(vq_bits_per_frm, ps_bit_allocation->i4_bits_per_frm, 0); |
742 | | |
743 | 820 | mult32_var_q(vq_bits_per_frm, vq_frms_in_period, &vq_num_bits_in_period); |
744 | | |
745 | 820 | irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, vq_num_bits_in_period); |
746 | 820 | } |
747 | | |
748 | | void irc_ba_check_and_update_bit_allocation(bit_allocation_t *ps_bit_allocation, |
749 | | pic_handling_handle ps_pic_handling, |
750 | | WORD32 i4_cur_buf_size, |
751 | | WORD32 i4_max_buf_size, |
752 | | WORD32 i4_max_bits_inflow_per_frm, |
753 | | WORD32 i4_tot_frame_bits) |
754 | 5.92k | { |
755 | | |
756 | 5.92k | number_t vq_max_drain_bits, vq_extra_bits, vq_less_bits, |
757 | 5.92k | vq_allocated_saved_bits, vq_min_bits_for_period; |
758 | 5.92k | WORD32 i4_num_frms_in_period = get_number_of_frms_in_a_gop(ps_pic_handling); |
759 | 5.92k | number_t vq_rem_bits_in_period, vq_num_frms_in_period, vq_zero; |
760 | 5.92k | WORD32 b_rem_bits_gt_max_drain, b_rem_bits_lt_min_bits, |
761 | 5.92k | b_saved_bits_gt_zero; |
762 | 5.92k | rem_bit_in_prd_t *ps_rbip = &ps_bit_allocation->s_rbip; |
763 | | |
764 | 5.92k | UNUSED(i4_cur_buf_size); |
765 | 5.92k | UNUSED(i4_max_buf_size); |
766 | 5.92k | UNUSED(i4_tot_frame_bits); |
767 | | |
768 | | /* |
769 | | * If the remaining bits is greater than what can be drained in that period |
770 | | * Clip the remaining bits in period to the maximum it can drain in that |
771 | | * period with the error of current buffer size.Accumulate the saved bits |
772 | | * if any. else if the remaining bits is lesser than the minimum bit rate |
773 | | * promised in that period Add the excess bits to remaining bits in period |
774 | | * and reduce it from the saved bits Else Provide the extra bits from the |
775 | | * "saved bits pool". |
776 | | */ |
777 | | /* |
778 | | * max_drain_bits = num_gops_in_period * num_frms_in_period * |
779 | | * * max_bits_inflow_per_frm |
780 | | */ |
781 | 5.92k | SET_VAR_Q(vq_num_frms_in_period, |
782 | 5.92k | (ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period), |
783 | 5.92k | 0); |
784 | 5.92k | SET_VAR_Q(vq_max_drain_bits, i4_max_bits_inflow_per_frm, 0); |
785 | 5.92k | SET_VAR_Q(vq_zero, 0, 0); |
786 | 5.92k | mult32_var_q(vq_max_drain_bits, vq_num_frms_in_period, &vq_max_drain_bits); |
787 | | |
788 | | /* |
789 | | * min_bits_for_period = num_gops_in_period * num_frms_in_period * |
790 | | * min_bits_per_frm |
791 | | */ |
792 | 5.92k | SET_VAR_Q(vq_min_bits_for_period, ps_bit_allocation->i4_min_bits_per_frm, |
793 | 5.92k | 0); |
794 | 5.92k | mult32_var_q(vq_min_bits_for_period, vq_num_frms_in_period, |
795 | 5.92k | &vq_min_bits_for_period); |
796 | | |
797 | 5.92k | vq_rem_bits_in_period = ps_rbip->vq_rem_bits_in_period; |
798 | | |
799 | | /* Evaluate rem_bits_in_period > max_drain_bits */ |
800 | 5.92k | VQ_A_GT_VQ_B(ps_rbip->vq_rem_bits_in_period, vq_max_drain_bits, |
801 | 5.92k | b_rem_bits_gt_max_drain); |
802 | | |
803 | | /* Evaluate rem_bits_in_period < min_bits_for_period */ |
804 | 5.92k | VQ_A_LT_VQ_B(ps_rbip->vq_rem_bits_in_period, vq_min_bits_for_period, |
805 | 5.92k | b_rem_bits_lt_min_bits); |
806 | | |
807 | | /* Evaluate saved_bits > 0 */ |
808 | 5.92k | VQ_A_LT_VQ_B(ps_bit_allocation->vq_saved_bits, vq_zero, |
809 | 5.92k | b_saved_bits_gt_zero); |
810 | | |
811 | | /* (i4_rem_bits_in_period > i4_max_drain_bits) */ |
812 | 5.92k | if(b_rem_bits_gt_max_drain) |
813 | 587 | { |
814 | | /* extra_bits = rem_bits_in_period - max_drain_bits */ |
815 | 587 | sub32_var_q(ps_rbip->vq_rem_bits_in_period, vq_max_drain_bits, |
816 | 587 | &vq_extra_bits); |
817 | | |
818 | | /* saved_bits += extra_bits */ |
819 | 587 | add32_var_q(ps_bit_allocation->vq_saved_bits, vq_extra_bits, |
820 | 587 | &ps_bit_allocation->vq_saved_bits); |
821 | | |
822 | | /* rem_bits_in_period = vq_max_drain_bits */ |
823 | 587 | ps_rbip->vq_rem_bits_in_period = vq_max_drain_bits; |
824 | 587 | } |
825 | 5.33k | else if(b_rem_bits_lt_min_bits) |
826 | 1.83k | { |
827 | | /* extra_bits(-ve) = rem_bits_in_period - i4_min_bits_for_period */ |
828 | 1.83k | sub32_var_q(ps_rbip->vq_rem_bits_in_period, vq_min_bits_for_period, |
829 | 1.83k | &vq_extra_bits); |
830 | | |
831 | | /* saved_bits += extra_bits(-ve) */ |
832 | 1.83k | add32_var_q(ps_bit_allocation->vq_saved_bits, vq_extra_bits, |
833 | 1.83k | &ps_bit_allocation->vq_saved_bits); |
834 | | |
835 | | /* rem_bits_in_period = min_bits_for_period */ |
836 | 1.83k | ps_rbip->vq_rem_bits_in_period = vq_min_bits_for_period; |
837 | 1.83k | } |
838 | 3.50k | else if(b_saved_bits_gt_zero) |
839 | 230 | { |
840 | | /* less_bits = max_drain_bits - _rem_bits_in_period */ |
841 | 230 | sub32_var_q(vq_max_drain_bits, vq_rem_bits_in_period, &vq_less_bits); |
842 | | |
843 | | /* allocated_saved_bits = MIN (less_bits, saved_bits) */ |
844 | 230 | MIN_VARQ(ps_bit_allocation->vq_saved_bits, vq_less_bits, |
845 | 230 | vq_allocated_saved_bits); |
846 | | |
847 | | /* rem_bits_in_period += allocted_save_bits */ |
848 | 230 | add32_var_q(ps_rbip->vq_rem_bits_in_period, vq_allocated_saved_bits, |
849 | 230 | &ps_rbip->vq_rem_bits_in_period); |
850 | | |
851 | | /* saved_bits -= allocted_save_bits */ |
852 | 230 | sub32_var_q(ps_bit_allocation->vq_saved_bits, vq_allocated_saved_bits, |
853 | 230 | &ps_bit_allocation->vq_saved_bits); |
854 | 230 | } |
855 | 5.92k | return; |
856 | 5.92k | } |
857 | | |
858 | | WORD32 irc_ba_get_frame_rate(bit_allocation_t *ps_bit_allocation) |
859 | 108k | { |
860 | 108k | return (ps_bit_allocation->i4_frame_rate); |
861 | 108k | } |
862 | | |
863 | | WORD32 irc_ba_get_bit_rate(bit_allocation_t *ps_bit_allocation) |
864 | 219k | { |
865 | 219k | return (ps_bit_allocation->i4_bit_rate); |
866 | 219k | } |
867 | | |
868 | | void irc_ba_get_peak_bit_rate(bit_allocation_t *ps_bit_allocation, |
869 | | WORD32 *pi4_peak_bit_rate) |
870 | 0 | { |
871 | 0 | WORD32 i; |
872 | 0 | for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) |
873 | 0 | { |
874 | 0 | pi4_peak_bit_rate[i] = ps_bit_allocation->ai4_peak_bit_rate[i]; |
875 | 0 | } |
876 | 0 | } |