[FFmpeg-devel] Allocating and deallocating AVFrame & AVPicture

Matthew Einhorn moiein2000 at gmail.com
Mon Feb 27 19:24:33 CET 2012


On Mon, Feb 27, 2012 at 12:22 PM, John Dexter <jdxsolutions at gmail.com> wrote:
>
> I record rendered output of my 3D engine to video using ffmpeg libs.
> I'm finding after a minute or so (a few hundred frames),
> avpicture_alloc suddenly fails in my frame method:
>
> bool frame( void *buf,unsigned int inWidth,unsigned int inHeight)
> {
>        AVFrame *pFrame = avcodec_alloc_frame();
>        if (!pFrame) {
>                error("Can't allocate memory for AVFrame");
>                return false;
>        }
>
>    avcodec_get_frame_defaults(pFrame);
>    if(avpicture_alloc((AVPicture*)pFrame, PIX_FMT_BGRA, inWidth,
> inHeight)<0)
>        {
>                error("Failed allocating frame in avpicture_alloc()");
>                return false;
>        }
>
> .... //do other stuff
>
>        av_free(pFrame);
> }
>
>

avcodec_alloc_frame() allocates the frame, but not the buffer in which
the pic would be stored. avpicture_alloc() allocates the actuall
buffer (you can do similar with av_image_alloc()). So when freeing,
av_free(pFrame) frees the frame not the buffer, so you also have to
call av_free(pFrame.data[0]) before to free the buffer.

Matt


More information about the ffmpeg-devel mailing list