audio-repack.c 2.5 KB

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