Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ANDROID_MEDIA_AUDIOFORMAT_H |
| 18 | #define ANDROID_MEDIA_AUDIOFORMAT_H |
| 19 | |
| 20 | #include <system/audio.h> |
| 21 | |
| 22 | // keep these values in sync with AudioFormat.java |
Eric Laurent | b4fae5b | 2016-07-07 18:58:11 -0700 | [diff] [blame] | 23 | #define ENCODING_PCM_16BIT 2 |
| 24 | #define ENCODING_PCM_8BIT 3 |
| 25 | #define ENCODING_PCM_FLOAT 4 |
| 26 | #define ENCODING_AC3 5 |
| 27 | #define ENCODING_E_AC3 6 |
| 28 | #define ENCODING_DTS 7 |
| 29 | #define ENCODING_DTS_HD 8 |
| 30 | #define ENCODING_MP3 9 |
| 31 | #define ENCODING_AAC_LC 10 |
| 32 | #define ENCODING_AAC_HE_V1 11 |
| 33 | #define ENCODING_AAC_HE_V2 12 |
| 34 | #define ENCODING_IEC61937 13 |
| 35 | #define ENCODING_DOLBY_TRUEHD 14 |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 36 | #define ENCODING_AAC_ELD 15 |
| 37 | #define ENCODING_AAC_XHE 16 |
Jean-Michel Trivi | 5c2aae0 | 2018-01-19 17:53:40 -0800 | [diff] [blame] | 38 | #define ENCODING_AC4 17 |
Mikhail Naganov | 43a8ebb | 2018-01-24 09:21:38 -0800 | [diff] [blame] | 39 | #define ENCODING_E_AC3_JOC 18 |
Mikhail Naganov | 98dbab1 | 2018-11-02 15:11:06 -0700 | [diff] [blame] | 40 | #define ENCODING_DOLBY_MAT 19 |
Eric Laurent | b4fae5b | 2016-07-07 18:58:11 -0700 | [diff] [blame] | 41 | |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 42 | #define ENCODING_INVALID 0 |
| 43 | #define ENCODING_DEFAULT 1 |
| 44 | |
| 45 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 46 | |
| 47 | #define CHANNEL_INVALID 0 |
| 48 | #define CHANNEL_OUT_DEFAULT 1 |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 49 | |
| 50 | static inline audio_format_t audioFormatToNative(int audioFormat) |
| 51 | { |
| 52 | switch (audioFormat) { |
| 53 | case ENCODING_PCM_16BIT: |
| 54 | return AUDIO_FORMAT_PCM_16_BIT; |
| 55 | case ENCODING_PCM_8BIT: |
| 56 | return AUDIO_FORMAT_PCM_8_BIT; |
Glenn Kasten | 313f598 | 2014-04-30 18:02:08 -0700 | [diff] [blame] | 57 | case ENCODING_PCM_FLOAT: |
| 58 | return AUDIO_FORMAT_PCM_FLOAT; |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 59 | case ENCODING_AC3: |
| 60 | return AUDIO_FORMAT_AC3; |
| 61 | case ENCODING_E_AC3: |
| 62 | return AUDIO_FORMAT_E_AC3; |
Phil Burk | e12189d | 2015-03-31 14:38:30 -0700 | [diff] [blame] | 63 | case ENCODING_DTS: |
| 64 | return AUDIO_FORMAT_DTS; |
| 65 | case ENCODING_DTS_HD: |
| 66 | return AUDIO_FORMAT_DTS_HD; |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 67 | case ENCODING_MP3: |
| 68 | return AUDIO_FORMAT_MP3; |
| 69 | case ENCODING_AAC_LC: |
| 70 | return AUDIO_FORMAT_AAC_LC; |
| 71 | case ENCODING_AAC_HE_V1: |
| 72 | return AUDIO_FORMAT_AAC_HE_V1; |
| 73 | case ENCODING_AAC_HE_V2: |
| 74 | return AUDIO_FORMAT_AAC_HE_V2; |
Phil Burk | 43f4b27 | 2016-01-27 15:35:20 -0800 | [diff] [blame] | 75 | case ENCODING_IEC61937: |
| 76 | return AUDIO_FORMAT_IEC61937; |
Mikhail Naganov | 0972565 | 2018-10-11 15:39:40 -0700 | [diff] [blame] | 77 | case ENCODING_DOLBY_TRUEHD: |
| 78 | return AUDIO_FORMAT_DOLBY_TRUEHD; |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 79 | case ENCODING_AAC_ELD: |
| 80 | return AUDIO_FORMAT_AAC_ELD; |
| 81 | case ENCODING_AAC_XHE: |
Jean-Michel Trivi | ef88450 | 2018-03-07 15:02:44 -0800 | [diff] [blame] | 82 | return AUDIO_FORMAT_AAC_XHE; |
Jean-Michel Trivi | 5c2aae0 | 2018-01-19 17:53:40 -0800 | [diff] [blame] | 83 | case ENCODING_AC4: |
| 84 | return AUDIO_FORMAT_AC4; |
Mikhail Naganov | 6d0c5f6 | 2018-03-06 17:37:32 -0800 | [diff] [blame] | 85 | case ENCODING_E_AC3_JOC: |
| 86 | return AUDIO_FORMAT_E_AC3_JOC; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 87 | case ENCODING_DEFAULT: |
| 88 | return AUDIO_FORMAT_DEFAULT; |
Mikhail Naganov | 98dbab1 | 2018-11-02 15:11:06 -0700 | [diff] [blame] | 89 | case ENCODING_DOLBY_MAT: |
| 90 | return AUDIO_FORMAT_MAT; |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 91 | default: |
| 92 | return AUDIO_FORMAT_INVALID; |
| 93 | } |
| 94 | } |
| 95 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 96 | static inline int audioFormatFromNative(audio_format_t nativeFormat) |
| 97 | { |
| 98 | switch (nativeFormat) { |
| 99 | case AUDIO_FORMAT_PCM_16_BIT: |
| 100 | return ENCODING_PCM_16BIT; |
| 101 | case AUDIO_FORMAT_PCM_8_BIT: |
| 102 | return ENCODING_PCM_8BIT; |
| 103 | case AUDIO_FORMAT_PCM_FLOAT: |
| 104 | return ENCODING_PCM_FLOAT; |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 105 | |
| 106 | // map these to ENCODING_PCM_FLOAT |
| 107 | case AUDIO_FORMAT_PCM_8_24_BIT: |
| 108 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 109 | case AUDIO_FORMAT_PCM_32_BIT: |
| 110 | return ENCODING_PCM_FLOAT; |
| 111 | |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 112 | case AUDIO_FORMAT_AC3: |
| 113 | return ENCODING_AC3; |
| 114 | case AUDIO_FORMAT_E_AC3: |
| 115 | return ENCODING_E_AC3; |
Phil Burk | e12189d | 2015-03-31 14:38:30 -0700 | [diff] [blame] | 116 | case AUDIO_FORMAT_DTS: |
| 117 | return ENCODING_DTS; |
| 118 | case AUDIO_FORMAT_DTS_HD: |
| 119 | return ENCODING_DTS_HD; |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 120 | case AUDIO_FORMAT_MP3: |
| 121 | return ENCODING_MP3; |
| 122 | case AUDIO_FORMAT_AAC_LC: |
| 123 | return ENCODING_AAC_LC; |
| 124 | case AUDIO_FORMAT_AAC_HE_V1: |
| 125 | return ENCODING_AAC_HE_V1; |
| 126 | case AUDIO_FORMAT_AAC_HE_V2: |
| 127 | return ENCODING_AAC_HE_V2; |
Phil Burk | 43f4b27 | 2016-01-27 15:35:20 -0800 | [diff] [blame] | 128 | case AUDIO_FORMAT_IEC61937: |
| 129 | return ENCODING_IEC61937; |
Eric Laurent | b4fae5b | 2016-07-07 18:58:11 -0700 | [diff] [blame] | 130 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 131 | return ENCODING_DOLBY_TRUEHD; |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 132 | case AUDIO_FORMAT_AAC_ELD: |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 133 | return ENCODING_AAC_ELD; |
| 134 | case AUDIO_FORMAT_AAC_XHE: |
| 135 | return ENCODING_AAC_XHE; |
Jean-Michel Trivi | 5c2aae0 | 2018-01-19 17:53:40 -0800 | [diff] [blame] | 136 | case AUDIO_FORMAT_AC4: |
| 137 | return ENCODING_AC4; |
Mikhail Naganov | 6d0c5f6 | 2018-03-06 17:37:32 -0800 | [diff] [blame] | 138 | case AUDIO_FORMAT_E_AC3_JOC: |
| 139 | return ENCODING_E_AC3_JOC; |
Mikhail Naganov | 98dbab1 | 2018-11-02 15:11:06 -0700 | [diff] [blame] | 140 | case AUDIO_FORMAT_MAT: |
| 141 | case AUDIO_FORMAT_MAT_1_0: |
| 142 | case AUDIO_FORMAT_MAT_2_0: |
| 143 | case AUDIO_FORMAT_MAT_2_1: |
| 144 | return ENCODING_DOLBY_MAT; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 145 | case AUDIO_FORMAT_DEFAULT: |
| 146 | return ENCODING_DEFAULT; |
| 147 | default: |
| 148 | return ENCODING_INVALID; |
| 149 | } |
| 150 | } |
| 151 | |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 152 | // This function converts Java channel masks to a native channel mask. |
| 153 | // validity should be checked with audio_is_output_channel(). |
| 154 | static inline audio_channel_mask_t nativeChannelMaskFromJavaChannelMasks( |
| 155 | jint channelPositionMask, jint channelIndexMask) |
| 156 | { |
| 157 | // 0 is the java android.media.AudioFormat.CHANNEL_INVALID value |
| 158 | if (channelIndexMask != 0) { // channel index mask takes priority |
| 159 | // To convert to a native channel mask, the Java channel index mask |
| 160 | // requires adding the index representation. |
| 161 | return audio_channel_mask_from_representation_and_bits( |
| 162 | AUDIO_CHANNEL_REPRESENTATION_INDEX, |
| 163 | channelIndexMask); |
| 164 | } |
| 165 | // To convert to a native channel mask, the Java channel position mask |
| 166 | // requires a shift by 2 to skip the two deprecated channel |
| 167 | // configurations "default" and "mono". |
| 168 | return (audio_channel_mask_t)((uint32_t)channelPositionMask >> 2); |
| 169 | } |
| 170 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 171 | static inline audio_channel_mask_t outChannelMaskToNative(int channelMask) |
| 172 | { |
| 173 | switch (channelMask) { |
| 174 | case CHANNEL_OUT_DEFAULT: |
| 175 | case CHANNEL_INVALID: |
| 176 | return AUDIO_CHANNEL_NONE; |
| 177 | default: |
| 178 | return (audio_channel_mask_t)(channelMask>>2); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static inline int outChannelMaskFromNative(audio_channel_mask_t nativeMask) |
| 183 | { |
| 184 | switch (nativeMask) { |
| 185 | case AUDIO_CHANNEL_NONE: |
| 186 | return CHANNEL_OUT_DEFAULT; |
| 187 | default: |
| 188 | return (int)nativeMask<<2; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static inline audio_channel_mask_t inChannelMaskToNative(int channelMask) |
| 193 | { |
| 194 | return (audio_channel_mask_t)channelMask; |
| 195 | } |
| 196 | |
| 197 | static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask) |
| 198 | { |
| 199 | return (int)nativeMask; |
| 200 | } |
| 201 | |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 202 | #endif // ANDROID_MEDIA_AUDIOFORMAT_H |