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;
86 *data =
av_malloc(
sizeof(*data) * nb_planes);
90 nb_samples, sample_fmt, align);
93 int main(
int argc,
char **argv)
96 int src_rate = 48000, dst_rate = 44100;
98 int src_nb_channels = 0, dst_nb_channels = 0;
99 int src_linesize, dst_linesize;
100 int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
102 const char *dst_filename =
NULL;
111 fprintf(stderr,
"Usage: %s output_file\n"
112 "API example program to show how to resample an audio stream with libswresample.\n"
113 "This program generates a series of audio frames, resamples them to a specified "
114 "output format and rate and saves them to an output file named output_file.\n",
118 dst_filename = argv[1];
120 dst_file = fopen(dst_filename,
"wb");
122 fprintf(stderr,
"Could not open destination file %s\n", dst_filename);
129 fprintf(stderr,
"Could not allocate resampler context\n");
144 if ((ret =
swr_init(swr_ctx)) < 0) {
145 fprintf(stderr,
"Failed to initialize the resampling context\n");
153 src_nb_samples, src_sample_fmt, 0);
155 fprintf(stderr,
"Could not allocate source samples\n");
162 max_dst_nb_samples = dst_nb_samples =
168 dst_nb_samples, dst_sample_fmt, 0);
170 fprintf(stderr,
"Could not allocate destination samples\n");
177 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
182 if (dst_nb_samples > max_dst_nb_samples) {
185 dst_nb_samples, dst_sample_fmt, 1);
188 max_dst_nb_samples = dst_nb_samples;
192 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
194 fprintf(stderr,
"Error while converting\n");
198 ret, dst_sample_fmt, 1);
199 printf(
"t:%f in:%d out:%d\n", t, src_nb_samples, ret);
200 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
205 fprintf(stderr,
"Resampling succeeded. Play the output file with the command:\n"
206 "ffplay -f %s -channel_layout %"PRId64
" -channels %d -ar %d %s\n",
207 fmt, dst_ch_layout, dst_nb_channels, dst_rate, dst_filename);