• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "a2dp_aac_decoder"
18 
19 #include "a2dp_aac_decoder.h"
20 
21 #include <aacdecoder_lib.h>
22 #include <base/logging.h>
23 
24 #include "a2dp_aac.h"
25 #include "osi/include/allocator.h"
26 #include "osi/include/log.h"
27 
28 #define DECODE_BUF_LEN (8 * 2 * 1024)
29 
30 typedef struct {
31   HANDLE_AACDECODER aac_handle;
32   bool has_aac_handle;  // True if aac_handle is valid
33   INT_PCM* decode_buf = nullptr;
34   decoded_data_callback_t decode_callback;
35 } tA2DP_AAC_DECODER_CB;
36 
37 static tA2DP_AAC_DECODER_CB a2dp_aac_decoder_cb;
38 
A2DP_LoadDecoderAac(void)39 bool A2DP_LoadDecoderAac(void) {
40   // Nothing to do - the library is statically linked
41   return true;
42 }
43 
A2DP_UnloadDecoderAac(void)44 void A2DP_UnloadDecoderAac(void) { a2dp_aac_decoder_cleanup(); }
45 
a2dp_aac_decoder_init(decoded_data_callback_t decode_callback)46 bool a2dp_aac_decoder_init(decoded_data_callback_t decode_callback) {
47   a2dp_aac_decoder_cleanup();
48 
49   a2dp_aac_decoder_cb.aac_handle =
50       aacDecoder_Open(TT_MP4_LATM_MCP1, 1 /* nrOfLayers */);
51   a2dp_aac_decoder_cb.has_aac_handle = true;
52   a2dp_aac_decoder_cb.decode_buf = static_cast<INT_PCM*>(
53       osi_malloc(sizeof(a2dp_aac_decoder_cb.decode_buf[0]) * DECODE_BUF_LEN));
54   a2dp_aac_decoder_cb.decode_callback = decode_callback;
55   return true;
56 }
57 
a2dp_aac_decoder_cleanup(void)58 void a2dp_aac_decoder_cleanup(void) {
59   if (a2dp_aac_decoder_cb.has_aac_handle)
60     aacDecoder_Close(a2dp_aac_decoder_cb.aac_handle);
61   osi_free(a2dp_aac_decoder_cb.decode_buf);
62   memset(&a2dp_aac_decoder_cb, 0, sizeof(a2dp_aac_decoder_cb));
63 }
64 
a2dp_aac_decoder_decode_packet(BT_HDR * p_buf)65 bool a2dp_aac_decoder_decode_packet(BT_HDR* p_buf) {
66   auto* pBuffer = reinterpret_cast<UCHAR*>(p_buf->data + p_buf->offset);
67   UINT bufferSize = p_buf->len;
68   UINT bytesValid = p_buf->len;
69   while (bytesValid > 0) {
70     AAC_DECODER_ERROR err = aacDecoder_Fill(a2dp_aac_decoder_cb.aac_handle,
71                                             &pBuffer, &bufferSize, &bytesValid);
72     if (err != AAC_DEC_OK) {
73       LOG_ERROR(LOG_TAG, "%s: aacDecoder_Fill failed: 0x%x", __func__,
74                 static_cast<unsigned>(err));
75       return false;
76     }
77 
78     while (true) {
79       err = aacDecoder_DecodeFrame(a2dp_aac_decoder_cb.aac_handle,
80                                    a2dp_aac_decoder_cb.decode_buf,
81                                    DECODE_BUF_LEN, 0 /* flags */);
82       if (err == AAC_DEC_NOT_ENOUGH_BITS) {
83         break;
84       }
85       if (err != AAC_DEC_OK) {
86         LOG_ERROR(LOG_TAG, "%s: aacDecoder_DecodeFrame failed: 0x%x", __func__,
87                   static_cast<int>(err));
88         break;
89       }
90 
91       CStreamInfo* info =
92           aacDecoder_GetStreamInfo(a2dp_aac_decoder_cb.aac_handle);
93       if (!info || info->sampleRate <= 0) {
94         LOG_ERROR(LOG_TAG, "%s: Invalid stream info", __func__);
95         break;
96       }
97 
98       size_t frame_len = info->frameSize * info->numChannels *
99                          sizeof(a2dp_aac_decoder_cb.decode_buf[0]);
100       a2dp_aac_decoder_cb.decode_callback(
101           reinterpret_cast<uint8_t*>(a2dp_aac_decoder_cb.decode_buf),
102           frame_len);
103     }
104   }
105 
106   return true;
107 }
108