vorbis.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #if BYTES_PER_FRAME == 4
  23. #define ALIGN(n) (n)
  24. #else
  25. #define ALIGN(n) (n << 16)
  26. #endif
  27. // automatically select between floating point (preferred) and fixed point libraries:
  28. // NOTE: works with Tremor version here: http://svn.xiph.org/trunk/Tremor, not vorbisidec.1.0.2 currently in ubuntu
  29. // we take common definations from <vorbis/vorbisfile.h> even though we can use tremor at run time
  30. // tremor's OggVorbis_File struct is normally smaller so this is ok, but padding added to malloc in case it is bigger
  31. #define OV_EXCLUDE_STATIC_CALLBACKS
  32. #ifdef TREMOR_ONLY
  33. #include <ivorbisfile.h>
  34. #else
  35. #include <vorbis/vorbisfile.h>
  36. #endif
  37. struct vorbis {
  38. OggVorbis_File *vf;
  39. bool opened;
  40. #if !LINKALL
  41. // vorbis symbols to be dynamically loaded - from either vorbisfile or vorbisidec (tremor) version of library
  42. vorbis_info *(* ov_info)(OggVorbis_File *vf, int link);
  43. int (* ov_clear)(OggVorbis_File *vf);
  44. long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
  45. long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
  46. int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
  47. #endif
  48. };
  49. static struct vorbis *v;
  50. extern log_level loglevel;
  51. extern struct buffer *streambuf;
  52. extern struct buffer *outputbuf;
  53. extern struct streamstate stream;
  54. extern struct outputstate output;
  55. extern struct decodestate decode;
  56. extern struct processstate process;
  57. #define LOCK_S mutex_lock(streambuf->mutex)
  58. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  59. #define LOCK_O mutex_lock(outputbuf->mutex)
  60. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  61. #if PROCESS
  62. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  63. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  64. #define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
  65. #define UNLOCK_O_not_direct if (!decode.direct) mutex_unlock(outputbuf->mutex)
  66. #define IF_DIRECT(x) if (decode.direct) { x }
  67. #define IF_PROCESS(x) if (!decode.direct) { x }
  68. #else
  69. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  70. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  71. #define LOCK_O_not_direct
  72. #define UNLOCK_O_not_direct
  73. #define IF_DIRECT(x) { x }
  74. #define IF_PROCESS(x)
  75. #endif
  76. #if LINKALL
  77. #define OV(h, fn, ...) (ov_ ## fn)(__VA_ARGS__)
  78. #define TREMOR(h) 0
  79. #if !WIN
  80. extern int ov_read_tremor(); // needed to enable compilation, not linked
  81. #endif
  82. #else
  83. #define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
  84. #define TREMOR(h) (h)->ov_read_tremor
  85. #endif
  86. // called with mutex locked within vorbis_decode to avoid locking O before S
  87. static size_t _read_cb(void *ptr, size_t size, size_t nmemb, void *datasource) {
  88. size_t bytes;
  89. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  90. bytes = min(bytes, size * nmemb);
  91. memcpy(ptr, streambuf->readp, bytes);
  92. _buf_inc_readp(streambuf, bytes);
  93. return bytes / size;
  94. }
  95. // these are needed for older versions of tremor, later versions and libvorbis allow NULL to be used
  96. static int _seek_cb(void *datasource, ogg_int64_t offset, int whence) { return -1; }
  97. static int _close_cb(void *datasource) { return 0; }
  98. static long _tell_cb(void *datasource) { return 0; }
  99. static decode_state vorbis_decode(void) {
  100. static int channels;
  101. bool end;
  102. frames_t frames;
  103. int bytes, s, n;
  104. u8_t *write_buf;
  105. LOCK_S;
  106. LOCK_O_direct;
  107. end = (stream.state <= DISCONNECT);
  108. IF_DIRECT(
  109. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  110. );
  111. IF_PROCESS(
  112. frames = process.max_in_frames;
  113. );
  114. if (!frames && end) {
  115. UNLOCK_O_direct;
  116. UNLOCK_S;
  117. return DECODE_COMPLETE;
  118. }
  119. if (decode.new_stream) {
  120. ov_callbacks cbs;
  121. int err;
  122. struct vorbis_info *info;
  123. cbs.read_func = _read_cb;
  124. if (TREMOR(v)) {
  125. cbs.seek_func = _seek_cb; cbs.close_func = _close_cb; cbs.tell_func = _tell_cb;
  126. } else {
  127. cbs.seek_func = NULL; cbs.close_func = NULL; cbs.tell_func = NULL;
  128. }
  129. if ((err = OV(v, open_callbacks, streambuf, v->vf, NULL, 0, cbs)) < 0) {
  130. LOG_WARN("open_callbacks error: %d", err);
  131. UNLOCK_O_direct;
  132. UNLOCK_S;
  133. return DECODE_COMPLETE;
  134. }
  135. v->opened = true;
  136. info = OV(v, info, v->vf, -1);
  137. LOG_INFO("setting track_start");
  138. LOCK_O_not_direct;
  139. output.next_sample_rate = decode_newstream(info->rate, output.supported_rates);
  140. IF_DSD( output.next_fmt = PCM; )
  141. output.track_start = outputbuf->writep;
  142. if (output.fade_mode) _checkfade(true);
  143. decode.new_stream = false;
  144. UNLOCK_O_not_direct;
  145. IF_PROCESS(
  146. frames = process.max_in_frames;
  147. );
  148. channels = info->channels;
  149. if (channels > 2) {
  150. LOG_WARN("too many channels: %d", channels);
  151. UNLOCK_O_direct;
  152. UNLOCK_S;
  153. return DECODE_ERROR;
  154. }
  155. }
  156. bytes = frames * 2 * channels; // samples returned are 16 bits
  157. IF_DIRECT(
  158. write_buf = outputbuf->writep;
  159. );
  160. IF_PROCESS(
  161. write_buf = process.inbuf;
  162. );
  163. // write the decoded frames into outputbuf even though they are 16 bits per sample, then unpack them
  164. #ifdef TREMOR_ONLY
  165. n = OV(v, read, v->vf, (char *)write_buf, bytes, &s);
  166. #else
  167. if (!TREMOR(v)) {
  168. #if SL_LITTLE_ENDIAN
  169. n = OV(v, read, v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
  170. #else
  171. n = OV(v, read, v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
  172. #endif
  173. #if !WIN
  174. } else {
  175. n = OV(v, read_tremor, v->vf, (char *)write_buf, bytes, &s);
  176. #endif
  177. }
  178. #endif
  179. if (n > 0) {
  180. frames_t count;
  181. s16_t *iptr;
  182. ISAMPLE_T *optr;
  183. frames = n / 2 / channels;
  184. count = frames * channels;
  185. iptr = (s16_t *)write_buf + count;
  186. optr = (ISAMPLE_T *)write_buf + frames * 2;
  187. if (channels == 2) {
  188. #if BYTES_PER_FRAME == 4
  189. memcpy(optr, iptr, count * BYTES_PER_FRAME / 2);
  190. #else
  191. while (count--) {
  192. *--optr = *--iptr << 16;
  193. }
  194. #endif
  195. } else if (channels == 1) {
  196. while (count--) {
  197. *--optr = ALIGN(*--iptr);
  198. *--optr = ALIGN(*iptr);
  199. }
  200. }
  201. IF_DIRECT(
  202. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  203. );
  204. IF_PROCESS(
  205. process.in_frames = frames;
  206. );
  207. LOG_SDEBUG("wrote %u frames", frames);
  208. } else if (n == 0) {
  209. LOG_INFO("end of stream");
  210. UNLOCK_O_direct;
  211. UNLOCK_S;
  212. return DECODE_COMPLETE;
  213. } else if (n == OV_HOLE) {
  214. // recoverable hole in stream, seen when skipping
  215. LOG_DEBUG("hole in stream");
  216. } else {
  217. LOG_INFO("ov_read error: %d", n);
  218. UNLOCK_O_direct;
  219. UNLOCK_S;
  220. return DECODE_COMPLETE;
  221. }
  222. UNLOCK_O_direct;
  223. UNLOCK_S;
  224. return DECODE_RUNNING;
  225. }
  226. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  227. if (!v->vf) {
  228. v->vf = malloc(sizeof(OggVorbis_File) + 128); // add some padding as struct size may be larger
  229. memset(v->vf, 0, sizeof(OggVorbis_File) + 128);
  230. } else {
  231. if (v->opened) {
  232. OV(v, clear, v->vf);
  233. v->opened = false;
  234. }
  235. }
  236. }
  237. static void vorbis_close(void) {
  238. if (v->opened) {
  239. OV(v, clear, v->vf);
  240. v->opened = false;
  241. }
  242. free(v->vf);
  243. v->vf = NULL;
  244. }
  245. static bool load_vorbis() {
  246. #if !LINKALL
  247. void *handle = dlopen(LIBVORBIS, RTLD_NOW);
  248. char *err;
  249. bool tremor = false;
  250. if (!handle) {
  251. handle = dlopen(LIBTREMOR, RTLD_NOW);
  252. if (handle) {
  253. tremor = true;
  254. } else {
  255. LOG_INFO("dlerror: %s", dlerror());
  256. return false;
  257. }
  258. }
  259. v->ov_read = tremor ? NULL : dlsym(handle, "ov_read");
  260. v->ov_read_tremor = tremor ? dlsym(handle, "ov_read") : NULL;
  261. v->ov_info = dlsym(handle, "ov_info");
  262. v->ov_clear = dlsym(handle, "ov_clear");
  263. v->ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  264. if ((err = dlerror()) != NULL) {
  265. LOG_INFO("dlerror: %s", err);
  266. return false;
  267. }
  268. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  269. #endif
  270. return true;
  271. }
  272. struct codec *register_vorbis(void) {
  273. static struct codec ret = {
  274. 'o', // id
  275. "ogg", // types
  276. 2048, // min read
  277. 20480, // min space
  278. vorbis_open, // open
  279. vorbis_close, // close
  280. vorbis_decode,// decode
  281. };
  282. v = malloc(sizeof(struct vorbis));
  283. if (!v) {
  284. return NULL;
  285. }
  286. v->vf = NULL;
  287. v->opened = false;
  288. if (!load_vorbis()) {
  289. return NULL;
  290. }
  291. LOG_INFO("using vorbis to decode ogg");
  292. return &ret;
  293. }