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