flac.c 10 KB

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