• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
42 #define ENCODING_INVALID    0
43 #define ENCODING_DEFAULT    1
44 
45 
46 
47 #define CHANNEL_INVALID 0
48 #define CHANNEL_OUT_DEFAULT 1
49 
audioFormatToNative(int audioFormat)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;
57     case ENCODING_PCM_FLOAT:
58         return AUDIO_FORMAT_PCM_FLOAT;
59     case ENCODING_AC3:
60         return AUDIO_FORMAT_AC3;
61     case ENCODING_E_AC3:
62         return AUDIO_FORMAT_E_AC3;
63     case ENCODING_DTS:
64         return AUDIO_FORMAT_DTS;
65     case ENCODING_DTS_HD:
66         return AUDIO_FORMAT_DTS_HD;
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;
75     case ENCODING_IEC61937:
76         return AUDIO_FORMAT_IEC61937;
77     case ENCODING_DOLBY_TRUEHD:
78         return AUDIO_FORMAT_DOLBY_TRUEHD;
79     case ENCODING_AAC_ELD:
80         return AUDIO_FORMAT_AAC_ELD;
81     case ENCODING_AAC_XHE:
82         return AUDIO_FORMAT_AAC_XHE;
83     case ENCODING_AC4:
84         return AUDIO_FORMAT_AC4;
85     case ENCODING_E_AC3_JOC:
86         return AUDIO_FORMAT_E_AC3_JOC;
87     case ENCODING_DEFAULT:
88         return AUDIO_FORMAT_DEFAULT;
89     case ENCODING_DOLBY_MAT:
90         return AUDIO_FORMAT_MAT;
91     default:
92         return AUDIO_FORMAT_INVALID;
93     }
94 }
95 
audioFormatFromNative(audio_format_t nativeFormat)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;
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 
112     case AUDIO_FORMAT_AC3:
113         return ENCODING_AC3;
114     case AUDIO_FORMAT_E_AC3:
115         return ENCODING_E_AC3;
116     case AUDIO_FORMAT_DTS:
117         return ENCODING_DTS;
118     case AUDIO_FORMAT_DTS_HD:
119         return ENCODING_DTS_HD;
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;
128     case AUDIO_FORMAT_IEC61937:
129         return ENCODING_IEC61937;
130     case AUDIO_FORMAT_DOLBY_TRUEHD:
131         return ENCODING_DOLBY_TRUEHD;
132     case AUDIO_FORMAT_AAC_ELD:
133         return ENCODING_AAC_ELD;
134     case AUDIO_FORMAT_AAC_XHE:
135         return ENCODING_AAC_XHE;
136     case AUDIO_FORMAT_AC4:
137         return ENCODING_AC4;
138     case AUDIO_FORMAT_E_AC3_JOC:
139         return ENCODING_E_AC3_JOC;
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;
145     case AUDIO_FORMAT_DEFAULT:
146         return ENCODING_DEFAULT;
147     default:
148         return ENCODING_INVALID;
149     }
150 }
151 
152 // This function converts Java channel masks to a native channel mask.
153 // validity should be checked with audio_is_output_channel().
nativeChannelMaskFromJavaChannelMasks(jint channelPositionMask,jint channelIndexMask)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 
outChannelMaskToNative(int channelMask)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 
outChannelMaskFromNative(audio_channel_mask_t nativeMask)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 
inChannelMaskToNative(int channelMask)192 static inline audio_channel_mask_t inChannelMaskToNative(int channelMask)
193 {
194     return (audio_channel_mask_t)channelMask;
195 }
196 
inChannelMaskFromNative(audio_channel_mask_t nativeMask)197 static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask)
198 {
199     return (int)nativeMask;
200 }
201 
202 #endif // ANDROID_MEDIA_AUDIOFORMAT_H
203