audio-resampler-ffmpeg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "audio-io.h"
  17. #include <libavutil/avutil.h>
  18. #include <libavformat/avformat.h>
  19. #include <libswresample/swresample.h>
  20. struct audio_resampler {
  21. struct SwrContext *context;
  22. bool opened;
  23. uint32_t input_freq;
  24. uint64_t input_layout;
  25. enum AVSampleFormat input_format;
  26. uint8_t *output_buffer[MAX_AV_PLANES];
  27. uint64_t output_layout;
  28. enum AVSampleFormat output_format;
  29. int output_size;
  30. uint32_t output_ch;
  31. uint32_t output_freq;
  32. uint32_t output_planes;
  33. };
  34. static inline enum AVSampleFormat convert_audio_format(enum audio_format format)
  35. {
  36. switch (format) {
  37. case AUDIO_FORMAT_UNKNOWN: return AV_SAMPLE_FMT_S16;
  38. case AUDIO_FORMAT_U8BIT: return AV_SAMPLE_FMT_U8;
  39. case AUDIO_FORMAT_16BIT: return AV_SAMPLE_FMT_S16;
  40. case AUDIO_FORMAT_32BIT: return AV_SAMPLE_FMT_S32;
  41. case AUDIO_FORMAT_FLOAT: return AV_SAMPLE_FMT_FLT;
  42. case AUDIO_FORMAT_U8BIT_PLANAR: return AV_SAMPLE_FMT_U8P;
  43. case AUDIO_FORMAT_16BIT_PLANAR: return AV_SAMPLE_FMT_S16P;
  44. case AUDIO_FORMAT_32BIT_PLANAR: return AV_SAMPLE_FMT_S32P;
  45. case AUDIO_FORMAT_FLOAT_PLANAR: return AV_SAMPLE_FMT_FLTP;
  46. }
  47. /* shouldn't get here */
  48. return AV_SAMPLE_FMT_S16;
  49. }
  50. static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
  51. {
  52. switch (layout) {
  53. case SPEAKERS_UNKNOWN: return 0;
  54. case SPEAKERS_MONO: return AV_CH_LAYOUT_MONO;
  55. case SPEAKERS_STEREO: return AV_CH_LAYOUT_STEREO;
  56. case SPEAKERS_2POINT1: return AV_CH_LAYOUT_SURROUND;
  57. case SPEAKERS_4POINT0: return AV_CH_LAYOUT_4POINT0;
  58. case SPEAKERS_4POINT1: return AV_CH_LAYOUT_4POINT1;
  59. case SPEAKERS_5POINT1: return AV_CH_LAYOUT_5POINT1_BACK;
  60. case SPEAKERS_7POINT1: return AV_CH_LAYOUT_7POINT1;
  61. }
  62. /* shouldn't get here */
  63. return 0;
  64. }
  65. audio_resampler_t *audio_resampler_create(const struct resample_info *dst,
  66. const struct resample_info *src)
  67. {
  68. struct audio_resampler *rs = bzalloc(sizeof(struct audio_resampler));
  69. int errcode;
  70. rs->opened = false;
  71. rs->input_freq = src->samples_per_sec;
  72. rs->input_layout = convert_speaker_layout(src->speakers);
  73. rs->input_format = convert_audio_format(src->format);
  74. rs->output_size = 0;
  75. rs->output_ch = get_audio_channels(dst->speakers);
  76. rs->output_freq = dst->samples_per_sec;
  77. rs->output_layout = convert_speaker_layout(dst->speakers);
  78. rs->output_format = convert_audio_format(dst->format);
  79. rs->output_planes = is_audio_planar(dst->format) ? rs->output_ch : 1;
  80. rs->context = swr_alloc_set_opts(NULL,
  81. rs->output_layout, rs->output_format, dst->samples_per_sec,
  82. rs->input_layout, rs->input_format, src->samples_per_sec,
  83. 0, NULL);
  84. if (!rs->context) {
  85. blog(LOG_ERROR, "swr_alloc_set_opts failed");
  86. audio_resampler_destroy(rs);
  87. return NULL;
  88. }
  89. if (rs->input_layout == AV_CH_LAYOUT_MONO && rs->output_ch > 1) {
  90. const double matrix[MAX_AUDIO_CHANNELS][MAX_AUDIO_CHANNELS] = {
  91. {1},
  92. {1, 1},
  93. {1, 1, 0},
  94. {1, 1, 1, 1},
  95. {1, 1, 1, 0, 1},
  96. {1, 1, 1, 1, 1, 1},
  97. {1, 1, 1, 0, 1, 1, 1},
  98. {1, 1, 1, 0, 1, 1, 1, 1},
  99. };
  100. if (swr_set_matrix(rs->context, matrix[rs->output_ch - 1], 1) < 0)
  101. blog(LOG_DEBUG, "swr_set_matrix failed for mono upmix\n");
  102. }
  103. errcode = swr_init(rs->context);
  104. if (errcode != 0) {
  105. blog(LOG_ERROR, "avresample_open failed: error code %d",
  106. errcode);
  107. audio_resampler_destroy(rs);
  108. return NULL;
  109. }
  110. return rs;
  111. }
  112. void audio_resampler_destroy(audio_resampler_t *rs)
  113. {
  114. if (rs) {
  115. if (rs->context)
  116. swr_free(&rs->context);
  117. if (rs->output_buffer[0])
  118. av_freep(&rs->output_buffer[0]);
  119. bfree(rs);
  120. }
  121. }
  122. bool audio_resampler_resample(audio_resampler_t *rs,
  123. uint8_t *output[], uint32_t *out_frames, uint64_t *ts_offset,
  124. const uint8_t *const input[], uint32_t in_frames)
  125. {
  126. if (!rs) return false;
  127. struct SwrContext *context = rs->context;
  128. int ret;
  129. int64_t delay = swr_get_delay(context, rs->input_freq);
  130. int estimated = (int)av_rescale_rnd(
  131. delay + (int64_t)in_frames,
  132. (int64_t)rs->output_freq, (int64_t)rs->input_freq,
  133. AV_ROUND_UP);
  134. *ts_offset = (uint64_t)swr_get_delay(context, 1000000000);
  135. /* resize the buffer if bigger */
  136. if (estimated > rs->output_size) {
  137. if (rs->output_buffer[0])
  138. av_freep(&rs->output_buffer[0]);
  139. av_samples_alloc(rs->output_buffer, NULL, rs->output_ch,
  140. estimated, rs->output_format, 0);
  141. rs->output_size = estimated;
  142. }
  143. ret = swr_convert(context,
  144. rs->output_buffer, rs->output_size,
  145. (const uint8_t**)input, in_frames);
  146. if (ret < 0) {
  147. blog(LOG_ERROR, "swr_convert failed: %d", ret);
  148. return false;
  149. }
  150. for (uint32_t i = 0; i < rs->output_planes; i++)
  151. output[i] = rs->output_buffer[i];
  152. *out_frames = (uint32_t)ret;
  153. return true;
  154. }