x264_encoder_encode 运行异常问题
我的程序采集过来视频数据是UYVY,采用X264编译,视频数据都是实时采集,问题是现在遇到x264_encoder_encode直接抛出异常,通过跟踪代码刚开始接受数据时x264_encoder_encode不会异常但是返回x264_nal_t为null即没有进行编码(通过查询信息知道前面的数据为b-frame时暂时不编码),但是循环几次后x264_encoder_encode就弹出异常:
下面我把编码代码部分贴出来,请高手指点:
x264Encoder::x264Encoder()
{
_x264_encoder = NULL;
_quality = 30;
_has_init = 0;
_default_width = 720;
_default_height = 480;
}
x264Encoder::~x264Encoder()
{
}
int x264Encoder::init()
{
return init_param(_default_width, _default_height);
}
void x264Encoder::unini()
{
if(_has_init) {
x264_picture_clean(&_in_pic);
x264_encoder_close(_x264_encoder);
}
}
void x264Encoder::set_image_size(int w, int h)
{
if(w != _default_width || h != _default_height) {
init_param(w, h);
_default_width = w;
_default_height = h;
}
}
int x264Encoder::init_param(int w, int h)
{
if(_has_init) {
x264_picture_clean(&_in_pic);
x264_encoder_close(_x264_encoder);
}
x264_param_default(&_x264_param);
_x264_param.i_width = w;
_x264_param.i_height = h;
/* _x264_param.i_frame_reference = 2;
_x264_param.analyse.i_subpel_refine = 6;
_x264_param.rc.i_lookahead = 30;
_x264_param.rc.i_rc_method = X264_RC_CQP;
_x264_param.rc.i_qp_constant = 51 - _quality / 2; // (0 <= m_nQuality <= 100)
*/
_x264_encoder = x264_encoder_open(&_x264_param);
x264_picture_alloc(&_in_pic,
X264_CSP_YV12,
_x264_param.i_width,
_x264_param.i_height);
_has_init = 1;
return (_x264_encoder == NULL ? -1 : 0);
}
int x264Encoder::encode_to_x264(unsigned char* src,
long len,
int w,
int h,
unsigned char* dest,
long *dest_len)
{
int ret = 0;
set_image_size(w, h);
_in_pic.img.i_plane = 3;
_in_pic.img.plane[0] = src;
_in_pic.img.plane[1] = src + w * h;
_in_pic.img.plane[2] = (BYTE*)_in_pic.img.plane[1] + w * h / 4;
_in_pic.img.plane[3] = 0;
_in_pic.i_type = X264_TYPE_AUTO;
_in_pic.i_qpplus1 = 0;
_in_pic.param = &_x264_param;
_in_pic.i_pts = 0;
ret = encode(&_in_pic, dest, dest_len);
return ret;
}
int x264Encoder::encode(x264_picture_t *inpic,
unsigned char* dest,
long *dest_len )
{
x264_picture_t pic_out;
x264_nal_t *nal;
int i_nal, i;
if(x264_encoder_encode(_x264_encoder, &nal, &i_nal, inpic, &pic_out) < 0) {
return -1;
}
*dest_len = 0;
for( i = 0; i < i_nal; i++ ) {
memcpy(dest, nal[i].p_payload, nal[i].i_payload);
dest += nal[i].i_payload;
*dest_len += nal[i].i_payload;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,好的,谢谢...
以前是直接采用libx264来做,我现在加到ffmpeg实现,没有这个问题了
你好,我遇到类似的问题,请问你解决了么