helix-aac.c 16 KB

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