audio-repack.c 7.1 KB

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