audio-resampler-ffmpeg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "../util/bmem.h"
  15. #include "audio-resampler.h"
  16. #include <libswresample/swresample.h>
  17. #include <libavutil/channel_layout.h>
  18. #include <libavutil/opt.h>
  19. struct audio_resampler {
  20. struct SwrContext *context;
  21. bool opened;
  22. uint32_t input_freq;
  23. uint64_t input_layout;
  24. enum AVSampleFormat input_format;
  25. uint8_t *output_buffer;
  26. uint64_t output_layout;
  27. enum AVSampleFormat output_format;
  28. int output_size;
  29. uint32_t output_ch;
  30. uint32_t output_freq;
  31. };
  32. static inline enum AVSampleFormat convert_audio_format(enum audio_format format)
  33. {
  34. switch (format) {
  35. case AUDIO_FORMAT_UNKNOWN: return AV_SAMPLE_FMT_S16;
  36. case AUDIO_FORMAT_U8BIT: return AV_SAMPLE_FMT_U8;
  37. case AUDIO_FORMAT_16BIT: return AV_SAMPLE_FMT_S16;
  38. case AUDIO_FORMAT_32BIT: return AV_SAMPLE_FMT_S32;
  39. case AUDIO_FORMAT_FLOAT: return AV_SAMPLE_FMT_FLT;
  40. }
  41. /* shouldn't get here */
  42. return AV_SAMPLE_FMT_S16;
  43. }
  44. static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
  45. {
  46. switch (layout) {
  47. case SPEAKERS_UNKNOWN: return 0;
  48. case SPEAKERS_MONO: return AV_CH_LAYOUT_MONO;
  49. case SPEAKERS_STEREO: return AV_CH_LAYOUT_STEREO;
  50. case SPEAKERS_2POINT1: return AV_CH_LAYOUT_2_1;
  51. case SPEAKERS_QUAD: return AV_CH_LAYOUT_QUAD;
  52. case SPEAKERS_4POINT1: return AV_CH_LAYOUT_4POINT1;
  53. case SPEAKERS_5POINT1: return AV_CH_LAYOUT_5POINT1;
  54. case SPEAKERS_5POINT1_SURROUND: return AV_CH_LAYOUT_5POINT1_BACK;
  55. case SPEAKERS_7POINT1: return AV_CH_LAYOUT_7POINT1;
  56. case SPEAKERS_7POINT1_SURROUND: return AV_CH_LAYOUT_7POINT1_WIDE_BACK;
  57. case SPEAKERS_SURROUND: return AV_CH_LAYOUT_SURROUND;
  58. }
  59. /* shouldn't get here */
  60. return 0;
  61. }
  62. audio_resampler_t audio_resampler_create(struct resample_info *dst,
  63. struct resample_info *src)
  64. {
  65. struct audio_resampler *rs = bmalloc(sizeof(struct audio_resampler));
  66. int errcode;
  67. rs->opened = false;
  68. rs->input_freq = src->samples_per_sec;
  69. rs->input_layout = convert_speaker_layout(src->speakers);
  70. rs->input_format = convert_audio_format(src->format);
  71. rs->output_buffer = NULL;
  72. rs->output_size = 0;
  73. rs->output_ch = get_audio_channels(dst->speakers);
  74. rs->output_freq = dst->samples_per_sec;
  75. rs->output_layout = convert_speaker_layout(dst->speakers);
  76. rs->output_format = convert_audio_format(dst->format);
  77. rs->context = swr_alloc_set_opts(NULL,
  78. rs->output_layout, rs->output_format, dst->samples_per_sec,
  79. rs->input_layout, rs->input_format, src->samples_per_sec,
  80. 0, NULL);
  81. if (!rs->context) {
  82. blog(LOG_ERROR, "swr_alloc_set_opts failed");
  83. audio_resampler_destroy(rs);
  84. return NULL;
  85. }
  86. errcode = swr_init(rs->context);
  87. if (errcode != 0) {
  88. blog(LOG_ERROR, "avresample_open failed: error code %d",
  89. errcode);
  90. audio_resampler_destroy(rs);
  91. return NULL;
  92. }
  93. return rs;
  94. }
  95. void audio_resampler_destroy(audio_resampler_t rs)
  96. {
  97. if (rs) {
  98. if (rs->context)
  99. swr_free(&rs->context);
  100. if (rs->output_buffer)
  101. av_freep(&rs->output_buffer);
  102. bfree(rs);
  103. }
  104. }
  105. bool audio_resampler_resample(audio_resampler_t rs,
  106. void **output, uint32_t *out_frames,
  107. const void *input, uint32_t in_frames,
  108. uint64_t *timestamp_offset)
  109. {
  110. struct SwrContext *context = rs->context;
  111. int ret;
  112. int64_t delay = swr_get_delay(context, rs->input_freq);
  113. int estimated = (int)av_rescale_rnd(
  114. delay + (int64_t)in_frames,
  115. (int64_t)rs->output_freq, (int64_t)rs->input_freq,
  116. AV_ROUND_UP);
  117. *timestamp_offset = (uint64_t)swr_get_delay(context, 1000000000);
  118. /* resize the buffer if bigger */
  119. if (estimated > rs->output_size) {
  120. if (rs->output_buffer)
  121. av_freep(&rs->output_buffer);
  122. av_samples_alloc(&rs->output_buffer, NULL, rs->output_ch,
  123. estimated, rs->output_format, 0);
  124. rs->output_size = estimated;
  125. }
  126. ret = swr_convert(context,
  127. &rs->output_buffer, rs->output_size,
  128. (const uint8_t**)&input, in_frames);
  129. if (ret < 0) {
  130. blog(LOG_ERROR, "swr_convert failed: %d", ret);
  131. return false;
  132. }
  133. *output = rs->output_buffer;
  134. *out_frames = (uint32_t)ret;
  135. return true;
  136. }