audio-repack.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "audio-repack.h"
  2. #define USE_SIMD
  3. #ifdef USE_SIMD
  4. #include <util/sse-intrin.h>
  5. #endif
  6. #define NUM_CHANNELS 8 /* max until OBS supports higher channel counts */
  7. int check_buffer(struct audio_repack *repack, uint32_t frame_count)
  8. {
  9. const uint32_t new_size = frame_count * repack->base_dst_size + repack->pad_dst_size;
  10. if (repack->packet_size < new_size) {
  11. repack->packet_buffer = brealloc(repack->packet_buffer, new_size);
  12. if (!repack->packet_buffer)
  13. return -1;
  14. repack->packet_size = new_size;
  15. }
  16. return 0;
  17. }
  18. #ifdef USE_SIMD
  19. /*
  20. * Squash array of 8ch to new channel count
  21. * 16-bit PCM, SIMD version
  22. * For instance:
  23. * 2.1:
  24. *
  25. * | FL | FR | LFE | emp | emp | emp |emp |emp |
  26. * | | |
  27. * | FL | FR | LFE |
  28. */
  29. int repack_squash16(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  30. {
  31. if (check_buffer(repack, frame_count) < 0)
  32. return -1;
  33. int squash = repack->squash_count;
  34. const __m128i *src = (__m128i *)bsrc;
  35. const __m128i *end = src + frame_count;
  36. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  37. /* Audio needs squashing in order to avoid resampling issues.
  38. * The condition checks for 7.1 audio for which no squash is needed.
  39. */
  40. if (squash > 0) {
  41. while (src != end) {
  42. __m128i target = _mm_load_si128(src++);
  43. _mm_storeu_si128((__m128i *)dst, target);
  44. dst += NUM_CHANNELS - squash;
  45. }
  46. }
  47. return 0;
  48. }
  49. /*
  50. * Squash array of 8ch and swap Front Center channel with LFE
  51. * 16-bit PCM, SIMD version
  52. * For instance:
  53. * 2.1:
  54. *
  55. * | FL | FR | FC | LFE | RL | RR | LC | RC |
  56. * | | |
  57. * | FL | FR | LFE | FC | RL | RR | LC | RC |
  58. */
  59. int repack_squash_swap16(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  60. {
  61. if (check_buffer(repack, frame_count) < 0)
  62. return -1;
  63. int squash = repack->squash_count;
  64. const __m128i *src = (__m128i *)bsrc;
  65. const __m128i *end = src + frame_count;
  66. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  67. while (src != end) {
  68. __m128i target = _mm_load_si128(src++);
  69. __m128i buf = _mm_shufflelo_epi16(target, _MM_SHUFFLE(2, 3, 1, 0));
  70. _mm_storeu_si128((__m128i *)dst, buf);
  71. dst += NUM_CHANNELS - squash;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * Squash array of 8ch to new channel count
  77. * 32-bit PCM, SIMD version
  78. */
  79. int repack_squash32(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  80. {
  81. if (check_buffer(repack, frame_count) < 0)
  82. return -1;
  83. int squash = repack->squash_count;
  84. const __m128i *src = (__m128i *)bsrc;
  85. const __m128i *end = (__m128i *)(bsrc + (frame_count * repack->base_src_size));
  86. uint32_t *dst = (uint32_t *)repack->packet_buffer;
  87. if (squash > 0) {
  88. while (src != end) {
  89. __m128i tgt_lo = _mm_loadu_si128(src++);
  90. __m128i tgt_hi = _mm_loadu_si128(src++);
  91. _mm_storeu_si128((__m128i *)dst, tgt_lo);
  92. dst += 4;
  93. _mm_storeu_si128((__m128i *)dst, tgt_hi);
  94. dst += 4;
  95. dst -= squash;
  96. }
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Squash array of 8ch to new channel count and swap FC with LFE
  102. * 32-bit PCM, SIMD version
  103. */
  104. int repack_squash_swap32(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  105. {
  106. if (check_buffer(repack, frame_count) < 0)
  107. return -1;
  108. int squash = repack->squash_count;
  109. const __m128i *src = (__m128i *)bsrc;
  110. const __m128i *end = (__m128i *)(bsrc + (frame_count * repack->base_src_size));
  111. uint32_t *dst = (uint32_t *)repack->packet_buffer;
  112. while (src != end) {
  113. __m128i tgt_lo = _mm_shuffle_epi32(_mm_loadu_si128(src++), _MM_SHUFFLE(2, 3, 1, 0));
  114. __m128i tgt_hi = _mm_loadu_si128(src++);
  115. _mm_storeu_si128((__m128i *)dst, tgt_lo);
  116. dst += 4;
  117. _mm_storeu_si128((__m128i *)dst, tgt_hi);
  118. dst += 4;
  119. dst -= squash;
  120. }
  121. return 0;
  122. }
  123. #else
  124. /*
  125. * Squash array of 8ch to new channel count
  126. * 16-bit or 32-bit PCM, C version
  127. */
  128. int repack_squash(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  129. {
  130. if (check_buffer(repack, frame_count) < 0)
  131. return -1;
  132. int squash = repack->squash_count;
  133. const uint8_t *src = bsrc;
  134. const uint8_t *end = src + frame_count * repack->base_src_size;
  135. uint8_t *dst = repack->packet_buffer;
  136. uint32_t new_channel_count = NUM_CHANNELS - squash;
  137. if (squash > 0) {
  138. while (src != end) {
  139. memcpy(dst, src, repack->bytes_per_sample * new_channel_count);
  140. dst += (new_channel_count * repack->bytes_per_sample);
  141. src += NUM_CHANNELS * repack->bytes_per_sample;
  142. }
  143. }
  144. return 0;
  145. }
  146. void shuffle_8ch(uint8_t *dst, const uint8_t *src, size_t szb, int ch1, int ch2, int ch3, int ch4, int ch5, int ch6,
  147. int ch7, int ch8)
  148. {
  149. /* shuffle 8 channels of audio */
  150. for (size_t i = 0; i < szb; i++) {
  151. dst[0 * szb + i] = src[ch1 * szb + i];
  152. dst[1 * szb + i] = src[ch2 * szb + i];
  153. dst[2 * szb + i] = src[ch3 * szb + i];
  154. dst[3 * szb + i] = src[ch4 * szb + i];
  155. dst[4 * szb + i] = src[ch5 * szb + i];
  156. dst[5 * szb + i] = src[ch6 * szb + i];
  157. dst[6 * szb + i] = src[ch7 * szb + i];
  158. dst[7 * szb + i] = src[ch8 * szb + i];
  159. }
  160. }
  161. /*
  162. * Squash array of 8ch to new channel count and swap FC with LFE
  163. * 16-bit or 32-bit PCM, C version
  164. */
  165. int repack_squash_swap(struct audio_repack *repack, const uint8_t *bsrc, uint32_t frame_count)
  166. {
  167. if (check_buffer(repack, frame_count) < 0)
  168. return -1;
  169. int squash = repack->squash_count;
  170. const uint8_t *src = bsrc;
  171. const uint8_t *end = src + (frame_count * repack->base_src_size);
  172. uint8_t *dst = repack->packet_buffer;
  173. uint32_t new_channel_count = NUM_CHANNELS - squash;
  174. if (squash > 0) {
  175. while (src != end) {
  176. shuffle_8ch(dst, src, 4, 0, 1, 3, 2, 4, 5, 6, 7);
  177. dst += (new_channel_count * repack->bytes_per_sample);
  178. src += (NUM_CHANNELS * repack->bytes_per_sample);
  179. }
  180. }
  181. return 0;
  182. }
  183. #endif
  184. int audio_repack_init(struct audio_repack *repack, audio_repack_mode_t repack_mode, uint8_t bits_per_sample)
  185. {
  186. memset(repack, 0, sizeof(*repack));
  187. if (bits_per_sample != 16 && bits_per_sample != 32)
  188. return -1;
  189. int _audio_repack_ch[9] = {2, 3, 4, 5, 6, 5, 6, 8, 8};
  190. int bytes_per_sample = (bits_per_sample / 8);
  191. repack->bytes_per_sample = bytes_per_sample;
  192. repack->base_src_size = NUM_CHANNELS * bytes_per_sample;
  193. repack->base_dst_size = _audio_repack_ch[repack_mode] * bytes_per_sample;
  194. uint32_t squash_count = NUM_CHANNELS - _audio_repack_ch[repack_mode];
  195. repack->pad_dst_size = squash_count * bytes_per_sample;
  196. repack->squash_count = squash_count;
  197. #ifdef USE_SIMD
  198. if (bits_per_sample == 16) {
  199. repack->repack_func = &repack_squash16;
  200. if (repack_mode == repack_mode_8to5ch_swap || repack_mode == repack_mode_8to6ch_swap ||
  201. repack_mode == repack_mode_8ch_swap)
  202. repack->repack_func = &repack_squash_swap16;
  203. } else if (bits_per_sample == 32) {
  204. repack->repack_func = &repack_squash32;
  205. if (repack_mode == repack_mode_8to5ch_swap || repack_mode == repack_mode_8to6ch_swap ||
  206. repack_mode == repack_mode_8ch_swap)
  207. repack->repack_func = &repack_squash_swap32;
  208. }
  209. #else
  210. repack->repack_func = &repack_squash;
  211. if (repack_mode == repack_mode_8to5ch_swap || repack_mode == repack_mode_8to6ch_swap ||
  212. repack_mode == repack_mode_8ch_swap)
  213. repack->repack_func = &repack_squash_swap;
  214. #endif
  215. return 0;
  216. }
  217. void audio_repack_free(struct audio_repack *repack)
  218. {
  219. if (repack->packet_buffer)
  220. bfree(repack->packet_buffer);
  221. memset(repack, 0, sizeof(*repack));
  222. }