vorbis.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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;
  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 && !_buf_used(streambuf)) {
  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. // write the decoded frames into outputbuf even though they are 16 bits per sample, then unpack them
  175. #ifdef TREMOR_ONLY
  176. n = OV(v, read, v->vf, (char *)write_buf, bytes, &s);
  177. #else
  178. if (!TREMOR(v)) {
  179. #if SL_LITTLE_ENDIAN
  180. n = OV(v, read, v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
  181. #else
  182. n = OV(v, read, v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
  183. #endif
  184. #if !WIN
  185. } else {
  186. n = OV(v, read_tremor, v->vf, (char *)write_buf, bytes, &s);
  187. #endif
  188. }
  189. #endif
  190. #if FRAME_BUF
  191. LOCK_O_direct;
  192. #endif
  193. if (n > 0) {
  194. frames_t count;
  195. s16_t *iptr;
  196. ISAMPLE_T *optr;
  197. frames = n / 2 / channels;
  198. count = frames * channels;
  199. // work backward to unpack samples (if needed)
  200. iptr = (s16_t *) write_buf + count;
  201. IF_DIRECT(
  202. optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
  203. )
  204. IF_PROCESS(
  205. optr = (ISAMPLE_T *) write_buf + frames * 2;
  206. )
  207. if (channels == 2) {
  208. #if BYTES_PER_FRAME == 4
  209. #if FRAME_BUF
  210. // copy needed only when DIRECT and FRAME_BUF
  211. IF_DIRECT(
  212. memcpy(outputbuf->writep, write_buf, frames * BYTES_PER_FRAME);
  213. )
  214. #endif
  215. #else
  216. while (count--) {
  217. *--optr = ALIGN(*--iptr);
  218. }
  219. #endif
  220. } else if (channels == 1) {
  221. while (count--) {
  222. *--optr = ALIGN(*--iptr);
  223. *--optr = ALIGN(*iptr);
  224. }
  225. }
  226. IF_DIRECT(
  227. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  228. );
  229. IF_PROCESS(
  230. process.in_frames = frames;
  231. );
  232. LOG_SDEBUG("wrote %u frames", frames);
  233. } else if (n == 0) {
  234. if (stream.state <= DISCONNECT) {
  235. LOG_INFO("partial decode");
  236. UNLOCK_O_direct;
  237. return DECODE_COMPLETE;
  238. } else {
  239. LOG_INFO("no frame decoded");
  240. }
  241. } else if (n == OV_HOLE) {
  242. // recoverable hole in stream, seen when skipping
  243. LOG_DEBUG("hole in stream");
  244. } else {
  245. LOG_INFO("ov_read error: %d", n);
  246. UNLOCK_O_direct;
  247. return DECODE_COMPLETE;
  248. }
  249. UNLOCK_O_direct;
  250. return DECODE_RUNNING;
  251. }
  252. static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  253. if (!v->vf) {
  254. v->vf = malloc(sizeof(OggVorbis_File) + 128); // add some padding as struct size may be larger
  255. memset(v->vf, 0, sizeof(OggVorbis_File) + 128);
  256. #if FRAME_BUF
  257. v->write_buf = malloc(FRAME_BUF * BYTES_PER_FRAME);
  258. #endif
  259. } else {
  260. if (v->opened) {
  261. OV(v, clear, v->vf);
  262. v->opened = false;
  263. }
  264. }
  265. }
  266. static void vorbis_close(void) {
  267. if (v->opened) {
  268. OV(v, clear, v->vf);
  269. v->opened = false;
  270. }
  271. free(v->vf);
  272. #if FRAME_BUF
  273. free(v->write_buf);
  274. v->write_buf = NULL;
  275. #endif
  276. v->vf = NULL;
  277. }
  278. static bool load_vorbis() {
  279. #if !LINKALL
  280. void *handle = dlopen(LIBVORBIS, RTLD_NOW);
  281. char *err;
  282. bool tremor = false;
  283. if (!handle) {
  284. handle = dlopen(LIBTREMOR, RTLD_NOW);
  285. if (handle) {
  286. tremor = true;
  287. } else {
  288. LOG_INFO("dlerror: %s", dlerror());
  289. return false;
  290. }
  291. }
  292. v->ov_read = tremor ? NULL : dlsym(handle, "ov_read");
  293. v->ov_read_tremor = tremor ? dlsym(handle, "ov_read") : NULL;
  294. v->ov_info = dlsym(handle, "ov_info");
  295. v->ov_clear = dlsym(handle, "ov_clear");
  296. v->ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
  297. if ((err = dlerror()) != NULL) {
  298. LOG_INFO("dlerror: %s", err);
  299. return false;
  300. }
  301. LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
  302. #endif
  303. return true;
  304. }
  305. struct codec *register_vorbis(void) {
  306. static struct codec ret = {
  307. 'o', // id
  308. "ogg", // types
  309. 4096, // min read
  310. 20480, // min space
  311. vorbis_open, // open
  312. vorbis_close, // close
  313. vorbis_decode,// decode
  314. };
  315. v = malloc(sizeof(struct vorbis));
  316. if (!v) {
  317. return NULL;
  318. }
  319. v->vf = NULL;
  320. v->opened = false;
  321. if (!load_vorbis()) {
  322. return NULL;
  323. }
  324. LOG_INFO("using vorbis to decode ogg");
  325. return &ret;
  326. }