2
0

vorbis.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. LOCK_S;
  90. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  91. bytes = min(bytes, size * nmemb);
  92. memcpy(ptr, streambuf->readp, bytes);
  93. _buf_inc_readp(streambuf, bytes);
  94. UNLOCK_S;
  95. return bytes / size;
  96. }
  97. // these are needed for older versions of tremor, later versions and libvorbis allow NULL to be used
  98. static int _seek_cb(void *datasource, ogg_int64_t offset, int whence) { return -1; }
  99. static int _close_cb(void *datasource) { return 0; }
  100. static long _tell_cb(void *datasource) { return 0; }
  101. static decode_state vorbis_decode(void) {
  102. static int channels;
  103. frames_t frames;
  104. int bytes, s, n;
  105. u8_t *write_buf;
  106. LOCK_S;
  107. if (stream.state <= DISCONNECT && !_buf_used(streambuf)) {
  108. UNLOCK_S;
  109. return DECODE_COMPLETE;
  110. }
  111. UNLOCK_S;
  112. if (decode.new_stream) {
  113. ov_callbacks cbs;
  114. int err;
  115. struct vorbis_info *info;
  116. cbs.read_func = _read_cb;
  117. if (TREMOR(v)) {
  118. cbs.seek_func = _seek_cb; cbs.close_func = _close_cb; cbs.tell_func = _tell_cb;
  119. } else {
  120. cbs.seek_func = NULL; cbs.close_func = NULL; cbs.tell_func = NULL;
  121. }
  122. if ((err = OV(v, open_callbacks, streambuf, v->vf, NULL, 0, cbs)) < 0) {
  123. LOG_WARN("open_callbacks error: %d", err);
  124. return DECODE_COMPLETE;
  125. }
  126. v->opened = true;
  127. info = OV(v, info, v->vf, -1);
  128. LOG_INFO("setting track_start");
  129. LOCK_O;
  130. output.next_sample_rate = decode_newstream(info->rate, output.supported_rates);
  131. IF_DSD( output.next_fmt = PCM; )
  132. output.track_start = outputbuf->writep;
  133. if (output.fade_mode) _checkfade(true);
  134. decode.new_stream = false;
  135. UNLOCK_O;
  136. channels = info->channels;
  137. if (channels > 2) {
  138. LOG_WARN("too many channels: %d", channels);
  139. return DECODE_ERROR;
  140. }
  141. }
  142. LOCK_O_direct;
  143. IF_DIRECT(
  144. frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  145. write_buf = outputbuf->writep;
  146. );
  147. IF_PROCESS(
  148. frames = process.max_in_frames;
  149. write_buf = process.inbuf;
  150. );
  151. bytes = frames * 2 * channels; // samples returned are 16 bits
  152. // write the decoded frames into outputbuf even though they are 16 bits per sample, then unpack them
  153. #ifdef TREMOR_ONLY
  154. n = OV(v, read, v->vf, (char *)write_buf, bytes, &s);
  155. #else
  156. if (!TREMOR(v)) {
  157. #if SL_LITTLE_ENDIAN
  158. n = OV(v, read, v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
  159. #else
  160. n = OV(v, read, v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
  161. #endif
  162. #if !WIN
  163. } else {
  164. n = OV(v, read_tremor, v->vf, (char *)write_buf, bytes, &s);
  165. #endif
  166. }
  167. #endif
  168. if (n > 0) {
  169. frames_t count;
  170. s16_t *iptr;
  171. ISAMPLE_T *optr;
  172. frames = n / 2 / channels;
  173. count = frames * channels;
  174. iptr = (s16_t *)write_buf + count;
  175. optr = (ISAMPLE_T *)write_buf + frames * 2;
  176. if (channels == 2) {
  177. #if BYTES_PER_FRAME == 4
  178. memcpy(optr, iptr, count * BYTES_PER_FRAME / 2);
  179. #else
  180. while (count--) {
  181. *--optr = *--iptr << 16;
  182. }
  183. #endif
  184. } else if (channels == 1) {
  185. while (count--) {
  186. *--optr = ALIGN(*--iptr);
  187. *--optr = ALIGN(*iptr);
  188. }
  189. }
  190. IF_DIRECT(
  191. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  192. );
  193. IF_PROCESS(
  194. process.in_frames = frames;
  195. );
  196. LOG_SDEBUG("wrote %u frames", frames);
  197. } else if (n == 0) {
  198. LOG_INFO("end of stream");
  199. UNLOCK_O_direct;
  200. return DECODE_COMPLETE;
  201. } else if (n == OV_HOLE) {
  202. // recoverable hole in stream, seen when skipping
  203. LOG_DEBUG("hole in stream");
  204. } else {
  205. LOG_INFO("ov_read error: %d", n);
  206. UNLOCK_O_direct;
  207. return DECODE_COMPLETE;
  208. }
  209. UNLOCK_O_direct;
  210. return DECODE_RUNNING;
  211. }
  212. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  213. if (!v->vf) {
  214. v->vf = malloc(sizeof(OggVorbis_File) + 128); // add some padding as struct size may be larger
  215. memset(v->vf, 0, sizeof(OggVorbis_File) + 128);
  216. } else {
  217. if (v->opened) {
  218. OV(v, clear, v->vf);
  219. v->opened = false;
  220. }
  221. }
  222. }
  223. static void vorbis_close(void) {
  224. if (v->opened) {
  225. OV(v, clear, v->vf);
  226. v->opened = false;
  227. }
  228. free(v->vf);
  229. v->vf = NULL;
  230. }
  231. static bool load_vorbis() {
  232. #if !LINKALL
  233. void *handle = dlopen(LIBVORBIS, RTLD_NOW);
  234. char *err;
  235. bool tremor = false;
  236. if (!handle) {
  237. handle = dlopen(LIBTREMOR, RTLD_NOW);
  238. if (handle) {
  239. tremor = true;
  240. } else {
  241. LOG_INFO("dlerror: %s", dlerror());
  242. return false;
  243. }
  244. }
  245. v->ov_read = tremor ? NULL : dlsym(handle, "ov_read");
  246. v->ov_read_tremor = tremor ? dlsym(handle, "ov_read") : NULL;
  247. v->ov_info = dlsym(handle, "ov_info");
  248. v->ov_clear = dlsym(handle, "ov_clear");
  249. v->ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  250. if ((err = dlerror()) != NULL) {
  251. LOG_INFO("dlerror: %s", err);
  252. return false;
  253. }
  254. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  255. #endif
  256. return true;
  257. }
  258. struct codec *register_vorbis(void) {
  259. static struct codec ret = {
  260. 'o', // id
  261. "ogg", // types
  262. 2048, // min read
  263. 20480, // min space
  264. vorbis_open, // open
  265. vorbis_close, // close
  266. vorbis_decode,// decode
  267. };
  268. v = malloc(sizeof(struct vorbis));
  269. if (!v) {
  270. return NULL;
  271. }
  272. v->vf = NULL;
  273. v->opened = false;
  274. if (!load_vorbis()) {
  275. return NULL;
  276. }
  277. LOG_INFO("using vorbis to decode ogg");
  278. return &ret;
  279. }