ambdec.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "config.h"
  2. #include "ambdec.h"
  3. #include <algorithm>
  4. #include <cctype>
  5. #include <cstdarg>
  6. #include <cstddef>
  7. #include <cstdio>
  8. #include <filesystem>
  9. #include <fstream>
  10. #include <iterator>
  11. #include <sstream>
  12. #include <string>
  13. #include "albit.h"
  14. #include "alspan.h"
  15. #include "opthelpers.h"
  16. namespace {
  17. std::string read_word(std::istream &f)
  18. {
  19. std::string ret;
  20. f >> ret;
  21. return ret;
  22. }
  23. bool is_at_end(const std::string &buffer, std::size_t endpos)
  24. {
  25. while(endpos < buffer.length() && std::isspace(buffer[endpos]))
  26. ++endpos;
  27. return !(endpos < buffer.length() && buffer[endpos] != '#');
  28. }
  29. enum class ReaderScope {
  30. Global,
  31. Speakers,
  32. LFMatrix,
  33. HFMatrix,
  34. };
  35. #ifdef __MINGW32__
  36. [[gnu::format(__MINGW_PRINTF_FORMAT,2,3)]]
  37. #else
  38. [[gnu::format(printf,2,3)]]
  39. #endif
  40. std::optional<std::string> make_error(size_t linenum, const char *fmt, ...)
  41. {
  42. std::optional<std::string> ret;
  43. auto &str = ret.emplace();
  44. str.resize(256);
  45. int printed{std::snprintf(str.data(), str.length(), "Line %zu: ", linenum)};
  46. if(printed < 0) printed = 0;
  47. auto plen = std::min(static_cast<size_t>(printed), str.length());
  48. /* NOLINTBEGIN(*-array-to-pointer-decay) */
  49. std::va_list args, args2;
  50. va_start(args, fmt);
  51. va_copy(args2, args);
  52. const int msglen{std::vsnprintf(&str[plen], str.size()-plen, fmt, args)};
  53. if(msglen >= 0 && static_cast<size_t>(msglen) >= str.size()-plen)
  54. {
  55. str.resize(static_cast<size_t>(msglen) + plen + 1u);
  56. std::vsnprintf(&str[plen], str.size()-plen, fmt, args2);
  57. }
  58. va_end(args2);
  59. va_end(args);
  60. /* NOLINTEND(*-array-to-pointer-decay) */
  61. return ret;
  62. }
  63. } // namespace
  64. AmbDecConf::~AmbDecConf() = default;
  65. std::optional<std::string> AmbDecConf::load(const char *fname) noexcept
  66. {
  67. std::ifstream f{std::filesystem::u8path(fname)};
  68. if(!f.is_open())
  69. return std::string("Failed to open file \"")+fname+"\"";
  70. ReaderScope scope{ReaderScope::Global};
  71. size_t speaker_pos{0};
  72. size_t lfmatrix_pos{0};
  73. size_t hfmatrix_pos{0};
  74. size_t linenum{0};
  75. std::string buffer;
  76. while(f.good() && std::getline(f, buffer))
  77. {
  78. ++linenum;
  79. std::istringstream istr{buffer};
  80. std::string command{read_word(istr)};
  81. if(command.empty() || command[0] == '#')
  82. continue;
  83. if(command == "/}")
  84. {
  85. if(scope == ReaderScope::Global)
  86. return make_error(linenum, "Unexpected /} in global scope");
  87. scope = ReaderScope::Global;
  88. continue;
  89. }
  90. if(scope == ReaderScope::Speakers)
  91. {
  92. if(command == "add_spkr")
  93. {
  94. if(speaker_pos == Speakers.size())
  95. return make_error(linenum, "Too many speakers specified");
  96. AmbDecConf::SpeakerConf &spkr = Speakers[speaker_pos++];
  97. istr >> spkr.Name;
  98. istr >> spkr.Distance;
  99. istr >> spkr.Azimuth;
  100. istr >> spkr.Elevation;
  101. istr >> spkr.Connection;
  102. }
  103. else
  104. return make_error(linenum, "Unexpected speakers command: %s", command.c_str());
  105. }
  106. else if(scope == ReaderScope::LFMatrix || scope == ReaderScope::HFMatrix)
  107. {
  108. auto &gains = (scope == ReaderScope::LFMatrix) ? LFOrderGain : HFOrderGain;
  109. auto matrix = (scope == ReaderScope::LFMatrix) ? LFMatrix : HFMatrix;
  110. auto &pos = (scope == ReaderScope::LFMatrix) ? lfmatrix_pos : hfmatrix_pos;
  111. if(command == "order_gain")
  112. {
  113. size_t toread{(ChanMask > Ambi3OrderMask) ? 5u : 4u};
  114. std::size_t curgain{0u};
  115. float value{};
  116. while(toread)
  117. {
  118. --toread;
  119. istr >> value;
  120. if(curgain < std::size(gains))
  121. gains[curgain++] = value;
  122. }
  123. }
  124. else if(command == "add_row")
  125. {
  126. if(pos == Speakers.size())
  127. return make_error(linenum, "Too many matrix rows specified");
  128. unsigned int mask{ChanMask};
  129. AmbDecConf::CoeffArray &mtxrow = matrix[pos++];
  130. mtxrow.fill(0.0f);
  131. float value{};
  132. while(mask)
  133. {
  134. auto idx = static_cast<unsigned>(al::countr_zero(mask));
  135. mask &= ~(1u << idx);
  136. istr >> value;
  137. if(idx < mtxrow.size())
  138. mtxrow[idx] = value;
  139. }
  140. }
  141. else
  142. return make_error(linenum, "Unexpected matrix command: %s", command.c_str());
  143. }
  144. // Global scope commands
  145. else if(command == "/description")
  146. {
  147. while(istr.good() && std::isspace(istr.peek()))
  148. istr.ignore();
  149. std::getline(istr, Description);
  150. while(!Description.empty() && std::isspace(Description.back()))
  151. Description.pop_back();
  152. }
  153. else if(command == "/version")
  154. {
  155. if(Version)
  156. return make_error(linenum, "Duplicate version definition");
  157. istr >> Version;
  158. if(Version != 3)
  159. return make_error(linenum, "Unsupported version: %d", Version);
  160. }
  161. else if(command == "/dec/chan_mask")
  162. {
  163. if(ChanMask)
  164. return make_error(linenum, "Duplicate chan_mask definition");
  165. istr >> std::hex >> ChanMask >> std::dec;
  166. if(!ChanMask || ChanMask > Ambi4OrderMask)
  167. return make_error(linenum, "Invalid chan_mask: 0x%x", ChanMask);
  168. if(ChanMask > Ambi3OrderMask && CoeffScale == AmbDecScale::FuMa)
  169. return make_error(linenum, "FuMa not compatible with over third-order");
  170. }
  171. else if(command == "/dec/freq_bands")
  172. {
  173. if(FreqBands)
  174. return make_error(linenum, "Duplicate freq_bands");
  175. istr >> FreqBands;
  176. if(FreqBands != 1 && FreqBands != 2)
  177. return make_error(linenum, "Invalid freq_bands: %u", FreqBands);
  178. }
  179. else if(command == "/dec/speakers")
  180. {
  181. if(!Speakers.empty())
  182. return make_error(linenum, "Duplicate speakers");
  183. size_t numspeakers{};
  184. istr >> numspeakers;
  185. if(!numspeakers)
  186. return make_error(linenum, "Invalid speakers: %zu", numspeakers);
  187. Speakers.resize(numspeakers);
  188. }
  189. else if(command == "/dec/coeff_scale")
  190. {
  191. if(CoeffScale != AmbDecScale::Unset)
  192. return make_error(linenum, "Duplicate coeff_scale");
  193. std::string scale{read_word(istr)};
  194. if(scale == "n3d") CoeffScale = AmbDecScale::N3D;
  195. else if(scale == "sn3d") CoeffScale = AmbDecScale::SN3D;
  196. else if(scale == "fuma") CoeffScale = AmbDecScale::FuMa;
  197. else
  198. return make_error(linenum, "Unexpected coeff_scale: %s", scale.c_str());
  199. if(ChanMask > Ambi3OrderMask && CoeffScale == AmbDecScale::FuMa)
  200. return make_error(linenum, "FuMa not compatible with over third-order");
  201. }
  202. else if(command == "/opt/xover_freq")
  203. {
  204. istr >> XOverFreq;
  205. }
  206. else if(command == "/opt/xover_ratio")
  207. {
  208. istr >> XOverRatio;
  209. }
  210. else if(command == "/opt/input_scale" || command == "/opt/nfeff_comp"
  211. || command == "/opt/delay_comp" || command == "/opt/level_comp")
  212. {
  213. /* Unused */
  214. read_word(istr);
  215. }
  216. else if(command == "/speakers/{")
  217. {
  218. if(Speakers.empty())
  219. return make_error(linenum, "Speakers defined without a count");
  220. scope = ReaderScope::Speakers;
  221. }
  222. else if(command == "/lfmatrix/{" || command == "/hfmatrix/{" || command == "/matrix/{")
  223. {
  224. if(Speakers.empty())
  225. return make_error(linenum, "Matrix defined without a speaker count");
  226. if(!ChanMask)
  227. return make_error(linenum, "Matrix defined without a channel mask");
  228. if(Matrix.empty())
  229. {
  230. Matrix.resize(Speakers.size() * FreqBands);
  231. LFMatrix = al::span{Matrix}.first(Speakers.size());
  232. HFMatrix = al::span{Matrix}.subspan(Speakers.size()*(FreqBands-1));
  233. }
  234. if(FreqBands == 1)
  235. {
  236. if(command != "/matrix/{")
  237. return make_error(linenum, "Unexpected \"%s\" for a single-band decoder",
  238. command.c_str());
  239. scope = ReaderScope::HFMatrix;
  240. }
  241. else
  242. {
  243. if(command == "/lfmatrix/{")
  244. scope = ReaderScope::LFMatrix;
  245. else if(command == "/hfmatrix/{")
  246. scope = ReaderScope::HFMatrix;
  247. else
  248. return make_error(linenum, "Unexpected \"%s\" for a dual-band decoder",
  249. command.c_str());
  250. }
  251. }
  252. else if(command == "/end")
  253. {
  254. const auto endpos = static_cast<std::size_t>(istr.tellg());
  255. if(!is_at_end(buffer, endpos))
  256. return make_error(linenum, "Extra junk on end: %s", buffer.substr(endpos).c_str());
  257. if(speaker_pos < Speakers.size() || hfmatrix_pos < Speakers.size()
  258. || (FreqBands == 2 && lfmatrix_pos < Speakers.size()))
  259. return make_error(linenum, "Incomplete decoder definition");
  260. if(CoeffScale == AmbDecScale::Unset)
  261. return make_error(linenum, "No coefficient scaling defined");
  262. return std::nullopt;
  263. }
  264. else
  265. return make_error(linenum, "Unexpected command: %s", command.c_str());
  266. istr.clear();
  267. const auto endpos = static_cast<std::size_t>(istr.tellg());
  268. if(!is_at_end(buffer, endpos))
  269. return make_error(linenum, "Extra junk on line: %s", buffer.substr(endpos).c_str());
  270. buffer.clear();
  271. }
  272. return make_error(linenum, "Unexpected end of file");
  273. }