blob: 0f5533a4d9de44b914ed0f11047ab716f2ea2fd1 [file] [log] [blame]
Antoine SOULIER4e34d052023-09-29 19:10:07 +00001/*
2 * Copyright (C) 2023 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#include "A2dpOffloadCodecAac.h"
18
19#include "A2dpBits.h"
20
21namespace aidl::android::hardware::bluetooth::audio {
22
23/**
24 * AAC Local Capabilities
25 */
26
27enum : bool {
28 kEnableObjectTypeMpeg2AacLc = true,
29 kEnableObjectTypeMpeg4AacLc = true,
30};
31
32enum : bool {
33 kEnableSamplingFrequency44100 = true,
34 kEnableSamplingFrequency48000 = true,
35 kEnableSamplingFrequency88200 = false,
36 kEnableSamplingFrequency96000 = false,
37};
38
39enum : bool {
40 kEnableChannels1 = true,
41 kEnableChannels2 = true,
42};
43
44enum : bool {
45 kEnableVbrSupported = true,
46};
47
48enum : int {
49 kBitdepth = 24,
50};
51
52/**
53 * AAC Signaling format [A2DP - 4.5]
54 */
55
56// clang-format off
57
58constexpr A2dpBits::Range kObjectType ( 0, 6 );
59constexpr A2dpBits::Range kDrcEnable ( 7 );
60constexpr A2dpBits::Range kSamplingFrequency ( 8, 19 );
61constexpr A2dpBits::Range kChannels ( 20, 23 );
62constexpr A2dpBits::Range kVbrSupported ( 24 );
63constexpr A2dpBits::Range kBitrate ( 25, 47 );
64constexpr size_t kCapabilitiesSize = 48/8;
65
66// clang-format on
67
68enum {
69 kObjectTypeMpeg2AacLc = kObjectType.first,
70 kObjectTypeMpeg4AacLc,
71 kObjectTypeMpeg4AacLtp,
72 kObjectTypeMpeg4AacScalable,
73 kObjectTypeMpeg4AacHeV1,
74 kObjectTypeMpeg4AacHeV2,
75 kObjectTypeMpeg4AacEldV2
76};
77
78enum {
79 kSamplingFrequency8000 = kSamplingFrequency.first,
80 kSamplingFrequency11025,
81 kSamplingFrequency12000,
82 kSamplingFrequency16000,
83 kSamplingFrequency22050,
84 kSamplingFrequency24000,
85 kSamplingFrequency32000,
86 kSamplingFrequency44100,
87 kSamplingFrequency48000,
88 kSamplingFrequency64000,
89 kSamplingFrequency88200,
90 kSamplingFrequency96000
91};
92
93enum { kChannels1 = kChannels.first, kChannels2, kChannels51, kChannels71 };
94
95/**
96 * AAC Conversion functions
97 */
98
99static AacParameters::ObjectType GetObjectTypeEnum(int object_type) {
100 switch (object_type) {
101 case kObjectTypeMpeg2AacLc:
102 return AacParameters::ObjectType::MPEG2_AAC_LC;
103 case kObjectTypeMpeg4AacLc:
104 default:
105 return AacParameters::ObjectType::MPEG4_AAC_LC;
106 }
107}
108
109static int GetSamplingFrequencyBit(int32_t sampling_frequency) {
110 switch (sampling_frequency) {
111 case 8000:
112 return kSamplingFrequency8000;
113 case 11025:
114 return kSamplingFrequency11025;
115 case 12000:
116 return kSamplingFrequency12000;
117 case 16000:
118 return kSamplingFrequency16000;
119 case 22050:
120 return kSamplingFrequency22050;
121 case 24000:
122 return kSamplingFrequency24000;
123 case 32000:
124 return kSamplingFrequency32000;
125 case 44100:
126 return kSamplingFrequency44100;
127 case 48000:
128 return kSamplingFrequency48000;
129 case 64000:
130 return kSamplingFrequency64000;
131 case 88200:
132 return kSamplingFrequency88200;
133 case 96000:
134 return kSamplingFrequency96000;
135 default:
136 return -1;
137 }
138}
139
140static int32_t GetSamplingFrequencyValue(int sampling_frequency) {
141 switch (sampling_frequency) {
142 case kSamplingFrequency8000:
143 return 8000;
144 case kSamplingFrequency11025:
145 return 11025;
146 case kSamplingFrequency12000:
147 return 12000;
148 case kSamplingFrequency16000:
149 return 16000;
150 case kSamplingFrequency22050:
151 return 22050;
152 case kSamplingFrequency24000:
153 return 24000;
154 case kSamplingFrequency32000:
155 return 32000;
156 case kSamplingFrequency44100:
157 return 44100;
158 case kSamplingFrequency48000:
159 return 48000;
160 case kSamplingFrequency64000:
161 return 64000;
162 case kSamplingFrequency88200:
163 return 88200;
164 case kSamplingFrequency96000:
165 return 96000;
166 default:
167 return 0;
168 }
169}
170
171static int GetChannelsBit(ChannelMode channel_mode) {
172 switch (channel_mode) {
173 case ChannelMode::MONO:
174 return kChannels1;
175 case ChannelMode::STEREO:
176 return kChannels2;
177 default:
178 return -1;
179 }
180}
181
182static ChannelMode GetChannelModeEnum(int channel_mode) {
183 switch (channel_mode) {
184 case kChannels1:
185 return ChannelMode::MONO;
186 case kChannels2:
187 return ChannelMode::STEREO;
188 default:
189 return ChannelMode::UNKNOWN;
190 }
191}
192
193/**
194 * AAC Class implementation
195 */
196
197const A2dpOffloadCodecAac* A2dpOffloadCodecAac::GetInstance() {
198 static A2dpOffloadCodecAac instance;
199 return &instance;
200}
201
202A2dpOffloadCodecAac::A2dpOffloadCodecAac()
203 : A2dpOffloadCodec(info_),
204 info_({.id = CodecId(CodecId::A2dp::AAC), .name = "AAC"}) {
205 info_.transport.set<CodecInfo::Transport::Tag::a2dp>();
206 auto& a2dp_info = info_.transport.get<CodecInfo::Transport::Tag::a2dp>();
207
208 /* --- Setup Capabilities --- */
209
210 a2dp_info.capabilities.resize(kCapabilitiesSize);
211 std::fill(begin(a2dp_info.capabilities), end(a2dp_info.capabilities), 0);
212
213 auto capabilities = A2dpBits(a2dp_info.capabilities);
214
215 capabilities.set(kObjectTypeMpeg2AacLc, kEnableObjectTypeMpeg2AacLc);
216 capabilities.set(kObjectTypeMpeg4AacLc, kEnableObjectTypeMpeg4AacLc);
217
218 capabilities.set(kSamplingFrequency44100, kEnableSamplingFrequency44100);
219 capabilities.set(kSamplingFrequency48000, kEnableSamplingFrequency48000);
220 capabilities.set(kSamplingFrequency88200, kEnableSamplingFrequency88200);
221 capabilities.set(kSamplingFrequency96000, kEnableSamplingFrequency96000);
222
223 capabilities.set(kChannels1, kEnableChannels1);
224 capabilities.set(kChannels2, kEnableChannels2);
225
226 capabilities.set(kVbrSupported, kEnableVbrSupported);
227
228 /* --- Setup Sampling Frequencies --- */
229
230 auto& sampling_frequency = a2dp_info.samplingFrequencyHz;
231
232 for (auto v : {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
233 64000, 88200, 96000})
234 if (capabilities.get(GetSamplingFrequencyBit(int32_t(v))))
235 sampling_frequency.push_back(v);
236
237 /* --- Setup Channel Modes --- */
238
239 auto& channel_modes = a2dp_info.channelMode;
240
241 for (auto v : {ChannelMode::MONO, ChannelMode::STEREO})
242 if (capabilities.get(GetChannelsBit(v))) channel_modes.push_back(v);
243
244 /* --- Setup Bitdepth --- */
245
246 a2dp_info.bitdepth.push_back(kBitdepth);
247}
248
249A2dpStatus A2dpOffloadCodecAac::ParseConfiguration(
250 const std::vector<uint8_t>& configuration,
251 CodecParameters* codec_parameters, AacParameters* aac_parameters) const {
252 auto& a2dp_info = info.transport.get<CodecInfo::Transport::Tag::a2dp>();
253
254 if (configuration.size() != a2dp_info.capabilities.size())
255 return A2dpStatus::BAD_LENGTH;
256
257 auto config = A2dpBits(configuration);
258 auto lcaps = A2dpBits(a2dp_info.capabilities);
259
260 /* --- Check Object Type --- */
261
262 int object_type = config.find_active_bit(kObjectType);
263 if (object_type < 0) return A2dpStatus::INVALID_OBJECT_TYPE;
264 if (!lcaps.get(object_type)) return A2dpStatus::NOT_SUPPORTED_OBJECT_TYPE;
265
266 /* --- Check Sampling Frequency --- */
267
268 int sampling_frequency = config.find_active_bit(kSamplingFrequency);
269 if (sampling_frequency < 0) return A2dpStatus::INVALID_SAMPLING_FREQUENCY;
270 if (!lcaps.get(sampling_frequency))
271 return A2dpStatus::NOT_SUPPORTED_SAMPLING_FREQUENCY;
272
273 /* --- Check Channels --- */
274
275 int channels = config.find_active_bit(kChannels);
276 if (channels < 0) return A2dpStatus::INVALID_CHANNELS;
277 if (!lcaps.get(channels)) return A2dpStatus::NOT_SUPPORTED_CHANNELS;
278
279 /* --- Check Bitrate --- */
280
281 bool vbr = config.get(kVbrSupported);
282 if (vbr && !lcaps.get(kVbrSupported)) return A2dpStatus::NOT_SUPPORTED_VBR;
283
284 int bitrate = config.get(kBitrate);
285 if (vbr && lcaps.get(kBitrate) && bitrate > lcaps.get(kBitrate))
286 return A2dpStatus::NOT_SUPPORTED_BIT_RATE;
287
288 /* --- Return --- */
289
290 codec_parameters->channelMode = GetChannelModeEnum(channels);
291 codec_parameters->samplingFrequencyHz =
292 GetSamplingFrequencyValue(sampling_frequency);
293 codec_parameters->bitdepth = kBitdepth;
294
295 codec_parameters->minBitrate = vbr ? 0 : bitrate;
296 codec_parameters->maxBitrate = bitrate;
297
298 if (aac_parameters)
299 aac_parameters->object_type = GetObjectTypeEnum(object_type);
300
301 return A2dpStatus::OK;
302}
303
304bool A2dpOffloadCodecAac::BuildConfiguration(
305 const std::vector<uint8_t>& remote_capabilities,
306 const std::optional<CodecParameters>& hint,
307 std::vector<uint8_t>* configuration) const {
308 auto& a2dp_info = info_.transport.get<CodecInfo::Transport::Tag::a2dp>();
309
310 if (remote_capabilities.size() != a2dp_info.capabilities.size()) return false;
311
312 auto lcaps = A2dpBits(a2dp_info.capabilities);
313 auto rcaps = A2dpBits(remote_capabilities);
314
315 configuration->resize(a2dp_info.capabilities.size());
316 std::fill(begin(*configuration), end(*configuration), 0);
317 auto config = A2dpBits(*configuration);
318
319 /* --- Select Object Type --- */
320
321 if (lcaps.get(kObjectTypeMpeg2AacLc) && rcaps.get(kObjectTypeMpeg2AacLc))
322 config.set(kObjectTypeMpeg2AacLc);
323 else if (lcaps.get(kObjectTypeMpeg4AacLc) && rcaps.get(kObjectTypeMpeg4AacLc))
324 config.set(kObjectTypeMpeg4AacLc);
325 else
326 return false;
327
328 /* --- Select Sampling Frequency --- */
329
330 auto sf_hint = hint ? GetSamplingFrequencyBit(hint->samplingFrequencyHz) : -1;
331
332 if (sf_hint >= 0 && lcaps.get(sf_hint) && rcaps.get(sf_hint))
333 config.set(sf_hint);
334 else if (lcaps.get(kSamplingFrequency96000) &&
335 rcaps.get(kSamplingFrequency96000))
336 config.set(kSamplingFrequency96000);
337 else if (lcaps.get(kSamplingFrequency88200) &&
338 rcaps.get(kSamplingFrequency88200))
339 config.set(kSamplingFrequency88200);
340 else if (lcaps.get(kSamplingFrequency48000) &&
341 rcaps.get(kSamplingFrequency48000))
342 config.set(kSamplingFrequency48000);
343 else if (lcaps.get(kSamplingFrequency44100) &&
344 rcaps.get(kSamplingFrequency44100))
345 config.set(kSamplingFrequency44100);
346 else
347 return false;
348
349 /* --- Select Channels --- */
350
351 auto ch_hint = hint ? GetChannelsBit(hint->channelMode) : -1;
352
353 if (ch_hint >= 0 && lcaps.get(ch_hint) && rcaps.get(ch_hint))
354 config.set(ch_hint);
355 else if (lcaps.get(kChannels2) && rcaps.get(kChannels2))
356 config.set(kChannels2);
357 else if (lcaps.get(kChannels1) && rcaps.get(kChannels1))
358 config.set(kChannels1);
359 else
360 return false;
361
362 /* --- Select Bitrate --- */
363
364 if (!hint || hint->minBitrate == 0)
365 config.set(kVbrSupported,
366 lcaps.get(kVbrSupported) && rcaps.get(kVbrSupported));
367
368 int32_t bitrate = lcaps.get(kBitrate);
369 if (hint && hint->maxBitrate > 0 && bitrate)
370 bitrate = std::min(hint->maxBitrate, bitrate);
371 else if (hint && hint->maxBitrate > 0)
372 bitrate = hint->maxBitrate;
373 config.set(kBitrate, bitrate);
374
375 return true;
376}
377
378} // namespace aidl::android::hardware::bluetooth::audio