opus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. do {
  123. size_t consumed = min(bytes, 4096);
  124. char* buffer = OG(&gu, sync_buffer, &u->sync, consumed);
  125. memcpy(buffer, streambuf->readp, consumed);
  126. OG(&gu, sync_wrote, &u->sync, consumed);
  127. _buf_inc_readp(streambuf, consumed);
  128. bytes -= consumed;
  129. } while (!(status = OG(&gu, sync_pageseek, &u->sync, &u->page)) && bytes);
  130. // if we have a new page, put it in
  131. if (status) OG(&go, stream_pagein, &u->state, &u->page);
  132. }
  133. // only return a negative value when end of streaming is reached
  134. if (status > 0) packet = status;
  135. else if (stream.state > DISCONNECT) packet = 0;
  136. UNLOCK_S;
  137. return packet;
  138. }
  139. static int read_opus_header(void) {
  140. int status = 0;
  141. bool fetch = true;
  142. LOCK_S;
  143. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  144. while (bytes && !status) {
  145. // first fetch a page if we need one
  146. if (fetch) {
  147. size_t consumed = min(bytes, 4096);
  148. char* buffer = OG(&gu, sync_buffer, &u->sync, consumed);
  149. memcpy(buffer, streambuf->readp, consumed);
  150. OG(&gu, sync_wrote, &u->sync, consumed);
  151. _buf_inc_readp(streambuf, consumed);
  152. bytes -= consumed;
  153. if (!OG(&gu, sync_pageseek, &u->sync, &u->page)) continue;
  154. }
  155. switch (u->status) {
  156. case OGG_SYNC:
  157. u->status = OGG_ID_HEADER;
  158. OG(&gu, stream_reset_serialno, &u->state, OG(&gu, page_serialno, &u->page));
  159. fetch = false;
  160. break;
  161. case OGG_ID_HEADER:
  162. status = OG(&gu, stream_pagein, &u->state, &u->page);
  163. if (OG(&gu, stream_packetout, &u->state, &u->packet)) {
  164. if (u->packet.bytes < 19 || memcmp(u->packet.packet, "OpusHead", 8)) {
  165. LOG_ERROR("wrong opus header packet (size:%u)", u->packet.bytes);
  166. status = -100;
  167. break;
  168. }
  169. u->status = OGG_COMMENT_HEADER;
  170. u->channels = u->packet.packet[9];
  171. u->pre_skip = parse_uint16(u->packet.packet + 10);
  172. u->rate = parse_uint32(u->packet.packet + 12);
  173. u->gain = parse_int16(u->packet.packet + 16);
  174. u->decoder = OP(&gu, decoder_create, 48000, u->channels, &status);
  175. if (!u->decoder || status != OPUS_OK) {
  176. LOG_ERROR("can't create decoder %d (channels:%u)", status, u->channels);
  177. }
  178. }
  179. fetch = true;
  180. break;
  181. case OGG_COMMENT_HEADER:
  182. // skip pakets to consume VorbisComment. With opus, header packets align on pages
  183. status = OG(&gu, page_packets, &u->page);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. UNLOCK_S;
  190. return status;
  191. }
  192. static decode_state opus_decompress(void) {
  193. frames_t frames;
  194. u8_t *write_buf;
  195. if (decode.new_stream) {
  196. int status = read_opus_header();
  197. if (status == 0) {
  198. return DECODE_RUNNING;
  199. } else if (status < 0) {
  200. LOG_WARN("can't create codec");
  201. return DECODE_ERROR;
  202. }
  203. LOCK_O;
  204. output.next_sample_rate = decode_newstream(48000, output.supported_rates);
  205. IF_DSD( output.next_fmt = PCM; )
  206. output.track_start = outputbuf->writep;
  207. if (output.fade_mode) _checkfade(true);
  208. decode.new_stream = false;
  209. UNLOCK_O;
  210. if (u->channels > 2) {
  211. LOG_WARN("too many channels: %d", u->channels);
  212. return DECODE_ERROR;
  213. }
  214. LOG_INFO("setting track_start");
  215. }
  216. LOCK_O_direct;
  217. IF_DIRECT(
  218. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  219. write_buf = outputbuf->writep;
  220. );
  221. IF_PROCESS(
  222. frames = process.max_in_frames;
  223. write_buf = process.inbuf;
  224. );
  225. int packet, n = 0;
  226. // get some packets and decode them, or use the leftover from previous pass
  227. if (u->overframes) {
  228. /* use potential leftover from previous encoding. We know that it will fit this time
  229. * as min_space is >=MAX_OPUS_FRAMES and we start from the beginning of the buffer */
  230. memcpy(write_buf, u->overbuf, u->overframes * BYTES_PER_FRAME);
  231. n = u->overframes;
  232. u->overframes = 0;
  233. } else if ((packet = get_opus_packet()) > 0) {
  234. if (frames < MAX_OPUS_FRAMES) {
  235. // don't have enough contiguous space, use the overflow buffer
  236. n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) u->overbuf, MAX_OPUS_FRAMES, 0);
  237. if (n > 0) {
  238. u->overframes = n - min(n, frames);
  239. n = min(n, frames);
  240. memcpy(write_buf, u->overbuf, n * BYTES_PER_FRAME);
  241. memmove(u->overbuf, u->overbuf + n, u->overframes);
  242. }
  243. } else {
  244. /* we just do one packet at a time, although we could loop on packets but that means locking the
  245. * outputbuf and streambuf for maybe a long time while we process it all, so don't do that */
  246. n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) write_buf, frames, 0);
  247. }
  248. } else if (!packet && !OG(&go, page_eos, &u->page)) {
  249. UNLOCK_O_direct;
  250. return DECODE_RUNNING;
  251. }
  252. if (n > 0) {
  253. frames_t count;
  254. s16_t *iptr;
  255. ISAMPLE_T *optr;
  256. frames = n;
  257. count = frames * u->channels;
  258. // work backward to unpack samples (if needed)
  259. iptr = (s16_t *) write_buf + count;
  260. IF_DIRECT(
  261. optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
  262. )
  263. IF_PROCESS(
  264. optr = (ISAMPLE_T *) write_buf + frames * 2;
  265. )
  266. if (u->channels == 2) {
  267. #if BYTES_PER_FRAME == 8
  268. while (count--) {
  269. *--optr = ALIGN(*--iptr);
  270. }
  271. #endif
  272. } else if (u->channels == 1) {
  273. while (count--) {
  274. *--optr = ALIGN(*--iptr);
  275. *--optr = ALIGN(*iptr);
  276. }
  277. }
  278. IF_DIRECT(
  279. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  280. );
  281. IF_PROCESS(
  282. process.in_frames = frames;
  283. );
  284. LOG_SDEBUG("wrote %u frames", frames);
  285. } else if (n == 0) {
  286. if (packet < 0) {
  287. LOG_INFO("end of decode");
  288. UNLOCK_O_direct;
  289. return DECODE_COMPLETE;
  290. } else {
  291. LOG_INFO("no frame decoded");
  292. }
  293. } else {
  294. LOG_INFO("opus decode error: %d", n);
  295. UNLOCK_O_direct;
  296. return DECODE_COMPLETE;
  297. }
  298. UNLOCK_O_direct;
  299. return DECODE_RUNNING;
  300. }
  301. static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  302. if (u->decoder) OP(&gu, decoder_destroy, u->decoder);
  303. u->decoder = NULL;
  304. if (!u->overbuf) u->overbuf = malloc(MAX_OPUS_FRAMES * BYTES_PER_FRAME);
  305. u->status = OGG_SYNC;
  306. u->overframes = 0;
  307. OG(&gu, sync_clear, &u->sync);
  308. OG(&gu, stream_clear, &u->state);
  309. OG(&gu, stream_init, &u->state, -1);
  310. }
  311. static void opus_close(void) {
  312. if (u->decoder) OP(&gu, decoder_destroy, u->decoder);
  313. u->decoder = NULL;
  314. free(u->overbuf);
  315. u->overbuf = NULL;
  316. OG(&gu, stream_clear, &u->state);
  317. OG(&gu, sync_clear, &u->sync);
  318. }
  319. static bool load_opus(void) {
  320. #if !LINKALL
  321. char *err;
  322. void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
  323. if (!u_handle) {
  324. LOG_INFO("opus dlerror: %s", dlerror());
  325. return false;
  326. }
  327. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  328. if (!g_handle) {
  329. dlclose(u_handle);
  330. LOG_INFO("ogg dlerror: %s", dlerror());
  331. return false;
  332. }
  333. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  334. g_handle->.ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  335. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  336. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  337. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  338. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  339. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  340. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  341. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  342. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  343. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  344. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  345. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  346. u_handle->opus_decoder_create = dlsym(u_handle->handle, "opus_decoder_create");
  347. u_handle->opus_decoder_destroy = dlsym(u_handle->handle, "opus_decoder_destroy");
  348. u_handle->opus_decode = dlsym(u_handle->handle, "opus_decode");
  349. if ((err = dlerror()) != NULL) {
  350. LOG_INFO("dlerror: %s", err);
  351. return false;
  352. }
  353. LOG_INFO("loaded "LIBOPUS);
  354. #endif
  355. return true;
  356. }
  357. struct codec *register_opus(void) {
  358. static struct codec ret = {
  359. 'u', // id
  360. "ops", // types
  361. 8*1024, // min read
  362. MAX_OPUS_FRAMES*BYTES_PER_FRAME*2, // min space
  363. opus_open, // open
  364. opus_close, // close
  365. opus_decompress, // decode
  366. };
  367. if ((u = calloc(1, sizeof(struct opus))) == NULL) {
  368. return NULL;
  369. }
  370. if (!load_opus()) {
  371. return NULL;
  372. }
  373. LOG_INFO("using opus to decode ops");
  374. return &ret;
  375. }