2
0

ambdec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #include "config.h"
  2. #include "ambdec.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "compat.h"
  7. static char *lstrip(char *line)
  8. {
  9. while(isspace(line[0]))
  10. line++;
  11. return line;
  12. }
  13. static char *rstrip(char *line)
  14. {
  15. size_t len = strlen(line);
  16. while(len > 0 && isspace(line[len-1]))
  17. len--;
  18. line[len] = 0;
  19. return line;
  20. }
  21. static int readline(FILE *f, char **output, size_t *maxlen)
  22. {
  23. size_t len = 0;
  24. int c;
  25. while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
  26. ;
  27. if(c == EOF)
  28. return 0;
  29. do {
  30. if(len+1 >= *maxlen)
  31. {
  32. void *temp = NULL;
  33. size_t newmax;
  34. newmax = (*maxlen ? (*maxlen)<<1 : 32);
  35. if(newmax > *maxlen)
  36. temp = realloc(*output, newmax);
  37. if(!temp)
  38. {
  39. ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
  40. return 0;
  41. }
  42. *output = temp;
  43. *maxlen = newmax;
  44. }
  45. (*output)[len++] = c;
  46. (*output)[len] = '\0';
  47. } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
  48. return 1;
  49. }
  50. /* Custom strtok_r, since we can't rely on it existing. */
  51. static char *my_strtok_r(char *str, const char *delim, char **saveptr)
  52. {
  53. /* Sanity check and update internal pointer. */
  54. if(!saveptr || !delim) return NULL;
  55. if(str) *saveptr = str;
  56. str = *saveptr;
  57. /* Nothing more to do with this string. */
  58. if(!str) return NULL;
  59. /* Find the first non-delimiter character. */
  60. while(*str != '\0' && strchr(delim, *str) != NULL)
  61. str++;
  62. if(*str == '\0')
  63. {
  64. /* End of string. */
  65. *saveptr = NULL;
  66. return NULL;
  67. }
  68. /* Find the next delimiter character. */
  69. *saveptr = strpbrk(str, delim);
  70. if(*saveptr) *((*saveptr)++) = '\0';
  71. return str;
  72. }
  73. static char *read_uint(ALuint *num, const char *line, int base)
  74. {
  75. char *end;
  76. *num = strtoul(line, &end, base);
  77. if(end && *end != '\0')
  78. end = lstrip(end);
  79. return end;
  80. }
  81. static char *read_float(ALfloat *num, const char *line)
  82. {
  83. char *end;
  84. #ifdef HAVE_STRTOF
  85. *num = strtof(line, &end);
  86. #else
  87. *num = (ALfloat)strtod(line, &end);
  88. #endif
  89. if(end && *end != '\0')
  90. end = lstrip(end);
  91. return end;
  92. }
  93. char *read_clipped_line(FILE *f, char **buffer, size_t *maxlen)
  94. {
  95. while(readline(f, buffer, maxlen))
  96. {
  97. char *line, *comment;
  98. line = lstrip(*buffer);
  99. comment = strchr(line, '#');
  100. if(comment) *(comment++) = 0;
  101. line = rstrip(line);
  102. if(line[0]) return line;
  103. }
  104. return NULL;
  105. }
  106. static int load_ambdec_speakers(AmbDecConf *conf, FILE *f, char **buffer, size_t *maxlen, char **saveptr)
  107. {
  108. ALuint cur = 0;
  109. while(cur < conf->NumSpeakers)
  110. {
  111. const char *cmd = my_strtok_r(NULL, " \t", saveptr);
  112. if(!cmd)
  113. {
  114. char *line = read_clipped_line(f, buffer, maxlen);
  115. if(!line)
  116. {
  117. ERR("Unexpected end of file\n");
  118. return 0;
  119. }
  120. cmd = my_strtok_r(line, " \t", saveptr);
  121. }
  122. if(strcmp(cmd, "add_spkr") == 0)
  123. {
  124. const char *name = my_strtok_r(NULL, " \t", saveptr);
  125. const char *dist = my_strtok_r(NULL, " \t", saveptr);
  126. const char *az = my_strtok_r(NULL, " \t", saveptr);
  127. const char *elev = my_strtok_r(NULL, " \t", saveptr);
  128. const char *conn = my_strtok_r(NULL, " \t", saveptr);
  129. if(!name) WARN("Name not specified for speaker %u\n", cur+1);
  130. else al_string_copy_cstr(&conf->Speakers[cur].Name, name);
  131. if(!dist) WARN("Distance not specified for speaker %u\n", cur+1);
  132. else read_float(&conf->Speakers[cur].Distance, dist);
  133. if(!az) WARN("Azimuth not specified for speaker %u\n", cur+1);
  134. else read_float(&conf->Speakers[cur].Azimuth, az);
  135. if(!elev) WARN("Elevation not specified for speaker %u\n", cur+1);
  136. else read_float(&conf->Speakers[cur].Elevation, elev);
  137. if(!conn) TRACE("Connection not specified for speaker %u\n", cur+1);
  138. else al_string_copy_cstr(&conf->Speakers[cur].Connection, conn);
  139. cur++;
  140. }
  141. else
  142. {
  143. ERR("Unexpected speakers command: %s\n", cmd);
  144. return 0;
  145. }
  146. cmd = my_strtok_r(NULL, " \t", saveptr);
  147. if(cmd)
  148. {
  149. ERR("Unexpected junk on line: %s\n", cmd);
  150. return 0;
  151. }
  152. }
  153. return 1;
  154. }
  155. static int load_ambdec_matrix(ALfloat *gains, ALfloat (*matrix)[MAX_AMBI_COEFFS], ALuint maxrow, FILE *f, char **buffer, size_t *maxlen, char **saveptr)
  156. {
  157. int gotgains = 0;
  158. ALuint cur = 0;
  159. while(cur < maxrow)
  160. {
  161. const char *cmd = my_strtok_r(NULL, " \t", saveptr);
  162. if(!cmd)
  163. {
  164. char *line = read_clipped_line(f, buffer, maxlen);
  165. if(!line)
  166. {
  167. ERR("Unexpected end of file\n");
  168. return 0;
  169. }
  170. cmd = my_strtok_r(line, " \t", saveptr);
  171. }
  172. if(strcmp(cmd, "order_gain") == 0)
  173. {
  174. ALuint curgain = 0;
  175. char *line;
  176. while((line=my_strtok_r(NULL, " \t", saveptr)) != NULL)
  177. {
  178. ALfloat value;
  179. line = read_float(&value, line);
  180. if(line && *line != '\0')
  181. {
  182. ERR("Extra junk on gain %u: %s\n", curgain+1, line);
  183. return 0;
  184. }
  185. if(curgain < MAX_AMBI_ORDER+1)
  186. gains[curgain] = value;
  187. curgain++;
  188. }
  189. while(curgain < MAX_AMBI_ORDER+1)
  190. gains[curgain++] = 0.0f;
  191. gotgains = 1;
  192. }
  193. else if(strcmp(cmd, "add_row") == 0)
  194. {
  195. ALuint curidx = 0;
  196. char *line;
  197. while((line=my_strtok_r(NULL, " \t", saveptr)) != NULL)
  198. {
  199. ALfloat value;
  200. line = read_float(&value, line);
  201. if(line && *line != '\0')
  202. {
  203. ERR("Extra junk on matrix element %ux%u: %s\n", cur, curidx, line);
  204. return 0;
  205. }
  206. if(curidx < MAX_AMBI_COEFFS)
  207. matrix[cur][curidx] = value;
  208. curidx++;
  209. }
  210. while(curidx < MAX_AMBI_COEFFS)
  211. matrix[cur][curidx++] = 0.0f;
  212. cur++;
  213. }
  214. else
  215. {
  216. ERR("Unexpected speakers command: %s\n", cmd);
  217. return 0;
  218. }
  219. cmd = my_strtok_r(NULL, " \t", saveptr);
  220. if(cmd)
  221. {
  222. ERR("Unexpected junk on line: %s\n", cmd);
  223. return 0;
  224. }
  225. }
  226. if(!gotgains)
  227. {
  228. ERR("Matrix order_gain not specified\n");
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. void ambdec_init(AmbDecConf *conf)
  234. {
  235. ALuint i;
  236. memset(conf, 0, sizeof(*conf));
  237. AL_STRING_INIT(conf->Description);
  238. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  239. {
  240. AL_STRING_INIT(conf->Speakers[i].Name);
  241. AL_STRING_INIT(conf->Speakers[i].Connection);
  242. }
  243. }
  244. void ambdec_deinit(AmbDecConf *conf)
  245. {
  246. ALuint i;
  247. al_string_deinit(&conf->Description);
  248. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  249. {
  250. al_string_deinit(&conf->Speakers[i].Name);
  251. al_string_deinit(&conf->Speakers[i].Connection);
  252. }
  253. memset(conf, 0, sizeof(*conf));
  254. }
  255. int ambdec_load(AmbDecConf *conf, const char *fname)
  256. {
  257. char *buffer = NULL;
  258. size_t maxlen = 0;
  259. char *line;
  260. FILE *f;
  261. f = al_fopen(fname, "r");
  262. if(!f)
  263. {
  264. ERR("Failed to open: %s\n", fname);
  265. return 0;
  266. }
  267. while((line=read_clipped_line(f, &buffer, &maxlen)) != NULL)
  268. {
  269. char *saveptr;
  270. char *command;
  271. command = my_strtok_r(line, "/ \t", &saveptr);
  272. if(!command)
  273. {
  274. ERR("Malformed line: %s\n", line);
  275. goto fail;
  276. }
  277. if(strcmp(command, "description") == 0)
  278. {
  279. char *value = my_strtok_r(NULL, "", &saveptr);
  280. al_string_copy_cstr(&conf->Description, lstrip(value));
  281. }
  282. else if(strcmp(command, "version") == 0)
  283. {
  284. line = my_strtok_r(NULL, "", &saveptr);
  285. line = read_uint(&conf->Version, line, 10);
  286. if(line && *line != '\0')
  287. {
  288. ERR("Extra junk after version: %s\n", line);
  289. goto fail;
  290. }
  291. if(conf->Version != 3)
  292. {
  293. ERR("Unsupported version: %u\n", conf->Version);
  294. goto fail;
  295. }
  296. }
  297. else if(strcmp(command, "dec") == 0)
  298. {
  299. const char *dec = my_strtok_r(NULL, "/ \t", &saveptr);
  300. if(strcmp(dec, "chan_mask") == 0)
  301. {
  302. line = my_strtok_r(NULL, "", &saveptr);
  303. line = read_uint(&conf->ChanMask, line, 16);
  304. if(line && *line != '\0')
  305. {
  306. ERR("Extra junk after mask: %s\n", line);
  307. goto fail;
  308. }
  309. }
  310. else if(strcmp(dec, "freq_bands") == 0)
  311. {
  312. line = my_strtok_r(NULL, "", &saveptr);
  313. line = read_uint(&conf->FreqBands, line, 10);
  314. if(line && *line != '\0')
  315. {
  316. ERR("Extra junk after freq_bands: %s\n", line);
  317. goto fail;
  318. }
  319. if(conf->FreqBands != 1 && conf->FreqBands != 2)
  320. {
  321. ERR("Invalid freq_bands value: %u\n", conf->FreqBands);
  322. goto fail;
  323. }
  324. }
  325. else if(strcmp(dec, "speakers") == 0)
  326. {
  327. line = my_strtok_r(NULL, "", &saveptr);
  328. line = read_uint(&conf->NumSpeakers, line, 10);
  329. if(line && *line != '\0')
  330. {
  331. ERR("Extra junk after speakers: %s\n", line);
  332. goto fail;
  333. }
  334. if(conf->NumSpeakers > MAX_OUTPUT_CHANNELS)
  335. {
  336. ERR("Unsupported speaker count: %u\n", conf->NumSpeakers);
  337. goto fail;
  338. }
  339. }
  340. else if(strcmp(dec, "coeff_scale") == 0)
  341. {
  342. line = my_strtok_r(NULL, " \t", &saveptr);
  343. if(strcmp(line, "n3d") == 0)
  344. conf->CoeffScale = ADS_N3D;
  345. else if(strcmp(line, "sn3d") == 0)
  346. conf->CoeffScale = ADS_SN3D;
  347. else if(strcmp(line, "fuma") == 0)
  348. conf->CoeffScale = ADS_FuMa;
  349. else
  350. {
  351. ERR("Unsupported coeff scale: %s\n", line);
  352. goto fail;
  353. }
  354. }
  355. else
  356. {
  357. ERR("Unexpected /dec option: %s\n", dec);
  358. goto fail;
  359. }
  360. }
  361. else if(strcmp(command, "opt") == 0)
  362. {
  363. const char *opt = my_strtok_r(NULL, "/ \t", &saveptr);
  364. if(strcmp(opt, "xover_freq") == 0)
  365. {
  366. line = my_strtok_r(NULL, "", &saveptr);
  367. line = read_float(&conf->XOverFreq, line);
  368. if(line && *line != '\0')
  369. {
  370. ERR("Extra junk after xover_freq: %s\n", line);
  371. goto fail;
  372. }
  373. }
  374. else if(strcmp(opt, "xover_ratio") == 0)
  375. {
  376. line = my_strtok_r(NULL, "", &saveptr);
  377. line = read_float(&conf->XOverRatio, line);
  378. if(line && *line != '\0')
  379. {
  380. ERR("Extra junk after xover_ratio: %s\n", line);
  381. goto fail;
  382. }
  383. }
  384. else if(strcmp(opt, "input_scale") == 0 || strcmp(opt, "nfeff_comp") == 0 ||
  385. strcmp(opt, "delay_comp") == 0 || strcmp(opt, "level_comp") == 0)
  386. {
  387. /* Unused */
  388. my_strtok_r(NULL, " \t", &saveptr);
  389. }
  390. else
  391. {
  392. ERR("Unexpected /opt option: %s\n", opt);
  393. goto fail;
  394. }
  395. }
  396. else if(strcmp(command, "speakers") == 0)
  397. {
  398. const char *value = my_strtok_r(NULL, "/ \t", &saveptr);
  399. if(strcmp(value, "{") != 0)
  400. {
  401. ERR("Expected { after %s command, got %s\n", command, value);
  402. goto fail;
  403. }
  404. if(!load_ambdec_speakers(conf, f, &buffer, &maxlen, &saveptr))
  405. goto fail;
  406. value = my_strtok_r(NULL, "/ \t", &saveptr);
  407. if(!value)
  408. {
  409. line = read_clipped_line(f, &buffer, &maxlen);
  410. if(!line)
  411. {
  412. ERR("Unexpected end of file\n");
  413. goto fail;
  414. }
  415. value = my_strtok_r(line, "/ \t", &saveptr);
  416. }
  417. if(strcmp(value, "}") != 0)
  418. {
  419. ERR("Expected } after speaker definitions, got %s\n", value);
  420. goto fail;
  421. }
  422. }
  423. else if(strcmp(command, "lfmatrix") == 0 || strcmp(command, "hfmatrix") == 0 ||
  424. strcmp(command, "matrix") == 0)
  425. {
  426. const char *value = my_strtok_r(NULL, "/ \t", &saveptr);
  427. if(strcmp(value, "{") != 0)
  428. {
  429. ERR("Expected { after %s command, got %s\n", command, value);
  430. goto fail;
  431. }
  432. if(conf->FreqBands == 1)
  433. {
  434. if(strcmp(command, "matrix") != 0)
  435. {
  436. ERR("Unexpected \"%s\" type for a single-band decoder\n", command);
  437. goto fail;
  438. }
  439. if(!load_ambdec_matrix(conf->HFOrderGain, conf->HFMatrix, conf->NumSpeakers,
  440. f, &buffer, &maxlen, &saveptr))
  441. goto fail;
  442. }
  443. else
  444. {
  445. if(strcmp(command, "lfmatrix") == 0)
  446. {
  447. if(!load_ambdec_matrix(conf->LFOrderGain, conf->LFMatrix, conf->NumSpeakers,
  448. f, &buffer, &maxlen, &saveptr))
  449. goto fail;
  450. }
  451. else if(strcmp(command, "hfmatrix") == 0)
  452. {
  453. if(!load_ambdec_matrix(conf->HFOrderGain, conf->HFMatrix, conf->NumSpeakers,
  454. f, &buffer, &maxlen, &saveptr))
  455. goto fail;
  456. }
  457. else
  458. {
  459. ERR("Unexpected \"%s\" type for a dual-band decoder\n", command);
  460. goto fail;
  461. }
  462. }
  463. value = my_strtok_r(NULL, "/ \t", &saveptr);
  464. if(!value)
  465. {
  466. line = read_clipped_line(f, &buffer, &maxlen);
  467. if(!line)
  468. {
  469. ERR("Unexpected end of file\n");
  470. goto fail;
  471. }
  472. value = my_strtok_r(line, "/ \t", &saveptr);
  473. }
  474. if(strcmp(value, "}") != 0)
  475. {
  476. ERR("Expected } after matrix definitions, got %s\n", value);
  477. goto fail;
  478. }
  479. }
  480. else if(strcmp(command, "end") == 0)
  481. {
  482. line = my_strtok_r(NULL, "/ \t", &saveptr);
  483. if(line)
  484. {
  485. ERR("Unexpected junk on end: %s\n", line);
  486. goto fail;
  487. }
  488. fclose(f);
  489. free(buffer);
  490. return 1;
  491. }
  492. else
  493. {
  494. ERR("Unexpected command: %s\n", command);
  495. goto fail;
  496. }
  497. line = my_strtok_r(NULL, "/ \t", &saveptr);
  498. if(line)
  499. {
  500. ERR("Unexpected junk on line: %s\n", line);
  501. goto fail;
  502. }
  503. }
  504. ERR("Unexpected end of file\n");
  505. fail:
  506. fclose(f);
  507. free(buffer);
  508. return 0;
  509. }