]> git.sesse.net Git - ffmpeg/blob - doc/examples/encode_audio.c
examples/decode_audio: flush the decoder
[ffmpeg] / doc / examples / encode_audio.c
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * audio encoding with libavcodec API example.
24  *
25  * @example encode_audio.c
26  */
27
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "libavcodec/avcodec.h"
33
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/common.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/samplefmt.h"
38
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)
41 {
42     const enum AVSampleFormat *p = codec->sample_fmts;
43
44     while (*p != AV_SAMPLE_FMT_NONE) {
45         if (*p == sample_fmt)
46             return 1;
47         p++;
48     }
49     return 0;
50 }
51
52 /* just pick the highest supported samplerate */
53 static int select_sample_rate(const AVCodec *codec)
54 {
55     const int *p;
56     int best_samplerate = 0;
57
58     if (!codec->supported_samplerates)
59         return 44100;
60
61     p = codec->supported_samplerates;
62     while (*p) {
63         best_samplerate = FFMAX(*p, best_samplerate);
64         p++;
65     }
66     return best_samplerate;
67 }
68
69 /* select layout with the highest channel count */
70 static int select_channel_layout(const AVCodec *codec)
71 {
72     const uint64_t *p;
73     uint64_t best_ch_layout = 0;
74     int best_nb_channels   = 0;
75
76     if (!codec->channel_layouts)
77         return AV_CH_LAYOUT_STEREO;
78
79     p = codec->channel_layouts;
80     while (*p) {
81         int nb_channels = av_get_channel_layout_nb_channels(*p);
82
83         if (nb_channels > best_nb_channels) {
84             best_ch_layout    = *p;
85             best_nb_channels = nb_channels;
86         }
87         p++;
88     }
89     return best_ch_layout;
90 }
91
92 static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
93                    FILE *output)
94 {
95     int ret;
96
97     /* send the frame for encoding */
98     ret = avcodec_send_frame(ctx, frame);
99     if (ret < 0) {
100         fprintf(stderr, "error sending the frame to the encoder\n");
101         exit(1);
102     }
103
104     /* read all the available output packets (in general there may be any
105      * number of them */
106     while (ret >= 0) {
107         ret = avcodec_receive_packet(ctx, pkt);
108         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
109             return;
110         else if (ret < 0) {
111             fprintf(stderr, "error encoding audio frame\n");
112             exit(1);
113         }
114
115         fwrite(pkt->data, 1, pkt->size, output);
116         av_packet_unref(pkt);
117     }
118 }
119
120 int main(int argc, char **argv)
121 {
122     const char *filename;
123     const AVCodec *codec;
124     AVCodecContext *c= NULL;
125     AVFrame *frame;
126     AVPacket *pkt;
127     int i, j, k, ret;
128     FILE *f;
129     uint16_t *samples;
130     float t, tincr;
131
132     if (argc <= 1) {
133         fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
134         return 0;
135     }
136     filename = argv[1];
137
138     /* register all the codecs */
139     avcodec_register_all();
140
141     /* find the MP2 encoder */
142     codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
143     if (!codec) {
144         fprintf(stderr, "codec not found\n");
145         exit(1);
146     }
147
148     c = avcodec_alloc_context3(codec);
149
150     /* put sample parameters */
151     c->bit_rate = 64000;
152
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));
158         exit(1);
159     }
160
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);
165
166     /* open it */
167     if (avcodec_open2(c, codec, NULL) < 0) {
168         fprintf(stderr, "could not open codec\n");
169         exit(1);
170     }
171
172     f = fopen(filename, "wb");
173     if (!f) {
174         fprintf(stderr, "could not open %s\n", filename);
175         exit(1);
176     }
177
178     /* packet for holding encoded output */
179     pkt = av_packet_alloc();
180     if (!pkt) {
181         fprintf(stderr, "could not allocate the packet\n");
182         exit(1);
183     }
184
185     /* frame containing input raw audio */
186     frame = av_frame_alloc();
187     if (!frame) {
188         fprintf(stderr, "could not allocate audio frame\n");
189         exit(1);
190     }
191
192     frame->nb_samples     = c->frame_size;
193     frame->format         = c->sample_fmt;
194     frame->channel_layout = c->channel_layout;
195
196     /* allocate the data buffers */
197     ret = av_frame_get_buffer(frame, 0);
198     if (ret < 0) {
199         fprintf(stderr, "could not allocate audio data buffers\n");
200         exit(1);
201     }
202
203     /* encode a single tone sound */
204     t = 0;
205     tincr = 2 * M_PI * 440.0 / c->sample_rate;
206     for(i=0;i<200;i++) {
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);
210         if (ret < 0)
211             exit(1);
212         samples = (uint16_t*)frame->data[0];
213
214         for (j = 0; j < c->frame_size; j++) {
215             samples[2*j] = (int)(sin(t) * 10000);
216
217             for (k = 1; k < c->channels; k++)
218                 samples[2*j + k] = samples[2*j];
219             t += tincr;
220         }
221         encode(c, frame, pkt, f);
222     }
223
224     /* flush the encoder */
225     encode(c, NULL, pkt, f);
226
227     fclose(f);
228
229     av_frame_free(&frame);
230     av_packet_free(&pkt);
231     avcodec_free_context(&c);
232 }