[Libav-user] Hello, I have some "memory problems" while using libavcodec in iOS platform

Lucian N lucian at phoenix-5.com
Fri Jan 29 09:32:54 EET 2021


My FFmpeg use version 4.3.1
While I'm using it to decode a rtmp stream from internet on iPad. It caused
memory leak after a long time.

By the code check line by line, I found memory increased about 0.1Mb per
10sec~12sec.
After a few hours, the memory usage would become very huge.

And It caused by the function:
"avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame),
which is implement in decode.c

I still not really figure out what caused the problem.

there is my code using avcodec_receive_frame below, I use it to decode each
frame.

- (BOOL)stepFrame

{



    int frameFinished=0;

    [lock lock];

    [self cleanupFrame];

    [self cleanupPacket];

    pFrame = av_frame_alloc();

    packet = av_packet_alloc();

    while (

           self != nil &&

           !frameFinished &&

           pFormatCtx != NULL && pFormatCtx != nil &&

           pFormatCtx->priv_data != NULL &&

           pFormatCtx->priv_data != nil &&

           av_read_frame(pFormatCtx, packet) >= 0 )

    {



        if(packet->stream_index==videoStream_id)

        {

            int ret = avcodec_send_packet(videoCodecCtx, packet);



            if( ret < 0 )

            {

                [self phoenixFFmpegLog:@"step frame send packet error"];

                return FALSE;

            }

            ret = 0;



            while( ret >= 0 )

            {



                ret = avcodec_receive_frame(videoCodecCtx, pFrame);

//                av_image_copy(tmp_framedata, tmp_linesize, (const
uint8_t**)pFrame->data , pFrame->linesize, videoCodecCtx->pix_fmt,
self.outputWidth, self.outputHeight);



//                av_frame_unref(pFrame);

                [self cleanupFrame];

                pFrame = av_frame_alloc();



                if( ret == AVERROR(EAGAIN) || ret == AVERROR_EOF )

                {

                    frameFinished = 1;

                    ret = 0;

                    break;

                }

            }



            if( ret < 0 )

            {

                [self phoenixFFmpegLog:@"step frame received error"];

                return FALSE;

            }

        }



        if (packet->stream_index==audioStream_id) {

            [_audioManager enqueuePacket:packet];



            if( !primed )

            {

                primed = YES;

                [_audioManager play];

            }

//            av_frame_unref(pFrame);

            [self cleanupFrame];

            pFrame = av_frame_alloc();

        }

    }



    if(frameFinished)

    {


//        sws_scale(img_convert_ctx,

//                  (const uint8_t* const *)tmp_framedata,

//                  tmp_linesize,

//                  0,

//                  self.outputHeight,

//                  picture_data,

//                  picture_linesize);


        av_freep(&tmp_framedata[0]);

        tmp_framedata[0] = nil;

        av_image_alloc(tmp_framedata, tmp_linesize, self.outputWidth, self.
outputHeight, AV_PIX_FMT_YUV420P, 1);

    }

//    av_frame_unref(pFrame);

    [self cleanupFrame];

    pFrame = av_frame_alloc();

    [self cleanupPacket];

    packet = av_packet_alloc();

    [lock unlock];

    return frameFinished!=0;

}

I use object-c with ffmpeg library to decode, I hope that could be help.
Besides, I had used av_frame_free in [self cleanupFrame], and
av_package_free in [self cleanupPackage].  but still happened the same
memory problem, although it increased 0.1Mb for abount each 10~12sec.

-(void) cleanupPacket

{

    av_packet_free(&packet);

    packet = nil;

}


-(void) cleanupFrame

{

    av_frame_free(&pFrame);

    av_free(pFrame);

    pFrame = nil;

}

Otherwise, I had thought if the problem caused by audio, but even I tried
not to include audio package and release the package of audio. the problem
is the same. It didn't work


The memory problem caused by the function "avcodec_receive_frame", I hope
there is some one to solve it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20210129/5e3a8bf3/attachment.html>


More information about the Libav-user mailing list