/src/libavc/encoder/svc/isvce_sub_pic_rc.c
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2022 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 | | /** |
22 | | ******************************************************************************* |
23 | | * @file |
24 | | * isvce_sub_pic_rc.c |
25 | | * |
26 | | * @brief |
27 | | * Contains functions used in sub-pic RC |
28 | | * |
29 | | ******************************************************************************* |
30 | | */ |
31 | | #include <stdbool.h> |
32 | | #include <stdio.h> |
33 | | #include <string.h> |
34 | | #include <math.h> |
35 | | |
36 | | #include "ih264_typedefs.h" |
37 | | #include "ih264_cavlc_tables.h" |
38 | | #include "ih264_platform_macros.h" |
39 | | #include "ithread.h" |
40 | | #include "isvc_defs.h" |
41 | | #include "isvc_structs.h" |
42 | | #include "isvce_structs.h" |
43 | | #include "isvce_defs.h" |
44 | | #include "isvce_sub_pic_rc.h" |
45 | | #include "isvce_sub_pic_rc_private_defs.h" |
46 | | |
47 | | /* Dependencies of 'irc_picture_type.h' */ |
48 | | #include "irc_mem_req_and_acq.h" |
49 | | |
50 | | /* Dependencies of 'irc_rate_control_api_structs' */ |
51 | | #include "irc_picture_type.h" |
52 | | #include "irc_rd_model.h" |
53 | | #include "irc_vbr_storage_vbv.h" |
54 | | #include "irc_est_sad.h" |
55 | | #include "irc_bit_allocation.h" |
56 | | #include "irc_mb_model_based.h" |
57 | | #include "irc_cbr_buffer_control.h" |
58 | | #include "irc_vbr_str_prms.h" |
59 | | #include "irc_common.h" |
60 | | |
61 | | #include "irc_rate_control_api_structs.h" |
62 | | #include "irc_rate_control_api.h" |
63 | | |
64 | | /** |
65 | | ******************************************************************************* |
66 | | * |
67 | | * @brief |
68 | | * Returns size of buffers for storing subPicRC ctxt |
69 | | * |
70 | | * @returns Size of buffers |
71 | | * |
72 | | ******************************************************************************* |
73 | | */ |
74 | | UWORD32 isvce_get_sub_pic_rc_ctxt_size(UWORD8 u1_num_spatial_layers, DOUBLE d_spatial_res_ratio, |
75 | | UWORD32 u4_wd, UWORD32 u4_ht) |
76 | 16.3k | { |
77 | 16.3k | WORD32 i; |
78 | | |
79 | 16.3k | UWORD32 u4_size = MAX_PROCESS_CTXT * sizeof(svc_sub_pic_rc_ctxt_t); |
80 | | |
81 | 16.3k | u4_size += sizeof(sub_pic_rc_state_t); |
82 | 16.3k | u4_size += ithread_get_mutex_struct_size(); |
83 | | |
84 | 54.2k | for(i = u1_num_spatial_layers - 1; i >= 0; i--) |
85 | 37.9k | { |
86 | 37.9k | WORD32 i4_layer_wd = |
87 | 37.9k | (WORD32) ((DOUBLE) u4_wd / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - i)) + |
88 | 37.9k | 0.99; |
89 | 37.9k | WORD32 i4_layer_ht = |
90 | 37.9k | ((DOUBLE) u4_ht / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - i)) + 0.99; |
91 | 37.9k | WORD32 i4_layer_mbs = (i4_layer_wd / MB_SIZE) * (i4_layer_ht / MB_SIZE); |
92 | | |
93 | | /* ps_mb_bits_info */ |
94 | 37.9k | u4_size += i4_layer_mbs * sizeof(mb_bits_info_t); |
95 | | |
96 | | #if DUMP_SUB_PIC_RC_DATA |
97 | | /* ps_mb_bits_actual */ |
98 | | u4_size += i4_layer_mbs * sizeof(mb_bits_info_t); |
99 | | #endif |
100 | 37.9k | } |
101 | | |
102 | 16.3k | return u4_size; |
103 | 16.3k | } |
104 | | |
105 | | void isvce_sub_pic_rc_ctxt_init(isvce_codec_t *ps_codec, iv_mem_rec_t *ps_mem_rec) |
106 | 5.44k | { |
107 | 5.44k | sub_pic_rc_state_t *ps_sub_pic_rc_state; |
108 | | |
109 | 5.44k | WORD32 i, j; |
110 | | |
111 | 5.44k | DOUBLE d_spatial_res_ratio = ps_codec->s_cfg.s_svc_params.d_spatial_res_ratio; |
112 | 5.44k | UWORD8 u1_num_spatial_layers = ps_codec->s_cfg.s_svc_params.u1_num_spatial_layers; |
113 | 5.44k | UWORD32 u4_wd = ps_codec->s_cfg.u4_wd; |
114 | 5.44k | UWORD32 u4_ht = ps_codec->s_cfg.u4_ht; |
115 | 5.44k | UWORD8 *pu1_buf = ps_mem_rec->pv_base; |
116 | 5.44k | WORD64 i8_alloc_mem_size = |
117 | 5.44k | isvce_get_sub_pic_rc_ctxt_size(u1_num_spatial_layers, d_spatial_res_ratio, u4_wd, u4_ht); |
118 | | |
119 | 48.9k | for(i = 0; i < MAX_PROCESS_CTXT; i++) |
120 | 43.5k | { |
121 | 43.5k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt = ps_codec->as_process[i].ps_sub_pic_rc_ctxt = |
122 | 43.5k | (svc_sub_pic_rc_ctxt_t *) pu1_buf; |
123 | | |
124 | 43.5k | pu1_buf += sizeof(ps_sub_pic_rc_ctxt[0]); |
125 | 43.5k | i8_alloc_mem_size -= sizeof(ps_sub_pic_rc_ctxt[0]); |
126 | | |
127 | 43.5k | if(0 == i) |
128 | 5.44k | { |
129 | 5.44k | ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants.pv_state = ps_sub_pic_rc_state = |
130 | 5.44k | (sub_pic_rc_state_t *) pu1_buf; |
131 | 5.44k | pu1_buf += sizeof(ps_sub_pic_rc_state[0]); |
132 | 5.44k | i8_alloc_mem_size -= sizeof(ps_sub_pic_rc_state[0]); |
133 | | |
134 | 5.44k | ASSERT(i8_alloc_mem_size >= 0); |
135 | 5.44k | ASSERT(NULL != ps_codec->s_rate_control.apps_rate_control_api); |
136 | 5.44k | ASSERT(NULL != ps_codec->as_process->s_me_ctxt.pu1_mv_bits); |
137 | | |
138 | 5.44k | ps_sub_pic_rc_state->s_svc_params = ps_codec->s_cfg.s_svc_params; |
139 | 5.44k | ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map = gau1_uev_codeword_to_bits_map; |
140 | 5.44k | ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map = |
141 | 5.44k | ps_codec->as_process->s_me_ctxt.pu1_mv_bits; |
142 | 5.44k | ps_sub_pic_rc_state->e_rc_mode = ps_codec->s_cfg.e_rc_mode; |
143 | | |
144 | 5.44k | ps_sub_pic_rc_state->pv_bits_accumulator_mutex = (void *) pu1_buf; |
145 | 5.44k | pu1_buf += ithread_get_mutex_struct_size(); |
146 | 5.44k | i8_alloc_mem_size -= ithread_get_mutex_struct_size(); |
147 | 5.44k | ithread_mutex_init(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
148 | | |
149 | 18.0k | for(j = u1_num_spatial_layers - 1; j >= 0; j--) |
150 | 12.6k | { |
151 | 12.6k | sub_pic_rc_layer_state_t *ps_layer_state = |
152 | 12.6k | &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[j]; |
153 | | |
154 | 12.6k | WORD32 i4_layer_wd = |
155 | 12.6k | (WORD32) ((DOUBLE) u4_wd / |
156 | 12.6k | pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - j)) + |
157 | 12.6k | 0.99; |
158 | 12.6k | WORD32 i4_layer_ht = |
159 | 12.6k | ((DOUBLE) u4_ht / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - j)) + |
160 | 12.6k | 0.99; |
161 | 12.6k | WORD32 i4_layer_mbs = (i4_layer_wd / MB_SIZE) * (i4_layer_ht / MB_SIZE); |
162 | | |
163 | 12.6k | ps_layer_state->i4_wd = i4_layer_wd; |
164 | 12.6k | ps_layer_state->i4_ht = i4_layer_ht; |
165 | 12.6k | ps_layer_state->i4_num_mbs = i4_layer_mbs; |
166 | 12.6k | ps_layer_state->pv_layer_rc_ctxt = |
167 | 12.6k | ps_codec->s_rate_control.apps_rate_control_api[j]; |
168 | 12.6k | ps_layer_state->ps_mb_bits_info = (mb_bits_info_t *) pu1_buf; |
169 | 12.6k | pu1_buf += i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_info[0]); |
170 | 12.6k | i8_alloc_mem_size -= i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_info[0]); |
171 | | |
172 | 12.6k | ASSERT(i8_alloc_mem_size >= 0); |
173 | | |
174 | | #if DUMP_SUB_PIC_RC_DATA |
175 | | ps_layer_state->ps_mb_bits_actual = (mb_bits_info_t *) pu1_buf; |
176 | | pu1_buf += i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_actual[0]); |
177 | | i8_alloc_mem_size -= i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_actual[0]); |
178 | | |
179 | | ASSERT(i8_alloc_mem_size >= 0); |
180 | | |
181 | | { |
182 | | UWORD8 au1_file_path[MAX_SUB_PIC_RC_DUMP_FILE_PATH_LENGTH + 1]; |
183 | | |
184 | | sprintf((WORD8 *) au1_file_path, "%ssubPicRC%1d.txt", SUB_PIC_RC_DUMP_FILE_PATH, |
185 | | j); |
186 | | |
187 | | ps_layer_state->ps_data_dump_file = fopen(au1_file_path, "w"); |
188 | | |
189 | | ASSERT(NULL != ps_layer_state->ps_data_dump_file); |
190 | | } |
191 | | #endif |
192 | 12.6k | } |
193 | 5.44k | } |
194 | 38.0k | else |
195 | 38.0k | { |
196 | 38.0k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt_src = |
197 | 38.0k | ps_codec->as_process[0].ps_sub_pic_rc_ctxt; |
198 | 38.0k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt_dst = |
199 | 38.0k | ps_codec->as_process[i].ps_sub_pic_rc_ctxt; |
200 | 38.0k | sub_pic_rc_state_t *ps_proc0_state = |
201 | 38.0k | (sub_pic_rc_state_t *) ps_sub_pic_rc_ctxt_src->s_sub_pic_rc_constants.pv_state; |
202 | | |
203 | 38.0k | ps_sub_pic_rc_ctxt_dst->s_sub_pic_rc_constants.pv_state = ps_proc0_state; |
204 | 38.0k | } |
205 | 43.5k | } |
206 | 5.44k | } |
207 | | |
208 | | static FORCEINLINE void isvce_sub_pic_rc_qp_params_init(sub_pic_rc_qp_params_t *ps_qp_params, |
209 | | UWORD8 u1_min_qp, UWORD8 u1_max_qp) |
210 | 507k | { |
211 | 507k | ps_qp_params->u1_min_qp = u1_min_qp; |
212 | 507k | ps_qp_params->u1_max_qp = u1_max_qp; |
213 | 507k | ps_qp_params->pu4_qp_to_qscale_map = gau4_qp_to_qscale_map; |
214 | 507k | ps_qp_params->pu1_qscale_to_qp_map = gau1_qscale_to_qp_map; |
215 | 507k | } |
216 | | |
217 | | void isvce_sub_pic_rc_ctxt_layer_init(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
218 | 507k | { |
219 | 507k | sub_pic_rc_layer_state_t *ps_layer_state; |
220 | | |
221 | 507k | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
222 | 507k | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
223 | 507k | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
224 | 507k | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
225 | 507k | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
226 | 507k | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
227 | | |
228 | 507k | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
229 | | |
230 | 507k | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
231 | | |
232 | 507k | memset(&ps_layer_state->s_cumulative_mb_bits, 0, sizeof(ps_layer_state->s_cumulative_mb_bits)); |
233 | 507k | ps_layer_state->u4_num_mbs_sampled = 0; |
234 | | |
235 | | /* Frames with frameNum=0 are usually IDR's. RC model will be reset for IDR's. |
236 | | */ |
237 | | /* Hence, using VBVBufSize as a proxy for estimated bits */ |
238 | 507k | if(0 == ps_sub_pic_rc_variables->s_layer_variables.i4_frame_num) |
239 | 157k | { |
240 | 157k | ps_layer_state->u4_allocated_bits = |
241 | 157k | irc_get_vbv_buf_size(ps_layer_state->pv_layer_rc_ctxt) / 10.; |
242 | 157k | } |
243 | 349k | else |
244 | 349k | { |
245 | 349k | ps_layer_state->u4_allocated_bits = |
246 | 349k | irc_get_prev_frm_est_bits(ps_layer_state->pv_layer_rc_ctxt); |
247 | 349k | } |
248 | | |
249 | 507k | isvce_sub_pic_rc_qp_params_init(&ps_layer_state->s_qp_params, |
250 | 507k | ps_sub_pic_rc_variables->s_layer_variables.u1_min_qp, |
251 | 507k | ps_sub_pic_rc_variables->s_layer_variables.u1_max_qp); |
252 | 507k | } |
253 | | |
254 | | static FORCEINLINE UWORD32 isvce_sub_pic_rc_get_res_pred_flag_bits( |
255 | | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables, sub_pic_rc_state_t *ps_sub_pic_rc_state) |
256 | 6.26M | { |
257 | 6.26M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
258 | | |
259 | 6.26M | UNUSED(ps_sub_pic_rc_state); |
260 | | |
261 | 6.26M | return (ENABLE_RESIDUAL_PREDICTION && !ps_mb_info->u1_is_intra); |
262 | 6.26M | } |
263 | | |
264 | | static FORCEINLINE UWORD32 isvce_sub_pic_rc_get_cbp_bits( |
265 | | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables, sub_pic_rc_state_t *ps_sub_pic_rc_state) |
266 | 2.26M | { |
267 | 2.26M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
268 | | |
269 | 2.26M | UWORD32 u4_cbp = ps_sub_pic_rc_variables->s_mb_variables.u4_cbp; |
270 | 2.26M | bool b_use_inter_cbp_map = !ps_mb_info->u1_is_intra || ps_mb_info->u1_base_mode_flag; |
271 | | |
272 | 2.26M | return ps_sub_pic_rc_state |
273 | 2.26M | ->pu1_uev_codeword_to_bits_map[gu1_cbp_map_tables[u4_cbp][b_use_inter_cbp_map]]; |
274 | 2.26M | } |
275 | | |
276 | | static FORCEINLINE UWORD32 isvce_sub_pic_rc_get_mb_type_bits( |
277 | | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables, sub_pic_rc_state_t *ps_sub_pic_rc_state) |
278 | 3.99M | { |
279 | 3.99M | UWORD32 u4_mb_type; |
280 | | |
281 | 3.99M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
282 | | |
283 | 3.99M | UWORD32 u4_cbp = ps_sub_pic_rc_variables->s_mb_variables.u4_cbp; |
284 | 3.99M | UWORD32 au4_cbps[NUM_SP_COMPONENTS] = {u4_cbp & 15, u4_cbp >> 4}; |
285 | | |
286 | 3.99M | switch(ps_mb_info->u2_mb_type) |
287 | 3.99M | { |
288 | 3.37M | case I16x16: |
289 | 3.37M | { |
290 | 3.37M | u4_mb_type = ps_mb_info->s_intra_pu.s_i16x16_mode_data.u1_mode + 1 + |
291 | 3.37M | (au4_cbps[UV] << 2) + (au4_cbps[Y] == 15) * 12; |
292 | | |
293 | 3.37M | break; |
294 | 0 | } |
295 | 376k | case I4x4: |
296 | 376k | { |
297 | 376k | u4_mb_type = 5 * (ps_sub_pic_rc_variables->s_layer_variables.i4_slice_type != ISLICE); |
298 | | |
299 | 376k | break; |
300 | 0 | } |
301 | 245k | case P16x16: |
302 | 245k | { |
303 | 245k | u4_mb_type = 0; |
304 | | |
305 | 245k | break; |
306 | 0 | } |
307 | 0 | default: |
308 | 0 | { |
309 | 0 | return 0; |
310 | 0 | } |
311 | 3.99M | } |
312 | | |
313 | 3.99M | return ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map[u4_mb_type]; |
314 | 3.99M | } |
315 | | |
316 | | static FORCEINLINE UWORD32 isvce_sub_pic_rc_get_mb_pred_bits( |
317 | | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables, sub_pic_rc_state_t *ps_sub_pic_rc_state) |
318 | 4.00M | { |
319 | 4.00M | WORD32 i; |
320 | | |
321 | 4.00M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
322 | | |
323 | 4.00M | UWORD32 u4_bits = 0; |
324 | | |
325 | 4.00M | switch(ps_mb_info->u2_mb_type) |
326 | 4.00M | { |
327 | 3.37M | case I16x16: |
328 | 3.37M | { |
329 | | /* intra_chroma_pred_mode */ |
330 | 3.37M | u4_bits += |
331 | 3.37M | ps_sub_pic_rc_state |
332 | 3.37M | ->pu1_uev_codeword_to_bits_map[ps_mb_info->s_intra_pu.u1_chroma_intra_mode]; |
333 | | |
334 | 3.37M | break; |
335 | 0 | } |
336 | 376k | case I4x4: |
337 | 376k | { |
338 | 376k | intra4x4_mode_data_t *ps_i4x4_mode_data = ps_mb_info->s_intra_pu.as_i4x4_mode_data; |
339 | | |
340 | 6.38M | for(i = 0; i < MAX_TU_IN_MB; i++) |
341 | 6.00M | { |
342 | | /* prev_intra4x4_pred_mode_flag */ |
343 | 6.00M | u4_bits += 1; |
344 | | |
345 | | /* rem_intra4x4_pred_mode */ |
346 | 6.00M | u4_bits += |
347 | 6.00M | 3 * (ps_i4x4_mode_data[i].u1_mode != ps_i4x4_mode_data[i].u1_predicted_mode); |
348 | 6.00M | } |
349 | | |
350 | | /* intra_chroma_pred_mode */ |
351 | 376k | u4_bits += |
352 | 376k | ps_sub_pic_rc_state |
353 | 376k | ->pu1_uev_codeword_to_bits_map[ps_mb_info->s_intra_pu.u1_chroma_intra_mode]; |
354 | | |
355 | 376k | break; |
356 | 0 | } |
357 | 245k | case P16x16: |
358 | 245k | { |
359 | 245k | mv_t s_mvd; |
360 | | |
361 | | /* motion_prediction_flag_l0 */ |
362 | 245k | u4_bits += USE_ILP_MV_AS_MVP; |
363 | | |
364 | | /* ref_idx_l0 */ |
365 | 245k | if(2 == ps_sub_pic_rc_variables->s_layer_variables.i4_max_num_reference_frames) |
366 | 3.08k | { |
367 | 3.08k | u4_bits += 1; |
368 | 3.08k | } |
369 | 242k | else if(2 < ps_sub_pic_rc_variables->s_layer_variables.i4_max_num_reference_frames) |
370 | 115k | { |
371 | 115k | u4_bits += ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map |
372 | 115k | [ps_mb_info->as_pu->as_me_info[L0].i1_ref_idx]; |
373 | 115k | } |
374 | | |
375 | | /* mvd_l0 */ |
376 | 245k | s_mvd.i2_mvx = ps_mb_info->as_pu->as_me_info[L0].s_mv.i2_mvx - |
377 | 245k | ps_sub_pic_rc_variables->s_mb_variables |
378 | 245k | .aps_mvps[ps_mb_info->as_pu->au1_mvp_idx[L0]] |
379 | 245k | ->s_mv.i2_mvx; |
380 | 245k | s_mvd.i2_mvy = ps_mb_info->as_pu->as_me_info[L0].s_mv.i2_mvy - |
381 | 245k | ps_sub_pic_rc_variables->s_mb_variables |
382 | 245k | .aps_mvps[ps_mb_info->as_pu->au1_mvp_idx[L0]] |
383 | 245k | ->s_mv.i2_mvy; |
384 | 245k | u4_bits += ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map[s_mvd.i2_mvx]; |
385 | 245k | u4_bits += ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map[s_mvd.i2_mvy]; |
386 | | |
387 | 245k | break; |
388 | 0 | } |
389 | 0 | default: |
390 | 0 | { |
391 | 0 | break; |
392 | 0 | } |
393 | 4.00M | } |
394 | | |
395 | 4.00M | return u4_bits; |
396 | 4.00M | } |
397 | | |
398 | | static void ihevce_svc_sub_pic_rc_set_header_bits(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
399 | 6.24M | { |
400 | 6.24M | sub_pic_rc_layer_state_t *ps_layer_state; |
401 | 6.24M | mb_bits_info_t *ps_mb_bits_info; |
402 | | |
403 | 6.24M | UWORD32 u4_mb_idx; |
404 | | |
405 | 6.24M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
406 | 6.24M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
407 | 6.24M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
408 | 6.24M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
409 | 6.24M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
410 | 6.24M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
411 | 6.24M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
412 | | |
413 | 6.24M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
414 | | |
415 | 6.24M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
416 | 6.24M | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
417 | 6.24M | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
418 | 6.24M | (ps_layer_state->i4_wd / MB_SIZE); |
419 | 6.24M | ps_mb_bits_info = &ps_layer_state->ps_mb_bits_info[u4_mb_idx]; |
420 | | |
421 | | /* Hypotheses used for header bits estimation - */ |
422 | | /* 1. mb_skip_run, base_mode_flag, mb_type, mb_pred, residual_prediction_flag, |
423 | | * and cbp */ |
424 | | /* are considered as contibuting to header bits. */ |
425 | | /* 2. mb_skip_run = 1 bit */ |
426 | | /* 3. base_mode_flag = 1 bit */ |
427 | | /* 4. mb_type = LUT mapping mbType to corresponding ue(v) */ |
428 | | /* 5. mb_pred.I4x4 = 1 bit for 16 'prev_intra4x4_pred_mode_flag'; */ |
429 | | /* 3 bits for each explicitly signaled |
430 | | * 'rem_intra4x4_pred_mode' */ |
431 | | /* 6. mb_pred.Inter = 1 bit for 'motion_prediction_flag_l0' and |
432 | | * 'motion_prediction_flag_l1', when necessary; */ |
433 | | /* mvbits LUT for 'mvd_l0' and 'mvd_l1' */ |
434 | | /* 7. mb_pred.intra_chroma_pred_mode = LUT mapping intra_chroma_pred_mode to |
435 | | * corresponding ue(v) */ |
436 | | /* 8. residual_prediction_flag = 1 bit */ |
437 | | /* 9. coded_block_pattern = LUT mapping mbType to corresponding me(v) */ |
438 | | |
439 | | /* mb_skip_run is assumed to be either 0 or 1 */ |
440 | 6.24M | ps_mb_bits_info->i8_header_bits += 1; |
441 | | |
442 | | /* 'base_mode_flag' */ |
443 | 6.26M | if((ENABLE_ILP_MV || ENABLE_IBL_MODE) && u1_spatial_layer_id) |
444 | 4.71M | { |
445 | 4.71M | ps_mb_bits_info->i8_header_bits += 1; |
446 | | |
447 | 4.71M | if(ps_mb_info->u1_base_mode_flag) |
448 | 2.26M | { |
449 | | /* 'residual_prediction_flag' */ |
450 | 2.26M | ps_mb_bits_info->i8_header_bits += isvce_sub_pic_rc_get_res_pred_flag_bits( |
451 | 2.26M | ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
452 | | |
453 | | /* 'coded_block_pattern' */ |
454 | 2.26M | ps_mb_bits_info->i8_header_bits += |
455 | 2.26M | isvce_sub_pic_rc_get_cbp_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
456 | | |
457 | 2.26M | return; |
458 | 2.26M | } |
459 | 4.71M | } |
460 | | |
461 | | /* 'mb_type' */ |
462 | 3.98M | ps_mb_bits_info->i8_header_bits += |
463 | 3.98M | isvce_sub_pic_rc_get_mb_type_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
464 | | |
465 | 3.98M | if(PSKIP == ps_mb_info->u2_mb_type) |
466 | 0 | { |
467 | 0 | return; |
468 | 0 | } |
469 | | |
470 | | /* 'mb_pred' */ |
471 | 3.98M | ps_mb_bits_info->i8_header_bits += |
472 | 3.98M | isvce_sub_pic_rc_get_mb_pred_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
473 | | |
474 | | /* 'residual_prediction_flag' */ |
475 | 3.98M | ps_mb_bits_info->i8_header_bits += |
476 | 3.98M | isvce_sub_pic_rc_get_res_pred_flag_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
477 | 3.98M | } |
478 | | |
479 | | static FORCEINLINE UWORD32 isvce_sub_pic_rc_get_tu_residual_bits( |
480 | | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables, WORD32 i4_coeff_start_idx, |
481 | | UWORD8 u1_num_coded_coeffs, UWORD8 u1_num_coeffs, bool b_is_chroma) |
482 | 4.06M | { |
483 | 4.06M | WORD32 i; |
484 | 4.06M | UWORD32 u4_num_bits; |
485 | | |
486 | 4.06M | UWORD32 u4_bits = 0; |
487 | 4.06M | WORD16 *pi2_coeff = |
488 | 4.06M | ((WORD16 *) ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[b_is_chroma ? UV : Y] |
489 | 4.06M | .pv_data) + |
490 | 4.06M | i4_coeff_start_idx; |
491 | | |
492 | 4.06M | if(0 == u1_num_coded_coeffs) |
493 | 766k | { |
494 | 766k | return 0; |
495 | 766k | } |
496 | | |
497 | 3.30M | GETRANGE(u4_num_bits, u1_num_coded_coeffs); |
498 | 3.30M | u4_bits += u4_num_bits; |
499 | | |
500 | 43.5M | for(i = 0; i < u1_num_coeffs; i++) |
501 | 40.2M | { |
502 | 40.2M | if(pi2_coeff[i]) |
503 | 12.1M | { |
504 | 12.1M | GETRANGE(u4_num_bits, pi2_coeff[i]); |
505 | 12.1M | u4_bits += u4_num_bits; |
506 | 12.1M | } |
507 | 40.2M | } |
508 | 3.30M | return u4_bits; |
509 | 4.06M | } |
510 | | |
511 | | static void ihevce_svc_sub_pic_rc_set_texture_bits(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
512 | 6.26M | { |
513 | 6.26M | sub_pic_rc_layer_state_t *ps_layer_state; |
514 | 6.26M | mb_bits_info_t *ps_mb_bits_info; |
515 | | |
516 | 6.26M | UWORD32 u4_mb_idx; |
517 | 6.26M | WORD32 i, j; |
518 | | |
519 | 6.26M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
520 | 6.26M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
521 | 6.26M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
522 | 6.26M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
523 | 6.26M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
524 | 6.26M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
525 | 6.26M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
526 | | |
527 | 6.26M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
528 | 6.26M | UWORD32 au4_cbps[NUM_SP_COMPONENTS] = {ps_sub_pic_rc_variables->s_mb_variables.u4_cbp & 15, |
529 | 6.26M | ps_sub_pic_rc_variables->s_mb_variables.u4_cbp >> 4}; |
530 | | |
531 | 6.26M | if(0 == ps_sub_pic_rc_variables->s_mb_variables.u4_cbp) |
532 | 5.27M | { |
533 | 5.27M | return; |
534 | 5.27M | } |
535 | | |
536 | 993k | if(MIN_TU_SIZE != ps_mb_info->u1_tx_size) |
537 | 0 | { |
538 | 0 | return; |
539 | 0 | } |
540 | | |
541 | 993k | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
542 | 993k | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
543 | 993k | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
544 | 993k | (ps_layer_state->i4_wd / MB_SIZE); |
545 | 993k | ps_mb_bits_info = &ps_layer_state->ps_mb_bits_info[u4_mb_idx]; |
546 | | |
547 | | /* Hypotheses used for texture bits estimation - */ |
548 | | /* 1. Only level information is considered. */ |
549 | | /* 2. nnz is used as a proxy for coeff_token. */ |
550 | | /* 3. Both of the above are assumed coded via i(n). */ |
551 | 993k | if(au4_cbps[Y]) |
552 | 792k | { |
553 | | /* Y - DC */ |
554 | 792k | if(I16x16 == ps_mb_info->u2_mb_type) |
555 | 258k | { |
556 | 258k | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
557 | 258k | ps_sub_pic_rc_variables, 0, ps_sub_pic_rc_variables->s_mb_variables.apu1_nnzs[Y][0], |
558 | 258k | NUM_COEFFS_IN_MIN_TU, false); |
559 | 258k | } |
560 | | |
561 | 3.95M | for(i = 0; i < MIN_TU_IN_MB; i++) |
562 | 3.16M | { |
563 | 3.16M | if(au4_cbps[Y] & (1 << i)) |
564 | 2.78M | { |
565 | 2.78M | UWORD32 u4_csbp = (ps_mb_info->u4_csbp >> (4 * i)) & 15; |
566 | | |
567 | 13.9M | for(j = 0; j < NUM_4x4_IN_8x8; j++) |
568 | 11.1M | { |
569 | 11.1M | if(u4_csbp & (1 << j)) |
570 | 5.11M | { |
571 | | /* 1 added to account for DC TU */ |
572 | 5.11M | UWORD8 u1_blk_id = 1 + gau4_tu_zscan_id_to_rasterscan_id_map[i][j]; |
573 | 5.11M | UWORD8 u1_nnz = |
574 | 5.11M | ps_sub_pic_rc_variables->s_mb_variables.apu1_nnzs[Y][u1_blk_id]; |
575 | | |
576 | 5.11M | if(u1_nnz && (I16x16 == ps_mb_info->u2_mb_type)) |
577 | 482k | { |
578 | 482k | u1_nnz -= !!(((WORD16 *) (ps_sub_pic_rc_variables->s_mb_variables |
579 | 482k | .as_quant_coeffs[Y] |
580 | 482k | .pv_data))[u1_blk_id - 1]); |
581 | | |
582 | 482k | ps_mb_bits_info->i8_texture_bits += |
583 | 482k | isvce_sub_pic_rc_get_tu_residual_bits( |
584 | 482k | ps_sub_pic_rc_variables, |
585 | 482k | u1_blk_id * ps_sub_pic_rc_variables->s_mb_variables |
586 | 482k | .as_quant_coeffs[Y] |
587 | 482k | .i4_data_stride + |
588 | 482k | (I16x16 == ps_mb_info->u2_mb_type), |
589 | 482k | u1_nnz, |
590 | 482k | NUM_COEFFS_IN_MIN_TU - (I16x16 == ps_mb_info->u2_mb_type), |
591 | 482k | false); |
592 | 482k | } |
593 | 5.11M | } |
594 | 11.1M | } |
595 | 2.78M | } |
596 | 3.16M | } |
597 | 792k | } |
598 | | |
599 | 993k | if(au4_cbps[UV]) |
600 | 498k | { |
601 | 1.49M | for(i = ((WORD32) U); i <= ((WORD32) V); i++) |
602 | 996k | { |
603 | 996k | bool b_is_v = (i == ((WORD32) V)); |
604 | | |
605 | 996k | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
606 | 996k | ps_sub_pic_rc_variables, b_is_v * NUM_4x4_IN_8x8, |
607 | 996k | ps_sub_pic_rc_variables->s_mb_variables |
608 | 996k | .apu1_nnzs[UV][0 + b_is_v * (1 + NUM_4x4_IN_8x8)], |
609 | 996k | NUM_4x4_IN_8x8, true); |
610 | | |
611 | 4.97M | for(j = 0; j < NUM_4x4_IN_8x8; j++) |
612 | 3.98M | { |
613 | 3.98M | UWORD8 u1_nnz = ps_sub_pic_rc_variables->s_mb_variables |
614 | 3.98M | .apu1_nnzs[UV][j + b_is_v * (1 + NUM_4x4_IN_8x8) + 1]; |
615 | | |
616 | 3.98M | if(u1_nnz) |
617 | 2.33M | { |
618 | 2.33M | u1_nnz -= |
619 | 2.33M | !!(((WORD16 *) (ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[UV] |
620 | 2.33M | .pv_data))[j + b_is_v * NUM_4x4_IN_8x8]); |
621 | | |
622 | 2.33M | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
623 | 2.33M | ps_sub_pic_rc_variables, |
624 | 2.33M | (j + b_is_v * NUM_4x4_IN_8x8 + 1) * |
625 | 2.33M | ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[UV] |
626 | 2.33M | .i4_data_stride + |
627 | 2.33M | 1, |
628 | 2.33M | u1_nnz, NUM_COEFFS_IN_MIN_TU - 1, true); |
629 | 2.33M | } |
630 | 3.98M | } |
631 | 996k | } |
632 | 498k | } |
633 | 993k | } |
634 | | |
635 | | void isvce_sub_pic_rc_ctxt_update(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
636 | 9.65M | { |
637 | 9.65M | sub_pic_rc_layer_state_t *ps_layer_state; |
638 | 9.65M | mb_bits_info_t *ps_mb_bits_info; |
639 | | |
640 | 9.65M | UWORD32 u4_mb_idx; |
641 | | |
642 | 9.65M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
643 | 9.65M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
644 | 9.65M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
645 | 9.65M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
646 | 9.65M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
647 | 9.65M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
648 | 9.65M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
649 | | |
650 | 9.65M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
651 | 9.65M | bool b_is_skip_mb = (PSKIP == ps_mb_info->u2_mb_type) || (BSKIP == ps_mb_info->u2_mb_type); |
652 | | |
653 | 9.66M | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
654 | 3.18M | { |
655 | 3.18M | return; |
656 | 3.18M | } |
657 | | |
658 | 6.46M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
659 | 6.46M | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
660 | 6.46M | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
661 | 6.46M | (ps_layer_state->i4_wd / MB_SIZE); |
662 | 6.46M | ps_mb_bits_info = &ps_layer_state->ps_mb_bits_info[u4_mb_idx]; |
663 | | |
664 | 6.46M | memset(ps_mb_bits_info, 0, sizeof(ps_mb_bits_info[0])); |
665 | | |
666 | 6.46M | if(!b_is_skip_mb) |
667 | 6.24M | { |
668 | 6.24M | ihevce_svc_sub_pic_rc_set_header_bits(ps_sub_pic_rc_ctxt); |
669 | | |
670 | 6.24M | ihevce_svc_sub_pic_rc_set_texture_bits(ps_sub_pic_rc_ctxt); |
671 | 6.24M | } |
672 | | |
673 | 6.46M | ithread_mutex_lock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
674 | | |
675 | 6.46M | ps_layer_state->s_cumulative_mb_bits.i8_header_bits += ps_mb_bits_info->i8_header_bits; |
676 | 6.46M | ps_layer_state->s_cumulative_mb_bits.i8_texture_bits += ps_mb_bits_info->i8_texture_bits; |
677 | 6.46M | ps_layer_state->u4_num_mbs_sampled++; |
678 | | |
679 | 6.46M | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
680 | 6.46M | } |
681 | | |
682 | | UWORD8 isvce_sub_pic_rc_get_mb_qp(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt, UWORD8 u1_cur_mb_qp) |
683 | 9.67M | { |
684 | 9.67M | sub_pic_rc_layer_state_t *ps_layer_state; |
685 | | |
686 | 9.67M | DOUBLE d_bit_consumption_ratio; |
687 | 9.67M | UWORD32 u4_frame_qscale; |
688 | 9.67M | UWORD8 u1_mb_qp; |
689 | 9.67M | UWORD32 u4_num_mbs_sampled; |
690 | 9.67M | WORD32 i4_cumulative_mb_bits; |
691 | | |
692 | 9.67M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
693 | 9.67M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
694 | 9.67M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
695 | 9.67M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
696 | 9.67M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
697 | 9.67M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
698 | | |
699 | 9.67M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
700 | 9.67M | UWORD8 u1_frame_qp = ps_sub_pic_rc_variables->s_layer_variables.u1_frame_qp; |
701 | | |
702 | 9.67M | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
703 | 3.17M | { |
704 | 3.17M | return u1_cur_mb_qp; |
705 | 3.17M | } |
706 | | |
707 | 6.49M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
708 | | |
709 | 6.49M | ithread_mutex_lock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
710 | | |
711 | 6.49M | u4_num_mbs_sampled = ps_layer_state->u4_num_mbs_sampled; |
712 | | |
713 | 6.49M | if(u4_num_mbs_sampled < |
714 | 6.49M | ((UWORD32) ceil(MIN_SAMPLED_MB_RATIO * ((DOUBLE) ps_layer_state->i4_num_mbs)))) |
715 | 310k | { |
716 | 310k | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
717 | | |
718 | 310k | return u1_cur_mb_qp; |
719 | 310k | } |
720 | | |
721 | 6.18M | i4_cumulative_mb_bits = (WORD32) (ps_layer_state->s_cumulative_mb_bits.i8_header_bits + |
722 | 6.18M | ps_layer_state->s_cumulative_mb_bits.i8_texture_bits); |
723 | | |
724 | 6.18M | if((0 == ps_layer_state->u4_allocated_bits) || (0 == u4_num_mbs_sampled)) |
725 | 525k | { |
726 | 525k | d_bit_consumption_ratio = nextafter(BIT_RATIO_FOR_OVERCONSUMPTION, INFINITY); |
727 | 525k | } |
728 | 5.65M | else |
729 | 5.65M | { |
730 | 5.65M | d_bit_consumption_ratio = |
731 | 5.65M | (((DOUBLE) i4_cumulative_mb_bits) * ((DOUBLE) ps_layer_state->i4_num_mbs)) / |
732 | 5.65M | (((DOUBLE) ps_layer_state->u4_allocated_bits) * ((DOUBLE) u4_num_mbs_sampled)); |
733 | 5.65M | } |
734 | | |
735 | 6.18M | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
736 | | |
737 | 6.18M | if((d_bit_consumption_ratio > BIT_RATIO_FOR_OVERCONSUMPTION) || |
738 | 3.62M | (d_bit_consumption_ratio < BIT_RATIO_FOR_UNDERCONSUMPTION)) |
739 | 5.58M | { |
740 | 5.58M | u4_frame_qscale = ps_layer_state->s_qp_params.pu4_qp_to_qscale_map[u1_frame_qp] * |
741 | 5.58M | d_bit_consumption_ratio + |
742 | 5.58M | 0.5; |
743 | 5.58M | u4_frame_qscale = CLIP3(ps_layer_state->s_qp_params.pu4_qp_to_qscale_map[0], MAX_SVC_QSCALE, |
744 | 5.58M | u4_frame_qscale); |
745 | 5.58M | u1_mb_qp = ps_layer_state->s_qp_params.pu1_qscale_to_qp_map[u4_frame_qscale]; |
746 | 5.58M | u1_mb_qp = CLIP3(ps_layer_state->s_qp_params.u1_min_qp, |
747 | 5.58M | ps_layer_state->s_qp_params.u1_max_qp, u1_mb_qp); |
748 | 5.58M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_cur_mb_qp) - MAX_MB_QP_DECREMENT), |
749 | 5.58M | MIN(MAX_H264_QP, ((WORD16) u1_cur_mb_qp) + MAX_MB_QP_INCREMENT), |
750 | 5.58M | ((WORD16) u1_mb_qp)); |
751 | | /* This ensures mb_qp_delta stays within the interval [-26, 25] */ |
752 | 5.58M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_frame_qp) - MAX_FRAME_QP_DECREMENT), |
753 | 5.58M | MIN(MAX_H264_QP, ((WORD16) u1_frame_qp) + MAX_FRAME_QP_INCREMENT), |
754 | 5.58M | ((WORD16) u1_mb_qp)); |
755 | 5.58M | } |
756 | 595k | else |
757 | 595k | { |
758 | 595k | u1_mb_qp = u1_cur_mb_qp; |
759 | 595k | } |
760 | | |
761 | 6.18M | { |
762 | 6.18M | vbv_buf_status_e e_vbv_buf_status; |
763 | 6.18M | picture_type_e e_rc_pic_type; |
764 | | |
765 | 6.18M | DOUBLE d_est_frame_bits; |
766 | | |
767 | 6.18M | WORD32 i4_num_bits_to_prevent_vbv_underflow; |
768 | | |
769 | 6.18M | d_est_frame_bits = ((DOUBLE) i4_cumulative_mb_bits) * ((DOUBLE) ps_layer_state->i4_num_mbs); |
770 | 6.18M | d_est_frame_bits /= u4_num_mbs_sampled; |
771 | | |
772 | 6.18M | switch(ps_sub_pic_rc_variables->s_layer_variables.i4_slice_type) |
773 | 6.18M | { |
774 | 5.49M | case ISLICE: |
775 | 5.49M | { |
776 | 5.49M | e_rc_pic_type = I_PIC; |
777 | 5.49M | break; |
778 | 0 | } |
779 | 703k | case PSLICE: |
780 | 703k | { |
781 | 703k | e_rc_pic_type = P_PIC; |
782 | 703k | break; |
783 | 0 | } |
784 | 0 | default: |
785 | 0 | { |
786 | 0 | e_rc_pic_type = B_PIC; |
787 | 0 | break; |
788 | 0 | } |
789 | 6.18M | } |
790 | | |
791 | 6.20M | e_vbv_buf_status = |
792 | 6.20M | irc_get_buffer_status(ps_layer_state->pv_layer_rc_ctxt, (WORD32) d_est_frame_bits, |
793 | 6.20M | e_rc_pic_type, &i4_num_bits_to_prevent_vbv_underflow); |
794 | | |
795 | | /* This models dec VBV buffer */ |
796 | 6.20M | if(VBV_OVERFLOW == e_vbv_buf_status) |
797 | 2.59M | { |
798 | 2.59M | u1_mb_qp--; |
799 | 2.59M | } |
800 | 3.60M | else if(VBV_UNDERFLOW == e_vbv_buf_status) |
801 | 777k | { |
802 | 777k | u1_mb_qp++; |
803 | 777k | } |
804 | | |
805 | | /* This ensures mb_qp_delta stays within the interval [-26, 25] */ |
806 | 6.20M | u1_mb_qp = CLIP3(ps_layer_state->s_qp_params.u1_min_qp, |
807 | 6.20M | ps_layer_state->s_qp_params.u1_max_qp, u1_mb_qp); |
808 | 6.20M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_frame_qp) - MAX_FRAME_QP_DECREMENT), |
809 | 6.20M | MIN(MAX_H264_QP, ((WORD16) u1_frame_qp) + MAX_FRAME_QP_INCREMENT), |
810 | 6.20M | ((WORD16) u1_mb_qp)); |
811 | 6.20M | } |
812 | | |
813 | 0 | return u1_mb_qp; |
814 | 6.18M | } |
815 | | |
816 | | void isvce_sub_pic_rc_get_entropy_data(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
817 | 9.70M | { |
818 | | #if DUMP_SUB_PIC_RC_DATA |
819 | | sub_pic_rc_layer_state_t *ps_layer_state; |
820 | | |
821 | | UWORD32 u4_mb_idx; |
822 | | |
823 | | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
824 | | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
825 | | svc_sub_pic_rc_entropy_variables_t *ps_sub_pic_rc_variables = |
826 | | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_entropy_variables; |
827 | | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
828 | | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
829 | | |
830 | | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->u1_spatial_layer_id; |
831 | | |
832 | | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
833 | | { |
834 | | return; |
835 | | } |
836 | | |
837 | | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
838 | | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_pos.i4_abscissa + |
839 | | ps_sub_pic_rc_variables->s_mb_pos.i4_ordinate * (ps_layer_state->i4_wd / MB_SIZE); |
840 | | |
841 | | ps_layer_state->ps_mb_bits_actual[u4_mb_idx] = ps_sub_pic_rc_variables->s_mb_bits; |
842 | | #else |
843 | 9.70M | UNUSED(ps_sub_pic_rc_ctxt); |
844 | 9.70M | #endif |
845 | 9.70M | } |
846 | | |
847 | | void isvce_sub_pic_rc_dump_data(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
848 | 29.1k | { |
849 | | #if DUMP_SUB_PIC_RC_DATA |
850 | | WORD32 i, j, k; |
851 | | |
852 | | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
853 | | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
854 | | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
855 | | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
856 | | |
857 | | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
858 | | { |
859 | | return; |
860 | | } |
861 | | |
862 | | for(i = 0; i < ps_sub_pic_rc_state->s_svc_params.u1_num_spatial_layers; i++) |
863 | | { |
864 | | sub_pic_rc_layer_state_t *ps_layer_state = |
865 | | &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[i]; |
866 | | |
867 | | for(j = 0; j < (ps_layer_state->i4_ht / MB_SIZE); j++) |
868 | | { |
869 | | for(k = 0; k < (ps_layer_state->i4_wd / MB_SIZE); k++) |
870 | | { |
871 | | mb_bits_info_t *ps_mb_bits_est = |
872 | | &ps_layer_state->ps_mb_bits_info[k + j * (ps_layer_state->i4_wd / MB_SIZE)]; |
873 | | mb_bits_info_t *ps_mb_bits_actual = |
874 | | &ps_layer_state->ps_mb_bits_actual[k + j * (ps_layer_state->i4_wd / MB_SIZE)]; |
875 | | |
876 | | fprintf(ps_layer_state->ps_data_dump_file, "%ld,%ld,%ld,%ld,\n", |
877 | | ps_mb_bits_est->i8_header_bits, ps_mb_bits_est->i8_texture_bits, |
878 | | ps_mb_bits_actual->i8_header_bits, ps_mb_bits_actual->i8_texture_bits); |
879 | | } |
880 | | } |
881 | | } |
882 | | #else |
883 | 29.1k | UNUSED(ps_sub_pic_rc_ctxt); |
884 | 29.1k | #endif |
885 | 29.1k | } |
886 | | |
887 | | void isvce_sub_pic_rc_ctxt_delete(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
888 | 0 | { |
889 | 0 | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
890 | 0 | (sub_pic_rc_state_t *) ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants.pv_state; |
891 | |
|
892 | 0 | ithread_mutex_destroy(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
893 | |
|
894 | | #if DUMP_SUB_PIC_RC_DATA |
895 | | { |
896 | | WORD32 i; |
897 | | |
898 | | UWORD8 u1_num_spatial_layers = ps_sub_pic_rc_state->s_svc_params.u1_num_spatial_layers; |
899 | | |
900 | | for(i = u1_num_spatial_layers - 1; i >= 0; i--) |
901 | | { |
902 | | sub_pic_rc_layer_state_t *ps_layer_state = |
903 | | &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[i]; |
904 | | |
905 | | if(ps_layer_state->ps_data_dump_file) |
906 | | { |
907 | | fclose(ps_layer_state->ps_data_dump_file); |
908 | | } |
909 | | |
910 | | ps_layer_state->ps_data_dump_file = NULL; |
911 | | } |
912 | | } |
913 | | #endif |
914 | 0 | } |