resample_audio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file audio resampling API usage example
  24. * @example resample_audio.c
  25. *
  26. * Generate a synthetic audio signal, and Use libswresample API to perform audio
  27. * resampling. The output is written to a raw audio file to be played with
  28. * ffplay.
  29. */
  30. #include <libavutil/opt.h>
  31. #include <libavutil/channel_layout.h>
  32. #include <libavutil/samplefmt.h>
  33. #include <libswresample/swresample.h>
  34. static int get_format_from_sample_fmt(const char **fmt,
  35. enum AVSampleFormat sample_fmt)
  36. {
  37. int i;
  38. struct sample_fmt_entry {
  39. enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
  40. } sample_fmt_entries[] = {
  41. { AV_SAMPLE_FMT_U8, "u8", "u8" },
  42. { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
  43. { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
  44. { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
  45. { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
  46. };
  47. *fmt = NULL;
  48. for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
  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);
  52. return 0;
  53. }
  54. }
  55. fprintf(stderr,
  56. "Sample format %s not supported as output format\n",
  57. av_get_sample_fmt_name(sample_fmt));
  58. return AVERROR(EINVAL);
  59. }
  60. /**
  61. * Fill dst buffer with nb_samples, generated starting from t.
  62. */
  63. static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
  64. {
  65. int i, j;
  66. double tincr = 1.0 / sample_rate, *dstp = dst;
  67. const double c = 2 * M_PI * 440.0;
  68. /* generate sin tone with 440Hz frequency and duplicated channels */
  69. for (i = 0; i < nb_samples; i++) {
  70. *dstp = sin(c * *t);
  71. for (j = 1; j < nb_channels; j++)
  72. dstp[j] = dstp[0];
  73. dstp += nb_channels;
  74. *t += tincr;
  75. }
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. AVChannelLayout src_ch_layout = AV_CHANNEL_LAYOUT_STEREO, dst_ch_layout = AV_CHANNEL_LAYOUT_SURROUND;
  80. int src_rate = 48000, dst_rate = 44100;
  81. uint8_t **src_data = NULL, **dst_data = NULL;
  82. int src_nb_channels = 0, dst_nb_channels = 0;
  83. int src_linesize, dst_linesize;
  84. int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
  85. enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
  86. const char *dst_filename = NULL;
  87. FILE *dst_file;
  88. int dst_bufsize;
  89. const char *fmt;
  90. struct SwrContext *swr_ctx;
  91. char buf[64];
  92. double t;
  93. int ret;
  94. if (argc != 2) {
  95. fprintf(stderr, "Usage: %s output_file\n"
  96. "API example program to show how to resample an audio stream with libswresample.\n"
  97. "This program generates a series of audio frames, resamples them to a specified "
  98. "output format and rate and saves them to an output file named output_file.\n",
  99. argv[0]);
  100. exit(1);
  101. }
  102. dst_filename = argv[1];
  103. dst_file = fopen(dst_filename, "wb");
  104. if (!dst_file) {
  105. fprintf(stderr, "Could not open destination file %s\n", dst_filename);
  106. exit(1);
  107. }
  108. /* create resampler context */
  109. swr_ctx = swr_alloc();
  110. if (!swr_ctx) {
  111. fprintf(stderr, "Could not allocate resampler context\n");
  112. ret = AVERROR(ENOMEM);
  113. goto end;
  114. }
  115. /* set options */
  116. av_opt_set_chlayout(swr_ctx, "in_chlayout", &src_ch_layout, 0);
  117. av_opt_set_int(swr_ctx, "in_sample_rate", src_rate, 0);
  118. av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
  119. av_opt_set_chlayout(swr_ctx, "out_chlayout", &dst_ch_layout, 0);
  120. av_opt_set_int(swr_ctx, "out_sample_rate", dst_rate, 0);
  121. av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
  122. /* initialize the resampling context */
  123. if ((ret = swr_init(swr_ctx)) < 0) {
  124. fprintf(stderr, "Failed to initialize the resampling context\n");
  125. goto end;
  126. }
  127. /* allocate source and destination samples buffers */
  128. src_nb_channels = src_ch_layout.nb_channels;
  129. ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
  130. src_nb_samples, src_sample_fmt, 0);
  131. if (ret < 0) {
  132. fprintf(stderr, "Could not allocate source samples\n");
  133. goto end;
  134. }
  135. /* compute the number of converted samples: buffering is avoided
  136. * ensuring that the output buffer will contain at least all the
  137. * converted input samples */
  138. max_dst_nb_samples = dst_nb_samples =
  139. av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
  140. /* buffer is going to be directly written to a rawaudio file, no alignment */
  141. dst_nb_channels = dst_ch_layout.nb_channels;
  142. ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
  143. dst_nb_samples, dst_sample_fmt, 0);
  144. if (ret < 0) {
  145. fprintf(stderr, "Could not allocate destination samples\n");
  146. goto end;
  147. }
  148. t = 0;
  149. do {
  150. /* generate synthetic audio */
  151. fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
  152. /* compute destination number of samples */
  153. dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
  154. src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
  155. if (dst_nb_samples > max_dst_nb_samples) {
  156. av_freep(&dst_data[0]);
  157. ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
  158. dst_nb_samples, dst_sample_fmt, 1);
  159. if (ret < 0)
  160. break;
  161. max_dst_nb_samples = dst_nb_samples;
  162. }
  163. /* convert to destination format */
  164. ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
  165. if (ret < 0) {
  166. fprintf(stderr, "Error while converting\n");
  167. goto end;
  168. }
  169. dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
  170. ret, dst_sample_fmt, 1);
  171. if (dst_bufsize < 0) {
  172. fprintf(stderr, "Could not get sample buffer size\n");
  173. goto end;
  174. }
  175. printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
  176. fwrite(dst_data[0], 1, dst_bufsize, dst_file);
  177. } while (t < 10);
  178. if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
  179. goto end;
  180. av_channel_layout_describe(&dst_ch_layout, buf, sizeof(buf));
  181. fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
  182. "ffplay -f %s -channel_layout %s -channels %d -ar %d %s\n",
  183. fmt, buf, dst_nb_channels, dst_rate, dst_filename);
  184. end:
  185. fclose(dst_file);
  186. if (src_data)
  187. av_freep(&src_data[0]);
  188. av_freep(&src_data);
  189. if (dst_data)
  190. av_freep(&dst_data[0]);
  191. av_freep(&dst_data);
  192. swr_free(&swr_ctx);
  193. return ret < 0;
  194. }