1 /*
2 * Copyright (c) 2012 Stefano Sabatini
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 * @example doc/examples/resampling_audio.c
25 * libswresample API use example.
26 */
27
32
35 {
36 int i;
37 struct sample_fmt_entry {
39 } sample_fmt_entries[] = {
45 };
47
49 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
50 if (sample_fmt == entry->sample_fmt) {
51 *fmt =
AV_NE(entry->fmt_be, entry->fmt_le);
52 return 0;
53 }
54 }
55
56 fprintf(stderr,
57 "Sample format %s not supported as output format\n",
60 }
61
62 /**
63 * Fill dst buffer with nb_samples, generated starting from t.
64 */
66 {
67 int i, j;
69 const double c = 2 *
M_PI * 440.0;
70
71 /* generate sin tone with 440Hz frequency and duplicated channels */
73 *dstp = sin(c * *t);
75 dstp[j] = dstp[0];
78 }
79 }
80
83 {
85
86 *data =
av_malloc(
sizeof(*data) * nb_planes);
87 if (!*data)
90 nb_samples, sample_fmt, align);
91 }
92
93 int main(
int argc,
char **argv)
94 {
96 int src_rate = 48000, dst_rate = 44100;
98 int src_nb_channels = 0, dst_nb_channels = 0;
99 int src_linesize, dst_linesize;
100 int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
102 const char *dst_filename =
NULL;
103 FILE *dst_file;
104 int dst_bufsize;
108 int ret;
109
110 if (argc != 2) {
111 fprintf(stderr, "Usage: %s output_file\n"
112 "API example program to show how to resample an audio stream with libswresample.\n"
113 "This program generates a series of audio frames, resamples them to a specified "
114 "output format and rate and saves them to an output file named output_file.\n",
115 argv[0]);
116 exit(1);
117 }
118 dst_filename = argv[1];
119
120 dst_file = fopen(dst_filename, "wb");
121 if (!dst_file) {
122 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
123 exit(1);
124 }
125
126 /* create resampler context */
128 if (!swr_ctx) {
129 fprintf(stderr, "Could not allocate resampler context\n");
132 }
133
134 /* set options */
138
142
143 /* initialize the resampling context */
144 if ((ret =
swr_init(swr_ctx)) < 0) {
145 fprintf(stderr, "Failed to initialize the resampling context\n");
147 }
148
149 /* allocate source and destination samples buffers */
150
153 src_nb_samples, src_sample_fmt, 0);
154 if (ret < 0) {
155 fprintf(stderr, "Could not allocate source samples\n");
157 }
158
159 /* compute the number of converted samples: buffering is avoided
160 * ensuring that the output buffer will contain at least all the
161 * converted input samples */
162 max_dst_nb_samples = dst_nb_samples =
164
165 /* buffer is going to be directly written to a rawaudio file, no alignment */
168 dst_nb_samples, dst_sample_fmt, 0);
169 if (ret < 0) {
170 fprintf(stderr, "Could not allocate destination samples\n");
172 }
173
174 t = 0;
175 do {
176 /* generate synthetic audio */
177 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
178
179 /* compute destination number of samples */
182 if (dst_nb_samples > max_dst_nb_samples) {
185 dst_nb_samples, dst_sample_fmt, 1);
186 if (ret < 0)
187 break;
188 max_dst_nb_samples = dst_nb_samples;
189 }
190
191 /* convert to destination format */
192 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
193 if (ret < 0) {
194 fprintf(stderr, "Error while converting\n");
196 }
198 ret, dst_sample_fmt, 1);
199 printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
200 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
201 } while (t < 10);
202
205 fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
206 "ffplay -f %s -channel_layout %"PRId64" -channels %d -ar %d %s\n",
207 fmt, dst_ch_layout, dst_nb_channels, dst_rate, dst_filename);
208
210 if (dst_file)
211 fclose(dst_file);
212
213 if (src_data)
216
217 if (dst_data)
220
222 return ret < 0;
223 }