1 /*
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 
21 namespace aidl::android::hardware::bluetooth::audio {
22 
23 /**
24  * AAC Local Capabilities
25  */
26 
27 enum : bool {
28   kEnableObjectTypeMpeg2AacLc = true,
29   kEnableObjectTypeMpeg4AacLc = true,
30 };
31 
32 enum : bool {
33   kEnableSamplingFrequency44100 = true,
34   kEnableSamplingFrequency48000 = true,
35   kEnableSamplingFrequency88200 = false,
36   kEnableSamplingFrequency96000 = false,
37 };
38 
39 enum : bool {
40   kEnableChannels1 = true,
41   kEnableChannels2 = true,
42 };
43 
44 enum : bool {
45   kEnableVbrSupported = true,
46 };
47 
48 enum : int {
49   kBitdepth = 24,
50 };
51 
52 /**
53  * AAC Signaling format [A2DP - 4.5]
54  */
55 
56 // clang-format off
57 
58 constexpr A2dpBits::Range kObjectType        (  0,  6 );
59 constexpr A2dpBits::Range kDrcEnable         (  7     );
60 constexpr A2dpBits::Range kSamplingFrequency (  8, 19 );
61 constexpr A2dpBits::Range kChannels          ( 20, 23 );
62 constexpr A2dpBits::Range kVbrSupported      ( 24     );
63 constexpr A2dpBits::Range kBitrate           ( 25, 47 );
64 constexpr size_t kCapabilitiesSize = 48/8;
65 
66 // clang-format on
67 
68 enum {
69   kObjectTypeMpeg2AacLc = kObjectType.first,
70   kObjectTypeMpeg4AacLc,
71   kObjectTypeMpeg4AacLtp,
72   kObjectTypeMpeg4AacScalable,
73   kObjectTypeMpeg4AacHeV1,
74   kObjectTypeMpeg4AacHeV2,
75   kObjectTypeMpeg4AacEldV2
76 };
77 
78 enum {
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 
93 enum { kChannels1 = kChannels.first, kChannels2, kChannels51, kChannels71 };
94 
95 /**
96  * AAC Conversion functions
97  */
98 
GetObjectTypeEnum(int object_type)99 static 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 
GetSamplingFrequencyBit(int32_t sampling_frequency)109 static 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 
GetSamplingFrequencyValue(int sampling_frequency)140 static 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 
GetChannelsBit(ChannelMode channel_mode)171 static 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 
GetChannelModeEnum(int channel_mode)182 static 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 
A2dpOffloadCodecAac()197 A2dpOffloadCodecAac::A2dpOffloadCodecAac()
198     : A2dpOffloadCodec(info_),
199       info_({.id = CodecId(CodecId::A2dp::AAC), .name = "AAC"}) {
200   info_.transport.set<CodecInfo::Transport::Tag::a2dp>();
201   auto& a2dp_info = info_.transport.get<CodecInfo::Transport::Tag::a2dp>();
202 
203   /* --- Setup Capabilities --- */
204 
205   a2dp_info.capabilities.resize(kCapabilitiesSize);
206   std::fill(begin(a2dp_info.capabilities), end(a2dp_info.capabilities), 0);
207 
208   auto capabilities = A2dpBits(a2dp_info.capabilities);
209 
210   capabilities.set(kObjectTypeMpeg2AacLc, kEnableObjectTypeMpeg2AacLc);
211   capabilities.set(kObjectTypeMpeg4AacLc, kEnableObjectTypeMpeg4AacLc);
212 
213   capabilities.set(kSamplingFrequency44100, kEnableSamplingFrequency44100);
214   capabilities.set(kSamplingFrequency48000, kEnableSamplingFrequency48000);
215   capabilities.set(kSamplingFrequency88200, kEnableSamplingFrequency88200);
216   capabilities.set(kSamplingFrequency96000, kEnableSamplingFrequency96000);
217 
218   capabilities.set(kChannels1, kEnableChannels1);
219   capabilities.set(kChannels2, kEnableChannels2);
220 
221   capabilities.set(kVbrSupported, kEnableVbrSupported);
222 
223   /* --- Setup Sampling Frequencies --- */
224 
225   auto& sampling_frequency = a2dp_info.samplingFrequencyHz;
226 
227   for (auto v : {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
228                  64000, 88200, 96000})
229     if (capabilities.get(GetSamplingFrequencyBit(int32_t(v))))
230       sampling_frequency.push_back(v);
231 
232   /* --- Setup Channel Modes --- */
233 
234   auto& channel_modes = a2dp_info.channelMode;
235 
236   for (auto v : {ChannelMode::MONO, ChannelMode::STEREO})
237     if (capabilities.get(GetChannelsBit(v))) channel_modes.push_back(v);
238 
239   /* --- Setup Bitdepth --- */
240 
241   a2dp_info.bitdepth.push_back(kBitdepth);
242 }
243 
ParseConfiguration(const std::vector<uint8_t> & configuration,CodecParameters * codec_parameters,AacParameters * aac_parameters) const244 A2dpStatus A2dpOffloadCodecAac::ParseConfiguration(
245     const std::vector<uint8_t>& configuration,
246     CodecParameters* codec_parameters, AacParameters* aac_parameters) const {
247   auto& a2dp_info = info.transport.get<CodecInfo::Transport::Tag::a2dp>();
248 
249   if (configuration.size() != a2dp_info.capabilities.size())
250     return A2dpStatus::BAD_LENGTH;
251 
252   auto config = A2dpBits(configuration);
253   auto lcaps = A2dpBits(a2dp_info.capabilities);
254 
255   /* --- Check Object Type --- */
256 
257   int object_type = config.find_active_bit(kObjectType);
258   if (object_type < 0) return A2dpStatus::INVALID_OBJECT_TYPE;
259   if (!lcaps.get(object_type)) return A2dpStatus::NOT_SUPPORTED_OBJECT_TYPE;
260 
261   /* --- Check Sampling Frequency --- */
262 
263   int sampling_frequency = config.find_active_bit(kSamplingFrequency);
264   if (sampling_frequency < 0) return A2dpStatus::INVALID_SAMPLING_FREQUENCY;
265   if (!lcaps.get(sampling_frequency))
266     return A2dpStatus::NOT_SUPPORTED_SAMPLING_FREQUENCY;
267 
268   /* --- Check Channels --- */
269 
270   int channels = config.find_active_bit(kChannels);
271   if (channels < 0) return A2dpStatus::INVALID_CHANNELS;
272   if (!lcaps.get(channels)) return A2dpStatus::NOT_SUPPORTED_CHANNELS;
273 
274   /* --- Check Bitrate --- */
275 
276   bool vbr = config.get(kVbrSupported);
277   if (vbr && !lcaps.get(kVbrSupported)) return A2dpStatus::NOT_SUPPORTED_VBR;
278 
279   int bitrate = config.get(kBitrate);
280   if (vbr && lcaps.get(kBitrate) && bitrate > lcaps.get(kBitrate))
281     return A2dpStatus::NOT_SUPPORTED_BIT_RATE;
282 
283   /* --- Return --- */
284 
285   codec_parameters->channelMode = GetChannelModeEnum(channels);
286   codec_parameters->samplingFrequencyHz =
287       GetSamplingFrequencyValue(sampling_frequency);
288   codec_parameters->bitdepth = kBitdepth;
289 
290   codec_parameters->minBitrate = vbr ? 0 : bitrate;
291   codec_parameters->maxBitrate = bitrate;
292 
293   if (aac_parameters)
294     aac_parameters->object_type = GetObjectTypeEnum(object_type);
295 
296   return A2dpStatus::OK;
297 }
298 
BuildConfiguration(const std::vector<uint8_t> & remote_capabilities,const std::optional<CodecParameters> & hint,std::vector<uint8_t> * configuration) const299 bool A2dpOffloadCodecAac::BuildConfiguration(
300     const std::vector<uint8_t>& remote_capabilities,
301     const std::optional<CodecParameters>& hint,
302     std::vector<uint8_t>* configuration) const {
303   auto& a2dp_info = info_.transport.get<CodecInfo::Transport::Tag::a2dp>();
304 
305   if (remote_capabilities.size() != a2dp_info.capabilities.size()) return false;
306 
307   auto lcaps = A2dpBits(a2dp_info.capabilities);
308   auto rcaps = A2dpBits(remote_capabilities);
309 
310   configuration->resize(a2dp_info.capabilities.size());
311   std::fill(begin(*configuration), end(*configuration), 0);
312   auto config = A2dpBits(*configuration);
313 
314   /* --- Select Object Type --- */
315 
316   if (lcaps.get(kObjectTypeMpeg2AacLc) && rcaps.get(kObjectTypeMpeg2AacLc))
317     config.set(kObjectTypeMpeg2AacLc);
318   else if (lcaps.get(kObjectTypeMpeg4AacLc) && rcaps.get(kObjectTypeMpeg4AacLc))
319     config.set(kObjectTypeMpeg4AacLc);
320   else
321     return false;
322 
323   /* --- Select Sampling Frequency --- */
324 
325   auto sf_hint = hint ? GetSamplingFrequencyBit(hint->samplingFrequencyHz) : -1;
326 
327   if (sf_hint >= 0 && lcaps.get(sf_hint) && rcaps.get(sf_hint))
328     config.set(sf_hint);
329   else if (lcaps.get(kSamplingFrequency96000) &&
330            rcaps.get(kSamplingFrequency96000))
331     config.set(kSamplingFrequency96000);
332   else if (lcaps.get(kSamplingFrequency88200) &&
333            rcaps.get(kSamplingFrequency88200))
334     config.set(kSamplingFrequency88200);
335   else if (lcaps.get(kSamplingFrequency48000) &&
336            rcaps.get(kSamplingFrequency48000))
337     config.set(kSamplingFrequency48000);
338   else if (lcaps.get(kSamplingFrequency44100) &&
339            rcaps.get(kSamplingFrequency44100))
340     config.set(kSamplingFrequency44100);
341   else
342     return false;
343 
344   /* --- Select Channels --- */
345 
346   auto ch_hint = hint ? GetChannelsBit(hint->channelMode) : -1;
347 
348   if (ch_hint >= 0 && lcaps.get(ch_hint) && rcaps.get(ch_hint))
349     config.set(ch_hint);
350   else if (lcaps.get(kChannels2) && rcaps.get(kChannels2))
351     config.set(kChannels2);
352   else if (lcaps.get(kChannels1) && rcaps.get(kChannels1))
353     config.set(kChannels1);
354   else
355     return false;
356 
357   /* --- Select Bitrate --- */
358 
359   if (!hint || hint->minBitrate == 0)
360     config.set(kVbrSupported,
361                lcaps.get(kVbrSupported) && rcaps.get(kVbrSupported));
362 
363   int32_t bitrate = lcaps.get(kBitrate);
364   if (hint && hint->maxBitrate > 0 && bitrate)
365     bitrate = std::min(hint->maxBitrate, bitrate);
366   else if (hint && hint->maxBitrate > 0)
367     bitrate = hint->maxBitrate;
368   config.set(kBitrate, bitrate);
369 
370   return true;
371 }
372 
373 }  // namespace aidl::android::hardware::bluetooth::audio
374