opus.c 7.8 KB

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