1 /*
2 * Copyright (C) 2023 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 #define LOG_TAG "AHAL_SpatializerSw"
18
19 #include "SpatializerSw.h"
20
21 #include <android-base/logging.h>
22 #include <system/audio_effects/effect_uuid.h>
23
24 #include <optional>
25
26 using aidl::android::hardware::audio::common::getChannelCount;
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::getEffectImplUuidSpatializerSw;
29 using aidl::android::hardware::audio::effect::getEffectTypeUuidSpatializer;
30 using aidl::android::hardware::audio::effect::IEffect;
31 using aidl::android::hardware::audio::effect::SpatializerSw;
32 using aidl::android::hardware::audio::effect::State;
33 using aidl::android::media::audio::common::AudioChannelLayout;
34 using aidl::android::media::audio::common::AudioUuid;
35 using aidl::android::media::audio::common::HeadTracking;
36 using aidl::android::media::audio::common::Spatialization;
37
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)38 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
39 std::shared_ptr<IEffect>* instanceSpp) {
40 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidSpatializerSw()) {
41 LOG(ERROR) << __func__ << "uuid not supported";
42 return EX_ILLEGAL_ARGUMENT;
43 }
44 if (!instanceSpp) {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48
49 *instanceSpp = ndk::SharedRefBase::make<SpatializerSw>();
50 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
51 return EX_NONE;
52 }
53
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)54 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
55 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidSpatializerSw()) {
56 LOG(ERROR) << __func__ << "uuid not supported";
57 return EX_ILLEGAL_ARGUMENT;
58 }
59 *_aidl_return = SpatializerSw::kDescriptor;
60 return EX_NONE;
61 }
62
63 namespace aidl::android::hardware::audio::effect {
64
65 const std::string SpatializerSw::kEffectName = "SpatializerSw";
66
67 const AudioChannelLayout kSupportedChannelMask =
68 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
69 AudioChannelLayout::LAYOUT_5POINT1);
70 const std::vector<Range::SpatializerRange> SpatializerSw::kRanges = {
71 MAKE_RANGE(Spatializer, supportedChannelLayout, {kSupportedChannelMask},
72 {kSupportedChannelMask}),
73 MAKE_RANGE(Spatializer, spatializationLevel, Spatialization::Level::NONE,
74 Spatialization::Level::BED_PLUS_OBJECTS),
75 MAKE_RANGE(Spatializer, spatializationMode, Spatialization::Mode::BINAURAL,
76 Spatialization::Mode::TRANSAURAL),
77 MAKE_RANGE(Spatializer, headTrackingSensorId, std::numeric_limits<int>::min(),
78 std::numeric_limits<int>::max()),
79 MAKE_RANGE(Spatializer, headTrackingMode, HeadTracking::Mode::OTHER,
80 HeadTracking::Mode::RELATIVE_SCREEN),
81 MAKE_RANGE(Spatializer, headTrackingConnectionMode,
82 HeadTracking::ConnectionMode::FRAMEWORK_PROCESSED,
83 HeadTracking::ConnectionMode::DIRECT_TO_SENSOR_TUNNEL)};
84 const Capability SpatializerSw::kCapability = {.range = {SpatializerSw::kRanges}};
85 const Descriptor SpatializerSw::kDescriptor = {
86 .common = {.id = {.type = getEffectTypeUuidSpatializer(),
87 .uuid = getEffectImplUuidSpatializerSw()},
88 .flags = {.type = Flags::Type::INSERT,
89 .insert = Flags::Insert::FIRST,
90 .hwAcceleratorMode = Flags::HardwareAccelerator::NONE},
91 .name = SpatializerSw::kEffectName,
92 .implementor = "The Android Open Source Project"},
93 .capability = SpatializerSw::kCapability};
94
getDescriptor(Descriptor * _aidl_return)95 ndk::ScopedAStatus SpatializerSw::getDescriptor(Descriptor* _aidl_return) {
96 LOG(DEBUG) << __func__ << kDescriptor.toString();
97 *_aidl_return = kDescriptor;
98 return ndk::ScopedAStatus::ok();
99 }
100
setParameterSpecific(const Parameter::Specific & specific)101 ndk::ScopedAStatus SpatializerSw::setParameterSpecific(const Parameter::Specific& specific) {
102 RETURN_IF(Parameter::Specific::spatializer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
103 "EffectNotSupported");
104 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
105
106 auto& param = specific.get<Parameter::Specific::spatializer>();
107 RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
108
109 return mContext->setParam(param.getTag(), param);
110 }
111
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)112 ndk::ScopedAStatus SpatializerSw::getParameterSpecific(const Parameter::Id& id,
113 Parameter::Specific* specific) {
114 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
115
116 auto tag = id.getTag();
117 RETURN_IF(Parameter::Id::spatializerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
118 auto spatializerId = id.get<Parameter::Id::spatializerTag>();
119 auto spatializerTag = spatializerId.getTag();
120 switch (spatializerTag) {
121 case Spatializer::Id::commonTag: {
122 auto specificTag = spatializerId.get<Spatializer::Id::commonTag>();
123 std::optional<Spatializer> param = mContext->getParam(specificTag);
124 if (!param.has_value()) {
125 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
126 EX_ILLEGAL_ARGUMENT, "SpatializerTagNotSupported");
127 }
128 specific->set<Parameter::Specific::spatializer>(param.value());
129 break;
130 }
131 default: {
132 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
133 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
134 "SpatializerTagNotSupported");
135 }
136 }
137 return ndk::ScopedAStatus::ok();
138 }
139
createContext(const Parameter::Common & common)140 std::shared_ptr<EffectContext> SpatializerSw::createContext(const Parameter::Common& common) {
141 if (common.input.base.channelMask != kSupportedChannelMask) {
142 LOG(ERROR) << __func__
143 << " channelMask not supported: " << common.input.base.channelMask.toString();
144 return nullptr;
145 }
146 if (mContext) {
147 LOG(DEBUG) << __func__ << " context already exist";
148 } else {
149 mContext = std::make_shared<SpatializerSwContext>(1 /* statusFmqDepth */, common);
150 }
151 return mContext;
152 }
153
releaseContext()154 RetCode SpatializerSw::releaseContext() {
155 if (mContext) {
156 mContext.reset();
157 }
158 return RetCode::SUCCESS;
159 }
160
~SpatializerSw()161 SpatializerSw::~SpatializerSw() {
162 cleanUp();
163 LOG(DEBUG) << __func__;
164 }
165
166 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)167 IEffect::Status SpatializerSw::effectProcessImpl(float* in, float* out, int samples) {
168 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
169 return mContext->process(in, out, samples);
170 }
171
SpatializerSwContext(int statusDepth,const Parameter::Common & common)172 SpatializerSwContext::SpatializerSwContext(int statusDepth, const Parameter::Common& common)
173 : EffectContext(statusDepth, common) {
174 LOG(DEBUG) << __func__;
175 }
176
~SpatializerSwContext()177 SpatializerSwContext::~SpatializerSwContext() {
178 LOG(DEBUG) << __func__;
179 }
180
181 template <typename TAG>
getParam(TAG tag)182 std::optional<Spatializer> SpatializerSwContext::getParam(TAG tag) {
183 if (mParamsMap.find(tag) != mParamsMap.end()) {
184 return mParamsMap.at(tag);
185 }
186 if (tag == Spatializer::supportedChannelLayout) {
187 return Spatializer::make<Spatializer::supportedChannelLayout>(
188 {AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
189 AudioChannelLayout::LAYOUT_5POINT1)});
190 }
191 return std::nullopt;
192 }
193
194 template <typename TAG>
setParam(TAG tag,Spatializer spatializer)195 ndk::ScopedAStatus SpatializerSwContext::setParam(TAG tag, Spatializer spatializer) {
196 RETURN_IF(tag == Spatializer::supportedChannelLayout, EX_ILLEGAL_ARGUMENT,
197 "supportedChannelLayoutGetOnly");
198
199 mParamsMap[tag] = spatializer;
200 return ndk::ScopedAStatus::ok();
201 }
202
process(float * in,float * out,int samples)203 IEffect::Status SpatializerSwContext::process(float* in, float* out, int samples) {
204 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
205 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
206
207 const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask);
208 const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask);
209 if (outputChannelCount < 2 || inputChannelCount < outputChannelCount) {
210 LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount
211 << " out: " << outputChannelCount;
212 return status;
213 }
214
215 int iFrames = samples / inputChannelCount;
216 for (int i = 0; i < iFrames; i++) {
217 std::memcpy(out, in, outputChannelCount);
218 in += inputChannelCount;
219 out += outputChannelCount;
220 }
221 return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount),
222 static_cast<int32_t>(iFrames * outputChannelCount)};
223 }
224
225 } // namespace aidl::android::hardware::audio::effect
226