下午好,
我得到了一份ffmpeg的api-example.c (8c-source.html)的副本。我测试了函数video_encode_example(),并使代码按预期工作。
然后,我将这个函数重新分解为不同的类方法,现在我在调用avcodev_open()时得到了一个seg错误。然后,我更改了代码以调用类方法中的原始video_encode_example()和对avcodec的调用.都很成功。
当从函数(例如video_encode_example())调用时,行为似乎与预期的一样,但是当从类方法调用时,avcodev_open会失败。我跟踪了代码,在这两种情况下,avcodev_open()参数的值都被分配给(和类似的)指针值。cgdb报告avcodev_open2()中发生了seg故障
有什么建议吗?代码是在Ubuntu12.04 64位机器上编译和测试的),使用libav包,可以通过apt-get (libavcodec.so.53.35.0)
你好,丹尼尔
在第一次答复之后添加了评论:
从上面的链接中,我复制了函数video_encode_example(),并将调用放在类构造函数中。这部分起作用了。
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);
}
}对avcodec_open的调用导致seg错误。此外,是否使用avcodev_也会发生故障。或者::avcodev_..。
发布于 2014-06-24 20:09:20
您的代码没有设置.width/.height成员的mCodecContext,这将导致随后的崩溃。
Ubuntu12.04附带了0.8版的ffmpeg叉libav,它与ffmpeg 1.0+甚至更高版本的ffmpeg更兼容。我假设您正在使用这个存储库版本,并且在测试期间使用了libav-0.8.12 (尽管我个人非常喜欢真正的ffmpeg,即目前的ffmpeg-2.2 )。
有趣的是,使用ffmpeg-2.2 (使用av_codec_open2)而不是libav-0.8并不会崩溃,而是通常只返回失败,并打印未设置宽度和高度的警告( ffmpeg而不是libav的另一个支持点)。
https://stackoverflow.com/questions/24393578
复制相似问题