• 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.c
24 *
25 * @brief
26 *   This file contains function definitions related to header encoding
27 *
28 * @author
29 *   Ittiam
30 *
31 * List of Functions
32 *   ihevce_generate_nal_unit_header
33 *   ihevce_generate_when_profile_present
34 *   ihevce_generate_profile_tier_level
35 *   ihevce_short_term_ref_pic_set
36 *   ihevce_generate_bit_rate_pic_rate_info
37 *   ihevce_generate_aud
38 *   ihevce_generate_eos
39 *   ihevce_generate_vps
40 *   ihevce_generate_sps
41 *   ihevce_generate_pps
42 *   ihevce_generate_slice_header
43 *   ihevce_populate_vps
44 *   ihevce_populate_sps
45 *   ihevce_populate_pps
46 *   ihevce_populate_slice_header
47 *   ihevce_insert_entry_offset_slice_header
48 *
49 ******************************************************************************
50 */
51 
52 /*****************************************************************************/
53 /* File Includes                                                             */
54 /*****************************************************************************/
55 /* System include files */
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <assert.h>
60 #include <stdarg.h>
61 #include <math.h>
62 
63 /* User include files */
64 #include "ihevc_typedefs.h"
65 #include "itt_video_api.h"
66 #include "ihevce_api.h"
67 
68 #include "rc_cntrl_param.h"
69 #include "rc_frame_info_collector.h"
70 #include "rc_look_ahead_params.h"
71 
72 #include "ihevc_defs.h"
73 #include "ihevc_macros.h"
74 #include "ihevc_debug.h"
75 #include "ihevc_structs.h"
76 #include "ihevc_platform_macros.h"
77 #include "ihevc_deblk.h"
78 #include "ihevc_itrans_recon.h"
79 #include "ihevc_chroma_itrans_recon.h"
80 #include "ihevc_chroma_intra_pred.h"
81 #include "ihevc_intra_pred.h"
82 #include "ihevc_inter_pred.h"
83 #include "ihevc_mem_fns.h"
84 #include "ihevc_padding.h"
85 #include "ihevc_weighted_pred.h"
86 #include "ihevc_sao.h"
87 #include "ihevc_resi_trans.h"
88 #include "ihevc_quant_iquant_ssd.h"
89 #include "ihevc_cabac_tables.h"
90 #include "ihevc_trans_tables.h"
91 #include "ihevc_trans_macros.h"
92 
93 #include "ihevce_defs.h"
94 #include "ihevce_lap_enc_structs.h"
95 #include "ihevce_multi_thrd_structs.h"
96 #include "ihevce_multi_thrd_funcs.h"
97 #include "ihevce_me_common_defs.h"
98 #include "ihevce_had_satd.h"
99 #include "ihevce_error_codes.h"
100 #include "ihevce_error_checks.h"
101 #include "ihevce_bitstream.h"
102 #include "ihevce_cabac.h"
103 #include "ihevce_rdoq_macros.h"
104 #include "ihevce_function_selector.h"
105 #include "ihevce_enc_structs.h"
106 #include "ihevce_global_tables.h"
107 #include "ihevce_encode_header.h"
108 #include "ihevce_encode_header_sei_vui.h"
109 #include "ihevce_trace.h"
110 
111 /*****************************************************************************/
112 /* Constant Macros                                                           */
113 /*****************************************************************************/
114 #define CU_LEVEL_QP_LIMIT_8x8 3
115 #define CU_LEVEL_QP_LIMIT_16x16 2
116 #define CU_LEVEL_QP_LIMIT_32x32 1
117 
118 /*****************************************************************************/
119 /* Function Definitions                                                      */
120 /*****************************************************************************/
121 
122 /**
123 ******************************************************************************
124 *
125 *  @brief Generate nal unit header in the stream as per section 7.3.1.2
126 *
127 *  @par   Description
128 *  Inserts the nal type and temporal id plus 1 as per section 7.3.1.2 Nal unit
129 *  header syntax
130 *
131 *  @param[inout]   ps_bitstrm
132 *  pointer to bitstream context (handle)
133 *
134 *  @param[in]   nal_unit_type
135 *  nal type to be inserted
136 *
137 *  @param[in]   temporal id
138 *  temporal id to be inserted
139 *
140 *  @return      success or failure error code
141 *
142 ******************************************************************************
143 */
ihevce_generate_nal_unit_header(bitstrm_t * ps_bitstrm,WORD32 nal_unit_type,WORD32 nuh_temporal_id)144 WORD32 ihevce_generate_nal_unit_header(
145     bitstrm_t *ps_bitstrm, WORD32 nal_unit_type, WORD32 nuh_temporal_id)
146 {
147     WORD32 return_status = IHEVCE_SUCCESS;
148 
149     /* sanity checks */
150     ASSERT((nal_unit_type >= 0) && (nal_unit_type < 64));
151     ASSERT((nuh_temporal_id >= 0) && (nuh_temporal_id < 7));
152 
153     /* forbidden_zero_bit + nal_unit_type */
154     PUT_BITS(
155         ps_bitstrm,
156         nal_unit_type,
157         (1 + 6), /*extra 1 is for forbidden zero bit */
158         return_status);
159 
160     /* nuh_reserved_zero_6bits */
161     PUT_BITS(ps_bitstrm, 0, 6, return_status);
162 
163     /* nuh_temporal_id_plus1 */
164     PUT_BITS(ps_bitstrm, (nuh_temporal_id + 1), 3, return_status);
165 
166     return (return_status);
167 }
168 
169 /**
170 ******************************************************************************
171 *
172 *  @brief Generates fields related to Profile, Tier and Level data.
173 *
174 *  @par   Description
175 *  Generates fields related to Profile, Tier and Level data.
176 *  Called when profile_present flag is 1
177 *
178 *  @param[in]   ps_bitstrm
179 *  pointer to bitstream context (handle)
180 *
181 *  @param[in]   ps_ptl
182 *  pointer to structure containing Profile, Tier and Level data data
183 *
184 *  @return      success or failure error code
185 *
186 ******************************************************************************
187 */
188 static WORD32
ihevce_generate_when_profile_present(bitstrm_t * ps_bitstrm,profile_tier_lvl_t * ps_ptl)189     ihevce_generate_when_profile_present(bitstrm_t *ps_bitstrm, profile_tier_lvl_t *ps_ptl)
190 {
191     WORD32 return_status = IHEVCE_SUCCESS;
192     WORD32 i;
193 
194     /* XXX_profile_space[] */
195     PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_space, 2, return_status);
196     ENTROPY_TRACE("XXX_profile_space[]", ps_ptl->i1_profile_space);
197 
198     /* XXX_tier_flag[] */
199     PUT_BITS(ps_bitstrm, ps_ptl->i1_tier_flag, 1, return_status);
200     ENTROPY_TRACE("XXX_tier_flag[]", ps_ptl->i1_tier_flag);
201 
202     /* XXX_profile_idc[] */
203     PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_idc, 5, return_status);
204     ENTROPY_TRACE("XXX_profile_idc[]", ps_ptl->i1_profile_idc);
205 
206     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
207     {
208         /* XXX_profile_compatibility_flag[][j] */
209         PUT_BITS(ps_bitstrm, ps_ptl->ai1_profile_compatibility_flag[i], 1, return_status);
210         ENTROPY_TRACE(
211             "XXX_profile_compatibility_flag[][j]", ps_ptl->ai1_profile_compatibility_flag[i]);
212     }
213 
214     /* XXX_progressive_source_flag[] */
215     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_progressive_source_flag, 1, return_status);
216     ENTROPY_TRACE("XXX_progressive_source_flag[]", ps_ptl->i1_general_progressive_source_flag);
217 
218     /* XXX_interlaced_source_flag[] */
219     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_interlaced_source_flag, 1, return_status);
220     ENTROPY_TRACE("XXX_interlaced_source_flag[]", ps_ptl->i1_general_interlaced_source_flag);
221 
222     /* XXX_non_packed_constraint_flag[] */
223     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_non_packed_constraint_flag, 1, return_status);
224     ENTROPY_TRACE(
225         "XXX_non_packed_constraint_flag[]", ps_ptl->i1_general_non_packed_constraint_flag);
226 
227     /* XXX_frame_only_constraint_flag[] */
228     PUT_BITS(ps_bitstrm, ps_ptl->i1_frame_only_constraint_flag, 1, return_status);
229     ENTROPY_TRACE("XXX_frame_only_constraint_flag[]", ps_ptl->i1_frame_only_constraint_flag);
230 
231     /* XXX_general_max_12bit_constraint_flag[] */
232     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_12bit_constraint_flag, 1, return_status);
233     ENTROPY_TRACE(
234         "XXX_general_max_12bit_constraint_flag[]", ps_ptl->i1_general_max_12bit_constraint_flag);
235 
236     /* XXX_general_max_10bit_constraint_flag[] */
237     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_10bit_constraint_flag, 1, return_status);
238     ENTROPY_TRACE(
239         "XXX_general_max_10bit_constraint_flag[]", ps_ptl->i1_general_max_10bit_constraint_flag);
240 
241     /* XXX_general_max_8bit_constraint_flag[] */
242     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_8bit_constraint_flag, 1, return_status);
243     ENTROPY_TRACE(
244         "XXX_general_max_8bit_constraint_flag[]", ps_ptl->i1_general_max_8bit_constraint_flag);
245 
246     /* XXX_general_max_422chroma_constraint_flag[] */
247     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_422chroma_constraint_flag, 1, return_status);
248     ENTROPY_TRACE(
249         "XXX_general_max_422chroma_constraint_flag[]",
250         ps_ptl->i1_general_max_422chroma_constraint_flag);
251 
252     /* XXX_general_max_420chroma_constraint_flag[] */
253     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_420chroma_constraint_flag, 1, return_status);
254     ENTROPY_TRACE(
255         "XXX_general_max_420chroma_constraint_flag[]",
256         ps_ptl->i1_general_max_420chroma_constraint_flag);
257 
258     /* XXX_general_max_monochrome_constraint_flag[] */
259     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_monochrome_constraint_flag, 1, return_status);
260     ENTROPY_TRACE(
261         "XXX_general_max_monochrome_constraint_flag[]",
262         ps_ptl->i1_general_max_monochrome_constraint_flag);
263 
264     /* XXX_general_intra_constraint_flag[] */
265     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_intra_constraint_flag, 1, return_status);
266     ENTROPY_TRACE("XXX_general_intra_constraint_flag[]", ps_ptl->i1_general_intra_constraint_flag);
267 
268     /* XXX_general_one_picture_only_constraint_flag[] */
269     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_one_picture_only_constraint_flag, 1, return_status);
270     ENTROPY_TRACE(
271         "XXX_general_one_picture_only_constraint_flag[]",
272         ps_ptl->i1_general_one_picture_only_constraint_flag);
273 
274     /* XXX_general_lower_bit_rate_constraint_flag[] */
275     PUT_BITS(ps_bitstrm, ps_ptl->i1_general_lower_bit_rate_constraint_flag, 1, return_status);
276     ENTROPY_TRACE(
277         "XXX_general_lower_bit_rate_constraint_flag[]",
278         ps_ptl->i1_general_lower_bit_rate_constraint_flag);
279 
280     /* XXX_reserved_zero_35bits[] */
281     PUT_BITS(ps_bitstrm, 0, 16, return_status);
282     PUT_BITS(ps_bitstrm, 0, 16, return_status);
283     PUT_BITS(ps_bitstrm, 0, 3, return_status);
284     ENTROPY_TRACE("XXX_reserved_zero_35bits[]", 0);
285 
286     return return_status;
287 }
288 
289 /**
290 ******************************************************************************
291 *
292 *  @brief Generates Profile, Tier and Level data
293 *
294 *  @par   Description
295 *  Generates Profile, Tier and Level data as per Section 7.3.3
296 *
297 *  @param[in]   ps_bitstrm
298 *  pointer to bitstream context (handle)
299 *
300 *  @param[in]   ps_ptl
301 *  pointer to structure containing Profile, Tier and Level data data
302 *
303 *  @param[in]   i1_profile_present_flag
304 *  flag that indicates whether profile-related data is present
305 *
306 *  @param[in]   i1_vps_max_sub_layers_minus1
307 *  (Maximum number of sub_layers present) minus 1
308 *
309 *  @return      success or failure error code
310 *
311 ******************************************************************************
312 */
ihevce_generate_profile_tier_level(bitstrm_t * ps_bitstrm,profile_tier_lvl_info_t * ps_ptl,WORD8 i1_profile_present_flag,WORD8 i1_max_sub_layers_minus1)313 static WORD32 ihevce_generate_profile_tier_level(
314     bitstrm_t *ps_bitstrm,
315     profile_tier_lvl_info_t *ps_ptl,
316     WORD8 i1_profile_present_flag,
317     WORD8 i1_max_sub_layers_minus1)
318 {
319     WORD32 i;
320     WORD32 return_status = IHEVCE_SUCCESS;
321 
322     if(i1_profile_present_flag)
323     {
324         ihevce_generate_when_profile_present(ps_bitstrm, &ps_ptl->s_ptl_gen);
325     }
326 
327     /* general_level_idc */
328     PUT_BITS(ps_bitstrm, ps_ptl->s_ptl_gen.u1_level_idc, 8, return_status);
329     ENTROPY_TRACE("general_level_idc", ps_ptl->s_ptl_gen.u1_level_idc);
330 
331     for(i = 0; i < i1_max_sub_layers_minus1; i++)
332     {
333         /* sub_layer_profile_present_flag[i] */
334         PUT_BITS(ps_bitstrm, ps_ptl->ai1_sub_layer_profile_present_flag[i], 1, return_status);
335         ENTROPY_TRACE(
336             "sub_layer_profile_present_flag[i]", ps_ptl->ai1_sub_layer_profile_present_flag[i]);
337 
338         /* sub_layer_level_present_flag[i] */
339         PUT_BITS(ps_bitstrm, ps_ptl->ai1_sub_layer_level_present_flag[i], 1, return_status);
340         ENTROPY_TRACE(
341             "sub_layer_level_present_flag[i]", ps_ptl->ai1_sub_layer_level_present_flag[i]);
342     }
343 
344     if(i1_max_sub_layers_minus1 > 0)
345     {
346         for(i = i1_max_sub_layers_minus1; i < 8; i++)
347         {
348             /* reserved_zero_2bits[i] */
349             PUT_BITS(ps_bitstrm, 0, 2, return_status);
350             ENTROPY_TRACE("reserved_zero_2bits[i]", 0);
351         }
352     }
353 
354     for(i = 0; i < i1_max_sub_layers_minus1; i++)
355     {
356         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
357         {
358             ihevce_generate_when_profile_present(ps_bitstrm, &ps_ptl->as_ptl_sub[i]);
359         }
360 
361         if(ps_ptl->ai1_sub_layer_level_present_flag[i])  //TEMPORALA_SCALABILITY CHANGES BUG_FIX
362         {
363             /* sub_layer_level_idc[i] */
364             PUT_BITS(ps_bitstrm, ps_ptl->as_ptl_sub[i].u1_level_idc, 8, return_status);
365             ENTROPY_TRACE("sub_layer_level_idc[i]", ps_ptl->as_ptl_sub[i].u1_level_idc);
366         }
367     }
368 
369     return return_status;
370 }
371 
372 /**
373 *******************************************************************************
374 *
375 * @brief
376 *  Generates short term reference picture set
377 *
378 * @par   Description
379 *  Generates short term reference picture set as per section 7.3.5.2.
380 *  Can be called by either SPS or Slice header parsing modules.
381 *
382 * @param[in] ps_bitstrm
383 *  Pointer to bitstream structure
384 *
385 * @param[out] ps_stref_picset_base
386 *  Pointer to first short term ref pic set structure
387 *
388 * @param[in] num_short_term_ref_pic_sets
389 *  Number of short term reference pic sets
390 *
391 * @param[in] idx
392 *  Current short term ref pic set id
393 *
394 * @returns Error code from WORD32
395 *
396 *
397 *******************************************************************************
398 */
ihevce_short_term_ref_pic_set(bitstrm_t * ps_bitstrm,stref_picset_t * ps_stref_picset_base,WORD32 num_short_term_ref_pic_sets,WORD32 idx,WORD32 * pi4_NumPocTotalCurr)399 static WORD32 ihevce_short_term_ref_pic_set(
400     bitstrm_t *ps_bitstrm,
401     stref_picset_t *ps_stref_picset_base,
402     WORD32 num_short_term_ref_pic_sets,
403     WORD32 idx,
404     WORD32 *pi4_NumPocTotalCurr)
405 {
406     WORD32 i;
407     WORD32 return_status = IHEVCE_SUCCESS;
408     stref_picset_t *ps_stref_picset = ps_stref_picset_base + idx;
409 
410     (void)num_short_term_ref_pic_sets;
411     if(idx > 0)
412     {
413         /* inter_ref_pic_set_prediction_flag */
414         PUT_BITS(
415             ps_bitstrm, ps_stref_picset->i1_inter_ref_pic_set_prediction_flag, 1, return_status);
416         ENTROPY_TRACE(
417             "inter_ref_pic_set_prediction_flag",
418             ps_stref_picset->i1_inter_ref_pic_set_prediction_flag);
419     }
420 
421     /* This flag is assumed to be 0 for now */
422     ASSERT(0 == ps_stref_picset->i1_inter_ref_pic_set_prediction_flag);
423 
424     /* num_negative_pics */
425     PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_neg_pics, return_status);
426     ENTROPY_TRACE("num_negative_pics", ps_stref_picset->i1_num_neg_pics);
427 
428     /* num_positive_pics */
429     PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_pos_pics, return_status);
430     ENTROPY_TRACE("num_positive_pics", ps_stref_picset->i1_num_pos_pics);
431 
432     for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
433     {
434         /* delta_poc_s0_minus1 */
435         PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
436         ENTROPY_TRACE("delta_poc_s0_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
437 
438         /* used_by_curr_pic_s0_flag */
439         PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
440         ENTROPY_TRACE("used_by_curr_pic_s0_flag", ps_stref_picset->ai1_used[i]);
441         /*get the num pocs used for cur pic*/
442         if(ps_stref_picset->ai1_used[i])
443         {
444             *pi4_NumPocTotalCurr += 1;
445         }
446     }
447 
448     for(; i < (ps_stref_picset->i1_num_pos_pics + ps_stref_picset->i1_num_neg_pics); i++)
449     {
450         /* delta_poc_s1_minus1 */
451         PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
452         ENTROPY_TRACE("delta_poc_s1_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
453 
454         /* used_by_curr_pic_s1_flag */
455         PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
456         ENTROPY_TRACE("used_by_curr_pic_s1_flag", ps_stref_picset->ai1_used[i]);
457         /*get the num pocs used for cur pic*/
458         if(ps_stref_picset->ai1_used[i])
459         {
460             *pi4_NumPocTotalCurr += 1;
461         }
462     }
463 
464     return return_status;
465 }
466 
467 /**
468 ******************************************************************************
469 *
470 *  @brief Generates ref pic list modification
471 *
472 *  @par   Description
473 *  Generate ref pic list modification syntax as per Section 7.3.6.2
474 *
475 *  @param[in]   ps_bitstrm
476 *  pointer to bitstream context (handle)
477 *
478 *  @param[in]   ps_slice_hdr
479 *  pointer to structure containing slice header
480 *
481 *  @return      success or failure error code
482 *
483 ******************************************************************************
484 */
ref_pic_list_modification(bitstrm_t * ps_bitstrm,slice_header_t * ps_slice_hdr,WORD32 i4_NumPocTotalCurr)485 static WORD32 ref_pic_list_modification(
486     bitstrm_t *ps_bitstrm, slice_header_t *ps_slice_hdr, WORD32 i4_NumPocTotalCurr)
487 {
488     WORD32 return_status = IHEVCE_SUCCESS;
489     WORD32 i;
490 
491     /* ref_pic_list_modification_flag_l0 */
492     PUT_BITS(
493         ps_bitstrm, ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0, 1, return_status);
494     ENTROPY_TRACE(
495         "ref_pic_list_modification_flag_l0",
496         ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0);
497 
498     if(ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0)
499     {
500         for(i = 0; i <= (ps_slice_hdr->i1_num_ref_idx_l0_active - 1); i++)
501         {
502             WORD32 num_bits = 32 - CLZ(i4_NumPocTotalCurr - 1);
503 
504             /* list_entry_l0[ i ] */
505             PUT_BITS(ps_bitstrm, ps_slice_hdr->s_rplm.i1_list_entry_l0[i], num_bits, return_status);
506             ENTROPY_TRACE("list_entry_l0", ps_slice_hdr->s_rplm.i1_list_entry_l0[i]);
507         }
508     }
509 
510     if((BSLICE == ps_slice_hdr->i1_slice_type))
511     {
512         /* ref_pic_list_modification_flag_l1 */
513         PUT_BITS(
514             ps_bitstrm, ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1, 1, return_status);
515         ENTROPY_TRACE(
516             "ref_pic_list_modification_flag_l1",
517             ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1);
518 
519         if(ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1)
520         {
521             for(i = 0; i <= (ps_slice_hdr->i1_num_ref_idx_l1_active - 1); i++)
522             {
523                 WORD32 num_bits = 32 - CLZ(i4_NumPocTotalCurr - 1);
524 
525                 /* list_entry_l1[ i ] */
526                 PUT_BITS(
527                     ps_bitstrm, ps_slice_hdr->s_rplm.i1_list_entry_l1[i], num_bits, return_status);
528                 ENTROPY_TRACE("list_entry_l1", ps_slice_hdr->s_rplm.i1_list_entry_l1[i]);
529             }
530         }
531     } /*end of B slice check*/
532 
533     return return_status;
534 }
535 
536 /**
537 ******************************************************************************
538 *
539 *  @brief Generates Pred Weight Table
540 *
541 *  @par   Description
542 *  Generate Pred Weight Table as per Section 7.3.5.4
543 *
544 *  @param[in]   ps_bitstrm
545 *  pointer to bitstream context (handle)
546 *
547 *  @param[in]   ps_sps
548 *  pointer to structure containing SPS data
549 *
550 *  @param[in]   ps_pps
551 *  pointer to structure containing PPS data
552 *
553 *  @param[in]   ps_slice_hdr
554 *  pointer to structure containing slice header
555 *
556 *  @return      success or failure error code
557 *
558 ******************************************************************************
559 */
ihevce_generate_pred_weight_table(bitstrm_t * ps_bitstrm,sps_t * ps_sps,pps_t * ps_pps,slice_header_t * ps_slice_hdr)560 static WORD32 ihevce_generate_pred_weight_table(
561     bitstrm_t *ps_bitstrm, sps_t *ps_sps, pps_t *ps_pps, slice_header_t *ps_slice_hdr)
562 {
563     WORD32 i;
564     WORD32 delta_luma_weight;
565     WORD32 delta_chroma_weight;
566     WORD32 return_status = IHEVCE_SUCCESS;
567     pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
568     UWORD32 u4_luma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom;
569     WORD32 chroma_log2_weight_denom = (ps_wt_ofst->i1_chroma_log2_weight_denom);
570     WORD32 i4_wght_count = 0;
571 
572     (void)ps_pps;
573     /* luma_log2_weight_denom */
574     PUT_BITS_UEV(ps_bitstrm, u4_luma_log2_weight_denom, return_status);
575     ENTROPY_TRACE("luma_log2_weight_denom", u4_luma_log2_weight_denom);
576 
577     if(ps_sps->i1_chroma_format_idc != 0)
578     {
579         /* delta_chroma_log2_weight_denom */
580         PUT_BITS_SEV(
581             ps_bitstrm, chroma_log2_weight_denom - u4_luma_log2_weight_denom, return_status);
582         ENTROPY_TRACE(
583             "delta_chroma_log2_weight_denom", chroma_log2_weight_denom - u4_luma_log2_weight_denom);
584     }
585 
586     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
587     {
588         /* luma_weight_l0_flag[ i ] */
589         PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_luma_weight_l0_flag[i], 1, return_status);
590         i4_wght_count += ps_wt_ofst->i1_luma_weight_l0_flag[i];
591         assert(i4_wght_count <= 24);
592         ENTROPY_TRACE("luma_weight_l0_flag[ i ]", ps_wt_ofst->i1_luma_weight_l0_flag[i]);
593     }
594 
595     if(ps_sps->i1_chroma_format_idc != 0)
596     {
597         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
598         {
599             /* chroma_weight_l0_flag[ i ] */
600             PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_chroma_weight_l0_flag[i], 1, return_status);
601             i4_wght_count += 2 * ps_wt_ofst->i1_chroma_weight_l0_flag[i];
602             assert(i4_wght_count <= 24);
603             ENTROPY_TRACE("chroma_weight_l0_flag[ i ]", ps_wt_ofst->i1_chroma_weight_l0_flag[i]);
604         }
605     }
606 
607     delta_luma_weight = (1 << u4_luma_log2_weight_denom);
608     delta_chroma_weight = (1 << chroma_log2_weight_denom);
609 
610     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
611     {
612         if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
613         {
614             /* delta_luma_weight_l0[ i ] */
615             PUT_BITS_SEV(
616                 ps_bitstrm, ps_wt_ofst->i2_luma_weight_l0[i] - delta_luma_weight, return_status);
617             ENTROPY_TRACE(
618                 "delta_luma_weight_l0[ i ]", ps_wt_ofst->i2_luma_weight_l0[i] - delta_luma_weight);
619 
620             /* luma_offset_l0[ i ] */
621             PUT_BITS_SEV(ps_bitstrm, ps_wt_ofst->i2_luma_offset_l0[i], return_status);
622             ENTROPY_TRACE("luma_offset_l0[ i ]", ps_wt_ofst->i2_luma_offset_l0[i]);
623         }
624 
625         if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
626         {
627             WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
628             WORD32 delta_chroma_weight_l0[2];
629             WORD32 delta_chroma_offset_l0[2];
630 
631             delta_chroma_weight_l0[0] = ps_wt_ofst->i2_chroma_weight_l0_cb[i] - delta_chroma_weight;
632             delta_chroma_weight_l0[1] = ps_wt_ofst->i2_chroma_weight_l0_cr[i] - delta_chroma_weight;
633 
634             delta_chroma_offset_l0[0] =
635                 ps_wt_ofst->i2_chroma_offset_l0_cb[i] +
636                 ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> chroma_log2_weight_denom) -
637                 shift;
638             delta_chroma_offset_l0[1] =
639                 ps_wt_ofst->i2_chroma_offset_l0_cr[i] +
640                 ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> chroma_log2_weight_denom) -
641                 shift;
642 
643             /* delta_chroma_weight_l0[ i ][j] */
644             PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l0[0], return_status);
645             ENTROPY_TRACE("delta_chroma_weight_l0[ i ]", delta_chroma_weight_l0[0]);
646 
647             /* delta_chroma_offset_l0[ i ][j] */
648             PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l0[0], return_status);
649             ENTROPY_TRACE("delta_chroma_offset_l0[ i ]", delta_chroma_offset_l0[0]);
650 
651             /* delta_chroma_weight_l0[ i ][j] */
652             PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l0[1], return_status);
653             ENTROPY_TRACE("delta_chroma_weight_l0[ i ]", delta_chroma_weight_l0[1]);
654 
655             /* delta_chroma_offset_l0[ i ][j] */
656             PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l0[1], return_status);
657             ENTROPY_TRACE("delta_chroma_offset_l0[ i ]", delta_chroma_offset_l0[1]);
658         }
659     }
660 
661     if(BSLICE == ps_slice_hdr->i1_slice_type)
662     {
663         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
664         {
665             /* luma_weight_l1_flag[ i ] */
666             PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_luma_weight_l1_flag[i], 1, return_status);
667             i4_wght_count += ps_wt_ofst->i1_luma_weight_l1_flag[i];
668             assert(i4_wght_count <= 24);
669             ENTROPY_TRACE("luma_weight_l1_flag[ i ]", ps_wt_ofst->i1_luma_weight_l1_flag[i]);
670         }
671 
672         if(ps_sps->i1_chroma_format_idc != 0)
673         {
674             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
675             {
676                 /* chroma_weight_l1_flag[ i ] */
677                 PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_chroma_weight_l1_flag[i], 1, return_status);
678                 i4_wght_count += ps_wt_ofst->i1_chroma_weight_l1_flag[i];
679                 assert(i4_wght_count <= 24);
680                 ENTROPY_TRACE(
681                     "chroma_weight_l1_flag[ i ]", ps_wt_ofst->i1_chroma_weight_l1_flag[i]);
682             }
683         }
684 
685         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
686         {
687             if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
688             {
689                 /* delta_luma_weight_l1[ i ] */
690                 PUT_BITS_SEV(
691                     ps_bitstrm,
692                     ps_wt_ofst->i2_luma_weight_l1[i] - delta_luma_weight,
693                     return_status);
694                 ENTROPY_TRACE(
695                     "delta_luma_weight_l1[ i ]",
696                     ps_wt_ofst->i2_luma_weight_l1[i] - delta_luma_weight);
697 
698                 /* luma_offset_l1[ i ] */
699                 PUT_BITS_SEV(ps_bitstrm, ps_wt_ofst->i2_luma_offset_l1[i], return_status);
700                 ENTROPY_TRACE("luma_offset_l1[ i ]", ps_wt_ofst->i2_luma_offset_l1[i]);
701             }
702 
703             if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
704             {
705                 WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
706                 WORD32 delta_chroma_weight_l1[2];
707                 WORD32 delta_chroma_offset_l1[2];
708 
709                 delta_chroma_weight_l1[0] =
710                     ps_wt_ofst->i2_chroma_weight_l1_cb[i] - delta_chroma_weight;
711                 delta_chroma_weight_l1[1] =
712                     ps_wt_ofst->i2_chroma_weight_l1_cr[i] - delta_chroma_weight;
713 
714                 delta_chroma_offset_l1[0] =
715                     ps_wt_ofst->i2_chroma_offset_l1_cb[i] +
716                     ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> chroma_log2_weight_denom) -
717                     shift;
718                 delta_chroma_offset_l1[1] =
719                     ps_wt_ofst->i2_chroma_offset_l1_cr[i] +
720                     ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> chroma_log2_weight_denom) -
721                     shift;
722 
723                 /* delta_chroma_weight_l1[ i ][j] */
724                 PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l1[0], return_status);
725                 ENTROPY_TRACE("delta_chroma_weight_l1[ i ]", delta_chroma_weight_l1[0]);
726 
727                 /* delta_chroma_offset_l1[ i ][j] */
728                 PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l1[0], return_status);
729                 ENTROPY_TRACE("delta_chroma_offset_l1[ i ]", delta_chroma_offset_l1[0]);
730 
731                 /* delta_chroma_weight_l1[ i ][j] */
732                 PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l1[1], return_status);
733                 ENTROPY_TRACE("delta_chroma_weight_l1[ i ]", delta_chroma_weight_l1[1]);
734 
735                 /* delta_chroma_offset_l1[ i ][j] */
736                 PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l1[1], return_status);
737                 ENTROPY_TRACE("delta_chroma_offset_l1[ i ]", delta_chroma_offset_l1[1]);
738             }
739         }
740     }
741 
742     return return_status;
743 }
744 
745 /**
746 ******************************************************************************
747 *
748 *  @brief Generates AUD (Access Unit Delimiter)
749 *
750 *  @par   Description
751 *  Generate Access Unit Delimiter as per Section 7.3.2.5
752 *
753 *  @param[in]   ps_bitstrm
754 *  pointer to bitstream context (handle)
755 *
756 *  @param[in]   pic_type
757 *  picture type
758 *
759 *  @return      success or failure error code
760 *
761 ******************************************************************************
762 */
ihevce_generate_aud(bitstrm_t * ps_bitstrm,WORD32 pic_type)763 WORD32 ihevce_generate_aud(bitstrm_t *ps_bitstrm, WORD32 pic_type)
764 {
765     WORD32 return_status = IHEVCE_SUCCESS;
766 
767     /* Insert the NAL start code */
768     return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
769 
770     /* Insert Nal Unit Header */
771     return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_AUD, 0);
772 
773     /* pic_type */
774     PUT_BITS(ps_bitstrm, pic_type, 3, return_status);
775     ENTROPY_TRACE("pic type", pic_type);
776 
777     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
778 
779     return return_status;
780 }
781 
782 /**
783 ******************************************************************************
784 *
785 *  @brief Generates EOS (End of Sequence)
786 *
787 *  @par   Description
788 *  Generate End of sequence as per Section 7.3.2.6
789 *
790 *  @param[in]   ps_bitstrm
791 *  pointer to bitstream context (handle)
792 *
793 *  @return      success or failure error code
794 *
795 ******************************************************************************
796 */
ihevce_generate_eos(bitstrm_t * ps_bitstrm)797 WORD32 ihevce_generate_eos(bitstrm_t *ps_bitstrm)
798 {
799     WORD32 return_status = IHEVCE_SUCCESS;
800 
801     /* Insert the NAL start code */
802     return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
803 
804     /* Insert Nal Unit Header */
805     return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_EOS, 0);
806 
807     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
808 
809     return return_status;
810 }
811 
812 /**
813 ******************************************************************************
814 *
815 *  @brief Generates VPS (Video Parameter Set)
816 *
817 *  @par   Description
818 *  Generate Video Parameter Set as per Section 7.3.2.1
819 *
820 *  @param[in]   ps_bitstrm
821 *  pointer to bitstream context (handle)
822 *
823 *  @param[in]   ps_vps
824 *  pointer to structure containing VPS data
825 *
826 *  @return      success or failure error code
827 *
828 ******************************************************************************
829 */
ihevce_generate_vps(bitstrm_t * ps_bitstrm,vps_t * ps_vps)830 WORD32 ihevce_generate_vps(bitstrm_t *ps_bitstrm, vps_t *ps_vps)
831 {
832     WORD32 i;
833     WORD8 i1_vps_max_sub_layers_minus1 = ps_vps->i1_vps_max_sub_layers - 1;
834     WORD32 return_status = IHEVCE_SUCCESS;
835 
836     /* Insert Start Code */
837     ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
838 
839     /* Insert Nal Unit Header */
840     ihevce_generate_nal_unit_header(ps_bitstrm, NAL_VPS, 0);
841 
842     /* video_parameter_set_id */
843     PUT_BITS(ps_bitstrm, ps_vps->i1_vps_id, 4, return_status);
844     ENTROPY_TRACE("video_parameter_set_id", ps_vps->i1_vps_id);
845 
846     /* vps_reserved_three_2bits */
847     PUT_BITS(ps_bitstrm, 3, 2, return_status);
848     ENTROPY_TRACE("vps_reserved_three_2bits", 3);
849 
850     /* vps_max_layers_minus1  */
851     PUT_BITS(ps_bitstrm, 0, 6, return_status);
852     ENTROPY_TRACE("vps_max_layers_minus1 ", 3);
853 
854     /* vps_max_sub_layers_minus1 */
855     PUT_BITS(ps_bitstrm, i1_vps_max_sub_layers_minus1, 3, return_status);
856     ENTROPY_TRACE("vps_max_sub_layers_minus1", i1_vps_max_sub_layers_minus1);
857 
858     /* vps_temporal_id_nesting_flag */
859     PUT_BITS(ps_bitstrm, ps_vps->i1_vps_temporal_id_nesting_flag, 1, return_status);
860     ENTROPY_TRACE("vps_temporal_id_nesting_flag", ps_vps->i1_vps_temporal_id_nesting_flag);
861 
862     /* vps_reserved_0xffff_16bits */
863     PUT_BITS(ps_bitstrm, 0xffff, 16, return_status);
864     ENTROPY_TRACE("vps_reserved_0xffff_16bits", 0xffff);
865 
866     /* profile-tier and level info */
867     ihevce_generate_profile_tier_level(ps_bitstrm, &ps_vps->s_ptl, 1, i1_vps_max_sub_layers_minus1);
868 
869     /* vps_sub_layer_ordering_info_present_flag */
870     PUT_BITS(ps_bitstrm, ps_vps->i1_sub_layer_ordering_info_present_flag, 1, return_status);
871     ENTROPY_TRACE(
872         "vps_sub_layer_ordering_info_present_flag",
873         ps_vps->i1_sub_layer_ordering_info_present_flag);
874 
875     i = ps_vps->i1_sub_layer_ordering_info_present_flag ? 0 : i1_vps_max_sub_layers_minus1;
876 
877     for(; i <= i1_vps_max_sub_layers_minus1; i++)
878     {
879         /* vps_max_dec_pic_buffering[i] */
880         PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_dec_pic_buffering[i], return_status);
881         ENTROPY_TRACE(
882             "vps_max_dec_pic_buffering_minus1[i]", ps_vps->ai1_vps_max_dec_pic_buffering[i]);
883 
884         /* vps_num_reorder_pics[i] */
885         PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_num_reorder_pics[i], return_status);
886         ENTROPY_TRACE("ai1_vps_max_num_reorder_pics[i]", ps_vps->ai1_vps_max_num_reorder_pics[i]);
887 
888         /* vps_max_latency_increase[i] */
889         PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_latency_increase[i], return_status);
890         ENTROPY_TRACE("ai1_vps_max_latency_increase[i]", ps_vps->ai1_vps_max_latency_increase[i]);
891     }
892 
893     /* vps_max_layer_id */
894     PUT_BITS(ps_bitstrm, ps_vps->i1_vps_max_nuh_reserved_zero_layer_id, 6, return_status);
895     ENTROPY_TRACE("vps_max_layer_id", ps_vps->i1_vps_max_nuh_reserved_zero_layer_id);
896 
897     /* vps_num_layer_sets_minus1 */
898     PUT_BITS_UEV(ps_bitstrm, 0, return_status);
899     ENTROPY_TRACE("vps_num_layer_sets_minus1", 0);
900 
901     /* vps_timing_info_present_flag */
902     PUT_BITS(ps_bitstrm, 0, 1, return_status);
903     ENTROPY_TRACE("vps_timing_info_present_flag", 0);
904 
905     /* vps_extension_flag */
906     PUT_BITS(ps_bitstrm, 0, 1, return_status);
907     ENTROPY_TRACE("vps_extension_flag", 0);
908 
909     /* rbsp trailing bits */
910     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
911 
912     return return_status;
913 }
914 
915 /**
916 ******************************************************************************
917 *
918 *  @brief Generates SPS (Video Parameter Set)
919 *
920 *  @par   Description
921 *  Parse Video Parameter Set as per Section 7.3.2.2
922 *
923 *  @param[in]   ps_bitstrm
924 *  pointer to bitstream context (handle)
925 *
926 *  @param[in]   ps_sps
927 *  pointer to structure containing SPS data
928 *
929 *  @return      success or failure error code
930 *
931 ******************************************************************************
932 */
ihevce_generate_sps(bitstrm_t * ps_bitstrm,sps_t * ps_sps)933 WORD32 ihevce_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps)
934 {
935     WORD32 i;
936     WORD32 return_status = IHEVCE_SUCCESS;
937     WORD8 i1_max_sub_layers_minus1 = ps_sps->i1_sps_max_sub_layers - 1;
938 
939     UWORD32 u4_log2_max_pic_order_cnt_lsb = (UWORD32)(ps_sps->i1_log2_max_pic_order_cnt_lsb);
940 
941     UWORD32 u4_log2_min_coding_block_size_minus3 =
942         (UWORD32)(ps_sps->i1_log2_min_coding_block_size) - 3;
943 
944     UWORD32 u4_log2_diff_max_min_coding_block_size =
945         (UWORD32)(ps_sps->i1_log2_diff_max_min_coding_block_size);
946 
947     UWORD32 u4_log2_min_transform_block_size_minus2 =
948         (UWORD32)(ps_sps->i1_log2_min_transform_block_size) - 2;
949 
950     UWORD32 u4_log2_diff_max_min_transform_block_size =
951         (UWORD32)(ps_sps->i1_log2_diff_max_min_transform_block_size);
952 
953     /* Insert Start Code */
954     return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
955 
956     /* Insert Nal Unit Header */
957     return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_SPS, 0);
958 
959     /* video_parameter_set_id */
960     PUT_BITS(ps_bitstrm, ps_sps->i1_vps_id, 4, return_status);
961     ENTROPY_TRACE("video_parameter_set_id", ps_sps->i1_vps_id);
962 
963     /* sps_max_sub_layers_minus1 */
964     PUT_BITS(ps_bitstrm, i1_max_sub_layers_minus1, 3, return_status);
965     ENTROPY_TRACE("sps_max_sub_layers_minus1", i1_max_sub_layers_minus1);
966 
967     /* sps_temporal_id_nesting_flag */
968     PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_id_nesting_flag, 1, return_status);
969     ENTROPY_TRACE("sps_temporal_id_nesting_flag", ps_sps->i1_sps_temporal_id_nesting_flag);
970 
971     /* profile-tier and level info */
972     ihevce_generate_profile_tier_level(ps_bitstrm, &ps_sps->s_ptl, 1, i1_max_sub_layers_minus1);
973 
974     /* seq_parameter_set_id */
975     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_sps_id, return_status);
976     ENTROPY_TRACE("seq_parameter_set_id", ps_sps->i1_sps_id);
977 
978     /* chroma_format_idc */
979     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_chroma_format_idc, return_status);
980     ENTROPY_TRACE("chroma_format_idc", ps_sps->i1_chroma_format_idc);
981 
982     if(CHROMA_FMT_IDC_YUV444 == ps_sps->i1_chroma_format_idc)
983     {
984         /* separate_colour_plane_flag */
985         PUT_BITS(ps_bitstrm, 1, 1, return_status);
986         ENTROPY_TRACE("separate_colour_plane_flag", 1);
987     }
988 
989     /* pic_width_in_luma_samples */
990     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_luma_samples, return_status);
991     ENTROPY_TRACE("pic_width_in_luma_samples", ps_sps->i2_pic_width_in_luma_samples);
992 
993     /* pic_height_in_luma_samples */
994     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_luma_samples, return_status);
995     ENTROPY_TRACE("pic_height_in_luma_samples", ps_sps->i2_pic_height_in_luma_samples);
996 
997     /* pic_cropping_flag */
998     PUT_BITS(ps_bitstrm, ps_sps->i1_pic_cropping_flag, 1, return_status);
999     ENTROPY_TRACE("pic_cropping_flag", ps_sps->i1_pic_cropping_flag);
1000 
1001     if(ps_sps->i1_pic_cropping_flag)
1002     {
1003         /* pic_crop_left_offset */
1004         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_left_offset, return_status);
1005         ENTROPY_TRACE("pic_crop_left_offset", ps_sps->i2_pic_crop_left_offset);
1006 
1007         /* pic_crop_right_offset */
1008         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_right_offset, return_status);
1009         ENTROPY_TRACE("pic_crop_right_offset", ps_sps->i2_pic_crop_right_offset);
1010 
1011         /* pic_crop_top_offset */
1012         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_top_offset, return_status);
1013         ENTROPY_TRACE("pic_crop_top_offset", ps_sps->i2_pic_crop_top_offset);
1014 
1015         /* pic_crop_bottom_offset */
1016         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_bottom_offset, return_status);
1017         ENTROPY_TRACE("pic_crop_bottom_offset", ps_sps->i2_pic_crop_bottom_offset);
1018     }
1019 
1020     /* bit_depth_luma_minus8 */
1021     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_luma_minus8, return_status);
1022     ENTROPY_TRACE("bit_depth_luma_minus8", ps_sps->i1_bit_depth_luma_minus8);
1023 
1024     /* bit_depth_chroma_minus8 */
1025     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_chroma_minus8, return_status);
1026     ENTROPY_TRACE("i1_bit_depth_chroma_minus8", ps_sps->i1_bit_depth_chroma_minus8);
1027 
1028     /* log2_max_pic_order_cnt_lsb_minus4 */
1029     PUT_BITS_UEV(ps_bitstrm, u4_log2_max_pic_order_cnt_lsb - 4, return_status);
1030     ENTROPY_TRACE("log2_max_pic_order_cnt_lsb_minus4", u4_log2_max_pic_order_cnt_lsb - 4);
1031 
1032     /* sps_sub_layer_ordering_info_present_flag */
1033     PUT_BITS(ps_bitstrm, ps_sps->i1_sps_sub_layer_ordering_info_present_flag, 1, return_status);
1034     ENTROPY_TRACE(
1035         "sps_sub_layer_ordering_info_present_flag",
1036         ps_sps->i1_sps_sub_layer_ordering_info_present_flag);
1037 
1038     i = ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : i1_max_sub_layers_minus1;
1039 
1040     for(; i <= i1_max_sub_layers_minus1; i++)
1041     {
1042         /* max_dec_pic_buffering */
1043         PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_dec_pic_buffering[i], return_status);
1044         ENTROPY_TRACE("max_dec_pic_buffering_minus1", ps_sps->ai1_sps_max_dec_pic_buffering[i]);
1045 
1046         /* num_reorder_pics */
1047         PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_num_reorder_pics[i], return_status);
1048         ENTROPY_TRACE("num_reorder_pics", ps_sps->ai1_sps_max_num_reorder_pics[i]);
1049 
1050         /* max_latency_increase */
1051         PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_latency_increase[i], return_status);
1052         ENTROPY_TRACE("max_latency_increase", ps_sps->ai1_sps_max_latency_increase[i]);
1053     }
1054 
1055     /* log2_min_coding_block_size_minus3 */
1056     PUT_BITS_UEV(ps_bitstrm, u4_log2_min_coding_block_size_minus3, return_status);
1057     ENTROPY_TRACE("log2_min_coding_block_size_minus3", u4_log2_min_coding_block_size_minus3);
1058 
1059     /* log2_diff_max_min_coding_block_size */
1060     PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_coding_block_size, return_status);
1061     ENTROPY_TRACE("log2_diff_max_min_coding_block_size", u4_log2_diff_max_min_coding_block_size);
1062 
1063     /* log2_min_transform_block_size_minus2 */
1064     PUT_BITS_UEV(ps_bitstrm, u4_log2_min_transform_block_size_minus2, return_status);
1065     ENTROPY_TRACE("log2_min_transform_block_size_minus2", u4_log2_min_transform_block_size_minus2);
1066 
1067     /* log2_diff_max_min_transform_block_size */
1068     PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_transform_block_size, return_status);
1069     ENTROPY_TRACE(
1070         "log2_diff_max_min_transform_block_size", u4_log2_diff_max_min_transform_block_size);
1071 
1072     /* max_transform_hierarchy_depth_inter */
1073     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_inter, return_status);
1074     ENTROPY_TRACE(
1075         "max_transform_hierarchy_depth_inter", ps_sps->i1_max_transform_hierarchy_depth_inter);
1076 
1077     /* max_transform_hierarchy_depth_intra */
1078     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_intra, return_status);
1079     ENTROPY_TRACE(
1080         "max_transform_hierarchy_depth_intra", ps_sps->i1_max_transform_hierarchy_depth_intra);
1081 
1082     /* scaling_list_enabled_flag */
1083     PUT_BITS(ps_bitstrm, ps_sps->i1_scaling_list_enable_flag, 1, return_status);
1084     ENTROPY_TRACE("scaling_list_enabled_flag", ps_sps->i1_scaling_list_enable_flag);
1085 
1086     if(ps_sps->i1_scaling_list_enable_flag)
1087     {
1088         /* sps_scaling_list_data_present_flag */
1089         PUT_BITS(ps_bitstrm, ps_sps->i1_sps_scaling_list_data_present_flag, 1, return_status);
1090         ENTROPY_TRACE(
1091             "sps_scaling_list_data_present_flag", ps_sps->i1_sps_scaling_list_data_present_flag);
1092 
1093 #if 0 /* TODO: Will be enabled once scaling list support is added */
1094         if(ps_sps->i1_sps_scaling_list_data_present_flag)
1095         {
1096             //TODO
1097             ihevce_generate_scaling_list_data(ps_bitstrm);
1098         }
1099 #endif
1100     }
1101 
1102     /* asymmetric_motion_partitions_enabled_flag */
1103     PUT_BITS(ps_bitstrm, ps_sps->i1_amp_enabled_flag, 1, return_status);
1104     ENTROPY_TRACE("asymmetric_motion_partitions_enabled_flag", ps_sps->i1_amp_enabled_flag);
1105 
1106     /* sample_adaptive_offset_enabled_flag */
1107     PUT_BITS(ps_bitstrm, ps_sps->i1_sample_adaptive_offset_enabled_flag, 1, return_status);
1108     ENTROPY_TRACE(
1109         "sample_adaptive_offset_enabled_flag", ps_sps->i1_sample_adaptive_offset_enabled_flag);
1110 
1111     /* pcm_enabled_flag */
1112     PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_enabled_flag, 1, return_status);
1113     ENTROPY_TRACE("pcm_enabled_flag", ps_sps->i1_pcm_enabled_flag);
1114     if(ps_sps->i1_pcm_enabled_flag)
1115     {
1116         UWORD32 u4_log2_min_pcm_coding_block_size = (ps_sps->i1_log2_min_pcm_coding_block_size);
1117         UWORD32 u4_log2_diff_max_min_pcm_coding_block_size =
1118             (ps_sps->i1_log2_diff_max_min_pcm_coding_block_size);
1119 
1120         /* pcm_sample_bit_depth_luma_minus1 */
1121         PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_sample_bit_depth_luma - 1, 4, return_status);
1122         ENTROPY_TRACE("pcm_sample_bit_depth_luma", ps_sps->i1_pcm_sample_bit_depth_luma - 1);
1123 
1124         /* pcm_sample_bit_depth_chroma_minus1 */
1125         PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_sample_bit_depth_chroma - 1, 4, return_status);
1126         ENTROPY_TRACE("pcm_sample_bit_depth_chroma", ps_sps->i1_pcm_sample_bit_depth_chroma - 1);
1127 
1128         /* log2_min_pcm_coding_block_size_minus3 */
1129         PUT_BITS_UEV(ps_bitstrm, u4_log2_min_pcm_coding_block_size - 3, return_status);
1130         ENTROPY_TRACE(
1131             "log2_min_pcm_coding_block_size_minus3", u4_log2_min_pcm_coding_block_size - 3);
1132 
1133         /* log2_diff_max_min_pcm_coding_block_size */
1134         PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_pcm_coding_block_size, return_status);
1135         ENTROPY_TRACE(
1136             "log2_diff_max_min_pcm_coding_block_size", u4_log2_diff_max_min_pcm_coding_block_size);
1137 
1138         /* pcm_loop_filter_disable_flag */
1139         PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_loop_filter_disable_flag, 1, return_status);
1140         ENTROPY_TRACE("pcm_loop_filter_disable_flag", ps_sps->i1_pcm_loop_filter_disable_flag);
1141     }
1142 
1143     /* num_short_term_ref_pic_sets */
1144     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_num_short_term_ref_pic_sets, return_status);
1145     ENTROPY_TRACE("num_short_term_ref_pic_sets", ps_sps->i1_num_short_term_ref_pic_sets);
1146 
1147     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1148     {
1149         WORD32 i4_NumPocTotalCurr = 0;
1150         ihevce_short_term_ref_pic_set(
1151             ps_bitstrm,
1152             &ps_sps->as_stref_picset[0],
1153             ps_sps->i1_num_short_term_ref_pic_sets,
1154             i,
1155             &i4_NumPocTotalCurr);
1156     }
1157 
1158     /* long_term_ref_pics_present_flag */
1159     PUT_BITS(ps_bitstrm, ps_sps->i1_long_term_ref_pics_present_flag, 1, return_status);
1160     ENTROPY_TRACE("long_term_ref_pics_present_flag", ps_sps->i1_long_term_ref_pics_present_flag);
1161 
1162     if(ps_sps->i1_long_term_ref_pics_present_flag)
1163     {
1164         /* num_long_term_ref_pics_sps */
1165         PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_num_long_term_ref_pics_sps, return_status);
1166         ENTROPY_TRACE("num_long_term_ref_pics_sps", ps_sps->i1_num_long_term_ref_pics_sps);
1167 
1168         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1169         {
1170             /* lt_ref_pic_poc_lsb_sps[i] */
1171             PUT_BITS(
1172                 ps_bitstrm,
1173                 ps_sps->au2_lt_ref_pic_poc_lsb_sps[i],
1174                 u4_log2_max_pic_order_cnt_lsb,
1175                 return_status);
1176             ENTROPY_TRACE("lt_ref_pic_poc_lsb_sps[i]", ps_sps->au2_lt_ref_pic_poc_lsb_sps[i]);
1177 
1178             /* used_by_curr_pic_lt_sps_flag[i] */
1179             PUT_BITS(ps_bitstrm, ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i], 1, return_status);
1180             ENTROPY_TRACE(
1181                 "used_by_curr_pic_lt_sps_flag[i]", ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i]);
1182         }
1183     }
1184 
1185     /* sps_temporal_mvp_enable_flag */
1186     PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_mvp_enable_flag, 1, return_status);
1187     ENTROPY_TRACE("sps_temporal_mvp_enable_flag", ps_sps->i1_sps_temporal_mvp_enable_flag);
1188 
1189 #if !HM_8DOT1_SYNTAX
1190     /* strong_intra_smoothing_enable_flag */
1191     PUT_BITS(ps_bitstrm, ps_sps->i1_strong_intra_smoothing_enable_flag, 1, return_status);
1192     ENTROPY_TRACE(
1193         "sps_strong_intra_smoothing_enable_flag", ps_sps->i1_strong_intra_smoothing_enable_flag);
1194 #endif
1195 
1196     /* vui_parameters_present_flag */
1197     PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status);
1198     ENTROPY_TRACE("vui_parameters_present_flag", ps_sps->i1_vui_parameters_present_flag);
1199 
1200     ENTROPY_TRACE("----------- vui_parameters -----------", 0);
1201 
1202     if(ps_sps->i1_vui_parameters_present_flag)
1203     {
1204         /* Add vui parameters to the bitstream */
1205         ihevce_generate_vui(ps_bitstrm, ps_sps, ps_sps->s_vui_parameters);
1206     }
1207 
1208     /* sps_extension_flag */
1209     PUT_BITS(ps_bitstrm, 0, 1, return_status);
1210     ENTROPY_TRACE("sps_extension_flag", 0);
1211 
1212     /* rbsp trailing bits */
1213     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1214 
1215     return return_status;
1216 }
1217 
1218 /**
1219 ******************************************************************************
1220 *
1221 *  @brief Generates PPS (Picture Parameter Set)
1222 *
1223 *  @par   Description
1224 *  Generate Picture Parameter Set as per Section 7.3.2.3
1225 *
1226 *  @param[in]   ps_bitstrm
1227 *  pointer to bitstream context (handle)
1228 *
1229 *  @param[in]   ps_pps
1230 *  pointer to structure containing PPS data
1231 *
1232 *  @return      success or failure error code
1233 *
1234 ******************************************************************************
1235 */
ihevce_generate_pps(bitstrm_t * ps_bitstrm,pps_t * ps_pps)1236 WORD32 ihevce_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps)
1237 {
1238     WORD32 i;
1239     WORD32 return_status = IHEVCE_SUCCESS;
1240 
1241     /* Insert the NAL start code */
1242     return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1243 
1244     /* Insert Nal Unit Header */
1245     return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_PPS, 0);
1246 
1247     /* pic_parameter_set_id */
1248     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_pps_id, return_status);
1249     ENTROPY_TRACE("pic_parameter_set_id", ps_pps->i1_pps_id);
1250 
1251     /* seq_parameter_set_id */
1252     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_sps_id, return_status);
1253     ENTROPY_TRACE("seq_parameter_set_id", ps_pps->i1_sps_id);
1254 
1255     /* dependent_slices_enabled_flag */
1256     PUT_BITS(ps_bitstrm, ps_pps->i1_dependent_slice_enabled_flag, 1, return_status);
1257     ENTROPY_TRACE("dependent_slices_enabled_flag", ps_pps->i1_dependent_slice_enabled_flag);
1258 
1259     /* output_flag_present_flag */
1260     PUT_BITS(ps_bitstrm, ps_pps->i1_output_flag_present_flag, 1, return_status);
1261     ENTROPY_TRACE("output_flag_present_flag", ps_pps->i1_output_flag_present_flag);
1262 
1263     /* num_extra_slice_header_bits */
1264     PUT_BITS(ps_bitstrm, ps_pps->i1_num_extra_slice_header_bits, 3, return_status);
1265     ENTROPY_TRACE("num_extra_slice_header_bits", ps_pps->i1_num_extra_slice_header_bits);
1266 
1267     /* sign_data_hiding_flag */
1268     PUT_BITS(ps_bitstrm, ps_pps->i1_sign_data_hiding_flag, 1, return_status);
1269     ENTROPY_TRACE("sign_data_hiding_flag", ps_pps->i1_sign_data_hiding_flag);
1270 
1271     /* cabac_init_present_flag */
1272     PUT_BITS(ps_bitstrm, ps_pps->i1_cabac_init_present_flag, 1, return_status);
1273     ENTROPY_TRACE("cabac_init_present_flag", ps_pps->i1_cabac_init_present_flag);
1274 
1275     /* num_ref_idx_l0_default_active_minus1 */
1276     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status);
1277     ENTROPY_TRACE(
1278         "num_ref_idx_l0_default_active_minus1", ps_pps->i1_num_ref_idx_l0_default_active - 1);
1279 
1280     /* num_ref_idx_l1_default_active_minus1 */
1281     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status);
1282     ENTROPY_TRACE(
1283         "num_ref_idx_l1_default_active_minus1", ps_pps->i1_num_ref_idx_l1_default_active - 1);
1284 
1285     /* pic_init_qp_minus26 */
1286     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status);
1287     ENTROPY_TRACE("pic_init_qp_minus26", ps_pps->i1_pic_init_qp - 26);
1288 
1289     /* constrained_intra_pred_flag */
1290     PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status);
1291     ENTROPY_TRACE("constrained_intra_pred_flag", ps_pps->i1_constrained_intra_pred_flag);
1292 
1293     /* transform_skip_enabled_flag */
1294     PUT_BITS(ps_bitstrm, ps_pps->i1_transform_skip_enabled_flag, 1, return_status);
1295     ENTROPY_TRACE("transform_skip_enabled_flag", ps_pps->i1_transform_skip_enabled_flag);
1296 
1297     /* cu_qp_delta_enabled_flag */
1298     PUT_BITS(ps_bitstrm, ps_pps->i1_cu_qp_delta_enabled_flag, 1, return_status);
1299     ENTROPY_TRACE("cu_qp_delta_enabled_flag", ps_pps->i1_cu_qp_delta_enabled_flag);
1300 
1301     if(ps_pps->i1_cu_qp_delta_enabled_flag)
1302     {
1303         /* diff_cu_qp_delta_depth */
1304         PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_diff_cu_qp_delta_depth, return_status);
1305         ENTROPY_TRACE("diff_cu_qp_delta_depth", ps_pps->i1_diff_cu_qp_delta_depth);
1306     }
1307 
1308     /* cb_qp_offset */
1309     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cb_qp_offset, return_status);
1310     ENTROPY_TRACE("cb_qp_offset", ps_pps->i1_pic_cb_qp_offset);
1311 
1312     /* cr_qp_offset */
1313     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cr_qp_offset, return_status);
1314     ENTROPY_TRACE("cr_qp_offset", ps_pps->i1_pic_cr_qp_offset);
1315 
1316     /* slicelevel_chroma_qp_flag */
1317     PUT_BITS(
1318         ps_bitstrm, ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag, 1, return_status);
1319     ENTROPY_TRACE(
1320         "slicelevel_chroma_qp_flag", ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag);
1321 
1322     /* weighted_pred_flag */
1323     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status);
1324     ENTROPY_TRACE("weighted_pred_flag", ps_pps->i1_weighted_pred_flag);
1325 
1326     /* weighted_bipred_flag */
1327     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_flag, 1, return_status);
1328     ENTROPY_TRACE("weighted_bipred_flag", ps_pps->i1_weighted_bipred_flag);
1329 
1330     /* transquant_bypass_enable_flag */
1331     PUT_BITS(ps_bitstrm, ps_pps->i1_transquant_bypass_enable_flag, 1, return_status);
1332     ENTROPY_TRACE("transquant_bypass_enable_flag", ps_pps->i1_transquant_bypass_enable_flag);
1333 
1334     /* tiles_enabled_flag */
1335     PUT_BITS(ps_bitstrm, ps_pps->i1_tiles_enabled_flag, 1, return_status);
1336     ENTROPY_TRACE("tiles_enabled_flag", ps_pps->i1_tiles_enabled_flag);
1337 
1338     /* entropy_coding_sync_enabled_flag */
1339     PUT_BITS(ps_bitstrm, ps_pps->i1_entropy_coding_sync_enabled_flag, 1, return_status);
1340     ENTROPY_TRACE("entropy_coding_sync_enabled_flag", ps_pps->i1_entropy_coding_sync_enabled_flag);
1341 
1342     if(ps_pps->i1_tiles_enabled_flag)
1343     {
1344         /* num_tile_columns_minus1 */
1345         PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_tile_columns - 1, return_status);
1346         ENTROPY_TRACE("num_tile_columns_minus1", ps_pps->i1_num_tile_columns - 1);
1347 
1348         /* num_tile_rows_minus1 */
1349         PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_tile_rows - 1, return_status);
1350         ENTROPY_TRACE("num_tile_rows_minus1", ps_pps->i1_num_tile_rows - 1);
1351 
1352         /* uniform_spacing_flag */
1353         PUT_BITS(ps_bitstrm, ps_pps->i1_uniform_spacing_flag, 1, return_status);
1354         ENTROPY_TRACE("uniform_spacing_flag", ps_pps->i1_uniform_spacing_flag);
1355 
1356         if(!ps_pps->i1_uniform_spacing_flag)
1357         {
1358             for(i = 0; i < ps_pps->i1_num_tile_columns - 1; i++)
1359             {
1360                 /* column_width_minus1[i] */
1361                 PUT_BITS_UEV(ps_bitstrm, ps_pps->ps_tile[i].u2_wd - 1, return_status);
1362                 ENTROPY_TRACE("column_width_minus1[i]", ps_pps->ps_tile[i].u2_wd - 1);
1363             }
1364             for(i = 0; i < ps_pps->i1_num_tile_rows - 1; i++)
1365             {
1366                 /* row_height_minus1[i] */
1367                 PUT_BITS_UEV(ps_bitstrm, ps_pps->ps_tile[i].u2_ht - 1, return_status);
1368                 ENTROPY_TRACE("row_height_minus1[i]", ps_pps->ps_tile[i].u2_ht - 1);
1369             }
1370         }
1371 
1372         /* loop_filter_across_tiles_enabled_flag */
1373         PUT_BITS(ps_bitstrm, ps_pps->i1_loop_filter_across_tiles_enabled_flag, 1, return_status);
1374         ENTROPY_TRACE(
1375             "loop_filter_across_tiles_enabled_flag",
1376             ps_pps->i1_loop_filter_across_tiles_enabled_flag);
1377     }
1378 
1379     /* loop_filter_across_slices_enabled_flag */
1380     PUT_BITS(ps_bitstrm, ps_pps->i1_loop_filter_across_slices_enabled_flag, 1, return_status);
1381     ENTROPY_TRACE(
1382         "loop_filter_across_slices_enabled_flag",
1383         ps_pps->i1_loop_filter_across_slices_enabled_flag);
1384 
1385     /* deblocking_filter_control_present_flag */
1386     PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status);
1387     ENTROPY_TRACE(
1388         "deblocking_filter_control_present_flag",
1389         ps_pps->i1_deblocking_filter_control_present_flag);
1390 
1391     if(ps_pps->i1_deblocking_filter_control_present_flag)
1392     {
1393         /* deblocking_filter_override_enabled_flag */
1394         PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_override_enabled_flag, 1, return_status);
1395         ENTROPY_TRACE(
1396             "deblocking_filter_override_enabled_flag",
1397             ps_pps->i1_deblocking_filter_override_enabled_flag);
1398 
1399         /* pic_disable_deblocking_filter_flag */
1400         PUT_BITS(ps_bitstrm, ps_pps->i1_pic_disable_deblocking_filter_flag, 1, return_status);
1401         ENTROPY_TRACE(
1402             "pic_disable_deblocking_filter_flag", ps_pps->i1_pic_disable_deblocking_filter_flag);
1403 
1404         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
1405         {
1406             /* beta_offset_div2 */
1407             PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_beta_offset_div2 >> 1, return_status);
1408             ENTROPY_TRACE("beta_offset_div2", ps_pps->i1_beta_offset_div2 >> 1);
1409 
1410             /* tc_offset_div2 */
1411             PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_tc_offset_div2 >> 1, return_status);
1412             ENTROPY_TRACE("tc_offset_div2", ps_pps->i1_tc_offset_div2 >> 1);
1413         }
1414     }
1415 
1416     /* pps_scaling_list_data_present_flag */
1417     PUT_BITS(ps_bitstrm, ps_pps->i1_pps_scaling_list_data_present_flag, 1, return_status);
1418     ENTROPY_TRACE(
1419         "pps_scaling_list_data_present_flag", ps_pps->i1_pps_scaling_list_data_present_flag);
1420 
1421 #if 0 /* TODO: Will be enabled once scaling list support is added */
1422     if(ps_pps->i1_pps_scaling_list_data_present_flag )
1423     {
1424         //TODO
1425         ihevce_scaling_list_data();
1426     }
1427 #endif
1428 
1429     /* lists_modification_present_flag */
1430     PUT_BITS(ps_bitstrm, ps_pps->i1_lists_modification_present_flag, 1, return_status);
1431     ENTROPY_TRACE("lists_modification_present_flag", ps_pps->i1_lists_modification_present_flag);
1432 
1433     {
1434         UWORD32 u4_log2_parallel_merge_level_minus2 = ps_pps->i1_log2_parallel_merge_level;
1435 
1436         u4_log2_parallel_merge_level_minus2 -= 2;
1437 
1438         /* log2_parallel_merge_level_minus2 */
1439         PUT_BITS_UEV(ps_bitstrm, u4_log2_parallel_merge_level_minus2, return_status);
1440         ENTROPY_TRACE("log2_parallel_merge_level_minus2", u4_log2_parallel_merge_level_minus2);
1441     }
1442 
1443     /* slice_header_extension_present_flag */
1444     PUT_BITS(ps_bitstrm, ps_pps->i1_slice_header_extension_present_flag, 1, return_status);
1445     ENTROPY_TRACE(
1446         "slice_header_extension_present_flag", ps_pps->i1_slice_header_extension_present_flag);
1447 
1448     /* pps_extension_flag */
1449     PUT_BITS(ps_bitstrm, 0, 1, return_status);
1450     ENTROPY_TRACE("pps_extension_flag", 0);
1451 
1452     ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1453 
1454     return return_status;
1455 }
1456 
1457 /**
1458 ******************************************************************************
1459 *
1460 *  @brief Generates Slice Header
1461 *
1462 *  @par   Description
1463 *  Generate Slice Header as per Section 7.3.5.1
1464 *
1465 *  @param[inout]   ps_bitstrm
1466 *  pointer to bitstream context for generating slice header
1467 *
1468 *  @param[in]   i1_nal_unit_type
1469 *  nal unit type
1470 *
1471 *  @param[in]   ps_slice_hdr
1472 *  pointer to slice header params
1473 *
1474 *  @param[in]   ps_pps
1475 *  pointer to pps params referred by slice
1476 *
1477 *  @param[in]   ps_sps
1478 *  pointer to sps params referred by slice
1479 *
1480 *  @return      success or failure error code
1481 *
1482 ******************************************************************************
1483 */
ihevce_generate_slice_header(bitstrm_t * ps_bitstrm,WORD8 i1_nal_unit_type,slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps,bitstrm_t * ps_dup_bit_strm_ent_offset,UWORD32 * pu4_first_slice_start_offset,ihevce_tile_params_t * ps_tile_params,WORD32 i4_next_slice_seg_x,WORD32 i4_next_slice_seg_y)1484 WORD32 ihevce_generate_slice_header(
1485     bitstrm_t *ps_bitstrm,
1486     WORD8 i1_nal_unit_type,
1487     slice_header_t *ps_slice_hdr,
1488     pps_t *ps_pps,
1489     sps_t *ps_sps,
1490     bitstrm_t *ps_dup_bit_strm_ent_offset,
1491     UWORD32 *pu4_first_slice_start_offset,
1492     ihevce_tile_params_t *ps_tile_params,
1493     WORD32 i4_next_slice_seg_x,
1494     WORD32 i4_next_slice_seg_y)
1495 {
1496     WORD32 i;
1497     WORD32 return_status = IHEVCE_SUCCESS;
1498 
1499     WORD32 RapPicFlag = (i1_nal_unit_type >= NAL_BLA_W_LP) &&
1500                         (i1_nal_unit_type <= NAL_RSV_RAP_VCL23);
1501     WORD32 idr_pic_flag = (NAL_IDR_W_LP == i1_nal_unit_type) || (NAL_IDR_N_LP == i1_nal_unit_type);
1502 
1503     WORD32 disable_deblocking_filter_flag;
1504 
1505     WORD32 i4_NumPocTotalCurr = 0;
1506     /* Initialize the pic width and pic height from sps parameters */
1507     WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples;
1508     WORD32 pic_height = ps_sps->i2_pic_height_in_luma_samples;
1509 
1510     /* Initialize the CTB size from sps parameters */
1511     WORD32 log2_ctb_size =
1512         ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1513     WORD32 ctb_size = (1 << log2_ctb_size);
1514 
1515     /* Update ps_slice_hdr->i2_slice_address based on tile position in frame */
1516     WORD32 num_ctb_in_row = (pic_width + ctb_size - 1) >> log2_ctb_size;
1517 
1518     /* Overwrite i2_slice_address here as pre-enc didn't had tile structure
1519     available in it's scope. Otherwise i2_slice_address would be set in
1520     populate_slice_header() itself */
1521     if(1 == ps_tile_params->i4_tiles_enabled_flag)
1522     {
1523         ps_slice_hdr->i2_slice_address =
1524             ps_tile_params->i4_first_ctb_y * num_ctb_in_row + ps_tile_params->i4_first_ctb_x;
1525     }
1526     else
1527     {
1528         ps_slice_hdr->i2_slice_address = i4_next_slice_seg_x + i4_next_slice_seg_y * num_ctb_in_row;
1529     }
1530 
1531     /* Overwrite i1_first_slice_in_pic_flag here as pre-enc didn't had tile structure
1532     available in it's scope. Otherwise i1_first_slice_in_pic_flag would be set in
1533     populate_slice_header() itself */
1534     ps_slice_hdr->i1_first_slice_in_pic_flag = (ps_slice_hdr->i2_slice_address == 0);
1535 
1536     /* Currently if dependent slices are enabled, then all slices
1537     after first slice of picture, are made dependent slices */
1538     if((1 == ps_pps->i1_dependent_slice_enabled_flag) &&
1539        (0 == ps_slice_hdr->i1_first_slice_in_pic_flag))
1540     {
1541         ps_slice_hdr->i1_dependent_slice_flag = 1;
1542     }
1543     else
1544     {
1545         ps_slice_hdr->i1_dependent_slice_flag = 0;
1546     }
1547 
1548     /* Insert start code */
1549     return_status |= ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1550 
1551     /* Insert Nal Unit Header */
1552     return_status |= ihevce_generate_nal_unit_header(
1553         ps_bitstrm,
1554         i1_nal_unit_type,
1555         ps_slice_hdr->u4_nuh_temporal_id);  //TEMPORALA_SCALABILITY CHANGES
1556 
1557     /* first_slice_in_pic_flag */
1558     PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_first_slice_in_pic_flag, 1, return_status);
1559     ENTROPY_TRACE("first_slice_in_pic_flag", ps_slice_hdr->i1_first_slice_in_pic_flag);
1560 
1561     if(RapPicFlag)
1562     {
1563         /* no_output_of_prior_pics_flag */
1564         PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_no_output_of_prior_pics_flag, 1, return_status);
1565         ENTROPY_TRACE(
1566             "no_output_of_prior_pics_flag", ps_slice_hdr->i1_no_output_of_prior_pics_flag);
1567     }
1568 
1569     /* pic_parameter_set_id */
1570     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_pps_id, return_status);
1571     ENTROPY_TRACE("pic_parameter_set_id", ps_slice_hdr->i1_pps_id);
1572 
1573     /* If ps_pps->i1_dependent_slice_enabled_flag is enabled and
1574     curent slice is not the first slice of picture then put
1575     i1_dependent_slice_flag into the bitstream */
1576     if((ps_pps->i1_dependent_slice_enabled_flag) && (!ps_slice_hdr->i1_first_slice_in_pic_flag))
1577     {
1578         /* dependent_slice_flag */
1579         PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_dependent_slice_flag, 1, return_status);
1580         ENTROPY_TRACE("dependent_slice_flag", ps_slice_hdr->i1_dependent_slice_flag);
1581     }
1582 
1583     if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
1584     {
1585         WORD32 num_bits;
1586         WORD32 num_ctb_in_pic;
1587 
1588         /* ctbs in frame ceiled for width / height not multiple of ctb size */
1589         num_ctb_in_pic = ((pic_width + (ctb_size - 1)) >> log2_ctb_size) *
1590                          ((pic_height + (ctb_size - 1)) >> log2_ctb_size);
1591 
1592         /* Use CLZ to compute Ceil( Log2( PicSizeInCtbsY ) ) */
1593         num_bits = 32 - CLZ(num_ctb_in_pic - 1);
1594 
1595         /* slice_address */
1596         PUT_BITS(ps_bitstrm, ps_slice_hdr->i2_slice_address, num_bits, return_status);
1597         ENTROPY_TRACE("slice_address", ps_slice_hdr->i2_slice_address);
1598     }
1599 
1600     if(!ps_slice_hdr->i1_dependent_slice_flag)
1601     {
1602         for(i = 0; i < ps_pps->i1_num_extra_slice_header_bits; i++)
1603         {
1604             /* slice_reserved_undetermined_flag */
1605             PUT_BITS(ps_bitstrm, 0, 1, return_status);
1606             ENTROPY_TRACE("slice_reserved_undetermined_flag", 0);
1607         }
1608         /* slice_type */
1609         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_slice_type, return_status);
1610         ENTROPY_TRACE("slice_type", ps_slice_hdr->i1_slice_type);
1611 
1612         if(ps_pps->i1_output_flag_present_flag)
1613         {
1614             /* pic_output_flag */
1615             PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_pic_output_flag, 1, return_status);
1616             ENTROPY_TRACE("pic_output_flag", ps_slice_hdr->i1_pic_output_flag);
1617         }
1618 
1619         if(!idr_pic_flag)
1620         {
1621             /* pic_order_cnt_lsb */
1622             PUT_BITS(
1623                 ps_bitstrm,
1624                 ps_slice_hdr->i4_pic_order_cnt_lsb,
1625                 ps_sps->i1_log2_max_pic_order_cnt_lsb,
1626                 return_status);
1627             ENTROPY_TRACE("pic_order_cnt_lsb", ps_slice_hdr->i4_pic_order_cnt_lsb);
1628 
1629             /* short_term_ref_pic_set_sps_flag */
1630             PUT_BITS(
1631                 ps_bitstrm, ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag, 1, return_status);
1632             ENTROPY_TRACE(
1633                 "short_term_ref_pic_set_sps_flag",
1634                 ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag);
1635 
1636             if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
1637             {
1638                 ihevce_short_term_ref_pic_set(
1639                     ps_bitstrm, &ps_slice_hdr->s_stref_picset, 1, 0, &i4_NumPocTotalCurr);
1640             }
1641             else
1642             {
1643                 WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets);
1644 
1645                 /* short_term_ref_pic_set_idx */
1646                 PUT_BITS(
1647                     ps_bitstrm,
1648                     ps_slice_hdr->i1_short_term_ref_pic_set_idx,
1649                     num_bits,
1650                     return_status);
1651                 ENTROPY_TRACE(
1652                     "short_term_ref_pic_set_idx", ps_slice_hdr->i1_short_term_ref_pic_set_idx);
1653             }
1654 
1655             if(ps_sps->i1_long_term_ref_pics_present_flag)
1656             {
1657                 if(ps_sps->i1_num_long_term_ref_pics_sps > 0)
1658                 {
1659                     /* num_long_term_sps */
1660                     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_long_term_sps, return_status);
1661                     ENTROPY_TRACE("num_long_term_sps", ps_slice_hdr->i1_num_long_term_sps);
1662                 }
1663 
1664                 /* num_long_term_pics */
1665                 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_long_term_pics, return_status);
1666                 ENTROPY_TRACE("num_long_term_pics", ps_slice_hdr->i1_num_long_term_pics);
1667 
1668                 for(i = 0;
1669                     i < (ps_slice_hdr->i1_num_long_term_sps + ps_slice_hdr->i1_num_long_term_pics);
1670                     i++)
1671                 {
1672                     if(i < ps_slice_hdr->i1_num_long_term_sps)
1673                     {
1674                         /* Use CLZ to compute Ceil( Log2
1675                         ( num_long_term_ref_pics_sps ) ) */
1676                         WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_long_term_ref_pics_sps);
1677 
1678                         /* lt_idx_sps[i] */
1679                         PUT_BITS(
1680                             ps_bitstrm, ps_slice_hdr->ai1_lt_idx_sps[i], num_bits, return_status);
1681                         ENTROPY_TRACE("lt_idx_sps[i]", ps_slice_hdr->ai1_lt_idx_sps[i]);
1682                     }
1683                     else
1684                     {
1685                         /* poc_lsb_lt[i] */
1686                         PUT_BITS(
1687                             ps_bitstrm,
1688                             ps_slice_hdr->ai4_poc_lsb_lt[i],
1689                             ps_sps->i1_log2_max_pic_order_cnt_lsb,
1690                             return_status);
1691                         ENTROPY_TRACE("poc_lsb_lt[i]", ps_slice_hdr->ai4_poc_lsb_lt[i]);
1692 
1693                         /* used_by_curr_pic_lt_flag[i] */
1694                         PUT_BITS(
1695                             ps_bitstrm,
1696                             ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i],
1697                             1,
1698                             return_status);
1699                         ENTROPY_TRACE(
1700                             "used_by_curr_pic_lt_flag[i]",
1701                             ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i]);
1702                     }
1703 
1704                     /* delta_poc_msb_present_flag[i] */
1705                     PUT_BITS(
1706                         ps_bitstrm,
1707                         ps_slice_hdr->ai1_delta_poc_msb_present_flag[i],
1708                         1,
1709                         return_status);
1710                     ENTROPY_TRACE(
1711                         "delta_poc_msb_present_flag[i]",
1712                         ps_slice_hdr->ai1_delta_poc_msb_present_flag[i]);
1713 
1714                     if(ps_slice_hdr->ai1_delta_poc_msb_present_flag[i])
1715                     {
1716                         /* delata_poc_msb_cycle_lt[i] */
1717                         PUT_BITS_UEV(
1718                             ps_bitstrm, ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i], return_status);
1719                         ENTROPY_TRACE(
1720                             "delata_poc_msb_cycle_lt", ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i]);
1721                     }
1722                 }
1723             }
1724 
1725             if(ps_sps->i1_sps_temporal_mvp_enable_flag)
1726             {
1727                 /* slice_temporal_mvp_enable_flag */
1728                 PUT_BITS(
1729                     ps_bitstrm, ps_slice_hdr->i1_slice_temporal_mvp_enable_flag, 1, return_status);
1730                 ENTROPY_TRACE(
1731                     "slice_temporal_mvp_enable_flag",
1732                     ps_slice_hdr->i1_slice_temporal_mvp_enable_flag);
1733             }
1734         }
1735 
1736         if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
1737         {
1738             /* slice_sao_luma_flag */
1739             PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_luma_flag, 1, return_status);
1740             ENTROPY_TRACE("slice_sao_luma_flag", ps_slice_hdr->i1_slice_sao_luma_flag);
1741 
1742             /* slice_sao_chroma_flag */
1743             PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_chroma_flag, 1, return_status);
1744             ENTROPY_TRACE("slice_sao_chroma_flag", ps_slice_hdr->i1_slice_sao_chroma_flag);
1745         }
1746         if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
1747         {
1748             /* num_ref_idx_active_override_flag */
1749             PUT_BITS(
1750                 ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_active_override_flag, 1, return_status);
1751             ENTROPY_TRACE(
1752                 "num_ref_idx_active_override_flag",
1753                 ps_slice_hdr->i1_num_ref_idx_active_override_flag);
1754 
1755             if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
1756             {
1757                 /* i1_num_ref_idx_l0_active_minus1 */
1758                 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status);
1759                 ENTROPY_TRACE(
1760                     "i1_num_ref_idx_l0_active_minus1", ps_slice_hdr->i1_num_ref_idx_l0_active - 1);
1761 
1762                 if(BSLICE == ps_slice_hdr->i1_slice_type)
1763                 {
1764                     /* i1_num_ref_idx_l1_active */
1765                     PUT_BITS_UEV(
1766                         ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status);
1767                     ENTROPY_TRACE(
1768                         "i1_num_ref_idx_l1_active", ps_slice_hdr->i1_num_ref_idx_l1_active - 1);
1769                 }
1770             }
1771 
1772             if(ps_pps->i1_lists_modification_present_flag && i4_NumPocTotalCurr > 1)
1773             {
1774                 ref_pic_list_modification(ps_bitstrm, ps_slice_hdr, i4_NumPocTotalCurr);
1775             }
1776 
1777             if(BSLICE == ps_slice_hdr->i1_slice_type)
1778             {
1779                 /* mvd_l1_zero_flag */
1780                 PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_mvd_l1_zero_flag, 1, return_status);
1781                 ENTROPY_TRACE("mvd_l1_zero_flag", ps_slice_hdr->i1_mvd_l1_zero_flag);
1782             }
1783 
1784             if(ps_pps->i1_cabac_init_present_flag)
1785             {
1786                 /* cabac_init_flag */
1787                 PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_cabac_init_flag, 1, return_status);
1788                 ENTROPY_TRACE("cabac_init_flag", ps_slice_hdr->i1_cabac_init_flag);
1789             }
1790 
1791             if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
1792             {
1793                 if(BSLICE == ps_slice_hdr->i1_slice_type)
1794                 {
1795                     /* collocated_from_l0_flag */
1796                     PUT_BITS(
1797                         ps_bitstrm, ps_slice_hdr->i1_collocated_from_l0_flag, 1, return_status);
1798                     ENTROPY_TRACE(
1799                         "collocated_from_l0_flag", ps_slice_hdr->i1_collocated_from_l0_flag);
1800                 }
1801                 if((ps_slice_hdr->i1_collocated_from_l0_flag &&
1802                     (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
1803                    (!ps_slice_hdr->i1_collocated_from_l0_flag &&
1804                     (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
1805                 {
1806                     /* collocated_ref_idx */
1807                     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_collocated_ref_idx, return_status);
1808                     ENTROPY_TRACE("collocated_ref_idx", ps_slice_hdr->i1_collocated_ref_idx);
1809                 }
1810             }
1811 
1812             if((ps_pps->i1_weighted_pred_flag && (PSLICE == ps_slice_hdr->i1_slice_type)) ||
1813                (ps_pps->i1_weighted_bipred_flag && (BSLICE == ps_slice_hdr->i1_slice_type)))
1814             {
1815                 ihevce_generate_pred_weight_table(ps_bitstrm, ps_sps, ps_pps, ps_slice_hdr);
1816             }
1817 
1818 #if !HM_8DOT1_SYNTAX
1819             /* five_minus_max_num_merge_cand */
1820             PUT_BITS_UEV(ps_bitstrm, 5 - ps_slice_hdr->i1_max_num_merge_cand, return_status);
1821             ENTROPY_TRACE("five_minus_max_num_merge_cand", 5 - ps_slice_hdr->i1_max_num_merge_cand);
1822 #endif
1823         }
1824 #if HM_8DOT1_SYNTAX
1825         /* five_minus_max_num_merge_cand */
1826         PUT_BITS_UEV(ps_bitstrm, 5 - ps_slice_hdr->i1_max_num_merge_cand, return_status);
1827         ENTROPY_TRACE("five_minus_max_num_merge_cand", 5 - ps_slice_hdr->i1_max_num_merge_cand);
1828 #endif
1829 
1830         /* slice_qp_delta */
1831         PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp_delta, return_status);
1832         ENTROPY_TRACE("slice_qp_delta", ps_slice_hdr->i1_slice_qp_delta);
1833 
1834         if(ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag)
1835         {
1836             /* slice_cb_qp_offset */
1837             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_cb_qp_offset, return_status);
1838             ENTROPY_TRACE("slice_cb_qp_offset", ps_slice_hdr->i1_slice_cb_qp_offset);
1839 
1840             /* slice_cr_qp_offset */
1841             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_cr_qp_offset, return_status);
1842             ENTROPY_TRACE("slice_cr_qp_offset", ps_slice_hdr->i1_slice_cr_qp_offset);
1843         }
1844 
1845         if(ps_pps->i1_deblocking_filter_control_present_flag)
1846         {
1847             if(ps_pps->i1_deblocking_filter_override_enabled_flag)
1848             {
1849                 /* deblocking_filter_override_flag */
1850                 PUT_BITS(
1851                     ps_bitstrm, ps_slice_hdr->i1_deblocking_filter_override_flag, 1, return_status);
1852                 ENTROPY_TRACE(
1853                     "deblocking_filter_override_flag",
1854                     ps_slice_hdr->i1_deblocking_filter_override_flag);
1855             }
1856 
1857             if(ps_slice_hdr->i1_deblocking_filter_override_flag)
1858             {
1859                 /* slice_disable_deblocking_filter_flag */
1860                 PUT_BITS(
1861                     ps_bitstrm,
1862                     ps_slice_hdr->i1_slice_disable_deblocking_filter_flag,
1863                     1,
1864                     return_status);
1865                 ENTROPY_TRACE(
1866                     "slice_disable_deblocking_filter_flag",
1867                     ps_slice_hdr->i1_slice_disable_deblocking_filter_flag);
1868 
1869                 if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
1870                 {
1871                     /* beta_offset_div2 */
1872                     PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_beta_offset_div2 >> 1, return_status);
1873                     ENTROPY_TRACE("beta_offset_div2", ps_slice_hdr->i1_beta_offset_div2 >> 1);
1874 
1875                     /* tc_offset_div2 */
1876                     PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_tc_offset_div2 >> 1, return_status);
1877                     ENTROPY_TRACE("tc_offset_div2", ps_slice_hdr->i1_tc_offset_div2 >> 1);
1878                 }
1879             }
1880         }
1881 
1882         disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
1883                                          ps_pps->i1_pic_disable_deblocking_filter_flag;
1884 
1885         if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
1886            (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
1887             !disable_deblocking_filter_flag))
1888         {
1889             /* slice_loop_filter_across_slices_enabled_flag */
1890             PUT_BITS(
1891                 ps_bitstrm,
1892                 ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag,
1893                 1,
1894                 return_status);
1895             ENTROPY_TRACE(
1896                 "slice_loop_filter_across_slices_enabled_flag",
1897                 ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag);
1898         }
1899     }
1900 
1901     if((ps_pps->i1_tiles_enabled_flag) || (ps_pps->i1_entropy_coding_sync_enabled_flag))
1902     {
1903         /* num_entry_point_offsets */
1904         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i4_num_entry_point_offsets, return_status);
1905         ENTROPY_TRACE("num_entry_point_offsets", ps_slice_hdr->i4_num_entry_point_offsets);
1906 
1907         /*copy the bitstream state at this stage, later once all the offset are known the duplicated state is used to write offset in bitstream*/
1908         memcpy(ps_dup_bit_strm_ent_offset, ps_bitstrm, sizeof(bitstrm_t));
1909 
1910         if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
1911         {
1912             /* offset_len_minus1 */
1913             PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
1914             ENTROPY_TRACE("offset_len_minus1", ps_slice_hdr->i1_offset_len - 1);
1915 
1916             /*check the bitstream offset here, the first offset will be fixed here based on num_entry_offset and maximum possible emulaiton prevention bytes*/
1917             /*This offset is used to generate bitstream, In the end of frame processing actual offset are updated and if there was no emulation bits the extra bytes
1918             shall be filled with 0xFF so that decoder discards it as part of slice header extension*/
1919 
1920             /*assume one byte of emulation preention for every offset we signal*/
1921             /*considering emulation prevention bytes and assuming incomplete word(4 bytes) that is yet to filled and offset length(4 bytes) that will be calc
1922             based on max offset length after frame is encoded*/
1923             pu4_first_slice_start_offset[0] =
1924                 ps_bitstrm->u4_strm_buf_offset +
1925                 ((ps_slice_hdr->i4_num_entry_point_offsets * ps_slice_hdr->i1_offset_len) >> 3) +
1926                 ps_slice_hdr->i4_num_entry_point_offsets + 4 + 4;
1927 
1928             ps_slice_hdr->pu4_entry_point_offset[0] = (*pu4_first_slice_start_offset);
1929 
1930             for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
1931             {
1932                 /* entry_point_offset[i] */
1933                 PUT_BITS(
1934                     ps_bitstrm,
1935                     ps_slice_hdr->pu4_entry_point_offset[i],
1936                     ps_slice_hdr->i1_offset_len,
1937                     return_status);
1938                 ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
1939             }
1940         }
1941     }
1942 
1943     if(ps_pps->i1_slice_header_extension_present_flag)
1944     {
1945         /* slice_header_extension_length */
1946         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i2_slice_header_extension_length, return_status);
1947         ENTROPY_TRACE(
1948             "slice_header_extension_length", ps_slice_hdr->i2_slice_header_extension_length);
1949 
1950         for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
1951         {
1952             /* slice_header_extension_data_byte[i] */
1953             PUT_BITS(ps_bitstrm, 0, 8, return_status);
1954             ENTROPY_TRACE("slice_header_extension_data_byte[i]", 0);
1955         }
1956     }
1957 
1958     BYTE_ALIGNMENT(ps_bitstrm);
1959 
1960     return return_status;
1961 }
1962 
1963 /**
1964 ******************************************************************************
1965 *
1966 *  @brief Populates vps structure
1967 *
1968 *  @par   Description
1969 *  All the parameters in vps are currently hard coded
1970 *
1971 *  @param[out]  ps_vps
1972 *  pointer to vps params that needs to be populated
1973 *
1974 *  @param[in]   ps_src_params
1975 *  pointer to source config params; resolution, frame rate etc
1976 *
1977 *  @param[in]   ps_out_strm_params
1978 *  pointer to output stream config params
1979 *
1980 *  @param[in]   ps_coding_params
1981 *  pointer to coding params; to enable/disable various toolsets in pps
1982 *
1983 *  @param[in]   ps_config_prms
1984 *  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
1985 *
1986 *
1987 *  @return      success or failure error code
1988 *
1989 ******************************************************************************
1990 */
ihevce_populate_vps(enc_ctxt_t * ps_enc_ctxt,vps_t * ps_vps,ihevce_src_params_t * ps_src_params,ihevce_out_strm_params_t * ps_out_strm_params,ihevce_coding_params_t * ps_coding_params,ihevce_config_prms_t * ps_config_prms,ihevce_static_cfg_params_t * ps_stat_cfg_prms,WORD32 i4_resolution_id)1991 WORD32 ihevce_populate_vps(
1992     enc_ctxt_t *ps_enc_ctxt,
1993     vps_t *ps_vps,
1994     ihevce_src_params_t *ps_src_params,
1995     ihevce_out_strm_params_t *ps_out_strm_params,
1996     ihevce_coding_params_t *ps_coding_params,
1997     ihevce_config_prms_t *ps_config_prms,
1998     ihevce_static_cfg_params_t *ps_stat_cfg_prms,
1999     WORD32 i4_resolution_id)
2000 {
2001     WORD8 *pi1_profile_compatiblity_flags;
2002     WORD32 i;
2003     WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2004     WORD32 i4_codec_level_index;
2005     ps_vps->i1_vps_id = DEFAULT_VPS_ID;
2006 
2007     (void)ps_config_prms;
2008     /* default sub layers is 1 */
2009     ps_vps->i1_vps_max_sub_layers = 1;
2010     if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_enable_temporal_scalability)
2011     {
2012         ps_vps->i1_vps_max_sub_layers = 2;
2013     }
2014 
2015     for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)
2016     {
2017         /* currently bit rate and pic rate signalling is disabled */
2018         ps_vps->ai1_bit_rate_info_present_flag[i] = 0;
2019         ps_vps->ai1_pic_rate_info_present_flag[i] = 0;
2020 
2021         if(ps_vps->ai1_bit_rate_info_present_flag[i])
2022         {
2023             /* TODO: Add support for bitrate and max bitrate */
2024             ps_vps->au2_avg_bit_rate[i] = 0;
2025             ps_vps->au2_max_bit_rate[i] = 0;
2026         }
2027 
2028         if(ps_vps->ai1_pic_rate_info_present_flag[i])
2029         {
2030             /* TODO: Add support for pic rate idc and avg pic rate */
2031         }
2032     }
2033 
2034     /* default sub layer ordering info present flag */
2035     ps_vps->i1_sub_layer_ordering_info_present_flag = VPS_SUB_LAYER_ORDERING_INFO_ABSENT;
2036 
2037     /* hrd and temporal id nesting not supported for now */
2038     ps_vps->i1_vps_num_hrd_parameters = 0;
2039 
2040     if(ps_vps->i1_vps_max_sub_layers == 1)
2041     {
2042         ps_vps->i1_vps_temporal_id_nesting_flag = 1;
2043     }
2044     else
2045     {
2046         ps_vps->i1_vps_temporal_id_nesting_flag = 0;
2047     }
2048 
2049     /* populate the general profile, tier and level information */
2050     ps_vps->s_ptl.s_ptl_gen.i1_profile_space = 0;  // BLU_RAY specific change is default
2051 
2052     /* set the profile according to user input */
2053     ps_vps->s_ptl.s_ptl_gen.i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2054 
2055     /***************************************************************/
2056     /* set the profile compatibility flag for current profile to 1 */
2057     /* the rest of the flags are set to 0                          */
2058     /***************************************************************/
2059     pi1_profile_compatiblity_flags = &ps_vps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[0];
2060 
2061     for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)  //TEMPORALA_SCALABILITY CHANGES
2062     {
2063         ps_vps->ai1_vps_max_dec_pic_buffering[i] =
2064             ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2065 
2066         ps_vps->ai1_vps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2067                                                   << i4_field_pic;
2068 
2069         ps_vps->ai1_vps_max_latency_increase[i] = 0;
2070 
2071         ps_vps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2072 
2073         ps_vps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2074 
2075         ps_vps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2076 
2077         ps_vps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2078 
2079         memset(
2080             ps_vps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2081             0,
2082             MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2083 
2084         ps_vps->s_ptl.as_ptl_sub[i]
2085             .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2086 
2087         ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2088             ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2089 
2090         if(0 == i)  // Only one level temporal scalability suport has been added.
2091         {
2092             i4_codec_level_index = ihevce_get_level_index(
2093                 ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2094 
2095             if(i4_codec_level_index)
2096                 i4_codec_level_index -= 1;
2097 
2098             ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2099                 (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2100         }
2101 
2102         ps_vps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2103 
2104         if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2105         {
2106             ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2107 
2108             ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2109         }
2110         else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2111         {
2112             ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2113 
2114             ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 1;
2115         }
2116         else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2117         {
2118             ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2119 
2120             ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2121         }
2122 
2123         ps_vps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2124             DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2125 
2126         if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2127         {
2128             ps_vps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag = 1;
2129         }
2130         else
2131         {
2132             ps_vps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2133                 DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2134         }
2135     }
2136 
2137     memset(pi1_profile_compatiblity_flags, 0, MAX_PROFILE_COMPATBLTY);
2138     pi1_profile_compatiblity_flags[ps_out_strm_params->i4_codec_profile] = 1;
2139 
2140     /* set the level idc according to user input */
2141     ps_vps->s_ptl.s_ptl_gen.u1_level_idc =
2142         ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2143 
2144     ps_vps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2145 
2146     if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2147     {
2148         ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2149 
2150         ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2151     }
2152     else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2153     {
2154         ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2155 
2156         ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 1;
2157     }
2158     else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2159     {
2160         ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2161 
2162         ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2163     }
2164 
2165     ps_vps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2166         DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2167 
2168     if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2169     {
2170         ps_vps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = 1;
2171     }
2172     else
2173     {
2174         ps_vps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2175     }
2176     if((ps_out_strm_params->i4_codec_profile == 4) &&
2177        (ps_src_params->i4_chr_format == IV_YUV_420SP_UV))
2178     {
2179         ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2180 
2181         ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2182 
2183         ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2184 
2185         ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2186 
2187         ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 1;
2188 
2189         ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2190 
2191         ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2192 
2193         ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2194 
2195         ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2196     }
2197     else if(
2198         (ps_out_strm_params->i4_codec_profile == 4) &&
2199         (ps_src_params->i4_chr_format == IV_YUV_422SP_UV))
2200     {
2201         ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2202 
2203         ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2204 
2205         ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2206 
2207         ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2208 
2209         ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2210 
2211         ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2212 
2213         ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2214 
2215         ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2216 
2217         ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2218     }
2219     else
2220     {
2221         ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2222 
2223         ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2224 
2225         ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2226 
2227         ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2228 
2229         ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2230 
2231         ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2232 
2233         ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2234 
2235         ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2236 
2237         ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2238     }
2239 
2240     ps_vps->i1_vps_max_nuh_reserved_zero_layer_id = 0;
2241 
2242     return IHEVCE_SUCCESS;
2243 }
2244 
2245 /**
2246 ******************************************************************************
2247 *
2248 *  @brief Populates sps structure
2249 *
2250 *  @par   Description
2251 *  Populates sps structure for its use in header generation
2252 *
2253 *  @param[out]  ps_sps
2254 *  pointer to sps params that needs to be populated
2255 *
2256 *  @param[in]   ps_vps
2257 *  pointer to vps params referred by the sps
2258 *
2259 *  @param[in]   ps_src_params
2260 *  pointer to source config params; resolution, frame rate etc
2261 *
2262 *  @param[in]   ps_out_strm_params
2263 *  pointer to output stream config params
2264 *
2265 *  @param[in]   ps_coding_params
2266 *  pointer to coding params; to enable/disable various toolsets in pps
2267 *
2268 *  @param[in]   ps_config_prms
2269 *  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
2270 *
2271 *  @return      success or failure error code
2272 *
2273 ******************************************************************************
2274 */
ihevce_populate_sps(enc_ctxt_t * ps_enc_ctxt,sps_t * ps_sps,vps_t * ps_vps,ihevce_src_params_t * ps_src_params,ihevce_out_strm_params_t * ps_out_strm_params,ihevce_coding_params_t * ps_coding_params,ihevce_config_prms_t * ps_config_prms,frm_ctb_ctxt_t * ps_frm_ctb_prms,ihevce_static_cfg_params_t * ps_stat_cfg_prms,WORD32 i4_resolution_id)2275 WORD32 ihevce_populate_sps(
2276     enc_ctxt_t *ps_enc_ctxt,
2277     sps_t *ps_sps,
2278     vps_t *ps_vps,
2279     ihevce_src_params_t *ps_src_params,
2280     ihevce_out_strm_params_t *ps_out_strm_params,
2281     ihevce_coding_params_t *ps_coding_params,
2282     ihevce_config_prms_t *ps_config_prms,
2283     frm_ctb_ctxt_t *ps_frm_ctb_prms,
2284     ihevce_static_cfg_params_t *ps_stat_cfg_prms,
2285     WORD32 i4_resolution_id)
2286 {
2287     WORD32 i;
2288     WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2289     WORD32 i4_quality_preset =
2290         ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;
2291     WORD32 i4_codec_level_index;
2292 
2293     if(i4_quality_preset == IHEVCE_QUALITY_P7)
2294     {
2295         i4_quality_preset = IHEVCE_QUALITY_P6;
2296     }
2297 
2298     ps_sps->i1_sps_id = DEFAULT_SPS_ID;
2299 
2300     if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_mres_single_out)
2301     {
2302         ps_sps->i1_sps_id = i4_resolution_id;
2303     }
2304 
2305     ps_sps->i1_vps_id = ps_vps->i1_vps_id;
2306 
2307     ps_sps->i2_pic_height_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_ht;
2308 
2309     ps_sps->i2_pic_width_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_wd;
2310 
2311     ps_sps->i1_amp_enabled_flag = AMP_ENABLED;
2312 
2313     ps_sps->i1_chroma_format_idc = (ps_src_params->i4_chr_format == IV_YUV_422SP_UV) ? 2 : 1;
2314 
2315     ps_sps->i1_separate_colour_plane_flag = 0;
2316 
2317     ps_sps->i1_bit_depth_luma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2318 
2319     ps_sps->i1_bit_depth_chroma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2320 
2321     ps_sps->i1_log2_min_coding_block_size = ps_config_prms->i4_min_log2_cu_size;
2322 
2323     ps_sps->i1_log2_diff_max_min_coding_block_size =
2324         ps_config_prms->i4_max_log2_cu_size - ps_config_prms->i4_min_log2_cu_size;
2325 
2326     ps_sps->i1_log2_ctb_size =
2327         ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
2328 
2329     ps_sps->i1_log2_diff_max_min_transform_block_size =
2330         ps_config_prms->i4_max_log2_tu_size - ps_config_prms->i4_min_log2_tu_size;
2331 
2332     ps_sps->i1_log2_min_transform_block_size = ps_config_prms->i4_min_log2_tu_size;
2333 
2334     ps_sps->i1_long_term_ref_pics_present_flag = LONG_TERM_REF_PICS_ABSENT;
2335 
2336     ps_sps->i1_max_transform_hierarchy_depth_inter = ps_config_prms->i4_max_tr_tree_depth_nI;
2337 
2338     ps_sps->i1_max_transform_hierarchy_depth_intra = ps_config_prms->i4_max_tr_tree_depth_I;
2339 
2340     ps_sps->i1_pcm_enabled_flag = PCM_DISABLED;
2341 
2342     ps_sps->i1_pcm_loop_filter_disable_flag = PCM_LOOP_FILTER_DISABLED;
2343 
2344     ps_sps->i1_pic_cropping_flag = !!ps_coding_params->i4_cropping_mode;
2345 
2346     if(i4_quality_preset < IHEVCE_QUALITY_P4)
2347     {
2348         /*** Enable SAO for PQ,HQ,MS presets **/
2349         ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_ENABLED;
2350     }
2351     else
2352     {
2353         ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2354     }
2355 #if DISABLE_SAO
2356     ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2357 #endif
2358 
2359     if(ps_coding_params->i4_use_default_sc_mtx == 1)
2360     {
2361         ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_ENABLED;
2362     }
2363     else
2364     {
2365         ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_DISABLED;
2366     }
2367 
2368     ps_sps->i1_sps_max_sub_layers = DEFAULT_SPS_MAX_SUB_LAYERS;
2369 
2370     if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_enable_temporal_scalability)
2371     {
2372         ps_sps->i1_sps_max_sub_layers = DEFAULT_SPS_MAX_SUB_LAYERS + 1;
2373     }
2374 
2375     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = SPS_SUB_LAYER_ORDERING_INFO_ABSENT;
2376 
2377     ps_sps->i1_sps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2378 
2379     if(ps_sps->i1_sps_max_sub_layers == 1)
2380     {
2381         ps_sps->i1_sps_temporal_id_nesting_flag = 1;  //NO_SPS_TEMPORAL_ID_NESTING_DONE;
2382     }
2383     else
2384     {
2385         ps_sps->i1_sps_temporal_id_nesting_flag = 0;  //NO_SPS_TEMPORAL_ID_NESTING_DONE;
2386     }
2387 
2388     /* short term and long term ref pic set not signalled in sps */
2389     ps_sps->i1_num_short_term_ref_pic_sets = 0;
2390     ps_sps->i1_long_term_ref_pics_present_flag = 0;
2391 
2392     ps_sps->i1_num_long_term_ref_pics_sps = 0;
2393     ps_sps->i1_sps_temporal_mvp_enable_flag = !DISABLE_TMVP;
2394 
2395     ps_sps->i1_strong_intra_smoothing_enable_flag = STRONG_INTRA_SMOOTHING_FLAG_ENABLE;
2396 
2397     ps_sps->i1_vui_parameters_present_flag = ps_out_strm_params->i4_vui_enable;
2398 
2399     /*required in generation of slice header*/
2400     ps_sps->i2_pic_ht_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_vert;
2401 
2402     ps_sps->i2_pic_wd_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_horz;
2403 
2404     ps_sps->i1_log2_max_pic_order_cnt_lsb = DEFAULT_LOG2_MAX_POC_LSB;
2405 
2406     if(ps_sps->i1_pic_cropping_flag)
2407     {
2408         WORD32 num_rows_to_pad_bottom =
2409             ps_sps->i2_pic_height_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_height;
2410         WORD32 num_rows_to_pad_right =
2411             ps_sps->i2_pic_width_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_width;
2412 
2413         ps_sps->i2_pic_crop_top_offset = DEFAULT_PIC_CROP_TOP_OFFSET;
2414 
2415         ps_sps->i2_pic_crop_left_offset = DEFAULT_PIC_CROP_LEFT_OFFSET;
2416 
2417         /* picture offsets should be signalled in terms of chroma unit */
2418         ps_sps->i2_pic_crop_bottom_offset = num_rows_to_pad_bottom >> 1;
2419 
2420         /* picture offsets should be signalled in terms of chroma unit */
2421         ps_sps->i2_pic_crop_right_offset = num_rows_to_pad_right >> 1;
2422     }
2423 
2424     for(i = 0; i < (ps_sps->i1_sps_max_sub_layers); i++)
2425     {
2426         ps_sps->ai1_sps_max_dec_pic_buffering[i] =
2427             ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2428 
2429         ps_sps->ai1_sps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2430                                                   << i4_field_pic;
2431 
2432         ps_sps->ai1_sps_max_latency_increase[i] = 0;
2433 
2434         ps_sps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2435 
2436         ps_sps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2437 
2438         ps_sps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2439 
2440         ps_sps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2441 
2442         memset(
2443             ps_sps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2444             0,
2445             MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2446 
2447         ps_sps->s_ptl.as_ptl_sub[i]
2448             .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2449 
2450         ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2451             ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2452 
2453         if(0 == i)  // Only one level temporal scalability suport has been added.
2454         {
2455             i4_codec_level_index = ihevce_get_level_index(
2456                 ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2457 
2458             if(i4_codec_level_index)
2459                 i4_codec_level_index -= 1;
2460 
2461             ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2462                 (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2463         }
2464         ps_sps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2465 
2466         if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2467         {
2468             ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2469 
2470             ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2471         }
2472         else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2473         {
2474             ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2475 
2476             ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 1;
2477         }
2478         else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2479         {
2480             ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2481 
2482             ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2483         }
2484 
2485         ps_sps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2486             DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2487 
2488         if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2489         {
2490             ps_sps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag = 1;
2491         }
2492         else
2493         {
2494             ps_sps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2495                 DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2496         }
2497         if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 1))
2498         {
2499             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 1;
2500 
2501             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2502 
2503             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2504 
2505             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 1;
2506 
2507             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 1;
2508 
2509             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2510 
2511             ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2512 
2513             ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2514 
2515             ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 1;
2516         }
2517         else if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 2))
2518         {
2519             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 1;
2520 
2521             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2522 
2523             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2524 
2525             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 1;
2526 
2527             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 0;
2528 
2529             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2530 
2531             ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2532 
2533             ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2534 
2535             ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 1;
2536         }
2537         else
2538         {
2539             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 0;
2540 
2541             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2542 
2543             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2544 
2545             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 0;
2546 
2547             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 0;
2548 
2549             ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2550 
2551             ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2552 
2553             ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2554 
2555             ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 0;
2556         }
2557     }
2558 
2559     memset(
2560         ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag,
2561         0,
2562         MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2563 
2564     /* populate the general profile, tier and level information */
2565     ps_sps->s_ptl.s_ptl_gen.i1_profile_space = 0;  // BLU_RAY specific change is default
2566 
2567     ps_sps->s_ptl.s_ptl_gen.i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2568 
2569     ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] =
2570         1;
2571 
2572     ps_sps->s_ptl.s_ptl_gen.u1_level_idc =
2573         ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2574 
2575     ps_sps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2576 
2577     if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2578     {
2579         ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2580 
2581         ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2582     }
2583     else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2584     {
2585         ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2586 
2587         ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 1;
2588     }
2589     else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2590     {
2591         ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2592 
2593         ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2594     }
2595 
2596     ps_sps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2597         DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2598 
2599     if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2600     {
2601         ps_sps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = 1;
2602     }
2603     else
2604     {
2605         ps_sps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2606     }
2607     if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 1))
2608     {
2609         ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2610 
2611         ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2612 
2613         ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2614 
2615         ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2616 
2617         ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 1;
2618 
2619         ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2620 
2621         ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2622 
2623         ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2624 
2625         ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2626     }
2627     else if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 2))
2628     {
2629         ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2630 
2631         ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2632 
2633         ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2634 
2635         ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2636 
2637         ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2638 
2639         ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2640 
2641         ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2642 
2643         ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2644 
2645         ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2646     }
2647     else
2648     {
2649         ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2650 
2651         ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2652 
2653         ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2654 
2655         ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2656 
2657         ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2658 
2659         ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2660 
2661         ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2662 
2663         ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2664 
2665         ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2666     }
2667 
2668     return IHEVCE_SUCCESS;
2669 }
2670 
2671 /**
2672 ******************************************************************************
2673 *
2674 *  @brief Populates pps structure based on input cofiguration params
2675 *
2676 *  @par   Description
2677 *  Populates pps structure for its use in header generation
2678 *
2679 *  @param[out]  ps_pps
2680 *  pointer to pps params structure which needs to be populated
2681 *
2682 *  @param[in]   ps_sps
2683 *  pointer to sps params refered by the pps
2684 *
2685 *  @param[in]   ps_src_params
2686 *  pointer to source config params; resolution, frame rate etc
2687 *
2688 *  @param[in]   ps_out_strm_params
2689 *  pointer to output stream config params
2690 *
2691 *  @param[in]   ps_coding_params
2692 *  pointer to coding params; to enable/disable various toolsets in pps
2693 *
2694 *  @param[in]   ps_config_prms
2695 *  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
2696 *
2697 *  @return      success or failure error code
2698 *
2699 ******************************************************************************
2700 */
ihevce_populate_pps(pps_t * ps_pps,sps_t * ps_sps,ihevce_src_params_t * ps_src_params,ihevce_out_strm_params_t * ps_out_strm_params,ihevce_coding_params_t * ps_coding_params,ihevce_config_prms_t * ps_config_prms,ihevce_static_cfg_params_t * ps_stat_cfg_prms,WORD32 i4_bitrate_instance_id,WORD32 i4_resolution_id,ihevce_tile_params_t * ps_tile_params_base,WORD32 * pi4_column_width_array,WORD32 * pi4_row_height_array)2701 WORD32 ihevce_populate_pps(
2702     pps_t *ps_pps,
2703     sps_t *ps_sps,
2704     ihevce_src_params_t *ps_src_params,
2705     ihevce_out_strm_params_t *ps_out_strm_params,
2706     ihevce_coding_params_t *ps_coding_params,
2707     ihevce_config_prms_t *ps_config_prms,
2708     ihevce_static_cfg_params_t *ps_stat_cfg_prms,
2709     WORD32 i4_bitrate_instance_id,
2710     WORD32 i4_resolution_id,
2711     ihevce_tile_params_t *ps_tile_params_base,
2712     WORD32 *pi4_column_width_array,
2713     WORD32 *pi4_row_height_array)
2714 {
2715     (void)ps_src_params;
2716     (void)ps_out_strm_params;
2717 
2718     ps_pps->i1_beta_offset_div2 = DEFAULT_BETA_OFFSET;
2719 
2720     ps_pps->i1_cabac_init_present_flag = CABAC_INIT_ABSENT;
2721 
2722     ps_pps->i1_constrained_intra_pred_flag = CONSTR_IPRED_DISABLED;
2723     /*delta qp can be disabled for constant qp mode to save on qp signalling bits*/
2724     ps_pps->i1_cu_qp_delta_enabled_flag = ps_config_prms->i4_cu_level_rc;
2725 
2726     ps_pps->i1_deblocking_filter_control_present_flag = DEBLOCKING_FILTER_CONTROL_PRESENT;
2727 
2728     ps_pps->i1_deblocking_filter_override_enabled_flag = DEBLOCKING_FILTER_OVERRIDE_DISABLED;
2729 
2730     ps_pps->i1_pic_disable_deblocking_filter_flag = ps_coding_params->i4_deblocking_type;
2731 
2732     if(0 != ps_stat_cfg_prms->s_slice_params.i4_slice_segment_mode)
2733     {
2734         ps_pps->i1_dependent_slice_enabled_flag = DEPENDENT_SLICE_ENABLED;
2735     }
2736     else
2737     {
2738         ps_pps->i1_dependent_slice_enabled_flag = DEPENDENT_SLICE_DISABLED;
2739     }
2740 
2741     /* Assign the diff_cu_qp_delta_depth with 3,2,1 for making
2742     CU_LEVEL_QP_MODULATION limited to 8x8, 16x16, 32x32 respectively : Lokesh */
2743     ps_pps->i1_diff_cu_qp_delta_depth = CU_LEVEL_QP_LIMIT_8x8;
2744 
2745     if(1 == ps_coding_params->i4_enable_entropy_sync)
2746     {
2747         ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_ENABLED;
2748     }
2749     else
2750     {
2751         ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_DISABLED;
2752     }
2753 
2754     ps_pps->i1_entropy_slice_enabled_flag = ENTROPY_SLICE_DISABLED;
2755 
2756     ps_pps->i1_lists_modification_present_flag = ps_coding_params->i4_weighted_pred_enable;
2757 
2758     ps_pps->i1_log2_parallel_merge_level = DEFAULT_PARALLEL_MERGE_LEVEL;
2759 
2760     ps_pps->i1_num_extra_slice_header_bits = 0;
2761 
2762     /* SAO_note_01: Currently SAO is implemented is such a way that the
2763     loop-filter has to be enabled across syntatical-tiles and slices.
2764     Search for <SAO_note_01> in workspace to know more */
2765     ps_pps->i1_loop_filter_across_slices_enabled_flag = LF_ACROSS_SLICES_ENABLED;
2766 
2767     ps_pps->i1_num_ref_idx_l0_default_active = DEFAULT_NUM_REF_IDX_L0_DEFAULT_ACTIVE;
2768 
2769     ps_pps->i1_num_ref_idx_l1_default_active = DEFAULT_NUM_REF_IDX_L1_DEFAULT_ACTIVE;
2770 
2771     if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2772     {
2773         ps_pps->i1_num_tile_columns = NUM_TILES_COLS;
2774 
2775         ps_pps->i1_num_tile_rows = NUM_TILES_ROWS;
2776 
2777         ps_pps->i1_tiles_enabled_flag = TILES_DISABLED;
2778 
2779         ps_pps->i1_uniform_spacing_flag = SPACING_IS_UNIFORM;
2780     }
2781     else
2782     {
2783         ps_pps->i1_num_tile_columns = ps_tile_params_base->i4_num_tile_cols;
2784 
2785         ps_pps->i1_num_tile_rows = ps_tile_params_base->i4_num_tile_rows;
2786 
2787         ps_pps->i1_tiles_enabled_flag = TILES_ENABLED;
2788 
2789         ps_pps->i1_uniform_spacing_flag = ps_tile_params_base->i4_uniform_spacing_flag;
2790 
2791         if(SPACING_IS_NONUNIFORM == ps_pps->i1_uniform_spacing_flag)
2792         {
2793             WORD32 i4_i;
2794             for(i4_i = 0; i4_i < ps_tile_params_base->i4_num_tile_cols; i4_i++)
2795             {
2796                 ps_pps->ps_tile[i4_i].u2_wd = pi4_column_width_array[i4_i] >>
2797                                               ps_config_prms->i4_max_log2_cu_size;
2798             }
2799             for(i4_i = 0; i4_i < ps_tile_params_base->i4_num_tile_rows; i4_i++)
2800             {
2801                 ps_pps->ps_tile[i4_i].u2_ht = pi4_row_height_array[i4_i] >>
2802                                               ps_config_prms->i4_max_log2_cu_size;
2803             }
2804         }
2805     }
2806 
2807     /* SAO_note_01: Currently SAO is implemented is such a way that the
2808     loop-filter has to be enabled across syntatical-tiles and slices.
2809     Search for <SAO_note_01> in workspace to know more */
2810     if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2811     {
2812         ps_pps->i1_loop_filter_across_tiles_enabled_flag = 1;
2813     }
2814     else
2815     {
2816         ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2817     }
2818 
2819     ps_pps->i1_output_flag_present_flag = OUTPUT_FLAG_ABSENT;
2820 
2821     ps_pps->i1_pic_cb_qp_offset = DEFAULT_PIC_CB_QP_OFFSET;
2822 
2823     ps_pps->i1_pic_cr_qp_offset = DEFAULT_PIC_CR_QP_OFFSET;
2824 
2825     /*init qp is different for each bit-rate instance */
2826     ps_pps->i1_pic_init_qp = CLIP3(
2827         ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2828             .ai4_frame_qp[i4_bitrate_instance_id],
2829         ps_config_prms->i4_min_frame_qp,
2830         ps_config_prms->i4_max_frame_qp);
2831 
2832     /* enable chroma QP offset only if stasino or psy rd is present */
2833     if(((ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER)) &&
2834         ((ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION)) ||
2835          (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_1)) ||
2836          (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_2)) ||
2837          (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_3)))))
2838     {
2839         ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag =
2840             SLICE_LEVEL_CHROMA_QP_OFFSETS_PRESENT;
2841     }
2842     else
2843     {
2844         ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag =
2845             SLICE_LEVEL_CHROMA_QP_OFFSETS_ABSENT;
2846     }
2847 
2848     ps_pps->i1_pps_id = DEFAULT_PPS_ID;
2849 
2850     if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_mres_single_out)
2851     {
2852         ps_pps->i1_pps_id = i4_resolution_id;
2853     }
2854 
2855     ps_pps->i1_pps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2856 
2857     if(ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset <
2858        IHEVCE_QUALITY_P3)
2859     {
2860         ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_HIDDEN;
2861     }
2862     else if(
2863         ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset ==
2864         IHEVCE_QUALITY_P3)
2865     {
2866         ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2867     }
2868     else
2869     {
2870         ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2871     }
2872 
2873 #if DISABLE_SBH
2874     ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2875 #endif
2876 
2877     ps_pps->i1_slice_extension_present_flag = SLICE_EXTENSION_ABSENT;
2878 
2879     ps_pps->i1_slice_header_extension_present_flag = SLICE_HEADER_EXTENSION_ABSENT;
2880 
2881     ps_pps->i1_sps_id = ps_sps->i1_sps_id;
2882 
2883     ps_pps->i1_tc_offset_div2 = DEFAULT_TC_OFFSET;
2884 
2885     ps_pps->i1_transform_skip_enabled_flag = TRANSFORM_SKIP_DISABLED;
2886 
2887     ps_pps->i1_transquant_bypass_enable_flag = TRANSFORM_BYPASS_DISABLED;
2888 
2889     ps_pps->i1_weighted_bipred_flag = ps_coding_params->i4_weighted_pred_enable;
2890 
2891     ps_pps->i1_weighted_pred_flag = ps_coding_params->i4_weighted_pred_enable;
2892 
2893     return IHEVCE_SUCCESS;
2894 }
2895 
2896 /**
2897 ******************************************************************************
2898 *
2899 *  @brief Populates slice header structure
2900 *
2901 *  @par   Description
2902 *  Populates slice header structure for its use in header generation
2903 *
2904 *  @param[out]  ps_slice_hdr
2905 *  pointer to slice header structure that needs to be populated
2906 *
2907 *  @param[in]  ps_pps
2908 *  pointer to pps params structure refered by the slice
2909 *
2910 *  @param[in]   ps_sps
2911 *  pointer to sps params refered by the pps
2912 *
2913 *  @param[in]   nal_unit_type
2914 *  nal unit type for current slice
2915 *
2916 *  @param[in]   slice_type
2917 *  current slice type
2918 *
2919 *  @param[in]   ctb_x
2920 *  x offset of first ctb in current slice (ctb units)
2921 *
2922 *  @param[in]   ctb_y
2923 *  y offset of first ctb in current slice (ctb units)
2924 *
2925 *  @param[in]   poc
2926 *  pic order count for current slice (shall be 0 for IDR pics)
2927 *
2928 *  @param[in]   cur_slice_qp
2929 *  qp for the current slice
2930 *
2931 *  @return      success or failure error code
2932 *
2933 ******************************************************************************
2934 */
ihevce_populate_slice_header(slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps,WORD32 nal_unit_type,WORD32 slice_type,WORD32 ctb_x,WORD32 ctb_y,WORD32 poc,WORD32 cur_slice_qp,WORD32 max_merge_candidates,WORD32 i4_rc_pass_num,WORD32 i4_quality_preset,WORD32 stasino_enabled)2935 WORD32 ihevce_populate_slice_header(
2936     slice_header_t *ps_slice_hdr,
2937     pps_t *ps_pps,
2938     sps_t *ps_sps,
2939     WORD32 nal_unit_type,
2940     WORD32 slice_type,
2941     WORD32 ctb_x,
2942     WORD32 ctb_y,
2943     WORD32 poc,
2944     WORD32 cur_slice_qp,
2945     WORD32 max_merge_candidates,
2946     WORD32 i4_rc_pass_num,
2947     WORD32 i4_quality_preset,
2948     WORD32 stasino_enabled)
2949 {
2950     WORD32 i;
2951     WORD32 return_status = IHEVCE_SUCCESS;
2952     WORD32 RapPicFlag = (nal_unit_type >= NAL_BLA_W_LP) && (nal_unit_type <= NAL_RSV_RAP_VCL23);
2953 
2954     WORD32 idr_pic_flag = (NAL_IDR_W_LP == nal_unit_type) || (NAL_IDR_N_LP == nal_unit_type);
2955 
2956     WORD32 disable_deblocking_filter_flag;
2957 
2958     (void)ctb_x;
2959     (void)ctb_y;
2960     /* first_slice_in_pic_flag  */
2961     if(i4_quality_preset == IHEVCE_QUALITY_P7)
2962     {
2963         i4_quality_preset = IHEVCE_QUALITY_P6;
2964     }
2965 
2966     if(RapPicFlag)
2967     {
2968         /* no_output_of_prior_pics_flag */ /* TODO:revisit this */
2969         ps_slice_hdr->i1_no_output_of_prior_pics_flag = 0;  //BLU_RAY specific already done
2970     }
2971 
2972     /* pic_parameter_set_id */
2973     ps_slice_hdr->i1_pps_id = ps_pps->i1_pps_id;
2974 
2975     {
2976         /* This i1_dependent_slice_flag will further be updated in generate_slice_header() function */
2977         ps_slice_hdr->i1_dependent_slice_flag = 0;
2978     }
2979 
2980     if(!ps_slice_hdr->i1_dependent_slice_flag)
2981     {
2982         /* slice_type */
2983         ps_slice_hdr->i1_slice_type = (WORD8)slice_type;
2984 
2985         if(ps_pps->i1_output_flag_present_flag)
2986         {
2987             /* pic_output_flag */ /* TODO:revisit this */
2988             ps_slice_hdr->i1_pic_output_flag = 0;
2989         }
2990 
2991         /* separate colour plane flag not supported in this encoder */
2992         ASSERT(0 == ps_sps->i1_separate_colour_plane_flag);
2993 
2994         if(!idr_pic_flag)
2995         {
2996             WORD32 log2_max_poc_lsb = ps_sps->i1_log2_max_pic_order_cnt_lsb;
2997 
2998             /* pic_order_cnt_lsb */
2999             ps_slice_hdr->i4_pic_order_cnt_lsb = poc & ((1 << log2_max_poc_lsb) - 1);
3000 
3001             /* short_term_ref_pic_set_sps_flag */
3002             /* TODO : revisit this */
3003             ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag = 0;
3004 
3005             if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
3006             {
3007                 /* TODO: To populate short term ref pic set for this slice   */
3008             }
3009 
3010             /* long term ref pic flag not supported */
3011             ASSERT(0 == ps_sps->i1_long_term_ref_pics_present_flag);
3012             if(ps_sps->i1_long_term_ref_pics_present_flag)
3013             {
3014                 /* TODO : not supported */
3015             }
3016         }
3017 
3018         //ASSERT(0 == ps_sps->i1_sample_adaptive_offset_enabled_flag);
3019         if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
3020         {
3021             /* slice_sao_luma_flag */
3022             ps_slice_hdr->i1_slice_sao_luma_flag = 1;
3023             ps_slice_hdr->i1_slice_sao_chroma_flag = 1;
3024         }
3025 
3026 #if DISABLE_LUMA_SAO
3027         ps_slice_hdr->i1_slice_sao_luma_flag = 0;
3028 #endif
3029 
3030 #if DISABLE_CHROMA_SAO
3031         ps_slice_hdr->i1_slice_sao_chroma_flag = 0;
3032 #endif
3033         if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
3034         {
3035             /* TODO: currently temporal mvp disabled, need to enable later */
3036             if(1 == ps_sps->i1_sps_temporal_mvp_enable_flag)
3037             {
3038                 ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 1;
3039             }
3040             else
3041             {
3042                 ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 0;
3043             }
3044 
3045             /* num_ref_idx_active_override_flag */
3046             ps_slice_hdr->i1_num_ref_idx_active_override_flag = 0;
3047 
3048             if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
3049             {
3050                 /* TODO revisit this*/
3051                 /* i1_num_ref_idx_l0_active_minus1 */
3052                 ps_slice_hdr->i1_num_ref_idx_l0_active = 1;
3053 
3054                 if(BSLICE == ps_slice_hdr->i1_slice_type)
3055                 {
3056                     /* i1_num_ref_idx_l1_active */
3057                     /* TODO revisit this*/
3058                     ps_slice_hdr->i1_num_ref_idx_l1_active = 1;
3059                 }
3060             }
3061 
3062             if(BSLICE == ps_slice_hdr->i1_slice_type)
3063             {
3064                 /* mvd_l1_zero_flag */
3065                 ps_slice_hdr->i1_mvd_l1_zero_flag = 0;
3066             }
3067 
3068             {
3069                 /* cabac_init_flag curently set to 0 */
3070                 ps_slice_hdr->i1_cabac_init_flag = ps_pps->i1_cabac_init_present_flag ? 1 : 0;
3071             }
3072 
3073             if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
3074             {
3075                 if(BSLICE == ps_slice_hdr->i1_slice_type)
3076                 {
3077                     /* collocated_from_l0_flag */
3078                     ps_slice_hdr->i1_collocated_from_l0_flag = 0;
3079                 }
3080                 else if(PSLICE == ps_slice_hdr->i1_slice_type)
3081                 {
3082                     ps_slice_hdr->i1_collocated_from_l0_flag = 1;
3083                 }
3084 
3085                 if((ps_slice_hdr->i1_collocated_from_l0_flag &&
3086                     (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
3087                    (!ps_slice_hdr->i1_collocated_from_l0_flag &&
3088                     (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
3089                 {
3090                     /* collocated_ref_idx */
3091                     /* TODO revisit this*/
3092                     ps_slice_hdr->i1_collocated_ref_idx = 0;
3093                     //ps_slice_hdr->i1_num_ref_idx_l1_active - 1;
3094                 }
3095             }
3096         }
3097         ps_slice_hdr->i1_max_num_merge_cand = max_merge_candidates;
3098 
3099         /* TODO : revisit this */
3100         ps_slice_hdr->i1_slice_qp_delta = (WORD8)cur_slice_qp - ps_pps->i1_pic_init_qp;
3101 
3102         if(!ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag || !stasino_enabled)
3103         {
3104             /* slice_cb_qp_offset */
3105             ps_slice_hdr->i1_slice_cb_qp_offset = 0;
3106 
3107             /* slice_cr_qp_offset */
3108             ps_slice_hdr->i1_slice_cr_qp_offset = 0;
3109         }
3110         else /* only noisy regions have lower Chroma QP rating */
3111         {
3112             ps_slice_hdr->i1_slice_cb_qp_offset = -2;
3113             ps_slice_hdr->i1_slice_cr_qp_offset = -2;
3114         }
3115 
3116         if(ps_pps->i1_deblocking_filter_control_present_flag)
3117         {
3118             ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
3119 
3120             if(ps_pps->i1_deblocking_filter_override_enabled_flag)
3121             {
3122                 /* deblocking_filter_override_flag */
3123                 ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
3124             }
3125 
3126             if(ps_slice_hdr->i1_deblocking_filter_override_flag)
3127             {
3128                 /* slice_disable_deblocking_filter_flag */
3129                 ps_slice_hdr->i1_slice_disable_deblocking_filter_flag = DISABLE_DEBLOCKING_FLAG;
3130 
3131                 if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
3132                 {
3133                     /* beta_offset_div2 */
3134                     ps_slice_hdr->i1_beta_offset_div2 = 0;
3135 
3136                     /* tc_offset_div2 */
3137                     ps_slice_hdr->i1_tc_offset_div2 = 0;
3138                 }
3139             }
3140         }
3141 
3142         disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
3143                                          ps_pps->i1_pic_disable_deblocking_filter_flag;
3144 
3145         if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
3146            (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
3147             !disable_deblocking_filter_flag))
3148         {
3149             /* slice_loop_filter_across_slices_enabled_flag */
3150             ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = 1;
3151         }
3152     }
3153 
3154     if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
3155     {
3156         /* num_entry_point_offsets, same as NUM of ctb rows to enable entropy sync at start of every CTB */
3157         ps_slice_hdr->i4_num_entry_point_offsets = ps_sps->i2_pic_ht_in_ctb - 1;
3158 
3159         if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3160         {
3161             /* generate offset_len here */
3162             /* fixing the offset lenght assuming 4kx2k is log2(w * h / num_ctb_row) = 20*/
3163             ps_slice_hdr->i1_offset_len = 24;
3164         }
3165     }
3166     else
3167     {
3168         ps_slice_hdr->i4_num_entry_point_offsets = 0;
3169         ps_slice_hdr->i1_offset_len = 0;
3170     }
3171 
3172     /* slice_header_extension_present_flag not supported */
3173     //if(ps_pps->i1_slice_header_extension_present_flag)
3174     {
3175         /* slice_header_extension_length */
3176         ps_slice_hdr->i2_slice_header_extension_length = 0;
3177 
3178         for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
3179         {
3180             /* slice_header_extension_data_byte[i] */
3181         }
3182     }
3183 
3184     /* TODO : hard coding ref pix set for now                           */
3185     /* Need to update this once the ref pics are known from lap output  */
3186 
3187     /*  NOTE                                                            */
3188     /* inter ref pic prediction is too much of logic for few bit savings*/
3189     /* at slice header level this is not supported by the encoder       */
3190     ps_slice_hdr->s_stref_picset.i1_inter_ref_pic_set_prediction_flag = 0;
3191 
3192     /* hardcoding 1 ref pic for now ..... will be updated base on lap output */
3193     ps_slice_hdr->s_stref_picset.i1_num_delta_pocs = 1;
3194     ps_slice_hdr->s_stref_picset.i1_num_neg_pics = 1;
3195     ps_slice_hdr->s_stref_picset.i1_num_pos_pics = 0;
3196 
3197     memset(
3198         ps_slice_hdr->s_stref_picset.ai2_delta_poc,
3199         0,
3200         MAX_DPB_SIZE * sizeof(*ps_slice_hdr->s_stref_picset.ai2_delta_poc));
3201     ps_slice_hdr->s_stref_picset.ai2_delta_poc[0] = 1;
3202 
3203     return return_status;
3204 }
3205 
3206 /**
3207 ******************************************************************************
3208 *
3209 *  @brief Insert entry point offset
3210 *
3211 *  @par   Description
3212 *  Insert entry point offset in slice header after frame processing is done 7.3.5.1
3213 *
3214 *  @param[inout]   ps_bitstrm
3215 *  pointer to bitstream context for generating slice header
3216 *
3217 *  @param[in]   i1_nal_unit_type
3218 *  nal unit type
3219 *
3220 *  @param[in]   ps_slice_hdr
3221 *  pointer to slice header params
3222 *
3223 *  @param[in]   ps_pps
3224 *  pointer to pps params referred by slice
3225 *
3226 *  @param[in]   ps_sps
3227 *  pointer to sps params referred by slice
3228 *
3229 *  @return      success or failure error code
3230 *
3231 ******************************************************************************
3232 */
ihevce_insert_entry_offset_slice_header(bitstrm_t * ps_bitstrm,slice_header_t * ps_slice_hdr,pps_t * ps_pps,UWORD32 u4_first_slice_start_offset)3233 WORD32 ihevce_insert_entry_offset_slice_header(
3234     bitstrm_t *ps_bitstrm,
3235     slice_header_t *ps_slice_hdr,
3236     pps_t *ps_pps,
3237     UWORD32 u4_first_slice_start_offset)
3238 {
3239     WORD32 i;
3240     WORD32 return_status = IHEVCE_SUCCESS;
3241     UWORD32 max_offset = 0, offset_len = 0, num_bytes_shift = 0;
3242     /*entire slice data has to be shifted*/
3243     num_bytes_shift =
3244         ps_slice_hdr->pu4_entry_point_offset[ps_slice_hdr->i4_num_entry_point_offsets + 1] -
3245         ps_slice_hdr->pu4_entry_point_offset[0];
3246     /*generate relative offset*/
3247     for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3248     {
3249         ps_slice_hdr->pu4_entry_point_offset[i] =
3250             ps_slice_hdr->pu4_entry_point_offset[i + 1] - ps_slice_hdr->pu4_entry_point_offset[i];
3251         if(ps_slice_hdr->pu4_entry_point_offset[i] > (WORD32)max_offset)
3252         {
3253             max_offset = ps_slice_hdr->pu4_entry_point_offset[i];
3254         }
3255     }
3256     while(1)
3257     {
3258         if(max_offset & 0x80000000)
3259         {
3260             break;
3261         }
3262         max_offset <<= 1;
3263         offset_len++;
3264     }
3265     offset_len = 32 - offset_len;
3266     ps_slice_hdr->i1_offset_len = offset_len;
3267 
3268     if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3269     {
3270         /* offset_len_minus1 */
3271         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
3272         ENTROPY_TRACE("offset_len_minus1", ps_slice_hdr->i1_offset_len - 1);
3273     }
3274 
3275     for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3276     {
3277         /* entry_point_offset[i] */
3278         /* entry point offset minus1 is indicated in 10.0 */
3279         PUT_BITS(
3280             ps_bitstrm,
3281             ps_slice_hdr->pu4_entry_point_offset[i] - 1,
3282             ps_slice_hdr->i1_offset_len,
3283             return_status);
3284         ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
3285     }
3286 
3287     if(ps_pps->i1_slice_header_extension_present_flag)
3288     {
3289         /* slice_header_extension_length */
3290         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i2_slice_header_extension_length, return_status);
3291         ENTROPY_TRACE(
3292             "slice_header_extension_length", ps_slice_hdr->i2_slice_header_extension_length);
3293         /*calculate slice header extension length to fill in the gap*/
3294 
3295         for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
3296         {
3297             /* slice_header_extension_data_byte[i] */
3298             PUT_BITS(ps_bitstrm, 0xFF, 8, return_status);
3299             ENTROPY_TRACE("slice_header_extension_data_byte[i]", 0);
3300         }
3301     }
3302 
3303     BYTE_ALIGNMENT(ps_bitstrm);
3304 
3305     ASSERT(num_bytes_shift > 0);
3306     /* copy the bitstream to point where header data has ended*/
3307     memmove(
3308         (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset),
3309         (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + u4_first_slice_start_offset),
3310         num_bytes_shift);
3311 
3312     /*send feedback of actual bytes generated*/
3313     ps_bitstrm->u4_strm_buf_offset += num_bytes_shift;
3314 
3315     //ASSERT(ps_bitstrm->u4_strm_buf_offset == u4_first_slice_start_offset);
3316     return return_status;
3317 }
3318