opus.c 13 KB

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