blob: 1e6da9210518235d25eaa341f75217324f40941a [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 Hung47068792021-05-18 16:28:32 -070022#include <math.h>
23#include <stdlib.h>
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080024
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -070025// Do not submit with DOWNMIX_TEST_CHANNEL_INDEX defined, strictly for testing
26//#define DOWNMIX_TEST_CHANNEL_INDEX 0
27// Do not submit with DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER defined, strictly for testing
28//#define DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER 0
29
Andy Hung47068792021-05-18 16:28:32 -070030#define MINUS_3_DB_IN_FLOAT M_SQRT1_2 // -3dB = 0.70710678
31
32#define LVM_FLOAT float
33
34const uint32_t kSides = AUDIO_CHANNEL_OUT_SIDE_LEFT | AUDIO_CHANNEL_OUT_SIDE_RIGHT;
35const uint32_t kBacks = AUDIO_CHANNEL_OUT_BACK_LEFT | AUDIO_CHANNEL_OUT_BACK_RIGHT;
36const uint32_t kUnsupported =
37 AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER | AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
38 AUDIO_CHANNEL_OUT_TOP_CENTER |
39 AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT |
40 AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER |
41 AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT |
42 AUDIO_CHANNEL_OUT_TOP_BACK_LEFT |
43 AUDIO_CHANNEL_OUT_TOP_BACK_CENTER |
44 AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT;
45
46typedef enum {
47 DOWNMIX_STATE_UNINITIALIZED,
48 DOWNMIX_STATE_INITIALIZED,
49 DOWNMIX_STATE_ACTIVE,
50} downmix_state_t;
51
52/* parameters for each downmixer */
53typedef struct {
54 downmix_state_t state;
55 downmix_type_t type;
56 bool apply_volume_correction;
57 uint8_t input_channel_count;
58} downmix_object_t;
59
60typedef struct downmix_module_s {
61 const struct effect_interface_s *itfe;
62 effect_config_t config;
63 downmix_object_t context;
64} downmix_module_t;
Ramesh Katurid7d01342016-05-02 15:03:47 +053065
Glenn Kasten029a64e2014-04-15 09:58:55 -070066// subset of possible audio_channel_mask_t values, and AUDIO_CHANNEL_OUT_* renamed to CHANNEL_MASK_*
Jean-Michel Trividb46b482012-04-23 11:29:26 -070067typedef enum {
Glenn Kasten029a64e2014-04-15 09:58:55 -070068 CHANNEL_MASK_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD_BACK,
69 CHANNEL_MASK_QUAD_SIDE = AUDIO_CHANNEL_OUT_QUAD_SIDE,
70 CHANNEL_MASK_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1_BACK,
71 CHANNEL_MASK_5POINT1_SIDE = AUDIO_CHANNEL_OUT_5POINT1_SIDE,
72 CHANNEL_MASK_7POINT1 = AUDIO_CHANNEL_OUT_7POINT1,
Jean-Michel Trividb46b482012-04-23 11:29:26 -070073} downmix_input_channel_mask_t;
74
Andy Hung47068792021-05-18 16:28:32 -070075// Audio Effect API
76static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
77 int32_t sessionId,
78 int32_t ioId,
79 effect_handle_t *pHandle);
80static int32_t DownmixLib_Release(effect_handle_t handle);
81static int32_t DownmixLib_GetDescriptor(const effect_uuid_t *uuid,
82 effect_descriptor_t *pDescriptor);
83static int32_t Downmix_Process(effect_handle_t self,
84 audio_buffer_t *inBuffer,
85 audio_buffer_t *outBuffer);
86static int32_t Downmix_Command(effect_handle_t self,
87 uint32_t cmdCode,
88 uint32_t cmdSize,
89 void *pCmdData,
90 uint32_t *replySize,
91 void *pReplyData);
92static int32_t Downmix_GetDescriptor(effect_handle_t self,
93 effect_descriptor_t *pDescriptor);
94
95// Internal methods
96static int Downmix_Init(downmix_module_t *pDwmModule);
97static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init);
98static int Downmix_Reset(downmix_object_t *pDownmixer, bool init);
99static int Downmix_setParameter(
100 downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue);
101static int Downmix_getParameter(
102 downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue);
103static void Downmix_foldFromQuad(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
104static void Downmix_foldFrom5Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
105static void Downmix_foldFrom7Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
106static bool Downmix_foldGeneric(
107 uint32_t mask, float *pSrc, float *pDst, size_t numFrames, bool accumulate);
108
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800109// effect_handle_t interface implementation for downmix effect
110const struct effect_interface_s gDownmixInterface = {
111 Downmix_Process,
112 Downmix_Command,
113 Downmix_GetDescriptor,
114 NULL /* no process_reverse function, no reference stream needed */
115};
116
Marco Nelissen7f16b192012-10-25 16:05:57 -0700117// This is the only symbol that needs to be exported
118__attribute__ ((visibility ("default")))
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800119audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700120 .tag = AUDIO_EFFECT_LIBRARY_TAG,
121 .version = EFFECT_LIBRARY_API_VERSION,
122 .name = "Downmix Library",
123 .implementor = "The Android Open Source Project",
124 .create_effect = DownmixLib_Create,
125 .release_effect = DownmixLib_Release,
126 .get_descriptor = DownmixLib_GetDescriptor,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800127};
128
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800129// AOSP insert downmix UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
130static const effect_descriptor_t gDownmixDescriptor = {
131 EFFECT_UIID_DOWNMIX__, //type
132 {0x93f04452, 0xe4fe, 0x41cc, 0x91f9, {0xe4, 0x75, 0xb6, 0xd1, 0xd6, 0x9f}}, // uuid
133 EFFECT_CONTROL_API_VERSION,
134 EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
135 0, //FIXME what value should be reported? // cpu load
136 0, //FIXME what value should be reported? // memory usage
137 "Multichannel Downmix To Stereo", // human readable effect name
138 "The Android Open Source Project" // human readable effect implementor name
139};
140
141// gDescriptors contains pointers to all defined effect descriptor in this library
142static const effect_descriptor_t * const gDescriptors[] = {
143 &gDownmixDescriptor
144};
145
146// number of effects in this library
147const int kNbEffects = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
Andy Hungca1c4e42021-05-06 22:32:02 -0700148
Ramesh Katurid7d01342016-05-02 15:03:47 +0530149static LVM_FLOAT clamp_float(LVM_FLOAT a) {
150 if (a > 1.0f) {
151 return 1.0f;
152 }
153 else if (a < -1.0f) {
154 return -1.0f;
155 }
156 else {
157 return a;
158 }
159}
Andy Hungca1c4e42021-05-06 22:32:02 -0700160
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800161/*----------------------------------------------------------------------------
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700162 * Test code
163 *--------------------------------------------------------------------------*/
164#ifdef DOWNMIX_TEST_CHANNEL_INDEX
165// strictly for testing, logs the indices of the channels for a given mask,
166// uses the same code as Downmix_foldGeneric()
167void Downmix_testIndexComputation(uint32_t mask) {
Andy Hung47068792021-05-18 16:28:32 -0700168 ALOGI("Testing index computation for %#x:", mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700169 // check against unsupported channels
170 if (mask & kUnsupported) {
171 ALOGE("Unsupported channels (top or front left/right of center)");
172 return;
173 }
174 // verify has FL/FR
175 if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
176 ALOGE("Front channels must be present");
177 return;
178 }
179 // verify uses SIDE as a pair (ok if not using SIDE at all)
180 bool hasSides = false;
181 if ((mask & kSides) != 0) {
182 if ((mask & kSides) != kSides) {
183 ALOGE("Side channels must be used as a pair");
184 return;
185 }
186 hasSides = true;
187 }
188 // verify uses BACK as a pair (ok if not using BACK at all)
189 bool hasBacks = false;
190 if ((mask & kBacks) != 0) {
191 if ((mask & kBacks) != kBacks) {
192 ALOGE("Back channels must be used as a pair");
193 return;
194 }
195 hasBacks = true;
196 }
197
Andy Hunge5412692014-05-16 11:25:07 -0700198 const int numChan = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700199 const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
200 const bool hasLFE =
201 ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
202 const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
203 // compute at what index each channel is: samples will be in the following order:
204 // FL FR FC LFE BL BR BC SL SR
205 // when a channel is not present, its index is set to the same as the index of the preceding
206 // channel
207 const int indexFC = hasFC ? 2 : 1; // front center
208 const int indexLFE = hasLFE ? indexFC + 1 : indexFC; // low frequency
209 const int indexBL = hasBacks ? indexLFE + 1 : indexLFE; // back left
210 const int indexBR = hasBacks ? indexBL + 1 : indexBL; // back right
211 const int indexBC = hasBC ? indexBR + 1 : indexBR; // back center
212 const int indexSL = hasSides ? indexBC + 1 : indexBC; // side left
213 const int indexSR = hasSides ? indexSL + 1 : indexSL; // side right
214
215 ALOGI(" FL FR FC LFE BL BR BC SL SR");
216 ALOGI(" %d %d %d %d %d %d %d %d %d",
217 0, 1, indexFC, indexLFE, indexBL, indexBR, indexBC, indexSL, indexSR);
218}
219#endif
220
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700221static bool Downmix_validChannelMask(uint32_t mask)
222{
223 if (!mask) {
224 return false;
225 }
226 // check against unsupported channels
227 if (mask & kUnsupported) {
228 ALOGE("Unsupported channels (top or front left/right of center)");
229 return false;
230 }
231 // verify has FL/FR
232 if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
233 ALOGE("Front channels must be present");
234 return false;
235 }
236 // verify uses SIDE as a pair (ok if not using SIDE at all)
237 if ((mask & kSides) != 0) {
238 if ((mask & kSides) != kSides) {
239 ALOGE("Side channels must be used as a pair");
240 return false;
241 }
242 }
243 // verify uses BACK as a pair (ok if not using BACK at all)
244 if ((mask & kBacks) != 0) {
245 if ((mask & kBacks) != kBacks) {
246 ALOGE("Back channels must be used as a pair");
247 return false;
248 }
249 }
250 return true;
251}
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700252
253/*----------------------------------------------------------------------------
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800254 * Effect API implementation
255 *--------------------------------------------------------------------------*/
256
257/*--- Effect Library Interface Implementation ---*/
258
Andy Hung47068792021-05-18 16:28:32 -0700259static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
Eric Laurent0f714a42015-06-19 15:33:57 -0700260 int32_t sessionId __unused,
261 int32_t ioId __unused,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800262 effect_handle_t *pHandle) {
263 int ret;
264 int i;
265 downmix_module_t *module;
266 const effect_descriptor_t *desc;
267
268 ALOGV("DownmixLib_Create()");
269
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700270#ifdef DOWNMIX_TEST_CHANNEL_INDEX
271 // should work (won't log an error)
272 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should work:");
273 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
274 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_CENTER);
275 Downmix_testIndexComputation(CHANNEL_MASK_QUAD_SIDE | CHANNEL_MASK_QUAD_BACK);
276 Downmix_testIndexComputation(CHANNEL_MASK_5POINT1_SIDE | AUDIO_CHANNEL_OUT_BACK_CENTER);
277 Downmix_testIndexComputation(CHANNEL_MASK_5POINT1_BACK | AUDIO_CHANNEL_OUT_BACK_CENTER);
278 // shouldn't work (will log an error, won't display channel indices)
279 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should NOT work:");
280 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
281 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_LEFT);
282 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
283 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_SIDE_LEFT);
284 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
285 AUDIO_CHANNEL_OUT_BACK_LEFT | AUDIO_CHANNEL_OUT_BACK_RIGHT);
286 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
287 AUDIO_CHANNEL_OUT_SIDE_LEFT | AUDIO_CHANNEL_OUT_SIDE_RIGHT);
288#endif
289
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800290 if (pHandle == NULL || uuid == NULL) {
291 return -EINVAL;
292 }
293
294 for (i = 0 ; i < kNbEffects ; i++) {
295 desc = gDescriptors[i];
296 if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t)) == 0) {
297 break;
298 }
299 }
300
301 if (i == kNbEffects) {
302 return -ENOENT;
303 }
304
305 module = malloc(sizeof(downmix_module_t));
306
307 module->itfe = &gDownmixInterface;
308
309 module->context.state = DOWNMIX_STATE_UNINITIALIZED;
310
311 ret = Downmix_Init(module);
312 if (ret < 0) {
313 ALOGW("DownmixLib_Create() init failed");
314 free(module);
315 return ret;
316 }
317
318 *pHandle = (effect_handle_t) module;
319
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700320 ALOGV("DownmixLib_Create() %p , size %zu", module, sizeof(downmix_module_t));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800321
322 return 0;
323}
324
Andy Hung47068792021-05-18 16:28:32 -0700325static int32_t DownmixLib_Release(effect_handle_t handle) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800326 downmix_module_t *pDwmModule = (downmix_module_t *)handle;
327
328 ALOGV("DownmixLib_Release() %p", handle);
329 if (handle == NULL) {
330 return -EINVAL;
331 }
332
333 pDwmModule->context.state = DOWNMIX_STATE_UNINITIALIZED;
334
335 free(pDwmModule);
336 return 0;
337}
338
Andy Hung47068792021-05-18 16:28:32 -0700339static int32_t DownmixLib_GetDescriptor(
340 const effect_uuid_t *uuid, effect_descriptor_t *pDescriptor) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800341 ALOGV("DownmixLib_GetDescriptor()");
342 int i;
343
344 if (pDescriptor == NULL || uuid == NULL){
345 ALOGE("DownmixLib_Create() called with NULL pointer");
346 return -EINVAL;
347 }
348 ALOGV("DownmixLib_GetDescriptor() nb effects=%d", kNbEffects);
349 for (i = 0; i < kNbEffects; i++) {
350 ALOGV("DownmixLib_GetDescriptor() i=%d", i);
351 if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
352 memcpy(pDescriptor, gDescriptors[i], sizeof(effect_descriptor_t));
Andy Hung47068792021-05-18 16:28:32 -0700353 ALOGV("EffectGetDescriptor - UUID matched downmix type %d, UUID = %#x",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800354 i, gDescriptors[i]->uuid.timeLow);
355 return 0;
356 }
357 }
358
359 return -EINVAL;
360}
361
Ramesh Katurid7d01342016-05-02 15:03:47 +0530362/*--- Effect Control Interface Implementation ---*/
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800363
Andy Hung47068792021-05-18 16:28:32 -0700364static int32_t Downmix_Process(effect_handle_t self,
Ramesh Katurid7d01342016-05-02 15:03:47 +0530365 audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
366
367 downmix_object_t *pDownmixer;
368 LVM_FLOAT *pSrc, *pDst;
369 downmix_module_t *pDwmModule = (downmix_module_t *)self;
370
371 if (pDwmModule == NULL) {
372 return -EINVAL;
373 }
374
375 if (inBuffer == NULL || inBuffer->raw == NULL ||
376 outBuffer == NULL || outBuffer->raw == NULL ||
377 inBuffer->frameCount != outBuffer->frameCount) {
378 return -EINVAL;
379 }
380
381 pDownmixer = (downmix_object_t*) &pDwmModule->context;
382
383 if (pDownmixer->state == DOWNMIX_STATE_UNINITIALIZED) {
384 ALOGE("Downmix_Process error: trying to use an uninitialized downmixer");
385 return -EINVAL;
386 } else if (pDownmixer->state == DOWNMIX_STATE_INITIALIZED) {
387 ALOGE("Downmix_Process error: trying to use a non-configured downmixer");
388 return -ENODATA;
389 }
390
391 pSrc = (LVM_FLOAT *) inBuffer->s16;
392 pDst = (LVM_FLOAT *) outBuffer->s16;
393 size_t numFrames = outBuffer->frameCount;
394
395 const bool accumulate =
396 (pDwmModule->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
397 const uint32_t downmixInputChannelMask = pDwmModule->config.inputCfg.channels;
398
399 switch(pDownmixer->type) {
400
401 case DOWNMIX_TYPE_STRIP:
402 if (accumulate) {
403 while (numFrames) {
404 pDst[0] = clamp_float(pDst[0] + pSrc[0]);
405 pDst[1] = clamp_float(pDst[1] + pSrc[1]);
406 pSrc += pDownmixer->input_channel_count;
407 pDst += 2;
408 numFrames--;
409 }
410 } else {
411 while (numFrames) {
412 pDst[0] = pSrc[0];
413 pDst[1] = pSrc[1];
414 pSrc += pDownmixer->input_channel_count;
415 pDst += 2;
416 numFrames--;
417 }
418 }
419 break;
420
421 case DOWNMIX_TYPE_FOLD:
422#ifdef DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER
423 // bypass the optimized downmix routines for the common formats
424 if (!Downmix_foldGeneric(
425 downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
Andy Hung47068792021-05-18 16:28:32 -0700426 ALOGE("Multichannel configuration %#x is not supported",
Ramesh Katurid7d01342016-05-02 15:03:47 +0530427 downmixInputChannelMask);
428 return -EINVAL;
429 }
430 break;
431#endif
432 // optimize for the common formats
433 switch((downmix_input_channel_mask_t)downmixInputChannelMask) {
434 case CHANNEL_MASK_QUAD_BACK:
435 case CHANNEL_MASK_QUAD_SIDE:
436 Downmix_foldFromQuad(pSrc, pDst, numFrames, accumulate);
437 break;
438 case CHANNEL_MASK_5POINT1_BACK:
439 case CHANNEL_MASK_5POINT1_SIDE:
440 Downmix_foldFrom5Point1(pSrc, pDst, numFrames, accumulate);
441 break;
442 case CHANNEL_MASK_7POINT1:
443 Downmix_foldFrom7Point1(pSrc, pDst, numFrames, accumulate);
444 break;
445 default:
446 if (!Downmix_foldGeneric(
447 downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
Andy Hung47068792021-05-18 16:28:32 -0700448 ALOGE("Multichannel configuration %#x is not supported",
Ramesh Katurid7d01342016-05-02 15:03:47 +0530449 downmixInputChannelMask);
450 return -EINVAL;
451 }
452 break;
453 }
454 break;
455
456 default:
457 return -EINVAL;
458 }
459
460 return 0;
461}
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800462
Andy Hung47068792021-05-18 16:28:32 -0700463static int32_t Downmix_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800464 void *pCmdData, uint32_t *replySize, void *pReplyData) {
465
466 downmix_module_t *pDwmModule = (downmix_module_t *) self;
467 downmix_object_t *pDownmixer;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800468
469 if (pDwmModule == NULL || pDwmModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
470 return -EINVAL;
471 }
472
473 pDownmixer = (downmix_object_t*) &pDwmModule->context;
474
Andy Hung47068792021-05-18 16:28:32 -0700475 ALOGV("Downmix_Command command %u cmdSize %u", cmdCode, cmdSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800476
477 switch (cmdCode) {
478 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700479 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800480 return -EINVAL;
481 }
482 *(int *) pReplyData = Downmix_Init(pDwmModule);
483 break;
484
485 case EFFECT_CMD_SET_CONFIG:
486 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700487 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800488 return -EINVAL;
489 }
490 *(int *) pReplyData = Downmix_Configure(pDwmModule,
491 (effect_config_t *)pCmdData, false);
492 break;
493
494 case EFFECT_CMD_RESET:
495 Downmix_Reset(pDownmixer, false);
496 break;
497
498 case EFFECT_CMD_GET_PARAM:
Andy Hung47068792021-05-18 16:28:32 -0700499 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %u, pReplyData: %p",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800500 pCmdData, *replySize, pReplyData);
501 if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700502 pReplyData == NULL || replySize == NULL ||
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800503 *replySize < (int) sizeof(effect_param_t) + 2 * sizeof(int32_t)) {
504 return -EINVAL;
505 }
506 effect_param_t *rep = (effect_param_t *) pReplyData;
507 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
Andy Hung47068792021-05-18 16:28:32 -0700508 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM param %d, replySize %u",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800509 *(int32_t *)rep->data, rep->vsize);
510 rep->status = Downmix_getParameter(pDownmixer, *(int32_t *)rep->data, &rep->vsize,
511 rep->data + sizeof(int32_t));
512 *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
513 break;
514
515 case EFFECT_CMD_SET_PARAM:
Andy Hung47068792021-05-18 16:28:32 -0700516 ALOGV("Downmix_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %u"
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700517 ", pReplyData %p", cmdSize, pCmdData, *replySize, pReplyData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800518 if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
Eric Laurent0f714a42015-06-19 15:33:57 -0700519 || pReplyData == NULL || replySize == NULL || *replySize != (int)sizeof(int32_t)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800520 return -EINVAL;
521 }
522 effect_param_t *cmd = (effect_param_t *) pCmdData;
Mikhail Naganov804632a2017-07-24 17:25:47 -0700523 if (cmd->psize != sizeof(int32_t)) {
524 android_errorWriteLog(0x534e4554, "63662938");
525 return -EINVAL;
526 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800527 *(int *)pReplyData = Downmix_setParameter(pDownmixer, *(int32_t *)cmd->data,
528 cmd->vsize, cmd->data + sizeof(int32_t));
529 break;
530
531 case EFFECT_CMD_SET_PARAM_DEFERRED:
532 //FIXME implement
533 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_DEFERRED not supported, FIXME");
534 break;
535
536 case EFFECT_CMD_SET_PARAM_COMMIT:
537 //FIXME implement
538 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_COMMIT not supported, FIXME");
539 break;
540
541 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700542 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800543 return -EINVAL;
544 }
545 if (pDownmixer->state != DOWNMIX_STATE_INITIALIZED) {
546 return -ENOSYS;
547 }
548 pDownmixer->state = DOWNMIX_STATE_ACTIVE;
549 ALOGV("EFFECT_CMD_ENABLE() OK");
550 *(int *)pReplyData = 0;
551 break;
552
553 case EFFECT_CMD_DISABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700554 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800555 return -EINVAL;
556 }
557 if (pDownmixer->state != DOWNMIX_STATE_ACTIVE) {
558 return -ENOSYS;
559 }
560 pDownmixer->state = DOWNMIX_STATE_INITIALIZED;
561 ALOGV("EFFECT_CMD_DISABLE() OK");
562 *(int *)pReplyData = 0;
563 break;
564
565 case EFFECT_CMD_SET_DEVICE:
566 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
567 return -EINVAL;
568 }
569 // FIXME change type if playing on headset vs speaker
Andy Hung47068792021-05-18 16:28:32 -0700570 ALOGV("Downmix_Command EFFECT_CMD_SET_DEVICE: %#x", *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800571 break;
572
573 case EFFECT_CMD_SET_VOLUME: {
574 // audio output is always stereo => 2 channel volumes
575 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
576 return -EINVAL;
577 }
578 // FIXME change volume
579 ALOGW("Downmix_Command command EFFECT_CMD_SET_VOLUME not supported, FIXME");
580 float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
581 float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
582 ALOGV("Downmix_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
583 break;
584 }
585
586 case EFFECT_CMD_SET_AUDIO_MODE:
587 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
588 return -EINVAL;
589 }
Andy Hung47068792021-05-18 16:28:32 -0700590 ALOGV("Downmix_Command EFFECT_CMD_SET_AUDIO_MODE: %u", *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800591 break;
592
593 case EFFECT_CMD_SET_CONFIG_REVERSE:
594 case EFFECT_CMD_SET_INPUT_DEVICE:
595 // these commands are ignored by a downmix effect
596 break;
597
598 default:
Andy Hung47068792021-05-18 16:28:32 -0700599 ALOGW("Downmix_Command invalid command %u", cmdCode);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800600 return -EINVAL;
601 }
602
603 return 0;
604}
605
Andy Hung47068792021-05-18 16:28:32 -0700606static int32_t Downmix_GetDescriptor(effect_handle_t self, effect_descriptor_t *pDescriptor)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800607{
608 downmix_module_t *pDwnmxModule = (downmix_module_t *) self;
609
610 if (pDwnmxModule == NULL ||
611 pDwnmxModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
612 return -EINVAL;
613 }
614
615 memcpy(pDescriptor, &gDownmixDescriptor, sizeof(effect_descriptor_t));
616
617 return 0;
618}
619
620
621/*----------------------------------------------------------------------------
622 * Downmix internal functions
623 *--------------------------------------------------------------------------*/
624
625/*----------------------------------------------------------------------------
626 * Downmix_Init()
627 *----------------------------------------------------------------------------
628 * Purpose:
629 * Initialize downmix context and apply default parameters
630 *
631 * Inputs:
632 * pDwmModule pointer to downmix effect module
633 *
634 * Outputs:
635 *
636 * Returns:
637 * 0 indicates success
638 *
639 * Side Effects:
640 * updates:
641 * pDwmModule->context.type
642 * pDwmModule->context.apply_volume_correction
643 * pDwmModule->config.inputCfg
644 * pDwmModule->config.outputCfg
645 * pDwmModule->config.inputCfg.samplingRate
646 * pDwmModule->config.outputCfg.samplingRate
647 * pDwmModule->context.state
648 * doesn't set:
649 * pDwmModule->itfe
650 *
651 *----------------------------------------------------------------------------
652 */
653
Andy Hung47068792021-05-18 16:28:32 -0700654static int Downmix_Init(downmix_module_t *pDwmModule) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800655
656 ALOGV("Downmix_Init module %p", pDwmModule);
657 int ret = 0;
658
659 memset(&pDwmModule->context, 0, sizeof(downmix_object_t));
660
661 pDwmModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Andy Hung47068792021-05-18 16:28:32 -0700662 pDwmModule->config.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800663 pDwmModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_7POINT1;
664 pDwmModule->config.inputCfg.bufferProvider.getBuffer = NULL;
665 pDwmModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
666 pDwmModule->config.inputCfg.bufferProvider.cookie = NULL;
667 pDwmModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
668
669 pDwmModule->config.inputCfg.samplingRate = 44100;
670 pDwmModule->config.outputCfg.samplingRate = pDwmModule->config.inputCfg.samplingRate;
671
672 // set a default value for the access mode, but should be overwritten by caller
673 pDwmModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Andy Hung47068792021-05-18 16:28:32 -0700674 pDwmModule->config.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800675 pDwmModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
676 pDwmModule->config.outputCfg.bufferProvider.getBuffer = NULL;
677 pDwmModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
678 pDwmModule->config.outputCfg.bufferProvider.cookie = NULL;
679 pDwmModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
680
681 ret = Downmix_Configure(pDwmModule, &pDwmModule->config, true);
682 if (ret != 0) {
683 ALOGV("Downmix_Init error %d on module %p", ret, pDwmModule);
684 } else {
685 pDwmModule->context.state = DOWNMIX_STATE_INITIALIZED;
686 }
687
688 return ret;
689}
690
691
692/*----------------------------------------------------------------------------
693 * Downmix_Configure()
694 *----------------------------------------------------------------------------
695 * Purpose:
696 * Set input and output audio configuration.
697 *
698 * Inputs:
699 * pDwmModule pointer to downmix effect module
700 * pConfig pointer to effect_config_t structure containing input
701 * and output audio parameters configuration
702 * init true if called from init function
703 *
704 * Outputs:
705 *
706 * Returns:
707 * 0 indicates success
708 *
709 * Side Effects:
710 *
711 *----------------------------------------------------------------------------
712 */
713
Andy Hung47068792021-05-18 16:28:32 -0700714static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800715
716 downmix_object_t *pDownmixer = &pDwmModule->context;
717
718 // Check configuration compatibility with build options, and effect capabilities
719 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate
Andy Hung47068792021-05-18 16:28:32 -0700720 || pConfig->outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
721 || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_FLOAT
722 || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_FLOAT) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800723 ALOGE("Downmix_Configure error: invalid config");
724 return -EINVAL;
725 }
726
Chih-Wei Huanga93c8c92013-01-10 20:52:57 +0800727 if (&pDwmModule->config != pConfig) {
728 memcpy(&pDwmModule->config, pConfig, sizeof(effect_config_t));
729 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800730
731 if (init) {
732 pDownmixer->type = DOWNMIX_TYPE_FOLD;
733 pDownmixer->apply_volume_correction = false;
734 pDownmixer->input_channel_count = 8; // matches default input of AUDIO_CHANNEL_OUT_7POINT1
735 } else {
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700736 // when configuring the effect, do not allow a blank or unsupported channel mask
737 if (!Downmix_validChannelMask(pConfig->inputCfg.channels)) {
738 ALOGE("Downmix_Configure error: input channel mask(0x%x) not supported",
739 pConfig->inputCfg.channels);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800740 return -EINVAL;
741 }
Andy Hunge5412692014-05-16 11:25:07 -0700742 pDownmixer->input_channel_count =
743 audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800744 }
745
746 Downmix_Reset(pDownmixer, init);
747
748 return 0;
749}
750
751
752/*----------------------------------------------------------------------------
753 * Downmix_Reset()
754 *----------------------------------------------------------------------------
755 * Purpose:
756 * Reset internal states.
757 *
758 * Inputs:
759 * pDownmixer pointer to downmix context
760 * init true if called from init function
761 *
762 * Outputs:
763*
764 * Returns:
765 * 0 indicates success
766 *
767 * Side Effects:
768 *
769 *----------------------------------------------------------------------------
770 */
771
Andy Hung47068792021-05-18 16:28:32 -0700772static int Downmix_Reset(downmix_object_t *pDownmixer __unused, bool init __unused) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800773 // nothing to do here
774 return 0;
775}
776
777
778/*----------------------------------------------------------------------------
779 * Downmix_setParameter()
780 *----------------------------------------------------------------------------
781 * Purpose:
782 * Set a Downmix parameter
783 *
784 * Inputs:
785 * pDownmixer handle to instance data
786 * param parameter
787 * pValue pointer to parameter value
788 * size value size
789 *
790 * Outputs:
791 *
792 * Returns:
793 * 0 indicates success
794 *
795 * Side Effects:
796 *
797 *----------------------------------------------------------------------------
798 */
Andy Hung47068792021-05-18 16:28:32 -0700799static int Downmix_setParameter(
800 downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800801
802 int16_t value16;
Andy Hung47068792021-05-18 16:28:32 -0700803 ALOGV("Downmix_setParameter, context %p, param %d, value16 %d, value32 %d",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800804 pDownmixer, param, *(int16_t *)pValue, *(int32_t *)pValue);
805
806 switch (param) {
807
808 case DOWNMIX_PARAM_TYPE:
809 if (size != sizeof(downmix_type_t)) {
Andy Hung47068792021-05-18 16:28:32 -0700810 ALOGE("Downmix_setParameter(DOWNMIX_PARAM_TYPE) invalid size %u, should be %zu",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800811 size, sizeof(downmix_type_t));
812 return -EINVAL;
813 }
814 value16 = *(int16_t *)pValue;
Andy Hung47068792021-05-18 16:28:32 -0700815 ALOGV("set DOWNMIX_PARAM_TYPE, type %d", value16);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700816 if (!((value16 > DOWNMIX_TYPE_INVALID) && (value16 <= DOWNMIX_TYPE_LAST))) {
Andy Hung47068792021-05-18 16:28:32 -0700817 ALOGE("Downmix_setParameter invalid DOWNMIX_PARAM_TYPE value %d", value16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800818 return -EINVAL;
819 } else {
820 pDownmixer->type = (downmix_type_t) value16;
821 break;
822
823 default:
Andy Hung47068792021-05-18 16:28:32 -0700824 ALOGE("Downmix_setParameter unknown parameter %d", param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800825 return -EINVAL;
826 }
827}
828
829 return 0;
830} /* end Downmix_setParameter */
831
832
833/*----------------------------------------------------------------------------
834 * Downmix_getParameter()
835 *----------------------------------------------------------------------------
836 * Purpose:
837 * Get a Downmix parameter
838 *
839 * Inputs:
840 * pDownmixer handle to instance data
841 * param parameter
842 * pValue pointer to variable to hold retrieved value
843 * pSize pointer to value size: maximum size as input
844 *
845 * Outputs:
846 * *pValue updated with parameter value
847 * *pSize updated with actual value size
848 *
849 * Returns:
850 * 0 indicates success
851 *
852 * Side Effects:
853 *
854 *----------------------------------------------------------------------------
855 */
Andy Hung47068792021-05-18 16:28:32 -0700856static int Downmix_getParameter(
857 downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800858 int16_t *pValue16;
859
860 switch (param) {
861
862 case DOWNMIX_PARAM_TYPE:
863 if (*pSize < sizeof(int16_t)) {
Andy Hung47068792021-05-18 16:28:32 -0700864 ALOGE("Downmix_getParameter invalid parameter size %u for DOWNMIX_PARAM_TYPE", *pSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800865 return -EINVAL;
866 }
867 pValue16 = (int16_t *)pValue;
868 *pValue16 = (int16_t) pDownmixer->type;
869 *pSize = sizeof(int16_t);
Andy Hung47068792021-05-18 16:28:32 -0700870 ALOGV("Downmix_getParameter DOWNMIX_PARAM_TYPE is %d", *pValue16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800871 break;
872
873 default:
Andy Hung47068792021-05-18 16:28:32 -0700874 ALOGE("Downmix_getParameter unknown parameter %d", param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800875 return -EINVAL;
876 }
877
878 return 0;
879} /* end Downmix_getParameter */
880
881
882/*----------------------------------------------------------------------------
883 * Downmix_foldFromQuad()
884 *----------------------------------------------------------------------------
885 * Purpose:
886 * downmix a quad signal to stereo
887 *
888 * Inputs:
889 * pSrc quad audio samples to downmix
890 * numFrames the number of quad frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700891 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
892 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800893 *
894 * Outputs:
895 * pDst downmixed stereo audio samples
896 *
897 *----------------------------------------------------------------------------
898 */
Ramesh Katurid7d01342016-05-02 15:03:47 +0530899void Downmix_foldFromQuad(LVM_FLOAT *pSrc, LVM_FLOAT *pDst, size_t numFrames, bool accumulate) {
900 // sample at index 0 is FL
901 // sample at index 1 is FR
902 // sample at index 2 is RL
903 // sample at index 3 is RR
904 if (accumulate) {
905 while (numFrames) {
906 // FL + RL
907 pDst[0] = clamp_float(pDst[0] + ((pSrc[0] + pSrc[2]) / 2.0f));
908 // FR + RR
909 pDst[1] = clamp_float(pDst[1] + ((pSrc[1] + pSrc[3]) / 2.0f));
910 pSrc += 4;
911 pDst += 2;
912 numFrames--;
913 }
914 } else { // same code as above but without adding and clamping pDst[i] to itself
915 while (numFrames) {
916 // FL + RL
917 pDst[0] = clamp_float((pSrc[0] + pSrc[2]) / 2.0f);
918 // FR + RR
919 pDst[1] = clamp_float((pSrc[1] + pSrc[3]) / 2.0f);
920 pSrc += 4;
921 pDst += 2;
922 numFrames--;
923 }
924 }
925}
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800926
927/*----------------------------------------------------------------------------
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800928 * Downmix_foldFrom5Point1()
929 *----------------------------------------------------------------------------
930 * Purpose:
931 * downmix a 5.1 signal to stereo
932 *
933 * Inputs:
934 * pSrc 5.1 audio samples to downmix
935 * numFrames the number of 5.1 frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700936 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
937 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800938 *
939 * Outputs:
940 * pDst downmixed stereo audio samples
941 *
942 *----------------------------------------------------------------------------
943 */
Ramesh Katurid7d01342016-05-02 15:03:47 +0530944void Downmix_foldFrom5Point1(LVM_FLOAT *pSrc, LVM_FLOAT *pDst, size_t numFrames, bool accumulate) {
945 LVM_FLOAT lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
946 // sample at index 0 is FL
947 // sample at index 1 is FR
948 // sample at index 2 is FC
949 // sample at index 3 is LFE
950 // sample at index 4 is RL
951 // sample at index 5 is RR
952 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
953 // for every sample
954 if (accumulate) {
955 while (numFrames) {
956 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
957 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
958 + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
959 // FL + centerPlusLfeContrib + RL
960 lt = pSrc[0] + centerPlusLfeContrib + pSrc[4];
961 // FR + centerPlusLfeContrib + RR
962 rt = pSrc[1] + centerPlusLfeContrib + pSrc[5];
963 // accumulate in destination
964 pDst[0] = clamp_float(pDst[0] + (lt / 2.0f));
965 pDst[1] = clamp_float(pDst[1] + (rt / 2.0f));
966 pSrc += 6;
967 pDst += 2;
968 numFrames--;
969 }
970 } else { // same code as above but without adding and clamping pDst[i] to itself
971 while (numFrames) {
972 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
973 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
974 + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
975 // FL + centerPlusLfeContrib + RL
976 lt = pSrc[0] + centerPlusLfeContrib + pSrc[4];
977 // FR + centerPlusLfeContrib + RR
978 rt = pSrc[1] + centerPlusLfeContrib + pSrc[5];
979 // store in destination
980 pDst[0] = clamp_float(lt / 2.0f); // differs from when accumulate is true above
981 pDst[1] = clamp_float(rt / 2.0f); // differs from when accumulate is true above
982 pSrc += 6;
983 pDst += 2;
984 numFrames--;
985 }
986 }
987}
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800988
989/*----------------------------------------------------------------------------
990 * Downmix_foldFrom7Point1()
991 *----------------------------------------------------------------------------
992 * Purpose:
993 * downmix a 7.1 signal to stereo
994 *
995 * Inputs:
996 * pSrc 7.1 audio samples to downmix
997 * numFrames the number of 7.1 frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700998 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
999 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -08001000 *
1001 * Outputs:
1002 * pDst downmixed stereo audio samples
1003 *
1004 *----------------------------------------------------------------------------
1005 */
Ramesh Katurid7d01342016-05-02 15:03:47 +05301006void Downmix_foldFrom7Point1(LVM_FLOAT *pSrc, LVM_FLOAT *pDst, size_t numFrames, bool accumulate) {
1007 LVM_FLOAT lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
1008 // sample at index 0 is FL
1009 // sample at index 1 is FR
1010 // sample at index 2 is FC
1011 // sample at index 3 is LFE
1012 // sample at index 4 is RL
1013 // sample at index 5 is RR
1014 // sample at index 6 is SL
1015 // sample at index 7 is SR
1016 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
1017 // for every sample
1018 if (accumulate) {
1019 while (numFrames) {
1020 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
Andy Hunged1172f2018-12-21 16:20:03 -08001021 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
1022 + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
Ramesh Katurid7d01342016-05-02 15:03:47 +05301023 // FL + centerPlusLfeContrib + SL + RL
1024 lt = pSrc[0] + centerPlusLfeContrib + pSrc[6] + pSrc[4];
1025 // FR + centerPlusLfeContrib + SR + RR
1026 rt = pSrc[1] + centerPlusLfeContrib + pSrc[7] + pSrc[5];
1027 //accumulate in destination
1028 pDst[0] = clamp_float(pDst[0] + (lt / 2.0f));
1029 pDst[1] = clamp_float(pDst[1] + (rt / 2.0f));
1030 pSrc += 8;
1031 pDst += 2;
1032 numFrames--;
1033 }
1034 } else { // same code as above but without adding and clamping pDst[i] to itself
1035 while (numFrames) {
1036 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
1037 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
1038 + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
1039 // FL + centerPlusLfeContrib + SL + RL
1040 lt = pSrc[0] + centerPlusLfeContrib + pSrc[6] + pSrc[4];
1041 // FR + centerPlusLfeContrib + SR + RR
1042 rt = pSrc[1] + centerPlusLfeContrib + pSrc[7] + pSrc[5];
1043 // store in destination
1044 pDst[0] = clamp_float(lt / 2.0f); // differs from when accumulate is true above
1045 pDst[1] = clamp_float(rt / 2.0f); // differs from when accumulate is true above
1046 pSrc += 8;
1047 pDst += 2;
1048 numFrames--;
1049 }
1050 }
1051}
Andy Hungca1c4e42021-05-06 22:32:02 -07001052
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001053/*----------------------------------------------------------------------------
1054 * Downmix_foldGeneric()
1055 *----------------------------------------------------------------------------
1056 * Purpose:
1057 * downmix to stereo a multichannel signal whose format is:
1058 * - has FL/FR
1059 * - if using AUDIO_CHANNEL_OUT_SIDE*, it contains both left and right
1060 * - if using AUDIO_CHANNEL_OUT_BACK*, it contains both left and right
1061 * - doesn't use any of the AUDIO_CHANNEL_OUT_TOP* channels
1062 * - doesn't use any of the AUDIO_CHANNEL_OUT_FRONT_*_OF_CENTER channels
1063 * Only handles channel masks not enumerated in downmix_input_channel_mask_t
1064 *
1065 * Inputs:
1066 * mask the channel mask of pSrc
1067 * pSrc multichannel audio buffer to downmix
1068 * numFrames the number of multichannel frames to downmix
1069 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
1070 * or overwrite pDst (when false)
1071 *
1072 * Outputs:
1073 * pDst downmixed stereo audio samples
1074 *
1075 * Returns: false if multichannel format is not supported
1076 *
1077 *----------------------------------------------------------------------------
1078 */
Ramesh Katurid7d01342016-05-02 15:03:47 +05301079bool Downmix_foldGeneric(
1080 uint32_t mask, LVM_FLOAT *pSrc, LVM_FLOAT *pDst, size_t numFrames, bool accumulate) {
1081
1082 if (!Downmix_validChannelMask(mask)) {
1083 return false;
1084 }
1085
1086 const bool hasSides = (mask & kSides) != 0;
1087 const bool hasBacks = (mask & kBacks) != 0;
1088
1089 const int numChan = audio_channel_count_from_out_mask(mask);
1090 const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
1091 const bool hasLFE =
1092 ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
1093 const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
1094 // compute at what index each channel is: samples will be in the following order:
1095 // FL FR FC LFE BL BR BC SL SR
1096 // when a channel is not present, its index is set to the same as the index of the preceding
1097 // channel
1098 const int indexFC = hasFC ? 2 : 1; // front center
1099 const int indexLFE = hasLFE ? indexFC + 1 : indexFC; // low frequency
1100 const int indexBL = hasBacks ? indexLFE + 1 : indexLFE; // back left
1101 const int indexBR = hasBacks ? indexBL + 1 : indexBL; // back right
1102 const int indexBC = hasBC ? indexBR + 1 : indexBR; // back center
1103 const int indexSL = hasSides ? indexBC + 1 : indexBC; // side left
1104 const int indexSR = hasSides ? indexSL + 1 : indexSL; // side right
1105
1106 LVM_FLOAT lt, rt, centersLfeContrib;
1107 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
1108 // for every sample
1109 if (accumulate) {
1110 while (numFrames) {
1111 // compute contribution of FC, BC and LFE
1112 centersLfeContrib = 0;
1113 if (hasFC) { centersLfeContrib += pSrc[indexFC]; }
1114 if (hasLFE) { centersLfeContrib += pSrc[indexLFE]; }
1115 if (hasBC) { centersLfeContrib += pSrc[indexBC]; }
1116 centersLfeContrib *= MINUS_3_DB_IN_FLOAT;
1117 // always has FL/FR
1118 lt = pSrc[0];
1119 rt = pSrc[1];
1120 // mix in sides and backs
1121 if (hasSides) {
1122 lt += pSrc[indexSL];
1123 rt += pSrc[indexSR];
1124 }
1125 if (hasBacks) {
1126 lt += pSrc[indexBL];
1127 rt += pSrc[indexBR];
1128 }
1129 lt += centersLfeContrib;
1130 rt += centersLfeContrib;
1131 // accumulate in destination
1132 pDst[0] = clamp_float(pDst[0] + (lt / 2.0f));
1133 pDst[1] = clamp_float(pDst[1] + (rt / 2.0f));
1134 pSrc += numChan;
1135 pDst += 2;
1136 numFrames--;
1137 }
1138 } else {
1139 while (numFrames) {
1140 // compute contribution of FC, BC and LFE
1141 centersLfeContrib = 0;
1142 if (hasFC) { centersLfeContrib += pSrc[indexFC]; }
1143 if (hasLFE) { centersLfeContrib += pSrc[indexLFE]; }
1144 if (hasBC) { centersLfeContrib += pSrc[indexBC]; }
1145 centersLfeContrib *= MINUS_3_DB_IN_FLOAT;
1146 // always has FL/FR
1147 lt = pSrc[0];
1148 rt = pSrc[1];
1149 // mix in sides and backs
1150 if (hasSides) {
1151 lt += pSrc[indexSL];
1152 rt += pSrc[indexSR];
1153 }
1154 if (hasBacks) {
1155 lt += pSrc[indexBL];
1156 rt += pSrc[indexBR];
1157 }
1158 lt += centersLfeContrib;
1159 rt += centersLfeContrib;
1160 // store in destination
1161 pDst[0] = clamp_float(lt / 2.0f); // differs from when accumulate is true above
1162 pDst[1] = clamp_float(rt / 2.0f); // differs from when accumulate is true above
1163 pSrc += numChan;
1164 pDst += 2;
1165 numFrames--;
1166 }
1167 }
1168 return true;
1169}