1.初始化ISVCEncoder
ISVCEncoder* encoder_;
int rv = WelsCreateSVCEncoder(&encoder_);
if (0 != rv || !encoder_) {
//error
}
ISVCEncoder提供了一系列的编码有关的接口,如初始化编码器、设置编码参数、编码等;
2. 设置相关参数
openh264提供了两个结构体来设置编码参数,SEncParamBase与SEncParamExt, SEncParamBase仅通过了最基础的参数设置, 其定义如下:
typedef struct TagEncParamBase {
EUsageType
iUsageType; ///< application type;1.CAMERA_VIDEO_REAL_TIME:camera video signal; 2.SCREEN_CONTENT_REAL_TIME:screen content signal;
int iPicWidth; ///< width of picture in luminance samples (the maximum of all layers if multiple spatial layers presents)
int iPicHeight; ///< height of picture in luminance samples((the maximum of all layers if multiple spatial layers presents)
int iTargetBitrate; ///< target bitrate desired, in unit of bps
RC_MODES iRCMode; ///< rate control mode
float fMaxFrameRate; ///< maximal input frame rate
} SEncParamBase, *PEncParamBase;
其中参数iUsageType指明应用的类型,类型包括:
typedef enum {
CAMERA_VIDEO_REAL_TIME, ///< camera video for real-time communication
SCREEN_CONTENT_REAL_TIME, ///< screen content signal
CAMERA_VIDEO_NON_REAL_TIME
} EUsageType;
iRCMode指定码率控制的模式, openh264提供的模式如下:
typedef enum {
RC_QUALITY_MODE = 0, ///< quality mode
RC_BITRATE_MODE = 1, ///< bitrate mode
RC_BUFFERBASED_MODE = 2, ///< no bitrate control,only using buffer status,adjust the video quality
RC_TIMESTAMP_MODE = 3, //rate control based timestamp
RC_BITRATE_MODE_POST_SKIP = 4, ///< this is in-building RC MODE, WILL BE DELETED after algorithm tuning!
RC_OFF_MODE = -1, ///< rate control off mode
} RC_MODES;
对编码器的初始化例子如下:
SEncParamBase paramBase;
paramBase.iPicWidth = width_;
paramBase.iPicHeight = height_;
paramBase.fMaxFrameRate = fps_;
paramBase.iTargetBitrate = 10 * width_ * height_;
paramBase.iUsageType = CAMERA_VIDEO_REAL_TIME;
paramBase.iRCMode = RC_BITRATE_MODE;
int ret = encoder_->Initialize(¶mBase);
if (0 != ret) {
//error
}
3. 编码
编码需要用到SSourcePicture和SFrameBSInfo两个结构体, SSourcePicture用来保存需要编码的数据信息, 而SFrameBSInfo会保存编码完成后的数据。
SSourcePicture的定义
typedef struct Source_Picture_s {
int iColorFormat; ///< color space type
int iStride[4]; ///< stride for each plane pData
unsigned char* pData[4]; ///< plane pData
int iPicWidth; ///< luma picture width in x coordinate
int iPicHeight; ///< luma picture height in y coordinate
long long uiTimeStamp; ///< timestamp of the source picture, unit: millisecond
} SSourcePicture;
- iColorFormat:颜色空间的类型,如videoFormatI420;
- iStride, 每个plane的stride,对于plane和stride的理解可参考yuv 图像里的stride和plane的解释。
- pData,指向每个plane的指针;
SFrameBSInfo结构体在编码前只需要用memset将结构体中的数据置为0即可。其定义如下:
typedef struct {
int iLayerNum;
SLayerBSInfo sLayerInfo[MAX_LAYER_NUM_OF_FRAME];
EVideoFrameType eFrameType;
int iFrameSizeInBytes;
long long uiTimeStamp;
} SFrameBSInfo, *PFrameBSInfo;
typedef struct {
unsigned char uiTemporalId;
unsigned char uiSpatialId;
unsigned char uiQualityId;
EVideoFrameType eFrameType;
unsigned char uiLayerType;
/**
* The sub sequence layers are ordered hierarchically based on their dependency on each other so that any picture in a layer shall not be
* predicted from any picture on any higher layer.
*/
int iSubSeqId; ///< refer to D.2.11 Sub-sequence information SEI message semantics
int iNalCount; ///< count number of NAL coded already
int* pNalLengthInByte; ///< length of NAL size in byte from 0 to iNalCount-1
unsigned char* pBsBuf; ///< buffer of bitstream contained
} SLayerBSInfo, *PLayerBSInfo;
SFrameBSInfo结构体比较复杂, 具体使用情况下面再解释。
对于i420数据的编码过程:
SSourcePicture pic;
memset(&pic, 0, sizeof(pic));
pic.iPicWidth = width_;
pic.iPicHeight = height_;
pic.iColorFormat = videoFormatI420;
pic.iStride[0] = pic.iPicWidth;
pic.iStride[1] = pic.iStride[2] = pic.iPicWidth >> 1;
pic.pData[0] = (unsigned char *) i420Buffer;
pic.pData[1] = pic.pData[0] + width_ * height_;
pic.pData[2] = pic.pData[1] + (width_ * height_ >> 2);
SFrameBSInfo info;
memset(&info, 0, sizeof(SFrameBSInfo));
int rv = encoder_->EncodeFrame(&pic, &info);
int retSize = 0;
if (rv != cmResultSuccess) {
//error info
return retSize;
}
if (info.eFrameType != videoFrameTypeSkip) {
int type = info.eFrameType;
for (int i = 0; i < info.iLayerNum; ++i) {
const SLayerBSInfo &layerInfo = info.sLayerInfo[i];
int layerSize = 0;
for (int j = 0; j < layerInfo.iNalCount; ++j) {
layerSize += layerInfo.pNalLengthInByte[j];
}
memcpy((char *) (oBuf + retSize), (char *) layerInfo.pBsBuf, layerSize);
retSize += layerSize;
}
}
其中i420Buffer是指向原始yuv数据的指针,oBuf是指向h264流缓冲区的指针。由上可知,整个过程分为几步:
- 定义SSourcePicture和SFrameBSInfo, 并给SSourcePicture赋值;
- 编码;
- 判断编码是否成功;
- 判断帧类型,如果不是跳帧, 则读取编码后数据;
SFrameBSInfo的参数iLayerNum表示编码后的NAL数量。编码后的h264数据存放在SFrameBSInfo的sLayerInfo结构数组中,其中每个结构体中的pBsBuf表示编码得到的数据,而长度是结构体pNalLengthInByteint数组加起来的和,数组长度由结构体的iNalCount成员表示。
4. 释放
先调用ISVCEncoder的Uninitialize函数,再调用WelsDestroySVCEncoder即可。