1
0

audio-resampler-ffmpeg.c 6.5 KB

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