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 "bluetooth-a2dp"
18
19 #include "a2dp_aac_decoder.h"
20
21 #include <aacdecoder_lib.h>
22 #include <bluetooth/log.h>
23
24 #include "os/log.h"
25 #include "osi/include/allocator.h"
26 #include "stack/include/bt_hdr.h"
27
28 #define DECODE_BUF_LEN (8 * 2 * 1024)
29
30 using namespace bluetooth;
31
32 typedef struct {
33 HANDLE_AACDECODER aac_handle;
34 bool has_aac_handle; // True if aac_handle is valid
35 INT_PCM* decode_buf = nullptr;
36 decoded_data_callback_t decode_callback;
37 } tA2DP_AAC_DECODER_CB;
38
39 static tA2DP_AAC_DECODER_CB a2dp_aac_decoder_cb;
40
A2DP_LoadDecoderAac(void)41 bool A2DP_LoadDecoderAac(void) {
42 // Nothing to do - the library is statically linked
43 return true;
44 }
45
A2DP_UnloadDecoderAac(void)46 void A2DP_UnloadDecoderAac(void) { a2dp_aac_decoder_cleanup(); }
47
a2dp_aac_decoder_init(decoded_data_callback_t decode_callback)48 bool a2dp_aac_decoder_init(decoded_data_callback_t decode_callback) {
49 a2dp_aac_decoder_cleanup();
50
51 a2dp_aac_decoder_cb.aac_handle =
52 aacDecoder_Open(TT_MP4_LATM_MCP1, 1 /* nrOfLayers */);
53 a2dp_aac_decoder_cb.has_aac_handle = true;
54 a2dp_aac_decoder_cb.decode_buf = static_cast<INT_PCM*>(
55 osi_malloc(sizeof(a2dp_aac_decoder_cb.decode_buf[0]) * DECODE_BUF_LEN));
56 a2dp_aac_decoder_cb.decode_callback = decode_callback;
57 return true;
58 }
59
a2dp_aac_decoder_cleanup(void)60 void a2dp_aac_decoder_cleanup(void) {
61 if (a2dp_aac_decoder_cb.has_aac_handle)
62 aacDecoder_Close(a2dp_aac_decoder_cb.aac_handle);
63 osi_free(a2dp_aac_decoder_cb.decode_buf);
64 memset(&a2dp_aac_decoder_cb, 0, sizeof(a2dp_aac_decoder_cb));
65 }
66
a2dp_aac_decoder_decode_packet(BT_HDR * p_buf)67 bool a2dp_aac_decoder_decode_packet(BT_HDR* p_buf) {
68 auto* pBuffer = reinterpret_cast<UCHAR*>(p_buf->data + p_buf->offset);
69 UINT bufferSize = p_buf->len;
70 UINT bytesValid = p_buf->len;
71 while (bytesValid > 0) {
72 AAC_DECODER_ERROR err = aacDecoder_Fill(a2dp_aac_decoder_cb.aac_handle,
73 &pBuffer, &bufferSize, &bytesValid);
74 if (err != AAC_DEC_OK) {
75 log::error("aacDecoder_Fill failed: 0x{:x}", 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("aacDecoder_DecodeFrame failed: 0x{:x}",
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("Invalid stream info");
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