helix-aac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. * Philippe, philippe_44@outlook.com
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "squeezelite.h"
  23. #include <aacdec.h>
  24. // AAC_MAX_SAMPLES is the number of samples for one channel
  25. #define FRAME_BUF (AAC_MAX_NSAMPS*2)
  26. #if BYTES_PER_FRAME == 4
  27. #define ALIGN(n) (n)
  28. #else
  29. #define ALIGN(n) (n << 16)
  30. #endif
  31. #define WRAPBUF_LEN 2048
  32. static unsigned rates[] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 };
  33. struct chunk_table {
  34. u32_t sample, offset;
  35. };
  36. struct helixaac {
  37. HAACDecoder hAac;
  38. u8_t type;
  39. u8_t *write_buf;
  40. u8_t *wrap_buf;
  41. // following used for mp4 only
  42. u32_t consume;
  43. u32_t pos;
  44. u32_t sample;
  45. u32_t nextchunk;
  46. void *stsc;
  47. u32_t skip;
  48. u64_t samples;
  49. u64_t sttssamples;
  50. bool empty;
  51. struct chunk_table *chunkinfo;
  52. #if !LINKALL
  53. #endif
  54. };
  55. static struct helixaac *a;
  56. extern log_level loglevel;
  57. extern struct buffer *streambuf;
  58. extern struct buffer *outputbuf;
  59. extern struct streamstate stream;
  60. extern struct outputstate output;
  61. extern struct decodestate decode;
  62. extern struct processstate process;
  63. #define LOCK_S mutex_lock(streambuf->mutex)
  64. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  65. #define LOCK_O mutex_lock(outputbuf->mutex)
  66. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  67. #if PROCESS
  68. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  69. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  70. #define IF_DIRECT(x) if (decode.direct) { x }
  71. #define IF_PROCESS(x) if (!decode.direct) { x }
  72. #else
  73. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  74. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  75. #define IF_DIRECT(x) { x }
  76. #define IF_PROCESS(x)
  77. #endif
  78. #if LINKALL
  79. #define HAAC(h, fn, ...) (AAC ## fn)(__VA_ARGS__)
  80. #else
  81. #define HAAC(h, fn, ...) (h)->AAC##fn(__VA_ARGS__)
  82. #endif
  83. // minimal code for mp4 file parsing to extract audio config and find media data
  84. // adapted from faad2/common/mp4ff
  85. u32_t mp4_desc_length(u8_t **buf) {
  86. u8_t b;
  87. u8_t num_bytes = 0;
  88. u32_t length = 0;
  89. do {
  90. b = **buf;
  91. *buf += 1;
  92. num_bytes++;
  93. length = (length << 7) | (b & 0x7f);
  94. } while ((b & 0x80) && num_bytes < 4);
  95. return length;
  96. }
  97. // read mp4 header to extract config data
  98. static int read_mp4_header(unsigned long *samplerate_p, unsigned char *channels_p) {
  99. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  100. char type[5];
  101. u32_t len;
  102. while (bytes >= 8) {
  103. // count trak to find the first playable one
  104. static unsigned trak, play;
  105. u32_t consume;
  106. len = unpackN((u32_t *)streambuf->readp);
  107. memcpy(type, streambuf->readp + 4, 4);
  108. type[4] = '\0';
  109. if (!strcmp(type, "moov")) {
  110. trak = 0;
  111. play = 0;
  112. }
  113. if (!strcmp(type, "trak")) {
  114. trak++;
  115. }
  116. // extract audio config from within esds and pass to DecInit2
  117. if (!strcmp(type, "esds") && bytes > len) {
  118. u8_t *ptr = streambuf->readp + 12;
  119. AACFrameInfo info;
  120. if (*ptr++ == 0x03) {
  121. mp4_desc_length(&ptr);
  122. ptr += 4;
  123. } else {
  124. ptr += 3;
  125. }
  126. mp4_desc_length(&ptr);
  127. ptr += 13;
  128. if (*ptr++ != 0x05) {
  129. LOG_WARN("error parsing esds");
  130. return -1;
  131. }
  132. int desc_len = mp4_desc_length(&ptr);
  133. info.profile = *ptr >> 3;
  134. info.sampRateCore = (*ptr++ & 0x07) << 1;
  135. info.sampRateCore |= (*ptr >> 7) & 0x01;
  136. info.sampRateCore = rates[info.sampRateCore];
  137. info.nChans = (*ptr++ & 0x7f) >> 3;
  138. *channels_p = info.nChans;
  139. if (desc_len > 2 && ((ptr[0] << 3) | (ptr[1] >> 5)) == 0x2b7 && (ptr[1] & 0x1f) == 0x05 && (ptr[2] & 0x80)) {
  140. *samplerate_p = rates[(ptr[2] & 0x78) >> 3];
  141. LOG_WARN("AAC SBR mode activated => high CPU consumption expected, please use LMS proxy to mitigate");
  142. } else {
  143. *samplerate_p = info.sampRateCore;
  144. }
  145. HAAC(a, SetRawBlockParams, a->hAac, 0, &info);
  146. LOG_DEBUG("playable aac track: %u (p:%x, r:%d, c:%d)", trak, info.profile, info.sampRateCore, info.nChans);
  147. play = trak;
  148. }
  149. // extract the total number of samples from stts
  150. if (!strcmp(type, "stts") && bytes > len) {
  151. u32_t i;
  152. u8_t *ptr = streambuf->readp + 12;
  153. u32_t entries = unpackN((u32_t *)ptr);
  154. ptr += 4;
  155. for (i = 0; i < entries; ++i) {
  156. u32_t count = unpackN((u32_t *)ptr);
  157. u32_t size = unpackN((u32_t *)(ptr + 4));
  158. a->sttssamples += count * size;
  159. ptr += 8;
  160. }
  161. LOG_DEBUG("total number of samples contained in stts: " FMT_u64, a->sttssamples);
  162. }
  163. // stash sample to chunk info, assume it comes before stco
  164. if (!strcmp(type, "stsc") && bytes > len && !a->chunkinfo) {
  165. a->stsc = malloc(len - 12);
  166. if (a->stsc == NULL) {
  167. LOG_WARN("malloc fail");
  168. return -1;
  169. }
  170. memcpy(a->stsc, streambuf->readp + 12, len - 12);
  171. }
  172. // build offsets table from stco and stored stsc
  173. if (!strcmp(type, "stco") && bytes > len && play == trak) {
  174. u32_t i;
  175. // extract chunk offsets
  176. u8_t *ptr = streambuf->readp + 12;
  177. u32_t entries = unpackN((u32_t *)ptr);
  178. ptr += 4;
  179. a->chunkinfo = malloc(sizeof(struct chunk_table) * (entries + 1));
  180. if (a->chunkinfo == NULL) {
  181. LOG_WARN("malloc fail");
  182. return -1;
  183. }
  184. for (i = 0; i < entries; ++i) {
  185. a->chunkinfo[i].offset = unpackN((u32_t *)ptr);
  186. a->chunkinfo[i].sample = 0;
  187. ptr += 4;
  188. }
  189. a->chunkinfo[i].sample = 0;
  190. a->chunkinfo[i].offset = 0;
  191. // fill in first sample id for each chunk from stored stsc
  192. if (a->stsc) {
  193. u32_t stsc_entries = unpackN((u32_t *)a->stsc);
  194. u32_t sample = 0;
  195. u32_t last = 0, last_samples = 0;
  196. u8_t *ptr = (u8_t *)a->stsc + 4;
  197. while (stsc_entries--) {
  198. u32_t first = unpackN((u32_t *)ptr);
  199. u32_t samples = unpackN((u32_t *)(ptr + 4));
  200. if (last) {
  201. for (i = last - 1; i < first - 1; ++i) {
  202. a->chunkinfo[i].sample = sample;
  203. sample += last_samples;
  204. }
  205. }
  206. if (stsc_entries == 0) {
  207. for (i = first - 1; i < entries; ++i) {
  208. a->chunkinfo[i].sample = sample;
  209. sample += samples;
  210. }
  211. }
  212. last = first;
  213. last_samples = samples;
  214. ptr += 12;
  215. }
  216. free(a->stsc);
  217. a->stsc = NULL;
  218. }
  219. }
  220. // found media data, advance to start of first chunk and return
  221. if (!strcmp(type, "mdat")) {
  222. _buf_inc_readp(streambuf, 8);
  223. a->pos += 8;
  224. bytes -= 8;
  225. if (play) {
  226. LOG_DEBUG("type: mdat len: %u pos: %u", len, a->pos);
  227. if (a->chunkinfo && a->chunkinfo[0].offset > a->pos) {
  228. u32_t skip = a->chunkinfo[0].offset - a->pos;
  229. LOG_DEBUG("skipping: %u", skip);
  230. if (skip <= bytes) {
  231. _buf_inc_readp(streambuf, skip);
  232. a->pos += skip;
  233. } else {
  234. a->consume = skip;
  235. }
  236. }
  237. a->sample = a->nextchunk = 1;
  238. return 1;
  239. } else {
  240. LOG_DEBUG("type: mdat len: %u, no playable track found", len);
  241. return -1;
  242. }
  243. }
  244. // parse key-value atoms within ilst ---- entries to get encoder padding within iTunSMPB entry for gapless
  245. if (!strcmp(type, "----") && bytes > len) {
  246. u8_t *ptr = streambuf->readp + 8;
  247. u32_t remain = len - 8, size;
  248. if (!memcmp(ptr + 4, "mean", 4) && (size = unpackN((u32_t *)ptr)) < remain) {
  249. ptr += size; remain -= size;
  250. }
  251. if (!memcmp(ptr + 4, "name", 4) && (size = unpackN((u32_t *)ptr)) < remain && !memcmp(ptr + 12, "iTunSMPB", 8)) {
  252. ptr += size; remain -= size;
  253. }
  254. if (!memcmp(ptr + 4, "data", 4) && remain > 16 + 48) {
  255. // data is stored as hex strings: 0 start end samples
  256. u32_t b, c; u64_t d;
  257. if (sscanf((const char *)(ptr + 16), "%x %x %x " FMT_x64, &b, &b, &c, &d) == 4) {
  258. LOG_DEBUG("iTunSMPB start: %u end: %u samples: " FMT_u64, b, c, d);
  259. if (a->sttssamples && a->sttssamples < b + c + d) {
  260. LOG_DEBUG("reducing samples as stts count is less");
  261. d = a->sttssamples - (b + c);
  262. }
  263. a->skip = b;
  264. a->samples = d;
  265. }
  266. }
  267. }
  268. // default to consuming entire box
  269. consume = len;
  270. // read into these boxes so reduce consume
  271. if (!strcmp(type, "moov") || !strcmp(type, "trak") || !strcmp(type, "mdia") || !strcmp(type, "minf") || !strcmp(type, "stbl") ||
  272. !strcmp(type, "udta") || !strcmp(type, "ilst")) {
  273. consume = 8;
  274. }
  275. // special cases which mix mix data in the enclosing box which we want to read into
  276. if (!strcmp(type, "stsd")) consume = 16;
  277. if (!strcmp(type, "mp4a")) consume = 36;
  278. if (!strcmp(type, "meta")) consume = 12;
  279. // consume rest of box if it has been parsed (all in the buffer) or is not one we want to parse
  280. if (bytes >= consume) {
  281. LOG_DEBUG("type: %s len: %u consume: %u", type, len, consume);
  282. _buf_inc_readp(streambuf, consume);
  283. a->pos += consume;
  284. bytes -= consume;
  285. } else if ( !(!strcmp(type, "esds") || !strcmp(type, "stts") || !strcmp(type, "stsc") ||
  286. !strcmp(type, "stco") || !strcmp(type, "----")) ) {
  287. LOG_DEBUG("type: %s len: %u consume: %u - partial consume: %u", type, len, consume, bytes);
  288. _buf_inc_readp(streambuf, bytes);
  289. a->pos += bytes;
  290. a->consume = consume - bytes;
  291. break;
  292. } else if (len > streambuf->size) {
  293. // can't process an atom larger than streambuf!
  294. LOG_ERROR("atom %s too large for buffer %u %u", type, len, streambuf->size);
  295. return -1;
  296. } else {
  297. // make sure there is 'len' contiguous space
  298. _buf_unwrap(streambuf, len);
  299. break;
  300. }
  301. }
  302. return 0;
  303. }
  304. static decode_state helixaac_decode(void) {
  305. size_t bytes_total, bytes_wrap;
  306. int res, bytes;
  307. static AACFrameInfo info;
  308. s16_t *iptr;
  309. u8_t *sptr;
  310. bool endstream;
  311. frames_t frames;
  312. LOCK_S;
  313. bytes_total = _buf_used(streambuf);
  314. bytes_wrap = min(bytes_total, _buf_cont_read(streambuf));
  315. if (stream.state <= DISCONNECT && !bytes_total) {
  316. UNLOCK_S;
  317. return DECODE_COMPLETE;
  318. }
  319. if (a->consume) {
  320. u32_t consume = min(a->consume, bytes_wrap);
  321. LOG_DEBUG("consume: %u of %u", consume, a->consume);
  322. _buf_inc_readp(streambuf, consume);
  323. a->pos += consume;
  324. a->consume -= consume;
  325. UNLOCK_S;
  326. return DECODE_RUNNING;
  327. }
  328. if (decode.new_stream) {
  329. int found = 0;
  330. static unsigned char channels;
  331. static unsigned long samplerate;
  332. if (a->type == '2') {
  333. // adts stream - seek for header
  334. long n = HAAC(a, FindSyncWord, streambuf->readp, bytes_wrap);
  335. LOG_DEBUG("Sync search in %d bytes %d", bytes_wrap, n);
  336. if (n >= 0) {
  337. u8_t *p = streambuf->readp + n;
  338. int bytes = bytes_wrap - n;
  339. if (!HAAC(a, Decode, a->hAac, &p, &bytes, (s16_t*) a->write_buf)) {
  340. HAAC(a, GetLastFrameInfo, a->hAac, &info);
  341. channels = info.nChans;
  342. samplerate = info.sampRateOut;
  343. found = 1;
  344. } else if (n == 0) n++;
  345. HAAC(a, FlushCodec, a->hAac);
  346. bytes_total -= n;
  347. bytes_wrap -= n;
  348. _buf_inc_readp(streambuf, n);
  349. } else {
  350. found = -1;
  351. }
  352. } else {
  353. // mp4 - read header
  354. found = read_mp4_header(&samplerate, &channels);
  355. }
  356. if (found == 1) {
  357. LOCK_O;
  358. output.next_sample_rate = decode_newstream(samplerate, output.supported_rates);
  359. IF_DSD( output.next_fmt = PCM; )
  360. output.track_start = outputbuf->writep;
  361. if (output.fade_mode) _checkfade(true);
  362. decode.new_stream = false;
  363. UNLOCK_O;
  364. LOG_INFO("setting track start, samplerate: %u channels: %u", samplerate, channels);
  365. bytes_total = _buf_used(streambuf);
  366. bytes_wrap = min(bytes_total, _buf_cont_read(streambuf));
  367. // come back later if we don' thave enough data
  368. if (bytes_total < WRAPBUF_LEN) {
  369. UNLOCK_S;
  370. LOG_INFO("need more audio data");
  371. return DECODE_RUNNING;
  372. }
  373. } else if (found == -1) {
  374. LOG_WARN("error reading stream header");
  375. UNLOCK_S;
  376. return DECODE_ERROR;
  377. } else {
  378. // not finished header parsing come back next time
  379. UNLOCK_S;
  380. LOG_INFO("header not found yet");
  381. return DECODE_RUNNING;
  382. }
  383. }
  384. // we always have at least WRAPBUF_LEN unless it's the end of a stream
  385. if (bytes_wrap < WRAPBUF_LEN && bytes_wrap != bytes_total) {
  386. // build a linear buffer if we are crossing the end of streambuf
  387. memcpy(a->wrap_buf, streambuf->readp, bytes_wrap);
  388. memcpy(a->wrap_buf + bytes_wrap, streambuf->buf, min(WRAPBUF_LEN, bytes_total) - bytes_wrap);
  389. sptr = a->wrap_buf;
  390. bytes = bytes_wrap = min(WRAPBUF_LEN, bytes_total);
  391. } else {
  392. sptr = streambuf->readp;
  393. bytes = bytes_wrap;
  394. }
  395. // decode function changes iptr, so can't use streambuf->readp (same for bytes)
  396. res = HAAC(a, Decode, a->hAac, &sptr, &bytes, (s16_t*) a->write_buf);
  397. if (res < 0) {
  398. LOG_WARN("AAC decode error %d", res);
  399. }
  400. HAAC(a, GetLastFrameInfo, a->hAac, &info);
  401. iptr = (s16_t*) a->write_buf;
  402. bytes = bytes_wrap - bytes;
  403. endstream = false;
  404. if (a->chunkinfo && a->chunkinfo[a->nextchunk].offset && a->sample++ == a->chunkinfo[a->nextchunk].sample) {
  405. // mp4 end of chunk - skip to next offset
  406. if (a->chunkinfo[a->nextchunk].offset > a->pos) {
  407. u32_t skip = a->chunkinfo[a->nextchunk].offset - a->pos;
  408. if (skip != bytes) {
  409. LOG_DEBUG("skipping to next chunk pos: %u consumed: %u != skip: %u", a->pos, bytes, skip);
  410. }
  411. if (bytes_total >= skip) {
  412. _buf_inc_readp(streambuf, skip);
  413. a->pos += skip;
  414. } else {
  415. a->consume = skip;
  416. }
  417. a->nextchunk++;
  418. } else {
  419. LOG_ERROR("error: need to skip backwards!");
  420. endstream = true;
  421. }
  422. } else if (bytes > 0) {
  423. // adts and mp4 when not at end of chunk
  424. _buf_inc_readp(streambuf, bytes);
  425. a->pos += bytes;
  426. } else {
  427. // error which doesn't advance streambuf - end
  428. endstream = true;
  429. }
  430. UNLOCK_S;
  431. if (endstream) {
  432. LOG_WARN("unable to decode further");
  433. return DECODE_ERROR;
  434. }
  435. if (!info.outputSamps) {
  436. a->empty = true;
  437. return DECODE_RUNNING;
  438. }
  439. frames = info.outputSamps / info.nChans;
  440. if (a->skip) {
  441. u32_t skip;
  442. if (a->empty) {
  443. a->empty = false;
  444. a->skip -= frames;
  445. LOG_DEBUG("gapless: first frame empty, skipped %u frames at start", frames);
  446. }
  447. skip = min(frames, a->skip);
  448. LOG_DEBUG("gapless: skipping %u frames at start", skip);
  449. frames -= skip;
  450. a->skip -= skip;
  451. iptr += skip * info.nChans;
  452. }
  453. if (a->samples) {
  454. if (a->samples < frames) {
  455. LOG_DEBUG("gapless: trimming %u frames from end", frames - a->samples);
  456. frames = (frames_t)a->samples;
  457. }
  458. a->samples -= frames;
  459. }
  460. LOG_SDEBUG("write %u frames", frames);
  461. LOCK_O_direct;
  462. while (frames > 0) {
  463. frames_t f;
  464. frames_t count;
  465. ISAMPLE_T *optr;
  466. IF_DIRECT(
  467. f = _buf_cont_write(outputbuf) / BYTES_PER_FRAME;
  468. optr = (ISAMPLE_T *)outputbuf->writep;
  469. );
  470. IF_PROCESS(
  471. f = process.max_in_frames;
  472. optr = (ISAMPLE_T *)process.inbuf;
  473. );
  474. f = min(f, frames);
  475. count = f;
  476. if (info.nChans == 2) {
  477. #if BYTES_PER_FRAME == 4
  478. memcpy(optr, iptr, count * BYTES_PER_FRAME);
  479. iptr += count * 2;
  480. #else
  481. while (count--) {
  482. *optr++ = ALIGN(*iptr++);
  483. *optr++ = ALIGN(*iptr++);
  484. }
  485. #endif
  486. } else if (info.nChans == 1) {
  487. while (count--) {
  488. *optr++ = ALIGN(*iptr);
  489. *optr++ = ALIGN(*iptr++);
  490. }
  491. } else {
  492. LOG_WARN("unsupported number of channels");
  493. }
  494. frames -= f;
  495. IF_DIRECT(
  496. _buf_inc_writep(outputbuf, f * BYTES_PER_FRAME);
  497. );
  498. IF_PROCESS(
  499. process.in_frames = f;
  500. if (frames) LOG_ERROR("unhandled case");
  501. );
  502. }
  503. UNLOCK_O_direct;
  504. return DECODE_RUNNING;
  505. }
  506. static void helixaac_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  507. LOG_INFO("opening %s stream", size == '2' ? "adts" : "mp4");
  508. a->type = size;
  509. a->pos = a->consume = a->sample = a->nextchunk = 0;
  510. if (a->chunkinfo) {
  511. free(a->chunkinfo);
  512. }
  513. if (a->stsc) {
  514. free(a->stsc);
  515. }
  516. a->chunkinfo = NULL;
  517. a->stsc = NULL;
  518. a->skip = 0;
  519. a->samples = 0;
  520. a->sttssamples = 0;
  521. a->empty = false;
  522. if (a->hAac) {
  523. // always free decoder as flush only works when no parameter has changed
  524. HAAC(a, FreeDecoder, a->hAac);
  525. } else {
  526. a->write_buf = malloc(FRAME_BUF * 4);
  527. a->wrap_buf = malloc(WRAPBUF_LEN);
  528. }
  529. a->hAac = HAAC(a, InitDecoder);
  530. }
  531. static void helixaac_close(void) {
  532. HAAC(a, FreeDecoder, a->hAac);
  533. a->hAac = NULL;
  534. if (a->chunkinfo) {
  535. free(a->chunkinfo);
  536. a->chunkinfo = NULL;
  537. }
  538. if (a->stsc) {
  539. free(a->stsc);
  540. a->stsc = NULL;
  541. }
  542. free(a->write_buf);
  543. free(a->wrap_buf);
  544. }
  545. static bool load_helixaac() {
  546. #if !LINKALL
  547. void *handle = dlopen(LIBHELIX-AAC, RTLD_NOW);
  548. char *err;
  549. if (!handle) {
  550. LOG_INFO("dlerror: %s", dlerror());
  551. return false;
  552. }
  553. // load symbols here
  554. if ((err = dlerror()) != NULL) {
  555. LOG_INFO("dlerror: %s", err);
  556. return false;
  557. }
  558. LOG_INFO("loaded "LIBHELIX-AAC"");
  559. #endif
  560. return true;
  561. }
  562. struct codec *register_helixaac(void) {
  563. static struct codec ret = {
  564. 'a', // id
  565. "aac", // types
  566. WRAPBUF_LEN, // min read
  567. 20480, // min space
  568. helixaac_open, // open
  569. helixaac_close, // close
  570. helixaac_decode, // decode
  571. };
  572. a = malloc(sizeof(struct helixaac));
  573. if (!a) {
  574. return NULL;
  575. }
  576. a->hAac = NULL;
  577. a->chunkinfo = NULL;
  578. a->stsc = NULL;
  579. if (!load_helixaac()) {
  580. return NULL;
  581. }
  582. LOG_INFO("using helix-aac to decode aac");
  583. return &ret;
  584. }