flac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <stdarg.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <fcntl.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #include <FLAC/stream_decoder.h>
  15. #include "../au.h"
  16. #include "../common.h"
  17. #include "afile.h"
  18. enum {
  19. A_NULL,
  20. A_SIZE,
  21. A_RATE,
  22. A_CHAN,
  23. A_ENC
  24. };
  25. struct afile {
  26. FLAC__StreamDecoder *dec;
  27. void *buf;
  28. uint64_t sample;
  29. size_t icount;
  30. size_t size;
  31. size_t index;
  32. int first;
  33. int again;
  34. int fd;
  35. };
  36. static FLAC__StreamDecoderReadStatus fdcb_read(
  37. const FLAC__StreamDecoder *, FLAC__byte[], size_t *, void *);
  38. static FLAC__StreamDecoderSeekStatus fdcb_seek(
  39. const FLAC__StreamDecoder *, FLAC__uint64, void *);
  40. static FLAC__StreamDecoderTellStatus fdcb_tell(
  41. const FLAC__StreamDecoder *, FLAC__uint64 *, void *);
  42. static FLAC__StreamDecoderLengthStatus fdcb_length(
  43. const FLAC__StreamDecoder *, FLAC__uint64 *, void *);
  44. static FLAC__bool fdcb_eof(const FLAC__StreamDecoder *, void *);
  45. static FLAC__StreamDecoderWriteStatus fdcb_write(
  46. const FLAC__StreamDecoder *, const FLAC__Frame *,
  47. const FLAC__int32 * const [], void *);
  48. static void fdcb_meta(const FLAC__StreamDecoder *,
  49. const FLAC__StreamMetadata *, void *);
  50. static void fdcb_error(const FLAC__StreamDecoder *,
  51. FLAC__StreamDecoderErrorStatus, void *);
  52. const struct a_lookup stab[] = {
  53. { "size", A_SIZE },
  54. { "enc", A_ENC },
  55. { "encoding", A_ENC },
  56. { "chan", A_CHAN },
  57. { "channel", A_CHAN },
  58. { "channels", A_CHAN },
  59. { "rate", A_RATE },
  60. { NULL, A_NULL }
  61. };
  62. struct afile *
  63. af_open(const char *file) {
  64. int fd;
  65. if (file == NULL)
  66. ERR(EINVAL, NULL);
  67. if ((fd = open(file, O_RDONLY)) < 0)
  68. return (NULL);
  69. return (af_fdopen(fd));
  70. }
  71. struct afile *
  72. af_fdopen(int fd)
  73. {
  74. struct afile *a;
  75. if (fd < 0)
  76. ERR(EBADF, NULL);
  77. if ((a = malloc(sizeof(struct afile))) == NULL)
  78. return (NULL);
  79. a->sample = 0;
  80. a->buf = NULL;
  81. a->icount = 0;
  82. a->size = 0;
  83. a->index = 0;
  84. a->fd = fd;
  85. if ((a->dec = FLAC__stream_decoder_new()) == NULL) {
  86. free(a);
  87. return (NULL);
  88. }
  89. if (FLAC__stream_decoder_init_stream(a->dec, &fdcb_read,
  90. &fdcb_seek, &fdcb_tell, &fdcb_length, &fdcb_eof,
  91. &fdcb_write, &fdcb_meta, &fdcb_error, a) !=
  92. FLAC__STREAM_DECODER_INIT_STATUS_OK) {
  93. FLAC__stream_decoder_delete(a->dec);
  94. free(a);
  95. return (NULL);
  96. }
  97. if (!FLAC__stream_decoder_process_until_end_of_metadata(
  98. a->dec)) {
  99. af_close(a);
  100. return (NULL);
  101. }
  102. FLAC__stream_decoder_process_single(a->dec);
  103. switch (FLAC__stream_decoder_get_state(a->dec)) {
  104. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  105. break;
  106. case FLAC__STREAM_DECODER_READ_METADATA:
  107. break;
  108. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  109. break;
  110. case FLAC__STREAM_DECODER_READ_FRAME:
  111. break;
  112. case FLAC__STREAM_DECODER_END_OF_STREAM:
  113. break;
  114. case FLAC__STREAM_DECODER_OGG_ERROR:
  115. /* FALLTHORUGH */
  116. case FLAC__STREAM_DECODER_SEEK_ERROR:
  117. /* FALLTHORUGH */
  118. case FLAC__STREAM_DECODER_ABORTED:
  119. /* FALLTHORUGH */
  120. case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
  121. /* FALLTHORUGH */
  122. case FLAC__STREAM_DECODER_UNINITIALIZED:
  123. af_close(a);
  124. return (NULL);
  125. break;
  126. }
  127. return (a);
  128. }
  129. void
  130. af_close(struct afile *a)
  131. {
  132. if (a == NULL)
  133. return;
  134. FLAC__stream_decoder_finish(a->dec);
  135. FLAC__stream_decoder_delete(a->dec);
  136. if (a->buf != NULL)
  137. free(a->buf);
  138. free(a);
  139. }
  140. ssize_t
  141. af_read(struct afile *a, void *b, size_t s)
  142. {
  143. size_t ssize;
  144. size_t i = 0;
  145. size_t n;
  146. if (a == NULL || b == NULL)
  147. ERR(EINVAL, -1);
  148. if (s == 0)
  149. return (0);
  150. if ((ssize = (FLAC__stream_decoder_get_bits_per_sample(
  151. a->dec) + 7) / 8 *
  152. FLAC__stream_decoder_get_channels(a->dec)) == 0)
  153. ERR(EINVAL, -1);
  154. s -= s % ssize;
  155. for (;;) {
  156. if (a->index < a->icount) {
  157. n = MIN(s - i, a->icount - a->index);
  158. (void)memcpy((char *)b + i, (char *)a->buf +
  159. a->index, n);
  160. a->index += n;
  161. if ((i += n) == s)
  162. break;
  163. }
  164. a->index = a->icount = a->again = 0;
  165. FLAC__stream_decoder_process_single(a->dec);
  166. switch (FLAC__stream_decoder_get_state(a->dec)) {
  167. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  168. break;
  169. case FLAC__STREAM_DECODER_READ_METADATA:
  170. break;
  171. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  172. break;
  173. case FLAC__STREAM_DECODER_READ_FRAME:
  174. break;
  175. case FLAC__STREAM_DECODER_END_OF_STREAM:
  176. goto ret;
  177. case FLAC__STREAM_DECODER_OGG_ERROR:
  178. return (-1);
  179. case FLAC__STREAM_DECODER_SEEK_ERROR:
  180. return (-1);
  181. case FLAC__STREAM_DECODER_ABORTED:
  182. return (-1);
  183. case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
  184. return (-1);
  185. case FLAC__STREAM_DECODER_UNINITIALIZED:
  186. return (-1);
  187. }
  188. if (a->again) {
  189. if (i == 0)
  190. return (-1);
  191. else
  192. break;
  193. }
  194. }
  195. ret:
  196. if (i > 0)
  197. a->sample += i / ssize;
  198. return (i);
  199. }
  200. int
  201. af_get(struct afile *a, const char *s, ...)
  202. {
  203. va_list ap;
  204. uint64_t u64;
  205. ssize_t r;
  206. unsigned u;
  207. unsigned o;
  208. int n;
  209. int v;
  210. if (a == NULL || s == NULL)
  211. ERR(EINVAL, -1);
  212. va_start(ap, s);
  213. for (n = 0; (r = a_lookup(stab, s, &v)) > 0; n++, s += r)
  214. switch (v) {
  215. case A_SIZE:
  216. u64 = FLAC__stream_decoder_get_total_samples(
  217. a->dec);
  218. u = FLAC__stream_decoder_get_channels(a->dec);
  219. o = (FLAC__stream_decoder_get_bits_per_sample(
  220. a->dec) + 7) / 8;
  221. if (u64 == 0 || u == 0 || o == 0 ||
  222. u64 > UINT32_MAX / u / o)
  223. *va_arg(ap, uint32_t *) = UINT32_MAX;
  224. else
  225. *va_arg(ap, uint32_t *) = u64 * u * o;
  226. break;
  227. case A_RATE:
  228. *va_arg(ap, uint32_t *) =
  229. FLAC__stream_decoder_get_sample_rate
  230. (a->dec);
  231. break;
  232. case A_CHAN:
  233. *va_arg(ap, uint32_t *) =
  234. FLAC__stream_decoder_get_channels(a->dec);
  235. break;
  236. case A_ENC:
  237. if ((u =
  238. FLAC__stream_decoder_get_bits_per_sample(
  239. a->dec)) == 0)
  240. goto err;
  241. switch ((u - 1) / 8) {
  242. case 0:
  243. *va_arg(ap, uint32_t *) = AU_PCM8;
  244. break;
  245. case 1:
  246. *va_arg(ap, uint32_t *) = AU_PCM16;
  247. break;
  248. case 2:
  249. *va_arg(ap, uint32_t *) = AU_PCM24;
  250. break;
  251. case 3:
  252. *va_arg(ap, uint32_t *) = AU_PCM32;
  253. break;
  254. default:
  255. goto err;
  256. }
  257. break;
  258. default:
  259. goto err;
  260. }
  261. if (r == 0) {
  262. va_end(ap);
  263. return (n);
  264. }
  265. err:
  266. va_end(ap);
  267. ERR(EINVAL, n);
  268. }
  269. int
  270. af_set(struct afile *a, const char *s, ...)
  271. {
  272. ERR(EINVAL, -1);
  273. }
  274. struct timespec
  275. af_seek(struct afile *a, struct timespec ts, int whence)
  276. {
  277. const struct timespec tserr = { -1, -1 };
  278. unsigned rate;
  279. int negative;
  280. uint64_t c;
  281. uint64_t l;
  282. uint64_t t;
  283. rate = FLAC__stream_decoder_get_sample_rate(a->dec);
  284. negative = ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec < 0);
  285. if (negative) {
  286. ts.tv_sec = -ts.tv_sec;
  287. ts.tv_nsec = -ts.tv_nsec;
  288. }
  289. if (ts.tv_nsec < 0) {
  290. ts.tv_sec--;
  291. ts.tv_nsec = 1000000000 - ts.tv_nsec;
  292. }
  293. t = (uint64_t)ts.tv_sec * (uint64_t)rate +
  294. (uint64_t)ts.tv_nsec * (uint64_t)rate /
  295. UINT64_C(1000000000);
  296. c = FLAC__stream_decoder_get_total_samples(a->dec);
  297. switch (whence) {
  298. case SEEK_CUR:
  299. l = a->sample;
  300. if (t == 0)
  301. goto ret;
  302. break;
  303. case SEEK_END:
  304. l = c;
  305. negative = !negative;
  306. break;
  307. case SEEK_SET:
  308. l = 0;
  309. break;
  310. default:
  311. ERR(EINVAL, tserr);
  312. }
  313. l = negative ? (t > l ? 0 : l - t) : l + t;
  314. if (l >= c)
  315. ERR(ERANGE, tserr);
  316. else if (!FLAC__stream_decoder_seek_absolute(a->dec, l))
  317. ERR(EOPNOTSUPP, tserr);
  318. a->icount = 0;
  319. a->sample = l;
  320. ret:
  321. ts.tv_sec = l / (uint64_t)rate;
  322. ts.tv_nsec = UINT64_C(1000000000) * (l % (uint64_t)rate) /
  323. (uint64_t)rate;
  324. return (ts);
  325. }
  326. struct timespec
  327. af_length(struct afile *a)
  328. {
  329. struct timespec ts;
  330. uint64_t n;
  331. unsigned rate;
  332. n = FLAC__stream_decoder_get_total_samples(a->dec);
  333. rate = FLAC__stream_decoder_get_sample_rate(a->dec);
  334. ts.tv_sec = n / (uint64_t)rate;
  335. ts.tv_nsec = UINT64_C(1000000000) * (n % (uint64_t)rate) /
  336. (uint64_t)rate;
  337. return (ts);
  338. }
  339. FLAC__StreamDecoderReadStatus
  340. fdcb_read(const FLAC__StreamDecoder *dec, FLAC__byte b[], size_t *s,
  341. void *ctx)
  342. {
  343. struct afile *a;
  344. size_t t;
  345. ssize_t r;
  346. assert(dec != NULL && b != NULL && s != NULL && ctx != NULL);
  347. a = ctx;
  348. t = *s;
  349. if ((r = read(a->fd, b, t)) < 0) {
  350. *s = 0;
  351. if (errno != EAGAIN)
  352. return (FLAC__STREAM_DECODER_READ_STATUS_ABORT);
  353. a->again = 1;
  354. return (FLAC__STREAM_DECODER_READ_STATUS_CONTINUE);
  355. }
  356. *s = r;
  357. return (r == 0 ?
  358. FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM :
  359. FLAC__STREAM_DECODER_READ_STATUS_CONTINUE);
  360. }
  361. FLAC__StreamDecoderSeekStatus
  362. fdcb_seek(const FLAC__StreamDecoder *dec, FLAC__uint64 v, void *ctx)
  363. {
  364. struct afile *a;
  365. assert(dec != NULL && ctx != NULL);
  366. a = ctx;
  367. if (lseek(a->fd, v, SEEK_SET) < 0)
  368. return (errno == ESPIPE ?
  369. FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED :
  370. FLAC__STREAM_DECODER_SEEK_STATUS_ERROR);
  371. return (FLAC__STREAM_DECODER_SEEK_STATUS_OK);
  372. }
  373. FLAC__StreamDecoderTellStatus
  374. fdcb_tell(const FLAC__StreamDecoder *dec, FLAC__uint64 *v, void *ctx)
  375. {
  376. struct afile *a;
  377. off_t r;
  378. assert(dec != NULL && v != NULL && ctx != NULL);
  379. a = ctx;
  380. if ((r = lseek(a->fd, 0, SEEK_CUR)) < 0)
  381. return (errno == ESPIPE ?
  382. FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED :
  383. FLAC__STREAM_DECODER_TELL_STATUS_ERROR);
  384. if (r > UINT64_MAX)
  385. ERR(EOVERFLOW, FLAC__STREAM_DECODER_TELL_STATUS_ERROR);
  386. *v = r;
  387. return (FLAC__STREAM_DECODER_TELL_STATUS_OK);
  388. }
  389. FLAC__StreamDecoderLengthStatus
  390. fdcb_length(const FLAC__StreamDecoder *dec, FLAC__uint64 *v, void *ctx)
  391. {
  392. struct afile *a;
  393. struct stat st;
  394. assert(dec != NULL && v != NULL && ctx != NULL);
  395. a = ctx;
  396. if (fstat(a->fd, &st) != 0)
  397. return (FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR);
  398. if (!S_ISREG(st.st_mode))
  399. return (FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED);
  400. if (st.st_size > UINT64_MAX)
  401. ERR(EOVERFLOW,
  402. FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR);
  403. *v = st.st_size;
  404. return (FLAC__STREAM_DECODER_LENGTH_STATUS_OK);
  405. }
  406. FLAC__bool
  407. fdcb_eof(const FLAC__StreamDecoder *dec, void *ctx)
  408. {
  409. assert(dec != NULL && ctx != NULL);
  410. return (0);
  411. }
  412. FLAC__StreamDecoderWriteStatus
  413. fdcb_write(const FLAC__StreamDecoder *dec, const FLAC__Frame *frm,
  414. const FLAC__int32 * const buf[], void *ctx)
  415. {
  416. struct afile *a = ctx;
  417. uint8_t *b;
  418. void *p;
  419. size_t i;
  420. uint32_t u;
  421. int32_t v;
  422. unsigned t;
  423. unsigned f;
  424. int n;
  425. int c;
  426. /* This is probably unnecessary */
  427. for (c = 0; c < frm->header.channels; c++)
  428. if (buf[c] == NULL)
  429. goto err;
  430. f = frm->header.bits_per_sample;
  431. t = (f + 7) & ~7;
  432. i = frm->header.blocksize * frm->header.channels * (t / 8);
  433. if (i > a->size) {
  434. if ((p = (a->buf == NULL ? malloc(i) :
  435. realloc(a->buf, i))) == NULL)
  436. goto err;
  437. a->buf = p;
  438. a->size = i;
  439. }
  440. a->icount = i;
  441. for(i = 0, b = a->buf; i < frm->header.blocksize; i++)
  442. for (c = 0; c < frm->header.channels; c++) {
  443. v = buf[c][i];
  444. if ((n = v < 0) != 0)
  445. v = -v;
  446. f = frm->header.bits_per_sample;
  447. while (f + f < t) {
  448. v |= (v << f);
  449. f += f;
  450. }
  451. if (f < t)
  452. v = (v << (t - f)) |
  453. (v >> (f - (t - f)));
  454. u = n ? ~(uint32_t)v + UINT32_C(1) :
  455. (uint32_t)v;
  456. for (f = t / 8; f > 0; f--)
  457. *b++ = (u >> (f - 1) * 8) &
  458. UINT8_C(0xFF);
  459. }
  460. return (FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE);
  461. err:
  462. return (FLAC__STREAM_DECODER_WRITE_STATUS_ABORT);
  463. }
  464. void
  465. fdcb_meta(const FLAC__StreamDecoder *dec,
  466. const FLAC__StreamMetadata *meta, void *ctx)
  467. {
  468. /* The information will be queried with the
  469. * FLAC__stream_decoder_get_* functions.
  470. */;
  471. }
  472. void
  473. fdcb_error(const FLAC__StreamDecoder *dec,
  474. FLAC__StreamDecoderErrorStatus st, void *ctx)
  475. {
  476. /* The state will be checked for errors. */;
  477. }