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_INVALID    0
35 #define ENCODING_DEFAULT    1
36 
37 
38 
39 #define CHANNEL_INVALID 0
40 #define CHANNEL_OUT_DEFAULT 1
41 
audioFormatToNative(int audioFormat)42 static inline audio_format_t audioFormatToNative(int audioFormat)
43 {
44     switch (audioFormat) {
45     case ENCODING_PCM_16BIT:
46         return AUDIO_FORMAT_PCM_16_BIT;
47     case ENCODING_PCM_8BIT:
48         return AUDIO_FORMAT_PCM_8_BIT;
49     case ENCODING_PCM_FLOAT:
50         return AUDIO_FORMAT_PCM_FLOAT;
51     case ENCODING_AC3:
52         return AUDIO_FORMAT_AC3;
53     case ENCODING_E_AC3:
54         return AUDIO_FORMAT_E_AC3;
55     case ENCODING_DTS:
56         return AUDIO_FORMAT_DTS;
57     case ENCODING_DTS_HD:
58         return AUDIO_FORMAT_DTS_HD;
59     case ENCODING_MP3:
60         return AUDIO_FORMAT_MP3;
61     case ENCODING_AAC_LC:
62         return AUDIO_FORMAT_AAC_LC;
63     case ENCODING_AAC_HE_V1:
64         return AUDIO_FORMAT_AAC_HE_V1;
65     case ENCODING_AAC_HE_V2:
66         return AUDIO_FORMAT_AAC_HE_V2;
67     case ENCODING_DEFAULT:
68         return AUDIO_FORMAT_DEFAULT;
69     default:
70         return AUDIO_FORMAT_INVALID;
71     }
72 }
73 
audioFormatFromNative(audio_format_t nativeFormat)74 static inline int audioFormatFromNative(audio_format_t nativeFormat)
75 {
76     switch (nativeFormat) {
77     case AUDIO_FORMAT_PCM_16_BIT:
78         return ENCODING_PCM_16BIT;
79     case AUDIO_FORMAT_PCM_8_BIT:
80         return ENCODING_PCM_8BIT;
81     case AUDIO_FORMAT_PCM_FLOAT:
82         return ENCODING_PCM_FLOAT;
83 
84     // map these to ENCODING_PCM_FLOAT
85     case AUDIO_FORMAT_PCM_8_24_BIT:
86     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
87     case AUDIO_FORMAT_PCM_32_BIT:
88         return ENCODING_PCM_FLOAT;
89 
90     case AUDIO_FORMAT_AC3:
91         return ENCODING_AC3;
92     case AUDIO_FORMAT_E_AC3:
93         return ENCODING_E_AC3;
94     case AUDIO_FORMAT_DTS:
95         return ENCODING_DTS;
96     case AUDIO_FORMAT_DTS_HD:
97         return ENCODING_DTS_HD;
98     case AUDIO_FORMAT_MP3:
99         return ENCODING_MP3;
100     case AUDIO_FORMAT_AAC_LC:
101         return ENCODING_AAC_LC;
102     case AUDIO_FORMAT_AAC_HE_V1:
103         return ENCODING_AAC_HE_V1;
104     case AUDIO_FORMAT_AAC_HE_V2:
105         return ENCODING_AAC_HE_V2;
106     case AUDIO_FORMAT_DEFAULT:
107         return ENCODING_DEFAULT;
108     default:
109         return ENCODING_INVALID;
110     }
111 }
112 
outChannelMaskToNative(int channelMask)113 static inline audio_channel_mask_t outChannelMaskToNative(int channelMask)
114 {
115     switch (channelMask) {
116     case CHANNEL_OUT_DEFAULT:
117     case CHANNEL_INVALID:
118         return AUDIO_CHANNEL_NONE;
119     default:
120         return (audio_channel_mask_t)(channelMask>>2);
121     }
122 }
123 
outChannelMaskFromNative(audio_channel_mask_t nativeMask)124 static inline int outChannelMaskFromNative(audio_channel_mask_t nativeMask)
125 {
126     switch (nativeMask) {
127     case AUDIO_CHANNEL_NONE:
128         return CHANNEL_OUT_DEFAULT;
129     default:
130         return (int)nativeMask<<2;
131     }
132 }
133 
inChannelMaskToNative(int channelMask)134 static inline audio_channel_mask_t inChannelMaskToNative(int channelMask)
135 {
136     return (audio_channel_mask_t)channelMask;
137 }
138 
inChannelMaskFromNative(audio_channel_mask_t nativeMask)139 static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask)
140 {
141     return (int)nativeMask;
142 }
143 
144 #endif // ANDROID_MEDIA_AUDIOFORMAT_H
145