opus.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. IF_DIRECT(
  159. optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
  160. )
  161. IF_PROCESS(
  162. optr = (ISAMPLE_T *) write_buf + frames * 2;
  163. )
  164. if (channels == 2) {
  165. #if BYTES_PER_FRAME == 4
  166. #if FRAME_BUF
  167. // copy needed only when DIRECT and FRAME_BUF
  168. IF_DIRECT(
  169. memcpy(outputbuf->writep, write_buf, frames * BYTES_PER_FRAME);
  170. )
  171. #endif
  172. #else
  173. while (count--) {
  174. *--optr = ALIGN(*--iptr);
  175. }
  176. #endif
  177. } else if (channels == 1) {
  178. while (count--) {
  179. *--optr = ALIGN(*--iptr);
  180. *--optr = ALIGN(*iptr);
  181. }
  182. }
  183. IF_DIRECT(
  184. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  185. );
  186. IF_PROCESS(
  187. process.in_frames = frames;
  188. );
  189. LOG_SDEBUG("wrote %u frames", frames);
  190. } else if (n == 0) {
  191. if (stream.state <= DISCONNECT) {
  192. LOG_INFO("partial decode");
  193. UNLOCK_O_direct;
  194. return DECODE_COMPLETE;
  195. } else {
  196. LOG_INFO("no frame decoded");
  197. }
  198. } else if (n == OP_HOLE) {
  199. // recoverable hole in stream, seen when skipping
  200. LOG_DEBUG("hole in stream");
  201. } else {
  202. LOG_INFO("op_read error: %d", n);
  203. UNLOCK_O_direct;
  204. return DECODE_COMPLETE;
  205. }
  206. UNLOCK_O_direct;
  207. return DECODE_RUNNING;
  208. }
  209. static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  210. if (!u->of) {
  211. #if FRAME_BUF
  212. if (!u->write_buf) u->write_buf = malloc(FRAME_BUF * BYTES_PER_FRAME);
  213. #endif
  214. } else {
  215. OP(u, free, u->of);
  216. u->of = NULL;
  217. }
  218. }
  219. static void opus_close(void) {
  220. if (u->of) {
  221. OP(u, free, u->of);
  222. u->of = NULL;
  223. }
  224. #if FRAME_BUF
  225. free(u->write_buf);
  226. u->write_buf = NULL;
  227. #endif
  228. }
  229. static bool load_opus(void) {
  230. #if !LINKALL
  231. void *handle = dlopen(LIBOPUS, RTLD_NOW);
  232. char *err;
  233. if (!handle) {
  234. LOG_INFO("dlerror: %s", dlerror());
  235. return false;
  236. }
  237. u->op_free = dlsym(handle, "op_free");
  238. u->op_read = dlsym(handle, "op_read");
  239. u->op_head = dlsym(handle, "op_head");
  240. u->op_open_callbacks = dlsym(handle, "op_open_callbacks");
  241. if ((err = dlerror()) != NULL) {
  242. LOG_INFO("dlerror: %s", err);
  243. return false;
  244. }
  245. LOG_INFO("loaded "LIBOPUS);
  246. #endif
  247. return true;
  248. }
  249. struct codec *register_opus(void) {
  250. static struct codec ret = {
  251. 'u', // id
  252. "ops", // types
  253. 4*1024, // min read
  254. 32*1024, // min space
  255. opus_open, // open
  256. opus_close, // close
  257. opus_decompress, // decode
  258. };
  259. u = malloc(sizeof(struct opus));
  260. if (!u) {
  261. return NULL;
  262. }
  263. u->of = NULL;
  264. #if FRAME_BUF
  265. u->write_buf = NULL;
  266. #endif
  267. if (!load_opus()) {
  268. return NULL;
  269. }
  270. LOG_INFO("using opus to decode ops");
  271. return &ret;
  272. }