1 /*
2 * Copyright (c) 2001 Fabrice Bellard
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file libavcodec encoding audio API usage examples
25 * @example encode_audio.c
26 *
27 * Generate a synthetic audio signal and encode it to an output MP2 file.
28 */
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
35
40
41 /* check that a given sample format is supported by the encoder */
43 {
45
48 return 1;
50 }
51 return 0;
52 }
53
54 /* just pick the highest supported samplerate */
56 {
58 int best_samplerate = 0;
59
61 return 44100;
62
65 if (!best_samplerate ||
abs(44100 - *
p) <
abs(44100 - best_samplerate))
68 }
69 return best_samplerate;
70 }
71
72 /* select layout with the highest channel count */
74 {
76 int best_nb_channels = 0;
77
80
82 while (
p->nb_channels) {
83 int nb_channels =
p->nb_channels;
84
85 if (nb_channels > best_nb_channels) {
87 best_nb_channels = nb_channels;
88 }
90 }
92 }
93
96 {
98
99 /* send the frame for encoding */
102 fprintf(stderr, "Error sending the frame to the encoder\n");
103 exit(1);
104 }
105
106 /* read all the available output packets (in general there may be any
107 * number of them */
111 return;
113 fprintf(stderr, "Error encoding audio frame\n");
114 exit(1);
115 }
116
119 }
120 }
121
122 int main(
int argc,
char **argv)
123 {
124 const char *filename;
132 float t, tincr;
133
134 if (argc <= 1) {
135 fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
136 return 0;
137 }
138 filename = argv[1];
139
140 /* find the MP2 encoder */
142 if (!codec) {
143 fprintf(stderr, "Codec not found\n");
144 exit(1);
145 }
146
149 fprintf(stderr, "Could not allocate audio codec context\n");
150 exit(1);
151 }
152
153 /* put sample parameters */
155
156 /* check that the encoder supports s16 pcm input */
159 fprintf(stderr, "Encoder does not support sample format %s",
161 exit(1);
162 }
163
164 /* select other audio parameters supported by the encoder */
168 exit(1);
169
170 /* open it */
172 fprintf(stderr, "Could not open codec\n");
173 exit(1);
174 }
175
176 f = fopen(filename,
"wb");
178 fprintf(stderr, "Could not open %s\n", filename);
179 exit(1);
180 }
181
182 /* packet for holding encoded output */
185 fprintf(stderr, "could not allocate the packet\n");
186 exit(1);
187 }
188
189 /* frame containing input raw audio */
192 fprintf(stderr, "Could not allocate audio frame\n");
193 exit(1);
194 }
195
196 frame->nb_samples =
c->frame_size;
197 frame->format =
c->sample_fmt;
200 exit(1);
201
202 /* allocate the data buffers */
205 fprintf(stderr, "Could not allocate audio data buffers\n");
206 exit(1);
207 }
208
209 /* encode a single tone sound */
210 t = 0;
211 tincr = 2 *
M_PI * 440.0 /
c->sample_rate;
212 for (
i = 0;
i < 200;
i++) {
213 /* make sure the frame is writable -- makes a copy if the encoder
214 * kept a reference internally */
217 exit(1);
219
220 for (j = 0; j <
c->frame_size; j++) {
221 samples[2*j] = (int)(sin(t) * 10000);
222
223 for (k = 1; k <
c->ch_layout.nb_channels; k++)
225 t += tincr;
226 }
228 }
229
230 /* flush the encoder */
232
234
238
239 return 0;
240 }