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/muxing.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/muxing.c</div> </div>
</div><!--header-->
<div class="contents">
<a href="muxing_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) 2003 Fabrice Bellard</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 API example.</span>
<a name="l00026"></a>00026 <span class="comment"> *</span>
<a name="l00027"></a>00027 <span class="comment"> * Output a media file in any supported libavformat format. The default</span>
<a name="l00028"></a>00028 <span class="comment"> * codecs are used.</span>
<a name="l00029"></a>00029 <span class="comment"> * @example muxing.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 <stdlib.h></span>
<a name="l00033"></a>00033 <span class="preprocessor">#include <stdio.h></span>
<a name="l00034"></a>00034 <span class="preprocessor">#include <string.h></span>
<a name="l00035"></a>00035 <span class="preprocessor">#include <math.h></span>
<a name="l00036"></a>00036
<a name="l00037"></a>00037 <span class="preprocessor">#include <<a class="code" href="avassert_8h.html" title="simple assert() macros that are a bit more flexible than ISO C assert().">libavutil/avassert.h</a>></span>
<a name="l00038"></a>00038 <span class="preprocessor">#include <<a class="code" href="channel__layout_8h.html" title="audio channel layout utility functions">libavutil/channel_layout.h</a>></span>
<a name="l00039"></a>00039 <span class="preprocessor">#include <<a class="code" href="opt_8h.html" title="AVOptions.">libavutil/opt.h</a>></span>
<a name="l00040"></a>00040 <span class="preprocessor">#include <<a class="code" href="mathematics_8h.html">libavutil/mathematics.h</a>></span>
<a name="l00041"></a>00041 <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>
<a name="l00042"></a>00042 <span class="preprocessor">#include <<a class="code" href="avformat_8h.html" title="Main libavformat public API header.">libavformat/avformat.h</a>></span>
<a name="l00043"></a>00043 <span class="preprocessor">#include <<a class="code" href="swscale_8h.html" title="external API header">libswscale/swscale.h</a>></span>
<a name="l00044"></a>00044 <span class="preprocessor">#include <<a class="code" href="swresample_8h.html" title="libswresample public header">libswresample/swresample.h</a>></span>
<a name="l00045"></a>00045
<a name="l00046"></a><a class="code" href="muxing_8c.html#ace0cad5fe744009aeaa62eb1fbb31831">00046</a> <span class="preprocessor">#define STREAM_DURATION 10.0</span>
<a name="l00047"></a><a class="code" href="muxing_8c.html#ad0fa7a6ac51a4021276c0a04dfe2283b">00047</a> <span class="preprocessor"></span><span class="preprocessor">#define STREAM_FRAME_RATE 25 </span><span class="comment">/* 25 images/s */</span>
<a name="l00048"></a><a class="code" href="muxing_8c.html#ae67b157ab0198fa81bf675b378ff06fd">00048</a> <span class="preprocessor">#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P </span><span class="comment">/* default pix_fmt */</span>
<a name="l00049"></a>00049
<a name="l00050"></a><a class="code" href="muxing_8c.html#ab746bcb79e75e498c4925ccd72a34f63">00050</a> <span class="preprocessor">#define SCALE_FLAGS SWS_BICUBIC</span>
<a name="l00051"></a>00051 <span class="preprocessor"></span>
<a name="l00052"></a>00052 <span class="comment">// a wrapper around a single output AVStream</span>
<a name="l00053"></a><a class="code" href="structOutputStream.html">00053</a> <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="structOutputStream.html">OutputStream</a> {
<a name="l00054"></a><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">00054</a> <a class="code" href="structAVStream.html" title="Stream structure.">AVStream</a> *<a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>;
<a name="l00055"></a>00055
<a name="l00056"></a>00056 <span class="comment">/* pts of the next frame that will be generated */</span>
<a name="l00057"></a><a class="code" href="structOutputStream.html#aaa3b9001e4dac709017237c1c177cd0a">00057</a> int64_t <a class="code" href="structOutputStream.html#aaa3b9001e4dac709017237c1c177cd0a">next_pts</a>;
<a name="l00058"></a><a class="code" href="structOutputStream.html#a0c3b9816e5c50c0ed816343d224121e7">00058</a> <span class="keywordtype">int</span> <a class="code" href="structOutputStream.html#a0c3b9816e5c50c0ed816343d224121e7">samples_count</a>;
<a name="l00059"></a>00059
<a name="l00060"></a><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">00060</a> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>;
<a name="l00061"></a><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">00061</a> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a>;
<a name="l00062"></a>00062
<a name="l00063"></a><a class="code" href="structOutputStream.html#a04fe3fc54e1a382047e501c4ce63497b">00063</a> <span class="keywordtype">float</span> <a class="code" href="structOutputStream.html#ab5bc1d10f171e32c5241ab23271ae765">t</a>, <a class="code" href="structOutputStream.html#afacbd1335db05b68d2ea528c8b5d197b">tincr</a>, <a class="code" href="structOutputStream.html#a04fe3fc54e1a382047e501c4ce63497b">tincr2</a>;
<a name="l00064"></a>00064
<a name="l00065"></a><a class="code" href="structOutputStream.html#a87e6c2172dab3143e4857aaaf6558573">00065</a> <span class="keyword">struct </span>SwsContext *<a class="code" href="structOutputStream.html#a87e6c2172dab3143e4857aaaf6558573">sws_ctx</a>;
<a name="l00066"></a><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">00066</a> <span class="keyword">struct </span><a class="code" href="group__lswr.html#ga4aa775b7fba31d2c8dc14c7b7e282863" title="The libswresample context.">SwrContext</a> *<a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>;
<a name="l00067"></a>00067 } <a class="code" href="structOutputStream.html">OutputStream</a>;
<a name="l00068"></a>00068
<a name="l00069"></a><a class="code" href="muxing_8c.html#ac7a1319852323152629ce60c4ef04ed5">00069</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#ac7a1319852323152629ce60c4ef04ed5">log_packet</a>(<span class="keyword">const</span> <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>, <span class="keyword">const</span> <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> *<a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>)
<a name="l00070"></a>00070 {
<a name="l00071"></a>00071 <a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a> *time_base = &fmt_ctx-><a class="code" href="structAVFormatContext.html#acfefb6b6cf21e87a0dcbd1a547ba2348" title="A list of all streams in the file.">streams</a>[pkt-><a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a>]-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>;
<a name="l00072"></a>00072
<a name="l00073"></a>00073 printf(<span class="stringliteral">"pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n"</span>,
<a name="l00074"></a>00074 <a class="code" href="timestamp_8h.html#a86d797e907fa454ed5fd34bfb0bcd747" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_ts2str</a>(pkt-><a class="code" href="structAVPacket.html#a73bde0a37f3b1efc839f11295bfbf42a" title="Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...">pts</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>(pkt-><a class="code" href="structAVPacket.html#a73bde0a37f3b1efc839f11295bfbf42a" title="Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...">pts</a>, time_base),
<a name="l00075"></a>00075 <a class="code" href="timestamp_8h.html#a86d797e907fa454ed5fd34bfb0bcd747" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_ts2str</a>(pkt-><a class="code" href="structAVPacket.html#a85dbbd306b44b02390cd91c45e6a0f76" title="Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...">dts</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>(pkt-><a class="code" href="structAVPacket.html#a85dbbd306b44b02390cd91c45e6a0f76" title="Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...">dts</a>, time_base),
<a name="l00076"></a>00076 <a class="code" href="timestamp_8h.html#a86d797e907fa454ed5fd34bfb0bcd747" title="Convenience macro, the return value should be used only directly in function arguments but never stan...">av_ts2str</a>(pkt-><a class="code" href="structAVPacket.html#af151ba1967c37038088883cd544eeacd" title="Duration of this packet in AVStream->time_base units, 0 if unknown.">duration</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>(pkt-><a class="code" href="structAVPacket.html#af151ba1967c37038088883cd544eeacd" title="Duration of this packet in AVStream->time_base units, 0 if unknown.">duration</a>, time_base),
<a name="l00077"></a>00077 pkt-><a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a>);
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079
<a name="l00080"></a><a class="code" href="muxing_8c.html#ab85f450fd9ee7aad777fc135b62dd3cd">00080</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="muxing_8c.html#ab85f450fd9ee7aad777fc135b62dd3cd">write_frame</a>(<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>, <span class="keyword">const</span> <a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a> *time_base, <a class="code" href="structAVStream.html" title="Stream structure.">AVStream</a> *st, <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> *<a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>)
<a name="l00081"></a>00081 {
<a name="l00082"></a>00082 <span class="comment">/* rescale output packet timestamp values from codec to stream timebase */</span>
<a name="l00083"></a>00083 <a class="code" href="group__lavc__packet.html#gae5c86e4d93f6e7aa62ef2c60763ea67e" title="Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...">av_packet_rescale_ts</a>(pkt, *time_base, st-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>);
<a name="l00084"></a>00084 pkt-><a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a> = st-><a class="code" href="structAVStream.html#a6ca823054632821e085377f7d371a2d1" title="stream index in AVFormatContext">index</a>;
<a name="l00085"></a>00085
<a name="l00086"></a>00086 <span class="comment">/* Write the compressed frame to the media file. */</span>
<a name="l00087"></a>00087 <a class="code" href="muxing_8c.html#ac7a1319852323152629ce60c4ef04ed5">log_packet</a>(fmt_ctx, pkt);
<a name="l00088"></a>00088 <span class="keywordflow">return</span> <a class="code" href="group__lavf__encoding.html#ga37352ed2c63493c38219d935e71db6c1" title="Write a packet to an output media file ensuring correct interleaving.">av_interleaved_write_frame</a>(fmt_ctx, pkt);
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090
<a name="l00091"></a>00091 <span class="comment">/* Add an output stream. */</span>
<a name="l00092"></a><a class="code" href="muxing_8c.html#aead090f3b6525cfdfa572f18f7575219">00092</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#aead090f3b6525cfdfa572f18f7575219">add_stream</a>(<a class="code" href="structOutputStream.html">OutputStream</a> *ost, <a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc,
<a name="l00093"></a>00093 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> **codec,
<a name="l00094"></a>00094 <span class="keyword">enum</span> <a class="code" href="group__lavc__core.html#gaadca229ad2c20e060a14fec08a5cc7ce" title="Identify the syntax and semantics of the bitstream.">AVCodecID</a> codec_id)
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c;
<a name="l00097"></a>00097 <span class="keywordtype">int</span> i;
<a name="l00098"></a>00098
<a name="l00099"></a>00099 <span class="comment">/* find the encoder */</span>
<a name="l00100"></a>00100 *codec = <a class="code" href="group__lavc__encoding.html#ga9f820c481615c3a02d0407bac0bdbf25" title="Find a registered encoder with a matching codec ID.">avcodec_find_encoder</a>(codec_id);
<a name="l00101"></a>00101 <span class="keywordflow">if</span> (!(*codec)) {
<a name="l00102"></a>00102 fprintf(stderr, <span class="stringliteral">"Could not find encoder for '%s'\n"</span>,
<a name="l00103"></a>00103 <a class="code" href="group__lavc__misc.html#ga2016a52e94f867ebe5113bdf448e182d" title="Get the name of a codec.">avcodec_get_name</a>(codec_id));
<a name="l00104"></a>00104 exit(1);
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106
<a name="l00107"></a>00107 ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a> = <a class="code" href="group__lavf__core.html#gadcb0fd3e507d9b58fe78f61f8ad39827" title="Add a new stream to a media file.">avformat_new_stream</a>(oc, *codec);
<a name="l00108"></a>00108 <span class="keywordflow">if</span> (!ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>) {
<a name="l00109"></a>00109 fprintf(stderr, <span class="stringliteral">"Could not allocate stream\n"</span>);
<a name="l00110"></a>00110 exit(1);
<a name="l00111"></a>00111 }
<a name="l00112"></a>00112 ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a6873ed62f196c24e8bf282609231786f" title="Format-specific stream ID.">id</a> = oc-><a class="code" href="structAVFormatContext.html#a0b748d924898b08b89ff4974afd17285" title="Number of elements in AVFormatContext.streams.">nb_streams</a>-1;
<a name="l00113"></a>00113 c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00114"></a>00114
<a name="l00115"></a>00115 <span class="keywordflow">switch</span> ((*codec)->type) {
<a name="l00116"></a>00116 <span class="keywordflow">case</span> <a class="code" href="group__lavu__misc.html#gga9a84bba4713dfced21a1a56163be1f48a7b4e33b8f30ac94d34ce8d9f7c33927a">AVMEDIA_TYPE_AUDIO</a>:
<a name="l00117"></a>00117 c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a> = (*codec)->sample_fmts ?
<a name="l00118"></a>00118 (*codec)->sample_fmts[0] : <a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5a2d21c520e4ab21100b6440ec2d81ba1b" title="float, planar">AV_SAMPLE_FMT_FLTP</a>;
<a name="l00119"></a>00119 c-><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 64000;
<a name="l00120"></a>00120 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> = 44100;
<a name="l00121"></a>00121 <span class="keywordflow">if</span> ((*codec)->supported_samplerates) {
<a name="l00122"></a>00122 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> = (*codec)->supported_samplerates[0];
<a name="l00123"></a>00123 <span class="keywordflow">for</span> (i = 0; (*codec)->supported_samplerates[i]; i++) {
<a name="l00124"></a>00124 <span class="keywordflow">if</span> ((*codec)->supported_samplerates[i] == 44100)
<a name="l00125"></a>00125 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> = 44100;
<a name="l00126"></a>00126 }
<a name="l00127"></a>00127 }
<a name="l00128"></a>00128 c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a> = <a class="code" href="group__channel__mask__c.html#gac95619abeb32e4a3daa18e3ff3419380" title="Return the number of channels in the channel layout.">av_get_channel_layout_nb_channels</a>(c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a>);
<a name="l00129"></a>00129 c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a> = <a class="code" href="group__channel__mask__c.html#gabc6d6651bba254cd0fa2c42a57228e65">AV_CH_LAYOUT_STEREO</a>;
<a name="l00130"></a>00130 <span class="keywordflow">if</span> ((*codec)->channel_layouts) {
<a name="l00131"></a>00131 c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a> = (*codec)->channel_layouts[0];
<a name="l00132"></a>00132 <span class="keywordflow">for</span> (i = 0; (*codec)->channel_layouts[i]; i++) {
<a name="l00133"></a>00133 <span class="keywordflow">if</span> ((*codec)->channel_layouts[i] == <a class="code" href="group__channel__mask__c.html#gabc6d6651bba254cd0fa2c42a57228e65">AV_CH_LAYOUT_STEREO</a>)
<a name="l00134"></a>00134 c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a> = <a class="code" href="group__channel__mask__c.html#gabc6d6651bba254cd0fa2c42a57228e65">AV_CH_LAYOUT_STEREO</a>;
<a name="l00135"></a>00135 }
<a name="l00136"></a>00136 }
<a name="l00137"></a>00137 c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a> = <a class="code" href="group__channel__mask__c.html#gac95619abeb32e4a3daa18e3ff3419380" title="Return the number of channels in the channel layout.">av_get_channel_layout_nb_channels</a>(c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a>);
<a name="l00138"></a>00138 ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a> = (<a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){ 1, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> };
<a name="l00139"></a>00139 <span class="keywordflow">break</span>;
<a name="l00140"></a>00140
<a name="l00141"></a>00141 <span class="keywordflow">case</span> <a class="code" href="group__lavu__misc.html#gga9a84bba4713dfced21a1a56163be1f48ac1a46f59be5c6c2d3412ab172d6b8cf5">AVMEDIA_TYPE_VIDEO</a>:
<a name="l00142"></a>00142 c-><a class="code" href="structAVCodecContext.html#adc5f65d6099fd8339c1580c091777223">codec_id</a> = codec_id;
<a name="l00143"></a>00143
<a name="l00144"></a>00144 c-><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 400000;
<a name="l00145"></a>00145 <span class="comment">/* Resolution must be a multiple of two. */</span>
<a name="l00146"></a>00146 c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a> = 352;
<a name="l00147"></a>00147 c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a> = 288;
<a name="l00148"></a>00148 <span class="comment">/* timebase: This is the fundamental unit of time (in seconds) in terms</span>
<a name="l00149"></a>00149 <span class="comment"> * of which frame timestamps are represented. For fixed-fps content,</span>
<a name="l00150"></a>00150 <span class="comment"> * timebase should be 1/framerate and timestamp increments should be</span>
<a name="l00151"></a>00151 <span class="comment"> * identical to 1. */</span>
<a name="l00152"></a>00152 ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a> = (<a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){ 1, <a class="code" href="muxing_8c.html#ad0fa7a6ac51a4021276c0a04dfe2283b">STREAM_FRAME_RATE</a> };
<a name="l00153"></a>00153 c-><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> = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>;
<a name="l00154"></a>00154
<a name="l00155"></a>00155 c-><a class="code" href="structAVCodecContext.html#a9b6b3f1fcbdcc2ad9f4dbb4370496e38" title="the number of pictures in a group of pictures, or 0 for intra_only">gop_size</a> = 12; <span class="comment">/* emit one intra frame every twelve frames at most */</span>
<a name="l00156"></a>00156 c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a> = <a class="code" href="muxing_8c.html#ae67b157ab0198fa81bf675b378ff06fd">STREAM_PIX_FMT</a>;
<a name="l00157"></a>00157 <span class="keywordflow">if</span> (c-><a class="code" href="structAVCodecContext.html#adc5f65d6099fd8339c1580c091777223">codec_id</a> == <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7ceaf614abb36f7c5d0c42ed522c1d6c99f0" title="preferred ID for MPEG-1/2 video decoding">AV_CODEC_ID_MPEG2VIDEO</a>) {
<a name="l00158"></a>00158 <span class="comment">/* just for testing, we also add B frames */</span>
<a name="l00159"></a>00159 c-><a class="code" href="structAVCodecContext.html#a3e5334a611a3e2a6a653805bb9e2d4d4" title="maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...">max_b_frames</a> = 2;
<a name="l00160"></a>00160 }
<a name="l00161"></a>00161 <span class="keywordflow">if</span> (c-><a class="code" href="structAVCodecContext.html#adc5f65d6099fd8339c1580c091777223">codec_id</a> == <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7ceaf019b13f4891b36ae579cd7bc96d1f78">AV_CODEC_ID_MPEG1VIDEO</a>) {
<a name="l00162"></a>00162 <span class="comment">/* Needed to avoid using macroblocks in which some coeffs overflow.</span>
<a name="l00163"></a>00163 <span class="comment"> * This does not happen with normal video, it just happens here as</span>
<a name="l00164"></a>00164 <span class="comment"> * the motion of the chroma plane does not match the luma plane. */</span>
<a name="l00165"></a>00165 c-><a class="code" href="structAVCodecContext.html#a66af0e26734255f1eacabd7d67558482" title="macroblock decision mode">mb_decision</a> = 2;
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167 <span class="keywordflow">break</span>;
<a name="l00168"></a>00168
<a name="l00169"></a>00169 <span class="keywordflow">default</span>:
<a name="l00170"></a>00170 <span class="keywordflow">break</span>;
<a name="l00171"></a>00171 }
<a name="l00172"></a>00172
<a name="l00173"></a>00173 <span class="comment">/* Some formats want stream headers to be separate. */</span>
<a name="l00174"></a>00174 <span class="keywordflow">if</span> (oc-><a class="code" href="structAVFormatContext.html#a20d80ac07e38ff5c268d15aaf2798b98" title="The output container format.">oformat</a>-><a class="code" href="structAVOutputFormat.html#aad55a00e728a020c1dcfaaf695320445" title="can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT">flags</a> & <a class="code" href="avformat_8h.html#ab203c7b734e9c31b7c37d34f6e2c6aef" title="Format wants global header.">AVFMT_GLOBALHEADER</a>)
<a name="l00175"></a>00175 c-><a class="code" href="structAVCodecContext.html#abb01e291550fa3fb96188af4d494587e" title="CODEC_FLAG_*.">flags</a> |= <a class="code" href="group__lavc__core.html#gaab6ceefd617cf265ef7ea1b87ed5933e" title="Place global headers in extradata instead of every keyframe.">CODEC_FLAG_GLOBAL_HEADER</a>;
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177
<a name="l00178"></a>00178 <span class="comment">/**************************************************************/</span>
<a name="l00179"></a>00179 <span class="comment">/* audio output */</span>
<a name="l00180"></a>00180
<a name="l00181"></a><a class="code" href="muxing_8c.html#a368b2deb0a63160293db69808acfeff2">00181</a> <span class="keyword">static</span> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="muxing_8c.html#a368b2deb0a63160293db69808acfeff2">alloc_audio_frame</a>(<span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> sample_fmt,
<a name="l00182"></a>00182 uint64_t channel_layout,
<a name="l00183"></a>00183 <span class="keywordtype">int</span> sample_rate, <span class="keywordtype">int</span> nb_samples)
<a name="l00184"></a>00184 {
<a name="l00185"></a>00185 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</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>();
<a name="l00186"></a>00186 <span class="keywordtype">int</span> ret;
<a name="l00187"></a>00187
<a name="l00188"></a>00188 <span class="keywordflow">if</span> (!frame) {
<a name="l00189"></a>00189 fprintf(stderr, <span class="stringliteral">"Error allocating an audio frame\n"</span>);
<a name="l00190"></a>00190 exit(1);
<a name="l00191"></a>00191 }
<a name="l00192"></a>00192
<a name="l00193"></a>00193 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> = sample_fmt;
<a name="l00194"></a>00194 frame-><a class="code" href="structAVFrame.html#a5f343e0325e3e9d9ed20e34c9e156aef" title="Channel layout of the audio data.">channel_layout</a> = channel_layout;
<a name="l00195"></a>00195 frame-><a class="code" href="structAVFrame.html#ac85daa1316e1f47e78da0ca19b7c60e6" title="Sample rate of the audio data.">sample_rate</a> = sample_rate;
<a name="l00196"></a>00196 frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a> = nb_samples;
<a name="l00197"></a>00197
<a name="l00198"></a>00198 <span class="keywordflow">if</span> (nb_samples) {
<a name="l00199"></a>00199 ret = <a class="code" href="group__lavu__frame.html#ga6b1acbfa82c79bf7fd78d868572f0ceb" title="Allocate new buffer(s) for audio or video data.">av_frame_get_buffer</a>(frame, 0);
<a name="l00200"></a>00200 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00201"></a>00201 fprintf(stderr, <span class="stringliteral">"Error allocating an audio buffer\n"</span>);
<a name="l00202"></a>00202 exit(1);
<a name="l00203"></a>00203 }
<a name="l00204"></a>00204 }
<a name="l00205"></a>00205
<a name="l00206"></a>00206 <span class="keywordflow">return</span> <a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>;
<a name="l00207"></a>00207 }
<a name="l00208"></a>00208
<a name="l00209"></a><a class="code" href="muxing_8c.html#ae7697b74a6a8605a91487b43e31f29be">00209</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#ae7697b74a6a8605a91487b43e31f29be">open_audio</a>(<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc, <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec, <a class="code" href="structOutputStream.html">OutputStream</a> *ost, <a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opt_arg)
<a name="l00210"></a>00210 {
<a name="l00211"></a>00211 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c;
<a name="l00212"></a>00212 <span class="keywordtype">int</span> nb_samples;
<a name="l00213"></a>00213 <span class="keywordtype">int</span> ret;
<a name="l00214"></a>00214 <a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opt = NULL;
<a name="l00215"></a>00215
<a name="l00216"></a>00216 c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00217"></a>00217
<a name="l00218"></a>00218 <span class="comment">/* open it */</span>
<a name="l00219"></a>00219 <a class="code" href="group__lavu__dict.html#ga6dabaab2d8a53674db6ffd5ac8de3d9f" title="Copy entries from one AVDictionary struct into another.">av_dict_copy</a>(&opt, opt_arg, 0);
<a name="l00220"></a>00220 ret = <a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, &opt);
<a name="l00221"></a>00221 <a class="code" href="group__lavu__dict.html#ga1bafd682b1fbb90e48a4cc3814b820f7" title="Free all the memory allocated for an AVDictionary struct and all keys and values.">av_dict_free</a>(&opt);
<a name="l00222"></a>00222 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00223"></a>00223 fprintf(stderr, <span class="stringliteral">"Could not open audio codec: %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="l00224"></a>00224 exit(1);
<a name="l00225"></a>00225 }
<a name="l00226"></a>00226
<a name="l00227"></a>00227 <span class="comment">/* init signal generator */</span>
<a name="l00228"></a>00228 ost-><a class="code" href="structOutputStream.html#ab5bc1d10f171e32c5241ab23271ae765">t</a> = 0;
<a name="l00229"></a>00229 ost-><a class="code" href="structOutputStream.html#afacbd1335db05b68d2ea528c8b5d197b">tincr</a> = 2 * M_PI * 110.0 / c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>;
<a name="l00230"></a>00230 <span class="comment">/* increment frequency by 110 Hz per second */</span>
<a name="l00231"></a>00231 ost-><a class="code" href="structOutputStream.html#a04fe3fc54e1a382047e501c4ce63497b">tincr2</a> = 2 * M_PI * 110.0 / c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> / c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>;
<a name="l00232"></a>00232
<a name="l00233"></a>00233 <span class="keywordflow">if</span> (c-><a class="code" href="structAVCodecContext.html#ab00398f08f8622c3e6c0f1a12eb99808">codec</a>-><a class="code" href="structAVCodec.html#af51f7ff3dac8b730f46b9713e49a2518" title="Codec capabilities.">capabilities</a> & <a class="code" href="group__lavc__core.html#gaa2e644f74c8013e29ec51d6accc7ac1b" title="Audio encoder supports receiving a different number of samples in each call.">CODEC_CAP_VARIABLE_FRAME_SIZE</a>)
<a name="l00234"></a>00234 nb_samples = 10000;
<a name="l00235"></a>00235 <span class="keywordflow">else</span>
<a name="l00236"></a>00236 nb_samples = c-><a class="code" href="structAVCodecContext.html#aec57f0d859a6df8b479cd93ca3a44a33" title="Number of samples per channel in an audio frame.">frame_size</a>;
<a name="l00237"></a>00237
<a name="l00238"></a>00238 ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a> = <a class="code" href="muxing_8c.html#a368b2deb0a63160293db69808acfeff2">alloc_audio_frame</a>(c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>, c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a>,
<a name="l00239"></a>00239 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, nb_samples);
<a name="l00240"></a>00240 ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a> = <a class="code" href="muxing_8c.html#a368b2deb0a63160293db69808acfeff2">alloc_audio_frame</a>(<a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5aea6132df57aebc3f76e10665395c46af" title="signed 16 bits">AV_SAMPLE_FMT_S16</a>, c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a>,
<a name="l00241"></a>00241 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, nb_samples);
<a name="l00242"></a>00242
<a name="l00243"></a>00243 <span class="comment">/* create resampler context */</span>
<a name="l00244"></a>00244 ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a> = <a class="code" href="group__lswr.html#gaf58c4ff10f73d74bdab8e5aa7193147c" title="Allocate SwrContext.">swr_alloc</a>();
<a name="l00245"></a>00245 <span class="keywordflow">if</span> (!ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>) {
<a name="l00246"></a>00246 fprintf(stderr, <span class="stringliteral">"Could not allocate resampler context\n"</span>);
<a name="l00247"></a>00247 exit(1);
<a name="l00248"></a>00248 }
<a name="l00249"></a>00249
<a name="l00250"></a>00250 <span class="comment">/* set options */</span>
<a name="l00251"></a>00251 <a class="code" href="group__opt__set__funcs.html#ga3adf7185c21cc080890a5ec02c2e56b2">av_opt_set_int</a> (ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"in_channel_count"</span>, c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>, 0);
<a name="l00252"></a>00252 <a class="code" href="group__opt__set__funcs.html#ga3adf7185c21cc080890a5ec02c2e56b2">av_opt_set_int</a> (ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"in_sample_rate"</span>, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, 0);
<a name="l00253"></a>00253 <a class="code" href="group__opt__set__funcs.html#gad446f7a7b92442e464d43e400e661040">av_opt_set_sample_fmt</a>(ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"in_sample_fmt"</span>, <a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5aea6132df57aebc3f76e10665395c46af" title="signed 16 bits">AV_SAMPLE_FMT_S16</a>, 0);
<a name="l00254"></a>00254 <a class="code" href="group__opt__set__funcs.html#ga3adf7185c21cc080890a5ec02c2e56b2">av_opt_set_int</a> (ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"out_channel_count"</span>, c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>, 0);
<a name="l00255"></a>00255 <a class="code" href="group__opt__set__funcs.html#ga3adf7185c21cc080890a5ec02c2e56b2">av_opt_set_int</a> (ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"out_sample_rate"</span>, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, 0);
<a name="l00256"></a>00256 <a class="code" href="group__opt__set__funcs.html#gad446f7a7b92442e464d43e400e661040">av_opt_set_sample_fmt</a>(ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, <span class="stringliteral">"out_sample_fmt"</span>, c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>, 0);
<a name="l00257"></a>00257
<a name="l00258"></a>00258 <span class="comment">/* initialize the resampling context */</span>
<a name="l00259"></a>00259 <span class="keywordflow">if</span> ((ret = <a class="code" href="group__lswr.html#gae173e8ed91717700471a1dcd06f00f67" title="Initialize context after user parameters have been set.">swr_init</a>(ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>)) < 0) {
<a name="l00260"></a>00260 fprintf(stderr, <span class="stringliteral">"Failed to initialize the resampling context\n"</span>);
<a name="l00261"></a>00261 exit(1);
<a name="l00262"></a>00262 }
<a name="l00263"></a>00263 }
<a name="l00264"></a>00264
<a name="l00265"></a>00265 <span class="comment">/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and</span>
<a name="l00266"></a>00266 <span class="comment"> * 'nb_channels' channels. */</span>
<a name="l00267"></a><a class="code" href="muxing_8c.html#a58000b15271c25fa9b2839d82f6971dc">00267</a> <span class="keyword">static</span> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="muxing_8c.html#a58000b15271c25fa9b2839d82f6971dc">get_audio_frame</a>(<a class="code" href="structOutputStream.html">OutputStream</a> *ost)
<a name="l00268"></a>00268 {
<a name="l00269"></a>00269 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a> = ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a>;
<a name="l00270"></a>00270 <span class="keywordtype">int</span> j, i, v;
<a name="l00271"></a>00271 int16_t *q = (int16_t*)frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[0];
<a name="l00272"></a>00272
<a name="l00273"></a>00273 <span class="comment">/* check if we want to generate more frames */</span>
<a name="l00274"></a>00274 if (<a class="code" href="group__lavu__math.html#ga151744358fff630942b926e67e67c415" title="Compare 2 timestamps each in its own timebases.">av_compare_ts</a>(ost-><a class="code" href="structOutputStream.html#aaa3b9001e4dac709017237c1c177cd0a">next_pts</a>, ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</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>,
<a name="l00275"></a>00275 <a class="code" href="muxing_8c.html#ace0cad5fe744009aeaa62eb1fbb31831">STREAM_DURATION</a>, (<a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){ 1, 1 }) >= 0)
<a name="l00276"></a>00276 <span class="keywordflow">return</span> NULL;
<a name="l00277"></a>00277
<a name="l00278"></a>00278 <span class="keywordflow">for</span> (j = 0; j <<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>; j++) {
<a name="l00279"></a>00279 v = (int)(sin(ost->t) * 10000);
<a name="l00280"></a>00280 <span class="keywordflow">for</span> (i = 0; i < ost->st->codec->channels; i++)
<a name="l00281"></a>00281 *q++ = v;
<a name="l00282"></a>00282 ost->t += ost->tincr;
<a name="l00283"></a>00283 ost->tincr += ost->tincr2;
<a name="l00284"></a>00284 }
<a name="l00285"></a>00285
<a name="l00286"></a>00286 <a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</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> = ost->next_pts;
<a name="l00287"></a>00287 ost->next_pts += <a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>;
<a name="l00288"></a>00288
<a name="l00289"></a>00289 <span class="keywordflow">return</span> <a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>;
<a name="l00290"></a>00290 }
<a name="l00291"></a>00291
<a name="l00292"></a>00292 <span class="comment">/*</span>
<a name="l00293"></a>00293 <span class="comment"> * encode one audio frame and send it to the muxer</span>
<a name="l00294"></a>00294 <span class="comment"> * return 1 when encoding is finished, 0 otherwise</span>
<a name="l00295"></a>00295 <span class="comment"> */</span>
<a name="l00296"></a><a class="code" href="muxing_8c.html#a789f35413429ab70610c90c22fc7a4d3">00296</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="muxing_8c.html#a789f35413429ab70610c90c22fc7a4d3">write_audio_frame</a>(<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc, <a class="code" href="structOutputStream.html">OutputStream</a> *ost)
<a name="l00297"></a>00297 {
<a name="l00298"></a>00298 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c;
<a name="l00299"></a>00299 <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a> = { 0 }; <span class="comment">// data and size must be 0;</span>
<a name="l00300"></a>00300 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>;
<a name="l00301"></a>00301 <span class="keywordtype">int</span> ret;
<a name="l00302"></a>00302 <span class="keywordtype">int</span> got_packet;
<a name="l00303"></a>00303 <span class="keywordtype">int</span> dst_nb_samples;
<a name="l00304"></a>00304
<a name="l00305"></a>00305 <a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
<a name="l00306"></a>00306 c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00307"></a>00307
<a name="l00308"></a>00308 frame = <a class="code" href="muxing_8c.html#a58000b15271c25fa9b2839d82f6971dc">get_audio_frame</a>(ost);
<a name="l00309"></a>00309
<a name="l00310"></a>00310 <span class="keywordflow">if</span> (frame) {
<a name="l00311"></a>00311 <span class="comment">/* convert samples from native format to destination codec format, using the resampler */</span>
<a name="l00312"></a>00312 <span class="comment">/* compute destination number of samples */</span>
<a name="l00313"></a>00313 dst_nb_samples = <a class="code" href="group__lavu__math.html#gad686ab94939ed4c65218309e94bdca72" title="Rescale a 64-bit integer with specified rounding.">av_rescale_rnd</a>(<a class="code" href="group__lswr.html#ga5121a5a7890a2d23b72dc871dd0ebb06" title="Gets the delay the next input sample will experience relative to the next output sample.">swr_get_delay</a>(ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>) + frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>,
<a name="l00314"></a>00314 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>, <a class="code" href="group__lavu__math.html#gga921d656eaf2c4d6800a734a13af021d0aa1d28e08e2f8b49b256e056f93038c1a" title="Round toward +infinity.">AV_ROUND_UP</a>);
<a name="l00315"></a>00315 <a class="code" href="avassert_8h.html#aae204192396f78cc8c5cd7ad5c57c23f" title="assert() equivalent, that is always enabled.">av_assert0</a>(dst_nb_samples == frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>);
<a name="l00316"></a>00316
<a name="l00317"></a>00317 <span class="comment">/* when we pass a frame to the encoder, it may keep a reference to it</span>
<a name="l00318"></a>00318 <span class="comment"> * internally;</span>
<a name="l00319"></a>00319 <span class="comment"> * make sure we do not overwrite it here</span>
<a name="l00320"></a>00320 <span class="comment"> */</span>
<a name="l00321"></a>00321 ret = <a class="code" href="group__lavu__frame.html#gadd5417c06f5a6b419b0dbd8f0ff363fd" title="Ensure that the frame data is writable, avoiding data copy if possible.">av_frame_make_writable</a>(ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>);
<a name="l00322"></a>00322 <span class="keywordflow">if</span> (ret < 0)
<a name="l00323"></a>00323 exit(1);
<a name="l00324"></a>00324
<a name="l00325"></a>00325 <span class="comment">/* convert to destination format */</span>
<a name="l00326"></a>00326 ret = <a class="code" href="group__lswr.html#gaa5bb6cab830146efa8c760fa66ee582a" title="Convert audio.">swr_convert</a>(ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>,
<a name="l00327"></a>00327 ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>, dst_nb_samples,
<a name="l00328"></a>00328 (<span class="keyword">const</span> uint8_t **)frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>, frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>);
<a name="l00329"></a>00329 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00330"></a>00330 fprintf(stderr, <span class="stringliteral">"Error while converting\n"</span>);
<a name="l00331"></a>00331 exit(1);
<a name="l00332"></a>00332 }
<a name="l00333"></a>00333 frame = ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>;
<a name="l00334"></a>00334
<a name="l00335"></a>00335 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="group__lavu__math.html#gaf02994a8bbeaa91d4757df179cbe567f" title="Rescale a 64-bit integer by 2 rational numbers.">av_rescale_q</a>(ost-><a class="code" href="structOutputStream.html#a0c3b9816e5c50c0ed816343d224121e7">samples_count</a>, (<a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){1, c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>}, c-><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>);
<a name="l00336"></a>00336 ost-><a class="code" href="structOutputStream.html#a0c3b9816e5c50c0ed816343d224121e7">samples_count</a> += dst_nb_samples;
<a name="l00337"></a>00337 }
<a name="l00338"></a>00338
<a name="l00339"></a>00339 ret = <a class="code" href="group__lavc__encoding.html#ga93a49fbd0973b216dcb8a8c5dffe1d82" title="Encode a frame of audio.">avcodec_encode_audio2</a>(c, &<a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>, <a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>, &got_packet);
<a name="l00340"></a>00340 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00341"></a>00341 fprintf(stderr, <span class="stringliteral">"Error encoding 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));
<a name="l00342"></a>00342 exit(1);
<a name="l00343"></a>00343 }
<a name="l00344"></a>00344
<a name="l00345"></a>00345 <span class="keywordflow">if</span> (got_packet) {
<a name="l00346"></a>00346 ret = <a class="code" href="muxing_8c.html#ab85f450fd9ee7aad777fc135b62dd3cd">write_frame</a>(oc, &c->time_base, ost->st, &<a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>);
<a name="l00347"></a>00347 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00348"></a>00348 fprintf(stderr, <span class="stringliteral">"Error while writing audio frame: %s\n"</span>,
<a name="l00349"></a>00349 <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="l00350"></a>00350 exit(1);
<a name="l00351"></a>00351 }
<a name="l00352"></a>00352 }
<a name="l00353"></a>00353
<a name="l00354"></a>00354 <span class="keywordflow">return</span> (<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a> || got_packet) ? 0 : 1;
<a name="l00355"></a>00355 }
<a name="l00356"></a>00356
<a name="l00357"></a>00357 <span class="comment">/**************************************************************/</span>
<a name="l00358"></a>00358 <span class="comment">/* video output */</span>
<a name="l00359"></a>00359
<a name="l00360"></a><a class="code" href="muxing_8c.html#a03824343735e44b58ec03ff1691f8b45">00360</a> <span class="keyword">static</span> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="muxing_8c.html#a03824343735e44b58ec03ff1691f8b45">alloc_picture</a>(<span class="keyword">enum</span> <a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5" title="Pixel format.">AVPixelFormat</a> <a class="code" href="demuxing__decoding_8c.html#a7331e302ea7bb48e0b605a069576e135">pix_fmt</a>, <span class="keywordtype">int</span> <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <span class="keywordtype">int</span> <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>)
<a name="l00361"></a>00361 {
<a name="l00362"></a>00362 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *picture;
<a name="l00363"></a>00363 <span class="keywordtype">int</span> ret;
<a name="l00364"></a>00364
<a name="l00365"></a>00365 picture = <a class="code" href="group__lavu__frame.html#gac700017c5270c79c1e1befdeeb008b2f" title="Allocate an AVFrame and set its fields to default values.">av_frame_alloc</a>();
<a name="l00366"></a>00366 <span class="keywordflow">if</span> (!picture)
<a name="l00367"></a>00367 <span class="keywordflow">return</span> NULL;
<a name="l00368"></a>00368
<a name="l00369"></a>00369 picture-><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>;
<a name="l00370"></a>00370 picture-><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>;
<a name="l00371"></a>00371 picture-><a class="code" href="structAVFrame.html#a3f89733f429c98ba5bc64373fb0a3f13">height</a> = <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>;
<a name="l00372"></a>00372
<a name="l00373"></a>00373 <span class="comment">/* allocate the buffers for the frame data */</span>
<a name="l00374"></a>00374 ret = <a class="code" href="group__lavu__frame.html#ga6b1acbfa82c79bf7fd78d868572f0ceb" title="Allocate new buffer(s) for audio or video data.">av_frame_get_buffer</a>(picture, 32);
<a name="l00375"></a>00375 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00376"></a>00376 fprintf(stderr, <span class="stringliteral">"Could not allocate frame data.\n"</span>);
<a name="l00377"></a>00377 exit(1);
<a name="l00378"></a>00378 }
<a name="l00379"></a>00379
<a name="l00380"></a>00380 <span class="keywordflow">return</span> picture;
<a name="l00381"></a>00381 }
<a name="l00382"></a>00382
<a name="l00383"></a><a class="code" href="muxing_8c.html#a34f7b1a6fc3698c190131c3df2e85903">00383</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#a34f7b1a6fc3698c190131c3df2e85903">open_video</a>(<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc, <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec, <a class="code" href="structOutputStream.html">OutputStream</a> *ost, <a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opt_arg)
<a name="l00384"></a>00384 {
<a name="l00385"></a>00385 <span class="keywordtype">int</span> ret;
<a name="l00386"></a>00386 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00387"></a>00387 <a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opt = NULL;
<a name="l00388"></a>00388
<a name="l00389"></a>00389 <a class="code" href="group__lavu__dict.html#ga6dabaab2d8a53674db6ffd5ac8de3d9f" title="Copy entries from one AVDictionary struct into another.">av_dict_copy</a>(&opt, opt_arg, 0);
<a name="l00390"></a>00390
<a name="l00391"></a>00391 <span class="comment">/* open the codec */</span>
<a name="l00392"></a>00392 ret = <a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, &opt);
<a name="l00393"></a>00393 <a class="code" href="group__lavu__dict.html#ga1bafd682b1fbb90e48a4cc3814b820f7" title="Free all the memory allocated for an AVDictionary struct and all keys and values.">av_dict_free</a>(&opt);
<a name="l00394"></a>00394 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00395"></a>00395 fprintf(stderr, <span class="stringliteral">"Could not open video codec: %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="l00396"></a>00396 exit(1);
<a name="l00397"></a>00397 }
<a name="l00398"></a>00398
<a name="l00399"></a>00399 <span class="comment">/* allocate and init a re-usable frame */</span>
<a name="l00400"></a>00400 ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a> = <a class="code" href="muxing_8c.html#a03824343735e44b58ec03ff1691f8b45">alloc_picture</a>(c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a>, c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>, c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>);
<a name="l00401"></a>00401 <span class="keywordflow">if</span> (!ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>) {
<a name="l00402"></a>00402 fprintf(stderr, <span class="stringliteral">"Could not allocate video frame\n"</span>);
<a name="l00403"></a>00403 exit(1);
<a name="l00404"></a>00404 }
<a name="l00405"></a>00405
<a name="l00406"></a>00406 <span class="comment">/* If the output format is not YUV420P, then a temporary YUV420P</span>
<a name="l00407"></a>00407 <span class="comment"> * picture is needed too. It is then converted to the required</span>
<a name="l00408"></a>00408 <span class="comment"> * output format. */</span>
<a name="l00409"></a>00409 ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a> = NULL;
<a name="l00410"></a>00410 <span class="keywordflow">if</span> (c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a> != <a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5a1aa7677092740d8def31655b5d7f0cc2" title="planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)">AV_PIX_FMT_YUV420P</a>) {
<a name="l00411"></a>00411 ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a> = <a class="code" href="muxing_8c.html#a03824343735e44b58ec03ff1691f8b45">alloc_picture</a>(<a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5a1aa7677092740d8def31655b5d7f0cc2" title="planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)">AV_PIX_FMT_YUV420P</a>, c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>, c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>);
<a name="l00412"></a>00412 <span class="keywordflow">if</span> (!ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a>) {
<a name="l00413"></a>00413 fprintf(stderr, <span class="stringliteral">"Could not allocate temporary picture\n"</span>);
<a name="l00414"></a>00414 exit(1);
<a name="l00415"></a>00415 }
<a name="l00416"></a>00416 }
<a name="l00417"></a>00417 }
<a name="l00418"></a>00418
<a name="l00419"></a>00419 <span class="comment">/* Prepare a dummy image. */</span>
<a name="l00420"></a><a class="code" href="muxing_8c.html#acc346658c36a422fc9ab42bbb976138e">00420</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#acc346658c36a422fc9ab42bbb976138e">fill_yuv_image</a>(<a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *pict, <span class="keywordtype">int</span> frame_index,
<a name="l00421"></a>00421 <span class="keywordtype">int</span> <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>, <span class="keywordtype">int</span> <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>)
<a name="l00422"></a>00422 {
<a name="l00423"></a>00423 <span class="keywordtype">int</span> x, y, i, ret;
<a name="l00424"></a>00424
<a name="l00425"></a>00425 <span class="comment">/* when we pass a frame to the encoder, it may keep a reference to it</span>
<a name="l00426"></a>00426 <span class="comment"> * internally;</span>
<a name="l00427"></a>00427 <span class="comment"> * make sure we do not overwrite it here</span>
<a name="l00428"></a>00428 <span class="comment"> */</span>
<a name="l00429"></a>00429 ret = <a class="code" href="group__lavu__frame.html#gadd5417c06f5a6b419b0dbd8f0ff363fd" title="Ensure that the frame data is writable, avoiding data copy if possible.">av_frame_make_writable</a>(pict);
<a name="l00430"></a>00430 <span class="keywordflow">if</span> (ret < 0)
<a name="l00431"></a>00431 exit(1);
<a name="l00432"></a>00432
<a name="l00433"></a>00433 i = frame_index;
<a name="l00434"></a>00434
<a name="l00435"></a>00435 <span class="comment">/* Y */</span>
<a name="l00436"></a>00436 <span class="keywordflow">for</span> (y = 0; y < <a class="code" href="demuxing__decoding_8c.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>; y++)
<a name="l00437"></a>00437 <span class="keywordflow">for</span> (x = 0; x < <a class="code" href="demuxing__decoding_8c.html#a2474a5474cbff19523a51eb1de01cda4">width</a>; x++)
<a name="l00438"></a>00438 pict-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[0][y * pict-><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</a>[0] + x] = x + y + i * 3;
<a name="l00439"></a>00439
<a name="l00440"></a>00440 <span class="comment">/* Cb and Cr */</span>
<a name="l00441"></a>00441 for (y = 0; y < height / 2; y++) {
<a name="l00442"></a>00442 <span class="keywordflow">for</span> (x = 0; x < width / 2; x++) {
<a name="l00443"></a>00443 pict-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[1][y * pict-><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</a>[1] + x] = 128 + y + i * 2;
<a name="l00444"></a>00444 pict-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[2][y * pict-><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</a>[2] + x] = 64 + x + i * 5;
<a name="l00445"></a>00445 }
<a name="l00446"></a>00446 }
<a name="l00447"></a>00447 }
<a name="l00448"></a>00448
<a name="l00449"></a><a class="code" href="muxing_8c.html#a2a1515bfcdc1e221407cba978bcad1f0">00449</a> <span class="keyword">static</span> <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="muxing_8c.html#a2a1515bfcdc1e221407cba978bcad1f0">get_video_frame</a>(<a class="code" href="structOutputStream.html">OutputStream</a> *ost)
<a name="l00450"></a>00450 {
<a name="l00451"></a>00451 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00452"></a>00452
<a name="l00453"></a>00453 <span class="comment">/* check if we want to generate more frames */</span>
<a name="l00454"></a>00454 <span class="keywordflow">if</span> (<a class="code" href="group__lavu__math.html#ga151744358fff630942b926e67e67c415" title="Compare 2 timestamps each in its own timebases.">av_compare_ts</a>(ost-><a class="code" href="structOutputStream.html#aaa3b9001e4dac709017237c1c177cd0a">next_pts</a>, ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</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>,
<a name="l00455"></a>00455 <a class="code" href="muxing_8c.html#ace0cad5fe744009aeaa62eb1fbb31831">STREAM_DURATION</a>, (<a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){ 1, 1 }) >= 0)
<a name="l00456"></a>00456 <span class="keywordflow">return</span> NULL;
<a name="l00457"></a>00457
<a name="l00458"></a>00458 <span class="keywordflow">if</span> (c->pix_fmt != <a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5a1aa7677092740d8def31655b5d7f0cc2" title="planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)">AV_PIX_FMT_YUV420P</a>) {
<a name="l00459"></a>00459 <span class="comment">/* as we only generate a YUV420P picture, we must convert it</span>
<a name="l00460"></a>00460 <span class="comment"> * to the codec pixel format if needed */</span>
<a name="l00461"></a>00461 <span class="keywordflow">if</span> (!ost->sws_ctx) {
<a name="l00462"></a>00462 ost->sws_ctx = <a class="code" href="group__libsws.html#gaf360d1a9e0e60f906f74d7d44f9abfdd" title="Allocate and return an SwsContext.">sws_getContext</a>(c->width, c->height,
<a name="l00463"></a>00463 <a class="code" href="pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5a1aa7677092740d8def31655b5d7f0cc2" title="planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)">AV_PIX_FMT_YUV420P</a>,
<a name="l00464"></a>00464 c->width, c->height,
<a name="l00465"></a>00465 c->pix_fmt,
<a name="l00466"></a>00466 <a class="code" href="muxing_8c.html#ab746bcb79e75e498c4925ccd72a34f63">SCALE_FLAGS</a>, NULL, NULL, NULL);
<a name="l00467"></a>00467 <span class="keywordflow">if</span> (!ost->sws_ctx) {
<a name="l00468"></a>00468 fprintf(stderr,
<a name="l00469"></a>00469 <span class="stringliteral">"Could not initialize the conversion context\n"</span>);
<a name="l00470"></a>00470 exit(1);
<a name="l00471"></a>00471 }
<a name="l00472"></a>00472 }
<a name="l00473"></a>00473 <a class="code" href="muxing_8c.html#acc346658c36a422fc9ab42bbb976138e">fill_yuv_image</a>(ost->tmp_frame, ost->next_pts, c->width, c->height);
<a name="l00474"></a>00474 <a class="code" href="group__libsws.html#gae531c9754c9205d90ad6800015046d74" title="Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst...">sws_scale</a>(ost->sws_ctx,
<a name="l00475"></a>00475 (<span class="keyword">const</span> uint8_t * <span class="keyword">const</span> *)ost->tmp_frame->data, ost->tmp_frame->linesize,
<a name="l00476"></a>00476 0, c->height, ost->frame->data, ost->frame->linesize);
<a name="l00477"></a>00477 } <span class="keywordflow">else</span> {
<a name="l00478"></a>00478 <a class="code" href="muxing_8c.html#acc346658c36a422fc9ab42bbb976138e">fill_yuv_image</a>(ost->frame, ost->next_pts, c->width, c->height);
<a name="l00479"></a>00479 }
<a name="l00480"></a>00480
<a name="l00481"></a>00481 ost->frame->pts = ost->next_pts++;
<a name="l00482"></a>00482
<a name="l00483"></a>00483 <span class="keywordflow">return</span> ost->frame;
<a name="l00484"></a>00484 }
<a name="l00485"></a>00485
<a name="l00486"></a>00486 <span class="comment">/*</span>
<a name="l00487"></a>00487 <span class="comment"> * encode one video frame and send it to the muxer</span>
<a name="l00488"></a>00488 <span class="comment"> * return 1 when encoding is finished, 0 otherwise</span>
<a name="l00489"></a>00489 <span class="comment"> */</span>
<a name="l00490"></a><a class="code" href="muxing_8c.html#a435288caae5076cca3273215cb6ad395">00490</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="muxing_8c.html#a435288caae5076cca3273215cb6ad395">write_video_frame</a>(<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc, <a class="code" href="structOutputStream.html">OutputStream</a> *ost)
<a name="l00491"></a>00491 {
<a name="l00492"></a>00492 <span class="keywordtype">int</span> ret;
<a name="l00493"></a>00493 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c;
<a name="l00494"></a>00494 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>;
<a name="l00495"></a>00495 <span class="keywordtype">int</span> got_packet = 0;
<a name="l00496"></a>00496
<a name="l00497"></a>00497 c = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>;
<a name="l00498"></a>00498
<a name="l00499"></a>00499 frame = <a class="code" href="muxing_8c.html#a2a1515bfcdc1e221407cba978bcad1f0">get_video_frame</a>(ost);
<a name="l00500"></a>00500
<a name="l00501"></a>00501 <span class="keywordflow">if</span> (oc-><a class="code" href="structAVFormatContext.html#a20d80ac07e38ff5c268d15aaf2798b98" title="The output container format.">oformat</a>-><a class="code" href="structAVOutputFormat.html#aad55a00e728a020c1dcfaaf695320445" title="can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT">flags</a> & <a class="code" href="avformat_8h.html#a71f4268e7674596c14c18b4bb3e60a3e" title="Format wants AVPicture structure for raw picture data.">AVFMT_RAWPICTURE</a>) {
<a name="l00502"></a>00502 <span class="comment">/* a hack to avoid data copy with some raw video muxers */</span>
<a name="l00503"></a>00503 <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>;
<a name="l00504"></a>00504 <a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
<a name="l00505"></a>00505
<a name="l00506"></a>00506 <span class="keywordflow">if</span> (!frame)
<a name="l00507"></a>00507 <span class="keywordflow">return</span> 1;
<a name="l00508"></a>00508
<a name="l00509"></a>00509 pkt.<a class="code" href="structAVPacket.html#a437be96a9da675f12caa228a9c81bd82" title="A combination of AV_PKT_FLAG values.">flags</a> |= <a class="code" href="group__lavc__packet.html#ga75603d7c2b8adf5829f4fd2fb860168f" title="The packet contains a keyframe.">AV_PKT_FLAG_KEY</a>;
<a name="l00510"></a>00510 pkt.<a class="code" href="structAVPacket.html#a0d1cb9b5a32b00fb6edc81ea3aae2a49">stream_index</a> = ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a6ca823054632821e085377f7d371a2d1" title="stream index in AVFormatContext">index</a>;
<a name="l00511"></a>00511 pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = (uint8_t *)frame;
<a name="l00512"></a>00512 pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = <span class="keyword">sizeof</span>(<a class="code" href="structAVPicture.html" title="Picture data structure.">AVPicture</a>);
<a name="l00513"></a>00513
<a name="l00514"></a>00514 pkt.<a class="code" href="structAVPacket.html#a73bde0a37f3b1efc839f11295bfbf42a" title="Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...">pts</a> = pkt.<a class="code" href="structAVPacket.html#a85dbbd306b44b02390cd91c45e6a0f76" title="Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...">dts</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 name="l00515"></a>00515 <a class="code" href="group__lavc__packet.html#gae5c86e4d93f6e7aa62ef2c60763ea67e" title="Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...">av_packet_rescale_ts</a>(&pkt, c-><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>, ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a9db755451f14e2bf590d4b85d82b32e6" title="This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...">time_base</a>);
<a name="l00516"></a>00516
<a name="l00517"></a>00517 ret = <a class="code" href="group__lavf__encoding.html#ga37352ed2c63493c38219d935e71db6c1" title="Write a packet to an output media file ensuring correct interleaving.">av_interleaved_write_frame</a>(oc, &pkt);
<a name="l00518"></a>00518 } <span class="keywordflow">else</span> {
<a name="l00519"></a>00519 <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a> = { 0 };
<a name="l00520"></a>00520 <a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
<a name="l00521"></a>00521
<a name="l00522"></a>00522 <span class="comment">/* encode the image */</span>
<a name="l00523"></a>00523 ret = <a class="code" href="group__lavc__encoding.html#gaa2dc9e9ea2567ebb2801a08153c7306b" title="Encode a frame of video.">avcodec_encode_video2</a>(c, &pkt, frame, &got_packet);
<a name="l00524"></a>00524 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00525"></a>00525 fprintf(stderr, <span class="stringliteral">"Error encoding video 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));
<a name="l00526"></a>00526 exit(1);
<a name="l00527"></a>00527 }
<a name="l00528"></a>00528
<a name="l00529"></a>00529 <span class="keywordflow">if</span> (got_packet) {
<a name="l00530"></a>00530 ret = <a class="code" href="muxing_8c.html#ab85f450fd9ee7aad777fc135b62dd3cd">write_frame</a>(oc, &c-><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>, ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>, &pkt);
<a name="l00531"></a>00531 } <span class="keywordflow">else</span> {
<a name="l00532"></a>00532 ret = 0;
<a name="l00533"></a>00533 }
<a name="l00534"></a>00534 }
<a name="l00535"></a>00535
<a name="l00536"></a>00536 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00537"></a>00537 fprintf(stderr, <span class="stringliteral">"Error while writing video 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));
<a name="l00538"></a>00538 exit(1);
<a name="l00539"></a>00539 }
<a name="l00540"></a>00540
<a name="l00541"></a>00541 <span class="keywordflow">return</span> (frame || got_packet) ? 0 : 1;
<a name="l00542"></a>00542 }
<a name="l00543"></a>00543
<a name="l00544"></a><a class="code" href="muxing_8c.html#a92d8b2751255677797d807161fae9d19">00544</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="muxing_8c.html#a92d8b2751255677797d807161fae9d19">close_stream</a>(<a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc, <a class="code" href="structOutputStream.html">OutputStream</a> *ost)
<a name="l00545"></a>00545 {
<a name="l00546"></a>00546 <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>(ost-><a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</a>);
<a name="l00547"></a>00547 <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>(&ost-><a class="code" href="structOutputStream.html#a09ee2998f47d0e69c99dee8a6f71105a">frame</a>);
<a name="l00548"></a>00548 <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>(&ost-><a class="code" href="structOutputStream.html#a2ffa8b8369d869e8a440c2611ba91ae2">tmp_frame</a>);
<a name="l00549"></a>00549 <a class="code" href="group__libsws.html#gad3af0ca76f071dbe0173444db9882932" title="Free the swscaler context swsContext.">sws_freeContext</a>(ost-><a class="code" href="structOutputStream.html#a87e6c2172dab3143e4857aaaf6558573">sws_ctx</a>);
<a name="l00550"></a>00550 <a class="code" href="group__lswr.html#ga818f7d78b1ad7d8d5b70de374b668c34" title="Free the given SwrContext and set the pointer to NULL.">swr_free</a>(&ost-><a class="code" href="structOutputStream.html#a1a5c19db8f6a49a4d7a14168e4b73ec1">swr_ctx</a>);
<a name="l00551"></a>00551 }
<a name="l00552"></a>00552
<a name="l00553"></a>00553 <span class="comment">/**************************************************************/</span>
<a name="l00554"></a>00554 <span class="comment">/* media file output */</span>
<a name="l00555"></a>00555
<a name="l00556"></a><a class="code" href="muxing_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">00556</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="l00557"></a>00557 {
<a name="l00558"></a>00558 <a class="code" href="structOutputStream.html">OutputStream</a> video_st = { 0 }, audio_st = { 0 };
<a name="l00559"></a>00559 <span class="keyword">const</span> <span class="keywordtype">char</span> *filename;
<a name="l00560"></a>00560 <a class="code" href="structAVOutputFormat.html">AVOutputFormat</a> *fmt;
<a name="l00561"></a>00561 <a class="code" href="structAVFormatContext.html" title="Format I/O context.">AVFormatContext</a> *oc;
<a name="l00562"></a>00562 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *audio_codec, *video_codec;
<a name="l00563"></a>00563 <span class="keywordtype">int</span> ret;
<a name="l00564"></a>00564 <span class="keywordtype">int</span> have_video = 0, have_audio = 0;
<a name="l00565"></a>00565 <span class="keywordtype">int</span> encode_video = 0, encode_audio = 0;
<a name="l00566"></a>00566 <a class="code" href="group__lavu__dict.html#ga1d7cc0833bee918994a600556410315f">AVDictionary</a> *opt = NULL;
<a name="l00567"></a>00567
<a name="l00568"></a>00568 <span class="comment">/* Initialize libavcodec, and register all codecs and formats. */</span>
<a name="l00569"></a>00569 <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="l00570"></a>00570
<a name="l00571"></a>00571 <span class="keywordflow">if</span> (argc < 2) {
<a name="l00572"></a>00572 printf(<span class="stringliteral">"usage: %s output_file\n"</span>
<a name="l00573"></a>00573 <span class="stringliteral">"API example program to output a media file with libavformat.\n"</span>
<a name="l00574"></a>00574 <span class="stringliteral">"This program generates a synthetic audio and video stream, encodes and\n"</span>
<a name="l00575"></a>00575 <span class="stringliteral">"muxes them into a file named output_file.\n"</span>
<a name="l00576"></a>00576 <span class="stringliteral">"The output format is automatically guessed according to the file extension.\n"</span>
<a name="l00577"></a>00577 <span class="stringliteral">"Raw images can also be output by using '%%d' in the filename.\n"</span>
<a name="l00578"></a>00578 <span class="stringliteral">"\n"</span>, argv[0]);
<a name="l00579"></a>00579 <span class="keywordflow">return</span> 1;
<a name="l00580"></a>00580 }
<a name="l00581"></a>00581
<a name="l00582"></a>00582 filename = argv[1];
<a name="l00583"></a>00583 <span class="keywordflow">if</span> (argc > 3 && !strcmp(argv[2], <span class="stringliteral">"-flags"</span>)) {
<a name="l00584"></a>00584 <a class="code" href="group__lavu__dict.html#ga8d9c2de72b310cef8e6a28c9cd3acbbe" title="Set the given entry in *pm, overwriting an existing entry.">av_dict_set</a>(&opt, argv[2]+1, argv[3], 0);
<a name="l00585"></a>00585 }
<a name="l00586"></a>00586
<a name="l00587"></a>00587 <span class="comment">/* allocate the output media context */</span>
<a name="l00588"></a>00588 <a class="code" href="avformat_8h.html#a6ddf3d982feb45fa5081420ee911f5d5" title="Allocate an AVFormatContext for an output format.">avformat_alloc_output_context2</a>(&oc, NULL, NULL, filename);
<a name="l00589"></a>00589 <span class="keywordflow">if</span> (!oc) {
<a name="l00590"></a>00590 printf(<span class="stringliteral">"Could not deduce output format from file extension: using MPEG.\n"</span>);
<a name="l00591"></a>00591 <a class="code" href="avformat_8h.html#a6ddf3d982feb45fa5081420ee911f5d5" title="Allocate an AVFormatContext for an output format.">avformat_alloc_output_context2</a>(&oc, NULL, <span class="stringliteral">"mpeg"</span>, filename);
<a name="l00592"></a>00592 }
<a name="l00593"></a>00593 <span class="keywordflow">if</span> (!oc)
<a name="l00594"></a>00594 <span class="keywordflow">return</span> 1;
<a name="l00595"></a>00595
<a name="l00596"></a>00596 fmt = oc-><a class="code" href="structAVFormatContext.html#a20d80ac07e38ff5c268d15aaf2798b98" title="The output container format.">oformat</a>;
<a name="l00597"></a>00597
<a name="l00598"></a>00598 <span class="comment">/* Add the audio and video streams using the default format codecs</span>
<a name="l00599"></a>00599 <span class="comment"> * and initialize the codecs. */</span>
<a name="l00600"></a>00600 <span class="keywordflow">if</span> (fmt-><a class="code" href="structAVOutputFormat.html#a1354a9c8542b1b698157218336bd4754" title="default video codec">video_codec</a> != <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7cea48761c50b102343e2648c3e28a94e7ab">AV_CODEC_ID_NONE</a>) {
<a name="l00601"></a>00601 <a class="code" href="muxing_8c.html#aead090f3b6525cfdfa572f18f7575219">add_stream</a>(&video_st, oc, &video_codec, fmt-><a class="code" href="structAVOutputFormat.html#a1354a9c8542b1b698157218336bd4754" title="default video codec">video_codec</a>);
<a name="l00602"></a>00602 have_video = 1;
<a name="l00603"></a>00603 encode_video = 1;
<a name="l00604"></a>00604 }
<a name="l00605"></a>00605 <span class="keywordflow">if</span> (fmt-><a class="code" href="structAVOutputFormat.html#a2e4fff0aa061984d586ea08ecad96141" title="default audio codec">audio_codec</a> != <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7cea48761c50b102343e2648c3e28a94e7ab">AV_CODEC_ID_NONE</a>) {
<a name="l00606"></a>00606 <a class="code" href="muxing_8c.html#aead090f3b6525cfdfa572f18f7575219">add_stream</a>(&audio_st, oc, &audio_codec, fmt-><a class="code" href="structAVOutputFormat.html#a2e4fff0aa061984d586ea08ecad96141" title="default audio codec">audio_codec</a>);
<a name="l00607"></a>00607 have_audio = 1;
<a name="l00608"></a>00608 encode_audio = 1;
<a name="l00609"></a>00609 }
<a name="l00610"></a>00610
<a name="l00611"></a>00611 <span class="comment">/* Now that all the parameters are set, we can open the audio and</span>
<a name="l00612"></a>00612 <span class="comment"> * video codecs and allocate the necessary encode buffers. */</span>
<a name="l00613"></a>00613 <span class="keywordflow">if</span> (have_video)
<a name="l00614"></a>00614 <a class="code" href="muxing_8c.html#a34f7b1a6fc3698c190131c3df2e85903">open_video</a>(oc, video_codec, &video_st, opt);
<a name="l00615"></a>00615
<a name="l00616"></a>00616 <span class="keywordflow">if</span> (have_audio)
<a name="l00617"></a>00617 <a class="code" href="muxing_8c.html#ae7697b74a6a8605a91487b43e31f29be">open_audio</a>(oc, audio_codec, &audio_st, opt);
<a name="l00618"></a>00618
<a name="l00619"></a>00619 <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>(oc, 0, filename, 1);
<a name="l00620"></a>00620
<a name="l00621"></a>00621 <span class="comment">/* open the output file, if needed */</span>
<a name="l00622"></a>00622 <span class="keywordflow">if</span> (!(fmt-><a class="code" href="structAVOutputFormat.html#aad55a00e728a020c1dcfaaf695320445" title="can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT">flags</a> & <a class="code" href="avformat_8h.html#a752cce390d480521919aa5d8be24ac0b" title="Demuxer will use avio_open, no opened file should be provided by the caller.">AVFMT_NOFILE</a>)) {
<a name="l00623"></a>00623 ret = <a class="code" href="avio_8h.html#a371a670112abc5f3e15bc570da076301" title="Create and initialize a AVIOContext for accessing the resource indicated by url.">avio_open</a>(&oc-><a class="code" href="structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87" title="I/O context.">pb</a>, filename, <a class="code" href="avio_8h.html#a5c9308f296545a8f3b7687d277a6dabc" title="write-only">AVIO_FLAG_WRITE</a>);
<a name="l00624"></a>00624 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00625"></a>00625 fprintf(stderr, <span class="stringliteral">"Could not open '%s': %s\n"</span>, filename,
<a name="l00626"></a>00626 <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="l00627"></a>00627 <span class="keywordflow">return</span> 1;
<a name="l00628"></a>00628 }
<a name="l00629"></a>00629 }
<a name="l00630"></a>00630
<a name="l00631"></a>00631 <span class="comment">/* Write the stream header, if any. */</span>
<a name="l00632"></a>00632 ret = <a class="code" href="group__lavf__encoding.html#ga78d4e734fecb1d2385536e6dd5b7b9f5" title="Allocate the stream private data and write the stream header to an output media file.">avformat_write_header</a>(oc, &opt);
<a name="l00633"></a>00633 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00634"></a>00634 fprintf(stderr, <span class="stringliteral">"Error occurred when opening output file: %s\n"</span>,
<a name="l00635"></a>00635 <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="l00636"></a>00636 <span class="keywordflow">return</span> 1;
<a name="l00637"></a>00637 }
<a name="l00638"></a>00638
<a name="l00639"></a>00639 <span class="keywordflow">while</span> (encode_video || encode_audio) {
<a name="l00640"></a>00640 <span class="comment">/* select the stream to encode */</span>
<a name="l00641"></a>00641 <span class="keywordflow">if</span> (encode_video &&
<a name="l00642"></a>00642 (!encode_audio || <a class="code" href="group__lavu__math.html#ga151744358fff630942b926e67e67c415" title="Compare 2 timestamps each in its own timebases.">av_compare_ts</a>(video_st.<a class="code" href="structOutputStream.html#aaa3b9001e4dac709017237c1c177cd0a">next_pts</a>, video_st.<a class="code" href="structOutputStream.html#ac11510a2eeed3e6a14f0fbc85db2e088">st</a>-><a class="code" href="structAVStream.html#a56f20051fb99339cc588cd3a07ab8c96" title="Codec context associated with this stream.">codec</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>,
<a name="l00643"></a>00643 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
<a name="l00644"></a>00644 encode_video = !<a class="code" href="muxing_8c.html#a435288caae5076cca3273215cb6ad395">write_video_frame</a>(oc, &video_st);
<a name="l00645"></a>00645 } <span class="keywordflow">else</span> {
<a name="l00646"></a>00646 encode_audio = !<a class="code" href="muxing_8c.html#a789f35413429ab70610c90c22fc7a4d3">write_audio_frame</a>(oc, &audio_st);
<a name="l00647"></a>00647 }
<a name="l00648"></a>00648 }
<a name="l00649"></a>00649
<a name="l00650"></a>00650 <span class="comment">/* Write the trailer, if any. The trailer must be written before you</span>
<a name="l00651"></a>00651 <span class="comment"> * close the CodecContexts open when you wrote the header; otherwise</span>
<a name="l00652"></a>00652 <span class="comment"> * av_write_trailer() may try to use memory that was freed on</span>
<a name="l00653"></a>00653 <span class="comment"> * av_codec_close(). */</span>
<a name="l00654"></a>00654 <a class="code" href="group__lavf__encoding.html#ga7f14007e7dc8f481f054b21614dfec13" title="Write the stream trailer to an output media file and free the file private data.">av_write_trailer</a>(oc);
<a name="l00655"></a>00655
<a name="l00656"></a>00656 <span class="comment">/* Close each codec. */</span>
<a name="l00657"></a>00657 <span class="keywordflow">if</span> (have_video)
<a name="l00658"></a>00658 <a class="code" href="muxing_8c.html#a92d8b2751255677797d807161fae9d19">close_stream</a>(oc, &video_st);
<a name="l00659"></a>00659 <span class="keywordflow">if</span> (have_audio)
<a name="l00660"></a>00660 <a class="code" href="muxing_8c.html#a92d8b2751255677797d807161fae9d19">close_stream</a>(oc, &audio_st);
<a name="l00661"></a>00661
<a name="l00662"></a>00662 <span class="keywordflow">if</span> (!(fmt-><a class="code" href="structAVOutputFormat.html#aad55a00e728a020c1dcfaaf695320445" title="can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT">flags</a> & <a class="code" href="avformat_8h.html#a752cce390d480521919aa5d8be24ac0b" title="Demuxer will use avio_open, no opened file should be provided by the caller.">AVFMT_NOFILE</a>))
<a name="l00663"></a>00663 <span class="comment">/* Close the output file. */</span>
<a name="l00664"></a>00664 <a class="code" href="avio_8h.html#ae118a1f37f1e48617609ead9910aac15" title="Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...">avio_closep</a>(&oc-><a class="code" href="structAVFormatContext.html#a1e7324262b6b78522e52064daaa7bc87" title="I/O context.">pb</a>);
<a name="l00665"></a>00665
<a name="l00666"></a>00666 <span class="comment">/* free the stream */</span>
<a name="l00667"></a>00667 <a class="code" href="group__lavf__core.html#gac2990b13b68e831a408fce8e1d0d6445" title="Free an AVFormatContext and all its streams.">avformat_free_context</a>(oc);
<a name="l00668"></a>00668
<a name="l00669"></a>00669 <span class="keywordflow">return</span> 0;
<a name="l00670"></a>00670 }
</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>