openH264编码流程

来源:互联网 发布:六十甲子单双计算法 编辑:程序博客网 时间:2024/06/05 23:06

openH264编码流程

之前在项目中用过openH264进行h264编码,本文介绍一下编码流程,算是做一个总结。

编码流程基本可以分为3步:
1.创建编码器。

int result = WelsCreateSVCEncoder(&encoder);

可以通过返回的参数是否为cmResultSuccess来判断编码器是否创建成功。
2.初始化编码器,包括各种参数的设置。

memset(&encParam, 0, sizeof(SEncParamExt));encoder->GetDefaultParams(&encParam);encParam.iUsageType = CAMERA_VIDEO_REAL_TIME;encParam.fMaxFrameRate = fps;encParam.iPicWidth = videoWidth;encParam.iPicHeight = videoHeight;encParam.iTargetBitrate = bitrate;encParam.iRCMode = RC_QUALITY_MODE;encParam.iTemporalLayerNum = 1;encParam.iSpatialLayerNum = 1;encParam.bEnableDenoise = false;encParam.bEnableBackgroundDetection = true;encParam.bEnableAdaptiveQuant = false;encParam.bEnableFrameSkip = false;encParam.bEnableLongTermReference = false;encParam.uiIntraPeriod = gop;encParam.eSpsPpsIdStrategy = CONSTANT_ID;encParam.bPrefixNalAddingCtrl = false;encParam.sSpatialLayers[0].iVideoWidth = videoWidth;encParam.sSpatialLayers[0].iVideoHeight = videoHeight;encParam.sSpatialLayers[0].fFrameRate = fps;encParam.sSpatialLayers[0].iSpatialBitrate = bitrate;encParam.sSpatialLayers[0].iMaxSpatialBitrate = encParam.iMaxBitrate;encoder->InitializeExt (&encParam);

在配置参数是,使用了SEncParamExt来进行配置,如果不需要配置这么多的参数的话,也可以使用SEncParamBase来进行配置。
3.调用编码器开始编码。

memset (&info, 0, sizeof (SFrameBSInfo));SSourcePicture pic;memset (&pic, 0, sizeof (SSourcePicture));pic.iPicWidth    = encParam.iPicWidth;pic.iPicHeight   = encParam.iPicHeight;pic.iColorFormat = videoFormatI420;pic.iStride[0]   = pic.iPicWidth;pic.iStride[1]   = pic.iStride[2] = pic.iPicWidth >> 1;pic.pData[0]     = i420_scaled_frame.y;pic.pData[1]     = i420_scaled_frame.u;pic.pData[2]     = i420_scaled_frame.v;int rv = encoder->EncodeFrame (&pic, &info);

因为编码接收的数据为YUV420P格式的,所以如果视频数据不是这种格式的话就需要转换一下格式,然后将数据赋值给pic,然后就完成了编码。编码后的数据是保存在info中的,取数据的时候要注意需循环读取。

0 0