Repository URL to install this package:
Version:
7:2.7.1-1ubuntu3 ▾
|
<!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: doc/examples/avio_reading.c Source File</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
 <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 Page</span></a></li>
<li><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data Structures</span></a></li>
<li class="current"><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 id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File List</span></a></li>
<li><a href="globals.html"><span>Globals</span></a></li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">doc/examples/avio_reading.c</div> </div>
</div><!--header-->
<div class="contents">
<a href="avio__reading_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (c) 2014 Stefano Sabatini</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<a name="l00005"></a>00005 <span class="comment"> * of this software and associated documentation files (the "Software"), to deal</span>
<a name="l00006"></a>00006 <span class="comment"> * in the Software without restriction, including without limitation the rights</span>
<a name="l00007"></a>00007 <span class="comment"> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<a name="l00008"></a>00008 <span class="comment"> * copies of the Software, and to permit persons to whom the Software is</span>
<a name="l00009"></a>00009 <span class="comment"> * furnished to do so, subject to the following conditions:</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * The above copyright notice and this permission notice shall be included in</span>
<a name="l00012"></a>00012 <span class="comment"> * all copies or substantial portions of the Software.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<a name="l00015"></a>00015 <span class="comment"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<a name="l00016"></a>00016 <span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</span>
<a name="l00017"></a>00017 <span class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<a name="l00018"></a>00018 <span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<a name="l00019"></a>00019 <span class="comment"> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<a name="l00020"></a>00020 <span class="comment"> * THE SOFTWARE.</span>
<a name="l00021"></a>00021 <span class="comment"> */</span>
<a name="l00022"></a>00022 <span class="comment"></span>
<a name="l00023"></a>00023 <span class="comment">/**</span>
<a name="l00024"></a>00024 <span class="comment"> * @file</span>
<a name="l00025"></a>00025 <span class="comment"> * libavformat AVIOContext API example.</span>
<a name="l00026"></a>00026 <span class="comment"> *</span>
<a name="l00027"></a>00027 <span class="comment"> * Make libavformat demuxer access media content through a custom</span>
<a name="l00028"></a>00028 <span class="comment"> * AVIOContext read callback.</span>
<a name="l00029"></a>00029 <span class="comment"> * @example avio_reading.c</span>
<a name="l00030"></a>00030 <span class="comment"> */</span>
<a name="l00031"></a>00031
<a name="l00032"></a>00032 <span class="preprocessor">#include <<a class="code" href="libavcodec_2avcodec_8h.html" title="Libavcodec external API header.">libavcodec/avcodec.h</a>></span>
<a name="l00033"></a>00033 <span class="preprocessor">#include <<a class="code" href="avformat_8h.html" title="Main libavformat public API header.">libavformat/avformat.h</a>></span>
<a name="l00034"></a>00034 <span class="preprocessor">#include <<a class="code" href="avio_8h.html" title="Buffered I/O operations.">libavformat/avio.h</a>></span>
<a name="l00035"></a>00035 <span class="preprocessor">#include <<a class="code" href="file_8h.html" title="Misc file utilities.">libavutil/file.h</a>></span>
<a name="l00036"></a>00036
<a name="l00037"></a><a class="code" href="structbuffer__data.html">00037</a> <span class="keyword">struct </span><a class="code" href="structbuffer__data.html">buffer_data</a> {
<a name="l00038"></a><a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">00038</a> uint8_t *<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>;
<a name="l00039"></a><a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c">00039</a> <span class="keywordtype">size_t</span> <a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>; <span class="comment">///< size left in the buffer</span>
<a name="l00040"></a>00040 <span class="comment"></span>};
<a name="l00041"></a>00041
<a name="l00042"></a><a class="code" href="avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c">00042</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <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)
<a name="l00043"></a>00043 {
<a name="l00044"></a>00044 <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;
<a name="l00045"></a>00045 buf_size = <a class="code" href="common_8h.html#a0f84cbd7fd810cd678157c98cbfad4b0">FFMIN</a>(buf_size, bd-><a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>);
<a name="l00046"></a>00046
<a name="l00047"></a>00047 printf(<span class="stringliteral">"ptr:%p size:%zu\n"</span>, bd-><a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>, bd-><a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a>);
<a name="l00048"></a>00048
<a name="l00049"></a>00049 <span class="comment">/* copy internal buffer data to buf */</span>
<a name="l00050"></a>00050 memcpy(buf, bd-><a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a>, buf_size);
<a name="l00051"></a>00051 bd-><a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a> += buf_size;
<a name="l00052"></a>00052 bd-><a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a> -= buf_size;
<a name="l00053"></a>00053
<a name="l00054"></a>00054 <span class="keywordflow">return</span> buf_size;
<a name="l00055"></a>00055 }
<a name="l00056"></a>00056
<a name="l00057"></a><a class="code" href="avio__reading_8c.html#a0ddf1224851353fc92bfbff6f499fa97">00057</a> <span class="keywordtype">int</span> <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="l00058"></a>00058 {
<a name="l00059"></a>00059 <a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *<a class="code" href="demuxing__decoding_8c.html#a409462acdf7aebe5bfbe1bcd811efa5d">fmt_ctx</a> = NULL;
<a name="l00060"></a>00060 <a class="code" href="structAVIOContext.html" title="Bytestream IO Context.">AVIOContext</a> *avio_ctx = NULL;
<a name="l00061"></a>00061 uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
<a name="l00062"></a>00062 <span class="keywordtype">size_t</span> buffer_size, avio_ctx_buffer_size = 4096;
<a name="l00063"></a>00063 <span class="keywordtype">char</span> *input_filename = NULL;
<a name="l00064"></a>00064 <span class="keywordtype">int</span> ret = 0;
<a name="l00065"></a>00065 <span class="keyword">struct </span><a class="code" href="structbuffer__data.html">buffer_data</a> bd = { 0 };
<a name="l00066"></a>00066
<a name="l00067"></a>00067 <span class="keywordflow">if</span> (argc != 2) {
<a name="l00068"></a>00068 fprintf(stderr, <span class="stringliteral">"usage: %s input_file\n"</span>
<a name="l00069"></a>00069 <span class="stringliteral">"API example program to show how to read from a custom buffer "</span>
<a name="l00070"></a>00070 <span class="stringliteral">"accessed through AVIOContext.\n"</span>, argv[0]);
<a name="l00071"></a>00071 <span class="keywordflow">return</span> 1;
<a name="l00072"></a>00072 }
<a name="l00073"></a>00073 input_filename = argv[1];
<a name="l00074"></a>00074
<a name="l00075"></a>00075 <span class="comment">/* register codecs and formats and other lavf/lavc components*/</span>
<a name="l00076"></a>00076 <a class="code" href="group__lavf__core.html#ga917265caec45ef5a0646356ed1a507e3" title="Initialize libavformat and register all the muxers, demuxers and protocols.">av_register_all</a>();
<a name="l00077"></a>00077
<a name="l00078"></a>00078 <span class="comment">/* slurp file content into buffer */</span>
<a name="l00079"></a>00079 ret = <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, &buffer, &buffer_size, 0, NULL);
<a name="l00080"></a>00080 <span class="keywordflow">if</span> (ret < 0)
<a name="l00081"></a>00081 <span class="keywordflow">goto</span> end;
<a name="l00082"></a>00082
<a name="l00083"></a>00083 <span class="comment">/* fill opaque structure used by the AVIOContext read callback */</span>
<a name="l00084"></a>00084 bd.<a class="code" href="structbuffer__data.html#a017ad9c72fdc7666c29841ddeefc8cd8">ptr</a> = buffer;
<a name="l00085"></a>00085 bd.<a class="code" href="structbuffer__data.html#a9ca6d8ef5edd7f113c04c865f9367f8c" title="size left in the buffer">size</a> = buffer_size;
<a name="l00086"></a>00086
<a name="l00087"></a>00087 <span class="keywordflow">if</span> (!(fmt_ctx = <a class="code" href="group__lavf__core.html#gac7a91abf2f59648d995894711f070f62" title="Allocate an AVFormatContext.">avformat_alloc_context</a>())) {
<a name="l00088"></a>00088 ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
<a name="l00089"></a>00089 <span class="keywordflow">goto</span> end;
<a name="l00090"></a>00090 }
<a name="l00091"></a>00091
<a name="l00092"></a>00092 avio_ctx_buffer = <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);
<a name="l00093"></a>00093 <span class="keywordflow">if</span> (!avio_ctx_buffer) {
<a name="l00094"></a>00094 ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
<a name="l00095"></a>00095 <span class="keywordflow">goto</span> end;
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 avio_ctx = <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,
<a name="l00098"></a>00098 0, &bd, &<a class="code" href="avio__reading_8c.html#a3a86bff38e7cb9093d0a32316b73b29c">read_packet</a>, NULL, NULL);
<a name="l00099"></a>00099 <span class="keywordflow">if</span> (!avio_ctx) {
<a name="l00100"></a>00100 ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
<a name="l00101"></a>00101 <span class="keywordflow">goto</span> end;
<a name="l00102"></a>00102 }
<a name="l00103"></a>00103 fmt_ctx-><a class="code" href="structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87" title="I/O context.">pb</a> = avio_ctx;
<a name="l00104"></a>00104
<a name="l00105"></a>00105 ret = <a class="code" href="group__lavf__decoding.html#ga10a404346c646e4ab58f4ed798baca32" title="Open an input stream and read the header.">avformat_open_input</a>(&fmt_ctx, NULL, NULL, NULL);
<a name="l00106"></a>00106 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00107"></a>00107 fprintf(stderr, <span class="stringliteral">"Could not open input\n"</span>);
<a name="l00108"></a>00108 <span class="keywordflow">goto</span> end;
<a name="l00109"></a>00109 }
<a name="l00110"></a>00110
<a name="l00111"></a>00111 ret = <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);
<a name="l00112"></a>00112 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00113"></a>00113 fprintf(stderr, <span class="stringliteral">"Could not find stream information\n"</span>);
<a name="l00114"></a>00114 <span class="keywordflow">goto</span> end;
<a name="l00115"></a>00115 }
<a name="l00116"></a>00116
<a name="l00117"></a>00117 <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);
<a name="l00118"></a>00118
<a name="l00119"></a>00119 end:
<a name="l00120"></a>00120 <a class="code" href="group__lavf__decoding.html#gae804b99aec044690162b8b9b110236a4" title="Close an opened input AVFormatContext.">avformat_close_input</a>(&fmt_ctx);
<a name="l00121"></a>00121 <span class="comment">/* note: the internal buffer could have changed, and be != avio_ctx_buffer */</span>
<a name="l00122"></a>00122 <span class="keywordflow">if</span> (avio_ctx) {
<a name="l00123"></a>00123 <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>(&avio_ctx-><a class="code" href="structAVIOContext.html#a901e8fd2b978d40957c4ed0b77c2daf0" title="Start of the buffer.">buffer</a>);
<a name="l00124"></a>00124 <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>(&avio_ctx);
<a name="l00125"></a>00125 }
<a name="l00126"></a>00126 <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);
<a name="l00127"></a>00127
<a name="l00128"></a>00128 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00129"></a>00129 fprintf(stderr, <span class="stringliteral">"Error occurred: %s\n"</span>, <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));
<a name="l00130"></a>00130 <span class="keywordflow">return</span> 1;
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132
<a name="l00133"></a>00133 <span class="keywordflow">return</span> 0;
<a name="l00134"></a>00134 }
</pre></div></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"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </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  <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>