H265编码在使用性上,相比H264有更大的功能性,因此越来越多的对视频有需求的用户或者项目团队都要求能够支持H265编码格式。
鉴于近期越来越多的用户提出国标GB28181视频流媒体平台EasyGBS对H265编码格式接入的需求,我们已经对下级推送上来的H265流进行适配,本文来看一下适配的具体流程。
1. 定义支持H265编码格式类型
(1) 我们分析PS流中的H265编码格式为0x24
(2) 定义H265编码推流格式为0xAE
2. 适配PS流编码格式为H264和H265的情况
if (streamCodec == 0x10)
videoCodec = 0x0D; //MPEG4
else if (streamCodec == 0x1B)
videoCodec = 0x1C; //H264
else if(0x24 == streamCodec)
videoCodec = 0xAE; //H265
else{
printf("========>PSDemux_demuxMapStreamInfo streamCodec:[%d],unknow videoCodec\n", streamCodec);
}
3. EasyRTMP推流兼容H264和H265
在EasyRTMP推流已经支持H265的基础上,我们需要对H265在EasyGBS内部推流做下适配。首先,因为推流设置流媒体编码类型的函数InitMetdata我们已经在初始化的时候就调用了,所以需要在库内部做编码格式兼容,兼容代码如下:
if (thiz->u32VideoCodec == EASY_SDK_VIDEO_CODEC_H264)
{
GetH264SPSandPPS((char *)frame->pBuffer, frame->u32AVFrameLen, thiz->sps, &thiz->nSPSLen, thiz->pps, &thiz->nPPSLen, &startpos);
_TRACE_LOG("InitMetadata nSPSLen=%d, nPPSLen=%d, startpos=%d\n", thiz->nSPSLen, thiz->nPPSLen, startpos);
//int h264_nal_type = (buf[0]) & 0x1f;
if ( (thiz->sps[0] & 0x1f ) != 0x07 || (thiz->pps[0] & 0x1f ) != 0x08 )
{
GetH265VPSandSPSandPPS((char *)frame->pBuffer, frame->u32AVFrameLen, thiz->vps, &thiz->nVPSLen,thiz->sps, &thiz->nSPSLen, thiz->pps, &thiz->nPPSLen);
if ( (((unsigned char)thiz->vps[0] >> 1) & 0x3f) == 32 && (((unsigned char)thiz->sps[0] >> 1) & 0x3f) == 33 && (((unsigned char)thiz->pps[0] >> 1) & 0x3f) == 34 )
{
thiz->u32VideoCodec = EASY_SDK_VIDEO_CODEC_H265;
}
}
}
else
{
GetH265VPSandSPSandPPS((char *)frame->pBuffer, frame->u32AVFrameLen, thiz->vps, &thiz->nVPSLen,thiz->sps, &thiz->nSPSLen, thiz->pps, &thiz->nPPSLen);
_TRACE_LOG("InitMetadata nVPSLen=%d, nSPSLen=%d, nPPSLen=%d\n", thiz->nVPSLen, thiz->nSPSLen, thiz->nPPSLen);
//int h265_nal_type = (buf[0] >> 1) & 0x3f;
}
以上三步完成后,我们再运行EasyGBS即可实现,不管下级平台推流视频编码格式是H264还是H265都可以完美的支持。