blob: d8f578743f2e82b35ae73aaa6d8c7c10eda028f8 [file] [log] [blame]
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -08001/*
2 * Copyright (C) 2012 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 "EffectDownmix"
Jean-Michel Trivif28c8792012-04-18 18:38:42 -070018//#define LOG_NDEBUG 0
Mark Salyzyneb165612017-01-10 09:08:19 -080019#include <log/log.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070020
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080021#include "EffectDownmix.h"
Andy Hunge807e6b2021-05-19 12:01:58 -070022#include <audio_utils/ChannelMix.h>
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080023
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -070024// Do not submit with DOWNMIX_TEST_CHANNEL_INDEX defined, strictly for testing
25//#define DOWNMIX_TEST_CHANNEL_INDEX 0
26// Do not submit with DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER defined, strictly for testing
27//#define DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER 0
28
Andy Hung47068792021-05-18 16:28:32 -070029#define MINUS_3_DB_IN_FLOAT M_SQRT1_2 // -3dB = 0.70710678
30
Andy Hung47068792021-05-18 16:28:32 -070031typedef enum {
32 DOWNMIX_STATE_UNINITIALIZED,
33 DOWNMIX_STATE_INITIALIZED,
34 DOWNMIX_STATE_ACTIVE,
35} downmix_state_t;
36
37/* parameters for each downmixer */
Andy Hunge807e6b2021-05-19 12:01:58 -070038struct downmix_object_t {
Andy Hung47068792021-05-18 16:28:32 -070039 downmix_state_t state;
40 downmix_type_t type;
41 bool apply_volume_correction;
42 uint8_t input_channel_count;
Andy Hunge807e6b2021-05-19 12:01:58 -070043 android::audio_utils::channels::ChannelMix channelMix;
44};
Andy Hung47068792021-05-18 16:28:32 -070045
46typedef struct downmix_module_s {
47 const struct effect_interface_s *itfe;
48 effect_config_t config;
49 downmix_object_t context;
50} downmix_module_t;
Ramesh Katurid7d01342016-05-02 15:03:47 +053051
Jean-Michel Trividb46b482012-04-23 11:29:26 -070052
Andy Hung47068792021-05-18 16:28:32 -070053// Audio Effect API
54static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
55 int32_t sessionId,
56 int32_t ioId,
57 effect_handle_t *pHandle);
58static int32_t DownmixLib_Release(effect_handle_t handle);
59static int32_t DownmixLib_GetDescriptor(const effect_uuid_t *uuid,
60 effect_descriptor_t *pDescriptor);
61static int32_t Downmix_Process(effect_handle_t self,
62 audio_buffer_t *inBuffer,
63 audio_buffer_t *outBuffer);
64static int32_t Downmix_Command(effect_handle_t self,
65 uint32_t cmdCode,
66 uint32_t cmdSize,
67 void *pCmdData,
68 uint32_t *replySize,
69 void *pReplyData);
70static int32_t Downmix_GetDescriptor(effect_handle_t self,
71 effect_descriptor_t *pDescriptor);
72
73// Internal methods
74static int Downmix_Init(downmix_module_t *pDwmModule);
75static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init);
76static int Downmix_Reset(downmix_object_t *pDownmixer, bool init);
77static int Downmix_setParameter(
78 downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue);
79static int Downmix_getParameter(
80 downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue);
Andy Hung47068792021-05-18 16:28:32 -070081
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080082// effect_handle_t interface implementation for downmix effect
83const struct effect_interface_s gDownmixInterface = {
84 Downmix_Process,
85 Downmix_Command,
86 Downmix_GetDescriptor,
87 NULL /* no process_reverse function, no reference stream needed */
88};
89
Marco Nelissen7f16b192012-10-25 16:05:57 -070090// This is the only symbol that needs to be exported
91__attribute__ ((visibility ("default")))
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080092audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -070093 .tag = AUDIO_EFFECT_LIBRARY_TAG,
94 .version = EFFECT_LIBRARY_API_VERSION,
95 .name = "Downmix Library",
96 .implementor = "The Android Open Source Project",
97 .create_effect = DownmixLib_Create,
98 .release_effect = DownmixLib_Release,
99 .get_descriptor = DownmixLib_GetDescriptor,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800100};
101
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800102// AOSP insert downmix UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
103static const effect_descriptor_t gDownmixDescriptor = {
104 EFFECT_UIID_DOWNMIX__, //type
105 {0x93f04452, 0xe4fe, 0x41cc, 0x91f9, {0xe4, 0x75, 0xb6, 0xd1, 0xd6, 0x9f}}, // uuid
106 EFFECT_CONTROL_API_VERSION,
107 EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
108 0, //FIXME what value should be reported? // cpu load
109 0, //FIXME what value should be reported? // memory usage
110 "Multichannel Downmix To Stereo", // human readable effect name
111 "The Android Open Source Project" // human readable effect implementor name
112};
113
114// gDescriptors contains pointers to all defined effect descriptor in this library
115static const effect_descriptor_t * const gDescriptors[] = {
116 &gDownmixDescriptor
117};
118
119// number of effects in this library
120const int kNbEffects = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
Andy Hungca1c4e42021-05-06 22:32:02 -0700121
Andy Hungef25b6b2021-05-19 09:26:49 -0700122static inline float clamp_float(float value) {
123 return fmin(fmax(value, -1.f), 1.f);
Ramesh Katurid7d01342016-05-02 15:03:47 +0530124}
Andy Hungca1c4e42021-05-06 22:32:02 -0700125
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800126/*----------------------------------------------------------------------------
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700127 * Test code
128 *--------------------------------------------------------------------------*/
129#ifdef DOWNMIX_TEST_CHANNEL_INDEX
130// strictly for testing, logs the indices of the channels for a given mask,
131// uses the same code as Downmix_foldGeneric()
132void Downmix_testIndexComputation(uint32_t mask) {
Andy Hung47068792021-05-18 16:28:32 -0700133 ALOGI("Testing index computation for %#x:", mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700134 // check against unsupported channels
135 if (mask & kUnsupported) {
136 ALOGE("Unsupported channels (top or front left/right of center)");
137 return;
138 }
139 // verify has FL/FR
140 if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
141 ALOGE("Front channels must be present");
142 return;
143 }
144 // verify uses SIDE as a pair (ok if not using SIDE at all)
145 bool hasSides = false;
146 if ((mask & kSides) != 0) {
147 if ((mask & kSides) != kSides) {
148 ALOGE("Side channels must be used as a pair");
149 return;
150 }
151 hasSides = true;
152 }
153 // verify uses BACK as a pair (ok if not using BACK at all)
154 bool hasBacks = false;
155 if ((mask & kBacks) != 0) {
156 if ((mask & kBacks) != kBacks) {
157 ALOGE("Back channels must be used as a pair");
158 return;
159 }
160 hasBacks = true;
161 }
162
Andy Hunge5412692014-05-16 11:25:07 -0700163 const int numChan = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700164 const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
165 const bool hasLFE =
166 ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
167 const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
168 // compute at what index each channel is: samples will be in the following order:
169 // FL FR FC LFE BL BR BC SL SR
170 // when a channel is not present, its index is set to the same as the index of the preceding
171 // channel
172 const int indexFC = hasFC ? 2 : 1; // front center
173 const int indexLFE = hasLFE ? indexFC + 1 : indexFC; // low frequency
174 const int indexBL = hasBacks ? indexLFE + 1 : indexLFE; // back left
175 const int indexBR = hasBacks ? indexBL + 1 : indexBL; // back right
176 const int indexBC = hasBC ? indexBR + 1 : indexBR; // back center
177 const int indexSL = hasSides ? indexBC + 1 : indexBC; // side left
178 const int indexSR = hasSides ? indexSL + 1 : indexSL; // side right
179
180 ALOGI(" FL FR FC LFE BL BR BC SL SR");
181 ALOGI(" %d %d %d %d %d %d %d %d %d",
182 0, 1, indexFC, indexLFE, indexBL, indexBR, indexBC, indexSL, indexSR);
183}
184#endif
185
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700186static bool Downmix_validChannelMask(uint32_t mask)
187{
188 if (!mask) {
189 return false;
190 }
Andy Hung69344d32021-10-04 21:09:48 -0700191 // check against unsupported channels (up to FCC_26)
192 constexpr uint32_t MAXIMUM_CHANNEL_MASK = AUDIO_CHANNEL_OUT_22POINT2
193 | AUDIO_CHANNEL_OUT_FRONT_WIDE_LEFT | AUDIO_CHANNEL_OUT_FRONT_WIDE_RIGHT;
194 if (mask & ~MAXIMUM_CHANNEL_MASK) {
195 ALOGE("Unsupported channels in %#x", mask & ~MAXIMUM_CHANNEL_MASK);
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700196 return false;
197 }
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700198 return true;
199}
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700200
201/*----------------------------------------------------------------------------
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800202 * Effect API implementation
203 *--------------------------------------------------------------------------*/
204
205/*--- Effect Library Interface Implementation ---*/
206
Andy Hung47068792021-05-18 16:28:32 -0700207static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
Andy Hung679c4712021-05-19 14:14:54 -0700208 int32_t /* sessionId */,
209 int32_t /* ioId */,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800210 effect_handle_t *pHandle) {
211 int ret;
212 int i;
213 downmix_module_t *module;
214 const effect_descriptor_t *desc;
215
216 ALOGV("DownmixLib_Create()");
217
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700218#ifdef DOWNMIX_TEST_CHANNEL_INDEX
219 // should work (won't log an error)
220 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should work:");
221 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
222 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_CENTER);
Andy Hung862b1ab2021-05-19 11:37:08 -0700223 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_QUAD_SIDE | AUDIO_CHANNEL_OUT_QUAD_BACK);
224 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_5POINT1_SIDE | AUDIO_CHANNEL_OUT_BACK_CENTER);
225 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_5POINT1_BACK | AUDIO_CHANNEL_OUT_BACK_CENTER);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700226 // shouldn't work (will log an error, won't display channel indices)
227 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should NOT work:");
228 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
229 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_LEFT);
230 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
231 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_SIDE_LEFT);
232 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
233 AUDIO_CHANNEL_OUT_BACK_LEFT | AUDIO_CHANNEL_OUT_BACK_RIGHT);
234 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
235 AUDIO_CHANNEL_OUT_SIDE_LEFT | AUDIO_CHANNEL_OUT_SIDE_RIGHT);
236#endif
237
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800238 if (pHandle == NULL || uuid == NULL) {
239 return -EINVAL;
240 }
241
242 for (i = 0 ; i < kNbEffects ; i++) {
243 desc = gDescriptors[i];
244 if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t)) == 0) {
245 break;
246 }
247 }
248
249 if (i == kNbEffects) {
250 return -ENOENT;
251 }
252
Andy Hung19748032021-05-19 17:34:16 -0700253 module = new downmix_module_t{};
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800254
255 module->itfe = &gDownmixInterface;
256
257 module->context.state = DOWNMIX_STATE_UNINITIALIZED;
258
259 ret = Downmix_Init(module);
260 if (ret < 0) {
261 ALOGW("DownmixLib_Create() init failed");
262 free(module);
263 return ret;
264 }
265
266 *pHandle = (effect_handle_t) module;
267
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700268 ALOGV("DownmixLib_Create() %p , size %zu", module, sizeof(downmix_module_t));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800269
270 return 0;
271}
272
Andy Hung47068792021-05-18 16:28:32 -0700273static int32_t DownmixLib_Release(effect_handle_t handle) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800274 downmix_module_t *pDwmModule = (downmix_module_t *)handle;
275
276 ALOGV("DownmixLib_Release() %p", handle);
277 if (handle == NULL) {
278 return -EINVAL;
279 }
280
281 pDwmModule->context.state = DOWNMIX_STATE_UNINITIALIZED;
282
Andy Hung19748032021-05-19 17:34:16 -0700283 delete pDwmModule;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800284 return 0;
285}
286
Andy Hung47068792021-05-18 16:28:32 -0700287static int32_t DownmixLib_GetDescriptor(
288 const effect_uuid_t *uuid, effect_descriptor_t *pDescriptor) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800289 ALOGV("DownmixLib_GetDescriptor()");
290 int i;
291
292 if (pDescriptor == NULL || uuid == NULL){
293 ALOGE("DownmixLib_Create() called with NULL pointer");
294 return -EINVAL;
295 }
296 ALOGV("DownmixLib_GetDescriptor() nb effects=%d", kNbEffects);
297 for (i = 0; i < kNbEffects; i++) {
298 ALOGV("DownmixLib_GetDescriptor() i=%d", i);
299 if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
300 memcpy(pDescriptor, gDescriptors[i], sizeof(effect_descriptor_t));
Andy Hung47068792021-05-18 16:28:32 -0700301 ALOGV("EffectGetDescriptor - UUID matched downmix type %d, UUID = %#x",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800302 i, gDescriptors[i]->uuid.timeLow);
303 return 0;
304 }
305 }
306
307 return -EINVAL;
308}
309
Ramesh Katurid7d01342016-05-02 15:03:47 +0530310/*--- Effect Control Interface Implementation ---*/
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800311
Andy Hung47068792021-05-18 16:28:32 -0700312static int32_t Downmix_Process(effect_handle_t self,
Ramesh Katurid7d01342016-05-02 15:03:47 +0530313 audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
314
315 downmix_object_t *pDownmixer;
Andy Hunge807e6b2021-05-19 12:01:58 -0700316 const float *pSrc;
317 float *pDst;
Ramesh Katurid7d01342016-05-02 15:03:47 +0530318 downmix_module_t *pDwmModule = (downmix_module_t *)self;
319
320 if (pDwmModule == NULL) {
321 return -EINVAL;
322 }
323
324 if (inBuffer == NULL || inBuffer->raw == NULL ||
325 outBuffer == NULL || outBuffer->raw == NULL ||
326 inBuffer->frameCount != outBuffer->frameCount) {
327 return -EINVAL;
328 }
329
330 pDownmixer = (downmix_object_t*) &pDwmModule->context;
331
332 if (pDownmixer->state == DOWNMIX_STATE_UNINITIALIZED) {
333 ALOGE("Downmix_Process error: trying to use an uninitialized downmixer");
334 return -EINVAL;
335 } else if (pDownmixer->state == DOWNMIX_STATE_INITIALIZED) {
336 ALOGE("Downmix_Process error: trying to use a non-configured downmixer");
337 return -ENODATA;
338 }
339
Andy Hunga11b17f2021-05-19 11:44:13 -0700340 pSrc = inBuffer->f32;
341 pDst = outBuffer->f32;
Ramesh Katurid7d01342016-05-02 15:03:47 +0530342 size_t numFrames = outBuffer->frameCount;
343
344 const bool accumulate =
345 (pDwmModule->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
Andy Hunge807e6b2021-05-19 12:01:58 -0700346 const audio_channel_mask_t downmixInputChannelMask =
347 (audio_channel_mask_t)pDwmModule->config.inputCfg.channels;
Ramesh Katurid7d01342016-05-02 15:03:47 +0530348
349 switch(pDownmixer->type) {
350
351 case DOWNMIX_TYPE_STRIP:
352 if (accumulate) {
353 while (numFrames) {
354 pDst[0] = clamp_float(pDst[0] + pSrc[0]);
355 pDst[1] = clamp_float(pDst[1] + pSrc[1]);
356 pSrc += pDownmixer->input_channel_count;
357 pDst += 2;
358 numFrames--;
359 }
360 } else {
361 while (numFrames) {
362 pDst[0] = pSrc[0];
363 pDst[1] = pSrc[1];
364 pSrc += pDownmixer->input_channel_count;
365 pDst += 2;
366 numFrames--;
367 }
368 }
369 break;
370
Andy Hunge807e6b2021-05-19 12:01:58 -0700371 case DOWNMIX_TYPE_FOLD: {
372 if (!pDownmixer->channelMix.process(
373 pSrc, pDst, numFrames, accumulate, downmixInputChannelMask)) {
Andy Hung47068792021-05-18 16:28:32 -0700374 ALOGE("Multichannel configuration %#x is not supported",
Ramesh Katurid7d01342016-05-02 15:03:47 +0530375 downmixInputChannelMask);
376 return -EINVAL;
377 }
Ramesh Katurid7d01342016-05-02 15:03:47 +0530378 }
379 break;
380
381 default:
382 return -EINVAL;
383 }
384
385 return 0;
386}
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800387
Andy Hung47068792021-05-18 16:28:32 -0700388static int32_t Downmix_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800389 void *pCmdData, uint32_t *replySize, void *pReplyData) {
390
391 downmix_module_t *pDwmModule = (downmix_module_t *) self;
392 downmix_object_t *pDownmixer;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800393
394 if (pDwmModule == NULL || pDwmModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
395 return -EINVAL;
396 }
397
398 pDownmixer = (downmix_object_t*) &pDwmModule->context;
399
Andy Hung47068792021-05-18 16:28:32 -0700400 ALOGV("Downmix_Command command %u cmdSize %u", cmdCode, cmdSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800401
402 switch (cmdCode) {
403 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700404 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800405 return -EINVAL;
406 }
407 *(int *) pReplyData = Downmix_Init(pDwmModule);
408 break;
409
410 case EFFECT_CMD_SET_CONFIG:
411 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700412 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800413 return -EINVAL;
414 }
415 *(int *) pReplyData = Downmix_Configure(pDwmModule,
416 (effect_config_t *)pCmdData, false);
417 break;
418
419 case EFFECT_CMD_RESET:
420 Downmix_Reset(pDownmixer, false);
421 break;
422
Andy Hung19748032021-05-19 17:34:16 -0700423 case EFFECT_CMD_GET_PARAM: {
Andy Hung47068792021-05-18 16:28:32 -0700424 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %u, pReplyData: %p",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800425 pCmdData, *replySize, pReplyData);
426 if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700427 pReplyData == NULL || replySize == NULL ||
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800428 *replySize < (int) sizeof(effect_param_t) + 2 * sizeof(int32_t)) {
429 return -EINVAL;
430 }
431 effect_param_t *rep = (effect_param_t *) pReplyData;
432 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
Andy Hung47068792021-05-18 16:28:32 -0700433 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM param %d, replySize %u",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800434 *(int32_t *)rep->data, rep->vsize);
435 rep->status = Downmix_getParameter(pDownmixer, *(int32_t *)rep->data, &rep->vsize,
436 rep->data + sizeof(int32_t));
437 *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
438 break;
Andy Hung19748032021-05-19 17:34:16 -0700439 }
440 case EFFECT_CMD_SET_PARAM: {
Andy Hung47068792021-05-18 16:28:32 -0700441 ALOGV("Downmix_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %u"
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700442 ", pReplyData %p", cmdSize, pCmdData, *replySize, pReplyData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800443 if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
Eric Laurent0f714a42015-06-19 15:33:57 -0700444 || pReplyData == NULL || replySize == NULL || *replySize != (int)sizeof(int32_t)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800445 return -EINVAL;
446 }
447 effect_param_t *cmd = (effect_param_t *) pCmdData;
Mikhail Naganov804632a2017-07-24 17:25:47 -0700448 if (cmd->psize != sizeof(int32_t)) {
449 android_errorWriteLog(0x534e4554, "63662938");
450 return -EINVAL;
451 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800452 *(int *)pReplyData = Downmix_setParameter(pDownmixer, *(int32_t *)cmd->data,
453 cmd->vsize, cmd->data + sizeof(int32_t));
454 break;
Andy Hung19748032021-05-19 17:34:16 -0700455 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800456
457 case EFFECT_CMD_SET_PARAM_DEFERRED:
458 //FIXME implement
459 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_DEFERRED not supported, FIXME");
460 break;
461
462 case EFFECT_CMD_SET_PARAM_COMMIT:
463 //FIXME implement
464 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_COMMIT not supported, FIXME");
465 break;
466
467 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700468 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800469 return -EINVAL;
470 }
471 if (pDownmixer->state != DOWNMIX_STATE_INITIALIZED) {
472 return -ENOSYS;
473 }
474 pDownmixer->state = DOWNMIX_STATE_ACTIVE;
475 ALOGV("EFFECT_CMD_ENABLE() OK");
476 *(int *)pReplyData = 0;
477 break;
478
479 case EFFECT_CMD_DISABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700480 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800481 return -EINVAL;
482 }
483 if (pDownmixer->state != DOWNMIX_STATE_ACTIVE) {
484 return -ENOSYS;
485 }
486 pDownmixer->state = DOWNMIX_STATE_INITIALIZED;
487 ALOGV("EFFECT_CMD_DISABLE() OK");
488 *(int *)pReplyData = 0;
489 break;
490
491 case EFFECT_CMD_SET_DEVICE:
492 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
493 return -EINVAL;
494 }
495 // FIXME change type if playing on headset vs speaker
Andy Hung47068792021-05-18 16:28:32 -0700496 ALOGV("Downmix_Command EFFECT_CMD_SET_DEVICE: %#x", *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800497 break;
498
499 case EFFECT_CMD_SET_VOLUME: {
500 // audio output is always stereo => 2 channel volumes
501 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
502 return -EINVAL;
503 }
504 // FIXME change volume
505 ALOGW("Downmix_Command command EFFECT_CMD_SET_VOLUME not supported, FIXME");
506 float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
507 float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
508 ALOGV("Downmix_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
509 break;
510 }
511
512 case EFFECT_CMD_SET_AUDIO_MODE:
513 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
514 return -EINVAL;
515 }
Andy Hung47068792021-05-18 16:28:32 -0700516 ALOGV("Downmix_Command EFFECT_CMD_SET_AUDIO_MODE: %u", *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800517 break;
518
519 case EFFECT_CMD_SET_CONFIG_REVERSE:
520 case EFFECT_CMD_SET_INPUT_DEVICE:
521 // these commands are ignored by a downmix effect
522 break;
523
524 default:
Andy Hung47068792021-05-18 16:28:32 -0700525 ALOGW("Downmix_Command invalid command %u", cmdCode);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800526 return -EINVAL;
527 }
528
529 return 0;
530}
531
Andy Hung47068792021-05-18 16:28:32 -0700532static int32_t Downmix_GetDescriptor(effect_handle_t self, effect_descriptor_t *pDescriptor)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800533{
534 downmix_module_t *pDwnmxModule = (downmix_module_t *) self;
535
536 if (pDwnmxModule == NULL ||
537 pDwnmxModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
538 return -EINVAL;
539 }
540
541 memcpy(pDescriptor, &gDownmixDescriptor, sizeof(effect_descriptor_t));
542
543 return 0;
544}
545
546
547/*----------------------------------------------------------------------------
548 * Downmix internal functions
549 *--------------------------------------------------------------------------*/
550
551/*----------------------------------------------------------------------------
552 * Downmix_Init()
553 *----------------------------------------------------------------------------
554 * Purpose:
555 * Initialize downmix context and apply default parameters
556 *
557 * Inputs:
558 * pDwmModule pointer to downmix effect module
559 *
560 * Outputs:
561 *
562 * Returns:
563 * 0 indicates success
564 *
565 * Side Effects:
566 * updates:
567 * pDwmModule->context.type
568 * pDwmModule->context.apply_volume_correction
569 * pDwmModule->config.inputCfg
570 * pDwmModule->config.outputCfg
571 * pDwmModule->config.inputCfg.samplingRate
572 * pDwmModule->config.outputCfg.samplingRate
573 * pDwmModule->context.state
574 * doesn't set:
575 * pDwmModule->itfe
576 *
577 *----------------------------------------------------------------------------
578 */
579
Andy Hung47068792021-05-18 16:28:32 -0700580static int Downmix_Init(downmix_module_t *pDwmModule) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800581
582 ALOGV("Downmix_Init module %p", pDwmModule);
583 int ret = 0;
584
585 memset(&pDwmModule->context, 0, sizeof(downmix_object_t));
586
587 pDwmModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Andy Hung47068792021-05-18 16:28:32 -0700588 pDwmModule->config.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800589 pDwmModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_7POINT1;
590 pDwmModule->config.inputCfg.bufferProvider.getBuffer = NULL;
591 pDwmModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
592 pDwmModule->config.inputCfg.bufferProvider.cookie = NULL;
593 pDwmModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
594
595 pDwmModule->config.inputCfg.samplingRate = 44100;
596 pDwmModule->config.outputCfg.samplingRate = pDwmModule->config.inputCfg.samplingRate;
597
598 // set a default value for the access mode, but should be overwritten by caller
599 pDwmModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Andy Hung47068792021-05-18 16:28:32 -0700600 pDwmModule->config.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800601 pDwmModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
602 pDwmModule->config.outputCfg.bufferProvider.getBuffer = NULL;
603 pDwmModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
604 pDwmModule->config.outputCfg.bufferProvider.cookie = NULL;
605 pDwmModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
606
607 ret = Downmix_Configure(pDwmModule, &pDwmModule->config, true);
608 if (ret != 0) {
609 ALOGV("Downmix_Init error %d on module %p", ret, pDwmModule);
610 } else {
611 pDwmModule->context.state = DOWNMIX_STATE_INITIALIZED;
612 }
613
614 return ret;
615}
616
617
618/*----------------------------------------------------------------------------
619 * Downmix_Configure()
620 *----------------------------------------------------------------------------
621 * Purpose:
622 * Set input and output audio configuration.
623 *
624 * Inputs:
625 * pDwmModule pointer to downmix effect module
626 * pConfig pointer to effect_config_t structure containing input
627 * and output audio parameters configuration
628 * init true if called from init function
629 *
630 * Outputs:
631 *
632 * Returns:
633 * 0 indicates success
634 *
635 * Side Effects:
636 *
637 *----------------------------------------------------------------------------
638 */
639
Andy Hung47068792021-05-18 16:28:32 -0700640static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800641
642 downmix_object_t *pDownmixer = &pDwmModule->context;
643
644 // Check configuration compatibility with build options, and effect capabilities
645 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate
Andy Hung47068792021-05-18 16:28:32 -0700646 || pConfig->outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
647 || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_FLOAT
648 || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_FLOAT) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800649 ALOGE("Downmix_Configure error: invalid config");
650 return -EINVAL;
651 }
Andy Hung08d9ba32021-10-05 11:32:18 -0700652 // when configuring the effect, do not allow a blank or unsupported channel mask
653 if (!Downmix_validChannelMask(pConfig->inputCfg.channels)) {
654 ALOGE("Downmix_Configure error: input channel mask(0x%x) not supported",
655 pConfig->inputCfg.channels);
656 return -EINVAL;
657 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800658
Chih-Wei Huanga93c8c92013-01-10 20:52:57 +0800659 if (&pDwmModule->config != pConfig) {
660 memcpy(&pDwmModule->config, pConfig, sizeof(effect_config_t));
661 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800662
663 if (init) {
664 pDownmixer->type = DOWNMIX_TYPE_FOLD;
665 pDownmixer->apply_volume_correction = false;
666 pDownmixer->input_channel_count = 8; // matches default input of AUDIO_CHANNEL_OUT_7POINT1
667 } else {
Andy Hunge5412692014-05-16 11:25:07 -0700668 pDownmixer->input_channel_count =
669 audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800670 }
671
672 Downmix_Reset(pDownmixer, init);
673
674 return 0;
675}
676
677
678/*----------------------------------------------------------------------------
679 * Downmix_Reset()
680 *----------------------------------------------------------------------------
681 * Purpose:
682 * Reset internal states.
683 *
684 * Inputs:
685 * pDownmixer pointer to downmix context
686 * init true if called from init function
687 *
688 * Outputs:
689*
690 * Returns:
691 * 0 indicates success
692 *
693 * Side Effects:
694 *
695 *----------------------------------------------------------------------------
696 */
697
Andy Hung679c4712021-05-19 14:14:54 -0700698static int Downmix_Reset(downmix_object_t* /* pDownmixer */, bool /* init */) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800699 // nothing to do here
700 return 0;
701}
702
703
704/*----------------------------------------------------------------------------
705 * Downmix_setParameter()
706 *----------------------------------------------------------------------------
707 * Purpose:
708 * Set a Downmix parameter
709 *
710 * Inputs:
711 * pDownmixer handle to instance data
712 * param parameter
713 * pValue pointer to parameter value
714 * size value size
715 *
716 * Outputs:
717 *
718 * Returns:
719 * 0 indicates success
720 *
721 * Side Effects:
722 *
723 *----------------------------------------------------------------------------
724 */
Andy Hung47068792021-05-18 16:28:32 -0700725static int Downmix_setParameter(
726 downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800727
728 int16_t value16;
Andy Hung47068792021-05-18 16:28:32 -0700729 ALOGV("Downmix_setParameter, context %p, param %d, value16 %d, value32 %d",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800730 pDownmixer, param, *(int16_t *)pValue, *(int32_t *)pValue);
731
732 switch (param) {
733
734 case DOWNMIX_PARAM_TYPE:
735 if (size != sizeof(downmix_type_t)) {
Andy Hung47068792021-05-18 16:28:32 -0700736 ALOGE("Downmix_setParameter(DOWNMIX_PARAM_TYPE) invalid size %u, should be %zu",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800737 size, sizeof(downmix_type_t));
738 return -EINVAL;
739 }
740 value16 = *(int16_t *)pValue;
Andy Hung47068792021-05-18 16:28:32 -0700741 ALOGV("set DOWNMIX_PARAM_TYPE, type %d", value16);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700742 if (!((value16 > DOWNMIX_TYPE_INVALID) && (value16 <= DOWNMIX_TYPE_LAST))) {
Andy Hung47068792021-05-18 16:28:32 -0700743 ALOGE("Downmix_setParameter invalid DOWNMIX_PARAM_TYPE value %d", value16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800744 return -EINVAL;
745 } else {
746 pDownmixer->type = (downmix_type_t) value16;
747 break;
748
749 default:
Andy Hung47068792021-05-18 16:28:32 -0700750 ALOGE("Downmix_setParameter unknown parameter %d", param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800751 return -EINVAL;
752 }
753}
754
755 return 0;
756} /* end Downmix_setParameter */
757
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800758/*----------------------------------------------------------------------------
759 * Downmix_getParameter()
760 *----------------------------------------------------------------------------
761 * Purpose:
762 * Get a Downmix parameter
763 *
764 * Inputs:
765 * pDownmixer handle to instance data
766 * param parameter
767 * pValue pointer to variable to hold retrieved value
768 * pSize pointer to value size: maximum size as input
769 *
770 * Outputs:
771 * *pValue updated with parameter value
772 * *pSize updated with actual value size
773 *
774 * Returns:
775 * 0 indicates success
776 *
777 * Side Effects:
778 *
779 *----------------------------------------------------------------------------
780 */
Andy Hung47068792021-05-18 16:28:32 -0700781static int Downmix_getParameter(
782 downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800783 int16_t *pValue16;
784
785 switch (param) {
786
787 case DOWNMIX_PARAM_TYPE:
788 if (*pSize < sizeof(int16_t)) {
Andy Hung47068792021-05-18 16:28:32 -0700789 ALOGE("Downmix_getParameter invalid parameter size %u for DOWNMIX_PARAM_TYPE", *pSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800790 return -EINVAL;
791 }
792 pValue16 = (int16_t *)pValue;
793 *pValue16 = (int16_t) pDownmixer->type;
794 *pSize = sizeof(int16_t);
Andy Hung47068792021-05-18 16:28:32 -0700795 ALOGV("Downmix_getParameter DOWNMIX_PARAM_TYPE is %d", *pValue16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800796 break;
797
798 default:
Andy Hung47068792021-05-18 16:28:32 -0700799 ALOGE("Downmix_getParameter unknown parameter %d", param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800800 return -EINVAL;
801 }
802
803 return 0;
804} /* end Downmix_getParameter */
805