1 /**
2 * Copyright (C) 2022 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 <ImsMediaAudioUtil.h>
18 #include <ImsMediaDefine.h>
19 #include <AudioConfig.h>
20 #include <ImsMediaTrace.h>
21 #include <string.h>
22
23 #define MAX_AMR_MODE 8
24 #define MAX_EVS_MODE 20
25
26 static const uint32_t gaAMRWBLen[32] = {
27 17, // 6.6
28 23, // 8.85
29 32, // 12.65
30 36, // 14.25
31 40, // 15.85
32 46, // 18.25
33 50, // 19.85
34 58, // 23.05
35 60, // 23.85
36 5, // SID
37 0,
38 };
39
40 static const uint32_t gaAMRWBbitLen[32] = {
41 132, // 6.6
42 177, // 8.85
43 253, // 12.65
44 285, // 14.25
45 317, // 15.85
46 365, // 18.25
47 397, // 19.85
48 461, // 23.05
49 477, // 23.85
50 40, // SID
51 0,
52 };
53
54 static const uint32_t gaEVSPrimaryByteLen[32] = {
55 7, // 2.8 special case
56 18, // 7.2
57 20, // 8.0
58 24, // 9.6
59 33, // 13.2
60 41, // 16.4
61 61, // 24.4
62 80, // 32.0
63 120, // 48.0
64 160, // 64.0
65 240, // 96.0
66 320, // 128.0
67 6, // SID
68 0,
69 };
70
71 static const uint32_t gaEVSPrimaryHeaderFullByteLen[32] = {
72 8, // 2.8 special case
73 19, // 7.2
74 21, // 8.0
75 25, // 9.6
76 34, // 13.2
77 42, // 16.4
78 62, // 24.4
79 81, // 32.0
80 121, // 48.0
81 161, // 64.0
82 241, // 96.0
83 321, // 128.0
84 7, // SID
85 };
86
87 static const uint32_t gaEVSPrimaryBitLen[32] = {
88 56, // 2.8 Special case
89 144, // 7.2
90 160, // 8.0
91 192, // 9.6
92 264, // 13.2
93 328, // 16.4
94 488, // 24.4
95 640, // 32.0
96 960, // 48.0
97 1280, // 64.0
98 1920, // 96.0
99 2560, // 128.0
100 48, // SID
101 0,
102 };
103
104 static const uint32_t gaEVSAMRWBIOLen[32] = {
105 17, // 6.6
106 23, // 8.85
107 32, // 12.65
108 36, // 14.25
109 40, // 15.85
110 46, // 18.25
111 50, // 19.85
112 58, // 23.05
113 60, // 23.85
114 5, // SID
115 0,
116 };
117
118 static const uint32_t gaEVSAmrWbIoBitLen[32] = {
119 136, // 6.6 AMR-WB IO
120 184, // 8.85 AMR-WB IO
121 256, // 12.65 AMR-WB IO
122 288, // 14.25 AMR-WB IO
123 320, // 15.85 AMR-WB IO
124 368, // 18.25 AMR-WB IO
125 400, // 19.85 AMR-WB IO
126 464, // 23.05 AMR-WB IO
127 480, // 23.85 AMR-WB IO
128 40, // SID for AMR-WB IO
129 0, /* Note that no Compact frame format EVS AMR-WB IO SID frames is defined.
130 For such frames the Header-Full format with CMR byte shall be used*/
131 };
132
133 static const uint32_t gaAMRLen[16] = {
134 12, // 4.75
135 13, // 5.15
136 15, // 5.90
137 17, // 6.70
138 19, // 7.40
139 20, // 7.95
140 26, // 10.20
141 31, // 12.20
142 5, // SID
143 0,
144 };
145
146 static const uint32_t gaAMRBitLen[16] = {
147 95, // 4.75
148 103, // 5.15
149 118, // 5.90
150 134, // 6.70
151 148, // 7.40
152 159, // 7.95
153 204, // 10.20
154 244, // 12.20
155 39, // SID
156 0,
157 };
158
ConvertCodecType(int32_t type)159 int32_t ImsMediaAudioUtil::ConvertCodecType(int32_t type)
160 {
161 switch (type)
162 {
163 default:
164 case AudioConfig::CODEC_AMR:
165 return kAudioCodecAmr;
166 case AudioConfig::CODEC_AMR_WB:
167 return kAudioCodecAmrWb;
168 case AudioConfig::CODEC_EVS:
169 return kAudioCodecEvs;
170 case AudioConfig::CODEC_PCMA:
171 return kAudioCodecPcma;
172 case AudioConfig::CODEC_PCMU:
173 return kAudioCodecPcmu;
174 }
175 }
176
ConvertEvsBandwidthToStr(kEvsBandwidth bandwidth,char * nBandwidth,uint32_t nLen)177 void ImsMediaAudioUtil::ConvertEvsBandwidthToStr(
178 kEvsBandwidth bandwidth, char* nBandwidth, uint32_t nLen)
179 {
180 switch (bandwidth)
181 {
182 case kEvsBandwidthNone:
183 strlcpy(nBandwidth, "NONE", nLen);
184 break;
185 case kEvsBandwidthNB:
186 strlcpy(nBandwidth, "NB", nLen);
187 break;
188 case kEvsBandwidthWB:
189 strlcpy(nBandwidth, "WB", nLen);
190 break;
191 case kEvsBandwidthSWB:
192 strlcpy(nBandwidth, "SWB", nLen);
193 break;
194 case kEvsBandwidthFB:
195 strlcpy(nBandwidth, "FB", nLen);
196 break;
197 default:
198 strlcpy(nBandwidth, "SWB", nLen);
199 break;
200 }
201 }
202
ConvertEvsCodecMode(int32_t evsMode)203 int32_t ImsMediaAudioUtil::ConvertEvsCodecMode(int32_t evsMode)
204 {
205 if (evsMode > MAX_AMR_MODE && evsMode <= MAX_EVS_MODE)
206 {
207 return kEvsCodecModePrimary;
208 }
209 else if (evsMode >= 0 && evsMode <= MAX_AMR_MODE)
210 {
211 return kEvsCodecModeAmrIo;
212 }
213 else
214 {
215 return kEvsCodecModeMax;
216 }
217 }
218
ConvertAmrModeToLen(uint32_t mode)219 uint32_t ImsMediaAudioUtil::ConvertAmrModeToLen(uint32_t mode)
220 {
221 if (mode > kImsAudioAmrModeSID)
222 { // over SID
223 return 0;
224 }
225 return gaAMRLen[mode];
226 }
227
ConvertAmrModeToBitLen(uint32_t mode)228 uint32_t ImsMediaAudioUtil::ConvertAmrModeToBitLen(uint32_t mode)
229 {
230 if (mode > kImsAudioAmrModeSID)
231 { // over SID
232 return 0;
233 }
234 return gaAMRBitLen[mode];
235 }
236
ConvertLenToAmrMode(uint32_t nLen)237 uint32_t ImsMediaAudioUtil::ConvertLenToAmrMode(uint32_t nLen)
238 {
239 uint32_t i;
240 if (nLen == 0)
241 {
242 return kImsAudioAmrModeNoData;
243 }
244
245 for (i = 0; i <= kImsAudioAmrModeSID; i++)
246 {
247 if (gaAMRLen[i] == nLen)
248 return i;
249 }
250 return kImsAudioAmrModeNoData;
251 }
252
ConvertAmrWbModeToLen(uint32_t mode)253 uint32_t ImsMediaAudioUtil::ConvertAmrWbModeToLen(uint32_t mode)
254 {
255 if (mode > kImsAudioAmrWbModeSID)
256 {
257 return 0;
258 }
259 return gaAMRWBLen[mode];
260 }
261
ConvertAmrWbModeToBitLen(uint32_t mode)262 uint32_t ImsMediaAudioUtil::ConvertAmrWbModeToBitLen(uint32_t mode)
263 {
264 if (mode > kImsAudioAmrWbModeSID)
265 {
266 return 0;
267 }
268 return gaAMRWBbitLen[mode];
269 }
270
ConvertLenToAmrWbMode(uint32_t nLen)271 uint32_t ImsMediaAudioUtil::ConvertLenToAmrWbMode(uint32_t nLen)
272 {
273 uint32_t i;
274 if (nLen == 0)
275 {
276 return kImsAudioAmrWbModeNoData;
277 }
278
279 for (i = 0; i <= kImsAudioAmrWbModeSID; i++)
280 {
281 if (gaAMRWBLen[i] == nLen)
282 return i;
283 }
284 return kImsAudioAmrWbModeNoData;
285 }
286
CheckEVSPrimaryHeaderFullModeFromSize(uint32_t size)287 bool ImsMediaAudioUtil::CheckEVSPrimaryHeaderFullModeFromSize(uint32_t size)
288 {
289 auto it = std::find(std::begin(gaEVSPrimaryHeaderFullByteLen),
290 std::end(gaEVSPrimaryHeaderFullByteLen), size);
291
292 // Check if the Evs size is headerfull or not
293 if (it != std::end(gaEVSPrimaryHeaderFullByteLen))
294 {
295 return true;
296 }
297
298 return false;
299 }
ConvertLenToEVSAudioMode(uint32_t nLen)300 uint32_t ImsMediaAudioUtil::ConvertLenToEVSAudioMode(uint32_t nLen)
301 {
302 uint32_t i = 0;
303 if (nLen == 0)
304 return kImsAudioEvsPrimaryModeNoData;
305 for (i = 0; i <= kImsAudioEvsPrimaryModeSID; i++)
306 {
307 if (gaEVSPrimaryByteLen[i] == nLen)
308 return i;
309 }
310 IMLOGD0("[ConvertLenToEVSAudioMode] No primery bit len found....");
311 return 0;
312 }
313
ConvertLenToEVSAMRIOAudioMode(uint32_t nLen)314 uint32_t ImsMediaAudioUtil::ConvertLenToEVSAMRIOAudioMode(uint32_t nLen)
315 {
316 uint32_t i = 0;
317 if (nLen == 0)
318 return kImsAudioEvsAmrWbIoModeNoData;
319 for (i = 0; i <= kImsAudioEvsAmrWbIoModeSID; i++)
320 {
321 if (gaEVSAMRWBIOLen[i] == nLen)
322 return i;
323 }
324 return 0;
325 }
326
ConvertEVSAudioModeToBitLen(uint32_t mode)327 uint32_t ImsMediaAudioUtil::ConvertEVSAudioModeToBitLen(uint32_t mode)
328 {
329 if (mode == 15)
330 return 0;
331 if (mode > 12)
332 return 0;
333 return gaEVSPrimaryBitLen[mode];
334 }
335
ConvertEVSAMRIOAudioModeToBitLen(uint32_t mode)336 uint32_t ImsMediaAudioUtil::ConvertEVSAMRIOAudioModeToBitLen(uint32_t mode)
337 {
338 if (mode == 15)
339 return 0;
340 if (mode > 9)
341 return 0;
342 return gaEVSAmrWbIoBitLen[mode];
343 }
344
ConvertAmrModeToBitrate(uint32_t mode)345 uint32_t ImsMediaAudioUtil::ConvertAmrModeToBitrate(uint32_t mode)
346 {
347 switch ((kImsAudioAmrMode)mode)
348 {
349 case kImsAudioAmrMode475:
350 return 4750;
351 case kImsAudioAmrMode515:
352 return 5150;
353 case kImsAudioAmrMode590:
354 return 5900;
355 case kImsAudioAmrMode670:
356 return 6700;
357 case kImsAudioAmrMode740:
358 return 7400;
359 case kImsAudioAmrMode795:
360 return 7950;
361 case kImsAudioAmrMode1020:
362 return 10200;
363 default:
364 case kImsAudioAmrMode1220:
365 return 12200;
366 }
367 }
368
ConvertAmrWbModeToBitrate(uint32_t mode)369 uint32_t ImsMediaAudioUtil::ConvertAmrWbModeToBitrate(uint32_t mode)
370 {
371 switch ((kImsAudioAmrWbMode)mode)
372 {
373 case kImsAudioAmrWbMode660:
374 return 6600;
375 case kImsAudioAmrWbMode885:
376 return 8850;
377 case kImsAudioAmrWbMode1265:
378 return 12650;
379 case kImsAudioAmrWbMode1425:
380 return 14250;
381 case kImsAudioAmrWbMode1585:
382 return 15850;
383 case kImsAudioAmrWbMode1825:
384 return 18250;
385 case kImsAudioAmrWbMode1985:
386 return 19850;
387 case kImsAudioAmrWbMode2305:
388 return 23050;
389 default:
390 case kImsAudioAmrWbMode2385:
391 return 23850;
392 }
393 }
394
GetMaximumAmrMode(int32_t codecType,int32_t bitmask)395 uint32_t ImsMediaAudioUtil::GetMaximumAmrMode(int32_t codecType, int32_t bitmask)
396 {
397 uint32_t maxMode =
398 (codecType == kAudioCodecAmr) ? kImsAudioAmrMode1220 : kImsAudioAmrWbMode2385;
399
400 for (int32_t i = maxMode; i >= 0; i--)
401 {
402 if (bitmask & (1 << i))
403 {
404 maxMode = i;
405 break;
406 }
407 }
408
409 return maxMode;
410 }
411
GetMaximumEvsMode(int32_t bitmask)412 uint32_t ImsMediaAudioUtil::GetMaximumEvsMode(int32_t bitmask)
413 {
414 uint32_t maxMode = 0;
415
416 for (int32_t i = 0; i <= MAX_EVS_MODE; i++)
417 {
418 if (bitmask & (1 << i))
419 {
420 maxMode = i;
421 }
422 }
423
424 return maxMode;
425 }
426
ConvertEVSModeToBitRate(const int32_t mode)427 int32_t ImsMediaAudioUtil::ConvertEVSModeToBitRate(const int32_t mode)
428 {
429 switch ((kImsAudioEvsMode)mode)
430 {
431 case kImsAudioEvsAmrWbIoMode660:
432 return 6600;
433 case kImsAudioEvsAmrWbIoMode885:
434 return 8850;
435 case kImsAudioEvsAmrWbIoMode1265:
436 return 12650;
437 case kImsAudioEvsAmrWbIoMode1425:
438 return 14250;
439 case kImsAudioEvsAmrWbIoMode1585:
440 return 15850;
441 case kImsAudioEvsAmrWbIoMode1825:
442 return 18250;
443 case kImsAudioEvsAmrWbIoMode1985:
444 return 19850;
445 case kImsAudioEvsAmrWbIoMode2305:
446 return 23050;
447 case kImsAudioEvsAmrWbIoMode2385:
448 return 23850;
449 case kImsAudioEvsPrimaryMode5900:
450 return 5900;
451 case kImsAudioEvsPrimaryMode7200:
452 return 7200;
453 case kImsAudioEvsPrimaryMode8000:
454 return 8000;
455 case kImsAudioEvsPrimaryMode9600:
456 return 9600;
457 case kImsAudioEvsPrimaryMode13200:
458 return 13200;
459 case kImsAudioEvsPrimaryMode16400:
460 return 16400;
461 case kImsAudioEvsPrimaryMode24400:
462 return 24400;
463 case kImsAudioEvsPrimaryMode32000:
464 return 32000;
465 case kImsAudioEvsPrimaryMode48000:
466 return 48000;
467 case kImsAudioEvsPrimaryMode64000:
468 return 64000;
469 case kImsAudioEvsPrimaryMode96000:
470 return 96000;
471 case kImsAudioEvsPrimaryMode128000:
472 return 128000;
473 default:
474 return 13200;
475 }
476 }
477
CheckEVSCodecMode(const uint32_t nAudioFrameLength)478 kEvsCodecMode ImsMediaAudioUtil::CheckEVSCodecMode(const uint32_t nAudioFrameLength)
479 {
480 switch (nAudioFrameLength)
481 {
482 // EVS AMR IO Mode Case
483 case 17:
484 case 23:
485 case 32:
486 case 36:
487 case 40:
488 case 46:
489 case 50:
490 case 58:
491 case 60:
492 case 5:
493 return kEvsCodecModeAmrIo;
494 // EVS Primary Mode Case
495 case 7:
496 case 18:
497 case 20:
498 case 24:
499 case 33:
500 case 41:
501 case 61:
502 case 80:
503 case 120:
504 case 160:
505 case 240:
506 case 320:
507 case 6:
508 default:
509 return kEvsCodecModePrimary;
510 }
511 }
512
ConvertEVSPayloadMode(uint32_t nDataSize,kEvsCodecMode * pEVSCodecMode,uint32_t * pEVSCompactId)513 kRtpPayloadHeaderMode ImsMediaAudioUtil::ConvertEVSPayloadMode(
514 uint32_t nDataSize, kEvsCodecMode* pEVSCodecMode, uint32_t* pEVSCompactId)
515 {
516 uint32_t i = 0;
517 uint32_t nDataBitSize = 0;
518 // nDataBitSize -= 2;
519 nDataBitSize = nDataSize * 8; // change byte to bit size
520
521 // compact format & primary mode
522 for (i = 0; i < EVS_COMPACT_PRIMARY_PAYLOAD_NUM; i++)
523 {
524 if (gaEVSPrimaryBitLen[i] == nDataBitSize)
525 {
526 *pEVSCodecMode = kEvsCodecModePrimary;
527 *pEVSCompactId = i;
528 return kRtpPayloadHeaderModeEvsCompact;
529 }
530 }
531
532 // compact format & amr-wb io mode
533 for (i = 0; i < EVS_COMPACT_AMRWBIO_PAYLOAD_NUM; i++)
534 {
535 if (gaEVSAmrWbIoBitLen[i] == nDataBitSize)
536 {
537 *pEVSCodecMode = kEvsCodecModeAmrIo;
538 *pEVSCompactId = i;
539 return kRtpPayloadHeaderModeEvsCompact;
540 }
541 }
542
543 // TODO : need to check ID...
544 *pEVSCodecMode = kEvsCodecModePrimary;
545 *pEVSCompactId = EVS_COMPACT_PAYLOAD_MAX_NUM;
546 return kRtpPayloadHeaderModeEvsHeaderFull;
547 }
548
FindMaxEvsBandwidthFromRange(const int32_t EvsBandwidthRange)549 kEvsBandwidth ImsMediaAudioUtil::FindMaxEvsBandwidthFromRange(const int32_t EvsBandwidthRange)
550 {
551 if (EvsBandwidthRange & kEvsBandwidthFB)
552 {
553 return kEvsBandwidthFB;
554 }
555 else if (EvsBandwidthRange & kEvsBandwidthSWB)
556 {
557 return kEvsBandwidthSWB;
558 }
559 else if (EvsBandwidthRange & kEvsBandwidthWB)
560 {
561 return kEvsBandwidthWB;
562 }
563 else if (EvsBandwidthRange & kEvsBandwidthNB)
564 {
565 return kEvsBandwidthNB;
566 }
567 else
568 {
569 return kEvsBandwidthNone;
570 }
571 }
572
GetAmrFrameType(const int32_t codecType,const uint32_t frameTypeIndex,const uint32_t size)573 ImsMediaSubType ImsMediaAudioUtil::GetAmrFrameType(
574 const int32_t codecType, const uint32_t frameTypeIndex, const uint32_t size)
575 {
576 ImsMediaSubType subType = MEDIASUBTYPE_AUDIO_NORMAL;
577 if (codecType == kAudioCodecAmr)
578 {
579 if (size == 0 || frameTypeIndex == kImsAudioAmrModeNoData)
580 {
581 subType = MEDIASUBTYPE_AUDIO_NODATA;
582 }
583 else if (frameTypeIndex == kImsAudioAmrModeSID)
584 {
585 subType = MEDIASUBTYPE_AUDIO_SID;
586 }
587 }
588 else if (codecType == kAudioCodecAmrWb)
589 {
590 if (size == 0 || frameTypeIndex == kImsAudioAmrWbModeNoData)
591 {
592 subType = MEDIASUBTYPE_AUDIO_NODATA;
593 }
594 else if (frameTypeIndex == kImsAudioAmrWbModeSID)
595 {
596 subType = MEDIASUBTYPE_AUDIO_SID;
597 }
598 }
599 return subType;
600 }
601