api_codec.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef _API_CODEC_H
  2. #define _API_CODEC_H
  3. /* 采样点数 -> PCM16字节数 */
  4. #define SAMPLE_2_PCM16_BYTE(x) (x*2)
  5. /* PCM16字节数 -> 采样点数 */
  6. #define PCM16_BYTE_2_SAMPLE(x) (x/2)
  7. /* 采样点数 -> ADPCM块字节数 */
  8. #define SAMPLE_2_ADPCM_BYTE(x) ((x-1)/2+4)
  9. /* ADPCM块字节数 -> 采样点数 */
  10. #define ADPCM_BYTE_2_SAMPLE(x) (((x-4)*2)+1)
  11. /* ADPCM块字节数 -> PCM16字节数 */
  12. #define ADPCM_BYTE_2_PCM16_BYTE(x) (((x-4)*4)+2)
  13. //wav
  14. uint16_t wav_res_analize(u8 *head_buf, u8 *nch, u32 *spr, u16 *block_size);
  15. //sbc
  16. /* sampling frequency */
  17. enum {
  18. SBC_FREQ_16000 = 0x00,
  19. SBC_FREQ_32000 = 0x01,
  20. SBC_FREQ_44100 = 0x02,
  21. SBC_FREQ_48000 = 0x03,
  22. };
  23. /* blocks */
  24. enum {
  25. SBC_BLK_4 = 0x00,
  26. SBC_BLK_8 = 0x01,
  27. SBC_BLK_12 = 0x02,
  28. SBC_BLK_16 = 0x03,
  29. };
  30. /* subbands */
  31. enum {
  32. SBC_SB_4 = 0x00,
  33. SBC_SB_8 = 0x01,
  34. };
  35. /* channel mode */
  36. enum {
  37. SBC_MODE_MONO = 0x00,
  38. SBC_MODE_DUAL_CHANNEL = 0x01,
  39. SBC_MODE_STEREO = 0x02,
  40. SBC_MODE_JOINT_STEREO = 0x03,
  41. };
  42. /* allocation method */
  43. enum {
  44. SBC_AM_LOUDNESS = 0x00,
  45. SBC_AM_SNR = 0x01,
  46. };
  47. /**
  48. * @brief Take the mono 16K sampling rate as an example
  49. * @brief mSBC:
  50. * @brief mSBC parameter cannot be changed, ilen = 120 * 2, olen = 57;
  51. *
  52. * @brief SBC:
  53. * @brief subbands_l = subbands ? 8 : 4;
  54. * @brief blocks_l = 4 + (blocks * 4);
  55. * @brief channels_l = mode ? 2 : 1;
  56. * @brief SBC ilen = subbands_l * blocks_l * channels_l * 2;
  57. * @brief SBC olen = 4 + (4 * subbands_l * channels_l) / 8 + ((blocks_l * channels_l * bitpool) + 7) / 8; //Ignore the decimal point
  58. *
  59. * @brief mic sample points = ilen / 2; (max: 128)
  60. */
  61. void sbc_encode_init(uint8_t freq, uint8_t blocks, uint8_t subbands, uint8_t mode, uint8_t allocation, uint8_t bitpool);
  62. uint16_t sbc_encode_frame(uint8_t *ibuf, uint16_t ilen, uint8_t *obuf, uint16_t olen);
  63. void msbc_encode_init(void);
  64. uint16_t msbc_encode_frame(uint8_t *ibuf, uint16_t ilen, uint8_t *obuf, uint16_t olen);
  65. /**
  66. * @brief SBC decode
  67. *
  68. * @brief is_msbc: 1 -- msbc, 0 -- sbc.
  69. * @brief ibuf: SBC encoded buf
  70. * @brief ilen: SBC encoded buf len
  71. * @brief obuf: SBC decoded output buf
  72. *
  73. * @brief return: decode output buf sample points. (max: 128)
  74. */
  75. void sbc_decode_init(bool is_msbc);
  76. uint16_t sbc_decode_frame(uint8_t *ibuf, uint16_t ilen, uint8_t *obuf);
  77. //adpcm
  78. /**
  79. * @brief ima adpcm encode compression ratio 4:1
  80. * @brief encode output buf format:2byte prediction sample, 1byte index, 1byte reserve, nbyte data
  81. * @brief decode input buf format:2byte prediction sample, 1byte index, 1byte reserve, nbyte data
  82. *
  83. * @brief ima adpcm need one more sample point. reference routine bsp_sdadc_voice.c
  84. *
  85. * @param p_dst: output buf
  86. * @param p_src: input buf
  87. * @param sample_len:sample points
  88. */
  89. void adpcm_decode_block(uint8_t *p_dst,uint8_t *p_src,uint32_t sample_len);
  90. void adpcm_encode_block(uint8_t *p_dst, uint8_t *p_src, uint32_t sample_len);
  91. void adpcm_decode_block_big(uint8_t *p_dst, uint8_t *p_src, uint32_t sample_len);
  92. void adpcm_encode_block_big(uint8_t *p_dst, uint8_t *p_src, uint32_t sample_len);
  93. void adpcm_sample_idx_set(int16_t idx);
  94. void adpcm_sample_idx_reset(void);
  95. void adpcm_presample_set(int32_t pcm_16);
  96. uint16_t adpcm_sample_idx_get(void);
  97. int16_t adpcm_presample_get(void);
  98. //opus
  99. /**
  100. * @brief opus encode initialization, 16bit, 20ms one frame
  101. * @brief opus encode requires high computing power, so the system needs to raise the main frequency to more than 120M
  102. * @param samplerate: sampling rate, only support 8000 or 16000
  103. * @param nch: number of channels,only support 1 channel now
  104. * @param bit_rate:16000 or 32000, 16000-->1:16 32000-->1:8
  105. * @return true or false
  106. */
  107. bool opus_enc_init(u32 samplerate, u32 nch, u32 bit_rate);
  108. /**
  109. * @brief opus encode exit
  110. * @return true or false
  111. */
  112. bool opus_enc_exit(void);
  113. /**
  114. * @brief opus encode process
  115. * @param pcm: Data to be encoded
  116. At a sample rate of 16K, 16bit, 20ms one frame, pcm_size = (16 * 20 * 2)byte
  117. Ensure that there is a sufficient length of data
  118. * @param out: opus encoded data, out_len = pcm_size / 16
  119. Ensure that there is a sufficient length of data
  120. */
  121. int32_t opus_enc_process(int16_t *pcm, u8 *out);
  122. /**
  123. * @brief mp3 decode
  124. */
  125. bool mp3_dec_frame(void);
  126. void mp3_music_pcm_kick(void);
  127. u8 mp3_music_dec_sta_get(void);
  128. void mp3_music_dec_sta_set(u8 sta);
  129. bool mp3_res_play_kick(u32 addr, u32 len);
  130. void mp3_res_play_exit(void);
  131. #endif // _API_CODEC_H