audio-encoders.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <map>
  4. #include <memory>
  5. #include <mutex>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. #include "audio-encoders.hpp"
  10. #include "obs-app.hpp"
  11. #include "window-main.hpp"
  12. using namespace std;
  13. static const char *NullToEmpty(const char *str)
  14. {
  15. return str ? str : "";
  16. }
  17. static const char *EncoderName(const std::string &id)
  18. {
  19. return NullToEmpty(obs_encoder_get_display_name(id.c_str()));
  20. }
  21. static map<int, std::string> bitrateMap;
  22. static void HandleIntProperty(obs_property_t *prop, const char *id)
  23. {
  24. const int max_ = obs_property_int_max(prop);
  25. const int step = obs_property_int_step(prop);
  26. for (int i = obs_property_int_min(prop); i <= max_; i += step)
  27. bitrateMap[i] = id;
  28. }
  29. static void HandleListProperty(obs_property_t *prop, const char *id)
  30. {
  31. obs_combo_format format = obs_property_list_format(prop);
  32. if (format != OBS_COMBO_FORMAT_INT) {
  33. blog(LOG_ERROR,
  34. "Encoder '%s' (%s) returned bitrate "
  35. "OBS_PROPERTY_LIST property of unhandled "
  36. "format %d",
  37. EncoderName(id), id, static_cast<int>(format));
  38. return;
  39. }
  40. const size_t count = obs_property_list_item_count(prop);
  41. for (size_t i = 0; i < count; i++) {
  42. if (obs_property_list_item_disabled(prop, i))
  43. continue;
  44. int bitrate =
  45. static_cast<int>(obs_property_list_item_int(prop, i));
  46. bitrateMap[bitrate] = id;
  47. }
  48. }
  49. static void HandleSampleRate(obs_property_t *prop, const char *id)
  50. {
  51. auto ReleaseData = [](obs_data_t *data) { obs_data_release(data); };
  52. std::unique_ptr<obs_data_t, decltype(ReleaseData)> data{
  53. obs_encoder_defaults(id), ReleaseData};
  54. if (!data) {
  55. blog(LOG_ERROR,
  56. "Failed to get defaults for encoder '%s' (%s) "
  57. "while populating bitrate map",
  58. EncoderName(id), id);
  59. return;
  60. }
  61. auto main = reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  62. if (!main) {
  63. blog(LOG_ERROR, "Failed to get main window while populating "
  64. "bitrate map");
  65. return;
  66. }
  67. uint32_t sampleRate =
  68. config_get_uint(main->Config(), "Audio", "SampleRate");
  69. obs_data_set_int(data.get(), "samplerate", sampleRate);
  70. obs_property_modified(prop, data.get());
  71. }
  72. static void HandleEncoderProperties(const char *id)
  73. {
  74. auto DestroyProperties = [](obs_properties_t *props) {
  75. obs_properties_destroy(props);
  76. };
  77. std::unique_ptr<obs_properties_t, decltype(DestroyProperties)> props{
  78. obs_get_encoder_properties(id), DestroyProperties};
  79. if (!props) {
  80. blog(LOG_ERROR,
  81. "Failed to get properties for encoder "
  82. "'%s' (%s)",
  83. EncoderName(id), id);
  84. return;
  85. }
  86. obs_property_t *samplerate =
  87. obs_properties_get(props.get(), "samplerate");
  88. if (samplerate)
  89. HandleSampleRate(samplerate, id);
  90. obs_property_t *bitrate = obs_properties_get(props.get(), "bitrate");
  91. obs_property_type type = obs_property_get_type(bitrate);
  92. switch (type) {
  93. case OBS_PROPERTY_INT:
  94. return HandleIntProperty(bitrate, id);
  95. case OBS_PROPERTY_LIST:
  96. return HandleListProperty(bitrate, id);
  97. default:
  98. break;
  99. }
  100. blog(LOG_ERROR,
  101. "Encoder '%s' (%s) returned bitrate property "
  102. "of unhandled type %d",
  103. EncoderName(id), id, static_cast<int>(type));
  104. }
  105. static const char *GetCodec(const char *id)
  106. {
  107. return NullToEmpty(obs_get_encoder_codec(id));
  108. }
  109. static void PopulateBitrateMap()
  110. {
  111. static once_flag once;
  112. call_once(once, []() {
  113. const string encoders[] = {
  114. "ffmpeg_aac",
  115. "mf_aac",
  116. "libfdk_aac",
  117. "CoreAudio_AAC",
  118. };
  119. const string fallbackEncoder = encoders[0];
  120. struct obs_audio_info aoi;
  121. obs_get_audio_info(&aoi);
  122. uint32_t output_channels = get_audio_channels(aoi.speakers);
  123. HandleEncoderProperties(fallbackEncoder.c_str());
  124. const char *id = nullptr;
  125. for (size_t i = 0; obs_enum_encoder_types(i, &id); i++) {
  126. auto Compare = [=](const string &val) {
  127. return val == NullToEmpty(id);
  128. };
  129. if (find_if(begin(encoders), end(encoders), Compare) !=
  130. end(encoders))
  131. continue;
  132. if (strcmp(GetCodec(id), "AAC") != 0)
  133. continue;
  134. HandleEncoderProperties(id);
  135. }
  136. for (auto &encoder : encoders) {
  137. if (encoder == fallbackEncoder)
  138. continue;
  139. if (strcmp(GetCodec(encoder.c_str()), "AAC") != 0)
  140. continue;
  141. // disable mf_aac if audio output is not stereo nor mono
  142. if (output_channels >= 3 && encoder == "mf_aac")
  143. continue;
  144. HandleEncoderProperties(encoder.c_str());
  145. }
  146. if (bitrateMap.empty()) {
  147. blog(LOG_ERROR, "Could not enumerate any AAC encoder "
  148. "bitrates");
  149. return;
  150. }
  151. ostringstream ss;
  152. for (auto &entry : bitrateMap)
  153. ss << "\n " << setw(3) << entry.first
  154. << " kbit/s: '" << EncoderName(entry.second) << "' ("
  155. << entry.second << ')';
  156. blog(LOG_DEBUG, "AAC encoder bitrate mapping:%s",
  157. ss.str().c_str());
  158. });
  159. }
  160. const map<int, std::string> &GetAACEncoderBitrateMap()
  161. {
  162. PopulateBitrateMap();
  163. return bitrateMap;
  164. }
  165. const char *GetAACEncoderForBitrate(int bitrate)
  166. {
  167. auto &map_ = GetAACEncoderBitrateMap();
  168. auto res = map_.find(bitrate);
  169. if (res == end(map_))
  170. return NULL;
  171. return res->second.c_str();
  172. }
  173. #define INVALID_BITRATE 10000
  174. int FindClosestAvailableAACBitrate(int bitrate)
  175. {
  176. auto &map_ = GetAACEncoderBitrateMap();
  177. int prev = 0;
  178. int next = INVALID_BITRATE;
  179. for (auto val : map_) {
  180. if (next > val.first) {
  181. if (val.first == bitrate)
  182. return bitrate;
  183. if (val.first < next && val.first > bitrate)
  184. next = val.first;
  185. if (val.first > prev && val.first < bitrate)
  186. prev = val.first;
  187. }
  188. }
  189. if (next != INVALID_BITRATE)
  190. return next;
  191. if (prev != 0)
  192. return prev;
  193. return 192;
  194. }