audio-resampler-ffmpeg.c 5.5 KB

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