SPDIFAudioSink.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "SPDIFAudioSink.h"
  2. #include "driver/i2s.h"
  3. // See http://www.hardwarebook.info/S/PDIF for more info on this protocol
  4. // Conversion table to biphase code mark (LSB first, ending in 1)
  5. static const uint16_t bmc_convert[256] = {
  6. 0x3333, 0xb333, 0xd333, 0x5333, 0xcb33, 0x4b33, 0x2b33, 0xab33, 0xcd33,
  7. 0x4d33, 0x2d33, 0xad33, 0x3533, 0xb533, 0xd533, 0x5533, 0xccb3, 0x4cb3,
  8. 0x2cb3, 0xacb3, 0x34b3, 0xb4b3, 0xd4b3, 0x54b3, 0x32b3, 0xb2b3, 0xd2b3,
  9. 0x52b3, 0xcab3, 0x4ab3, 0x2ab3, 0xaab3, 0xccd3, 0x4cd3, 0x2cd3, 0xacd3,
  10. 0x34d3, 0xb4d3, 0xd4d3, 0x54d3, 0x32d3, 0xb2d3, 0xd2d3, 0x52d3, 0xcad3,
  11. 0x4ad3, 0x2ad3, 0xaad3, 0x3353, 0xb353, 0xd353, 0x5353, 0xcb53, 0x4b53,
  12. 0x2b53, 0xab53, 0xcd53, 0x4d53, 0x2d53, 0xad53, 0x3553, 0xb553, 0xd553,
  13. 0x5553, 0xcccb, 0x4ccb, 0x2ccb, 0xaccb, 0x34cb, 0xb4cb, 0xd4cb, 0x54cb,
  14. 0x32cb, 0xb2cb, 0xd2cb, 0x52cb, 0xcacb, 0x4acb, 0x2acb, 0xaacb, 0x334b,
  15. 0xb34b, 0xd34b, 0x534b, 0xcb4b, 0x4b4b, 0x2b4b, 0xab4b, 0xcd4b, 0x4d4b,
  16. 0x2d4b, 0xad4b, 0x354b, 0xb54b, 0xd54b, 0x554b, 0x332b, 0xb32b, 0xd32b,
  17. 0x532b, 0xcb2b, 0x4b2b, 0x2b2b, 0xab2b, 0xcd2b, 0x4d2b, 0x2d2b, 0xad2b,
  18. 0x352b, 0xb52b, 0xd52b, 0x552b, 0xccab, 0x4cab, 0x2cab, 0xacab, 0x34ab,
  19. 0xb4ab, 0xd4ab, 0x54ab, 0x32ab, 0xb2ab, 0xd2ab, 0x52ab, 0xcaab, 0x4aab,
  20. 0x2aab, 0xaaab, 0xcccd, 0x4ccd, 0x2ccd, 0xaccd, 0x34cd, 0xb4cd, 0xd4cd,
  21. 0x54cd, 0x32cd, 0xb2cd, 0xd2cd, 0x52cd, 0xcacd, 0x4acd, 0x2acd, 0xaacd,
  22. 0x334d, 0xb34d, 0xd34d, 0x534d, 0xcb4d, 0x4b4d, 0x2b4d, 0xab4d, 0xcd4d,
  23. 0x4d4d, 0x2d4d, 0xad4d, 0x354d, 0xb54d, 0xd54d, 0x554d, 0x332d, 0xb32d,
  24. 0xd32d, 0x532d, 0xcb2d, 0x4b2d, 0x2b2d, 0xab2d, 0xcd2d, 0x4d2d, 0x2d2d,
  25. 0xad2d, 0x352d, 0xb52d, 0xd52d, 0x552d, 0xccad, 0x4cad, 0x2cad, 0xacad,
  26. 0x34ad, 0xb4ad, 0xd4ad, 0x54ad, 0x32ad, 0xb2ad, 0xd2ad, 0x52ad, 0xcaad,
  27. 0x4aad, 0x2aad, 0xaaad, 0x3335, 0xb335, 0xd335, 0x5335, 0xcb35, 0x4b35,
  28. 0x2b35, 0xab35, 0xcd35, 0x4d35, 0x2d35, 0xad35, 0x3535, 0xb535, 0xd535,
  29. 0x5535, 0xccb5, 0x4cb5, 0x2cb5, 0xacb5, 0x34b5, 0xb4b5, 0xd4b5, 0x54b5,
  30. 0x32b5, 0xb2b5, 0xd2b5, 0x52b5, 0xcab5, 0x4ab5, 0x2ab5, 0xaab5, 0xccd5,
  31. 0x4cd5, 0x2cd5, 0xacd5, 0x34d5, 0xb4d5, 0xd4d5, 0x54d5, 0x32d5, 0xb2d5,
  32. 0xd2d5, 0x52d5, 0xcad5, 0x4ad5, 0x2ad5, 0xaad5, 0x3355, 0xb355, 0xd355,
  33. 0x5355, 0xcb55, 0x4b55, 0x2b55, 0xab55, 0xcd55, 0x4d55, 0x2d55, 0xad55,
  34. 0x3555, 0xb555, 0xd555, 0x5555,
  35. };
  36. #define I2S_BUG_MAGIC (26 * 1000 * 1000) // magic number for avoiding I2S bug
  37. #define BITS_PER_SUBFRAME 64
  38. #define FRAMES_PER_BLOCK 192
  39. #define SPDIF_BUF_SIZE (BITS_PER_SUBFRAME / 8 * 2 * FRAMES_PER_BLOCK)
  40. #define SPDIF_BUF_ARRAY_SIZE (SPDIF_BUF_SIZE / sizeof(uint32_t))
  41. #define BMC_B 0x33173333 // block start
  42. #define BMC_M 0x331d3333 // left ch
  43. #define BMC_W 0x331b3333 // right ch
  44. #define BMC_MW_DIF (BMC_M ^ BMC_W)
  45. static uint32_t spdif_buf[SPDIF_BUF_ARRAY_SIZE];
  46. static uint32_t* spdif_ptr;
  47. static void spdif_buf_init(void) {
  48. // first bllock has W preamble
  49. spdif_buf[0] = BMC_B;
  50. // all other blocks are alternating M, then W preamble
  51. uint32_t bmc_mw = BMC_M;
  52. for (int i = 2; i < SPDIF_BUF_ARRAY_SIZE; i += 2) {
  53. spdif_buf[i] = bmc_mw ^= BMC_MW_DIF;
  54. }
  55. }
  56. SPDIFAudioSink::SPDIFAudioSink(uint8_t spdifPin) {
  57. // initialize S/PDIF buffer
  58. spdif_buf_init();
  59. spdif_ptr = spdif_buf;
  60. this->spdifPin = spdifPin;
  61. this->setParams(44100, 16, 2);
  62. startI2sFeed(SPDIF_BUF_SIZE * 16);
  63. }
  64. bool SPDIFAudioSink::setParams(uint32_t sampleRate, uint8_t channelCount,
  65. uint8_t bitDepth) {
  66. if (bitDepth != 16 ||
  67. channelCount != 2) // TODO support mono playback and different bit widths
  68. return false;
  69. int sample_rate = (int)sampleRate * 2;
  70. int bclk = sample_rate * 64 * 2;
  71. int mclk = (I2S_BUG_MAGIC / bclk) * bclk;
  72. i2s_config_t i2s_config = {
  73. .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
  74. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
  75. .sample_rate = (uint32_t)sample_rate,
  76. #else
  77. .sample_rate = (int)sample_rate,
  78. #endif
  79. .bits_per_sample = (i2s_bits_per_sample_t)(bitDepth * 2),
  80. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  81. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  82. .intr_alloc_flags = 0,
  83. .dma_buf_count = 8,
  84. .dma_buf_len = 512,
  85. .use_apll = true,
  86. .tx_desc_auto_clear = true,
  87. .fixed_mclk = mclk, // avoiding I2S bug
  88. };
  89. i2s_pin_config_t pin_config = {
  90. .bck_io_num = -1,
  91. .ws_io_num = -1,
  92. .data_out_num = spdifPin,
  93. .data_in_num = -1,
  94. };
  95. i2s_driver_uninstall((i2s_port_t)0);
  96. int err = i2s_driver_install((i2s_port_t)0, &i2s_config, 0, nullptr);
  97. i2s_set_pin((i2s_port_t)0, &pin_config);
  98. return !err;
  99. }
  100. SPDIFAudioSink::~SPDIFAudioSink() {
  101. i2s_driver_uninstall((i2s_port_t)0);
  102. }
  103. int num_frames = 0;
  104. void SPDIFAudioSink::feedPCMFrames(const uint8_t* buffer, size_t bytes) {
  105. for (int i = 0; i < bytes; i += 2) {
  106. /**
  107. * What is this, and why does it work?
  108. *
  109. * Rather than assemble all S/PDIF frames from scratch we want to do the
  110. * minimum amount of work possible. To that extent, we fix the final four
  111. * bits (VUCP) to be all-zero prior to BMC encoding (= valid, no subcode
  112. * or channel-status bits set, even parity), and zero the lowest 8 sample
  113. * bits (prior to BMC encoding). This is all done in spdif_buf_init(),
  114. * aligning at word boundaries and setting alternating preambles as well
  115. * as encoding 8 bits of zeros as 0x33, leaving the final bit high.
  116. *
  117. * We must therefore BMC encode our 16 bit PCM data in such a way that:
  118. * - the first (least significant) bit is 0 (to fit with 0x33 zeros)
  119. * - the final bit is 1 (so as to fit with the following 0x33 VUCP bits)
  120. * - the result has even parity
  121. *
  122. * As biphase mark code retains parity (0 encodes as two 1s or two 0s),
  123. * this is evidently not possible without loss of data, as the input PCM
  124. * data isn't already even parity. We can use the first (least significant)
  125. * bit as parity bit to achieve our desired encoding.
  126. *
  127. * The bmc_convert table converts the lower and upper 8 bit of our PCM
  128. * frames into 16 bit biphase mark code patterns with the first two bits
  129. * encoding the LSB and the final bit always high. We combine both 16bit
  130. * patterns into a 32 bit encoding of our original input data by shifting
  131. * the first (lower) 16 bit into position, then sign-extending the second
  132. * (higher) 16bit pattern. If that pattern started with a 1, the resulting
  133. * 32 bit pattern will now contain 1s in the first 16 bits.
  134. *
  135. * Keep in mind that the shifted value in the first (lower) 16 bits always
  136. * ends in a 1 bit, so the entire pattern must be flipped in case the
  137. * second (higher) 16 bit pattern starts with a 1 bit. XORing the sign-
  138. * extended component to the first one achieves exactly that.
  139. *
  140. * Finally, we zero out the very first bit of the resulting value. This
  141. * may change the lowest bit of our encoded value, but ensures that our
  142. * newly encoded bits form a valid BMC pattern with the already zeroed out
  143. * lower 8 bits in the pattern set up in spdif_buf_init().
  144. *
  145. * Further, this also happens to ensure even parity:
  146. * All entries in the BMC table end in a 1, so an all-zero pattern would
  147. * end (after encoding an even number of bits) in two 0 bits. Setting any
  148. * bit will cause the BMC-encoded pattern to flip its first (lowest) bit,
  149. * meaning we can use that bit to infer parity. Setting it to zero flips
  150. * the first (lowest) bit such that we always have even parity.
  151. *
  152. * I did not come up with this, all credit goes to
  153. * github.com/amedes/esp_a2dp_sink_spdif
  154. */
  155. uint32_t lo = ((uint32_t)(bmc_convert[buffer[i]]) << 16);
  156. uint32_t hi = (uint32_t)((int16_t)bmc_convert[buffer[i + 1]]);
  157. *(spdif_ptr + 1) = ((lo ^ hi) << 1) >> 1;
  158. spdif_ptr += 2; // advance to next audio data
  159. if (spdif_ptr >= &spdif_buf[SPDIF_BUF_ARRAY_SIZE]) {
  160. feedPCMFramesInternal(spdif_buf, sizeof(spdif_buf));
  161. spdif_ptr = spdif_buf;
  162. }
  163. }
  164. }