1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <cstdlib> 20 #include <memory> 21 #include <vector> 22 23 #include <Utils.h> 24 #include <aidl/android/hardware/audio/effect/BnEffect.h> 25 #include <fmq/AidlMessageQueue.h> 26 27 #include "effect-impl/EffectImpl.h" 28 29 namespace aidl::android::hardware::audio::effect { 30 31 class DynamicsProcessingSwContext final : public EffectContext { 32 public: DynamicsProcessingSwContext(int statusDepth,const Parameter::Common & common)33 DynamicsProcessingSwContext(int statusDepth, const Parameter::Common& common) 34 : EffectContext(statusDepth, common), 35 mChannelCount(::aidl::android::hardware::audio::common::getChannelCount( 36 common.input.base.channelMask)), 37 mPreEqChCfgs(mChannelCount, {.channel = kInvalidChannelId}), 38 mPostEqChCfgs(mChannelCount, {.channel = kInvalidChannelId}), 39 mMbcChCfgs(mChannelCount, {.channel = kInvalidChannelId}), 40 mLimiterCfgs(mChannelCount, {.channel = kInvalidChannelId}) { 41 LOG(DEBUG) << __func__; 42 } 43 44 // utils 45 RetCode setChannelCfgs(const std::vector<DynamicsProcessing::ChannelConfig>& cfgs, 46 std::vector<DynamicsProcessing::ChannelConfig>& targetCfgs, 47 const DynamicsProcessing::StageEnablement& engineSetting); 48 49 RetCode setEqBandCfgs(const std::vector<DynamicsProcessing::EqBandConfig>& cfgs, 50 std::vector<DynamicsProcessing::EqBandConfig>& targetCfgs, 51 const DynamicsProcessing::StageEnablement& stage, 52 const std::vector<DynamicsProcessing::ChannelConfig>& channelConfig); 53 54 // set params 55 RetCode setCommon(const Parameter::Common& common) override; 56 RetCode setEngineArchitecture(const DynamicsProcessing::EngineArchitecture& cfg); 57 RetCode setPreEqChannelCfgs(const std::vector<DynamicsProcessing::ChannelConfig>& cfgs); 58 RetCode setPostEqChannelCfgs(const std::vector<DynamicsProcessing::ChannelConfig>& cfgs); 59 RetCode setMbcChannelCfgs(const std::vector<DynamicsProcessing::ChannelConfig>& cfgs); 60 RetCode setPreEqBandCfgs(const std::vector<DynamicsProcessing::EqBandConfig>& cfgs); 61 RetCode setPostEqBandCfgs(const std::vector<DynamicsProcessing::EqBandConfig>& cfgs); 62 RetCode setMbcBandCfgs(const std::vector<DynamicsProcessing::MbcBandConfig>& cfgs); 63 RetCode setLimiterCfgs(const std::vector<DynamicsProcessing::LimiterConfig>& cfgs); 64 RetCode setInputGainCfgs(const std::vector<DynamicsProcessing::InputGain>& cfgs); 65 66 // get params getEngineArchitecture()67 DynamicsProcessing::EngineArchitecture getEngineArchitecture() { return mEngineSettings; } getPreEqChannelCfgs()68 std::vector<DynamicsProcessing::ChannelConfig> getPreEqChannelCfgs() { return mPreEqChCfgs; } getPostEqChannelCfgs()69 std::vector<DynamicsProcessing::ChannelConfig> getPostEqChannelCfgs() { return mPostEqChCfgs; } getMbcChannelCfgs()70 std::vector<DynamicsProcessing::ChannelConfig> getMbcChannelCfgs() { return mMbcChCfgs; } getPreEqBandCfgs()71 std::vector<DynamicsProcessing::EqBandConfig> getPreEqBandCfgs() { return mPreEqChBands; } getPostEqBandCfgs()72 std::vector<DynamicsProcessing::EqBandConfig> getPostEqBandCfgs() { return mPostEqChBands; } getMbcBandCfgs()73 std::vector<DynamicsProcessing::MbcBandConfig> getMbcBandCfgs() { return mMbcChBands; } getLimiterCfgs()74 std::vector<DynamicsProcessing::LimiterConfig> getLimiterCfgs() { return mLimiterCfgs; } 75 std::vector<DynamicsProcessing::InputGain> getInputGainCfgs(); 76 77 private: 78 static constexpr int32_t kInvalidChannelId = -1; 79 size_t mChannelCount = 0; 80 DynamicsProcessing::EngineArchitecture mEngineSettings; 81 // Channel config vector with size of mChannelCount 82 std::vector<DynamicsProcessing::ChannelConfig> mPreEqChCfgs; 83 std::vector<DynamicsProcessing::ChannelConfig> mPostEqChCfgs; 84 std::vector<DynamicsProcessing::ChannelConfig> mMbcChCfgs; 85 std::vector<DynamicsProcessing::LimiterConfig> mLimiterCfgs; 86 std::vector<DynamicsProcessing::InputGain> mInputGainCfgs; 87 // Band config vector with size of mChannelCount * bandCount 88 std::vector<DynamicsProcessing::EqBandConfig> mPreEqChBands; 89 std::vector<DynamicsProcessing::EqBandConfig> mPostEqChBands; 90 std::vector<DynamicsProcessing::MbcBandConfig> mMbcChBands; 91 bool validateStageEnablement(const DynamicsProcessing::StageEnablement& enablement); 92 bool validateEngineConfig(const DynamicsProcessing::EngineArchitecture& engine); 93 bool validateEqBandConfig(const DynamicsProcessing::EqBandConfig& band, int maxChannel, 94 int maxBand, 95 const std::vector<DynamicsProcessing::ChannelConfig>& channelConfig); 96 bool validateMbcBandConfig(const DynamicsProcessing::MbcBandConfig& band, int maxChannel, 97 int maxBand, 98 const std::vector<DynamicsProcessing::ChannelConfig>& channelConfig); 99 bool validateLimiterConfig(const DynamicsProcessing::LimiterConfig& limiter, int maxChannel); 100 void resizeChannels(); 101 void resizeBands(); 102 }; // DynamicsProcessingSwContext 103 104 class DynamicsProcessingSw final : public EffectImpl { 105 public: 106 static const std::string kEffectName; 107 static const Capability kCapability; 108 static const Descriptor kDescriptor; DynamicsProcessingSw()109 DynamicsProcessingSw() { LOG(DEBUG) << __func__; } ~DynamicsProcessingSw()110 ~DynamicsProcessingSw() { 111 cleanUp(); 112 LOG(DEBUG) << __func__; 113 } 114 115 ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override; 116 ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) 117 REQUIRES(mImplMutex) override; 118 ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id, Parameter::Specific* specific) 119 REQUIRES(mImplMutex) override; 120 121 std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) 122 REQUIRES(mImplMutex) override; 123 RetCode releaseContext() REQUIRES(mImplMutex) override; 124 125 IEffect::Status effectProcessImpl(float* in, float* out, int samples) 126 REQUIRES(mImplMutex) override; getEffectName()127 std::string getEffectName() override { return kEffectName; }; 128 129 private: 130 static const DynamicsProcessing::EqBandConfig kEqBandConfigMin; 131 static const DynamicsProcessing::EqBandConfig kEqBandConfigMax; 132 static const Range::DynamicsProcessingRange kPreEqBandRange; 133 static const Range::DynamicsProcessingRange kPostEqBandRange; 134 static const std::vector<Range::DynamicsProcessingRange> kRanges; 135 std::shared_ptr<DynamicsProcessingSwContext> mContext GUARDED_BY(mImplMutex); 136 ndk::ScopedAStatus getParameterDynamicsProcessing(const DynamicsProcessing::Tag& tag, 137 Parameter::Specific* specific) 138 REQUIRES(mImplMutex); 139 140 }; // DynamicsProcessingSw 141 142 } // namespace aidl::android::hardware::audio::effect 143