2
0

ambdec.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "config.h"
  2. #include "ambdec.h"
  3. #include <algorithm>
  4. #include <cctype>
  5. #include <cstddef>
  6. #include <iterator>
  7. #include <sstream>
  8. #include <string>
  9. #include "alfstream.h"
  10. #include "logging.h"
  11. namespace {
  12. template<typename T, std::size_t N>
  13. constexpr inline std::size_t size(const T(&)[N]) noexcept
  14. { return N; }
  15. int readline(std::istream &f, std::string &output)
  16. {
  17. while(f.good() && f.peek() == '\n')
  18. f.ignore();
  19. return std::getline(f, output) && !output.empty();
  20. }
  21. bool read_clipped_line(std::istream &f, std::string &buffer)
  22. {
  23. while(readline(f, buffer))
  24. {
  25. std::size_t pos{0};
  26. while(pos < buffer.length() && std::isspace(buffer[pos]))
  27. pos++;
  28. buffer.erase(0, pos);
  29. std::size_t cmtpos{buffer.find_first_of('#')};
  30. if(cmtpos < buffer.length())
  31. buffer.resize(cmtpos);
  32. while(!buffer.empty() && std::isspace(buffer.back()))
  33. buffer.pop_back();
  34. if(!buffer.empty())
  35. return true;
  36. }
  37. return false;
  38. }
  39. std::string read_word(std::istream &f)
  40. {
  41. std::string ret;
  42. f >> ret;
  43. return ret;
  44. }
  45. bool is_at_end(const std::string &buffer, std::size_t endpos)
  46. {
  47. while(endpos < buffer.length() && std::isspace(buffer[endpos]))
  48. ++endpos;
  49. return !(endpos < buffer.length());
  50. }
  51. bool load_ambdec_speakers(al::vector<AmbDecConf::SpeakerConf> &spkrs, const std::size_t num_speakers, std::istream &f, std::string &buffer)
  52. {
  53. while(spkrs.size() < num_speakers)
  54. {
  55. std::istringstream istr{buffer};
  56. std::string cmd{read_word(istr)};
  57. if(cmd.empty())
  58. {
  59. if(!read_clipped_line(f, buffer))
  60. {
  61. ERR("Unexpected end of file\n");
  62. return false;
  63. }
  64. continue;
  65. }
  66. if(cmd == "add_spkr")
  67. {
  68. spkrs.emplace_back();
  69. AmbDecConf::SpeakerConf &spkr = spkrs.back();
  70. const size_t spkr_num{spkrs.size()};
  71. istr >> spkr.Name;
  72. if(istr.fail()) WARN("Name not specified for speaker %zu\n", spkr_num);
  73. istr >> spkr.Distance;
  74. if(istr.fail()) WARN("Distance not specified for speaker %zu\n", spkr_num);
  75. istr >> spkr.Azimuth;
  76. if(istr.fail()) WARN("Azimuth not specified for speaker %zu\n", spkr_num);
  77. istr >> spkr.Elevation;
  78. if(istr.fail()) WARN("Elevation not specified for speaker %zu\n", spkr_num);
  79. istr >> spkr.Connection;
  80. if(istr.fail()) TRACE("Connection not specified for speaker %zu\n", spkr_num);
  81. }
  82. else
  83. {
  84. ERR("Unexpected speakers command: %s\n", cmd.c_str());
  85. return false;
  86. }
  87. istr.clear();
  88. const auto endpos = static_cast<std::size_t>(istr.tellg());
  89. if(!is_at_end(buffer, endpos))
  90. {
  91. ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
  92. return false;
  93. }
  94. buffer.clear();
  95. }
  96. return true;
  97. }
  98. bool load_ambdec_matrix(float (&gains)[MAX_AMBI_ORDER+1], al::vector<AmbDecConf::CoeffArray> &matrix, const std::size_t maxrow, std::istream &f, std::string &buffer)
  99. {
  100. bool gotgains{false};
  101. std::size_t cur{0u};
  102. while(cur < maxrow)
  103. {
  104. std::istringstream istr{buffer};
  105. std::string cmd{read_word(istr)};
  106. if(cmd.empty())
  107. {
  108. if(!read_clipped_line(f, buffer))
  109. {
  110. ERR("Unexpected end of file\n");
  111. return false;
  112. }
  113. continue;
  114. }
  115. if(cmd == "order_gain")
  116. {
  117. std::size_t curgain{0u};
  118. float value;
  119. while(istr.good())
  120. {
  121. istr >> value;
  122. if(istr.fail()) break;
  123. if(!istr.eof() && !std::isspace(istr.peek()))
  124. {
  125. ERR("Extra junk on gain %zu: %s\n", curgain+1,
  126. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  127. return false;
  128. }
  129. if(curgain < size(gains))
  130. gains[curgain++] = value;
  131. }
  132. std::fill(std::begin(gains)+curgain, std::end(gains), 0.0f);
  133. gotgains = true;
  134. }
  135. else if(cmd == "add_row")
  136. {
  137. matrix.emplace_back();
  138. AmbDecConf::CoeffArray &mtxrow = matrix.back();
  139. std::size_t curidx{0u};
  140. float value{};
  141. while(istr.good())
  142. {
  143. istr >> value;
  144. if(istr.fail()) break;
  145. if(!istr.eof() && !std::isspace(istr.peek()))
  146. {
  147. ERR("Extra junk on matrix element %zux%zu: %s\n", curidx,
  148. matrix.size(), buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  149. matrix.pop_back();
  150. return false;
  151. }
  152. if(curidx < mtxrow.size())
  153. mtxrow[curidx++] = value;
  154. }
  155. std::fill(mtxrow.begin()+curidx, mtxrow.end(), 0.0f);
  156. cur++;
  157. }
  158. else
  159. {
  160. ERR("Unexpected matrix command: %s\n", cmd.c_str());
  161. return false;
  162. }
  163. istr.clear();
  164. const auto endpos = static_cast<std::size_t>(istr.tellg());
  165. if(!is_at_end(buffer, endpos))
  166. {
  167. ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
  168. return false;
  169. }
  170. buffer.clear();
  171. }
  172. if(!gotgains)
  173. {
  174. ERR("Matrix order_gain not specified\n");
  175. return false;
  176. }
  177. return true;
  178. }
  179. } // namespace
  180. int AmbDecConf::load(const char *fname) noexcept
  181. {
  182. al::ifstream f{fname};
  183. if(!f.is_open())
  184. {
  185. ERR("Failed to open: %s\n", fname);
  186. return 0;
  187. }
  188. std::size_t num_speakers{0u};
  189. std::string buffer;
  190. while(read_clipped_line(f, buffer))
  191. {
  192. std::istringstream istr{buffer};
  193. std::string command{read_word(istr)};
  194. if(command.empty())
  195. {
  196. ERR("Malformed line: %s\n", buffer.c_str());
  197. return 0;
  198. }
  199. if(command == "/description")
  200. istr >> Description;
  201. else if(command == "/version")
  202. {
  203. istr >> Version;
  204. if(!istr.eof() && !std::isspace(istr.peek()))
  205. {
  206. ERR("Extra junk after version: %s\n",
  207. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  208. return 0;
  209. }
  210. if(Version != 3)
  211. {
  212. ERR("Unsupported version: %u\n", Version);
  213. return 0;
  214. }
  215. }
  216. else if(command == "/dec/chan_mask")
  217. {
  218. istr >> std::hex >> ChanMask >> std::dec;
  219. if(!istr.eof() && !std::isspace(istr.peek()))
  220. {
  221. ERR("Extra junk after mask: %s\n",
  222. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  223. return 0;
  224. }
  225. }
  226. else if(command == "/dec/freq_bands")
  227. {
  228. istr >> FreqBands;
  229. if(!istr.eof() && !std::isspace(istr.peek()))
  230. {
  231. ERR("Extra junk after freq_bands: %s\n",
  232. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  233. return 0;
  234. }
  235. if(FreqBands != 1 && FreqBands != 2)
  236. {
  237. ERR("Invalid freq_bands value: %u\n", FreqBands);
  238. return 0;
  239. }
  240. }
  241. else if(command == "/dec/speakers")
  242. {
  243. istr >> num_speakers;
  244. if(!istr.eof() && !std::isspace(istr.peek()))
  245. {
  246. ERR("Extra junk after speakers: %s\n",
  247. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  248. return 0;
  249. }
  250. Speakers.reserve(num_speakers);
  251. LFMatrix.reserve(num_speakers);
  252. HFMatrix.reserve(num_speakers);
  253. }
  254. else if(command == "/dec/coeff_scale")
  255. {
  256. std::string scale = read_word(istr);
  257. if(scale == "n3d") CoeffScale = AmbDecScale::N3D;
  258. else if(scale == "sn3d") CoeffScale = AmbDecScale::SN3D;
  259. else if(scale == "fuma") CoeffScale = AmbDecScale::FuMa;
  260. else
  261. {
  262. ERR("Unsupported coeff scale: %s\n", scale.c_str());
  263. return 0;
  264. }
  265. }
  266. else if(command == "/opt/xover_freq")
  267. {
  268. istr >> XOverFreq;
  269. if(!istr.eof() && !std::isspace(istr.peek()))
  270. {
  271. ERR("Extra junk after xover_freq: %s\n",
  272. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  273. return 0;
  274. }
  275. }
  276. else if(command == "/opt/xover_ratio")
  277. {
  278. istr >> XOverRatio;
  279. if(!istr.eof() && !std::isspace(istr.peek()))
  280. {
  281. ERR("Extra junk after xover_ratio: %s\n",
  282. buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
  283. return 0;
  284. }
  285. }
  286. else if(command == "/opt/input_scale" || command == "/opt/nfeff_comp" ||
  287. command == "/opt/delay_comp" || command == "/opt/level_comp")
  288. {
  289. /* Unused */
  290. read_word(istr);
  291. }
  292. else if(command == "/speakers/{")
  293. {
  294. const auto endpos = static_cast<std::size_t>(istr.tellg());
  295. if(!is_at_end(buffer, endpos))
  296. {
  297. ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
  298. return 0;
  299. }
  300. buffer.clear();
  301. if(!load_ambdec_speakers(Speakers, num_speakers, f, buffer))
  302. return 0;
  303. if(!read_clipped_line(f, buffer))
  304. {
  305. ERR("Unexpected end of file\n");
  306. return 0;
  307. }
  308. std::istringstream istr2{buffer};
  309. std::string endmark{read_word(istr2)};
  310. if(endmark != "/}")
  311. {
  312. ERR("Expected /} after speaker definitions, got %s\n", endmark.c_str());
  313. return 0;
  314. }
  315. istr.swap(istr2);
  316. }
  317. else if(command == "/lfmatrix/{" || command == "/hfmatrix/{" || command == "/matrix/{")
  318. {
  319. const auto endpos = static_cast<std::size_t>(istr.tellg());
  320. if(!is_at_end(buffer, endpos))
  321. {
  322. ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
  323. return 0;
  324. }
  325. buffer.clear();
  326. if(FreqBands == 1)
  327. {
  328. if(command != "/matrix/{")
  329. {
  330. ERR("Unexpected \"%s\" type for a single-band decoder\n", command.c_str());
  331. return 0;
  332. }
  333. if(!load_ambdec_matrix(HFOrderGain, HFMatrix, num_speakers, f, buffer))
  334. return 0;
  335. }
  336. else
  337. {
  338. if(command == "/lfmatrix/{")
  339. {
  340. if(!load_ambdec_matrix(LFOrderGain, LFMatrix, num_speakers, f, buffer))
  341. return 0;
  342. }
  343. else if(command == "/hfmatrix/{")
  344. {
  345. if(!load_ambdec_matrix(HFOrderGain, HFMatrix, num_speakers, f, buffer))
  346. return 0;
  347. }
  348. else
  349. {
  350. ERR("Unexpected \"%s\" type for a dual-band decoder\n", command.c_str());
  351. return 0;
  352. }
  353. }
  354. if(!read_clipped_line(f, buffer))
  355. {
  356. ERR("Unexpected end of file\n");
  357. return 0;
  358. }
  359. std::istringstream istr2{buffer};
  360. std::string endmark{read_word(istr2)};
  361. if(endmark != "/}")
  362. {
  363. ERR("Expected /} after matrix definitions, got %s\n", endmark.c_str());
  364. return 0;
  365. }
  366. istr.swap(istr2);
  367. }
  368. else if(command == "/end")
  369. {
  370. const auto endpos = static_cast<std::size_t>(istr.tellg());
  371. if(!is_at_end(buffer, endpos))
  372. {
  373. ERR("Unexpected junk on end: %s\n", buffer.c_str()+endpos);
  374. return 0;
  375. }
  376. return 1;
  377. }
  378. else
  379. {
  380. ERR("Unexpected command: %s\n", command.c_str());
  381. return 0;
  382. }
  383. istr.clear();
  384. const auto endpos = static_cast<std::size_t>(istr.tellg());
  385. if(!is_at_end(buffer, endpos))
  386. {
  387. ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
  388. return 0;
  389. }
  390. buffer.clear();
  391. }
  392. ERR("Unexpected end of file\n");
  393. return 0;
  394. }