vorbis.c 10 KB

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