helix-aac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. *samplerate_p = info.sampRateCore;
  140. if (desc_len > 2 && ((ptr[0] << 3) | (ptr[1] >> 5)) == 0x2b7 && (ptr[1] & 0x1f) == 0x05 && (ptr[2] & 0x80)) {
  141. LOG_WARN("AAC SBR mode activated => high CPU consumption (please proxy)");
  142. *samplerate_p = rates[(ptr[2] & 0x78) >> 3];
  143. }
  144. HAAC(a, SetRawBlockParams, a->hAac, 0, &info);
  145. LOG_DEBUG("playable aac track: %u (p:%x, r:%d, c:%d)", trak, info.profile, info.sampRateCore, info.nChans);
  146. play = trak;
  147. }
  148. // extract the total number of samples from stts
  149. if (!strcmp(type, "stts") && bytes > len) {
  150. u32_t i;
  151. u8_t *ptr = streambuf->readp + 12;
  152. u32_t entries = unpackN((u32_t *)ptr);
  153. ptr += 4;
  154. for (i = 0; i < entries; ++i) {
  155. u32_t count = unpackN((u32_t *)ptr);
  156. u32_t size = unpackN((u32_t *)(ptr + 4));
  157. a->sttssamples += count * size;
  158. ptr += 8;
  159. }
  160. LOG_DEBUG("total number of samples contained in stts: " FMT_u64, a->sttssamples);
  161. }
  162. // stash sample to chunk info, assume it comes before stco
  163. if (!strcmp(type, "stsc") && bytes > len && !a->chunkinfo) {
  164. a->stsc = malloc(len - 12);
  165. if (a->stsc == NULL) {
  166. LOG_WARN("malloc fail");
  167. return -1;
  168. }
  169. memcpy(a->stsc, streambuf->readp + 12, len - 12);
  170. }
  171. // build offsets table from stco and stored stsc
  172. if (!strcmp(type, "stco") && bytes > len && play == trak) {
  173. u32_t i;
  174. // extract chunk offsets
  175. u8_t *ptr = streambuf->readp + 12;
  176. u32_t entries = unpackN((u32_t *)ptr);
  177. ptr += 4;
  178. a->chunkinfo = malloc(sizeof(struct chunk_table) * (entries + 1));
  179. if (a->chunkinfo == NULL) {
  180. LOG_WARN("malloc fail");
  181. return -1;
  182. }
  183. for (i = 0; i < entries; ++i) {
  184. a->chunkinfo[i].offset = unpackN((u32_t *)ptr);
  185. a->chunkinfo[i].sample = 0;
  186. ptr += 4;
  187. }
  188. a->chunkinfo[i].sample = 0;
  189. a->chunkinfo[i].offset = 0;
  190. // fill in first sample id for each chunk from stored stsc
  191. if (a->stsc) {
  192. u32_t stsc_entries = unpackN((u32_t *)a->stsc);
  193. u32_t sample = 0;
  194. u32_t last = 0, last_samples = 0;
  195. u8_t *ptr = (u8_t *)a->stsc + 4;
  196. while (stsc_entries--) {
  197. u32_t first = unpackN((u32_t *)ptr);
  198. u32_t samples = unpackN((u32_t *)(ptr + 4));
  199. if (last) {
  200. for (i = last - 1; i < first - 1; ++i) {
  201. a->chunkinfo[i].sample = sample;
  202. sample += last_samples;
  203. }
  204. }
  205. if (stsc_entries == 0) {
  206. for (i = first - 1; i < entries; ++i) {
  207. a->chunkinfo[i].sample = sample;
  208. sample += samples;
  209. }
  210. }
  211. last = first;
  212. last_samples = samples;
  213. ptr += 12;
  214. }
  215. free(a->stsc);
  216. a->stsc = NULL;
  217. }
  218. }
  219. // found media data, advance to start of first chunk and return
  220. if (!strcmp(type, "mdat")) {
  221. _buf_inc_readp(streambuf, 8);
  222. a->pos += 8;
  223. bytes -= 8;
  224. if (play) {
  225. LOG_DEBUG("type: mdat len: %u pos: %u", len, a->pos);
  226. if (a->chunkinfo && a->chunkinfo[0].offset > a->pos) {
  227. u32_t skip = a->chunkinfo[0].offset - a->pos;
  228. LOG_DEBUG("skipping: %u", skip);
  229. if (skip <= bytes) {
  230. _buf_inc_readp(streambuf, skip);
  231. a->pos += skip;
  232. } else {
  233. a->consume = skip;
  234. }
  235. }
  236. a->sample = a->nextchunk = 1;
  237. return 1;
  238. } else {
  239. LOG_DEBUG("type: mdat len: %u, no playable track found", len);
  240. return -1;
  241. }
  242. }
  243. // parse key-value atoms within ilst ---- entries to get encoder padding within iTunSMPB entry for gapless
  244. if (!strcmp(type, "----") && bytes > len) {
  245. u8_t *ptr = streambuf->readp + 8;
  246. u32_t remain = len - 8, size;
  247. if (!memcmp(ptr + 4, "mean", 4) && (size = unpackN((u32_t *)ptr)) < remain) {
  248. ptr += size; remain -= size;
  249. }
  250. if (!memcmp(ptr + 4, "name", 4) && (size = unpackN((u32_t *)ptr)) < remain && !memcmp(ptr + 12, "iTunSMPB", 8)) {
  251. ptr += size; remain -= size;
  252. }
  253. if (!memcmp(ptr + 4, "data", 4) && remain > 16 + 48) {
  254. // data is stored as hex strings: 0 start end samples
  255. u32_t b, c; u64_t d;
  256. if (sscanf((const char *)(ptr + 16), "%x %x %x " FMT_x64, &b, &b, &c, &d) == 4) {
  257. LOG_DEBUG("iTunSMPB start: %u end: %u samples: " FMT_u64, b, c, d);
  258. if (a->sttssamples && a->sttssamples < b + c + d) {
  259. LOG_DEBUG("reducing samples as stts count is less");
  260. d = a->sttssamples - (b + c);
  261. }
  262. a->skip = b;
  263. a->samples = d;
  264. }
  265. }
  266. }
  267. // default to consuming entire box
  268. consume = len;
  269. // read into these boxes so reduce consume
  270. if (!strcmp(type, "moov") || !strcmp(type, "trak") || !strcmp(type, "mdia") || !strcmp(type, "minf") || !strcmp(type, "stbl") ||
  271. !strcmp(type, "udta") || !strcmp(type, "ilst")) {
  272. consume = 8;
  273. }
  274. // special cases which mix mix data in the enclosing box which we want to read into
  275. if (!strcmp(type, "stsd")) consume = 16;
  276. if (!strcmp(type, "mp4a")) consume = 36;
  277. if (!strcmp(type, "meta")) consume = 12;
  278. // consume rest of box if it has been parsed (all in the buffer) or is not one we want to parse
  279. if (bytes >= consume) {
  280. LOG_DEBUG("type: %s len: %u consume: %u", type, len, consume);
  281. _buf_inc_readp(streambuf, consume);
  282. a->pos += consume;
  283. bytes -= consume;
  284. } else if ( !(!strcmp(type, "esds") || !strcmp(type, "stts") || !strcmp(type, "stsc") ||
  285. !strcmp(type, "stco") || !strcmp(type, "----")) ) {
  286. LOG_DEBUG("type: %s len: %u consume: %u - partial consume: %u", type, len, consume, bytes);
  287. _buf_inc_readp(streambuf, bytes);
  288. a->pos += bytes;
  289. a->consume = consume - bytes;
  290. break;
  291. } else if (len > streambuf->size) {
  292. // can't process an atom larger than streambuf!
  293. LOG_ERROR("atom %s too large for buffer %u %u", type, len, streambuf->size);
  294. return -1;
  295. } else {
  296. // make sure there is 'len' contiguous space
  297. _buf_unwrap(streambuf, len);
  298. break;
  299. }
  300. }
  301. return 0;
  302. }
  303. static decode_state helixaac_decode(void) {
  304. size_t bytes_total, bytes_wrap;
  305. int res, bytes;
  306. static AACFrameInfo info;
  307. s16_t *iptr;
  308. u8_t *sptr;
  309. bool endstream;
  310. frames_t frames;
  311. LOCK_S;
  312. bytes_total = _buf_used(streambuf);
  313. bytes_wrap = min(bytes_total, _buf_cont_read(streambuf));
  314. if (stream.state <= DISCONNECT && !bytes_total) {
  315. UNLOCK_S;
  316. return DECODE_COMPLETE;
  317. }
  318. if (a->consume) {
  319. u32_t consume = min(a->consume, bytes_wrap);
  320. LOG_DEBUG("consume: %u of %u", consume, a->consume);
  321. _buf_inc_readp(streambuf, consume);
  322. a->pos += consume;
  323. a->consume -= consume;
  324. UNLOCK_S;
  325. return DECODE_RUNNING;
  326. }
  327. if (decode.new_stream) {
  328. int found = 0;
  329. static unsigned char channels;
  330. static unsigned long samplerate;
  331. if (a->type == '2') {
  332. // adts stream - seek for header
  333. long n = HAAC(a, FindSyncWord, streambuf->readp, bytes_wrap);
  334. LOG_DEBUG("Sync search in %d bytes %d", bytes_wrap, n);
  335. if (n >= 0) {
  336. u8_t *p = streambuf->readp + n;
  337. int bytes = bytes_wrap - n;
  338. if (!HAAC(a, Decode, a->hAac, &p, &bytes, (s16_t*) a->write_buf)) {
  339. HAAC(a, GetLastFrameInfo, a->hAac, &info);
  340. channels = info.nChans;
  341. samplerate = info.sampRateOut;
  342. found = 1;
  343. } else if (n == 0) n++;
  344. HAAC(a, FlushCodec, a->hAac);
  345. bytes_total -= n;
  346. bytes_wrap -= n;
  347. _buf_inc_readp(streambuf, n);
  348. } else {
  349. found = -1;
  350. }
  351. } else {
  352. // mp4 - read header
  353. found = read_mp4_header(&samplerate, &channels);
  354. }
  355. if (found == 1) {
  356. LOCK_O;
  357. output.next_sample_rate = decode_newstream(samplerate, output.supported_rates);
  358. IF_DSD( output.next_fmt = PCM; )
  359. output.track_start = outputbuf->writep;
  360. if (output.fade_mode) _checkfade(true);
  361. decode.new_stream = false;
  362. UNLOCK_O;
  363. LOG_INFO("setting track start, samplerate: %u channels: %u", samplerate, channels);
  364. bytes_total = _buf_used(streambuf);
  365. bytes_wrap = min(bytes_total, _buf_cont_read(streambuf));
  366. // come back later if we don' thave enough data
  367. if (bytes_total < WRAPBUF_LEN) {
  368. UNLOCK_S;
  369. LOG_INFO("need more audio data");
  370. return DECODE_RUNNING;
  371. }
  372. } else if (found == -1) {
  373. LOG_WARN("error reading stream header");
  374. UNLOCK_S;
  375. return DECODE_ERROR;
  376. } else {
  377. // not finished header parsing come back next time
  378. UNLOCK_S;
  379. LOG_INFO("header not found yet");
  380. return DECODE_RUNNING;
  381. }
  382. }
  383. // we always have at least WRAPBUF_LEN unless it's the end of a stream
  384. if (bytes_wrap < WRAPBUF_LEN && bytes_wrap != bytes_total) {
  385. // build a linear buffer if we are crossing the end of streambuf
  386. memcpy(a->wrap_buf, streambuf->readp, bytes_wrap);
  387. memcpy(a->wrap_buf + bytes_wrap, streambuf->buf, min(WRAPBUF_LEN, bytes_total) - bytes_wrap);
  388. sptr = a->wrap_buf;
  389. bytes = bytes_wrap = min(WRAPBUF_LEN, bytes_total);
  390. } else {
  391. sptr = streambuf->readp;
  392. bytes = bytes_wrap;
  393. }
  394. // decode function changes iptr, so can't use streambuf->readp (same for bytes)
  395. res = HAAC(a, Decode, a->hAac, &sptr, &bytes, (s16_t*) a->write_buf);
  396. if (res < 0) {
  397. LOG_WARN("AAC decode error %d", res);
  398. }
  399. HAAC(a, GetLastFrameInfo, a->hAac, &info);
  400. iptr = (s16_t*) a->write_buf;
  401. bytes = bytes_wrap - bytes;
  402. endstream = false;
  403. if (a->chunkinfo && a->chunkinfo[a->nextchunk].offset && a->sample++ == a->chunkinfo[a->nextchunk].sample) {
  404. // mp4 end of chunk - skip to next offset
  405. if (a->chunkinfo[a->nextchunk].offset > a->pos) {
  406. u32_t skip = a->chunkinfo[a->nextchunk].offset - a->pos;
  407. if (skip != bytes) {
  408. LOG_DEBUG("skipping to next chunk pos: %u consumed: %u != skip: %u", a->pos, bytes, skip);
  409. }
  410. if (bytes_total >= skip) {
  411. _buf_inc_readp(streambuf, skip);
  412. a->pos += skip;
  413. } else {
  414. a->consume = skip;
  415. }
  416. a->nextchunk++;
  417. } else {
  418. LOG_ERROR("error: need to skip backwards!");
  419. endstream = true;
  420. }
  421. } else if (bytes > 0) {
  422. // adts and mp4 when not at end of chunk
  423. _buf_inc_readp(streambuf, bytes);
  424. a->pos += bytes;
  425. } else {
  426. // error which doesn't advance streambuf - end
  427. endstream = true;
  428. }
  429. UNLOCK_S;
  430. if (endstream) {
  431. LOG_WARN("unable to decode further");
  432. return DECODE_ERROR;
  433. }
  434. if (!info.outputSamps) {
  435. a->empty = true;
  436. return DECODE_RUNNING;
  437. }
  438. frames = info.outputSamps / info.nChans;
  439. if (a->skip) {
  440. u32_t skip;
  441. if (a->empty) {
  442. a->empty = false;
  443. a->skip -= frames;
  444. LOG_DEBUG("gapless: first frame empty, skipped %u frames at start", frames);
  445. }
  446. skip = min(frames, a->skip);
  447. LOG_DEBUG("gapless: skipping %u frames at start", skip);
  448. frames -= skip;
  449. a->skip -= skip;
  450. iptr += skip * info.nChans;
  451. }
  452. if (a->samples) {
  453. if (a->samples < frames) {
  454. LOG_DEBUG("gapless: trimming %u frames from end", frames - a->samples);
  455. frames = (frames_t)a->samples;
  456. }
  457. a->samples -= frames;
  458. }
  459. LOG_SDEBUG("write %u frames", frames);
  460. LOCK_O_direct;
  461. while (frames > 0) {
  462. frames_t f;
  463. frames_t count;
  464. ISAMPLE_T *optr;
  465. IF_DIRECT(
  466. f = _buf_cont_write(outputbuf) / BYTES_PER_FRAME;
  467. optr = (ISAMPLE_T *)outputbuf->writep;
  468. );
  469. IF_PROCESS(
  470. f = process.max_in_frames;
  471. optr = (ISAMPLE_T *)process.inbuf;
  472. );
  473. f = min(f, frames);
  474. count = f;
  475. if (info.nChans == 2) {
  476. #if BYTES_PER_FRAME == 4
  477. memcpy(optr, iptr, count * BYTES_PER_FRAME);
  478. iptr += count * 2;
  479. #else
  480. while (count--) {
  481. *optr++ = ALIGN(*iptr++);
  482. *optr++ = ALIGN(*iptr++);
  483. }
  484. #endif
  485. } else if (info.nChans == 1) {
  486. while (count--) {
  487. *optr++ = ALIGN(*iptr);
  488. *optr++ = ALIGN(*iptr++);
  489. }
  490. } else {
  491. LOG_WARN("unsupported number of channels");
  492. }
  493. frames -= f;
  494. IF_DIRECT(
  495. _buf_inc_writep(outputbuf, f * BYTES_PER_FRAME);
  496. );
  497. IF_PROCESS(
  498. process.in_frames = f;
  499. if (frames) LOG_ERROR("unhandled case");
  500. );
  501. }
  502. UNLOCK_O_direct;
  503. return DECODE_RUNNING;
  504. }
  505. static void helixaac_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  506. LOG_INFO("opening %s stream", size == '2' ? "adts" : "mp4");
  507. a->type = size;
  508. a->pos = a->consume = a->sample = a->nextchunk = 0;
  509. if (a->chunkinfo) {
  510. free(a->chunkinfo);
  511. }
  512. if (a->stsc) {
  513. free(a->stsc);
  514. }
  515. a->chunkinfo = NULL;
  516. a->stsc = NULL;
  517. a->skip = 0;
  518. a->samples = 0;
  519. a->sttssamples = 0;
  520. a->empty = false;
  521. if (a->hAac) {
  522. // always free decoder as flush only works when no parameter has changed
  523. HAAC(a, FreeDecoder, a->hAac);
  524. } else {
  525. a->write_buf = malloc(FRAME_BUF * 4);
  526. a->wrap_buf = malloc(WRAPBUF_LEN);
  527. }
  528. a->hAac = HAAC(a, InitDecoder);
  529. }
  530. static void helixaac_close(void) {
  531. HAAC(a, FreeDecoder, a->hAac);
  532. a->hAac = NULL;
  533. if (a->chunkinfo) {
  534. free(a->chunkinfo);
  535. a->chunkinfo = NULL;
  536. }
  537. if (a->stsc) {
  538. free(a->stsc);
  539. a->stsc = NULL;
  540. }
  541. free(a->write_buf);
  542. free(a->wrap_buf);
  543. }
  544. static bool load_helixaac() {
  545. #if !LINKALL
  546. void *handle = dlopen(LIBHELIX-AAC, RTLD_NOW);
  547. char *err;
  548. if (!handle) {
  549. LOG_INFO("dlerror: %s", dlerror());
  550. return false;
  551. }
  552. // load symbols here
  553. if ((err = dlerror()) != NULL) {
  554. LOG_INFO("dlerror: %s", err);
  555. return false;
  556. }
  557. LOG_INFO("loaded "LIBHELIX-AAC"");
  558. #endif
  559. return true;
  560. }
  561. struct codec *register_helixaac(void) {
  562. static struct codec ret = {
  563. 'a', // id
  564. "aac", // types
  565. WRAPBUF_LEN, // min read
  566. 20480, // min space
  567. helixaac_open, // open
  568. helixaac_close, // close
  569. helixaac_decode, // decode
  570. };
  571. a = malloc(sizeof(struct helixaac));
  572. if (!a) {
  573. return NULL;
  574. }
  575. a->hAac = NULL;
  576. a->chunkinfo = NULL;
  577. a->stsc = NULL;
  578. if (!load_helixaac()) {
  579. return NULL;
  580. }
  581. LOG_INFO("using helix-aac to decode aac");
  582. return &ret;
  583. }