Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 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 | #pragma once |
| 18 | |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 19 | #include <initializer_list> |
| 20 | #include <type_traits> |
| 21 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 22 | #include <aidl/android/media/audio/common/AudioChannelLayout.h> |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 23 | #include <aidl/android/media/audio/common/AudioDeviceType.h> |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 24 | #include <aidl/android/media/audio/common/AudioFormatDescription.h> |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 25 | #include <aidl/android/media/audio/common/AudioInputFlags.h> |
| 26 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 27 | #include <aidl/android/media/audio/common/PcmType.h> |
| 28 | |
| 29 | namespace android::hardware::audio::common { |
| 30 | |
| 31 | constexpr size_t getPcmSampleSizeInBytes(::aidl::android::media::audio::common::PcmType pcm) { |
| 32 | using ::aidl::android::media::audio::common::PcmType; |
| 33 | switch (pcm) { |
| 34 | case PcmType::UINT_8_BIT: |
| 35 | return 1; |
| 36 | case PcmType::INT_16_BIT: |
| 37 | return 2; |
| 38 | case PcmType::INT_32_BIT: |
| 39 | return 4; |
| 40 | case PcmType::FIXED_Q_8_24: |
| 41 | return 4; |
| 42 | case PcmType::FLOAT_32_BIT: |
| 43 | return 4; |
| 44 | case PcmType::INT_24_BIT: |
| 45 | return 3; |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | constexpr size_t getChannelCount( |
Shraddha Basantwani | ae7dde5 | 2022-12-18 15:01:14 +0530 | [diff] [blame^] | 51 | const ::aidl::android::media::audio::common::AudioChannelLayout& layout, |
| 52 | int32_t mask = std::numeric_limits<int32_t>::max()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 53 | using Tag = ::aidl::android::media::audio::common::AudioChannelLayout::Tag; |
| 54 | switch (layout.getTag()) { |
| 55 | case Tag::none: |
| 56 | return 0; |
| 57 | case Tag::invalid: |
| 58 | return 0; |
| 59 | case Tag::indexMask: |
Shraddha Basantwani | ae7dde5 | 2022-12-18 15:01:14 +0530 | [diff] [blame^] | 60 | return __builtin_popcount(layout.get<Tag::indexMask>() & mask); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 61 | case Tag::layoutMask: |
Shraddha Basantwani | ae7dde5 | 2022-12-18 15:01:14 +0530 | [diff] [blame^] | 62 | return __builtin_popcount(layout.get<Tag::layoutMask>() & mask); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 63 | case Tag::voiceMask: |
Shraddha Basantwani | ae7dde5 | 2022-12-18 15:01:14 +0530 | [diff] [blame^] | 64 | return __builtin_popcount(layout.get<Tag::voiceMask>() & mask); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | constexpr size_t getFrameSizeInBytes( |
| 70 | const ::aidl::android::media::audio::common::AudioFormatDescription& format, |
| 71 | const ::aidl::android::media::audio::common::AudioChannelLayout& layout) { |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame] | 72 | if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) { |
| 73 | // Unspecified format. |
| 74 | return 0; |
| 75 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 76 | using ::aidl::android::media::audio::common::AudioFormatType; |
| 77 | if (format.type == AudioFormatType::PCM) { |
| 78 | return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout); |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame] | 79 | } else if (format.type == AudioFormatType::NON_PCM) { |
| 80 | // For non-PCM formats always use the underlying PCM size. The default value for |
| 81 | // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1. |
| 82 | return getPcmSampleSizeInBytes(format.pcm); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 83 | } |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame] | 84 | // Something unexpected. |
| 85 | return 0; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 88 | constexpr bool isTelephonyDeviceType( |
| 89 | ::aidl::android::media::audio::common::AudioDeviceType device) { |
| 90 | return device == ::aidl::android::media::audio::common::AudioDeviceType::IN_TELEPHONY_RX || |
| 91 | device == ::aidl::android::media::audio::common::AudioDeviceType::OUT_TELEPHONY_TX; |
| 92 | } |
| 93 | |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 94 | // The helper functions defined below are only applicable to the case when an enum type |
| 95 | // specifies zero-based bit positions, not bit masks themselves. This is why instantiation |
| 96 | // is restricted to certain enum types. |
| 97 | template <typename E> |
| 98 | using is_bit_position_enum = std::integral_constant< |
| 99 | bool, std::is_same_v<E, ::aidl::android::media::audio::common::AudioInputFlags> || |
| 100 | std::is_same_v<E, ::aidl::android::media::audio::common::AudioOutputFlags>>; |
| 101 | |
| 102 | template <typename E, typename U = std::underlying_type_t<E>, |
| 103 | typename = std::enable_if_t<is_bit_position_enum<E>::value>> |
| 104 | constexpr U makeBitPositionFlagMask(E flag) { |
| 105 | return 1 << static_cast<U>(flag); |
| 106 | } |
| 107 | |
| 108 | template <typename E, typename U = std::underlying_type_t<E>, |
| 109 | typename = std::enable_if_t<is_bit_position_enum<E>::value>> |
| 110 | constexpr bool isBitPositionFlagSet(U mask, E flag) { |
| 111 | return (mask & makeBitPositionFlagMask(flag)) != 0; |
| 112 | } |
| 113 | |
| 114 | template <typename E, typename U = std::underlying_type_t<E>, |
| 115 | typename = std::enable_if_t<is_bit_position_enum<E>::value>> |
| 116 | constexpr U makeBitPositionFlagMask(std::initializer_list<E> flags) { |
| 117 | U result = 0; |
| 118 | for (const auto flag : flags) { |
| 119 | result |= makeBitPositionFlagMask(flag); |
| 120 | } |
| 121 | return result; |
| 122 | } |
| 123 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 124 | } // namespace android::hardware::audio::common |