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
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
36 #define ENCODING_AAC_ELD 15
37 #define ENCODING_AAC_XHE 16
38 #define ENCODING_AC4 17
39 #define ENCODING_E_AC3_JOC 18
40 #define ENCODING_DOLBY_MAT 19
41 #define ENCODING_OPUS 20
42 #define ENCODING_PCM_24BIT_PACKED 21
43 #define ENCODING_PCM_32BIT 22
44 #define ENCODING_MPEGH_BL_L3 23
45 #define ENCODING_MPEGH_BL_L4 24
46 #define ENCODING_MPEGH_LC_L3 25
47 #define ENCODING_MPEGH_LC_L4 26
48 #define ENCODING_DTS_UHD_P1 27
49 #define ENCODING_DRA 28
50 #define ENCODING_DTS_HD_MA 29
51 #define ENCODING_DTS_UHD_P2 30
52 #define ENCODING_DSD 31
53
54 #define ENCODING_INVALID 0
55 #define ENCODING_DEFAULT 1
56
57
58
59 #define CHANNEL_INVALID 0
60 #define CHANNEL_OUT_DEFAULT 1
61 #define CHANNEL_IN_DEFAULT 1
62
audioFormatToNative(int audioFormat)63 static inline audio_format_t audioFormatToNative(int audioFormat)
64 {
65 switch (audioFormat) {
66 case ENCODING_PCM_16BIT:
67 return AUDIO_FORMAT_PCM_16_BIT;
68 case ENCODING_PCM_8BIT:
69 return AUDIO_FORMAT_PCM_8_BIT;
70 case ENCODING_PCM_FLOAT:
71 return AUDIO_FORMAT_PCM_FLOAT;
72 case ENCODING_AC3:
73 return AUDIO_FORMAT_AC3;
74 case ENCODING_E_AC3:
75 return AUDIO_FORMAT_E_AC3;
76 case ENCODING_DTS:
77 return AUDIO_FORMAT_DTS;
78 case ENCODING_DTS_HD:
79 return AUDIO_FORMAT_DTS_HD;
80 case ENCODING_MP3:
81 return AUDIO_FORMAT_MP3;
82 case ENCODING_AAC_LC:
83 return AUDIO_FORMAT_AAC_LC;
84 case ENCODING_AAC_HE_V1:
85 return AUDIO_FORMAT_AAC_HE_V1;
86 case ENCODING_AAC_HE_V2:
87 return AUDIO_FORMAT_AAC_HE_V2;
88 case ENCODING_IEC61937:
89 return AUDIO_FORMAT_IEC61937;
90 case ENCODING_DOLBY_TRUEHD:
91 return AUDIO_FORMAT_DOLBY_TRUEHD;
92 case ENCODING_AAC_ELD:
93 return AUDIO_FORMAT_AAC_ELD;
94 case ENCODING_AAC_XHE:
95 return AUDIO_FORMAT_AAC_XHE;
96 case ENCODING_AC4:
97 return AUDIO_FORMAT_AC4;
98 case ENCODING_E_AC3_JOC:
99 return AUDIO_FORMAT_E_AC3_JOC;
100 case ENCODING_DEFAULT:
101 return AUDIO_FORMAT_DEFAULT;
102 case ENCODING_DOLBY_MAT:
103 return AUDIO_FORMAT_MAT;
104 case ENCODING_OPUS:
105 return AUDIO_FORMAT_OPUS;
106 case ENCODING_PCM_24BIT_PACKED:
107 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
108 case ENCODING_PCM_32BIT:
109 return AUDIO_FORMAT_PCM_32_BIT;
110 case ENCODING_MPEGH_BL_L3:
111 return AUDIO_FORMAT_MPEGH_BL_L3;
112 case ENCODING_MPEGH_BL_L4:
113 return AUDIO_FORMAT_MPEGH_BL_L4;
114 case ENCODING_MPEGH_LC_L3:
115 return AUDIO_FORMAT_MPEGH_LC_L3;
116 case ENCODING_MPEGH_LC_L4:
117 return AUDIO_FORMAT_MPEGH_LC_L4;
118 case ENCODING_DTS_UHD_P1:
119 return AUDIO_FORMAT_DTS_UHD;
120 case ENCODING_DRA:
121 return AUDIO_FORMAT_DRA;
122 case ENCODING_DTS_HD_MA:
123 return AUDIO_FORMAT_DTS_HD_MA;
124 case ENCODING_DTS_UHD_P2:
125 return AUDIO_FORMAT_DTS_UHD_P2;
126 case ENCODING_DSD:
127 return AUDIO_FORMAT_DSD;
128 default:
129 return AUDIO_FORMAT_INVALID;
130 }
131 }
132
audioFormatFromNative(audio_format_t nativeFormat)133 static inline int audioFormatFromNative(audio_format_t nativeFormat)
134 {
135 switch (nativeFormat) {
136 case AUDIO_FORMAT_PCM_16_BIT:
137 return ENCODING_PCM_16BIT;
138 case AUDIO_FORMAT_PCM_8_BIT:
139 return ENCODING_PCM_8BIT;
140 case AUDIO_FORMAT_PCM_FLOAT:
141 return ENCODING_PCM_FLOAT;
142
143 // As of S, these extend integer precision formats now return more specific values
144 // than ENCODING_PCM_FLOAT.
145 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
146 return ENCODING_PCM_24BIT_PACKED;
147 case AUDIO_FORMAT_PCM_32_BIT:
148 return ENCODING_PCM_32BIT;
149
150 // map this to ENCODING_PCM_FLOAT
151 case AUDIO_FORMAT_PCM_8_24_BIT:
152 return ENCODING_PCM_FLOAT;
153
154 case AUDIO_FORMAT_AC3:
155 return ENCODING_AC3;
156 case AUDIO_FORMAT_E_AC3:
157 return ENCODING_E_AC3;
158 case AUDIO_FORMAT_DTS:
159 return ENCODING_DTS;
160 case AUDIO_FORMAT_DTS_HD:
161 return ENCODING_DTS_HD;
162 case AUDIO_FORMAT_MP3:
163 return ENCODING_MP3;
164 case AUDIO_FORMAT_AAC_LC:
165 return ENCODING_AAC_LC;
166 case AUDIO_FORMAT_AAC_HE_V1:
167 return ENCODING_AAC_HE_V1;
168 case AUDIO_FORMAT_AAC_HE_V2:
169 return ENCODING_AAC_HE_V2;
170 case AUDIO_FORMAT_IEC61937:
171 return ENCODING_IEC61937;
172 case AUDIO_FORMAT_DOLBY_TRUEHD:
173 return ENCODING_DOLBY_TRUEHD;
174 case AUDIO_FORMAT_AAC_ELD:
175 return ENCODING_AAC_ELD;
176 case AUDIO_FORMAT_AAC_XHE:
177 return ENCODING_AAC_XHE;
178 case AUDIO_FORMAT_AC4:
179 return ENCODING_AC4;
180 case AUDIO_FORMAT_E_AC3_JOC:
181 return ENCODING_E_AC3_JOC;
182 case AUDIO_FORMAT_MAT:
183 case AUDIO_FORMAT_MAT_1_0:
184 case AUDIO_FORMAT_MAT_2_0:
185 case AUDIO_FORMAT_MAT_2_1:
186 return ENCODING_DOLBY_MAT;
187 case AUDIO_FORMAT_OPUS:
188 return ENCODING_OPUS;
189 case AUDIO_FORMAT_MPEGH_BL_L3:
190 return ENCODING_MPEGH_BL_L3;
191 case AUDIO_FORMAT_MPEGH_BL_L4:
192 return ENCODING_MPEGH_BL_L4;
193 case AUDIO_FORMAT_MPEGH_LC_L3:
194 return ENCODING_MPEGH_LC_L3;
195 case AUDIO_FORMAT_MPEGH_LC_L4:
196 return ENCODING_MPEGH_LC_L4;
197 case AUDIO_FORMAT_DTS_UHD:
198 return ENCODING_DTS_UHD_P1;
199 case AUDIO_FORMAT_DRA:
200 return ENCODING_DRA;
201 case AUDIO_FORMAT_DTS_HD_MA:
202 return ENCODING_DTS_HD_MA;
203 case AUDIO_FORMAT_DTS_UHD_P2:
204 return ENCODING_DTS_UHD_P2;
205 case AUDIO_FORMAT_DEFAULT:
206 return ENCODING_DEFAULT;
207 case AUDIO_FORMAT_DSD:
208 return ENCODING_DSD;
209 default:
210 return ENCODING_INVALID;
211 }
212 }
213
214 // This function converts Java channel masks to a native channel mask.
215 // validity should be checked with audio_is_output_channel().
nativeChannelMaskFromJavaChannelMasks(jint channelPositionMask,jint channelIndexMask)216 static inline audio_channel_mask_t nativeChannelMaskFromJavaChannelMasks(
217 jint channelPositionMask, jint channelIndexMask)
218 {
219 // 0 is the java android.media.AudioFormat.CHANNEL_INVALID value
220 if (channelIndexMask != 0) { // channel index mask takes priority
221 // To convert to a native channel mask, the Java channel index mask
222 // requires adding the index representation.
223 return audio_channel_mask_from_representation_and_bits(
224 AUDIO_CHANNEL_REPRESENTATION_INDEX,
225 channelIndexMask);
226 }
227 // To convert to a native channel mask, the Java channel position mask
228 // requires a shift by 2 to skip the two deprecated channel
229 // configurations "default" and "mono".
230 return (audio_channel_mask_t)((uint32_t)channelPositionMask >> 2);
231 }
232
outChannelMaskToNative(int channelMask)233 static inline audio_channel_mask_t outChannelMaskToNative(int channelMask)
234 {
235 switch (channelMask) {
236 case CHANNEL_OUT_DEFAULT:
237 case CHANNEL_INVALID:
238 return AUDIO_CHANNEL_NONE;
239 default:
240 return (audio_channel_mask_t)(channelMask>>2);
241 }
242 }
243
outChannelMaskFromNative(audio_channel_mask_t nativeMask)244 static inline int outChannelMaskFromNative(audio_channel_mask_t nativeMask)
245 {
246 switch (nativeMask) {
247 case AUDIO_CHANNEL_NONE:
248 return CHANNEL_OUT_DEFAULT;
249 default:
250 return (int)nativeMask<<2;
251 }
252 }
253
inChannelMaskToNative(int channelMask)254 static inline audio_channel_mask_t inChannelMaskToNative(int channelMask)
255 {
256 switch (channelMask) {
257 case CHANNEL_IN_DEFAULT:
258 return AUDIO_CHANNEL_NONE;
259 default:
260 return (audio_channel_mask_t)channelMask;
261 }
262 }
263
inChannelMaskFromNative(audio_channel_mask_t nativeMask)264 static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask)
265 {
266 switch (nativeMask) {
267 case AUDIO_CHANNEL_NONE:
268 return CHANNEL_IN_DEFAULT;
269 default:
270 return (int)nativeMask;
271 }
272 }
273
274 #endif // ANDROID_MEDIA_AUDIOFORMAT_H
275