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(LOG_TAG,
49 "%s: OI_CODEC_SBC_DecoderReset failed with error code %d",
50 __func__, status);
51 return false;
52 }
53
54 a2dp_sbc_decoder_cb.decode_callback = decode_callback;
55 return true;
56 }
57
a2dp_sbc_decoder_cleanup(void)58 void a2dp_sbc_decoder_cleanup(void) {
59 // Do nothing.
60 }
61
a2dp_sbc_decoder_decode_packet(BT_HDR * p_buf)62 bool a2dp_sbc_decoder_decode_packet(BT_HDR* p_buf) {
63 uint8_t* data = p_buf->data + p_buf->offset;
64 size_t data_size = p_buf->len;
65
66 if (data_size == 0) {
67 LOG_ERROR(LOG_TAG, "%s: Empty packet", __func__);
68 return false;
69 }
70 size_t num_frames = data[0] & 0xf;
71 data += 1;
72 data_size -= 1;
73
74 const OI_BYTE* oi_data = data;
75 uint32_t oi_size = data_size;
76 size_t out_avail = sizeof(a2dp_sbc_decoder_cb.decode_buf);
77 int16_t* out_ptr = a2dp_sbc_decoder_cb.decode_buf;
78
79 for (size_t i = 0; i < num_frames; ++i) {
80 uint32_t out_size = out_avail;
81 OI_STATUS status =
82 OI_CODEC_SBC_DecodeFrame(&a2dp_sbc_decoder_cb.decoder_context, &oi_data,
83 &oi_size, out_ptr, &out_size);
84 if (!OI_SUCCESS(status)) {
85 LOG_ERROR(LOG_TAG, "%s: Decoding failure: %d", __func__, status);
86 return false;
87 }
88 out_avail -= out_size;
89 out_ptr += out_size / sizeof(*out_ptr);
90 }
91
92 size_t out_used =
93 (out_ptr - a2dp_sbc_decoder_cb.decode_buf) * sizeof(*out_ptr);
94 a2dp_sbc_decoder_cb.decode_callback(
95 reinterpret_cast<uint8_t*>(a2dp_sbc_decoder_cb.decode_buf), out_used);
96 return true;
97 }
98