pcm.c 13 KB

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