1
0

audio-encoders.cpp 5.2 KB

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