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/decoding_encoding.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/decoding_encoding.c</div> </div>
</div><!--header-->
<div class="contents">
<a href="decoding__encoding_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) 2001 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"> * libavcodec API use example.</span>
<a name="l00026"></a>00026 <span class="comment"> *</span>
<a name="l00027"></a>00027 <span class="comment"> * @example decoding_encoding.c</span>
<a name="l00028"></a>00028 <span class="comment"> * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),</span>
<a name="l00029"></a>00029 <span class="comment"> * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the</span>
<a name="l00030"></a>00030 <span class="comment"> * format handling</span>
<a name="l00031"></a>00031 <span class="comment"> */</span>
<a name="l00032"></a>00032
<a name="l00033"></a>00033 <span class="preprocessor">#include <math.h></span>
<a name="l00034"></a>00034
<a name="l00035"></a>00035 <span class="preprocessor">#include <<a class="code" href="opt_8h.html" title="AVOptions.">libavutil/opt.h</a>></span>
<a name="l00036"></a>00036 <span class="preprocessor">#include <<a class="code" href="libavcodec_2avcodec_8h.html" title="Libavcodec external API header.">libavcodec/avcodec.h</a>></span>
<a name="l00037"></a>00037 <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="l00038"></a>00038 <span class="preprocessor">#include <<a class="code" href="common_8h.html" title="common internal and external API header">libavutil/common.h</a>></span>
<a name="l00039"></a>00039 <span class="preprocessor">#include <<a class="code" href="imgutils_8h.html" title="misc image utilities">libavutil/imgutils.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="samplefmt_8h.html">libavutil/samplefmt.h</a>></span>
<a name="l00042"></a>00042
<a name="l00043"></a><a class="code" href="decoding__encoding_8c.html#a6378f14810330164b7fb66f3334b2a27">00043</a> <span class="preprocessor">#define INBUF_SIZE 4096</span>
<a name="l00044"></a><a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">00044</a> <span class="preprocessor"></span><span class="preprocessor">#define AUDIO_INBUF_SIZE 20480</span>
<a name="l00045"></a><a class="code" href="decoding__encoding_8c.html#aebbadad5d16117f57006e8358ebf1ccc">00045</a> <span class="preprocessor"></span><span class="preprocessor">#define AUDIO_REFILL_THRESH 4096</span>
<a name="l00046"></a>00046 <span class="preprocessor"></span>
<a name="l00047"></a>00047 <span class="comment">/* check that a given sample format is supported by the encoder */</span>
<a name="l00048"></a><a class="code" href="decoding__encoding_8c.html#a93df45821682fdbabbc0092abff0bd61">00048</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="decoding__encoding_8c.html#a93df45821682fdbabbc0092abff0bd61">check_sample_fmt</a>(<a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec, <span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> sample_fmt)
<a name="l00049"></a>00049 {
<a name="l00050"></a>00050 <span class="keyword">const</span> <span class="keyword">enum</span> <a class="code" href="group__lavu__sampfmts.html#gaf9a51ca15301871723577c730b5865c5" title="Audio sample formats.">AVSampleFormat</a> *p = codec-><a class="code" href="structAVCodec.html#aac19f4c45370f715412ad5c7b78daacf" title="array of supported sample formats, or NULL if unknown, array is terminated by -1">sample_fmts</a>;
<a name="l00051"></a>00051
<a name="l00052"></a>00052 <span class="keywordflow">while</span> (*p != <a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5ac5d4bb5b0fb0dc7316fb3c7a73029912">AV_SAMPLE_FMT_NONE</a>) {
<a name="l00053"></a>00053 <span class="keywordflow">if</span> (*p == sample_fmt)
<a name="l00054"></a>00054 <span class="keywordflow">return</span> 1;
<a name="l00055"></a>00055 p++;
<a name="l00056"></a>00056 }
<a name="l00057"></a>00057 <span class="keywordflow">return</span> 0;
<a name="l00058"></a>00058 }
<a name="l00059"></a>00059
<a name="l00060"></a>00060 <span class="comment">/* just pick the highest supported samplerate */</span>
<a name="l00061"></a><a class="code" href="decoding__encoding_8c.html#a5809ff9edcce25b26466e3c806bf9854">00061</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="decoding__encoding_8c.html#a5809ff9edcce25b26466e3c806bf9854">select_sample_rate</a>(<a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec)
<a name="l00062"></a>00062 {
<a name="l00063"></a>00063 <span class="keyword">const</span> <span class="keywordtype">int</span> *p;
<a name="l00064"></a>00064 <span class="keywordtype">int</span> best_samplerate = 0;
<a name="l00065"></a>00065
<a name="l00066"></a>00066 <span class="keywordflow">if</span> (!codec-><a class="code" href="structAVCodec.html#aee7a194f1c3251958d3dabed3fa8148e" title="array of supported audio samplerates, or NULL if unknown, array is terminated by 0">supported_samplerates</a>)
<a name="l00067"></a>00067 <span class="keywordflow">return</span> 44100;
<a name="l00068"></a>00068
<a name="l00069"></a>00069 p = codec-><a class="code" href="structAVCodec.html#aee7a194f1c3251958d3dabed3fa8148e" title="array of supported audio samplerates, or NULL if unknown, array is terminated by 0">supported_samplerates</a>;
<a name="l00070"></a>00070 <span class="keywordflow">while</span> (*p) {
<a name="l00071"></a>00071 best_samplerate = <a class="code" href="common_8h.html#a9960bd6780a9b16a216a57be1ee04359">FFMAX</a>(*p, best_samplerate);
<a name="l00072"></a>00072 p++;
<a name="l00073"></a>00073 }
<a name="l00074"></a>00074 <span class="keywordflow">return</span> best_samplerate;
<a name="l00075"></a>00075 }
<a name="l00076"></a>00076
<a name="l00077"></a>00077 <span class="comment">/* select layout with the highest channel count */</span>
<a name="l00078"></a><a class="code" href="decoding__encoding_8c.html#ae6bb0fc8f05c78c13e9d38605038d994">00078</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="decoding__encoding_8c.html#ae6bb0fc8f05c78c13e9d38605038d994">select_channel_layout</a>(<a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec)
<a name="l00079"></a>00079 {
<a name="l00080"></a>00080 <span class="keyword">const</span> uint64_t *p;
<a name="l00081"></a>00081 uint64_t best_ch_layout = 0;
<a name="l00082"></a>00082 <span class="keywordtype">int</span> best_nb_channels = 0;
<a name="l00083"></a>00083
<a name="l00084"></a>00084 <span class="keywordflow">if</span> (!codec-><a class="code" href="structAVCodec.html#a1d4c5152626bbd1f575a3e0014cb59e7" title="array of support channel layouts, or NULL if unknown. array is terminated by 0">channel_layouts</a>)
<a name="l00085"></a>00085 <span class="keywordflow">return</span> <a class="code" href="group__channel__mask__c.html#gabc6d6651bba254cd0fa2c42a57228e65">AV_CH_LAYOUT_STEREO</a>;
<a name="l00086"></a>00086
<a name="l00087"></a>00087 p = codec-><a class="code" href="structAVCodec.html#a1d4c5152626bbd1f575a3e0014cb59e7" title="array of support channel layouts, or NULL if unknown. array is terminated by 0">channel_layouts</a>;
<a name="l00088"></a>00088 <span class="keywordflow">while</span> (*p) {
<a name="l00089"></a>00089 <span class="keywordtype">int</span> nb_channels = <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>(*p);
<a name="l00090"></a>00090
<a name="l00091"></a>00091 <span class="keywordflow">if</span> (nb_channels > best_nb_channels) {
<a name="l00092"></a>00092 best_ch_layout = *p;
<a name="l00093"></a>00093 best_nb_channels = nb_channels;
<a name="l00094"></a>00094 }
<a name="l00095"></a>00095 p++;
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 <span class="keywordflow">return</span> best_ch_layout;
<a name="l00098"></a>00098 }
<a name="l00099"></a>00099
<a name="l00100"></a>00100 <span class="comment">/*</span>
<a name="l00101"></a>00101 <span class="comment"> * Audio encoding example</span>
<a name="l00102"></a>00102 <span class="comment"> */</span>
<a name="l00103"></a><a class="code" href="decoding__encoding_8c.html#a67ded2b27004b72c27d4f3500a578ff6">00103</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="decoding__encoding_8c.html#a67ded2b27004b72c27d4f3500a578ff6">audio_encode_example</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename)
<a name="l00104"></a>00104 {
<a name="l00105"></a>00105 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a name="l00106"></a>00106 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<a name="l00107"></a>00107 <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="l00108"></a>00108 <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="l00109"></a>00109 <span class="keywordtype">int</span> i, j, k, ret, got_output;
<a name="l00110"></a>00110 <span class="keywordtype">int</span> buffer_size;
<a name="l00111"></a>00111 FILE *f;
<a name="l00112"></a>00112 uint16_t *samples;
<a name="l00113"></a>00113 <span class="keywordtype">float</span> t, tincr;
<a name="l00114"></a>00114
<a name="l00115"></a>00115 printf(<span class="stringliteral">"Encode audio file %s\n"</span>, filename);
<a name="l00116"></a>00116
<a name="l00117"></a>00117 <span class="comment">/* find the MP2 encoder */</span>
<a name="l00118"></a>00118 codec = <a class="code" href="group__lavc__encoding.html#ga9f820c481615c3a02d0407bac0bdbf25" title="Find a registered encoder with a matching codec ID.">avcodec_find_encoder</a>(AV_CODEC_ID_MP2);
<a name="l00119"></a>00119 <span class="keywordflow">if</span> (!codec) {
<a name="l00120"></a>00120 fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
<a name="l00121"></a>00121 exit(1);
<a name="l00122"></a>00122 }
<a name="l00123"></a>00123
<a name="l00124"></a>00124 c = <a class="code" href="group__lavc__core.html#gae80afec6f26df6607eaacf39b561c315" title="Allocate an AVCodecContext and set its fields to default values.">avcodec_alloc_context3</a>(codec);
<a name="l00125"></a>00125 <span class="keywordflow">if</span> (!c) {
<a name="l00126"></a>00126 fprintf(stderr, <span class="stringliteral">"Could not allocate audio codec context\n"</span>);
<a name="l00127"></a>00127 exit(1);
<a name="l00128"></a>00128 }
<a name="l00129"></a>00129
<a name="l00130"></a>00130 <span class="comment">/* put sample parameters */</span>
<a name="l00131"></a>00131 c-><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 64000;
<a name="l00132"></a>00132
<a name="l00133"></a>00133 <span class="comment">/* check that the encoder supports s16 pcm input */</span>
<a name="l00134"></a>00134 c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a> = <a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5aea6132df57aebc3f76e10665395c46af" title="signed 16 bits">AV_SAMPLE_FMT_S16</a>;
<a name="l00135"></a>00135 <span class="keywordflow">if</span> (!<a class="code" href="decoding__encoding_8c.html#a93df45821682fdbabbc0092abff0bd61">check_sample_fmt</a>(codec, c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>)) {
<a name="l00136"></a>00136 fprintf(stderr, <span class="stringliteral">"Encoder does not support sample format %s"</span>,
<a name="l00137"></a>00137 <a class="code" href="group__lavu__sampfmts.html#ga31b9d149b2de9821a65f4f5612970838" title="Return the name of sample_fmt, or NULL if sample_fmt is not recognized.">av_get_sample_fmt_name</a>(c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>));
<a name="l00138"></a>00138 exit(1);
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140
<a name="l00141"></a>00141 <span class="comment">/* select other audio parameters supported by the encoder */</span>
<a name="l00142"></a>00142 c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a> = <a class="code" href="decoding__encoding_8c.html#a5809ff9edcce25b26466e3c806bf9854">select_sample_rate</a>(codec);
<a name="l00143"></a>00143 c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a> = <a class="code" href="decoding__encoding_8c.html#ae6bb0fc8f05c78c13e9d38605038d994">select_channel_layout</a>(codec);
<a name="l00144"></a>00144 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="l00145"></a>00145
<a name="l00146"></a>00146 <span class="comment">/* open it */</span>
<a name="l00147"></a>00147 <span class="keywordflow">if</span> (<a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, NULL) < 0) {
<a name="l00148"></a>00148 fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
<a name="l00149"></a>00149 exit(1);
<a name="l00150"></a>00150 }
<a name="l00151"></a>00151
<a name="l00152"></a>00152 f = fopen(filename, <span class="stringliteral">"wb"</span>);
<a name="l00153"></a>00153 <span class="keywordflow">if</span> (!f) {
<a name="l00154"></a>00154 fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
<a name="l00155"></a>00155 exit(1);
<a name="l00156"></a>00156 }
<a name="l00157"></a>00157
<a name="l00158"></a>00158 <span class="comment">/* frame containing input raw audio */</span>
<a name="l00159"></a>00159 frame = <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="l00160"></a>00160 <span class="keywordflow">if</span> (!frame) {
<a name="l00161"></a>00161 fprintf(stderr, <span class="stringliteral">"Could not allocate audio frame\n"</span>);
<a name="l00162"></a>00162 exit(1);
<a name="l00163"></a>00163 }
<a name="l00164"></a>00164
<a name="l00165"></a>00165 frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a> = c-><a class="code" href="structAVCodecContext.html#aec57f0d859a6df8b479cd93ca3a44a33" title="Number of samples per channel in an audio frame.">frame_size</a>;
<a name="l00166"></a>00166 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> = c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>;
<a name="l00167"></a>00167 frame-><a class="code" href="structAVFrame.html#a5f343e0325e3e9d9ed20e34c9e156aef" title="Channel layout of the audio data.">channel_layout</a> = c-><a class="code" href="structAVCodecContext.html#aeb08c575a79eb84fc4155dda88f46c06" title="Audio channel layout.">channel_layout</a>;
<a name="l00168"></a>00168
<a name="l00169"></a>00169 <span class="comment">/* the codec gives us the frame size, in samples,</span>
<a name="l00170"></a>00170 <span class="comment"> * we calculate the size of the samples buffer in bytes */</span>
<a name="l00171"></a>00171 buffer_size = <a class="code" href="group__lavu__sampfmts.html#gaa7368bc4e3a366b688e81938ed55eb06" title="Get the required buffer size for the given audio parameters.">av_samples_get_buffer_size</a>(NULL, c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>, c-><a class="code" href="structAVCodecContext.html#aec57f0d859a6df8b479cd93ca3a44a33" title="Number of samples per channel in an audio frame.">frame_size</a>,
<a name="l00172"></a>00172 c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>, 0);
<a name="l00173"></a>00173 <span class="keywordflow">if</span> (buffer_size < 0) {
<a name="l00174"></a>00174 fprintf(stderr, <span class="stringliteral">"Could not get sample buffer size\n"</span>);
<a name="l00175"></a>00175 exit(1);
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177 samples = <a class="code" href="group__lavu__mem.html#ga9722446c5e310ffedfaac9489864796d" title="Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...">av_malloc</a>(buffer_size);
<a name="l00178"></a>00178 <span class="keywordflow">if</span> (!samples) {
<a name="l00179"></a>00179 fprintf(stderr, <span class="stringliteral">"Could not allocate %d bytes for samples buffer\n"</span>,
<a name="l00180"></a>00180 buffer_size);
<a name="l00181"></a>00181 exit(1);
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183 <span class="comment">/* setup the data pointers in the AVFrame */</span>
<a name="l00184"></a>00184 ret = <a class="code" href="group__lavc__misc.html#ga66b44aad56fa22a78fb8916eb8cc0516" title="Fill AVFrame audio data and linesize pointers.">avcodec_fill_audio_frame</a>(frame, c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>, c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>,
<a name="l00185"></a>00185 (<span class="keyword">const</span> uint8_t*)samples, buffer_size, 0);
<a name="l00186"></a>00186 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00187"></a>00187 fprintf(stderr, <span class="stringliteral">"Could not setup audio frame\n"</span>);
<a name="l00188"></a>00188 exit(1);
<a name="l00189"></a>00189 }
<a name="l00190"></a>00190
<a name="l00191"></a>00191 <span class="comment">/* encode a single tone sound */</span>
<a name="l00192"></a>00192 t = 0;
<a name="l00193"></a>00193 tincr = 2 * M_PI * 440.0 / c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>;
<a name="l00194"></a>00194 <span class="keywordflow">for</span> (i = 0; i < 200; i++) {
<a name="l00195"></a>00195 <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="l00196"></a>00196 pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL; <span class="comment">// packet data will be allocated by the encoder</span>
<a name="l00197"></a>00197 pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<a name="l00198"></a>00198
<a name="l00199"></a>00199 <span class="keywordflow">for</span> (j = 0; j < c-><a class="code" href="structAVCodecContext.html#aec57f0d859a6df8b479cd93ca3a44a33" title="Number of samples per channel in an audio frame.">frame_size</a>; j++) {
<a name="l00200"></a>00200 samples[2*j] = (int)(sin(t) * 10000);
<a name="l00201"></a>00201
<a name="l00202"></a>00202 <span class="keywordflow">for</span> (k = 1; k < c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>; k++)
<a name="l00203"></a>00203 samples[2*j + k] = samples[2*j];
<a name="l00204"></a>00204 t += tincr;
<a name="l00205"></a>00205 }
<a name="l00206"></a>00206 <span class="comment">/* encode the samples */</span>
<a name="l00207"></a>00207 ret = <a class="code" href="group__lavc__encoding.html#ga93a49fbd0973b216dcb8a8c5dffe1d82" title="Encode a frame of audio.">avcodec_encode_audio2</a>(c, &pkt, frame, &got_output);
<a name="l00208"></a>00208 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00209"></a>00209 fprintf(stderr, <span class="stringliteral">"Error encoding audio frame\n"</span>);
<a name="l00210"></a>00210 exit(1);
<a name="l00211"></a>00211 }
<a name="l00212"></a>00212 <span class="keywordflow">if</span> (got_output) {
<a name="l00213"></a>00213 fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a name="l00214"></a>00214 <a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
<a name="l00215"></a>00215 }
<a name="l00216"></a>00216 }
<a name="l00217"></a>00217
<a name="l00218"></a>00218 <span class="comment">/* get the delayed frames */</span>
<a name="l00219"></a>00219 <span class="keywordflow">for</span> (got_output = 1; got_output; i++) {
<a name="l00220"></a>00220 ret = <a class="code" href="group__lavc__encoding.html#ga93a49fbd0973b216dcb8a8c5dffe1d82" title="Encode a frame of audio.">avcodec_encode_audio2</a>(c, &pkt, NULL, &got_output);
<a name="l00221"></a>00221 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00222"></a>00222 fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
<a name="l00223"></a>00223 exit(1);
<a name="l00224"></a>00224 }
<a name="l00225"></a>00225
<a name="l00226"></a>00226 <span class="keywordflow">if</span> (got_output) {
<a name="l00227"></a>00227 fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a name="l00228"></a>00228 <a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
<a name="l00229"></a>00229 }
<a name="l00230"></a>00230 }
<a name="l00231"></a>00231 fclose(f);
<a name="l00232"></a>00232
<a name="l00233"></a>00233 <a class="code" href="group__lavu__mem.html#ga0cc84043ea2167ad005c86e11d0bcdba" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...">av_freep</a>(&samples);
<a name="l00234"></a>00234 <a class="code" href="group__lavu__frame.html#ga979d73f3228814aee56aeca0636e37cc" title="Free the frame and any dynamically allocated objects in it, e.g.">av_frame_free</a>(&frame);
<a name="l00235"></a>00235 <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>(c);
<a name="l00236"></a>00236 <a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
<a name="l00237"></a>00237 }
<a name="l00238"></a>00238
<a name="l00239"></a>00239 <span class="comment">/*</span>
<a name="l00240"></a>00240 <span class="comment"> * Audio decoding.</span>
<a name="l00241"></a>00241 <span class="comment"> */</span>
<a name="l00242"></a><a class="code" href="decoding__encoding_8c.html#a5cbf8bd3441619622d3956d623d944b3">00242</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="decoding__encoding_8c.html#a5cbf8bd3441619622d3956d623d944b3">audio_decode_example</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *outfilename, <span class="keyword">const</span> <span class="keywordtype">char</span> *filename)
<a name="l00243"></a>00243 {
<a name="l00244"></a>00244 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a name="l00245"></a>00245 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<a name="l00246"></a>00246 <span class="keywordtype">int</span> len;
<a name="l00247"></a>00247 FILE *f, *outfile;
<a name="l00248"></a>00248 uint8_t inbuf[<a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">AUDIO_INBUF_SIZE</a> + <a class="code" href="group__lavc__decoding.html#ga9207dd7cb4ee4416f8fd1f7a63af34b7" title="Required number of additionally allocated bytes at the end of the input bitstream for decoding...">FF_INPUT_BUFFER_PADDING_SIZE</a>];
<a name="l00249"></a>00249 <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> avpkt;
<a name="l00250"></a>00250 <a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *decoded_frame = NULL;
<a name="l00251"></a>00251
<a name="l00252"></a>00252 <a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&avpkt);
<a name="l00253"></a>00253
<a name="l00254"></a>00254 printf(<span class="stringliteral">"Decode audio file %s to %s\n"</span>, filename, outfilename);
<a name="l00255"></a>00255
<a name="l00256"></a>00256 <span class="comment">/* find the mpeg audio decoder */</span>
<a name="l00257"></a>00257 codec = <a class="code" href="group__lavc__decoding.html#ga19a0ca553277f019dd5b0fec6e1f9dca" title="Find a registered decoder with a matching codec ID.">avcodec_find_decoder</a>(AV_CODEC_ID_MP2);
<a name="l00258"></a>00258 <span class="keywordflow">if</span> (!codec) {
<a name="l00259"></a>00259 fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
<a name="l00260"></a>00260 exit(1);
<a name="l00261"></a>00261 }
<a name="l00262"></a>00262
<a name="l00263"></a>00263 c = <a class="code" href="group__lavc__core.html#gae80afec6f26df6607eaacf39b561c315" title="Allocate an AVCodecContext and set its fields to default values.">avcodec_alloc_context3</a>(codec);
<a name="l00264"></a>00264 <span class="keywordflow">if</span> (!c) {
<a name="l00265"></a>00265 fprintf(stderr, <span class="stringliteral">"Could not allocate audio codec context\n"</span>);
<a name="l00266"></a>00266 exit(1);
<a name="l00267"></a>00267 }
<a name="l00268"></a>00268
<a name="l00269"></a>00269 <span class="comment">/* open it */</span>
<a name="l00270"></a>00270 <span class="keywordflow">if</span> (<a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, NULL) < 0) {
<a name="l00271"></a>00271 fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
<a name="l00272"></a>00272 exit(1);
<a name="l00273"></a>00273 }
<a name="l00274"></a>00274
<a name="l00275"></a>00275 f = fopen(filename, <span class="stringliteral">"rb"</span>);
<a name="l00276"></a>00276 <span class="keywordflow">if</span> (!f) {
<a name="l00277"></a>00277 fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
<a name="l00278"></a>00278 exit(1);
<a name="l00279"></a>00279 }
<a name="l00280"></a>00280 outfile = fopen(outfilename, <span class="stringliteral">"wb"</span>);
<a name="l00281"></a>00281 <span class="keywordflow">if</span> (!outfile) {
<a name="l00282"></a>00282 <a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
<a name="l00283"></a>00283 exit(1);
<a name="l00284"></a>00284 }
<a name="l00285"></a>00285
<a name="l00286"></a>00286 <span class="comment">/* decode until eof */</span>
<a name="l00287"></a>00287 avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
<a name="l00288"></a>00288 avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = fread(inbuf, 1, <a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">AUDIO_INBUF_SIZE</a>, f);
<a name="l00289"></a>00289
<a name="l00290"></a>00290 <span class="keywordflow">while</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> > 0) {
<a name="l00291"></a>00291 <span class="keywordtype">int</span> i, ch;
<a name="l00292"></a>00292 <span class="keywordtype">int</span> got_frame = 0;
<a name="l00293"></a>00293
<a name="l00294"></a>00294 <span class="keywordflow">if</span> (!decoded_frame) {
<a name="l00295"></a>00295 <span class="keywordflow">if</span> (!(decoded_frame = <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="l00296"></a>00296 fprintf(stderr, <span class="stringliteral">"Could not allocate audio frame\n"</span>);
<a name="l00297"></a>00297 exit(1);
<a name="l00298"></a>00298 }
<a name="l00299"></a>00299 }
<a name="l00300"></a>00300
<a name="l00301"></a>00301 len = <a class="code" href="group__lavc__decoding.html#ga834bb1b062fbcc2de4cf7fb93f154a3e" title="Decode the audio frame of size avpkt->size from avpkt->data into frame.">avcodec_decode_audio4</a>(c, decoded_frame, &got_frame, &avpkt);
<a name="l00302"></a>00302 <span class="keywordflow">if</span> (len < 0) {
<a name="l00303"></a>00303 fprintf(stderr, <span class="stringliteral">"Error while decoding\n"</span>);
<a name="l00304"></a>00304 exit(1);
<a name="l00305"></a>00305 }
<a name="l00306"></a>00306 <span class="keywordflow">if</span> (got_frame) {
<a name="l00307"></a>00307 <span class="comment">/* if a frame has been decoded, output it */</span>
<a name="l00308"></a>00308 <span class="keywordtype">int</span> data_size = <a class="code" href="group__lavu__sampfmts.html#ga0c3c218e1dd570ad4917c69a35a6c77d" title="Return number of bytes per sample.">av_get_bytes_per_sample</a>(c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>);
<a name="l00309"></a>00309 <span class="keywordflow">if</span> (data_size < 0) {
<a name="l00310"></a>00310 <span class="comment">/* This should not occur, checking just for paranoia */</span>
<a name="l00311"></a>00311 fprintf(stderr, <span class="stringliteral">"Failed to calculate data size\n"</span>);
<a name="l00312"></a>00312 exit(1);
<a name="l00313"></a>00313 }
<a name="l00314"></a>00314 <span class="keywordflow">for</span> (i=0; i<decoded_frame-><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a>; i++)
<a name="l00315"></a>00315 <span class="keywordflow">for</span> (ch=0; ch<c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>; ch++)
<a name="l00316"></a>00316 fwrite(decoded_frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[ch] + data_size*i, 1, data_size, outfile);
<a name="l00317"></a>00317 }
<a name="l00318"></a>00318 avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> -= len;
<a name="l00319"></a>00319 avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> += len;
<a name="l00320"></a>00320 avpkt.<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 name="l00321"></a>00321 avpkt.<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="group__lavu__time.html#ga2eaefe702f95f619ea6f2d08afa01be1" title="Undefined timestamp value.">AV_NOPTS_VALUE</a>;
<a name="l00322"></a>00322 <span class="keywordflow">if</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> < <a class="code" href="decoding__encoding_8c.html#aebbadad5d16117f57006e8358ebf1ccc">AUDIO_REFILL_THRESH</a>) {
<a name="l00323"></a>00323 <span class="comment">/* Refill the input buffer, to avoid trying to decode</span>
<a name="l00324"></a>00324 <span class="comment"> * incomplete frames. Instead of this, one could also use</span>
<a name="l00325"></a>00325 <span class="comment"> * a parser, or use a proper container format through</span>
<a name="l00326"></a>00326 <span class="comment"> * libavformat. */</span>
<a name="l00327"></a>00327 memmove(inbuf, avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
<a name="l00328"></a>00328 avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
<a name="l00329"></a>00329 len = fread(avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> + avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, 1,
<a name="l00330"></a>00330 <a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">AUDIO_INBUF_SIZE</a> - avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a name="l00331"></a>00331 <span class="keywordflow">if</span> (len > 0)
<a name="l00332"></a>00332 avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> += len;
<a name="l00333"></a>00333 }
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335
<a name="l00336"></a>00336 fclose(outfile);
<a name="l00337"></a>00337 fclose(f);
<a name="l00338"></a>00338
<a name="l00339"></a>00339 <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>(c);
<a name="l00340"></a>00340 <a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
<a name="l00341"></a>00341 <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>(&decoded_frame);
<a name="l00342"></a>00342 }
<a name="l00343"></a>00343
<a name="l00344"></a>00344 <span class="comment">/*</span>
<a name="l00345"></a>00345 <span class="comment"> * Video encoding example</span>
<a name="l00346"></a>00346 <span class="comment"> */</span>
<a name="l00347"></a><a class="code" href="decoding__encoding_8c.html#a7293f14f6e6315d54331137ae883079f">00347</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="decoding__encoding_8c.html#a7293f14f6e6315d54331137ae883079f">video_encode_example</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename, <span class="keywordtype">int</span> codec_id)
<a name="l00348"></a>00348 {
<a name="l00349"></a>00349 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a name="l00350"></a>00350 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<a name="l00351"></a>00351 <span class="keywordtype">int</span> i, ret, x, y, got_output;
<a name="l00352"></a>00352 FILE *f;
<a name="l00353"></a>00353 <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="l00354"></a>00354 <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="l00355"></a>00355 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
<a name="l00356"></a>00356
<a name="l00357"></a>00357 printf(<span class="stringliteral">"Encode video file %s\n"</span>, filename);
<a name="l00358"></a>00358
<a name="l00359"></a>00359 <span class="comment">/* find the mpeg1 video encoder */</span>
<a name="l00360"></a>00360 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="l00361"></a>00361 <span class="keywordflow">if</span> (!codec) {
<a name="l00362"></a>00362 fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
<a name="l00363"></a>00363 exit(1);
<a name="l00364"></a>00364 }
<a name="l00365"></a>00365
<a name="l00366"></a>00366 c = <a class="code" href="group__lavc__core.html#gae80afec6f26df6607eaacf39b561c315" title="Allocate an AVCodecContext and set its fields to default values.">avcodec_alloc_context3</a>(codec);
<a name="l00367"></a>00367 <span class="keywordflow">if</span> (!c) {
<a name="l00368"></a>00368 fprintf(stderr, <span class="stringliteral">"Could not allocate video codec context\n"</span>);
<a name="l00369"></a>00369 exit(1);
<a name="l00370"></a>00370 }
<a name="l00371"></a>00371
<a name="l00372"></a>00372 <span class="comment">/* put sample parameters */</span>
<a name="l00373"></a>00373 c-><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 400000;
<a name="l00374"></a>00374 <span class="comment">/* resolution must be a multiple of two */</span>
<a name="l00375"></a>00375 c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a> = 352;
<a name="l00376"></a>00376 c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a> = 288;
<a name="l00377"></a>00377 <span class="comment">/* frames per second */</span>
<a name="l00378"></a>00378 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 class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){1,25};
<a name="l00379"></a>00379 <span class="comment">/* emit one intra frame every ten frames</span>
<a name="l00380"></a>00380 <span class="comment"> * check frame pict_type before passing frame</span>
<a name="l00381"></a>00381 <span class="comment"> * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I</span>
<a name="l00382"></a>00382 <span class="comment"> * then gop_size is ignored and the output of encoder</span>
<a name="l00383"></a>00383 <span class="comment"> * will always be I frame irrespective to gop_size</span>
<a name="l00384"></a>00384 <span class="comment"> */</span>
<a name="l00385"></a>00385 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> = 10;
<a name="l00386"></a>00386 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> = 1;
<a name="l00387"></a>00387 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="l00388"></a>00388
<a name="l00389"></a>00389 <span class="keywordflow">if</span> (codec_id == <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7cea3d4c051c8b18a3c18750676a308c10be">AV_CODEC_ID_H264</a>)
<a name="l00390"></a>00390 <a class="code" href="group__opt__set__funcs.html#ga5fd4b92bdf4f392a2847f711676a7537">av_opt_set</a>(c-><a class="code" href="structAVCodecContext.html#af3379123060ad8cc9c321c29af4f8360">priv_data</a>, <span class="stringliteral">"preset"</span>, <span class="stringliteral">"slow"</span>, 0);
<a name="l00391"></a>00391
<a name="l00392"></a>00392 <span class="comment">/* open it */</span>
<a name="l00393"></a>00393 <span class="keywordflow">if</span> (<a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, NULL) < 0) {
<a name="l00394"></a>00394 fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
<a name="l00395"></a>00395 exit(1);
<a name="l00396"></a>00396 }
<a name="l00397"></a>00397
<a name="l00398"></a>00398 f = fopen(filename, <span class="stringliteral">"wb"</span>);
<a name="l00399"></a>00399 <span class="keywordflow">if</span> (!f) {
<a name="l00400"></a>00400 fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
<a name="l00401"></a>00401 exit(1);
<a name="l00402"></a>00402 }
<a name="l00403"></a>00403
<a name="l00404"></a>00404 frame = <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="l00405"></a>00405 <span class="keywordflow">if</span> (!frame) {
<a name="l00406"></a>00406 fprintf(stderr, <span class="stringliteral">"Could not allocate video frame\n"</span>);
<a name="l00407"></a>00407 exit(1);
<a name="l00408"></a>00408 }
<a name="l00409"></a>00409 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> = c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a>;
<a name="l00410"></a>00410 frame-><a class="code" href="structAVFrame.html#a1e71ce60cedd5f3b6811714a9f7f9e0a" title="width and height of the video frame">width</a> = c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>;
<a name="l00411"></a>00411 frame-><a class="code" href="structAVFrame.html#a3f89733f429c98ba5bc64373fb0a3f13">height</a> = c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>;
<a name="l00412"></a>00412
<a name="l00413"></a>00413 <span class="comment">/* the image can be allocated by any means and av_image_alloc() is</span>
<a name="l00414"></a>00414 <span class="comment"> * just the most convenient way if av_malloc() is to be used */</span>
<a name="l00415"></a>00415 ret = <a class="code" href="group__lavu__picture.html#ga841e0a89a642e24141af1918a2c10448" title="Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...">av_image_alloc</a>(frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>, frame-><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</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="l00416"></a>00416 c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a>, 32);
<a name="l00417"></a>00417 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00418"></a>00418 fprintf(stderr, <span class="stringliteral">"Could not allocate raw picture buffer\n"</span>);
<a name="l00419"></a>00419 exit(1);
<a name="l00420"></a>00420 }
<a name="l00421"></a>00421
<a name="l00422"></a>00422 <span class="comment">/* encode 1 second of video */</span>
<a name="l00423"></a>00423 <span class="keywordflow">for</span> (i = 0; i < 25; i++) {
<a name="l00424"></a>00424 <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="l00425"></a>00425 pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL; <span class="comment">// packet data will be allocated by the encoder</span>
<a name="l00426"></a>00426 pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<a name="l00427"></a>00427
<a name="l00428"></a>00428 fflush(stdout);
<a name="l00429"></a>00429 <span class="comment">/* prepare a dummy image */</span>
<a name="l00430"></a>00430 <span class="comment">/* Y */</span>
<a name="l00431"></a>00431 <span class="keywordflow">for</span> (y = 0; y < c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>; y++) {
<a name="l00432"></a>00432 <span class="keywordflow">for</span> (x = 0; x < c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>; x++) {
<a name="l00433"></a>00433 frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[0][y * frame-><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="l00434"></a>00434 }
<a name="l00435"></a>00435 }
<a name="l00436"></a>00436
<a name="l00437"></a>00437 <span class="comment">/* Cb and Cr */</span>
<a name="l00438"></a>00438 <span class="keywordflow">for</span> (y = 0; y < c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>/2; y++) {
<a name="l00439"></a>00439 <span class="keywordflow">for</span> (x = 0; x < c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>/2; x++) {
<a name="l00440"></a>00440 frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[1][y * frame-><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="l00441"></a>00441 frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[2][y * frame-><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="l00442"></a>00442 }
<a name="l00443"></a>00443 }
<a name="l00444"></a>00444
<a name="l00445"></a>00445 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> = i;
<a name="l00446"></a>00446
<a name="l00447"></a>00447 <span class="comment">/* encode the image */</span>
<a name="l00448"></a>00448 ret = <a class="code" href="group__lavc__encoding.html#gaa2dc9e9ea2567ebb2801a08153c7306b" title="Encode a frame of video.">avcodec_encode_video2</a>(c, &pkt, frame, &got_output);
<a name="l00449"></a>00449 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00450"></a>00450 fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
<a name="l00451"></a>00451 exit(1);
<a name="l00452"></a>00452 }
<a name="l00453"></a>00453
<a name="l00454"></a>00454 <span class="keywordflow">if</span> (got_output) {
<a name="l00455"></a>00455 printf(<span class="stringliteral">"Write frame %3d (size=%5d)\n"</span>, i, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
<a name="l00456"></a>00456 fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a name="l00457"></a>00457 <a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
<a name="l00458"></a>00458 }
<a name="l00459"></a>00459 }
<a name="l00460"></a>00460
<a name="l00461"></a>00461 <span class="comment">/* get the delayed frames */</span>
<a name="l00462"></a>00462 <span class="keywordflow">for</span> (got_output = 1; got_output; i++) {
<a name="l00463"></a>00463 fflush(stdout);
<a name="l00464"></a>00464
<a name="l00465"></a>00465 ret = <a class="code" href="group__lavc__encoding.html#gaa2dc9e9ea2567ebb2801a08153c7306b" title="Encode a frame of video.">avcodec_encode_video2</a>(c, &pkt, NULL, &got_output);
<a name="l00466"></a>00466 <span class="keywordflow">if</span> (ret < 0) {
<a name="l00467"></a>00467 fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
<a name="l00468"></a>00468 exit(1);
<a name="l00469"></a>00469 }
<a name="l00470"></a>00470
<a name="l00471"></a>00471 <span class="keywordflow">if</span> (got_output) {
<a name="l00472"></a>00472 printf(<span class="stringliteral">"Write frame %3d (size=%5d)\n"</span>, i, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
<a name="l00473"></a>00473 fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a name="l00474"></a>00474 <a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
<a name="l00475"></a>00475 }
<a name="l00476"></a>00476 }
<a name="l00477"></a>00477
<a name="l00478"></a>00478 <span class="comment">/* add sequence end code to have a real mpeg file */</span>
<a name="l00479"></a>00479 fwrite(endcode, 1, <span class="keyword">sizeof</span>(endcode), f);
<a name="l00480"></a>00480 fclose(f);
<a name="l00481"></a>00481
<a name="l00482"></a>00482 <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>(c);
<a name="l00483"></a>00483 <a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
<a name="l00484"></a>00484 <a class="code" href="group__lavu__mem.html#ga0cc84043ea2167ad005c86e11d0bcdba" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...">av_freep</a>(&frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[0]);
<a name="l00485"></a>00485 <a class="code" href="group__lavu__frame.html#ga979d73f3228814aee56aeca0636e37cc" title="Free the frame and any dynamically allocated objects in it, e.g.">av_frame_free</a>(&frame);
<a name="l00486"></a>00486 printf(<span class="stringliteral">"\n"</span>);
<a name="l00487"></a>00487 }
<a name="l00488"></a>00488
<a name="l00489"></a>00489 <span class="comment">/*</span>
<a name="l00490"></a>00490 <span class="comment"> * Video decoding example</span>
<a name="l00491"></a>00491 <span class="comment"> */</span>
<a name="l00492"></a>00492
<a name="l00493"></a><a class="code" href="decoding__encoding_8c.html#aea973224783a5595366684c76c0ee06c">00493</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="decoding__encoding_8c.html#aea973224783a5595366684c76c0ee06c">pgm_save</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> wrap, <span class="keywordtype">int</span> xsize, <span class="keywordtype">int</span> ysize,
<a name="l00494"></a>00494 <span class="keywordtype">char</span> *filename)
<a name="l00495"></a>00495 {
<a name="l00496"></a>00496 FILE *f;
<a name="l00497"></a>00497 <span class="keywordtype">int</span> i;
<a name="l00498"></a>00498
<a name="l00499"></a>00499 f = fopen(filename,<span class="stringliteral">"w"</span>);
<a name="l00500"></a>00500 fprintf(f, <span class="stringliteral">"P5\n%d %d\n%d\n"</span>, xsize, ysize, 255);
<a name="l00501"></a>00501 <span class="keywordflow">for</span> (i = 0; i < ysize; i++)
<a name="l00502"></a>00502 fwrite(buf + i * wrap, 1, xsize, f);
<a name="l00503"></a>00503 fclose(f);
<a name="l00504"></a>00504 }
<a name="l00505"></a>00505
<a name="l00506"></a><a class="code" href="decoding__encoding_8c.html#a1d01e17e5817622d9419f681603f6c8f">00506</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="decoding__encoding_8c.html#a1d01e17e5817622d9419f681603f6c8f">decode_write_frame</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *outfilename, <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *avctx,
<a name="l00507"></a>00507 <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>, <span class="keywordtype">int</span> *frame_count, <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>, <span class="keywordtype">int</span> last)
<a name="l00508"></a>00508 {
<a name="l00509"></a>00509 <span class="keywordtype">int</span> len, got_frame;
<a name="l00510"></a>00510 <span class="keywordtype">char</span> buf[1024];
<a name="l00511"></a>00511
<a name="l00512"></a>00512 len = <a class="code" href="group__lavc__decoding.html#ga99ee61b6dcffb7817a275d39da58cc74" title="Decode the video frame of size avpkt->size from avpkt->data into picture.">avcodec_decode_video2</a>(avctx, frame, &got_frame, pkt);
<a name="l00513"></a>00513 <span class="keywordflow">if</span> (len < 0) {
<a name="l00514"></a>00514 fprintf(stderr, <span class="stringliteral">"Error while decoding frame %d\n"</span>, *frame_count);
<a name="l00515"></a>00515 <span class="keywordflow">return</span> len;
<a name="l00516"></a>00516 }
<a name="l00517"></a>00517 <span class="keywordflow">if</span> (got_frame) {
<a name="l00518"></a>00518 printf(<span class="stringliteral">"Saving %sframe %3d\n"</span>, last ? <span class="stringliteral">"last "</span> : <span class="stringliteral">""</span>, *frame_count);
<a name="l00519"></a>00519 fflush(stdout);
<a name="l00520"></a>00520
<a name="l00521"></a>00521 <span class="comment">/* the picture is allocated by the decoder, no need to free it */</span>
<a name="l00522"></a>00522 snprintf(buf, <span class="keyword">sizeof</span>(buf), outfilename, *frame_count);
<a name="l00523"></a>00523 <a class="code" href="decoding__encoding_8c.html#aea973224783a5595366684c76c0ee06c">pgm_save</a>(frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[0], frame-><a class="code" href="structAVFrame.html#aa52bfc6605f6a3059a0c3226cc0f6567" title="For video, size in bytes of each picture line.">linesize</a>[0],
<a name="l00524"></a>00524 avctx-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>, avctx-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>, buf);
<a name="l00525"></a>00525 (*frame_count)++;
<a name="l00526"></a>00526 }
<a name="l00527"></a>00527 <span class="keywordflow">if</span> (pkt-><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>) {
<a name="l00528"></a>00528 pkt-><a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> -= len;
<a name="l00529"></a>00529 pkt-><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> += len;
<a name="l00530"></a>00530 }
<a name="l00531"></a>00531 <span class="keywordflow">return</span> 0;
<a name="l00532"></a>00532 }
<a name="l00533"></a>00533
<a name="l00534"></a><a class="code" href="decoding__encoding_8c.html#a8b416e073a13f42326e97a86ce283db1">00534</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="decoding__encoding_8c.html#a8b416e073a13f42326e97a86ce283db1">video_decode_example</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *outfilename, <span class="keyword">const</span> <span class="keywordtype">char</span> *filename)
<a name="l00535"></a>00535 {
<a name="l00536"></a>00536 <a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a name="l00537"></a>00537 <a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<a name="l00538"></a>00538 <span class="keywordtype">int</span> frame_count;
<a name="l00539"></a>00539 FILE *f;
<a name="l00540"></a>00540 <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="l00541"></a>00541 uint8_t inbuf[<a class="code" href="decoding__encoding_8c.html#a6378f14810330164b7fb66f3334b2a27">INBUF_SIZE</a> + <a class="code" href="group__lavc__decoding.html#ga9207dd7cb4ee4416f8fd1f7a63af34b7" title="Required number of additionally allocated bytes at the end of the input bitstream for decoding...">FF_INPUT_BUFFER_PADDING_SIZE</a>];
<a name="l00542"></a>00542 <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> avpkt;
<a name="l00543"></a>00543
<a name="l00544"></a>00544 <a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&avpkt);
<a name="l00545"></a>00545
<a name="l00546"></a>00546 <span class="comment">/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */</span>
<a name="l00547"></a>00547 memset(inbuf + <a class="code" href="decoding__encoding_8c.html#a6378f14810330164b7fb66f3334b2a27">INBUF_SIZE</a>, 0, <a class="code" href="group__lavc__decoding.html#ga9207dd7cb4ee4416f8fd1f7a63af34b7" title="Required number of additionally allocated bytes at the end of the input bitstream for decoding...">FF_INPUT_BUFFER_PADDING_SIZE</a>);
<a name="l00548"></a>00548
<a name="l00549"></a>00549 printf(<span class="stringliteral">"Decode video file %s to %s\n"</span>, filename, outfilename);
<a name="l00550"></a>00550
<a name="l00551"></a>00551 <span class="comment">/* find the mpeg1 video decoder */</span>
<a name="l00552"></a>00552 codec = <a class="code" href="group__lavc__decoding.html#ga19a0ca553277f019dd5b0fec6e1f9dca" title="Find a registered decoder with a matching codec ID.">avcodec_find_decoder</a>(<a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7ceaf019b13f4891b36ae579cd7bc96d1f78">AV_CODEC_ID_MPEG1VIDEO</a>);
<a name="l00553"></a>00553 <span class="keywordflow">if</span> (!codec) {
<a name="l00554"></a>00554 fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
<a name="l00555"></a>00555 exit(1);
<a name="l00556"></a>00556 }
<a name="l00557"></a>00557
<a name="l00558"></a>00558 c = <a class="code" href="group__lavc__core.html#gae80afec6f26df6607eaacf39b561c315" title="Allocate an AVCodecContext and set its fields to default values.">avcodec_alloc_context3</a>(codec);
<a name="l00559"></a>00559 <span class="keywordflow">if</span> (!c) {
<a name="l00560"></a>00560 fprintf(stderr, <span class="stringliteral">"Could not allocate video codec context\n"</span>);
<a name="l00561"></a>00561 exit(1);
<a name="l00562"></a>00562 }
<a name="l00563"></a>00563
<a name="l00564"></a>00564 <span class="keywordflow">if</span>(codec-><a class="code" href="structAVCodec.html#af51f7ff3dac8b730f46b9713e49a2518" title="Codec capabilities.">capabilities</a>&<a class="code" href="group__lavc__core.html#ga8783bdc2c37a5b8a0862450a482b6816">CODEC_CAP_TRUNCATED</a>)
<a name="l00565"></a>00565 c-><a class="code" href="structAVCodecContext.html#abb01e291550fa3fb96188af4d494587e" title="CODEC_FLAG_*.">flags</a>|= <a class="code" href="group__lavc__core.html#ga3bbd99307dfa5f8a096d646bc9d03bca">CODEC_FLAG_TRUNCATED</a>; <span class="comment">/* we do not send complete frames */</span>
<a name="l00566"></a>00566
<a name="l00567"></a>00567 <span class="comment">/* For some codecs, such as msmpeg4 and mpeg4, width and height</span>
<a name="l00568"></a>00568 <span class="comment"> MUST be initialized there because this information is not</span>
<a name="l00569"></a>00569 <span class="comment"> available in the bitstream. */</span>
<a name="l00570"></a>00570
<a name="l00571"></a>00571 <span class="comment">/* open it */</span>
<a name="l00572"></a>00572 <span class="keywordflow">if</span> (<a class="code" href="group__lavc__core.html#ga11f785a188d7d9df71621001465b0f1d" title="Initialize the AVCodecContext to use the given AVCodec.">avcodec_open2</a>(c, codec, NULL) < 0) {
<a name="l00573"></a>00573 fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
<a name="l00574"></a>00574 exit(1);
<a name="l00575"></a>00575 }
<a name="l00576"></a>00576
<a name="l00577"></a>00577 f = fopen(filename, <span class="stringliteral">"rb"</span>);
<a name="l00578"></a>00578 <span class="keywordflow">if</span> (!f) {
<a name="l00579"></a>00579 fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
<a name="l00580"></a>00580 exit(1);
<a name="l00581"></a>00581 }
<a name="l00582"></a>00582
<a name="l00583"></a>00583 frame = <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="l00584"></a>00584 <span class="keywordflow">if</span> (!frame) {
<a name="l00585"></a>00585 fprintf(stderr, <span class="stringliteral">"Could not allocate video frame\n"</span>);
<a name="l00586"></a>00586 exit(1);
<a name="l00587"></a>00587 }
<a name="l00588"></a>00588
<a name="l00589"></a>00589 frame_count = 0;
<a name="l00590"></a>00590 <span class="keywordflow">for</span> (;;) {
<a name="l00591"></a>00591 avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = fread(inbuf, 1, <a class="code" href="decoding__encoding_8c.html#a6378f14810330164b7fb66f3334b2a27">INBUF_SIZE</a>, f);
<a name="l00592"></a>00592 <span class="keywordflow">if</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> == 0)
<a name="l00593"></a>00593 <span class="keywordflow">break</span>;
<a name="l00594"></a>00594
<a name="l00595"></a>00595 <span class="comment">/* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)</span>
<a name="l00596"></a>00596 <span class="comment"> and this is the only method to use them because you cannot</span>
<a name="l00597"></a>00597 <span class="comment"> know the compressed data size before analysing it.</span>
<a name="l00598"></a>00598 <span class="comment"></span>
<a name="l00599"></a>00599 <span class="comment"> BUT some other codecs (msmpeg4, mpeg4) are inherently frame</span>
<a name="l00600"></a>00600 <span class="comment"> based, so you must call them with all the data for one</span>
<a name="l00601"></a>00601 <span class="comment"> frame exactly. You must also initialize 'width' and</span>
<a name="l00602"></a>00602 <span class="comment"> 'height' before initializing them. */</span>
<a name="l00603"></a>00603
<a name="l00604"></a>00604 <span class="comment">/* NOTE2: some codecs allow the raw parameters (frame size,</span>
<a name="l00605"></a>00605 <span class="comment"> sample rate) to be changed at any frame. We handle this, so</span>
<a name="l00606"></a>00606 <span class="comment"> you should also take care of it */</span>
<a name="l00607"></a>00607
<a name="l00608"></a>00608 <span class="comment">/* here, we use a stream based decoder (mpeg1video), so we</span>
<a name="l00609"></a>00609 <span class="comment"> feed decoder and see if it could decode a frame */</span>
<a name="l00610"></a>00610 avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
<a name="l00611"></a>00611 <span class="keywordflow">while</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> > 0)
<a name="l00612"></a>00612 <span class="keywordflow">if</span> (<a class="code" href="decoding__encoding_8c.html#a1d01e17e5817622d9419f681603f6c8f">decode_write_frame</a>(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
<a name="l00613"></a>00613 exit(1);
<a name="l00614"></a>00614 }
<a name="l00615"></a>00615
<a name="l00616"></a>00616 <span class="comment">/* some codecs, such as MPEG, transmit the I and P frame with a</span>
<a name="l00617"></a>00617 <span class="comment"> latency of one frame. You must do the following to have a</span>
<a name="l00618"></a>00618 <span class="comment"> chance to get the last frame of the video */</span>
<a name="l00619"></a>00619 avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL;
<a name="l00620"></a>00620 avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<a name="l00621"></a>00621 <a class="code" href="decoding__encoding_8c.html#a1d01e17e5817622d9419f681603f6c8f">decode_write_frame</a>(outfilename, c, frame, &frame_count, &avpkt, 1);
<a name="l00622"></a>00622
<a name="l00623"></a>00623 fclose(f);
<a name="l00624"></a>00624
<a name="l00625"></a>00625 <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>(c);
<a name="l00626"></a>00626 <a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
<a name="l00627"></a>00627 <a class="code" href="group__lavu__frame.html#ga979d73f3228814aee56aeca0636e37cc" title="Free the frame and any dynamically allocated objects in it, e.g.">av_frame_free</a>(&frame);
<a name="l00628"></a>00628 printf(<span class="stringliteral">"\n"</span>);
<a name="l00629"></a>00629 }
<a name="l00630"></a>00630
<a name="l00631"></a><a class="code" href="decoding__encoding_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">00631</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="l00632"></a>00632 {
<a name="l00633"></a>00633 <span class="keyword">const</span> <span class="keywordtype">char</span> *output_type;
<a name="l00634"></a>00634
<a name="l00635"></a>00635 <span class="comment">/* register all the codecs */</span>
<a name="l00636"></a>00636 <a class="code" href="group__lavc__core.html#gaf1a2bb4e7c7611c131bb6212bf0fa639" title="Register all the codecs, parsers and bitstream filters which were enabled at configuration time...">avcodec_register_all</a>();
<a name="l00637"></a>00637
<a name="l00638"></a>00638 <span class="keywordflow">if</span> (argc < 2) {
<a name="l00639"></a>00639 printf(<span class="stringliteral">"usage: %s output_type\n"</span>
<a name="l00640"></a>00640 <span class="stringliteral">"API example program to decode/encode a media stream with libavcodec.\n"</span>
<a name="l00641"></a>00641 <span class="stringliteral">"This program generates a synthetic stream and encodes it to a file\n"</span>
<a name="l00642"></a>00642 <span class="stringliteral">"named test.h264, test.mp2 or test.mpg depending on output_type.\n"</span>
<a name="l00643"></a>00643 <span class="stringliteral">"The encoded stream is then decoded and written to a raw data output.\n"</span>
<a name="l00644"></a>00644 <span class="stringliteral">"output_type must be chosen between 'h264', 'mp2', 'mpg'.\n"</span>,
<a name="l00645"></a>00645 argv[0]);
<a name="l00646"></a>00646 <span class="keywordflow">return</span> 1;
<a name="l00647"></a>00647 }
<a name="l00648"></a>00648 output_type = argv[1];
<a name="l00649"></a>00649
<a name="l00650"></a>00650 <span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"h264"</span>)) {
<a name="l00651"></a>00651 <a class="code" href="decoding__encoding_8c.html#a7293f14f6e6315d54331137ae883079f">video_encode_example</a>(<span class="stringliteral">"test.h264"</span>, <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7cea3d4c051c8b18a3c18750676a308c10be">AV_CODEC_ID_H264</a>);
<a name="l00652"></a>00652 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"mp2"</span>)) {
<a name="l00653"></a>00653 <a class="code" href="decoding__encoding_8c.html#a67ded2b27004b72c27d4f3500a578ff6">audio_encode_example</a>(<span class="stringliteral">"test.mp2"</span>);
<a name="l00654"></a>00654 <a class="code" href="decoding__encoding_8c.html#a5cbf8bd3441619622d3956d623d944b3">audio_decode_example</a>(<span class="stringliteral">"test.pcm"</span>, <span class="stringliteral">"test.mp2"</span>);
<a name="l00655"></a>00655 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"mpg"</span>)) {
<a name="l00656"></a>00656 <a class="code" href="decoding__encoding_8c.html#a7293f14f6e6315d54331137ae883079f">video_encode_example</a>(<span class="stringliteral">"test.mpg"</span>, <a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7ceaf019b13f4891b36ae579cd7bc96d1f78">AV_CODEC_ID_MPEG1VIDEO</a>);
<a name="l00657"></a>00657 <a class="code" href="decoding__encoding_8c.html#a8b416e073a13f42326e97a86ce283db1">video_decode_example</a>(<span class="stringliteral">"test%02d.pgm"</span>, <span class="stringliteral">"test.mpg"</span>);
<a name="l00658"></a>00658 } <span class="keywordflow">else</span> {
<a name="l00659"></a>00659 fprintf(stderr, <span class="stringliteral">"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n"</span>,
<a name="l00660"></a>00660 output_type);
<a name="l00661"></a>00661 <span class="keywordflow">return</span> 1;
<a name="l00662"></a>00662 }
<a name="l00663"></a>00663
<a name="l00664"></a>00664 <span class="keywordflow">return</span> 0;
<a name="l00665"></a>00665 }
</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>