audio-resampler-ffmpeg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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, const struct resample_info *src)
  90. {
  91. struct audio_resampler *rs = bzalloc(sizeof(struct audio_resampler));
  92. int errcode;
  93. rs->opened = false;
  94. rs->input_freq = src->samples_per_sec;
  95. rs->input_format = convert_audio_format(src->format);
  96. rs->output_size = 0;
  97. rs->output_ch = get_audio_channels(dst->speakers);
  98. rs->output_freq = dst->samples_per_sec;
  99. rs->output_format = convert_audio_format(dst->format);
  100. rs->output_planes = is_audio_planar(dst->format) ? rs->output_ch : 1;
  101. #if (LIBSWRESAMPLE_VERSION_INT < AV_VERSION_INT(4, 5, 100))
  102. rs->input_layout = convert_speaker_layout(src->speakers);
  103. rs->output_layout = convert_speaker_layout(dst->speakers);
  104. rs->context = swr_alloc_set_opts(NULL, rs->output_layout, rs->output_format, dst->samples_per_sec,
  105. rs->input_layout, rs->input_format, src->samples_per_sec, 0, NULL);
  106. #else
  107. int nb_ch = get_audio_channels(src->speakers);
  108. av_channel_layout_default(&rs->input_ch_layout, nb_ch);
  109. av_channel_layout_default(&rs->output_ch_layout, rs->output_ch);
  110. if (src->speakers == SPEAKERS_4POINT1)
  111. rs->input_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  112. if (dst->speakers == SPEAKERS_4POINT1)
  113. rs->output_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  114. swr_alloc_set_opts2(&rs->context, &rs->output_ch_layout, rs->output_format, dst->samples_per_sec,
  115. &rs->input_ch_layout, rs->input_format, src->samples_per_sec, 0, NULL);
  116. #endif
  117. if (!rs->context) {
  118. blog(LOG_ERROR, "swr_alloc_set_opts failed");
  119. audio_resampler_destroy(rs);
  120. return NULL;
  121. }
  122. #if (LIBSWRESAMPLE_VERSION_INT < AV_VERSION_INT(4, 5, 100))
  123. if (rs->input_layout == AV_CH_LAYOUT_MONO && rs->output_ch > 1) {
  124. #else
  125. AVChannelLayout test_ch = AV_CHANNEL_LAYOUT_MONO;
  126. if (av_channel_layout_compare(&rs->input_ch_layout, &test_ch) == 0 && rs->output_ch > 1) {
  127. #endif
  128. const double matrix[MAX_AUDIO_CHANNELS][MAX_AUDIO_CHANNELS] = {
  129. {1},
  130. {1, 1},
  131. {1, 1, 0},
  132. {1, 1, 1, 1},
  133. {1, 1, 1, 0, 1},
  134. {1, 1, 1, 1, 1, 1},
  135. {1, 1, 1, 0, 1, 1, 1},
  136. {1, 1, 1, 0, 1, 1, 1, 1},
  137. };
  138. if (swr_set_matrix(rs->context, matrix[rs->output_ch - 1], 1) < 0)
  139. blog(LOG_DEBUG, "swr_set_matrix failed for mono upmix\n");
  140. }
  141. errcode = swr_init(rs->context);
  142. if (errcode != 0) {
  143. blog(LOG_ERROR, "avresample_open failed: error code %d", errcode);
  144. audio_resampler_destroy(rs);
  145. return NULL;
  146. }
  147. return rs;
  148. }
  149. void audio_resampler_destroy(audio_resampler_t *rs)
  150. {
  151. if (rs) {
  152. if (rs->context)
  153. swr_free(&rs->context);
  154. if (rs->output_buffer[0])
  155. av_freep(&rs->output_buffer[0]);
  156. bfree(rs);
  157. }
  158. }
  159. bool audio_resampler_resample(audio_resampler_t *rs, uint8_t *output[], uint32_t *out_frames, uint64_t *ts_offset,
  160. const uint8_t *const input[], uint32_t in_frames)
  161. {
  162. if (!rs)
  163. return false;
  164. struct SwrContext *context = rs->context;
  165. int ret;
  166. int64_t delay = swr_get_delay(context, rs->input_freq);
  167. int estimated = (int)av_rescale_rnd(delay + (int64_t)in_frames, (int64_t)rs->output_freq,
  168. (int64_t)rs->input_freq, AV_ROUND_UP);
  169. *ts_offset = (uint64_t)swr_get_delay(context, 1000000000);
  170. /* resize the buffer if bigger */
  171. if (estimated > rs->output_size) {
  172. if (rs->output_buffer[0])
  173. av_freep(&rs->output_buffer[0]);
  174. av_samples_alloc(rs->output_buffer, NULL, rs->output_ch, estimated, rs->output_format, 0);
  175. rs->output_size = estimated;
  176. }
  177. ret = swr_convert(context, rs->output_buffer, rs->output_size, (const uint8_t **)input, in_frames);
  178. if (ret < 0) {
  179. blog(LOG_ERROR, "swr_convert failed: %d", ret);
  180. return false;
  181. }
  182. for (uint32_t i = 0; i < rs->output_planes; i++)
  183. output[i] = rs->output_buffer[i];
  184. *out_frames = (uint32_t)ret;
  185. return true;
  186. }