vorbis.c 8.6 KB

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