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) {
61 "Sample format %s not supported as output format\n",
69static void fill_samples(
double *
dst,
int nb_samples,
int nb_channels,
int sample_rate,
double *t)
72 double tincr = 1.0 / sample_rate, *dstp =
dst;
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++)
85int 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");
137 if ((ret =
swr_init(swr_ctx)) < 0) {
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);
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
__device__ int printf(const char *,...)
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.
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.
@ AV_ROUND_UP
Round toward +infinity.
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.
@ AV_SAMPLE_FMT_S32
signed 32 bits
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
@ AV_SAMPLE_FMT_DBL
double
@ AV_SAMPLE_FMT_S16
signed 16 bits
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...
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
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.
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 *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
av_cold 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 *channel_layout, int search_flags)
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, 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.
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
The libswresample context.
libswresample public header