btstack_sbc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2014 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. /*
  38. * btstack_sbc.h
  39. *
  40. */
  41. #ifndef BTSTACK_SBC_H
  42. #define BTSTACK_SBC_H
  43. #include <stdint.h>
  44. #include "btstack_sbc_plc.h"
  45. #if defined __cplusplus
  46. extern "C" {
  47. #endif
  48. typedef enum{
  49. SBC_MODE_STANDARD,
  50. SBC_MODE_mSBC
  51. } btstack_sbc_mode_t;
  52. typedef struct {
  53. void * context;
  54. void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
  55. // private
  56. void * decoder_state;
  57. btstack_sbc_plc_state_t plc_state;
  58. btstack_sbc_mode_t mode;
  59. // summary of processed good, bad and zero frames
  60. int good_frames_nr;
  61. int bad_frames_nr;
  62. int zero_frames_nr;
  63. } btstack_sbc_decoder_state_t;
  64. typedef struct {
  65. // private
  66. void * encoder_state;
  67. btstack_sbc_mode_t mode;
  68. } btstack_sbc_encoder_state_t;
  69. /* API_START */
  70. /* BTstack SBC decoder */
  71. /**
  72. * @brief Init SBC decoder
  73. * @param state
  74. * @param mode
  75. * @param callback for decoded PCM data in host endianess
  76. * @param context provided in callback
  77. */
  78. void btstack_sbc_decoder_init(btstack_sbc_decoder_state_t * state, btstack_sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context);
  79. /**
  80. * @brief Process received SBC data
  81. * @param state
  82. * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
  83. * @param buffer
  84. * @param size
  85. */
  86. void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size);
  87. /**
  88. * @brief Get number of samples per SBC frame
  89. */
  90. int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state);
  91. /*
  92. * @brief Get number of channels
  93. */
  94. int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state);
  95. /*
  96. * @brief Get sample rate in hz
  97. */
  98. int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
  99. /* BTstack SBC Encoder */
  100. /**
  101. * @brief Init SBC encoder
  102. * @param state
  103. * @param mode
  104. * @param blocks
  105. * @param subbands
  106. * @param allocation_method
  107. * @param sample_rate
  108. * @param bitpool
  109. * @param channel_mode
  110. */
  111. void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
  112. int blocks, int subbands, int allocation_method, int sample_rate, int bitpool, int channel_mode);
  113. /**
  114. * @brief Encode PCM data
  115. * @param buffer with samples in host endianess
  116. */
  117. void btstack_sbc_encoder_process_data(int16_t * input_buffer);
  118. /**
  119. * @brief Return SBC frame
  120. */
  121. uint8_t * btstack_sbc_encoder_sbc_buffer(void);
  122. /**
  123. * @brief Return SBC frame length
  124. */
  125. uint16_t btstack_sbc_encoder_sbc_buffer_length(void);
  126. /**
  127. * @brief Return number of audio frames required for one SBC packet
  128. * @note each audio frame contains 2 sample values in stereo modes
  129. */
  130. int btstack_sbc_encoder_num_audio_frames(void);
  131. /* API_END */
  132. // testing only
  133. void btstack_sbc_decoder_test_set_plc_enabled(int plc_enabled);
  134. void btstack_sbc_decoder_test_simulate_corrupt_frames(int period);
  135. #if defined __cplusplus
  136. }
  137. #endif
  138. #endif // BTSTACK_SBC_H