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 | |
| 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 | |
| 23 | namespace android::hardware::audio::common { |
| 24 | |
| 25 | constexpr 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 | |
| 44 | constexpr 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 | |
| 62 | constexpr size_t getFrameSizeInBytes( |
| 63 | const ::aidl::android::media::audio::common::AudioFormatDescription& format, |
| 64 | const ::aidl::android::media::audio::common::AudioChannelLayout& layout) { |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame^] | 65 | if (format == ::aidl::android::media::audio::common::AudioFormatDescription{}) { |
| 66 | // Unspecified format. |
| 67 | return 0; |
| 68 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 69 | using ::aidl::android::media::audio::common::AudioFormatType; |
| 70 | if (format.type == AudioFormatType::PCM) { |
| 71 | return getPcmSampleSizeInBytes(format.pcm) * getChannelCount(layout); |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame^] | 72 | } else if (format.type == AudioFormatType::NON_PCM) { |
| 73 | // For non-PCM formats always use the underlying PCM size. The default value for |
| 74 | // PCM is "UINT_8_BIT", thus non-encapsulated streams have the frame size of 1. |
| 75 | return getPcmSampleSizeInBytes(format.pcm); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 76 | } |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame^] | 77 | // Something unexpected. |
| 78 | return 0; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace android::hardware::audio::common |