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