2 * Copyright (c) 2012 Stefano Sabatini
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
24 * @example doc/examples/resampling_audio.c
25 * libswresample API use example.
28 #include <libavutil/opt.h>
29 #include <libavutil/channel_layout.h>
30 #include <libavutil/samplefmt.h>
31 #include <libswresample/swresample.h>
33 static int get_format_from_sample_fmt(const char **fmt,
34 enum AVSampleFormat sample_fmt)
37 struct sample_fmt_entry {
38 enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
39 } sample_fmt_entries[] = {
40 { AV_SAMPLE_FMT_U8, "u8", "u8" },
41 { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
42 { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
43 { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
44 { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
48 for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
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);
57 "Sample format %s not supported as output format\n",
58 av_get_sample_fmt_name(sample_fmt));
59 return AVERROR(EINVAL);
63 * Fill dst buffer with nb_samples, generated starting from t.
65 void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
68 double tincr = 1.0 / sample_rate, *dstp = dst;
69 const double c = 2 * M_PI * 440.0;
71 /* generate sin tone with 440Hz frequency and duplicated channels */
72 for (i = 0; i < nb_samples; i++) {
74 for (j = 1; j < nb_channels; j++)
81 int alloc_samples_array_and_data(uint8_t ***data, int *linesize, int nb_channels,
82 int nb_samples, enum AVSampleFormat sample_fmt, int align)
84 int nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
86 *data = av_malloc(sizeof(*data) * nb_planes);
88 return AVERROR(ENOMEM);
89 return av_samples_alloc(*data, linesize, nb_channels,
90 nb_samples, sample_fmt, align);
93 int main(int argc, char **argv)
95 int64_t src_ch_layout = AV_CH_LAYOUT_STEREO, dst_ch_layout = AV_CH_LAYOUT_SURROUND;
96 int src_rate = 48000, dst_rate = 44100;
97 uint8_t **src_data = NULL, **dst_data = NULL;
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;
101 enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
102 const char *dst_filename = NULL;
106 struct SwrContext *swr_ctx;
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",
118 dst_filename = argv[1];
120 dst_file = fopen(dst_filename, "wb");
122 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
126 /* create resampler context */
127 swr_ctx = swr_alloc();
129 fprintf(stderr, "Could not allocate resampler context\n");
130 ret = AVERROR(ENOMEM);
135 av_opt_set_int(swr_ctx, "in_channel_layout", src_ch_layout, 0);
136 av_opt_set_int(swr_ctx, "in_sample_rate", src_rate, 0);
137 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
139 av_opt_set_int(swr_ctx, "out_channel_layout", dst_ch_layout, 0);
140 av_opt_set_int(swr_ctx, "out_sample_rate", dst_rate, 0);
141 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
143 /* initialize the resampling context */
144 if ((ret = swr_init(swr_ctx)) < 0) {
145 fprintf(stderr, "Failed to initialize the resampling context\n");
149 /* allocate source and destination samples buffers */
151 src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);
152 ret = alloc_samples_array_and_data(&src_data, &src_linesize, src_nb_channels,
153 src_nb_samples, src_sample_fmt, 0);
155 fprintf(stderr, "Could not allocate source samples\n");
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 =
163 av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
165 /* buffer is going to be directly written to a rawaudio file, no alignment */
166 dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);
167 ret = alloc_samples_array_and_data(&dst_data, &dst_linesize, dst_nb_channels,
168 dst_nb_samples, dst_sample_fmt, 0);
170 fprintf(stderr, "Could not allocate destination samples\n");
176 /* generate synthetic audio */
177 fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
179 /* compute destination number of samples */
180 dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
181 src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
182 if (dst_nb_samples > max_dst_nb_samples) {
183 av_free(dst_data[0]);
184 ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
185 dst_nb_samples, dst_sample_fmt, 1);
188 max_dst_nb_samples = dst_nb_samples;
191 /* convert to destination format */
192 ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
194 fprintf(stderr, "Error while converting\n");
197 dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
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);
203 if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
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);
214 av_freep(&src_data[0]);
218 av_freep(&dst_data[0]);