首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >avcodec_open分割故障

avcodec_open分割故障
EN

Stack Overflow用户
提问于 2014-06-24 18:16:34
回答 1查看 927关注 0票数 0

下午好,

我得到了一份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(),并将调用放在类构造函数中。这部分起作用了。

代码语言:javascript
复制
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方法。构造函数现在看起来如下:

代码语言:javascript
复制
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_..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-24 20:09:20

您的代码没有设置.width/.height成员的mCodecContext,这将导致随后的崩溃。

Ubuntu12.04附带了0.8版的ffmpeglibav,它与ffmpeg 1.0+甚至更高版本的ffmpeg更兼容。我假设您正在使用这个存储库版本,并且在测试期间使用了libav-0.8.12 (尽管我个人非常喜欢真正的ffmpeg,即目前的ffmpeg-2.2 )。

有趣的是,使用ffmpeg-2.2 (使用av_codec_open2)而不是libav-0.8并不会崩溃,而是通常只返回失败,并打印未设置宽度和高度的警告( ffmpeg而不是libav的另一个支持点)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24393578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档