audio-repack.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. */
  37. while (src != esrc) {
  38. __m128i target = _mm_load_si128(src++);
  39. _mm_storeu_si128((__m128i *)dst, target);
  40. dst += 8 - squash;
  41. }
  42. return 0;
  43. }
  44. int audio_repack_init(struct audio_repack *repack,
  45. audio_repack_mode_t repack_mode, uint8_t sample_bit)
  46. {
  47. memset(repack, 0, sizeof(*repack));
  48. if (sample_bit != 16)
  49. return -1;
  50. repack->base_src_size = 8 * (16 / 8);
  51. repack->base_dst_size = (int)repack_mode * (16 / 8);
  52. repack->extra_dst_size = 8 - (int)repack_mode;
  53. repack->repack_func = &repack_squash;
  54. return 0;
  55. }
  56. void audio_repack_free(struct audio_repack *repack)
  57. {
  58. if (repack->packet_buffer)
  59. bfree(repack->packet_buffer);
  60. memset(repack, 0, sizeof(*repack));
  61. }