<div dir="ltr">Dear nfxjfg:<div>    Seems it's still not all right for the solution which you mentioned.</div><div>    I write the codes like following:</div><div>        1. pPacket  = av_malloc(sizeof(AVPacket))</div>
<div>        2. av_init_packet(pPacket);</div><div>        3. av_read_frame(pPacket);</div><div>        4. put_queue(packet);</div><div><br></div><div>    But it seems for a little situation, the frame will contain garbage. About 0.5s per 30 seconds I can reproduce this issue.</div>
<div>    Is there another way I can improve my code?</div><div>    Thanks very much.</div><div><br></div><div>Sincerely</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-06-26 9:17 GMT-04:00 Xiemin Chen <span dir="ltr"><<a href="mailto:chenxiemin@gmail.com" target="_blank">chenxiemin@gmail.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear <span style="color:rgb(85,85,85);font-family:arial,sans-serif;font-size:14px;white-space:nowrap">nfxjfg:</span><div>
<span style="color:rgb(85,85,85);font-family:arial,sans-serif;font-size:14px;white-space:nowrap">    I just do av_malloc(sizeof(AVPacket)) instead of define local variable AVPacket packet before av_read_frame().</span></div>

<div><span style="color:rgb(85,85,85);font-family:arial,sans-serif;font-size:14px;white-space:nowrap">    And nothing else except for putting into queue, then seems that it does solve my problem. Thanks for your professional answer.</span></div>

<div><span style="color:rgb(85,85,85);font-family:arial,sans-serif;font-size:14px;white-space:nowrap">    Also thanks for indicating my </span><font color="#555555" face="arial, sans-serif"><span style="font-size:14px;white-space:nowrap">potential memory-leaking mistake.</span></font></div>

<div><font color="#555555" face="arial, sans-serif"><span style="font-size:14px;white-space:nowrap">    Thanks very much.</span></font></div><div><font color="#555555" face="arial, sans-serif"><span style="font-size:14px;white-space:nowrap"><br>

</span></font></div><div><font color="#555555" face="arial, sans-serif"><span style="font-size:14px;white-space:nowrap">Sincerely</span></font></div><div><font color="#555555" face="arial, sans-serif"><span style="font-size:14px;white-space:nowrap"><br>

</span></font></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-06-26 8:33 GMT-04:00 wm4 <span dir="ltr"><<a href="mailto:nfxjfg@googlemail.com" target="_blank">nfxjfg@googlemail.com</a>></span>:<div>
<div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On Wed, 25 Jun 2014 20:46:59 -0400<br>
Xiemin Chen <<a href="mailto:chenxiemin@gmail.com" target="_blank">chenxiemin@gmail.com</a>> wrote:<br>
<br>
> Dear all:<br>
>     I want to split av_read_frame() and avcodec_decode_video2() in to<br>
> different thread so I need to put a queue behind them. av_read_frame() read<br>
> a packet then put the packet into queue, avcodec_decode_video2() read a<br>
> packet from the queue then decode.<br>
>     But sometimes the decoded frames may contain rubbish, it's random. So I<br>
> think that AVPacket shares its data memory and overwrite by somebody before<br>
> I decode it. The following code like this:<br>
<br>
</div>Why do you think that? It could be something else. Did you test with<br>
valgrind?<br>
<div><br>
>          1. av_read_frame(&packet);<br>
>          2.     av_dup_packet(&packet);<br>
>          3.     AVPacket *newPakcet = av_malloc(sizeof(AVPacket));<br>
>          4.     *newPacket = packet;<br>
<br>
</div>It depends what you do with the original packet after this. If you<br>
unref it, the packet might be free'd. If you malloc the AVPacket before<br>
the av_read_frame call, and then put that into the queue, you should be<br>
fine.<br>
<div><br>
>          5.  put_queue(packet);<br>
>          6.  get_queue(&packet); (Another thread)<br>
>          7.  avcodec_decode_video2(packet, &frame);<br>
>          8.  show frame;<br>
>     Is there any mistake I took on these codes? Thanks very much.<br>
><br>
> Sincerely<br>
<br>
</div>AVPackets have other data than the packet buffer - at least side data.<br>
av_dup_packet() is an old API that AFAIK does nothing on packets<br>
returned by libavformat. (Yes, that is very misleading - and I'm not<br>
even sure if it _really_ does nothing.) Now you should use<br>
av_packet_ref(), which is a relatively recently added function. But<br>
it looks like you don't need it in your specific use-case.<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div></div></div><br></div>
</blockquote></div><br></div>