2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * audio encoding with libavcodec API example.
25 * @example encode_audio.c
32 #include "libavcodec/avcodec.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/common.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/samplefmt.h"
39 /* check that a given sample format is supported by the encoder */
40 static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
42 const enum AVSampleFormat *p = codec->sample_fmts;
44 while (*p != AV_SAMPLE_FMT_NONE) {
52 /* just pick the highest supported samplerate */
53 static int select_sample_rate(const AVCodec *codec)
56 int best_samplerate = 0;
58 if (!codec->supported_samplerates)
61 p = codec->supported_samplerates;
63 best_samplerate = FFMAX(*p, best_samplerate);
66 return best_samplerate;
69 /* select layout with the highest channel count */
70 static int select_channel_layout(const AVCodec *codec)
73 uint64_t best_ch_layout = 0;
74 int best_nb_channels = 0;
76 if (!codec->channel_layouts)
77 return AV_CH_LAYOUT_STEREO;
79 p = codec->channel_layouts;
81 int nb_channels = av_get_channel_layout_nb_channels(*p);
83 if (nb_channels > best_nb_channels) {
85 best_nb_channels = nb_channels;
89 return best_ch_layout;
92 static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
97 /* send the frame for encoding */
98 ret = avcodec_send_frame(ctx, frame);
100 fprintf(stderr, "error sending the frame to the encoder\n");
104 /* read all the available output packets (in general there may be any
107 ret = avcodec_receive_packet(ctx, pkt);
108 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
111 fprintf(stderr, "error encoding audio frame\n");
115 fwrite(pkt->data, 1, pkt->size, output);
116 av_packet_unref(pkt);
120 int main(int argc, char **argv)
122 const char *filename;
123 const AVCodec *codec;
124 AVCodecContext *c= NULL;
133 fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
138 /* register all the codecs */
139 avcodec_register_all();
141 /* find the MP2 encoder */
142 codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
144 fprintf(stderr, "codec not found\n");
148 c = avcodec_alloc_context3(codec);
150 /* put sample parameters */
153 /* check that the encoder supports s16 pcm input */
154 c->sample_fmt = AV_SAMPLE_FMT_S16;
155 if (!check_sample_fmt(codec, c->sample_fmt)) {
156 fprintf(stderr, "encoder does not support %s",
157 av_get_sample_fmt_name(c->sample_fmt));
161 /* select other audio parameters supported by the encoder */
162 c->sample_rate = select_sample_rate(codec);
163 c->channel_layout = select_channel_layout(codec);
164 c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
167 if (avcodec_open2(c, codec, NULL) < 0) {
168 fprintf(stderr, "could not open codec\n");
172 f = fopen(filename, "wb");
174 fprintf(stderr, "could not open %s\n", filename);
178 /* packet for holding encoded output */
179 pkt = av_packet_alloc();
181 fprintf(stderr, "could not allocate the packet\n");
185 /* frame containing input raw audio */
186 frame = av_frame_alloc();
188 fprintf(stderr, "could not allocate audio frame\n");
192 frame->nb_samples = c->frame_size;
193 frame->format = c->sample_fmt;
194 frame->channel_layout = c->channel_layout;
196 /* allocate the data buffers */
197 ret = av_frame_get_buffer(frame, 0);
199 fprintf(stderr, "could not allocate audio data buffers\n");
203 /* encode a single tone sound */
205 tincr = 2 * M_PI * 440.0 / c->sample_rate;
207 /* make sure the frame is writable -- makes a copy if the encoder
208 * kept a reference internally */
209 ret = av_frame_make_writable(frame);
212 samples = (uint16_t*)frame->data[0];
214 for (j = 0; j < c->frame_size; j++) {
215 samples[2*j] = (int)(sin(t) * 10000);
217 for (k = 1; k < c->channels; k++)
218 samples[2*j + k] = samples[2*j];
221 encode(c, frame, pkt, f);
224 /* flush the encoder */
225 encode(c, NULL, pkt, f);
229 av_frame_free(&frame);
230 av_packet_free(&pkt);
231 avcodec_free_context(&c);