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