blob: 1a87882c1a03ac9876cb7619631a435da5cff488 [file] [log] [blame]
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001/*
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#pragma once
18
19#include <aidl/android/media/audio/common/AudioChannelLayout.h>
20#include <aidl/android/media/audio/common/AudioFormatDescription.h>
21#include <aidl/android/media/audio/common/PcmType.h>
22
23namespace android::hardware::audio::common {
24
25constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) {
26 using ::aidl::android::media::audio::common::PcmType;
27 switch (pcm) {
28 case PcmType::UINT_8_BIT:
29 return 1;
30 case PcmType::INT_16_BIT:
31 return 2;
32 case PcmType::INT_32_BIT:
33 return 4;
34 case PcmType::FIXED_Q_8_24:
35 return 4;
36 case PcmType::FLOAT_32_BIT:
37 return 4;
38 case PcmType::INT_24_BIT:
39 return 3;
40 }
41 return 0;
42}
43
44constexpr size_t getChannelCount(
45 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
46 using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag;
47 switch (layout.getTag()) {
48 case Tag::none:
49 return 0;
50 case Tag::invalid:
51 return 0;
52 case Tag::indexMask:
53 return __builtin_popcount(layout.get<Tag::indexMask>());
54 case Tag::layoutMask:
55 return __builtin_popcount(layout.get<Tag::layoutMask>());
56 case Tag::voiceMask:
57 return __builtin_popcount(layout.get<Tag::voiceMask>());
58 }
59 return 0;
60}
61
62constexpr size_t getFrameSizeInBytes(
63 const ::aidl::android::media::audio::common::AudioFormatDescription& format,
64 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
65 using ::aidl::android::media::audio::common::AudioFormatType;
66 if (format.type == AudioFormatType::PCM) {
67 return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout);
68 }
69 // For non-PCM formats always use frame size of 1.
70 return 1;
71}
72
73} // namespace android::hardware::audio::common