/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 | 15.8k | { |
77 | 15.8k | WORD32 i; |
78 | | |
79 | 15.8k | UWORD32 u4_size = MAX_PROCESS_CTXT * sizeof(svc_sub_pic_rc_ctxt_t); |
80 | | |
81 | 15.8k | u4_size += sizeof(sub_pic_rc_state_t); |
82 | 15.8k | u4_size += ithread_get_mutex_struct_size(); |
83 | | |
84 | 52.7k | for(i = u1_num_spatial_layers - 1; i >= 0; i--) |
85 | 36.9k | { |
86 | 36.9k | WORD32 i4_layer_wd = |
87 | 36.9k | (WORD32) ((DOUBLE) u4_wd / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - i)) + |
88 | 36.9k | 0.99; |
89 | 36.9k | WORD32 i4_layer_ht = |
90 | 36.9k | ((DOUBLE) u4_ht / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - i)) + 0.99; |
91 | 36.9k | WORD32 i4_layer_mbs = (i4_layer_wd / MB_SIZE) * (i4_layer_ht / MB_SIZE); |
92 | | |
93 | | /* ps_mb_bits_info */ |
94 | 36.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 | 36.9k | } |
101 | | |
102 | 15.8k | return u4_size; |
103 | 15.8k | } |
104 | | |
105 | | void isvce_sub_pic_rc_ctxt_init(isvce_codec_t *ps_codec, iv_mem_rec_t *ps_mem_rec) |
106 | 5.29k | { |
107 | 5.29k | sub_pic_rc_state_t *ps_sub_pic_rc_state; |
108 | | |
109 | 5.29k | WORD32 i, j; |
110 | | |
111 | 5.29k | DOUBLE d_spatial_res_ratio = ps_codec->s_cfg.s_svc_params.d_spatial_res_ratio; |
112 | 5.29k | UWORD8 u1_num_spatial_layers = ps_codec->s_cfg.s_svc_params.u1_num_spatial_layers; |
113 | 5.29k | UWORD32 u4_wd = ps_codec->s_cfg.u4_wd; |
114 | 5.29k | UWORD32 u4_ht = ps_codec->s_cfg.u4_ht; |
115 | 5.29k | UWORD8 *pu1_buf = ps_mem_rec->pv_base; |
116 | 5.29k | WORD64 i8_alloc_mem_size = |
117 | 5.29k | isvce_get_sub_pic_rc_ctxt_size(u1_num_spatial_layers, d_spatial_res_ratio, u4_wd, u4_ht); |
118 | | |
119 | 47.6k | for(i = 0; i < MAX_PROCESS_CTXT; i++) |
120 | 42.3k | { |
121 | 42.3k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt = ps_codec->as_process[i].ps_sub_pic_rc_ctxt = |
122 | 42.3k | (svc_sub_pic_rc_ctxt_t *) pu1_buf; |
123 | | |
124 | 42.3k | pu1_buf += sizeof(ps_sub_pic_rc_ctxt[0]); |
125 | 42.3k | i8_alloc_mem_size -= sizeof(ps_sub_pic_rc_ctxt[0]); |
126 | | |
127 | 42.3k | if(0 == i) |
128 | 5.29k | { |
129 | 5.29k | ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants.pv_state = ps_sub_pic_rc_state = |
130 | 5.29k | (sub_pic_rc_state_t *) pu1_buf; |
131 | 5.29k | pu1_buf += sizeof(ps_sub_pic_rc_state[0]); |
132 | 5.29k | i8_alloc_mem_size -= sizeof(ps_sub_pic_rc_state[0]); |
133 | | |
134 | 5.29k | ASSERT(i8_alloc_mem_size >= 0); |
135 | 5.29k | ASSERT(NULL != ps_codec->s_rate_control.apps_rate_control_api); |
136 | 5.29k | ASSERT(NULL != ps_codec->as_process->s_me_ctxt.pu1_mv_bits); |
137 | | |
138 | 5.29k | ps_sub_pic_rc_state->s_svc_params = ps_codec->s_cfg.s_svc_params; |
139 | 5.29k | ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map = gau1_uev_codeword_to_bits_map; |
140 | 5.29k | ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map = |
141 | 5.29k | ps_codec->as_process->s_me_ctxt.pu1_mv_bits; |
142 | 5.29k | ps_sub_pic_rc_state->e_rc_mode = ps_codec->s_cfg.e_rc_mode; |
143 | | |
144 | 5.29k | ps_sub_pic_rc_state->pv_bits_accumulator_mutex = (void *) pu1_buf; |
145 | 5.29k | pu1_buf += ithread_get_mutex_struct_size(); |
146 | 5.29k | i8_alloc_mem_size -= ithread_get_mutex_struct_size(); |
147 | 5.29k | ithread_mutex_init(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
148 | | |
149 | 17.5k | for(j = u1_num_spatial_layers - 1; j >= 0; j--) |
150 | 12.2k | { |
151 | 12.2k | sub_pic_rc_layer_state_t *ps_layer_state = |
152 | 12.2k | &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[j]; |
153 | | |
154 | 12.2k | WORD32 i4_layer_wd = |
155 | 12.2k | (WORD32) ((DOUBLE) u4_wd / |
156 | 12.2k | pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - j)) + |
157 | 12.2k | 0.99; |
158 | 12.2k | WORD32 i4_layer_ht = |
159 | 12.2k | ((DOUBLE) u4_ht / pow(d_spatial_res_ratio, u1_num_spatial_layers - 1 - j)) + |
160 | 12.2k | 0.99; |
161 | 12.2k | WORD32 i4_layer_mbs = (i4_layer_wd / MB_SIZE) * (i4_layer_ht / MB_SIZE); |
162 | | |
163 | 12.2k | ps_layer_state->i4_wd = i4_layer_wd; |
164 | 12.2k | ps_layer_state->i4_ht = i4_layer_ht; |
165 | 12.2k | ps_layer_state->i4_num_mbs = i4_layer_mbs; |
166 | 12.2k | ps_layer_state->pv_layer_rc_ctxt = |
167 | 12.2k | ps_codec->s_rate_control.apps_rate_control_api[j]; |
168 | 12.2k | ps_layer_state->ps_mb_bits_info = (mb_bits_info_t *) pu1_buf; |
169 | 12.2k | pu1_buf += i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_info[0]); |
170 | 12.2k | i8_alloc_mem_size -= i4_layer_mbs * sizeof(ps_layer_state->ps_mb_bits_info[0]); |
171 | | |
172 | 12.2k | 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.2k | } |
193 | 5.29k | } |
194 | 37.0k | else |
195 | 37.0k | { |
196 | 37.0k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt_src = |
197 | 37.0k | ps_codec->as_process[0].ps_sub_pic_rc_ctxt; |
198 | 37.0k | svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt_dst = |
199 | 37.0k | ps_codec->as_process[i].ps_sub_pic_rc_ctxt; |
200 | 37.0k | sub_pic_rc_state_t *ps_proc0_state = |
201 | 37.0k | (sub_pic_rc_state_t *) ps_sub_pic_rc_ctxt_src->s_sub_pic_rc_constants.pv_state; |
202 | | |
203 | 37.0k | ps_sub_pic_rc_ctxt_dst->s_sub_pic_rc_constants.pv_state = ps_proc0_state; |
204 | 37.0k | } |
205 | 42.3k | } |
206 | 5.29k | } |
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 | 491k | { |
211 | 491k | ps_qp_params->u1_min_qp = u1_min_qp; |
212 | 491k | ps_qp_params->u1_max_qp = u1_max_qp; |
213 | 491k | ps_qp_params->pu4_qp_to_qscale_map = gau4_qp_to_qscale_map; |
214 | 491k | ps_qp_params->pu1_qscale_to_qp_map = gau1_qscale_to_qp_map; |
215 | 491k | } |
216 | | |
217 | | void isvce_sub_pic_rc_ctxt_layer_init(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
218 | 491k | { |
219 | 491k | sub_pic_rc_layer_state_t *ps_layer_state; |
220 | | |
221 | 491k | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
222 | 491k | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
223 | 491k | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
224 | 491k | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
225 | 491k | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
226 | 491k | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
227 | | |
228 | 491k | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
229 | | |
230 | 491k | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
231 | | |
232 | 491k | memset(&ps_layer_state->s_cumulative_mb_bits, 0, sizeof(ps_layer_state->s_cumulative_mb_bits)); |
233 | 491k | 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 | 491k | if(0 == ps_sub_pic_rc_variables->s_layer_variables.i4_frame_num) |
239 | 155k | { |
240 | 155k | ps_layer_state->u4_allocated_bits = |
241 | 155k | irc_get_vbv_buf_size(ps_layer_state->pv_layer_rc_ctxt) / 10.; |
242 | 155k | } |
243 | 335k | else |
244 | 335k | { |
245 | 335k | ps_layer_state->u4_allocated_bits = |
246 | 335k | irc_get_prev_frm_est_bits(ps_layer_state->pv_layer_rc_ctxt); |
247 | 335k | } |
248 | | |
249 | 491k | isvce_sub_pic_rc_qp_params_init(&ps_layer_state->s_qp_params, |
250 | 491k | ps_sub_pic_rc_variables->s_layer_variables.u1_min_qp, |
251 | 491k | ps_sub_pic_rc_variables->s_layer_variables.u1_max_qp); |
252 | 491k | } |
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.38M | { |
257 | 6.38M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
258 | | |
259 | 6.38M | UNUSED(ps_sub_pic_rc_state); |
260 | | |
261 | 6.38M | return (ENABLE_RESIDUAL_PREDICTION && !ps_mb_info->u1_is_intra); |
262 | 6.38M | } |
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.27M | { |
267 | 2.27M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
268 | | |
269 | 2.27M | UWORD32 u4_cbp = ps_sub_pic_rc_variables->s_mb_variables.u4_cbp; |
270 | 2.27M | bool b_use_inter_cbp_map = !ps_mb_info->u1_is_intra || ps_mb_info->u1_base_mode_flag; |
271 | | |
272 | 2.27M | return ps_sub_pic_rc_state |
273 | 2.27M | ->pu1_uev_codeword_to_bits_map[gu1_cbp_map_tables[u4_cbp][b_use_inter_cbp_map]]; |
274 | 2.27M | } |
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 | 4.11M | { |
279 | 4.11M | UWORD32 u4_mb_type; |
280 | | |
281 | 4.11M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
282 | | |
283 | 4.11M | UWORD32 u4_cbp = ps_sub_pic_rc_variables->s_mb_variables.u4_cbp; |
284 | 4.11M | UWORD32 au4_cbps[NUM_SP_COMPONENTS] = {u4_cbp & 15, u4_cbp >> 4}; |
285 | | |
286 | 4.11M | switch(ps_mb_info->u2_mb_type) |
287 | 4.11M | { |
288 | 3.45M | case I16x16: |
289 | 3.45M | { |
290 | 3.45M | u4_mb_type = ps_mb_info->s_intra_pu.s_i16x16_mode_data.u1_mode + 1 + |
291 | 3.45M | (au4_cbps[UV] << 2) + (au4_cbps[Y] == 15) * 12; |
292 | | |
293 | 3.45M | break; |
294 | 0 | } |
295 | 363k | case I4x4: |
296 | 363k | { |
297 | 363k | u4_mb_type = 5 * (ps_sub_pic_rc_variables->s_layer_variables.i4_slice_type != ISLICE); |
298 | | |
299 | 363k | break; |
300 | 0 | } |
301 | 290k | case P16x16: |
302 | 290k | { |
303 | 290k | u4_mb_type = 0; |
304 | | |
305 | 290k | break; |
306 | 0 | } |
307 | 0 | default: |
308 | 0 | { |
309 | 0 | return 0; |
310 | 0 | } |
311 | 4.11M | } |
312 | | |
313 | 4.11M | return ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map[u4_mb_type]; |
314 | 4.11M | } |
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.11M | { |
319 | 4.11M | WORD32 i; |
320 | | |
321 | 4.11M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
322 | | |
323 | 4.11M | UWORD32 u4_bits = 0; |
324 | | |
325 | 4.11M | switch(ps_mb_info->u2_mb_type) |
326 | 4.11M | { |
327 | 3.46M | case I16x16: |
328 | 3.46M | { |
329 | | /* intra_chroma_pred_mode */ |
330 | 3.46M | u4_bits += |
331 | 3.46M | ps_sub_pic_rc_state |
332 | 3.46M | ->pu1_uev_codeword_to_bits_map[ps_mb_info->s_intra_pu.u1_chroma_intra_mode]; |
333 | | |
334 | 3.46M | break; |
335 | 0 | } |
336 | 363k | case I4x4: |
337 | 363k | { |
338 | 363k | intra4x4_mode_data_t *ps_i4x4_mode_data = ps_mb_info->s_intra_pu.as_i4x4_mode_data; |
339 | | |
340 | 6.16M | for(i = 0; i < MAX_TU_IN_MB; i++) |
341 | 5.79M | { |
342 | | /* prev_intra4x4_pred_mode_flag */ |
343 | 5.79M | u4_bits += 1; |
344 | | |
345 | | /* rem_intra4x4_pred_mode */ |
346 | 5.79M | u4_bits += |
347 | 5.79M | 3 * (ps_i4x4_mode_data[i].u1_mode != ps_i4x4_mode_data[i].u1_predicted_mode); |
348 | 5.79M | } |
349 | | |
350 | | /* intra_chroma_pred_mode */ |
351 | 363k | u4_bits += |
352 | 363k | ps_sub_pic_rc_state |
353 | 363k | ->pu1_uev_codeword_to_bits_map[ps_mb_info->s_intra_pu.u1_chroma_intra_mode]; |
354 | | |
355 | 363k | break; |
356 | 0 | } |
357 | 290k | case P16x16: |
358 | 290k | { |
359 | 290k | mv_t s_mvd; |
360 | | |
361 | | /* motion_prediction_flag_l0 */ |
362 | 290k | u4_bits += USE_ILP_MV_AS_MVP; |
363 | | |
364 | | /* ref_idx_l0 */ |
365 | 290k | if(2 == ps_sub_pic_rc_variables->s_layer_variables.i4_max_num_reference_frames) |
366 | 3.65k | { |
367 | 3.65k | u4_bits += 1; |
368 | 3.65k | } |
369 | 286k | else if(2 < ps_sub_pic_rc_variables->s_layer_variables.i4_max_num_reference_frames) |
370 | 113k | { |
371 | 113k | u4_bits += ps_sub_pic_rc_state->pu1_uev_codeword_to_bits_map |
372 | 113k | [ps_mb_info->as_pu->as_me_info[L0].i1_ref_idx]; |
373 | 113k | } |
374 | | |
375 | | /* mvd_l0 */ |
376 | 290k | s_mvd.i2_mvx = ps_mb_info->as_pu->as_me_info[L0].s_mv.i2_mvx - |
377 | 290k | ps_sub_pic_rc_variables->s_mb_variables |
378 | 290k | .aps_mvps[ps_mb_info->as_pu->au1_mvp_idx[L0]] |
379 | 290k | ->s_mv.i2_mvx; |
380 | 290k | s_mvd.i2_mvy = ps_mb_info->as_pu->as_me_info[L0].s_mv.i2_mvy - |
381 | 290k | ps_sub_pic_rc_variables->s_mb_variables |
382 | 290k | .aps_mvps[ps_mb_info->as_pu->au1_mvp_idx[L0]] |
383 | 290k | ->s_mv.i2_mvy; |
384 | 290k | u4_bits += ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map[s_mvd.i2_mvx]; |
385 | 290k | u4_bits += ps_sub_pic_rc_state->pu1_sev_codeword_to_bits_map[s_mvd.i2_mvy]; |
386 | | |
387 | 290k | break; |
388 | 0 | } |
389 | 0 | default: |
390 | 0 | { |
391 | 0 | break; |
392 | 0 | } |
393 | 4.11M | } |
394 | | |
395 | 4.11M | return u4_bits; |
396 | 4.11M | } |
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.36M | { |
400 | 6.36M | sub_pic_rc_layer_state_t *ps_layer_state; |
401 | 6.36M | mb_bits_info_t *ps_mb_bits_info; |
402 | | |
403 | 6.36M | UWORD32 u4_mb_idx; |
404 | | |
405 | 6.36M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
406 | 6.36M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
407 | 6.36M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
408 | 6.36M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
409 | 6.36M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
410 | 6.36M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
411 | 6.36M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
412 | | |
413 | 6.36M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
414 | | |
415 | 6.36M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
416 | 6.36M | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
417 | 6.36M | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
418 | 6.36M | (ps_layer_state->i4_wd / MB_SIZE); |
419 | 6.36M | 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.36M | ps_mb_bits_info->i8_header_bits += 1; |
441 | | |
442 | | /* 'base_mode_flag' */ |
443 | 6.38M | if((ENABLE_ILP_MV || ENABLE_IBL_MODE) && u1_spatial_layer_id) |
444 | 4.84M | { |
445 | 4.84M | ps_mb_bits_info->i8_header_bits += 1; |
446 | | |
447 | 4.84M | if(ps_mb_info->u1_base_mode_flag) |
448 | 2.27M | { |
449 | | /* 'residual_prediction_flag' */ |
450 | 2.27M | ps_mb_bits_info->i8_header_bits += isvce_sub_pic_rc_get_res_pred_flag_bits( |
451 | 2.27M | ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
452 | | |
453 | | /* 'coded_block_pattern' */ |
454 | 2.27M | ps_mb_bits_info->i8_header_bits += |
455 | 2.27M | isvce_sub_pic_rc_get_cbp_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
456 | | |
457 | 2.27M | return; |
458 | 2.27M | } |
459 | 4.84M | } |
460 | | |
461 | | /* 'mb_type' */ |
462 | 4.09M | ps_mb_bits_info->i8_header_bits += |
463 | 4.09M | isvce_sub_pic_rc_get_mb_type_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
464 | | |
465 | 4.09M | if(PSKIP == ps_mb_info->u2_mb_type) |
466 | 0 | { |
467 | 0 | return; |
468 | 0 | } |
469 | | |
470 | | /* 'mb_pred' */ |
471 | 4.09M | ps_mb_bits_info->i8_header_bits += |
472 | 4.09M | isvce_sub_pic_rc_get_mb_pred_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
473 | | |
474 | | /* 'residual_prediction_flag' */ |
475 | 4.09M | ps_mb_bits_info->i8_header_bits += |
476 | 4.09M | isvce_sub_pic_rc_get_res_pred_flag_bits(ps_sub_pic_rc_variables, ps_sub_pic_rc_state); |
477 | 4.09M | } |
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 | 5.21M | { |
483 | 5.21M | WORD32 i; |
484 | 5.21M | UWORD32 u4_num_bits; |
485 | | |
486 | 5.21M | UWORD32 u4_bits = 0; |
487 | 5.21M | WORD16 *pi2_coeff = |
488 | 5.21M | ((WORD16 *) ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[b_is_chroma ? UV : Y] |
489 | 5.21M | .pv_data) + |
490 | 5.21M | i4_coeff_start_idx; |
491 | | |
492 | 5.21M | if(0 == u1_num_coded_coeffs) |
493 | 804k | { |
494 | 804k | return 0; |
495 | 804k | } |
496 | | |
497 | 4.41M | GETRANGE(u4_num_bits, u1_num_coded_coeffs); |
498 | 4.41M | u4_bits += u4_num_bits; |
499 | | |
500 | 59.0M | for(i = 0; i < u1_num_coeffs; i++) |
501 | 54.6M | { |
502 | 54.6M | if(pi2_coeff[i]) |
503 | 17.7M | { |
504 | 17.7M | GETRANGE(u4_num_bits, pi2_coeff[i]); |
505 | 17.7M | u4_bits += u4_num_bits; |
506 | 17.7M | } |
507 | 54.6M | } |
508 | 4.41M | return u4_bits; |
509 | 5.21M | } |
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.39M | { |
513 | 6.39M | sub_pic_rc_layer_state_t *ps_layer_state; |
514 | 6.39M | mb_bits_info_t *ps_mb_bits_info; |
515 | | |
516 | 6.39M | UWORD32 u4_mb_idx; |
517 | 6.39M | WORD32 i, j; |
518 | | |
519 | 6.39M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
520 | 6.39M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
521 | 6.39M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
522 | 6.39M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
523 | 6.39M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
524 | 6.39M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
525 | 6.39M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
526 | | |
527 | 6.39M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
528 | 6.39M | UWORD32 au4_cbps[NUM_SP_COMPONENTS] = {ps_sub_pic_rc_variables->s_mb_variables.u4_cbp & 15, |
529 | 6.39M | ps_sub_pic_rc_variables->s_mb_variables.u4_cbp >> 4}; |
530 | | |
531 | 6.39M | if(0 == ps_sub_pic_rc_variables->s_mb_variables.u4_cbp) |
532 | 5.27M | { |
533 | 5.27M | return; |
534 | 5.27M | } |
535 | | |
536 | 1.11M | if(MIN_TU_SIZE != ps_mb_info->u1_tx_size) |
537 | 0 | { |
538 | 0 | return; |
539 | 0 | } |
540 | | |
541 | 1.11M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
542 | 1.11M | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
543 | 1.11M | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
544 | 1.11M | (ps_layer_state->i4_wd / MB_SIZE); |
545 | 1.11M | 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 | 1.11M | if(au4_cbps[Y]) |
552 | 893k | { |
553 | | /* Y - DC */ |
554 | 893k | if(I16x16 == ps_mb_info->u2_mb_type) |
555 | 281k | { |
556 | 281k | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
557 | 281k | ps_sub_pic_rc_variables, 0, ps_sub_pic_rc_variables->s_mb_variables.apu1_nnzs[Y][0], |
558 | 281k | NUM_COEFFS_IN_MIN_TU, false); |
559 | 281k | } |
560 | | |
561 | 4.46M | for(i = 0; i < MIN_TU_IN_MB; i++) |
562 | 3.56M | { |
563 | 3.56M | if(au4_cbps[Y] & (1 << i)) |
564 | 3.14M | { |
565 | 3.14M | UWORD32 u4_csbp = (ps_mb_info->u4_csbp >> (4 * i)) & 15; |
566 | | |
567 | 15.6M | for(j = 0; j < NUM_4x4_IN_8x8; j++) |
568 | 12.5M | { |
569 | 12.5M | if(u4_csbp & (1 << j)) |
570 | 5.70M | { |
571 | | /* 1 added to account for DC TU */ |
572 | 5.70M | UWORD8 u1_blk_id = 1 + gau4_tu_zscan_id_to_rasterscan_id_map[i][j]; |
573 | 5.70M | UWORD8 u1_nnz = |
574 | 5.70M | ps_sub_pic_rc_variables->s_mb_variables.apu1_nnzs[Y][u1_blk_id]; |
575 | | |
576 | 5.70M | if(u1_nnz && (I16x16 == ps_mb_info->u2_mb_type)) |
577 | 636k | { |
578 | 636k | u1_nnz -= !!(((WORD16 *) (ps_sub_pic_rc_variables->s_mb_variables |
579 | 636k | .as_quant_coeffs[Y] |
580 | 636k | .pv_data))[u1_blk_id - 1]); |
581 | | |
582 | 636k | ps_mb_bits_info->i8_texture_bits += |
583 | 636k | isvce_sub_pic_rc_get_tu_residual_bits( |
584 | 636k | ps_sub_pic_rc_variables, |
585 | 636k | u1_blk_id * ps_sub_pic_rc_variables->s_mb_variables |
586 | 636k | .as_quant_coeffs[Y] |
587 | 636k | .i4_data_stride + |
588 | 636k | (I16x16 == ps_mb_info->u2_mb_type), |
589 | 636k | u1_nnz, |
590 | 636k | NUM_COEFFS_IN_MIN_TU - (I16x16 == ps_mb_info->u2_mb_type), |
591 | 636k | false); |
592 | 636k | } |
593 | 5.70M | } |
594 | 12.5M | } |
595 | 3.14M | } |
596 | 3.56M | } |
597 | 893k | } |
598 | | |
599 | 1.11M | if(au4_cbps[UV]) |
600 | 605k | { |
601 | 1.81M | for(i = ((WORD32) U); i <= ((WORD32) V); i++) |
602 | 1.20M | { |
603 | 1.20M | bool b_is_v = (i == ((WORD32) V)); |
604 | | |
605 | 1.20M | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
606 | 1.20M | ps_sub_pic_rc_variables, b_is_v * NUM_4x4_IN_8x8, |
607 | 1.20M | ps_sub_pic_rc_variables->s_mb_variables |
608 | 1.20M | .apu1_nnzs[UV][0 + b_is_v * (1 + NUM_4x4_IN_8x8)], |
609 | 1.20M | NUM_4x4_IN_8x8, true); |
610 | | |
611 | 6.03M | for(j = 0; j < NUM_4x4_IN_8x8; j++) |
612 | 4.82M | { |
613 | 4.82M | UWORD8 u1_nnz = ps_sub_pic_rc_variables->s_mb_variables |
614 | 4.82M | .apu1_nnzs[UV][j + b_is_v * (1 + NUM_4x4_IN_8x8) + 1]; |
615 | | |
616 | 4.82M | if(u1_nnz) |
617 | 3.09M | { |
618 | 3.09M | u1_nnz -= |
619 | 3.09M | !!(((WORD16 *) (ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[UV] |
620 | 3.09M | .pv_data))[j + b_is_v * NUM_4x4_IN_8x8]); |
621 | | |
622 | 3.09M | ps_mb_bits_info->i8_texture_bits += isvce_sub_pic_rc_get_tu_residual_bits( |
623 | 3.09M | ps_sub_pic_rc_variables, |
624 | 3.09M | (j + b_is_v * NUM_4x4_IN_8x8 + 1) * |
625 | 3.09M | ps_sub_pic_rc_variables->s_mb_variables.as_quant_coeffs[UV] |
626 | 3.09M | .i4_data_stride + |
627 | 3.09M | 1, |
628 | 3.09M | u1_nnz, NUM_COEFFS_IN_MIN_TU - 1, true); |
629 | 3.09M | } |
630 | 4.82M | } |
631 | 1.20M | } |
632 | 605k | } |
633 | 1.11M | } |
634 | | |
635 | | void isvce_sub_pic_rc_ctxt_update(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
636 | 9.50M | { |
637 | 9.50M | sub_pic_rc_layer_state_t *ps_layer_state; |
638 | 9.50M | mb_bits_info_t *ps_mb_bits_info; |
639 | | |
640 | 9.50M | UWORD32 u4_mb_idx; |
641 | | |
642 | 9.50M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
643 | 9.50M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
644 | 9.50M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
645 | 9.50M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
646 | 9.50M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
647 | 9.50M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
648 | 9.50M | isvce_mb_info_t *ps_mb_info = ps_sub_pic_rc_variables->s_mb_variables.ps_mb_info; |
649 | | |
650 | 9.50M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
651 | 9.50M | bool b_is_skip_mb = (PSKIP == ps_mb_info->u2_mb_type) || (BSKIP == ps_mb_info->u2_mb_type); |
652 | | |
653 | 9.52M | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
654 | 2.94M | { |
655 | 2.94M | return; |
656 | 2.94M | } |
657 | | |
658 | 6.56M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
659 | 6.56M | u4_mb_idx = ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_abscissa + |
660 | 6.56M | ps_sub_pic_rc_variables->s_mb_variables.s_mb_pos.i4_ordinate * |
661 | 6.56M | (ps_layer_state->i4_wd / MB_SIZE); |
662 | 6.56M | ps_mb_bits_info = &ps_layer_state->ps_mb_bits_info[u4_mb_idx]; |
663 | | |
664 | 6.56M | memset(ps_mb_bits_info, 0, sizeof(ps_mb_bits_info[0])); |
665 | | |
666 | 6.56M | if(!b_is_skip_mb) |
667 | 6.36M | { |
668 | 6.36M | ihevce_svc_sub_pic_rc_set_header_bits(ps_sub_pic_rc_ctxt); |
669 | | |
670 | 6.36M | ihevce_svc_sub_pic_rc_set_texture_bits(ps_sub_pic_rc_ctxt); |
671 | 6.36M | } |
672 | | |
673 | 6.56M | ithread_mutex_lock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
674 | | |
675 | 6.56M | ps_layer_state->s_cumulative_mb_bits.i8_header_bits += ps_mb_bits_info->i8_header_bits; |
676 | 6.56M | ps_layer_state->s_cumulative_mb_bits.i8_texture_bits += ps_mb_bits_info->i8_texture_bits; |
677 | 6.56M | ps_layer_state->u4_num_mbs_sampled++; |
678 | | |
679 | 6.56M | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
680 | 6.56M | } |
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.52M | { |
684 | 9.52M | sub_pic_rc_layer_state_t *ps_layer_state; |
685 | | |
686 | 9.52M | DOUBLE d_bit_consumption_ratio; |
687 | 9.52M | UWORD32 u4_frame_qscale; |
688 | 9.52M | UWORD8 u1_mb_qp; |
689 | 9.52M | UWORD32 u4_num_mbs_sampled; |
690 | 9.52M | WORD32 i4_cumulative_mb_bits; |
691 | | |
692 | 9.52M | svc_sub_pic_rc_constants_t *ps_sub_pic_rc_constants = |
693 | 9.52M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_constants; |
694 | 9.52M | svc_sub_pic_rc_variables_t *ps_sub_pic_rc_variables = |
695 | 9.52M | &ps_sub_pic_rc_ctxt->s_sub_pic_rc_variables; |
696 | 9.52M | sub_pic_rc_state_t *ps_sub_pic_rc_state = |
697 | 9.52M | (sub_pic_rc_state_t *) ps_sub_pic_rc_constants->pv_state; |
698 | | |
699 | 9.52M | UWORD8 u1_spatial_layer_id = ps_sub_pic_rc_variables->s_layer_variables.u1_spatial_layer_id; |
700 | 9.52M | UWORD8 u1_frame_qp = ps_sub_pic_rc_variables->s_layer_variables.u1_frame_qp; |
701 | | |
702 | 9.53M | if(!ENABLE_IN_FRAME_RC || (IVE_RC_NONE == ps_sub_pic_rc_state->e_rc_mode)) |
703 | 2.93M | { |
704 | 2.93M | return u1_cur_mb_qp; |
705 | 2.93M | } |
706 | | |
707 | 6.58M | ps_layer_state = &ps_sub_pic_rc_state->as_sub_pic_rc_layer_states[u1_spatial_layer_id]; |
708 | | |
709 | 6.58M | ithread_mutex_lock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
710 | | |
711 | 6.58M | u4_num_mbs_sampled = ps_layer_state->u4_num_mbs_sampled; |
712 | | |
713 | 6.58M | if(u4_num_mbs_sampled < |
714 | 6.58M | ((UWORD32) ceil(MIN_SAMPLED_MB_RATIO * ((DOUBLE) ps_layer_state->i4_num_mbs)))) |
715 | 315k | { |
716 | 315k | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
717 | | |
718 | 315k | return u1_cur_mb_qp; |
719 | 315k | } |
720 | | |
721 | 6.26M | i4_cumulative_mb_bits = (WORD32) (ps_layer_state->s_cumulative_mb_bits.i8_header_bits + |
722 | 6.26M | ps_layer_state->s_cumulative_mb_bits.i8_texture_bits); |
723 | | |
724 | 6.26M | if((0 == ps_layer_state->u4_allocated_bits) || (0 == u4_num_mbs_sampled)) |
725 | 447k | { |
726 | 447k | d_bit_consumption_ratio = nextafter(BIT_RATIO_FOR_OVERCONSUMPTION, INFINITY); |
727 | 447k | } |
728 | 5.82M | else |
729 | 5.82M | { |
730 | 5.82M | d_bit_consumption_ratio = |
731 | 5.82M | (((DOUBLE) i4_cumulative_mb_bits) * ((DOUBLE) ps_layer_state->i4_num_mbs)) / |
732 | 5.82M | (((DOUBLE) ps_layer_state->u4_allocated_bits) * ((DOUBLE) u4_num_mbs_sampled)); |
733 | 5.82M | } |
734 | | |
735 | 6.26M | ithread_mutex_unlock(ps_sub_pic_rc_state->pv_bits_accumulator_mutex); |
736 | | |
737 | 6.26M | if((d_bit_consumption_ratio > BIT_RATIO_FOR_OVERCONSUMPTION) || |
738 | 3.56M | (d_bit_consumption_ratio < BIT_RATIO_FOR_UNDERCONSUMPTION)) |
739 | 5.62M | { |
740 | 5.62M | u4_frame_qscale = ps_layer_state->s_qp_params.pu4_qp_to_qscale_map[u1_frame_qp] * |
741 | 5.62M | d_bit_consumption_ratio + |
742 | 5.62M | 0.5; |
743 | 5.62M | u4_frame_qscale = CLIP3(ps_layer_state->s_qp_params.pu4_qp_to_qscale_map[0], MAX_SVC_QSCALE, |
744 | 5.62M | u4_frame_qscale); |
745 | 5.62M | u1_mb_qp = ps_layer_state->s_qp_params.pu1_qscale_to_qp_map[u4_frame_qscale]; |
746 | 5.62M | u1_mb_qp = CLIP3(ps_layer_state->s_qp_params.u1_min_qp, |
747 | 5.62M | ps_layer_state->s_qp_params.u1_max_qp, u1_mb_qp); |
748 | 5.62M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_cur_mb_qp) - MAX_MB_QP_DECREMENT), |
749 | 5.62M | MIN(MAX_H264_QP, ((WORD16) u1_cur_mb_qp) + MAX_MB_QP_INCREMENT), |
750 | 5.62M | ((WORD16) u1_mb_qp)); |
751 | | /* This ensures mb_qp_delta stays within the interval [-26, 25] */ |
752 | 5.62M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_frame_qp) - MAX_FRAME_QP_DECREMENT), |
753 | 5.62M | MIN(MAX_H264_QP, ((WORD16) u1_frame_qp) + MAX_FRAME_QP_INCREMENT), |
754 | 5.62M | ((WORD16) u1_mb_qp)); |
755 | 5.62M | } |
756 | 649k | else |
757 | 649k | { |
758 | 649k | u1_mb_qp = u1_cur_mb_qp; |
759 | 649k | } |
760 | | |
761 | 6.26M | { |
762 | 6.26M | vbv_buf_status_e e_vbv_buf_status; |
763 | 6.26M | picture_type_e e_rc_pic_type; |
764 | | |
765 | 6.26M | DOUBLE d_est_frame_bits; |
766 | | |
767 | 6.26M | WORD32 i4_num_bits_to_prevent_vbv_underflow; |
768 | | |
769 | 6.26M | d_est_frame_bits = ((DOUBLE) i4_cumulative_mb_bits) * ((DOUBLE) ps_layer_state->i4_num_mbs); |
770 | 6.26M | d_est_frame_bits /= u4_num_mbs_sampled; |
771 | | |
772 | 6.26M | switch(ps_sub_pic_rc_variables->s_layer_variables.i4_slice_type) |
773 | 6.26M | { |
774 | 5.52M | case ISLICE: |
775 | 5.52M | { |
776 | 5.52M | e_rc_pic_type = I_PIC; |
777 | 5.52M | break; |
778 | 0 | } |
779 | 772k | case PSLICE: |
780 | 772k | { |
781 | 772k | e_rc_pic_type = P_PIC; |
782 | 772k | break; |
783 | 0 | } |
784 | 0 | default: |
785 | 0 | { |
786 | 0 | e_rc_pic_type = B_PIC; |
787 | 0 | break; |
788 | 0 | } |
789 | 6.26M | } |
790 | | |
791 | 6.29M | e_vbv_buf_status = |
792 | 6.29M | irc_get_buffer_status(ps_layer_state->pv_layer_rc_ctxt, (WORD32) d_est_frame_bits, |
793 | 6.29M | e_rc_pic_type, &i4_num_bits_to_prevent_vbv_underflow); |
794 | | |
795 | | /* This models dec VBV buffer */ |
796 | 6.29M | if(VBV_OVERFLOW == e_vbv_buf_status) |
797 | 2.99M | { |
798 | 2.99M | u1_mb_qp--; |
799 | 2.99M | } |
800 | 3.30M | else if(VBV_UNDERFLOW == e_vbv_buf_status) |
801 | 766k | { |
802 | 766k | u1_mb_qp++; |
803 | 766k | } |
804 | | |
805 | | /* This ensures mb_qp_delta stays within the interval [-26, 25] */ |
806 | 6.29M | u1_mb_qp = CLIP3(ps_layer_state->s_qp_params.u1_min_qp, |
807 | 6.29M | ps_layer_state->s_qp_params.u1_max_qp, u1_mb_qp); |
808 | 6.29M | u1_mb_qp = CLIP3(MAX(MIN_H264_QP, ((WORD16) u1_frame_qp) - MAX_FRAME_QP_DECREMENT), |
809 | 6.29M | MIN(MAX_H264_QP, ((WORD16) u1_frame_qp) + MAX_FRAME_QP_INCREMENT), |
810 | 6.29M | ((WORD16) u1_mb_qp)); |
811 | 6.29M | } |
812 | | |
813 | 0 | return u1_mb_qp; |
814 | 6.26M | } |
815 | | |
816 | | void isvce_sub_pic_rc_get_entropy_data(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
817 | 9.57M | { |
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.57M | UNUSED(ps_sub_pic_rc_ctxt); |
844 | 9.57M | #endif |
845 | 9.57M | } |
846 | | |
847 | | void isvce_sub_pic_rc_dump_data(svc_sub_pic_rc_ctxt_t *ps_sub_pic_rc_ctxt) |
848 | 28.0k | { |
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 | 28.0k | UNUSED(ps_sub_pic_rc_ctxt); |
884 | 28.0k | #endif |
885 | 28.0k | } |
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 | } |