视频流传输并做实时分析过程记录
最近完成了实时视频流做图像算法分析的工程,记录下整体流程和关键步骤。
1.项目中使用RTP协议H264载荷传输视频数据,流媒体服务器收到流数据解封装提取出H264数据进行解码,解码函数:
int CITStreamTrans::StreamDec(unsigned char* buffer_in, int32_t bufferLen_in, unsigned char** yuv_out,int32_t &bufferLen_out){
int ret;
AVPacket packet = {
0 };
packet.data = (uint8_t*)buffer_in;
packet.size = bufferLen_in;
packet.pts = FRAME_TIMESTAMP * m_nFrameCounter++;
ret = avcodec_send_packet(_pDecCodecContext, &packet);
//return ret;
if (avcodec_receive_frame(_pDecCodecContext, _pFrameYuv) == 0)
{
m_nCodecID = m_nCodecID == 0 ? _pDecCodecContext->codec_id: m_nCodecID;
m_nWidth = m_nWidth == 0 ? _pDecCodecContext->width : m_nWidth;
m_nHeight = m_nHeight == 0 ? _pDecCodecContext->height : m_nHeight;
m_nFps = m_nFps == 0 ? ENCODE_DEFAULT_FRAMERATE : m_nFps;
m_nMbps = m_nMbps == 0 ? ENCODE_DEAULT_BITRATES : m_nMbps;
_pFrameYuv->pts = _pFrameYuv->best_effort_timestamp;
//int height = _pCodecContext->height;
//int width = _pCodecContext->width;
av_packet_unref(&packet);
if(false){
//禁用硬解码
AVFrame *tmp_frame = NULL;
if (_pFrameYuv->format == hw_pix_fmt)
{
/* retrieve data from GPU to CPU */
if ((ret = av_hwframe_transfer_data(m_pSWFrame, _pFrameYuv, 0)) < 0)
{
printf("Error transferring the data to system memory\n");
return -1;
}
//tmp_frame = m_pSWFrame;
}
//else