Go to the documentation of this file.
41 struct sample_fmt_entry {
43 } sample_fmt_entries[] = {
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);
61 "Sample format %s not supported as output format\n",
73 const double c = 2 *
M_PI * 440.0;
76 for (
i = 0;
i < nb_samples;
i++) {
78 for (j = 1; j < nb_channels; j++)
85 int main(
int argc,
char **argv)
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;
94 const char *dst_filename =
NULL;
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",
111 dst_filename = argv[1];
113 dst_file = fopen(dst_filename,
"wb");
115 fprintf(stderr,
"Could not open destination file %s\n", dst_filename);
122 fprintf(stderr,
"Could not allocate resampler context\n");
138 fprintf(stderr,
"Failed to initialize the resampling context\n");
146 src_nb_samples, src_sample_fmt, 0);
148 fprintf(stderr,
"Could not allocate source samples\n");
155 max_dst_nb_samples = dst_nb_samples =
159 dst_nb_channels = dst_ch_layout.nb_channels;
161 dst_nb_samples, dst_sample_fmt, 0);
163 fprintf(stderr,
"Could not allocate destination samples\n");
170 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
175 if (dst_nb_samples > max_dst_nb_samples) {
178 dst_nb_samples, dst_sample_fmt, 1);
181 max_dst_nb_samples = dst_nb_samples;
185 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
187 fprintf(stderr,
"Error while converting\n");
191 ret, dst_sample_fmt, 1);
192 if (dst_bufsize < 0) {
193 fprintf(stderr,
"Could not get sample buffer size\n");
196 printf(
"t:%f in:%d out:%d\n", t, src_nb_samples,
ret);
197 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
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);
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
#define AV_CHANNEL_LAYOUT_STEREO
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.
int nb_channels
Number of channels in this layout.
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.
@ AV_ROUND_UP
Round toward +infinity.
#define AV_CHANNEL_LAYOUT_SURROUND
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.
#define FF_ARRAY_ELEMS(a)
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
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.
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
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.
static int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt)
The libswresample context.
int main(int argc, char **argv)
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
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
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.
An AVChannelLayout holds information about the channel layout of audio data.
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
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.
printf("static const uint8_t my_array[100] = {\n")
#define i(width, name, range_min, range_max)
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
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.
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...
@ AV_SAMPLE_FMT_DBL
double
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
@ AV_SAMPLE_FMT_S32
signed 32 bits