• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_encode_header_sei_vui.c
24 *
25 * @brief
26 *   This file contains function definitions related to header vui/sei encoding
27 *
28 * @author
29 *   ittiam
30 *
31 * List of Functions
32 *   ihevce_generate_sub_layer_hrd_params()
33 *   ihevce_generate_hrd_params()
34 *   ihevce_generate_vui()
35 *   ihevce_put_buf_period_sei_params()
36 *   ihevce_put_active_parameter_set_sei_params()
37 *   ihevce_put_mastering_disp_col_vol_sei_params()
38 *   ihevce_put_mastering_disp_col_vol_sei_params()
39 *   ihevce_put_sei_params()
40 *   ihevce_put_cll_info_sei_params()
41 *   ihevce_put_recovery_point_sei_params()
42 *   ihevce_put_pic_timing_sei_params()
43 *   ihevce_put_hash_sei_params()
44 *   ihevce_put_sei_msg()
45 *   ihevce_generate_sei()
46 *   ihevce_populate_mastering_disp_col_vol_sei()
47 *   ihevce_populate_recovery_point_sei()
48 *   ihevce_populate_picture_timing_sei()
49 *   ihevce_populate_buffering_period_sei()
50 *   ihevce_populate_active_parameter_set_sei()
51 *   ihevce_calc_CRC()
52 *   ihevce_calc_checksum()
53 *   ihevce_populate_hash_sei()
54 *   ihevce_populate_vui()
55 *
56 ******************************************************************************
57 */
58 
59 /*****************************************************************************/
60 /* File Includes                                                             */
61 /*****************************************************************************/
62 /* System include files */
63 #include <stdio.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <assert.h>
67 #include <stdarg.h>
68 #include <math.h>
69 
70 /* User include files */
71 #include "ihevc_typedefs.h"
72 #include "itt_video_api.h"
73 #include "ihevce_api.h"
74 
75 #include "rc_cntrl_param.h"
76 #include "rc_frame_info_collector.h"
77 #include "rc_look_ahead_params.h"
78 
79 #include "ihevc_defs.h"
80 #include "ihevc_macros.h"
81 #include "ihevc_debug.h"
82 #include "ihevc_structs.h"
83 #include "ihevc_platform_macros.h"
84 #include "ihevc_deblk.h"
85 #include "ihevc_itrans_recon.h"
86 #include "ihevc_chroma_itrans_recon.h"
87 #include "ihevc_chroma_intra_pred.h"
88 #include "ihevc_intra_pred.h"
89 #include "ihevc_inter_pred.h"
90 #include "ihevc_mem_fns.h"
91 #include "ihevc_padding.h"
92 #include "ihevc_weighted_pred.h"
93 #include "ihevc_sao.h"
94 #include "ihevc_resi_trans.h"
95 #include "ihevc_quant_iquant_ssd.h"
96 #include "ihevc_cabac_tables.h"
97 #include "ihevc_trans_tables.h"
98 #include "ihevc_trans_macros.h"
99 
100 #include "ihevce_defs.h"
101 #include "ihevce_lap_enc_structs.h"
102 #include "ihevce_multi_thrd_structs.h"
103 #include "ihevce_multi_thrd_funcs.h"
104 #include "ihevce_me_common_defs.h"
105 #include "ihevce_had_satd.h"
106 #include "ihevce_error_codes.h"
107 #include "ihevce_error_checks.h"
108 #include "ihevce_bitstream.h"
109 #include "ihevce_cabac.h"
110 #include "ihevce_rdoq_macros.h"
111 #include "ihevce_function_selector.h"
112 #include "ihevce_enc_structs.h"
113 #include "ihevce_global_tables.h"
114 #include "ihevce_encode_header.h"
115 #include "ihevce_encode_header_sei_vui.h"
116 #include "ihevce_trace.h"
117 
118 /*****************************************************************************/
119 /* Function Definitions                                                      */
120 /*****************************************************************************/
121 
122 /**
123 ******************************************************************************
124 *
125 *  @brief Generates sub-layer  hrd parameters of VUI (Video Usability Info  Set)
126 *
127 *  @par   Description
128 *  Parse Video Usability Info as per ANNEX E.2
129 *
130 *  @param[in]   ps_bitstrm
131 *  pointer to bitstream context (handle)
132 *
133 *  @param[in]   ps_hrd_params
134 *  pointer to structure containing sub-layer hrd params of VUI data
135 *
136 *  @return      success or failure error code
137 *
138 ******************************************************************************
139 */
ihevce_generate_sub_layer_hrd_params(bitstrm_t * ps_bitstrm,sub_lyr_hrd_params_t * ps_sub_lyr_hrd_params,hrd_params_t * ps_hrd_params,WORD32 cpb_cnt_minus1)140 WORD32 ihevce_generate_sub_layer_hrd_params(
141     bitstrm_t *ps_bitstrm,
142     sub_lyr_hrd_params_t *ps_sub_lyr_hrd_params,
143     hrd_params_t *ps_hrd_params,
144     WORD32 cpb_cnt_minus1)
145 {
146     WORD32 j;
147     WORD32 return_status = IHEVCE_SUCCESS;
148 
149     for(j = 0; j <= cpb_cnt_minus1; j++)
150     {
151         /* au4_bit_rate_value_minus1 */
152         PUT_BITS_UEV(
153             ps_bitstrm, ps_sub_lyr_hrd_params->au4_bit_rate_value_minus1[j], return_status);
154         ENTROPY_TRACE("bit_rate_value_minus1", ps_sub_lyr_hrd_params->au4_bit_rate_value_minus1[j]);
155 
156         /* au4_cpb_size_value_minus1 */
157         PUT_BITS_UEV(
158             ps_bitstrm, ps_sub_lyr_hrd_params->au4_cpb_size_value_minus1[j], return_status);
159         ENTROPY_TRACE("cpb_size_value_minus1", ps_sub_lyr_hrd_params->au4_cpb_size_value_minus1[j]);
160 
161         if(ps_hrd_params->u1_sub_pic_cpb_params_present_flag)
162         {
163             /* au4_cpb_size_du_value_minus1 */
164             PUT_BITS_UEV(
165                 ps_bitstrm, ps_sub_lyr_hrd_params->au4_cpb_size_du_value_minus1[j], return_status);
166             ENTROPY_TRACE(
167                 "cpb_size_du_value_minus1", ps_sub_lyr_hrd_params->au4_cpb_size_du_value_minus1[j]);
168 
169             /* au4_bit_rate_du_value_minus1 */
170             PUT_BITS_UEV(
171                 ps_bitstrm, ps_sub_lyr_hrd_params->au4_bit_rate_du_value_minus1[j], return_status);
172             ENTROPY_TRACE(
173                 "bit_rate_du_value_minus1", ps_sub_lyr_hrd_params->au4_bit_rate_du_value_minus1[j]);
174         }
175 
176         /* au1_cbr_flag */
177         PUT_BITS(ps_bitstrm, ps_sub_lyr_hrd_params->au1_cbr_flag[j], 1, return_status);
178         ENTROPY_TRACE("cbr_flag", ps_sub_lyr_hrd_params->au1_cbr_flag[j]);
179     }
180     return return_status;
181 }
182 
183 /**
184 ******************************************************************************
185 *
186 *  @brief Generates hrd parameters of VUI (Video Usability Info  Set)
187 *
188 *  @par   Description
189 *  Parse Video Usability Info as per ANNEX E.2
190 *
191 *  @param[in]   ps_bitstrm
192 *  pointer to bitstream context (handle)
193 *
194 *  @param[in]   ps_sps
195 *  pointer to structure containing SPS data
196 *
197 *  @param[in]   ps_hrd_params
198 *  pointer to structure containing hrd params of VUI data
199 *
200 *  @return      success or failure error code
201 *
202 ******************************************************************************
203 */
ihevce_generate_hrd_params(bitstrm_t * ps_bitstrm,hrd_params_t * ps_hrd_params,sps_t * ps_sps)204 WORD32 ihevce_generate_hrd_params(bitstrm_t *ps_bitstrm, hrd_params_t *ps_hrd_params, sps_t *ps_sps)
205 {
206     WORD32 i;
207     WORD32 return_status = IHEVCE_SUCCESS;
208     UWORD8 u1_common_info_present_flag = 1;
209 
210     if(u1_common_info_present_flag)
211     {
212         /* u1_nal_hrd_parameters_present_flag */
213         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_nal_hrd_parameters_present_flag, 1, return_status);
214         ENTROPY_TRACE(
215             "nal_hrd_parameters_present_flag", ps_hrd_params->u1_nal_hrd_parameters_present_flag);
216 
217         /* u1_vcl_hrd_parameters_present_flag */
218         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_vcl_hrd_parameters_present_flag, 1, return_status);
219         ENTROPY_TRACE(
220             "vcl_hrd_parameters_present_flag", ps_hrd_params->u1_vcl_hrd_parameters_present_flag);
221 
222         if(ps_hrd_params->u1_vcl_hrd_parameters_present_flag ||
223            ps_hrd_params->u1_nal_hrd_parameters_present_flag)
224         {
225             /* u1_sub_pic_cpb_params_present_flag */
226             PUT_BITS(
227                 ps_bitstrm, ps_hrd_params->u1_sub_pic_cpb_params_present_flag, 1, return_status);
228             ENTROPY_TRACE(
229                 "sub_pic_Cpb_params_present_flag",
230                 ps_hrd_params->u1_sub_pic_cpb_params_present_flag);
231 
232             if(ps_hrd_params->u1_sub_pic_cpb_params_present_flag)
233             {
234                 /* u1_tick_divisor_minus2 */
235                 PUT_BITS(ps_bitstrm, ps_hrd_params->u1_tick_divisor_minus2, 8, return_status);
236                 ENTROPY_TRACE("tick_divisor_minus2", ps_hrd_params->u1_tick_divisor_minus2);
237 
238                 /* u1_du_cpb_removal_delay_increment_length_minus1 */
239                 PUT_BITS(
240                     ps_bitstrm,
241                     ps_hrd_params->u1_du_cpb_removal_delay_increment_length_minus1,
242                     5,
243                     return_status);
244                 ENTROPY_TRACE(
245                     "du_cpb_removal_delay_increment_length_minus1",
246                     ps_hrd_params->u1_du_cpb_removal_delay_increment_length_minus1);
247 
248                 /* u1_sub_pic_cpb_params_in_pic_timing_sei_flag */
249                 PUT_BITS(
250                     ps_bitstrm,
251                     ps_hrd_params->u1_sub_pic_cpb_params_in_pic_timing_sei_flag,
252                     1,
253                     return_status);
254                 ENTROPY_TRACE(
255                     "sub_pic_cpb_params_in_pic_timing_sei_flag",
256                     ps_hrd_params->u1_sub_pic_cpb_params_in_pic_timing_sei_flag);
257 
258                 /* u1_dpb_output_delay_du_length_minus1 */
259                 PUT_BITS(
260                     ps_bitstrm,
261                     ps_hrd_params->u1_dpb_output_delay_du_length_minus1,
262                     5,
263                     return_status);
264                 ENTROPY_TRACE(
265                     "dpb_output_delay_du_length_minus1",
266                     ps_hrd_params->u1_dpb_output_delay_du_length_minus1);
267             }
268 
269             /* u4_bit_rate_scale */
270             PUT_BITS(ps_bitstrm, ps_hrd_params->u4_bit_rate_scale, 4, return_status);
271             ENTROPY_TRACE("bit_rate_scale", ps_hrd_params->u4_bit_rate_scale);
272 
273             /* u4_cpb_size_scale */
274             PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_scale, 4, return_status);
275             ENTROPY_TRACE("cpb_size_scale", ps_hrd_params->u4_cpb_size_scale);
276 
277             if(ps_hrd_params->u1_sub_pic_cpb_params_present_flag)
278             {
279                 /* u4_cpb_size_du_scale */
280                 PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_du_scale, 4, return_status);
281                 ENTROPY_TRACE("cpb_size_du_scale", ps_hrd_params->u4_cpb_size_du_scale);
282             }
283 
284             /* u1_initial_cpb_removal_delay_length_minus1 */
285             PUT_BITS(
286                 ps_bitstrm,
287                 ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1,
288                 5,
289                 return_status);
290             ENTROPY_TRACE(
291                 "initial_cpb_removal_delay_length_minus1",
292                 ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1);
293 
294             /* u1_au_cpb_removal_delay_length_minus1 */
295             PUT_BITS(
296                 ps_bitstrm, ps_hrd_params->u1_au_cpb_removal_delay_length_minus1, 5, return_status);
297             ENTROPY_TRACE(
298                 "cpb_removal_delay_length_minus1",
299                 ps_hrd_params->u1_au_cpb_removal_delay_length_minus1);
300 
301             /* u1_dpb_output_delay_length_minus1 */
302             PUT_BITS(
303                 ps_bitstrm, ps_hrd_params->u1_dpb_output_delay_length_minus1, 5, return_status);
304             ENTROPY_TRACE(
305                 "dpb_output_delay_length_minus1", ps_hrd_params->u1_dpb_output_delay_length_minus1);
306         }
307     }
308 
309     for(i = 0; i < (ps_sps->i1_sps_max_sub_layers); i++)
310     {
311         /* au1_fixed_pic_rate_general_flag */
312         PUT_BITS(ps_bitstrm, ps_hrd_params->au1_fixed_pic_rate_general_flag[i], 1, return_status);
313         ENTROPY_TRACE(
314             "fixed_pic_rate_general_flag", ps_hrd_params->au1_fixed_pic_rate_general_flag[i]);
315 
316         if(!ps_hrd_params->au1_fixed_pic_rate_general_flag[i])
317         {
318             /* au1_fixed_pic_rate_within_cvs_flag */
319             PUT_BITS(
320                 ps_bitstrm, ps_hrd_params->au1_fixed_pic_rate_within_cvs_flag[i], 1, return_status);
321             ENTROPY_TRACE(
322                 "fixed_pic_rate_within_cvs_flag",
323                 ps_hrd_params->au1_fixed_pic_rate_within_cvs_flag[i]);
324         }
325 
326         if(ps_hrd_params->au1_fixed_pic_rate_within_cvs_flag[i])
327         {
328             /* au2_elemental_duration_in_tc_minus1 */
329             PUT_BITS_UEV(
330                 ps_bitstrm, ps_hrd_params->au2_elemental_duration_in_tc_minus1[i], return_status);
331             ENTROPY_TRACE(
332                 "elemental_duration_in_tc_minus1",
333                 ps_hrd_params->au2_elemental_duration_in_tc_minus1[i]);
334         }
335         else
336         {
337             /* au1_low_delay_hrd_flag */
338             PUT_BITS(ps_bitstrm, ps_hrd_params->au1_low_delay_hrd_flag[i], 1, return_status);
339             ENTROPY_TRACE("low_delay_hrd_flag", ps_hrd_params->au1_low_delay_hrd_flag[i]);
340         }
341 
342         if(!ps_hrd_params->au1_low_delay_hrd_flag[i])
343         {
344             /* au1_cpb_cnt_minus1 */
345             PUT_BITS_UEV(ps_bitstrm, ps_hrd_params->au1_cpb_cnt_minus1[i], return_status);
346             ENTROPY_TRACE("cpb_cnt_minus1", ps_hrd_params->au1_cpb_cnt_minus1[i]);
347         }
348 
349         if(ps_hrd_params->u1_nal_hrd_parameters_present_flag)
350         {
351             sub_lyr_hrd_params_t *ps_sub_lyr_hrd_params =
352                 &ps_hrd_params->as_sub_layer_hrd_params[i];
353             return_status |= ihevce_generate_sub_layer_hrd_params(
354                 ps_bitstrm,
355                 ps_sub_lyr_hrd_params,
356                 ps_hrd_params,
357                 ps_hrd_params->au1_cpb_cnt_minus1[i]);
358         }
359 
360         if(ps_hrd_params->u1_vcl_hrd_parameters_present_flag)
361         {
362             sub_lyr_hrd_params_t *ps_sub_lyr_hrd_params =
363                 &ps_hrd_params->as_sub_layer_hrd_params[i];
364             return_status |= ihevce_generate_sub_layer_hrd_params(
365                 ps_bitstrm,
366                 ps_sub_lyr_hrd_params,
367                 ps_hrd_params,
368                 ps_hrd_params->au1_cpb_cnt_minus1[i]);
369         }
370     }
371 
372     return return_status;
373 }
374 
375 /**
376 ******************************************************************************
377 *
378 *  @brief Generates VUI (Video Usability Info  Set)
379 *
380 *  @par   Description
381 *  Parse Video Usability Info as per ANNEX E.2
382 *
383 *  @param[in]   ps_bitstrm
384 *  pointer to bitstream context (handle)
385 *
386 *  @param[in]   ps_sps
387 *  pointer to structure containing SPS data
388 *
389 *  @param[in]   ps_vui
390 *  pointer to structure containing VUI data
391 *
392 *  @return      success or failure error code
393 *
394 ******************************************************************************
395 */
ihevce_generate_vui(bitstrm_t * ps_bitstrm,sps_t * ps_sps,vui_t s_vui)396 WORD32 ihevce_generate_vui(bitstrm_t *ps_bitstrm, sps_t *ps_sps, vui_t s_vui)
397 {
398     WORD32 return_status = IHEVCE_SUCCESS;
399 
400     /* aspect_ratio_info_present_flag */
401     PUT_BITS(ps_bitstrm, s_vui.u1_aspect_ratio_info_present_flag, 1, return_status);
402     ENTROPY_TRACE("aspect_ratio_info_present_flag", s_vui.u1_aspect_ratio_info_present_flag);
403 
404     if(s_vui.u1_aspect_ratio_info_present_flag)
405     {
406         /* aspect_ratio_idc */
407         PUT_BITS(ps_bitstrm, s_vui.u1_aspect_ratio_idc, 8, return_status);
408         ENTROPY_TRACE("aspect_ratio_idc", s_vui.u1_aspect_ratio_idc);
409         if(s_vui.u1_aspect_ratio_idc == IHEVCE_EXTENDED_SAR)
410         {
411             /* SAR_width */
412             PUT_BITS(ps_bitstrm, s_vui.u2_sar_width, 16, return_status);
413             ENTROPY_TRACE("sar_width", s_vui.u2_sar_width);
414 
415             /* SAR_hieght */
416             PUT_BITS(ps_bitstrm, s_vui.u2_sar_height, 16, return_status);
417             ENTROPY_TRACE("sar_height", s_vui.u2_sar_height);
418         }
419     }
420 
421     /* overscan_info_present_flag */
422     PUT_BITS(ps_bitstrm, s_vui.u1_overscan_info_present_flag, 1, return_status);
423     ENTROPY_TRACE("overscan_info_present_flag", s_vui.u1_overscan_info_present_flag);
424 
425     if(s_vui.u1_overscan_info_present_flag)
426     {
427         /* u1_overscan_appropriate_flag */
428         PUT_BITS(ps_bitstrm, s_vui.u1_overscan_appropriate_flag, 1, return_status);
429         ENTROPY_TRACE("overscan_appropriate_flag", s_vui.u1_overscan_appropriate_flag);
430     }
431 
432     /* video_signal_type_present_flag */
433     PUT_BITS(ps_bitstrm, s_vui.u1_video_signal_type_present_flag, 1, return_status);
434     ENTROPY_TRACE("video_signal_type_present_flag", s_vui.u1_video_signal_type_present_flag);
435 
436     if(s_vui.u1_video_signal_type_present_flag)
437     {
438         /* u1_video_format */
439         PUT_BITS(ps_bitstrm, s_vui.u1_video_format, 3, return_status);
440         ENTROPY_TRACE("video_format", s_vui.u1_video_format);
441 
442         /* u1_video_full_range_flag */
443         PUT_BITS(ps_bitstrm, s_vui.u1_video_full_range_flag, 1, return_status);
444         ENTROPY_TRACE("video_full_range_flag", s_vui.u1_video_full_range_flag);
445 
446         /* u1_colour_description_present_flag */
447         PUT_BITS(ps_bitstrm, s_vui.u1_colour_description_present_flag, 1, return_status);
448         ENTROPY_TRACE("colour_description_present_flag", s_vui.u1_colour_description_present_flag);
449 
450         if(s_vui.u1_colour_description_present_flag)
451         {
452             /* u1_colour_primaries */
453             PUT_BITS(ps_bitstrm, s_vui.u1_colour_primaries, 8, return_status);
454             ENTROPY_TRACE("colour_primaries", s_vui.u1_colour_primaries);
455 
456             /* u1_transfer_characteristics */
457             PUT_BITS(ps_bitstrm, s_vui.u1_transfer_characteristics, 8, return_status);
458             ENTROPY_TRACE("transfer_characteristics", s_vui.u1_transfer_characteristics);
459 
460             /* u1_matrix_coefficients */
461             PUT_BITS(ps_bitstrm, s_vui.u1_matrix_coefficients, 8, return_status);
462             ENTROPY_TRACE("matrix_coefficients", s_vui.u1_matrix_coefficients);
463         }
464     }
465 
466     /* u1_chroma_loc_info_present_flag */
467     PUT_BITS(ps_bitstrm, s_vui.u1_chroma_loc_info_present_flag, 1, return_status);
468     ENTROPY_TRACE("chroma_loc_info_present_flag", s_vui.u1_chroma_loc_info_present_flag);
469 
470     if(s_vui.u1_chroma_loc_info_present_flag)
471     {
472         /* u1_chroma_sample_loc_type_top_field */
473         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_chroma_sample_loc_type_top_field, return_status);
474         ENTROPY_TRACE(
475             "chroma_sample_loc_type_top_field", s_vui.u1_chroma_sample_loc_type_top_field);
476 
477         /* u1_chroma_sample_loc_type_bottom_field */
478         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_chroma_sample_loc_type_bottom_field, return_status);
479         ENTROPY_TRACE(
480             "chroma_sample_loc_type_bottom_field", s_vui.u1_chroma_sample_loc_type_bottom_field);
481     }
482 
483     /* u1_neutral_chroma_indication_flag */
484     PUT_BITS(ps_bitstrm, s_vui.u1_neutral_chroma_indication_flag, 1, return_status);
485     ENTROPY_TRACE("neutral_chroma_indication_flag", s_vui.u1_neutral_chroma_indication_flag);
486 
487     /* u1_field_seq_flag */
488     PUT_BITS(ps_bitstrm, s_vui.u1_field_seq_flag, 1, return_status);
489     ENTROPY_TRACE("field_seq_flag", s_vui.u1_field_seq_flag);
490 
491     /* HM CURRENTLY NOT SUPPOSTED */
492     /* u1_frame_field_info_present_flag */
493     PUT_BITS(ps_bitstrm, s_vui.u1_frame_field_info_present_flag, 1, return_status);
494     ENTROPY_TRACE("frame_field_info_present_flag", s_vui.u1_frame_field_info_present_flag);
495 
496     /* u1_default_display_window_flag */
497     PUT_BITS(ps_bitstrm, s_vui.u1_default_display_window_flag, 1, return_status);
498     ENTROPY_TRACE("default_display_window_flag", s_vui.u1_default_display_window_flag);
499 
500     if(s_vui.u1_default_display_window_flag)
501     {
502         /* u4_def_disp_win_left_offset */
503         PUT_BITS_UEV(ps_bitstrm, s_vui.u4_def_disp_win_left_offset, return_status);
504         ENTROPY_TRACE("def_disp_win_left_offset", s_vui.u4_def_disp_win_left_offset);
505 
506         /* u4_def_disp_win_right_offset */
507         PUT_BITS_UEV(ps_bitstrm, s_vui.u4_def_disp_win_right_offset, return_status);
508         ENTROPY_TRACE("def_disp_win_right_offset", s_vui.u4_def_disp_win_right_offset);
509 
510         /* u4_def_disp_win_top_offset */
511         PUT_BITS_UEV(ps_bitstrm, s_vui.u4_def_disp_win_top_offset, return_status);
512         ENTROPY_TRACE("def_disp_win_top_offset", s_vui.u4_def_disp_win_top_offset);
513 
514         /* u4_def_disp_win_bottom_offset */
515         PUT_BITS_UEV(ps_bitstrm, s_vui.u4_def_disp_win_bottom_offset, return_status);
516         ENTROPY_TRACE("def_disp_win_bottom_offset", s_vui.u4_def_disp_win_bottom_offset);
517     }
518 
519     /* u1_vui_timing_info_present_flag */
520     PUT_BITS(ps_bitstrm, s_vui.u1_vui_timing_info_present_flag, 1, return_status);
521     ENTROPY_TRACE("vui_timing_info_present_flag", s_vui.u1_vui_timing_info_present_flag);
522 
523     if(s_vui.u1_vui_timing_info_present_flag)
524     {
525         /* u4_num_units_in_tick */
526         PUT_BITS(ps_bitstrm, s_vui.u4_vui_num_units_in_tick, 32, return_status);
527         ENTROPY_TRACE("num_units_in_tick", s_vui.u4_vui_num_units_in_tick);
528 
529         /* u4_time_scale */
530         PUT_BITS(ps_bitstrm, s_vui.u4_vui_time_scale, 32, return_status);
531         ENTROPY_TRACE("time_scale", s_vui.u4_vui_time_scale);
532 
533         /* u1_poc_proportional_to_timing_flag */
534         PUT_BITS(ps_bitstrm, s_vui.u1_poc_proportional_to_timing_flag, 1, return_status);
535         ENTROPY_TRACE("poc_proportional_to_timing_flag", s_vui.u1_poc_proportional_to_timing_flag);
536 
537         /* u1_num_ticks_poc_diff_one_minus1 */
538         if(s_vui.u1_poc_proportional_to_timing_flag)
539         {
540             PUT_BITS_UEV(ps_bitstrm, s_vui.u4_num_ticks_poc_diff_one_minus1, return_status);
541             ENTROPY_TRACE("num_ticks_poc_diff_one_minus1", s_vui.u4_num_ticks_poc_diff_one_minus1);
542         }
543 
544         /* u1_vui_hrd_parameters_present_flag */
545         PUT_BITS(ps_bitstrm, s_vui.u1_vui_hrd_parameters_present_flag, 1, return_status);
546         ENTROPY_TRACE("vui_hrd_parameters_present_flag", s_vui.u1_vui_hrd_parameters_present_flag);
547 
548         if(s_vui.u1_vui_hrd_parameters_present_flag)
549         {
550             return_status |=
551                 ihevce_generate_hrd_params(ps_bitstrm, &(s_vui.s_vui_hrd_parameters), ps_sps);
552         }
553     }
554 
555     /* u1_bitstream_restriction_flag */
556     PUT_BITS(ps_bitstrm, s_vui.u1_bitstream_restriction_flag, 1, return_status);
557     ENTROPY_TRACE("bitstream_restriction_flag", s_vui.u1_bitstream_restriction_flag);
558 
559     if(s_vui.u1_bitstream_restriction_flag)
560     {
561         /* u1_tiles_fixed_structure_flag */
562         PUT_BITS(ps_bitstrm, s_vui.u1_tiles_fixed_structure_flag, 1, return_status);
563         ENTROPY_TRACE("tiles_fixed_structure_flag", s_vui.u1_tiles_fixed_structure_flag);
564 
565         /* u1_motion_vectors_over_pic_boundaries_flag */
566         PUT_BITS(ps_bitstrm, s_vui.u1_motion_vectors_over_pic_boundaries_flag, 1, return_status);
567         ENTROPY_TRACE(
568             "motion_vectors_over_pic_boundaries_flag",
569             s_vui.u1_motion_vectors_over_pic_boundaries_flag);
570 
571         /* u1_restricted_ref_pic_lists_flag */
572         PUT_BITS(ps_bitstrm, s_vui.u1_restricted_ref_pic_lists_flag, 1, return_status);
573         ENTROPY_TRACE("restricted_ref_pic_lists_flag", s_vui.u1_restricted_ref_pic_lists_flag);
574 
575         /* u4_min_spatial_segmentation_idc */
576         PUT_BITS_UEV(ps_bitstrm, s_vui.u4_min_spatial_segmentation_idc, return_status);
577         ENTROPY_TRACE("min_spatial_segmentation_idc", s_vui.u4_min_spatial_segmentation_idc);
578 
579         /* u1_max_bytes_per_pic_denom */
580         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_max_bytes_per_pic_denom, return_status);
581         ENTROPY_TRACE("max_bytes_per_pic_denom", s_vui.u1_max_bytes_per_pic_denom);
582 
583         /* u1_max_bits_per_mincu_denom */
584         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_max_bits_per_mincu_denom, return_status);
585         ENTROPY_TRACE("max_bits_per_mincu_denom", s_vui.u1_max_bits_per_mincu_denom);
586 
587         /* u1_log2_max_mv_length_horizontal */
588         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_log2_max_mv_length_horizontal, return_status);
589         ENTROPY_TRACE("log2_max_mv_length_horizontal", s_vui.u1_log2_max_mv_length_horizontal);
590 
591         /* u1_log2_max_mv_length_vertical */
592         PUT_BITS_UEV(ps_bitstrm, s_vui.u1_log2_max_mv_length_vertical, return_status);
593         ENTROPY_TRACE("log2_max_mv_length_vertical", s_vui.u1_log2_max_mv_length_vertical);
594     }
595     return return_status;
596 }
597 
598 /**
599 ******************************************************************************
600 *
601 *  @brief Generates Buffering Period (Supplemental Enhancement Information )
602 *
603 *  @par   Description
604 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
605 *
606 *  @param[in]   ps_bitstrm
607 *  pointer to bitstream context (handle)
608 *
609 *  @param[in]   ps_pt_sei
610 *  pointer to structure containing buffering period SEI data
611 *
612 *  @param[in]   ps_vui_params
613 *  pointer to structure containing VUI data
614 *
615 *  @return      success or failure error code
616 *
617 ******************************************************************************
618 */
ihevce_put_buf_period_sei_params(buf_period_sei_params_t * ps_bp_sei,vui_t * ps_vui_params,bitstrm_t * ps_bitstrm)619 WORD32 ihevce_put_buf_period_sei_params(
620     buf_period_sei_params_t *ps_bp_sei, vui_t *ps_vui_params, bitstrm_t *ps_bitstrm)
621 {
622     UWORD32 i;
623     WORD32 return_status = IHEVCE_SUCCESS;
624     UWORD8 u1_payload_size = 0;
625     UWORD8 u1_sub_pic_cpb_params_present_flag =
626         ps_vui_params->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag;
627 
628     {
629         //UWORD32 range;
630         //UWORD32 sps_id = ps_bp_sei->u1_bp_seq_parameter_set_id;
631 
632         //range = 0;
633         //GETRANGE(range, sps_id);
634         u1_payload_size += 1;  //(((range - 1) << 1) + 1);
635 
636         if(!u1_sub_pic_cpb_params_present_flag)
637             u1_payload_size += 1;
638         u1_payload_size += 1;
639         u1_payload_size +=
640             ps_vui_params->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 + 1;
641         if(1 == ps_vui_params->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag)
642         {
643             for(i = 0; i <= ps_bp_sei->u4_cpb_cnt; i++)
644             {
645                 u1_payload_size += ps_bp_sei->u4_initial_cpb_removal_delay_length << 1;
646                 if(u1_sub_pic_cpb_params_present_flag || ps_bp_sei->u1_rap_cpb_params_present_flag)
647                     u1_payload_size += ps_bp_sei->u4_initial_cpb_removal_delay_length << 1;
648             }
649         }
650         if(1 == ps_vui_params->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag)
651         {
652             for(i = 0; i <= ps_bp_sei->u4_cpb_cnt; i++)
653             {
654                 u1_payload_size += ps_bp_sei->u4_initial_cpb_removal_delay_length << 1;
655                 if(u1_sub_pic_cpb_params_present_flag || ps_bp_sei->u1_rap_cpb_params_present_flag)
656                     u1_payload_size += ps_bp_sei->u4_initial_cpb_removal_delay_length << 1;
657             }
658         }
659     }
660 
661     u1_payload_size = (u1_payload_size + 7) >> 3;
662 
663     /************************************************************************************************/
664     /* Calculating the cbp removal delay and cbp removal delay offset based on the                  */
665     /* buffer level information from Rate control                                                   */
666     /* NOTE : Buffer fullness level for Rate control is updated using Approximate                   */
667     /* number of bits from RDOPT stage rather than from accurate number of bits from ENTROPY coding */
668     /************************************************************************************************/
669 
670     {
671         ULWORD64 u8_temp;
672         UWORD32 u4_buffer_size, u4_dbf;
673 
674         u4_buffer_size = ps_bp_sei->u4_buffer_size_sei;
675         u4_dbf = ps_bp_sei->u4_dbf_sei;
676         for(i = 0; i <= ps_bp_sei->u4_cpb_cnt; i++)
677         {
678             u8_temp = ((ULWORD64)(u4_dbf)*90000);
679             u8_temp = u8_temp / ps_bp_sei->u4_target_bit_rate_sei;
680             ps_bp_sei->au4_nal_initial_cpb_removal_delay[i] = (UWORD32)u8_temp;
681 
682             u8_temp = ((ULWORD64)(u4_buffer_size - u4_dbf) * 90000);
683             u8_temp = u8_temp / ps_bp_sei->u4_target_bit_rate_sei;
684             ps_bp_sei->au4_nal_initial_cpb_removal_delay_offset[i] = (UWORD32)u8_temp;
685 
686             if(ps_bp_sei->au4_nal_initial_cpb_removal_delay[i] < 1)
687                 ps_bp_sei->au4_nal_initial_cpb_removal_delay[i] =
688                     1; /* ps_bp_sei->au4_nal_initial_cpb_removal_delay[i] should be always greater than 0 */
689         }
690     }
691 
692     /************************************************************************/
693     /* PayloadSize : This is the size of the payload in bytes               */
694     /************************************************************************/
695     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
696     ENTROPY_TRACE("payload_size", u1_payload_size);
697 
698     /************************************************************************/
699     /* Put the buffering period SEI parameters into the bitstream. For      */
700     /* details refer to section D.1.1 of the standard                       */
701     /************************************************************************/
702 
703     /* seq_parameter_set_id */
704     PUT_BITS_UEV(ps_bitstrm, ps_bp_sei->u1_bp_seq_parameter_set_id, return_status);
705     ENTROPY_TRACE("seq_parameter_set_id", ps_bp_sei->u1_bp_seq_parameter_set_id);
706 
707     //PUT_BITS(ps_bitstrm, u1_sub_pic_cpb_params_present_flag, 1, return_status);
708     //ENTROPY_TRACE("sub_pic_cpb_params_present_flag", u1_sub_pic_cpb_params_present_flag);
709 
710     if(!u1_sub_pic_cpb_params_present_flag)
711     {
712         /* u1_rap_cpb_params_present_flag */
713         PUT_BITS(ps_bitstrm, ps_bp_sei->u1_rap_cpb_params_present_flag, 1, return_status);
714         ENTROPY_TRACE("rap_cpb_params_present_flag", ps_bp_sei->u1_rap_cpb_params_present_flag);
715     }
716 
717     if(ps_bp_sei->u1_rap_cpb_params_present_flag)
718     {
719         PUT_BITS(
720             ps_bitstrm,
721             ps_bp_sei->u4_cpb_delay_offset,
722             (ps_vui_params->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 + 1),
723             return_status);
724         ENTROPY_TRACE("cpb_delay_offset", ps_bp_sei->cpb_delay_offset);
725 
726         PUT_BITS(
727             ps_bitstrm,
728             ps_bp_sei->u4_dpb_delay_offset,
729             (ps_vui_params->s_vui_hrd_parameters.u1_dpb_output_delay_length_minus1 + 1),
730             return_status);
731         ENTROPY_TRACE("dpb_delay_offset", ps_bp_sei->dpb_delay_offset);
732     }
733 
734     PUT_BITS(ps_bitstrm, ps_bp_sei->u1_concatenation_flag, 1, return_status);
735     ENTROPY_TRACE("concatenation_flag", ps_bp_sei->concatenation_flag);
736 
737     PUT_BITS(
738         ps_bitstrm,
739         ps_bp_sei->u4_au_cpb_removal_delay_delta_minus1,
740         (ps_vui_params->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 + 1),
741         return_status);
742 
743     ENTROPY_TRACE(
744         "au_cpb_removal_delay_delta_minus1", ps_bp_sei->au_cpb_removal_delay_delta_minus1);
745 
746     if(1 == ps_vui_params->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag)
747     {
748         for(i = 0; i <= ps_bp_sei->u4_cpb_cnt; i++)
749         {
750             PUT_BITS(
751                 ps_bitstrm,
752                 ps_bp_sei->au4_nal_initial_cpb_removal_delay[i],
753                 ps_bp_sei->u4_initial_cpb_removal_delay_length,
754                 return_status);
755             ENTROPY_TRACE(
756                 "nal_initial_cpb_removal_delay", ps_bp_sei->au4_nal_initial_cpb_removal_delay[i]);
757 
758             PUT_BITS(
759                 ps_bitstrm,
760                 ps_bp_sei->au4_nal_initial_cpb_removal_delay_offset[i],
761                 ps_bp_sei->u4_initial_cpb_removal_delay_length,
762                 return_status);
763             ENTROPY_TRACE(
764                 "nal_initial_cpb_removal_delay_offset",
765                 ps_bp_sei->au4_nal_initial_cpb_removal_delay_offset[i]);
766 
767             if(u1_sub_pic_cpb_params_present_flag || ps_bp_sei->u1_rap_cpb_params_present_flag)
768             {
769                 PUT_BITS(
770                     ps_bitstrm,
771                     ps_bp_sei->au4_nal_initial_alt_cpb_removal_delay[i],
772                     ps_bp_sei->u4_initial_cpb_removal_delay_length,
773                     return_status);
774                 ENTROPY_TRACE(
775                     "nal_initial_alt_cpb_removal_delay",
776                     ps_bp_sei->au4_nal_initial_alt_cpb_removal_delay[i]);
777 
778                 PUT_BITS(
779                     ps_bitstrm,
780                     ps_bp_sei->au4_nal_initial_alt_cpb_removal_delay_offset[i],
781                     ps_bp_sei->u4_initial_cpb_removal_delay_length,
782                     return_status);
783                 ENTROPY_TRACE(
784                     "nal_initial_alt_cpb_removal_delay_offset",
785                     ps_bp_sei->au4_nal_initial_alt_cpb_removal_delay_offset[i]);
786             }
787         }
788     }
789 
790     if(1 == ps_vui_params->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag)
791     {
792         for(i = 0; i <= ps_bp_sei->u4_cpb_cnt; i++)
793         {
794             PUT_BITS(
795                 ps_bitstrm,
796                 ps_bp_sei->au4_vcl_initial_cpb_removal_delay[i],
797                 ps_bp_sei->u4_initial_cpb_removal_delay_length,
798                 return_status);
799             ENTROPY_TRACE(
800                 "vcl_initial_cpb_removal_delay", ps_bp_sei->au4_vcl_initial_cpb_removal_delay[i]);
801 
802             PUT_BITS(
803                 ps_bitstrm,
804                 ps_bp_sei->au4_vcl_initial_cpb_removal_delay_offset[i],
805                 ps_bp_sei->u4_initial_cpb_removal_delay_length,
806                 return_status);
807             ENTROPY_TRACE(
808                 "vcl_initial_cpb_removal_delay_offset",
809                 ps_bp_sei->au4_vcl_initial_cpb_removal_delay_offset[i]);
810 
811             if(u1_sub_pic_cpb_params_present_flag || ps_bp_sei->u1_rap_cpb_params_present_flag)
812             {
813                 PUT_BITS(
814                     ps_bitstrm,
815                     ps_bp_sei->au4_vcl_initial_alt_cpb_removal_delay[i],
816                     ps_bp_sei->u4_initial_cpb_removal_delay_length,
817                     return_status);
818                 ENTROPY_TRACE(
819                     "vcl_initial_alt_cpb_removal_delay",
820                     ps_bp_sei->au4_vcl_initial_alt_cpb_removal_delay[i]);
821 
822                 PUT_BITS(
823                     ps_bitstrm,
824                     ps_bp_sei->au4_vcl_initial_alt_cpb_removal_delay_offset[i],
825                     ps_bp_sei->u4_initial_cpb_removal_delay_length,
826                     return_status);
827                 ENTROPY_TRACE(
828                     "vcl_initial_alt_cpb_removal_delay_offset",
829                     ps_bp_sei->au4_vcl_initial_alt_cpb_removal_delay_offset[i]);
830             }
831         }
832     }
833 
834     return (return_status);
835 }
836 
837 /**
838 ******************************************************************************
839 *
840 *  @brief Generates active parameter set  (Supplemental Enhancement Information )
841 *
842 *  @par   Description
843 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
844 *
845 *  @param[in]   ps_bitstrm
846 *  pointer to bitstream context (handle)
847 *
848 *  @param[in]   ps_act_sei
849 *  pointer to structure containing active parameter set SEI data
850 *
851 *  @return      success or failure error code
852 *
853 *****************************************************************************
854 */
ihevce_put_active_parameter_set_sei_params(active_parameter_set_sei_param_t * ps_act_sei,bitstrm_t * ps_bitstrm)855 WORD32 ihevce_put_active_parameter_set_sei_params(
856     active_parameter_set_sei_param_t *ps_act_sei, bitstrm_t *ps_bitstrm)
857 
858 {
859     UWORD8 u1_payload_size = 0, i;
860     UWORD8 u1_range = 0, u1_act_sps_id;
861     WORD32 return_status = IHEVCE_SUCCESS;
862 
863     u1_payload_size += 4;
864     u1_payload_size += 1;
865     u1_payload_size += 1;
866     u1_payload_size += 1;  // num_sps_ids_minus1  ahould be zero as per the standard
867 
868     u1_act_sps_id = ps_act_sei->u1_active_video_parameter_set_id;
869 
870     GETRANGE(u1_range, u1_act_sps_id);
871 
872     u1_payload_size += (((u1_range - 1) << 1) + 1);
873 
874     u1_payload_size = (u1_payload_size + 7) >> 3;
875 
876     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
877     ENTROPY_TRACE("payload_size", u1_payload_size);
878 
879     PUT_BITS(ps_bitstrm, ps_act_sei->u1_active_video_parameter_set_id, 4, return_status);
880     ENTROPY_TRACE("active_video_parameter_set_id", ps_act_sei->u1_active_video_parameter_set_id);
881 
882     PUT_BITS(ps_bitstrm, ps_act_sei->u1_self_contained_cvs_flag, 1, return_status);
883     ENTROPY_TRACE("self_contained_cvs_flag", ps_act_sei->u1_self_contained_cvs_flag);
884 
885     PUT_BITS(ps_bitstrm, ps_act_sei->u1_no_parameter_set_update_flag, 1, return_status);
886     ENTROPY_TRACE("no_parameter_set_update_flag", ps_act_sei->u1_no_parameter_set_update_flag);
887 
888     PUT_BITS_UEV(ps_bitstrm, ps_act_sei->u1_num_sps_ids_minus1, return_status);
889 
890     ENTROPY_TRACE("num_sps_ids_minus1", ps_act_sei->u1_num_sps_ids_minus1);
891 
892     for(i = 0; i <= ps_act_sei->u1_num_sps_ids_minus1; i++)
893     {
894         PUT_BITS_UEV(ps_bitstrm, ps_act_sei->au1_active_seq_parameter_set_id[i], return_status);
895         ENTROPY_TRACE(
896             "active_video_parameter_set_id", ps_act_sei->au1_active_seq_parameter_set_id[i]);
897     }
898     return (return_status);
899 }
900 
901 /**
902 ******************************************************************************
903 *
904 *  @brief Generates Mastering Display Colour Volume (Supplemental Enhancement Information )
905 *
906 *  @par   Description
907 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
908 *
909 *  @param[in]   ps_bitstrm
910 *  pointer to bitstream context (handle)
911 *
912 *  @param[in]   ps_rp_sei
913 *  pointer to structure containing recovery point SEI data
914 *
915 *  @param[in]   ps_vui_params
916 *  pointer to structure containing VUI data
917 *
918 *  @return      success or failure error code
919 *
920 ******************************************************************************
921 */
ihevce_put_mastering_disp_col_vol_sei_params(mastering_dis_col_vol_sei_params_t * ps_mdcl_sei,bitstrm_t * ps_bitstrm)922 WORD32 ihevce_put_mastering_disp_col_vol_sei_params(
923     mastering_dis_col_vol_sei_params_t *ps_mdcl_sei, bitstrm_t *ps_bitstrm)
924 {
925     WORD32 return_status = IHEVCE_SUCCESS;
926     UWORD8 u1_payload_size = 0;
927     WORD32 c;
928 
929     u1_payload_size += 6; /* display primaries x */
930     u1_payload_size += 6; /* display primaries y */
931     u1_payload_size += 2; /* white point x */
932     u1_payload_size += 2; /* white point y */
933     u1_payload_size += 4; /* max display mastering luminance */
934     u1_payload_size += 4; /* min display mastering luminance */
935 
936     /************************************************************************/
937     /* PayloadSize : This is the size of the payload in bytes               */
938     /************************************************************************/
939     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
940     ENTROPY_TRACE("u1_payload_size", u1_payload_size);
941 
942     ASSERT(ps_mdcl_sei->u2_white_point_x <= 50000);
943 
944     ASSERT(ps_mdcl_sei->u2_white_point_y <= 50000);
945 
946     ASSERT(
947         ps_mdcl_sei->u4_max_display_mastering_luminance >
948         ps_mdcl_sei->u4_min_display_mastering_luminance);
949 
950     /*******************************************************************************/
951     /* Put the mastering display colour volume SEI parameters into the bitstream.  */
952     /* For details refer to section D.1.1 of the standard                          */
953     /*******************************************************************************/
954 
955     /* display primaries x */
956     for(c = 0; c < 3; c++)
957     {
958         ASSERT(ps_mdcl_sei->au2_display_primaries_x[c] <= 50000);
959 
960         PUT_BITS(ps_bitstrm, ps_mdcl_sei->au2_display_primaries_x[c], 16, return_status);
961         ENTROPY_TRACE("u2_display_primaries_x", ps_mdcl_sei->au2_display_primaries_x[c]);
962 
963         ASSERT(ps_mdcl_sei->au2_display_primaries_y[c] <= 50000);
964 
965         PUT_BITS(ps_bitstrm, ps_mdcl_sei->au2_display_primaries_y[c], 16, return_status);
966         ENTROPY_TRACE("u2_display_primaries_y", ps_mdcl_sei->au2_display_primaries_y[c]);
967     }
968 
969     /* white point x */
970     PUT_BITS(ps_bitstrm, ps_mdcl_sei->u2_white_point_x, 16, return_status);
971     ENTROPY_TRACE("u2_white point x", ps_mdcl_sei->u2_white_point_x);
972 
973     /* white point y */
974     PUT_BITS(ps_bitstrm, ps_mdcl_sei->u2_white_point_y, 16, return_status);
975     ENTROPY_TRACE("u2_white point y", ps_mdcl_sei->u2_white_point_y);
976 
977     /* max display mastering luminance */
978     PUT_BITS(ps_bitstrm, ps_mdcl_sei->u4_max_display_mastering_luminance, 32, return_status);
979     ENTROPY_TRACE(
980         "u4_max_display_mastering_luminance", ps_mdcl_sei->u4_max_display_mastering_luminance);
981 
982     /* min display mastering luminance */
983     PUT_BITS(ps_bitstrm, ps_mdcl_sei->u4_min_display_mastering_luminance, 32, return_status);
984     ENTROPY_TRACE(
985         "u4_max_display_mastering_luminance", ps_mdcl_sei->u4_min_display_mastering_luminance);
986 
987     return (return_status);
988 }
989 
990 /**
991 ******************************************************************************
992 *
993 *  @brief Stores user data in bitstream
994 *
995 *  @par   Description
996 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
997 *
998 *  @param[in]   ps_bitstrm
999 *  pointer to bitstream context (handle)
1000 *
1001 *  @param[in]   u4_sei_payload_length
1002 *  SEI Payload Length
1003 *
1004 *  @param[in]   pu1_sei_payload
1005 *  pointer to SEI Payload
1006 *
1007 *  @return      success or failure error code
1008 *
1009 ******************************************************************************
1010 */
ihevce_put_sei_params(UWORD32 u4_sei_payload_length,UWORD8 * pu1_sei_payload,bitstrm_t * ps_bitstrm)1011 WORD32 ihevce_put_sei_params(
1012     UWORD32 u4_sei_payload_length, UWORD8 *pu1_sei_payload, bitstrm_t *ps_bitstrm)
1013 {
1014     WORD32 return_status = IHEVCE_SUCCESS;
1015     UWORD32 i, u4_length = 0;
1016 
1017     u4_length = u4_sei_payload_length;
1018     while(u4_sei_payload_length >= 0xFF)
1019     {
1020         PUT_BITS(ps_bitstrm, (UWORD32)0xFF, 8, return_status);
1021         u4_sei_payload_length -= 0xFF;
1022     }
1023     PUT_BITS(ps_bitstrm, (UWORD32)u4_sei_payload_length, 8, return_status);
1024 
1025     for(i = 0; i < u4_length; i++)
1026     {
1027         PUT_BITS(ps_bitstrm, (UWORD32)*pu1_sei_payload, 8, return_status);
1028         pu1_sei_payload++;
1029     }
1030 
1031     return (return_status);
1032 }
1033 
1034 /**
1035 ******************************************************************************
1036 *
1037 *  @brief Stores content light level info in bitstream
1038 *
1039 *  @par   Description
1040 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
1041 *
1042 *  @param[in]   ps_bitstrm
1043 *  pointer to bitstream context (handle)
1044 *
1045 *  @param[in]
1046 *
1047 *
1048 *  @param[in]
1049 *
1050 *
1051 *  @return      success or failure error code
1052 *
1053 ******************************************************************************
1054 */
ihevce_put_cll_info_sei_params(UWORD16 u2_avg_cll,UWORD16 u2_max_cll,bitstrm_t * ps_bitstrm)1055 WORD32 ihevce_put_cll_info_sei_params(UWORD16 u2_avg_cll, UWORD16 u2_max_cll, bitstrm_t *ps_bitstrm)
1056 {
1057     WORD32 return_status = IHEVCE_SUCCESS;
1058     UWORD8 u1_payload_size;
1059 
1060     u1_payload_size = 4;
1061     /************************************************************************/
1062     /* PayloadSize : This is the size of the payload in bytes               */
1063     /************************************************************************/
1064     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
1065     ENTROPY_TRACE("u1_payload_size", u1_payload_size);
1066 
1067     PUT_BITS(ps_bitstrm, (UWORD32)u2_avg_cll, 16, return_status);
1068     PUT_BITS(ps_bitstrm, (UWORD32)u2_max_cll, 16, return_status);
1069 
1070     return (return_status);
1071 }
1072 
1073 /**
1074 ******************************************************************************
1075 *
1076 *  @brief Generates Recovery Point (Supplemental Enhancement Information )
1077 *
1078 *  @par   Description
1079 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
1080 *
1081 *  @param[in]   ps_bitstrm
1082 *  pointer to bitstream context (handle)
1083 *
1084 *  @param[in]   ps_rp_sei
1085 *  pointer to structure containing recovery point SEI data
1086 *
1087 *  @param[in]   ps_vui_params
1088 *  pointer to structure containing VUI data
1089 *
1090 *  @return      success or failure error code
1091 *
1092 ******************************************************************************
1093 */
ihevce_put_recovery_point_sei_params(recovery_point_sei_params_t * ps_rp_sei,bitstrm_t * ps_bitstrm)1094 WORD32 ihevce_put_recovery_point_sei_params(
1095     recovery_point_sei_params_t *ps_rp_sei, bitstrm_t *ps_bitstrm)
1096 {
1097     WORD32 return_status = IHEVCE_SUCCESS;
1098     UWORD8 u1_payload_size = 0;
1099 
1100     {
1101         UWORD32 range, val;
1102         WORD32 recov_point = ps_rp_sei->i4_recovery_poc_cnt;
1103         val = 0;
1104         range = 0;
1105 
1106         if(recov_point <= 0)
1107             val = ((-recov_point) << 1);
1108         else
1109             val = (recov_point << 1) - 1;
1110 
1111         GETRANGE(range, val);
1112 
1113         u1_payload_size += (((range - 1) << 1) + 1);
1114 
1115         u1_payload_size += 1;
1116         u1_payload_size += 1;
1117     }
1118 
1119     u1_payload_size = (u1_payload_size + 7) >> 3;
1120     /************************************************************************/
1121     /* PayloadSize : This is the size of the payload in bytes               */
1122     /************************************************************************/
1123     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
1124     ENTROPY_TRACE("u1_payload_size", u1_payload_size);
1125     /************************************************************************/
1126     /* Put the recovery point SEI parameters into the bitstream. For      */
1127     /* details refer to section D.1.1 of the standard                       */
1128     /************************************************************************/
1129 
1130     /* i4_recovery_poc_cnt */
1131     PUT_BITS_SEV(ps_bitstrm, ps_rp_sei->i4_recovery_poc_cnt, return_status);
1132     ENTROPY_TRACE("i4_recovery_poc_cnt", ps_rp_sei->i4_recovery_poc_cnt);
1133 
1134     /* u1_exact_match_flag */
1135     PUT_BITS(ps_bitstrm, ps_rp_sei->u1_exact_match_flag, 1, return_status);
1136     ENTROPY_TRACE("exact_match_flag", ps_rp_sei->u1_exact_match_flag);
1137 
1138     /* u1_broken_link_flag */
1139     PUT_BITS(ps_bitstrm, ps_rp_sei->u1_broken_link_flag, 1, return_status);
1140     ENTROPY_TRACE("broken_link_flag", ps_rp_sei->u1_broken_link_flag);
1141 
1142     return (return_status);
1143 }
1144 
1145 /**
1146 ******************************************************************************
1147 *
1148 *  @brief Generates Picture timing (Supplemental Enhancement Information )
1149 *
1150 *  @par   Description
1151 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
1152 *
1153 *  @param[in]   ps_bitstrm
1154 *  pointer to bitstream context (handle)
1155 *
1156 *  @param[in]   ps_pt_sei
1157 *  pointer to structure containing oicture timing SEI data
1158 *
1159 *  @param[in]   ps_vui_params
1160 *  pointer to structure containing VUI data
1161 *
1162 *  @return      success or failure error code
1163 *
1164 ******************************************************************************
1165 */
ihevce_put_pic_timing_sei_params(pic_timing_sei_params_t * ps_pt_sei,vui_t * ps_vui_params,bitstrm_t * ps_bitstrm)1166 WORD32 ihevce_put_pic_timing_sei_params(
1167     pic_timing_sei_params_t *ps_pt_sei, vui_t *ps_vui_params, bitstrm_t *ps_bitstrm)
1168 {
1169     UWORD32 i;
1170     UWORD8 u1_payload_size = 0;
1171     WORD32 return_status = IHEVCE_SUCCESS;
1172     UWORD8 u1_au_cpb_removal_delay_length =
1173         ps_vui_params->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 + 1;
1174 
1175     UWORD8 u1_dpb_output_delay_length =
1176         ps_vui_params->s_vui_hrd_parameters.u1_dpb_output_delay_length_minus1 + 1;
1177 
1178     UWORD8 u1_du_cpb_removal_delay_increment_length =
1179         ps_vui_params->s_vui_hrd_parameters.u1_du_cpb_removal_delay_increment_length_minus1 + 1;
1180 
1181     UWORD8 u1_sub_pic_cpb_params_present_flag =
1182         ps_vui_params->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag;
1183 
1184     UWORD8 u1_sub_pic_cpb_params_in_pt_sei_flag =
1185         ps_vui_params->s_vui_hrd_parameters.u1_sub_pic_cpb_params_in_pic_timing_sei_flag;
1186 
1187     {
1188         if(1 == ps_vui_params->u1_frame_field_info_present_flag)
1189         {
1190             u1_payload_size += 4;
1191             u1_payload_size += 2;
1192             u1_payload_size += 1;
1193         }
1194 
1195         if(ps_vui_params->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag ||
1196            ps_vui_params->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag)
1197         {
1198             u1_payload_size += u1_au_cpb_removal_delay_length;
1199             u1_payload_size += u1_dpb_output_delay_length;
1200         }
1201 
1202         if(u1_sub_pic_cpb_params_in_pt_sei_flag && u1_sub_pic_cpb_params_present_flag)
1203         {
1204             UWORD32 range, val = ps_pt_sei->u4_num_decoding_units_minus1;
1205             range = 0;
1206 
1207             GETRANGE(range, val);
1208             u1_payload_size += (((range - 1) << 1) + 1);
1209 
1210             u1_payload_size += 1;
1211             if(1 == ps_pt_sei->u1_du_common_cpb_removal_delay_flag)
1212             {
1213                 u1_payload_size += u1_du_cpb_removal_delay_increment_length;
1214 
1215                 for(i = 0; i <= ps_pt_sei->u4_num_decoding_units_minus1; i++)
1216                 {
1217                     val = ps_pt_sei->au4_num_nalus_in_du_minus1[0];
1218                     range = 0;
1219 
1220                     GETRANGE(range, val);
1221                     u1_payload_size += (((range - 1) << 1) + 1);
1222 
1223                     if((1 != ps_pt_sei->u1_du_common_cpb_removal_delay_flag) &&
1224                        (i < ps_pt_sei->u4_num_decoding_units_minus1))
1225                     {
1226                         u1_payload_size += u1_du_cpb_removal_delay_increment_length;
1227                     }
1228                 }
1229             }
1230         }
1231     }
1232 
1233     ASSERT((ps_pt_sei->u4_au_cpb_removal_delay_minus1 < (1 << u1_au_cpb_removal_delay_length)));
1234 
1235     u1_payload_size = (u1_payload_size + 7) >> 3;
1236     /************************************************************************/
1237     /* PayloadSize : This is the size of the payload in bytes               */
1238     /************************************************************************/
1239     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
1240     ENTROPY_TRACE("u1_payload_size", u1_payload_size);
1241 
1242     /************************************************************************/
1243     /* Put the picture timing SEI parameters into the bitstream. For        */
1244     /* details refer to section D.1.2 of the standard                       */
1245     /************************************************************************/
1246 
1247     if(1 == ps_vui_params->u1_frame_field_info_present_flag)
1248     {
1249         PUT_BITS(ps_bitstrm, ps_pt_sei->u4_pic_struct, 4, return_status);
1250         ENTROPY_TRACE("pic_struct", ps_pt_sei->u4_pic_struct);
1251 
1252         PUT_BITS(ps_bitstrm, ps_pt_sei->u4_source_scan_type, 2, return_status);
1253         ENTROPY_TRACE("source_scan_type", ps_pt_sei->u4_source_scan_type);
1254 
1255         PUT_BITS(ps_bitstrm, ps_pt_sei->u1_duplicate_flag, 1, return_status);
1256         ENTROPY_TRACE("duplicate_flag", ps_pt_sei->u1_duplicate_flag);
1257     }
1258 
1259     if(ps_vui_params->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag ||
1260        ps_vui_params->s_vui_hrd_parameters
1261            .u1_vcl_hrd_parameters_present_flag)  // condition from standard when CpbDpbDelaysPresentFlag flag will be present
1262     {
1263         PUT_BITS(
1264             ps_bitstrm,
1265             ps_pt_sei->u4_au_cpb_removal_delay_minus1,
1266             u1_au_cpb_removal_delay_length,
1267             return_status);
1268         ENTROPY_TRACE("cpb_removal_delay_minus1", ps_pt_sei->u4_au_cpb_removal_delay_minus1);
1269 
1270         PUT_BITS(
1271             ps_bitstrm,
1272             ps_pt_sei->u4_pic_dpb_output_delay,
1273             u1_dpb_output_delay_length,
1274             return_status);
1275         ENTROPY_TRACE("pic_dpb_output_delay", ps_pt_sei->u4_pic_dpb_output_delay);
1276 
1277         if(u1_sub_pic_cpb_params_present_flag)
1278         {
1279             PUT_BITS(
1280                 ps_bitstrm,
1281                 ps_pt_sei->u4_pic_dpb_output_du_delay,
1282                 ps_vui_params->s_vui_hrd_parameters.u1_dpb_output_delay_du_length_minus1,
1283                 return_status);
1284             ENTROPY_TRACE("pic_dpb_output_du_delay", ps_pt_sei->u4_pic_dpb_output_delay);
1285         }
1286 
1287         if(u1_sub_pic_cpb_params_in_pt_sei_flag && u1_sub_pic_cpb_params_present_flag)
1288         {
1289             PUT_BITS_UEV(ps_bitstrm, ps_pt_sei->u4_num_decoding_units_minus1, return_status);
1290             ENTROPY_TRACE("num_decoding_units_minus1", ps_pt_sei->u4_num_decoding_units_minus1);
1291 
1292             PUT_BITS(ps_bitstrm, ps_pt_sei->u1_du_common_cpb_removal_delay_flag, 1, return_status);
1293             ENTROPY_TRACE(
1294                 "du_common_cpb_removal_delay_flag", ps_pt_sei->u4_du_common_cpb_removal_delay_flag);
1295 
1296             if(1 == ps_pt_sei->u1_du_common_cpb_removal_delay_flag)
1297             {
1298                 PUT_BITS(
1299                     ps_bitstrm,
1300                     ps_pt_sei->u4_du_common_cpb_removal_delay_increment_minus1,
1301                     u1_du_cpb_removal_delay_increment_length,
1302                     return_status);
1303                 ENTROPY_TRACE(
1304                     "du_common_cpb_removal_delay_increment_minus1",
1305                     ps_pt_sei->u4_du_common_cpb_removal_delay_increment_minus1);
1306             }
1307 
1308             for(i = 0; i <= ps_pt_sei->u4_num_decoding_units_minus1; i++)
1309             {
1310                 PUT_BITS_UEV(ps_bitstrm, ps_pt_sei->au4_num_nalus_in_du_minus1[0], return_status);
1311                 ENTROPY_TRACE("num_nalus_in_du_minus1", ps_pt_sei->u4_num_nalus_in_du_minus1);
1312 
1313                 if((1 != ps_pt_sei->u1_du_common_cpb_removal_delay_flag) &&
1314                    (i < ps_pt_sei->u4_num_decoding_units_minus1))
1315                 {
1316                     PUT_BITS(
1317                         ps_bitstrm,
1318                         ps_pt_sei->au4_du_cpb_removal_delay_increment_minus1[0],
1319                         u1_du_cpb_removal_delay_increment_length,
1320                         return_status);
1321                     ENTROPY_TRACE(
1322                         "du_cpb_removal_delay_increment_minus1",
1323                         ps_pt_sei->u4_du_cpb_removal_delay_increment_minus1);
1324                 }
1325             }
1326         }
1327     }
1328 
1329     return (return_status);
1330 }
1331 
1332 /**
1333 ******************************************************************************
1334 *
1335 *  @brief Generates Hash (Supplemental Enhancement Information )
1336 *
1337 *  @par   Description
1338 *  Put hash sei params
1339 *
1340 *  @param[in]   ps_hash_sei_params
1341 *  pointer to structure containing hash SEI data
1342 *
1343 *  @param[in]   i1_decoded_pic_hash_sei_flag
1344 *  flag saying the hash SEI type
1345 *
1346 *  @param[in]   ps_bitstrm
1347 *  pointer to bitstream context (handle)
1348 *
1349 *  @return      success or failure error code
1350 *
1351 ******************************************************************************
1352 */
ihevce_put_hash_sei_params(hash_sei_param_t * ps_hash_sei_params,WORD8 i1_decoded_pic_hash_sei_flag,bitstrm_t * ps_bitstrm)1353 WORD32 ihevce_put_hash_sei_params(
1354     hash_sei_param_t *ps_hash_sei_params, WORD8 i1_decoded_pic_hash_sei_flag, bitstrm_t *ps_bitstrm)
1355 {
1356     UWORD32 i, c_idx, val;
1357     WORD32 return_status = IHEVCE_SUCCESS;
1358     UWORD8 u1_payload_size = 0;
1359 
1360     u1_payload_size += 1; /* hash_type */
1361 
1362     if(1 == i1_decoded_pic_hash_sei_flag)
1363     {
1364         /* MD5 : 3 color planes * 16 byte values */
1365         u1_payload_size += (16 * 3);
1366     }
1367     else if(2 == i1_decoded_pic_hash_sei_flag)
1368     {
1369         /* CRC : 3 color planes * 2 byte values */
1370         u1_payload_size += (2 * 3);
1371     }
1372     else if(3 == i1_decoded_pic_hash_sei_flag)
1373     {
1374         /* Checksum : 3 color planes * 4 byte values */
1375         u1_payload_size += (4 * 3);
1376     }
1377     else
1378     {
1379         ASSERT(0);
1380     }
1381 
1382     /************************************************************************/
1383     /* PayloadSize : This is the size of the payload in bytes               */
1384     /************************************************************************/
1385     PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status);
1386     ENTROPY_TRACE("payload_size", u1_payload_size);
1387 
1388     /************************************************************************/
1389     /* Put the hash SEI parameters into the bitstream. For                  */
1390     /* details refer to section D.2.19 of the standard                      */
1391     /************************************************************************/
1392 
1393     PUT_BITS(ps_bitstrm, (i1_decoded_pic_hash_sei_flag - 1), 8, return_status);
1394     ENTROPY_TRACE("hash_type", (i1_decoded_pic_hash_sei_flag - 1));
1395 
1396     if(1 == i1_decoded_pic_hash_sei_flag)
1397     {
1398         for(c_idx = 0; c_idx < 3; c_idx++)
1399         {
1400             for(i = 0; i < 16; i++)
1401             {
1402                 PUT_BITS(ps_bitstrm, ps_hash_sei_params->au1_sei_hash[c_idx][i], 8, return_status);
1403                 ENTROPY_TRACE("picture_md5", ps_hash_sei_params->au1_sei_hash[c_idx][i]);
1404             }
1405         }
1406     }
1407     else if(2 == i1_decoded_pic_hash_sei_flag)
1408     {
1409         for(c_idx = 0; c_idx < 3; c_idx++)
1410         {
1411             val = (ps_hash_sei_params->au1_sei_hash[c_idx][0] << 8) +
1412                   ps_hash_sei_params->au1_sei_hash[c_idx][1];
1413             PUT_BITS(ps_bitstrm, val, 16, return_status);
1414             ENTROPY_TRACE("picture_crc", val);
1415         }
1416     }
1417     else if(3 == i1_decoded_pic_hash_sei_flag)
1418     {
1419         for(c_idx = 0; c_idx < 3; c_idx++)
1420         {
1421             val = (ps_hash_sei_params->au1_sei_hash[c_idx][0] << 24) +
1422                   (ps_hash_sei_params->au1_sei_hash[c_idx][1] << 16) +
1423                   (ps_hash_sei_params->au1_sei_hash[c_idx][2] << 8) +
1424                   (ps_hash_sei_params->au1_sei_hash[c_idx][3]);
1425 
1426             PUT_BITS(ps_bitstrm, val, 32, return_status);
1427             ENTROPY_TRACE("picture_checksum", val);
1428         }
1429     }
1430     else
1431     {
1432         ASSERT(0);
1433     }
1434 
1435     return (return_status);
1436 }
1437 
1438 /**
1439 ******************************************************************************
1440 *
1441 *  @brief Generates SEI (Supplemental Enhancement Information )
1442 *
1443 *  @par   Description
1444 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
1445 *
1446 *  @param[in]   e_payload_type
1447 *  Determines the type of SEI msg
1448 *
1449 *  @param[in]   ps_bitstrm
1450 *  pointer to bitstream context (handle)
1451 *
1452 *  @param[in]   ps_sei_params
1453 *  pointer to structure containing SEI data
1454 *               buffer period, recovery point, picture timing
1455 *
1456 *  @param[in]   ps_vui_params
1457 *  pointer to structure containing VUI data
1458 *
1459 *  @return      success or failure error code
1460 *
1461 ******************************************************************************
1462 */
ihevce_put_sei_msg(IHEVCE_SEI_TYPE e_payload_type,sei_params_t * ps_sei_params,vui_t * ps_vui_params,bitstrm_t * ps_bitstrm,UWORD32 i4_sei_payload_length,UWORD8 * pu1_sei_payload)1463 WORD32 ihevce_put_sei_msg(
1464     IHEVCE_SEI_TYPE e_payload_type,
1465     sei_params_t *ps_sei_params,
1466     vui_t *ps_vui_params,
1467     bitstrm_t *ps_bitstrm,
1468     UWORD32 i4_sei_payload_length,
1469     UWORD8 *pu1_sei_payload)
1470 {
1471     WORD32 return_status = IHEVCE_SUCCESS;
1472     /************************************************************************/
1473     /* PayloadType : Send in the SEI type in the stream                     */
1474     /************************************************************************/
1475     UWORD32 u4_payload_type = e_payload_type;
1476     while(u4_payload_type > 0xFF)
1477     {
1478         PUT_BITS(ps_bitstrm, 0xFF, 8, return_status);
1479         u4_payload_type -= 0xFF;
1480     }
1481     PUT_BITS(ps_bitstrm, (UWORD32)u4_payload_type, 8, return_status);
1482     ENTROPY_TRACE("e_payload_type", e_payload_type);
1483 
1484     /************************************************************************/
1485     /* PayloadSize : This is sent from within the type specific functions   */
1486     /************************************************************************/
1487 
1488     switch(e_payload_type)
1489     {
1490     case IHEVCE_SEI_BUF_PERIOD_T:
1491         return_status |= ihevce_put_buf_period_sei_params(
1492             &(ps_sei_params->s_buf_period_sei_params), ps_vui_params, ps_bitstrm);
1493         break;
1494 
1495     case IHEVCE_SEI_PIC_TIMING_T:
1496         return_status |= ihevce_put_pic_timing_sei_params(
1497             &(ps_sei_params->s_pic_timing_sei_params), ps_vui_params, ps_bitstrm);
1498         break;
1499 
1500     case IHEVCE_SEI_RECOVERY_POINT_T:
1501         return_status |= ihevce_put_recovery_point_sei_params(
1502             &(ps_sei_params->s_recovery_point_params), ps_bitstrm);
1503         break;
1504     case IHEVCE_SEI_ACTIVE_PARAMETER_SETS_T:
1505         return_status |= ihevce_put_active_parameter_set_sei_params(
1506             &(ps_sei_params->s_active_parameter_set_sei_params), ps_bitstrm);
1507         break;
1508     case IHEVCE_SEI_DECODED_PICTURE_HASH_T:
1509         return_status |= ihevce_put_hash_sei_params(
1510             &(ps_sei_params->s_hash_sei_params),
1511             ps_sei_params->i1_decoded_pic_hash_sei_flag,
1512             ps_bitstrm);
1513         break;
1514     case IHEVCE_SEI_MASTERING_DISP_COL_VOL_T:
1515         return_status |= ihevce_put_mastering_disp_col_vol_sei_params(
1516             &(ps_sei_params->s_mastering_dis_col_vol_sei_params), ps_bitstrm);
1517         break;
1518     case IHEVCE_SEI_CONTENT_LIGHT_LEVEL_DATA_T:
1519         return_status |= ihevce_put_cll_info_sei_params(
1520             ps_sei_params->s_cll_info_sei_params.u2_sei_avg_cll,
1521             ps_sei_params->s_cll_info_sei_params.u2_sei_max_cll,
1522             ps_bitstrm);
1523         break;
1524     //case IHEVCE_SEI_USER_DATA_REGISTERED_ITU_T_T35_T:
1525     //return_status |= ihevce_put_sei_params(i4_sei_payload_length, pu1_sei_payload, ps_bitstrm);
1526     //break;
1527     default:
1528         //Any Payload type other than the above cases will entred hereand be added to the bitstream
1529         return_status |= ihevce_put_sei_params(i4_sei_payload_length, pu1_sei_payload, ps_bitstrm);
1530         //return_status = IHEVCE_FAIL;
1531     }
1532 
1533     ASSERT(IHEVCE_SUCCESS == return_status);
1534 
1535     /* rbsp trailing bits */
1536     if((IHEVCE_SUCCESS == return_status) && (ps_bitstrm->i4_bits_left_in_cw & 0x7))
1537         ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1538 
1539     return (return_status);
1540 }
1541 
1542 /**
1543 ******************************************************************************
1544 *
1545 *  @brief Generates SEI (Supplemental Enhancement Information )
1546 *
1547 *  @par   Description
1548 *  Parse Supplemental Enhancement Information as per Section 7.3.2.4
1549 *
1550 *  @param[in]   ps_bitstrm
1551 *  pointer to bitstream context (handle)
1552 *
1553 *  @param[in]   ps_sei_params
1554 *  pointer to structure containing SEI data
1555 *               buffer period, recovery point, picture timing
1556 *
1557 *  @param[in]   ps_vui_params
1558 *  pointer to structure containing VUI data
1559 *
1560 *  @param[in]   nal_unit_header
1561 *  NAL_PREFIX_SEI / NAL_SUFFIX_SEI
1562 *
1563 *  @param[in]   u4_num_sei_payloads
1564 *  Number of SEI payloads
1565 *
1566 *  @param[in]   ps_sei_payload
1567 *  pointer to structure containing SEI payload data
1568 *
1569 *  @return      success or failure error code
1570 *
1571 ******************************************************************************
1572 */
ihevce_generate_sei(bitstrm_t * ps_bitstrm,sei_params_t * ps_sei_params,vui_t * ps_vui_params,WORD32 insert_per_cra,WORD32 nal_unit_header,UWORD32 u4_num_sei_payloads,sei_payload_t * ps_sei_payload)1573 WORD32 ihevce_generate_sei(
1574     bitstrm_t *ps_bitstrm,
1575     sei_params_t *ps_sei_params,
1576     vui_t *ps_vui_params,
1577     WORD32 insert_per_cra,
1578     WORD32 nal_unit_header,
1579     UWORD32 u4_num_sei_payloads,
1580     sei_payload_t *ps_sei_payload)
1581 {
1582     UWORD32 u4_i;
1583     WORD32 return_status = IHEVCE_SUCCESS;
1584 
1585     (void)insert_per_cra;
1586     /* Insert Start Code */
1587     return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1588 
1589     ASSERT((NAL_PREFIX_SEI == nal_unit_header) || (NAL_SUFFIX_SEI == nal_unit_header));
1590     /* Insert Nal Unit Header */
1591     return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, nal_unit_header, 0);
1592 
1593     if(NAL_PREFIX_SEI == nal_unit_header)
1594     {
1595         /* Active Parameter and Buffering period insertion */
1596 
1597         if(ps_sei_params->i1_buf_period_params_present_flag)
1598         {
1599             /* insert active_parameter_set SEI required if buffering period SEI messages are inserted */
1600             return_status |= ihevce_put_sei_msg(
1601                 IHEVCE_SEI_ACTIVE_PARAMETER_SETS_T,
1602                 ps_sei_params,
1603                 ps_vui_params,
1604                 ps_bitstrm,
1605                 0,
1606                 NULL);
1607 
1608             /*************************************************************************************************/
1609             /* NOTE: Need to terminate and start new SEI message after active parameter set SEI              */
1610             /* Buffering period/pic timing SEI refering to active SPS cannot be embedded in same SEI message */
1611             /* This is because SPS is activated in HM decoder after completely parsing full SEI message.     */
1612             /*************************************************************************************************/
1613             if(1) /* Insert New SEI for buffering period after active parameter set SEI */
1614             {
1615                 ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1616 
1617                 /* Insert Next SEI Start Code */
1618                 return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1619 
1620                 /* Insert Next SEI Nal Unit Header */
1621                 return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, nal_unit_header, 0);
1622             }
1623 
1624             /* Buffering Period SEI meassage for all IDR, CRA pics */
1625             return_status |= ihevce_put_sei_msg(
1626                 IHEVCE_SEI_BUF_PERIOD_T, ps_sei_params, ps_vui_params, ps_bitstrm, 0, NULL);
1627         }
1628 
1629         /* Pic timing SEI meassage for non IDR, non CRA pics */
1630         if(ps_sei_params->i1_pic_timing_params_present_flag)
1631         {
1632             return_status |= ihevce_put_sei_msg(
1633                 IHEVCE_SEI_PIC_TIMING_T, ps_sei_params, ps_vui_params, ps_bitstrm, 0, NULL);
1634         }
1635 
1636         /* Recovery point SEI meassage for all IDR, CRA pics */
1637         if(ps_sei_params->i1_recovery_point_params_present_flag)
1638         {
1639             return_status |= ihevce_put_sei_msg(
1640                 IHEVCE_SEI_RECOVERY_POINT_T, ps_sei_params, ps_vui_params, ps_bitstrm, 0, NULL);
1641         }
1642 
1643         /* Mastering Display Colour SEI for all IDR, CRA pics */
1644         if(ps_sei_params->i4_sei_mastering_disp_colour_vol_params_present_flags)
1645         {
1646             return_status |= ihevce_put_sei_msg(
1647                 IHEVCE_SEI_MASTERING_DISP_COL_VOL_T,
1648                 ps_sei_params,
1649                 ps_vui_params,
1650                 ps_bitstrm,
1651                 0,
1652                 NULL);
1653         }
1654         /*Registered User Data*/
1655         for(u4_i = 0; u4_i < u4_num_sei_payloads; u4_i++)
1656         {
1657             return_status |= ihevce_put_sei_msg(
1658                 (IHEVCE_SEI_TYPE)(ps_sei_payload[u4_i].u4_payload_type),
1659                 ps_sei_params,
1660                 ps_vui_params,
1661                 ps_bitstrm,
1662                 ps_sei_payload[u4_i].u4_payload_length,
1663                 ps_sei_payload[u4_i].pu1_sei_payload);
1664         }
1665         /* Content Light Level Information*/
1666         if(ps_sei_params->i1_sei_cll_enable)
1667         {
1668             return_status |= ihevce_put_sei_msg(
1669                 IHEVCE_SEI_CONTENT_LIGHT_LEVEL_DATA_T,
1670                 ps_sei_params,
1671                 ps_vui_params,
1672                 ps_bitstrm,
1673                 0,
1674                 NULL);
1675         }
1676     }
1677     else if(NAL_SUFFIX_SEI == nal_unit_header)
1678     {
1679         /* Insert hash SEI */
1680         if(ps_sei_params->i1_decoded_pic_hash_sei_flag)
1681         {
1682             return_status |= ihevce_put_sei_msg(
1683                 IHEVCE_SEI_DECODED_PICTURE_HASH_T,
1684                 ps_sei_params,
1685                 ps_vui_params,
1686                 ps_bitstrm,
1687                 0,
1688                 NULL);
1689         }
1690     }
1691 
1692     /*put trailing bits to indicate end of sei*/
1693     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1694 
1695     return return_status;
1696 }
1697 
1698 /**
1699 ******************************************************************************
1700 *
1701 *  @brief Populates ihevce_populate_mastering_disp_col_vol_sei of SEI  structure
1702 *
1703 *  @par   Description
1704 *  Populates mastering display colour volume sei structure
1705 *
1706 *  @param[in]  ps_sei
1707 *  pointer to sei params that needs to be populated
1708 *
1709 *  @param[in]  ps_out_strm_prms
1710 *  pointer to output stream params
1711 *
1712 *  @return      success or failure error code
1713 *
1714 ******************************************************************************
1715 */
ihevce_populate_mastering_disp_col_vol_sei(sei_params_t * ps_sei,ihevce_out_strm_params_t * ps_out_strm_prms)1716 WORD32 ihevce_populate_mastering_disp_col_vol_sei(
1717     sei_params_t *ps_sei, ihevce_out_strm_params_t *ps_out_strm_prms)
1718 {
1719     WORD32 i;
1720 
1721     mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol_sei_params =
1722         &ps_sei->s_mastering_dis_col_vol_sei_params;
1723 
1724     for(i = 0; i < 3; i++)
1725     {
1726         ps_mastering_dis_col_vol_sei_params->au2_display_primaries_x[i] =
1727             ps_out_strm_prms->au2_display_primaries_x[i];
1728         ps_mastering_dis_col_vol_sei_params->au2_display_primaries_y[i] =
1729             ps_out_strm_prms->au2_display_primaries_y[i];
1730     }
1731 
1732     ps_mastering_dis_col_vol_sei_params->u2_white_point_x = ps_out_strm_prms->u2_white_point_x;
1733     ps_mastering_dis_col_vol_sei_params->u2_white_point_y = ps_out_strm_prms->u2_white_point_y;
1734 
1735     ps_mastering_dis_col_vol_sei_params->u4_max_display_mastering_luminance =
1736         ps_out_strm_prms->u4_max_display_mastering_luminance;
1737     ps_mastering_dis_col_vol_sei_params->u4_min_display_mastering_luminance =
1738         ps_out_strm_prms->u4_min_display_mastering_luminance;
1739 
1740     return IHEVCE_SUCCESS;
1741 }
1742 
1743 /**
1744 ******************************************************************************
1745 *
1746 *  @brief Populates ihevce_populate_recovery_point_sei of SEI  structure
1747 *
1748 *  @par   Description
1749 *  Populates vui structure for its use in header generation
1750 *
1751 *  @param[out]  ps_sei
1752 *  pointer to sei params that needs to be populated
1753 *
1754 *  @param[out]  ps_vui
1755 *  pointer to vui params referred by sei
1756 *
1757 *  @param[out]  ps_sps
1758 *  pointer to sps params referred by sei
1759 *
1760 *  @param[in]   ps_src_params
1761 *  pointer to source config params; resolution, frame rate etc
1762 *
1763 *  @param[in]   ps_config_prms
1764 *  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
1765 *
1766 *  @return      success or failure error code
1767 *
1768 ******************************************************************************
1769 */
ihevce_populate_recovery_point_sei(sei_params_t * ps_sei,ihevce_vui_sei_params_t * ps_vui_sei_prms)1770 WORD32 ihevce_populate_recovery_point_sei(
1771     sei_params_t *ps_sei, ihevce_vui_sei_params_t *ps_vui_sei_prms)
1772 {
1773     recovery_point_sei_params_t *ps_rec_point_params = &ps_sei->s_recovery_point_params;
1774 
1775     (void)ps_vui_sei_prms;
1776     ps_rec_point_params->i4_recovery_poc_cnt = 0;
1777     ps_rec_point_params->u1_broken_link_flag = 0;
1778     ps_rec_point_params->u1_exact_match_flag = 1;
1779 
1780     return IHEVCE_SUCCESS;
1781 }
1782 
1783 /**
1784 ******************************************************************************
1785 *
1786 *  @brief Populates picture timing of SEI  structure
1787 *
1788 *  @par   Description
1789 *  Populates vui structure for its use in header generation
1790 *
1791 *  @param[out]  ps_sei
1792 *  pointer to sei params that needs to be populated
1793 *
1794 *  @param[out]  ps_vui
1795 *  pointer to vui params referred by sei
1796 *
1797 *  @param[in]   ps_src_params
1798 *  pointer to source config params; resolution, frame rate etc
1799 *
1800 *  @param[in]   u4_bottom_field_flag
1801 *  Used only for interlaced field coding. 0:top field, 1:bottom field
1802 *
1803 *  @return      success or failure error code
1804 *
1805 ******************************************************************************
1806 */
ihevce_populate_picture_timing_sei(sei_params_t * ps_sei,vui_t * ps_vui,ihevce_src_params_t * ps_src_params,WORD32 u4_bottom_field_flag)1807 WORD32 ihevce_populate_picture_timing_sei(
1808     sei_params_t *ps_sei,
1809     vui_t *ps_vui,
1810     ihevce_src_params_t *ps_src_params,
1811     WORD32 u4_bottom_field_flag)
1812 {
1813     pic_timing_sei_params_t *ps_pic_timing_params = &ps_sei->s_pic_timing_sei_params;
1814     UWORD8 u1_prog_seq = !ps_src_params->i4_field_pic;
1815     UWORD8 u1_top_field_first = 1;  //ps_curr_inp->s_input_buf.i4_topfield_first;
1816 
1817     UWORD8 u1_repeat_first_field = 0;
1818     WORD32 field_seq_flag = ps_vui->u1_field_seq_flag;
1819 
1820     if(ps_vui->u1_frame_field_info_present_flag)
1821     {
1822         /**************************************************************************/
1823         /* Refer Table D-1                                                        */
1824         /**************************************************************************/
1825         if(0 == u1_prog_seq)
1826         {
1827             if(field_seq_flag)
1828             {
1829                 ASSERT((u4_bottom_field_flag == 0) || (u4_bottom_field_flag == 1));
1830 
1831                 /* 1 => top field pic */
1832                 /* 2 => bottom field pic */
1833                 ps_pic_timing_params->u4_pic_struct = 1 + u4_bottom_field_flag;
1834             }
1835             else if(0 == u1_repeat_first_field)
1836             {
1837                 /******************************************************************/
1838                 /* [PROGRESSIVE SEQ]    = 0;                                      */
1839                 /* [MPEG2 PIC STRUCT]   = FIELD_PICTURE                           */
1840                 /* [REPEAT_FIRST_FIELD] = 0                                       */
1841                 /* u1_pic_struct = 3 => top    - bottom field pic                 */
1842                 /* u1_pic_struct = 4 => bottom - top                              */
1843                 /******************************************************************/
1844                 ps_pic_timing_params->u4_pic_struct = 4 - u1_top_field_first;
1845             }
1846             else
1847             {
1848                 /******************************************************************/
1849                 /* [PROGRESSIVE SEQ]    = 0;                                      */
1850                 /* [MPEG2 PIC STRUCT]   = FIELD_PICTURE                           */
1851                 /* [REPEAT_FIRST_FIELD] = 1                                       */
1852                 /* u1_pic_struct = 5 => top    - bottom - top                     */
1853                 /* u1_pic_struct = 6 => bottom - top    - bottom                  */
1854                 /******************************************************************/
1855                 ps_pic_timing_params->u4_pic_struct = 6 - u1_top_field_first;
1856             }
1857         }
1858         else
1859         {
1860             if(0 == u1_repeat_first_field)
1861             {
1862                 /******************************************************************/
1863                 /* [PROGRESSIVE SEQ]    = 1;                                      */
1864                 /* [MPEG2 PIC STRUCT]   = FRAME_PICTURE                           */
1865                 /* u1_pic_struct = 0 => frame picture (no repeat)                 */
1866                 /******************************************************************/
1867                 ps_pic_timing_params->u4_pic_struct = 0;
1868             }
1869             else
1870             {
1871                 /******************************************************************/
1872                 /* [PROGRESSIVE SEQ]    = 1;                                      */
1873                 /* [MPEG2 PIC STRUCT]   = FRAME_PICTURE                           */
1874                 /* u1_pic_struct = 7 => frame picture (repeat once)               */
1875                 /* u1_pic_struct = 8 => frame picture (repeat twice)              */
1876                 /******************************************************************/
1877                 ps_pic_timing_params->u4_pic_struct = 7 + u1_top_field_first;
1878             }
1879         }
1880         /* Porogressive frame - 1 ; Interlace - 0 */
1881         ps_pic_timing_params->u4_source_scan_type = !ps_src_params->i4_field_pic;
1882 
1883         ps_pic_timing_params->u1_duplicate_flag = 0;
1884     }
1885     ps_pic_timing_params->u4_pic_dpb_output_du_delay = 0;
1886 
1887     ps_pic_timing_params->u4_num_decoding_units_minus1 = 1;
1888 
1889     ps_pic_timing_params->u1_du_common_cpb_removal_delay_flag = 1;
1890 
1891     ps_pic_timing_params->u4_du_common_cpb_removal_delay_increment_minus1 = 1;
1892 
1893     ps_pic_timing_params->au4_num_nalus_in_du_minus1[0] = 1;
1894 
1895     ps_pic_timing_params->au4_du_cpb_removal_delay_increment_minus1[0] = 1;
1896 
1897     return IHEVCE_SUCCESS;
1898 }
1899 
1900 /**
1901 ******************************************************************************
1902 *
1903 *  @brief Populates buffer period of sei structure
1904 *
1905 *  @par   Description
1906 *  Populates vui structure for its use in header generation
1907 *
1908 *  @param[out]  ps_sei
1909 *  pointer to sei params that needs to be populated
1910 *
1911 *  @param[out]  ps_vui
1912 *  pointer to vui params referred by sei
1913 *
1914 *  @param[out]  ps_sps
1915 *  pointer to sps params referred by sei
1916 *
1917 *  @param[out]  ps_vui_sei_prms
1918 *  pointer to sps params referred by application
1919 *
1920 *  @return      success or failure error code
1921 *
1922 ******************************************************************************
1923 */
ihevce_populate_buffering_period_sei(sei_params_t * ps_sei,vui_t * ps_vui,sps_t * ps_sps,ihevce_vui_sei_params_t * ps_vui_sei_prms)1924 WORD32 ihevce_populate_buffering_period_sei(
1925     sei_params_t *ps_sei, vui_t *ps_vui, sps_t *ps_sps, ihevce_vui_sei_params_t *ps_vui_sei_prms)
1926 {
1927     WORD32 i;
1928 
1929     buf_period_sei_params_t *ps_bp_sei = &ps_sei->s_buf_period_sei_params;
1930 
1931     WORD32 i1_sps_max_sub_layersminus1 = ps_sps->i1_sps_max_sub_layers - 1;
1932 
1933     hrd_params_t *ps_vui_hrd_parameters = &ps_vui->s_vui_hrd_parameters;
1934 
1935     sub_lyr_hrd_params_t *ps_sub_layer_hrd_params =
1936         &ps_vui_hrd_parameters->as_sub_layer_hrd_params[i1_sps_max_sub_layersminus1];
1937 
1938     WORD32 cpb_cnt = ps_vui_hrd_parameters->au1_cpb_cnt_minus1[i1_sps_max_sub_layersminus1];
1939 
1940     WORD32 cpb_size, bit_rate;
1941 
1942     (void)ps_vui_sei_prms;
1943     ps_bp_sei->u1_bp_seq_parameter_set_id = ps_sps->i1_sps_id;
1944 
1945     ps_bp_sei->u4_initial_cpb_removal_delay_length =
1946         ps_vui->s_vui_hrd_parameters.u1_initial_cpb_removal_delay_length_minus1 + 1;
1947 
1948     ps_bp_sei->u1_sub_pic_cpb_params_present_flag =
1949         ps_vui_hrd_parameters->u1_sub_pic_cpb_params_present_flag;
1950 
1951     ps_bp_sei->u1_rap_cpb_params_present_flag = 0;  //DEFAULT value
1952 
1953     ps_bp_sei->u4_cpb_delay_offset = 0;  //DEFAULT value
1954     ps_bp_sei->u4_dpb_delay_offset = 0;  //DEFAULT value
1955 
1956     ps_bp_sei->u1_concatenation_flag = 0;  //DEFAULT value ???
1957     ps_bp_sei->u4_au_cpb_removal_delay_delta_minus1 = 0;  //DEFAULT value  ???
1958 
1959     ps_bp_sei->u4_cpb_cnt = cpb_cnt;
1960 
1961     if(ps_vui->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag)
1962     {
1963         for(i = 0; i <= cpb_cnt; i++)
1964         {
1965             ULWORD64 u8_temp;
1966             if(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
1967             {
1968                 ASSERT(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag);
1969 
1970                 cpb_size = (ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i] + 1) *
1971                            (1 << (4 + ps_vui_hrd_parameters->u4_cpb_size_du_scale));
1972 
1973                 bit_rate = (ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i] + 1) *
1974                            (1 << (6 + ps_vui_hrd_parameters->u4_bit_rate_scale));
1975             }
1976             else
1977             {
1978                 cpb_size = (ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i] + 1) *
1979                            (1 << (4 + ps_vui_hrd_parameters->u4_cpb_size_scale));
1980 
1981                 bit_rate = (ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i] + 1) *
1982                            (1 << (6 + ps_vui_hrd_parameters->u4_bit_rate_scale));
1983             }
1984 
1985             u8_temp = (ULWORD64)(90000 * (ULWORD64)cpb_size);
1986             ps_bp_sei->au4_nal_initial_cpb_removal_delay[i] = (UWORD32)(u8_temp / bit_rate);
1987 
1988             ps_bp_sei->au4_nal_initial_cpb_removal_delay_offset[i] = 0;
1989 
1990             if(ps_bp_sei->u1_rap_cpb_params_present_flag ||
1991                ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
1992             {
1993                 ps_bp_sei->au4_nal_initial_alt_cpb_removal_delay[i] = (UWORD32)(u8_temp / bit_rate);
1994                 ps_bp_sei->au4_nal_initial_cpb_removal_delay_offset[i] = 0;
1995             }
1996         }
1997     }
1998 
1999     if(ps_vui->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag)
2000     {
2001         for(i = 0; i <= cpb_cnt; i++)
2002         {
2003             if(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
2004             {
2005                 ASSERT(1 == ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag);
2006                 cpb_size = (ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i] + 1) *
2007                            (1 << (4 + ps_vui_hrd_parameters->u4_cpb_size_du_scale));
2008 
2009                 bit_rate = (ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i] + 1) *
2010                            (1 << (6 + ps_vui_hrd_parameters->u4_bit_rate_scale));
2011             }
2012             else
2013             {
2014                 cpb_size = (ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i] + 1) *
2015                            (1 << (4 + ps_vui_hrd_parameters->u4_cpb_size_scale));
2016 
2017                 bit_rate = (ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i] + 1) *
2018                            (1 << (6 + ps_vui_hrd_parameters->u4_bit_rate_scale));
2019             }
2020 
2021             ps_bp_sei->au4_vcl_initial_cpb_removal_delay[i] = 90000 * (cpb_size / bit_rate);
2022 
2023             ps_bp_sei->au4_vcl_initial_cpb_removal_delay_offset[i] = 0;
2024 
2025             if(ps_bp_sei->u1_rap_cpb_params_present_flag ||
2026                ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
2027             {
2028                 ps_bp_sei->au4_vcl_initial_alt_cpb_removal_delay[i] = 90000 * (cpb_size / bit_rate);
2029                 ps_bp_sei->au4_vcl_initial_cpb_removal_delay_offset[i] = 0;
2030             }
2031         }
2032     }
2033 
2034     /* Reset picture timing cbp removal delay at every insertion Buffering period SEI */
2035     //ps_sei->s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1 = 0;
2036 
2037     return IHEVCE_SUCCESS;
2038 }
2039 
2040 /**
2041 ******************************************************************************
2042 *
2043 *  @brief Populates active parameter set sei structure
2044 *
2045 *  @par   Description
2046 *
2047 *  @param[out]  ps_sei
2048 *  pointer to sei params that needs to be populated
2049 *
2050 *  @param[in]   ps_vps
2051 *  pointer to configuration vps_t.
2052 *
2053 *  @return      success or failure error code
2054 *
2055 *****************************************************************************
2056 */
ihevce_populate_active_parameter_set_sei(sei_params_t * ps_sei,vps_t * ps_vps,sps_t * ps_sps)2057 WORD32 ihevce_populate_active_parameter_set_sei(sei_params_t *ps_sei, vps_t *ps_vps, sps_t *ps_sps)
2058 {
2059     UWORD8 i;
2060     active_parameter_set_sei_param_t *ps_act_sei = &ps_sei->s_active_parameter_set_sei_params;
2061 
2062     (void)ps_sps;
2063     ps_act_sei->u1_active_video_parameter_set_id = ps_vps->i1_vps_id;
2064     ps_act_sei->u1_self_contained_cvs_flag = 0;
2065     ps_act_sei->u1_no_parameter_set_update_flag = 1;
2066     ps_act_sei->u1_num_sps_ids_minus1 = 0;
2067     for(i = 0; i <= ps_act_sei->u1_num_sps_ids_minus1; i++)
2068     {
2069         ps_act_sei->au1_active_seq_parameter_set_id[i] = 0;
2070     }
2071 
2072     return IHEVCE_SUCCESS;
2073 }
2074 
2075 /**
2076 ******************************************************************************
2077 *
2078 *  @brief Populates Hash SEI values for CRC Hash
2079 *
2080 *  @par   Description
2081 *
2082 *  @param[out]  ps_hash_sei_params
2083 *  pointer to hash sei params that needs to be populated
2084 *
2085 *  @param[in]   bit_depth
2086 *  i4_internal_bit_depth value. Assume same for Luma & Chroma
2087 *
2088 *  @param[in]   pv_y_buf
2089 *  Pointer to decoded/recon Luma buffer
2090 *
2091 *  @param[in]   y_wd
2092 *  pic width in luma samples
2093 *
2094 *  @param[in]   y_ht
2095 *  pic height in luma samples
2096 *
2097 *  @param[in]   y_strd
2098 *  Stride of luma buffer
2099 *
2100 *  @param[in]   pv_u_buf
2101 *  Pointer to decoded/recon Chroma buffer
2102 *
2103 *  @param[in]   uv_wd
2104 *  pic width in luma samples / SubWidthC
2105 *
2106 *  @param[in]   uv_ht
2107 *  pic height in luma samples / SubHeightC
2108 *
2109 *  @param[in]   uv_strd
2110 *  Stride of chroma buffer
2111 *
2112 *  @return  None
2113 *
2114 *****************************************************************************
2115 */
ihevce_calc_CRC(hash_sei_param_t * ps_hash_sei_params,WORD32 bit_depth,void * pv_y_buf,WORD32 y_wd,WORD32 y_ht,WORD32 y_strd,void * pv_u_buf,WORD32 uv_wd,WORD32 uv_ht,WORD32 uv_strd)2116 static void ihevce_calc_CRC(
2117     hash_sei_param_t *ps_hash_sei_params,
2118     WORD32 bit_depth,
2119     void *pv_y_buf,
2120     WORD32 y_wd,
2121     WORD32 y_ht,
2122     WORD32 y_strd,
2123     void *pv_u_buf,
2124     WORD32 uv_wd,
2125     WORD32 uv_ht,
2126     WORD32 uv_strd)
2127 {
2128     WORD32 x, y, bit_idx, is_gt8bit = 0, gt8bit_mul;
2129     UWORD8 *pu_buf;
2130 
2131     /* For this to work, assumes little endian in case of HBD */
2132     if(bit_depth > 8)
2133         is_gt8bit = 1;
2134     gt8bit_mul = 1 + is_gt8bit;
2135 
2136     /* Luma CRC */
2137     {
2138         UWORD32 u4_crc_val = 0xffff;
2139 
2140         pu_buf = (UWORD8 *)pv_y_buf;
2141 
2142         for(y = 0; y < y_ht; y++)
2143         {
2144             for(x = 0; x < y_wd; x++)
2145             {
2146                 // take CRC of first pictureData byte
2147                 for(bit_idx = 0; bit_idx < 8; bit_idx++)
2148                 {
2149                     CALC_CRC_BIT_LEVEL(
2150                         u4_crc_val, pu_buf[y * (y_strd * gt8bit_mul) + (x * gt8bit_mul)], bit_idx);
2151                 }
2152                 // take CRC of second pictureData byte if bit depth is greater than 8-bits
2153                 if(bit_depth > 8)
2154                 {
2155                     for(bit_idx = 0; bit_idx < 8; bit_idx++)
2156                     {
2157                         CALC_CRC_BIT_LEVEL(
2158                             u4_crc_val,
2159                             pu_buf[y * (y_strd * gt8bit_mul) + (x * gt8bit_mul + 1)],
2160                             bit_idx);
2161                     }
2162                 }
2163             }
2164         }
2165 
2166         for(bit_idx = 0; bit_idx < 16; bit_idx++)
2167         {
2168             UWORD32 u4_crc_msb = (u4_crc_val >> 15) & 1;
2169             u4_crc_val = ((u4_crc_val << 1) & 0xffff) ^ (u4_crc_msb * 0x1021);
2170         }
2171 
2172         ps_hash_sei_params->au1_sei_hash[0][0] = (u4_crc_val >> 8) & 0xff;
2173         ps_hash_sei_params->au1_sei_hash[0][1] = u4_crc_val & 0xff;
2174     }
2175 
2176     /* Cb & Cr CRC */
2177     {
2178         UWORD32 u4_crc_val_u = 0xffff, u4_crc_val_v = 0xffff;
2179 
2180         pu_buf = (UWORD8 *)pv_u_buf;
2181 
2182         for(y = 0; y < uv_ht; y++)
2183         {
2184             for(x = 0; x < uv_wd; x += 2)
2185             {
2186                 // take CRC of first pictureData byte
2187                 for(bit_idx = 0; bit_idx < 8; bit_idx++)
2188                 {
2189                     CALC_CRC_BIT_LEVEL(
2190                         u4_crc_val_u,
2191                         pu_buf[y * (uv_strd * gt8bit_mul) + (x * gt8bit_mul)],
2192                         bit_idx);
2193                     CALC_CRC_BIT_LEVEL(
2194                         u4_crc_val_v,
2195                         pu_buf[y * (uv_strd * gt8bit_mul) + ((x + 1) * gt8bit_mul)],
2196                         bit_idx);
2197                 }
2198                 // take CRC of second pictureData byte if bit depth is greater than 8-bits
2199                 if(bit_depth > 8)
2200                 {
2201                     for(bit_idx = 0; bit_idx < 8; bit_idx++)
2202                     {
2203                         CALC_CRC_BIT_LEVEL(
2204                             u4_crc_val_u,
2205                             pu_buf[y * (uv_strd * gt8bit_mul) + (x * gt8bit_mul) + 1],
2206                             bit_idx);
2207                         CALC_CRC_BIT_LEVEL(
2208                             u4_crc_val_v,
2209                             pu_buf[y * (uv_strd * gt8bit_mul) + ((x + 1) * gt8bit_mul) + 1],
2210                             bit_idx);
2211                     }
2212                 }
2213             }
2214         }
2215 
2216         for(bit_idx = 0; bit_idx < 16; bit_idx++)
2217         {
2218             UWORD32 u4_crc_msb = (u4_crc_val_u >> 15) & 1;
2219             u4_crc_val_u = ((u4_crc_val_u << 1) & 0xffff) ^ (u4_crc_msb * 0x1021);
2220 
2221             u4_crc_msb = (u4_crc_val_v >> 15) & 1;
2222             u4_crc_val_v = ((u4_crc_val_v << 1) & 0xffff) ^ (u4_crc_msb * 0x1021);
2223         }
2224 
2225         ps_hash_sei_params->au1_sei_hash[1][0] = (u4_crc_val_u >> 8) & 0xff;
2226         ps_hash_sei_params->au1_sei_hash[1][1] = u4_crc_val_u & 0xff;
2227 
2228         ps_hash_sei_params->au1_sei_hash[2][0] = (u4_crc_val_v >> 8) & 0xff;
2229         ps_hash_sei_params->au1_sei_hash[2][1] = u4_crc_val_v & 0xff;
2230     }
2231 }
2232 
2233 /**
2234 ******************************************************************************
2235 *
2236 *  @brief Populates Hash SEI values for Checksum Hash
2237 *
2238 *  @par   Description
2239 *
2240 *  @param[out]  ps_hash_sei_params
2241 *  pointer to hash sei params that needs to be populated
2242 *
2243 *  @param[in]   bit_depth
2244 *  i4_internal_bit_depth value. Assume same for Luma & Chroma
2245 *
2246 *  @param[in]   pv_y_buf
2247 *  Pointer to decoded/recon Luma buffer
2248 *
2249 *  @param[in]   y_wd
2250 *  pic width in luma samples
2251 *
2252 *  @param[in]   y_ht
2253 *  pic height in luma samples
2254 *
2255 *  @param[in]   y_strd
2256 *  Stride of luma buffer
2257 *
2258 *  @param[in]   pv_u_buf
2259 *  Pointer to decoded/recon Chroma buffer
2260 *
2261 *  @param[in]   uv_wd
2262 *  pic width in luma samples / SubWidthC
2263 *
2264 *  @param[in]   uv_ht
2265 *  pic height in luma samples / SubHeightC
2266 *
2267 *  @param[in]   uv_strd
2268 *  Stride of chroma buffer
2269 *
2270 *  @return  None
2271 *
2272 *****************************************************************************
2273 */
ihevce_calc_checksum(hash_sei_param_t * ps_hash_sei_params,WORD32 bit_depth,void * pv_y_buf,WORD32 y_wd,WORD32 y_ht,WORD32 y_strd,void * pv_u_buf,WORD32 uv_wd,WORD32 uv_ht,WORD32 uv_strd,WORD32 i4_frame_pos_x,WORD32 i4_frame_pos_y)2274 static void ihevce_calc_checksum(
2275     hash_sei_param_t *ps_hash_sei_params,
2276     WORD32 bit_depth,
2277     void *pv_y_buf,
2278     WORD32 y_wd,
2279     WORD32 y_ht,
2280     WORD32 y_strd,
2281     void *pv_u_buf,
2282     WORD32 uv_wd,
2283     WORD32 uv_ht,
2284     WORD32 uv_strd,
2285     WORD32 i4_frame_pos_x,
2286     WORD32 i4_frame_pos_y)
2287 {
2288     WORD32 x, y;
2289     UWORD8 *pu_buf;
2290     WORD32 row, col;
2291     UWORD32 u4_xor_mask;
2292     UWORD32 gt8bit_mul = 1;
2293 
2294     if(bit_depth > 8)
2295         gt8bit_mul++;
2296 
2297     /* Luma Checksum */
2298     {
2299         UWORD32 u4_sum_luma = 0;
2300 
2301         pu_buf = (UWORD8 *)pv_y_buf;
2302 
2303         for(y = i4_frame_pos_y, row = 0; row < y_ht; y++, row++)
2304         {
2305             for(x = i4_frame_pos_x, col = 0; col < y_wd; x++, col++)
2306             {
2307                 // take checksum of first pictureData byte
2308                 u4_xor_mask = (x & 0xFF) ^ (y & 0xFF) ^ (x >> 8) ^ (y >> 8);
2309                 u4_sum_luma =
2310                     (u4_sum_luma + ((pu_buf[(row * y_strd + col) * gt8bit_mul]) ^ u4_xor_mask)) &
2311                     0xFFFFFFFF;
2312 
2313                 // take checksum of second pictureData byte if bit depth is greater than 8-bits
2314                 if(bit_depth > 8)
2315                 {
2316                     u4_sum_luma = (u4_sum_luma + ((pu_buf[(row * y_strd + col) * gt8bit_mul + 1]) ^
2317                                                   u4_xor_mask)) &
2318                                   0xFFFFFFFF;
2319                 }
2320             }
2321         }
2322 
2323         ps_hash_sei_params->au1_sei_hash[0][0] = (u4_sum_luma >> 24) & 0xff;
2324         ps_hash_sei_params->au1_sei_hash[0][1] = (u4_sum_luma >> 16) & 0xff;
2325         ps_hash_sei_params->au1_sei_hash[0][2] = (u4_sum_luma >> 8) & 0xff;
2326         ps_hash_sei_params->au1_sei_hash[0][3] = (u4_sum_luma)&0xff;
2327     }
2328 
2329     /* Cb & Cr checksum */
2330     {
2331         UWORD32 u4_sum_cb = 0, u4_sum_cr = 0;
2332         pu_buf = (UWORD8 *)pv_u_buf;
2333 
2334         for(y = (i4_frame_pos_y / 2), row = 0; row < uv_ht; y++, row++)
2335         {
2336             for(x = (i4_frame_pos_x / 2), col = 0; col < uv_wd; x++, col += 2)
2337             {
2338                 // take checksum of first pictureData byte
2339                 u4_xor_mask = (x & 0xFF) ^ (y & 0xFF) ^ (x >> 8) ^ (y >> 8);
2340                 u4_sum_cb =
2341                     (u4_sum_cb + ((pu_buf[(row * uv_strd + (col)) * gt8bit_mul]) ^ u4_xor_mask)) &
2342                     0xFFFFFFFF;
2343                 u4_sum_cr = (u4_sum_cr +
2344                              ((pu_buf[(row * uv_strd + (col + 1)) * gt8bit_mul]) ^ u4_xor_mask)) &
2345                             0xFFFFFFFF;
2346 
2347                 // take checksum of second pictureData byte if bit depth is greater than 8-bits
2348                 if(bit_depth > 8)
2349                 {
2350                     u4_sum_cb = (u4_sum_cb + ((pu_buf[(row * uv_strd + (col)) * gt8bit_mul + 1]) ^
2351                                               u4_xor_mask)) &
2352                                 0xFFFFFFFF;
2353                     u4_sum_cr =
2354                         (u4_sum_cr +
2355                          ((pu_buf[(row * uv_strd + (col + 1)) * gt8bit_mul + 1]) ^ u4_xor_mask)) &
2356                         0xFFFFFFFF;
2357                 }
2358             }
2359         }
2360 
2361         ps_hash_sei_params->au1_sei_hash[1][0] = (u4_sum_cb >> 24) & 0xff;
2362         ps_hash_sei_params->au1_sei_hash[1][1] = (u4_sum_cb >> 16) & 0xff;
2363         ps_hash_sei_params->au1_sei_hash[1][2] = (u4_sum_cb >> 8) & 0xff;
2364         ps_hash_sei_params->au1_sei_hash[1][3] = (u4_sum_cb)&0xff;
2365 
2366         ps_hash_sei_params->au1_sei_hash[2][0] = (u4_sum_cr >> 24) & 0xff;
2367         ps_hash_sei_params->au1_sei_hash[2][1] = (u4_sum_cr >> 16) & 0xff;
2368         ps_hash_sei_params->au1_sei_hash[2][2] = (u4_sum_cr >> 8) & 0xff;
2369         ps_hash_sei_params->au1_sei_hash[2][3] = (u4_sum_cr)&0xff;
2370     }
2371 }
2372 
2373 /**
2374 ******************************************************************************
2375 *
2376 *  @brief Populates Hash SEI values
2377 *
2378 *  @par   Description
2379 *
2380 *  @param[out]  ps_sei
2381 *  pointer to sei params that needs to be populated
2382 *
2383 *  @param[in]   bit_depth
2384 *  i4_internal_bit_depth value. Assume same for Luma & Chroma
2385 *
2386 *  @param[in]   pv_y_buf
2387 *  Pointer to decoded/recon Luma buffer
2388 *
2389 *  @param[in]   y_wd
2390 *  pic width in luma samples
2391 *
2392 *  @param[in]   y_ht
2393 *  pic height in luma samples
2394 *
2395 *  @param[in]   y_strd
2396 *  Stride of luma buffer
2397 *
2398 *  @param[in]   pv_u_buf
2399 *  Pointer to decoded/recon Chroma buffer
2400 *
2401 *  @param[in]   uv_wd
2402 *  pic width in luma samples / SubWidthC
2403 *
2404 *  @param[in]   uv_ht
2405 *  pic height in luma samples / SubHeightC
2406 *
2407 *  @param[in]   uv_strd
2408 *  Stride of chroma buffer
2409 *
2410 *  @return      success or failure error code
2411 *
2412 *****************************************************************************
2413 */
ihevce_populate_hash_sei(sei_params_t * ps_sei,WORD32 bit_depth,void * pv_y_buf,WORD32 y_wd,WORD32 y_ht,WORD32 y_strd,void * pv_u_buf,WORD32 uv_wd,WORD32 uv_ht,WORD32 uv_strd,WORD32 i4_frame_pos_x,WORD32 i4_frame_pos_y)2414 WORD32 ihevce_populate_hash_sei(
2415     sei_params_t *ps_sei,
2416     WORD32 bit_depth,
2417     void *pv_y_buf,
2418     WORD32 y_wd,
2419     WORD32 y_ht,
2420     WORD32 y_strd,
2421     void *pv_u_buf,
2422     WORD32 uv_wd,
2423     WORD32 uv_ht,
2424     WORD32 uv_strd,
2425     WORD32 i4_frame_pos_x,
2426     WORD32 i4_frame_pos_y)
2427 {
2428     hash_sei_param_t *ps_hash_sei_params = &ps_sei->s_hash_sei_params;
2429 
2430     if(1 == ps_sei->i1_decoded_pic_hash_sei_flag)
2431     {
2432         ASSERT(0);  // Not supported now!
2433     }
2434     else if(2 == ps_sei->i1_decoded_pic_hash_sei_flag)
2435     {
2436         /* calculate CRC for entire reconstructed picture */
2437         ihevce_calc_CRC(
2438             ps_hash_sei_params,
2439             bit_depth,
2440             pv_y_buf,
2441             y_wd,
2442             y_ht,
2443             y_strd,
2444             pv_u_buf,
2445             uv_wd,
2446             uv_ht,
2447             uv_strd);
2448     }
2449     else if(3 == ps_sei->i1_decoded_pic_hash_sei_flag)
2450     {
2451         /* calculate Checksum for entire reconstructed picture */
2452         ihevce_calc_checksum(
2453             ps_hash_sei_params,
2454             bit_depth,
2455             pv_y_buf,
2456             y_wd,
2457             y_ht,
2458             y_strd,
2459             pv_u_buf,
2460             uv_wd,
2461             uv_ht,
2462             uv_strd,
2463             i4_frame_pos_x,
2464             i4_frame_pos_y);
2465     }
2466     else
2467     {
2468         ASSERT(0);
2469     }
2470 
2471     return IHEVCE_SUCCESS;
2472 }
2473 
2474 /**
2475 ******************************************************************************
2476 *
2477 *  @brief Populates vui structure
2478 *
2479 *  @par   Description
2480 *  Populates vui structure for its use in header generation
2481 *
2482 *  @param[out]  ps_vui
2483 *  pointer to vui params that needs to be populated
2484 *
2485 *  @param[out]  ps_sps
2486 *  pointer to sps params referred by vui
2487 *
2488 *  @param[in]   ps_src_params
2489 *  pointer to source config params; resolution, frame rate etc
2490 *
2491 *  @param[out]  ps_vui_sei_prms
2492 *  pointer to sps params referred by application
2493 *
2494 *  @return      success or failure error code
2495 *
2496 ******************************************************************************
2497 */
ihevce_populate_vui(vui_t * ps_vui,sps_t * ps_sps,ihevce_src_params_t * ps_src_params,ihevce_vui_sei_params_t * ps_vui_sei_prms,WORD32 i4_resolution_id,ihevce_tgt_params_t * ps_tgt_params,ihevce_static_cfg_params_t * ps_stat_prms,WORD32 i4_bitrate_instance_id)2498 WORD32 ihevce_populate_vui(
2499     vui_t *ps_vui,
2500     sps_t *ps_sps,
2501     ihevce_src_params_t *ps_src_params,
2502     ihevce_vui_sei_params_t *ps_vui_sei_prms,
2503     WORD32 i4_resolution_id,
2504     ihevce_tgt_params_t *ps_tgt_params,
2505     ihevce_static_cfg_params_t *ps_stat_prms,
2506     WORD32 i4_bitrate_instance_id)
2507 {
2508     WORD32 i, j, i4_range_idr, i4_range_cdr;
2509     ULWORD64 max_vbv_size;
2510 
2511     ps_vui->u1_aspect_ratio_info_present_flag = ps_vui_sei_prms->u1_aspect_ratio_info_present_flag;
2512 
2513     ps_vui->u1_aspect_ratio_idc = ps_vui_sei_prms->au1_aspect_ratio_idc[i4_resolution_id];
2514 
2515     ps_vui->u2_sar_height = ps_vui_sei_prms->au2_sar_height[i4_resolution_id];
2516 
2517     ps_vui->u2_sar_width = ps_vui_sei_prms->au2_sar_width[i4_resolution_id];
2518 
2519     ps_vui->u1_overscan_info_present_flag = ps_vui_sei_prms->u1_overscan_info_present_flag;
2520 
2521     ps_vui->u1_overscan_appropriate_flag = ps_vui_sei_prms->u1_overscan_appropriate_flag;
2522 
2523     ps_vui->u1_video_signal_type_present_flag = ps_vui_sei_prms->u1_video_signal_type_present_flag;
2524 
2525     ps_vui->u1_video_format = ps_vui_sei_prms->u1_video_format;
2526 
2527     ps_vui->u1_video_full_range_flag = ps_vui_sei_prms->u1_video_full_range_flag;
2528 
2529     ps_vui->u1_colour_description_present_flag =
2530         ps_vui_sei_prms->u1_colour_description_present_flag;
2531 
2532     ps_vui->u1_colour_primaries = ps_vui_sei_prms->u1_colour_primaries;
2533 
2534     ps_vui->u1_transfer_characteristics = ps_vui_sei_prms->u1_transfer_characteristics;
2535 
2536     ps_vui->u1_matrix_coefficients = ps_vui_sei_prms->u1_matrix_coefficients;
2537 
2538     ps_vui->u1_chroma_loc_info_present_flag = ps_vui_sei_prms->u1_chroma_loc_info_present_flag;
2539 
2540     ps_vui->u1_chroma_sample_loc_type_top_field =
2541         ps_vui_sei_prms->u1_chroma_sample_loc_type_top_field;
2542 
2543     ps_vui->u1_chroma_sample_loc_type_bottom_field =
2544         ps_vui_sei_prms->u1_chroma_sample_loc_type_bottom_field;
2545 
2546     ps_vui->u1_neutral_chroma_indication_flag = 0;
2547 
2548     ps_vui->u1_default_display_window_flag = 0;
2549 
2550     /* Default Values for display offset added */
2551     if(ps_vui->u1_default_display_window_flag)
2552     {
2553         ps_vui->u4_def_disp_win_bottom_offset = 0;
2554 
2555         ps_vui->u4_def_disp_win_left_offset = 0;
2556 
2557         ps_vui->u4_def_disp_win_right_offset = 0;
2558 
2559         ps_vui->u4_def_disp_win_top_offset = 0;
2560     }
2561 
2562     ps_vui->u1_vui_hrd_parameters_present_flag =
2563         ps_vui_sei_prms->u1_vui_hrd_parameters_present_flag;
2564 
2565     ps_vui->u1_field_seq_flag = ps_src_params->i4_field_pic;
2566 
2567     ps_vui->u1_frame_field_info_present_flag = 1;
2568 
2569     ps_vui->u1_vui_timing_info_present_flag = ps_vui_sei_prms->u1_timing_info_present_flag;
2570 
2571     //if(ps_vui->u1_vui_timing_info_present_flag)
2572     {
2573         /* NumUnits in tick is same as the frame rate denominator assuming delta poc as 1 */
2574         ps_vui->u4_vui_num_units_in_tick = ps_src_params->i4_frm_rate_denom;
2575 
2576         /* TimeScale is the same as the frame rate numerator assuming delta poc as 1      */
2577         ps_vui->u4_vui_time_scale =
2578             (ps_src_params->i4_frm_rate_num / ps_tgt_params->i4_frm_rate_scale_factor);
2579     }
2580 
2581     ps_vui->u1_poc_proportional_to_timing_flag = 1;
2582 
2583     if(ps_vui->u1_poc_proportional_to_timing_flag && ps_vui->u1_vui_timing_info_present_flag)
2584         ps_vui->u4_num_ticks_poc_diff_one_minus1 = 0;
2585 
2586     //if (ps_vui->u1_vui_hrd_parameters_present_flag)
2587     {
2588         ps_vui->s_vui_hrd_parameters.u1_initial_cpb_removal_delay_length_minus1 = 23;
2589         ps_vui->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 = 23; /* Default value */
2590 
2591         ps_vui->s_vui_hrd_parameters.u1_dpb_output_delay_length_minus1 =
2592             4;  // max num of B pics are 7. So the max delay can go up to 5 and a maximun 10 is allowed for initial removal dalay.
2593 
2594         ps_vui->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag =
2595             ps_vui_sei_prms->u1_nal_hrd_parameters_present_flag;
2596 
2597         ps_vui->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag =
2598             0;  //ps_vui_sei_prms->u1_vcl_hrd_parameters_present_flag;
2599         ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag = 0;
2600 
2601         if(ps_vui->s_vui_hrd_parameters.u1_nal_hrd_parameters_present_flag ||
2602            ps_vui->s_vui_hrd_parameters.u1_vcl_hrd_parameters_present_flag)
2603         {
2604             /* Initialize  u1_au_cpb_removal_delay_length_minus1 based on configured intra periods */
2605             ps_vui->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 =
2606                 8; /* Default value when HRD params are enabled */
2607             if(ps_stat_prms->s_coding_tools_prms.i4_max_cra_open_gop_period ||
2608                ps_stat_prms->s_coding_tools_prms.i4_max_closed_gop_period)
2609             {
2610                 GETRANGE(
2611                     i4_range_cdr, ps_stat_prms->s_coding_tools_prms.i4_max_cra_open_gop_period);
2612 
2613                 GETRANGE(i4_range_idr, ps_stat_prms->s_coding_tools_prms.i4_max_closed_gop_period);
2614 
2615                 ps_vui->s_vui_hrd_parameters.u1_au_cpb_removal_delay_length_minus1 =
2616                     MAX(i4_range_cdr, i4_range_idr);
2617             }
2618             /*BLU_RAY Default set to 0 */
2619             ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag = 0;
2620             if(ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
2621             {
2622                 ps_vui->s_vui_hrd_parameters.u1_tick_divisor_minus2 = 1;
2623                 ps_vui->s_vui_hrd_parameters.u1_du_cpb_removal_delay_increment_length_minus1 = 23;
2624                 ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 1;
2625                 ps_vui->s_vui_hrd_parameters.u1_dpb_output_delay_du_length_minus1 = 0;
2626             }
2627         }
2628 
2629         ps_vui->s_vui_hrd_parameters.u4_bit_rate_scale = VUI_BIT_RATE_SCALE;
2630         ps_vui->s_vui_hrd_parameters.u4_cpb_size_scale = VUI_CPB_SIZE_SCALE;
2631         if(ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
2632         {
2633             ps_vui->s_vui_hrd_parameters.u4_cpb_size_du_scale = 0;
2634         }
2635 
2636         for(i = 0; i <= (ps_sps->i1_sps_max_sub_layers - 1); i++)
2637         {
2638             ps_vui->s_vui_hrd_parameters.au1_fixed_pic_rate_general_flag[i] =
2639                 1; /*BLU_RAY specific change already done */
2640             ps_vui->s_vui_hrd_parameters.au1_fixed_pic_rate_within_cvs_flag[i] = 1;
2641             ps_vui->s_vui_hrd_parameters.au2_elemental_duration_in_tc_minus1[i] = 0;
2642 
2643             /*BLU_RAY low_delay_hrd_flag is always set to 0*/
2644             ps_vui->s_vui_hrd_parameters.au1_low_delay_hrd_flag[i] = 0;
2645 
2646             /************************************************************************/
2647             /* cpb_cnt_minus1 is set to zero because we assume that the decoder     */
2648             /* can work with just one CPB specification                             */
2649             /************************************************************************/
2650             ps_vui->s_vui_hrd_parameters.au1_cpb_cnt_minus1[i] = 0;
2651 
2652             max_vbv_size = ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2653                                .ai4_max_vbv_buffer_size[i4_bitrate_instance_id];
2654             for(j = 0; j <= ps_vui->s_vui_hrd_parameters.au1_cpb_cnt_minus1[i]; j++)
2655             {
2656                 ULWORD64 u8_bit_rate_val =
2657                     (ULWORD64)ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2658                         .ai4_tgt_bitrate[i4_bitrate_instance_id];
2659                 ULWORD64 u8_max_cpb_size;
2660                 if((ps_stat_prms->s_config_prms.i4_rate_control_mode == 2) ||
2661                    (ps_stat_prms->s_config_prms.i4_rate_control_mode ==
2662                     1))  // VBR/Capped VBR rate control mode
2663                     u8_bit_rate_val =
2664                         (ULWORD64)(ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2665                                        .ai4_peak_bitrate[i4_bitrate_instance_id]);
2666                 u8_max_cpb_size =
2667                     max_vbv_size;  //((ULWORD64)(max_vbv_size * u8_bit_rate_val)/1000);
2668 
2669                 if(3 == ps_stat_prms->s_config_prms.i4_rate_control_mode)
2670                 {
2671                     /* For CQP mode, assume Level specified max rate and buffer size */
2672                     WORD32 codec_level_index = ihevce_get_level_index(
2673                         ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2674                     WORD32 codec_tier = ps_stat_prms->s_out_strm_prms.i4_codec_tier;
2675 
2676                     /* Bitrate as per level and tier limits */
2677                     u8_bit_rate_val =
2678                         g_as_level_data[codec_level_index].i4_max_bit_rate[codec_tier] *
2679                         CBP_VCL_FACTOR;
2680                     u8_max_cpb_size =
2681                         g_as_level_data[codec_level_index].i4_max_cpb[codec_tier] * CBP_VCL_FACTOR;
2682                 }
2683 
2684                 u8_bit_rate_val >>= (6 + ps_vui->s_vui_hrd_parameters.u4_bit_rate_scale);
2685                 /************************************************************************/
2686                 /* u8_bit_rate_value forced >= 4096 as the shift is always of 12        */
2687                 /* because u4_bit_rate_scale = 6.                                       */
2688                 /************************************************************************/
2689                 if (u8_bit_rate_val == 0)
2690                 {
2691                     return IHEVCE_BITRATE_NOT_SUPPORTED;
2692                 }
2693 
2694                 u8_max_cpb_size >>= (4 + ps_vui->s_vui_hrd_parameters.u4_cpb_size_scale);
2695 
2696                 ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[i]
2697                     .au4_bit_rate_value_minus1[j] = (UWORD32)(u8_bit_rate_val - 1);
2698                 ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[i]
2699                     .au4_cpb_size_value_minus1[j] = (UWORD32)(u8_max_cpb_size - 1);
2700 
2701                 if(ps_vui->s_vui_hrd_parameters.u1_sub_pic_cpb_params_present_flag)
2702                 {
2703                     ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[i]
2704                         .au4_cpb_size_value_minus1[j] = 0;
2705                 }
2706 
2707                 /************************************************************************/
2708                 /* CBR flag is set as per the RATE_CONTROL macro                        */
2709                 /************************************************************************/
2710 
2711                 /* Default cbr falg setting. will discard Decoder buffer overflows ( No stuffing required)*/
2712 
2713                 ps_vui->s_vui_hrd_parameters.as_sub_layer_hrd_params[i].au1_cbr_flag[j] = 0;
2714             }
2715         }
2716     }
2717 
2718     ps_vui->u1_bitstream_restriction_flag = 0;
2719 
2720     if(ps_vui->u1_bitstream_restriction_flag)
2721     {
2722         ps_vui->u1_tiles_fixed_structure_flag = 1;
2723 
2724         ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
2725 
2726         ps_vui->u4_min_spatial_segmentation_idc = 0;
2727 
2728         ps_vui->u1_restricted_ref_pic_lists_flag = 0;
2729 
2730         ps_vui->u1_max_bytes_per_pic_denom = 2;
2731 
2732         ps_vui->u1_max_bits_per_mincu_denom = 1;
2733 
2734         ps_vui->u1_log2_max_mv_length_horizontal = 15;
2735 
2736         ps_vui->u1_log2_max_mv_length_vertical = 15;
2737     }
2738 
2739     return IHEVCE_SUCCESS;
2740 }
2741