FFmpeg
resampling_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  * @example resampling_audio.c
25  * libswresample API use example.
26  */
27 
28 #include <libavutil/opt.h>
30 #include <libavutil/samplefmt.h>
32 
33 static int get_format_from_sample_fmt(const char **fmt,
34  enum AVSampleFormat sample_fmt)
35 {
36  int i;
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" },
45  };
46  *fmt = NULL;
47 
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);
52  return 0;
53  }
54  }
55 
56  fprintf(stderr,
57  "Sample format %s not supported as output format\n",
58  av_get_sample_fmt_name(sample_fmt));
59  return AVERROR(EINVAL);
60 }
61 
62 /**
63  * Fill dst buffer with nb_samples, generated starting from t.
64  */
65 static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
66 {
67  int i, j;
68  double tincr = 1.0 / sample_rate, *dstp = dst;
69  const double c = 2 * M_PI * 440.0;
70 
71  /* generate sin tone with 440Hz frequency and duplicated channels */
72  for (i = 0; i < nb_samples; i++) {
73  *dstp = sin(c * *t);
74  for (j = 1; j < nb_channels; j++)
75  dstp[j] = dstp[0];
76  dstp += nb_channels;
77  *t += tincr;
78  }
79 }
80 
81 int main(int argc, char **argv)
82 {
84  int src_rate = 48000, dst_rate = 44100;
85  uint8_t **src_data = NULL, **dst_data = NULL;
86  int src_nb_channels = 0, dst_nb_channels = 0;
87  int src_linesize, dst_linesize;
88  int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
89  enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
90  const char *dst_filename = NULL;
91  FILE *dst_file;
92  int dst_bufsize;
93  const char *fmt;
94  struct SwrContext *swr_ctx;
95  char buf[64];
96  double t;
97  int ret;
98 
99  if (argc != 2) {
100  fprintf(stderr, "Usage: %s output_file\n"
101  "API example program to show how to resample an audio stream with libswresample.\n"
102  "This program generates a series of audio frames, resamples them to a specified "
103  "output format and rate and saves them to an output file named output_file.\n",
104  argv[0]);
105  exit(1);
106  }
107  dst_filename = argv[1];
108 
109  dst_file = fopen(dst_filename, "wb");
110  if (!dst_file) {
111  fprintf(stderr, "Could not open destination file %s\n", dst_filename);
112  exit(1);
113  }
114 
115  /* create resampler context */
116  swr_ctx = swr_alloc();
117  if (!swr_ctx) {
118  fprintf(stderr, "Could not allocate resampler context\n");
119  ret = AVERROR(ENOMEM);
120  goto end;
121  }
122 
123  /* set options */
124  av_opt_set_chlayout(swr_ctx, "in_chlayout", &src_ch_layout, 0);
125  av_opt_set_int(swr_ctx, "in_sample_rate", src_rate, 0);
126  av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
127 
128  av_opt_set_chlayout(swr_ctx, "out_chlayout", &dst_ch_layout, 0);
129  av_opt_set_int(swr_ctx, "out_sample_rate", dst_rate, 0);
130  av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
131 
132  /* initialize the resampling context */
133  if ((ret = swr_init(swr_ctx)) < 0) {
134  fprintf(stderr, "Failed to initialize the resampling context\n");
135  goto end;
136  }
137 
138  /* allocate source and destination samples buffers */
139 
140  src_nb_channels = src_ch_layout.nb_channels;
141  ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
142  src_nb_samples, src_sample_fmt, 0);
143  if (ret < 0) {
144  fprintf(stderr, "Could not allocate source samples\n");
145  goto end;
146  }
147 
148  /* compute the number of converted samples: buffering is avoided
149  * ensuring that the output buffer will contain at least all the
150  * converted input samples */
151  max_dst_nb_samples = dst_nb_samples =
152  av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
153 
154  /* buffer is going to be directly written to a rawaudio file, no alignment */
155  dst_nb_channels = dst_ch_layout.nb_channels;
156  ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
157  dst_nb_samples, dst_sample_fmt, 0);
158  if (ret < 0) {
159  fprintf(stderr, "Could not allocate destination samples\n");
160  goto end;
161  }
162 
163  t = 0;
164  do {
165  /* generate synthetic audio */
166  fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
167 
168  /* compute destination number of samples */
169  dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
170  src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
171  if (dst_nb_samples > max_dst_nb_samples) {
172  av_freep(&dst_data[0]);
173  ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
174  dst_nb_samples, dst_sample_fmt, 1);
175  if (ret < 0)
176  break;
177  max_dst_nb_samples = dst_nb_samples;
178  }
179 
180  /* convert to destination format */
181  ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
182  if (ret < 0) {
183  fprintf(stderr, "Error while converting\n");
184  goto end;
185  }
186  dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
187  ret, dst_sample_fmt, 1);
188  if (dst_bufsize < 0) {
189  fprintf(stderr, "Could not get sample buffer size\n");
190  goto end;
191  }
192  printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
193  fwrite(dst_data[0], 1, dst_bufsize, dst_file);
194  } while (t < 10);
195 
196  if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
197  goto end;
198  av_channel_layout_describe(&dst_ch_layout, buf, sizeof(buf));
199  fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
200  "ffplay -f %s -channel_layout %s -channels %d -ar %d %s\n",
201  fmt, buf, dst_nb_channels, dst_rate, dst_filename);
202 
203 end:
204  fclose(dst_file);
205 
206  if (src_data)
207  av_freep(&src_data[0]);
208  av_freep(&src_data);
209 
210  if (dst_data)
211  av_freep(&dst_data[0]);
212  av_freep(&dst_data);
213 
214  swr_free(&swr_ctx);
215  return ret < 0;
216 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
main
int main(int argc, char **argv)
Definition: resampling_audio.c:81
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:354
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
samplefmt.h
av_samples_alloc
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.
Definition: samplefmt.c:182
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:357
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
fill_samples
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.
Definition: resampling_audio.c:65
swr_get_delay
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.
Definition: swresample.c:955
get_format_from_sample_fmt
static int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt)
Definition: resampling_audio.c:33
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:191
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:167
av_get_sample_fmt_name
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.
Definition: samplefmt.c:51
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
NULL
#define NULL
Definition: coverity.c:32
swresample.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:624
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
av_opt_set_chlayout
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition: opt.c:786
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:173
swr_convert
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t **out_arg, int out_count, const uint8_t **in_arg, int in_count)
Convert audio.
Definition: swresample.c:800
printf
printf("static const uint8_t my_array[100] = {\n")
M_PI
#define M_PI
Definition: mathematics.h:52
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
av_samples_get_buffer_size
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.
Definition: samplefmt.c:121
ret
ret
Definition: filter_design.txt:187
channel_layout.h
av_channel_layout_describe
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.
Definition: channel_layout.c:776
av_samples_alloc_array_and_samples
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...
Definition: samplefmt.c:207
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
av_opt_set_sample_fmt
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:742
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60