使用av_frame_get_buffer()时发生内存泄漏

时间:2018-07-19 00:16:28

标签: c++ c windows ffmpeg libav

我正在用ffmpeg制作一个简单的视频播放器。我注意到有一个源自libavutil的内存泄漏。因为ffmpeg是成熟的库,所以我假设我分配的帧不正确。该文档还含糊不清释放调用av_frame_get_buffer()时创建的缓冲区。下面是我用来解码视频并将其排队以显示在UI线程上的代码。

DWORD WINAPI DecoderThread(LPVOID lpParam)
{
    AVFrame *frame = NULL;
    AVPacket pkt;

    SwsContext *swsCtx = NULL;
    UINT8 *buffer = NULL;
    INT iNumBytes = 0;

    INT result = 0;

    frame = av_frame_alloc();

    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    // Create scaling context
    swsCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt, codecCtx->width, codecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);

   while (av_read_frame(fmtCtx, &pkt) >= 0) {
        if (pkt.stream_index == videoStream) {
            result = avcodec_send_packet(codecCtx, &pkt);

            while (result >= 0) {
                result = avcodec_receive_frame(codecCtx, frame);
                if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) {
                    break;
                } else if (result < 0) {
                    // another error.
                }
                // Create a new frame to store the RGB24 data.
                AVFrame *pFrameRGB = av_frame_alloc();
                // Allocate space for the new RGB image.
                //av_image_alloc(pFrameRGB->data, pFrameRGB->linesize, codecCtx->width, codecCtx->height, AV_PIX_FMT_BGR24, 1);
                // Copy all of the properties from the YUV420P frame.
                av_frame_copy_props(pFrameRGB, frame);
                pFrameRGB->width = frame->width;
                pFrameRGB->height = frame->height;
                pFrameRGB->format = AV_PIX_FMT_BGR24;
                av_frame_get_buffer(pFrameRGB, 0);

                // Convert fram from YUV420P to BGR24 for display.
                sws_scale(swsCtx, (const UINT8* const *) frame->data, frame->linesize, 0, codecCtx->height, pFrameRGB->data, pFrameRGB->linesize);

                // Queue thr BGR frame for drawing by the main thread.
                AddItemToFrameQueue(pFrameRGB);

                av_frame_unref(frame);
            }
        }
       while (GetQueueSize() > 100) {
            Sleep(10);
       }
   }

    CloseFrameQueue();

    av_frame_free(&frame);
   avcodec_close(codecCtx);
   avformat_close_input(&fmtCtx);

    return 0;
}

是否有更好的方法来分配新的帧来保存发布后的sws_scale()转换?

有一个类似的stackoverflow question,它主要使用已贬值的函数调用。我似乎在文档中找不到符合ffmpeg新版本的答案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

按照评论中的建议,我向解码循环添加了一个av_packet_unref()调用,它停止了我遇到的内存泄漏问题。

                sws_scale(swsCtx, (const UINT8* const *) frame->data, frame->linesize, 0, codecCtx->height, pFrameRGB->data, pFrameRGB->linesize);

            // Queue thr BGR frame for drawing by the main thread.
            AddItemToFrameQueue(pFrameRGB);

            av_frame_unref(frame);
        }
        av_packet_unref(&pkt);
    }
    while (GetQueueSize() > 100) {
        Sleep(10);
    }
相关问题