audio-io.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #pragma once
  15. #include "media-io-defs.h"
  16. #include "../util/c99defs.h"
  17. #include "../util/util_uint64.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define MAX_AUDIO_MIXES 6
  22. #define MAX_AUDIO_CHANNELS 8
  23. #define MAX_DEVICE_INPUT_CHANNELS 64
  24. #define AUDIO_OUTPUT_FRAMES 1024
  25. #define TOTAL_AUDIO_SIZE \
  26. (MAX_AUDIO_MIXES * MAX_AUDIO_CHANNELS * AUDIO_OUTPUT_FRAMES * \
  27. sizeof(float))
  28. /*
  29. * Base audio output component. Use this to create an audio output track
  30. * for the media.
  31. */
  32. struct audio_output;
  33. typedef struct audio_output audio_t;
  34. enum audio_format {
  35. AUDIO_FORMAT_UNKNOWN,
  36. AUDIO_FORMAT_U8BIT,
  37. AUDIO_FORMAT_16BIT,
  38. AUDIO_FORMAT_32BIT,
  39. AUDIO_FORMAT_FLOAT,
  40. AUDIO_FORMAT_U8BIT_PLANAR,
  41. AUDIO_FORMAT_16BIT_PLANAR,
  42. AUDIO_FORMAT_32BIT_PLANAR,
  43. AUDIO_FORMAT_FLOAT_PLANAR,
  44. };
  45. /**
  46. * The speaker layout describes where the speakers are located in the room.
  47. * For OBS it dictates:
  48. * * how many channels are available and
  49. * * which channels are used for which speakers.
  50. *
  51. * Standard channel layouts where retrieved from ffmpeg documentation at:
  52. * https://trac.ffmpeg.org/wiki/AudioChannelManipulation
  53. */
  54. enum speaker_layout {
  55. SPEAKERS_UNKNOWN, /**< Unknown setting, fallback is stereo. */
  56. SPEAKERS_MONO, /**< Channels: MONO */
  57. SPEAKERS_STEREO, /**< Channels: FL, FR */
  58. SPEAKERS_2POINT1, /**< Channels: FL, FR, LFE */
  59. SPEAKERS_4POINT0, /**< Channels: FL, FR, FC, RC */
  60. SPEAKERS_4POINT1, /**< Channels: FL, FR, FC, LFE, RC */
  61. SPEAKERS_5POINT1, /**< Channels: FL, FR, FC, LFE, RL, RR */
  62. SPEAKERS_7POINT1 = 8, /**< Channels: FL, FR, FC, LFE, RL, RR, SL, SR */
  63. };
  64. struct audio_data {
  65. uint8_t *data[MAX_AV_PLANES];
  66. uint32_t frames;
  67. uint64_t timestamp;
  68. };
  69. struct audio_output_data {
  70. float *data[MAX_AUDIO_CHANNELS];
  71. };
  72. typedef bool (*audio_input_callback_t)(void *param, uint64_t start_ts,
  73. uint64_t end_ts, uint64_t *new_ts,
  74. uint32_t active_mixers,
  75. struct audio_output_data *mixes);
  76. struct audio_output_info {
  77. const char *name;
  78. uint32_t samples_per_sec;
  79. enum audio_format format;
  80. enum speaker_layout speakers;
  81. audio_input_callback_t input_callback;
  82. void *input_param;
  83. };
  84. struct audio_convert_info {
  85. uint32_t samples_per_sec;
  86. enum audio_format format;
  87. enum speaker_layout speakers;
  88. bool allow_clipping;
  89. };
  90. static inline uint32_t get_audio_channels(enum speaker_layout speakers)
  91. {
  92. switch (speakers) {
  93. case SPEAKERS_MONO:
  94. return 1;
  95. case SPEAKERS_STEREO:
  96. return 2;
  97. case SPEAKERS_2POINT1:
  98. return 3;
  99. case SPEAKERS_4POINT0:
  100. return 4;
  101. case SPEAKERS_4POINT1:
  102. return 5;
  103. case SPEAKERS_5POINT1:
  104. return 6;
  105. case SPEAKERS_7POINT1:
  106. return 8;
  107. case SPEAKERS_UNKNOWN:
  108. return 0;
  109. }
  110. return 0;
  111. }
  112. static inline size_t get_audio_bytes_per_channel(enum audio_format format)
  113. {
  114. switch (format) {
  115. case AUDIO_FORMAT_U8BIT:
  116. case AUDIO_FORMAT_U8BIT_PLANAR:
  117. return 1;
  118. case AUDIO_FORMAT_16BIT:
  119. case AUDIO_FORMAT_16BIT_PLANAR:
  120. return 2;
  121. case AUDIO_FORMAT_FLOAT:
  122. case AUDIO_FORMAT_FLOAT_PLANAR:
  123. case AUDIO_FORMAT_32BIT:
  124. case AUDIO_FORMAT_32BIT_PLANAR:
  125. return 4;
  126. case AUDIO_FORMAT_UNKNOWN:
  127. return 0;
  128. }
  129. return 0;
  130. }
  131. static inline bool is_audio_planar(enum audio_format format)
  132. {
  133. switch (format) {
  134. case AUDIO_FORMAT_U8BIT:
  135. case AUDIO_FORMAT_16BIT:
  136. case AUDIO_FORMAT_32BIT:
  137. case AUDIO_FORMAT_FLOAT:
  138. return false;
  139. case AUDIO_FORMAT_U8BIT_PLANAR:
  140. case AUDIO_FORMAT_FLOAT_PLANAR:
  141. case AUDIO_FORMAT_16BIT_PLANAR:
  142. case AUDIO_FORMAT_32BIT_PLANAR:
  143. return true;
  144. case AUDIO_FORMAT_UNKNOWN:
  145. return false;
  146. }
  147. return false;
  148. }
  149. static inline size_t get_audio_planes(enum audio_format format,
  150. enum speaker_layout speakers)
  151. {
  152. return (is_audio_planar(format) ? get_audio_channels(speakers) : 1);
  153. }
  154. static inline size_t get_audio_size(enum audio_format format,
  155. enum speaker_layout speakers,
  156. uint32_t frames)
  157. {
  158. bool planar = is_audio_planar(format);
  159. return (planar ? 1 : get_audio_channels(speakers)) *
  160. get_audio_bytes_per_channel(format) * frames;
  161. }
  162. static inline size_t get_total_audio_size(enum audio_format format,
  163. enum speaker_layout speakers,
  164. uint32_t frames)
  165. {
  166. return get_audio_channels(speakers) *
  167. get_audio_bytes_per_channel(format) * frames;
  168. }
  169. static inline uint64_t audio_frames_to_ns(size_t sample_rate, uint64_t frames)
  170. {
  171. return util_mul_div64(frames, 1000000000ULL, sample_rate);
  172. }
  173. static inline uint64_t ns_to_audio_frames(size_t sample_rate, uint64_t frames)
  174. {
  175. return util_mul_div64(frames, sample_rate, 1000000000ULL);
  176. }
  177. #define AUDIO_OUTPUT_SUCCESS 0
  178. #define AUDIO_OUTPUT_INVALIDPARAM -1
  179. #define AUDIO_OUTPUT_FAIL -2
  180. EXPORT int audio_output_open(audio_t **audio, struct audio_output_info *info);
  181. EXPORT void audio_output_close(audio_t *audio);
  182. typedef void (*audio_output_callback_t)(void *param, size_t mix_idx,
  183. struct audio_data *data);
  184. EXPORT bool audio_output_connect(audio_t *video, size_t mix_idx,
  185. const struct audio_convert_info *conversion,
  186. audio_output_callback_t callback, void *param);
  187. EXPORT void audio_output_disconnect(audio_t *video, size_t mix_idx,
  188. audio_output_callback_t callback,
  189. void *param);
  190. EXPORT bool audio_output_active(const audio_t *audio);
  191. EXPORT size_t audio_output_get_block_size(const audio_t *audio);
  192. EXPORT size_t audio_output_get_planes(const audio_t *audio);
  193. EXPORT size_t audio_output_get_channels(const audio_t *audio);
  194. EXPORT uint32_t audio_output_get_sample_rate(const audio_t *audio);
  195. EXPORT const struct audio_output_info *
  196. audio_output_get_info(const audio_t *audio);
  197. #ifdef __cplusplus
  198. }
  199. #endif