opus.c 7.9 KB

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