37 struct sample_fmt_entry {
39 } sample_fmt_entries[] = {
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",
69 const double c = 2 *
M_PI * 440.0;
72 for (i = 0; i < nb_samples; i++) {
81 int main(
int argc,
char **argv)
84 int src_rate = 48000, dst_rate = 44100;
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;
90 const char *dst_filename =
NULL;
99 fprintf(stderr,
"Usage: %s output_file\n"
100 "API example program to show how to resample an audio stream with libswresample.\n"
101 "This program generates a series of audio frames, resamples them to a specified "
102 "output format and rate and saves them to an output file named output_file.\n",
106 dst_filename = argv[1];
108 dst_file = fopen(dst_filename,
"wb");
110 fprintf(stderr,
"Could not open destination file %s\n", dst_filename);
117 fprintf(stderr,
"Could not allocate resampler context\n");
132 if ((ret =
swr_init(swr_ctx)) < 0) {
133 fprintf(stderr,
"Failed to initialize the resampling context\n");
141 src_nb_samples, src_sample_fmt, 0);
143 fprintf(stderr,
"Could not allocate source samples\n");
150 max_dst_nb_samples = dst_nb_samples =
156 dst_nb_samples, dst_sample_fmt, 0);
158 fprintf(stderr,
"Could not allocate destination samples\n");
165 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
170 if (dst_nb_samples > max_dst_nb_samples) {
173 dst_nb_samples, dst_sample_fmt, 1);
176 max_dst_nb_samples = dst_nb_samples;
180 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
182 fprintf(stderr,
"Error while converting\n");
186 ret, dst_sample_fmt, 1);
187 if (dst_bufsize < 0) {
188 fprintf(stderr,
"Could not get sample buffer size\n");
191 printf(
"t:%f in:%d out:%d\n", t, src_nb_samples, ret);
192 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
197 fprintf(stderr,
"Resampling succeeded. Play the output file with the command:\n"
198 "ffplay -f %s -channel_layout %"PRId64
" -channels %d -ar %d %s\n",
199 fmt, dst_ch_layout, dst_nb_channels, dst_rate, dst_filename);
static int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt)
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.
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_STEREO
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...
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
static av_cold int end(AVCodecContext *avctx)
int main(int argc, char **argv)
libswresample public header
The libswresample context.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
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.
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.
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.
audio channel layout utility functions
#define FF_ARRAY_ELEMS(a)
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...
AVSampleFormat
Audio sample formats.
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
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 attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count, const uint8_t *in_arg[SWR_CH_MAX], int in_count)
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.