1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define HST_LOG_TAG "HdiAuUtils"
17
18 #include "hdi_au_utils.h"
19
20 #include <utility>
21 #include <map>
22
23 namespace OHOS {
24 namespace Media {
25 namespace HosLitePlugin {
26 namespace {
27 struct PluginHdiAudioFormatTable {
28 AudioFormat hFormat;
29 bool isSigned;
30 bool isInterleaved;
31 bool isBigEndian;
32 OHOS::Media::Plugin::AudioSampleFormat pFormat;
33 };
34
35 PluginHdiAudioFormatTable g_phft[] = {
36 {AUDIO_FORMAT_PCM_8_BIT, false, true, false, OHOS::Media::Plugin::AudioSampleFormat::U8},
37 {AUDIO_FORMAT_PCM_8_BIT, false, false, false, OHOS::Media::Plugin::AudioSampleFormat::U8P},
38 {AUDIO_FORMAT_PCM_8_BIT, true, true, false, OHOS::Media::Plugin::AudioSampleFormat::S8},
39 {AUDIO_FORMAT_PCM_8_BIT, true, false, false, OHOS::Media::Plugin::AudioSampleFormat::S8P},
40 {AUDIO_FORMAT_PCM_16_BIT, false, true, false, OHOS::Media::Plugin::AudioSampleFormat::U16},
41 {AUDIO_FORMAT_PCM_16_BIT, false, false, false, OHOS::Media::Plugin::AudioSampleFormat::U16P},
42 {AUDIO_FORMAT_PCM_16_BIT, true, true, false, OHOS::Media::Plugin::AudioSampleFormat::S16},
43 {AUDIO_FORMAT_PCM_16_BIT, true, false, false, OHOS::Media::Plugin::AudioSampleFormat::S16P},
44 {AUDIO_FORMAT_PCM_32_BIT, false, true, false, OHOS::Media::Plugin::AudioSampleFormat::U32},
45 {AUDIO_FORMAT_PCM_32_BIT, false, false, false, OHOS::Media::Plugin::AudioSampleFormat::U32P},
46 {AUDIO_FORMAT_PCM_32_BIT, true, true, false, OHOS::Media::Plugin::AudioSampleFormat::S32},
47 {AUDIO_FORMAT_PCM_32_BIT, true, false, false, OHOS::Media::Plugin::AudioSampleFormat::S32P},
48 };
49
50 std::pair<uint32_t, AudioSampleRatesMask> g_phst[] = {
51 {0, AUDIO_SAMPLE_RATE_MASK_INVALID},
52 {8000, AUDIO_SAMPLE_RATE_MASK_8000},
53 {12000, AUDIO_SAMPLE_RATE_MASK_12000},
54 {11025, AUDIO_SAMPLE_RATE_MASK_11025},
55 {16000, AUDIO_SAMPLE_RATE_MASK_16000},
56 {22050, AUDIO_SAMPLE_RATE_MASK_22050},
57 {24000, AUDIO_SAMPLE_RATE_MASK_24000},
58 {32000, AUDIO_SAMPLE_RATE_MASK_32000},
59 {44100, AUDIO_SAMPLE_RATE_MASK_44100},
60 {48000, AUDIO_SAMPLE_RATE_MASK_48000},
61 {64000, AUDIO_SAMPLE_RATE_MASK_64000},
62 {96000, AUDIO_SAMPLE_RATE_MASK_96000},
63 };
64
65 std::pair<OHOS::Media::Plugin::AudioChannelLayout, AudioChannelMask> g_phcmt[] = {
66 {OHOS::Media::Plugin::AudioChannelLayout::MONO, AUDIO_CHANNEL_MONO},
67 {OHOS::Media::Plugin::AudioChannelLayout::STEREO, AUDIO_CHANNEL_STEREO}
68 };
69 } // namespace
70
PluginAuFormat2HdiAttrs(OHOS::Media::Plugin::AudioSampleFormat pFormat,AudioSampleAttributes & attrs)71 bool PluginAuFormat2HdiAttrs(OHOS::Media::Plugin::AudioSampleFormat pFormat, AudioSampleAttributes& attrs)
72 {
73 for (const auto& item : g_phft) {
74 if (item.pFormat == pFormat) {
75 attrs.format = item.hFormat;
76 attrs.isSignedData = item.isSigned;
77 attrs.isBigEndian = item.isBigEndian;
78 attrs.interleaved = item.isInterleaved;
79 return true;
80 }
81 }
82 return false;
83 }
84
HdiAuFormat2PluginFormat(::AudioSampleFormat hdiAudioFormat,OHOS::Media::Plugin::AudioSampleFormat & pluginFormat)85 bool HdiAuFormat2PluginFormat(::AudioSampleFormat hdiAudioFormat, OHOS::Media::Plugin::AudioSampleFormat& pluginFormat)
86 {
87 using PluginAudioSampleFormat = OHOS::Media::Plugin::AudioSampleFormat;
88 static const std::map<::AudioSampleFormat, PluginAudioSampleFormat> formatMap = {
89 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U8, PluginAudioSampleFormat::U8},
90 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U8P, PluginAudioSampleFormat::U8P},
91 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S8, PluginAudioSampleFormat::S8},
92 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S8P, PluginAudioSampleFormat::S8P},
93 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U16, PluginAudioSampleFormat::U16},
94 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U16P, PluginAudioSampleFormat::U16P},
95 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S16, PluginAudioSampleFormat::S16},
96 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S16P, PluginAudioSampleFormat::S16P},
97 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U24, PluginAudioSampleFormat::U24},
98 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U24, PluginAudioSampleFormat::U24P},
99 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S24, PluginAudioSampleFormat::S24},
100 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S24P, PluginAudioSampleFormat::S24P},
101 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U32, PluginAudioSampleFormat::U32},
102 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U32P, PluginAudioSampleFormat::U32P},
103 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S32, PluginAudioSampleFormat::S32},
104 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S32P, PluginAudioSampleFormat::S32P},
105 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U64, PluginAudioSampleFormat::U64},
106 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_U64P, PluginAudioSampleFormat::U64P},
107 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S64, PluginAudioSampleFormat::S64},
108 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_S64, PluginAudioSampleFormat::S64P},
109 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_F32, PluginAudioSampleFormat::F32},
110 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_F32P, PluginAudioSampleFormat::F32P},
111 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_F64, PluginAudioSampleFormat::F64},
112 {::AudioSampleFormat::AUDIO_SAMPLE_FORMAT_F64P, PluginAudioSampleFormat::F64P},
113 };
114 if (formatMap.count(hdiAudioFormat) == 0) {
115 return false;
116 }
117 pluginFormat = formatMap.find(hdiAudioFormat)->second;
118 return true;
119 }
120
HdiAttrs2PluginAuFormat(AudioSampleAttributes attrs,OHOS::Media::Plugin::AudioSampleFormat & pFormat)121 bool HdiAttrs2PluginAuFormat(AudioSampleAttributes attrs, OHOS::Media::Plugin::AudioSampleFormat& pFormat)
122 {
123 for (const auto& item : g_phft) {
124 if (attrs.format == item.hFormat && attrs.isSignedData == item.isSigned &&
125 attrs.interleaved == item.isInterleaved && attrs.isBigEndian == item.isBigEndian) {
126 pFormat = item.pFormat;
127 return true;
128 }
129 }
130 return false;
131 }
132
PluginSampleRate2HdiRate(uint32_t pRate,AudioSampleRatesMask & mask)133 bool PluginSampleRate2HdiRate(uint32_t pRate, AudioSampleRatesMask& mask)
134 {
135 for (const auto& item : g_phst) {
136 if (item.first == pRate) {
137 mask = item.second;
138 return true;
139 }
140 }
141 mask = AUDIO_SAMPLE_RATE_MASK_INVALID;
142 return false;
143 }
144
HdiSampleRatesMask2PluginRates(uint32_t mask)145 std::vector<uint32_t> HdiSampleRatesMask2PluginRates(uint32_t mask)
146 {
147 std::vector<uint32_t> ret;
148 for (const auto& pairKey : g_phst) {
149 if ((static_cast<uint32_t>(pairKey.second) & mask) != 0) {
150 ret.emplace_back(pairKey.first);
151 }
152 }
153 return ret;
154 }
155
HdiRate2PluginSampleRate(AudioSampleRatesMask mask,uint32_t & pRate)156 bool HdiRate2PluginSampleRate(AudioSampleRatesMask mask, uint32_t& pRate)
157 {
158 for (const auto& item : g_phst) {
159 if (item.second == mask) {
160 pRate = item.first;
161 return true;
162 }
163 }
164 return false;
165 }
166
PluginChannelLayout2HdiMask(OHOS::Media::Plugin::AudioChannelLayout layout,AudioChannelMask & mask)167 bool PluginChannelLayout2HdiMask(OHOS::Media::Plugin::AudioChannelLayout layout, AudioChannelMask& mask)
168 {
169 for (const auto& item : g_phcmt) {
170 if (item.first == layout) {
171 mask = item.second;
172 return true;
173 }
174 }
175 return false;
176 }
HdiMask2PluginChannelLayout(AudioChannelMask mask,OHOS::Media::Plugin::AudioChannelLayout & layout)177 bool HdiMask2PluginChannelLayout(AudioChannelMask mask, OHOS::Media::Plugin::AudioChannelLayout& layout)
178 {
179 for (const auto& item : g_phcmt) {
180 if (item.second == mask) {
181 layout = item.first;
182 return true;
183 }
184 }
185 return false;
186 }
GetPcmBytes(AudioFormat format)187 int32_t GetPcmBytes(AudioFormat format)
188 {
189 switch (format) {
190 case AUDIO_FORMAT_PCM_8_BIT:
191 return 1;
192 case AUDIO_FORMAT_PCM_16_BIT:
193 return 2; // 2
194 case AUDIO_FORMAT_PCM_24_BIT:
195 return 3; // 3
196 case AUDIO_FORMAT_PCM_32_BIT:
197 return 4; // 4
198 default:
199 return 0;
200 }
201 }
202 } // namespace HosLitePlugin
203 } // namespace Media
204 } // namespace OHOS
205