<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hello libav users.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m a writing to hopefully expedite my task of eliminating a memory leak and assert0 crash when remuxing AVPackets.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I used the remuxing example at <a href="https://ffmpeg.org/doxygen/2.8/remuxing_8c-example.html">
https://ffmpeg.org/doxygen/2.8/remuxing_8c-example.html</a> as a reference for writing my code.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The remux example was a great help to me, but it only opens an input file, reads the AVPackets out, and writes these AVPackets to an output file and then exists.
<o:p></o:p></p>
<p class="MsoNormal">My code opens an RTSP input URL, continuously reads H.264 AVPackets from the RTSP input, and writes these unmodified H.264 AVPackets out to an MP4 file with a fixed duration of 30 seconds. This means after writing out 30 seconds worth
of AVPackets, I close only the AVFormatContext for the MP4 output file and I leave the RTSP input open so I don’t miss reading any frames from the camera. I then open a new AVFormatContext for the next 30 second MP4 clip and continue writing out the H.264
AVPackets from the RTSP input. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Unfortunately, every time I open/close an MP4 output file, a memory leak occurs amounting to 250KB. I don’t know if this memory leak occurs in the remuxing example because the remuxing example code closes both the input and output and
exits thereby eliminating any memory leaks when the OS terminates the process. <o:p>
</o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">To get my code to run without crashing, I had to comment out a section of code that calls int attribute_align_arg avcodec_encode_video2(…) where it passes in NULL AVFrame’s and then calls
<span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
libffmpeg::av_interleaved_write_frame</span><span style="font-size:9.5pt;font-family:Consolas;color:black">()</span> to apparently flush any/all delayed AVPackets. The AVPacket (for flushing) is initialized with .size = 0 and .data = NULL, before calling avcodec_encode_video2().<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The crash (when running the flush code) occurs in avcodec_encode_video2() within libavcodec/utils.c when the following assert0 is called:<o:p></o:p></p>
<p class="MsoNormal">av_assert0(avctx->codec->encode2);<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Apparently the input AvCodecContext does not contain an encoding AvCodec.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The avctx and codec are created the same way as the remuxing example as follows:<o:p></o:p></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->VideoStream
= libffmpeg::avformat_new_stream(</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->FormatContext,
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">pRtspInputAvCodecContext</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->codec);<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> ret = avcodec_copy_context(</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->VideoStream->codec,
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">pRtspInputAvCodecContext</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">);<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white;mso-highlight:white">if</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> (ret < 0)
<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> {<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> fprintf(</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;background:white;mso-highlight:white">stderr</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">,
</span><span style="font-size:9.5pt;font-family:Consolas;color:#A31515;background:white;mso-highlight:white">"Failed to copy context from input to output stream codec context\n"</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">);<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> }<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white;mso-highlight:white">else</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> {<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->VideoStream->codec->codec_tag = 0;<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue;background:white;mso-highlight:white">if</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> (</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->FormatContext->oformat->flags
& </span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;background:white;mso-highlight:white">AVFMT_GLOBALHEADER</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">)<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> {<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;background:white;mso-highlight:white">data</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">->VideoStream->codec->flags |=
</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;background:white;mso-highlight:white">CODEC_FLAG_GLOBAL_HEADER</span><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white">;<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black;background:white;mso-highlight:white"> }</span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Does anyone know if flushing is required to eliminate the memory leak and if so, then how do I get flushing to work without running into the assert0 as described above?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D">Thank you,<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D">Jeff Mueller<br>
<i>Senior Systems Engineer</i><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;color:#1F497D"><img border="0" width="165" height="41" id="Picture_x0020_1" src="cid:image001.png@01D175FF.4D40FCF0" alt="Beveled Wi-Tronix logo no shadow"></span><span style="font-size:10.0pt;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D">631 E Boughton Rd, Suite 240 | Bolingbrook, IL 60440-3455 | </span><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D"><a href="mailto:jmueller@wi-tronix.com"><span style="font-size:8.0pt;color:#1F497D">jmueller@wi-tronix.com</span></a>
</span><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D">| <a href="http://goo.gl/maps/WgZCJ"><span style="color:#1F497D">Directions & Map</span></a></span><span style="font-size:10.0pt;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D"><img border="0" width="15" height="15" id="Picture_x0020_2" src="cid:image002.jpg@01D175FF.4D40FCF0" alt="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS9ii3VxAces63_UOaGvunLMLuIVzGCeDLCZaJyWXYeUPh18S7ESw"> (630)
679-9927 Ext. xxx | <img border="0" width="15" height="15" id="Picture_x0020_3" src="cid:image003.png@01D175FF.4D40FCF0" alt="http://cpanelcloudvps.com/wp-content/uploads/2013/12/cell-phone-logo-pnghs-enterprises-9fttttbq.png"> (847) xxx-xxxx | Toll Free:
(888) WI-TRONIX (948-7664) | <img border="0" width="18" height="18" id="Picture_x0020_4" src="cid:image004.png@01D175FF.4D40FCF0" alt="http://png-1.findicons.com/files/icons/1580/devine_icons_part_2/128/printer_and_fax.png"> (630) 679-9954 <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;color:#1F497D"> </span><a href="https://www.facebook.com/pages/Wi-Tronix/167958306554722"><span style="color:#1F497D;text-decoration:none"><img border="0" width="32" height="32" id="Picture_x0020_5" src="cid:image005.png@01D175FF.4D40FCF0" alt="cid:image001.png@01CEBDFF.75DF9460"></span></a><span style="font-size:10.0pt;color:#1F497D"> </span><a href="https://twitter.com/WiTronix"><span style="color:#1F497D;text-decoration:none"><img border="0" width="32" height="32" id="Picture_x0020_6" src="cid:image006.png@01D175FF.4D40FCF0" alt="cid:image002.png@01CEBDFF.75DF9460"></span></a><span style="color:#1F497D">
</span><a href="http://www.linkedin.com/company/wi-tronix-llc?trk=cp_followed_name_wi-tronix-llc"><span style="color:#1F497D;text-decoration:none"><img border="0" width="32" height="32" id="Picture_x0020_7" src="cid:image007.png@01D175FF.4D40FCF0" alt="cid:image003.png@01CEBDFF.75DF9460"></span></a><span style="font-size:10.0pt;color:#1F497D">
</span><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:3.0pt;margin-right:0in;margin-bottom:3.0pt;margin-left:0in">
<span style="font-size:7.0pt;font-family:"Arial",sans-serif;color:#1F497D">CONFIDENTIALITY NOTICE<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:3.0pt;margin-right:0in;margin-bottom:3.0pt;margin-left:0in">
<span style="font-size:7.0pt;font-family:"Arial",sans-serif;color:#1F497D">This email and any attached files are confidential, protected by copyright and may be legally privileged. If you are not the intended addressee or have received the e-mail in error,
any use of this e-mail or any copying, distribution or other dissemination of it is strictly prohibited. If you have received this transmission in error, please notify the sender immediately and then delete the e-mail. E-mail cannot be guaranteed to be secure,
error free or free from viruses. Wi-Tronix, LLC and its affiliates do not accept any liability whatsoever for any loss or damage which may be caused as a result of the transmission of this message by e-mail. If verification is required, please request a hard
copy version.</span><span style="font-size:8.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>