2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * audio encoding with libavcodec API example.
27 * @example encode_audio.c
34 #include "libavcodec/avcodec.h"
36 #include "libavutil/channel_layout.h"
37 #include "libavutil/common.h"
38 #include "libavutil/frame.h"
39 #include "libavutil/samplefmt.h"
41 /* check that a given sample format is supported by the encoder */
42 static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
44 const enum AVSampleFormat *p = codec->sample_fmts;
46 while (*p != AV_SAMPLE_FMT_NONE) {
54 /* just pick the highest supported samplerate */
55 static int select_sample_rate(const AVCodec *codec)
58 int best_samplerate = 0;
60 if (!codec->supported_samplerates)
63 p = codec->supported_samplerates;
65 best_samplerate = FFMAX(*p, best_samplerate);
68 return best_samplerate;
71 /* select layout with the highest channel count */
72 static int select_channel_layout(const AVCodec *codec)
75 uint64_t best_ch_layout = 0;
76 int best_nb_channels = 0;
78 if (!codec->channel_layouts)
79 return AV_CH_LAYOUT_STEREO;
81 p = codec->channel_layouts;
83 int nb_channels = av_get_channel_layout_nb_channels(*p);
85 if (nb_channels > best_nb_channels) {
87 best_nb_channels = nb_channels;
91 return best_ch_layout;
94 int main(int argc, char **argv)
98 AVCodecContext *c= NULL;
101 int i, j, k, ret, got_output;
108 fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
113 /* register all the codecs */
114 avcodec_register_all();
116 /* find the MP2 encoder */
117 codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
119 fprintf(stderr, "Codec not found\n");
123 c = avcodec_alloc_context3(codec);
125 fprintf(stderr, "Could not allocate audio codec context\n");
129 /* put sample parameters */
132 /* check that the encoder supports s16 pcm input */
133 c->sample_fmt = AV_SAMPLE_FMT_S16;
134 if (!check_sample_fmt(codec, c->sample_fmt)) {
135 fprintf(stderr, "Encoder does not support sample format %s",
136 av_get_sample_fmt_name(c->sample_fmt));
140 /* select other audio parameters supported by the encoder */
141 c->sample_rate = select_sample_rate(codec);
142 c->channel_layout = select_channel_layout(codec);
143 c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
146 if (avcodec_open2(c, codec, NULL) < 0) {
147 fprintf(stderr, "Could not open codec\n");
151 f = fopen(filename, "wb");
153 fprintf(stderr, "Could not open %s\n", filename);
157 /* frame containing input raw audio */
158 frame = av_frame_alloc();
160 fprintf(stderr, "Could not allocate audio frame\n");
164 frame->nb_samples = c->frame_size;
165 frame->format = c->sample_fmt;
166 frame->channel_layout = c->channel_layout;
168 /* the codec gives us the frame size, in samples,
169 * we calculate the size of the samples buffer in bytes */
170 buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
172 if (buffer_size < 0) {
173 fprintf(stderr, "Could not get sample buffer size\n");
176 samples = av_malloc(buffer_size);
178 fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
182 /* setup the data pointers in the AVFrame */
183 ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
184 (const uint8_t*)samples, buffer_size, 0);
186 fprintf(stderr, "Could not setup audio frame\n");
190 /* encode a single tone sound */
192 tincr = 2 * M_PI * 440.0 / c->sample_rate;
193 for (i = 0; i < 200; i++) {
194 av_init_packet(&pkt);
195 pkt.data = NULL; // packet data will be allocated by the encoder
198 for (j = 0; j < c->frame_size; j++) {
199 samples[2*j] = (int)(sin(t) * 10000);
201 for (k = 1; k < c->channels; k++)
202 samples[2*j + k] = samples[2*j];
205 /* encode the samples */
206 ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
208 fprintf(stderr, "Error encoding audio frame\n");
212 fwrite(pkt.data, 1, pkt.size, f);
213 av_packet_unref(&pkt);
217 /* get the delayed frames */
218 for (got_output = 1; got_output; i++) {
219 ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
221 fprintf(stderr, "Error encoding frame\n");
226 fwrite(pkt.data, 1, pkt.size, f);
227 av_packet_unref(&pkt);
233 av_frame_free(&frame);
234 avcodec_free_context(&c);