2
0

helix-aac.c 18 KB

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