flac.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Squeezelite - lightweight headless squeezeplay emulator for linux
  3. *
  4. * (c) Adrian Smith 2012, triode1@btinternet.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "squeezelite.h"
  21. #include <FLAC/stream_decoder.h>
  22. #if BYTES_PER_FRAME == 4
  23. #define ALIGN8(n) (n << 8)
  24. #define ALIGN16(n) (n)
  25. #define ALIGN24(n) (n >> 8)
  26. #define ALIGN32(n) (n >> 16)
  27. #else
  28. #define ALIGN8(n) (n << 24)
  29. #define ALIGN16(n) (n << 16)
  30. #define ALIGN24(n) (n << 8)
  31. #define ALIGN32(n) (n)
  32. #endif
  33. struct flac {
  34. FLAC__StreamDecoder *decoder;
  35. #if !LINKALL
  36. // FLAC symbols to be dynamically loaded
  37. const char **FLAC__StreamDecoderErrorStatusString;
  38. const char **FLAC__StreamDecoderStateString;
  39. FLAC__StreamDecoder * (* FLAC__stream_decoder_new)(void);
  40. FLAC__bool (* FLAC__stream_decoder_reset)(FLAC__StreamDecoder *decoder);
  41. void (* FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
  42. FLAC__StreamDecoderInitStatus (* FLAC__stream_decoder_init_stream)(
  43. FLAC__StreamDecoder *decoder,
  44. FLAC__StreamDecoderReadCallback read_callback,
  45. FLAC__StreamDecoderSeekCallback seek_callback,
  46. FLAC__StreamDecoderTellCallback tell_callback,
  47. FLAC__StreamDecoderLengthCallback length_callback,
  48. FLAC__StreamDecoderEofCallback eof_callback,
  49. FLAC__StreamDecoderWriteCallback write_callback,
  50. FLAC__StreamDecoderMetadataCallback metadata_callback,
  51. FLAC__StreamDecoderErrorCallback error_callback,
  52. void *client_data
  53. );
  54. FLAC__bool (* FLAC__stream_decoder_process_single)(FLAC__StreamDecoder *decoder);
  55. FLAC__StreamDecoderState (* FLAC__stream_decoder_get_state)(const FLAC__StreamDecoder *decoder);
  56. #endif
  57. };
  58. static struct flac *f;
  59. extern log_level loglevel;
  60. extern struct buffer *streambuf;
  61. extern struct buffer *outputbuf;
  62. extern struct streamstate stream;
  63. extern struct outputstate output;
  64. extern struct decodestate decode;
  65. extern struct processstate process;
  66. #define LOCK_S mutex_lock(streambuf->mutex)
  67. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  68. #define LOCK_O mutex_lock(outputbuf->mutex)
  69. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  70. #if PROCESS
  71. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  72. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  73. #define IF_DIRECT(x) if (decode.direct) { x }
  74. #define IF_PROCESS(x) if (!decode.direct) { x }
  75. #else
  76. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  77. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  78. #define IF_DIRECT(x) { x }
  79. #define IF_PROCESS(x)
  80. #endif
  81. #if LINKALL
  82. #define FLAC(h, fn, ...) (FLAC__ ## fn)(__VA_ARGS__)
  83. #define FLAC_A(h, a) (FLAC__ ## a)
  84. #else
  85. #define FLAC(h, fn, ...) (h)->FLAC__##fn(__VA_ARGS__)
  86. #define FLAC_A(h, a) (h)->FLAC__ ## a
  87. #endif
  88. static FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *want, void *client_data) {
  89. size_t bytes;
  90. bool end;
  91. LOCK_S;
  92. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  93. bytes = min(bytes, *want);
  94. end = (stream.state <= DISCONNECT && bytes == 0);
  95. memcpy(buffer, streambuf->readp, bytes);
  96. _buf_inc_readp(streambuf, bytes);
  97. UNLOCK_S;
  98. *want = bytes;
  99. return end ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  100. }
  101. static FLAC__StreamDecoderWriteStatus write_cb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
  102. const FLAC__int32 *const buffer[], void *client_data) {
  103. size_t frames = frame->header.blocksize;
  104. unsigned bits_per_sample = frame->header.bits_per_sample;
  105. unsigned channels = frame->header.channels;
  106. FLAC__int32 *lptr = (FLAC__int32 *)buffer[0];
  107. FLAC__int32 *rptr = (FLAC__int32 *)buffer[channels > 1 ? 1 : 0];
  108. if (decode.new_stream) {
  109. LOCK_O;
  110. LOG_INFO("setting track_start");
  111. output.track_start = outputbuf->writep;
  112. decode.new_stream = false;
  113. #if DSD
  114. #if SL_LITTLE_ENDIAN
  115. #define MARKER_OFFSET 2
  116. #else
  117. #define MARKER_OFFSET 1
  118. #endif
  119. if (bits_per_sample == 24 && is_stream_dop(((u8_t *)lptr) + MARKER_OFFSET, ((u8_t *)rptr) + MARKER_OFFSET, 4, frames)) {
  120. LOG_INFO("file contains DOP");
  121. if (output.dsdfmt == DOP_S24_LE || output.dsdfmt == DOP_S24_3LE)
  122. output.next_fmt = output.dsdfmt;
  123. else
  124. output.next_fmt = DOP;
  125. output.next_sample_rate = frame->header.sample_rate;
  126. output.fade = FADE_INACTIVE;
  127. } else {
  128. output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
  129. output.next_fmt = PCM;
  130. if (output.fade_mode) _checkfade(true);
  131. }
  132. #else
  133. output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
  134. if (output.fade_mode) _checkfade(true);
  135. #endif
  136. UNLOCK_O;
  137. }
  138. LOCK_O_direct;
  139. while (frames > 0) {
  140. frames_t f;
  141. frames_t count;
  142. ISAMPLE_T *optr;
  143. IF_DIRECT(
  144. optr = (ISAMPLE_T *)outputbuf->writep;
  145. f = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  146. );
  147. IF_PROCESS(
  148. optr = (ISAMPLE_T *)process.inbuf;
  149. f = process.max_in_frames;
  150. );
  151. f = min(f, frames);
  152. count = f;
  153. if (bits_per_sample == 8) {
  154. while (count--) {
  155. *optr++ = ALIGN8(*lptr++);
  156. *optr++ = ALIGN8(*rptr++);
  157. }
  158. } else if (bits_per_sample == 16) {
  159. while (count--) {
  160. *optr++ = ALIGN16(*lptr++);
  161. *optr++ = ALIGN16(*rptr++);
  162. }
  163. } else if (bits_per_sample == 24) {
  164. while (count--) {
  165. *optr++ = ALIGN24(*lptr++);
  166. *optr++ = ALIGN24(*rptr++);
  167. }
  168. } else if (bits_per_sample == 32) {
  169. while (count--) {
  170. *optr++ = ALIGN32(*lptr++);
  171. *optr++ = ALIGN32(*rptr++);
  172. }
  173. } else {
  174. LOG_ERROR("unsupported bits per sample: %u", bits_per_sample);
  175. }
  176. frames -= f;
  177. IF_DIRECT(
  178. _buf_inc_writep(outputbuf, f * BYTES_PER_FRAME);
  179. );
  180. IF_PROCESS(
  181. process.in_frames = f;
  182. if (frames) LOG_ERROR("unhandled case");
  183. );
  184. }
  185. UNLOCK_O_direct;
  186. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  187. }
  188. static void error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
  189. LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderErrorStatusString)[status]);
  190. }
  191. static void flac_open(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness) {
  192. if (f->decoder) {
  193. FLAC(f, stream_decoder_reset, f->decoder);
  194. } else {
  195. f->decoder = FLAC(f, stream_decoder_new);
  196. }
  197. FLAC(f, stream_decoder_init_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
  198. }
  199. static void flac_close(void) {
  200. FLAC(f, stream_decoder_delete, f->decoder);
  201. f->decoder = NULL;
  202. }
  203. static decode_state flac_decode(void) {
  204. bool ok = FLAC(f, stream_decoder_process_single, f->decoder);
  205. FLAC__StreamDecoderState state = FLAC(f, stream_decoder_get_state, f->decoder);
  206. if (!ok && state != FLAC__STREAM_DECODER_END_OF_STREAM) {
  207. LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderStateString)[state]);
  208. };
  209. if (state == FLAC__STREAM_DECODER_END_OF_STREAM) {
  210. return DECODE_COMPLETE;
  211. } else if (state > FLAC__STREAM_DECODER_END_OF_STREAM) {
  212. return DECODE_ERROR;
  213. } else {
  214. return DECODE_RUNNING;
  215. }
  216. }
  217. static bool load_flac() {
  218. #if !LINKALL
  219. void *handle = dlopen(LIBFLAC, RTLD_NOW);
  220. char *err;
  221. if (!handle) {
  222. LOG_INFO("dlerror: %s", dlerror());
  223. return false;
  224. }
  225. f->FLAC__StreamDecoderErrorStatusString = dlsym(handle, "FLAC__StreamDecoderErrorStatusString");
  226. f->FLAC__StreamDecoderStateString = dlsym(handle, "FLAC__StreamDecoderStateString");
  227. f->FLAC__stream_decoder_new = dlsym(handle, "FLAC__stream_decoder_new");
  228. f->FLAC__stream_decoder_reset = dlsym(handle, "FLAC__stream_decoder_reset");
  229. f->FLAC__stream_decoder_delete = dlsym(handle, "FLAC__stream_decoder_delete");
  230. f->FLAC__stream_decoder_init_stream = dlsym(handle, "FLAC__stream_decoder_init_stream");
  231. f->FLAC__stream_decoder_process_single = dlsym(handle, "FLAC__stream_decoder_process_single");
  232. f->FLAC__stream_decoder_get_state = dlsym(handle, "FLAC__stream_decoder_get_state");
  233. if ((err = dlerror()) != NULL) {
  234. LOG_INFO("dlerror: %s", err);
  235. return false;
  236. }
  237. LOG_INFO("loaded "LIBFLAC);
  238. #endif
  239. return true;
  240. }
  241. struct codec *register_flac(void) {
  242. static struct codec ret = {
  243. 'f', // id
  244. "flc", // types
  245. 16384, // min read
  246. 204800, // min space
  247. flac_open, // open
  248. flac_close, // close
  249. flac_decode, // decode
  250. };
  251. f = malloc(sizeof(struct flac));
  252. if (!f) {
  253. return NULL;
  254. }
  255. f->decoder = NULL;
  256. if (!load_flac()) {
  257. return NULL;
  258. }
  259. LOG_INFO("using flac to decode flc");
  260. return &ret;
  261. }