Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 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 std::vector<Range::SpatializerRange> SpatializerSw::kRanges = { |
Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 68 | MAKE_RANGE(Spatializer, spatializationLevel, Spatialization::Level::NONE, |
| 69 | Spatialization::Level::BED_PLUS_OBJECTS), |
| 70 | MAKE_RANGE(Spatializer, spatializationMode, Spatialization::Mode::BINAURAL, |
| 71 | Spatialization::Mode::TRANSAURAL), |
| 72 | MAKE_RANGE(Spatializer, headTrackingSensorId, std::numeric_limits<int>::min(), |
| 73 | std::numeric_limits<int>::max()), |
| 74 | MAKE_RANGE(Spatializer, headTrackingMode, HeadTracking::Mode::OTHER, |
| 75 | HeadTracking::Mode::RELATIVE_SCREEN), |
| 76 | MAKE_RANGE(Spatializer, headTrackingConnectionMode, |
| 77 | HeadTracking::ConnectionMode::FRAMEWORK_PROCESSED, |
| 78 | HeadTracking::ConnectionMode::DIRECT_TO_SENSOR_TUNNEL)}; |
| 79 | const Capability SpatializerSw::kCapability = {.range = {SpatializerSw::kRanges}}; |
| 80 | const Descriptor SpatializerSw::kDescriptor = { |
| 81 | .common = {.id = {.type = getEffectTypeUuidSpatializer(), |
| 82 | .uuid = getEffectImplUuidSpatializerSw()}, |
| 83 | .flags = {.type = Flags::Type::INSERT, |
| 84 | .insert = Flags::Insert::FIRST, |
| 85 | .hwAcceleratorMode = Flags::HardwareAccelerator::NONE}, |
| 86 | .name = SpatializerSw::kEffectName, |
| 87 | .implementor = "The Android Open Source Project"}, |
| 88 | .capability = SpatializerSw::kCapability}; |
| 89 | |
| 90 | ndk::ScopedAStatus SpatializerSw::getDescriptor(Descriptor* _aidl_return) { |
| 91 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 92 | *_aidl_return = kDescriptor; |
| 93 | return ndk::ScopedAStatus::ok(); |
| 94 | } |
| 95 | |
| 96 | ndk::ScopedAStatus SpatializerSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 97 | RETURN_IF(Parameter::Specific::spatializer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 98 | "EffectNotSupported"); |
| 99 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 100 | |
| 101 | auto& param = specific.get<Parameter::Specific::spatializer>(); |
| 102 | RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
| 103 | |
| 104 | return mContext->setParam(param.getTag(), param); |
| 105 | } |
| 106 | |
| 107 | ndk::ScopedAStatus SpatializerSw::getParameterSpecific(const Parameter::Id& id, |
| 108 | Parameter::Specific* specific) { |
Shunkai Yao | c7f5c2d | 2024-02-13 01:59:55 +0000 | [diff] [blame] | 109 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 110 | |
Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 111 | auto tag = id.getTag(); |
| 112 | RETURN_IF(Parameter::Id::spatializerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 113 | auto spatializerId = id.get<Parameter::Id::spatializerTag>(); |
| 114 | auto spatializerTag = spatializerId.getTag(); |
| 115 | switch (spatializerTag) { |
| 116 | case Spatializer::Id::commonTag: { |
| 117 | auto specificTag = spatializerId.get<Spatializer::Id::commonTag>(); |
| 118 | std::optional<Spatializer> param = mContext->getParam(specificTag); |
| 119 | if (!param.has_value()) { |
| 120 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 121 | EX_ILLEGAL_ARGUMENT, "SpatializerTagNotSupported"); |
| 122 | } |
| 123 | specific->set<Parameter::Specific::spatializer>(param.value()); |
| 124 | break; |
| 125 | } |
| 126 | default: { |
| 127 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 128 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 129 | "SpatializerTagNotSupported"); |
| 130 | } |
| 131 | } |
| 132 | return ndk::ScopedAStatus::ok(); |
| 133 | } |
| 134 | |
| 135 | std::shared_ptr<EffectContext> SpatializerSw::createContext(const Parameter::Common& common) { |
| 136 | if (mContext) { |
| 137 | LOG(DEBUG) << __func__ << " context already exist"; |
| 138 | } else { |
| 139 | mContext = std::make_shared<SpatializerSwContext>(1 /* statusFmqDepth */, common); |
| 140 | } |
| 141 | return mContext; |
| 142 | } |
| 143 | |
Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 144 | RetCode SpatializerSw::releaseContext() { |
| 145 | if (mContext) { |
| 146 | mContext.reset(); |
| 147 | } |
| 148 | return RetCode::SUCCESS; |
| 149 | } |
| 150 | |
| 151 | SpatializerSw::~SpatializerSw() { |
| 152 | cleanUp(); |
| 153 | LOG(DEBUG) << __func__; |
| 154 | } |
| 155 | |
| 156 | // Processing method running in EffectWorker thread. |
| 157 | IEffect::Status SpatializerSw::effectProcessImpl(float* in, float* out, int samples) { |
| 158 | RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext"); |
| 159 | return mContext->process(in, out, samples); |
| 160 | } |
| 161 | |
| 162 | SpatializerSwContext::SpatializerSwContext(int statusDepth, const Parameter::Common& common) |
| 163 | : EffectContext(statusDepth, common) { |
| 164 | LOG(DEBUG) << __func__; |
| 165 | } |
| 166 | |
| 167 | SpatializerSwContext::~SpatializerSwContext() { |
| 168 | LOG(DEBUG) << __func__; |
| 169 | } |
| 170 | |
| 171 | template <typename TAG> |
| 172 | std::optional<Spatializer> SpatializerSwContext::getParam(TAG tag) { |
| 173 | if (mParamsMap.find(tag) != mParamsMap.end()) { |
| 174 | return mParamsMap.at(tag); |
| 175 | } |
Shunkai Yao | 759c9cb | 2024-02-26 18:24:23 +0000 | [diff] [blame] | 176 | if (tag == Spatializer::supportedChannelLayout) { |
| 177 | return Spatializer::make<Spatializer::supportedChannelLayout>( |
| 178 | {AudioChannelLayout::make<AudioChannelLayout::layoutMask>( |
| 179 | AudioChannelLayout::LAYOUT_5POINT1)}); |
| 180 | } |
Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 181 | return std::nullopt; |
| 182 | } |
| 183 | |
| 184 | template <typename TAG> |
| 185 | ndk::ScopedAStatus SpatializerSwContext::setParam(TAG tag, Spatializer spatializer) { |
Shunkai Yao | 759c9cb | 2024-02-26 18:24:23 +0000 | [diff] [blame] | 186 | RETURN_IF(tag == Spatializer::supportedChannelLayout, EX_ILLEGAL_ARGUMENT, |
| 187 | "supportedChannelLayoutGetOnly"); |
| 188 | |
Shunkai Yao | f60fc37 | 2023-12-12 17:48:18 +0000 | [diff] [blame] | 189 | mParamsMap[tag] = spatializer; |
| 190 | return ndk::ScopedAStatus::ok(); |
| 191 | } |
| 192 | |
| 193 | IEffect::Status SpatializerSwContext::process(float* in, float* out, int samples) { |
| 194 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 195 | IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0}; |
| 196 | |
| 197 | const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask); |
| 198 | const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask); |
| 199 | if (outputChannelCount < 2 || inputChannelCount < outputChannelCount) { |
| 200 | LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount |
| 201 | << " out: " << outputChannelCount; |
| 202 | return status; |
| 203 | } |
| 204 | |
| 205 | int iFrames = samples / inputChannelCount; |
| 206 | for (int i = 0; i < iFrames; i++) { |
| 207 | std::memcpy(out, in, outputChannelCount); |
| 208 | in += inputChannelCount; |
| 209 | out += outputChannelCount; |
| 210 | } |
| 211 | return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount), |
| 212 | static_cast<int32_t>(iFrames * outputChannelCount)}; |
| 213 | } |
| 214 | |
| 215 | } // namespace aidl::android::hardware::audio::effect |