Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
ffmpeg-doc / usr / share / doc / ffmpeg-doc / api / avio_reading_8c-example.html
Size: Mime:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>FFmpeg: avio_reading.c</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">FFmpeg
   &#160;<span id="projectnumber">2.7.1</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">avio_reading.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2014 Stefano Sabatini</span>
<span class="comment"> *</span>
<span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="comment"> * of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="comment"> * in the Software without restriction, including without limitation the rights</span>
<span class="comment"> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="comment"> * copies of the Software, and to permit persons to whom the Software is</span>
<span class="comment"> * furnished to do so, subject to the following conditions:</span>
<span class="comment"> *</span>
<span class="comment"> * The above copyright notice and this permission notice shall be included in</span>
<span class="comment"> * all copies or substantial portions of the Software.</span>
<span class="comment"> *</span>
<span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="comment"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</span>
<span class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="comment"> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<span class="comment"> * THE SOFTWARE.</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * @file</span>
<span class="comment"> * libavformat AVIOContext API example.</span>
<span class="comment"> *</span>
<span class="comment"> * Make libavformat demuxer access media content through a custom</span>
<span class="comment"> * AVIOContext read callback.</span>
<span class="comment"> * @example avio_reading.c</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;<a class="code" href="libavcodec_2avcodec_8h.html" title="Libavcodec external API header.">libavcodec/avcodec.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="avformat_8h.html" title="Main libavformat public API header.">libavformat/avformat.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="avio_8h.html" title="Buffered I/O operations.">libavformat/avio.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="file_8h.html" title="Misc file utilities.">libavutil/file.h</a>&gt;</span>

<span class="keyword">struct </span><a name="_a0"></a><a class="code" href="structbuffer__data.html">buffer_data</a> {
    uint8_t *<a name="a1"></a><a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>;
    <span class="keywordtype">size_t</span> <a name="a2"></a><a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>; <span class="comment">///&lt; size left in the buffer</span>
<span class="comment"></span>};

<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a3"></a><a class="code" href="avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c">read_packet</a>(<span class="keywordtype">void</span> *opaque, uint8_t *buf, <span class="keywordtype">int</span> buf_size)
{
    <span class="keyword">struct </span><a class="code" href="structbuffer__data.html">buffer_data</a> *bd = (<span class="keyword">struct </span><a class="code" href="structbuffer__data.html">buffer_data</a> *)opaque;
    buf_size = <a name="a4"></a><a class="code" href="common_8h.html#a0f84cbd7fd810cd678157c98cbfad4b0">FFMIN</a>(buf_size, bd-&gt;<a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>);

    printf(<span class="stringliteral">&quot;ptr:%p size:%zu\n&quot;</span>, bd-&gt;<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>, bd-&gt;<a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>);

    <span class="comment">/* copy internal buffer data to buf */</span>
    memcpy(buf, bd-&gt;<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>, buf_size);
    bd-&gt;<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>  += buf_size;
    bd-&gt;<a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a> -= buf_size;

    <span class="keywordflow">return</span> buf_size;
}

