首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ffmpeg/libavcodec内存管理

ffmpeg/libavcodec内存管理
EN

Stack Overflow用户
提问于 2013-08-02 19:32:43
回答 1查看 7.2K关注 0票数 5

libavcodec文档对于何时释放分配的数据以及如何释放这些数据不是很具体。在阅读了文档和示例之后,我将下面的示例程序放在一起。源代码中有一些具体的问题,但我的一般问题是,下面的代码是否正确地释放了所有内存?我意识到下面的程序不会在错误发生后进行任何清理--重点是最后的清理。

testfile()函数就是问题所在。

代码语言:javascript
复制
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
}

#include <cstdio>

using namespace std;


void AVFAIL (int code, const char *what) {
    char msg[500];
    av_strerror(code, msg, sizeof(msg));
    fprintf(stderr, "failed: %s\nerror: %s\n", what, msg);
    exit(2);
}

#define AVCHECK(f) do { int e = (f); if (e < 0) AVFAIL(e, #f); } while (0)
#define AVCHECKPTR(p,f) do { p = (f); if (!p) AVFAIL(AVERROR_UNKNOWN, #f); } while (0)


void testfile (const char *filename) {

    AVFormatContext *format;
    unsigned streamIndex;
    AVStream *stream = NULL;
    AVCodec *codec;
    SwsContext *sws;
    AVPacket packet;
    AVFrame *rawframe;
    AVFrame *rgbframe;
    unsigned char *rgbdata;

    av_register_all();

    // load file header
    AVCHECK(av_open_input_file(&format, filename, NULL, 0, NULL));
    AVCHECK(av_find_stream_info(format));

    // find video stream
    for (streamIndex = 0; streamIndex < format->nb_streams && !stream; ++ streamIndex)
        if (format->streams[streamIndex]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            stream = format->streams[streamIndex];
    if (!stream) {
        fprintf(stderr, "no video stream\n");
        exit(2);
    }

    // initialize codec
    AVCHECKPTR(codec, avcodec_find_decoder(stream->codec->codec_id));
    AVCHECK(avcodec_open(stream->codec, codec));
    int width = stream->codec->width;
    int height = stream->codec->height;

    // initialize frame buffers
    int rgbbytes = avpicture_get_size(PIX_FMT_RGB24, width, height);
    AVCHECKPTR(rawframe, avcodec_alloc_frame());
    AVCHECKPTR(rgbframe, avcodec_alloc_frame());
    AVCHECKPTR(rgbdata, (unsigned char *)av_mallocz(rgbbytes));
    AVCHECK(avpicture_fill((AVPicture *)rgbframe, rgbdata, PIX_FMT_RGB24, width, height));

    // initialize sws (for conversion to rgb24)
    AVCHECKPTR(sws, sws_getContext(width, height, stream->codec->pix_fmt, width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL));

    // read all frames fromfile
    while (av_read_frame(format, &packet) >= 0) {       

        int frameok = 0;
        if (packet.stream_index == (int)streamIndex)
            AVCHECK(avcodec_decode_video2(stream->codec, rawframe, &frameok, &packet));

        av_free_packet(&packet); // Q: is this necessary or will next av_read_frame take care of it?

        if (frameok) {
            sws_scale(sws, rawframe->data, rawframe->linesize, 0, height, rgbframe->data, rgbframe->linesize);
            // would process rgbframe here
        }

        // Q: is there anything i need to free here?

    }

    // CLEANUP: Q: am i missing anything / doing anything unnecessary?
    av_free(sws); // Q: is av_free all i need here?
    av_free_packet(&packet); // Q: is this necessary (av_read_frame has returned < 0)?
    av_free(rgbframe);
    av_free(rgbdata); 
    av_free(rawframe); // Q: i can just do this once at end, instead of in loop above, right?
    avcodec_close(stream->codec); // Q: do i need av_free(codec)?
    av_close_input_file(format); // Q: do i need av_free(format)?

}


int main (int argc, char **argv) {

    if (argc != 2) {
        fprintf(stderr, "usage: %s filename\n", argv[0]);
        return 1;
    }

    testfile(argv[1]);

}

具体问题:

  1. 在帧处理循环中有什么我需要释放的吗?或者libav会为我负责内存管理吗?
  2. av_free是释放SwsContext的正确方法吗?
  3. av_read_frame返回< 0时,框架循环退出。在这种情况下,当它完成时,我还需要av_free_packet吗?
  4. 我是否需要每次通过循环调用av_free_packet,还是av_read_frame会自动释放/重用旧的AVPacket
  5. 我只需在循环结束时重新分配av_free,而不是每次重新分配AVFrame,对吗?它似乎运转得很好,但我想确认一下,它的工作是应该的,而不是靠运气。
  6. 我需要av_free(codec) AVCodec还是在avcodec_close之后在AVCodecContext上做其他事情?
  7. 我需要av_free(format) AVFormatContext还是在av_close_input_file之后做其他事情?

我还意识到,在当前版本的libav中,有些函数被废弃了。由于这里不相关的原因,我不得不使用它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-04 20:34:24

这些函数不仅不受欢迎,它们在一段时间前就被删除了。所以你真的应该考虑升级。

不管怎样,至于你的问题:

1)不,没有比这更自由的了

2)不,使用sws_freeContext()

3)否,如果av_read_frame()返回错误,则数据包不包含任何有效数据

4)是的,你必须在处理完包后,在下一次av_read_frame()呼叫之前释放它

5)是的,它是完全有效的

6)不,编解码器上下文本身是由libavformat分配的,因此av_close_input_file()负责释放它。所以你没什么可做的了。

7)不,av_close_input_file()释放格式上下文,所以您应该没有什么可做的了。

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

https://stackoverflow.com/questions/18024835

复制
相关文章

相似问题

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