blob: da4cdb6762833d041af8cbbb2b22dfdb581027d4 [file] [log] [blame]
Glenn Kastenfe834d32014-01-08 14:49:08 -08001/*
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
Eric Laurentb4fae5b2016-07-07 18:58:11 -070023#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
Jean-Michel Trivi980d38f2018-01-08 15:43:35 -080036#define ENCODING_AAC_ELD 15
37#define ENCODING_AAC_XHE 16
Jean-Michel Trivi5c2aae02018-01-19 17:53:40 -080038#define ENCODING_AC4 17
Mikhail Naganov43a8ebb2018-01-24 09:21:38 -080039#define ENCODING_E_AC3_JOC 18
Eric Laurentb4fae5b2016-07-07 18:58:11 -070040
Eric Laurentff0d9f02014-06-09 17:23:02 -070041#define ENCODING_INVALID 0
42#define ENCODING_DEFAULT 1
43
44
Eric Laurentb69681c2014-05-19 19:02:51 -070045
46#define CHANNEL_INVALID 0
47#define CHANNEL_OUT_DEFAULT 1
Glenn Kastenfe834d32014-01-08 14:49:08 -080048
49static 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;
Glenn Kasten313f5982014-04-30 18:02:08 -070056 case ENCODING_PCM_FLOAT:
57 return AUDIO_FORMAT_PCM_FLOAT;
Eric Laurentff0d9f02014-06-09 17:23:02 -070058 case ENCODING_AC3:
59 return AUDIO_FORMAT_AC3;
60 case ENCODING_E_AC3:
61 return AUDIO_FORMAT_E_AC3;
Phil Burke12189d2015-03-31 14:38:30 -070062 case ENCODING_DTS:
63 return AUDIO_FORMAT_DTS;
64 case ENCODING_DTS_HD:
65 return AUDIO_FORMAT_DTS_HD;
Eric Laurentcae34662015-05-19 16:46:52 -070066 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;
Eric Laurentb4fae5b2016-07-07 18:58:11 -070074 case ENCODING_DOLBY_TRUEHD:
75 return AUDIO_FORMAT_DOLBY_TRUEHD;
Phil Burk43f4b272016-01-27 15:35:20 -080076 case ENCODING_IEC61937:
77 return AUDIO_FORMAT_IEC61937;
Jean-Michel Trivi980d38f2018-01-08 15:43:35 -080078 case ENCODING_AAC_ELD:
79 return AUDIO_FORMAT_AAC_ELD;
80 case ENCODING_AAC_XHE:
Jean-Michel Trivief884502018-03-07 15:02:44 -080081 return AUDIO_FORMAT_AAC_XHE;
Jean-Michel Trivi5c2aae02018-01-19 17:53:40 -080082 case ENCODING_AC4:
83 return AUDIO_FORMAT_AC4;
Mikhail Naganov6d0c5f62018-03-06 17:37:32 -080084 case ENCODING_E_AC3_JOC:
85 return AUDIO_FORMAT_E_AC3_JOC;
Eric Laurentb69681c2014-05-19 19:02:51 -070086 case ENCODING_DEFAULT:
87 return AUDIO_FORMAT_DEFAULT;
Glenn Kastenfe834d32014-01-08 14:49:08 -080088 default:
89 return AUDIO_FORMAT_INVALID;
90 }
91}
92
Eric Laurentb69681c2014-05-19 19:02:51 -070093static 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;
Paul McLean4e5e9e92015-06-17 10:10:11 -0700102
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
Eric Laurentff0d9f02014-06-09 17:23:02 -0700109 case AUDIO_FORMAT_AC3:
110 return ENCODING_AC3;
111 case AUDIO_FORMAT_E_AC3:
112 return ENCODING_E_AC3;
Phil Burke12189d2015-03-31 14:38:30 -0700113 case AUDIO_FORMAT_DTS:
114 return ENCODING_DTS;
115 case AUDIO_FORMAT_DTS_HD:
116 return ENCODING_DTS_HD;
Eric Laurentcae34662015-05-19 16:46:52 -0700117 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;
Phil Burk43f4b272016-01-27 15:35:20 -0800125 case AUDIO_FORMAT_IEC61937:
126 return ENCODING_IEC61937;
Eric Laurentb4fae5b2016-07-07 18:58:11 -0700127 case AUDIO_FORMAT_DOLBY_TRUEHD:
128 return ENCODING_DOLBY_TRUEHD;
Jean-Michel Trivi980d38f2018-01-08 15:43:35 -0800129 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;
Jean-Michel Trivi5c2aae02018-01-19 17:53:40 -0800134 case AUDIO_FORMAT_AC4:
135 return ENCODING_AC4;
Mikhail Naganov6d0c5f62018-03-06 17:37:32 -0800136 case AUDIO_FORMAT_E_AC3_JOC:
137 return ENCODING_E_AC3_JOC;
Eric Laurentb69681c2014-05-19 19:02:51 -0700138 case AUDIO_FORMAT_DEFAULT:
139 return ENCODING_DEFAULT;
140 default:
141 return ENCODING_INVALID;
142 }
143}
144
Jean-Michel Trivi980d38f2018-01-08 15:43:35 -0800145// This function converts Java channel masks to a native channel mask.
146// validity should be checked with audio_is_output_channel().
147static 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
Eric Laurentb69681c2014-05-19 19:02:51 -0700164static 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
175static 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
185static inline audio_channel_mask_t inChannelMaskToNative(int channelMask)
186{
187 return (audio_channel_mask_t)channelMask;
188}
189
190static inline int inChannelMaskFromNative(audio_channel_mask_t nativeMask)
191{
192 return (int)nativeMask;
193}
194
Glenn Kastenfe834d32014-01-08 14:49:08 -0800195#endif // ANDROID_MEDIA_AUDIOFORMAT_H