最近在做录屏项目时用到了H264编码,感觉很崩溃,就在网上down了段代码,调试的时候不动屏幕还好,还可以一直编码,一动屏幕(比如最大化,最小化一个窗口,或打开网页等操作)程序就崩溃了,
错误位置:(x264_encoder_encode(h, &nal, &nnal, &pic, &pic_out) < 0)
显示的错误: 0x00000000 处有未经处理的异常: 0xC0000005: Access violation
nal这个变量为NULL。
下面是我初始化的代码:
bool x264enc::InitX264Encoder(unsigned short usWidth,unsigned short usHeight,int nKeyFrameInterval,int nFrameRate,int nQuality)
{
x264_param_t param;
x264_param_default(¶m);
param.i_width = usWidth;
param.i_height = usHeight;
param.rc.i_qp_min=20;
param.rc.i_qp_max=31;
param.rc.f_qcompress=0.5f;
param.rc.i_qp_constant=0;
param.rc.i_rc_method = X264_RC_CRF;
param.rc.f_rf_constant = 8.5f;
param.i_log_level = X264_LOG_NONE;
h = x264_encoder_open(¶m);
if(h == NULL)
{
return false;
}
m_usWidth=usWidth;
m_usHeight=usHeight;
return true;
}
下面是我编码时候的代码:
bool x264enc::X264Encode(unsigned char* pInFrame,const int& nInLen,unsigned char* pOutFrame,int& nOutLen,bool& bKeyFrame)
{
x264_nal_t *nal;
x264_picture_t pic_out;
x264_picture_t pic;
int nnal = 0;
if(pInFrame)
{
pic.img.i_csp = X264_CSP_I420;
pic.img.i_plane = 3;
pic.img.plane[0] = pInFrame;
pic.img.plane[1] = pInFrame + m_usWidth*m_usHeight;
pic.img.plane[2] = pic.img.plane[1] + (m_usWidth*m_usHeight / 4);
pic.img.i_stride[0] = m_usWidth;
pic.img.i_stride[1] = m_usWidth / 2;
pic.img.i_stride[2] = m_usWidth / 2;
pic.i_type = X264_TYPE_AUTO;
if(x264_encoder_encode(h, &nal, &nnal, &pic, &pic_out) < 0)
{
return false;
}
}
else
{
if(x264_encoder_encode(h, &nal, &nnal, NULL, &pic_out) < 0)
{
return false;
}
}
if(nnal <= 0)
return false;
nOutLen = encode_nals(pOutFrame,nOutLen, nal, nnal);
if(nOutLen < 0)
{
return false;
}
if(pic_out.i_type == X264_TYPE_IDR)
{
bKeyFrame = true;
}
else
{
bKeyFrame = false;
}
return true;
}
我在网上看大家写的都差不多,搞不懂是哪里错了,求大神指点,有没有x264的开发文档啊,里面的参数都不知道是什么意思,英文的也行。