audio_decoder.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <netinet/in.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "audio_decoder.h"
  5. #include "esp_log.h"
  6. #include "alac_magic_cookie.h"
  7. #include "decoder/impl/esp_aac_dec.h"
  8. #include "decoder/impl/esp_alac_dec.h"
  9. #include "esp_audio_dec.h"
  10. #define ADTS_HEADER_LEN 7
  11. #define MAX_FALLBACK_CHANNELS 2
  12. typedef enum {
  13. AUDIO_DECODER_NONE = 0,
  14. AUDIO_DECODER_PCM,
  15. AUDIO_DECODER_ALAC,
  16. AUDIO_DECODER_AAC
  17. } audio_decoder_kind_t;
  18. struct audio_decoder {
  19. audio_decoder_kind_t kind;
  20. audio_format_t format;
  21. void *alac_decoder;
  22. void *aac_decoder;
  23. uint8_t alac_magic_cookie[ALAC_MAGIC_COOKIE_SIZE];
  24. uint8_t *aac_frame_buffer;
  25. size_t aac_frame_buffer_size;
  26. };
  27. static const char *TAG = "audio_dec";
  28. // Reopen the AAC decoder to reset its internal state after a corrupt frame.
  29. // The codec's state machine can get stuck after certain errors (e.g. error 20)
  30. // and will continue failing every subsequent frame until it is recreated.
  31. static void aac_decoder_reset(audio_decoder_t *decoder) {
  32. if (decoder->aac_decoder) {
  33. esp_aac_dec_close(decoder->aac_decoder);
  34. decoder->aac_decoder = NULL;
  35. }
  36. esp_aac_dec_cfg_t aac_cfg = ESP_AAC_DEC_CONFIG_DEFAULT();
  37. aac_cfg.sample_rate = decoder->format.sample_rate;
  38. aac_cfg.channel = decoder->format.channels;
  39. aac_cfg.bits_per_sample =
  40. decoder->format.bits_per_sample ? decoder->format.bits_per_sample : 16;
  41. aac_cfg.no_adts_header = false;
  42. aac_cfg.aac_plus_enable = false;
  43. esp_audio_err_t err =
  44. esp_aac_dec_open(&aac_cfg, sizeof(aac_cfg), &decoder->aac_decoder);
  45. if (err != ESP_AUDIO_ERR_OK) {
  46. ESP_LOGE(TAG, "AAC decoder reset failed: %d", err);
  47. decoder->aac_decoder = NULL;
  48. } else {
  49. ESP_LOGW(TAG, "AAC decoder reset OK");
  50. }
  51. }
  52. static bool codec_is_alac(const char *codec) {
  53. if (!codec) {
  54. return false;
  55. }
  56. return strcmp(codec, "AppleLossless") == 0 || strcmp(codec, "ALAC") == 0;
  57. }
  58. static bool codec_is_aac(const char *codec) {
  59. if (!codec) {
  60. return false;
  61. }
  62. return strstr(codec, "AAC") != NULL || strstr(codec, "aac") != NULL ||
  63. strstr(codec, "mpeg4-generic") != NULL;
  64. }
  65. static bool aac_has_adts_header(const uint8_t *data, size_t len) {
  66. return len >= 2 && data[0] == 0xFF && (data[1] & 0xF0) == 0xF0;
  67. }
  68. static void build_adts_header(uint8_t *header, size_t frame_len,
  69. int sample_rate, int channels) {
  70. (void)sample_rate;
  71. (void)channels;
  72. int profile = 2;
  73. int freq_idx = 4;
  74. int chan_cfg = 2;
  75. int packet_len = (int)(frame_len + ADTS_HEADER_LEN);
  76. header[0] = 0xFF;
  77. header[1] = 0xF1;
  78. header[2] = ((profile - 1) << 6) + (freq_idx << 2) + (chan_cfg >> 2);
  79. header[3] = ((chan_cfg & 3) << 6) + (packet_len >> 11);
  80. header[4] = (packet_len & 0x7FF) >> 3;
  81. header[5] = ((packet_len & 7) << 5) + 0x1F;
  82. header[6] = 0xFC;
  83. }
  84. audio_decoder_t *audio_decoder_create(const audio_decoder_config_t *config) {
  85. if (!config) {
  86. return NULL;
  87. }
  88. audio_decoder_t *decoder = calloc(1, sizeof(*decoder));
  89. if (!decoder) {
  90. return NULL;
  91. }
  92. decoder->format = config->format;
  93. if (codec_is_alac(config->format.codec)) {
  94. decoder->kind = AUDIO_DECODER_ALAC;
  95. build_alac_magic_cookie(decoder->alac_magic_cookie, &config->format);
  96. esp_alac_dec_cfg_t alac_cfg = {.codec_spec_info =
  97. decoder->alac_magic_cookie,
  98. .spec_info_len = ALAC_MAGIC_COOKIE_SIZE};
  99. esp_audio_err_t err =
  100. esp_alac_dec_open(&alac_cfg, sizeof(alac_cfg), &decoder->alac_decoder);
  101. if (err != ESP_AUDIO_ERR_OK) {
  102. ESP_LOGE(TAG, "Failed to open ALAC decoder: %d", err);
  103. decoder->alac_decoder = NULL;
  104. decoder->kind = AUDIO_DECODER_NONE;
  105. }
  106. } else if (codec_is_aac(config->format.codec)) {
  107. decoder->kind = AUDIO_DECODER_AAC;
  108. esp_aac_dec_cfg_t aac_cfg = ESP_AAC_DEC_CONFIG_DEFAULT();
  109. aac_cfg.sample_rate = config->format.sample_rate;
  110. aac_cfg.channel = config->format.channels;
  111. aac_cfg.bits_per_sample =
  112. config->format.bits_per_sample ? config->format.bits_per_sample : 16;
  113. aac_cfg.no_adts_header = false;
  114. aac_cfg.aac_plus_enable = false;
  115. esp_audio_err_t err =
  116. esp_aac_dec_open(&aac_cfg, sizeof(aac_cfg), &decoder->aac_decoder);
  117. if (err != ESP_AUDIO_ERR_OK) {
  118. ESP_LOGE(TAG, "Failed to open AAC decoder: %d", err);
  119. decoder->aac_decoder = NULL;
  120. decoder->kind = AUDIO_DECODER_NONE;
  121. }
  122. } else if (strcmp(config->format.codec, "L16") == 0 ||
  123. strcmp(config->format.codec, "PCM") == 0) {
  124. decoder->kind = AUDIO_DECODER_PCM;
  125. } else {
  126. decoder->kind = AUDIO_DECODER_NONE;
  127. }
  128. return decoder;
  129. }
  130. void audio_decoder_destroy(audio_decoder_t *decoder) {
  131. if (!decoder) {
  132. return;
  133. }
  134. if (decoder->alac_decoder) {
  135. esp_alac_dec_close(decoder->alac_decoder);
  136. decoder->alac_decoder = NULL;
  137. }
  138. if (decoder->aac_decoder) {
  139. esp_aac_dec_close(decoder->aac_decoder);
  140. decoder->aac_decoder = NULL;
  141. }
  142. if (decoder->aac_frame_buffer) {
  143. free(decoder->aac_frame_buffer);
  144. decoder->aac_frame_buffer = NULL;
  145. decoder->aac_frame_buffer_size = 0;
  146. }
  147. free(decoder);
  148. }
  149. int audio_decoder_decode(audio_decoder_t *decoder, const uint8_t *input,
  150. size_t input_len, int16_t *output,
  151. size_t output_capacity_samples,
  152. audio_decode_info_t *info) {
  153. if (!decoder || !input || !output || output_capacity_samples == 0) {
  154. return -1;
  155. }
  156. int channels = decoder->format.channels;
  157. if (channels <= 0) {
  158. channels = MAX_FALLBACK_CHANNELS;
  159. }
  160. if (decoder->kind == AUDIO_DECODER_PCM) {
  161. size_t decoded_samples = input_len / (channels * sizeof(int16_t));
  162. if (decoded_samples > output_capacity_samples) {
  163. decoded_samples = output_capacity_samples;
  164. }
  165. const int16_t *src = (const int16_t *)input;
  166. for (size_t i = 0; i < decoded_samples * channels; i++) {
  167. output[i] = ntohs(src[i]);
  168. }
  169. if (info) {
  170. info->channels = channels;
  171. }
  172. return (int)decoded_samples;
  173. }
  174. if (decoder->kind == AUDIO_DECODER_ALAC) {
  175. if (!decoder->alac_decoder) {
  176. return -1;
  177. }
  178. esp_audio_dec_in_raw_t raw = {.buffer = (uint8_t *)input,
  179. .len = (uint32_t)input_len,
  180. .consumed = 0,
  181. .frame_recover = ESP_AUDIO_DEC_RECOVERY_NONE};
  182. esp_audio_dec_out_frame_t frame = {
  183. .buffer = (uint8_t *)output,
  184. .len = (uint32_t)(output_capacity_samples * channels * sizeof(int16_t)),
  185. .decoded_size = 0};
  186. esp_audio_dec_info_t dec_info = {0};
  187. esp_audio_err_t err =
  188. esp_alac_dec_decode(decoder->alac_decoder, &raw, &frame, &dec_info);
  189. if (err != ESP_AUDIO_ERR_OK) {
  190. return -1;
  191. }
  192. int dec_channels = dec_info.channel > 0 ? dec_info.channel : channels;
  193. if (dec_channels <= 0) {
  194. dec_channels = MAX_FALLBACK_CHANNELS;
  195. }
  196. size_t decoded_samples =
  197. frame.decoded_size / (dec_channels * sizeof(int16_t));
  198. if (decoded_samples > output_capacity_samples) {
  199. decoded_samples = output_capacity_samples;
  200. }
  201. if (info) {
  202. info->channels = dec_channels;
  203. }
  204. return (int)decoded_samples;
  205. }
  206. if (decoder->kind == AUDIO_DECODER_AAC) {
  207. if (!decoder->aac_decoder) {
  208. return -1;
  209. }
  210. const uint8_t *decode_data = input;
  211. size_t decode_len = input_len;
  212. if (!aac_has_adts_header(input, input_len)) {
  213. size_t needed = input_len + ADTS_HEADER_LEN;
  214. if (!decoder->aac_frame_buffer ||
  215. decoder->aac_frame_buffer_size < needed) {
  216. uint8_t *new_buf = realloc(decoder->aac_frame_buffer, needed);
  217. if (!new_buf) {
  218. return -1;
  219. }
  220. decoder->aac_frame_buffer = new_buf;
  221. decoder->aac_frame_buffer_size = needed;
  222. }
  223. build_adts_header(decoder->aac_frame_buffer, input_len,
  224. decoder->format.sample_rate, decoder->format.channels);
  225. memcpy(decoder->aac_frame_buffer + ADTS_HEADER_LEN, input, input_len);
  226. decode_data = decoder->aac_frame_buffer;
  227. decode_len = needed;
  228. }
  229. esp_audio_dec_in_raw_t raw = {.buffer = (uint8_t *)decode_data,
  230. .len = (uint32_t)decode_len,
  231. .consumed = 0,
  232. .frame_recover = ESP_AUDIO_DEC_RECOVERY_NONE};
  233. esp_audio_dec_out_frame_t frame = {
  234. .buffer = (uint8_t *)output,
  235. .len = (uint32_t)(output_capacity_samples * channels * sizeof(int16_t)),
  236. .decoded_size = 0};
  237. esp_audio_dec_info_t dec_info = {0};
  238. esp_audio_err_t err =
  239. esp_aac_dec_decode(decoder->aac_decoder, &raw, &frame, &dec_info);
  240. if (err != ESP_AUDIO_ERR_OK) {
  241. ESP_LOGW(TAG, "AAC decode error %d — resetting decoder", err);
  242. aac_decoder_reset(decoder);
  243. return -1;
  244. }
  245. int dec_channels = dec_info.channel > 0 ? dec_info.channel : channels;
  246. if (dec_channels <= 0) {
  247. dec_channels = MAX_FALLBACK_CHANNELS;
  248. }
  249. size_t decoded_samples =
  250. frame.decoded_size / (dec_channels * sizeof(int16_t));
  251. if (decoded_samples > output_capacity_samples) {
  252. decoded_samples = output_capacity_samples;
  253. }
  254. if (info) {
  255. info->channels = dec_channels;
  256. }
  257. return (int)decoded_samples;
  258. }
  259. return -1;
  260. }
  261. bool audio_decoder_is_aac(const audio_decoder_t *decoder) {
  262. return decoder && decoder->kind == AUDIO_DECODER_AAC;
  263. }
  264. bool audio_decoder_is_alac(const audio_decoder_t *decoder) {
  265. return decoder && decoder->kind == AUDIO_DECODER_ALAC;
  266. }