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",
65 static void fill_samples(
double *dst,
int nb_samples,
int nb_channels,
int sample_rate,
double *t)
68 double tincr = 1.0 / sample_rate, *dstp = dst;
69 const double c = 2 *
M_PI * 440.0;
72 for (i = 0; i < nb_samples; i++) {
74 for (j = 1; j < nb_channels; j++)
81 int main(
int argc,
char **argv)
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;
90 const char *dst_filename = NULL;
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",
107 dst_filename = argv[1];
109 dst_file = fopen(dst_filename,
"wb");
111 fprintf(stderr,
"Could not open destination file %s\n", dst_filename);
118 fprintf(stderr,
"Could not allocate resampler context\n");
133 if ((ret =
swr_init(swr_ctx)) < 0) {
134 fprintf(stderr,
"Failed to initialize the resampling context\n");
142 src_nb_samples, src_sample_fmt, 0);
144 fprintf(stderr,
"Could not allocate source samples\n");
151 max_dst_nb_samples = dst_nb_samples =
155 dst_nb_channels = dst_ch_layout.nb_channels;
157 dst_nb_samples, dst_sample_fmt, 0);
159 fprintf(stderr,
"Could not allocate destination samples\n");
166 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
171 if (dst_nb_samples > max_dst_nb_samples) {
174 dst_nb_samples, dst_sample_fmt, 1);
177 max_dst_nb_samples = dst_nb_samples;
181 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
183 fprintf(stderr,
"Error while converting\n");
187 ret, dst_sample_fmt, 1);
188 if (dst_bufsize < 0) {
189 fprintf(stderr,
"Could not get sample buffer size\n");
192 printf(
"t:%f in:%d out:%d\n", t, src_nb_samples, ret);
193 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
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);