[Libav-user] How to keep AVFrame data after codec close

Eric Beuque eric.beuque at gmail.com
Thu Dec 5 10:33:11 CET 2013

Hi, i'm using libavcodec from FFMpeg 1.1.2.

I'm decoding MJPEG stream. My problem is that i need to keep a reference to
the decoded frame, after the codec close. But when using avcodec_close, it
freed the AVFrame data.

Here a sample program showing the function i use:

int main(int argc, char **argv)
    int bGotPicture;


    AVCodec* pCodec = avcodec_find_decoder (CODEC_ID_MJPEG);
    AVCodecContext* pAVContext = avcodec_alloc_context3 (pCodec);

    avcodec_open2(pAVContext, pCodec, NULL);

    AVPacket packet;
    av_init_packet (&packet);

    size_t iSize;
    std::string content = get_file_contents("../img1.jpg", iSize);
    packet.data = (unsigned char*)content.c_str();
    packet.size = iSize;

    AVFrame* pFrame = avcodec_alloc_frame();
    avcodec_decode_video2 (pAVContext, pFrame, &bGotPicture, &packet);

    // Here pFrame->data is valid


    // Here pFrame->data has been freed, access may crash

    pFrame = NULL;

How i can tell libavcodec that i want to become the owner of the data and
be able to free it, to without codec context instance? I was thinking about
using get/release_buffer functions, but i don't know how to compute size of
the data.

Is data memcpy the only way to do this?

Note, that i also can't upgrade to new version of FFMPEG.

Thanks for your help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20131205/ac610e02/attachment.html>

More information about the Libav-user mailing list