opus.c 7.6 KB

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