audio-io.h 5.0 KB

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