mp3_decode_port.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "luat_base.h"
  2. #include "luat_mem.h"
  3. #undef LUAT_BSP_SUPPORT_FLOAT
  4. #ifdef LUAT_BSP_SUPPORT_FLOAT
  5. #include "mp3_decode/minimp3.h"
  6. #else
  7. #include "mp3_decode/madmp3.h"
  8. #endif
  9. void *mp3_decoder_create(void)
  10. {
  11. #ifdef LUAT_BSP_SUPPORT_FLOAT
  12. return luat_heap_malloc(sizeof(mp3dec_t));
  13. #else
  14. return luat_heap_malloc(sizeof(madmp3_t));
  15. #endif
  16. }
  17. void mp3_decoder_init(void *decoder)
  18. {
  19. #ifdef LUAT_BSP_SUPPORT_FLOAT
  20. memset(decoder, 0, sizeof(mp3dec_t));
  21. mp3dec_init(decoder);
  22. #else
  23. madmp3_init(decoder);
  24. #endif
  25. }
  26. int mp3_decoder_get_info(void *decoder, const uint8_t *input, uint32_t len, uint32_t *hz, uint8_t *channel)
  27. {
  28. #ifdef LUAT_BSP_SUPPORT_FLOAT
  29. mp3dec_frame_info_t info;
  30. if (mp3dec_decode_frame(decoder, input, len, NULL, &info) > 0)
  31. {
  32. *hz = info.hz;
  33. *channel = info.channels;
  34. return 1;
  35. }
  36. else
  37. {
  38. return 0;
  39. }
  40. #else
  41. if (madmp3_decode(decoder, input, len, NULL) > 0)
  42. {
  43. madmp3_t *mad = (madmp3_t *)decoder;
  44. *hz = mad->frame.header.samplerate;
  45. if (mad->frame.header.mode)
  46. {
  47. *channel = 2;
  48. }
  49. else
  50. {
  51. *channel = 1;
  52. }
  53. return 1;
  54. }
  55. else
  56. {
  57. return 0;
  58. }
  59. #endif
  60. }
  61. int mp3_decoder_get_data(void *decoder, const uint8_t *input, uint32_t len, int16_t *pcm, uint32_t *out_len, uint32_t *hz, uint32_t *used)
  62. {
  63. #ifdef LUAT_BSP_SUPPORT_FLOAT
  64. mp3dec_frame_info_t info;
  65. int result = mp3dec_decode_frame(decoder, input, len, pcm, &info);
  66. *hz = info.hz;
  67. *out_len = (result * info.channels * 2);
  68. *used = info.frame_bytes;
  69. return result;
  70. #else
  71. madmp3_t *mad = (madmp3_t *)decoder;
  72. mad->stream.md_len = 0;
  73. if (madmp3_decode(decoder, input, len, pcm) > 0)
  74. {
  75. *hz = mad->frame.header.samplerate;
  76. *out_len = (mad->synth.pcm.length * mad->synth.pcm.channels * 2);
  77. *used = (uint32_t)mad->stream.next_frame - (uint32_t)input;
  78. }
  79. #endif
  80. }