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

Eric Beuque eric.beuque at gmail.com
Thu Dec 5 13:33:18 CET 2013


On Thu, Dec 5, 2013 at 12:41 PM, Paul B Mahol <onemda at gmail.com> wrote:

> On 12/5/13, Eric Beuque <eric.beuque at gmail.com> wrote:
> > 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_register_all();
> >
> >     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
> >
> >     av_free_packet(&packet);
> >     avcodec_close(pAVContext);
> >     av_free(pAVContext);
> >
> >     // Here pFrame->data has been freed, access may crash
> >
> >     av_free(pFrame);
> >     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.
> >
>
> Use reference counting system.
> They you need to manually free frame once you will not need it
> (otherwise you leak memory), so you do not need to do copy.
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>

Thank you for your answer, i saw that av_frame_ref/av_frame_unref is
avaliable in FFMpeg 2.0, but i'm using 1.1.2, and I don't find any way to
use reference couting in this version. Is this feature available in 1.1.x
throught some others functions?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20131205/3a0eba28/attachment.html>


More information about the Libav-user mailing list