pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. extern log_level loglevel;
  23. extern struct buffer *streambuf;
  24. extern struct buffer *outputbuf;
  25. extern struct streamstate stream;
  26. extern struct outputstate output;
  27. extern struct decodestate decode;
  28. extern struct processstate process;
  29. bool pcm_check_header = false;
  30. #define LOCK_S mutex_lock(streambuf->mutex)
  31. #define UNLOCK_S mutex_unlock(streambuf->mutex)
  32. #define LOCK_O mutex_lock(outputbuf->mutex)
  33. #define UNLOCK_O mutex_unlock(outputbuf->mutex)
  34. #if PROCESS
  35. #define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
  36. #define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
  37. #define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
  38. #define UNLOCK_O_not_direct if (!decode.direct) mutex_unlock(outputbuf->mutex)
  39. #define IF_DIRECT(x) if (decode.direct) { x }
  40. #define IF_PROCESS(x) if (!decode.direct) { x }
  41. #else
  42. #define LOCK_O_direct mutex_lock(outputbuf->mutex)
  43. #define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
  44. #define LOCK_O_not_direct
  45. #define UNLOCK_O_not_direct
  46. #define IF_DIRECT(x) { x }
  47. #define IF_PROCESS(x)
  48. #endif
  49. #define MAX_DECODE_FRAMES 4096
  50. static u32_t sample_rates[] = {
  51. 11025, 22050, 32000, 44100, 48000, 8000, 12000, 16000, 24000, 96000, 88200, 176400, 192000, 352800, 384000, 705600, 768000
  52. };
  53. static u32_t sample_rate;
  54. static u32_t sample_size;
  55. static u32_t channels;
  56. static bool bigendian;
  57. static bool limit;
  58. static u32_t audio_left;
  59. static u32_t bytes_per_frame;
  60. typedef enum { UNKNOWN = 0, WAVE, AIFF } header_format;
  61. static void _check_header(void) {
  62. u8_t *ptr = streambuf->readp;
  63. unsigned bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  64. header_format format = UNKNOWN;
  65. // simple parsing of wav and aiff headers and get to samples
  66. if (bytes > 12) {
  67. if (!memcmp(ptr, "RIFF", 4) && !memcmp(ptr+8, "WAVE", 4)) {
  68. LOG_INFO("WAVE");
  69. format = WAVE;
  70. } else if (!memcmp(ptr, "FORM", 4) && (!memcmp(ptr+8, "AIFF", 4) || !memcmp(ptr+8, "AIFC", 4))) {
  71. LOG_INFO("AIFF");
  72. format = AIFF;
  73. }
  74. }
  75. if (format != UNKNOWN) {
  76. ptr += 12;
  77. bytes -= 12;
  78. while (bytes >= 8) {
  79. char id[5];
  80. unsigned len;
  81. memcpy(id, ptr, 4);
  82. id[4] = '\0';
  83. if (format == WAVE) {
  84. len = *(ptr+4) | *(ptr+5) << 8 | *(ptr+6) << 16| *(ptr+7) << 24;
  85. } else {
  86. len = *(ptr+4) << 24 | *(ptr+5) << 16 | *(ptr+6) << 8 | *(ptr+7);
  87. }
  88. LOG_INFO("header: %s len: %d", id, len);
  89. if (format == WAVE && !memcmp(ptr, "data", 4)) {
  90. ptr += 8;
  91. _buf_inc_readp(streambuf, ptr - streambuf->readp);
  92. audio_left = len;
  93. if ((audio_left == 0xFFFFFFFF) || (audio_left == 0x7FFFEFFC)) {
  94. LOG_INFO("wav audio size unknown: %u", audio_left);
  95. limit = false;
  96. } else {
  97. LOG_INFO("wav audio size: %u", audio_left);
  98. limit = true;
  99. }
  100. return;
  101. }
  102. if (format == AIFF && !memcmp(ptr, "SSND", 4) && bytes >= 16) {
  103. unsigned offset = *(ptr+8) << 24 | *(ptr+9) << 16 | *(ptr+10) << 8 | *(ptr+11);
  104. // following 4 bytes is blocksize - ignored
  105. ptr += 8 + 8;
  106. _buf_inc_readp(streambuf, ptr + offset - streambuf->readp);
  107. // Reading from an upsampled stream, length could be wrong.
  108. // Only use length in header for files.
  109. if (stream.state == STREAMING_FILE) {
  110. audio_left = len - 8 - offset;
  111. LOG_INFO("aif audio size: %u", audio_left);
  112. limit = true;
  113. }
  114. return;
  115. }
  116. if (format == WAVE && !memcmp(ptr, "fmt ", 4) && bytes >= 24) {
  117. // override the server parsed values with our own
  118. channels = *(ptr+10) | *(ptr+11) << 8;
  119. sample_rate = *(ptr+12) | *(ptr+13) << 8 | *(ptr+14) << 16 | *(ptr+15) << 24;
  120. sample_size = (*(ptr+22) | *(ptr+23) << 8) / 8;
  121. bigendian = 0;
  122. LOG_INFO("pcm size: %u rate: %u chan: %u bigendian: %u", sample_size, sample_rate, channels, bigendian);
  123. }
  124. if (format == AIFF && !memcmp(ptr, "COMM", 4) && bytes >= 26) {
  125. int exponent;
  126. // override the server parsed values with our own
  127. channels = *(ptr+8) << 8 | *(ptr+9);
  128. sample_size = (*(ptr+14) << 8 | *(ptr+15)) / 8;
  129. bigendian = 1;
  130. // sample rate is encoded as IEEE 80 bit extended format
  131. // make some assumptions to simplify processing - only use first 32 bits of mantissa
  132. exponent = ((*(ptr+16) & 0x7f) << 8 | *(ptr+17)) - 16383 - 31;
  133. sample_rate = *(ptr+18) << 24 | *(ptr+19) << 16 | *(ptr+20) << 8 | *(ptr+21);
  134. while (exponent < 0) { sample_rate >>= 1; ++exponent; }
  135. while (exponent > 0) { sample_rate <<= 1; --exponent; }
  136. LOG_INFO("pcm size: %u rate: %u chan: %u bigendian: %u", sample_size, sample_rate, channels, bigendian);
  137. }
  138. if (bytes >= len + 8) {
  139. ptr += len + 8;
  140. bytes -= (len + 8);
  141. } else {
  142. LOG_WARN("run out of data");
  143. return;
  144. }
  145. }
  146. } else {
  147. LOG_WARN("unknown format - can't parse header");
  148. }
  149. }
  150. static decode_state pcm_decode(void) {
  151. unsigned bytes, in, out;
  152. frames_t frames, count;
  153. u32_t *optr;
  154. u8_t *iptr;
  155. u8_t tmp[16];
  156. LOCK_S;
  157. if ( decode.new_stream && ( ( stream.state == STREAMING_FILE ) || pcm_check_header ) ) {
  158. _check_header();
  159. }
  160. LOCK_O_direct;
  161. bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
  162. IF_DIRECT(
  163. out = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
  164. );
  165. IF_PROCESS(
  166. out = process.max_in_frames;
  167. );
  168. if ((stream.state <= DISCONNECT && bytes == 0) || (limit && audio_left == 0)) {
  169. UNLOCK_O_direct;
  170. UNLOCK_S;
  171. return DECODE_COMPLETE;
  172. }
  173. if (decode.new_stream) {
  174. LOG_INFO("setting track_start");
  175. LOCK_O_not_direct;
  176. output.track_start = outputbuf->writep;
  177. decode.new_stream = false;
  178. #if DSD
  179. if (sample_size == 3 &&
  180. is_stream_dop(((u8_t *)streambuf->readp) + (bigendian?0:2),
  181. ((u8_t *)streambuf->readp) + (bigendian?0:2) + sample_size,
  182. sample_size * channels, bytes / (sample_size * channels))) {
  183. LOG_INFO("file contains DOP");
  184. if (output.dsdfmt == DOP_S24_LE || output.dsdfmt == DOP_S24_3LE)
  185. output.next_fmt = output.dsdfmt;
  186. else
  187. output.next_fmt = DOP;
  188. output.next_sample_rate = sample_rate;
  189. output.fade = FADE_INACTIVE;
  190. } else {
  191. output.next_sample_rate = decode_newstream(sample_rate, output.supported_rates);
  192. output.next_fmt = PCM;
  193. if (output.fade_mode) _checkfade(true);
  194. }
  195. #else
  196. output.next_sample_rate = decode_newstream(sample_rate, output.supported_rates);
  197. if (output.fade_mode) _checkfade(true);
  198. #endif
  199. UNLOCK_O_not_direct;
  200. IF_PROCESS(
  201. out = process.max_in_frames;
  202. );
  203. bytes_per_frame = channels * sample_size;
  204. }
  205. IF_DIRECT(
  206. optr = (u32_t *)outputbuf->writep;
  207. );
  208. IF_PROCESS(
  209. optr = (u32_t *)process.inbuf;
  210. );
  211. iptr = (u8_t *)streambuf->readp;
  212. in = bytes / bytes_per_frame;
  213. // handle frame wrapping round end of streambuf
  214. // - only need if resizing of streambuf does not avoid this, could occur in localfile case
  215. if (in == 0 && bytes > 0 && _buf_used(streambuf) >= bytes_per_frame) {
  216. memcpy(tmp, iptr, bytes);
  217. memcpy(tmp + bytes, streambuf->buf, bytes_per_frame - bytes);
  218. iptr = tmp;
  219. in = 1;
  220. }
  221. frames = min(in, out);
  222. frames = min(frames, MAX_DECODE_FRAMES);
  223. if (limit && frames * bytes_per_frame > audio_left) {
  224. LOG_INFO("reached end of audio");
  225. frames = audio_left / bytes_per_frame;
  226. }
  227. count = frames * channels;
  228. if (channels == 2) {
  229. if (sample_size == 1) {
  230. while (count--) {
  231. *optr++ = *iptr++ << 24;
  232. }
  233. } else if (sample_size == 2) {
  234. if (bigendian) {
  235. while (count--) {
  236. *optr++ = *(iptr) << 24 | *(iptr+1) << 16;
  237. iptr += 2;
  238. }
  239. } else {
  240. while (count--) {
  241. *optr++ = *(iptr) << 16 | *(iptr+1) << 24;
  242. iptr += 2;
  243. }
  244. }
  245. } else if (sample_size == 3) {
  246. if (bigendian) {
  247. while (count--) {
  248. *optr++ = *(iptr) << 24 | *(iptr+1) << 16 | *(iptr+2) << 8;
  249. iptr += 3;
  250. }
  251. } else {
  252. while (count--) {
  253. *optr++ = *(iptr) << 8 | *(iptr+1) << 16 | *(iptr+2) << 24;
  254. iptr += 3;
  255. }
  256. }
  257. } else if (sample_size == 4) {
  258. if (bigendian) {
  259. while (count--) {
  260. *optr++ = *(iptr) << 24 | *(iptr+1) << 16 | *(iptr+2) << 8 | *(iptr+3);
  261. iptr += 4;
  262. }
  263. } else {
  264. while (count--) {
  265. *optr++ = *(iptr) | *(iptr+1) << 8 | *(iptr+2) << 16 | *(iptr+3) << 24;
  266. iptr += 4;
  267. }
  268. }
  269. }
  270. } else if (channels == 1) {
  271. if (sample_size == 1) {
  272. while (count--) {
  273. *optr = *iptr++ << 24;
  274. *(optr+1) = *optr;
  275. optr += 2;
  276. }
  277. } else if (sample_size == 2) {
  278. if (bigendian) {
  279. while (count--) {
  280. *optr = *(iptr) << 24 | *(iptr+1) << 16;
  281. *(optr+1) = *optr;
  282. iptr += 2;
  283. optr += 2;
  284. }
  285. } else {
  286. while (count--) {
  287. *optr = *(iptr) << 16 | *(iptr+1) << 24;
  288. *(optr+1) = *optr;
  289. iptr += 2;
  290. optr += 2;
  291. }
  292. }
  293. } else if (sample_size == 3) {
  294. if (bigendian) {
  295. while (count--) {
  296. *optr = *(iptr) << 24 | *(iptr+1) << 16 | *(iptr+2) << 8;
  297. *(optr+1) = *optr;
  298. iptr += 3;
  299. optr += 2;
  300. }
  301. } else {
  302. while (count--) {
  303. *optr = *(iptr) << 8 | *(iptr+1) << 16 | *(iptr+2) << 24;
  304. *(optr+1) = *optr;
  305. iptr += 3;
  306. optr += 2;
  307. }
  308. }
  309. } else if (sample_size == 4) {
  310. if (bigendian) {
  311. while (count--) {
  312. *optr++ = *(iptr) << 24 | *(iptr+1) << 16 | *(iptr+2) << 8 | *(iptr+3);
  313. *(optr+1) = *optr;
  314. iptr += 4;
  315. optr += 2;
  316. }
  317. } else {
  318. while (count--) {
  319. *optr++ = *(iptr) | *(iptr+1) << 8 | *(iptr+2) << 16 | *(iptr+3) << 24;
  320. *(optr+1) = *optr;
  321. iptr += 4;
  322. optr += 2;
  323. }
  324. }
  325. }
  326. } else {
  327. LOG_ERROR("unsupported channels");
  328. }
  329. LOG_SDEBUG("decoded %u frames", frames);
  330. _buf_inc_readp(streambuf, frames * bytes_per_frame);
  331. if (limit) {
  332. audio_left -= frames * bytes_per_frame;
  333. }
  334. IF_DIRECT(
  335. _buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
  336. );
  337. IF_PROCESS(
  338. process.in_frames = frames;
  339. );
  340. UNLOCK_O_direct;
  341. UNLOCK_S;
  342. return DECODE_RUNNING;
  343. }
  344. static void pcm_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
  345. sample_size = size - '0' + 1;
  346. sample_rate = sample_rates[rate - '0'];
  347. channels = chan - '0';
  348. bigendian = (endianness == '0');
  349. limit = false;
  350. LOG_INFO("pcm size: %u rate: %u chan: %u bigendian: %u", sample_size, sample_rate, channels, bigendian);
  351. buf_adjust(streambuf, sample_size * channels);
  352. }
  353. static void pcm_close(void) {
  354. buf_adjust(streambuf, 1);
  355. }
  356. struct codec *register_pcm(void) {
  357. if ( pcm_check_header )
  358. {
  359. static struct codec ret = {
  360. 'p', // id
  361. "wav,aif,pcm", // types
  362. 4096, // min read
  363. 102400, // min space
  364. pcm_open, // open
  365. pcm_close, // close
  366. pcm_decode, // decode
  367. };
  368. LOG_INFO("using pcm to decode wav,aif,pcm");
  369. return &ret;
  370. }
  371. else
  372. {
  373. static struct codec ret = {
  374. 'p', // id
  375. "aif,pcm", // types
  376. 4096, // min read
  377. 102400, // min space
  378. pcm_open, // open
  379. pcm_close, // close
  380. pcm_decode, // decode
  381. };
  382. LOG_INFO("using pcm to decode aif,pcm");
  383. return &ret;
  384. }
  385. return NULL;
  386. }