blob: 10261344f4d709f8daaeb3520bbc935d83eab43c [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
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000019#include <initializer_list>
20#include <type_traits>
21
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000022#include <aidl/android/media/audio/common/AudioChannelLayout.h>
23#include <aidl/android/media/audio/common/AudioFormatDescription.h>
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000024#include <aidl/android/media/audio/common/AudioInputFlags.h>
25#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000026#include <aidl/android/media/audio/common/PcmType.h>
27
28namespace android::hardware::audio::common {
29
30constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) {
31 using ::aidl::android::media::audio::common::PcmType;
32 switch (pcm) {
33 case PcmType::UINT_8_BIT:
34 return 1;
35 case PcmType::INT_16_BIT:
36 return 2;
37 case PcmType::INT_32_BIT:
38 return 4;
39 case PcmType::FIXED_Q_8_24:
40 return 4;
41 case PcmType::FLOAT_32_BIT:
42 return 4;
43 case PcmType::INT_24_BIT:
44 return 3;
45 }
46 return 0;
47}
48
49constexpr size_t getChannelCount(
50 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
51 using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag;
52 switch (layout.getTag()) {
53 case Tag::none:
54 return 0;
55 case Tag::invalid:
56 return 0;
57 case Tag::indexMask:
58 return __builtin_popcount(layout.get<Tag::indexMask>());
59 case Tag::layoutMask:
60 return __builtin_popcount(layout.get<Tag::layoutMask>());
61 case Tag::voiceMask:
62 return __builtin_popcount(layout.get<Tag::voiceMask>());
63 }
64 return 0;
65}
66
67constexpr size_t getFrameSizeInBytes(
68 const ::aidl::android::media::audio::common::AudioFormatDescription& format,
69 const ::aidl::android::media::audio::common::AudioChannelLayout& layout) {
Mikhail Naganova2c71412022-08-19 21:37:35 +000070 if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) {
71 // Unspecified format.
72 return 0;
73 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000074 using ::aidl::android::media::audio::common::AudioFormatType;
75 if (format.type == AudioFormatType::PCM) {
76 return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout);
Mikhail Naganova2c71412022-08-19 21:37:35 +000077 } else if (format.type == AudioFormatType::NON_PCM) {
78 // For non-PCM formats always use the underlying PCM size. The default value for
79 // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1.
80 return getPcmSampleSizeInBytes(format.pcm);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000081 }
Mikhail Naganova2c71412022-08-19 21:37:35 +000082 // Something unexpected.
83 return 0;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000084}
85
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000086// The helper functions defined below are only applicable to the case when an enum type
87// specifies zero-based bit positions, not bit masks themselves. This is why instantiation
88// is restricted to certain enum types.
89template <typename E>
90using is_bit_position_enum = std::integral_constant<
91 bool, std::is_same_v<E, ::aidl::android::media::audio::common::AudioInputFlags> ||
92 std::is_same_v<E, ::aidl::android::media::audio::common::AudioOutputFlags>>;
93
94template <typename E, typename U = std::underlying_type_t<E>,
95 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
96constexpr U makeBitPositionFlagMask(E flag) {
97 return 1 << static_cast<U>(flag);
98}
99
100template <typename E, typename U = std::underlying_type_t<E>,
101 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
102constexpr bool isBitPositionFlagSet(U mask, E flag) {
103 return (mask & makeBitPositionFlagMask(flag)) != 0;
104}
105
106template <typename E, typename U = std::underlying_type_t<E>,
107 typename = std::enable_if_t<is_bit_position_enum<E>::value>>
108constexpr U makeBitPositionFlagMask(std::initializer_list<E> flags) {
109 U result = 0;
110 for (const auto flag : flags) {
111 result |= makeBitPositionFlagMask(flag);
112 }
113 return result;
114}
115
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000116} // namespace android::hardware::audio::common