C++ avcodec_开放分段故障
下午好 我得到了ffmpeg的api example.c()的副本。我测试了函数video_encode_example(),并使代码按预期工作 然后,我将这个函数重新分解为不同的类方法,现在调用avcodev_open()会出现seg错误。然后,我更改了代码,从类方法调用原始的video_encode_example(),并调用avcodec。。。我们取得了成功 从函数(例如video_encode_example())调用时,行为似乎与预期一样,但从类方法调用时,avcodev_open失败。我跟踪了代码,在这两种情况下,avcodev_open()参数的值都被分配(和类似的)指针值。cgdb报告avcodev_open2()中出现seg故障 有什么建议吗? 代码是在Ubuntu 12.04(64位机器)上编译和测试的,使用的是通过apt get(libavcodec.so.53.35.0)提供的libav包 问候,, 丹尼尔 在第一次答复后添加了评论: 从上面的链接中,我复制了函数video_encode_example(),并将调用放在类构造函数中。这部分工作正常C++ avcodec_开放分段故障,c++,ffmpeg,ubuntu-12.04,libavcodec,C++,Ffmpeg,Ubuntu 12.04,Libavcodec,下午好 我得到了ffmpeg的api example.c()的副本。我测试了函数video_encode_example(),并使代码按预期工作 然后,我将这个函数重新分解为不同的类方法,现在调用avcodev_open()会出现seg错误。然后,我更改了代码,从类方法调用原始的video_encode_example(),并调用avcodec。。。我们取得了成功 从函数(例如video_encode_example())调用时,行为似乎与预期一样,但从类方法调用时,avcodev_open失败
VideoEncoder::VideoEncoder( const std::string& aFileName)
: mCodec( NULL)
, mCodecContext( NULL)
, picture (NULL)
, mFileName( aFileName)
, mFileHandler( NULL)
{
// must be called before using avcodec lib
::avcodec_init();
// register all the codecs
::avcodec_register_all();
video_encode_example( aFileName.c_str());
return;
...
}
重新分解的部分包括将avcodev调用(从原始video_encode_example()函数)拆分为不同的VideoEncoder方法。构造函数现在看起来像:
VideoEncoder::VideoEncoder( const std::string& aFileName)
: mCodec( NULL)
, mCodecContext( NULL)
, picture (NULL)
, mFileName( aFileName)
, mFileHandler( NULL)
{
// must be called before using avcodec lib
::avcodec_init();
// register all the codecs
::avcodec_register_all();
// find the mpeg1 video encoder
mCodec = ::avcodec_find_encoder( CODEC_ID_MPEG1VIDEO);
if (!mCodec) {
fprintf( stderr, "codec not found\n");
exit( 1);
}
mCodecContext = ::avcodec_alloc_context3( mCodec);
picture = ::avcodec_alloc_frame();
// put sample parameters
mCodecContext->bit_rate = 400000;
// frames per second
mCodecContext->time_base = (AVRational){1,25};
mCodecContext->gop_size = 10; // emit one intra frame every ten frames
mCodecContext->max_b_frames = 1;
mCodecContext->pix_fmt = PIX_FMT_YUV420P;
// open it
// Initializes the AVCodecContext to use the given AVCodec. Prior to using this function the context has to be allocated.
if (::avcodec_open( mCodecContext, mCodec) < 0) {
fprintf(stderr, "could not open codec\n");
exit( 1);
}
mFileHandler = fopen( mFileName.c_str(), "wb");
if (!mFileHandler) {
fprintf( stderr, "could not open %s\n", mFileName.c_str());
exit( 1);
}
}
VideoEncoder::VideoEncoder(const std::string&aFileName)
:mCodec(空)
,mCodecContext(NULL)
,图片(空)
,mFileName(aFileName)
,mFileHandler(NULL)
{
//必须在使用avcodec lib之前调用
::avcodec_init();
//注册所有编解码器
::avcodec_register_all();
//查找mpeg1视频编码器
mCodec=::avcodec\u find\u编码器(CODEC\u ID\u mpeg1视频);
如果(!mCodec){
fprintf(stderr,“找不到编解码器”);
出口(1);
}
mCodecContext=::avcodec\u alloc\u context3(mCodec);
图片=::avcodec_alloc_frame();
//输入样本参数
mCodecContext->比特率=400000;
//每秒帧数
mCodecContext->time_base=(AVRational){1,25};
mCodecContext->gop_size=10;//每十帧发射一帧
mCodecContext->max_b_frames=1;
mCodecContext->pix_fmt=pix_fmt_YUV420P;
//打开它
//初始化AvcodeContext以使用给定的AVCodec。在使用此函数之前,必须分配上下文。
如果(::avcodec_open(mCodecContext,mCodec)<0){
fprintf(stderr,“无法打开编解码器”\n);
出口(1);
}
mFileHandler=fopen(mFileName.c_str(),“wb”);
if(!mFileHandler){
fprintf(stderr,“无法打开%s\n”,mFileName.c_str());
出口(1);
}
}
对avcodec_open的调用导致seg故障。此外,无论是否使用avcodev_。。或::avcodev_u2; 您的代码没有设置
mCodecContext
的.width
/.height
成员,这会导致以后崩溃
Ubuntu 12.04在版本0.8中附带了ffmpeg
forklibav
,它与ffmpeg
1.0+甚至更高版本的ffmpeg
IIRC更兼容。我假设您正在使用该存储库版本,并且在测试我自己的过程中使用了libav-0.8.12
(尽管我个人非常喜欢真正的ffmpeg
,即ffmpeg-2.2
)
有趣的是,使用
ffmpeg-2.2
(使用av\u codec\u open2
)而不是libav-0.8
)不会崩溃,但通常只会返回故障,并打印一条关于宽度和高度未设置的警告(另一个关于ffmpeg
而不是libav
)。您必须分享更多信息。你说代码在工作,你做了秘密更改,现在它坏了?我建议你回滚更改,直到它再次工作。你成功了,谢谢。在调用avcodec_open之后,我用另一种方法设置了宽度/高度。我确实使用LIAV-0.8,满足我目前的需要。因为我“钉”它,考虑把我的答案标记为接受(左边的检查标记)和/或对它进行投票。