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