vorbis.c 8.6 KB

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