vorbis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. bool eos;
  63. };
  64. #if !LINKALL
  65. static struct vorbis {
  66. // vorbis symbols to be dynamically loaded - from either vorbisfile or vorbisidec (tremor) version of library
  67. vorbis_info *(* ov_info)(OggVorbis_File *vf, int link);
  68. int (* ov_clear)(OggVorbis_File *vf);
  69. long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
  70. long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
  71. int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
  72. } gv;
  73. static struct {
  74. void *handle;
  75. int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
  76. int (*ogg_stream_clear)(ogg_stream_state* os);
  77. int (*ogg_stream_reset)(ogg_stream_state* os);
  78. int (*ogg_stream_eos)(ogg_stream_state* os);
  79. int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
  80. int (*ogg_sync_clear)(ogg_sync_state* oy);
  81. void (*ogg_packet_clear)(ogg_packet* op);
  82. char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
  83. int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
  84. long (*ogg_sync_pageseek)(ogg_sync_state* oy, ogg_page* og);
  85. int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
  86. int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
  87. int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
  88. int (*ogg_page_packets)(const ogg_page* og);
  89. } go;
  90. #endif
  91. static struct vorbis *v;
  92. extern log_level loglevel;
  93. extern struct buffer *streambuf;
  94. extern struct buffer *outputbuf;
  95. extern struct streamstate stream;
  96. extern struct outputstate output;
  97. extern struct decodestate decode;
  98. extern struct processstate process;
  99. #define LOCK_S mutex_lock(streambuf->mutex)
  100. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  101. #define LOCK_O mutex_lock(outputbuf->mutex)
  102. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  103. #if PROCESS
  104. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  105. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  106. #define IF_DIRECT(x) if (decode.direct) { x }
  107. #define IF_PROCESS(x) if (!decode.direct) { x }
  108. #else
  109. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  110. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  111. #define IF_DIRECT(x) { x }
  112. #define IF_PROCESS(x)
  113. #endif
  114. #if LINKALL
  115. #define OV(h, fn, ...) (vorbis_ ## fn)(__VA_ARGS__)
  116. #define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
  117. #else
  118. #define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
  119. #define OG(h, fn, ...) (h)->ogg_ ## fn(__VA_ARGS__)
  120. #endif
  121. static int get_ogg_packet(void) {
  122. int status = 0;
  123. LOCK_S;
  124. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  125. while (!(status = OG(&go, stream_packetout, &v->state, &v->packet)) && bytes) {
  126. do {
  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. } while (!(status = OG(&go, sync_pageseek, &v->sync, &v->page)) && bytes);
  134. // if we have a new page, put it in
  135. if (status) OG(&go, stream_pagein, &v->state, &v->page);
  136. }
  137. UNLOCK_S;
  138. return status;
  139. }
  140. static int read_vorbis_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(&go, sync_buffer, &v->sync, consumed);
  150. memcpy(buffer, streambuf->readp, consumed);
  151. OG(&go, sync_wrote, &v->sync, consumed);
  152. _buf_inc_readp(streambuf, consumed);
  153. bytes -= consumed;
  154. if (!OG(&go, sync_pageseek, &v->sync, &v->page)) continue;
  155. }
  156. switch (v->status) {
  157. case OGG_SYNC:
  158. v->status = OGG_ID_HEADER;
  159. OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
  160. fetch = false;
  161. break;
  162. case OGG_ID_HEADER:
  163. status = OG(&go, stream_pagein, &v->state, &v->page);
  164. if (!OG(&go, stream_packetout, &v->state, &v->packet)) break;
  165. OV(&gv, info_init, &v->info);
  166. status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet);
  167. if (status) {
  168. LOG_ERROR("vorbis id header packet error %d", status);
  169. status = -1;
  170. } else {
  171. v->channels = v->info.channels;
  172. v->rate = v->info.rate;
  173. v->status = OGG_COMMENT_HEADER;
  174. // only fetch if no other packet already in (they should not)
  175. fetch = OG(&go, page_packets, &v->page) <= 1;
  176. if (!fetch) LOG_INFO("id packet should terminate page");
  177. LOG_INFO("id acquired");
  178. }
  179. break;
  180. case OGG_SETUP_HEADER:
  181. // header packets don't align with pages on Vorbis (contrary to Opus)
  182. if (fetch) OG(&go, stream_pagein, &v->state, &v->page);
  183. // finally build a codec if we have the packet
  184. status = OG(&go, stream_packetout, &v->state, &v->packet);
  185. if (status && ((status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet)) ||
  186. (status = OV(&gv, synthesis_init, &v->decoder, &v->info)))) {
  187. LOG_ERROR("vorbis setup header packet error %d", status);
  188. // no need to free comment, it's fake
  189. OV(&gv, info_clear, &v->info);
  190. status = -1;
  191. } else {
  192. OV(&gv, block_init, &v->decoder, &v->block);
  193. v->opened = true;
  194. LOG_INFO("codec up and running (rate: %d, channels:%d)", v->rate, v->channels);
  195. status = 1;
  196. }
  197. //@FIXME: can we have audio on that page as well?
  198. break;
  199. case OGG_COMMENT_HEADER: {
  200. // don't consume VorbisComment, just skip it
  201. int packets = OG(&go, page_packets, &v->page);
  202. if (packets) {
  203. v->status = OGG_SETUP_HEADER;
  204. OG(&go, stream_pagein, &v->state, &v->page);
  205. OG(&go, stream_packetout, &v->state, &v->packet);
  206. OV(&gv, comment_init, &v->comment);
  207. v->comment.vendor = "N/A";
  208. // because of lack of page alignment, we might have the setup page already fully in
  209. if (packets > 1) fetch = false;
  210. LOG_INFO("comment skipped succesfully");
  211. }
  212. break;
  213. }
  214. default:
  215. break;
  216. }
  217. }
  218. UNLOCK_S;
  219. return status;
  220. }
  221. inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
  222. #ifndef TREMOR_ONLY
  223. if (!tremor) return OV(&gv, synthesis_pcmout, decoder, (ogg_float_t***) pcm);
  224. #endif
  225. return OV(&gv, synthesis_pcmout, decoder, (ogg_int32_t***) pcm);
  226. }
  227. static decode_state vorbis_decode(void) {
  228. frames_t frames;
  229. int n = 0;
  230. u8_t *write_buf;
  231. void** pcm = NULL;
  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. if (v->overflow) {
  263. n = pcm_out(&v->decoder, &pcm);
  264. v->overflow = n - min(n, frames);
  265. } else if (get_ogg_packet() > 0) {
  266. n = OV(&gv, synthesis, &v->block, &v->packet);
  267. if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
  268. if (n == 0) n = pcm_out(&v->decoder, &pcm);
  269. v->overflow = n - min(n, frames);
  270. } else if (!OG(&go, page_eos, &v->page)) {
  271. UNLOCK_O_direct;
  272. return DECODE_RUNNING;
  273. } else v->eos = true;
  274. if (n > 0) {
  275. ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
  276. frames = min(n, frames);
  277. frames_t count = frames;
  278. #ifndef TREMOR_ONLY
  279. if (!tremor) {
  280. if (v->channels == 2) {
  281. float* iptr_l = (float*) pcm[0];
  282. float* iptr_r = (float*) pcm[1];
  283. while (count--) {
  284. *optr++ = ALIGN_FLOAT(*iptr_l++);
  285. *optr++ = ALIGN_FLOAT(*iptr_r++);;
  286. }
  287. } else if (v->channels == 1) {
  288. float* iptr = pcm[0];
  289. while (count--) {
  290. *optr++ = ALIGN_FLOAT(*iptr);
  291. *optr++ = ALIGN_FLOAT(*iptr++);
  292. }
  293. }
  294. } else
  295. #else
  296. {
  297. if (v->channels == 2) {
  298. s32_t* iptr_l = (s32_t*) pcm[0];
  299. s32_t* iptr_r = (s32_t*) pcm[1];
  300. while (count--) {
  301. *optr++ = ALIGN(*iptr_l++);
  302. *optr++ = ALIGN(*iptr_r++);
  303. }
  304. } else if (v->channels == 1) {
  305. s32_t* iptr = (s32_t*) pcm[0];
  306. while (count--) {
  307. *optr++ = ALIGN(*iptr);
  308. *optr++ = ALIGN(*iptr++);
  309. }
  310. }
  311. }
  312. #endif
  313. // return samples to vorbis/tremor decoder
  314. OV(&gv, synthesis_read, &v->decoder, frames);
  315. IF_DIRECT(
  316. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  317. );
  318. IF_PROCESS(
  319. process.in_frames = frames;
  320. );
  321. LOG_SDEBUG("wrote %u frames", frames);
  322. } else if (n == 0) {
  323. if (stream.state <= DISCONNECT && v->eos) {
  324. LOG_INFO("end of decode");
  325. UNLOCK_O_direct;
  326. return DECODE_COMPLETE;
  327. } else {
  328. LOG_INFO("no frame decoded");
  329. }
  330. } else {
  331. LOG_INFO("ov_read error: %d", n);
  332. UNLOCK_O_direct;
  333. return DECODE_COMPLETE;
  334. }
  335. UNLOCK_O_direct;
  336. return DECODE_RUNNING;
  337. }
  338. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  339. LOG_INFO("OPENING CODEC");
  340. if (v->opened) {
  341. OV(&go, block_clear, &v->block);
  342. OV(&go, info_clear, &v->info);
  343. OV(&go, dsp_clear, &v->decoder);
  344. }
  345. v->eos = false;
  346. v->opened = false;
  347. v->status = OGG_SYNC;
  348. v->overflow = 0;
  349. OG(&gu, sync_clear, &v->sync);
  350. OG(&gu, stream_clear, &v->state);
  351. OG(&gu, stream_init, &v->state, -1);
  352. }
  353. static void vorbis_close() {
  354. return;
  355. LOG_INFO("CLOSING CODEC");
  356. if (v->opened) {
  357. OV(&go, block_clear, &v->block);
  358. OV(&go, info_clear, &v->info);
  359. OV(&go, dsp_clear, &v->decoder);
  360. }
  361. v->opened = false;
  362. OG(&go, stream_clear, &v->state);
  363. OG(&go, sync_clear, &v->sync);
  364. }
  365. static bool load_vorbis() {
  366. #if !LINKALL
  367. char *err;
  368. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  369. if (!g_handle) {
  370. LOG_INFO("ogg dlerror: %s", dlerror());
  371. return false
  372. }
  373. void *v_handle = NULL;
  374. #ifndef TREMOR_ONLY
  375. v_handle = dlopen(LIBVORBIS, RTLD_NOW);
  376. #endif
  377. if (!v_handle) {
  378. v_handle = dlopen(LIBTREMOR, RTLD_NOW);
  379. if (v_handle) {
  380. tremor = true;
  381. } else {
  382. dlclose(g_handle);
  383. LOG_INFO("vorbis/tremor dlerror: %s", dlerror());
  384. return false;
  385. }
  386. }
  387. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  388. g_handle->ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  389. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  390. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  391. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  392. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  393. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  394. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  395. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  396. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  397. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  398. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  399. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  400. v_handle.ov_read = dlsym(handle, "ov_read");
  401. v_handle.ov_info = dlsym(handle, "ov_info");
  402. v_handle.ov_clear = dlsym(handle, "ov_clear");
  403. v_handle.ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  404. if ((err = dlerror()) != NULL) {
  405. LOG_INFO("dlerror: %s", err);
  406. return false;
  407. }
  408. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  409. #endif
  410. return true;
  411. }
  412. struct codec *register_vorbis(void) {
  413. static struct codec ret = {
  414. 'o', // id
  415. "ogg", // types
  416. 4096, // min read
  417. 20480, // min space
  418. vorbis_open, // open
  419. vorbis_close, // close
  420. vorbis_decode,// decode
  421. };
  422. if ((v = calloc(1, sizeof(struct vorbis))) == NULL) {
  423. return NULL;
  424. }
  425. v->opened = false;
  426. if (!load_vorbis()) {
  427. return NULL;
  428. }
  429. LOG_INFO("using vorbis to decode ogg");
  430. return &ret;
  431. }