<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><div>That's how I allocate the context.</div><div><br></div><div>if ( (avioCxt_buffer = (uint8_t *) av_malloc(4096)) == NULL)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>avformat_close_input(outputFormatCxt);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>fprintf(stderr, "TranscoderWrapper @%d : failed to allocate memory for buffer", __LINE__);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return ERROR(ERR_OUT_MEMORY);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ( (avioCxt = avio_alloc_context(avioCxt_buffer, 4096, 0, &bd, read_packet, NULL, NULL)) == NULL)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>av_free(avioCxt_buffer);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>avformat_close_input(outputFormatCxt);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>fprintf(stderr, "TranscoderWrapper @%d : failed to allocate memory for context", __LINE__);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return ERROR(ERR_OUT_MEMORY);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>// set the manually read data buffer to the context</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>avioCxt->seekable = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(*outputFormatCxt)->pb = avioCxt;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if ( (ret = avformat_open_input(outputFormatCxt, NULL, NULL, NULL)) < 0)</div><div><br></div>then, I deallocate the context.<div><br></div><div><div>if (avioCxt)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>av_freep(&avioCxt->buffer);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>av_freep(&avioCxt);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><div><hr id="stopSpelling">From: fxtalgorn-at-yahoo.fr@ffmpeg.org<br>Date: Sat, 27 Jun 2015 14:43:23 +0200<br>To: libav-user@ffmpeg.org<br>Subject: Re: [Libav-user] Custom IO strange behavior.<br><br>Hi,<div><br></div><div>It would help if we could see how you allocate the AVIOContext, and your main loop.</div><div><br><div><div>Le 27 juin 2015 à 11:33, Mohamed Moanis <<a href="mailto:mohamed_moanis@outlook.com">mohamed_moanis@outlook.com</a>> a écrit :</div><br class="ecxApple-interchange-newline"><blockquote><div class="ecxhmmessage" style="font-size:12pt;font-family:Calibri;"><div dir="ltr"><div dir="ltr"><span style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;display:inline !important;">I am implementing a custom IO to read data packets then transcode them.</span><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;">The problem I have is memory leakage, which I guess is coming from the custom IO buffer I allocate to read the data.</div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;">I am referring to the avio example. I have exactly the same callback function.</div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;"><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"><span class="ecxkeyword" style="color:rgb(0, 128, 0);">struct </span><a name="_a0" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">buffer_data</a> {</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> <a name="a1" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/audio__convert_8c.html#ae1affc9ca37cfb624959c866a73f83c2" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">uint8_t</a> *<a name="a2" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">ptr</a>;</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> <span class="ecxkeywordtype" style="color:rgb(96, 64, 32);">size_t</span> <a name="a3" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">size</a>; <span class="comment" style="color:rgb(128, 0, 0);">///< size left in the buffer</span></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"><span class="comment" style="color:rgb(128, 0, 0);"></span>};</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"><span class="ecxkeyword" style="color:rgb(0, 128, 0);">static</span> <span class="ecxkeywordtype" style="color:rgb(96, 64, 32);">int</span> <a name="a4" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">read_packet</a>(<span class="ecxkeywordtype" style="color:rgb(96, 64, 32);">void</span> *opaque, <a class="code" href="https://www.ffmpeg.org/doxygen/2.3/audio__convert_8c.html#ae1affc9ca37cfb624959c866a73f83c2" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">uint8_t</a> *<a name="a5" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/avisynth__c_8h.html#a5bc5fa69bee375df074734a2c4858604" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">buf</a>, <span class="ecxkeywordtype" style="color:rgb(96, 64, 32);">int</span> buf_size)</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;">{</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> <span class="ecxkeyword" style="color:rgb(0, 128, 0);">struct </span><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">buffer_data</a> *bd = (<span class="ecxkeyword" style="color:rgb(0, 128, 0);">struct </span><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">buffer_data</a> *)opaque;</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> buf_size = <a name="a6" style="color:rgb(61, 87, 140);" target="_blank"></a><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/common_8h.html#a0f84cbd7fd810cd678157c98cbfad4b0" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">FFMIN</a>(buf_size, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">size</a>);</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> printf(<span class="ecxstringliteral" style="color:rgb(0, 32, 128);">"ptr:%p size:%zu\n"</span>, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">ptr</a>, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">size</a>);</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> <span class="comment" style="color:rgb(128, 0, 0);">/* copy internal buffer data to buf */</span></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> memcpy(buf, bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">ptr</a>, buf_size);</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">ptr</a> += buf_size;</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> bd-><a class="code" href="https://www.ffmpeg.org/doxygen/2.3/structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer" style="color:rgb(70, 101, 162);text-decoration:none;" target="_blank">size</a> -= buf_size;</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"></div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"> <span class="ecxkeywordflow" style="color:rgb(224, 128, 0);">return</span> buf_size;</div><div class="ecxline" style="font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;">}</div></div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;"><br></div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;">When I debug my code, the function is called even after the data was all read. It is called more than a time with the buffer size is zero and later on the ptr itself is null. My question here is why does that happen?</div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;"><br></div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;">The second question, is how to free the buffer allocated. In my application I require reading a JPEG frame from a camera that is considerable in size. What I do, is that I free the AVIOContext and it's buffer just after I call avformat_open_input. But I can see the memory usage increasing by an estimate of the frame size each run.</div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;"><br></div><div style="font-family:Calibri;font-size:16px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;">Regards.</div><div class="ecxline" style="font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-transform:none;word-spacing:0px;font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"><br></div><div class="ecxline" style="font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-transform:none;word-spacing:0px;font-size:13px;line-height:1;font-family:monospace, fixed;min-height:13px;white-space:pre-wrap;word-wrap:break-word;text-indent:-53px;padding-left:53px;padding-bottom:0px;transition:background-color 0.5s, box-shadow;"><br></div><br class="ecxApple-interchange-newline"></div></div></div></blockquote></div><br></div><br>_______________________________________________
Libav-user mailing list
Libav-user@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/libav-user</div></div></div> </div></body>
</html>