/src/libhevc/encoder/ihevce_rc_interface.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2018 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 ihevce_rc_interface.c |
24 | | * |
25 | | * @brief |
26 | | * This file contains function definitions for rc api interface |
27 | | * |
28 | | * @author |
29 | | * Ittiam |
30 | | * |
31 | | * List of Functions |
32 | | * <TODO: Update this> |
33 | | * |
34 | | ****************************************************************************** |
35 | | */ |
36 | | |
37 | | /*****************************************************************************/ |
38 | | /* File Includes */ |
39 | | /*****************************************************************************/ |
40 | | |
41 | | /* System include files */ |
42 | | #include <stdio.h> |
43 | | #include <string.h> |
44 | | #include <stdlib.h> |
45 | | #include <assert.h> |
46 | | #include <stdarg.h> |
47 | | #include <math.h> |
48 | | |
49 | | /* User include files */ |
50 | | #include "ihevc_typedefs.h" |
51 | | #include "itt_video_api.h" |
52 | | #include "ihevce_api.h" |
53 | | |
54 | | #include "rc_cntrl_param.h" |
55 | | #include "rc_frame_info_collector.h" |
56 | | #include "rc_look_ahead_params.h" |
57 | | #include "mem_req_and_acq.h" |
58 | | #include "rate_control_api.h" |
59 | | #include "var_q_operator.h" |
60 | | |
61 | | #include "ihevc_defs.h" |
62 | | #include "ihevc_debug.h" |
63 | | #include "ihevc_macros.h" |
64 | | #include "ihevc_structs.h" |
65 | | #include "ihevc_platform_macros.h" |
66 | | #include "ihevc_deblk.h" |
67 | | #include "ihevc_itrans_recon.h" |
68 | | #include "ihevc_chroma_itrans_recon.h" |
69 | | #include "ihevc_chroma_intra_pred.h" |
70 | | #include "ihevc_intra_pred.h" |
71 | | #include "ihevc_inter_pred.h" |
72 | | #include "ihevc_mem_fns.h" |
73 | | #include "ihevc_padding.h" |
74 | | #include "ihevc_weighted_pred.h" |
75 | | #include "ihevc_sao.h" |
76 | | #include "ihevc_resi_trans.h" |
77 | | #include "ihevc_quant_iquant_ssd.h" |
78 | | |
79 | | #include "ihevce_defs.h" |
80 | | #include "ihevce_hle_interface.h" |
81 | | #include "ihevce_lap_enc_structs.h" |
82 | | #include "ihevce_lap_interface.h" |
83 | | #include "ihevce_multi_thrd_structs.h" |
84 | | #include "ihevce_me_common_defs.h" |
85 | | #include "ihevce_had_satd.h" |
86 | | #include "ihevce_function_selector.h" |
87 | | #include "ihevce_enc_structs.h" |
88 | | #include "ihevce_cmn_utils_instr_set_router.h" |
89 | | #include "hme_datatype.h" |
90 | | #include "hme_interface.h" |
91 | | #include "hme_common_defs.h" |
92 | | #include "hme_defs.h" |
93 | | #include "ihevce_rc_enc_structs.h" |
94 | | #include "ihevce_rc_structs.h" |
95 | | #include "ihevce_rc_interface.h" |
96 | | #include "ihevce_frame_process_utils.h" |
97 | | |
98 | | /*****************************************************************************/ |
99 | | /* Constant Macros */ |
100 | | /*****************************************************************************/ |
101 | | #define USE_USER_FIRST_FRAME_QP 0 |
102 | | #define DEBUG_PRINT 0 |
103 | | #define DETERMINISTIC_RC 1 |
104 | | #define USE_QP_OFFSET_POST_SCD 1 |
105 | | #define USE_SQRT 0 |
106 | | #define K_SCALING_FACTOR 8 |
107 | | #define ENABLE_2_PASS_BIT_ALLOC_FRM_1ST 0 |
108 | | |
109 | 114k | #define VBV_THRSH_I_PIC_DELTA_QP_1 (0.85) |
110 | 114k | #define VBV_THRSH_I_PIC_DELTA_QP_2 (0.75) |
111 | 114k | #define VBV_THRSH_P_PIC_DELTA_QP_1 (0.80) |
112 | 114k | #define VBV_THRSH_P_PIC_DELTA_QP_2 (0.70) |
113 | 114k | #define VBV_THRSH_BR_PIC_DELTA_QP_1 (0.75) |
114 | 114k | #define VBV_THRSH_BR_PIC_DELTA_QP_2 (0.65) |
115 | 114k | #define VBV_THRSH_BNR_PIC_DELTA_QP_1 (0.75) |
116 | 114k | #define VBV_THRSH_BNR_PIC_DELTA_QP_2 (0.65) |
117 | 114k | #define VBV_THRSH_DELTA_QP (0.6) |
118 | | |
119 | 0 | #define VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_1 (0.70) |
120 | 0 | #define VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_2 (0.60) |
121 | 0 | #define VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_1 (0.65) |
122 | 0 | #define VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_2 (0.55) |
123 | 0 | #define VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_1 (0.60) |
124 | 0 | #define VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_2 (0.50) |
125 | 0 | #define VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_1 (0.60) |
126 | 0 | #define VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_2 (0.50) |
127 | 0 | #define VBV_THRSH_FRM_PRLL_DELTA_QP (0.45) |
128 | | |
129 | | #define TRACE_SUPPORT 0 |
130 | | |
131 | | /*****************************************************************************/ |
132 | | /* Globals */ |
133 | | /*****************************************************************************/ |
134 | | |
135 | | /* |
136 | | Modified bpp vs nor satd/act/qp : |
137 | | ================================= |
138 | | |
139 | | Prestine Quality |
140 | | ----------------- |
141 | | 480p y = -0.1331x3 - 0.0589x2 + 2.5091x - 0.0626 |
142 | | 720p y = -0.3603x3 + 0.4504x2 + 2.2056x - 0.0411 |
143 | | 1080p y = -0.7085x3 + 0.9743x2 + 1.939x - 0.0238 |
144 | | 2160p y = -1.2447x3 + 2.1218x2 + 1.4995x - 0.0108 |
145 | | |
146 | | High Quality |
147 | | ------------- |
148 | | 480p y = -0.1348x3 - 0.0557x2 + 2.5055x - 0.0655 |
149 | | 720p y = -0.0811x3 + 0.1988x2 + 1.246x - 0.0385 |
150 | | 1080p y = -0.74x3 + 1.0552x2 + 1.8942x - 0.0251 |
151 | | 2160p y = -1.3851x3 + 2.3372x2 + 1.4255x - 0.0113 |
152 | | |
153 | | Medium Speed |
154 | | ------------- |
155 | | 480p y = -0.143x3 - 0.0452x2 + 2.5581x - 0.0765 |
156 | | 720p y = -0.3997x3 + 0.542x2 + 2.201x - 0.0507 |
157 | | 1080p y = -0.816x3 + 1.2048x2 + 1.8689x - 0.0298 |
158 | | 2160p y = -1.5169x3 + 2.5857x2 + 1.3478x - 0.0126 |
159 | | |
160 | | High Speed |
161 | | ----------- |
162 | | 480p y = -0.1472x3 - 0.0341x2 + 2.5605x - 0.0755 |
163 | | 720p y = -0.3967x3 + 0.526x2 + 2.2228x - 0.0504 |
164 | | 1080p y = -0.8008x3 + 1.1713x2 + 1.8897x - 0.0297 |
165 | | 2160p y = -1.503x3 + 2.576x2 + 1.3476x - 0.0123 |
166 | | |
167 | | Extreme Speed |
168 | | -------------- |
169 | | 480p y = -0.1379x3 - 0.059x2 + 2.5716x - 0.0756 |
170 | | 720p y = -0.3938x3 + 0.521x2 + 2.2239x - 0.0505 |
171 | | 1080p y = -0.8041x3 + 1.1725x2 + 1.8874x - 0.0293 |
172 | | 2160p y = -1.4863x3 + 2.556x2 + 1.344x - 0.0122 |
173 | | |
174 | | */ |
175 | | |
176 | | const double g_offline_i_model_coeff[20][4] = { |
177 | | |
178 | | /*ultra_HD*/ |
179 | | { -1.2447, 2.1218, 1.4995, -0.0108 }, /*Prestine quality*/ |
180 | | { -1.3851, 2.3372, 1.4255, -0.0113 }, /*High quality*/ |
181 | | { -1.5169, 2.5857, 1.3478, -0.0126 }, /*Medium speed*/ |
182 | | { -1.503, 2.576, 1.3476, -0.0123 }, /*high speed*/ |
183 | | { -1.4863, 2.556, 1.344, -0.0122 }, /*Extreme Speed*/ |
184 | | |
185 | | /*Full HD*/ |
186 | | { -0.7085, 0.9743, 1.939, -0.0238 }, /*Prestine quality*/ |
187 | | { -0.74, 1.0552, 1.8942, -0.0251 }, /*High quality*/ |
188 | | { -0.816, 1.2048, 1.8689, -0.0298 }, /*Medium speed*/ |
189 | | { -0.8008, 1.1713, 1.8897, -0.0297 }, /*high speed*/ |
190 | | { -0.8041, 1.1725, 1.8874, -0.0293 }, /*Extreme Speed*/ |
191 | | |
192 | | /*720p*/ |
193 | | { -0.3603, 0.4504, 2.2056, -0.0411 }, /*Prestine quality*/ |
194 | | // {-0.0811, 0.1988, 1.246, - 0.0385},/*High quality*/ |
195 | | { -0.3997, 0.542, 2.201, -0.0507 }, |
196 | | { -0.3997, 0.542, 2.201, -0.0507 }, /*Medium speed*/ |
197 | | { -0.3967, 0.526, 2.2228, -0.0504 }, /*high speed*/ |
198 | | { -0.3938, 0.521, 2.2239, -0.0505 }, /*Extreme Speed*/ |
199 | | |
200 | | /*SD*/ |
201 | | { -0.1331, -0.0589, 2.5091, -0.0626 }, /*Prestine quality*/ |
202 | | { -0.1348, -0.0557, 2.5055, -0.0655 }, /*High quality*/ |
203 | | { -0.143, -0.0452, 2.5581, -0.0765 }, /*Medium speed*/ |
204 | | { -0.1472, -0.0341, 2.5605, -0.0755 }, /*high speed*/ |
205 | | { -0.1379, -0.059, 2.5716, -0.0756 } /*Extreme Speed*/ |
206 | | |
207 | | }; |
208 | | |
209 | | /*****************************************************************************/ |
210 | | /* Function Declarations */ |
211 | | /*****************************************************************************/ |
212 | | |
213 | | picture_type_e ihevce_rc_conv_pic_type( |
214 | | IV_PICTURE_CODING_TYPE_T pic_type, |
215 | | WORD32 i4_field_pic, |
216 | | WORD32 i4_temporal_layer_id, |
217 | | WORD32 i4_is_bottom_field, |
218 | | WORD32 i4_top_field_first); |
219 | | |
220 | | WORD32 ihevce_rc_get_scaled_mpeg2_qp(WORD32 i4_frame_qp, rc_quant_t *ps_rc_quant_ctxt); |
221 | | |
222 | | static WORD32 ihevce_get_offline_index(rc_context_t *ps_rc_ctxt, WORD32 i4_num_pels_in_frame); |
223 | | |
224 | | static void ihevce_rc_get_pic_param( |
225 | | picture_type_e rc_pic_type, WORD32 *pi4_tem_lyr, WORD32 *pi4_is_bottom_field); |
226 | | |
227 | | static double ihevce_get_frame_lambda_modifier( |
228 | | WORD8 slice_type, |
229 | | WORD32 i4_rc_temporal_lyr_id, |
230 | | WORD32 i4_first_field, |
231 | | WORD32 i4_rc_is_ref_pic, |
232 | | WORD32 i4_num_b_frms); |
233 | | |
234 | | static WORD32 ihevce_clip_min_max_qp( |
235 | | rc_context_t *ps_rc_ctxt, |
236 | | WORD32 i4_hevc_frame_qp, |
237 | | picture_type_e rc_pic_type, |
238 | | WORD32 i4_rc_temporal_lyr_id); |
239 | | |
240 | | WORD32 ihevce_ebf_based_rc_correction_to_avoid_overflow( |
241 | | rc_context_t *ps_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 *pi4_tot_bits_estimated); |
242 | | |
243 | | /*****************************************************************************/ |
244 | | /* Function Definitions */ |
245 | | /*****************************************************************************/ |
246 | | |
247 | | /*! |
248 | | ************************************************************************ |
249 | | * @brief |
250 | | * return number of records used by RC |
251 | | ************************************************************************ |
252 | | */ |
253 | | WORD32 ihevce_rc_get_num_mem_recs(void) |
254 | 17.7k | { |
255 | 17.7k | WORD32 i4_num_rc_mem_tab = 0; |
256 | | |
257 | | /*get the number of memtab request from RC*/ |
258 | 17.7k | rate_control_handle ps_rate_control_api; |
259 | 17.7k | itt_memtab_t *ps_memtab = NULL; |
260 | 17.7k | i4_num_rc_mem_tab = |
261 | 17.7k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_memtab, GET_NUM_MEMTAB); |
262 | | |
263 | 17.7k | return ((NUM_RC_MEM_RECS + i4_num_rc_mem_tab)); |
264 | 17.7k | } |
265 | | |
266 | | /*! |
267 | | ************************************************************************ |
268 | | * @brief |
269 | | * return each record attributes of RC |
270 | | ************************************************************************ |
271 | | */ |
272 | | WORD32 ihevce_rc_get_mem_recs( |
273 | | iv_mem_rec_t *ps_mem_tab, |
274 | | ihevce_static_cfg_params_t *ps_init_prms, |
275 | | WORD32 mem_space, |
276 | | ihevce_sys_api_t *ps_sys_api) |
277 | 8.85k | { |
278 | 8.85k | float f_temp; |
279 | 8.85k | WORD32 i4_temp_size; |
280 | 8.85k | WORD32 i4_num_memtab = 0; |
281 | 8.85k | WORD32 i4_num_rc_mem_tab, i; |
282 | 8.85k | rate_control_handle ps_rate_control_api; |
283 | 8.85k | itt_memtab_t *ps_itt_memtab = NULL; |
284 | 8.85k | itt_memtab_t as_rc_mem_tab[30]; |
285 | | |
286 | | /*memory requirements to store RC context */ |
287 | 8.85k | ps_mem_tab[RC_CTXT].i4_mem_size = sizeof(rc_context_t); |
288 | | //DBG_PRINTF("size of RC context = %d\n",sizeof(rc_context_t)); |
289 | 8.85k | ps_mem_tab[RC_CTXT].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
290 | | |
291 | 8.85k | ps_mem_tab[RC_CTXT].i4_mem_alignment = 64; |
292 | | |
293 | 8.85k | (void)ps_sys_api; |
294 | | //i4_temp_size = (51 + ((ps_init_prms->s_src_prms.i4_bit_depth - 8) * 6)); |
295 | 8.85k | i4_temp_size = (51 + ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6)); |
296 | | |
297 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE].i4_mem_size = (i4_temp_size + 1) * 4; |
298 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
299 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE].i4_mem_alignment = 64; |
300 | | |
301 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].i4_mem_size = (i4_temp_size + 1) * 4; |
302 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
303 | 8.85k | ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].i4_mem_alignment = 64; |
304 | | |
305 | 8.85k | f_temp = (float)(51 + ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6)); |
306 | 8.85k | f_temp = ((float)(f_temp - 4) / 6); |
307 | 8.85k | i4_temp_size = (WORD32)((float)pow(2, f_temp) + 0.5); |
308 | 8.85k | i4_temp_size = (i4_temp_size << 3); // Q3 format is mantained for accuarate calc at lower qp |
309 | | |
310 | 8.85k | ps_mem_tab[RC_QSCALE_TO_QP].i4_mem_size = (i4_temp_size + 1) * sizeof(UWORD32); |
311 | 8.85k | ps_mem_tab[RC_QSCALE_TO_QP].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
312 | 8.85k | ps_mem_tab[RC_QSCALE_TO_QP].i4_mem_alignment = 64; |
313 | | |
314 | | /*memory requirements to store RC context */ |
315 | 8.85k | ps_mem_tab[RC_MULTI_PASS_GOP_STAT].i4_mem_size = sizeof(gop_level_stat_t); |
316 | 8.85k | ps_mem_tab[RC_MULTI_PASS_GOP_STAT].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
317 | 8.85k | ps_mem_tab[RC_MULTI_PASS_GOP_STAT].i4_mem_alignment = 64; |
318 | | |
319 | 8.85k | i4_num_rc_mem_tab = |
320 | 8.85k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_itt_memtab, GET_NUM_MEMTAB); |
321 | | |
322 | 8.85k | i4_num_memtab = |
323 | 8.85k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, FILL_MEMTAB); |
324 | | |
325 | 203k | for(i = 0; i < i4_num_memtab; i++) |
326 | 194k | { |
327 | 194k | ps_mem_tab[i + NUM_RC_MEM_RECS].i4_mem_size = as_rc_mem_tab[i].u4_size; |
328 | 194k | ps_mem_tab[i + NUM_RC_MEM_RECS].i4_mem_alignment = as_rc_mem_tab[i].i4_alignment; |
329 | 194k | ps_mem_tab[i + NUM_RC_MEM_RECS].e_mem_type = (IV_MEM_TYPE_T)mem_space; |
330 | 194k | } |
331 | 8.85k | return (i4_num_memtab + NUM_RC_MEM_RECS); |
332 | 8.85k | } |
333 | | |
334 | | /** |
335 | | ****************************************************************************** |
336 | | * |
337 | | * @brief Initilizes the rate control module |
338 | | * |
339 | | * @par Description |
340 | | * |
341 | | * @param[inout] ps_mem_tab |
342 | | * pointer to memory descriptors table |
343 | | * |
344 | | * @param[in] ps_init_prms |
345 | | * Create time static parameters |
346 | | * |
347 | | * @return void |
348 | | * |
349 | | ****************************************************************************** |
350 | | */ |
351 | | void *ihevce_rc_mem_init( |
352 | | iv_mem_rec_t *ps_mem_tab, |
353 | | ihevce_static_cfg_params_t *ps_init_prms, |
354 | | WORD32 i4_bitrate_instance_id, |
355 | | rc_quant_t *ps_rc_quant, |
356 | | WORD32 i4_resolution_id, |
357 | | WORD32 i4_look_ahead_frames_in_first_pass) |
358 | 8.85k | { |
359 | 8.85k | rc_context_t *ps_rc_ctxt; |
360 | 8.85k | WORD32 i4_num_memtab, i, j, i4_avg_bitrate, u4_buf_size; |
361 | 8.85k | WORD32 i4_cdr_period = 0, i4_idr_period = 0; |
362 | 8.85k | WORD32 i4_peak_bitrate_factor; |
363 | 8.85k | rate_control_handle ps_rate_control_api; |
364 | 8.85k | itt_memtab_t as_rc_mem_tab[30]; |
365 | 8.85k | itt_memtab_t *ps_itt_memtab = NULL; |
366 | 8.85k | ps_rc_ctxt = (rc_context_t *)ps_mem_tab[RC_CTXT].pv_base; |
367 | 8.85k | memset(ps_rc_ctxt, 0, sizeof(rc_context_t)); |
368 | | |
369 | 8.85k | ps_rc_ctxt->i4_br_id_for_2pass = i4_bitrate_instance_id; |
370 | 8.85k | if(ps_init_prms->s_coding_tools_prms.i4_max_cra_open_gop_period) |
371 | 5.11k | { |
372 | 5.11k | i4_cdr_period = ps_init_prms->s_coding_tools_prms.i4_max_cra_open_gop_period; |
373 | 5.11k | } |
374 | 8.85k | if(ps_init_prms->s_coding_tools_prms.i4_max_i_open_gop_period) |
375 | 5.29k | { |
376 | 5.29k | i4_cdr_period = ps_init_prms->s_coding_tools_prms.i4_max_i_open_gop_period; |
377 | 5.29k | } |
378 | 8.85k | i4_idr_period = ps_init_prms->s_coding_tools_prms.i4_max_closed_gop_period; |
379 | | |
380 | 8.85k | ps_rc_quant->pi4_qscale_to_qp = (WORD32 *)ps_mem_tab[RC_QSCALE_TO_QP].pv_base; |
381 | | |
382 | 8.85k | ps_rc_quant->pi4_qp_to_qscale_q_factor = (WORD32 *)ps_mem_tab[RC_QP_TO_QSCALE_Q_FACTOR].pv_base; |
383 | | |
384 | 8.85k | ps_rc_quant->pi4_qp_to_qscale = (WORD32 *)ps_mem_tab[RC_QP_TO_QSCALE].pv_base; |
385 | | |
386 | 8.85k | ps_rc_ctxt->pv_gop_stat = (void *)ps_mem_tab[RC_MULTI_PASS_GOP_STAT].pv_base; |
387 | | |
388 | | /*assign memtabs to rc module*/ |
389 | 8.85k | i4_num_memtab = |
390 | 8.85k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, ps_itt_memtab, GET_NUM_MEMTAB); |
391 | | |
392 | 8.85k | i4_num_memtab = |
393 | 8.85k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, FILL_MEMTAB); |
394 | 203k | for(i = 0; i < i4_num_memtab; i++) |
395 | 194k | { |
396 | 194k | as_rc_mem_tab[i].pv_base = ps_mem_tab[i + NUM_RC_MEM_RECS].pv_base; |
397 | 194k | } |
398 | 8.85k | i4_num_memtab = |
399 | 8.85k | rate_control_num_fill_use_free_memtab(&ps_rate_control_api, as_rc_mem_tab, USE_BASE); |
400 | | |
401 | 8.85k | ps_rc_ctxt->rc_hdl = |
402 | 8.85k | ps_rate_control_api; /*handle to entire RC structure private to RC library*/ |
403 | 8.85k | ps_rc_ctxt->i4_field_pic = ps_init_prms->s_src_prms.i4_field_pic; |
404 | | |
405 | 8.85k | ps_rc_ctxt->i4_is_first_frame_encoded = 0; |
406 | | /*added for field encoding*/ |
407 | 8.85k | ps_rc_ctxt->i4_max_inter_frm_int = |
408 | 8.85k | 1 << (ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers + ps_rc_ctxt->i4_field_pic); |
409 | 8.85k | ps_rc_ctxt->i4_max_temporal_lyr = ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers; |
410 | | /*Number of picture types used if different models are used for hierarchial B frames*/ |
411 | | |
412 | 8.85k | if(i4_idr_period == 1 || i4_cdr_period == 1) |
413 | 512 | ps_rc_ctxt->i4_num_active_pic_type = 1; |
414 | 8.34k | else |
415 | 8.34k | ps_rc_ctxt->i4_num_active_pic_type = |
416 | 8.34k | 2 + ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers; |
417 | | |
418 | 8.85k | ps_rc_ctxt->i4_quality_preset = |
419 | 8.85k | ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset; |
420 | | |
421 | 8.85k | if(ps_rc_ctxt->i4_quality_preset == IHEVCE_QUALITY_P7) |
422 | 315 | { |
423 | 315 | ps_rc_ctxt->i4_quality_preset = IHEVCE_QUALITY_P6; |
424 | 315 | } |
425 | | |
426 | 8.85k | ps_rc_ctxt->i4_rc_pass = ps_init_prms->s_pass_prms.i4_pass; |
427 | 8.85k | ps_rc_ctxt->i8_num_gop_mem_alloc = 0; |
428 | | |
429 | 8.85k | ps_rc_ctxt->u1_is_mb_level_rc_on = 0; /*no mb level RC*/ |
430 | | |
431 | 8.85k | ps_rc_ctxt->i4_is_infinite_gop = 0; |
432 | 8.85k | ps_rc_ctxt->u1_bit_depth = (UWORD8)ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth; |
433 | | |
434 | | //ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset = ((ps_init_prms->s_src_prms.i4_bit_depth-8)*6); |
435 | 8.85k | ps_rc_quant->i1_qp_offset = ((ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8) * 6); |
436 | | |
437 | 8.85k | ps_rc_quant->i2_max_qp = MIN(ps_init_prms->s_config_prms.i4_max_frame_qp, |
438 | 8.85k | 51); // FOR Encoder |
439 | 8.85k | ps_rc_quant->i2_min_qp = |
440 | 8.85k | MAX(-(ps_rc_quant->i1_qp_offset), ps_init_prms->s_config_prms.i4_min_frame_qp); |
441 | | |
442 | 8.85k | if(ps_init_prms->s_lap_prms.i4_rc_look_ahead_pics) |
443 | 0 | { |
444 | 0 | ps_rc_ctxt->i4_num_frame_in_lap_window = |
445 | 0 | ps_init_prms->s_lap_prms.i4_rc_look_ahead_pics + MIN_L1_L0_STAGGER_NON_SEQ; |
446 | 0 | } |
447 | 8.85k | else |
448 | 8.85k | ps_rc_ctxt->i4_num_frame_in_lap_window = 0; |
449 | | |
450 | 8.85k | if(i4_cdr_period > 0 && i4_idr_period > 0) |
451 | 5.23k | { |
452 | | /*both IDR and CDR are positive*/ |
453 | | //WORD32 i4_rem; |
454 | 5.23k | ps_rc_ctxt->u4_intra_frame_interval = i4_cdr_period; |
455 | 5.23k | ps_rc_ctxt->u4_idr_period = i4_idr_period; |
456 | | |
457 | | /*Allow configuration where IDR period is multiple of CDR period. Though any configuiration is supported by LAP rate control |
458 | | does not handle assymeteric GOPS, Bit-allocation is exposed to CDR or IDR. It treats everything as I pic*/ |
459 | 5.23k | } |
460 | 3.62k | else if(!i4_idr_period && i4_cdr_period > 0) |
461 | 1.06k | { |
462 | 1.06k | ps_rc_ctxt->u4_intra_frame_interval = i4_cdr_period; |
463 | 1.06k | ps_rc_ctxt->u4_idr_period = 0; |
464 | 1.06k | } |
465 | 2.55k | else if(!i4_cdr_period && i4_idr_period > 0) |
466 | 1.20k | { |
467 | 1.20k | ps_rc_ctxt->u4_intra_frame_interval = i4_idr_period; |
468 | 1.20k | ps_rc_ctxt->u4_idr_period = i4_idr_period; |
469 | 1.20k | } |
470 | 1.34k | else |
471 | 1.34k | { |
472 | | /*ASSERT(0);*/ |
473 | | |
474 | 1.34k | ps_rc_ctxt->u4_intra_frame_interval = |
475 | 1.34k | INFINITE_GOP_CDR_TIME_S * |
476 | 1.34k | ((ps_init_prms->s_src_prms.i4_frm_rate_num / |
477 | 1.34k | (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_frm_rate_scale_factor * |
478 | 1.34k | ps_init_prms->s_src_prms.i4_frm_rate_denom))); |
479 | 1.34k | ps_rc_ctxt->u4_idr_period = 0; |
480 | 1.34k | ps_rc_ctxt->i4_is_infinite_gop = 1; |
481 | 1.34k | } |
482 | | |
483 | | /*If cdr period is 0 then only it is closed gop*/ |
484 | 8.85k | ps_rc_ctxt->i4_is_gop_closed = 0; |
485 | 8.85k | if(i4_cdr_period == 0) |
486 | 2.55k | { |
487 | 2.55k | ps_rc_ctxt->i4_is_gop_closed = 1; |
488 | 2.55k | } |
489 | | /*This is required because the intra sad returned by non I pic is not correct. Use only I pic sad for next I pic qp calculation*/ |
490 | 8.85k | ps_rc_ctxt->i4_use_est_intra_sad = 0; |
491 | 8.85k | ps_rc_ctxt->u4_src_ticks = 1000; |
492 | 8.85k | ps_rc_ctxt->u4_tgt_ticks = 1000; |
493 | 8.85k | ps_rc_ctxt->i4_auto_generate_init_qp = 1; |
494 | | |
495 | 8.85k | ps_rc_ctxt->i8_prev_i_frm_cost = 0; |
496 | | |
497 | 88.5k | for(i = 0; i < MAX_PIC_TYPE; i++) |
498 | 79.7k | { |
499 | | /* -1 cost indicates the picture type not been encoded*/ |
500 | 79.7k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1; |
501 | 79.7k | ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] = -1; |
502 | 79.7k | ps_rc_ctxt->ai8_prev_frame_hme_sad[i] = -1; |
503 | 79.7k | ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i] = -1; |
504 | | /*L1 state metrics*/ |
505 | 79.7k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[i] = -1; |
506 | 79.7k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[i] = -1; |
507 | 79.7k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[i] = -1; |
508 | | /* SGI & Enc Loop Parallelism related changes*/ |
509 | 79.7k | ps_rc_ctxt->s_l1_state_metric.au4_prev_scene_num[i] = 0; |
510 | 79.7k | ps_rc_ctxt->au4_prev_scene_num_pre_enc[i] = 0xFFFFFFFF; |
511 | 79.7k | ps_rc_ctxt->ai4_qp_for_previous_scene_pre_enc[i] = 0; |
512 | 79.7k | } |
513 | 8.85k | ps_rc_ctxt->u4_scene_num_est_L0_intra_sad_available = 0xFFFFFFFF; |
514 | | |
515 | 150k | for(i = 0; i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i++) |
516 | 141k | { |
517 | 141k | ps_rc_ctxt->as_non_ref_b_qp[i].i4_enc_order_num_rc = 0x7FFFFFFF; |
518 | 141k | ps_rc_ctxt->as_non_ref_b_qp[i].i4_non_ref_B_pic_qp = 0x7FFFFFFF; |
519 | 141k | ps_rc_ctxt->as_non_ref_b_qp[i].u4_scene_num_rc = MAX_SCENE_NUM + 1; |
520 | 141k | } |
521 | 8.85k | ps_rc_ctxt->i4_non_ref_B_ctr = 0; |
522 | 8.85k | ps_rc_ctxt->i4_prev_qp_ctr = 0; |
523 | 8.85k | ps_rc_ctxt->i4_cur_scene_num = 0; |
524 | | |
525 | | /*init = 0 set to 1 when atleast one frame of each picture type has completed L1 stage*/ |
526 | 8.85k | ps_rc_ctxt->i4_is_est_L0_intra_sad_available = 0; |
527 | | |
528 | | /*Min and max qp from user*/ |
529 | 8.85k | ps_rc_ctxt->i4_min_frame_qp = ps_init_prms->s_config_prms.i4_min_frame_qp; |
530 | 8.85k | ps_rc_ctxt->i4_max_frame_qp = ps_init_prms->s_config_prms.i4_max_frame_qp; |
531 | 8.85k | ASSERT(ps_rc_ctxt->i4_min_frame_qp >= ps_rc_quant->i2_min_qp); |
532 | 8.85k | ASSERT(ps_rc_ctxt->i4_max_frame_qp <= ps_rc_quant->i2_max_qp); |
533 | | /*bitrate init*/ |
534 | | /*take average bitrate from comfig file*/ |
535 | 8.85k | i4_avg_bitrate = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
536 | 8.85k | .ai4_tgt_bitrate[i4_bitrate_instance_id]; |
537 | | |
538 | 8.85k | if((ps_init_prms->s_config_prms.i4_rate_control_mode == VBR_STREAMING) && |
539 | 8.85k | (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
540 | 4.01k | .ai4_peak_bitrate[i4_bitrate_instance_id] < (1050 * (i4_avg_bitrate / 1000)))) |
541 | 26 | { |
542 | 26 | ps_init_prms->s_config_prms.i4_rate_control_mode = CBR_NLDRC; |
543 | 26 | } |
544 | | |
545 | 8.85k | ps_rc_ctxt->e_rate_control_type = (rc_type_e)ps_init_prms->s_config_prms.i4_rate_control_mode; |
546 | 8.85k | ps_rc_ctxt->i4_capped_vbr_flag = 0; |
547 | 8.85k | if(1 == ps_init_prms->s_config_prms.i4_rate_control_mode) |
548 | 0 | { |
549 | | /* The path taken by capped vbr mode is same as normal VBR mode. Only a flag needs to be enabled |
550 | | which tells the rc module that encoder is running in capped vbr mode */ |
551 | 0 | ps_rc_ctxt->e_rate_control_type = VBR_STREAMING; |
552 | 0 | ps_rc_ctxt->i4_capped_vbr_flag = 1; |
553 | 0 | } |
554 | 8.85k | ASSERT( |
555 | 8.85k | (ps_rc_ctxt->e_rate_control_type == CBR_NLDRC) || |
556 | 8.85k | (ps_rc_ctxt->e_rate_control_type == CONST_QP) || |
557 | 8.85k | (ps_rc_ctxt->e_rate_control_type == VBR_STREAMING)); |
558 | | |
559 | 8.85k | ps_rc_ctxt->u4_avg_bit_rate = i4_avg_bitrate; |
560 | 88.5k | for(i = 0; i < MAX_PIC_TYPE; i++) |
561 | 79.7k | { |
562 | 79.7k | if(ps_rc_ctxt->e_rate_control_type == VBR_STREAMING) |
563 | 35.8k | { |
564 | 35.8k | ps_rc_ctxt->au4_peak_bit_rate[i] = |
565 | 35.8k | ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
566 | 35.8k | .ai4_peak_bitrate[i4_bitrate_instance_id]; |
567 | 35.8k | } |
568 | 43.8k | else |
569 | 43.8k | { |
570 | | /*peak bitrate parameter is ignored in CBR*/ |
571 | 43.8k | ps_rc_ctxt->au4_peak_bit_rate[i] = i4_avg_bitrate; |
572 | 43.8k | } |
573 | 79.7k | } |
574 | 8.85k | ps_rc_ctxt->u4_min_bit_rate = i4_avg_bitrate; |
575 | | |
576 | | /*buffer size init*/ |
577 | 8.85k | u4_buf_size = (WORD32)(ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
578 | 8.85k | .ai4_max_vbv_buffer_size[i4_bitrate_instance_id]); |
579 | 8.85k | ps_rc_ctxt->u4_max_delay = (UWORD32)( |
580 | 8.85k | (float)u4_buf_size / i4_avg_bitrate * 1000); /*delay in milli-seconds based on buffer size*/ |
581 | 8.85k | ps_rc_ctxt->u4_max_vbv_buff_size = u4_buf_size; /*buffer size should be in bits*/ |
582 | | /*This dictates the max deviaiton allowed for file size in VBR mode. */ |
583 | 8.85k | ps_rc_ctxt->f_vbr_max_peak_sustain_dur = |
584 | 8.85k | ((float)ps_init_prms->s_config_prms.i4_vbr_max_peak_rate_dur) / 1000; |
585 | 8.85k | ps_rc_ctxt->i8_num_frms_to_encode = (WORD32)ps_init_prms->s_config_prms.i4_num_frms_to_encode; |
586 | 8.85k | i4_peak_bitrate_factor = (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
587 | 8.85k | .ai4_peak_bitrate[i4_bitrate_instance_id] / |
588 | 8.85k | i4_avg_bitrate) * |
589 | 8.85k | 1000; |
590 | 8.85k | { |
591 | | //float f_delay = ((float)ps_init_prms->s_config_prms.i4_max_vbv_buffer_size*1000)/i4_peak_bitrate_factor; |
592 | 8.85k | float f_delay = ((float)ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
593 | 8.85k | .ai4_max_vbv_buffer_size[i4_bitrate_instance_id] * |
594 | 8.85k | 1000) / |
595 | 8.85k | ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
596 | 8.85k | .ai4_peak_bitrate[i4_bitrate_instance_id]; |
597 | 8.85k | ps_rc_ctxt->i4_initial_decoder_delay_frames = (WORD32)( |
598 | 8.85k | ((f_delay) * (ps_init_prms->s_src_prms.i4_frm_rate_num / |
599 | 8.85k | (ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
600 | 8.85k | .i4_frm_rate_scale_factor * |
601 | 8.85k | ps_init_prms->s_src_prms.i4_frm_rate_denom))) / |
602 | 8.85k | 1000); |
603 | 8.85k | } |
604 | | /*Initial buffer fullness*/ |
605 | 8.85k | ps_rc_ctxt->i4_init_vbv_fullness = ps_init_prms->s_config_prms.i4_init_vbv_fullness; |
606 | | |
607 | | /*Init Qp updation. This seems to be used for pre enc stage of second frame. Needs to be looked into*/ |
608 | 8.85k | ps_rc_ctxt->i4_init_frame_qp_user = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id] |
609 | 8.85k | .ai4_frame_qp[i4_bitrate_instance_id]; |
610 | | |
611 | 274k | for(i = 0; i < MAX_SCENE_NUM; i++) |
612 | 265k | { |
613 | 2.65M | for(j = 0; j < MAX_PIC_TYPE; j++) |
614 | 2.39M | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i][j] = INIT_HEVCE_QP_RC; |
615 | 265k | } |
616 | 8.85k | memset(&ps_rc_ctxt->ai4_scene_numbers[0], 0, sizeof(ps_rc_ctxt->ai4_scene_numbers)); |
617 | 8.85k | memset(&ps_rc_ctxt->ai4_scene_num_last_pic[0], 0, sizeof(ps_rc_ctxt->ai4_scene_num_last_pic)); |
618 | 8.85k | ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0] = ps_rc_ctxt->i4_min_frame_qp - 1; |
619 | 8.85k | ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1] = ps_rc_ctxt->i4_min_frame_qp - 1; |
620 | | /* SGI & Enc Loop Parallelism related changes*/ |
621 | 17.7k | for(i = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++) |
622 | 8.85k | { |
623 | 8.85k | ps_rc_ctxt->ai8_cur_frm_intra_cost[i] = 0; |
624 | 8.85k | ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i] = 0; |
625 | 8.85k | ps_rc_ctxt->ai4_I_model_only_reset[i] = 0; |
626 | 8.85k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i] = 0; |
627 | 8.85k | ps_rc_ctxt->ai4_is_pause_to_resume[i] = 0; |
628 | 8.85k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i] = 0; |
629 | 8.85k | ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i] = 0; |
630 | | /*initialize assuming 30 percent intra and 70 percent inter weightage*/ |
631 | 8.85k | ps_rc_ctxt->ai4_lap_complexity_q7[i] = MODERATE_LAP2_COMPLEXITY_Q7; |
632 | | |
633 | 8.85k | ps_rc_ctxt->ai4_lap_f_sim[i] = MODERATE_FSIM_VALUE; |
634 | 8.85k | } |
635 | | |
636 | | /*Init variables required to handle entropy and rdopt consumption mismatch*/ |
637 | 8.85k | ps_rc_ctxt->i4_rdopt_bit_count = 0; |
638 | 8.85k | ps_rc_ctxt->i4_entropy_bit_count = 0; |
639 | 26.5k | for(i = 0; i < NUM_BUF_RDOPT_ENT_CORRECT; i++) |
640 | 17.7k | { |
641 | 17.7k | ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[i] = |
642 | 17.7k | -1; /*negative bit signifies that value is not populated*/ |
643 | 17.7k | ps_rc_ctxt->ai4_rdopt_bit_consumption_buf_id[i] = -1; |
644 | 17.7k | ps_rc_ctxt->ai4_entropy_bit_consumption[i] = -1; |
645 | 17.7k | ps_rc_ctxt->ai4_entropy_bit_consumption_buf_id[i] = -1; |
646 | 17.7k | } |
647 | | |
648 | | /** scd model reset related param init*/ |
649 | 44.2k | for(i = 0; i < MAX_NUM_TEMPORAL_LAYERS; i++) |
650 | 35.4k | { |
651 | 35.4k | ps_rc_ctxt->au4_scene_num_temp_id[i] = 0; |
652 | 35.4k | } |
653 | | /* SGI & Enc Loop Parallelism related changes*/ |
654 | 17.7k | for(i = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++) |
655 | 8.85k | { |
656 | 8.85k | ps_rc_ctxt->ai4_is_frame_scd[i] = 0; |
657 | 8.85k | } |
658 | | |
659 | | /*Stat file pointer passed from applicaition*/ |
660 | 8.85k | ps_rc_ctxt->pf_stat_file = NULL; |
661 | 8.85k | ps_rc_ctxt->i8_num_frame_read = 0; |
662 | | |
663 | 8.85k | return ps_rc_ctxt; |
664 | 8.85k | } |
665 | | |
666 | | /*###############################################*/ |
667 | | /******* END OF RC MEM INIT FUNCTIONS **********/ |
668 | | /*###############################################*/ |
669 | | |
670 | | /*###############################################*/ |
671 | | /******* START OF RC INIT FUNCTIONS **************/ |
672 | | /*###############################################*/ |
673 | | /** |
674 | | ****************************************************************************** |
675 | | * |
676 | | * @brief Initialises teh Rate control ctxt |
677 | | * |
678 | | * @par Description |
679 | | * |
680 | | * @param[inout] pv_ctxt |
681 | | * pointer to memory descriptors table |
682 | | * |
683 | | * @param[in] ps_run_time_src_param |
684 | | * Create time static parameters |
685 | | * |
686 | | * @return void |
687 | | * |
688 | | ****************************************************************************** |
689 | | */ |
690 | | void ihevce_rc_init( |
691 | | void *pv_ctxt, |
692 | | ihevce_src_params_t *ps_run_time_src_param, |
693 | | ihevce_tgt_params_t *ps_tgt_params, |
694 | | rc_quant_t *ps_rc_quant, |
695 | | ihevce_sys_api_t *ps_sys_api, |
696 | | ihevce_lap_params_t *ps_lap_prms, |
697 | | WORD32 i4_num_frame_parallel) |
698 | 8.85k | { |
699 | 8.85k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
700 | 8.85k | WORD32 i, i_temp, j; |
701 | 8.85k | float f_temp; |
702 | | |
703 | | /*run time width and height has to considered*/ |
704 | 8.85k | ps_rc_ctxt->i4_frame_height = ps_tgt_params->i4_height; |
705 | 8.85k | ps_rc_ctxt->i4_frame_width = ps_tgt_params->i4_width; |
706 | 8.85k | ps_rc_ctxt->i4_field_pic = ps_run_time_src_param->i4_field_pic; |
707 | 8.85k | ps_rc_ctxt->i8_num_bit_alloc_period = 0; |
708 | 8.85k | ps_rc_ctxt->i8_new_bitrate = -1; /*-1 indicates no dynamic change in bitrate request pending*/ |
709 | 8.85k | ps_rc_ctxt->i8_new_peak_bitrate = -1; |
710 | | |
711 | 8.85k | ps_rc_ctxt->i4_is_last_frame_scan = 0; |
712 | | |
713 | 8.85k | memset(ps_rc_ctxt->ai4_offsets, 0, 5 * sizeof(WORD32)); |
714 | | |
715 | 8.85k | ps_rc_ctxt->i4_complexity_bin = 5; |
716 | 8.85k | ps_rc_ctxt->i4_last_p_or_i_frame_gop = 0; |
717 | 8.85k | ps_rc_ctxt->i4_qp_at_I_frame_for_skip_sad = 1; |
718 | 8.85k | ps_rc_ctxt->i4_denominator_i_to_avg = 1; |
719 | 8.85k | ps_rc_ctxt->i4_fp_bit_alloc_in_sp = 0; |
720 | | |
721 | 8.85k | ps_rc_ctxt->ai4_offsets[0] = 0; |
722 | 8.85k | ps_rc_ctxt->ai4_offsets[1] = 1; |
723 | 8.85k | ps_rc_ctxt->ai4_offsets[2] = 2; |
724 | 8.85k | ps_rc_ctxt->ai4_offsets[3] = 3; |
725 | 8.85k | ps_rc_ctxt->ai4_offsets[4] = 4; |
726 | | |
727 | 8.85k | ps_rc_ctxt->i4_num_frames_subgop = 0; |
728 | 8.85k | ps_rc_ctxt->i8_total_acc_coarse_me_sad = 0; |
729 | | |
730 | 8.85k | ps_rc_ctxt->i4_L0_frame_qp = 1; |
731 | | |
732 | 8.85k | ps_rc_ctxt->i4_est_text_bits_ctr_get_qp = 0; |
733 | 8.85k | ps_rc_ctxt->i4_est_text_bits_ctr_update_qp = 0; |
734 | | |
735 | | /*CAllback functions need to be copied for use inside RC*/ |
736 | 8.85k | ps_rc_ctxt->ps_sys_rc_api = ps_sys_api; |
737 | | |
738 | 8.85k | f_temp = ((float)(ps_rc_quant->i2_max_qp + ps_rc_quant->i1_qp_offset - 4) / 6); |
739 | | |
740 | 8.85k | ps_rc_quant->i2_max_qscale = (WORD16)((float)pow(2, f_temp) + 0.5) << 3; |
741 | | |
742 | 8.85k | f_temp = ((float)(ps_rc_quant->i2_min_qp + ps_rc_quant->i1_qp_offset - 4) / 6); |
743 | | |
744 | 8.85k | ps_rc_quant->i2_min_qscale = (WORD16)((float)pow(2, f_temp) + 0.5); |
745 | | |
746 | 8.85k | f_temp = |
747 | 8.85k | ((float)(51 + ps_rc_quant->i1_qp_offset - 4) / |
748 | 8.85k | 6); // default MPEG2 to HEVC and HEVC to MPEG2 Qp conversion tables |
749 | 8.85k | i_temp = (WORD16)((float)pow(2, f_temp) + 0.5); |
750 | | |
751 | 8.85k | i_temp = (i_temp << 3); // Q3 format is mantained for accuarate calc at lower qp |
752 | | |
753 | 16.1M | for(i = 0; i <= i_temp; i++) |
754 | 16.1M | { |
755 | 16.1M | ps_rc_quant->pi4_qscale_to_qp[i] = |
756 | 16.1M | ihevce_rc_get_scaled_hevce_qp_q3(i, ps_rc_ctxt->u1_bit_depth); |
757 | 16.1M | } |
758 | | |
759 | 469k | for(i = (0 - ps_rc_quant->i1_qp_offset); i <= 51; i++) |
760 | 460k | { |
761 | 460k | ps_rc_quant->pi4_qp_to_qscale_q_factor[i + ps_rc_quant->i1_qp_offset] = |
762 | 460k | ihevce_rc_get_scaled_mpeg2_qp_q6( |
763 | 460k | i + ps_rc_quant->i1_qp_offset, ps_rc_ctxt->u1_bit_depth); |
764 | 460k | ps_rc_quant->pi4_qp_to_qscale[i + ps_rc_quant->i1_qp_offset] = |
765 | 460k | ((ps_rc_quant->pi4_qp_to_qscale_q_factor[i + ps_rc_quant->i1_qp_offset] + |
766 | 460k | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
767 | 460k | QSCALE_Q_FAC_3); |
768 | 460k | } |
769 | | |
770 | 8.85k | if(ps_rc_quant->i2_min_qscale < 1) |
771 | 0 | { |
772 | 0 | ps_rc_quant->i2_min_qscale = 1; |
773 | 0 | } |
774 | | |
775 | 8.85k | ps_rc_ctxt->ps_rc_quant_ctxt = ps_rc_quant; |
776 | | |
777 | | /*Frame rate init*/ |
778 | 8.85k | ps_rc_ctxt->u4_max_frame_rate = |
779 | 8.85k | ps_run_time_src_param->i4_frm_rate_num / ps_tgt_params->i4_frm_rate_scale_factor; |
780 | 8.85k | ps_rc_ctxt->i4_top_field_first = ps_run_time_src_param->i4_topfield_first; /**/ |
781 | | /*min and max qp initialization*/ |
782 | 8.85k | if(ps_rc_ctxt->i4_field_pic == 0) |
783 | 8.85k | { |
784 | 8.85k | WORD32 i4_max_qp = 0; |
785 | | |
786 | 8.85k | if(ps_rc_ctxt->u1_bit_depth == 10) |
787 | 0 | { |
788 | 0 | i4_max_qp = MAX_HEVC_QP_10bit; |
789 | 0 | } |
790 | 8.85k | else if(ps_rc_ctxt->u1_bit_depth == 12) |
791 | 0 | { |
792 | 0 | i4_max_qp = MAX_HEVC_QP_12bit; |
793 | 0 | } |
794 | 8.85k | else |
795 | 8.85k | { |
796 | 8.85k | i4_max_qp = MAX_HEVC_QP; |
797 | 8.85k | } |
798 | | |
799 | 88.5k | for(i = 0; i < MAX_PIC_TYPE; i++) |
800 | 79.7k | { |
801 | 79.7k | if((ps_rc_ctxt->i4_init_frame_qp_user + (2 * i) + |
802 | 79.7k | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset) <= |
803 | 79.7k | i4_max_qp) //BUG_FIX related to init QP allocation |
804 | 62.7k | { |
805 | 62.7k | ps_rc_ctxt->ai4_init_qp[i] = (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale |
806 | 62.7k | [(ps_rc_ctxt->i4_init_frame_qp_user + (2 * i)) + |
807 | 62.7k | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset] + |
808 | 62.7k | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
809 | 62.7k | QSCALE_Q_FAC_3; |
810 | 62.7k | } |
811 | 16.9k | else |
812 | 16.9k | { |
813 | 16.9k | ps_rc_ctxt->ai4_init_qp[i] = |
814 | 16.9k | (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_max_qp] + |
815 | 16.9k | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
816 | 16.9k | QSCALE_Q_FAC_3; // + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset]; |
817 | 16.9k | } |
818 | 79.7k | ps_rc_ctxt->ai4_min_max_qp[i * 2] = |
819 | 79.7k | ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/ |
820 | 79.7k | ps_rc_ctxt->ai4_min_max_qp[i * 2 + 1] = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale >> |
821 | 79.7k | QSCALE_Q_FAC_3; /*max qp for each picture type*/ |
822 | 79.7k | } |
823 | 8.85k | } |
824 | 0 | else |
825 | 0 | { |
826 | 0 | WORD32 i4_num_pic_types = MAX_PIC_TYPE; |
827 | 0 | WORD32 i4_max_qp = 0; |
828 | |
|
829 | 0 | if(ps_rc_ctxt->u1_bit_depth == 10) |
830 | 0 | { |
831 | 0 | i4_max_qp = MAX_HEVC_QP_10bit; |
832 | 0 | } |
833 | 0 | else if(ps_rc_ctxt->u1_bit_depth == 12) |
834 | 0 | { |
835 | 0 | i4_max_qp = MAX_HEVC_QP_12bit; |
836 | 0 | } |
837 | 0 | else |
838 | 0 | { |
839 | 0 | i4_max_qp = MAX_HEVC_QP; |
840 | 0 | } |
841 | |
|
842 | 0 | i4_num_pic_types >>= 1; |
843 | |
|
844 | 0 | for(i = 0; i < i4_num_pic_types; i++) |
845 | 0 | { |
846 | 0 | if((ps_rc_ctxt->i4_init_frame_qp_user + (2 * i) + |
847 | 0 | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset) <= i4_max_qp) |
848 | 0 | { |
849 | 0 | ps_rc_ctxt->ai4_init_qp[i] = (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale |
850 | 0 | [(ps_rc_ctxt->i4_init_frame_qp_user + (2 * i)) + |
851 | 0 | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset] + |
852 | 0 | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
853 | 0 | QSCALE_Q_FAC_3; |
854 | |
|
855 | 0 | if(i != 0) |
856 | 0 | ps_rc_ctxt->ai4_init_qp[i + FIELD_OFFSET] = ps_rc_ctxt->ai4_init_qp[i]; |
857 | 0 | } |
858 | 0 | else |
859 | 0 | { |
860 | 0 | ps_rc_ctxt->ai4_init_qp[i] = |
861 | 0 | (ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_max_qp] + |
862 | 0 | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
863 | 0 | QSCALE_Q_FAC_3; // + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset]; |
864 | |
|
865 | 0 | if(i != 0) |
866 | 0 | ps_rc_ctxt->ai4_init_qp[i + FIELD_OFFSET] = ps_rc_ctxt->ai4_init_qp[i]; |
867 | 0 | } |
868 | 0 | ps_rc_ctxt->ai4_min_max_qp[i * 2] = |
869 | 0 | ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/ |
870 | 0 | ps_rc_ctxt->ai4_min_max_qp[i * 2 + 1] = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale >> |
871 | 0 | QSCALE_Q_FAC_3; /*max qp for each picture type*/ |
872 | 0 | if(i != 0) |
873 | 0 | { |
874 | 0 | ps_rc_ctxt->ai4_min_max_qp[(i + FIELD_OFFSET) * 2] = |
875 | 0 | ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qscale; /*min qp for each picture type*/ |
876 | 0 | ps_rc_ctxt->ai4_min_max_qp[(i + FIELD_OFFSET) * 2 + 1] = |
877 | 0 | ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qscale; /*max qp for each picture type*/ |
878 | 0 | } |
879 | 0 | } |
880 | 0 | } |
881 | | |
882 | 8.85k | for(j = 0; i < MAX_NUM_ENC_LOOP_PARALLEL; i++) |
883 | 0 | { |
884 | | /*initialise the coeffs to 1 in case lap is not used */ |
885 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
886 | 0 | { |
887 | 0 | ps_rc_ctxt->af_sum_weigh[j][i][0] = 1.0; |
888 | 0 | ps_rc_ctxt->af_sum_weigh[j][i][1] = 0.0; |
889 | 0 | ps_rc_ctxt->af_sum_weigh[j][i][2] = 0.0; |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | 8.85k | ps_rc_ctxt->i4_num_frame_parallel = i4_num_frame_parallel; //ELP_RC |
894 | 8.85k | i4_num_frame_parallel = (i4_num_frame_parallel > 1) ? i4_num_frame_parallel : 0; |
895 | | |
896 | 8.85k | if(ps_rc_ctxt->i4_num_frame_parallel > 1) |
897 | 0 | { |
898 | 0 | ps_rc_ctxt->i4_pre_enc_rc_delay = MAX_PRE_ENC_RC_DELAY; |
899 | 0 | } |
900 | 8.85k | else |
901 | 8.85k | { |
902 | 8.85k | ps_rc_ctxt->i4_pre_enc_rc_delay = MIN_PRE_ENC_RC_DELAY; |
903 | 8.85k | } |
904 | | /*Bitrate and resolutioon based scene cut min qp*/ |
905 | 8.85k | { |
906 | | /*The min qp for scene cut frame is chosen based on bitrate*/ |
907 | 8.85k | float i4_bpp = ((float)ps_rc_ctxt->u4_avg_bit_rate / ps_rc_ctxt->u4_max_frame_rate) * 1000 / |
908 | 8.85k | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width); |
909 | 8.85k | if(ps_rc_ctxt->u4_intra_frame_interval == 1) |
910 | 512 | { |
911 | | /*Ultra High resolution)*/ |
912 | 512 | if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) > 5000000) |
913 | 0 | { |
914 | 0 | if(i4_bpp > 0.24) |
915 | 0 | { |
916 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR; |
917 | 0 | } |
918 | 0 | else if(i4_bpp > 0.16) |
919 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = |
920 | 0 | SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 40mbps for 4k 30p*/ |
921 | 0 | else |
922 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP; |
923 | 0 | } |
924 | 512 | else |
925 | 512 | { |
926 | 512 | if(i4_bpp > 0.32) |
927 | 312 | { |
928 | 312 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR; |
929 | 312 | } |
930 | 200 | else if(i4_bpp > 0.24) |
931 | 126 | ps_rc_ctxt->i4_min_scd_hevc_qp = |
932 | 126 | SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 15mbps for 1080 30p*/ |
933 | 74 | else |
934 | 74 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP; |
935 | 512 | } |
936 | 512 | } |
937 | 8.34k | else |
938 | 8.34k | { |
939 | | /*Ultra High resolution)*/ |
940 | 8.34k | if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) > 5000000) |
941 | 0 | { |
942 | 0 | if(i4_bpp > 0.16) |
943 | 0 | { |
944 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR; |
945 | 0 | } |
946 | 0 | else if(i4_bpp > 0.08) |
947 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = |
948 | 0 | SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 20mbps for 4k 30p*/ |
949 | 0 | else |
950 | 0 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP; |
951 | 0 | } |
952 | 8.34k | else |
953 | 8.34k | { |
954 | | /*Resolution lesser than full HD (including )*/ |
955 | 8.34k | if(i4_bpp > 0.24) |
956 | 7.18k | { |
957 | 7.18k | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP_VHBR; |
958 | 7.18k | } |
959 | 1.15k | else if(i4_bpp > 0.16) |
960 | 342 | ps_rc_ctxt->i4_min_scd_hevc_qp = |
961 | 342 | SCD_MIN_HEVC_QP_HBR; /*corresponds to bitrate greater than 10mbps for 1080 30p*/ |
962 | 814 | else |
963 | 814 | ps_rc_ctxt->i4_min_scd_hevc_qp = SCD_MIN_HEVC_QP; |
964 | 8.34k | } |
965 | 8.34k | } |
966 | 8.85k | } |
967 | | |
968 | 8.85k | initialise_rate_control( |
969 | 8.85k | ps_rc_ctxt->rc_hdl, |
970 | 8.85k | ps_rc_ctxt->e_rate_control_type, |
971 | 8.85k | ps_rc_ctxt->u1_is_mb_level_rc_on, //0,/*disabling MB level RC*/ |
972 | 8.85k | ps_rc_ctxt->u4_avg_bit_rate, |
973 | 8.85k | ps_rc_ctxt->au4_peak_bit_rate, |
974 | 8.85k | ps_rc_ctxt->u4_min_bit_rate, |
975 | 8.85k | ps_rc_ctxt->u4_max_frame_rate, |
976 | 8.85k | ps_rc_ctxt->u4_max_delay, /*max delay in milli seconds based on buffer size*/ |
977 | 8.85k | ps_rc_ctxt->u4_intra_frame_interval, |
978 | 8.85k | ps_rc_ctxt->u4_idr_period, |
979 | 8.85k | ps_rc_ctxt->ai4_init_qp, |
980 | 8.85k | ps_rc_ctxt->u4_max_vbv_buff_size, |
981 | 8.85k | ps_rc_ctxt->i4_max_inter_frm_int, |
982 | 8.85k | ps_rc_ctxt->i4_is_gop_closed, |
983 | 8.85k | ps_rc_ctxt->ai4_min_max_qp, /*min and max qp to be used for each of picture type*/ |
984 | 8.85k | ps_rc_ctxt->i4_use_est_intra_sad, |
985 | 8.85k | ps_rc_ctxt->u4_src_ticks, |
986 | 8.85k | ps_rc_ctxt->u4_tgt_ticks, |
987 | 8.85k | ps_rc_ctxt->i4_frame_height, /*pels in frame considering 420 semi planar format*/ |
988 | 8.85k | ps_rc_ctxt->i4_frame_width, |
989 | 8.85k | ps_rc_ctxt->i4_num_active_pic_type, |
990 | 8.85k | ps_rc_ctxt->i4_field_pic, |
991 | 8.85k | ps_rc_ctxt->i4_quality_preset, |
992 | 8.85k | ps_rc_ctxt->i4_num_frame_in_lap_window, |
993 | 8.85k | ps_rc_ctxt->i4_initial_decoder_delay_frames, |
994 | 8.85k | ps_rc_ctxt->f_vbr_max_peak_sustain_dur, |
995 | 8.85k | ps_rc_ctxt->i8_num_frms_to_encode, |
996 | 8.85k | ps_rc_ctxt->i4_min_scd_hevc_qp, |
997 | 8.85k | ps_rc_ctxt->u1_bit_depth, |
998 | 8.85k | ps_rc_ctxt->pf_stat_file, |
999 | 8.85k | ps_rc_ctxt->i4_rc_pass, |
1000 | 8.85k | ps_rc_ctxt->pv_gop_stat, |
1001 | 8.85k | ps_rc_ctxt->i8_num_gop_mem_alloc, |
1002 | 8.85k | ps_rc_ctxt->i4_is_infinite_gop, |
1003 | 8.85k | sizeof(ihevce_lap_output_params_t), |
1004 | 8.85k | sizeof(rc_lap_out_params_t), |
1005 | 8.85k | (void *)ps_sys_api, |
1006 | 8.85k | ps_rc_ctxt->i4_fp_bit_alloc_in_sp, |
1007 | 8.85k | i4_num_frame_parallel, |
1008 | 8.85k | ps_rc_ctxt->i4_capped_vbr_flag); |
1009 | | |
1010 | | //ps_rc_ctxt->i4_init_vbv_fullness = 500000; |
1011 | 8.85k | rc_init_set_ebf(ps_rc_ctxt->rc_hdl, ps_rc_ctxt->i4_init_vbv_fullness); |
1012 | | |
1013 | | /*get init qp based on ebf for rate control*/ |
1014 | 8.85k | if(ps_rc_ctxt->e_rate_control_type != CONST_QP) |
1015 | 5.17k | { |
1016 | 5.17k | WORD32 I_frame_qp, I_frame_mpeg2_qp; |
1017 | | /*assume moderate fsim*/ |
1018 | 5.17k | WORD32 i4_fsim_global = MODERATE_FSIM_VALUE; |
1019 | 5.17k | I_frame_mpeg2_qp = rc_get_bpp_based_scene_cut_qp( |
1020 | 5.17k | ps_rc_ctxt->rc_hdl, |
1021 | 5.17k | I_PIC, |
1022 | 5.17k | ((3 * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 1), |
1023 | 5.17k | i4_fsim_global, |
1024 | 5.17k | ps_rc_ctxt->af_sum_weigh[0], |
1025 | 5.17k | 1); |
1026 | | |
1027 | 5.17k | I_frame_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3( |
1028 | 5.17k | I_frame_mpeg2_qp << QSCALE_Q_FAC_3, ps_rc_ctxt->ps_rc_quant_ctxt); |
1029 | | |
1030 | 5.17k | I_frame_qp = I_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
1031 | | |
1032 | 5.17k | if(I_frame_qp > 44) |
1033 | 144 | I_frame_qp = 44; |
1034 | | |
1035 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[I_PIC] = I_frame_qp; |
1036 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[P_PIC] = I_frame_qp + 1; |
1037 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[B_PIC] = I_frame_qp + 2; |
1038 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[B1_PIC] = I_frame_qp + 3; |
1039 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[B2_PIC] = I_frame_qp + 4; |
1040 | | /*Bottom fields*/ |
1041 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[P1_PIC] = I_frame_qp + 1; |
1042 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[BB_PIC] = I_frame_qp + 2; |
1043 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[B11_PIC] = I_frame_qp + 3; |
1044 | 5.17k | ps_rc_ctxt->ai4_init_pre_enc_qp[B22_PIC] = I_frame_qp + 4; |
1045 | | |
1046 | 5.17k | ps_rc_ctxt->i4_pre_enc_qp_read_index = 0; |
1047 | 5.17k | ps_rc_ctxt->i4_pre_enc_qp_write_index = ps_rc_ctxt->i4_pre_enc_rc_delay - 1; |
1048 | 20.7k | for(i = 0; i < ps_rc_ctxt->i4_pre_enc_rc_delay; i++) |
1049 | 15.5k | { |
1050 | | /*initialize it to -1 to indicate it as not produced*/ |
1051 | 15.5k | ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_is_qp_valid = -1; |
1052 | 15.5k | } |
1053 | 15.5k | for(i = 0; i < (ps_rc_ctxt->i4_pre_enc_qp_write_index); i++) |
1054 | 10.3k | { |
1055 | 10.3k | WORD32 j; |
1056 | 10.3k | ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_is_qp_valid = 1; |
1057 | 103k | for(j = 0; j < MAX_PIC_TYPE; j++) |
1058 | 93.1k | { |
1059 | 93.1k | ps_rc_ctxt->as_pre_enc_qp_queue[i].ai4_quant[j] = |
1060 | 93.1k | ps_rc_ctxt->ai4_init_pre_enc_qp[j]; |
1061 | 93.1k | ps_rc_ctxt->as_pre_enc_qp_queue[i].i4_scd_qp = |
1062 | 93.1k | ps_rc_ctxt->ai4_init_pre_enc_qp[I_PIC]; |
1063 | 93.1k | } |
1064 | 10.3k | } |
1065 | | |
1066 | 5.17k | ps_rc_ctxt->i4_use_qp_offset_pre_enc = 1; |
1067 | 5.17k | ps_rc_ctxt->i4_num_frms_from_reset = 0; |
1068 | | /* SGI & Enc Loop Parallelism related changes*/ |
1069 | 5.17k | ps_rc_ctxt->u4_prev_scene_num = 0; |
1070 | | //ps_rc_ctxt->i4_use_init_qp_for_pre_enc = 0; |
1071 | 88.0k | for(j = 0; j < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; j++) |
1072 | 82.8k | { |
1073 | 82.8k | ps_rc_ctxt->au4_prev_scene_num_multi_scene[j] = 0x3FFFFFFF; |
1074 | 828k | for(i = 0; i < MAX_PIC_TYPE; i++) |
1075 | 745k | { |
1076 | 745k | ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[j][i] = |
1077 | 745k | ps_rc_ctxt->ai4_init_pre_enc_qp[i]; |
1078 | 745k | } |
1079 | 82.8k | } |
1080 | | |
1081 | | /* SGI & Enc Loop Parallelism related changes*/ |
1082 | 51.7k | for(i = 0; i < MAX_PIC_TYPE; i++) |
1083 | 46.5k | { |
1084 | 46.5k | ps_rc_ctxt->ai4_qp_for_previous_scene[i] = ps_rc_ctxt->ai4_init_pre_enc_qp[i]; |
1085 | 46.5k | } |
1086 | 5.17k | } |
1087 | 3.67k | else |
1088 | 3.67k | { |
1089 | 36.7k | for(i = 0; i < MAX_PIC_TYPE; i++) |
1090 | 33.1k | { |
1091 | 33.1k | ps_rc_ctxt->ai4_init_pre_enc_qp[i] = ps_rc_ctxt->i4_init_frame_qp_user; |
1092 | 33.1k | ps_rc_ctxt->ai4_qp_for_previous_scene[i] = ps_rc_ctxt->i4_init_frame_qp_user; |
1093 | 33.1k | } |
1094 | 3.67k | } |
1095 | 8.85k | } |
1096 | | |
1097 | | /** |
1098 | | ****************************************************************************** |
1099 | | * |
1100 | | * @brief Populate common params from lap_out structure to rc_lap_out structure |
1101 | | * Also the init of some rc_lap_out params done here |
1102 | | * @par Description |
1103 | | * |
1104 | | * @param[in] ps_lap_out |
1105 | | * pointer to lap_out structure |
1106 | | * |
1107 | | * @param[out] ps_rc_lap_out |
1108 | | * pointer to rc_lap_out structure |
1109 | | * |
1110 | | * @return void |
1111 | | * |
1112 | | ****************************************************************************** |
1113 | | */ |
1114 | | |
1115 | | void ihevce_rc_populate_common_params( |
1116 | | ihevce_lap_output_params_t *ps_lap_out, rc_lap_out_params_t *ps_rc_lap_out) |
1117 | 165k | { |
1118 | | /* Update common params */ |
1119 | | |
1120 | 165k | ps_rc_lap_out->i4_rc_pic_type = ps_lap_out->i4_pic_type; |
1121 | 165k | ps_rc_lap_out->i4_rc_poc = ps_lap_out->i4_poc; |
1122 | 165k | ps_rc_lap_out->i4_rc_temporal_lyr_id = ps_lap_out->i4_temporal_lyr_id; |
1123 | 165k | ps_rc_lap_out->i4_rc_is_ref_pic = ps_lap_out->i4_is_ref_pic; |
1124 | 165k | ps_rc_lap_out->i4_rc_scene_type = ps_lap_out->i4_scene_type; |
1125 | 165k | ps_rc_lap_out->u4_rc_scene_num = ps_lap_out->u4_scene_num; |
1126 | 165k | ps_rc_lap_out->i4_rc_display_num = ps_lap_out->i4_display_num; |
1127 | 165k | ps_rc_lap_out->i4_rc_quality_preset = ps_lap_out->i4_quality_preset; |
1128 | 165k | ps_rc_lap_out->i4_rc_first_field = ps_lap_out->i4_first_field; |
1129 | | |
1130 | | /*params populated in LAP-2*/ |
1131 | 165k | ps_rc_lap_out->i8_frame_acc_coarse_me_cost = -1; |
1132 | 165k | memset(ps_rc_lap_out->ai8_frame_acc_coarse_me_sad, -1, sizeof(WORD32) * 52); |
1133 | | |
1134 | 165k | ps_rc_lap_out->i8_pre_intra_satd = -1; |
1135 | | |
1136 | 165k | ps_rc_lap_out->i8_raw_pre_intra_sad = -1; |
1137 | | |
1138 | 165k | ps_rc_lap_out->i8_raw_l1_coarse_me_sad = -1; |
1139 | | |
1140 | 165k | ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated = 1; |
1141 | | /* SGI & Enc Loop Parallelism related changes*/ |
1142 | 165k | ps_rc_lap_out->i4_ignore_for_rc_update = 0; |
1143 | | |
1144 | | /*For 1 pass HQ I frames*/ |
1145 | | |
1146 | 165k | ps_rc_lap_out->i4_complexity_bin = 5; |
1147 | 165k | { |
1148 | 165k | WORD32 ai4_offsets[5] = { 0, 1, 2, 3, 4 }; |
1149 | 165k | memmove(ps_rc_lap_out->ai4_offsets, ai4_offsets, sizeof(WORD32) * 5); |
1150 | 165k | ps_rc_lap_out->i4_offsets_set_flag = -1; |
1151 | 165k | } |
1152 | | |
1153 | 165k | ps_rc_lap_out->i4_L1_qp = -1; |
1154 | 165k | ps_rc_lap_out->i4_L0_qp = -1; |
1155 | 165k | } |
1156 | | |
1157 | | /*###############################################*/ |
1158 | | /******* END OF RC INIT FUNCTIONS **************/ |
1159 | | /*###############################################*/ |
1160 | | |
1161 | | /*#########################################################*/ |
1162 | | /******* START OF PRE-ENC QP QUERY FUNCTIONS **************/ |
1163 | | /*#######################################################*/ |
1164 | | |
1165 | | /** |
1166 | | ****************************************************************************** |
1167 | | * |
1168 | | * @name ihevce_rc_get_bpp_based_frame_qp |
1169 | | * |
1170 | | * @par Description |
1171 | | * |
1172 | | * @param[in] ps_rc_ctxt - pointer to rc context |
1173 | | * ps_rc_lap_out |
1174 | | * @return frame qp |
1175 | | * |
1176 | | ****************************************************************************** |
1177 | | */ |
1178 | | WORD32 ihevce_rc_get_bpp_based_frame_qp(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out) |
1179 | 114k | { |
1180 | 114k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
1181 | 114k | WORD32 i4_frame_qs_q3, i4_hevc_frame_qp, i; |
1182 | 114k | frame_info_t *ps_frame_info; |
1183 | 114k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
1184 | 114k | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
1185 | 114k | ps_rc_ctxt->i4_field_pic, |
1186 | 114k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
1187 | 114k | ps_rc_lap_out->i4_is_bottom_field, |
1188 | 114k | ps_rc_ctxt->i4_top_field_first); |
1189 | | /*initialise the coeffs to 1 in case lap is not used */ |
1190 | 1.14M | for(i = 0; i < MAX_PIC_TYPE; i++) |
1191 | 1.03M | { |
1192 | 1.03M | ps_rc_ctxt->af_sum_weigh[0][i][0] = 1.0; |
1193 | 1.03M | ps_rc_ctxt->af_sum_weigh[0][i][1] = 0.0; |
1194 | 1.03M | ps_rc_ctxt->af_sum_weigh[0][i][2] = 0.0; |
1195 | 1.03M | } |
1196 | 114k | { |
1197 | | /*scene cut handling during pre-enc stage*/ |
1198 | | /*assume lap fsim as 117. not used since ratio is direclt sent*/ |
1199 | 114k | if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT) |
1200 | 0 | { |
1201 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
1202 | 0 | { |
1203 | 0 | ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] = -1; |
1204 | 0 | ps_rc_ctxt->ai8_prev_frame_hme_sad[i] = -1; |
1205 | 0 | ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i] = -1; |
1206 | 0 | } |
1207 | 0 | ps_rc_ctxt->i4_is_est_L0_intra_sad_available = 0; |
1208 | 0 | } |
1209 | | |
1210 | 114k | if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT || |
1211 | 114k | !ps_rc_ctxt->i4_is_est_L0_intra_sad_available) |
1212 | 10.3k | { |
1213 | | /*compute bpp based qp if current frame is scene cut or data is not sufficient*/ |
1214 | 10.3k | i4_frame_qs_q3 = rc_get_bpp_based_scene_cut_qp( |
1215 | 10.3k | ps_rc_ctxt->rc_hdl, |
1216 | 10.3k | I_PIC, |
1217 | 10.3k | ((3 * ps_rc_lap_out->i4_num_pels_in_frame_considered) >> 1), |
1218 | 10.3k | 117, |
1219 | 10.3k | ps_rc_ctxt->af_sum_weigh[0], |
1220 | 10.3k | 0); |
1221 | 10.3k | i4_frame_qs_q3 = i4_frame_qs_q3 << QSCALE_Q_FAC_3; |
1222 | 10.3k | } |
1223 | 104k | else |
1224 | 104k | { |
1225 | | /*using previous one sub-gop data calculate i to rest ratio and qp assuming it is I frame*/ |
1226 | 104k | WORD32 i4_num_b, i, ai4_pic_dist[MAX_PIC_TYPE], index, i4_total_bits; |
1227 | 104k | LWORD64 i8_average_pre_intra_sad = 0, i8_average_est_l0_satd_by_act = 0; |
1228 | 104k | double lambda_modifier[MAX_PIC_TYPE], complexity[MAX_PIC_TYPE], den = 0.0f, |
1229 | 104k | i_to_rest_bit_ratio; |
1230 | 104k | WORD32 i4_curr_bits_estimated = 0; |
1231 | | |
1232 | 1.04M | for(i = 0; i < MAX_PIC_TYPE; i++) |
1233 | 938k | { |
1234 | 938k | complexity[i] = 0; |
1235 | 938k | lambda_modifier[i] = 0; |
1236 | 938k | ai4_pic_dist[i] = 0; |
1237 | 938k | } |
1238 | | |
1239 | 104k | index = ihevce_get_offline_index( |
1240 | 104k | ps_rc_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered); |
1241 | 104k | if(ps_rc_ctxt->i4_max_temporal_lyr) |
1242 | 10.3k | { |
1243 | 10.3k | i4_num_b = ((WORD32)pow((float)2, ps_rc_ctxt->i4_max_temporal_lyr)) - 1; |
1244 | 10.3k | } |
1245 | 93.9k | else |
1246 | 93.9k | { |
1247 | 93.9k | i4_num_b = 0; |
1248 | 93.9k | } |
1249 | | |
1250 | 104k | lambda_modifier[I_PIC] = |
1251 | 104k | ihevce_get_frame_lambda_modifier((WORD8)I_PIC, 0, 1, 1, i4_num_b); |
1252 | 104k | lambda_modifier[P_PIC] = |
1253 | 104k | ihevce_get_frame_lambda_modifier((WORD8)P_PIC, 0, 1, 1, i4_num_b) * |
1254 | 104k | pow((float)1.125, 1); |
1255 | 104k | lambda_modifier[B_PIC] = |
1256 | 104k | ihevce_get_frame_lambda_modifier( |
1257 | 104k | (WORD8)B_PIC, 1, (ps_rc_ctxt->i4_max_temporal_lyr > 1), 1, i4_num_b) * |
1258 | 104k | pow((float)1.125, 2); |
1259 | 104k | lambda_modifier[B1_PIC] = |
1260 | 104k | ihevce_get_frame_lambda_modifier( |
1261 | 104k | (WORD8)B1_PIC, 2, 1, (ps_rc_ctxt->i4_max_temporal_lyr > 2), i4_num_b) * |
1262 | 104k | pow((float)1.125, 3); |
1263 | 104k | lambda_modifier[B2_PIC] = |
1264 | 104k | ihevce_get_frame_lambda_modifier((WORD8)B2_PIC, 3, 1, 0, i4_num_b) * |
1265 | 104k | pow((float)1.125, 4); |
1266 | | |
1267 | | /*consider average of one sub-gop for intra sad*/ |
1268 | | |
1269 | 104k | if(ps_rc_ctxt->i4_quality_preset == IHEVCE_QUALITY_P6) |
1270 | 33.7k | { |
1271 | 101k | for(i = 0; i < 2; i++) |
1272 | 67.5k | { |
1273 | 67.5k | i8_average_pre_intra_sad += ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i]; |
1274 | 67.5k | i8_average_est_l0_satd_by_act += ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i]; |
1275 | 67.5k | if(ps_rc_ctxt->i4_field_pic == 1 && i != 0) |
1276 | 0 | { |
1277 | 0 | i8_average_pre_intra_sad += |
1278 | 0 | ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i + FIELD_OFFSET]; |
1279 | 0 | i8_average_est_l0_satd_by_act += |
1280 | 0 | ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET]; |
1281 | 0 | } |
1282 | 67.5k | } |
1283 | 33.7k | if(ps_rc_ctxt->i4_field_pic == 1) |
1284 | 0 | { |
1285 | 0 | i8_average_pre_intra_sad /= 3; |
1286 | 0 | i8_average_est_l0_satd_by_act /= 3; |
1287 | 0 | } |
1288 | 33.7k | else |
1289 | 33.7k | { |
1290 | 33.7k | i8_average_pre_intra_sad <<= 1; |
1291 | 33.7k | i8_average_est_l0_satd_by_act <<= 1; |
1292 | 33.7k | } |
1293 | 33.7k | } |
1294 | 70.4k | else |
1295 | 70.4k | { |
1296 | 225k | for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
1297 | 154k | { |
1298 | 154k | i8_average_pre_intra_sad += ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i]; |
1299 | 154k | i8_average_est_l0_satd_by_act += ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i]; |
1300 | 154k | if(ps_rc_ctxt->i4_field_pic == 1 && i != 0) |
1301 | 0 | { |
1302 | 0 | i8_average_pre_intra_sad += |
1303 | 0 | ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[i + FIELD_OFFSET]; |
1304 | 0 | i8_average_est_l0_satd_by_act += |
1305 | 0 | ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET]; |
1306 | 0 | } |
1307 | 154k | } |
1308 | 70.4k | if(ps_rc_ctxt->i4_field_pic == 1) |
1309 | 0 | { |
1310 | 0 | i8_average_pre_intra_sad /= ((i << 1) - 1); |
1311 | 0 | i8_average_est_l0_satd_by_act /= ((i << 1) - 1); |
1312 | 0 | } |
1313 | 70.4k | else |
1314 | 70.4k | { |
1315 | 70.4k | i8_average_pre_intra_sad /= i; |
1316 | 70.4k | i8_average_est_l0_satd_by_act /= i; |
1317 | 70.4k | } |
1318 | 70.4k | } |
1319 | | |
1320 | | /*no lambda modifier is considered for I pic as other lambda are scaled according to I frame lambda*/ |
1321 | 104k | complexity[I_PIC] = (double)i8_average_pre_intra_sad; |
1322 | | |
1323 | 212k | for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
1324 | 108k | { |
1325 | 108k | #if !USE_SQRT |
1326 | 108k | complexity[i] = ps_rc_ctxt->ai8_prev_frame_hme_sad[i] / pow(1.125, i); |
1327 | | |
1328 | 108k | if(ps_rc_ctxt->i4_field_pic == 1) |
1329 | 0 | { |
1330 | 0 | complexity[i + FIELD_OFFSET] = |
1331 | 0 | ps_rc_ctxt->ai8_prev_frame_hme_sad[i + FIELD_OFFSET] / pow(1.125, i); |
1332 | 0 | } |
1333 | | #else |
1334 | | complexity[i] = ps_rc_ctxt->ai8_prev_frame_hme_sad[i] / |
1335 | | (sqrt(lambda_modifier[i] / lambda_modifier[I_PIC]) * pow(1.125, i)); |
1336 | | #endif |
1337 | 108k | } |
1338 | | /*get picture type distribution in LAP*/ |
1339 | 104k | rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]); |
1340 | | |
1341 | 1.04M | for(i = 0; i < MAX_PIC_TYPE; i++) |
1342 | 938k | { |
1343 | 938k | den += complexity[i] * ai4_pic_dist[i]; |
1344 | 938k | } |
1345 | | /*subtract I frame complexity to get I to rest ratio*/ |
1346 | 104k | { |
1347 | 104k | WORD32 num_inter_pic = 0; |
1348 | 938k | for(i = 1; i < MAX_PIC_TYPE; i++) |
1349 | 834k | { |
1350 | 834k | num_inter_pic += ai4_pic_dist[i]; |
1351 | 834k | } |
1352 | 104k | if(num_inter_pic > 0) |
1353 | 87.8k | den = (den - (complexity[I_PIC] * ai4_pic_dist[I_PIC])) / num_inter_pic; |
1354 | 16.4k | else |
1355 | 16.4k | den = complexity[I_PIC]; |
1356 | 104k | } |
1357 | | |
1358 | 104k | if(den > 0) |
1359 | 75.1k | i_to_rest_bit_ratio = (float)((complexity[I_PIC]) / den); |
1360 | 29.1k | else |
1361 | 29.1k | i_to_rest_bit_ratio = 15; |
1362 | | |
1363 | | /*get qp for scene cut frame based on offline data*/ |
1364 | 104k | i4_frame_qs_q3 = rc_get_qp_for_scd_frame( |
1365 | 104k | ps_rc_ctxt->rc_hdl, |
1366 | 104k | I_PIC, |
1367 | 104k | i8_average_est_l0_satd_by_act, |
1368 | 104k | ps_rc_lap_out->i4_num_pels_in_frame_considered, |
1369 | 104k | -1, |
1370 | 104k | MODERATE_FSIM_VALUE, |
1371 | 104k | (void *)&g_offline_i_model_coeff[index][0], |
1372 | 104k | (float)i_to_rest_bit_ratio, |
1373 | 104k | 0, |
1374 | 104k | ps_rc_ctxt->af_sum_weigh[0], |
1375 | 104k | ps_rc_lap_out->ps_frame_info, |
1376 | 104k | ps_rc_ctxt->i4_rc_pass, |
1377 | 104k | 0, |
1378 | 104k | 0, |
1379 | 104k | 0, |
1380 | 104k | &i4_total_bits, |
1381 | 104k | &i4_curr_bits_estimated, |
1382 | 104k | ps_rc_lap_out->i4_use_offline_model_2pass, |
1383 | 104k | 0, |
1384 | 104k | 0, |
1385 | 104k | -1, |
1386 | 104k | NULL); |
1387 | 104k | } |
1388 | | |
1389 | 114k | i4_hevc_frame_qp = |
1390 | 114k | ihevce_rc_get_scaled_hevc_qp_from_qs_q3(i4_frame_qs_q3, ps_rc_ctxt->ps_rc_quant_ctxt); |
1391 | | |
1392 | 114k | i4_hevc_frame_qp = i4_hevc_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
1393 | | |
1394 | 114k | if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp) |
1395 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp; |
1396 | | |
1397 | | /*offset depending on current picture type*/ |
1398 | 114k | if(rc_pic_type != I_PIC) |
1399 | 72.6k | i4_hevc_frame_qp += ps_rc_lap_out->i4_rc_temporal_lyr_id + 1; |
1400 | | /*clip min and max qp to be within range*/ |
1401 | 114k | i4_hevc_frame_qp = ihevce_clip_min_max_qp( |
1402 | 114k | ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id); |
1403 | | |
1404 | 114k | ps_rc_ctxt->ai4_qp_for_previous_scene_pre_enc[rc_pic_type] = i4_hevc_frame_qp; |
1405 | 114k | ps_rc_ctxt->au4_prev_scene_num_pre_enc[rc_pic_type] = ps_rc_lap_out->u4_rc_scene_num; |
1406 | 114k | } |
1407 | | |
1408 | 114k | return i4_hevc_frame_qp; |
1409 | 114k | } |
1410 | | /** |
1411 | | ****************************************************************************** |
1412 | | * |
1413 | | * @name ihevce_rc_get_pre_enc_pic_quant |
1414 | | * |
1415 | | * @par Description - Called from ihevce_rc_cal_pre_enc_qp. updates frame qp |
1416 | | * which will be used by next frame of same pic type in |
1417 | | * pre-enc stage |
1418 | | * |
1419 | | * @param[in] ps_rc_ctxt - pointer to rc context |
1420 | | * @return void |
1421 | | * |
1422 | | ****************************************************************************** |
1423 | | */ |
1424 | | WORD32 |
1425 | | ihevce_rc_get_pre_enc_pic_quant(void *pv_ctxt, picture_type_e rc_pic_type, WORD32 *pi4_scd_qp) |
1426 | 228k | { |
1427 | 228k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
1428 | 228k | WORD32 i4_frame_qp, i4_frame_qp_q6, i4_hevc_frame_qp = -1; |
1429 | 228k | WORD32 i4_max_frame_bits = (1 << 30); |
1430 | 228k | WORD32 i4_temporal_layer_id, i4_is_bottom_field, i4_cur_est_texture_bits; |
1431 | | |
1432 | 228k | ihevce_rc_get_pic_param(rc_pic_type, &i4_temporal_layer_id, &i4_is_bottom_field); |
1433 | | |
1434 | 228k | { |
1435 | 228k | WORD32 is_scd_ref_frame = 0, i4_num_scd_in_lap_window = 0, num_frames_b4_scd = 0; |
1436 | | |
1437 | | /*treat even first frame as scd frame*/ |
1438 | 228k | if(!ps_rc_ctxt->i4_is_first_frame_encoded) |
1439 | 0 | { |
1440 | 0 | is_scd_ref_frame = 1; |
1441 | 0 | } |
1442 | | |
1443 | 228k | { |
1444 | | /*Only I frames are considered as scd pic during pre-enc*/ |
1445 | 228k | is_scd_ref_frame &= (rc_pic_type == I_PIC); |
1446 | 228k | } |
1447 | | |
1448 | 228k | rc_set_num_scd_in_lap_window( |
1449 | 228k | ps_rc_ctxt->rc_hdl, i4_num_scd_in_lap_window, num_frames_b4_scd); |
1450 | | |
1451 | | /** Pre-enc thread as of now SCD handling is not present */ |
1452 | | //if(!(is_scd_ref_frame || ps_rc_ctxt->i4_is_pause_to_resume) || call_type == PRE_ENC_GET_QP) |
1453 | 228k | { |
1454 | 228k | WORD32 i4_is_first_frame_coded; |
1455 | | /*Once first frame has been encoded use prev frame intra satd and cur frame satd to alter est intra sad for cur frame*/ |
1456 | 228k | i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl); |
1457 | 228k | { |
1458 | 228k | int i; |
1459 | 228k | WORD32 i4_curr_bits_estimated, i4_is_model_valid; |
1460 | | /*initialise the coeffs to 1 and 0in case lap is not used */ |
1461 | 2.28M | for(i = 0; i < MAX_PIC_TYPE; i++) |
1462 | 2.05M | { |
1463 | 2.05M | ps_rc_ctxt->af_sum_weigh[0][i][0] = 1.0; |
1464 | 2.05M | ps_rc_ctxt->af_sum_weigh[0][i][1] = 0.0; |
1465 | 2.05M | } |
1466 | | |
1467 | 228k | i4_frame_qp_q6 = get_frame_level_qp( |
1468 | 228k | ps_rc_ctxt->rc_hdl, |
1469 | 228k | rc_pic_type, |
1470 | 228k | i4_max_frame_bits, |
1471 | 228k | &i4_cur_est_texture_bits, //this value is returned by rc |
1472 | 228k | ps_rc_ctxt->af_sum_weigh[0], |
1473 | 228k | 0, |
1474 | 228k | 8.0f, |
1475 | 228k | NULL, |
1476 | 228k | ps_rc_ctxt->i4_complexity_bin, |
1477 | 228k | ps_rc_ctxt->i4_scene_num_latest, /*no pause resume concept*/ |
1478 | 228k | &i4_curr_bits_estimated, |
1479 | 228k | &i4_is_model_valid, |
1480 | 228k | NULL, |
1481 | 228k | NULL, |
1482 | 228k | NULL, |
1483 | 228k | NULL, |
1484 | 228k | NULL, |
1485 | 228k | NULL); |
1486 | | |
1487 | | /** The usage of global table will truncate the input given as qp format and hence will not return very low qp values desirable at very |
1488 | | low bitrate. Hence on the fly calculation is enabled*/ |
1489 | 228k | i4_hevc_frame_qp = |
1490 | 228k | ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth); |
1491 | | |
1492 | 228k | if(rc_pic_type == I_PIC) |
1493 | 109k | { |
1494 | | /*scene cut handling during pre-enc stage*/ |
1495 | 109k | i4_frame_qp = rc_get_bpp_based_scene_cut_qp( |
1496 | 109k | ps_rc_ctxt->rc_hdl, |
1497 | 109k | rc_pic_type, |
1498 | 109k | ((3 * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 1), |
1499 | 109k | ps_rc_ctxt->ai4_lap_f_sim[0], |
1500 | 109k | ps_rc_ctxt->af_sum_weigh[0], |
1501 | 109k | 0); |
1502 | | |
1503 | 109k | *pi4_scd_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3( |
1504 | 109k | i4_frame_qp << QSCALE_Q_FAC_3, ps_rc_ctxt->ps_rc_quant_ctxt); |
1505 | 109k | *pi4_scd_qp = *pi4_scd_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
1506 | 109k | if(*pi4_scd_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp) |
1507 | 0 | *pi4_scd_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp; |
1508 | 109k | } |
1509 | 118k | else |
1510 | 118k | { |
1511 | | /*scene cut qp is only valid when queried for I_PIC*/ |
1512 | 118k | *pi4_scd_qp = i4_hevc_frame_qp; |
1513 | 118k | } |
1514 | 228k | } |
1515 | 228k | } |
1516 | | |
1517 | 228k | ASSERT(i4_hevc_frame_qp >= (-ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset)); |
1518 | | |
1519 | | /*constraint qp swing based on neighbour frames*/ |
1520 | 228k | if(is_first_frame_coded(ps_rc_ctxt->rc_hdl)) |
1521 | 208k | { |
1522 | 208k | if(ps_rc_ctxt->i4_field_pic == 0) |
1523 | 208k | { |
1524 | 208k | if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) && |
1525 | 208k | i4_hevc_frame_qp > |
1526 | 19.9k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1527 | 19.9k | [rc_pic_type - 1] + |
1528 | 19.9k | 3) |
1529 | 390 | { |
1530 | | /*allow max of +3 compared to previous frame*/ |
1531 | 390 | i4_hevc_frame_qp = |
1532 | 390 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1533 | 390 | [rc_pic_type - 1] + |
1534 | 390 | 3; |
1535 | 390 | } |
1536 | 208k | if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) && |
1537 | 208k | i4_hevc_frame_qp < |
1538 | 19.9k | (ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1539 | 19.9k | [rc_pic_type - 1])) |
1540 | 19.1k | { |
1541 | 19.1k | i4_hevc_frame_qp = |
1542 | 19.1k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1543 | 19.1k | [rc_pic_type - 1]; |
1544 | 19.1k | } |
1545 | | |
1546 | | /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/ |
1547 | 208k | if(i4_temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr && |
1548 | 208k | ps_rc_ctxt->i4_max_temporal_lyr > 1) |
1549 | 9.97k | { |
1550 | 9.97k | i4_hevc_frame_qp = |
1551 | 9.97k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1552 | 9.97k | [rc_pic_type - 1] + |
1553 | 9.97k | 1; |
1554 | 9.97k | } |
1555 | 208k | } |
1556 | 0 | else /*for field case*/ |
1557 | 0 | { |
1558 | 0 | if(i4_temporal_layer_id >= 1) |
1559 | 0 | { |
1560 | | /*To make the comparison of qp with the top field's of previous layer tempor layer id matches with the pic type. */ |
1561 | 0 | if(i4_hevc_frame_qp > |
1562 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1563 | 0 | [i4_temporal_layer_id] + |
1564 | 0 | 3) |
1565 | 0 | { |
1566 | | /*allow max of +3 compared to previous frame*/ |
1567 | 0 | i4_hevc_frame_qp = |
1568 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1569 | 0 | [i4_temporal_layer_id] + |
1570 | 0 | 3; |
1571 | 0 | } |
1572 | 0 | if(i4_hevc_frame_qp < |
1573 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1574 | 0 | [i4_temporal_layer_id]) |
1575 | 0 | { |
1576 | 0 | i4_hevc_frame_qp = |
1577 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1578 | 0 | [i4_temporal_layer_id]; |
1579 | 0 | } |
1580 | | /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/ |
1581 | 0 | if(i4_temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr && |
1582 | 0 | ps_rc_ctxt->i4_max_temporal_lyr > 1) |
1583 | 0 | { |
1584 | 0 | i4_hevc_frame_qp = |
1585 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[ps_rc_ctxt->i4_scene_num_latest] |
1586 | 0 | [i4_temporal_layer_id] + |
1587 | 0 | 1; |
1588 | 0 | } |
1589 | 0 | } |
1590 | 0 | } |
1591 | 208k | } |
1592 | | |
1593 | | #if USE_USER_FIRST_FRAME_QP |
1594 | | /*I_PIC check is necessary coz pre-enc can query for qp even before first frame update has happened*/ |
1595 | | if(!ps_rc_ctxt->i4_is_first_frame_encoded && rc_pic_type == I_PIC) |
1596 | | { |
1597 | | i4_hevc_frame_qp = ps_rc_ctxt->i4_init_frame_qp_user; |
1598 | | DBG_PRINTF("FIXED START QP PATH *************************\n"); |
1599 | | } |
1600 | | #endif |
1601 | | /**clip to min qp which is user configurable*/ |
1602 | 228k | i4_hevc_frame_qp = |
1603 | 228k | ihevce_clip_min_max_qp(ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, i4_temporal_layer_id); |
1604 | | |
1605 | 228k | return i4_hevc_frame_qp; |
1606 | 228k | } |
1607 | 228k | } |
1608 | | /** |
1609 | | ****************************************************************************** |
1610 | | * |
1611 | | * @name ihevce_rc_cal_pre_enc_qp |
1612 | | * |
1613 | | * @par Description - Called from enc_loop_init. updates frame qp which will |
1614 | | be used by next frame of same pic type in pre-enc stage |
1615 | | * |
1616 | | * @param[in] ps_rc_ctxt - pointer to rc context |
1617 | | * @return void |
1618 | | * |
1619 | | ****************************************************************************** |
1620 | | */ |
1621 | | void ihevce_rc_cal_pre_enc_qp(void *pv_rc_ctxt) |
1622 | 147k | { |
1623 | 147k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
1624 | 147k | WORD32 i, i4_frame_qp, i4_scd_qp; |
1625 | 147k | WORD32 i4_delay_l0_enc = 0; |
1626 | | |
1627 | 147k | i4_delay_l0_enc = ps_rc_ctxt->i4_pre_enc_rc_delay; |
1628 | | |
1629 | 147k | if(ps_rc_ctxt->e_rate_control_type != CONST_QP) |
1630 | 109k | { |
1631 | | //DBG_PRINTF("\ncheck query read = %d write = %d",ps_rc_ctxt->i4_pre_enc_qp_read_index,ps_rc_ctxt->i4_pre_enc_qp_write_index); |
1632 | 109k | #if DETERMINISTIC_RC |
1633 | 109k | ASSERT( |
1634 | 109k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_is_qp_valid == |
1635 | 109k | -1); |
1636 | 109k | #endif |
1637 | 337k | for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
1638 | 228k | { |
1639 | 228k | i4_frame_qp = |
1640 | 228k | ihevce_rc_get_pre_enc_pic_quant(ps_rc_ctxt, (picture_type_e)i, &i4_scd_qp); |
1641 | | |
1642 | 228k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].ai4_quant[i] = |
1643 | 228k | i4_frame_qp; |
1644 | | /*returns valid scene cut qp only when queried as I_PIC*/ |
1645 | 228k | if(i == 0) |
1646 | 109k | { |
1647 | 109k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_scd_qp = |
1648 | 109k | i4_scd_qp; |
1649 | 109k | } |
1650 | | |
1651 | 228k | if(ps_rc_ctxt->i4_field_pic && i > 0) |
1652 | 0 | { |
1653 | 0 | i4_frame_qp = ihevce_rc_get_pre_enc_pic_quant( |
1654 | 0 | ps_rc_ctxt, (picture_type_e)(i + FIELD_OFFSET), &i4_scd_qp); |
1655 | |
|
1656 | 0 | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index] |
1657 | 0 | .ai4_quant[i + FIELD_OFFSET] = i4_frame_qp; |
1658 | 0 | } |
1659 | 228k | } |
1660 | | /*mark index as populated*/ |
1661 | 109k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_write_index].i4_is_qp_valid = 1; |
1662 | | |
1663 | 109k | ps_rc_ctxt->i4_pre_enc_qp_write_index = |
1664 | 109k | (ps_rc_ctxt->i4_pre_enc_qp_write_index + 1) % i4_delay_l0_enc; |
1665 | 109k | } |
1666 | 147k | } |
1667 | | /** |
1668 | | ****************************************************************************** |
1669 | | * |
1670 | | * @brief function to get updated qp after L1 analysis for L0. ' |
1671 | | * This uses estimated L0 satd based on L1 satd/act |
1672 | | * |
1673 | | * @par Description |
1674 | | * |
1675 | | * @param[in] pv_rc_ctxt |
1676 | | * void pointer to rc ctxt |
1677 | | * @param[in] rc_lap_out_params_t * |
1678 | | pointer to lap out structure |
1679 | | * @param[in] i8_est_L0_satd_act |
1680 | | * estimated L0 satd/act based on L1 satd/act |
1681 | | * @return void |
1682 | | * |
1683 | | ****************************************************************************** |
1684 | | */ |
1685 | | WORD32 ihevce_get_L0_est_satd_based_scd_qp( |
1686 | | void *pv_rc_ctxt, |
1687 | | rc_lap_out_params_t *ps_rc_lap_out, |
1688 | | LWORD64 i8_est_L0_satd_act, |
1689 | | float i_to_avg_rest_ratio) |
1690 | 124k | { |
1691 | 124k | rc_context_t *ps_ctxt = (rc_context_t *)pv_rc_ctxt; |
1692 | 124k | WORD32 i4_frame_qs_q3, i4_hevc_qp, i4_est_header_bits, index, i, i4_total_bits; |
1693 | 124k | picture_type_e rc_pic_type; |
1694 | | |
1695 | 124k | rc_pic_type = ihevce_rc_conv_pic_type( |
1696 | 124k | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
1697 | 124k | ps_ctxt->i4_field_pic, |
1698 | 124k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
1699 | 124k | ps_rc_lap_out->i4_is_bottom_field, |
1700 | 124k | ps_ctxt->i4_top_field_first); |
1701 | | |
1702 | | /*initialise the coeffs to 1 in case lap is not used */ |
1703 | | |
1704 | 1.24M | for(i = 0; i < MAX_PIC_TYPE; i++) |
1705 | 1.12M | { |
1706 | 1.12M | ps_ctxt->af_sum_weigh[0][i][0] = 1.0; |
1707 | 1.12M | ps_ctxt->af_sum_weigh[0][i][1] = 0.0; |
1708 | 1.12M | } |
1709 | | |
1710 | | /*get bits to find estimate of header bits*/ |
1711 | 124k | i4_est_header_bits = rc_get_scene_change_est_header_bits( |
1712 | 124k | ps_ctxt->rc_hdl, |
1713 | 124k | ps_rc_lap_out->i4_num_pels_in_frame_considered, |
1714 | 124k | ps_ctxt->ai4_lap_f_sim[0], |
1715 | 124k | ps_ctxt->af_sum_weigh[0], |
1716 | 124k | i_to_avg_rest_ratio); |
1717 | | |
1718 | 124k | index = ihevce_get_offline_index(ps_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered); |
1719 | 124k | { |
1720 | 124k | WORD32 i4_true_scd = 0; |
1721 | 124k | WORD32 i4_curr_bits_estimated; |
1722 | | |
1723 | 124k | i4_frame_qs_q3 = rc_get_qp_for_scd_frame( |
1724 | 124k | ps_ctxt->rc_hdl, |
1725 | 124k | I_PIC, |
1726 | 124k | i8_est_L0_satd_act, |
1727 | 124k | ps_rc_lap_out->i4_num_pels_in_frame_considered, |
1728 | 124k | i4_est_header_bits, |
1729 | 124k | ps_ctxt->ai4_lap_f_sim[0], |
1730 | 124k | (void *)&g_offline_i_model_coeff[index][0], |
1731 | 124k | i_to_avg_rest_ratio, |
1732 | 124k | i4_true_scd, |
1733 | 124k | ps_ctxt->af_sum_weigh[0], |
1734 | 124k | ps_rc_lap_out->ps_frame_info, |
1735 | 124k | ps_ctxt->i4_rc_pass, |
1736 | 124k | 0, |
1737 | 124k | 0, |
1738 | 124k | 0, |
1739 | 124k | &i4_total_bits, |
1740 | 124k | &i4_curr_bits_estimated, |
1741 | 124k | ps_rc_lap_out->i4_use_offline_model_2pass, |
1742 | 124k | 0, |
1743 | 124k | 0, |
1744 | 124k | -1, |
1745 | 124k | NULL); |
1746 | 124k | } |
1747 | 124k | i4_hevc_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3(i4_frame_qs_q3, ps_ctxt->ps_rc_quant_ctxt); |
1748 | 124k | i4_hevc_qp = i4_hevc_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
1749 | | |
1750 | 124k | if(i4_hevc_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qp) |
1751 | 0 | i4_hevc_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qp; |
1752 | | |
1753 | 124k | if(i4_hevc_qp < (SCD_MIN_HEVC_QP - |
1754 | 124k | ps_ctxt->ps_rc_quant_ctxt |
1755 | 124k | ->i1_qp_offset)) // since outside RC the QP range is -12 to 51 for 10 bit |
1756 | 122k | { |
1757 | 122k | i4_hevc_qp = (SCD_MIN_HEVC_QP - ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset); |
1758 | 122k | } |
1759 | 2.13k | else if(i4_hevc_qp > SCD_MAX_HEVC_QP) |
1760 | 0 | { |
1761 | 0 | i4_hevc_qp = SCD_MAX_HEVC_QP; |
1762 | 0 | } |
1763 | | /*this is done outside loop*/ |
1764 | | |
1765 | 124k | return i4_hevc_qp; |
1766 | 124k | } |
1767 | | /** |
1768 | | ****************************************************************************** |
1769 | | * |
1770 | | * @name ihevce_rc_pre_enc_qp_query |
1771 | | * |
1772 | | * @par Description - Called from pre enc thrd for getting the qp of non scd |
1773 | | frames. updates frame qp from reverse queue from enc loop |
1774 | | when its available |
1775 | | * |
1776 | | * @param[in] ps_rc_ctxt - pointer to rc context |
1777 | | * @param[in] i4_update_delay : The Delay in the update. This can happen for dist. case! |
1778 | | * All decision should consider this delay for updation! |
1779 | | * |
1780 | | * @return void |
1781 | | * |
1782 | | ****************************************************************************** |
1783 | | */ |
1784 | | |
1785 | | WORD32 ihevce_rc_pre_enc_qp_query( |
1786 | | void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 i4_update_delay) |
1787 | 197k | { |
1788 | 197k | WORD32 scene_type, i4_is_scd = 0, i4_frame_qp, slice_type = ISLICE; |
1789 | 197k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
1790 | 197k | rc_type_e e_rc_type = ps_rc_ctxt->e_rate_control_type; |
1791 | 197k | IV_PICTURE_CODING_TYPE_T pic_type = (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type; |
1792 | 197k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
1793 | 197k | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
1794 | 197k | ps_rc_ctxt->i4_field_pic, |
1795 | 197k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
1796 | 197k | ps_rc_lap_out->i4_is_bottom_field, |
1797 | 197k | ps_rc_ctxt->i4_top_field_first); |
1798 | 197k | WORD32 i4_use_offset_flag = 0, k = 0; |
1799 | 197k | WORD32 i4_inter_frame_interval = rc_get_inter_frame_interval(ps_rc_ctxt->rc_hdl); |
1800 | 197k | WORD32 ai4_offsets[5] = { 0, 1, 2, 3, 4 }; |
1801 | 197k | rc_lap_out_params_t *ps_rc_lap_out_temp = ps_rc_lap_out; |
1802 | | |
1803 | | /* The window for which your update is guaranteed */ |
1804 | 197k | WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay; |
1805 | | |
1806 | 197k | k = 0; |
1807 | 197k | if((updated_window >= i4_inter_frame_interval) && (ps_rc_ctxt->i4_rc_pass != 2) && |
1808 | 197k | ((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC))) |
1809 | 0 | { |
1810 | 0 | WORD32 i4_count = 0; |
1811 | |
|
1812 | 0 | for(i4_count = 0; i4_count < updated_window; i4_count++) |
1813 | 0 | { |
1814 | 0 | picture_type_e rc_pic_type_temp = ihevce_rc_conv_pic_type( |
1815 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out_temp->i4_rc_pic_type, |
1816 | 0 | ps_rc_ctxt->i4_field_pic, |
1817 | 0 | ps_rc_lap_out_temp->i4_rc_temporal_lyr_id, |
1818 | 0 | ps_rc_lap_out_temp->i4_is_bottom_field, |
1819 | 0 | ps_rc_ctxt->i4_top_field_first); |
1820 | |
|
1821 | 0 | if((rc_pic_type_temp == I_PIC) || (rc_pic_type_temp == P_PIC)) |
1822 | 0 | ihevce_compute_temporal_complexity_reset_Kp_Kb(ps_rc_lap_out_temp, pv_rc_ctxt, 0); |
1823 | |
|
1824 | 0 | ps_rc_lap_out_temp = |
1825 | 0 | (rc_lap_out_params_t *)ps_rc_lap_out_temp->ps_rc_lap_out_next_encode; |
1826 | |
|
1827 | 0 | if(ps_rc_lap_out_temp == NULL) |
1828 | 0 | break; |
1829 | 0 | } |
1830 | 0 | } |
1831 | | |
1832 | 197k | if(updated_window >= i4_inter_frame_interval) |
1833 | 0 | { |
1834 | 0 | i4_use_offset_flag = 1; |
1835 | 0 | memmove(ai4_offsets, ps_rc_lap_out->ai4_offsets, sizeof(WORD32) * 5); |
1836 | 0 | } |
1837 | | |
1838 | 197k | if(CONST_QP == e_rc_type) |
1839 | 83.3k | { |
1840 | 83.3k | switch(pic_type) |
1841 | 83.3k | { |
1842 | 3.87k | case IV_I_FRAME: |
1843 | 17.8k | case IV_IDR_FRAME: |
1844 | 17.8k | { |
1845 | 17.8k | slice_type = ISLICE; |
1846 | 17.8k | break; |
1847 | 3.87k | } |
1848 | 58.0k | case IV_P_FRAME: |
1849 | 58.0k | { |
1850 | 58.0k | slice_type = PSLICE; |
1851 | 58.0k | break; |
1852 | 3.87k | } |
1853 | 7.46k | case IV_B_FRAME: |
1854 | 7.46k | { |
1855 | 7.46k | slice_type = BSLICE; |
1856 | 7.46k | break; |
1857 | 3.87k | } |
1858 | 0 | default: |
1859 | 0 | { |
1860 | 0 | DBG_PRINTF("Invalid picture type %d\n", pic_type); |
1861 | 0 | break; |
1862 | 3.87k | } |
1863 | 83.3k | } |
1864 | | |
1865 | 83.3k | i4_frame_qp = ihevce_get_cur_frame_qp( |
1866 | 83.3k | ps_rc_ctxt->i4_init_frame_qp_user, |
1867 | 83.3k | slice_type, |
1868 | 83.3k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
1869 | 83.3k | ps_rc_ctxt->i4_min_frame_qp, |
1870 | 83.3k | ps_rc_ctxt->i4_max_frame_qp, |
1871 | 83.3k | ps_rc_ctxt->ps_rc_quant_ctxt); |
1872 | | |
1873 | 83.3k | return i4_frame_qp; |
1874 | 83.3k | } |
1875 | 114k | else |
1876 | 114k | { |
1877 | | /*check scene type*/ |
1878 | 114k | scene_type = ihevce_rc_lap_get_scene_type(ps_rc_lap_out); |
1879 | | |
1880 | 114k | if(scene_type == SCENE_TYPE_SCENE_CUT) |
1881 | 0 | { |
1882 | 0 | i4_is_scd = 1; |
1883 | 0 | ps_rc_ctxt->i4_num_frms_from_reset = 0; |
1884 | 0 | #if USE_QP_OFFSET_POST_SCD |
1885 | 0 | ps_rc_ctxt->i4_use_qp_offset_pre_enc = 1; |
1886 | | #else |
1887 | | ps_rc_ctxt->i4_use_qp_offset_pre_enc = 0; |
1888 | | #endif |
1889 | 0 | } |
1890 | 114k | ASSERT( |
1891 | 114k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid == |
1892 | 114k | 1 || |
1893 | 114k | ps_rc_lap_out->i4_rc_poc < 20); |
1894 | | |
1895 | 114k | if(ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid == |
1896 | 114k | 1) |
1897 | 114k | { |
1898 | 114k | if(i4_is_scd || ps_rc_ctxt->i4_use_qp_offset_pre_enc) |
1899 | 11.6k | { |
1900 | 11.6k | #if 1 //The qp will be populated assuming the frame is I_PIC. Adjust according to current pic type |
1901 | 11.6k | i4_frame_qp = |
1902 | 11.6k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_scd_qp; |
1903 | 11.6k | if(rc_pic_type == P_PIC) |
1904 | 5.05k | i4_frame_qp++; |
1905 | 6.61k | else |
1906 | 6.61k | i4_frame_qp = i4_frame_qp + ps_rc_lap_out->i4_rc_temporal_lyr_id; |
1907 | 11.6k | #endif |
1908 | 11.6k | if(i4_use_offset_flag) |
1909 | 0 | { |
1910 | 0 | if(rc_pic_type > B2_PIC) |
1911 | 0 | i4_frame_qp = ps_rc_ctxt->i4_L0_frame_qp + ai4_offsets[rc_pic_type - 4]; |
1912 | 0 | else |
1913 | 0 | i4_frame_qp = ps_rc_ctxt->i4_L0_frame_qp + ai4_offsets[rc_pic_type]; |
1914 | 0 | } |
1915 | 11.6k | } |
1916 | 102k | else |
1917 | 102k | { |
1918 | 102k | #if DETERMINISTIC_RC |
1919 | 102k | i4_frame_qp = ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index] |
1920 | 102k | .ai4_quant[rc_pic_type]; |
1921 | | #else |
1922 | | /*read the latest qp updated by enc*/ |
1923 | | i4_frame_qp = |
1924 | | ps_rc_ctxt |
1925 | | ->as_pre_enc_qp_queue |
1926 | | [(ps_rc_ctxt->i4_pre_enc_qp_write_index + MAX_PRE_ENC_RC_DELAY - 1) % |
1927 | | MAX_PRE_ENC_RC_DELAY] |
1928 | | .ai4_quant[rc_pic_type]; |
1929 | | #endif |
1930 | 102k | } |
1931 | | |
1932 | 114k | ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index].i4_is_qp_valid = |
1933 | 114k | -1; |
1934 | | /*once encoder starts reading from qp queue it should always read from qp queue*/ |
1935 | | //ps_rc_ctxt->i4_use_init_qp_for_pre_enc = 0; |
1936 | 114k | } |
1937 | 0 | else |
1938 | 0 | { |
1939 | 0 | i4_frame_qp = ps_rc_ctxt->ai4_init_pre_enc_qp[rc_pic_type]; |
1940 | 0 | } |
1941 | 114k | { |
1942 | 114k | WORD32 i4_delay_l0_enc = ps_rc_ctxt->i4_pre_enc_rc_delay; |
1943 | 114k | ps_rc_ctxt->i4_pre_enc_qp_read_index = |
1944 | 114k | (ps_rc_ctxt->i4_pre_enc_qp_read_index + 1) % i4_delay_l0_enc; |
1945 | | |
1946 | 114k | if(ps_rc_ctxt->i4_num_frms_from_reset < i4_delay_l0_enc) |
1947 | 11.6k | { |
1948 | 11.6k | ps_rc_ctxt->i4_num_frms_from_reset++; |
1949 | 11.6k | if(ps_rc_ctxt->i4_num_frms_from_reset >= i4_delay_l0_enc) |
1950 | 2.97k | ps_rc_ctxt->i4_use_qp_offset_pre_enc = 0; |
1951 | 11.6k | } |
1952 | 114k | } |
1953 | | |
1954 | 114k | i4_frame_qp = CLIP3(i4_frame_qp, ps_rc_ctxt->i4_min_frame_qp, ps_rc_ctxt->i4_max_frame_qp); |
1955 | 114k | return i4_frame_qp; |
1956 | 114k | } |
1957 | 197k | } |
1958 | | /** |
1959 | | ****************************************************************************** |
1960 | | * |
1961 | | * @brief function to estimate L0 satd based on L1 satd. ' |
1962 | | * |
1963 | | * |
1964 | | * @par Description |
1965 | | * |
1966 | | * @param[in] pv_rc_ctxt |
1967 | | * void pointer to rc ctxt |
1968 | | * @param[in] rc_lap_out_params_t * |
1969 | | pointer to lap out structure |
1970 | | * @param[in] i8_est_L0_satd_act |
1971 | | * estimated L0 satd/act based on L1 satd/act |
1972 | | * @return void |
1973 | | * |
1974 | | ****************************************************************************** |
1975 | | */ |
1976 | | LWORD64 ihevce_get_L0_satd_based_on_L1( |
1977 | | LWORD64 i8_satd_by_act_L1, WORD32 i4_num_pixel, WORD32 i4_cur_q_scale) |
1978 | 270k | { |
1979 | 270k | LWORD64 est_L0_satd_by_act; |
1980 | 270k | float m, c; |
1981 | | /** choose coeff based on resolution*/ |
1982 | 270k | if(i4_num_pixel > 5184000) |
1983 | 0 | { |
1984 | 0 | m = (float)2.3911; |
1985 | 0 | c = (float)86329; |
1986 | 0 | } |
1987 | 270k | else if(i4_num_pixel > 1497600) |
1988 | 25 | { |
1989 | 25 | m = (float)2.7311; |
1990 | 25 | c = (float)-1218.9; |
1991 | 25 | } |
1992 | 270k | else if(i4_num_pixel > 633600) |
1993 | 230 | { |
1994 | 230 | m = (float)3.1454; |
1995 | 230 | c = (float)-5836.1; |
1996 | 230 | } |
1997 | 270k | else |
1998 | 270k | { |
1999 | 270k | m = (float)3.5311; |
2000 | 270k | c = (float)-2377.2; |
2001 | 270k | } |
2002 | | /*due to qp difference between I and P, For P pic for same */ |
2003 | 270k | est_L0_satd_by_act = (LWORD64)(i8_satd_by_act_L1 / i4_cur_q_scale * m + c) * i4_cur_q_scale; |
2004 | | |
2005 | 270k | { |
2006 | 270k | if(est_L0_satd_by_act < (i4_num_pixel >> 3)) |
2007 | 198k | est_L0_satd_by_act = (i4_num_pixel >> 3); |
2008 | 270k | } |
2009 | 270k | return est_L0_satd_by_act; |
2010 | 270k | } |
2011 | | /** |
2012 | | ****************************************************************************** |
2013 | | * |
2014 | | * @name ihevce_rc_register_L1_analysis_data |
2015 | | * |
2016 | | * @par Description |
2017 | | * |
2018 | | * @param[in] ps_rc_ctxt - pointer to rc context |
2019 | | * ps_rc_lap_out |
2020 | | * i8_est_L0_satd_by_act |
2021 | | * i8_pre_intra_sad |
2022 | | * i8_l1_hme_sad |
2023 | | * @return void |
2024 | | * |
2025 | | ****************************************************************************** |
2026 | | */ |
2027 | | void ihevce_rc_register_L1_analysis_data( |
2028 | | void *pv_rc_ctxt, |
2029 | | rc_lap_out_params_t *ps_rc_lap_out, |
2030 | | LWORD64 i8_est_L0_satd_by_act, |
2031 | | LWORD64 i8_pre_intra_sad, |
2032 | | LWORD64 i8_l1_hme_sad) |
2033 | 156k | { |
2034 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
2035 | 156k | WORD32 i, data_available = 1; |
2036 | 156k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
2037 | 156k | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
2038 | 156k | ps_rc_ctxt->i4_field_pic, |
2039 | 156k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
2040 | 156k | ps_rc_lap_out->i4_is_bottom_field, |
2041 | 156k | ps_rc_ctxt->i4_top_field_first); |
2042 | | |
2043 | | //if( ps_rc_ctxt->u4_rc_scene_num_est_L0_intra_sad_available == ps_rc_lap_out->u4_rc_scene_num) |
2044 | 156k | { |
2045 | | /*update current frame's data*/ |
2046 | 156k | ps_rc_ctxt->ai8_prev_frame_est_L0_satd[rc_pic_type] = i8_est_L0_satd_by_act; |
2047 | 156k | ps_rc_ctxt->ai8_prev_frame_hme_sad[rc_pic_type] = i8_l1_hme_sad; |
2048 | 156k | ps_rc_ctxt->ai8_prev_frame_pre_intra_sad[rc_pic_type] = i8_pre_intra_sad; |
2049 | 156k | } |
2050 | | /*check if data is available for all picture type*/ |
2051 | 156k | if(!ps_rc_ctxt->i4_is_est_L0_intra_sad_available) |
2052 | 17.6k | { |
2053 | 65.1k | for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
2054 | 47.4k | { |
2055 | 47.4k | data_available &= (ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i] >= 0); |
2056 | 47.4k | if(ps_rc_ctxt->i4_field_pic == 1 && i != 0) |
2057 | 0 | data_available &= (ps_rc_ctxt->ai8_prev_frame_est_L0_satd[i + FIELD_OFFSET] >= 0); |
2058 | 47.4k | } |
2059 | 17.6k | ps_rc_ctxt->i4_is_est_L0_intra_sad_available = data_available; |
2060 | 17.6k | } |
2061 | 156k | } |
2062 | | |
2063 | | /*#######################################################*/ |
2064 | | /******* END OF PRE-ENC QP QUERY FUNCTIONS **************/ |
2065 | | /*#####################################################*/ |
2066 | | |
2067 | | /*##########################################################*/ |
2068 | | /******* START OF ENC THRD QP QUERY FUNCTIONS **************/ |
2069 | | /*########################################################*/ |
2070 | | |
2071 | | /** |
2072 | | ****************************************************************************** |
2073 | | * |
2074 | | * @brief function to get ihevce_rc_get_pic_quant |
2075 | | * |
2076 | | * @par Description |
2077 | | * @param[in] i4_update_delay : The Delay in the update. This can happen for dist. case! |
2078 | | * All decision should consider this delay for updation! |
2079 | | ****************************************************************************** |
2080 | | */ |
2081 | | |
2082 | | WORD32 ihevce_rc_get_pic_quant( |
2083 | | void *pv_ctxt, |
2084 | | rc_lap_out_params_t *ps_rc_lap_out, |
2085 | | IHEVCE_RC_CALL_TYPE call_type, |
2086 | | WORD32 i4_enc_frm_id, |
2087 | | WORD32 i4_update_delay, |
2088 | | WORD32 *pi4_tot_bits_estimated) |
2089 | 156k | { |
2090 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
2091 | 156k | WORD32 i4_frame_qp, i4_frame_qp_q6, i4_hevc_frame_qp = -1, i4_deltaQP = 0; |
2092 | 156k | WORD32 i4_max_frame_bits = (1 << 30); |
2093 | 156k | rc_type_e e_rc_type = ps_rc_ctxt->e_rate_control_type; |
2094 | 156k | WORD32 slice_type = ISLICE, index, i4_num_frames_in_cur_gop, i4_cur_est_texture_bits; |
2095 | 156k | WORD32 temporal_layer_id = ps_rc_lap_out->i4_rc_temporal_lyr_id; |
2096 | 156k | IV_PICTURE_CODING_TYPE_T pic_type = (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type; |
2097 | 156k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
2098 | 156k | pic_type, |
2099 | 156k | ps_rc_ctxt->i4_field_pic, |
2100 | 156k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
2101 | 156k | ps_rc_lap_out->i4_is_bottom_field, |
2102 | 156k | ps_rc_ctxt->i4_top_field_first); |
2103 | 156k | float i_to_avg_bit_ratio; |
2104 | 156k | frame_info_t s_frame_info_temp; |
2105 | 156k | WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM; |
2106 | 156k | WORD32 i4_vbv_buf_max_bits; |
2107 | 156k | WORD32 i4_est_tex_bits; |
2108 | 156k | WORD32 i4_cur_est_header_bits, i4_fade_scene; |
2109 | 156k | WORD32 i4_model_available, i4_is_no_model_scd; |
2110 | 156k | WORD32 i4_estimate_to_calc_frm_error; |
2111 | | |
2112 | | /* The window for which your update is guaranteed */ |
2113 | 156k | WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay; |
2114 | | |
2115 | 156k | ps_rc_ctxt->i4_scene_num_latest = i4_scene_num; |
2116 | | |
2117 | 156k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP = INVALID_QP; |
2118 | 156k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = INVALID_QP; |
2119 | 156k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP = INVALID_QP; |
2120 | | |
2121 | 156k | ps_rc_ctxt->i4_quality_preset = ps_rc_lap_out->i4_rc_quality_preset; |
2122 | 156k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = INVALID_QP; |
2123 | | |
2124 | 156k | if(1 == ps_rc_ctxt->i4_bitrate_changed) |
2125 | 5.62k | { |
2126 | 5.62k | ps_rc_ctxt->i4_bitrate_changed = 0; |
2127 | 5.62k | } |
2128 | 156k | if(CONST_QP == e_rc_type) |
2129 | 41.6k | { |
2130 | 41.6k | switch(pic_type) |
2131 | 41.6k | { |
2132 | 1.93k | case IV_I_FRAME: |
2133 | 8.93k | case IV_IDR_FRAME: |
2134 | 8.93k | { |
2135 | 8.93k | slice_type = ISLICE; |
2136 | 8.93k | break; |
2137 | 1.93k | } |
2138 | 29.0k | case IV_P_FRAME: |
2139 | 29.0k | { |
2140 | 29.0k | slice_type = PSLICE; |
2141 | 29.0k | break; |
2142 | 1.93k | } |
2143 | 3.73k | case IV_B_FRAME: |
2144 | 3.73k | { |
2145 | 3.73k | slice_type = BSLICE; |
2146 | 3.73k | break; |
2147 | 1.93k | } |
2148 | 0 | default: |
2149 | 0 | { |
2150 | 0 | DBG_PRINTF("Invalid picture type %d\n", pic_type); |
2151 | 0 | break; |
2152 | 1.93k | } |
2153 | 41.6k | } |
2154 | | |
2155 | 41.6k | i4_frame_qp = ihevce_get_cur_frame_qp( |
2156 | 41.6k | ps_rc_ctxt->i4_init_frame_qp_user, |
2157 | 41.6k | slice_type, |
2158 | 41.6k | temporal_layer_id, |
2159 | 41.6k | ps_rc_ctxt->i4_min_frame_qp, |
2160 | 41.6k | ps_rc_ctxt->i4_max_frame_qp, |
2161 | 41.6k | ps_rc_ctxt->ps_rc_quant_ctxt); |
2162 | 41.6k | return i4_frame_qp; |
2163 | 41.6k | } |
2164 | 114k | else |
2165 | 114k | { |
2166 | 114k | WORD32 is_scd_ref_frame = 0, i4_num_scd_in_lap_window = 0, num_frames_b4_scd = 0, |
2167 | 114k | scene_type = 0, i; |
2168 | | //ihevce_lap_output_params_t *ps_cur_rc_lap_out; |
2169 | | |
2170 | 114k | if(ps_rc_ctxt->ai4_scene_num_last_pic[rc_pic_type] != |
2171 | 114k | (WORD32)ps_rc_lap_out->u4_rc_scene_num) |
2172 | 0 | { |
2173 | 0 | rc_reset_pic_model(ps_rc_ctxt->rc_hdl, rc_pic_type); |
2174 | 0 | rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, rc_pic_type); |
2175 | 0 | } |
2176 | 114k | ps_rc_ctxt->ai4_scene_num_last_pic[rc_pic_type] = ps_rc_lap_out->u4_rc_scene_num; |
2177 | | |
2178 | 114k | if(call_type == ENC_GET_QP) |
2179 | 114k | { |
2180 | 114k | i4_model_available = model_availability(ps_rc_ctxt->rc_hdl, rc_pic_type); |
2181 | | |
2182 | 114k | ps_rc_lap_out->i8_est_text_bits = -1; |
2183 | 114k | } |
2184 | | |
2185 | 114k | if((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC) || (rc_pic_type == P1_PIC)) |
2186 | 105k | { |
2187 | 105k | ps_rc_ctxt->i4_cur_scene_num = ps_rc_lap_out->u4_rc_scene_num; |
2188 | 105k | } |
2189 | | |
2190 | 114k | { |
2191 | 114k | if(!(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME)) |
2192 | 72.6k | { |
2193 | 72.6k | ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id] = |
2194 | 72.6k | ps_rc_lap_out->i8_frame_acc_coarse_me_cost; |
2195 | 72.6k | } |
2196 | | /*check if frame is scene cut*/ |
2197 | | /* If scd do not query the model. obtain qp from offline data model*/ |
2198 | 114k | scene_type = ihevce_rc_lap_get_scene_type(ps_rc_lap_out); |
2199 | | |
2200 | 114k | if(ps_rc_ctxt->ai4_scene_numbers[ps_rc_lap_out->u4_rc_scene_num] == 0 && |
2201 | 114k | (scene_type != SCENE_TYPE_SCENE_CUT)) |
2202 | 5.13k | { |
2203 | 5.13k | scene_type = SCENE_TYPE_SCENE_CUT; |
2204 | 5.13k | } |
2205 | | |
2206 | 114k | if(ps_rc_ctxt->ai4_scene_numbers[ps_rc_lap_out->u4_rc_scene_num] > 0 && |
2207 | 114k | (scene_type == SCENE_TYPE_SCENE_CUT)) |
2208 | 0 | { |
2209 | 0 | scene_type = SCENE_TYPE_NORMAL; |
2210 | 0 | } |
2211 | 114k | if(scene_type == SCENE_TYPE_SCENE_CUT) |
2212 | 5.13k | { |
2213 | 5.13k | if((ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) && |
2214 | 5.13k | (rc_pic_type > P_PIC)) |
2215 | 0 | { |
2216 | 0 | is_scd_ref_frame = 0; |
2217 | 0 | } |
2218 | 5.13k | else |
2219 | 5.13k | { |
2220 | 5.13k | is_scd_ref_frame = 1; |
2221 | 5.13k | } |
2222 | 5.13k | } |
2223 | 109k | else if(scene_type == SCENE_TYPE_PAUSE_TO_RESUME) |
2224 | 0 | { |
2225 | | /*pause to resume flag will only be set in layer 0 frames( I and P pic)*/ |
2226 | | /*I PIC can handle this by detecting I_only SCD which is based on open loop SATD hence explicit handling for pause to resume is required only for P_PIC*/ |
2227 | |
|
2228 | 0 | if(ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) |
2229 | 0 | { |
2230 | 0 | if(call_type == ENC_GET_QP && rc_pic_type == P_PIC) |
2231 | 0 | { |
2232 | 0 | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 1; |
2233 | 0 | } |
2234 | 0 | } |
2235 | 0 | else |
2236 | 0 | { |
2237 | 0 | if(call_type == ENC_GET_QP && rc_pic_type != I_PIC) |
2238 | 0 | { |
2239 | 0 | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 1; |
2240 | 0 | } |
2241 | 0 | } |
2242 | 0 | } |
2243 | | |
2244 | 114k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] = |
2245 | 114k | ps_rc_lap_out->i4_is_cmplx_change_reset_model; |
2246 | 114k | ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id] = |
2247 | 114k | ps_rc_lap_out->i4_is_cmplx_change_reset_bits; |
2248 | | |
2249 | | /*initialise the coeffs to 1 in case lap is not used */ |
2250 | 1.14M | for(i = 0; i < MAX_PIC_TYPE; i++) |
2251 | 1.03M | { |
2252 | 1.03M | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][0] = 1.0; |
2253 | 1.03M | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][1] = 0.0; |
2254 | 1.03M | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][2] = 0.0; |
2255 | 1.03M | } |
2256 | | |
2257 | | /*treat even first frame as scd frame*/ |
2258 | 114k | if(!ps_rc_ctxt->i4_is_first_frame_encoded) |
2259 | 5.13k | { |
2260 | 5.13k | is_scd_ref_frame = 1; |
2261 | 5.13k | } |
2262 | | |
2263 | | /*special case SCD handling for Non-I pic*/ |
2264 | 114k | if(!(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) && call_type == ENC_GET_QP) |
2265 | 72.6k | { |
2266 | 72.6k | if(is_scd_ref_frame) |
2267 | 0 | { |
2268 | | /*A non-I pic will only be marked as scene cut only if there is another SCD follows within another subgop*/ |
2269 | 0 | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 1; |
2270 | 0 | } |
2271 | | /*check if current sad is very different from previous SAD and */ |
2272 | 72.6k | else if( |
2273 | 72.6k | !ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] && |
2274 | 72.6k | ps_rc_lap_out->i4_is_non_I_scd) |
2275 | 1.57k | { |
2276 | 1.57k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 1; |
2277 | 1.57k | is_scd_ref_frame = 1; |
2278 | 1.57k | } |
2279 | 72.6k | } |
2280 | | |
2281 | 114k | if(call_type == PRE_ENC_GET_QP) |
2282 | 0 | { |
2283 | | /*Only I frames are considered as scd pic during pre-enc*/ |
2284 | 0 | is_scd_ref_frame &= (pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME); |
2285 | 0 | } |
2286 | | |
2287 | | /*special case SCD handling for I pic*/ |
2288 | 114k | if((pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) && !is_scd_ref_frame) |
2289 | 36.8k | { |
2290 | | /*If open loop SATD's of two I picture are very different then treat the I pic as SCD and reset only model as this can |
2291 | | happen during fade-in and fade-out where other picture types would have learnt. Reset is required only for I.*/ |
2292 | | |
2293 | 36.8k | if(ps_rc_lap_out->i4_is_I_only_scd) |
2294 | 3.53k | { |
2295 | 3.53k | is_scd_ref_frame = 1; |
2296 | 3.53k | ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 1; |
2297 | 3.53k | } |
2298 | 36.8k | } |
2299 | | /*should be recalculated for every picture*/ |
2300 | 114k | if((updated_window) > 0 && (call_type == ENC_GET_QP) && (ps_rc_ctxt->i4_rc_pass != 2)) |
2301 | 0 | { |
2302 | 0 | rc_lap_out_params_t *ps_cur_rc_lap_out; |
2303 | |
|
2304 | 0 | UWORD32 u4_L1_based_lap_complexity_q7; |
2305 | 0 | WORD32 i = 0, k = 0, i4_f_sim = 0, i4_h_sim = 0, i4_var_sum = 0, |
2306 | 0 | i4_num_pic_metric_count = 0, i4_is_first_frm = 1, |
2307 | 0 | i4_intra_frame_interval = 0; |
2308 | 0 | LWORD64 i8_l1_analysis_lap_comp = 0; |
2309 | 0 | LWORD64 nor_frm_hme_sad_q10; |
2310 | 0 | picture_type_e curr_rc_pic_type; |
2311 | 0 | WORD32 ai4_pic_dist[MAX_PIC_TYPE] = { 0 }; |
2312 | 0 | LWORD64 i8_sad_first_frame_pic_type[MAX_PIC_TYPE] = { 0 }, |
2313 | 0 | i8_total_sad_pic_type[MAX_PIC_TYPE] = { 0 }; |
2314 | 0 | LWORD64 i8_last_frame_pic_type[MAX_PIC_TYPE] = { 0 }, i8_esti_consum_bits = 0; |
2315 | 0 | WORD32 i4_num_pic_type[MAX_PIC_TYPE] = { 0 }, i4_frames_in_lap_end = 0, |
2316 | 0 | i4_first_frame_coded_flag, i4_gop_end_flag = 1, i4_num_frame_for_ebf = 0; |
2317 | 0 | i4_first_frame_coded_flag = is_first_frame_coded(ps_rc_ctxt->rc_hdl); |
2318 | | |
2319 | | /*Setting the next scene cut as well as pic distribution for the gop*/ |
2320 | |
|
2321 | 0 | ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_rc_lap_out; |
2322 | 0 | i4_intra_frame_interval = rc_get_intra_frame_interval(ps_rc_ctxt->rc_hdl); |
2323 | | |
2324 | | /*Set the rc sc i next*/ |
2325 | 0 | if(ps_cur_rc_lap_out != NULL) |
2326 | 0 | { |
2327 | 0 | WORD32 i4_count = 0; |
2328 | 0 | do |
2329 | 0 | { |
2330 | 0 | if(((rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode == |
2331 | 0 | NULL)) //||((( (ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_pre_intra_sad == -1) || ( ((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_raw_pre_intra_sad == -1) ||( ((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_raw_l1_coarse_me_sad == -1) ||(((ihevce_lap_output_params_t*)ps_cur_rc_lap_out->ps_lap_out_next)->i8_frame_acc_coarse_me_sad == -1))) |
2332 | 0 | break; |
2333 | | |
2334 | 0 | ps_cur_rc_lap_out = |
2335 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode; |
2336 | 0 | i4_count++; |
2337 | |
|
2338 | 0 | } while((i4_count + 1) < updated_window); |
2339 | | |
2340 | 0 | rc_set_next_sc_i_in_rc_look_ahead( |
2341 | 0 | ps_rc_ctxt->rc_hdl, ps_cur_rc_lap_out->i4_next_sc_i_in_rc_look_ahead); |
2342 | 0 | rc_update_pic_distn_lap_to_rc( |
2343 | 0 | ps_rc_ctxt->rc_hdl, ps_cur_rc_lap_out->ai4_num_pic_type); |
2344 | |
|
2345 | 0 | ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead = |
2346 | 0 | ps_cur_rc_lap_out->i4_next_sc_i_in_rc_look_ahead; |
2347 | 0 | } |
2348 | | |
2349 | 0 | ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_rc_lap_out; |
2350 | 0 | if(ps_cur_rc_lap_out != NULL) |
2351 | 0 | { |
2352 | | /*initialise the coeffs to 1 in case lap is not used */ |
2353 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
2354 | 0 | { |
2355 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][0] = 0.0; |
2356 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][1] = 0.0; |
2357 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][i][2] = 0.0; |
2358 | 0 | } |
2359 | 0 | i = 0; |
2360 | 0 | k = 0; |
2361 | | |
2362 | | //ASSERT(ps_cur_rc_lap_out != NULL); |
2363 | 0 | do |
2364 | 0 | { |
2365 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
2366 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out->i4_rc_pic_type, |
2367 | 0 | ps_rc_ctxt->i4_field_pic, |
2368 | 0 | ps_cur_rc_lap_out->i4_rc_temporal_lyr_id, |
2369 | 0 | ps_cur_rc_lap_out->i4_is_bottom_field, |
2370 | 0 | ps_rc_ctxt->i4_top_field_first); |
2371 | 0 | if(ps_rc_ctxt->i4_is_first_frame_encoded || !i4_is_first_frm) |
2372 | 0 | { |
2373 | | /*Ignore first frame Fsim as it is not valid for first frame*/ |
2374 | 0 | i4_f_sim += ps_cur_rc_lap_out->s_pic_metrics.i4_fsim; |
2375 | 0 | i4_h_sim += ps_cur_rc_lap_out->s_pic_metrics.ai4_hsim[0]; |
2376 | 0 | i4_var_sum += (WORD32)ps_cur_rc_lap_out->s_pic_metrics.i8_8x8_var_lum; |
2377 | 0 | i4_num_pic_metric_count++; |
2378 | | //DBG_PRINTF("\n fsim = %d i = %d",ps_cur_rc_lap_out->s_pic_metrics.i4_fsim,i); |
2379 | | //ASSERT(ps_cur_rc_lap_out->s_pic_metrics.i4_fsim <= 128); |
2380 | 0 | } |
2381 | | |
2382 | | /*accumulate complexity from LAP2*/ |
2383 | 0 | if(curr_rc_pic_type == I_PIC) |
2384 | 0 | { |
2385 | 0 | i8_l1_analysis_lap_comp += |
2386 | 0 | (LWORD64)(1.17 * ps_cur_rc_lap_out->i8_raw_pre_intra_sad); |
2387 | 0 | } |
2388 | 0 | else |
2389 | 0 | { |
2390 | 0 | if(curr_rc_pic_type <= B2_PIC) |
2391 | 0 | i8_l1_analysis_lap_comp += (LWORD64)( |
2392 | 0 | (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad / |
2393 | 0 | pow(1.125f, curr_rc_pic_type)); |
2394 | 0 | else |
2395 | 0 | i8_l1_analysis_lap_comp += (LWORD64)( |
2396 | 0 | (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad / |
2397 | 0 | pow(1.125f, curr_rc_pic_type - B2_PIC)); |
2398 | 0 | } |
2399 | 0 | i++; |
2400 | 0 | i4_is_first_frm = 0; |
2401 | | |
2402 | | /*CAll the function for predictting the ebf and stuffing condition check*/ |
2403 | | /*rd model pass lapout l1 pass ebf return estimated ebf and signal*/ |
2404 | |
|
2405 | 0 | { |
2406 | 0 | if(i4_first_frame_coded_flag && (i4_gop_end_flag != 0)) |
2407 | 0 | { |
2408 | 0 | if(curr_rc_pic_type == 0) |
2409 | 0 | i4_gop_end_flag = 0; |
2410 | |
|
2411 | 0 | if(i4_gop_end_flag) |
2412 | 0 | { |
2413 | 0 | WORD32 prev_frm_cl_sad = |
2414 | 0 | rc_get_prev_frame_sad(ps_rc_ctxt->rc_hdl, curr_rc_pic_type); |
2415 | 0 | WORD32 cur_frm_est_cl_sad = (WORD32)( |
2416 | 0 | (ps_cur_rc_lap_out->i8_frame_acc_coarse_me_cost * |
2417 | 0 | prev_frm_cl_sad) / |
2418 | 0 | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[curr_rc_pic_type]); |
2419 | 0 | i8_esti_consum_bits += bit_alloc_get_estimated_bits_for_pic( |
2420 | 0 | ps_rc_ctxt->rc_hdl, |
2421 | 0 | cur_frm_est_cl_sad, |
2422 | 0 | prev_frm_cl_sad, |
2423 | 0 | curr_rc_pic_type); |
2424 | 0 | i4_num_frame_for_ebf++; |
2425 | 0 | } |
2426 | 0 | } |
2427 | 0 | } |
2428 | 0 | ps_cur_rc_lap_out = |
2429 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode; |
2430 | | /*The scene cut is lap window other than current frame is used to reduce bit alloc window for I pic*/ |
2431 | 0 | if(ps_cur_rc_lap_out != NULL && |
2432 | 0 | ps_cur_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT) |
2433 | 0 | { |
2434 | 0 | i4_num_scd_in_lap_window++; |
2435 | 0 | if(i4_num_scd_in_lap_window == 1) |
2436 | 0 | { |
2437 | | /*Note how many frames are parsed before first scd is hit*/ |
2438 | 0 | num_frames_b4_scd = i + 1; |
2439 | 0 | } |
2440 | 0 | } |
2441 | |
|
2442 | 0 | if((ps_cur_rc_lap_out == NULL || |
2443 | 0 | (i >= |
2444 | 0 | (updated_window - |
2445 | 0 | k)))) //||((( -1 == ps_cur_rc_lap_out->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out->i8_frame_acc_coarse_me_sad)))) |
2446 | 0 | break; |
2447 | 0 | if(0) //(( -1 == ps_cur_rc_lap_out->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out->i8_frame_acc_coarse_me_sad))) |
2448 | 0 | { |
2449 | 0 | k++; |
2450 | 0 | ps_cur_rc_lap_out = |
2451 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode; |
2452 | 0 | if(ps_cur_rc_lap_out == NULL) |
2453 | 0 | break; |
2454 | 0 | continue; |
2455 | 0 | } |
2456 | |
|
2457 | 0 | } while(1); |
2458 | 0 | ; |
2459 | 0 | } |
2460 | | /*For the first subgop we cant have underflow prevention logic |
2461 | | since once picture of each type is not encoded also happens for static contents thants high i_to avg_ratio */ |
2462 | 0 | if(i4_first_frame_coded_flag && |
2463 | 0 | (ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] > I_TO_REST_SLOW)) |
2464 | 0 | { |
2465 | 0 | if(!(i4_num_frame_for_ebf < ps_rc_ctxt->i4_max_inter_frm_int)) |
2466 | 0 | rc_bit_alloc_detect_ebf_stuff_scenario( |
2467 | 0 | ps_rc_ctxt->rc_hdl, |
2468 | 0 | i4_num_frame_for_ebf, |
2469 | 0 | i8_esti_consum_bits, |
2470 | 0 | ps_rc_ctxt->i4_max_inter_frm_int); |
2471 | 0 | } |
2472 | |
|
2473 | 0 | k = 0; |
2474 | |
|
2475 | 0 | i4_frames_in_lap_end = 0; |
2476 | 0 | { |
2477 | 0 | rc_lap_out_params_t *ps_cur_rc_lap_out1; |
2478 | |
|
2479 | 0 | ps_cur_rc_lap_out1 = (rc_lap_out_params_t *)ps_rc_lap_out; |
2480 | 0 | do |
2481 | 0 | { |
2482 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
2483 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out1->i4_rc_pic_type, |
2484 | 0 | ps_rc_ctxt->i4_field_pic, |
2485 | 0 | ps_cur_rc_lap_out1->i4_rc_temporal_lyr_id, |
2486 | 0 | ps_cur_rc_lap_out1->i4_is_bottom_field, |
2487 | 0 | ps_rc_ctxt->i4_top_field_first); |
2488 | | /*accumulate complexity from LAP2*/ |
2489 | |
|
2490 | 0 | if(curr_rc_pic_type == I_PIC) |
2491 | 0 | { |
2492 | 0 | i8_total_sad_pic_type[I_PIC] += |
2493 | 0 | ps_cur_rc_lap_out1->i8_raw_pre_intra_sad; |
2494 | 0 | i8_last_frame_pic_type[I_PIC] = |
2495 | 0 | ps_cur_rc_lap_out1->i8_raw_pre_intra_sad; |
2496 | 0 | } |
2497 | 0 | else |
2498 | 0 | { |
2499 | 0 | i8_total_sad_pic_type[curr_rc_pic_type] += |
2500 | 0 | ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad; |
2501 | 0 | i8_last_frame_pic_type[curr_rc_pic_type] = |
2502 | 0 | ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad; |
2503 | 0 | } |
2504 | 0 | if(i4_num_pic_type[curr_rc_pic_type] == 0) |
2505 | 0 | { |
2506 | 0 | if(curr_rc_pic_type == I_PIC) |
2507 | 0 | { |
2508 | 0 | i8_sad_first_frame_pic_type[I_PIC] = |
2509 | 0 | ps_cur_rc_lap_out1->i8_raw_pre_intra_sad; |
2510 | 0 | } |
2511 | 0 | else |
2512 | 0 | { |
2513 | 0 | i8_sad_first_frame_pic_type[curr_rc_pic_type] = |
2514 | 0 | ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad; |
2515 | 0 | } |
2516 | 0 | } |
2517 | 0 | i4_num_pic_type[curr_rc_pic_type]++; |
2518 | |
|
2519 | 0 | i4_frames_in_lap_end++; |
2520 | |
|
2521 | 0 | ps_cur_rc_lap_out1 = |
2522 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out1->ps_rc_lap_out_next_encode; |
2523 | 0 | if((ps_cur_rc_lap_out1 == NULL || |
2524 | 0 | (i4_frames_in_lap_end >= |
2525 | 0 | (updated_window - |
2526 | 0 | k)))) //||((( -1 == ps_cur_rc_lap_out1->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out1->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out1->i8_frame_acc_coarse_me_sad)))) |
2527 | 0 | { |
2528 | 0 | break; |
2529 | 0 | } |
2530 | 0 | if(0) //((( -1 == ps_cur_rc_lap_out1->i8_pre_intra_sad ) || ( -1 == ps_cur_rc_lap_out1->i8_raw_pre_intra_sad ) ||( -1 == ps_cur_rc_lap_out1->i8_raw_l1_coarse_me_sad) ||(-1 == ps_cur_rc_lap_out1->i8_frame_acc_coarse_me_sad)))) |
2531 | 0 | { |
2532 | 0 | k++; |
2533 | 0 | ps_cur_rc_lap_out1 = (rc_lap_out_params_t *) |
2534 | 0 | ps_cur_rc_lap_out1->ps_rc_lap_out_next_encode; |
2535 | 0 | if(ps_cur_rc_lap_out1 == NULL) |
2536 | 0 | break; |
2537 | 0 | continue; |
2538 | 0 | } |
2539 | |
|
2540 | 0 | } while(i4_frames_in_lap_end < (ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead - k)); |
2541 | 0 | } |
2542 | | |
2543 | | /*get picture type distribution in LAP*/ |
2544 | 0 | rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]); |
2545 | |
|
2546 | 0 | { |
2547 | 0 | float f_prev_comp; |
2548 | 0 | WORD32 j; |
2549 | 0 | float af_sum_weigh[MAX_PIC_TYPE], af_nume_weight[MAX_PIC_TYPE]; |
2550 | 0 | float af_average_sad_pic_type[MAX_PIC_TYPE] = { 0 }; |
2551 | 0 | for(j = 0; j < MAX_PIC_TYPE; j++) |
2552 | 0 | { |
2553 | 0 | if(i4_num_pic_type[j] > 0) |
2554 | 0 | { |
2555 | 0 | af_average_sad_pic_type[j] = |
2556 | 0 | (float)i8_total_sad_pic_type[j] / i4_num_pic_type[j]; |
2557 | 0 | } |
2558 | |
|
2559 | 0 | f_prev_comp = 1.; |
2560 | |
|
2561 | 0 | i4_num_pic_type[j] = (i4_num_pic_type[j] > ai4_pic_dist[j]) |
2562 | 0 | ? ai4_pic_dist[j] |
2563 | 0 | : i4_num_pic_type[j]; |
2564 | |
|
2565 | 0 | af_sum_weigh[j] = (float)i4_num_pic_type[j]; |
2566 | 0 | af_nume_weight[j] = 1.0; |
2567 | |
|
2568 | 0 | if(i4_num_pic_type[j] > 1 && (af_average_sad_pic_type[j] > 0)) |
2569 | 0 | { |
2570 | 0 | af_nume_weight[j] = |
2571 | 0 | (float)i8_sad_first_frame_pic_type[j] / af_average_sad_pic_type[j]; |
2572 | |
|
2573 | 0 | f_prev_comp = |
2574 | 0 | (float)i8_last_frame_pic_type[j] / af_average_sad_pic_type[j]; |
2575 | 0 | } |
2576 | | //if(rc_pic_type != I_PIC) |
2577 | 0 | { |
2578 | 0 | af_sum_weigh[j] += f_prev_comp * (ai4_pic_dist[j] - i4_num_pic_type[j]); |
2579 | 0 | } |
2580 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][0] = af_nume_weight[j]; |
2581 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][1] = af_sum_weigh[j]; |
2582 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][2] = af_average_sad_pic_type[j]; |
2583 | | |
2584 | | /*Disabling steady state complexity based bit movement*/ |
2585 | | /*Enable it in CBR and not in VBR since VBR already has complexity based bit movement*/ |
2586 | |
|
2587 | 0 | if(0) /*i4_frames_in_lap_end < (updated_window) || ps_rc_ctxt->e_rate_control_type == VBR_STREAMING)*/ |
2588 | 0 | { |
2589 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][0] = 1.0; |
2590 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id][j][1] = |
2591 | 0 | 0; //(float)ai4_pic_dist[j]; |
2592 | 0 | } |
2593 | 0 | } |
2594 | 0 | memmove( |
2595 | 0 | ps_rc_lap_out->ps_frame_info->af_sum_weigh, |
2596 | 0 | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id], |
2597 | 0 | sizeof(float) * MAX_PIC_TYPE * 3); |
2598 | 0 | } |
2599 | |
|
2600 | 0 | if(i4_num_pic_metric_count > 0) |
2601 | 0 | { |
2602 | 0 | i4_f_sim = i4_f_sim / i4_num_pic_metric_count; |
2603 | 0 | i4_h_sim = i4_h_sim / i4_num_pic_metric_count; |
2604 | 0 | i4_var_sum = i4_var_sum / i4_num_pic_metric_count; |
2605 | 0 | } |
2606 | 0 | else |
2607 | 0 | { |
2608 | 0 | i4_f_sim = MODERATE_FSIM_VALUE; |
2609 | 0 | i4_h_sim = MODERATE_FSIM_VALUE; |
2610 | 0 | } |
2611 | |
|
2612 | 0 | if(i > 0) |
2613 | 0 | { |
2614 | 0 | float lap_L1_comp = |
2615 | 0 | (float)i8_l1_analysis_lap_comp / |
2616 | 0 | (i * ps_rc_ctxt->i4_frame_height * |
2617 | 0 | ps_rc_ctxt->i4_frame_width); //per frame per pixel complexity |
2618 | |
|
2619 | 0 | lap_L1_comp = rc_get_offline_normalized_complexity( |
2620 | 0 | ps_rc_ctxt->u4_intra_frame_interval, |
2621 | 0 | ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width, |
2622 | 0 | lap_L1_comp, |
2623 | 0 | ps_rc_ctxt->i4_rc_pass); |
2624 | |
|
2625 | 0 | u4_L1_based_lap_complexity_q7 = (WORD32)((lap_L1_comp * (1 << 7)) + .05f); |
2626 | 0 | } |
2627 | 0 | else |
2628 | 0 | { |
2629 | 0 | u4_L1_based_lap_complexity_q7 = 25; |
2630 | 0 | } |
2631 | 0 | ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id] = |
2632 | 0 | (WORD32)u4_L1_based_lap_complexity_q7; |
2633 | | /*clip f_sim to 0.3 for better stability*/ |
2634 | 0 | if(i4_f_sim < 38) |
2635 | 0 | i4_f_sim = 128 - MAX_LAP_COMPLEXITY_Q7; |
2636 | 0 | ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id] = i4_f_sim; |
2637 | | |
2638 | | /*calculate normalized per pixel sad*/ |
2639 | 0 | nor_frm_hme_sad_q10 = (ps_rc_lap_out->i8_frame_acc_coarse_me_cost << 10) / |
2640 | 0 | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width); |
2641 | | /*if(rc_pic_type == P_PIC) |
2642 | | DBG_PRINTF("\n P frm hme sad = %f ",((float)nor_frm_hme_sad_q10/ (1 << 10))); */ |
2643 | 0 | rc_put_temp_comp_lap( |
2644 | 0 | ps_rc_ctxt->rc_hdl, i4_f_sim, nor_frm_hme_sad_q10, rc_pic_type); |
2645 | |
|
2646 | 0 | rc_set_num_scd_in_lap_window( |
2647 | 0 | ps_rc_ctxt->rc_hdl, i4_num_scd_in_lap_window, num_frames_b4_scd); |
2648 | |
|
2649 | 0 | if(rc_pic_type == I_PIC && updated_window > (ps_rc_ctxt->i4_max_inter_frm_int << 1)) |
2650 | 0 | { |
2651 | 0 | float i_to_avg_bit_ratio = ihevce_get_i_to_avg_ratio( |
2652 | 0 | (void *)ps_rc_ctxt, |
2653 | 0 | ps_rc_lap_out, |
2654 | 0 | 1, |
2655 | 0 | 1, |
2656 | 0 | 1, |
2657 | 0 | ps_rc_lap_out->ai4_offsets, |
2658 | 0 | i4_update_delay); |
2659 | 0 | i_to_avg_bit_ratio = i_to_avg_bit_ratio * 1; |
2660 | 0 | } |
2661 | | |
2662 | | /* accumulation of the hme sad over next sub gop to find the temporal comlexity of the sub GOP*/ |
2663 | 0 | if((rc_pic_type == I_PIC) || (rc_pic_type == P_PIC)) |
2664 | 0 | { |
2665 | 0 | ihevce_compute_temporal_complexity_reset_Kp_Kb( |
2666 | 0 | ps_rc_lap_out, (void *)ps_rc_ctxt, 1); |
2667 | 0 | } |
2668 | |
|
2669 | 0 | if(i4_var_sum > MAX_LAP_VAR) |
2670 | 0 | { |
2671 | 0 | i4_var_sum = MAX_LAP_VAR; |
2672 | 0 | } |
2673 | |
|
2674 | 0 | { |
2675 | | /*Filling for dumping data */ |
2676 | |
|
2677 | 0 | ps_rc_ctxt->ai4_num_scd_in_lap_window[i4_enc_frm_id] = i4_num_scd_in_lap_window; |
2678 | 0 | ps_rc_ctxt->ai4_num_frames_b4_scd[i4_enc_frm_id] = num_frames_b4_scd; |
2679 | 0 | } |
2680 | 0 | } |
2681 | 114k | } |
2682 | | |
2683 | 114k | if((ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) && (rc_pic_type > P_PIC)) |
2684 | 705 | { |
2685 | 705 | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 0; |
2686 | 705 | is_scd_ref_frame = 0; |
2687 | 705 | } |
2688 | 114k | i4_fade_scene = 0; |
2689 | | /*Scene type fade is marked only for P pics which are in fade regions*/ |
2690 | 114k | if((ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_IN || |
2691 | 114k | ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_OUT) && |
2692 | 114k | (ps_rc_lap_out->i4_rc_temporal_lyr_id == 0)) |
2693 | 0 | { |
2694 | 0 | is_scd_ref_frame = 1; |
2695 | 0 | i4_fade_scene = 1; |
2696 | 0 | } |
2697 | | |
2698 | 114k | if((!(is_scd_ref_frame || ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id])) && |
2699 | 114k | (((is_first_frame_coded(ps_rc_ctxt->rc_hdl)) && (pic_type == IV_I_FRAME)) || |
2700 | 104k | (pic_type != IV_I_FRAME))) |
2701 | 104k | { |
2702 | 104k | WORD32 i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl); |
2703 | 104k | i4_is_no_model_scd = 0; |
2704 | 104k | if(call_type == ENC_GET_QP) |
2705 | 104k | { |
2706 | 104k | if(((0 == i4_model_available) || (!i4_is_first_frame_coded))) |
2707 | 7.04k | { |
2708 | | /*No scene change but model not available*/ |
2709 | 7.04k | i4_is_no_model_scd = 1; |
2710 | 7.04k | } |
2711 | 104k | } |
2712 | 104k | } |
2713 | 10.4k | else |
2714 | 10.4k | { |
2715 | | /*actual scene changes*/ |
2716 | 10.4k | i4_is_no_model_scd = 2; |
2717 | 10.4k | } |
2718 | | /** Pre-enc thread as of now SCD handling is not present */ |
2719 | 114k | if(!i4_is_no_model_scd) |
2720 | 97.1k | { |
2721 | 97.1k | WORD32 i4_is_first_frame_coded, i4_prev_I_frm_sad, i4_cur_I_frm_sad; |
2722 | | /*Once first frame has been encoded use prev frame intra satd and cur frame satd to alter est intra sad for cur frame*/ |
2723 | 97.1k | i4_is_first_frame_coded = is_first_frame_coded(ps_rc_ctxt->rc_hdl); |
2724 | | |
2725 | | /*prev I frame sad i changes only in enc stage. For pre enc cur and prev will be same*/ |
2726 | 97.1k | if(ps_rc_ctxt->i8_prev_i_frm_cost > 0) |
2727 | 97.1k | { |
2728 | 97.1k | if(i4_is_first_frame_coded && (pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME)) |
2729 | 32.7k | { |
2730 | 32.7k | i4_prev_I_frm_sad = rc_get_prev_frame_intra_sad(ps_rc_ctxt->rc_hdl); |
2731 | 32.7k | i4_cur_I_frm_sad = (WORD32)( |
2732 | 32.7k | (ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id] * i4_prev_I_frm_sad) / |
2733 | 32.7k | ps_rc_ctxt->i8_prev_i_frm_cost); |
2734 | 32.7k | rc_update_prev_frame_intra_sad(ps_rc_ctxt->rc_hdl, i4_cur_I_frm_sad); |
2735 | 32.7k | } |
2736 | 97.1k | } |
2737 | | /*scale previous frame closed loop SAD with current frame HME SAD to be considered as current frame SAD*/ |
2738 | 97.1k | if(i4_is_first_frame_coded && !(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) && |
2739 | 97.1k | call_type == ENC_GET_QP) |
2740 | 64.3k | { |
2741 | 64.3k | if(ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] > 0) |
2742 | 64.3k | { |
2743 | 64.3k | WORD32 prev_frm_cl_sad = rc_get_prev_frame_sad(ps_rc_ctxt->rc_hdl, rc_pic_type); |
2744 | 64.3k | WORD32 cur_frm_est_cl_sad = (WORD32)( |
2745 | 64.3k | (ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id] * |
2746 | 64.3k | prev_frm_cl_sad) / |
2747 | 64.3k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type]); |
2748 | 64.3k | rc_update_prev_frame_sad(ps_rc_ctxt->rc_hdl, cur_frm_est_cl_sad, rc_pic_type); |
2749 | 64.3k | } |
2750 | 64.3k | } |
2751 | | |
2752 | 97.1k | if(rc_pic_type == I_PIC && updated_window > (ps_rc_ctxt->i4_max_inter_frm_int << 1)) |
2753 | 0 | { |
2754 | 0 | ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] = ihevce_get_i_to_avg_ratio( |
2755 | 0 | (void *)ps_rc_ctxt, |
2756 | 0 | ps_rc_lap_out, |
2757 | 0 | 1, |
2758 | 0 | 0, |
2759 | 0 | 1, |
2760 | 0 | ps_rc_lap_out->ai4_offsets, |
2761 | 0 | i4_update_delay); |
2762 | 0 | } |
2763 | | |
2764 | 97.1k | ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP = -1; |
2765 | 97.1k | i4_frame_qp_q6 = get_frame_level_qp( |
2766 | 97.1k | ps_rc_ctxt->rc_hdl, |
2767 | 97.1k | rc_pic_type, |
2768 | 97.1k | i4_max_frame_bits, |
2769 | 97.1k | &i4_cur_est_texture_bits, //this value is returned by rc |
2770 | 97.1k | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id], |
2771 | 97.1k | 1, |
2772 | 97.1k | ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id], |
2773 | 97.1k | ps_rc_lap_out->ps_frame_info, |
2774 | 97.1k | ps_rc_lap_out->i4_complexity_bin, |
2775 | 97.1k | i4_scene_num, /*no pause resume concept*/ |
2776 | 97.1k | pi4_tot_bits_estimated, |
2777 | 97.1k | &ps_rc_lap_out->i4_is_model_valid, |
2778 | 97.1k | &i4_vbv_buf_max_bits, |
2779 | 97.1k | &i4_est_tex_bits, |
2780 | 97.1k | &i4_cur_est_header_bits, |
2781 | 97.1k | &ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP, |
2782 | 97.1k | &ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP, |
2783 | 97.1k | &i4_estimate_to_calc_frm_error); |
2784 | 97.1k | ASSERT(*pi4_tot_bits_estimated != 0); |
2785 | | /** The usage of global table will truncate the input given as qp format and hence will not return very low qp values desirable at very |
2786 | | low bitrate. Hence on the fly calculation is enabled*/ |
2787 | | |
2788 | 97.1k | i4_hevc_frame_qp = |
2789 | 97.1k | ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth); |
2790 | | |
2791 | 97.1k | if(1 == ps_rc_lap_out->i4_is_model_valid) |
2792 | 97.1k | ps_rc_lap_out->i4_is_steady_state = 1; |
2793 | 0 | else |
2794 | 0 | ps_rc_lap_out->i4_is_steady_state = 0; |
2795 | | |
2796 | 97.1k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used = 0; |
2797 | 97.1k | ps_rc_ctxt->i8_est_I_pic_header_bits = i4_cur_est_header_bits; |
2798 | 97.1k | } |
2799 | 17.4k | else |
2800 | 17.4k | { |
2801 | 17.4k | WORD32 i4_count = 0, i4_total_bits, i4_min_error_hevc_qp = 0; |
2802 | 17.4k | float f_percent_error = 0.0f, f_min_error = 10000.0f; |
2803 | 17.4k | WORD32 i4_current_bits_estimated = 0; |
2804 | 17.4k | float i4_i_to_rest_ratio_final; |
2805 | 17.4k | WORD32 i4_best_br_id = 0; |
2806 | 17.4k | float af_i_qs[2]; |
2807 | 17.4k | LWORD64 ai8_i_tex_bits[2]; |
2808 | 17.4k | WORD32 i4_ref_qscale = ihevce_rc_get_scaled_mpeg2_qp( |
2809 | 17.4k | ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt); |
2810 | 17.4k | WORD32 ai4_header_bits[2]; |
2811 | | |
2812 | 17.4k | ps_rc_lap_out->i4_is_steady_state = 0; |
2813 | | |
2814 | 17.4k | if(ps_rc_lap_out->i4_L0_qp > 44) |
2815 | 0 | ps_rc_lap_out->i4_L0_qp = 44; |
2816 | 17.4k | if(ps_rc_lap_out->i4_L0_qp < 7 - ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset) |
2817 | 6.19k | ps_rc_lap_out->i4_L0_qp = 7 - ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
2818 | | |
2819 | 17.4k | ps_rc_lap_out->i4_L0_qp = ps_rc_lap_out->i4_L0_qp - 9; |
2820 | 17.4k | ps_rc_lap_out->i4_is_model_valid = 0; |
2821 | 17.4k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used = 1; |
2822 | 17.4k | ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP = -1; |
2823 | | |
2824 | 17.4k | ps_rc_ctxt->i4_normal_inter_pic = (i4_is_no_model_scd == 1); |
2825 | 314k | while(1) |
2826 | 314k | { |
2827 | 314k | WORD32 i4_frame_qs_q3; |
2828 | 314k | WORD32 i4_estimate_to_calc_frm_error_temp; |
2829 | | |
2830 | 314k | i_to_avg_bit_ratio = ihevce_get_i_to_avg_ratio( |
2831 | 314k | (void *)ps_rc_ctxt, |
2832 | 314k | ps_rc_lap_out, |
2833 | 314k | 1, |
2834 | 314k | 0, |
2835 | 314k | 1, |
2836 | 314k | ps_rc_lap_out->ai4_offsets, |
2837 | 314k | i4_update_delay); |
2838 | | |
2839 | 314k | ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id] = i_to_avg_bit_ratio; |
2840 | | |
2841 | | /** Use estimate of header bits from pre-enc*/ |
2842 | 314k | if(1 == i4_is_no_model_scd) |
2843 | 126k | { |
2844 | 126k | ps_rc_ctxt->i8_est_I_pic_header_bits = |
2845 | 126k | get_est_hdr_bits(ps_rc_ctxt->rc_hdl, rc_pic_type); |
2846 | 126k | } |
2847 | 187k | else |
2848 | 187k | { |
2849 | 187k | WORD32 i4_curr_qscale = ihevce_rc_get_scaled_mpeg2_qp( |
2850 | 187k | ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt); |
2851 | | /*Assume that 30% of header bits are constant and remaining are dependent on Qp |
2852 | | and map them accordingly*/ |
2853 | 187k | ps_rc_ctxt->i8_est_I_pic_header_bits = (LWORD64)( |
2854 | 187k | (.3 * ps_rc_lap_out->i8_est_I_pic_header_bits + |
2855 | 187k | (1. - .3) * ps_rc_lap_out->i8_est_I_pic_header_bits * i4_ref_qscale) / |
2856 | 187k | i4_curr_qscale); |
2857 | 187k | } |
2858 | | |
2859 | | /*get qp for scene cut frame based on offline data*/ |
2860 | 314k | index = ihevce_get_offline_index( |
2861 | 314k | ps_rc_ctxt, ps_rc_lap_out->i4_num_pels_in_frame_considered); |
2862 | | |
2863 | | /*Sub pic rC bits extraction */ |
2864 | 314k | i4_frame_qs_q3 = rc_get_qp_for_scd_frame( |
2865 | 314k | ps_rc_ctxt->rc_hdl, |
2866 | 314k | I_PIC, |
2867 | 314k | ps_rc_lap_out->i8_frame_satd_act_accum, |
2868 | 314k | ps_rc_lap_out->i4_num_pels_in_frame_considered, |
2869 | 314k | (WORD32)ps_rc_ctxt->i8_est_I_pic_header_bits, |
2870 | 314k | ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id], |
2871 | 314k | (void *)&g_offline_i_model_coeff[index][0], |
2872 | 314k | i_to_avg_bit_ratio, |
2873 | 314k | 1, |
2874 | 314k | ps_rc_ctxt->af_sum_weigh[i4_enc_frm_id], |
2875 | 314k | ps_rc_lap_out->ps_frame_info, |
2876 | 314k | ps_rc_ctxt->i4_rc_pass, |
2877 | 314k | (rc_pic_type != I_PIC), |
2878 | 314k | ((ps_rc_lap_out->i4_rc_temporal_lyr_id != ps_rc_ctxt->i4_max_temporal_lyr) || |
2879 | 314k | (!ps_rc_ctxt->i4_max_temporal_lyr)), |
2880 | 314k | 1, |
2881 | 314k | &i4_total_bits, |
2882 | 314k | &i4_current_bits_estimated, |
2883 | 314k | ps_rc_lap_out->i4_use_offline_model_2pass, |
2884 | 314k | ai8_i_tex_bits, |
2885 | 314k | af_i_qs, |
2886 | 314k | i4_best_br_id, |
2887 | 314k | &i4_estimate_to_calc_frm_error_temp); |
2888 | | |
2889 | 314k | i4_hevc_frame_qp = ihevce_rc_get_scaled_hevc_qp_from_qs_q3( |
2890 | 314k | i4_frame_qs_q3, ps_rc_ctxt->ps_rc_quant_ctxt); |
2891 | | |
2892 | | /*Get corresponding q scale*/ |
2893 | 314k | i4_frame_qp = |
2894 | 314k | ihevce_rc_get_scaled_mpeg2_qp(i4_hevc_frame_qp, ps_rc_ctxt->ps_rc_quant_ctxt); |
2895 | | |
2896 | 314k | if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp) |
2897 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp; |
2898 | | |
2899 | 314k | { |
2900 | 314k | WORD32 i4_init_qscale = ihevce_rc_get_scaled_mpeg2_qp( |
2901 | 314k | ps_rc_lap_out->i4_L0_qp, ps_rc_ctxt->ps_rc_quant_ctxt); |
2902 | 314k | f_percent_error = (float)(abs(i4_init_qscale - i4_frame_qp)) / i4_init_qscale; |
2903 | 314k | if(f_percent_error < f_min_error) |
2904 | 31.2k | { |
2905 | 31.2k | f_min_error = f_percent_error; |
2906 | 31.2k | i4_min_error_hevc_qp = i4_hevc_frame_qp; |
2907 | 31.2k | i4_i_to_rest_ratio_final = i_to_avg_bit_ratio; |
2908 | | /*Get the bits estimated for least error*/ |
2909 | 31.2k | *pi4_tot_bits_estimated = i4_current_bits_estimated; |
2910 | 31.2k | i4_estimate_to_calc_frm_error = i4_estimate_to_calc_frm_error_temp; |
2911 | 31.2k | } |
2912 | 283k | else |
2913 | 283k | {} |
2914 | 314k | ASSERT(*pi4_tot_bits_estimated != 0); |
2915 | 314k | } |
2916 | 314k | i4_count++; |
2917 | 314k | if(/*(ps_rc_lap_out->i4_L0_qp == i4_hevc_frame_qp) ||*/ (i4_count > 17)) |
2918 | 17.4k | break; |
2919 | 297k | ps_rc_lap_out->i4_L0_qp++; |
2920 | 297k | } |
2921 | 17.4k | ps_rc_lap_out->i4_L0_qp = i4_min_error_hevc_qp; |
2922 | | |
2923 | 17.4k | i4_hevc_frame_qp = i4_min_error_hevc_qp; |
2924 | 17.4k | if(2 == i4_is_no_model_scd) |
2925 | 10.4k | { |
2926 | | /* SGI & Enc Loop Parallelism related changes*/ |
2927 | | |
2928 | | /*model reset not required if it is first frame*/ |
2929 | 10.4k | if(ps_rc_ctxt->i4_is_first_frame_encoded && !i4_fade_scene && |
2930 | 10.4k | !ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] && |
2931 | 10.4k | !ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] && |
2932 | 10.4k | !ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] && |
2933 | 10.4k | !ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id]) |
2934 | 193 | { |
2935 | 193 | ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id] = 1; |
2936 | | /*reset all pic type is first frame encoded flag*/ |
2937 | | |
2938 | 193 | ASSERT(pic_type == IV_IDR_FRAME || pic_type == IV_I_FRAME); |
2939 | 193 | } |
2940 | 10.2k | else if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id]) |
2941 | 3.53k | { |
2942 | 3.53k | rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, I_PIC); |
2943 | 3.53k | ASSERT(rc_pic_type == I_PIC); |
2944 | 3.53k | ASSERT(ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] == 0); |
2945 | 3.53k | } |
2946 | 6.70k | else if( |
2947 | 6.70k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] || |
2948 | 6.70k | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] || |
2949 | 6.70k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] || i4_fade_scene) |
2950 | 1.57k | { |
2951 | | /*Only when there are back to back scene cuts we need a non- Ipic will be marked as scene cut*/ |
2952 | | /* Same path can also be followed during pause to resume detection to determine cur frame qp however handling during update is different*/ |
2953 | 1.57k | WORD32 i4_prev_qp, i, i4_new_qp_hevc_qp, I_hevc_qp, cur_hevc_qp; |
2954 | | |
2955 | | /*both cannot be set at same time since lap cannot mark same frame as both scene cut and pause to resume flag*/ |
2956 | 1.57k | ASSERT( |
2957 | 1.57k | (ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] && |
2958 | 1.57k | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id]) == 0); |
2959 | | |
2960 | 1.57k | I_hevc_qp = i4_hevc_frame_qp; |
2961 | | |
2962 | | /*alter ai4_prev_pic_hevc_qp so that qp restriction ll not let even other pictures temporary scd are thrashed*/ |
2963 | | //if(ps_rc_lap_out->i4_rc_temporal_lyr_id != ps_rc_ctxt->i4_max_temporal_lyr) |
2964 | 1.57k | { |
2965 | 1.57k | if(ps_rc_ctxt->i4_field_pic == 0) |
2966 | 1.57k | { |
2967 | 3.98k | for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
2968 | 2.41k | { |
2969 | 2.41k | i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i]; |
2970 | 2.41k | i4_new_qp_hevc_qp = I_hevc_qp + i; |
2971 | 2.41k | i4_new_qp_hevc_qp = ihevce_clip_min_max_qp( |
2972 | 2.41k | ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1); |
2973 | 2.41k | if(i4_prev_qp < i4_new_qp_hevc_qp) |
2974 | 1.47k | { |
2975 | 1.47k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i] = |
2976 | 1.47k | i4_new_qp_hevc_qp; |
2977 | 1.47k | } |
2978 | 2.41k | } |
2979 | 1.57k | } |
2980 | 0 | else |
2981 | 0 | { /*field case*/ |
2982 | |
|
2983 | 0 | for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
2984 | 0 | { |
2985 | 0 | i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i]; |
2986 | 0 | i4_new_qp_hevc_qp = I_hevc_qp + i; |
2987 | 0 | i4_new_qp_hevc_qp = ihevce_clip_min_max_qp( |
2988 | 0 | ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1); |
2989 | 0 | if(i4_prev_qp < i4_new_qp_hevc_qp) |
2990 | 0 | { |
2991 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i] = |
2992 | 0 | i4_new_qp_hevc_qp; |
2993 | 0 | } |
2994 | |
|
2995 | 0 | i4_prev_qp = |
2996 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i + FIELD_OFFSET]; |
2997 | 0 | i4_new_qp_hevc_qp = I_hevc_qp + i; |
2998 | 0 | i4_new_qp_hevc_qp = ihevce_clip_min_max_qp( |
2999 | 0 | ps_rc_ctxt, i4_new_qp_hevc_qp, (picture_type_e)i, i - 1); |
3000 | 0 | if(i4_prev_qp < i4_new_qp_hevc_qp) |
3001 | 0 | { |
3002 | 0 | ps_rc_ctxt |
3003 | 0 | ->ai4_prev_pic_hevc_qp[i4_scene_num][i + FIELD_OFFSET] = |
3004 | 0 | i4_new_qp_hevc_qp; |
3005 | 0 | } |
3006 | 0 | } |
3007 | 0 | } |
3008 | 1.57k | } |
3009 | 1.57k | { |
3010 | 1.57k | WORD32 i4_updated_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i]; |
3011 | 1.57k | WORD32 i4_scale; |
3012 | | |
3013 | 1.57k | if(I_hevc_qp == i4_updated_qp) |
3014 | 40 | i4_scale = 16; |
3015 | 1.53k | else if(I_hevc_qp == (i4_updated_qp - 1)) |
3016 | 41 | i4_scale = 14; |
3017 | 1.49k | else if(I_hevc_qp == (i4_updated_qp - 2)) |
3018 | 776 | i4_scale = 12; |
3019 | 716 | else |
3020 | 716 | i4_scale = 10; |
3021 | | |
3022 | 1.57k | *pi4_tot_bits_estimated = (i4_scale * (*pi4_tot_bits_estimated)) >> 4; |
3023 | 1.57k | i4_estimate_to_calc_frm_error = |
3024 | 1.57k | (i4_scale * i4_estimate_to_calc_frm_error) >> 4; |
3025 | 1.57k | } |
3026 | 1.57k | if(call_type == ENC_GET_QP) |
3027 | 1.57k | { |
3028 | 1.57k | ps_rc_lap_out->i8_est_text_bits = *pi4_tot_bits_estimated; |
3029 | 1.57k | } |
3030 | 1.57k | ASSERT(*pi4_tot_bits_estimated != 0); |
3031 | | |
3032 | | /*use previous frame qp of same pic type or SCD i frame qp with offset whichever is maximum*/ |
3033 | | /*For field case adding of grater than 4 results in the qp increasing greatly when compared to previous pics/fields*/ |
3034 | 1.57k | if(rc_pic_type <= FIELD_OFFSET) |
3035 | 1.57k | cur_hevc_qp = I_hevc_qp + rc_pic_type; |
3036 | 0 | else |
3037 | 0 | cur_hevc_qp = I_hevc_qp + (rc_pic_type - FIELD_OFFSET); |
3038 | | |
3039 | 1.57k | i4_prev_qp = ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type]; |
3040 | | |
3041 | 1.57k | if((cur_hevc_qp < i4_prev_qp) && (ps_rc_ctxt->i4_num_active_pic_type > 2) && |
3042 | 1.57k | (is_first_frame_coded(ps_rc_ctxt->rc_hdl)) && (!i4_fade_scene)) |
3043 | 80 | { |
3044 | 80 | cur_hevc_qp = i4_prev_qp; |
3045 | 80 | } |
3046 | 1.57k | i4_frame_qp = |
3047 | 1.57k | ihevce_rc_get_scaled_mpeg2_qp(cur_hevc_qp, ps_rc_ctxt->ps_rc_quant_ctxt); |
3048 | 1.57k | i4_hevc_frame_qp = cur_hevc_qp; |
3049 | | //ps_rc_ctxt->i4_is_non_I_scd_pic = 0; |
3050 | | |
3051 | 1.57k | rc_reset_first_frame_coded_flag(ps_rc_ctxt->rc_hdl, rc_pic_type); |
3052 | 1.57k | } |
3053 | 5.13k | else |
3054 | 5.13k | {} |
3055 | 10.4k | } |
3056 | 17.4k | if((1 == i4_is_no_model_scd) && (call_type == ENC_GET_QP)) |
3057 | 7.04k | { |
3058 | 7.04k | WORD32 i4_clip_QP; |
3059 | 7.04k | i4_frame_qp_q6 = |
3060 | 7.04k | clip_qp_based_on_prev_ref(ps_rc_ctxt->rc_hdl, rc_pic_type, 1, i4_scene_num); |
3061 | 7.04k | i4_clip_QP = |
3062 | 7.04k | ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rc_ctxt->u1_bit_depth); |
3063 | 7.04k | if(ps_rc_ctxt->i4_rc_pass != 2) |
3064 | 7.04k | { |
3065 | 7.04k | i4_hevc_frame_qp = i4_clip_QP; |
3066 | 7.04k | } |
3067 | 7.04k | if((rc_pic_type == P_PIC) || (rc_pic_type == P1_PIC)) |
3068 | 4.71k | { |
3069 | 4.71k | *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 11) >> 4; /* P picture*/ |
3070 | 4.71k | i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 11) >> 4; |
3071 | 4.71k | } |
3072 | 2.33k | else if((rc_pic_type == B_PIC) || (rc_pic_type == BB_PIC)) |
3073 | 924 | { |
3074 | 924 | *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 9) >> 4; /* B layer 1*/ |
3075 | 924 | i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 9) >> 4; |
3076 | 924 | } |
3077 | 1.40k | else if((rc_pic_type == B1_PIC) || (rc_pic_type == B11_PIC)) |
3078 | 1.05k | { |
3079 | 1.05k | *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 7) >> 4; /* B layer 2*/ |
3080 | 1.05k | i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 7) >> 4; |
3081 | 1.05k | } |
3082 | 358 | else if((rc_pic_type == B2_PIC) || (rc_pic_type == B22_PIC)) |
3083 | 0 | { |
3084 | 0 | *pi4_tot_bits_estimated = (*pi4_tot_bits_estimated * 5) >> 4; /* B layer 3*/ |
3085 | 0 | i4_estimate_to_calc_frm_error = (i4_estimate_to_calc_frm_error * 5) >> 4; |
3086 | 0 | } |
3087 | 7.04k | } |
3088 | 17.4k | rc_add_est_tot(ps_rc_ctxt->rc_hdl, *pi4_tot_bits_estimated); |
3089 | 17.4k | } |
3090 | | |
3091 | 114k | ASSERT(i4_hevc_frame_qp >= -ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset); |
3092 | | |
3093 | | /*constraint qp swing based on neighbour frames*/ |
3094 | 114k | if(is_first_frame_coded(ps_rc_ctxt->rc_hdl)) |
3095 | 97.3k | { |
3096 | 97.3k | if(ps_rc_ctxt->i4_field_pic == 0) |
3097 | 97.3k | { |
3098 | | /*In dissolve case the p frame comes before an I pic and ref b comes after then what |
3099 | | happens is b frame qp is restricted by the p frame qp so changed it to prev ref pic type*/ |
3100 | 97.3k | if(rc_pic_type != I_PIC && rc_pic_type != P_PIC) |
3101 | 6.62k | { |
3102 | 6.62k | if(ps_rc_lap_out->i4_rc_temporal_lyr_id == 1) |
3103 | 1.99k | { |
3104 | 1.99k | picture_type_e prev_ref_pic_type = |
3105 | 1.99k | rc_getprev_ref_pic_type(ps_rc_ctxt->rc_hdl); |
3106 | | |
3107 | 1.99k | if(i4_hevc_frame_qp > |
3108 | 1.99k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] + 3) |
3109 | 1 | { |
3110 | 1 | if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] > |
3111 | 1 | 0) |
3112 | 1 | i4_hevc_frame_qp = |
3113 | 1 | ps_rc_ctxt |
3114 | 1 | ->ai4_prev_pic_hevc_qp[i4_scene_num][prev_ref_pic_type] + |
3115 | 1 | 3; |
3116 | 1 | } |
3117 | 1.99k | } |
3118 | 4.63k | else if( |
3119 | 4.63k | i4_hevc_frame_qp > |
3120 | 4.63k | (ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 3)) |
3121 | 2 | { |
3122 | | /*allow max of +3 compared to previous frame*/ |
3123 | 2 | if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] > 0) |
3124 | 2 | i4_hevc_frame_qp = |
3125 | 2 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 3; |
3126 | 2 | } |
3127 | 6.62k | } |
3128 | | |
3129 | 97.3k | if((rc_pic_type != I_PIC && rc_pic_type != P_PIC) && |
3130 | 97.3k | (i4_hevc_frame_qp < |
3131 | 6.62k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1])) |
3132 | 6.48k | { |
3133 | 6.48k | i4_hevc_frame_qp = |
3134 | 6.48k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1]; |
3135 | 6.48k | } |
3136 | | |
3137 | | /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/ |
3138 | 97.3k | if(temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr && |
3139 | 97.3k | ps_rc_ctxt->i4_max_temporal_lyr > 1) |
3140 | 4.63k | { |
3141 | 4.63k | i4_hevc_frame_qp = |
3142 | 4.63k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type - 1] + 1; |
3143 | 4.63k | } |
3144 | 97.3k | } |
3145 | 0 | else /*for field case*/ |
3146 | 0 | { |
3147 | 0 | if(ps_rc_lap_out->i4_rc_temporal_lyr_id >= 1) |
3148 | 0 | { |
3149 | | /*To make the comparison of qp with the top field's of previous layer tempor layer id matches with the pic type. */ |
3150 | 0 | if(i4_hevc_frame_qp > |
3151 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num] |
3152 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id] + |
3153 | 0 | 3) |
3154 | 0 | { |
3155 | | /*allow max of +3 compared to previous frame*/ |
3156 | 0 | if(0 < |
3157 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num] |
3158 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id]) |
3159 | 0 | i4_hevc_frame_qp = |
3160 | 0 | ps_rc_ctxt |
3161 | 0 | ->ai4_prev_pic_hevc_qp[i4_scene_num] |
3162 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id] + |
3163 | 0 | 3; |
3164 | 0 | } |
3165 | 0 | if(i4_hevc_frame_qp < |
3166 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num] |
3167 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id]) |
3168 | 0 | { |
3169 | 0 | i4_hevc_frame_qp = |
3170 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num] |
3171 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id]; |
3172 | 0 | } |
3173 | | |
3174 | | /** Force non-ref B pic qp to be ref_B_PIC_qp - 1. This is not valid for when max teporla later is less than 2*/ |
3175 | 0 | if(temporal_layer_id == ps_rc_ctxt->i4_max_temporal_lyr && |
3176 | 0 | ps_rc_ctxt->i4_max_temporal_lyr > 1) |
3177 | 0 | { |
3178 | 0 | i4_hevc_frame_qp = |
3179 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num] |
3180 | 0 | [ps_rc_lap_out->i4_rc_temporal_lyr_id] + |
3181 | 0 | 1; |
3182 | 0 | } |
3183 | 0 | } |
3184 | | /** At lower range qp swing for same pic type is also imposed to make sure |
3185 | | qp does not fall from 10 to 4 since they differ by only one q scale*/ |
3186 | 0 | } |
3187 | 97.3k | } |
3188 | | |
3189 | | /**clip to min qp which is user configurable*/ |
3190 | 114k | i4_hevc_frame_qp = ihevce_clip_min_max_qp( |
3191 | 114k | ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id); |
3192 | | |
3193 | 114k | #if 1 //FRAME_PARALLEL_LVL |
3194 | 114k | ps_rc_ctxt->i4_est_text_bits_ctr_get_qp++; //ELP_RC |
3195 | 114k | ps_rc_ctxt->i4_est_text_bits_ctr_get_qp = |
3196 | 114k | (ps_rc_ctxt->i4_est_text_bits_ctr_get_qp % (ps_rc_ctxt->i4_num_frame_parallel)); |
3197 | 114k | #endif |
3198 | | /** the estimates are reset only duing enc call*/ |
3199 | | |
3200 | | #if USE_USER_FIRST_FRAME_QP |
3201 | | /*I_PIC check is necessary coz pre-enc can query for qp even before first frame update has happened*/ |
3202 | | if(!ps_rc_ctxt->i4_is_first_frame_encoded && rc_pic_type == I_PIC) |
3203 | | { |
3204 | | i4_hevc_frame_qp = ps_rc_ctxt->i4_init_frame_qp_user; |
3205 | | DBG_PRINTF("FIXED START QP PATH *************************\n"); |
3206 | | } |
3207 | | #endif |
3208 | 114k | } |
3209 | | |
3210 | 114k | if(CONST_QP != e_rc_type) |
3211 | 114k | { |
3212 | 114k | ASSERT(*pi4_tot_bits_estimated != 0); |
3213 | 114k | } |
3214 | | |
3215 | 114k | ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP = i4_hevc_frame_qp; |
3216 | 114k | if(ps_rc_lap_out->i4_is_model_valid) |
3217 | 97.1k | { |
3218 | 97.1k | get_bits_for_final_qp( |
3219 | 97.1k | ps_rc_ctxt->rc_hdl, |
3220 | 97.1k | &ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP, |
3221 | 97.1k | &ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP, |
3222 | 97.1k | &ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP, |
3223 | 97.1k | i4_hevc_frame_qp, |
3224 | 97.1k | ihevce_rc_get_scaled_mpeg2_qp_q6( |
3225 | 97.1k | i4_hevc_frame_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset, |
3226 | 97.1k | ps_rc_ctxt->u1_bit_depth), |
3227 | 97.1k | i4_cur_est_header_bits, |
3228 | 97.1k | i4_est_tex_bits, |
3229 | 97.1k | i4_vbv_buf_max_bits, |
3230 | 97.1k | rc_pic_type, |
3231 | 97.1k | ps_rc_lap_out->i4_rc_display_num); |
3232 | 97.1k | } |
3233 | 114k | i4_deltaQP = ihevce_ebf_based_rc_correction_to_avoid_overflow( |
3234 | 114k | ps_rc_ctxt, ps_rc_lap_out, pi4_tot_bits_estimated); |
3235 | 114k | i4_hevc_frame_qp += i4_deltaQP; |
3236 | | |
3237 | | /**clip to min qp which is user configurable*/ |
3238 | 114k | i4_hevc_frame_qp = ihevce_clip_min_max_qp( |
3239 | 114k | ps_rc_ctxt, i4_hevc_frame_qp, rc_pic_type, ps_rc_lap_out->i4_rc_temporal_lyr_id); |
3240 | | |
3241 | | /*set estimate status for frame level error calculation*/ |
3242 | 114k | if(i4_estimate_to_calc_frm_error > 0) |
3243 | 114k | { |
3244 | 114k | rc_set_estimate_status( |
3245 | 114k | ps_rc_ctxt->rc_hdl, |
3246 | 114k | i4_estimate_to_calc_frm_error - ps_rc_ctxt->i8_est_I_pic_header_bits, |
3247 | 114k | ps_rc_ctxt->i8_est_I_pic_header_bits, |
3248 | 114k | ps_rc_ctxt->i4_est_text_bits_ctr_get_qp); |
3249 | 114k | } |
3250 | 14 | else |
3251 | 14 | { |
3252 | 14 | rc_set_estimate_status( |
3253 | 14 | ps_rc_ctxt->rc_hdl, |
3254 | 14 | -1, |
3255 | 14 | ps_rc_ctxt->i8_est_I_pic_header_bits, |
3256 | 14 | ps_rc_ctxt->i4_est_text_bits_ctr_get_qp); |
3257 | 14 | } |
3258 | | |
3259 | 114k | ps_rc_lap_out->i8_est_text_bits = *pi4_tot_bits_estimated; |
3260 | | |
3261 | | /*B pictures which are in fades will take the highest QP of either side of P pics*/ |
3262 | 114k | if(ps_rc_lap_out->i4_rc_pic_type == IV_B_FRAME && |
3263 | 114k | (ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_IN || |
3264 | 8.76k | ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_FADE_OUT)) |
3265 | 0 | { |
3266 | 0 | i4_hevc_frame_qp = |
3267 | 0 | MAX(ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0], ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1]); |
3268 | 0 | } |
3269 | | |
3270 | | /*saving the last two pics of layer 0*/ |
3271 | 114k | if(0 == ps_rc_lap_out->i4_rc_temporal_lyr_id) |
3272 | 105k | { |
3273 | 105k | ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[1] = ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0]; |
3274 | 105k | ps_rc_ctxt->ai4_last_tw0_lyr0_pic_qp[0] = i4_hevc_frame_qp; |
3275 | 105k | } |
3276 | | |
3277 | 114k | return i4_hevc_frame_qp; |
3278 | 114k | } |
3279 | | |
3280 | | /*##########################################################*/ |
3281 | | /******* END OF ENC THRD QP QUERY FUNCTIONS ****************/ |
3282 | | /*########################################################*/ |
3283 | | |
3284 | | /*####################################################*/ |
3285 | | /******* START OF I2AVG RATIO FUNCTIONS **************/ |
3286 | | /*##################################################*/ |
3287 | | |
3288 | | /** |
3289 | | ****************************************************************************** |
3290 | | * |
3291 | | * @brief function to get i_to_avg_rest at scene cut frame based on data available from LAP |
3292 | | * |
3293 | | * @par Description |
3294 | | * |
3295 | | * @param[in] pv_rc_ctxt |
3296 | | * void pointer to rc ctxt |
3297 | | * @param[in] ps_rc_lap_out : pointer to lap out structure |
3298 | | * @param[in] i4_update_delay : The Delay in the update. This can happen for dist. case! |
3299 | | * All decision should consider this delay for updation! |
3300 | | * @return WORD32 i_to_rest bit ratio |
3301 | | * |
3302 | | ****************************************************************************** |
3303 | | */ |
3304 | | float ihevce_get_i_to_avg_ratio( |
3305 | | void *pv_rc_ctxt, |
3306 | | rc_lap_out_params_t *ps_rc_lap_out, |
3307 | | WORD32 i_to_p_qp_offset, |
3308 | | WORD32 i4_offset_flag, |
3309 | | WORD32 i4_call_type, |
3310 | | WORD32 ai4_qp_offsets[4], |
3311 | | WORD32 i4_update_delay) |
3312 | 324k | { |
3313 | 324k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
3314 | 324k | WORD32 i = 0, k = 0, num_frames_in_lap[MAX_PIC_TYPE] = { 0 }, ai4_pic_dist[MAX_PIC_TYPE], |
3315 | 324k | ai4_pic_dist_in_cur_gop[MAX_PIC_TYPE] = { 0 }; |
3316 | 324k | WORD32 i4_num_b, i4_num_frms_traversed_in_lap = 0, total_frms_considered = 0, |
3317 | 324k | i4_flag_i_frame_exit = 0, u4_rc_scene_number; |
3318 | 324k | rc_lap_out_params_t *ps_cur_rc_lap_out = ps_rc_lap_out; |
3319 | | |
3320 | 324k | rc_lap_out_params_t *ps_cur_rc_lap_out_I = ps_rc_lap_out; |
3321 | 324k | double complexity[MAX_PIC_TYPE] = { 0 }, d_first_i_complexity = 0, d_first_p_complexity = 0.0f, |
3322 | 324k | cur_lambda_modifer, den = 0, average_intra_complexity = 0; |
3323 | 324k | double i_frm_lambda_modifier; |
3324 | 324k | float i_to_rest_bit_ratio = 8.00; |
3325 | 324k | picture_type_e curr_rc_pic_type; |
3326 | 324k | LWORD64 i8_l1_analysis_lap_comp = 0; |
3327 | 324k | WORD32 i4_intra_frame_interval = rc_get_intra_frame_interval(ps_rc_ctxt->rc_hdl); |
3328 | 324k | UWORD32 u4_L1_based_lap_complexity_q7 = 0; |
3329 | 324k | WORD32 i4_frame_qp = 0, i4_I_frame_qp = 0; |
3330 | | |
3331 | 324k | WORD32 ai4_lambda_offsets[5] = { -3, -2, 2, 6, 7 }; |
3332 | | /* The window for which your update is guaranteed */ |
3333 | 324k | WORD32 updated_window = ps_rc_ctxt->i4_num_frame_in_lap_window - i4_update_delay; |
3334 | | |
3335 | 324k | ASSERT(ps_rc_ctxt->i4_rc_pass != 2); |
3336 | 324k | rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]); |
3337 | | |
3338 | 324k | if(ps_rc_ctxt->i4_max_temporal_lyr) |
3339 | 86.2k | { |
3340 | 86.2k | i4_num_b = ((WORD32)pow((float)2, ps_rc_ctxt->i4_max_temporal_lyr)) - 1; |
3341 | 86.2k | } |
3342 | 238k | else |
3343 | 238k | { |
3344 | 238k | i4_num_b = 0; |
3345 | 238k | } |
3346 | 324k | i_frm_lambda_modifier = ihevce_get_frame_lambda_modifier((WORD8)I_PIC, 0, 1, 1, i4_num_b); |
3347 | | /* check should be wrt inter frame interval*/ |
3348 | | /*If lap frames are not sufficient return default ratio*/ |
3349 | 324k | u4_rc_scene_number = ps_cur_rc_lap_out_I->u4_rc_scene_num; |
3350 | | |
3351 | 324k | if(updated_window < 4) |
3352 | 324k | { |
3353 | 324k | return i_to_rest_bit_ratio; |
3354 | 324k | } |
3355 | | |
3356 | 0 | k = 0; |
3357 | 0 | if(ps_cur_rc_lap_out != NULL) |
3358 | 0 | { |
3359 | 0 | WORD32 i4_temp_frame_qp; |
3360 | |
|
3361 | 0 | if(ps_cur_rc_lap_out->i4_L0_qp == -1) |
3362 | 0 | { |
3363 | 0 | i4_frame_qp = ps_cur_rc_lap_out->i4_L1_qp; |
3364 | 0 | i4_I_frame_qp = ps_cur_rc_lap_out->i4_L1_qp - 3; |
3365 | 0 | } |
3366 | 0 | else |
3367 | 0 | { |
3368 | 0 | i4_frame_qp = ps_cur_rc_lap_out->i4_L0_qp; |
3369 | 0 | i4_I_frame_qp = ps_cur_rc_lap_out->i4_L0_qp - 3; |
3370 | 0 | } |
3371 | |
|
3372 | 0 | do |
3373 | 0 | { |
3374 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
3375 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out->i4_rc_pic_type, |
3376 | 0 | ps_rc_ctxt->i4_field_pic, |
3377 | 0 | ps_cur_rc_lap_out->i4_rc_temporal_lyr_id, |
3378 | 0 | ps_cur_rc_lap_out->i4_is_bottom_field, |
3379 | 0 | ps_rc_ctxt->i4_top_field_first); |
3380 | 0 | cur_lambda_modifer = ihevce_get_frame_lambda_modifier( |
3381 | 0 | (WORD8)curr_rc_pic_type, |
3382 | 0 | ps_cur_rc_lap_out->i4_rc_temporal_lyr_id, |
3383 | 0 | 1, |
3384 | 0 | ps_cur_rc_lap_out->i4_rc_is_ref_pic, |
3385 | 0 | i4_num_b); |
3386 | 0 | if(curr_rc_pic_type == I_PIC) |
3387 | 0 | { |
3388 | 0 | i4_temp_frame_qp = i4_frame_qp + ai4_lambda_offsets[curr_rc_pic_type]; |
3389 | 0 | } |
3390 | 0 | else |
3391 | 0 | { |
3392 | 0 | i4_temp_frame_qp = |
3393 | 0 | i4_frame_qp + ai4_lambda_offsets[ps_cur_rc_lap_out->i4_rc_temporal_lyr_id + 1]; |
3394 | 0 | i4_temp_frame_qp = |
3395 | 0 | i4_temp_frame_qp + |
3396 | 0 | ps_cur_rc_lap_out->ai4_offsets[ps_cur_rc_lap_out->i4_rc_temporal_lyr_id + 1]; |
3397 | 0 | } |
3398 | |
|
3399 | 0 | i4_temp_frame_qp = CLIP3(i4_temp_frame_qp, 1, 51); |
3400 | 0 | i4_I_frame_qp = CLIP3(i4_I_frame_qp, 1, 51); |
3401 | |
|
3402 | 0 | if(curr_rc_pic_type == I_PIC) |
3403 | 0 | { |
3404 | 0 | complexity[I_PIC] += (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp]; |
3405 | 0 | if(total_frms_considered == 0) |
3406 | 0 | d_first_i_complexity = |
3407 | 0 | (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp]; |
3408 | |
|
3409 | 0 | num_frames_in_lap[I_PIC]++; |
3410 | 0 | i8_l1_analysis_lap_comp += |
3411 | 0 | (LWORD64)(1.17 * ps_cur_rc_lap_out->i8_raw_pre_intra_sad); |
3412 | 0 | } |
3413 | 0 | else |
3414 | 0 | { |
3415 | 0 | if((num_frames_in_lap[P_PIC] == 0) && (curr_rc_pic_type == P_PIC)) |
3416 | 0 | d_first_p_complexity = |
3417 | 0 | (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp]; |
3418 | |
|
3419 | 0 | if(total_frms_considered == 0) |
3420 | 0 | { |
3421 | 0 | num_frames_in_lap[I_PIC]++; |
3422 | 0 | { |
3423 | 0 | complexity[I_PIC] += |
3424 | 0 | (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp]; |
3425 | 0 | d_first_i_complexity = |
3426 | 0 | (double)ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp]; |
3427 | 0 | } |
3428 | 0 | } |
3429 | 0 | else |
3430 | 0 | { |
3431 | | /*SAD is scaled according the lambda parametrs use to make it proportional to bits consumed in the end*/ |
3432 | 0 | #if !USE_SQRT |
3433 | | //complexity[curr_rc_pic_type] += (double)(MIN(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp],ps_cur_rc_lap_out->i8_pre_intra_sad)/(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/)))); |
3434 | 0 | if((curr_rc_pic_type > P_PIC) && |
3435 | 0 | (ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6)) |
3436 | 0 | complexity[curr_rc_pic_type] += |
3437 | 0 | (double)(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad |
3438 | 0 | [i4_temp_frame_qp]); // /(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/)))); |
3439 | 0 | else |
3440 | 0 | complexity[curr_rc_pic_type] += (double)(MIN( |
3441 | 0 | ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp], |
3442 | 0 | ps_cur_rc_lap_out->ai8_pre_intra_sad |
3443 | 0 | [i4_temp_frame_qp])); ///(/*(cur_lambda_modifer/i_frm_lambda_modifier) * */pow(1.125,(ps_rc_lap_out->i4_rc_temporal_lyr_id + 1/*i_to_p_qp_offset*/)))); |
3444 | |
|
3445 | | #else |
3446 | | complexity[curr_rc_pic_type] += |
3447 | | MIN(ps_cur_rc_lap_out->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp], |
3448 | | ps_cur_rc_lap_out->i8_pre_intra_sad) / |
3449 | | (sqrt(cur_lambda_modifer / i_frm_lambda_modifier) * |
3450 | | pow(1.125, (ps_rc_lap_out->i4_rc_temporal_lyr_id + 1))); |
3451 | | #endif |
3452 | 0 | num_frames_in_lap[curr_rc_pic_type]++; |
3453 | 0 | } |
3454 | 0 | i8_l1_analysis_lap_comp += (LWORD64)( |
3455 | 0 | (float)ps_cur_rc_lap_out->i8_raw_l1_coarse_me_sad / |
3456 | 0 | pow(1.125, curr_rc_pic_type)); |
3457 | 0 | } |
3458 | |
|
3459 | 0 | if(ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6) |
3460 | 0 | { |
3461 | 0 | if(curr_rc_pic_type < B_PIC) |
3462 | 0 | { |
3463 | | /*accumulate average intra sad*/ |
3464 | 0 | average_intra_complexity += |
3465 | 0 | ps_cur_rc_lap_out |
3466 | 0 | ->ai8_pre_intra_sad[i4_I_frame_qp] /*/i_frm_lambda_modifier*/; |
3467 | 0 | i4_num_frms_traversed_in_lap++; |
3468 | 0 | } |
3469 | 0 | } |
3470 | 0 | else |
3471 | 0 | { |
3472 | | /*accumulate average intra sad*/ |
3473 | 0 | average_intra_complexity += |
3474 | 0 | ps_cur_rc_lap_out->ai8_pre_intra_sad[i4_I_frame_qp] /*/i_frm_lambda_modifier*/; |
3475 | 0 | i4_num_frms_traversed_in_lap++; |
3476 | 0 | } |
3477 | |
|
3478 | 0 | ai4_pic_dist_in_cur_gop[curr_rc_pic_type]++; |
3479 | 0 | i++; |
3480 | 0 | total_frms_considered++; |
3481 | 0 | i4_num_frms_traversed_in_lap++; |
3482 | 0 | ps_cur_rc_lap_out = (rc_lap_out_params_t *)ps_cur_rc_lap_out->ps_rc_lap_out_next_encode; |
3483 | |
|
3484 | 0 | if((ps_cur_rc_lap_out == NULL) || |
3485 | 0 | ((total_frms_considered + k) == i4_intra_frame_interval) || (i >= updated_window)) |
3486 | 0 | { |
3487 | 0 | break; |
3488 | 0 | } |
3489 | | |
3490 | 0 | if((i >= (ps_rc_ctxt->i4_next_sc_i_in_rc_look_ahead - k) || |
3491 | 0 | (ps_cur_rc_lap_out->i4_rc_pic_type == IV_I_FRAME) || |
3492 | 0 | (ps_cur_rc_lap_out->i4_rc_pic_type == IV_IDR_FRAME)) && |
3493 | 0 | (i4_offset_flag == 1)) |
3494 | 0 | { |
3495 | 0 | break; |
3496 | 0 | } |
3497 | | /*If an I frame enters the lookahead it can cause bit allocation to go bad |
3498 | | if corresponding p/b frames are absent*/ |
3499 | 0 | if(((total_frms_considered + k) > (WORD32)(0.75f * i4_intra_frame_interval)) && |
3500 | 0 | ((ps_cur_rc_lap_out->i4_rc_pic_type == IV_I_FRAME) || |
3501 | 0 | (ps_cur_rc_lap_out->i4_rc_pic_type == IV_IDR_FRAME))) |
3502 | 0 | { |
3503 | 0 | i4_flag_i_frame_exit = 1; |
3504 | 0 | break; |
3505 | 0 | } |
3506 | |
|
3507 | 0 | } while(1); |
3508 | | |
3509 | 0 | if(total_frms_considered > 0) |
3510 | 0 | { |
3511 | 0 | float lap_L1_comp = |
3512 | 0 | (float)i8_l1_analysis_lap_comp / |
3513 | 0 | (total_frms_considered * ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width); |
3514 | |
|
3515 | 0 | lap_L1_comp = rc_get_offline_normalized_complexity( |
3516 | 0 | ps_rc_ctxt->u4_intra_frame_interval, |
3517 | 0 | ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width, |
3518 | 0 | lap_L1_comp, |
3519 | 0 | ps_rc_ctxt->i4_rc_pass); |
3520 | |
|
3521 | 0 | u4_L1_based_lap_complexity_q7 = (WORD32)((lap_L1_comp * (1 << 7)) + .05f); |
3522 | 0 | } |
3523 | 0 | else |
3524 | 0 | { |
3525 | 0 | u4_L1_based_lap_complexity_q7 = 25; |
3526 | 0 | } |
3527 | |
|
3528 | 0 | if(i4_call_type == 1) |
3529 | 0 | { |
3530 | 0 | if(num_frames_in_lap[0] > 0) |
3531 | 0 | { |
3532 | 0 | float f_curr_i_to_sum = (float)(d_first_i_complexity / complexity[0]); |
3533 | 0 | f_curr_i_to_sum = CLIP3(f_curr_i_to_sum, 0.1f, 100.0f); |
3534 | 0 | rc_set_i_to_sum_api_ba(ps_rc_ctxt->rc_hdl, f_curr_i_to_sum); |
3535 | 0 | } |
3536 | 0 | } |
3537 | |
|
3538 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
3539 | 0 | { |
3540 | 0 | if(num_frames_in_lap[i] > 0) |
3541 | 0 | { |
3542 | 0 | complexity[i] = complexity[i] / num_frames_in_lap[i]; |
3543 | 0 | } |
3544 | 0 | } |
3545 | | /*for non - I scd case it is possible that entire LAP window might not have intra picture. Consider average intra sad when |
3546 | | atleast one I pic is not available*/ |
3547 | 0 | if(num_frames_in_lap[I_PIC] == 0) |
3548 | 0 | { |
3549 | 0 | ASSERT(i4_num_frms_traversed_in_lap); |
3550 | 0 | complexity[I_PIC] = average_intra_complexity / i4_num_frms_traversed_in_lap; |
3551 | 0 | } |
3552 | | /*get picture type distribution in LAP*/ |
3553 | 0 | if(num_frames_in_lap[I_PIC] == 0) |
3554 | 0 | { |
3555 | 0 | rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]); |
3556 | 0 | } |
3557 | 0 | else |
3558 | 0 | { |
3559 | 0 | memmove(ai4_pic_dist, num_frames_in_lap, sizeof(WORD32) * MAX_PIC_TYPE); |
3560 | 0 | } |
3561 | |
|
3562 | 0 | { |
3563 | 0 | WORD32 num_inter_pic = 0; |
3564 | 0 | for(i = 1; i < MAX_PIC_TYPE; i++) |
3565 | 0 | { |
3566 | 0 | den += complexity[i] * ai4_pic_dist[i]; |
3567 | 0 | } |
3568 | |
|
3569 | 0 | for(i = 1; i < MAX_PIC_TYPE; i++) |
3570 | 0 | { |
3571 | 0 | num_inter_pic += ai4_pic_dist[i]; |
3572 | 0 | } |
3573 | 0 | if(num_inter_pic > 0) |
3574 | 0 | den = den / num_inter_pic; |
3575 | 0 | else |
3576 | 0 | den = 0.0; |
3577 | 0 | } |
3578 | |
|
3579 | 0 | if(den > 0) |
3580 | 0 | i_to_rest_bit_ratio = (float)((complexity[I_PIC]) / den); |
3581 | 0 | else |
3582 | 0 | i_to_rest_bit_ratio = 15; |
3583 | |
|
3584 | 0 | if((total_frms_considered < (WORD32)(0.75f * i4_intra_frame_interval)) && |
3585 | 0 | (total_frms_considered < (updated_window - 1)) && |
3586 | 0 | ((UWORD32)total_frms_considered < ((ps_rc_ctxt->u4_max_frame_rate / 1000)))) |
3587 | 0 | { |
3588 | | /*This GOP will only sustain for few frames hence have strict restriction for I to rest ratio*/ |
3589 | 0 | if(i_to_rest_bit_ratio > 12) |
3590 | 0 | i_to_rest_bit_ratio = 12; |
3591 | |
|
3592 | 0 | if(i_to_rest_bit_ratio > 8 && |
3593 | 0 | total_frms_considered < (ps_rc_ctxt->i4_max_inter_frm_int * 2)) |
3594 | 0 | i_to_rest_bit_ratio = 8; |
3595 | 0 | } |
3596 | 0 | } |
3597 | | |
3598 | 0 | if((i4_call_type == 1) && (i_to_rest_bit_ratio < I_TO_REST_VVFAST) && (i4_offset_flag == 1)) |
3599 | 0 | { |
3600 | 0 | float f_p_to_i_ratio = (float)(d_first_p_complexity / d_first_i_complexity); |
3601 | 0 | if(ps_rc_lap_out->i8_frame_satd_act_accum < |
3602 | 0 | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width * 1.5f)) |
3603 | 0 | rc_set_p_to_i_complexity_ratio(ps_rc_ctxt->rc_hdl, f_p_to_i_ratio); |
3604 | 0 | } |
3605 | | |
3606 | | /*Reset the pic distribution if I frame exit was encountered*/ |
3607 | |
|
3608 | 0 | if(ps_rc_ctxt->e_rate_control_type != CONST_QP) |
3609 | 0 | { |
3610 | 0 | rc_get_pic_distribution(ps_rc_ctxt->rc_hdl, &ai4_pic_dist[0]); |
3611 | 0 | if((ai4_pic_dist_in_cur_gop[I_PIC] > 1) && (ai4_pic_dist[0] == 1)) |
3612 | 0 | { |
3613 | 0 | i4_flag_i_frame_exit = 1; |
3614 | 0 | } |
3615 | 0 | if(i4_flag_i_frame_exit && (i4_call_type == 1)) |
3616 | 0 | { |
3617 | 0 | if(ai4_pic_dist_in_cur_gop[I_PIC] == 0) |
3618 | 0 | memmove(ai4_pic_dist_in_cur_gop, num_frames_in_lap, sizeof(WORD32) * MAX_PIC_TYPE); |
3619 | |
|
3620 | 0 | rc_update_pic_distn_lap_to_rc(ps_rc_ctxt->rc_hdl, ai4_pic_dist_in_cur_gop); |
3621 | 0 | rc_set_bits_based_on_complexity( |
3622 | 0 | ps_rc_ctxt->rc_hdl, u4_L1_based_lap_complexity_q7, total_frms_considered); |
3623 | 0 | } |
3624 | 0 | } |
3625 | |
|
3626 | 0 | return i_to_rest_bit_ratio; |
3627 | 0 | } |
3628 | | |
3629 | | /*##################################################*/ |
3630 | | /******* END OF I2AVG RATIO FUNCTIONS **************/ |
3631 | | /*################################################*/ |
3632 | | |
3633 | | /*#########################################################*/ |
3634 | | /******* START OF QSCALE CONVERSION FUNCTIONS *************/ |
3635 | | /*########################################################*/ |
3636 | | |
3637 | | /** |
3638 | | ****************************************************************************** |
3639 | | * |
3640 | | * @brief function to convert from qscale to qp |
3641 | | * |
3642 | | * @par Description |
3643 | | * @param[in] i4_frame_qs_q3 : QP value in qscale |
3644 | | * return frame qp |
3645 | | ****************************************************************************** |
3646 | | */ |
3647 | | |
3648 | | WORD32 ihevce_rc_get_scaled_hevc_qp_from_qs_q3(WORD32 i4_frame_qs_q3, rc_quant_t *ps_rc_quant_ctxt) |
3649 | 668k | { |
3650 | 668k | if(i4_frame_qs_q3 > ps_rc_quant_ctxt->i2_max_qscale) |
3651 | 0 | { |
3652 | 0 | i4_frame_qs_q3 = ps_rc_quant_ctxt->i2_max_qscale; |
3653 | 0 | } |
3654 | 668k | else if(i4_frame_qs_q3 < ps_rc_quant_ctxt->i2_min_qscale) |
3655 | 0 | { |
3656 | 0 | i4_frame_qs_q3 = ps_rc_quant_ctxt->i2_min_qscale; |
3657 | 0 | } |
3658 | | |
3659 | 668k | return (ps_rc_quant_ctxt->pi4_qscale_to_qp[i4_frame_qs_q3]); |
3660 | 668k | } |
3661 | | |
3662 | | /** |
3663 | | ****************************************************************************** |
3664 | | * |
3665 | | * @brief function to convert from qp to qscale |
3666 | | * |
3667 | | * @par Description |
3668 | | * @param[in] i4_frame_qp : QP value |
3669 | | * return value in qscale |
3670 | | ****************************************************************************** |
3671 | | */ |
3672 | | WORD32 ihevce_rc_get_scaled_mpeg2_qp(WORD32 i4_frame_qp, rc_quant_t *ps_rc_quant_ctxt) |
3673 | 836k | { |
3674 | | //i4_frame_qp = i4_frame_qp >> 3; // Q3 format is mantained for accuarate calc at lower qp |
3675 | 836k | WORD32 i4_qscale; |
3676 | 836k | if(i4_frame_qp > ps_rc_quant_ctxt->i2_max_qp) |
3677 | 302 | { |
3678 | 302 | i4_frame_qp = ps_rc_quant_ctxt->i2_max_qp; |
3679 | 302 | } |
3680 | 835k | else if(i4_frame_qp < ps_rc_quant_ctxt->i2_min_qp) |
3681 | 28.1k | { |
3682 | 28.1k | i4_frame_qp = ps_rc_quant_ctxt->i2_min_qp; |
3683 | 28.1k | } |
3684 | | |
3685 | 836k | i4_qscale = (ps_rc_quant_ctxt->pi4_qp_to_qscale[i4_frame_qp + ps_rc_quant_ctxt->i1_qp_offset] + |
3686 | 836k | (1 << (QSCALE_Q_FAC_3 - 1))) >> |
3687 | 836k | QSCALE_Q_FAC_3; |
3688 | 836k | return i4_qscale; |
3689 | 836k | } |
3690 | | |
3691 | | /** |
3692 | | ****************************************************************************** |
3693 | | * |
3694 | | * @brief function to convert from qp to qscale |
3695 | | * |
3696 | | * @par Description : This function maps logarithmic QP values to linear QP |
3697 | | * values. The linear values are represented in Q6 format. |
3698 | | * |
3699 | | * @param[in] i4_frame_qp : QP value (log scale) |
3700 | | * |
3701 | | * @return value in QP (linear scale) |
3702 | | * |
3703 | | ****************************************************************************** |
3704 | | */ |
3705 | | WORD32 ihevce_rc_get_scaled_mpeg2_qp_q6(WORD32 i4_frame_qp, UWORD8 u1_bit_depth) |
3706 | 557k | { |
3707 | 557k | WORD32 i4_frame_qp_q6; |
3708 | 557k | number_t s_frame_qp; |
3709 | 557k | float f_qp; |
3710 | | |
3711 | 557k | (void)u1_bit_depth; |
3712 | 557k | ASSERT(i4_frame_qp >= 0); |
3713 | 557k | ASSERT(i4_frame_qp <= 51 + ((u1_bit_depth - 8) * 6)); |
3714 | 557k | f_qp = (float)pow((float)2, ((float)(i4_frame_qp - 4) / 6)); |
3715 | 557k | convert_float_to_fix(f_qp, &s_frame_qp); |
3716 | 557k | convert_varq_to_fixq(s_frame_qp, &i4_frame_qp_q6, QSCALE_Q_FAC); |
3717 | | |
3718 | 557k | if(i4_frame_qp_q6 < (1 << QSCALE_Q_FAC)) |
3719 | 35.4k | i4_frame_qp_q6 = 1 << QSCALE_Q_FAC; |
3720 | | |
3721 | 557k | return i4_frame_qp_q6; |
3722 | 557k | } |
3723 | | |
3724 | | /** |
3725 | | ****************************************************************************** |
3726 | | * |
3727 | | * @brief function to convert from qscale to qp |
3728 | | * |
3729 | | * @par Description |
3730 | | * @param[in] i4_frame_qp_q6 : QP value in qscale. the input is assumed to be in q6 format |
3731 | | * return frame qp |
3732 | | ****************************************************************************** |
3733 | | */ |
3734 | | WORD32 ihevce_rc_get_scaled_hevce_qp_q6(WORD32 i4_frame_qp_q6, UWORD8 u1_bit_depth) |
3735 | 526k | { |
3736 | 526k | WORD32 i4_hevce_qp; |
3737 | 526k | number_t s_hevce_qp, s_temp; |
3738 | 526k | float f_mpeg2_qp, f_hevce_qp; |
3739 | 526k | f_mpeg2_qp = (float)i4_frame_qp_q6 / (1 << QSCALE_Q_FAC); |
3740 | 526k | f_hevce_qp = (6 * ((float)log(f_mpeg2_qp) / (float)log((float)2))) + 4; |
3741 | 526k | convert_float_to_fix(f_hevce_qp, &s_hevce_qp); |
3742 | | |
3743 | | /*rounf off to nearest integer*/ |
3744 | 526k | s_temp.sm = 1; |
3745 | 526k | s_temp.e = 1; |
3746 | 526k | add32_var_q(s_hevce_qp, s_temp, &s_hevce_qp); |
3747 | 526k | number_t_to_word32(s_hevce_qp, &i4_hevce_qp); |
3748 | 526k | if(i4_frame_qp_q6 == 0) |
3749 | 0 | { |
3750 | 0 | i4_hevce_qp = 0; |
3751 | 0 | } |
3752 | | |
3753 | 526k | i4_hevce_qp -= ((u1_bit_depth - 8) * 6); |
3754 | | |
3755 | 526k | return i4_hevce_qp; |
3756 | 526k | } |
3757 | | |
3758 | | /** |
3759 | | ****************************************************************************** |
3760 | | * |
3761 | | * @brief function to convert from qp scale to qp |
3762 | | * |
3763 | | * @par Description : This function maps linear QP values to logarithimic QP |
3764 | | * values. The linear values are represented in Q3 format. |
3765 | | * |
3766 | | * @param[in] i4_frame_qp : QP value (linear scale, Q3 mode) |
3767 | | * |
3768 | | * @return value in QP (log scale) |
3769 | | * |
3770 | | ****************************************************************************** |
3771 | | */ |
3772 | | WORD32 ihevce_rc_get_scaled_hevce_qp_q3(WORD32 i4_frame_qp, UWORD8 u1_bit_depth) |
3773 | 16.1M | { |
3774 | 16.1M | WORD32 i4_hevce_qp; |
3775 | 16.1M | number_t s_hevce_qp, s_temp; |
3776 | | |
3777 | 16.1M | if(i4_frame_qp == 0) |
3778 | 8.85k | { |
3779 | 8.85k | i4_hevce_qp = 0; |
3780 | 8.85k | } |
3781 | 16.1M | else |
3782 | 16.1M | { |
3783 | 16.1M | float f_mpeg2_qp, f_hevce_qp; |
3784 | | |
3785 | 16.1M | f_mpeg2_qp = (float)i4_frame_qp; |
3786 | 16.1M | f_hevce_qp = (6 * ((float)log(f_mpeg2_qp) / (float)log((float)2) - 3)) + 4; |
3787 | 16.1M | convert_float_to_fix(f_hevce_qp, &s_hevce_qp); |
3788 | | |
3789 | | /*rounf off to nearest integer*/ |
3790 | 16.1M | s_temp.sm = 1; |
3791 | 16.1M | s_temp.e = 1; |
3792 | 16.1M | add32_var_q(s_hevce_qp, s_temp, &s_hevce_qp); |
3793 | 16.1M | number_t_to_word32(s_hevce_qp, &i4_hevce_qp); |
3794 | 16.1M | } |
3795 | 16.1M | i4_hevce_qp -= ((u1_bit_depth - 8) * 6); |
3796 | | |
3797 | 16.1M | return i4_hevce_qp; |
3798 | 16.1M | } |
3799 | | |
3800 | | /*#######################################################*/ |
3801 | | /******* END OF QSCALE CONVERSION FUNCTIONS *************/ |
3802 | | /*######################################################*/ |
3803 | | |
3804 | | /*###############################################*/ |
3805 | | /******* START OF SET,GET FUNCTIONS *************/ |
3806 | | /*#############################################*/ |
3807 | | |
3808 | | /** |
3809 | | ****************************************************************************** |
3810 | | * |
3811 | | * @brief Convert pic type to rc pic type |
3812 | | * |
3813 | | * @par Description |
3814 | | * |
3815 | | * |
3816 | | * @param[in] pic_type |
3817 | | * Pic type |
3818 | | * |
3819 | | * @return rc_pic_type |
3820 | | * |
3821 | | ****************************************************************************** |
3822 | | */ |
3823 | | picture_type_e ihevce_rc_conv_pic_type( |
3824 | | IV_PICTURE_CODING_TYPE_T pic_type, |
3825 | | WORD32 i4_field_pic, |
3826 | | WORD32 i4_temporal_layer_id, |
3827 | | WORD32 i4_is_bottom_field, |
3828 | | WORD32 i4_top_field_first) |
3829 | 1.36M | { |
3830 | 1.36M | picture_type_e rc_pic_type = (picture_type_e)pic_type; |
3831 | | /*interlaced pictype are not supported*/ |
3832 | 1.36M | if(pic_type > 9 && i4_temporal_layer_id > 3) /**/ |
3833 | 0 | { |
3834 | 0 | DBG_PRINTF("unsupported picture type or temporal id\n"); |
3835 | 0 | exit(0); |
3836 | 0 | } |
3837 | | |
3838 | 1.36M | if(i4_field_pic == 0) /*Progressive Source*/ |
3839 | 1.36M | { |
3840 | 1.36M | if(pic_type == IV_IDR_FRAME) |
3841 | 299k | { |
3842 | 299k | rc_pic_type = I_PIC; |
3843 | 299k | } |
3844 | 1.06M | else |
3845 | 1.06M | { |
3846 | 1.06M | rc_pic_type = (picture_type_e)pic_type; |
3847 | | |
3848 | | /*return different picture type based on temporal layer*/ |
3849 | 1.06M | if(i4_temporal_layer_id > 1) |
3850 | 70.4k | { |
3851 | 70.4k | rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1)); |
3852 | 70.4k | } |
3853 | 1.06M | } |
3854 | 1.36M | } |
3855 | | |
3856 | 0 | else if(i4_field_pic == 1) |
3857 | 0 | { |
3858 | 0 | if(pic_type == IV_IDR_FRAME || pic_type == IV_I_FRAME) |
3859 | 0 | { |
3860 | 0 | rc_pic_type = I_PIC; |
3861 | 0 | } |
3862 | | |
3863 | 0 | else if(i4_top_field_first == 1) |
3864 | 0 | { |
3865 | 0 | rc_pic_type = (picture_type_e)pic_type; |
3866 | |
|
3867 | 0 | if(i4_temporal_layer_id <= 1) |
3868 | | |
3869 | 0 | { |
3870 | 0 | if(i4_is_bottom_field == 1) |
3871 | 0 | rc_pic_type = (picture_type_e)(pic_type + 4); |
3872 | 0 | } |
3873 | | /*return different picture type based on temporal layer*/ |
3874 | 0 | if(i4_temporal_layer_id > 1) |
3875 | 0 | { |
3876 | 0 | if(i4_is_bottom_field == 0) |
3877 | 0 | rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1)); |
3878 | 0 | else |
3879 | 0 | rc_pic_type = (picture_type_e)( |
3880 | 0 | pic_type + (i4_temporal_layer_id - 1) + |
3881 | 0 | 4); /*Offset of 4 for the bottomfield*/ |
3882 | 0 | } |
3883 | 0 | } |
3884 | 0 | else if(i4_top_field_first == 0) |
3885 | 0 | { |
3886 | 0 | rc_pic_type = (picture_type_e)pic_type; |
3887 | |
|
3888 | 0 | if(i4_temporal_layer_id <= 1) |
3889 | 0 | { |
3890 | 0 | if(i4_is_bottom_field == 1) |
3891 | 0 | rc_pic_type = (picture_type_e)(pic_type + 4); |
3892 | 0 | } |
3893 | | /*return different picture type based on temporal layer*/ |
3894 | 0 | if(i4_temporal_layer_id > 1) |
3895 | 0 | { |
3896 | 0 | if(i4_is_bottom_field == 0) |
3897 | 0 | rc_pic_type = (picture_type_e)(pic_type + (i4_temporal_layer_id - 1)); |
3898 | 0 | else |
3899 | 0 | rc_pic_type = (picture_type_e)( |
3900 | 0 | pic_type + (i4_temporal_layer_id - 1) + 4); /*Offset of 4 for the topfield*/ |
3901 | 0 | } |
3902 | 0 | } |
3903 | 0 | } |
3904 | | |
3905 | 1.36M | return rc_pic_type; |
3906 | 1.36M | } |
3907 | | |
3908 | | /** |
3909 | | ****************************************************************************** |
3910 | | * |
3911 | | * @brief function to update current frame intra cost |
3912 | | * |
3913 | | * @par Description |
3914 | | * @param[inout] ps_rc_ctxt |
3915 | | * @param[in] i8_cur_frm_intra_cost |
3916 | | ****************************************************************************** |
3917 | | */ |
3918 | | void ihevce_rc_update_cur_frm_intra_satd( |
3919 | | void *pv_ctxt, LWORD64 i8_cur_frm_intra_cost, WORD32 i4_enc_frm_id) |
3920 | 50.9k | { |
3921 | 50.9k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
3922 | 50.9k | ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id] = i8_cur_frm_intra_cost; |
3923 | 50.9k | } |
3924 | | /** |
3925 | | ****************************************************************************** |
3926 | | * |
3927 | | * @brief function to return scene type |
3928 | | * |
3929 | | * @par Description |
3930 | | * @param[inout] ps_rc_lap_out |
3931 | | * @return i4_rc_scene_type |
3932 | | ****************************************************************************** |
3933 | | */ |
3934 | | /* Functions dependent on lap input*/ |
3935 | | WORD32 ihevce_rc_lap_get_scene_type(rc_lap_out_params_t *ps_rc_lap_out) |
3936 | 229k | { |
3937 | 229k | return (WORD32)ps_rc_lap_out->i4_rc_scene_type; |
3938 | 229k | } |
3939 | | |
3940 | | /** |
3941 | | ****************************************************************************** |
3942 | | * |
3943 | | * @name ihevce_rc_get_pic_param |
3944 | | * |
3945 | | * @par Description |
3946 | | * |
3947 | | * @param[in] rc_pic_type |
3948 | | * |
3949 | | * @return void |
3950 | | * |
3951 | | ****************************************************************************** |
3952 | | */ |
3953 | | static void ihevce_rc_get_pic_param( |
3954 | | picture_type_e rc_pic_type, WORD32 *pi4_tem_lyr, WORD32 *pi4_is_bottom_field) |
3955 | 228k | { |
3956 | | /*bottom field determination*/ |
3957 | 228k | if(rc_pic_type >= P1_PIC) |
3958 | 0 | *pi4_is_bottom_field = 1; |
3959 | 228k | else |
3960 | 228k | *pi4_is_bottom_field = 0; |
3961 | | |
3962 | | /*temporal lyr id determination*/ |
3963 | 228k | if(rc_pic_type == I_PIC || rc_pic_type == P_PIC || rc_pic_type == P1_PIC) |
3964 | 202k | { |
3965 | 202k | *pi4_tem_lyr = 0; |
3966 | 202k | } |
3967 | 25.9k | else if(rc_pic_type == B_PIC || rc_pic_type == BB_PIC) |
3968 | 12.9k | { |
3969 | 12.9k | *pi4_tem_lyr = 1; |
3970 | 12.9k | } |
3971 | 12.9k | else if(rc_pic_type == B1_PIC || rc_pic_type == B11_PIC) |
3972 | 12.9k | { |
3973 | 12.9k | *pi4_tem_lyr = 2; |
3974 | 12.9k | } |
3975 | 0 | else if(rc_pic_type == B2_PIC || rc_pic_type == B22_PIC) |
3976 | 0 | { |
3977 | 0 | *pi4_tem_lyr = 3; |
3978 | 0 | } |
3979 | 0 | else |
3980 | 0 | { |
3981 | 0 | ASSERT(0); |
3982 | 0 | } |
3983 | 228k | } |
3984 | | /** |
3985 | | ****************************************************************************** |
3986 | | * |
3987 | | * @name ihevce_get_offline_index |
3988 | | * |
3989 | | * @par Description |
3990 | | * |
3991 | | * @param[in] ps_rc_ctxt - pointer to rc context |
3992 | | * |
3993 | | * @return index |
3994 | | * |
3995 | | ****************************************************************************** |
3996 | | */ |
3997 | | static WORD32 ihevce_get_offline_index(rc_context_t *ps_rc_ctxt, WORD32 i4_num_pels_in_frame) |
3998 | 543k | { |
3999 | 543k | WORD32 i4_rc_quality_preset = ps_rc_ctxt->i4_quality_preset; |
4000 | 543k | WORD32 base = 1; |
4001 | 543k | if(i4_num_pels_in_frame > 5000000) /*ultra HD*/ |
4002 | 0 | { |
4003 | 0 | base = 0; |
4004 | 0 | } |
4005 | 543k | else if(i4_num_pels_in_frame > 1500000) /*Full HD*/ |
4006 | 209 | { |
4007 | 209 | base = 5; |
4008 | 209 | } |
4009 | 543k | else if(i4_num_pels_in_frame > 600000) /*720p*/ |
4010 | 1.69k | { |
4011 | 1.69k | base = 10; |
4012 | 1.69k | } |
4013 | 541k | else /*SD*/ |
4014 | 541k | { |
4015 | 541k | base = 15; |
4016 | 541k | } |
4017 | | /*based on preset choose coeff*/ |
4018 | 543k | if(i4_rc_quality_preset == IHEVCE_QUALITY_P0) /*Pristine quality*/ |
4019 | 143k | { |
4020 | 143k | return base; |
4021 | 143k | } |
4022 | 399k | else if(i4_rc_quality_preset == IHEVCE_QUALITY_P2) /*High quality*/ |
4023 | 50.2k | { |
4024 | 50.2k | return base + 1; |
4025 | 50.2k | } |
4026 | 349k | else if( |
4027 | 349k | (i4_rc_quality_preset == IHEVCE_QUALITY_P5) || |
4028 | 349k | (i4_rc_quality_preset == IHEVCE_QUALITY_P6) || |
4029 | 349k | (i4_rc_quality_preset == IHEVCE_QUALITY_P7)) /*Extreme speed */ |
4030 | 188k | { |
4031 | 188k | return base + 4; |
4032 | 188k | } |
4033 | 161k | else if(i4_rc_quality_preset == IHEVCE_QUALITY_P4) /*High speed */ |
4034 | 29.2k | { |
4035 | 29.2k | return base + 3; |
4036 | 29.2k | } |
4037 | 132k | else if(i4_rc_quality_preset == IHEVCE_QUALITY_P3) /*default assume Medium speed*/ |
4038 | 132k | { |
4039 | 132k | return base + 2; |
4040 | 132k | } |
4041 | 0 | else |
4042 | 0 | { |
4043 | 0 | ASSERT(0); |
4044 | 0 | } |
4045 | 0 | return base + 2; |
4046 | 543k | } |
4047 | | |
4048 | | /** |
4049 | | ****************************************************************************** |
4050 | | * |
4051 | | * @name ihevce_get_frame_lambda_modifier |
4052 | | * |
4053 | | * @par Description |
4054 | | * |
4055 | | * @param[in] pic_type |
4056 | | * i4_rc_temporal_lyr_id |
4057 | | * @param[in] i4_first_field |
4058 | | * @param[in] i4_rc_is_ref_pic |
4059 | | * @return lambda_modifier |
4060 | | * |
4061 | | ****************************************************************************** |
4062 | | */ |
4063 | | static double ihevce_get_frame_lambda_modifier( |
4064 | | WORD8 pic_type, |
4065 | | WORD32 i4_rc_temporal_lyr_id, |
4066 | | WORD32 i4_first_field, |
4067 | | WORD32 i4_rc_is_ref_pic, |
4068 | | WORD32 i4_num_b_frms) |
4069 | 846k | { |
4070 | 846k | double lambda_modifier; |
4071 | 846k | WORD32 num_b_frms = i4_num_b_frms, first_field = i4_first_field; |
4072 | | |
4073 | 846k | if(I_PIC == pic_type) |
4074 | 429k | { |
4075 | 429k | double temporal_correction_islice = 1.0 - 0.05 * num_b_frms; |
4076 | 429k | temporal_correction_islice = MAX(0.5, temporal_correction_islice); |
4077 | | |
4078 | 429k | lambda_modifier = 0.57 * temporal_correction_islice; |
4079 | 429k | } |
4080 | 417k | else if(P_PIC == pic_type) |
4081 | 104k | { |
4082 | 104k | if(first_field) |
4083 | 104k | lambda_modifier = 0.442; //0.442*0.8; |
4084 | 0 | else |
4085 | 0 | lambda_modifier = 0.442; |
4086 | | |
4087 | | //lambda_modifier *= pow(2.00,(double)(1.00/3.00)); |
4088 | 104k | } |
4089 | 312k | else |
4090 | 312k | { |
4091 | | /* BSLICE */ |
4092 | 312k | if(1 == i4_rc_is_ref_pic) |
4093 | 104k | { |
4094 | 104k | lambda_modifier = 0.3536; |
4095 | 104k | } |
4096 | 208k | else if(2 == i4_rc_is_ref_pic) |
4097 | 0 | { |
4098 | 0 | lambda_modifier = 0.45; |
4099 | 0 | } |
4100 | 208k | else |
4101 | 208k | { |
4102 | 208k | lambda_modifier = 0.68; |
4103 | 208k | } |
4104 | | |
4105 | | /* TODO: Disable lambda modification for interlace encode to match HM runs */ |
4106 | | //if(0 == ps_enc_ctxt->s_runtime_src_prms.i4_field_pic) |
4107 | 312k | { |
4108 | | /* modify b lambda further based on temporal id */ |
4109 | 312k | if(i4_rc_temporal_lyr_id) |
4110 | 312k | { |
4111 | 312k | lambda_modifier *= 3.00; |
4112 | 312k | } |
4113 | 312k | } |
4114 | | //lambda_modifier *= pow(2.00,(double)((1.00/3.00) * (i4_rc_temporal_lyr_id + 1))); |
4115 | 312k | } |
4116 | | |
4117 | | /* modify the base lambda according to lambda modifier */ |
4118 | 846k | lambda_modifier = sqrt(lambda_modifier); |
4119 | 846k | return lambda_modifier; |
4120 | 846k | } |
4121 | | |
4122 | | /*! |
4123 | | ****************************************************************************** |
4124 | | * \if Function name : get_avg_bitrate_bufsize |
4125 | | * |
4126 | | * \brief |
4127 | | * |
4128 | | * \param[in] *pv_ctxt -> rc context |
4129 | | * |
4130 | | * \return |
4131 | | * |
4132 | | * \author |
4133 | | * Ittiam |
4134 | | * |
4135 | | ***************************************************************************** |
4136 | | */ |
4137 | | void get_avg_bitrate_bufsize(void *pv_ctxt, LWORD64 *pi8_bitrate, LWORD64 *pi8_ebf) |
4138 | 5.62k | { |
4139 | 5.62k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
4140 | 5.62k | *pi8_bitrate = rc_get_bit_rate(ps_rc_ctxt->rc_hdl); |
4141 | 5.62k | *pi8_ebf = rc_get_vbv_buf_size(ps_rc_ctxt->rc_hdl); |
4142 | 5.62k | } |
4143 | | |
4144 | | /** |
4145 | | ****************************************************************************** |
4146 | | * |
4147 | | * @name ihevce_get_dbf_buffer_size |
4148 | | * |
4149 | | * @par Description |
4150 | | * |
4151 | | * @param[in] ps_rc_ctxt - pointer to rc context |
4152 | | * |
4153 | | * @return qp |
4154 | | * |
4155 | | ****************************************************************************** |
4156 | | */ |
4157 | | void ihevce_get_dbf_buffer_size( |
4158 | | void *pv_rc_ctxt, UWORD32 *pi4_buffer_size, UWORD32 *pi4_dbf, UWORD32 *pi4_bit_rate) |
4159 | 114k | { |
4160 | 114k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
4161 | | |
4162 | 114k | pi4_buffer_size[0] = (WORD32)ps_rc_ctxt->s_vbv_compliance.f_buffer_size; |
4163 | 114k | pi4_dbf[0] = (WORD32)(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level); |
4164 | 114k | ASSERT( |
4165 | 114k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size >= |
4166 | 114k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level); |
4167 | | |
4168 | 114k | pi4_bit_rate[0] = (WORD32)ps_rc_ctxt->s_vbv_compliance.f_bit_rate; |
4169 | 114k | } |
4170 | | |
4171 | | /*! |
4172 | | ****************************************************************************** |
4173 | | * \if Function name : ihevce_set_L0_scd_qp |
4174 | | * |
4175 | | * \brief |
4176 | | * |
4177 | | * \param[in] *pv_ctxt -> rc context |
4178 | | * |
4179 | | * \return |
4180 | | * |
4181 | | * \author |
4182 | | * Ittiam |
4183 | | * |
4184 | | ***************************************************************************** |
4185 | | */ |
4186 | | void ihevce_set_L0_scd_qp(void *pv_rc_ctxt, WORD32 i4_scd_qp) |
4187 | 10.2k | { |
4188 | 10.2k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
4189 | | |
4190 | 10.2k | ps_rc_ctxt->i4_L0_frame_qp = i4_scd_qp; |
4191 | 10.2k | } |
4192 | | |
4193 | | /** |
4194 | | ****************************************************************************** |
4195 | | * |
4196 | | * @name rc_get_buffer_level_unclip |
4197 | | * |
4198 | | * @par Description |
4199 | | * |
4200 | | * @param[in] pv_rc_ctxt |
4201 | | * |
4202 | | * |
4203 | | * @return void |
4204 | | * |
4205 | | ****************************************************************************** |
4206 | | */ |
4207 | | float rc_get_buffer_level_unclip(void *pv_rc_ctxt) |
4208 | 0 | { |
4209 | 0 | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
4210 | 0 | return (ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip); |
4211 | 0 | } |
4212 | | |
4213 | | /** |
4214 | | ****************************************************************************** |
4215 | | * |
4216 | | * @brief Clip QP based on min and max frame qp |
4217 | | * |
4218 | | * @par Description |
4219 | | * |
4220 | | * @param[inout] ps_rc_ctxt |
4221 | | * pointer to rc context |
4222 | | * |
4223 | | * @param[in] rc_pic_type |
4224 | | * Pic type |
4225 | | * |
4226 | | * @return i4_hevc_frame_qp |
4227 | | * |
4228 | | ****************************************************************************** |
4229 | | */ |
4230 | | static WORD32 ihevce_clip_min_max_qp( |
4231 | | rc_context_t *ps_rc_ctxt, |
4232 | | WORD32 i4_hevc_frame_qp, |
4233 | | picture_type_e rc_pic_type, |
4234 | | WORD32 i4_rc_temporal_lyr_id) |
4235 | 913k | { |
4236 | 913k | ASSERT(i4_rc_temporal_lyr_id >= 0); |
4237 | | /**clip to min qp which is user configurable*/ |
4238 | 913k | if(rc_pic_type == I_PIC && i4_hevc_frame_qp < ps_rc_ctxt->i4_min_frame_qp) |
4239 | 0 | { |
4240 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp; |
4241 | 0 | } |
4242 | 913k | else if(rc_pic_type == P_PIC && i4_hevc_frame_qp < (ps_rc_ctxt->i4_min_frame_qp + 1)) |
4243 | 0 | { |
4244 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp + 1; |
4245 | 0 | } |
4246 | 913k | else if(i4_hevc_frame_qp < (ps_rc_ctxt->i4_min_frame_qp + i4_rc_temporal_lyr_id + 1)) |
4247 | 23.3k | { |
4248 | | /** For B frame max qp is set based on temporal reference*/ |
4249 | 23.3k | i4_hevc_frame_qp = ps_rc_ctxt->i4_min_frame_qp + i4_rc_temporal_lyr_id + 1; |
4250 | 23.3k | } |
4251 | | /* clip the Qp to MAX QP */ |
4252 | 913k | if(i4_hevc_frame_qp < ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qp) |
4253 | 0 | { |
4254 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_min_qp; |
4255 | 0 | } |
4256 | | /**clip to max qp based on pic type*/ |
4257 | 913k | if(rc_pic_type == I_PIC && i4_hevc_frame_qp > ps_rc_ctxt->i4_max_frame_qp) |
4258 | 0 | { |
4259 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp; |
4260 | 0 | } |
4261 | 913k | else if(rc_pic_type == P_PIC && i4_hevc_frame_qp > (ps_rc_ctxt->i4_max_frame_qp + 1)) |
4262 | 0 | { |
4263 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp + 1; |
4264 | 0 | } |
4265 | 913k | else if(i4_hevc_frame_qp > (ps_rc_ctxt->i4_max_frame_qp + i4_rc_temporal_lyr_id + 1)) |
4266 | 0 | { |
4267 | | /** For B frame max qp is set based on temporal reference*/ |
4268 | 0 | i4_hevc_frame_qp = ps_rc_ctxt->i4_max_frame_qp + i4_rc_temporal_lyr_id + 1; |
4269 | 0 | } |
4270 | | /* clip the Qp to MAX QP */ |
4271 | 913k | if(i4_hevc_frame_qp > ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp) |
4272 | 640 | { |
4273 | 640 | i4_hevc_frame_qp = ps_rc_ctxt->ps_rc_quant_ctxt->i2_max_qp; |
4274 | 640 | } |
4275 | 913k | return i4_hevc_frame_qp; |
4276 | 913k | } |
4277 | | |
4278 | | /*#############################################*/ |
4279 | | /******* END OF SET,GET FUNCTIONS *************/ |
4280 | | /*###########################################*/ |
4281 | | |
4282 | | /*#################################################*/ |
4283 | | /******* START OF RC UPDATE FUNCTIONS **************/ |
4284 | | /*#################################################*/ |
4285 | | |
4286 | | /** |
4287 | | ****************************************************************************** |
4288 | | * |
4289 | | * @brief updates the picture level information like bits consumed and |
4290 | | * |
4291 | | * @par Description |
4292 | | * |
4293 | | * @param[inout] ps_mem_tab |
4294 | | * pointer to memory descriptors table |
4295 | | * |
4296 | | * @param[in] ps_init_prms |
4297 | | * Create time static parameters |
4298 | | * |
4299 | | * @return void |
4300 | | * |
4301 | | ****************************************************************************** |
4302 | | */ |
4303 | | |
4304 | | void ihevce_rc_update_pic_info( |
4305 | | void *pv_ctxt, |
4306 | | UWORD32 u4_total_bits_consumed, |
4307 | | UWORD32 u4_total_header_bits, |
4308 | | UWORD32 u4_frame_sad, |
4309 | | UWORD32 u4_frame_intra_sad, |
4310 | | IV_PICTURE_CODING_TYPE_T pic_type, |
4311 | | WORD32 i4_avg_frame_hevc_qp, |
4312 | | WORD32 i4_suppress_bpic_update, |
4313 | | WORD32 *pi4_qp_normalized_8x8_cu_sum, |
4314 | | WORD32 *pi4_8x8_cu_sum, |
4315 | | LWORD64 *pi8_sad_by_qscale, |
4316 | | ihevce_lap_output_params_t *ps_lap_out, |
4317 | | rc_lap_out_params_t *ps_rc_lap_out, |
4318 | | WORD32 i4_buf_id, |
4319 | | UWORD32 u4_open_loop_intra_sad, |
4320 | | LWORD64 i8_total_ssd_frame, |
4321 | | WORD32 i4_enc_frm_id) |
4322 | 156k | { |
4323 | 156k | LWORD64 a_mb_type_sad[2]; |
4324 | 156k | WORD32 a_mb_type_tex_bits[2]; |
4325 | | /*dummy variables not used*/ |
4326 | 156k | WORD32 a_mb_in_type[2] = { 0, 0 }; |
4327 | 156k | LWORD64 a_mb_type_qp_q6[2] = { 0, 0 }; |
4328 | | /*qp accumulation at */ |
4329 | 156k | WORD32 i4_avg_activity = 250; //hardcoding to usual value |
4330 | 156k | WORD32 i4_intra_cost, i4_avg_frame_qp_q6, i; |
4331 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
4332 | 156k | WORD32 i4_frame_complexity, i4_bits_to_be_stuffed = 0, i4_is_last_frm_period = 0; |
4333 | 156k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
4334 | 156k | pic_type, |
4335 | 156k | ps_rc_ctxt->i4_field_pic, |
4336 | 156k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
4337 | 156k | ps_rc_lap_out->i4_is_bottom_field, |
4338 | 156k | ps_rc_ctxt->i4_top_field_first); |
4339 | 156k | frame_info_t s_frame_info; |
4340 | 156k | WORD32 i4_ctr = -1, i4_i, i4_j; |
4341 | 156k | WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM; |
4342 | | |
4343 | | /*update bit consumption. used only in rdopt*/ |
4344 | | //ASSERT(ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[ps_rc_ctxt->i4_rdopt_bit_count] == -1); |
4345 | | //ASSERT(i4_buf_id>=0); |
4346 | 156k | ps_rc_ctxt->ai4_rdopt_bit_consumption_estimate[ps_rc_ctxt->i4_rdopt_bit_count] = |
4347 | 156k | u4_total_bits_consumed; |
4348 | 156k | ps_rc_ctxt->ai4_rdopt_bit_consumption_buf_id[ps_rc_ctxt->i4_rdopt_bit_count] = i4_buf_id; |
4349 | 156k | ps_rc_ctxt->i4_rdopt_bit_count = |
4350 | 156k | (ps_rc_ctxt->i4_rdopt_bit_count + 1) % NUM_BUF_RDOPT_ENT_CORRECT; |
4351 | | |
4352 | 156k | { |
4353 | 156k | LWORD64 i8_texture_bits = u4_total_bits_consumed - u4_total_header_bits; |
4354 | 156k | ps_rc_lap_out->i4_use_offline_model_2pass = 0; |
4355 | | |
4356 | | /*flag to guide whether 2nd pass can use offline model or not*/ |
4357 | 156k | if((abs(ps_rc_lap_out->i4_orig_rc_qp - i4_avg_frame_hevc_qp) < 2) && |
4358 | 156k | (i8_texture_bits <= (ps_rc_lap_out->i8_est_text_bits * 2.0f)) && |
4359 | 156k | (i8_texture_bits >= (ps_rc_lap_out->i8_est_text_bits * 0.5f))) |
4360 | | |
4361 | 6.58k | { |
4362 | 6.58k | ps_rc_lap_out->i4_use_offline_model_2pass = 1; |
4363 | 6.58k | } |
4364 | 156k | } |
4365 | | /*Counter of number of bit alloction periods*/ |
4366 | 156k | if(rc_pic_type == I_PIC) |
4367 | 50.9k | ps_rc_ctxt |
4368 | 50.9k | ->i8_num_bit_alloc_period++; //Currently only I frame periods are considerd as bit allocation period (Ignoring non- I scd and complexity reset flag |
4369 | | /*initialze frame info*/ |
4370 | 156k | init_frame_info(&s_frame_info); |
4371 | 156k | s_frame_info.i4_rc_hevc_qp = i4_avg_frame_hevc_qp; |
4372 | 156k | s_frame_info.i4_num_entries++; |
4373 | 156k | s_frame_info.i8_L1_me_sad = ps_rc_lap_out->i8_raw_l1_coarse_me_sad; |
4374 | 156k | s_frame_info.i8_L1_ipe_raw_sad = ps_rc_lap_out->i8_raw_pre_intra_sad; |
4375 | 156k | s_frame_info.i4_num_entries++; |
4376 | 156k | s_frame_info.i4_num_entries++; |
4377 | 156k | s_frame_info.i8_L0_open_cost = (LWORD64)u4_open_loop_intra_sad; |
4378 | 156k | s_frame_info.i4_num_entries++; |
4379 | | |
4380 | 156k | if(rc_pic_type == I_PIC) |
4381 | 50.9k | s_frame_info.i8_L1_me_or_ipe_raw_sad = ps_rc_lap_out->i8_raw_pre_intra_sad; |
4382 | 105k | else |
4383 | 105k | s_frame_info.i8_L1_me_or_ipe_raw_sad = ps_rc_lap_out->i8_raw_l1_coarse_me_sad; |
4384 | 156k | s_frame_info.i4_num_entries++; |
4385 | 156k | s_frame_info.i4_poc = ps_rc_lap_out->i4_rc_poc; |
4386 | 156k | s_frame_info.i4_num_entries++; |
4387 | 156k | s_frame_info.i4_scene_type = ps_rc_lap_out->i4_rc_scene_type; |
4388 | 156k | s_frame_info.i4_num_entries++; |
4389 | 156k | s_frame_info.i4_non_i_scd = ps_rc_lap_out->i4_is_non_I_scd || ps_rc_lap_out->i4_is_I_only_scd; |
4390 | 156k | s_frame_info.i4_num_entries++; |
4391 | 156k | s_frame_info.i8_cl_sad = u4_frame_sad; |
4392 | 156k | s_frame_info.i4_num_entries++; |
4393 | 156k | s_frame_info.i8_header_bits = u4_total_header_bits; |
4394 | 156k | s_frame_info.i4_num_entries++; |
4395 | 156k | s_frame_info.i8_tex_bits = u4_total_bits_consumed - u4_total_header_bits; |
4396 | 156k | s_frame_info.i4_num_entries++; |
4397 | 156k | s_frame_info.e_pic_type = rc_pic_type; |
4398 | 156k | s_frame_info.i4_num_entries++; |
4399 | 156k | s_frame_info.i8_est_texture_bits = ps_rc_lap_out->i8_est_text_bits; |
4400 | 156k | s_frame_info.i4_num_entries++; |
4401 | 156k | s_frame_info.i4_lap_complexity_q7 = ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id]; |
4402 | 156k | s_frame_info.i4_num_entries++; |
4403 | 156k | s_frame_info.i4_lap_f_sim = ps_rc_ctxt->ai4_lap_f_sim[i4_enc_frm_id]; |
4404 | 156k | s_frame_info.i4_num_entries++; |
4405 | 156k | s_frame_info.i8_frame_acc_coarse_me_cost = ps_rc_lap_out->i8_frame_acc_coarse_me_cost; |
4406 | 156k | s_frame_info.i4_num_entries++; |
4407 | 156k | s_frame_info.i_to_avg_bit_ratio = ps_rc_ctxt->ai_to_avg_bit_ratio[i4_enc_frm_id]; |
4408 | 156k | s_frame_info.i4_num_entries++; |
4409 | 156k | s_frame_info.i4_num_scd_in_lap_window = ps_rc_ctxt->ai4_num_scd_in_lap_window[i4_enc_frm_id]; |
4410 | 156k | s_frame_info.i4_num_entries++; |
4411 | 156k | s_frame_info.i4_num_frames_b4_scd = ps_rc_ctxt->ai4_num_frames_b4_scd[i4_enc_frm_id]; |
4412 | 156k | s_frame_info.i4_num_entries++; |
4413 | 156k | s_frame_info.i8_num_bit_alloc_period = ps_rc_ctxt->i8_num_bit_alloc_period; |
4414 | 156k | s_frame_info.i4_num_entries++; |
4415 | 156k | s_frame_info.i1_is_complexity_based_bits_reset = |
4416 | 156k | (WORD8)ps_rc_lap_out->i4_is_cmplx_change_reset_bits; |
4417 | 156k | s_frame_info.i4_num_entries++; |
4418 | | /*For the complexity based movement in 2nd pass*/ |
4419 | 156k | memmove( |
4420 | 156k | (void *)s_frame_info.af_sum_weigh, |
4421 | 156k | ps_rc_lap_out->ps_frame_info->af_sum_weigh, |
4422 | 156k | sizeof(float) * MAX_PIC_TYPE * 3); |
4423 | 156k | s_frame_info.i4_num_entries++; |
4424 | | |
4425 | | /*store frame qp to clip qp accordingly*/ |
4426 | 156k | if(ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated) |
4427 | 156k | { |
4428 | 156k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type] = i4_avg_frame_hevc_qp; |
4429 | 156k | } |
4430 | | |
4431 | 270k | for(i4_i = 0; i4_i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i4_i++) |
4432 | 270k | { |
4433 | 270k | if(ps_rc_lap_out->u4_rc_scene_num == ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_i]) |
4434 | 156k | { |
4435 | 156k | i4_ctr = i4_i; |
4436 | 156k | break; |
4437 | 156k | } |
4438 | 270k | } |
4439 | 156k | if(-1 == i4_ctr) |
4440 | 0 | { |
4441 | 0 | ps_rc_ctxt->i4_prev_qp_ctr++; |
4442 | 0 | ps_rc_ctxt->i4_prev_qp_ctr = ps_rc_ctxt->i4_prev_qp_ctr % MAX_NON_REF_B_PICS_IN_QUEUE_SGI; |
4443 | 0 | i4_ctr = ps_rc_ctxt->i4_prev_qp_ctr; |
4444 | 0 | ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_ctr] = ps_rc_lap_out->u4_rc_scene_num; |
4445 | 0 | for(i4_j = 0; i4_j < MAX_PIC_TYPE; i4_j++) |
4446 | 0 | { |
4447 | 0 | ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][i4_j] = 0; |
4448 | 0 | } |
4449 | 0 | } |
4450 | | |
4451 | 156k | { |
4452 | 156k | ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][rc_pic_type] = |
4453 | 156k | i4_avg_frame_hevc_qp; |
4454 | 156k | } |
4455 | 156k | if(i4_scene_num < HALF_MAX_SCENE_ARRAY_QP) |
4456 | 156k | { |
4457 | 156k | WORD32 i4_i; |
4458 | 156k | ps_rc_ctxt->ai4_scene_numbers[i4_scene_num + HALF_MAX_SCENE_ARRAY_QP] = 0; |
4459 | 1.56M | for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++) |
4460 | 1.40M | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num + HALF_MAX_SCENE_ARRAY_QP][i4_i] = |
4461 | 1.40M | INIT_HEVCE_QP_RC; |
4462 | 156k | } |
4463 | 0 | else |
4464 | 0 | { |
4465 | 0 | WORD32 i4_i; |
4466 | 0 | ps_rc_ctxt->ai4_scene_numbers[i4_scene_num - HALF_MAX_SCENE_ARRAY_QP] = 0; |
4467 | 0 | for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++) |
4468 | 0 | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num - HALF_MAX_SCENE_ARRAY_QP][i4_i] = |
4469 | 0 | INIT_HEVCE_QP_RC; |
4470 | 0 | } |
4471 | | |
4472 | | /*update will have HEVC qp, convert it back to mpeg2 range qp for all internal calculations of RC*/ |
4473 | | |
4474 | 156k | i4_avg_frame_qp_q6 = ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor |
4475 | 156k | [i4_avg_frame_hevc_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset]; |
4476 | | |
4477 | 156k | if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) |
4478 | 50.9k | { |
4479 | | /*TODO : Take care of precision of a_mb_type_sad*/ |
4480 | 50.9k | a_mb_type_sad[0] = |
4481 | 50.9k | (((pi8_sad_by_qscale[1] * i4_avg_frame_qp_q6) + |
4482 | 50.9k | (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >> |
4483 | 50.9k | (SAD_BY_QSCALE_Q + QSCALE_Q_FAC)); //u4_frame_sad; |
4484 | | |
4485 | 50.9k | a_mb_type_sad[1] = |
4486 | 50.9k | (((pi8_sad_by_qscale[0] * i4_avg_frame_qp_q6) + |
4487 | 50.9k | (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >> |
4488 | 50.9k | (SAD_BY_QSCALE_Q + QSCALE_Q_FAC)); |
4489 | 50.9k | a_mb_type_tex_bits[0] = |
4490 | 50.9k | u4_total_bits_consumed - u4_total_header_bits; //(u4_total_bits_consumed >> 3); |
4491 | 50.9k | a_mb_type_tex_bits[1] = 0; |
4492 | 50.9k | a_mb_in_type[0] = (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 8; |
4493 | 50.9k | a_mb_in_type[1] = 0; |
4494 | 50.9k | } |
4495 | 105k | else |
4496 | 105k | { |
4497 | | /*TODO : Take care of precision of a_mb_type_sad*/ |
4498 | 105k | a_mb_type_sad[1] = |
4499 | 105k | (((pi8_sad_by_qscale[0] * i4_avg_frame_qp_q6) + |
4500 | 105k | (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >> |
4501 | 105k | (SAD_BY_QSCALE_Q + QSCALE_Q_FAC)); |
4502 | | |
4503 | 105k | a_mb_type_tex_bits[0] = |
4504 | 105k | u4_total_bits_consumed - u4_total_header_bits; //(u4_total_bits_consumed >> 3); |
4505 | 105k | a_mb_type_sad[0] = |
4506 | 105k | (((pi8_sad_by_qscale[1] * i4_avg_frame_qp_q6) + |
4507 | 105k | (((LWORD64)1) << (SAD_BY_QSCALE_Q + QSCALE_Q_FAC - 1))) >> |
4508 | 105k | (SAD_BY_QSCALE_Q + QSCALE_Q_FAC)); //u4_frame_sad; |
4509 | 105k | a_mb_type_tex_bits[1] = |
4510 | 105k | u4_total_bits_consumed - u4_total_header_bits; //(u4_total_bits_consumed >> 3); |
4511 | 105k | a_mb_type_tex_bits[0] = 0; |
4512 | 105k | a_mb_in_type[1] = (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 8; |
4513 | 105k | a_mb_in_type[0] = 0; |
4514 | 105k | } |
4515 | 156k | ASSERT(a_mb_type_sad[0] >= 0); |
4516 | 156k | ASSERT(a_mb_type_sad[1] >= 0); |
4517 | | /*THis calclates sum of Qps of all MBs as per the corresponding mb type*/ |
4518 | | /*THis is different from a_mb_in_type,a_mb_type_sad and a_mb_type_tex_bits*/ |
4519 | 156k | a_mb_type_qp_q6[0] = ((LWORD64)i4_avg_frame_qp_q6) * a_mb_in_type[0]; |
4520 | 156k | a_mb_type_qp_q6[1] = ((LWORD64)i4_avg_frame_qp_q6) * a_mb_in_type[1]; |
4521 | 156k | { |
4522 | 156k | WORD32 i4_avg_qp_q6_without_offset = 0, i4_hevc_qp_rc = i4_avg_frame_hevc_qp; |
4523 | 156k | WORD32 i4_rc_pic_type_rc_for_offset = rc_pic_type; |
4524 | 156k | if(i4_rc_pic_type_rc_for_offset > B2_PIC) |
4525 | 0 | i4_rc_pic_type_rc_for_offset = i4_rc_pic_type_rc_for_offset - B2_PIC; |
4526 | 156k | i4_hevc_qp_rc = i4_hevc_qp_rc - ps_rc_lap_out->ai4_offsets[i4_rc_pic_type_rc_for_offset] + |
4527 | 156k | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset; |
4528 | | |
4529 | 156k | i4_hevc_qp_rc = |
4530 | 156k | CLIP3(i4_hevc_qp_rc, 1, MAX_HEVC_QP + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset); |
4531 | 156k | i4_avg_qp_q6_without_offset = |
4532 | 156k | ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor[i4_hevc_qp_rc]; |
4533 | | |
4534 | | /*Store the HBD qscale with and without accounting for offset*/ |
4535 | 156k | s_frame_info.f_hbd_q_scale_without_offset = |
4536 | 156k | (float)i4_avg_qp_q6_without_offset / (1 << QSCALE_Q_FAC); |
4537 | 156k | s_frame_info.f_hbd_q_scale = (float)i4_avg_frame_qp_q6 / (1 << QSCALE_Q_FAC); |
4538 | 156k | s_frame_info.i4_num_entries++; |
4539 | 156k | s_frame_info.i4_num_entries++; |
4540 | | |
4541 | | /*Store the 8 bit qscale with and without accounting for offset*/ |
4542 | | /*Can be useful for pre-enc stage*/ |
4543 | 156k | if(ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset != 0) |
4544 | 0 | { |
4545 | 0 | s_frame_info.f_8bit_q_scale_without_offset = |
4546 | 0 | s_frame_info.f_hbd_q_scale_without_offset / (1 << (ps_rc_ctxt->u1_bit_depth - 8)); |
4547 | 0 | s_frame_info.f_8bit_q_scale = |
4548 | 0 | s_frame_info.f_hbd_q_scale / (1 << (ps_rc_ctxt->u1_bit_depth - 8)); |
4549 | 0 | } |
4550 | 156k | else |
4551 | 156k | { |
4552 | 156k | s_frame_info.f_8bit_q_scale_without_offset = s_frame_info.f_hbd_q_scale_without_offset; |
4553 | 156k | s_frame_info.f_8bit_q_scale = s_frame_info.f_hbd_q_scale; |
4554 | 156k | } |
4555 | 156k | s_frame_info.i4_num_entries++; |
4556 | 156k | s_frame_info.i4_num_entries++; |
4557 | 156k | } |
4558 | | |
4559 | | /*making intra cost same as ssd as of now*/ |
4560 | 156k | i4_intra_cost = u4_frame_intra_sad; |
4561 | | |
4562 | | /* Handling bits stuffing and skips */ |
4563 | 156k | { |
4564 | 156k | WORD32 i4_num_bits_to_prevent_vbv_underflow; |
4565 | 156k | vbv_buf_status_e vbv_buffer_status; |
4566 | 156k | vbv_buffer_status = get_buffer_status( |
4567 | 156k | ps_rc_ctxt->rc_hdl, |
4568 | 156k | u4_total_bits_consumed, |
4569 | 156k | rc_pic_type, //the picture type convention is different in buffer handling |
4570 | 156k | &i4_num_bits_to_prevent_vbv_underflow); |
4571 | | |
4572 | 156k | if(vbv_buffer_status == VBV_UNDERFLOW) |
4573 | 0 | { |
4574 | 0 | } |
4575 | 156k | if(vbv_buffer_status == VBV_OVERFLOW) |
4576 | 23.2k | { |
4577 | 23.2k | i4_bits_to_be_stuffed = |
4578 | 23.2k | get_bits_to_stuff(ps_rc_ctxt->rc_hdl, u4_total_bits_consumed, rc_pic_type); |
4579 | | //i4_bits_to_be_stuffed = 0;/*STORAGE_RC*/ |
4580 | 23.2k | } |
4581 | 156k | } |
4582 | 156k | { |
4583 | 156k | WORD32 ai4_sad[MAX_PIC_TYPE], i4_valid_sad_entry = 0; |
4584 | 156k | UWORD32 u4_avg_sad = 0; |
4585 | | |
4586 | | /*calculate frame complexity. Given same content frame complexity should not vary across I,P and Bpic. Hence frame complexity is calculated |
4587 | | based on average of all pic types SAD*/ |
4588 | 156k | if(rc_pic_type == I_PIC) |
4589 | 50.9k | { |
4590 | 50.9k | ai4_sad[I_PIC] = u4_frame_intra_sad; |
4591 | 50.9k | } |
4592 | 105k | else |
4593 | 105k | { |
4594 | | /*call to get previous I-PIC sad*/ |
4595 | 105k | rc_get_sad(ps_rc_ctxt->rc_hdl, &ai4_sad[0]); |
4596 | 105k | } |
4597 | | |
4598 | | /*since intra sad is not available for every frame use previous I pic intra frame SAD*/ |
4599 | 156k | rc_put_sad(ps_rc_ctxt->rc_hdl, ai4_sad[I_PIC], u4_frame_sad, rc_pic_type); |
4600 | 156k | rc_get_sad(ps_rc_ctxt->rc_hdl, &ai4_sad[0]); |
4601 | | /*for first few frame valid SAD is not available. This will make sure invalid data is not used*/ |
4602 | 156k | if(ps_rc_ctxt->i4_field_pic == 0) |
4603 | 156k | { |
4604 | 491k | for(i = 0; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
4605 | 335k | { |
4606 | 335k | if(ai4_sad[i] >= 0) |
4607 | 318k | { |
4608 | 318k | u4_avg_sad += ai4_sad[i]; |
4609 | 318k | i4_valid_sad_entry++; |
4610 | 318k | } |
4611 | 335k | } |
4612 | 156k | } |
4613 | 0 | else /*for field case*/ |
4614 | 0 | { |
4615 | 0 | if(ai4_sad[0] >= 0) |
4616 | 0 | { |
4617 | 0 | u4_avg_sad += ai4_sad[0]; |
4618 | 0 | i4_valid_sad_entry++; |
4619 | 0 | } |
4620 | |
|
4621 | 0 | for(i = 1; i < ps_rc_ctxt->i4_num_active_pic_type; i++) |
4622 | 0 | { |
4623 | 0 | if(ai4_sad[i] >= 0) |
4624 | 0 | { |
4625 | 0 | u4_avg_sad += ai4_sad[i]; |
4626 | 0 | i4_valid_sad_entry++; |
4627 | 0 | } |
4628 | |
|
4629 | 0 | if(ai4_sad[i + FIELD_OFFSET] >= 0) |
4630 | 0 | { |
4631 | 0 | u4_avg_sad += ai4_sad[i + FIELD_OFFSET]; |
4632 | 0 | i4_valid_sad_entry++; |
4633 | 0 | } |
4634 | 0 | } |
4635 | 0 | } |
4636 | | |
4637 | 156k | if(i4_valid_sad_entry > 0) |
4638 | 156k | { |
4639 | 156k | i4_frame_complexity = |
4640 | 156k | (u4_avg_sad) / |
4641 | 156k | (i4_valid_sad_entry * (ps_rc_ctxt->i4_frame_width * ps_rc_ctxt->i4_frame_height)); |
4642 | 156k | } |
4643 | 0 | else |
4644 | 0 | { |
4645 | 0 | i4_frame_complexity = 1; |
4646 | 0 | } |
4647 | 156k | } |
4648 | 156k | ASSERT(i4_frame_complexity >= 0); |
4649 | | /*I_model only reset In case of fade-in and fade-out*/ |
4650 | 156k | if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id]) |
4651 | 0 | { |
4652 | 0 | ASSERT(rc_pic_type == I_PIC); |
4653 | 0 | rc_reset_pic_model(ps_rc_ctxt->rc_hdl, I_PIC); |
4654 | 0 | ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 0; |
4655 | 0 | } |
4656 | | |
4657 | | /*check if next picture is I frame, both scene cuts and I pictures are treated as end of period*/ |
4658 | 156k | { |
4659 | 156k | if(ps_rc_lap_out->i4_rc_pic_type != -1 && ps_rc_lap_out->i4_rc_scene_type != -1) |
4660 | 156k | { |
4661 | 156k | if(ps_rc_ctxt->u4_intra_frame_interval != 1) |
4662 | 138k | { |
4663 | | /*TBD: For second pass this should be only criteria, While merging to latest verison make sure non - I SCD is not considered as one of the condition*/ |
4664 | 138k | i4_is_last_frm_period = (WORD32)( |
4665 | 138k | ps_rc_lap_out->i4_next_pic_type == IV_IDR_FRAME || |
4666 | 138k | ps_rc_lap_out->i4_next_pic_type == IV_I_FRAME); |
4667 | 138k | } |
4668 | 17.8k | else |
4669 | 17.8k | { |
4670 | 17.8k | i4_is_last_frm_period = |
4671 | 17.8k | (WORD32)(ps_rc_lap_out->i4_next_scene_type == SCENE_TYPE_SCENE_CUT); |
4672 | 17.8k | } |
4673 | 156k | } |
4674 | | |
4675 | | /*In two pass only I frame ending should be considered end of period, otherwise complexity changes should be allowed to reset model in CBR and VBR modes*/ |
4676 | 156k | if(ps_rc_ctxt->i4_rc_pass != 2) |
4677 | 156k | i4_is_last_frm_period = i4_is_last_frm_period || |
4678 | 156k | ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id]; |
4679 | 156k | } |
4680 | | |
4681 | 156k | #if 1 //FRAME_PARALLEL_LVL //ELP_RC |
4682 | 156k | ps_rc_ctxt->i4_est_text_bits_ctr_update_qp++; |
4683 | 156k | ps_rc_ctxt->i4_est_text_bits_ctr_update_qp = |
4684 | 156k | (ps_rc_ctxt->i4_est_text_bits_ctr_update_qp % (ps_rc_ctxt->i4_num_frame_parallel)); |
4685 | 156k | #endif |
4686 | | |
4687 | 156k | update_frame_level_info( |
4688 | 156k | ps_rc_ctxt->rc_hdl, |
4689 | 156k | rc_pic_type, |
4690 | 156k | a_mb_type_sad, |
4691 | 156k | u4_total_bits_consumed, /*total bits consumed by frame*/ |
4692 | 156k | u4_total_header_bits, |
4693 | 156k | a_mb_type_tex_bits, |
4694 | 156k | a_mb_type_qp_q6, /*sum of qp of all mb in frame, since no ctb level modulation*/ |
4695 | 156k | a_mb_in_type, |
4696 | 156k | i4_avg_activity, |
4697 | 156k | ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id], /*currenlty SCD is not enabled*/ |
4698 | 156k | 0, /*not a pre encode skip*/ |
4699 | 156k | i4_intra_cost, |
4700 | 156k | 0, |
4701 | 156k | ps_rc_lap_out |
4702 | 156k | ->i4_ignore_for_rc_update, /*HEVC_hierarchy: do not supress update for non-ref B pic*/ |
4703 | 156k | i4_bits_to_be_stuffed, |
4704 | 156k | (ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] || |
4705 | 156k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] || |
4706 | 156k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id]), |
4707 | 156k | ps_rc_ctxt->ai4_lap_complexity_q7[i4_enc_frm_id], |
4708 | 156k | i4_is_last_frm_period, |
4709 | 156k | ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id], |
4710 | 156k | &s_frame_info, |
4711 | 156k | ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated, |
4712 | 156k | ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset, |
4713 | 156k | i4_scene_num, |
4714 | 156k | ps_rc_ctxt->ai4_scene_numbers[i4_scene_num], |
4715 | 156k | ps_rc_ctxt->i4_est_text_bits_ctr_update_qp); |
4716 | | /** reset flags valid for only one frame*/ |
4717 | 156k | ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id] = 0; |
4718 | 156k | ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] = 0; |
4719 | 156k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] = 0; |
4720 | 156k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id] = 0; |
4721 | 156k | ps_rc_ctxt->ai4_is_cmplx_change_reset_bits[i4_enc_frm_id] = 0; |
4722 | | |
4723 | 156k | ps_rc_ctxt->i4_is_first_frame_encoded = 1; |
4724 | | |
4725 | | /** update the scene num for current frame*/ |
4726 | 156k | ps_rc_ctxt->au4_scene_num_temp_id[ps_rc_lap_out->i4_rc_temporal_lyr_id] = |
4727 | 156k | ps_rc_lap_out->u4_rc_scene_num; |
4728 | | |
4729 | 156k | if(ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id]) |
4730 | 0 | { |
4731 | | /*reset pre-enc SAD whenever SCD is detected so that it does not detect scene cut for other pictures*/ |
4732 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
4733 | 0 | { |
4734 | 0 | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1; |
4735 | 0 | } |
4736 | 0 | } |
4737 | | |
4738 | | /*remember i frame's cost metric to scale SAD of next of I frame*/ |
4739 | 156k | if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) |
4740 | 50.9k | { |
4741 | 50.9k | ps_rc_ctxt->i8_prev_i_frm_cost = ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id]; |
4742 | 50.9k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] = |
4743 | 50.9k | ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id]; |
4744 | 50.9k | } |
4745 | | /*for other picture types update hme cost*/ |
4746 | 105k | else |
4747 | 105k | { |
4748 | 105k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] = |
4749 | 105k | ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id]; |
4750 | 105k | } |
4751 | 156k | } |
4752 | | /*! |
4753 | | ****************************************************************************** |
4754 | | * \if Function name : ihevce_rc_interface_update \endif |
4755 | | * |
4756 | | * \brief |
4757 | | * Updating rate control interface parameters after the query call. |
4758 | | * |
4759 | | * \param[in] Rate control interface context, |
4760 | | * Picture Type |
4761 | | * Lap out structure pointer |
4762 | | * |
4763 | | * |
4764 | | * \return |
4765 | | * None |
4766 | | * |
4767 | | * \author Ittiam |
4768 | | * Ittiam |
4769 | | * |
4770 | | ***************************************************************************** |
4771 | | */ |
4772 | | void ihevce_rc_interface_update( |
4773 | | void *pv_ctxt, |
4774 | | IV_PICTURE_CODING_TYPE_T pic_type, |
4775 | | rc_lap_out_params_t *ps_rc_lap_out, |
4776 | | WORD32 i4_avg_frame_hevc_qp, |
4777 | | WORD32 i4_enc_frm_id) |
4778 | 303k | { |
4779 | 303k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
4780 | 303k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
4781 | 303k | pic_type, |
4782 | 303k | ps_rc_ctxt->i4_field_pic, |
4783 | 303k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
4784 | 303k | ps_rc_lap_out->i4_is_bottom_field, |
4785 | 303k | ps_rc_ctxt->i4_top_field_first); |
4786 | 303k | WORD32 i; |
4787 | 303k | WORD32 i4_avg_frame_qp_q6, i4_ctr = -1, i4_i, i4_j; |
4788 | 303k | WORD32 i4_scene_num = ps_rc_lap_out->u4_rc_scene_num % MAX_SCENE_NUM; |
4789 | | |
4790 | | /*store frame qp to clip qp accordingly*/ |
4791 | 303k | if(ps_rc_lap_out->i4_is_rc_model_needs_to_be_updated) |
4792 | 303k | { |
4793 | 303k | WORD32 i4_i, i4_temp_i_qp, i4_temp_qp; |
4794 | 303k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][rc_pic_type] = i4_avg_frame_hevc_qp; |
4795 | 303k | ps_rc_ctxt->ai4_scene_numbers[i4_scene_num]++; |
4796 | | |
4797 | 303k | if(rc_pic_type < P1_PIC) |
4798 | 303k | i4_temp_i_qp = i4_avg_frame_hevc_qp - rc_pic_type; |
4799 | 0 | else |
4800 | 0 | i4_temp_i_qp = i4_avg_frame_hevc_qp - rc_pic_type + 4; |
4801 | | |
4802 | 303k | i4_temp_i_qp = ihevce_clip_min_max_qp(ps_rc_ctxt, i4_temp_i_qp, I_PIC, 0); |
4803 | | |
4804 | 303k | if(ps_rc_ctxt->ai4_scene_numbers[i4_scene_num] == 1) |
4805 | 8.80k | { |
4806 | 52.8k | for(i4_i = 0; i4_i < 5; i4_i++) |
4807 | 44.0k | { |
4808 | 44.0k | if(ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i] == INIT_HEVCE_QP_RC) |
4809 | 35.2k | { |
4810 | 35.2k | i4_temp_qp = i4_temp_i_qp + i4_i; |
4811 | 35.2k | i4_temp_qp = ihevce_clip_min_max_qp( |
4812 | 35.2k | ps_rc_ctxt, i4_temp_qp, (picture_type_e)i4_i, MAX(i4_i - 1, 0)); |
4813 | 35.2k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i] = i4_temp_qp; |
4814 | | |
4815 | 35.2k | if(i4_i > 0) |
4816 | 35.2k | ps_rc_ctxt->ai4_prev_pic_hevc_qp[i4_scene_num][i4_i + 4] = i4_temp_qp; |
4817 | 35.2k | } |
4818 | 44.0k | } |
4819 | 8.80k | } |
4820 | 303k | } |
4821 | | |
4822 | 604k | for(i4_i = 0; i4_i < MAX_NON_REF_B_PICS_IN_QUEUE_SGI; i4_i++) |
4823 | 599k | { |
4824 | 599k | if(ps_rc_lap_out->u4_rc_scene_num == ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_i]) |
4825 | 298k | { |
4826 | 298k | i4_ctr = i4_i; |
4827 | 298k | break; |
4828 | 298k | } |
4829 | 599k | } |
4830 | 303k | if(-1 == i4_ctr) |
4831 | 5.13k | { |
4832 | 5.13k | ps_rc_ctxt->i4_prev_qp_ctr++; |
4833 | 5.13k | ps_rc_ctxt->i4_prev_qp_ctr = ps_rc_ctxt->i4_prev_qp_ctr % MAX_NON_REF_B_PICS_IN_QUEUE_SGI; |
4834 | 5.13k | i4_ctr = ps_rc_ctxt->i4_prev_qp_ctr; |
4835 | 5.13k | ps_rc_ctxt->au4_prev_scene_num_multi_scene[i4_ctr] = ps_rc_lap_out->u4_rc_scene_num; |
4836 | 51.3k | for(i4_j = 0; i4_j < MAX_PIC_TYPE; i4_j++) |
4837 | 46.2k | { |
4838 | 46.2k | ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][i4_j] = 0; |
4839 | 46.2k | } |
4840 | 5.13k | } |
4841 | | |
4842 | 303k | { |
4843 | 303k | ps_rc_ctxt->ai4_qp_for_previous_scene_multi_scene[i4_ctr][rc_pic_type] = |
4844 | 303k | i4_avg_frame_hevc_qp; |
4845 | 303k | } |
4846 | | |
4847 | | /*I_model only reset In case of fade-in and fade-out*/ |
4848 | 303k | if(ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id]) |
4849 | 3.53k | { |
4850 | 3.53k | ASSERT(rc_pic_type == I_PIC); |
4851 | 3.53k | rc_reset_pic_model(ps_rc_ctxt->rc_hdl, I_PIC); |
4852 | 3.53k | ps_rc_ctxt->ai4_I_model_only_reset[i4_enc_frm_id] = 0; |
4853 | 3.53k | } |
4854 | | |
4855 | 303k | i4_avg_frame_qp_q6 = ps_rc_ctxt->ps_rc_quant_ctxt->pi4_qp_to_qscale_q_factor |
4856 | 303k | [i4_avg_frame_hevc_qp + ps_rc_ctxt->ps_rc_quant_ctxt->i1_qp_offset]; |
4857 | | |
4858 | 303k | update_frame_rc_get_frame_qp_info( |
4859 | 303k | ps_rc_ctxt->rc_hdl, |
4860 | 303k | rc_pic_type, |
4861 | 303k | ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id], |
4862 | 303k | (ps_rc_ctxt->ai4_is_pause_to_resume[i4_enc_frm_id] || |
4863 | 303k | ps_rc_ctxt->ai4_is_non_I_scd_pic[i4_enc_frm_id] || |
4864 | 303k | ps_rc_ctxt->ai4_is_cmplx_change_reset_model[i4_enc_frm_id]), |
4865 | 303k | i4_avg_frame_qp_q6, |
4866 | 303k | ps_rc_lap_out->i4_ignore_for_rc_update, |
4867 | 303k | i4_scene_num, |
4868 | 303k | ps_rc_ctxt->ai4_scene_numbers[i4_scene_num]); |
4869 | | |
4870 | | /** update the scene num for current frame*/ |
4871 | 303k | ps_rc_ctxt->au4_scene_num_temp_id[ps_rc_lap_out->i4_rc_temporal_lyr_id] = |
4872 | 303k | ps_rc_lap_out->u4_rc_scene_num; |
4873 | | |
4874 | 303k | if(ps_rc_ctxt->ai4_is_frame_scd[i4_enc_frm_id]) |
4875 | 375 | { |
4876 | | /*reset pre-enc SAD whenever SCD is detected so that it does not detect scene cut for other pictures*/ |
4877 | 3.75k | for(i = 0; i < MAX_PIC_TYPE; i++) |
4878 | 3.37k | { |
4879 | 3.37k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[i] = -1; |
4880 | 3.37k | } |
4881 | 375 | } |
4882 | | |
4883 | | /*remember i frame's cost metric to scale SAD of next of I frame*/ |
4884 | 303k | if(pic_type == IV_I_FRAME || pic_type == IV_IDR_FRAME) |
4885 | 97.8k | { |
4886 | 97.8k | ps_rc_ctxt->i8_prev_i_frm_cost = ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id]; |
4887 | 97.8k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] = |
4888 | 97.8k | ps_rc_ctxt->ai8_cur_frm_intra_cost[i4_enc_frm_id]; |
4889 | 97.8k | } |
4890 | | /*for other picture types update hme cost*/ |
4891 | 205k | else |
4892 | 205k | { |
4893 | 205k | ps_rc_ctxt->ai8_prev_frm_pre_enc_cost[rc_pic_type] = |
4894 | 205k | ps_rc_ctxt->ai8_cur_frame_coarse_ME_cost[i4_enc_frm_id]; |
4895 | 205k | } |
4896 | | |
4897 | 303k | ps_rc_ctxt->i4_is_first_frame_encoded = 1; |
4898 | 303k | } |
4899 | | |
4900 | | /**************************************************************************** |
4901 | | Function Name : ihevce_rc_store_retrive_update_info |
4902 | | Description : for storing and retrieving the data in case of the Enc Loop Parallelism. |
4903 | | Inputs : |
4904 | | Globals : |
4905 | | Processing : |
4906 | | Outputs : |
4907 | | Returns : |
4908 | | Issues : |
4909 | | Revision History: |
4910 | | DD MM YYYY Author(s) Changes (Describe the changes made) |
4911 | | *****************************************************************************/ |
4912 | | |
4913 | | void ihevce_rc_store_retrive_update_info( |
4914 | | void *pv_ctxt, |
4915 | | rc_bits_sad_t *ps_rc_frame_stat, |
4916 | | WORD32 i4_enc_frm_id_rc, |
4917 | | WORD32 bit_rate_id, |
4918 | | WORD32 i4_store_retrive, |
4919 | | WORD32 *pout_buf_id, |
4920 | | WORD32 *pi4_rc_pic_type, |
4921 | | WORD32 *pcur_qp, |
4922 | | void *ps_lap_out, |
4923 | | void *ps_rc_lap_out) |
4924 | 312k | { |
4925 | 312k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
4926 | 312k | if(1 == i4_store_retrive) |
4927 | 156k | { |
4928 | 156k | memcpy( |
4929 | 156k | &ps_rc_ctxt->as_rc_frame_stat_store[i4_enc_frm_id_rc][bit_rate_id], |
4930 | 156k | ps_rc_frame_stat, |
4931 | 156k | sizeof(rc_bits_sad_t)); |
4932 | 156k | memcpy(&ps_rc_ctxt->out_buf_id[i4_enc_frm_id_rc][bit_rate_id], pout_buf_id, sizeof(WORD32)); |
4933 | 156k | memcpy(&ps_rc_ctxt->i4_pic_type[i4_enc_frm_id_rc], pi4_rc_pic_type, sizeof(WORD32)); |
4934 | 156k | memcpy(&ps_rc_ctxt->cur_qp[i4_enc_frm_id_rc][bit_rate_id], pcur_qp, sizeof(WORD32)); |
4935 | 156k | memcpy( |
4936 | 156k | &ps_rc_ctxt->as_lap_out[i4_enc_frm_id_rc], |
4937 | 156k | ps_lap_out, |
4938 | 156k | sizeof(ihevce_lap_output_params_t)); |
4939 | 156k | memcpy( |
4940 | 156k | &ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc], |
4941 | 156k | ps_rc_lap_out, |
4942 | 156k | sizeof(rc_lap_out_params_t)); |
4943 | | |
4944 | 156k | { |
4945 | 156k | rc_lap_out_params_t *ps_rc_lap_out_curr = (rc_lap_out_params_t *)ps_rc_lap_out; |
4946 | 156k | rc_lap_out_params_t *ps_rc_lap_out_next_encode = |
4947 | 156k | (rc_lap_out_params_t *)ps_rc_lap_out_curr->ps_rc_lap_out_next_encode; |
4948 | | |
4949 | 156k | if(NULL != ps_rc_lap_out_next_encode) |
4950 | 0 | { |
4951 | 0 | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type = |
4952 | 0 | ps_rc_lap_out_next_encode->i4_rc_pic_type; |
4953 | 0 | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_scene_type = |
4954 | 0 | ps_rc_lap_out_next_encode->i4_rc_scene_type; |
4955 | 0 | } |
4956 | 156k | else |
4957 | 156k | { |
4958 | 156k | if(ps_rc_ctxt->u4_intra_frame_interval <= 1 || |
4959 | 156k | (ps_rc_lap_out_curr->i4_rc_display_num && |
4960 | 138k | (ps_rc_lap_out_curr->i4_rc_display_num % |
4961 | 130k | (ps_rc_ctxt->u4_intra_frame_interval - 1)) == 0)) |
4962 | 58.2k | { |
4963 | 58.2k | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type = IV_I_FRAME; |
4964 | 58.2k | } |
4965 | 98.0k | else |
4966 | 98.0k | { |
4967 | 98.0k | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_pic_type = -1; |
4968 | 98.0k | } |
4969 | 156k | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].i4_next_scene_type = -1; |
4970 | 156k | } |
4971 | 156k | ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc].ps_rc_lap_out_next_encode = NULL; |
4972 | 156k | } |
4973 | 156k | } |
4974 | 156k | else if(2 == i4_store_retrive) |
4975 | 156k | { |
4976 | 156k | memcpy( |
4977 | 156k | ps_rc_frame_stat, |
4978 | 156k | &ps_rc_ctxt->as_rc_frame_stat_store[i4_enc_frm_id_rc][bit_rate_id], |
4979 | 156k | sizeof(rc_bits_sad_t)); |
4980 | 156k | memcpy(pout_buf_id, &ps_rc_ctxt->out_buf_id[i4_enc_frm_id_rc][bit_rate_id], sizeof(WORD32)); |
4981 | 156k | memcpy(pi4_rc_pic_type, &ps_rc_ctxt->i4_pic_type[i4_enc_frm_id_rc], sizeof(WORD32)); |
4982 | 156k | memcpy(pcur_qp, &ps_rc_ctxt->cur_qp[i4_enc_frm_id_rc][bit_rate_id], sizeof(WORD32)); |
4983 | 156k | memcpy( |
4984 | 156k | ps_lap_out, |
4985 | 156k | &ps_rc_ctxt->as_lap_out[i4_enc_frm_id_rc], |
4986 | 156k | sizeof(ihevce_lap_output_params_t)); |
4987 | 156k | memcpy( |
4988 | 156k | ps_rc_lap_out, |
4989 | 156k | &ps_rc_ctxt->as_rc_lap_out[i4_enc_frm_id_rc], |
4990 | 156k | sizeof(rc_lap_out_params_t)); |
4991 | 156k | } |
4992 | 0 | else |
4993 | 0 | { |
4994 | 0 | ASSERT(0); |
4995 | 0 | } |
4996 | 312k | } |
4997 | | |
4998 | | /*###############################################*/ |
4999 | | /******* END OF RC UPDATE FUNCTIONS **************/ |
5000 | | /*###############################################*/ |
5001 | | |
5002 | | /*#################################################*/ |
5003 | | /******* START OF RC UTILS FUNCTIONS **************/ |
5004 | | /*#################################################*/ |
5005 | | |
5006 | | /** |
5007 | | ****************************************************************************** |
5008 | | * |
5009 | | * @brief function to account for error correction between bits rdopt estimate |
5010 | | * and actual entropy bit generation |
5011 | | * |
5012 | | * @par Description |
5013 | | * |
5014 | | * @param[in] pv_rc_ctxt |
5015 | | * void pointer to rc ctxt |
5016 | | * @param[in] i4_rdopt_bits_gen_error |
5017 | | * WODd32 variable with error correction between rdopt and entropy bytes gen |
5018 | | * |
5019 | | * @return void |
5020 | | * |
5021 | | ****************************************************************************** |
5022 | | */ |
5023 | | |
5024 | | void ihevce_rc_rdopt_entropy_bit_correct( |
5025 | | void *pv_rc_ctxt, WORD32 i4_cur_entropy_consumption, WORD32 i4_buf_id) |
5026 | 156k | { |
5027 | 156k | rc_context_t *ps_ctxt = (rc_context_t *)pv_rc_ctxt; |
5028 | 156k | WORD32 i4_error; |
5029 | 156k | WORD32 i, count = 0; |
5030 | 156k | ASSERT(i4_buf_id >= 0); |
5031 | 156k | ps_ctxt->ai4_entropy_bit_consumption[ps_ctxt->i4_entropy_bit_count] = |
5032 | 156k | i4_cur_entropy_consumption; |
5033 | 156k | ps_ctxt->ai4_entropy_bit_consumption_buf_id[ps_ctxt->i4_entropy_bit_count] = i4_buf_id; |
5034 | 156k | ps_ctxt->i4_entropy_bit_count = (ps_ctxt->i4_entropy_bit_count + 1) % NUM_BUF_RDOPT_ENT_CORRECT; |
5035 | | |
5036 | 468k | for(i = 0; i < NUM_BUF_RDOPT_ENT_CORRECT; i++) |
5037 | 312k | { |
5038 | 312k | if(ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] >= 0 && |
5039 | 312k | (ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] == |
5040 | 147k | ps_ctxt->ai4_entropy_bit_consumption_buf_id[i])) |
5041 | 147k | { |
5042 | 147k | i4_error = ps_ctxt->ai4_rdopt_bit_consumption_estimate[i] - |
5043 | 147k | ps_ctxt->ai4_entropy_bit_consumption[i]; |
5044 | | //DBG_PRINTF("entropy mismatch error = %d\n",i4_error/ps_ctxt->ai4_rdopt_bit_consumption_estimate[i]); |
5045 | 147k | ps_ctxt->ai4_rdopt_bit_consumption_estimate[i] = -1; |
5046 | 147k | ps_ctxt->ai4_rdopt_bit_consumption_buf_id[i] = -1; |
5047 | 147k | ps_ctxt->ai4_entropy_bit_consumption[i] = -1; |
5048 | 147k | ps_ctxt->ai4_entropy_bit_consumption_buf_id[i] = -1; |
5049 | | /*accumulate mismatch along with gop level bit error that is propogated to next frame*/ |
5050 | | /*error = rdopt - entropy so it is expected to be negative*/ |
5051 | 147k | rc_update_mismatch_error(ps_ctxt->rc_hdl, i4_error); |
5052 | 147k | count++; |
5053 | 147k | } |
5054 | 312k | } |
5055 | 156k | } |
5056 | | |
5057 | | /** |
5058 | | ****************************************************************************** |
5059 | | * |
5060 | | * @name ihevce_rc_check_non_lap_scd |
5061 | | * |
5062 | | * @par Description Detects SCD frames as I_only_scds or non_I_scds based |
5063 | | on intrasatd & ME costs. Updates scd flags |
5064 | | * |
5065 | | * @param[in] ps_rc_ctxt - pointer to rc context |
5066 | | * ps_rc_lap_out |
5067 | | * @return void |
5068 | | * |
5069 | | ****************************************************************************** |
5070 | | */ |
5071 | | void ihevce_rc_check_non_lap_scd(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out) |
5072 | 156k | { |
5073 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
5074 | 156k | picture_type_e rc_pic_type = ihevce_rc_conv_pic_type( |
5075 | 156k | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
5076 | 156k | ps_rc_ctxt->i4_field_pic, |
5077 | 156k | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
5078 | 156k | ps_rc_lap_out->i4_is_bottom_field, |
5079 | 156k | ps_rc_ctxt->i4_top_field_first); |
5080 | | |
5081 | | /*Init to normal frames*/ |
5082 | 156k | ps_rc_lap_out->i4_is_I_only_scd = 0; |
5083 | 156k | ps_rc_lap_out->i4_is_non_I_scd = 0; |
5084 | | |
5085 | | /*None of the above check is valid if marked as scene cut*/ |
5086 | 156k | if(ps_rc_lap_out->i4_rc_scene_type == SCENE_TYPE_SCENE_CUT) |
5087 | 0 | { |
5088 | 0 | WORD32 i; |
5089 | | /*reset all older data*/ |
5090 | 0 | for(i = 0; i < MAX_PIC_TYPE; i++) |
5091 | 0 | { |
5092 | 0 | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[i] = -1; |
5093 | 0 | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[i] = -1; |
5094 | 0 | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[i] = -1; |
5095 | 0 | } |
5096 | 0 | } |
5097 | 156k | else |
5098 | 156k | { |
5099 | | /*Check if it is I only reset case, lap_out is assumed to have latest data which is used to set the corresponding flags*/ |
5100 | | /*For I pic check for I only reset case and for other pictures check for non-I scd case*/ |
5101 | 156k | if(rc_pic_type == I_PIC) |
5102 | 50.9k | { |
5103 | 50.9k | if(ps_rc_lap_out->i8_pre_intra_satd < |
5104 | 50.9k | (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] >> 1) || |
5105 | 50.9k | ps_rc_lap_out->i8_pre_intra_satd > |
5106 | 48.8k | (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] << 1)) |
5107 | 12.6k | { |
5108 | | /*Check if atleast one frame data is available*/ |
5109 | 12.6k | if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] >= 0) |
5110 | 3.85k | ps_rc_lap_out->i4_is_I_only_scd = 1; |
5111 | 12.6k | } |
5112 | 50.9k | } |
5113 | 105k | else if( |
5114 | 105k | ((rc_pic_type == P_PIC) && |
5115 | 105k | (ps_rc_lap_out->i4_rc_quality_preset == IHEVCE_QUALITY_P6)) || |
5116 | 105k | (ps_rc_lap_out->i4_rc_quality_preset < IHEVCE_QUALITY_P6)) |
5117 | 101k | { |
5118 | 101k | #define SAD_THREASHOLD_30FPS (2.5) |
5119 | | /*Choose threshold as 2.5 for 30 fps content and 1.75 for 60 fps. Scale accordingly for intermediate framerate*/ |
5120 | 101k | WORD32 i4_non_simple_repeat_prev_frame_detect = 0; |
5121 | 101k | float sad_change_threshold = |
5122 | 101k | (float)(-0.8f * ((float)ps_rc_ctxt->u4_max_frame_rate / 30000) + 3.05f); /*Change of SAD threshold for 30 fps content, this should be lowered for 60 fps*/ |
5123 | 101k | if(sad_change_threshold < 1.5f) |
5124 | 0 | sad_change_threshold = 1.5f; |
5125 | 101k | if(sad_change_threshold > 3.0f) |
5126 | 0 | sad_change_threshold = 3.0f; |
5127 | 101k | ASSERT(ps_rc_lap_out->i8_raw_l1_coarse_me_sad >= 0); |
5128 | | |
5129 | | /*block variance computed at 4x4 level in w/4*h/4, |
5130 | | percent dc blks is how many block's variance are less than or equal to 16*/ |
5131 | 101k | if(ps_rc_lap_out->i4_perc_dc_blks < 85) |
5132 | 101k | { |
5133 | | /*me sad is expected to be zero for repeat frames*/ |
5134 | 101k | if((ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[rc_pic_type] == |
5135 | 101k | 0) && |
5136 | 101k | (ps_rc_lap_out->i4_rc_temporal_lyr_id == ps_rc_ctxt->i4_max_temporal_lyr)) |
5137 | 22.8k | { |
5138 | 22.8k | i4_non_simple_repeat_prev_frame_detect = 1; |
5139 | 22.8k | } |
5140 | 101k | } |
5141 | 101k | if(ps_rc_lap_out->i8_frame_acc_coarse_me_cost > |
5142 | 101k | (sad_change_threshold * |
5143 | 101k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type]) && |
5144 | 101k | (ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] >= 0) && |
5145 | 101k | (!i4_non_simple_repeat_prev_frame_detect)) |
5146 | 6.53k | { |
5147 | 6.53k | WORD32 one_per_pixel_sad_L1; |
5148 | | /*per pixel sad has to be greater than 1 to avoid repeat frames influence non-I scd detection*/ |
5149 | 6.53k | if((ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) < 4000000) |
5150 | 6.53k | { |
5151 | | /*1080*/ |
5152 | 6.53k | one_per_pixel_sad_L1 = |
5153 | 6.53k | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 2; |
5154 | 6.53k | } |
5155 | 0 | else |
5156 | 0 | { |
5157 | | /*4k*/ |
5158 | 0 | one_per_pixel_sad_L1 = |
5159 | 0 | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width) >> 4; |
5160 | 0 | } |
5161 | 6.53k | if(ps_rc_lap_out->i8_frame_acc_coarse_me_cost > one_per_pixel_sad_L1) |
5162 | 1.75k | { |
5163 | 1.75k | { |
5164 | 1.75k | ps_rc_lap_out->i4_is_non_I_scd = 1; |
5165 | 1.75k | } |
5166 | 1.75k | } |
5167 | 6.53k | } |
5168 | | |
5169 | 101k | if(rc_pic_type == P_PIC) |
5170 | 90.2k | { |
5171 | 90.2k | if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] < 0) |
5172 | 5.08k | { |
5173 | 5.08k | if(ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[I_PIC] > 0) |
5174 | 5.08k | { |
5175 | 5.08k | if(ps_rc_lap_out->i8_pre_intra_satd > |
5176 | 5.08k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[I_PIC] << 1) |
5177 | 206 | { |
5178 | 206 | ps_rc_lap_out->i4_is_non_I_scd = 1; |
5179 | 206 | } |
5180 | 5.08k | } |
5181 | 5.08k | } |
5182 | 90.2k | } |
5183 | 101k | } |
5184 | 156k | } |
5185 | | |
5186 | | /*remember the previous frame stats*/ |
5187 | 156k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_I_intra_raw_satd[rc_pic_type] = |
5188 | 156k | ps_rc_lap_out->i8_pre_intra_satd; //ps_rc_lap_out->i8_pre_intra_satd; |
5189 | 156k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_cost[rc_pic_type] = |
5190 | 156k | ps_rc_lap_out->i8_frame_acc_coarse_me_cost; //ps_rc_lap_out->i8_frame_acc_coarse_me_sad; |
5191 | 156k | ps_rc_ctxt->s_l1_state_metric.ai8_L1_prev_pic_coarse_me_sad[rc_pic_type] = |
5192 | 156k | ps_rc_lap_out->i8_raw_l1_coarse_me_sad; |
5193 | 156k | } |
5194 | | |
5195 | | /** |
5196 | | ****************************************************************************** |
5197 | | * |
5198 | | * @name ihevce_rc_check_is_pre_enc_qp_valid |
5199 | | * |
5200 | | * @par Description checking whether enc thread has updated qp in reverse queue |
5201 | | * |
5202 | | * @param[in] ps_rc_ctxt - pointer to rc context |
5203 | | * |
5204 | | * @return zero on success |
5205 | | * |
5206 | | ****************************************************************************** |
5207 | | */ |
5208 | | /**only function accessed by encoder without using mutex lock*/ |
5209 | | WORD32 ihevce_rc_check_is_pre_enc_qp_valid(void *pv_rc_ctxt, volatile WORD32 *pi4_force_end_flag) |
5210 | 156k | { |
5211 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
5212 | | |
5213 | 156k | volatile WORD32 i4_is_qp_valid; |
5214 | 156k | volatile WORD32 *pi4_is_qp_valid; |
5215 | | |
5216 | 156k | pi4_is_qp_valid = |
5217 | 156k | (volatile WORD32 *)&ps_rc_ctxt->as_pre_enc_qp_queue[ps_rc_ctxt->i4_pre_enc_qp_read_index] |
5218 | 156k | .i4_is_qp_valid; |
5219 | 156k | i4_is_qp_valid = *pi4_is_qp_valid; |
5220 | | |
5221 | | /*Due to stagger between L1 IPE and L0 IPE, towards the end (when encoder is in flush mode) L0 IPE can race ahead of enc |
5222 | | since it will suddenly get stagger between L1 and L0 worth of free buffers. It could try to start L0 even before enc has |
5223 | | populated qp for such frames. qp = -1 is returned in such case which implies encoder should wait for qp to be pop*/ |
5224 | | |
5225 | 156k | while(i4_is_qp_valid == -1) |
5226 | 0 | { |
5227 | | /*this rate control call is outside mutex lock to avoid deadlock. If this acquires mutex lock enc will not be able to |
5228 | | populate qp*/ |
5229 | 0 | i4_is_qp_valid = *pi4_is_qp_valid; |
5230 | |
|
5231 | 0 | if(1 == (*pi4_force_end_flag)) |
5232 | 0 | { |
5233 | 0 | *pi4_is_qp_valid = 1; |
5234 | 0 | i4_is_qp_valid = 1; |
5235 | 0 | } |
5236 | 0 | } |
5237 | 156k | return 0; |
5238 | 156k | } |
5239 | | |
5240 | | /*! |
5241 | | ****************************************************************************** |
5242 | | * \if Function name : ihevce_compute_temporal_complexity_reset_Kp_Kb |
5243 | | * |
5244 | | * \brief |
5245 | | * |
5246 | | * \param[in] *pv_ctxt -> rc context |
5247 | | * |
5248 | | * \return |
5249 | | * |
5250 | | * \author |
5251 | | * Ittiam |
5252 | | * |
5253 | | ***************************************************************************** |
5254 | | */ |
5255 | | void ihevce_compute_temporal_complexity_reset_Kp_Kb( |
5256 | | rc_lap_out_params_t *ps_rc_lap_out, void *pv_rc_ctxt, WORD32 i4_Kp_Kb_reset_flag) |
5257 | 0 | { |
5258 | 0 | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
5259 | 0 | rc_lap_out_params_t *ps_cur_rc_lap_out_temporal_offset, |
5260 | 0 | *ps_cur_rc_lap_out_temporal_offset_scd_detect; |
5261 | 0 | picture_type_e curr_rc_pic_type; |
5262 | 0 | LWORD64 i8_total_acc_coarse_me_sad = 0, i8_avg_acc_coarse_me_sad = 0; |
5263 | 0 | WORD8 i1_num_frames_in_Sub_GOP = 0, i = 0, i1_no_reset = 0; |
5264 | 0 | WORD32 i4_inter_frame_interval = rc_get_inter_frame_interval(ps_rc_ctxt->rc_hdl); |
5265 | 0 | WORD32 i4_frame_qp = 0, i4_temp_frame_qp = 0; |
5266 | 0 | WORD32 ai4_offsets[5] = { -3, -2, 2, 6, 7 }; |
5267 | 0 | ps_cur_rc_lap_out_temporal_offset = ps_rc_lap_out; |
5268 | 0 | ps_cur_rc_lap_out_temporal_offset_scd_detect = ps_rc_lap_out; |
5269 | |
|
5270 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
5271 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type, |
5272 | 0 | ps_rc_ctxt->i4_field_pic, |
5273 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id, |
5274 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field, |
5275 | 0 | ps_rc_ctxt->i4_top_field_first); |
5276 | |
|
5277 | 0 | if(curr_rc_pic_type == I_PIC) |
5278 | 0 | { |
5279 | 0 | ps_cur_rc_lap_out_temporal_offset_scd_detect = |
5280 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode; |
5281 | 0 | ps_cur_rc_lap_out_temporal_offset = |
5282 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode; |
5283 | |
|
5284 | 0 | if(NULL != ps_cur_rc_lap_out_temporal_offset) |
5285 | 0 | { |
5286 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
5287 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type, |
5288 | 0 | ps_rc_ctxt->i4_field_pic, |
5289 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id, |
5290 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field, |
5291 | 0 | ps_rc_ctxt->i4_top_field_first); |
5292 | 0 | } |
5293 | 0 | else |
5294 | 0 | return; |
5295 | 0 | } |
5296 | | |
5297 | 0 | if(ps_cur_rc_lap_out_temporal_offset->i4_L1_qp == -1) |
5298 | 0 | return; |
5299 | | |
5300 | 0 | if(ps_cur_rc_lap_out_temporal_offset->i4_L0_qp == -1) |
5301 | 0 | i4_frame_qp = ps_cur_rc_lap_out_temporal_offset->i4_L1_qp; |
5302 | 0 | else |
5303 | 0 | i4_frame_qp = ps_cur_rc_lap_out_temporal_offset->i4_L0_qp; |
5304 | |
|
5305 | 0 | i1_num_frames_in_Sub_GOP = 0; |
5306 | 0 | i = 0; |
5307 | |
|
5308 | 0 | i1_no_reset = 0; |
5309 | 0 | do |
5310 | 0 | { |
5311 | 0 | if(ps_cur_rc_lap_out_temporal_offset != NULL) |
5312 | 0 | { |
5313 | 0 | if(curr_rc_pic_type != I_PIC) |
5314 | 0 | i4_temp_frame_qp = |
5315 | 0 | i4_frame_qp + ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id + 1; |
5316 | |
|
5317 | 0 | i4_temp_frame_qp += ai4_offsets[curr_rc_pic_type]; |
5318 | 0 | i4_temp_frame_qp = CLIP3(i4_temp_frame_qp, 1, 51); |
5319 | |
|
5320 | 0 | { |
5321 | 0 | if(curr_rc_pic_type != I_PIC) |
5322 | 0 | { |
5323 | 0 | i8_total_acc_coarse_me_sad += |
5324 | 0 | ps_cur_rc_lap_out_temporal_offset |
5325 | 0 | ->ai8_frame_acc_coarse_me_sad[i4_temp_frame_qp]; |
5326 | 0 | i1_num_frames_in_Sub_GOP++; |
5327 | 0 | i++; |
5328 | 0 | } |
5329 | 0 | else |
5330 | 0 | { |
5331 | 0 | break; |
5332 | 0 | } |
5333 | 0 | } |
5334 | | |
5335 | 0 | ps_cur_rc_lap_out_temporal_offset = |
5336 | 0 | (rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode; |
5337 | |
|
5338 | 0 | if(ps_cur_rc_lap_out_temporal_offset == NULL) |
5339 | 0 | { |
5340 | 0 | break; |
5341 | 0 | } |
5342 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
5343 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_cur_rc_lap_out_temporal_offset->i4_rc_pic_type, |
5344 | 0 | ps_rc_ctxt->i4_field_pic, |
5345 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_rc_temporal_lyr_id, |
5346 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_is_bottom_field, |
5347 | 0 | ps_rc_ctxt->i4_top_field_first); |
5348 | 0 | } |
5349 | 0 | else |
5350 | 0 | { |
5351 | 0 | i1_num_frames_in_Sub_GOP = 0; |
5352 | 0 | break; |
5353 | 0 | } |
5354 | 0 | } while( |
5355 | 0 | ((((curr_rc_pic_type != P_PIC) && ((curr_rc_pic_type != I_PIC))) || |
5356 | 0 | (curr_rc_pic_type == P_PIC)) && |
5357 | 0 | (i1_num_frames_in_Sub_GOP < i4_inter_frame_interval))); |
5358 | | |
5359 | 0 | if((i1_num_frames_in_Sub_GOP) && (i1_no_reset == 0)) |
5360 | 0 | { |
5361 | 0 | float f_hme_sad_per_pixel; |
5362 | 0 | i8_avg_acc_coarse_me_sad = (i8_total_acc_coarse_me_sad / i1_num_frames_in_Sub_GOP); |
5363 | 0 | f_hme_sad_per_pixel = |
5364 | 0 | ((float)i8_avg_acc_coarse_me_sad / |
5365 | 0 | (ps_rc_ctxt->i4_frame_height * ps_rc_ctxt->i4_frame_width)); |
5366 | 0 | f_hme_sad_per_pixel = CLIP3(f_hme_sad_per_pixel, 0.01f, 5.0f); |
5367 | | /*reset the QP offsets for the next sub GOP depending on the offline model based on the temporal complexity */ |
5368 | 0 | if(i4_Kp_Kb_reset_flag) |
5369 | 0 | { |
5370 | 0 | WORD32 i4_bin; |
5371 | |
|
5372 | 0 | rc_reset_Kp_Kb( |
5373 | 0 | ps_rc_ctxt->rc_hdl, |
5374 | 0 | 8.00, |
5375 | 0 | ps_rc_ctxt->i4_num_active_pic_type, |
5376 | 0 | f_hme_sad_per_pixel, |
5377 | 0 | &i4_bin, |
5378 | 0 | ps_rc_ctxt->i4_rc_pass); |
5379 | 0 | } |
5380 | 0 | else |
5381 | 0 | { |
5382 | 0 | rc_ba_get_qp_offset_offline_data( |
5383 | 0 | ps_rc_ctxt->rc_hdl, |
5384 | 0 | ps_rc_lap_out->ai4_offsets, |
5385 | 0 | f_hme_sad_per_pixel, |
5386 | 0 | ps_rc_ctxt->i4_num_active_pic_type, |
5387 | 0 | &ps_rc_lap_out->i4_complexity_bin); |
5388 | |
|
5389 | 0 | ps_cur_rc_lap_out_temporal_offset = ps_rc_lap_out; |
5390 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_offsets_set_flag = 1; |
5391 | |
|
5392 | 0 | curr_rc_pic_type = ihevce_rc_conv_pic_type( |
5393 | 0 | (IV_PICTURE_CODING_TYPE_T)ps_rc_lap_out->i4_rc_pic_type, |
5394 | 0 | ps_rc_ctxt->i4_field_pic, |
5395 | 0 | ps_rc_lap_out->i4_rc_temporal_lyr_id, |
5396 | 0 | ps_rc_lap_out->i4_is_bottom_field, |
5397 | 0 | ps_rc_ctxt->i4_top_field_first); |
5398 | |
|
5399 | 0 | if((curr_rc_pic_type == I_PIC) && |
5400 | 0 | ((rc_lap_out_params_t *)ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode) |
5401 | 0 | ->i4_rc_pic_type == P_PIC) |
5402 | 0 | i1_num_frames_in_Sub_GOP++; |
5403 | |
|
5404 | 0 | for(i = 1; i < i1_num_frames_in_Sub_GOP; i++) |
5405 | 0 | { |
5406 | 0 | ps_cur_rc_lap_out_temporal_offset = |
5407 | 0 | (rc_lap_out_params_t *) |
5408 | 0 | ps_cur_rc_lap_out_temporal_offset->ps_rc_lap_out_next_encode; |
5409 | 0 | memmove( |
5410 | 0 | ps_cur_rc_lap_out_temporal_offset->ai4_offsets, |
5411 | 0 | ps_rc_lap_out->ai4_offsets, |
5412 | 0 | sizeof(WORD32) * 5); |
5413 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_complexity_bin = |
5414 | 0 | ps_rc_lap_out->i4_complexity_bin; |
5415 | 0 | ps_cur_rc_lap_out_temporal_offset->i4_offsets_set_flag = 1; |
5416 | 0 | } |
5417 | 0 | } |
5418 | 0 | } |
5419 | 0 | } |
5420 | | |
5421 | | /** |
5422 | | ****************************************************************************** |
5423 | | * |
5424 | | * @brief function to get delta QP or In frame RC bits estimate to avoid buffer underflow |
5425 | | * |
5426 | | * @par Description |
5427 | | * @param[in] |
5428 | | ****************************************************************************** |
5429 | | */ |
5430 | | |
5431 | | WORD32 ihevce_ebf_based_rc_correction_to_avoid_overflow( |
5432 | | rc_context_t *ps_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 *pi4_tot_bits_estimated) |
5433 | 114k | { |
5434 | 114k | WORD32 i4_modelQP, i4_clipQP, i4_maxEbfQP, i4_diffQP, i4_is_model_valid, i4_deltaQP = 0; |
5435 | 114k | LWORD64 i8_bitsClipQP, i8_grwEbf; // i8_bitsComp; |
5436 | 114k | WORD32 i4_is_offline_model_used; |
5437 | 114k | WORD32 i4_vbv_buffer_size, i4_drain_rate, i4_currEbf, i4_maxEbf; |
5438 | 114k | WORD32 i4_case = -1; |
5439 | 114k | float f_thrsh_i_pic_delta_qp_1, f_thrsh_i_pic_delta_qp_2, f_thrsh_p_pic_delta_qp_1, |
5440 | 114k | f_thrsh_p_pic_delta_qp_2; |
5441 | 114k | float f_thrsh_br_pic_delta_qp_1, f_thrsh_br_pic_delta_qp_2, f_thrsh_bnr_pic_delta_qp_1, |
5442 | 114k | f_thrsh_bnr_pic_delta_qp_2; |
5443 | 114k | float f_vbv_thrsh_delta_qp; |
5444 | | |
5445 | | /*initialization of all the variables*/ |
5446 | 114k | rc_init_buffer_info( |
5447 | 114k | ps_rc_ctxt->rc_hdl, &i4_vbv_buffer_size, &i4_currEbf, &i4_maxEbf, &i4_drain_rate); |
5448 | | |
5449 | 114k | i4_is_model_valid = ps_rc_lap_out->i4_is_model_valid; |
5450 | 114k | i4_modelQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_modelQP; |
5451 | 114k | i4_clipQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_finalQP; |
5452 | 114k | i4_maxEbfQP = ps_rc_ctxt->s_rc_high_lvl_stat.i4_maxEbfQP; |
5453 | 114k | i8_bitsClipQP = ps_rc_ctxt->s_rc_high_lvl_stat.i8_bits_from_finalQP; |
5454 | 114k | i4_is_offline_model_used = ps_rc_ctxt->s_rc_high_lvl_stat.i4_is_offline_model_used; |
5455 | 114k | ASSERT(i4_clipQP != INVALID_QP); |
5456 | | |
5457 | 114k | if(ps_rc_ctxt->i4_num_frame_parallel > 1) |
5458 | 0 | { |
5459 | 0 | f_thrsh_i_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_1; |
5460 | 0 | f_thrsh_i_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_I_PIC_DELTA_QP_2; |
5461 | 0 | f_thrsh_p_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_1; |
5462 | 0 | f_thrsh_p_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_P_PIC_DELTA_QP_2; |
5463 | 0 | f_thrsh_br_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_1; |
5464 | 0 | f_thrsh_br_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_BR_PIC_DELTA_QP_2; |
5465 | 0 | f_thrsh_bnr_pic_delta_qp_1 = (float)VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_1; |
5466 | 0 | f_thrsh_bnr_pic_delta_qp_2 = (float)VBV_THRSH_FRM_PRLL_BNR_PIC_DELTA_QP_2; |
5467 | 0 | f_vbv_thrsh_delta_qp = (float)VBV_THRSH_FRM_PRLL_DELTA_QP; |
5468 | 0 | } |
5469 | 114k | else |
5470 | 114k | { |
5471 | 114k | f_thrsh_i_pic_delta_qp_1 = (float)VBV_THRSH_I_PIC_DELTA_QP_1; |
5472 | 114k | f_thrsh_i_pic_delta_qp_2 = (float)VBV_THRSH_I_PIC_DELTA_QP_2; |
5473 | 114k | f_thrsh_p_pic_delta_qp_1 = (float)VBV_THRSH_P_PIC_DELTA_QP_1; |
5474 | 114k | f_thrsh_p_pic_delta_qp_2 = (float)VBV_THRSH_P_PIC_DELTA_QP_2; |
5475 | 114k | f_thrsh_br_pic_delta_qp_1 = (float)VBV_THRSH_BR_PIC_DELTA_QP_1; |
5476 | 114k | f_thrsh_br_pic_delta_qp_2 = (float)VBV_THRSH_BR_PIC_DELTA_QP_2; |
5477 | 114k | f_thrsh_bnr_pic_delta_qp_1 = (float)VBV_THRSH_BNR_PIC_DELTA_QP_1; |
5478 | 114k | f_thrsh_bnr_pic_delta_qp_2 = (float)VBV_THRSH_BNR_PIC_DELTA_QP_2; |
5479 | 114k | f_vbv_thrsh_delta_qp = (float)VBV_THRSH_DELTA_QP; |
5480 | 114k | } |
5481 | | |
5482 | | /* function logic starts */ |
5483 | 114k | if(i4_is_model_valid) |
5484 | 97.1k | { |
5485 | 97.1k | ASSERT(i4_modelQP != INVALID_QP); |
5486 | 97.1k | i8_grwEbf = i8_bitsClipQP - (LWORD64)i4_drain_rate; |
5487 | 97.1k | if(((i4_currEbf + i8_grwEbf) > (0.6*i4_vbv_buffer_size)) /*&& |
5488 | 97.1k | i4_modelQP >= i4_clipQP*/) |
5489 | 36.2k | { |
5490 | | /* part of existing scene (i.e. no new scene) |
5491 | | In which case this is not first I/P/Bref/Bnref etc |
5492 | | The models for I/P/Bref/Bnref are all valid*/ |
5493 | 36.2k | if(((i4_currEbf + i8_grwEbf) < |
5494 | 36.2k | i4_maxEbf)) /* does not matter whether this is 2pass, 1 pass, VBR, CBR etc*/ |
5495 | 36.2k | { |
5496 | | /* clipQP has been determined keeping in view certain other quality constraints like pusling etc. |
5497 | | So better to honour it if possible*/ |
5498 | | //if (i8_bitsClipQP > i8_drain_rate) |
5499 | 36.2k | { |
5500 | 36.2k | LWORD64 i8_thrsh_for_deltaQP_2 = i4_vbv_buffer_size, |
5501 | 36.2k | i8_thrsh_for_deltaQP_1 = i4_vbv_buffer_size; |
5502 | | /*even when (modelQP - clipQP) = 0, we intend to QP increase as expected ebf is above 60%*/ |
5503 | 36.2k | i4_diffQP = MAX(i4_modelQP - i4_clipQP, 1); |
5504 | 36.2k | switch(ps_rc_lap_out->i4_rc_pic_type) |
5505 | 36.2k | { |
5506 | 10.2k | case IV_I_FRAME: |
5507 | 24.6k | case IV_IDR_FRAME: |
5508 | 24.6k | { |
5509 | 24.6k | i8_thrsh_for_deltaQP_1 = |
5510 | 24.6k | (LWORD64)(f_thrsh_i_pic_delta_qp_1 * i4_vbv_buffer_size); |
5511 | 24.6k | i8_thrsh_for_deltaQP_2 = |
5512 | 24.6k | (LWORD64)(f_thrsh_i_pic_delta_qp_2 * i4_vbv_buffer_size); |
5513 | 24.6k | break; |
5514 | 10.2k | } |
5515 | 11.6k | case IV_P_FRAME: |
5516 | 11.6k | { |
5517 | 11.6k | i8_thrsh_for_deltaQP_1 = |
5518 | 11.6k | (LWORD64)(f_thrsh_p_pic_delta_qp_1 * i4_vbv_buffer_size); |
5519 | 11.6k | i8_thrsh_for_deltaQP_2 = |
5520 | 11.6k | (LWORD64)(f_thrsh_p_pic_delta_qp_2 * i4_vbv_buffer_size); |
5521 | 11.6k | break; |
5522 | 10.2k | } |
5523 | 2 | case IV_B_FRAME: |
5524 | 2 | { |
5525 | 2 | if(ps_rc_lap_out->i4_rc_is_ref_pic) |
5526 | 2 | { |
5527 | 2 | i8_thrsh_for_deltaQP_1 = |
5528 | 2 | (LWORD64)(f_thrsh_br_pic_delta_qp_1 * i4_vbv_buffer_size); |
5529 | 2 | i8_thrsh_for_deltaQP_2 = |
5530 | 2 | (LWORD64)(f_thrsh_br_pic_delta_qp_2 * i4_vbv_buffer_size); |
5531 | 2 | } |
5532 | 0 | else |
5533 | 0 | { |
5534 | | /*as of now using the same thresholds as B reference, later may have to tune if required*/ |
5535 | 0 | i8_thrsh_for_deltaQP_1 = |
5536 | 0 | (LWORD64)(f_thrsh_bnr_pic_delta_qp_1 * i4_vbv_buffer_size); |
5537 | 0 | i8_thrsh_for_deltaQP_2 = |
5538 | 0 | (LWORD64)(f_thrsh_bnr_pic_delta_qp_2 * i4_vbv_buffer_size); |
5539 | 0 | } |
5540 | 2 | break; |
5541 | 10.2k | } |
5542 | 0 | default: |
5543 | 0 | break; |
5544 | 36.2k | } |
5545 | | |
5546 | 36.2k | if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_1) |
5547 | 36.2k | { |
5548 | | /*For more than 2 QP chnage this means a larger scale issue and probably needs to be handled elsewhere ?*/ |
5549 | 36.2k | i4_deltaQP = |
5550 | 36.2k | MIN(2, i4_diffQP); /* we dont intend to change QP by more than 2 */ |
5551 | 36.2k | i4_case = 0; |
5552 | 36.2k | } |
5553 | 9 | else if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_2) |
5554 | 0 | { |
5555 | 0 | i4_deltaQP = MIN(1, i4_diffQP); |
5556 | 0 | i4_case = 1; |
5557 | 0 | } |
5558 | 36.2k | } |
5559 | | /* else if (i8_bitsClipQP > i8_drain_rate) |
5560 | | { |
5561 | | we have no correection, buffer will be healthy after this. |
5562 | | However, there could be one problem if the currEbf is already close to say 80% of EBF. |
5563 | | This means we have not reacted well early - needs to be handled? |
5564 | | |
5565 | | This could be the case where it is a simple scene immediately following a complex scene |
5566 | | and is the I picture (not the first I since model is valid). |
5567 | | Is this possible - maybe, what to do - dont know? |
5568 | | } |
5569 | | */ |
5570 | 36.2k | } |
5571 | 0 | else /*(i4_clipQP < i4_maxEbfQP)*/ |
5572 | 0 | { |
5573 | 0 | i4_deltaQP = 2; |
5574 | 0 | i4_case = 2; |
5575 | 0 | } |
5576 | 36.2k | } |
5577 | 97.1k | if((i4_currEbf + i8_grwEbf) < (0.6 * i4_vbv_buffer_size)) |
5578 | 60.8k | { |
5579 | 60.8k | *pi4_tot_bits_estimated = i8_bitsClipQP; |
5580 | 60.8k | } |
5581 | 97.1k | } |
5582 | 17.4k | else |
5583 | 17.4k | { |
5584 | 17.4k | if(i4_is_offline_model_used) |
5585 | 17.4k | { |
5586 | | /* this can be only for non-I SCD, where we reset RC */ |
5587 | 17.4k | WORD32 i4_bits_est_for_in_frm_rc = *pi4_tot_bits_estimated; |
5588 | 17.4k | i8_grwEbf = i4_bits_est_for_in_frm_rc - i4_drain_rate; |
5589 | 17.4k | if((i4_currEbf + i8_grwEbf) > (f_vbv_thrsh_delta_qp * i4_vbv_buffer_size)) |
5590 | 1.17k | { |
5591 | 1.17k | i4_bits_est_for_in_frm_rc = |
5592 | 1.17k | i4_drain_rate + (WORD32)(0.85 * i4_vbv_buffer_size) - i4_currEbf; |
5593 | | /* if pi4_tot_bits_estimated becomes less than zero or less than drain rate this indiactes that we are near or above 85% of the buffer */ |
5594 | | /* this needs a reaction */ |
5595 | 1.17k | if(i4_bits_est_for_in_frm_rc < i4_drain_rate) |
5596 | 0 | { |
5597 | 0 | *pi4_tot_bits_estimated = |
5598 | 0 | MAX((i4_drain_rate + (WORD32)(0.95 * i4_vbv_buffer_size) - i4_currEbf), |
5599 | 0 | i4_drain_rate); |
5600 | 0 | i4_deltaQP = 2; /* this needs some review, needs to be handled well */ |
5601 | 0 | } |
5602 | 1.17k | } |
5603 | 17.4k | i4_case = 3; |
5604 | 17.4k | } |
5605 | 0 | else |
5606 | 0 | { |
5607 | 0 | i8_bitsClipQP = *pi4_tot_bits_estimated; |
5608 | 0 | i8_grwEbf = i8_bitsClipQP - i4_drain_rate; |
5609 | |
|
5610 | 0 | if(((i4_currEbf + i8_grwEbf) < |
5611 | 0 | i4_maxEbf)) /* does not matter whether this is 2pass, 1 pass, VBR, CBR etc*/ |
5612 | 0 | { |
5613 | | /* clipQP has been determined keeping in view certain other quality constraints like pusling etc. |
5614 | | So better to honour it if possible*/ |
5615 | | //if (i8_bitsClipQP > i8_drain_rate) |
5616 | 0 | { |
5617 | 0 | LWORD64 i8_thrsh_for_deltaQP_2 = i4_vbv_buffer_size, |
5618 | 0 | i8_thrsh_for_deltaQP_1 = i4_vbv_buffer_size; |
5619 | |
|
5620 | 0 | switch(ps_rc_lap_out->i4_rc_pic_type) |
5621 | 0 | { |
5622 | 0 | case IV_I_FRAME: |
5623 | 0 | case IV_IDR_FRAME: |
5624 | 0 | { |
5625 | 0 | i8_thrsh_for_deltaQP_1 = |
5626 | 0 | (LWORD64)(f_thrsh_i_pic_delta_qp_1 * i4_vbv_buffer_size); |
5627 | 0 | i8_thrsh_for_deltaQP_2 = |
5628 | 0 | (LWORD64)(f_thrsh_i_pic_delta_qp_2 * i4_vbv_buffer_size); |
5629 | 0 | break; |
5630 | 0 | } |
5631 | 0 | case IV_P_FRAME: |
5632 | 0 | { |
5633 | 0 | i8_thrsh_for_deltaQP_1 = |
5634 | 0 | (LWORD64)(f_thrsh_p_pic_delta_qp_1 * i4_vbv_buffer_size); |
5635 | 0 | i8_thrsh_for_deltaQP_2 = |
5636 | 0 | (LWORD64)(f_thrsh_p_pic_delta_qp_2 * i4_vbv_buffer_size); |
5637 | 0 | break; |
5638 | 0 | } |
5639 | 0 | case IV_B_FRAME: |
5640 | 0 | { |
5641 | 0 | if(ps_rc_lap_out->i4_rc_is_ref_pic) |
5642 | 0 | { |
5643 | 0 | i8_thrsh_for_deltaQP_1 = |
5644 | 0 | (LWORD64)(f_thrsh_br_pic_delta_qp_1 * i4_vbv_buffer_size); |
5645 | 0 | i8_thrsh_for_deltaQP_2 = |
5646 | 0 | (LWORD64)(f_thrsh_br_pic_delta_qp_2 * i4_vbv_buffer_size); |
5647 | 0 | } |
5648 | 0 | else |
5649 | 0 | { |
5650 | | /*as of now using the same thresholds as B reference, later may have to tune if required*/ |
5651 | 0 | i8_thrsh_for_deltaQP_1 = |
5652 | 0 | (LWORD64)(f_thrsh_bnr_pic_delta_qp_1 * i4_vbv_buffer_size); |
5653 | 0 | i8_thrsh_for_deltaQP_2 = |
5654 | 0 | (LWORD64)(f_thrsh_bnr_pic_delta_qp_2 * i4_vbv_buffer_size); |
5655 | 0 | } |
5656 | 0 | break; |
5657 | 0 | } |
5658 | 0 | default: |
5659 | 0 | break; |
5660 | 0 | } |
5661 | | |
5662 | 0 | if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_1) |
5663 | 0 | { |
5664 | | /*For more than 2 QP chnage this means a larger scale issue and probably needs to be handled elsewhere ?*/ |
5665 | 0 | i4_deltaQP = 2; /* we dont intend to change QP by more than 2 */ |
5666 | 0 | i4_case = 5; |
5667 | 0 | } |
5668 | 0 | else if((i4_currEbf + i8_grwEbf) > i8_thrsh_for_deltaQP_2) |
5669 | 0 | { |
5670 | 0 | i4_deltaQP = 1; |
5671 | 0 | i4_case = 6; |
5672 | 0 | } |
5673 | 0 | } |
5674 | 0 | } |
5675 | 0 | else |
5676 | 0 | { |
5677 | 0 | i4_deltaQP = 2; |
5678 | 0 | i4_case = 7; |
5679 | 0 | } |
5680 | 0 | } |
5681 | 17.4k | } |
5682 | 114k | return i4_deltaQP; |
5683 | 114k | } |
5684 | | |
5685 | | /*###############################################*/ |
5686 | | /******* END OF RC UTILS FUNCTIONS ***************/ |
5687 | | /*###############################################*/ |
5688 | | |
5689 | | /*########################################################*/ |
5690 | | /******* START OF VBV COMPLIANCE FUNCTIONS ***************/ |
5691 | | /*#######################################################*/ |
5692 | | |
5693 | | /*! |
5694 | | ****************************************************************************** |
5695 | | * \if Function name : ihevce_vbv_compliance_frame_level_update |
5696 | | * |
5697 | | * \brief |
5698 | | * this function initializes the hrd buffer level to be used for vbv compliance testing using the parameters feeded in VUI parameters |
5699 | | * |
5700 | | * \param[in] *pv_ctxt -> rc context |
5701 | | * i4_bits_generated -> bits generated from entropy |
5702 | | * i4_resolution_id -> info needed for log Dump |
5703 | | * i4_appln_bitrate_inst -> info needed for log Dump |
5704 | | * u4_cur_cpb_removal_delay_minus1 -> cbp removal delay of present frame |
5705 | | * \return |
5706 | | * |
5707 | | * \author |
5708 | | * Ittiam |
5709 | | * |
5710 | | ***************************************************************************** |
5711 | | */ |
5712 | | |
5713 | | void ihevce_vbv_compliance_frame_level_update( |
5714 | | void *pv_rc_ctxt, |
5715 | | WORD32 i4_bits_generated, |
5716 | | WORD32 i4_resolution_id, |
5717 | | WORD32 i4_appln_bitrate_inst, |
5718 | | UWORD32 u4_cur_cpb_removal_delay_minus1) |
5719 | 114k | { |
5720 | 114k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_rc_ctxt; |
5721 | 114k | float f_max_vbv_buff_size = (float)ps_rc_ctxt->s_vbv_compliance.f_buffer_size; |
5722 | 114k | WORD32 i4_cbp_removal_delay_diff = 1; |
5723 | | |
5724 | 114k | if((ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 > 0) && |
5725 | 114k | (u4_cur_cpb_removal_delay_minus1 > |
5726 | 105k | ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1)) |
5727 | 105k | i4_cbp_removal_delay_diff = |
5728 | 105k | (u4_cur_cpb_removal_delay_minus1 - |
5729 | 105k | ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1); |
5730 | | |
5731 | 114k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = |
5732 | 114k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level - (float)i4_bits_generated + |
5733 | 114k | (i4_cbp_removal_delay_diff * ps_rc_ctxt->s_vbv_compliance.f_drain_rate); |
5734 | | |
5735 | 114k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip = |
5736 | 114k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level; |
5737 | | |
5738 | 114k | if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level < 0) |
5739 | 63 | { |
5740 | 63 | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = 0; |
5741 | 63 | } |
5742 | | |
5743 | 114k | if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level > |
5744 | 114k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size) |
5745 | 75.9k | { |
5746 | 75.9k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = |
5747 | 75.9k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size; |
5748 | 75.9k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip -= |
5749 | 75.9k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size; |
5750 | 75.9k | } |
5751 | 38.6k | else if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip > 0) |
5752 | 38.5k | { |
5753 | 38.5k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip = 0; |
5754 | 38.5k | } |
5755 | | |
5756 | 114k | if(ps_rc_ctxt->e_rate_control_type == VBR_STREAMING) |
5757 | 91.2k | { |
5758 | 91.2k | if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip > 0) |
5759 | 57.1k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level_unclip = 0; |
5760 | 91.2k | } |
5761 | 114k | ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 = u4_cur_cpb_removal_delay_minus1; |
5762 | 114k | } |
5763 | | |
5764 | | /*! |
5765 | | ****************************************************************************** |
5766 | | * \if Function name : ihevce_vbv_complaince_init_level |
5767 | | * |
5768 | | * \brief |
5769 | | * this function initializes the hrd buffer level to be used for vbv compliance testing using the parameters feeded in VUI parameters |
5770 | | * |
5771 | | * \param[in] *pv_ctxt -> rc context |
5772 | | * *ps_vui -> VUI parameters |
5773 | | * \return |
5774 | | * |
5775 | | * \author |
5776 | | * Ittiam |
5777 | | * |
5778 | | ***************************************************************************** |
5779 | | */ |
5780 | | |
5781 | | void ihevce_vbv_complaince_init_level(void *pv_ctxt, vui_t *ps_vui) |
5782 | 8.85k | { |
5783 | 8.85k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5784 | | |
5785 | 8.85k | ps_rc_ctxt->s_vbv_compliance.f_frame_rate = |
5786 | 8.85k | (float)((float)ps_vui->u4_vui_time_scale / ps_vui->u4_vui_num_units_in_tick); //rc_get_frame_rate(ps_rc_ctxt->rc_hdl); |
5787 | | |
5788 | 8.85k | if(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag) |
5789 | 0 | { |
5790 | 0 | ASSERT(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag); |
5791 | | |
5792 | 0 | ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)(( |
5793 | 0 | (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_bit_rate_du_value_minus1[0] + |
5794 | 0 | 1) |
5795 | 0 | << (6 + ps_vui->s_vui_hrd_parameters |
5796 | 0 | .u4_bit_rate_scale))); //rc_get_bit_rate(ps_rc_ctxt->rc_hdl); |
5797 | |
|
5798 | 0 | ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)(( |
5799 | 0 | (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_cpb_size_du_value_minus1[0] + |
5800 | 0 | 1) |
5801 | 0 | << (4 + ps_vui->s_vui_hrd_parameters |
5802 | 0 | .u4_cpb_size_du_scale))); //ps_rc_ctxt->u4_max_vbv_buff_size; |
5803 | 0 | } |
5804 | 8.85k | else |
5805 | 8.85k | { |
5806 | 8.85k | ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)(( |
5807 | 8.85k | (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_bit_rate_value_minus1[0] + |
5808 | 8.85k | 1) |
5809 | 8.85k | << (6 + ps_vui->s_vui_hrd_parameters |
5810 | 8.85k | .u4_bit_rate_scale))); //rc_get_bit_rate(ps_rc_ctxt->rc_hdl); |
5811 | | |
5812 | 8.85k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)(( |
5813 | 8.85k | (ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[0].au4_cpb_size_value_minus1[0] + |
5814 | 8.85k | 1) |
5815 | 8.85k | << (4 + ps_vui->s_vui_hrd_parameters |
5816 | 8.85k | .u4_cpb_size_scale))); //ps_rc_ctxt->u4_max_vbv_buff_size; |
5817 | 8.85k | } |
5818 | 8.85k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = |
5819 | 8.85k | (float)ps_rc_ctxt->s_vbv_compliance.f_buffer_size; //ps_rc_ctxt->u4_max_vbv_buff_size; |
5820 | | |
5821 | 8.85k | ps_rc_ctxt->s_vbv_compliance.f_drain_rate = |
5822 | 8.85k | ((ps_rc_ctxt->s_vbv_compliance.f_bit_rate) / ps_rc_ctxt->s_vbv_compliance.f_frame_rate); |
5823 | | |
5824 | 8.85k | ps_rc_ctxt->s_vbv_compliance.u4_prev_cpb_removal_delay_minus1 = 0; |
5825 | 8.85k | } |
5826 | | |
5827 | | /*########################################################*/ |
5828 | | /******* END OF VBV COMPLIANCE FUNCTIONS *****************/ |
5829 | | /*#######################################################*/ |
5830 | | |
5831 | | /*################################################################*/ |
5832 | | /******* START OF DYN CHANGE iN BITRATE FUNCTIONS *****************/ |
5833 | | /*################################################################*/ |
5834 | | /*! |
5835 | | ****************************************************************************** |
5836 | | * \if Function name : change_bitrate_vbv_complaince |
5837 | | * |
5838 | | * \brief |
5839 | | * this function updates the new bitrate and re calculates the drain rate |
5840 | | * |
5841 | | * \param[in] *pv_ctxt -> rc context |
5842 | | * \return |
5843 | | * |
5844 | | * \author |
5845 | | * Ittiam |
5846 | | * |
5847 | | ***************************************************************************** |
5848 | | */ |
5849 | | void change_bitrate_vbv_complaince(void *pv_ctxt, LWORD64 i8_new_bitrate, LWORD64 i8_buffer_size) |
5850 | 5.62k | { |
5851 | 5.62k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5852 | 5.62k | ps_rc_ctxt->s_vbv_compliance.f_buffer_size = (float)i8_buffer_size; |
5853 | 5.62k | ps_rc_ctxt->s_vbv_compliance.f_bit_rate = (float)i8_new_bitrate; |
5854 | 5.62k | if(ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level > i8_buffer_size) |
5855 | 2.30k | ps_rc_ctxt->s_vbv_compliance.f_curr_buffer_level = (float)i8_buffer_size; |
5856 | 5.62k | ps_rc_ctxt->s_vbv_compliance.f_drain_rate = |
5857 | 5.62k | ps_rc_ctxt->s_vbv_compliance.f_bit_rate / ps_rc_ctxt->s_vbv_compliance.f_frame_rate; |
5858 | 5.62k | } |
5859 | | /*! |
5860 | | ****************************************************************************** |
5861 | | * \if Function name : ihevce_rc_register_dyn_change_bitrate |
5862 | | * |
5863 | | * \brief |
5864 | | * this function registers call to change bitrate dynamically. |
5865 | | * |
5866 | | * \param[in] *pv_ctxt -> rc context |
5867 | | * |
5868 | | * \return |
5869 | | * |
5870 | | * \author |
5871 | | * Ittiam |
5872 | | * |
5873 | | ***************************************************************************** |
5874 | | */ |
5875 | | |
5876 | | void ihevce_rc_register_dyn_change_bitrate( |
5877 | | void *pv_ctxt, LWORD64 i8_new_bitrate, LWORD64 i8_new_peak_bitrate) |
5878 | 5.76k | { |
5879 | 5.76k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5880 | 5.76k | ps_rc_ctxt->i8_new_bitrate = i8_new_bitrate; |
5881 | 5.76k | ps_rc_ctxt->i8_new_peak_bitrate = i8_new_peak_bitrate; |
5882 | 5.76k | ps_rc_ctxt->i4_bitrate_changed = 1; |
5883 | 5.76k | ASSERT(ps_rc_ctxt->i8_new_bitrate > 0); |
5884 | 5.76k | ASSERT(ps_rc_ctxt->i8_new_peak_bitrate > 0); |
5885 | 5.76k | } |
5886 | | |
5887 | | /*! |
5888 | | ****************************************************************************** |
5889 | | * \if Function name : ihevce_rc_get_new_bitrate |
5890 | | * |
5891 | | * \brief |
5892 | | * get new bitrate |
5893 | | * |
5894 | | * \param[in] *pv_ctxt -> rc context |
5895 | | * |
5896 | | * \return |
5897 | | * |
5898 | | * \author |
5899 | | * Ittiam |
5900 | | * |
5901 | | ***************************************************************************** |
5902 | | */ |
5903 | | LWORD64 ihevce_rc_get_new_bitrate(void *pv_ctxt) |
5904 | 156k | { |
5905 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5906 | 156k | return ps_rc_ctxt->i8_new_bitrate; |
5907 | 156k | } |
5908 | | /*! |
5909 | | ****************************************************************************** |
5910 | | * \if Function name : ihevce_rc_get_new_peak_bitrate |
5911 | | * |
5912 | | * \brief |
5913 | | * get new peak rate |
5914 | | * |
5915 | | * \param[in] *pv_ctxt -> rc context |
5916 | | * |
5917 | | * \return |
5918 | | * |
5919 | | * \author |
5920 | | * Ittiam |
5921 | | * |
5922 | | ***************************************************************************** |
5923 | | */ |
5924 | | LWORD64 ihevce_rc_get_new_peak_bitrate(void *pv_ctxt) |
5925 | 156k | { |
5926 | 156k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5927 | 156k | return ps_rc_ctxt->i8_new_peak_bitrate; |
5928 | 156k | } |
5929 | | |
5930 | | /*! |
5931 | | ****************************************************************************** |
5932 | | * \if Function name : ihevce_rc_change_avg_bitrate |
5933 | | * |
5934 | | * \brief |
5935 | | * change average bitrate configured based on new bitrate |
5936 | | * |
5937 | | * \param[in] *pv_ctxt -> rc context |
5938 | | * |
5939 | | * \return |
5940 | | * |
5941 | | * \author |
5942 | | * Ittiam |
5943 | | * |
5944 | | ***************************************************************************** |
5945 | | */ |
5946 | | LWORD64 ihevce_rc_change_avg_bitrate(void *pv_ctxt) |
5947 | 5.62k | { |
5948 | 5.62k | rc_context_t *ps_rc_ctxt = (rc_context_t *)pv_ctxt; |
5949 | 5.62k | LWORD64 vbv_buffer_level_b4_change; |
5950 | | |
5951 | 5.62k | ASSERT(ps_rc_ctxt->i8_new_bitrate != -1); |
5952 | 5.62k | ASSERT(ps_rc_ctxt->i8_new_peak_bitrate != -1); |
5953 | | /*Get the VBV buffer level just before forcing bitrate change*/ |
5954 | 5.62k | vbv_buffer_level_b4_change = (LWORD64)rc_get_ebf(ps_rc_ctxt->rc_hdl); |
5955 | | |
5956 | 5.62k | change_avg_bit_rate( |
5957 | 5.62k | ps_rc_ctxt->rc_hdl, |
5958 | 5.62k | (UWORD32)ps_rc_ctxt->i8_new_bitrate, |
5959 | 5.62k | (UWORD32)ps_rc_ctxt->i8_new_peak_bitrate); |
5960 | | /*Once the request is serviced set new bitrate to -1*/ |
5961 | 5.62k | ps_rc_ctxt->i8_new_bitrate = -1; |
5962 | 5.62k | ps_rc_ctxt->i8_new_peak_bitrate = -1; |
5963 | 5.62k | return vbv_buffer_level_b4_change; |
5964 | 5.62k | } |
5965 | | |
5966 | | /*##############################################################*/ |
5967 | | /******* END OF DYN CHNAGE iN BITRATE FUNCTIONS *****************/ |
5968 | | /*##############################################################*/ |