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: demuxing_decoding.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
 <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><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">demuxing_decoding.c</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2012 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 "Software"), 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 "AS IS", 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"> * Demuxing and decoding example.</span>
<span class="comment"> *</span>
<span class="comment"> * Show how to use the libavformat and libavcodec API to demux and</span>
<span class="comment"> * decode audio and video data.</span>
<span class="comment"> * @example demuxing_decoding.c</span>
<span class="comment"> */</span>
<span class="preprocessor">#include <<a class="code" href="imgutils_8h.html" title="misc image utilities">libavutil/imgutils.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="samplefmt_8h.html">libavutil/samplefmt.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="timestamp_8h.html" title="timestamp utils, mostly useful for debugging/logging purposes">libavutil/timestamp.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="avformat_8h.html" title="Main libavformat public API header.">libavformat/avformat.h</a>></span>
<span class="keyword">static</span> <a name="_a0"></a><a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *<a name="a1"></a><a class="code" href="demuxing__decoding_8c.html#a409462acdf7aebe5bfbe1bcd811efa5d">fmt_ctx</a> = NULL;
<span class="keyword">static</span> <a name="_a2"></a><a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *<a name="a3"></a><a class="code" href="demuxing__decoding_8c.html#a2e10f5f24d4c8dd311b8eedc376accf5">video_dec_ctx</a> = NULL, *<a name="a4"></a><a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>;
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a5"></a><a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <a name="a6"></a><a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>;
<span class="keyword">static</span> <span class="keyword">enum</span> <a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5" title="Pixel format.">AVPixelFormat</a> <a name="a7"></a><a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>;
<span class="keyword">static</span> <a name="_a8"></a><a class="code" href="structAVStream.html" title="Stream structure.">AVStream</a> *<a name="a9"></a><a class="code" href="demuxing__decoding_8c.html#a9decdb9a2529baa6bd2a855a12f1f0fc">video_stream</a> = NULL, *<a name="a10"></a><a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a> = NULL;
<span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a name="a11"></a><a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a> = NULL;
<span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a name="a12"></a><a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a> = NULL;
<span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a name="a13"></a><a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a> = NULL;
<span class="keyword">static</span> FILE *<a name="a14"></a><a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a> = NULL;
<span class="keyword">static</span> FILE *<a name="a15"></a><a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a> = NULL;
<span class="keyword">static</span> uint8_t *<a name="a16"></a><a class="code" href="demuxing__decoding_8c.html#a0b39e94f88658412ecadb5e2116baccc">video_dst_data</a>[4] = {NULL};
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a17"></a><a class="code" href="demuxing__decoding_8c.html#a8dbcd4dc85b9345bde18395026d0ffe4">video_dst_linesize</a>[4];
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a18"></a><a class="code" href="demuxing__decoding_8c.html#a849609cb135f2d344ff03e99610b43df">video_dst_bufsize</a>;
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a19"></a><a class="code" href="demuxing__decoding_8c.html#af35f69f407b98b5244f837e25813bc11">video_stream_idx</a> = -1, <a name="a20"></a><a class="code" href="demuxing__decoding_8c.html#a16cd83c72f483b9fa2844c5516452798">audio_stream_idx</a> = -1;
<span class="keyword">static</span> <a name="_a21"></a><a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a name="a22"></a><a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a> = NULL;
<span class="keyword">static</span> <a name="_a23"></a><a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a name="a24"></a><a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>;
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a25"></a><a class="code" href="demuxing__decoding_8c.html#ac9f496a4d3575928c5b94568bbedd753">video_frame_count</a> = 0;
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a26"></a><a class="code" href="demuxing__decoding_8c.html#a48eccd48ea840bc45254f44995320463">audio_frame_count</a> = 0;
<span class="comment">/* The different ways of decoding and managing data memory. You are not</span>
<span class="comment"> * supposed to support all the modes in your application but pick the one most</span>
<span class="comment"> * appropriate to your needs. Look for the use of api_mode in this example to</span>
<span class="comment"> * see what are the differences of API usage between them */</span>
<span class="keyword">enum</span> {
<a name="a27"></a><a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2dab844a3f0eaa875df057068f0367489b1">API_MODE_OLD</a> = 0, <span class="comment">/* old method, deprecated */</span>
<a name="a28"></a><a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da8a168414de8770c24226d90ba3d1f7cc">API_MODE_NEW_API_REF_COUNT</a> = 1, <span class="comment">/* new method, using the frame reference counting */</span>
<a name="a29"></a><a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da328cfb892cfa821bdad4cad6ab6d9ea1">API_MODE_NEW_API_NO_REF_COUNT</a> = 2, <span class="comment">/* new method, without reference counting */</span>
};
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a30"></a><a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> = <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2dab844a3f0eaa875df057068f0367489b1">API_MODE_OLD</a>;
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a31"></a><a class="code" href="demuxing__decoding_8c.html#af8c0388188ed14c6585580b04d960a2c">decode_packet</a>(<span class="keywordtype">int</span> *got_frame, <span class="keywordtype">int</span> cached)
{
<span class="keywordtype">int</span> ret = 0;
<span class="keywordtype">int</span> decoded = pkt.<a name="a32"></a><a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>;
*got_frame = 0;
<span class="keywordflow">if</span> (pkt.<a name="a33"></a><a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a> == <a class="code" href="demuxing__decoding_8c.html#af35f69f407b98b5244f837e25813bc11">video_stream_idx</a>) {
<span class="comment">/* decode video frame */</span>
ret = <a name="a34"></a><a class="code" href="group__lavc__decoding.html#ga99ee61b6dcffb7817a275d39da58cc74" title="Decode the video frame of size avpkt->size from avpkt->data into picture.">avcodec_decode_video2</a>(video_dec_ctx, frame, got_frame, &pkt);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error decoding video frame (%s)\n"</span>, <a name="a35"></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> ret;
}
<span class="keywordflow">if</span> (*got_frame) {
<span class="keywordflow">if</span> (frame-><a name="a36"></a><a class="code" href="structAVFrame.html#a1e71ce60cedd5f3b6811714a9f7f9e0a" title="width and height of the video frame">width</a> != <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a> || frame-><a name="a37"></a><a class="code" href="structAVFrame.html#a3f89733f429c98ba5bc64373fb0a3f13">height</a> != <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a> ||
frame-><a name="a38"></a><a class="code" href="structAVFrame.html#aed14fa772ce46881020fd1545c86432c" title="format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...">format</a> != <a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>) {
<span class="comment">/* To handle this change, one could call av_image_alloc again and</span>
<span class="comment"> * decode the following frames into another rawvideo file. */</span>
fprintf(stderr, <span class="stringliteral">"Error: Width, height and pixel format have to be "</span>
<span class="stringliteral">"constant in a rawvideo file, but the width, height or "</span>
<span class="stringliteral">"pixel format of the input video changed:\n"</span>
<span class="stringliteral">"old: width = %d, height = %d, format = %s\n"</span>
<span class="stringliteral">"new: width = %d, height = %d, format = %s\n"</span>,
<a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>, <a name="a39"></a><a class="code" href="pixdesc_8h.html#ab92e2a8a9b58c982560c49df9f01e47e" title="Return the short name for a pixel format, NULL in case pix_fmt is unknown.">av_get_pix_fmt_name</a>(<a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>),
frame-><a class="code" href="structAVFrame.html#a1e71ce60cedd5f3b6811714a9f7f9e0a" title="width and height of the video frame">width</a>, frame-><a class="code" href="structAVFrame.html#a3f89733f429c98ba5bc64373fb0a3f13">height</a>,
<a class="code" href="pixdesc_8h.html#ab92e2a8a9b58c982560c49df9f01e47e" title="Return the short name for a pixel format, NULL in case pix_fmt is unknown.">av_get_pix_fmt_name</a>(frame-><a class="code" href="structAVFrame.html#aed14fa772ce46881020fd1545c86432c" title="format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...">format</a>));
<span class="keywordflow">return</span> -1;
}
printf(<span class="stringliteral">"video_frame%s n:%d coded_n:%d pts:%s\n"</span>,
cached ? <span class="stringliteral">"(cached)"</span> : <span class="stringliteral">""</span>,
<a class="code" href="demuxing__decoding_8c.html#ac9f496a4d3575928c5b94568bbedd753">video_frame_count</a>++, frame-><a name="a40"></a><a class="code" href="structAVFrame.html#a02ca9bb7f500d3d850f8736f3cb29c6c" title="picture number in bitstream order">coded_picture_number</a>,
<a name="a41"></a><a class="code" href="timestamp_8h.html#ad344b91ede6b86fc0a530611293f42da" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_ts2timestr</a>(frame-><a name="a42"></a><a class="code" href="structAVFrame.html#a0452833e3ab6ddd7acbf82817a7818a4" title="Presentation timestamp in time_base units (time when frame should be shown to user).">pts</a>, &video_dec_ctx-><a name="a43"></a><a class="code" href="structAVCodecContext.html#ab7bfeb9fa5840aac090e2b0bd0ef7589" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>));
<span class="comment">/* copy decoded frame to destination buffer:</span>
<span class="comment"> * this is required since rawvideo expects non aligned data */</span>
<a name="a44"></a><a class="code" href="group__lavu__picture.html#ga6b5a267e202daadf81cbde6584055d1e" title="Copy image in src_data to dst_data.">av_image_copy</a>(<a class="code" href="demuxing__decoding_8c.html#a0b39e94f88658412ecadb5e2116baccc">video_dst_data</a>, <a class="code" href="demuxing__decoding_8c.html#a8dbcd4dc85b9345bde18395026d0ffe4">video_dst_linesize</a>,
(<span class="keyword">const</span> uint8_t **)(frame-><a name="a45"></a><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>), frame-><a name="a46"></a><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</a>,
<a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>, <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>);
<span class="comment">/* write to rawvideo file */</span>
fwrite(<a class="code" href="demuxing__decoding_8c.html#a0b39e94f88658412ecadb5e2116baccc">video_dst_data</a>[0], 1, <a class="code" href="demuxing__decoding_8c.html#a849609cb135f2d344ff03e99610b43df">video_dst_bufsize</a>, <a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a>);
}
} <span class="keywordflow">else</span> <span class="keywordflow">if</span> (pkt.<a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a> == <a class="code" href="demuxing__decoding_8c.html#a16cd83c72f483b9fa2844c5516452798">audio_stream_idx</a>) {
<span class="comment">/* decode audio frame */</span>
ret = <a name="a47"></a><a class="code" href="group__lavc__decoding.html#ga834bb1b062fbcc2de4cf7fb93f154a3e" title="Decode the audio frame of size avpkt->size from avpkt->data into frame.">avcodec_decode_audio4</a>(<a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>, frame, got_frame, &pkt);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error decoding audio frame (%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));
<span class="keywordflow">return</span> ret;
}
<span class="comment">/* Some audio decoders decode only part of the packet, and have to be</span>
<span class="comment"> * called again with the remainder of the packet data.</span>
<span class="comment"> * Sample: fate-suite/lossless-audio/luckynight-partial.shn</span>
<span class="comment"> * Also, some decoders might over-read the packet. */</span>
decoded = <a name="a48"></a><a class="code" href="common_8h.html#a0f84cbd7fd810cd678157c98cbfad4b0">FFMIN</a>(ret, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
<span class="keywordflow">if</span> (*got_frame) {
<span class="keywordtype">size_t</span> unpadded_linesize = frame-><a name="a49"></a><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a> * <a name="a50"></a><a class="code" href="group__lavu__sampfmts.html#ga0c3c218e1dd570ad4917c69a35a6c77d" title="Return number of bytes per sample.">av_get_bytes_per_sample</a>(frame-><a class="code" href="structAVFrame.html#aed14fa772ce46881020fd1545c86432c" title="format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...">format</a>);
printf(<span class="stringliteral">"audio_frame%s n:%d nb_samples:%d pts:%s\n"</span>,
cached ? <span class="stringliteral">"(cached)"</span> : <span class="stringliteral">""</span>,
<a class="code" href="demuxing__decoding_8c.html#a48eccd48ea840bc45254f44995320463">audio_frame_count</a>++, frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>,
<a class="code" href="timestamp_8h.html#ad344b91ede6b86fc0a530611293f42da" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_ts2timestr</a>(frame-><a class="code" href="structAVFrame.html#a0452833e3ab6ddd7acbf82817a7818a4" title="Presentation timestamp in time_base units (time when frame should be shown to user).">pts</a>, &<a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>-><a class="code" href="structAVCodecContext.html#ab7bfeb9fa5840aac090e2b0bd0ef7589" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>));
<span class="comment">/* Write the raw audio data samples of the first plane. This works</span>
<span class="comment"> * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,</span>
<span class="comment"> * most audio decoders output planar audio, which uses a separate</span>
<span class="comment"> * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).</span>
<span class="comment"> * In other words, this code will write only the first audio channel</span>
<span class="comment"> * in these cases.</span>
<span class="comment"> * You should use libswresample or libavfilter to convert the frame</span>
<span class="comment"> * to packed data. */</span>
fwrite(frame-><a name="a51"></a><a class="code" href="structAVFrame.html#afca04d808393822625e09b5ba91c6756" title="pointers to the data planes/channels.">extended_data</a>[0], 1, unpadded_linesize, <a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a>);
}
}
<span class="comment">/* If we use the new API with reference counting, we own the data and need</span>
<span class="comment"> * to de-reference it when we don't use it anymore */</span>
<span class="keywordflow">if</span> (*got_frame && <a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> == <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da8a168414de8770c24226d90ba3d1f7cc">API_MODE_NEW_API_REF_COUNT</a>)
<a name="a52"></a><a class="code" href="group__lavu__frame.html#ga0a2b687f9c1c5ed0089b01fd61227108" title="Unreference all the buffers referenced by frame and reset the frame fields.">av_frame_unref</a>(frame);
<span class="keywordflow">return</span> decoded;
}
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a53"></a><a class="code" href="demuxing__decoding_8c.html#a85e44b1a7e74c90b84b2298caefb5f73">open_codec_context</a>(<span class="keywordtype">int</span> *stream_idx,
<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *fmt_ctx, <span class="keyword">enum</span> <a class="code" href="group__lavu__misc.html#ga9a84bba4713dfced21a1a56163be1f48">AVMediaType</a> type)
{
<span class="keywordtype">int</span> ret, stream_index;
<a class="code" href="structAVStream.html" title="Stream structure.">AVStream</a> *st;
<a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *<a name="a54"></a><a class="code" href="filtering__audio_8c.html#ac2bf6798f37e95d3d2ff2d44e4ab3c12">dec_ctx</a> = NULL;
<a name="_a55"></a><a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *dec = NULL;
<a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opts = NULL;
ret = <a name="a56"></a><a class="code" href="group__lavf__decoding.html#gaa6fa468c922ff5c60a6021dcac09aff9" title="Find the "best" stream in the file.">av_find_best_stream</a>(fmt_ctx, type, -1, -1, NULL, 0);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Could not find %s stream in input file '%s'\n"</span>,
<a name="a57"></a><a class="code" href="group__lavu__misc.html#gaf21645cfa855b2caf9699d7dc7b2d08e" title="Return a string describing the media_type enum, NULL if media_type is unknown.">av_get_media_type_string</a>(type), <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>);
<span class="keywordflow">return</span> ret;
} <span class="keywordflow">else</span> {
stream_index = ret;
st = fmt_ctx-><a name="a58"></a><a class="code" href="structAVFormatContext.html#acfefb6b6cf21e87a0dcbd1a547ba2348" title="A list of all streams in the file.">streams</a>[stream_index];
<span class="comment">/* find decoder for the stream */</span>
dec_ctx = st-><a name="a59"></a><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
dec = <a name="a60"></a><a class="code" href="group__lavc__decoding.html#ga19a0ca553277f019dd5b0fec6e1f9dca" title="Find a registered decoder with a matching codec ID.">avcodec_find_decoder</a>(dec_ctx-><a name="a61"></a><a class="code" href="structAVCodecContext.html#adc5f65d6099fd8339c1580c091777223">codec_id</a>);
<span class="keywordflow">if</span> (!dec) {
fprintf(stderr, <span class="stringliteral">"Failed to find %s codec\n"</span>,
<a class="code" href="group__lavu__misc.html#gaf21645cfa855b2caf9699d7dc7b2d08e" title="Return a string describing the media_type enum, NULL if media_type is unknown.">av_get_media_type_string</a>(type));
<span class="keywordflow">return</span> <a name="a62"></a><a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(EINVAL);
}
<span class="comment">/* Init the decoders, with or without reference counting */</span>
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> == <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da8a168414de8770c24226d90ba3d1f7cc">API_MODE_NEW_API_REF_COUNT</a>)
<a name="a63"></a><a class="code" href="group__lavu__dict.html#ga8d9c2de72b310cef8e6a28c9cd3acbbe" title="Set the given entry in *pm, overwriting an existing entry.">av_dict_set</a>(&opts, <span class="stringliteral">"refcounted_frames"</span>, <span class="stringliteral">"1"</span>, 0);
<span class="keywordflow">if</span> ((ret = <a name="a64"></a><a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(dec_ctx, dec, &opts)) < 0) {
fprintf(stderr, <span class="stringliteral">"Failed to open %s codec\n"</span>,
<a class="code" href="group__lavu__misc.html#gaf21645cfa855b2caf9699d7dc7b2d08e" title="Return a string describing the media_type enum, NULL if media_type is unknown.">av_get_media_type_string</a>(type));
<span class="keywordflow">return</span> ret;
}
*stream_idx = stream_index;
}
<span class="keywordflow">return</span> 0;
}
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a65"></a><a class="code" href="demuxing__decoding_8c.html#a5e068fc5e6dc5c59638ec235f7f6db89">get_format_from_sample_fmt</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> **fmt,
<span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> sample_fmt)
{
<span class="keywordtype">int</span> i;
<span class="keyword">struct </span>sample_fmt_entry {
<span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> sample_fmt; <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt_be, *fmt_le;
} sample_fmt_entries[] = {
{ <a name="a66"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5adf0f66f21492f8fd5db6debb8b3f932a" title="unsigned 8 bits">AV_SAMPLE_FMT_U8</a>, <span class="stringliteral">"u8"</span>, <span class="stringliteral">"u8"</span> },
{ <a name="a67"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5aea6132df57aebc3f76e10665395c46af" title="signed 16 bits">AV_SAMPLE_FMT_S16</a>, <span class="stringliteral">"s16be"</span>, <span class="stringliteral">"s16le"</span> },
{ <a name="a68"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5ab7ae6bb6718c07cd316778783a57a63b" title="signed 32 bits">AV_SAMPLE_FMT_S32</a>, <span class="stringliteral">"s32be"</span>, <span class="stringliteral">"s32le"</span> },
{ <a name="a69"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5a5251d62cc3c7f18e56bc8e0c48a27903" title="float">AV_SAMPLE_FMT_FLT</a>, <span class="stringliteral">"f32be"</span>, <span class="stringliteral">"f32le"</span> },
{ <a name="a70"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5ae35a2a6a7593723d9fe4bcefb2876606" title="double">AV_SAMPLE_FMT_DBL</a>, <span class="stringliteral">"f64be"</span>, <span class="stringliteral">"f64le"</span> },
};
*fmt = NULL;
<span class="keywordflow">for</span> (i = 0; i < <a name="a71"></a><a class="code" href="common_8h.html#a3508a98d3ee7e2c9a4841fe264cf70f7">FF_ARRAY_ELEMS</a>(sample_fmt_entries); i++) {
<span class="keyword">struct </span>sample_fmt_entry *entry = &sample_fmt_entries[i];
<span class="keywordflow">if</span> (sample_fmt == entry->sample_fmt) {
*fmt = <a name="a72"></a><a class="code" href="common_8h.html#a309bdca20f6a78431379fc69442d98ea">AV_NE</a>(entry->fmt_be, entry->fmt_le);
<span class="keywordflow">return</span> 0;
}
}
fprintf(stderr,
<span class="stringliteral">"sample format %s is not supported as output format\n"</span>,
<a name="a73"></a><a class="code" href="group__lavu__sampfmts.html#ga31b9d149b2de9821a65f4f5612970838" title="Return the name of sample_fmt, or NULL if sample_fmt is not recognized.">av_get_sample_fmt_name</a>(sample_fmt));
<span class="keywordflow">return</span> -1;
}
<span class="keywordtype">int</span> <a name="a74"></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)
{
<span class="keywordtype">int</span> ret = 0, got_frame;
<span class="keywordflow">if</span> (argc != 4 && argc != 5) {
fprintf(stderr, <span class="stringliteral">"usage: %s [-refcount=<old|new_norefcount|new_refcount>] "</span>
<span class="stringliteral">"input_file video_output_file audio_output_file\n"</span>
<span class="stringliteral">"API example program to show how to read frames from an input file.\n"</span>
<span class="stringliteral">"This program reads frames from a file, decodes them, and writes decoded\n"</span>
<span class="stringliteral">"video frames to a rawvideo file named video_output_file, and decoded\n"</span>
<span class="stringliteral">"audio frames to a rawaudio file named audio_output_file.\n\n"</span>
<span class="stringliteral">"If the -refcount option is specified, the program use the\n"</span>
<span class="stringliteral">"reference counting frame system which allows keeping a copy of\n"</span>
<span class="stringliteral">"the data for longer than one decode call. If unset, it's using\n"</span>
<span class="stringliteral">"the classic old method.\n"</span>
<span class="stringliteral">"\n"</span>, argv[0]);
exit(1);
}
<span class="keywordflow">if</span> (argc == 5) {
<span class="keyword">const</span> <span class="keywordtype">char</span> *mode = argv[1] + strlen(<span class="stringliteral">"-refcount="</span>);
<span class="keywordflow">if</span> (!strcmp(mode, <span class="stringliteral">"old"</span>)) <a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> = <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2dab844a3f0eaa875df057068f0367489b1">API_MODE_OLD</a>;
<span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(mode, <span class="stringliteral">"new_norefcount"</span>)) <a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> = <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da328cfb892cfa821bdad4cad6ab6d9ea1">API_MODE_NEW_API_NO_REF_COUNT</a>;
<span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(mode, <span class="stringliteral">"new_refcount"</span>)) <a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> = <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2da8a168414de8770c24226d90ba3d1f7cc">API_MODE_NEW_API_REF_COUNT</a>;
<span class="keywordflow">else</span> {
fprintf(stderr, <span class="stringliteral">"unknow mode '%s'\n"</span>, mode);
exit(1);
}
argv++;
}
<a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a> = argv[1];
<a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a> = argv[2];
<a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a> = argv[3];
<span class="comment">/* register all formats and codecs */</span>
<a name="a75"></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">/* open input file, and allocate format context */</span>
<span class="keywordflow">if</span> (<a name="a76"></a><a class="code" href="group__lavf__decoding.html#ga10a404346c646e4ab58f4ed798baca32" title="Open an input stream and read the header.">avformat_open_input</a>(&fmt_ctx, <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>, NULL, NULL) < 0) {
fprintf(stderr, <span class="stringliteral">"Could not open source file %s\n"</span>, <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>);
exit(1);
}
<span class="comment">/* retrieve stream information */</span>
<span class="keywordflow">if</span> (<a name="a77"></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) < 0) {
fprintf(stderr, <span class="stringliteral">"Could not find stream information\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#a85e44b1a7e74c90b84b2298caefb5f73">open_codec_context</a>(&<a class="code" href="demuxing__decoding_8c.html#af35f69f407b98b5244f837e25813bc11">video_stream_idx</a>, fmt_ctx, <a name="a78"></a><a class="code" href="group__lavu__misc.html#gga9a84bba4713dfced21a1a56163be1f48ac1a46f59be5c6c2d3412ab172d6b8cf5">AVMEDIA_TYPE_VIDEO</a>) >= 0) {
video_stream = fmt_ctx-><a class="code" href="structAVFormatContext.html#acfefb6b6cf21e87a0dcbd1a547ba2348" title="A list of all streams in the file.">streams</a>[<a class="code" href="demuxing__decoding_8c.html#af35f69f407b98b5244f837e25813bc11">video_stream_idx</a>];
video_dec_ctx = video_stream-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a> = fopen(<a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a>, <span class="stringliteral">"wb"</span>);
<span class="keywordflow">if</span> (!<a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a>) {
fprintf(stderr, <span class="stringliteral">"Could not open destination file %s\n"</span>, <a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a>);
ret = 1;
<span class="keywordflow">goto</span> end;
}
<span class="comment">/* allocate image where the decoded image will be put */</span>
<a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a> = video_dec_ctx-><a name="a79"></a><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>;
<a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a> = video_dec_ctx-><a name="a80"></a><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>;
<a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a> = video_dec_ctx-><a name="a81"></a><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a>;
ret = <a name="a82"></a><a class="code" href="group__lavu__picture.html#ga841e0a89a642e24141af1918a2c10448" title="Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...">av_image_alloc</a>(<a class="code" href="demuxing__decoding_8c.html#a0b39e94f88658412ecadb5e2116baccc">video_dst_data</a>, <a class="code" href="demuxing__decoding_8c.html#a8dbcd4dc85b9345bde18395026d0ffe4">video_dst_linesize</a>,
<a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>, <a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>, 1);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Could not allocate raw video buffer\n"</span>);
<span class="keywordflow">goto</span> end;
}
<a class="code" href="demuxing__decoding_8c.html#a849609cb135f2d344ff03e99610b43df">video_dst_bufsize</a> = ret;
}
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#a85e44b1a7e74c90b84b2298caefb5f73">open_codec_context</a>(&<a class="code" href="demuxing__decoding_8c.html#a16cd83c72f483b9fa2844c5516452798">audio_stream_idx</a>, fmt_ctx, <a name="a83"></a><a class="code" href="group__lavu__misc.html#gga9a84bba4713dfced21a1a56163be1f48a7b4e33b8f30ac94d34ce8d9f7c33927a">AVMEDIA_TYPE_AUDIO</a>) >= 0) {
<a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a> = fmt_ctx-><a class="code" href="structAVFormatContext.html#acfefb6b6cf21e87a0dcbd1a547ba2348" title="A list of all streams in the file.">streams</a>[<a class="code" href="demuxing__decoding_8c.html#a16cd83c72f483b9fa2844c5516452798">audio_stream_idx</a>];
<a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a> = <a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a> = fopen(<a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a>, <span class="stringliteral">"wb"</span>);
<span class="keywordflow">if</span> (!<a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a>) {
fprintf(stderr, <span class="stringliteral">"Could not open destination file %s\n"</span>, <a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a>);
ret = 1;
<span class="keywordflow">goto</span> end;
}
}
<span class="comment">/* dump input information to stderr */</span>
<a name="a84"></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, <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>, 0);
<span class="keywordflow">if</span> (!<a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a> && !video_stream) {
fprintf(stderr, <span class="stringliteral">"Could not find audio or video stream in the input, aborting\n"</span>);
ret = 1;
<span class="keywordflow">goto</span> end;
}
<span class="comment">/* When using the new API, you need to use the libavutil/frame.h API, while</span>
<span class="comment"> * the classic frame management is available in libavcodec */</span>
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> == <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2dab844a3f0eaa875df057068f0367489b1">API_MODE_OLD</a>)
frame = avcodec_alloc_frame();
<span class="keywordflow">else</span>
frame = <a name="a85"></a><a class="code" href="group__lavu__frame.html#gac700017c5270c79c1e1befdeeb008b2f" title="Allocate an AVFrame and set its fields to default values.">av_frame_alloc</a>();
<span class="keywordflow">if</span> (!frame) {
fprintf(stderr, <span class="stringliteral">"Could not allocate frame\n"</span>);
ret = <a class="code" href="group__lavu__error.html#gae4bb6f165973d09584e0ec0f335f69ca">AVERROR</a>(ENOMEM);
<span class="keywordflow">goto</span> end;
}
<span class="comment">/* initialize packet, set data to NULL, let the demuxer fill it */</span>
<a name="a86"></a><a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
pkt.<a name="a87"></a><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL;
pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<span class="keywordflow">if</span> (video_stream)
printf(<span class="stringliteral">"Demuxing video from file '%s' into '%s'\n"</span>, <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>, <a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a>);
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a>)
printf(<span class="stringliteral">"Demuxing audio from file '%s' into '%s'\n"</span>, <a class="code" href="demuxing__decoding_8c.html#ad3e56268ab4ce7a25e7a17c2e088a3c1">src_filename</a>, <a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a>);
<span class="comment">/* read frames from the file */</span>
<span class="keywordflow">while</span> (<a name="a88"></a><a class="code" href="group__lavf__decoding.html#ga4fdb3084415a82e3810de6ee60e46a61" title="Return the next frame of a stream.">av_read_frame</a>(fmt_ctx, &pkt) >= 0) {
<a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> orig_pkt = <a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>;
<span class="keywordflow">do</span> {
ret = <a class="code" href="demuxing__decoding_8c.html#af8c0388188ed14c6585580b04d960a2c">decode_packet</a>(&got_frame, 0);
<span class="keywordflow">if</span> (ret < 0)
<span class="keywordflow">break</span>;
pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> += ret;
pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> -= ret;
} <span class="keywordflow">while</span> (pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> > 0);
<a name="a89"></a><a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&orig_pkt);
}
<span class="comment">/* flush cached frames */</span>
pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL;
pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<span class="keywordflow">do</span> {
<a class="code" href="demuxing__decoding_8c.html#af8c0388188ed14c6585580b04d960a2c">decode_packet</a>(&got_frame, 1);
} <span class="keywordflow">while</span> (got_frame);
printf(<span class="stringliteral">"Demuxing succeeded.\n"</span>);
<span class="keywordflow">if</span> (video_stream) {
printf(<span class="stringliteral">"Play the output video file with the command:\n"</span>
<span class="stringliteral">"ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n"</span>,
<a class="code" href="pixdesc_8h.html#ab92e2a8a9b58c982560c49df9f01e47e" title="Return the short name for a pixel format, NULL in case pix_fmt is unknown.">av_get_pix_fmt_name</a>(<a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>), <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>,
<a class="code" href="demuxing__decoding_8c.html#a780830fc3afc836feb64d80eef4185b5">video_dst_filename</a>);
}
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#ab269038fe8cd6f8fa4ccec9eaf939345">audio_stream</a>) {
<span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> sfmt = <a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>-><a name="a90"></a><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>;
<span class="keywordtype">int</span> n_channels = <a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>-><a name="a91"></a><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>;
<span class="keyword">const</span> <span class="keywordtype">char</span> *fmt;
<span class="keywordflow">if</span> (<a name="a92"></a><a class="code" href="group__lavu__sampfmts.html#ga06ba8a64dc4382c422789a5d0b6bf592" title="Check if the sample format is planar.">av_sample_fmt_is_planar</a>(sfmt)) {
<span class="keyword">const</span> <span class="keywordtype">char</span> *packed = <a class="code" href="group__lavu__sampfmts.html#ga31b9d149b2de9821a65f4f5612970838" title="Return the name of sample_fmt, or NULL if sample_fmt is not recognized.">av_get_sample_fmt_name</a>(sfmt);
printf(<span class="stringliteral">"Warning: the sample format the decoder produced is planar "</span>
<span class="stringliteral">"(%s). This example will output the first channel only.\n"</span>,
packed ? packed : <span class="stringliteral">"?"</span>);
sfmt = <a name="a93"></a><a class="code" href="group__lavu__sampfmts.html#ga7817ec0eff4dc6fc0962f31e6d138bca" title="Get the packed alternative form of the given sample format.">av_get_packed_sample_fmt</a>(sfmt);
n_channels = 1;
}
<span class="keywordflow">if</span> ((ret = <a class="code" href="demuxing__decoding_8c.html#a5e068fc5e6dc5c59638ec235f7f6db89">get_format_from_sample_fmt</a>(&fmt, sfmt)) < 0)
<span class="keywordflow">goto</span> end;
printf(<span class="stringliteral">"Play the output audio file with the command:\n"</span>
<span class="stringliteral">"ffplay -f %s -ac %d -ar %d %s\n"</span>,
fmt, n_channels, <a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>-><a name="a94"></a><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>,
<a class="code" href="demuxing__decoding_8c.html#a9f79f9440c018d4eb8448d849f35f467">audio_dst_filename</a>);
}
end:
<a name="a95"></a><a class="code" href="group__lavc__core.html#gaf4daa92361efb3523ef5afeb0b54077f" title="Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...">avcodec_close</a>(video_dec_ctx);
<a class="code" href="group__lavc__core.html#gaf4daa92361efb3523ef5afeb0b54077f" title="Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...">avcodec_close</a>(<a class="code" href="demuxing__decoding_8c.html#adecce4fda9b9d4fc0d6713d461755cfd">audio_dec_ctx</a>);
<a name="a96"></a><a class="code" href="group__lavf__decoding.html#gae804b99aec044690162b8b9b110236a4" title="Close an opened input AVFormatContext.">avformat_close_input</a>(&fmt_ctx);
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a>)
fclose(<a class="code" href="demuxing__decoding_8c.html#ac3ad9da7180c9a9baa85e968eb6937e3">video_dst_file</a>);
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a>)
fclose(<a class="code" href="demuxing__decoding_8c.html#af0d7c67df6575ed6b3b7de4d843ed7a6">audio_dst_file</a>);
<span class="keywordflow">if</span> (<a class="code" href="demuxing__decoding_8c.html#a4bb7a82d583b93cb9e28b93dda64ff14">api_mode</a> == <a class="code" href="demuxing__decoding_8c.html#a0411cd49bb5b71852cecd93bcbf0ca2dab844a3f0eaa875df057068f0367489b1">API_MODE_OLD</a>)
avcodec_free_frame(&frame);
<span class="keywordflow">else</span>
<a name="a97"></a><a class="code" href="group__lavu__frame.html#ga979d73f3228814aee56aeca0636e37cc" title="Free the frame and any dynamically allocated objects in it, e.g.">av_frame_free</a>(&frame);
<a name="a98"></a><a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(<a class="code" href="demuxing__decoding_8c.html#a0b39e94f88658412ecadb5e2116baccc">video_dst_data</a>[0]);
<span class="keywordflow">return</span> ret < 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"> </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>