[Libav-user] How to keep AVFrame data after codec close
Paul B Mahol
onemda at gmail.com
Thu Dec 5 12:41:17 CET 2013
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.
More information about the Libav-user
mailing list