x264获取sps pps 等信息

通常是通过 x264_nal_t::i_type 判断当前的NAL的类型;


其定义的枚举类型:

enum nal_unit_type_e
{
    NAL_UNKNOWN     = 0,
    NAL_SLICE       = 1,
    NAL_SLICE_DPA   = 2,
    NAL_SLICE_DPB   = 3,
    NAL_SLICE_DPC   = 4,
    NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
    NAL_SEI         = 6,    /* ref_idc == 0 */
    NAL_SPS         = 7,
    NAL_PPS         = 8,
    NAL_AUD         = 9,
    NAL_FILLER      = 12,
    /* ref_idc == 0 for 6,9,10,11,12 */
};


通常,在关键帧前面的就是sps和pps等信息,但是如果要立刻获取相关信息或持续一开始就要先获取,可以通过 x264_encoder_headers 函数获取;

函数注释:

/* x264_encoder_headers:
 *      return the SPS and PPS that will be used for the whole stream.
 *      *pi_nal is the number of NAL units outputted in pp_nal.
 *      returns the number of bytes in the returned NALs.
 *      returns negative on error.
 *      the payloads of all output NALs are guaranteed to be sequential in memory. */
int     x264_encoder_headers( x264_t *, x264_nal_t **pp_nal, int *pi_nal );


应用示例:

 //* 获取整个流的PPS和SPS,不需要可以不调用.
 iResult = x264_encoder_headers(pX264Handle, &pNals, &iNal);
 assert(iResult >= 0);

//一个Nal保存一个相关信息,所以有多个信息,就会有多个Nal;

 for (int i = 0; i < iNal; ++i)
 {
  switch (pNals[i].i_type)  //也可以判断上述枚举中的其它相关信息;
  {
  case NAL_SPS:
   break;
  case  NAL_PPS:
   break;
  default:
   break;
  }
 }


这里可以注意,在x264中,一般sps和pps的分隔符是4个字节:00 00 00 01, 普通的分隔符是3个字节:00 00 01;








你可能感兴趣的:(H264)