audio-repack.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "audio-repack.h"
  2. #include <util/sse-intrin.h>
  3. int check_buffer(struct audio_repack *repack, uint32_t frame_count)
  4. {
  5. const uint32_t new_size =
  6. frame_count * repack->base_dst_size + repack->extra_dst_size;
  7. if (repack->packet_size < new_size) {
  8. repack->packet_buffer =
  9. brealloc(repack->packet_buffer, new_size);
  10. if (!repack->packet_buffer)
  11. return -1;
  12. repack->packet_size = new_size;
  13. }
  14. return 0;
  15. }
  16. /*
  17. * Squash arrays.
  18. * For instance:
  19. * 2.1:
  20. *
  21. * | FL | FR | LFE | emp | emp | emp |emp |emp |
  22. * | | |
  23. * | FL | FR | LFE |
  24. */
  25. int repack_squash(struct audio_repack *repack, const uint8_t *bsrc,
  26. uint32_t frame_count)
  27. {
  28. if (check_buffer(repack, frame_count) < 0)
  29. return -1;
  30. int squash = repack->extra_dst_size;
  31. const __m128i *src = (__m128i *)bsrc;
  32. const __m128i *esrc = src + frame_count;
  33. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  34. /* Audio needs squashing in order to avoid resampling issues.
  35. * The condition checks for 7.1 audio for which no squash is needed.
  36. */
  37. if (squash > 0) {
  38. while (src != esrc) {
  39. __m128i target = _mm_load_si128(src++);
  40. _mm_storeu_si128((__m128i *)dst, target);
  41. dst += 8 - squash;
  42. }
  43. }
  44. return 0;
  45. }
  46. int repack_squash_swap(struct audio_repack *repack, const uint8_t *bsrc,
  47. uint32_t frame_count)
  48. {
  49. if (check_buffer(repack, frame_count) < 0)
  50. return -1;
  51. int squash = repack->extra_dst_size;
  52. const __m128i *src = (__m128i *)bsrc;
  53. const __m128i *esrc = src + frame_count;
  54. uint16_t *dst = (uint16_t *)repack->packet_buffer;
  55. while (src != esrc) {
  56. __m128i target = _mm_load_si128(src++);
  57. __m128i buf =
  58. _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. }