• 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 #include "stack/include/bt_hdr.h"
28 
29 #define DECODE_BUF_LEN (8 * 2 * 1024)
30 
31 typedef struct {
32   HANDLE_AACDECODER aac_handle;
33   bool has_aac_handle;  // True if aac_handle is valid
34   INT_PCM* decode_buf = nullptr;
35   decoded_data_callback_t decode_callback;
36 } tA2DP_AAC_DECODER_CB;
37 
38 static tA2DP_AAC_DECODER_CB a2dp_aac_decoder_cb;
39 
A2DP_LoadDecoderAac(void)40 bool A2DP_LoadDecoderAac(void) {
41   // Nothing to do - the library is statically linked
42   return true;
43 }
44 
A2DP_UnloadDecoderAac(void)45 void A2DP_UnloadDecoderAac(void) { a2dp_aac_decoder_cleanup(); }
46 
a2dp_aac_decoder_init(decoded_data_callback_t decode_callback)47 bool a2dp_aac_decoder_init(decoded_data_callback_t decode_callback) {
48   a2dp_aac_decoder_cleanup();
49 
50   a2dp_aac_decoder_cb.aac_handle =
51       aacDecoder_Open(TT_MP4_LATM_MCP1, 1 /* nrOfLayers */);
52   a2dp_aac_decoder_cb.has_aac_handle = true;
53   a2dp_aac_decoder_cb.decode_buf = static_cast<INT_PCM*>(
54       osi_malloc(sizeof(a2dp_aac_decoder_cb.decode_buf[0]) * DECODE_BUF_LEN));
55   a2dp_aac_decoder_cb.decode_callback = decode_callback;
56   return true;
57 }
58 
a2dp_aac_decoder_cleanup(void)59 void a2dp_aac_decoder_cleanup(void) {
60   if (a2dp_aac_decoder_cb.has_aac_handle)
61     aacDecoder_Close(a2dp_aac_decoder_cb.aac_handle);
62   osi_free(a2dp_aac_decoder_cb.decode_buf);
63   memset(&a2dp_aac_decoder_cb, 0, sizeof(a2dp_aac_decoder_cb));
64 }
65 
a2dp_aac_decoder_decode_packet(BT_HDR * p_buf)66 bool a2dp_aac_decoder_decode_packet(BT_HDR* p_buf) {
67   auto* pBuffer = reinterpret_cast<UCHAR*>(p_buf->data + p_buf->offset);
68   UINT bufferSize = p_buf->len;
69   UINT bytesValid = p_buf->len;
70   while (bytesValid > 0) {
71     AAC_DECODER_ERROR err = aacDecoder_Fill(a2dp_aac_decoder_cb.aac_handle,
72                                             &pBuffer, &bufferSize, &bytesValid);
73     if (err != AAC_DEC_OK) {
74       LOG_ERROR("%s: aacDecoder_Fill failed: 0x%x", __func__,
75                 static_cast<unsigned>(err));
76       return false;
77     }
78 
79     while (true) {
80       err = aacDecoder_DecodeFrame(a2dp_aac_decoder_cb.aac_handle,
81                                    a2dp_aac_decoder_cb.decode_buf,
82                                    DECODE_BUF_LEN, 0 /* flags */);
83       if (err == AAC_DEC_NOT_ENOUGH_BITS) {
84         break;
85       }
86       if (err != AAC_DEC_OK) {
87         LOG_ERROR("%s: aacDecoder_DecodeFrame failed: 0x%x", __func__,
88                   static_cast<int>(err));
89         break;
90       }
91 
92       CStreamInfo* info =
93           aacDecoder_GetStreamInfo(a2dp_aac_decoder_cb.aac_handle);
94       if (!info || info->sampleRate <= 0) {
95         LOG_ERROR("%s: Invalid stream info", __func__);
96         break;
97       }
98 
99       size_t frame_len = info->frameSize * info->numChannels *
100                          sizeof(a2dp_aac_decoder_cb.decode_buf[0]);
101       a2dp_aac_decoder_cb.decode_callback(
102           reinterpret_cast<uint8_t*>(a2dp_aac_decoder_cb.decode_buf),
103           frame_len);
104     }
105   }
106 
107   return true;
108 }
109