vorbis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "squeezelite.h"
  22. // automatically select between floating point (preferred) and fixed point libraries:
  23. // NOTE: works with Tremor version here: http://svn.xiph.org/trunk/Tremor, not vorbisidec.1.0.2 currently in ubuntu
  24. #include <ogg/ogg.h>
  25. #ifdef TREMOR_ONLY
  26. #include <vorbis/ivorbiscodec.h>
  27. #else
  28. #include <vorbis/codec.h>
  29. static bool tremor = false;
  30. #endif
  31. // this is tremor packing, not mine...
  32. static inline int32_t clip15(int32_t x) {
  33. int ret = x;
  34. ret -= ((x<=32767)-1)&(x-32767);
  35. ret -= ((x>=-32768)-1)&(x+32768);
  36. return ret;
  37. }
  38. #if BYTES_PER_FRAME == 4
  39. #define ALIGN(n) clip15((n) >> 9);
  40. #define ALIGN_FLOAT(n) ((n)*32768.0f + 0.5f)
  41. #else
  42. #define ALIGN(n) (clip15((n) >> 9) << 16)
  43. #define ALIGN_FLOAT ((n)*32768.0f*65536.0f + 0.5f)
  44. #endif
  45. struct vorbis {
  46. bool opened;
  47. enum { OGG_SYNC, OGG_ID_HEADER, OGG_COMMENT_HEADER, OGG_SETUP_HEADER } status;
  48. struct {
  49. ogg_stream_state state;
  50. ogg_packet packet;
  51. ogg_sync_state sync;
  52. ogg_page page;
  53. };
  54. struct {
  55. vorbis_dsp_state decoder;
  56. vorbis_info info;
  57. vorbis_comment comment;
  58. vorbis_block block;
  59. };
  60. int rate, channels;
  61. uint32_t overflow;
  62. };
  63. #if !LINKALL
  64. static struct vorbis {
  65. // vorbis symbols to be dynamically loaded - from either vorbisfile or vorbisidec (tremor) version of library
  66. vorbis_info *(* ov_info)(OggVorbis_File *vf, int link);
  67. int (* ov_clear)(OggVorbis_File *vf);
  68. long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
  69. long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
  70. int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
  71. } gv;
  72. static struct {
  73. void *handle;
  74. int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
  75. int (*ogg_stream_clear)(ogg_stream_state* os);
  76. int (*ogg_stream_reset)(ogg_stream_state* os);
  77. int (*ogg_stream_eos)(ogg_stream_state* os);
  78. int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
  79. int (*ogg_sync_clear)(ogg_sync_state* oy);
  80. void (*ogg_packet_clear)(ogg_packet* op);
  81. char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
  82. int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
  83. long (*ogg_sync_pageseek)(ogg_sync_state* oy, ogg_page* og);
  84. int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
  85. int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
  86. int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
  87. int (*ogg_page_packets)(const ogg_page* og);
  88. } go;
  89. #endif
  90. static struct vorbis *v;
  91. extern log_level loglevel;
  92. extern struct buffer *streambuf;
  93. extern struct buffer *outputbuf;
  94. extern struct streamstate stream;
  95. extern struct outputstate output;
  96. extern struct decodestate decode;
  97. extern struct processstate process;
  98. #define LOCK_S mutex_lock(streambuf->mutex)
  99. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  100. #define LOCK_O mutex_lock(outputbuf->mutex)
  101. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  102. #if PROCESS
  103. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  104. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  105. #define IF_DIRECT(x) if (decode.direct) { x }
  106. #define IF_PROCESS(x) if (!decode.direct) { x }
  107. #else
  108. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  109. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  110. #define IF_DIRECT(x) { x }
  111. #define IF_PROCESS(x)
  112. #endif
  113. #if LINKALL
  114. #define OV(h, fn, ...) (vorbis_ ## fn)(__VA_ARGS__)
  115. #define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
  116. #else
  117. #define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
  118. #define OG(h, fn, ...) (h)->ogg_ ## fn(__VA_ARGS__)
  119. #endif
  120. static int get_ogg_packet(void) {
  121. int status, packet = -1;
  122. LOCK_S;
  123. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  124. while (!(status = OG(&go, stream_packetout, &v->state, &v->packet)) && bytes) {
  125. // if sync_pageout (or sync_pageseek) is not called first, sync buffers build ups
  126. while (!(status = OG(&go, sync_pageout, &v->sync, &v->page)) && bytes) {
  127. size_t consumed = min(bytes, 4096);
  128. char* buffer = OG(&gv, sync_buffer, &v->sync, consumed);
  129. memcpy(buffer, streambuf->readp, consumed);
  130. OG(&gv, sync_wrote, &v->sync, consumed);
  131. _buf_inc_readp(streambuf, consumed);
  132. bytes -= consumed;
  133. }
  134. // if we have a new page, put it in
  135. if (status) OG(&go, stream_pagein, &v->state, &v->page);
  136. }
  137. // only return a negative value when true end of streaming is reached
  138. if (status > 0) packet = status;
  139. else if (stream.state > DISCONNECT || _buf_used(streambuf)) packet = 0;
  140. UNLOCK_S;
  141. return packet;
  142. }
  143. static int read_vorbis_header(void) {
  144. int status = 0;
  145. bool fetch = true;
  146. LOCK_S;
  147. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  148. while (bytes && !status) {
  149. // first fetch a page if we need one
  150. if (fetch) {
  151. size_t consumed = min(bytes, 4096);
  152. char* buffer = OG(&go, sync_buffer, &v->sync, consumed);
  153. memcpy(buffer, streambuf->readp, consumed);
  154. OG(&go, sync_wrote, &v->sync, consumed);
  155. _buf_inc_readp(streambuf, consumed);
  156. bytes -= consumed;
  157. if (!OG(&go, sync_pageseek, &v->sync, &v->page)) continue;
  158. }
  159. switch (v->status) {
  160. case OGG_SYNC:
  161. v->status = OGG_ID_HEADER;
  162. OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
  163. fetch = false;
  164. break;
  165. case OGG_ID_HEADER:
  166. status = OG(&go, stream_pagein, &v->state, &v->page);
  167. if (!OG(&go, stream_packetout, &v->state, &v->packet)) break;
  168. OV(&gv, info_init, &v->info);
  169. status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet);
  170. if (status) {
  171. LOG_ERROR("vorbis id header packet error %d", status);
  172. status = -1;
  173. } else {
  174. v->channels = v->info.channels;
  175. v->rate = v->info.rate;
  176. v->status = OGG_COMMENT_HEADER;
  177. // only fetch if no other packet already in (they should not)
  178. fetch = OG(&go, page_packets, &v->page) <= 1;
  179. if (!fetch) LOG_INFO("id packet should terminate page");
  180. LOG_INFO("id acquired");
  181. }
  182. break;
  183. case OGG_SETUP_HEADER:
  184. // header packets don't align with pages on Vorbis (contrary to Opus)
  185. if (fetch) OG(&go, stream_pagein, &v->state, &v->page);
  186. // finally build a codec if we have the packet
  187. status = OG(&go, stream_packetout, &v->state, &v->packet);
  188. if (status && ((status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet)) ||
  189. (status = OV(&gv, synthesis_init, &v->decoder, &v->info)))) {
  190. LOG_ERROR("vorbis setup header packet error %d", status);
  191. // no need to free comment, it's fake
  192. OV(&gv, info_clear, &v->info);
  193. status = -1;
  194. } else {
  195. OV(&gv, block_init, &v->decoder, &v->block);
  196. v->opened = true;
  197. LOG_INFO("codec up and running (rate: %d, channels:%d)", v->rate, v->channels);
  198. status = 1;
  199. }
  200. //@FIXME: can we have audio on that page as well?
  201. break;
  202. case OGG_COMMENT_HEADER: {
  203. // don't consume VorbisComment, just skip it
  204. int packets = OG(&go, page_packets, &v->page);
  205. if (packets) {
  206. v->status = OGG_SETUP_HEADER;
  207. OG(&go, stream_pagein, &v->state, &v->page);
  208. OG(&go, stream_packetout, &v->state, &v->packet);
  209. OV(&gv, comment_init, &v->comment);
  210. v->comment.vendor = "N/A";
  211. // because of lack of page alignment, we might have the setup page already fully in
  212. if (packets > 1) fetch = false;
  213. LOG_INFO("comment skipped succesfully");
  214. }
  215. break;
  216. }
  217. default:
  218. break;
  219. }
  220. }
  221. UNLOCK_S;
  222. return status;
  223. }
  224. inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
  225. #ifndef TREMOR_ONLY
  226. if (!tremor) return OV(&gv, synthesis_pcmout, decoder, (ogg_float_t***) pcm);
  227. #endif
  228. return OV(&gv, synthesis_pcmout, decoder, (ogg_int32_t***) pcm);
  229. }
  230. static decode_state vorbis_decode(void) {
  231. frames_t frames;
  232. u8_t *write_buf;
  233. if (decode.new_stream) {
  234. int status = read_vorbis_header();
  235. if (status == 0) {
  236. return DECODE_RUNNING;
  237. } else if (status < 0) {
  238. LOG_WARN("can't create codec");
  239. return DECODE_ERROR;
  240. }
  241. LOCK_O;
  242. output.next_sample_rate = decode_newstream(v->rate, output.supported_rates);
  243. IF_DSD( output.next_fmt = PCM; )
  244. output.track_start = outputbuf->writep;
  245. if (output.fade_mode) _checkfade(true);
  246. decode.new_stream = false;
  247. UNLOCK_O;
  248. if (v->channels > 2) {
  249. LOG_WARN("too many channels: %d", v->channels);
  250. return DECODE_ERROR;
  251. }
  252. LOG_INFO("setting track_start");
  253. }
  254. LOCK_O_direct;
  255. IF_DIRECT(
  256. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  257. write_buf = outputbuf->writep;
  258. );
  259. IF_PROCESS(
  260. frames = process.max_in_frames;
  261. write_buf = process.inbuf;
  262. );
  263. void** pcm = NULL;
  264. int packet, n = 0;
  265. if (v->overflow) {
  266. n = pcm_out(&v->decoder, &pcm);
  267. v->overflow = n - min(n, frames);
  268. } else if ((packet = get_ogg_packet()) > 0) {
  269. n = OV(&gv, synthesis, &v->block, &v->packet);
  270. if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
  271. if (n == 0) n = pcm_out(&v->decoder, &pcm);
  272. v->overflow = n - min(n, frames);
  273. } else if (!packet && !OG(&go, page_eos, &v->page)) {
  274. UNLOCK_O_direct;
  275. return DECODE_RUNNING;
  276. }
  277. if (n > 0) {
  278. ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
  279. frames = min(n, frames);
  280. frames_t count = frames;
  281. #ifndef TREMOR_ONLY
  282. if (!tremor) {
  283. if (v->channels == 2) {
  284. float* iptr_l = (float*) pcm[0];
  285. float* iptr_r = (float*) pcm[1];
  286. while (count--) {
  287. *optr++ = ALIGN_FLOAT(*iptr_l++);
  288. *optr++ = ALIGN_FLOAT(*iptr_r++);;
  289. }
  290. } else if (v->channels == 1) {
  291. float* iptr = pcm[0];
  292. while (count--) {
  293. *optr++ = ALIGN_FLOAT(*iptr);
  294. *optr++ = ALIGN_FLOAT(*iptr++);
  295. }
  296. }
  297. } else
  298. #else
  299. {
  300. if (v->channels == 2) {
  301. s32_t* iptr_l = (s32_t*) pcm[0];
  302. s32_t* iptr_r = (s32_t*) pcm[1];
  303. while (count--) {
  304. *optr++ = ALIGN(*iptr_l++);
  305. *optr++ = ALIGN(*iptr_r++);
  306. }
  307. } else if (v->channels == 1) {
  308. s32_t* iptr = (s32_t*) pcm[0];
  309. while (count--) {
  310. *optr++ = ALIGN(*iptr);
  311. *optr++ = ALIGN(*iptr++);
  312. }
  313. }
  314. }
  315. #endif
  316. // return samples to vorbis/tremor decoder
  317. OV(&gv, synthesis_read, &v->decoder, frames);
  318. IF_DIRECT(
  319. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  320. );
  321. IF_PROCESS(
  322. process.in_frames = frames;
  323. );
  324. LOG_SDEBUG("wrote %u frames", frames);
  325. } else if (n == 0) {
  326. if (packet < 0) {
  327. LOG_INFO("end of decode");
  328. UNLOCK_O_direct;
  329. return DECODE_COMPLETE;
  330. } else {
  331. LOG_INFO("no frame decoded");
  332. }
  333. } else {
  334. LOG_INFO("ov_read error: %d", n);
  335. UNLOCK_O_direct;
  336. return DECODE_COMPLETE;
  337. }
  338. UNLOCK_O_direct;
  339. return DECODE_RUNNING;
  340. }
  341. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  342. LOG_INFO("OPENING CODEC");
  343. if (v->opened) {
  344. OV(&go, block_clear, &v->block);
  345. OV(&go, info_clear, &v->info);
  346. OV(&go, dsp_clear, &v->decoder);
  347. }
  348. v->opened = false;
  349. v->status = OGG_SYNC;
  350. v->overflow = 0;
  351. OG(&gu, sync_clear, &v->sync);
  352. OG(&gu, stream_clear, &v->state);
  353. OG(&gu, stream_init, &v->state, -1);
  354. }
  355. static void vorbis_close() {
  356. return;
  357. LOG_INFO("CLOSING CODEC");
  358. if (v->opened) {
  359. OV(&go, block_clear, &v->block);
  360. OV(&go, info_clear, &v->info);
  361. OV(&go, dsp_clear, &v->decoder);
  362. }
  363. v->opened = false;
  364. OG(&go, stream_clear, &v->state);
  365. OG(&go, sync_clear, &v->sync);
  366. }
  367. static bool load_vorbis() {
  368. #if !LINKALL
  369. char *err;
  370. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  371. if (!g_handle) {
  372. LOG_INFO("ogg dlerror: %s", dlerror());
  373. return false
  374. }
  375. void *v_handle = NULL;
  376. #ifndef TREMOR_ONLY
  377. v_handle = dlopen(LIBVORBIS, RTLD_NOW);
  378. #endif
  379. if (!v_handle) {
  380. v_handle = dlopen(LIBTREMOR, RTLD_NOW);
  381. if (v_handle) {
  382. tremor = true;
  383. } else {
  384. dlclose(g_handle);
  385. LOG_INFO("vorbis/tremor dlerror: %s", dlerror());
  386. return false;
  387. }
  388. }
  389. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  390. g_handle->ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  391. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  392. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  393. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  394. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  395. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  396. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  397. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  398. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  399. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  400. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  401. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  402. v_handle.ov_read = dlsym(handle, "ov_read");
  403. v_handle.ov_info = dlsym(handle, "ov_info");
  404. v_handle.ov_clear = dlsym(handle, "ov_clear");
  405. v_handle.ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  406. if ((err = dlerror()) != NULL) {
  407. LOG_INFO("dlerror: %s", err);
  408. return false;
  409. }
  410. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  411. #endif
  412. return true;
  413. }
  414. struct codec *register_vorbis(void) {
  415. static struct codec ret = {
  416. 'o', // id
  417. "ogg", // types
  418. 4096, // min read
  419. 20480, // min space
  420. vorbis_open, // open
  421. vorbis_close, // close
  422. vorbis_decode,// decode
  423. };
  424. if ((v = calloc(1, sizeof(struct vorbis))) == NULL) {
  425. return NULL;
  426. }
  427. v->opened = false;
  428. if (!load_vorbis()) {
  429. return NULL;
  430. }
  431. LOG_INFO("using vorbis to decode ogg");
  432. return &ret;
  433. }