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
reset()103 void DownmixContext::reset() {
104 disable();
105 resetBuffer();
106 }
107
downmixProcess(float * in,float * out,int samples)108 IEffect::Status DownmixContext::downmixProcess(float* in, float* out, int samples) {
109 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
110
111 if (in == nullptr || out == nullptr ||
112 getCommon().input.frameCount != getCommon().output.frameCount || getInputFrameSize() == 0) {
113 return status;
114 }
115
116 status = {EX_ILLEGAL_STATE, 0, 0};
117 if (mState == DOWNMIX_STATE_UNINITIALIZED) {
118 LOG(ERROR) << __func__ << "Trying to use an uninitialized downmixer";
119 return status;
120 } else if (mState == DOWNMIX_STATE_INITIALIZED) {
121 LOG(ERROR) << __func__ << "Trying to use a non-configured downmixer";
122 return status;
123 }
124
125 bool accumulate = false;
126 int frames = samples * sizeof(float) / getInputFrameSize();
127 if (mType == Downmix::Type::STRIP) {
128 while (frames) {
129 if (accumulate) {
130 out[0] = std::clamp(out[0] + in[0], -1.f, 1.f);
131 out[1] = std::clamp(out[1] + in[1], -1.f, 1.f);
132 } else {
133 out[0] = in[0];
134 out[1] = in[1];
135 }
136 in += mInputChannelCount;
137 out += 2;
138 frames--;
139 }
140 } else {
141 int chMask = mChMask.get<AudioChannelLayout::layoutMask>();
142 if (!mChannelMix.process(in, out, frames, accumulate, (audio_channel_mask_t)chMask)) {
143 LOG(ERROR) << "Multichannel configuration " << mChMask.toString()
144 << " is not supported";
145 return status;
146 }
147 }
148 int producedSamples = (samples / mInputChannelCount) << 1;
149 return {STATUS_OK, samples, producedSamples};
150 }
151
init_params(const Parameter::Common & common)152 void DownmixContext::init_params(const Parameter::Common& common) {
153 // when configuring the effect, do not allow a blank or unsupported channel mask
154 AudioChannelLayout channelMask = common.input.base.channelMask;
155 if (!isChannelMaskValid(channelMask)) {
156 LOG(ERROR) << "Downmix_Configure error: input channel mask " << channelMask.toString()
157 << " not supported";
158 } else {
159 mType = Downmix::Type::FOLD;
160 mChMask = channelMask;
161 mState = DOWNMIX_STATE_INITIALIZED;
162 }
163 }
164
165 } // namespace aidl::android::hardware::audio::effect
166