Home | History | Annotate | Download | only in encoder
      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 */
    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 * @brief Generates VUI (Video usability information)
    143 *
    144 * @par   Description
    145 *  This function generates VUI header as per the spec
    146 *
    147 * @param[in]   ps_bitstrm
    148 *  pointer to bitstream context (handle)
    149 *
    150 * @param[in]   ps_vui
    151 *  pointer to structure containing VUI data
    152 
    153 *
    154 * @return      success or failure error code
    155 *
    156 ******************************************************************************
    157 */
    158 WORD32 ih264e_generate_vui(bitstrm_t *ps_bitstrm, vui_t *ps_vui)
    159 {
    160     WORD32 return_status = IH264E_SUCCESS;
    161 
    162     /* aspect_ratio_info_present_flag */
    163     PUT_BITS(ps_bitstrm, ps_vui->u1_aspect_ratio_info_present_flag, 1,
    164              return_status, "aspect_ratio_info_present_flag");
    165 
    166     if(ps_vui->u1_aspect_ratio_info_present_flag)
    167     { /* aspect_ratio_idc */
    168         PUT_BITS(ps_bitstrm, ps_vui->u1_aspect_ratio_idc, 8, return_status,
    169                  "aspect_ratio_idc");
    170         if(255 == ps_vui->u1_aspect_ratio_idc) /* Extended_SAR */
    171         { /* sar_width */
    172             PUT_BITS(ps_bitstrm, ps_vui->u2_sar_width, 16, return_status,
    173                      "sar_width");
    174             /* sar_height */
    175             PUT_BITS(ps_bitstrm, ps_vui->u2_sar_height, 16, return_status,
    176                      "sar_height");
    177         }
    178 
    179     }
    180     /* overscan_info_present_flag */
    181     PUT_BITS(ps_bitstrm, ps_vui->u1_overscan_info_present_flag, 1,
    182              return_status, "overscan_info_present_flag");
    183 
    184     if(ps_vui->u1_overscan_info_present_flag)
    185     {
    186         /* overscan_appropriate_flag */
    187         PUT_BITS(ps_bitstrm, ps_vui->u1_overscan_appropriate_flag, 1,
    188                  return_status, "overscan_appropriate_flag");
    189 
    190     }
    191     /* video_signal_type_present_flag */
    192     PUT_BITS(ps_bitstrm, ps_vui->u1_video_signal_type_present_flag, 1,
    193              return_status, "video_signal_type_present_flag");
    194 
    195     if(ps_vui->u1_video_signal_type_present_flag)
    196     { /* video_format */
    197         PUT_BITS(ps_bitstrm, ps_vui->u1_video_format, 3, return_status,
    198                  "video_format");
    199 
    200         /* video_full_range_flag */
    201         PUT_BITS(ps_bitstrm, ps_vui->u1_video_full_range_flag, 1, return_status,
    202                  "video_full_range_flag");
    203 
    204         /* colour_description_present_flag */
    205         PUT_BITS(ps_bitstrm, ps_vui->u1_colour_description_present_flag, 1,
    206                  return_status, "colour_description_present_flag");
    207 
    208         if(ps_vui->u1_colour_description_present_flag)
    209         {
    210             /* colour_primaries */
    211             PUT_BITS(ps_bitstrm, ps_vui->u1_colour_primaries, 8, return_status,
    212                      "colour_primaries");
    213 
    214             /* transfer_characteristics */
    215             PUT_BITS(ps_bitstrm, ps_vui->u1_transfer_characteristics, 8,
    216                      return_status, "transfer_characteristics");
    217 
    218             /* matrix_coefficients */
    219             PUT_BITS(ps_bitstrm, ps_vui->u1_matrix_coefficients, 8,
    220                      return_status, "matrix_coefficients");
    221         }
    222 
    223     }
    224 
    225     /* chroma_loc_info_present_flag */
    226     PUT_BITS(ps_bitstrm, ps_vui->u1_chroma_loc_info_present_flag, 1,
    227              return_status, "chroma_loc_info_present_flag");
    228 
    229     if(ps_vui->u1_chroma_loc_info_present_flag)
    230     {
    231         /* chroma_sample_loc_type_top_field */
    232         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_chroma_sample_loc_type_top_field,
    233                      return_status, "chroma_sample_loc_type_top_field");
    234 
    235         /* chroma_sample_loc_type_bottom_field */
    236         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_chroma_sample_loc_type_bottom_field,
    237                      return_status, "chroma_sample_loc_type_bottom_field");
    238     }
    239 
    240     /* timing_info_present_flag */
    241     PUT_BITS(ps_bitstrm, ps_vui->u1_vui_timing_info_present_flag, 1,
    242              return_status, "timing_info_present_flag");
    243 
    244     if(ps_vui->u1_vui_timing_info_present_flag)
    245     {
    246         /* num_units_in_tick */
    247         PUT_BITS(ps_bitstrm, ps_vui->u4_vui_num_units_in_tick, 32,
    248                  return_status, "num_units_in_tick");
    249 
    250         /* time_scale */
    251         PUT_BITS(ps_bitstrm, ps_vui->u4_vui_time_scale, 32, return_status,
    252                  "time_scale");
    253 
    254         /* fixed_frame_rate_flag */
    255         PUT_BITS(ps_bitstrm, ps_vui->u1_fixed_frame_rate_flag, 1, return_status,
    256                  "fixed_frame_rate_flag");
    257 
    258     }
    259 
    260     /* nal_hrd_parameters_present_flag */
    261     PUT_BITS(ps_bitstrm, ps_vui->u1_nal_hrd_parameters_present_flag, 1,
    262              return_status, "nal_hrd_parameters_present_flag");
    263 
    264     if(ps_vui->u1_nal_hrd_parameters_present_flag)
    265     {
    266         hrd_params_t * ps_hrd_params = &ps_vui->s_nal_hrd_parameters;
    267         WORD32 i;
    268         /* cpb_cnt_minus1 */
    269         PUT_BITS_UEV(ps_bitstrm, ps_hrd_params->u1_cpb_cnt_minus1,
    270                      return_status, "cpb_cnt_minus1");
    271 
    272         /* bit_rate_scale */
    273         PUT_BITS(ps_bitstrm, ps_hrd_params->u4_bit_rate_scale, 4, return_status,
    274                  "bit_rate_scale");
    275 
    276         /* cpb_size_scale */
    277         PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_scale, 4, return_status,
    278                  "cpb_size_scale");
    279         for(i = 0; i < ps_hrd_params->u1_cpb_cnt_minus1; i++)
    280         {
    281             /* bit_rate_value_minus1[SchedSelIdx] */
    282             PUT_BITS_UEV(ps_bitstrm,
    283                          ps_hrd_params->au4_bit_rate_value_minus1[i],
    284                          return_status, "bit_rate_value_minus1[SchedSelIdx]");
    285 
    286             /* cpb_size_value_minus1[SchedSelIdx] */
    287             PUT_BITS_UEV(ps_bitstrm,
    288                          ps_hrd_params->au4_cpb_size_value_minus1[i],
    289                          return_status, "cpb_size_value_minus1[SchedSelIdx]");
    290 
    291             /* cbr_flag[SchedSelIdx] */
    292             PUT_BITS(ps_bitstrm, ps_hrd_params->au1_cbr_flag[i], 1,
    293                      return_status, "cbr_flag[SchedSelIdx]");
    294         }
    295 
    296         /* initial_cpb_removal_delay_length_minus1 */
    297         PUT_BITS(ps_bitstrm,
    298                  ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1, 5,
    299                  return_status, "initial_cpb_removal_delay_length_minus1");
    300 
    301         /* cpb_removal_delay_length_minus1 */
    302         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_cpb_removal_delay_length_minus1,
    303                  5, return_status, "cpb_removal_delay_length_minus1");
    304 
    305         /* dpb_output_delay_length_minus1 */
    306         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_dpb_output_delay_length_minus1,
    307                  5, return_status, "dpb_output_delay_length_minus1");
    308 
    309         /* time_offset_length */
    310         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_time_offset_length, 5,
    311                  return_status, "time_offset_length");
    312     }
    313 
    314     /* vcl_hrd_parameters_present_flag */
    315     PUT_BITS(ps_bitstrm, ps_vui->u1_vcl_hrd_parameters_present_flag, 1,
    316              return_status, "vcl_hrd_parameters_present_flag");
    317 
    318     if(ps_vui->u1_vcl_hrd_parameters_present_flag)
    319     {
    320         hrd_params_t * ps_hrd_params = &ps_vui->s_vcl_hrd_parameters;
    321         WORD32 i;
    322         /* cpb_cnt_minus1 */
    323         PUT_BITS_UEV(ps_bitstrm, ps_hrd_params->u1_cpb_cnt_minus1,
    324                      return_status, "cpb_cnt_minus1");
    325 
    326         /* bit_rate_scale */
    327         PUT_BITS(ps_bitstrm, ps_hrd_params->u4_bit_rate_scale, 4, return_status,
    328                  "bit_rate_scale");
    329 
    330         /* cpb_size_scale */
    331         PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_scale, 4, return_status,
    332                  "cpb_size_scale");
    333         for(i = 0; i < ps_hrd_params->u1_cpb_cnt_minus1; i++)
    334         {
    335             /* bit_rate_value_minus1[SchedSelIdx] */
    336             PUT_BITS_UEV(ps_bitstrm,
    337                          ps_hrd_params->au4_bit_rate_value_minus1[i],
    338                          return_status, "bit_rate_value_minus1[SchedSelIdx]");
    339 
    340             /* cpb_size_value_minus1[SchedSelIdx] */
    341             PUT_BITS_UEV(ps_bitstrm,
    342                          ps_hrd_params->au4_cpb_size_value_minus1[i],
    343                          return_status, "cpb_size_value_minus1[SchedSelIdx]");
    344 
    345             /* cbr_flag[SchedSelIdx] */
    346             PUT_BITS(ps_bitstrm, ps_hrd_params->au1_cbr_flag[i], 1,
    347                      return_status, "cbr_flag[SchedSelIdx]");
    348         }
    349 
    350         /* initial_cpb_removal_delay_length_minus1 */
    351         PUT_BITS(ps_bitstrm,
    352                  ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1, 5,
    353                  return_status, "initial_cpb_removal_delay_length_minus1");
    354 
    355         /* cpb_removal_delay_length_minus1 */
    356         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_cpb_removal_delay_length_minus1,
    357                  5, return_status, "cpb_removal_delay_length_minus1");
    358 
    359         /* dpb_output_delay_length_minus1 */
    360         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_dpb_output_delay_length_minus1,
    361                  5, return_status, "dpb_output_delay_length_minus1");
    362 
    363         /* time_offset_length */
    364         PUT_BITS(ps_bitstrm, ps_hrd_params->u1_time_offset_length, 5,
    365                  return_status, "time_offset_length");
    366     }
    367 
    368     if(ps_vui->u1_nal_hrd_parameters_present_flag
    369                     || ps_vui->u1_vcl_hrd_parameters_present_flag)
    370     {
    371         /* low_delay_hrd_flag */
    372         PUT_BITS(ps_bitstrm, ps_vui->u1_low_delay_hrd_flag, 1, return_status,
    373                  "low_delay_hrd_flag");
    374     }
    375     /* pic_struct_present_flag */
    376     PUT_BITS(ps_bitstrm, ps_vui->u1_pic_struct_present_flag, 1, return_status,
    377              "pic_struct_present_flag");
    378 
    379     /* bitstream_restriction_flag */
    380     PUT_BITS(ps_bitstrm, ps_vui->u1_bitstream_restriction_flag, 1,
    381              return_status, "bitstream_restriction_flag");
    382 
    383     if(ps_vui->u1_bitstream_restriction_flag == 1)
    384     {
    385         /* motion_vectors_over_pic_boundaries_flag */
    386         PUT_BITS(ps_bitstrm, ps_vui->u1_motion_vectors_over_pic_boundaries_flag,
    387                  1, return_status, "motion_vectors_over_pic_boundaries_flag");
    388 
    389         /* max_bytes_per_pic_denom */
    390         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_bytes_per_pic_denom,
    391                      return_status, "max_bytes_per_pic_denom");
    392 
    393         /* max_bits_per_mb_denom */
    394         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_bits_per_mb_denom,
    395                      return_status, "max_bits_per_mb_denom");
    396 
    397         /* log2_max_mv_length_horizontal */
    398         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_log2_max_mv_length_horizontal,
    399                      return_status, "log2_max_mv_length_horizontal");
    400 
    401         /* log2_max_mv_length_vertical */
    402         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_log2_max_mv_length_vertical,
    403                      return_status, "log2_max_mv_length_vertical");
    404 
    405         /* max_num_reorder_frames */
    406         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_num_reorder_frames, return_status,
    407                      "max_num_reorder_frames");
    408 
    409         /* max_dec_frame_buffering */
    410         PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_dec_frame_buffering,
    411                      return_status, "max_dec_frame_buffering");
    412     }
    413 
    414     return return_status;
    415 }
    416 
    417 /**
    418 ******************************************************************************
    419 *
    420 * @brief Generates SPS (Sequence Parameter Set)
    421 *
    422 * @par   Description
    423 *  This function generates Sequence Parameter Set header as per the spec
    424 *
    425 * @param[in]   ps_bitstrm
    426 *  pointer to bitstream context (handle)
    427 *
    428 * @param[in]   ps_sps
    429 *  pointer to structure containing SPS data
    430 *
    431 * @param[in]   ps_vui
    432 *  pointer to structure containing VUI data
    433 *
    434 * @return      success or failure error code
    435 *
    436 ******************************************************************************
    437 */
    438 WORD32 ih264e_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps, vui_t *ps_vui)
    439 {
    440     WORD32 return_status = IH264E_SUCCESS;
    441     WORD32 i;
    442     WORD8  i1_nal_unit_type = 7;
    443     WORD8  i1_nal_ref_idc = 3;
    444 
    445     /* Insert Start Code */
    446     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
    447 
    448     /* Insert Nal Unit Header */
    449     return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, i1_nal_unit_type, i1_nal_ref_idc);
    450 
    451     /* profile_idc */
    452     PUT_BITS(ps_bitstrm, ps_sps->u1_profile_idc, 8, return_status, "profile_idc");
    453 
    454     /* constrained_set_flags */
    455     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set0_flag, 1, return_status, "constrained_set0_flag");
    456     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set1_flag, 1, return_status, "constrained_set1_flag");
    457     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set2_flag, 1, return_status, "constrained_set2_flag");
    458     PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set3_flag, 1, return_status, "constrained_set3_flag");
    459 
    460     /* reserved_zero_four_bits */
    461     PUT_BITS(ps_bitstrm, 0, 4, return_status, "reserved_zero_four_bits");
    462 
    463     /* level_idc */
    464     PUT_BITS(ps_bitstrm, ps_sps->u1_level_idc, 8, return_status, "level_idc");
    465 
    466     /* seq_parameter_set_id */
    467     PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_sps_id, return_status, "seq_parameter_set_id");
    468 
    469     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
    470     {
    471         /* chroma_format_idc */
    472         PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_chroma_format_idc, return_status, "chroma_format_idc");
    473 
    474         if (ps_sps->u1_chroma_format_idc == CHROMA_FMT_IDC_YUV444)
    475         {
    476             /* i1_residual_colour_transform_flag */
    477             PUT_BITS(ps_bitstrm, ps_sps->i1_residual_colour_transform_flag, 1, return_status, "i1_residual_colour_transform_flag");
    478         }
    479 
    480         /* bit_depth_luma_minus8 */
    481         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_luma - 8), return_status, "bit_depth_luma_minus8");
    482 
    483         /* bit_depth_chroma_minus8 */
    484         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_chroma - 8), return_status, "bit_depth_chroma_minus8");
    485 
    486         /* qpprime_y_zero_transform_bypass_flag */
    487         PUT_BITS(ps_bitstrm, ps_sps->i1_qpprime_y_zero_transform_bypass_flag, 1, return_status, "qpprime_y_zero_transform_bypass_flag");
    488 
    489         /* seq_scaling_matrix_present_flag */
    490         PUT_BITS(ps_bitstrm, ps_sps->i1_seq_scaling_matrix_present_flag, 1, return_status, "seq_scaling_matrix_present_flag");
    491 
    492         /* seq_scaling_list */
    493         if (ps_sps->i1_seq_scaling_matrix_present_flag)
    494         {
    495             /* TODO_LATER: Will be enabled once scaling list support is added */
    496         }
    497     }
    498 
    499     /* log2_max_frame_num_minus4 */
    500     PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_frame_num - 4), return_status, "log2_max_frame_num_minus4");
    501 
    502     /* pic_order_cnt_type */
    503     PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_pic_order_cnt_type, return_status, "pic_order_cnt_type");
    504 
    505     if (ps_sps->i1_pic_order_cnt_type == 0)
    506     {
    507         /* log2_max_pic_order_cnt_lsb_minus4 */
    508         PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_pic_order_cnt_lsb - 4), return_status, "log2_max_pic_order_cnt_lsb_minus4");
    509     }
    510     else if (ps_sps->i1_pic_order_cnt_type == 1)
    511     {
    512         /* delta_pic_order_always_zero_flag */
    513         PUT_BITS(ps_bitstrm, ps_sps->i1_delta_pic_order_always_zero_flag, 1, return_status, "delta_pic_order_always_zero_flag");
    514 
    515         /* offset_for_non_ref_pic */
    516         PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_non_ref_pic, return_status, "offset_for_non_ref_pic");
    517 
    518         /* offset_for_top_to_bottom_field */
    519         PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_top_to_bottom_field, return_status, "offset_for_top_to_bottom_field");
    520 
    521         /* num_ref_frames_in_pic_order_cnt_cycle */
    522         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");
    523 
    524         /* Offset for ref frame */
    525         for (i=0; i<ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
    526         {
    527             /* offset_for_ref_frame */
    528             PUT_BITS_SEV(ps_bitstrm, ps_sps->ai4_offset_for_ref_frame[i], return_status, "offset_for_ref_frame");
    529         }
    530     }
    531 
    532     /* num_ref_frames */
    533     PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_max_num_ref_frames, return_status, "num_ref_frames");
    534 
    535     /* gaps_in_frame_num_value_allowed_flag */
    536     PUT_BITS(ps_bitstrm, ps_sps->i1_gaps_in_frame_num_value_allowed_flag, 1, return_status, "gaps_in_frame_num_value_allowed_flag");
    537 
    538     /* pic_width_in_mbs_minus1 */
    539     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_mbs_minus1, return_status, "pic_width_in_mbs_minus1");
    540 
    541     /* pic_height_in_map_units_minus1 */
    542     PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_map_units_minus1, return_status, "pic_height_in_map_units_minus1");
    543 
    544     /* frame_mbs_only_flag */
    545     PUT_BITS(ps_bitstrm, ps_sps->i1_frame_mbs_only_flag, 1, return_status, "frame_mbs_only_flag");
    546 
    547     if (!ps_sps->i1_frame_mbs_only_flag)
    548     {
    549         /* mb_adaptive_frame_field_flag */
    550         PUT_BITS(ps_bitstrm, ps_sps->i1_mb_adaptive_frame_field_flag, 1, return_status, "mb_adaptive_frame_field_flag");
    551     }
    552 
    553     /* direct_8x8_inference_flag */
    554     PUT_BITS(ps_bitstrm, ps_sps->i1_direct_8x8_inference_flag, 1, return_status, "direct_8x8_inference_flag");
    555 
    556     /* frame_cropping_flag */
    557     PUT_BITS(ps_bitstrm, ps_sps->i1_frame_cropping_flag, 1, return_status, "frame_cropping_flag");
    558 
    559     if (ps_sps->i1_frame_cropping_flag)
    560     {
    561         /* frame_crop_left_offset */
    562         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_left_offset, return_status, "frame_crop_left_offset");
    563 
    564         /* frame_crop_right_offset */
    565         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_right_offset, return_status, "frame_crop_right_offset");
    566 
    567         /* frame_crop_top_offset */
    568         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_top_offset, return_status, "frame_crop_top_offset");
    569 
    570         /* frame_crop_bottom_offset */
    571         PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_bottom_offset, return_status, "frame_crop_bottom_offset");
    572     }
    573 
    574     /* vui_parameters_present_flag */
    575     PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status, "vui_parameters_present_flag");
    576 
    577     if (ps_sps->i1_vui_parameters_present_flag)
    578     {
    579         /* Add vui parameters to the bitstream */;
    580         return_status |= ih264e_generate_vui(ps_bitstrm, ps_vui);
    581     }
    582 
    583     /* rbsp trailing bits */
    584     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
    585 
    586     return return_status;
    587 }
    588 
    589 /**
    590 ******************************************************************************
    591 *
    592 * @brief Generates PPS (Picture Parameter Set)
    593 *
    594 * @par   Description
    595 *  Generate Picture Parameter Set as per Section 7.3.2.2
    596 *
    597 * @param[in]   ps_bitstrm
    598 *  pointer to bitstream context (handle)
    599 *
    600 * @param[in]   ps_pps
    601 *  pointer to structure containing PPS data
    602 *
    603 * @return      success or failure error code
    604 *
    605 ******************************************************************************
    606 */
    607 WORD32 ih264e_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps, sps_t *ps_sps)
    608 {
    609     WORD32 return_status = IH264E_SUCCESS;
    610 
    611     /* Insert the NAL start code */
    612     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
    613 
    614     /* Insert Nal Unit Header */
    615     PUT_BITS(ps_bitstrm, NAL_PPS_FIRST_BYTE, 8, return_status, "pps_header");
    616 
    617     /* pic_parameter_set_id */
    618     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_pps_id, return_status, "pic_parameter_set_id");
    619 
    620     /* seq_parameter_set_id */
    621     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_sps_id, return_status, "seq_parameter_set_id");
    622 
    623     /* Entropy coding : 0-VLC; 1 - CABAC */
    624     PUT_BITS(ps_bitstrm, ps_pps->u1_entropy_coding_mode_flag, 1, return_status, "Entropy coding : 0-VLC; 1 - CABAC");
    625 
    626     /* Pic order present flag */
    627     PUT_BITS(ps_bitstrm, ps_pps->u1_pic_order_present_flag, 1, return_status, "Pic order present flag");
    628 
    629     /* Number of slice groups */
    630     PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_num_slice_groups - 1, return_status, "Number of slice groups");
    631 
    632     if (ps_pps->u1_num_slice_groups > 1)
    633     {
    634         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
    635          * If this is not the case, we have to add Slice group map type to the bit stream*/
    636     }
    637 
    638     /* num_ref_idx_l0_default_active_minus1 */
    639     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status, "num_ref_idx_l0_default_active_minus1");
    640 
    641     /* num_ref_idx_l1_default_active_minus1 */
    642     PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status, "num_ref_idx_l1_default_active_minus1");
    643 
    644     /* weighted_pred_flag */
    645     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status, "weighted_pred_flag");
    646 
    647     /* weighted_bipred_flag */
    648     PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_idc, 2, return_status, "weighted_bipred_idc");
    649 
    650     /* pic_init_qp_minus26 */
    651     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status, "pic_init_qp_minus26");
    652 
    653     /* pic_init_qs_minus26 */
    654     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qs - 26, return_status, "pic_init_qs_minus26");
    655 
    656     /* chroma_qp_index_offset */
    657     PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_chroma_qp_index_offset, return_status, "chroma_qp_index_offset");
    658 
    659     /* deblocking_filter_control_present_flag */
    660     PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status, "deblocking_filter_control_present_flag");
    661 
    662     /* constrained_intra_pred_flag */
    663     PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status, "constrained_intra_pred_flag");
    664 
    665     /*redundant_pic_cnt_present_flag */
    666     PUT_BITS(ps_bitstrm, ps_pps->i1_redundant_pic_cnt_present_flag, 1, return_status, "redundant_pic_cnt_present_flag");
    667 
    668     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
    669     {
    670         /* transform_8x8_mode_flag */
    671         PUT_BITS(ps_bitstrm, ps_pps->i1_transform_8x8_mode_flag, 1, return_status, "transform_8x8_mode_flag");
    672 
    673         /* pic_scaling_matrix_present_flag */
    674         PUT_BITS(ps_bitstrm, ps_pps->i1_pic_scaling_matrix_present_flag, 1, return_status, "pic_scaling_matrix_present_flag");
    675 
    676         if(ps_pps->i1_pic_scaling_matrix_present_flag)
    677         {
    678             /* TODO_LATER: Will be enabled once scaling list support is added */
    679         }
    680 
    681         /* Second chroma QP offset */
    682         PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_second_chroma_qp_index_offset, return_status, "Second chroma QP offset");
    683     }
    684 
    685     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
    686 
    687     return return_status;
    688 }
    689 
    690 /**
    691 ******************************************************************************
    692 *
    693 * @brief Generates Slice Header
    694 *
    695 * @par   Description
    696 *  Generate Slice Header as per Section 7.3.5.1
    697 *
    698 * @param[inout]   ps_bitstrm
    699 *  pointer to bitstream context for generating slice header
    700 *
    701 * @param[in]   ps_slice_hdr
    702 *  pointer to slice header params
    703 *
    704 * @param[in]   ps_pps
    705 *  pointer to pps params referred by slice
    706 *
    707 * @param[in]   ps_sps
    708 *  pointer to sps params referred by slice
    709 *
    710 * @param[out]   ps_dup_bit_strm_ent_offset
    711 *  Bitstream struct to store bitstream state
    712 *
    713 * @param[out]   pu4_first_slice_start_offset
    714 *  first slice offset is returned
    715 *
    716 * @return      success or failure error code
    717 *
    718 ******************************************************************************
    719 */
    720 WORD32 ih264e_generate_slice_header(bitstrm_t *ps_bitstrm,
    721                                     slice_header_t *ps_slice_hdr,
    722                                     pps_t *ps_pps,
    723                                     sps_t *ps_sps)
    724 {
    725 
    726     WORD32 return_status = IH264E_SUCCESS;
    727 
    728     /* Insert start code */
    729     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
    730 
    731     /* Insert Nal Unit Header */
    732     return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, ps_slice_hdr->i1_nal_unit_type, ps_slice_hdr->i1_nal_unit_idc);
    733 
    734     /* first_mb_in_slice */
    735     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_first_mb_in_slice, return_status, "first_mb_in_slice");
    736 
    737     /* slice_type */
    738     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_slice_type, return_status, "slice_type");
    739 
    740     /* pic_parameter_set_id */
    741     PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_pps_id, return_status, "pic_parameter_set_id");
    742 
    743     /* frame_num */
    744     PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_frame_num, ps_sps->i1_log2_max_frame_num, return_status, "frame_num");
    745 
    746     if (!ps_sps->i1_frame_mbs_only_flag)
    747     {
    748         /* field_pic_flag */
    749         PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_field_pic_flag, 1, return_status, "field_pic_flag");
    750 
    751         if(ps_slice_hdr->i1_field_pic_flag)
    752         {
    753             /* bottom_field_flag */
    754             PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_bottom_field_flag, 1, return_status, "bottom_field_flag");
    755         }
    756     }
    757 
    758     if (ps_slice_hdr->i1_nal_unit_type == 5)
    759     {
    760         /* u2_idr_pic_id */
    761         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_idr_pic_id, return_status, "u2_idr_pic_id");
    762     }
    763 
    764     if (ps_sps->i1_pic_order_cnt_type == 0)
    765     {
    766         /* pic_order_cnt_lsb */
    767         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");
    768 
    769         if(ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
    770         {
    771             /* delta_pic_order_cnt_bottom */
    772             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i4_delta_pic_order_cnt_bottom, return_status, "delta_pic_order_cnt_bottom");
    773         }
    774     }
    775 
    776     if (ps_sps->i1_pic_order_cnt_type == 1 && !ps_sps->i1_delta_pic_order_always_zero_flag)
    777     {
    778         /* delta_pic_order_cnt[0] */
    779         PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[0], return_status, "delta_pic_order_cnt[0]");
    780 
    781         if (ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
    782         {
    783             /* delta_pic_order_cnt[1] */
    784             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[1], return_status, "delta_pic_order_cnt[1]");
    785         }
    786     }
    787 
    788     if (ps_pps->i1_redundant_pic_cnt_present_flag)
    789     {
    790         /* redundant_pic_cnt */
    791         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_redundant_pic_cnt, return_status, "redundant_pic_cnt");
    792     }
    793 
    794     if (ps_slice_hdr->u1_slice_type == BSLICE)
    795     {
    796         /* direct_spatial_mv_pred_flag */
    797         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_direct_spatial_mv_pred_flag, 1, return_status, "direct_spatial_mv_pred_flag");
    798     }
    799 
    800     if (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == BSLICE)
    801     {
    802         /* num_ref_idx_active_override_flag */
    803         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_num_ref_idx_active_override_flag, 1, return_status, "num_ref_idx_active_override_flag");
    804 
    805         if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
    806         {
    807             /* num_ref_idx_l0_active_minus1 */
    808             PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status, "num_ref_idx_l0_active_minus1");
    809 
    810             if (ps_slice_hdr->u1_slice_type == BSLICE)
    811             {
    812                 /* num_ref_idx_l1_active_minus1 */
    813                 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status, "num_ref_idx_l1_active_minus1");
    814             }
    815         }
    816     }
    817 
    818     /* ref_idx_reordering */
    819     /* TODO: ref_idx_reordering */
    820     if ((ps_slice_hdr->u1_slice_type != ISLICE) && (ps_slice_hdr->u1_slice_type != SISLICE))
    821     {
    822         /* ref_pic_list_reordering_flag_l0 */
    823         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l0, 1, return_status, "ref_pic_list_reordering_flag_l0");
    824 
    825         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
    826         {
    827 
    828         }
    829     }
    830 
    831     if (ps_slice_hdr->u1_slice_type == BSLICE)
    832     {
    833         /* ref_pic_list_reordering_flag_l1 */
    834         PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l1, 1, return_status, "ref_pic_list_reordering_flag_l1");
    835 
    836         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
    837         {
    838 
    839         }
    840     }
    841 
    842     if ((ps_pps->i1_weighted_pred_flag &&
    843                     (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE)) ||
    844                     (ps_slice_hdr->u1_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
    845     {
    846         /* TODO_LATER: Currently there is no support for weighted prediction.
    847          This needs to be updated when the support is added */
    848     }
    849 
    850     if (ps_slice_hdr->i1_nal_unit_idc != 0)
    851     {
    852         if (ps_slice_hdr->i1_nal_unit_type == 5)
    853         {
    854             /* no_output_of_prior_pics_flag  */
    855             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_no_output_of_prior_pics_flag , 1, return_status, "no_output_of_prior_pics_flag ");
    856 
    857             /* long_term_reference_flag  */
    858             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_long_term_reference_flag , 1, return_status, "long_term_reference_flag ");
    859         }
    860         else
    861         {
    862             /* adaptive_ref_pic_marking_mode_flag  */
    863             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag , 1, return_status, "adaptive_ref_pic_marking_mode_flag ");
    864 
    865             if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
    866             {
    867                 /* TODO: if the reference picture marking mode is adaptive
    868                  add these fields in the bit-stream */
    869             }
    870         }
    871     }
    872 
    873     if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_slice_hdr->u1_slice_type != ISLICE &&
    874                     ps_slice_hdr->u1_slice_type != SISLICE)
    875     {
    876         /* cabac_init_idc */
    877         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_cabac_init_idc, return_status, "cabac_init_idc");
    878     }
    879 
    880     /* slice_qp_delta */
    881     PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp - ps_pps->i1_pic_init_qp, return_status, "slice_qp_delta");
    882 
    883     if (ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == SISLICE)
    884     {
    885         if (ps_slice_hdr->u1_slice_type == SPSLICE)
    886         {
    887             /* sp_for_switch_flag */
    888             PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_sp_for_switch_flag , 1, return_status, "sp_for_switch_flag");
    889         }
    890         /* slice_qs_delta */
    891         PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->u1_slice_qs - ps_pps->i1_pic_init_qs, return_status, "slice_qs_delta");
    892     }
    893 
    894     if (ps_pps->i1_deblocking_filter_control_present_flag)
    895     {
    896         /* disable_deblocking_filter_idc */
    897         PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_disable_deblocking_filter_idc, return_status, "disable_deblocking_filter_idc");
    898 
    899         if(ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
    900         {
    901             /* slice_alpha_c0_offset_div2 */
    902             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_alpha_c0_offset_div2, return_status, "slice_alpha_c0_offset_div2");
    903 
    904             /* slice_beta_offset_div2 */
    905             PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_beta_offset_div2, return_status, "slice_beta_offset_div2");
    906         }
    907     }
    908 
    909     if (ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
    910                     ps_pps->u1_slice_group_map_type >= 3 &&
    911                     ps_pps->u1_slice_group_map_type <= 5)
    912     {
    913         /* slice_group_change_cycle */
    914         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
    915          * If this is not the case, we have to add Slice group map type to the bit stream */
    916     }
    917 
    918     return return_status;
    919 }
    920 
    921 /**
    922 ******************************************************************************
    923 *
    924 * @brief Populates VUI structure
    925 *
    926 * @par   Description
    927 *  Populates VUI structure for its use in header generation
    928 *
    929 * @param[in]   ps_codec
    930 *  pointer to encoder context
    931 *
    932 * @return      success or failure error code
    933 *
    934 ******************************************************************************
    935 */
    936 IH264E_ERROR_T ih264e_populate_vui(codec_t *ps_codec)
    937 {
    938 
    939     vui_t *ps_vui = &ps_codec->s_cfg.s_vui;
    940     sps_t *ps_sps = ps_codec->ps_sps_base + ps_codec->i4_sps_id;
    941 
    942 
    943     ps_vui->u1_nal_hrd_parameters_present_flag = 0;
    944     ps_vui->u1_vcl_hrd_parameters_present_flag = 0;
    945 
    946     ps_vui->u1_bitstream_restriction_flag = 1;
    947     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
    948     ps_vui->u1_max_bytes_per_pic_denom = 0;
    949     ps_vui->u1_max_bits_per_mb_denom = 0;
    950     ps_vui->u1_log2_max_mv_length_horizontal = 16;
    951     ps_vui->u1_log2_max_mv_length_vertical = 16;
    952 
    953     if(ps_codec->s_cfg.u4_num_bframes == 0)
    954     {
    955         ps_vui->u1_num_reorder_frames = 0;
    956     }
    957     else
    958     {
    959         ps_vui->u1_num_reorder_frames = 1;
    960     }
    961 
    962     ps_vui->u1_max_dec_frame_buffering = ps_sps->u1_max_num_ref_frames;
    963 
    964 
    965     return 0;
    966 }
    967 
    968 
    969 
    970 /**
    971 ******************************************************************************
    972 *
    973 * @brief Populates sps structure
    974 *
    975 * @par   Description
    976 *  Populates sps structure for its use in header generation
    977 *
    978 * @param[in]   ps_codec
    979 *  pointer to encoder context
    980 *
    981 * @param[out]  ps_sps
    982 *  pointer to sps params that needs to be populated
    983 *
    984 * @return      success or failure error code
    985 *
    986 ******************************************************************************
    987 */
    988 IH264E_ERROR_T ih264e_populate_sps(codec_t *ps_codec, sps_t *ps_sps)
    989 {
    990     /* active config parameters */
    991     cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
    992 
    993 //    /* level */
    994 //    IH264_LEVEL_T   level_idc;
    995 
    996     /* error_status */
    997     IH264E_ERROR_T i4_err_code = IH264E_FAIL;
    998 
    999     /* profile */
   1000     /*
   1001      * Baseline profile supports, 8 bits per sample, 4:2:0 format, CAVLC.
   1002      * B frames are not allowed. Further, Flexible mb ordering, Redundant slices, Arbitrary slice ordering are supported.
   1003      * The constrained baseline profile is baseline profile minus ASO, FMO and redundant slices.
   1004      * To the constrained baseline profile if we add support for B slices, support for encoding interlaced frames,
   1005      * support for weighted prediction and introduce CABAC entropy coding then we have Main Profile.
   1006      */
   1007     if ((ps_cfg->u4_num_bframes) || (ps_cfg->e_content_type != IV_PROGRESSIVE) ||
   1008         (ps_cfg->u4_entropy_coding_mode == CABAC) || (ps_cfg->u4_weighted_prediction))
   1009     {
   1010         ps_sps->u1_profile_idc = IH264_PROFILE_MAIN;
   1011     }
   1012     else
   1013     {
   1014         ps_sps->u1_profile_idc = IH264_PROFILE_BASELINE;
   1015     }
   1016 
   1017     /* level */
   1018     ps_sps->u1_level_idc = MAX(ps_cfg->u4_max_level,
   1019                                (UWORD32)ih264e_get_min_level(ps_cfg->u4_max_wd, ps_cfg->u4_max_ht));
   1020 
   1021     /* constrained flags */
   1022     /*
   1023      * baseline profile automatically implies set 0 flag
   1024      */
   1025     ps_sps->u1_constraint_set0_flag = (ps_sps->u1_profile_idc == IH264_PROFILE_BASELINE);
   1026     /*
   1027      * main profile automatically implies set 1 flag
   1028      * Although the encoder says it supports Baseline profile it actually supports constrained
   1029      * baseline profile as ASO, FMO and redundant slices are not supported
   1030      */
   1031     ps_sps->u1_constraint_set1_flag = (ps_sps->u1_profile_idc <= IH264_PROFILE_MAIN);
   1032     /*
   1033      * extended profile is not supported
   1034      */
   1035     ps_sps->u1_constraint_set2_flag = 0x00;
   1036     /*
   1037      * level 1b or level 11
   1038      */
   1039     if (ps_sps->u1_level_idc == IH264_LEVEL_1B)
   1040     {
   1041         ps_sps->u1_constraint_set3_flag = 0;
   1042         ps_sps->u1_level_idc = IH264_LEVEL_11;
   1043     }
   1044     else
   1045     {
   1046         ps_sps->u1_constraint_set3_flag = 0;
   1047     }
   1048 
   1049     /* active sps id */
   1050     ps_sps->u1_sps_id = ps_codec->i4_sps_id;
   1051 
   1052     if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
   1053     {
   1054         /* chroma format idc */
   1055         ps_sps->u1_chroma_format_idc = CHROMA_FMT_IDC_YUV420;
   1056 
   1057         /* residual_colour_transform_flag */
   1058         ps_sps->i1_residual_colour_transform_flag = 0;
   1059 
   1060         /* luma bit depth 8 */
   1061         ps_sps->i1_bit_depth_luma = 8;
   1062 
   1063         /* chroma bit depth 8 */
   1064         ps_sps->i1_bit_depth_chroma = 8;
   1065 
   1066         /* qpprime_y_zero_transform_bypass_flag */
   1067         ps_sps->i1_qpprime_y_zero_transform_bypass_flag = 0;
   1068 
   1069         /* seq_scaling_matrix_present_flag */
   1070         ps_sps->i1_seq_scaling_matrix_present_flag = 0;
   1071 
   1072         if (ps_sps->i1_seq_scaling_matrix_present_flag)
   1073         {
   1074             /* TODO_LATER: Will be enabled once scaling list support is added */
   1075         }
   1076     }
   1077 
   1078     /* log2_max_frame_num_minus4 */
   1079     ps_sps->i1_log2_max_frame_num = 16;
   1080 
   1081     /* pic_order_cnt_type */
   1082     ps_sps->i1_pic_order_cnt_type = 2;
   1083 
   1084     if (ps_codec->i4_non_ref_frames_in_stream)
   1085     {
   1086         ps_sps->i1_pic_order_cnt_type = 0;
   1087     }
   1088 
   1089     /* log2_max_pic_order_cnt_lsb_minus4 */
   1090     ps_sps->i1_log2_max_pic_order_cnt_lsb = 8;
   1091 
   1092     /* TODO : add support for other poc types */
   1093     if (ps_sps->i1_pic_order_cnt_type == 0)
   1094     {
   1095 
   1096     }
   1097     else if (ps_sps->i1_pic_order_cnt_type == 1)
   1098     {
   1099 
   1100     }
   1101 
   1102     /* num_ref_frames */
   1103     /* TODO : Should we have a flexible num ref frames */
   1104     if (ps_codec->s_cfg.u4_num_bframes > 0)
   1105     {
   1106         ps_sps->u1_max_num_ref_frames = 2;
   1107     }
   1108     else
   1109     {
   1110         ps_sps->u1_max_num_ref_frames = 1;
   1111     }
   1112 
   1113     /* gaps_in_frame_num_value_allowed_flag */
   1114     ps_sps->i1_gaps_in_frame_num_value_allowed_flag = 0;
   1115 
   1116     /* pic width in mb - 1 */
   1117     ps_sps->i2_pic_width_in_mbs_minus1 = ps_cfg->i4_wd_mbs - 1;
   1118 
   1119     /* pic height in mb - 1 */
   1120     ps_sps->i2_pic_height_in_map_units_minus1 = ps_cfg->i4_ht_mbs - 1;;
   1121 
   1122     /* frame_mbs_only_flag, no support for interlace encoding */
   1123     ps_sps->i1_frame_mbs_only_flag = 1;
   1124 
   1125     /* mb_adaptive_frame_field_flag */
   1126     if (ps_sps->i1_frame_mbs_only_flag == 0)
   1127     {
   1128         ps_sps->i1_mb_adaptive_frame_field_flag = 0;
   1129     }
   1130 
   1131     /* direct_8x8_inference_flag */
   1132     if (ps_sps->u1_level_idc < IH264_LEVEL_30)
   1133     {
   1134         ps_sps->i1_direct_8x8_inference_flag = 0;
   1135     }
   1136     else
   1137     {
   1138         ps_sps->i1_direct_8x8_inference_flag = 1;
   1139     }
   1140 
   1141     /* cropping params */
   1142     /*NOTE : Cropping values depend on the chroma format
   1143      * For our case ,decoder interprets the cropping values as 2*num pixels
   1144      * Hence the difference in the disp width and width must be halved before sending
   1145      * to get the expected results
   1146      */
   1147     ps_sps->i1_frame_cropping_flag      = 0;
   1148     ps_sps->i2_frame_crop_left_offset   = 0;
   1149     ps_sps->i2_frame_crop_right_offset  = (ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd)>>1;
   1150     ps_sps->i2_frame_crop_top_offset    = 0;
   1151     ps_sps->i2_frame_crop_bottom_offset = (ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht)>>1;
   1152 
   1153     if (ps_sps->i2_frame_crop_left_offset    ||
   1154                     ps_sps->i2_frame_crop_right_offset   ||
   1155                     ps_sps->i2_frame_crop_top_offset     ||
   1156                     ps_sps->i2_frame_crop_bottom_offset)
   1157     {
   1158         ps_sps->i1_frame_cropping_flag      = 1;
   1159     }
   1160 
   1161     /* vui params */
   1162     ps_sps->i1_vui_parameters_present_flag = 1;
   1163 
   1164     if (ps_sps->i1_vui_parameters_present_flag)
   1165     {
   1166         /* populate vui params */
   1167         ih264e_populate_vui(ps_codec);
   1168     }
   1169 
   1170     return i4_err_code;
   1171 }
   1172 
   1173 /**
   1174 ******************************************************************************
   1175 *
   1176 * @brief Populates pps structure
   1177 *
   1178 * @par   Description
   1179 *  Populates pps structure for its use in header generation
   1180 *
   1181 * @param[in]   ps_codec
   1182 *  pointer to encoder context
   1183 *
   1184 * @param[out]  ps_pps
   1185 *  pointer to pps params that needs to be populated
   1186 *
   1187 * @return      success or failure error code
   1188 *
   1189 ******************************************************************************
   1190 */
   1191 IH264E_ERROR_T ih264e_populate_pps(codec_t *ps_codec, pps_t *ps_pps)
   1192 {
   1193     /* active config parameters */
   1194     cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
   1195 
   1196     /* seq_parameter_set_id */
   1197     ps_pps->u1_sps_id = ps_codec->i4_sps_id;
   1198 
   1199     /* pic_parameter_set_id */
   1200     ps_pps->u1_pps_id = ps_codec->i4_pps_id;
   1201 
   1202     /* entropy_coding_mode */
   1203     ps_pps->u1_entropy_coding_mode_flag = ps_cfg->u4_entropy_coding_mode;
   1204 
   1205     /* pic_order_present_flag is unset if we don't have feilds */
   1206     ps_pps->u1_pic_order_present_flag = 0;
   1207 
   1208     /* Currently number of slice groups supported are 1 */
   1209     ps_pps->u1_num_slice_groups = 1;
   1210 
   1211     if (ps_pps->u1_num_slice_groups - 1)
   1212     {
   1213         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
   1214          * If this is not the case, we have to add Slice group map type to the bit stream*/
   1215     }
   1216 
   1217     /* number of reference frames for list 0 */
   1218     /* FIXME : fix this hard coded value */
   1219     ps_pps->i1_num_ref_idx_l0_default_active = 1;
   1220 
   1221     /* number of reference frames for list 1 */
   1222     ps_pps->i1_num_ref_idx_l1_default_active = 1;
   1223 
   1224     /* weighted prediction for now is disabled */
   1225     ps_pps->i1_weighted_pred_flag = 0;
   1226     ps_pps->i1_weighted_bipred_idc = 0;
   1227 
   1228     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
   1229     ps_pps->i1_pic_init_qp = 0;
   1230 
   1231     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
   1232     ps_pps->i1_pic_init_qs = 0;
   1233 
   1234     /* The intent is to not signal qp from pps. Rather send the same in slice headers */
   1235     ps_pps->i1_chroma_qp_index_offset = 0;
   1236 
   1237     /* deblocking filter flags present in slice header */
   1238     ps_pps->i1_deblocking_filter_control_present_flag = 1;
   1239 
   1240     /* constrained intra prediction */
   1241     ps_pps->i1_constrained_intra_pred_flag = ps_cfg->u4_constrained_intra_pred;
   1242 
   1243     /* sending redundant slices is not supported for now */
   1244     ps_pps->i1_redundant_pic_cnt_present_flag = 0;
   1245 
   1246     ps_pps->u1_slice_group_map_type = 0;
   1247     return IH264E_SUCCESS;
   1248 }
   1249 
   1250 /**
   1251 ******************************************************************************
   1252 *
   1253 * @brief Populates slice header structure
   1254 *
   1255 * @par   Description
   1256 *  Populates slice header structure for its use in header generation
   1257 *
   1258 * @param[in]  ps_proc
   1259 *  pointer to proc context
   1260 *
   1261 * @param[out]  ps_slice_hdr
   1262 *  pointer to slice header structure that needs to be populated
   1263 *
   1264 * @param[in]  ps_pps
   1265 *  pointer to pps params structure referred by the slice
   1266 *
   1267 * @param[in]   ps_sps
   1268 *  pointer to sps params referred by the pps
   1269 *
   1270 * @return      success or failure error code
   1271 *
   1272 ******************************************************************************
   1273 */
   1274 WORD32 ih264e_populate_slice_header(process_ctxt_t *ps_proc,
   1275                                     slice_header_t *ps_slice_hdr,
   1276                                     pps_t *ps_pps,
   1277                                     sps_t *ps_sps)
   1278 {
   1279     /* entropy context */
   1280     entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
   1281 
   1282     codec_t *ps_codec = ps_proc->ps_codec;
   1283 
   1284     if (ps_proc->ps_codec->u4_is_curr_frm_ref)
   1285     {
   1286         ps_slice_hdr->i1_nal_unit_idc = 3;
   1287     }
   1288     else
   1289     {
   1290         ps_slice_hdr->i1_nal_unit_idc = 0;
   1291     }
   1292 
   1293     /* start mb address */
   1294     ps_slice_hdr->u2_first_mb_in_slice = ps_entropy->i4_mb_start_add;
   1295 
   1296     /* slice type */
   1297     ps_slice_hdr->u1_slice_type = ps_proc->i4_slice_type;
   1298 
   1299     /* pic_parameter_set_id */
   1300     ps_slice_hdr->u1_pps_id = ps_pps->u1_pps_id;
   1301 
   1302     /* Separate color plane flag is 0,
   1303      * hence the syntax element color_plane_id not included */
   1304 
   1305     /* frame num */
   1306     ps_slice_hdr->i4_frame_num = ps_proc->i4_frame_num;
   1307 
   1308     /* frame_mbs_only_flag, no support for interlace encoding */
   1309     if (!ps_sps->i1_frame_mbs_only_flag)
   1310     {
   1311         ps_slice_hdr->i1_field_pic_flag = 0;
   1312 
   1313         if (ps_slice_hdr->i1_field_pic_flag)
   1314         {
   1315             ps_slice_hdr->i1_bottom_field_flag = 0;
   1316         }
   1317     }
   1318 
   1319     /* idr pic id */
   1320     if (ps_proc->u4_is_idr)
   1321     {
   1322         ps_slice_hdr->u2_idr_pic_id = ps_proc->u4_idr_pic_id;
   1323         ps_slice_hdr->i1_nal_unit_type = 5;
   1324     }
   1325     else
   1326     {
   1327         ps_slice_hdr->i1_nal_unit_type = 1;
   1328     }
   1329 
   1330     if (ps_sps->i1_pic_order_cnt_type == 0)
   1331     {
   1332 
   1333         WORD32 i4_poc;
   1334         i4_poc = ps_codec->i4_poc;
   1335         i4_poc %= (1 << ps_sps->i1_log2_max_pic_order_cnt_lsb);
   1336         ps_slice_hdr->i4_pic_order_cnt_lsb = i4_poc;
   1337     }
   1338     /* TODO add support for poc type 1 */
   1339     else if (ps_sps->i1_pic_order_cnt_type == 1)
   1340     {
   1341 
   1342     }
   1343 
   1344 
   1345     /*
   1346      * redundant slices are not currently supported.
   1347      * Hence the syntax element redundant slice cnt is not initialized
   1348      */
   1349     if (ps_pps->i1_redundant_pic_cnt_present_flag)
   1350     {
   1351 
   1352     }
   1353 
   1354     /* direct spatial mv pred flag */
   1355     if (ps_proc->i4_slice_type == BSLICE)
   1356     {
   1357         ps_slice_hdr->u1_direct_spatial_mv_pred_flag = 1;
   1358     }
   1359 
   1360     if (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == BSLICE)
   1361     {
   1362         /* num_ref_idx_active_override_flag */
   1363         ps_slice_hdr->u1_num_ref_idx_active_override_flag = 0;
   1364 
   1365         if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
   1366         {
   1367             /* num_ref_idx_l0_active_minus1 */
   1368 
   1369             if (ps_proc->i4_slice_type == BSLICE)
   1370             {
   1371                 /* num_ref_idx_l1_active_minus1 */
   1372 
   1373             }
   1374         }
   1375     }
   1376 
   1377     /* ref_idx_reordering */
   1378     /* TODO: ref_idx_reordering */
   1379     if ((ps_proc->i4_slice_type != ISLICE) && (ps_proc->i4_slice_type != SISLICE))
   1380     {
   1381         /* ref_pic_list_reordering_flag_l0 */
   1382         ps_slice_hdr->u1_ref_idx_reordering_flag_l0 = 0;
   1383 
   1384         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
   1385         {
   1386 
   1387         }
   1388 
   1389         /* ref_pic_list_reordering_flag_l1 */
   1390         ps_slice_hdr->u1_ref_idx_reordering_flag_l1 = 0;
   1391 
   1392         if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
   1393         {
   1394 
   1395         }
   1396     }
   1397 
   1398 
   1399     /* Currently we do not support weighted pred */
   1400     /* ps_slice_hdr->u1_weighted_bipred_idc = 0; */
   1401 
   1402     if ((ps_pps->i1_weighted_pred_flag &&
   1403                     (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE)) ||
   1404                     (ps_proc->i4_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
   1405     {
   1406         /* TODO_LATER: Currently there is no support for weighted prediction.
   1407              This needs to be updated when the support is added */
   1408     }
   1409 
   1410     if (ps_slice_hdr->i1_nal_unit_idc != 0)
   1411     {
   1412         if (ps_slice_hdr->i1_nal_unit_type == 5)
   1413         {
   1414             /* no_output_of_prior_pics_flag  */
   1415             ps_slice_hdr->u1_no_output_of_prior_pics_flag = 0;
   1416 
   1417             /* long_term_reference_flag  */
   1418             ps_slice_hdr->u1_long_term_reference_flag = 0;
   1419         }
   1420         else
   1421         {
   1422             /* adaptive_ref_pic_marking_mode_flag  */
   1423             ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag = 0;
   1424 
   1425             if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
   1426             {
   1427                 /* TODO: if the reference picture marking mode is adaptive
   1428                      add these fields in the bit-stream */
   1429             }
   1430         }
   1431     }
   1432 
   1433     /* entropy coding mode flag */
   1434     ps_slice_hdr->u1_entropy_coding_mode_flag = ps_entropy->u1_entropy_coding_mode_flag;
   1435 
   1436     if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_proc->i4_slice_type != ISLICE &&
   1437                     ps_proc->i4_slice_type != SISLICE)
   1438     {
   1439         /* cabac_init_idc */
   1440     }
   1441 
   1442     /* slice qp */
   1443     ps_slice_hdr->i1_slice_qp = ps_proc->u4_frame_qp;
   1444 
   1445     if (ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == SISLICE)
   1446     {
   1447         if (ps_proc->i4_slice_type == SPSLICE)
   1448         {
   1449             /* sp_for_switch_flag */
   1450         }
   1451         /* slice_qs_delta */
   1452     }
   1453 
   1454     if (ps_pps->i1_deblocking_filter_control_present_flag)
   1455     {
   1456         /* disable_deblocking_filter_idc */
   1457         ps_slice_hdr->u1_disable_deblocking_filter_idc = ps_proc->u4_disable_deblock_level;
   1458 
   1459         if (ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
   1460         {
   1461             /* slice_alpha_c0_offset_div2 */
   1462             ps_slice_hdr->i1_slice_alpha_c0_offset_div2 = 0;
   1463 
   1464             /* slice_beta_offset_div2 */
   1465             ps_slice_hdr->i1_slice_beta_offset_div2 = 0;
   1466         }
   1467     }
   1468     ps_slice_hdr->u1_num_slice_groups_minus1 = 0;
   1469     if(ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
   1470         ps_pps->u1_slice_group_map_type >= 3 &&
   1471         ps_pps->u1_slice_group_map_type <= 5)
   1472     {
   1473         /* slice_group_change_cycle */
   1474         /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
   1475          * If this is not the case, we have to add Slice group map type to the bit stream */
   1476     }
   1477 
   1478     ps_slice_hdr->i1_cabac_init_idc = CABAC_INIT_IDC;
   1479 
   1480     return IH264E_SUCCESS;
   1481 }
   1482 
   1483 /**
   1484 ******************************************************************************
   1485 *
   1486 * @brief inserts FILLER Nal Unit.
   1487 *
   1488 * @par   Description
   1489 *  In constant bit rate rc mode, when the bits generated by the codec is
   1490 *  underflowing the target bit rate, the encoder library inserts filler nal unit.
   1491 *
   1492 * @param[in]    ps_bitstrm
   1493 *  pointer to bitstream context (handle)
   1494 *
   1495 * @param[in]    insert_fill_bytes
   1496 *  Number of fill bytes to be inserted
   1497 *
   1498 * @return      success or failure error code
   1499 *
   1500 ******************************************************************************
   1501 */
   1502 IH264E_ERROR_T ih264e_add_filler_nal_unit(bitstrm_t *ps_bitstrm,
   1503                                           WORD32 insert_fill_bytes)
   1504 {
   1505     WORD32  i4_num_words_to_fill, i4_words_filled;
   1506 
   1507     IH264E_ERROR_T return_status = IH264E_SUCCESS;
   1508 
   1509     /* Insert the NAL start code */
   1510     return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
   1511 
   1512     if (ps_bitstrm->u4_strm_buf_offset + insert_fill_bytes >= ps_bitstrm->u4_max_strm_size)
   1513     {
   1514         return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
   1515     }
   1516 
   1517     /* Insert Nal Unit Header */
   1518     PUT_BITS(ps_bitstrm, NAL_FILLER_FIRST_BYTE, 8, return_status, "filler_header");
   1519 
   1520     PUT_BITS(ps_bitstrm, 0xFFFFFF, 24, return_status, "fill bytes");
   1521 
   1522     /* Initializing Variables                           */
   1523     i4_words_filled    = 1;
   1524 
   1525     /****************************************************/
   1526     /* Flooring the number of bytes for be stuffed to   */
   1527     /* WORD unit                                        */
   1528     /****************************************************/
   1529     i4_num_words_to_fill = (insert_fill_bytes >> 2);
   1530 
   1531     /****************************************************/
   1532     /* Reducing already 4 bytes filled. In case stuffing*/
   1533     /* is <= 4 bytes, we are actually not stuffing      */
   1534     /* anything                                         */
   1535     /****************************************************/
   1536     i4_num_words_to_fill -= i4_words_filled;
   1537 
   1538     while (i4_num_words_to_fill > 0)
   1539     {
   1540         /* Insert Nal Unit Header */
   1541         PUT_BITS(ps_bitstrm, 0xFFFFFFFF, 32, return_status, "fill bytes");
   1542 
   1543         i4_num_words_to_fill-- ;
   1544     }
   1545 
   1546     return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
   1547 
   1548     return return_status;
   1549 }
   1550 
   1551