nginx_rtmp中解析sps和pps

一 首先这里要啰嗦一下,为什么要提取sps和pps
sps 主要包含了视频的分辨率和profile类型信息。pps是什么暂时没有关注。
二 在nginx-rtmp中,如何提取到sps和pps信息呢?
直接找到nginx_rtmp循环主干。
ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
                 ngx_chain_t *in)

(1) 获取到codec ctx
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
codec_ctx->aac_header;//音频codec header
codec_ctx->avc_header;//视频codec header
(2) 合并avc(h264)buf
ngx_chain_t *in = NULL;
for(in = avc_header;in;in = in->next)
{
memcpy(tmp_str.data + in_len,in->buf->pos,(int)(in->buf->last - in->buf->pos));
in_len += (int)(in->buf->last - in->buf->pos);
}
//buf 有4个指针,pos,last,start,end。end-start表示buf的容量,
last-pos表示buf数据量
(3) 解析sps和pps
从avc_header中提取到的buf数据符合rtmp message协议标准(即flv文件格式标准)
下面附上解析代码和注释:

int parse_offset = 0;
int frametype = avc_header->data[parse_offset]&0xff>>4;//4 bit帧格式
int codec_id = avc_header->data[parse_offset]&0x0f;//编码器id 7 AVC
parse_offset ++;
int packet_type = avc_header->data[parse_offset]&0xff;//数据包类型 0 sequence head(codec head) 1 nalu u
parse_offset ++;


parse_offset += 3;//3 字节时间戳


parser_AVCDecoderHeader *sps_header = (parser_AVCDecoderHeader *)(avc_header->data + parse_offset);
//parser_AVCDecoderHeader_DUMP(sps_header);
parse_offset += sizeof(parser_AVCDecoderHeader);//sps 解码头信息
int sps_len = ntohs(*(unsigned short *)((char *)avc_header->data + parse_offset));//sps长度  
        parse_offset += 2;




memcpy(sps->data,nalu_head,4);//在sps前面加上0x00000001 
sps->len += 4;
memcpy(sps->data + sps->len,avc_header->data + parse_offset,sps_len);//把sps数据串到后面
sps->len += sps_len;
parse_offset += sps_len;

int ppscount = ((char*)avc_header->data)[parse_offset];//pps 个数
parse_offset ++;
        int pps_len = ntohs(*(unsigned short *)((char *)avc_header->data + parse_offset));//pps 长度
parse_offset += 2;


memcpy(pps->data,nalu_head,4);//在pps前面加上0x00000001
pps->len += 4;
memcpy(pps->data + pps->len,avc_header->data + parse_offset,pps_len);//把pps数据串到后面
pps->len += pps_len;


三 aac header解析可以自行参考flv文档。这里就不多讲解了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值