opus.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 2018-2019, 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. /*
  24. * with some low-end CPU, the decode call takes a fair bit of time and if the outputbuf is locked during that
  25. * period, the output_thread (or equivalent) will be locked although there is plenty of samples available.
  26. * Normally, with PRIO_INHERIT, that thread should increase decoder priority and get the lock quickly but it
  27. * seems that when the streambuf has plenty of data, the decode thread grabs the CPU to much, even it the output
  28. * thread has a higher priority. Using an interim buffer where opus decoder writes the output is not great from
  29. * an efficiency (one extra memory copy) point of view, but it allows the lock to not be kept for too long
  30. */
  31. #define FRAME_BUF 2048
  32. #if BYTES_PER_FRAME == 4
  33. #define ALIGN(n) (n)
  34. #else
  35. #define ALIGN(n) (n << 16)
  36. #endif
  37. #include <opusfile.h>
  38. struct opus {
  39. struct OggOpusFile *of;
  40. bool opened;
  41. #if FRAME_BUF
  42. u8_t *write_buf;
  43. #endif
  44. #if !LINKALL
  45. // opus symbols to be dynamically loaded
  46. void (*op_free)(OggOpusFile *_of);
  47. int (*op_read)(OggOpusFile *_of, opus_int16 *_pcm, int _buf_size, int *_li);
  48. const OpusHead* (*op_head)(OggOpusFile *_of, int _li);
  49. OggOpusFile* (*op_open_callbacks) (void *_source, OpusFileCallbacks *_cb, unsigned char *_initial_data, size_t _initial_bytes, int *_error);
  50. #endif
  51. };
  52. static struct opus *u;
  53. extern log_level loglevel;
  54. extern struct buffer *streambuf;
  55. extern struct buffer *outputbuf;
  56. extern struct streamstate stream;
  57. extern struct outputstate output;
  58. extern struct decodestate decode;
  59. extern struct processstate process;
  60. #define LOCK_S mutex_lock(streambuf->mutex)
  61. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  62. #define LOCK_O mutex_lock(outputbuf->mutex)
  63. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  64. #if PROCESS
  65. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  66. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  67. #define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
  68. #define UNLOCK_O_not_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 LOCK_O_not_direct
  75. #define UNLOCK_O_not_direct
  76. #define IF_DIRECT(x) { x }
  77. #define IF_PROCESS(x)
  78. #endif
  79. #if LINKALL
  80. #define OP(h, fn, ...) (op_ ## fn)(__VA_ARGS__)
  81. #else
  82. #define OP(h, fn, ...) (h)->op_ ## fn(__VA_ARGS__)
  83. #endif
  84. // called with mutex locked within vorbis_decode to avoid locking O before S
  85. static int _read_cb(void *datasource, char *ptr, int size) {
  86. size_t bytes;
  87. LOCK_S;
  88. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  89. bytes = min(bytes, size);
  90. memcpy(ptr, streambuf->readp, bytes);
  91. _buf_inc_readp(streambuf, bytes);
  92. UNLOCK_S;
  93. return bytes;
  94. }
  95. static decode_state opus_decompress(void) {
  96. frames_t frames;
  97. int n;
  98. static int channels;
  99. u8_t *write_buf;
  100. LOCK_S;
  101. if (stream.state <= DISCONNECT && !_buf_used(streambuf)) {
  102. UNLOCK_S;
  103. return DECODE_COMPLETE;
  104. }
  105. UNLOCK_S;
  106. if (decode.new_stream) {
  107. struct OpusFileCallbacks cbs;
  108. const struct OpusHead *info;
  109. int err;
  110. cbs.read = (op_read_func) _read_cb;
  111. cbs.seek = NULL; cbs.tell = NULL; cbs.close = NULL;
  112. if ((u->of = OP(u, open_callbacks, streambuf, &cbs, NULL, 0, &err)) == NULL) {
  113. LOG_WARN("open_callbacks error: %d", err);
  114. return DECODE_COMPLETE;
  115. }
  116. u->opened = true;
  117. info = OP(u, head, u->of, -1);
  118. LOCK_O;
  119. output.next_sample_rate = 48000;
  120. IF_DSD( output.next_fmt = PCM; )
  121. output.track_start = outputbuf->writep;
  122. if (output.fade_mode) _checkfade(true);
  123. decode.new_stream = false;
  124. UNLOCK_O;
  125. channels = info->channel_count;
  126. LOG_INFO("setting track_start");
  127. }
  128. #if !FRAME_BUF
  129. LOCK_O_direct;
  130. #endif
  131. #if FRAME_BUF
  132. IF_DIRECT(
  133. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  134. write_buf = u->write_buf;
  135. );
  136. #else
  137. IF_DIRECT(
  138. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  139. write_buf = outputbuf->writep;
  140. );
  141. #endif
  142. IF_PROCESS(
  143. frames = process.max_in_frames;
  144. write_buf = process.inbuf;
  145. );
  146. #if FRAME_BUF
  147. frames = min(frames, FRAME_BUF);
  148. #endif
  149. // write the decoded frames into outputbuf then unpack them (they are 16 bits)
  150. n = OP(u, read, u->of, (opus_int16*) write_buf, frames * channels, NULL);
  151. #if FRAME_BUF
  152. LOCK_O_direct;
  153. #endif
  154. if (n > 0) {
  155. frames_t count;
  156. s16_t *iptr;
  157. ISAMPLE_T *optr;
  158. frames = n;
  159. count = frames * channels;
  160. iptr = (s16_t *)write_buf + count;
  161. optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
  162. if (channels == 2) {
  163. #if BYTES_PER_FRAME == 4
  164. memcpy(outputbuf->writep, write_buf, frames * BYTES_PER_FRAME);
  165. #else
  166. while (count--) {
  167. *--optr = *--iptr << 16;
  168. }
  169. #endif
  170. } else if (channels == 1) {
  171. while (count--) {
  172. *--optr = ALIGN(*--iptr);
  173. *--optr = ALIGN(*iptr);
  174. }
  175. }
  176. IF_DIRECT(
  177. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  178. );
  179. IF_PROCESS(
  180. process.in_frames = frames;
  181. );
  182. LOG_SDEBUG("wrote %u frames", frames);
  183. } else if (n == 0) {
  184. LOG_INFO("end of stream");
  185. UNLOCK_O_direct;
  186. return DECODE_COMPLETE;
  187. } else if (n == OP_HOLE) {
  188. // recoverable hole in stream, seen when skipping
  189. LOG_DEBUG("hole in stream");
  190. } else {
  191. LOG_INFO("op_read error: %d", n);
  192. UNLOCK_O_direct;
  193. return DECODE_COMPLETE;
  194. }
  195. UNLOCK_O_direct;
  196. return DECODE_RUNNING;
  197. }
  198. static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  199. if (!u->of) {
  200. #if FRAME_BUF
  201. if (!u->write_buf) u->write_buf = malloc(FRAME_BUF * BYTES_PER_FRAME);
  202. #endif
  203. } else {
  204. OP(u, free, u->of);
  205. u->of = NULL;
  206. }
  207. u->opened = false;
  208. }
  209. static void opus_close(void) {
  210. u->opened = false;
  211. OP(u, free, u->of);
  212. #if FRAME_BUF
  213. free(u->write_buf);
  214. u->write_buf = NULL;
  215. #endif
  216. u->of = NULL;
  217. }
  218. static bool load_opus(void) {
  219. #if !LINKALL
  220. void *handle = dlopen(LIBOPUS, RTLD_NOW);
  221. char *err;
  222. if (!handle) {
  223. LOG_INFO("dlerror: %s", dlerror());
  224. return false;
  225. }
  226. u->op_free = dlsym(handle, "op_free");
  227. u->op_read = dlsym(handle, "op_read");
  228. u->op_head = dlsym(handle, "op_head");
  229. u->op_open_callbacks = dlsym(handle, "op_open_callbacks");
  230. if ((err = dlerror()) != NULL) {
  231. LOG_INFO("dlerror: %s", err);
  232. return false;
  233. }
  234. LOG_INFO("loaded "LIBOPUS);
  235. #endif
  236. return true;
  237. }
  238. struct codec *register_opus(void) {
  239. static struct codec ret = {
  240. 'u', // id
  241. "ops", // types
  242. 4096, // min read
  243. 20480, // min space
  244. opus_open, // open
  245. opus_close, // close
  246. opus_decompress, // decode
  247. };
  248. u = malloc(sizeof(struct opus));
  249. if (!u) {
  250. return NULL;
  251. }
  252. u->of = NULL;
  253. u->write_buf = NULL;
  254. if (!load_opus()) {
  255. return NULL;
  256. }
  257. LOG_INFO("using opus to decode ops");
  258. return &ret;
  259. }