[Libav-user] How To Duplicate AVPacket

wm4 nfxjfg at googlemail.com
Thu Jun 26 14:33:18 CEST 2014

On Wed, 25 Jun 2014 20:46:59 -0400
Xiemin Chen <chenxiemin at gmail.com> wrote:

> Dear all:
>     I want to split av_read_frame() and avcodec_decode_video2() in to
> different thread so I need to put a queue behind them. av_read_frame() read
> a packet then put the packet into queue, avcodec_decode_video2() read a
> packet from the queue then decode.
>     But sometimes the decoded frames may contain rubbish, it's random. So I
> think that AVPacket shares its data memory and overwrite by somebody before
> I decode it. The following code like this:

Why do you think that? It could be something else. Did you test with

>          1. av_read_frame(&packet);
>          2.     av_dup_packet(&packet);
>          3.     AVPacket *newPakcet = av_malloc(sizeof(AVPacket));
>          4.     *newPacket = packet;

It depends what you do with the original packet after this. If you
unref it, the packet might be free'd. If you malloc the AVPacket before
the av_read_frame call, and then put that into the queue, you should be

>          5.  put_queue(packet);
>          6.  get_queue(&packet); (Another thread)
>          7.  avcodec_decode_video2(packet, &frame);
>          8.  show frame;
>     Is there any mistake I took on these codes? Thanks very much.
> Sincerely

AVPackets have other data than the packet buffer - at least side data.
av_dup_packet() is an old API that AFAIK does nothing on packets
returned by libavformat. (Yes, that is very misleading - and I'm not
even sure if it _really_ does nothing.) Now you should use
av_packet_ref(), which is a relatively recently added function. But
it looks like you don't need it in your specific use-case.

More information about the Libav-user mailing list