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_sbc_decoder"
18
19 #include "a2dp_sbc_decoder.h"
20
21 #include <base/logging.h>
22
23 #include "embdrv/sbc/decoder/include/oi_codec_sbc.h"
24 #include "embdrv/sbc/decoder/include/oi_status.h"
25 #include "osi/include/log.h"
26
27 typedef struct {
28 OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
29 uint32_t context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
30 int16_t decode_buf[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
31 decoded_data_callback_t decode_callback;
32 } tA2DP_SBC_DECODER_CB;
33
34 static tA2DP_SBC_DECODER_CB a2dp_sbc_decoder_cb;
35
A2DP_LoadDecoderSbc(void)36 bool A2DP_LoadDecoderSbc(void) {
37 // Nothing to do - the library is statically linked
38 return true;
39 }
40
A2DP_UnloadDecoderSbc(void)41 void A2DP_UnloadDecoderSbc(void) { a2dp_sbc_decoder_cleanup(); }
42
a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback)43 bool a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback) {
44 OI_STATUS status = OI_CODEC_SBC_DecoderReset(
45 &a2dp_sbc_decoder_cb.decoder_context, a2dp_sbc_decoder_cb.context_data,
46 sizeof(a2dp_sbc_decoder_cb.context_data), 2, 2, false);
47 if (!OI_SUCCESS(status)) {
48 LOG_ERROR("%s: OI_CODEC_SBC_DecoderReset failed with error code %d",
49 __func__, status);
50 return false;
51 }
52
53 a2dp_sbc_decoder_cb.decode_callback = decode_callback;
54 return true;
55 }
56
a2dp_sbc_decoder_cleanup(void)57 void a2dp_sbc_decoder_cleanup(void) {
58 // Do nothing.
59 }
60
a2dp_sbc_decoder_decode_packet(BT_HDR * p_buf)61 bool a2dp_sbc_decoder_decode_packet(BT_HDR* p_buf) {
62 uint8_t* data = p_buf->data + p_buf->offset;
63 size_t data_size = p_buf->len;
64
65 if (data_size == 0) {
66 LOG_ERROR("%s: Empty packet", __func__);
67 return false;
68 }
69 size_t num_frames = data[0] & 0xf;
70 data += 1;
71 data_size -= 1;
72
73 const OI_BYTE* oi_data = data;
74 uint32_t oi_size = data_size;
75 size_t out_avail = sizeof(a2dp_sbc_decoder_cb.decode_buf);
76 int16_t* out_ptr = a2dp_sbc_decoder_cb.decode_buf;
77
78 for (size_t i = 0; i < num_frames; ++i) {
79 uint32_t out_size = out_avail;
80 OI_STATUS status =
81 OI_CODEC_SBC_DecodeFrame(&a2dp_sbc_decoder_cb.decoder_context, &oi_data,
82 &oi_size, out_ptr, &out_size);
83 if (!OI_SUCCESS(status)) {
84 LOG_ERROR("%s: Decoding failure: %d", __func__, status);
85 return false;
86 }
87 out_avail -= out_size;
88 out_ptr += out_size / sizeof(*out_ptr);
89 }
90
91 size_t out_used =
92 (out_ptr - a2dp_sbc_decoder_cb.decode_buf) * sizeof(*out_ptr);
93 a2dp_sbc_decoder_cb.decode_callback(
94 reinterpret_cast<uint8_t*>(a2dp_sbc_decoder_cb.decode_buf), out_used);
95 return true;
96 }
97