vorbis.c 9.8 KB

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