opus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 BYTES_PER_FRAME == 4
  32. #define ALIGN(n) (n)
  33. #else
  34. #define ALIGN(n) (n << 16)
  35. #endif
  36. #include <ogg/ogg.h>
  37. #include <opus.h>
  38. // opus maximum output frames is 120ms @ 48kHz
  39. #define MAX_OPUS_FRAMES 5760
  40. struct opus {
  41. enum {OGG_SYNC, OGG_HEADER, OGG_PCM, OGG_DECODE} status;
  42. ogg_stream_state state;
  43. ogg_packet packet;
  44. ogg_sync_state sync;
  45. ogg_page page;
  46. OpusDecoder* decoder;
  47. int rate, gain, pre_skip;
  48. bool fetch;
  49. size_t overframes;
  50. u8_t *overbuf;
  51. int channels;
  52. };
  53. #if !LINKALL
  54. static struct {
  55. void *handle;
  56. int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
  57. int (*ogg_stream_clear)(ogg_stream_state* os);
  58. int (*ogg_stream_reset)(ogg_stream_state* os);
  59. int (*ogg_stream_eos)(ogg_stream_state* os);
  60. int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
  61. int (*ogg_sync_clear)(ogg_sync_state* oy);
  62. void (*ogg_packet_clear)(ogg_packet* op);
  63. char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
  64. int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
  65. long (*ogg_sync_pageseek)(ogg_sync_state* oy, ogg_page* og);
  66. int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
  67. int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
  68. int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
  69. int (*ogg_page_packets)(const ogg_page* og);
  70. } go;
  71. static struct {
  72. void* handle;
  73. OpusDecoder* (*opus_decoder_create)(opus_int32 Fs, int channels, int* error);
  74. int (*opus_decode)(OpusDecoder* st, const unsigned char* data, opus_int32 len, opus_int16* pcm, int frame_size, int decode_fec);
  75. void (*opus_decoder_destroy)(OpusDecoder* st);
  76. } gu;
  77. #endif
  78. static struct opus *u;
  79. extern log_level loglevel;
  80. extern struct buffer *streambuf;
  81. extern struct buffer *outputbuf;
  82. extern struct streamstate stream;
  83. extern struct outputstate output;
  84. extern struct decodestate decode;
  85. extern struct processstate process;
  86. #define LOCK_S mutex_lock(streambuf->mutex)
  87. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  88. #define LOCK_O mutex_lock(outputbuf->mutex)
  89. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  90. #if PROCESS
  91. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  92. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  93. #define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
  94. #define UNLOCK_O_not_direct if (!decode.direct) mutex_unlock(outputbuf->mutex)
  95. #define IF_DIRECT(x) if (decode.direct) { x }
  96. #define IF_PROCESS(x) if (!decode.direct) { x }
  97. #else
  98. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  99. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  100. #define LOCK_O_not_direct
  101. #define UNLOCK_O_not_direct
  102. #define IF_DIRECT(x) { x }
  103. #define IF_PROCESS(x)
  104. #endif
  105. #if LINKALL
  106. #define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
  107. #define OP(h, fn, ...) (opus_ ## fn)(__VA_ARGS__)
  108. #else
  109. #define OG(h, fn, ...) (h)->ogg_ ## fn(__VA_ARGS__)
  110. #define OP(h, fn, ...) (h)->opus_ ## fn(__VA_ARGS__)
  111. #endif
  112. static unsigned parse_uint16(const unsigned char* _data) {
  113. return _data[0] | _data[1] << 8;
  114. }
  115. static int parse_int16(const unsigned char* _data) {
  116. return ((_data[0] | _data[1] << 8) ^ 0x8000) - 0x8000;
  117. }
  118. static opus_uint32 parse_uint32(const unsigned char* _data) {
  119. return _data[0] | (opus_uint32)_data[1] << 8 |
  120. (opus_uint32)_data[2] << 16 | (opus_uint32)_data[3] << 24;
  121. }
  122. static int get_opus_packet(void) {
  123. int status = 0;
  124. LOCK_S;
  125. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  126. while (!(status = OG(&go, stream_packetout, &u->state, &u->packet)) && bytes) {
  127. do {
  128. size_t consumed = min(bytes, 4096);
  129. char* buffer = OG(&gu, sync_buffer, &u->sync, consumed);
  130. memcpy(buffer, streambuf->readp, consumed);
  131. OG(&gu, sync_wrote, &u->sync, consumed);
  132. _buf_inc_readp(streambuf, consumed);
  133. bytes -= consumed;
  134. } while (!(status = OG(&gu, sync_pageseek, &u->sync, &u->page)) && bytes);
  135. // if we have a new page, put it in
  136. if (status) OG(&go, stream_pagein, &u->state, &u->page);
  137. }
  138. UNLOCK_S;
  139. return status;
  140. }
  141. static int read_opus_header(void) {
  142. int status = 0;
  143. LOCK_S;
  144. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  145. while (bytes && !status) {
  146. // first fetch a page if we need one
  147. if (u->fetch) {
  148. size_t consumed = min(bytes, 4096);
  149. char* buffer = OG(&gu, sync_buffer, &u->sync, consumed);
  150. memcpy(buffer, streambuf->readp, consumed);
  151. OG(&gu, sync_wrote, &u->sync, consumed);
  152. _buf_inc_readp(streambuf, consumed);
  153. bytes -= consumed;
  154. if (!OG(&gu, sync_pageseek, &u->sync, &u->page)) continue;
  155. u->fetch = false;
  156. }
  157. //bytes = min(bytes, size);
  158. switch (u->status) {
  159. case OGG_SYNC:
  160. u->status = OGG_HEADER;
  161. //OG(&gu, sync_pageout, &u->sync, &u->page);
  162. OG(&gu, stream_reset_serialno, &u->state, OG(&gu, page_serialno, &u->page));
  163. break;
  164. case OGG_HEADER:
  165. status = OG(&gu, stream_pagein, &u->state, &u->page);
  166. if (OG(&gu, stream_packetout, &u->state, &u->packet)) {
  167. u->status = OGG_PCM;
  168. if (u->packet.bytes < 19 || memcmp(u->packet.packet, "OpusHead", 8)) {
  169. LOG_ERROR("wrong opus header packet (size:%u)", u->packet.bytes);
  170. status = -100;
  171. break;
  172. }
  173. u->channels = u->packet.packet[9];
  174. u->pre_skip = parse_uint16(u->packet.packet + 10);
  175. u->rate = parse_uint32(u->packet.packet + 12);
  176. u->gain = parse_int16(u->packet.packet + 16);
  177. u->decoder = OP(&gu, decoder_create, 48000, u->channels, &status);
  178. if (!u->decoder || status != OPUS_OK) {
  179. LOG_ERROR("can't create decoder %d (channels:%u)", status, u->channels);
  180. }
  181. }
  182. u->fetch = true;
  183. break;
  184. case OGG_PCM:
  185. // loop until we have consumed VorbisComment and get ready for a new packet
  186. u->fetch = true;
  187. status = OG(&gu, page_packets, &u->page);
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. UNLOCK_S;
  194. return status;
  195. }
  196. static decode_state opus_decompress(void) {
  197. frames_t frames;
  198. int n;
  199. static int channels;
  200. u8_t *write_buf;
  201. if (decode.new_stream) {
  202. int status = read_opus_header();
  203. if (status == 0) {
  204. return DECODE_RUNNING;
  205. } else if (status < 0) {
  206. LOG_WARN("can't create codec");
  207. return DECODE_ERROR;
  208. }
  209. LOCK_O;
  210. output.next_sample_rate = decode_newstream(48000, output.supported_rates);
  211. IF_DSD( output.next_fmt = PCM; )
  212. output.track_start = outputbuf->writep;
  213. if (output.fade_mode) _checkfade(true);
  214. decode.new_stream = false;
  215. UNLOCK_O;
  216. channels = u->channels;
  217. LOG_INFO("setting track_start");
  218. }
  219. LOCK_O_direct;
  220. IF_DIRECT(
  221. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  222. write_buf = outputbuf->writep;
  223. );
  224. IF_PROCESS(
  225. frames = process.max_in_frames;
  226. write_buf = process.inbuf;
  227. );
  228. // get some packets and decode them, or use the leftover from previous pass
  229. if (u->overframes) {
  230. /* use potential leftover from previous encoding. We know that it will fit this time
  231. * as min_space is >=MAX_OPUS_FRAMES and we start from the beginning of the buffer */
  232. memcpy(write_buf, u->overbuf, u->overframes * BYTES_PER_FRAME);
  233. n = u->overframes;
  234. u->overframes = 0;
  235. } else if (get_opus_packet() > 0) {
  236. if (frames < MAX_OPUS_FRAMES) {
  237. // don't have enough contiguous space, use the overflow buffer (still works if n < 0)
  238. n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) u->overbuf, MAX_OPUS_FRAMES, 0);
  239. if (n > 0) {
  240. u->overframes = n - min(n, frames);
  241. n = min(n, frames);
  242. memcpy(write_buf, u->overbuf, n * BYTES_PER_FRAME);
  243. memmove(u->overbuf, u->overbuf + n, u->overframes);
  244. }
  245. } else {
  246. /* we just do one packet at a time, although we could loop on packets but that means locking the
  247. * outputbuf and streambuf for maybe a long time while we process it all, so don't do that */
  248. n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) write_buf, frames, 0);
  249. }
  250. } else if (!OG(&go, page_eos, &u->page)) {
  251. UNLOCK_O_direct;
  252. return DECODE_RUNNING;
  253. }
  254. if (n > 0) {
  255. frames_t count;
  256. s16_t *iptr;
  257. ISAMPLE_T *optr;
  258. frames = n;
  259. count = frames * channels;
  260. // work backward to unpack samples (if needed)
  261. iptr = (s16_t *) write_buf + count;
  262. IF_DIRECT(
  263. optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
  264. )
  265. IF_PROCESS(
  266. optr = (ISAMPLE_T *) write_buf + frames * 2;
  267. )
  268. if (channels == 2) {
  269. #if BYTES_PER_FRAME == 8
  270. while (count--) {
  271. *--optr = ALIGN(*--iptr);
  272. }
  273. #endif
  274. } else if (channels == 1) {
  275. while (count--) {
  276. *--optr = ALIGN(*--iptr);
  277. *--optr = ALIGN(*iptr);
  278. }
  279. }
  280. IF_DIRECT(
  281. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  282. );
  283. IF_PROCESS(
  284. process.in_frames = frames;
  285. );
  286. LOG_SDEBUG("wrote %u frames", frames);
  287. } else if (n == 0) {
  288. if (stream.state <= DISCONNECT) {
  289. LOG_INFO("end of decode");
  290. UNLOCK_O_direct;
  291. return DECODE_COMPLETE;
  292. } else {
  293. LOG_INFO("no frame decoded");
  294. }
  295. } else {
  296. LOG_INFO("opus decode error: %d", n);
  297. UNLOCK_O_direct;
  298. return DECODE_COMPLETE;
  299. }
  300. UNLOCK_O_direct;
  301. return DECODE_RUNNING;
  302. }
  303. static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  304. if (u->decoder) OP(&gu, decoder_destroy, u->decoder);
  305. if (!u->overbuf) u->overbuf = malloc(MAX_OPUS_FRAMES * BYTES_PER_FRAME);
  306. u->status = OGG_SYNC;
  307. u->fetch = true;
  308. u->overframes = 0;
  309. OG(&gu, sync_init, &u->sync);
  310. OG(&gu, stream_init, &u->state, -1);
  311. }
  312. static void opus_close(void) {
  313. if (u->decoder) OP(&gu, decoder_destroy, u->decoder);
  314. free(u->overbuf);
  315. OG(&gu, stream_clear, &u->state);
  316. OG(&gu, sync_clear, &u->sync);
  317. }
  318. static bool load_opus(void) {
  319. #if !LINKALL
  320. char *err;
  321. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  322. void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
  323. if (!g_handle || !u_handle) {
  324. LOG_INFO("dlerror: %s", dlerror());
  325. return false;
  326. }
  327. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  328. g_handle->.ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  329. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  330. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  331. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  332. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  333. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  334. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  335. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  336. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  337. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  338. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  339. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  340. u_handle->opus_decoder_create = dlsym(u_handle->handle, "opus_decoder_create");
  341. u_handle->opus_decoder_destroy = dlsym(u_handle->handle, "opus_decoder_destroy");
  342. u_handle->opus_decode = dlsym(u_handle->handle, "opus_decode");
  343. if ((err = dlerror()) != NULL) {
  344. LOG_INFO("dlerror: %s", err);
  345. return false;
  346. }
  347. LOG_INFO("loaded "LIBOPUS);
  348. #endif
  349. return true;
  350. }
  351. struct codec *register_opus(void) {
  352. static struct codec ret = {
  353. 'u', // id
  354. "ops", // types
  355. 8*1024, // min read
  356. MAX_OPUS_FRAMES*BYTES_PER_FRAME*2, // min space
  357. opus_open, // open
  358. opus_close, // close
  359. opus_decompress, // decode
  360. };
  361. if ((u = calloc(1, sizeof(struct opus))) == NULL) {
  362. return NULL;
  363. }
  364. if (!load_opus()) {
  365. return NULL;
  366. }
  367. LOG_INFO("using opus to decode ops");
  368. return &ret;
  369. }