opus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. 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_ID_HEADER;
  161. OG(&gu, stream_reset_serialno, &u->state, OG(&gu, page_serialno, &u->page));
  162. break;
  163. case OGG_ID_HEADER:
  164. status = OG(&gu, stream_pagein, &u->state, &u->page);
  165. if (OG(&gu, stream_packetout, &u->state, &u->packet)) {
  166. if (u->packet.bytes < 19 || memcmp(u->packet.packet, "OpusHead", 8)) {
  167. LOG_ERROR("wrong opus header packet (size:%u)", u->packet.bytes);
  168. status = -100;
  169. break;
  170. }
  171. u->status = OGG_COMMENT_HEADER;
  172. u->channels = u->packet.packet[9];
  173. u->pre_skip = parse_uint16(u->packet.packet + 10);
  174. u->rate = parse_uint32(u->packet.packet + 12);
  175. u->gain = parse_int16(u->packet.packet + 16);
  176. u->decoder = OP(&gu, decoder_create, 48000, u->channels, &status);
  177. if (!u->decoder || status != OPUS_OK) {
  178. LOG_ERROR("can't create decoder %d (channels:%u)", status, u->channels);
  179. }
  180. }
  181. u->fetch = true;
  182. break;
  183. case OGG_COMMENT_HEADER:
  184. // loop until we have consumed VorbisComment and get ready for a new packet
  185. u->fetch = true;
  186. status = OG(&gu, page_packets, &u->page);
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. UNLOCK_S;
  193. return status;
  194. }
  195. static decode_state opus_decompress(void) {
  196. frames_t frames;
  197. int n;
  198. static int channels;
  199. u8_t *write_buf;
  200. if (decode.new_stream) {
  201. int status = read_opus_header();
  202. if (status == 0) {
  203. return DECODE_RUNNING;
  204. } else if (status < 0) {
  205. LOG_WARN("can't create codec");
  206. return DECODE_ERROR;
  207. }
  208. LOCK_O;
  209. output.next_sample_rate = decode_newstream(48000, output.supported_rates);
  210. IF_DSD( output.next_fmt = PCM; )
  211. output.track_start = outputbuf->writep;
  212. if (output.fade_mode) _checkfade(true);
  213. decode.new_stream = false;
  214. UNLOCK_O;
  215. channels = u->channels;
  216. LOG_INFO("setting track_start");
  217. }
  218. LOCK_O_direct;
  219. IF_DIRECT(
  220. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  221. write_buf = outputbuf->writep;
  222. );
  223. IF_PROCESS(
  224. frames = process.max_in_frames;
  225. write_buf = process.inbuf;
  226. );
  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 (get_opus_packet() > 0) {
  235. if (frames < MAX_OPUS_FRAMES) {
  236. // don't have enough contiguous space, use the overflow buffer (still works if n < 0)
  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 (!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 * 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 (channels == 2) {
  268. #if BYTES_PER_FRAME == 8
  269. while (count--) {
  270. *--optr = ALIGN(*--iptr);
  271. }
  272. #endif
  273. } else if (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 (stream.state <= DISCONNECT) {
  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->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. 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 *g_handle = dlopen(LIBOGG, RTLD_NOW);
  324. void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
  325. if (!g_handle || !u_handle) {
  326. LOG_INFO("dlerror: %s", dlerror());
  327. return false;
  328. }
  329. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  330. g_handle->.ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  331. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  332. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  333. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  334. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  335. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  336. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  337. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  338. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  339. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  340. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  341. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  342. u_handle->opus_decoder_create = dlsym(u_handle->handle, "opus_decoder_create");
  343. u_handle->opus_decoder_destroy = dlsym(u_handle->handle, "opus_decoder_destroy");
  344. u_handle->opus_decode = dlsym(u_handle->handle, "opus_decode");
  345. if ((err = dlerror()) != NULL) {
  346. LOG_INFO("dlerror: %s", err);
  347. return false;
  348. }
  349. LOG_INFO("loaded "LIBOPUS);
  350. #endif
  351. return true;
  352. }
  353. struct codec *register_opus(void) {
  354. static struct codec ret = {
  355. 'u', // id
  356. "ops", // types
  357. 8*1024, // min read
  358. MAX_OPUS_FRAMES*BYTES_PER_FRAME*2, // min space
  359. opus_open, // open
  360. opus_close, // close
  361. opus_decompress, // decode
  362. };
  363. if ((u = calloc(1, sizeof(struct opus))) == NULL) {
  364. return NULL;
  365. }
  366. if (!load_opus()) {
  367. return NULL;
  368. }
  369. LOG_INFO("using opus to decode ops");
  370. return &ret;
  371. }