vorbis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. do {
  126. size_t consumed = min(bytes, 4096);
  127. char* buffer = OG(&gv, sync_buffer, &v->sync, consumed);
  128. memcpy(buffer, streambuf->readp, consumed);
  129. OG(&gv, sync_wrote, &v->sync, consumed);
  130. _buf_inc_readp(streambuf, consumed);
  131. bytes -= consumed;
  132. } while (!(status = OG(&go, sync_pageseek, &v->sync, &v->page)) && bytes);
  133. // if we have a new page, put it in
  134. if (status) OG(&go, stream_pagein, &v->state, &v->page);
  135. }
  136. // only return a negative value when end of streaming is reached
  137. if (status > 0) packet = status;
  138. else if (stream.state > DISCONNECT) packet = 0;
  139. UNLOCK_S;
  140. return packet;
  141. }
  142. static int read_vorbis_header(void) {
  143. int status = 0;
  144. bool fetch = true;
  145. LOCK_S;
  146. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  147. while (bytes && !status) {
  148. // first fetch a page if we need one
  149. if (fetch) {
  150. size_t consumed = min(bytes, 4096);
  151. char* buffer = OG(&go, sync_buffer, &v->sync, consumed);
  152. memcpy(buffer, streambuf->readp, consumed);
  153. OG(&go, sync_wrote, &v->sync, consumed);
  154. _buf_inc_readp(streambuf, consumed);
  155. bytes -= consumed;
  156. if (!OG(&go, sync_pageseek, &v->sync, &v->page)) continue;
  157. }
  158. switch (v->status) {
  159. case OGG_SYNC:
  160. v->status = OGG_ID_HEADER;
  161. OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
  162. fetch = false;
  163. break;
  164. case OGG_ID_HEADER:
  165. status = OG(&go, stream_pagein, &v->state, &v->page);
  166. if (!OG(&go, stream_packetout, &v->state, &v->packet)) break;
  167. OV(&gv, info_init, &v->info);
  168. status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet);
  169. if (status) {
  170. LOG_ERROR("vorbis id header packet error %d", status);
  171. status = -1;
  172. } else {
  173. v->channels = v->info.channels;
  174. v->rate = v->info.rate;
  175. v->status = OGG_COMMENT_HEADER;
  176. // only fetch if no other packet already in (they should not)
  177. fetch = OG(&go, page_packets, &v->page) <= 1;
  178. if (!fetch) LOG_INFO("id packet should terminate page");
  179. LOG_INFO("id acquired");
  180. }
  181. break;
  182. case OGG_SETUP_HEADER:
  183. // header packets don't align with pages on Vorbis (contrary to Opus)
  184. if (fetch) OG(&go, stream_pagein, &v->state, &v->page);
  185. // finally build a codec if we have the packet
  186. status = OG(&go, stream_packetout, &v->state, &v->packet);
  187. if (status && ((status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet)) ||
  188. (status = OV(&gv, synthesis_init, &v->decoder, &v->info)))) {
  189. LOG_ERROR("vorbis setup header packet error %d", status);
  190. // no need to free comment, it's fake
  191. OV(&gv, info_clear, &v->info);
  192. status = -1;
  193. } else {
  194. OV(&gv, block_init, &v->decoder, &v->block);
  195. v->opened = true;
  196. LOG_INFO("codec up and running (rate: %d, channels:%d)", v->rate, v->channels);
  197. status = 1;
  198. }
  199. //@FIXME: can we have audio on that page as well?
  200. break;
  201. case OGG_COMMENT_HEADER: {
  202. // don't consume VorbisComment, just skip it
  203. int packets = OG(&go, page_packets, &v->page);
  204. if (packets) {
  205. v->status = OGG_SETUP_HEADER;
  206. OG(&go, stream_pagein, &v->state, &v->page);
  207. OG(&go, stream_packetout, &v->state, &v->packet);
  208. OV(&gv, comment_init, &v->comment);
  209. v->comment.vendor = "N/A";
  210. // because of lack of page alignment, we might have the setup page already fully in
  211. if (packets > 1) fetch = false;
  212. LOG_INFO("comment skipped succesfully");
  213. }
  214. break;
  215. }
  216. default:
  217. break;
  218. }
  219. }
  220. UNLOCK_S;
  221. return status;
  222. }
  223. inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
  224. #ifndef TREMOR_ONLY
  225. if (!tremor) return OV(&gv, synthesis_pcmout, decoder, (ogg_float_t***) pcm);
  226. #endif
  227. return OV(&gv, synthesis_pcmout, decoder, (ogg_int32_t***) pcm);
  228. }
  229. static decode_state vorbis_decode(void) {
  230. frames_t frames;
  231. u8_t *write_buf;
  232. if (decode.new_stream) {
  233. int status = read_vorbis_header();
  234. if (status == 0) {
  235. return DECODE_RUNNING;
  236. } else if (status < 0) {
  237. LOG_WARN("can't create codec");
  238. return DECODE_ERROR;
  239. }
  240. LOCK_O;
  241. output.next_sample_rate = decode_newstream(v->rate, output.supported_rates);
  242. IF_DSD( output.next_fmt = PCM; )
  243. output.track_start = outputbuf->writep;
  244. if (output.fade_mode) _checkfade(true);
  245. decode.new_stream = false;
  246. UNLOCK_O;
  247. if (v->channels > 2) {
  248. LOG_WARN("too many channels: %d", v->channels);
  249. return DECODE_ERROR;
  250. }
  251. LOG_INFO("setting track_start");
  252. }
  253. LOCK_O_direct;
  254. IF_DIRECT(
  255. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  256. write_buf = outputbuf->writep;
  257. );
  258. IF_PROCESS(
  259. frames = process.max_in_frames;
  260. write_buf = process.inbuf;
  261. );
  262. void** pcm = NULL;
  263. int packet, n = 0;
  264. if (v->overflow) {
  265. n = pcm_out(&v->decoder, &pcm);
  266. v->overflow = n - min(n, frames);
  267. } else if ((packet = get_ogg_packet()) > 0) {
  268. n = OV(&gv, synthesis, &v->block, &v->packet);
  269. if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
  270. if (n == 0) n = pcm_out(&v->decoder, &pcm);
  271. v->overflow = n - min(n, frames);
  272. } else if (!packet && !OG(&go, page_eos, &v->page)) {
  273. UNLOCK_O_direct;
  274. return DECODE_RUNNING;
  275. }
  276. if (n > 0) {
  277. ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
  278. frames = min(n, frames);
  279. frames_t count = frames;
  280. #ifndef TREMOR_ONLY
  281. if (!tremor) {
  282. if (v->channels == 2) {
  283. float* iptr_l = (float*) pcm[0];
  284. float* iptr_r = (float*) pcm[1];
  285. while (count--) {
  286. *optr++ = ALIGN_FLOAT(*iptr_l++);
  287. *optr++ = ALIGN_FLOAT(*iptr_r++);;
  288. }
  289. } else if (v->channels == 1) {
  290. float* iptr = pcm[0];
  291. while (count--) {
  292. *optr++ = ALIGN_FLOAT(*iptr);
  293. *optr++ = ALIGN_FLOAT(*iptr++);
  294. }
  295. }
  296. } else
  297. #else
  298. {
  299. if (v->channels == 2) {
  300. s32_t* iptr_l = (s32_t*) pcm[0];
  301. s32_t* iptr_r = (s32_t*) pcm[1];
  302. while (count--) {
  303. *optr++ = ALIGN(*iptr_l++);
  304. *optr++ = ALIGN(*iptr_r++);
  305. }
  306. } else if (v->channels == 1) {
  307. s32_t* iptr = (s32_t*) pcm[0];
  308. while (count--) {
  309. *optr++ = ALIGN(*iptr);
  310. *optr++ = ALIGN(*iptr++);
  311. }
  312. }
  313. }
  314. #endif
  315. // return samples to vorbis/tremor decoder
  316. OV(&gv, synthesis_read, &v->decoder, frames);
  317. IF_DIRECT(
  318. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  319. );
  320. IF_PROCESS(
  321. process.in_frames = frames;
  322. );
  323. LOG_SDEBUG("wrote %u frames", frames);
  324. } else if (n == 0) {
  325. if (packet < 0) {
  326. LOG_INFO("end of decode");
  327. UNLOCK_O_direct;
  328. return DECODE_COMPLETE;
  329. } else {
  330. LOG_INFO("no frame decoded");
  331. }
  332. } else {
  333. LOG_INFO("ov_read error: %d", n);
  334. UNLOCK_O_direct;
  335. return DECODE_COMPLETE;
  336. }
  337. UNLOCK_O_direct;
  338. return DECODE_RUNNING;
  339. }
  340. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  341. LOG_INFO("OPENING CODEC");
  342. if (v->opened) {
  343. OV(&go, block_clear, &v->block);
  344. OV(&go, info_clear, &v->info);
  345. OV(&go, dsp_clear, &v->decoder);
  346. }
  347. v->opened = false;
  348. v->status = OGG_SYNC;
  349. v->overflow = 0;
  350. OG(&gu, sync_clear, &v->sync);
  351. OG(&gu, stream_clear, &v->state);
  352. OG(&gu, stream_init, &v->state, -1);
  353. }
  354. static void vorbis_close() {
  355. return;
  356. LOG_INFO("CLOSING CODEC");
  357. if (v->opened) {
  358. OV(&go, block_clear, &v->block);
  359. OV(&go, info_clear, &v->info);
  360. OV(&go, dsp_clear, &v->decoder);
  361. }
  362. v->opened = false;
  363. OG(&go, stream_clear, &v->state);
  364. OG(&go, sync_clear, &v->sync);
  365. }
  366. static bool load_vorbis() {
  367. #if !LINKALL
  368. char *err;
  369. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  370. if (!g_handle) {
  371. LOG_INFO("ogg dlerror: %s", dlerror());
  372. return false
  373. }
  374. void *v_handle = NULL;
  375. #ifndef TREMOR_ONLY
  376. v_handle = dlopen(LIBVORBIS, RTLD_NOW);
  377. #endif
  378. if (!v_handle) {
  379. v_handle = dlopen(LIBTREMOR, RTLD_NOW);
  380. if (v_handle) {
  381. tremor = true;
  382. } else {
  383. dlclose(g_handle);
  384. LOG_INFO("vorbis/tremor dlerror: %s", dlerror());
  385. return false;
  386. }
  387. }
  388. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  389. g_handle->ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  390. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  391. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  392. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  393. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  394. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  395. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  396. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  397. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  398. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  399. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  400. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  401. v_handle.ov_read = dlsym(handle, "ov_read");
  402. v_handle.ov_info = dlsym(handle, "ov_info");
  403. v_handle.ov_clear = dlsym(handle, "ov_clear");
  404. v_handle.ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  405. if ((err = dlerror()) != NULL) {
  406. LOG_INFO("dlerror: %s", err);
  407. return false;
  408. }
  409. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  410. #endif
  411. return true;
  412. }
  413. struct codec *register_vorbis(void) {
  414. static struct codec ret = {
  415. 'o', // id
  416. "ogg", // types
  417. 4096, // min read
  418. 20480, // min space
  419. vorbis_open, // open
  420. vorbis_close, // close
  421. vorbis_decode,// decode
  422. };
  423. if ((v = calloc(1, sizeof(struct vorbis))) == NULL) {
  424. return NULL;
  425. }
  426. v->opened = false;
  427. if (!load_vorbis()) {
  428. return NULL;
  429. }
  430. LOG_INFO("using vorbis to decode ogg");
  431. return &ret;
  432. }