parset.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:50k
源码类别:

Audio

开发平台:

Visual C++

  1. /*!
  2.  **************************************************************************************
  3.  * file
  4.  *    parset.c
  5.  * brief
  6.  *    Picture and Sequence Parameter set generation and handling
  7.  *  date 25 November 2002
  8.  * author
  9.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10.  *      - Stephan Wenger        <stewe@cs.tu-berlin.de>
  11.  *
  12.  **************************************************************************************
  13.  */
  14. #include <time.h>
  15. #include <sys/timeb.h>
  16. #include "global.h"
  17. #include "contributors.h"
  18. #include "nal.h"
  19. #include "mbuffer.h"
  20. #include "parset.h"
  21. #include "vlc.h"
  22. #include "q_matrix.h"
  23. // Local helpers
  24. static int IdentifyProfile(void);
  25. static int IdentifyLevel(void);
  26. static int GenerateVUI_parameters_rbsp(seq_parameter_set_rbsp_t *sps, Bitstream *bitstream);
  27. extern ColocatedParams *Co_located;
  28. extern ColocatedParams *Co_located_JV[MAX_PLANE];  //!< Co_located to be used during 4:4:4 independent mode encoding
  29. pic_parameter_set_rbsp_t *PicParSet[MAXPPS];
  30. static const byte ZZ_SCAN[16]  =
  31. {  0,  1,  4,  8,  5,  2,  3,  6,  9, 12, 13, 10,  7, 11, 14, 15
  32. };
  33. static const byte ZZ_SCAN8[64] =
  34. {  0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
  35.    12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
  36.    35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  37.    58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  38. };
  39. /*!
  40.  *************************************************************************************
  41.  * brief
  42.  *    generates a sequence and picture parameter set and stores these in global
  43.  *    active_sps and active_pps
  44.  *
  45.  * return
  46.  *    A NALU containing the Sequence ParameterSet
  47.  *
  48.  *************************************************************************************
  49. */
  50. void GenerateParameterSets (void)
  51. {
  52.   int i;
  53.   seq_parameter_set_rbsp_t *sps = NULL;
  54.   sps = AllocSPS();
  55.   for (i=0; i<MAXPPS; i++)
  56.   {
  57.     PicParSet[i] = NULL;
  58.   }
  59.   GenerateSequenceParameterSet(sps, 0);
  60.   if (params->GenerateMultiplePPS)
  61.   {
  62.     PicParSet[0] = AllocPPS();
  63.     PicParSet[1] = AllocPPS();
  64.     PicParSet[2] = AllocPPS();
  65.     if (IS_FREXT_PROFILE(sps->profile_idc))
  66.     {
  67.       GeneratePictureParameterSet( PicParSet[0], sps, 0, 0, 0, params->cb_qp_index_offset, params->cr_qp_index_offset);
  68.       GeneratePictureParameterSet( PicParSet[1], sps, 1, 1, 1, params->cb_qp_index_offset, params->cr_qp_index_offset);
  69.       GeneratePictureParameterSet( PicParSet[2], sps, 2, 1, 2, params->cb_qp_index_offset, params->cr_qp_index_offset);
  70.     }
  71.     else
  72.     {
  73.       GeneratePictureParameterSet( PicParSet[0], sps, 0, 0, 0, params->chroma_qp_index_offset, 0);
  74.       GeneratePictureParameterSet( PicParSet[1], sps, 1, 1, 1, params->chroma_qp_index_offset, 0);
  75.       GeneratePictureParameterSet( PicParSet[2], sps, 2, 1, 2, params->chroma_qp_index_offset, 0);
  76.     }
  77.   }
  78.   else
  79.   {
  80.     PicParSet[0] = AllocPPS();
  81.     if (IS_FREXT_PROFILE(sps->profile_idc))
  82.       GeneratePictureParameterSet( PicParSet[0], sps, 0, params->WeightedPrediction, params->WeightedBiprediction,
  83.                                    params->cb_qp_index_offset, params->cr_qp_index_offset);
  84.     else
  85.       GeneratePictureParameterSet( PicParSet[0], sps, 0, params->WeightedPrediction, params->WeightedBiprediction,
  86.                                    params->chroma_qp_index_offset, 0);
  87.   }
  88.   active_sps = sps;
  89.   active_pps = PicParSet[0];
  90. }
  91. /*!
  92. *************************************************************************************
  93. * brief
  94. *    frees global parameter sets active_sps and active_pps
  95. *
  96. * return
  97. *    A NALU containing the Sequence ParameterSet
  98. *
  99. *************************************************************************************
  100. */
  101. void FreeParameterSets (void)
  102. {
  103.   int i;
  104.   for (i=0; i<MAXPPS; i++)
  105.   {
  106.     if ( NULL != PicParSet[i])
  107.     {
  108.       FreePPS(PicParSet[i]);
  109.       PicParSet[i] = NULL;
  110.     }
  111.   }
  112.   FreeSPS (active_sps);
  113. }
  114. /*!
  115. *************************************************************************************
  116. * brief
  117. *    int GenerateSeq_parameter_set_NALU (void);
  118. *
  119. * note
  120. *    Uses the global variables through GenerateSequenceParameterSet()
  121. *    and GeneratePictureParameterSet
  122. *
  123. * return
  124. *    A NALU containing the Sequence ParameterSet
  125. *
  126. *************************************************************************************
  127. */
  128. NALU_t *GenerateSeq_parameter_set_NALU (void)
  129. {
  130.   NALU_t *n = AllocNALU(64000);
  131.   int RBSPlen = 0;
  132.   int NALUlen;
  133.   byte rbsp[MAXRBSPSIZE];
  134.   RBSPlen = GenerateSeq_parameter_set_rbsp (active_sps, rbsp);
  135.   NALUlen = RBSPtoNALU (rbsp, n, RBSPlen, NALU_TYPE_SPS, NALU_PRIORITY_HIGHEST, 0, 1);
  136.   n->startcodeprefix_len = 4;
  137.   return n;
  138. }
  139. /*!
  140. *************************************************************************************
  141. * brief
  142. *    NALU_t *GeneratePic_parameter_set_NALU (int PPS_id);
  143. *
  144. * note
  145. *    Uses the global variables through GenerateSequenceParameterSet()
  146. *    and GeneratePictureParameterSet
  147. *
  148. * return
  149. *    A NALU containing the Picture Parameter Set
  150. *
  151. *************************************************************************************
  152. */
  153. NALU_t *GeneratePic_parameter_set_NALU(int PPS_id)
  154. {
  155.   NALU_t *n = AllocNALU(64000);
  156.   int RBSPlen = 0;
  157.   int NALUlen;
  158.   byte rbsp[MAXRBSPSIZE];
  159.   RBSPlen = GeneratePic_parameter_set_rbsp (PicParSet[PPS_id], rbsp);
  160.   NALUlen = RBSPtoNALU (rbsp, n, RBSPlen, NALU_TYPE_PPS, NALU_PRIORITY_HIGHEST, 0, 1);
  161.   n->startcodeprefix_len = 4;
  162.   return n;
  163. }
  164. /*!
  165.  ************************************************************************
  166.  * brief
  167.  *    GenerateSequenceParameterSet: extracts info from global variables and
  168.  *    generates sequence parameter set structure
  169.  *
  170.  * par
  171.  *    Function reads all kinds of values from several global variables,
  172.  *    including params-> and image-> and fills in the sps.  Many
  173.  *    values are current hard-coded to defaults.
  174.  *
  175.  ************************************************************************
  176.  */
  177. void GenerateSequenceParameterSet( seq_parameter_set_rbsp_t *sps, //!< Sequence Parameter Set to be filled
  178.                                    int SPS_id                     //!< SPS ID
  179.                                    )
  180. {
  181.   unsigned i;
  182.   unsigned n_ScalingList;
  183.   int SubWidthC  [4]= { 1, 2, 2, 1};
  184.   int SubHeightC [4]= { 1, 2, 1, 1};
  185.   int frext_profile = ((IdentifyProfile()==FREXT_HP) ||
  186.                       (IdentifyProfile()==FREXT_Hi10P) ||
  187.                       (IdentifyProfile()==FREXT_Hi422) ||
  188.                       (IdentifyProfile()==FREXT_Hi444) ||
  189.                       (IdentifyProfile()==FREXT_CAVLC444));
  190.   // *************************************************************************
  191.   // Sequence Parameter Set
  192.   // *************************************************************************
  193.   assert (sps != NULL);
  194.   // Profile and Level should be calculated using the info from the config
  195.   // file.  Calculation is hidden in IndetifyProfile() and IdentifyLevel()
  196.   sps->profile_idc = IdentifyProfile();
  197.   sps->level_idc = IdentifyLevel();
  198.   // needs to be set according to profile
  199.   sps->constrained_set0_flag = FALSE;
  200.   sps->constrained_set1_flag = FALSE;
  201.   sps->constrained_set2_flag = FALSE;
  202.   if ( (sps->level_idc == 9) && !IS_FREXT_PROFILE(sps->profile_idc) ) // Level 1.b
  203.   {
  204.     sps->constrained_set3_flag = TRUE;
  205.     sps->level_idc = 11;
  206.   }
  207.   else if (frext_profile && params->IntraProfile)
  208.   {
  209.     sps->constrained_set3_flag = TRUE;
  210.   }
  211.   else
  212.   {
  213.     sps->constrained_set3_flag = FALSE;
  214.   }
  215.   // Parameter Set ID hard coded to zero
  216.   sps->seq_parameter_set_id = SPS_id;
  217.   // Fidelity Range Extensions stuff
  218.   sps->bit_depth_luma_minus8   = params->output.bit_depth[0] - 8;
  219.   sps->bit_depth_chroma_minus8 = params->output.bit_depth[1] - 8;
  220.   img->lossless_qpprime_flag = params->lossless_qpprime_y_zero_flag & 
  221.       (sps->profile_idc==FREXT_Hi444 || sps->profile_idc==FREXT_CAVLC444);
  222.   //! POC stuff:
  223.   //! The following values are hard-coded in init_poc().  Apparently,
  224.   //! the poc implementation covers only a subset of the poc functionality.
  225.   //! Here, the same subset is implemented.  Changes in the POC stuff have
  226.   //! also to be reflected here
  227.   sps->log2_max_frame_num_minus4 = log2_max_frame_num_minus4;
  228.   sps->log2_max_pic_order_cnt_lsb_minus4 = log2_max_pic_order_cnt_lsb_minus4;
  229.   sps->pic_order_cnt_type = params->pic_order_cnt_type;
  230.   sps->num_ref_frames_in_pic_order_cnt_cycle = img->num_ref_frames_in_pic_order_cnt_cycle;
  231.   sps->delta_pic_order_always_zero_flag = img->delta_pic_order_always_zero_flag;
  232.   sps->offset_for_non_ref_pic = img->offset_for_non_ref_pic;
  233.   sps->offset_for_top_to_bottom_field = img->offset_for_top_to_bottom_field;
  234.   for (i=0; i<img->num_ref_frames_in_pic_order_cnt_cycle; i++)
  235.   {
  236.     sps->offset_for_ref_frame[i] = img->offset_for_ref_frame[i];
  237.   }
  238.   // End of POC stuff
  239.   // Number of Reference Frames
  240.   sps->num_ref_frames = params->num_ref_frames;
  241.   //required_frame_num_update_behaviour_flag hardcoded to zero
  242.   sps->gaps_in_frame_num_value_allowed_flag = FALSE;    // double check
  243.   sps->frame_mbs_only_flag = (Boolean) !(params->PicInterlace || params->MbInterlace);
  244.   // Picture size, finally a simple one :-)
  245.   sps->pic_width_in_mbs_minus1        = (( params->output.width  + img->auto_crop_right)/16) -1;
  246.   sps->pic_height_in_map_units_minus1 = (((params->output.height + img->auto_crop_bottom)/16)/ (2 - sps->frame_mbs_only_flag)) - 1;
  247.   // a couple of flags, simple
  248.   sps->mb_adaptive_frame_field_flag = (Boolean) (FRAME_CODING != params->MbInterlace);
  249.   sps->direct_8x8_inference_flag = (Boolean) params->directInferenceFlag;
  250.   // Sequence VUI not implemented, signalled as not present
  251.   sps->vui_parameters_present_flag = (Boolean) ((params->rgb_input_flag && params->yuv_format==3) || params->EnableVUISupport);
  252.   sps->chroma_format_idc = params->yuv_format;
  253.   sps->separate_colour_plane_flag = ( sps->chroma_format_idc == YUV444 ) ? params->separate_colour_plane_flag : 0;
  254.   if ( sps->vui_parameters_present_flag )
  255.     GenerateVUIParameters(sps);
  256.   // This should be moved somewhere else.
  257.   {
  258.     int PicWidthInMbs, PicHeightInMapUnits, FrameHeightInMbs;
  259.     int width, height;
  260.     int nplane;
  261.     PicWidthInMbs = (sps->pic_width_in_mbs_minus1 +1);
  262.     PicHeightInMapUnits = (sps->pic_height_in_map_units_minus1 +1);
  263.     FrameHeightInMbs = ( 2 - sps->frame_mbs_only_flag ) * PicHeightInMapUnits;
  264.     width = PicWidthInMbs * MB_BLOCK_SIZE;
  265.     height = FrameHeightInMbs * MB_BLOCK_SIZE;
  266.     if( IS_INDEPENDENT(params) )
  267.     {
  268.       for( nplane=0; nplane<MAX_PLANE; nplane++ )
  269.       {
  270.         Co_located_JV[nplane] = alloc_colocated (width, height,sps->mb_adaptive_frame_field_flag);           
  271.       }
  272.     }
  273.     else
  274.     {
  275.       Co_located = alloc_colocated (width, height,sps->mb_adaptive_frame_field_flag);
  276.     }
  277.   }
  278.   // Fidelity Range Extensions stuff
  279.   if(frext_profile)
  280.   {
  281.     sps->seq_scaling_matrix_present_flag = (Boolean) (params->ScalingMatrixPresentFlag&1);
  282.     n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
  283.     for(i=0; i<n_ScalingList; i++)
  284.     {
  285.       if(i<6)
  286.         sps->seq_scaling_list_present_flag[i] = (params->ScalingListPresentFlag[i]&1);
  287.       else
  288.       {
  289.         if(params->Transform8x8Mode)
  290.           sps->seq_scaling_list_present_flag[i] = (params->ScalingListPresentFlag[i]&1);
  291.         else
  292.           sps->seq_scaling_list_present_flag[i] = 0;
  293.       }
  294.       if( sps->seq_scaling_matrix_present_flag == FALSE )
  295.         sps->seq_scaling_list_present_flag[i] = 0;
  296.     }
  297.   }
  298.   else
  299.   {
  300.     sps->seq_scaling_matrix_present_flag = FALSE;
  301.     for(i=0; i<12; i++)
  302.       sps->seq_scaling_list_present_flag[i] = 0;
  303.   }
  304.   if (img->auto_crop_right || img->auto_crop_bottom)
  305.   {
  306.     sps->frame_cropping_flag = TRUE;
  307.     sps->frame_cropping_rect_left_offset=0;
  308.     sps->frame_cropping_rect_top_offset=0;
  309.     sps->frame_cropping_rect_right_offset=  (img->auto_crop_right / SubWidthC[sps->chroma_format_idc]);
  310.     sps->frame_cropping_rect_bottom_offset= (img->auto_crop_bottom / (SubHeightC[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag)));
  311.     if (img->auto_crop_right % SubWidthC[sps->chroma_format_idc])
  312.     {
  313.       error("automatic frame cropping (width) not possible",500);
  314.     }
  315.     if (img->auto_crop_bottom % (SubHeightC[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag)))
  316.     {
  317.       error("automatic frame cropping (height) not possible",500);
  318.     }
  319.   }
  320.   else
  321.   {
  322.     sps->frame_cropping_flag = FALSE;
  323.   }
  324. }
  325. /*!
  326.  ************************************************************************
  327.  * brief
  328.  *    GeneratePictureParameterSet:
  329.  *    Generates a Picture Parameter Set structure
  330.  *
  331.  * par
  332.  *    Regarding the QP
  333.  *    The previous software versions coded the absolute QP only in the
  334.  *    slice header.  This is kept, and the offset in the PPS is coded
  335.  *    even if we could save bits by intelligently using this field.
  336.  *
  337.  ************************************************************************
  338.  */
  339. void GeneratePictureParameterSet( pic_parameter_set_rbsp_t *pps, //!< Picture Parameter Set to be filled
  340.                                   seq_parameter_set_rbsp_t *sps, //!< used Sequence Parameter Set
  341.                                   int PPS_id,                    //!< PPS ID
  342.                                   int WeightedPrediction,        //!< value of weighted_pred_flag
  343.                                   int WeightedBiprediction,      //!< value of weighted_bipred_idc
  344.                                   int cb_qp_index_offset,        //!< value of cb_qp_index_offset
  345.                                   int cr_qp_index_offset         //!< value of cr_qp_index_offset
  346.                                   )
  347. {
  348.   unsigned i;
  349.   unsigned n_ScalingList;
  350.   int frext_profile = ((IdentifyProfile()==FREXT_HP) ||
  351.                       (IdentifyProfile()==FREXT_Hi10P) ||
  352.                       (IdentifyProfile()==FREXT_Hi422) ||
  353.                       (IdentifyProfile()==FREXT_Hi444) ||
  354.                       (IdentifyProfile()==FREXT_CAVLC444));
  355.   // *************************************************************************
  356.   // Picture Parameter Set
  357.   // *************************************************************************
  358.   pps->seq_parameter_set_id = sps->seq_parameter_set_id;
  359.   pps->pic_parameter_set_id = PPS_id;
  360.   pps->entropy_coding_mode_flag = (params->symbol_mode == CAVLC ? FALSE : TRUE);
  361.   // Fidelity Range Extensions stuff
  362.   if(frext_profile)
  363.   {
  364.     pps->transform_8x8_mode_flag = (params->Transform8x8Mode ? TRUE:FALSE);
  365.     pps->pic_scaling_matrix_present_flag = (Boolean) ((params->ScalingMatrixPresentFlag&2)>>1);
  366.     n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
  367.     for(i=0; i<n_ScalingList; i++)
  368.     {
  369.       if(i<6)
  370.         pps->pic_scaling_list_present_flag[i] = (params->ScalingListPresentFlag[i]&2)>>1;
  371.       else
  372.       {
  373.         if(pps->transform_8x8_mode_flag)
  374.           pps->pic_scaling_list_present_flag[i] = (params->ScalingListPresentFlag[i]&2)>>1;
  375.         else
  376.           pps->pic_scaling_list_present_flag[i] = 0;
  377.       }
  378.       if( pps->pic_scaling_matrix_present_flag == FALSE )
  379.         pps->pic_scaling_list_present_flag[i] = 0;
  380.     }
  381.   }
  382.   else
  383.   {
  384.     pps->pic_scaling_matrix_present_flag = FALSE;
  385.     for(i=0; i<12; i++)
  386.       pps->pic_scaling_list_present_flag[i] = 0;
  387.     pps->transform_8x8_mode_flag = FALSE;
  388.     params->Transform8x8Mode = 0;
  389.   }
  390.   // JVT-Fxxx (by Stephan Wenger, make this flag unconditional
  391.   pps->pic_order_present_flag = img->pic_order_present_flag;
  392.   // Begin FMO stuff
  393.   pps->num_slice_groups_minus1 = params->num_slice_groups_minus1;
  394.   //! Following set the parameter for different slice group types
  395.   if (pps->num_slice_groups_minus1 > 0)
  396.   {
  397.      if ((pps->slice_group_id = calloc ((sps->pic_height_in_map_units_minus1+1)*(sps->pic_width_in_mbs_minus1+1), sizeof(byte))) == NULL)
  398.        no_mem_exit ("GeneratePictureParameterSet: slice_group_id");
  399.     switch (params->slice_group_map_type)
  400.     {
  401.     case 0:
  402.       pps->slice_group_map_type = 0;
  403.       for(i=0; i<=pps->num_slice_groups_minus1; i++)
  404.       {
  405.         pps->run_length_minus1[i]=params->run_length_minus1[i];
  406.       }
  407.       break;
  408.     case 1:
  409.       pps->slice_group_map_type = 1;
  410.       break;
  411.     case 2:
  412.       // i loops from 0 to num_slice_groups_minus1-1, because no info for background needed
  413.       pps->slice_group_map_type = 2;
  414.       for(i=0; i<pps->num_slice_groups_minus1; i++)
  415.       {
  416.         pps->top_left[i] = params->top_left[i];
  417.         pps->bottom_right[i] = params->bottom_right[i];
  418.       }
  419.      break;
  420.     case 3:
  421.     case 4:
  422.     case 5:
  423.       pps->slice_group_map_type = params->slice_group_map_type;
  424.       pps->slice_group_change_direction_flag = (Boolean) params->slice_group_change_direction_flag;
  425.       pps->slice_group_change_rate_minus1 = params->slice_group_change_rate_minus1;
  426.       break;
  427.     case 6:
  428.       pps->slice_group_map_type = 6;
  429.       pps->pic_size_in_map_units_minus1 =
  430.         (((params->output.height + img->auto_crop_bottom)/MB_BLOCK_SIZE)/(2-sps->frame_mbs_only_flag))
  431.         *((params->output.width  + img->auto_crop_right)/MB_BLOCK_SIZE) -1;
  432.       for (i=0;i<=pps->pic_size_in_map_units_minus1; i++)
  433.         pps->slice_group_id[i] = params->slice_group_id[i];
  434.       break;
  435.     default:
  436.       printf ("Parset.c: slice_group_map_type invalid, defaultn");
  437.       assert (0==1);
  438.     }
  439.   }
  440. // End FMO stuff
  441.   pps->num_ref_idx_l0_active_minus1 = sps->frame_mbs_only_flag ? (sps->num_ref_frames-1) : (2 * sps->num_ref_frames - 1) ;   // set defaults
  442.   pps->num_ref_idx_l1_active_minus1 = sps->frame_mbs_only_flag ? (sps->num_ref_frames-1) : (2 * sps->num_ref_frames - 1) ;   // set defaults
  443.   pps->weighted_pred_flag = (Boolean) WeightedPrediction;
  444.   pps->weighted_bipred_idc = WeightedBiprediction;
  445.   pps->pic_init_qp_minus26 = 0;         // hard coded to zero, QP lives in the slice header
  446.   pps->pic_init_qs_minus26 = 0;
  447.   pps->chroma_qp_index_offset = cb_qp_index_offset;
  448.   if (frext_profile)
  449.   {
  450.     pps->cb_qp_index_offset     = cb_qp_index_offset;
  451.     pps->cr_qp_index_offset     = cr_qp_index_offset;
  452.   }
  453.   else
  454.     pps->cb_qp_index_offset = pps->cr_qp_index_offset = pps->chroma_qp_index_offset;
  455.   pps->deblocking_filter_control_present_flag = (Boolean) params->DFSendParameters;
  456.   pps->constrained_intra_pred_flag = (Boolean) params->UseConstrainedIntraPred;
  457.   // if redundant slice is in use.
  458.   pps->redundant_pic_cnt_present_flag = (Boolean) params->redundant_pic_flag;
  459. }
  460. /*!
  461.  *************************************************************************************
  462.  * brief
  463.  *    syntax for scaling list matrix values
  464.  *
  465.  * param scalingListinput
  466.  *    input scaling list
  467.  * param scalingList
  468.  *    scaling list to be used
  469.  * param sizeOfScalingList
  470.  *    size of the scaling list
  471.  * param UseDefaultScalingMatrix
  472.  *    usage of default Scaling Matrix
  473.  * param bitstream
  474.  *    target bitstream for writing syntax
  475.  *
  476.  * return
  477.  *    size of the RBSP in bytes
  478.  *
  479.  *************************************************************************************
  480.  */
  481. int Scaling_List(short *scalingListinput, short *scalingList, int sizeOfScalingList, short *UseDefaultScalingMatrix, Bitstream *bitstream)
  482. {
  483.   int j, scanj;
  484.   int len=0;
  485.   int delta_scale, lastScale, nextScale;
  486.   lastScale = 8;
  487.   nextScale = 8;
  488.   for(j=0; j<sizeOfScalingList; j++)
  489.   {
  490.     scanj = (sizeOfScalingList==16) ? ZZ_SCAN[j]:ZZ_SCAN8[j];
  491.     if(nextScale!=0)
  492.     {
  493.       delta_scale = scalingListinput[scanj]-lastScale; // Calculate delta from the scalingList data from the input file
  494.       if(delta_scale>127)
  495.         delta_scale=delta_scale-256;
  496.       else if(delta_scale<-128)
  497.         delta_scale=delta_scale+256;
  498.       len+=se_v ("   : delta_sl   ",                      delta_scale,                       bitstream);
  499.       nextScale = scalingListinput[scanj];
  500.       *UseDefaultScalingMatrix|=(scanj==0 && nextScale==0); // Check first matrix value for zero
  501.     }
  502.     scalingList[scanj] = (short) ((nextScale==0) ? lastScale:nextScale); // Update the actual scalingList matrix with the correct values
  503.     lastScale = scalingList[scanj];
  504.   }
  505.   return len;
  506. }
  507. /*!
  508.  *************************************************************************************
  509.  * brief
  510.  *    int GenerateSeq_parameter_set_rbsp (seq_parameter_set_rbsp_t *sps, char *rbsp);
  511.  *
  512.  * param sps
  513.  *    sequence parameter structure
  514.  * param rbsp
  515.  *    buffer to be filled with the rbsp, size should be at least MAXIMUMPARSETRBSPSIZE
  516.  *
  517.  * return
  518.  *    size of the RBSP in bytes
  519.  *
  520.  * note
  521.  *    Sequence Parameter VUI function is called, but the function implements
  522.  *    an exit (-1)
  523.  *************************************************************************************
  524.  */
  525. int GenerateSeq_parameter_set_rbsp (seq_parameter_set_rbsp_t *sps, byte *rbsp)
  526. {
  527.   Bitstream *bitstream;
  528.   int len = 0, LenInBytes;
  529.   unsigned i;
  530.   unsigned n_ScalingList;
  531.   assert (rbsp != NULL);
  532.   if ((bitstream=calloc(1, sizeof(Bitstream)))==NULL) no_mem_exit("SeqParameterSet:bitstream");
  533.   // .. and use the rbsp provided (or allocated above) for the data
  534.   bitstream->streamBuffer = rbsp;
  535.   bitstream->bits_to_go = 8;
  536.   len+=u_v  (8, "SPS: profile_idc",                             sps->profile_idc,                               bitstream);
  537.   len+=u_1  ("SPS: constrained_set0_flag",                      sps->constrained_set0_flag,    bitstream);
  538.   len+=u_1  ("SPS: constrained_set1_flag",                      sps->constrained_set1_flag,    bitstream);
  539.   len+=u_1  ("SPS: constrained_set2_flag",                      sps->constrained_set2_flag,    bitstream);
  540.   len+=u_1  ("SPS: constrained_set3_flag",                      sps->constrained_set3_flag,    bitstream);
  541.   len+=u_v  (4, "SPS: reserved_zero_4bits",                     0,                             bitstream);
  542.   len+=u_v  (8, "SPS: level_idc",                               sps->level_idc,                                 bitstream);
  543.   len+=ue_v ("SPS: seq_parameter_set_id",                    sps->seq_parameter_set_id,                      bitstream);
  544.   // Fidelity Range Extensions stuff
  545.   if( IS_FREXT_PROFILE(sps->profile_idc) )
  546.   {
  547.     len+=ue_v ("SPS: chroma_format_idc",                        sps->chroma_format_idc,                          bitstream);
  548.     if(img->yuv_format == YUV444)
  549.       len+=u_1  ("SPS: separate_colour_plane_flag",             sps->separate_colour_plane_flag,                 bitstream);
  550.     len+=ue_v ("SPS: bit_depth_luma_minus8",                    sps->bit_depth_luma_minus8,                      bitstream);
  551.     len+=ue_v ("SPS: bit_depth_chroma_minus8",                  sps->bit_depth_chroma_minus8,                    bitstream);
  552.     len+=u_1  ("SPS: lossless_qpprime_y_zero_flag",             img->lossless_qpprime_flag,                      bitstream);
  553.     //other chroma info to be added in the future
  554.     len+=u_1 ("SPS: seq_scaling_matrix_present_flag",           sps->seq_scaling_matrix_present_flag,            bitstream);
  555.     if(sps->seq_scaling_matrix_present_flag)
  556.     {
  557.       n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
  558.       for(i=0; i<n_ScalingList; i++)
  559.       {
  560.         len+=u_1 ("SPS: seq_scaling_list_present_flag",         sps->seq_scaling_list_present_flag[i],           bitstream);
  561.         if(sps->seq_scaling_list_present_flag[i])
  562.         {
  563.           if(i<6)
  564.             len+=Scaling_List(ScalingList4x4input[i], ScalingList4x4[i], 16, &UseDefaultScalingMatrix4x4Flag[i], bitstream);
  565.           else
  566.             len+=Scaling_List(ScalingList8x8input[i-6], ScalingList8x8[i-6], 64, &UseDefaultScalingMatrix8x8Flag[i-6], bitstream);
  567.         }
  568.       }
  569.     }
  570.   }
  571.   len+=ue_v ("SPS: log2_max_frame_num_minus4",               sps->log2_max_frame_num_minus4,                 bitstream);
  572.   len+=ue_v ("SPS: pic_order_cnt_type",                      sps->pic_order_cnt_type,                        bitstream);
  573.   if (sps->pic_order_cnt_type == 0)
  574.     len+=ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4",     sps->log2_max_pic_order_cnt_lsb_minus4,         bitstream);
  575.   else if (sps->pic_order_cnt_type == 1)
  576.   {
  577.     len+=u_1  ("SPS: delta_pic_order_always_zero_flag",        sps->delta_pic_order_always_zero_flag,          bitstream);
  578.     len+=se_v ("SPS: offset_for_non_ref_pic",                  sps->offset_for_non_ref_pic,                    bitstream);
  579.     len+=se_v ("SPS: offset_for_top_to_bottom_field",          sps->offset_for_top_to_bottom_field,            bitstream);
  580.     len+=ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle",   sps->num_ref_frames_in_pic_order_cnt_cycle,     bitstream);
  581.     for (i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
  582.       len+=se_v ("SPS: offset_for_ref_frame",                  sps->offset_for_ref_frame[i],                      bitstream);
  583.   }
  584.   len+=ue_v ("SPS: num_ref_frames",                          sps->num_ref_frames,                            bitstream);
  585.   len+=u_1  ("SPS: gaps_in_frame_num_value_allowed_flag",    sps->gaps_in_frame_num_value_allowed_flag,      bitstream);
  586.   len+=ue_v ("SPS: pic_width_in_mbs_minus1",                 sps->pic_width_in_mbs_minus1,                   bitstream);
  587.   len+=ue_v ("SPS: pic_height_in_map_units_minus1",          sps->pic_height_in_map_units_minus1,            bitstream);
  588.   len+=u_1  ("SPS: frame_mbs_only_flag",                     sps->frame_mbs_only_flag,                       bitstream);
  589.   if (!sps->frame_mbs_only_flag)
  590.   {
  591.     len+=u_1  ("SPS: mb_adaptive_frame_field_flag",            sps->mb_adaptive_frame_field_flag,              bitstream);
  592.   }
  593.   len+=u_1  ("SPS: direct_8x8_inference_flag",               sps->direct_8x8_inference_flag,                 bitstream);
  594.   len+=u_1  ("SPS: frame_cropping_flag",                      sps->frame_cropping_flag,                       bitstream);
  595.   if (sps->frame_cropping_flag)
  596.   {
  597.     len+=ue_v ("SPS: frame_cropping_rect_left_offset",          sps->frame_cropping_rect_left_offset,           bitstream);
  598.     len+=ue_v ("SPS: frame_cropping_rect_right_offset",         sps->frame_cropping_rect_right_offset,          bitstream);
  599.     len+=ue_v ("SPS: frame_cropping_rect_top_offset",           sps->frame_cropping_rect_top_offset,            bitstream);
  600.     len+=ue_v ("SPS: frame_cropping_rect_bottom_offset",        sps->frame_cropping_rect_bottom_offset,         bitstream);
  601.   }
  602.   len+=u_1  ("SPS: vui_parameters_present_flag",             sps->vui_parameters_present_flag,               bitstream);
  603.   if (sps->vui_parameters_present_flag)
  604.     len+=GenerateVUI_parameters_rbsp(sps, bitstream);    // currently a dummy, asserting
  605.   SODBtoRBSP(bitstream);     // copies the last couple of bits into the byte buffer
  606.   LenInBytes=bitstream->byte_pos;
  607.   free (bitstream);
  608.   return LenInBytes;
  609. }
  610. /*!
  611.  ***********************************************************************************************
  612.  * brief
  613.  *    int GeneratePic_parameter_set_rbsp (pic_parameter_set_rbsp_t *sps, char *rbsp);
  614.  *
  615.  * param pps
  616.  *    picture parameter structure
  617.  * param rbsp
  618.  *    buffer to be filled with the rbsp, size should be at least MAXIMUMPARSETRBSPSIZE
  619.  *
  620.  * return
  621.  *    size of the RBSP in bytes, negative in case of an error
  622.  *
  623.  * note
  624.  *    Picture Parameter VUI function is called, but the function implements
  625.  *    an exit (-1)
  626.  ************************************************************************************************
  627.  */
  628. int GeneratePic_parameter_set_rbsp (pic_parameter_set_rbsp_t *pps, byte *rbsp)
  629. {
  630.   Bitstream *bitstream;
  631.   int len = 0, LenInBytes;
  632.   unsigned i;
  633.   unsigned n_ScalingList;
  634.   unsigned NumberBitsPerSliceGroupId;
  635.   int profile_idc;
  636.   assert (rbsp != NULL);
  637.   if ((bitstream=calloc(1, sizeof(Bitstream)))==NULL) no_mem_exit("PicParameterSet:bitstream");
  638.   // .. and use the rbsp provided (or allocated above) for the data
  639.   bitstream->streamBuffer = rbsp;
  640.   bitstream->bits_to_go = 8;
  641.   pps->pic_order_present_flag = img->pic_order_present_flag;
  642.   len+=ue_v ("PPS: pic_parameter_set_id",                    pps->pic_parameter_set_id,                      bitstream);
  643.   len+=ue_v ("PPS: seq_parameter_set_id",                    pps->seq_parameter_set_id,                      bitstream);
  644.   len+=u_1  ("PPS: entropy_coding_mode_flag",                pps->entropy_coding_mode_flag,                  bitstream);
  645.   len+=u_1  ("PPS: pic_order_present_flag",                  pps->pic_order_present_flag,                    bitstream);
  646.   len+=ue_v ("PPS: num_slice_groups_minus1",                 pps->num_slice_groups_minus1,                   bitstream);
  647.   // FMO stuff
  648.   if(pps->num_slice_groups_minus1 > 0 )
  649.   {
  650.     len+=ue_v ("PPS: slice_group_map_type",                 pps->slice_group_map_type,                   bitstream);
  651.     if (pps->slice_group_map_type == 0)
  652.       for (i=0; i<=pps->num_slice_groups_minus1; i++)
  653.         len+=ue_v ("PPS: run_length_minus1[i]",                           pps->run_length_minus1[i],                             bitstream);
  654.     else if (pps->slice_group_map_type==2)
  655.       for (i=0; i<pps->num_slice_groups_minus1; i++)
  656.       {
  657.         len+=ue_v ("PPS: top_left[i]",                          pps->top_left[i],                           bitstream);
  658.         len+=ue_v ("PPS: bottom_right[i]",                      pps->bottom_right[i],                       bitstream);
  659.       }
  660.     else if (pps->slice_group_map_type == 3 ||
  661.              pps->slice_group_map_type == 4 ||
  662.              pps->slice_group_map_type == 5)
  663.     {
  664.       len+=u_1  ("PPS: slice_group_change_direction_flag",         pps->slice_group_change_direction_flag,         bitstream);
  665.       len+=ue_v ("PPS: slice_group_change_rate_minus1",            pps->slice_group_change_rate_minus1,            bitstream);
  666.     }
  667.     else if (pps->slice_group_map_type == 6)
  668.     {
  669.       if (pps->num_slice_groups_minus1>=4)
  670.         NumberBitsPerSliceGroupId=3;
  671.       else if (pps->num_slice_groups_minus1>=2)
  672.         NumberBitsPerSliceGroupId=2;
  673.       else if (pps->num_slice_groups_minus1>=1)
  674.         NumberBitsPerSliceGroupId=1;
  675.       else
  676.         NumberBitsPerSliceGroupId=0;
  677.       len+=ue_v ("PPS: pic_size_in_map_units_minus1",                       pps->pic_size_in_map_units_minus1,             bitstream);
  678.       for(i=0; i<=pps->pic_size_in_map_units_minus1; i++)
  679.         len+= u_v  (NumberBitsPerSliceGroupId, "PPS: >slice_group_id[i]",   pps->slice_group_id[i],                        bitstream);
  680.     }
  681.   }
  682.   // End of FMO stuff
  683.   len+=ue_v ("PPS: num_ref_idx_l0_active_minus1",             pps->num_ref_idx_l0_active_minus1,              bitstream);
  684.   len+=ue_v ("PPS: num_ref_idx_l1_active_minus1",             pps->num_ref_idx_l1_active_minus1,              bitstream);
  685.   len+=u_1  ("PPS: weighted_pred_flag",                       pps->weighted_pred_flag,                        bitstream);
  686.   len+=u_v  (2, "PPS: weighted_bipred_idc",                   pps->weighted_bipred_idc,                       bitstream);
  687.   len+=se_v ("PPS: pic_init_qp_minus26",                      pps->pic_init_qp_minus26,                       bitstream);
  688.   len+=se_v ("PPS: pic_init_qs_minus26",                      pps->pic_init_qs_minus26,                       bitstream);
  689.   profile_idc = IdentifyProfile();
  690.   if( IS_FREXT_PROFILE(profile_idc) )
  691.     len+=se_v ("PPS: chroma_qp_index_offset",                 pps->cb_qp_index_offset,                        bitstream);
  692.   else
  693.     len+=se_v ("PPS: chroma_qp_index_offset",                 pps->chroma_qp_index_offset,                    bitstream);
  694.   len+=u_1  ("PPS: deblocking_filter_control_present_flag",   pps->deblocking_filter_control_present_flag,    bitstream);
  695.   len+=u_1  ("PPS: constrained_intra_pred_flag",              pps->constrained_intra_pred_flag,               bitstream);
  696.   len+=u_1  ("PPS: redundant_pic_cnt_present_flag",           pps->redundant_pic_cnt_present_flag,            bitstream);
  697.   // Fidelity Range Extensions stuff
  698.   if( IS_FREXT_PROFILE(profile_idc) )
  699.   {
  700.     len+=u_1  ("PPS: transform_8x8_mode_flag",                pps->transform_8x8_mode_flag,                   bitstream);
  701.     len+=u_1  ("PPS: pic_scaling_matrix_present_flag",        pps->pic_scaling_matrix_present_flag,           bitstream);
  702.     if(pps->pic_scaling_matrix_present_flag)
  703.     {
  704.       n_ScalingList = 6 + ((active_sps->chroma_format_idc != 3) ? 2 : 6) * pps->transform_8x8_mode_flag;
  705.       for(i=0; i<n_ScalingList; i++)  // SS-70226
  706.       {
  707.         len+=u_1  ("PPS: pic_scaling_list_present_flag",      pps->pic_scaling_list_present_flag[i],          bitstream);
  708.         if(pps->pic_scaling_list_present_flag[i])
  709.         {
  710.           if(i<6)
  711.             len+=Scaling_List(ScalingList4x4input[i], ScalingList4x4[i], 16, &UseDefaultScalingMatrix4x4Flag[i], bitstream);
  712.           else
  713.             len+=Scaling_List(ScalingList8x8input[i-6], ScalingList8x8[i-6], 64, &UseDefaultScalingMatrix8x8Flag[i-6], bitstream);
  714.         }
  715.       }
  716.     }
  717.     len+=se_v ("PPS: second_chroma_qp_index_offset",          pps->cr_qp_index_offset,                        bitstream);
  718.   }
  719.   SODBtoRBSP(bitstream);     // copies the last couple of bits into the byte buffer
  720.   LenInBytes=bitstream->byte_pos;
  721.   // Get rid of the helper structures
  722.   free (bitstream);
  723.   return LenInBytes;
  724. }
  725. /*!
  726.  *************************************************************************************
  727.  * brief
  728.  *    Returns the Profile
  729.  *
  730.  * return
  731.  *    Profile according to Annex A
  732.  *
  733.  * note
  734.  *    Function is currently a dummy.  Should "calculate" the profile from those
  735.  *    config file parameters.  E.g.
  736.  *
  737.  *    Profile = Baseline;
  738.  *    if (CABAC Used || Interlace used) Profile=Main;
  739.  *    if (!Cabac Used) && (Bframes | SPframes) Profile = Streaming;
  740.  *
  741.  *************************************************************************************
  742.  */
  743. int IdentifyProfile(void)
  744. {
  745.   return params->ProfileIDC;
  746. }
  747. /*!
  748.  *************************************************************************************
  749.  * brief
  750.  *    Returns the Level
  751.  *
  752.  * return
  753.  *    Level according to Annex A
  754.  *
  755.  * note
  756.  *    This function is currently a dummy, but should calculate the level out of
  757.  *    the config file parameters (primarily the picture size)
  758.  *************************************************************************************
  759.  */
  760. int IdentifyLevel(void)
  761. {
  762.   return params->LevelIDC;
  763. }
  764. /*!
  765.  *************************************************************************************
  766.  * brief
  767.  *    Function body for VUI Parameter generation (to be done)
  768.  *
  769.  * return
  770.  *    exits with error message
  771.  *************************************************************************************
  772.  */
  773. static int GenerateVUI_parameters_rbsp(seq_parameter_set_rbsp_t *sps, Bitstream *bitstream)
  774. {
  775.   int len=0;
  776.   vui_seq_parameters_t *vui_seq_parameters = &(sps->vui_seq_parameters);
  777.   len+=u_1 ("VUI: aspect_ratio_info_present_flag", vui_seq_parameters->aspect_ratio_info_present_flag, bitstream);
  778.   if (vui_seq_parameters->aspect_ratio_info_present_flag)
  779.   {        
  780.     len+=u_v (8,"VUI: aspect_ratio_idc", vui_seq_parameters->aspect_ratio_idc, bitstream);
  781.     if (vui_seq_parameters->aspect_ratio_idc == 255)
  782.     {
  783.       len+=u_v (16,"VUI: sar_width",  vui_seq_parameters->sar_width,  bitstream);
  784.       len+=u_v (16,"VUI: sar_height", vui_seq_parameters->sar_height, bitstream);
  785.     }
  786.   }  
  787.   len+=u_1 ("VUI: overscan_info_present_flag", vui_seq_parameters->overscan_info_present_flag, bitstream);
  788.   if (vui_seq_parameters->overscan_info_present_flag)
  789.   {
  790.     len+=u_1 ("VUI: overscan_appropriate_flag", vui_seq_parameters->overscan_appropriate_flag, bitstream);
  791.   } 
  792.   len+=u_1 ("VUI: video_signal_type_present_flag", vui_seq_parameters->video_signal_type_present_flag, bitstream);
  793.   if (vui_seq_parameters->video_signal_type_present_flag)
  794.   {
  795.     len+=u_v (3,"VUI: video_format", vui_seq_parameters->video_format, bitstream);
  796.     len+=u_1 ("VUI: video_full_range_flag", vui_seq_parameters->video_full_range_flag, bitstream);
  797.     len+=u_1 ("VUI: colour_description_present_flag", vui_seq_parameters->colour_description_present_flag, bitstream);
  798.     if (vui_seq_parameters->colour_description_present_flag)
  799.     {
  800.       len+=u_v (8,"VUI: colour_primaries", vui_seq_parameters->colour_primaries, bitstream);
  801.       len+=u_v (8,"VUI: transfer_characteristics", vui_seq_parameters->transfer_characteristics, bitstream);
  802.       len+=u_v (8,"VUI: matrix_coefficients", vui_seq_parameters->matrix_coefficients, bitstream);
  803.     }
  804.   }
  805.   len+=u_1 ("VUI: chroma_loc_info_present_flag", vui_seq_parameters->chroma_location_info_present_flag, bitstream);
  806.   if (vui_seq_parameters->chroma_location_info_present_flag)
  807.   {
  808.     len+=ue_v ("VUI: chroma_sample_loc_type_top_field", vui_seq_parameters->chroma_sample_loc_type_top_field, bitstream);
  809.     len+=ue_v ("VUI: chroma_sample_loc_type_bottom_field", vui_seq_parameters->chroma_sample_loc_type_bottom_field, bitstream);
  810.   }
  811.   len+=u_1 ("VUI: timing_info_present_flag", vui_seq_parameters->timing_info_present_flag, bitstream);
  812.   // timing parameters
  813.   if (vui_seq_parameters->timing_info_present_flag)
  814.   {
  815.     len+=u_v (32,"VUI: num_units_in_tick",  vui_seq_parameters->num_units_in_tick, bitstream);
  816.     len+=u_v (32,"VUI: time_scale",         vui_seq_parameters->time_scale, bitstream);
  817.     len+=u_1 ("VUI: fixed_frame_rate_flag", vui_seq_parameters->fixed_frame_rate_flag, bitstream);
  818.   }
  819.   // end of timing parameters
  820.   // nal_hrd_parameters_present_flag
  821.   len+=u_1 ("VUI: nal_hrd_parameters_present_flag", vui_seq_parameters->nal_hrd_parameters_present_flag, bitstream);
  822.   if ( vui_seq_parameters->nal_hrd_parameters_present_flag )
  823.   {
  824.     len += WriteHRDParameters(sps, bitstream);
  825.   }
  826.   // vcl_hrd_parameters_present_flag
  827.   len+=u_1 ("VUI: vcl_hrd_parameters_present_flag", vui_seq_parameters->vcl_hrd_parameters_present_flag, bitstream);
  828.   if ( vui_seq_parameters->vcl_hrd_parameters_present_flag )
  829.   {
  830.     len += WriteHRDParameters(sps, bitstream);
  831.   }
  832.   if ( vui_seq_parameters->nal_hrd_parameters_present_flag || vui_seq_parameters->vcl_hrd_parameters_present_flag )
  833.   {
  834.     len+=u_1 ("VUI: low_delay_hrd_flag", vui_seq_parameters->low_delay_hrd_flag, bitstream );
  835.   }
  836.   len+=u_1 ("VUI: pic_struct_present_flag", vui_seq_parameters->pic_struct_present_flag, bitstream);
  837.   len+=u_1 ("VUI: bitstream_restriction_flag", vui_seq_parameters->bitstream_restriction_flag, bitstream);
  838.   if (vui_seq_parameters->bitstream_restriction_flag)
  839.   {
  840.     len+=u_1  ("VUI: motion_vectors_over_pic_boundaries_flag", vui_seq_parameters->motion_vectors_over_pic_boundaries_flag, bitstream);
  841.     len+=ue_v ("VUI: max_bytes_per_pic_denom", vui_seq_parameters->max_bytes_per_pic_denom, bitstream);
  842.     len+=ue_v ("VUI: max_bits_per_mb_denom", vui_seq_parameters->max_bits_per_mb_denom, bitstream);
  843.     len+=ue_v ("VUI: log2_max_mv_length_horizontal", vui_seq_parameters->log2_max_mv_length_horizontal, bitstream);
  844.     len+=ue_v ("VUI: log2_max_mv_length_vertical", vui_seq_parameters->log2_max_mv_length_vertical, bitstream);
  845.     len+=ue_v ("VUI: num_reorder_frames", vui_seq_parameters->num_reorder_frames, bitstream);
  846.     len+=ue_v ("VUI: max_dec_frame_buffering", vui_seq_parameters->max_dec_frame_buffering, bitstream);
  847.   }
  848.   return len;
  849. }
  850. /*!
  851.  *************************************************************************************
  852.  * brief
  853.  *    Function body for SEI message NALU generation
  854.  *
  855.  * return
  856.  *    A NALU containing the SEI messages
  857.  *
  858.  *************************************************************************************
  859.  */
  860. NALU_t *GenerateSEImessage_NALU()
  861. {
  862.   NALU_t *n = AllocNALU(64000);
  863.   int RBSPlen = 0;
  864.   int NALUlen;
  865.   byte rbsp[MAXRBSPSIZE];
  866.   RBSPlen = GenerateSEImessage_rbsp (NORMAL_SEI, rbsp);
  867.   NALUlen = RBSPtoNALU (rbsp, n, RBSPlen, NALU_TYPE_SEI, NALU_PRIORITY_DISPOSABLE, 0, 1);
  868.   n->startcodeprefix_len = 4;
  869.   return n;
  870. }
  871. /*!
  872.  *************************************************************************************
  873.  * brief
  874.  *    int GenerateSEImessage_rbsp (int, bufferingperiod_information_struct*, char*)
  875.  *
  876.  *
  877.  * return
  878.  *    size of the RBSP in bytes, negative in case of an error
  879.  *
  880.  * note
  881.  *************************************************************************************
  882.  */
  883. int GenerateSEImessage_rbsp (int id, byte *rbsp)
  884. {
  885.   Bitstream *bitstream;
  886.   int len = 0, LenInBytes;
  887.   assert (rbsp != NULL);
  888.   if ((bitstream=calloc(1, sizeof(Bitstream)))==NULL) 
  889.     no_mem_exit("SeqParameterSet:bitstream");
  890.   // .. and use the rbsp provided (or allocated above) for the data
  891.   bitstream->streamBuffer = rbsp;
  892.   bitstream->bits_to_go = 8;
  893.   {
  894.     char sei_message[500] = "";
  895.     char uuid_message[9] = "RandomMSG"; // This is supposed to be Random
  896.     unsigned int i, message_size = strlen(params->SEIMessageText);
  897.     struct TIMEB tstruct;
  898.     ftime( &tstruct);    // start time
  899.     if (message_size == 0)
  900.     {
  901.       message_size = 13;
  902.       strncpy(sei_message,"Empty Message",message_size);
  903.     }
  904.     else
  905.       strncpy(sei_message,params->SEIMessageText,message_size);
  906.     len+=u_v (8,"SEI: last_payload_type_byte", 5, bitstream);
  907.     message_size += 17;
  908.     while (message_size > 254)
  909.     {
  910.       len+=u_v (8,"SEI: ff_byte",255, bitstream);
  911.       message_size -= 255;
  912.     }
  913.     len+=u_v (8,"SEI: last_payload_size_byte",message_size, bitstream);
  914.     // Lets randomize uuid based on time
  915.     len+=u_v (32,"SEI: uuid_iso_iec_11578",(int) tstruct.timezone, bitstream);
  916.     len+=u_v (32,"SEI: uuid_iso_iec_11578",(int) tstruct.time*1000+tstruct.millitm, bitstream);
  917.     len+=u_v (32,"SEI: uuid_iso_iec_11578",(int) (uuid_message[0] << 24) + (uuid_message[1] << 16)  + (uuid_message[2] << 8) + (uuid_message[3] << 0), bitstream);
  918.     len+=u_v (32,"SEI: uuid_iso_iec_11578",(int) (uuid_message[4] << 24) + (uuid_message[5] << 16)  + (uuid_message[6] << 8) + (uuid_message[7] << 0), bitstream);
  919.     for (i = 0; i < strlen(sei_message); i++)
  920.       len+=u_v (8,"SEI: user_data_payload_byte",sei_message[i], bitstream);
  921.     len+=u_v (8,"SEI: user_data_payload_byte",   0, bitstream);
  922.   }
  923.   SODBtoRBSP(bitstream);     // copies the last couple of bits into the byte buffer
  924.   LenInBytes=bitstream->byte_pos;
  925.   free(bitstream);
  926.   return LenInBytes;
  927. }
  928. /*!
  929.  *************************************************************************************
  930.  * brief
  931.  *    int WriteHRDParameters((seq_parameter_set_rbsp_t *sps, Bitstream *bitstream)
  932.  *
  933.  *
  934.  * return
  935.  *    size of the RBSP in bytes, negative in case of an error
  936.  *
  937.  * note
  938.  *************************************************************************************
  939.  */
  940. int WriteHRDParameters(seq_parameter_set_rbsp_t *sps, Bitstream *bitstream)
  941. {
  942.   // hrd_parameters()
  943.   int len = 0;
  944.   unsigned int SchedSelIdx = 0;
  945.   hrd_parameters_t *hrd = &(sps->vui_seq_parameters.nal_hrd_parameters);
  946.   len+=ue_v ("VUI: cpb_cnt_minus1", hrd->cpb_cnt_minus1, bitstream);
  947.   len+=u_v  (4, "VUI: bit_rate_scale", hrd->bit_rate_scale, bitstream);
  948.   len+=u_v  (4, "VUI: cpb_size_scale", hrd->cpb_size_scale, bitstream);
  949.   for( SchedSelIdx = 0; SchedSelIdx <= (hrd->cpb_cnt_minus1); SchedSelIdx++ )
  950.   {
  951.     len+=ue_v ("VUI: bit_rate_value_minus1", hrd->bit_rate_value_minus1[SchedSelIdx], bitstream);
  952.     len+=ue_v ("VUI: cpb_size_value_minus1", hrd->cpb_size_value_minus1[SchedSelIdx], bitstream);
  953.     len+=u_1  ("VUI: cbr_flag", hrd->cbr_flag[SchedSelIdx], bitstream);
  954.   }
  955.   len+=u_v  (5, "VUI: initial_cpb_removal_delay_length_minus1", hrd->initial_cpb_removal_delay_length_minus1, bitstream);
  956.   len+=u_v  (5, "VUI: cpb_removal_delay_length_minus1", hrd->cpb_removal_delay_length_minus1, bitstream);
  957.   len+=u_v  (5, "VUI: dpb_output_delay_length_minus1", hrd->dpb_output_delay_length_minus1, bitstream);
  958.   len+=u_v  (5, "VUI: time_offset_length", hrd->time_offset_length, bitstream);
  959.   return len;
  960. }
  961. /*!
  962.  *************************************************************************************
  963.  * brief
  964.  *    void GenerateVUIParameters(seq_parameter_set_rbsp_t *sps)
  965.  *
  966.  *
  967.  * return
  968.  *    none
  969.  *
  970.  * note
  971.  *************************************************************************************
  972.  */
  973. void GenerateVUIParameters(seq_parameter_set_rbsp_t *sps)
  974. {
  975.   unsigned int          SchedSelIdx;
  976.   hrd_parameters_t     *nal_hrd = &(sps->vui_seq_parameters.nal_hrd_parameters);
  977.   hrd_parameters_t     *vcl_hrd = &(sps->vui_seq_parameters.vcl_hrd_parameters);
  978.   vui_seq_parameters_t *vui     = &(sps->vui_seq_parameters);
  979.   VUIParameters *iVui = &params->VUI;
  980.   vui->aspect_ratio_info_present_flag      = (Boolean) iVui->aspect_ratio_info_present_flag;
  981.   vui->aspect_ratio_idc                    = (unsigned int) iVui->aspect_ratio_idc;
  982.   vui->sar_width                           = (unsigned int) iVui->sar_width;
  983.   vui->sar_height                          = (unsigned int) iVui->sar_height;
  984.   vui->overscan_info_present_flag          = (Boolean) iVui->overscan_info_present_flag;
  985.   vui->overscan_appropriate_flag           = (Boolean) iVui->overscan_appropriate_flag;
  986.   vui->video_signal_type_present_flag      = (Boolean) iVui->video_signal_type_present_flag;
  987.   vui->video_format                        = (unsigned int) iVui->video_format;
  988.   vui->video_full_range_flag               = (Boolean) iVui->video_full_range_flag;
  989.   vui->colour_description_present_flag     = (Boolean) iVui->colour_description_present_flag;
  990.   vui->colour_primaries                    = (unsigned int) iVui->colour_primaries;
  991.   vui->transfer_characteristics            = (unsigned int) iVui->transfer_characteristics;
  992.   vui->matrix_coefficients                 = (unsigned int) iVui->matrix_coefficients;
  993.   vui->chroma_location_info_present_flag   = (Boolean) iVui->chroma_location_info_present_flag;
  994.   vui->chroma_sample_loc_type_top_field    = (unsigned int) iVui->chroma_sample_loc_type_top_field;
  995.   vui->chroma_sample_loc_type_bottom_field = (unsigned int) iVui->chroma_sample_loc_type_bottom_field;
  996.   vui->timing_info_present_flag            = (Boolean) iVui->timing_info_present_flag;
  997.   vui->num_units_in_tick                   = (unsigned int) iVui->num_units_in_tick;
  998.   vui->time_scale                          = (unsigned int) iVui->time_scale;
  999.   vui->fixed_frame_rate_flag               = (Boolean) iVui->fixed_frame_rate_flag;  
  1000.   // NAL HRD parameters
  1001.   vui->nal_hrd_parameters_present_flag             = (Boolean) iVui->nal_hrd_parameters_present_flag;  
  1002.   nal_hrd->cpb_cnt_minus1                          = (unsigned int) iVui->nal_cpb_cnt_minus1;
  1003.   nal_hrd->bit_rate_scale                          = (unsigned int) iVui->nal_bit_rate_scale;
  1004.   nal_hrd->cpb_size_scale                          = (unsigned int) iVui->nal_cpb_size_scale;
  1005.   for ( SchedSelIdx = 0; SchedSelIdx <= nal_hrd->cpb_cnt_minus1; SchedSelIdx++ )
  1006.   {
  1007.     nal_hrd->bit_rate_value_minus1[SchedSelIdx]    = (unsigned int) iVui->nal_bit_rate_value_minus1;
  1008.     nal_hrd->cpb_size_value_minus1[SchedSelIdx]    = (unsigned int) iVui->nal_cpb_size_value_minus1;
  1009.     nal_hrd->cbr_flag[SchedSelIdx]                 = (unsigned int) iVui->nal_vbr_cbr_flag;
  1010.   }
  1011.   nal_hrd->initial_cpb_removal_delay_length_minus1 = (unsigned int) iVui->nal_initial_cpb_removal_delay_length_minus1;
  1012.   nal_hrd->cpb_removal_delay_length_minus1         = (unsigned int) iVui->nal_cpb_removal_delay_length_minus1;
  1013.   nal_hrd->dpb_output_delay_length_minus1          = (unsigned int) iVui->nal_dpb_output_delay_length_minus1;
  1014.   nal_hrd->time_offset_length                      = (unsigned int) iVui->nal_time_offset_length;
  1015.   
  1016.   // VCL HRD parameters
  1017.   vui->vcl_hrd_parameters_present_flag             = (Boolean) iVui->vcl_hrd_parameters_present_flag;  
  1018.   vcl_hrd->cpb_cnt_minus1                          = (unsigned int) iVui->vcl_cpb_cnt_minus1;
  1019.   vcl_hrd->bit_rate_scale                          = (unsigned int) iVui->vcl_bit_rate_scale;
  1020.   vcl_hrd->cpb_size_scale                          = (unsigned int) iVui->vcl_cpb_size_scale;
  1021.   for ( SchedSelIdx = 0; SchedSelIdx <= vcl_hrd->cpb_cnt_minus1; SchedSelIdx++ )
  1022.   {
  1023.     vcl_hrd->bit_rate_value_minus1[SchedSelIdx]    = (unsigned int) iVui->vcl_bit_rate_value_minus1;
  1024.     vcl_hrd->cpb_size_value_minus1[SchedSelIdx]    = (unsigned int) iVui->vcl_cpb_size_value_minus1;
  1025.     vcl_hrd->cbr_flag[SchedSelIdx]                 = (unsigned int) iVui->vcl_vbr_cbr_flag;
  1026.   }
  1027.   vcl_hrd->initial_cpb_removal_delay_length_minus1 = (unsigned int) iVui->vcl_initial_cpb_removal_delay_length_minus1;
  1028.   vcl_hrd->cpb_removal_delay_length_minus1         = (unsigned int) iVui->vcl_cpb_removal_delay_length_minus1;
  1029.   vcl_hrd->dpb_output_delay_length_minus1          = (unsigned int) iVui->vcl_dpb_output_delay_length_minus1;
  1030.   vcl_hrd->time_offset_length                      = (unsigned int) iVui->vcl_time_offset_length;
  1031.   vui->low_delay_hrd_flag                      = (Boolean) iVui->low_delay_hrd_flag;
  1032.   vui->pic_struct_present_flag                 = (Boolean) iVui->pic_struct_present_flag;
  1033.   vui->bitstream_restriction_flag              = (Boolean) iVui->bitstream_restriction_flag;
  1034.   vui->motion_vectors_over_pic_boundaries_flag = (Boolean) iVui->motion_vectors_over_pic_boundaries_flag;
  1035.   vui->max_bytes_per_pic_denom                 = (unsigned int) iVui->max_bytes_per_pic_denom;
  1036.   vui->max_bits_per_mb_denom                   = (unsigned int) iVui->max_bits_per_mb_denom;
  1037.   vui->log2_max_mv_length_horizontal           = (unsigned int) iVui->log2_max_mv_length_horizontal;
  1038.   vui->log2_max_mv_length_vertical             = (unsigned int) iVui->log2_max_mv_length_vertical;
  1039.   vui->num_reorder_frames                      = (unsigned int) iVui->num_reorder_frames;
  1040.   vui->max_dec_frame_buffering                 = (unsigned int) iVui->max_dec_frame_buffering;
  1041.   
  1042.   // special case to signal the RGB format
  1043.   if(params->rgb_input_flag && params->yuv_format==3)
  1044.   {
  1045.     printf   ("VUI: writing Sequence Parameter VUI to signal RGB formatn");
  1046.     vui->aspect_ratio_info_present_flag = FALSE;
  1047.     vui->overscan_info_present_flag = FALSE;
  1048.     vui->video_signal_type_present_flag = TRUE;
  1049.     vui->video_format = 2;
  1050.     vui->video_full_range_flag = TRUE;
  1051.     vui->colour_description_present_flag = TRUE;
  1052.     vui->colour_primaries = 2;
  1053.     vui->transfer_characteristics = 2;
  1054.     vui->matrix_coefficients = 0;
  1055.     vui->chroma_location_info_present_flag = FALSE;
  1056.     vui->timing_info_present_flag = FALSE;
  1057.     vui->nal_hrd_parameters_present_flag = FALSE;
  1058.     vui->vcl_hrd_parameters_present_flag = FALSE;
  1059.     vui->pic_struct_present_flag = FALSE;
  1060.     vui->bitstream_restriction_flag = FALSE;
  1061.   } 
  1062. }