audio-repack.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "audio-repack.h"
  2. #include <emmintrin.h>
  3. int check_buffer(struct audio_repack *repack,
  4. uint32_t frame_count)
  5. {
  6. const uint32_t new_size = frame_count * repack->base_dst_size
  7. + repack->extra_dst_size;
  8. if (repack->packet_size < new_size) {
  9. repack->packet_buffer = brealloc(
  10. repack->packet_buffer, new_size);
  11. if (!repack->packet_buffer)
  12. return -1;
  13. repack->packet_size = new_size;
  14. }
  15. return 0;
  16. }
  17. /*
  18. * Squash arrays.
  19. * For instance:
  20. * 2.1:
  21. *
  22. * | FL | FR | LFE | emp | emp | emp |emp |emp |
  23. * | | |
  24. * | FL | FR | LFE |
  25. */
  26. int repack_squash(struct audio_repack *repack,
  27. const uint8_t *bsrc, uint32_t frame_count)
  28. {
  29. if (check_buffer(repack, frame_count) < 0)
  30. return -1;
  31. int squash = repack->extra_dst_size;
  32. const __m128i *src = (__m128i *)bsrc;
  33. const __m128i *esrc = src + frame_count;
  34. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  35. /* Audio needs squashing in order to avoid resampling issues.
  36. * The condition checks for 7.1 audio for which no squash is needed.
  37. */
  38. if (squash > 0) {
  39. while (src != esrc) {
  40. __m128i target = _mm_load_si128(src++);
  41. _mm_storeu_si128((__m128i *)dst, target);
  42. dst += 8 - squash;
  43. }
  44. }
  45. return 0;
  46. }
  47. int repack_squash_swap(struct audio_repack *repack,
  48. const uint8_t *bsrc, uint32_t frame_count)
  49. {
  50. if (check_buffer(repack, frame_count) < 0)
  51. return -1;
  52. int squash = repack->extra_dst_size;
  53. const __m128i *src = (__m128i *)bsrc;
  54. const __m128i *esrc = src + frame_count;
  55. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  56. while (src != esrc) {
  57. __m128i target = _mm_load_si128(src++);
  58. __m128i buf = _mm_shufflelo_epi16(target, _MM_SHUFFLE(2, 3, 1, 0));
  59. _mm_storeu_si128((__m128i *)dst, buf);
  60. dst += 8 - squash;
  61. }
  62. return 0;
  63. }
  64. int audio_repack_init(struct audio_repack *repack,
  65. audio_repack_mode_t repack_mode, uint8_t sample_bit)
  66. {
  67. memset(repack, 0, sizeof(*repack));
  68. if (sample_bit != 16)
  69. return -1;
  70. int _audio_repack_ch[8] = { 3, 4, 5, 6, 5, 6, 8, 8 };
  71. repack->base_src_size = 8 * (16 / 8);
  72. repack->base_dst_size = _audio_repack_ch[repack_mode] * (16 / 8);
  73. repack->extra_dst_size = 8 - _audio_repack_ch[repack_mode];
  74. repack->repack_func = &repack_squash;
  75. if (repack_mode == repack_mode_8to5ch_swap ||
  76. repack_mode == repack_mode_8to6ch_swap ||
  77. repack_mode == repack_mode_8ch_swap)
  78. repack->repack_func = &repack_squash_swap;
  79. return 0;
  80. }
  81. void audio_repack_free(struct audio_repack *repack)
  82. {
  83. if (repack->packet_buffer)
  84. bfree(repack->packet_buffer);
  85. memset(repack, 0, sizeof(*repack));
  86. }