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 
43 #define ENCODING_INVALID    0
44 #define ENCODING_DEFAULT    1
45 
46 
47 
48 #define CHANNEL_INVALID 0
49 #define CHANNEL_OUT_DEFAULT 1
50 #define CHANNEL_IN_DEFAULT 1
51 
audioFormatToNative(int audioFormat)52 static inline audio_format_t audioFormatToNative(int audioFormat)
53 {
54     switch (audioFormat) {
55     case ENCODING_PCM_16BIT:
56         return AUDIO_FORMAT_PCM_16_BIT;
57     case ENCODING_PCM_8BIT:
58         return AUDIO_FORMAT_PCM_8_BIT;
59     case ENCODING_PCM_FLOAT:
60         return AUDIO_FORMAT_PCM_FLOAT;
61     case ENCODING_AC3:
62         return AUDIO_FORMAT_AC3;
63     case ENCODING_E_AC3:
64         return AUDIO_FORMAT_E_AC3;
65     case ENCODING_DTS:
66         return AUDIO_FORMAT_DTS;
67     case ENCODING_DTS_HD:
68         return AUDIO_FORMAT_DTS_HD;
69     case ENCODING_MP3:
70         return AUDIO_FORMAT_MP3;
71     case ENCODING_AAC_LC:
72         return AUDIO_FORMAT_AAC_LC;
73     case ENCODING_AAC_HE_V1:
74         return AUDIO_FORMAT_AAC_HE_V1;
75     case ENCODING_AAC_HE_V2:
76         return AUDIO_FORMAT_AAC_HE_V2;
77     case ENCODING_IEC61937:
78         return AUDIO_FORMAT_IEC61937;
79     case ENCODING_DOLBY_TRUEHD:
80         return AUDIO_FORMAT_DOLBY_TRUEHD;
81     case ENCODING_AAC_ELD:
82         return AUDIO_FORMAT_AAC_ELD;
83     case ENCODING_AAC_XHE:
84         return AUDIO_FORMAT_AAC_XHE;
85     case ENCODING_AC4:
86         return AUDIO_FORMAT_AC4;
87     case ENCODING_E_AC3_JOC:
88         return AUDIO_FORMAT_E_AC3_JOC;
89     case ENCODING_DEFAULT:
90         return AUDIO_FORMAT_DEFAULT;
91     case ENCODING_DOLBY_MAT:
92         return AUDIO_FORMAT_MAT;
93     case ENCODING_OPUS:
94         return AUDIO_FORMAT_OPUS;
95     default:
96         return AUDIO_FORMAT_INVALID;
97     }
98 }
99 
audioFormatFromNative(audio_format_t nativeFormat)100 static inline int audioFormatFromNative(audio_format_t nativeFormat)
101 {
102     switch (nativeFormat) {
103     case AUDIO_FORMAT_PCM_16_BIT:
104         return ENCODING_PCM_16BIT;
105     case AUDIO_FORMAT_PCM_8_BIT:
106         return ENCODING_PCM_8BIT;
107     case AUDIO_FORMAT_PCM_FLOAT:
108         return ENCODING_PCM_FLOAT;
109 
110     // map these to ENCODING_PCM_FLOAT
111     case AUDIO_FORMAT_PCM_8_24_BIT:
112     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
113     case AUDIO_FORMAT_PCM_32_BIT:
114         return ENCODING_PCM_FLOAT;
115 
116     case AUDIO_FORMAT_AC3:
117         return ENCODING_AC3;
118     case AUDIO_FORMAT_E_AC3:
119         return ENCODING_E_AC3;
120     case AUDIO_FORMAT_DTS:
121         return ENCODING_DTS;
122     case AUDIO_FORMAT_DTS_HD:
123         return ENCODING_DTS_HD;
124     case AUDIO_FORMAT_MP3:
125         return ENCODING_MP3;
126     case AUDIO_FORMAT_AAC_LC:
127         return ENCODING_AAC_LC;
128     case AUDIO_FORMAT_AAC_HE_V1:
129         return ENCODING_AAC_HE_V1;
130     case AUDIO_FORMAT_AAC_HE_V2:
131         return ENCODING_AAC_HE_V2;
132     case AUDIO_FORMAT_IEC61937:
133         return ENCODING_IEC61937;
134     case AUDIO_FORMAT_DOLBY_TRUEHD:
135         return ENCODING_DOLBY_TRUEHD;
136     case AUDIO_FORMAT_AAC_ELD:
137         return ENCODING_AAC_ELD;
138     case AUDIO_FORMAT_AAC_XHE:
139         return ENCODING_AAC_XHE;
140     case AUDIO_FORMAT_AC4:
141         return ENCODING_AC4;
142     case AUDIO_FORMAT_E_AC3_JOC:
143         return ENCODING_E_AC3_JOC;
144     case AUDIO_FORMAT_MAT:
145     case AUDIO_FORMAT_MAT_1_0:
146     case AUDIO_FORMAT_MAT_2_0:
147     case AUDIO_FORMAT_MAT_2_1:
148         return ENCODING_DOLBY_MAT;
149     case AUDIO_FORMAT_OPUS:
150         return ENCODING_OPUS;
151     case AUDIO_FORMAT_DEFAULT:
152         return ENCODING_DEFAULT;
153     default:
154         return ENCODING_INVALID;
155     }
156 }
157 
158 // This function converts Java channel masks to a native channel mask.
159 // validity should be checked with audio_is_output_channel().
nativeChannelMaskFromJavaChannelMasks(jint channelPositionMask,jint channelIndexMask)160 static inline audio_channel_mask_t nativeChannelMaskFromJavaChannelMasks(
161         jint channelPositionMask, jint channelIndexMask)
162 {
163     // 0 is the java android.media.AudioFormat.CHANNEL_INVALID value
164     if (channelIndexMask != 0) {  // channel index mask takes priority
165         // To convert to a native channel mask, the Java channel index mask
166         // requires adding the index representation.
167         return audio_channel_mask_from_representation_and_bits(
168                         AUDIO_CHANNEL_REPRESENTATION_INDEX,
169                         channelIndexMask);
170     }
171     // To convert to a native channel mask, the Java channel position mask
172     // requires a shift by 2 to skip the two deprecated channel
173     // configurations "default" and "mono".
174     return (audio_channel_mask_t)((uint32_t)channelPositionMask >> 2);
175 }
176 
outChannelMaskToNative(int channelMask)177 static inline audio_channel_mask_t outChannelMaskToNative(int channelMask)
178 {
179     switch (channelMask) {
180     case CHANNEL_OUT_DEFAULT:
181     case CHANNEL_INVALID:
182         return AUDIO_CHANNEL_NONE;
183     default:
184         return (audio_channel_mask_t)(channelMask>>2);
185     }
186 }
187 
outChannelMaskFromNative(audio_channel_mask_t nativeMask)188 static inline int outChannelMaskFromNative(audio_channel_mask_t nativeMask)
189 {
190     switch (nativeMask) {
191     case AUDIO_CHANNEL_NONE:
192         return CHANNEL_OUT_DEFAULT;
193     default:
194         return (int)nativeMask<<2;
195     }
196 }
197 
inChannelMaskToNative(int channelMask)198 static inline audio_channel_mask_t inChannelMaskToNative(int channelMask)
199 {
200     switch (channelMask) {
201         case CHANNEL_IN_DEFAULT:
202             return AUDIO_CHANNEL_NONE;
203         default:
204             return (audio_channel_mask_t)channelMask;
205     }
206 }
207 
inChannelMaskFromNative(audio_channel_mask_t nativeMask)208 static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask)
209 {
210     switch (nativeMask) {
211         case AUDIO_CHANNEL_NONE:
212             return CHANNEL_IN_DEFAULT;
213         default:
214             return (int)nativeMask;
215     }
216 }
217 
218 #endif // ANDROID_MEDIA_AUDIOFORMAT_H
219