ffmpeg RTSP流解码内存泄漏

时间:2019-02-06 15:28:26

标签: qt memory-leaks ffmpeg rtsp

我需要使用ffmpeg从ip摄像机解码rtsp流,以下是解码器的代码

ffmpeg_decoder.h

class ffmpeg_decoder
{
public:
    ffmpeg_decoder();
    int initial(QString & url);
    int h264Decodec();
    void close_stream();
    virtual ~ffmpeg_decoder();
    AVPicture  picture;
    int width;
    int height;
    QMutex mutex;
    QImage imageDecoded;

private:
    AVFormatContext *pFormatCtx;
    AVCodecContext *pCodecCtx;
    AVFrame *pFrame;
    AVPacket packet;


    SwsContext * pSwsCtx;
    int videoStream;

    QString rtspURL;

};

ffmpeg_decoder.cpp

ffmpeg_decoder::ffmpeg_decoder()
{
    pCodecCtx = NULL;
    videoStream=-1;

}

ffmpeg_decoder::~ffmpeg_decoder()
{
    sws_freeContext(pSwsCtx);
}

int ffmpeg_decoder::initial(QString & url)
{
    int err;
    rtspURL=url;
    AVCodec *pCodec;
    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();
    pFrame = av_frame_alloc();
    err = avformat_open_input(&pFormatCtx, rtspURL.toStdString().c_str(), NULL,
                              NULL);
    if (err < 0)
    {
        printf("Can not open this file");
        return -1;
    }
    if (avformat_find_stream_info(pFormatCtx,NULL) < 0)
    {
        printf("Unable to get stream info");
        return -1;
    }
    int i = 0;
    videoStream = -1;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
    {
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoStream = i;
            break;
        }
    }
    if (videoStream == -1)
    {
        printf("Unable to find video stream");
        return -1;
    }
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;

    width=pCodecCtx->width;
    height=pCodecCtx->height;
    avpicture_alloc(&picture,PIX_FMT_RGB24,pCodecCtx->width,pCodecCtx->height);
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    pSwsCtx = sws_getContext(width, height, PIX_FMT_YUV420P, width,
            height, PIX_FMT_RGB24,
            SWS_BICUBIC, 0, 0, 0);

    if (pCodec == NULL)
    {
        printf("Unsupported codec");
        return -1;
    }
    printf("video size : width=%d height=%d \n", pCodecCtx->width,
           pCodecCtx->height);
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("Unable to open codec");
        return -1;
    }
    printf("initial successfully");
    return 0;
}

int ffmpeg_decoder::h264Decodec()
{
    int frameFinished=0;
   // while (av_read_frame(pFormatCtx, &packet) >= 0)

    if(av_read_frame(pFormatCtx, &packet) >= 0)
    {
        if(packet.stream_index==videoStream)
        {
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
            if (frameFinished)
            {
                printf("***************ffmpeg decodec*******************\n");
                mutex.lock();
                int rs = sws_scale(pSwsCtx, (const uint8_t* const *) pFrame->data,
                                   pFrame->linesize, 0,
                                   height, picture.data, picture.linesize);

               imageDecoded = QImage();
               imageDecoded= QImage(this->picture.data[0],this->width,this->height,QImage::Format_RGB888);
               //imageDecoded = imageDecoded.copy();
                mutex.unlock();

                if (rs == -1)
                {
                    printf("__________Can open to change to des imag_____________e\n");
                    return -1;
                }
            }
        }
    }
    av_free_packet(&packet);
    av_frame_unref(pFrame);
    av_packet_unref(&packet);
    avpicture_free(&picture);


    return 1;

}

void ffmpeg_decoder::close_stream(){

    /*if (pFrame)
        av_free(&pFrame);*/

    if (pCodecCtx)
        avcodec_close(pCodecCtx);

    if (pSwsCtx)
        sws_freeContext(pSwsCtx);

    avpicture_free(&picture);

    if (pFormatCtx)
        avformat_close_input(&pFormatCtx);



}

下面是进行解码的主线程。

我正在使用Qt创建线程并进行解码

ffmpeg_decoder * ffmpeg =  new ffmpeg_decoder();;

if(ffmpeg->initial(rtspURL)==0){
    while (1) {

        ffmpeg->h264Decodec();
        //get frame and do processing right now it disabled, and still see the memory leak.
           .......

        if(stopFlg.load()==1)
            break;

    }
    //close stream if break
    ffmpeg->close_stream();

   }
   else {

       ffmpeg->close_stream();
   }

当我使用不同的URL运行36个线程时,我可以看到程序的内存使用量随时间增加。

我已经使用valgrind来检测泄漏,这是日志的相关部分

这是第一个内存泄漏位置

=14402==    by 0x692017F: av_malloc (in /usr/lib/x86_64-linux-gnu/libavutil-ffmpeg.so.54.31.100)
==14402==    by 0x692048D: av_mallocz (in /usr/lib/x86_64-linux-gnu/libavutil-ffmpeg.so.54.31.100)
==14402==    by 0x691915E: av_frame_alloc (in /usr/lib/x86_64-linux-gnu/libavutil-ffmpeg.so.54.31.100)
==14402==    by 0x419663: ffmpeg_decoder::initial(QString&) (ffmpeg_decoder.cpp:24)
==14402==    by 0x41ABEC: RTSP_Player_Object::run() (rtsp_player_object.cpp:15)

另一个

==14402== 2,176 bytes in 16 blocks are indirectly lost in loss record 23,113 of 23,379
==14402==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14402==    by 0x7780A4E: QImageData::create(unsigned char*, int, int, int, QImage::Format, bool, void (*)(void*), void*) (in /home/vapplica/Qt5.11.1/5.11.1/gcc_64/lib/libQt5Gui.so.5.11.1)
==14402==    by 0x7780C30: QImage::QImage(unsigned char*, int, int, QImage::Format, void (*)(void*), void*) (in /home/vapplica/Qt5.11.1/5.11.1/gcc_64/lib/libQt5Gui.so.5.11.1)
==14402==    by 0x419B21: ffmpeg_decoder::h264Decodec() (ffmpeg_decoder.cpp:96)

我已经检查了ffmpeg网站上的文档和示例,并且我认为我正在释放分配的内存,但是运行程序时仍然可以看到内存泄漏。

0 个答案:

没有答案