<span class="keywordtype">int</span> <a name="a5"></a><a class="code" href="avio__list__dir_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
{
    <a name="_a6"></a><a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *<a name="a7"></a><a class="code" href="demuxing__decoding_8c.html#a409462acdf7aebe5bfbe1bcd811efa5d">fmt_ctx</a> = NULL;
    <a name="_a8"></a><a class="code" href="structAVIOContext.html" title="Bytestream IO Context.">AVIOContext</a> *avio_ctx = NULL;
    uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
    <span class="keywordtype">size_t</span> buffer_size, avio_ctx_buffer_size = 4096;
    <span class="keywordtype">char</span> *input_filename = NULL;
    <span class="keywordtype">int</span> ret = 0;
    <span class="keyword">struct </span><a class="code" href="structbuffer__data.html">buffer_data</a> bd = { 0 };

    <span class="keywordflow">if</span> (argc != 2) {
        fprintf(stderr, <span class="stringliteral">&quot;usage: %s input_file\n&quot;</span>
                <span class="stringliteral">&quot;API example program to show how to read from a custom buffer &quot;</span>
                <span class="stringliteral">&quot;accessed through AVIOContext.\n&quot;</span>, argv[0]);
        <span class="keywordflow">return</span> 1;
    }
    input_filename = argv[1];

    <span class="comment">/* register codecs and formats and other lavf/lavc components*/</span>
    <a name="a9"></a><a class="code" href="group__lavf__core.html#ga917265caec45ef5a0646356ed1a507e3" title="Initialize libavformat and register all the muxers, demuxers and protocols.">av_register_all</a>();

    <span class="comment">/* slurp file content into buffer */</span>
    ret = <a name="a10"></a><a class="code" href="file_8h.html#a20ab80637ab92af5fbbe4c00be406ad3" title="Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...">av_file_map</a>(input_filename, &amp;buffer, &amp;buffer_size, 0, NULL);
    <span class="keywordflow">if</span> (ret &lt; 0)
        <span class="keywordflow">goto</span> end;

    <span class="comment">/* fill opaque structure used by the AVIOContext read callback */</span>
    bd.<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>  = buffer;
    bd.<a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a> = buffer_size;

    <span class="keywordflow">if</span> (!(fmt_ctx = <a name="a11"></a><a class="code" href="group__lavf__core.html#gac7a91abf2f59648d995894711f070f62" title="Allocate an AVFormatContext.">avformat_alloc_context</a>())) {
        ret = <a name="a12"></a><a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
        <span class="keywordflow">goto</span> end;
    }

    avio_ctx_buffer = <a name="a13"></a><a class="code" href="group__lavu__mem.html#ga9722446c5e310ffedfaac9489864796d" title="Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...">av_malloc</a>(avio_ctx_buffer_size);
    <span class="keywordflow">if</span> (!avio_ctx_buffer) {
        ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
        <span class="keywordflow">goto</span> end;
    }
    avio_ctx = <a name="a14"></a><a class="code" href="avio_8h.html#a853f5149136a27ffba3207d8520172a5" title="Allocate and initialize an AVIOContext for buffered I/O.">avio_alloc_context</a>(avio_ctx_buffer, avio_ctx_buffer_size,
                                  0, &amp;bd, &amp;<a class="code" href="avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c">read_packet</a>, NULL, NULL);
    <span class="keywordflow">if</span> (!avio_ctx) {
        ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
        <span class="keywordflow">goto</span> end;
    }
    fmt_ctx-&gt;<a name="a15"></a><a class="code" href="structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87" title="I/O context.">pb</a> = avio_ctx;

    ret = <a name="a16"></a><a class="code" href="group__lavf__decoding.html#ga10a404346c646e4ab58f4ed798baca32" title="Open an input stream and read the header.">avformat_open_input</a>(&amp;fmt_ctx, NULL, NULL, NULL);
    <span class="keywordflow">if</span> (ret &lt; 0) {
        fprintf(stderr, <span class="stringliteral">&quot;Could not open input\n&quot;</span>);
        <span class="keywordflow">goto</span> end;
    }

    ret = <a name="a17"></a><a class="code" href="group__lavf__decoding.html#gad42172e27cddafb81096939783b157bb" title="Read packets of a media file to get stream information.">avformat_find_stream_info</a>(fmt_ctx, NULL);
    <span class="keywordflow">if</span> (ret &lt; 0) {
        fprintf(stderr, <span class="stringliteral">&quot;Could not find stream information\n&quot;</span>);
        <span class="keywordflow">goto</span> end;
    }

    <a name="a18"></a><a class="code" href="group__lavf__misc.html#gae2645941f2dc779c307eb6314fd39f10" title="Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.">av_dump_format</a>(fmt_ctx, 0, input_filename, 0);

end:
    <a name="a19"></a><a class="code" href="group__lavf__decoding.html#gae804b99aec044690162b8b9b110236a4" title="Close an opened input AVFormatContext.">avformat_close_input</a>(&amp;fmt_ctx);
    <span class="comment">/* note: the internal buffer could have changed, and be != avio_ctx_buffer */</span>
    <span class="keywordflow">if</span> (avio_ctx) {
        <a name="a20"></a><a class="code" href="group__lavu__mem.html#ga0cc84043ea2167ad005c86e11d0bcdba" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...">av_freep</a>(&amp;avio_ctx-&gt;<a name="a21"></a><a class="code" href="structAVIOContext.html#a901e8fd2b978d40957c4ed0b77c2daf0" title="Start of the buffer.">buffer</a>);
        <a class="code" href="group__lavu__mem.html#ga0cc84043ea2167ad005c86e11d0bcdba" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...">av_freep</a>(&amp;avio_ctx);
    }
    <a name="a22"></a><a class="code" href="file_8h.html#a12e643af4a329ae6d1d567d8a782af1e" title="Unmap or free the buffer bufptr created by av_file_map().">av_file_unmap</a>(buffer, buffer_size);

    <span class="keywordflow">if</span> (ret &lt; 0) {
        fprintf(stderr, <span class="stringliteral">&quot;Error occurred: %s\n&quot;</span>, <a name="a23"></a><a class="code" href="group__lavu__error.html#gac4f6e109c242c81aeee21868d3f35e12" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_err2str</a>(ret));
        <span class="keywordflow">return</span> 1;
    }

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>