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