Shunkai Yao | 922576f | 2024-10-17 00:05:58 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2024 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_Eraser" |
| 18 | |
| 19 | #include "Eraser.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::EraserSw; |
| 29 | using aidl::android::hardware::audio::effect::getEffectImplUuidEraserSw; |
| 30 | using aidl::android::hardware::audio::effect::getEffectTypeUuidEraser; |
| 31 | using aidl::android::hardware::audio::effect::IEffect; |
| 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 | |
| 36 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 37 | std::shared_ptr<IEffect>* instanceSpp) { |
| 38 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEraserSw()) { |
| 39 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 40 | return EX_ILLEGAL_ARGUMENT; |
| 41 | } |
| 42 | if (!instanceSpp) { |
| 43 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 44 | return EX_ILLEGAL_ARGUMENT; |
| 45 | } |
| 46 | |
| 47 | *instanceSpp = ndk::SharedRefBase::make<EraserSw>(); |
| 48 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 49 | return EX_NONE; |
| 50 | } |
| 51 | |
| 52 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 53 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEraserSw()) { |
| 54 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 55 | return EX_ILLEGAL_ARGUMENT; |
| 56 | } |
| 57 | *_aidl_return = EraserSw::kDescriptor; |
| 58 | return EX_NONE; |
| 59 | } |
| 60 | |
| 61 | namespace aidl::android::hardware::audio::effect { |
| 62 | |
| 63 | const std::string EraserSw::kEffectName = "EraserSw"; |
| 64 | const Descriptor EraserSw::kDescriptor = { |
| 65 | .common = {.id = {.type = getEffectTypeUuidEraser(), .uuid = getEffectImplUuidEraserSw()}, |
| 66 | .flags = {.type = Flags::Type::INSERT, |
| 67 | .insert = Flags::Insert::FIRST, |
| 68 | .hwAcceleratorMode = Flags::HardwareAccelerator::NONE}, |
| 69 | .name = EraserSw::kEffectName, |
| 70 | .implementor = "The Android Open Source Project"}}; |
| 71 | |
| 72 | ndk::ScopedAStatus EraserSw::getDescriptor(Descriptor* _aidl_return) { |
| 73 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 74 | *_aidl_return = kDescriptor; |
| 75 | return ndk::ScopedAStatus::ok(); |
| 76 | } |
| 77 | |
| 78 | ndk::ScopedAStatus EraserSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 79 | RETURN_IF(Parameter::Specific::eraser != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 80 | "EffectNotSupported"); |
| 81 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 82 | |
| 83 | auto& param = specific.get<Parameter::Specific::eraser>(); |
| 84 | return mContext->setParam(param.getTag(), param); |
| 85 | } |
| 86 | |
| 87 | ndk::ScopedAStatus EraserSw::getParameterSpecific(const Parameter::Id& id, |
| 88 | Parameter::Specific* specific) { |
| 89 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 90 | |
| 91 | auto tag = id.getTag(); |
| 92 | RETURN_IF(Parameter::Id::eraserTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 93 | auto eraserId = id.get<Parameter::Id::eraserTag>(); |
| 94 | auto eraserTag = eraserId.getTag(); |
| 95 | switch (eraserTag) { |
| 96 | case Eraser::Id::commonTag: { |
| 97 | auto specificTag = eraserId.get<Eraser::Id::commonTag>(); |
| 98 | std::optional<Eraser> param = mContext->getParam(specificTag); |
| 99 | if (!param.has_value()) { |
| 100 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 101 | "EraserTagNotSupported"); |
| 102 | } |
| 103 | specific->set<Parameter::Specific::eraser>(param.value()); |
| 104 | break; |
| 105 | } |
| 106 | default: { |
| 107 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 108 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 109 | "EraserTagNotSupported"); |
| 110 | } |
| 111 | } |
| 112 | return ndk::ScopedAStatus::ok(); |
| 113 | } |
| 114 | |
| 115 | std::shared_ptr<EffectContext> EraserSw::createContext(const Parameter::Common& common) { |
| 116 | if (mContext) { |
| 117 | LOG(DEBUG) << __func__ << " context already exist"; |
| 118 | } else { |
| 119 | mContext = std::make_shared<EraserSwContext>(1 /* statusFmqDepth */, common); |
| 120 | } |
| 121 | return mContext; |
| 122 | } |
| 123 | |
| 124 | RetCode EraserSw::releaseContext() { |
| 125 | if (mContext) { |
| 126 | mContext.reset(); |
| 127 | } |
| 128 | return RetCode::SUCCESS; |
| 129 | } |
| 130 | |
| 131 | EraserSw::~EraserSw() { |
| 132 | cleanUp(); |
| 133 | LOG(DEBUG) << __func__; |
| 134 | } |
| 135 | |
| 136 | // Processing method running in EffectWorker thread. |
| 137 | IEffect::Status EraserSw::effectProcessImpl(float* in, float* out, int samples) { |
| 138 | RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext"); |
| 139 | return mContext->process(in, out, samples); |
| 140 | } |
| 141 | |
| 142 | EraserSwContext::EraserSwContext(int statusDepth, const Parameter::Common& common) |
| 143 | : EffectContext(statusDepth, common) { |
| 144 | LOG(DEBUG) << __func__; |
| 145 | } |
| 146 | |
| 147 | EraserSwContext::~EraserSwContext() { |
| 148 | LOG(DEBUG) << __func__; |
| 149 | } |
| 150 | |
| 151 | template <typename TAG> |
| 152 | std::optional<Eraser> EraserSwContext::getParam(TAG tag) { |
| 153 | if (mParamsMap.find(tag) != mParamsMap.end()) { |
| 154 | return mParamsMap.at(tag); |
| 155 | } |
| 156 | return std::nullopt; |
| 157 | } |
| 158 | |
| 159 | template <typename TAG> |
| 160 | ndk::ScopedAStatus EraserSwContext::setParam(TAG tag, Eraser eraser) { |
| 161 | mParamsMap[tag] = eraser; |
| 162 | return ndk::ScopedAStatus::ok(); |
| 163 | } |
| 164 | |
| 165 | IEffect::Status EraserSwContext::process(float* in, float* out, int samples) { |
| 166 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 167 | IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0}; |
| 168 | |
| 169 | const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask); |
| 170 | const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask); |
| 171 | if (inputChannelCount < outputChannelCount) { |
| 172 | LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount |
| 173 | << " out: " << outputChannelCount; |
| 174 | return status; |
| 175 | } |
| 176 | |
| 177 | int iFrames = samples / inputChannelCount; |
| 178 | for (int i = 0; i < iFrames; i++) { |
| 179 | std::memcpy(out, in, outputChannelCount); |
| 180 | in += inputChannelCount; |
| 181 | out += outputChannelCount; |
| 182 | } |
| 183 | return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount), |
| 184 | static_cast<int32_t>(iFrames * outputChannelCount)}; |
| 185 | } |
| 186 | |
| 187 | } // namespace aidl::android::hardware::audio::effect |