FFmpeg 7.1.1
Loading...
Searching...
No Matches
resample_audio.c
Go to the documentation of this file.
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 * @file audio resampling API usage example
25 * @example resample_audio.c
26 *
27 * Generate a synthetic audio signal, and Use libswresample API to perform audio
28 * resampling. The output is written to a raw audio file to be played with
29 * ffplay.
30 */
31
32#include <libavutil/opt.h>
34#include <libavutil/samplefmt.h>
36
37static int get_format_from_sample_fmt(const char **fmt,
38 enum AVSampleFormat sample_fmt)
39{
40 int i;
41 struct sample_fmt_entry {
42 enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
43 } sample_fmt_entries[] = {
44 { AV_SAMPLE_FMT_U8, "u8", "u8" },
45 { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
46 { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
47 { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
48 { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
49 };
50 *fmt = NULL;
51
52 for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
53 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
54 if (sample_fmt == entry->sample_fmt) {
55 *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
56 return 0;
57 }
58 }
59
60 fprintf(stderr,
61 "Sample format %s not supported as output format\n",
62 av_get_sample_fmt_name(sample_fmt));
63 return AVERROR(EINVAL);
64}
65
66/**
67 * Fill dst buffer with nb_samples, generated starting from t.
68 */
69static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
70{
71 int i, j;
72 double tincr = 1.0 / sample_rate, *dstp = dst;
73 const double c = 2 * M_PI * 440.0;
74
75 /* generate sin tone with 440Hz frequency and duplicated channels */
76 for (i = 0; i < nb_samples; i++) {
77 *dstp = sin(c * *t);
78 for (j = 1; j < nb_channels; j++)
79 dstp[j] = dstp[0];
80 dstp += nb_channels;
81 *t += tincr;
82 }
83}
84
85int main(int argc, char **argv)
86{
88 int src_rate = 48000, dst_rate = 44100;
89 uint8_t **src_data = NULL, **dst_data = NULL;
90 int src_nb_channels = 0, dst_nb_channels = 0;
91 int src_linesize, dst_linesize;
92 int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
93 enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
94 const char *dst_filename = NULL;
95 FILE *dst_file;
96 int dst_bufsize;
97 const char *fmt;
98 struct SwrContext *swr_ctx;
99 char buf[64];
100 double t;
101 int ret;
102
103 if (argc != 2) {
104 fprintf(stderr, "Usage: %s output_file\n"
105 "API example program to show how to resample an audio stream with libswresample.\n"
106 "This program generates a series of audio frames, resamples them to a specified "
107 "output format and rate and saves them to an output file named output_file.\n",
108 argv[0]);
109 exit(1);
110 }
111 dst_filename = argv[1];
112
113 dst_file = fopen(dst_filename, "wb");
114 if (!dst_file) {
115 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
116 exit(1);
117 }
118
119 /* create resampler context */
120 swr_ctx = swr_alloc();
121 if (!swr_ctx) {
122 fprintf(stderr, "Could not allocate resampler context\n");
123 ret = AVERROR(ENOMEM);
124 goto end;
125 }
126
127 /* set options */
128 av_opt_set_chlayout(swr_ctx, "in_chlayout", &src_ch_layout, 0);
129 av_opt_set_int(swr_ctx, "in_sample_rate", src_rate, 0);
130 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
131
132 av_opt_set_chlayout(swr_ctx, "out_chlayout", &dst_ch_layout, 0);
133 av_opt_set_int(swr_ctx, "out_sample_rate", dst_rate, 0);
134 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
135
136 /* initialize the resampling context */
137 if ((ret = swr_init(swr_ctx)) < 0) {
138 fprintf(stderr, "Failed to initialize the resampling context\n");
139 goto end;
140 }
141
142 /* allocate source and destination samples buffers */
143
144 src_nb_channels = src_ch_layout.nb_channels;
145 ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
146 src_nb_samples, src_sample_fmt, 0);
147 if (ret < 0) {
148 fprintf(stderr, "Could not allocate source samples\n");
149 goto end;
150 }
151
152 /* compute the number of converted samples: buffering is avoided
153 * ensuring that the output buffer will contain at least all the
154 * converted input samples */
155 max_dst_nb_samples = dst_nb_samples =
156 av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
157
158 /* buffer is going to be directly written to a rawaudio file, no alignment */
159 dst_nb_channels = dst_ch_layout.nb_channels;
160 ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
161 dst_nb_samples, dst_sample_fmt, 0);
162 if (ret < 0) {
163 fprintf(stderr, "Could not allocate destination samples\n");
164 goto end;
165 }
166
167 t = 0;
168 do {
169 /* generate synthetic audio */
170 fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
171
172 /* compute destination number of samples */
173 dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
174 src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
175 if (dst_nb_samples > max_dst_nb_samples) {
176 av_freep(&dst_data[0]);
177 ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
178 dst_nb_samples, dst_sample_fmt, 1);
179 if (ret < 0)
180 break;
181 max_dst_nb_samples = dst_nb_samples;
182 }
183
184 /* convert to destination format */
185 ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
186 if (ret < 0) {
187 fprintf(stderr, "Error while converting\n");
188 goto end;
189 }
190 dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
191 ret, dst_sample_fmt, 1);
192 if (dst_bufsize < 0) {
193 fprintf(stderr, "Could not get sample buffer size\n");
194 goto end;
195 }
196 printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
197 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
198 } while (t < 10);
199
200 if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
201 goto end;
202 av_channel_layout_describe(&dst_ch_layout, buf, sizeof(buf));
203 fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
204 "ffplay -f %s -channel_layout %s -channels %d -ar %d %s\n",
205 fmt, buf, dst_nb_channels, dst_rate, dst_filename);
206
207end:
208 fclose(dst_file);
209
210 if (src_data)
211 av_freep(&src_data[0]);
212 av_freep(&src_data);
213
214 if (dst_data)
215 av_freep(&dst_data[0]);
216 av_freep(&dst_data);
217
218 swr_free(&swr_ctx);
219 return ret < 0;
220}
int main(int argc, char *argv[])
Public libavutil channel layout APIs header.
static int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt)
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_SURROUND
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
#define AVERROR(e)
Definition error.h:45
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const
Rescale a 64-bit integer with specified rounding.
@ AV_ROUND_UP
Round toward +infinity.
void av_freep(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family,...
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly.
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
struct SwrContext * swr_alloc(void)
Allocate SwrContext.
struct SwrContext SwrContext
The libswresample context.
Definition swresample.h:189
int64_t swr_get_delay(struct SwrContext *s, int64_t base)
Gets the delay the next input sample will experience relative to the next output sample.
void swr_free(struct SwrContext **s)
Free the given SwrContext and set the pointer to NULL.
int swr_convert(struct SwrContext *s, uint8_t *const *out, int out_count, const uint8_t *const *in, int in_count)
Convert audio.
int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *layout, int search_flags)
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
#define AV_NE(be, le)
Definition macros.h:33
#define FF_ARRAY_ELEMS(a)
Definition macros.h:53
#define M_PI
Definition mathematics.h:67
AVOptions.
static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
Fill dst buffer with nb_samples, generated starting from t.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
libswresample public header