audio-io.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #pragma once
  15. #include "media-io-defs.h"
  16. #include "../util/c99defs.h"
  17. #include "../util/util_uint128.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define MAX_AUDIO_MIXES 6
  22. #define MAX_AUDIO_CHANNELS 8
  23. #define AUDIO_OUTPUT_FRAMES 1024
  24. /*
  25. * Base audio output component. Use this to create an audio output track
  26. * for the media.
  27. */
  28. struct audio_output;
  29. typedef struct audio_output audio_t;
  30. enum audio_format {
  31. AUDIO_FORMAT_UNKNOWN,
  32. AUDIO_FORMAT_U8BIT,
  33. AUDIO_FORMAT_16BIT,
  34. AUDIO_FORMAT_32BIT,
  35. AUDIO_FORMAT_FLOAT,
  36. AUDIO_FORMAT_U8BIT_PLANAR,
  37. AUDIO_FORMAT_16BIT_PLANAR,
  38. AUDIO_FORMAT_32BIT_PLANAR,
  39. AUDIO_FORMAT_FLOAT_PLANAR,
  40. };
  41. enum speaker_layout {
  42. SPEAKERS_UNKNOWN,
  43. SPEAKERS_MONO,
  44. SPEAKERS_STEREO,
  45. SPEAKERS_2POINT1,
  46. SPEAKERS_QUAD,
  47. SPEAKERS_4POINT1,
  48. SPEAKERS_5POINT1,
  49. SPEAKERS_5POINT1_SURROUND,
  50. SPEAKERS_7POINT1,
  51. SPEAKERS_7POINT1_SURROUND,
  52. SPEAKERS_SURROUND,
  53. };
  54. struct audio_data {
  55. uint8_t *data[MAX_AV_PLANES];
  56. uint32_t frames;
  57. uint64_t timestamp;
  58. };
  59. struct audio_output_data {
  60. float *data[MAX_AUDIO_CHANNELS];
  61. };
  62. typedef bool (*audio_input_callback_t)(void *param,
  63. uint64_t start_ts, uint64_t end_ts, uint64_t *new_ts,
  64. uint32_t active_mixers, struct audio_output_data *mixes);
  65. struct audio_output_info {
  66. const char *name;
  67. uint32_t samples_per_sec;
  68. enum audio_format format;
  69. enum speaker_layout speakers;
  70. audio_input_callback_t input_callback;
  71. void *input_param;
  72. };
  73. struct audio_convert_info {
  74. uint32_t samples_per_sec;
  75. enum audio_format format;
  76. enum speaker_layout speakers;
  77. };
  78. static inline uint32_t get_audio_channels(enum speaker_layout speakers)
  79. {
  80. switch (speakers) {
  81. case SPEAKERS_MONO: return 1;
  82. case SPEAKERS_STEREO: return 2;
  83. case SPEAKERS_2POINT1:
  84. case SPEAKERS_SURROUND: return 3;
  85. case SPEAKERS_QUAD: return 4;
  86. case SPEAKERS_4POINT1: return 5;
  87. case SPEAKERS_5POINT1:
  88. case SPEAKERS_5POINT1_SURROUND: return 6;
  89. case SPEAKERS_7POINT1:
  90. case SPEAKERS_7POINT1_SURROUND: return 8;
  91. case SPEAKERS_UNKNOWN: return 0;
  92. }
  93. return 0;
  94. }
  95. static inline size_t get_audio_bytes_per_channel(enum audio_format format)
  96. {
  97. switch (format) {
  98. case AUDIO_FORMAT_U8BIT:
  99. case AUDIO_FORMAT_U8BIT_PLANAR:
  100. return 1;
  101. case AUDIO_FORMAT_16BIT:
  102. case AUDIO_FORMAT_16BIT_PLANAR:
  103. return 2;
  104. case AUDIO_FORMAT_FLOAT:
  105. case AUDIO_FORMAT_FLOAT_PLANAR:
  106. case AUDIO_FORMAT_32BIT:
  107. case AUDIO_FORMAT_32BIT_PLANAR:
  108. return 4;
  109. case AUDIO_FORMAT_UNKNOWN:
  110. return 0;
  111. }
  112. return 0;
  113. }
  114. static inline bool is_audio_planar(enum audio_format format)
  115. {
  116. switch (format) {
  117. case AUDIO_FORMAT_U8BIT:
  118. case AUDIO_FORMAT_16BIT:
  119. case AUDIO_FORMAT_32BIT:
  120. case AUDIO_FORMAT_FLOAT:
  121. return false;
  122. case AUDIO_FORMAT_U8BIT_PLANAR:
  123. case AUDIO_FORMAT_FLOAT_PLANAR:
  124. case AUDIO_FORMAT_16BIT_PLANAR:
  125. case AUDIO_FORMAT_32BIT_PLANAR:
  126. return true;
  127. case AUDIO_FORMAT_UNKNOWN:
  128. return false;
  129. }
  130. return false;
  131. }
  132. static inline size_t get_audio_planes(enum audio_format format,
  133. enum speaker_layout speakers)
  134. {
  135. return (is_audio_planar(format) ? get_audio_channels(speakers) : 1);
  136. }
  137. static inline size_t get_audio_size(enum audio_format format,
  138. enum speaker_layout speakers, uint32_t frames)
  139. {
  140. bool planar = is_audio_planar(format);
  141. return (planar ? 1 : get_audio_channels(speakers)) *
  142. get_audio_bytes_per_channel(format) *
  143. frames;
  144. }
  145. static inline uint64_t audio_frames_to_ns(size_t sample_rate,
  146. uint64_t frames)
  147. {
  148. util_uint128_t val;
  149. val = util_mul64_64(frames, 1000000000ULL);
  150. val = util_div128_32(val, (uint32_t)sample_rate);
  151. return val.low;
  152. }
  153. static inline uint64_t ns_to_audio_frames(size_t sample_rate,
  154. uint64_t frames)
  155. {
  156. util_uint128_t val;
  157. val = util_mul64_64(frames, sample_rate);
  158. val = util_div128_32(val, 1000000000);
  159. return val.low;
  160. }
  161. #define AUDIO_OUTPUT_SUCCESS 0
  162. #define AUDIO_OUTPUT_INVALIDPARAM -1
  163. #define AUDIO_OUTPUT_FAIL -2
  164. EXPORT int audio_output_open(audio_t **audio, struct audio_output_info *info);
  165. EXPORT void audio_output_close(audio_t *audio);
  166. typedef void (*audio_output_callback_t)(void *param, size_t mix_idx,
  167. struct audio_data *data);
  168. EXPORT bool audio_output_connect(audio_t *video, size_t mix_idx,
  169. const struct audio_convert_info *conversion,
  170. audio_output_callback_t callback, void *param);
  171. EXPORT void audio_output_disconnect(audio_t *video, size_t mix_idx,
  172. audio_output_callback_t callback, void *param);
  173. EXPORT bool audio_output_active(const audio_t *audio);
  174. EXPORT size_t audio_output_get_block_size(const audio_t *audio);
  175. EXPORT size_t audio_output_get_planes(const audio_t *audio);
  176. EXPORT size_t audio_output_get_channels(const audio_t *audio);
  177. EXPORT uint32_t audio_output_get_sample_rate(const audio_t *audio);
  178. EXPORT const struct audio_output_info *audio_output_get_info(
  179. const audio_t *audio);
  180. #ifdef __cplusplus
  181. }
  182. #endif