1 /*
2  * Copyright (C) 2021 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_vendor_opus_decoder.h"
20 
21 #include <bluetooth/log.h>
22 #include <opus.h>
23 
24 #include "a2dp_vendor_opus.h"
25 #include "os/log.h"
26 #include "osi/include/allocator.h"
27 
28 using namespace bluetooth;
29 
30 typedef struct {
31   OpusDecoder* opus_handle = nullptr;
32   bool has_opus_handle;
33   int16_t* decode_buf = nullptr;
34   decoded_data_callback_t decode_callback;
35 } tA2DP_OPUS_DECODER_CB;
36 
37 static tA2DP_OPUS_DECODER_CB a2dp_opus_decoder_cb;
38 
a2dp_vendor_opus_decoder_cleanup(void)39 void a2dp_vendor_opus_decoder_cleanup(void) {
40   if (a2dp_opus_decoder_cb.has_opus_handle) {
41     osi_free(a2dp_opus_decoder_cb.opus_handle);
42 
43     if (a2dp_opus_decoder_cb.decode_buf != nullptr) {
44       memset(a2dp_opus_decoder_cb.decode_buf, 0,
45              A2DP_OPUS_DECODE_BUFFER_LENGTH);
46       osi_free(a2dp_opus_decoder_cb.decode_buf);
47       a2dp_opus_decoder_cb.decode_buf = nullptr;
48     }
49     a2dp_opus_decoder_cb.has_opus_handle = false;
50   }
51 
52   return;
53 }
54 
a2dp_vendor_opus_decoder_init(decoded_data_callback_t decode_callback)55 bool a2dp_vendor_opus_decoder_init(decoded_data_callback_t decode_callback) {
56   a2dp_vendor_opus_decoder_cleanup();
57 
58   int32_t err_val = OPUS_OK;
59   int32_t size = 0;
60 
61   size = opus_decoder_get_size(A2DP_OPUS_CODEC_OUTPUT_CHS);
62   a2dp_opus_decoder_cb.opus_handle =
63       static_cast<OpusDecoder*>(osi_malloc(size));
64   if (a2dp_opus_decoder_cb.opus_handle == nullptr) {
65     log::error("failed to allocate opus decoder handle");
66     return false;
67   }
68   err_val = opus_decoder_init(a2dp_opus_decoder_cb.opus_handle,
69                               A2DP_OPUS_CODEC_DEFAULT_SAMPLERATE,
70                               A2DP_OPUS_CODEC_OUTPUT_CHS);
71   if (err_val == OPUS_OK) {
72     a2dp_opus_decoder_cb.has_opus_handle = true;
73 
74     a2dp_opus_decoder_cb.decode_buf =
75         static_cast<int16_t*>(osi_malloc(A2DP_OPUS_DECODE_BUFFER_LENGTH));
76 
77     memset(a2dp_opus_decoder_cb.decode_buf, 0, A2DP_OPUS_DECODE_BUFFER_LENGTH);
78 
79     a2dp_opus_decoder_cb.decode_callback = decode_callback;
80     log::info("decoder init success");
81     return true;
82   } else {
83     log::error("failed to initialize Opus Decoder");
84     a2dp_opus_decoder_cb.has_opus_handle = false;
85     return false;
86   }
87 
88   return false;
89 }
90 
a2dp_vendor_opus_decoder_configure(const uint8_t * p_codec_info)91 void a2dp_vendor_opus_decoder_configure(const uint8_t* p_codec_info) { return; }
92 
a2dp_vendor_opus_decoder_decode_packet(BT_HDR * p_buf)93 bool a2dp_vendor_opus_decoder_decode_packet(BT_HDR* p_buf) {
94   uint32_t frameSize;
95   uint32_t numChannels;
96   uint32_t numFrames;
97   int32_t ret_val = 0;
98   uint32_t frameLen = 0;
99 
100   if (p_buf == nullptr) {
101     log::error("Dropping packet with nullptr");
102     return false;
103   }
104 
105   if (p_buf->len == 0) {
106     log::error("Empty packet");
107     return false;
108   }
109 
110   auto* pBuffer =
111       reinterpret_cast<unsigned char*>(p_buf->data + p_buf->offset + 1);
112   int32_t bufferSize = p_buf->len - 1;
113 
114   numChannels = opus_packet_get_nb_channels(pBuffer);
115   numFrames = opus_packet_get_nb_frames(pBuffer, bufferSize);
116   frameSize = opus_packet_get_samples_per_frame(
117       pBuffer, A2DP_OPUS_CODEC_DEFAULT_SAMPLERATE);
118   frameLen = opus_packet_get_nb_samples(pBuffer, bufferSize,
119                                         A2DP_OPUS_CODEC_DEFAULT_SAMPLERATE);
120   uint32_t num_frames = pBuffer[0] & 0xf;
121 
122   log::error("numframes {} framesize {} framelen {} bufferSize {}", num_frames,
123              frameSize, frameLen, bufferSize);
124   log::error("numChannels {} numFrames {} offset {}", numChannels, numFrames,
125              p_buf->offset);
126 
127   for (uint32_t frame = 0; frame < numFrames; ++frame) {
128     {
129       numChannels = opus_packet_get_nb_channels(pBuffer);
130 
131       ret_val = opus_decode(a2dp_opus_decoder_cb.opus_handle,
132                             reinterpret_cast<unsigned char*>(pBuffer),
133                             bufferSize, a2dp_opus_decoder_cb.decode_buf,
134                             A2DP_OPUS_DECODE_BUFFER_LENGTH, 0 /* flags */);
135 
136       if (ret_val < OPUS_OK) {
137         log::error("Opus DecodeFrame failed {}, applying concealment", ret_val);
138         ret_val = opus_decode(a2dp_opus_decoder_cb.opus_handle, NULL, 0,
139                               a2dp_opus_decoder_cb.decode_buf,
140                               A2DP_OPUS_DECODE_BUFFER_LENGTH, 0 /* flags */);
141       }
142 
143       if (ret_val < OPUS_OK) {
144         log::error("Opus DecodeFrame retry failed with {}, dropping packet",
145                    ret_val);
146         return false;
147       }
148 
149       size_t frame_len =
150           ret_val * numChannels * sizeof(a2dp_opus_decoder_cb.decode_buf[0]);
151       a2dp_opus_decoder_cb.decode_callback(
152           reinterpret_cast<uint8_t*>(a2dp_opus_decoder_cb.decode_buf),
153           frame_len);
154     }
155   }
156   return true;
157 }
158 
a2dp_vendor_opus_decoder_start(void)159 void a2dp_vendor_opus_decoder_start(void) { return; }
160 
a2dp_vendor_opus_decoder_suspend(void)161 void a2dp_vendor_opus_decoder_suspend(void) {
162   int32_t err_val = 0;
163 
164   if (a2dp_opus_decoder_cb.has_opus_handle) {
165     err_val =
166         opus_decoder_ctl(a2dp_opus_decoder_cb.opus_handle, OPUS_RESET_STATE);
167     if (err_val != OPUS_OK) {
168       log::error("failed to reset decoder");
169     }
170   }
171   return;
172 }
173