ambdec.cpp 9.6 KB

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