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: decoding_encoding.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">FFmpeg
 <span id="projectnumber">2.7.1</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">decoding_encoding.c</div> </div>
</div><!--header-->
<div class="contents">
<p>Note that libavcodec only handles codecs (mpeg, mpeg4, etc...), not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the format handling</p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2001 Fabrice Bellard</span>
<span class="comment"> *</span>
<span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="comment"> * of this software and associated documentation files (the "Software"), to deal</span>
<span class="comment"> * in the Software without restriction, including without limitation the rights</span>
<span class="comment"> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="comment"> * copies of the Software, and to permit persons to whom the Software is</span>
<span class="comment"> * furnished to do so, subject to the following conditions:</span>
<span class="comment"> *</span>
<span class="comment"> * The above copyright notice and this permission notice shall be included in</span>
<span class="comment"> * all copies or substantial portions of the Software.</span>
<span class="comment"> *</span>
<span class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="comment"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</span>
<span class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="comment"> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<span class="comment"> * THE SOFTWARE.</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * @file</span>
<span class="comment"> * libavcodec API use example.</span>
<span class="comment"> *</span>
<span class="comment"> * @example decoding_encoding.c</span>
<span class="comment"> * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),</span>
<span class="comment"> * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the</span>
<span class="comment"> * format handling</span>
<span class="comment"> */</span>
<span class="preprocessor">#include <math.h></span>
<span class="preprocessor">#include <<a class="code" href="opt_8h.html" title="AVOptions.">libavutil/opt.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="libavcodec_2avcodec_8h.html" title="Libavcodec external API header.">libavcodec/avcodec.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="channel__layout_8h.html" title="audio channel layout utility functions">libavutil/channel_layout.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="common_8h.html" title="common internal and external API header">libavutil/common.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="imgutils_8h.html" title="misc image utilities">libavutil/imgutils.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="mathematics_8h.html">libavutil/mathematics.h</a>></span>
<span class="preprocessor">#include <<a class="code" href="samplefmt_8h.html">libavutil/samplefmt.h</a>></span>
<span class="preprocessor">#define INBUF_SIZE 4096</span>
<span class="preprocessor"></span><span class="preprocessor">#define AUDIO_INBUF_SIZE 20480</span>
<span class="preprocessor"></span><span class="preprocessor">#define AUDIO_REFILL_THRESH 4096</span>
<span class="preprocessor"></span>
<span class="comment">/* check that a given sample format is supported by the encoder */</span>
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a0"></a><a class="code" href="decoding__encoding_8c.html#a93df45821682fdbabbc0092abff0bd61">check_sample_fmt</a>(<a name="_a1"></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)
{
<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 name="a2"></a><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>;
<span class="keywordflow">while</span> (*p != <a name="a3"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5ac5d4bb5b0fb0dc7316fb3c7a73029912">AV_SAMPLE_FMT_NONE</a>) {
<span class="keywordflow">if</span> (*p == sample_fmt)
<span class="keywordflow">return</span> 1;
p++;
}
<span class="keywordflow">return</span> 0;
}
<span class="comment">/* just pick the highest supported samplerate */</span>
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a4"></a><a class="code" href="decoding__encoding_8c.html#a5809ff9edcce25b26466e3c806bf9854">select_sample_rate</a>(<a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec)
{
<span class="keyword">const</span> <span class="keywordtype">int</span> *p;
<span class="keywordtype">int</span> best_samplerate = 0;
<span class="keywordflow">if</span> (!codec-><a name="a5"></a><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>)
<span class="keywordflow">return</span> 44100;
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>;
<span class="keywordflow">while</span> (*p) {
best_samplerate = <a name="a6"></a><a class="code" href="common_8h.html#a9960bd6780a9b16a216a57be1ee04359">FFMAX</a>(*p, best_samplerate);
p++;
}
<span class="keywordflow">return</span> best_samplerate;
}
<span class="comment">/* select layout with the highest channel count */</span>
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a7"></a><a class="code" href="decoding__encoding_8c.html#ae6bb0fc8f05c78c13e9d38605038d994">select_channel_layout</a>(<a class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec)
{
<span class="keyword">const</span> uint64_t *p;
uint64_t best_ch_layout = 0;
<span class="keywordtype">int</span> best_nb_channels = 0;
<span class="keywordflow">if</span> (!codec-><a name="a8"></a><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>)
<span class="keywordflow">return</span> <a name="a9"></a><a class="code" href="group__channel__mask__c.html#gabc6d6651bba254cd0fa2c42a57228e65">AV_CH_LAYOUT_STEREO</a>;
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>;
<span class="keywordflow">while</span> (*p) {
<span class="keywordtype">int</span> nb_channels = <a name="a10"></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>(*p);
<span class="keywordflow">if</span> (nb_channels > best_nb_channels) {
best_ch_layout = *p;
best_nb_channels = nb_channels;
}
p++;
}
<span class="keywordflow">return</span> best_ch_layout;
}
<span class="comment">/*</span>
<span class="comment"> * Audio encoding example</span>
<span class="comment"> */</span>
<span class="keyword">static</span> <span class="keywordtype">void</span> <a name="a11"></a><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 class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a name="_a12"></a><a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<a name="_a13"></a><a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *<a name="a14"></a><a class="code" href="demuxing__decoding_8c.html#ad7d33d579a8d4241a5e643e39287a209">frame</a>;
<a name="_a15"></a><a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a name="a16"></a><a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>;
<span class="keywordtype">int</span> i, j, k, ret, got_output;
<span class="keywordtype">int</span> buffer_size;
FILE *f;
uint16_t *samples;
<span class="keywordtype">float</span> t, tincr;
printf(<span class="stringliteral">"Encode audio file %s\n"</span>, filename);
<span class="comment">/* find the MP2 encoder */</span>
codec = <a name="a17"></a><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);
<span class="keywordflow">if</span> (!codec) {
fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
exit(1);
}
c = <a name="a18"></a><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);
<span class="keywordflow">if</span> (!c) {
fprintf(stderr, <span class="stringliteral">"Could not allocate audio codec context\n"</span>);
exit(1);
}
<span class="comment">/* put sample parameters */</span>
c-><a name="a19"></a><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 64000;
<span class="comment">/* check that the encoder supports s16 pcm input */</span>
c-><a name="a20"></a><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a> = <a name="a21"></a><a class="code" href="group__lavu__sampfmts.html#ggaf9a51ca15301871723577c730b5865c5aea6132df57aebc3f76e10665395c46af" title="signed 16 bits">AV_SAMPLE_FMT_S16</a>;
<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>)) {
fprintf(stderr, <span class="stringliteral">"Encoder does not support sample format %s"</span>,
<a name="a22"></a><a class="code" href="group__lavu__sampfmts.html#ga31b9d149b2de9821a65f4f5612970838" title="Return the name of sample_fmt, or NULL if sample_fmt is not recognized.">av_get_sample_fmt_name</a>(c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>));
exit(1);
}
<span class="comment">/* select other audio parameters supported by the encoder */</span>
c-><a name="a23"></a><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);
c-><a name="a24"></a><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);
c-><a name="a25"></a><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>);
<span class="comment">/* open it */</span>
<span class="keywordflow">if</span> (<a name="a26"></a><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) {
fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
exit(1);
}
f = fopen(filename, <span class="stringliteral">"wb"</span>);
<span class="keywordflow">if</span> (!f) {
fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
exit(1);
}
<span class="comment">/* frame containing input raw audio */</span>
frame = <a name="a27"></a><a class="code" href="group__lavu__frame.html#gac700017c5270c79c1e1befdeeb008b2f" title="Allocate an AVFrame and set its fields to default values.">av_frame_alloc</a>();
<span class="keywordflow">if</span> (!frame) {
fprintf(stderr, <span class="stringliteral">"Could not allocate audio frame\n"</span>);
exit(1);
}
frame-><a name="a28"></a><a class="code" href="structAVFrame.html#a02f45ab8191aea1660159f1e464237ea" title="number of audio samples (per channel) described by this frame">nb_samples</a> = c-><a name="a29"></a><a class="code" href="structAVCodecContext.html#aec57f0d859a6df8b479cd93ca3a44a33" title="Number of samples per channel in an audio frame.">frame_size</a>;
frame-><a name="a30"></a><a class="code" href="structAVFrame.html#aed14fa772ce46881020fd1545c86432c" title="format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...">format</a> = c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>;
frame-><a name="a31"></a><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>;
<span class="comment">/* the codec gives us the frame size, in samples,</span>
<span class="comment"> * we calculate the size of the samples buffer in bytes */</span>
buffer_size = <a name="a32"></a><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>,
c-><a class="code" href="structAVCodecContext.html#a1bdba69ea111e2a9d03fdaa7a46a4c45" title="audio sample format">sample_fmt</a>, 0);
<span class="keywordflow">if</span> (buffer_size < 0) {
fprintf(stderr, <span class="stringliteral">"Could not get sample buffer size\n"</span>);
exit(1);
}
samples = <a name="a33"></a><a class="code" href="group__lavu__mem.html#ga9722446c5e310ffedfaac9489864796d" title="Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...">av_malloc</a>(buffer_size);
<span class="keywordflow">if</span> (!samples) {
fprintf(stderr, <span class="stringliteral">"Could not allocate %d bytes for samples buffer\n"</span>,
buffer_size);
exit(1);
}
<span class="comment">/* setup the data pointers in the AVFrame */</span>
ret = <a name="a34"></a><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>,
(<span class="keyword">const</span> uint8_t*)samples, buffer_size, 0);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Could not setup audio frame\n"</span>);
exit(1);
}
<span class="comment">/* encode a single tone sound */</span>
t = 0;
tincr = 2 * M_PI * 440.0 / c-><a class="code" href="structAVCodecContext.html#a8ff0b000c463361e234af48d03aadfc0" title="samples per second">sample_rate</a>;
<span class="keywordflow">for</span> (i = 0; i < 200; i++) {
<a name="a35"></a><a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
pkt.<a name="a36"></a><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL; <span class="comment">// packet data will be allocated by the encoder</span>
pkt.<a name="a37"></a><a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<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++) {
samples[2*j] = (int)(sin(t) * 10000);
<span class="keywordflow">for</span> (k = 1; k < c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>; k++)
samples[2*j + k] = samples[2*j];
t += tincr;
}
<span class="comment">/* encode the samples */</span>
ret = <a name="a38"></a><a class="code" href="group__lavc__encoding.html#ga93a49fbd0973b216dcb8a8c5dffe1d82" title="Encode a frame of audio.">avcodec_encode_audio2</a>(c, &pkt, frame, &got_output);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error encoding audio frame\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (got_output) {
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="a39"></a><a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
}
}
<span class="comment">/* get the delayed frames */</span>
<span class="keywordflow">for</span> (got_output = 1; got_output; i++) {
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);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (got_output) {
fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
}
}
fclose(f);
<a name="a40"></a><a class="code" href="group__lavu__mem.html#ga0cc84043ea2167ad005c86e11d0bcdba" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...">av_freep</a>(&samples);
<a name="a41"></a><a class="code" href="group__lavu__frame.html#ga979d73f3228814aee56aeca0636e37cc" title="Free the frame and any dynamically allocated objects in it, e.g.">av_frame_free</a>(&frame);
<a name="a42"></a><a class="code" href="group__lavc__core.html#gaf4daa92361efb3523ef5afeb0b54077f" title="Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...">avcodec_close</a>(c);
<a name="a43"></a><a class="code" href="group__lavu__mem.html#ga0c9096f498624c525aa2315b8a20c411" title="Free a memory block which has been allocated with av_malloc(z)() or av_realloc().">av_free</a>(c);
}
<span class="comment">/*</span>
<span class="comment"> * Audio decoding.</span>
<span class="comment"> */</span>
<span class="keyword">static</span> <span class="keywordtype">void</span> <a name="a44"></a><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 class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<span class="keywordtype">int</span> len;
FILE *f, *outfile;
uint8_t inbuf[<a name="a45"></a><a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">AUDIO_INBUF_SIZE</a> + <a name="a46"></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 class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> avpkt;
<a class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *decoded_frame = NULL;
<a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&avpkt);
printf(<span class="stringliteral">"Decode audio file %s to %s\n"</span>, filename, outfilename);
<span class="comment">/* find the mpeg audio decoder */</span>
codec = <a name="a47"></a><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);
<span class="keywordflow">if</span> (!codec) {
fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
exit(1);
}
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);
<span class="keywordflow">if</span> (!c) {
fprintf(stderr, <span class="stringliteral">"Could not allocate audio codec context\n"</span>);
exit(1);
}
<span class="comment">/* open it */</span>
<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) {
fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
exit(1);
}
f = fopen(filename, <span class="stringliteral">"rb"</span>);
<span class="keywordflow">if</span> (!f) {
fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
exit(1);
}
outfile = fopen(outfilename, <span class="stringliteral">"wb"</span>);
<span class="keywordflow">if</span> (!outfile) {
<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);
exit(1);
}
<span class="comment">/* decode until eof */</span>
avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
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);
<span class="keywordflow">while</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> > 0) {
<span class="keywordtype">int</span> i, ch;
<span class="keywordtype">int</span> got_frame = 0;
<span class="keywordflow">if</span> (!decoded_frame) {
<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>())) {
fprintf(stderr, <span class="stringliteral">"Could not allocate audio frame\n"</span>);
exit(1);
}
}
len = <a name="a48"></a><a class="code" href="group__lavc__decoding.html#ga834bb1b062fbcc2de4cf7fb93f154a3e" title="Decode the audio frame of size avpkt->size from avpkt->data into frame.">avcodec_decode_audio4</a>(c, decoded_frame, &got_frame, &avpkt);
<span class="keywordflow">if</span> (len < 0) {
fprintf(stderr, <span class="stringliteral">"Error while decoding\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (got_frame) {
<span class="comment">/* if a frame has been decoded, output it */</span>
<span class="keywordtype">int</span> data_size = <a name="a49"></a><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>);
<span class="keywordflow">if</span> (data_size < 0) {
<span class="comment">/* This should not occur, checking just for paranoia */</span>
fprintf(stderr, <span class="stringliteral">"Failed to calculate data size\n"</span>);
exit(1);
}
<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++)
<span class="keywordflow">for</span> (ch=0; ch<c-><a class="code" href="structAVCodecContext.html#ac1e6c2cd1269caa7570575725c682a49" title="number of audio channels">channels</a>; ch++)
fwrite(decoded_frame-><a name="a50"></a><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>[ch] + data_size*i, 1, data_size, outfile);
}
avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> -= len;
avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> += len;
avpkt.<a name="a51"></a><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> =
avpkt.<a name="a52"></a><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 name="a53"></a><a class="code" href="group__lavu__time.html#ga2eaefe702f95f619ea6f2d08afa01be1" title="Undefined timestamp value.">AV_NOPTS_VALUE</a>;
<span class="keywordflow">if</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> < <a name="a54"></a><a class="code" href="decoding__encoding_8c.html#aebbadad5d16117f57006e8358ebf1ccc">AUDIO_REFILL_THRESH</a>) {
<span class="comment">/* Refill the input buffer, to avoid trying to decode</span>
<span class="comment"> * incomplete frames. Instead of this, one could also use</span>
<span class="comment"> * a parser, or use a proper container format through</span>
<span class="comment"> * libavformat. */</span>
memmove(inbuf, avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
len = fread(avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> + avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, 1,
<a class="code" href="decoding__encoding_8c.html#a3a513b5612784dd20c0dfbdf456abd01">AUDIO_INBUF_SIZE</a> - avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<span class="keywordflow">if</span> (len > 0)
avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> += len;
}
}
fclose(outfile);
fclose(f);
<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 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 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);
}
<span class="comment">/*</span>
<span class="comment"> * Video encoding example</span>
<span class="comment"> */</span>
<span class="keyword">static</span> <span class="keywordtype">void</span> <a name="a55"></a><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 class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<span class="keywordtype">int</span> i, ret, x, y, got_output;
FILE *f;
<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="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> <a class="code" href="demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090">pkt</a>;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
printf(<span class="stringliteral">"Encode video file %s\n"</span>, filename);
<span class="comment">/* find the mpeg1 video encoder */</span>
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);
<span class="keywordflow">if</span> (!codec) {
fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
exit(1);
}
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);
<span class="keywordflow">if</span> (!c) {
fprintf(stderr, <span class="stringliteral">"Could not allocate video codec context\n"</span>);
exit(1);
}
<span class="comment">/* put sample parameters */</span>
c-><a class="code" href="structAVCodecContext.html#ad9214d5af3e98ea7f1d4e94be6a522de" title="the average bitrate">bit_rate</a> = 400000;
<span class="comment">/* resolution must be a multiple of two */</span>
c-><a name="a56"></a><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a> = 352;
c-><a name="a57"></a><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a> = 288;
<span class="comment">/* frames per second */</span>
c-><a name="a58"></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="_a59"></a><a class="code" href="structAVRational.html" title="rational number numerator/denominator">AVRational</a>){1,25};
<span class="comment">/* emit one intra frame every ten frames</span>
<span class="comment"> * check frame pict_type before passing frame</span>
<span class="comment"> * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I</span>
<span class="comment"> * then gop_size is ignored and the output of encoder</span>
<span class="comment"> * will always be I frame irrespective to gop_size</span>
<span class="comment"> */</span>
c-><a name="a60"></a><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;
c-><a name="a61"></a><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;
c-><a name="a62"></a><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a> = <a name="a63"></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>;
<span class="keywordflow">if</span> (codec_id == <a name="a64"></a><a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7cea3d4c051c8b18a3c18750676a308c10be">AV_CODEC_ID_H264</a>)
<a name="a65"></a><a class="code" href="group__opt__set__funcs.html#ga5fd4b92bdf4f392a2847f711676a7537">av_opt_set</a>(c-><a name="a66"></a><a class="code" href="structAVCodecContext.html#af3379123060ad8cc9c321c29af4f8360">priv_data</a>, <span class="stringliteral">"preset"</span>, <span class="stringliteral">"slow"</span>, 0);
<span class="comment">/* open it */</span>
<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) {
fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
exit(1);
}
f = fopen(filename, <span class="stringliteral">"wb"</span>);
<span class="keywordflow">if</span> (!f) {
fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
exit(1);
}
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>();
<span class="keywordflow">if</span> (!frame) {
fprintf(stderr, <span class="stringliteral">"Could not allocate video frame\n"</span>);
exit(1);
}
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>;
frame-><a name="a67"></a><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>;
frame-><a name="a68"></a><a class="code" href="structAVFrame.html#a3f89733f429c98ba5bc64373fb0a3f13">height</a> = c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>;
<span class="comment">/* the image can be allocated by any means and av_image_alloc() is</span>
<span class="comment"> * just the most convenient way if av_malloc() is to be used */</span>
ret = <a name="a69"></a><a class="code" href="group__lavu__picture.html#ga841e0a89a642e24141af1918a2c10448" title="Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...">av_image_alloc</a>(frame-><a class="code" href="structAVFrame.html#a1d0f65014a8d1bf78cec8cbed2304992" title="pointer to the picture/channel planes.">data</a>, frame-><a name="a70"></a><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>,
c-><a class="code" href="structAVCodecContext.html#a0425c77b3d06d71e5db88b1d7e1b37f2" title="Pixel format, see AV_PIX_FMT_xxx.">pix_fmt</a>, 32);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Could not allocate raw picture buffer\n"</span>);
exit(1);
}
<span class="comment">/* encode 1 second of video */</span>
<span class="keywordflow">for</span> (i = 0; i < 25; i++) {
<a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&pkt);
pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL; <span class="comment">// packet data will be allocated by the encoder</span>
pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
fflush(stdout);
<span class="comment">/* prepare a dummy image */</span>
<span class="comment">/* Y */</span>
<span class="keywordflow">for</span> (y = 0; y < c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>; y++) {
<span class="keywordflow">for</span> (x = 0; x < c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>; x++) {
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;
}
}
<span class="comment">/* Cb and Cr */</span>
<span class="keywordflow">for</span> (y = 0; y < c-><a class="code" href="structAVCodecContext.html#a0449afd803eb107bd4dbc8b5ea22e363">height</a>/2; y++) {
<span class="keywordflow">for</span> (x = 0; x < c-><a class="code" href="structAVCodecContext.html#a0d8f46461754e8abea0847dcbc41b956" title="picture width / height.">width</a>/2; x++) {
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;
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;
}
}
frame-><a name="a71"></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> = i;
<span class="comment">/* encode the image */</span>
ret = <a name="a72"></a><a class="code" href="group__lavc__encoding.html#gaa2dc9e9ea2567ebb2801a08153c7306b" title="Encode a frame of video.">avcodec_encode_video2</a>(c, &pkt, frame, &got_output);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (got_output) {
printf(<span class="stringliteral">"Write frame %3d (size=%5d)\n"</span>, i, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
}
}
<span class="comment">/* get the delayed frames */</span>
<span class="keywordflow">for</span> (got_output = 1; got_output; i++) {
fflush(stdout);
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);
<span class="keywordflow">if</span> (ret < 0) {
fprintf(stderr, <span class="stringliteral">"Error encoding frame\n"</span>);
exit(1);
}
<span class="keywordflow">if</span> (got_output) {
printf(<span class="stringliteral">"Write frame %3d (size=%5d)\n"</span>, i, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>);
fwrite(pkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>, 1, pkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a>, f);
<a class="code" href="group__lavc__packet.html#gae65881462e78f98b268f34661d921ee0" title="Free a packet.">av_free_packet</a>(&pkt);
}
}
<span class="comment">/* add sequence end code to have a real mpeg file */</span>
fwrite(endcode, 1, <span class="keyword">sizeof</span>(endcode), f);
fclose(f);
<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 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 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 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);
printf(<span class="stringliteral">"\n"</span>);
}
<span class="comment">/*</span>
<span class="comment"> * Video decoding example</span>
<span class="comment"> */</span>
<span class="keyword">static</span> <span class="keywordtype">void</span> <a name="a73"></a><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,
<span class="keywordtype">char</span> *filename)
{
FILE *f;
<span class="keywordtype">int</span> i;
f = fopen(filename,<span class="stringliteral">"w"</span>);
fprintf(f, <span class="stringliteral">"P5\n%d %d\n%d\n"</span>, xsize, ysize, 255);
<span class="keywordflow">for</span> (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
<span class="keyword">static</span> <span class="keywordtype">int</span> <a name="a74"></a><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 class="code" href="structAVFrame.html" title="This structure describes decoded (raw) audio or video data.">AVFrame</a> *frame, <span class="keywordtype">int</span> *frame_count, <a class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> *pkt, <span class="keywordtype">int</span> last)
{
<span class="keywordtype">int</span> len, got_frame;
<span class="keywordtype">char</span> buf[1024];
len = <a name="a75"></a><a class="code" href="group__lavc__decoding.html#ga99ee61b6dcffb7817a275d39da58cc74" title="Decode the video frame of size avpkt->size from avpkt->data into picture.">avcodec_decode_video2</a>(avctx, frame, &got_frame, pkt);
<span class="keywordflow">if</span> (len < 0) {
fprintf(stderr, <span class="stringliteral">"Error while decoding frame %d\n"</span>, *frame_count);
<span class="keywordflow">return</span> len;
}
<span class="keywordflow">if</span> (got_frame) {
printf(<span class="stringliteral">"Saving %sframe %3d\n"</span>, last ? <span class="stringliteral">"last "</span> : <span class="stringliteral">""</span>, *frame_count);
fflush(stdout);
<span class="comment">/* the picture is allocated by the decoder, no need to free it */</span>
snprintf(buf, <span class="keyword">sizeof</span>(buf), outfilename, *frame_count);
<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],
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);
(*frame_count)++;
}
<span class="keywordflow">if</span> (pkt-><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a>) {
pkt-><a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> -= len;
pkt-><a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> += len;
}
<span class="keywordflow">return</span> 0;
}
<span class="keyword">static</span> <span class="keywordtype">void</span> <a name="a76"></a><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 class="code" href="structAVCodec.html" title="AVCodec.">AVCodec</a> *codec;
<a class="code" href="structAVCodecContext.html" title="main external API structure.">AVCodecContext</a> *c= NULL;
<span class="keywordtype">int</span> frame_count;
FILE *f;
<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>;
uint8_t inbuf[<a name="a77"></a><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 class="code" href="structAVPacket.html" title="This structure stores compressed data.">AVPacket</a> avpkt;
<a class="code" href="group__lavc__packet.html#gac9cb9756175b96e7441575803757fb73" title="Initialize optional fields of a packet with default values.">av_init_packet</a>(&avpkt);
<span class="comment">/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */</span>
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>);
printf(<span class="stringliteral">"Decode video file %s to %s\n"</span>, filename, outfilename);
<span class="comment">/* find the mpeg1 video decoder */</span>
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 name="a78"></a><a class="code" href="group__lavc__core.html#ggaadca229ad2c20e060a14fec08a5cc7ceaf019b13f4891b36ae579cd7bc96d1f78">AV_CODEC_ID_MPEG1VIDEO</a>);
<span class="keywordflow">if</span> (!codec) {
fprintf(stderr, <span class="stringliteral">"Codec not found\n"</span>);
exit(1);
}
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);
<span class="keywordflow">if</span> (!c) {
fprintf(stderr, <span class="stringliteral">"Could not allocate video codec context\n"</span>);
exit(1);
}
<span class="keywordflow">if</span>(codec-><a name="a79"></a><a class="code" href="structAVCodec.html#af51f7ff3dac8b730f46b9713e49a2518" title="Codec capabilities.">capabilities</a>&<a name="a80"></a><a class="code" href="group__lavc__core.html#ga8783bdc2c37a5b8a0862450a482b6816">CODEC_CAP_TRUNCATED</a>)
c-><a name="a81"></a><a class="code" href="structAVCodecContext.html#abb01e291550fa3fb96188af4d494587e" title="CODEC_FLAG_*.">flags</a>|= <a name="a82"></a><a class="code" href="group__lavc__core.html#ga3bbd99307dfa5f8a096d646bc9d03bca">CODEC_FLAG_TRUNCATED</a>; <span class="comment">/* we do not send complete frames */</span>
<span class="comment">/* For some codecs, such as msmpeg4 and mpeg4, width and height</span>
<span class="comment"> MUST be initialized there because this information is not</span>
<span class="comment"> available in the bitstream. */</span>
<span class="comment">/* open it */</span>
<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) {
fprintf(stderr, <span class="stringliteral">"Could not open codec\n"</span>);
exit(1);
}
f = fopen(filename, <span class="stringliteral">"rb"</span>);
<span class="keywordflow">if</span> (!f) {
fprintf(stderr, <span class="stringliteral">"Could not open %s\n"</span>, filename);
exit(1);
}
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>();
<span class="keywordflow">if</span> (!frame) {
fprintf(stderr, <span class="stringliteral">"Could not allocate video frame\n"</span>);
exit(1);
}
frame_count = 0;
<span class="keywordflow">for</span> (;;) {
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);
<span class="keywordflow">if</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> == 0)
<span class="keywordflow">break</span>;
<span class="comment">/* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)</span>
<span class="comment"> and this is the only method to use them because you cannot</span>
<span class="comment"> know the compressed data size before analysing it.</span>
<span class="comment"></span>
<span class="comment"> BUT some other codecs (msmpeg4, mpeg4) are inherently frame</span>
<span class="comment"> based, so you must call them with all the data for one</span>
<span class="comment"> frame exactly. You must also initialize 'width' and</span>
<span class="comment"> 'height' before initializing them. */</span>
<span class="comment">/* NOTE2: some codecs allow the raw parameters (frame size,</span>
<span class="comment"> sample rate) to be changed at any frame. We handle this, so</span>
<span class="comment"> you should also take care of it */</span>
<span class="comment">/* here, we use a stream based decoder (mpeg1video), so we</span>
<span class="comment"> feed decoder and see if it could decode a frame */</span>
avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = inbuf;
<span class="keywordflow">while</span> (avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> > 0)
<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)
exit(1);
}
<span class="comment">/* some codecs, such as MPEG, transmit the I and P frame with a</span>
<span class="comment"> latency of one frame. You must do the following to have a</span>
<span class="comment"> chance to get the last frame of the video */</span>
avpkt.<a class="code" href="structAVPacket.html#aaf4fe58dfcc7c232c1f2268b539d8367">data</a> = NULL;
avpkt.<a class="code" href="structAVPacket.html#a4d1ea19f63eb107111fd650ca514d1f4">size</a> = 0;
<a class="code" href="decoding__encoding_8c.html#a1d01e17e5817622d9419f681603f6c8f">decode_write_frame</a>(outfilename, c, frame, &frame_count, &avpkt, 1);
fclose(f);
<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 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 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);
printf(<span class="stringliteral">"\n"</span>);
}
<span class="keywordtype">int</span> <a name="a83"></a><a class="code" href="avio__list__dir_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
<span class="keyword">const</span> <span class="keywordtype">char</span> *output_type;
<span class="comment">/* register all the codecs */</span>
<a name="a84"></a><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>();
<span class="keywordflow">if</span> (argc < 2) {
printf(<span class="stringliteral">"usage: %s output_type\n"</span>
<span class="stringliteral">"API example program to decode/encode a media stream with libavcodec.\n"</span>
<span class="stringliteral">"This program generates a synthetic stream and encodes it to a file\n"</span>
<span class="stringliteral">"named test.h264, test.mp2 or test.mpg depending on output_type.\n"</span>
<span class="stringliteral">"The encoded stream is then decoded and written to a raw data output.\n"</span>
<span class="stringliteral">"output_type must be chosen between 'h264', 'mp2', 'mpg'.\n"</span>,
argv[0]);
<span class="keywordflow">return</span> 1;
}
output_type = argv[1];
<span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"h264"</span>)) {
<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>);
} <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"mp2"</span>)) {
<a class="code" href="decoding__encoding_8c.html#a67ded2b27004b72c27d4f3500a578ff6">audio_encode_example</a>(<span class="stringliteral">"test.mp2"</span>);
<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>);
} <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcmp(output_type, <span class="stringliteral">"mpg"</span>)) {
<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 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>);
} <span class="keywordflow">else</span> {
fprintf(stderr, <span class="stringliteral">"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n"</span>,
output_type);
<span class="keywordflow">return</span> 1;
}
<span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </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>