flac.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. u8_t container;
  36. #if !LINKALL
  37. // FLAC symbols to be dynamically loaded
  38. const char **FLAC__StreamDecoderErrorStatusString;
  39. const char **FLAC__StreamDecoderStateString;
  40. FLAC__StreamDecoder * (* FLAC__stream_decoder_new)(void);
  41. FLAC__bool (* FLAC__stream_decoder_reset)(FLAC__StreamDecoder *decoder);
  42. void (* FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
  43. FLAC__StreamDecoderInitStatus (* FLAC__stream_decoder_init_stream)(
  44. FLAC__StreamDecoder *decoder,
  45. FLAC__StreamDecoderReadCallback read_callback,
  46. FLAC__StreamDecoderSeekCallback seek_callback,
  47. FLAC__StreamDecoderTellCallback tell_callback,
  48. FLAC__StreamDecoderLengthCallback length_callback,
  49. FLAC__StreamDecoderEofCallback eof_callback,
  50. FLAC__StreamDecoderWriteCallback write_callback,
  51. FLAC__StreamDecoderMetadataCallback metadata_callback,
  52. FLAC__StreamDecoderErrorCallback error_callback,
  53. void *client_data
  54. );
  55. FLAC__StreamDecoderInitStatus (* FLAC__stream_decoder_init_ogg_stream)(
  56. FLAC__StreamDecoder *decoder,
  57. FLAC__StreamDecoderReadCallback read_callback,
  58. FLAC__StreamDecoderSeekCallback seek_callback,
  59. FLAC__StreamDecoderTellCallback tell_callback,
  60. FLAC__StreamDecoderLengthCallback length_callback,
  61. FLAC__StreamDecoderEofCallback eof_callback,
  62. FLAC__StreamDecoderWriteCallback write_callback,
  63. FLAC__StreamDecoderMetadataCallback metadata_callback,
  64. FLAC__StreamDecoderErrorCallback error_callback,
  65. void *client_data
  66. );
  67. FLAC__bool (* FLAC__stream_decoder_process_single)(FLAC__StreamDecoder *decoder);
  68. FLAC__StreamDecoderState (* FLAC__stream_decoder_get_state)(const FLAC__StreamDecoder *decoder);
  69. #endif
  70. };
  71. static struct flac *f;
  72. extern log_level loglevel;
  73. extern struct buffer *streambuf;
  74. extern struct buffer *outputbuf;
  75. extern struct streamstate stream;
  76. extern struct outputstate output;
  77. extern struct decodestate decode;
  78. extern struct processstate process;
  79. #define LOCK_S mutex_lock(streambuf->mutex)
  80. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  81. #define LOCK_O mutex_lock(outputbuf->mutex)
  82. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  83. #if PROCESS
  84. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  85. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  86. #define IF_DIRECT(x) if (decode.direct) { x }
  87. #define IF_PROCESS(x) if (!decode.direct) { x }
  88. #else
  89. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  90. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  91. #define IF_DIRECT(x) { x }
  92. #define IF_PROCESS(x)
  93. #endif
  94. #if LINKALL
  95. #define FLAC(h, fn, ...) (FLAC__ ## fn)(__VA_ARGS__)
  96. #define FLAC_A(h, a) (FLAC__ ## a)
  97. #else
  98. #define FLAC(h, fn, ...) (h)->FLAC__##fn(__VA_ARGS__)
  99. #define FLAC_A(h, a) (h)->FLAC__ ## a
  100. #endif
  101. static FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *want, void *client_data) {
  102. size_t bytes;
  103. bool end;
  104. LOCK_S;
  105. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  106. bytes = min(bytes, *want);
  107. end = (stream.state <= DISCONNECT && bytes == 0);
  108. memcpy(buffer, streambuf->readp, bytes);
  109. _buf_inc_readp(streambuf, bytes);
  110. UNLOCK_S;
  111. *want = bytes;
  112. return end ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  113. }
  114. static FLAC__StreamDecoderWriteStatus write_cb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
  115. const FLAC__int32 *const buffer[], void *client_data) {
  116. size_t frames = frame->header.blocksize;
  117. unsigned bits_per_sample = frame->header.bits_per_sample;
  118. unsigned channels = frame->header.channels;
  119. FLAC__int32 *lptr = (FLAC__int32 *)buffer[0];
  120. FLAC__int32 *rptr = (FLAC__int32 *)buffer[channels > 1 ? 1 : 0];
  121. if (decode.new_stream) {
  122. LOCK_O;
  123. LOG_INFO("setting track_start");
  124. output.track_start = outputbuf->writep;
  125. decode.new_stream = false;
  126. #if DSD
  127. #if SL_LITTLE_ENDIAN
  128. #define MARKER_OFFSET 2
  129. #else
  130. #define MARKER_OFFSET 1
  131. #endif
  132. if (bits_per_sample == 24 && is_stream_dop(((u8_t *)lptr) + MARKER_OFFSET, ((u8_t *)rptr) + MARKER_OFFSET, 4, frames)) {
  133. LOG_INFO("file contains DOP");
  134. if (output.dsdfmt == DOP_S24_LE || output.dsdfmt == DOP_S24_3LE)
  135. output.next_fmt = output.dsdfmt;
  136. else
  137. output.next_fmt = DOP;
  138. output.next_sample_rate = frame->header.sample_rate;
  139. output.fade = FADE_INACTIVE;
  140. } else {
  141. output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
  142. output.next_fmt = PCM;
  143. if (output.fade_mode) _checkfade(true);
  144. }
  145. #else
  146. output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
  147. if (output.fade_mode) _checkfade(true);
  148. #endif
  149. UNLOCK_O;
  150. }
  151. LOCK_O_direct;
  152. while (frames > 0) {
  153. frames_t f;
  154. frames_t count;
  155. ISAMPLE_T *optr;
  156. IF_DIRECT(
  157. optr = (ISAMPLE_T *)outputbuf->writep;
  158. f = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  159. );
  160. IF_PROCESS(
  161. optr = (ISAMPLE_T *)process.inbuf;
  162. f = process.max_in_frames;
  163. );
  164. f = min(f, frames);
  165. count = f;
  166. if (bits_per_sample == 8) {
  167. while (count--) {
  168. *optr++ = ALIGN8(*lptr++);
  169. *optr++ = ALIGN8(*rptr++);
  170. }
  171. } else if (bits_per_sample == 16) {
  172. while (count--) {
  173. *optr++ = ALIGN16(*lptr++);
  174. *optr++ = ALIGN16(*rptr++);
  175. }
  176. } else if (bits_per_sample == 24) {
  177. while (count--) {
  178. *optr++ = ALIGN24(*lptr++);
  179. *optr++ = ALIGN24(*rptr++);
  180. }
  181. } else if (bits_per_sample == 32) {
  182. while (count--) {
  183. *optr++ = ALIGN32(*lptr++);
  184. *optr++ = ALIGN32(*rptr++);
  185. }
  186. } else {
  187. LOG_ERROR("unsupported bits per sample: %u", bits_per_sample);
  188. }
  189. frames -= f;
  190. IF_DIRECT(
  191. _buf_inc_writep(outputbuf, f * BYTES_PER_FRAME);
  192. );
  193. IF_PROCESS(
  194. process.in_frames = f;
  195. if (frames) LOG_ERROR("unhandled case");
  196. );
  197. }
  198. UNLOCK_O_direct;
  199. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  200. }
  201. static void error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
  202. LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderErrorStatusString)[status]);
  203. }
  204. static void flac_close(void) {
  205. FLAC(f, stream_decoder_delete, f->decoder);
  206. f->decoder = NULL;
  207. }
  208. static void flac_open(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness) {
  209. if ( f->decoder && f->container != sample_size ) {
  210. flac_close();
  211. }
  212. f->container = sample_size;
  213. if (f->decoder) {
  214. FLAC(f, stream_decoder_reset, f->decoder);
  215. } else {
  216. f->decoder = FLAC(f, stream_decoder_new);
  217. }
  218. if ( f->container == 'o' ) {
  219. LOG_INFO("ogg/flac container - using init_ogg_stream");
  220. FLAC(f, stream_decoder_init_ogg_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
  221. } else {
  222. FLAC(f, stream_decoder_init_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
  223. }
  224. }
  225. static decode_state flac_decode(void) {
  226. bool ok = FLAC(f, stream_decoder_process_single, f->decoder);
  227. FLAC__StreamDecoderState state = FLAC(f, stream_decoder_get_state, f->decoder);
  228. if (!ok && state != FLAC__STREAM_DECODER_END_OF_STREAM) {
  229. LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderStateString)[state]);
  230. };
  231. if (state == FLAC__STREAM_DECODER_END_OF_STREAM) {
  232. return DECODE_COMPLETE;
  233. } else if (state > FLAC__STREAM_DECODER_END_OF_STREAM) {
  234. return DECODE_ERROR;
  235. } else {
  236. return DECODE_RUNNING;
  237. }
  238. }
  239. static bool load_flac() {
  240. #if !LINKALL
  241. void *handle = dlopen(LIBFLAC, RTLD_NOW);
  242. char *err;
  243. if (!handle) {
  244. LOG_INFO("dlerror: %s", dlerror());
  245. return false;
  246. }
  247. f->FLAC__StreamDecoderErrorStatusString = dlsym(handle, "FLAC__StreamDecoderErrorStatusString");
  248. f->FLAC__StreamDecoderStateString = dlsym(handle, "FLAC__StreamDecoderStateString");
  249. f->FLAC__stream_decoder_new = dlsym(handle, "FLAC__stream_decoder_new");
  250. f->FLAC__stream_decoder_reset = dlsym(handle, "FLAC__stream_decoder_reset");
  251. f->FLAC__stream_decoder_delete = dlsym(handle, "FLAC__stream_decoder_delete");
  252. f->FLAC__stream_decoder_init_stream = dlsym(handle, "FLAC__stream_decoder_init_stream");
  253. f->FLAC__stream_decoder_init_ogg_stream = dlsym(handle, "FLAC__stream_decoder_init_ogg_stream");
  254. f->FLAC__stream_decoder_process_single = dlsym(handle, "FLAC__stream_decoder_process_single");
  255. f->FLAC__stream_decoder_get_state = dlsym(handle, "FLAC__stream_decoder_get_state");
  256. if ((err = dlerror()) != NULL) {
  257. LOG_INFO("dlerror: %s", err);
  258. return false;
  259. }
  260. LOG_INFO("loaded "LIBFLAC);
  261. #endif
  262. return true;
  263. }
  264. struct codec *register_flac(void) {
  265. static struct codec ret = {
  266. 'f', // id
  267. "ogf,flc", // types
  268. 16384, // min read
  269. 204800, // min space
  270. flac_open, // open
  271. flac_close, // close
  272. flac_decode, // decode
  273. };
  274. f = malloc(sizeof(struct flac));
  275. if (!f) {
  276. return NULL;
  277. }
  278. f->decoder = NULL;
  279. if (!load_flac()) {
  280. return NULL;
  281. }
  282. LOG_INFO("using flac to decode ogf,flc");
  283. return &ret;
  284. }