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 #define LOG_TAG "AHAL_DownmixContext"
18
19 #include <android-base/logging.h>
20
21 #include "DownmixContext.h"
22
23 using aidl::android::hardware::audio::common::getChannelCount;
24 using aidl::android::hardware::audio::effect::IEffect;
25 using aidl::android::media::audio::common::AudioChannelLayout;
26 using aidl::android::media::audio::common::AudioConfig;
27
28 namespace aidl::android::hardware::audio::effect {
29
30 namespace {
31
isChannelMaskValid(const AudioChannelLayout & channelMask)32 inline bool isChannelMaskValid(const AudioChannelLayout& channelMask) {
33 if (channelMask.getTag() != AudioChannelLayout::layoutMask) return false;
34 int chMask = channelMask.get<AudioChannelLayout::layoutMask>();
35 // check against unsupported channels (up to FCC_26)
36 constexpr uint32_t MAXIMUM_CHANNEL_MASK = AudioChannelLayout::LAYOUT_22POINT2 |
37 AudioChannelLayout::CHANNEL_FRONT_WIDE_LEFT |
38 AudioChannelLayout::CHANNEL_FRONT_WIDE_RIGHT;
39 if (chMask & ~MAXIMUM_CHANNEL_MASK) {
40 LOG(ERROR) << "Unsupported channels in " << (chMask & ~MAXIMUM_CHANNEL_MASK);
41 return false;
42 }
43 return true;
44 }
45
isStereoChannelMask(const AudioChannelLayout & channelMask)46 inline bool isStereoChannelMask(const AudioChannelLayout& channelMask) {
47 if (channelMask.getTag() != AudioChannelLayout::layoutMask) return false;
48
49 return channelMask.get<AudioChannelLayout::layoutMask>() == AudioChannelLayout::LAYOUT_STEREO;
50 }
51
52 } // namespace
53
validateCommonConfig(const Parameter::Common & common)54 bool DownmixContext::validateCommonConfig(const Parameter::Common& common) {
55 const AudioConfig& input = common.input;
56 const AudioConfig& output = common.output;
57 if (input.base.sampleRate != output.base.sampleRate) {
58 LOG(ERROR) << __func__ << ": SRC not supported, input: " << input.toString()
59 << " output: " << output.toString();
60 return false;
61 }
62
63 if (!isStereoChannelMask(output.base.channelMask)) {
64 LOG(ERROR) << __func__ << ": output should be stereo, not "
65 << output.base.channelMask.toString();
66 return false;
67 }
68
69 if (!isChannelMaskValid(input.base.channelMask)) {
70 LOG(ERROR) << __func__ << ": invalid input channel, " << input.base.channelMask.toString();
71 return false;
72 }
73
74 return true;
75 }
76
DownmixContext(int statusDepth,const Parameter::Common & common)77 DownmixContext::DownmixContext(int statusDepth, const Parameter::Common& common)
78 : EffectContext(statusDepth, common) {
79 mState = DOWNMIX_STATE_UNINITIALIZED;
80 init_params(common);
81 }
82
~DownmixContext()83 DownmixContext::~DownmixContext() {
84 mState = DOWNMIX_STATE_UNINITIALIZED;
85 }
86
enable()87 RetCode DownmixContext::enable() {
88 if (mState != DOWNMIX_STATE_INITIALIZED) {
89 return RetCode::ERROR_EFFECT_LIB_ERROR;
90 }
91 mState = DOWNMIX_STATE_ACTIVE;
92 return RetCode::SUCCESS;
93 }
94
disable()95 RetCode DownmixContext::disable() {
96 if (mState != DOWNMIX_STATE_ACTIVE) {
97 return RetCode::ERROR_EFFECT_LIB_ERROR;
98 }
99 mState = DOWNMIX_STATE_INITIALIZED;
100 return RetCode::SUCCESS;
101 }
102
downmixProcess(float * in,float * out,int samples)103 IEffect::Status DownmixContext::downmixProcess(float* in, float* out, int samples) {
104 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
105
106 if (in == nullptr || out == nullptr ||
107 getCommon().input.frameCount != getCommon().output.frameCount || getInputFrameSize() == 0) {
108 return status;
109 }
110
111 status = {EX_ILLEGAL_STATE, 0, 0};
112 if (mState == DOWNMIX_STATE_UNINITIALIZED) {
113 LOG(ERROR) << __func__ << "Trying to use an uninitialized downmixer";
114 return status;
115 } else if (mState == DOWNMIX_STATE_INITIALIZED) {
116 LOG(ERROR) << __func__ << "Trying to use a non-configured downmixer";
117 return status;
118 }
119
120 bool accumulate = false;
121 int frames = samples * sizeof(float) / getInputFrameSize();
122 if (mType == Downmix::Type::STRIP) {
123 while (frames) {
124 if (accumulate) {
125 out[0] = std::clamp(out[0] + in[0], -1.f, 1.f);
126 out[1] = std::clamp(out[1] + in[1], -1.f, 1.f);
127 } else {
128 out[0] = in[0];
129 out[1] = in[1];
130 }
131 in += mInputChannelCount;
132 out += 2;
133 frames--;
134 }
135 } else {
136 int chMask = mChMask.get<AudioChannelLayout::layoutMask>();
137 if (!mChannelMix.process(in, out, frames, accumulate, (audio_channel_mask_t)chMask)) {
138 LOG(ERROR) << "Multichannel configuration " << mChMask.toString()
139 << " is not supported";
140 return status;
141 }
142 }
143 int producedSamples = (samples / mInputChannelCount) << 1;
144 return {STATUS_OK, samples, producedSamples};
145 }
146
init_params(const Parameter::Common & common)147 void DownmixContext::init_params(const Parameter::Common& common) {
148 // when configuring the effect, do not allow a blank or unsupported channel mask
149 AudioChannelLayout channelMask = common.input.base.channelMask;
150 if (!isChannelMaskValid(channelMask)) {
151 LOG(ERROR) << "Downmix_Configure error: input channel mask " << channelMask.toString()
152 << " not supported";
153 } else {
154 mType = Downmix::Type::FOLD;
155 mChMask = channelMask;
156 mState = DOWNMIX_STATE_INITIALIZED;
157 }
158 }
159
160 } // namespace aidl::android::hardware::audio::effect
161