1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 *******************************************************************************
23 * @file
24 *  ih264e_encode_header.c
25 *
26 * @brief
27 *  This file contains function definitions related to header encoding.
28 *
29 * @author
30 *  ittiam
31 *
32 * @par List of Functions:
33 *  - ih264e_generate_nal_unit_header()
34 *  - ih264e_generate_sps()
35 *  - ih264e_generate_pps()
36 *  - ih264e_generate_slice_header()
37 *  - ih264e_get_level()
38 *  - ih264e_populate_sps()
39 *  - ih264e_populate_pps()
40 *  - ih264e_populate_slice_header()
41 *  - ih264e_add_filler_nal_unit()
42 *
43 * @remarks
44 *  None
45 *
46 *******************************************************************************
47 */
48 
49 /*****************************************************************************/
50 /* File Includes                                                             */
51 /*****************************************************************************/
52 
53 /* System include files */
54 #include <stdio.h>
55 #include <stddef.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <assert.h>
59 
60 /* User Include Files */
61 #include "ih264_typedefs.h"
62 #include "iv2.h"
63 #include "ive2.h"
64 #include "ih264e.h"
65 #include "ithread.h"
66 #include "ih264e_config.h"
67 #include "ih264e_trace.h"
68 #include "ih264e_error.h"
69 #include "ih264e_bitstream.h"
70 #include "ih264_debug.h"
71 #include "ih264_defs.h"
72 #include "ime_distortion_metrics.h"
73 #include "ime_defs.h"
74 #include "ime_structs.h"
75 #include "ih264_error.h"
76 #include "ih264_structs.h"
77 #include "ih264_trans_quant_itrans_iquant.h"
78 #include "ih264_inter_pred_filters.h"
79 #include "ih264_mem_fns.h"
80 #include "ih264_padding.h"
81 #include "ih264_intra_pred_filters.h"
82 #include "ih264_deblk_edge_filters.h"
83 #include "ih264_cabac_tables.h"
84 #include "ih264e_defs.h"
85 #include "irc_cntrl_param.h"
86 #include "irc_frame_info_collector.h"
87 #include "ih264e_rate_control.h"
88 #include "ih264e_cabac_structs.h"
89 #include "ih264e_structs.h"
90 #include "ih264e_encode_header.h"
91 #include "ih264_common_tables.h"
92 #include "ih264_macros.h"
93 #include "ih264e_utils.h"
94 
95 
96 /*****************************************************************************/
97 /* Function Definitions                                                      */
98 /*****************************************************************************/
99 
100 /**
101 ******************************************************************************
102 *
103 * @brief Generate nal unit header in the stream as per section 7.4.1
104 *
105 * @par   Description
106 *  Inserts Nal unit header syntax as per section 7.4.1
107 *
108 * @param[inout]   ps_bitstrm
109 *  pointer to bitstream context (handle)
110 *
111 * @param[in]   nal_unit_type
112 *  nal type to be inserted
113 *
114 * @param[in]   nal_ref_idc
115 *  nal ref idc to be inserted
116 *
117 * @return      success or failure error code
118 *
119 ******************************************************************************
120 */
ih264e_generate_nal_unit_header(bitstrm_t * ps_bitstrm,WORD32 nal_unit_type,WORD32 nal_ref_idc)121 static WORD32 ih264e_generate_nal_unit_header(bitstrm_t *ps_bitstrm,
122                                               WORD32 nal_unit_type,
123                                               WORD32 nal_ref_idc)
124 {
125     WORD32 return_status = IH264E_SUCCESS;
126 
127     /* sanity checks */
128     ASSERT((nal_unit_type > 0) && (nal_unit_type < 32));
129 
130     /* forbidden_zero_bit + nal_ref_idc + nal_unit_type */
131     PUT_BITS(ps_bitstrm,
132              ((nal_ref_idc << 5) + nal_unit_type),
133              (1+2+5), /*1 forbidden zero bit + 2 nal_ref_idc + 5 nal_unit_type */
134              return_status,
135              "nal_unit_header");
136 
137     return(return_status);
138 }
139 
140 /**
141 ******************************************************************************
142 *
143 * @brief Generates SPS (Sequence Parameter Set)
144 *
145 * @par   Description
146 *  This function generates Sequence Parameter Set header as per the spec
147 *
148 * @param[in]   ps_bitstrm
149 *  pointer to bitstream context (handle)
150 *
151 * @param[in]   ps_sps
152 *  pointer to structure containing SPS data
153 *
154 * @return      success or failure error code
155 *
156 ******************************************************************************
157 */
ih264e_generate_sps(bitstrm_t * ps_bitstrm,sps_t * ps_sps)158 WORD32 ih264e_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps)
159 {
160     WORD32 return_status = IH264E_SUCCESS;
161     WORD32 i;
162     WORD8  i1_nal_unit_type = 7;
163     WORD8  i1_nal_ref_idc = 3;
164 
165     /* Insert Start Code */
166     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
167 
168     /* Insert Nal Unit Header */
169     return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, i1_nal_unit_type, i1_nal_ref_idc);
170 
171     /* profile_idc */
172     PUT_BITS(ps_bitstrm, ps_sps->u1_profile_idc, 8, return_status, "profile_idc");
173 
174     /* constrained_set_flags */
175     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set0_flag, 1, return_status, "constrained_set0_flag");
176     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set1_flag, 1, return_status, "constrained_set1_flag");
177     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set2_flag, 1, return_status, "constrained_set2_flag");
178     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set3_flag, 1, return_status, "constrained_set3_flag");
179 
180     /* reserved_zero_four_bits */
181     PUT_BITS(ps_bitstrm, 0, 4, return_status, "reserved_zero_four_bits");
182 
183     /* level_idc */
184     PUT_BITS(ps_bitstrm, ps_sps->u1_level_idc, 8, return_status, "level_idc");
185 
186     /* seq_parameter_set_id */
187     PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_sps_id, return_status, "seq_parameter_set_id");
188 
189     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
190     {
191         /* chroma_format_idc */
192         PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_chroma_format_idc, return_status, "chroma_format_idc");
193 
194         if (ps_sps->u1_chroma_format_idc == CHROMA_FMT_IDC_YUV444)
195         {
196             /* i1_residual_colour_transform_flag */
197             PUT_BITS(ps_bitstrm, ps_sps->i1_residual_colour_transform_flag, 1, return_status, "i1_residual_colour_transform_flag");
198         }
199 
200         /* bit_depth_luma_minus8 */
201         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_luma - 8), return_status, "bit_depth_luma_minus8");
202 
203         /* bit_depth_chroma_minus8 */
204         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_chroma - 8), return_status, "bit_depth_chroma_minus8");
205 
206         /* qpprime_y_zero_transform_bypass_flag */
207         PUT_BITS(ps_bitstrm, ps_sps->i1_qpprime_y_zero_transform_bypass_flag, 1, return_status, "qpprime_y_zero_transform_bypass_flag");
208 
209         /* seq_scaling_matrix_present_flag */
210         PUT_BITS(ps_bitstrm, ps_sps->i1_seq_scaling_matrix_present_flag, 1, return_status, "seq_scaling_matrix_present_flag");
211 
212         /* seq_scaling_list */
213         if (ps_sps->i1_seq_scaling_matrix_present_flag)
214         {
215             /* TODO_LATER: Will be enabled once scaling list support is added */
216         }
217     }
218 
219     /* log2_max_frame_num_minus4 */
220     PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_frame_num - 4), return_status, "log2_max_frame_num_minus4");
221 
222     /* pic_order_cnt_type */
223     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_pic_order_cnt_type, return_status, "pic_order_cnt_type");
224 
225     if (ps_sps->i1_pic_order_cnt_type == 0)
226     {
227         /* log2_max_pic_order_cnt_lsb_minus4 */
228         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_pic_order_cnt_lsb - 4), return_status, "log2_max_pic_order_cnt_lsb_minus4");
229     }
230     else if (ps_sps->i1_pic_order_cnt_type == 1)
231     {
232         /* delta_pic_order_always_zero_flag */
233         PUT_BITS(ps_bitstrm, ps_sps->i1_delta_pic_order_always_zero_flag, 1, return_status, "delta_pic_order_always_zero_flag");
234 
235         /* offset_for_non_ref_pic */
236         PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_non_ref_pic, return_status, "offset_for_non_ref_pic");
237 
238         /* offset_for_top_to_bottom_field */
239         PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_top_to_bottom_field, return_status, "offset_for_top_to_bottom_field");
240 
241         /* num_ref_frames_in_pic_order_cnt_cycle */
242         PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle, return_status, "num_ref_frames_in_pic_order_cnt_cycle");
243 
244         /* Offset for ref frame */
245         for (i=0; i<ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
246         {
247             /* offset_for_ref_frame */
248             PUT_BITS_SEV(ps_bitstrm, ps_sps->ai4_offset_for_ref_frame[i], return_status, "offset_for_ref_frame");
249         }
250     }
251 
252     /* num_ref_frames */
253     PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_max_num_ref_frames, return_status, "num_ref_frames");
254 
255     /* gaps_in_frame_num_value_allowed_flag */
256     PUT_BITS(ps_bitstrm, ps_sps->i1_gaps_in_frame_num_value_allowed_flag, 1, return_status, "gaps_in_frame_num_value_allowed_flag");
257 
258     /* pic_width_in_mbs_minus1 */
259     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_mbs_minus1, return_status, "pic_width_in_mbs_minus1");
260 
261     /* pic_height_in_map_units_minus1 */
262     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_map_units_minus1, return_status, "pic_height_in_map_units_minus1");
263 
264     /* frame_mbs_only_flag */
265     PUT_BITS(ps_bitstrm, ps_sps->i1_frame_mbs_only_flag, 1, return_status, "frame_mbs_only_flag");
266 
267     if (!ps_sps->i1_frame_mbs_only_flag)
268     {
269         /* mb_adaptive_frame_field_flag */
270         PUT_BITS(ps_bitstrm, ps_sps->i1_mb_adaptive_frame_field_flag, 1, return_status, "mb_adaptive_frame_field_flag");
271     }
272 
273     /* direct_8x8_inference_flag */
274     PUT_BITS(ps_bitstrm, ps_sps->i1_direct_8x8_inference_flag, 1, return_status, "direct_8x8_inference_flag");
275 
276     /* frame_cropping_flag */
277     PUT_BITS(ps_bitstrm, ps_sps->i1_frame_cropping_flag, 1, return_status, "frame_cropping_flag");
278 
279     if (ps_sps->i1_frame_cropping_flag)
280     {
281         /* frame_crop_left_offset */
282         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_left_offset, return_status, "frame_crop_left_offset");
283 
284         /* frame_crop_right_offset */
285         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_right_offset, return_status, "frame_crop_right_offset");
286 
287         /* frame_crop_top_offset */
288         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_top_offset, return_status, "frame_crop_top_offset");
289 
290         /* frame_crop_bottom_offset */
291         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_bottom_offset, return_status, "frame_crop_bottom_offset");
292     }
293 
294     /* vui_parameters_present_flag */
295     PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status, "vui_parameters_present_flag");
296 
297     if (ps_sps->i1_vui_parameters_present_flag)
298     {
299         /* Add vui parameters to the bitstream */;
300     }
301 
302     /* rbsp trailing bits */
303     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
304 
305     return return_status;
306 }
307 
308 /**
309 ******************************************************************************
310 *
311 * @brief Generates PPS (Picture Parameter Set)
312 *
313 * @par   Description
314 *  Generate Picture Parameter Set as per Section 7.3.2.2
315 *
316 * @param[in]   ps_bitstrm
317 *  pointer to bitstream context (handle)
318 *
319 * @param[in]   ps_pps
320 *  pointer to structure containing PPS data
321 *
322 * @return      success or failure error code
323 *
324 ******************************************************************************
325 */
ih264e_generate_pps(bitstrm_t * ps_bitstrm,pps_t * ps_pps,sps_t * ps_sps)326 WORD32 ih264e_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps, sps_t *ps_sps)
327 {
328     WORD32 return_status = IH264E_SUCCESS;
329 
330     /* Insert the NAL start code */
331     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
332 
333     /* Insert Nal Unit Header */
334     PUT_BITS(ps_bitstrm, NAL_PPS_FIRST_BYTE, 8, return_status, "pps_header");
335 
336     /* pic_parameter_set_id */
337     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_pps_id, return_status, "pic_parameter_set_id");
338 
339     /* seq_parameter_set_id */
340     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_sps_id, return_status, "seq_parameter_set_id");
341 
342     /* Entropy coding : 0-VLC; 1 - CABAC */
343     PUT_BITS(ps_bitstrm, ps_pps->u1_entropy_coding_mode_flag, 1, return_status, "Entropy coding : 0-VLC; 1 - CABAC");
344 
345     /* Pic order present flag */
346     PUT_BITS(ps_bitstrm, ps_pps->u1_pic_order_present_flag, 1, return_status, "Pic order present flag");
347 
348     /* Number of slice groups */
349     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_num_slice_groups - 1, return_status, "Number of slice groups");
350 
351     if (ps_pps->u1_num_slice_groups > 1)
352     {
353         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
354          * If this is not the case, we have to add Slice group map type to the bit stream*/
355     }
356 
357     /* num_ref_idx_l0_default_active_minus1 */
358     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status, "num_ref_idx_l0_default_active_minus1");
359 
360     /* num_ref_idx_l1_default_active_minus1 */
361     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status, "num_ref_idx_l1_default_active_minus1");
362 
363     /* weighted_pred_flag */
364     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status, "weighted_pred_flag");
365 
366     /* weighted_bipred_flag */
367     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_idc, 2, return_status, "weighted_bipred_idc");
368 
369     /* pic_init_qp_minus26 */
370     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status, "pic_init_qp_minus26");
371 
372     /* pic_init_qs_minus26 */
373     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qs - 26, return_status, "pic_init_qs_minus26");
374 
375     /* chroma_qp_index_offset */
376     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_chroma_qp_index_offset, return_status, "chroma_qp_index_offset");
377 
378     /* deblocking_filter_control_present_flag */
379     PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status, "deblocking_filter_control_present_flag");
380 
381     /* constrained_intra_pred_flag */
382     PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status, "constrained_intra_pred_flag");
383 
384     /*redundant_pic_cnt_present_flag */
385     PUT_BITS(ps_bitstrm, ps_pps->i1_redundant_pic_cnt_present_flag, 1, return_status, "redundant_pic_cnt_present_flag");
386 
387     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
388     {
389         /* transform_8x8_mode_flag */
390         PUT_BITS(ps_bitstrm, ps_pps->i1_transform_8x8_mode_flag, 1, return_status, "transform_8x8_mode_flag");
391 
392         /* pic_scaling_matrix_present_flag */
393         PUT_BITS(ps_bitstrm, ps_pps->i1_pic_scaling_matrix_present_flag, 1, return_status, "pic_scaling_matrix_present_flag");
394 
395         if(ps_pps->i1_pic_scaling_matrix_present_flag)
396         {
397             /* TODO_LATER: Will be enabled once scaling list support is added */
398         }
399 
400         /* Second chroma QP offset */
401         PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_second_chroma_qp_index_offset, return_status, "Second chroma QP offset");
402     }
403 
404     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
405 
406     return return_status;
407 }
408 
409 /**
410 ******************************************************************************
411 *
412 * @brief Generates Slice Header
413 *
414 * @par   Description
415 *  Generate Slice Header as per Section 7.3.5.1
416 *
417 * @param[inout]   ps_bitstrm
418 *  pointer to bitstream context for generating slice header
419 *
420 * @param[in]   ps_slice_hdr
421 *  pointer to slice header params
422 *
423 * @param[in]   ps_pps
424 *  pointer to pps params referred by slice
425 *
426 * @param[in]   ps_sps
427 *  pointer to sps params referred by slice
428 *
429 * @param[out]   ps_dup_bit_strm_ent_offset
430 *  Bitstream struct to store bitstream state
431 *
432 * @param[out]   pu4_first_slice_start_offset
433 *  first slice offset is returned
434 *
435 * @return      success or failure error code
436 *
437 ******************************************************************************
438 */
ih264e_generate_slice_header(bitstrm_t * ps_bitstrm,slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps)439 WORD32 ih264e_generate_slice_header(bitstrm_t *ps_bitstrm,
440                                     slice_header_t *ps_slice_hdr,
441                                     pps_t *ps_pps,
442                                     sps_t *ps_sps)
443 {
444 
445     WORD32 return_status = IH264E_SUCCESS;
446 
447     /* Insert start code */
448     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
449 
450     /* Insert Nal Unit Header */
451     return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, ps_slice_hdr->i1_nal_unit_type, ps_slice_hdr->i1_nal_unit_idc);
452 
453     /* first_mb_in_slice */
454     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_first_mb_in_slice, return_status, "first_mb_in_slice");
455 
456     /* slice_type */
457     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_slice_type, return_status, "slice_type");
458 
459     /* pic_parameter_set_id */
460     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_pps_id, return_status, "pic_parameter_set_id");
461 
462     /* frame_num */
463     PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_frame_num, ps_sps->i1_log2_max_frame_num, return_status, "frame_num");
464 
465     if (!ps_sps->i1_frame_mbs_only_flag)
466     {
467         /* field_pic_flag */
468         PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_field_pic_flag, 1, return_status, "field_pic_flag");
469 
470         if(ps_slice_hdr->i1_field_pic_flag)
471         {
472             /* bottom_field_flag */
473             PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_bottom_field_flag, 1, return_status, "bottom_field_flag");
474         }
475     }
476 
477     if (ps_slice_hdr->i1_nal_unit_type == 5)
478     {
479         /* u2_idr_pic_id */
480         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_idr_pic_id, return_status, "u2_idr_pic_id");
481     }
482 
483     if (ps_sps->i1_pic_order_cnt_type == 0)
484     {
485         /* pic_order_cnt_lsb */
486         PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_pic_order_cnt_lsb, ps_sps->i1_log2_max_pic_order_cnt_lsb, return_status, "pic_order_cnt_lsb");
487 
488         if(ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
489         {
490             /* delta_pic_order_cnt_bottom */
491             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i4_delta_pic_order_cnt_bottom, return_status, "delta_pic_order_cnt_bottom");
492         }
493     }
494 
495     if (ps_sps->i1_pic_order_cnt_type == 1 && !ps_sps->i1_delta_pic_order_always_zero_flag)
496     {
497         /* delta_pic_order_cnt[0] */
498         PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[0], return_status, "delta_pic_order_cnt[0]");
499 
500         if (ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
501         {
502             /* delta_pic_order_cnt[1] */
503             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[1], return_status, "delta_pic_order_cnt[1]");
504         }
505     }
506 
507     if (ps_pps->i1_redundant_pic_cnt_present_flag)
508     {
509         /* redundant_pic_cnt */
510         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_redundant_pic_cnt, return_status, "redundant_pic_cnt");
511     }
512 
513     if (ps_slice_hdr->u1_slice_type == BSLICE)
514     {
515         /* direct_spatial_mv_pred_flag */
516         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_direct_spatial_mv_pred_flag, 1, return_status, "direct_spatial_mv_pred_flag");
517     }
518 
519     if (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == BSLICE)
520     {
521         /* num_ref_idx_active_override_flag */
522         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_num_ref_idx_active_override_flag, 1, return_status, "num_ref_idx_active_override_flag");
523 
524         if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
525         {
526             /* num_ref_idx_l0_active_minus1 */
527             PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status, "num_ref_idx_l0_active_minus1");
528 
529             if (ps_slice_hdr->u1_slice_type == BSLICE)
530             {
531                 /* num_ref_idx_l1_active_minus1 */
532                 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status, "num_ref_idx_l1_active_minus1");
533             }
534         }
535     }
536 
537     /* ref_idx_reordering */
538     /* TODO: ref_idx_reordering */
539     if ((ps_slice_hdr->u1_slice_type != ISLICE) && (ps_slice_hdr->u1_slice_type != SISLICE))
540     {
541         /* ref_pic_list_reordering_flag_l0 */
542         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l0, 1, return_status, "ref_pic_list_reordering_flag_l0");
543 
544         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
545         {
546 
547         }
548     }
549 
550     if (ps_slice_hdr->u1_slice_type == BSLICE)
551     {
552         /* ref_pic_list_reordering_flag_l1 */
553         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l1, 1, return_status, "ref_pic_list_reordering_flag_l1");
554 
555         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
556         {
557 
558         }
559     }
560 
561     if ((ps_pps->i1_weighted_pred_flag &&
562                     (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE)) ||
563                     (ps_slice_hdr->u1_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
564     {
565         /* TODO_LATER: Currently there is no support for weighted prediction.
566          This needs to be updated when the support is added */
567     }
568 
569     if (ps_slice_hdr->i1_nal_unit_idc != 0)
570     {
571         if (ps_slice_hdr->i1_nal_unit_type == 5)
572         {
573             /* no_output_of_prior_pics_flag  */
574             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_no_output_of_prior_pics_flag , 1, return_status, "no_output_of_prior_pics_flag ");
575 
576             /* long_term_reference_flag  */
577             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_long_term_reference_flag , 1, return_status, "long_term_reference_flag ");
578         }
579         else
580         {
581             /* adaptive_ref_pic_marking_mode_flag  */
582             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag , 1, return_status, "adaptive_ref_pic_marking_mode_flag ");
583 
584             if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
585             {
586                 /* TODO: if the reference picture marking mode is adaptive
587                  add these fields in the bit-stream */
588             }
589         }
590     }
591 
592     if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_slice_hdr->u1_slice_type != ISLICE &&
593                     ps_slice_hdr->u1_slice_type != SISLICE)
594     {
595         /* cabac_init_idc */
596         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_cabac_init_idc, return_status, "cabac_init_idc");
597     }
598 
599     /* slice_qp_delta */
600     PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp - ps_pps->i1_pic_init_qp, return_status, "slice_qp_delta");
601 
602     if (ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == SISLICE)
603     {
604         if (ps_slice_hdr->u1_slice_type == SPSLICE)
605         {
606             /* sp_for_switch_flag */
607             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_sp_for_switch_flag , 1, return_status, "sp_for_switch_flag");
608         }
609         /* slice_qs_delta */
610         PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->u1_slice_qs - ps_pps->i1_pic_init_qs, return_status, "slice_qs_delta");
611     }
612 
613     if (ps_pps->i1_deblocking_filter_control_present_flag)
614     {
615         /* disable_deblocking_filter_idc */
616         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_disable_deblocking_filter_idc, return_status, "disable_deblocking_filter_idc");
617 
618         if(ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
619         {
620             /* slice_alpha_c0_offset_div2 */
621             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_alpha_c0_offset_div2, return_status, "slice_alpha_c0_offset_div2");
622 
623             /* slice_beta_offset_div2 */
624             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_beta_offset_div2, return_status, "slice_beta_offset_div2");
625         }
626     }
627 
628     if (ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
629                     ps_pps->u1_slice_group_map_type >= 3 &&
630                     ps_pps->u1_slice_group_map_type <= 5)
631     {
632         /* slice_group_change_cycle */
633         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
634          * If this is not the case, we have to add Slice group map type to the bit stream */
635     }
636 
637     return return_status;
638 }
639 
640 
641 
642 /**
643 ******************************************************************************
644 *
645 * @brief Populates sps structure
646 *
647 * @par   Description
648 *  Populates sps structure for its use in header generation
649 *
650 * @param[in]   ps_codec
651 *  pointer to encoder context
652 *
653 * @param[out]  ps_sps
654 *  pointer to sps params that needs to be populated
655 *
656 * @return      success or failure error code
657 *
658 ******************************************************************************
659 */
ih264e_populate_sps(codec_t * ps_codec,sps_t * ps_sps)660 IH264E_ERROR_T ih264e_populate_sps(codec_t *ps_codec, sps_t *ps_sps)
661 {
662     /* active config parameters */
663     cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
664 
665 //    /* level */
666 //    IH264_LEVEL_T   level_idc;
667 
668     /* error_status */
669     IH264E_ERROR_T i4_err_code = IH264E_FAIL;
670 
671     /* profile */
672     /*
673      * Baseline profile supports, 8 bits per sample, 4:2:0 format, CAVLC.
674      * B frames are not allowed. Further, Flexible mb ordering, Redundant slices, Arbitrary slice ordering are supported.
675      * The constrained baseline profile is baseline profile minus ASO, FMO and redundant slices.
676      * To the constrained baseline profile if we add support for B slices, support for encoding interlaced frames,
677      * support for weighted prediction and introduce CABAC entropy coding then we have Main Profile.
678      */
679     if ((ps_cfg->u4_num_bframes) || (ps_cfg->e_content_type != IV_PROGRESSIVE) ||
680         (ps_cfg->u4_entropy_coding_mode == CABAC) || (ps_cfg->u4_weighted_prediction))
681     {
682         ps_sps->u1_profile_idc = IH264_PROFILE_MAIN;
683     }
684     else
685     {
686         ps_sps->u1_profile_idc = IH264_PROFILE_BASELINE;
687     }
688 
689     /* level */
690     ps_sps->u1_level_idc = MAX(ps_cfg->u4_max_level,
691                                (UWORD32)ih264e_get_min_level(ps_cfg->u4_max_wd, ps_cfg->u4_max_ht));
692 
693     /* constrained flags */
694     /*
695      * baseline profile automatically implies set 0 flag
696      */
697     ps_sps->u1_constraint_set0_flag = (ps_sps->u1_profile_idc == IH264_PROFILE_BASELINE);
698     /*
699      * main profile automatically implies set 1 flag
700      * Although the encoder says it supports Baseline profile it actually supports constrained
701      * baseline profile as ASO, FMO and redundant slices are not supported
702      */
703     ps_sps->u1_constraint_set1_flag = (ps_sps->u1_profile_idc <= IH264_PROFILE_MAIN);
704     /*
705      * extended profile is not supported
706      */
707     ps_sps->u1_constraint_set2_flag = 0x00;
708     /*
709      * level 1b or level 11
710      */
711     if (ps_sps->u1_level_idc == IH264_LEVEL_1B)
712     {
713         ps_sps->u1_constraint_set3_flag = 0;
714         ps_sps->u1_level_idc = IH264_LEVEL_11;
715     }
716     else
717     {
718         ps_sps->u1_constraint_set3_flag = 0;
719     }
720 
721     /* active sps id */
722     ps_sps->u1_sps_id = ps_codec->i4_sps_id;
723 
724     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
725     {
726         /* chroma format idc */
727         ps_sps->u1_chroma_format_idc = CHROMA_FMT_IDC_YUV420;
728 
729         /* residual_colour_transform_flag */
730         ps_sps->i1_residual_colour_transform_flag = 0;
731 
732         /* luma bit depth 8 */
733         ps_sps->i1_bit_depth_luma = 8;
734 
735         /* chroma bit depth 8 */
736         ps_sps->i1_bit_depth_chroma = 8;
737 
738         /* qpprime_y_zero_transform_bypass_flag */
739         ps_sps->i1_qpprime_y_zero_transform_bypass_flag = 0;
740 
741         /* seq_scaling_matrix_present_flag */
742         ps_sps->i1_seq_scaling_matrix_present_flag = 0;
743 
744         if (ps_sps->i1_seq_scaling_matrix_present_flag)
745         {
746             /* TODO_LATER: Will be enabled once scaling list support is added */
747         }
748     }
749 
750     /* log2_max_frame_num_minus4 */
751     ps_sps->i1_log2_max_frame_num = 16;
752 
753     /* pic_order_cnt_type */
754     ps_sps->i1_pic_order_cnt_type = 2;
755 
756     if (ps_codec->i4_non_ref_frames_in_stream)
757     {
758         ps_sps->i1_pic_order_cnt_type = 0;
759     }
760 
761     /* log2_max_pic_order_cnt_lsb_minus4 */
762     ps_sps->i1_log2_max_pic_order_cnt_lsb = 8;
763 
764     /* TODO : add support for other poc types */
765     if (ps_sps->i1_pic_order_cnt_type == 0)
766     {
767 
768     }
769     else if (ps_sps->i1_pic_order_cnt_type == 1)
770     {
771 
772     }
773 
774     /* num_ref_frames */
775     /* TODO : Should we have a flexible num ref frames */
776     if (ps_codec->s_cfg.u4_num_bframes > 0)
777     {
778         ps_sps->u1_max_num_ref_frames = 2;
779     }
780     else
781     {
782         ps_sps->u1_max_num_ref_frames = 1;
783     }
784 
785     /* gaps_in_frame_num_value_allowed_flag */
786     ps_sps->i1_gaps_in_frame_num_value_allowed_flag = 0;
787 
788     /* pic width in mb - 1 */
789     ps_sps->i2_pic_width_in_mbs_minus1 = ps_cfg->i4_wd_mbs - 1;
790 
791     /* pic height in mb - 1 */
792     ps_sps->i2_pic_height_in_map_units_minus1 = ps_cfg->i4_ht_mbs - 1;;
793 
794     /* frame_mbs_only_flag, no support for interlace encoding */
795     ps_sps->i1_frame_mbs_only_flag = 1;
796 
797     /* mb_adaptive_frame_field_flag */
798     if (ps_sps->i1_frame_mbs_only_flag == 0)
799     {
800         ps_sps->i1_mb_adaptive_frame_field_flag = 0;
801     }
802 
803     /* direct_8x8_inference_flag */
804     ps_sps->i1_direct_8x8_inference_flag = 0;
805 
806     /* cropping params */
807     /*NOTE : Cropping values depend on the chroma format
808      * For our case ,decoder interprets the cropping values as 2*num pixels
809      * Hence the difference in the disp width and width must be halved before sending
810      * to get the expected results
811      */
812     ps_sps->i1_frame_cropping_flag      = 0;
813     ps_sps->i2_frame_crop_left_offset   = 0;
814     ps_sps->i2_frame_crop_right_offset  = (ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd)>>1;
815     ps_sps->i2_frame_crop_top_offset    = 0;
816     ps_sps->i2_frame_crop_bottom_offset = (ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht)>>1;
817 
818     if (ps_sps->i2_frame_crop_left_offset    ||
819                     ps_sps->i2_frame_crop_right_offset   ||
820                     ps_sps->i2_frame_crop_top_offset     ||
821                     ps_sps->i2_frame_crop_bottom_offset)
822     {
823         ps_sps->i1_frame_cropping_flag      = 1;
824     }
825 
826     /* vui params */
827     ps_sps->i1_vui_parameters_present_flag = 0;
828 
829     if (ps_sps->i1_vui_parameters_present_flag)
830     {
831         /* populate vui params */
832     }
833 
834     return i4_err_code;
835 }
836 
837 /**
838 ******************************************************************************
839 *
840 * @brief Populates pps structure
841 *
842 * @par   Description
843 *  Populates pps structure for its use in header generation
844 *
845 * @param[in]   ps_codec
846 *  pointer to encoder context
847 *
848 * @param[out]  ps_pps
849 *  pointer to pps params that needs to be populated
850 *
851 * @return      success or failure error code
852 *
853 ******************************************************************************
854 */
ih264e_populate_pps(codec_t * ps_codec,pps_t * ps_pps)855 IH264E_ERROR_T ih264e_populate_pps(codec_t *ps_codec, pps_t *ps_pps)
856 {
857     /* active config parameters */
858     cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
859 
860     /* seq_parameter_set_id */
861     ps_pps->u1_sps_id = ps_codec->i4_sps_id;
862 
863     /* pic_parameter_set_id */
864     ps_pps->u1_pps_id = ps_codec->i4_pps_id;
865 
866     /* entropy_coding_mode */
867     ps_pps->u1_entropy_coding_mode_flag = ps_cfg->u4_entropy_coding_mode;
868 
869     /* pic_order_present_flag is unset if we don't have feilds */
870     ps_pps->u1_pic_order_present_flag = 0;
871 
872     /* Currently number of slice groups supported are 1 */
873     ps_pps->u1_num_slice_groups = 1;
874 
875     if (ps_pps->u1_num_slice_groups - 1)
876     {
877         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
878          * If this is not the case, we have to add Slice group map type to the bit stream*/
879     }
880 
881     /* number of reference frames for list 0 */
882     /* FIXME : fix this hard coded value */
883     ps_pps->i1_num_ref_idx_l0_default_active = 1;
884 
885     /* number of reference frames for list 1 */
886     ps_pps->i1_num_ref_idx_l1_default_active = 1;
887 
888     /* weighted prediction for now is disabled */
889     ps_pps->i1_weighted_pred_flag = 0;
890     ps_pps->i1_weighted_bipred_idc = 0;
891 
892     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
893     ps_pps->i1_pic_init_qp = 0;
894 
895     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
896     ps_pps->i1_pic_init_qs = 0;
897 
898     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
899     ps_pps->i1_chroma_qp_index_offset = 0;
900 
901     /* deblocking filter flags present in slice header */
902     ps_pps->i1_deblocking_filter_control_present_flag = 1;
903 
904     /* constrained intra prediction */
905     ps_pps->i1_constrained_intra_pred_flag = ps_cfg->u4_constrained_intra_pred;
906 
907     /* sending redundant slices is not supported for now */
908     ps_pps->i1_redundant_pic_cnt_present_flag = 0;
909 
910     ps_pps->u1_slice_group_map_type = 0;
911     return IH264E_SUCCESS;
912 }
913 
914 /**
915 ******************************************************************************
916 *
917 * @brief Populates slice header structure
918 *
919 * @par   Description
920 *  Populates slice header structure for its use in header generation
921 *
922 * @param[in]  ps_proc
923 *  pointer to proc context
924 *
925 * @param[out]  ps_slice_hdr
926 *  pointer to slice header structure that needs to be populated
927 *
928 * @param[in]  ps_pps
929 *  pointer to pps params structure referred by the slice
930 *
931 * @param[in]   ps_sps
932 *  pointer to sps params referred by the pps
933 *
934 * @return      success or failure error code
935 *
936 ******************************************************************************
937 */
ih264e_populate_slice_header(process_ctxt_t * ps_proc,slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps)938 WORD32 ih264e_populate_slice_header(process_ctxt_t *ps_proc,
939                                     slice_header_t *ps_slice_hdr,
940                                     pps_t *ps_pps,
941                                     sps_t *ps_sps)
942 {
943     /* entropy context */
944     entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
945 
946     codec_t *ps_codec = ps_proc->ps_codec;
947 
948     if (ps_proc->ps_codec->u4_is_curr_frm_ref)
949     {
950         ps_slice_hdr->i1_nal_unit_idc = 3;
951     }
952     else
953     {
954         ps_slice_hdr->i1_nal_unit_idc = 0;
955     }
956 
957     /* start mb address */
958     ps_slice_hdr->u2_first_mb_in_slice = ps_entropy->i4_mb_start_add;
959 
960     /* slice type */
961     ps_slice_hdr->u1_slice_type = ps_proc->i4_slice_type;
962 
963     /* pic_parameter_set_id */
964     ps_slice_hdr->u1_pps_id = ps_pps->u1_pps_id;
965 
966     /* Separate color plane flag is 0,
967      * hence the syntax element color_plane_id not included */
968 
969     /* frame num */
970     ps_slice_hdr->i4_frame_num = ps_proc->i4_frame_num;
971 
972     /* frame_mbs_only_flag, no support for interlace encoding */
973     if (!ps_sps->i1_frame_mbs_only_flag)
974     {
975         ps_slice_hdr->i1_field_pic_flag = 0;
976 
977         if (ps_slice_hdr->i1_field_pic_flag)
978         {
979             ps_slice_hdr->i1_bottom_field_flag = 0;
980         }
981     }
982 
983     /* idr pic id */
984     if (ps_proc->u4_is_idr)
985     {
986         ps_slice_hdr->u2_idr_pic_id = ps_proc->u4_idr_pic_id;
987         ps_slice_hdr->i1_nal_unit_type = 5;
988     }
989     else
990     {
991         ps_slice_hdr->i1_nal_unit_type = 1;
992     }
993 
994     if (ps_sps->i1_pic_order_cnt_type == 0)
995     {
996 
997         WORD32 i4_poc;
998         i4_poc = ps_codec->i4_poc;
999         i4_poc %= (1 << ps_sps->i1_log2_max_pic_order_cnt_lsb);
1000         ps_slice_hdr->i4_pic_order_cnt_lsb = i4_poc;
1001     }
1002     /* TODO add support for poc type 1 */
1003     else if (ps_sps->i1_pic_order_cnt_type == 1)
1004     {
1005 
1006     }
1007 
1008 
1009     /*
1010      * redundant slices are not currently supported.
1011      * Hence the syntax element redundant slice cnt is not initialized
1012      */
1013     if (ps_pps->i1_redundant_pic_cnt_present_flag)
1014     {
1015 
1016     }
1017 
1018     /* direct spatial mv pred flag */
1019     if (ps_proc->i4_slice_type == BSLICE)
1020     {
1021         ps_slice_hdr->u1_direct_spatial_mv_pred_flag = 1;
1022     }
1023 
1024     if (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == BSLICE)
1025     {
1026         /* num_ref_idx_active_override_flag */
1027         ps_slice_hdr->u1_num_ref_idx_active_override_flag = 0;
1028 
1029         if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
1030         {
1031             /* num_ref_idx_l0_active_minus1 */
1032 
1033             if (ps_proc->i4_slice_type == BSLICE)
1034             {
1035                 /* num_ref_idx_l1_active_minus1 */
1036 
1037             }
1038         }
1039     }
1040 
1041     /* ref_idx_reordering */
1042     /* TODO: ref_idx_reordering */
1043     if ((ps_proc->i4_slice_type != ISLICE) && (ps_proc->i4_slice_type != SISLICE))
1044     {
1045         /* ref_pic_list_reordering_flag_l0 */
1046         ps_slice_hdr->u1_ref_idx_reordering_flag_l0 = 0;
1047 
1048         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
1049         {
1050 
1051         }
1052 
1053         /* ref_pic_list_reordering_flag_l1 */
1054         ps_slice_hdr->u1_ref_idx_reordering_flag_l1 = 0;
1055 
1056         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
1057         {
1058 
1059         }
1060     }
1061 
1062 
1063     /* Currently we do not support weighted pred */
1064     /* ps_slice_hdr->u1_weighted_bipred_idc = 0; */
1065 
1066     if ((ps_pps->i1_weighted_pred_flag &&
1067                     (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE)) ||
1068                     (ps_proc->i4_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
1069     {
1070         /* TODO_LATER: Currently there is no support for weighted prediction.
1071              This needs to be updated when the support is added */
1072     }
1073 
1074     if (ps_slice_hdr->i1_nal_unit_idc != 0)
1075     {
1076         if (ps_slice_hdr->i1_nal_unit_type == 5)
1077         {
1078             /* no_output_of_prior_pics_flag  */
1079             ps_slice_hdr->u1_no_output_of_prior_pics_flag = 0;
1080 
1081             /* long_term_reference_flag  */
1082             ps_slice_hdr->u1_long_term_reference_flag = 0;
1083         }
1084         else
1085         {
1086             /* adaptive_ref_pic_marking_mode_flag  */
1087             ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag = 0;
1088 
1089             if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
1090             {
1091                 /* TODO: if the reference picture marking mode is adaptive
1092                      add these fields in the bit-stream */
1093             }
1094         }
1095     }
1096 
1097     /* entropy coding mode flag */
1098     ps_slice_hdr->u1_entropy_coding_mode_flag = ps_entropy->u1_entropy_coding_mode_flag;
1099 
1100     if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_proc->i4_slice_type != ISLICE &&
1101                     ps_proc->i4_slice_type != SISLICE)
1102     {
1103         /* cabac_init_idc */
1104     }
1105 
1106     /* slice qp */
1107     ps_slice_hdr->i1_slice_qp = ps_proc->u4_frame_qp;
1108 
1109     if (ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == SISLICE)
1110     {
1111         if (ps_proc->i4_slice_type == SPSLICE)
1112         {
1113             /* sp_for_switch_flag */
1114         }
1115         /* slice_qs_delta */
1116     }
1117 
1118     if (ps_pps->i1_deblocking_filter_control_present_flag)
1119     {
1120         /* disable_deblocking_filter_idc */
1121         ps_slice_hdr->u1_disable_deblocking_filter_idc = ps_proc->u4_disable_deblock_level;
1122 
1123         if (ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1124         {
1125             /* slice_alpha_c0_offset_div2 */
1126             ps_slice_hdr->i1_slice_alpha_c0_offset_div2 = 0;
1127 
1128             /* slice_beta_offset_div2 */
1129             ps_slice_hdr->i1_slice_beta_offset_div2 = 0;
1130         }
1131     }
1132     ps_slice_hdr->u1_num_slice_groups_minus1 = 0;
1133     if(ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1134         ps_pps->u1_slice_group_map_type >= 3 &&
1135         ps_pps->u1_slice_group_map_type <= 5)
1136     {
1137         /* slice_group_change_cycle */
1138         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1139          * If this is not the case, we have to add Slice group map type to the bit stream */
1140     }
1141 
1142     ps_slice_hdr->i1_cabac_init_idc = CABAC_INIT_IDC;
1143 
1144     return IH264E_SUCCESS;
1145 }
1146 
1147 /**
1148 ******************************************************************************
1149 *
1150 * @brief inserts FILLER Nal Unit.
1151 *
1152 * @par   Description
1153 *  In constant bit rate rc mode, when the bits generated by the codec is
1154 *  underflowing the target bit rate, the encoder library inserts filler nal unit.
1155 *
1156 * @param[in]    ps_bitstrm
1157 *  pointer to bitstream context (handle)
1158 *
1159 * @param[in]    insert_fill_bytes
1160 *  Number of fill bytes to be inserted
1161 *
1162 * @return      success or failure error code
1163 *
1164 ******************************************************************************
1165 */
ih264e_add_filler_nal_unit(bitstrm_t * ps_bitstrm,WORD32 insert_fill_bytes)1166 IH264E_ERROR_T ih264e_add_filler_nal_unit(bitstrm_t *ps_bitstrm,
1167                                           WORD32 insert_fill_bytes)
1168 {
1169     WORD32  i4_num_words_to_fill, i4_words_filled;
1170 
1171     IH264E_ERROR_T return_status = IH264E_SUCCESS;
1172 
1173     /* Insert the NAL start code */
1174     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
1175 
1176     if (ps_bitstrm->u4_strm_buf_offset + insert_fill_bytes >= ps_bitstrm->u4_max_strm_size)
1177     {
1178         return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1179     }
1180 
1181     /* Insert Nal Unit Header */
1182     PUT_BITS(ps_bitstrm, NAL_FILLER_FIRST_BYTE, 8, return_status, "filler_header");
1183 
1184     PUT_BITS(ps_bitstrm, 0xFFFFFF, 24, return_status, "fill bytes");
1185 
1186     /* Initializing Variables                           */
1187     i4_words_filled    = 1;
1188 
1189     /****************************************************/
1190     /* Flooring the number of bytes for be stuffed to   */
1191     /* WORD unit                                        */
1192     /****************************************************/
1193     i4_num_words_to_fill = (insert_fill_bytes >> 2);
1194 
1195     /****************************************************/
1196     /* Reducing already 4 bytes filled. In case stuffing*/
1197     /* is <= 4 bytes, we are actually not stuffing      */
1198     /* anything                                         */
1199     /****************************************************/
1200     i4_num_words_to_fill -= i4_words_filled;
1201 
1202     while (i4_num_words_to_fill > 0)
1203     {
1204         /* Insert Nal Unit Header */
1205         PUT_BITS(ps_bitstrm, 0xFFFFFFFF, 32, return_status, "fill bytes");
1206 
1207         i4_num_words_to_fill-- ;
1208     }
1209 
1210     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
1211 
1212     return return_status;
1213 }
1214 
1215