vorbis.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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_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. void (* ov_info_init)(vorbis_info* vi);
  69. void (* ov_info_clear)(vorbis_info* vi);
  70. void (* ov_comment_init)(vorbis_comment* vc);
  71. void (* ov_comment_clear(vorbis_comment *vc);
  72. int (* ov_block_init)(vorbis_dsp_state* v, vorbis_block* vb);
  73. int (* ov_block_clear)(vorbis_block* vb);
  74. void (* ov_vorbis_dsp_clear)(vorbis_dsp_state* v);
  75. long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
  76. long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
  77. int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
  78. } gv;
  79. static struct {
  80. void *handle;
  81. int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
  82. int (*ogg_stream_clear)(ogg_stream_state* os);
  83. int (*ogg_stream_reset)(ogg_stream_state* os);
  84. int (*ogg_stream_eos)(ogg_stream_state* os);
  85. int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
  86. int (*ogg_sync_clear)(ogg_sync_state* oy);
  87. void (*ogg_packet_clear)(ogg_packet* op);
  88. char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
  89. int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
  90. long (*ogg_sync_pageseek)(ogg_sync_state* oy, ogg_page* og);
  91. int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
  92. int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
  93. int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
  94. int (*ogg_page_packets)(const ogg_page* og);
  95. } go;
  96. #endif
  97. static struct vorbis *v;
  98. extern log_level loglevel;
  99. extern struct buffer *streambuf;
  100. extern struct buffer *outputbuf;
  101. extern struct streamstate stream;
  102. extern struct outputstate output;
  103. extern struct decodestate decode;
  104. extern struct processstate process;
  105. #define LOCK_S mutex_lock(streambuf->mutex)
  106. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  107. #define LOCK_O mutex_lock(outputbuf->mutex)
  108. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  109. #if PROCESS
  110. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  111. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  112. #define IF_DIRECT(x) if (decode.direct) { x }
  113. #define IF_PROCESS(x) if (!decode.direct) { x }
  114. #else
  115. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  116. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  117. #define IF_DIRECT(x) { x }
  118. #define IF_PROCESS(x)
  119. #endif
  120. #if LINKALL
  121. #define OV(h, fn, ...) (vorbis_ ## fn)(__VA_ARGS__)
  122. #define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
  123. #else
  124. #define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
  125. #define OG(h, fn, ...) (h)->ogg_ ## fn(__VA_ARGS__)
  126. #endif
  127. static int get_audio_packet(void) {
  128. int status, packet = -1;
  129. LOCK_S;
  130. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  131. while (!(status = OG(&go, stream_packetout, &v->state, &v->packet)) && bytes) {
  132. // if sync_pageout (or sync_pageseek) is not called here, sync buffers build up
  133. while (!(status = OG(&go, sync_pageout, &v->sync, &v->page)) && bytes) {
  134. size_t consumed = min(bytes, 4096);
  135. char* buffer = OG(&go, sync_buffer, &v->sync, consumed);
  136. memcpy(buffer, streambuf->readp, consumed);
  137. OG(&go, sync_wrote, &v->sync, consumed);
  138. _buf_inc_readp(streambuf, consumed);
  139. bytes -= consumed;
  140. }
  141. // if we have a new page, put it in and reset serialno at BoS
  142. if (status) {
  143. OG(&go, stream_pagein, &v->state, &v->page);
  144. if (OG(&go, page_bos, &v->page)) OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
  145. }
  146. }
  147. /* odd packets are not audio and should be discarded. With no packet, we
  148. * return a negative value when there is really nothing more to proceed */
  149. if (status > 0 && (v->packet.packet[0] & 0x01) == 0) packet = status;
  150. else if (stream.state > DISCONNECT || _buf_used(streambuf)) packet = 0;
  151. UNLOCK_S;
  152. return packet;
  153. }
  154. static int read_vorbis_header(void) {
  155. int done = 0;
  156. bool fetch = true;
  157. LOCK_S;
  158. size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  159. while (bytes && !done) {
  160. int status;
  161. // get aligned to a page and ready to bring it in
  162. do {
  163. size_t consumed = min(bytes, 4096);
  164. char* buffer = OG(&go, sync_buffer, &v->sync, consumed);
  165. memcpy(buffer, streambuf->readp, consumed);
  166. OG(&go, sync_wrote, &v->sync, consumed);
  167. _buf_inc_readp(streambuf, consumed);
  168. bytes -= consumed;
  169. status = fetch ? OG(&go, sync_pageout, &v->sync, &v->page) :
  170. OG(&go, sync_pageseek, &v->sync, &v->page);
  171. } while (bytes && status <= 0);
  172. // nothing has been found and we have no more bytes, come back later
  173. if (status <= 0) break;
  174. // always set stream serialno if we have a new one (no multiplexed streams)
  175. if (OG(&go, page_bos, &v->page)) OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
  176. // bring new page in if we want it (otherwise we're just skipping)
  177. if (fetch) OG(&go, stream_pagein, &v->state, &v->page);
  178. // not a switch...case b/c we might have multiple packets in a page in vorbis
  179. if (v->status == OGG_ID_HEADER) {
  180. // we need the id packet, get more pages if we don't
  181. if (!OG(&go, stream_packetout, &v->state, &v->packet)) continue;
  182. OV(&gv, info_init, &v->info);
  183. status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet);
  184. if (status) {
  185. LOG_ERROR("id header packet error %d", status);
  186. done = -1;
  187. } else {
  188. v->channels = v->info.channels;
  189. v->rate = v->info.rate;
  190. v->status = OGG_COMMENT_HEADER;
  191. fetch = false;
  192. LOG_INFO("id acquired");
  193. // we should only have one packet, so get next pages
  194. if (OG(&go, page_packets, &v->page) == 1) continue;
  195. }
  196. }
  197. if (v->status == OGG_COMMENT_HEADER) {
  198. // don't consume VorbisComment which could be a huge packet, just skip it
  199. int packets = OG(&go, page_packets, &v->page);
  200. if (!packets) continue;
  201. // we have a "fake" comment packet that is just has the last page...
  202. v->status = OGG_SETUP_HEADER;
  203. OG(&go, stream_pagein, &v->state, &v->page);
  204. OG(&go, stream_packetout, &v->state, &v->packet);
  205. OV(&gv, comment_init, &v->comment);
  206. v->comment.vendor = "N/A";
  207. fetch = true;
  208. LOG_INFO("comment skipped successfully");
  209. // because of lack of page alignment, we might have the setup page already fully in
  210. if (packets == 1) continue;
  211. }
  212. if (v->status == OGG_SETUP_HEADER) {
  213. // we need the setup packet, get more pages if we don't
  214. if (OG(&go, stream_packetout, &v->state, &v->packet) <= 0) continue;
  215. // finally build a codec if we have the packet
  216. if (OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet) ||
  217. OV(&gv, synthesis_init, &v->decoder, &v->info)) {
  218. LOG_ERROR("setup header packet error %d", status);
  219. // no need to free comment, it's fake
  220. OV(&gv, info_clear, &v->info);
  221. done = -1;
  222. } else {
  223. OV(&gv, block_init, &v->decoder, &v->block);
  224. v->opened = true;
  225. LOG_INFO("codec up and running");
  226. done = 1;
  227. }
  228. break;
  229. }
  230. }
  231. UNLOCK_S;
  232. return done;
  233. }
  234. inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
  235. #ifndef TREMOR_ONLY
  236. if (!tremor) return OV(&gv, synthesis_pcmout, decoder, (ogg_float_t***) pcm);
  237. #endif
  238. return OV(&gv, synthesis_pcmout, decoder, (ogg_int32_t***) pcm);
  239. }
  240. static decode_state vorbis_decode(void) {
  241. frames_t frames;
  242. u8_t *write_buf;
  243. if (decode.new_stream) {
  244. int status = read_vorbis_header();
  245. if (status == 0) {
  246. return DECODE_RUNNING;
  247. } else if (status < 0) {
  248. LOG_WARN("can't create codec");
  249. return DECODE_ERROR;
  250. }
  251. LOCK_O;
  252. output.next_sample_rate = decode_newstream(v->rate, output.supported_rates);
  253. IF_DSD( output.next_fmt = PCM; )
  254. output.track_start = outputbuf->writep;
  255. if (output.fade_mode) _checkfade(true);
  256. decode.new_stream = false;
  257. UNLOCK_O;
  258. if (v->channels > 2) {
  259. LOG_WARN("too many channels: %d", v->channels);
  260. return DECODE_ERROR;
  261. }
  262. LOG_INFO("setting track_start");
  263. }
  264. LOCK_O_direct;
  265. IF_DIRECT(
  266. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  267. write_buf = outputbuf->writep;
  268. );
  269. IF_PROCESS(
  270. frames = process.max_in_frames;
  271. write_buf = process.inbuf;
  272. );
  273. void** pcm = NULL;
  274. int packet, n = 0;
  275. if (v->overflow) {
  276. n = pcm_out(&v->decoder, &pcm);
  277. v->overflow = n - min(n, frames);
  278. } else if ((packet = get_audio_packet()) > 0) {
  279. n = OV(&gv, synthesis, &v->block, &v->packet);
  280. if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
  281. if (n == 0) n = pcm_out(&v->decoder, &pcm);
  282. v->overflow = n - min(n, frames);
  283. } else if (!packet) {
  284. UNLOCK_O_direct;
  285. return DECODE_RUNNING;
  286. }
  287. if (n > 0) {
  288. ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
  289. frames = min(n, frames);
  290. frames_t count = frames;
  291. #ifndef TREMOR_ONLY
  292. if (!tremor) {
  293. if (v->channels == 2) {
  294. float* iptr_l = (float*) pcm[0];
  295. float* iptr_r = (float*) pcm[1];
  296. while (count--) {
  297. *optr++ = ALIGN_FLOAT(*iptr_l++);
  298. *optr++ = ALIGN_FLOAT(*iptr_r++);;
  299. }
  300. } else if (v->channels == 1) {
  301. float* iptr = pcm[0];
  302. while (count--) {
  303. *optr++ = ALIGN_FLOAT(*iptr);
  304. *optr++ = ALIGN_FLOAT(*iptr++);
  305. }
  306. }
  307. } else
  308. #else
  309. {
  310. if (v->channels == 2) {
  311. s32_t* iptr_l = (s32_t*) pcm[0];
  312. s32_t* iptr_r = (s32_t*) pcm[1];
  313. while (count--) {
  314. *optr++ = ALIGN(*iptr_l++);
  315. *optr++ = ALIGN(*iptr_r++);
  316. }
  317. } else if (v->channels == 1) {
  318. s32_t* iptr = (s32_t*) pcm[0];
  319. while (count--) {
  320. *optr++ = ALIGN(*iptr);
  321. *optr++ = ALIGN(*iptr++);
  322. }
  323. }
  324. }
  325. #endif
  326. // return samples to vorbis/tremor decoder
  327. OV(&gv, synthesis_read, &v->decoder, frames);
  328. IF_DIRECT(
  329. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  330. );
  331. IF_PROCESS(
  332. process.in_frames = frames;
  333. );
  334. LOG_SDEBUG("wrote %u frames", frames);
  335. } else if (n == 0) {
  336. if (packet < 0) {
  337. LOG_INFO("end of decode");
  338. UNLOCK_O_direct;
  339. return DECODE_COMPLETE;
  340. } else {
  341. LOG_INFO("no frame decoded");
  342. }
  343. } else {
  344. LOG_INFO("ov_read error: %d", n);
  345. UNLOCK_O_direct;
  346. return DECODE_COMPLETE;
  347. }
  348. UNLOCK_O_direct;
  349. return DECODE_RUNNING;
  350. }
  351. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  352. if (v->opened) {
  353. OV(&gv, block_clear, &v->block);
  354. OV(&gv, dsp_clear, &v->decoder);
  355. OV(&gv, info_clear, &v->info);
  356. }
  357. v->opened = false;
  358. v->status = OGG_ID_HEADER;
  359. v->overflow = 0;
  360. OG(&go, stream_clear, &v->state);
  361. OG(&go, sync_clear, &v->sync);
  362. OG(&go, stream_init, &v->state, -1);
  363. }
  364. static void vorbis_close() {
  365. if (v->opened) {
  366. OV(&gv, block_clear, &v->block);
  367. OV(&gv, dsp_clear, &v->decoder);
  368. // info must be last otherwise there is memory leak (where is it said... nowhere)
  369. OV(&gv, info_clear, &v->info);
  370. // we don' t have comments to free
  371. }
  372. v->opened = false;
  373. OG(&go, stream_clear, &v->state);
  374. OG(&go, sync_clear, &v->sync);
  375. }
  376. static bool load_vorbis() {
  377. #if !LINKALL
  378. char *err;
  379. void *g_handle = dlopen(LIBOGG, RTLD_NOW);
  380. if (!g_handle) {
  381. LOG_INFO("ogg dlerror: %s", dlerror());
  382. return false
  383. }
  384. void *v_handle = NULL;
  385. #ifndef TREMOR_ONLY
  386. v_handle = dlopen(LIBVORBIS, RTLD_NOW);
  387. #endif
  388. if (!v_handle) {
  389. v_handle = dlopen(LIBTREMOR, RTLD_NOW);
  390. if (v_handle) {
  391. tremor = true;
  392. } else {
  393. dlclose(g_handle);
  394. LOG_INFO("vorbis/tremor dlerror: %s", dlerror());
  395. return false;
  396. }
  397. }
  398. g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
  399. g_handle->ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
  400. g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
  401. g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
  402. g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
  403. g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
  404. g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
  405. g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
  406. g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
  407. g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
  408. g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
  409. g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
  410. g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
  411. v_handle.ov_read = dlsym(handle, "ov_read");
  412. v_handle.ov_info = dlsym(handle, "ov_info");
  413. v_handle.ov_clear = dlsym(handle, "ov_clear");
  414. v.handle.ov_info_clear = dlsym(gv.handle, "vorbis_info_clear");
  415. v.handle.ov_comment_init = dlsym(gv.handle, "vorbis_comment_init");
  416. v.handle.ov_comment_clear = dlsym(gv.handle, "vorbis_comment_clear");
  417. v.handle.ov_block_init = dlsym(gv.handle, "vorbis_block_init");
  418. v.handle.ov_block_clear = dlsym(gv.handle, "vorbis_block_clear");
  419. v_handle.ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  420. if ((err = dlerror()) != NULL) {
  421. LOG_INFO("dlerror: %s", err);
  422. return false;
  423. }
  424. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  425. #endif
  426. return true;
  427. }
  428. struct codec *register_vorbis(void) {
  429. static struct codec ret = {
  430. 'o', // id
  431. "ogg", // types
  432. 4096, // min read
  433. 20480, // min space
  434. vorbis_open, // open
  435. vorbis_close, // close
  436. vorbis_decode,// decode
  437. };
  438. if ((v = calloc(1, sizeof(struct vorbis))) == NULL) {
  439. return NULL;
  440. }
  441. v->opened = false;
  442. if (!load_vorbis()) {
  443. return NULL;
  444. }
  445. LOG_INFO("using vorbis to decode ogg");
  446. return &ret;
  447. }